Task 5 of 6

Velocity Verlet: the Symplectic Step

Velocity Verlet reads like Euler with one extra term. Move the position with a quadratic instead of a straight line, then update the velocity using the average of the accelerations at the two ends of the step:

x ← x + v·dt + ½·a·dt²
v ← v + ½·(a + a_new)·dt        where a_new = −x_new

It is second order — the same exponent midpoint gave you, so the instrument you have been using cannot tell them apart by exponent alone. (It is about four times more accurate than midpoint at the same step size, and a real implementation carries a_new into the next step as its a, so it costs one force evaluation per step against midpoint's two. Here a = −x is free, so the code below just recomputes it.)

What it has that midpoint does not is invisible in a single step and decisive over a million. The Verlet step is an exactly area-preserving map of the (x, v) plane — symplectic — and for this oscillator it conserves (1 − dt²/4)·x² + v² exactly, forever, in a way no accumulation of steps can erode. Measure the order here. The next task is where that sentence starts to matter.

Goal: write the velocity Verlet step. The ratio should be ≈4 again — and the errors themselves about four times smaller than midpoint's.

Requirements

Hint 1 — order of operations

a is captured before x moves; aNext is read after. The velocity update needs both, so it has to come last.

Hint 2 — the extra term

0.5 * a * dt * dt is the constant-acceleration formula from first-year mechanics. Dropping it leaves a scheme that still looks plausible and is back to first order.

Hint 3 — the whole step
const a = -x;
x = x + v * dt + 0.5 * a * dt * dt;
const aNext = -x;
v = v + 0.5 * (a + aNext) * dt;

Same idea elsewhere

Velocity Verlet, not RK4, is what every production molecular-dynamics code on a GPU actually ships — GROMACS, LAMMPS, HOOMD-blue and OpenMM all step with Verlet or leapfrog on CUDA, HIP and Metal. It is not because they cannot afford RK4's four evaluations. It is because a run of a hundred million steps is judged on whether the energy stayed put, and that is a property of the shape of the update, not of its order.

All tasks in ODE Integrators

  1. One Thread, One Whole Trajectory
  2. Halve the Step, Halve the Error
  3. Look Before You Leap
  4. Four Slopes, Weighted
  5. Velocity Verlet: the Symplectic Step
  6. Payoff: RK4 Drifts, Verlet Does Not

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.