Computation fluid dynamics, usually just called fluid simulation, is an integral part of aerospace, weather prediction, computer graphics, and just about any other field of engineering. This version is an implementation of Position Based Fluids by Macklin and Müller.
Click to enter interactive mode
Esc to exit interactive mode
Move mouse: rotate camera
W/S: move camera forward/backward
A/D: move camera left/right
E/Q: move camera up/down
Hold shift: move camera faster
HOW IT WORKS
In order to understand how fluid simulation works, we must first understand the basics of particle simulation. Then, we can move on to what makes this specific simulation interesting.
Many physical phenomena can be modeled with particles, such as fire, dirt, air, water, and even living things. In this case, we are modeling a liquid using around 15,000 particles. Each particle keeps track of two values: its position and its velocity, so all we have to do each frame is each particle's position and velocity. If we can do that efficiently and realistically, we can create fascinating simulations.
If we're using particle simulation as our framework, then the question becomes, "how can we distill the essence of fluid into a particle update rule?" For centuries, mathematicians and physicists have attempted to answer this question, the most substantial of which has been the Navier-Stokes equations.
For Position Based Fluids, a different approach is used. A key observation of water is that it keeps a constant density; no matter what's happening, it remains at 1,000 kg/m³. Thus, we can achieve a water-like effect by moving around the particles each frame to as closely match the density of water as possible. If the particles are too far apart, we move them close together, and if they're too compact, we move them apart.
The technique of enforcing constant density already gets us a pretty good simulation, but there's one more trick here that really seals the deal. It's a nearly universal fact that the more action a simulation has, the more it grabs the viewer's attention. In this case, action corresponds to high turbulance, with water flowing every which way.
We can quantify this "chaos factor" using a mathematical quantity called curl. When we find areas with high curl, we amplify it further, thus creating larger splashes and waves. This technique is called vorticity confinement, because it focuses on areas where fluid is moving in a vortex shape.