Debian Codegraph @

dune/istl/bdmatrix.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_ISTL_BDMATRIX_HH
6 #define DUNE_ISTL_BDMATRIX_HH
7
8 #include <memory>
9
10 #include <dune/common/rangeutilities.hh>
11 #include <dune/common/scalarmatrixview.hh>
12
13 #include <dune/istl/bcrsmatrix.hh>
14 #include <dune/istl/blocklevel.hh>
15
16 /** \file
17 \author Oliver Sander
18 \brief Implementation of the BDMatrix class
19 */
20
21 namespace Dune {
22 /**
23 * @addtogroup ISTL_SPMV
24 * @{
25 */
26 /** \brief A block-diagonal matrix
27
28 \todo It would be safer and more efficient to have a real implementation of
29 a block-diagonal matrix and not just subclassing from BCRSMatrix. But that's
30 quite a lot of work for that little advantage.*/
31 template <class B, class A=std::allocator<B> >
32 class BDMatrix : public BCRSMatrix<B,A>
33 {
34 public:
35
36 //===== type definitions and constants
37
38 //! export the type representing the field
39 using field_type = typename Imp::BlockTraits<B>::field_type;
40
41 //! export the type representing the components
42 typedef B block_type;
43
44 //! export the allocator type
45 typedef A allocator_type;
46
47 //! implement row_type with compressed vector
48 //typedef BCRSMatrix<B,A>::row_type row_type;
49
50 //! The type for the index access and the size
51 typedef typename A::size_type size_type;
52
53 /** \brief Default constructor */
54 BDMatrix() : BCRSMatrix<B,A>() {}
55
56 explicit BDMatrix(int size)
57 : BCRSMatrix<B,A>(size, size, BCRSMatrix<B,A>::random) {
58
59 for (int i=0; i<size; i++)
60 this->BCRSMatrix<B,A>::setrowsize(i, 1);
61
62 this->BCRSMatrix<B,A>::endrowsizes();
63
64 for (int i=0; i<size; i++)
65 this->BCRSMatrix<B,A>::addindex(i, i);
66
67 this->BCRSMatrix<B,A>::endindices();
68
69 }
70
71 /** \brief Construct from a std::initializer_list */
72 BDMatrix (std::initializer_list<B> const &list)
73 : BDMatrix(list.size())
74 {
75 size_t i=0;
76 for (auto it = list.begin(); it != list.end(); ++it, ++i)
77 (*this)[i][i] = *it;
78 }
79
80 /** \brief Resize the matrix. Invalidates the content! */
81 void setSize(size_type size)
82 {
83 this->BCRSMatrix<B,A>::setSize(size, // rows
84 size, // columns
85 size); // nonzeros
86
87 for (auto i : range(size))
88 this->BCRSMatrix<B,A>::setrowsize(i, 1);
89
90 this->BCRSMatrix<B,A>::endrowsizes();
91
92 for (auto i : range(size))
93 this->BCRSMatrix<B,A>::addindex(i, i);
94
95 this->BCRSMatrix<B,A>::endindices();
96 }
97
98 //! assignment
99 BDMatrix& operator= (const BDMatrix& other) {
100 this->BCRSMatrix<B,A>::operator=(other);
101 return *this;
102 }
103
104 //! assignment from scalar
105 BDMatrix& operator= (const field_type& k) {
106 this->BCRSMatrix<B,A>::operator=(k);
107 return *this;
108 }
109
110 /** \brief Solve the system Ax=b in O(n) time
111 *
112 * \exception ISTLError if the matrix is singular
113 *
114 */
115 template <class V>
116 void solve (V& x, const V& rhs) const {
117 for (size_type i=0; i<this->N(); i++)
118 {
119 auto&& xv = Impl::asVector(x[i]);
120 auto&& rhsv = Impl::asVector(rhs[i]);
121 Impl::asMatrix((*this)[i][i]).solve(xv,rhsv);
122 }
123 }
124
125 /** \brief Inverts the matrix */
126 void invert() {
127 for (size_type i=0; i<this->N(); i++)
128 Impl::asMatrix((*this)[i][i]).invert();
129 }
130
131 private:
132
133 // ////////////////////////////////////////////////////////////////////////////
134 // The following methods from the base class should now actually be called
135 // ////////////////////////////////////////////////////////////////////////////
136
137 // createbegin and createend should be in there, too, but I can't get it to compile
138 // BCRSMatrix<B,A>::CreateIterator createbegin () {}
139 // BCRSMatrix<B,A>::CreateIterator createend () {}
140 void setrowsize (size_type i, size_type s) {}
141 void incrementrowsize (size_type i) {}
142 void endrowsizes () {}
143 void addindex (size_type row, size_type col) {}
144 void endindices () {}
145 };
146
147 template<typename B, typename A>
148 struct FieldTraits< BDMatrix<B, A> >
149 {
150 using field_type = typename BDMatrix<B, A>::field_type;
151 using real_type = typename FieldTraits<field_type>::real_type;
152 };
153 /** @}*/
154
155 } // end namespace Dune
156
157 #endif