Algorithm Search⌘K

Algorithmcore

Roe solver

Also called Roe approximate Riemann solver, Roe linearization.

Approximates the Riemann problem by a linear one with a Roe-averaged Jacobian that resolves single shocks exactly.

Time
O(m^2 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
conservativedeterministicnot entropy satisfyingexact single discontinuityexplicitkeeps stationary contactnot positive
Origin
Philip L. Roe, 1981

The Roe solver replaces the nonlinear Riemann problem at a cell face by a linear one, Ut+A^Ux=0U_t + \hat A U_x = 0, with a constant matrix A^\hat A built from the two neighbor states. Roe (1981) chose A^\hat A so that F(UR)−F(UL)=A^(UR−UL)F(U_R) - F(U_L) = \hat A(U_R - U_L) holds exactly. This "property U" makes the method conservative and makes it resolve any single shock or contact exactly. For the Euler equations, A^\hat A is the Jacobian at a special average of the two states, the Roe average, weighted by ρ\sqrt\rho. The flux needs the eigenvalues and eigenvectors of A^\hat A but no iteration. It is one of the sharpest first order fluxes. It has two known flaws: it accepts expansion shocks, so it needs an entropy fix, and it can give negative pressure near vacuum.

In plain words

The exact Riemann problem at a face is nonlinear and needs an iteration. Roe asks a simpler question: which linear problem gives the right answer? A linear problem is a set of waves that each move at one fixed speed. Roe found an average state such that the linear waves at that state add up exactly to the jump between the two cells, and their fluxes add up exactly to the jump in the flux. Then each wave is upwinded by the sign of its own speed. A single shock, or a single contact, is one wave of the linear problem, so it comes out exactly right.

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 face flux Fj+1/2F_{j+1/2} from UL=UjU_L = U_j and UR=Uj+1U_R = U_{j+1} for the 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}). The flux should be exact for a single discontinuity. The page uses the Euler equations with U=(ρ,ρu,E)U = (\rho, \rho u, E), F=(ρu,ρu2+p,ρuH)F = (\rho u, \rho u^2 + p, \rho u H), H=(E+p)/ρH = (E + p)/\rho, c2=γp/ρc^2 = \gamma p/\rho and γ=1.4\gamma = 1.4, and a scalar law.

The algorithm

  1. ROE-FLUX(U_L, U_R)
  2. 1ũ <- (√ρ_L u_L + √ρ_R u_R)/(√ρ_L + √ρ_R); H̃ <- (√ρ_L H_L + √ρ_R H_R)/(√ρ_L + √ρ_R); c̃ <- √((γ-1)(H̃ - ũ²/2))
  3. 2λ <- (ũ - c̃, ũ, ũ + c̃); r_1 <- (1, ũ - c̃, H̃ - ũc̃); r_2 <- (1, ũ, ũ²/2); r_3 <- (1, ũ + c̃, H̃ + ũc̃)
  4. 3Δ <- U_R - U_L; α_2 <- (γ-1)/c̃² (Δ_0 (H̃ - ũ²) + ũ Δ_1 - Δ_2); α_1 <- (Δ_0 (ũ + c̃) - Δ_1 - c̃ α_2)/(2c̃); α_3 <- Δ_0 - α_1 - α_2
  5. 4return (F(U_L) + F(U_R))/2 - (1/2) Σ_k |λ_k| α_k r_k
  6. FV-STEP(U, Δt, Δx)
  7. 5add one ghost cell on each side (copy for outflow, wrap for periodic)
  8. 6for each face j+1/2: F_{j+1/2} <- ROE-FLUX(U_j, U_{j+1})
  9. 7for each cell j: U_j <- U_j - (Δt/Δx)(F_{j+1/2} - F_{j-1/2})

