// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2003 - 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_matrix_h #define dealii_mg_matrix_h #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN /** * @addtogroup mg * @{ */ namespace mg { /** * Multilevel matrix. This matrix stores an MGLevelObject of * LinearOperator objects. It implements the interface defined in * MGMatrixBase, so that it can be used as a matrix in Multigrid. */ template > class Matrix : public MGMatrixBase { public: /** * Default constructor for an empty object. */ Matrix() = default; /** * Constructor setting up pointers to the matrices in M by * calling initialize(). */ template Matrix(const MGLevelObject &M); /** * Initialize the object such that the level multiplication uses the * matrices in M */ template void initialize(const MGLevelObject &M); /** * Reset the object. */ void reset(); /** * Access matrix on a level. */ const LinearOperator & operator[](unsigned int level) const; virtual void vmult(const unsigned int level, VectorType &dst, const VectorType &src) const override; virtual void vmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const override; virtual void Tvmult(const unsigned int level, VectorType &dst, const VectorType &src) const override; virtual void Tvmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const override; virtual unsigned int get_minlevel() const override; virtual unsigned int get_maxlevel() const override; /** * Memory used by this object. */ std::size_t memory_consumption() const; private: MGLevelObject> matrices; }; } // namespace mg /** * Multilevel matrix selecting from block matrices. This class implements the * interface defined by MGMatrixBase. The template parameter @p MatrixType * should be a block matrix class like BlockSparseMatrix or @p * BlockSparseMatrixEZ. Then, this class stores a pointer to a MGLevelObject * of this matrix class. In each @p vmult, the block selected on * initialization will be multiplied with the vector provided. */ template class MGMatrixSelect : public MGMatrixBase> { public: /** * Constructor. @p row and @p col are the coordinate of the selected block. * The other argument is handed over to the @p ObserverPointer constructor. */ MGMatrixSelect(const unsigned int row = 0, const unsigned int col = 0, MGLevelObject *matrix = 0); /** * Set the matrix object to be used. The matrix object must exist longer as * the @p MGMatrixSelect object, since only a pointer is stored. */ void set_matrix(MGLevelObject *M); /** * Select the block for multiplication. */ void select_block(const unsigned int row, const unsigned int col); /** * Matrix-vector-multiplication on a certain level. */ virtual void vmult(const unsigned int level, Vector &dst, const Vector &src) const; /** * Adding matrix-vector-multiplication on a certain level. */ virtual void vmult_add(const unsigned int level, Vector &dst, const Vector &src) const; /** * Transpose matrix-vector-multiplication on a certain level. */ virtual void Tvmult(const unsigned int level, Vector &dst, const Vector &src) const; /** * Adding transpose matrix-vector-multiplication on a certain level. */ virtual void Tvmult_add(const unsigned int level, Vector &dst, const Vector &src) const; private: /** * Pointer to the matrix objects on each level. */ ObserverPointer, MGMatrixSelect> matrix; /** * Row coordinate of selected block. */ unsigned int row; /** * Column coordinate of selected block. */ unsigned int col; }; /** @} */ /*----------------------------------------------------------------------*/ namespace mg { template template inline void Matrix::initialize(const MGLevelObject &p) { matrices.resize(p.min_level(), p.max_level()); for (unsigned int level = p.min_level(); level <= p.max_level(); ++level) { // Workaround: Unfortunately, not every "p[level]" object has a // rich enough interface to populate reinit_(domain|range)_vector. // Thus, apply an empty LinearOperator exemplar. matrices[level] = linear_operator(LinearOperator(), Utilities::get_underlying_value( p[level])); } } template inline void Matrix::reset() { matrices.resize(0, 0); } template template inline Matrix::Matrix(const MGLevelObject &p) { initialize(p); } template inline const LinearOperator & Matrix::operator[](unsigned int level) const { return matrices[level]; } template void Matrix::vmult(const unsigned int level, VectorType &dst, const VectorType &src) const { matrices[level].vmult(dst, src); } template void Matrix::vmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const { matrices[level].vmult_add(dst, src); } template void Matrix::Tvmult(const unsigned int level, VectorType &dst, const VectorType &src) const { matrices[level].Tvmult(dst, src); } template void Matrix::Tvmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const { matrices[level].Tvmult_add(dst, src); } template unsigned int Matrix::get_minlevel() const { return matrices.min_level(); } template unsigned int Matrix::get_maxlevel() const { return matrices.max_level(); } template inline std::size_t Matrix::memory_consumption() const { return sizeof(*this) + matrices->memory_consumption(); } } // namespace mg /*----------------------------------------------------------------------*/ template MGMatrixSelect::MGMatrixSelect(const unsigned int row, const unsigned int col, MGLevelObject *p) : matrix(p, typeid(*this).name()) , row(row) , col(col) {} template void MGMatrixSelect::set_matrix(MGLevelObject *p) { matrix = p; } template void MGMatrixSelect::select_block(const unsigned int brow, const unsigned int bcol) { row = brow; col = bcol; } template void MGMatrixSelect::vmult(const unsigned int level, Vector &dst, const Vector &src) const { Assert(matrix != 0, ExcNotInitialized()); const MGLevelObject &m = *matrix; m[level].block(row, col).vmult(dst, src); } template void MGMatrixSelect::vmult_add(const unsigned int level, Vector &dst, const Vector &src) const { Assert(matrix != 0, ExcNotInitialized()); const MGLevelObject &m = *matrix; m[level].block(row, col).vmult_add(dst, src); } template void MGMatrixSelect::Tvmult(const unsigned int level, Vector &dst, const Vector &src) const { Assert(matrix != 0, ExcNotInitialized()); const MGLevelObject &m = *matrix; m[level].block(row, col).Tvmult(dst, src); } template void MGMatrixSelect::Tvmult_add(const unsigned int level, Vector &dst, const Vector &src) const { Assert(matrix != 0, ExcNotInitialized()); const MGLevelObject &m = *matrix; m[level].block(row, col).Tvmult_add(dst, src); } DEAL_II_NAMESPACE_CLOSE #endif