numerics 0.1.0
Loading...
Searching...
No Matches
Poisson Solver Example

num::pde::poisson2d solves the Dirichlet problem

\[ -\Delta u = f,\qquad u|_{\partial\Omega}=0 \]

on an \(N\times N\) interior grid by DST-I diagonalization.

Solve on a Square Grid

constexpr int N = 63;
constexpr double h = 1.0 / (N + 1);
num::Matrix f(N, N, 0.0);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
double x = (i + 1) * h;
double y = (j + 1) * h;
f(i, j) = 2.0 * std::numbers::pi * std::numbers::pi
* std::sin(std::numbers::pi * x)
* std::sin(std::numbers::pi * y);
}
}
Matrix poisson2d(const Matrix &f, int N)
Solve using continuous eigenvalues .
Definition poisson.cpp:141

The exact solution in this example is

\[ u(x,y)=\sin(\pi x)\sin(\pi y). \]

Finite-Difference Reference

Matrix poisson2d_fd(const Matrix &f, int N)
Solve using finite-difference eigenvalues.
Definition poisson.cpp:109

Use poisson2d_fd as a direct finite-difference reference for small grids.

Size Constraint

The DST implementation uses FFTs of length \(2(N+1)\). The current built-in radix-2 path requires \(N+1\) to be a power of two, for example \(N=7,15,31,63,127\).