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;
initialise_gaussian(u, N, h);
void diffusion_step_2d_dirichlet(Vector &u, int N, double coeff, Backend b=best_backend)
constexpr Backend best_backend
Backward Euler Heat Solve
double coeff = kappa * dt / (h * h);
return num::cg(Aop, rhs, x, 1e-8, 500);
};
Sparse matrix in Compressed Sparse Row (CSR) format.
SparseMatrix backward_euler_matrix(int N, double coeff)
std::function< SolverResult(const Vector &rhs, Vector &x)> LinearSolver
Callable that solves .
ODEResult solve(const P &prob, const RK45 &alg, ObserverFn obs=nullptr)
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Adapt a SparseMatrix to the operator protocol.
The linear system for one step is
\[
(I - \Delta t\,\kappa L_h)u^{n+1}=u^n .
\]
ADI Diffusion
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:
fill_rhs(f, N);
Matrix poisson2d(const Matrix &f, int N)
Solve using continuous eigenvalues .
See Poisson Solver Example for the DST-based Poisson example.