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.

3. Poisson solvers: SOR, conjugate gradients and multigrid

School of Engineering, Nazarbayev University

In the projection methods for incompressible flow (Lectures 12 to 14), every time step requires the solution of an elliptic pressure Poisson equation −∇2p=f-\nabla^2 p = f, and this solution usually takes most of the computing time. If you use classical Jacobi relaxation, doubling the grid resolution in 2-D (N→2NN \to 2N, 4×4\times as many unknowns) multiplies the iteration count by 4×4\times (O(N2)O(N^2) iterations, O(N4)O(N^4) total work) because local five-point averaging removes smooth, long-wavelength error very slowly. In this lecture, we benchmark five solvers in elliptic_multigrid, Jacobi, Young’s optimal Red–Black SOR, matrix-free Conjugate Gradient (Dirichlet & singular pure-Neumann), exact O(N2log⁡N)O(N^2\log N) Fast Sine Transform, and recursive Geometric Multigrid V-Cycles achieving mesh-independent ρ<0.12\rho < 0.12 convergence across 172 through 1292 grids.

Learning objectives

  1. Formulate the 5-point discrete Laplacian −∇h2u=f-\nabla_h^2 u = f and verify O(h2)O(h^2) truncation convergence using the direct spectral solver poisson_2d_fast_sine.

  2. Compare spectral-radius convergence scaling of poisson_2d_jacobi (O(N2)O(N^2)), Young’s optimal Red–Black poisson_2d_sor (O(N)O(N)), and matrix-free poisson_2d_cg.

  3. Resolve the nullspace compatibility condition of singular pure-Neumann pressure-Poisson systems via zero-mean gauge projection in poisson_2d_cg(..., neumann=True).

  4. Demonstrate mesh-independent O(1)O(1)-cycle convergence (ρ<0.12\rho < 0.12) across 172,332,652,129217^2, 33^2, 65^2, 129^2 dyadic grids using poisson_2d_multigrid_vcycle.

Time: about 60 min

1. The 5-Point Discrete Poisson Operator & Spectral Fast Sine Solver

1.1 Discrete Eigenfunctions & Eigenvalues on a Vertex Grid

On a uniform Nx×NyN_x \times N_y vertex grid covering [0,Lx]×[0,Ly][0, L_x]\times[0, L_y] with hx=Lx/(Nx−1)h_x = L_x/(N_x - 1) and hy=Ly/(Ny−1)h_y = L_y/(N_y - 1), the 5-point finite-difference operator −∇h2u=f-\nabla_h^2 u = f at interior vertex (i,j)∈{1,…,Nx−2}×{1,…,Ny−2}(i, j) \in \{1,\dots,N_x-2\}\times\{1,\dots,N_y-2\} is:

(−∇h2u)i,j=2ui,j−ui−1,j−ui+1,jhx2+2ui,j−ui,j−1−ui,j+1hy2=fi,j(1.1)(-\nabla_h^2 u)_{i,j} = \frac{2u_{i,j} - u_{i-1,j} - u_{i+1,j}}{h_x^2} + \frac{2u_{i,j} - u_{i,j-1} - u_{i,j+1}}{h_y^2} = f_{i,j} \tag{1.1}

With homogeneous Dirichlet walls (u=0u = 0 on ∂Ω\partial\Omega), the sine modes ϕp,q(xi,yj)=sin⁡(pπxi/Lx)sin⁡(qπyj/Ly)\phi_{p,q}(x_i, y_j) = \sin(p\pi x_i/L_x)\sin(q\pi y_j/L_y) diagonalize −∇h2-\nabla_h^2 exactly with discrete eigenvalues:

λp,qh=4hx2sin⁡2 ⁣(pπhx2Lx)+4hy2sin⁡2 ⁣(qπhy2Ly)→h→0π2 ⁣(p2Lx2+q2Ly2)+O(h2)(1.2)\lambda_{p,q}^h = \frac{4}{h_x^2}\sin^2\!\left(\frac{p\pi h_x}{2L_x}\right) + \frac{4}{h_y^2}\sin^2\!\left(\frac{q\pi h_y}{2L_y}\right) \xrightarrow{h \to 0} \pi^2\!\left(\frac{p^2}{L_x^2} + \frac{q^2}{L_y^2}\right) + O(h^2) \tag{1.2}

elliptic_multigrid.poisson_2d_fast_sine(rhs_interior, dx, dy) applies the 2-D Discrete Sine Transform (DST-I via FFT), divides by λp,qh\lambda_{p,q}^h, and transforms back in O(N2log⁡N)O(N^2 \log N) time with zero iterations.


