# MTK & efficient formulation

**URL:** <https://discourse.julialang.org/t/mtk-efficient-formulation/103971>\
**Category:** Modelling & Simulations\
**Created:** [September 18, 2023, 9:19am UTC](https://discourse.julialang.org/t/mtk-efficient-formulation/103971 "2023-09-18T09:19:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 18, 2023, 9:19am UTC](https://discourse.julialang.org/t/mtk-efficient-formulation/103971/1 "2023-09-18T09:19:51Z")

</div>

I have a file with a number of models (ModelingToolkit-MTK, functions, etc.) where, say, temperature, is a _parameter_. I want to explore what happens for different parameter values (e.g., temperature). To make sure that I use the same temperature everywhere, I want to define the value in one location so that I change it once and know it is consistent throughout the code.

I’m curious about the _efficiency_ of different ways of doing this. As an example, consider a linear ODE where I use _time constant_ as parameter (instead of temperature).

```julia
# Global setting of parameter
_τ = 1
#
# Model
#
# Independent variable
@variables t
# Differentiation operator 
Dt = Differential(t)
# Parameters
@parameters τ = _τ
# Dependent variable
@variables x(t)=1
# Equation & model
eqs = [Dt(x) ~ -x/τ + sin(t)]
@named model = ODESystem(eqs)
# Time span & numeric model & solution
tspan=(0,5)
prob = ODEProblem(model,[],tspan)
sol = solve(prob)

```

OK – if I re-run the entire code above every time I change the parameter (`_τ`), things work.

- I _assume_ that the `parameters` macro makes sure that there is no type problem, i.e., that the _value_ of the global variable `_τ` is assigned to the local variable `τ` in the function that eventually is produced. I.e., so that the local variable `τ` does not depend on a global variable. (??)

An _alternative_ way to do it would be:

```julia
...
# Parameters
@parameters τ = 1
# Dependent variable
... 
_τ = 2
... 
prob = ODEProblem(model,[],tspan, [τ=>_τ])

```

OK – I think this should work, too.

- This alternative formulation is probably more efficient in that I only have to run the code generating the `prob` every time I change `τ`, i.e., I don’t re-create the symbolic model, variables, etc. every time (?)
- Are there other advantageous to this alternative way?

Finally, I could do a `remake`, I guess:

```julia
...
_τ = 2
... 
prob1 = remake(prob, p=Dict([τ=>_τ]))
...

```

which probably saves some more time.

**Questions** :

- Which of the above methods is the recommended method?

---

<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 18, 2023, 12:41pm UTC](https://discourse.julialang.org/t/mtk-efficient-formulation/103971/2 "2023-09-18T12:41:41Z")

</div>

`remake` is the recommended method.

The other thing you can do is `prob[τ] = _τ`
