Reference documentation for deal.II version 9.0.0
precondition_block.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 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_precondition_block_h
17 #define dealii_precondition_block_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/subscriptor.h>
23 #include <deal.II/base/smartpointer.h>
24 #include <deal.II/lac/precondition_block_base.h>
25 
26 #include <vector>
27 
28 DEAL_II_NAMESPACE_OPEN
29 
80 template <typename MatrixType, typename inverse_type = typename MatrixType::value_type>
82  : public virtual Subscriptor,
83  protected PreconditionBlockBase<inverse_type>
84 {
85 private:
89  typedef typename MatrixType::value_type number;
90 
94  typedef inverse_type value_type;
95 
96 public:
101 
106  {
107  public:
113  const double relaxation = 1.,
114  const bool invert_diagonal = true,
115  const bool same_diagonal = false);
116 
120  double relaxation;
121 
126 
131 
140 
146  double threshold;
147  };
148 
149 
153  PreconditionBlock(bool store_diagonals = false);
154 
158  ~PreconditionBlock() = default;
159 
167  void initialize (const MatrixType &A,
168  const AdditionalData parameters);
169 protected:
181  void initialize (const MatrixType &A,
182  const std::vector<size_type> &permutation,
183  const std::vector<size_type> &inverse_permutation,
184  const AdditionalData parameters);
185 
209  void set_permutation(const std::vector<size_type> &permutation,
210  const std::vector<size_type> &inverse_permutation);
211 
216 public:
222  void clear();
223 
227  bool empty () const;
228 
234  size_type j) const;
235 
251  void invert_diagblocks();
252 
264  template <typename number2>
265  void forward_step (Vector<number2> &dst,
266  const Vector<number2> &prev,
267  const Vector<number2> &src,
268  const bool transpose_diagonal) const;
269 
281  template <typename number2>
282  void backward_step (Vector<number2> &dst,
283  const Vector<number2> &prev,
284  const Vector<number2> &src,
285  const bool transpose_diagonal) const;
286 
287 
291  size_type block_size () const;
292 
297  std::size_t memory_consumption () const;
298 
309  int, int,
310  << "The blocksize " << arg1
311  << " and the size of the matrix " << arg2
312  << " do not match.");
313 
318 
320 
321 protected:
327 
338  double relaxation;
339 
343  std::vector<size_type> permutation;
344 
348  std::vector<size_type> inverse_permutation;
349 };
350 
351 
352 
366 template <typename MatrixType, typename inverse_type = typename MatrixType::value_type>
367 class PreconditionBlockJacobi : public virtual Subscriptor,
368  private PreconditionBlock<MatrixType, inverse_type>
369 {
370 private:
374  typedef typename MatrixType::value_type number;
375 
376 public:
381 
386  {
387  private:
391  class Accessor
392  {
393  public:
399  const size_type row);
400 
404  size_type row() const;
405 
409  size_type column() const;
410 
414  inverse_type value() const;
415 
416  protected:
421 
426 
431 
436 
441 
445  friend class const_iterator;
446  };
447 
448  public:
453  const size_type row);
454 
459 
463  const Accessor &operator* () const;
464 
468  const Accessor *operator-> () const;
469 
473  bool operator == (const const_iterator &) const;
477  bool operator != (const const_iterator &) const;
478 
483  bool operator < (const const_iterator &) const;
484 
485  private:
490  };
491 
508 
516  template <typename number2>
517  void vmult (Vector<number2> &, const Vector<number2> &) const;
518 
522  template <typename number2>
523  void Tvmult (Vector<number2> &, const Vector<number2> &) const;
531  template <typename number2>
532  void vmult_add (Vector<number2> &, const Vector<number2> &) const;
533 
537  template <typename number2>
538  void Tvmult_add (Vector<number2> &, const Vector<number2> &) const;
539 
543  template <typename number2>
544  void step (Vector<number2> &dst, const Vector<number2> &rhs) const;
545 
549  template <typename number2>
550  void Tstep (Vector<number2> &dst, const Vector<number2> &rhs) const;
551 
555  const_iterator begin () const;
556 
560  const_iterator end () const;
561 
565  const_iterator begin (const size_type r) const;
566 
570  const_iterator end (const size_type r) const;
571 
572 
573 private:
580  template <typename number2>
581  void do_vmult (Vector<number2> &,
582  const Vector<number2> &,
583  bool adding) const;
584 
585  friend class Accessor;
586  friend class const_iterator;
587 };
588 
589 
590 
626 template <typename MatrixType, typename inverse_type = typename MatrixType::value_type>
627 class PreconditionBlockSOR : public virtual Subscriptor,
628  protected PreconditionBlock<MatrixType, inverse_type>
629 {
630 public:
635 
640 
644  typedef typename MatrixType::value_type number;
645 
659 
670  template <typename number2>
671  void vmult (Vector<number2> &, const Vector<number2> &) const;
672 
683  template <typename number2>
684  void vmult_add (Vector<number2> &, const Vector<number2> &) const;
685 
694  template <typename number2>
695  void Tvmult (Vector<number2> &, const Vector<number2> &) const;
696 
707  template <typename number2>
708  void Tvmult_add (Vector<number2> &, const Vector<number2> &) const;
709 
713  template <typename number2>
714  void step (Vector<number2> &dst, const Vector<number2> &rhs) const;
715 
719  template <typename number2>
720  void Tstep (Vector<number2> &dst, const Vector<number2> &rhs) const;
721 
722 protected:
726  PreconditionBlockSOR(bool store);
727 
737  template <typename number2>
738  void forward (Vector<number2> &,
739  const Vector<number2> &,
740  const bool transpose_diagonal,
741  const bool adding) const;
742 
752  template <typename number2>
753  void backward (Vector<number2> &,
754  const Vector<number2> &,
755  const bool transpose_diagonal,
756  const bool adding) const;
757 };
758 
759 
781 template <typename MatrixType, typename inverse_type = typename MatrixType::value_type>
782 class PreconditionBlockSSOR : public virtual Subscriptor,
783  private PreconditionBlockSOR<MatrixType, inverse_type>
784 {
785 public:
790 
794  typedef typename MatrixType::value_type number;
795 
800 
801  // Keep AdditionalData accessible
803 
804  // The following are the
805  // functions of the base classes
806  // which we want to keep
807  // accessible.
822 
830  template <typename number2>
831  void vmult (Vector<number2> &, const Vector<number2> &) const;
832 
836  template <typename number2>
837  void Tvmult (Vector<number2> &, const Vector<number2> &) const;
838 
842  template <typename number2>
843  void step (Vector<number2> &dst, const Vector<number2> &rhs) const;
844 
848  template <typename number2>
849  void Tstep (Vector<number2> &dst, const Vector<number2> &rhs) const;
850 };
851 
853 //---------------------------------------------------------------------------
854 
855 #ifndef DOXYGEN
856 
857 template <typename MatrixType, typename inverse_type>
858 inline bool
860 {
861  if (A == nullptr)
862  return true;
863  return A->empty();
864 }
865 
866 
867 template <typename MatrixType, typename inverse_type>
868 inline inverse_type
870  size_type i,
871  size_type j) const
872 {
873  const size_type bs = blocksize;
874  const unsigned int nb = i/bs;
875 
876  const FullMatrix<inverse_type> &B = this->inverse(nb);
877 
878  const size_type ib = i % bs;
879  const size_type jb = j % bs;
880 
881  if (jb + nb*bs != j)
882  {
883  return 0.;
884  }
885 
886  return B(ib, jb);
887 }
888 
889 //---------------------------------------------------------------------------
890 
891 template <typename MatrixType, typename inverse_type>
892 inline
895  const size_type row)
896  :
897  matrix(matrix),
898  b_iterator(&matrix->inverse(0), 0, 0),
899  b_end(&matrix->inverse(0), 0, 0)
900 {
901  bs = matrix->block_size();
902  a_block = row / bs;
903 
904  // This is the end accessor, which
905  // does not have a valid block.
906  if (a_block == matrix->size())
907  return;
908 
909  const size_type r = row % bs;
910 
911  b_iterator = matrix->inverse(a_block).begin(r);
912  b_end = matrix->inverse(a_block).end();
913 
914  Assert (a_block < matrix->size(),
915  ExcIndexRange(a_block, 0, matrix->size()));
916 }
917 
918 
919 template <typename MatrixType, typename inverse_type>
920 inline
923 {
924  Assert (a_block < matrix->size(),
926 
927  return bs * a_block + b_iterator->row();
928 }
929 
930 
931 template <typename MatrixType, typename inverse_type>
932 inline
935 {
936  Assert (a_block < matrix->size(),
938 
939  return bs * a_block + b_iterator->column();
940 }
941 
942 
943 template <typename MatrixType, typename inverse_type>
944 inline
945 inverse_type
947 {
948  Assert (a_block < matrix->size(),
950 
951  return b_iterator->value();
952 }
953 
954 
955 template <typename MatrixType, typename inverse_type>
956 inline
959  const size_type row)
960  :
961  accessor(matrix, row)
962 {}
963 
964 
965 template <typename MatrixType, typename inverse_type>
966 inline
969 {
970  Assert (*this != accessor.matrix->end(), ExcIteratorPastEnd());
971 
974  {
975  ++accessor.a_block;
976 
977  if (accessor.a_block < accessor.matrix->size())
978  {
979  accessor.b_iterator = accessor.matrix->inverse(accessor.a_block).begin();
980  accessor.b_end = accessor.matrix->inverse(accessor.a_block).end();
981  }
982  }
983  return *this;
984 }
985 
986 
987 template <typename MatrixType, typename inverse_type>
988 inline
991 {
992  return accessor;
993 }
994 
995 
996 template <typename MatrixType, typename inverse_type>
997 inline
1000 {
1001  return &accessor;
1002 }
1003 
1004 
1005 template <typename MatrixType, typename inverse_type>
1006 inline
1007 bool
1009 operator == (const const_iterator &other) const
1010 {
1011  if (accessor.a_block == accessor.matrix->size() &&
1012  accessor.a_block == other.accessor.a_block)
1013  return true;
1014 
1015  if (accessor.a_block != other.accessor.a_block)
1016  return false;
1017 
1018  return (accessor.row() == other.accessor.row() &&
1019  accessor.column() == other.accessor.column());
1020 }
1021 
1022 
1023 template <typename MatrixType, typename inverse_type>
1024 inline
1025 bool
1027 operator != (const const_iterator &other) const
1028 {
1029  return ! (*this == other);
1030 }
1031 
1032 
1033 template <typename MatrixType, typename inverse_type>
1034 inline
1035 bool
1037 operator < (const const_iterator &other) const
1038 {
1039  return (accessor.row() < other.accessor.row() ||
1040  (accessor.row() == other.accessor.row() &&
1041  accessor.column() < other.accessor.column()));
1042 }
1043 
1044 
1045 template <typename MatrixType, typename inverse_type>
1046 inline
1049 {
1050  return const_iterator(this, 0);
1051 }
1052 
1053 
1054 template <typename MatrixType, typename inverse_type>
1055 inline
1058 {
1059  return const_iterator(this, this->size() * this->block_size());
1060 }
1061 
1062 
1063 template <typename MatrixType, typename inverse_type>
1064 inline
1067  const size_type r) const
1068 {
1069  Assert (r < this->A->m(), ExcIndexRange(r, 0, this->A->m()));
1070  return const_iterator(this, r);
1071 }
1072 
1073 
1074 
1075 template <typename MatrixType, typename inverse_type>
1076 inline
1079  const size_type r) const
1080 {
1081  Assert (r < this->A->m(), ExcIndexRange(r, 0, this->A->m()));
1082  return const_iterator(this, r+1);
1083 }
1084 
1085 #endif // DOXYGEN
1086 
1087 DEAL_II_NAMESPACE_CLOSE
1088 
1089 #endif
FullMatrix< inverse_type >::const_iterator b_end
std::size_t memory_consumption() const
types::global_dof_index size_type
void set_permutation(const std::vector< size_type > &permutation, const std::vector< size_type > &inverse_permutation)
void Tvmult_add(Vector< number2 > &, const Vector< number2 > &) const
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:358
Contents is actually a matrix.
FullMatrix< inverse_type >::const_iterator b_iterator
void Tstep(Vector< number2 > &dst, const Vector< number2 > &rhs) const
AdditionalData(const size_type block_size, const double relaxation=1., const bool invert_diagonal=true, const bool same_diagonal=false)
static ::ExceptionBase & ExcWrongBlockSize(int arg1, int arg2)
void step(Vector< number2 > &dst, const Vector< number2 > &rhs) const
const Accessor * operator->() const
void Tvmult(Vector< number2 > &, const Vector< number2 > &) const
const_iterator end() const
Accessor(const PreconditionBlockJacobi< MatrixType, inverse_type > *matrix, const size_type row)
void backward_step(Vector< number2 > &dst, const Vector< number2 > &prev, const Vector< number2 > &src, const bool transpose_diagonal) const
types::global_dof_index size_type
void initialize(const MatrixType &A, const AdditionalData parameters)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void Tvmult_add(Vector< number2 > &, const Vector< number2 > &) const
~PreconditionBlock()=default
std::vector< size_type > inverse_permutation
const_iterator(const PreconditionBlockJacobi< MatrixType, inverse_type > *matrix, const size_type row)
size_type block_size() const
MatrixType::value_type number
static ::ExceptionBase & ExcInverseMatricesAlreadyExist()
void step(Vector< number2 > &dst, const Vector< number2 > &rhs) const
bool operator!=(const const_iterator &) const
bool operator==(const const_iterator &) const
unsigned int global_dof_index
Definition: types.h:88
types::global_dof_index size_type
#define Assert(cond, exc)
Definition: exceptions.h:1142
value_type el(size_type i, size_type j) const
void vmult(Vector< number2 > &, const Vector< number2 > &) const
void Tvmult(Vector< number2 > &, const Vector< number2 > &) const
void vmult_add(Vector< number2 > &, const Vector< number2 > &) const
#define DeclException0(Exception0)
Definition: exceptions.h:323
MatrixType::value_type number
PreconditionBlockBase< inverse_type >::Inversion inversion
void vmult_add(Vector< number2 > &, const Vector< number2 > &) const
const_iterator begin() const
PreconditionBlock(bool store_diagonals=false)
void forward_step(Vector< number2 > &dst, const Vector< number2 > &prev, const Vector< number2 > &src, const bool transpose_diagonal) const
void Tvmult(Vector< number2 > &, const Vector< number2 > &) const
static ::ExceptionBase & ExcIteratorPastEnd()
void do_vmult(Vector< number2 > &, const Vector< number2 > &, bool adding) const
void backward(Vector< number2 > &, const Vector< number2 > &, const bool transpose_diagonal, const bool adding) const
void vmult(Vector< number2 > &, const Vector< number2 > &) const
types::global_dof_index size_type
MatrixType::value_type number
void Tstep(Vector< number2 > &dst, const Vector< number2 > &rhs) const
void invert_diagblocks()
std::vector< size_type > permutation
void vmult(Vector< number2 > &, const Vector< number2 > &) const
bool empty() const
void Tstep(Vector< number2 > &dst, const Vector< number2 > &rhs) const
void step(Vector< number2 > &dst, const Vector< number2 > &rhs) const
const Accessor & operator*() const
void invert_permuted_diagblocks()
const PreconditionBlockJacobi< MatrixType, inverse_type > * matrix
inverse_type value_type
void forward(Vector< number2 > &, const Vector< number2 > &, const bool transpose_diagonal, const bool adding) const
SmartPointer< const MatrixType, PreconditionBlock< MatrixType, inverse_type > > A
bool operator<(const const_iterator &) const
MatrixType::value_type number