Reference documentation for deal.II version 9.1.0-pre
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.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  template <>
136  struct MatrixSelector<::LinearAlgebra::EpetraWrappers::Vector>
137  {
138  using Sparsity = ::TrilinosWrappers::SparsityPattern;
139  using Matrix = ::TrilinosWrappers::SparseMatrix;
140 
141  static const bool requires_distributed_sparsity_pattern = false;
142 
143  template <typename SparsityPatternType, typename DoFHandlerType>
144  static void
145  reinit(Matrix &matrix,
146  Sparsity &,
147  int level,
148  const SparsityPatternType &sp,
149  DoFHandlerType & dh)
150  {
151  const parallel::Triangulation<DoFHandlerType::dimension,
152  DoFHandlerType::space_dimension>
153  *dist_tria = dynamic_cast<
154  const parallel::Triangulation<DoFHandlerType::dimension,
155  DoFHandlerType::space_dimension> *>(
156  &(dh.get_triangulation()));
157  MPI_Comm communicator =
158  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
159  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
160  dh.locally_owned_mg_dofs(level),
161  sp,
162  communicator,
163  true);
164  }
165  };
166 # endif
167 
168 #else
169  // ! DEAL_II_WITH_TRILINOS
170  template <typename Number>
171  struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>>
172  {
173  using Sparsity = ::SparsityPattern;
174  using Matrix = ::SparseMatrix<Number>;
175 
176  static const bool requires_distributed_sparsity_pattern = false;
177 
178  template <typename SparsityPatternType, typename DoFHandlerType>
179  static void
180  reinit(Matrix &,
181  Sparsity &,
182  int,
183  const SparsityPatternType &,
184  const DoFHandlerType &)
185  {
186  AssertThrow(
187  false,
189  "ERROR: MGTransferPrebuilt with LinearAlgebra::distributed::Vector currently "
190  "needs deal.II to be configured with Trilinos."));
191  }
192  };
193 
194 #endif
195 
196 #ifdef DEAL_II_WITH_PETSC
197  template <>
198  struct MatrixSelector<::PETScWrappers::MPI::Vector>
199  {
200  using Sparsity = ::DynamicSparsityPattern;
201  using Matrix = ::PETScWrappers::MPI::SparseMatrix;
202 
203  static const bool requires_distributed_sparsity_pattern = true;
204 
205  template <typename SparsityPatternType, typename DoFHandlerType>
206  static void
207  reinit(Matrix &matrix,
208  Sparsity &,
209  int level,
210  const SparsityPatternType &sp,
211  const DoFHandlerType & dh)
212  {
213  const parallel::Triangulation<DoFHandlerType::dimension,
214  DoFHandlerType::space_dimension>
215  *dist_tria = dynamic_cast<
216  const parallel::Triangulation<DoFHandlerType::dimension,
217  DoFHandlerType::space_dimension> *>(
218  &(dh.get_triangulation()));
219  MPI_Comm communicator =
220  dist_tria != nullptr ? dist_tria->get_communicator() : MPI_COMM_SELF;
221  // Reinit PETSc matrix
222  matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
223  dh.locally_owned_mg_dofs(level),
224  sp,
225  communicator);
226  }
227  };
228 #endif
229 } // namespace internal
230 
231 /*
232  * MGTransferBase is defined in mg_base.h
233  */
234 
237 
238 
239 
247 template <typename VectorType>
248 class MGLevelGlobalTransfer : public MGTransferBase<VectorType>
249 {
250 public:
254  void
255  clear();
256 
263  template <int dim, class InVector, int spacedim>
264  void
265  copy_to_mg(const DoFHandler<dim, spacedim> &mg_dof,
267  const InVector & src) const;
268 
276  template <int dim, class OutVector, int spacedim>
277  void
278  copy_from_mg(const DoFHandler<dim, spacedim> &mg_dof,
279  OutVector & dst,
280  const MGLevelObject<VectorType> &src) const;
281 
287  template <int dim, class OutVector, int spacedim>
288  void
289  copy_from_mg_add(const DoFHandler<dim, spacedim> &mg_dof,
290  OutVector & dst,
291  const MGLevelObject<VectorType> &src) const;
292 
308  void
309  set_component_to_block_map(const std::vector<unsigned int> &map);
310 
314  std::size_t
315  memory_consumption() const;
316 
320  void
321  print_indices(std::ostream &os) const;
322 
323 protected:
327  template <int dim, int spacedim>
328  void
329  fill_and_communicate_copy_indices(const DoFHandler<dim, spacedim> &mg_dof);
330 
334  std::vector<types::global_dof_index> sizes;
335 
343  std::vector<
344  std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
346 
354  std::vector<
355  std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
357 
365  std::vector<
366  std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
368 
376 
381  std::vector<unsigned int> component_to_block_map;
382 
388 };
389 
390 
391 
403 template <typename Number>
404 class MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>>
405  : public MGTransferBase<LinearAlgebra::distributed::Vector<Number>>
406 {
407 public:
411  void
412  clear();
413 
420  template <int dim, typename Number2, int spacedim>
421  void
422  copy_to_mg(const DoFHandler<dim, spacedim> & mg_dof,
425 
433  template <int dim, typename Number2, int spacedim>
434  void
435  copy_from_mg(
436  const DoFHandler<dim, spacedim> & mg_dof,
439 
445  template <int dim, typename Number2, int spacedim>
446  void
447  copy_from_mg_add(
448  const DoFHandler<dim, spacedim> & mg_dof,
451 
467  void
468  set_component_to_block_map(const std::vector<unsigned int> &map);
469 
473  std::size_t
474  memory_consumption() const;
475 
479  void
480  print_indices(std::ostream &os) const;
481 
482 protected:
487  template <int dim, typename Number2, int spacedim>
488  void
489  copy_to_mg(const DoFHandler<dim, spacedim> & mg_dof,
492  const bool solution_transfer) const;
493 
497  template <int dim, int spacedim>
498  void
499  fill_and_communicate_copy_indices(const DoFHandler<dim, spacedim> &mg_dof);
500 
504  std::vector<types::global_dof_index> sizes;
505 
513  std::vector<std::vector<std::pair<unsigned int, unsigned int>>> copy_indices;
514 
515 
519  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
521 
529  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
531 
535  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
537 
545  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
547 
551  std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
553 
561 
569 
574  std::vector<unsigned int> component_to_block_map;
575 
579  SmartPointer<
580  const MGConstrainedDoFs,
583 
590 
596 
603 
609 };
610 
611 
612 
626 template <typename VectorType>
627 class MGTransferPrebuilt : public MGLevelGlobalTransfer<VectorType>
628 {
629 public:
634  MGTransferPrebuilt() = default;
635 
640  MGTransferPrebuilt(const MGConstrainedDoFs &mg_constrained_dofs);
641 
648  DEAL_II_DEPRECATED
650  const MGConstrainedDoFs & mg_constrained_dofs);
651 
655  virtual ~MGTransferPrebuilt() override = default;
656 
660  void
661  initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs);
662 
668  DEAL_II_DEPRECATED
669  void
670  initialize_constraints(const AffineConstraints<double> &constraints,
671  const MGConstrainedDoFs & mg_constrained_dofs);
672 
676  void
677  clear();
678 
682  template <int dim, int spacedim>
683  void
684  build_matrices(const DoFHandler<dim, spacedim> &mg_dof);
685 
697  virtual void
698  prolongate(const unsigned int to_level,
699  VectorType & dst,
700  const VectorType & src) const override;
701 
717  virtual void
718  restrict_and_add(const unsigned int from_level,
719  VectorType & dst,
720  const VectorType & src) const override;
721 
725  DeclException0(ExcNoProlongation);
726 
730  DeclException0(ExcMatricesNotBuilt);
731 
735  std::size_t
736  memory_consumption() const;
737 
741  void
742  print_matrices(std::ostream &os) const;
743 
744 private:
748  std::vector<
749  std::shared_ptr<typename internal::MatrixSelector<VectorType>::Sparsity>>
751 
757  std::vector<
758  std::shared_ptr<typename internal::MatrixSelector<VectorType>::Matrix>>
760 
765  std::vector<std::vector<bool>> interface_dofs;
766 };
767 
768 
772 DEAL_II_NAMESPACE_CLOSE
773 
774 #endif
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices
Definition: mg_transfer.h:345
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices_global_mine
Definition: mg_transfer.h:530
LinearAlgebra::distributed::Vector< Number > solution_ghosted_global_vector
Definition: mg_transfer.h:595
#define AssertThrow(cond, exc)
Definition: exceptions.h:1329
std::vector< unsigned int > component_to_block_map
Definition: mg_transfer.h:381
LinearAlgebra::distributed::Vector< Number > ghosted_global_vector
Definition: mg_transfer.h:589
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices_global_mine
Definition: mg_transfer.h:536
virtual MPI_Comm get_communicator() const
Definition: tria_base.cc:155
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_level_mine
Definition: mg_transfer.h:367
#define DeclException0(Exception0)
Definition: exceptions.h:385
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_global_mine
Definition: mg_transfer.h:356
SmartPointer< const MGConstrainedDoFs, MGLevelGlobalTransfer< VectorType > > mg_constrained_dofs
Definition: mg_transfer.h:387
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices
Definition: mg_transfer.h:513
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Sparsity > > prolongation_sparsities
Definition: mg_transfer.h:750
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices_level_mine
Definition: mg_transfer.h:552
std::vector< types::global_dof_index > sizes
Definition: mg_transfer.h:334
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > solution_copy_indices
Definition: mg_transfer.h:520
SmartPointer< const MGConstrainedDoFs, MGLevelGlobalTransfer< LinearAlgebra::distributed::Vector< Number > > > mg_constrained_dofs
Definition: mg_transfer.h:582
MGLevelObject< LinearAlgebra::distributed::Vector< Number > > solution_ghosted_level_vector
Definition: mg_transfer.h:608
static::ExceptionBase & ExcNotImplemented()
std::vector< std::vector< bool > > interface_dofs
Definition: mg_transfer.h:765
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Matrix > > prolongation_matrices
Definition: mg_transfer.h:759
MGLevelObject< LinearAlgebra::distributed::Vector< Number > > ghosted_level_vector
Definition: mg_transfer.h:602
std::vector< std::vector< std::pair< unsigned int, unsigned int > > > copy_indices_level_mine
Definition: mg_transfer.h:546