Long-Time Integration and Symplectic Methods
Planetary orbits, molecular dynamics and game physics run for millions of steps. Over such times, a tiny systematic drift in energy, invisible after one orbit, ruins the simulation: planets spiral into the sun or fly away. Symplectic integrators respect the geometric structure of Hamiltonian mechanics, so their energy error stays bounded forever, even with fairly large steps.
Hamiltonian systems
For a mechanical system with position , momentum and energy , Hamilton's equations are
and the energy is conserved. The flow also preserves phase-space area (volume). Methods whose steps preserve area are called symplectic.
Energy drift of standard methods
import numpy as np
def kepler_acc(q):
return -q / np.linalg.norm(q) ** 3
def energy(q, p):
return 0.5 * p @ p - 1 / np.linalg.norm(q)
def euler(q, p, h):
return q + h * p, p + h * kepler_acc(q)
def rk4(q, p, h):
f = lambda y: np.r_[y[2:], kepler_acc(y[:2])]
y = np.r_[q, p]
k1 = f(y); k2 = f(y + h/2*k1); k3 = f(y + h/2*k2); k4 = f(y + h*k3)
y = y + h/6 * (k1 + 2*k2 + 2*k3 + k4)
return y[:2], y[2:]
def leapfrog(q, p, h): # velocity Verlet
p_half = p + h / 2 * kepler_acc(q)
q_new = q + h * p_half
return q_new, p_half + h / 2 * kepler_acc(q_new)
h = 0.1
checkpoints = (5000, 10000, 20000, 40000) # 40000 steps is about 270 orbits
for name, step in (("explicit Euler", euler), ("RK4", rk4), ("leapfrog", leapfrog)):
q, p = np.array([1.0, 0.0]), np.array([0.0, 1.2]) # an eccentric orbit
E0 = energy(q, p)
errors = []
for k in range(1, checkpoints[-1] + 1):
q, p = step(q, p, h)
if k in checkpoints:
errors.append(energy(q, p) - E0)
print(f"{name:15s}:", " ".join(f"{e:+.1e}" for e in errors))explicit Euler : +2.6e-01 +2.9e-01 +2.9e-01 +2.9e-01
RK4 : -1.4e-05 -2.7e-05 -5.4e-05 -1.1e-04
leapfrog : +7.9e-04 +7.8e-04 +7.9e-04 +7.9e-04
The columns are the energy errors after 5,000, 10,000, 20,000 and 40,000 steps. Explicit Euler's orbit falls apart. RK4 is more accurate per step than leapfrog (order 4 against order 2), and over this run its error is still the smaller one, but it doubles every time the run length doubles: a steady drift that eventually dominates. Leapfrog's error does not grow at all.
Symplectic Euler and leapfrog
Symplectic Euler updates momentum first and then uses the new momentum for the position (or the other way round):
It is only first order, but symplectic. Leapfrog / velocity Verlet composes two half steps symmetrically, which makes it second order, time-reversible and symplectic, at one force evaluation per step. It is the standard integrator of molecular dynamics and N-body codes.
Why the energy stays bounded
A symplectic method exactly conserves a modified energy that is close to the true one (backward error analysis). The computed trajectory is the exact trajectory of a slightly different Hamiltonian system, so energy oscillates around the right value instead of drifting.
import numpy as np
def acc(q): return -q / np.linalg.norm(q) ** 3
def energy(q, p): return 0.5 * p @ p - 1 / np.linalg.norm(q)
q, p = np.array([1.0, 0.0]), np.array([0.0, 1.2])
E0 = energy(q, p)
h = 0.05
worst = {}
for k in range(1, 100_001):
p_half = p + h / 2 * acc(q); q = q + h * p_half; p = p_half + h / 2 * acc(q)
if k in (1_000, 10_000, 100_000):
worst[k] = abs(energy(q, p) - E0)
print("leapfrog energy error at 1e3, 1e4, 1e5 steps:", [f"{v:.1e}" for v in worst.values()])leapfrog energy error at 1e3, 1e4, 1e5 steps: ['2.0e-04', '2.0e-04', '2.0e-04']
Caveats
- Symplectic methods need a fixed step size; naive adaptive steps destroy the structure (special time-transformations exist).
- They conserve energy only approximately (bounded error) and do not fix phase errors: positions along the orbit still drift linearly in time.
- For dissipative systems (with friction) the advantage disappears; use standard methods with tight tolerances.
In software
- Molecular dynamics (GROMACS, LAMMPS, OpenMM) use velocity Verlet or leapfrog; astronomy codes use leapfrog and higher-order symplectic splittings (Yoshida, Wisdom-Holman in REBOUND).
- Game engines use semi-implicit (symplectic) Euler for exactly this reason (Differential Equations in Software, Game Physics).
Exercises
Exercise 1: One leapfrog step by hand
For the oscillator with , , take one velocity Verlet step with .
Answer
, .
Solution
; ; . (Exact: , .)
Exercise 2: Time reversibility
What happens if you run leapfrog forward steps and then backward steps with ?
Answer
You return exactly to the start (up to rounding).
Solution
The leapfrog step with is the exact inverse of the step with , a property Euler and RK4 lack.
Exercise 3: Symplectic Euler on the oscillator (code)
Compare explicit Euler and symplectic Euler on with for 10,000 steps. Print the final energies (initial energy ).
Answer
Explicit Euler's energy explodes; symplectic Euler's stays close to .
Solution
h, n = 0.1, 10_000
q, p = 1.0, 0.0
for _ in range(n):
q, p = q + h * p, p - h * q
qs, ps = 1.0, 0.0
for _ in range(n):
ps = ps - h * qs
qs = qs + h * ps
print(f"explicit Euler energy {0.5 * (q*q + p*p):.3e}, symplectic Euler energy {0.5 * (qs*qs + ps*ps):.6f}")explicit Euler energy 8.179e+42, symplectic Euler energy 0.493602
Test yourself
Over very long simulations of orbits, a symplectic method's energy error…
- grows linearly
- stays bounded
- grows exponentially
- is exactly zero
Velocity Verlet (leapfrog) is…
- first order and dissipative
- second order, symplectic and time-reversible
- fourth order
- implicit
Why can adaptive step sizes spoil symplectic integrators?
- They are too slow
- Changing the step destroys the conserved modified energy
- They need derivatives
- They are not accurate