Pouyan Jahangiri

All work

Particle Simulator, in 4 minutes. Choose a chapter to look at it first, or play the video from the start.

GPU physics lab

Particle Simulator

Seven particle simulations to tune, measure and share, on the GPU, in the browser.

Read about it

The product

A physics lab that measures what it shows

Most particle demos are made to be watched. Particle Lab is made to be measured. It runs gases, gravity, charges, flocks and particle life live in the browser, and puts them inside an instrument: readouts, charts, a check on the gas law, and challenges that grade a prediction against what the simulation goes on to do. It is also the engine behind the site's N-body article.

Seven models, one contract

Each simulation is a plugin with the same five duties: start from a seed and its parameters, step a fixed time, hand over its particles, hand over its measurements, and clean up. A new model is one registration, and nothing around it changes.

The seven simulation models, where each runs and what it shows
ModelRuns onWhat it shows
Elastic gas CPUElastic collisions relax to the Maxwell-Boltzmann distribution, and P·V = N·k·T holds.
Molecular dynamics CPUA Lennard-Jones gas by velocity Verlet: the ideal-gas law, phase changes and real substances.
N-body gravity CPUEvery body pulls on every other: spiral discs and gravitational collapse.
GPU N-body WebGPU computeThe same model at tens of thousands of bodies, in real time.
Particle Life CPUAttraction and repulsion between types that is not returned in kind, and lifelike structure out of it.
Boids CPUA flock from separation, alignment and cohesion alone.
Electrostatics CPUCharged particles under Coulomb's law.

An instrument, not a demo

  • Live readouts. Temperature, pressure, kinetic energy and momentum, with charts of the average speed and the energy over time.
  • The speed distribution. A histogram of the particles' speeds drawn against the analytic Maxwell-Boltzmann curve.
  • The gas law. P·V set against N·k·T, with how far the gas is from ideal shown on a gauge.
  • Real units. The Lennard-Jones gas can be read as argon, neon, krypton or xenon, in kelvin, pascals and joules, by corresponding states.

Predict, then look

Nine curated scenarios drop straight into a phenomenon, from a gas settling onto Maxwell-Boltzmann to a galaxy of 60,000 bodies on the GPU. Four guided challenges ask for a prediction first, then grade it against the live measurements.

  • Maxwell-Boltzmann convergence. Watch an elastic gas relax onto the curve, then hold it there with the thermostat.
  • Boyle's law. Halve the volume at a constant temperature, and the pressure has to climb.
  • Equipartition. Heat the gas, and the kinetic energy and the average speed rise with the temperature.
  • Form a galaxy. A spinning disc collapses and clumps into spiral arms, shaped by its spin and its gravity.

Save, share, record

A scenario is its model, seed, parameters, substance and camera. The particles themselves are never saved, since the seed and the parameters bring back the same run. A scenario travels in a link, a named preset or a JSON file, and is checked as untrusted input when it comes back. The canvas records to MP4, WebM or GIF, with PNG screenshots and the measurements exported as CSV or JSON.

Around it are undo and redo, a command palette, full use by keyboard, a tour on the first visit, and an app that installs and runs offline.

How it is built

Four layers, each depending only inward: the interface, the renderer, the state, and at the centre a physics core that depends on nothing.

  • The physics is pure. The core is plain TypeScript, with no React and no three.js, so it is deterministic and tested on its own.
  • A fixed time step. The same seed and parameters give the same run, every time.
  • The render loop leaves React alone. Each frame reads the parameters straight from the store, so playing the simulation never re-renders the interface.
  • The GPU where it pays. Gravity at scale runs in a WebGPU compute kernel, drawn by three.js's WebGPU renderer, which falls back to WebGL2 where WebGPU is missing.

Checked by the laws it keeps

The physics core is tested by invariants, not by snapshots, in more than 400 tests.

  • Conservation. Energy and momentum stay where the physics says they must.
  • Convergence. The distribution of speeds approaches the analytic Maxwell-Boltzmann curve.
  • Stability. The integrators hold steady over long runs.
  • Determinism. The same seed and parameters reproduce the same run, step for step.