Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

15. The lattice Boltzmann method on the D2Q9 lattice

School of Engineering, Nazarbayev University

Lectures 12–14 followed the classical continuum CFD paradigm: discretize the macroscopic Navier–Stokes PDEs directly and pay an elliptic global Poisson tax at every step. The Lattice Boltzmann Method (LBM) flips CFD upside down by stepping one level deeper to mesoscale kinetic theory: instead of tracking u(x,t)\mathbf{u}(\mathbf{x}, t) and p(x,t)p(\mathbf{x}, t), we evolve nine discrete particle velocity populations f0,…,f8f_0, \dots, f_8 on a Cartesian lattice via two embarrassingly local operations, algebraic BGK collision at each node and exact shift-streaming to nearest/diagonal neighbors. Through Sydney Chapman and David Enskog’s multiscale expansion, the macroscopic incompressible Navier–Stokes equations emerge automatically with kinematic viscosity ν=cs2(τ−12)Δx2Δt\nu = c_s^2(\tau - \frac{1}{2})\frac{\Delta x^2}{\Delta t}. In this lecture we verify D2Q9 Gauss–Hermite quadrature isotropy, moment conservation of lattice_boltzmann.d2q9_equilibrium, Boltzmann’s H-theorem stability barrier (τ>0.5\tau > 0.5), Guo’s exact 2nd-order body forcing, halfway bounce-back walls, and the “magic” relaxation time τ=12+34\tau = \frac{1}{2} + \frac{\sqrt{3}}{4} in lattice_boltzmann.simulate_lbm_poiseuille_2d.

Learning objectives

  1. Verify 2nd- and 4th-order Gauss–Hermite quadrature isotropy of D2Q9_VELOCITIES and D2Q9_WEIGHTS (cs2=1/3c_s^2 = 1/3) and exact mass/momentum/stress moment recovery of lattice_boltzmann.d2q9_equilibrium.

  2. Relate dimensionless relaxation time τ\tau to kinematic viscosity ν=13(τ−12)Δx2Δt\nu = \frac{1}{3}(\tau - \frac{1}{2})\frac{\Delta x^2}{\Delta t} via lbm_viscosity_from_tau and lbm_tau_from_viscosity, and explain why τ≤0.5\tau \leq 0.5 violates Boltzmann’s H-theorem (ν≤0\nu \leq 0, over-relaxation blow-up).

  3. Combine Guo–Zheng–Shi (2002) body forcing (ρu=∑icifi+12FΔt\rho\mathbf{u} = \sum_i \mathbf{c}_i f_i + \frac{1}{2}\mathbf{F}\Delta t) with halfway bounce-back no-slip walls (ywall=0.5,Ny−1.5y_{\mathrm{wall}} = 0.5, N_y - 1.5) in simulate_lbm_poiseuille_2d.

  4. Demonstrate that setting τmagic=12+34≈0.93301\tau_{\mathrm{magic}} = \frac{1}{2} + \frac{\sqrt{3}}{4} \approx 0.93301 cancels second-order BGK bounce-back slip error, matching analytical Poiseuille flow to <10−8< 10^{-8}.

Time: about 60 min

1. Physical Principles: Mesoscale Kinetic Theory & The D2Q9 Lattice

Rather than tracking individual molecules (Molecular Dynamics) or continuum averages (Navier–Stokes), Ludwig Boltzmann’s kinetic equation with the Bhatnagar–Gross–Krook (BGK, 1954) single-relaxation-time collision operator evolves the single-particle distribution fi(x,t)f_i(\mathbf{x}, t) along Q=9Q = 9 discrete 2-D lattice velocities ci∈{0,±1}2\mathbf{c}_i \in \{0, \pm 1\}^2 (lattice_boltzmann.D2Q9_VELOCITIES, lattice units Δx=Δt=1\Delta x = \Delta t = 1):

fi(x+ciΔt, t+Δt)−fi(x,t)=−1τ[fi(x,t)−fieq(ρ,u)]+Si(1.1)f_i(\mathbf{x} + \mathbf{c}_i \Delta t,\, t + \Delta t) - f_i(\mathbf{x}, t) = -\frac{1}{\tau}\left[f_i(\mathbf{x}, t) - f_i^{\mathrm{eq}}(\rho, \mathbf{u})\right] + S_i \tag{1.1}

