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
- Model
- Real RAM
- Properties
- conservativedeterministicexplicitnot keeps stationary contact
- Evidence
- Proof sketchCode in 1 languages
- 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, , where 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 . 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 of a hyperbolic conservation law , compute a numerical flux at each face so that the conservative update is consistent and stable. The page uses two cases: a scalar law , and the Euler equations with , , , sound speed and . The cells are with width , and is a cell average.
The algorithm
RUSANOV-FLUX(U_L, U_R)- 1
s <- max(|u_L| + c_L, |u_R| + c_R) ▷ scalar law: s <- max(|f'(u_L)|, |f'(u_R)|) - 2
return (F(U_L) + F(U_R))/2 - (s/2)(U_R - U_L) FV-STEP(U, Δt, Δx)- 3
add one ghost cell on each side (copy for outflow, wrap for periodic) - 4
for each face j+1/2: F_{j+1/2} <- RUSANOV-FLUX(U_j, U_{j+1}) - 5
for each cell j: U_j <- U_j - (Δt/Δx)(F_{j+1/2} - F_{j-1/2})
The time step is with a Courant number . The code uses 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: and . The conserved states are and . The fluxes are and . The sound speeds are and , so . Line L2 gives
The exact Godunov flux at this face is (see the exact Riemann solver). The Rusanov flux moves too much mass and energy, which is the extra dissipation.
Sod's tube. On with the jump at , outflow boundaries and , the scheme runs to . The table gives the 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 | steps | |
|---|---|---|
| 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 , , on a periodic grid is an exact solution. At the density errors for are , , and , 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 on the left half and on the right half, with and everywhere. This is an exact steady solution. With 40 cells, after 8 steps () the velocity and the pressure are still exact to rounding, but the density in the 8 cells around the contact is . The term is a mass flux even though the gas does not move.
Correctness
The proof is in proofs/correctness.md. Theorem: the flux is consistent, , and the scheme is conservative, so the Lax-Wendroff theorem applies: if the scheme converges boundedly in , the limit is a weak solution. For a convex scalar law, the scheme is in Harten's incremental form with coefficients and , where is the slope of between the two states. These are nonnegative because , and their sum is . So the scheme is TVD for , and each new value is a convex combination of its three old neighbors for . For the Euler equations, positivity of density and pressure holds when bounds the true wave speeds and (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 on a uniform grid with width and steps with (line L5). The face flux is the Rusanov flux of lines L1 and L2,
where is the largest wave speed of the two states: for a scalar law, and 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). for every state .
Proof. With the dissipation term is zero, and the average of the two fluxes is .
Lemma 2 (conservation). On a periodic grid, for each conserved component.
Proof. Each face flux enters the update of cell with the factor and the update of cell with the factor . 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.
Lemma 3 (Harten's incremental form). Let a scheme be written as with , where the coefficients may depend on the data. If , and for all , then on a periodic grid.
Proof. Subtract the formula for cell from the formula for cell :
The three coefficients are nonnegative. Take absolute values, use the triangle inequality, and sum over . After a shift of the index in the second and the third sum, the right side is .
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 be convex and .
- If , the Rusanov scheme is TVD.
- If , then for every .
Proof. Let when , and otherwise. By the mean value theorem for some between and . Because is convex, is nondecreasing, so lies between and , and . Two ways to write the flux follow from :
Use the first form at the face and the second form at the face in line L5. The terms cancel, and
Both coefficients are nonnegative because , and . Lemma 3 gives part 1.
For part 2, write the same update as a weighted sum of three values:
The weights sum to 1, and the last two are nonnegative. Since , and , so . All weights are nonnegative, and is a convex combination of , and .
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, is at least the largest absolute wave speed of the exact Riemann solution between and , and that . If all have positive density and pressure, then so do all .
Proof. The Rusanov flux is the HLL flux with the speeds and . With these speeds, the HLL intermediate state equals the average of the exact Riemann solution over at time . 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 , the fans of neighboring faces do not meet, and is the average over cell of a function whose values are 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.
The estimate 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 and smooth data, the flux differs from by , so the local truncation error is and the scheme is first order accurate (LeVeque 2002, Section 8.2). The dissipation coefficient 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 operations and one square root per state, for conserved components. One step costs for cells, and the memory is . To reach a fixed time at a fixed Courant number, the scheme needs steps, so the total cost is . 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 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 , 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 . Roe and HLLC keep a contact at rest exactly.
- Positive under a condition. With true wave speed bounds and , density and pressure stay positive. On the 123 problem (, , , ) 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 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, 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 and from MUSCL, ENO or WENO. The dissipation is then 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 from or from alone can give smaller than 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 , the endpoint speeds do not bound between them. Take the maximum of over the interval between and .
- 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 , 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.
# 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 . 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 .
References
- 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
- 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)
- 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)
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag PDKD22 Edit this page History Report an error Markdown