Playground
Fluid (SPH)

A fluid made of particles

Smoothed particle hydrodynamics SPH describes a fluid as many small particles. Every particle carries mass and moves with the forces of the fluid around it. Quantities such as the density are smooth sums over the neighbors within the smoothing radius h, weighted by a kernel. The fluid itself needs no grid, so it can splash freely. Wikipedia

Pressure and viscosity The model of Müller, Charypar and Gross (2003) computes the density of each particle with the poly6 kernel. The pressure follows from the density: stiffness × (density − 1), and it is never negative. The gradient of the spiky kernel pushes particles apart where the fluid is compressed. The Laplacian of the viscosity kernel pulls the velocity of a particle toward the velocity of its neighbors. Müller 2003

Neighbor search on the GPU Checking all pairs would be too slow. The box is split into square cells that are at least as big as h, so all neighbors of a particle lie in its 3 × 3 cells. On the GPU every particle takes a slot in its cell with an atomic counter. Each cell has 24 slots. The density and force passes then read only the slots of the 3 × 3 cells. Wikipedia

Original work

How it works

Every substep has five passes, in the same order on the GPU and the CPU: clear the grid, bin the particles, density, forces, integrate. The default is 8,192 particles with WebGPU (at most 32,768) and 1,024 without it (at most 2,048). The smoothing radius h is twice the particle spacing, so the fluid has the same volume at every count.

  • Clear and bin: the cell counters are set to 0. Then each particle adds 1 to the counter of its cell (atomicAdd) and stores its index in the slot it got. A full cell still counts, but stores nothing.
  • Density: each particle sums the poly6 kernel over the particles in its 3 × 3 cells, itself included. The particle mass is chosen so that a square lattice at rest has the density 1.
  • Forces: the pressure force uses the gradient of the spiky kernel and the mean pressure of the pair. The viscosity force uses the Laplacian of the viscosity kernel. Both are divided by the density. Then come gravity and, while you press the canvas, a spring toward the speed of your pointer within a radius of 10.
  • Integrate and walls: velocity += acceleration × dt, the speed is limited to 150, then the position moves. A particle that crosses a wall is put back on the wall and bounces with 0.3 of its speed.
  • Substeps: dt is the stable time step, the smaller of 0.4 × h / (speed of sound + 150) and 0.03 × h² / ν. The speed of sound is √stiffness and ν is the scaled viscosity. The page runs as many substeps as the frame needs, up to the limit. Without WebGPU the same five steps run in TypeScript and a WebGL shader draws the quads.

Source code:sph.shader.tssph-gpu.strategy.tssph-cpu-physics.ts

Note Simplifications in this demo:

  • The fluid is weakly compressible: it gives way a little under its own weight, so the density at the bottom is slightly above 1.
  • The time step follows stiffness and viscosity, not the frame. At most 16 substeps (GPU) or 6 (CPU) run per frame. With many particles, high stiffness or high viscosity the fluid therefore runs in slow motion.
  • Each cell holds at most 24 particles, and the neighbor search ignores more. The viscosity is scaled with the smoothing radius and the speed of sound, so the slider is not a physical constant. The walls have no boundary particles: particles line up along them.
Particles
Stiffness
Viscosity
Gravity

Press and hold the mouse or a finger on the canvas and drag to stir the fluid.

Frames/s: 0Backend: –Particles: 8,192Substeps per frame: 15Time step dt: 0.0011