Reference documentation for deal.II version 8.4.2
mg_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2016 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/lac/vector.h>
20 #include <deal.II/lac/pointer_matrix.h>
21 #include <deal.II/lac/sparse_matrix.h>
22 #include <deal.II/multigrid/mg_base.h>
23 #include <deal.II/base/mg_level_object.h>
24 #include <deal.II/base/std_cxx11/shared_ptr.h>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
30 
31 namespace mg
32 {
41  template <typename VectorType = Vector<double> >
42  class Matrix
43  : public MGMatrixBase<VectorType>
44  {
45  public:
49  Matrix();
50 
55  template <typename MatrixType>
57 
62  template <typename MatrixType>
63  void
65 
69  const PointerMatrixBase<VectorType> &operator[] (unsigned int level) const;
70 
71  virtual void vmult (const unsigned int level, VectorType &dst, const VectorType &src) const;
72  virtual void vmult_add (const unsigned int level, VectorType &dst, const VectorType &src) const;
73  virtual void Tvmult (const unsigned int level, VectorType &dst, const VectorType &src) const;
74  virtual void Tvmult_add (const unsigned int level, VectorType &dst, const VectorType &src) const;
75 
79  std::size_t memory_consumption () const;
80  private:
82  };
83 
84 }
85 
86 
97 template <typename MatrixType, typename number>
98 class MGMatrixSelect : public MGMatrixBase<Vector<number> >
99 {
100 public:
105  MGMatrixSelect (const unsigned int row = 0,
106  const unsigned int col = 0,
107  MGLevelObject<MatrixType> *matrix = 0);
108 
113  void set_matrix (MGLevelObject<MatrixType> *M);
114 
118  void select_block (const unsigned int row,
119  const unsigned int col);
120 
124  virtual void vmult (const unsigned int level,
125  Vector<number> &dst,
126  const Vector<number> &src) const;
127 
131  virtual void vmult_add (const unsigned int level,
132  Vector<number> &dst,
133  const Vector<number> &src) const;
134 
138  virtual void Tvmult (const unsigned int level,
139  Vector<number> &dst,
140  const Vector<number> &src) const;
141 
145  virtual void Tvmult_add (const unsigned int level,
146  Vector<number> &dst,
147  const Vector<number> &src) const;
148 
149 private:
157  unsigned int row;
161  unsigned int col;
162 
163 };
164 
167 /*----------------------------------------------------------------------*/
168 
169 namespace mg
170 {
171  template <typename VectorType>
172  template <typename MatrixType>
173  inline
174  void
176  {
177  matrices.resize(p.min_level(), p.max_level());
178  for (unsigned int level=p.min_level(); level <= p.max_level(); ++level)
179  matrices[level] = std_cxx11::shared_ptr<PointerMatrixBase<VectorType> >
180  (new_pointer_matrix_base(p[level], VectorType()));
181  }
182 
183 
184  template <typename VectorType>
185  template <typename MatrixType>
186  inline
188  {
189  initialize(p);
190  }
191 
192 
193  template <typename VectorType>
194  inline
196  {}
197 
198 
199  template <typename VectorType>
200  inline
202  Matrix<VectorType>::operator[] (unsigned int level) const
203  {
204  return *matrices[level];
205  }
206 
207 
208  template <typename VectorType>
209  void
210  Matrix<VectorType>::vmult (const unsigned int level,
211  VectorType &dst,
212  const VectorType &src) const
213  {
214  matrices[level]->vmult(dst, src);
215  }
216 
217 
218  template <typename VectorType>
219  void
220  Matrix<VectorType>::vmult_add (const unsigned int level,
221  VectorType &dst,
222  const VectorType &src) const
223  {
224  matrices[level]->vmult_add(dst, src);
225  }
226 
227 
228  template <typename VectorType>
229  void
230  Matrix<VectorType>::Tvmult (const unsigned int level,
231  VectorType &dst,
232  const VectorType &src) const
233  {
234  matrices[level]->Tvmult(dst, src);
235  }
236 
237 
238  template <typename VectorType>
239  void
240  Matrix<VectorType>::Tvmult_add (const unsigned int level,
241  VectorType &dst,
242  const VectorType &src) const
243  {
244  matrices[level]->Tvmult_add(dst, src);
245  }
246 
247 
248  template <typename VectorType>
249  inline
250  std::size_t
252  {
253  return sizeof(*this) + matrices->memory_consumption();
254  }
255 }
256 
257 
258 /*----------------------------------------------------------------------*/
259 
260 template <typename MatrixType, typename number>
262 MGMatrixSelect (const unsigned int row,
263  const unsigned int col,
265  :
266  matrix (p, typeid(*this).name()),
267  row(row),
268  col(col)
269 {}
270 
271 
272 
273 template <typename MatrixType, typename number>
274 void
276 {
277  matrix = p;
278 }
279 
280 
281 template <typename MatrixType, typename number>
282 void
284 select_block (const unsigned int brow,
285  const unsigned int bcol)
286 {
287  row = brow;
288  col = bcol;
289 }
290 
291 
292 template <typename MatrixType, typename number>
293 void
295 vmult (const unsigned int level,
296  Vector<number> &dst,
297  const Vector<number> &src) const
298 {
299  Assert(matrix != 0, ExcNotInitialized());
300 
301  const MGLevelObject<MatrixType> &m = *matrix;
302  m[level].block(row, col).vmult(dst, src);
303 }
304 
305 
306 template <typename MatrixType, typename number>
307 void
309 vmult_add (const unsigned int level,
310  Vector<number> &dst,
311  const Vector<number> &src) const
312 {
313  Assert(matrix != 0, ExcNotInitialized());
314 
315  const MGLevelObject<MatrixType> &m = *matrix;
316  m[level].block(row, col).vmult_add(dst, src);
317 }
318 
319 
320 template <typename MatrixType, typename number>
321 void
323 Tvmult (const unsigned int level,
324  Vector<number> &dst,
325  const Vector<number> &src) const
326 {
327  Assert(matrix != 0, ExcNotInitialized());
328 
329  const MGLevelObject<MatrixType> &m = *matrix;
330  m[level].block(row, col).Tvmult(dst, src);
331 }
332 
333 
334 template <typename MatrixType, typename number>
335 void
337 Tvmult_add (const unsigned int level,
338  Vector<number> &dst,
339  const Vector<number> &src) const
340 {
341  Assert(matrix != 0, ExcNotInitialized());
342 
343  const MGLevelObject<MatrixType> &m = *matrix;
344  m[level].block(row, col).Tvmult_add(dst, src);
345 }
346 
347 DEAL_II_NAMESPACE_CLOSE
348 
349 #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:262
unsigned int col
Definition: mg_matrix.h:161
virtual void vmult_add(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:309
Definition: mg.h:81
virtual void Tvmult(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:323
void select_block(const unsigned int row, const unsigned int col)
Definition: mg_matrix.h:284
void set_matrix(MGLevelObject< MatrixType > *M)
Definition: mg_matrix.h:275
virtual void vmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:220
unsigned int min_level() const
SmartPointer< MGLevelObject< MatrixType >, MGMatrixSelect< MatrixType, number > > matrix
Definition: mg_matrix.h:153
#define Assert(cond, exc)
Definition: exceptions.h:294
unsigned int row
Definition: mg_matrix.h:157
const PointerMatrixBase< VectorType > & operator[](unsigned int level) const
Definition: mg_matrix.h:202
virtual void Tvmult_add(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:240
void initialize(const MGLevelObject< MatrixType > &M)
Definition: mg_matrix.h:175
virtual void vmult(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:210
virtual void Tvmult(const unsigned int level, VectorType &dst, const VectorType &src) const
Definition: mg_matrix.h:230
virtual void vmult(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:295
virtual void Tvmult_add(const unsigned int level, Vector< number > &dst, const Vector< number > &src) const
Definition: mg_matrix.h:337
std::size_t memory_consumption() const
Definition: mg_matrix.h:251