Partial differential equations (PDEs) are central to modeling phenomena in physics, engineering, and applied sciences, including heat transfer, fluid dynamics, elasticity, and diffusion processes.
Solving these equations numerically is a core task in scientific computing, and the finite element method (FEM) is one of the most widely used approaches.
Python has become a common language in this area due to its readability and the availability of high-level scientific libraries, making it accessible to both students and researchers.
About the book
Solving PDEs in Python: The FEniCS Tutorial I by Hans Petter Langtangen and Anders Logg provides a concise introduction to finite element programming using the FEniCS software library. The book focuses exclusively on Python, presenting it as the most accessible entry point for beginners working with FEniCS.
It is intended for readers with basic Python programming skills and some background knowledge of mathematics, particularly differential equations and the finite element method. The tutorial is designed as a compact learning resource and can be used for self-study or as material for a focused course or research seminar.
What you will learn
Readers will learn how to formulate and solve partial differential equations using the finite element method in Python with FEniCS. The book explains how to translate mathematical PDE formulations into variational forms and implement them in code. It covers practical aspects such as mesh generation, boundary conditions, solver configuration, postprocessing, and error analysis.
Through worked examples, readers gain experience solving common PDEs, including the Poisson equation, heat equation, elasticity problems, Navier–Stokes equations, and coupled systems. The content prepares readers to work with more advanced FEniCS documentation and example programs.
Table of contents
- Preface
- 1 Preliminaries
- 1.1 The FEniCS Project
- 1.2 What you will learn
- 1.3 Working with this tutorial
- 1.4 Obtaining the software
- 1.4.1 Installation using Docker containers
- 1.4.2 Installation using Ubuntu packages
- 1.4.3 Testing your installation
- 1.5 Obtaining the tutorial examples
- 1.6 Background knowledge
- 1.6.1 Programming in Python
- 1.6.2 The finite element method
- 2 Fundamentals: Solving the Poisson equation
- 2.1 Mathematical problem formulation
- 2.1.1 Finite element variational formulation
- 2.1.2 Abstract finite element variational formulation
- 2.1.3 Choosing a test problem
- 2.2 FEniCS implementation
- 2.2.1 The complete program
- 2.2.2 Running the program
- 2.3 Dissection of the program
- 2.3.1 The important first line
- 2.3.2 Generating simple meshes
- 2.3.3 Defining the finite element function space
- 2.3.4 Defining the trial and test functions
- 2.3.5 Defining the boundary conditions
- 2.3.6 Defining the source term
- 2.3.7 Defining the variational problem
- 2.3.8 Forming and solving the linear system
- 2.3.9 Plotting the solution using the plot command
- 2.3.10 Plotting the solution using ParaView
- 2.3.11 Computing the error
- 2.3.12 Examining degrees of freedom and vertex values
- 2.4 Deflection of a membrane
- 2.4.1 Scaling the equation
- 2.4.2 Defining the mesh
- 2.4.3 Defining the load
- 2.4.4 Defining the variational problem
- 2.4.5 Plotting the solution
- 2.4.6 Making curve plots through the domain
- 2.1 Mathematical problem formulation
- 3 A Gallery of finite element solvers
- 3.1 The heat equation
- 3.1.1 PDE problem
- 3.1.2 Variational formulation
- 3.1.3 FEniCS implementation
- 3.2 A nonlinear Poisson equation
- 3.2.1 PDE problem
- 3.2.2 Variational formulation
- 3.2.3 FEniCS implementation
- 3.3 The equations of linear elasticity
- 3.3.1 PDE problem
- 3.3.2 Variational formulation
- 3.3.3 FEniCS implementation
- 3.4 The Navier–Stokes equations
- 3.4.1 PDE problem
- 3.4.2 Variational formulation
- 3.4.3 FEniCS implementation
- 3.5 A system of advection–diffusion–reaction equations
- 3.5.1 PDE problem
- 3.5.2 Variational formulation
- 3.5.3 FEniCS implementation
- 3.1 The heat equation
- 4 Subdomains and boundary conditions
- 4.1 Combining Dirichlet and Neumann conditions
- 4.1.1 PDE problem
- 4.1.2 Variational formulation
- 4.1.3 FEniCS implementation
- 4.2 Setting multiple Dirichlet conditions
- 4.3 Defining subdomains for different materials
- 4.3.1 Using expressions to define subdomains
- 4.3.2 Using mesh functions to define subdomains
- 4.3.3 Using C++ code snippets to define subdomains
- 4.4 Setting multiple Dirichlet, Neumann, and Robin conditions
- 4.4.1 Three types of boundary conditions
- 4.4.2 PDE problem
- 4.4.3 Variational formulation
- 4.4.4 FEniCS implementation
- 4.4.5 Test problem
- 4.4.6 Debugging boundary conditions
- 4.5 Generating meshes with subdomains
- 4.5.1 PDE problem
- 4.5.2 Variational formulation
- 4.5.3 FEniCS implementation
- 4.1 Combining Dirichlet and Neumann conditions
- 5 Extensions: Improving the Poisson solver
- 5.1 Refactoring the Poisson solver
- 5.1.1 A more general solver function
- 5.1.2 Writing the solver as a Python module
- 5.1.3 Verification and unit tests
- 5.1.4 Parameterizing the number of space dimensions
- 5.2 Working with linear solvers
- 5.2.1 Choosing a linear solver and preconditioner
- 5.2.2 Choosing a linear algebra backend
- 5.2.3 Setting solver parameters
- 5.2.4 An extended solver function
- 5.2.5 A remark regarding unit tests
- 5.2.6 List of linear solver methods and preconditioners
- 5.3 High-level and low-level solver interfaces
- 5.3.1 Linear variational problem and solver objects
- 5.3.2 Explicit assembly and solve
- 5.3.3 Examining matrix and vector values
- 5.4 Degrees of freedom and function evaluation
- 5.4.1 Examining the degrees of freedom
- 5.4.2 Setting the degrees of freedom
- 5.4.3 Function evaluation
- 5.5 Postprocessing computations
- 5.5.1 Test problem
- 5.5.2 Flux computations
- 5.5.3 Computing functionals
- 5.5.4 Computing convergence rates
- 5.5.5 Taking advantage of structured mesh data
- 5.6 Taking the next step
- 5.1 Refactoring the Poisson solver
- References
- Index
Book details
- Title: Solving PDEs in Python: The FEniCS Tutorial I
- Author(s): Hans Petter Langtangen, Anders Logg
- Main category: Programming
- Subcategory: Python
- License: Creative Commons Attribution 4.0 International (CC BY 4.0)
More books by language: Programming, Python
Legal notice: This book is shared for educational purposes only. The content is distributed under Creative Commons licenses or with explicit permission from the author. FreeProgrammingBooks does not host copyrighted material.