Skip to content

The Sod shock tube

FVM sod_shock_tube_fvm.py

A tube holds dense, high-pressure gas on the left and thin gas on the right. The membrane between them bursts: a shock runs right, a rarefaction fan runs left, and a contact surface between the two gases follows the shock.

Density, velocity and pressure along the tube as stacked panels, animated from the initial jump at x = 0.5 to t = 0.2: a rarefaction fan moving left, a contact and a shock moving right, each computed curve in orange close to the thin dashed exact Riemann solution.

Density, velocity and pressure on 200 cells (Roe flux), with the exact Riemann solution dashed.

The problem

The Euler equations of an ideal gas, \(\gamma=1.4\):

\[\mathbf q_t + \mathbf F(\mathbf q)_x = 0,\qquad \mathbf q=\begin{pmatrix}\rho\\ \rho u\\ E\end{pmatrix},\quad \mathbf F=\begin{pmatrix}\rho u\\ \rho u^2+p\\ u(E+p)\end{pmatrix},\quad p=(\gamma-1)\left(E-\tfrac12\rho u^2\right).\]

The jNO code

The flux is a stacked vector of the three components, and nothing else is Euler-specific. Every numerical flux reads the Jacobian of the flux you write: Rusanov its spectral radius, Roe the matrix \(|A|=\sqrt{A^2+\delta^2 I}\).

q = d.unknown.vector(3)
qi = q.bind(x=x, t=t)
rho, mom, E = qi[0], qi[1], qi[2]
vel = mom / rho
p = (GAMMA - 1) * (E - 0.5 * mom * vel)
F = jno.np.stack([mom, mom * vel + p, vel * (E + p)], axis=-1)  # axis=-1: the components of every point

for flux in ("rusanov", "roe"):
    prob = jno.fvm([qi.t + F.d(x, scheme=jno.fv(flux=flux, limiter="minmod")), q(xi) - q0])
    traj = np.asarray(prob.solve(time=jno.solve.erk("ssprk3", cfl=0.5)))

Result

At \(h=0.005\), against the exact Riemann solution:

flux density \(L^1\) error
Rusanov \(5.7\times10^{-3}\)
Roe \(4.5\times10^{-3}\) (sharper contact)

The density stays in \([0.125,1]\) and the mass is constant to \(10^{-12}\).

Full script

The whole script, as it runs in the tests
"""The Sod shock tube: the Euler equations as the flux of a vector unknown.

    q = (rho, rho u, E),  q_t + F(q)_x = 0,  F = (rho u, rho u^2 + p, u (E + p)),  p = (gamma - 1)(E - rho u^2/2).

No Euler-specific code: the numerical fluxes read the Jacobian of the flux you write. The generic Roe flux
(|A| = sqrt(A^2 + delta^2 I)) resolves the contact sharper than Rusanov.
"""

import jax

jax.config.update("jax_enable_x64", True)

import numpy as np

import jno

run = jno.setup(__file__)

GAMMA, T = 1.4, 0.2
d = jno.Path(0.0, 0.0).line_to(1.0, 0.0).curve(size=0.005).domain(time=(0.0, T, 101))
x, t = d.variable("interior", split=True)[:2]
xi = d.variable("initial", split=True)[0]
q = d.unknown.vector(3)
qi = q.bind(x=x, t=t)
rho, mom, E = qi[0], qi[1], qi[2]
vel = mom / rho
p = (GAMMA - 1) * (E - 0.5 * mom * vel)
F = jno.np.stack([mom, mom * vel + p, vel * (E + p)], axis=-1)  # axis=-1: the components of every point
left = (1.0 - jno.np.tanh(1e4 * (xi - 0.5))) / 2
q0 = jno.np.stack([0.125 + 0.875 * left, 0.0 * xi, (0.1 + 0.9 * left) / (GAMMA - 1)], axis=-1)

trajs = {}
for flux in ("rusanov", "roe"):
    prob = jno.fvm([qi.t + F.d(x, scheme=jno.fv(flux=flux, limiter="minmod")), q(xi) - q0])
    trajs[flux] = traj = np.asarray(prob.solve(time=jno.solve.erk("ssprk3", cfl=0.5)))
    mass = traj[:, 0] @ prob.volumes
    print(
        f"{flux:8s} density in [{traj[-1, 0].min():.3f}, {traj[-1, 0].max():.3f}], mass drift {abs(mass[-1] - mass[0]):.1e}"
    )
    assert abs(mass[-1] - mass[0]) < 1e-12