// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2005 - 2024 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_mg_block_smoother_h #define dealii_mg_block_smoother_h #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN /* * MGSmootherBase is defined in mg_base.h */ /** * @addtogroup mg * @{ */ /** * General smoother class for block vectors. This class gives complete freedom * to the choice of a block smoother by being initialized with a matrix and a * smoother object. Therefore, the smoother object for each level must be * constructed by hand. */ template class MGSmootherBlock : public MGSmoother> { public: /** * Constructor. */ MGSmootherBlock(const unsigned int steps = 1, const bool variable = false, const bool symmetric = false, const bool transpose = false, const bool reverse = false); /** * Initialize for matrices. The parameter matrices can be any * object having functions get_minlevel() and * get_maxlevel() as well as an operator[] returning a * reference to @p MatrixType. * * The same convention is used for the parameter smoothers, such * that operator[] returns the object doing the block-smoothing on * a single level. * * This function stores pointers to the level matrices and smoothing * operator for each level. */ template void initialize(const MGMatrixType &matrices, const MGRelaxationType &smoothers); /** * Empty all vectors. */ void clear(); /** * Switch on/off reversed. This is mutually exclusive with transpose(). */ void set_reverse(const bool); /** * Implementation of the interface for @p Multigrid. This function does * nothing, which by comparison with the definition of this function means * that the smoothing operator equals the null operator. */ virtual void smooth(const unsigned int level, BlockVector &u, const BlockVector &rhs) const; /** * Memory used by this object. */ std::size_t memory_consumption() const; private: /** * Pointer to the matrices. */ MGLevelObject>> matrices; /** * Pointer to the matrices. */ MGLevelObject>> smoothers; /** * Reverse? */ bool reverse; /** * Memory for auxiliary vectors. */ ObserverPointer>, MGSmootherBlock> mem; }; /** @} */ //--------------------------------------------------------------------------- #ifndef DOXYGEN template inline MGSmootherBlock::MGSmootherBlock( const unsigned int steps, const bool variable, const bool symmetric, const bool transpose, const bool reverse) : MGSmoother>(steps, variable, symmetric, transpose) , reverse(reverse) , mem(&this->vector_memory) {} template inline void MGSmootherBlock::clear() { unsigned int i = matrices.min_level(), max_level = matrices.max_level(); for (; i <= max_level; ++i) { smoothers[i] = LinearOperator>(); matrices[i] = LinearOperator>(); } } template template inline void MGSmootherBlock::initialize( const MGMatrixType &m, const MGRelaxationType &s) { const unsigned int min = m.min_level(); const unsigned int max = m.max_level(); matrices.resize(min, max); smoothers.resize(min, max); for (unsigned int i = min; i <= max; ++i) { // Workaround: Unfortunately, not every "m[i]" object has a // rich enough interface to populate reinit_(domain|range)_vector. // Thus, apply an empty LinearOperator exemplar. matrices[i] = linear_operator>( LinearOperator>(), m[i]); smoothers[i] = linear_operator>(matrices[i], s[i]); } } template inline void MGSmootherBlock::set_reverse( const bool flag) { reverse = flag; } template inline std::size_t MGSmootherBlock::memory_consumption() const { return sizeof(*this) + matrices.memory_consumption() + smoothers.memory_consumption() + this->vector_memory.memory_consumption(); } template inline void MGSmootherBlock::smooth( const unsigned int level, BlockVector &u, const BlockVector &rhs) const { LogStream::Prefix prefix("Smooth"); unsigned int maxlevel = matrices.max_level(); unsigned int steps2 = this->steps; if (this->variable) steps2 *= (1 << (maxlevel - level)); typename VectorMemory>::Pointer r(*this->mem); typename VectorMemory>::Pointer d(*this->mem); r->reinit(u); d->reinit(u); bool T = this->transpose; if (this->symmetric && (steps2 % 2 == 0)) T = false; for (unsigned int i = 0; i < steps2; ++i) { if (T) { matrices[level].vmult(*r, u); r->sadd(-1., 1., rhs); smoothers[level].Tvmult(*d, *r); } else { matrices[level].vmult(*r, u); r->sadd(-1., 1., rhs); smoothers[level].vmult(*d, *r); } u += *d; if (this->symmetric) T = !T; } } #endif // DOXYGEN DEAL_II_NAMESPACE_CLOSE #endif