Algorithm Search⌘K

Algorithmcore

Rusanov flux

Also called local Lax-Friedrichs flux.

A numerical flux that averages the two physical fluxes and adds dissipation scaled by the largest local wave speed. Simple, robust and first order.

Time
O(m M K) worst
Space
O(m M) extra
Where
K = the number of time steps (O(M) for a fixed end time and Courant number); M = the number of cells; m = the number of conserved components
Properties
conservativedeterministicexplicitnot keeps stationary contact
Origin
V. V. Rusanov, 1961

The Rusanov flux is the simplest numerical flux for a finite volume scheme. At each cell face it takes the average of the physical fluxes of the two neighbor states and subtracts a dissipation term, 12s(UR−UL)\frac12 s (U_R - U_L), where ss is the largest wave speed of the two states. It is also called the local Lax-Friedrichs flux, because it is the Lax-Friedrichs scheme with the dissipation set by the local speed instead of Δx/Δt\Delta x/\Delta t. It needs no Riemann problem solution and no eigenvectors, only one speed. The price is more dissipation than the Roe, HLL and HLLC fluxes: it smears contact discontinuities, and even a contact at rest is not kept.

In plain words

A finite volume scheme stores the average of the solution in each cell. Each step it moves stuff between neighbor cells through the faces. The amount that crosses a face is the flux. The left cell and the right cell each say what the flux is. The Rusanov flux takes the mean of the two answers. That mean alone gives wild wiggles, so it adds a correction that moves stuff from the larger value to the smaller value, like a small diffusion. The size of the correction is the fastest wave speed near the face. Fast waves get more smoothing, slow waves get less. This is enough to make the scheme stable, and it is very cheap.

The problem

Given cell averages UjnU_j^n of a hyperbolic conservation law Ut+F(U)x=0U_t + F(U)_x = 0, compute a numerical flux Fj+1/2=F(Uj,Uj+1)F_{j+1/2} = F(U_j, U_{j+1}) at each face so that the conservative update Ujn+1=Ujn−ΔtΔx(Fj+1/2−Fj−1/2)U_j^{n+1} = U_j^n - \frac{\Delta t}{\Delta x}(F_{j+1/2} - F_{j-1/2}) is consistent and stable. The page uses two cases: a scalar law ut+f(u)x=0u_t + f(u)_x = 0, and the Euler equations with U=(ρ,ρu,E)U = (\rho, \rho u, E), F(U)=(ρu,ρu2+p,u(E+p))F(U) = (\rho u, \rho u^2 + p, u(E + p)), E=p/(γ−1)+ρu2/2E = p/(\gamma - 1) + \rho u^2/2, sound speed c=γp/ρc = \sqrt{\gamma p/\rho} and γ=1.4\gamma = 1.4. The cells are Cj=[xj−1/2,xj+1/2]C_j = [x_{j-1/2}, x_{j+1/2}] with width Δx\Delta x, and UjnU_j^n is a cell average.

The algorithm

  1. RUSANOV-FLUX(U_L, U_R)
  2. 1s <- max(|u_L| + c_L, |u_R| + c_R) ▷ scalar law: s <- max(|f'(u_L)|, |f'(u_R)|)
  3. 2return (F(U_L) + F(U_R))/2 - (s/2)(U_R - U_L)
  4. FV-STEP(U, Δt, Δx)
  5. 3add one ghost cell on each side (copy for outflow, wrap for periodic)
  6. 4for each face j+1/2: F_{j+1/2} <- RUSANOV-FLUX(U_j, U_{j+1})
  7. 5for each cell j: U_j <- U_j - (Δt/Δx)(F_{j+1/2} - F_{j-1/2})

The time step is Δt=νΔx/max⁡j(∣uj∣+cj)\Delta t = \nu \Delta x/\max_j(|u_j| + c_j) with a Courant number ν≤1\nu \le 1. The code uses ν=0.9\nu = 0.9 and forward Euler in time. With MUSCL reconstruction and an SSP Runge-Kutta method, the same flux gives a second order scheme.

Example

The flux by hand. Take the two states of Sod's shock tube: WL=(ρ,u,p)=(1,0,1)W_L = (\rho, u, p) = (1, 0, 1) and WR=(0.125,0,0.1)W_R = (0.125, 0, 0.1). The conserved states are UL=(1,0,2.5)U_L = (1, 0, 2.5) and UR=(0.125,0,0.25)U_R = (0.125, 0, 0.25). The fluxes are F(UL)=(0,1,0)F(U_L) = (0, 1, 0) and F(UR)=(0,0.1,0)F(U_R) = (0, 0.1, 0). The sound speeds are cL=1.4=1.18322c_L = \sqrt{1.4} = 1.18322 and cR=1.12=1.05830c_R = \sqrt{1.12} = 1.05830, so s=1.18322s = 1.18322. Line L2 gives

