deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
mg_transfer.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2001 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_mg_transfer_h
14#define dealii_mg_transfer_h
15
16#include <deal.II/base/config.h>
17
19
21
23
34
37
38#include <memory>
39
40
42
43
44namespace internal
45{
46 template <typename VectorType>
48 {
51
52 static const bool requires_distributed_sparsity_pattern = false;
53
54 template <typename SparsityPatternType, int dim, int spacedim>
55 static void
56 reinit(Matrix &matrix,
57 Sparsity &sparsity,
58 int level,
59 const SparsityPatternType &sp,
61 {
62 sparsity.copy_from(sp);
63 (void)level;
64 matrix.reinit(sparsity);
65 }
66 };
67
68#ifdef DEAL_II_WITH_TRILINOS
69# ifdef DEAL_II_TRILINOS_WITH_EPETRA
70 template <typename Number>
71 struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>>
72 {
75
76 static const bool requires_distributed_sparsity_pattern = false;
77
78 template <typename SparsityPatternType, int dim, int spacedim>
79 static void
80 reinit(Matrix &matrix,
81 Sparsity &,
82 int level,
83 const SparsityPatternType &sp,
85 {
86 const MPI_Comm communicator = dh.get_mpi_communicator();
87
88 matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
90 sp,
91 communicator,
92 true);
93 }
94 };
95
96 template <>
98 {
101
102 static const bool requires_distributed_sparsity_pattern = false;
103
104 template <typename SparsityPatternType, int dim, int spacedim>
105 static void
106 reinit(Matrix &matrix,
107 Sparsity &,
108 int level,
109 const SparsityPatternType &sp,
111 {
112 const MPI_Comm communicator = dh.get_mpi_communicator();
113
114 matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
116 sp,
117 communicator,
118 true);
119 }
120 };
121# endif
122
123# ifdef DEAL_II_WITH_MPI
124# ifdef DEAL_II_TRILINOS_WITH_TPETRA
125 template <typename Number, typename MemorySpace>
127 ::LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>>
128 {
129 using Sparsity =
131 using Matrix =
134
135 static const bool requires_distributed_sparsity_pattern = false;
136
137 template <typename SparsityPatternType, int dim, int spacedim>
138 static void
139 reinit(Matrix &matrix,
140 Sparsity &,
141 int level,
142 const SparsityPatternType &sp,
144 {
145 const MPI_Comm communicator = dh.get_mpi_communicator();
146
147 matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
149 sp,
150 communicator,
151 true);
152 }
153 };
154# endif
155
156# ifdef DEAL_II_TRILINOS_WITH_EPETRA
157 template <>
159 {
162
163 static const bool requires_distributed_sparsity_pattern = false;
164
165 template <typename SparsityPatternType, int dim, int spacedim>
166 static void
167 reinit(Matrix &matrix,
168 Sparsity &,
169 int level,
170 const SparsityPatternType &sp,
172 {
173 const MPI_Comm communicator = dh.get_mpi_communicator();
174
175 matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
177 sp,
178 communicator,
179 true);
180 }
181 };
182# endif
183# endif
184
185#else
186 // ! DEAL_II_WITH_TRILINOS
187 template <typename Number>
188 struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>>
189 {
192
193 static const bool requires_distributed_sparsity_pattern = false;
194
195 template <typename SparsityPatternType, int dim, int spacedim>
196 static void
197 reinit(Matrix &,
198 Sparsity &,
199 int,
200 const SparsityPatternType &,
202 {
204 false,
206 "ERROR: MGTransferPrebuilt with LinearAlgebra::distributed::Vector currently "
207 "needs deal.II to be configured with Trilinos."));
208 }
209 };
210
211#endif
212
213#ifdef DEAL_II_WITH_PETSC
214 template <>
216 {
219
221
222 template <typename SparsityPatternType, int dim, int spacedim>
223 static void
224 reinit(Matrix &matrix,
225 Sparsity &,
226 int level,
227 const SparsityPatternType &sp,
229 {
230 const MPI_Comm communicator = dh.get_mpi_communicator();
231
232 // Reinit PETSc matrix
233 matrix.reinit(dh.locally_owned_mg_dofs(level + 1),
235 sp,
236 communicator);
237 }
238 };
239#endif
240} // namespace internal
241
242/*
243 * MGTransferBase is defined in mg_base.h
244 */
245
255template <typename VectorType>
256class MGLevelGlobalTransfer : public MGTransferBase<VectorType>
257{
258public:
262 void
263 clear();
264
271 template <int dim, class InVector, int spacedim>
272 void
275 const InVector &src) const;
276
284 template <int dim, class OutVector, int spacedim>
285 void
287 OutVector &dst,
288 const MGLevelObject<VectorType> &src) const;
289
295 template <int dim, class OutVector, int spacedim>
296 void
298 OutVector &dst,
299 const MGLevelObject<VectorType> &src) const;
300
316 void
317 set_component_to_block_map(const std::vector<unsigned int> &map);
318
322 std::size_t
323 memory_consumption() const;
324
328 void
329 print_indices(std::ostream &os) const;
330
331protected:
335 template <int dim, int spacedim>
336 void
338 const DoFHandler<dim, spacedim> &dof_handler);
339
343 std::vector<types::global_dof_index> sizes;
344
352 std::vector<
353 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
355
363 std::vector<
364 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
366
374 std::vector<
375 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
377
385
390 std::vector<unsigned int> component_to_block_map;
391
396
397private:
401 template <int dim, int spacedim>
402 void
403 assert_built(const DoFHandler<dim, spacedim> &dof_handler) const;
404};
405
406
407
416template <typename Number, typename MemorySpace>
418 LinearAlgebra::distributed::Vector<Number, MemorySpace>>
419 : public MGTransferBase<
420 LinearAlgebra::distributed::Vector<Number, MemorySpace>>
421{
422public:
426 void
427 clear();
428
435 template <int dim, typename Number2, int spacedim>
436 void
438 const DoFHandler<dim, spacedim> &dof_handler,
441
449 template <int dim, typename Number2, int spacedim>
450 void
452 const DoFHandler<dim, spacedim> &dof_handler,
455 &src) const;
456
462 template <int dim, typename Number2, int spacedim>
463 void
465 const DoFHandler<dim, spacedim> &dof_handler,
468 &src) const;
469
485 void
486 set_component_to_block_map(const std::vector<unsigned int> &map);
487
491 std::size_t
492 memory_consumption() const;
493
497 void
498 print_indices(std::ostream &os) const;
499
500protected:
505 template <int dim, typename Number2, int spacedim>
506 void
508 const DoFHandler<dim, spacedim> &dof_handler,
511 const bool solution_transfer) const;
512
516 template <int dim, int spacedim>
517 void
519 const DoFHandler<dim, spacedim> &dof_handler);
520
524 std::vector<types::global_dof_index> sizes;
525
534 std::vector<Table<2, unsigned int>> copy_indices;
535
539 std::vector<Table<2, unsigned int>> solution_copy_indices;
540
548 std::vector<Table<2, unsigned int>> copy_indices_global_mine;
549
557 std::vector<Table<2, unsigned int>> copy_indices_level_mine;
558
562 std::vector<Table<2, unsigned int>> solution_copy_indices_level_mine;
563
571
579
584 std::vector<unsigned int> component_to_block_map;
585
590
598
604
611
615 std::function<void(const unsigned int,
618
619private:
623 template <int dim, int spacedim>
624 void
625 assert_built(const DoFHandler<dim, spacedim> &dof_handler) const;
626};
627
628
629
640template <typename VectorType>
642{
643public:
649
655
659 virtual ~MGTransferPrebuilt() override = default;
660
664 void
666
670 void
671 clear();
672
677 template <int dim, int spacedim>
678 void
679 build(const DoFHandler<dim, spacedim> &dof_handler);
680
692 virtual void
693 prolongate(const unsigned int to_level,
694 VectorType &dst,
695 const VectorType &src) const override;
696
712 virtual void
713 restrict_and_add(const unsigned int from_level,
714 VectorType &dst,
715 const VectorType &src) const override;
716
721
726
730 std::size_t
731 memory_consumption() const;
732
736 void
737 print_matrices(std::ostream &os) const;
738
739private:
743 std::vector<
744 std::shared_ptr<typename internal::MatrixSelector<VectorType>::Sparsity>>
746
752 std::vector<
753 std::shared_ptr<typename internal::MatrixSelector<VectorType>::Matrix>>
755
760 std::vector<std::vector<bool>> interface_dofs;
761};
762
763
768
769#endif
const IndexSet & locally_owned_mg_dofs(const unsigned int level) const
MPI_Comm get_mpi_communicator() const
void assert_built(const DoFHandler< dim, spacedim > &dof_handler) const
LinearAlgebra::distributed::Vector< Number, MemorySpace > ghosted_global_vector
std::function< void(const unsigned int, LinearAlgebra::distributed::Vector< Number, MemorySpace > &)> initialize_dof_vector
void copy_to_mg(const DoFHandler< dim, spacedim > &dof_handler, MGLevelObject< LinearAlgebra::distributed::Vector< Number, MemorySpace > > &dst, const LinearAlgebra::distributed::Vector< Number2, MemorySpace > &src) const
LinearAlgebra::distributed::Vector< Number, MemorySpace > solution_ghosted_global_vector
void copy_to_mg(const DoFHandler< dim, spacedim > &dof_handler, MGLevelObject< LinearAlgebra::distributed::Vector< Number, MemorySpace > > &dst, const LinearAlgebra::distributed::Vector< Number2, MemorySpace > &src, const bool solution_transfer) const
void copy_from_mg(const DoFHandler< dim, spacedim > &dof_handler, LinearAlgebra::distributed::Vector< Number2, MemorySpace > &dst, const MGLevelObject< LinearAlgebra::distributed::Vector< Number, MemorySpace > > &src) const
void copy_from_mg_add(const DoFHandler< dim, spacedim > &dof_handler, LinearAlgebra::distributed::Vector< Number2, MemorySpace > &dst, const MGLevelObject< LinearAlgebra::distributed::Vector< Number, MemorySpace > > &src) const
MGLevelObject< LinearAlgebra::distributed::Vector< Number, MemorySpace > > ghosted_level_vector
void print_indices(std::ostream &os) const
void assert_built(const DoFHandler< dim, spacedim > &dof_handler) const
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_level_mine
std::size_t memory_consumption() const
void set_component_to_block_map(const std::vector< unsigned int > &map)
ObserverPointer< const MGConstrainedDoFs > mg_constrained_dofs
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices
void fill_and_communicate_copy_indices(const DoFHandler< dim, spacedim > &dof_handler)
std::vector< unsigned int > component_to_block_map
void copy_from_mg_add(const DoFHandler< dim, spacedim > &dof_handler, OutVector &dst, const MGLevelObject< VectorType > &src) const
std::vector< types::global_dof_index > sizes
std::vector< std::vector< std::pair< types::global_dof_index, types::global_dof_index > > > copy_indices_global_mine
void copy_from_mg(const DoFHandler< dim, spacedim > &dof_handler, OutVector &dst, const MGLevelObject< VectorType > &src) const
void copy_to_mg(const DoFHandler< dim, spacedim > &dof_handler, MGLevelObject< VectorType > &dst, const InVector &src) const
virtual ~MGTransferPrebuilt() override=default
std::vector< std::vector< bool > > interface_dofs
virtual void restrict_and_add(const unsigned int from_level, VectorType &dst, const VectorType &src) const override
void build(const DoFHandler< dim, spacedim > &dof_handler)
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Sparsity > > prolongation_sparsities
std::size_t memory_consumption() const
MGTransferPrebuilt()=default
virtual void prolongate(const unsigned int to_level, VectorType &dst, const VectorType &src) const override
std::vector< std::shared_ptr< typename internal::MatrixSelector< VectorType >::Matrix > > prolongation_matrices
void print_matrices(std::ostream &os) const
void initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs)
void copy_from(const size_type n_rows, const size_type n_cols, const ForwardIterator begin, const ForwardIterator end)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
unsigned int level
Definition grid_out.cc:4642
#define DeclException0(Exception0)
static ::ExceptionBase & ExcMatricesNotBuilt()
static ::ExceptionBase & ExcNoProlongation()
static ::ExceptionBase & ExcNotImplemented()
#define AssertThrow(cond, exc)
static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, const DoFHandler< dim, spacedim > &dh)
Definition mg_transfer.h:80
static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, const DoFHandler< dim, spacedim > &dh)
static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, const DoFHandler< dim, spacedim > &dh)
static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, const DoFHandler< dim, spacedim > &dh)
static void reinit(Matrix &matrix, Sparsity &, int level, const SparsityPatternType &sp, const DoFHandler< dim, spacedim > &dh)
static const bool requires_distributed_sparsity_pattern
Definition mg_transfer.h:52
static void reinit(Matrix &matrix, Sparsity &sparsity, int level, const SparsityPatternType &sp, const DoFHandler< dim, spacedim > &)
Definition mg_transfer.h:56
::SparseMatrix< typename VectorType::value_type > Matrix
Definition mg_transfer.h:50
::SparsityPattern Sparsity
Definition mg_transfer.h:49