Reference documentation for deal.II version 8.4.2
filtered_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 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__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 <typename number> class Vector;
33 template <class VectorType> class FilteredMatrixBlock;
34 
35 
193 template <typename VectorType>
195 {
196 public:
198 
203 
207  class Accessor
208  {
214  const size_type index);
215 
216  public:
220  size_type row() const;
221 
225  size_type column() const;
226 
230  double value() const;
231 
232  private:
236  void advance ();
237 
242 
246  size_type index;
247  /*
248  * Make enclosing class a
249  * friend.
250  */
251  friend class const_iterator;
252  };
253 
258  {
259  public:
264  const size_type index);
265 
269  const_iterator &operator++ ();
270 
274  const_iterator &operator++ (int);
275 
279  const Accessor &operator* () const;
280 
284  const Accessor *operator-> () const;
285 
289  bool operator == (const const_iterator &) const;
293  bool operator != (const const_iterator &) const;
294 
299  bool operator < (const const_iterator &) const;
300 
305  bool operator > (const const_iterator &) const;
306 
307  private:
312  };
313 
318  typedef std::pair<size_type, double> IndexValuePair;
319 
328  FilteredMatrix ();
329 
334  FilteredMatrix (const FilteredMatrix &fm);
335 
344  template <typename MatrixType>
345  FilteredMatrix (const MatrixType &matrix,
346  bool expect_constrained_source = false);
347 
352 
362  template <typename MatrixType>
363  void initialize (const MatrixType &m,
364  bool expect_constrained_source = false);
365 
369  void clear ();
371 
379  void add_constraint (const size_type i, const double v);
380 
396  template <class ConstraintList>
397  void add_constraints (const ConstraintList &new_constraints);
398 
402  void clear_constraints ();
404 
415  void apply_constraints (VectorType &v,
416  const bool matrix_is_symmetric) const DEAL_II_DEPRECATED;
421  void apply_constraints (VectorType &v) const;
422 
427  void vmult (VectorType &dst,
428  const VectorType &src) const;
429 
435  void Tvmult (VectorType &dst,
436  const VectorType &src) const;
437 
445  void vmult_add (VectorType &dst,
446  const VectorType &src) const;
447 
455  void Tvmult_add (VectorType &dst,
456  const VectorType &src) const;
458 
466  const_iterator begin () const;
470  const_iterator end () const;
472 
478  std::size_t memory_consumption () const;
479 
480 private:
493 
499  typedef typename std::vector<IndexValuePair>::const_iterator const_index_value_iterator;
500 
506  {
510  bool operator () (const IndexValuePair &i1,
511  const IndexValuePair &i2) const;
512  };
513 
517  std_cxx11::shared_ptr<PointerMatrixBase<VectorType> > matrix;
518 
523  std::vector<IndexValuePair> constraints;
524 
529  void pre_filter (VectorType &v) const;
530 
536  void post_filter (const VectorType &in,
537  VectorType &out) const;
538 
539  friend class Accessor;
543  friend class FilteredMatrixBlock<VectorType>;
544 };
545 
547 /*---------------------- Inline functions -----------------------------------*/
548 
549 
550 //--------------------------------Iterators--------------------------------------//
551 
552 template<typename VectorType>
553 inline
556  const size_type index)
557  :
558  matrix(matrix),
559  index(index)
560 {
561  Assert (index <= matrix->constraints.size(),
562  ExcIndexRange(index, 0, matrix->constraints.size()));
563 }
564 
565 
566 
567 template<typename VectorType>
568 inline
571 {
572  return matrix->constraints[index].first;
573 }
574 
575 
576 
577 template<typename VectorType>
578 inline
581 {
582  return matrix->constraints[index].first;
583 }
584 
585 
586 
587 template<typename VectorType>
588 inline
589 double
591 {
592  return matrix->constraints[index].second;
593 }
594 
595 
596 
597 template<typename VectorType>
598 inline
599 void
601 {
602  Assert (index < matrix->constraints.size(), ExcIteratorPastEnd());
603  ++index;
604 }
605 
606 
607 
608 
609 template<typename VectorType>
610 inline
613  const size_type index)
614  :
615  accessor(matrix, index)
616 {}
617 
618 
619 
620 template<typename VectorType>
621 inline
624 {
625  accessor.advance();
626  return *this;
627 }
628 
629 
630 template <typename number>
631 inline
632 const typename FilteredMatrix<number>::Accessor &
634 {
635  return accessor;
636 }
637 
638 
639 template <typename number>
640 inline
641 const typename FilteredMatrix<number>::Accessor *
643 {
644  return &accessor;
645 }
646 
647 
648 template <typename number>
649 inline
650 bool
652 operator == (const const_iterator &other) const
653 {
654  return (accessor.index == other.accessor.index
655  && accessor.matrix == other.accessor.matrix);
656 }
657 
658 
659 template <typename number>
660 inline
661 bool
663 operator != (const const_iterator &other) const
664 {
665  return ! (*this == other);
666 }
667 
668 
669 
670 //------------------------------- FilteredMatrix ---------------------------------------//
671 
672 template <typename number>
673 inline
676 {
677  return const_iterator(this, 0);
678 }
679 
680 
681 template <typename number>
682 inline
685 {
686  return const_iterator(this, constraints.size());
687 }
688 
689 
690 template <typename VectorType>
691 inline
692 bool
695  const IndexValuePair &i2) const
696 {
697  return (i1.first < i2.first);
698 }
699 
700 
701 
702 template <typename VectorType>
703 template <typename MatrixType>
704 inline
705 void
706 FilteredMatrix<VectorType>::initialize (const MatrixType &m, bool ecs)
707 {
708  matrix.reset (new_pointer_matrix_base(m, VectorType()));
709 
711 }
712 
713 
714 
715 template <typename VectorType>
716 inline
718 {}
719 
720 
721 
722 template <typename VectorType>
723 inline
725  :
726  Subscriptor(),
728  matrix(fm.matrix),
730 {}
731 
732 
733 
734 template <typename VectorType>
735 template <typename MatrixType>
736 inline
738 FilteredMatrix (const MatrixType &m, bool ecs)
739 {
740  initialize (m, ecs);
741 }
742 
743 
744 
745 template <typename VectorType>
746 inline
749 {
750  matrix = fm.matrix;
753  return *this;
754 }
755 
756 
757 
758 template <typename VectorType>
759 inline
760 void
761 FilteredMatrix<VectorType>::add_constraint (const size_type index, const double value)
762 {
763  // add new constraint to end
764  constraints.push_back(IndexValuePair(index, value));
765 }
766 
767 
768 
769 template <typename VectorType>
770 template <class ConstraintList>
771 inline
772 void
773 FilteredMatrix<VectorType>::add_constraints (const ConstraintList &new_constraints)
774 {
775  // add new constraints to end
776  const size_type old_size = constraints.size();
777  constraints.reserve (old_size + new_constraints.size());
778  constraints.insert (constraints.end(),
779  new_constraints.begin(),
780  new_constraints.end());
781  // then merge the two arrays to
782  // form one sorted one
783  std::inplace_merge (constraints.begin(),
784  constraints.begin()+old_size,
785  constraints.end(),
786  PairComparison());
787 }
788 
789 
790 
791 template <typename VectorType>
792 inline
793 void
795 {
796  // swap vectors to release memory
797  std::vector<IndexValuePair> empty;
798  constraints.swap (empty);
799 }
800 
801 
802 
803 template <typename VectorType>
804 inline
805 void
807 {
809  matrix.reset();
810 }
811 
812 
813 
814 template <typename VectorType>
815 inline
816 void
818 (VectorType &v,
819  const bool /* matrix_is_symmetric */) const
820 {
822 }
823 
824 
825 template <typename VectorType>
826 inline
827 void
829 {
831  typename VectorMemory<VectorType>::Pointer tmp_vector(mem);
832  tmp_vector->reinit(v);
834  const const_index_value_iterator e = constraints.end();
835  for (; i!=e; ++i)
836  {
837  AssertIsFinite(i->second);
838  (*tmp_vector)(i->first) = -i->second;
839  }
840 
841  // This vmult is without bc, to get
842  // the rhs correction in a correct
843  // way.
844  matrix->vmult_add(v, *tmp_vector);
845  // finally set constrained
846  // entries themselves
847  for (i=constraints.begin(); i!=e; ++i)
848  {
849  AssertIsFinite(i->second);
850  v(i->first) = i->second;
851  }
852 }
853 
854 
855 template <typename VectorType>
856 inline
857 void
859 {
860  // iterate over all constraints and
861  // zero out value
863  const const_index_value_iterator e = constraints.end();
864  for (; i!=e; ++i)
865  v(i->first) = 0;
866 }
867 
868 
869 
870 template <typename VectorType>
871 inline
872 void
874  VectorType &out) const
875 {
876  // iterate over all constraints and
877  // set value correctly
879  const const_index_value_iterator e = constraints.end();
880  for (; i!=e; ++i)
881  {
882  AssertIsFinite(in(i->first));
883  out(i->first) = in(i->first);
884  }
885 }
886 
887 
888 
889 template <typename VectorType>
890 inline
891 void
892 FilteredMatrix<VectorType>::vmult (VectorType &dst, const VectorType &src) const
893 {
895  {
897  VectorType *tmp_vector = mem.alloc();
898  // first copy over src vector and
899  // pre-filter
900  tmp_vector->reinit(src, true);
901  *tmp_vector = src;
902  pre_filter (*tmp_vector);
903  // then let matrix do its work
904  matrix->vmult (dst, *tmp_vector);
905  mem.free(tmp_vector);
906  }
907  else
908  {
909  matrix->vmult (dst, src);
910  }
911 
912  // finally do post-filtering
913  post_filter (src, dst);
914 }
915 
916 
917 
918 template <typename VectorType>
919 inline
920 void
921 FilteredMatrix<VectorType>::Tvmult (VectorType &dst, const VectorType &src) const
922 {
924  {
926  VectorType *tmp_vector = mem.alloc();
927  // first copy over src vector and
928  // pre-filter
929  tmp_vector->reinit(src, true);
930  *tmp_vector = src;
931  pre_filter (*tmp_vector);
932  // then let matrix do its work
933  matrix->Tvmult (dst, *tmp_vector);
934  mem.free(tmp_vector);
935  }
936  else
937  {
938  matrix->Tvmult (dst, src);
939  }
940 
941  // finally do post-filtering
942  post_filter (src, dst);
943 }
944 
945 
946 
947 template <typename VectorType>
948 inline
949 void
950 FilteredMatrix<VectorType>::vmult_add (VectorType &dst, const VectorType &src) const
951 {
953  {
955  VectorType *tmp_vector = mem.alloc();
956  // first copy over src vector and
957  // pre-filter
958  tmp_vector->reinit(src, true);
959  *tmp_vector = src;
960  pre_filter (*tmp_vector);
961  // then let matrix do its work
962  matrix->vmult_add (dst, *tmp_vector);
963  mem.free(tmp_vector);
964  }
965  else
966  {
967  matrix->vmult_add (dst, src);
968  }
969 
970  // finally do post-filtering
971  post_filter (src, dst);
972 }
973 
974 
975 
976 template <typename VectorType>
977 inline
978 void
979 FilteredMatrix<VectorType>::Tvmult_add (VectorType &dst, const VectorType &src) const
980 {
982  {
984  VectorType *tmp_vector = mem.alloc();
985  // first copy over src vector and
986  // pre-filter
987  tmp_vector->reinit(src, true);
988  *tmp_vector = src;
989  pre_filter (*tmp_vector);
990  // then let matrix do its work
991  matrix->Tvmult_add (dst, *tmp_vector);
992  mem.free(tmp_vector);
993  }
994  else
995  {
996  matrix->Tvmult_add (dst, src);
997  }
998 
999  // finally do post-filtering
1000  post_filter (src, dst);
1001 }
1002 
1003 
1004 
1005 template <typename VectorType>
1006 inline
1007 std::size_t
1009 {
1010  return (MemoryConsumption::memory_consumption (matrix) +
1012 }
1013 
1014 
1015 
1016 DEAL_II_NAMESPACE_CLOSE
1017 
1018 #endif
1019 /*---------------------------- filtered_matrix.h ---------------------------*/
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
size_type column() const
void pre_filter(VectorType &v) const
std::vector< IndexValuePair >::const_iterator const_index_value_iterator
void post_filter(const VectorType &in, VectorType &out) const
std::vector< IndexValuePair > constraints
FilteredMatrix & operator=(const FilteredMatrix &fm)
Accessor(const FilteredMatrix< VectorType > *matrix, const size_type index)
void initialize(const MatrixType &m, bool expect_constrained_source=false)
void add_constraint(const size_type i, const double v)
#define DEAL_II_DEPRECATED
Definition: config.h:88
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:294
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()
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)
void apply_constraints(VectorType &v, const bool matrix_is_symmetric) const DEAL_II_DEPRECATED
#define AssertIsFinite(number)
Definition: exceptions.h:1096
std::size_t memory_consumption() const
const_iterator end() const