F=(0−0.59161×(−0.875), 0.55−0, 0−0.59161×(−2.25))=(0.517657,0.55,1.331118).F = \big(0 - 0.59161 \times (-0.875),\ 0.55 - 0,\ 0 - 0.59161 \times (-2.25)\big) = (0.517657, 0.55, 1.331118).

The exact Godunov flux at this face is (0.395391,0.669837,1.154038)(0.395391, 0.669837, 1.154038) (see the exact Riemann solver). The Rusanov flux moves too much mass and energy, which is the extra dissipation.

Sod's tube. On [0,1][0, 1] with the jump at x=0.5x = 0.5, outflow boundaries and ν=0.9\nu = 0.9, the scheme runs to t=0.2t = 0.2. The table gives the L1L^1 error of the density against the exact cell averages (a 16-point midpoint rule in each cell). These numbers are measurements made with the code on this page.

cells MM steps ∥eρ∥1\lVert e_\rho \rVert_1
100 47 0.02148
200 96 0.01515
400 193 0.00998

The observed orders are 0.50 and 0.60. The solution has a shock and a contact, so the order is far below 1, as expected for a first order scheme near discontinuities.

A smooth wave. The density wave ρ=1+0.2sin⁡(2π(x−t))\rho = 1 + 0.2\sin(2\pi(x - t)), u=1u = 1, p=1p = 1 on a periodic grid is an exact solution. At t=0.1t = 0.1 the L1L^1 density errors for M=25,50,100,200M = 25, 50, 100, 200 are 0.0171810.017181, 0.0087840.008784, 0.0044590.004459 and 0.0022460.002246, with observed orders 0.97, 0.98 and 0.99. This is the combined order in space and time, at a fixed Courant number.

A contact at rest. Take ρ=1\rho = 1 on the left half and ρ=0.5\rho = 0.5 on the right half, with u=0u = 0 and p=1p = 1 everywhere. This is an exact steady solution. With 40 cells, after 8 steps (t=0.1t = 0.1) the velocity and the pressure are still exact to rounding, but the density in the 8 cells around the contact is 0.967,0.924,0.861,0.782,0.703,0.632,0.579,0.5400.967, 0.924, 0.861, 0.782, 0.703, 0.632, 0.579, 0.540. The term 12s(ρR−ρL)\frac12 s(\rho_R - \rho_L) is a mass flux even though the gas does not move.

Correctness

The proof is in proofs/correctness.md. Theorem: the flux is consistent, F(U,U)=F(U)F(U, U) = F(U), and the scheme is conservative, so the Lax-Wendroff theorem applies: if the scheme converges boundedly in Lloc1L^1_{loc}, the limit is a weak solution. For a convex scalar law, the scheme is in Harten's incremental form with coefficients Cj+1/2=λ2(s−a)j+1/2C_{j+1/2} = \frac{\lambda}{2}(s - a)_{j+1/2} and Dj+1/2=λ2(s+a)j+1/2D_{j+1/2} = \frac{\lambda}{2}(s + a)_{j+1/2}, where aa is the slope of ff between the two states. These are nonnegative because ∣a∣≤s|a| \le s, and their sum is λs\lambda s. So the scheme is TVD for λmax⁡s≤1\lambda \max s \le 1, and each new value is a convex combination of its three old neighbors for λmax⁡s≤1/2\lambda \max s \le 1/2. For the Euler equations, positivity of density and pressure holds when ss bounds the true wave speeds and λs≤1/2\lambda s \le 1/2 (Einfeldt, Munz, Roe and Sjögreen 1991). The simple speed estimate of line L1 is not a proven bound in every case.

Proof: The Rusanov scheme is conservative and consistent, and for a convex scalar law it is TVD and keeps a discrete maximum principlefull

Setting. The scheme stores cell averages UjnU_j^n on a uniform grid with width Δx\Delta x and steps Ujn+1=Ujn−λ(Fj+1/2−Fj−1/2)U_j^{n+1} = U_j^n - \lambda(F_{j+1/2} - F_{j-1/2}) with λ=Δt/Δx\lambda = \Delta t/\Delta x (line L5). The face flux is the Rusanov flux of lines L1 and L2,

