Algorithm Search⌘K

Algorithmadvanced

HLLC Riemann solver

Also called HLLC flux.

Restores the contact wave in the HLL solver with two intermediate states, giving sharp contacts and shear layers.

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
conservativedeterministicexact isolated contactexplicitkeeps stationary contact
Origin
Eleuterio F. Toro, M. Spruce, W. Speares, 1994

The HLLC solver is the HLL solver with the contact wave put back. HLL replaces the fan of the Riemann problem by one constant state between a left speed SLS_L and a right speed SRS_R. That state cannot hold a contact discontinuity, so HLL smears contacts. HLLC (the C is for contact) splits the middle into two states UL∗U^*_L and UR∗U^*_R at a contact speed S∗S^*. The two states have the same velocity S∗S^* and the same pressure p∗p^*, but different densities. The jump conditions across the three waves then fix everything. Toro, Spruce and Speares (1994) introduced it. It keeps an isolated contact exactly, like the Roe solver, and it keeps most of the robustness of HLL, so it is a default solver in many gas dynamics codes.

In plain words

HLL treats everything between the fastest left wave and the fastest right wave as one blended state. For gas this blends hot and cold gas at a contact, so the contact spreads out over time. HLLC adds one more wave in the middle that moves with the gas. Gas to the left of this wave keeps its own density, gas to the right keeps its own, and only the pressure and the velocity are shared. If the data are already just a contact, the method sees that and moves it without any smearing at the face.

The problem

Given cell averages UjnU_j^n of the Euler equations Ut+F(U)x=0U_t + F(U)_x = 0 with U=(ρ,ρu,E)U = (\rho, \rho u, E), F=(ρu,ρu2+p,u(E+p))F = (\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, 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 flux should be exact for an isolated contact.

The algorithm

  1. HLLC-FLUX(U_L, U_R)
  2. 1(S_L, S_R) <- WAVE-SPEEDS(U_L, U_R) ▷ Einfeldt estimates
  3. 2if S_L >= 0: return F(U_L)
  4. 3if S_R <= 0: return F(U_R)
  5. 4S* <- (p_R - p_L + ρ_L u_L (S_L - u_L) - ρ_R u_R (S_R - u_R)) / (ρ_L (S_L - u_L) - ρ_R (S_R - u_R))
  6. 5if S* >= 0: K <- L else K <- R
  7. 6U*_K <- ρ_K (S_K - u_K)/(S_K - S*) · (1, S*, E_K/ρ_K + (S* - u_K)(S* + p_K/(ρ_K (S_K - u_K))))
  8. 7return F(U_K) + S_K (U*_K - U_K)
  9. FV-STEP(U, Δt, Δx)
  10. 8add one ghost cell on each side (copy for outflow, wrap for periodic)
  11. 9for each face j+1/2: F_{j+1/2} <- HLLC-FLUX(U_j, U_{j+1})
  12. 10for each cell j: U_j <- U_j - (Δt/Δx)(F_{j+1/2} - F_{j-1/2})

The speeds of line L1 are 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) with the Roe averages u~\tilde u, H~\tilde H and c~2=(γ−1)(H~−u~2/2)\tilde c^2 = (\gamma - 1)(\tilde H - \tilde u^2/2). 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 middle pressure is p∗=pK+ρK(SK−uK)(S∗−uK)p^* = p_K + \rho_K(S_K - u_K)(S^* - u_K), the same for K=LK = L and K=RK = R; line L4 is the condition that the two are equal.

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). The Einfeldt speeds are SL=−1.18322S_L = -1.18322 and SR=1.15190S_R = 1.15190 (computed on the HLL page). Both velocities are zero, so line L4 is

S∗=0.1−1SL−0.125 SR=−0.9−1.32721=0.678118.S^* = \frac{0.1 - 1}{S_L - 0.125\,S_R} = \frac{-0.9}{-1.32721} = 0.678118.