To recover isotropic Galilean hydrodynamics (so viscous stress does not depend on whether a channel aligns with the grid axes or at 45∘45^\circ), the quadrature weights w0=49w_0 = \frac{4}{9}, w1..4=19w_{1..4} = \frac{1}{9}, w5..8=136w_{5..8} = \frac{1}{36} (D2Q9_WEIGHTS) are exact 3-point Gauss–Hermite tensor-product weights with lattice sound speed cs=1/3c_s = 1/\sqrt{3} (cs2=1/3c_s^2 = 1/3):

∑i=08wi=1,∑i=08wiciαciβ=cs2δαβ,∑i=08wiciαciβciγciθ=cs4(δαβδγθ+δαγδβθ+δαθδβγ)(1.2)\sum_{i=0}^8 w_i = 1, \qquad \sum_{i=0}^8 w_i c_{i\alpha} c_{i\beta} = c_s^2 \delta_{\alpha\beta}, \qquad \sum_{i=0}^8 w_i c_{i\alpha} c_{i\beta} c_{i\gamma} c_{i\theta} = c_s^4\left(\delta_{\alpha\beta}\delta_{\gamma\theta} + \delta_{\alpha\gamma}\delta_{\beta\theta} + \delta_{\alpha\theta}\delta_{\beta\gamma}\right) \tag{1.2}

Expanding the continuous Maxwell–Boltzmann distribution to O(∣u∣2/cs2)O(|\mathbf{u}|^2 / c_s^2) in Hermite polynomials yields lattice_boltzmann.d2q9_equilibrium, which conserves density ρ\rho, momentum ρu\rho \mathbf{u}, and the Euler inviscid momentum-flux tensor Παβeq=pδαβ+ρuαuβ\Pi_{\alpha\beta}^{\mathrm{eq}} = p\delta_{\alpha\beta} + \rho u_\alpha u_\beta (with isothermal ideal-gas pressure p=ρcs2=ρ/3p = \rho c_s^2 = \rho / 3):

fieq(ρ,u)=wiρ[1+ci⋅ucs2+(ci⋅u)22cs4−∣u∣22cs2]=wiρ[1+3(ci⋅u)+92(ci⋅u)2−32∣u∣2](1.3)f_i^{\mathrm{eq}}(\rho, \mathbf{u}) = w_i \rho \left[1 + \frac{\mathbf{c}_i \cdot \mathbf{u}}{c_s^2} + \frac{(\mathbf{c}_i \cdot \mathbf{u})^2}{2 c_s^4} - \frac{|\mathbf{u}|^2}{2 c_s^2}\right] = w_i \rho \left[1 + 3(\mathbf{c}_i \cdot \mathbf{u}) + \frac{9}{2}(\mathbf{c}_i \cdot \mathbf{u})^2 - \frac{3}{2}|\mathbf{u}|^2\right] \tag{1.3}

Demonstration 1: D2Q9 Gauss-Hermite Isotropy Tensors & Equilibrium Moment Conservation
======================================================================================
Sum w_i = 1.000000000000000 | 2nd-order isotropy error = 5.551e-17
4th-order Gauss-Hermite tensor isotropy error  = 5.551e-17
Equilibrium mass error     : 0.000e+00
Equilibrium momentum error : 2.776e-17
Equilibrium Euler stress |Pi^eq - (rho*cs^2*I + rho*u*u)| : 5.551e-17
<Figure size 1210x451 with 2 Axes>

Figure 15.1. (a) The nine velocities of the D2Q9 lattice with their weights. (b) Equilibrium populations of four directions against the horizontal velocity.

What Demonstration 1 shows. In panel (b), f3eq(ux)f_3^{\mathrm{eq}}(u_x) stays positive for moderate lattice Mach numbers Ma=∣ux∣/cs≪1\mathrm{Ma} = |u_x|/c_s \ll 1, and biasing f1eq>f3eqf_1^{\mathrm{eq}} > f_3^{\mathrm{eq}} when ux>0u_x > 0 carries net momentum ∑icixfieq=ρux\sum_i c_{ix} f_i^{\mathrm{eq}} = \rho u_x while reproducing the exact 2-D Euler stress tensor ρcs2I+ρuu\rho c_s^2 \mathbf{I} + \rho \mathbf{u}\mathbf{u} to 10-16.


