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

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:

and here are the unknowns in MTK:
image

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:

Checking the list of unknowns, I observe that
image

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?

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

Dummy derivative. Equivalent to the derivative.

1 Like

Hm. Above, I listed the model as:

and the unknowns as:
image

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> 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> 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)

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

1 Like

OK… I’ll share the stack traces:

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"
}

… and for the other case:

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

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

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

into:

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

I guess my confusion lies in doing:

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:

using DifferentialEquations: solve

However, function solve does not seem to find the algorithms

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

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

However, if I do:

using DifferentialEquations

then the code works.