| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2005 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_mg_block_smoother_h |
| 16 |
#define dealii_mg_block_smoother_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/mg_level_object.h> |
| 22 |
#include <deal.II/base/observer_pointer.h> |
| 23 |
|
| 24 |
#include <deal.II/lac/block_vector.h> |
| 25 |
#include <deal.II/lac/linear_operator.h> |
| 26 |
#include <deal.II/lac/vector_memory.h> |
| 27 |
|
| 28 |
#include <deal.II/multigrid/mg_smoother.h> |
| 29 |
|
| 30 |
#include <vector> |
| 31 |
|
| 32 |
DEAL_II_NAMESPACE_OPEN |
| 33 |
|
| 34 |
/* |
| 35 |
* MGSmootherBase is defined in mg_base.h |
| 36 |
*/ |
| 37 |
|
| 38 |
/** |
| 39 |
* @addtogroup mg |
| 40 |
* @{ |
| 41 |
*/ |
| 42 |
|
| 43 |
/** |
| 44 |
* General smoother class for block vectors. This class gives complete freedom |
| 45 |
* to the choice of a block smoother by being initialized with a matrix and a |
| 46 |
* smoother object. Therefore, the smoother object for each level must be |
| 47 |
* constructed by hand. |
| 48 |
*/ |
| 49 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 50 |
class MGSmootherBlock : public MGSmoother<BlockVector<number>> |
| 51 |
{ |
| 52 |
public: |
| 53 |
/** |
| 54 |
* Constructor. |
| 55 |
*/ |
| 56 |
MGSmootherBlock(const unsigned int steps = 1, |
| 57 |
const bool variable = false, |
| 58 |
const bool symmetric = false, |
| 59 |
const bool transpose = false, |
| 60 |
const bool reverse = false); |
| 61 |
|
| 62 |
/** |
| 63 |
* Initialize for matrices. The parameter <tt>matrices</tt> can be any |
| 64 |
* object having functions <tt>get_minlevel()</tt> and |
| 65 |
* <tt>get_maxlevel()</tt> as well as an <tt>operator[]</tt> returning a |
| 66 |
* reference to @p MatrixType. |
| 67 |
* |
| 68 |
* The same convention is used for the parameter <tt>smoothers</tt>, such |
| 69 |
* that <tt>operator[]</tt> returns the object doing the block-smoothing on |
| 70 |
* a single level. |
| 71 |
* |
| 72 |
* This function stores pointers to the level matrices and smoothing |
| 73 |
* operator for each level. |
| 74 |
*/ |
| 75 |
template <typename MGMatrixType, typename MGRelaxationType> |
| 76 |
void |
| 77 |
initialize(const MGMatrixType &matrices, const MGRelaxationType &smoothers); |
| 78 |
|
| 79 |
/** |
| 80 |
* Empty all vectors. |
| 81 |
*/ |
| 82 |
void |
| 83 |
clear(); |
| 84 |
|
| 85 |
/** |
| 86 |
* Switch on/off reversed. This is mutually exclusive with transpose(). |
| 87 |
*/ |
| 88 |
void |
| 89 |
set_reverse(const bool); |
| 90 |
|
| 91 |
/** |
| 92 |
* Implementation of the interface for @p Multigrid. This function does |
| 93 |
* nothing, which by comparison with the definition of this function means |
| 94 |
* that the smoothing operator equals the null operator. |
| 95 |
*/ |
| 96 |
virtual void |
| 97 |
smooth(const unsigned int level, |
| 98 |
BlockVector<number> &u, |
| 99 |
const BlockVector<number> &rhs) const; |
| 100 |
|
| 101 |
/** |
| 102 |
* Memory used by this object. |
| 103 |
*/ |
| 104 |
std::size_t |
| 105 |
memory_consumption() const; |
| 106 |
|
| 107 |
private: |
| 108 |
/** |
| 109 |
* Pointer to the matrices. |
| 110 |
*/ |
| 111 |
MGLevelObject<LinearOperator<BlockVector<number>>> matrices; |
| 112 |
|
| 113 |
/** |
| 114 |
* Pointer to the matrices. |
| 115 |
*/ |
| 116 |
MGLevelObject<LinearOperator<BlockVector<number>>> smoothers; |
| 117 |
|
| 118 |
/** |
| 119 |
* Reverse? |
| 120 |
*/ |
| 121 |
bool reverse; |
| 122 |
|
| 123 |
/** |
| 124 |
* Memory for auxiliary vectors. |
| 125 |
*/ |
| 126 |
ObserverPointer<VectorMemory<BlockVector<number>>, |
| 127 |
MGSmootherBlock<MatrixType, RelaxationType, number>> |
| 128 |
mem; |
| 129 |
}; |
| 130 |
|
| 131 |
/** @} */ |
| 132 |
|
| 133 |
//--------------------------------------------------------------------------- |
| 134 |
|
| 135 |
#ifndef DOXYGEN |
| 136 |
|
| 137 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 138 |
inline MGSmootherBlock<MatrixType, RelaxationType, number>::MGSmootherBlock( |
| 139 |
const unsigned int steps, |
| 140 |
const bool variable, |
| 141 |
const bool symmetric, |
| 142 |
const bool transpose, |
| 143 |
const bool reverse) |
| 144 |
: MGSmoother<BlockVector<number>>(steps, variable, symmetric, transpose) |
| 145 |
, reverse(reverse) |
| 146 |
, mem(&this->vector_memory) |
| 147 |
{} |
| 148 |
|
| 149 |
|
| 150 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 151 |
inline void |
| 152 |
MGSmootherBlock<MatrixType, RelaxationType, number>::clear() |
| 153 |
{ |
| 154 |
unsigned int i = matrices.min_level(), max_level = matrices.max_level(); |
| 155 |
for (; i <= max_level; ++i) |
| 156 |
{ |
| 157 |
smoothers[i] = LinearOperator<BlockVector<number>>(); |
| 158 |
matrices[i] = LinearOperator<BlockVector<number>>(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 164 |
template <typename MGMatrixType, typename MGRelaxationType> |
| 165 |
inline void |
| 166 |
MGSmootherBlock<MatrixType, RelaxationType, number>::initialize( |
| 167 |
const MGMatrixType &m, |
| 168 |
const MGRelaxationType &s) |
| 169 |
{ |
| 170 |
const unsigned int min = m.min_level(); |
| 171 |
const unsigned int max = m.max_level(); |
| 172 |
|
| 173 |
matrices.resize(min, max); |
| 174 |
smoothers.resize(min, max); |
| 175 |
|
| 176 |
for (unsigned int i = min; i <= max; ++i) |
| 177 |
{ |
| 178 |
// Workaround: Unfortunately, not every "m[i]" object has a |
| 179 |
// rich enough interface to populate reinit_(domain|range)_vector. |
| 180 |
// Thus, apply an empty LinearOperator exemplar. |
| 181 |
matrices[i] = linear_operator<BlockVector<number>>( |
| 182 |
LinearOperator<BlockVector<number>>(), m[i]); |
| 183 |
smoothers[i] = linear_operator<BlockVector<number>>(matrices[i], s[i]); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 189 |
inline void |
| 190 |
MGSmootherBlock<MatrixType, RelaxationType, number>::set_reverse( |
| 191 |
const bool flag) |
| 192 |
{ |
| 193 |
reverse = flag; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 198 |
inline std::size_t |
| 199 |
MGSmootherBlock<MatrixType, RelaxationType, number>::memory_consumption() const |
| 200 |
{ |
| 201 |
return sizeof(*this) + matrices.memory_consumption() + |
| 202 |
smoothers.memory_consumption() + |
| 203 |
this->vector_memory.memory_consumption(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
template <typename MatrixType, typename RelaxationType, typename number> |
| 208 |
inline void |
| 209 |
MGSmootherBlock<MatrixType, RelaxationType, number>::smooth( |
| 210 |
const unsigned int level, |
| 211 |
BlockVector<number> &u, |
| 212 |
const BlockVector<number> &rhs) const |
| 213 |
{ |
| 214 |
LogStream::Prefix prefix("Smooth"); |
| 215 |
|
| 216 |
unsigned int maxlevel = matrices.max_level(); |
| 217 |
unsigned int steps2 = this->steps; |
| 218 |
|
| 219 |
if (this->variable) |
| 220 |
steps2 *= (1 << (maxlevel - level)); |
| 221 |
|
| 222 |
typename VectorMemory<BlockVector<number>>::Pointer r(*this->mem); |
| 223 |
typename VectorMemory<BlockVector<number>>::Pointer d(*this->mem); |
| 224 |
r->reinit(u); |
| 225 |
d->reinit(u); |
| 226 |
|
| 227 |
bool T = this->transpose; |
| 228 |
if (this->symmetric && (steps2 % 2 == 0)) |
| 229 |
T = false; |
| 230 |
|
| 231 |
for (unsigned int i = 0; i < steps2; ++i) |
| 232 |
{ |
| 233 |
if (T) |
| 234 |
{ |
| 235 |
matrices[level].vmult(*r, u); |
| 236 |
r->sadd(-1., 1., rhs); |
| 237 |
smoothers[level].Tvmult(*d, *r); |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
matrices[level].vmult(*r, u); |
| 242 |
r->sadd(-1., 1., rhs); |
| 243 |
smoothers[level].vmult(*d, *r); |
| 244 |
} |
| 245 |
u += *d; |
| 246 |
if (this->symmetric) |
| 247 |
T = !T; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
#endif // DOXYGEN |
| 252 |
|
| 253 |
DEAL_II_NAMESPACE_CLOSE |
| 254 |
|
| 255 |
#endif |