.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/heat/plot_03_method_of_lines_2d_heat.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_heat_plot_03_method_of_lines_2d_heat.py: The method of lines: a hot plate as a system of ODEs ==================================================== The method of lines discretizes only space. On a grid, the 2D heat equation :math:`u_t = u_{xx} + u_{yy}` becomes one ODE per interior point, :math:`dU/dt = L U`, which any ODE integrator can then advance. Here that integrator is :func:`mathematicskit.integrators.rk4_integrate` (the same one :mod:`mathematicskit.ode_dynamics` uses) and, for comparison, the adaptive :func:`~mathematicskit.integrators.dopri5_integrate`. The ODE system is *stiff*, so RK4's stable step shrinks like :math:`\Delta x^2`. .. GENERATED FROM PYTHON SOURCE LINES 16-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import HeatEquation2D from mathematicskit.pde.visualizers import plot_field_2d def hot_square(X, Y): return np.where((np.abs(X - 0.5) < 0.2) & (np.abs(Y - 0.5) < 0.2), 1.0, 0.0) heat = HeatEquation2D(hot_square, n=(41, 41)) print(f"ODE system size: {heat.state0.size} unknowns (one per interior grid point)") print(f"RK4 stable step on this grid: dt <= {heat.max_stable_dt('rk4'):.2e}") .. rst-class:: sphx-glr-script-out .. code-block:: none ODE system size: 1521 unknowns (one per interior grid point) RK4 stable step on this grid: dt <= 2.18e-04 .. GENERATED FROM PYTHON SOURCE LINES 32-34 Integrate the semi-discrete system with RK4 ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-44 .. code-block:: Python sol = heat.solve(0.04, dt=0.9 * heat.max_stable_dt("rk4"), save_every=40) fig, axes = plt.subplots(1, 3, figsize=(12, 3.8)) for ax, t in zip(axes, (0.0, 0.01, 0.04), strict=False): k = int(np.argmin(np.abs(sol.t - t))) plot_field_2d(sol, time_index=k, ax=ax) ax.set_title(f"t = {sol.t[k]:.3f}") fig.suptitle("Method of lines + RK4: the hot square spreads and cools") fig.tight_layout() .. image-sg:: /api/gallery/pde/heat/images/sphx_glr_plot_03_method_of_lines_2d_heat_001.png :alt: Method of lines + RK4: the hot square spreads and cools, t = 0.000, t = 0.008, t = 0.040 :srcset: /api/gallery/pde/heat/images/sphx_glr_plot_03_method_of_lines_2d_heat_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-47 An adaptive integrator agrees ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-53 .. code-block:: Python adaptive = heat.solve(0.04, method="dopri5", rtol=1e-6, atol=1e-9) print(f"dopri5 accepted steps: {len(adaptive.t) - 1}") print(f"max |RK4 - dopri5| at t = 0.04: {np.max(np.abs(sol.final - adaptive.final)):.2e}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none dopri5 accepted steps: 170 max |RK4 - dopri5| at t = 0.04: 3.68e-08 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.596 seconds) .. _sphx_glr_download_api_gallery_pde_heat_plot_03_method_of_lines_2d_heat.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/heat/plot_03_method_of_lines_2d_heat.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_method_of_lines_2d_heat.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_method_of_lines_2d_heat.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_method_of_lines_2d_heat.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_