18 axpy(coeff, lap, u, b);
27 axpy(coeff, lap, u, b);
36 axpy(coeff, lap, u, b);
53 std::vector<idx> rows, cols;
54 std::vector<real> vals;
58 for (
int i = 0; i < N; ++i) {
59 for (
int j = 0; j < N; ++j) {
66 cols.push_back((i - 1) * N + j);
71 cols.push_back((i + 1) * N + j);
76 cols.push_back(i * N + (j - 1));
81 cols.push_back(i * N + (j + 1));
91 std::vector<idx> rows, cols;
92 std::vector<real> vals;
96 for (
int i = 0; i < N; ++i) {
97 for (
int j = 0; j < N; ++j) {
101 vals.push_back(1.0 + 4.0 * coeff);
104 cols.push_back((i - 1) * N + j);
105 vals.push_back(-coeff);
109 cols.push_back((i + 1) * N + j);
110 vals.push_back(-coeff);
114 cols.push_back(i * N + (j - 1));
115 vals.push_back(-coeff);
119 cols.push_back(i * N + (j + 1));
120 vals.push_back(-coeff);
134 return cg(op, rhs, x, tol);
Conjugate gradient solvers.
constexpr idx size() const noexcept
Sparse matrix in Compressed Sparse Row (CSR) format.
static SparseMatrix from_triplets(idx n_rows, idx n_cols, const std::vector< idx > &rows, const std::vector< idx > &cols, const std::vector< real > &vals)
Build from coordinate (COO / triplet) lists.
Backend enum and default backend selection.
2D uniform interior grid: geometry only, no field data.
Compressed Sparse Row (CSR) matrix and operations.
Universal linear solver callable type.
SparseMatrix laplacian_sparse_2d(int N)
void diffusion_step_2d_4th_dirichlet(Vector &u, int N, double coeff, Backend b=best_backend)
void diffusion_step_2d_dirichlet(Vector &u, int N, double coeff, Backend b=best_backend)
void diffusion_step_2d(Vector &u, int N, double coeff, Backend b=best_backend)
SparseMatrix backward_euler_matrix(int N, double coeff)
LinearSolver make_cg_solver(const SparseMatrix &A, real tol=1e-6)
void laplacian_stencil_2d(const BasicVector< T > &x, BasicVector< T > &y, int N)
std::function< SolverResult(const Vector &rhs, Vector &x)> LinearSolver
Callable that solves .
constexpr Backend best_backend
void laplacian_stencil_2d_4th(const BasicVector< T > &x, BasicVector< T > &y, int N)
Fourth-order 2D Laplacian cross stencil.
void laplacian_stencil_2d_periodic(const BasicVector< T > &x, BasicVector< T > &y, int N)
Periodic second-order 2D Laplacian stencil.
void axpy(real alpha, const Vector &x, Vector &y, Backend b=default_backend)
Compute .
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Higher-order stencil and grid-sweep utilities.
int N
interior nodes per side
Adapt a SparseMatrix to the operator protocol.
Dense vector storage and operations.