// 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_BDMATRIX_HH #define DUNE_ISTL_BDMATRIX_HH #include #include #include #include #include /** \file \author Oliver Sander \brief Implementation of the BDMatrix class */ namespace Dune { /** * @addtogroup ISTL_SPMV * @{ */ /** \brief A block-diagonal matrix \todo It would be safer and more efficient to have a real implementation of a block-diagonal matrix and not just subclassing from BCRSMatrix. But that's quite a lot of work for that little advantage.*/ template > class BDMatrix : public BCRSMatrix { public: //===== type definitions and constants //! export the type representing the field using field_type = typename Imp::BlockTraits::field_type; //! export the type representing the components typedef B block_type; //! export the allocator type typedef A allocator_type; //! implement row_type with compressed vector //typedef BCRSMatrix::row_type row_type; //! The type for the index access and the size typedef typename A::size_type size_type; /** \brief Default constructor */ BDMatrix() : BCRSMatrix() {} explicit BDMatrix(int size) : BCRSMatrix(size, size, BCRSMatrix::random) { for (int i=0; iBCRSMatrix::setrowsize(i, 1); this->BCRSMatrix::endrowsizes(); for (int i=0; iBCRSMatrix::addindex(i, i); this->BCRSMatrix::endindices(); } /** \brief Construct from a std::initializer_list */ BDMatrix (std::initializer_list const &list) : BDMatrix(list.size()) { size_t i=0; for (auto it = list.begin(); it != list.end(); ++it, ++i) (*this)[i][i] = *it; } /** \brief Resize the matrix. Invalidates the content! */ void setSize(size_type size) { this->BCRSMatrix::setSize(size, // rows size, // columns size); // nonzeros for (auto i : range(size)) this->BCRSMatrix::setrowsize(i, 1); this->BCRSMatrix::endrowsizes(); for (auto i : range(size)) this->BCRSMatrix::addindex(i, i); this->BCRSMatrix::endindices(); } //! assignment BDMatrix& operator= (const BDMatrix& other) { this->BCRSMatrix::operator=(other); return *this; } //! assignment from scalar BDMatrix& operator= (const field_type& k) { this->BCRSMatrix::operator=(k); return *this; } /** \brief Solve the system Ax=b in O(n) time * * \exception ISTLError if the matrix is singular * */ template void solve (V& x, const V& rhs) const { for (size_type i=0; iN(); i++) { auto&& xv = Impl::asVector(x[i]); auto&& rhsv = Impl::asVector(rhs[i]); Impl::asMatrix((*this)[i][i]).solve(xv,rhsv); } } /** \brief Inverts the matrix */ void invert() { for (size_type i=0; iN(); i++) Impl::asMatrix((*this)[i][i]).invert(); } private: // //////////////////////////////////////////////////////////////////////////// // The following methods from the base class should now actually be called // //////////////////////////////////////////////////////////////////////////// // createbegin and createend should be in there, too, but I can't get it to compile // BCRSMatrix::CreateIterator createbegin () {} // BCRSMatrix::CreateIterator createend () {} void setrowsize (size_type i, size_type s) {} void incrementrowsize (size_type i) {} void endrowsizes () {} void addindex (size_type row, size_type col) {} void endindices () {} }; template struct FieldTraits< BDMatrix > { using field_type = typename BDMatrix::field_type; using real_type = typename FieldTraits::real_type; }; /** @}*/ } // end namespace Dune #endif