Save a simplified ModelingToolkit problem as text

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?

3 Likes

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:

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

Does ODEProblemExpr do what you want?

1 Like

How can I convert an ODEProblemExpr back into an ODEProblem ?

UPDATE:
The following approach works:

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?

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.

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.

1 Like

I found this old issue: Save expressions to .jl? · Issue #253 · JuliaIO/FileIO.jl · GitHub. I guess this is the missing link for actually writing this expression to a .jl file…