.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fluids/navier_stokes/plot_decaying_vortex_patch.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_fluids_navier_stokes_plot_decaying_vortex_patch.py: Viscous decay under the Navier-Stokes equations ==================================================== Navier (1822) and Stokes (1845) added a viscous friction term to Euler's inviscid equations of motion, giving the equation that still governs essentially all of fluid dynamics today, .. math:: \partial_t\mathbf{u} + (\mathbf{u}\cdot\nabla)\mathbf{u} = -\nabla p/\rho + \nu\nabla^2\mathbf{u}. :class:`~physicskit.fluids.systems.navier_stokes.NavierStokes2D` solves the equivalent, pressure-free vorticity-transport form of exactly this equation in two dimensions, on a doubly periodic domain, .. math:: \partial_t \omega + (\mathbf{u}\cdot\nabla)\omega = \nu \nabla^2 \omega, \qquad \nabla^2\psi=-\omega, \qquad \mathbf{u}=(\partial_y\psi,\,-\partial_x\psi), recovering the incompressible velocity :math:`\mathbf{u}` from an exact spectral Poisson solve for the streamfunction :math:`\psi` at every step. This example starts from a smooth, doubly periodic sinusoidal vortex patch :math:`\omega_0(x,y)=\sin x\sin y` and shows the signature of the viscous term directly, as a steady decay of the patch's peak vorticity. .. GENERATED FROM PYTHON SOURCE LINES 30-38 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from physicskit.fluids.systems.navier_stokes import NavierStokes2D from physicskit.fluids.visualizers import theme from physicskit.fluids.visualizers.flow_fields import plot_vorticity_field .. GENERATED FROM PYTHON SOURCE LINES 39-41 A doubly periodic, sinusoidal vortex patch ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python n, length = 64, 2 * np.pi solver = NavierStokes2D(n=n, length=length, nu=0.1) omega0 = np.sin(solver.X) * np.sin(solver.Y) .. GENERATED FROM PYTHON SOURCE LINES 47-52 Advance the vorticity-transport form of Navier-Stokes with RK4 --------------------------------------------------------------------- Advancing in chunks (rather than one 200-step call) costs nothing extra -- it is the same 200 RK4 steps in the same order -- but lets the peak vorticity be recorded along the way, for the decay-rate check below. .. GENERATED FROM PYTHON SOURCE LINES 52-63 .. code-block:: Python dt, chunk, n_chunks = 0.01, 20, 10 omega = omega0.copy() times, peaks = [0.0], [np.max(np.abs(omega0))] for i in range(n_chunks): result = solver.simulate(omega, dt=dt, steps=chunk) omega = result["omega"] times.append((i + 1) * dt * chunk) peaks.append(np.max(np.abs(omega))) times, peaks = np.array(times), np.array(peaks) .. GENERATED FROM PYTHON SOURCE LINES 64-66 Viscosity :math:`\nu\nabla^2\omega` steadily bleeds enstrophy out of the flow -- the hallmark of the added friction term. .. GENERATED FROM PYTHON SOURCE LINES 66-73 .. code-block:: Python fig, ax = plot_vorticity_field(solver.X, solver.Y, result["omega"], result["u"], result["v"]) ax.set_title("Viscous decay of a periodic vortex patch") fig.tight_layout() print(f"max|omega|: {np.max(np.abs(omega0)):.4f} -> {np.max(np.abs(result['omega'])):.4f}") .. image-sg:: /api/gallery/fluids/navier_stokes/images/sphx_glr_plot_decaying_vortex_patch_001.png :alt: Viscous decay of a periodic vortex patch :srcset: /api/gallery/fluids/navier_stokes/images/sphx_glr_plot_decaying_vortex_patch_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none max|omega|: 1.0000 -> 0.6703 .. GENERATED FROM PYTHON SOURCE LINES 74-84 The decay rate itself, checked against pure diffusion ----------------------------------------------------------- :math:`\omega_0=\sin x\sin y` is an eigenmode of the Laplacian (:math:`\nabla^2\omega_0=-2\omega_0`) whose induced streamfunction is exactly proportional to itself (:math:`\psi=\omega/2` here), which makes the advecting velocity :math:`(\partial_y\psi,-\partial_x\psi)` everywhere perpendicular to :math:`\nabla\omega` -- so the nonlinear advection term :math:`(\mathbf{u}\cdot\nabla)\omega` vanishes identically and only viscous diffusion acts: the peak vorticity should decay as :math:`e^{-2\nu t}`, with no free parameters to fit. .. GENERATED FROM PYTHON SOURCE LINES 84-99 .. code-block:: Python analytic_decay = peaks[0] * np.exp(-2.0 * solver.nu * times) fig, ax = plt.subplots(figsize=(6, 4)) ax.plot(times, peaks, "o", color=theme.PRIMARY, label="simulated max|omega|") ax.plot(times, analytic_decay, color=theme.ACCENT, ls="--", label=r"$e^{-2\nu t}$ (pure diffusion)") ax.set_xlabel("t") ax.set_ylabel(r"max$|\omega|$") ax.legend() ax.set_title("Peak-vorticity decay matches pure diffusion exactly") fig.tight_layout() print(f"max relative deviation from e^(-2 nu t): {np.max(np.abs(peaks - analytic_decay) / analytic_decay):.2e}") plt.show() .. image-sg:: /api/gallery/fluids/navier_stokes/images/sphx_glr_plot_decaying_vortex_patch_002.png :alt: Peak-vorticity decay matches pure diffusion exactly :srcset: /api/gallery/fluids/navier_stokes/images/sphx_glr_plot_decaying_vortex_patch_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none max relative deviation from e^(-2 nu t): 5.33e-14 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.375 seconds) .. _sphx_glr_download_api_gallery_fluids_navier_stokes_plot_decaying_vortex_patch.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_decaying_vortex_patch.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_decaying_vortex_patch.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_decaying_vortex_patch.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_