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.

Solutions · 15. The lattice Boltzmann method on the D2Q9 lattice

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 15. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. 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.


Exercise 1: D2Q9 Hermite Moment Conservation & Exact O(Ma^3) Galilean Defect
============================================================================
Max |sum(f^eq) - rho|               : 2.220e-16
Max |Pi_xx^eq - (rho*cs^2+rho*ux^2)|: 1.110e-16
Max |Pi_xy^eq - rho*ux*uy|          : 1.019e-17
Max |(Q_xxx^MB - Q_xxx^D2Q9) - rho*ux^3| : 5.193e-17

Explanation. Because every component of D2Q9_VELOCITIES belongs to {−1,0,+1}\{-1, 0, +1\}, the algebraic identity ciα3=ciαc_{i\alpha}^3 = c_{i\alpha} forces ∑icix3fieq≡∑icixfieq=ρux\sum_i c_{ix}^3 f_i^{\mathrm{eq}} \equiv \sum_i c_{ix} f_i^{\mathrm{eq}} = \rho u_x, omitting the ρux3\rho u_x^3 term. That single identity explains why standard 3-speed D2Q9 LBM is strictly a low-Mach weakly compressible Navier–Stokes solver (∣u∣≲0.15  ⟹  Ma≲0.26|\mathbf{u}| \lesssim 0.15 \implies \mathrm{Ma} \lesssim 0.26).


Exercise 2

Problem. 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.


Exercise 2: H-Theorem Guards & Raw Over-Relaxation Instability at tau = 0.36 < 0.5
==================================================================================
Initial amplitude = 1.000e-03 | tau=0.80 final amp = 2.064e-09 (decayed) | tau=0.36 final amp = 9.754e+05 (grew without bound)

Explanation. For τ=0.36\tau = 0.36, the BGK relaxation factor ω=1/τ=2.778>2\omega = 1/\tau = 2.778 > 2 overshoots local equilibrium on every collision (1 - omega = -1.778 < -1), corresponding to negative kinematic viscosity ν=(0.36−0.5)/3=−0.0467<0\nu = (0.36 - 0.5)/3 = -0.0467 < 0 and short-wavelength shear modes that grow without bound within 45 steps.


Exercise 3

Problem. 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\}.


Exercise 3: Implement It Yourself — Chapman-Enskog Viscosity Measurement via Shear-Wave Decay
=============================================================================================
tau = 0.65 | nu_measured = 0.050116 vs nu_CE = 0.050000 | rel diff = 0.231%
tau = 0.85 | nu_measured = 0.116818 vs nu_CE = 0.116667 | rel diff = 0.130%
tau = 1.10 | nu_measured = 0.199774 vs nu_CE = 0.200000 | rel diff = 0.113%

Summary. Measuring macroscopic viscous decay from pure D2Q9 streaming + BGK collision confirms Chapman & Enskog’s ν=(τ−1/2)/3\nu = (\tau - 1/2)/3 law directly from kinetic dynamics. Note also that solid boolean indexing handles channel walls in 2 lines of code; in Lecture 16 the same masking is used to simulate bluff-body wakes and porous-rock Darcy permeability with zero body-fitted remeshing.