Since S∗>0S^* > 0, K=LK = L. Line L6 gives ρL∗=SL/(SL−S∗)=0.635682\rho^*_L = S_L/(S_L - S^*) = 0.635682, so the middle state is UL∗=(0.635682,0.431067,1.517200)U^*_L = (0.635682, 0.431067, 1.517200). The middle pressure is p∗=1+(−1.18322)(0.678118)=0.197640p^* = 1 + (-1.18322)(0.678118) = 0.197640. Line L7 gives the flux

F=(ρL∗S∗, ρL∗S∗2+p∗, S∗(EL∗+p∗))=(0.431067,0.489954,1.162864).F = (\rho^*_L S^*,\ \rho^*_L S^{*2} + p^*,\ S^*(E^*_L + p^*)) = (0.431067, 0.489954, 1.162864).

The exact solution has u∗=0.92745u^* = 0.92745 and p∗=0.30313p^* = 0.30313 and the exact Godunov flux is (0.395391,0.669837,1.154038)(0.395391, 0.669837, 1.154038). The HLL flux with the same speeds is (0.510714,0.543964,1.313264)(0.510714, 0.543964, 1.313264). The HLLC mass flux is closer to the exact one.

Sod's tube. On [0,1][0, 1], outflow boundaries, Courant number 0.9, t=0.2t = 0.2, the L1L^1 density errors against exact cell averages, measured with the code on this page, are:

cells MM steps HLLC HLL (Einfeldt)
100 48 0.01387 0.01505
200 97 0.00919 0.00994
400 194 0.00581 0.00629

The observed orders are 0.59 and 0.66. The gain over HLL comes from the contact.

A moving contact. On a periodic grid of 100 cells, put ρ=1\rho = 1 for 0.25<x<0.750.25 < x < 0.75 and ρ=0.5\rho = 0.5 elsewhere, with u=1u = 1 and p=1p = 1. After 149 steps (t=0.5t = 0.5) the L1L^1 density error is 0.0459 for HLLC and 0.0578 for HLL. In both, uu and pp stay exact to 10−1510^{-15}. A moving contact is still smeared by HLLC, because the scheme averages over a cell each step. Only the face flux is exact.

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, every face flux is (0,1,0)(0, 1, 0) and the solution does not change at all. HLL, on the same data, gives 0.968,0.927,…,0.5390.968, 0.927, \ldots, 0.539 around the contact after 8 steps.

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. The HLL errors are 0.008008,…,0.0010100.008008, \ldots, 0.001010.

Correctness

The proof is in proofs/correctness.md. Theorem: with SL<uLS_L < u_L, uR<SRu_R < S_R and SL<S∗<SRS_L < S^* < S_R, the middle states of lines L4 and L6 are the unique states with a common velocity S∗S^* and a common pressure p∗p^* that satisfy the Rankine-Hugoniot conditions across SLS_L and SRS_R. The jump across S∗S^* also satisfies them, so the average of the two middle states over the fan is the HLL state: the solver is consistent with the integral form of the law. The flux is consistent, F(U,U)=F(U)F(U, U) = F(U), it is continuous across the switch S∗=0S^* = 0, and the scheme is conservative. For data with uL=uRu_L = u_R and pL=pRp_L = p_R, the solver returns the exact flux, and a contact at rest is a steady solution of the scheme. The middle densities are positive. Positivity of the middle pressures is shown by Batten et al. (1997) for these speeds and is not proved on this page.

Proof: The HLLC flux is consistent and conservative, its middle states satisfy the jump conditions, and it resolves an isolated contact exactlyfull

Setting. The system is the 1D Euler equations Ut+F(U)x=0U_t + F(U)_x = 0 with U=(ρ,ρu,E)U = (\rho, \rho u, E), F=(ρu,ρu2+p,u(E+p))F = (\rho u, \rho u^2 + p, u(E + p)) and p=(γ−1)(E−ρu2/2)p = (\gamma - 1)(E - \rho u^2/2). At a face with states ULU_L and URU_R, line L1 gives speeds SL<SRS_L < S_R. If SL≥0S_L \ge 0 or SR≤0S_R \le 0, lines L2 and L3 return the upwind flux. Otherwise lines L4 to L7 use the contact speed

