Debian Codegraph @

include/deal.II/multigrid/mg_transfer_global_coarsening.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_mg_transfer_global_coarsening_h
16 #define dealii_mg_transfer_global_coarsening_h
17
18 #include <deal.II/base/memory_space.h>
19 #include <deal.II/base/mg_level_object.h>
20 #include <deal.II/base/mpi_remote_point_evaluation.h>
21 #include <deal.II/base/vectorization.h>
22
23 #include <deal.II/dofs/dof_handler.h>
24
25 #include <deal.II/lac/affine_constraints.h>
26 #include <deal.II/lac/la_parallel_vector.h>
27
28 #include <deal.II/matrix_free/constraint_info.h>
29 #include <deal.II/matrix_free/matrix_free.h>
30 #include <deal.II/matrix_free/shape_info.h>
31
32 #include <deal.II/multigrid/mg_base.h>
33 #include <deal.II/multigrid/mg_transfer_matrix_free.h>
34
35 #include <deal.II/non_matching/mapping_info.h>
36
37
38
39 DEAL_II_NAMESPACE_OPEN
40
41 // Forward declarations
42 #ifndef DOXYGEN
43 namespace internal
44 {
45 class MGTwoLevelTransferImplementation;
46 }
47
48 namespace RepartitioningPolicyTools
49 {
50 template <int dim, int spacedim>
51 class Base;
52 }
53
54 template <int dim,
55 typename Number,
56 typename MemorySpace = ::dealii::MemorySpace::Host>
57 class MGTransferMF;
58 #endif
59
60
61 /**
62 * @addtogroup mg
63 * @{
64 */
65
66 namespace mg
67 {
68 /**
69 * A structure with boost::signal objects for optional processing in a
70 * non-nested multigrid solver.
71 *
72 * Similarly to mg::Signals, each signal is called twice: once before and
73 * once after the action is performed. The two calls only differ in the
74 * booleanargument @p before, which is true the first time and false the
75 * second.
76 *
77 */
78 struct SignalsNonNested
79 {
80 /**
81 * This signal is triggered before and after the call to the actual
82 * evaluation function inside RemotePointEvaluation::evaluate_and_process()
83 * during prolongation.
84 */
85 boost::signals2::signal<void(const bool before)> prolongation_cell_loop;
86
87 /**
88 * This signal is triggered before and after the call to the actual
89 * evaluation function inside RemotePointEvaluation::process_and_evaluate()
90 * during restriction.
91 */
92 boost::signals2::signal<void(const bool before)> restriction_cell_loop;
93
94 /**
95 * This signal is triggered before and after the call to
96 * RemotePointEvaluation::evaluate_and_process() used in
97 * MGTwoLevelTransferNonNested::prolongate_and_add(). The difference
98 * with the @p prolongation_cell_loop signal is that also the
99 * communication phase is included.
100 */
101 boost::signals2::signal<void(const bool before)> prolongation;
102
103 /**
104 * This signal is triggered before and after the call to
105 * RemotePointEvaluation::process_and_evaluate() used in
106 * MGTwoLevelTransferNonNested::restrict_and_add(). Similarly to
107 * the @p restriction_cell_loop signal, also the communication phase is
108 * included.
109 */
110 boost::signals2::signal<void(const bool before)> restriction;
111 };
112 } // namespace mg
113
114 /**
115 * Global coarsening utility functions.
116 */
117 namespace MGTransferGlobalCoarseningTools
118 {
119 /**
120 * Common polynomial coarsening sequences.
121 *
122 * @note These polynomial coarsening sequences up to a degree of 9 are
123 * precompiled in MGTwoLevelTransfer. See also:
124 * MGTwoLevelTransfer::fast_polynomial_transfer_supported()
125 */
126 enum class PolynomialCoarseningSequenceType
127 {
128 /**
129 * Half polynomial degree by integer division. For example, for degree=7
130 * the following sequence would be obtained:: 7 -> 3 -> 1
131 */
132 bisect,
133 /**
134 * Decrease the polynomial degree by one. E.g., for degree=7 following
135 * sequence would result: 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1
136 */
137 decrease_by_one,
138 /**
139 * Decrease the polynomial degree to one. E.g., for degree=7 following
140 * sequence would result: 7 -> 1
141 */
142 go_to_one
143 };
144
145 /**
146 * For a given @p degree and polynomial coarsening sequence @p p_sequence,
147 * determine the next coarser degree.
148 */
149 unsigned int
150 create_next_polynomial_coarsening_degree(
151 const unsigned int degree,
152 const PolynomialCoarseningSequenceType &p_sequence);
153
154 /**
155 * For a given @p max_degree and polynomial coarsening sequence @p p_sequence,
156 * determine the full sequence of polynomial degrees, sorted in ascending
157 * order.
158 */
159 std::vector<unsigned int>
160 create_polynomial_coarsening_sequence(
161 const unsigned int max_degree,
162 const PolynomialCoarseningSequenceType &p_sequence);
163
164 /**
165 * For a given triangulation @p tria, determine the geometric coarsening
166 * sequence by repeated global coarsening of the provided triangulation.
167 *
168 * @note For convenience, a reference to the input triangulation is stored in
169 * the last entry of the return vector.
170 * @note Currently, not implemented for parallel::fullydistributed::Triangulation.
171 * @note The type of the returned triangulations is the same as of the input
172 * triangulation.
173 */
174 template <int dim, int spacedim>
175 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
176 create_geometric_coarsening_sequence(
177 const Triangulation<dim, spacedim> &tria);
178
179 /**
180 * Similar to the above function but also taking a @p policy for
181 * repartitioning the triangulations on the coarser levels. If
182 * @p preserve_fine_triangulation is set, the input triangulation is not
183 * altered,
184 * else the triangulation is coarsened. If @p repartition_fine_triangulation
185 * is set, the triangulation on the finest level is repartitioned as well. If
186 * the flags are set to true/false, the input triangulation is simply used as
187 * the finest triangulation.
188 *
189 * @note For convenience, a reference to the input triangulation is stored in
190 * the last entry of the return vector.
191 * @note The type of the returned triangulations is
192 * parallel::fullydistributed::Triangulation.
193 * @note Currently, only implemented for parallel::distributed::Triangulation.
194 */
195 template <int dim, int spacedim>
196 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
197 create_geometric_coarsening_sequence(
198 Triangulation<dim, spacedim> &tria,
199 const RepartitioningPolicyTools::Base<dim, spacedim> &policy,
200 const bool preserve_fine_triangulation,
201 const bool repartition_fine_triangulation);
202
203 /**
204 * Similar to the above function but taking in a constant version of
205 * @p tria. As a consequence, it can not be used for coarsening directly,
206 * so a temporary copy will be created internally.
207 */
208 template <int dim, int spacedim>
209 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
210 create_geometric_coarsening_sequence(
211 const Triangulation<dim, spacedim> &tria,
212 const RepartitioningPolicyTools::Base<dim, spacedim> &policy,
213 const bool repartition_fine_triangulation = false);
214
215 } // namespace MGTransferGlobalCoarseningTools
216
217
218
219 /**
220 * An abstract base class for transfer operators between two multigrid levels.
221 * The implementation of
222 * restriction and prolongation between levels is delegated to derived classes,
223 * which implement prolongate_and_add_internal() and restrict_and_add_internal()
224 * accordingly.
225 */
226 template <typename VectorType>
227 class MGTwoLevelTransferBase : public EnableObserverPointer
228 {
229 public:
230 static_assert(
231 std::is_same_v<
232 VectorType,
233 LinearAlgebra::distributed::Vector<typename VectorType::value_type,
234 MemorySpace::Host>> ||
235 std::is_same_v<
236 VectorType,
237 LinearAlgebra::distributed::Vector<typename VectorType::value_type,
238 MemorySpace::Default>>,
239 "This class is currently only implemented for vectors of "
240 "type LinearAlgebra::distributed::Vector.");
241
242 /**
243 * The scalar type used by the vector-type template argument.
244 */
245 using Number = typename VectorType::value_type;
246
247 /**
248 * Default constructor.
249 */
250 MGTwoLevelTransferBase();
251
252 /**
253 * Perform prolongation on a solution vector.
254 */
255 void
256 prolongate_and_add(VectorType &dst, const VectorType &src) const;
257
258 /**
259 * Perform restriction on a residual vector.
260 */
261 void
262 restrict_and_add(VectorType &dst, const VectorType &src) const;
263
264 /**
265 * Perform interpolation of a solution vector from the fine level to the
266 * coarse level. This function is different from restriction, where a
267 * weighted residual is transferred to a coarser level (transposition of
268 * prolongation matrix). In other words, restriction acts on right hand
269 * side vectors, whereas interpolation acts on solution vectors.
270 */
271 virtual void
272 interpolate(VectorType &dst, const VectorType &src) const = 0;
273
274 /**
275 * Enable inplace vector operations if external and internal vectors
276 * are compatible. The returned pair indicates if the operation
277 * was successful on the coarse and the fine level.
278 */
279 virtual std::pair<bool, bool>
280 enable_inplace_operations_if_possible(
281 const std::shared_ptr<const Utilities::MPI::Partitioner>
282 &partitioner_coarse,
283 const std::shared_ptr<const Utilities::MPI::Partitioner>
284 &partitioner_fine) = 0;
285
286 /**
287 * Return the memory consumption of the allocated memory in this class.
288 */
289 virtual std::size_t
290 memory_consumption() const = 0;
291
292 protected:
293 /**
294 * Perform prolongation on vectors with correct ghosting.
295 */
296 virtual void
297 prolongate_and_add_internal(VectorType &dst, const VectorType &src) const = 0;
298
299 /**
300 * Perform restriction on vectors with correct ghosting.
301 */
302 virtual void
303 restrict_and_add_internal(VectorType &dst, const VectorType &src) const = 0;
304
305 /**
306 * A wrapper around update_ghost_values() optimized in case the
307 * present vector has the same parallel layout of one of the external
308 * partitioners.
309 */
310 void
311 update_ghost_values(const VectorType &vec) const;
312
313 /**
314 * A wrapper around compress() optimized in case the
315 * present vector has the same parallel layout of one of the external
316 * partitioners.
317 */
318 void
319 compress(VectorType &vec, const VectorOperation::values op) const;
320
321 /**
322 * A wrapper around zero_out_ghost_values() optimized in case the
323 * present vector has the same parallel layout of one of the external
324 * partitioners.
325 */
326 void
327 zero_out_ghost_values(const VectorType &vec) const;
328
329 /**
330 * Enable inplace vector operations if external and internal vectors
331 * are compatible.
332 */
333 template <int dim, std::size_t width, typename IndexType>
334 std::pair<bool, bool>
335 internal_enable_inplace_operations_if_possible(
336 const std::shared_ptr<const Utilities::MPI::Partitioner>
337 &partitioner_coarse,
338 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner_fine,
339 bool &vec_fine_needs_ghost_update,
340 internal::MatrixFreeFunctions::
341 ConstraintInfo<dim, VectorizedArray<Number, width>, IndexType>
342 &constraint_info_coarse,
343 std::vector<unsigned int> &dof_indices_fine);
344
345 /**
346 * Flag if the finite elements on the fine cells are continuous. If yes,
347 * the multiplicity of DoF sharing a vertex/line as well as constraints have
348 * to be taken into account via weights.
349 */
350 bool fine_element_is_continuous;
351
352 public:
353 /**
354 * Partitioner needed by the intermediate vector.
355 */
356 std::shared_ptr<const Utilities::MPI::Partitioner> partitioner_coarse;
357
358 /**
359 * Partitioner needed by the intermediate vector.
360 */
361 std::shared_ptr<const Utilities::MPI::Partitioner> partitioner_fine;
362
363 protected:
364 /**
365 * Internal vector on which the actual prolongation/restriction is performed.
366 */
367 mutable VectorType vec_coarse;
368
369 /**
370 * Internal vector needed for collecting all degrees of freedom of the fine
371 * cells. It is only initialized if the fine-level DoF indices touch DoFs
372 * other than the locally active ones (which we always assume can be
373 * accessed by the given vectors in the prolongate/restrict functions),
374 * otherwise it is left at size zero.
375 */
376 mutable VectorType vec_fine;
377
378 /**
379 * Bool indicating whether fine vector has relevant ghost values.
380 */
381 bool vec_fine_needs_ghost_update;
382
383 /**
384 * Embedded partitioner for efficient communication if locally relevant DoFs
385 * are a subset of an external Partitioner object.
386 */
387 std::shared_ptr<const Utilities::MPI::Partitioner>
388 partitioner_coarse_embedded;
389
390 /**
391 * Embedded partitioner for efficient communication if locally relevant DoFs
392 * are a subset of an external Partitioner object.
393 */
394 std::shared_ptr<const Utilities::MPI::Partitioner> partitioner_fine_embedded;
395
396 /**
397 * Buffer for efficient communication if locally relevant DoFs
398 * are a subset of an external Partitioner object.
399 */
400 mutable AlignedVector<Number> buffer_coarse_embedded;
401
402 /**
403 * Buffer for efficient communication if locally relevant DoFs
404 * are a subset of an external Partitioner object.
405 */
406 mutable AlignedVector<Number> buffer_fine_embedded;
407 };
408
409
410
411 /**
412 * Class for transfer between two multigrid levels for p- or global
413 * coarsening. It relies on a list of DoF indices associated with the cells on
414 * the coarse and fine side of the transfer, and implements a cell-by-cell
415 * (matrix-free) interpolation setup with the reference-cell embedding
416 * matrices.
417 *
418 * The implementation of this class is explained in detail in @cite munch2022gc.
419 *
420 * There are two possible ways to use this class. In the first option, the
421 * transfer is built from the underlying DoFHandler and AffineConstraints
422 * objects on the coarse and fine side, collecting an explicit copy of all
423 * indices on both sides. This works for a relatively wide set of
424 * FiniteElement combinations, including p-adaptive schemes using
425 * hp::FECollection. The second, more setup-efficient approach is to build the
426 * transfer between two multigrid levels for polynomial coarsening
427 * (p-coarsening) from two MatrixFree objects that might already exist from
428 * other parts of the code. In this case, we require that both objects share
429 * the same triangulation (but differ through their DoFHandler descriptions)
430 * and are described by the respective DoFHandler/AffineConstraints pair. This
431 * second variant is more efficient because no queries to the DoFHandler need
432 * to be made, reducing both the setup time and the overall memory
433 * consumption. Note that not all options are supported for the second entry
434 * point, and we fall back to the first option in such a case.
435 */
436 template <int dim, typename VectorType>
437 class MGTwoLevelTransfer : public MGTwoLevelTransferBase<VectorType>
438 {
439 public:
440 static_assert(
441 std::is_same_v<
442 VectorType,
443 LinearAlgebra::distributed::Vector<typename VectorType::value_type,
444 MemorySpace::Host>> ||
445 std::is_same_v<
446 VectorType,
447 LinearAlgebra::distributed::Vector<typename VectorType::value_type,
448 MemorySpace::Default>>,
449 "This class is currently only implemented for vectors of "
450 "type LinearAlgebra::distributed::Vector.");
451
452 /**
453 * The scalar type used by the vector-type template argument.
454 */
455 using Number = typename VectorType::value_type;
456
457 /**
458 * A data type representing a vectorized array of the same kind of objects
459 * stored in the `VectorType`.
460 */
461 using VectorizedArrayType = VectorizedArray<Number>;
462
463 /**
464 * Set up global coarsening between the given DoFHandler objects (
465 * @p dof_handler_fine and @p dof_handler_coarse). The transfer
466 * can be only performed on active levels.
467 */
468 void
469 reinit_geometric_transfer(
470 const DoFHandler<dim> &dof_handler_fine,
471 const DoFHandler<dim> &dof_handler_coarse,
472 const AffineConstraints<Number> &constraint_fine =
473 AffineConstraints<Number>(),
474 const AffineConstraints<Number> &constraint_coarse =
475 AffineConstraints<Number>(),
476 const unsigned int mg_level_fine = numbers::invalid_unsigned_int,
477 const unsigned int mg_level_coarse = numbers::invalid_unsigned_int);
478
479 /**
480 * Set up polynomial coarsening between the given DoFHandler objects (
481 * @p dof_handler_fine and @p dof_handler_coarse). Polynomial transfers
482 * can be only performed on active levels (`numbers::invalid_unsigned_int`)
483 * or on coarse-grid levels, i.e., levels without hanging nodes.
484 *
485 * @note The function polynomial_transfer_supported() can be used to
486 * check if the given polynomial coarsening strategy is supported.
487 */
488 void
489 reinit_polynomial_transfer(
490 const DoFHandler<dim> &dof_handler_fine,
491 const DoFHandler<dim> &dof_handler_coarse,
492 const AffineConstraints<Number> &constraint_fine =
493 AffineConstraints<Number>(),
494 const AffineConstraints<Number> &constraint_coarse =
495 AffineConstraints<Number>(),
496 const unsigned int mg_level_fine = numbers::invalid_unsigned_int,
497 const unsigned int mg_level_coarse = numbers::invalid_unsigned_int);
498
499 /**
500 * Set up the transfer operator between the given DoFHandler objects (
501 * @p dof_handler_fine and @p dof_handler_coarse). Depending on the
502 * underlying Triangulation objects polynomial or geometrical global
503 * coarsening is performed.
504 *
505 * @note While geometric transfer can be only performed on active levels
506 * (`numbers::invalid_unsigned_int`), polynomial transfers can also be
507 * performed on coarse-grid levels, i.e., levels without hanging nodes.
508 *
509 * @note The function polynomial_transfer_supported() can be used to
510 * check if the given polynomial coarsening strategy is supported.
511 */
512 void
513 reinit(const DoFHandler<dim> &dof_handler_fine,
514 const DoFHandler<dim> &dof_handler_coarse,
515 const AffineConstraints<Number> &constraint_fine =
516 AffineConstraints<Number>(),
517 const AffineConstraints<Number> &constraint_coarse =
518 AffineConstraints<Number>(),
519 const unsigned int mg_level_fine = numbers::invalid_unsigned_int,
520 const unsigned int mg_level_coarse = numbers::invalid_unsigned_int);
521
522 /**
523 * Set up polynomial coarsening between the DoFHandler objects underlying
524 * two MatrixFree objects and the respective numbers for the DoFHandler
525 * objects within MatrixFree. This reinit() function allows for a more
526 * efficient setup of the transfer operator and reduces the overall memory
527 * consumption of a multigrid cycle in case the same MatrixFree objects are
528 * also used for smoothers and residual evaluation on the two involved
529 * levels.
530 */
531 void
532 reinit(const MatrixFree<dim, Number> &matrix_free_fine,
533 const unsigned int dof_no_fine,
534 const MatrixFree<dim, Number> &matrix_free_coarse,
535 const unsigned int dof_no_coarse);
536
537 /**
538 * Check if a fast templated version of the polynomial transfer between
539 * @p fe_degree_fine and @p fe_degree_coarse is available.
540 *
541 * @note Currently, the polynomial coarsening strategies: 1) go-to-one,
542 * 2) bisect, and 3) decrease-by-one are precompiled with templates for
543 * degrees up to 9.
544 */
545 static bool
546 fast_polynomial_transfer_supported(const unsigned int fe_degree_fine,
547 const unsigned int fe_degree_coarse);
548
549 /**
550 * @copydoc MGTwoLevelTransferBase::interpolate
551 */
552 void
553 interpolate(VectorType &dst, const VectorType &src) const override;
554
555 /**
556 * Enable inplace vector operations if external and internal vectors
557 * are compatible.
558 */
559 std::pair<bool, bool>
560 enable_inplace_operations_if_possible(
561 const std::shared_ptr<const Utilities::MPI::Partitioner>
562 &partitioner_coarse,
563 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner_fine)
564 override;
565
566 /**
567 * Return the memory consumption of the allocated memory in this class.
568 */
569 std::size_t
570 memory_consumption() const override;
571
572 protected:
573 void
574 prolongate_and_add_internal(VectorType &dst,
575 const VectorType &src) const override;
576
577 void
578 restrict_and_add_internal(VectorType &dst,
579 const VectorType &src) const override;
580
581 private:
582 /**
583 * A multigrid transfer scheme. A multrigrid transfer class can have different
584 * transfer schemes to enable p-adaptivity (one transfer scheme per
585 * polynomial degree pair) and to enable global coarsening (one transfer
586 * scheme for transfer between children and parent cells, as well as, one
587 * transfer scheme for cells that are not refined).
588 */
589 struct MGTransferScheme
590 {
591 /**
592 * Number of coarse cells.
593 */
594 unsigned int n_coarse_cells;
595
596 /**
597 * Number of degrees of freedom of a coarse cell.
598 *
599 * @note For tensor-product elements, the value equals
600 * `n_components * (degree_coarse + 1)^dim`.
601 */
602 unsigned int n_dofs_per_cell_coarse;
603
604 /**
605 * Number of degrees of freedom of fine cell.
606 *
607 * @note For tensor-product elements, the value equals
608 * `n_components * (n_dofs_per_cell_fine + 1)^dim`.
609 */
610 unsigned int n_dofs_per_cell_fine;
611
612 /**
613 * Polynomial degree of the finite element of a coarse cell.
614 */
615 unsigned int degree_coarse;
616
617 /**
618 * "Polynomial degree" of the finite element of the union of all children
619 * of a coarse cell, i.e., actually `degree_fine * 2 + 1` if a cell is
620 * refined.
621 */
622 unsigned int degree_fine;
623
624 /**
625 * Prolongation matrix used for the prolongate_and_add() and
626 * restrict_and_add() functions.
627 */
628 AlignedVector<double> prolongation_matrix;
629
630 /**
631 * Restriction matrix used for the interpolate() function.
632 */
633 AlignedVector<double> restriction_matrix;
634
635 /**
636 * ShapeInfo description of the coarse cell. Needed during the
637 * fast application of hanging-node constraints.
638 */
639 internal::MatrixFreeFunctions::ShapeInfo<double> shape_info_coarse;
640 };
641
642 /**
643 * Transfer schemes.
644 */
645 std::vector<MGTransferScheme> schemes;
646
647 /**
648 * Helper class for reading from and writing to global coarse vectors and for
649 * applying constraints.
650 */
651 internal::MatrixFreeFunctions::
652 ConstraintInfo<dim, VectorizedArrayType, types::global_dof_index>
653 constraint_info_coarse;
654
655 /**
656 * Helper class for reading from and writing to global fine vectors.
657 */
658 internal::MatrixFreeFunctions::
659 ConstraintInfo<dim, VectorizedArrayType, types::global_dof_index>
660 constraint_info_fine;
661
662 struct MatrixFreeRelatedData
663 {
664 /**
665 * Matrix-free object on the fine side.
666 */
667 ObserverPointer<const MatrixFree<dim, Number>> matrix_free_fine;
668
669 /**
670 * Index within the list of DoFHandler objects in the matrix_free_fine
671 * object.
672 */
673 unsigned int dof_handler_index_fine;
674
675 /**
676 * Matrix-free object on the coarse side.
677 */
678 ObserverPointer<const MatrixFree<dim, Number>> matrix_free_coarse;
679
680 /**
681 * Index within the list of DoFHandler objects in the matrix_free_coarse
682 * object.
683 */
684 unsigned int dof_handler_index_coarse;
685
686 /**
687 * The two matrix-free objects will in general not agree on the order the
688 * cells are traversed. Thus, the loop will be run by the matrix-free object
689 * on the fine side, and the coarse side will adapt to those cell indices.
690 */
691 std::vector<std::array<unsigned int, VectorizedArrayType::size()>>
692 cell_list_fine_to_coarse;
693 };
694
695 /**
696 * In case this class is built with MatrixFree objects (see the respective
697 * reinit() function), we set up this data structure and skip the other
698 * fields of the class.
699 */
700 std::unique_ptr<MatrixFreeRelatedData> matrix_free_data;
701
702 /**
703 * CRS-like pointer to the start into the weights array, as that array can
704 * be compressed or in full format.
705 */
706 std::vector<unsigned int> weights_start;
707
708 /**
709 * Weights for continuous elements, either in full format or compressed into
710 * 3^dim doubles per cell if possible.
711 */
712 AlignedVector<VectorizedArrayType> weights;
713
714 /**
715 * Store whether the weights are in compressed format or not, in the
716 * ordering of the weights_start array.
717 */
718 std::vector<unsigned char> weights_are_compressed;
719
720 /**
721 * Number of components.
722 */
723 unsigned int n_components;
724
725 /**
726 * Pointer to the DoFHandler object used during initialization.
727 */
728 ObserverPointer<const DoFHandler<dim>> dof_handler_fine;
729
730 /**
731 * Multigrid level used during initialization.
732 */
733 unsigned int mg_level_fine;
734
735 friend class internal::MGTwoLevelTransferImplementation;
736
737 friend class MGTransferMF<dim, Number, typename VectorType::memory_space>;
738 };
739
740
741
742 /**
743 * Class for transfer between two non-nested multigrid levels.
744 */
745 template <int dim, typename VectorType>
746 class MGTwoLevelTransferNonNested : public MGTwoLevelTransferBase<VectorType>
747 {
748 private:
749 static_assert(
750 std::is_same_v<
751 VectorType,
752 LinearAlgebra::distributed::Vector<typename VectorType::value_type>>,
753 "This class is currently only implemented for vectors of "
754 "type LinearAlgebra::distributed::Vector.");
755
756 using Number = typename VectorType::value_type;
757 using VectorizedArrayType = VectorizedArray<Number, 1>;
758
759 mg::SignalsNonNested signals_non_nested;
760
761 public:
762 /**
763 * AdditionalData structure that can be used to tweak parameters
764 * related to the search procedure (used internally by RemotePointEvaluation)
765 * or, in the future, transfer operators needed by the non-nested multigrid
766 * algorithm.
767 */
768 struct AdditionalData
769 {
770 /**
771 * Constructor. By default, the @p tolerance and @p rtree_level parameters
772 * are set to the default values used in the constructor of
773 * RemotePointEvaluation, i.e. 1e-6 and 0, respectively. The last Boolean
774 * parameter @p enforce_all_points_found is true by default and checks
775 * that all points submitted internally to RemotePointEvaluation::reinit()
776 * have been found.
777 *
778 */
779 AdditionalData(const double tolerance = 1e-6,
780 const unsigned int rtree_level = 0,
781 const bool enforce_all_points_found = true)
782 : tolerance(tolerance)
783 , rtree_level(rtree_level)
784 , enforce_all_points_found(enforce_all_points_found)
785 {}
786
787 /**
788 * Tolerance parameter. See the constructor of RemotePointEvaluation for
789 * more details.
790 */
791 double tolerance;
792
793 /**
794 * RTree level parameter. See the constructor of RemotePointEvaluation for
795 * more details.
796 *
797 */
798 unsigned int rtree_level;
799
800 /**
801 * If set to true, it checks if RemotePointEvaluation::all_points_found()
802 * evaluates to true internally during the each call to reinit() from one
803 * level to the next one, ensuring that all submitted points have been found
804 * inside the domain.
805 *
806 */
807 bool enforce_all_points_found;
808 };
809
810 /**
811 * Constructor.
812 */
813 MGTwoLevelTransferNonNested(const AdditionalData &data = AdditionalData());
814
815 /**
816 * Set up transfer operator between the given DoFHandler objects (
817 * @p dof_handler_fine and @p dof_handler_coarse).
818 */
819 void
820 reinit(const DoFHandler<dim> &dof_handler_fine,
821 const DoFHandler<dim> &dof_handler_coarse,
822 const Mapping<dim> &mapping_fine,
823 const Mapping<dim> &mapping_coarse,
824 const AffineConstraints<Number> &constraint_fine =
825 AffineConstraints<Number>(),
826 const AffineConstraints<Number> &constraint_coarse =
827 AffineConstraints<Number>());
828
829 /**
830 * Perform interpolation of a solution vector from the fine level to the
831 * coarse level. This function is different from restriction, where a
832 * weighted residual is transferred to a coarser level (transposition of
833 * prolongation matrix).
834 */
835 void
836 interpolate(VectorType &dst, const VectorType &src) const override;
837
838 /**
839 * Enable inplace vector operations if external and internal vectors
840 * are compatible.
841 */
842 std::pair<bool, bool>
843 enable_inplace_operations_if_possible(
844 const std::shared_ptr<const Utilities::MPI::Partitioner>
845 &partitioner_coarse,
846 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner_fine)
847 override;
848
849 /**
850 * Return the memory consumption of the allocated memory in this class.
851 */
852 std::size_t
853 memory_consumption() const override;
854
855 /**
856 * Connect a function to mg::SignalsNonNested::prolongation_cell_loop.
857 */
858 boost::signals2::connection
859 connect_prolongation_cell_loop(const std::function<void(const bool)> &slot);
860
861 /**
862 * Connect a function to mg::SignalsNonNested::restriction_cell_loop.
863 */
864 boost::signals2::connection
865 connect_restriction_cell_loop(const std::function<void(const bool)> &slot);
866
867 /**
868 * Connect a function to mg::SignalsNonNested::prolongation.
869 */
870 boost::signals2::connection
871 connect_prolongation(const std::function<void(const bool)> &slot);
872
873 /**
874 * Connect a function to mg::SignalsNonNested::restriction.
875 */
876 boost::signals2::connection
877 connect_restriction(const std::function<void(const bool)> &slot);
878
879 protected:
880 AdditionalData additional_data;
881 /**
882 * Perform prolongation.
883 */
884 void
885 prolongate_and_add_internal(VectorType &dst,
886 const VectorType &src) const override;
887
888 /**
889 * Perform restriction.
890 */
891 void
892 restrict_and_add_internal(VectorType &dst,
893 const VectorType &src) const override;
894
895 private:
896 /**
897 * Perform prolongation for correct number of components.
898 */
899 template <int n_components>
900 void
901 prolongate_and_add_internal_comp(VectorType &dst,
902 const VectorType &src) const;
903
904 /**
905 * Perform restriction for correct number of components.
906 */
907 template <int n_components>
908 void
909 restrict_and_add_internal_comp(VectorType &dst, const VectorType &src) const;
910
911 /**
912 * Pointer to the DoFHandler object used during initialization.
913 */
914 ObserverPointer<const DoFHandler<dim>> dof_handler_fine;
915
916 /**
917 * Multigrid level used during initialization.
918 */
919 unsigned int mg_level_fine;
920
921 /**
922 * Object to evaluate shape functions on one mesh on visited support points of
923 * the other mesh.
924 */
925 Utilities::MPI::RemotePointEvaluation<dim> rpe;
926
927 /**
928 * Vectors for input/output for rpe.
929 */
930 mutable std::vector<Number> rpe_input_output;
931
932 /**
933 * Buffers to be reused by rpe.
934 */
935 mutable std::vector<Number> rpe_buffer;
936
937 /**
938 * MappingInfo object needed as Mapping argument by FEPointEvaluation.
939 */
940 std::shared_ptr<NonMatching::MappingInfo<dim, dim, Number>> mapping_info;
941
942 /**
943 * Helper class for reading from and writing to global vectors and for
944 * applying constraints.
945 */
946 internal::MatrixFreeFunctions::
947 ConstraintInfo<dim, VectorizedArrayType, unsigned int>
948 constraint_info;
949
950 /**
951 * Finite element of the coarse DoFHandler passed to reinit().
952 */
953 std::unique_ptr<FiniteElement<dim>> fe_coarse;
954
955 /**
956 * DoF indices of the fine cells, expressed in indices local to the MPI
957 * rank.
958 */
959 std::vector<unsigned int> level_dof_indices_fine;
960
961 /**
962 * CRS like structure which points to DoFs associated with the same support
963 * point. The vector stays empty if only one DoF corresponds to one support
964 * point.
965 */
966 std::vector<unsigned int> level_dof_indices_fine_ptrs;
967
968 friend class MGTransferMF<dim, Number, typename VectorType::memory_space>;
969 };
970
971
972
973 /**
974 * Implementation of the MGTransferBase. In contrast to
975 * other multigrid transfer operators, the user can provide separate
976 * transfer operators of type MGTwoLevelTransfer between each level. The
977 * sequence of functions calls for setup is:
978 * @code
979 * MGTransferGlobalCoarsening mg_transfer;
980 * mg_transfer.initialize_two_level_transfers(two_level_transfers);
981 * mg_transfer.build(partitioners);
982 * @endcode
983 *
984 * Alternatively, this class can also be set up as in the case of
985 * MGTransferMatrixFree:
986 * @code
987 * MGTransferGlobalCoarsening mg_transfer;
988 * mg_transfer.initialize_constraints(mg_constrained_dofs);
989 * mg_transfer.build(dof_handler, partitioners);
990 * @endcode
991 * However, this is way to set up is currently only working for globally
992 * refined meshes.
993 *
994 * This class currently only works for the tensor-product finite elements
995 * FE_Q, FE_DGQ, and FE_DGP and simplex elements FE_SimplexP and FE_SimplexDGP
996 * as well as for systems involving multiple components of one of these
997 * elements. Other elements are currently not implemented.
998 *
999 * The implementation of this class is explained in detail in @cite munch2022gc.
1000 */
1001 template <int dim, typename Number, typename MemorySpace>
1002 class MGTransferMF : public dealii::MGLevelGlobalTransfer<
1003 LinearAlgebra::distributed::Vector<Number, MemorySpace>>
1004 {
1005 public:
1006 /**
1007 * Value type.
1008 */
1009 using VectorType = LinearAlgebra::distributed::Vector<Number, MemorySpace>;
1010
1011 /**
1012 * Default constructor.
1013 *
1014 * @note See also MGTransferMatrixFree.
1015 */
1016 MGTransferMF();
1017
1018 /**
1019 * @name Global coarsening.
1020 */
1021 /** @{ */
1022
1023 /**
1024 * Constructor taking a collection of transfer operators (with the coarsest
1025 * level kept empty in @p transfer) and an optional function that initializes the
1026 * internal level vectors within the function call copy_to_mg() if used in the
1027 * context of PreconditionMG. The template parameter @p MGTwoLevelTransferObject should derive from
1028 * MGTwoLevelTransferBase and implement the transfer operation (see for
1029 * instance MGTwoLevelTransfer). It can also be a std::shared_ptr or
1030 * std::unique_ptr to the actual transfer operator.
1031 */
1032 template <typename MGTwoLevelTransferObject>
1033 MGTransferMF(const MGLevelObject<MGTwoLevelTransferObject> &transfer,
1034 const std::function<void(const unsigned int, VectorType &)>
1035 &initialize_dof_vector = {});
1036
1037 /**
1038 * Set two-level transfers.
1039 */
1040 template <typename MGTwoLevelTransferObject>
1041 void
1042 initialize_two_level_transfers(
1043 const MGLevelObject<MGTwoLevelTransferObject> &transfer);
1044
1045 /**
1046 * Similar function to MGTransferMatrixFree::build() with the difference that
1047 * the information for the prolongation for each level has already been built.
1048 * So this function only tries to optimize the data structures of the
1049 * two-level transfer operators, e.g., by enabling inplace vector operations,
1050 * by checking if @p external_partitioners and the internal ones are
1051 * compatible.
1052 */
1053 void
1054 build(const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
1055 &external_partitioners = {});
1056
1057 /**
1058 * Same as above but taking a lambda for initializing vector instead of
1059 * partitioners.
1060 */
1061 void
1062 build(const std::function<void(const unsigned int, VectorType &)>
1063 &initialize_dof_vector);
1064
1065 /** @} */
1066
1067 /**
1068 * @name Local smoothing.
1069 */
1070 /** @{ */
1071
1072 /**
1073 * Constructor with constraints. Equivalent to the default constructor
1074 * followed by initialize_constraints().
1075 *
1076 * @note See also MGTransferMatrixFree.
1077 */
1078 MGTransferMF(const MGConstrainedDoFs &mg_constrained_dofs);
1079
1080 /**
1081 * Initialize the constraints to be used in build().
1082 *
1083 * @note See also MGTransferMatrixFree.
1084 */
1085 void
1086 initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs);
1087
1088 /** @} */
1089
1090 /**
1091 * @name Global coarsening and local smoothing.
1092 */
1093 /** @{ */
1094
1095 /**
1096 * Actually build the information for the prolongation for each level.
1097 *
1098 * @note In the case of global coarsening, you can pass a @p dof_handler
1099 * with different DoF numbering as the one used within the provided
1100 * two-level transfer objects into this function. In this case, vector
1101 * entries are permuted during copy_to_mg(), copy_from_mg(), and
1102 * interpolate_to_mg().
1103 */
1104 void
1105 build(const DoFHandler<dim> &dof_handler,
1106 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
1107 &external_partitioners = {});
1108
1109 /**
1110 * Same as above but taking a lambda for initializing vector instead of
1111 * partitioners.
1112 *
1113 * @note In the case of global coarsening, you can pass into this function
1114 * a @p dof_handler with different DoF numbering as the one used within the
1115 * provided two-level transfer objects. In this case, vector entries are
1116 * permuted during copy_to_mg(), copy_from_mg(), and interpolate_to_mg().
1117 */
1118 void
1119 build(const DoFHandler<dim> &dof_handler,
1120 const std::function<void(const unsigned int, VectorType &)>
1121 &initialize_dof_vector);
1122
1123 /** @} */
1124
1125 /**
1126 * @name Transfer functions.
1127 */
1128 /** @{ */
1129
1130 /**
1131 * Perform prolongation.
1132 */
1133 void
1134 prolongate(const unsigned int to_level,
1135 VectorType &dst,
1136 const VectorType &src) const override;
1137
1138 /**
1139 * Perform prolongation.
1140 */
1141 void
1142 prolongate_and_add(const unsigned int to_level,
1143 VectorType &dst,
1144 const VectorType &src) const override;
1145
1146 /**
1147 * Perform restriction.
1148 */
1149 virtual void
1150 restrict_and_add(const unsigned int from_level,
1151 VectorType &dst,
1152 const VectorType &src) const override;
1153
1154 /**
1155 * Initialize internal vectors and copy @p src vector
1156 * (associated to @p dof_handler) to the finest multigrid level.
1157 *
1158 * @note The @p dof_handler object needs to be the same as the DoFHandler passed
1159 * directly to the function build() or indirectly to the function
1160 * initialize_two_level_transfers(). Alternatively, the numbering of the DoFs
1161 * need to be same.
1162 */
1163 template <class InVector>
1164 void
1165 copy_to_mg(const DoFHandler<dim> &dof_handler,
1166 MGLevelObject<VectorType> &dst,
1167 const InVector &src) const;
1168
1169 /**
1170 * Copy the values on the finest multigrid level to @p dst
1171 * vector (associated to @p dof_handler).
1172 *
1173 * @note The @p dof_handler object needs to be the same as the DoFHandler passed
1174 * directly to the function build() or indirectly to the function
1175 * initialize_two_level_transfers(). Alternatively, the numbering of the DoFs
1176 * need to be same.
1177 */
1178 template <class OutVector>
1179 void
1180 copy_from_mg(const DoFHandler<dim> &dof_handler,
1181 OutVector &dst,
1182 const MGLevelObject<VectorType> &src) const;
1183
1184 /**
1185 * Interpolate fine-mesh field @p src (associated to @p dof_handler)
1186 * to each multigrid level and
1187 * store the result in @p dst. This function is different from
1188 * restriction, where a weighted residual is transferred to a coarser
1189 * level (transposition of prolongation matrix).
1190 *
1191 * The argument @p dst has to be initialized with the correct size according
1192 * to the number of levels of the triangulation.
1193 *
1194 * If an inner vector of @p dst is empty or has incorrect locally owned size,
1195 * it will be resized to locally relevant degrees of freedom on each level.
1196 *
1197 * @note The @p dof_handler object needs to be the same as the DoFHandler passed
1198 * directly to the function build() or indirectly to the function
1199 * initialize_two_level_transfers(). Alternatively, the numbering of the DoFs
1200 * need to be same.
1201 */
1202 template <class InVector>
1203 void
1204 interpolate_to_mg(const DoFHandler<dim> &dof_handler,
1205 MGLevelObject<VectorType> &dst,
1206 const InVector &src) const;
1207
1208 /**
1209 * Interpolate fine-mesh field @p src to each multigrid level and
1210 * store the result in @p dst.
1211 *
1212 * @note In contrast to the last function, no @p dof_handler object needs passed.
1213 * This function used the DoFHandler passed directly to the function build()
1214 * or indirectly to the function initialize_two_level_transfers().
1215 */
1216 template <class InVector>
1217 void
1218 interpolate_to_mg(MGLevelObject<VectorType> &dst, const InVector &src) const;
1219
1220 /** @} */
1221
1222 /**
1223 * @name Utility functions.
1224 */
1225 /** @{ */
1226
1227 /**
1228 * Return the memory consumption of the allocated memory in this class.
1229 *
1230 * @note Counts also the memory consumption of the underlying two-level
1231 * transfer operators.
1232 */
1233 std::size_t
1234 memory_consumption() const;
1235
1236 /**
1237 * Minimum level.
1238 */
1239 unsigned int
1240 min_level() const;
1241
1242 /**
1243 * Maximum level.
1244 */
1245 unsigned int
1246 max_level() const;
1247
1248 /**
1249 * Clear all data fields and brings the class into a condition similar
1250 * to after having called the default constructor.
1251 */
1252 void
1253 clear();
1254
1255 /** @} */
1256
1257 private:
1258 /**
1259 * Initial internal transfer operator.
1260 *
1261 * @note See also MGTransferMatrixFree.
1262 */
1263 void
1264 initialize_internal_transfer(
1265 const DoFHandler<dim> &dof_handler,
1266 const ObserverPointer<const MGConstrainedDoFs> &mg_constrained_dofs);
1267
1268 /**
1269 * Retrieve finest DoFHandler from two-level transfer objects.
1270 */
1271 std::pair<const DoFHandler<dim> *, unsigned int>
1272 get_dof_handler_fine() const;
1273
1274 /**
1275 * Initialize copy indices for MGTransferMF::copy_to_mg(),
1276 * MGTransferMF::copy_to_mg(), and MGTransferMF::interpolate_to_mg()
1277 * in the case of global coarsening.
1278 */
1279 void
1280 fill_and_communicate_copy_indices_global_coarsening(
1281 const DoFHandler<dim> &dof_handler);
1282
1283 /**
1284 * Set references to two-level transfer operators to be used.
1285 */
1286 template <typename MGTwoLevelTransferObject>
1287 void
1288 initialize_transfer_references(
1289 const MGLevelObject<MGTwoLevelTransferObject> &transfer);
1290
1291 /**
1292 * Function to initialize internal level vectors.
1293 */
1294 template <class InVector>
1295 void
1296 initialize_dof_vector(const unsigned int level,
1297 VectorType &vector,
1298 const InVector &vector_reference,
1299 const bool omit_zeroing_entries) const;
1300
1301 /**
1302 * Check that the internal DoFHandler is compatible with the external one
1303 * provided by copy_to_mg(), copy_from_mg() and interpolate_to_mg()
1304 * used, e.g., by PreconditionMG.
1305 */
1306 void
1307 assert_dof_handler(const DoFHandler<dim> &dof_handler_out) const;
1308
1309 /**
1310 * Internal transfer operator.
1311 *
1312 * @note See also MGTransferMatrixFree.
1313 */
1314 MGLevelObject<MGTwoLevelTransfer<dim, VectorType>> internal_transfer;
1315
1316 /**
1317 * Collection of the two-level transfer operators.
1318 */
1319 MGLevelObject<ObserverPointer<MGTwoLevelTransferBase<VectorType>>> transfer;
1320
1321 /**
1322 * External partitioners used during initialize_dof_vector().
1323 */
1324 std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
1325 external_partitioners;
1326 };
1327
1328
1329
1330 /**
1331 * This class works with LinearAlgebra::distributed::BlockVector and
1332 * performs exactly the same transfer operations for each block as
1333 * MGTransferMF.
1334 */
1335 template <int dim, typename Number>
1336 class MGTransferBlockMF
1337 : public MGTransferBlockMatrixFreeBase<
1338 dim,
1339 Number,
1340 MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>>
1341 {
1342 public:
1343 /**
1344 * Constructor.
1345 */
1346 MGTransferBlockMF(const MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>
1347 &transfer_operator);
1348
1349 /**
1350 * Constructor.
1351 *
1352 * @note See also MGTransferBlockMatrixFree.
1353 */
1354 MGTransferBlockMF() = default;
1355
1356 /**
1357 * Constructor.
1358 *
1359 * @note See also MGTransferBlockMatrixFree.
1360 */
1361 MGTransferBlockMF(const MGConstrainedDoFs &mg_constrained_dofs);
1362
1363 /**
1364 * Constructor.
1365 *
1366 * @note See also MGTransferBlockMatrixFree.
1367 */
1368 MGTransferBlockMF(const std::vector<MGConstrainedDoFs> &mg_constrained_dofs);
1369
1370 /**
1371 * Initialize the constraints to be used in build().
1372 *
1373 * @note See also MGTransferBlockMatrixFree.
1374 */
1375 void
1376 initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs);
1377
1378 /**
1379 * Same as above for the case that each block has its own DoFHandler.
1380 *
1381 * @note See also MGTransferBlockMatrixFree.
1382 */
1383 void
1384 initialize_constraints(
1385 const std::vector<MGConstrainedDoFs> &mg_constrained_dofs);
1386
1387 /**
1388 * Actually build the information for the prolongation for each level.
1389 *
1390 * @note See also MGTransferBlockMatrixFree.
1391 */
1392 void
1393 build(const DoFHandler<dim> &dof_handler);
1394
1395 /**
1396 * Same as above for the case that each block has its own DoFHandler.
1397 *
1398 * @note See also MGTransferBlockMatrixFree.
1399 */
1400 void
1401 build(const std::vector<const DoFHandler<dim> *> &dof_handler);
1402
1403 protected:
1404 const MGTransferMF<dim, Number, ::dealii::MemorySpace::Host> &
1405 get_matrix_free_transfer(const unsigned int b) const override;
1406
1407 private:
1408 /**
1409 * Internal non-block version of transfer operation.
1410 */
1411 std::vector<MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>>
1412 transfer_operators_internal;
1413
1414 /**
1415 * Non-block version of transfer operation.
1416 */
1417 std::vector<ObserverPointer<
1418 const MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>>>
1419 transfer_operators;
1420 };
1421
1422
1423
1424 template <int dim, typename VectorType>
1425 using MGTransferGlobalCoarsening = MGTransferMF<dim,
1426 typename VectorType::value_type,
1427 ::dealii::MemorySpace::Host>;
1428
1429 template <int dim, typename VectorType>
1430 using MGTransferBlockGlobalCoarsening =
1431 MGTransferBlockMF<dim, typename VectorType::value_type>;
1432
1433
1434 /** @} */
1435
1436 #ifndef DOXYGEN
1437
1438 /* ----------------------- Inline functions --------------------------------- */
1439
1440
1441
1442 template <int dim, typename Number, typename MemorySpace>
1443 template <typename MGTwoLevelTransferObject>
1444 MGTransferMF<dim, Number, MemorySpace>::MGTransferMF(
1445 const MGLevelObject<MGTwoLevelTransferObject> &transfer,
1446 const std::function<void(const unsigned int, VectorType &)>
1447 &initialize_dof_vector)
1448 {
1449 this->transfer.clear();
1450 this->internal_transfer.clear();
1451
1452 this->initialize_transfer_references(transfer);
1453 this->build(initialize_dof_vector);
1454 }
1455
1456
1457
1458 template <int dim, typename Number, typename MemorySpace>
1459 template <typename MGTwoLevelTransferObject>
1460 void
1461 MGTransferMF<dim, Number, MemorySpace>::initialize_two_level_transfers(
1462 const MGLevelObject<MGTwoLevelTransferObject> &transfer)
1463 {
1464 this->initialize_transfer_references(transfer);
1465 }
1466
1467
1468
1469 template <int dim, typename Number, typename MemorySpace>
1470 template <typename MGTwoLevelTransferObject>
1471 void
1472 MGTransferMF<dim, Number, MemorySpace>::initialize_transfer_references(
1473 const MGLevelObject<MGTwoLevelTransferObject> &transfer)
1474 {
1475 const unsigned int min_level = transfer.min_level();
1476 const unsigned int max_level = transfer.max_level();
1477
1478 this->transfer.resize(min_level, max_level);
1479
1480 // Note that transfer[min_level] is empty and never used:
1481 for (unsigned int l = min_level + 1; l <= max_level; ++l)
1482 this->transfer[l] = &const_cast<MGTwoLevelTransferBase<VectorType> &>(
1483 static_cast<const MGTwoLevelTransferBase<VectorType> &>(
1484 Utilities::get_underlying_value(transfer[l])));
1485 }
1486
1487
1488
1489 template <int dim, typename Number, typename MemorySpace>
1490 template <class InVector>
1491 void
1492 MGTransferMF<dim, Number, MemorySpace>::initialize_dof_vector(
1493 const unsigned int level,
1494 VectorType &vec,
1495 const InVector &vec_reference,
1496 const bool omit_zeroing_entries) const
1497 {
1498 std::shared_ptr<const Utilities::MPI::Partitioner> partitioner;
1499
1500 if (external_partitioners.empty())
1501 {
1502 partitioner = vec_reference.get_partitioner();
1503 }
1504 else
1505 {
1506 Assert(transfer.min_level() <= level && level <= transfer.max_level(),
1507 ExcInternalError());
1508
1509 partitioner = external_partitioners[level - transfer.min_level()];
1510 }
1511
1512 // check if vectors are already correctly initialized
1513
1514 // yes: same partitioners are used
1515 if (vec.get_partitioner().get() == partitioner.get())
1516 {
1517 if (omit_zeroing_entries == false)
1518 vec = 0;
1519 return; // nothing to do
1520 }
1521
1522 // yes: vectors are compatible
1523 if (vec.size() == partitioner->size() &&
1524 vec.locally_owned_size() == partitioner->locally_owned_size())
1525 {
1526 if (omit_zeroing_entries == false)
1527 vec = 0;
1528 return; // nothing to do
1529 }
1530
1531 // no
1532 vec.reinit(partitioner, omit_zeroing_entries);
1533 }
1534
1535
1536
1537 template <int dim, typename Number, typename MemorySpace>
1538 template <class InVector>
1539 void
1540 MGTransferMF<dim, Number, MemorySpace>::copy_to_mg(
1541 const DoFHandler<dim> &dof_handler,
1542 MGLevelObject<VectorType> &dst,
1543 const InVector &src) const
1544 {
1545 assert_dof_handler(dof_handler);
1546
1547 for (unsigned int level = dst.min_level(); level <= dst.max_level(); ++level)
1548 {
1549 const bool zero_out_values =
1550 (this->perform_plain_copy == false &&
1551 this->perform_renumbered_plain_copy == false) ||
1552 level != dst.max_level();
1553
1554 this->initialize_dof_vector(level, dst[level], src, !zero_out_values);
1555 }
1556
1557 if (this->perform_plain_copy)
1558 {
1559 dst[dst.max_level()].copy_locally_owned_data_from(src);
1560 }
1561 else if (this->perform_renumbered_plain_copy)
1562 {
1563 auto &dst_level = dst[dst.max_level()];
1564
1565 for (unsigned int i = 0; i < this->copy_indices.back().n_cols(); ++i)
1566 dst_level.local_element(this->copy_indices.back()(1, i)) =
1567 src.local_element(i);
1568 }
1569 else
1570 {
1571 this->ghosted_global_vector = src;
1572 this->ghosted_global_vector.update_ghost_values();
1573
1574 for (unsigned int l = dst.max_level() + 1; l != dst.min_level();)
1575 {
1576 --l;
1577
1578 auto &dst_level = dst[l];
1579
1580 const auto copy_unknowns = [&](const auto &indices) {
1581 for (unsigned int i = 0; i < indices.n_cols(); ++i)
1582 dst_level.local_element(indices(1, i)) =
1583 this->ghosted_global_vector.local_element(indices(0, i));
1584 };
1585
1586 copy_unknowns(this->copy_indices[l]);
1587 copy_unknowns(this->copy_indices_level_mine[l]);
1588
1589 dst_level.compress(VectorOperation::insert);
1590 }
1591 }
1592 }
1593
1594
1595
1596 template <int dim, typename Number, typename MemorySpace>
1597 template <class OutVector>
1598 void
1599 MGTransferMF<dim, Number, MemorySpace>::copy_from_mg(
1600 const DoFHandler<dim> &dof_handler,
1601 OutVector &dst,
1602 const MGLevelObject<VectorType> &src) const
1603 {
1604 assert_dof_handler(dof_handler);
1605
1606 if (this->perform_plain_copy)
1607 {
1608 dst.zero_out_ghost_values();
1609 dst.copy_locally_owned_data_from(src[src.max_level()]);
1610 }
1611 else if (this->perform_renumbered_plain_copy)
1612 {
1613 const auto &src_level = src[src.max_level()];
1614 dst.zero_out_ghost_values();
1615 for (unsigned int i = 0; i < this->copy_indices.back().n_cols(); ++i)
1616 dst.local_element(i) =
1617 src_level.local_element(this->copy_indices.back()(1, i));
1618 }
1619 else
1620 {
1621 dst = 0;
1622 for (unsigned int l = src.min_level(); l <= src.max_level(); ++l)
1623 {
1624 auto &ghosted_vector = this->ghosted_level_vector[l];
1625
1626 if (this->ghosted_level_vector[l].size() > 0)
1627 ghosted_vector = src[l];
1628
1629 const auto *const ghosted_vector_ptr =
1630 (this->ghosted_level_vector[l].size() > 0) ? &ghosted_vector :
1631 &src[l];
1632
1633 ghosted_vector_ptr->update_ghost_values();
1634
1635 const auto copy_unknowns = [&](const auto &indices) {
1636 for (unsigned int i = 0; i < indices.n_cols(); ++i)
1637 dst.local_element(indices(0, i)) =
1638 ghosted_vector_ptr->local_element(indices(1, i));
1639 };
1640
1641 copy_unknowns(this->copy_indices[l]);
1642 copy_unknowns(this->copy_indices_global_mine[l]);
1643 }
1644 dst.compress(VectorOperation::insert);
1645 }
1646 }
1647
1648
1649
1650 template <int dim, typename Number, typename MemorySpace>
1651 template <class InVector>
1652 void
1653 MGTransferMF<dim, Number, MemorySpace>::interpolate_to_mg(
1654 MGLevelObject<VectorType> &dst,
1655 const InVector &src) const
1656 {
1657 DoFHandler<dim> dof_handler_dummy;
1658
1659 this->interpolate_to_mg(dof_handler_dummy, dst, src);
1660 }
1661
1662
1663
1664 template <int dim, typename Number, typename MemorySpace>
1665 template <class InVector>
1666 void
1667 MGTransferMF<dim, Number, MemorySpace>::interpolate_to_mg(
1668 const DoFHandler<dim> &dof_handler,
1669 MGLevelObject<VectorType> &dst,
1670 const InVector &src) const
1671 {
1672 assert_dof_handler(dof_handler);
1673
1674 const unsigned int min_level = transfer.min_level();
1675 const unsigned int max_level = transfer.max_level();
1676
1677 AssertDimension(min_level, dst.min_level());
1678 AssertDimension(max_level, dst.max_level());
1679
1680 for (unsigned int level = min_level; level <= max_level; ++level)
1681 {
1682 const bool zero_out_values = false;
1683 this->initialize_dof_vector(level, dst[level], src, !zero_out_values);
1684 }
1685
1686 if (this->perform_plain_copy)
1687 {
1688 dst[max_level].copy_locally_owned_data_from(src);
1689
1690 for (unsigned int l = max_level; l > min_level; --l)
1691 this->transfer[l]->interpolate(dst[l - 1], dst[l]);
1692 }
1693 else if (this->perform_renumbered_plain_copy)
1694 {
1695 auto &dst_level = dst[max_level];
1696
1697 for (unsigned int i = 0; i < this->solution_copy_indices.back().n_cols();
1698 ++i)
1699 dst_level.local_element(this->solution_copy_indices.back()(1, i)) =
1700 src.local_element(i);
1701
1702 for (unsigned int l = max_level; l > min_level; --l)
1703 this->transfer[l]->interpolate(dst[l - 1], dst[l]);
1704 }
1705 else
1706 {
1707 this->solution_ghosted_global_vector = src;
1708 this->solution_ghosted_global_vector.update_ghost_values();
1709
1710 for (unsigned int l = max_level + 1; l != min_level;)
1711 {
1712 --l;
1713
1714 auto &dst_level = dst[l];
1715
1716 const auto copy_unknowns = [&](const auto &indices) {
1717 for (unsigned int i = 0; i < indices.n_cols(); ++i)
1718 dst_level.local_element(indices(1, i)) =
1719 this->solution_ghosted_global_vector.local_element(
1720 indices(0, i));
1721 };
1722
1723 copy_unknowns(this->solution_copy_indices[l]);
1724 copy_unknowns(this->solution_copy_indices_level_mine[l]);
1725
1726 dst_level.compress(VectorOperation::insert);
1727
1728 if (l != min_level)
1729 this->transfer[l]->interpolate(dst[l - 1], dst[l]);
1730 }
1731 }
1732 }
1733
1734 #endif
1735
1736 DEAL_II_NAMESPACE_CLOSE
1737
1738 #endif