Create Your Own Navier-Stokes Spectral Method Fluid Simulation (With Python)

For today’s recreational coding exercise, we solve the Navier-Stokes equations for an incompressible viscous fluid. To do so, we will implement a spectral method. The Navier-Stokes equations approximately describe the motions of a fluid such as water. The equations can capture the phenomenon of turbulence. The system is also of interest in mathematics as a general proof for the existence and smoothness of solutions in 3D is one of the open Millenium Prize Problems.

You may find the accompanying Python code on GitHub.

Before diving in, below is a gif of what running our simulation looks like:

Navier-Stokes simulation

The Navier-Stokes Equations

We will consider the evolution of the velocity field v(x) of an incompressible fluid with viscosity ν, governed by the Navier-Stokes equations:

Navier-Stokes equations

where is the fluid pressure. The pressure field in the case of incompressible fluids is special: it is not a separate variable that is evolved in time (unlike in the case of compressible fluids). Instead, the pressure is the solution that keeps the velocity field divergence-free (the second equation). The velocity field experiences advection — the non-linear term on the left-hand side of the first equation — which can cause compression. The fluid also experiences diffusion due to the viscosity coefficient ν. The larger the viscosity coefficient, the ‘stickier’ the fluid becomes, like honey.

Click Here