Fj+1/2=12(f(Uj)+f(Uj+1))−12sj+1/2(Uj+1−Uj),F_{j+1/2} = \tfrac12\big(f(U_j) + f(U_{j+1})\big) - \tfrac12 s_{j+1/2}(U_{j+1} - U_j),

where sj+1/2s_{j+1/2} is the largest wave speed of the two states: max⁡(∣f′(Uj)∣,∣f′(Uj+1)∣)\max(|f'(U_j)|, |f'(U_{j+1})|) for a scalar law, and max⁡(∣u∣+c)\max(|u| + c) over the two states for the Euler equations. The grid is periodic, or it has one outflow ghost cell on each side, filled by copy, as in line L3.

Lemma 1 (consistency). F(U,U)=f(U)F(U, U) = f(U) for every state UU.

Proof. With UL=UR=UU_L = U_R = U the dissipation term 12s(UR−UL)\frac12 s(U_R - U_L) is zero, and the average of the two fluxes is f(U)f(U). □\square

Lemma 2 (conservation). On a periodic grid, ∑jUjn+1=∑jUjn\sum_j U_j^{n+1} = \sum_j U_j^n for each conserved component.

Proof. Each face flux Fj+1/2F_{j+1/2} enters the update of cell jj with the factor −λ-\lambda and the update of cell j+1j+1 with the factor +λ+\lambda. The sum over all cells telescopes to zero. In floating point arithmetic the sum is kept up to rounding. With rational data and Fraction arithmetic it is kept exactly, and the test checks this for Burgers' equation. □\square