S∗=pR−pL+ρLuL(SL−uL)−ρRuR(SR−uR)ρL(SL−uL)−ρR(SR−uR),S^* = \frac{p_R - p_L + \rho_L u_L(S_L - u_L) - \rho_R u_R(S_R - u_R)}{\rho_L(S_L - u_L) - \rho_R(S_R - u_R)},

the middle states, for K=L,RK = L, R,

UK∗=ρKSK−uKSK−S∗(1, S∗, EKρK+(S∗−uK)(S∗+pKρK(SK−uK))),U^*_K = \rho_K\frac{S_K - u_K}{S_K - S^*}\Big(1,\ S^*,\ \frac{E_K}{\rho_K} + (S^* - u_K)\Big(S^* + \frac{p_K}{\rho_K(S_K - u_K)}\Big)\Big),

and the flux FK∗=FK+SK(UK∗−UK)F^*_K = F_K + S_K(U^*_K - U_K) with K=LK = L if S∗≥0S^* \ge 0 and K=RK = R if S∗<0S^* < 0. The approximate Riemann solution U^(x/t)\hat U(x/t) is ULU_L, UL∗U^*_L, UR∗U^*_R, URU_R on the four intervals cut by SLS_L, S∗S^*, SRS_R. The scheme is 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 L10).

We assume SL<uLS_L < u_L, uR<SRu_R < S_R and SL<S∗<SRS_L < S^* < S_R. The Einfeldt speeds of line L1 give the first two, since SL≤uL−cLS_L \le u_L - c_L and SR≥uR+cRS_R \ge u_R + c_R. Batten, Clarke, Lambert and Causon (1997) discuss the third. The denominator of S∗S^* is then ρL(SL−uL)−ρR(SR−uR)<0\rho_L(S_L - u_L) - \rho_R(S_R - u_R) < 0, so S∗S^* is defined.

Lemma 1 (the middle states are the unique solution of the jump conditions). Look for two middle states UL∗U^*_L, UR∗U^*_R with the same velocity S∗S^* and the same pressure p∗p^*, such that the Rankine-Hugoniot conditions

F(UK∗)−F(UK)=SK(UK∗−UK),K=L,R,F(U^*_K) - F(U_K) = S_K\big(U^*_K - U_K\big), \qquad K = L, R,

hold, where F(UK∗)F(U^*_K) means (ρK∗S∗, ρK∗S∗2+p∗, S∗(EK∗+p∗))(\rho^*_K S^*,\ \rho^*_K S^{*2} + p^*,\ S^*(E^*_K + p^*)). Then S∗S^*, ρK∗\rho^*_K and EK∗E^*_K are the ones above, and

p∗=pK+ρK(SK−uK)(S∗−uK)for both K=L and K=R.p^* = p_K + \rho_K(S_K - u_K)(S^* - u_K) \quad\text{for both } K = L \text{ and } K = R.

Proof. The mass condition is ρK∗S∗−ρKuK=SK(ρK∗−ρK)\rho^*_K S^* - \rho_K u_K = S_K(\rho^*_K - \rho_K), so ρK∗(SK−S∗)=ρK(SK−uK)\rho^*_K(S_K - S^*) = \rho_K(S_K - u_K), which gives ρK∗\rho^*_K. The momentum condition is ρK∗S∗2+p∗−ρKuK2−pK=SK(ρK∗S∗−ρKuK)\rho^*_K S^{*2} + p^* - \rho_K u_K^2 - p_K = S_K(\rho^*_K S^* - \rho_K u_K). Use ρK∗(SK−S∗)=ρK(SK−uK)\rho^*_K(S_K - S^*) = \rho_K(S_K - u_K):

