Extending Jos Stam's Fluid Simulation to Three Dimensions

I've recently revisited my fluid sim, extending it from two dimensions to three dimensions. Code at: github

Extending to 3D

The original 2D simulation solves the Navier-Stokes equations in a simplified, computationally feasible way. Extending this to 3D was annoying but straightforward. Add another array to the directional values, edit the boundary conditions to handle a third dimension, and consider the extra neighbours for each cell when solving. I've been writing a lot of C recently, so I wrote this in C too.

Numerical Methods

I rewrote the linear system solving logic using the Jacobi iteration method. The diffusion and pressure projection equations are solved by iteratively approximating a system of linear equations. Since I was overhauling most of the code, and with plans to parallelize portions to boost performance and increase my grid resolution, I opted for the Jacobi method over Gauss-Seidel. The Jacobi algorithm updates each unknown based solely on values from the previous iteration, allowing for independent, parallel computation across the grid. In contrast, Gauss-Seidel updates sequentially using the latest values, which creates dependencies and limits parallelism.

Rendering: 2.5D

For rendering, I used a simple approach that still looks great. Since I was already comfortable rendering a grid of 2D cells, moving to 3D was as easy as stacking another dimension of these grids and rotating the camera to view them from the side. Because I only render from a fixed camera angle, any edge artifacts are minimal when using a large grid.

Colours

Experimenting with the fragment shaders produced some very nice results, particularly the fire shader and a GOT-style "wildfire" shader. Despite several attempts at tweaking the viscosity and diffusion parameters, I couldn't get anything that looked convincingly like water.

Performance Considerations and Parallelization

The current implementation handles a resolution of 50x50x50 (125,000 cells) quite well. To push this further, I plan to explore GPU computation or multi-threading. Theres also some other small improvements to be made like early termination on the Jacobi solver.

For more details on the algorithm, refer to Jos Stam's original paper .

This project is licensed under the MIT License. See the LICENSE file for details.