# Save a simplified ModelingToolkit problem as text

**URL:** https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit, serialization, caching
**Created:** [April 16, 2025, 1:23pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124 "2025-04-16T13:23:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [April 16, 2025, 1:23pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/1 "2025-04-16T13:23:01Z")

</div>

Because `structural_simplify` is slow, I want to write the simplified system to a file, from where I can load it later. Using `serialize("sys.bin", sys)` works, but this causes problems when switching between julia versions, and with undefined variables when creating getter functions… Is there a way to write and load the simplified system as text in a human-readable format?

I guess it should be possible to write `unknowns(sys), equations(sys) etc...` to a file in the correct format, so that you can create a model from it again. Maybe someone has done this before?

---

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [April 17, 2025, 1:17pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/2 "2025-04-17T13:17:50Z")

</div>

I got problems and systems mixed up here. I think that serializing the `ODESystem` and not the `ODEProblem` will solve the errors I am getting with the getu and setu functions. But it is not clear to me how I can serialize just the `ODESystem` and the initialization system: how can I pass a deserialized initialization system to the `ODEProblem` constructor?

What I am trying to achieve:

```julia
sys = ODESystem(eqs, t)
isys = generate_initializesystem(sys)
serialize("test.bin", (sys, isys))
sys, isys = deserialize("test.bin")
prob = ODEProblem(sys, u0map, tspan; isys)

```

@ChrisRackauckas

---

<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 17, 2025, 8:31pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/3 "2025-04-17T20:31:00Z")

</div>

> [@Bart\_van\_de\_Lint](#):
>
> Is there a way to write and load the simplified system as text in a human-readable format?

Does `ODEProblemExpr` do what you want?

---

<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: [April 18, 2025, 2:19pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/4 "2025-04-18T14:19:44Z")

</div>

How can I convert an `ODEProblemExpr` back into an `ODEProblem` ?

UPDATE:  
The following approach works:

```julia
expr = ODEProblemExpr(s.sys, defaults, (0.0, dt); guesses)
prob = eval(expr)

```

But now solving the problem is 100 times slower than before. Perhpas ODEProblemExpr does not store the simplified problem, but the original problem?

---

<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 18, 2025, 3:36pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/5 "2025-04-18T15:36:40Z")

</div>

It should be the simplified problem? It is effectively what is stated in the OP, i.e. using `equations(sys)`, etc. and then writing a Julia expression that would be the expression to reconstruct itself. If you run that after simplification, then `equations(sys)` etc. are the changed versions and so it should “just work” (and I think we test that?).

You can print it out to a file and see what the resulting system is and it should look like the . If it’s giving something incorrect, please open an issue.

---

<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 19, 2025, 12:15pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/6 "2025-04-19T12:15:36Z")

</div>

Note that since it’s just a Julia file, if you put it into a repo and call it then it will play nicely with package precompilation and store the binary.

---

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [April 19, 2025, 7:56pm UTC](https://discourse.julialang.org/t/save-a-simplified-modelingtoolkit-problem-as-text/128124/7 "2025-04-19T19:56:03Z")

</div>

I found this old issue: [Save expressions to .jl? · Issue #253 · JuliaIO/FileIO.jl · GitHub](https://github.com/JuliaIO/FileIO.jl/issues/253). I guess this is the missing link for actually writing this expression to a `.jl` file…
