Home / Insights / Chmltech Online Journal

Chmltech Online JournalVolume 106, Issue 27Research article

A Flow Solver in the Browser: The Lattice Boltzmann Method Behind the Live CFD Figures

Quak Foo LeeChief Technology Officer, Chmltech Ltd.Division of Clean Energy Technology Innovation · Vancouver, BC, Canada

Abstract

The live flow figures on Chmltech run a lattice Boltzmann solver written in JavaScript, in the reader’s browser. This paper explains the method and tests the engine: the D2Q9 lattice, a two-relaxation-time collision with magic parameter 1/4, halfway bounce-back walls, a Zou–He inlet and a momentum-exchange force. In pressure-driven channel flow the profile error fell from 3.1 × 10⁻³ to 2.0 × 10⁻⁴ from 8 to 32 nodes, an observed order of 1.95 and 1.98; the wall sat 0.0840/H beyond the halfway point, and the error was 73 times smaller with Λ = 3/16. In the lid-driven cavity at Re 1,000 the minimum centreline velocity on a 128 × 128 grid was 0.51% too weak against the Botella–Peyret benchmark, converging at order 2.6 and 2.7 and level with OpenFOAM at that resolution. For a cylinder at zero blockage, drag was 1.586 at Re 40 (classic 1.522, modern 1.493), and drag 1.413 and Strouhal number 0.1635 at Re 100 (1.33 and 0.1643); refinement moved drag away from the references. Lid speeds from 0.025 to 0.2 moved the Re 100 cavity result by at most 0.02%; 17 of 31 runs on a 400-pass stability map diverged, and every setting the live figure offers stayed stable for 400 passes, its least viscous one for 1,000. In headless Chromium it ran 7.3 million node updates per second. It is accurate enough for teaching and quick comparisons within stated limits: two dimensions, low Mach number, a uniform grid and staircase walls.

Keywords: lattice Boltzmann method; two-relaxation-time collision; bounce-back boundary; lid-driven cavity; cylinder wake; verification and validation; JavaScript; engineering education

1. Introduction

Most of the flow figures in the Chmltech CFD series are pictures of solved fields. A few are not: the opening part of the series [1] has figures in which the reader changes the Reynolds number of a flow past a cylinder or an airfoil and watches the wake respond, and the flow is being solved while they watch, in their own browser, by about two hundred lines of JavaScript. It explains the method the engine uses, the lattice Boltzmann method, in enough detail to read the code, and then measures how wrong it is.

The lattice Boltzmann method suits a browser: each time step is a fixed sequence of local operations on a regular grid, with no pressure equation and no matrix to invert. The price is that it is a weakly compressible scheme, accurate when the flow speed is small against the lattice sound speed, with staircase walls on a uniform grid.

The tests follow the usual split between verification, which asks whether the equations are solved correctly, and validation, which asks whether they are the right equations for the flow [2]. Verification uses pressure-driven channel flow, which has an exact solution and shows where a bounce-back wall really sits. Validation uses two classic benchmarks with published reference data: the lid-driven square cavity [3, 4] and the flow past a circular cylinder in its steady and shedding regimes [5–7]. The cavity also allows a direct comparison with the OpenFOAM runs of CFD series Part 3 [8]. Compressibility error, stability limits and browser speed are measured last. Every number below comes from scripts that can be downloaded with the paper; 94 runs took 1.8 CPU-hours in Node.

2. The Lattice Boltzmann Method

2.1 Packets of fluid on a lattice

Where a finite-volume code such as OpenFOAM stores velocity and pressure in each cell, the lattice Boltzmann method stores, at each node of a square grid, nine numbers f₀ … f₈, each the amount of fluid moving in one of nine directions (Figure 1(a)). One packet is at rest, four move to the neighbouring nodes along the axes and four along the diagonals. This set of directions is called D2Q9, for two dimensions and nine velocities [9]. Density and momentum at a node are the sum of the packets and the sum of the packets times their directions, ρ = Σ fᵢ and ρu = Σ fᵢ eᵢ. Everything is in lattice units: the node spacing and the time step are both 1, so a packet on an axis moves one node per step.

Three panels. Left: a node with arrows to its eight neighbours, four along the axes in blue and four diagonals in orange, labelled e1 to e8. Middle: a bar chart of nine packet values before and after collision with equilibrium marks; after collision each bar is closer to its mark. Right: a small grid with arrows from one node to its neighbours, and an arrow into a grey wall that returns as a dashed green arrow. e1 e2 e3 e4 e5 e6 e7 e8 e0 Weights: rest 4/9, axes 1/9 (blue), diagonals 1/36 (orange) (a) D2Q9: nine packet directions f0 f1 f2 f3 f4 f5 f6 f7 f8 (b) Collide: relax toward equilibrium Equilibrium (Eq. 1) Before collision After collision solid wall bounces back each packet moves to the neighbouring node in its direction (c) Stream, with bounce-back at walls
Figure 1. The lattice Boltzmann method in one step: (a) the nine packet directions of the D2Q9 lattice and their weights; (b) collision, in which the packets at a node relax toward the equilibrium of Eq. (1) (illustrative values); (c) streaming, in which each packet moves to the neighbouring node in its direction and a packet that meets a wall returns along its own path.

2.2 Collision: relaxing toward equilibrium

Each time step has two parts. In the first, collision, the packets at each node are pushed toward an equilibrium set that has the same density and momentum but no other memory of how the fluid got there. The equilibrium is a truncated expansion of the Maxwell–Boltzmann distribution of molecular speeds, Eq. (1) [9, 10]:

\[f_i^{\mathrm{eq}} = w_i\,\rho \left[ 1 + \frac{\mathbf{e}_i \cdot \mathbf{u}}{c_s^2} + \frac{(\mathbf{e}_i \cdot \mathbf{u})^2}{2 c_s^4} - \frac{\mathbf{u} \cdot \mathbf{u}}{2 c_s^2} \right], \qquad c_s^2 = \tfrac{1}{3}\](1)

