# How to convert an ODESystem to NonlinearSystem?

**URL:** https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079
**Category:** Modelling & Simulations
**Tags:** question, modelingtoolkit
**Created:** [December 1, 2022, 7:34am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079 "2022-12-01T07:34:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![wangyunyueyueniao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wangyunyueyueniao/32/44789_2.png) [@wangyunyueyueniao](https://discourse.julialang.org/u/wangyunyueyueniao)
#### Post date: [December 1, 2022, 7:34am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/1 "2022-12-01T07:34:32Z")

</div>

I know there is a “convert\_system” function which can convert a “NonlinearSystem” to an “ODESystem”. Can we do it backwards? So that we can have both steady-state and dynamic simulation with a single code base.

---

<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: [December 1, 2022, 8:16am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/2 "2022-12-01T08:16:59Z")

</div>

Good point. Open an issue. Right now, if you have a NonlinearSystem you can use the SteadyStateDiffEq.jl methods to solve the nonlinear system via ODEs, but it’s not at the symbolic level.

---

<div class="post-metadata">

### Author: ![wangyunyueyueniao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wangyunyueyueniao/32/44789_2.png) [@wangyunyueyueniao](https://discourse.julialang.org/u/wangyunyueyueniao)
#### Post date: [December 1, 2022, 10:18am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/3 "2022-12-01T10:18:36Z")

</div>

It turns out that a similar issue has been opened 4 weeks ago.  
[link to issue](https://github.com/SciML/ModelingToolkit.jl/issues/1916#issue-1435184045)

---

<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: [December 1, 2022, 11:02am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/4 "2022-12-01T11:02:38Z")

</div>

Okay cool. Yeah we’ll get to it: it’s not too difficult. Also the NonlinearSolve.jl docs are what’s on my table right now, and there’s some simple fixes there that would make this straight forward. So check back there in a little bit, and remind me if I drop it.

---

<div class="post-metadata">

### Author: ![BernhardAhrens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhardahrens/32/13570_2.png) [@BernhardAhrens](https://discourse.julialang.org/u/BernhardAhrens)
#### Post date: [November 29, 2023, 5:40pm UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/5 "2023-11-29T17:40:48Z")

</div>

I am also interested in converting an ODESystem to a NonlinearSystem. I wonder if this feature has been implemented or if there is a good workaround @ChrisRackauckas. I am aware of doing something like this for an ODESystem but then we don’t have the nice solution interface anymore

```julia
dfg = NonlinearProblem(odeprob)
nsol = solve(dfg, SciMLNLSolve.NLSolveJL())
```

---

<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: [November 30, 2023, 2:33am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/6 "2023-11-30T02:33:15Z")

</div>

> [@BernhardAhrens](#):
>
> but then we don’t have the nice solution interface anymore

The solution interface is the same? MWE?

---

<div class="post-metadata">

### Author: ![BernhardAhrens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhardahrens/32/13570_2.png) [@BernhardAhrens](https://discourse.julialang.org/u/BernhardAhrens)
#### Post date: [November 30, 2023, 12:46pm UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/7 "2023-11-30T12:46:02Z")

</div>

Here is an MWE showcasing what would be convenient but doesn’t work currently. It boils down to that for an `ODEProblem`, you can access the solution with `sol[u(t,x,y)]` while for a `NonlinearProblem` created from that `ODEProblem` you cannot access it via `nsol[u(t,x,y)]` or `nsol[u(x,y)]`. I could rewrite the equation to a nonlinear problem which would allow to access `nsol[u(x,y)]`, but it would be nice if there was a convenience function that would do that conversion. I believe that is also what the original post was looking for.

```julia
using ModelingToolkit, OrdinaryDiffEq, MethodOfLines, Plots, SciMLNLSolve

@parameters x y t
@variables u(..)
Dxx = Differential(x)^2
Dyy = Differential(y)^2
Dt = Differential(t)

# Boundary conditions
bcs = [
	u(0,x,y) ~ 0.0,
	u(t,0,y) ~ 0.0, u(t,1,y) ~ 0.0,
	u(t,x,0) ~ 0.0, u(t,x,1) ~ 0.0
]

# Space and time domains
domains = [
	t ∈ IntervalDomain(0.0,1.0),
	x ∈ IntervalDomain(0.0,1.0),
	y ∈ IntervalDomain(0.0,1.0)
]

# Discretization
dx = 0.04
nd = round(Int, 1 / dx) + 1
discretization = MOLFiniteDifference([x=>dx,y=>dx],t)

eq = Dt(u(t,x,y)) ~ Dxx(u(t,x,y)) + Dyy(u(t,x,y)) + sin(pi*x)*sin(pi*y)
@named pde_system = PDESystem([eq],bcs,domains,[t,x,y],[u(t,x,y)])

prob = discretize(pde_system,discretization)

sol = solve(prob,TRBDF2(),saveat=0.1)

# solution interface accessible via u(t,x,y)
sol[u(t,x,y)]

dfg = NonlinearProblem(prob)
nsol = solve(dfg, SciMLNLSolve.NLSolveJL())

# solution interface not accessible via u(t,x,y): ArgumentError: u(t, x, y) is neither an observed nor a state variable.
nsol[u(t,x,y)] # does not work

# instead, we have to access the solution via the solution vector - this is not convenient for larger models
M_ss = zeros(nd,nd)
M_ss[2:end-1, 2:end-1] .= reshape(nsol.u, nd-2, nd-2)

```

MWE adapted from [Control of PDE systems · JuliaSimControl (juliahub.com)](https://help.juliahub.com/juliasimcontrol/stable/examples/pde_control/)

---

<div class="post-metadata">

### Author: ![BernhardAhrens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhardahrens/32/13570_2.png) [@BernhardAhrens](https://discourse.julialang.org/u/BernhardAhrens)
#### Post date: [December 4, 2023, 9:42am UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/8 "2023-12-04T09:42:27Z")

</div>

@ChrisRackauckas - you can find the MWE in the post above. And btw, big thanks to all your great work with SciML and MethodOfLines.jl in particular 👏

---

<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: [December 10, 2023, 2:31pm UTC](https://discourse.julialang.org/t/how-to-convert-an-odesystem-to-nonlinearsystem/91079/9 "2023-12-10T14:31:46Z")

</div>

Yeah that’s [Unable to Index into a steadystate solution using symbols · Issue #338 · SciML/MethodOfLines.jl · GitHub](https://github.com/SciML/MethodOfLines.jl/issues/338), it’s on our mind and needs to get fixed.
