# How to tell ModelingToolkit that two variables have the same initial conditions without computing them

**URL:** <https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999>\
**Category:** Modelling & Simulations\
**Tags:** question, modelingtoolkit\
**Created:** [September 29, 2022, 5:18pm UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999 "2022-09-29T17:18:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![natema](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@natema](https://discourse.julialang.org/u/natema)\
**Post date:** [September 29, 2022, 5:18pm UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/1 "2022-09-29T17:18:32Z")

</div>

Suppose that we have a differential-algebraic system of equations, with two variables f(t) and g(t) such that f(0) = g(0). Suppose moreover that g(t) is algebraic and g(0) could be explicitly calculated.  
Is it possible to avoid the latter calculation and let MTK figure it out by itself?

Here is a MWE of what we are trying to do, which returns an error:

```nohighlight
using ModelingToolkit, DifferentialEquations

@variables t
D = Differential(t)

function system_f(; name)
    @variables f(t) g(t)
    eqs = [D(f) ~ g - f]
    ODESystem(eqs; name)
end

function system_g(; name)
    @variables g(t)
    eqs = [g ~ t]
    ODESystem(eqs; name)
end

@named f = system_f()
@named g = system_g()

connected_eqs = [
    f.g ~ g.g,
]

@named _model = ODESystem(connected_eqs, t)
@named model = compose(_model, [f, g])

model = structural_simplify(model)
prob = ODEProblem(model, [f.f => g.g], (0.0, 10.0))
sol = solve(prob)

```

In the above example, we have that g(t)=t, thus f(0)=g(0)=0.  
When g(t) is much more complicated, is there a way to avoid having to provide g(0) to MTK explicitly?

---

<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 29, 2022, 10:01pm UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/2 "2022-09-29T22:01:24Z")

</div>

Define a parameter and make both initial conditions equal to that parameter.

---

<div class="post-metadata">

**Author:** ![paulobruno](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@paulobruno](https://discourse.julialang.org/u/paulobruno)\
**Post date:** [September 30, 2022, 7:59am UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/3 "2022-09-30T07:59:21Z")

</div>

Hi Chris, thanks for your answer.

Regarding your proposal, how can we set the parameter value to be dependent on g(t\_0)?

The initial value of g is automatically computed and dependent on t\_0 (from the timespan passed to ODEProblem), thus we cannot assign a constant value to it.  
Since we don’t need to provide an initial value of g, we also don’t want to provide an initial value to f (or to a parameter p), because it can be clearly derived from the problem.

---

<div class="post-metadata">

**Author:** ![natema](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@natema](https://discourse.julialang.org/u/natema)\
**Post date:** [October 3, 2022, 1:22pm UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/4 "2022-10-03T13:22:56Z")

</div>

One -quite inelegant- way I’m managing to address the problem is to solve the system 2 times, where the first time is merely used to get the initial value of g, as follows:

- first set both initial conditions equal to an arbitrary value
- solve the system, getting the solution `pre_sol`,
- solve again the system, this time with initial condition f(0)=\text{pre\_sol}.g(0)

For example, here are the lines to substitute to the last two ones in the initial MWE:

```nohighlight
pre_prob = ODEProblem(model, [f.f=>42,g.g=>42], (0.0, 10.0)) 
pre_sol = solve(pre_prob)
prob = ODEProblem(model, [f.f=>pre_sol[g.g, 1],g.g=>pre_sol[g.g, 1]], (0.0, 10.0))
sol = solve(prob)

```

Of course, I suppose that there’s a more direct solution.

---

<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:** [October 22, 2022, 8:22am UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/5 "2022-10-22T08:22:15Z")

</div>

Open an issue.

---

<div class="post-metadata">

**Author:** ![natema](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@natema](https://discourse.julialang.org/u/natema)\
**Post date:** [October 30, 2022, 4:55pm UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/6 "2022-10-30T16:55:16Z")

</div>

Opened: [Solving a system with initial condition of the form $f(0) = g(0)$ · Issue #1911 · SciML/ModelingToolkit.jl · GitHub](https://github.com/SciML/ModelingToolkit.jl/issues/1911)

---

<div class="post-metadata">

**Author:** ![natema](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@natema](https://discourse.julialang.org/u/natema)\
**Post date:** [November 12, 2022, 2:46pm UTC](https://discourse.julialang.org/t/how-to-tell-modelingtoolkit-that-two-variables-have-the-same-initial-conditions-without-computing-them/87999/7 "2022-11-12T14:46:48Z")

</div>

Just a small update: we added [a simpler MWE in the MTK issue](https://github.com/SciML/ModelingToolkit.jl/issues/1911#issuecomment-1312483682).  
We’ve been hitting this limitation again lately, in [trying to port some famous models from Vensim to a Julia library](https://github.com/worlddynamics/WorldDynamics.jl/issues/141).