2. Chapman–Enskog Multiscale Viscosity & Boltzmann’s H-Theorem (τ>0.5\tau > 0.5)

Expanding fi=fi(0)+ϵfi(1)+ϵ2fi(2)+…f_i = f_i^{(0)} + \epsilon f_i^{(1)} + \epsilon^2 f_i^{(2)} + \dots and ∂t=ϵ∂t1+ϵ2∂t2\partial_t = \epsilon \partial_{t_1} + \epsilon^2 \partial_{t_2} in Knudsen number ϵ≪1\epsilon \ll 1, Chapman–Enskog theory shows that non-equilibrium stress Παβ(1)=∑iciαciβfi(1)\Pi_{\alpha\beta}^{(1)} = \sum_i c_{i\alpha}c_{i\beta} f_i^{(1)} produces exact Newtonian viscous stress −ρν(∂αuβ+∂βuα)-\rho \nu (\partial_\alpha u_\beta + \partial_\beta u_\alpha) with kinematic viscosity (lattice_boltzmann.lbm_viscosity_from_tau):

ν=cs2(τ−12)Δx2Δt=τ−0.53Δx2Δt,τ=12+3ν ΔtΔx2(2.1)\nu = c_s^2 \left(\tau - \frac{1}{2}\right)\frac{\Delta x^2}{\Delta t} = \frac{\tau - 0.5}{3}\frac{\Delta x^2}{\Delta t}, \qquad \tau = \frac{1}{2} + \frac{3\nu\,\Delta t}{\Delta x^2} \tag{2.1}

Where does the −1/2-1/2 shift come from? Taylor-expanding the discrete streaming difference fi(x+ciΔt,t+Δt)−fi(x,t)f_i(\mathbf{x} + \mathbf{c}_i\Delta t, t + \Delta t) - f_i(\mathbf{x}, t) to second order generates +Δt2(∂t+ci⋅∇)2fi+\frac{\Delta t}{2}(\partial_t + \mathbf{c}_i \cdot \nabla)^2 f_i, an intrinsic discrete numerical viscosity −12cs2Δt-\frac{1}{2}c_s^2 \Delta t that subtracts 1/21/2 from τ\tau. Consequently, τ→0.5+\tau \to 0.5^+ corresponds to ν→0+\nu \to 0^+ (infinite Reynolds number), while τ≤0.5\tau \leq 0.5 yields unphysical negative viscosity ν≤0\nu \leq 0 (over-relaxation factor ω=1/τ≥2\omega = 1/\tau \geq 2), violating Boltzmann’s H-theorem and blowing up high-wavenumber modes.


Demonstration 2: Chapman-Enskog Viscosity Mapping & H-Theorem Stability Barrier (tau > 0.5)
===========================================================================================
  tau [-] |  omega = 1/tau |  nu_lb [lu^2/ts] |  Inverted tau
--------------------------------------------------------------
    0.520 |         1.9231 |         0.006667 |      0.520000
    0.600 |         1.6667 |         0.033333 |      0.600000
    0.800 |         1.2500 |         0.100000 |      0.800000
    1.000 |         1.0000 |         0.166667 |      1.000000
    1.400 |         0.7143 |         0.300000 |      1.400000

Water at 298 K (dx = 100 um, dt = 2 ms): nu = 8.926e-07 m^2/s -> tau = 1.03558 -> recovered nu = 8.926e-07

What Demonstration 2 shows. Equation (2.1) converts between lattice and SI units: with Δx=100 μm\Delta x = 100\,\mu\text{m} and Δt=2 ms\Delta t = 2\,\text{ms}, water (ν=8.93×10−7 m2/s\nu = 8.93 \times 10^{-7}\text{ m}^2/\text{s}) has the relaxation time τ=0.5+3νΔt/Δx2=1.0356\tau = 0.5 + 3\nu \Delta t / \Delta x^2 = 1.0356. A time step a thousand times smaller would give τ=0.5005\tau = 0.5005, too close to the stability limit τ=0.5\tau = 0.5 to be usable; in the lattice Boltzmann method the time step is chosen from the viscosity and the grid spacing, not from a Courant number alone.


