Reference documentation for deal.II version 8.5.1
matrix_lib.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 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__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,
171  const double factor);
172 
176  ~ScaledMatrix ();
177 
181  template <typename MatrixType>
182  void initialize (const MatrixType &M,
183  const double factor);
184 
188  void clear ();
189 
193  void vmult (VectorType &w,
194  const VectorType &v) const;
195 
199  void Tvmult (VectorType &w,
200  const VectorType &v) const;
201 
202 private:
207 
211  double factor;
212 };
213 
214 
215 
232 template<typename number, typename vector_number>
233 class ProductSparseMatrix : public PointerMatrixBase<Vector<vector_number> >
234 {
235 public:
240 
245 
251  const MatrixType &m2,
253 
259 
260  void initialize (const MatrixType &m1,
261  const MatrixType &m2,
263 
264  // Doc in PointerMatrixBase
265  void clear();
266 
270  virtual void vmult (VectorType &w,
271  const VectorType &v) const;
272 
277  virtual void Tvmult (VectorType &w,
278  const VectorType &v) const;
279 
283  virtual void vmult_add (VectorType &w,
284  const VectorType &v) const;
285 
290  virtual void Tvmult_add (VectorType &w,
291  const VectorType &v) const;
292 
293 private:
298 
303 
308 };
309 
310 
328 {
329 public:
334 
339 
343  template <typename number>
344  void filter (Vector<number> &v) const;
345 
349  template <typename number>
350  void filter (BlockVector<number> &v) const;
351 
355  template <typename number>
356  void vmult (Vector<number> &dst,
357  const Vector<number> &src) const;
358 
362  template <typename number>
363  void vmult_add (Vector<number> &dst,
364  const Vector<number> &src) const;
365 
370  template <typename number>
371  void vmult (BlockVector<number> &dst,
372  const BlockVector<number> &src) const;
373 
378  template <typename number>
379  void vmult_add (BlockVector<number> &dst,
380  const BlockVector<number> &src) const;
381 
382 
386  template <typename VectorType>
387  void Tvmult(VectorType &, const VectorType &) const;
388 
392  template <typename VectorType>
393  void Tvmult_add(VectorType &, const VectorType &) const;
394 
395 private:
400 };
401 
402 
403 
439 template<typename VectorType>
441 {
442 public:
453 
458  template <typename MatrixType, typename PreconditionerType>
459  void initialize (const MatrixType &,
460  const PreconditionerType &);
461 
465  SolverControl &control() const;
469  void vmult (VectorType &, const VectorType &) const;
470 
474  void vmult_add (VectorType &, const VectorType &) const;
475 
479  void Tvmult (VectorType &, const VectorType &) const;
480 
484  void Tvmult_add (VectorType &, const VectorType &) const;
485 
486 private:
491 
496 
501 
506 };
507 
508 
509 
510 
512 //---------------------------------------------------------------------------
513 
514 
515 template<typename VectorType>
516 inline
518  :
519  m(0),
520  factor (0)
521 {}
522 
523 
524 
525 template<typename VectorType>
526 template<typename MatrixType>
527 inline
528 ScaledMatrix<VectorType>::ScaledMatrix(const MatrixType &mat, const double factor)
529  :
530  m(new_pointer_matrix_base(mat, VectorType())),
531  factor(factor)
532 {}
533 
534 
535 
536 template<typename VectorType>
537 template<typename MatrixType>
538 inline
539 void
540 ScaledMatrix<VectorType>::initialize(const MatrixType &mat, const double f)
541 {
542  if (m) delete m;
543  m = new_pointer_matrix_base(mat, VectorType());
544  factor = f;
545 }
546 
547 
548 
549 template<typename VectorType>
550 inline
551 void
553 {
554  if (m) delete m;
555  m = 0;
556 }
557 
558 
559 
560 template<typename VectorType>
561 inline
563 {
564  clear ();
565 }
566 
567 
568 template<typename VectorType>
569 inline
570 void
571 ScaledMatrix<VectorType>::vmult (VectorType &w, const VectorType &v) const
572 {
573  m->vmult(w, v);
574  w *= factor;
575 }
576 
577 
578 template<typename VectorType>
579 inline
580 void
581 ScaledMatrix<VectorType>::Tvmult (VectorType &w, const VectorType &v) const
582 {
583  m->Tvmult(w, v);
584  w *= factor;
585 }
586 
587 
588 //---------------------------------------------------------------------------
589 
590 template<typename VectorType>
592  : m1(0), m2(0), mem(0)
593 {}
594 
595 
596 template<typename VectorType>
598  : m1(0), m2(0), mem(&m)
599 {}
600 
601 
602 template<typename VectorType>
603 template<typename MatrixType1, typename MatrixType2>
605  const MatrixType2 &mat2,
607  : mem(&m)
608 {
609  m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
610  m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
611 }
612 
613 
614 template<typename VectorType>
615 template<typename MatrixType1, typename MatrixType2>
616 void
617 ProductMatrix<VectorType>::reinit (const MatrixType1 &mat1, const MatrixType2 &mat2)
618 {
619  if (m1) delete m1;
620  if (m2) delete m2;
621  m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
622  m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
623 }
624 
625 
626 template<typename VectorType>
627 template<typename MatrixType1, typename MatrixType2>
628 void
629 ProductMatrix<VectorType>::initialize (const MatrixType1 &mat1,
630  const MatrixType2 &mat2,
631  VectorMemory<VectorType> &memory)
632 {
633  mem = &memory;
634  if (m1) delete m1;
635  if (m2) delete m2;
636  m1 = new PointerMatrix<MatrixType1, VectorType>(&mat1, typeid(*this).name());
637  m2 = new PointerMatrix<MatrixType2, VectorType>(&mat2, typeid(*this).name());
638 }
639 
640 
641 template<typename VectorType>
643 {
644  if (m1) delete m1;
645  if (m2) delete m2;
646 }
647 
648 
649 template<typename VectorType>
650 void
652 {
653  if (m1) delete m1;
654  m1 = 0;
655  if (m2) delete m2;
656  m2 = 0;
657 }
658 
659 
660 template<typename VectorType>
661 void
662 ProductMatrix<VectorType>::vmult (VectorType &dst, const VectorType &src) const
663 {
664  Assert (mem != 0, ExcNotInitialized());
665  Assert (m1 != 0, ExcNotInitialized());
666  Assert (m2 != 0, ExcNotInitialized());
667 
668  VectorType *v = mem->alloc();
669  v->reinit(dst);
670  m2->vmult (*v, src);
671  m1->vmult (dst, *v);
672  mem->free(v);
673 }
674 
675 
676 template<typename VectorType>
677 void
678 ProductMatrix<VectorType>::vmult_add (VectorType &dst, const VectorType &src) const
679 {
680  Assert (mem != 0, ExcNotInitialized());
681  Assert (m1 != 0, ExcNotInitialized());
682  Assert (m2 != 0, ExcNotInitialized());
683 
684  VectorType *v = mem->alloc();
685  v->reinit(dst);
686  m2->vmult (*v, src);
687  m1->vmult_add (dst, *v);
688  mem->free(v);
689 }
690 
691 
692 template<typename VectorType>
693 void
694 ProductMatrix<VectorType>::Tvmult (VectorType &dst, const VectorType &src) const
695 {
696  Assert (mem != 0, ExcNotInitialized());
697  Assert (m1 != 0, ExcNotInitialized());
698  Assert (m2 != 0, ExcNotInitialized());
699 
700  VectorType *v = mem->alloc();
701  v->reinit(dst);
702  m1->Tvmult (*v, src);
703  m2->Tvmult (dst, *v);
704  mem->free(v);
705 }
706 
707 
708 template<typename VectorType>
709 void
710 ProductMatrix<VectorType>::Tvmult_add (VectorType &dst, const VectorType &src) const
711 {
712  Assert (mem != 0, ExcNotInitialized());
713  Assert (m1 != 0, ExcNotInitialized());
714  Assert (m2 != 0, ExcNotInitialized());
715 
716  VectorType *v = mem->alloc();
717  v->reinit(dst);
718  m1->Tvmult (*v, src);
719  m2->Tvmult_add (dst, *v);
720  mem->free(v);
721 }
722 
723 
724 
725 //---------------------------------------------------------------------------
726 
727 template <typename VectorType>
728 inline void
729 MeanValueFilter::Tvmult(VectorType &, const VectorType &) const
730 {
731  Assert(false, ExcNotImplemented());
732 }
733 
734 
735 template <typename VectorType>
736 inline void
737 MeanValueFilter::Tvmult_add(VectorType &, const VectorType &) const
738 {
739  Assert(false, ExcNotImplemented());
740 }
741 
742 //-----------------------------------------------------------------------//
743 
744 template <typename VectorType>
745 template <typename MatrixType, typename PreconditionerType>
746 inline void
748  const PreconditionerType &p)
749 {
750  if (matrix != 0)
751  delete matrix;
752  matrix = new PointerMatrix<MatrixType, VectorType>(&m);
753  if (precondition != 0)
754  delete precondition;
755  precondition = new PointerMatrix<PreconditionerType, VectorType>(&p);
756 }
757 
758 
759 DEAL_II_NAMESPACE_CLOSE
760 
761 #endif
SolverRichardson< VectorType > solver
Definition: matrix_lib.h:495
void Tvmult_add(VectorType &, const VectorType &) const
Definition: matrix_lib.h:737
const types::global_dof_index invalid_size_type
Definition: types.h:179
MeanValueFilter(const size_type component=numbers::invalid_size_type)
Definition: matrix_lib.cc:21
PointerMatrixBase< VectorType > * m
Definition: matrix_lib.h:206
void vmult(VectorType &, const VectorType &) const
void Tvmult_add(VectorType &, const VectorType &) const
virtual void Tvmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:694
InverseMatrixRichardson(SolverControl &control, VectorMemory< VectorType > &mem)
virtual void vmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.cc:75
void clear()
Definition: matrix_lib.h:651
void initialize(const MatrixType &, const PreconditionerType &)
Definition: matrix_lib.h:747
void initialize(const MatrixType1 &m1, const MatrixType2 &m2, VectorMemory< VectorType > &mem)
Definition: matrix_lib.h:629
void clear()
Definition: matrix_lib.h:552
void vmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:571
void vmult(Vector< number > &dst, const Vector< number > &src) const
static ::ExceptionBase & ExcNotInitialized()
PointerMatrixBase< VectorType > * matrix
Definition: matrix_lib.h:500
PointerMatrixBase< VectorType > * m1
Definition: matrix_lib.h:131
virtual void Tvmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.cc:107
SmartPointer< VectorMemory< VectorType >, ProductSparseMatrix< number, vector_number > > mem
Definition: matrix_lib.h:307
void vmult_add(Vector< number > &dst, const Vector< number > &src) const
virtual void vmult_add(VectorType &w, const VectorType &v) const
Definition: matrix_lib.cc:91
void initialize(const MatrixType &M, const double factor)
Definition: matrix_lib.h:540
PointerMatrixBase< VectorType > * new_pointer_matrix_base(MatrixType &matrix, const VectorType &, const char *name="PointerMatrixAux")
virtual void Tvmult_add(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:710
virtual void vmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:662
void Tvmult(VectorType &, const VectorType &) const
void vmult_add(VectorType &, const VectorType &) const
unsigned int global_dof_index
Definition: types.h:88
Vector< vector_number > VectorType
Definition: matrix_lib.h:244
SmartPointer< const MatrixType, ProductSparseMatrix< number, vector_number > > m1
Definition: matrix_lib.h:297
#define Assert(cond, exc)
Definition: exceptions.h:313
void filter(Vector< number > &v) const
PointerMatrixBase< VectorType > * m2
Definition: matrix_lib.h:136
SparseMatrix< number > MatrixType
Definition: matrix_lib.h:239
double factor
Definition: matrix_lib.h:211
SolverControl & control() const
VectorMemory< VectorType > & mem
Definition: matrix_lib.h:490
SmartPointer< const MatrixType, ProductSparseMatrix< number, vector_number > > m2
Definition: matrix_lib.h:302
types::global_dof_index size_type
Definition: matrix_lib.h:333
void reinit(const MatrixType1 &m1, const MatrixType2 &m2)
Definition: matrix_lib.h:617
virtual void Tvmult_add(VectorType &w, const VectorType &v) const
Definition: matrix_lib.cc:123
static ::ExceptionBase & ExcNotImplemented()
PointerMatrixBase< VectorType > * precondition
Definition: matrix_lib.h:505
void Tvmult(VectorType &, const VectorType &) const
Definition: matrix_lib.h:729
virtual void vmult_add(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:678
void Tvmult(VectorType &w, const VectorType &v) const
Definition: matrix_lib.h:581
const size_type component
Definition: matrix_lib.h:399
SmartPointer< VectorMemory< VectorType >, ProductMatrix< VectorType > > mem
Definition: matrix_lib.h:141