7#include <Eigen/SparseCholesky>
22template <
class EleType>
128 size_t H_dofs = (h_dofs + 1) / 2 - 1;
153 const Eigen::SparseMatrix<EleType>& A,
154 const Eigen::Matrix<EleType, -1, 1>& b,
size_t n_levels_,
155 EleType tolerance_ = 1e-9,
size_t compute_error_every_n_iters_ = 10,
156 size_t n_iters_ = 100)
164 std::string errormsg;
167 "`compute_error_every_n_iters` must be leq to `n_iters`, got " +
170 throw(std::invalid_argument(errormsg));
173 if (A.rows() != b.rows()) {
175 "`A` and `b` must have the same number of degrees of freedom, got " +
176 std::to_string(A.rows()) +
" and " + std::to_string(b.rows());
177 throw(std::invalid_argument(errormsg));
191 size_t n_fine_dofs = A.rows();
196 Eigen::Matrix<EleType, -1, 1> u(n_fine_dofs);
211 for (
size_t level = 1; level <
n_levels; ++level) {
216 interpolator->make_operators(n_h_dofs, n_H_dofs, level - 1);
222 auto A_H = R_h * (A_h * P_h);
226 Eigen::Matrix<EleType, -1, 1> u_H(n_H_dofs);
231 Eigen::Matrix<EleType, -1, 1> rhs_H(n_H_dofs);
265 for (
size_t level = 0; level <
n_levels; ++level) {
311 const Eigen::Matrix<EleType, -1, 1>&
solve() {
316 while (iter < n_iters && error >
tolerance) {
321 error =
rss(A, u, b);
324 std::cout <<
"Iter: " << iter <<
" | Error: " << error << std::endl;
330 std::cout <<
"AMG converged after " << iter <<
" iterations."
333 std::cout <<
"AMG did not converge after " << iter <<
" iterations."
340 size_t level)
const {
344 const Eigen::Matrix<EleType, -1, 1>&
get_soln(
size_t level)
const {
348 const Eigen::Matrix<EleType, -1, 1>&
get_rhs(
size_t level)
const {
Base class for interpolators that implement prolongation and restriction as linear operators construc...
Definition interpolator.hpp:16
Interface for multigrid solver (via solve) that iteratively performs standard vcycles.
Definition multigrid.hpp:23
const EleType get_tolerance() const
Definition multigrid.hpp:354
const Eigen::Matrix< EleType, -1, 1 > & get_soln(size_t level) const
Definition multigrid.hpp:344
AMG::SmootherBase< EleType > * smoother
Definition multigrid.hpp:31
void display_error_on()
Definition multigrid.hpp:356
size_t n_levels
Number of multigrid levels where level 0 is finest.
Definition multigrid.hpp:63
size_t compute_error_every_n_iters
Compute the error every n iterations during smoothing.
Definition multigrid.hpp:45
void vcycle()
A single multigrid vcycle.
Definition multigrid.hpp:263
const Eigen::Matrix< EleType, -1, 1 > & get_rhs(size_t level) const
Definition multigrid.hpp:348
Eigen::SimplicialLDLT< Eigen::SparseMatrix< EleType > > coarse_direct_solver
Definition multigrid.hpp:33
size_t finest_grid_ix
Index of the finest grid.
Definition multigrid.hpp:69
std::vector< Eigen::Matrix< EleType, -1, 1 > > level_to_rhs
Map multigrid level to right hand side (forcing) vector.
Definition multigrid.hpp:95
const Eigen::Matrix< EleType, -1, 1 > & solve()
Solve a linear system of equations via vcycle.
Definition multigrid.hpp:311
bool display_error
Definition multigrid.hpp:132
const Eigen::SparseMatrix< EleType > & get_coefficient_matrix(size_t level) const
Definition multigrid.hpp:339
size_t n_fine_nodes
Definition multigrid.hpp:57
~Multigrid()
Definition multigrid.hpp:135
size_t n_iters
Maximum number of iterations before smoothing termination.
Definition multigrid.hpp:51
InterpolatorBase< EleType > * interpolator
Interpolator providing restriction and prolongation operations.
Definition multigrid.hpp:29
size_t n_H_dofs_from_n_h_dofs(size_t h_dofs)
Return number of next coarsest dofs given finer dofs.
Definition multigrid.hpp:127
EleType tolerance
Tolerance below which a smoother is considered to have converged.
Definition multigrid.hpp:39
std::vector< Eigen::Matrix< EleType, -1, 1 > > level_to_residual
Map multigrid level to residual (e) vector.
Definition multigrid.hpp:107
size_t coarsest_grid_ix
Index of the coarsest grid.
Definition multigrid.hpp:75
std::vector< size_t > level_to_n_dofs
Map multigrid level to number of degrees of freedom for that level.
Definition multigrid.hpp:83
const size_t get_n_dofs(size_t level) const
Definition multigrid.hpp:352
std::vector< Eigen::Matrix< EleType, -1, 1 > > level_to_soln
Map multigrid level to solution (u) vector.
Definition multigrid.hpp:101
void display_error_off()
Definition multigrid.hpp:361
std::vector< Eigen::SparseMatrix< EleType > > level_to_coefficient_matrix
Map multigrid level to coefficient matrix A.
Definition multigrid.hpp:89
Multigrid(AMG::InterpolatorBase< EleType > *interpolator_, AMG::SmootherBase< EleType > *smoother_, const Eigen::SparseMatrix< EleType > &A, const Eigen::Matrix< EleType, -1, 1 > &b, size_t n_levels_, EleType tolerance_=1e-9, size_t compute_error_every_n_iters_=10, size_t n_iters_=100)
Construct a new Multigrid object.
Definition multigrid.hpp:151
Base class for smoothers that must implement a smooth function for the iterative solution of a linear...
Definition smoother.hpp:19
EleType rss(const Eigen::SparseMatrix< EleType > &A, const Eigen::Matrix< EleType, -1, 1 > &u, const Eigen::Matrix< EleType, -1, 1 > &b)
Return residual sum of squares of bhat from Au and rhs b.
Definition common.hpp:18