// 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_BTDMATRIX_HH #define DUNE_ISTL_BTDMATRIX_HH #include #include #include #include #include /** \file \author Oliver Sander \brief Implementation of the BTDMatrix class */ namespace Dune { /** * @addtogroup ISTL_SPMV * @{ */ /** \brief A block-tridiagonal matrix \todo It would be safer and more efficient to have a real implementation of a block-tridiagonal matrix and not just subclassing from BCRSMatrix. But that's quite a lot of work for that little advantage.*/ template > class BTDMatrix : 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 */ BTDMatrix() : BCRSMatrix() {} explicit BTDMatrix(size_type size) : BCRSMatrix(size, size, BCRSMatrix::random) { // Set number of entries for each row // All rows get three entries, except for the first and the last one for (size_t i=0; iBCRSMatrix::setrowsize(i, 3 - (i==0) - (i==(size-1))); this->BCRSMatrix::endrowsizes(); // The actual entries for each row for (size_t i=0; i0) this->BCRSMatrix::addindex(i, i-1); this->BCRSMatrix::addindex(i, i ); if (iBCRSMatrix::addindex(i, i+1); } this->BCRSMatrix::endindices(); } /** \brief Resize the matrix. Invalidates the content! */ void setSize(size_type size) { auto nonZeros = (size==0) ? 0 : size + 2*(size-1); this->BCRSMatrix::setSize(size, // rows size, // columns nonZeros); // Set number of entries for each row // All rows get three entries, except for the first and the last one for (size_t i=0; iBCRSMatrix::setrowsize(i, 3 - (i==0) - (i==(size-1))); this->BCRSMatrix::endrowsizes(); // The actual entries for each row for (size_t i=0; i0) this->BCRSMatrix::addindex(i, i-1); this->BCRSMatrix::addindex(i, i ); if (iBCRSMatrix::addindex(i, i+1); } this->BCRSMatrix::endindices(); } //! assignment BTDMatrix& operator= (const BTDMatrix& other) { this->BCRSMatrix::operator=(other); return *this; } //! assignment from scalar BTDMatrix& operator= (const field_type& k) { this->BCRSMatrix::operator=(k); return *this; } /** \brief Use the Thomas algorithm to 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 { // special handling for 1x1 matrices. The generic algorithm doesn't work for them if (this->N()==1) { auto&& x0 = Impl::asVector(x[0]); auto&& rhs0 = Impl::asVector(rhs[0]); Impl::asMatrix((*this)[0][0]).solve(x0, rhs0); return; } // Make copies of the rhs and the right matrix band V d = rhs; std::vector c(this->N()-1); for (size_t i=0; iN()-1; i++) c[i] = (*this)[i][i+1]; /* Modify the coefficients. */ block_type a_00_inv = (*this)[0][0]; Impl::asMatrix(a_00_inv).invert(); //c[0] /= (*this)[0][0]; /* Division by zero risk. */ block_type tmp = a_00_inv; Impl::asMatrix(tmp).rightmultiply(Impl::asMatrix(c[0])); c[0] = tmp; // d = a^{-1} d /* Division by zero would imply a singular matrix. */ auto d_0_tmp = d[0]; auto&& d_0 = Impl::asVector(d[0]); Impl::asMatrix(a_00_inv).mv(Impl::asVector(d_0_tmp),d_0); for (unsigned int i = 1; i < this->N(); i++) { // id = ( a_ii - c_{i-1} a_{i, i-1} ) ^{-1} block_type tmp; tmp = (*this)[i][i-1]; Impl::asMatrix(tmp).rightmultiply(Impl::asMatrix(c[i-1])); block_type id = (*this)[i][i]; id -= tmp; Impl::asMatrix(id).invert(); /* Division by zero risk. */ if (iN() - 1] = d[this->N() - 1]; for (int i = this->N() - 2; i >= 0; i--) { //x[i] = d[i] - c[i] * x[i + 1]; x[i] = d[i]; auto&& x_i = Impl::asVector(x[i]); Impl::asMatrix(c[i]).mmv(Impl::asVector(x[i+1]), x_i); } } 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< BTDMatrix > { using field_type = typename BTDMatrix::field_type; using real_type = typename FieldTraits::real_type; }; /** @}*/ } // end namespace Dune #endif