.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/spectral/plot_01_fourier_pseudospectral_burgers.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_pde_spectral_plot_01_fourier_pseudospectral_burgers.py: Fourier pseudo-spectral methods: Burgers' equation ================================================== Steven Orszag (1969-1971) and Kreiss and Oliger (1972) made the case for differentiating with the FFT instead of finite differences. For a smooth periodic function the error falls faster than any power of the grid spacing. Nonlinear terms are simply formed pointwise on the grid ("pseudo-spectral"). This script first compares Fourier and finite-difference derivatives, then solves viscous Burgers' equation :math:`u_t + u u_x = \nu u_{xx}` as a steepening wave, integrating the spectral ODE system with :func:`mathematicskit.integrators.rk4_integrate`. .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import BurgersEquation1D, fourier_derivative from mathematicskit.pde.visualizers import plot_snapshots .. GENERATED FROM PYTHON SOURCE LINES 23-25 Spectral versus finite-difference accuracy ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 25-44 .. code-block:: Python f = lambda x: np.exp(np.sin(x)) df = lambda x: np.cos(x) * np.exp(np.sin(x)) ns = np.array([6, 8, 12, 16, 24, 32, 48, 64]) spectral, central = [], [] for n in ns: x = np.linspace(0, 2 * np.pi, n, endpoint=False) h = x[1] - x[0] spectral.append(np.max(np.abs(fourier_derivative(f(x)) - df(x)))) central.append(np.max(np.abs((np.roll(f(x), -1) - np.roll(f(x), 1)) / (2 * h) - df(x)))) fig1, ax1 = plt.subplots(figsize=(6, 4)) ax1.semilogy(ns, np.maximum(spectral, 1e-17), "o-", label="Fourier (FFT)") ax1.semilogy(ns, central, "s-", label="central difference, $O(h^2)$") ax1.set_xlabel("grid points $N$") ax1.set_ylabel("max derivative error") ax1.set_title("Spectral accuracy: exponential convergence") ax1.legend() fig1.tight_layout() .. image-sg:: /api/gallery/pde/spectral/images/sphx_glr_plot_01_fourier_pseudospectral_burgers_001.png :alt: Spectral accuracy: exponential convergence :srcset: /api/gallery/pde/spectral/images/sphx_glr_plot_01_fourier_pseudospectral_burgers_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-47 A steepening Burgers wave ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-57 .. code-block:: Python burgers = BurgersEquation1D(lambda x: np.sin(x), n=128, nu=0.02) sol = burgers.solve(2.0, dt=0.5 * burgers.max_stable_dt(), save_every=40) print(f"mean of u: initial {sol.u[0].mean():+.2e}, final {sol.final.mean():+.2e} (conserved)") fig2, ax2 = plt.subplots(figsize=(7, 4)) plot_snapshots(sol, n_snapshots=6, ax=ax2) ax2.set_title(r"Burgers: $u_t + u u_x = 0.02\,u_{xx}$, pseudo-spectral + RK4") fig2.tight_layout() plt.show() .. image-sg:: /api/gallery/pde/spectral/images/sphx_glr_plot_01_fourier_pseudospectral_burgers_002.png :alt: Burgers: $u_t + u u_x = 0.02\,u_{xx}$, pseudo-spectral + RK4 :srcset: /api/gallery/pde/spectral/images/sphx_glr_plot_01_fourier_pseudospectral_burgers_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none mean of u: initial -2.44e-18, final +8.67e-18 (conserved) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.069 seconds) .. _sphx_glr_download_api_gallery_pde_spectral_plot_01_fourier_pseudospectral_burgers.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/pde/spectral/plot_01_fourier_pseudospectral_burgers.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_fourier_pseudospectral_burgers.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_fourier_pseudospectral_burgers.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_fourier_pseudospectral_burgers.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_