18template <
class EleType>
50 SmootherBase(
double tolerance_,
size_t compute_error_every_n_iters_,
63 virtual void smooth(
const Eigen::SparseMatrix<EleType>& A,
64 Eigen::Matrix<EleType, -1, 1>& u,
65 const Eigen::Matrix<EleType, -1, 1>& b) = 0;
86template <
class EleType>
101 void matvecprod(
int col, EleType& rsum, EleType& diag,
const EleType& z,
102 const Eigen::SparseMatrix<EleType>& A,
103 const Eigen::Matrix<EleType, -1, 1>& u) {
106 for (
typename Eigen::SparseMatrix<EleType>::InnerIterator it(A, col); it;
129 void update(
int col,
const Eigen::SparseMatrix<EleType>& A,
130 const Eigen::Matrix<EleType, -1, 1>& b,
131 Eigen::Matrix<EleType, -1, 1>& u) {
136 u[col] = diag == z ? u[col] : (b[col] - rsum) / diag;
149 const Eigen::Matrix<EleType, -1, 1>& b,
150 Eigen::Matrix<EleType, -1, 1>& u,
const int& ncols) {
153 for (
int col = 0; col < ncols; ++col) {
168 const Eigen::Matrix<EleType, -1, 1>& b,
169 Eigen::Matrix<EleType, -1, 1>& u,
const int& ncols) {
171 for (
int col = ncols - 1; col >= 0; --col) {
189 void smooth(
const Eigen::SparseMatrix<EleType>& A,
190 Eigen::Matrix<EleType, -1, 1>& u,
191 const Eigen::Matrix<EleType, -1, 1>& b) {
192 int ncols = A.cols();
201 error =
rss(A, u, b);
207 std::cout <<
"SPGS converged after " << iter <<
" iterations."
210 std::cout <<
"SPGS did not converge after " << iter <<
" iterations."
223template <
class EleType>
239 void smooth(
const Eigen::SparseMatrix<EleType>& A,
240 Eigen::Matrix<EleType, -1, 1>& u,
241 const Eigen::Matrix<EleType, -1, 1>& b) {
243 size_t ndofs = b.size();
247 for (
size_t i = 0; i < ndofs; ++i) {
249 for (
size_t j = 0; j < ndofs; ++j) {
251 sigma += A.coeff(i, j) * u[j];
254 EleType aii = A.coeff(i, i);
255 u[i] = (b[i] - sigma) / aii;
259 error =
rss(A, u, b);
271template <
class EleType>
289 "`omega` must be in [0, 2] but got omega=" + std::to_string(
omega) +
291 throw std::invalid_argument(msg);
322 size_t compute_error_every_n_iters_,
size_t n_iters_)
323 :
SmootherBase<EleType>(tolerance_, compute_error_every_n_iters_,
339 void smooth(
const Eigen::SparseMatrix<EleType>& A,
340 Eigen::Matrix<EleType, -1, 1>& u,
341 const Eigen::Matrix<EleType, -1, 1>& b) {
343 size_t ndofs = b.size();
345 EleType sigma_j_less_i;
346 EleType sigma_j_greater_i;
348 for (
size_t i = 0; i < ndofs; ++i) {
350 for (
size_t j = 0; j < i; ++j) {
351 sigma_j_less_i += A.coeff(i, j) * u[j];
354 sigma_j_greater_i = 0;
355 for (
size_t j = i + 1; j < ndofs; ++j) {
356 sigma_j_greater_i += A.coeff(i, j) * u[j];
359 EleType aii = A.coeff(i, i);
360 EleType uk_plus_one_gauss_seidel =
361 (b[i] - sigma_j_less_i - sigma_j_greater_i) / aii;
364 u[i] = uk +
omega * (uk_plus_one_gauss_seidel - uk);
368 error =
rss(A, u, b);
Dense Jacobi iterative method.
Definition smoother.hpp:224
void smooth(const Eigen::SparseMatrix< EleType > &A, Eigen::Matrix< EleType, -1, 1 > &u, const Eigen::Matrix< EleType, -1, 1 > &b)
Update initial guess u inplace using Jacobi method.
Definition smoother.hpp:239
Jacobi()
Definition smoother.hpp:230
Base class for smoothers that must implement a smooth function for the iterative solution of a linear...
Definition smoother.hpp:19
SmootherBase()
Definition smoother.hpp:39
EleType tolerance
Tolerance below which a smoother is considered to have converged.
Definition smoother.hpp:25
size_t n_iters
Maximum number of iterations before smoothing termination.
Definition smoother.hpp:37
virtual void smooth(const Eigen::SparseMatrix< EleType > &A, Eigen::Matrix< EleType, -1, 1 > &u, const Eigen::Matrix< EleType, -1, 1 > &b)=0
Must implement function that smooths Au = b.
size_t compute_error_every_n_iters
Compute the error every n iterations during smoothing.
Definition smoother.hpp:31
SmootherBase(double tolerance_, size_t compute_error_every_n_iters_, size_t n_iters_)
Construct a new Smoother Base object with iterative solver member data.
Definition smoother.hpp:50
SmootherBase(size_t n_iters_)
Definition smoother.hpp:41
Symmetric Gauss-Seidel smoother for sparse systems.
Definition smoother.hpp:87
SparseGaussSeidel()
Construct a new Sparse Gauss Seidel object for pre/post smoother in AMG.
Definition smoother.hpp:183
void backwardsweep(const Eigen::SparseMatrix< EleType > &A, const Eigen::Matrix< EleType, -1, 1 > &b, Eigen::Matrix< EleType, -1, 1 > &u, const int &ncols)
Gauss-Seidel iteration starting from the last row.
Definition smoother.hpp:167
void matvecprod(int col, EleType &rsum, EleType &diag, const EleType &z, const Eigen::SparseMatrix< EleType > &A, const Eigen::Matrix< EleType, -1, 1 > &u)
Modified sparse matvec product of Au for Gauss-Seidel sweeps.
Definition smoother.hpp:101
void smooth(const Eigen::SparseMatrix< EleType > &A, Eigen::Matrix< EleType, -1, 1 > &u, const Eigen::Matrix< EleType, -1, 1 > &b)
Must implement function that smooths Au = b.
Definition smoother.hpp:189
void update(int col, const Eigen::SparseMatrix< EleType > &A, const Eigen::Matrix< EleType, -1, 1 > &b, Eigen::Matrix< EleType, -1, 1 > &u)
Updates a single entry in u inplace using the definition of a Gauss-Seidel.
Definition smoother.hpp:129
void forwardsweep(const Eigen::SparseMatrix< EleType > &A, const Eigen::Matrix< EleType, -1, 1 > &b, Eigen::Matrix< EleType, -1, 1 > &u, const int &ncols)
Gauss-Seidel iteration starting from the first row.
Definition smoother.hpp:148
Dense successive over relaxation iterative method.
Definition smoother.hpp:272
SuccessiveOverRelaxation()
Definition smoother.hpp:300
void smooth(const Eigen::SparseMatrix< EleType > &A, Eigen::Matrix< EleType, -1, 1 > &u, const Eigen::Matrix< EleType, -1, 1 > &b)
Update initial guess u inplace with SOR and internal relaxation param omega
Definition smoother.hpp:339
double omega
Relaxation parameter in [0, 2].
Definition smoother.hpp:278
void validate_omega()
Force omega to be in [0, 2].
Definition smoother.hpp:286
SuccessiveOverRelaxation(double omega_)
Construct a new Successive Over Relaxation object.
Definition smoother.hpp:309
SuccessiveOverRelaxation(double omega_, double tolerance_, size_t compute_error_every_n_iters_, size_t n_iters_)
Construct a new Successive Over Relaxation object.
Definition smoother.hpp:321
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