Week 5: The 2D Poisson equation
\[ \renewcommand{\vec}[1]{\boldsymbol{#1}} \newcommand{\td}[2]{\frac{\mathrm{d}#1}{\mathrm{d}#2}} \newcommand{\tdd}[2]{\frac{\mathrm{d}^2#1}{\mathrm{d}#2^2}} \newcommand{\pd}[2]{\frac{\partial#1}{\partial#2}} \newcommand{\pdd}[2]{\frac{\partial^2#1}{\partial#2^2}} \]
Overview
This week, we will use finite differences to solve PDEs in two dimensions. Particular focus will be placed on solving Poisson’s equation. For 2D problems, the number of unknowns and hence the size of the linear systems quickly grow as the number of grid points is increased. Sparse matrices, which only store the non-zero elements of a matrix, can lead to substantial reductions in the memory that needed to numerically solve the PDE, especially for 2D problems.
Supplementary material
Exercises
For the exercises below, you can use the example code for solving Poisson’s equation as a starting point. Alternatively, you can build your own code from scratch to solve the problem using linear algebra functions.
Solve the Poisson equation \[ \nabla^2 u + 2 = 0 \] on the domain \(0 \leq x \leq 1\) and \(1 \leq y \leq 4\). Assume that \(u = 0\) on the boundaries.
Use memory profiling to determine how much memory is required to solve the problem in Exercise 1. How does the memory usage depend on the number of grid points?
Generalise your code from Exercise 1 so that it can solve problems of the form \[ D \nabla^2 u + q(x,y) = 0. \] To validate your code, set \[ q(x,y) = D \pi^2 \left[\frac{1}{(b-a)^2} + \frac{1}{(d-c)^2}\right]\sin\left[\frac{\pi(x-a)}{b-a}\right]\sin\left[\frac{\pi(y-c)}{d-c}\right]; \] in this case, the exact solution to the problem is given by \[ u(x,y) = \sin\left[\frac{\pi(x-a)}{b-a}\right]\sin\left[\frac{\pi(y-c)}{d-c}\right]. \]
Further generalise your code so that non-homogeneous Dirichlet boundary conditions can be used. For example, solve the Poisson equation for Exercise 1 but replace the boundary condition at \(x = a\) with \(u(a, y) = \sin(\pi(y-c)/(d-c))\).
Use sparse matrices to solve the 1D Poisson equation given by \[ \tdd{u}{x} + 1 = 0 \] with \(u(0) = u(1) = 0\).
- First use 101 grid points (\(N = 100\)). Time your code and profile its memory usage. Is your sparse code faster and more memory efficient than your previous code?
- Repeat part (a) but now use \(N = 1000\). If you have correctly implemented sparse matrices, then you should now see a significant speed up and memory reduction.
Solve the 2D Poisson equation with sparse matrices. Use memory profiling to examine the memory saved by using sparse matrices.