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

<p>For today&rsquo;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&nbsp;<em>turbulence.&nbsp;</em>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&nbsp;<a href="https://en.wikipedia.org/wiki/Millennium_Prize_Problems" rel="noopener ugc nofollow" target="_blank">Millenium Prize Problems</a>.</p> <p>You may find the accompanying&nbsp;<a href="https://github.com/pmocz/navier-stokes-spectral-python" rel="noopener ugc nofollow" target="_blank">Python code on GitHub</a>.</p> <p>Before diving in, below is a gif of what running our simulation looks like:</p> <p><img alt="Navier-Stokes simulation" src="https://miro.medium.com/v2/resize:fit:270/1*aSTHQxFaXKuKtYyB7ybEIQ.gif" style="height:300px; width:300px" /></p> <h2>The Navier-Stokes Equations</h2> <p>We will consider the evolution of the velocity field&nbsp;<strong><em>v(x)&nbsp;</em></strong>of an incompressible fluid with viscosity&nbsp;<em>&nu;</em>, governed by the Navier-Stokes equations:</p> <p><img alt="Navier-Stokes equations" src="https://miro.medium.com/v2/resize:fit:508/1*w4PDPX5rUyOkhbYfmjkg8w.png" style="height:227px; width:565px" /></p> <p>where&nbsp;<em>P&nbsp;</em>is the fluid pressure. The pressure field in the case of&nbsp;<em>incompressible</em>&nbsp;fluids is special: it is not a separate variable that is evolved in time (unlike in the case of&nbsp;<a href="https://levelup.gitconnected.com/create-your-own-finite-volume-fluid-simulation-with-python-8f9eab0b8305" rel="noopener ugc nofollow" target="_blank">compressible fluids</a>). Instead, the pressure is the solution that keeps the velocity field divergence-free (the second equation). The velocity field experiences&nbsp;<em>advection &mdash;&nbsp;</em>the non-linear term on the left-hand side of the first equation &mdash; which can cause compression. The fluid also experiences&nbsp;<em>diffusion</em>&nbsp;due to the viscosity coefficient&nbsp;<em>&nu;</em>. The larger the viscosity coefficient, the &lsquo;stickier&rsquo; the fluid becomes, like honey.</p> <p><a href="https://levelup.gitconnected.com/create-your-own-navier-stokes-spectral-method-fluid-simulation-with-python-3f37405524f4">Click Here</a></p>