# Why is ModelingToolkit simplifying this way?

**URL:** <https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872>\
**Category:** Modelling & Simulations\
**Created:** [March 31, 2023, 4:25am UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872 "2023-03-31T04:25:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [March 31, 2023, 4:25am UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872/1 "2023-03-31T04:25:13Z")

</div>

I created a pretty simple composed system:

```julia
using OrdinaryDiffEq, ModelingToolkit, Plots

@parameters t
d_dt = Differential(t)

function Cart(; init_pos, init_vel, mass, name=:cart)
	@variables pos(t)=init_pos vel(t)=init_vel
	@variables f(t)
	@parameters mass=mass

	eqs = [
		d_dt(pos) ~ vel
		d_dt(vel) ~ f / mass
	]

	return ODESystem(eqs; name)
end

function PDController(; kp=0, kd=0, name=:controller)
	@variables x(t) v(t) f(t)
	@parameters kp=kp kd=kd

	eqs = [
		f ~ -kp*x - kd*v
	]

	return ODESystem(eqs; name)
end

function ControlledCart(; cart, controller, name=:sys)
	 eqs = [
	 	cart.pos ~ controller.x
	 	cart.vel ~ controller.v
		cart.f ~ controller.f
	]
	return ODESystem(eqs; name, systems=[cart, controller])
end

cart = Cart(init_pos=0.0, init_vel=1.0, mass=0.5)
controller = PDController(kp=1.0, kd=0.5)
controlled_cart = ControlledCart(; cart, controller)

```

When I go to `structural_simplify` it with

```julia
sys = controlled_cart |> structural_simplify

```

it does it like so:

![image](https://global.discourse-cdn.com/julialang/original/3X/1/d/1d819be11ce9fb5c4e70872da4e86159dfbc6ff9.png)  
which, I guess is technically true. Trying to build the `ODEProblem` gives the error

```julia
ArgumentError: SymbolicUtils.BasicSymbolic{Real}[controller₊v(t)] are missing from the variable map.

```

which makes sense because I purposely didn’t set a default value for `controller.v` because it was supposed to just be equal to `cart.vel`, which does has a default. I could, of course, give `controller.v` a default for this simple example, but if this was a larger more complicated sim, I wouldn’t want to be wondering which version of two defaults it was going to choose.

FWIW, writing the `Controller` and `ControlledCart` equations with a `0` on the LHS, not `structural_simplify`ing it, giving default values to everything, and then solving it with a DAE solver does work as expected.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [March 31, 2023, 6:59am UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872/2 "2023-03-31T06:59:40Z")

</div>

I do not know the best practice, having never worked with ModelingToolkit, but as far as I understand, it will symbolically change your equations such that they are optimized for both performance and, perhaps more importantly, numerical stability.  
At the very least, to me this definitely sounds like it might be worth the effort of having a bit of redundancy in defining your initial conditions.

---

<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:** [March 31, 2023, 9:31am UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872/3 "2023-03-31T09:31:48Z")

</div>

> [@jonniedie](#):
>
> which makes sense because I purposely didn’t set a default value for `controller.v` because it was supposed to just be equal to `cart.vel`, which does has a default. I could, of course, give `controller.v` a default for this simple example, but if this was a larger more complicated sim, I wouldn’t want to be wondering which version of two defaults it was going to choose.

Yeah this is a piece that’s still missing. There’s already issues open on improving the initializations. For now you just need to make sure to follow the error message and define the right initializations.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [March 31, 2023, 4:43pm UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872/5 "2023-03-31T16:43:49Z")

</div>

> [@ChrisRackauckas](#):
>
> Yeah this is a piece that’s still missing. There’s already issues open on improving the initializations. For now you just need to make sure to follow the error message and define the right initializations.

Okay, cool. Thanks for the response. Is [this](https://github.com/SciML/ModelingToolkit.jl/pull/2048) the best place to track it?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [March 31, 2023, 5:01pm UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872/6 "2023-03-31T17:01:08Z")

</div>

> [@Salmon](#):
>
> At the very least, to me this definitely sounds like it might be worth the effort of having a bit of redundancy in defining your initial conditions.

In one of our real sims, there might be dozens to hundreds of submodels nested pretty deeply in the system all written by different people, so it’s a little harder to keep track of which things should be set for initialization. We could add a default for `controller.v`, but that just turns our nice helpful error into a silent unexpected value. `cart.vel` is the “real” state here and will be the one that’s user-facing as an initial condition. If they specify a value for it and a `controller.v` default supersedes it, that would be a really tricky issue to debug.

---

<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 2, 2023, 11:16am UTC](https://discourse.julialang.org/t/why-is-modelingtoolkit-simplifying-this-way/96872/7 "2023-04-02T11:16:54Z")

</div>

> [@jonniedie](#):
>
> Okay, cool. Thanks for the response. Is [this](https://github.com/SciML/ModelingToolkit.jl/pull/2048) the best place to track it?

[How to prevent breakage from changes to `structural_simplify` · Issue #2000 · SciML/ModelingToolkit.jl · GitHub](https://github.com/SciML/ModelingToolkit.jl/issues/2000) is. The one you link is related but isn’t a full solution.
