35 axpy(coeff, lap, u, b);
44 axpy(coeff, lap, u, b);
53 axpy(coeff, lap, u, b);
72 std::vector<idx> rows, cols;
73 std::vector<real> vals;
74 rows.reserve(5 * n); cols.reserve(5 * n); vals.reserve(5 * n);
75 for (
int i = 0; i < N; ++i) {
76 for (
int j = 0; j < N; ++j) {
78 rows.push_back(k); cols.push_back(k); vals.push_back(-4.0);
79 if (i > 0) { rows.push_back(k); cols.push_back((i-1)*N+j); vals.push_back(1.0); }
80 if (i < N-1) { rows.push_back(k); cols.push_back((i+1)*N+j); vals.push_back(1.0); }
81 if (j > 0) { rows.push_back(k); cols.push_back(i*N+(j-1)); vals.push_back(1.0); }
82 if (j < N-1) { rows.push_back(k); cols.push_back(i*N+(j+1)); vals.push_back(1.0); }
92 std::vector<idx> rows, cols;
93 std::vector<real> vals;
94 rows.reserve(5 * n); cols.reserve(5 * n); vals.reserve(5 * n);
95 for (
int i = 0; i < N; ++i) {
96 for (
int j = 0; j < N; ++j) {
98 rows.push_back(k); cols.push_back(k); vals.push_back(1.0 + 4.0 * coeff);
99 if (i > 0) { rows.push_back(k); cols.push_back((i-1)*N+j); vals.push_back(-coeff); }
100 if (i < N-1) { rows.push_back(k); cols.push_back((i+1)*N+j); vals.push_back(-coeff); }
101 if (j > 0) { rows.push_back(k); cols.push_back(i*N+(j-1)); vals.push_back(-coeff); }
102 if (j < N-1) { rows.push_back(k); cols.push_back(i*N+(j+1)); vals.push_back(-coeff); }
Conjugate gradient solvers (dense and matrix-free)
constexpr idx size() const noexcept
Vector & vec()
Satisfy VecField concept: exposes the underlying flat vector.
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 for linear algebra operations.
2D uniform interior grid: geometry only, no field data.
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)
Backend
Selects which backend handles a linalg operation.
std::function< SolverResult(const Vector &rhs, Vector &x)> LinearSolver
constexpr Backend best_backend
void laplacian_stencil_2d_4th(const BasicVector< T > &x, BasicVector< T > &y, int N)
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
void sparse_matvec(const SparseMatrix &A, const Vector &x, Vector &y)
y = A * x
void laplacian_stencil_2d_periodic(const BasicVector< T > &x, BasicVector< T > &y, int N)
void axpy(real alpha, const Vector &x, Vector &y, Backend b=default_backend)
y += alpha * x
SolverResult cg_matfree(MatVecFn matvec, const Vector &b, Vector &x, real tol=1e-6, idx max_iter=1000)
Matrix-free conjugate gradient for Ax = b where A is SPD.
Compressed Sparse Row (CSR) matrix and operations.
Higher-order stencil and grid-sweep utilities.
int N
interior nodes per side