.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/spectral/plot_03_hopf_cole_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_03_hopf_cole_burgers.py: Burgers' equation and the Hopf-Cole transformation ================================================== J. M. Burgers (1948) proposed viscous Burgers' equation as the simplest model of nonlinear steepening balanced by viscosity. Eberhard Hopf (1950) and Julian Cole (1951) independently found that the substitution :math:`u = -2\nu\,\varphi_x/\varphi` turns the *nonlinear* viscous Burgers' equation :math:`u_t + u u_x = \nu u_{xx}` into the *linear* heat equation :math:`\varphi_t = \nu\varphi_{xx}`. A nonlinear PDE with shock-like fronts thus has an exact solution. This script uses it to watch a sine wave form a viscous shock whose width shrinks with :math:`\nu`, and to verify the pseudo-spectral Burgers solver to near machine precision. .. GENERATED FROM PYTHON SOURCE LINES 18-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import BurgersEquation1D, burgers_cole_hopf_solution .. GENERATED FROM PYTHON SOURCE LINES 24-26 Viscous shocks, exactly ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-37 .. code-block:: Python fig1, ax1 = plt.subplots(figsize=(7, 4)) for nu, color in ((0.3, "tab:blue"), (0.1, "tab:green"), (0.04, "tab:red")): x, u = burgers_cole_hopf_solution(np.sin, t=1.5, nu=nu, n=512) ax1.plot(x, u, color=color, label=rf"$\nu = {nu}$") ax1.plot(x, np.sin(x), "k--", lw=1, label="initial data") ax1.set_xlabel("$x$") ax1.set_title(r"Hopf-Cole: the front at $x = \pi$ sharpens as $\nu \to 0$") ax1.legend() fig1.tight_layout() .. image-sg:: /api/gallery/pde/spectral/images/sphx_glr_plot_03_hopf_cole_burgers_001.png :alt: Hopf-Cole: the front at $x = \pi$ sharpens as $\nu \to 0$ :srcset: /api/gallery/pde/spectral/images/sphx_glr_plot_03_hopf_cole_burgers_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-40 An exact benchmark for the numerical solver ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: Python nu, t = 0.1, 1.0 for n in (32, 64, 128): x, exact = burgers_cole_hopf_solution(np.sin, t=t, nu=nu, n=n) numerical = BurgersEquation1D(np.sin, n=n, nu=nu).solve(t, dt=1e-3).final print(f"n = {n:3d}: max |pseudo-spectral - Hopf-Cole| = {np.max(np.abs(numerical - exact)):.1e}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none n = 32: max |pseudo-spectral - Hopf-Cole| = 1.3e-04 n = 64: max |pseudo-spectral - Hopf-Cole| = 4.5e-08 n = 128: max |pseudo-spectral - Hopf-Cole| = 6.5e-13 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.159 seconds) .. _sphx_glr_download_api_gallery_pde_spectral_plot_03_hopf_cole_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_03_hopf_cole_burgers.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_hopf_cole_burgers.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_hopf_cole_burgers.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_hopf_cole_burgers.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_