High-Level Solution Overview
Incompressibility Condition Reviewed
Recall Equation 1 from the last chapter:
This equation enforces the incompressibility condition. It says that the divergence of the fluid’s velocity field (called $\vec{u}$) is zero.
If the divergence of the velocity field is non-zero, then that means the amount of fluid entering a cell is not equal to the amount of fluid exiting that same cell. This would indicate either:
- Matter is being created or destroyed within that cell
- The fluid is compressible
Statement 1 would violate the law of conservation of mass, and statement 2 contradicts our assumption that the fluid is incompressible. We want to avoid compressible fluids because the math governing them is significantly more complex. We know both statements must be false:
The Problem
Our issue is that in the process of updating our fluid simulation we need to make changes to the velocity field, which is will almost definitely result in us creating some kind of non-zero divergence within the velocity field.
Solution Overview
Our goal then for each step of our fluid simulation can be broken down into the following extremely high-level steps:
- Calculate a new velocity field that has a non-zero divergence
- Correct the vectors in the new velocity field to create a divergence-free velocity field somehow… (spoiler: it’s the next chapter)
Going back to what we learned in the last chapter, step 1 involves changing the velocity field, and as we’ve learnt, changing a velocity means applying an acceleration. So step 1 involves the successive application of all acceleration terms to the velocity field, neglecting, at this stage, any considerations regarding the divergence of the resulting flow.
This can be further decomposed into 3 shader stages:
- Advection
- Viscous Diffusion
- Adding External Forces
Now we move on to step 2, which actually has a technical name known as “projection”. As we will see later, the projection step really comes down to doing three things:
- Calculating the pressure field (by solving a Poisson equation)
- Calculating the gradient of this pressure field
- Subtracting the gradient of the pressure field from our intermediate velocity field
Which makes our final simulation flowchart look like this: