Reference documentation for deal.II version 9.0.0
mg_transfer.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 2018 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_mg_transfer_h
17 #define dealii_mg_transfer_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/lac/block_vector.h>
22 #include <deal.II/lac/constraint_matrix.h>
23 #include <deal.II/lac/sparse_matrix.h>
24 #include <deal.II/lac/block_sparsity_pattern.h>
25 #include <deal.II/lac/trilinos_sparse_matrix.h>
26 #include <deal.II/lac/la_parallel_vector.h>
27 
28 #include <deal.II/lac/vector_memory.h>
29 
30 #include <deal.II/multigrid/mg_base.h>
31 #include <deal.II/multigrid/mg_constrained_dofs.h>
32 #include <deal.II/base/mg_level_object.h>
33 
34 #include <deal.II/dofs/dof_handler.h>
35 
36 #include <memory>
37 
38 
39 DEAL_II_NAMESPACE_OPEN
40 
41 
42 namespace internal
43 {
44  template <typename VectorType>
45  struct MatrixSelector
46  {
47  typedef ::SparsityPattern Sparsity;
48  typedef ::SparseMatrix<typename VectorType::value_type> Matrix;
49 
50  template <typename SparsityPatternType, typename DoFHandlerType>
51  static void reinit(Matrix &matrix, Sparsity &sparsity, int level, const SparsityPatternType &sp, const DoFHandlerType &)
52  {
53  sparsity.copy_from (sp);
54  (void)level;
55  matrix.reinit (sparsity);
56  }
57  };
58 
59 #ifdef DEAL_II_WITH_TRILINOS
60  template <typename Number>
61  struct MatrixSelector<LinearAlgebra::distributed::Vector<Number> >
62  {
63  typedef ::TrilinosWrappers::SparsityPattern Sparsity;
64  typedef ::TrilinosWrappers::SparseMatrix Matrix;
65 
66  template <typename SparsityPatternType, typename DoFHandlerType>
67  static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, DoFHandlerType &dh)
68  {
71  (&(dh.get_triangulation()));
72  MPI_Comm communicator = dist_tria != nullptr ?
73  dist_tria->get_communicator() :
74  MPI_COMM_SELF;
75 
76  matrix.reinit(dh.locally_owned_mg_dofs(level+1),
77  dh.locally_owned_mg_dofs(level),
78  sp, communicator, true);
79  }
80 
81  };
82 
83  template <>
84  struct MatrixSelector<::TrilinosWrappers::MPI::Vector>
85  {
86  typedef ::TrilinosWrappers::SparsityPattern Sparsity;
87  typedef ::TrilinosWrappers::SparseMatrix Matrix;
88 
89  template <typename SparsityPatternType, typename DoFHandlerType>
90  static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, DoFHandlerType &dh)
91  {
94  (&(dh.get_triangulation()));
95  MPI_Comm communicator = dist_tria != nullptr ?
96  dist_tria->get_communicator() :
97  MPI_COMM_SELF;
98  matrix.reinit(dh.locally_owned_mg_dofs(level+1),
99  dh.locally_owned_mg_dofs(level),
100  sp, communicator, true);
101  }
102 
103  };
104 
105 #ifdef DEAL_II_WITH_MPI
106  template <>
107  struct MatrixSelector<::LinearAlgebra::EpetraWrappers::Vector>
108  {
109  typedef ::TrilinosWrappers::SparsityPattern Sparsity;
110  typedef ::TrilinosWrappers::SparseMatrix Matrix;
111 
112  template <typename SparsityPatternType, typename DoFHandlerType>
113  static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, DoFHandlerType &dh)
114  {
117  (&(dh.get_triangulation()));
118  MPI_Comm communicator = dist_tria != nullptr ?
119  dist_tria->get_communicator() :
120  MPI_COMM_SELF;
121  matrix.reinit(dh.locally_owned_mg_dofs(level+1),
122  dh.locally_owned_mg_dofs(level),
123  sp, communicator, true);
124  }
125  };
126 #endif
127 
128 #else
129  // ! DEAL_II_WITH_TRILINOS
130  template <typename Number>
131  struct MatrixSelector<LinearAlgebra::distributed::Vector<Number> >
132  {
133  typedef ::SparsityPattern Sparsity;
134  typedef ::SparseMatrix<Number> Matrix;
135 
136  template <typename SparsityPatternType, typename DoFHandlerType>
137  static void reinit(Matrix &, Sparsity &, int, const SparsityPatternType &, const DoFHandlerType &)
138  {
140  "ERROR: MGTransferPrebuilt with LinearAlgebra::distributed::Vector currently "
141  "needs deal.II to be configured with Trilinos."));
142  }
143  };
144 
145 #endif
146 }
147 
148 /*
149  * MGTransferBase is defined in mg_base.h
150  */
151 
154 
155 
156 
164 template <typename VectorType>
165 class MGLevelGlobalTransfer : public MGTransferBase<VectorType>
166 {
167 public:
168 
172  void clear ();
173 
180  template <int dim, class InVector, int spacedim>
181  void
182  copy_to_mg (const DoFHandler<dim,spacedim> &mg_dof,
184  const InVector &src) const;
185 
193  template <int dim, class OutVector, int spacedim>
194  void
195  copy_from_mg (const DoFHandler<dim,spacedim> &mg_dof,
196  OutVector &dst,
197  const MGLevelObject<VectorType> &src) const;
198 
204  template <int dim, class OutVector, int spacedim>
205  void
207  OutVector &dst,
208  const MGLevelObject<VectorType> &src) const;
209 
225  void
226  set_component_to_block_map (const std::vector<unsigned int> &map);
227 
231  std::size_t memory_consumption () const;
232 
236  void print_indices(std::ostream &os) const;
237 
238 protected:
239 
243  template <int dim, int spacedim>
245 
249  std::vector<types::global_dof_index> sizes;
250 
258  std::vector<std::vector<std::pair<types::global_dof_index, types::global_dof_index> > >
260 
268  std::vector<std::vector<std::pair<types::global_dof_index, types::global_dof_index> > >
270 
278  std::vector<std::vector<std::pair<types::global_dof_index, types::global_dof_index> > >
280 
288 
293  std::vector<unsigned int> component_to_block_map;
294 
299 };
300 
301 
302 
314 template <typename Number>
315 class MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number> > : public MGTransferBase<LinearAlgebra::distributed::Vector<Number> >
316 {
317 public:
318 
322  void clear ();
323 
330  template <int dim, typename Number2, int spacedim>
331  void
332  copy_to_mg (const DoFHandler<dim,spacedim> &mg_dof,
335 
343  template <int dim, typename Number2, int spacedim>
344  void
345  copy_from_mg (const DoFHandler<dim,spacedim> &mg_dof,
348 
354  template <int dim, typename Number2, int spacedim>
355  void
359 
375  void
376  set_component_to_block_map (const std::vector<unsigned int> &map);
377 
381  std::size_t memory_consumption () const;
382 
386  void print_indices(std::ostream &os) const;
387 
388 protected:
389 
394  template <int dim, typename Number2, int spacedim>
395  void
396  copy_to_mg (const DoFHandler<dim,spacedim> &mg_dof,
399  const bool solution_transfer) const;
400 
404  template <int dim, int spacedim>
406 
410  std::vector<types::global_dof_index> sizes;
411 
419  std::vector<std::vector<std::pair<unsigned int, unsigned int> > >
421 
422 
426  std::vector<std::vector<std::pair<unsigned int, unsigned int> > >
428 
436  std::vector<std::vector<std::pair<unsigned int, unsigned int> > >
438 
442  std::vector<std::vector<std::pair<unsigned int, unsigned int> > >
444 
452  std::vector<std::vector<std::pair<unsigned int, unsigned int> > >
454 
458  std::vector<std::vector<std::pair<unsigned int, unsigned int> > >
460 
468 
476 
481  std::vector<unsigned int> component_to_block_map;
482 
487 
494 
499 
505 
510 
511 };
512 
513 
514 
528 template <typename VectorType>
529 class MGTransferPrebuilt : public MGLevelGlobalTransfer<VectorType>
530 {
531 public:
536  MGTransferPrebuilt () = default;
537 
543 
550  DEAL_II_DEPRECATED
551  MGTransferPrebuilt (const ConstraintMatrix &constraints,
553 
557  virtual ~MGTransferPrebuilt () = default;
558 
563 
569  DEAL_II_DEPRECATED
570  void initialize_constraints (const ConstraintMatrix &constraints,
572 
576  void clear ();
577 
581  template <int dim, int spacedim>
582  void build_matrices (const DoFHandler<dim,spacedim> &mg_dof);
583 
595  virtual void prolongate (const unsigned int to_level,
596  VectorType &dst,
597  const VectorType &src) const;
598 
614  virtual void restrict_and_add (const unsigned int from_level,
615  VectorType &dst,
616  const VectorType &src) const;
617 
622 
627 
631  std::size_t memory_consumption () const;
632 
636  void print_matrices(std::ostream &os) const;
637 
638 private:
639 
643  std::vector<std::shared_ptr<typename internal::MatrixSelector<VectorType>::Sparsity> > prolongation_sparsities;
644 
650  std::vector<std::shared_ptr<typename internal::MatrixSelector<VectorType>::Matrix> > prolongation_matrices;
651 
656  std::vector<std::vector<bool> > interface_dofs;
657 };
658 
659 
663 DEAL_II_NAMESPACE_CLOSE
664 
665 #endif
void initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs)
MGTransferPrebuilt()=default
void copy_from_mg(const DoFHandler< dim, spacedim > &mg_dof, OutVector &dst, const MGLevelObject< VectorType > &src) const
std::size_t memory_consumption() const
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Matrix > > prolongation_matrices
Definition: mg_transfer.h:650
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices
Definition: mg_transfer.h:259
Contents is actually a matrix.
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices_global_mine
Definition: mg_transfer.h:437
void build_matrices(const DoFHandler< dim, spacedim > &mg_dof)
void copy_from_mg_add(const DoFHandler< dim, spacedim > &mg_dof, OutVector &dst, const MGLevelObject< VectorType > &src) const
LinearAlgebra::distributed::Vector< Number > solution_ghosted_global_vector
Definition: mg_transfer.h:498
void copy_to_mg(const DoFHandler< dim, spacedim > &mg_dof, MGLevelObject< VectorType > &dst, const InVector &src) const
#define AssertThrow(cond, exc)
Definition: exceptions.h:1221
std::vector< unsigned int > component_to_block_map
Definition: mg_transfer.h:293
void print_indices(std::ostream &os) const
LinearAlgebra::distributed::Vector< Number > ghosted_global_vector
Definition: mg_transfer.h:493
static ::ExceptionBase & ExcNoProlongation()
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices_global_mine
Definition: mg_transfer.h:443
virtual ~MGTransferPrebuilt()=default
void print_matrices(std::ostream &os) const
std::size_t memory_consumption() const
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_level_mine
Definition: mg_transfer.h:279
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Sparsity > > prolongation_sparsities
Definition: mg_transfer.h:643
void fill_and_communicate_copy_indices(const DoFHandler< dim, spacedim > &mg_dof)
virtual MPI_Comm get_communicator() const
Definition: tria_base.cc:146
#define DeclException0(Exception0)
Definition: exceptions.h:323
SmartPointer< const MGConstrainedDoFs, MGLevelGlobalTransfer< LinearAlgebra::distributed::Vector< Number > > > mg_constrained_dofs
Definition: mg_transfer.h:486
SmartPointer< const MGConstrainedDoFs, MGLevelGlobalTransfer< VectorType > > mg_constrained_dofs
Definition: mg_transfer.h:298
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices
Definition: mg_transfer.h:420
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices_level_mine
Definition: mg_transfer.h:459
std::vector< types::global_dof_index > sizes
Definition: mg_transfer.h:249
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices
Definition: mg_transfer.h:427
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_global_mine
Definition: mg_transfer.h:269
static ::ExceptionBase & ExcMatricesNotBuilt()
virtual void prolongate(const unsigned int to_level, VectorType &dst, const VectorType &src) const
MGLevelObject< LinearAlgebra::distributed::Vector< Number > > solution_ghosted_level_vector
Definition: mg_transfer.h:509
static ::ExceptionBase & ExcNotImplemented()
std::vector< std::vector< bool > > interface_dofs
Definition: mg_transfer.h:656
void set_component_to_block_map(const std::vector< unsigned int > &map)
MGLevelObject< LinearAlgebra::distributed::Vector< Number > > ghosted_level_vector
Definition: mg_transfer.h:504
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices_level_mine
Definition: mg_transfer.h:453
virtual void restrict_and_add(const unsigned int from_level, VectorType &dst, const VectorType &src) const