p∗=pK+ρK∗S∗(SK−S∗)−ρKuK(SK−uK)=pK+ρK(SK−uK)(S∗−uK).p^* = p_K + \rho^*_K S^*(S_K - S^*) - \rho_K u_K(S_K - u_K) = p_K + \rho_K(S_K - u_K)(S^* - u_K).

The two values of p∗p^*, for K=LK = L and K=RK = R, must be equal. This is one linear equation in S∗S^*:

pL+ρL(SL−uL)S∗−ρLuL(SL−uL)=pR+ρR(SR−uR)S∗−ρRuR(SR−uR),p_L + \rho_L(S_L - u_L)S^* - \rho_L u_L(S_L - u_L) = p_R + \rho_R(S_R - u_R)S^* - \rho_R u_R(S_R - u_R),

whose solution is the formula for S∗S^*. The energy condition is S∗(EK∗+p∗)−uK(EK+pK)=SK(EK∗−EK)S^*(E^*_K + p^*) - u_K(E_K + p_K) = S_K(E^*_K - E_K), so

EK∗=(SK−uK)EK−pKuK+p∗S∗SK−S∗.E^*_K = \frac{(S_K - u_K)E_K - p_K u_K + p^* S^*}{S_K - S^*}.

Put in p∗S∗=pKS∗+ρK(SK−uK)(S∗−uK)S∗p^* S^* = p_K S^* + \rho_K(S_K - u_K)(S^* - u_K)S^*. The numerator becomes (SK−uK)EK+pK(S∗−uK)+ρK(SK−uK)(S∗−uK)S∗(S_K - u_K)E_K + p_K(S^* - u_K) + \rho_K(S_K - u_K)(S^* - u_K)S^*, and dividing by SK−S∗S_K - S^* gives the third component of UK∗U^*_K above. □\square

The test checks the equal velocity, the equal p∗p^* and the jump conditions on 50 random pairs of states.

Note that p∗p^* is the pressure in the momentum flux, not the pressure that the equation of state gives for UK∗U^*_K. The two differ in general.

Lemma 2 (consistency with the integral form). (S∗−SL)UL∗+(SR−S∗)UR∗=SRUR−SLUL−(FR−FL)(S^* - S_L)U^*_L + (S_R - S^*)U^*_R = S_R U_R - S_L U_L - (F_R - F_L). So the average of U^\hat U over the fan [SL,SR][S_L, S_R] is the HLL state.

Proof. Write FK∗=F(UK∗)F^*_K = F(U^*_K) as in Lemma 1. The jump across S∗S^* also satisfies the Rankine-Hugoniot condition: FR∗−FL∗=(S∗(ρR∗−ρL∗), S∗2(ρR∗−ρL∗), S∗(ER∗−EL∗))=S∗(UR∗−UL∗)F^*_R - F^*_L = \big(S^*(\rho^*_R - \rho^*_L),\ S^{*2}(\rho^*_R - \rho^*_L),\ S^*(E^*_R - E^*_L)\big) = S^*(U^*_R - U^*_L), because the velocity and p∗p^* are the same on both sides. Add the three conditions across SLS_L, S∗S^* and SRS_R:

FR−FL=SL(UL∗−UL)+S∗(UR∗−UL∗)+SR(UR−UR∗).F_R - F_L = S_L(U^*_L - U_L) + S^*(U^*_R - U^*_L) + S_R(U_R - U^*_R).

Collect the terms. □\square

Lemma 3 (the flux). The flux of U^\hat U at x/t=0x/t = 0 is FL∗=FL+SL(UL∗−UL)F^*_L = F_L + S_L(U^*_L - U_L) if SL<0≤S∗S_L < 0 \le S^* and FR∗=FR+SR(UR∗−UR)F^*_R = F_R + S_R(U^*_R - U_R) if S∗<0<SRS^* < 0 < S_R. At S∗=0S^* = 0 the two are equal, so the flux is a continuous function of the data across the switch of line L5.

Proof. The first two statements are the jump conditions of Lemma 1. At S∗=0S^* = 0, the condition across S∗S^* of Lemma 2 gives FR∗−FL∗=0F^*_R - F^*_L = 0. □\square

