# How to create SecondOrderODEProblem from ODESystem

**URL:** <https://discourse.julialang.org/t/how-to-create-secondorderodeproblem-from-odesystem/95847>\
**Category:** Specific Domains\
**Tags:** differentialequation\
**Created:** [March 10, 2023, 8:37am UTC](https://discourse.julialang.org/t/how-to-create-secondorderodeproblem-from-odesystem/95847 "2023-03-10T08:37:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [March 10, 2023, 8:37am UTC](https://discourse.julialang.org/t/how-to-create-secondorderodeproblem-from-odesystem/95847/1 "2023-03-10T08:37:19Z")

</div>

I want to simulate a single body orbiting (0, 0) by `VelocityVerlet()` and using a system defined through ModelingToolkit. Functional code without modeling toolkit is as follows:

```julia
using DifferentialEquations
function ddu_kepler(du, u, p,t) # In this form, u is r (position vector)
    rx, ry = u
    pos_mag³ = hypot(rx, ry)^3
    ddu = [-rx/pos_mag³, -ry/pos_mag³]
    return ddu
end

r0 = [1.0, 0.0]
v0 = [0, 1/√r0[1]]
tspan = (0.0, 100.0)
prob = SecondOrderODEProblem(ddu_kepler, v0, r0, tspan)
sol = solve(prob_partitioned, VelocityVerlet(), dt=0.1)

```

My best attempt with ModelingToolkit is as follows:

```julia
using ModelingToolkit
@variables t rx(t) ry(t)
∂ₜ = Differential(t)
∂ₜ² = ∂ₜ^2

@named kepler_eqns = ODESystem([∂ₜ²(rx) ~ -rx/hypot(rx, ry)^3,
                                ∂ₜ²(ry) ~ -ry/hypot(rx, ry)^3]
)
kepler_eqns_simplified = structural_simplify(kepler_eqns)
u0 = [rx=>1, ry=>0, ∂ₜ(rx)=>0, ∂ₜ(ry)=>1]
tspan = (0.0, 10.0)

```

Which can be solved by non-second order solvers:

```julia
prob = ODEProblem(kepler_eqns_simplified, u0, tspan)
sol = solve(prob, Tsit5())

```

However, I am unable to define a SecondOrderODEProblem from the ODESystem:

```julia
prob = SecondOrderODEProblem(kepler_eqns_simplified, u0, tspan)
prob = SecondOrderODEProblem(kepler_eqns_simplified, u0, tspan, [])
prob = SecondOrderODEProblem(kepler_eqns, u0, tspan)
prob = SecondOrderODEProblem(kepler_eqns, u0, tspan, [])

```

results in the following errors (it seems strange that error is different when no parameters are passed explicitly):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/9/29541d674ae4db3c239fbe846ec2361b8224e728.png)

Is this not implemented, or am I doing it wrong?

---

<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:** [March 10, 2023, 8:45am UTC](https://discourse.julialang.org/t/how-to-create-secondorderodeproblem-from-odesystem/95847/2 "2023-03-10T08:45:47Z")

</div>

You cannot. There is information lost if you have already lowered to a first order ode. Mtk would need to allow for building it directly. Worth an issue.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [March 10, 2023, 8:55am UTC](https://discourse.julialang.org/t/how-to-create-secondorderodeproblem-from-odesystem/95847/3 "2023-03-10T08:55:10Z")

</div>

Thanks for the quick reply.

> <https://github.com/SciML/ModelingToolkit.jl/issues/2114>
>
> Ref: https://discourse.julialang.org/t/how-to-create-secondorderodeproblem-from-…odesystem/95847
> It would be nice to be able to build the following 3 systems (from \[this section\](https://docs.sciml.ai/DiffEqDocs/latest/solvers/dynamical\_solve/) of docs):
> \`\`\`
> DynamicalODEProblem{isinplace}(f1, f2, v0, u0, tspan, p = NullParameters(); kwargs...)
> SecondOrderODEProblem{isinplace}(f, du0, u0, tspan, p = NullParameters(); kwargs...)
> HamiltonianProblem{T}(H, p0, q0, tspan, p = NullParameters(); kwargs...)
> \`\`\`
> 
> Directly from an ODESystem.
