Optional parameter assignment map for modelingtoolkit system with defaults

Hi, how would I make the parameter map for running a modelingtoolkit system using an optional defaults approach like

fun(sys::ODEsystem; p1=1,p2=0.2,p3=0.47)
   # Goal: end up with an assignment map that uses the defaults in the function header which can be passed to ODEProblem
   # I tried these:
    p=[p1=>p1,p2=>p2,p3=>p3] # this does not work
    p=[eval(Symbol(x)) for x in parameters(sys)] # Neither does this

    u0=[X=>0]

    prob=ODEProblem(sys,u0,tspan,p);

    return solve(prob,Tsit5(),saveat=1.0) 
end

*Note: one needs a map [x=>value] since the order of the parameters is not easily predictable unless you iterate through parameters(sys) as I did here (but apparently I am somehow not able to get the value from the defaults given in the function header…

The parameter maps already do this by default if a default is set on p1 etc. I don’t quite get the use case here?

Use case is mostly user friendliness, run the model by only changing one parameter, not having to send in the whole parameter map:

fun(sys,p2=0.4)

or

fun(sys,p3=4)

Or did I misunderstand what you mean by “The parameter maps already do this by default”

If you set @variables p1=0.4 then it’s respected when left out.

Ah, ok! so you’d define default values when you create the ODESystem as you say, and then you just use the parameter map for any overriding

fun(sys::ODEsystem; p=[])


    u0=[X=>0]

    prob=ODEProblem(sys,u0,tspan,p);

    return solve(prob,Tsit5(),saveat=1.0) 
end

so I could then just do

fun(sys,[p1=>0.6])

Is this how you meant? Or are the parameters global so I just set p1=>0.3 anywhere and its going to be reflected when I solve the ode?

yes

The variables are just Julia variables, so they are global only if you make them global. But the default is tagged metadata, so if you use that variable it will bring along its metadata.

thanks!