Theorem 1 (consistency and conservation). Fhllc(U,U)=F(U)F^{hllc}(U, U) = F(U), and 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 formula gives S∗=uS^* = u, since numerator and denominator both reduce to a multiple of ρ(SL−SR)\rho(S_L - S_R): the numerator is ρu(SL−SR)\rho u(S_L - S_R) and the denominator is ρ(SL−SR)\rho(S_L - S_R). Then ρK∗=ρ\rho^*_K = \rho, the velocity is uu, and the energy is EE, so UK∗=UU^*_K = U and the flux is F(U)F(U). In the supersonic cases the flux is F(U)F(U) directly. Conservation: each face flux enters two cells with opposite signs, and the sum telescopes. □\square

Theorem 2 (an isolated contact is exact). Let uL=uR=vu_L = u_R = v and pL=pR=pp_L = p_R = p, with any densities, and SL<v<SRS_L < v < S_R. Then S∗=vS^* = v, UL∗=ULU^*_L = U_L and UR∗=URU^*_R = U_R. So the HLLC flux is F(UL)F(U_L) if v≥0v \ge 0 and F(UR)F(U_R) if v<0v < 0, which is the exact Godunov flux of this Riemann problem. In particular, for v=0v = 0 every face flux of the scheme is (0,p,0)(0, p, 0), and a contact at rest is a steady solution of the scheme.

Proof. With equal uu and pp, the numerator of S∗S^* is v(ρL(SL−v)−ρR(SR−v))v\big(\rho_L(S_L - v) - \rho_R(S_R - v)\big), which is vv times the denominator. So S∗=vS^* = v. Then ρK∗=ρK(SK−v)/(SK−v)=ρK\rho^*_K = \rho_K(S_K - v)/(S_K - v) = \rho_K, the velocity is v=uKv = u_K, and the energy component is ρK(EK/ρK+0)=EK\rho_K\big(E_K/\rho_K + 0\big) = E_K. So UK∗=UKU^*_K = U_K, and Lemma 3 gives FK∗=FKF^*_K = F_K. The exact solution of this Riemann problem is the contact moving at speed vv, and its flux at x=0x = 0 is F(UL)F(U_L) for v>0v > 0 and F(UR)F(U_R) for v<0v < 0. For v=0v = 0 both are (0,p,0)(0, p, 0). In the scheme every face then has the flux (0,p,0)(0, p, 0), so every flux difference is zero. □\square

Theorem 3 (positive density and convex averages). Assume the speeds satisfy the conditions of the setting at every face and λmax⁡∣S∣≤12\lambda \max|S| \le \frac12. Then (1) every middle density ρK∗\rho^*_K is positive, and (2) Ujn+1U_j^{n+1} is the average over cell jj of the approximate solutions U^\hat U of its two faces. So if all the states UjU_j, UL∗U^*_L and UR∗U^*_R have positive density and pressure, so does Ujn+1U_j^{n+1}.

Proof. (1) ρL∗=ρL(SL−uL)/(SL−S∗)\rho^*_L = \rho_L(S_L - u_L)/(S_L - S^*) is a ratio of two negative numbers, and ρR∗=ρR(SR−uR)/(SR−S∗)\rho^*_R = \rho_R(S_R - u_R)/(S_R - S^*) is a ratio of two positive numbers. (2) With λmax⁡∣S∣≤12\lambda\max|S| \le \frac12 the fans of two faces do not meet inside a cell. By Lemma 3 and the jump conditions, the integral of U^\hat U over the part of each fan inside cell jj changes Δx Uj\Delta x\, U_j by exactly −Δt-\Delta t times the flux difference across that part. Summing the two parts gives ∫CjU^ dx=Δx Uj−Δt(Fj+1/2−Fj−1/2)\int_{C_j}\hat U\,dx = \Delta x\,U_j - \Delta t(F_{j+1/2} - F_{j-1/2}). The set of states with positive density and pressure is convex (the pressure is a concave function of UU for ρ>0\rho > 0, see Lemma 5 of the proof on the HLL page), so an average of such states is such a state. □\square

