# MTK programmatic populating initial guess values

**URL:** <https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912>\
**Category:** Modelling & Simulations\
**Created:** [April 28, 2026, 7:22pm UTC](https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912 "2026-04-28T19:22:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark.garnett](https://avatars.discourse-cdn.com/v4/letter/m/b9bd4f/32.png) [@mark.garnett](https://discourse.julialang.org/u/mark.garnett)\
**Post date:** [April 28, 2026, 7:22pm UTC](https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912/1 "2026-04-28T19:22:28Z")

</div>

Hi all, this should be really easy but I can’t quite figure it out.  
When you create an MTK system, and want to solve it as a NonlinearProblem, ODEProblem, or whatever, you often need provide an initial guess or initial conditions.

Not a MWE here, just some generic whiteboard code

```julia-auto
sys = mtkcompile(_sys)
guess = [pump.port_b.m_dot => -100.0, pipe.port_a.p => 10.0, some_valve.port_a.m_dot => 1.0]
# and so on with more guess values

prob = NonlinearProblem(sys, guess)
sol = solve(prob)

```

what I’d like to do is be able to iterate in a loop building the guess vector from strings, as I know what is in the system. Something like this:

```julia-auto
for i in N_valves
push!(guesses, Pair(Symbol("valve_$i.port_a.m_dot"), 33)) 
push!(guesses, Pair(Symbol("valve_$i.port_a.p"), 42)) 
# exact guess values don't matter here
end

```

However, the type of the guess array is

```julia-auto
typeof(guesses)
Vector{Pair{Num, Float64}}

```

The type of the guess pair vectors doesn’t say anything about a Symbol, and the above for loop doesn’t work. Num is supposed to be an abstract type, although displaying the guess vector shows the names and guess value

```julia-auto
julia> guesses
3-element Vector{Pair{Num, Float64}}:
   pump₊port_b₊m_dot(t) => -100.0
   pipe₊port_a₊p(t) => 10.0
 some_valve₊port_a₊m_dot(t) => 1.0

```

So that is puzzling.

---

<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:** [April 28, 2026, 9:15pm UTC](https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912/2 "2026-04-28T21:15:13Z")

</div>

@Oscar_Smith @cryptic.ax may have comments here.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 29, 2026, 4:05am UTC](https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912/3 "2026-04-29T04:05:03Z")

</div>

See [MTK string parsing](https://docs.sciml.ai/ModelingToolkit/stable/API/System/#String-parsing). The docs are broken, but it should point you to the right functions

---

<div class="post-metadata">

**Author:** ![cryptic.ax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cryptic.ax/32/220540_2.png) [@cryptic.ax](https://discourse.julialang.org/u/cryptic.ax)\
**Post date:** [April 29, 2026, 6:09am UTC](https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912/4 "2026-04-29T06:09:41Z")

</div>

> what I’d like to do is be able to iterate in a loop building the guess vector from strings, as I know what is in the system.

We allow specifying `guesses` as a vector of pairs, but it’s actually a `Dict`. I’d recommend doing

```julia
guess = Dict{Symbolics.SymbolicT, Symbolics.SymbolicT}()
for i in N_valves
  var = getproperty(sys, Symbol(:valve_, i)).port_a.m_dot
  guess[var] = 33
end

```

The same pattern applies for initial conditions. If you need more flexibility in getting the name, `var = ModelingToolkit.parse_variable(sys, "valve_$i.port_a.m_dot")` should work.

> The type of the guess pair vectors doesn’t say anything about a Symbol

Symbolic variables are not `Symbol`s.

> `Num` is supposed to be an abstract type

`Num` is a concrete type.

When using `getproperty` to access variables from systems, you will get one of a couple different types depending on the variable that was accessed (whether it’s a scalar or array, etc.). `Num` is one of those types. All these types are very thin wrappers over `Symbolics.SymbolicT`, and automatically convert to it. Thus the above approach is likely the best way to do this.

---

<div class="post-metadata">

**Author:** ![mark.garnett](https://avatars.discourse-cdn.com/v4/letter/m/b9bd4f/32.png) [@mark.garnett](https://discourse.julialang.org/u/mark.garnett)\
**Post date:** [April 29, 2026, 3:36pm UTC](https://discourse.julialang.org/t/mtk-programmatic-populating-initial-guess-values/136912/5 "2026-04-29T15:36:34Z")

</div>

Thank you for the solution, this works. Thank you all for replying! And yes I was confusing “Number” and “Num”.
