# Optional parameter assignment map for modelingtoolkit system with defaults

**URL:** https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [September 1, 2023, 8:46am UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437 "2023-09-01T08:46:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)
#### Post date: [September 1, 2023, 8:46am UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/1 "2023-09-01T08:46:06Z")

</div>

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

```julia
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…

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 1, 2023, 10:48am UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/2 "2023-09-01T10:48:48Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)
#### Post date: [September 1, 2023, 11:19am UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/3 "2023-09-01T11:19:47Z")

</div>

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

```julia
fun(sys,p2=0.4)

```

or

```julia
fun(sys,p3=4)

```

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

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 1, 2023, 11:42am UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/4 "2023-09-01T11:42:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)
#### Post date: [September 1, 2023, 11:57am UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/5 "2023-09-01T11:57:25Z")

</div>

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

```julia
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

```julia
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?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 1, 2023, 1:24pm UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/6 "2023-09-01T13:24:55Z")

</div>

> [@Jon\_Norberg](#):
>
> Is this how you meant?

yes

> [@Jon\_Norberg](#):
>
> Or are the parameters global so I just set p1=\>0.3 anywhere and its going to be reflected when I solve the ode?

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.

---

<div class="post-metadata">

### Author: ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)
#### Post date: [September 1, 2023, 2:27pm UTC](https://discourse.julialang.org/t/optional-parameter-assignment-map-for-modelingtoolkit-system-with-defaults/103437/7 "2023-09-01T14:27:55Z")

</div>

thanks!