Remark. Theorem 3 does not prove that the pressures of UL∗U^*_L and UR∗U^*_R are positive. Batten, Clarke, Lambert and Causon (1997) show that HLLC with the Einfeldt speeds is positively conservative. The proof here does not repeat their argument, and the test checks positivity only on the 123 problem. Unlike HLL, the HLLC middle states are not averages of the exact solution, so the cell entropy inequality of the HLL proof does not carry over.

Complexity

Theorem, in the real-RAM model: one HLLC flux costs O(m)O(m) operations and a constant number of square roots for the speeds, a few more operations than HLL. One step costs O(mM)O(mM) 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(mM2)O(mM^2).

Properties

  • Keeps contacts and shear waves. A contact at rest is exact. In 2D and 3D the transverse velocity is carried by the two middle states like the density, so a shear layer at rest is also kept.
  • Upwind for supersonic flow. For SL≥0S_L \ge 0 or SR≤0S_R \le 0 it returns F(UL)F(U_L) or F(UR)F(U_R).
  • Positive in practice. 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.0186 and the smallest pressure is 0.0069. The exact density at the center is 0.0219.
  • First order. The measured order on the smooth wave is 0.99 to 1.01.
  • Not entropy stable by proof. The middle states are not averages of the exact solution, so the entropy argument of HLL does not apply.

Variants and improvements

  • Other speed estimates. Davis speeds or pressure-based speeds (Toro 2009, Chapter 10) can replace the Einfeldt speeds of line L1.
  • Low Mach corrections. At low Mach number the dissipation on the acoustic waves is too large. Rescaled forms reduce it.
  • Hybrids for the carbuncle. HLLC, like Roe, can give the carbuncle at strong shocks aligned with the grid. Codes switch to HLL in the direction normal to such a shock.
  • HLLD and multi-state solvers. For magnetohydrodynamics, more middle states give the HLLD solver.
  • High order. Use the flux on MUSCL or WENO values with a Runge-Kutta method.

When to use it

Use HLLC as the default flux for compressible gas dynamics: it keeps contacts and shear layers like Roe, it needs no eigenvectors and no entropy fix, and it is robust near vacuum. Use HLL instead at strong grid-aligned shocks where the carbuncle appears, or for systems where the middle wave structure is not known.

Pitfalls

  • The pressure of the middle state. The pressure p∗p^* in the flux is not the pressure that the equation of state gives for UK∗U^*_K. Do not recompute it from UK∗U^*_K.
  • The sign of the denominator. The denominator of S∗S^* is negative when SL<uLS_L < u_L and SR>uRS_R > u_R. Estimates that break these conditions can make it zero.
  • Choosing KK. At S∗=0S^* = 0 both choices give the same flux (Lemma 3 of the proof), so the switch S∗≥0S^* \ge 0 is safe. A test on uLu_L or uRu_R in place of S∗S^* is wrong.
  • Expecting a sharp moving contact. The face flux of an isolated contact is exact, but a moving contact still smears at first order because of the cell averages. Only a contact at rest stays sharp.

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.

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

"""HLLC approximate Riemann solver for the 1D Euler equations (Toro, Spruce and Speares 1994).

Two middle states, separated by a contact at speed S*, replace the one HLL state.
The outer speeds are the estimates of Einfeldt (1988) with Roe averages.
"""

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):
    """Einfeldt estimates (S_L, S_R) with the Roe averages of u and H."""
    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)
    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)
    c = sqrt((g - 1.0) * (h - 0.5 * u * u))
    return min(ul - cl, u - c), max(ur + cr, u + c)


def contact_speed(ql, qr, sl, sr, g=GAMMA):
    """S* from the jump conditions across S_L and S_R with equal middle pressure and velocity."""
    rl, ul, pl = cons_to_prim(ql, g)
    rr, ur, pr = cons_to_prim(qr, g)
    return (pr - pl + rl * ul * (sl - ul) - rr * ur * (sr - ur)) / (rl * (sl - ul) - rr * (sr - ur))


