Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
mg_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2019 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_mg_matrix_h
17 #define dealii_mg_matrix_h
18 
19 #include <deal.II/base/mg_level_object.h>
20 
21 #include <deal.II/lac/linear_operator.h>
22 #include <deal.II/lac/sparse_matrix.h>
23 #include <deal.II/lac/vector.h>
24 
25 #include <deal.II/multigrid/mg_base.h>
26 
27 #include <memory>
28 
29 DEAL_II_NAMESPACE_OPEN
30 
33 
34 namespace mg
35 {
44  template <typename VectorType = Vector<double>>
45  class Matrix : public MGMatrixBase<VectorType>
46  {
47  public:
51  Matrix() = default;
52 
57  template <typename MatrixType>
59 
64  template <typename MatrixType>
65  void
67 
71  void
72  reset();
73 
77  const LinearOperator<VectorType> &operator[](unsigned int level) const;
78 
79  virtual void
80  vmult(const unsigned int level,
81  VectorType & dst,
82  const VectorType & src) const override;
83  virtual void
84  vmult_add(const unsigned int level,
85  VectorType & dst,
86  const VectorType & src) const override;
87  virtual void
88  Tvmult(const unsigned int level,
89  VectorType & dst,
90  const VectorType & src) const override;
91  virtual void
92  Tvmult_add(const unsigned int level,
93  VectorType & dst,
94  const VectorType & src) const override;
95  virtual unsigned int
96  get_minlevel() const override;
97  virtual unsigned int
98  get_maxlevel() const override;
99 
103  std::size_t
104  memory_consumption() const;
105 
106  private:
108  };
109 
110 } // namespace mg
111 
112 
123 template <typename MatrixType, typename number>
124 class MGMatrixSelect : public MGMatrixBase<Vector<number>>
125 {
126 public:
131  MGMatrixSelect(const unsigned int row = 0,
132  const unsigned int col = 0,
134 
139  void
141 
145  void
146  select_block(const unsigned int row, const unsigned int col);
147 
151  virtual void
152  vmult(const unsigned int level,
153  Vector<number> & dst,
154  const Vector<number> &src) const;
155 
159  virtual void
160  vmult_add(const unsigned int level,
161  Vector<number> & dst,
162  const Vector<number> &src) const;
163 
167  virtual void
168  Tvmult(const unsigned int level,
169  Vector<number> & dst,
170  const Vector<number> &src) const;
171 
175  virtual void
176  Tvmult_add(const unsigned int level,
177  Vector<number> & dst,
178  const Vector<number> &src) const;
179 
180 private:
189  unsigned int row;
193  unsigned int col;
194 };
195 
198 /*----------------------------------------------------------------------*/
199 
200 namespace mg
201 {
202  template <typename VectorType>
203  template <typename MatrixType>
204  inline void
206  {
207  matrices.resize(p.min_level(), p.max_level());
208  for (unsigned int level = p.min_level(); level <= p.max_level(); ++level)
209  {
210  // Workaround: Unfortunately, not every "p[level]" object has a
211  // rich enough interface to populate reinit_(domain|range)_vector.
212  // Thus, apply an empty LinearOperator exemplar.
213  matrices[level] =
214  linear_operator<VectorType>(LinearOperator<VectorType>(), p[level]);
215  }
216  }
217 
218 
219 
220  template <typename VectorType>
221  inline void
223  {
224  matrices.resize(0, 0);
225  }
226 
227 
228 
229  template <typename VectorType>
230  template <typename MatrixType>
232  {
233  initialize(p);
234  }
235 
236 
237 
238  template <typename VectorType>
240  operator[](unsigned int level) const
241  {
242  return matrices[level];
243  }
244 
245 
246 
247  template <typename VectorType>
248  void
249  Matrix<VectorType>::vmult(const unsigned int level,
250  VectorType & dst,
251  const VectorType & src) const
252  {
253  matrices[level].vmult(dst, src);
254  }
255 
256 
257 
258  template <typename VectorType>
259  void
260  Matrix<VectorType>::vmult_add(const unsigned int level,
261  VectorType & dst,
262  const VectorType & src) const
263  {
264  matrices[level].vmult_add(dst, src);
265  }
266 
267 
268 
269  template <typename VectorType>
270  void
271  Matrix<VectorType>::Tvmult(const unsigned int level,
272  VectorType & dst,
273  const VectorType & src) const
274  {
275  matrices[level].Tvmult(dst, src);
276  }
277 
278 
279 
280  template <typename VectorType>
281  void
282  Matrix<VectorType>::Tvmult_add(const unsigned int level,
283  VectorType & dst,
284  const VectorType & src) const
285  {
286  matrices[level].Tvmult_add(dst, src);
287  }
288 
289 
290 
291  template <typename VectorType>
292  unsigned int
294  {
295  return matrices.min_level();
296  }
297 
298 
299 
300  template <typename VectorType>
301  unsigned int
303  {
304  return matrices.max_level();
305  }
306 
307 
308 
309  template <typename VectorType>
310  inline std::size_t
312  {
313  return sizeof(*this) + matrices->memory_consumption();
314  }
315 } // namespace mg
316 
317 
318 /*----------------------------------------------------------------------*/
319 
320 template <typename MatrixType, typename number>
322  const unsigned int col,
324  : matrix(p, typeid(*this).name())
325  , row(row)
326  , col(col)
327 {}
328 
329 
330 
331 template <typename MatrixType, typename number>
332 void
334 {
335  matrix = p;
336 }
337 
338 
339 
340 template <typename MatrixType, typename number>
341 void
343  const unsigned int bcol)
344 {
345  row = brow;
346  col = bcol;
347 }
348 
349 
350 
351 template <typename MatrixType, typename number>
352 void
354  Vector<number> & dst,
355  const Vector<number> &src) const
356 {
357  Assert(matrix != 0, ExcNotInitialized());
358 
359  const MGLevelObject<MatrixType> &m = *matrix;
360  m[level].block(row, col).vmult(dst, src);
361 }
362 
363 
364 
365 template <typename MatrixType, typename number>
366 void
368  Vector<number> & dst,
369  const Vector<number> &src) const
370 {
371  Assert(matrix != 0, ExcNotInitialized());
372 
373  const MGLevelObject<MatrixType> &m = *matrix;
374  m[level].block(row, col).vmult_add(dst, src);
375 }
376 
377 
378 
379 template <typename MatrixType, typename number>
380 void
382  Vector<number> & dst,
383  const Vector<number> &src) const
384 {
385  Assert(matrix != 0, ExcNotInitialized());
386 
387  const MGLevelObject<MatrixType> &m = *matrix;
388  m[level].block(row, col).Tvmult(dst, src);
389 }
390 
391 
392 
393 template <typename MatrixType, typename number>
394 void
396  Vector<number> & dst,
397  const Vector<number> &src) const
398 {
399  Assert(matrix != 0, ExcNotInitialized());
400 
401  const MGLevelObject<MatrixType> &m = *matrix;
402  m[level].block(row, col).Tvmult_add(dst, src);
403 }
404 
405 DEAL_II_NAMESPACE_CLOSE
406 
407 #endif
unsigned int max_level() const
MGMatrixSelect(const unsigned int row=0, const unsigned int col=0, MGLevelObject< MatrixType > *matrix=0)
Definition: mg_matrix.h:321
unsigned int col
Definition: mg_matrix.h:193
Matrix()=default
virtual void vmult_add(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:367
Definition: mg.h:81
virtual void Tvmult(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:381
virtual void Tvmult(const unsigned int level, VectorType &dst, const VectorType &src) const override
Definition: mg_matrix.h:271
void select_block(const unsigned int row, const unsigned int col)
Definition: mg_matrix.h:342
void set_matrix(MGLevelObject< MatrixType > *M)
Definition: mg_matrix.h:333
static ::ExceptionBase & ExcNotInitialized()
void reset()
Definition: mg_matrix.h:222
unsigned int min_level() const
const LinearOperator< VectorType > & operator[](unsigned int level) const
Definition: mg_matrix.h:240
virtual void vmult(const unsigned int level, VectorType &dst, const VectorType &src) const override
Definition: mg_matrix.h:249
#define Assert(cond, exc)
Definition: exceptions.h:1407
unsigned int row
Definition: mg_matrix.h:189
void initialize(const MGLevelObject< MatrixType > &M)
Definition: mg_matrix.h:205
virtual unsigned int get_maxlevel() const override
Definition: mg_matrix.h:302
SmartPointer< MGLevelObject< MatrixType >, MGMatrixSelect< MatrixType, number > > matrix
Definition: mg_matrix.h:185
virtual unsigned int get_minlevel() const override
Definition: mg_matrix.h:293
virtual void vmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const override
Definition: mg_matrix.h:260
virtual void Tvmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const override
Definition: mg_matrix.h:282
virtual void vmult(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:353
virtual void Tvmult_add(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:395
std::size_t memory_consumption() const
Definition: mg_matrix.h:311