Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
mg_transfer.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 2019 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.md at
12 // the top level directory of deal.II.
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/base/mg_level_object.h>
22 
23 #include <deal.II/dofs/dof_handler.h>
24 
25 #include <deal.II/lac/affine_constraints.h>
26 #include <deal.II/lac/block_sparsity_pattern.h>
27 #include <deal.II/lac/block_vector.h>
28 #include <deal.II/lac/la_parallel_vector.h>
29 #include <deal.II/lac/petsc_sparse_matrix.h>
30 #include <deal.II/lac/petsc_vector.h>
31 #include <deal.II/lac/sparse_matrix.h>
32 #include <deal.II/lac/trilinos_sparse_matrix.h>
33 #include <deal.II/lac/vector_memory.h>
34 
35 #include <deal.II/multigrid/mg_base.h>
36 #include <deal.II/multigrid/mg_constrained_dofs.h>
37 
38 #include <memory>
39 
40 
41 DEAL_II_NAMESPACE_OPEN
42 
43 
44 namespace internal
45 {
46  template <typename VectorType>
47  struct MatrixSelector
48  {
49  using Sparsity = ::SparsityPattern;
51 
52  static const bool requires_distributed_sparsity_pattern = false;
53 
54  template <typename SparsityPatternType, typename DoFHandlerType>
55  static void
56  reinit(Matrix & matrix,
57  Sparsity & sparsity,
58  int level,
59  const SparsityPatternType &sp,
60  const DoFHandlerType &)
61  {
62  sparsity.copy_from(sp);
63  (void)level;
64  matrix.reinit(sparsity);
65  }
66  };
67 
68 #ifdef DEAL_II_WITH_TRILINOS
69  template <typename Number>
70  struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>>
71  {
72  using Sparsity = ::TrilinosWrappers::SparsityPattern;
73  using Matrix = ::TrilinosWrappers::SparseMatrix;
74 
75  static const bool requires_distributed_sparsity_pattern = false;
76 
77  template <typename SparsityPatternType, typename DoFHandlerType>
78  static void
79  reinit(Matrix &matrix,
80  Sparsity &,
81  int level,
82  const SparsityPatternType &sp,
83  DoFHandlerType & dh)
84  {
85  const parallel::Triangulation<DoFHandlerType::dimension,
86  DoFHandlerType::space_dimension>
87  *dist_tria = dynamic_cast<
88  const parallel::Triangulation<DoFHandlerType::dimension,
89  DoFHandlerType::space_dimension> *>(
90  &(dh.get_triangulation()));
91  MPI_Comm communicator =
92  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
93 
94  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
95  dh.locally_owned_mg_dofs(level),
96  sp,
97  communicator,
98  true);
99  }
100  };
101 
102  template <>
103  struct MatrixSelector<::TrilinosWrappers::MPI::Vector>
104  {
105  using Sparsity = ::TrilinosWrappers::SparsityPattern;
106  using Matrix = ::TrilinosWrappers::SparseMatrix;
107 
108  static const bool requires_distributed_sparsity_pattern = false;
109 
110  template <typename SparsityPatternType, typename DoFHandlerType>
111  static void
112  reinit(Matrix &matrix,
113  Sparsity &,
114  int level,
115  const SparsityPatternType &sp,
116  DoFHandlerType & dh)
117  {
118  const parallel::Triangulation<DoFHandlerType::dimension,
119  DoFHandlerType::space_dimension>
120  *dist_tria = dynamic_cast<
121  const parallel::Triangulation<DoFHandlerType::dimension,
122  DoFHandlerType::space_dimension> *>(
123  &(dh.get_triangulation()));
124  MPI_Comm communicator =
125  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
126  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
127  dh.locally_owned_mg_dofs(level),
128  sp,
129  communicator,
130  true);
131  }
132  };
133 
134 # ifdef DEAL_II_WITH_MPI
135 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
136  template <typename Number>
137  struct MatrixSelector<::LinearAlgebra::TpetraWrappers::Vector<Number>>
138  {
139  using Sparsity = ::TrilinosWrappers::SparsityPattern;
140  using Matrix = ::TrilinosWrappers::SparseMatrix;
141 
142  static const bool requires_distributed_sparsity_pattern = false;
143 
144  template <typename SparsityPatternType, typename DoFHandlerType>
145  static void
146  reinit(Matrix &matrix,
147  Sparsity &,
148  int level,
149  const SparsityPatternType &sp,
150  DoFHandlerType & dh)
151  {
152  const parallel::Triangulation<DoFHandlerType::dimension,
153  DoFHandlerType::space_dimension>
154  *dist_tria = dynamic_cast<
155  const parallel::Triangulation<DoFHandlerType::dimension,
156  DoFHandlerType::space_dimension> *>(
157  &(dh.get_triangulation()));
158  MPI_Comm communicator =
159  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
160  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
161  dh.locally_owned_mg_dofs(level),
162  sp,
163  communicator,
164  true);
165  }
166  };
167 # endif
168 
169  template <>
170  struct MatrixSelector<::LinearAlgebra::EpetraWrappers::Vector>
171  {
172  using Sparsity = ::TrilinosWrappers::SparsityPattern;
173  using Matrix = ::TrilinosWrappers::SparseMatrix;
174 
175  static const bool requires_distributed_sparsity_pattern = false;
176 
177  template <typename SparsityPatternType, typename DoFHandlerType>
178  static void
179  reinit(Matrix &matrix,
180  Sparsity &,
181  int level,
182  const SparsityPatternType &sp,
183  DoFHandlerType & dh)
184  {
185  const parallel::Triangulation<DoFHandlerType::dimension,
186  DoFHandlerType::space_dimension>
187  *dist_tria = dynamic_cast<
188  const parallel::Triangulation<DoFHandlerType::dimension,
189  DoFHandlerType::space_dimension> *>(
190  &(dh.get_triangulation()));
191  MPI_Comm communicator =
192  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
193  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
194  dh.locally_owned_mg_dofs(level),
195  sp,
196  communicator,
197  true);
198  }
199  };
200 # endif
201 
202 #else
203  // ! DEAL_II_WITH_TRILINOS
204  template <typename Number>
205  struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>>
206  {
207  using Sparsity = ::SparsityPattern;
208  using Matrix = ::SparseMatrix<Number>;
209 
210  static const bool requires_distributed_sparsity_pattern = false;
211 
212  template <typename SparsityPatternType, typename DoFHandlerType>
213  static void
214  reinit(Matrix &,
215  Sparsity &,
216  int,
217  const SparsityPatternType &,
218  const DoFHandlerType &)
219  {
220  AssertThrow(
221  false,
223  "ERROR: MGTransferPrebuilt with LinearAlgebra::distributed::Vector currently "
224  "needs deal.II to be configured with Trilinos."));
225  }
226  };
227 
228 #endif
229 
230 #ifdef DEAL_II_WITH_PETSC
231  template <>
232  struct MatrixSelector<::PETScWrappers::MPI::Vector>
233  {
234  using Sparsity = ::DynamicSparsityPattern;
235  using Matrix = ::PETScWrappers::MPI::SparseMatrix;
236 
237  static const bool requires_distributed_sparsity_pattern = true;
238 
239  template <typename SparsityPatternType, typename DoFHandlerType>
240  static void
241  reinit(Matrix &matrix,
242  Sparsity &,
243  int level,
244  const SparsityPatternType &sp,
245  const DoFHandlerType & dh)
246  {
247  const parallel::Triangulation<DoFHandlerType::dimension,
248  DoFHandlerType::space_dimension>
249  *dist_tria = dynamic_cast<
250  const parallel::Triangulation<DoFHandlerType::dimension,
251  DoFHandlerType::space_dimension> *>(
252  &(dh.get_triangulation()));
253  MPI_Comm communicator =
254  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
255  // Reinit PETSc matrix
256  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
257  dh.locally_owned_mg_dofs(level),
258  sp,
259  communicator);
260  }
261  };
262 #endif
263 } // namespace internal
264 
265 /*
266  * MGTransferBase is defined in mg_base.h
267  */
268 
271 
272 
273 
281 template <typename VectorType>
282 class MGLevelGlobalTransfer : public MGTransferBase<VectorType>
283 {
284 public:
288  void
289  clear();
290 
297  template <int dim, class InVector, int spacedim>
298  void
301  const InVector & src) const;
302 
310  template <int dim, class OutVector, int spacedim>
311  void
313  OutVector & dst,
314  const MGLevelObject<VectorType> &src) const;
315 
321  template <int dim, class OutVector, int spacedim>
322  void
324  OutVector & dst,
325  const MGLevelObject<VectorType> &src) const;
326 
342  void
343  set_component_to_block_map(const std::vector<unsigned int> &map);
344 
348  std::size_t
349  memory_consumption() const;
350 
354  void
355  print_indices(std::ostream &os) const;
356 
357 protected:
361  template <int dim, int spacedim>
362  void
364 
368  std::vector<types::global_dof_index> sizes;
369 
377  std::vector<
378  std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
380 
388  std::vector<
389  std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
391 
399  std::vector<
400  std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
402 
410 
415  std::vector<unsigned int> component_to_block_map;
416 
422 };
423 
424 
425 
437 template <typename Number>
439  : public MGTransferBase<LinearAlgebra::distributed::Vector<Number>>
440 {
441 public:
445  void
446  clear();
447 
454  template <int dim, typename Number2, int spacedim>
455  void
456  copy_to_mg(const DoFHandler<dim, spacedim> & mg_dof,
459 
467  template <int dim, typename Number2, int spacedim>
468  void
469  copy_from_mg(
470  const DoFHandler<dim, spacedim> & mg_dof,
473 
479  template <int dim, typename Number2, int spacedim>
480  void
482  const DoFHandler<dim, spacedim> & mg_dof,
485 
501  void
502  set_component_to_block_map(const std::vector<unsigned int> &map);
503 
507  std::size_t
508  memory_consumption() const;
509 
513  void
514  print_indices(std::ostream &os) const;
515 
516 protected:
521  template <int dim, typename Number2, int spacedim>
522  void
523  copy_to_mg(const DoFHandler<dim, spacedim> & mg_dof,
526  const bool solution_transfer) const;
527 
531  template <int dim, int spacedim>
532  void
534 
538  std::vector<types::global_dof_index> sizes;
539 
547  std::vector<std::vector<std::pair<unsigned int, unsigned int>>> copy_indices;
548 
549 
553  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
555 
563  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
565 
569  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
571 
579  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
581 
585  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
587 
595 
603 
608  std::vector<unsigned int> component_to_block_map;
609 
613  SmartPointer<
614  const MGConstrainedDoFs,
617 
624 
630 
637 
643 };
644 
645 
646 
660 template <typename VectorType>
661 class MGTransferPrebuilt : public MGLevelGlobalTransfer<VectorType>
662 {
663 public:
668  MGTransferPrebuilt() = default;
669 
675 
682  DEAL_II_DEPRECATED
685 
689  virtual ~MGTransferPrebuilt() override = default;
690 
694  void
696 
702  DEAL_II_DEPRECATED
703  void
706 
710  void
711  clear();
712 
716  template <int dim, int spacedim>
717  void
719 
731  virtual void
732  prolongate(const unsigned int to_level,
733  VectorType & dst,
734  const VectorType & src) const override;
735 
751  virtual void
752  restrict_and_add(const unsigned int from_level,
753  VectorType & dst,
754  const VectorType & src) const override;
755 
760 
765 
769  std::size_t
770  memory_consumption() const;
771 
775  void
776  print_matrices(std::ostream &os) const;
777 
778 private:
782  std::vector<
783  std::shared_ptr<typename internal::MatrixSelector<VectorType>::Sparsity>>
785 
791  std::vector<
792  std::shared_ptr<typename internal::MatrixSelector<VectorType>::Matrix>>
794 
799  std::vector<std::vector<bool>> interface_dofs;
800 };
801 
802 
806 DEAL_II_NAMESPACE_CLOSE
807 
808 #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::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices
Definition: mg_transfer.h:379
std::size_t memory_consumption() const
Contents is actually a matrix.
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices_global_mine
Definition: mg_transfer.h:564
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:629
void copy_to_mg(const DoFHandler< dim, spacedim > &mg_dof, MGLevelObject< VectorType > &dst, const InVector &src) const
#define AssertThrow(cond, exc)
Definition: exceptions.h:1519
std::vector< unsigned int > component_to_block_map
Definition: mg_transfer.h:415
void print_indices(std::ostream &os) const
LinearAlgebra::distributed::Vector< Number > ghosted_global_vector
Definition: mg_transfer.h:623
static ::ExceptionBase & ExcNoProlongation()
LinearAlgebra::distributed::Vector< Number > Vector
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices_global_mine
Definition: mg_transfer.h:570
virtual ~MGTransferPrebuilt() override=default
virtual void restrict_and_add(const unsigned int from_level, VectorType &dst, const VectorType &src) const override
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:401
void fill_and_communicate_copy_indices(const DoFHandler< dim, spacedim > &mg_dof)
virtual MPI_Comm get_communicator() const
Definition: tria_base.cc:155
#define DeclException0(Exception0)
Definition: exceptions.h:473
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_global_mine
Definition: mg_transfer.h:390
SmartPointer< const MGConstrainedDoFs, MGLevelGlobalTransfer< VectorType > > mg_constrained_dofs
Definition: mg_transfer.h:421
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices
Definition: mg_transfer.h:547
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Sparsity > > prolongation_sparsities
Definition: mg_transfer.h:784
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices_level_mine
Definition: mg_transfer.h:586
std::vector< types::global_dof_index > sizes
Definition: mg_transfer.h:368
virtual void prolongate(const unsigned int to_level, VectorType &dst, const VectorType &src) const override
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices
Definition: mg_transfer.h:554
SmartPointer< const MGConstrainedDoFs, MGLevelGlobalTransfer< LinearAlgebra::distributed::Vector< Number > > > mg_constrained_dofs
Definition: mg_transfer.h:616
static ::ExceptionBase & ExcMatricesNotBuilt()
MGLevelObject< LinearAlgebra::distributed::Vector< Number > > solution_ghosted_level_vector
Definition: mg_transfer.h:642
static ::ExceptionBase & ExcNotImplemented()
std::vector< std::vector< bool > > interface_dofs
Definition: mg_transfer.h:799
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Matrix > > prolongation_matrices
Definition: mg_transfer.h:793
void set_component_to_block_map(const std::vector< unsigned int > &map)
MGLevelObject< LinearAlgebra::distributed::Vector< Number > > ghosted_level_vector
Definition: mg_transfer.h:636
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices_level_mine
Definition: mg_transfer.h:580