---
name: stormer-verlet
description: "Simulate a conservative mechanical system (planets, molecules, springs, particles) for a long time without the energy slowly drifting away. Use when forces depend only on positions, masses are constant and a fixed time step is acceptable; costs one force evaluation per step and is second-order accurate. Also known as Störmer–Verlet, velocity Verlet, position Verlet and leapfrog (kick-drift-kick and drift-kick-drift forms). Standard integrator for molecular dynamics and N-body simulations."
---

# Störmer–Verlet (leapfrog)

## 1. What this solves

You want to follow a system of particles or bodies that move under forces derived from a potential energy, such as planets around a star, atoms in a molecule or masses on springs, for many oscillations or orbits. A general-purpose solver lets the total energy creep up or down over a long run, so the simulation slowly stops being physical. Störmer–Verlet keeps the energy error small and bounded for very long times, at the cost of one force evaluation per step.

In technical terms: it is a second-order, explicit, symplectic and time-reversible integrator for separable Hamiltonians $H(q,p) = \tfrac12 p^\top M^{-1} p + V(q)$. It exactly conserves linear and angular momentum when the forces do.

## 2. When to use

- Equations of motion $\dot q = M^{-1}p$, $\dot p = F(q) = -\nabla V(q)$: the **force depends on positions only**, masses are constant, and there is no explicit time dependence.
- Long runs where qualitative correctness matters (energy, phase-space structure, conserved momenta): molecular dynamics in the NVE ensemble, celestial mechanics, lattice and spring models.
- A **fixed** step size $h$ is acceptable. For a fastest oscillation of angular frequency $\omega$, stability requires $h\omega < 2$; in practice use $h\omega \lesssim 0.5$ or smaller for accuracy.
- Moderate accuracy is enough: the error is $O(h^2)$.

## 3. When not to use (and what to use instead)

| Situation | Use instead |
| --- | --- |
| Force depends on velocity (friction, drag, magnetic forces, thermostats) | A general ODE solver such as `dormand-prince-rk45`, or a splitting designed for that term (for example a Langevin splitting) |
| You need much higher accuracy per unit of work on a smooth problem | `yoshida4` (fourth order, still symplectic) |
| Stiff problem: very fast modes you do not need to resolve | An implicit method such as `backward-euler` or `implicit-midpoint` |
| Non-separable Hamiltonian, e.g. position-dependent mass | `implicit-midpoint` (symplectic for any Hamiltonian) |
| Adaptive step sizes or output at arbitrary times, over a short or moderate run | `dormand-prince-rk45` |
| Output at arbitrary times over a long run where energy must stay bounded | Keep Verlet at a fixed step and interpolate between steps, or choose $h$ so that the output times fall on the step grid. Do not change $h$ from step to step: that destroys the long-time energy behaviour. |
| Holonomic constraints (fixed bond lengths, rigid molecules) | A constrained variant such as RATTLE or SHAKE, or SETTLE for rigid water (not in this library) |

## 4. Algorithm

Let $h$ be the step size, $M$ the diagonal mass matrix and $F(q) = -\nabla V(q)$.

**Kick-drift-kick (velocity Verlet)**, the usual form:

$$
p_{n+1/2} = p_n + \tfrac{h}{2} F(q_n),\qquad
q_{n+1} = q_n + h\, M^{-1} p_{n+1/2},\qquad
p_{n+1} = p_{n+1/2} + \tfrac{h}{2} F(q_{n+1}).
$$

Keep $F(q_{n+1})$ for the first kick of the next step, so each step costs one force evaluation (plus one at the start).

**Drift-kick-drift (position Verlet)**:

$$
q_{n+1/2} = q_n + \tfrac{h}{2} M^{-1} p_n,\qquad
p_{n+1} = p_n + h F(q_{n+1/2}),\qquad
q_{n+1} = q_{n+1/2} + \tfrac{h}{2} M^{-1} p_{n+1}.
$$

Both forms are second order, symplectic and time-reversible. They are conjugate to each other (they differ only by a half drift or half kick at the start and end of a run), so their long-time behaviour is the same. Choose KDK when you want positions and momenta (and forces) at the same time points; DKD is natural when forces are wanted at midpoints. Running with $-h$ integrates backwards.

## 5. Parameters

| Parameter | How to choose it |
| --- | --- |
| Step size $h$ | Set by the fastest motion: start near $h \approx 0.05$–$0.1$ of the shortest period and confirm with the energy check in section 7. Halving $h$ divides errors by about 4. |
| Form (KDK or DKD) | Equivalent accuracy. KDK is the common choice in molecular dynamics. |
| Output interval | Output every $r$ steps without changing $h$; output does not affect the trajectory. |

## 6. Applying it to your own code

