How a fluid can emerge from particles that only know about their neighbors.
Part 1 of Smooth Particle Hydrodynamics
One way to simulate a fluid is to divide space into a grid and track how much fluid is in each cell. Click and drag below to inject dye into a grid-based fluid. The simulation advects density and velocity through fixed cells.
Grid simulations are powerful, but every cell exists whether there's fluid in it or not. Another way is to forget the grid and model the fluid as a collection of particles, each carrying mass, position, and velocity. Particles only exist where the fluid is.
If you've played with a flocking simulation before, the particle approach feels similar. In flocking, each bird looks at nearby birds and follows three rules: separate, align, cohere.
In SPH, each particle follows the same pattern: look at nearby particles, compute local forces, update. Instead of steering rules, SPH uses forces derived from fluid physics: how crowded is it here? Which way is the pressure pushing me? How fast are my neighbors moving compared to me? Fluid-like behavior emerges from these local interactions.
The "smooth" in SPH comes from the kernel function. It answers a simple question: how much should a particle care about a neighbor at a given distance?
A particle at the center of its smoothing radius cares most about particles right next to it, and cares less about particles further away. Beyond the smoothing radius, influence drops to zero.
The specific shape of the kernel matters less than its properties: it's smooth, it falls off to zero at the smoothing radius, and it's normalized so that the total influence integrates to 1.
The first thing each particle needs to figure out is: how crowded is it here? This is the density at the particle's location. A particle estimates its density by summing up kernel-weighted contributions from all neighbors within the smoothing radius.
Fluids resist compression. When particles are packed too tightly, they push apart. The pressure force on a particle points away from regions of high density.
Pressure alone gives you something like an ideal gas. Real fluids have viscosity, which makes neighboring particles tend to move at similar speeds. The viscosity force is proportional to the velocity difference between a particle and its neighbors.
We now have all the forces. At each time step, we update velocity and position. The order of operations and the number of intermediate evaluations determine how accurately the integration tracks the true path. Here are four methods, from simplest to most accurate.
A spout on the left wall emits particles into a shallow container where they feel pressure, viscosity, and gravity. Watch the fluid fill up and splash.
The simulation loop is straightforward: compute densities, compute forces, update velocities, update positions. The expensive part is finding neighbors, which we'll solve with spatial hashing on the GPU in Part 2.