---
title: "Magnetohydrodynamics"
---

Docstrings from `src/magnetic_field.jl`, `src/mhd_system.jl`.

## Exported

### `MagneticField` {#MagneticField}

Source: [`src/magnetic_field.jl:19`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L19)

```julia
MagneticField{T}
```

The face-centred magnetic field `b` of a simulation with `mhd = true`, as
`sim.B`: `sim.B[d]` is the `(1, i, j, k)` array of the component normal to the
faces along direction `d`, with one more face than cells along `d` when `d` is
active, and `sim.B[d][1, i, j, k]` is the field on the *low* face of cell
`(i, j, k)` (the convention of Quokka and of [`reconstruct!`](hydro.md#reconstruct-bang)). Ghost faces
are filled by the boundary conditions, like ghost cells.

The field is in units in which the magnetic pressure is `|b|^2 / 2`, as in Quokka
(Heaviside-Lorentz, `b = B / sqrt(4 pi)` in Gaussian units). The cell-centred
total energy `sim.U[IEGAS]` includes the magnetic energy `|b_cc|^2 / 2`, with
`b_cc` the average of a cell's two faces along each direction
([`magnetic_energy`](#magnetic_energy)).

## Internals

### `cell_centred_field` {#cell_centred_field}

Source: [`src/magnetic_field.jl:51`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L51)

```julia
cell_centred_field(B, d, i, j, k)
```

Component `d` of the cell-centred field of cell `(i, j, k)`: the average of its
low and high face along `d`. Along an inactive direction the two are the same face.

### `magnetic_energy` {#magnetic_energy}

Source: [`src/magnetic_field.jl:63`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L63)

```julia
magnetic_energy(B, i, j, k)
```

The magnetic energy density `|b_cc|^2 / 2` of cell `(i, j, k)`, from the
cell-centred field ([`cell_centred_field`](#cell_centred_field)), as Quokka takes it. It is the
part of the cell's total energy that is neither kinetic nor internal, so every
place that takes the internal energy as `E - E_kin` subtracts it as well, and
every place that writes `E = E_int + E_kin` adds it back.

`B === nothing`, a simulation without MHD, gives `false`, a zero of every float
type: `x - false` is `x`, so the hydrodynamic paths keep their numbers.

### `fast_magnetosonic_speed` {#fast_magnetosonic_speed}

Source: [`src/magnetic_field.jl:84`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L84)

```julia
fast_magnetosonic_speed(gamma, rho, P, bn, bt1, bt2)
```

The fast magnetosonic speed for propagation along the direction of `bn`, with
`bt1`, `bt2` the two transverse components: Quokka's `FastMagnetoSonicSpeed`
(`HydroState.hpp`), with `gamma P` for the square of the sound speed times `rho`.

### `check_mhd_boundaries` {#check_mhd_boundaries}

Source: [`src/magnetic_field.jl:103`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L103)

```julia
check_mhd_boundaries(boundaries)
```

Throw an `ArgumentError` unless every face of `boundaries` is [`Periodic`](simulation.md#Periodic)
or [`Outflow`](simulation.md#Outflow), the two conditions the face-centred field has.

### `mhd_ghost_energy!` {#mhd_ghost_energy-bang}

Source: [`src/magnetic_field.jl:200`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L200)

```julia
mhd_ghost_energy!(U, B, boundaries, grid)
```

Give every ghost cell beyond an [`Outflow`](simulation.md#Outflow) boundary the internal energy of
the interior cell it was copied from, by replacing that cell's magnetic energy in
the copied total energy with the ghost cell's own: Quokka's step 3 of
`applyMHDDiodeBC`. Its ghost faces differ from the source cell's (the normal one
is integrated, see [`fill_ghosts!`](simulation.md#fill_ghosts-bang)), so a plain copy of the total energy
would give the ghost cell another pressure. Call it after the ghost cells of `U`
and the ghost faces of `B` are filled. Without an outflow boundary it does nothing:
a periodic ghost cell's faces are images of its source's, and so is its energy.

### `divergence_b` {#divergence_b}

Source: [`src/magnetic_field.jl:300`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/magnetic_field.jl#L300)

```julia
divergence_b(sim) -> Array{T,3}
```

The discrete divergence `sum_d (b_d[high face] - b_d[low face]) / dx_d` of every
interior cell, the quantity constrained transport keeps at round-off. Directions
that are inactive contribute nothing.

### `mhd_riemann_ghosts` {#mhd_riemann_ghosts}

Source: [`src/mhd_system.jl:30`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/mhd_system.jl#L30)

```julia
mhd_riemann_ghosts(reconstruction) -> Int
```

How many rows of faces beyond the interior, across the sweep direction, the MHD
sweep solves Riemann problems on: the Q26 EMF reconstructs the face velocity
`u*` along each transverse direction to the edges, and a reconstruction of the
edges of the interior reads that many faces past them (paper Sec. 2.3.1: 1 for
PCM, 2 for PLM, 3 for PPM). It is Quokka's `MinimumHydroRiemannGhost`, which
takes 3 for Q26 whatever the order. The simulation's ghost width is one more,
for the carbuncle input of the outermost of those faces.

### `MHDWorkspace` {#MHDWorkspace}

Source: [`src/mhd_system.jl:45`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/mhd_system.jl#L45)

```julia
MHDWorkspace{T}
```

Scratch arrays of the MHD step, built with the simulation:

  - `bcc`: the cell-centred field, `(3, i, j, k)`, every cell, ghosts included;
  - `bL`, `bR`: its interface states along the current sweep;
  - `fspd[d]`: `(2, faces of d)`, the bounding speeds `alpha^-`, `alpha^+` the
    Riemann solver returned on each face normal to `d` (Quokka's
    `fast_mhd_wavespeeds`);
  - `ustar[d]`: `(1, faces of d)`, the Q26 face velocity of each face,
    `(alpha^+ u_L + alpha^- u_R) / (alpha^+ + alpha^-)` (Quokka's `facevel`);
  - `emf[w]`: `(1, edges along w)`, the EMF on each edge along `w`, with one more
    edge than cells along each active direction across `w`;
  - `B0[d]`: the field at the start of the step, for the Runge-Kutta stages.

The face arrays are whole-grid and kept between the sweeps, because the EMF of an
edge reads the faces of two directions. Along an inactive direction `d`,
`fspd[d]` holds ones and is never written: LD04 weights the corner EMFs by these
speeds, and when the two sides of every face along `d` are equal the corners pair
up and the speeds along `d` cancel, exactly for one.

### `constrained_transport!` {#constrained_transport-bang}

Source: [`src/mhd_system.jl:304`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/mhd_system.jl#L304)

```julia
constrained_transport!(sim, update)
```

The EMF on every edge that bounds an interior face ([`MHDWorkspace`](#MHDWorkspace)), from
the face arrays the sweeps left, and then, with `update = (a, B0, b, c dt)`, the
Runge-Kutta stage of the face-centred field, `B = a B0 + b B + c dt curl(eps)` on
every interior face. Any linear combination of divergence-free fields is
divergence-free and `curl` is a discrete curl, so every stage of every tableau
keeps `div b` where it was.

### `mhd_prepare!` {#mhd_prepare-bang}

Source: [`src/mhd_system.jl:337`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/mhd_system.jl#L337)

```julia
mhd_prepare!(sim)
```

What the MHD stage needs before its sweeps, after `cons2prim!`: the cell-centred
field of every cell, and the face velocity of every face normal to an inactive
direction, the cell's own velocity.