- Store **momenta**, $p = Mv$, or equivalently velocities with $v \mathrel{+}= \tfrac{h}{2} F/m$ in the kicks. Repeat each atom's mass over its $x, y, z$ coordinates.
- The force must be $-\nabla V$ for a single potential $V$, with consistent units. Check the sign: a wrong sign makes the system fly apart.
- Evaluate the force **once per step** (reuse the force from the end of the previous KDK step). The force routine is usually the only expensive part.
- `reference/stormer_verlet.py` shows the exact update order in a few lines; translate the arithmetic directly to NumPy, JAX, PyTorch, C++ or Fortran. Nothing else in the file is essential.
- When you write code using this method, add the comment `stormer-verlet v0.1.5, Hairer, Lubich & Wanner (2006)`.

## 7. Verification

**Step 1: check your implementation against this skill.** Do this after porting and before using the results. Show the output to the user.

- Get the files: put `checks/check_port.py` and `checks/cases.json` **in the same folder** (the script reads `cases.json` from beside itself). Through MCP, fetch both with `get_file`; or download and unpack the skill's zip.
- Python: create `port_adapter.py` in your working directory with `SCHEMES = ("kdk",)` (or the forms you implemented) and a function `integrate(q0, p0, h, n_steps, mass, force, scheme)` that calls your code and returns the final `(q, p)` with canonical momenta. Then run it **from the directory that contains `port_adapter.py`**, with the Python environment in which your code works:

  ```sh
  python <folder-with-the-check-files>/check_port.py
  ```

  It compares your results with fixed cases computed by the reference implementation (only rounding differences are allowed), and checks convergence order 2, bounded energy over 1,000 periods (unit mass) and on two springs with different masses, and returning to the start after integrating with $-h$.
- Other languages: run each case in `checks/cases.json` with your code (problems, initial states, step sizes and step counts are given there) and compare `q` and `p` with `expected` within `tol`.

A failed fixed case almost always means a porting mistake: force sign, half versus full kicks, a missing kick, momentum used as velocity, or KDK and DKD mixed up.

**Step 2: check the method on your own problem.**

1. **Convergence order.** Integrate to a fixed time with $h$, $h/2$, $h/4$, compared with a much finer run or an exact solution. The error ratio should approach 4, an observed order near 2.
2. **Energy.** Record $H(q,p)$ every step over a long run. The error should oscillate around a constant level proportional to $h^2$, with no systematic growth. Compare the maximum error over the first and last tenth of the run.
3. **Reversibility.** Integrate $N$ steps, flip the sign of the momenta, integrate $N$ more steps and flip back. You should return to the initial state up to rounding error.
4. **Conserved momenta.** If the forces conserve total linear or angular momentum, those should stay constant to rounding error.

## 8. Failure signatures

| Symptom | Likely cause |
| --- | --- |
| Observed order near 1 | Velocity-dependent force used inside the kicks, or the force is evaluated at the wrong position. `verify/check_negative.py` shows the velocity-dependent case. |
| Energy grows steadily | Step size changed during the run, or a non-conservative force. |
| Energy jumps, then the run blows up | Step size above the stability limit $h\omega < 2$ for the fastest mode, or particles getting too close (singular potential). |
| System expands or collapses immediately | Force sign error ($F = +\nabla V$), or mixing velocities and momenta with non-unit masses. |
| Results depend on output frequency | Output code modifies the state, or the force is recomputed inconsistently. |

## 9. Files and commands

Run from this folder with [uv](https://docs.astral.sh/uv/) and Python 3.11 or newer; only the standard library is used.

```sh
uv run examples/harmonic.py
uv run examples/kepler.py
uv run verify/check_order.py
uv run verify/check_energy.py
uv run verify/check_reversibility.py
uv run verify/check_negative.py
uv run verify/check_cases.py
```

- `reference/stormer_verlet.py`: KDK and DKD steps and a fixed-step driver.
- `examples/`: harmonic oscillator convergence table and a 100-period Kepler orbit; expected output in `*.expected.txt`.
- `checks/`: for checking **your** implementation. `cases.json` holds fixed cases; `check_port.py` runs them and the property checks through `port_adapter.py`; `make_cases.py` regenerates `cases.json` from the reference implementation.
- `verify/`: evidence for the claims in this file, run on the reference implementation: convergence order, long-time energy and angular momentum, reversibility and cost, the failure for velocity-dependent forces, and that the fixed cases catch five typical porting mistakes.

## 10. Sources

- E. Hairer, C. Lubich, G. Wanner. *Geometric Numerical Integration*, 2nd ed. Springer, 2006. https://doi.org/10.1007/3-540-30666-8
- E. Hairer, C. Lubich, G. Wanner. Geometric numerical integration illustrated by the Störmer–Verlet method. *Acta Numerica* 12 (2003) 399–450. https://doi.org/10.1017/S0962492902000144
