A pile of particles becomes a fluid when each particle feels its neighbours: they crowd it, push it away and drag its velocity toward theirs. This lesson pairs with Coding Adventure: Simulating Fluids by Sebastian Lague, linked under Watch & read. The video builds this kind of simulation from falling particles through density, pressure, viscosity and neighbour lookup. You build a small 2D version in NumPy, with forces that cancel in pairs, and test it against independent checks.
Start with the smoothing kernel. A particle does not sit at a point of the fluid; it stands for a blob of mass spread over a radius h. The density at particle i is the sum of mass × W(rᵢⱼ) over every particle j within h, itself included, where W falls smoothly from a peak at r = 0 to zero at r = h. For this to be a density, mass per area, W must integrate to 1 over the plane. In 2D the poly6 kernel is 4 (h² - r²)³ / (π h⁸). The constant comes from integrating 2π r (h² - r²)³ from 0 to h, which gives π h⁸ / 4.
Pressure needs the gradient of a kernel, and poly6 is a poor choice: its gradient vanishes at r = 0, so two particles that land on top of each other feel no push. The spiky kernel 10 (h - r)³ / (π h⁵) does not have that flaw. Its gradient at offset dx from a neighbour is -30 (h - r)² / (π h⁵) × dx / r, and it is defined as the zero vector at r = 0 and beyond h. The check below sums the poly6 kernel over thin rings to confirm it integrates to 1.
import numpy as np
h = 1.0
r = (np.arange(10000) + 0.5) / 10000 * h # rings of width h / 10000
w = 4 / (np.pi * h**8) * (h * h - r * r) ** 3 # poly6 on each ring
print(round(float((w * 2 * np.pi * r).sum() * h / 10000), 4))
print(round(4 / np.pi, 4))From density comes pressure: stiffness × (density - rest), clamped at zero. The clamp matters at the surface, where a particle has fewer neighbours and a low density. A negative pressure there would pull particles together and the surface would clump.
The pressure force is where simulations quietly go wrong. Written as -m² Σⱼ (pᵢ / ρᵢ² + pⱼ / ρⱼ²) ∇W(xᵢ - xⱼ), the bracket is the same number whichever particle is called i, while the gradient flips sign. Particle i is pushed by j exactly as hard as j is pushed by i, in the opposite direction, so the forces sum to zero and momentum is conserved. A common alternative, (pᵢ + pⱼ) / (2 ρⱼ), looks reasonable but divides by only one density.
Work one pair by hand. With h = 1, mass 1, two particles 0.5 apart at densities 2 and pressures 4, each has p / ρ² = 1, so the bracket is 2. The spiky slope at distance 0.5 is -30 × 0.25 / π = -7.5 / π, so the force on the left particle is -2 × 7.5 / π = -15 / π ≈ -4.7746 along x: it is pushed left, the other right.
import numpy as np
h, d = 1.0, 0.5 # two particles, 0.5 apart
slope = -30 / (np.pi * h**5) * (h - d) ** 2 # spiky kernel slope at distance d
gx = -slope # gradient at particle 0: its offset points toward -x
share = 4.0 / 2.0**2 # p / rho^2 for p = 4, rho = 2
pair = share + share
print(round(-pair * gx, 4), round(-pair * -gx, 4))Viscosity uses a third, simple weight, the cone 3 (h - r) / (π h³). The force on i is μ m² Σⱼ w (vⱼ - vᵢ) / (ρᵢ ρⱼ). It is zero when every particle moves alike and it slows relative motion, and since it flips sign under i ↔ j it also conserves momentum. Neighbours here come from a brute-force pass over all pairs, which is fine for a few dozen particles; the spatial hash from the previous lesson replaces it when the count grows.
The stepper updates the velocity first and then moves the particle with the new velocity. At a wall the coordinate is clamped to the box and only the velocity component that crossed it is reversed, scaled by a damping factor below 1 so the fluid loses energy there.
Two mistakes to watch for. The first is the wrong normalization constant. The 3D poly6 constant, 315 / (64 π h⁹), is easy to copy into a 2D code. It makes the kernel integrate to about 1.54, so every density is too high by that factor and the pressure is far from the rest density. The second is the asymmetric pressure term described above. It passes any test with two identical particles and fails only when densities differ. In a three-particle example it leaves a net force of about (0.0114, -0.0587) on a fluid that should feel none.
The tests check the kernels by numerical integration and against a finite difference, the density against a brute-force loop, the forces against a written-out pairwise formula and the zero total force, and a block of fluid dropped in a box against collapsing onto the floor. Passing them shows the pieces behave as described. It does not show the simulation is stable at every time step or stiffness: too stiff or too large a step makes the particles explode, and those settings still have to be tuned by eye. It also does not cover boundary particles, the third dimension or fluids that stay incompressible.