Create Your Own Navier-Stokes Spectral Method Fluid Simulation (With Python)
<p>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 <em>turbulence. </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 <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 <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 <strong><em>v(x) </em></strong>of an incompressible fluid with viscosity <em>ν</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 <em>P </em>is the fluid pressure. The pressure field in the case of <em>incompressible</em> fluids is special: it is not a separate variable that is evolved in time (unlike in the case of <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 <em>advection — </em>the non-linear term on the left-hand side of the first equation — which can cause compression. The fluid also experiences <em>diffusion</em> due to the viscosity coefficient <em>ν</em>. The larger the viscosity coefficient, the ‘stickier’ 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>