TExcelTAD
Home › Numerical Methods › Long-Time Integration and Symplectic Methods

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 qq, momentum pp and energy H(q,p)=T(p)+V(q)H(q, p) = T(p) + V(q), Hamilton's equations are

q′=∂H∂p,p′=−∂H∂q,q' = \frac{\partial H}{\partial p}, \qquad p' = -\frac{\partial H}{\partial q},

and the energy HH is conserved. The flow also preserves phase-space area (volume). Methods whose steps preserve area are called symplectic.

Energy drift of standard methods

python · try it yourself
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):

pi+1=pi−h∇V(qi),qi+1=qi+h pi+1.p_{i+1} = p_i - h\nabla V(q_i), \qquad q_{i+1} = q_i + h\,p_{i+1}.

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 H~=H+O(hp)\tilde{H} = H + O(h^p) 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.

python · try it yourself
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 q′′=−qq'' = -q with q0=1q_0 = 1, p0=0p_0 = 0, take one velocity Verlet step with h=0.1h = 0.1.

Answer

q1=0.995q_1 = 0.995, p1=−0.09975p_1 = -0.09975.

Solution

p1/2=0−0.05⋅1=−0.05p_{1/2} = 0 - 0.05 \cdot 1 = -0.05; q1=1+0.1⋅(−0.05)=0.995q_1 = 1 + 0.1 \cdot (-0.05) = 0.995; p1=−0.05−0.05⋅0.995=−0.09975p_1 = -0.05 - 0.05 \cdot 0.995 = -0.09975. (Exact: cos⁡0.1=0.995004\cos 0.1 = 0.995004, −sin⁡0.1=−0.099833-\sin 0.1 = -0.099833.)

Exercise 2: Time reversibility

What happens if you run leapfrog forward nn steps and then backward nn steps with −h-h?

Answer

You return exactly to the start (up to rounding).

Solution

The leapfrog step with −h-h is the exact inverse of the step with hh, a property Euler and RK4 lack.

Exercise 3: Symplectic Euler on the oscillator (code)

Compare explicit Euler and symplectic Euler on q′′=−qq'' = -q with h=0.1h = 0.1 for 10,000 steps. Print the final energies (initial energy 0.50.5).

Answer

Explicit Euler's energy explodes; symplectic Euler's stays close to 0.50.5.

Solution
python · try it yourself
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…

  1. grows linearly
  2. stays bounded
  3. grows exponentially
  4. is exactly zero

Velocity Verlet (leapfrog) is…

  1. first order and dissipative
  2. second order, symplectic and time-reversible
  3. fourth order
  4. implicit

Why can adaptive step sizes spoil symplectic integrators?

  1. They are too slow
  2. Changing the step destroys the conserved modified energy
  3. They need derivatives
  4. They are not accurate