Hello,
I am using the DifferentialEquations.jl
scaffolding to solve a Hamiltonian dynamical system. It is important to conserve the Hamiltonian to a reasonable accuracy, and so I am using symplectic integrators (SymplecticEuler
or VerletLeapfrog
work well for me) together with the constructor HamiltonianProblem{T}(H,p0,q0,tspan;kwargs...)
. This works well as long as there is no time-dependence.
However, I am at a loss as to how I can use this approach for a time-dependent Hamiltonian. The time-dependence is not very complicated (periodically switches), and I would like for my Hamiltonian function to look something like this:
function H(r,θ,p)
switchtime = p
if sin((Ď/switchtime)*t)>=0
H = # some function of r,θ which does not depend on time
else
H = # another function of r,θ which does not depend on time
end
end
But of course, since t
is not a recognized variable, and H
only takes the arguments p,q,params
, this does not work.
I could easily accomplish this if I use the simple ODE constructor, e.g. ODEProblem(f::ODEFunction,u0,tspan,p=NullParameters();kwargs...)
by including the explicit time dependence inside f(u,p,t)
, but this would not allow me to use symplectic methods and my Hamiltonian diverges too quickly.
So my question is, is it possible to introduce simple time-dependence in the Hamiltonian for a HamiltonianProblem
? If the answer is no (which would be a bummer â I think this is the most elegant way to implement my ODEs), then can someone explain how I could do this with one of the other symplectic-friendly constructors, SecondOrderODEProblem
or DynamicalODEProblem
?
My equations are ODEs of the form \dot{r} = f_1(r,\theta;t), \dot{\theta} = f_2(r,\theta;t), which I can re-cast as Hamiltonian with H = H(r,\theta;t). In the Hamiltonian formulation, r is my âmomentumâ and \theta is my coordinate.
Iâll be happy to share more code and a MWE if it will help someone understand the question better!