def star_state(q, s, ss, g=GAMMA):
    """The middle state U*_K next to the outer wave of speed s = S_K, for the contact speed ss = S*."""
    rho, u, p = cons_to_prim(q, g)
    r = rho * (s - u) / (s - ss)
    return (r, r * ss, r * (q[2] / rho + (ss - u) * (ss + p / (rho * (s - u)))))


def hllc_flux(ql, qr, g=GAMMA):
    """The HLLC flux at the face between the conserved states ql and qr."""
    sl, sr = wave_speeds(ql, qr, g)  # @line L1
    if sl >= 0.0:  # @line L2
        return euler_flux(ql, g)
    if sr <= 0.0:  # @line L3
        return euler_flux(qr, g)
    ss = contact_speed(ql, qr, sl, sr, g)  # @line L4
    if ss >= 0.0:  # @line L5
        q, s = ql, sl
    else:
        q, s = qr, sr
    st = star_state(q, s, ss, g)  # @line L6
    return tuple(f + s * (a - b) for f, a, b in zip(euler_flux(q, g), st, q))  # @line L7


def step(cells, dt, dx, flux, periodic=False):
    """One forward Euler step of the conservative scheme with one ghost cell on each side."""
    ext = ([cells[-1]] + list(cells) + [cells[0]]) if periodic else ([cells[0]] + list(cells) + [cells[-1]])  # @line L8
    faces = [flux(ext[j], ext[j + 1]) for j in range(len(ext) - 1)]  # @line L9
    lam = dt / dx
    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))]  # @line L10


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: hllc_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_hllc_riemann_solver.py 181 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hllc-riemann-solver/. The authors are listed in prov/HQEZ7E.jsonl.

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

