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.

Computational fluid dynamics in Python: finite-volume transport, shock capturing, incompressible flow and the lattice Boltzmann method

School of Engineering, Nazarbayev University

The methods inside a flow solver, each written out in NumPy and checked against an exact solution or a published benchmark.

A flow solver is built from a small number of ideas: a grid of control volumes over which quantities are conserved, a rule for the flux through each face, a solver for the linear systems that the fluxes produce, and a way to keep the velocity field free of divergence. This book treats them one at a time, in five parts. The first part sets up finite-volume grids, solves steady diffusion, and compares five solvers for the Poisson equation. The second part adds convection: it shows why central differences fail at a large cell Péclet number, analyses stability and numerical diffusion, and introduces flux limiters. The third part treats nonlinear conservation laws with shocks: the Burgers, shallow-water and Euler equations. The fourth part solves the incompressible Navier–Stokes equations by the projection method, on collocated and staggered grids, and computes the lid-driven cavity. The fifth part introduces the lattice Boltzmann method and closes with a project that connects the modules.

Each lecture is an executed Jupyter notebook of about one hour. It states the principle and its equations, works through four short demonstrations, and then inspects a function of the package that it has used. The notebooks share a small Python package, engcfd, written to be read as much as to be used; its core needs NumPy alone. Every figure has a numbered caption, and each lecture ends with three exercises: one that verifies a result, one that looks for a way in which the method fails, and one that asks the reader to write a function. A worked solution to every exercise is given in the Worked solutions part of the book.

The test problems are chosen so that the answer is known: an exact solution, an identity that the discrete equations satisfy to rounding error, an order of convergence, or, for the lid-driven cavity, the values published by Ghia, Ghia and Shin (1982). The fluid properties in the package are representative values typed in for the examples and are not reference data. The geometry, the operating conditions and the transport closures of the closing project are assumed, and the project is a teaching model and not a validated design.

The level is that of the final undergraduate year or the first year of graduate study in chemical, mechanical or materials engineering. The lectures assume calculus, linear algebra, a first course in fluid mechanics or transport phenomena, and Python with NumPy. Symbols are collected in Notation and conventions, a plan for a semester in the syllabus, the checks of the package in the validation report, and original papers and books for further study in Suggested references.

Part I. Conservation laws, finite-volume grids and elliptic solvers

LectureTopics
0. Conservation laws, dimensionless groups and time-step limitsReynolds, Prandtl and Péclet numbers of ten fluids; the Courant and diffusion numbers; the convective and diffusive limits on an explicit time step and the grid spacing at which they cross
1. Finite-volume grids, stretched meshes and the discrete Gauss theoremcell-centered grids in one and two dimensions; tanh stretching toward one or two walls; resolving a wall layer with a fixed number of cells; the discrete Gauss theorem
2. Steady diffusion and the harmonic mean at an interfacesteady diffusion with sources; Dirichlet and Neumann boundaries; the tridiagonal solver; harmonic against arithmetic mean at an interface between two materials; variable conductivity on a stretched grid
3. Poisson solvers: SOR, conjugate gradients and multigridthe five-point Laplacian; Jacobi, red–black SOR, conjugate gradients, the fast sine transform and multigrid V-cycles; how the iteration counts grow with the grid; the optimal relaxation factor

Part II. Convection and diffusion of a scalar

LectureTopics
4. Convection and diffusion: the cell Péclet number and the power-law schemethe cell Péclet number; central, upwind, hybrid, power-law and exponential schemes; boundedness; numerical diffusion of the upwind scheme; false diffusion in two dimensions
5. Von Neumann stability, numerical dispersion and numerical diffusionthe amplification factor; stability regions of four explicit schemes; numerical diffusion and dispersion; the modified equation
6. Flux limiters and total-variation-diminishing schemesGodunov’s theorem; flux limiters and the Sweby diagram; total variation; square and smooth profiles; behavior at unit Courant number
7. Advection in two dimensions by Strang splitting, with diffusion and reactiondimensional splitting; Lie and Strang splitting; rotation of a cosine bell; a steady plume with diffusion and a first-order reaction

Part III. Nonlinear conservation laws and shock capturing

LectureTopics
8. The Burgers equation, the Cole–Hopf solution and shock speedsthe Cole–Hopf solution; the thickness of a viscous shock; the Rankine–Hugoniot condition; conservative against non-conservative form; the entropy condition at a sonic point
9. Shallow-water equations, the HLL flux and the dam-break problemcharacteristics and the Froude number; the exact solution of the dam-break problem; the HLL flux; conservation of volume
10. The Euler equations and the Sod shock tubethe three wave families; the exact Riemann solution by Newton iteration; HLL with and without MUSCL reconstruction; dependence on γ\gamma

Part IV. Incompressible Navier–Stokes equations

LectureTopics
11. Couette–Poiseuille flow and the start-up of channel flowCouette–Poiseuille profiles and back-flow; the start-up of Couette flow as a series and as an error function; the Crank–Nicolson method beyond the explicit limit
12. The projection method and the Taylor–Green vortexthe fractional-step method; the discrete wavenumbers of the pressure equation; convergence in space; decay of kinetic energy
13. Pressure–velocity coupling, checkerboard modes and the staggered gridcheckerboard pressure modes of collocated central differences; the staggered grid; solvability of the Neumann pressure problem; the projection as an orthogonal decomposition
14. The lid-driven cavitythe vorticity–streamfunction formulation; Thom’s wall vorticity; the cavity at Reynolds numbers 100 and 400; grid refinement; corner eddies

Part V. Lattice Boltzmann method and closing project

LectureTopics
15. The lattice Boltzmann method on the D2Q9 latticethe D2Q9 lattice and its equilibrium; relaxation time and viscosity; body force and bounce-back walls; the relaxation time at which the wall lies exactly halfway between nodes
16. Flow past obstacles and the permeability of porous mediaflow past a cylinder in a channel; Darcy’s law and permeability; the limits of small Reynolds and Mach number; permeability against porosity
17. Project: flow, mixing and reaction in a microchannela model microchannel reactor with assumed parameters: choice of fluid, baffles and pressure drop, conversion with wall and bulk reaction, heat conduction through a two-layer wall, conversion against pressure drop

Where to start

If you are…Read
new to the finite-volume methodLectures 0 to 4
solving pressure equationsLecture 3, then Lectures 12 and 13
transporting a scalar with sharp frontsLectures 4 to 7
working with shocksLectures 8 to 10
validating an incompressible solverLecture 14
starting with the lattice Boltzmann methodLectures 15 and 16
short of timeLectures 2, 4, 6, 8, 12 and 15

Companion books

Author and licence

Yanwei Wang, Department of Chemical and Materials Engineering, School of Engineering, Nazarbayev University, Astana, Kazakhstan (yanwei.wang@nu.edu.kz, ORCID 0000-0002-8488-9833). The code is under the MIT licence; the text, figures and notebook outputs are under CC BY 4.0. The source is at github.com/ktwyw/engcfd.