# Use Symplectic Integrator for ODE for time reversability constraint

**URL:** <https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [February 18, 2019, 10:44pm UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955 "2019-02-18T22:44:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [February 18, 2019, 10:44pm UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/1 "2019-02-18T22:44:54Z")

</div>

Hi everyone,

I would like to use Symplectic Integrators such as `VelocityVerlet`  
or `VerletLeapfrog` from DifferentialEquations.jl to ensure the time-reversibility of the equation of motion.  
Nonetheless, my problem can not be set as a ` DynamicalODEProblem`.

My equation of motion is just:  
`\frac{du}{dt} = f2(u) ` where v = f2(u) is a model for the velocity which depends only of the position u )

Since my function f2 is not a function of the velocity v but only of the position u, I can not use these symplectic integrators.  
Is there a way to get around this issue?

Thank you for your time,

---

<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:** [February 18, 2019, 11:42pm UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/2 "2019-02-18T23:42:26Z")

</div>

> [@mleprovost](#):
>
> Since my function f2 is not a function of the velocity v but only of the position u, I can not use these symplectic integrators.

where did you get this idea? I think you flipped the equations. The docs say

[http://docs.juliadiffeq.org/latest/types/dynamical\_types.html#Mathematical-Specification-of-a-Dynamical-ODE-Problem-1](http://docs.juliadiffeq.org/latest/types/dynamical_types.html#Mathematical-Specification-of-a-Dynamical-ODE-Problem-1)

> This is a Partitioned ODE partitioned into two groups, so the functions should be specified as `f1(dv,v,u,p,t)` and `f2(du,v,u,p,t)` (in the inplace form), where `f1` is independent of `v` (unless specified by the solver), and `f2` is independent of `u` and `t` .

which matches your problem. Position depends on velocity, velocity depends on position and possibly time.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [February 19, 2019, 1:29am UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/3 "2019-02-19T01:29:00Z")

</div>

Thank you for your answer,

Sorry I should have been clearer, I mean that I don’t have 2 ODEs to solve (velocity v and position u ) but only one ODE to solve for the position u. Indeed, the velocity is given by a kinematic model and not by an ODE, i.e. I know in advance the velocity v at each position u : `v=f2(u)`,

Therefore I don’t know what equation to set for \frac{dv}{dt}  
I want to do something like this:

```julia
function f1(dv,v,u, p ,t)
    dv[1:2] = [0.0, 0.0]
end

function f2(du,v,u,p,t)
    #Velocity(u) known model for velocity
    du[1:2] = Velocity(u)
end

```

Maybe I don’t need to use ` DynamicalODEProblem`, I just want to ensure the time-reversibility of my solution.

---

<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:** [February 19, 2019, 3:24pm UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/4 "2019-02-19T15:24:51Z")

</div>

Oh, then it’s not a good problem for these integrators. You’ll want to use a reversible method for first order ODEs, like `Trapezoid()`. You can set that to fixed point iteration if the problem isn’t stiff.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [February 19, 2019, 6:34pm UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/5 "2019-02-19T18:34:15Z")

</div>

Thanks for the advice,

My model is an interpolated function using `CubicSplineInterpolation` of Interpolations.jl, therefore it doesn’t support ForwardDiff used by default in the implicit time scheme `Trapezoid`.

Do you know a derivative-free method I can use in `Trapezoid()`?  
Else I am thinking that I can probably provide the Jacobian of the model but I think we can only get Gradients with Interpolations.jl and not Jacobians.

---

<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:** [February 20, 2019, 4:12am UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/6 "2019-02-20T04:12:31Z")

</div>

Yes, try `Trapezoid(nlsolve = NLFunctional())` or `Trapezoid(nlsolve = NLAnderson())`

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [February 21, 2019, 8:39pm UTC](https://discourse.julialang.org/t/use-symplectic-integrator-for-ode-for-time-reversability-constraint/20955/7 "2019-02-21T20:39:59Z")

</div>

Thank you @ChrisRackauckas for your help!
