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.

Step 9 -- 2D Laplace Equation

Authors
Affiliations
University of Valencia

The Laplace equation is the prototypical elliptic PDE:

∇2ϕ=0\nabla^2 \phi = 0

Unlike steps 1--8, this is not a time-dependent initial-value problem. It is a boundary-value problem (BVP): given boundary conditions on the domain edges, find the unique harmonic function ϕ\phi that satisfies ∇2ϕ=0\nabla^2 \phi = 0 everywhere inside.

Physics: The Laplace equation governs steady-state heat conduction (no sources), electrostatics in free space, and potential flow. It is the homogeneous form of the Poisson equation we tackle in Step 10.

What you will learn:

  1. How to use the PoissonSolver2D spectral solver for elliptic problems

  2. That a zero RHS with homogeneous Dirichlet BCs gives the trivial solution

  3. How to verify the solver against a known analytical solution

  4. How the DST-based spectral solver works

Arakawa C-Grid and Boundary Conditions

The solver operates on a 2D Arakawa C-grid where scalar fields (temperature, pressure, streamfunction) live at cell centres and velocity components live on cell faces:

       V_{j+1/2}
    ┌─────●─────┐
    │           │
 U  ●     T     ●  U
i-1/2   (i,j)    i+1/2
    │           │
    └─────●─────┘
       V_{j-1/2}

The spectral solver operates on interior cells only. The boundary condition type determines which spectral transform is used.

Dirichlet BCs (ϕ=0\phi = 0 on boundary): The DST (Discrete Sine Transform) automatically enforces zero values at the domain edges. Ghost cells are set to the negative of the nearest interior cell: ghost = -interior (antisymmetric).

1. The trivial case: zero RHS

With homogeneous Dirichlet boundary conditions (ϕ=0\phi = 0 on all walls) and zero right-hand side, the only solution is ϕ=0\phi = 0 everywhere. This is a sanity check for the solver.

Grid shape: (66, 66)
Max |phi| on interior: 0.00e+00

As expected the interior solution is machine-zero. The solver satisfies the trivial Laplace equation exactly.

2. Non-trivial case: manufactured solution

To test the solver on a non-trivial problem, we use the method of manufactured solutions. Pick an analytical solution that satisfies homogeneous Dirichlet BCs on [0,1]2[0, 1]^2:

ϕexact(x,y)=sin⁡(πx) sin⁡(πy)\phi_{\text{exact}}(x, y) = \sin(\pi x)\,\sin(\pi y)

This vanishes on all four walls. Apply the Laplacian:

∇2ϕexact=−π2sin⁡(πx)sin⁡(πy)−π2sin⁡(πx)sin⁡(πy)=−2π2sin⁡(πx)sin⁡(πy)\nabla^2 \phi_{\text{exact}} = -\pi^2 \sin(\pi x)\sin(\pi y) - \pi^2 \sin(\pi x)\sin(\pi y) = -2\pi^2 \sin(\pi x)\sin(\pi y)

So we solve ∇2ϕ=f\nabla^2 \phi = f with f=−2π2sin⁡(πx)sin⁡(πy)f = -2\pi^2 \sin(\pi x)\sin(\pi y) and check that the numerical solution matches ϕexact\phi_{\text{exact}}.

Strictly speaking this is a Poisson equation (non-zero RHS). The key point is that the solver is the same -- the Laplace equation is just the special case f=0f = 0.

L2  error (interior): 5.585729e-02
Linf error (interior): 9.197066e-02

3. Visualise solution and error

<Figure size 1500x400 with 6 Axes>

4. Higher-mode test

The DST solver handles any combination of Fourier modes. Let us test a higher-mode solution ϕ=sin⁡(2πx)sin⁡(3πy)\phi = \sin(2\pi x)\sin(3\pi y) whose Laplacian is −π2(4+9)sin⁡(2πx)sin⁡(3πy)=−13π2sin⁡(2πx)sin⁡(3πy)-\pi^2(4 + 9)\sin(2\pi x)\sin(3\pi y) = -13\pi^2\sin(2\pi x)\sin(3\pi y).

Higher-mode L2 error: 7.211602e-02
<Figure size 1000x400 with 4 Axes>

5. Convergence with grid refinement

We verify that the solver converges as O(Δx2)O(\Delta x^2) by solving the same manufactured problem at increasing resolution.

n=  16  L2 error = 2.046900e-01
n=  32  L2 error = 1.088572e-01
n=  64  L2 error = 5.585729e-02
n= 128  L2 error = 2.825762e-02
<Figure size 600x400 with 1 Axes>

6. How the DST solver works

Under the hood, PoissonSolver2D with bc="dirichlet" uses the Discrete Sine Transform (DST). The idea:

  1. Eigenfunction expansion. The eigenfunctions of the Laplacian on [0,L][0, L] with homogeneous Dirichlet BCs are sin⁡(kπx/L)\sin(k\pi x / L). In 2D the eigenfunctions are products sin⁡(mπx/Lx) sin⁡(nπy/Ly)\sin(m\pi x / L_x)\,\sin(n\pi y / L_y).

  2. Transform to spectral space. The DST decomposes both the RHS ff and the unknown ϕ\phi into these eigenfunctions.

  3. Algebraic solve. In spectral space the Laplacian is diagonal: each mode (m,n)(m, n) satisfies ϕ^mn=f^mn/λmn\hat{\phi}_{mn} = \hat{f}_{mn} / \lambda_{mn} where λmn=−π2(m2/Lx2+n2/Ly2)\lambda_{mn} = -\pi^2(m^2/L_x^2 + n^2/L_y^2).

  4. Transform back. An inverse DST returns ϕ\phi on the physical grid.

The result is an exact solve (up to floating-point arithmetic) with O(Nlog⁡N)O(N \log N) cost -- far cheaper than iterative methods for these structured grids.

7. Summary

ConceptAPI
Create solverPoissonSolver2D.create(nx, ny, Lx, Ly, bc="dirichlet")
Grid coordinatesx = jnp.arange(solver.grid.Nx) * solver.grid.dx
Solve Laplace (f=0f=0)solver.solve(jnp.zeros(...))
Solve Poissonsolver.solve(rhs)
Spectral methodDST (Dirichlet), DCT (Neumann), FFT (periodic)

Key takeaway: Elliptic solves are not time-stepping. You call solver.solve(rhs) once and get the steady-state answer.

Next: Step 10 explores the full Poisson equation with multiple boundary conditions and convergence analysis.