This package generates code for the Euler-Lagrange equations as well as Hamilton's equations for GeometricIntegrators.jl and related packages.
EulerLagrange.jl and all of its dependencies can be installed via the Julia REPL by typing
]add EulerLagrange
Using EulerLagrange.jl is very simple and typically consists of four to five steps:
- Obtain symbolic variables for a Lagrangian or Hamiltonian system of a given dimension.
- Obtain a symbolic representation of the parameters of the system if it has any.
- Build the Lagrangian or Hamiltonian using those symbolic variables and parameters.
- Construct a
LagrangianSystem
orHamiltonianSystem
, which is where the actual code generation happens. - Generate a
LODEProblem
orHODEProblem
that can then be solved with GeometricIntegrators.jl.
In the following, we showcase this procedure for a particle in a square potential.
Before any use, we need to load EulerLagrange
:
using EulerLagrange
Next, we generate symbolic variables for a one-dimensional system:
t, x, v = lagrangian_variables(1)
With those variables, we can construct a Lagrangian
L = v ⋅ v / 2 - x ⋅ x / 2
This Lagrangian together with the symbolic variables is then used to construct a LagrangianSystem
:
lag_sys = LagrangianSystem(L, t, x, v)
The constructor computes the Euler-Lagrange equations and generates the corresponding Julia code.
In the last step, we can now construct a LODEProblem
from the LagrangianSystem
and some appropriate initial conditions, a time span to integrate over and a time step:
tspan = (0.0, 10.0)
tstep = 0.01
q₀ = [1.0]
p₀ = [0.5]
lprob = LODEProblem(lag_sys, tspan, tstep, q₀, p₀)
Should we fancy so, we can integrate this system using GeometricIntegrators:
using GeometricIntegrators
integrate(lprob, ExplicitMidpoint())
If you use EulerLagrange.jl in your work, please consider citing it by
@misc{Kraus:2023:EulerLagrange,
title={EulerLagrange.jl: Code generation for Euler-Lagrange equations in Julia},
author={Kraus, Michael},
year={2023},
howpublished={\url{https://github.com/JuliaGNI/EulerLagrange.jl}},
doi={10.5281/zenodo.8241048}
}
We are using git hooks, e.g., to enforce that all tests pass before pushing. In order to activate these hooks, the following command must be executed once:
git config core.hooksPath .githooks