---
title: "Hydrodynamics"
---

Docstrings from `src/eos.jl`, `src/hydro_system.jl`, `src/tiling.jl`, `src/limiters.jl`, `src/reconstruction.jl`, `src/riemann/`.

## Exported

### `IdealGas` {#IdealGas}

Source: [`src/eos.jl:36`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L36)

```julia
IdealGas(; gamma = 5/3, cv = 1.0)
```

Ideal gas with constant adiabatic index `gamma` and constant specific heat at
constant volume `cv` (per unit mass).

`gamma` and `cv` are independent inputs so that the code stays unit-agnostic:
in code units the usual choice is `cv = 1`, while in CGS you want
`cv = `[`cv_from_mu`](#cv_from_mu)`(mu, gamma)`.

`cv` only enters through the gas temperature, i.e. only when radiation is
enabled; pure hydrodynamics is unaffected by it.

### `cv_from_mu` {#cv_from_mu}

Source: [`src/eos.jl:56`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L56)

```julia
cv_from_mu(mu, gamma; k_B = CGS.k_B, m_u = CGS.m_u)
```

Specific heat at constant volume of an ideal gas of mean molecular weight `mu`,
`cv = k_B / (mu m_u (gamma - 1))`. Defaults give CGS units (erg g⁻¹ K⁻¹).

### `Floors` {#Floors}

Source: [`src/hydro_system.jl:4`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L4)

```julia
Floors(; rho = 1e-30, pressure = 1e-30, erad = 1e-30)
```

Lower bounds enforced on the state after every stage. Cells that fall below
`rho` are reset to `(rho, v = 0, p = pressure)`; cells whose internal energy
falls below `pressure/(gamma-1)` keep their momentum and have only the thermal
part reset.

### `MinMod` {#MinMod}

Source: [`src/limiters.jl:14`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/limiters.jl#L14)

```julia
MinMod()
```

`minmod(dL, dR)`. The most diffusive of the TVD limiters, and the most robust.

### `MonotonizedCentral` {#MonotonizedCentral}

Source: [`src/limiters.jl:21`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/limiters.jl#L21)

```julia
MonotonizedCentral()
```

The MC limiter of van Leer (1977), `minmod(2dL, (dL+dR)/2, 2dR)`. The default:
sharper than minmod while remaining TVD.

### `VanLeer` {#VanLeer}

Source: [`src/limiters.jl:29`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/limiters.jl#L29)

```julia
VanLeer()
```

The harmonic-mean limiter `2 dL dR / (dL + dR)` for `dL dR > 0`, else zero.
Smooth, which helps steady-state convergence.

### `Sweby` {#Sweby}

Source: [`src/limiters.jl:37`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/limiters.jl#L37)

```julia
Sweby(beta = 1.5)
```

Generalised minmod limiter `minmod(beta dL, (dL+dR)/2, beta dR)`. TVD for
`1 <= beta <= 2`; `beta = 1` is minmod and `beta = 2` is MC.

### `PCM` {#PCM}

Source: [`src/reconstruction.jl:21`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L21)

```julia
PCM()
```

Piecewise-constant (Godunov) reconstruction. First-order accurate; useful as a
robustness fallback and for verifying that higher-order schemes actually help.

### `PLM` {#PLM}

Source: [`src/reconstruction.jl:29`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L29)

```julia
PLM(limiter = MonotonizedCentral())
```

Piecewise-linear reconstruction with a TVD slope limiter. Second-order accurate
in smooth regions.

### `PPM` {#PPM}

Source: [`src/reconstruction.jl:40`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L40)

```julia
PPM()
```

Piecewise-parabolic method of Colella & Woodward (1984), with the interface
constraint and monotonicity correction used by Athena++ / Quokka (Mignone 2014):
edge values are clamped to the range of the three neighbouring cell averages,
and at a local extremum the parabola collapses to an MC-limited line.

Third-order accurate on smooth data away from extrema; this is the recommended
default.

### `HLLMHD` {#HLLMHD}

Source: [`src/riemann/hll_mhd.jl:7`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/hll_mhd.jl#L7)

```julia
HLLMHD()
```

Two-wave HLL solver for ideal MHD: one intermediate state between the two fast
waves, with HLLD's bounding speeds (Miyoshi & Kusano 2005, eq. 67),
`S_L = min(u_L, u_R) - max(c_fL, c_fR)` and `S_R = max(u_L, u_R) + max(c_fL, c_fR)`,
returned as `am = -min(0, S_L)` and `ap = max(0, S_R)`.

For benchmarking [`HLLD`](#HLLD): the two share their speeds, so constrained
transport gets the same `alpha^±` from either, and they differ only inside the fan,
where HLL smears the contact and the rotational discontinuities that HLLD resolves.
It is the flux HLLD blends toward with the weight of Minoshima & Miyoshi (2021),
and with LD04 averaging, itself the HLL flux of the transverse field along one
direction, it makes a scheme whose gas and field see the same two-wave model.
Quokka has no MHD HLL solver. Rebuilds the energy of each side from the pressure,
`P / (gamma - 1)`, as HLLD does, and uses neither the carbuncle input nor `sL.E`.

### `LLF` {#LLF}

Source: [`src/riemann/llf.jl:7`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/llf.jl#L7)

```julia
LLF()
```

Local Lax-Friedrichs, also called Rusanov: a single wave at the fastest signal
speed `max(|u| + c_s)`, with the whole Riemann fan replaced by that one
estimate.

The most diffusive and most robust of the solvers, and the useful control in a
comparison: whatever [`HLLC`](#HLLC) buys is the difference between them. It
smears contact discontinuities badly, because it has no contact wave to resolve
them with, while shocks -- which are self-steepening -- survive comparatively
well. Uses neither `gamma` nor the carbuncle correction.

```julia
compare(Honghu.Problems.sod_shock_tube,
        (HLLC = (riemann = HLLC(),), LLF = (riemann = LLF(),)); sizes = (64, 128, 256))
```

### `LLFMHD` {#LLFMHD}

Source: [`src/riemann/llf_mhd.jl:7`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/llf_mhd.jl#L7)

```julia
LLFMHD()
```

Local Lax-Friedrichs (Rusanov) solver for ideal MHD: one wave at the fastest
signal speed `max(|u| + c_f)` over the two sides, with `c_f` the fast
magnetosonic speed along the face normal. Quokka's `LLF_MHD`.

The control in a comparison, as [`LLF`](#LLF) is for [`HLLC`](#HLLC): it smears
contacts and rotational discontinuities, which [`HLLD`](#HLLD) resolves. Quokka
uses it for its first-order fallback fluxes. The bounding speeds it returns,
`am = -min(0, uL - c_fL, uR - c_fR)` and `ap = max(0, uL + c_fL, uR + c_fR)`,
are Davis's estimates rather than HLLD's, as in Quokka. Uses neither the carbuncle
input nor `sL.E`: the energy is rebuilt from the pressure, `P/(gamma - 1)`, as
Quokka does.

### `HydroState` {#HydroState}

Source: [`src/riemann/state.jl:10`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/state.jl#L10)

```julia
HydroState(rho, u, v, w, P, cs, E, by = 0, bz = 0)
```

One side of an interface, in *canonical* form: `u` is the velocity normal to the
interface and `v`, `w` the two transverse components, and likewise `by`, `bz`
for the magnetic field. The caller permutes the components; the solvers never
think about direction.

Field names match `quokka::HydroState`, the transverse field included: one state
type serves both the hydrodynamic and the MHD solvers, as in Quokka, so a solver
transliterates field for field. The hydrodynamic solvers ignore `by` and `bz`, and
the seven-argument constructor sets them to zero. It is an immutable struct of
concrete floats, so it lives on the stack and costs exactly what passing the
numbers separately costs -- `test_performance.jl` holds that claim to account.
The normal field is not a field of the state: it is single-valued at the face and
is passed to [`mhd_flux`](#mhd_flux) on its own, as Quokka passes `bx`.

| field | meaning |
|:---|:---|
| `rho` | mass density |
| `u` | normal velocity |
| `v`, `w` | transverse velocities |
| `P` | pressure |
| `cs` | adiabatic sound speed |
| `E` | total energy density, the magnetic energy included |
| `by`, `bz` | transverse magnetic field |

### `HLLD` {#HLLD}

Source: [`src/riemann/hlld.jl:12`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/hlld.jl#L12)

```julia
HLLD()
```

Five-wave HLLD solver of Miyoshi & Kusano (2005): the two fast waves, the two
rotational (Alfven) waves and the contact, with the low-Mach pressure and the
carbuncle blending of Minoshima & Miyoshi (2021). Quokka's `HLLD`, and the
default solver of a simulation with `mhd = true`.

  - The bounding speeds are MK5's eq. 67, `S_L = min(u_L, u_R) - max(c_fL, c_fR)`
    and `S_R = max(u_L, u_R) + max(c_fL, c_fR)`, returned as
    `am = -min(0, S_L)` and `ap = max(0, S_R)`.
  - The total pressure of the star states carries MM21's eq. 15 correction, a
    factor `phi = chi (2 - chi)` on the velocity-jump term with
    `chi = min(1, c_u / c_f)`, so that pressure perturbations scale as `M^2` at
    low Mach number.
  - Inside the fan the flux is blended toward HLL by MM21's eq. 10 sensor,
    `theta = min(1, (c_f - min(du, 0)) / (c_f - min(dw, 0)))^4`, with `du` the jump
    of the reconstructed normal velocity and `dw` the transverse velocity jump
    the sweep passes in, so that a shock aligned with the grid does not grow a
    carbuncle.

Like Quokka's, it rebuilds the energy of each side from the pressure,
`P / (gamma - 1)`, and does not read `sL.E`.

### `HLLC` {#HLLC}

Source: [`src/riemann/hllc.jl:14`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/hllc.jl#L14)

```julia
HLLC()
```

Three-wave HLLC solver: left- and right-going acoustic waves plus the contact
discontinuity. Resolving the contact is what keeps a material interface sharp,
which is why this is the default.

Structurally a gamma-law solver -- `gamma` enters the Roe-averaged sound speed
and Rider's correction as a scalar -- so it is not appropriate for a genuinely
non-ideal equation of state. See [`AbstractEOS`](#AbstractEOS).

### `AbstractRiemannSolver` {#AbstractRiemannSolver}

Source: [`src/riemann/state.jl:57`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/state.jl#L57)

```julia
AbstractRiemannSolver
```

Supertype for the interface flux solvers: [`HLLC`](#HLLC) and [`LLF`](#LLF).
Pass one to [`Simulation`](simulation.md#Simulation) as `riemann = HLLC()`.

A concrete subtype implements

    riemann_flux(solver, sL::HydroState, sR::HydroState, gamma, du, dw) -> SVec{5}

returning the conservative flux `(mass, normal momentum, transverse momentum 1,
transverse momentum 2, energy)` in canonical form.

Every solver takes the same arguments and ignores what it does not need --
`LLF` uses neither `gamma` nor the carbuncle inputs. That is a small departure
from Quokka, whose per-solver argument lists differ, and it is deliberate: the
sweep computes `du` and `dw` unconditionally in both codes, so a uniform
signature costs nothing and keeps one call site instead of one per solver.

`test/test_extensions.jl` pins this contract with a toy solver.

### `AbstractMHDRiemannSolver` {#AbstractMHDRiemannSolver}

Source: [`src/riemann/state.jl:80`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/state.jl#L80)

```julia
AbstractMHDRiemannSolver <: AbstractRiemannSolver
```

Supertype of the MHD interface solvers, [`LLFMHD`](#LLFMHD) and [`HLLD`](#HLLD),
which a simulation with `mhd = true` takes as `riemann`. A concrete subtype
implements

    mhd_flux(solver, sL::HydroState, sR::HydroState, gamma, bx, dw) -> (SVec{5}, am, ap)

the counterpart of Quokka's `HLLD(sL, sR, gamma, bx, perp_v_jump)`. It returns the
flux of `(mass, normal momentum, transverse momentum 1, transverse momentum 2,
energy)` in canonical form, and the two non-negative bounding speeds of the
Riemann fan, `am` to the left and `ap` to the right, which constrained transport
needs at every face: for the face velocity and the averaging of the EMFs. `bx`
is the normal field at the face and `dw` the carbuncle input of
[`riemann_flux`](#riemann_flux). The flux of the transverse field is not returned: the
field is advanced by the EMFs, never by a flux.

### `riemann_flux` {#riemann_flux}

Source: [`src/riemann/state.jl:108`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/state.jl#L108)

```julia
riemann_flux(solver, sL, sR, gamma, du, dw) -> SVec{5}
```

The conservative flux across one interface in canonical form.

`du` is the jump in normal velocity across the interface and `dw` the smallest
transverse velocity difference in the two adjoining cells. Both feed the
carbuncle correction and may be passed as zero to disable it; solvers without
one ignore them.

## Internals

### `AbstractEOS` {#AbstractEOS}

Source: [`src/eos.jl:6`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L6)

```julia
AbstractEOS
```

Supertype for equations of state.

A concrete subtype `E` must implement

  - [`pressure`](#pressure)`(eos, rho, eint)`
  - [`internal_energy`](simulation.md#internal_energy)`(eos, rho, p)`
  - [`soundspeed`](#soundspeed)`(eos, rho, p)`
  - [`adiabatic_index`](#adiabatic_index)`(eos)`
  - [`temperature`](#temperature)`(eos, rho, eint)`
  - [`eint_from_temperature`](#eint_from_temperature)`(eos, rho, T)`
  - [`heat_capacity`](#heat_capacity)`(eos, rho, T)`

Only the first four are needed for pure hydrodynamics; the rest are used by the
radiation-matter coupling. `test/test_extensions.jl` pins this list: it defines
an EOS implementing exactly these methods and runs a simulation with it.

::: {.callout-warning title="The built-in HLLC solver assumes a gamma-law gas"}
[`HLLC`](#HLLC) uses `adiabatic_index(eos)` as a scalar in the Roe-averaged
sound speed and in Rider's wave-speed correction, so it is *structurally* a
gamma-law solver. Defining `adiabatic_index` on a genuinely non-ideal EOS
silences the `MethodError` and then silently uses an effective constant
gamma, which is worse than failing. Such an EOS needs its own
[`AbstractRiemannSolver`](#AbstractRiemannSolver); [`LLF`](#LLF) is a good starting point,
since it needs only the sound speed.
:::

### `pressure` {#pressure}

Source: [`src/eos.jl:64`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L64)

```julia
pressure(eos, rho, eint)
```

Thermal pressure from mass density `rho` and internal energy *density* `eint`.

### `soundspeed` {#soundspeed}

Source: [`src/eos.jl:78`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L78)

```julia
soundspeed(eos, rho, p)
```

Adiabatic sound speed.

### `temperature` {#temperature}

Source: [`src/eos.jl:85`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L85)

```julia
temperature(eos, rho, eint)
```

Gas temperature from mass density and internal energy density.

### `eint_from_temperature` {#eint_from_temperature}

Source: [`src/eos.jl:92`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L92)

```julia
eint_from_temperature(eos, rho, T)
```

Internal energy density from mass density and temperature.

### `heat_capacity` {#heat_capacity}

Source: [`src/eos.jl:99`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L99)

```julia
heat_capacity(eos, rho, T)
```

Volumetric heat capacity `∂(eint)/∂T` at fixed density.

### `adiabatic_index` {#adiabatic_index}

Source: [`src/eos.jl:106`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/eos.jl#L106)

```julia
adiabatic_index(eos)
```

Adiabatic index `gamma`.

### `enforce_floors!` {#enforce_floors-bang}

Source: [`src/hydro_system.jl:22`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L22)

```julia
enforce_floors!(U, eos, grid, floors, nscalars = 0; bfield = nothing) -> Int
```

Project the conserved gas state onto the admissible set, over the whole array,
ghost cells included. Cells below `floors.rho` are reset to a quiescent floor
state; cells whose internal energy is below `floors.pressure/(gamma-1)` keep
their momentum and have only the thermal part reset.

With MHD, `bfield` is the simulation's [`MagneticField`](mhd.md#MagneticField): the total energy
then holds the magnetic energy as well, which a floor keeps, and only the internal
energy is reset, to a value that [`cons2prim!`](#cons2prim-bang) is guaranteed to read as
above the floor. Where the magnetic energy is large the internal energy is then
known only to its round-off, which is what a dual-energy formalism would cure.

Returns the number of cells repaired, which the driver accumulates so that a
silently unphysical run is visible. Only the hydro repairs are counted: a
density reset also rescales the `nscalars` mass-scalar rows, but that is not a
repair of its own.

The density reset keeps each mass fraction `X_s = rho X_s / rho` of the cell and
writes `floors.rho * X_s`, so a scalar is not erased by the floor. Quokka scales
`rho X_s` by `rho / rho_new` and renormalises; this departs from it on purpose,
because a floored cell is a state the scheme has already lost and the one thing
worth keeping is what was in it. A cell whose density was not positive and
finite has no mass fraction to keep and gets `X_s = 0`.

This is deliberately a *separate* operation from [`cons2prim!`](#cons2prim-bang), matching
Quokka's split between `EnforceLimits` and its `const`-qualified
`ComputePrimVars`. The two used to be fused, which meant that reading a
diagnostic field silently mutated the state being diagnosed: results depended on
how often you looked, and the repairs went uncounted. Keep them apart.

### `enforce_scalar_floors!` {#enforce_scalar_floors-bang}

Source: [`src/hydro_system.jl:113`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L113)

```julia
enforce_scalar_floors!(U, grid, nscalars, renormalise = false) -> Int
```

Set every negative or NaN mass-scalar density `rho X_s` to zero, over the whole
array, ghost cells included, and return the number of cells changed. A scalar is
a mass fraction and cannot be negative; a small negative value is what
reconstruction leaves behind at a sharp front. The count goes to
`Diagnostics.floors_scalar`, kept apart from the gas floors because a scalar
clip says nothing about the gas.

With `renormalise = false`, the default, there is nothing more: the scalars are
passive and independent, and `sum_s X_s` is not forced to one.

With `renormalise = true` the scalars of a cell are then multiplied by
`rho / sum_s rho X_s`, wherever that sum and `rho` are positive, so that the mass
fractions add up to one. A simulation with a chemistry network passes `true`,
because its scalars are the species of one gas and must add up to `rho`. Each
`rho X_s` is reconstructed and limited on its own, so the species fluxes do not
add up to the mass flux, and the sum drifts from `rho` by a few percent in ten
steps of a run with PLM or PPM (3.7% for PPM on a density wave; PCM keeps it). This
is Quokka's cure, the renormalisation in the mass-scalar part of `EnforceLimits`
(`hydro_system.hpp`, after the density floor). The conservative alternative, a
consistent multi-fluid advection (CMA, Plewa & Mueller 1999), makes the species
fluxes add up to the mass flux by construction; it is not implemented. The
renormalisation is not a repair and is not counted.

Separate from [`enforce_floors!`](#enforce_floors-bang) so that a run without scalars never
reaches it.

### `cons2prim!` {#cons2prim-bang}

Source: [`src/hydro_system.jl:178`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L178)

```julia
cons2prim!(prim, U, eos, grid, nscalars = 0; bfield = nothing)
```

Fill `prim = (rho, vx, vy, vz, p)` from the conserved state `U` over the whole
array, ghost cells included. With MHD, `bfield` is the simulation's
[`MagneticField`](mhd.md#MagneticField), and the pressure is that of `E - E_kin - E_mag`. The `nscalars` mass-scalar rows are copied
unchanged, so they hold `rho X_s` and not `X_s`: that is what is reconstructed,
as in Quokka.

`U` is **only read**. Every caller — the right-hand side, `timestep`, and every
diagnostic and plotting path — gets the same guarantee, so looking at a
simulation cannot change it. Admissibility is the caller's job, via
[`enforce_floors!`](#enforce_floors-bang); on a state that has not been floored this returns
whatever the arithmetic gives, including negative pressures.

### `prim2cons!` {#prim2cons-bang}

Source: [`src/hydro_system.jl:219`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L219)

```julia
prim2cons!(U, prim, eos, grid)
```

Fill the conserved state from primitives over the whole array.

### `hydro_fluxes!` {#hydro_fluxes-bang}

Source: [`src/hydro_system.jl:236`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L236)

```julia
hydro_fluxes!(flux, qL, qR, prim, eos, solver, Val(D), grid, nscalars = 0)
```

Solve the Riemann problem at every interface normal to direction `D` with
`solver` (an [`AbstractRiemannSolver`](#AbstractRiemannSolver)) and store the resulting
conservative flux.

If the reconstruction produced an inadmissible state (non-positive density or
pressure, or a NaN) the interface falls back to the piecewise-constant state of
the adjoining cell, which is guaranteed admissible because the floors have
already been applied.

The `nscalars` mass scalars are carried by the Riemann mass flux `F_rho`: the
flux of `rho X_s` is `F_rho` times the mass fraction of the upwind side, taken
from the reconstructed `rho X_s / rho` on that side (or from the cell-centred
state if that side fell back). So a uniform `X_s` is advected exactly, to
round-off, whatever the solver does, because the flux of `rho X_s` and `F_rho`
move together. The face fraction is a ratio of two separately limited
reconstructions, so `X_s` can overshoot where `rho` and `X_s` jump together;
[`enforce_scalar_floors!`](#enforce_scalar_floors-bang) repairs a negative result.

The solvers themselves do not know about scalars. Quokka's HLLC and LLF instead
carry them as extra conserved components inside the solver (`HLLC.hpp:123-129`,
`LLF.hpp:26-33`). For HLLC that is the same thing as the mass flux times the
upwind `X_s`. For LLF it is a deliberate departure: it keeps the
[`riemann_flux`](#riemann_flux) contract of five components, and it is less diffusive
for the scalar than LLF's own dissipation term would be.

The carbuncle inputs `du` and `dw` are computed for every solver, not gated on
whether the chosen one consumes them. Quokka does the same
(`hydro_system.hpp:1484-1498`), and reuses the intermediates for its velocity
divergence; the reads are 18 per face in 3D and 2 in 1D, against a reconstruction
and a Riemann solve.

### `flux_divergence!` {#flux_divergence-bang}

Source: [`src/hydro_system.jl:393`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L393)

```julia
flux_divergence!(dU, flux, Val(D), grid, nvar, overwrite = false, update = nothing)
```

Subtract `(F_{i+1/2} - F_{i-1/2}) / dx` from `dU` over the interior cells, or,
with `overwrite = true`, store its negative there and ignore what `dU` held.

With `update = (a, U0, b, U, c)` the result is not stored in `dU` at all: it is
the last term of the Runge-Kutta stage `U = a U0 + b U + c dU`, which is formed
on the spot, with the same arithmetic as [`combine!`](simulation.md#combine-bang). That saves the
stage a pass over four whole arrays, and is how Quokka forms its stages too,
from the fluxes and not from a stored right-hand side. Only the last direction
of a right-hand side may do it, and only because the sweeps read `prim`, never
`U`.

The right-hand sides pass `overwrite = true` for the first active direction, in
place of zeroing `dU` first: one fewer pass over a whole array per right-hand
side. The result is the same to the bit, except that `0 - x` and `-x` differ in
the sign of an exact zero, which no comparison sees.

### `hydro_timestep` {#hydro_timestep}

Source: [`src/hydro_system.jl:456`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/hydro_system.jl#L456)

```julia
hydro_timestep(prim, eos, grid, cfl; bfield = nothing)
```

Largest stable step of the unsplit method-of-lines scheme,

    dt = cfl / max_cells Σ_d (|v_d| + c_s) / dx_d

summed over the active directions. Reduces to `cfl dx / (|v| + c_s)` in 1D.
With MHD (`bfield` a [`MagneticField`](mhd.md#MagneticField)) the sound speed is replaced by the
fast magnetosonic speed for propagation along each direction `d`, Quokka's
`FastMagnetoSonicSpeed` of the cell-centred field.

### `DEFAULT_TILE` {#DEFAULT_TILE}

Source: [`src/tiling.jl:23`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L23)

```julia
DEFAULT_TILE
```

The transverse block sizes `(b1, b2, b3)` of the tiled hydrodynamic sweep. A
sweep along `D` ignores `b_D` and takes the whole interior along `D`, so with
`(32, 1, 1)` the `x` sweep runs one row of cells at a time and the `y` and `z`
sweeps one 32-cell-wide strip of a plane. The scratch of a box, interface states
and fluxes, is `3 nvar (n_D + 3) b_d1 b_d2` numbers: 0.5 MB at 128^3 and 1 MB at
256^3, about a core's L2. Chosen by measurement on a 32-core node; see
`docs/parallel-findings.md`.

### `TILE_MIN_THREADS` {#TILE_MIN_THREADS}

Source: [`src/tiling.jl:36`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L36)

```julia
TILE_MIN_THREADS
```

Below this many threads, `Simulation(tile = :auto)` (the default) does not tile,
nor on a grid where a sweep would have fewer boxes than threads (a 2D run below
about 1024^2 at 32 threads).
Tiling pays by relieving the memory bus that many cores share, and costs 4-7% on
one core, so on this node it loses at 2 and 4 threads and wins from 8 on (5% at
8, 8% at 16, 30% at 32). The answer is the same either way. On Apple Silicon
`:auto` never tiles: on an M5 Max tiling was slower at every thread count, all
18 cores included.

### `TileArray` {#TileArray}

Source: [`src/tiling.jl:78`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L78)

```julia
TileArray(buf, dims, offset)
```

A box-local array indexed with *global* cell indices: `A[n, i, j, k]` is element
`(n, i - offset[1], j - offset[2], k - offset[3])` of the column-major array of
size `dims` stored at the start of `buf`. It is the counterpart of AMReX's
`Array4`, and lets the cell kernels write the interface states
and fluxes of one tile without a change. One buffer serves the sweeps in every
direction, each with its own `dims`.

Deliberately not an `AbstractArray`, like [`SVec`](utilities.md#SVec), because its axes
would not be `1:size`. Only scalar `getindex` and `setindex!` are defined, so
nothing else can be done with it by accident.

### `has_tiled_method` {#has_tiled_method}

Source: [`src/tiling.jl:114`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L114)

```julia
has_tiled_method(reconstruction) -> Bool
```

Whether the tiled sweep can run `reconstruction`. True for `PCM`, `PLM` and
`PPM`, which have cell kernels. A reconstruction defined outside the package
owns its whole sweep through `reconstruct!`, so it runs untiled.

### `tile_boxes` {#tile_boxes}

Source: [`src/tiling.jl:124`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L124)

```julia
tile_boxes(grid, D, tile) -> Vector{NTuple{3,UnitRange{Int}}}
```

The boxes of interior cells that the tiled sweep along `D` visits: the whole
interior along `D`, blocks of `tile[d]` cells along each other direction (the
last block may be shorter), together exactly the interior. They are ordered with
the first index fastest, so consecutive boxes, which run on the same task, are
neighbours in memory.

### `HydroTiles` {#HydroTiles}

Source: [`src/tiling.jl:147`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L147)

```julia
HydroTiles{T}
```

What the tiled sweep needs, built once with the simulation: the boxes and the
scratch shape of each direction, and one `(qL, qR, flux)` set of buffers per
task. A task's scratch is indexed by its chunk number, never by `threadid()`, so
no two tasks share one.

### `hydro_sweep!` {#hydro_sweep-bang}

Source: [`src/tiling.jl:171`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L171)

```julia
hydro_sweep!(dU, prim, scratch, reconstruction, eos, solver, Val(D), grid, nvar,
             nscalars, overwrite, update, backend, tiles)
```

The hydrodynamic sweep along `D`: reconstruct the interface states, solve the
Riemann problems and take the flux divergence into `dU` (overwriting it if
`overwrite`, or forming the Runge-Kutta stage `update`, as
[`flux_divergence!`](#flux_divergence-bang) does). It is the same three launches whatever runs
them, box by box ([`_sweep_box!`](#_sweep_box-bang)):

  - **tiled**, when `tiles` is a [`HydroTiles`](#HydroTiles), the reconstruction has
    cell kernels and the backend is [`CPUThreads`](simulation.md#CPUThreads): each task takes a run
    of [`tile_boxes`](#tile_boxes) and launches them serially into its own
    cache-sized scratch, a [`TileArray`](#TileArray);
  - otherwise as **one box**, the whole interior, with `scratch = (qL, qR, flux)`
    three whole-grid arrays and the launches spread over `backend`. This is
    how a GPU runs it, a CPU below `TILE_MIN_THREADS` threads, and Apple
    Silicon.

A reconstruction defined outside the package has no cell kernel, so it runs as
one box with its own `reconstruct!`.

### `_sweep_box!` {#_sweep_box-bang}

Source: [`src/tiling.jl:265`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/tiling.jl#L265)

```julia
_sweep_box!(backend, box, qL, qR, flux, dU, prim, reconstruction, eos, solver,
            Val(D), grid, gam, act1, act2, invdx, nvar, nscalars, Val(overwrite),
            update)
```

The sweep along `D` over the interior cells `box`, as three launches on
`backend`: reconstruction over the cells of `box` extended by one along `D` (as
[`cell_ranges`](simulation.md#cell_ranges) does for the whole grid), the Riemann solves over its
faces `lo:hi+1` along `D` (as [`face_ranges`](simulation.md#face_ranges)), and the divergence over
`box`. `qL`, `qR` and `flux` need only cover those cells and faces: a tile's
[`TileArray`](#TileArray), or three whole-grid arrays.

### `AbstractLimiter` {#AbstractLimiter}

Source: [`src/limiters.jl:6`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/limiters.jl#L6)

```julia
AbstractLimiter
```

Supertype for slope limiters. Implement `limit(lim, dL, dR)`, returning the
limited undivided slope across the cell.

### `limit` {#limit}

Source: [`src/limiters.jl:48`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/limiters.jl#L48)

```julia
limit(limiter, dL, dR)
```

Limited slope of a cell whose backward and forward differences are `dL`, `dR`.

### `AbstractReconstruction` {#AbstractReconstruction}

Source: [`src/reconstruction.jl:13`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L13)

```julia
AbstractReconstruction
```

Supertype for spatial reconstruction schemes: [`PCM`](#PCM), [`PLM`](#PLM),
[`PPM`](#PPM).

### `required_ghosts` {#required_ghosts}

Source: [`src/reconstruction.jl:53`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L53)

```julia
required_ghosts(method)
```

Minimum number of ghost cells needed by a reconstruction method. At least two
are always used so that the reconstruction sweep can write the interface states
of the first ghost cell without leaving the array.

### `reconstruct!` {#reconstruct-bang}

Source: [`src/reconstruction.jl:64`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L64)

```julia
reconstruct!(qL, qR, q, method, Val(D), grid, nvar)
```

Fill the interface states `qL`, `qR` along direction `D` from the cell-centred
primitive variables `q`. All three arrays are `(nvar, i, j, k)` and share the
grid layout; only the components `1:nvar` are touched.

### `ppm_edges` {#ppm_edges}

Source: [`src/reconstruction.jl:135`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/reconstruction.jl#L135)

```julia
ppm_edges(qmm, qm, qc, qp, qpp) -> (a_minus, a_plus)
```

PPM interface values on the low and high edge of the central cell, given the
five-point stencil of cell averages.

### `HLLD_DELTA` {#HLLD_DELTA}

Source: [`src/riemann/hlld.jl:9`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/hlld.jl#L9)

Quokka's `DELTA`: the relative size of `rho (S - u)(S - S_M) - bx^2` below which MK5's star state is degenerate.

### `mhd_flux` {#mhd_flux}

Source: [`src/riemann/state.jl:100`](https://github.com/chongchonghe/Honghu.jl/blob/e6bd095ecb38a1e27e4481cb8bbbcb062ea0ae9a/src/riemann/state.jl#L100)

```julia
mhd_flux(solver, sL, sR, gamma, bx, dw) -> (F::SVec{5}, am, ap)
```

The conservative flux across one interface in canonical form, for an
[`AbstractMHDRiemannSolver`](#AbstractMHDRiemannSolver), and the bounding speeds of the fan.