from hllc_riemann_solver import (
    cons_to_prim,
    contact_speed,
    euler_flux,
    hllc_flux,
    prim_to_cons,
    solve_euler,
    star_state,
    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


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 random_state(rng):
    return prim_to_cons((rng.uniform(0.1, 2), rng.uniform(-1, 1), rng.uniform(0.1, 2)))


def test_flux_at_the_sod_interface_by_hand() -> None:
    ql, qr = prim_to_cons(SOD[0]), prim_to_cons(SOD[1])
    sl, sr = wave_speeds(ql, qr)
    ss = contact_speed(ql, qr, sl, sr)
    assert abs(ss - (0.1 - 1.0) / (sl - 0.125 * sr)) < 1e-14  # both velocities are 0
    st = star_state(ql, sl, ss)
    assert abs(st[0] - sl / (sl - ss)) < 1e-14
    f = hllc_flux(ql, qr)
    assert abs(f[0] - st[0] * ss) < 1e-14  # the mass flux through the face is rho*_L S*
    assert all(abs(a - (b + sl * (c - d))) < 1e-14 for a, b, c, d in zip(f, euler_flux(ql), st, ql))
    assert abs(ss - 0.678118) < 1e-6
    assert abs(f[0] - 0.431067) < 1e-6 and abs(f[1] - 0.489954) < 1e-6 and abs(f[2] - 1.162864) < 1e-6


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


def test_middle_states_have_equal_pressure_and_velocity_and_match_the_hll_average() -> None:
    rng = random.Random(3)
    for _ in range(50):
        ql, qr = random_state(rng), random_state(rng)
        sl, sr = wave_speeds(ql, qr)
        ss = contact_speed(ql, qr, sl, sr)
        a, b = star_state(ql, sl, ss), star_state(qr, sr, ss)
        assert abs(a[1] / a[0] - ss) < 1e-12 and abs(b[1] / b[0] - ss) < 1e-12
        (rl, ul, pl), (rr, ur, pr) = cons_to_prim(ql), cons_to_prim(qr)
        p_left = pl + rl * (sl - ul) * (ss - ul)  # the middle pressure from the momentum jump condition
        p_right = pr + rr * (sr - ur) * (ss - ur)
        assert abs(p_left - p_right) < 1e-12 * (1 + abs(p_left))
        fl, fr = euler_flux(ql), euler_flux(qr)
        for q, s, st in ((ql, sl, a), (qr, sr, b)):  # the jump conditions across S_L and S_R
            fs = (st[1], st[1] * ss + p_left, ss * (st[2] + p_left))
            assert all(abs((fs[i] - euler_flux(q)[i]) - s * (st[i] - q[i])) < 1e-10 for i in range(3))
        for i in range(3):  # (S* - S_L) U*_L + (S_R - S*) U*_R = S_R U_R - S_L U_L - (F_R - F_L)
            lhs = (ss - sl) * a[i] + (sr - ss) * b[i]
            rhs = sr * qr[i] - sl * ql[i] - (fr[i] - fl[i])
            assert abs(lhs - rhs) < 1e-10 * (1 + abs(rhs))


def test_an_isolated_contact_gives_the_exact_flux() -> None:
    for u in (0.3, -0.3):
        ql, qr = prim_to_cons((1.0, u, 1.0)), prim_to_cons((0.5, u, 1.0))
        want = euler_flux(ql) if u > 0 else euler_flux(qr)
        assert all(abs(a - b) < 1e-13 for a, b in zip(hllc_flux(ql, qr), want))


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_moving_contact_keeps_u_and_p() -> None:
    m = 100
    cells = [prim_to_cons((1.0 if 0.25 < (j + 0.5) / m < 0.75 else 0.5, 1.0, 1.0)) for j in range(m)]
    cells, _ = solve_euler(cells, 1.0 / m, 0.5, periodic=True)
    exact = [0.5 if 0.25 < ((j + 0.5) / m) % 1.0 < 0.75 else 1.0 for j in range(m)]  # moved by 0.5
    err = sum(abs(q[0] - e) for q, e in zip(cells, exact)) / m
    assert err < 0.05
    assert all(abs(cons_to_prim(q)[1] - 1.0) < 1e-13 and abs(cons_to_prim(q)[2] - 1.0) < 1e-13 for q in cells)


def test_sod_l1_error_decreases() -> None:
    e100, e200 = sod_l1(100), sod_l1(200)
    assert e200 < 0.0095 and e100 < 0.0142
    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_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.01 and min(x[2] for x in w) > 0.0

History

Harten, Lax and van Leer (1983) noted that a solver with one middle state loses the contact and suggested that more states would help. Toro, Spruce and Speares (1994) built the solver with two middle states for the Euler equations and named it HLLC. Batten, Clarke, Lambert and Causon (1997) studied the choice of the outer speeds and of S∗S^*, and showed positivity with the Einfeldt speeds. The solver is described in detail in Toro (2009, Chapter 10).

References

  1. E. F. Toro, M. Spruce and W. Speares, Restoration of the contact surface in the HLL-Riemann solver, Shock Waves 4(1) (1994) 25-34, the two middle states, the contact speed and the flux; doi:10.1007/BF01414629
  2. P. Batten, N. Clarke, C. Lambert and D. M. Causon, On the choice of wavespeeds for the HLLC Riemann solver, SIAM Journal on Scientific Computing 18(6) (1997) 1553-1570, the speed estimates, the contact speed and positivity of HLLC; doi:10.1137/S1064827593260140
  3. Eleuterio F. Toro, Riemann Solvers and Numerical Methods for Fluid Dynamics, third edition, Springer, 2009, Chapter 10 (the HLLC solver for the Euler equations, the middle states and the wave speed estimates)
  4. 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, approximate Riemann solvers consistent with the integral form of the conservation law; doi:10.1137/1025002
  5. Bernd Einfeldt, On Godunov-type methods for gas dynamics, SIAM Journal on Numerical Analysis 25(2) (1988) 294-318, the outer speed estimates with Roe averages; doi:10.1137/0725021

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