where the weights wᵢ are 4/9 for the rest packet, 1/9 along the axes and 1/36 on the diagonals, and c_s = 1/√3 is the lattice speed of sound. The simplest collision, due to Bhatnagar, Gross and Krook (BGK) [11], moves every packet a fraction 1/τ of the way to its equilibrium. The engine uses a refinement, the two-relaxation-time (TRT) collision [12]. Each packet fᵢ is paired with the packet f_ī moving the opposite way and split into a symmetric part f⁺ = (fᵢ + f_ī)/2, which carries density and momentum flux, and an antisymmetric part f⁻ = (fᵢ − f_ī)/2, which carries momentum. Each part relaxes at its own rate, Eq. (2):

\[f_i^{*} = f_i - \frac{1}{\tau^{+}} \left( f_i^{+} - f_i^{\mathrm{eq}+} \right) - \frac{1}{\tau^{-}} \left( f_i^{-} - f_i^{\mathrm{eq}-} \right)\](2)

The symmetric rate τ⁺ sets the viscosity. The antisymmetric rate τ⁻ is a free parameter, and the combination that matters is the magic parameter, Eq. (3):

\[\Lambda = \left( \tau^{+} - \tfrac{1}{2} \right) \left( \tau^{-} - \tfrac{1}{2} \right)\](3)

Holding Λ fixed when the viscosity changes keeps the steady solution, including where a bounce-back wall effectively sits, independent of the viscosity; with BGK (τ⁺ = τ⁻ = τ, so Λ = (τ − ½)²) the wall moves when the viscosity does [10, 12]. The engine uses Λ = 1/4, the value usually recommended for stability; Λ = 3/16 places the wall exactly halfway between nodes for channel flow [13]. Both choices are tested in Section 4.

2.3 Streaming and boundaries

In the second part of the step, streaming, each post-collision packet moves one node in its own direction, Eq. (4):

\[f_i(\mathbf{x} + \mathbf{e}_i,\; t + 1) = f_i^{*}(\mathbf{x}, t)\](4)

Walls are handled at the streaming step. A packet that would move from a fluid node into a solid node is sent back along its own path instead, arriving at the next step at the node it left, with the opposite direction. This halfway bounce-back puts the wall roughly halfway between the last fluid node and the first solid node, and it is exact for no-slip only to the degree measured in Section 4. For a wall moving at speed u_w, such as the lid of a cavity, the returning packet gains momentum from the wall, Eq. (5) [10, 14]:

\[f_{\bar{\imath}}(\mathbf{x}_b,\; t + 1) = f_i^{*}(\mathbf{x}_b, t) - 6\, w_i\, \rho\, (\mathbf{e}_i \cdot \mathbf{u}_w)\](5)

The force on a body follows directly from the same bookkeeping: every bounced packet reverses its momentum, so the force is the sum over all fluid–solid links of twice the momentum the packet carried, Eq. (6), the momentum-exchange method [14]:

\[\mathbf{F} = \sum_{\text{wall links}} 2\, f_i^{*}(\mathbf{x}_b)\, \mathbf{e}_i\](6)

The engine's inlet sets a uniform velocity with the boundary condition of Zou and He [15], which solves for the unknown packets entering the domain so that the node has the required velocity. Its default outlet copies the neighbouring column (zero gradient) after a sponge zone, the last 12% of the domain, where rising viscosity damps vortices; an optional Zou–He outlet fixes the pressure instead. The side walls of the cylinder channel are free-slip: packets are mirrored rather than reversed.

2.4 From packets to the Navier–Stokes equations

The link to the Navier–Stokes equations is a multiscale (Chapman–Enskog) expansion: when the packets are close to equilibrium and the flow varies slowly on the scale of the lattice, the moments of Eqs. (1), (2) and (4) satisfy the continuity and momentum equations with pressure p = c_s² ρ and kinematic viscosity, Eq. (7) [9, 10, 16]:

\[\nu = c_s^2 \left( \tau^{+} - \tfrac{1}{2} \right) = \frac{\tau^{+} - 1/2}{3}\](7)

The equations recovered are those of a slightly compressible fluid, with extra terms of order Ma², where the Mach number Ma = u/c_s = √3 u; the incompressible equations are recovered only as Ma → 0. The scheme is second-order accurate in space for smooth flow in the bulk; the boundaries decide whether it stays second order overall.

2.5 Units, Mach number and stability

A simulation is set up from a length in nodes N, a speed u and the viscosity ν of Eq. (7), matched to Re = u N / ν. At fixed Re and grid, a larger u raises the Mach number and the compressibility error; a smaller u lowers ν, brings τ⁺ toward ½ and the stability limit, and needs more steps per pass. The live figures use u = 0.1 (Ma = 0.17); the verification runs use 0.06 for the cylinder and 0.1 for the cavity.

3. The Browser Engine

The engine, cfd-lbm.js, is a single JavaScript file with no dependencies that runs unchanged in a browser and in Node. It stores the nine packet arrays as 32-bit typed arrays, fuses collision and streaming in one loop, and exposes a small interface: create a grid, set an obstacle mask, step, and read velocity, density and force. The obstacle can be a cylinder, a square, a flat plate or an airfoil, drawn on the grid as a mask of solid nodes, so curved walls become staircases of node-sized steps. Its options were extended for this paper with two additions that leave the default behaviour unchanged: a moving top wall for the cavity, and the magic parameter, so that the wall-location test could be run; a cylinder run with the default options gives the same lift history, bit for bit, as before. The live figure below runs the engine on the three test cases of this paper.

Three panels. Left, a long channel with a grey cylinder and a wake of alternating red and blue vortices. Middle, a square cavity shaded by speed with white streamlines forming one large clockwise vortex and small corner vortices. Right, a line chart of the velocity across a channel: blue points from the solver lie on an orange parabola between two dark wall lines. (a) Cylinder, Re 100: vorticity (b) Cavity, Re 1000 0 1 u / mean u 0 2 4 6 8 10 12 14 16 Node across the channel (c) Channel flow Exact LBM, 16 nodes
Figure 2. The engine running live on the three test cases of this paper: (a) the cylinder wake at Re 100, coloured by vorticity; (b) the lid-driven cavity at Re 1,000; (c) channel flow against the exact parabolic profile. In the online version the solver runs in the browser and the reader picks the case, the field shown and the Reynolds number; the printed version shows solved fields from the verification runs.

