numerics 0.1.0
Loading...
Searching...
No Matches
PDE Examples

Finite-difference routines live under num::pde and are included by numerics.hpp.

Explicit Diffusion Step

constexpr int N = 128;
constexpr double h = 1.0 / (N + 1);
constexpr double dt = 0.25 * h * h;
constexpr double kappa = 1.0;
num::Vector u(N * N, 0.0);
initialise_gaussian(u, N, h);
u, N, kappa * dt / (h * h), num::best_backend);
void diffusion_step_2d_dirichlet(Vector &u, int N, double coeff, Backend b=best_backend)
Definition diffusion.hpp:21
constexpr Backend best_backend
Definition policy.hpp:64

Backward Euler Heat Solve

num::Grid2D grid{N, h};
double coeff = kappa * dt / (h * h);
num::LinearSolver solver = [&](const num::Vector& rhs, num::Vector& x) {
return num::cg(Aop, rhs, x, 1e-8, 500);
};
num::ScalarField2D u(grid, initial_condition);
num::solve(u, num::BackwardEuler{.solver = solver, .dt = dt, .nstep = 100});
Sparse matrix in Compressed Sparse Row (CSR) format.
Definition sparse.hpp:15
SparseMatrix backward_euler_matrix(int N, double coeff)
Definition diffusion.hpp:89
std::function< SolverResult(const Vector &rhs, Vector &x)> LinearSolver
Callable that solves .
ODEResult solve(const P &prob, const RK45 &alg, ObserverFn obs=nullptr)
Definition solve.hpp:39
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Definition cg.cpp:8
LinearSolver solver
Adapt a SparseMatrix to the operator protocol.
Definition sparse.hpp:11

The linear system for one step is

\[ (I - \Delta t\,\kappa L_h)u^{n+1}=u^n . \]

ADI Diffusion

num::CrankNicolsonADI stepper(N, h, kappa, dt);
for (int step = 0; step < 200; ++step) {
stepper.step(u.vec());
}

Poisson Solve

For a square Dirichlet problem on an \(N\times N\) interior grid:

num::Matrix f(N, N, 0.0);
fill_rhs(f, N);
Matrix poisson2d(const Matrix &f, int N)
Solve using continuous eigenvalues .
Definition poisson.cpp:141

See Poisson Solver Example for the DST-based Poisson example.