Algorithm Search⌘K

Algorithmcore

HLL Riemann solver

Also called Harten-Lax-van Leer solver, HLLE solver.

Approximates the Riemann fan by one intermediate state between the slowest and fastest wave estimates; robust and positive but diffusive at contacts.

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
Amiram Harten, Peter D. Lax, Bram van Leer, Bernd Einfeldt, S. F. Davis, 1983

The HLL solver replaces the full solution of the Riemann problem at a cell face by a simpler one: a single constant state U∗U^* between a left speed SLS_L and a right speed SRS_R. If these speeds bound all waves of the exact solution, the integral form of the conservation law fixes U∗U^* and the flux with no further assumption. The solver needs no eigenvectors and no iteration, only two speed estimates. Harten, Lax and van Leer (1983) defined it. With the speed estimates of Einfeldt (1988) it is often called HLLE. It is the standard robust solver of Godunov type: it keeps density and pressure positive and gives entropy solutions. Its weakness is that the one middle state averages away a contact discontinuity, which HLLC restores.

In plain words

At each face between two cells, gas from the left meets gas from the right, and a set of waves goes out. The exact answer has several waves and needs an iteration. HLL keeps only the fastest wave to the left and the fastest wave to the right and puts one averaged state between them. The conservation law tells you exactly what that averaged state must be, once you know the two speeds. So the method is simple and it never makes negative density, but anything inside the fan, such as a contact between hot and cold gas, is blurred into the average.

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 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}). The page uses 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, c=γp/ρc = \sqrt{\gamma p/\rho} and γ=1.4\gamma = 1.4, and a scalar law ut+f(u)x=0u_t + f(u)_x = 0.

The algorithm

  1. HLL-FLUX(U_L, U_R)
  2. 1(S_L, S_R) <- WAVE-SPEEDS(U_L, U_R)
  3. 2if S_L >= 0: return F(U_L) ▷ all waves go right
  4. 3if S_R <= 0: return F(U_R) ▷ all waves go left
  5. 4return (S_R F(U_L) - S_L F(U_R) + S_L S_R (U_R - U_L)) / (S_R - S_L)
  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} <- HLL-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})

The code gives two choices for line L1.

  • Davis: SL=min⁡(uL−cL,uR−cR)S_L = \min(u_L - c_L, u_R - c_R) and SR=max⁡(uL+cL,uR+cR)S_R = \max(u_L + c_L, u_R + c_R).
  • Einfeldt (the default): SL=min⁡(uL−cL,u~−c~)S_L = \min(u_L - c_L, \tilde u - \tilde c) and SR=max⁡(uR+cR,u~+c~)S_R = \max(u_R + c_R, \tilde u + \tilde c), where u~\tilde u, H~\tilde H are the Roe averages, weighted by ρ\sqrt\rho, and c~2=(γ−1)(H~−u~2/2)\tilde c^2 = (\gamma - 1)(\tilde H - \tilde u^2/2) with H=(E+p)/ρH = (E + p)/\rho.

For a scalar law the code takes SL=min⁡(f′(uL),f′(uR))S_L = \min(f'(u_L), f'(u_R)) and SR=max⁡(f′(uL),f′(uR))S_R = \max(f'(u_L), f'(u_R)). 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 flux of line L4 comes from the HLL state

U∗=SRUR−SLUL−(FR−FL)SR−SL,Fhll=FL+SL(U∗−UL),U^* = \frac{S_R U_R - S_L U_L - (F_R - F_L)}{S_R - S_L}, \qquad F^{hll} = F_L + S_L(U^* - U_L),

which are the integral form of the law over the fan and the jump condition across the left wave.

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 UL=(1,0,2.5)U_L = (1, 0, 2.5), UR=(0.125,0,0.25)U_R = (0.125, 0, 0.25), FL=(0,1,0)F_L = (0, 1, 0) and FR=(0,0.1,0)F_R = (0, 0.1, 0). The enthalpies are HL=3.5H_L = 3.5 and HR=2.8H_R = 2.8. With weights 11 and 0.125=0.353553\sqrt{0.125} = 0.353553, the Roe averages are u~=0\tilde u = 0 and H~=4.48995/1.353553=3.31715\tilde H = 4.48995/1.353553 = 3.31715, so c~=0.4×3.31715=1.15190\tilde c = \sqrt{0.4 \times 3.31715} = 1.15190. The Einfeldt speeds are SL=min⁡(−1.18322,−1.15190)=−1.18322S_L = \min(-1.18322, -1.15190) = -1.18322 and SR=max⁡(1.05830,1.15190)=1.15190S_R = \max(1.05830, 1.15190) = 1.15190. Then SR−SL=2.33512S_R - S_L = 2.33512, SLSR=−1.36294S_L S_R = -1.36294, and line L4 gives

