Reference documentation for deal.II version 8.5.1
filtered_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 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__filtered_matrix_h
17 #define dealii__filtered_matrix_h
18 
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/smartpointer.h>
23 #include <deal.II/base/thread_management.h>
24 #include <deal.II/base/memory_consumption.h>
25 #include <deal.II/lac/pointer_matrix.h>
26 #include <deal.II/lac/vector_memory.h>
27 #include <vector>
28 #include <algorithm>
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 template <class VectorType> class FilteredMatrixBlock;
33 
191 template <typename VectorType>
193 {
194 public:
196 
201 
205  class Accessor
206  {
212  const size_type index);
213 
214  public:
218  size_type row() const;
219 
223  size_type column() const;
224 
228  double value() const;
229 
230  private:
234  void advance ();
235 
240 
245  /*
246  * Make enclosing class a
247  * friend.
248  */
249  friend class const_iterator;
250  };
251 
256  {
257  public:
262  const size_type index);
263 
268 
273 
277  const Accessor &operator* () const;
278 
282  const Accessor *operator-> () const;
283 
287  bool operator == (const const_iterator &) const;
291  bool operator != (const const_iterator &) const;
292 
297  bool operator < (const const_iterator &) const;
298 
303  bool operator > (const const_iterator &) const;
304 
305  private:
310  };
311 
316  typedef std::pair<size_type, double> IndexValuePair;
317 
326  FilteredMatrix ();
327 
332  FilteredMatrix (const FilteredMatrix &fm);
333 
342  template <typename MatrixType>
343  FilteredMatrix (const MatrixType &matrix,
344  const bool expect_constrained_source = false);
345 
350 
360  template <typename MatrixType>
361  void initialize (const MatrixType &m,
362  const bool expect_constrained_source = false);
363 
367  void clear ();
369 
377  void add_constraint (const size_type i, const double v);
378 
394  template <class ConstraintList>
395  void add_constraints (const ConstraintList &new_constraints);
396 
400  void clear_constraints ();
402 
413  void apply_constraints (VectorType &v,
414  const bool matrix_is_symmetric) const DEAL_II_DEPRECATED;
419  void apply_constraints (VectorType &v) const;
420 
425  void vmult (VectorType &dst,
426  const VectorType &src) const;
427 
433  void Tvmult (VectorType &dst,
434  const VectorType &src) const;
435 
443  void vmult_add (VectorType &dst,
444  const VectorType &src) const;
445 
453  void Tvmult_add (VectorType &dst,
454  const VectorType &src) const;
456 
464  const_iterator begin () const;
468  const_iterator end () const;
470 
476  std::size_t memory_consumption () const;
477 
478 private:
491 
497  typedef typename std::vector<IndexValuePair>::const_iterator const_index_value_iterator;
498 
504  {
508  bool operator () (const IndexValuePair &i1,
509  const IndexValuePair &i2) const;
510  };
511 
515  std_cxx11::shared_ptr<PointerMatrixBase<VectorType> > matrix;
516 
521  std::vector<IndexValuePair> constraints;
522 
527  void pre_filter (VectorType &v) const;
528 
534  void post_filter (const VectorType &in,
535  VectorType &out) const;
536 
537  friend class Accessor;
541  friend class FilteredMatrixBlock<VectorType>;
542 };
543 
545 /*---------------------- Inline functions -----------------------------------*/
546 
547 
548 //--------------------------------Iterators--------------------------------------//
549 
550 template<typename VectorType>
551 inline
554  const size_type index)
555  :
556  matrix(matrix),
557  index(index)
558 {
559  Assert (index <= matrix->constraints.size(),
560  ExcIndexRange(index, 0, matrix->constraints.size()));
561 }
562 
563 
564 
565 template<typename VectorType>
566 inline
569 {
570  return matrix->constraints[index].first;
571 }
572 
573 
574 
575 template<typename VectorType>
576 inline
579 {
580  return matrix->constraints[index].first;
581 }
582 
583 
584 
585 template<typename VectorType>
586 inline
587 double
589 {
590  return matrix->constraints[index].second;
591 }
592 
593 
594 
595 template<typename VectorType>
596 inline
597 void
599 {
600  Assert (index < matrix->constraints.size(), ExcIteratorPastEnd());
601  ++index;
602 }
603 
604 
605 
606 
607 template<typename VectorType>
608 inline
611  const size_type index)
612  :
613  accessor(matrix, index)
614 {}
615 
616 
617 
618 template<typename VectorType>
619 inline
622 {
623  accessor.advance();
624  return *this;
625 }
626 
627 
628 template <typename number>
629 inline
630 const typename FilteredMatrix<number>::Accessor &
632 {
633  return accessor;
634 }
635 
636 
637 template <typename number>
638 inline
639 const typename FilteredMatrix<number>::Accessor *
641 {
642  return &accessor;
643 }
644 
645 
646 template <typename number>
647 inline
648 bool
650 operator == (const const_iterator &other) const
651 {
652  return (accessor.index == other.accessor.index
653  && accessor.matrix == other.accessor.matrix);
654 }
655 
656 
657 template <typename number>
658 inline
659 bool
661 operator != (const const_iterator &other) const
662 {
663  return ! (*this == other);
664 }
665 
666 
667 
668 //------------------------------- FilteredMatrix ---------------------------------------//
669 
670 template <typename number>
671 inline
674 {
675  return const_iterator(this, 0);
676 }
677 
678 
679 template <typename number>
680 inline
683 {
684  return const_iterator(this, constraints.size());
685 }
686 
687 
688 template <typename VectorType>
689 inline
690 bool
693  const IndexValuePair &i2) const
694 {
695  return (i1.first < i2.first);
696 }
697 
698 
699 
700 template <typename VectorType>
701 template <typename MatrixType>
702 inline
703 void
704 FilteredMatrix<VectorType>::initialize (const MatrixType &m, bool ecs)
705 {
706  matrix.reset (new_pointer_matrix_base(m, VectorType()));
707 
709 }
710 
711 
712 
713 template <typename VectorType>
714 inline
716  :
718 {}
719 
720 
721 
722 template <typename VectorType>
723 inline
725  :
726  Subscriptor(),
727  expect_constrained_source(fm.expect_constrained_source),
728  matrix(fm.matrix),
729  constraints (fm.constraints)
730 {}
731 
732 
733 
734 template <typename VectorType>
735 template <typename MatrixType>
736 inline
738 FilteredMatrix (const MatrixType &m,
739  const bool ecs)
740  :
741  expect_constrained_source (false)
742 {
743  initialize (m, ecs);
744 }
745 
746 
747 
748 template <typename VectorType>
749 inline
752 {
753  matrix = fm.matrix;
754  expect_constrained_source = fm.expect_constrained_source;
755  constraints = fm.constraints;
756  return *this;
757 }
758 
759 
760 
761 template <typename VectorType>
762 inline
763 void
764 FilteredMatrix<VectorType>::add_constraint (const size_type index, const double value)
765 {
766  // add new constraint to end
767  constraints.push_back(IndexValuePair(index, value));
768 }
769 
770 
771 
772 template <typename VectorType>
773 template <class ConstraintList>
774 inline
775 void
776 FilteredMatrix<VectorType>::add_constraints (const ConstraintList &new_constraints)
777 {
778  // add new constraints to end
779  const size_type old_size = constraints.size();
780  constraints.reserve (old_size + new_constraints.size());
781  constraints.insert (constraints.end(),
782  new_constraints.begin(),
783  new_constraints.end());
784  // then merge the two arrays to
785  // form one sorted one
786  std::inplace_merge (constraints.begin(),
787  constraints.begin()+old_size,
788  constraints.end(),
789  PairComparison());
790 }
791 
792 
793 
794 template <typename VectorType>
795 inline
796 void
798 {
799  // swap vectors to release memory
800  std::vector<IndexValuePair> empty;
801  constraints.swap (empty);
802 }
803 
804 
805 
806 template <typename VectorType>
807 inline
808 void
810 {
811  clear_constraints();
812  matrix.reset();
813 }
814 
815 
816 
817 template <typename VectorType>
818 inline
819 void
821 (VectorType &v,
822  const bool /* matrix_is_symmetric */) const
823 {
824  apply_constraints(v);
825 }
826 
827 
828 template <typename VectorType>
829 inline
830 void
832 {
834  typename VectorMemory<VectorType>::Pointer tmp_vector(mem);
835  tmp_vector->reinit(v);
836  const_index_value_iterator i = constraints.begin();
837  const const_index_value_iterator e = constraints.end();
838  for (; i!=e; ++i)
839  {
840  AssertIsFinite(i->second);
841  (*tmp_vector)(i->first) = -i->second;
842  }
843 
844  // This vmult is without bc, to get
845  // the rhs correction in a correct
846  // way.
847  matrix->vmult_add(v, *tmp_vector);
848  // finally set constrained
849  // entries themselves
850  for (i=constraints.begin(); i!=e; ++i)
851  {
852  AssertIsFinite(i->second);
853  v(i->first) = i->second;
854  }
855 }
856 
857 
858 template <typename VectorType>
859 inline
860 void
862 {
863  // iterate over all constraints and
864  // zero out value
865  const_index_value_iterator i = constraints.begin();
866  const const_index_value_iterator e = constraints.end();
867  for (; i!=e; ++i)
868  v(i->first) = 0;
869 }
870 
871 
872 
873 template <typename VectorType>
874 inline
875 void
877  VectorType &out) const
878 {
879  // iterate over all constraints and
880  // set value correctly
881  const_index_value_iterator i = constraints.begin();
882  const const_index_value_iterator e = constraints.end();
883  for (; i!=e; ++i)
884  {
885  AssertIsFinite(in(i->first));
886  out(i->first) = in(i->first);
887  }
888 }
889 
890 
891 
892 template <typename VectorType>
893 inline
894 void
895 FilteredMatrix<VectorType>::vmult (VectorType &dst, const VectorType &src) const
896 {
897  if (!expect_constrained_source)
898  {
900  VectorType *tmp_vector = mem.alloc();
901  // first copy over src vector and
902  // pre-filter
903  tmp_vector->reinit(src, true);
904  *tmp_vector = src;
905  pre_filter (*tmp_vector);
906  // then let matrix do its work
907  matrix->vmult (dst, *tmp_vector);
908  mem.free(tmp_vector);
909  }
910  else
911  {
912  matrix->vmult (dst, src);
913  }
914 
915  // finally do post-filtering
916  post_filter (src, dst);
917 }
918 
919 
920 
921 template <typename VectorType>
922 inline
923 void
924 FilteredMatrix<VectorType>::Tvmult (VectorType &dst, const VectorType &src) const
925 {
926  if (!expect_constrained_source)
927  {
929  VectorType *tmp_vector = mem.alloc();
930  // first copy over src vector and
931  // pre-filter
932  tmp_vector->reinit(src, true);
933  *tmp_vector = src;
934  pre_filter (*tmp_vector);
935  // then let matrix do its work
936  matrix->Tvmult (dst, *tmp_vector);
937  mem.free(tmp_vector);
938  }
939  else
940  {
941  matrix->Tvmult (dst, src);
942  }
943 
944  // finally do post-filtering
945  post_filter (src, dst);
946 }
947 
948 
949 
950 template <typename VectorType>
951 inline
952 void
953 FilteredMatrix<VectorType>::vmult_add (VectorType &dst, const VectorType &src) const
954 {
955  if (!expect_constrained_source)
956  {
958  VectorType *tmp_vector = mem.alloc();
959  // first copy over src vector and
960  // pre-filter
961  tmp_vector->reinit(src, true);
962  *tmp_vector = src;
963  pre_filter (*tmp_vector);
964  // then let matrix do its work
965  matrix->vmult_add (dst, *tmp_vector);
966  mem.free(tmp_vector);
967  }
968  else
969  {
970  matrix->vmult_add (dst, src);
971  }
972 
973  // finally do post-filtering
974  post_filter (src, dst);
975 }
976 
977 
978 
979 template <typename VectorType>
980 inline
981 void
982 FilteredMatrix<VectorType>::Tvmult_add (VectorType &dst, const VectorType &src) const
983 {
984  if (!expect_constrained_source)
985  {
987  VectorType *tmp_vector = mem.alloc();
988  // first copy over src vector and
989  // pre-filter
990  tmp_vector->reinit(src, true);
991  *tmp_vector = src;
992  pre_filter (*tmp_vector);
993  // then let matrix do its work
994  matrix->Tvmult_add (dst, *tmp_vector);
995  mem.free(tmp_vector);
996  }
997  else
998  {
999  matrix->Tvmult_add (dst, src);
1000  }
1001 
1002  // finally do post-filtering
1003  post_filter (src, dst);
1004 }
1005 
1006 
1007 
1008 template <typename VectorType>
1009 inline
1010 std::size_t
1012 {
1013  return (MemoryConsumption::memory_consumption (matrix) +
1015 }
1016 
1017 
1018 
1019 DEAL_II_NAMESPACE_CLOSE
1020 
1021 #endif
1022 /*---------------------------- filtered_matrix.h ---------------------------*/
void initialize(const MatrixType &m, const bool expect_constrained_source=false)
const FilteredMatrix< VectorType > * matrix
const Accessor * operator->() const
const_iterator begin() const
std_cxx11::shared_ptr< PointerMatrixBase< VectorType > > matrix
types::global_dof_index size_type
void vmult_add(VectorType &dst, const VectorType &src) const
bool operator<(const const_iterator &) const
size_type column() const
void pre_filter(VectorType &v) const
void apply_constraints(VectorType &v, const bool matrix_is_symmetric) const 1
std::vector< IndexValuePair >::const_iterator const_index_value_iterator
void post_filter(const VectorType &in, VectorType &out) const
bool operator>(const const_iterator &) const
std::vector< IndexValuePair > constraints
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
FilteredMatrix & operator=(const FilteredMatrix &fm)
Accessor(const FilteredMatrix< VectorType > *matrix, const size_type index)
void add_constraint(const size_type i, const double v)
const Accessor & operator*() const
void vmult(VectorType &dst, const VectorType &src) const
unsigned int global_dof_index
Definition: types.h:88
void Tvmult(VectorType &dst, const VectorType &src) const
virtual VectorType * alloc()
#define Assert(cond, exc)
Definition: exceptions.h:313
bool expect_constrained_source
bool operator()(const IndexValuePair &i1, const IndexValuePair &i2) const
bool operator!=(const const_iterator &) const
virtual void free(const VectorType *const)
bool operator==(const const_iterator &) const
void Tvmult_add(VectorType &dst, const VectorType &src) const
void add_constraints(const ConstraintList &new_constraints)
void clear_constraints()
static ::ExceptionBase & ExcIteratorPastEnd()
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
std::pair< size_type, double > IndexValuePair
const_iterator(const FilteredMatrix< VectorType > *matrix, const size_type index)
#define AssertIsFinite(number)
Definition: exceptions.h:1197
std::size_t memory_consumption() const
const_iterator end() const