A modern approach to learning finite difference methods for partial differential equations, featuring Devito for high-performance PDE solvers.
Based on Finite Difference Computing with Partial Differential Equations by Hans Petter Langtangen and Svein Linge, this edition has been modernized with:
- Quarto for document generation (replacing DocOnce)
- Devito DSL for symbolic PDE specification and automatic code generation
- Modern Python practices with type hints, testing, and CI/CD
Devito is a domain-specific language (DSL) embedded in Python for solving PDEs using finite differences. Instead of manually implementing stencil operations, you write mathematical expressions symbolically and Devito generates optimized C code:
import numpy as np from devito import Constant, Eq, Grid, Operator, TimeFunction, solve # Define computational grid grid = Grid(shape=(101,), extent=(1.0,)) # Create field with time derivative capability u = TimeFunction(name='u', grid=grid, time_order=2, space_order=2) # Wave speed parameter (passed at runtime) c = Constant(name="c") # Set an initial condition (Gaussian pulse) x = np.linspace(0.0, 1.0, 101) u.data[0, :] = np.exp(-((x - 0.5) ** 2) / (2 * 0.1**2)) u.data[1, :] = u.data[0, :] # zero initial velocity (demo) # Write the wave equation symbolically and derive an explicit update stencil pde = Eq(u.dt2, c**2 * u.dx2) update = Eq(u.forward, solve(pde, u.forward)) # Devito generates optimized C code automatically op = Operator([update]) op.apply(time_M=100, dt=0.001, c=1.0)# Clone the repository git clone https://github.com/devitocodes/devito_book.git cd devito_book # Create virtual environment and install dependencies python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -e ".[devito]" # Build the book quarto render --to pdfpip install -e ".[devito]" # With Devito support pip install -e ".[devito-petsc]" # With PETSc for implicit schemes pip install -e ".[dev]" # Development toolsInstall Quarto:
# macOS brew install quarto # Ubuntu/Debian - see https://quarto.org/docs/get-started/ for current version # Download the .deb from https://github.com/quarto-dev/quarto-cli/releases/latest sudo dpkg -i quarto-*.deb# macOS brew install --cask mactex # Ubuntu/Debian sudo apt-get install texlive-fullquarto render --to pdf # PDF only quarto render # All formats (HTML + PDF) quarto preview # Live preview with hot reloadOutput: _book/Finite-Difference-Computing-with-PDEs.pdf
pytest tests/ -v # All tests pytest tests/ -v -m "not devito" # Skip Devito tests pytest tests/ -v -m devito # Devito tests only- Introduction to Devito - DSL concepts, Grid, Function, TimeFunction, Operator
- Wave Equations - 1D/2D wave propagation, absorbing boundaries, sources
- Diffusion Equations - Heat equation, stability analysis, 2D extension
- Advection Equations - Upwind schemes, Lax-Wendroff, CFL condition
- Nonlinear Problems - Operator splitting, Burgers' equation, Picard iteration
- Finite difference formulas and derivations
- Truncation error analysis
- Software engineering practices
devito_book/ ├── src/ # Python solvers and utilities │ ├── wave/ # Wave equation solvers (Devito) │ ├── diffu/ # Diffusion solvers (Devito) │ ├── advec/ # Advection solvers (Devito) │ ├── nonlin/ # Nonlinear solvers (Devito) │ ├── operators.py # FD operators with symbolic derivation │ └── verification.py # Symbolic verification utilities ├── tests/ # Pytest test suite ├── chapters/ # Quarto book chapters │ ├── devito_intro/ # Introduction to Devito │ ├── wave/ # Wave equations │ ├── diffu/ # Diffusion equations │ ├── advec/ # Advection equations │ ├── nonlin/ # Nonlinear problems │ └── appendices/ # Reference material ├── _quarto.yml # Book configuration └── pyproject.toml # Python package configuration - Fork the repository
- Create a feature branch:
git checkout -b feature/improvement - Install dev dependencies:
pip install -e ".[dev]" - Install pre-commit hooks:
pre-commit install - Make changes and run tests:
pytest tests/ -v - Verify the build:
quarto render - Submit a Pull Request
Pre-commit hooks enforce:
- ruff - Linting
- isort - Import sorting
- typos - Spell checking
- markdownlint - Markdown formatting
This work is adapted from:
Langtangen, H.P., Linge, S. (2017). Finite Difference Computing with PDEs: A Modern Software Approach. Springer, Cham. DOI: 10.1007/978-3-319-55456-3
Licensed under CC BY 4.0.
Original Authors:
- Hans Petter Langtangen (1962-2016)
- Svein Linge
Adapted by:
- Gerard J. Gorman, Imperial College London