3. Guo Body Forcing & Halfway Bounce-Back Poiseuille Verification

To drive planar channel flow along +x+x between solid rows j=0j = 0 and j=Ny−1j = N_y - 1 (lattice_boltzmann.simulate_lbm_poiseuille_2d):

  1. Halfway Bounce-Back Walls: Reflecting outgoing populations back along D2Q9_OPPOSITE ([0, 3, 4, 1, 2, 7, 8, 5, 6]) places the effective zero-velocity walls half a lattice spacing inside the fluid at ybot=0.5y_{\mathrm{bot}} = 0.5 and ytop=Ny−1.5y_{\mathrm{top}} = N_y - 1.5, giving channel aperture H=Ny−2H = N_y - 2 and wall-normal coordinate ηj=j−0.5∈(0,H)\eta_j = j - 0.5 \in (0, H) for fluid rows j=1,…,Ny−2j = 1, \dots, N_y - 2.

  2. Guo–Zheng–Shi (2002) Exact 2nd-Order Forcing: Discrete body forcing FxF_x requires both the source term Si=(1−12τ)wi[3(cix−ux)+9(ci⋅u)cix]FxS_i = (1 - \frac{1}{2\tau})w_i\left[3(c_{ix} - u_x) + 9(\mathbf{c}_i\cdot\mathbf{u})c_{ix}\right]F_x and the half-step momentum shift ρux=∑icixfi+12FxΔt\rho u_x = \sum_i c_{ix} f_i + \frac{1}{2}F_x \Delta t to eliminate spurious discrete forcing artifacts and match navier_stokes.poiseuille_couette_exact.


Demonstration 3: 2-D LBM Poiseuille Channel vs Continuum Navier-Stokes Parabola
===============================================================================
Fluid rows j = 1..8 -> Halfway bounce-back aperture H = 8.0 lu, nu_lb = 0.116667
Max relative error ||u_LBM - u_exact||_inf / u_max     : 5.5103e-03 (0.551%)
Cross-module check vs poiseuille_couette_exact         : 0.000e+00
<Figure size 770x451 with 1 Axes>

Figure 15.2. Poiseuille flow between bounce-back walls driven by a body force: lattice Boltzmann result at the eight fluid nodes (points) and the analytical parabola (line).

What Demonstration 3 shows. Without solving a single Poisson equation or discretizing ∇2u\nabla^2 u, local D2Q9 collisions + halfway bounce-back reproduce the continuum Navier–Stokes parabola to 0.55%0.55\% on just 8 fluid nodes (H = 8) at τ=0.85\tau = 0.85. Why is the error 0.55%0.55\% and not at the level of rounding error, when a parabola has constant ∂y2u\partial_y^2 u and zero ∂y3u\partial_y^3 u?


4. The TRT/BGK “Magic” Relaxation Time τ=12+34\tau = \frac{1}{2} + \frac{\sqrt{3}}{4}

Ginzburg & d’Humières (2003) proved via exact Milne kinetic boundary-layer analysis that single-relaxation-time BGK halfway bounce-back shifts the apparent no-slip wall position by a viscosity-dependent slip error proportional to:

ΛBGK(τ)=(τ−12)2−316⟹ΛBGK(τmagic)=0atτmagic=12+34≈0.9330127.(4.1)\Lambda_{\mathrm{BGK}}(\tau) = \left(\tau - \frac{1}{2}\right)^2 - \frac{3}{16} \quad \Longrightarrow \quad \Lambda_{\mathrm{BGK}}(\tau_{\mathrm{magic}}) = 0 \quad \text{at} \quad \tau_{\mathrm{magic}} = \frac{1}{2} + \frac{\sqrt{3}}{4} \approx 0.9330127. \tag{4.1}