Lemma 3 (Harten's incremental form). Let a scheme be written as Ujn+1=Uj+Cj+1/2Δj+1/2−Dj−1/2Δj−1/2U_j^{n+1} = U_j + C_{j+1/2}\Delta_{j+1/2} - D_{j-1/2}\Delta_{j-1/2} with Δj+1/2=Uj+1−Uj\Delta_{j+1/2} = U_{j+1} - U_j, where the coefficients may depend on the data. If Cj+1/2≥0C_{j+1/2} \ge 0, Dj+1/2≥0D_{j+1/2} \ge 0 and Cj+1/2+Dj+1/2≤1C_{j+1/2} + D_{j+1/2} \le 1 for all jj, then TV(Un+1)≤TV(Un)TV(U^{n+1}) \le TV(U^n) on a periodic grid.

Proof. Subtract the formula for cell jj from the formula for cell j+1j+1:

Δj+1/2n+1=(1−Cj+1/2−Dj+1/2)Δj+1/2+Cj+3/2Δj+3/2+Dj−1/2Δj−1/2.\Delta_{j+1/2}^{n+1} = (1 - C_{j+1/2} - D_{j+1/2})\Delta_{j+1/2} + C_{j+3/2}\Delta_{j+3/2} + D_{j-1/2}\Delta_{j-1/2}.

The three coefficients are nonnegative. Take absolute values, use the triangle inequality, and sum over jj. After a shift of the index in the second and the third sum, the right side is ∑j(1−Cj+1/2−Dj+1/2+Cj+1/2+Dj+1/2)∣Δj+1/2∣=TV(Un)\sum_j (1 - C_{j+1/2} - D_{j+1/2} + C_{j+1/2} + D_{j+1/2})|\Delta_{j+1/2}| = TV(U^n). □\square

This is the lemma of Harten (1983); LeVeque (2002, Section 12.12) uses it in the same way.

Theorem 1 (TVD and maximum principle for a convex scalar law). Let ff be convex and C1C^1.

  1. If λmax⁡jsj+1/2≤1\lambda \max_j s_{j+1/2} \le 1, the Rusanov scheme is TVD.
  2. If λmax⁡jsj+1/2≤1/2\lambda \max_j s_{j+1/2} \le 1/2, then min⁡(Uj−1,Uj,Uj+1)≤Ujn+1≤max⁡(Uj−1,Uj,Uj+1)\min(U_{j-1}, U_j, U_{j+1}) \le U_j^{n+1} \le \max(U_{j-1}, U_j, U_{j+1}) for every jj.

Proof. Let aj+1/2=(f(Uj+1)−f(Uj))/Δj+1/2a_{j+1/2} = (f(U_{j+1}) - f(U_j))/\Delta_{j+1/2} when Δj+1/2≠0\Delta_{j+1/2} \ne 0, and aj+1/2=f′(Uj)a_{j+1/2} = f'(U_j) otherwise. By the mean value theorem aj+1/2=f′(ξ)a_{j+1/2} = f'(\xi) for some ξ\xi between UjU_j and Uj+1U_{j+1}. Because ff is convex, f′f' is nondecreasing, so f′(ξ)f'(\xi) lies between f′(Uj)f'(U_j) and f′(Uj+1)f'(U_{j+1}), and ∣aj+1/2∣≤sj+1/2|a_{j+1/2}| \le s_{j+1/2}. Two ways to write the flux follow from f(Uj+1)=f(Uj)+aj+1/2Δj+1/2f(U_{j+1}) = f(U_j) + a_{j+1/2}\Delta_{j+1/2}:

Fj+1/2=f(Uj)+12(aj+1/2−sj+1/2)Δj+1/2=f(Uj+1)−12(aj+1/2+sj+1/2)Δj+1/2.F_{j+1/2} = f(U_j) + \tfrac12(a_{j+1/2} - s_{j+1/2})\Delta_{j+1/2} = f(U_{j+1}) - \tfrac12(a_{j+1/2} + s_{j+1/2})\Delta_{j+1/2}.

Use the first form at the face j+1/2j+1/2 and the second form at the face j−1/2j-1/2 in line L5. The terms f(Uj)f(U_j) cancel, and

Ujn+1=Uj+Cj+1/2Δj+1/2−Dj−1/2Δj−1/2,Cj+1/2=λ2(s−a)j+1/2,Dj+1/2=λ2(s+a)j+1/2.U_j^{n+1} = U_j + C_{j+1/2}\Delta_{j+1/2} - D_{j-1/2}\Delta_{j-1/2}, \qquad C_{j+1/2} = \tfrac{\lambda}{2}(s - a)_{j+1/2}, \quad D_{j+1/2} = \tfrac{\lambda}{2}(s + a)_{j+1/2}.

Both coefficients are nonnegative because ∣a∣≤s|a| \le s, and Cj+1/2+Dj+1/2=λsj+1/2≤1C_{j+1/2} + D_{j+1/2} = \lambda s_{j+1/2} \le 1. Lemma 3 gives part 1.

For part 2, write the same update as a weighted sum of three values:

Ujn+1=(1−Cj+1/2−Dj−1/2)Uj+Cj+1/2Uj+1+Dj−1/2Uj−1.U_j^{n+1} = (1 - C_{j+1/2} - D_{j-1/2})U_j + C_{j+1/2}U_{j+1} + D_{j-1/2}U_{j-1}.

The weights sum to 1, and the last two are nonnegative. Since ∣a∣≤s|a| \le s, Cj+1/2≤λsj+1/2C_{j+1/2} \le \lambda s_{j+1/2} and Dj−1/2≤λsj−1/2D_{j-1/2} \le \lambda s_{j-1/2}, so Cj+1/2+Dj−1/2≤2λmax⁡s≤1C_{j+1/2} + D_{j-1/2} \le 2\lambda \max s \le 1. All weights are nonnegative, and Ujn+1U_j^{n+1} is a convex combination of Uj−1U_{j-1}, UjU_j and Uj+1U_{j+1}. □\square

The test checks the global bounds on a Burgers step at the Courant number 0.9, above the bound 1/2 of part 2. That check is a measurement for this problem, not a proof for that Courant number.

Theorem 2 (positivity for the Euler equations, under a hypothesis). Suppose that at every face, sj+1/2s_{j+1/2} is at least the largest absolute wave speed of the exact Riemann solution between UjU_j and Uj+1U_{j+1}, and that λmax⁡s≤1/2\lambda \max s \le 1/2. If all UjnU_j^n have positive density and pressure, then so do all Ujn+1U_j^{n+1}.

Proof. The Rusanov flux is the HLL flux with the speeds SL=−sS_L = -s and SR=sS_R = s. With these speeds, the HLL intermediate state Uj+1/2∗=12(Uj+Uj+1)−12s(f(Uj+1)−f(Uj))U^*_{j+1/2} = \frac12(U_j + U_{j+1}) - \frac{1}{2s}(f(U_{j+1}) - f(U_j)) equals the average of the exact Riemann solution over [−sΔt,sΔt][-s\Delta t, s\Delta t] at time Δt\Delta t. This follows from the integral form of the conservation law over that rectangle, because no wave leaves the interval. The set of states with positive density and pressure is convex, so an average of such states is again such a state. When λs≤1/2\lambda s \le 1/2, the fans of neighboring faces do not meet, and Ujn+1U_j^{n+1} is the average over cell jj of a function whose values are UjU_j and the two intermediate states. So it is a convex combination of admissible states. This is the argument of Einfeldt, Munz, Roe and Sjögreen (1991) for HLL type solvers. □\square

The estimate s=max⁡(∣uL∣+cL,∣uR∣+cR)s = \max(|u_L| + c_L, |u_R| + c_R) of line L1 bounds the wave speeds in many cases, but not in all cases: in a strong collision a shock can be faster than both. So Theorem 2 is a conditional result for the code on this page, and the test on the 123 problem is a measurement.

Order. For smooth ff and smooth data, the flux differs from f(Uj)+12aΔj+1/2f(U_j) + \frac12 a\Delta_{j+1/2} by 12sΔj+1/2=O(Δx)\frac12 s\Delta_{j+1/2} = O(\Delta x), so the local truncation error is O(Δx)O(\Delta x) and the scheme is first order accurate (LeVeque 2002, Section 8.2). The dissipation coefficient ss does not vanish where the solution is smooth, so the order is exactly 1 in general, as the refinement study on the page measures.

Complexity

Theorem, in the real-RAM model: one flux costs O(m)O(m) operations and one square root per state, for mm conserved components. One step costs O(mM)O(mM) for MM cells, and the memory is O(mM)O(mM). To reach a fixed time TT at a fixed Courant number, the scheme needs O(M)O(M) steps, so the total cost is O(mM2)O(mM^2). The flux has no iteration and no eigenvector, so it is the cheapest flux on the pages of this group.

Properties

  • First order. The dissipation coefficient s/2s/2 does not go to zero where the solution is smooth. The measured order on the smooth wave is 0.97 to 0.99.
  • Monotone character for scalar laws. For a convex ff, the scheme is TVD under the stated Courant condition (Theorem above). It is an E-scheme (LeVeque 2002, Section 12.7), so its limits satisfy the entropy condition for scalar laws.
  • Robust. It needs only a bound on the wave speeds. It works for any hyperbolic system for which one can bound the eigenvalues of the Jacobian, even when the eigenvectors are unknown or hard to compute.
  • Smears contacts. A contact, at rest or moving, is smeared, because the dissipation acts on all fields with the same coefficient ss. Roe and HLLC keep a contact at rest exactly.
  • Positive under a condition. With true wave speed bounds and λs≤1/2\lambda s \le 1/2, density and pressure stay positive. On the 123 problem (WL=(1,−2,0.4)W_L = (1, -2, 0.4), WR=(1,2,0.4)W_R = (1, 2, 0.4), M=100M = 100, t=0.15t = 0.15) the smallest density is 0.0299 and the smallest pressure is 0.0081. The exact density at the center is 0.0219.

Variants and improvements

  • Global Lax-Friedrichs. Use one speed s=max⁡j(∣uj∣+cj)s = \max_j(|u_j| + c_j) for all faces. This is the flux splitting used by WENO finite difference codes. It is more dissipative.
  • HLL. Replace the one speed by two speeds, SL<0<SRS_L < 0 < S_R estimated from each side. The result is the HLL flux, which is less dissipative when the flow is fast in one direction.
  • Kurganov-Tadmor. The semi-discrete central scheme of Kurganov and Tadmor uses the Rusanov flux on reconstructed interface values. At first order the two schemes are the same.
  • High order. Use the flux on reconstructed values Uj+1/2−U_{j+1/2}^- and Uj+1/2+U_{j+1/2}^+ from MUSCL, ENO or WENO. The dissipation is then O(Δxk)O(\Delta x^{k}) times the jump, small where the solution is smooth.
  • Discontinuous Galerkin. The Rusanov flux is a common face flux of discontinuous Galerkin methods.

When to use it

Use the Rusanov flux to start a new code, for a system whose eigenvectors are hard to compute (magnetohydrodynamics, relativistic flows, many coupled equations), and as a robust fallback where a sharper flux fails. With a high order reconstruction the extra dissipation matters less. Do not use it at first order when contacts or shear layers must stay sharp; use HLLC or Roe there.

Pitfalls

  • Speed of one state only. Taking ss from ULU_L or from URU_R alone can give ss smaller than ∣f′∣|f'| on the other side. The scheme then loses its TVD bound and can oscillate.
  • A non-convex flux. For a scalar law with a non-convex ff, the endpoint speeds do not bound ∣f′∣|f'| between them. Take the maximum of ∣f′∣|f'| over the interval between uLu_L and uRu_R.
  • Time step from the old speeds. The Courant condition must use the largest speed over all cells, including the ghost cells. A step chosen from the interior only can be too large at a boundary.
  • Expecting a sharp contact. The smeared contact is a property of the flux, not a bug. Do not try to fix it with a smaller ss, which breaks stability.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

rusanov_flux.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/rusanov-flux/. The authors are listed in prov/PDKD22.jsonl.

"""Rusanov (local Lax-Friedrichs) flux for scalar laws and the 1D Euler equations.

F(U_L, U_R) = (F(U_L) + F(U_R)) / 2 - (s / 2) (U_R - U_L), with s the largest
local wave speed. The first order finite volume update uses outflow ghost cells
(one on each side, filled by copy) or a periodic grid.
"""

from math import sqrt

GAMMA = 1.4


def prim_to_cons(w, g=GAMMA):
    rho, u, p = w
    return (rho, rho * u, p / (g - 1.0) + 0.5 * rho * u * u)


def cons_to_prim(q, g=GAMMA):
    rho, m, e = q
    u = m / rho
    return (rho, u, (g - 1.0) * (e - 0.5 * m * u))


def euler_flux(q, g=GAMMA):
    rho, m, e = q
    u = m / rho
    p = (g - 1.0) * (e - 0.5 * m * u)
    return (m, m * u + p, u * (e + p))


def max_speed(q, g=GAMMA):
    rho, u, p = cons_to_prim(q, g)
    return abs(u) + sqrt(g * p / rho)


def rusanov_euler(ql, qr, g=GAMMA):
    """The Rusanov flux of the Euler equations between conserved states ql and qr."""
    s = max(max_speed(ql, g), max_speed(qr, g))  # @line L1
    fl, fr = euler_flux(ql, g), euler_flux(qr, g)
    return tuple(0.5 * (a + b) - 0.5 * s * (y - x) for a, b, x, y in zip(fl, fr, ql, qr))  # @line L2


def rusanov_scalar(ul, ur, f, df):
    """The Rusanov flux of u_t + f(u)_x = 0; df is f'. For convex f the endpoint speeds bound |f'|."""
    s = max(abs(df(ul)), abs(df(ur)))  # @line L1
    return 0.5 * (f(ul) + f(ur)) - 0.5 * s * (ur - ul)  # @line L2


def step(cells, dt, dx, flux, periodic=False):
    """One forward Euler step of the conservative scheme. cells is a list of states (tuples or floats)."""
    if periodic:  # @line L3
        ext = [cells[-1]] + list(cells) + [cells[0]]
    else:
        ext = [cells[0]] + list(cells) + [cells[-1]]
    faces = [flux(ext[j], ext[j + 1]) for j in range(len(ext) - 1)]  # @line L4
    lam = dt / dx
    if isinstance(cells[0], tuple):  # @line L5
        return [tuple(q - lam * (b - a) for q, a, b in zip(cells[j], faces[j], faces[j + 1])) for j in range(len(cells))]
    return [cells[j] - lam * (faces[j + 1] - faces[j]) for j in range(len(cells))]


def solve_euler(cells, dx, t_end, cfl=0.9, g=GAMMA, periodic=False):
    """March the Euler equations to t_end with dt = cfl dx / max(|u| + c). Returns (cells, number of steps)."""
    t, n = 0.0, 0
    flux = lambda a, b: rusanov_euler(a, b, g)
    while t < t_end - 1e-14:
        smax = max(max_speed(q, g) for q in cells)
        dt = min(cfl * dx / smax, t_end - t)
        cells = step(cells, dt, dx, flux, periodic)
        t += dt
        n += 1
    return cells, n
test_rusanov_flux.py 152 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/rusanov-flux/. The authors are listed in prov/PDKD22.jsonl.

import random
from fractions import Fraction
from math import cos, log2, pi, sqrt

from rusanov_flux import (
    cons_to_prim,
    euler_flux,
    prim_to_cons,
    rusanov_euler,
    rusanov_scalar,
    solve_euler,
    step,
)

SOD = ((1.0, 0.0, 1.0), (0.125, 0.0, 0.1))


def exact_sample(left, right, xi, g=1.4):
    """Reference: the exact Riemann solution (rho, u, p) at xi = x / t, with p* found by bisection."""
    (rl, ul, pl), (rr, ur, pr) = left, right
    cl, cr = sqrt(g * pl / rl), sqrt(g * pr / rr)
    z = (g - 1.0) / (2.0 * g)

    def fk(p, rk, pk, ck):
        if p > pk:
            return (p - pk) * sqrt(2.0 / ((g + 1.0) * rk) / (p + (g - 1.0) / (g + 1.0) * pk))
        return 2.0 * ck / (g - 1.0) * ((p / pk) ** z - 1.0)

    lo, hi = 1e-12, 1e4
    for _ in range(200):
        mid = 0.5 * (lo + hi)
        if fk(mid, rl, pl, cl) + fk(mid, rr, pr, cr) + ur - ul > 0.0:
            hi = mid
        else:
            lo = mid
    ps = 0.5 * (lo + hi)
    us = 0.5 * (ul + ur) + 0.5 * (fk(ps, rr, pr, cr) - fk(ps, rl, pl, cl))
    rk, uk, pk, ck, sg = (rl, ul, pl, cl, 1.0) if xi <= us else (rr, ur, pr, cr, -1.0)
    xs, uss, uks = sg * xi, sg * us, sg * uk
    if ps > pk:
        if xs <= uks - ck * sqrt((g + 1.0) / (2.0 * g) * ps / pk + z):
            return (rk, uk, pk)
        r, gg = ps / pk, (g - 1.0) / (g + 1.0)
        return (rk * (r + gg) / (gg * r + 1.0), us, ps)
    if xs <= uks - ck:
        return (rk, uk, pk)
    if xs >= uss - ck * (ps / pk) ** z:
        return (rk * (ps / pk) ** (1.0 / g), us, ps)
    w = 2.0 / (g + 1.0) + (g - 1.0) / ((g + 1.0) * ck) * (uks - xs)
    return (rk * w ** (2.0 / (g - 1.0)), sg * 2.0 / (g + 1.0) * (ck + 0.5 * (g - 1.0) * uks + xs), pk * w ** (2.0 * g / (g - 1.0)))


def sod_l1(m, t=0.2):
    """L1 error of the density against exact cell averages (16-point midpoint rule per cell)."""
    cells = [prim_to_cons(SOD[0] if (j + 0.5) / m < 0.5 else SOD[1]) for j in range(m)]
    cells, _ = solve_euler(cells, 1.0 / m, t)
    err = 0.0
    for j, q in enumerate(cells):
        ex = sum(exact_sample(*SOD, ((j + (k + 0.5) / 16) / m - 0.5) / t)[0] for k in range(16)) / 16
        err += abs(q[0] - ex) / m
    return err


def density_wave(m, t):
    """Exact cell averages of rho = 1 + 0.2 sin(2 pi (x - t)), with u = 1 and p = 1."""
    dx = 1.0 / m
    return [1.0 + 0.2 * (cos(2 * pi * (j * dx - t)) - cos(2 * pi * ((j + 1) * dx - t))) / (2 * pi * dx) for j in range(m)]


def test_flux_at_the_sod_interface_by_hand() -> None:
    f = rusanov_euler(prim_to_cons(SOD[0]), prim_to_cons(SOD[1]))
    s = sqrt(1.4)  # c_L is the larger speed
    assert abs(f[0] - 0.5 * s * 0.875) < 1e-14
    assert abs(f[1] - 0.55) < 1e-14
    assert abs(f[2] - 0.5 * s * 2.25) < 1e-14
    assert abs(f[0] - 0.517657) < 1e-6 and abs(f[2] - 1.331118) < 1e-6


def test_consistency() -> None:
    rng = random.Random(1)
    for _ in range(50):
        q = prim_to_cons((rng.uniform(0.1, 2), rng.uniform(-2, 2), rng.uniform(0.1, 2)))
        assert all(abs(a - b) < 1e-12 for a, b in zip(rusanov_euler(q, q), euler_flux(q)))


def test_sod_l1_error_decreases() -> None:
    e100, e200 = sod_l1(100), sod_l1(200)
    assert e200 < 0.0155 and e100 < 0.022
    assert 0.3 < log2(e100 / e200) < 0.8  # about 1/2 near discontinuities


def test_first_order_on_a_smooth_wave() -> None:
    t, errs = 0.1, []
    for m in (25, 50, 100):
        cells = [prim_to_cons((r, 1.0, 1.0)) for r in density_wave(m, 0.0)]
        cells, _ = solve_euler(cells, 1.0 / m, t, periodic=True)
        errs.append(sum(abs(q[0] - e) for q, e in zip(cells, density_wave(m, t))) / m)
    orders = [log2(errs[i] / errs[i + 1]) for i in range(2)]
    assert all(abs(p - 1.0) < 0.2 for p in orders)


def test_conservation_on_a_periodic_grid() -> None:
    m = 64
    cells = [prim_to_cons((r, 1.0, 1.0)) for r in density_wave(m, 0.0)]
    before = [sum(q[k] for q in cells) for k in range(3)]
    cells, _ = solve_euler(cells, 1.0 / m, 0.05, periodic=True)
    after = [sum(q[k] for q in cells) for k in range(3)]
    assert all(abs(a - b) <= 1e-12 * abs(a) for a, b in zip(before, after))


def test_exact_conservation_for_burgers_in_fractions() -> None:
    f, df = (lambda u: u * u / 2), (lambda u: u)
    rng = random.Random(2)
    u = [Fraction(rng.randint(-8, 8), 4) for _ in range(12)]
    total = sum(u)
    for _ in range(5):
        u = step(u, Fraction(1, 10), Fraction(1, 2), lambda a, b: rusanov_scalar(a, b, f, df), periodic=True)
        assert sum(u) == total


def test_tvd_and_no_new_extrema_for_burgers() -> None:
    f, df = (lambda u: u * u / 2), (lambda u: u)
    m, dx = 80, 1.0 / 80
    u = [1.0 if 0.2 < (j + 0.5) * dx < 0.5 else -0.5 for j in range(m)]
    tv0 = sum(abs(u[j + 1] - u[j]) for j in range(m - 1))
    for _ in range(60):
        dt = 0.9 * dx / max(abs(v) for v in u)
        u = step(u, dt, dx, lambda a, b: rusanov_scalar(a, b, f, df))
        tv = sum(abs(u[j + 1] - u[j]) for j in range(m - 1))
        assert tv <= tv0 + 1e-12
        assert -0.5 - 1e-12 <= min(u) and max(u) <= 1.0 + 1e-12
        tv0 = tv


def test_stationary_contact_is_smeared_but_u_and_p_stay_exact() -> None:
    m = 40
    cells = [prim_to_cons((1.0 if j < m // 2 else 0.5, 0.0, 1.0)) for j in range(m)]
    cells, _ = solve_euler(cells, 1.0 / m, 0.1)
    w = [cons_to_prim(q) for q in cells]
    assert all(abs(x[1]) < 1e-13 and abs(x[2] - 1.0) < 1e-13 for x in w)
    assert abs(w[m // 2][0] - 0.5) > 0.05  # the contact is not kept


def test_positivity_on_the_123_problem() -> None:
    m = 100
    cells = [prim_to_cons((1.0, -2.0, 0.4) if j < m // 2 else (1.0, 2.0, 0.4)) for j in range(m)]
    cells, _ = solve_euler(cells, 1.0 / m, 0.15)
    w = [cons_to_prim(q) for q in cells]
    assert min(x[0] for x in w) > 0.0 and min(x[2] for x in w) > 0.0

History

Rusanov (1961) published a scheme for the interaction of shock waves with obstacles in which the artificial dissipation was proportional to the local value of ∣u∣+c|u| + c. Lax (1954) had used the global form, now called the Lax-Friedrichs scheme. The flux with the maximum of the speeds of the two neighbor states is the form in modern books (LeVeque 2002, Section 12.5; Toro 2009, Chapter 10), where it appears as the local Lax-Friedrichs flux and as the HLL flux with the single speed ss.

References

  1. V. V. Rusanov, The calculation of the interaction of non-stationary shock waves with obstacles, Zhurnal Vychislitel'noi Matematiki i Matematicheskoi Fiziki 1(2) (1961) 267-279; English translation in USSR Computational Mathematics and Mathematical Physics 1(2) (1962) 304-320, the scheme with dissipation scaled by the local largest wave speed; the page relies on the English translation; doi:10.1016/0041-5553(62)90062-9
  2. Randall J. LeVeque, Finite Volume Methods for Hyperbolic Problems, Cambridge University Press, 2002, Section 12.4 (numerical viscosity), Section 12.5 (the Lax-Friedrichs and local Lax-Friedrichs methods), Section 12.7 (E-schemes), Section 12.12 (nonlinear stability)
  3. Eleuterio F. Toro, Riemann Solvers and Numerical Methods for Fluid Dynamics, third edition, Springer, 2009, Chapter 10 (the HLL solver; the Rusanov flux is the HLL flux with one speed)
  4. Ami Harten, High resolution schemes for hyperbolic conservation laws, Journal of Computational Physics 49 (1983) 357-393, the incremental form and the lemma on TVD coefficients; doi:10.1016/0021-9991(83)90136-5
  5. Bernd Einfeldt, Claus-Dieter Munz, Philip L. Roe and Björn Sjögreen, On Godunov-type methods near low densities, Journal of Computational Physics 92 (1991) 273-295, positivity of HLL type fluxes when the speeds bound the waves; doi:10.1016/0021-9991(91)90211-3

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example