// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_ISTL_MULTITYPEBLOCKMATRIX_HH #define DUNE_ISTL_MULTITYPEBLOCKMATRIX_HH #include #include #include #include #include "istlexception.hh" // forward declaration namespace Dune { template class MultiTypeBlockMatrix; template class MultiTypeBlockMatrix_Solver; } #include "gsetc.hh" namespace Dune { /** @addtogroup ISTL_SPMV @{ */ /** @brief A Matrix class to support different block types This matrix class combines MultiTypeBlockVector elements as rows. */ template class MultiTypeBlockMatrix : public std::tuple { using ParentType = std::tuple; public: /** \brief Get the constructors from tuple */ using ParentType::ParentType; /** * own class' type */ typedef MultiTypeBlockMatrix type; /** \brief Type used for sizes */ using size_type = std::size_t; /** \brief The type used for scalars * * Use the `std::common_type_t` of the `Args`' field_type and use `nonesuch` if no implementation of * `std::common_type` is provided for the given `field_type` arguments. */ using field_type = Std::detected_t::field_type, typename FieldTraits::field_type...>; /** \brief The type used for real values * * Use the `std::common_type_t` of the `Args`' real_type and use `nonesuch` if no implementation of * `std::common_type` is provided for the given `real_type` arguments. */ using real_type = Std::detected_t::real_type, typename FieldTraits::real_type...>; // make sure that we have an std::common_type: using an assertion produces a more readable error message // than a compiler template instantiation error static_assert ( sizeof...(Args) == 0 or not (std::is_same_v or std::is_same_v), "No std::common_type implemented for the given field_type/real_type of the Args. Please provide an implementation and check that a FieldTraits class is present for your type."); /** \brief Return the number of matrix rows */ static constexpr size_type N() { return 1+sizeof...(Args); } /** \brief Return the number of matrix columns */ static constexpr size_type M() { return FirstRow::size(); } /** \brief Random-access operator * * This method mimics the behavior of normal vector access with square brackets like, e.g., m[5] = .... * The problem is that the return type is different for each value of the argument in the brackets. * Therefore we implement a trick using std::integral_constant. To access the first row of * a MultiTypeBlockMatrix named m write * \code * std::integral_constant _0; * m[_0] = ... * \endcode * The name '_0' used here as a static replacement of the integer number zero is arbitrary. * Any other variable name can be used. If you don't like the separate variable, you can write * \code * m[std::integral_constant()] = ... * \endcode */ template< size_type index > auto operator[] ([[maybe_unused]] const std::integral_constant< size_type, index > indexVariable) -> decltype(std::get(*this)) { return std::get(*this); } /** \brief Const random-access operator * * This is the const version of the random-access operator. See the non-const version for a full * explanation of how to use it. */ template< size_type index > auto operator[] ([[maybe_unused]] const std::integral_constant< size_type, index > indexVariable) const -> decltype(std::get(*this)) { return std::get(*this); } /** * assignment operator */ template void operator= (const T& newval) { using namespace Dune::Hybrid; auto size = index_constant<1+sizeof...(Args)>(); // Since Dune::Hybrid::size(MultiTypeBlockMatrix) is not implemented, // we cannot use a plain forEach(*this, ...). This could be achieved, // e.g., by implementing a static size() method. forEach(integralRange(size), [&](auto&& i) { (*this)[i] = newval; }); } //===== vector space arithmetic //! vector space multiplication with scalar MultiTypeBlockMatrix& operator*= (const field_type& k) { auto size = index_constant(); Hybrid::forEach(Hybrid::integralRange(size), [&](auto&& i) { (*this)[i] *= k; }); return *this; } //! vector space division by scalar MultiTypeBlockMatrix& operator/= (const field_type& k) { auto size = index_constant(); Hybrid::forEach(Hybrid::integralRange(size), [&](auto&& i) { (*this)[i] /= k; }); return *this; } /*! \brief Add the entries of another matrix to this one. * * \param b The matrix to add to this one. Its sparsity pattern * has to be subset of the sparsity pattern of this matrix. */ MultiTypeBlockMatrix& operator+= (const MultiTypeBlockMatrix& b) { auto size = index_constant(); Hybrid::forEach(Hybrid::integralRange(size), [&](auto&& i) { (*this)[i] += b[i]; }); return *this; } /*! \brief Subtract the entries of another matrix from this one. * * \param b The matrix to subtract from this one. Its sparsity pattern * has to be subset of the sparsity pattern of this matrix. */ MultiTypeBlockMatrix& operator-= (const MultiTypeBlockMatrix& b) { auto size = index_constant(); Hybrid::forEach(Hybrid::integralRange(size), [&](auto&& i) { (*this)[i] -= b[i]; }); return *this; } /** \brief y = A x */ template void mv (const X& x, Y& y) const { static_assert(X::size() == M(), "length of x does not match row length"); static_assert(Y::size() == N(), "length of y does not match row count"); y = 0; //reset y (for mv uses umv) umv(x,y); } /** \brief y += A x */ template void umv (const X& x, Y& y) const { static_assert(X::size() == M(), "length of x does not match row length"); static_assert(Y::size() == N(), "length of y does not match row count"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[i][j].umv(x[j], y[i]); }); }); } /** \brief y -= A x */ template void mmv (const X& x, Y& y) const { static_assert(X::size() == M(), "length of x does not match row length"); static_assert(Y::size() == N(), "length of y does not match row count"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[i][j].mmv(x[j], y[i]); }); }); } /** \brief y += alpha A x */ template void usmv (const AlphaType& alpha, const X& x, Y& y) const { static_assert(X::size() == M(), "length of x does not match row length"); static_assert(Y::size() == N(), "length of y does not match row count"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[i][j].usmv(alpha, x[j], y[i]); }); }); } /** \brief y = A^T x */ template void mtv (const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); y = 0; umtv(x,y); } /** \brief y += A^T x */ template void umtv (const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[j][i].umtv(x[j], y[i]); }); }); } /** \brief y -= A^T x */ template void mmtv (const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[j][i].mmtv(x[j], y[i]); }); }); } /** \brief y += alpha A^T x */ template void usmtv (const field_type& alpha, const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[j][i].usmtv(alpha, x[j], y[i]); }); }); } /** \brief y += A^H x */ template void umhv (const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[j][i].umhv(x[j], y[i]); }); }); } /** \brief y -= A^H x */ template void mmhv (const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[j][i].mmhv(x[j], y[i]); }); }); } /** \brief y += alpha A^H x */ template void usmhv (const field_type& alpha, const X& x, Y& y) const { static_assert(X::size() == N(), "length of x does not match number of rows"); static_assert(Y::size() == M(), "length of y does not match number of columns"); using namespace Dune::Hybrid; forEach(integralRange(Hybrid::size(y)), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(Hybrid::size(x)), [&](auto&& j) { (*this)[j][i].usmhv(alpha, x[j], y[i]); }); }); } //===== norms //! square of frobenius norm, need for block recursion real_type frobenius_norm2 () const { real_type sum=0; auto rows = index_constant(); Hybrid::forEach(Hybrid::integralRange(rows), [&](auto&& i) { auto cols = index_constant::M()>(); Hybrid::forEach(Hybrid::integralRange(cols), [&](auto&& j) { sum += (*this)[i][j].frobenius_norm2(); }); }); return sum; } //! frobenius norm: sqrt(sum over squared values of entries) real_type frobenius_norm () const { return sqrt(frobenius_norm2()); } //! Bastardized version of the infinity-norm / row-sum norm real_type infinity_norm () const { using std::max; real_type norm=0; auto rows = index_constant(); Hybrid::forEach(Hybrid::integralRange(rows), [&](auto&& i) { real_type sum=0; auto cols = index_constant::M()>(); Hybrid::forEach(Hybrid::integralRange(cols), [&](auto&& j) { sum += (*this)[i][j].infinity_norm(); }); norm = max(sum, norm); }); return norm; } //! Bastardized version of the infinity-norm / row-sum norm real_type infinity_norm_real () const { using std::max; real_type norm=0; auto rows = index_constant(); Hybrid::forEach(Hybrid::integralRange(rows), [&](auto&& i) { real_type sum=0; auto cols = index_constant::M()>(); Hybrid::forEach(Hybrid::integralRange(cols), [&](auto&& j) { sum += (*this)[i][j].infinity_norm_real(); }); norm = max(sum, norm); }); return norm; } }; /** @addtogroup DenseMatVec @{ */ template struct FieldTraits< MultiTypeBlockMatrix > { using field_type = typename MultiTypeBlockMatrix::field_type; using real_type = typename MultiTypeBlockMatrix::real_type; }; /** @brief << operator for a MultiTypeBlockMatrix operator<< for printing out a MultiTypeBlockMatrix */ template std::ostream& operator<< (std::ostream& s, const MultiTypeBlockMatrix& m) { auto N = index_constant::N()>(); auto M = index_constant::M()>(); using namespace Dune::Hybrid; forEach(integralRange(N), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(M), [&](auto&& j) { s << "\t(" << i << ", " << j << "): \n" << m[i][j]; }); }); s << std::endl; return s; } //make algmeta_itsteps known template struct algmeta_itsteps; /** @brief part of solvers for MultiTypeBlockVector & MultiTypeBlockMatrix types For the given row (index "crow") each element is used to calculate the equation's right side. */ template //MultiTypeBlockMatrix_Solver_Col: iterating over one row class MultiTypeBlockMatrix_Solver_Col { //calculating b- A[i][j]*x[j] public: /** * iterating over one row in MultiTypeBlockMatrix to calculate right side b-A[i][j]*x[j] */ template static void calc_rhs(const TMatrix& A, TVector& x, TVector& v, Trhs& b, const K& w) { std::get( std::get(A) ).mmv( std::get(x), b ); MultiTypeBlockMatrix_Solver_Col::calc_rhs(A,x,v,b,w); //next column element } }; template //MultiTypeBlockMatrix_Solver_Col recursion end class MultiTypeBlockMatrix_Solver_Col { public: template static void calc_rhs(const TMatrix&, TVector&, TVector&, Trhs&, const K&) {} }; /** @brief solver for MultiTypeBlockVector & MultiTypeBlockMatrix types The methods of this class are called by the solver specializations for MultiTypeBlockVector & MultiTypeBlockMatrix types (dbgs, bsorf, bsorb, dbjac). */ template class MultiTypeBlockMatrix_Solver { public: /** * Gauss-Seidel solver for MultiTypeBlockMatrix & MultiTypeBlockVector types */ template static void dbgs(const TMatrix& A, TVector& x, const TVector& b, const K& w) { TVector xold(x); xold=x; //store old x values MultiTypeBlockMatrix_Solver::dbgs(A,x,x,b,w); x *= w; x.axpy(1-w,xold); //improve x } template static void dbgs(const TMatrix& A, TVector& x, TVector& v, const TVector& b, const K& w) { auto rhs = std::get (b); MultiTypeBlockMatrix_Solver_Col::calc_rhs(A,x,v,rhs,w); // calculate right side of equation //solve on blocklevel I-1 using M = typename std::remove_cv< typename std::remove_reference< decltype(std::get( std::get(A))) >::type >::type; algmeta_itsteps::dbgs(std::get( std::get(A)), std::get(x),rhs,w); MultiTypeBlockMatrix_Solver::dbgs(A,x,v,b,w); //next row } /** * bsorf for MultiTypeBlockMatrix & MultiTypeBlockVector types */ template static void bsorf(const TMatrix& A, TVector& x, const TVector& b, const K& w) { TVector v; v=x; //use latest x values in right side calculation MultiTypeBlockMatrix_Solver::bsorf(A,x,v,b,w); } template //recursion over all matrix rows (A) static void bsorf(const TMatrix& A, TVector& x, TVector& v, const TVector& b, const K& w) { auto rhs = std::get (b); MultiTypeBlockMatrix_Solver_Col::calc_rhs(A,x,v,rhs,w); // calculate right side of equation //solve on blocklevel I-1 using M = typename std::remove_cv< typename std::remove_reference< decltype(std::get( std::get(A))) >::type >::type; algmeta_itsteps::bsorf(std::get( std::get(A)), std::get(v),rhs,w); std::get(x).axpy(w,std::get(v)); MultiTypeBlockMatrix_Solver::bsorf(A,x,v,b,w); //next row } /** * bsorb for MultiTypeBlockMatrix & MultiTypeBlockVector types */ template static void bsorb(const TMatrix& A, TVector& x, const TVector& b, const K& w) { TVector v; v=x; //use latest x values in right side calculation MultiTypeBlockMatrix_Solver::bsorb(A,x,v,b,w); } template //recursion over all matrix rows (A) static void bsorb(const TMatrix& A, TVector& x, TVector& v, const TVector& b, const K& w) { auto rhs = std::get (b); MultiTypeBlockMatrix_Solver_Col::calc_rhs(A,x,v,rhs,w); // calculate right side of equation //solve on blocklevel I-1 using M = typename std::remove_cv< typename std::remove_reference< decltype(std::get( std::get(A))) >::type >::type; algmeta_itsteps::bsorb(std::get( std::get(A)), std::get(v),rhs,w); std::get(x).axpy(w,std::get(v)); MultiTypeBlockMatrix_Solver::bsorb(A,x,v,b,w); //next row } /** * Jacobi solver for MultiTypeBlockMatrix & MultiTypeBlockVector types */ template static void dbjac(const TMatrix& A, TVector& x, const TVector& b, const K& w) { TVector v(x); v=0; //calc new x in v MultiTypeBlockMatrix_Solver::dbjac(A,x,v,b,w); x.axpy(w,v); //improve x } template static void dbjac(const TMatrix& A, TVector& x, TVector& v, const TVector& b, const K& w) { auto rhs = std::get (b); MultiTypeBlockMatrix_Solver_Col::calc_rhs(A,x,v,rhs,w); // calculate right side of equation //solve on blocklevel I-1 using M = typename std::remove_cv< typename std::remove_reference< decltype(std::get( std::get(A))) >::type >::type; algmeta_itsteps::dbjac(std::get( std::get(A)), std::get(v),rhs,w); MultiTypeBlockMatrix_Solver::dbjac(A,x,v,b,w); //next row } }; template //recursion end for remain_row = 0 class MultiTypeBlockMatrix_Solver { public: template static void dbgs(const TMatrix&, TVector&, TVector&, const TVector&, const K&) {} template static void bsorf(const TMatrix&, TVector&, TVector&, const TVector&, const K&) {} template static void bsorb(const TMatrix&, TVector&, TVector&, const TVector&, const K&) {} template static void dbjac(const TMatrix&, TVector&, TVector&, const TVector&, const K&) {} }; } // end namespace Dune namespace std { /** \brief Make std::tuple_element work for MultiTypeBlockMatrix * * It derives from std::tuple after all. */ template struct tuple_element > { using type = typename std::tuple_element >::type; }; /** \brief Make std::tuple_size work for MultiTypeBlockMatrix * * It derives from std::tuple after all. */ template struct tuple_size > : std::integral_constant {}; } #endif