Demonstration 1: Fast Sine Direct Solver & O(h^2) Truncation Convergence
========================================================================
Grid  17x17  (h = 1/16 ) | Discrete algebraic residual = 2.13e-13 | PDE error ||u_h - u||_inf = 1.0989e-02
Grid  33x33  (h = 1/32 ) | Discrete algebraic residual = 9.81e-13 | PDE error ||u_h - u||_inf = 2.7350e-03
Grid  65x65  (h = 1/64 ) | Discrete algebraic residual = 3.62e-12 | PDE error ||u_h - u||_inf = 6.8297e-04
Grid 129x129 (h = 1/128) | Discrete algebraic residual = 1.58e-11 | PDE error ||u_h - u||_inf = 1.7069e-04
Observed PDE convergence orders: [2.0065, 2.0016, 2.0004]
<Figure size 1144x462 with 4 Axes>

Figure 3.1. A Poisson problem −∇2u=f-\nabla^2 u = f with u=0u = 0 on the boundary, solved with the fast sine transform on a 65×6565 \times 65 grid. (a) A localized source. (b) The solution, which is much smoother than the source and extends over the whole domain.

What Demonstration 1 shows. Two errors must be distinguished: the algebraic residual (<10−12< 10^{-12}, meaning poisson_2d_fast_sine solved the discrete linear system Ahuh=fhA_h u_h = f_h to rounding error) and PDE discretization error ∥uh−uPDE∥∞∝h2\|u_h - u_{\mathrm{PDE}}\|_\infty \propto h^2 (it falls by a factor close to 4 whenever hh is halved).


2. Relaxation vs. Krylov Solvers: Jacobi, Optimal Red–Black SOR & Conjugate Gradient

When irregular geometry or variable density ∇⋅(1ρ∇p)\nabla\cdot(\frac{1}{\rho}\nabla p) precludes FFTs, we turn to iterative solvers:

  1. Jacobi (poisson_2d_jacobi): Spectral radius ρJac=cos⁡(πh/L)≈1−12π2h2  ⟹  \rho_{\mathrm{Jac}} = \cos(\pi h/L) \approx 1 - \frac{1}{2}\pi^2 h^2 \implies requires O(N2)O(N^2) iterations.

  2. Young’s Optimal Red–Black SOR (poisson_2d_sor): Colors vertices like a checkerboard (i + j) % 2 for full NumPy vectorization and applies Young’s exact optimal over-relaxation factor:

ωopt=21+1−ρJac2≈2−2πhL,ρSOR(ωopt)=ωopt−1≈1−2πhL  ⟹  O(N) iterations!(2.1)\omega_{\mathrm{opt}} = \frac{2}{1 + \sqrt{1 - \rho_{\mathrm{Jac}}^2}} \approx 2 - \frac{2\pi h}{L}, \qquad \rho_{\mathrm{SOR}}(\omega_{\mathrm{opt}}) = \omega_{\mathrm{opt}} - 1 \approx 1 - \frac{2\pi h}{L} \implies O(N)\ \text{iterations!} \tag{2.1}
  1. Matrix-Free Conjugate Gradient (poisson_2d_cg): Minimizes the SPD energy norm ∥ek∥A\|e_k\|_A over Krylov subspaces in O(N)O(N) iterations without tuning ω\omega, and solves singular pure-Neumann pressure equations (neumann=True) by projecting both RHS and iterates onto the zero-mean subspace ∑i,jui,j=0\sum_{i,j} u_{i,j} = 0.


Demonstration 2: Comparing Jacobi, Optimal Red-Black SOR, and CG (Dirichlet & Pure Neumann) on 33x33
====================================================================================================
Jacobi        : 5001 iters | max diff vs FastSine = 5.07e-11
Optimal SOR   :  156 iters (omega_opt = 1.8215) | max diff = 2.43e-12
Conjugate Grad:  108 iters | max diff vs FastSine = 7.18e-13
Multigrid V(2,2):  9 V-cycles (rho = 0.0542) | max diff = 1.84e-11
Pure-Neumann CG (32x32): 150 iters, mean(u) = +1.39e-17, final res = 9.65e-12

What Demonstration 2 shows. On the 33×3333 \times 33 mesh, Jacobi needs 5001 sweeps to bring the residual below 10-9, red–black SOR with the optimal factor (ωopt=1.8215\omega_{\mathrm{opt}} = 1.8215) needs 156 sweeps (32 times fewer), conjugate gradients need 108 iterations and multigrid needs 9 V-cycles. The right-hand side contains a localized source so that many eigenmodes are excited; conjugate gradients would otherwise finish in as many iterations as there are distinct eigenvalues in the right-hand side.


