MHD on patches
MHD2D solves ideal MHD in the plane, with all three components of the velocity and the
magnetic field, on the patches of a patchwork hierarchy. The model has no grid of its own:
a uniform grid is a hierarchy of one level, and refined patches are patches of finer
levels. The in-plane field is the curl of a vector potential on the nodes, so its
divergence is zero to round-off in every cell, across the edges of the patches too. This
tutorial runs the Orszag–Tang vortex, checks the order on an Alfvén wave and moves a
magnetic loop across a refined patch. Every case runs in seconds.
import numpy as npimport plasma_plotsfrom patchwork import AMRHierarchy
from carafe import MHD2D
plasma_plots.set_backend("plotly")plasma_plots.set_math("latex");1. The Orszag–Tang vortex
Section titled “1. The Orszag–Tang vortex”A periodic square with a smooth vortex in the velocity and the field. Shocks form and
interact by ; the pressure shows them. MHD2D.orszag_tang sets the standard
initial state (Gardiner and Stone 2005).
vortex = MHD2D.orszag_tang(64)steps = vortex.run(0.5)data = vortex.dataset()data.pressure.plasma.plot.slice(x="x", y="y")The divergence of the field (times the cell size) and the change of the conserved totals stay at round-off:
print(f"{steps} steps, max |div B| dx = {vortex.divergence():.1e}")print( f"pressure from {float(data.pressure.min()):.3f} to {float(data.pressure.max()):.3f}")354 steps, max |div B| dx = 8.3e-17pressure from 0.038 to 0.4892. Order of accuracy
Section titled “2. Order of accuracy”A circularly polarized Alfvén wave (Tóth 2000) is an exact nonlinear solution: it travels obliquely across a periodic box and is back where it started after one period. With the unlimited reconstruction the error of falls by four for every halving of the cells: second order.
errors = {}for cells in (8, 16, 32): wave, exact = MHD2D.alfven_wave(cells, limiter="none") wave.run(wave.period) by = wave.dataset()["magnetic_field_y"] errors[cells] = float(np.abs(by - exact["magnetic_field_y"]).mean())for coarse, fine in ((8, 16), (16, 32)): order = np.log2(errors[coarse] / errors[fine]) print( f"{coarse:3d} -> {fine:3d} cells: error {errors[fine]:.2e}, order {order:.2f}" ) 8 -> 16 cells: error 2.19e-03, order 1.91 16 -> 32 cells: error 5.37e-04, order 2.023. A loop across a refined patch
Section titled “3. A loop across a refined patch”A weak magnetic loop is carried by a uniform flow across a periodic box with a
patch refined by two in its middle. The field of the fine patch comes from the same :
the nodes the patch shares with the coarse level carry the fine values, so the flux
through a coarse face equals the sum of the fine fluxes through it, and the flux
register (patchwork.FluxRegister) does the same for the mass, momentum and energy.
cells = 32hierarchy = AMRHierarchy( (2 * cells, cells), ((-1.0, 1.0), (-0.5, 0.5)), periodic=(True, True))hierarchy.add_patch(1, ((cells - 16, cells + 16), (cells - 16, cells + 16)))loop = MHD2D(hierarchy)loop.set_state( 1.0, (2.0, 1.0, 0.0), 1.0, vector_potential=lambda x, y: 1e-3 * np.maximum(0.3 - np.hypot(x, y), 0.0),)start = loop.totals()
def show(model): """The magnetic pressure of the fine patch, over that of the base level.""" with plasma_plots.figure(1, 1, figsize=(8, 4)) as fig: model.dataset(0).magnetic_pressure.plasma.plot.slice(x="x", y="y", ax=fig[0]) fig[0].plot([-0.5, 0.5, 0.5, -0.5, -0.5], [-0.25, -0.25, 0.25, 0.25, -0.25])
show(loop)At = 0.25$ the centre of the loop sits on the upper right corner of the patch: three quarters of the loop are in coarse cells and the rest on the fine patch, and its field passes the coarse–fine edges without a kink:
loop.run(0.25)show(loop)change = {name: loop.totals()[name] - start[name] for name in start}print(f"max |div B| dx = {loop.divergence():.1e}")print(f"coarse-fine flux mismatch = {loop.composite_divergence():.1e}")print({name: f"{value:.1e}" for name, value in change.items()})max |div B| dx = 1.1e-19coarse-fine flux mismatch = 0.0e+00{'density': '-2.2e-16', 'momentum_x': '0.0e+00', 'momentum_y': '0.0e+00', 'momentum_z': '0.0e+00', 'energy': '0.0e+00', 'bz': '0.0e+00'}4. Rayleigh–Taylor with and without a field
Section titled “4. Rayleigh–Taylor with and without a field”Heavy gas on light under gravity, between conducting walls (MHD2D.rayleigh_taylor, the
setup of Stone and Gardiner 2008). Without a field the light gas rises in a bubble and the heavy
gas falls around it; a field of 0.1 along the interface stiffens it against bending
enough to hold this mode.
unmagnetised = MHD2D.rayleigh_taylor(20)unmagnetised.run(8.0)unmagnetised.dataset().density.plasma.plot.slice(x="x", y="y")magnetised = MHD2D.rayleigh_taylor(20, field=0.1)magnetised.run(8.0)magnetised.dataset().density.plasma.plot.slice(x="x", y="y")5. Resistivity and anisotropic conduction
Section titled “5. Resistivity and anisotropic conduction”With resistivity the field diffuses: a sinusoidal field decays as and
its energy goes into heat, so the total energy stays constant. With conduction heat flows
along the field (Braginskii’s ) and, much more slowly, across it
(). A hot spot in an oblique field spreads along the field lines only:
hierarchy = AMRHierarchy((64, 64), ((-1.0, 1.0), (-1.0, 1.0)), periodic=(True, True))spot = MHD2D( hierarchy, mean_field=(np.cos(np.pi / 6), np.sin(np.pi / 6)), conduction=(10.0, 0.0), limiter="mc",)spot.set_state( 1.0, (0.0, 0.0, 0.0), lambda x, y: 1.0 + 0.01 * np.exp(-(x**2 + y**2) / 0.02))spot.run(4e-3)data = spot.dataset()temperature = (data.pressure / data.density).assign_attrs(long_name="$T = p/\\rho$")temperature.plasma.plot.slice(x="x", y="y")The limited gradient across the faces (Sharma and Hammett 2007) keeps the temperature from falling below its initial minimum, which the plain average would not:
print(f"min T - 1 = {float(temperature.min()) - 1.0:.1e}")min T - 1 = 0.0e+00What is next
Section titled “What is next”Patches that follow the solution and levels that take their own time steps (in patchwork), and the gallery studies of the MHD instabilities.