Computational fluid dynamics in Python: finite-volume transport, shock capturing, incompressible flow and the lattice Boltzmann method
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¶
| Lecture | Topics |
|---|---|
| 0. Conservation laws, dimensionless groups and time-step limits | Reynolds, 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 theorem | cell-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 interface | steady 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 multigrid | the 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¶
| Lecture | Topics |
|---|---|
| 4. Convection and diffusion: the cell Péclet number and the power-law scheme | the 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 diffusion | the amplification factor; stability regions of four explicit schemes; numerical diffusion and dispersion; the modified equation |
| 6. Flux limiters and total-variation-diminishing schemes | Godunov’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 reaction | dimensional 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¶
| Lecture | Topics |
|---|---|
| 8. The Burgers equation, the Cole–Hopf solution and shock speeds | the 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 problem | characteristics 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 tube | the three wave families; the exact Riemann solution by Newton iteration; HLL with and without MUSCL reconstruction; dependence on |
Part IV. Incompressible Navier–Stokes equations¶
| Lecture | Topics |
|---|---|
| 11. Couette–Poiseuille flow and the start-up of channel flow | Couette–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 vortex | the 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 grid | checkerboard 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 cavity | the 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¶
| Lecture | Topics |
|---|---|
| 15. The lattice Boltzmann method on the D2Q9 lattice | the 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 media | flow 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 microchannel | a 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 method | Lectures 0 to 4 |
| solving pressure equations | Lecture 3, then Lectures 12 and 13 |
| transporting a scalar with sharp fronts | Lectures 4 to 7 |
| working with shocks | Lectures 8 to 10 |
| validating an incompressible solver | Lecture 14 |
| starting with the lattice Boltzmann method | Lectures 15 and 16 |
| short of time | Lectures 2, 4, 6, 8, 12 and 15 |
Companion books¶
fluidmech: fluid mechanics through worked problems.
engtransport: heat and mass transfer through worked problems.
engmaterials: computational materials science, from the unit cell to phase-field models.
engsim: molecular dynamics and Monte Carlo simulation from scratch.
engml-1-foundations, engml-2-applied and engml-3-frontier: machine learning and physics-informed modelling for chemical and materials engineers.
onsager
-variational -methods: variational methods in soft matter. error-analysis: evaluating, propagating and reporting uncertainty.
Author and licence¶
Yanwei Wang, Department of Chemical and Materials Engineering, School of Engineering, Nazarbayev
University, Astana, Kazakhstan (yanwei