# MTK -- how to set initial unknowns introduced by MTK?

**URL:** <https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832>\
**Category:** Modelling & Simulations\
**Created:** [August 5, 2024, 8:18am UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832 "2024-08-05T08:18:26Z")\
**Posts on this page:** 7\
**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:** [August 5, 2024, 8:18am UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/1 "2024-08-05T08:18:26Z")

</div>

I’m converting a model from Modelica to ModelingToolkit in Julia. Modelica is somewhat more flexible wrt. specifying initial values for unknowns, so I have to change which initial unknowns I specify. Here is the model printout from `@mtkbuild`:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/c/1cedfe8902ccde47a69a45df4c9bb25bebee75e9.png)

and here are the unknowns in MTK:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/f/5fb634ab02bc96ce37b2e082b0a00d99d351a3bb.png)

Of these 7 unknowns, the 5 first have a physical meaning, and I have specified their initial values as default values for the variables in my model.

However, the two last ones seem to be variables that MTK has introduced… I have variables `Vd_d` and `v_i` in my code, but not `Vd_d_t` and `v_i_t`. My problem is two-fold:

- I don’t really know what values I should give them, and am inclined to try with zero. (How is `Vd_d` related to `Vd_d_t`??)
- I don’t know how to type the variable names… I tried with, e.g., `Vd_d_t` and `Vd_dₜ`, but that doesn’t work:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc1b851dd9704972c2ac1fbfaedfc4e54ca80751.png)

Checking the list of unknowns, I observe that  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/f/cfeba7ee649f88eae5bb352b01e3ce8691ab7f98.png)

looks different than `Vd_d_t`, i.e., the last “underscore” is different.

**Question** :

1. How can I typeset the two extra variables introduced by MTK when I specify the initial values in the `ODEProblem`?
2. How should I interpret `Vd_d_t` (or whatever) vs. `Vd_d`?

---

<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:** [August 5, 2024, 12:09pm UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/2 "2024-08-05T12:09:35Z")

</div>

> [@BLI](#):
>
> How can I typeset the two extra variables introduced by MTK when I specify the initial values in the `ODEProblem`?

Don’t type set anything. Just use `D(Vd_d)`.

> [@BLI](#):
>
> 1. How should I interpret `Vd_d_t` (or whatever) vs. `Vd_d`?

Dummy derivative. Equivalent to the derivative.

---

<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:** [August 5, 2024, 12:36pm UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/3 "2024-08-05T12:36:51Z")

</div>

Hm. Above, I listed the model as:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/758f00b2e1d8c1be29ecba8f6df860f034bc26ab.png)

and the unknowns as:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c0e77d47b76f1ebe5c7732c099ecc79e06f6824.png)

In the past (I think…), it was necessary to specify initial values for all the unknowns.

I tried to do [I use `Dt` for derivative because I use `D` for diameter…]:

```julia
julia> prob_sys = ODEProblem(hps, [Dt(Vd_d)=>0, Dt(v_i)=>0], tspan)
UndefVarError: `Vd_d` not defined

Stacktrace:
 [1] top-level scope
...

```

Then I tried with:

```julia
julia> prob_sys = ODEProblem(hps, [], tspan)
 Warning: Initialization system is overdetermined. 3 equations for 2 unknowns. Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true
└ @ ModelingToolkit ...
...
ArgumentError: Any[p_d1(0)] are either missing from the variable map or missing from the system's unknowns/parameters list.

Stacktrace:

```

I don’t understand the error message wrt. overdetermined and 3 equations for 2 unknowns…

Is this the 3 algebraic equations above? I have specified variable `w`, so that perhaps that is the problem??

Also, I don’t understand why there should be a problem with `p_d1(0)`…

---

<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:** [August 5, 2024, 1:07pm UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/4 "2024-08-05T13:07:58Z")

</div>