The live figure's cylinder (Figure 2) is 10 nodes across in a channel 8 diameters wide and 20 long (200 × 80 nodes), from Re 20 to 150 (τ⁺ = 0.5200 at the top). The cavity is 64 × 64 nodes, from Re 100 to 1,000, and the channel 20 nodes across, from Re 5 to 20. Compare all runs fixed settings: a cylinder 8 nodes across at Re 100, a 48 × 48 cavity at Re 400 and a 14-node channel at Re 10. Each setting, at both ends of each slider and at its default, was run in Node for 400 flow passes and stayed stable (12 runs), the cylinder's top setting for 1,000 (Section 7.2). An earlier Compare all with a 6-node cylinder diverged after about 200 passes. The figure also checks every solver after each frame and restarts it, with a note, if it goes unstable. The time steps per frame adapt to the reader's device; the converged solution does not.

4. Verification: Channel Flow

4.1 Convergence with grid refinement

Pressure-driven flow between two parallel plates (plane Poiseuille flow) has an exact parabolic velocity profile and exercises bounce-back and the pressure inlet and outlet. The channel was H nodes across and 8H long, with the Zou–He velocity inlet carrying a uniform flow and the Zou–He pressure outlet, at τ⁺ = 0.8 and Re = 1 on the channel height. Each run continued for at least three viscous diffusion times H²/ν, and until the profile changed by less than 10⁻⁹ of its peak per quarter diffusion time or for at most 40 diffusion times, when the change was below 3 × 10⁻⁵. The profile was read at 60% of the length, where it is fully developed, and compared with the exact parabola with the walls halfway between nodes, scaled to the same flow rate.

With the engine's Λ = 1/4 the root-mean-square (RMS) error of the profile was 3.1 × 10⁻³, 7.9 × 10⁻⁴ and 2.0 × 10⁻⁴ of the mean speed on grids of 8, 16 and 32 nodes, an observed order of 1.95 and 1.98 between successive grids (Figure 3(a)). With BGK at the same τ (Λ = 0.09) the errors were 4.8 × 10⁻³, 1.2 × 10⁻³ and 2.5 × 10⁻⁴. With Λ = 3/16 they were 4.5 × 10⁻⁵, 8.1 × 10⁻⁶ and 2.7 × 10⁻⁶, 67 times smaller than with 1/4 on the coarsest grid and 73 times smaller on the finest. What remains at 3/16 is compressibility: the driving pressure drop is a density difference of up to 5.87% along the channel, and the parabola is exact only for an incompressible fluid. The appendix program removes this effect by driving the same channel with a uniform body force and periodic ends, so that the density is uniform: there the error is 7.13 × 10⁻³ at 8 nodes and 4.46 × 10⁻⁴ at 32 with Λ = 1/4 (an order of 2.00), and below 9 × 10⁻¹³ with 3/16, round-off for this flow.