F=(1.36294×0.8752.33512, 1.15190+0.1183222.33512, 1.36294×2.252.33512)=(0.510714,0.543964,1.313264).F = \Big(\frac{1.36294 \times 0.875}{2.33512},\ \frac{1.15190 + 0.118322}{2.33512},\ \frac{1.36294 \times 2.25}{2.33512}\Big) = (0.510714, 0.543964, 1.313264).

The exact Godunov flux is (0.395391,0.669837,1.154038)(0.395391, 0.669837, 1.154038), and the Rusanov flux is (0.517657,0.55,1.331118)(0.517657, 0.55, 1.331118). With the Davis speeds, SL=−SR=−1.18322S_L = -S_R = -1.18322, and the HLL flux is exactly the Rusanov flux.

The HLL state is an average. With SL=−2S_L = -2 and SR=2S_R = 2, which bound all Sod waves (the shock moves at 1.75216), the HLL state is U∗=(0.5625,0.225,1.375)U^* = (0.5625, 0.225, 1.375). The test computes the average of the exact solution over −2≤x/t≤2-2 \le x/t \le 2 with 4000 midpoints and finds the same state to 2×10−32 \times 10^{-3}, the error of the midpoint rule at the three jumps.

Sod's tube. On [0,1][0, 1] with the jump at x=0.5x = 0.5, outflow boundaries and Courant number 0.9, the scheme runs to t=0.2t = 0.2. The L1L^1 errors of the density against exact cell averages are measurements made with the code on this page.

cells MM steps Einfeldt Davis
100 48 0.01505 0.01522
200 96 0.00994 0.01008
400 194 0.00629 0.00634

The observed orders with the Einfeldt speeds are 0.60 and 0.66. The Rusanov errors on the same grids are 0.02148, 0.01515 and 0.00998.

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.0080080.008008, 0.0040100.004010, 0.0020160.002016 and 0.0010100.001010, with orders 1.00, 0.99 and 1.00. They are half of the Rusanov errors, because here SL=u−cS_L = u - c is much smaller in size than ∣u∣+c|u| + c.

A contact at rest. With ρ=1\rho = 1 on the left half and 0.50.5 on the right half, u=0u = 0 and p=1p = 1, after 8 steps on 40 cells the velocity and the pressure are exact to rounding, but the density around the contact is 0.968,0.927,0.863,0.783,0.702,0.630,0.576,0.5390.968, 0.927, 0.863, 0.783, 0.702, 0.630, 0.576, 0.539. HLL averages the contact into the middle state.

Correctness

The proof is in proofs/correctness.md. Theorem: the flux is consistent and the scheme is conservative. If the speeds bound the waves of each local Riemann problem and ΔtΔxmax⁡∣S∣≤12\frac{\Delta t}{\Delta x}\max|S| \le \frac12, then (1) U∗U^* is the average of the exact Riemann solution over the fan, (2) for the Euler equations, positive density and pressure are kept, because the admissible set is convex and the new cell average is an average of admissible states, and (3) for any convex entropy pair (η,q)(\eta, q) the scheme satisfies the cell entropy inequality η(Ujn+1)≤η(Ujn)−λ(Qj+1/2−Qj−1/2)\eta(U_j^{n+1}) \le \eta(U_j^n) - \lambda(Q_{j+1/2} - Q_{j-1/2}) with a consistent numerical entropy flux QQ. With the Lax-Wendroff theorem, bounded L1L^1 limits are entropy weak solutions. For a convex scalar law the scheme is TVD for λmax⁡∣S∣≤1\lambda\max|S| \le 1.

The estimates of Davis and Einfeldt are not proven bounds: for Sod's data they give SR=1.18322S_R = 1.18322 and 1.151901.15190, below the shock speed 1.75216. The positivity of HLLE with the Einfeldt speeds rests on the analysis of Einfeldt, Munz, Roe and Sjögreen (1991), which the proof here does not repeat.

Proof: The HLL scheme is conservative and consistent, and with speeds that bound the waves it keeps positivity and satisfies a cell entropy inequalityfull

Setting. A system Ut+F(U)x=0U_t + F(U)_x = 0 has a convex entropy pair (η,q)(\eta, q), so that entropy solutions satisfy η(U)t+q(U)x≤0\eta(U)_t + q(U)_x \le 0 in the weak sense. 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 L7). At each face, with UL=UjU_L = U_j and UR=Uj+1U_R = U_{j+1}, lines L1 to L4 give