> [@BLI](#):
>
> Is this the 3 algebraic equations above? I have specified variable `w`, so that perhaps that is the problem??

You didn’t share the stack trace, is that in the initialization?

---

<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:** [August 5, 2024, 1:17pm UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/5 "2024-08-05T13:17:29Z")

</div>

OK… I’ll share the stack traces:

```julia
julia> prob_sys = ODEProblem(hps, [], tspan)
Warning: Initialization system is overdetermined. 3 equations for 2 unknowns. Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true
└ @ ModelingToolkit C:\Users\Bernt\.julia\packages\ModelingToolkit\jEIdO\src\systems\diffeqs\abstractodesystem.jl:1547

{
	"name": "ArgumentError",
	"message": "ArgumentError: Any[p_d1(0)] are either missing from the variable map or missing from the system's unknowns/parameters list.",
	"stack": "ArgumentError: Any[p_d1(0)] are either missing from the variable map or missing from the system's unknowns/parameters list.\n\nStacktrace:\n [1] throw_missingvars_in_sys(vars::Vector{Any})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\utils.jl:657\n [2] promote_to_concrete(vs::Vector{Any}; tofloat::Bool, use_union::Bool)\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\utils.jl:676\n [3] varmap_to_vars(varmap::Dict{Any, Any}, varlist::Vector{SymbolicUtils.BasicSymbolic{Real}}; defaults::Dict{Any, Any}, check::Bool, toterm::Function, promotetoconcrete::Nothing, tofloat::Bool, use_union::Bool)\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\variables.jl:170\n [4] get_u0(sys::NonlinearSystem, u0map::Dict{Any, Any}, parammap::Vector{Pair{SymbolicUtils.BasicSymbolic{Real}, Int64}}; symbolic_u0::Bool, toterm::Function)\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:742\n [5] get_u0\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:719 [inlined]\n [6] process_NonlinearProblem(constructor::Type, sys::NonlinearSystem, u0map::Dict{Any, Any}, parammap::Vector{Pair{SymbolicUtils.BasicSymbolic{Real}, Int64}}; version::Nothing, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, eval_module::Module, use_union::Bool, tofloat::Bool, kwargs::@Kwargs{check_length::Bool})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\nonlinear\\nonlinearsystem.jl:392\n [7] process_NonlinearProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\nonlinear\\nonlinearsystem.jl:377 [inlined]\n [8] (SciMLBase.NonlinearLeastSquaresProblem{true})(sys::NonlinearSystem, u0map::Dict{Any, Any}, parammap::Vector{Pair{SymbolicUtils.BasicSymbolic{Real}, Int64}}; check_length::Bool, kwargs::@Kwargs{eval_expression::Bool, eval_module::Module})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\nonlinear\\nonlinearsystem.jl:459\n [9] NonlinearLeastSquaresProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\nonlinear\\nonlinearsystem.jl:453 [inlined]\n [10] #NonlinearLeastSquaresProblem#589\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\nonlinear\\nonlinearsystem.jl:450 [inlined]\n [11] NonlinearLeastSquaresProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\nonlinear\\nonlinearsystem.jl:449 [inlined]\n [12] ModelingToolkit.InitializationProblem{true, SciMLBase.AutoSpecialize}(sys::ODESystem, t::Int64, u0map::Dict{Any, Any}, parammap::SciMLBase.NullParameters; guesses::Dict{Any, Any}, check_length::Bool, warn_initialize_determined::Bool, initialization_eqs::Vector{Any}, fully_determined::Bool, kwargs::@Kwargs{eval_expression::Bool, eval_module::Module})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1567\n [13] InitializationProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1513 [inlined]\n [14] #_#824\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1491 [inlined]\n [15] InitializationProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1490 [inlined]\n [16] #InitializationProblem#822\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1479 [inlined]\n [17] InitializationProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1478 [inlined]\n [18] process_DEProblem(constructor::Type, sys::ODESystem, u0map::Vector{Any}, parammap::SciMLBase.NullParameters; implicit_dae::Bool, du0map::Nothing, version::Nothing, tgrad::Bool, jac::Bool, checkbounds::Bool, sparse::Bool, simplify::Bool, linenumbers::Bool, parallel::Symbolics.SerialForm, eval_expression::Bool, eval_module::Module, use_union::Bool, tofloat::Bool, symbolic_u0::Bool, u0_constructor::typeof(identity), guesses::Dict{Any, Any}, t::Int64, warn_initialize_determined::Bool, build_initializeprob::Bool, initialization_eqs::Vector{Any}, fully_determined::Bool, kwargs::@Kwargs{check_length::Bool})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:837\n [19] process_DEProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:747 [inlined]\n [20] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Int64, Int64}, parammap::SciMLBase.NullParameters; callback::Nothing, check_length::Bool, warn_initialize_determined::Bool, eval_expression::Bool, eval_module::Module, kwargs::@Kwargs{})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:1005\n [21] ODEProblem\n @ C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:993 [inlined]\n [22] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Int64, Int64})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:993\n [23] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:980\n [24] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:979\n [25] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:969\n [26] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any})\n @ ModelingToolkit C:\\Users\\Bernt\\.julia\\packages\\ModelingToolkit\\jEIdO\\src\\systems\\diffeqs\\abstractodesystem.jl:968\n [27] top-level scope\n @ c:\\Users\\Bernt\\OneDrive\\Documents\\researchUSN\\Supervisor\\MScThesis\\Fall2023\\jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_X30sZmlsZQ==.jl: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:** [August 5, 2024, 1:21pm UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/6 "2024-08-05T13:21:01Z")

</div>

… and for the other case:

```julia
julia> prob_sys = ODEProblem(hps, [Dt(Vd_d)=>0, Dt(v_i)=>0], tspan)
UndefVarError: `Vd_d` not defined

Stacktrace:
 [1] top-level scope
   @ c:\Users\Bernt\OneDrive\Documents\researchUSN\Supervisor\MScThesis\Fall2023\jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_X32sZmlsZQ==.jl: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:** [August 6, 2024, 9:36am UTC](https://discourse.julialang.org/t/mtk-how-to-set-initial-unknowns-introduced-by-mtk/117832/7 "2024-08-06T09:36:17Z")

</div>

OK – I found the problem… I needed to change…

```julia
prob_sys = ODEProblem(hps, [Dt(Vd_d)=>0, Dt(v_i)=>0], tspan)

```

into:

```julia
prob_sys = ODEProblem(hps, [Dt(hps.Vd_d)=>0, Dt(hps.v_i)=>0], tspan)

```

I guess my confusion lies in doing:

```julia
julia> unknowns(hps)
7-element Vector{SymbolicUtils.BasicSymbolic{Real}}:
 v_s(t)
 m_s(t)
 K_a(t)
 v_d(t)
 w(t)
 Vd_dˍt(t)
 v_iˍt(t)

```

i.e., here, the unknowns are listed _without_ the prefix `hps.`

One more problem I ran into… following the MTK _tutorial_, I imported the solvers from `DifferentialEquations.jl` by:

```julia
using DifferentialEquations: solve

```

However, function `solve` does not seem to find the _algorithms_…

```julia
julia> sol = solve(prob_sys, Rodas5())
UndefVarError: `Rodas5` not defined

Stacktrace:
 [1] top-level scope
   @ c:\Users\Bernt\OneDrive
...

```

However, if I do:

```julia
using DifferentialEquations

```

then the code works.
