Extracting ODEProblem formulas and passing them to JuMP NLExpressions

I am trying to write a code that extracts a model’s equations from a DiffEq.jl ODEProblem or ModelingToolkit.jl ODESystem and copies them into JuMP.jl NLExpressions for use in nonlinear programming. In particular, I need each coordinate function of the vector field separately, ideally as a Julia expression in the variables x[i] and p[i]. Is there any interface in the SciML ecosystem that would facilitate this? It would be nice to be able to write nicely annotated vector field functions (e.g. using variable names other than x and p) and then automatically generate the less readable version as “copyable Julia code” (as opposed to a compiled ODEFunction). Any help is much appreciated!

If you just trace it with symbolic expressions and build the OptimizationSystem, you’ll get what you’re looking for.

Would you mind elaborating slightly? In particular, some specific questions:

Does “trace it with symbolic expressions” just mean passing Symbolics.jl variables as input to the vector field?

Once the OptimizationSystem is built, will OptimizationProblemExpr enable recovery of the equations in the desired format?

Also, how does one use the latter function? When I run it on the example system on the OptimizationSystem page (OptimizationProblemExpr(os,DiffEqBase.NullParameters())), I receive ERROR: MethodError: no method matching getindex(::Nothing, ::Int64). I’m assuming NullParameters() isn’t the right thing to pass as second argument there, so what is?

You don’t need to use OptimizationProblemExpr just OptimizationProblem. See the example here hopefully that clears things up Symbolic Problem Building with ModelingToolkit · Optimization.jl and Modeling Optimization Problems · ModelingToolkit.jl

That’s a very old blog post, and uses SymPy instead of symbolics, but you’d just do that with Symbolics and get expressions from the ODE solver for the update equations out.