We sweep τ∈{0.72,0.84,12+34,1.12}\tau \in \{0.72, 0.84, \frac{1}{2}+\frac{\sqrt{3}}{4}, 1.12\} on ny = 8 (H = 6) and watch the relative Poiseuille error collapse by six orders of magnitude right at τmagic\tau_{\mathrm{magic}}.


Demonstration 4: Sweeping Relaxation Time tau — Error against the Relaxation Time
=================================================================================
   tau [-] |  |(tau-0.5)^2 - 3/16| |  Max Poiseuille Rel. Error
------------------------------------------------------------------
  0.720000 |            1.3910e-01 |                 2.1293e-02
  0.840000 |            7.1900e-02 |                 1.0957e-02
  0.933013 |            2.7756e-17 |                 1.5037e-08
  1.120000 |            1.9690e-01 |                 3.0004e-02
<Figure size 770x451 with 1 Axes>

Figure 15.3. Largest relative error of the lattice Boltzmann Poiseuille profile against the relaxation time. The vertical line marks τ=1/2+3/4\tau = 1/2 + \sqrt{3}/4, at which the bounce-back wall lies exactly halfway between the nodes.

Inside the algorithm

What Demonstration 4 shows. Away from τmagic\tau_{\mathrm{magic}}, BGK wall-location error is ∼1–3%\sim 1\text{–}3\% on a coarse channel, whereas at τmagic=12+34≈0.933013\tau_{\mathrm{magic}} = \frac{1}{2} + \frac{\sqrt{3}}{4} \approx 0.933013, the error falls to 1.5×10−81.5 \times 10^{-8}. (Two-Relaxation-Time, TRT, LBM schemes exploit this exact identity at any viscosity by fixing the antisymmetric relaxation rate to keep Λ=3/16\Lambda = 3/16 constant.) Below we inspect d2q9_equilibrium and simulate_lbm_poiseuille_2d, and verify D2Q9_OPPOSITE involution (opp[opp[i]] == i).

Check against the library


Inside the Algorithm: lattice_boltzmann.d2q9_equilibrium & simulate_lbm_poiseuille_2d
=====================================================================================
def d2q9_equilibrium(
    rho: float | np.ndarray,
    ux: float | np.ndarray,
    uy: float | np.ndarray,
) -> np.ndarray:
    """Evaluate 2nd-order Hermite Maxwell-Boltzmann equilibrium distributions f_i^eq (shape (9, ...))."""
    rho_a = np.asarray(rho, dtype=float)
    ux_a = np.asarray(ux, dtype=float)
    uy_a = np.asarray(uy, dtype=float)
    u2 = ux_a**2 + uy_a**2
    feq = np.empty((9,) + rho_a.shape, dtype=float)
    for i in range(9):
        cx, cy = D2Q9_VELOCITIES[i]
        cu = cx * ux_a + cy * uy_a
        feq[i] = D2Q9_WEIGHTS[i] * rho_a * (1.0 + 3.0 * cu + 4.5 * (cu**2) - 1.5 * u2)
    return feq

