This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

PDE solvers · Steady states and modes (elliptic)

Poisson's equation

Method
five-point Laplacian, solved by conjugate gradients without ever forming the matrix
Checked
a manufactured solution; the measured order; and that conjugate gradients converges in far fewer steps than unknowns

The equation of electrostatics, steady heat flow and pressure in an incompressible fluid. There is no time to march through: every unknown depends on every other, and the work is solving a large sparse linear system.

Pick the answer first — , zero on the boundary of the unit square — and the right-hand side follows by differentiating: . That is a manufactured solution, and it turns "is the solver right?" into a subtraction.

The polynomial term is there on purpose. A pure sine product is an eigenvector of the discrete Laplacian, and conjugate gradients solves an eigenvector problem in ONE step — which would make the iteration count below meaningless. The bubble has a piece of every mode.

The five-point Laplacian is symmetric positive definite, which is exactly what conjugate gradients wants. CG only ever needs the matrix applied to a vector, so the matrix is never built: the grid carries a ring of zeros around it, a neighbor is a roll, and a mask puts the boundary back to zero.

What the run shows

The solution of Poisson's equation
y
x
42 × 42 grid
  • the solve matches the manufactured solution
  • the five-point Laplacian is second order
  • conjugate gradients needs far fewer steps than there are unknowns (33 x 33 = 1089)
e1
0.00486389
e2
0.00121112
order
2.00577
iters
64

Computed when this page was built, by the same interpreter that runs it in your browser: 3 of 3 tests, under 0.1 s. The numbers above are the program's show lines.

The program

95 lines, 6 functions. It is editable: change it and press Run (or ⌘/Ctrl + Enter). The tests at the bottom run too, so if your change breaks the solver, a test fails.

apply_laplacian
Apply -h^2 Laplacian to a padded (n+2) x (n+2) grid; the boundary ring stays zero.
interior_mask
1 on interior points, 0 on the boundary ring.
exact_solution
The manufactured solution sampled on the padded grid.
right_hand_side
Its right-hand side, f = -Laplacian(u), worked out by hand.
solve_poisson
Solve -Laplacian u = f on an n x n interior by conjugate gradients.
poisson_error
L2 error of the solve on an n x n grid.
Viv [1]

Open in the workbench The atlas Download poisson_2d.viv