// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2002 - 2025 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_coarse_h #define dealii_mg_coarse_h #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN /** * @addtogroup mg * @{ */ /** * Coarse grid solver using smoother only. This is a little wrapper, * transforming a smoother into a coarse grid solver. */ template > class MGCoarseGridApplySmoother : public MGCoarseGridBase { public: /** * Default constructor. */ MGCoarseGridApplySmoother(); /** * Constructor. Store a pointer to the smoother for later use. */ MGCoarseGridApplySmoother(const MGSmootherBase &coarse_smooth); /** * Clear the pointer. */ void clear(); /** * Initialize new data. */ void initialize(const MGSmootherBase &coarse_smooth); /** * Implementation of the abstract function. */ void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override; private: /** * Reference to the smoother. */ ObserverPointer, MGCoarseGridApplySmoother> coarse_smooth; }; /** * Coarse grid solver using an operator, e.g., preconditioner, only. * This is a little wrapper, transforming an operator into a coarse grid solver. */ template class MGCoarseGridApplyOperator : public MGCoarseGridBase { public: /** * Default constructor. */ MGCoarseGridApplyOperator(); /** * Constructor. Store a pointer to the operator for later use. */ MGCoarseGridApplyOperator(const MatrixType &matrix); /** * Clear the pointer. */ void clear(); /** * Initialize new data. */ void initialize(const MatrixType &matrix); /** * Implementation of the abstract function. */ virtual void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override; private: /** * Reference to the operator. */ ObserverPointer> matrix; }; /** * Coarse grid multigrid operator for an iterative solver. * * This class provides a wrapper for a deal.II iterative solver with a given * matrix and preconditioner as a coarse grid operator. */ template class MGCoarseGridIterativeSolver : public MGCoarseGridBase { public: /** * Default constructor. */ MGCoarseGridIterativeSolver(); /** * Constructor. Only a reference to these objects is stored, so * their lifetime needs to exceed the usage in this class. */ MGCoarseGridIterativeSolver(SolverType &solver, const MatrixType &matrix, const PreconditionerType &precondition); /** * Initialize with new data, see the corresponding constructor for more * details. */ void initialize(SolverType &solver, const MatrixType &matrix, const PreconditionerType &precondition); /** * Clear all pointers. */ void clear(); /** * Implementation of the abstract function. Calls the solve method of the * given solver with matrix, vectors, and preconditioner. */ virtual void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override; private: /** * Reference to the solver. */ ObserverPointer> solver; /** * Reference to the matrix. */ ObserverPointer> matrix; /** * Reference to the preconditioner. */ ObserverPointer> preconditioner; }; /** * Coarse grid solver by QR factorization implemented in the class * Householder. * * Upon initialization, the QR decomposition of the matrix is computed. then, * the operator() uses Householder::least_squares() to compute the action of * the inverse. */ template > class MGCoarseGridHouseholder : public MGCoarseGridBase { public: /** * Constructor, taking the coarse grid matrix. */ MGCoarseGridHouseholder(const FullMatrix *A = nullptr); /** * Initialize for a new matrix. */ void initialize(const FullMatrix &A); void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const override; private: /** * Matrix for QR-factorization. */ Householder householder; }; /** * Coarse grid solver using singular value decomposition of LAPACK matrices. * * Upon initialization, the singular value decomposition of the matrix is * computed. then, the operator() uses */ template > class MGCoarseGridSVD : public MGCoarseGridBase { public: /** * Constructor leaving an uninitialized object. */ MGCoarseGridSVD() = default; /** * Initialize for a new matrix. This resets the dimensions to the */ void initialize(const FullMatrix &A, const double threshold = 0); void operator()(const unsigned int level, VectorType &dst, const VectorType &src) const; /** * Write the singular values to @p deallog. */ void log() const; private: /** * Matrix for singular value decomposition. */ LAPACKFullMatrix matrix; }; /** @} */ #ifndef DOXYGEN /* ------------------ Functions for MGCoarseGridApplySmoother -----------*/ template MGCoarseGridApplySmoother::MGCoarseGridApplySmoother() : coarse_smooth(nullptr) {} template MGCoarseGridApplySmoother::MGCoarseGridApplySmoother( const MGSmootherBase &coarse_smooth) : coarse_smooth(nullptr) { initialize(coarse_smooth); } template void MGCoarseGridApplySmoother::initialize( const MGSmootherBase &coarse_smooth_) { coarse_smooth = ObserverPointer, MGCoarseGridApplySmoother>( &coarse_smooth_, typeid(*this).name()); } template void MGCoarseGridApplySmoother::clear() { coarse_smooth = nullptr; } template void MGCoarseGridApplySmoother::operator()(const unsigned int level, VectorType &dst, const VectorType &src) const { coarse_smooth->apply(level, dst, src); } /* ------------------ Functions for MGCoarseGridApplyOperator ----------*/ template MGCoarseGridApplyOperator::MGCoarseGridApplyOperator() : matrix(0, typeid(*this).name()) {} template MGCoarseGridApplyOperator:: MGCoarseGridApplyOperator(const PreconditionerType &matrix) : matrix(&matrix, typeid(*this).name()) {} template void MGCoarseGridApplyOperator::initialize( const PreconditionerType &matrix_) { matrix = &matrix_; } template void MGCoarseGridApplyOperator::clear() { matrix = 0; } template void MGCoarseGridApplyOperator::operator()( const unsigned int /*level*/, VectorType &dst, const VectorType &src) const { Assert(matrix != nullptr, ExcNotInitialized()); matrix->vmult(dst, src); } /* ------------------ Functions for MGCoarseGridIterativeSolver ------------ */ template MGCoarseGridIterativeSolver::MGCoarseGridIterativeSolver() : solver(0, typeid(*this).name()) , matrix(0, typeid(*this).name()) , preconditioner(0, typeid(*this).name()) {} template MGCoarseGridIterativeSolver:: MGCoarseGridIterativeSolver(SolverType &solver, const MatrixType &matrix, const PreconditionerType &preconditioner) : solver(&solver, typeid(*this).name()) , matrix(&matrix, typeid(*this).name()) , preconditioner(&preconditioner, typeid(*this).name()) {} template void MGCoarseGridIterativeSolver< VectorType, SolverType, MatrixType, PreconditionerType>::initialize(SolverType &solver_, const MatrixType &matrix_, const PreconditionerType &preconditioner_) { solver = &solver_; matrix = &matrix_; preconditioner = &preconditioner_; } template void MGCoarseGridIterativeSolver::clear() { solver = 0; matrix = 0; preconditioner = 0; } template void MGCoarseGridIterativeSolver< VectorType, SolverType, MatrixType, PreconditionerType>::operator()(const unsigned int /*level*/, VectorType &dst, const VectorType &src) const { Assert(solver != nullptr, ExcNotInitialized()); Assert(matrix != nullptr, ExcNotInitialized()); Assert(preconditioner != nullptr, ExcNotInitialized()); dst = 0; if constexpr (std::is_same_v) { solver->solve(*matrix, dst, src, *preconditioner); } else { typename SolverType::vector_type src_; typename SolverType::vector_type dst_; src_ = src; dst_ = dst; solver->solve(*matrix, dst_, src_, *preconditioner); dst = dst_; } } /* ------------------ Functions for MGCoarseGridHouseholder ------------ */ template MGCoarseGridHouseholder::MGCoarseGridHouseholder( const FullMatrix *A) { if (A != nullptr) householder.initialize(*A); } template void MGCoarseGridHouseholder::initialize( const FullMatrix &A) { householder.initialize(A); } template void MGCoarseGridHouseholder::operator()( const unsigned int /*level*/, VectorType &dst, const VectorType &src) const { householder.least_squares(dst, src); } //--------------------------------------------------------------------------- template void MGCoarseGridSVD::initialize(const FullMatrix &A, double threshold) { matrix.reinit(A.n_rows(), A.n_cols()); matrix = A; matrix.compute_inverse_svd(threshold); } template void MGCoarseGridSVD::operator()(const unsigned int /*level*/, VectorType &dst, const VectorType &src) const { matrix.vmult(dst, src); } template void MGCoarseGridSVD::log() const { const unsigned int n = std::min(matrix.n_rows(), matrix.n_cols()); for (unsigned int i = 0; i < n; ++i) deallog << ' ' << matrix.singular_value(i); deallog << std::endl; } #endif // DOXYGEN DEAL_II_NAMESPACE_CLOSE #endif