3. Geometric Multigrid V-Cycles: Mesh-Independent O(Ndof)O(N_{\mathrm{dof}}) Convergence

Why does Multigrid win so decisively as N→∞N \to \infty?

  1. High-frequency smoothing: 2 sweeps of Red–Black Gauss–Seidel rapidly damp oscillatory error modes (kh∈[π/2,π]k h \in [\pi/2, \pi]) on grid hh.

  2. Coarse-grid aliasing (_restrict_full_weighting): Smooth error on grid hh appears twice as oscillatory (2kh2k h) when restricted to coarse grid 2h2h, recursive down to the 3×33 \times 3 single-interior-node grid where (4/H2)e1,1=r1,1(4/H^2) e_{1,1} = r_{1,1} is solved directly in 1 division.

  3. Bilinear prolongation (_prolong_bilinear) & post-smoothing: Interpolates the coarse error correction back to grid hh and cleans up interpolation ripples.

We benchmark iteration/cycle scaling across four dyadic grids: 172,332,652,129217^2, 33^2, 65^2, 129^2.


Demonstration 3: Mesh-Refinement Scaling Benchmark Across 17^2, 33^2, 65^2, and 129^2 Grids
===========================================================================================
Grid      |    DOFs | Jacobi(th) |  Opt SOR |     CG | MG V(2,2) |   MG rho
------------------------------------------------------------------------
 17x17    |     225 |       1187 |       70 |     48 |         8 |   0.0575
 33x33    |     961 |       4771 |      144 |    101 |         9 |   0.0605
 65x65    |    3969 |      19105 |      295 |    209 |         9 |   0.0623
129x129   |   16129 |      76441 |      604 |    423 |         9 |   0.0634
<Figure size 1232x473 with 2 Axes>

Figure 3.2. (a) Largest residual against the iteration number on the 33×3333 \times 33 grid for Jacobi, optimal SOR, conjugate gradients and multigrid V-cycles. (b) Number of iterations or V-cycles needed against the number of grid points per axis; the Jacobi values are the estimate from its spectral radius.

What Demonstration 3 shows. Refining the mesh by a factor of 8 per axis, from 172 (225 unknowns) to 1292 (16 12916\,129 unknowns), raises the estimated number of Jacobi sweeps from 1187 to 76 44176\,441, the SOR sweeps from 70 to 604 and the conjugate-gradient iterations from 48 to 423. Multigrid needs 8 or 9 V-cycles on every grid (panel (b)); its convergence factor stays between 0.058 and 0.063, so each V-cycle lowers the residual by more than a factor of ten whatever the grid size.


4. SOR Sensitivity Cliff Around ωopt\omega_{\mathrm{opt}} & Dyadic Guards

What happens if we guess ω\omega slightly too low or too high in poisson_2d_sor instead of using Young’s theoretical ωopt\omega_{\mathrm{opt}}? We sweep ω∈[1.0,1.95]\omega \in [1.0, 1.95] on 33×3333 \times 33 (ωopt=1.8215\omega_{\mathrm{opt}} = 1.8215) and verify the dyadic (2k+1)(2^k + 1) grid check in poisson_2d_multigrid_vcycle.


Demonstration 4: SOR Relaxation-Factor Cliff Around Young's Optimum & Multigrid Guards
======================================================================================
Theoretical Young optimum on 33x33: omega_opt = 1.82147
omega = 1.00000 -> 2573 iterations <-- Gauss-Seidel
omega = 1.40000 -> 1114 iterations
omega = 1.70000 ->  440 iterations
omega = 1.82147 ->  156 iterations <-- Young Optimum
omega = 1.88000 ->  212 iterations
omega = 1.95000 ->  530 iterations

Inside the algorithm

Geometric multigrid relies on the variational property of its transfer operators: full-weighting restriction Ih2hI_h^{2h} (_restrict_full_weighting) is the exact scaled transpose of bilinear prolongation I2hhI_{2h}^h (_prolong_bilinear) with respect to the discrete L2L^2 inner product:

⟨Ih2hrh, e2h⟩2h=⟨rh, I2hhe2h⟩h,⟨u,v⟩H≡H2∑i,jintui,jvi,j(4.1)\langle I_h^{2h} r_h,\, e_{2h} \rangle_{2h} = \langle r_h,\, I_{2h}^h e_{2h} \rangle_h, \qquad \langle u, v \rangle_H \equiv H^2 \sum_{i,j}^{\mathrm{int}} u_{i,j} v_{i,j} \tag{4.1}