def simulate_lbm_poiseuille_2d(
    nx: int = 8,
    ny: int = 26,
    tau: float = 0.85,
    body_force_x: float = 1.0e-5,
    n_steps: int = 2500,
) -> dict:
    """Simulate 2-D body-force-driven Poiseuille channel flow via D2Q9 BGK + Guo forcing + halfway bounce-back."""
    if ny < 5 or nx < 2 or tau <= 0.5:
        raise ValueError("Require nx >= 2, ny >= 5, and tau > 0.5.")
    nu = lbm_viscosity_from_tau(tau)
    solid = np.zeros((nx, ny), dtype=bool)
    solid[:, 0] = True
    solid[:, -1] = True
    fluid = ~solid

    rho = np.ones((nx, ny), dtype=float)
    ux = np.zeros((nx, ny), dtype=float)
    uy = np.zeros((nx, ny), dtype=float)
    f = d2q9_equilibrium(rho, ux, uy)

    for _ in range(n_steps):
        rho = np.sum(f, axis=0)
        cx_vec = D2Q9_VELOCITIES[:, 0][:, None, None]
        cy_vec = D2Q9_VELOCITIES[:, 1][:, None, None]
        ux = (np.sum(cx_vec * f, axis=0) + 0.5 * body_force_x) / rho
        uy = np.sum(cy_vec * f, axis=0) / rho
        ux[solid] = 0.0
        uy[solid] = 0.0

        feq = d2q9_equilibrium(rho, ux, uy)
        src = _guo_forcing_term(ux, uy, body_force_x, 0.0, tau)
        f_post = f - (f - feq) / tau + src
        # Halfway bounce-back: solid nodes reflect incoming post-collision populations back into fluid:
        for i in range(9):
            f_post[i, solid] = f[D2Q9_OPPOSITE[i], solid]
        f = _stream_periodic(f_post)

    # Final macroscopic velocity on fluid rows j = 1 .. ny-2 (walls at y_bot = 0.5, y_top = ny - 1.5, H = ny - 2):
    rho = np.sum(f, axis=0)
    ux = (np.sum(D2Q9_VELOCITIES[:, 0][:, None, None] * f, axis=0) + 0.5 * body_force_x) / rho
    ux[solid] = 0.0

    y_fluid = np.arange(1, ny - 1, dtype=float)
    h_chan = float(ny - 2)
    eta = y_fluid - 0.5
    u_exact = (body_force_x / (2.0 * nu)) * eta * (h_chan - eta)
    u_num = np.mean(ux[:, fluid[0, :]], axis=0)
    rel_err = float(np.max(np.abs(u_num - u_exact)) / np.max(np.abs(u_exact)))
    return {
        "y_fluid": y_fluid,
        "u_lbm": u_num,
        "u_exact": u_exact,
        "nu_lb": nu,
        "channel_height": h_chan,
        "max_rel_error": rel_err,
    }

At tau_magic: Guo-shifted error = 2.670e-09 vs unshifted momentum sum error = 2.309e-02

Exercises

Worked solutions are in 15_solutions.ipynb. The cell below the list is a short self-check to start from.

  1. Verify a result. Across 50 random states (ρ∈[0.8,1.2],ux,uy∈[−0.15,0.15])(\rho \in [0.8, 1.2], u_x, u_y \in [-0.15, 0.15]), verify to < 1e-14 that d2q9_equilibrium reproduces ρ\rho, ρu\rho\mathbf{u}, and ρcs2δαβ+ρuαuβ\rho c_s^2 \delta_{\alpha\beta} + \rho u_\alpha u_\beta, and show that the 3rd-order moment discrepancy QxxxMaxwell−∑icix3fieqQ_{xxx}^{\mathrm{Maxwell}} - \sum_i c_{ix}^3 f_i^{\mathrm{eq}} equals ρux3=O(Ma3)\rho u_x^3 = O(\mathrm{Ma}^3) identically.

  2. Find how it fails. Verify ValueError guards on lbm_viscosity_from_tau(0.5), lbm_tau_from_viscosity(0.0), and simulate_lbm_poiseuille_2d(tau=0.49), and run an unguarded periodic BGK shear-wave test comparing τ=0.80\tau = 0.80 vs. τ=0.36<0.5\tau = 0.36 < 0.5 over 45 steps to demonstrate H-theorem over-relaxation blow-up.

  3. Implement it yourself. Implement my_lbm_kolmogorov_shear_decay(ny, tau, n_steps) evolving a periodic shear wave ux(y,0)=10−3sin⁡(2πy/Ny)u_x(y, 0) = 10^{-3}\sin(2\pi y / N_y) on a 4×Ny4 \times N_y D2Q9 lattice (ny=36, n_steps=160), extract asymptotic νmeas\nu_{\mathrm{meas}} between t1=30t_1 = 30 and T=160T = 160, and verify < 0.5% relative error against lattice_boltzmann.lbm_viscosity_from_tau(tau) across τ∈{0.65,0.85,1.10}\tau \in \{0.65, 0.85, 1.10\}.


Self-check for the exercises
============================
D2Q9 sum(c_x^3 f^eq) = 0.13200000 == rho*u_x = 0.13200000 | Exact cubic Mach defect rho*u_x^3 = 0.00190080
All checks passed.