For a scalar law the code uses the same lines with one wave: a=(f(uR)−f(uL))/(uR−uL)a = (f(u_R) - f(u_L))/(u_R - u_L) (or f′(uL)f'(u_L) if the states are equal) and F=12(fL+fR)−12∣a∣(uR−uL)F = \frac12(f_L + f_R) - \frac12|a|(u_R - u_L). This is the Murman-Roe scheme. The time step is Δt=0.9 Δx/max⁡j(∣uj∣+cj)\Delta t = 0.9\,\Delta x/\max_j(|u_j| + c_j) with forward Euler. The code has no entropy fix. The Harten-Hyman entropy fix page adds one.

Example

The flux by hand. Take Sod's states 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), so Δ=(−0.875,0,−2.25)\Delta = (-0.875, 0, -2.25). Line L1 gives u~=0\tilde u = 0, H~=3.31716\tilde H = 3.31716 and c~=1.15190\tilde c = 1.15190. Line L3 gives

α2=0.41.32686(−0.875×3.31716+2.25)=−0.196708,α1=α3=−0.875+0.1967082=−0.339146.\alpha_2 = \frac{0.4}{1.32686}(-0.875 \times 3.31716 + 2.25) = -0.196708, \qquad \alpha_1 = \alpha_3 = \frac{-0.875 + 0.196708}{2} = -0.339146.

The waves 11 and 33 have ∣λ∣=c~|\lambda| = \tilde c, the wave 22 has λ=0\lambda = 0. Line L4 gives the mass flux −c~ α1=0.390660-\tilde c\,\alpha_1 = 0.390660, the momentum flux 0.550.55 (the two acoustic terms cancel), and the energy flux −c~ α1H~=1.295882-\tilde c\,\alpha_1\tilde H = 1.295882. The exact Godunov flux is (0.395391,0.669837,1.154038)(0.395391, 0.669837, 1.154038). The Roe mass flux is closer to it than the HLL value 0.5107140.510714.

Sod's tube. On [0,1][0, 1] with outflow boundaries and Courant number 0.9 at t=0.2t = 0.2, the L1L^1 density errors against exact cell averages, measured with the code on this page, are 0.01319 (M=100M = 100, 48 steps), 0.00886 (M=200M = 200, 97 steps) and 0.00565 (M=400M = 400, 194 steps). The observed orders are 0.57 and 0.65. On the same grids HLLC gives 0.01387, 0.00919 and 0.00581. The rarefaction of Sod's problem is not transonic: its head moves at −1.18322-1.18322 and its tail at u∗−cL∗=−0.0703u^* - c^*_L = -0.0703. So the missing entropy fix does no harm here.

A smooth wave. For ρ=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, the L1L^1 density errors at t=0.1t = 0.1 for M=25,50,100,200M = 25, 50, 100, 200 are 0.0061430.006143, 0.0030560.003056, 0.0015360.001536 and 0.0007700.000770, with orders 1.01, 0.99 and 1.00. These are the HLLC numbers to all digits shown: with constant uu and pp the data are pure contact waves, and both solvers give the upwind flux for them.

Three discontinuities at rest. Take a Mach 2 shock at rest: WL=(1,21.4,1)W_L = (1, 2\sqrt{1.4}, 1) and WR=(8/3,0.88741,4.5)W_R = (8/3, 0.88741, 4.5). It satisfies the Rankine-Hugoniot condition with speed 0. On 40 cells, after 30 steps, the solution is unchanged to 10−1210^{-12}. A contact at rest is also kept. Now swap the two states. The result is a jump from subsonic to supersonic flow at rest, an expansion shock, which violates the entropy condition. The Roe scheme keeps it too, unchanged. For Burgers' equation the jump from u=−1u = -1 to u=1u = 1 gives a=0a = 0, the flux 12=f(±1)\frac12 = f(\pm 1) on every face, and the same steady expansion shock.

Negative pressure. The 123 problem has WL=(1,−2,0.4)W_L = (1, -2, 0.4) and WR=(1,2,0.4)W_R = (1, 2, 0.4). At the center face u~=0\tilde u = 0, H~=3.4\tilde H = 3.4 and c~=1.16619\tilde c = 1.16619, and α1=−α3=−1.71499\alpha_1 = -\alpha_3 = -1.71499. The Roe flux is (0,2.06762,0)(0, 2.06762, 0): no mass and no energy leaves the center, but the momentum flux is much less than the true 4.44.4. On 100 cells with the Courant number 0.9, after one step the two center cells have ρ=0.3451\rho = 0.3451 and p=−0.5765p = -0.5765. HLL and HLLC run the same problem with positive pressure.

Correctness

The proof is in proofs/correctness.md. Theorem: (1) c~2>0\tilde c^2 > 0 for any two physical states. (2) Property U: with the parameter vector Z=ρ(1,u,H)Z = \sqrt\rho(1, u, H), both UU and FF are quadratic in ZZ, so FR−FL=A^(UR−UL)F_R - F_L = \hat A(U_R - U_L) exactly, where A^\hat A is the Jacobian at (u~,H~)(\tilde u, \tilde H). (3) The rkr_k are the eigenvectors of A^\hat A and ∑kαkrk=UR−UL\sum_k\alpha_k r_k = U_R - U_L. (4) The Roe flux is the Godunov flux of the linear problem Vt+A^Vx=0V_t + \hat A V_x = 0. (5) The flux is consistent and the scheme is conservative. (6) If ULU_L and URU_R are joined by a single discontinuity of speed ss, the flux is FLF_L for s≥0s \ge 0 and FRF_R for s<0s < 0, so a single shock or contact is exact, and one at rest is a steady solution. (7) For a scalar law, the Murman-Roe scheme is TVD for λmax⁡∣a∣≤1\lambda\max|a| \le 1. The scheme does not satisfy the entropy condition and is not positive, as the examples show.

Proof: The Roe matrix has property U, the Roe flux is the Godunov flux of the linearized problem, and a single discontinuity is resolved exactlyfull

Setting. The system is the 1D Euler equations Ut+F(U)x=0U_t + F(U)_x = 0 for an ideal gas with U=(ρ,ρu,E)U = (\rho, \rho u, E), F=(ρu,ρu2+p,ρuH)F = (\rho u, \rho u^2 + p, \rho u H), enthalpy H=(E+p)/ρH = (E + p)/\rho and p=(γ−1)(E−ρu2/2)p = (\gamma - 1)(E - \rho u^2/2), so that c2=γp/ρ=(γ−1)(H−u2/2)c^2 = \gamma p/\rho = (\gamma - 1)(H - u^2/2). At a face with states ULU_L, URU_R, line L1 computes the Roe averages with wK=ρKw_K = \sqrt{\rho_K}:

u~=wLuL+wRuRwL+wR,H~=wLHL+wRHRwL+wR,c~2=(γ−1)(H~−u~2/2).\tilde u = \frac{w_L u_L + w_R u_R}{w_L + w_R}, \qquad \tilde H = \frac{w_L H_L + w_R H_R}{w_L + w_R}, \qquad \tilde c^2 = (\gamma - 1)(\tilde H - \tilde u^2/2).

Line L2 takes λ1=u~−c~\lambda_1 = \tilde u - \tilde c, λ2=u~\lambda_2 = \tilde u, λ3=u~+c~\lambda_3 = \tilde u + \tilde c and

r1=(1,u~−c~,H~−u~c~),r2=(1,u~,u~2/2),r3=(1,u~+c~,H~+u~c~).r_1 = (1, \tilde u - \tilde c, \tilde H - \tilde u\tilde c), \quad r_2 = (1, \tilde u, \tilde u^2/2), \quad r_3 = (1, \tilde u + \tilde c, \tilde H + \tilde u\tilde c).

Line L3 computes, with Δ=UR−UL=(Δ0,Δ1,Δ2)\Delta = U_R - U_L = (\Delta_0, \Delta_1, \Delta_2),

α2=γ−1c~2(Δ0(H~−u~2)+u~Δ1−Δ2),α1=Δ0(u~+c~)−Δ1−c~α22c~,α3=Δ0−α1−α2,\alpha_2 = \frac{\gamma - 1}{\tilde c^2}\big(\Delta_0(\tilde H - \tilde u^2) + \tilde u\Delta_1 - \Delta_2\big), \quad \alpha_1 = \frac{\Delta_0(\tilde u + \tilde c) - \Delta_1 - \tilde c\alpha_2}{2\tilde c}, \quad \alpha_3 = \Delta_0 - \alpha_1 - \alpha_2,

and line L4 returns Froe=12(FL+FR)−12∑k∣λk∣αkrkF^{roe} = \frac12(F_L + F_R) - \frac12\sum_k |\lambda_k|\alpha_k r_k.

Lemma 1 (c~2>0\tilde c^2 > 0). For any two states with positive density and pressure, H~−u~2/2>0\tilde H - \tilde u^2/2 > 0.

Proof. Let θ=wL/(wL+wR)∈(0,1)\theta = w_L/(w_L + w_R) \in (0, 1). Since HK=cK2/(γ−1)+uK2/2H_K = c_K^2/(\gamma - 1) + u_K^2/2,

H~−u~22=θcL2+(1−θ)cR2γ−1+12(θuL2+(1−θ)uR2−(θuL+(1−θ)uR)2)=θcL2+(1−θ)cR2γ−1+θ(1−θ)2(uL−uR)2,\tilde H - \frac{\tilde u^2}{2} = \frac{\theta c_L^2 + (1 - \theta)c_R^2}{\gamma - 1} + \frac12\big(\theta u_L^2 + (1 - \theta)u_R^2 - (\theta u_L + (1 - \theta)u_R)^2\big) = \frac{\theta c_L^2 + (1 - \theta)c_R^2}{\gamma - 1} + \frac{\theta(1 - \theta)}{2}(u_L - u_R)^2,

which is positive. □\square

Lemma 2 (property U). Let U^\hat U be the state with velocity u~\tilde u and enthalpy H~\tilde H (its density does not matter), and let A^\hat A be the Jacobian F′(U)F'(U) written as a function of (u,H)(u, H) and evaluated at (u~,H~)(\tilde u, \tilde H). Then F(UR)−F(UL)=A^ (UR−UL)F(U_R) - F(U_L) = \hat A\,(U_R - U_L).

Proof. Use Roe's parameter vector Z=ρ (1,u,H)=(z1,z2,z3)Z = \sqrt\rho\,(1, u, H) = (z_1, z_2, z_3). A direct computation gives

U=(z12, z1z2, z1z3γ+γ−12γz22),F=(z1z2, γ−1γz1z3+γ+12γz22, z2z3),U = \Big(z_1^2,\ z_1 z_2,\ \frac{z_1 z_3}{\gamma} + \frac{\gamma - 1}{2\gamma}z_2^2\Big), \qquad F = \Big(z_1 z_2,\ \frac{\gamma - 1}{\gamma}z_1 z_3 + \frac{\gamma + 1}{2\gamma}z_2^2,\ z_2 z_3\Big),

using p=γ−1γ(ρH−ρu2/2)p = \frac{\gamma - 1}{\gamma}(\rho H - \rho u^2/2). Every component of UU and of FF is a quadratic form in ZZ. For a quadratic form q(Z)=ZTQZq(Z) = Z^T Q Z with QQ symmetric, q(ZR)−q(ZL)=(ZR+ZL)TQ(ZR−ZL)=q′(Zˉ)(ZR−ZL)q(Z_R) - q(Z_L) = (Z_R + Z_L)^T Q (Z_R - Z_L) = q'(\bar Z)(Z_R - Z_L) with Zˉ=12(ZL+ZR)\bar Z = \frac12(Z_L + Z_R), exactly. So

UR−UL=B(Zˉ)(ZR−ZL),FR−FL=C(Zˉ)(ZR−ZL),U_R - U_L = B(\bar Z)(Z_R - Z_L), \qquad F_R - F_L = C(\bar Z)(Z_R - Z_L),

where B=∂U/∂ZB = \partial U/\partial Z and C=∂F/∂ZC = \partial F/\partial Z are linear in ZZ. BB is lower triangular with diagonal (2z1,z1,z1/γ)(2z_1, z_1, z_1/\gamma), so det⁡B=2z13/γ>0\det B = 2z_1^3/\gamma > 0 for z1>0z_1 > 0. Hence FR−FL=C(Zˉ)B(Zˉ)−1(UR−UL)F_R - F_L = C(\bar Z)B(\bar Z)^{-1}(U_R - U_L). By the chain rule F′(U)=C(Z)B(Z)−1F'(U) = C(Z)B(Z)^{-1} at every state, so C(Zˉ)B(Zˉ)−1C(\bar Z)B(\bar Z)^{-1} is the Jacobian at the state whose parameter vector is Zˉ\bar Z. That state has u=zˉ2/zˉ1=u~u = \bar z_2/\bar z_1 = \tilde u and H=zˉ3/zˉ1=H~H = \bar z_3/\bar z_1 = \tilde H. The Jacobian of the Euler equations depends on the state only through uu and HH, so it is A^\hat A. □\square

Lemma 3 (eigenstructure and wave strengths). A^rk=λkrk\hat A r_k = \lambda_k r_k for k=1,2,3k = 1, 2, 3, and ∑kαkrk=UR−UL\sum_k \alpha_k r_k = U_R - U_L.

Proof. In terms of uu and HH the Euler Jacobian is

A=(010γ−32u2(3−γ)uγ−1u(γ−12u2−H)H−(γ−1)u2γu).A = \begin{pmatrix} 0 & 1 & 0\\ \frac{\gamma - 3}{2}u^2 & (3 - \gamma)u & \gamma - 1\\ u\big(\frac{\gamma - 1}{2}u^2 - H\big) & H - (\gamma - 1)u^2 & \gamma u\end{pmatrix}.

For r2=(1,u,u2/2)r_2 = (1, u, u^2/2): the rows give uu, u2(γ−32+(3−γ)+γ−12)=u2u^2\big(\frac{\gamma - 3}{2} + (3 - \gamma) + \frac{\gamma - 1}{2}\big) = u^2 and u3(γ−12−(γ−1)+γ2)=u3/2u^3\big(\frac{\gamma - 1}{2} - (\gamma - 1) + \frac{\gamma}{2}\big) = u^3/2, which is u r2u\,r_2. For r1,3=(1,u±c,H±uc)r_{1,3} = (1, u \pm c, H \pm uc): the first row gives u±cu \pm c. The second row gives 3−γ2u2+(γ−1)H±2uc\frac{3 - \gamma}{2}u^2 + (\gamma - 1)H \pm 2uc, and with (γ−1)H=c2+γ−12u2(\gamma - 1)H = c^2 + \frac{\gamma - 1}{2}u^2 this is (u±c)2(u \pm c)^2. The third row minus (u±c)(H±uc)(u \pm c)(H \pm uc) is u((γ−1)(H−u2/2)−c2)=0u\big((\gamma - 1)(H - u^2/2) - c^2\big) = 0. So Ar=λrA r = \lambda r at every (u,H)(u, H) with c2=(γ−1)(H−u2/2)c^2 = (\gamma - 1)(H - u^2/2), in particular at (u~,H~,c~)(\tilde u, \tilde H, \tilde c).

For the strengths, compare components of ∑kαkrk\sum_k \alpha_k r_k with Δ\Delta. The first component is α1+α2+α3=Δ0\alpha_1 + \alpha_2 + \alpha_3 = \Delta_0 by the definition of α3\alpha_3. The second is u~Δ0+c~(α3−α1)\tilde u\Delta_0 + \tilde c(\alpha_3 - \alpha_1), so we need α3−α1=(Δ1−u~Δ0)/c~\alpha_3 - \alpha_1 = (\Delta_1 - \tilde u\Delta_0)/\tilde c. The third is H~(α1+α3)+u~c~(α3−α1)+u~22α2\tilde H(\alpha_1 + \alpha_3) + \tilde u\tilde c(\alpha_3 - \alpha_1) + \frac{\tilde u^2}{2}\alpha_2. Put in α1+α3=Δ0−α2\alpha_1 + \alpha_3 = \Delta_0 - \alpha_2 and the value of α3−α1\alpha_3 - \alpha_1: the condition becomes α2(H~−u~2/2)=H~Δ0+u~Δ1−u~2Δ0−Δ2\alpha_2(\tilde H - \tilde u^2/2) = \tilde H\Delta_0 + \tilde u\Delta_1 - \tilde u^2\Delta_0 - \Delta_2, which is the formula for α2\alpha_2 because H~−u~2/2=c~2/(γ−1)\tilde H - \tilde u^2/2 = \tilde c^2/(\gamma - 1) (positive by Lemma 1). Then α1=12((Δ0−α2)−(Δ1−u~Δ0)/c~)\alpha_1 = \frac12\big((\Delta_0 - \alpha_2) - (\Delta_1 - \tilde u\Delta_0)/\tilde c\big), which is the formula of line L3. □\square

Lemmas 2 and 3 together give FR−FL=∑kλkαkrkF_R - F_L = \sum_k \lambda_k\alpha_k r_k. The test checks both identities on 100 random pairs of states.

Theorem 1 (the Roe flux is the Godunov flux of the linearized problem). Let VV solve the linear Riemann problem Vt+A^Vx=0V_t + \hat A V_x = 0 with data ULU_L, URU_R. Then FL+A^(V(0+)−UL)=FroeF_L + \hat A\big(V(0^+) - U_L\big) = F^{roe}, so the Roe flux is the flux of the linearized solution at the face, made consistent with FF by property U.

Proof. The solution of the linear problem is V(x/t)=UL+∑λk<x/tαkrkV(x/t) = U_L + \sum_{\lambda_k < x/t}\alpha_k r_k. So

FL+A^(V(0)−UL)=FL+∑λk<0λkαkrk=FR−∑λk>0λkαkrk,F_L + \hat A(V(0) - U_L) = F_L + \sum_{\lambda_k < 0}\lambda_k\alpha_k r_k = F_R - \sum_{\lambda_k > 0}\lambda_k\alpha_k r_k,

where the second form uses FR−FL=∑kλkαkrkF_R - F_L = \sum_k\lambda_k\alpha_k r_k. The mean of the two forms is 12(FL+FR)−12∑k∣λk∣αkrk\frac12(F_L + F_R) - \frac12\sum_k|\lambda_k|\alpha_k r_k. □\square

Theorem 2 (consistency and conservation). Froe(U,U)=F(U)F^{roe}(U, U) = F(U), and on a periodic grid the scheme keeps ∑jUj\sum_j U_j.

Proof. With UL=URU_L = U_R, Δ=0\Delta = 0, so all αk=0\alpha_k = 0, and the flux is F(U)F(U). The update is in conservation form, so the sum telescopes. □\square

Theorem 3 (a single discontinuity is exact). Let UL≠URU_L \ne U_R satisfy the Rankine-Hugoniot condition FR−FL=s(UR−UL)F_R - F_L = s(U_R - U_L) for some speed ss. Then UR−ULU_R - U_L is an eigenvector of A^\hat A with eigenvalue ss, and Froe=FLF^{roe} = F_L if s≥0s \ge 0 and Froe=FRF^{roe} = F_R if s<0s < 0, the flux of the moving discontinuity. If s=0s = 0, a grid with ULU_L on the left and URU_R on the right of one face is a steady solution of the scheme. This holds whether or not the discontinuity satisfies the entropy condition.

Proof. By Lemma 2, A^Δ=FR−FL=sΔ\hat A\Delta = F_R - F_L = s\Delta. The eigenvalues of A^\hat A are distinct (Lemma 1 gives c~>0\tilde c > 0), so Δ\Delta is a multiple of one eigenvector rkr_k with λk=s\lambda_k = s, and by Lemma 3, α\alpha has only that component. Then ∑k∣λk∣αkrk=∣s∣Δ\sum_k|\lambda_k|\alpha_k r_k = |s|\Delta, and

Froe=12(FL+FR)−12∣s∣Δ=12(FL+FR)∓12(FR−FL),F^{roe} = \tfrac12(F_L + F_R) - \tfrac12|s|\Delta = \tfrac12(F_L + F_R) \mp \tfrac12(F_R - F_L),

which is FLF_L for s≥0s \ge 0 and FRF_R for s<0s < 0. For s=0s = 0, FL=FRF_L = F_R. Every other face has equal states and the flux FLF_L or FRF_R. All flux differences are zero. □\square

The tests check this for a contact at rest, a Mach 2 shock at rest, and the same pair of states swapped, which is an expansion shock at rest. The scheme keeps all three to 10−1210^{-12}. The last one is not an entropy solution: this is why Roe's scheme needs an entropy fix.

Theorem 4 (a scalar law). For ut+f(u)x=0u_t + f(u)_x = 0 the Roe flux is 12(fL+fR)−12∣a∣(uR−uL)\frac12(f_L + f_R) - \frac12|a|(u_R - u_L) with a=(fR−fL)/(uR−uL)a = (f_R - f_L)/(u_R - u_L). If λmax⁡∣a∣≤1\lambda\max|a| \le 1 the scheme is TVD.

Proof. The scheme is 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 C=λ2(∣a∣−a)≥0C = \frac{\lambda}{2}(|a| - a) \ge 0 and D=λ2(∣a∣+a)≥0D = \frac{\lambda}{2}(|a| + a) \ge 0, since Fj+1/2−fj=12(a−∣a∣)Δj+1/2F_{j+1/2} - f_j = \frac12(a - |a|)\Delta_{j+1/2} and Fj−1/2−fj=−12(a+∣a∣)Δj−1/2F_{j-1/2} - f_j = -\frac12(a + |a|)\Delta_{j-1/2}. So C+D=λ∣a∣≤1C + D = \lambda|a| \le 1, and Harten's lemma (Lemma 3 of the proof on the Rusanov flux page) applies. □\square

Remark (what is not true). The Roe scheme does not satisfy the entropy condition (Theorem 3 with an expansion shock), and it is not positively conservative. Einfeldt, Munz, Roe and Sjögreen (1991) showed that schemes based on linearized Riemann solvers, Roe's among them, are not positive for all data. The test shows it on the 123 problem: after one step, the two center cells have negative pressure.

Complexity

Theorem, in the real-RAM model: for a system of mm equations, the Roe flux costs O(m2)O(m^2) operations once the eigenvectors are known in closed form, as here, plus a constant number of square roots. One step costs O(m2M)O(m^2 M) and the memory is O(mM)O(mM). A fixed end time at a fixed Courant number takes O(M)O(M) steps, so the total is O(m2M2)O(m^2 M^2). For the Euler equations, m=3m = 3, and the cost is a few times that of HLL.

Properties

  • Exact for single waves. The face flux of a single shock or contact is exact. One at rest stays sharp. A moving one still smears over a few cells, because the scheme averages over each cell every step.
  • Upwind on each field. Each wave is upwinded by the sign of its own speed, so the dissipation on the contact wave is 12∣u~∣\frac12|\tilde u|, which is zero for a contact at rest.
  • Not entropy satisfying. A sonic point is where some λk\lambda_k changes sign. There the dissipation ∣λk∣|\lambda_k| is zero and an expansion shock can stay. The Harten-Hyman fix adds dissipation there.
  • Not positive. Near vacuum and in strong rarefactions it can give negative density or pressure, as on the 123 problem.
  • Carbuncle. In 2D, a strong shock aligned with the grid can grow a spurious bump, the carbuncle (Quirk 1994).

Variants and improvements

  • Entropy fixes. The Harten-Hyman fix replaces ∣λk∣|\lambda_k| near a sonic point by a larger value. Harten's earlier fix uses a smooth parabola with a fixed width.
  • Roe-type schemes for other systems. A matrix with property U exists for every system with a convex entropy (Harten, Lax and van Leer 1983). For shallow water, MHD and real gases, special averages are known.
  • HLLE and HLLC. These need only speed estimates and are positive. HLLC keeps contacts like Roe.
  • Flux difference splitting. The Roe flux can be written FL+A^−ΔF_L + \hat A^-\Delta, a split of the flux difference into left and right going parts. It contrasts with flux vector splitting (LeVeque 2002, Section 4.13).
  • High order. Use the flux on MUSCL or WENO values with a Runge-Kutta method.

When to use it

Use the Roe solver when sharp shocks and contacts matter and the flow stays away from vacuum, for example in steady aerodynamics. Always add an entropy fix. Prefer HLLC or HLLE near vacuum, in strong rarefactions and at strong shocks aligned with the grid. For systems with no closed form eigenvectors, the cost of the Roe solver grows, and HLL type solvers are simpler.

Pitfalls

  • No entropy fix. Without one, a transonic rarefaction can turn into an expansion shock. This is the most common error with Roe's solver.
  • The arithmetic mean. Taking A^\hat A as the Jacobian at 12(UL+UR)\frac12(U_L + U_R) breaks property U. The scheme is then still conservative, but shocks are no longer exact. The weights must be ρ\sqrt{\rho}.
  • Negative pressure. Check density and pressure after each step, and fall back to HLL in cells where they become negative.
  • Dividing by c~\tilde c. The strengths of line L3 divide by c~\tilde c. Lemma 1 of the proof shows c~>0\tilde c > 0 for physical states, but a state with p≤0p \le 0 gives a domain error.

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.

roe_solver.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/roe-solver/. The authors are listed in prov/9FFHHH.jsonl.

"""Roe approximate Riemann solver for the 1D Euler equations (Roe 1981), without an entropy fix.

The flux is (F_L + F_R)/2 - (1/2) sum_k |lambda_k| alpha_k r_k, where lambda_k and r_k are the
eigenvalues and eigenvectors of the Jacobian at the Roe average and alpha = R^{-1}(U_R - U_L).
"""

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 roe_average(ql, qr, g=GAMMA):
    """The Roe averages (u, H, c), weighted by the square roots of the densities."""
    rl, ul, pl = cons_to_prim(ql, g)
    rr, ur, pr = cons_to_prim(qr, g)
    wl, wr = sqrt(rl), sqrt(rr)
    u = (wl * ul + wr * ur) / (wl + wr)
    h = (wl * (ql[2] + pl) / rl + wr * (qr[2] + pr) / rr) / (wl + wr)
    return u, h, sqrt((g - 1.0) * (h - 0.5 * u * u))


def roe_waves(ql, qr, g=GAMMA):
    """The eigenvalues, the right eigenvectors and the wave strengths of the Roe matrix."""
    u, h, c = roe_average(ql, qr, g)  # @line L1
    lam = (u - c, u, u + c)  # @line L2
    vecs = ((1.0, u - c, h - u * c), (1.0, u, 0.5 * u * u), (1.0, u + c, h + u * c))
    d0, d1, d2 = (b - a for a, b in zip(ql, qr))  # @line L3
    a2 = (g - 1.0) / (c * c) * (d0 * (h - u * u) + u * d1 - d2)
    a1 = (d0 * (u + c) - d1 - c * a2) / (2.0 * c)
    return lam, vecs, (a1, a2, d0 - a1 - a2)


def roe_flux(ql, qr, g=GAMMA):
    """The Roe flux at the face between the conserved states ql and qr."""
    lam, vecs, alpha = roe_waves(ql, qr, g)
    fl, fr = euler_flux(ql, g), euler_flux(qr, g)
    return tuple(0.5 * (fl[i] + fr[i]) - 0.5 * sum(abs(lam[k]) * alpha[k] * vecs[k][i] for k in range(3)) for i in range(3))  # @line L4


def roe_scalar(ul, ur, f, df):
    """The Roe flux for a scalar law: the speed is the slope of the chord of f."""
    a = (f(ur) - f(ul)) / (ur - ul) if ur != ul else df(ul)  # @line L1
    return (f(ul) + f(ur)) / 2 - abs(a) * (ur - ul) / 2  # @line L4


def step(cells, dt, dx, flux, periodic=False):
    """One forward Euler step of the conservative scheme. cells is a list of states (tuples or numbers)."""
    ext = ([cells[-1]] + list(cells) + [cells[0]]) if periodic else ([cells[0]] + list(cells) + [cells[-1]])  # @line L5
    faces = [flux(ext[j], ext[j + 1]) for j in range(len(ext) - 1)]  # @line L6
    lam = dt / dx
    if isinstance(cells[0], tuple):  # @line L7
        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 to t_end with dt = cfl dx / max(|u| + c). Returns (cells, number of steps)."""
    t, n = 0.0, 0
    flux = lambda a, b: roe_flux(a, b, g)
    while t < t_end - 1e-14:
        smax = 0.0
        for q in cells:
            rho, u, p = cons_to_prim(q, g)
            smax = max(smax, abs(u) + sqrt(g * p / rho))
        dt = min(cfl * dx / smax, t_end - t)
        cells = step(cells, dt, dx, flux, periodic)
        t += dt
        n += 1
    return cells, n
test_roe_solver.py 200 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/roe-solver/. The authors are listed in prov/9FFHHH.jsonl.

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

from roe_solver import (
    cons_to_prim,
    euler_flux,
    prim_to_cons,
    roe_flux,
    roe_scalar,
    roe_waves,
    solve_euler,
    step,
)

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


def exact_solution(left, right, g=1.4):
    """Reference: the exact Riemann solution. Returns a function of xi = x / t giving (rho, u, p)."""
    (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))

    def sample(xi):
        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)))

    return sample


def sod_l1(m, t=0.2):
    """L1 error of the density against exact cell averages (16-point midpoint rule per cell)."""
    sample = exact_solution(*SOD)
    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(sample(((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 normal_shock(mach, g=1.4):
    """A shock at rest: the supersonic state (1, M c, 1) and the subsonic state behind it."""
    u = mach * sqrt(g)
    r = (g + 1.0) * mach**2 / ((g - 1.0) * mach**2 + 2.0)
    p = (2.0 * g * mach**2 - (g - 1.0)) / (g + 1.0)
    return (1.0, u, 1.0), (r, u / r, p)


def run_steps(cells, n, cfl=0.9):
    dx = 1.0 / len(cells)
    for _ in range(n):
        smax = max(abs(cons_to_prim(q)[1]) + sqrt(1.4 * cons_to_prim(q)[2] / q[0]) for q in cells)
        cells = step(cells, cfl * dx / smax, dx, roe_flux)
    return cells


def test_flux_at_the_sod_interface_by_hand() -> None:
    ql, qr = prim_to_cons(SOD[0]), prim_to_cons(SOD[1])
    lam, _, alpha = roe_waves(ql, qr)
    h = (3.5 + sqrt(0.125) * 2.8) / (1.0 + sqrt(0.125))
    c = sqrt(0.4 * h)
    a2 = 0.4 / (c * c) * (-0.875 * h + 2.25)
    assert abs(alpha[1] - a2) < 1e-14 and abs(alpha[0] - (-0.875 - a2) / 2) < 1e-14 and alpha[0] == alpha[2]
    f = roe_flux(ql, qr)
    assert abs(f[0] + c * alpha[0]) < 1e-14 and abs(f[1] - 0.55) < 1e-14 and abs(f[2] + c * alpha[0] * h) < 1e-14
    assert abs(f[0] - 0.390660) < 1e-6 and abs(f[2] - 1.295882) < 1e-6


def test_property_u() -> None:
    rng = random.Random(4)
    for _ in range(100):
        ql = prim_to_cons((rng.uniform(0.1, 3), rng.uniform(-3, 3), rng.uniform(0.1, 3)))
        qr = prim_to_cons((rng.uniform(0.1, 3), rng.uniform(-3, 3), rng.uniform(0.1, 3)))
        lam, vecs, alpha = roe_waves(ql, qr)
        df = [b - a for a, b in zip(euler_flux(ql), euler_flux(qr))]
        for i in range(3):
            assert abs(sum(alpha[k] * vecs[k][i] for k in range(3)) - (qr[i] - ql[i])) < 1e-11
            assert abs(sum(lam[k] * alpha[k] * vecs[k][i] for k in range(3)) - df[i]) < 1e-10 * (1 + abs(df[i]))


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(roe_flux(q, q), euler_flux(q)))


def test_stationary_contact_is_kept_exactly() -> 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[0] - (1.0 if j < m // 2 else 0.5)) < 1e-13 for j, x in enumerate(w))
    assert all(abs(x[1]) < 1e-13 and abs(x[2] - 1.0) < 1e-13 for x in w)


def test_stationary_shock_is_kept_exactly() -> None:
    a, b = normal_shock(2.0)
    assert abs(b[0] - 8.0 / 3.0) < 1e-14 and abs(b[2] - 4.5) < 1e-14
    cells = [prim_to_cons(a if j < 20 else b) for j in range(40)]
    out = run_steps(cells, 30)
    assert all(abs(x - y) < 1e-12 for p, q in zip(cells, out) for x, y in zip(p, q))


def test_stationary_expansion_shock_is_also_kept() -> None:
    a, b = normal_shock(2.0)
    cells = [prim_to_cons(b if j < 20 else a) for j in range(40)]  # subsonic to supersonic: violates the entropy condition
    out = run_steps(cells, 30)
    assert all(abs(x - y) < 1e-12 for p, q in zip(cells, out) for x, y in zip(p, q))
    u = [-1.0] * 10 + [1.0] * 10  # the same for Burgers' equation
    out = step(u, 0.05, 0.1, lambda x, y: roe_scalar(x, y, lambda v: v * v / 2, lambda v: v))
    assert out == u


def test_sod_l1_error_decreases() -> None:
    e100, e200 = sod_l1(100), sod_l1(200)
    assert e200 < 0.0092 and e100 < 0.0137
    assert 0.4 < log2(e100 / e200) < 0.8


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() -> 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))
    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 x, y: roe_scalar(x, y, lambda v: v * v / 2, lambda v: v), periodic=True)
        assert sum(u) == total


def test_tvd_for_burgers() -> None:
    f, df = (lambda v: v * v / 2), (lambda v: v)
    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):
        u = step(u, 0.9 * dx / max(abs(v) for v in u), dx, lambda x, y: roe_scalar(x, y, f, df))
        tv = sum(abs(u[j + 1] - u[j]) for j in range(m - 1))
        assert tv <= tv0 + 1e-12
        tv0 = tv


def test_negative_pressure_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 = run_steps(cells, 1)  # Roe's linearization is not positively conservative
    assert min(cons_to_prim(q)[2] for q in cells) < 0.0

History

Roe (1981) introduced the linearization with property U and the parameter vector Z=ρ(1,u,H)Z = \sqrt\rho(1, u, H) for the Euler equations. The scalar scheme is often called the Murman-Roe scheme, after earlier work of Murman on transonic flow. Harten, Lax and van Leer (1983) proved that a Roe matrix exists for every system with a convex entropy. Harten and Hyman (1983) gave the entropy fix used with the solver. Einfeldt, Munz, Roe and Sjögreen (1991) showed that linearized solvers are not positive, and Quirk (1994) collected the failures of the solver in 2D, among them the carbuncle.

References

  1. P. L. Roe, Approximate Riemann solvers, parameter vectors, and difference schemes, Journal of Computational Physics 43(2) (1981) 357-372, property U, the parameter vector and the averaged Jacobian for the Euler equations; doi:10.1016/0021-9991(81)90128-5
  2. Randall J. LeVeque, Finite Volume Methods for Hyperbolic Problems, Cambridge University Press, 2002, Section 4.14 (Roe's method for linear systems), Section 15.3 (approximate Riemann solvers, the Roe linearization and the entropy fix)
  3. Eleuterio F. Toro, Riemann Solvers and Numerical Methods for Fluid Dynamics, third edition, Springer, 2009, Chapter 11 (the Riemann solver of Roe, the Roe averages, the wave strengths and the entropy fix)
  4. 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, linearized Riemann solvers are not positively conservative; doi:10.1016/0021-9991(91)90211-3
  5. James J. Quirk, A contribution to the great Riemann solver debate, International Journal for Numerical Methods in Fluids 18(6) (1994) 555-574, failures of the Roe solver such as the carbuncle and the odd-even decoupling; doi:10.1002/fld.1650180603

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