Fhll={FLSL≥0,SRFL−SLFR+SLSR(UR−UL)SR−SLSL<0<SR,FRSR≤0,F^{hll} = \begin{cases} F_L & S_L \ge 0,\\ \dfrac{S_R F_L - S_L F_R + S_L S_R (U_R - U_L)}{S_R - S_L} & S_L < 0 < S_R,\\ F_R & S_R \le 0,\end{cases}

with FL=F(UL)F_L = F(U_L) and FR=F(UR)F_R = F(U_R). Let SL−=min⁡(SL,0)S_L^- = \min(S_L, 0) and SR+=max⁡(SR,0)S_R^+ = \max(S_R, 0). A short check shows that the middle formula with SL−S_L^- and SR+S_R^+ in place of SLS_L and SRS_R gives all three cases: if SL≥0S_L \ge 0 it gives SRFL/SR=FLS_R F_L / S_R = F_L, and if SR≤0S_R \le 0 it gives FRF_R. So below we use only the middle formula, with speeds that satisfy SL≤0≤SRS_L \le 0 \le S_R.

Define the HLL state

U∗=SRUR−SLUL−(FR−FL)SR−SL.U^* = \frac{S_R U_R - S_L U_L - (F_R - F_L)}{S_R - S_L}.

The approximate Riemann solution is U^(x/t)=UL\hat U(x/t) = U_L for x/t<SLx/t < S_L, U∗U^* for SL<x/t<SRS_L < x/t < S_R, and URU_R for x/t>SRx/t > S_R.

We say that the speeds bound the waves if every wave of the exact Riemann solution with data ULU_L, URU_R moves at a speed in [SL,SR][S_L, S_R]. The proofs of Lemma 3 and Theorems 1 and 2 need this. The estimates of line L1 do not give it in every case (see the remark at the end).

Lemma 1 (consistency and conservation). Fhll(U,U)=F(U)F^{hll}(U, U) = F(U). On a periodic grid, ∑jUjn+1=∑jUjn\sum_j U_j^{n+1} = \sum_j U_j^n.

Proof. With UL=UR=UU_L = U_R = U, the middle formula is (SR−SL)F(U)/(SR−SL)=F(U)(S_R - S_L)F(U)/(S_R - S_L) = F(U), and the other two cases give F(U)F(U) directly. For conservation, each face flux enters cell jj with the factor −λ-\lambda and cell j+1j+1 with +λ+\lambda, so the sum telescopes. The test checks this exactly for Burgers' equation in Fraction arithmetic. □\square

Lemma 2 (the flux is the flux of the approximate solution). Fhll=FL+SL(U∗−UL)=FR+SR(U∗−UR)F^{hll} = F_L + S_L(U^* - U_L) = F_R + S_R(U^* - U_R).

Proof. U∗−UL=(SR(UR−UL)−(FR−FL))/(SR−SL)U^* - U_L = \big(S_R(U_R - U_L) - (F_R - F_L)\big)/(S_R - S_L). So

FL+SL(U∗−UL)=(SR−SL)FL+SLSR(UR−UL)−SLFR+SLFLSR−SL=SRFL−SLFR+SLSR(UR−UL)SR−SL.F_L + S_L(U^* - U_L) = \frac{(S_R - S_L)F_L + S_L S_R (U_R - U_L) - S_L F_R + S_L F_L}{S_R - S_L} = \frac{S_R F_L - S_L F_R + S_L S_R (U_R - U_L)}{S_R - S_L}.

The second form follows in the same way, or from the first form and the definition of U∗U^*. □\square

The two forms are the Rankine-Hugoniot conditions of the two jumps of U^\hat U, at speed SLS_L and at speed SRS_R. So U^\hat U is a weak solution of the conservation law in the integral sense over the fan, though not a true solution.

Lemma 3 (the HLL state is the average of the exact fan). If the speeds bound the waves and UU is the exact Riemann solution, then for every T>0T > 0

U∗=1(SR−SL)T∫SLTSRTU(x,T) dx.U^* = \frac{1}{(S_R - S_L)T}\int_{S_L T}^{S_R T} U(x, T)\,dx.

Proof. Integrate Ut+F(U)x=0U_t + F(U)_x = 0 over the rectangle [SLT,SRT]×[0,T][S_L T, S_R T] \times [0, T]. Because the speeds bound the waves, U=ULU = U_L on the left side x=SLTx = S_L T and U=URU = U_R on the right side x=SRTx = S_R T for 0<t<T0 < t < T. The integral form of the law gives

∫SLTSRTU(x,T) dx=SRT UR−SLT UL−T(FR−FL).\int_{S_L T}^{S_R T} U(x, T)\,dx = S_R T\, U_R - S_L T\, U_L - T(F_R - F_L).

Divide by (SR−SL)T(S_R - S_L)T. □\square

Lemma 4 (Jensen). Let η\eta be convex on a convex set GG and let VV be an integrable function on an interval II with values in GG. Then the average Vˉ\bar V is in GG and η(Vˉ)≤1∣I∣∫Iη(V) dx\eta(\bar V) \le \frac{1}{|I|}\int_I \eta(V)\,dx.

Proof. This is Jensen's inequality for the uniform probability measure on II. Vˉ\bar V is in the closed convex hull of the values of VV. □\square

Lemma 5 (the admissible set is convex). For the Euler equations, the set G={(ρ,m,E):ρ>0, E−m2/(2ρ)>0}G = \{(\rho, m, E) : \rho > 0,\ E - m^2/(2\rho) > 0\} of states with positive density and pressure is convex, and the pressure p=(γ−1)(E−m2/(2ρ))p = (\gamma - 1)(E - m^2/(2\rho)) is a concave function on ρ>0\rho > 0.

Proof. The function ϕ(ρ,m)=m2/ρ\phi(\rho, m) = m^2/\rho is convex on ρ>0\rho > 0: its Hessian is 2ρ3(m2−mρ−mρρ2)\frac{2}{\rho^3}\begin{pmatrix} m^2 & -m\rho \\ -m\rho & \rho^2\end{pmatrix}, which has determinant 0 and trace 2(m2+ρ2)/ρ3>02(m^2 + \rho^2)/\rho^3 > 0, so it is positive semidefinite. Then E−ϕ/2E - \phi/2 is concave, and GG is the intersection of the half space ρ>0\rho > 0 with the set where a concave function is positive. Both are convex. □\square

Theorem 1 (positivity). For the Euler equations, let every face use speeds that bound the waves, and let λmax⁡∣S∣≤12\lambda \max |S| \le \frac12. If every UjnU_j^n is in GG, then every Ujn+1U_j^{n+1} is in GG.

Proof. Put the approximate solution U^\hat U of each face at the face, and run it for the time Δt\Delta t. Because λmax⁡∣S∣≤12\lambda\max|S| \le \frac12, each fan stays in the half cells next to its face, and the fans do not meet. Let W(x)W(x) be the function so made on cell jj at time Δt\Delta t. It is UjU_j except on the part [xj−1/2,xj−1/2+SRΔt][x_{j-1/2}, x_{j-1/2} + S_R\Delta t], where it is the state U∗U^* of the left face, and on the part [xj+1/2+SLΔt,xj+1/2][x_{j+1/2} + S_L\Delta t, x_{j+1/2}], where it is the state U∗U^* of the right face. By Lemma 2 its cell average is

1Δx∫CjW dx=Uj−λ(Fj+1/2−Fj−1/2)=Ujn+1,\frac{1}{\Delta x}\int_{C_j} W\,dx = U_j - \lambda\big(F_{j+1/2} - F_{j-1/2}\big) = U_j^{n+1},

because the integral over each half fan changes by exactly −Δt-\Delta t times the flux difference across it. By Lemma 3, each U∗U^* is the average of an exact Riemann solution with data in GG. That solution has positive density and pressure except in a vacuum region, if the data make one, where ρ=m=E=0\rho = m = E = 0. It equals ULU_L or URU_R near the two sides of the fan. So the average density is positive, and by the concavity of Lemma 5 (Jensen's inequality for the concave pressure, with p=0p = 0 in the vacuum) the pressure of the average is at least the average pressure, which is positive. So U∗U^* is in GG. Then Ujn+1U_j^{n+1} is the average of WW, whose values are in GG, so by Lemma 4 again it is in GG. □\square

Theorem 2 (cell entropy inequality). Let every face use speeds that bound the waves, and let λmax⁡∣S∣≤12\lambda \max|S| \le \frac12. Define the numerical entropy flux at each face by

Qj+1/2=qL+SL(η(U∗)−ηL),Q_{j+1/2} = q_L + S_L\big(\eta(U^*) - \eta_L\big),

with ηL=η(UL)\eta_L = \eta(U_L) and qL=q(UL)q_L = q(U_L). Then Q(U,U)=q(U)Q(U, U) = q(U) and

η(Ujn+1)≤η(Ujn)−λ(Qj+1/2−Qj−1/2).\eta(U_j^{n+1}) \le \eta(U_j^n) - \lambda\big(Q_{j+1/2} - Q_{j-1/2}\big).

Proof. Consistency: with UL=UR=UU_L = U_R = U, the state U∗U^* is UU, so Q=q(U)Q = q(U).

Fix a face and use local coordinates with the face at x=0x = 0. The exact Riemann solution satisfies the integral form of the entropy inequality over the rectangle [SLΔt,SRΔt]×[0,Δt][S_L \Delta t, S_R \Delta t] \times [0, \Delta t], and its side values are ULU_L and URU_R, so

∫SLΔtSRΔtη(U(x,Δt)) dx≤SRΔt ηR−SLΔt ηL−Δt(qR−qL).\int_{S_L\Delta t}^{S_R\Delta t} \eta(U(x, \Delta t))\,dx \le S_R\Delta t\,\eta_R - S_L\Delta t\,\eta_L - \Delta t(q_R - q_L).

By Lemma 3 and Lemma 4, the left side is at least (SR−SL)Δt η(U∗)(S_R - S_L)\Delta t\,\eta(U^*). So

(SR−SL) η(U∗)≤SRηR−SLηL−(qR−qL).(1)(S_R - S_L)\,\eta(U^*) \le S_R\eta_R - S_L\eta_L - (q_R - q_L). \tag{1}

Now split the fan of U^\hat U at x=0x = 0. On the left half, by the definition of QQ (with equality),

∫SLΔt0η(U^) dx=−SLΔt η(U∗)=−SLΔt ηL−Δt(Q−qL).\int_{S_L\Delta t}^{0} \eta(\hat U)\,dx = -S_L\Delta t\,\eta(U^*) = -S_L\Delta t\,\eta_L - \Delta t(Q - q_L).

On the right half, (1) and the definition of QQ give

∫0SRΔtη(U^) dx=SRΔt η(U∗)≤SRΔt ηR−Δt(qR−Q).\int_0^{S_R\Delta t} \eta(\hat U)\,dx = S_R\Delta t\,\eta(U^*) \le S_R\Delta t\,\eta_R - \Delta t(q_R - Q).

Now take cell jj and the function WW of the proof of Theorem 1. Its integral of η(W)\eta(W) has three parts: the right half of the fan of the face j−1/2j - 1/2 (where UR=UjU_R = U_j), the middle part where W=UjW = U_j, and the left half of the fan of the face j+1/2j + 1/2 (where UL=UjU_L = U_j). Add the two bounds above and η(Uj)\eta(U_j) times the length of the middle part, Δx−SR(j−1/2)Δt+SL(j+1/2)Δt\Delta x - S_R^{(j-1/2)}\Delta t + S_L^{(j+1/2)}\Delta t:

∫Cjη(W) dx≤Δx η(Uj)−Δt(qj−Qj−1/2)−Δt(Qj+1/2−qj)=Δx η(Uj)−Δt(Qj+1/2−Qj−1/2).\int_{C_j}\eta(W)\,dx \le \Delta x\,\eta(U_j) - \Delta t\big(q_j - Q_{j-1/2}\big) - \Delta t\big(Q_{j+1/2} - q_j\big) = \Delta x\,\eta(U_j) - \Delta t\big(Q_{j+1/2} - Q_{j-1/2}\big).

By Lemma 4, η(Ujn+1)=η(average of W)≤1Δx∫Cjη(W) dx\eta(U_j^{n+1}) = \eta(\text{average of } W) \le \frac{1}{\Delta x}\int_{C_j}\eta(W)\,dx. Divide by Δx\Delta x. □\square

With Lemma 1, this gives the conclusion of Harten, Lax and van Leer (1983): if the scheme converges boundedly in Lloc1L^1_{loc}, the Lax-Wendroff theorem makes the limit a weak solution, and the same argument applied to the cell entropy inequality makes it satisfy η(U)t+q(U)x≤0\eta(U)_t + q(U)_x \le 0.

Theorem 3 (TVD for a convex scalar law). For a scalar law with convex ff, let line L1 take SL=min⁡(f′(uL),f′(uR))S_L = \min(f'(u_L), f'(u_R)) and SR=max⁡(f′(uL),f′(uR))S_R = \max(f'(u_L), f'(u_R)). If λmax⁡(∣SL∣,∣SR∣)≤1\lambda \max(|S_L|, |S_R|) \le 1 at every face, the scheme is TVD.

Proof. Let a=(f(uR)−f(uL))/(uR−uL)a = (f(u_R) - f(u_L))/(u_R - u_L) when uR≠uLu_R \ne u_L. Since f′f' is nondecreasing, aa is a mean of f′f' over the interval between the states, so SL≤a≤SRS_L \le a \le S_R. Take the case SL<0<SRS_L < 0 < S_R. From the middle formula,

Fhll−fL=SL(SR−a)SR−SL(uR−uL),Fhll−fR=SR(SL−a)SR−SL(uR−uL).F^{hll} - f_L = \frac{S_L(S_R - a)}{S_R - S_L}(u_R - u_L), \qquad F^{hll} - f_R = \frac{S_R(S_L - a)}{S_R - S_L}(u_R - u_L).

Write Ujn+1=Uj−λ(Fj+1/2−fj)+λ(Fj−1/2−fj)U_j^{n+1} = U_j - \lambda(F_{j+1/2} - f_j) + \lambda(F_{j-1/2} - f_j). This is Harten's incremental form Uj+Cj+1/2Δj+1/2−Dj−1/2Δj−1/2U_j + C_{j+1/2}\Delta_{j+1/2} - D_{j-1/2}\Delta_{j-1/2}, with

C=−λSL(SR−a)SR−SL≥0,D=λSR(a−SL)SR−SL≥0.C = -\lambda\frac{S_L(S_R - a)}{S_R - S_L} \ge 0, \qquad D = \lambda\frac{S_R(a - S_L)}{S_R - S_L} \ge 0.

The sum C+D=λ a(SL+SR)−2SLSRSR−SLC + D = \lambda\,\frac{a(S_L + S_R) - 2S_L S_R}{S_R - S_L} is linear in aa. At a=SRa = S_R it is λSR\lambda S_R, and at a=SLa = S_L it is −λSL-\lambda S_L. So C+D≤λmax⁡(∣SL∣,∣SR∣)≤1C + D \le \lambda\max(|S_L|, |S_R|) \le 1. In the case SL≥0S_L \ge 0 the flux is fLf_L, so C=0C = 0 and D=λa∈[0,1]D = \lambda a \in [0, 1]. The case SR≤0S_R \le 0 is the mirror case. Harten's lemma (proved on the Rusanov flux page, Lemma 3 of its proof) gives TV(un+1)≤TV(un)TV(u^{n+1}) \le TV(u^n). □\square

Remark (the speed estimates are not proven bounds). For Sod's data, the right shock moves at 1.75216. The Davis estimate gives SR=max⁡(uL+cL,uR+cR)=1.18322S_R = \max(u_L + c_L, u_R + c_R) = 1.18322, and the Einfeldt estimate gives SR=1.15190S_R = 1.15190. Neither bounds the shock, so Theorems 1 and 2 do not apply to these faces. The scheme still runs well in the tests, but its positivity and entropy properties then rest on the analysis of Einfeldt (1988) and Einfeldt, Munz, Roe and Sjögreen (1991), not on the proof above. Bounds that are proven, such as those built from the two-rarefaction or the two-shock pressure estimates (Toro 2009, Chapter 10), make the theorems apply.

Complexity

Theorem, in the real-RAM model: one HLL flux costs O(m)O(m) operations, plus a constant number of square roots for the speeds. One step costs O(mM)O(mM) and the memory is O(mM)O(mM). A fixed end time at a fixed Courant number needs O(M)O(M) steps, so the total cost is O(mM2)O(mM^2). The cost per flux is close to that of the Rusanov flux and below that of the Roe flux, which needs the eigenvectors.

Properties

  • First order and upwind. For supersonic flow, SL≥0S_L \ge 0 or SR≤0S_R \le 0, and the flux is the upwind flux FLF_L or FRF_R exactly. The measured order on the smooth wave is 0.99 to 1.00.
  • 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.0228 and the smallest pressure is 0.0070. The exact density at the center is 0.0219. Linearized solvers such as Roe's can give negative density on data of this kind (Einfeldt et al. 1991).
  • Entropy satisfying. A transonic rarefaction is opened: for Burgers' equation with uL=−1u_L = -1 and uR=1u_R = 1 the flux is −12-\frac12, not f(±1)=12f(\pm1) = \frac12, so the jump spreads. No entropy fix is needed.
  • Smears contacts and shear waves. The middle state has no structure, so a contact, at rest or moving, is smeared like in the Rusanov scheme.

Variants and improvements

  • HLLC. Restore the contact with two middle states separated by a contact speed S∗S^*. See HLLC.
  • Rusanov. Use one speed SR=−SL=sS_R = -S_L = s. See the Rusanov flux.
  • Pressure-based speeds. Estimate the middle pressure first, then set the speeds from the shock or rarefaction relations; this gives better bounds (Toro 2009, Chapter 10).
  • HLLEM and other one-state solvers with anti-diffusion. Add back a part of the contact wave with an estimate of its strength (Einfeldt et al. 1991).
  • High order. Use the flux on reconstructed values from MUSCL or WENO with a Runge-Kutta method. Positivity of the reconstruction then needs a positivity-preserving limiter.

When to use it

Use HLL or HLLE when robustness matters more than sharpness: near vacuum, strong rarefactions, strong shocks and for systems whose eigenstructure is costly. It is a good fallback in a code whose main solver is Roe or HLLC. Avoid it at first order when contacts, shear layers or boundary layers must stay sharp, because it smears them like the Rusanov flux.

Pitfalls

  • Speeds that do not bound the waves. The simple estimates can be too small for a strong shock (Sod's shock moves faster than both estimates). The scheme usually still works, but the proven properties then do not apply. Use pressure-based estimates when this matters.
  • The Roe average near vacuum. The Einfeldt speeds need c~2>0\tilde c^2 > 0. With extreme data, check it before the square root.
  • Dividing by SR−SLS_R - S_L. Lines L2 and L3 handle the supersonic cases first, so line L4 has SL<0<SRS_L < 0 < S_R and the divisor is positive. Do not reorder the tests.
  • Time step. The theorems need λmax⁡∣S∣≤12\lambda\max|S| \le \frac12, a stricter bound than the usual Courant condition. The code uses ν=0.9\nu = 0.9 with max⁡(∣u∣+c)\max(|u| + c), which is a common choice that the tests show to work, not a proven bound.

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.

hll_riemann_solver.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hll-riemann-solver/. The authors are listed in prov/MA9G95.jsonl.

"""HLL approximate Riemann solver for the 1D Euler equations (Harten, Lax and van Leer 1983).

The fan between the slowest speed S_L and the fastest speed S_R is replaced by one
constant state. Two speed estimates are given: the one of Davis (1988) and the one of
Einfeldt (1988), which uses Roe averages (HLLE).
"""

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 wave_speeds(ql, qr, g=GAMMA, method="einfeldt"):
    """Estimates (S_L, S_R) of the slowest and the fastest signal speed."""
    rl, ul, pl = cons_to_prim(ql, g)
    rr, ur, pr = cons_to_prim(qr, g)
    cl, cr = sqrt(g * pl / rl), sqrt(g * pr / rr)
    if method == "davis":
        return min(ul - cl, ur - cr), max(ul + cl, ur + cr)
    wl, wr = sqrt(rl), sqrt(rr)  # Roe averages of u and the enthalpy H = (E + p) / rho
    hl, hr = (ql[2] + pl) / rl, (qr[2] + pr) / rr
    u = (wl * ul + wr * ur) / (wl + wr)
    h = (wl * hl + wr * hr) / (wl + wr)
    c = sqrt((g - 1.0) * (h - 0.5 * u * u))
    return min(ul - cl, u - c), max(ur + cr, u + c)


def hll_flux(ql, qr, g=GAMMA, method="einfeldt"):
    """The HLL flux at the face between the conserved states ql and qr."""
    sl, sr = wave_speeds(ql, qr, g, method)  # @line L1
    fl, fr = euler_flux(ql, g), euler_flux(qr, g)
    if sl >= 0.0:  # @line L2
        return fl
    if sr <= 0.0:  # @line L3
        return fr
    return tuple((sr * a - sl * b + sl * sr * (y - x)) / (sr - sl) for a, b, x, y in zip(fl, fr, ql, qr))  # @line L4


def hll_scalar(ul, ur, f, df):
    """The HLL flux for a scalar law u_t + f(u)_x = 0 with speeds from f' at the two states."""
    sl, sr = min(df(ul), df(ur)), max(df(ul), df(ur))  # @line L1
    if sl >= 0:  # @line L2
        return f(ul)
    if sr <= 0:  # @line L3
        return f(ur)
    return (sr * f(ul) - sl * f(ur) + sl * sr * (ur - ul)) / (sr - sl)  # @line L4


def hll_state(ql, qr, sl, sr, g=GAMMA):
    """The HLL intermediate state U* (the average of the exact fan when the speeds bound it)."""
    fl, fr = euler_flux(ql, g), euler_flux(qr, g)
    return tuple((sr * y - sl * x - (b - a)) / (sr - sl) for a, b, x, y in zip(fl, fr, ql, qr))


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, method="einfeldt"):
    """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: hll_flux(a, b, g, method)
    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_hll_riemann_solver.py 192 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hll-riemann-solver/. The authors are listed in prov/MA9G95.jsonl.

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

from hll_riemann_solver import (
    cons_to_prim,
    euler_flux,
    hll_flux,
    hll_scalar,
    hll_state,
    prim_to_cons,
    solve_euler,
    step,
    wave_speeds,
)

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, ps, us


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)[0]
    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 test_einfeldt_speeds_and_flux_at_the_sod_interface() -> None:
    ql, qr = prim_to_cons(SOD[0]), prim_to_cons(SOD[1])
    sl, sr = wave_speeds(ql, qr)
    wr = sqrt(0.125)
    h = (3.5 + wr * 2.8) / (1.0 + wr)  # Roe average of the enthalpy; the Roe average of u is 0
    assert abs(sl + sqrt(1.4)) < 1e-14 and abs(sr - sqrt(0.4 * h)) < 1e-14
    f = hll_flux(ql, qr)
    want = [(sr * a - sl * b + sl * sr * (y - x)) / (sr - sl) for a, b, x, y in zip((0, 1, 0), (0, 0.1, 0), ql, qr)]
    assert all(abs(a - b) < 1e-14 for a, b in zip(f, want))
    assert abs(sr - 1.151895) < 1e-6
    assert abs(f[0] - 0.510714) < 1e-6 and abs(f[1] - 0.543964) < 1e-6 and abs(f[2] - 1.313264) < 1e-6


def test_davis_speeds_give_the_rusanov_flux_for_sod() -> None:
    f = hll_flux(prim_to_cons(SOD[0]), prim_to_cons(SOD[1]), method="davis")
    s = sqrt(1.4)  # S_L = -S_R, so HLL equals the Rusanov flux
    assert abs(f[0] - 0.5 * s * 0.875) < 1e-14 and abs(f[1] - 0.55) < 1e-14 and abs(f[2] - 0.5 * s * 2.25) < 1e-14


def test_consistency_and_upwinding() -> None:
    rng = random.Random(1)
    for _ in range(50):
        w = (rng.uniform(0.1, 2), rng.uniform(-2, 2), rng.uniform(0.1, 2))
        q = prim_to_cons(w)
        for meth in ("davis", "einfeldt"):
            assert all(abs(a - b) < 1e-12 for a, b in zip(hll_flux(q, q, method=meth), euler_flux(q)))
    ql, qr = prim_to_cons((1.0, 3.0, 1.0)), prim_to_cons((0.8, 3.2, 0.9))  # supersonic to the right
    assert hll_flux(ql, qr) == euler_flux(ql)
    ql, qr = prim_to_cons((1.0, -3.0, 1.0)), prim_to_cons((0.8, -3.2, 0.9))  # supersonic to the left
    assert hll_flux(ql, qr) == euler_flux(qr)


def test_hll_state_is_the_average_of_the_exact_fan() -> None:
    sample, _, _ = exact_solution(*SOD)
    sl, sr, n = -2.0, 2.0, 4000  # these speeds bound all Sod waves (the shock moves at 1.75216)
    avg = [0.0, 0.0, 0.0]
    for k in range(n):
        q = prim_to_cons(sample(sl + (sr - sl) * (k + 0.5) / n))
        for i in range(3):
            avg[i] += q[i] / n
    star = hll_state(prim_to_cons(SOD[0]), prim_to_cons(SOD[1]), sl, sr)
    assert all(abs(a - b) < 2e-3 for a, b in zip(avg, star))
    assert abs(star[0] - 0.5625) < 1e-14 and abs(star[1] - 0.225) < 1e-14  # (2 rho_R + 2 rho_L) / 4 and (0.1 - 1) / -4


def test_sod_l1_error_decreases() -> None:
    e100, e200 = sod_l1(100), sod_l1(200)
    assert e200 < 0.0105 and e100 < 0.0155
    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_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: hll_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.5 * dx / max(abs(v) for v in u)
        u = step(u, dt, dx, lambda a, b: hll_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_transonic_rarefaction_is_opened() -> None:
    assert hll_scalar(-1.0, 1.0, lambda u: u * u / 2, lambda u: u) == -0.5  # not f(-1) = f(1), so the jump spreads


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


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.02 and min(x[2] for x in w) > 0.0

History

Harten, Lax and van Leer (1983) set up the theory of approximate Riemann solvers that are consistent with the integral form of the conservation law and of the entropy condition, and gave the solver with one middle state as the simplest example. Davis (1988) and Einfeldt (1988) proposed the speed estimates. Einfeldt showed that the choice with Roe averages keeps the density and the internal energy positive, and Einfeldt, Munz, Roe and Sjögreen (1991) showed that linearized solvers such as Roe's cannot have this property. Toro, Spruce and Speares (1994) added the contact wave to get HLLC.

References

  1. Amiram Harten, Peter D. Lax and Bram van Leer, On upstream differencing and Godunov-type schemes for hyperbolic conservation laws, SIAM Review 25(1) (1983) 35-61, the approximate Riemann solver with one intermediate state, its consistency with the integral form of the conservation law and of the entropy condition; doi:10.1137/1025002
  2. Bernd Einfeldt, On Godunov-type methods for gas dynamics, SIAM Journal on Numerical Analysis 25(2) (1988) 294-318, the speed estimates with Roe averages (HLLE) and positivity; doi:10.1137/0725021
  3. S. F. Davis, Simplified second-order Godunov-type methods, SIAM Journal on Scientific and Statistical Computing 9(3) (1988) 445-473, the simple speed estimates from the two states; doi:10.1137/0909030
  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, positively conservative schemes; the HLLE scheme is positive and linearized solvers are not; doi:10.1016/0021-9991(91)90211-3
  5. Eleuterio F. Toro, Riemann Solvers and Numerical Methods for Fluid Dynamics, third edition, Springer, 2009, Chapter 10 (the HLL and HLLC solvers and the wave speed estimates)
  6. Randall J. LeVeque, Finite Volume Methods for Hyperbolic Problems, Cambridge University Press, 2002, Section 15.3 (approximate Riemann solvers, including the HLL solver), Section 12.10 (the Lax-Wendroff theorem)

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