Debian Codegraph @

include/deal.II/multigrid/mg_transfer_global_coarsening.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2025 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
16 #ifndef dealii_mg_transfer_global_coarsening_templates_h
17 #define dealii_mg_transfer_global_coarsening_templates_h
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/memory_space.h>
22 #include <deal.II/base/mpi_consensus_algorithms.h>
23
24 #include <deal.II/distributed/fully_distributed_tria.h>
25 #include <deal.II/distributed/repartitioning_policy_tools.h>
26 #include <deal.II/distributed/shared_tria.h>
27 #include <deal.II/distributed/tria.h>
28
29 #include <deal.II/dofs/dof_handler.h>
30 #include <deal.II/dofs/dof_tools.h>
31
32 #include <deal.II/fe/fe_q.h>
33 #include <deal.II/fe/fe_simplex_p.h>
34 #include <deal.II/fe/fe_system.h>
35 #include <deal.II/fe/fe_tools.h>
36 #include <deal.II/fe/fe_values.h>
37
38 #include <deal.II/grid/cell_id_translator.h>
39 #include <deal.II/grid/filtered_iterator.h>
40 #include <deal.II/grid/tria_description.h>
41
42 #include <deal.II/matrix_free/evaluation_kernels.h>
43 #include <deal.II/matrix_free/evaluation_template_factory.h>
44 #include <deal.II/matrix_free/fe_evaluation.h>
45 #include <deal.II/matrix_free/fe_point_evaluation.h>
46 #include <deal.II/matrix_free/matrix_free.h>
47 #include <deal.II/matrix_free/tensor_product_kernels.h>
48 #include <deal.II/matrix_free/vector_access_internal.h>
49
50 #include <deal.II/multigrid/mg_tools.h>
51 #include <deal.II/multigrid/mg_transfer_global_coarsening.h>
52 #include <deal.II/multigrid/mg_transfer_matrix_free.templates.h>
53
54 #include <boost/algorithm/string/join.hpp>
55
56 #include <limits>
57
58 DEAL_II_NAMESPACE_OPEN
59
60 namespace internal
61 {
62 /**
63 * Helper class to select the right templated implementation.
64 *
65 * @note This class is similar to internal::FEEvaluationFactory
66 */
67 class CellTransferFactory
68 {
69 public:
70 static const unsigned int max_degree = 9;
71
72 CellTransferFactory(const unsigned int degree_fine,
73 const unsigned int degree_coarse)
74 : degree_fine(degree_fine)
75 , degree_coarse(degree_coarse)
76 {}
77
78 template <typename Fu, unsigned int deg = 1>
79 bool
80 run(Fu &fu)
81 {
82 if ((degree_fine == (2 * deg)) && (degree_coarse == deg))
83 fu.template run<2 * deg, deg>(); // h-MG (FE_Q)
84 else if ((degree_fine == (2 * deg + 1)) && (degree_coarse == deg))
85 fu.template run<2 * deg + 1, deg>(); // h-MG
86 else if ((degree_fine == deg) && (degree_coarse == std::max(deg / 2, 1u)))
87 {
88 constexpr unsigned int degree_coarse_used = std::max(deg / 2u, 1u);
89 fu.template run<deg, degree_coarse_used>(); // p-MG: bisection
90 }
91 else if ((degree_fine == deg) && (degree_coarse == deg))
92 fu.template run<deg, deg>(); // identity (nothing to do)
93 else if ((degree_fine == deg) && (degree_coarse == std::max(deg - 1, 1u)))
94 {
95 constexpr unsigned int degree_coarse_used = std::max(deg - 1u, 1u);
96 fu.template run<deg, degree_coarse_used>(); // p-MG: --
97 }
98 else if ((degree_fine == deg) && (degree_coarse == 1))
99 fu.template run<deg, 1>(); // p-MG: jump to 1
100 else if (deg < max_degree)
101 return run<Fu, std::min(deg + 1, max_degree)>(fu); // try next degree
102 else
103 {
104 // no match -> slow path
105 fu.template run<-1, -1>(degree_fine, degree_coarse);
106 return false; // indicate that slow path has been taken
107 }
108
109 return true; // indicate that fast path has been taken
110 }
111
112 private:
113 const unsigned int degree_fine;
114 const unsigned int degree_coarse;
115 };
116
117 /**
118 * Helper class containing the cell-wise prolongation operation.
119 */
120 template <int dim, typename Number, typename Number2>
121 class CellProlongator
122 {
123 public:
124 CellProlongator(const AlignedVector<Number> &prolongation_matrix,
125 const Number2 *evaluation_data_coarse,
126 Number2 *evaluation_data_fine)
127 : prolongation_matrix(prolongation_matrix)
128 , evaluation_data_coarse(evaluation_data_coarse)
129 , evaluation_data_fine(evaluation_data_fine)
130 {}
131
132 template <int degree_fine, int degree_coarse>
133 void
134 run(const unsigned int degree_fine_ = numbers::invalid_unsigned_int,
135 const unsigned int degree_coarse_ = numbers::invalid_unsigned_int)
136 {
137 Assert(prolongation_matrix.size() > 0, ExcNotImplemented());
138
139 internal::FEEvaluationImplBasisChange<
140 internal::evaluate_general,
141 internal::EvaluatorQuantity::value,
142 dim,
143 degree_coarse + 1,
144 degree_fine + 1>::do_forward(1,
145 prolongation_matrix,
146 evaluation_data_coarse,
147 evaluation_data_fine,
148 degree_coarse_ + 1,
149 degree_fine_ + 1);
150 }
151
152 void
153 run_full(const unsigned int n_dofs_fine, const unsigned int n_dofs_coarse)
154 {
155 AssertDimension(prolongation_matrix.size(), n_dofs_coarse * n_dofs_fine);
156
157 internal::FEEvaluationImplBasisChange<
158 internal::evaluate_general,
159 internal::EvaluatorQuantity::value,
160 1,
161 0,
162 0>::do_forward(1,
163 prolongation_matrix,
164 evaluation_data_coarse,
165 evaluation_data_fine,
166 n_dofs_coarse,
167 n_dofs_fine);
168 }
169
170 private:
171 const AlignedVector<Number> &prolongation_matrix;
172 const Number2 *evaluation_data_coarse;
173 Number2 *evaluation_data_fine;
174 };
175
176 /**
177 * Helper class containing the cell-wise restriction operation.
178 */
179 template <int dim, typename Number, typename Number2>
180 class CellRestrictor
181 {
182 public:
183 CellRestrictor(const AlignedVector<Number> &prolongation_matrix,
184 Number2 *evaluation_data_fine,
185 Number2 *evaluation_data_coarse)
186 : prolongation_matrix(prolongation_matrix)
187 , evaluation_data_fine(evaluation_data_fine)
188 , evaluation_data_coarse(evaluation_data_coarse)
189 {}
190
191 template <int degree_fine, int degree_coarse>
192 void
193 run(const unsigned int degree_fine_ = numbers::invalid_unsigned_int,
194 const unsigned int degree_coarse_ = numbers::invalid_unsigned_int)
195 {
196 Assert(prolongation_matrix.size() > 0, ExcNotImplemented());
197
198 internal::FEEvaluationImplBasisChange<
199 internal::evaluate_general,
200 internal::EvaluatorQuantity::value,
201 dim,
202 degree_coarse == 0 ? -1 : (degree_coarse + 1),
203 degree_fine == 0 ? -1 : (degree_fine + 1)>::
204 do_backward(1,
205 prolongation_matrix,
206 false,
207 evaluation_data_fine,
208 evaluation_data_coarse,
209 degree_coarse_ + 1,
210 degree_fine_ + 1);
211 }
212
213 void
214 run_full(const unsigned int n_dofs_fine, const unsigned int n_dofs_coarse)
215 {
216 AssertDimension(prolongation_matrix.size(), n_dofs_coarse * n_dofs_fine);
217
218 internal::FEEvaluationImplBasisChange<
219 internal::evaluate_general,
220 internal::EvaluatorQuantity::value,
221 1,
222 0,
223 0>::do_backward(1,
224 prolongation_matrix,
225 false,
226 evaluation_data_fine,
227 evaluation_data_coarse,
228 n_dofs_coarse,
229 n_dofs_fine);
230 }
231
232 private:
233 const AlignedVector<Number> &prolongation_matrix;
234 Number2 *evaluation_data_fine;
235 Number2 *evaluation_data_coarse;
236 };
237
238 class CellProlongatorTest
239 {
240 public:
241 template <int degree_fine, int degree_coarse>
242 void
243 run(const unsigned int = numbers::invalid_unsigned_int,
244 const unsigned int = numbers::invalid_unsigned_int)
245 {}
246 };
247
248 } // namespace internal
249
250
251
252 namespace internal
253 {
254 template <typename MeshType, typename OPType>
255 DEAL_II_CXX20_REQUIRES(concepts::is_triangulation_or_dof_handler<MeshType>)
256 void loop_over_active_or_level_cells(const MeshType &tria,
257 const unsigned int level,
258 const OPType &op)
259 {
260 if (level == numbers::invalid_unsigned_int)
261 {
262 for (const auto &cell : tria.active_cell_iterators())
263 if (cell->is_locally_owned())
264 op(cell);
265 }
266 else
267 {
268 for (const auto &cell : tria.cell_iterators_on_level(level))
269 if (cell->is_locally_owned_on_level())
270 op(cell);
271 }
272 }
273
274 template <int dim>
275 unsigned int
276 compute_shift_within_children(const unsigned int child,
277 const unsigned int fe_shift_1d,
278 const unsigned int fe_degree)
279 {
280 // we put the degrees of freedom of all child cells in lexicographic
281 // ordering
282 unsigned int c_tensor_index[dim];
283 unsigned int tmp = child;
284 for (unsigned int d = 0; d < dim; ++d)
285 {
286 c_tensor_index[d] = tmp % 2;
287 tmp /= 2;
288 }
289 const unsigned int n_child_dofs_1d = fe_degree + 1 + fe_shift_1d;
290 unsigned int factor = 1;
291 unsigned int shift = fe_shift_1d * c_tensor_index[0];
292 for (unsigned int d = 1; d < dim; ++d)
293 {
294 factor *= n_child_dofs_1d;
295 shift = shift + factor * fe_shift_1d * c_tensor_index[d];
296 }
297 return shift;
298 }
299
300 template <int dim>
301 void
302 get_child_offset(const unsigned int child,
303 const unsigned int fe_shift_1d,
304 const unsigned int fe_degree,
305 std::vector<unsigned int> &local_dof_indices)
306 {
307 const unsigned int n_child_dofs_1d = fe_degree + 1 + fe_shift_1d;
308 const unsigned int shift =
309 compute_shift_within_children<dim>(child, fe_shift_1d, fe_degree);
310 const unsigned int n_components =
311 local_dof_indices.size() / Utilities::fixed_power<dim>(fe_degree + 1);
312 const unsigned int n_scalar_cell_dofs =
313 Utilities::fixed_power<dim>(n_child_dofs_1d);
314 for (unsigned int c = 0, m = 0; c < n_components; ++c)
315 for (unsigned int k = 0; k < (dim > 2 ? (fe_degree + 1) : 1); ++k)
316 for (unsigned int j = 0; j < (dim > 1 ? (fe_degree + 1) : 1); ++j)
317 for (unsigned int i = 0; i < (fe_degree + 1); ++i, ++m)
318 local_dof_indices[m] = c * n_scalar_cell_dofs +
319 k * n_child_dofs_1d * n_child_dofs_1d +
320 j * n_child_dofs_1d + i + shift;
321 }
322
323 template <int dim>
324 std::vector<std::vector<unsigned int>>
325 get_child_offsets(const unsigned int n_dofs_per_cell_coarse,
326 const unsigned int fe_shift_1d,
327 const unsigned int fe_degree)
328 {
329 std::vector<std::vector<unsigned int>> cell_local_children_indices(
330 GeometryInfo<dim>::max_children_per_cell,
331 std::vector<unsigned int>(n_dofs_per_cell_coarse));
332 for (unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell; c++)
333 get_child_offset<dim>(c,
334 fe_shift_1d,
335 fe_degree,
336 cell_local_children_indices[c]);
337 return cell_local_children_indices;
338 }
339
340 template <int dim>
341 std::vector<std::vector<unsigned int>>
342 get_child_offsets_general(const unsigned int n_dofs_per_cell_coarse)
343 {
344 std::vector<std::vector<unsigned int>> cell_local_children_indices(
345 GeometryInfo<dim>::max_children_per_cell,
346 std::vector<unsigned int>(n_dofs_per_cell_coarse));
347 for (unsigned int c = 0, k = 0;
348 c < GeometryInfo<dim>::max_children_per_cell;
349 c++)
350 for (unsigned int d = 0; d < n_dofs_per_cell_coarse; ++d, ++k)
351 cell_local_children_indices[c][d] = k;
352 return cell_local_children_indices;
353 }
354
355 template <int dim, int spacedim>
356 std::unique_ptr<FiniteElement<1>>
357 create_1D_fe(const FiniteElement<dim, spacedim> &fe)
358 {
359 std::string fe_name = fe.get_name();
360 {
361 const std::size_t template_starts = fe_name.find_first_of('<');
362 Assert(fe_name[template_starts + 1] ==
363 (dim == 1 ? '1' : (dim == 2 ? '2' : '3')),
364 ExcInternalError());
365 fe_name[template_starts + 1] = '1';
366 }
367 return FETools::get_fe_by_name<1, 1>(fe_name);
368 }
369
370 template <int dim, int spacedim>
371 FullMatrix<double>
372 get_restriction_matrix(
373 const FiniteElement<dim, spacedim> &fe,
374 const unsigned int child,
375 RefinementCase<dim> ref_case = RefinementCase<dim>::isotropic_refinement)
376 {
377 auto matrix = fe.get_restriction_matrix(child, ref_case);
378
379 for (unsigned int c_other = 0; c_other < child; ++c_other)
380 {
381 const auto &matrix_other = fe.get_restriction_matrix(c_other, ref_case);
382 for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i)
383 {
384 if (fe.restriction_is_additive(i) == true)
385 continue;
386
387 bool do_zero = false;
388 for (unsigned int j = 0; j < fe.n_dofs_per_cell(); ++j)
389 if (std::fabs(matrix_other(i, j)) > 1e-12)
390 do_zero = true;
391
392 if (do_zero)
393 for (unsigned int j = 0; j < fe.n_dofs_per_cell(); ++j)
394 matrix(i, j) = 0.0;
395 }
396 }
397 return matrix;
398 }
399
400 template <int dim>
401 bool
402 use_fast_hanging_node_algorithm(const DoFHandler<dim> &dof_handler_coarse,
403 const unsigned int mg_level_coarse)
404 {
405 // algorithm is only needed on active levels
406 bool use_fast_hanging_node_algorithm =
407 mg_level_coarse == numbers::invalid_unsigned_int;
408
409 // algorithm can be only used on meshes consisting of hypercube and
410 // simplices
411 if (use_fast_hanging_node_algorithm)
412 {
413 const auto &reference_cells =
414 dof_handler_coarse.get_triangulation().get_reference_cells();
415 use_fast_hanging_node_algorithm =
416 std::all_of(reference_cells.begin(),
417 reference_cells.end(),
418 [](const auto &r) {
419 return r.is_hyper_cube() || r.is_simplex();
420 });
421 }
422
423 // local p-refinement is not supported
424 if (use_fast_hanging_node_algorithm)
425 {
426 const auto &fes = dof_handler_coarse.get_fe_collection();
427
428 use_fast_hanging_node_algorithm &=
429 std::all_of(fes.begin(), fes.end(), [&fes](const auto &fe) {
430 return fes[0].compare_for_domination(fe) ==
431 FiniteElementDomination::Domination::
432 either_element_can_dominate;
433 });
434 }
435
436 // check that all components are either supported or not
437 if (use_fast_hanging_node_algorithm)
438 {
439 const std::vector<std::vector<bool>> supported_components = internal::
440 MatrixFreeFunctions::HangingNodes<dim>::compute_supported_components(
441 dof_handler_coarse.get_fe_collection());
442
443 use_fast_hanging_node_algorithm &=
444 std::any_of(supported_components.begin(),
445 supported_components.end(),
446 [](const auto &supported_components_per_fe) {
447 return std::all_of(supported_components_per_fe.begin(),
448 supported_components_per_fe.end(),
449 [](const auto &a) {
450 return a == true;
451 });
452 });
453
454 use_fast_hanging_node_algorithm &= std::all_of(
455 supported_components.begin(),
456 supported_components.end(),
457 [](const auto &supported_components_per_fe) {
458 return std::all_of(supported_components_per_fe.begin(),
459 supported_components_per_fe.end(),
460 [&supported_components_per_fe](const auto &a) {
461 return a == supported_components_per_fe[0];
462 });
463 });
464 }
465
466 return use_fast_hanging_node_algorithm;
467 }
468
469
470
471 /**
472 * A class behaving like DoFCellAccessor. Intended to be used for locally
473 * relevant cell as a wrapper around DoFCellAccessor and for other cells
474 * behaving as if the cell would be available.
475 */
476 class FineDoFHandlerViewCell
477 {
478 public:
479 /**
480 * Constructor.
481 */
482 FineDoFHandlerViewCell(
483 const std::function<bool()> &has_children_function,
484 const std::function<void(std::vector<types::global_dof_index> &)>
485 &get_dof_indices_function,
486 const std::function<unsigned int()> &active_fe_index_function,
487 const std::function<std::uint8_t()> &refinement_case_function)
488 : has_children_function(has_children_function)
489 , get_dof_indices_function(get_dof_indices_function)
490 , active_fe_index_function(active_fe_index_function)
491 , refinement_case_function(refinement_case_function)
492 {}
493
494 /**
495 * Return if cell has child.
496 */
497 bool
498 has_children() const
499 {
500 return has_children_function();
501 }
502
503 /**
504 * Get DoF indices.
505 */
506 void
507 get_dof_indices(std::vector<types::global_dof_index> &dof_indices) const
508 {
509 get_dof_indices_function(dof_indices);
510 }
511 /**
512 * Get active FE index.
513 */
514 unsigned int
515 active_fe_index() const
516 {
517 return active_fe_index_function();
518 }
519 /**
520 * Get refinement choice.
521 */
522 std::uint8_t
523 refinement_case() const
524 {
525 return refinement_case_function();
526 }
527
528 private:
529 /**
530 * Lambda function returning whether cell has children.
531 */
532 const std::function<bool()> has_children_function;
533
534 /**
535 * Lambda function returning DoF indices.
536 */
537 const std::function<void(std::vector<types::global_dof_index> &)>
538 get_dof_indices_function;
539
540 /**
541 * Lambda function returning active FE index.
542 */
543 const std::function<unsigned int()> active_fe_index_function;
544
545 /**
546 * Lambda function returning the refinement choice.
547 */
548 const std::function<std::uint8_t()> refinement_case_function;
549 };
550
551
552
553 /**
554 * Base class for a view on fine DoFHandler.
555 *
556 * Implementations include:
557 * - IdentityFineDoFHandlerView: all cells on the fine mesh are either
558 * locally owned or ghosted; useful for p-multigrid without repartitioning;
559 * - FirstChildPolicyFineDoFHandlerView: parent cells are owned by the first
560 * child cell; useful for local smoothing with fast setup;
561 * - PermutationFineDoFHandlerView: fine mesh has the same cells as the
562 * coarse mesh but is partitioned differently; useful for p-multigrid with
563 * repartitioning;
564 * - GlobalCoarseningFineDoFHandlerView: cells on the coarse mesh are either
565 * refined or not; useful for global coarsening.
566 */
567 template <int dim>
568 class FineDoFHandlerViewBase
569 {
570 public:
571 virtual ~FineDoFHandlerViewBase() = default;
572
573 /**
574 * Return cell on fine DoFHandler.
575 */
576 virtual FineDoFHandlerViewCell
577 get_cell_view(
578 const typename DoFHandler<dim>::cell_iterator &cell) const = 0;
579
580 /**
581 * Return child of cell on fine DoFHandler.
582 */
583 virtual FineDoFHandlerViewCell
584 get_cell_view(const typename DoFHandler<dim>::cell_iterator &cell,
585 const unsigned int c) const = 0;
586 };
587
588
589
590 template <int dim>
591 class IdentityFineDoFHandlerView : public FineDoFHandlerViewBase<dim>
592 {
593 public:
594 IdentityFineDoFHandlerView(const DoFHandler<dim> &dof_handler_fine,
595 const unsigned int mg_level_fine)
596 : dof_handler_fine(dof_handler_fine)
597 , mg_level_fine(mg_level_fine)
598 {}
599
600 virtual ~IdentityFineDoFHandlerView() = default;
601
602 FineDoFHandlerViewCell
603 get_cell_view(
604 const typename DoFHandler<dim>::cell_iterator &cell) const override
605 {
606 return FineDoFHandlerViewCell(
607 []() {
608 DEAL_II_ASSERT_UNREACHABLE();
609 return false;
610 },
611 [this, cell](auto &dof_indices) {
612 if (this->mg_level_fine == numbers::invalid_unsigned_int)
613 cell->as_dof_handler_iterator(this->dof_handler_fine)
614 ->get_dof_indices(dof_indices);
615 else
616 cell->as_dof_handler_level_iterator(this->dof_handler_fine)
617 ->get_mg_dof_indices(dof_indices);
618 },
619 [this, cell]() {
620 if (this->mg_level_fine == numbers::invalid_unsigned_int)
621 return cell->as_dof_handler_iterator(dof_handler_fine)
622 ->active_fe_index();
623 else
624 return cell->as_dof_handler_level_iterator(this->dof_handler_fine)
625 ->active_fe_index();
626 },
627 []() {
628 DEAL_II_ASSERT_UNREACHABLE();
629 return 0;
630 });
631 }
632
633 FineDoFHandlerViewCell
634 get_cell_view(const typename DoFHandler<dim>::cell_iterator &,
635 const unsigned int) const override
636 {
637 DEAL_II_ASSERT_UNREACHABLE();
638
639 return FineDoFHandlerViewCell(
640 []() {
641 DEAL_II_ASSERT_UNREACHABLE();
642 return false;
643 },
644 [](auto &) { DEAL_II_ASSERT_UNREACHABLE(); },
645 []() {
646 DEAL_II_ASSERT_UNREACHABLE();
647 return 0;
648 },
649 []() {
650 DEAL_II_ASSERT_UNREACHABLE();
651 return 0;
652 });
653 }
654
655 private:
656 const DoFHandler<dim> &dof_handler_fine;
657 const unsigned int mg_level_fine;
658 };
659
660
661
662 template <int dim>
663 class FirstChildPolicyFineDoFHandlerView : public FineDoFHandlerViewBase<dim>
664 {
665 public:
666 FirstChildPolicyFineDoFHandlerView(const DoFHandler<dim> &dof_handler_fine,
667 const unsigned int mg_level_fine)
668 : dof_handler_fine(dof_handler_fine)
669 , mg_level_fine(mg_level_fine)
670 {}
671
672 virtual ~FirstChildPolicyFineDoFHandlerView() = default;
673
674 FineDoFHandlerViewCell
675 get_cell_view(
676 const typename DoFHandler<dim>::cell_iterator &cell) const override
677 {
678 return FineDoFHandlerViewCell(
679 [this, cell]() {
680 if (this->mg_level_fine == numbers::invalid_unsigned_int)
681 {
682 // create fine cell in two steps, since the coarse cell and
683 // the fine cell are associated to different Triangulation
684 // objects
685 const auto cell_id = cell->id();
686 const auto cell_fine_raw =
687 dof_handler_fine.get_triangulation().create_cell_iterator(
688 cell_id);
689 return cell_fine_raw->has_children();
690 }
691 else
692 {
693 return cell->has_children();
694 }
695 },
696 [this, cell](auto &dof_indices) {
697 if (this->mg_level_fine == numbers::invalid_unsigned_int)
698 {
699 const auto cell_id = cell->id();
700 const auto cell_fine_raw =
701 dof_handler_fine.get_triangulation().create_cell_iterator(
702 cell_id);
703 return cell_fine_raw->as_dof_handler_iterator(dof_handler_fine)
704 ->get_dof_indices(dof_indices);
705 }
706 else
707 {
708 cell->get_mg_dof_indices(dof_indices);
709 }
710 },
711 [this, cell]() {
712 if (this->mg_level_fine == numbers::invalid_unsigned_int)
713 {
714 const auto cell_id = cell->id();
715 const auto cell_fine_raw =
716 dof_handler_fine.get_triangulation().create_cell_iterator(
717 cell_id);
718 return cell_fine_raw->as_dof_handler_iterator(dof_handler_fine)
719 ->active_fe_index();
720 }
721 else
722 {
723 return cell->active_fe_index();
724 }
725 },
726 []() {
727 DEAL_II_ASSERT_UNREACHABLE(); // only children have the correct info
728 return 0;
729 });
730 }
731
732 FineDoFHandlerViewCell
733 get_cell_view(const typename DoFHandler<dim>::cell_iterator &cell,
734 const unsigned int c) const override
735 {
736 return FineDoFHandlerViewCell(
737 []() {
738 DEAL_II_ASSERT_UNREACHABLE();
739 return false;
740 },
741 [this, cell, c](auto &dof_indices) {
742 if (this->mg_level_fine == numbers::invalid_unsigned_int)
743 {
744 const auto cell_id = cell->id();
745 const auto cell_fine_raw = dof_handler_fine.get_triangulation()
746 .create_cell_iterator(cell_id)
747 ->child(c);
748 cell_fine_raw->as_dof_handler_iterator(dof_handler_fine)
749 ->get_dof_indices(dof_indices);
750 }
751 else
752 {
753 cell->child(c)->get_mg_dof_indices(dof_indices);
754 }
755 },
756 [this, cell, c]() {
757 if (this->mg_level_fine == numbers::invalid_unsigned_int)
758 {
759 const auto cell_id = cell->id();
760 const auto cell_fine_raw = dof_handler_fine.get_triangulation()
761 .create_cell_iterator(cell_id)
762 ->child(c);
763 return cell_fine_raw->as_dof_handler_iterator(dof_handler_fine)
764 ->active_fe_index();
765 }
766 else
767 {
768 return cell->child(c)->active_fe_index();
769 }
770 },
771 [this, cell]() {
772 if (this->mg_level_fine == numbers::invalid_unsigned_int)
773 {
774 const auto cell_id = cell->id();
775 const auto cell_raw =
776 dof_handler_fine.get_triangulation().create_cell_iterator(
777 cell_id);
778 return cell_raw->refinement_case();
779 }
780 else
781 {
782 return cell->refinement_case();
783 }
784 });
785 }
786
787 private:
788 const DoFHandler<dim> &dof_handler_fine;
789 const unsigned int mg_level_fine;
790 };
791
792
793
794 template <int dim>
795 class BlackBoxFineDoFHandlerView : public FineDoFHandlerViewBase<dim>
796 {
797 public:
798 BlackBoxFineDoFHandlerView(const DoFHandler<dim> &dof_handler_fine,
799 const DoFHandler<dim> &dof_handler_coarse,
800 const unsigned int mg_level_fine)
801 : dof_handler_fine(dof_handler_fine)
802 , dof_handler_coarse(dof_handler_coarse)
803 , mg_level_fine(mg_level_fine)
804 , communicator(
805 dof_handler_fine
806 .get_mpi_communicator() /*TODO: fix for different comms*/)
807 , cell_id_translator(
808 dof_handler_fine.get_triangulation().n_global_coarse_cells(),
809 dof_handler_fine.get_triangulation().n_global_levels())
810 {
811 AssertDimension(
812 dof_handler_fine.get_triangulation().n_global_coarse_cells(),
813 dof_handler_coarse.get_triangulation().n_global_coarse_cells());
814 AssertIndexRange(dof_handler_coarse.get_triangulation().n_global_levels(),
815 dof_handler_fine.get_triangulation().n_global_levels() +
816 1);
817 }
818
819 virtual ~BlackBoxFineDoFHandlerView() = default;
820
821 void
822 reinit(const IndexSet &is_dst_locally_owned,
823 const IndexSet &is_dst_remote_input,
824 const IndexSet &is_src_locally_owned,
825 const bool check_if_elements_in_is_dst_remote_exist = false)
826 {
827 IndexSet is_dst_remote = is_dst_remote_input;
828
829 if (check_if_elements_in_is_dst_remote_exist)
830 {
831 IndexSet is_dst_remote_potentially_relevant = is_dst_remote;
832 is_dst_remote.clear();
833
834 is_dst_remote_potentially_relevant.subtract_set(is_dst_locally_owned);
835
836 const std::vector<unsigned int> owning_ranks_of_ghosts =
837 Utilities::MPI::compute_index_owner(
838 is_dst_locally_owned,
839 is_dst_remote_potentially_relevant,
840 communicator);
841
842 for (unsigned i = 0;
843 i < is_dst_remote_potentially_relevant.n_elements();
844 ++i)
845 if (owning_ranks_of_ghosts[i] != numbers::invalid_unsigned_int)
846 is_dst_remote.add_index(
847 is_dst_remote_potentially_relevant.nth_index_in_set(i));
848 }
849
850 // determine owners of remote cells
851 const auto [is_dst_remote_owners, targets_with_indexset] =
852 Utilities::MPI::compute_index_owner_and_requesters(is_dst_locally_owned,
853 is_dst_remote,
854 communicator);
855
856
857 this->is_dst_locally_owned = is_dst_locally_owned;
858 this->is_dst_remote = is_dst_remote;
859 this->is_src_locally_owned = is_src_locally_owned;
860
861 #ifndef DEAL_II_WITH_MPI
862 Assert(targets_with_indexset.empty(), ExcInternalError());
863 #else
864
865 std::map<unsigned int, std::vector<types::global_dof_index>>
866 indices_to_be_sent;
867 std::vector<MPI_Request> requests;
868 requests.reserve(targets_with_indexset.size());
869 const unsigned int my_rank =
870 Utilities::MPI::this_mpi_process(communicator);
871
872 {
873 std::vector<types::global_dof_index> indices;
874
875 for (const auto &i : targets_with_indexset)
876 {
877 // Skip communication in case we would send to ourselves or when
878 // there are no indices to send (this can still happen in the run
879 // of the consensus algorithms above if the index spaces are
880 // sparse).
881 if (i.first == my_rank || i.second.begin() == i.second.end())
882 continue;
883
884 indices_to_be_sent[i.first] = {};
885 std::vector<types::global_dof_index> &buffer =
886 indices_to_be_sent[i.first];
887
888 for (auto cell_id : i.second)
889 {
890 typename DoFHandler<dim>::cell_iterator cell(
891 *dof_handler_fine.get_triangulation().create_cell_iterator(
892 cell_id_translator.to_cell_id(cell_id)),
893 &dof_handler_fine);
894
895 indices.resize(cell->get_fe().n_dofs_per_cell());
896
897 if (mg_level_fine == numbers::invalid_unsigned_int)
898 cell->get_dof_indices(indices);
899 else
900 cell->get_mg_dof_indices(indices);
901
902 buffer.push_back(cell->active_fe_index());
903 if (cell->level() != 0)
904 buffer.push_back(cell->parent()->refinement_case());
905 else
906 buffer.push_back(numbers::invalid_dof_index);
907 buffer.insert(buffer.end(), indices.begin(), indices.end());
908 }
909
910 requests.emplace_back();
911
912 const auto ierr_1 = MPI_Isend(
913 buffer.data(),
914 buffer.size(),
915 Utilities::MPI::mpi_type_id_for_type<decltype(*buffer.data())>,
916 i.first,
917 Utilities::MPI::internal::Tags::fine_dof_handler_view_reinit,
918 communicator,
919 &requests.back());
920 AssertThrowMPI(ierr_1);
921 }
922 }
923
924 // process local cells
925 {
926 std::map<unsigned int, std::vector<types::global_dof_index>>
927 rank_to_ids;
928 for (unsigned int i = 0; i < is_dst_remote_owners.size(); ++i)
929 rank_to_ids[is_dst_remote_owners[i]].push_back(
930 is_dst_remote.nth_index_in_set(i));
931
932 for (const auto &pair : rank_to_ids)
933 {
934 // above we skip messages sent to myself, so also skip MPI_Probe
935 if (pair.first == my_rank)
936 continue;
937
938 MPI_Status status;
939 const int ierr_1 = MPI_Probe(
940 MPI_ANY_SOURCE,
941 Utilities::MPI::internal::Tags::fine_dof_handler_view_reinit,
942 communicator,
943 &status);
944 AssertThrowMPI(ierr_1);
945
946 std::vector<types::global_dof_index> buffer;
947
948 int message_length;
949 const int ierr_2 = MPI_Get_count(
950 &status,
951 Utilities::MPI::mpi_type_id_for_type<decltype(*buffer.data())>,
952 &message_length);
953 AssertThrowMPI(ierr_2);
954
955 buffer.resize(message_length);
956
957 const int ierr_3 = MPI_Recv(
958 buffer.data(),
959 buffer.size(),
960 Utilities::MPI::mpi_type_id_for_type<decltype(*buffer.data())>,
961 status.MPI_SOURCE,
962 Utilities::MPI::internal::Tags::fine_dof_handler_view_reinit,
963 communicator,
964 MPI_STATUS_IGNORE);
965 AssertThrowMPI(ierr_3);
966
967 const unsigned int rank = status.MPI_SOURCE;
968
969 const auto ids = rank_to_ids[rank];
970
971 std::vector<types::global_dof_index> indices;
972
973 for (unsigned int i = 0, k = 0; i < ids.size(); ++i)
974 {
975 const unsigned int active_fe_index = buffer[k++];
976 const std::uint8_t refinement_case =
977 static_cast<std::uint8_t>(buffer[k++]);
978 indices.resize(
979 dof_handler_fine.get_fe(active_fe_index).n_dofs_per_cell());
980
981 for (unsigned int j = 0; j < indices.size(); ++j, ++k)
982 indices[j] = buffer[k];
983 remote_cell_info_map[ids[i]] = {active_fe_index,
984 indices,
985 refinement_case};
986 }
987 }
988
989 if (!requests.empty())
990 {
991 const int ierr_1 = MPI_Waitall(requests.size(),
992 requests.data(),
993 MPI_STATUSES_IGNORE);
994 AssertThrowMPI(ierr_1);
995 }
996 }
997 #endif
998 }
999
1000 FineDoFHandlerViewCell
1001 get_cell_view(
1002 const typename DoFHandler<dim>::cell_iterator &cell) const override
1003 {
1004 const auto id = this->cell_id_translator.translate(cell);
1005
1006 const bool is_cell_locally_owned =
1007 this->is_dst_locally_owned.is_element(id);
1008 const bool is_cell_remotely_owned = this->is_dst_remote.is_element(id);
1009
1010 const bool has_cell_any_children = [&]() {
1011 for (unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_cell;
1012 ++i)
1013 {
1014 const auto j = this->cell_id_translator.translate(cell, i);
1015
1016 if (this->is_dst_locally_owned.is_element(j))
1017 return true;
1018
1019 if (this->is_dst_remote.is_element(j))
1020 return true;
1021 }
1022
1023 AssertThrow(is_cell_locally_owned || is_cell_remotely_owned,
1024 ExcInternalError());
1025
1026 return false;
1027 }();
1028
1029 return FineDoFHandlerViewCell(
1030 [has_cell_any_children]() { return has_cell_any_children; },
1031 [cell, is_cell_locally_owned, is_cell_remotely_owned, id, this](
1032 auto &dof_indices) {
1033 if (is_cell_locally_owned)
1034 {
1035 typename DoFHandler<dim>::cell_iterator cell_fine(
1036 *dof_handler_fine.get_triangulation().create_cell_iterator(
1037 cell->id()),
1038 &dof_handler_fine);
1039 if (mg_level_fine == numbers::invalid_unsigned_int)
1040 cell_fine->get_dof_indices(dof_indices);
1041 else
1042 cell_fine->get_mg_dof_indices(dof_indices);
1043 }
1044 else if (is_cell_remotely_owned)
1045 {
1046 dof_indices = std::get<1>(remote_cell_info_map.at(id));
1047 }
1048 else
1049 {
1050 AssertThrow(false, ExcNotImplemented()); // should not happen!
1051 }
1052 },
1053 [cell, is_cell_locally_owned, is_cell_remotely_owned, id, this]()
1054 -> unsigned int {
1055 if (is_cell_locally_owned)
1056 {
1057 return (typename DoFHandler<dim>::cell_iterator(
1058 *dof_handler_fine.get_triangulation()
1059 .create_cell_iterator(cell->id()),
1060 &dof_handler_fine))
1061 ->active_fe_index();
1062 }
1063 else if (is_cell_remotely_owned)
1064 {
1065 return std::get<0>(remote_cell_info_map.at(id));
1066 }
1067 else
1068 {
1069 AssertThrow(false, ExcNotImplemented()); // should not happen!
1070 return 0;
1071 }
1072 },
1073 []() -> std::uint8_t {
1074 AssertThrow(false,
1075 ExcNotImplemented()); // only children hold correct info
1076 return 0;
1077 });
1078 }
1079
1080 FineDoFHandlerViewCell
1081 get_cell_view(const typename DoFHandler<dim>::cell_iterator &cell,
1082 const unsigned int c) const override
1083 {
1084 const auto id = this->cell_id_translator.translate(cell, c);
1085
1086 const bool is_cell_locally_owned =
1087 this->is_dst_locally_owned.is_element(id);
1088 const bool is_cell_remotely_owned = this->is_dst_remote.is_element(id);
1089
1090 return FineDoFHandlerViewCell(
1091 [cell]() {
1092 AssertThrow(false, ExcNotImplemented()); // currently we do not need
1093 // children of children
1094
1095 return false;
1096 },
1097 [cell, is_cell_locally_owned, is_cell_remotely_owned, c, id, this](
1098 auto &dof_indices) {
1099 if (is_cell_locally_owned)
1100 {
1101 const auto cell_fine =
1102 (typename DoFHandler<dim>::cell_iterator(
1103 *dof_handler_fine.get_triangulation().create_cell_iterator(
1104 cell->id()),
1105 &dof_handler_fine))
1106 ->child(c);
1107
1108 if (mg_level_fine == numbers::invalid_unsigned_int)
1109 cell_fine->get_dof_indices(dof_indices);
1110 else
1111 cell_fine->get_mg_dof_indices(dof_indices);
1112 }
1113 else if (is_cell_remotely_owned)
1114 {
1115 dof_indices = std::get<1>(remote_cell_info_map.at(id));
1116 }
1117 else
1118 {
1119 AssertThrow(false, ExcNotImplemented()); // should not happen!
1120 }
1121 },
1122 []() -> unsigned int {
1123 AssertThrow(false, ExcNotImplemented()); // currently we do not need
1124 // active_fe_index() for
1125 // children
1126
1127 return 0;
1128 },
1129 [cell, is_cell_locally_owned, is_cell_remotely_owned, id, this]() {
1130 if (is_cell_locally_owned &&
1131 (mg_level_fine == numbers::invalid_unsigned_int))
1132 {
1133 const auto cell_fine = (typename DoFHandler<dim>::cell_iterator(
1134 *dof_handler_fine.get_triangulation().create_cell_iterator(
1135 cell->id()),
1136 &dof_handler_fine));
1137 return cell_fine->refinement_case();
1138 }
1139 else if (is_cell_locally_owned)
1140 {
1141 return cell->refinement_case();
1142 }
1143
1144 else if (is_cell_remotely_owned)
1145 {
1146 return RefinementCase<dim>(
1147 std::get<2>(remote_cell_info_map.at(id)));
1148 }
1149 else
1150 {
1151 AssertThrow(false, ExcNotImplemented());
1152 }
1153 // return no refinement
1154 return RefinementCase<dim>(0);
1155 });
1156 }
1157
1158 private:
1159 const DoFHandler<dim> &dof_handler_fine;
1160 const DoFHandler<dim> &dof_handler_coarse;
1161 const unsigned int mg_level_fine;
1162
1163 protected:
1164 const MPI_Comm communicator;
1165 const CellIDTranslator<dim> cell_id_translator;
1166 IndexSet is_dst_locally_owned;
1167 IndexSet is_dst_remote;
1168
1169 private:
1170 IndexSet is_src_locally_owned;
1171
1172 std::map<types::global_cell_index,
1173 std::tuple<unsigned int,
1174 std::vector<types::global_dof_index>,
1175 std::uint8_t>>
1176 remote_cell_info_map;
1177 };
1178
1179 template <int dim>
1180 class GlobalCoarseningFineDoFHandlerView
1181 : public BlackBoxFineDoFHandlerView<dim>
1182 {
1183 public:
1184 GlobalCoarseningFineDoFHandlerView(const DoFHandler<dim> &dof_handler_dst,
1185 const DoFHandler<dim> &dof_handler_src,
1186 const unsigned int mg_level_fine,
1187 const unsigned int mg_level_coarse)
1188 : BlackBoxFineDoFHandlerView<dim>(dof_handler_dst,
1189 dof_handler_src,
1190 mg_level_fine)
1191 {
1192 Assert((mg_level_fine == numbers::invalid_unsigned_int &&
1193 mg_level_coarse == numbers::invalid_unsigned_int) ||
1194 (mg_level_coarse + 1 == mg_level_fine),
1195 ExcNotImplemented());
1196
1197 // get reference to triangulations
1198 const auto &tria_dst = dof_handler_dst.get_triangulation();
1199 const auto &tria_src = dof_handler_src.get_triangulation();
1200
1201 // create index sets
1202 IndexSet is_dst_locally_owned(this->cell_id_translator.size());
1203 IndexSet is_dst_remote(this->cell_id_translator.size());
1204 IndexSet is_src_locally_owned(this->cell_id_translator.size());
1205
1206 const auto fine_operation = [&](const auto &cell) {
1207 is_dst_locally_owned.add_index(
1208 this->cell_id_translator.translate(cell));
1209 };
1210
1211 const auto coarse_operation = [&](const auto &cell) {
1212 is_src_locally_owned.add_index(
1213 this->cell_id_translator.translate(cell));
1214
1215 // in the case of global coarsening identity transfer is possible
1216 if (mg_level_coarse == numbers::invalid_unsigned_int)
1217 is_dst_remote.add_index(this->cell_id_translator.translate(cell));
1218
1219 if (cell->level() + 1u == tria_dst.n_global_levels())
1220 return;
1221
1222 for (unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_cell;
1223 ++i)
1224 is_dst_remote.add_index(this->cell_id_translator.translate(cell, i));
1225 };
1226
1227 loop_over_active_or_level_cells(tria_dst, mg_level_fine, fine_operation);
1228 loop_over_active_or_level_cells(tria_src,
1229 mg_level_coarse,
1230 coarse_operation);
1231
1232 this->reinit(is_dst_locally_owned,
1233 is_dst_remote,
1234 is_src_locally_owned,
1235 true);
1236
1237 // check if meshes are compatible
1238 if (mg_level_coarse == numbers::invalid_unsigned_int)
1239 {
1240 std::vector<std::string> not_found_cells_local;
1241
1242 const auto coarse_operation_check = [&](const auto &cell) {
1243 bool flag = false;
1244
1245 const auto index = this->cell_id_translator.translate(cell);
1246
1247 flag |= this->is_dst_remote.is_element(index) ||
1248 this->is_dst_locally_owned.is_element(index);
1249
1250 if (cell->level() + 1u != tria_dst.n_global_levels())
1251 {
1252 for (unsigned int i = 0;
1253 i < GeometryInfo<dim>::max_children_per_cell;
1254 ++i)
1255 {
1256 const auto index =
1257 this->cell_id_translator.translate(cell, i);
1258
1259 flag |= this->is_dst_remote.is_element(index) ||
1260 this->is_dst_locally_owned.is_element(index);
1261 }
1262 }
1263
1264 if (!flag)
1265 not_found_cells_local.emplace_back(cell->id().to_string());
1266 };
1267
1268 loop_over_active_or_level_cells(tria_src,
1269 mg_level_coarse,
1270 coarse_operation_check);
1271
1272 auto not_found_cells =
1273 Utilities::MPI::reduce<std::vector<std::string>>(
1274 not_found_cells_local,
1275 this->communicator,
1276 [](const auto &a, const auto &b) {
1277 auto result = a;
1278 result.insert(result.end(), b.begin(), b.end());
1279 return result;
1280 },
1281 0);
1282
1283 if (Utilities::MPI::this_mpi_process(this->communicator) == 0 &&
1284 !not_found_cells.empty())
1285 {
1286 std::sort(not_found_cells.begin(), not_found_cells.end());
1287
1288 const std::string str =
1289 boost::algorithm::join(not_found_cells, ", ");
1290
1291 AssertThrow(
1292 false,
1293 ExcMessage(
1294 "Problem setting up two-level transfer operator, since coarse triangulation "
1295 "seems to be obtainable by simple coarsening. Following coarse cells "
1296 "or children cells could not be found in the fine mesh: " +
1297 str + "."));
1298 }
1299 }
1300 }
1301
1302 virtual ~GlobalCoarseningFineDoFHandlerView() = default;
1303 };
1304
1305
1306
1307 template <int dim>
1308 class PermutationFineDoFHandlerView : public BlackBoxFineDoFHandlerView<dim>
1309 {
1310 public:
1311 PermutationFineDoFHandlerView(const DoFHandler<dim> &dof_handler_dst,
1312 const DoFHandler<dim> &dof_handler_src,
1313 const unsigned int mg_level_fine,
1314 const unsigned int mg_level_coarse)
1315 : BlackBoxFineDoFHandlerView<dim>(dof_handler_dst,
1316 dof_handler_src,
1317 mg_level_fine)
1318 {
1319 // get reference to triangulations
1320 const auto &tria_dst = dof_handler_dst.get_triangulation();
1321 const auto &tria_src = dof_handler_src.get_triangulation();
1322
1323 // create index sets
1324 IndexSet is_dst_locally_owned(this->cell_id_translator.size());
1325 IndexSet is_dst_remote(this->cell_id_translator.size());
1326 IndexSet is_src_locally_owned(this->cell_id_translator.size());
1327
1328 const auto fine_operation = [&](const auto &cell) {
1329 is_dst_locally_owned.add_index(
1330 this->cell_id_translator.translate(cell));
1331 };
1332
1333 const auto coarse_operation = [&](const auto &cell) {
1334 is_src_locally_owned.add_index(
1335 this->cell_id_translator.translate(cell));
1336 is_dst_remote.add_index(this->cell_id_translator.translate(cell));
1337 };
1338
1339 loop_over_active_or_level_cells(tria_dst, mg_level_fine, fine_operation);
1340 loop_over_active_or_level_cells(tria_src,
1341 mg_level_coarse,
1342 coarse_operation);
1343
1344 this->reinit(is_dst_locally_owned,
1345 is_dst_remote,
1346 is_src_locally_owned,
1347 false);
1348 }
1349
1350 virtual ~PermutationFineDoFHandlerView() = default;
1351 };
1352
1353
1354
1355 template <int dim, int spacedim>
1356 bool
1357 p_transfer_involves_repartitioning(
1358 const DoFHandler<dim, spacedim> &dof_handler_fine,
1359 const DoFHandler<dim, spacedim> &dof_handler_coarse,
1360 const unsigned int mg_level_fine,
1361 const unsigned int mg_level_coarse)
1362 {
1363 if (mg_level_fine != mg_level_coarse)
1364 return true;
1365
1366 if (&dof_handler_fine.get_triangulation() !=
1367 &dof_handler_coarse.get_triangulation())
1368 return true;
1369
1370 return false;
1371 }
1372
1373
1374
1375 template <int dim, int spacedim>
1376 bool
1377 h_transfer_uses_first_child_policy(
1378 const DoFHandler<dim, spacedim> &dof_handler_fine,
1379 const DoFHandler<dim, spacedim> &dof_handler_coarse,
1380 const unsigned int mg_level_fine,
1381 const unsigned int mg_level_coarse)
1382 {
1383 if (mg_level_fine == numbers::invalid_unsigned_int &&
1384 mg_level_coarse == numbers::invalid_unsigned_int)
1385 {
1386 // two DoFHandlers
1387
1388 bool flag = true;
1389
1390 loop_over_active_or_level_cells(
1391 dof_handler_coarse.get_triangulation(),
1392 mg_level_coarse,
1393 [&](const auto &cell) {
1394 const auto cell_id = cell->id();
1395
1396 if (dof_handler_fine.get_triangulation().contains_cell(cell_id) ==
1397 false)
1398 {
1399 flag = false;
1400 }
1401 else
1402 {
1403 const auto cell_fine =
1404 dof_handler_fine.get_triangulation().create_cell_iterator(
1405 cell_id);
1406
1407 if (cell_fine->is_active())
1408 {
1409 if (cell_fine->subdomain_id() != cell->subdomain_id())
1410 flag = false;
1411 }
1412 else
1413 {
1414 Assert(
1415 cell_fine->child(0)->is_active(),
1416 ExcMessage(
1417 "In building a transfer operator, we are "
1418 "expecting that a cell is not or at most once refined "
1419 "on the other multigrid level. But it is refined more than once. "
1420 "Are you trying to build a transfer operator across "
1421 "more than one level of mesh refinement?"));
1422 if (cell_fine->child(0)->subdomain_id() !=
1423 cell->subdomain_id())
1424 flag = false;
1425 }
1426 }
1427 });
1428
1429 return Utilities::MPI::min(static_cast<unsigned int>(flag),
1430 dof_handler_fine.get_mpi_communicator()) ==
1431 1;
1432 }
1433 else
1434 {
1435 // single DoFHandler
1436 if (mg_level_fine == numbers::invalid_unsigned_int ||
1437 mg_level_coarse == numbers::invalid_unsigned_int)
1438 return false;
1439
1440 if (mg_level_coarse + 1 != mg_level_fine)
1441 return false;
1442
1443 if (&dof_handler_fine != &dof_handler_coarse)
1444 return false;
1445
1446 return true;
1447 }
1448 }
1449
1450
1451
1452 template <int dim>
1453 bool
1454 fe_has_tp_structure(const FiniteElement<dim> &fe)
1455 {
1456 return fe.n_base_elements() == 1 &&
1457 ((dynamic_cast<const FE_Q<dim> *>(&fe.base_element(0)) != nullptr) ||
1458 (dynamic_cast<const FE_DGQ<dim> *>(&fe.base_element(0)) !=
1459 nullptr) ||
1460 (dynamic_cast<const FE_Q_iso_Q1<dim> *>(&fe.base_element(0)) !=
1461 nullptr));
1462 }
1463
1464
1465
1466 class MGTwoLevelTransferImplementation
1467 {
1468 /**
1469 * Compute weights.
1470 */
1471 template <int dim, typename Number, typename MemorySpace>
1472 static void
1473 setup_weights(
1474 const dealii::AffineConstraints<Number> &constraints_fine,
1475 MGTwoLevelTransfer<
1476 dim,
1477 LinearAlgebra::distributed::Vector<Number, MemorySpace>> &transfer,
1478 const bool is_feq)
1479 {
1480 if (transfer.fine_element_is_continuous == false)
1481 return; // nothing to do
1482
1483 // 1) compute weights globally
1484 LinearAlgebra::distributed::Vector<Number> weight_vector;
1485
1486 weight_vector.reinit(transfer.partitioner_fine);
1487
1488 // ... compute valence of DoFs
1489 for (const auto i : transfer.constraint_info_fine.dof_indices)
1490 weight_vector.local_element(i) += 1;
1491 weight_vector.compress(VectorOperation::add);
1492
1493 // ... invert valence
1494 for (unsigned int i = 0; i < weight_vector.locally_owned_size(); ++i)
1495 if (weight_vector.local_element(i) > 0.)
1496 weight_vector.local_element(i) =
1497 Number(1.) / weight_vector.local_element(i);
1498
1499 // ... clear constrained indices
1500 for (const auto &i : constraints_fine.get_lines())
1501 if (weight_vector.locally_owned_elements().is_element(i.index))
1502 weight_vector[i.index] = 0.0;
1503
1504 weight_vector.update_ghost_values();
1505
1506 // 2) store data cell-wise a DG format and try to compress
1507 const unsigned int n_lanes = VectorizedArray<Number>::size();
1508 std::size_t n_batches = 0;
1509 for (const auto &scheme : transfer.schemes)
1510 n_batches += (scheme.n_coarse_cells + n_lanes - 1) / n_lanes;
1511
1512 transfer.weights.clear();
1513 transfer.weights.reserve(n_batches * Utilities::pow(3, dim));
1514 transfer.weights_start.clear();
1515 transfer.weights_start.reserve(n_batches + 1);
1516 transfer.weights_are_compressed.clear();
1517 transfer.weights_are_compressed.resize(n_batches);
1518
1519 AlignedVector<VectorizedArray<Number>> local_weights;
1520 std::array<VectorizedArray<Number>, Utilities::pow(3, dim)>
1521 local_weights_compressed;
1522 unsigned int offset = 0;
1523
1524 // ... loop over cells
1525 for (const auto &scheme : transfer.schemes)
1526 {
1527 local_weights.resize(scheme.n_dofs_per_cell_fine);
1528 for (unsigned int cell = 0; cell < scheme.n_coarse_cells;
1529 cell += n_lanes)
1530 {
1531 transfer.weights_start.push_back(transfer.weights.size());
1532
1533 const unsigned int n_lanes_filled =
1534 (cell + n_lanes > scheme.n_coarse_cells) ?
1535 (scheme.n_coarse_cells - cell) :
1536 n_lanes;
1537
1538 for (unsigned int v = 0; v < n_lanes_filled;
1539 ++v, offset += scheme.n_dofs_per_cell_fine)
1540 for (unsigned int i = 0; i < scheme.n_dofs_per_cell_fine; ++i)
1541 local_weights[i][v] = weight_vector.local_element(
1542 transfer.constraint_info_fine.dof_indices[offset + i]);
1543
1544 const bool can_compress_weights =
1545 is_feq && compute_weights_fe_q_dofs_by_entity<dim, -1>(
1546 local_weights.data(),
1547 transfer.n_components,
1548 scheme.degree_fine + 1,
1549 local_weights_compressed.data());
1550
1551 if (can_compress_weights && scheme.degree_fine > 1)
1552 {
1553 for (const VectorizedArray<Number> weight :
1554 local_weights_compressed)
1555 transfer.weights.push_back(weight);
1556 transfer
1557 .weights_are_compressed[transfer.weights_start.size() - 1] =
1558 1;
1559 }
1560 else
1561 for (const VectorizedArray<Number> weight : local_weights)
1562 transfer.weights.push_back(weight);
1563 }
1564 }
1565 AssertDimension(transfer.weights_start.size(), n_batches);
1566 }
1567
1568
1569
1570 public:
1571 template <int dim, typename Number>
1572 static std::shared_ptr<const Utilities::MPI::Partitioner>
1573 create_coarse_partitioner(
1574 const DoFHandler<dim> &dof_handler_coarse,
1575 const dealii::AffineConstraints<Number> &constraints_coarse,
1576 const unsigned int mg_level_coarse)
1577 {
1578 IndexSet locally_relevant_dofs =
1579 (mg_level_coarse == numbers::invalid_unsigned_int) ?
1580 DoFTools::extract_locally_active_dofs(dof_handler_coarse) :
1581 DoFTools::extract_locally_active_level_dofs(dof_handler_coarse,
1582 mg_level_coarse);
1583
1584 std::vector<types::global_dof_index> locally_relevant_dofs_temp;
1585
1586 for (const auto i : locally_relevant_dofs)
1587 {
1588 if (locally_relevant_dofs.is_element(i) == false)
1589 locally_relevant_dofs_temp.emplace_back(i);
1590
1591 const auto constraints = constraints_coarse.get_constraint_entries(i);
1592
1593 if (constraints)
1594 for (const auto &p : *constraints)
1595 if (locally_relevant_dofs.is_element(p.first) == false)
1596 locally_relevant_dofs_temp.emplace_back(p.first);
1597 }
1598
1599 std::sort(locally_relevant_dofs_temp.begin(),
1600 locally_relevant_dofs_temp.end());
1601 locally_relevant_dofs.add_indices(locally_relevant_dofs_temp.begin(),
1602 locally_relevant_dofs_temp.end());
1603
1604 return std::make_shared<Utilities::MPI::Partitioner>(
1605 mg_level_coarse == numbers::invalid_unsigned_int ?
1606 dof_handler_coarse.locally_owned_dofs() :
1607 dof_handler_coarse.locally_owned_mg_dofs(mg_level_coarse),
1608 locally_relevant_dofs,
1609 dof_handler_coarse.get_mpi_communicator());
1610 }
1611
1612
1613
1614 template <int dim, typename Number, typename MemorySpace>
1615 static void
1616 reinit_geometric_transfer(
1617 const DoFHandler<dim> &dof_handler_fine,
1618 const DoFHandler<dim> &dof_handler_coarse,
1619 const dealii::AffineConstraints<Number> &constraints_fine,
1620 const dealii::AffineConstraints<Number> &constraints_coarse,
1621 const unsigned int mg_level_fine,
1622 const unsigned int mg_level_coarse,
1623 MGTwoLevelTransfer<
1624 dim,
1625 LinearAlgebra::distributed::Vector<Number, MemorySpace>> &transfer)
1626 {
1627 Assert((mg_level_fine == numbers::invalid_unsigned_int &&
1628 mg_level_coarse == numbers::invalid_unsigned_int) ||
1629 (mg_level_coarse + 1 == mg_level_fine),
1630 ExcNotImplemented());
1631
1632 AssertDimension(constraints_fine.n_inhomogeneities(), 0);
1633 AssertDimension(constraints_coarse.n_inhomogeneities(), 0);
1634
1635 transfer.dof_handler_fine = &dof_handler_fine;
1636 transfer.mg_level_fine = mg_level_fine;
1637
1638 std::unique_ptr<FineDoFHandlerViewBase<dim>> dof_handler_fine_view;
1639
1640 if (internal::h_transfer_uses_first_child_policy(dof_handler_fine,
1641 dof_handler_coarse,
1642 mg_level_fine,
1643 mg_level_coarse))
1644 dof_handler_fine_view =
1645 std::make_unique<FirstChildPolicyFineDoFHandlerView<dim>>(
1646 dof_handler_fine, mg_level_fine);
1647 else
1648 dof_handler_fine_view =
1649 std::make_unique<GlobalCoarseningFineDoFHandlerView<dim>>(
1650 dof_handler_fine,
1651 dof_handler_coarse,
1652 mg_level_fine,
1653 mg_level_coarse);
1654
1655 // gather ranges for active FE indices on both fine and coarse dofhandlers
1656 std::array<unsigned int, 2> min_active_fe_indices = {
1657 {std::numeric_limits<unsigned int>::max(),
1658 std::numeric_limits<unsigned int>::max()}};
1659 std::array<unsigned int, 2> max_active_fe_indices = {{0, 0}};
1660
1661 loop_over_active_or_level_cells(
1662 dof_handler_fine, mg_level_fine, [&](const auto &cell) {
1663 min_active_fe_indices[0] =
1664 std::min<unsigned int>(min_active_fe_indices[0],
1665 cell->active_fe_index());
1666 max_active_fe_indices[0] =
1667 std::max<unsigned int>(max_active_fe_indices[0],
1668 cell->active_fe_index());
1669 });
1670
1671 loop_over_active_or_level_cells(
1672 dof_handler_coarse, mg_level_coarse, [&](const auto &cell) {
1673 min_active_fe_indices[1] =
1674 std::min<unsigned int>(min_active_fe_indices[1],
1675 cell->active_fe_index());
1676 max_active_fe_indices[1] =
1677 std::max<unsigned int>(max_active_fe_indices[1],
1678 cell->active_fe_index());
1679 });
1680
1681 const auto comm = dof_handler_fine.get_mpi_communicator();
1682
1683 Assert(comm == dof_handler_coarse.get_mpi_communicator(),
1684 ExcNotImplemented());
1685
1686 ArrayView<unsigned int> temp_min(min_active_fe_indices);
1687 ArrayView<unsigned int> temp_max(max_active_fe_indices);
1688 Utilities::MPI::min(temp_min, comm, temp_min);
1689 Utilities::MPI::max(temp_max, comm, temp_max);
1690
1691 // make sure that hp is used neither on the coarse nor on the fine
1692 // dofhandler
1693 AssertDimension(min_active_fe_indices[0], max_active_fe_indices[0]);
1694 AssertDimension(min_active_fe_indices[1], max_active_fe_indices[1]);
1695
1696 const auto reference_cell = dof_handler_fine.get_fe(0).reference_cell();
1697 // set up mg-schemes
1698 // (0) no refinement -> identity
1699 // (1) h-refinement
1700 // (2) h-refinement choice II <- e.g. for Tets
1701 // .
1702 // .
1703 // .
1704 transfer.schemes.resize(1 +
1705 reference_cell.n_isotropic_refinement_choices());
1706
1707 const unsigned int fe_index_fine = min_active_fe_indices[0];
1708 const unsigned int fe_index_coarse = min_active_fe_indices[1];
1709
1710 const auto &fe_fine = dof_handler_fine.get_fe(fe_index_fine);
1711 const auto &fe_coarse = dof_handler_coarse.get_fe(fe_index_coarse);
1712
1713 // extract number of components
1714 AssertDimension(fe_fine.n_components(), fe_coarse.n_components());
1715
1716 transfer.n_components = fe_fine.n_components();
1717
1718 // helper function: to process the fine level cells; function @p fu_non_refined is
1719 // performed on cells that are not refined and @fu_refined is performed on
1720 // children of cells that are refined
1721 const auto process_cells = [&](const auto &fu_non_refined,
1722 const auto &fu_refined) {
1723 loop_over_active_or_level_cells(
1724 dof_handler_coarse, mg_level_coarse, [&](const auto &cell_coarse) {
1725 if (mg_level_coarse == numbers::invalid_unsigned_int)
1726 {
1727 // get a reference to the equivalent cell on the fine
1728 // triangulation
1729 const auto cell_coarse_on_fine_mesh =
1730 dof_handler_fine_view->get_cell_view(cell_coarse);
1731
1732 // check if cell has children
1733 if (cell_coarse_on_fine_mesh.has_children())
1734 // ... cell has children -> process children
1735 for (unsigned int c = 0;
1736 c < GeometryInfo<dim>::max_children_per_cell;
1737 c++)
1738 fu_refined(cell_coarse,
1739 dof_handler_fine_view->get_cell_view(cell_coarse,
1740 c),
1741 c);
1742 else // ... cell has no children -> process cell
1743 fu_non_refined(cell_coarse, cell_coarse_on_fine_mesh);
1744 }
1745 else
1746 {
1747 // check if cell has children
1748 if (cell_coarse->has_children())
1749 // ... cell has children -> process children
1750 for (unsigned int c = 0;
1751 c < GeometryInfo<dim>::max_children_per_cell;
1752 c++)
1753 fu_refined(cell_coarse,
1754 dof_handler_fine_view->get_cell_view(cell_coarse,
1755 c),
1756 c);
1757 }
1758 });
1759 };
1760
1761 // check if FE is the same
1762 AssertDimension(fe_coarse.n_dofs_per_cell(), fe_fine.n_dofs_per_cell());
1763
1764
1765 const bool is_feq = fe_fine.n_base_elements() == 1 &&
1766 ((dynamic_cast<const FE_Q<dim> *>(
1767 &fe_fine.base_element(0)) != nullptr));
1768
1769 const bool has_tp_structure = fe_has_tp_structure(fe_fine);
1770
1771 for (auto &scheme : transfer.schemes)
1772 {
1773 // number of dofs on coarse and fine cells
1774 scheme.n_dofs_per_cell_coarse = fe_coarse.n_dofs_per_cell();
1775 scheme.n_dofs_per_cell_fine =
1776 is_feq ? (fe_fine.n_components() *
1777 Utilities::pow(2 * fe_fine.degree + 1, dim)) :
1778 (fe_coarse.n_dofs_per_cell() *
1779 GeometryInfo<dim>::max_children_per_cell);
1780
1781 // degree of FE on coarse and fine cell
1782 scheme.degree_coarse = fe_coarse.degree;
1783 scheme.degree_fine =
1784 is_feq ? (fe_coarse.degree * 2) : (fe_coarse.degree * 2 + 1);
1785
1786 // reset number of coarse cells
1787 scheme.n_coarse_cells = 0;
1788 }
1789 // correct for first scheme
1790 transfer.schemes[0].n_dofs_per_cell_fine = fe_coarse.n_dofs_per_cell();
1791 transfer.schemes[0].degree_fine = fe_coarse.degree;
1792
1793 // continuous or discontinuous
1794 transfer.fine_element_is_continuous = fe_fine.n_dofs_per_vertex() > 0;
1795 std::uint8_t current_refinement_case = static_cast<std::uint8_t>(-1);
1796
1797 // count coarse cells for each scheme (0, 1, ...)
1798 {
1799 // count by looping over all coarse cells
1800 process_cells(
1801 [&](const auto &, const auto &) {
1802 transfer.schemes[0].n_coarse_cells++;
1803 },
1804 [&](const auto &, const auto &cell_fine, const auto c) {
1805 std::uint8_t refinement_case = cell_fine.refinement_case();
1806 // Assert triggers if cell has no children
1807 if (reference_cell == ReferenceCells::Tetrahedron)
1808 Assert(RefinementCase<dim>(refinement_case) ==
1809 RefinementCase<dim>(static_cast<std::uint8_t>(
1810 IsotropicRefinementChoice::cut_tet_68)) ||
1811 RefinementCase<dim>(refinement_case) ==
1812 RefinementCase<dim>(static_cast<std::uint8_t>(
1813 IsotropicRefinementChoice::cut_tet_57)) ||
1814 RefinementCase<dim>(refinement_case) ==
1815 RefinementCase<dim>(static_cast<std::uint8_t>(
1816 IsotropicRefinementChoice::cut_tet_49)),
1817 ExcNotImplemented());
1818 else
1819 {
1820 Assert(RefinementCase<dim>(refinement_case) ==
1821 RefinementCase<dim>::isotropic_refinement,
1822 ExcNotImplemented());
1823 refinement_case = 1;
1824 }
1825
1826 if (c == 0)
1827 {
1828 transfer.schemes[refinement_case].n_coarse_cells++;
1829 current_refinement_case = refinement_case;
1830 }
1831 else
1832 // Check that all children have the same refinement case
1833 AssertThrow(current_refinement_case == refinement_case,
1834 ExcNotImplemented());
1835 });
1836 }
1837
1838
1839 const auto cell_local_children_indices =
1840 (has_tp_structure) ?
1841 get_child_offsets<dim>(transfer.schemes[0].n_dofs_per_cell_coarse,
1842 is_feq ? fe_fine.degree : (fe_fine.degree + 1),
1843 fe_fine.degree) :
1844 get_child_offsets_general<dim>(
1845 transfer.schemes[0].n_dofs_per_cell_coarse);
1846
1847 std::vector<unsigned int> n_dof_indices_fine(transfer.schemes.size() + 1);
1848 std::vector<unsigned int> n_dof_indices_coarse(transfer.schemes.size() +
1849 1);
1850
1851 for (unsigned int i = 0; i < transfer.schemes.size(); ++i)
1852 {
1853 n_dof_indices_fine[i + 1] = transfer.schemes[i].n_dofs_per_cell_fine *
1854 transfer.schemes[i].n_coarse_cells;
1855 n_dof_indices_coarse[i + 1] =
1856 transfer.schemes[i].n_dofs_per_cell_coarse *
1857 transfer.schemes[i].n_coarse_cells;
1858 }
1859
1860 for (unsigned int i = 0; i < transfer.schemes.size(); ++i)
1861 {
1862 n_dof_indices_fine[i + 1] += n_dof_indices_fine[i];
1863 n_dof_indices_coarse[i + 1] += n_dof_indices_coarse[i];
1864 }
1865
1866 // indices
1867 {
1868 std::vector<types::global_dof_index> local_dof_indices(
1869 transfer.schemes[0].n_dofs_per_cell_coarse);
1870
1871 // ---------------------- lexicographic_numbering ----------------------
1872 std::vector<unsigned int> lexicographic_numbering_fine;
1873 std::vector<unsigned int> lexicographic_numbering_coarse;
1874 if (has_tp_structure)
1875 {
1876 const Quadrature<1> dummy_quadrature(
1877 std::vector<Point<1>>(1, Point<1>()));
1878 internal::MatrixFreeFunctions::ShapeInfo<Number> shape_info;
1879 shape_info.reinit(dummy_quadrature, fe_fine, 0);
1880 lexicographic_numbering_fine = shape_info.lexicographic_numbering;
1881 shape_info.reinit(dummy_quadrature, fe_coarse, 0);
1882 lexicographic_numbering_coarse = shape_info.lexicographic_numbering;
1883 }
1884 else
1885 {
1886 const auto dummy_quadrature =
1887 reference_cell.template get_gauss_type_quadrature<dim>(1);
1888 internal::MatrixFreeFunctions::ShapeInfo<Number> shape_info;
1889 shape_info.reinit(dummy_quadrature, fe_fine, 0);
1890 lexicographic_numbering_fine = shape_info.lexicographic_numbering;
1891 shape_info.reinit(dummy_quadrature, fe_coarse, 0);
1892 lexicographic_numbering_coarse = shape_info.lexicographic_numbering;
1893 }
1894
1895 // ------------------------------ indices ------------------------------
1896 std::vector<types::global_dof_index> level_dof_indices_coarse(
1897 transfer.schemes[0].n_dofs_per_cell_fine);
1898 std::vector<types::global_dof_index> level_dof_indices_fine(
1899 transfer.schemes[1].n_dofs_per_cell_fine);
1900
1901
1902 unsigned int n_coarse_cells_total = 0;
1903 for (const auto &scheme : transfer.schemes)
1904 n_coarse_cells_total += scheme.n_coarse_cells;
1905
1906 transfer.constraint_info_coarse.reinit(
1907 dof_handler_coarse,
1908 n_coarse_cells_total,
1909 constraints_coarse.n_constraints() > 0 &&
1910 use_fast_hanging_node_algorithm(dof_handler_coarse,
1911 mg_level_coarse));
1912 transfer.constraint_info_coarse.set_locally_owned_indices(
1913 (mg_level_coarse == numbers::invalid_unsigned_int) ?
1914 dof_handler_coarse.locally_owned_dofs() :
1915 dof_handler_coarse.locally_owned_mg_dofs(mg_level_coarse));
1916
1917 transfer.constraint_info_fine.reinit(n_coarse_cells_total);
1918 transfer.constraint_info_fine.set_locally_owned_indices(
1919 (mg_level_fine == numbers::invalid_unsigned_int) ?
1920 dof_handler_fine.locally_owned_dofs() :
1921 dof_handler_fine.locally_owned_mg_dofs(mg_level_fine));
1922
1923
1924 std::vector<unsigned int> cell_no(transfer.schemes.size(), 0);
1925 for (unsigned int i = 1; i < transfer.schemes.size(); ++i)
1926 cell_no[i] = cell_no[i - 1] + transfer.schemes[i - 1].n_coarse_cells;
1927
1928 process_cells(
1929 [&](const auto &cell_coarse, const auto &cell_fine) {
1930 // first process cells with scheme 0
1931 // parent
1932 {
1933 transfer.constraint_info_coarse.read_dof_indices(
1934 cell_no[0],
1935 mg_level_coarse,
1936 cell_coarse,
1937 constraints_coarse,
1938 {});
1939 }
1940
1941 // child
1942 {
1943 cell_fine.get_dof_indices(local_dof_indices);
1944 for (unsigned int i = 0;
1945 i < transfer.schemes[0].n_dofs_per_cell_coarse;
1946 i++)
1947 level_dof_indices_coarse[i] =
1948 local_dof_indices[lexicographic_numbering_fine[i]];
1949
1950 transfer.constraint_info_fine.read_dof_indices(
1951 cell_no[0], level_dof_indices_coarse, {});
1952 }
1953
1954 // move pointers
1955 {
1956 ++cell_no[0];
1957 }
1958 },
1959 [&](const auto &cell_coarse, const auto &cell_fine, const auto c) {
1960 // process rest of cells
1961 const std::uint8_t refinement_case =
1962 reference_cell == ReferenceCells::Tetrahedron ?
1963 cell_fine.refinement_case() :
1964 1;
1965 // parent (only once at the beginning)
1966 if (c == 0)
1967 {
1968 transfer.constraint_info_coarse.read_dof_indices(
1969 cell_no[refinement_case],
1970 mg_level_coarse,
1971 cell_coarse,
1972 constraints_coarse,
1973 {});
1974
1975 level_dof_indices_fine.assign(level_dof_indices_fine.size(),
1976 numbers::invalid_dof_index);
1977 }
1978
1979 // child
1980 {
1981 cell_fine.get_dof_indices(local_dof_indices);
1982 for (unsigned int i = 0;
1983 i < transfer.schemes[refinement_case].n_dofs_per_cell_coarse;
1984 ++i)
1985 {
1986 const auto index =
1987 local_dof_indices[lexicographic_numbering_fine[i]];
1988 Assert(
1989 level_dof_indices_fine[cell_local_children_indices[c][i]] ==
1990 numbers::invalid_dof_index ||
1991 level_dof_indices_fine[cell_local_children_indices[c]
1992 [i]] ==
1993 index,
1994 ExcInternalError());
1995
1996 level_dof_indices_fine[cell_local_children_indices[c][i]] =
1997 index;
1998 }
1999 }
2000
2001 // move pointers (only once at the end)
2002 if (c + 1 == GeometryInfo<dim>::max_children_per_cell)
2003 {
2004 transfer.constraint_info_fine.read_dof_indices(
2005 cell_no[refinement_case], level_dof_indices_fine, {});
2006
2007 ++cell_no[refinement_case];
2008 }
2009 });
2010 }
2011
2012 {
2013 transfer.partitioner_coarse = transfer.constraint_info_coarse.finalize(
2014 dof_handler_coarse.get_mpi_communicator());
2015 transfer.vec_coarse.reinit(transfer.partitioner_coarse);
2016
2017 transfer.partitioner_fine = transfer.constraint_info_fine.finalize(
2018 dof_handler_fine.get_mpi_communicator());
2019 transfer.vec_fine.reinit(transfer.partitioner_fine);
2020 }
2021
2022
2023 // ------------- prolongation matrix (0) -> identity matrix --------------
2024
2025 // nothing to do since for identity prolongation matrices a short-cut
2026 // code path is used during prolongation/restriction
2027
2028 // -------------------prolongation matrix (i = 1 ... n)-------------------
2029 {
2030 AssertDimension(fe_fine.n_base_elements(), 1);
2031 for (unsigned int transfer_scheme_index = 1;
2032 transfer_scheme_index < transfer.schemes.size();
2033 ++transfer_scheme_index)
2034 {
2035 if (has_tp_structure)
2036 {
2037 const auto fe = create_1D_fe(fe_fine.base_element(0));
2038
2039 std::vector<unsigned int> renumbering(fe->n_dofs_per_cell());
2040 {
2041 AssertIndexRange(fe->n_dofs_per_vertex(), 2);
2042 renumbering[0] = 0;
2043 for (unsigned int i = 0; i < fe->dofs_per_line; ++i)
2044 renumbering[i + fe->n_dofs_per_vertex()] =
2045 GeometryInfo<1>::vertices_per_cell *
2046 fe->n_dofs_per_vertex() +
2047 i;
2048 if (fe->n_dofs_per_vertex() > 0)
2049 renumbering[fe->n_dofs_per_cell() -
2050 fe->n_dofs_per_vertex()] =
2051 fe->n_dofs_per_vertex();
2052 }
2053
2054 // TODO: data structures are saved in form of DG data structures
2055 // here
2056 const unsigned int shift =
2057 is_feq ? (fe->n_dofs_per_cell() - fe->n_dofs_per_vertex()) :
2058 (fe->n_dofs_per_cell());
2059 const unsigned int n_child_dofs_1d =
2060 is_feq ?
2061 (fe->n_dofs_per_cell() * 2 - fe->n_dofs_per_vertex()) :
2062 (fe->n_dofs_per_cell() * 2);
2063
2064 {
2065 transfer.schemes[transfer_scheme_index]
2066 .prolongation_matrix.resize(fe->n_dofs_per_cell() *
2067 n_child_dofs_1d);
2068
2069 for (unsigned int c = 0;
2070 c < GeometryInfo<1>::max_children_per_cell;
2071 ++c)
2072 for (unsigned int i = 0; i < fe->n_dofs_per_cell(); ++i)
2073 for (unsigned int j = 0; j < fe->n_dofs_per_cell(); ++j)
2074 transfer.schemes[transfer_scheme_index]
2075 .prolongation_matrix[i * n_child_dofs_1d + j +
2076 c * shift] =
2077 fe->get_prolongation_matrix(c)(renumbering[j],
2078 renumbering[i]);
2079 }
2080 {
2081 transfer.schemes[transfer_scheme_index]
2082 .restriction_matrix.resize(fe->n_dofs_per_cell() *
2083 n_child_dofs_1d);
2084
2085 for (unsigned int c = 0;
2086 c < GeometryInfo<1>::max_children_per_cell;
2087 ++c)
2088 {
2089 const auto matrix = get_restriction_matrix(*fe, c);
2090 for (unsigned int i = 0; i < fe->n_dofs_per_cell(); ++i)
2091 for (unsigned int j = 0; j < fe->n_dofs_per_cell(); ++j)
2092 transfer.schemes[transfer_scheme_index]
2093 .restriction_matrix[i * n_child_dofs_1d + j +
2094 c * shift] +=
2095 matrix(renumbering[i], renumbering[j]);
2096 }
2097 }
2098 }
2099 else
2100 {
2101 const auto &fe = fe_fine.base_element(0);
2102 const unsigned int n_dofs_per_cell = fe.n_dofs_per_cell();
2103
2104 {
2105 transfer.schemes[transfer_scheme_index]
2106 .prolongation_matrix.resize(
2107 n_dofs_per_cell * n_dofs_per_cell *
2108 GeometryInfo<dim>::max_children_per_cell);
2109
2110 for (unsigned int c = 0;
2111 c < GeometryInfo<dim>::max_children_per_cell;
2112 ++c)
2113 {
2114 const auto matrix =
2115 reference_cell == ReferenceCells::Tetrahedron ?
2116 fe.get_prolongation_matrix(
2117 c, RefinementCase<dim>(transfer_scheme_index)) :
2118 fe.get_prolongation_matrix(c);
2119
2120
2121 for (unsigned int i = 0; i < n_dofs_per_cell; ++i)
2122 for (unsigned int j = 0; j < n_dofs_per_cell; ++j)
2123 transfer.schemes[transfer_scheme_index]
2124 .prolongation_matrix
2125 [i * n_dofs_per_cell *
2126 GeometryInfo<dim>::max_children_per_cell +
2127 j + c * n_dofs_per_cell] = matrix(j, i);
2128 }
2129 }
2130 {
2131 transfer.schemes[transfer_scheme_index]
2132 .restriction_matrix.resize(
2133 n_dofs_per_cell * n_dofs_per_cell *
2134 GeometryInfo<dim>::max_children_per_cell);
2135
2136 for (unsigned int c = 0;
2137 c < GeometryInfo<dim>::max_children_per_cell;
2138 ++c)
2139 {
2140 const auto matrix =
2141 reference_cell == ReferenceCells::Tetrahedron ?
2142 get_restriction_matrix(
2143 fe, c, RefinementCase<dim>(transfer_scheme_index)) :
2144 get_restriction_matrix(fe, c);
2145 for (unsigned int i = 0; i < n_dofs_per_cell; ++i)
2146 for (unsigned int j = 0; j < n_dofs_per_cell; ++j)
2147 transfer.schemes[transfer_scheme_index]
2148 .restriction_matrix
2149 [i * n_dofs_per_cell *
2150 GeometryInfo<dim>::max_children_per_cell +
2151 j + c * n_dofs_per_cell] += matrix(i, j);
2152 }
2153 }
2154 }
2155 }
2156 }
2157
2158 // ------------------------------- weights -------------------------------
2159 if (transfer.fine_element_is_continuous)
2160 setup_weights(constraints_fine, transfer, is_feq);
2161 }
2162
2163
2164
2165 template <int dim, typename Number>
2166 static void
2167 compute_prolongation_and_restriction_matrices(
2168 const FiniteElement<dim> &fe_fine,
2169 const FiniteElement<dim> &fe_coarse,
2170 typename MGTwoLevelTransfer<
2171 dim,
2172 LinearAlgebra::distributed::Vector<Number>>::MGTransferScheme &scheme)
2173 {
2174 AssertDimension(fe_fine.n_base_elements(), 1);
2175 AssertDimension(fe_coarse.n_base_elements(), 1);
2176
2177 const bool has_tp_structure = fe_has_tp_structure(fe_fine);
2178
2179 const FiniteElement<dim> &fe_fine_scalar = fe_fine.base_element(0);
2180 const FiniteElement<dim> &fe_coarse_scalar = fe_coarse.base_element(0);
2181
2182 Assert(fe_fine.reference_cell() == fe_coarse.reference_cell(),
2183 ExcNotImplemented());
2184
2185 if (has_tp_structure && (fe_coarse != fe_fine) &&
2186 (fe_coarse.n_dofs_per_cell() != 0 && fe_fine.n_dofs_per_cell() != 0))
2187 {
2188 const auto fe_fine_1d = create_1D_fe(fe_fine_scalar);
2189
2190 std::vector<unsigned int> renumbering_fine(
2191 fe_fine_1d->n_dofs_per_cell());
2192 AssertIndexRange(fe_fine_1d->n_dofs_per_vertex(), 2);
2193 renumbering_fine[0] = 0;
2194 for (unsigned int i = 0; i < fe_fine_1d->dofs_per_line; ++i)
2195 renumbering_fine[i + fe_fine_1d->n_dofs_per_vertex()] =
2196 GeometryInfo<1>::vertices_per_cell *
2197 fe_fine_1d->n_dofs_per_vertex() +
2198 i;
2199 if (fe_fine_1d->n_dofs_per_vertex() > 0)
2200 renumbering_fine[fe_fine_1d->n_dofs_per_cell() -
2201 fe_fine_1d->n_dofs_per_vertex()] =
2202 fe_fine_1d->n_dofs_per_vertex();
2203
2204 const auto fe_coarse_1d = create_1D_fe(fe_coarse_scalar);
2205
2206 std::vector<unsigned int> renumbering_coarse(
2207 fe_coarse_1d->n_dofs_per_cell());
2208 AssertIndexRange(fe_coarse_1d->n_dofs_per_vertex(), 2);
2209 renumbering_coarse[0] = 0;
2210 for (unsigned int i = 0; i < fe_coarse_1d->dofs_per_line; ++i)
2211 renumbering_coarse[i + fe_coarse_1d->n_dofs_per_vertex()] =
2212 GeometryInfo<1>::vertices_per_cell *
2213 fe_coarse_1d->n_dofs_per_vertex() +
2214 i;
2215 if (fe_coarse_1d->n_dofs_per_vertex() > 0)
2216 renumbering_coarse[fe_coarse_1d->n_dofs_per_cell() -
2217 fe_coarse_1d->n_dofs_per_vertex()] =
2218 fe_coarse_1d->n_dofs_per_vertex();
2219
2220 FullMatrix<double> matrix(fe_fine_1d->n_dofs_per_cell(),
2221 fe_coarse_1d->n_dofs_per_cell());
2222 FETools::get_projection_matrix(*fe_coarse_1d, *fe_fine_1d, matrix);
2223 scheme.prolongation_matrix.resize(fe_fine_1d->n_dofs_per_cell() *
2224 fe_coarse_1d->n_dofs_per_cell());
2225
2226 for (unsigned int i = 0, k = 0; i < fe_coarse_1d->n_dofs_per_cell();
2227 ++i)
2228 for (unsigned int j = 0; j < fe_fine_1d->n_dofs_per_cell();
2229 ++j, ++k)
2230 scheme.prolongation_matrix[k] =
2231 matrix(renumbering_fine[j], renumbering_coarse[i]);
2232
2233 matrix.reinit(fe_coarse_1d->n_dofs_per_cell(),
2234 fe_fine_1d->n_dofs_per_cell());
2235 FETools::get_projection_matrix(*fe_fine_1d, *fe_coarse_1d, matrix);
2236 scheme.restriction_matrix.resize(fe_fine_1d->n_dofs_per_cell() *
2237 fe_coarse_1d->n_dofs_per_cell());
2238
2239 for (unsigned int i = 0, k = 0; i < fe_coarse_1d->n_dofs_per_cell();
2240 ++i)
2241 for (unsigned int j = 0; j < fe_fine_1d->n_dofs_per_cell();
2242 ++j, ++k)
2243 scheme.restriction_matrix[k] =
2244 matrix(renumbering_coarse[i], renumbering_fine[j]);
2245 }
2246 else if ((!has_tp_structure) && (fe_fine != fe_coarse) &&
2247 (fe_fine.n_dofs_per_cell() != 0 &&
2248 fe_coarse.n_dofs_per_cell() != 0))
2249 {
2250 FullMatrix<double> matrix(fe_fine_scalar.n_dofs_per_cell(),
2251 fe_coarse_scalar.n_dofs_per_cell());
2252 FETools::get_projection_matrix(fe_coarse_scalar,
2253 fe_fine_scalar,
2254 matrix);
2255 scheme.prolongation_matrix.resize(matrix.m() * matrix.n());
2256
2257 for (unsigned int i = 0, k = 0; i < matrix.n(); ++i)
2258 for (unsigned int j = 0; j < matrix.m(); ++j, ++k)
2259 scheme.prolongation_matrix[k] = matrix(j, i);
2260
2261 matrix.reinit(fe_coarse_scalar.n_dofs_per_cell(),
2262 fe_fine_scalar.n_dofs_per_cell());
2263 FETools::get_projection_matrix(fe_fine_scalar,
2264 fe_coarse_scalar,
2265 matrix);
2266 scheme.restriction_matrix.resize(matrix.m() * matrix.n());
2267
2268 for (unsigned int i = 0, k = 0; i < matrix.m(); ++i)
2269 for (unsigned int j = 0; j < matrix.n(); ++j, ++k)
2270 scheme.restriction_matrix[k] = matrix(i, j);
2271 }
2272 }
2273
2274
2275
2276 template <int dim, typename Number>
2277 static void
2278 reinit_polynomial_transfer(
2279 const DoFHandler<dim> &dof_handler_fine,
2280 const DoFHandler<dim> &dof_handler_coarse,
2281 const dealii::AffineConstraints<Number> &constraints_fine,
2282 const dealii::AffineConstraints<Number> &constraints_coarse,
2283 const unsigned int mg_level_fine,
2284 const unsigned int mg_level_coarse,
2285 MGTwoLevelTransfer<dim, LinearAlgebra::distributed::Vector<Number>>
2286 &transfer)
2287 {
2288 Assert(mg_level_fine == numbers::invalid_unsigned_int ||
2289 mg_level_fine <= MGTools::max_level_for_coarse_mesh(
2290 dof_handler_fine.get_triangulation()),
2291 ExcMessage("Polynomial transfer is only allowed on the active "
2292 "level (numbers::invalid_unsigned_int) or on "
2293 "refinement levels without hanging nodes."));
2294 Assert(mg_level_coarse == numbers::invalid_unsigned_int ||
2295 mg_level_coarse <= MGTools::max_level_for_coarse_mesh(
2296 dof_handler_coarse.get_triangulation()),
2297 ExcMessage("Polynomial transfer is only allowed on the active "
2298 "level (numbers::invalid_unsigned_int) or on "
2299 "refinement levels without hanging nodes."));
2300
2301 AssertDimension(constraints_fine.n_inhomogeneities(), 0);
2302 AssertDimension(constraints_coarse.n_inhomogeneities(), 0);
2303
2304 transfer.dof_handler_fine = &dof_handler_fine;
2305 transfer.mg_level_fine = mg_level_fine;
2306
2307 std::unique_ptr<FineDoFHandlerViewBase<dim>> dof_handler_fine_view;
2308
2309 if (internal::p_transfer_involves_repartitioning(dof_handler_fine,
2310 dof_handler_coarse,
2311 mg_level_fine,
2312 mg_level_coarse))
2313 dof_handler_fine_view =
2314 std::make_unique<PermutationFineDoFHandlerView<dim>>(
2315 dof_handler_fine,
2316 dof_handler_coarse,
2317 mg_level_fine,
2318 mg_level_coarse);
2319 else
2320 dof_handler_fine_view =
2321 std::make_unique<IdentityFineDoFHandlerView<dim>>(dof_handler_fine,
2322 mg_level_fine);
2323
2324 // TODO: adjust assert
2325 AssertDimension(
2326 dof_handler_fine.get_triangulation().n_global_active_cells(),
2327 dof_handler_coarse.get_triangulation().n_global_active_cells());
2328
2329 // extract number of components
2330 AssertDimension(dof_handler_fine.get_fe_collection().n_components(),
2331 dof_handler_coarse.get_fe_collection().n_components());
2332
2333 transfer.n_components =
2334 dof_handler_fine.get_fe_collection().n_components();
2335
2336 transfer.fine_element_is_continuous =
2337 std::all_of(dof_handler_fine.get_fe_collection().begin(),
2338 dof_handler_fine.get_fe_collection().end(),
2339 [](const auto &fe) {
2340 return fe.n_dofs_per_cell() == 0 ||
2341 fe.n_dofs_per_vertex() > 0;
2342 });
2343
2344 if constexpr (running_in_debug_mode())
2345 {
2346 const bool fine_element_is_discontinuous =
2347 std::all_of(dof_handler_fine.get_fe_collection().begin(),
2348 dof_handler_fine.get_fe_collection().end(),
2349 [](const auto &fe) {
2350 return fe.n_dofs_per_cell() == 0 ||
2351 fe.n_dofs_per_vertex() == 0;
2352 });
2353
2354 Assert(transfer.fine_element_is_continuous !=
2355 fine_element_is_discontinuous,
2356 ExcNotImplemented());
2357 }
2358 const bool is_feq =
2359 std::all_of(dof_handler_fine.get_fe_collection().begin(),
2360 dof_handler_fine.get_fe_collection().end(),
2361 [](const auto &fe) {
2362 return fe.n_base_elements() == 1 &&
2363 (dynamic_cast<const FE_Q<dim> *>(
2364 &fe.base_element(0)) != nullptr);
2365 });
2366
2367 const bool has_tp_structure =
2368 std::all_of(dof_handler_fine.get_fe_collection().begin(),
2369 dof_handler_fine.get_fe_collection().end(),
2370 [](const auto &fe) { return fe_has_tp_structure(fe); });
2371
2372 const auto process_cells = [&](const auto &fu) {
2373 loop_over_active_or_level_cells(
2374 dof_handler_coarse, mg_level_coarse, [&](const auto &cell_coarse) {
2375 const auto cell_coarse_on_fine_mesh =
2376 dof_handler_fine_view->get_cell_view(cell_coarse);
2377 fu(cell_coarse, &cell_coarse_on_fine_mesh);
2378 });
2379 };
2380
2381 std::map<std::pair<unsigned int, unsigned int>, unsigned int>
2382 fe_index_pairs;
2383
2384 process_cells([&](const auto &cell_coarse, const auto &cell_fine) {
2385 fe_index_pairs.emplace(
2386 std::pair<unsigned int, unsigned int>(cell_coarse->active_fe_index(),
2387 cell_fine->active_fe_index()),
2388 0);
2389 });
2390
2391 unsigned int counter = 0;
2392 for (auto &f : fe_index_pairs)
2393 f.second = counter++;
2394
2395 transfer.schemes.resize(counter);
2396
2397 // extract number of coarse cells
2398 {
2399 for (auto &scheme : transfer.schemes)
2400 scheme.n_coarse_cells = 0;
2401 process_cells([&](const auto &cell_coarse, const auto &cell_fine) {
2402 transfer
2403 .schemes[fe_index_pairs[std::pair<unsigned int, unsigned int>(
2404 cell_coarse->active_fe_index(), cell_fine->active_fe_index())]]
2405 .n_coarse_cells++;
2406 });
2407 }
2408
2409 for (const auto &fe_index_pair : fe_index_pairs)
2410 {
2411 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_coarse =
2412 dof_handler_coarse.get_fe(fe_index_pair.first.first)
2413 .n_dofs_per_cell();
2414 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_fine =
2415 dof_handler_fine.get_fe(fe_index_pair.first.second)
2416 .n_dofs_per_cell();
2417
2418 transfer.schemes[fe_index_pair.second].degree_coarse =
2419 dof_handler_coarse.get_fe(fe_index_pair.first.first).degree;
2420 transfer.schemes[fe_index_pair.second].degree_fine =
2421 dof_handler_fine.get_fe(fe_index_pair.first.second).degree;
2422 }
2423
2424 std::vector<unsigned int> n_dof_indices_fine(fe_index_pairs.size() + 1);
2425 std::vector<unsigned int> n_dof_indices_coarse(fe_index_pairs.size() + 1);
2426 std::vector<unsigned int> cell_no(fe_index_pairs.size() + 1, 0);
2427
2428 {
2429 std::vector<std::vector<unsigned int>> lexicographic_numbering_fine(
2430 fe_index_pairs.size());
2431 std::vector<std::vector<unsigned int>> lexicographic_numbering_coarse(
2432 fe_index_pairs.size());
2433 std::vector<std::vector<types::global_dof_index>>
2434 local_dof_indices_coarse(fe_index_pairs.size());
2435 std::vector<std::vector<types::global_dof_index>>
2436 local_dof_indices_coarse_lex(fe_index_pairs.size());
2437 std::vector<std::vector<types::global_dof_index>>
2438 local_dof_indices_fine(fe_index_pairs.size());
2439 std::vector<std::vector<types::global_dof_index>>
2440 local_dof_indices_fine_lex(fe_index_pairs.size());
2441
2442 for (const auto &fe_index_pair : fe_index_pairs)
2443 {
2444 local_dof_indices_coarse[fe_index_pair.second].resize(
2445 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_coarse);
2446 local_dof_indices_coarse_lex[fe_index_pair.second].resize(
2447 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_coarse);
2448 local_dof_indices_fine[fe_index_pair.second].resize(
2449 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_fine);
2450 local_dof_indices_fine_lex[fe_index_pair.second].resize(
2451 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_fine);
2452
2453 n_dof_indices_fine[fe_index_pair.second + 1] =
2454 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_fine *
2455 transfer.schemes[fe_index_pair.second].n_coarse_cells;
2456 n_dof_indices_coarse[fe_index_pair.second + 1] =
2457 transfer.schemes[fe_index_pair.second].n_dofs_per_cell_coarse *
2458 transfer.schemes[fe_index_pair.second].n_coarse_cells;
2459 cell_no[fe_index_pair.second + 1] =
2460 transfer.schemes[fe_index_pair.second].n_coarse_cells;
2461
2462 const auto reference_cell =
2463 dof_handler_fine.get_fe(fe_index_pair.first.second)
2464 .reference_cell();
2465
2466 Assert(reference_cell ==
2467 dof_handler_coarse.get_fe(fe_index_pair.first.first)
2468 .reference_cell(),
2469 ExcNotImplemented());
2470
2471 // ------------------- lexicographic_numbering --------------------
2472 if (has_tp_structure)
2473 {
2474 const Quadrature<1> dummy_quadrature(
2475 std::vector<Point<1>>(1, Point<1>()));
2476 internal::MatrixFreeFunctions::ShapeInfo<
2477 VectorizedArray<Number>>
2478 shape_info;
2479 shape_info.reinit(dummy_quadrature,
2480 dof_handler_fine.get_fe(
2481 fe_index_pair.first.second),
2482 0);
2483 lexicographic_numbering_fine[fe_index_pair.second] =
2484 shape_info.lexicographic_numbering;
2485
2486 shape_info.reinit(dummy_quadrature,
2487 dof_handler_coarse.get_fe(
2488 fe_index_pair.first.first),
2489 0);
2490 lexicographic_numbering_coarse[fe_index_pair.second] =
2491 shape_info.lexicographic_numbering;
2492 }
2493 else
2494 {
2495 const auto dummy_quadrature =
2496 reference_cell.template get_gauss_type_quadrature<dim>(1);
2497
2498 internal::MatrixFreeFunctions::ShapeInfo<Number> shape_info;
2499 shape_info.reinit(dummy_quadrature,
2500 dof_handler_fine.get_fe(
2501 fe_index_pair.first.second),
2502 0);
2503 lexicographic_numbering_fine[fe_index_pair.second] =
2504 shape_info.lexicographic_numbering;
2505
2506 shape_info.reinit(dummy_quadrature,
2507 dof_handler_coarse.get_fe(
2508 fe_index_pair.first.first),
2509 0);
2510 lexicographic_numbering_coarse[fe_index_pair.second] =
2511 shape_info.lexicographic_numbering;
2512 }
2513 }
2514
2515 for (unsigned int i = 0; i < fe_index_pairs.size(); ++i)
2516 {
2517 n_dof_indices_fine[i + 1] += n_dof_indices_fine[i];
2518 n_dof_indices_coarse[i + 1] += n_dof_indices_coarse[i];
2519 cell_no[i + 1] += cell_no[i];
2520 }
2521
2522 // ------------------------------ indices -----------------------------
2523
2524 transfer.constraint_info_coarse.reinit(
2525 dof_handler_coarse,
2526 cell_no.back(),
2527 constraints_coarse.n_constraints() > 0 &&
2528 use_fast_hanging_node_algorithm(dof_handler_coarse,
2529 mg_level_coarse));
2530 transfer.constraint_info_coarse.set_locally_owned_indices(
2531 (mg_level_coarse == numbers::invalid_unsigned_int) ?
2532 dof_handler_coarse.locally_owned_dofs() :
2533 dof_handler_coarse.locally_owned_mg_dofs(mg_level_coarse));
2534
2535 transfer.constraint_info_fine.reinit(cell_no.back());
2536 transfer.constraint_info_fine.set_locally_owned_indices(
2537 (mg_level_fine == numbers::invalid_unsigned_int) ?
2538 dof_handler_fine.locally_owned_dofs() :
2539 dof_handler_fine.locally_owned_mg_dofs(mg_level_fine));
2540
2541 process_cells([&](const auto &cell_coarse, const auto &cell_fine) {
2542 const auto fe_pair_no =
2543 fe_index_pairs[std::pair<unsigned int, unsigned int>(
2544 cell_coarse->active_fe_index(), cell_fine->active_fe_index())];
2545
2546 // parent
2547 {
2548 transfer.constraint_info_coarse.read_dof_indices(
2549 cell_no[fe_pair_no],
2550 mg_level_coarse,
2551 cell_coarse,
2552 constraints_coarse,
2553 {});
2554 }
2555
2556 // child
2557 {
2558 cell_fine->get_dof_indices(local_dof_indices_fine[fe_pair_no]);
2559
2560 for (unsigned int i = 0;
2561 i < transfer.schemes[fe_pair_no].n_dofs_per_cell_fine;
2562 ++i)
2563 local_dof_indices_fine_lex[fe_pair_no][i] = local_dof_indices_fine
2564 [fe_pair_no][lexicographic_numbering_fine[fe_pair_no][i]];
2565
2566 transfer.constraint_info_fine.read_dof_indices(
2567 cell_no[fe_pair_no], local_dof_indices_fine_lex[fe_pair_no], {});
2568 }
2569
2570 // move pointers
2571 {
2572 cell_no[fe_pair_no]++;
2573 }
2574 });
2575 }
2576
2577 {
2578 transfer.partitioner_coarse = transfer.constraint_info_coarse.finalize(
2579 dof_handler_coarse.get_mpi_communicator());
2580 transfer.vec_coarse.reinit(transfer.partitioner_coarse);
2581
2582 transfer.partitioner_fine = transfer.constraint_info_fine.finalize(
2583 dof_handler_fine.get_mpi_communicator());
2584 transfer.vec_fine.reinit(transfer.partitioner_fine);
2585 }
2586
2587 // ------------------------- prolongation matrix -------------------------
2588 for (const auto &fe_index_pair_ : fe_index_pairs)
2589 compute_prolongation_and_restriction_matrices<dim, Number>(
2590 dof_handler_fine.get_fe(fe_index_pair_.first.second),
2591 dof_handler_coarse.get_fe(fe_index_pair_.first.first),
2592 transfer.schemes[fe_index_pair_.second]);
2593
2594 // ------------------------------- weights -------------------------------
2595 if (transfer.fine_element_is_continuous)
2596 setup_weights(constraints_fine, transfer, is_feq);
2597 }
2598 };
2599
2600
2601
2602 template <typename Number>
2603 struct SimpleVectorDataExchange
2604 {
2605 SimpleVectorDataExchange(
2606 const std::shared_ptr<const Utilities::MPI::Partitioner>
2607 &embedded_partitioner,
2608 AlignedVector<Number> &buffer)
2609 : embedded_partitioner(embedded_partitioner)
2610 , buffer(buffer)
2611 {}
2612
2613 template <typename VectorType>
2614 void
2615 update_ghost_values(const VectorType &vec) const
2616 {
2617 update_ghost_values_start(vec);
2618 update_ghost_values_finish(vec);
2619 }
2620
2621 template <typename VectorType>
2622 void
2623 update_ghost_values_start(const VectorType &vec) const
2624 {
2625 #ifndef DEAL_II_WITH_MPI
2626 Assert(false, ExcNeedsMPI());
2627 (void)vec;
2628 #else
2629 const auto &vector_partitioner = vec.get_partitioner();
2630
2631 buffer.resize_fast(embedded_partitioner->n_import_indices());
2632
2633 embedded_partitioner
2634 ->template export_to_ghosted_array_start<Number, MemorySpace::Host>(
2635 0,
2636 dealii::ArrayView<const Number>(
2637 vec.begin(), embedded_partitioner->locally_owned_size()),
2638 dealii::ArrayView<Number>(buffer.begin(), buffer.size()),
2639 dealii::ArrayView<Number>(
2640 const_cast<Number *>(vec.begin()) +
2641 embedded_partitioner->locally_owned_size(),
2642 vector_partitioner->n_ghost_indices()),
2643 requests);
2644 #endif
2645 }
2646
2647 template <typename VectorType>
2648 void
2649 update_ghost_values_finish(const VectorType &vec) const
2650 {
2651 #ifndef DEAL_II_WITH_MPI
2652 Assert(false, ExcNeedsMPI());
2653 (void)vec;
2654 #else
2655 const auto &vector_partitioner = vec.get_partitioner();
2656
2657 embedded_partitioner
2658 ->template export_to_ghosted_array_finish<Number, MemorySpace::Host>(
2659 dealii::ArrayView<Number>(
2660 const_cast<Number *>(vec.begin()) +
2661 embedded_partitioner->locally_owned_size(),
2662 vector_partitioner->n_ghost_indices()),
2663 requests);
2664
2665 vec.set_ghost_state(true);
2666 #endif
2667 }
2668
2669 template <typename VectorType>
2670 void
2671 compress(VectorType &vec) const
2672 {
2673 compress_start(vec);
2674 compress_finish(vec);
2675 }
2676
2677 template <typename VectorType>
2678 void
2679 compress_start(VectorType &vec) const
2680 {
2681 #ifndef DEAL_II_WITH_MPI
2682 Assert(false, ExcNeedsMPI());
2683 (void)vec;
2684 #else
2685 const auto &vector_partitioner = vec.get_partitioner();
2686
2687 buffer.resize_fast(embedded_partitioner->n_import_indices());
2688
2689 embedded_partitioner
2690 ->template import_from_ghosted_array_start<Number, MemorySpace::Host>(
2691 VectorOperation::add,
2692 0,
2693 dealii::ArrayView<Number>(
2694 const_cast<Number *>(vec.begin()) +
2695 embedded_partitioner->locally_owned_size(),
2696 vector_partitioner->n_ghost_indices()),
2697 dealii::ArrayView<Number>(buffer.begin(), buffer.size()),
2698 requests);
2699 #endif
2700 }
2701
2702 template <typename VectorType>
2703 void
2704 compress_finish(VectorType &vec) const
2705 {
2706 #ifndef DEAL_II_WITH_MPI
2707 Assert(false, ExcNeedsMPI());
2708 (void)vec;
2709 #else
2710 const auto &vector_partitioner = vec.get_partitioner();
2711
2712 embedded_partitioner
2713 ->template import_from_ghosted_array_finish<Number, MemorySpace::Host>(
2714 VectorOperation::add,
2715 dealii::ArrayView<const Number>(buffer.begin(), buffer.size()),
2716 dealii::ArrayView<Number>(vec.begin(),
2717 embedded_partitioner->locally_owned_size()),
2718 dealii::ArrayView<Number>(
2719 const_cast<Number *>(vec.begin()) +
2720 embedded_partitioner->locally_owned_size(),
2721 vector_partitioner->n_ghost_indices()),
2722 requests);
2723 #endif
2724 }
2725
2726 template <typename VectorType>
2727 void
2728 zero_out_ghost_values(const VectorType &vec) const
2729 {
2730 const auto &vector_partitioner = vec.get_partitioner();
2731
2732 ArrayView<Number> ghost_array(const_cast<VectorType &>(vec).begin() +
2733 vector_partitioner->locally_owned_size(),
2734 vector_partitioner->n_ghost_indices());
2735
2736 for (const auto &my_ghosts :
2737 embedded_partitioner->ghost_indices_within_larger_ghost_set())
2738 for (unsigned int j = my_ghosts.first; j < my_ghosts.second; ++j)
2739 ghost_array[j] = 0.;
2740
2741 vec.set_ghost_state(false);
2742 }
2743
2744 private:
2745 const std::shared_ptr<const Utilities::MPI::Partitioner>
2746 embedded_partitioner;
2747 dealii::AlignedVector<Number> &buffer;
2748 mutable std::vector<MPI_Request> requests;
2749 };
2750
2751 } // namespace internal
2752
2753
2754
2755 namespace MGTransferGlobalCoarseningTools
2756 {
2757 template <int dim, int spacedim>
2758 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
2759 create_geometric_coarsening_sequence(
2760 const Triangulation<dim, spacedim> &fine_triangulation_in)
2761 {
2762 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
2763 coarse_grid_triangulations(fine_triangulation_in.n_global_levels());
2764
2765 coarse_grid_triangulations.back().reset(&fine_triangulation_in, [](auto *) {
2766 // empty deleter, since fine_triangulation_in is an external field
2767 // and its destructor is called somewhere else
2768 });
2769
2770 // for a single level nothing has to be done
2771 if (fine_triangulation_in.n_global_levels() == 1)
2772 return coarse_grid_triangulations;
2773
2774 Assert(
2775 (dynamic_cast<
2776 const parallel::fullydistributed::Triangulation<dim, spacedim> *>(
2777 &fine_triangulation_in) == nullptr),
2778 ExcMessage(
2779 "Triangulations of type parallel::fullydistributed::Triangulation are "
2780 "not supported by this function!"));
2781
2782 const auto create_new_empty_triangulation =
2783 [&]() -> std::shared_ptr<Triangulation<dim, spacedim>> {
2784 #ifdef DEAL_II_WITH_P4EST
2785 if (const auto fine_triangulation = dynamic_cast<
2786 const parallel::distributed::Triangulation<dim, spacedim> *>(
2787 &fine_triangulation_in))
2788 return std::make_shared<
2789 parallel::distributed::Triangulation<dim, spacedim>>(
2790 fine_triangulation->get_mpi_communicator());
2791 else
2792 #endif
2793 #ifdef DEAL_II_WITH_MPI
2794 if (const auto fine_triangulation = dynamic_cast<
2795 const parallel::shared::Triangulation<dim, spacedim> *>(
2796 &fine_triangulation_in))
2797 return std::make_shared<parallel::shared::Triangulation<dim, spacedim>>(
2798 fine_triangulation->get_mpi_communicator(),
2799 Triangulation<dim, spacedim>::none,
2800 fine_triangulation->with_artificial_cells());
2801 else
2802 #endif
2803 return std::make_shared<Triangulation<dim, spacedim>>();
2804 };
2805
2806 const unsigned int max_level = fine_triangulation_in.n_global_levels() - 1;
2807
2808 // clear 'eliminate_unrefined_islands' from MeshSmoothing flags
2809 // to prevent unintentional refinement during coarsen_global()
2810 const auto mesh_smoothing =
2811 static_cast<typename Triangulation<dim, spacedim>::MeshSmoothing>(
2812 fine_triangulation_in.get_mesh_smoothing() &
2813 ~(Triangulation<dim, spacedim>::eliminate_unrefined_islands));
2814
2815 // create coarse meshes
2816 for (unsigned int l = max_level; l > 0; --l)
2817 {
2818 // copy triangulation
2819 auto new_tria = create_new_empty_triangulation();
2820 new_tria->copy_triangulation(*coarse_grid_triangulations[l]);
2821 new_tria->set_mesh_smoothing(mesh_smoothing);
2822
2823 // coarsen mesh
2824 new_tria->coarsen_global();
2825
2826 // save mesh
2827 coarse_grid_triangulations[l - 1] = new_tria;
2828 }
2829
2830 AssertDimension(coarse_grid_triangulations[0]->n_global_levels(), 1);
2831
2832 return coarse_grid_triangulations;
2833 }
2834
2835
2836
2837 template <int dim, int spacedim>
2838 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
2839 create_geometric_coarsening_sequence(
2840 Triangulation<dim, spacedim> &fine_triangulation_in,
2841 const RepartitioningPolicyTools::Base<dim, spacedim> &policy,
2842 const bool keep_fine_triangulation,
2843 const bool repartition_fine_triangulation)
2844 {
2845 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
2846 coarse_grid_triangulations(fine_triangulation_in.n_global_levels());
2847
2848 #ifndef DEAL_II_WITH_P4EST
2849 DEAL_II_NOT_IMPLEMENTED();
2850 (void)policy;
2851 (void)keep_fine_triangulation;
2852 (void)repartition_fine_triangulation;
2853 #else
2854 const auto fine_triangulation =
2855 dynamic_cast<parallel::distributed::Triangulation<dim, spacedim> *>(
2856 &fine_triangulation_in);
2857
2858 Assert(fine_triangulation, ExcNotImplemented());
2859
2860 const auto comm = fine_triangulation->get_mpi_communicator();
2861
2862 if (keep_fine_triangulation == true &&
2863 repartition_fine_triangulation == false)
2864 {
2865 coarse_grid_triangulations.back().reset(&fine_triangulation_in,
2866 [](auto *) {
2867 // empty deleter, since
2868 // fine_triangulation_in is an
2869 // external field and its
2870 // destructor is called
2871 // somewhere else
2872 });
2873 }
2874 else
2875 {
2876 // create triangulation description
2877 const auto construction_data =
2878 repartition_fine_triangulation ?
2879 TriangulationDescription::Utilities::
2880 create_description_from_triangulation(
2881 *fine_triangulation, policy.partition(*fine_triangulation)) :
2882 TriangulationDescription::Utilities::
2883 create_description_from_triangulation(*fine_triangulation, comm);
2884
2885 // create new triangulation
2886 const auto new_fine_triangulation = std::make_shared<
2887 parallel::fullydistributed::Triangulation<dim, spacedim>>(comm);
2888
2889 for (const auto i : fine_triangulation->get_manifold_ids())
2890 if (i != numbers::flat_manifold_id)
2891 new_fine_triangulation->set_manifold(
2892 i, fine_triangulation->get_manifold(i));
2893
2894 new_fine_triangulation->create_triangulation(construction_data);
2895
2896 // save mesh
2897 coarse_grid_triangulations.back() = new_fine_triangulation;
2898 }
2899
2900 // for a single level nothing has to be done
2901 if (fine_triangulation_in.n_global_levels() == 1)
2902 return coarse_grid_triangulations;
2903
2904 parallel::distributed::Triangulation<dim, spacedim> temp_triangulation(
2905 comm);
2906
2907 if (keep_fine_triangulation == true)
2908 temp_triangulation.copy_triangulation(*fine_triangulation);
2909
2910 auto *temp_triangulation_ptr =
2911 keep_fine_triangulation ? &temp_triangulation : fine_triangulation;
2912
2913 // clear 'eliminate_unrefined_islands' from MeshSmoothing flags
2914 // to prevent unintentional refinement during coarsen_global()
2915 const auto mesh_smoothing =
2916 static_cast<typename Triangulation<dim, spacedim>::MeshSmoothing>(
2917 temp_triangulation_ptr->get_mesh_smoothing() &
2918 ~(Triangulation<dim, spacedim>::eliminate_unrefined_islands));
2919 temp_triangulation_ptr->set_mesh_smoothing(mesh_smoothing);
2920
2921 const unsigned int max_level = fine_triangulation->n_global_levels() - 1;
2922
2923 // create coarse meshes
2924 for (unsigned int l = max_level; l > 0; --l)
2925 {
2926 // coarsen mesh
2927 temp_triangulation_ptr->coarsen_global();
2928
2929 // create triangulation description
2930 const auto construction_data = TriangulationDescription::Utilities::
2931 create_description_from_triangulation(
2932 *temp_triangulation_ptr, policy.partition(*temp_triangulation_ptr));
2933
2934 // create new triangulation
2935 const auto level_triangulation = std::make_shared<
2936 parallel::fullydistributed::Triangulation<dim, spacedim>>(comm);
2937
2938 for (const auto i : fine_triangulation->get_manifold_ids())
2939 if (i != numbers::flat_manifold_id)
2940 level_triangulation->set_manifold(
2941 i, fine_triangulation->get_manifold(i));
2942
2943 level_triangulation->create_triangulation(construction_data);
2944
2945 // save mesh
2946 coarse_grid_triangulations[l - 1] = level_triangulation;
2947 }
2948
2949 // recover MeshSmoothing flags in case we used the fine_triangulation
2950 // to build the sequence
2951 if (keep_fine_triangulation == false)
2952 fine_triangulation->set_mesh_smoothing(
2953 coarse_grid_triangulations.back()->get_mesh_smoothing());
2954 #endif
2955
2956 AssertDimension(coarse_grid_triangulations[0]->n_global_levels(), 1);
2957
2958 return coarse_grid_triangulations;
2959 }
2960
2961
2962
2963 template <int dim, int spacedim>
2964 std::vector<std::shared_ptr<const Triangulation<dim, spacedim>>>
2965 create_geometric_coarsening_sequence(
2966 const Triangulation<dim, spacedim> &fine_triangulation_in,
2967 const RepartitioningPolicyTools::Base<dim, spacedim> &policy,
2968 const bool repartition_fine_triangulation)
2969 {
2970 // remove const and convert it to flag
2971 return create_geometric_coarsening_sequence(
2972 const_cast<Triangulation<dim, spacedim> &>(fine_triangulation_in),
2973 policy,
2974 true,
2975 repartition_fine_triangulation);
2976 }
2977
2978 } // namespace MGTransferGlobalCoarseningTools
2979
2980
2981
2982 template <typename VectorType>
2983 MGTwoLevelTransferBase<VectorType>::MGTwoLevelTransferBase()
2984 : vec_fine_needs_ghost_update(true)
2985 {}
2986
2987
2988
2989 template <typename VectorType>
2990 void
2991 MGTwoLevelTransferBase<VectorType>::prolongate_and_add(
2992 VectorType &dst,
2993 const VectorType &src) const
2994 {
2995 const bool use_dst_inplace = this->vec_fine.size() == 0;
2996 auto *const vec_fine_ptr = use_dst_inplace ? &dst : &this->vec_fine;
2997 Assert(vec_fine_ptr->get_partitioner().get() == this->partitioner_fine.get(),
2998 ExcInternalError());
2999
3000 const bool use_src_inplace = this->vec_coarse.size() == 0;
3001 const auto *const vec_coarse_ptr = use_src_inplace ? &src : &this->vec_coarse;
3002 Assert(vec_coarse_ptr->get_partitioner().get() ==
3003 this->partitioner_coarse.get(),
3004 ExcInternalError());
3005
3006 const bool src_ghosts_have_been_set = src.has_ghost_elements();
3007
3008 if (use_src_inplace == false)
3009 this->vec_coarse.copy_locally_owned_data_from(src);
3010
3011 if ((use_src_inplace == false) || (src_ghosts_have_been_set == false))
3012 this->update_ghost_values(*vec_coarse_ptr);
3013
3014 if (use_dst_inplace == false)
3015 *vec_fine_ptr = Number(0.);
3016
3017 this->prolongate_and_add_internal(*vec_fine_ptr, *vec_coarse_ptr);
3018
3019 if (this->vec_fine_needs_ghost_update || use_dst_inplace == false)
3020 this->compress(*vec_fine_ptr, VectorOperation::add);
3021
3022 if (use_dst_inplace == false)
3023 dst += this->vec_fine;
3024
3025 if (use_src_inplace && (src_ghosts_have_been_set == false))
3026 this->zero_out_ghost_values(*vec_coarse_ptr);
3027 }
3028
3029
3030
3031 namespace internal
3032 {
3033 // Helper class to compute correct weights, which works by simply using
3034 // the degrees of freedom stored in MatrixFree, bypassing the hanging node
3035 // interpolation matrices.
3036 template <int dim, typename Number>
3037 class FEEvaluationNoConstraints : public FEEvaluation<dim, -1, 0, 1, Number>
3038 {
3039 public:
3040 FEEvaluationNoConstraints(const MatrixFree<dim, Number> &data,
3041 const unsigned int dof_index,
3042 const unsigned int component = 0)
3043 : FEEvaluation<dim, -1, 0, 1, Number>(data, dof_index, 0, component)
3044 {}
3045
3046 template <typename VectorType>
3047 void
3048 read_dof_values_unconstrained(VectorType &src)
3049 {
3050 std::array<VectorType *, 1> src_vector{{&src}};
3051 internal::VectorReader<Number, VectorizedArray<Number>> reader;
3052 this->template read_write_operation<VectorType>(
3053 reader,
3054 src_vector,
3055 {},
3056 std::bitset<VectorizedArray<Number>::size()>().flip(),
3057 false);
3058 }
3059
3060 template <typename VectorType>
3061 void
3062 distribute_local_to_global_unconstrained(VectorType &dst)
3063 {
3064 std::array<VectorType *, 1> dst_vector{{&dst}};
3065 internal::VectorDistributorLocalToGlobal<Number, VectorizedArray<Number>>
3066 writer;
3067 this->template read_write_operation<VectorType>(
3068 writer,
3069 dst_vector,
3070 {},
3071 std::bitset<VectorizedArray<Number>::size()>().flip(),
3072 false);
3073 }
3074 };
3075 } // namespace internal
3076
3077 template <int dim, typename VectorType>
3078 void
3079 MGTwoLevelTransfer<dim, VectorType>::prolongate_and_add_internal(
3080 VectorType &dst,
3081 const VectorType &src) const
3082 {
3083 if (matrix_free_data.get() != nullptr)
3084 {
3085 matrix_free_data->matrix_free_fine->template cell_loop<VectorType, int>(
3086 [&](const MatrixFree<dim, Number> &data,
3087 VectorType &dst,
3088 const int &,
3089 const std::pair<unsigned int, unsigned int> &range) {
3090 for (unsigned int comp = 0; comp < n_components; ++comp)
3091 {
3092 internal::FEEvaluationNoConstraints<dim, Number> eval_fine(
3093 data, matrix_free_data->dof_handler_index_fine, comp);
3094 FEEvaluation<dim, -1, 0, 1, Number> eval_coarse(
3095 *matrix_free_data->matrix_free_coarse,
3096 matrix_free_data->dof_handler_index_coarse,
3097 0,
3098 comp);
3099
3100 internal::CellTransferFactory cell_transfer(
3101 eval_fine.get_shape_info().data[0].fe_degree,
3102 eval_coarse.get_shape_info().data[0].fe_degree);
3103 for (unsigned int cell = range.first; cell < range.second; ++cell)
3104 {
3105 eval_fine.reinit(cell);
3106 eval_coarse.reinit(
3107 matrix_free_data->cell_list_fine_to_coarse[cell]);
3108
3109 eval_coarse.read_dof_values(src);
3110 if (schemes[0].prolongation_matrix.empty() == false)
3111 {
3112 internal::
3113 CellProlongator<dim, double, VectorizedArrayType>
3114 cell_prolongator(schemes[0].prolongation_matrix,
3115 eval_coarse.begin_dof_values(),
3116 eval_fine.begin_dof_values());
3117
3118 if (schemes[0].prolongation_matrix.size() <
3119 eval_fine.dofs_per_cell * eval_coarse.dofs_per_cell)
3120 cell_transfer.run(cell_prolongator);
3121 else
3122 cell_prolongator.run_full(eval_fine.dofs_per_cell,
3123 eval_coarse.dofs_per_cell);
3124 }
3125 else
3126 for (unsigned int i = 0; i < eval_coarse.dofs_per_cell; ++i)
3127 eval_fine.begin_dof_values()[i] =
3128 eval_coarse.begin_dof_values()[i];
3129
3130 if (weights.size() > 0)
3131 {
3132 const VectorizedArrayType *cell_weights =
3133 weights.data() + weights_start[cell];
3134 if (weights_are_compressed[cell])
3135 internal::weight_fe_q_dofs_by_entity<
3136 dim,
3137 -1,
3138 VectorizedArrayType>(
3139 cell_weights,
3140 1,
3141 eval_fine.get_shape_info().data[0].fe_degree + 1,
3142 eval_fine.begin_dof_values());
3143 else
3144 for (unsigned int i = 0; i < eval_fine.dofs_per_cell;
3145 ++i)
3146 eval_fine.begin_dof_values()[i] *= cell_weights[i];
3147 }
3148 eval_fine.distribute_local_to_global_unconstrained(dst);
3149 }
3150 }
3151 },
3152 dst,
3153 0);
3154 }
3155 else
3156 {
3157 const unsigned int n_lanes = VectorizedArrayType::size();
3158
3159 AlignedVector<VectorizedArrayType> evaluation_data_fine;
3160 AlignedVector<VectorizedArrayType> evaluation_data_coarse;
3161
3162 unsigned int cell_counter = 0;
3163 unsigned int batch_counter = 0;
3164
3165 for (const auto &scheme : schemes)
3166 {
3167 if (scheme.n_coarse_cells == 0)
3168 continue;
3169
3170 const bool needs_interpolation =
3171 scheme.prolongation_matrix.empty() == false;
3172
3173 evaluation_data_fine.clear();
3174 evaluation_data_coarse.clear();
3175
3176 const unsigned int max_n_dofs_per_cell =
3177 std::max(scheme.n_dofs_per_cell_fine,
3178 scheme.n_dofs_per_cell_coarse);
3179 evaluation_data_fine.resize(max_n_dofs_per_cell);
3180 evaluation_data_coarse.resize(max_n_dofs_per_cell);
3181
3182 internal::CellTransferFactory cell_transfer(scheme.degree_fine,
3183 scheme.degree_coarse);
3184
3185 const unsigned int n_scalar_dofs_fine =
3186 scheme.n_dofs_per_cell_fine / n_components;
3187 const unsigned int n_scalar_dofs_coarse =
3188 scheme.n_dofs_per_cell_coarse / n_components;
3189
3190 for (unsigned int cell = 0; cell < scheme.n_coarse_cells;
3191 cell += n_lanes, ++batch_counter)
3192 {
3193 const unsigned int n_lanes_filled =
3194 (cell + n_lanes > scheme.n_coarse_cells) ?
3195 (scheme.n_coarse_cells - cell) :
3196 n_lanes;
3197
3198 // read from src vector (similar to
3199 // FEEvaluation::read_dof_values())
3200 internal::VectorReader<Number, VectorizedArrayType> reader;
3201 constraint_info_coarse.read_write_operation(
3202 reader,
3203 src,
3204 evaluation_data_coarse.data(),
3205 cell_counter,
3206 n_lanes_filled,
3207 scheme.n_dofs_per_cell_coarse,
3208 true);
3209 constraint_info_coarse.apply_hanging_node_constraints(
3210 cell_counter, n_lanes_filled, false, evaluation_data_coarse);
3211
3212 // ---------------------------- coarse ---------------------------
3213 if (needs_interpolation)
3214 for (int c = n_components - 1; c >= 0; --c)
3215 {
3216 internal::CellProlongator<dim, double, VectorizedArrayType>
3217 cell_prolongator(scheme.prolongation_matrix,
3218 evaluation_data_coarse.begin() +
3219 c * n_scalar_dofs_coarse,
3220 evaluation_data_fine.begin() +
3221 c * n_scalar_dofs_fine);
3222
3223 if (scheme.prolongation_matrix.size() <
3224 n_scalar_dofs_fine * n_scalar_dofs_coarse)
3225 cell_transfer.run(cell_prolongator);
3226 else
3227 cell_prolongator.run_full(n_scalar_dofs_fine,
3228 n_scalar_dofs_coarse);
3229 }
3230 else
3231 evaluation_data_fine = evaluation_data_coarse; // TODO
3232 // ------------------------------ fine ---------------------------
3233
3234 // weight
3235 if (weights.size() > 0)
3236 {
3237 const VectorizedArrayType *cell_weights =
3238 weights.data() + weights_start[batch_counter];
3239 if (weights_are_compressed[batch_counter])
3240 internal::
3241 weight_fe_q_dofs_by_entity<dim, -1, VectorizedArrayType>(
3242 cell_weights,
3243 n_components,
3244 scheme.degree_fine + 1,
3245 evaluation_data_fine.begin());
3246 else
3247 for (unsigned int i = 0; i < scheme.n_dofs_per_cell_fine;
3248 ++i)
3249 evaluation_data_fine[i] *= cell_weights[i];
3250 }
3251
3252 // add into dst vector
3253 internal::VectorDistributorLocalToGlobal<Number,
3254 VectorizedArrayType>
3255 writer;
3256 constraint_info_fine.read_write_operation(
3257 writer,
3258 dst,
3259 evaluation_data_fine.data(),
3260 cell_counter,
3261 n_lanes_filled,
3262 scheme.n_dofs_per_cell_fine,
3263 false);
3264
3265 cell_counter += n_lanes_filled;
3266 }
3267 }
3268 }
3269 }
3270
3271
3272
3273 template <typename VectorType>
3274 void
3275 MGTwoLevelTransferBase<VectorType>::restrict_and_add(
3276 VectorType &dst,
3277 const VectorType &src) const
3278 {
3279 const bool use_src_inplace = this->vec_fine.size() == 0;
3280 const auto *const vec_fine_ptr = use_src_inplace ? &src : &this->vec_fine;
3281 Assert(vec_fine_ptr->get_partitioner().get() == this->partitioner_fine.get(),
3282 ExcInternalError());
3283
3284 const bool use_dst_inplace = this->vec_coarse.size() == 0;
3285 auto *const vec_coarse_ptr = use_dst_inplace ? &dst : &this->vec_coarse;
3286 Assert(vec_coarse_ptr->get_partitioner().get() ==
3287 this->partitioner_coarse.get(),
3288 ExcInternalError());
3289
3290 const bool src_ghosts_have_been_set = src.has_ghost_elements();
3291
3292 if (use_src_inplace == false)
3293 this->vec_fine.copy_locally_owned_data_from(src);
3294
3295 if ((use_src_inplace == false) ||
3296 (vec_fine_needs_ghost_update && (src_ghosts_have_been_set == false)))
3297 this->update_ghost_values(*vec_fine_ptr);
3298
3299 if (use_dst_inplace == false)
3300 *vec_coarse_ptr = Number(0.0);
3301
3302 // since we might add into the ghost values and call compress
3303 this->zero_out_ghost_values(*vec_coarse_ptr);
3304
3305 this->restrict_and_add_internal(*vec_coarse_ptr, *vec_fine_ptr);
3306
3307 // clean up related to update_ghost_values()
3308 if (vec_fine_needs_ghost_update == false && use_src_inplace == false)
3309 this->zero_out_ghost_values(*vec_fine_ptr); // internal vector (DG)
3310 else if (vec_fine_needs_ghost_update && use_src_inplace == false)
3311 vec_fine_ptr->set_ghost_state(false); // internal vector (CG)
3312 else if (vec_fine_needs_ghost_update && (src_ghosts_have_been_set == false))
3313 this->zero_out_ghost_values(*vec_fine_ptr); // external vector
3314
3315 this->compress(*vec_coarse_ptr, VectorOperation::add);
3316
3317 if (use_dst_inplace == false)
3318 dst += this->vec_coarse;
3319 }
3320
3321
3322
3323 template <int dim, typename VectorType>
3324 void
3325 MGTwoLevelTransfer<dim, VectorType>::restrict_and_add_internal(
3326 VectorType &dst,
3327 const VectorType &src) const
3328 {
3329 if (matrix_free_data.get() != nullptr)
3330 {
3331 int dummy = 0;
3332 matrix_free_data->matrix_free_fine->template cell_loop<int, VectorType>(
3333 [&](const MatrixFree<dim, Number> &data,
3334 int &,
3335 const VectorType &src,
3336 const std::pair<unsigned int, unsigned int> &range) {
3337 for (unsigned int comp = 0; comp < n_components; ++comp)
3338 {
3339 internal::FEEvaluationNoConstraints<dim, Number> eval_fine(
3340 data, matrix_free_data->dof_handler_index_fine, comp);
3341 FEEvaluation<dim, -1, 0, 1, Number> eval_coarse(
3342 *matrix_free_data->matrix_free_coarse,
3343 matrix_free_data->dof_handler_index_coarse,
3344 0,
3345 comp);
3346
3347 internal::CellTransferFactory cell_transfer(
3348 eval_fine.get_shape_info().data[0].fe_degree,
3349 eval_coarse.get_shape_info().data[0].fe_degree);
3350 for (unsigned int cell = range.first; cell < range.second; ++cell)
3351 {
3352 eval_fine.reinit(cell);
3353 eval_coarse.reinit(
3354 matrix_free_data->cell_list_fine_to_coarse[cell]);
3355
3356 eval_fine.read_dof_values_unconstrained(src);
3357 if (weights.size() > 0)
3358 {
3359 const VectorizedArrayType *cell_weights =
3360 weights.data() + weights_start[cell];
3361 if (weights_are_compressed[cell])
3362 internal::weight_fe_q_dofs_by_entity<
3363 dim,
3364 -1,
3365 VectorizedArrayType>(
3366 cell_weights,
3367 1,
3368 eval_fine.get_shape_info().data[0].fe_degree + 1,
3369 eval_fine.begin_dof_values());
3370 else
3371 for (unsigned int i = 0; i < eval_fine.dofs_per_cell;
3372 ++i)
3373 eval_fine.begin_dof_values()[i] *= cell_weights[i];
3374 }
3375
3376 if (schemes[0].prolongation_matrix.empty() == false)
3377 {
3378 internal::CellRestrictor<dim, double, VectorizedArrayType>
3379 cell_restrictor(schemes[0].prolongation_matrix,
3380 eval_fine.begin_dof_values(),
3381 eval_coarse.begin_dof_values());
3382
3383 if (schemes[0].prolongation_matrix.size() <
3384 eval_fine.dofs_per_cell * eval_coarse.dofs_per_cell)
3385 cell_transfer.run(cell_restrictor);
3386 else
3387 cell_restrictor.run_full(eval_fine.dofs_per_cell,
3388 eval_coarse.dofs_per_cell);
3389 }
3390 else
3391 for (unsigned int i = 0; i < eval_fine.dofs_per_cell; ++i)
3392 eval_coarse.begin_dof_values()[i] =
3393 eval_fine.begin_dof_values()[i];
3394
3395 eval_coarse.distribute_local_to_global(dst);
3396 }
3397 }
3398 },
3399 dummy,
3400 src);
3401 }
3402 else
3403 {
3404 const unsigned int n_lanes = VectorizedArrayType::size();
3405
3406 AlignedVector<VectorizedArrayType> evaluation_data_fine;
3407 AlignedVector<VectorizedArrayType> evaluation_data_coarse;
3408
3409 unsigned int cell_counter = 0;
3410 unsigned int batch_counter = 0;
3411
3412 for (const auto &scheme : schemes)
3413 {
3414 if (scheme.n_coarse_cells == 0)
3415 continue;
3416
3417 const bool needs_interpolation =
3418 scheme.prolongation_matrix.empty() == false;
3419
3420 evaluation_data_fine.clear();
3421 evaluation_data_coarse.clear();
3422
3423 const unsigned int max_n_dofs_per_cell =
3424 std::max(scheme.n_dofs_per_cell_fine,
3425 scheme.n_dofs_per_cell_coarse);
3426 evaluation_data_fine.resize(max_n_dofs_per_cell);
3427 evaluation_data_coarse.resize(max_n_dofs_per_cell);
3428
3429 internal::CellTransferFactory cell_transfer(scheme.degree_fine,
3430 scheme.degree_coarse);
3431
3432 const unsigned int n_scalar_dofs_fine =
3433 scheme.n_dofs_per_cell_fine / n_components;
3434 const unsigned int n_scalar_dofs_coarse =
3435 scheme.n_dofs_per_cell_coarse / n_components;
3436
3437 for (unsigned int cell = 0; cell < scheme.n_coarse_cells;
3438 cell += n_lanes, ++batch_counter)
3439 {
3440 const unsigned int n_lanes_filled =
3441 (cell + n_lanes > scheme.n_coarse_cells) ?
3442 (scheme.n_coarse_cells - cell) :
3443 n_lanes;
3444
3445 // read from source vector
3446 internal::VectorReader<Number, VectorizedArrayType> reader;
3447 constraint_info_fine.read_write_operation(
3448 reader,
3449 src,
3450 evaluation_data_fine.data(),
3451 cell_counter,
3452 n_lanes_filled,
3453 scheme.n_dofs_per_cell_fine,
3454 false);
3455
3456 // weight
3457 if (weights.size() > 0)
3458 {
3459 const VectorizedArrayType *cell_weights =
3460 weights.data() + weights_start[batch_counter];
3461 if (weights_are_compressed[batch_counter])
3462 internal::
3463 weight_fe_q_dofs_by_entity<dim, -1, VectorizedArrayType>(
3464 cell_weights,
3465 n_components,
3466 scheme.degree_fine + 1,
3467 evaluation_data_fine.data());
3468 else
3469 for (unsigned int i = 0; i < scheme.n_dofs_per_cell_fine;
3470 ++i)
3471 evaluation_data_fine[i] *= cell_weights[i];
3472 }
3473
3474 // ------------------------------ fine ---------------------------
3475 if (needs_interpolation)
3476 for (int c = n_components - 1; c >= 0; --c)
3477 {
3478 internal::CellRestrictor<dim, double, VectorizedArrayType>
3479 cell_restrictor(scheme.prolongation_matrix,
3480 evaluation_data_fine.begin() +
3481 c * n_scalar_dofs_fine,
3482 evaluation_data_coarse.begin() +
3483 c * n_scalar_dofs_coarse);
3484
3485 if (scheme.prolongation_matrix.size() <
3486 n_scalar_dofs_fine * n_scalar_dofs_coarse)
3487 cell_transfer.run(cell_restrictor);
3488 else
3489 cell_restrictor.run_full(n_scalar_dofs_fine,
3490 n_scalar_dofs_coarse);
3491 }
3492 else
3493 evaluation_data_coarse = evaluation_data_fine; // TODO
3494 // ----------------------------- coarse --------------------------
3495
3496 // write into dst vector (similar to
3497 // FEEvaluation::distribute_global_to_local())
3498 internal::VectorDistributorLocalToGlobal<Number,
3499 VectorizedArrayType>
3500 writer;
3501 constraint_info_coarse.apply_hanging_node_constraints(
3502 cell_counter, n_lanes_filled, true, evaluation_data_coarse);
3503 constraint_info_coarse.read_write_operation(
3504 writer,
3505 dst,
3506 evaluation_data_coarse.data(),
3507 cell_counter,
3508 n_lanes_filled,
3509 scheme.n_dofs_per_cell_coarse,
3510 true);
3511
3512 cell_counter += n_lanes_filled;
3513 }
3514 }
3515 }
3516 }
3517
3518
3519
3520 template <int dim, typename VectorType>
3521 void
3522 MGTwoLevelTransfer<dim, VectorType>::interpolate(VectorType &dst,
3523 const VectorType &src) const
3524 {
3525 if (matrix_free_data.get() != nullptr)
3526 {
3527 src.update_ghost_values();
3528 for (unsigned int comp = 0; comp < n_components; ++comp)
3529 {
3530 internal::FEEvaluationNoConstraints<dim, Number> eval_fine(
3531 *matrix_free_data->matrix_free_fine,
3532 matrix_free_data->dof_handler_index_fine,
3533 comp);
3534 FEEvaluation<dim, -1, 0, 1, Number> eval_coarse(
3535 *matrix_free_data->matrix_free_coarse,
3536 matrix_free_data->dof_handler_index_coarse,
3537 0,
3538 comp);
3539
3540 internal::CellTransferFactory cell_transfer(
3541 eval_fine.get_shape_info().data[0].fe_degree,
3542 eval_coarse.get_shape_info().data[0].fe_degree);
3543 for (unsigned int cell = 0;
3544 cell < matrix_free_data->matrix_free_fine->n_cell_batches();
3545 ++cell)
3546 {
3547 eval_fine.reinit(cell);
3548 eval_coarse.reinit(
3549 matrix_free_data->cell_list_fine_to_coarse[cell]);
3550
3551 eval_fine.read_dof_values_unconstrained(src);
3552
3553 if (schemes[0].restriction_matrix.empty() == false)
3554 {
3555 internal::CellRestrictor<dim, double, VectorizedArrayType>
3556 cell_restrictor(schemes[0].restriction_matrix,
3557 eval_fine.begin_dof_values(),
3558 eval_coarse.begin_dof_values());
3559
3560 if (schemes[0].prolongation_matrix.size() <
3561 eval_fine.dofs_per_cell * eval_coarse.dofs_per_cell)
3562 cell_transfer.run(cell_restrictor);
3563 else
3564 cell_restrictor.run_full(eval_fine.dofs_per_cell,
3565 eval_coarse.dofs_per_cell);
3566 }
3567 else
3568 for (unsigned int i = 0; i < eval_fine.dofs_per_cell; ++i)
3569 eval_coarse.begin_dof_values()[i] =
3570 eval_fine.begin_dof_values()[i];
3571
3572 eval_coarse.set_dof_values(dst);
3573 }
3574 }
3575 src.zero_out_ghost_values();
3576 dst.zero_out_ghost_values();
3577 }
3578 else
3579 {
3580 const unsigned int n_lanes = VectorizedArrayType::size();
3581
3582 const bool use_src_inplace = this->vec_fine.size() == 0;
3583 const auto *const vec_fine_ptr = use_src_inplace ? &src : &this->vec_fine;
3584 Assert(vec_fine_ptr->get_partitioner().get() ==
3585 this->partitioner_fine.get(),
3586 ExcInternalError());
3587
3588 const bool use_dst_inplace = this->vec_coarse.size() == 0;
3589 auto *const vec_coarse_ptr = use_dst_inplace ? &dst : &this->vec_coarse;
3590 Assert(vec_coarse_ptr->get_partitioner().get() ==
3591 this->partitioner_coarse.get(),
3592 ExcInternalError());
3593
3594 const bool src_ghosts_have_been_set = src.has_ghost_elements();
3595
3596 if (use_src_inplace == false)
3597 this->vec_fine.copy_locally_owned_data_from(src);
3598
3599 if ((use_src_inplace == false) || (this->vec_fine_needs_ghost_update &&
3600 (src_ghosts_have_been_set == false)))
3601 this->update_ghost_values(*vec_fine_ptr);
3602
3603 *vec_coarse_ptr = Number(0.0);
3604
3605 AlignedVector<VectorizedArrayType> evaluation_data_fine;
3606 AlignedVector<VectorizedArrayType> evaluation_data_coarse;
3607
3608 unsigned int cell_counter = 0;
3609
3610 for (const auto &scheme : schemes)
3611 {
3612 if (scheme.n_coarse_cells == 0)
3613 continue;
3614
3615 if (scheme.n_dofs_per_cell_fine == 0 ||
3616 scheme.n_dofs_per_cell_coarse == 0)
3617 {
3618 cell_counter += scheme.n_coarse_cells;
3619
3620 continue;
3621 }
3622
3623 const bool needs_interpolation =
3624 scheme.restriction_matrix.empty() == false;
3625
3626 // general case -> local restriction is needed
3627 evaluation_data_fine.resize(scheme.n_dofs_per_cell_fine);
3628 evaluation_data_coarse.resize(scheme.n_dofs_per_cell_fine);
3629
3630 internal::CellTransferFactory cell_transfer(scheme.degree_fine,
3631 scheme.degree_coarse);
3632
3633 const unsigned int n_scalar_dofs_fine =
3634 scheme.n_dofs_per_cell_fine / n_components;
3635 const unsigned int n_scalar_dofs_coarse =
3636 scheme.n_dofs_per_cell_coarse / n_components;
3637
3638 for (unsigned int cell = 0; cell < scheme.n_coarse_cells;
3639 cell += n_lanes)
3640 {
3641 const unsigned int n_lanes_filled =
3642 (cell + n_lanes > scheme.n_coarse_cells) ?
3643 (scheme.n_coarse_cells - cell) :
3644 n_lanes;
3645
3646 // read from source vector
3647 internal::VectorReader<Number, VectorizedArrayType> reader;
3648 constraint_info_fine.read_write_operation(
3649 reader,
3650 *vec_fine_ptr,
3651 evaluation_data_fine.data(),
3652 cell_counter,
3653 n_lanes_filled,
3654 scheme.n_dofs_per_cell_fine,
3655 false);
3656
3657 // ------------------------------ fine ---------------------------
3658 if (needs_interpolation)
3659 for (int c = n_components - 1; c >= 0; --c)
3660 {
3661 internal::CellRestrictor<dim, double, VectorizedArrayType>
3662 cell_restrictor(scheme.restriction_matrix,
3663 evaluation_data_fine.begin() +
3664 c * n_scalar_dofs_fine,
3665 evaluation_data_coarse.begin() +
3666 c * n_scalar_dofs_coarse);
3667
3668 if (scheme.restriction_matrix.size() <
3669 n_scalar_dofs_fine * n_scalar_dofs_coarse)
3670 cell_transfer.run(cell_restrictor);
3671 else
3672 cell_restrictor.run_full(n_scalar_dofs_fine,
3673 n_scalar_dofs_coarse);
3674 }
3675 else
3676 evaluation_data_coarse = evaluation_data_fine; // TODO
3677 // ----------------------------- coarse --------------------------
3678
3679 // write into dst vector (similar to
3680 // FEEvaluation::set_dof_values_plain())
3681 internal::VectorSetter<Number, VectorizedArrayType> writer;
3682 constraint_info_coarse.read_write_operation(
3683 writer,
3684 *vec_coarse_ptr,
3685 evaluation_data_coarse.data(),
3686 cell_counter,
3687 n_lanes_filled,
3688 scheme.n_dofs_per_cell_coarse,
3689 false);
3690
3691 cell_counter += n_lanes_filled;
3692 }
3693 }
3694
3695 // clean up related to update_ghost_values()
3696 if (use_src_inplace == false)
3697 vec_fine_ptr->set_ghost_state(false); // internal vector
3698 else if (this->fine_element_is_continuous &&
3699 (src_ghosts_have_been_set == false))
3700 this->zero_out_ghost_values(*vec_fine_ptr); // external vector
3701
3702 if (use_dst_inplace == false)
3703 dst.copy_locally_owned_data_from(this->vec_coarse);
3704 }
3705 }
3706
3707
3708 namespace internal
3709 {
3710 inline bool
3711 is_partitioner_contained(
3712 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
3713 const std::shared_ptr<const Utilities::MPI::Partitioner>
3714 &external_partitioner)
3715 {
3716 // no external partitioner has been given
3717 if (external_partitioner.get() == nullptr)
3718 return false;
3719
3720 // check if locally owned ranges are the same
3721 if (external_partitioner->size() != partitioner->size())
3722 return false;
3723
3724 if (external_partitioner->locally_owned_range() !=
3725 partitioner->locally_owned_range())
3726 return false;
3727
3728 const int ghosts_locally_contained =
3729 ((external_partitioner->ghost_indices() & partitioner->ghost_indices()) ==
3730 partitioner->ghost_indices()) ?
3731 1 :
3732 0;
3733
3734 // check if ghost values are contained in external partititioner
3735 return Utilities::MPI::min(ghosts_locally_contained,
3736 partitioner->get_mpi_communicator()) == 1;
3737 }
3738
3739 inline std::shared_ptr<Utilities::MPI::Partitioner>
3740 create_embedded_partitioner(
3741 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
3742 const std::shared_ptr<const Utilities::MPI::Partitioner>
3743 &larger_partitioner)
3744 {
3745 auto embedded_partitioner = std::make_shared<Utilities::MPI::Partitioner>(
3746 larger_partitioner->locally_owned_range(),
3747 larger_partitioner->get_mpi_communicator());
3748
3749 embedded_partitioner->set_ghost_indices(
3750 partitioner->ghost_indices(), larger_partitioner->ghost_indices());
3751
3752 return embedded_partitioner;
3753 }
3754 } // namespace internal
3755
3756
3757
3758 template <typename VectorType>
3759 template <int dim, std::size_t width, typename IndexType>
3760 std::pair<bool, bool>
3761 MGTwoLevelTransferBase<VectorType>::
3762 internal_enable_inplace_operations_if_possible(
3763 const std::shared_ptr<const Utilities::MPI::Partitioner>
3764 &external_partitioner_coarse,
3765 const std::shared_ptr<const Utilities::MPI::Partitioner>
3766 &external_partitioner_fine,
3767 bool &vec_fine_needs_ghost_update,
3768 internal::MatrixFreeFunctions::
3769 ConstraintInfo<dim, VectorizedArray<Number, width>, IndexType>
3770 &constraint_info_coarse,
3771 std::vector<unsigned int> &dof_indices_fine)
3772 {
3773 std::pair<bool, bool> success_flags = {false, false};
3774
3775 if (this->partitioner_coarse->is_globally_compatible(
3776 *external_partitioner_coarse))
3777 {
3778 this->vec_coarse.reinit(0);
3779 this->partitioner_coarse = external_partitioner_coarse;
3780 success_flags.first = true;
3781 }
3782 else if (internal::is_partitioner_contained(this->partitioner_coarse,
3783 external_partitioner_coarse))
3784 {
3785 this->vec_coarse.reinit(0);
3786
3787 for (auto &i : constraint_info_coarse.dof_indices)
3788 i = external_partitioner_coarse->global_to_local(
3789 this->partitioner_coarse->local_to_global(i));
3790
3791 for (auto &i : constraint_info_coarse.plain_dof_indices)
3792 i = external_partitioner_coarse->global_to_local(
3793 this->partitioner_coarse->local_to_global(i));
3794
3795 this->partitioner_coarse_embedded =
3796 internal::create_embedded_partitioner(this->partitioner_coarse,
3797 external_partitioner_coarse);
3798
3799 this->partitioner_coarse = external_partitioner_coarse;
3800 success_flags.first = true;
3801 }
3802
3803 vec_fine_needs_ghost_update =
3804 Utilities::MPI::max(this->partitioner_fine->ghost_indices().n_elements(),
3805 this->partitioner_fine->get_mpi_communicator()) != 0;
3806
3807 if (this->partitioner_fine->is_globally_compatible(
3808 *external_partitioner_fine))
3809 {
3810 this->vec_fine.reinit(0);
3811 this->partitioner_fine = external_partitioner_fine;
3812 success_flags.second = true;
3813 }
3814 else if (internal::is_partitioner_contained(this->partitioner_fine,
3815 external_partitioner_fine))
3816 {
3817 this->vec_fine.reinit(0);
3818
3819 for (auto &i : dof_indices_fine)
3820 i = external_partitioner_fine->global_to_local(
3821 this->partitioner_fine->local_to_global(i));
3822
3823 this->partitioner_fine_embedded =
3824 internal::create_embedded_partitioner(this->partitioner_fine,
3825 external_partitioner_fine);
3826
3827 this->partitioner_fine = external_partitioner_fine;
3828 success_flags.second = true;
3829 }
3830
3831 return success_flags;
3832 }
3833
3834
3835
3836 template <int dim, typename VectorType>
3837 std::pair<bool, bool>
3838 MGTwoLevelTransfer<dim, VectorType>::enable_inplace_operations_if_possible(
3839 const std::shared_ptr<const Utilities::MPI::Partitioner>
3840 &external_partitioner_coarse,
3841 const std::shared_ptr<const Utilities::MPI::Partitioner>
3842 &external_partitioner_fine)
3843 {
3844 if (matrix_free_data.get() != nullptr)
3845 return std::make_pair(true, true);
3846 else
3847 return this->internal_enable_inplace_operations_if_possible(
3848 external_partitioner_coarse,
3849 external_partitioner_fine,
3850 this->vec_fine_needs_ghost_update,
3851 constraint_info_coarse,
3852 constraint_info_fine.dof_indices);
3853 }
3854
3855
3856
3857 template <int dim, typename VectorType>
3858 void
3859 MGTwoLevelTransfer<dim, VectorType>::reinit_geometric_transfer(
3860 const DoFHandler<dim> &dof_handler_fine,
3861 const DoFHandler<dim> &dof_handler_coarse,
3862 const AffineConstraints<Number> &constraints_fine,
3863 const AffineConstraints<Number> &constraints_coarse,
3864 const unsigned int mg_level_fine,
3865 const unsigned int mg_level_coarse)
3866 {
3867 matrix_free_data.reset();
3868 internal::MGTwoLevelTransferImplementation::reinit_geometric_transfer(
3869 dof_handler_fine,
3870 dof_handler_coarse,
3871 constraints_fine,
3872 constraints_coarse,
3873 mg_level_fine,
3874 mg_level_coarse,
3875 *this);
3876 }
3877
3878
3879
3880 template <int dim, typename VectorType>
3881 void
3882 MGTwoLevelTransfer<dim, VectorType>::reinit_polynomial_transfer(
3883 const DoFHandler<dim> &dof_handler_fine,
3884 const DoFHandler<dim> &dof_handler_coarse,
3885 const AffineConstraints<Number> &constraints_fine,
3886 const AffineConstraints<Number> &constraints_coarse,
3887 const unsigned int mg_level_fine,
3888 const unsigned int mg_level_coarse)
3889 {
3890 matrix_free_data.reset();
3891 internal::MGTwoLevelTransferImplementation::reinit_polynomial_transfer(
3892 dof_handler_fine,
3893 dof_handler_coarse,
3894 constraints_fine,
3895 constraints_coarse,
3896 mg_level_fine,
3897 mg_level_coarse,
3898 *this);
3899 }
3900
3901
3902
3903 template <int dim, typename VectorType>
3904 void
3905 MGTwoLevelTransfer<dim, VectorType>::reinit(
3906 const DoFHandler<dim> &dof_handler_fine,
3907 const DoFHandler<dim> &dof_handler_coarse,
3908 const AffineConstraints<Number> &constraints_fine,
3909 const AffineConstraints<Number> &constraints_coarse,
3910 const unsigned int mg_level_fine,
3911 const unsigned int mg_level_coarse)
3912 {
3913 // determine if polynomial transfer can be performed via the following two
3914 // criteria:
3915 // 1) multigrid levels can be only used with polynomial transfer
3916 bool do_polynomial_transfer =
3917 (mg_level_fine != numbers::invalid_unsigned_int) ||
3918 (mg_level_coarse != numbers::invalid_unsigned_int);
3919
3920 // 2) the meshes are identical
3921 if (do_polynomial_transfer == false)
3922 {
3923 const internal::CellIDTranslator<dim> cell_id_translator(
3924 dof_handler_fine.get_triangulation().n_global_coarse_cells(),
3925 dof_handler_fine.get_triangulation().n_global_levels());
3926
3927 AssertDimension(
3928 dof_handler_fine.get_triangulation().n_global_coarse_cells(),
3929 dof_handler_coarse.get_triangulation().n_global_coarse_cells());
3930 AssertIndexRange(dof_handler_coarse.get_triangulation().n_global_levels(),
3931 dof_handler_fine.get_triangulation().n_global_levels() +
3932 1);
3933
3934 IndexSet is_locally_owned_fine(cell_id_translator.size());
3935 IndexSet is_locally_owned_coarse(cell_id_translator.size());
3936
3937 for (const auto &cell : dof_handler_fine.active_cell_iterators() |
3938 IteratorFilters::LocallyOwnedCell())
3939 is_locally_owned_fine.add_index(cell_id_translator.translate(cell));
3940
3941 for (const auto &cell : dof_handler_coarse.active_cell_iterators() |
3942 IteratorFilters::LocallyOwnedCell())
3943 is_locally_owned_coarse.add_index(cell_id_translator.translate(cell));
3944
3945 const MPI_Comm communicator = dof_handler_fine.get_mpi_communicator();
3946
3947 const std::vector<unsigned int> owning_ranks =
3948 Utilities::MPI::compute_index_owner(is_locally_owned_fine,
3949 is_locally_owned_coarse,
3950 communicator);
3951
3952 bool all_cells_found = true;
3953
3954 for (unsigned i = 0; i < is_locally_owned_coarse.n_elements(); ++i)
3955 all_cells_found &= (owning_ranks[i] != numbers::invalid_unsigned_int);
3956
3957 do_polynomial_transfer =
3958 Utilities::MPI::min(static_cast<unsigned int>(all_cells_found),
3959 communicator) == 1;
3960 }
3961
3962 if (do_polynomial_transfer)
3963 internal::MGTwoLevelTransferImplementation::reinit_polynomial_transfer(
3964 dof_handler_fine,
3965 dof_handler_coarse,
3966 constraints_fine,
3967 constraints_coarse,
3968 mg_level_fine,
3969 mg_level_coarse,
3970 *this);
3971 else
3972 internal::MGTwoLevelTransferImplementation::reinit_geometric_transfer(
3973 dof_handler_fine,
3974 dof_handler_coarse,
3975 constraints_fine,
3976 constraints_coarse,
3977 mg_level_fine,
3978 mg_level_coarse,
3979 *this);
3980 }
3981
3982
3983
3984 template <int dim, typename VectorType>
3985 void
3986 MGTwoLevelTransfer<dim, VectorType>::reinit(
3987 const MatrixFree<dim, Number> &matrix_free_fine,
3988 const unsigned int dof_no_fine,
3989 const MatrixFree<dim, Number> &matrix_free_coarse,
3990 const unsigned int dof_no_coarse)
3991 {
3992 matrix_free_data = std::make_unique<MatrixFreeRelatedData>();
3993
3994 MatrixFreeRelatedData &data = *matrix_free_data;
3995 data.matrix_free_fine = &matrix_free_fine;
3996 data.matrix_free_coarse = &matrix_free_coarse;
3997 AssertIndexRange(dof_no_fine, matrix_free_fine.n_components());
3998 data.dof_handler_index_fine = dof_no_fine;
3999 AssertIndexRange(dof_no_coarse, matrix_free_coarse.n_components());
4000 data.dof_handler_index_coarse = dof_no_coarse;
4001
4002 const DoFHandler<dim> &dof_fine =
4003 matrix_free_fine.get_dof_handler(dof_no_fine);
4004 const DoFHandler<dim> &dof_coarse =
4005 matrix_free_coarse.get_dof_handler(dof_no_coarse);
4006
4007 Assert(&dof_fine.get_triangulation() == &dof_coarse.get_triangulation(),
4008 ExcMessage("You can only use this class if both MatrixFree objects "
4009 "use the same underlying triangulation!"));
4010 AssertDimension(matrix_free_fine.get_mg_level(),
4011 matrix_free_coarse.get_mg_level());
4012
4013 AssertDimension(matrix_free_fine.n_physical_cells(),
4014 matrix_free_coarse.n_physical_cells());
4015
4016 Assert(dof_fine.get_fe_collection().size() == 1,
4017 ExcMessage("hp finite element objects are currently not supported "
4018 "by this class."));
4019 Assert(dof_coarse.get_fe_collection().size() == 1,
4020 ExcMessage("hp finite element objects are currently not supported "
4021 "by this class."));
4022
4023 this->dof_handler_fine = &dof_fine;
4024 this->mg_level_fine = matrix_free_fine.get_mg_level();
4025
4026 const FiniteElement<dim> &fe_fine = dof_fine.get_fe();
4027
4028 AssertDimension(fe_fine.n_components(), dof_coarse.get_fe().n_components());
4029 this->n_components = fe_fine.n_components();
4030
4031 // Compute the link between cells on matrix_free_coarse and
4032 // matrix_free_fine, with the latter controlling the loop
4033 const Triangulation<dim> &tria = dof_fine.get_triangulation();
4034 std::vector<unsigned int> coarse_cell_indices(tria.n_active_cells());
4035 for (unsigned int cell = 0; cell < matrix_free_coarse.n_cell_batches();
4036 ++cell)
4037 for (unsigned int v = 0;
4038 v < matrix_free_coarse.n_active_entries_per_cell_batch(cell);
4039 ++v)
4040 {
4041 coarse_cell_indices[matrix_free_coarse.get_cell_iterator(cell, v)
4042 ->active_cell_index()] =
4043 cell * VectorizedArrayType::size() + v;
4044 }
4045
4046 std::array<unsigned int, VectorizedArrayType::size()> default_entry;
4047 default_entry.fill(numbers::invalid_unsigned_int);
4048 data.cell_list_fine_to_coarse.resize(matrix_free_fine.n_cell_batches(),
4049 default_entry);
4050 for (unsigned int cell = 0; cell < matrix_free_fine.n_cell_batches(); ++cell)
4051 for (unsigned int v = 0;
4052 v < matrix_free_fine.n_active_entries_per_cell_batch(cell);
4053 ++v)
4054 {
4055 data.cell_list_fine_to_coarse[cell][v] =
4056 coarse_cell_indices[matrix_free_fine.get_cell_iterator(cell, v)
4057 ->active_cell_index()];
4058 }
4059
4060 // Compute weights
4061 if (fe_fine.dofs_per_vertex > 0)
4062 {
4063 VectorType weight_vector;
4064 matrix_free_fine.initialize_dof_vector(weight_vector, dof_no_fine);
4065
4066 internal::FEEvaluationNoConstraints<dim, Number> evaluator(
4067 matrix_free_fine, dof_no_fine);
4068 for (unsigned int cell = 0; cell < matrix_free_fine.n_cell_batches();
4069 ++cell)
4070 {
4071 evaluator.reinit(cell);
4072 for (unsigned int i = 0; i < evaluator.dofs_per_cell; ++i)
4073 evaluator.begin_dof_values()[i] = 1.;
4074 evaluator.distribute_local_to_global_unconstrained(weight_vector);
4075 }
4076 weight_vector.compress(VectorOperation::add);
4077
4078 for (unsigned int i = 0; i < weight_vector.locally_owned_size(); ++i)
4079 if (weight_vector.local_element(i) > 0.)
4080 weight_vector.local_element(i) =
4081 Number(1.) / weight_vector.local_element(i);
4082 for (const unsigned int index :
4083 matrix_free_fine.get_constrained_dofs(dof_no_fine))
4084 weight_vector.local_element(index) = 0;
4085 weight_vector.update_ghost_values();
4086
4087 weights.clear();
4088 weights.reserve(matrix_free_fine.n_cell_batches() *
4089 Utilities::pow(3, dim));
4090 weights_start.clear();
4091 weights_start.resize(matrix_free_fine.n_cell_batches());
4092 weights_are_compressed.clear();
4093 weights_are_compressed.resize(weights_start.size());
4094 std::array<VectorizedArrayType, Utilities::pow(3, dim)>
4095 weights_compressed;
4096 for (unsigned int cell = 0; cell < matrix_free_fine.n_cell_batches();
4097 ++cell)
4098 {
4099 weights_start[cell] = weights.size();
4100
4101 weights_compressed.fill(VectorizedArray<Number>());
4102 evaluator.reinit(cell);
4103 evaluator.read_dof_values_unconstrained(weight_vector);
4104
4105 const bool can_compress_weights =
4106 internal::compute_weights_fe_q_dofs_by_entity<dim, -1>(
4107 evaluator.begin_dof_values(),
4108 1,
4109 fe_fine.degree + 1,
4110 weights_compressed.data());
4111 if (can_compress_weights && fe_fine.degree > 1)
4112 {
4113 for (const VectorizedArrayType weight : weights_compressed)
4114 weights.push_back(weight);
4115 weights_are_compressed[cell] = 1;
4116 }
4117 else
4118 for (unsigned int i = 0; i < evaluator.dofs_per_cell; ++i)
4119 weights.push_back(evaluator.begin_dof_values()[i]);
4120 }
4121 }
4122
4123 // Compute interpolation matrices, possibly in the 1D version
4124 schemes.resize(1);
4125 internal::MGTwoLevelTransferImplementation::
4126 compute_prolongation_and_restriction_matrices<dim, Number>(
4127 fe_fine, dof_coarse.get_fe(), schemes[0]);
4128
4129 // We internally call the ghost updates through the matrix-free framework
4130 this->vec_fine_needs_ghost_update = false;
4131
4132 this->partitioner_fine = matrix_free_fine.get_vector_partitioner(dof_no_fine);
4133 this->partitioner_coarse =
4134 matrix_free_coarse.get_vector_partitioner(dof_no_coarse);
4135 }
4136
4137
4138
4139 template <int dim, typename VectorType>
4140 bool
4141 MGTwoLevelTransfer<dim, VectorType>::fast_polynomial_transfer_supported(
4142 const unsigned int fe_degree_fine,
4143 const unsigned int fe_degree_coarse)
4144 {
4145 internal::CellTransferFactory cell_transfer(fe_degree_fine, fe_degree_coarse);
4146 internal::CellProlongatorTest cell_transfer_test;
4147
4148 return cell_transfer.run(cell_transfer_test);
4149 }
4150
4151
4152
4153 template <int dim, typename VectorType>
4154 std::size_t
4155 MGTwoLevelTransfer<dim, VectorType>::memory_consumption() const
4156 {
4157 std::size_t size = 0;
4158
4159 for (const auto &scheme : schemes)
4160 {
4161 size += scheme.prolongation_matrix.memory_consumption();
4162 size += scheme.restriction_matrix.memory_consumption();
4163 }
4164
4165 if (matrix_free_data.get() != nullptr)
4166 size += MemoryConsumption::memory_consumption(
4167 matrix_free_data->cell_list_fine_to_coarse);
4168
4169 size += this->partitioner_fine->memory_consumption();
4170 size += this->partitioner_coarse->memory_consumption();
4171 size += this->vec_fine.memory_consumption();
4172 size += this->vec_coarse.memory_consumption();
4173 size += weights.memory_consumption();
4174 size += MemoryConsumption::memory_consumption(weights_start);
4175 size += MemoryConsumption::memory_consumption(weights_are_compressed);
4176 size += constraint_info_coarse.memory_consumption();
4177 size += constraint_info_fine.memory_consumption();
4178
4179 return size;
4180 }
4181
4182
4183
4184 template <typename VectorType>
4185 void
4186 MGTwoLevelTransferBase<VectorType>::update_ghost_values(
4187 const VectorType &vec) const
4188 {
4189 if ((vec.get_partitioner().get() == this->partitioner_coarse.get()) &&
4190 (this->partitioner_coarse_embedded != nullptr))
4191 internal::SimpleVectorDataExchange<Number>(
4192 this->partitioner_coarse_embedded, this->buffer_coarse_embedded)
4193 .update_ghost_values(vec);
4194 else if ((vec.get_partitioner().get() == this->partitioner_fine.get()) &&
4195 (this->partitioner_fine_embedded != nullptr))
4196 internal::SimpleVectorDataExchange<Number>(this->partitioner_fine_embedded,
4197 this->buffer_fine_embedded)
4198 .update_ghost_values(vec);
4199 else
4200 vec.update_ghost_values();
4201 }
4202
4203
4204
4205 template <typename VectorType>
4206 void
4207 MGTwoLevelTransferBase<VectorType>::compress(
4208 VectorType &vec,
4209 const VectorOperation::values op) const
4210 {
4211 Assert(op == VectorOperation::add, ExcNotImplemented());
4212
4213 if ((vec.get_partitioner().get() == this->partitioner_coarse.get()) &&
4214 (this->partitioner_coarse_embedded != nullptr))
4215 internal::SimpleVectorDataExchange<Number>(
4216 this->partitioner_coarse_embedded, this->buffer_coarse_embedded)
4217 .compress(vec);
4218 else if ((vec.get_partitioner().get() == this->partitioner_fine.get()) &&
4219 (this->partitioner_fine_embedded != nullptr))
4220 internal::SimpleVectorDataExchange<Number>(this->partitioner_fine_embedded,
4221 this->buffer_fine_embedded)
4222 .compress(vec);
4223 else
4224 vec.compress(op);
4225 }
4226
4227
4228
4229 template <typename VectorType>
4230 void
4231 MGTwoLevelTransferBase<VectorType>::zero_out_ghost_values(
4232 const VectorType &vec) const
4233 {
4234 if ((vec.get_partitioner().get() == this->partitioner_coarse.get()) &&
4235 (this->partitioner_coarse_embedded != nullptr))
4236 internal::SimpleVectorDataExchange<Number>(
4237 this->partitioner_coarse_embedded, this->buffer_coarse_embedded)
4238 .zero_out_ghost_values(vec);
4239 else if ((vec.get_partitioner().get() == (this->partitioner_fine.get()) &&
4240 this->partitioner_fine_embedded != nullptr))
4241 internal::SimpleVectorDataExchange<Number>(this->partitioner_fine_embedded,
4242 this->buffer_fine_embedded)
4243 .zero_out_ghost_values(vec);
4244 else
4245 vec.zero_out_ghost_values();
4246 }
4247
4248
4249
4250 template <int dim, typename Number, typename MemorySpace>
4251 MGTransferMF<dim, Number, MemorySpace>::MGTransferMF()
4252 {
4253 this->transfer.clear();
4254 this->internal_transfer.clear();
4255 }
4256
4257
4258
4259 template <int dim, typename Number, typename MemorySpace>
4260 MGTransferMF<dim, Number, MemorySpace>::MGTransferMF(
4261 const MGConstrainedDoFs &mg_constrained_dofs)
4262 {
4263 this->transfer.clear();
4264 this->internal_transfer.clear();
4265 this->initialize_constraints(mg_constrained_dofs);
4266 }
4267
4268
4269
4270 template <int dim, typename Number, typename MemorySpace>
4271 void
4272 MGTransferMF<dim, Number, MemorySpace>::initialize_constraints(
4273 const MGConstrainedDoFs &mg_constrained_dofs)
4274 {
4275 this->mg_constrained_dofs = &mg_constrained_dofs;
4276 }
4277
4278
4279
4280 template <int dim, typename Number, typename MemorySpace>
4281 void
4282 MGTransferMF<dim, Number, MemorySpace>::initialize_internal_transfer(
4283 const DoFHandler<dim> &dof_handler,
4284 const ObserverPointer<const MGConstrainedDoFs> &mg_constrained_dofs)
4285 {
4286 const unsigned int min_level = 0;
4287 const unsigned int max_level =
4288 dof_handler.get_triangulation().n_global_levels() - 1;
4289
4290 MGLevelObject<AffineConstraints<typename VectorType::value_type>> constraints(
4291 min_level, max_level);
4292
4293 if (mg_constrained_dofs)
4294 for (unsigned int l = min_level; l <= max_level; ++l)
4295 mg_constrained_dofs->merge_constraints(
4296 constraints[l],
4297 l,
4298 /*add_boundary_indices*/ true,
4299 /*add_refinement_edge_indices*/ false,
4300 /*add_level_constraints*/ true,
4301 /*add_user_constraints*/ true);
4302
4303 this->internal_transfer.resize(min_level, max_level);
4304
4305 for (unsigned int l = min_level; l < max_level; ++l)
4306 internal_transfer[l + 1].reinit_geometric_transfer(
4307 dof_handler, dof_handler, constraints[l + 1], constraints[l], l + 1, l);
4308 }
4309
4310
4311
4312 template <int dim, typename Number, typename MemorySpace>
4313 std::pair<const DoFHandler<dim> *, unsigned int>
4314 MGTransferMF<dim, Number, MemorySpace>::get_dof_handler_fine() const
4315 {
4316 if (this->transfer.n_levels() <= 1)
4317 // single level: the information cannot be retrieved
4318 return {nullptr, numbers::invalid_unsigned_int};
4319
4320 if (const auto t = dynamic_cast<const MGTwoLevelTransfer<
4321 dim,
4322 LinearAlgebra::distributed::Vector<Number, MemorySpace>> *>(
4323 this->transfer[this->transfer.max_level()].get()))
4324 {
4325 return {t->dof_handler_fine, t->mg_level_fine};
4326 }
4327
4328 if constexpr (std::is_same_v<MemorySpace, ::dealii::MemorySpace::Host>)
4329 {
4330 // MGTwoLevelTransferNonNested transfer is only instantiated for Host
4331 // memory:
4332 if (const auto t = dynamic_cast<const MGTwoLevelTransferNonNested<
4333 dim,
4334 LinearAlgebra::distributed::Vector<Number, MemorySpace>> *>(
4335 this->transfer[this->transfer.max_level()].get()))
4336 {
4337 return {t->dof_handler_fine, t->mg_level_fine};
4338 }
4339 }
4340
4341 {
4342 DEAL_II_NOT_IMPLEMENTED();
4343 return {nullptr, numbers::invalid_unsigned_int};
4344 }
4345 }
4346
4347
4348
4349 template <int dim, typename Number, typename MemorySpace>
4350 void
4351 MGTransferMF<dim, Number, MemorySpace>::
4352 fill_and_communicate_copy_indices_global_coarsening(
4353 const DoFHandler<dim> &dof_handler_out)
4354 {
4355 const auto dof_handler_and_level_in = get_dof_handler_fine();
4356 const auto dof_handler_in = dof_handler_and_level_in.first;
4357 const auto level_in = dof_handler_and_level_in.second;
4358
4359 if ((dof_handler_in == nullptr) || (dof_handler_in == &dof_handler_out))
4360 return; // nothing to do
4361
4362 this->copy_indices.resize(1);
4363 this->copy_indices[0].reinit(2, dof_handler_out.n_locally_owned_dofs());
4364
4365 std::vector<types::global_dof_index> dof_indices_in;
4366 std::vector<types::global_dof_index> dof_indices_out;
4367
4368 this->perform_plain_copy = true;
4369
4370 const auto &is_out = (level_in == numbers::invalid_unsigned_int) ?
4371 dof_handler_out.locally_owned_dofs() :
4372 dof_handler_out.locally_owned_mg_dofs(level_in);
4373
4374 const auto &is_in = (level_in == numbers::invalid_unsigned_int) ?
4375 dof_handler_in->locally_owned_dofs() :
4376 dof_handler_in->locally_owned_mg_dofs(level_in);
4377
4378 internal::loop_over_active_or_level_cells(
4379 dof_handler_in->get_triangulation(), level_in, [&](const auto &cell) {
4380 const auto cell_id = cell->id();
4381
4382 Assert(
4383 dof_handler_out.get_triangulation().contains_cell(cell_id),
4384 ExcMessage(
4385 "DoFHandler instances used for set up of MGTransferMF and copy_to_mg(), "
4386 "copy_from_mg(), or interpolate_to_mg() are not compatible."));
4387
4388 if (level_in == numbers::invalid_unsigned_int)
4389 {
4390 const auto cell_in = cell->as_dof_handler_iterator(*dof_handler_in);
4391 const auto cell_out = dof_handler_out.get_triangulation()
4392 .create_cell_iterator(cell_id)
4393 ->as_dof_handler_iterator(dof_handler_out);
4394
4395 AssertDimension(cell_in->get_fe().n_dofs_per_cell(),
4396 cell_out->get_fe().n_dofs_per_cell());
4397
4398 dof_indices_in.resize(cell_in->get_fe().n_dofs_per_cell());
4399 dof_indices_out.resize(cell_out->get_fe().n_dofs_per_cell());
4400
4401 cell_in->get_dof_indices(dof_indices_in);
4402 cell_out->get_dof_indices(dof_indices_out);
4403 }
4404 else
4405 {
4406 const auto cell_in =
4407 cell->as_dof_handler_level_iterator(*dof_handler_in);
4408 const auto cell_out =
4409 dof_handler_out.get_triangulation()
4410 .create_cell_iterator(cell_id)
4411 ->as_dof_handler_level_iterator(dof_handler_out);
4412
4413 AssertDimension(cell_in->get_fe().n_dofs_per_cell(),
4414 cell_out->get_fe().n_dofs_per_cell());
4415
4416 dof_indices_in.resize(cell_in->get_fe().n_dofs_per_cell());
4417 dof_indices_out.resize(cell_out->get_fe().n_dofs_per_cell());
4418
4419 cell_in->get_mg_dof_indices(dof_indices_in);
4420 cell_out->get_mg_dof_indices(dof_indices_out);
4421 }
4422
4423 this->perform_plain_copy &= (dof_indices_in == dof_indices_out);
4424
4425 for (unsigned int i = 0; i < dof_indices_in.size(); ++i)
4426 if (is_out.is_element(dof_indices_out[i]))
4427 this->copy_indices[0](1,
4428 is_out.index_within_set(dof_indices_out[i])) =
4429 is_in.index_within_set(dof_indices_in[i]);
4430 });
4431
4432
4433 this->perform_plain_copy =
4434 Utilities::MPI::max(this->perform_plain_copy ? 1 : 0,
4435 dof_handler_out.get_mpi_communicator()) != 0;
4436
4437 if (this->perform_plain_copy)
4438 {
4439 this->copy_indices.clear();
4440 }
4441 else
4442 {
4443 this->perform_renumbered_plain_copy = true;
4444 this->solution_copy_indices = this->copy_indices;
4445 }
4446 }
4447
4448
4449
4450 template <int dim, typename Number, typename MemorySpace>
4451 void
4452 MGTransferMF<dim, Number, MemorySpace>::build(
4453 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
4454 &external_partitioners)
4455 {
4456 this->external_partitioners = external_partitioners;
4457
4458 if (this->external_partitioners.size() > 0)
4459 {
4460 const unsigned int min_level = transfer.min_level();
4461 const unsigned int max_level = transfer.max_level();
4462
4463 AssertDimension(this->external_partitioners.size(), transfer.n_levels());
4464
4465 for (unsigned int l = min_level + 1; l <= max_level; ++l)
4466 transfer[l]->enable_inplace_operations_if_possible(
4467 this->external_partitioners[l - 1 - min_level],
4468 this->external_partitioners[l - min_level]);
4469 }
4470 else
4471 {
4472 const unsigned int min_level = transfer.min_level();
4473 const unsigned int max_level = transfer.max_level();
4474
4475 for (unsigned int l = min_level + 1; l <= max_level; ++l)
4476 {
4477 if (l == min_level + 1)
4478 this->external_partitioners.push_back(
4479 transfer[l]->partitioner_coarse);
4480
4481 this->external_partitioners.push_back(transfer[l]->partitioner_fine);
4482 }
4483 }
4484
4485 this->perform_plain_copy = true;
4486 this->perform_renumbered_plain_copy = false;
4487 }
4488
4489
4490
4491 template <int dim, typename Number, typename MemorySpace>
4492 void
4493 MGTransferMF<dim, Number, MemorySpace>::build(
4494 const std::function<void(const unsigned int, VectorType &)>
4495 &initialize_dof_vector)
4496 {
4497 if (initialize_dof_vector)
4498 {
4499 const unsigned int min_level = transfer.min_level();
4500 const unsigned int max_level = transfer.max_level();
4501 const unsigned int n_levels = transfer.n_levels();
4502
4503 std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
4504 external_partitioners(n_levels);
4505
4506 for (unsigned int l = min_level; l <= max_level; ++l)
4507 {
4508 LinearAlgebra::distributed::Vector<typename VectorType::value_type,
4509 MemorySpace>
4510 vector;
4511 initialize_dof_vector(l, vector);
4512 external_partitioners[l - min_level] = vector.get_partitioner();
4513 }
4514
4515 this->build(external_partitioners);
4516 }
4517 else
4518 {
4519 this->build();
4520 }
4521 }
4522
4523
4524
4525 template <int dim, typename Number, typename MemorySpace>
4526 void
4527 MGTransferMF<dim, Number, MemorySpace>::build(
4528 const DoFHandler<dim> &dof_handler,
4529 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
4530 &external_partitioners)
4531 {
4532 const bool use_local_smoothing =
4533 this->transfer.n_levels() == 0 || this->internal_transfer.n_levels() > 0;
4534
4535 if (use_local_smoothing)
4536 {
4537 this->initialize_internal_transfer(dof_handler,
4538 this->mg_constrained_dofs);
4539 this->initialize_transfer_references(internal_transfer);
4540 }
4541
4542 this->build(external_partitioners);
4543
4544 if (use_local_smoothing)
4545 this->fill_and_communicate_copy_indices(dof_handler);
4546 else
4547 this->fill_and_communicate_copy_indices_global_coarsening(dof_handler);
4548 }
4549
4550
4551
4552 template <int dim, typename Number, typename MemorySpace>
4553 void
4554 MGTransferMF<dim, Number, MemorySpace>::build(
4555 const DoFHandler<dim> &dof_handler,
4556 const std::function<void(const unsigned int, VectorType &)>
4557 &initialize_dof_vector)
4558 {
4559 const bool use_local_smoothing =
4560 this->transfer.n_levels() == 0 || this->internal_transfer.n_levels() > 0;
4561
4562 if (use_local_smoothing)
4563 {
4564 this->initialize_internal_transfer(dof_handler,
4565 this->mg_constrained_dofs);
4566 this->initialize_transfer_references(internal_transfer);
4567 }
4568
4569 this->build(initialize_dof_vector);
4570
4571 if (use_local_smoothing)
4572 this->fill_and_communicate_copy_indices(dof_handler);
4573 else
4574 this->fill_and_communicate_copy_indices_global_coarsening(dof_handler);
4575 }
4576
4577
4578
4579 template <int dim, typename Number, typename MemorySpace>
4580 void
4581 MGTransferMF<dim, Number, MemorySpace>::prolongate(const unsigned int to_level,
4582 VectorType &dst,
4583 const VectorType &src) const
4584 {
4585 dst = Number(0.0);
4586 prolongate_and_add(to_level, dst, src);
4587 }
4588
4589
4590
4591 template <int dim, typename Number, typename MemorySpace>
4592 void
4593 MGTransferMF<dim, Number, MemorySpace>::prolongate_and_add(
4594 const unsigned int to_level,
4595 VectorType &dst,
4596 const VectorType &src) const
4597 {
4598 this->transfer[to_level]->prolongate_and_add(dst, src);
4599 }
4600
4601
4602
4603 template <int dim, typename Number, typename MemorySpace>
4604 void
4605 MGTransferMF<dim, Number, MemorySpace>::restrict_and_add(
4606 const unsigned int from_level,
4607 VectorType &dst,
4608 const VectorType &src) const
4609 {
4610 this->transfer[from_level]->restrict_and_add(dst, src);
4611 }
4612
4613
4614
4615 template <int dim, typename Number, typename MemorySpace>
4616 void
4617 MGTransferMF<dim, Number, MemorySpace>::assert_dof_handler(
4618 const DoFHandler<dim> &dof_handler_out) const
4619 {
4620 (void)dof_handler_out;
4621 if constexpr (running_in_debug_mode())
4622 {
4623 const auto dof_handler_and_level_in = get_dof_handler_fine();
4624 const auto dof_handler_in = dof_handler_and_level_in.first;
4625 const auto level_in = dof_handler_and_level_in.second;
4626
4627 if ((dof_handler_out.n_dofs() == 0) || // dummy DoFHandler
4628 (dof_handler_in == nullptr) || // single level
4629 (dof_handler_in == &dof_handler_out // same DoFHandler
4630 ))
4631 return; // nothing to do
4632
4633 if (this->perform_plain_copy)
4634 {
4635 // global-coarsening path: compare indices of cells
4636
4637 std::vector<types::global_dof_index> dof_indices_in;
4638 std::vector<types::global_dof_index> dof_indices_out;
4639
4640 internal::loop_over_active_or_level_cells(
4641 dof_handler_in->get_triangulation(),
4642 level_in,
4643 [&](const auto &cell) {
4644 const auto cell_id = cell->id();
4645
4646 Assert(
4647 dof_handler_out.get_triangulation().contains_cell(cell_id),
4648 ExcMessage(
4649 "DoFHandler instances used for set up of MGTransferMF and copy_to_mg(), "
4650 "copy_from_mg(), or interpolate_to_mg() are not compatible."));
4651
4652 if (level_in == numbers::invalid_unsigned_int)
4653 {
4654 const auto cell_in =
4655 cell->as_dof_handler_iterator(*dof_handler_in);
4656 const auto cell_out =
4657 dof_handler_out.get_triangulation()
4658 .create_cell_iterator(cell_id)
4659 ->as_dof_handler_iterator(dof_handler_out);
4660
4661 AssertDimension(cell_in->get_fe().n_dofs_per_cell(),
4662 cell_out->get_fe().n_dofs_per_cell());
4663
4664 dof_indices_in.resize(cell_in->get_fe().n_dofs_per_cell());
4665 dof_indices_out.resize(cell_out->get_fe().n_dofs_per_cell());
4666
4667 cell_in->get_dof_indices(dof_indices_in);
4668 cell_out->get_dof_indices(dof_indices_out);
4669 }
4670 else
4671 {
4672 const auto cell_in =
4673 cell->as_dof_handler_level_iterator(*dof_handler_in);
4674 const auto cell_out =
4675 dof_handler_out.get_triangulation()
4676 .create_cell_iterator(cell_id)
4677 ->as_dof_handler_level_iterator(dof_handler_out);
4678
4679 AssertDimension(cell_in->get_fe().n_dofs_per_cell(),
4680 cell_out->get_fe().n_dofs_per_cell());
4681
4682 dof_indices_in.resize(cell_in->get_fe().n_dofs_per_cell());
4683 dof_indices_out.resize(cell_out->get_fe().n_dofs_per_cell());
4684
4685 cell_in->get_mg_dof_indices(dof_indices_in);
4686 cell_out->get_mg_dof_indices(dof_indices_out);
4687 }
4688
4689 Assert(
4690 dof_indices_in == dof_indices_out,
4691 ExcMessage(
4692 "DoFHandler instances used for set up of MGTransferMF and copy_to_mg(), "
4693 "copy_from_mg(), or interpolate_to_mg() are not compatible."));
4694 });
4695 }
4696 else if (this->perform_renumbered_plain_copy)
4697 {
4698 // nothing to do
4699 }
4700 }
4701 }
4702
4703
4704
4705 template <int dim, typename Number, typename MemorySpace>
4706 std::size_t
4707 MGTransferMF<dim, Number, MemorySpace>::memory_consumption() const
4708 {
4709 std::size_t size = 0;
4710
4711 const unsigned int min_level = transfer.min_level();
4712 const unsigned int max_level = transfer.max_level();
4713
4714 for (unsigned int l = min_level + 1; l <= max_level; ++l)
4715 size += this->transfer[l]->memory_consumption();
4716
4717 return size;
4718 }
4719
4720
4721
4722 template <int dim, typename Number, typename MemorySpace>
4723 inline unsigned int
4724 MGTransferMF<dim, Number, MemorySpace>::min_level() const
4725 {
4726 return transfer.min_level();
4727 }
4728
4729
4730
4731 template <int dim, typename Number, typename MemorySpace>
4732 inline unsigned int
4733 MGTransferMF<dim, Number, MemorySpace>::max_level() const
4734 {
4735 return transfer.max_level();
4736 }
4737
4738
4739 template <int dim, typename Number, typename MemorySpace>
4740 inline void
4741 MGTransferMF<dim, Number, MemorySpace>::clear()
4742 {
4743 MGLevelGlobalTransfer<VectorType>::clear();
4744
4745 internal_transfer.clear();
4746 transfer.clear();
4747 external_partitioners.clear();
4748 }
4749
4750
4751
4752 template <int dim, typename Number>
4753 MGTransferBlockMF<dim, Number>::MGTransferBlockMF(
4754 const MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>
4755 &transfer_operator)
4756 : MGTransferBlockMatrixFreeBase<
4757 dim,
4758 Number,
4759 MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>>(true)
4760 {
4761 this->transfer_operators = {&transfer_operator};
4762 }
4763
4764
4765
4766 template <int dim, typename Number>
4767 MGTransferBlockMF<dim, Number>::MGTransferBlockMF(
4768 const MGConstrainedDoFs &mg_constrained_dofs)
4769 : MGTransferBlockMatrixFreeBase<
4770 dim,
4771 Number,
4772 MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>>(true)
4773 {
4774 initialize_constraints(mg_constrained_dofs);
4775 }
4776
4777
4778
4779 template <int dim, typename Number>
4780 MGTransferBlockMF<dim, Number>::MGTransferBlockMF(
4781 const std::vector<MGConstrainedDoFs> &mg_constrained_dofs)
4782 : MGTransferBlockMatrixFreeBase<
4783 dim,
4784 Number,
4785 MGTransferMF<dim, Number, ::dealii::MemorySpace::Host>>(false)
4786 {
4787 initialize_constraints(mg_constrained_dofs);
4788 }
4789
4790
4791
4792 template <int dim, typename Number>
4793 void
4794 MGTransferBlockMF<dim, Number>::initialize_constraints(
4795 const MGConstrainedDoFs &mg_constrained_dofs)
4796 {
4797 this->transfer_operators_internal.clear();
4798 this->transfer_operators.clear();
4799
4800 Assert(this->same_for_all,
4801 ExcMessage("This object was initialized with support for usage with "
4802 "one DoFHandler for each block, but this method assumes "
4803 "that the same DoFHandler is used for all the blocks!"));
4804
4805 this->transfer_operators_internal.emplace_back(mg_constrained_dofs);
4806 this->transfer_operators = {&transfer_operators_internal.back()};
4807 }
4808
4809
4810
4811 template <int dim, typename Number>
4812 void
4813 MGTransferBlockMF<dim, Number>::initialize_constraints(
4814 const std::vector<MGConstrainedDoFs> &mg_constrained_dofs)
4815 {
4816 this->transfer_operators_internal.clear();
4817 this->transfer_operators.clear();
4818
4819 Assert(!this->same_for_all,
4820 ExcMessage("This object was initialized with support for using "
4821 "the same DoFHandler for all the blocks, but this "
4822 "method assumes that there is a separate DoFHandler "
4823 "for each block!"));
4824
4825 for (const auto &dofs : mg_constrained_dofs)
4826 this->transfer_operators_internal.emplace_back(dofs);
4827
4828 for (const auto &transfer : this->transfer_operators_internal)
4829 this->transfer_operators.emplace_back(&transfer);
4830 }
4831
4832
4833
4834 template <int dim, typename Number>
4835 void
4836 MGTransferBlockMF<dim, Number>::build(const DoFHandler<dim> &dof_handler)
4837 {
4838 AssertDimension(transfer_operators.size(), 1);
4839 this->transfer_operators_internal[0].build(dof_handler);
4840 }
4841
4842
4843
4844 template <int dim, typename Number>
4845 void
4846 MGTransferBlockMF<dim, Number>::build(
4847 const std::vector<const DoFHandler<dim> *> &dof_handler)
4848 {
4849 AssertDimension(transfer_operators.size(), dof_handler.size());
4850 AssertDimension(transfer_operators_internal.size(), dof_handler.size());
4851
4852 for (unsigned int i = 0; i < dof_handler.size(); ++i)
4853 this->transfer_operators_internal[i].build(*dof_handler[i]);
4854 }
4855
4856
4857
4858 template <int dim, typename Number>
4859 const MGTransferMF<dim, Number, ::dealii::MemorySpace::Host> &
4860 MGTransferBlockMF<dim, Number>::get_matrix_free_transfer(
4861 const unsigned int b) const
4862 {
4863 AssertIndexRange(b, transfer_operators.size());
4864 return *transfer_operators[b];
4865 }
4866
4867 namespace internal
4868 {
4869 template <int dim, typename Number>
4870 std::shared_ptr<NonMatching::MappingInfo<dim, dim, Number>>
4871 fill_mapping_info(const Utilities::MPI::RemotePointEvaluation<dim> &rpe)
4872 {
4873 const auto &cell_data = rpe.get_cell_data();
4874
4875 std::vector<typename Triangulation<dim>::active_cell_iterator>
4876 cell_iterators;
4877 std::vector<std::vector<Point<dim>>> unit_points_vector;
4878
4879 for (unsigned int i = 0; i < cell_data.cells.size(); ++i)
4880 {
4881 typename Triangulation<dim>::active_cell_iterator cell(
4882 &rpe.get_triangulation(),
4883 cell_data.cells[i].first,
4884 cell_data.cells[i].second);
4885
4886 const ArrayView<const Point<dim>> unit_points(
4887 cell_data.reference_point_values.data() +
4888 cell_data.reference_point_ptrs[i],
4889 cell_data.reference_point_ptrs[i + 1] -
4890 cell_data.reference_point_ptrs[i]);
4891
4892 cell_iterators.emplace_back(cell);
4893 unit_points_vector.emplace_back(unit_points.begin(), unit_points.end());
4894 }
4895
4896 typename NonMatching::MappingInfo<dim, dim, Number>::AdditionalData ad;
4897 ad.store_cells = true;
4898
4899 auto mapping_info =
4900 std::make_shared<NonMatching::MappingInfo<dim, dim, Number>>(
4901 rpe.get_mapping(), update_values, ad);
4902 mapping_info->reinit_cells(cell_iterators, unit_points_vector);
4903
4904 return mapping_info;
4905 }
4906
4907 /**
4908 * This function provides information which DoF index is associated with
4909 * a support point.
4910 *
4911 * @param[in] dof_handler DoFHandler with @c FE_DGQ or @c FE_Q elements
4912 * providing DoF indices which are collected at support points.
4913 * @param[in] dof_handler_support_points DoFHandler with one component used
4914 * to determine support point indices (the underlying finite element is @c
4915 * FE_Q or @c FE_DGQ in case of polynomial degree 0).
4916 * @param[in] constraint AffineConstrains associated with @p dof_handler.
4917 * Only unconstrained DoFs are considered
4918 * @return a tuple containing 0) local support point indices,
4919 * 1) pointers to global DoF indices, and 2) global DoF indices.
4920 */
4921 template <int dim, int spacedim, typename Number>
4922 std::tuple<std::vector<unsigned int>,
4923 std::vector<unsigned int>,
4924 std::vector<types::global_dof_index>>
4925 support_point_indices_to_dof_indices(
4926 const DoFHandler<dim, spacedim> &dof_handler,
4927 const DoFHandler<dim, spacedim> &dof_handler_support_points,
4928 const dealii::AffineConstraints<Number> &constraint)
4929 {
4930 // in case a FE_DGQ space of order 0 is provided, DoFs indices are always
4931 // uniquely assigned to support points (they are always defined in the
4932 // center of the element) and are never shared at vertices or faces.
4933 Assert((dynamic_cast<const FE_DGQ<dim, spacedim> *>(
4934 &dof_handler.get_fe().base_element(0)) != nullptr) ||
4935 (dynamic_cast<const FE_Q<dim, spacedim> *>(
4936 &dof_handler.get_fe().base_element(0)) != nullptr) ||
4937 (dynamic_cast<const FE_SimplexP<dim, spacedim> *>(
4938 &dof_handler.get_fe().base_element(0)) != nullptr) ||
4939 (dynamic_cast<const FE_SimplexDGP<dim, spacedim> *>(
4940 &dof_handler.get_fe().base_element(0)) != nullptr),
4941 ExcMessage("Function expects FE_DGQ, FE_Q, FE_SimplexP, or "
4942 "FE_SimplexDGP in dof_handler."));
4943
4944 Assert(
4945 (dynamic_cast<const FE_Q<dim, spacedim> *>(
4946 &dof_handler_support_points.get_fe().base_element(0)) != nullptr ||
4947 dynamic_cast<const FE_SimplexP<dim, spacedim> *>(
4948 &dof_handler_support_points.get_fe().base_element(0)) != nullptr) ||
4949 ((dynamic_cast<const FE_DGQ<dim, spacedim> *>(
4950 &dof_handler_support_points.get_fe().base_element(0)) != nullptr ||
4951 dynamic_cast<const FE_SimplexDGP<dim, spacedim> *>(
4952 &dof_handler_support_points.get_fe().base_element(0)) != nullptr) &&
4953 dof_handler_support_points.get_fe().degree == 0),
4954 ExcMessage("Function expects (FE_DGQ||FE_SimplexDGP)&&degree==0 or "
4955 "(FE_Q||FE_SimplexP) in dof_handler_support_points."));
4956
4957 Assert(
4958 dof_handler_support_points.get_fe().n_components() == 1,
4959 ExcMessage(
4960 "dof_handler_support_points needs element with exactly one component."));
4961
4962 Assert(&dof_handler.get_triangulation() ==
4963 &dof_handler_support_points.get_triangulation(),
4964 ExcMessage("DoFHandlers need the same underlying triangulation."));
4965
4966 Assert(dof_handler.get_fe().degree ==
4967 dof_handler_support_points.get_fe().degree,
4968 ExcMessage("DoFHandlers need the same degree."));
4969
4970 Assert(dof_handler.get_fe().is_primitive(),
4971 ExcMessage("Only primitive elements are allowed."));
4972
4973 const auto degree = dof_handler.get_fe().degree;
4974 const auto dofs_per_cell = dof_handler.get_fe().n_dofs_per_cell();
4975 const auto support_points_per_cell =
4976 dof_handler_support_points.get_fe().n_dofs_per_cell();
4977
4978 std::vector<std::pair<unsigned int, types::global_dof_index>>
4979 support_point_dofs;
4980 support_point_dofs.reserve(dof_handler.n_locally_owned_dofs());
4981
4982 const unsigned int n_components = dof_handler.get_fe().n_components();
4983
4984 // fill support_point_dofs
4985 {
4986 // Support points have a hierarchic numbering, L2 DoFs have
4987 // lexicographic numbering. Therefore, we need to convert the DoF
4988 // indices if DoFHandler is L2 conforming and has degree > 0.
4989 const bool needs_conversion =
4990 dof_handler.get_fe().conforming_space ==
4991 FiniteElementData<dim>::Conformity::L2 &&
4992 (dof_handler.get_fe().degree > 0) &&
4993 dof_handler.get_fe().reference_cell().is_hyper_cube();
4994 std::vector<unsigned int> lexicographic_to_hierarchic;
4995 if (needs_conversion)
4996 lexicographic_to_hierarchic =
4997 FETools::lexicographic_to_hierarchic_numbering<dim>(degree);
4998
4999 const Utilities::MPI::Partitioner partitioner_support_points(
5000 dof_handler_support_points.locally_owned_dofs(),
5001 dof_handler_support_points.get_mpi_communicator());
5002
5003 const Utilities::MPI::Partitioner partitioner_dof(
5004 dof_handler.locally_owned_dofs(),
5005 DoFTools::extract_locally_relevant_dofs(dof_handler),
5006 dof_handler.get_mpi_communicator());
5007
5008 std::vector<bool> dof_processed(partitioner_dof.locally_owned_size() +
5009 partitioner_dof.n_ghost_indices(),
5010 false);
5011
5012
5013 std::vector<types::global_dof_index> support_point_indices(
5014 support_points_per_cell);
5015 std::vector<types::global_dof_index> dof_indices(dofs_per_cell);
5016 std::vector<std::pair<unsigned int, types::global_dof_index>>
5017 support_point_dofs_comp;
5018 support_point_dofs_comp.reserve(n_components);
5019
5020 for (const auto &cell : dof_handler.active_cell_iterators())
5021 {
5022 if (cell->is_locally_owned() || cell->is_ghost())
5023 {
5024 const auto cell_support_point =
5025 cell->as_dof_handler_iterator(dof_handler_support_points);
5026
5027 cell_support_point->get_dof_indices(support_point_indices);
5028 cell->get_dof_indices(dof_indices);
5029
5030 // collect unconstrained DoFs for support point. In case of DG
5031 // elements with polynomial degree > 0 or continuous elements
5032 // with multiple components, more DoFs are associated to the
5033 // same support point.
5034 for (unsigned int i = 0; i < support_point_indices.size(); ++i)
5035 if (partitioner_support_points.in_local_range(
5036 support_point_indices[i]))
5037 {
5038 for (unsigned int c = 0; c < n_components; ++c)
5039 {
5040 const auto global_dof_idx =
5041 needs_conversion ?
5042 dof_indices
5043 [dof_handler.get_fe().component_to_system_index(
5044 c, lexicographic_to_hierarchic[i])] :
5045 dof_indices[dof_handler.get_fe()
5046 .component_to_system_index(c, i)];
5047
5048 const auto local_dof_idx =
5049 partitioner_dof.global_to_local(global_dof_idx);
5050
5051 AssertIndexRange(local_dof_idx, dof_processed.size());
5052
5053 if (dof_processed[local_dof_idx] == false)
5054 {
5055 if (!constraint.is_constrained(global_dof_idx))
5056 support_point_dofs_comp.emplace_back(
5057 partitioner_support_points.global_to_local(
5058 support_point_indices[i]),
5059 global_dof_idx);
5060
5061 dof_processed[local_dof_idx] = true;
5062 }
5063 }
5064
5065 Assert(support_point_dofs_comp.size() == 0 ||
5066 support_point_dofs_comp.size() == n_components,
5067 ExcNotImplemented());
5068
5069 if (support_point_dofs_comp.empty() == false)
5070 support_point_dofs.insert(support_point_dofs.end(),
5071 support_point_dofs_comp.begin(),
5072 support_point_dofs_comp.end());
5073
5074 support_point_dofs_comp.clear();
5075 }
5076 }
5077 }
5078 }
5079
5080 // sort for support points (stable sort needed for multiple components)
5081 std::stable_sort(support_point_dofs.begin(),
5082 support_point_dofs.end(),
5083 [](const auto &a, const auto &b) {
5084 return a.first < b.first;
5085 });
5086
5087 // convert to CRS format
5088 std::vector<types::global_dof_index> dof_indices;
5089 dof_indices.reserve(support_point_dofs.size());
5090 std::vector<unsigned int> dof_ptrs;
5091 dof_ptrs.reserve(dof_handler_support_points.n_locally_owned_dofs() + 1);
5092 dof_ptrs.push_back(0);
5093 std::vector<unsigned int> support_point_indices;
5094 support_point_indices.reserve(
5095 dof_handler_support_points.n_locally_owned_dofs());
5096
5097 auto it = support_point_dofs.begin();
5098 while (it != support_point_dofs.end())
5099 {
5100 const unsigned int index = std::get<0>(*it);
5101 while (it != support_point_dofs.end() && it->first == index)
5102 {
5103 dof_indices.push_back(it->second);
5104 ++it;
5105 }
5106 support_point_indices.push_back(index);
5107 dof_ptrs.push_back(dof_indices.size() / n_components);
5108 }
5109
5110 return std::make_tuple(std::move(support_point_indices),
5111 std::move(dof_ptrs),
5112 std::move(dof_indices));
5113 }
5114
5115
5116 /**
5117 * Create DoFHandler with unique support points.
5118 */
5119 template <int dim, int spacedim>
5120 std::shared_ptr<const DoFHandler<dim, spacedim>>
5121 create_support_point_dof_handler(const DoFHandler<dim, spacedim> &dof_handler)
5122 {
5123 const auto &fe = dof_handler.get_fe();
5124 const auto &tria = dof_handler.get_triangulation();
5125 const auto degree = fe.degree;
5126 const auto n_components = fe.n_components();
5127
5128 if (n_components == 1 &&
5129 ((fe.reference_cell().is_hyper_cube() ||
5130 fe.reference_cell().is_simplex()) &&
5131 (fe.conforming_space == FiniteElementData<dim>::Conformity::H1 ||
5132 degree == 0)))
5133 {
5134 // in case a DG space of order 0 is provided, DoFs indices are always
5135 // uniquely assigned to support points (they are always defined in the
5136 // center of the element) and are never shared at vertices or faces.
5137 return std::shared_ptr<const DoFHandler<dim, spacedim>>(&dof_handler,
5138 [](auto *) {});
5139 }
5140 else
5141 {
5142 // Create dummy dof handler for support point numbering.
5143 // Unique support points are generally numbered according to FE_Q with
5144 // n_components==1. If degree==0 we use FE_DGQ which ensures a unique
5145 // support point numbering since the support point is located in the
5146 // center of the cell.
5147 auto dof_handler_support_points =
5148 std::make_shared<DoFHandler<dim, spacedim>>(tria);
5149
5150 if (fe.reference_cell().is_simplex() && (degree == 0))
5151 dof_handler_support_points->distribute_dofs(
5152 FE_SimplexDGP<dim, spacedim>(degree));
5153 else if (fe.reference_cell().is_simplex())
5154 dof_handler_support_points->distribute_dofs(
5155 FE_SimplexP<dim, spacedim>(degree));
5156 else if (degree == 0)
5157 dof_handler_support_points->distribute_dofs(
5158 FE_DGQ<dim, spacedim>(degree));
5159 else
5160 dof_handler_support_points->distribute_dofs(
5161 FE_Q<dim, spacedim>(degree));
5162
5163 return dof_handler_support_points;
5164 }
5165 }
5166
5167 // Loop over cells and collect unique set of points
5168 template <int dim, typename Number>
5169 std::tuple<std::vector<Point<dim>>,
5170 std::vector<unsigned int>,
5171 std::vector<types::global_dof_index>>
5172 collect_unconstrained_unique_support_points(
5173 const DoFHandler<dim> &dof_handler,
5174 const Mapping<dim> &mapping,
5175 const dealii::AffineConstraints<Number> &constraint)
5176 {
5177 AssertThrow(dof_handler.get_fe().has_support_points(), ExcNotImplemented());
5178
5179 // create DoFHandler for support points
5180 const auto dof_handler_support_points =
5181 create_support_point_dof_handler(dof_handler);
5182
5183 // compute mapping: index of locally owned support points to (global) DoF
5184 // indices
5185 const auto support_point_dofs_crs =
5186 support_point_indices_to_dof_indices(dof_handler,
5187 *dof_handler_support_points,
5188 constraint);
5189
5190 const std::vector<unsigned int> &local_support_point_indices =
5191 std::get<0>(support_point_dofs_crs);
5192
5193 // compute locally owned support points
5194 std::vector<Point<dim>> points;
5195 points.resize(local_support_point_indices.size());
5196
5197 const auto locally_owned_support_point =
5198 dof_handler_support_points->locally_owned_dofs();
5199 std::vector<unsigned int> indices_state(
5200 locally_owned_support_point.n_elements(), numbers::invalid_unsigned_int);
5201
5202 AssertIndexRange(local_support_point_indices.size(),
5203 indices_state.size() + 1);
5204
5205 for (unsigned int i = 0; i < local_support_point_indices.size(); ++i)
5206 indices_state[local_support_point_indices[i]] = i;
5207
5208 const auto &fe_support_point = dof_handler_support_points->get_fe();
5209 FEValues<dim> fe_values(mapping,
5210 fe_support_point,
5211 Quadrature<dim>(
5212 fe_support_point.get_unit_support_points()),
5213 update_quadrature_points);
5214
5215 std::vector<types::global_dof_index> dof_indices(
5216 fe_support_point.n_dofs_per_cell());
5217
5218 for (const auto &cell :
5219 dof_handler_support_points->active_cell_iterators() |
5220 IteratorFilters::LocallyOwnedCell())
5221 {
5222 fe_values.reinit(cell);
5223 cell->get_dof_indices(dof_indices);
5224
5225 for (const unsigned int q : fe_values.quadrature_point_indices())
5226 if (locally_owned_support_point.is_element(dof_indices[q]))
5227 {
5228 const auto index =
5229 locally_owned_support_point.index_within_set(dof_indices[q]);
5230
5231 if (indices_state[index] != numbers::invalid_unsigned_int)
5232 {
5233 points[indices_state[index]] = fe_values.quadrature_point(q);
5234 indices_state[index] = numbers::invalid_unsigned_int;
5235 }
5236 }
5237 }
5238
5239 return std::make_tuple(
5240 std::move(points),
5241 std::move(std::get<1>(support_point_dofs_crs)), // global_dofs_ptrs
5242 std::move(std::get<2>(support_point_dofs_crs))); // global_dofs_indices
5243 }
5244
5245 } // namespace internal
5246
5247
5248
5249 template <int dim, typename VectorType>
5250 MGTwoLevelTransferNonNested<dim, VectorType>::MGTwoLevelTransferNonNested(
5251 const AdditionalData &data)
5252 : additional_data(data)
5253 , rpe(typename Utilities::MPI::RemotePointEvaluation<dim>::AdditionalData(
5254 data.tolerance,
5255 false,
5256 data.rtree_level,
5257 {}))
5258 {}
5259
5260 template <int dim, typename VectorType>
5261 void
5262 MGTwoLevelTransferNonNested<dim, VectorType>::reinit(
5263 const DoFHandler<dim> &dof_handler_fine,
5264 const DoFHandler<dim> &dof_handler_coarse,
5265 const Mapping<dim> &mapping_fine,
5266 const Mapping<dim> &mapping_coarse,
5267 const AffineConstraints<Number> &constraint_fine,
5268 const AffineConstraints<Number> &constraint_coarse)
5269 {
5270 AssertThrow(dof_handler_coarse.get_fe().has_support_points(),
5271 ExcNotImplemented());
5272 Assert(dof_handler_coarse.get_fe().n_components() > 0 &&
5273 dof_handler_fine.get_fe().n_components() > 0,
5274 ExcNotImplemented());
5275
5276 this->dof_handler_fine = &dof_handler_fine;
5277 this->mg_level_fine = numbers::invalid_unsigned_int;
5278
5279 this->fine_element_is_continuous =
5280 dof_handler_fine.get_fe().n_dofs_per_vertex() > 0;
5281
5282 // collect points, ptrs, and global indices
5283 const auto points_ptrs_indices =
5284 internal::collect_unconstrained_unique_support_points(dof_handler_fine,
5285 mapping_fine,
5286 constraint_fine);
5287 const auto &global_dof_indices = std::get<2>(points_ptrs_indices);
5288
5289 // create partitioners and internal vectors
5290 {
5291 this->partitioner_coarse =
5292 internal::MGTwoLevelTransferImplementation::create_coarse_partitioner(
5293 dof_handler_coarse, constraint_coarse, numbers::invalid_unsigned_int);
5294
5295 this->vec_coarse.reinit(this->partitioner_coarse);
5296 }
5297 {
5298 // in case a DG space of order 0 is provided, DoFs indices are never defined
5299 // on element faces or vertices and therefore, the partitioner is fine
5300 IndexSet locally_relevant_dofs(dof_handler_fine.n_dofs());
5301 if (!this->fine_element_is_continuous &&
5302 dof_handler_fine.get_fe().degree != 0)
5303 locally_relevant_dofs.add_indices(global_dof_indices.begin(),
5304 global_dof_indices.end());
5305
5306 this->partitioner_fine.reset(
5307 new Utilities::MPI::Partitioner(dof_handler_fine.locally_owned_dofs(),
5308 locally_relevant_dofs,
5309 dof_handler_fine.get_mpi_communicator()));
5310
5311 this->vec_fine.reinit(this->partitioner_fine);
5312 }
5313
5314 const auto &points = std::get<0>(points_ptrs_indices);
5315
5316 // using level_dof_indices_fine_ptrs always works but in case of CG or DG
5317 // with degree==0 and n_components==1 support points to dof mapping is unique
5318 // and we don't need it.
5319 if (dof_handler_fine.get_fe().n_components() == 1 &&
5320 (this->fine_element_is_continuous ||
5321 dof_handler_fine.get_fe().degree == 0))
5322 this->level_dof_indices_fine_ptrs.clear();
5323 else
5324 this->level_dof_indices_fine_ptrs = std::get<1>(points_ptrs_indices);
5325
5326 // fill level_dof_indices_fine with local indices
5327 this->level_dof_indices_fine.resize(global_dof_indices.size());
5328 for (unsigned int i = 0; i < global_dof_indices.size(); ++i)
5329 this->level_dof_indices_fine[i] =
5330 this->partitioner_fine->global_to_local(global_dof_indices[i]);
5331
5332 // hand points over to RPE
5333 rpe.reinit(points, dof_handler_coarse.get_triangulation(), mapping_coarse);
5334
5335 AssertThrow(
5336 !additional_data.enforce_all_points_found || rpe.all_points_found(),
5337 ExcMessage(
5338 "You requested that all points should be found, but this didn'thappen."
5339 " You can change this option through the AdditionalData struct in the constructor."));
5340
5341 // set up MappingInfo for easier data access
5342 mapping_info = internal::fill_mapping_info<dim, Number>(rpe);
5343
5344 // set up constraints
5345 const auto &cell_data = rpe.get_cell_data();
5346
5347 constraint_info.reinit(dof_handler_coarse,
5348 cell_data.cells.size(),
5349 false /*TODO*/);
5350
5351 for (unsigned int i = 0; i < cell_data.cells.size(); ++i)
5352 {
5353 typename DoFHandler<dim>::active_cell_iterator cell(
5354 &rpe.get_triangulation(),
5355 cell_data.cells[i].first,
5356 cell_data.cells[i].second,
5357 &dof_handler_coarse);
5358
5359 constraint_info.read_dof_indices(i,
5360 numbers::invalid_unsigned_int,
5361 cell,
5362 constraint_coarse,
5363 this->partitioner_coarse);
5364 }
5365
5366 constraint_info.finalize();
5367
5368 const auto &fe_base = dof_handler_coarse.get_fe().base_element(0);
5369 const unsigned int n_components = dof_handler_coarse.get_fe().n_components();
5370
5371 if (const auto fe = dynamic_cast<const FE_Q<dim> *>(&fe_base))
5372 fe_coarse = std::make_unique<FESystem<dim>>(FE_DGQ<dim>(fe->get_degree()),
5373 n_components);
5374 else if (const auto fe = dynamic_cast<const FE_SimplexP<dim> *>(&fe_base))
5375 fe_coarse =
5376 std::make_unique<FESystem<dim>>(FE_SimplexDGP<dim>(fe->get_degree()),
5377 n_components);
5378 else if (dynamic_cast<const FE_DGQ<dim> *>(&fe_base) ||
5379 dynamic_cast<const FE_SimplexP<dim> *>(&fe_base))
5380 fe_coarse = dof_handler_coarse.get_fe().clone();
5381 else
5382 AssertThrow(false, ExcMessage(dof_handler_coarse.get_fe().get_name()));
5383 }
5384
5385
5386 // Access utilities for scalar or vector valued fields computed with
5387 // FEPointEvaluation. Mimicking the members of
5388 // internal::FEPointEvaluation::EvaluatorTypeTraits
5389 namespace internal
5390 {
5391 template <typename T>
5392 const T &
5393 access(const T &value, const unsigned int)
5394 {
5395 return value;
5396 }
5397
5398 template <typename T>
5399 T &
5400 access(T &value, const unsigned int)
5401 {
5402 return value;
5403 }
5404
5405 template <int dim, typename T>
5406 const T &
5407 access(const Tensor<1, dim, T> &value, const unsigned int c)
5408 {
5409 return value[c];
5410 }
5411
5412 template <int dim, typename T>
5413 T &
5414 access(Tensor<1, dim, T> &value, const unsigned int c)
5415 {
5416 return value[c];
5417 }
5418 } // namespace internal
5419
5420
5421
5422 template <int dim, typename VectorType>
5423 template <int n_components>
5424 void
5425 MGTwoLevelTransferNonNested<dim, VectorType>::prolongate_and_add_internal_comp(
5426 VectorType &dst,
5427 const VectorType &src) const
5428 {
5429 const auto evaluation_function = [&](auto &values, const auto &cell_data) {
5430 this->signals_non_nested.prolongation_cell_loop(true);
5431 std::vector<Number> solution_values;
5432
5433 FEPointEvaluation<n_components, dim, dim, Number> evaluator(*mapping_info,
5434 *fe_coarse);
5435
5436 const auto &send_permutation = rpe.get_send_permutation();
5437
5438 for (unsigned int cell = 0; cell < cell_data.cells.size(); ++cell)
5439 {
5440 solution_values.resize(fe_coarse->n_dofs_per_cell());
5441
5442 // gather and resolve constraints
5443 internal::VectorReader<Number, VectorizedArrayType> reader;
5444 constraint_info.read_write_operation(
5445 reader,
5446 src,
5447 reinterpret_cast<VectorizedArrayType *>(solution_values.data()),
5448 cell,
5449 1,
5450 solution_values.size(),
5451 true);
5452
5453 // evaluate and scatter
5454 evaluator.reinit(cell);
5455
5456 evaluator.evaluate(solution_values, dealii::EvaluationFlags::values);
5457
5458 for (const auto q : evaluator.quadrature_point_indices())
5459 {
5460 const unsigned int index =
5461 send_permutation[q + cell_data.reference_point_ptrs[cell]];
5462
5463 for (unsigned int c = 0; c < n_components; ++c)
5464 values[index * n_components + c] =
5465 internal::access(evaluator.get_value(q), c);
5466 }
5467 }
5468 this->signals_non_nested.prolongation_cell_loop(false);
5469 };
5470
5471 this->signals_non_nested.prolongation(true);
5472
5473 std::vector<Number> &evaluation_point_results = this->rpe_input_output;
5474 std::vector<Number> &buffer = this->rpe_buffer;
5475
5476 rpe.template evaluate_and_process<Number, n_components>(
5477 evaluation_point_results, buffer, evaluation_function, false);
5478 this->signals_non_nested.prolongation(false);
5479
5480 // Keep a vector of typical inverse touch counts that avoid divisions, all
5481 // other cases are handled by a division in the code below.
5482 std::array<Number, 8> typical_weights;
5483 for (unsigned int i = 0; i < typical_weights.size(); ++i)
5484 typical_weights[i] = Number(1) / Number(i + 1);
5485
5486 const bool must_interpolate = (rpe.is_map_unique() == false);
5487 const auto &ptr = rpe.get_point_ptrs();
5488 const auto &recv_permutation_inv = rpe.get_inverse_recv_permutation();
5489 for (unsigned int j = 0; j < ptr.size() - 1; ++j)
5490 {
5491 std::array<Number, n_components> result;
5492 // Weight operator in case some points are owned by multiple cells.
5493 if (must_interpolate)
5494 {
5495 const unsigned int n_entries = ptr[j + 1] - ptr[j];
5496
5497 if (n_entries > 1)
5498 {
5499 result = {};
5500 for (unsigned int k = 0; k < n_entries; ++k)
5501 {
5502 const unsigned int index = recv_permutation_inv[ptr[j] + k];
5503
5504 for (unsigned int c = 0; c < n_components; ++c)
5505 result[c] +=
5506 evaluation_point_results[index * n_components + c];
5507 }
5508 if (n_entries <= typical_weights.size())
5509 for (unsigned int c = 0; c < n_components; ++c)
5510 result[c] *= typical_weights[n_entries - 1];
5511 else
5512 for (unsigned int c = 0; c < n_components; ++c)
5513 result[c] *= Number(1) / Number(n_entries);
5514 }
5515 else if (n_entries == 1)
5516 {
5517 const unsigned int index = recv_permutation_inv[ptr[j]];
5518
5519 for (unsigned int c = 0; c < n_components; ++c)
5520 result[c] = evaluation_point_results[index * n_components + c];
5521 }
5522 else
5523 result = {};
5524 }
5525 else
5526 for (unsigned int c = 0; c < n_components; ++c)
5527 result[c] = evaluation_point_results[j * n_components + c];
5528
5529 if (level_dof_indices_fine_ptrs.empty())
5530 {
5531 for (unsigned int c = 0; c < n_components; ++c)
5532 {
5533 AssertIndexRange(n_components * j + c,
5534 this->level_dof_indices_fine.size());
5535 dst.local_element(
5536 this->level_dof_indices_fine[n_components * j + c]) +=
5537 result[c];
5538 }
5539 }
5540 else
5541 {
5542 for (unsigned int i = this->level_dof_indices_fine_ptrs[j];
5543 i < this->level_dof_indices_fine_ptrs[j + 1];
5544 ++i)
5545 for (unsigned int c = 0; c < n_components; ++c)
5546 {
5547 AssertIndexRange(n_components * i + c,
5548 this->level_dof_indices_fine.size());
5549 dst.local_element(
5550 this->level_dof_indices_fine[n_components * i + c]) +=
5551 result[c];
5552 }
5553 }
5554 }
5555 }
5556
5557
5558
5559 template <int dim, typename VectorType>
5560 void
5561 MGTwoLevelTransferNonNested<dim, VectorType>::prolongate_and_add_internal(
5562 VectorType &dst,
5563 const VectorType &src) const
5564 {
5565 if (this->fe_coarse->n_components() == 1)
5566 prolongate_and_add_internal_comp<1>(dst, src);
5567 else if (this->fe_coarse->n_components() == dim)
5568 prolongate_and_add_internal_comp<dim>(dst, src);
5569 else
5570 AssertThrow(false, ExcNotImplemented());
5571 }
5572
5573
5574
5575 template <int dim, typename VectorType>
5576 template <int n_components>
5577 void
5578 MGTwoLevelTransferNonNested<dim, VectorType>::restrict_and_add_internal_comp(
5579 VectorType &dst,
5580 const VectorType &src) const
5581 {
5582 std::vector<Number> &evaluation_point_results = this->rpe_input_output;
5583 std::vector<Number> &buffer = this->rpe_buffer;
5584
5585 std::array<Number, 8> typical_weights;
5586 for (unsigned int i = 0; i < typical_weights.size(); ++i)
5587 typical_weights[i] = Number(1) / Number(i + 1);
5588
5589 const bool must_interpolate = (rpe.is_map_unique() == false);
5590 const auto &ptr = rpe.get_point_ptrs();
5591 const auto &recv_permutation_inv = rpe.get_inverse_recv_permutation();
5592 evaluation_point_results.resize(ptr.back() * n_components);
5593
5594 for (unsigned int j = 0; j < ptr.size() - 1; ++j)
5595 {
5596 std::array<Number, n_components> result;
5597 if (level_dof_indices_fine_ptrs.empty())
5598 {
5599 for (unsigned int c = 0; c < n_components; ++c)
5600 {
5601 AssertIndexRange(n_components * j + c,
5602 this->level_dof_indices_fine.size());
5603
5604 result[c] = src.local_element(
5605 this->level_dof_indices_fine[n_components * j + c]);
5606 }
5607 }
5608 else
5609 {
5610 result = {};
5611
5612 for (unsigned int i = this->level_dof_indices_fine_ptrs[j];
5613 i < this->level_dof_indices_fine_ptrs[j + 1];
5614 ++i)
5615 {
5616 for (unsigned int c = 0; c < n_components; ++c)
5617 {
5618 AssertIndexRange(n_components * i + c,
5619 this->level_dof_indices_fine.size());
5620 result[c] += src.local_element(
5621 this->level_dof_indices_fine[n_components * i + c]);
5622 }
5623 }
5624 }
5625 if (must_interpolate)
5626 {
5627 const unsigned int n_entries = ptr[j + 1] - ptr[j];
5628 if (n_entries > 1)
5629 {
5630 if (n_entries <= typical_weights.size())
5631 for (unsigned int c = 0; c < n_components; ++c)
5632 result[c] *= typical_weights[n_entries - 1];
5633 else
5634 for (unsigned int c = 0; c < n_components; ++c)
5635 result[c] /= Number(n_entries);
5636 }
5637 }
5638
5639 for (unsigned int i = ptr[j]; i < ptr[j + 1]; ++i)
5640 {
5641 const unsigned int index = recv_permutation_inv[i];
5642
5643 for (unsigned int c = 0; c < n_components; ++c)
5644 evaluation_point_results[index * n_components + c] = result[c];
5645 }
5646 }
5647
5648 const auto evaluation_function = [&](const auto &values,
5649 const auto &cell_data) {
5650 this->signals_non_nested.restriction_cell_loop(true);
5651 std::vector<Number> solution_values;
5652 FEPointEvaluation<n_components, dim, dim, Number> evaluator(*mapping_info,
5653 *fe_coarse);
5654
5655 const auto &send_permutation = rpe.get_send_permutation();
5656
5657 for (unsigned int cell = 0; cell < cell_data.cells.size(); ++cell)
5658 {
5659 solution_values.resize(fe_coarse->n_dofs_per_cell());
5660
5661 // gather and integrate
5662 evaluator.reinit(cell);
5663
5664 for (const auto q : evaluator.quadrature_point_indices())
5665 {
5666 typename internal::FEPointEvaluation::
5667 EvaluatorTypeTraits<dim, dim, n_components, Number>::value_type
5668 value;
5669
5670 const unsigned int index =
5671 send_permutation[q + cell_data.reference_point_ptrs[cell]];
5672
5673 for (unsigned int c = 0; c < n_components; ++c)
5674 internal::access(value, c) = values[index * n_components + c];
5675
5676 evaluator.submit_value(value, q);
5677 }
5678
5679 evaluator.test_and_sum(solution_values, EvaluationFlags::values);
5680
5681 // resolve constraints and scatter
5682 internal::VectorDistributorLocalToGlobal<Number, VectorizedArrayType>
5683 writer;
5684 constraint_info.read_write_operation(
5685 writer,
5686 dst,
5687 reinterpret_cast<VectorizedArrayType *>(solution_values.data()),
5688 cell,
5689 1,
5690 solution_values.size(),
5691 true);
5692 }
5693 this->signals_non_nested.restriction_cell_loop(false);
5694 };
5695
5696 this->signals_non_nested.restriction(true);
5697 rpe.template process_and_evaluate<Number, n_components>(
5698 evaluation_point_results, buffer, evaluation_function, false);
5699 this->signals_non_nested.restriction(false);
5700 }
5701
5702
5703
5704 template <int dim, typename VectorType>
5705 void
5706 MGTwoLevelTransferNonNested<dim, VectorType>::restrict_and_add_internal(
5707 VectorType &dst,
5708 const VectorType &src) const
5709 {
5710 if (this->fe_coarse->n_components() == 1)
5711 restrict_and_add_internal_comp<1>(dst, src);
5712 else if (this->fe_coarse->n_components() == dim)
5713 restrict_and_add_internal_comp<dim>(dst, src);
5714 else
5715 AssertThrow(false, ExcNotImplemented());
5716 }
5717
5718
5719
5720 template <int dim, typename VectorType>
5721 void
5722 MGTwoLevelTransferNonNested<dim, VectorType>::interpolate(
5723 VectorType &dst,
5724 const VectorType &src) const
5725 {
5726 AssertThrow(false, ExcNotImplemented());
5727 (void)dst;
5728 (void)src;
5729 }
5730
5731
5732
5733 template <int dim, typename VectorType>
5734 std::pair<bool, bool>
5735 MGTwoLevelTransferNonNested<dim, VectorType>::
5736 enable_inplace_operations_if_possible(
5737 const std::shared_ptr<const Utilities::MPI::Partitioner>
5738 &external_partitioner_coarse,
5739 const std::shared_ptr<const Utilities::MPI::Partitioner>
5740 &external_partitioner_fine)
5741 {
5742 return this->internal_enable_inplace_operations_if_possible(
5743 external_partitioner_coarse,
5744 external_partitioner_fine,
5745 this->vec_fine_needs_ghost_update,
5746 constraint_info,
5747 this->level_dof_indices_fine);
5748 }
5749
5750
5751
5752 template <int dim, typename VectorType>
5753 std::size_t
5754 MGTwoLevelTransferNonNested<dim, VectorType>::memory_consumption() const
5755 {
5756 std::size_t size = 0;
5757
5758 size += this->partitioner_coarse->memory_consumption();
5759 size += this->vec_coarse.memory_consumption();
5760 size += MemoryConsumption::memory_consumption(this->level_dof_indices_fine);
5761 // TODO: add consumption for rpe, mapping_info and constraint_info.
5762
5763 return size;
5764 }
5765
5766
5767
5768 template <int dim, typename VectorType>
5769 boost::signals2::connection
5770 MGTwoLevelTransferNonNested<dim, VectorType>::connect_prolongation_cell_loop(
5771 const std::function<void(const bool)> &slot)
5772 {
5773 return signals_non_nested.prolongation_cell_loop.connect(slot);
5774 }
5775
5776
5777
5778 template <int dim, typename VectorType>
5779 boost::signals2::connection
5780 MGTwoLevelTransferNonNested<dim, VectorType>::connect_restriction_cell_loop(
5781 const std::function<void(const bool)> &slot)
5782 {
5783 return signals_non_nested.restriction_cell_loop.connect(slot);
5784 }
5785
5786
5787
5788 template <int dim, typename VectorType>
5789 boost::signals2::connection
5790 MGTwoLevelTransferNonNested<dim, VectorType>::connect_prolongation(
5791 const std::function<void(const bool)> &slot)
5792 {
5793 return signals_non_nested.prolongation.connect(slot);
5794 }
5795
5796
5797
5798 template <int dim, typename VectorType>
5799 boost::signals2::connection
5800 MGTwoLevelTransferNonNested<dim, VectorType>::connect_restriction(
5801 const std::function<void(const bool)> &slot)
5802 {
5803 return signals_non_nested.restriction.connect(slot);
5804 }
5805
5806
5807 DEAL_II_NAMESPACE_CLOSE
5808
5809 #endif