Position Based Fluids
Description§
In this project, I reimplemented Position Based Fluids (PBF) by Macklin and Müller (2013)1.
Essentially, PBF is just an extension of smoothed-particle hydrodynamics (SPH) into the Position Based Dynamics framework (Müller 2007).
In contrast to SPH, the major benefit that PBF gives is stability, allowing us to take larger physics time-steps. Typically SPH would require multiple physics substeps for one animation frame, PBF can take just one physics step per frame.
Videos§
Algorithm§
The PBF algorithm is very simple, and is easily parallelisable.
Euler Integration§
Integration
First, the algorithm does an unconstrained step by applying external forces and particle velocities; but the particles do not move quite like a liquid.
Particle Sorting§
Sorting
A lot of our computations are dependent on a particle’s neighbours.
- Naively, for each particle we look at every other particle, this ends up being
- A better way is to discretise the simulation space into a uniform grid of bins, insert the particles to the bins, then sort.
Jacobi Optimizer§
Optimizer
This step projects the particles’ positions back into their expected fluid position using a constraint function.
Optimization
Next, define the constraint function by
Due to the integration step we have that As a result, we optimize for a positional correction such that where is a Lagrange multiplier given by
The computation is straightforward,
where is a tensile instability term, and is the gradient of the spiky kernel (Müller 2003)4.
The tensile instability adds an artificial pressure that encourages particles to clump together to mimic surface tension.
Typically, the optimizer is not run until convergence – rather, it is run for a few (4-6) steps which gives results that look good visually.
Viscosity and Vorticity§
Viscosity and Vorticity
To make the results look more visually appealing, XSPH viscosity and vorticity confinement is applied.
Vorticity confinement reintroduces energy that was lost in the integration and optimization steps making the simulation look nicer, however, this technique is not physically based.
Implementation§
I used CUDA to implement the entire algorithm on the GPU to exploit its highly parallel nature.
Originally, I had a lot of issues with performance in simulating ~150K particles in real time.
- I had a lot of CPU GPU data transfers which introduced some latency.
- Instead, I used the CUDA/OpenGL interop features to directly map the OpenGL VBOs to the PBF simulation buffer.
- GPUs are optimised for single precision.
- A lot of the library functions I was using were causing type promotion from floats to doubles, preventing this gave me a 10x speed up.
My renderer simply reuses the code from the particle simulation labs, but I replaced the shaders with one that did Blinn-Phong shading on point-sprite spheres based on Green (2010)5.
Initially, I wanted to implement screen space fluid rendering5 with an anisotropic kernel6. Unfortunately, I only managed to implement the kernel and not the shaders.
Macklin and Müller. (SIGGRAPH 2013). Position Based Fluids ↩︎
R. Hoetzlin. (GDC 2014). Fast Fixed-Radius Nearest Neighbors: Interactive Million-Particle Fluids ↩︎
(Fall 2022). UPenn CIS565 - GPU Programming, Project 2: Stream Compaction ↩︎
Müller et al. (SIGGRAPH 2003). Particle-Based Fluid Simulation for Interactive Applications ↩︎
S. Green. (GDC 2010). Screen Space Fluid Rendering for Games ↩︎ ↩︎
Yu and Turk. (SIGGRAPH 2010). Reconstructing Surfaces of Particle-Based Fluids Using Anisotropic Kernels ↩︎