Reference documentation for deal.II version 8.4.2
matrix_lib.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2015 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__matrix_lib_h
17 #define dealii__matrix_lib_h
18 
19 #include <deal.II/base/subscriptor.h>
20 #include <deal.II/lac/vector_memory.h>
21 #include <deal.II/lac/pointer_matrix.h>
22 #include <deal.II/lac/solver_richardson.h>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
26 template<typename number> class Vector;
27 template<typename number> class BlockVector;
28 template<typename number> class SparseMatrix;
29 
54 template<typename VectorType>
55 class ProductMatrix : public PointerMatrixBase<VectorType>
56 {
57 public:
62  ProductMatrix();
63 
69 
74  template <typename MatrixType1, typename MatrixType2>
75  ProductMatrix (const MatrixType1 &m1,
76  const MatrixType2 &m2,
78 
83 
87  template <typename MatrixType1, typename MatrixType2>
88  void reinit (const MatrixType1 &m1, const MatrixType2 &m2);
89 
93  template <typename MatrixType1, typename MatrixType2>
94  void initialize (const MatrixType1 &m1,
95  const MatrixType2 &m2,
97 
98  // Doc in PointerMatrixBase
99  void clear();
100 
104  virtual void vmult (VectorType &w,
105  const VectorType &v) const;
106 
111  virtual void Tvmult (VectorType &w,
112  const VectorType &v) const;
113 
117  virtual void vmult_add (VectorType &w,
118  const VectorType &v) const;
119 
124  virtual void Tvmult_add (VectorType &w,
125  const VectorType &v) const;
126 
127 private:
132 
137 
142 };
143 
144 
158 template<typename VectorType>
159 class ScaledMatrix : public Subscriptor
160 {
161 public:
165  ScaledMatrix ();
169  template <typename MatrixType>
170  ScaledMatrix (const MatrixType &M, const double factor);
171 
175  ~ScaledMatrix ();
179  template <typename MatrixType>
180  void initialize (const MatrixType &M, const double factor);
181 
185  void clear ();
186 
190  void vmult (VectorType &w, const VectorType &v) const;
191 
195  void Tvmult (VectorType &w, const VectorType &v) const;
196 
197 private:
205  double factor;
206 };
207 
208 
209 
226 template<typename number, typename vector_number>
227 class ProductSparseMatrix : public PointerMatrixBase<Vector<vector_number> >
228 {
229 public:
234 
239 
244  ProductSparseMatrix (const MatrixType &m1,
245  const MatrixType &m2,
247 
253 
254  void initialize (const MatrixType &m1,
255  const MatrixType &m2,
257 
258  // Doc in PointerMatrixBase
259  void clear();
260 
264  virtual void vmult (VectorType &w,
265  const VectorType &v) const;
266 
271  virtual void Tvmult (VectorType &w,
272  const VectorType &v) const;
273 
277  virtual void vmult_add (VectorType &w,
278  const VectorType &v) const;
279 
284  virtual void Tvmult_add (VectorType &w,
285  const VectorType &v) const;
286 
287 private:
292 
297 
302 };
303 
304 
322 {
323 public:
328 
332  MeanValueFilter(const size_type component = numbers::invalid_size_type);
333 
337  template <typename number>
338  void filter (Vector<number> &v) const;
339 
343  template <typename number>
344  void filter (BlockVector<number> &v) const;
345 
349  template <typename number>
350  void vmult (Vector<number> &dst,
351  const Vector<number> &src) const;
352 
356  template <typename number>
357  void vmult_add (Vector<number> &dst,
358  const Vector<number> &src) const;
359 
364  template <typename number>
365  void vmult (BlockVector<number> &dst,
366  const BlockVector<number> &src) const;
367 
372  template <typename number>
373  void vmult_add (BlockVector<number> &dst,
374  const BlockVector<number> &src) const;
375 
376 
380  template <typename VectorType>
381  void Tvmult(VectorType &, const VectorType &) const;
382 
386  template <typename VectorType>
387  void Tvmult_add(VectorType &, const VectorType &) const;
388 
389 private:
393  const size_type component;
394 };
395 
396 
397 
433 template<typename VectorType>
435 {
436 public:
447 
452  template <typename MatrixType, typename PreconditionerType>
453  void initialize (const MatrixType &,
454  const PreconditionerType &);
455 
459  SolverControl &control() const;
463  void vmult (VectorType &, const VectorType &) const;
464 
468  void vmult_add (VectorType &, const VectorType &) const;
469 
473  void Tvmult (VectorType &, const VectorType &) const;
474 
478  void Tvmult_add (VectorType &, const VectorType &) const;
479 
480 private:
485 
490 
495 
500 };
501 
502 
503 
504 
506 //---------------------------------------------------------------------------
507 
508 
509 template<typename VectorType>
510 inline
512  :
513  m(0)
514 {}
515 
516 
517 
518 template<typename VectorType>
519 template<typename MatrixType>
520 inline
521 ScaledMatrix<VectorType>::ScaledMatrix(const MatrixType &mat, const double factor)
522  :
523  m(new_pointer_matrix_base(mat, VectorType())),
524  factor(factor)
525 {}
526 
527 
528 
529 template<typename VectorType>
530 template<typename MatrixType>
531 inline
532 void
533 ScaledMatrix<VectorType>::initialize(const MatrixType &mat, const double f)
534 {
535  if (m) delete m;
536  m = new_pointer_matrix_base(mat, VectorType());
537  factor = f;
538 }
539 
540 
541 
542 template<typename VectorType>
543 inline
544 void
546 {
547  if (m) delete m;
548  m = 0;
549 }
550 
551 
552 
553 template<typename VectorType>
554 inline
556 {
557  clear ();
558 }
559 
560 
561 template<typename VectorType>
562 inline
563 void
564 ScaledMatrix<VectorType>::vmult (VectorType &w, const VectorType &v) const
565 {
566  m->vmult(w, v);
567  w *= factor;
568 }
569 
570 
571 template<typename VectorType>
572 inline
573 void
574 ScaledMatrix<VectorType>::Tvmult (VectorType &w, const VectorType &v) const
575 {
576  m->Tvmult(w, v);
577  w *= factor;
578 }
579 
580 
581 //---------------------------------------------------------------------------
582 
583 template<typename VectorType>
585  : m1(0), m2(0), mem(0)
586 {}
587 
588 
589 template<typename VectorType>
591  : m1(0), m2(0), mem(&m)
592 {}
593 
594 
595 template<typename VectorType>
596 template<typename MatrixType1, typename MatrixType2>
598  const MatrixType2 &mat2,
600  : mem(&m)
601 {
602  m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
603  m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
604 }
605 
606 
607 template<typename VectorType>
608 template<typename MatrixType1, typename MatrixType2>
609 void
610 ProductMatrix<VectorType>::reinit (const MatrixType1 &mat1, const MatrixType2 &mat2)
611 {
612  if (m1) delete m1;
613  if (m2) delete m2;
614  m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
615  m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
616 }
617 
618 
619 template<typename VectorType>
620 template<typename MatrixType1, typename MatrixType2>
621 void
622 ProductMatrix<VectorType>::initialize (const MatrixType1 &mat1,
623  const MatrixType2 &mat2,
624  VectorMemory<VectorType> &memory)
625 {
626  mem = &memory;
627  if (m1) delete m1;
628  if (m2) delete m2;
629  m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
630  m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
631 }
632 
633 
634 template<typename VectorType>
636 {
637  if (m1) delete m1;
638  if (m2) delete m2;
639 }
640 
641 
642 template<typename VectorType>
643 void
645 {
646  if (m1) delete m1;
647  m1 = 0;
648  if (m2) delete m2;
649  m2 = 0;
650 }
651 
652 
653 template<typename VectorType>
654 void
655 ProductMatrix<VectorType>::vmult (VectorType &dst, const VectorType &src) const
656 {
657  Assert (mem != 0, ExcNotInitialized());
658  Assert (m1 != 0, ExcNotInitialized());
659  Assert (m2 != 0, ExcNotInitialized());
660 
661  VectorType *v = mem->alloc();
662  v->reinit(dst);
663  m2->vmult (*v, src);
664  m1->vmult (dst, *v);
665  mem->free(v);
666 }
667 
668 
669 template<typename VectorType>
670 void
671 ProductMatrix<VectorType>::vmult_add (VectorType &dst, const VectorType &src) const
672 {
673  Assert (mem != 0, ExcNotInitialized());
674  Assert (m1 != 0, ExcNotInitialized());
675  Assert (m2 != 0, ExcNotInitialized());
676 
677  VectorType *v = mem->alloc();
678  v->reinit(dst);
679  m2->vmult (*v, src);
680  m1->vmult_add (dst, *v);
681  mem->free(v);
682 }
683 
684 
685 template<typename VectorType>
686 void
687 ProductMatrix<VectorType>::Tvmult (VectorType &dst, const VectorType &src) const
688 {
689  Assert (mem != 0, ExcNotInitialized());
690  Assert (m1 != 0, ExcNotInitialized());
691  Assert (m2 != 0, ExcNotInitialized());
692 
693  VectorType *v = mem->alloc();
694  v->reinit(dst);
695  m1->Tvmult (*v, src);
696  m2->Tvmult (dst, *v);
697  mem->free(v);
698 }
699 
700 
701 template<typename VectorType>
702 void
703 ProductMatrix<VectorType>::Tvmult_add (VectorType &dst, const VectorType &src) const
704 {
705  Assert (mem != 0, ExcNotInitialized());
706  Assert (m1 != 0, ExcNotInitialized());
707  Assert (m2 != 0, ExcNotInitialized());
708 
709  VectorType *v = mem->alloc();
710  v->reinit(dst);
711  m1->Tvmult (*v, src);
712  m2->Tvmult_add (dst, *v);
713  mem->free(v);
714 }
715 
716 
717 
718 //---------------------------------------------------------------------------
719 
720 template <typename VectorType>
721 inline void
722 MeanValueFilter::Tvmult(VectorType &, const VectorType &) const
723 {
724  Assert(false, ExcNotImplemented());
725 }
726 
727 
728 template <typename VectorType>
729 inline void
730 MeanValueFilter::Tvmult_add(VectorType &, const VectorType &) const
731 {
732  Assert(false, ExcNotImplemented());
733 }
734 
735 //-----------------------------------------------------------------------//
736 
737 template <typename VectorType>
738 template <typename MatrixType, typename PreconditionerType>
739 inline void
741  const PreconditionerType &p)
742 {
743  if (matrix != 0)
744  delete matrix;
745  matrix = new PointerMatrix<MatrixType, VectorType>(&m);
746  if (precondition != 0)
747  delete precondition;
748  precondition = new PointerMatrix<PreconditionerType, VectorType>(&p);
749 }
750 
751 
752 DEAL_II_NAMESPACE_CLOSE
753 
754 #endif
SolverRichardson< VectorType > solver
Definition: matrix_lib.h:489
void Tvmult_add(VectorType &, const VectorType &) const
Definition: matrix_lib.h:730
const types::global_dof_index invalid_size_type
Definition: types.h:173
PointerMatrixBase< VectorType > * m
Definition: matrix_lib.h:201
virtual void Tvmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:687
void clear()
Definition: matrix_lib.h:644
void initialize(const MatrixType &, const PreconditionerType &)
Definition: matrix_lib.h:740
void initialize(const MatrixType1 &m1, const MatrixType2 &m2, VectorMemory< VectorType > &mem)
Definition: matrix_lib.h:622
void clear()
Definition: matrix_lib.h:545
void vmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:564
PointerMatrixBase< VectorType > * matrix
Definition: matrix_lib.h:494
PointerMatrixBase< VectorType > * m1
Definition: matrix_lib.h:131
SmartPointer< VectorMemory< VectorType >, ProductSparseMatrix< number, vector_number > > mem
Definition: matrix_lib.h:301
void initialize(const MatrixType &M, const double factor)
Definition: matrix_lib.h:533
virtual void Tvmult_add(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:703
virtual void vmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:655
unsigned int global_dof_index
Definition: types.h:88
Vector< vector_number > VectorType
Definition: matrix_lib.h:238
SmartPointer< const MatrixType, ProductSparseMatrix< number, vector_number > > m1
Definition: matrix_lib.h:291
#define Assert(cond, exc)
Definition: exceptions.h:294
PointerMatrixBase< VectorType > * m2
Definition: matrix_lib.h:136
SparseMatrix< number > MatrixType
Definition: matrix_lib.h:233
double factor
Definition: matrix_lib.h:205
VectorMemory< VectorType > & mem
Definition: matrix_lib.h:484
SmartPointer< const MatrixType, ProductSparseMatrix< number, vector_number > > m2
Definition: matrix_lib.h:296
types::global_dof_index size_type
Definition: matrix_lib.h:327
void reinit(const MatrixType1 &m1, const MatrixType2 &m2)
Definition: matrix_lib.h:610
PointerMatrixBase< VectorType > * precondition
Definition: matrix_lib.h:499
void Tvmult(VectorType &, const VectorType &) const
Definition: matrix_lib.h:722
virtual void vmult_add(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:671
void Tvmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:574
const size_type component
Definition: matrix_lib.h:393
SmartPointer< VectorMemory< VectorType >, ProductMatrix< VectorType > > mem
Definition: matrix_lib.h:141