Algorithmadvanced
Multiple-relaxation-time lattice Boltzmann method
Also called MRT lattice Boltzmann, MRT collision operator.
Lattice Boltzmann with collision in moment space, where each moment relaxes at its own rate. It is often more stable than BGK at low viscosity, and the free rates can place bounce-back walls exactly.
- Solves
- Navier-Stokes equations
- Time
- Θ(K M) worst
- Space
- Θ(M) extra
- Where
- K = the number of time steps; M = the number of lattice nodes
- Model
- Real RAM
- Properties
- conserves mass exactlyconserves momentum exactlydeterministicexplicitlocal collision
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Dominique d'Humières, Pierre Lallemand, Li Shi Luo, 1992
The multiple-relaxation-time (MRT) lattice Boltzmann method is the lattice Boltzmann method with a different collision step. The BGK collision operator relaxes all nine populations of a D2Q9 node toward equilibrium at one rate . MRT first maps the populations to nine moments (density, energy, momentum, heat flux, stress), relaxes each moment at its own rate, and maps back. The shear rate fixes the viscosity as in BGK, and the other rates are free. The streaming step and the bounce-back walls are the same as in BGK.
In plain words
Each lattice node carries nine numbers, one for each direction a particle can move. A collision pushes these numbers toward a target that depends only on the local density and velocity. BGK pushes all nine with the same strength. MRT first rewrites the nine numbers as nine physical quantities: mass, momentum, energy, stress and two more. It then pushes each quantity with its own strength. Mass and momentum are not changed. The stress decides the viscosity. The other quantities have no physical role in slow flow, so you can choose their strengths to damp noise or to make walls sit exactly where you want them. If every strength is the same, MRT is exactly BGK.
The problem
Solve the weakly compressible Navier-Stokes equations on a lattice, as the lattice Boltzmann method does, but with a collision that has more than one free parameter. With BGK the single time sets the viscosity and also every other property of the scheme: its stability at small , the decay of nonhydrodynamic modes, and the position of bounce-back walls. MRT separates these. Units are lattice units: grid spacing 1, time step 1, .
The algorithm
The populations use the velocities , , , , , , , , , in the same order as the lattice Boltzmann page. The moments are with the matrix of Lallemand and Luo (2000):
| moment | row of | equilibrium | rate |
|---|---|---|---|
| (density) | any | ||
| (energy) | |||
| (energy squared) | |||
| (momentum) | any | ||
| (heat flux) | |||
| any | |||
| (normal stress) | |||
| (shear stress) |
The rows are orthogonal, so with . is the diagonal matrix of the rates. is Guo's force term for a body force per unit volume.
MRT-D2Q9(n_x, n_y, S, K, init, g, walls)- 1
for each node x: (ρ, u) ← init(x); f(x) ← f^eq(ρ, u) - 2
repeat K times - 3
for each node x: ρ ← Σ f_i, u ← (Σ c_i f_i + g/2) / ρ - 4
m ← M f - 5
m* ← m - S (m - m^eq(ρ, u)) + (I - S/2) M F(u, g) - 6
f* ← M^-1 m* - 7
for each node x and each i: f_i(x + c_i) ← f*_i(x); if x + c_i is beyond a wall, f_ī(x) ← f*_i(x) - 8
return ρ, u at every node
Line L3 computes the density and the velocity, with the half force shift of Guo's scheme. Line L4 maps the populations to moments; with the integer rows of this is a few additions and no multiplications. Line L5 is the collision: the conserved moments , and equal their equilibrium (up to the force shift), so their rates have no effect, and each other moment moves toward its equilibrium by the fraction . Line L6 maps back. Line L7 is the streaming step of the lattice Boltzmann method, periodic in , with halfway bounce-back walls at and when walls are on ( is the index with ).
The rates must lie in . The shear rate sets the kinematic viscosity
so plays the role of . The rate sets the bulk viscosity. The rate of the heat flux moments has no effect on the Navier-Stokes limit, but it changes the error terms, in particular at walls.
Example
One collision by hand. Take a node at rest with , and add to and to (two populations along ). The moments are , , , , and the others are 0. The velocity is 0, so the equilibrium moments are , and . Choose , (so ) and . Line L5 gives:
| moment | rate | |||
|---|---|---|---|---|
| any | ||||
| 1 | ||||
| 1 | ||||
| 0 | ||||
| others | 0 | 0 | 0 |
The energy moments reach their equilibrium in one step, and the stress overshoots to , as any rate above 1 does. Line L6 gives . BGK with (the same viscosity) gives : the same density and the same stress , but the energy moments also overshoot, to and , because they relax at instead of 1. The test file checks the MRT fractions.
Poiseuille flow. A channel of nodes, one node wide and periodic in , with halfway bounce-back walls and a force along , run to steady state. The exact profile is . With (all nonconserved rates equal, which is BGK) the computed profile is the parabola plus a constant slip. With chosen so that , the slip goes away:
| () | (0.6) | (0.8) | 1 (1.0) |
|---|---|---|---|
| slip with | |||
| for | |||
| max error with that |
The BGK slips match the formula of the lattice Boltzmann page. The remaining errors with are the convergence tolerance of the run, not a discretization error. These runs used . This is a measurement; the result that the wall position depends on the rates only through , and that puts it exactly halfway for this flow, is from the analysis of Ginzburg and d'Humières (2003) for two-rate schemes.
Taylor-Green vortex. On an periodic lattice with , , and steps, the relative max error of against was:
| observed orders | ||||
|---|---|---|---|---|
| (BGK) | 8.64e-2 | 2.11e-2 | 5.26e-3 | 2.03, 2.01 |
| 1.1 | 7.96e-2 | 1.95e-2 | 4.84e-3 | 2.03, 2.01 |
| () | 6.63e-2 | 1.62e-2 | 4.02e-3 | 2.03, 2.01 |
The order is 2 for every ; the rate changes only the constant.
Correctness
Proved in the correctness file, in exact arithmetic: the rows of are orthogonal; equals the closed-form of the table; the collision of lines L3 to L6 keeps the density of each node and adds exactly to its momentum, for any rates of the conserved moments; with every rate equal to the collision is BGK with Guo forcing, term by term; and streaming permutes the populations, so the total mass is constant, and on a fully periodic lattice the total momentum grows by exactly per step. The tests check all of this with fractions.Fraction, and check the BGK reduction against an independent BGK collision on 500 random nodes.
The link to the Navier-Stokes equations with is a consistency result from the Chapman-Enskog expansion (Lallemand and Luo 2000). It holds for small Mach and Knudsen numbers, and it is not proved here. The Taylor-Green table shows the second order in space that it predicts.
Proof: The MRT collision conserves mass and momentum, and equal rates give BGKfull
We use the D2Q9 velocities , , , , , , , , and the weights , , . The rows of are, in order, , , , , , , , and , as in the implementation. We claim four things, all in exact arithmetic:
- is invertible and with .
- , the closed form used on line L5.
- Lines L3 to L6 keep the density of a node and add exactly to its momentum , whatever the rates of the conserved moments are. If every rate equals , lines L3 to L6 give the same as BGK collision with Guo forcing.
- Line L7 keeps the total mass. On a lattice that is periodic in both directions, it also keeps the total momentum. So the total mass is constant over a run, and on a periodic lattice the total momentum grows by exactly per step.
Claim 1. Each row of has integer entries, and a direct computation of the 36 products of pairs of distinct rows gives 0 (the test test_rows_of_m_are_orthogonal does all 81 products). For example, the rows and give . The squared lengths of the rows are the entries of . So , and .
Claim 2. Write . Over the four axis velocities, and ; over the four diagonal velocities, and . So the rest, axis and diagonal sums are
The rows , and take the values , and on the rest, axis and diagonal groups. Hence -row: ; ; .
For the odd rows only the term survives, because the even terms are equal for and . The axis part is , and the diagonal part is . So and . The rows follow by symmetry. For the stress rows only the term survives: , and . This is the closed form .
Claim 3. Let . The lattice sums , , and give and . So the , and entries of are , and .
Line L3 sets and , where . By Claim 2, and . With any rate on the row, line L5 gives
and in the same way and for any rate on the row. Line L6 inverts exactly by Claim 1, so and .
If , line L5 reads . By Claim 2, , so line L6 gives , which is BGK collision with Guo forcing, term by term.
Claim 4. Line L7 moves the value into one slot of the new array: slot if is inside the lattice (with taken modulo , and modulo when there are no walls), else slot with . We show that this map from old slots to new slots is a bijection. Take a new slot . If is inside, the only old slot that maps to it is , because a bounced value at would need to lie beyond a wall. If lies beyond a wall, then does not exist, and the only old slot that maps to is , whose target is beyond the wall. So every new slot gets exactly one value, and streaming permutes the values. The total mass is therefore the same before and after line L7. Without walls the permutation keeps the index of every value, so is also kept.
Together with Claim 3, each step keeps the total mass exactly, and on a fully periodic lattice each step adds at each of the nodes and nothing else to the total momentum. The walls take momentum out (they exert the wall shear force), so with walls only the mass is conserved.
These are exact statements about the update. The claim that the scheme approximates the Navier-Stokes equations with is a consistency result from the Chapman-Enskog expansion (Lallemand and Luo 2000), not part of this proof.
Complexity
In the real-RAM model with lattice nodes (in this section is the node count, not the moment matrix), one step does a constant amount of work per node. Line L4 is a fixed set of about 30 additions and a few doublings, line L5 is 9 multiply-adds, plus the 9 force moments when , line L6 is about 30 additions and 9 divisions by constants, and line L7 moves 9 values. That is a few times the work of a BGK collision, and per step. steps cost . The lattice stores 9 populations per node, plus a second array for streaming, so the memory is . Reaching a physical time with grid spacing in diffusive scaling takes steps, as for BGK.
Properties
- Exact conservation. Mass is conserved exactly by every step, and momentum changes only by the force and by the walls.
- Contains BGK. With the method is BGK, so every BGK result applies as a special case.
- Free rates. sets the bulk viscosity, and act only on nonhydrodynamic moments. Tuning them changes stability and wall errors, not the viscosity.
- Galilean invariance and isotropy. As for D2Q9 BGK, these hold only to second order in ; Lallemand and Luo (2000) analyse the dispersion and the isotropy of the D2Q9 MRT model.
- Second order in space for smooth flow, as measured above.
Variants and improvements
- Two-relaxation-time (TRT). Ginzburg's TRT scheme uses one rate for all even moments and one for all odd moments. It is the simplest form of MRT that keeps the free parameter , and is the choice that puts halfway bounce-back walls exactly halfway for Poiseuille flow (Ginzburg and d'Humières 2003).
- Other bases. The basis can use raw moments, central moments (moments in the frame moving with ) or cumulants. Central moment and cumulant schemes reduce the Galilean errors at high speed.
- Three dimensions. d'Humières et al. (2002) give D3Q15 and D3Q19 MRT models.
- Turbulence. An eddy viscosity from a large eddy simulation model enters by changing at each node.
- Regularized and entropic schemes are other ways to make the collision more stable at small viscosity.
When to use it
Use MRT (or TRT) instead of BGK when the viscosity is small and BGK becomes unstable, when bounce-back walls must sit at a position that does not change with the viscosity (porous media, permeability computations), or when the bulk viscosity must differ from the shear viscosity. For smooth periodic flow at moderate viscosity, BGK is simpler and gives the same accuracy. MRT does not make the lattice Boltzmann method suitable for compressible flow or high Mach numbers.
Pitfalls
- A basis that is not the one of the equilibrium table. The closed forms of hold only for the rows of in the table above, in the velocity order above. With another velocity numbering, permute the columns of too, and check before the first run.
- Using as the inverse. The rows are orthogonal but not normalized, so line L6 needs , not .
- The force term in population space. Adding after line L6 with the BGK factor is wrong when the rates differ; the factor must act in moment space on line L5.
- Velocity without the half shift. Line L3 must use . Without the shift the scheme is first order in the force.
- Expecting exact walls for free. With the wall slip of BGK remains. The wall position is exact only for the right , and only for flows like Poiseuille flow whose profile is at most quadratic.
- Rates outside . A rate of 2 or more makes the moment grow instead of decay.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/multiple-relaxation-time-lattice-boltzmann/. The authors are listed in prov/JZ1BG8.jsonl.
"""The multiple-relaxation-time (MRT) lattice Boltzmann method on D2Q9.
Lattice units: grid spacing 1, time step 1, c_s^2 = 1/3. The collision
works on the nine moments m = M f of the populations (the moment basis of
Lallemand and Luo 2000). Moment k relaxes toward its equilibrium with its
own rate s_k:
m* = m - S (m - m_eq) + (I - S/2) M F, f* = M^-1 m*,
where F is Guo's force term. The rates of the conserved moments (density
and momentum) do not matter, because m = m_eq for them. The shear rate
s_nu gives the kinematic viscosity nu = (1/s_nu - 1/2)/3. With every rate
equal to 1/tau the scheme is exactly the BGK scheme.
The x direction is periodic. In y the lattice is periodic, or it has two
walls halfway between nodes, at y = -1/2 and y = ny - 1/2 (halfway
bounce-back). Pass Fraction rates, forces and initial data with
exact=True to run in exact rational arithmetic.
"""
from dataclasses import dataclass
from fractions import Fraction
from typing import Any, Callable, List, Optional, Sequence, Tuple
C = [(0, 0), (1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (-1, -1), (1, -1)]
OPP = [0, 3, 4, 1, 2, 7, 8, 5, 6]
W_EXACT = [Fraction(4, 9)] + [Fraction(1, 9)] * 4 + [Fraction(1, 36)] * 4
W_FLOAT = [float(w) for w in W_EXACT]
# Rows: rho, e, epsilon, j_x, q_x, j_y, q_y, p_xx, p_xy (Lallemand and Luo 2000).
M = [
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[-4, -1, -1, -1, -1, 2, 2, 2, 2],
[4, -2, -2, -2, -2, 1, 1, 1, 1],
[0, 1, 0, -1, 0, 1, -1, -1, 1],
[0, -2, 0, 2, 0, 1, -1, -1, 1],
[0, 0, 1, 0, -1, 1, 1, -1, -1],
[0, 0, -2, 0, 2, 1, 1, -1, -1],
[0, 1, -1, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, -1, 1, -1],
]
# The rows of M are orthogonal: M M^T = diag(NORM2), so M^-1 = M^T diag(1/NORM2).
NORM2 = [9, 36, 36, 6, 12, 6, 12, 4, 4]
@dataclass
class MRTResult:
"""Density and velocity fields indexed [y][x], and the populations f[y * nx + x][i]."""
rho: List[List[Any]]
ux: List[List[Any]]
uy: List[List[Any]]
steps: int
f: List[List[Any]]
def nu_of_rate(s_nu: Any) -> Any:
"""Kinematic viscosity of the shear rate: nu = (1/s_nu - 1/2)/3."""
return (1 / s_nu - Fraction(1, 2)) / 3 if isinstance(s_nu, Fraction) else (1.0 / s_nu - 0.5) / 3.0
def magic_q_rate(s_nu: Any, magic: Any = Fraction(3, 16)) -> Any:
"""The rate s_q that gives (1/s_nu - 1/2)(1/s_q - 1/2) = magic."""
half = Fraction(1, 2) if isinstance(s_nu, Fraction) else 0.5
return 1 / (magic / (1 / s_nu - half) + half)
def rates(s_nu: Any, s_q: Any, s_e: Any = None, s_eps: Any = None) -> List[Any]:
"""The diagonal of S in the order of the rows of M. s_e and s_eps default to s_nu."""
s_e = s_nu if s_e is None else s_e
s_eps = s_nu if s_eps is None else s_eps
zero = 0 * s_nu
return [zero, s_e, s_eps, zero, s_q, zero, s_q, s_nu, s_nu]
def equilibrium(rho: Any, ux: Any, uy: Any, exact: bool = False) -> List[Any]:
"""D2Q9 equilibrium populations, second order in u."""
w = W_EXACT if exact else W_FLOAT
usq = ux * ux + uy * uy
out = []
for i in range(9):
cu = C[i][0] * ux + C[i][1] * uy
out.append(w[i] * rho * (1 + 3 * cu + Fraction(9, 2) * cu * cu - Fraction(3, 2) * usq)
if exact else w[i] * rho * (1 + 3 * cu + 4.5 * cu * cu - 1.5 * usq))
return out
def equilibrium_moments(rho: Any, ux: Any, uy: Any) -> List[Any]:
"""m_eq = M f_eq in closed form."""
jx, jy = rho * ux, rho * uy
usq = ux * ux + uy * uy
return [rho, -2 * rho + 3 * rho * usq, rho - 3 * rho * usq, jx, -jx, jy, -jy,
rho * (ux * ux - uy * uy), rho * ux * uy]
def guo_force(ux: Any, uy: Any, gx: Any, gy: Any, exact: bool = False) -> List[Any]:
"""Guo's force populations F_i = w_i [3 (c_i - u) + 9 (c_i . u) c_i] . g."""
w = W_EXACT if exact else W_FLOAT
out = []
for i in range(9):
cx, cy = C[i]
cu = cx * ux + cy * uy
out.append(w[i] * ((3 * (cx - ux) + 9 * cu * cx) * gx + (3 * (cy - uy) + 9 * cu * cy) * gy))
return out
def to_moments(f: Sequence[Any]) -> List[Any]:
"""m = M f, written out row by row."""
f0, f1, f2, f3, f4, f5, f6, f7, f8 = f
axes = f1 + f2 + f3 + f4
diag = f5 + f6 + f7 + f8
dx = f5 - f6 - f7 + f8
dy = f5 + f6 - f7 - f8
return [f0 + axes + diag, -4 * f0 - axes + 2 * diag, 4 * f0 - 2 * axes + diag,
f1 - f3 + dx, 2 * (f3 - f1) + dx, f2 - f4 + dy, 2 * (f4 - f2) + dy,
f1 - f2 + f3 - f4, f5 - f6 + f7 - f8]
def from_moments(m: Sequence[Any]) -> List[Any]:
"""f = M^-1 m = M^T diag(1/NORM2) m, written out column by column."""
r, e, ep, jx, qx, jy, qy, pxx, pxy = (m[k] / NORM2[k] for k in range(9))
a = r - e - 2 * ep
d = r + 2 * e + ep
return [r - 4 * e + 4 * ep,
a + jx - 2 * qx + pxx, a + jy - 2 * qy - pxx,
a - jx + 2 * qx + pxx, a - jy + 2 * qy - pxx,
d + jx + qx + jy + qy + pxy, d - jx - qx + jy + qy - pxy,
d - jx - qx - jy - qy + pxy, d + jx + qx - jy - qy - pxy]
def macros(f: Sequence[Any], gx: Any, gy: Any, half: Any) -> Tuple[Any, Any, Any]:
"""Density and velocity with the half force shift: u = (sum c_i f_i + g/2)/rho."""
rho = sum(f)
jx = sum(C[i][0] * f[i] for i in range(9))
jy = sum(C[i][1] * f[i] for i in range(9))
return rho, (jx + half * gx) / rho, (jy + half * gy) / rho
def collide(f: Sequence[Any], s: Sequence[Any], gx: Any = 0, gy: Any = 0,
exact: bool = False) -> List[Any]:
"""One MRT collision at one node: lines L3 to L6."""
half = Fraction(1, 2) if exact else 0.5
rho, ux, uy = macros(f, gx, gy, half) # @line L3
m = to_moments(f) # @line L4
meq = equilibrium_moments(rho, ux, uy)
if gx or gy:
mf = to_moments(guo_force(ux, uy, gx, gy, exact))
else:
mf = [0] * 9
ms = [m[k] - s[k] * (m[k] - meq[k]) + (1 - half * s[k]) * mf[k] # @line L5
for k in range(9)]
return from_moments(ms) # @line L6
def mrt_d2q9(nx: int, ny: int, s: Sequence[Any], steps: int,
init: Callable[[int, int], Tuple[Any, Any, Any]],
gx: Any = 0, gy: Any = 0, walls: bool = False, exact: bool = False,
f0: Optional[List[List[Any]]] = None) -> MRTResult:
"""Run the MRT lattice Boltzmann method on an nx by ny lattice.
s is the diagonal of S (see rates). init(x, y) returns (rho, ux, uy);
the populations start at equilibrium unless f0 is given. Precondition:
0 < s_k < 2 for the nonconserved moments, nx >= 1, ny >= 2 with walls.
Cost: Theta(steps * nx * ny) node updates, each a constant number of
operations (two 9 by 9 products with the fixed matrix M).
"""
half = Fraction(1, 2) if exact else 0.5
n = nx * ny
if f0 is not None:
f = [list(p) for p in f0]
else:
f = []
for y in range(ny): # @line L1
for x in range(nx):
rho, ux, uy = init(x, y)
f.append(equilibrium(rho, ux, uy, exact))
for _ in range(steps): # @line L2
post = [collide(f[k], s, gx, gy, exact) for k in range(n)]
new = [[0] * 9 for _ in range(n)]
for y in range(ny): # @line L7
for x in range(nx):
p = post[x + nx * y]
for i in range(9):
xt = (x + C[i][0]) % nx
yt = y + C[i][1]
if walls and (yt < 0 or yt >= ny):
new[x + nx * y][OPP[i]] = p[i]
continue
new[xt + nx * (yt % ny)][i] = p[i]
f = new
rho_g, ux_g, uy_g = [], [], []
for y in range(ny): # @line L8
rr, uu, vv = [], [], []
for x in range(nx):
r, u, v = macros(f[x + nx * y], gx, gy, half)
rr.append(r)
uu.append(u)
vv.append(v)
rho_g.append(rr)
ux_g.append(uu)
uy_g.append(vv)
return MRTResult(rho_g, ux_g, uy_g, steps, f)
test_multiple_relaxation_time_lattice_boltzmann.py 192 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/multiple-relaxation-time-lattice-boltzmann/. The authors are listed in prov/JZ1BG8.jsonl.
import math
import random
from fractions import Fraction as Fr
from multiple_relaxation_time_lattice_boltzmann import (
C, M, NORM2, W_EXACT, collide, equilibrium, equilibrium_moments, from_moments,
guo_force, magic_q_rate, mrt_d2q9, nu_of_rate, rates, to_moments)
def rand_fr(rng, lo=-50, hi=50, den=100):
return Fr(rng.randint(lo, hi), den)
def rand_pops(rng):
return [w * (1 + rand_fr(rng, -20, 20)) for w in W_EXACT]
# ---------------------------------------------------------------- the moment basis
def test_rows_of_m_are_orthogonal():
for a in range(9):
for b in range(9):
d = sum(M[a][i] * M[b][i] for i in range(9))
assert d == (NORM2[a] if a == b else 0)
def test_transform_matches_matrix_and_inverts_exactly():
rng = random.Random(1)
for _ in range(500):
f = [rand_fr(rng) for _ in range(9)]
m = to_moments(f)
assert m == [sum(M[k][i] * f[i] for i in range(9)) for k in range(9)]
assert from_moments(m) == f
def test_equilibrium_moments_closed_form():
rng = random.Random(2)
for _ in range(500):
rho = 1 + rand_fr(rng, -30, 30)
ux, uy = rand_fr(rng, -20, 20), rand_fr(rng, -20, 20)
assert to_moments(equilibrium(rho, ux, uy, True)) == equilibrium_moments(rho, ux, uy)
def test_hand_example_equilibrium_moments():
# rho = 1, u = (1/10, 0): e = -2 + 3/100, epsilon = 1 - 3/100, p_xx = 1/100
m = equilibrium_moments(Fr(1), Fr(1, 10), Fr(0))
assert m == [1, Fr(-197, 100), Fr(97, 100), Fr(1, 10), Fr(-1, 10), 0, 0, Fr(1, 100), 0]
# ---------------------------------------------------------------- collision
def bgk_collide(f, tau, gx, gy):
"""Independent oracle: BGK with Guo forcing in population space."""
rho = sum(f)
ux = (sum(C[i][0] * f[i] for i in range(9)) + gx / 2) / rho
uy = (sum(C[i][1] * f[i] for i in range(9)) + gy / 2) / rho
feq = equilibrium(rho, ux, uy, True)
out = []
for i in range(9):
cx, cy = C[i]
cu = cx * ux + cy * uy
force = W_EXACT[i] * ((3 * (cx - ux) + 9 * cu * cx) * gx + (3 * (cy - uy) + 9 * cu * cy) * gy)
out.append(f[i] - (f[i] - feq[i]) / tau + (1 - 1 / (2 * tau)) * force)
return out
def test_equal_rates_give_bgk_exactly():
rng = random.Random(3)
for _ in range(500):
f = rand_pops(rng)
tau = Fr(rng.randint(51, 300), 100)
gx, gy = rand_fr(rng, -10, 10, 1000), rand_fr(rng, -10, 10, 1000)
s = [1 / tau] * 9
assert collide(f, s, gx, gy, exact=True) == bgk_collide(f, tau, gx, gy)
def test_conserved_rates_do_not_matter():
rng = random.Random(4)
for _ in range(200):
f = rand_pops(rng)
gx, gy = rand_fr(rng, -10, 10, 1000), rand_fr(rng, -10, 10, 1000)
s = rates(Fr(rng.randint(10, 190), 100), Fr(rng.randint(10, 190), 100),
Fr(rng.randint(10, 190), 100), Fr(rng.randint(10, 190), 100))
s2 = list(s)
for k in (0, 3, 5):
s2[k] = Fr(rng.randint(10, 190), 100)
assert collide(f, s, gx, gy, True) == collide(f, s2, gx, gy, True)
def test_collision_moments():
# density kept, momentum gains exactly g
rng = random.Random(5)
for _ in range(200):
f = rand_pops(rng)
gx, gy = rand_fr(rng, -10, 10, 1000), rand_fr(rng, -10, 10, 1000)
s = rates(Fr(rng.randint(10, 190), 100), Fr(rng.randint(10, 190), 100))
a, b = to_moments(f), to_moments(collide(f, s, gx, gy, True))
assert b[0] == a[0] and b[3] == a[3] + gx and b[5] == a[5] + gy
def test_hand_example_collision():
f = list(W_EXACT)
f[1] += Fr(1, 100)
f[3] += Fr(1, 100)
s = rates(Fr(5, 4), Fr(8, 9), Fr(1), Fr(1))
out = collide(f, s, exact=True)
assert out == [Fr(34, 75), Fr(269, 2400), Fr(11, 96), Fr(269, 2400), Fr(11, 96)] + [Fr(17, 600)] * 4
assert to_moments(out) == [Fr(51, 50), Fr(-51, 25), Fr(51, 50), 0, 0, 0, 0, Fr(-1, 200), 0]
bgk = collide(f, [Fr(5, 4)] * 9, exact=True)
assert bgk == [Fr(41, 90), Fr(401, 3600), Fr(41, 360), Fr(401, 3600), Fr(41, 360)] + [Fr(41, 1440)] * 4
def test_guo_force_moments():
u, v, gx, gy = Fr(1, 10), Fr(1, 20), Fr(1, 1000), Fr(0)
mf = to_moments(guo_force(u, v, gx, gy, True))
assert mf == [0, Fr(3, 5000), Fr(-3, 5000), Fr(1, 1000), Fr(-1, 1000), 0, 0, Fr(1, 5000), Fr(1, 20000)]
# ---------------------------------------------------------------- whole runs
def test_exact_conservation_on_a_small_lattice():
rng = random.Random(6)
nx, ny = 3, 4
f0 = [rand_pops(rng) for _ in range(nx * ny)]
s = rates(Fr(6, 5), Fr(11, 10), Fr(13, 10), Fr(7, 5))
gx, gy = Fr(1, 500), Fr(-1, 700)
mass0 = sum(sum(p) for p in f0)
jx0 = sum(sum(C[i][0] * p[i] for i in range(9)) for p in f0)
jy0 = sum(sum(C[i][1] * p[i] for i in range(9)) for p in f0)
for steps in (1, 2, 3):
res = mrt_d2q9(nx, ny, s, steps, None, gx, gy, walls=False, exact=True, f0=f0)
assert sum(sum(p) for p in res.f) == mass0
assert sum(sum(C[i][0] * p[i] for i in range(9)) for p in res.f) == jx0 + steps * nx * ny * gx
assert sum(sum(C[i][1] * p[i] for i in range(9)) for p in res.f) == jy0 + steps * nx * ny * gy
walled = mrt_d2q9(nx, ny, s, steps, None, gx, 0, walls=True, exact=True, f0=f0)
assert sum(sum(p) for p in walled.f) == mass0
def poiseuille(s_nu, s_q, ny, g=1e-6):
nu = nu_of_rate(s_nu)
steps = int(30 * ny * ny / (math.pi ** 2 * nu)) + 100
res = mrt_d2q9(1, ny, rates(s_nu, s_q), steps, lambda x, y: (1.0, 0.0, 0.0), gx=g, walls=True)
return [(res.ux[y][0] - g / (2 * nu) * (y + 0.5) * (ny - 0.5 - y)) / g for y in range(ny)]
def test_poiseuille_magic_rate_is_exact():
for s_nu in (1.0, 1 / 0.8):
err = poiseuille(s_nu, magic_q_rate(s_nu), 6)
assert max(abs(e) for e in err) < 1e-7
def test_poiseuille_bgk_rates_slip():
for tau in (1.0, 0.8):
lam = tau - 0.5
slip = 2 * lam - 3 / (8 * lam)
err = poiseuille(1 / tau, 1 / tau, 6)
assert max(abs(e - slip) for e in err) < 1e-6
def test_magic_rate_value():
assert magic_q_rate(Fr(1)) == Fr(8, 7)
assert (1 / Fr(5, 4) - Fr(1, 2)) * (1 / magic_q_rate(Fr(5, 4)) - Fr(1, 2)) == Fr(3, 16)
def taylor_green_error(n, s_nu, s_q):
nu = nu_of_rate(s_nu)
k = 2 * math.pi / n
u0 = 0.16 / n
steps = n * n // 8
def init(x, y):
rho = 1.0 - 0.75 * u0 * u0 * (math.cos(2 * k * x) + math.cos(2 * k * y))
return rho, -u0 * math.cos(k * x) * math.sin(k * y), u0 * math.sin(k * x) * math.cos(k * y)
res = mrt_d2q9(n, n, rates(s_nu, s_q), steps, init)
dec = math.exp(-2 * nu * k * k * steps)
err = max(abs(res.ux[y][x] + u0 * math.cos(k * x) * math.sin(k * y) * dec)
for y in range(n) for x in range(n))
return err / (u0 * dec)
def test_taylor_green_second_order():
e8 = taylor_green_error(8, 1.25, 1.1)
e16 = taylor_green_error(16, 1.25, 1.1)
assert abs(e8 - 0.0796) < 5e-4
assert abs(math.log2(e8 / e16) - 2) < 0.2
History
Lattice Boltzmann models grew from lattice gas automata in the late 1980s. In 1992 d'Humières proposed a generalized lattice Boltzmann equation whose collision is a matrix acting in a basis of moments, in the same year that the single-rate BGK form of Qian, d'Humières and Lallemand and of Chen, Chen and Matthaeus appeared. BGK became the common choice because it is simple. Lallemand and Luo (2000) gave a full linear analysis of the D2Q9 MRT model, with the basis above, and showed that the free rates improve its stability. d'Humières, Ginzburg, Krafczyk, Lallemand and Luo (2002) extended the method to three dimensions, and Ginzburg and coworkers developed the two-relaxation-time form and the analysis of the wall position.
References
- D. d'Humières, Generalized lattice-Boltzmann equations, in B. D. Shizgal and D. P. Weaver (eds.), Rarefied Gas Dynamics: Theory and Simulations, Progress in Astronautics and Aeronautics 159, AIAA, 1992, pp. 450-458
- Pierre Lallemand and Li-Shi Luo, Theory of the lattice Boltzmann method: Dispersion, dissipation, isotropy, Galilean invariance, and stability, Physical Review E 61 (2000) 6546-6562, the D2Q9 moment basis, the equilibrium moments and the linear stability analysis
- Dominique d'Humières, Irina Ginzburg, Manfred Krafczyk, Pierre Lallemand and Li-Shi Luo, Multiple-relaxation-time lattice Boltzmann models in three dimensions, Philosophical Transactions of the Royal Society A 360 (2002) 437-451
- Irina Ginzburg and Dominique d'Humières, Multireflection boundary conditions for lattice Boltzmann models, Physical Review E 68 (2003) 066614, the dependence of the bounce-back wall position on the relaxation rates
- Timm Krüger, Halim Kusumaatmaja, Alexandr Kuzmin, Orest Shardt, Goncalo Silva and Erlend Magnus Viggen, The Lattice Boltzmann Method: Principles and Practice, Springer, 2017, Chapter 10 (MRT and TRT collision operators)
- Zhaoli Guo, Chuguang Zheng and Baochang Shi, Discrete lattice effects on the forcing term in the lattice Boltzmann method, Physical Review E 65 (2002) 046308
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag JZ1BG8 Edit this page History Report an error Markdown