An enhanced version of the famous Stable Fluids FFT fluid solver first introduced by Jos Stam [1-2]. The spectral simulation code solves the Navier-Stokes equations for incompressible fluid flow over a periodic domain with no internal obstacles. The code features:
- GPU acceleration with PyTorch
- 5 possible advection routines for reduced numerical dissipation
- Vorticity confinement
- 2D & 3D implementations
- Solution of all equation terms from the original paper
The solver is unconditionally stable as long as the vorticity confinement parameter is not set too high. An equivalent (and much slower) pure NumPy implementation is also included. Five flow examples are presented for both 2D and 3D: jet flow, vortical flow, Taylor Green vortex, curl noise [3] and homogenous isotropic turbulence (HIT) [4]. Please note that to run the curl noise example, you need to download my Perlin Noise package https://github.com/flow-zone/Perlin-Noise4D.
Please run the example scripts to understand how to use the code. The output of the simulation is the velocity field components (U
,V
,W
) and scalar density (S
). The minimum parameters you need to specify are:
- Domain dimensions (
Nx
,Ny
) for 2D or (Nx
,Ny
,Nz
) for 3D - Number of iterations
n_iter
- Simulation timestep
dt
- Initial conditions of the velocity components (
U_inflow
,V_inflow
) for 2D or (U_inflow
,V_inflow
,W_inflow
) for 3D - Initial scalar density field
S_inflow
- Forcing duration in number of iterations
n_iter_inflow
Additional parameters include:
- Fluid viscosity
nu
- Scalar viscosity
nu_scalar
- Scalar dissipation constant
C_dissip
- Vorticity confinement weighting
VC_weight
- Advection type
advection_type
- GPU deployment
gpu
The possible advection methods are:
linear
- the classic original [1-2]bfecc
- backward and forward error correction [5]maccormack
- an accelerated variant of BFECC [6]fedkiw
- no overshoot monotonic spline interpolation from R. Fedkiw [7]cubic
- from Robert Bridsons' book [8]
Out of these options, cubic
is the most accurate and least diffusive.
The coordinate system follows a classical image-based coordinate system with the origin defined at the top-left pixel. X
coordinates go across columns (1st dimension), Y
goes down rows (0th dimension) and Z
goes into the 2nd dimension as the out-of-plane axis if simulating in 3D. The last dimension (2 for 2D or 3 for 3D) is always the time dimension. Therefore, the arrays are of size [Ny,Nx,Nz,n_iter]
and a field variable at position x,y,z
and iteration t
is indexed by S[y,x,z,t]
Velocity units are [pixels/timestep].
The animation of various quantities can be viewed in the code. If using Spyder, uncomment animate_scalar_inline
to view them in the IDE itself. Visualisations can be saved as gifs. The examples below show 2D (left) and a slice through 3D (right) results.
Scalar density evolution of impinging jet flow.
A vortical flow field is made by superimposing randomly generated vortices with varying strengths and radii. These are batchelor vortices in 2D and burger vortices in 3D. Animations show the vorticity field.
The classical example of the Taylor Green vortex. Examples show the scalar density evolution.
Based on Robert Bridsons' work [3]. A divergence-free flow field is generated as the initial condition by taking the curl of a potential scalar field, such as a Perlin noise field. Modulating the frequency of the noise field influences the vortex size. Animations shows the vorticity field.
A homogenous isotropic turbulent field with a predefined power spectrum is generated from the TurboGen package by Tony Saad [4]. This example uses the Kolmogorov spectrum. Please see the publication and GitHub repo (https://github.com/saadgroup/TurboGenPY) for more details. You can also use my turbulence synthesis code (https://github.com/flow-zone/4D-Turbulence-Generator). Animation shows the vorticity field.
Tested on a MacBook Pro (M3 Max) using MPS for GPU acceleration. The code is fully compatible with CUDA.
[1] Stable Fluids, Jos Stam.
[2] A Simple Fluid Solver based on the FFT. Jos Stam.
[3] Curl-Noise for Procedural Fluid Flow. R. Bridson.
[4] Scalable Tools for Generating Synthetic Isotropic Turbulence with Arbitrary Spectra. Tony Saad. et al
[5] FlowFixer: Using BFECC for Fluid Simulation B.M. Kim et al
[6] An Unconditionally Stable MacCormack Method. A. Selle. et al
[7] Visual Simulation of Smoke. R. Fedkiw. et al
[8] Fluid Simulation for Computer Graphics. R. Bridson.
- PyTorch
- NumPy
- Matplotlib
- PIL
- SciPy
This project is licensed under the MIT License. Enjoy CFD!