Dune Core Modules (2.7.1)

btdmatrix.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ISTL_BTDMATRIX_HH
4#define DUNE_ISTL_BTDMATRIX_HH
5
10
16namespace Dune {
26 template <class B, class A=std::allocator<B> >
27 class BTDMatrix : public BCRSMatrix<B,A>
28 {
29 public:
30
31 //===== type definitions and constants
32
34 using field_type = typename Imp::BlockTraits<B>::field_type;
35
37 typedef B block_type;
38
40 typedef A allocator_type;
41
43 //typedef BCRSMatrix<B,A>::row_type row_type;
44
46 typedef typename A::size_type size_type;
47
49 static constexpr unsigned int blocklevel = Imp::BlockTraits<B>::blockLevel()+1;
50
52 BTDMatrix() : BCRSMatrix<B,A>() {}
53
54 explicit BTDMatrix(size_type size)
55 : BCRSMatrix<B,A>(size, size, BCRSMatrix<B,A>::random)
56 {
57 // Set number of entries for each row
58 // All rows get three entries, except for the first and the last one
59 for (size_t i=0; i<size; i++)
60 this->BCRSMatrix<B,A>::setrowsize(i, 3 - (i==0) - (i==(size-1)));
61
63
64 // The actual entries for each row
65 for (size_t i=0; i<size; i++) {
66 if (i>0)
67 this->BCRSMatrix<B,A>::addindex(i, i-1);
68 this->BCRSMatrix<B,A>::addindex(i, i );
69 if (i<size-1)
70 this->BCRSMatrix<B,A>::addindex(i, i+1);
71 }
72
74 }
75
77 void setSize(size_type size)
78 {
79 auto nonZeros = (size==0) ? 0 : size + 2*(size-1);
80 this->BCRSMatrix<B,A>::setSize(size, // rows
81 size, // columns
82 nonZeros);
83
84 // Set number of entries for each row
85 // All rows get three entries, except for the first and the last one
86 for (size_t i=0; i<size; i++)
87 this->BCRSMatrix<B,A>::setrowsize(i, 3 - (i==0) - (i==(size-1)));
88
90
91 // The actual entries for each row
92 for (size_t i=0; i<size; i++) {
93 if (i>0)
94 this->BCRSMatrix<B,A>::addindex(i, i-1);
95 this->BCRSMatrix<B,A>::addindex(i, i );
96 if (i<size-1)
97 this->BCRSMatrix<B,A>::addindex(i, i+1);
98 }
99
101 }
102
105 this->BCRSMatrix<B,A>::operator=(other);
106 return *this;
107 }
108
112 return *this;
113 }
114
120 template <class V>
121 void solve (V& x, const V& rhs) const {
122
123 // special handling for 1x1 matrices. The generic algorithm doesn't work for them
124 if (this->N()==1) {
125 auto&& x0 = Impl::asVector(x[0]);
126 auto&& rhs0 = Impl::asVector(rhs[0]);
127 Impl::asMatrix((*this)[0][0]).solve(x0, rhs0);
128 return;
129 }
130
131 // Make copies of the rhs and the right matrix band
132 V d = rhs;
133 std::vector<block_type> c(this->N()-1);
134 for (size_t i=0; i<this->N()-1; i++)
135 c[i] = (*this)[i][i+1];
136
137 /* Modify the coefficients. */
138 block_type a_00_inv = (*this)[0][0];
139 Impl::asMatrix(a_00_inv).invert();
140
141 //c[0] /= (*this)[0][0]; /* Division by zero risk. */
142 block_type tmp = a_00_inv;
143 Impl::asMatrix(tmp).rightmultiply(Impl::asMatrix(c[0]));
144 c[0] = tmp;
145
146 // d = a^{-1} d /* Division by zero would imply a singular matrix. */
147 auto d_0_tmp = d[0];
148 auto&& d_0 = Impl::asVector(d[0]);
149 Impl::asMatrix(a_00_inv).mv(Impl::asVector(d_0_tmp),d_0);
150
151 for (unsigned int i = 1; i < this->N(); i++) {
152
153 // id = ( a_ii - c_{i-1} a_{i, i-1} ) ^{-1}
154 block_type tmp;
155 tmp = (*this)[i][i-1];
156 Impl::asMatrix(tmp).rightmultiply(Impl::asMatrix(c[i-1]));
157
158 block_type id = (*this)[i][i];
159 id -= tmp;
160 Impl::asMatrix(id).invert(); /* Division by zero risk. */
161
162 if (i<c.size()) {
163 Impl::asMatrix(c[i]).leftmultiply(Impl::asMatrix(id)); /* Last value calculated is redundant. */
164 }
165
166 // d[i] = (d[i] - d[i-1] * (*this)[i][i-1]) * id;
167 auto&& d_i = Impl::asVector(d[i]);
168 Impl::asMatrix((*this)[i][i-1]).mmv(Impl::asVector(d[i-1]), d_i);
169 auto tmpVec = d[i];
170 Impl::asMatrix(id).mv(Impl::asVector(tmpVec), d_i);
171 }
172
173 /* Now back substitute. */
174 x[this->N() - 1] = d[this->N() - 1];
175 for (int i = this->N() - 2; i >= 0; i--) {
176 //x[i] = d[i] - c[i] * x[i + 1];
177 x[i] = d[i];
178 auto&& x_i = Impl::asVector(x[i]);
179 Impl::asMatrix(c[i]).mmv(Impl::asVector(x[i+1]), x_i);
180 }
181
182 }
183
184 private:
185
186 // ////////////////////////////////////////////////////////////////////////////
187 // The following methods from the base class should now actually be called
188 // ////////////////////////////////////////////////////////////////////////////
189
190 // createbegin and createend should be in there, too, but I can't get it to compile
191 // BCRSMatrix<B,A>::CreateIterator createbegin () {}
192 // BCRSMatrix<B,A>::CreateIterator createend () {}
193 void setrowsize (size_type i, size_type s) {}
194 void incrementrowsize (size_type i) {}
195 void endrowsizes () {}
196 void addindex (size_type row, size_type col) {}
197 void endindices () {}
198 };
201} // end namespace Dune
202
203#endif
Implementation of the BCRSMatrix class.
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:426
void endrowsizes()
indicate that size of all rows is defined
Definition: bcrsmatrix.hh:1108
@ random
Build entries randomly.
Definition: bcrsmatrix.hh:489
void addindex(size_type row, size_type col)
add index (row,col) to the matrix
Definition: bcrsmatrix.hh:1150
void endindices()
indicate that all indices are defined, check consistency
Definition: bcrsmatrix.hh:1207
size_type N() const
number of rows (counted in blocks)
Definition: bcrsmatrix.hh:1931
void setSize(size_type rows, size_type columns, size_type nnz=0)
Set the size of the matrix.
Definition: bcrsmatrix.hh:820
BCRSMatrix & operator=(const BCRSMatrix &Mat)
assignment
Definition: bcrsmatrix.hh:870
A block-tridiagonal matrix.
Definition: btdmatrix.hh:28
void solve(V &x, const V &rhs) const
Use the Thomas algorithm to solve the system Ax=b in O(n) time.
Definition: btdmatrix.hh:121
A::size_type size_type
implement row_type with compressed vector
Definition: btdmatrix.hh:46
A allocator_type
export the allocator type
Definition: btdmatrix.hh:40
static constexpr unsigned int blocklevel
increment block level counter
Definition: btdmatrix.hh:49
B block_type
export the type representing the components
Definition: btdmatrix.hh:37
typename Imp::BlockTraits< B >::field_type field_type
export the type representing the field
Definition: btdmatrix.hh:34
BTDMatrix & operator=(const BTDMatrix &other)
assignment
Definition: btdmatrix.hh:104
BTDMatrix()
Default constructor.
Definition: btdmatrix.hh:52
void setSize(size_type size)
Resize the matrix. Invalidates the content!
Definition: btdmatrix.hh:77
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Dune namespace.
Definition: alignedallocator.hh:14
Implements a scalar matrix view wrapper around an existing scalar.
Implements a scalar vector view wrapper around an existing scalar.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)