Extracting base symbol from array of symbols

I am having trouble creating a dictionary from symbols to numbers for defining an ODEProblem. In essence, the problem boils down to having to extract the “base” symbol from a symbolic array. My code is structured in such a way that the function as input gets an ODESystem from ModelingToolkit.jl. So I need to be able to make the symbol => value map using only this system.

A sketch of this issue can be seen as follows. It considers two modules, one that creates the system, and one that should create and ODEProblem from it to simulate it using DifferentialEquations. The specific system at hand does not matter, so I just use a simple linear system for now.

# create_sys.jl
function create_system(S)
    @variables t
    @variables (x(t))[1:S]
    @variables a[1:S] b[1:S]
    D = Differential(t)
   
    eqns = [D(x[i]) ~ a[i]*x[i] + b[i] for i in 1:S] 
    @named odesys = ODESystem(eqns, t)
    return odesys
end

and then the function that I do not know how to write

# create_prob.jl
function create_problem(system, X0, avals, bvals, tspan)
    params = parameters(system)
    # Filter parameters and assign [a[i] => avals[i] ...] and [b[i] => bvals[i] ...]
    # [...]
    # pmap = ...
    prob = ODEProblem(complete(system), X0, tspan, pmap)
end

I am aware of the Symbolics.arguments(...) function, which could potentially be used to filter, but it returns a SymbolicUtils.BasicSymbolic{Matrix{Real}} for some reason, and I do not know how to do something like, e.g., :r == argument(symbol) ? ... : ...

Any help on this topic is greatly appreciated.

I just figured out that I can use the @unpack macro, possibly in combination with the hasproperty(odesys, :a) command, to get the parameters in question.

1 Like