numerics 0.1.0
Loading...
Searching...
No Matches
Stencil Operator Examples

Stencil routines are useful both as direct updates and as matrix-free linear operators.

Apply a 2D Laplacian

num::Vector u(N * N, 0.0);
num::Vector Lu(N * N, 0.0);
num::pde::laplacian_stencil_2d(u, Lu, N);

Matrix-Free Operator

[N](const num::Vector& x, num::Vector& y) {
num::pde::laplacian_stencil_2d(x, y, N);
num::scale(y, -1.0);
},
N * N);
num::SolverResult info = num::cg(A, rhs, sol, 1e-8, 1000);
CallableOp< F > make_op(F f, idx rows, idx cols)
Definition callable.hpp:39
void scale(Vector &v, real alpha, Backend b=default_backend)
Compute .
Definition vector.cpp:15
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

The operator represents

\[ y = -L_h x . \]

Periodic Diffusion Step

double coeff = kappa * dt / (h * h);
void diffusion_step_2d(Vector &u, int N, double coeff, Backend b=best_backend)
Definition diffusion.hpp:15
constexpr Backend best_backend
Definition policy.hpp:64

Fourth-Order Dirichlet Laplacian

num::Vector Lu4(N * N, 0.0);
num::pde::laplacian_stencil_2d_4th(u, Lu4, N);

Use matrix-free operators when the sparse matrix would be expensive to assemble or when the stencil is applied many times inside a Krylov method.