Debian Codegraph @

include/deal.II/multigrid/mg_matrix.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2003 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_mg_matrix_h
16 #define dealii_mg_matrix_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/mg_level_object.h>
21
22 #include <deal.II/lac/linear_operator.h>
23 #include <deal.II/lac/sparse_matrix.h>
24 #include <deal.II/lac/vector.h>
25
26 #include <deal.II/multigrid/mg_base.h>
27
28 #include <memory>
29
30 DEAL_II_NAMESPACE_OPEN
31
32 /**
33 * @addtogroup mg
34 * @{
35 */
36
37 namespace mg
38 {
39 /**
40 * Multilevel matrix. This matrix stores an MGLevelObject of
41 * LinearOperator objects. It implements the interface defined in
42 * MGMatrixBase, so that it can be used as a matrix in Multigrid.
43 */
44 template <typename VectorType = Vector<double>>
45 class Matrix : public MGMatrixBase<VectorType>
46 {
47 public:
48 /**
49 * Default constructor for an empty object.
50 */
51 Matrix() = default;
52
53 /**
54 * Constructor setting up pointers to the matrices in <tt>M</tt> by
55 * calling initialize().
56 */
57 template <typename MatrixType>
58 Matrix(const MGLevelObject<MatrixType> &M);
59
60 /**
61 * Initialize the object such that the level multiplication uses the
62 * matrices in <tt>M</tt>
63 */
64 template <typename MatrixType>
65 void
66 initialize(const MGLevelObject<MatrixType> &M);
67
68 /**
69 * Reset the object.
70 */
71 void
72 reset();
73
74 /**
75 * Access matrix on a level.
76 */
77 const LinearOperator<VectorType> &
78 operator[](unsigned int level) const;
79
80 virtual void
81 vmult(const unsigned int level,
82 VectorType &dst,
83 const VectorType &src) const override;
84 virtual void
85 vmult_add(const unsigned int level,
86 VectorType &dst,
87 const VectorType &src) const override;
88 virtual void
89 Tvmult(const unsigned int level,
90 VectorType &dst,
91 const VectorType &src) const override;
92 virtual void
93 Tvmult_add(const unsigned int level,
94 VectorType &dst,
95 const VectorType &src) const override;
96 virtual unsigned int
97 get_minlevel() const override;
98 virtual unsigned int
99 get_maxlevel() const override;
100
101 /**
102 * Memory used by this object.
103 */
104 std::size_t
105 memory_consumption() const;
106
107 private:
108 MGLevelObject<LinearOperator<VectorType>> matrices;
109 };
110
111 } // namespace mg
112
113
114 /**
115 * Multilevel matrix selecting from block matrices. This class implements the
116 * interface defined by MGMatrixBase. The template parameter @p MatrixType
117 * should be a block matrix class like BlockSparseMatrix or @p
118 * BlockSparseMatrixEZ. Then, this class stores a pointer to a MGLevelObject
119 * of this matrix class. In each @p vmult, the block selected on
120 * initialization will be multiplied with the vector provided.
121 */
122 template <typename MatrixType, typename number>
123 class MGMatrixSelect : public MGMatrixBase<Vector<number>>
124 {
125 public:
126 /**
127 * Constructor. @p row and @p col are the coordinate of the selected block.
128 * The other argument is handed over to the @p ObserverPointer constructor.
129 */
130 MGMatrixSelect(const unsigned int row = 0,
131 const unsigned int col = 0,
132 MGLevelObject<MatrixType> *matrix = 0);
133
134 /**
135 * Set the matrix object to be used. The matrix object must exist longer as
136 * the @p MGMatrixSelect object, since only a pointer is stored.
137 */
138 void
139 set_matrix(MGLevelObject<MatrixType> *M);
140
141 /**
142 * Select the block for multiplication.
143 */
144 void
145 select_block(const unsigned int row, const unsigned int col);
146
147 /**
148 * Matrix-vector-multiplication on a certain level.
149 */
150 virtual void
151 vmult(const unsigned int level,
152 Vector<number> &dst,
153 const Vector<number> &src) const;
154
155 /**
156 * Adding matrix-vector-multiplication on a certain level.
157 */
158 virtual void
159 vmult_add(const unsigned int level,
160 Vector<number> &dst,
161 const Vector<number> &src) const;
162
163 /**
164 * Transpose matrix-vector-multiplication on a certain level.
165 */
166 virtual void
167 Tvmult(const unsigned int level,
168 Vector<number> &dst,
169 const Vector<number> &src) const;
170
171 /**
172 * Adding transpose matrix-vector-multiplication on a certain level.
173 */
174 virtual void
175 Tvmult_add(const unsigned int level,
176 Vector<number> &dst,
177 const Vector<number> &src) const;
178
179 private:
180 /**
181 * Pointer to the matrix objects on each level.
182 */
183 ObserverPointer<MGLevelObject<MatrixType>, MGMatrixSelect<MatrixType, number>>
184 matrix;
185 /**
186 * Row coordinate of selected block.
187 */
188 unsigned int row;
189 /**
190 * Column coordinate of selected block.
191 */
192 unsigned int col;
193 };
194
195 /** @} */
196
197 /*----------------------------------------------------------------------*/
198
199 namespace mg
200 {
201 template <typename VectorType>
202 template <typename MatrixType>
203 inline void
204 Matrix<VectorType>::initialize(const MGLevelObject<MatrixType> &p)
205 {
206 matrices.resize(p.min_level(), p.max_level());
207 for (unsigned int level = p.min_level(); level <= p.max_level(); ++level)
208 {
209 // Workaround: Unfortunately, not every "p[level]" object has a
210 // rich enough interface to populate reinit_(domain|range)_vector.
211 // Thus, apply an empty LinearOperator exemplar.
212 matrices[level] =
213 linear_operator<VectorType>(LinearOperator<VectorType>(),
214 Utilities::get_underlying_value(
215 p[level]));
216 }
217 }
218
219
220
221 template <typename VectorType>
222 inline void
223 Matrix<VectorType>::reset()
224 {
225 matrices.resize(0, 0);
226 }
227
228
229
230 template <typename VectorType>
231 template <typename MatrixType>
232 inline Matrix<VectorType>::Matrix(const MGLevelObject<MatrixType> &p)
233 {
234 initialize(p);
235 }
236
237
238
239 template <typename VectorType>
240 inline const LinearOperator<VectorType> &
241 Matrix<VectorType>::operator[](unsigned int level) const
242 {
243 return matrices[level];
244 }
245
246
247
248 template <typename VectorType>
249 void
250 Matrix<VectorType>::vmult(const unsigned int level,
251 VectorType &dst,
252 const VectorType &src) const
253 {
254 matrices[level].vmult(dst, src);
255 }
256
257
258
259 template <typename VectorType>
260 void
261 Matrix<VectorType>::vmult_add(const unsigned int level,
262 VectorType &dst,
263 const VectorType &src) const
264 {
265 matrices[level].vmult_add(dst, src);
266 }
267
268
269
270 template <typename VectorType>
271 void
272 Matrix<VectorType>::Tvmult(const unsigned int level,
273 VectorType &dst,
274 const VectorType &src) const
275 {
276 matrices[level].Tvmult(dst, src);
277 }
278
279
280
281 template <typename VectorType>
282 void
283 Matrix<VectorType>::Tvmult_add(const unsigned int level,
284 VectorType &dst,
285 const VectorType &src) const
286 {
287 matrices[level].Tvmult_add(dst, src);
288 }
289
290
291
292 template <typename VectorType>
293 unsigned int
294 Matrix<VectorType>::get_minlevel() const
295 {
296 return matrices.min_level();
297 }
298
299
300
301 template <typename VectorType>
302 unsigned int
303 Matrix<VectorType>::get_maxlevel() const
304 {
305 return matrices.max_level();
306 }
307
308
309
310 template <typename VectorType>
311 inline std::size_t
312 Matrix<VectorType>::memory_consumption() const
313 {
314 return sizeof(*this) + matrices->memory_consumption();
315 }
316 } // namespace mg
317
318
319 /*----------------------------------------------------------------------*/
320
321 template <typename MatrixType, typename number>
322 MGMatrixSelect<MatrixType, number>::MGMatrixSelect(const unsigned int row,
323 const unsigned int col,
324 MGLevelObject<MatrixType> *p)
325 : matrix(p, typeid(*this).name())
326 , row(row)
327 , col(col)
328 {}
329
330
331
332 template <typename MatrixType, typename number>
333 void
334 MGMatrixSelect<MatrixType, number>::set_matrix(MGLevelObject<MatrixType> *p)
335 {
336 matrix = p;
337 }
338
339
340
341 template <typename MatrixType, typename number>
342 void
343 MGMatrixSelect<MatrixType, number>::select_block(const unsigned int brow,
344 const unsigned int bcol)
345 {
346 row = brow;
347 col = bcol;
348 }
349
350
351
352 template <typename MatrixType, typename number>
353 void
354 MGMatrixSelect<MatrixType, number>::vmult(const unsigned int level,
355 Vector<number> &dst,
356 const Vector<number> &src) const
357 {
358 Assert(matrix != 0, ExcNotInitialized());
359
360 const MGLevelObject<MatrixType> &m = *matrix;
361 m[level].block(row, col).vmult(dst, src);
362 }
363
364
365
366 template <typename MatrixType, typename number>
367 void
368 MGMatrixSelect<MatrixType, number>::vmult_add(const unsigned int level,
369 Vector<number> &dst,
370 const Vector<number> &src) const
371 {
372 Assert(matrix != 0, ExcNotInitialized());
373
374 const MGLevelObject<MatrixType> &m = *matrix;
375 m[level].block(row, col).vmult_add(dst, src);
376 }
377
378
379
380 template <typename MatrixType, typename number>
381 void
382 MGMatrixSelect<MatrixType, number>::Tvmult(const unsigned int level,
383 Vector<number> &dst,
384 const Vector<number> &src) const
385 {
386 Assert(matrix != 0, ExcNotInitialized());
387
388 const MGLevelObject<MatrixType> &m = *matrix;
389 m[level].block(row, col).Tvmult(dst, src);
390 }
391
392
393
394 template <typename MatrixType, typename number>
395 void
396 MGMatrixSelect<MatrixType, number>::Tvmult_add(const unsigned int level,
397 Vector<number> &dst,
398 const Vector<number> &src) const
399 {
400 Assert(matrix != 0, ExcNotInitialized());
401
402 const MGLevelObject<MatrixType> &m = *matrix;
403 m[level].block(row, col).Tvmult_add(dst, src);
404 }
405
406 DEAL_II_NAMESPACE_CLOSE
407
408 #endif