# MTK and JuMP

**URL:** <https://discourse.julialang.org/t/mtk-and-jump/58161>\
**Category:** Modelling & Simulations\
**Tags:** question, jump, modelingtoolkit\
**Created:** [March 29, 2021, 12:27pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161 "2021-03-29T12:27:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [March 29, 2021, 12:27pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/1 "2021-03-29T12:27:36Z")

</div>

I would like to use MTK to build a physical model and than use the equations and convert them into a JuMP model. Any ideas how to do that?

My thoughs (based on [Automatic Conversion of Julia Code to C Functions · Symbolics.jl](https://symbolics.juliasymbolics.org/stable/tutorials/converting_to_C/)):

- This could be a method that takes an `ODESystem` and a `JuMP.Model` and attaches the needed variables and constraints to it.
- Apply `build_function` and afterwards feed `JuMP.VariableRefs` through it and obtain expressions
- Use this RHS expressions and do a simple forward Euler discretization.

```julia
using ModelingToolkit

@parameters t σ ρ β
@variables x(t) y(t) z(t)
D = Differential(t)

eqs = [
    D(x) ~ σ*(y-x),
    D(y) ~ x*(ρ-z)-y,
    D(z) ~ x*y - β*z,
]

sys = ODESystem(eqs, t, [x,y,z], [σ, ρ, β])

fnc = eval(build_function(
    equations(sys), 
    states(sys), 
    parameters(sys), 
    t, 
    target=Symbolics.JuliaTarget(), expression=Val{false},
)[1])

```

However, I still get `Equations` back.

```julia
julia> fnc(zeros(3), [10, 28, 8/3], 0.0)
3-element Array{Equation,1}:
 Differential(t)(x(t)) ~ σ*(y(t) - x(t))
 Differential(t)(y(t)) ~ x(t)*(ρ - z(t)) - y(t)
 Differential(t)(z(t)) ~ x(t)*y(t) - (β*z(t))

import JuMP
m = JuMP.Model()
JuMP.@variable(m, x[1:3])

julia> fnc(x, [10, 28, 8/3], 0.0)
3-element Array{Equation,1}:
 Differential(t)(x(t)) ~ σ*(y(t) - x(t))
 Differential(t)(y(t)) ~ x(t)*(ρ - z(t)) - y(t)
 Differential(t)(z(t)) ~ x(t)*y(t) - (β*z(t))

```

---

<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 1, 2021, 12:44pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/2 "2021-04-01T12:44:42Z")

</div>

There’s a way to do this with an arbitrary differential equation stepper:

[https://mtk.sciml.ai/dev/tutorials/nonlinear\_optimal\_control/](https://mtk.sciml.ai/dev/tutorials/nonlinear_optimal_control/)

It’s on the ControlSystem but easy to map over the ODESystem.

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [April 1, 2021, 2:16pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/3 "2021-04-01T14:16:49Z")

</div>

Thx!  
Do you mean the `runge_kutta_discretize(sys::ControlSystem`?

---

<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 1, 2021, 4:44pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/4 "2021-04-01T16:44:58Z")

</div>

Yes. Needs a bit more to do everything but it’s close

---

<div class="post-metadata">

**Author:** ![jbcaillau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbcaillau/32/206767_2.png) [@jbcaillau](https://discourse.julialang.org/u/jbcaillau)\
**Post date:** [June 15, 2022, 10:52am UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/5 "2022-06-15T10:52:35Z")

</div>

@ChrisRackauckas @ohmsweetohm1 Dear all, not sure this is the best place to ask but: what is the current state of MTK in terms of re-usability / translation towards other interfaces (such as JuMP) for optimisation?

For instance: how can I use MTK and Ipopt?

---

<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:** [June 15, 2022, 4:28pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/6 "2022-06-15T16:28:20Z")

</div>

> [@jbcaillau](#):
>
> For instance: how can I use MTK and Ipopt?

MTK hooks into Optimization.jl.

[http://optimization.sciml.ai/stable/tutorials/symbolic/](http://optimization.sciml.ai/stable/tutorials/symbolic/)

Optimization.jl has an MOI interface to hit the JuMP solvers directly.

[http://optimization.sciml.ai/stable/optimization\_packages/mathoptinterface/](http://optimization.sciml.ai/stable/optimization_packages/mathoptinterface/)

---

<div class="post-metadata">

**Author:** ![jbcaillau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbcaillau/32/206767_2.png) [@jbcaillau](https://discourse.julialang.org/u/jbcaillau)\
**Post date:** [June 16, 2022, 3:15pm UTC](https://discourse.julialang.org/t/mtk-and-jump/58161/7 "2022-06-16T15:15:24Z")

</div>

Thanks for the prompt answer: having a look at it.