Left, a log–log chart: the errors with Λ = 1/4 and with BGK fall parallel to a dotted slope of −2, and Λ = 3/16 is far below both. Right, the offset rises in a straight line with the magic parameter and crosses zero near Λ = 3/16, marked by a dashed vertical line; the Λ = 1/4 points at different τ sit on top of each other while the BGK points spread along the line. 8 16 32 Nodes across the channel, H 10⁻⁵ 10⁻⁴ 10⁻³ RMS error of the profile dotted: slope −2 (a) Error against the exact profile TRT, Λ = 1/4 (the site's engine) BGK at τ = 0.8 (Λ = 0.09) TRT, Λ = 3/16 0.0 0.2 0.4 0.6 0.8 1.0 Magic parameter Λ −0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 Wall offset × H (lattice units²) (b) Where the bounce-back wall sits TRT, Λ = 1/4, τ from 0.55 to 1.5 BGK, τ from 0.55 to 1.5 (Λ = (τ − ½)²) TRT, Λ = 3/16, H = 8 to 32
Figure 3. Verification on pressure-driven channel flow: (a) RMS error of the velocity profile against the exact parabola as the grid is refined, for the engine's TRT collision with Λ = 1/4, BGK at τ = 0.8 and TRT with Λ = 3/16; (b) offset of the effective wall from the halfway point, times the channel height, against the magic parameter, for TRT and BGK at five relaxation times between 0.55 and 1.5 and for Λ = 3/16 on three grids, with a straight line fitted through all points.

4.2 Where the wall really is

The error at Λ = 1/4 has a simple cause, visible by fitting a parabola to the solved profile and asking where it reaches zero. The fitted channel is slightly wider than H: the effective walls sit 0.0840/H nodes outside the halfway points, on every grid (Figure 3(b)). The offset shrinks as 1/H, which is why the error still falls as H⁻². The same test at τ⁺ = 0.55, 0.6, 0.8, 1, 1.5 gave offsets of 0.0830 to 0.0953 (times H) with Λ = 1/4: the wall position does not depend on the viscosity, which is the purpose of fixing Λ. With BGK, where Λ changes with τ, the offset ran from -0.256 to 1.088; at τ = 1.5 the 16-node channel was effectively 0.136 nodes too wide, or 0.8% of its width. All 16 runs lie on one straight line in Λ, with slope 1.341 and zero offset at Λ = 0.1870, against 3/16 = 0.1875 at which bounce-back is known to be exact for this flow [12, 13]. The engine's walls in a 16-node channel sit 0.0053 of a node beyond the halfway points, which widens the channel by 0.07%, whatever the Reynolds number.

5. Validation: The Lid-Driven Cavity

The lid-driven cavity is a square box of fluid whose top wall slides at a constant speed U, driving one large vortex and, as the Reynolds number Re = U L/ν rises, smaller counter-rotating vortices in the corners. The references are the multigrid solutions of Ghia, Ghia and Shin [3] and, at Re 1,000, the spectral solution of Botella and Peyret [4], accurate to about seven digits. The engine was run with the moving-wall bounce-back of Eq. (5) on the lid and still bounce-back on the other three walls, with U = 0.1, on grids of N × N fluid nodes. Each run continued for at least 20 passes of the lid (20 L/U), and until the extreme centreline velocities changed by less than 10⁻⁷ per thousand steps or for at most 400 passes (150 at Re 1,000), when they changed by less than 3 × 10⁻⁶. The quantities compared are the minimum of the horizontal velocity u on the vertical centreline and the maximum and minimum of the vertical velocity v on the horizontal centreline, found by fitting a parabola through the extreme node and its neighbours.

At Re 100 the engine's minimum centreline velocity was -0.2105, -0.2133 and -0.2139 (in units of U) on grids of 32, 64 and 128, against -0.2109 from Ghia et al.: errors of +0.20%, −1.16% and −1.41% (Figure 4(a)). The engine's sequence converges at an observed order of 2.5 to -0.2140 (grid convergence index 0.07%), 1.5% beyond Ghia's value, which is a grid-node value from a 129 × 129 solution (a cubic through their points gives -0.2110); at this level Ghia's solution cannot tell which is closer, so the discriminating test is Re 1,000 against the spectral benchmark. That comparison is in Table 1 and Figure 4(b) and (c).

Table 1. Lid-driven cavity at Re 1,000: extreme centreline velocities (in units of the lid speed) against the spectral benchmark of Botella and Peyret, and the cost of each run on one core. Error = (value − reference)/|reference|: a positive error in u min or v min, or a negative one in v max, means the vortex is too weak.
Gridτ⁺u min (error)v max (error)v min (error)Lid passesCPU time (min)
64 × 640.5192-0.3763 (+3.15%)0.3639 (−3.46%)-0.5068 (+3.85%)1500.5
96 × 960.5288-0.3843 (+1.11%)0.3722 (−1.25%)-0.5201 (+1.33%)1501.5
128 × 1280.5384-0.3866 (+0.51%)0.3748 (−0.58%)-0.5236 (+0.65%)1504.2
Botella and Peyret (1998)—-0.38860.3769-0.5271——

The error in u min fell from +3.15% to +0.51% as the grid doubled, an observed order of 2.6 and 2.7 between successive grids. Richardson extrapolation of the three grids [17] gives an observed order of 2.5 and an extrapolated u min of -0.3888, −0.05% from the benchmark, with a grid convergence index of 0.71% on the finest grid. The residual is well inside the grid convergence index, and the observed orders, close to the extrapolated one, show that these grids are in the asymptotic range.

Left and middle, line charts of u against height: the solver's curve passes through Ghia's circles at Re 100 and through Botella's diamond at Re 1,000, where the profile is S-shaped with a thin layer under the lid. Right, a log–log chart: both errors fall with the grid; the solver's start higher and fall more steeply, meeting OpenFOAM's line at the finest grid. 0.0 0.5 1.0 u / U (vertical centre line) 0.0 0.2 0.4 0.6 0.8 1.0 y / L (a) Re 100 LBM, 128 × 128 Ghia et al. (1982) 0.0 0.5 1.0 u / U (vertical centre line) 0.0 0.2 0.4 0.6 0.8 1.0 y / L (b) Re 1,000 LBM, 128 × 128 Botella & Peyret (1998), u min 20 40 64 128 160 Grid points across the cavity 10⁰ 10¹ Error in u min (%) (c) Re 1,000: error against Botella LBM (this paper) OpenFOAM, CFD series Part 3
Figure 4. The lid-driven cavity: (a) horizontal velocity on the vertical centre line at Re 100 against Ghia et al. [3]; (b) the same at Re 1,000 with the benchmark minimum of Botella and Peyret [4]; (c) error in the minimum centreline velocity at Re 1,000 against the number of grid points across the cavity, for this engine and for the OpenFOAM runs of CFD series Part 3.

CFD series Part 3 solved the same cavity with OpenFOAM on meshes of 20 × 20, 40 × 40, 80 × 80, 160 × 160 cells and found u-min errors of 17.88%, 4.46%, 1.27%, 0.34% against the same benchmark, at observed orders of 2.0, 1.8, 1.9 [8, 18]. Interpolated to the engine's grids along its own convergence line, OpenFOAM's error would be 1.9% at 64 × 64 and 0.52% at 128 × 128, against the engine's +3.15% and +0.51%: the engine is 1.7 times worse on the coarser grid and level on the finer, because it converges faster. Halving the lid speed on the 64 × 64 grid changed u min by 0.12%, so compressibility is a small part of the error. The lid's corners need care. Where the moving lid meets a still side wall the velocity is discontinuous, and the diagonal link of each top corner node that points into the corner crosses the boundary at that corner point; the engine bounces it back as from a still wall. A first version gave it the lid's momentum, as the straight links get, so the lid acted half a node beyond its ends: its errors were +9.90%, +5.77% and +4.05% on the same grids, at an observed order of only 1.3 and 1.2. It was corrected before publication. The live figure's 64 × 64 cavity shows the right vortex structure, but at Re 1,000 its speeds are about 3.15% too low.

6. Validation: Flow Past a Cylinder

6.1 Set-up and blockage

The cylinder is the flow of the live figures in CFD series Part 1 [1]. It was set up as there: a cylinder D nodes across, 5D behind a uniform Zou–He inlet in a channel 20D long with free-slip side walls, the zero-gradient outlet with its sponge, and inlet speed u = 0.06 (Ma = 0.10). Published reference values are for a cylinder in open flow, while the channel is confined: with the walls 8D apart, the cylinder blocks 1/8 of the width (blockage ratio B = 1/8), and confinement raises the drag and the shedding frequency. Following Part 1, each case was run in channels 8D and 16D wide (B = 1/8 and 1/16) and extrapolated to zero blockage assuming the effect is linear in B, v₀ = 2 v(1/16) − v(1/8). A third run with a finer cylinder, 16 nodes across instead of 12, measured the effect of the grid. Forces came from Eq. (6), averaged over the last 30 passes of the flow at Re 40 (after 90) and the last 80 at Re 100 (after 70); the drag coefficient is C_d = F_x / (½ ρ u² D). The Strouhal number St = f D/u was taken from the peak of the lift spectrum, checked by a sine fit.

6.2 Re 40: steady separated flow

At Re 40 the flow separates from the rear of the cylinder and forms two steady counter-rotating eddies, the recirculation zone (Figure 5(a)). Table 2 gives the drag and the length of the zone behind the cylinder, in diameters. Extrapolated to zero blockage, the drag was 1.586, +4.2% from the 1.522 of Dennis and Chang [5], a classic steady solution, and +6.2% from the modern reference of Gautier, Biau and Lamballais, 1.493 [19]. The recirculation length, measured from the rear of the drawn circle, was 2.530 diameters against 2.345 and 2.236 (+7.9% and +13.2%). Refining the cylinder from 12 to 16 nodes increased the drag by +1.9% and the length by +7.2%; applied to the extrapolated values, which already lie above both references, that gives 1.62 and 2.71 D, further from both, so the closeness after extrapolation is partly fortuitous. On the 12-node staircase the bounce-back wall on the centre rows sits half a node upstream of the drawn rear, an ambiguity of about 0.04 D in the length.

Table 2. Cylinder at Re 40 and Re 100: drag coefficient, recirculation length and Strouhal number for a cylinder D nodes across in channels with blockage B = 1/8 and 1/16, extrapolated to zero blockage, and published values for open flow: classic, Dennis and Chang (Re 40), Posdziech and Grundmann (C_d, Re 100) and Williamson's fit (St); modern, Gautier et al. (Re 40).
CaseBC_d Re 40L_w/D Re 40C_d Re 100St Re 100
D 121/81.8772.081.5380.1824
D 161/81.9142.221.6020.1815
D 121/161.7312.301.4760.1730
D 12, extrapolated01.5862.5301.4130.1635
Same, with D 12→16 change01.622.711.47—
Classic reference01.5222.3451.330.1643
Modern reference01.4932.236——

6.3 Re 100: vortex shedding

Above a Reynolds number of about 47 the steady wake becomes unstable and sheds vortices alternately from each side, the Kármán vortex street [6]. At Re 100 the lift coefficient oscillated with an amplitude of 0.37 and the drag by a standard deviation of 0.017 on the 12-node, B = 1/8 grid. Extrapolated to zero blockage, the Strouhal number was 0.1635, −0.5% from 0.1643 given by Williamson's fit to experiments [20], and the drag 1.413, +6.2% from the 1.33 of the careful two-dimensional computations of Posdziech and Grundmann [7]. The finer cylinder changed St by −0.5% and the drag by +4.1%, again away from the reference (1.47 with the change applied, +11%). The 16D-wide run is not fully periodic: its lift amplitude grew from 0.40 to 0.52 across the averaging window, and continued to 300 passes it reached 2.0, with a second frequency, St ≈ 0.29, dominating from about 180 passes. That matches the lowest transverse acoustic mode between the free-slip walls, St = c_s D/(2Wu) ≈ 0.30 for a channel W wide. In the window the spectral St stayed between 0.170 and 0.173, but the B = 1/16 values, and so the zero-blockage ones, come from a run that is not converged to a periodic state.

Left, a blue speed map around a grey cylinder with white streamlines that close into two eddies behind it, their length marked by an orange bracket. Middle and right, grouped bars for the 12-node and 16-node cylinders at blockage 1/8, the 12-node cylinder at 1/16 and the extrapolated value, each group with an orange line at the classic published value and, at Re 40, a dashed green line at the modern reference; the finer cylinder's drag bars sit further from the lines than the coarser cylinder's. (a) Re 40: steady wake, 12 nodes across L = 2.08 D Drag Cd Wake L/D 0.0 0.5 1.0 1.5 2.0 2.5 3.0 (b) Re 40 1.88 1.91 1.73 1.59 2.08 2.22 2.3 2.53 Drag Cd St (right axis) 0.0 0.5 1.0 1.5 2.0 (c) Re 100 1.54 1.6 1.48 1.41 0.00 0.05 0.10 0.15 0.20 0.25 Strouhal number St 0.182 0.181 0.173 0.164 Published (Dennis & Chang; Posdziech & Grundmann; Williamson) Gautier et al. (2013) 12 nodes, B = 1/8 16 nodes, B = 1/8 12 nodes, B = 1/16 Extrapolated to B = 0
Figure 5. Flow past a cylinder: (a) speed and streamlines at Re 40 with the steady recirculation zone behind the cylinder, 12 nodes across, blockage 1/8; (b) drag coefficient and recirculation length at Re 40 and (c) drag coefficient and Strouhal number at Re 100, for the three grids, extrapolated to zero blockage, against published values for a cylinder in open flow.

The drag is the least accurate quantity: a 12-node cylinder is a staircase, and halfway bounce-back on it gives a body that is neither the circle drawn nor exactly any other shape. The live figure, with a 10-node cylinder at blockage 1/8, shows the onset of shedding, the vortex street and its frequency, but not a drag coefficient to quote.

7. Compressibility, Stability and Speed

7.1 The compressibility error

The cavity at Re 100 on a 64 × 64 grid was run with lid speeds of 0.025, 0.05, 0.1 and 0.2, keeping the Reynolds number fixed by changing the viscosity, with the stopping rule of Section 5. The minimum centreline velocity moved by at most 0.02% from the run at the smallest lid speed, with no trend in Ma² above that scatter (Figure 6(a)), although at U = 0.2 (Ma = 0.346) the density varied by up to 76.9%, largest at the downstream top corner, where the pressure of the continuum flow is singular. At Re 1,000 halving the lid speed changed u min by 0.12%. In the cavity the compressibility error is therefore well below the discretization error of these grids at the live figures' U = 0.1; it matters more where a density difference drives the flow, as in the channel of Section 4.

Left, four points within a few hundredths of a percent of zero, with no trend in Ma². Right, a grid of green and red cells: runs with few nodes across the cylinder and high Reynolds number, where τ is closest to one half, diverged; the rest stayed stable. 0.00 0.02 0.04 0.06 0.08 0.10 0.12 Ma² (lid speed over lattice sound speed, squared) −0.100 −0.075 −0.050 −0.025 0.000 0.025 0.050 0.075 0.100 Change in u min from U = 0.025 (%) U = 0.025 U = 0.05 U = 0.1 U = 0.2 (a) Compressibility error Cavity, Re 100, 64 × 64 50 100 150 160 200 300 400 Reynolds number 6 8 10 12 16 Nodes across the cylinder, D stable τ = 0.536 diverged τ = 0.518 diverged τ = 0.512 diverged τ = 0.509 diverged τ = 0.506 diverged τ = 0.504 stable τ = 0.548 stable τ = 0.524 diverged τ = 0.516 diverged τ = 0.512 diverged τ = 0.508 diverged τ = 0.506 stable τ = 0.560 stable τ = 0.530 stable τ = 0.520 stable τ = 0.519 diverged τ = 0.515 diverged τ = 0.510 diverged τ = 0.507 stable τ = 0.572 stable τ = 0.536 stable τ = 0.524 diverged τ = 0.518 diverged τ = 0.512 diverged τ = 0.509 stable τ = 0.596 stable τ = 0.548 stable τ = 0.532 stable τ = 0.524 diverged τ = 0.516 diverged τ = 0.512 (b) Stability over 400 passes, u = 0.1, B = 1/8
Figure 6. Limits of the scheme: (a) change in the minimum centreline velocity of the Re 100 cavity from the run at the smallest lid speed, against the square of the Mach number, for four lid speeds at fixed Re on a 64 × 64 grid; (b) stability of the cylinder at inlet speed 0.1 for five grid resolutions and seven Reynolds numbers (Re 160 only at 10 nodes across), with the relaxation time τ⁺ of each run.

7.2 Stability

The TRT scheme is stable for a wider range of relaxation times than BGK, but not for all. A cylinder at the live figure's inlet speed of 0.1, in a channel 8 diameters wide (B = 1/8), was run for 400 passes of the flow past it on grids of 6, 8, 10, 12, 16 nodes across, at Re 50, 100, 150, 160, 200, 300, 400 (Figure 6(b)). A run counted as unstable if the speed at any node, checked every 50 steps, passed 0.6 or was not a number. 17 of the 31 runs diverged, every one above a limit that rises with D: D = 6 from Re 100; D = 8 from Re 150; D = 10 and 12 from Re 200; D = 16 from Re 300. Forty passes, the length of an earlier version of this map, were not enough: the 6-node cylinder at Re 100 passed 40 and diverged after about 200. Over 400 passes the relaxation time separated the two groups, but only just: every run with τ⁺ up to 0.5180 diverged and every run from 0.5188 up stayed stable, and the least viscous stable run was D = 10 at Re 160, which diverged after about 670 passes when run longer, so the live figure stops at Re 150, stable for 1,000. With D = 10, the live figure's grid, the runs at Re 50, 100, 150, 160 stayed stable; Re 200 (τ⁺ = 0.5150) diverged. Stability is a property of the whole set-up, the map holds for this one, and the wide channel of Section 6.3 shows a slow acoustic growth that a longer run could turn into divergence.

7.3 Speed in Node and in the browser

Table 3 gives the throughput of the engine in million node updates per second (Mnups), the usual measure for lattice Boltzmann codes [10], in Node on a shared two-core server and in headless Chromium on the same machine, with the number of time steps that fit the live figure's budget of 9 ms per animation frame.

Table 3. Speed of the engine on one core of a shared server: million node updates per second, and time steps that fit a 9 ms budget per animation frame in the browser (one cylinder at Re 100 with the whole budget; Compare all gives each of its three cases a third). A laptop browser is usually faster; drawing the frame is not included.
GridUseMnups, Chromiumms per stepSteps per frameFlow past the cylinder at 60 frames/s (D per s)
120 × 48Coarser grid (D 6)9.30.621414.0
200 × 80Live figure7.32.1942.4
400 × 160Twice as fine7.98.1510.3

In Node the engine ran at 8.4, 7.2 and 7.6 Mnups on grids of 120 × 48, 200 × 80 and 400 × 160. These rates are modest against compiled or GPU lattice Boltzmann codes, which reach hundreds or thousands of Mnups, but they animate a 200 × 80 grid at several diameters of flow per second. Doubling the resolution costs eight times as much per unit of flow time, which is why the live figures stay coarse.

8. Why a Solver in the Browser

A browser solver is a teaching instrument, not a production CFD code. The hardest ideas to convey in print are about behaviour: that a wake becomes unsteady at a particular Reynolds number, that a coarse grid gives a plausible but wrong answer, that a result depends on how far the walls are. A figure that lets the reader cross the shedding threshold, solved live rather than replayed, carries that better than static pictures, and being small enough to run on a phone it shows what a few thousand cells can and cannot do [8]. The engine is also small enough to read, and once a solver is visible and testable its accuracy has to be stated, which is what this paper does.

9. Limits

The engine solves two-dimensional flow, so it cannot show three-dimensional effects: the wake of a real cylinder becomes three-dimensional above a Reynolds number of about 190 [6], and turbulent flow, the regime of most process equipment, is three-dimensional at every scale. The engine has no turbulence model and no energy equation, so it shows neither turbulent mixing nor heat transfer. It is weakly compressible: the error was small in the cavity (Section 7.1) but limited the channel flow of Section 4, where a density difference drives the flow. Its grid is uniform, so it cannot concentrate nodes in boundary layers, and curved walls are staircases, whose effect on drag was a few percent at 12 to 16 nodes across a cylinder. The magic parameter of 1/4 puts the walls slightly beyond the halfway point, 0.0840/H of a node, and the moving lid's corners need the rule of Section 5. The blockage extrapolation assumes a linear effect from two channel widths. The timings were measured on a shared server and are lower bounds. For process equipment, an impeller, a jacket, a nozzle, the engine is an illustration, not a design tool.

10. Conclusions

The lattice Boltzmann engine behind the site's live flow figures was explained and tested with fresh runs, every number traced to scripts.

  1. In pressure-driven channel flow the error fell as the square of the grid spacing (observed orders 1.95 and 1.98). With the engine's magic parameter of 1/4 the walls sit 0.0840/H nodes beyond the halfway point, independent of the viscosity; with 3/16 the error was 73 times smaller, limited by compressibility.
  2. In the lid-driven cavity at Re 1,000 the minimum centreline velocity on a 128 × 128 grid was +0.51% from the spectral benchmark, converging at an observed order of 2.6 and 2.7 to within 0.05% of it, and level with OpenFOAM at the same resolution in CFD series Part 3. This needs the lid's corner links to bounce back as from a still wall; giving them the lid's momentum left an error of +4.05% at an order of 1.3 and 1.2.
  3. For a cylinder, extrapolated to zero blockage, the drag at Re 40 was 1.586 (classic 1.522, modern 1.493) and the recirculation length 2.530 D (2.345, 2.236); at Re 100 the Strouhal number was 0.1635 (0.1643) and the drag 1.413 (1.33). Refining the cylinder would move the extrapolated drag and wake length further from the references, so the drag is good to several percent at best and the Strouhal number is the reliable result.
  4. The compressibility error was small in the cavity: lid speeds up to 0.2 moved the Re 100 result by at most 0.02%, and halving the lid speed at Re 1,000 changed it by 0.12%. Over 400 passes 17 of 31 stability runs diverged, all with τ⁺ ≤ 0.5180; every setting the live figure offers stayed stable for 400 passes, its top cylinder setting for 1,000.
  5. In headless Chromium the engine runs at 7.3 million node updates per second on the live figure's grid, enough to animate it in real time.

The engine is accurate enough for what it is used for, showing how flows behave and how a solver's answers depend on its grid and its boundaries, and its limits are stated where the figures appear.

11. Appendix: The Collide-and-Stream Step and the Test Harness

The listing below is a self-contained version of the engine's core step, cut down to the essentials: a periodic channel driven by a uniform body force, with the TRT collision of Eq. (2) and halfway bounce-back walls. It reproduces the appendix numbers in Section 4.1 (run with node lbm_appendix.js 32 0.25). The full engine adds the inlet and outlet conditions, obstacles, the sponge and the force calculation. The test harness, which reproduces every number in this paper from a clean unzip, is listed in Table 4.

Table 4. Files of the downloadable test harness.
FileWhat it does
cfd-lbm.jsThe engine, as used on the site (a copy)
lbm.js, spectrum.jsLoader for the engine; lift spectrum and Strouhal number
cases.jsEvery case of the paper: node cases.js --list, node cases.js CASE
run.pyRuns the cases one after another, skipping those already done
browser_perf.pyEngine speed in headless Chromium (Playwright)
summarise.pyReference data, errors, orders, extrapolations; writes results.json
lbm_appendix.jsThe listing below
out/, results.json, RESULTS.mdRun outputs, results and the source of every number in the text
A collide-and-stream step: D2Q9, two-relaxation-time collision and halfway bounce-back in a force-driven channel, checked against the exact profile (JavaScript, Node 16 or later).
/* The core of a lattice Boltzmann solver: one collide-and-stream step (D2Q9, two-relaxation-time collision,
   halfway bounce-back walls), run on force-driven flow between two plates and checked against the exact profile.
   A teaching version of the step in Chmltech's browser engine (assets/js/cfd-lbm.js), cut down to the essentials:
   periodic in x, a uniform body force g drives the flow, solid rows at y = 0 and y = ny - 1.
   Chmltech Online Journal, Vol. 106, Issue 27. Usage: node lbm_appendix.js [H] [magic]     (Node 16 or later) */
'use strict';
const H = +(process.argv[2] || 16), MAGIC = +(process.argv[3] || 0.25);   // nodes across the channel; magic parameter
const nx = 4, ny = H + 2, N = nx * ny;
const EX = [0, 1, 0, -1, 0, 1, -1, -1, 1], EY = [0, 0, 1, 0, -1, 1, 1, -1, -1];
const W = [4 / 9, 1 / 9, 1 / 9, 1 / 9, 1 / 9, 1 / 36, 1 / 36, 1 / 36, 1 / 36], OPP = [0, 3, 4, 1, 2, 7, 8, 5, 6];
const tauP = 0.8, nu = (tauP - 0.5) / 3;                  // viscosity in lattice units: nu = cs^2 (tau+ - 1/2), cs^2 = 1/3
const tauM = 0.5 + MAGIC / (tauP - 0.5);                  // the magic parameter fixes the second relaxation time
const wP = 1 / tauP, wM = 1 / tauM;
const umax = 0.01, g = 8 * nu * umax / (H * H);           // body force for a peak speed of 0.01 (exact solution below)
let f = new Float64Array(9 * N), ft = new Float64Array(9 * N);
const solid = (y) => y === 0 || y === ny - 1;

function feq(i, rho, ux, uy) {                            // Eq. (1): second-order equilibrium
  const eu = EX[i] * ux + EY[i] * uy;
  return W[i] * rho * (1 + 3 * eu + 4.5 * eu * eu - 1.5 * (ux * ux + uy * uy));
}
for (let k = 0; k < N; k++) for (let i = 0; i < 9; i++) f[i * N + k] = W[i];

function step() {
  for (let y = 0; y < ny; y++) for (let x = 0; x < nx; x++) {
    const k = x + y * nx; if (solid(y)) continue;
    let rho = 0, jx = 0, jy = 0;
    for (let i = 0; i < 9; i++) { const v = f[i * N + k]; rho += v; jx += v * EX[i]; jy += v * EY[i]; }
    const ux = (jx + 0.5 * g) / rho, uy = jy / rho;     // half the force added to the velocity (second-order forcing)
    for (let i = 0; i < 9; i++) {
      const j = OPP[i], fi = f[i * N + k], fj = f[j * N + k], ei = feq(i, rho, ux, uy), ej = feq(j, rho, ux, uy);
      // collide, Eq. (2): symmetric part relaxes at wP, antisymmetric part at wM
      const sym = 0.5 * (fi + fj) - 0.5 * (ei + ej), asym = 0.5 * (fi - fj) - 0.5 * (ei - ej);
      const force = 3 * W[i] * EX[i] * g * (1 - 0.5 * wM);   // body force acts on the antisymmetric (momentum) part
      let post = fi - wP * sym - wM * asym + force;
      // stream, Eq. (4): move one node in direction i; a packet that would enter a wall comes back (halfway bounce-back)
      const tx = (x + EX[i] + nx) % nx, ty = y + EY[i];
      if (solid(ty)) ft[j * N + k] = post; else ft[i * N + tx + ty * nx] = post;
    }
  }
  const t = f; f = ft; ft = t;
}

const tDiff = H * H / nu;
for (let s = 0; s < 6 * tDiff; s++) step();
// compare with the exact parabola, walls halfway between the solid and the first fluid row (y = 1/2 and y = H + 1/2)
let e2 = 0, u2 = 0;
for (let y = 1; y <= H; y++) {
  const k = 1 + y * nx; let rho = 0, jx = 0;
  for (let i = 0; i < 9; i++) { rho += f[i * N + k]; jx += f[i * N + k] * EX[i]; }
  const u = (jx + 0.5 * g) / rho, eta = (y - 0.5) / H, exact = 4 * umax * eta * (1 - eta);
  e2 += (u - exact) ** 2; u2 += exact * exact;
}
console.log(`H = ${H}, magic = ${MAGIC}: RMS error of the profile ${Math.sqrt(e2 / u2).toExponential(3)} of the RMS speed`);

References

  1. Lee, Q. F. (2026). What CFD is, and when it is worth paying for. Chmltech Insights (CFD series, Part 1). www.chmltech.com/post/what-cfd-is-and-when-it-is-worth-paying-for.html
  2. Roache, P. J. (1998). Verification and validation in computational science and engineering. Hermosa Publishers.
  3. Ghia, U., Ghia, K. N., & Shin, C. T. (1982). High-Re solutions for incompressible flow using the Navier–Stokes equations and a multigrid method. Journal of Computational Physics, 48(3), 387–411. 10.1016/0021-9991(82)90058-4
  4. Botella, O., & Peyret, R. (1998). Benchmark spectral results on the lid-driven cavity flow. Computers & Fluids, 27(4), 421–433. 10.1016/S0045-7930(98)00002-4
  5. Dennis, S. C. R., & Chang, G.-Z. (1970). Numerical solutions for steady flow past a circular cylinder at Reynolds numbers up to 100. Journal of Fluid Mechanics, 42(3), 471–489. 10.1017/S0022112070001428
  6. Williamson, C. H. K. (1996). Vortex dynamics in the cylinder wake. Annual Review of Fluid Mechanics, 28, 477–539. 10.1146/annurev.fl.28.010196.002401
  7. Posdziech, O., & Grundmann, R. (2007). A systematic approach to the numerical calculation of fundamental quantities of the two-dimensional flow over a circular cylinder. Journal of Fluids and Structures, 23(3), 479–499. 10.1016/j.jfluidstructs.2006.09.004
  8. Lee, Q. F. (2026). The mesh: Why the same problem gives different answers. Chmltech Insights (CFD series, Part 3). www.chmltech.com/post/the-cfd-mesh-why-the-same-problem-gives-different-answers.
  9. Qian, Y. H., d’Humières, D., & Lallemand, P. (1992). Lattice BGK models for Navier–Stokes equation. Europhysics Letters, 17(6), 479–484. 10.1209/0295-5075/17/6/001
  10. Krüger, T., Kusumaatmaja, H., Kuzmin, A., Shardt, O., Silva, G., & Viggen, E. M. (2017). The lattice Boltzmann method: Principles and practice. Springer. 10.1007/978-3-319-44649-3
  11. Bhatnagar, P. L., Gross, E. P., & Krook, M. (1954). A model for collision processes in gases. I. Small amplitude processes in charged and neutral one-component systems. Physical Review, 94(3), 511–525. 10.1103/PhysRev.94.511
  12. Ginzburg, I., Verhaeghe, F., & d’Humières, D. (2008). Two-relaxation-time lattice Boltzmann scheme: About parametrization, velocity, pressure and mixed boundary conditions. Communications in Computational Physics, 3(2), 427–478. global-sci.com/article/81208/two-relaxation-time-lattice-boltzmann-scheme-about-
  13. Ginzburg, I., & d’Humières, D. (2003). Multireflection boundary conditions for lattice Boltzmann models. Physical Review E, 68(6), 066614. 10.1103/PhysRevE.68.066614
  14. Ladd, A. J. C. (1994). Numerical simulations of particulate suspensions via a discretized Boltzmann equation. Part 1. Theoretical foundation. Journal of Fluid Mechanics, 271, 285–309. 10.1017/S0022112094001771
  15. Zou, Q., & He, X. (1997). On pressure and velocity boundary conditions for the lattice Boltzmann BGK model. Physics of Fluids, 9(6), 1591–1598. 10.1063/1.869307
  16. He, X., & Luo, L.-S. (1997). Theory of the lattice Boltzmann method: From the Boltzmann equation to the lattice Boltzmann equation. Physical Review E, 56(6), 6811–6817. 10.1103/PhysRevE.56.6811
  17. Celik, I. B., Ghia, U., Roache, P. J., Freitas, C. J., Coleman, H., & Raad, P. E. (2008). Procedure for estimation and reporting of uncertainty due to discretization in CFD applications. Journal of Fluids Engineering, 130(7), 078001. 10.1115/1.2960953
  18. OpenCFD Ltd. (2019). OpenFOAM v1912 user guide. www.openfoam.com/documentation/user-guide
  19. Gautier, R., Biau, D., & Lamballais, E. (2013). A reference solution of the flow over a circular cylinder at Re = 40. Computers & Fluids, 75, 103–111. arXiv:1310.6641. 10.1016/j.compfluid.2012.12.017
  20. Williamson, C. H. K. (1988). Defining a universal and continuous Strouhal–Reynolds number relationship for the laminar vortex shedding of a circular cylinder. Physics of Fluids, 31(10), 2742–2744. 10.1063/1.866978

How to cite this article

Lee, Q. F. (2026). A Flow Solver in the Browser: The Lattice Boltzmann Method Behind the Live CFD Figures. Chmltech Online Journal, 106(27). https://www.chmltech.com/post/a-flow-solver-in-the-browser-the-lattice-boltzmann-method-behind-the-live-cfd-figures

Chmltech Online Journal · Vol. 106, Issue 27

Download the PDF

A Flow Solver in the Browser: The Lattice Boltzmann Method Behind the Live CFD Figures

We use your details to record this request and may follow up about it. See our privacy notice.