# Question about structural\_simplify in ModelingToolkit

**URL:** <https://discourse.julialang.org/t/question-about-structural-simplify-in-modelingtoolkit/60568>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [May 5, 2021, 11:58am UTC](https://discourse.julialang.org/t/question-about-structural-simplify-in-modelingtoolkit/60568 "2021-05-05T11:58:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)\
**Post date:** [May 5, 2021, 11:58am UTC](https://discourse.julialang.org/t/question-about-structural-simplify-in-modelingtoolkit/60568/1 "2021-05-05T11:58:16Z")

</div>

It should be something simple, but I really cannot spot the error.

Lets suppose I want to solve a simple mass-spring-damper problem

> m D2(x) + c D(x) + k x = f(t)

Following MTK documentation I can use

> @parameters t k m ζ  
> @variables x(t) F(t)  
> D = Differential(t)  
> D2 = D\*D  
> f(t) = 100.0/m;  
> eq = [ F ~ f(t),  
> D2(x) ~ F - (k/m)\*x - ( ζ\*2\*sqrt(k\*m)\*D(x) )/m ]  
> sys = ODESystem(eq,t)  
> sys\_simplified = structural\_simplify(sys)  
> sys\_lower = ode\_order\_lowering(sys\_simplified);

so I can keep track of x, F and D(x), as expected. It is working.

Now suppose I want to keep track of the damping force (actually, I want to use it in a more complex operation to define the RHS of D2(x)). I can try

> @parameters t k m ζ  
> @variables x(t) F(t) FD(t)  
> D = Differential(t)  
> D2 = D\*D  
> f(t) = 100.0/m  
> eq = [ F ~ f(t),  
> FD ~ ( ζ\*2\*sqrt(k\*m)\*D(x) )/m  
> D2(x) ~ F - (k/m)\*x - FD ]  
> sys = ODESystem(eq,t)  
> sys\_simplified = structural\_simplify(sys)  
> sys\_lower = ode\_order\_lowering(sys\_simplified);

but when I try to use this second approach I get

> prob = ODEProblem(sys\_lower,u0,tspan,p,jac=true);  
> ArgumentError: Any[FD(t)] are missing from the variable map.

I read the documentation and it says that MTK can miss some variables in the mapping, and the suggestion is to use

> ODESystem(eq,t).

It is not working in this case, so I suspect it is something related to the presence of D(x) in FD. But at the same time, the first version works and also has D(x) in the RHS.

The complete code to obtain the error is

using LinearAlgebra, Symbolics, ModelingToolkit,  
using Plots, DifferentialEquations

> @parameters t k m ζ  
> @variables x(t) F(t) FS(t) FD(t)  
> D = Differential(t)  
> D2 = D\*D  
> f(t) = 100.0/m  
> fd(t) = ( ζ\*2\*sqrt(k\*m)\*D(x) )/m  
> eq = [ F ~ f(t),  
> FD ~ ( ζ\*2\*sqrt(k\*m)\*D(x) )/m,  
> D2(x) ~ F -(k/m)\*x - FD ]  
> sys = ODESystem(eq,t)  
> sys\_simplified = structural\_simplify(sys)  
> sys\_lower = ode\_order\_lowering(sys\_simplified)  
> u0 = [D(x) =\> 0.0 ,  
> x =\> 1.0 ,  
> ]  
> p = [ k =\> 10.0,  
> m =\> 1.0,  
> ζ =\> 0.1  
> ]  
> tspan = (0.0, 10.0)  
> prob = ODEProblem(sys\_lower,u0,tspan,p)  
> sol = solve(prob)

Thank your for your assistance.

---

<div class="post-metadata">

**Author:** ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)\
**Post date:** [May 5, 2021, 12:04pm UTC](https://discourse.julialang.org/t/question-about-structural-simplify-in-modelingtoolkit/60568/2 "2021-05-05T12:04:45Z")

</div>

I forget to mention that using

> fd(t) = ( ζ_2_sqrt(k\*m)\*D(x) )/m;

and

> D2(x) ~ F - (k/m)\*x - fd(t)

also works.

---

<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:** [May 5, 2021, 12:21pm UTC](https://discourse.julialang.org/t/question-about-structural-simplify-in-modelingtoolkit/60568/3 "2021-05-05T12:21:04Z")

</div>

Can you open this as an issue?

---

<div class="post-metadata">

**Author:** ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)\
**Post date:** [May 5, 2021, 12:25pm UTC](https://discourse.julialang.org/t/question-about-structural-simplify-in-modelingtoolkit/60568/4 "2021-05-05T12:25:39Z")

</div>

For sure.
