# How does structural\_simplify work?

**URL:** <https://discourse.julialang.org/t/how-does-structural-simplify-work/120810>\
**Category:** General Usage\
**Tags:** question, package, modelingtoolkit\
**Created:** [October 2, 2024, 12:39pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810 "2024-10-02T12:39:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2024, 12:39pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/1 "2024-10-02T12:39:11Z")

</div>

The function `structural_simplify()` of the package ModelingToolkit can reduce the number of equations of a DAE a lot, often by 80%.

But how does this actually work?

- does the order of the DAE stays the same?
- does it introduce new variables to replace expressions in the original DAE system?

Any hints welcome!

---

<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:** [October 2, 2024, 1:29pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/2 "2024-10-02T13:29:00Z")

</div>

This presentation should be a good intro

> **[03%20-%20Symbolic%20Manipulation.pdf](https://staff.polito.it/roberto.zanino/sub1/teach_files/modelica_minicourse/03%20-%20Symbolic%20Manipulation.pdf)**

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2024, 2:21pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/3 "2024-10-02T14:21:59Z")

</div>

Thanks for sharing. But the presentation was written for Modelica, and Modelica is generating C code. Is MTK generating Julia code?

---

<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:** [October 2, 2024, 2:23pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/4 "2024-10-02T14:23:32Z")

</div>

> [@ufechner7](#):
>
> But the presentation was written for Modelica

MTK and modelica work in very similar ways

> [@ufechner7](#):
>
> Is MTK generating Julia code?

yes.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2024, 3:58pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/5 "2024-10-02T15:58:58Z")

</div>

> [@baggepinnen](#):
>
> yes.

And is it possible to see or to print this code?

---

<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:** [October 2, 2024, 4:12pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/6 "2024-10-02T16:12:39Z")

</div>

Kind-of, if you have generated a `prob = ODEProblem(...)`, you can look at `prof.f` or `prof.f.f` or something like that, it’s in there somewhere.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [October 2, 2024, 4:26pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/7 "2024-10-02T16:26:08Z")

</div>

I like the introduction to the topic in Cellier, François E., and Ernesto Kofman. _Continuous System Simulation_. Softcover reprint of hardcover 1st ed. 2006. New York, NY: Springer, 2010. [Continuous System Simulation | SpringerLink](https://doi.org/10.1007/0-387-30260-3). In particular the chapter 7. Check it out, I guess you may have an institutional subscription.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2024, 5:46pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/8 "2024-10-02T17:46:27Z")

</div>

Thanks a lot, I can access this book.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2024, 8:22pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/9 "2024-10-02T20:22:33Z")

</div>

I get:

```julia
julia> prob.f
(::ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#815"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe6756fb3, 0x73b9ba2c, 0x45942c73, 0x8ef0d825, 0xf7df8f64), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x5201bd46, 0xaaa9ab4a, 0x28da1c62, 0x57e60ec5, 0x99d2b9c2), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing}) (generic function with 1 method)

```

Is this Julia code? Can it be formatted nicer?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [October 2, 2024, 9:22pm UTC](https://discourse.julialang.org/t/how-does-structural-simplify-work/120810/10 "2024-10-02T21:22:20Z")

</div>

To see just the generated function code, you can create the `ODEProblem` and look at `prob.f.f.f_oop` for the out-of-place version or `prob.f.f.f_iip` for the in-place version.

You can see the whole expression to create the ODEProblem with

```
ODEProblemExpr(ode_system, u0, tspan)

```
