darian.lagman
WORK / 03

N-Body Simulator

Shipped

A gravitational N-body simulator built to compare numerical integrators by energy conservation, performance, and visual stability — written for my Physics & Astronomy minor coursework, then extended.

Role
Sole engineer
Timeline
2024 – 2025
Status
Shipped
Stack
CPythonNumPyPhysics
N-Body Simulator hero

Problem

Numerical integration is one of those topics that's easy to use and hard to use correctly. I wanted a simulator I could trust — meaning I could read off energy and momentum at every step and watch what the integrator was actually doing to the system.

Integrators compared

Diagram
Integrator benchmark loop
State
Integrator
Next step
Energy drift
Momentum

The simulator treats conservation drift as the test surface, not just a visual artifact.

Forward Euler

Simplest, fastest, wrong. Energy grows without bound on a stable orbit. Useful only as a baseline.

RK4

Classical fourth-order Runge–Kutta. Accurate over short windows but not symplectic — energy drifts slowly.

Velocity Verlet

Symplectic, second-order. Energy oscillates around a fixed value rather than drifting; the right default for gravitational systems.

Leapfrog

Same family as Verlet, slightly different staggering. Equivalent for most purposes here.

Conservation laws as ground truth

The benchmark isn't visual stability; it's energy and angular momentum drift over a long run. Symplectic integrators (Verlet, leapfrog) keep both bounded. RK4 doesn't, despite a higher local accuracy order — that's the symplectic-vs-non-symplectic story in one chart.

Visualization

The recordings on the home page are this simulator running with a few dozen bodies, with a HUD overlay showing instantaneous total energy and angular momentum. I left the HUD on intentionally; without it you can't see the integrator misbehaving until orbits visibly fly apart.

What I'd do differently

  • Pick velocity Verlet earlier. The "let's compare four integrators" framing was a coursework artifact; in practice I always use Verlet.
  • Move the visualization to the GPU. The CPU loop caps the simulation at the framerate it can render.
Outcomes
64+
Bodies
real-time
Verlet
Best integrator
energy drift
Euler
Worst
as expected
60
FPS @ 64 bodies
single thread