MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
sparsesmoothers.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_SPARSEMATSMOOTHERS
13#define MFEM_SPARSEMATSMOOTHERS
14
15#include "../config/config.hpp"
16#include "sparsemat.hpp"
17
18#include <memory>
19
20namespace mfem
21{
22
23/// Abstract base class for smoothers created from a SparseMatrix.
25{
26protected:
27 const SparseMatrix *oper = nullptr; ///< The underlying matrix.
28
29 /// Pointer to the transpose of the underlying matrix. If the matrix is
30 /// symmetric, this will be the same as @a oper. If the matrix is not
31 /// symmetric, the transpose will be formed and stored in @a At. The
32 /// transpose will only be formed if MultTranspose() is called.
33 mutable const SparseMatrix *oper_T = nullptr;
34
35 mutable std::unique_ptr<SparseMatrix> At; ///< Transpose of A, if needed.
36
37 void EnsureTranspose() const; ///< Ensure that the transpose is set.
38
39public:
40 SparseSmoother() = default;
41
43
44 /// Sets the underlying matrix. @a a must be a SparseMatrix.
45 void SetOperator(const Operator &a) override;
46};
47
48/// Gauss-Seidel smoother of a sparse matrix.
50{
51public:
52 enum GSType
53 {
54 SYMMETRIC, ///< Forward Gauss-Seidel, then backward.
55 FORWARD, ///< Forward Gauss-Seidel ($L^{-1}$).
56 BACKWARD ///< Backward Gauss-Seidel ($U^{-1}$).
57 };
58protected:
59 GSType type; ///< Type of Gauss-Seidel, see GSSmoother::GSType.
60 int iterations; ///< Number of stationary iterations.
61
62public:
63 /// @brief Create a Gauss-Seidel smoother. SetOperator() will need to be
64 /// called with a SparseMatrix before first use.
65 ///
66 /// @param[in] t Type of GS smoother (see GSSmoother::GSType)
67 /// @param[in] it Number of stationary iterations to perform
68 GSSmoother(GSType t = SYMMETRIC, int it = 1) { type = t; iterations = it; }
69
70 /// @brief Create a Gauss-Seidel smoother using the SparseMatrix @a a.
71 ///
72 /// @param[in] a The underlying SparseMatrix
73 /// @param[in] t Type of GS smoother (see GSSmoother::GSType)
74 /// @param[in] it Number of stationary iterations to perform
75 GSSmoother(const SparseMatrix &a, GSType t = SYMMETRIC, int it = 1)
76 : GSSmoother(t, it) { SetOperator(a); }
77
78 /// Same as GSSmoother(GSType,int), for backwards compatibility.
79 GSSmoother(int t, int it = 1) : GSSmoother(GSType(t), it) { }
80
81 /// @brief Same as GSSmoother(const SparseMatrix&,GSType,int), for
82 /// backwards compatibility.
83 GSSmoother(const SparseMatrix &a, int t, int it = 1)
84 : GSSmoother(a, GSType(t), it) { }
85
86 /// @brief Application of the Gauss-Seidel smoother.
87 ///
88 /// Applies a stationary Gauss-Seidel iteration. If Solver::iterative_mode is
89 /// true, then @a y is used as the initial guess, and Gauss-Seidel is applied
90 /// to the residual $x - Ay$.
91 void Mult(const Vector &x, Vector &y) const override;
92
93 /// Application of the transpose of the Gauss-Seidel smoother.
94 void MultTranspose(const Vector &x, Vector &y) const override;
95};
96
97/// Jacobi-type diagonal smoother of a sparse matrix.
99{
100public:
102 {
103 JACOBI, ///< Scale by the diagonal of the matrix.
104 L1_JACOBI, ///< Scale by the l1-norm of the rows.
105 LUMPED_JACOBI ///< Scale by the sum of the rows.
106 };
107protected:
108 JacobiType type; ///< Type of diagonal scaling, see DSmoother::JacobiType.
109 real_t scale; ///< Scaling (damping) factor.
110 int iterations; ///< Number of stationary iterations to perform.
111
112 /// @brief Uses abs values of the diagonal entries. Relevant only with type
113 /// JacobiType::JACOBI.
114 bool use_abs_diag = false;
115
116 mutable Vector z; ///< Temporary work vector.
117
118 /// Apply the Jacobi smoother (used internally by Mult() and MultTranspose())
119 void Mult_(const SparseMatrix &A, const Vector &x, Vector &y) const;
120
121public:
122 /// @brief Create a Jacobi smoother. SetOperator() will need to be called
123 /// with a SparseMatrix before first use.
124 ///
125 /// @param[in] t Type of Jacobi smoother (see DSmoother::JacobiType)
126 /// @param[in] s Scaling factor
127 /// @param[in] it Number of stationary iterations to perform
128 DSmoother(JacobiType t = JACOBI, real_t s = 1., int it = 1)
129 { type = t; scale = s; iterations = it; }
130
131 /// @brief Create a Jacobi smoother using the SparseMatrix @a a.
132 ///
133 /// @param[in] a The underlying SparseMatrix
134 /// @param[in] t Type of Jacobi smoother (see DSmoother::JacobiType)
135 /// @param[in] s Scaling factor
136 /// @param[in] it Number of stationary iterations to perform
138 int it = 1) : DSmoother(t, s, it) { SetOperator(a); }
139
140 /// @brief Same as DSmoother(JacobiType,real_t,int), for backwards compatibility.
141 DSmoother(int t, real_t s = 1., int it = 1)
142 : DSmoother(JacobiType(t), s, it) { }
143
144 /// @brief Same as DSmoother(const SparseMatrix&,JacobiType,real_t,int), for
145 /// backwards compatibility.
146 DSmoother(const SparseMatrix &a, int t, real_t s = 1., int it = 1)
147 : DSmoother(a, JacobiType(t), s, it) { }
148
149 /// @brief Replace diagonal entries with their absolute values. Relevant only
150 /// with JacobiType::JACOBI.
151 void SetPositiveDiagonal(bool pos_diag = true) { use_abs_diag = pos_diag; }
152
153 /// @brief Apply the Jacobi smoother.
154 ///
155 /// Applies a stationary iteration with diagonal scaling. If
156 /// Solver::iterative_mode is true, then @a y is used as the initial guess
157 /// (and the diagonal scaling is applied to the residual $x - Ay$, giving
158 /// $D^{-1}(x - Ay)$).
159 ///
160 /// By default, Solver::iterative_mode is false and only one iteration is
161 /// performed, corresponding to $y = D^{-1}x$.
162 void Mult(const Vector &x, Vector &y) const override;
163
164 /// @brief Apply the transpose of the Jacobi smoother.
165 ///
166 /// If the underlying matrix is symmetric, or if only one iteration is
167 /// performed with zero initial guess (Solver::iterative_mode is false), then
168 /// this is the same as Mult(). For non-symmetric matrices with iteration
169 /// count greater than one, only JacobiType::JACOBI is supported.
170 void MultTranspose(const Vector &x, Vector &y) const override;
171};
172
173}
174
175#endif
Jacobi-type diagonal smoother of a sparse matrix.
JacobiType type
Type of diagonal scaling, see DSmoother::JacobiType.
DSmoother(const SparseMatrix &a, int t, real_t s=1., int it=1)
Same as DSmoother(const SparseMatrix&,JacobiType,real_t,int), for backwards compatibility.
bool use_abs_diag
Uses abs values of the diagonal entries. Relevant only with type JacobiType::JACOBI.
void SetPositiveDiagonal(bool pos_diag=true)
Replace diagonal entries with their absolute values. Relevant only with JacobiType::JACOBI.
void Mult_(const SparseMatrix &A, const Vector &x, Vector &y) const
Apply the Jacobi smoother (used internally by Mult() and MultTranspose())
int iterations
Number of stationary iterations to perform.
void Mult(const Vector &x, Vector &y) const override
Apply the Jacobi smoother.
DSmoother(const SparseMatrix &a, JacobiType t=JACOBI, real_t s=1., int it=1)
Create a Jacobi smoother using the SparseMatrix a.
real_t scale
Scaling (damping) factor.
DSmoother(JacobiType t=JACOBI, real_t s=1., int it=1)
Create a Jacobi smoother. SetOperator() will need to be called with a SparseMatrix before first use.
DSmoother(int t, real_t s=1., int it=1)
Same as DSmoother(JacobiType,real_t,int), for backwards compatibility.
@ L1_JACOBI
Scale by the l1-norm of the rows.
@ LUMPED_JACOBI
Scale by the sum of the rows.
@ JACOBI
Scale by the diagonal of the matrix.
Vector z
Temporary work vector.
void MultTranspose(const Vector &x, Vector &y) const override
Apply the transpose of the Jacobi smoother.
Gauss-Seidel smoother of a sparse matrix.
int iterations
Number of stationary iterations.
void Mult(const Vector &x, Vector &y) const override
Application of the Gauss-Seidel smoother.
GSSmoother(const SparseMatrix &a, int t, int it=1)
Same as GSSmoother(const SparseMatrix&,GSType,int), for backwards compatibility.
GSSmoother(const SparseMatrix &a, GSType t=SYMMETRIC, int it=1)
Create a Gauss-Seidel smoother using the SparseMatrix a.
GSSmoother(GSType t=SYMMETRIC, int it=1)
Create a Gauss-Seidel smoother. SetOperator() will need to be called with a SparseMatrix before first...
void MultTranspose(const Vector &x, Vector &y) const override
Application of the transpose of the Gauss-Seidel smoother.
@ BACKWARD
Backward Gauss-Seidel ( ).
@ FORWARD
Forward Gauss-Seidel ( ).
@ SYMMETRIC
Forward Gauss-Seidel, then backward.
GSSmoother(int t, int it=1)
Same as GSSmoother(GSType,int), for backwards compatibility.
GSType type
Type of Gauss-Seidel, see GSSmoother::GSType.
Abstract data type for matrix inverse.
Definition matrix.hpp:63
Abstract operator.
Definition operator.hpp:27
Data type sparse matrix.
Definition sparsemat.hpp:51
Abstract base class for smoothers created from a SparseMatrix.
void SetOperator(const Operator &a) override
Sets the underlying matrix. a must be a SparseMatrix.
SparseSmoother()=default
std::unique_ptr< SparseMatrix > At
Transpose of A, if needed.
SparseSmoother(const SparseMatrix &a)
void EnsureTranspose() const
Ensure that the transpose is set.
const SparseMatrix * oper
The underlying matrix.
const SparseMatrix * oper_T
Vector data type.
Definition vector.hpp:82
real_t a
Definition lissajous.cpp:41
float real_t
Definition config.hpp:46