Below we inspect both transfer operators and verify Equation (4.1) to rounding error (<10−14< 10^{-14}).

Check against the library


Inside the Algorithm: Full-Weighting Restriction == Adjoint of Bilinear Prolongation
====================================================================================
def _restrict_full_weighting(res_fine: np.ndarray) -> np.ndarray:
    """Full-weighting restriction from (2^k + 1) x (2^k + 1) fine vertex grid to (2^{k-1} + 1) coarse grid."""
    nxf, nyf = res_fine.shape
    nxc, nyc = (nxf - 1) // 2 + 1, (nyf - 1) // 2 + 1
    rc = np.zeros((nxc, nyc), dtype=float)
    rc[1:-1, 1:-1] = (
        4.0 * res_fine[2:-2:2, 2:-2:2]
        + 2.0
        * (
            res_fine[1:-3:2, 2:-2:2]
            + res_fine[3:-1:2, 2:-2:2]
            + res_fine[2:-2:2, 1:-3:2]
            + res_fine[2:-2:2, 3:-1:2]
        )
        + (
            res_fine[1:-3:2, 1:-3:2]
            + res_fine[1:-3:2, 3:-1:2]
            + res_fine[3:-1:2, 1:-3:2]
            + res_fine[3:-1:2, 3:-1:2]
        )
    ) / 16.0
    return rc

def _prolong_bilinear(err_coarse: np.ndarray) -> np.ndarray:
    """Bilinear prolongation from (2^{k-1} + 1) coarse vertex grid to (2^k + 1) fine vertex grid."""
    nxc, nyc = err_coarse.shape
    nxf, nyf = 2 * (nxc - 1) + 1, 2 * (nyc - 1) + 1
    ef = np.zeros((nxf, nyf), dtype=float)
    ef[0::2, 0::2] = err_coarse
    ef[1::2, 0::2] = 0.5 * (err_coarse[:-1, :] + err_coarse[1:, :])
    ef[0::2, 1::2] = 0.5 * (err_coarse[:, :-1] + err_coarse[:, 1:])
    ef[1::2, 1::2] = 0.25 * (
        err_coarse[:-1, :-1] + err_coarse[1:, :-1] + err_coarse[:-1, 1:] + err_coarse[1:, 1:]
    )
    return ef

<I_h^2h r_h, e_2h>_2h = +0.00032483723828
<r_h, I_2h^h e_2h>_h  = +0.00032483723828 | |diff| = 8.67e-18

Exercises

Worked solutions are in 03_solutions.ipynb. The cell below the list is a short self-check to start from.

  1. Verify a result. On a 33×3333 \times 33 grid (h=1/32h = 1/32), verify that poisson_2d_fast_sine recovers v3,5=sin⁡(3πx)sin⁡(5πy)v_{3,5} = \sin(3\pi x)\sin(5\pi y) to <10−14< 10^{-14} when forced by λ3,5hv3,5\lambda_{3,5}^h v_{3,5}, and verify the exact ratio λ3,5PDE/λ3,5h\lambda_{3,5}^{\mathrm{PDE}} / \lambda_{3,5}^h when forced by (32+52)π2v3,5(3^2 + 5^2)\pi^2 v_{3,5}.

  2. Find how it fails. Demonstrate Ostrowski–Reich instability of poisson_2d_sor at ω=2.04>2\omega = 2.04 > 2 on 17×1717 \times 17, show algebraically on random arrays that the discrete pure-Neumann Laplacian sums to zero (∑i,j(−∇h2u)i,j=0\sum_{i,j} (-\nabla_h^2 u)_{i,j} = 0), and verify zero-mean gauge projection in poisson_2d_cg(..., neumann=True).

  3. Implement it yourself. Implement audit_smoother_frequency_selectivity(n_pts=33, n_sweeps=10) applying Red–Black Gauss–Seidel (_rbgs_smooth) to −∇h2e=0-\nabla_h^2 e = 0 from initial error e(0)=sin⁡(kπx)sin⁡(kπy)e^{(0)} = \sin(k\pi x)\sin(k\pi y) for smooth k=1k = 1 vs. oscillatory k=16k = 16, and show that 10 sweeps damp k=16k = 16 by >105×> 10^5\times while leaving >90%> 90\% of k=1k = 1 untouched.


Self-check for the exercises
============================
Discrete eigenmode (3,5) recovered to 1.55e-15
All checks passed.