// 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_MATRIXUTILS_HH #define DUNE_ISTL_MATRIXUTILS_HH #include #include #include #include #include #include #include #include #include #include "istlexception.hh" namespace Dune { #ifndef DOYXGEN template class BCRSMatrix; template class FieldMatrix; template class Matrix; #endif /** * @addtogroup ISTL_SPMV * @{ */ /** * @file * @brief Some handy generic functions for ISTL matrices. * @author Markus Blatt */ /** * @brief Check whether the a matrix has diagonal values * on blocklevel recursion levels. */ template struct CheckIfDiagonalPresent { /** * @brief Check whether the a matrix has diagonal values * on blocklevel recursion levels. */ static void check([[maybe_unused]] const Matrix& mat) { #ifdef DUNE_ISTL_WITH_CHECKING typedef typename Matrix::ConstRowIterator Row; typedef typename Matrix::ConstColIterator Entry; for(Row row = mat.begin(); row!=mat.end(); ++row) { Entry diagonal = row->find(row.index()); if(diagonal==row->end()) DUNE_THROW(ISTLError, "Missing diagonal value in row "<::check(m); } } #endif } }; template struct CheckIfDiagonalPresent { static void check(const Matrix& mat) { typedef typename Matrix::ConstRowIterator Row; for(Row row = mat.begin(); row!=mat.end(); ++row) { if(row->find(row.index())==row->end()) DUNE_THROW(ISTLError, "Missing diagonal value in row "< class MultiTypeBlockMatrix; template struct CheckIfDiagonalPresent, blocklevel,l> { typedef MultiTypeBlockMatrix Matrix; /** * @brief Check whether the a matrix has diagonal values * on blocklevel recursion levels. */ static void check(const Matrix& /* mat */) { #ifdef DUNE_ISTL_WITH_CHECKING // TODO Implement check #endif } }; /** * @brief Get the number of nonzero fields in the matrix. * * This is not the number of nonzero blocks, but the number of non * zero scalar entries (on blocklevel 1) if the matrix is viewed as * a flat matrix. * * For FieldMatrix this is simply the number of columns times the * number of rows, for a BCRSMatrix> this is the * number of nonzero blocks time n*m. */ template inline auto countNonZeros(const M&, [[maybe_unused]] typename std::enable_if_t::value>* sfinae = nullptr) { return 1; } template inline auto countNonZeros(const M& matrix, [[maybe_unused]] typename std::enable_if_t::value>* sfinae = nullptr) { typename M::size_type nonZeros = 0; for(auto&& row : matrix) for(auto&& entry : row) nonZeros += countNonZeros(entry); return nonZeros; } /* template struct ProcessOnFieldsOfMatrix */ /** @} */ namespace { struct CompPair { template bool operator()(const std::pair& p1, const std::pair& p2) const { return p1.first void printGlobalSparseMatrix(const M& mat, C& ooc, std::ostream& os) { typedef typename C::ParallelIndexSet::const_iterator IIter; typedef typename C::OwnerSet OwnerSet; typedef typename C::ParallelIndexSet::GlobalIndex GlobalIndex; GlobalIndex gmax=0; for(IIter idx=ooc.indexSet().begin(), eidx=ooc.indexSet().end(); idx!=eidx; ++idx) gmax=std::max(gmax,idx->global()); gmax=ooc.communicator().max(gmax); ooc.buildGlobalLookup(); for(IIter idx=ooc.indexSet().begin(), eidx=ooc.indexSet().end(); idx!=eidx; ++idx) { if(OwnerSet::contains(idx->local().attribute())) { typedef typename M::block_type Block; std::set,CompPair> entries; // sort rows typedef typename M::ConstColIterator CIter; for(CIter c=mat[idx->local()].begin(), cend=mat[idx->local()].end(); c!=cend; ++c) { const typename C::ParallelIndexSet::IndexPair* pair =ooc.globalLookup().pair(c.index()); assert(pair); entries.insert(std::make_pair(pair->global(), *c)); } //wait until its the rows turn. GlobalIndex rowidx = idx->global(); GlobalIndex cur=std::numeric_limits::max(); while(cur!=rowidx) cur=ooc.communicator().min(rowidx); // print rows typedef typename std::set,CompPair>::iterator SIter; for(SIter s=entries.begin(), send=entries.end(); s!=send; ++s) os<global()<<" "<first<<" "<second<::max(); while(cur!=ooc.communicator().min(cur)) ; } // Default implementation for scalar types template struct MatrixDimension { static_assert(IsNumber::value, "MatrixDimension is not implemented for this type!"); static auto rowdim(const M& /*A*/) { return 1; } static auto coldim(const M& /*A*/) { return 1; } }; // Default implementation for scalar types template struct MatrixDimension > { using block_type = typename Matrix::block_type; using size_type = typename Matrix::size_type; static size_type rowdim (const Matrix& A, size_type i) { return MatrixDimension::rowdim(A[i][0]); } static size_type coldim (const Matrix& A, size_type c) { return MatrixDimension::coldim(A[0][c]); } static size_type rowdim (const Matrix& A) { size_type nn=0; for (size_type i=0; i& A) { size_type nn=0; for (size_type i=0; i struct MatrixDimension > { typedef BCRSMatrix Matrix; typedef typename Matrix::block_type block_type; typedef typename Matrix::size_type size_type; static size_type rowdim (const Matrix& A, size_type i) { const B* row = A.r[i].getptr(); if(row) return MatrixDimension::rowdim(*row); else return 0; } static size_type coldim (const Matrix& A, size_type c) { // find an entry in column c if (A.nnz_ > 0) { for (size_type k=0; k::coldim(A.a[k]); } } } else { for (size_type i=0; i::coldim(a[k]); } } } // not found return 0; } static size_type rowdim (const Matrix& A){ size_type nn=0; for (size_type i=0; i coldims(A.M(), std::numeric_limits::max()); for (ConstRowIterator row=A.begin(); row!=A.end(); ++row) for (ConstColIterator col=row->begin(); col!=row->end(); ++col) // only compute blocksizes we don't already have if (coldims[col.index()]==std::numeric_limits::max()) coldims[col.index()] = MatrixDimension::coldim(*col); size_type sum = 0; for (typename std::vector::iterator it=coldims.begin(); it!=coldims.end(); ++it) // skip rows for which no coldim could be determined if ((*it)>=0) sum += *it; return sum; } }; template struct MatrixDimension ,TA> > { typedef BCRSMatrix ,TA> Matrix; typedef typename Matrix::size_type size_type; static size_type rowdim (const Matrix& /*A*/, size_type /*i*/) { return n; } static size_type coldim (const Matrix& /*A*/, size_type /*c*/) { return m; } static size_type rowdim (const Matrix& A) { return A.N()*n; } static size_type coldim (const Matrix& A) { return A.M()*m; } }; template struct MatrixDimension > { typedef FieldMatrix Matrix; typedef typename Matrix::size_type size_type; static size_type rowdim(const Matrix& /*A*/, size_type /*r*/) { return 1; } static size_type coldim(const Matrix& /*A*/, size_type /*r*/) { return 1; } static size_type rowdim(const Matrix& /*A*/) { return n; } static size_type coldim(const Matrix& /*A*/) { return m; } }; template struct MatrixDimension > { typedef Dune::DynamicMatrix MatrixType; typedef typename MatrixType::size_type size_type; static size_type rowdim(const MatrixType& /*A*/, size_type /*r*/) { return 1; } static size_type coldim(const MatrixType& /*A*/, size_type /*r*/) { return 1; } static size_type rowdim(const MatrixType& A) { return A.N(); } static size_type coldim(const MatrixType& A) { return A.M(); } }; template struct MatrixDimension, TA> > { typedef Matrix, TA> ThisMatrix; typedef typename ThisMatrix::size_type size_type; static size_type rowdim(const ThisMatrix& /*A*/, size_type /*r*/) { return n; } static size_type coldim(const ThisMatrix& /*A*/, size_type /*r*/) { return m; } static size_type rowdim(const ThisMatrix& A) { return A.N()*n; } static size_type coldim(const ThisMatrix& A) { return A.M()*m; } }; template struct MatrixDimension > { typedef DiagonalMatrix Matrix; typedef typename Matrix::size_type size_type; static size_type rowdim(const Matrix& /*A*/, size_type /*r*/) { return 1; } static size_type coldim(const Matrix& /*A*/, size_type /*r*/) { return 1; } static size_type rowdim(const Matrix& /*A*/) { return n; } static size_type coldim(const Matrix& /*A*/) { return n; } }; template struct MatrixDimension > { typedef ScaledIdentityMatrix Matrix; typedef typename Matrix::size_type size_type; static size_type rowdim(const Matrix& /*A*/, size_type /*r*/) { return 1; } static size_type coldim(const Matrix& /*A*/, size_type /*r*/) { return 1; } static size_type rowdim(const Matrix& /*A*/) { return n; } static size_type coldim(const Matrix& /*A*/) { return n; } }; /** * @brief Test whether a type is an ISTL Matrix */ template struct IsMatrix { enum { /** * @brief True if T is an ISTL matrix */ value = false }; }; template struct IsMatrix > { enum { /** * @brief True if T is an ISTL matrix */ value = true }; }; template struct IsMatrix > { enum { /** * @brief True if T is an ISTL matrix */ value = true }; }; template struct [[deprecated("Use a lambda instead instead of PointerCompare!")]] PointerCompare { bool operator()(const T* l, const T* r) { return *l < *r; } }; } #endif