deal.II version GIT relicensing-7058-gb35ee29762 2026-11-11 09:30: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
280 template <int dim, class InVector, int spacedim>
281 void
284 const InVector &src) const;
285
293 template <int dim, class OutVector, int spacedim>
294 void
296 OutVector &dst,
297 const MGLevelObject<VectorType> &src) const;
298
304 template <int dim, class OutVector, int spacedim>
305 void
307 OutVector &dst,
308 const MGLevelObject<VectorType> &src) const;
309
325 void
326 set_component_to_block_map(const std::vector<unsigned int> &map);
327
331 std::size_t
332 memory_consumption() const;
333
337 void
338 print_indices(std::ostream &os) const;
339
340protected:
344 template <int dim, int spacedim>
345 void
347 const DoFHandler<dim, spacedim> &dof_handler);
348
352 std::vector<types::global_dof_index> sizes;
353
361 std::vector<
362 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
364
372 std::vector<
373 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
375
383 std::vector<
384 std::vector<std::pair<types::global_dof_index, types::global_dof_index>>>
386
394
399 std::vector<unsigned int> component_to_block_map;
400
405
406private:
410 template <int dim, int spacedim>
411 void
412 assert_built(const DoFHandler<dim, spacedim> &dof_handler) const;
413};
414
415
416
425template <typename Number, typename MemorySpace>
427 LinearAlgebra::distributed::Vector<Number, MemorySpace>>
428 : public MGTransferBase<
429 LinearAlgebra::distributed::Vector<Number, MemorySpace>>
430{
431public:
435 void
436 clear();
437
444 template <int dim, typename Number2, int spacedim>
445 void
447 const DoFHandler<dim, spacedim> &dof_handler,
450
458 template <int dim, typename Number2, int spacedim>
459 void
461 const DoFHandler<dim, spacedim> &dof_handler,
464 &src) const;
465
471 template <int dim, typename Number2, int spacedim>
472 void
474 const DoFHandler<dim, spacedim> &dof_handler,
477 &src) const;
478
494 void
495 set_component_to_block_map(const std::vector<unsigned int> &map);
496
500 std::size_t
501 memory_consumption() const;
502
506 void
507 print_indices(std::ostream &os) const;
508
509protected:
514 template <int dim, typename Number2, int spacedim>
515 void
517 const DoFHandler<dim, spacedim> &dof_handler,
520 const bool solution_transfer) const;
521
525 template <int dim, int spacedim>
526 void
528 const DoFHandler<dim, spacedim> &dof_handler);
529
533 std::vector<types::global_dof_index> sizes;
534
543 std::vector<Table<2, unsigned int>> copy_indices;
544
548 std::vector<Table<2, unsigned int>> solution_copy_indices;
549
557 std::vector<Table<2, unsigned int>> copy_indices_global_mine;
558
566 std::vector<Table<2, unsigned int>> copy_indices_level_mine;
567
571 std::vector<Table<2, unsigned int>> solution_copy_indices_level_mine;
572
580
588
593 std::vector<unsigned int> component_to_block_map;
594
599
607
613
620
624 std::function<void(const unsigned int,
627
628private:
632 template <int dim, int spacedim>
633 void
634 assert_built(const DoFHandler<dim, spacedim> &dof_handler) const;
635};
636
637
638
649template <typename VectorType>
651{
652public:
658
664
668 virtual ~MGTransferPrebuilt() override = default;
669
673 void
675
679 void
680 clear();
681
686 template <int dim, int spacedim>
687 void
688 build(const DoFHandler<dim, spacedim> &dof_handler);
689
701 virtual void
702 prolongate(const unsigned int to_level,
703 VectorType &dst,
704 const VectorType &src) const override;
705
721 virtual void
722 restrict_and_add(const unsigned int from_level,
723 VectorType &dst,
724 const VectorType &src) const override;
725
730
735
739 std::size_t
740 memory_consumption() const;
741
745 void
746 print_matrices(std::ostream &os) const;
747
748private:
752 std::vector<
753 std::shared_ptr<typename internal::MatrixSelector<VectorType>::Sparsity>>
755
761 std::vector<
762 std::shared_ptr<typename internal::MatrixSelector<VectorType>::Matrix>>
764
769 std::vector<std::vector<bool>> interface_dofs;
770};
771
772
777
778#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