Reference documentation for deal.II version 9.0.0
mg_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2017 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 at
12 // the top level of the deal.II distribution.
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 #include <deal.II/lac/linear_operator.h>
21 #include <deal.II/lac/sparse_matrix.h>
22 #include <deal.II/lac/vector.h>
23 #include <deal.II/multigrid/mg_base.h>
24 
25 #include <memory>
26 
27 DEAL_II_NAMESPACE_OPEN
28 
31 
32 namespace mg
33 {
42  template <typename VectorType = Vector<double> >
43  class Matrix
44  : public MGMatrixBase<VectorType>
45  {
46  public:
50  Matrix() = default;
51 
56  template <typename MatrixType>
58 
63  template <typename MatrixType>
64  void
66 
70  void reset();
71 
75  const LinearOperator<VectorType> &operator[] (unsigned int level) const;
76 
77  virtual void vmult (const unsigned int level, VectorType &dst, const VectorType &src) const;
78  virtual void vmult_add (const unsigned int level, VectorType &dst, const VectorType &src) const;
79  virtual void Tvmult (const unsigned int level, VectorType &dst, const VectorType &src) const;
80  virtual void Tvmult_add (const unsigned int level, VectorType &dst, const VectorType &src) const;
81  virtual unsigned int get_minlevel() const;
82  virtual unsigned int get_maxlevel() const;
83 
87  std::size_t memory_consumption () const;
88  private:
90  };
91 
92 }
93 
94 
105 template <typename MatrixType, typename number>
106 class MGMatrixSelect : public MGMatrixBase<Vector<number> >
107 {
108 public:
113  MGMatrixSelect (const unsigned int row = 0,
114  const unsigned int col = 0,
116 
122 
126  void select_block (const unsigned int row,
127  const unsigned int col);
128 
132  virtual void vmult (const unsigned int level,
133  Vector<number> &dst,
134  const Vector<number> &src) const;
135 
139  virtual void vmult_add (const unsigned int level,
140  Vector<number> &dst,
141  const Vector<number> &src) const;
142 
146  virtual void Tvmult (const unsigned int level,
147  Vector<number> &dst,
148  const Vector<number> &src) const;
149 
153  virtual void Tvmult_add (const unsigned int level,
154  Vector<number> &dst,
155  const Vector<number> &src) const;
156 
157 private:
165  unsigned int row;
169  unsigned int col;
170 
171 };
172 
175 /*----------------------------------------------------------------------*/
176 
177 namespace mg
178 {
179  template <typename VectorType>
180  template <typename MatrixType>
181  inline
182  void
184  {
185  matrices.resize(p.min_level(), p.max_level());
186  for (unsigned int level = p.min_level(); level <= p.max_level(); ++level)
187  {
188  // Workaround: Unfortunately, not every "p[level]" object has a
189  // rich enough interface to populate reinit_(domain|range)_vector.
190  // Thus, apply an empty LinearOperator exemplar.
191  matrices[level] =
192  linear_operator<VectorType>(LinearOperator<VectorType>(), p[level]);
193  }
194  }
195 
196 
197 
198  template <typename VectorType>
199  inline
200  void
202  {
203  matrices.resize(0,0);
204  }
205 
206 
207 
208  template <typename VectorType>
209  template <typename MatrixType>
210  inline
212  {
213  initialize(p);
214  }
215 
216 
217 
218  template <typename VectorType>
219  inline
221  Matrix<VectorType>::operator[] (unsigned int level) const
222  {
223  return matrices[level];
224  }
225 
226 
227 
228  template <typename VectorType>
229  void
230  Matrix<VectorType>::vmult (const unsigned int level,
231  VectorType &dst,
232  const VectorType &src) const
233  {
234  matrices[level].vmult(dst, src);
235  }
236 
237 
238 
239  template <typename VectorType>
240  void
241  Matrix<VectorType>::vmult_add (const unsigned int level,
242  VectorType &dst,
243  const VectorType &src) const
244  {
245  matrices[level].vmult_add(dst, src);
246  }
247 
248 
249 
250  template <typename VectorType>
251  void
252  Matrix<VectorType>::Tvmult (const unsigned int level,
253  VectorType &dst,
254  const VectorType &src) const
255  {
256  matrices[level].Tvmult(dst, src);
257  }
258 
259 
260 
261  template <typename VectorType>
262  void
263  Matrix<VectorType>::Tvmult_add (const unsigned int level,
264  VectorType &dst,
265  const VectorType &src) const
266  {
267  matrices[level].Tvmult_add(dst, src);
268  }
269 
270 
271 
272  template <typename VectorType>
273  unsigned int
275  {
276  return matrices.min_level();
277  }
278 
279 
280 
281  template <typename VectorType>
282  unsigned int
284  {
285  return matrices.max_level();
286  }
287 
288 
289 
290  template <typename VectorType>
291  inline
292  std::size_t
294  {
295  return sizeof(*this) + matrices->memory_consumption();
296  }
297 }
298 
299 
300 /*----------------------------------------------------------------------*/
301 
302 template <typename MatrixType, typename number>
304 MGMatrixSelect (const unsigned int row,
305  const unsigned int col,
307  :
308  matrix (p, typeid(*this).name()),
309  row(row),
310  col(col)
311 {}
312 
313 
314 
315 template <typename MatrixType, typename number>
316 void
318 {
319  matrix = p;
320 }
321 
322 
323 
324 template <typename MatrixType, typename number>
325 void
327 select_block (const unsigned int brow,
328  const unsigned int bcol)
329 {
330  row = brow;
331  col = bcol;
332 }
333 
334 
335 
336 template <typename MatrixType, typename number>
337 void
339 vmult (const unsigned int level,
340  Vector<number> &dst,
341  const Vector<number> &src) const
342 {
343  Assert(matrix != 0, ExcNotInitialized());
344 
345  const MGLevelObject<MatrixType> &m = *matrix;
346  m[level].block(row, col).vmult(dst, src);
347 }
348 
349 
350 
351 template <typename MatrixType, typename number>
352 void
354 vmult_add (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_add(dst, src);
362 }
363 
364 
365 
366 template <typename MatrixType, typename number>
367 void
369 Tvmult (const unsigned int level,
370  Vector<number> &dst,
371  const Vector<number> &src) const
372 {
373  Assert(matrix != 0, ExcNotInitialized());
374 
375  const MGLevelObject<MatrixType> &m = *matrix;
376  m[level].block(row, col).Tvmult(dst, src);
377 }
378 
379 
380 
381 template <typename MatrixType, typename number>
382 void
384 Tvmult_add (const unsigned int level,
385  Vector<number> &dst,
386  const Vector<number> &src) const
387 {
388  Assert(matrix != 0, ExcNotInitialized());
389 
390  const MGLevelObject<MatrixType> &m = *matrix;
391  m[level].block(row, col).Tvmult_add(dst, src);
392 }
393 
394 DEAL_II_NAMESPACE_CLOSE
395 
396 #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:304
unsigned int col
Definition: mg_matrix.h:169
virtual unsigned int get_maxlevel() const
Definition: mg_matrix.h:283
Matrix()=default
virtual void vmult_add(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:354
Definition: mg.h:81
virtual void Tvmult(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:369
void select_block(const unsigned int row, const unsigned int col)
Definition: mg_matrix.h:327
void set_matrix(MGLevelObject< MatrixType > *M)
Definition: mg_matrix.h:317
virtual void vmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:241
static ::ExceptionBase & ExcNotInitialized()
void reset()
Definition: mg_matrix.h:201
unsigned int min_level() const
const LinearOperator< VectorType > & operator[](unsigned int level) const
Definition: mg_matrix.h:221
SmartPointer< MGLevelObject< MatrixType >, MGMatrixSelect< MatrixType, number > > matrix
Definition: mg_matrix.h:161
#define Assert(cond, exc)
Definition: exceptions.h:1142
unsigned int row
Definition: mg_matrix.h:165
virtual void Tvmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:263
void initialize(const MGLevelObject< MatrixType > &M)
Definition: mg_matrix.h:183
virtual void vmult(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:230
virtual void Tvmult(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:252
virtual unsigned int get_minlevel() const
Definition: mg_matrix.h:274
virtual void vmult(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:339
virtual void Tvmult_add(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:384
std::size_t memory_consumption() const
Definition: mg_matrix.h:293