Fluid Domain
A FLIP/PIC water solver with obstacles and surface meshing, plus cloth and rigid bodies.
The solver is about 718 lines in src/simulation/fluid_solver.cpp, and the mesher is about 370 lines. It runs as a modifier on a domain object. This video has obstacles in the water and was rendered at 3840x2160 with 64 samples.
The same simulation module has a cloth solver, rigid bodies with sleeping contacts, and a bake that writes the result into the scene.
The solver stores the water as particles and solves pressure on a grid. Particles preserve small details and the grid enforces incompressibility, so velocities move between the two on every substep.
Setup
The domain is a grid with 64 cells along its longest side by default. Each liquid cell gets 8 particles, one per octant, with a random offset in each. Obstacles are voxelised into the same grid.
Picking the time step
A frame is split into substeps so that no particle crosses more than 2 cells in one substep. The solver works out the step from the fastest particle, with an extra term for gravity, and allows up to 16 substeps per frame.
Particles to grid
Velocities are stored on cell faces. Each face gathers the particles within one cell of it, weighted by a trilinear tent function. I used a gather because each face only writes to itself, so faces can run in parallel without locks.
Pressure
Faces touching a wall or an obstacle are set to zero so that no water flows into solids. The solver then builds one equation per liquid cell, A p = -div. The diagonal counts the non-solid neighbours, liquid neighbours give -1, air cells have a pressure of 0, and solids are closed.
I solve this with conjugate gradients and a diagonal preconditioner, running in parallel on the job system. Subtracting the pressure gradient from the faces next to liquid gives a divergence-free velocity field.
Grid to particles
Particles near the surface need velocities from faces in the air, so the solver extrapolates velocities a few faces out from the liquid.
Each particle then blends two updates. FLIP uses the particle's own velocity and adds the change on the grid. PIC takes the grid velocity directly. Pure FLIP retains more detail and more noise, and pure PIC is smoother and loses detail, so flip_ratio sets the mix. The default is 0.95.
Particles move through the grid velocity with a midpoint (RK2) step. Inflows top cells up to the target particle count, and drains delete particles that reach them.
Building the surface
Particles have no surface, so the mesher builds one for every frame.
- It bins particles into buckets the size of the kernel radius.
- It builds a distance field following Zhu and Bridson, using the distance to the kernel-weighted mean of nearby particles, minus the particle radius.
- It runs marching tetrahedra in parallel slabs. Each cube splits into six tetrahedra around its 0-7 diagonal, so neighbouring cubes agree on their shared faces.
- It welds the slabs together by keying each crossing point to its grid edge.
- It applies Taubin smoothing, which alternates shrink and inflate steps to smooth the blobby surface without losing volume.
Pinched areas can break the half-edge build. In that case the mesher falls back to loose triangles, which still render but lose smooth shading there.