ryujin 2.1.1 revision 955e869188d49b3c97ca7b1cf4fd9ceb0e6f46ef
offline_data.template.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2020 - 2024 by the ryujin authors
4//
5
6#pragma once
7
8#include "discretization.h"
11#include "offline_data.h"
12#include "scratch_data.h"
13#include "sparse_matrix_simd.template.h" /* instantiate read_in */
14
15#include <deal.II/base/graph_coloring.h>
16#include <deal.II/base/parallel.h>
17#include <deal.II/base/work_stream.h>
18#include <deal.II/dofs/dof_renumbering.h>
19#include <deal.II/dofs/dof_tools.h>
20#include <deal.II/fe/fe_values.h>
21#include <deal.II/grid/grid_tools.h>
22#include <deal.II/lac/dynamic_sparsity_pattern.h>
23#include <deal.II/lac/la_parallel_vector.h>
24#ifdef DEAL_II_WITH_TRILINOS
25#include <deal.II/lac/trilinos_sparse_matrix.h>
26#endif
27
28#ifdef FORCE_DEAL_II_SPARSE_MATRIX
29#undef DEAL_II_WITH_TRILINOS
30#endif
31
32namespace ryujin
33{
34 using namespace dealii;
35
36
37 template <int dim, typename Number>
39 const MPIEnsemble &mpi_ensemble,
40 const Discretization<dim> &discretization,
41 const std::string &subsection /*= "OfflineData"*/)
42 : ParameterAcceptor(subsection)
43 , mpi_ensemble_(mpi_ensemble)
44 , discretization_(&discretization)
45 {
46 incidence_relaxation_even_ = 0.5;
47 add_parameter("incidence matrix relaxation even degree",
48 incidence_relaxation_even_,
49 "Scaling exponent for incidence matrix used for "
50 "discontinuous finite elements with even degree. The default "
51 "value 0.5 scales the jump penalization with (h_i+h_j)^0.5.");
52
53 incidence_relaxation_odd_ = 0.0;
54 add_parameter("incidence matrix relaxation odd degree",
55 incidence_relaxation_odd_,
56 "Scaling exponent for incidence matrix used for "
57 "discontinuous finite elements with even degree. The default "
58 "value of 0.0 sets the jump penalization to a constant 1.");
59 }
60
61
62 template <int dim, typename Number>
64 {
65 /*
66 * First, we set up the locally_relevant index set, determine (globally
67 * indexed) affine constraints and create a (globally indexed) sparsity
68 * pattern:
69 */
70
71 auto &dof_handler = *dof_handler_;
72 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
73
74 IndexSet locally_relevant;
75 DoFTools::extract_locally_relevant_dofs(dof_handler, locally_relevant);
76
77 affine_constraints_.reinit(locally_relevant);
78 DoFTools::make_hanging_node_constraints(dof_handler, affine_constraints_);
79
80#ifndef DEAL_II_WITH_TRILINOS
81 AssertThrow(affine_constraints_.n_constraints() == 0,
82 ExcMessage("ryujin was built without Trilinos support - no "
83 "hanging node support available"));
84#endif
85
86 /*
87 * Enforce periodic boundary conditions. We assume that the mesh is in
88 * "normal configuration."
89 */
90
91 const auto &periodic_faces =
92 discretization_->triangulation().get_periodic_face_map();
93
94 for (const auto &[left, value] : periodic_faces) {
95 const auto &[right, orientation] = value;
96
97 typename DoFHandler<dim>::cell_iterator dof_cell_left(
98 &left.first->get_triangulation(),
99 left.first->level(),
100 left.first->index(),
101 &dof_handler);
102
103 typename DoFHandler<dim>::cell_iterator dof_cell_right(
104 &right.first->get_triangulation(),
105 right.first->level(),
106 right.first->index(),
107 &dof_handler);
108
109 if constexpr (std::is_same_v<Number, double>) {
110 DoFTools::make_periodicity_constraints(
111 dof_cell_left->face(left.second),
112 dof_cell_right->face(right.second),
113 affine_constraints_,
114 ComponentMask(),
115#if DEAL_II_VERSION_GTE(9, 6, 0)
116 orientation);
117#else
118 /* orientation */ orientation[0],
119 /* flip */ orientation[1],
120 /* rotation */ orientation[2]);
121#endif
122 } else {
123 AssertThrow(false, dealii::ExcNotImplemented());
124 __builtin_trap();
125 }
126 }
127
128 affine_constraints_.close();
129
130#ifdef DEBUG
131 {
132 /* Check that constraints are consistent in parallel: */
133 const std::vector<IndexSet> &locally_owned_dofs =
134 Utilities::MPI::all_gather(mpi_ensemble_.ensemble_communicator(),
135 dof_handler.locally_owned_dofs());
136 const IndexSet locally_active =
137 dealii::DoFTools::extract_locally_active_dofs(dof_handler);
138 Assert(affine_constraints_.is_consistent_in_parallel(
139 locally_owned_dofs,
140 locally_active,
141 mpi_ensemble_.ensemble_communicator(),
142 /*verbose*/ true),
143 ExcInternalError());
144 }
145#endif
146
147 sparsity_pattern_.reinit(
148 dof_handler.n_dofs(), dof_handler.n_dofs(), locally_relevant);
149
150 if (discretization_->have_discontinuous_ansatz()) {
151 /*
152 * Create dG sparsity pattern:
153 */
155 dof_handler, sparsity_pattern_, affine_constraints_, false);
156 } else {
157 /*
158 * Create cG sparsity pattern:
159 */
160#ifdef DEAL_II_WITH_TRILINOS
161 DoFTools::make_sparsity_pattern(
162 dof_handler, sparsity_pattern_, affine_constraints_, false);
163#else
164 /*
165 * In case we use dealii::SparseMatrix<Number> for assembly we need a
166 * sparsity pattern that also includes the full locally relevant -
167 * locally relevant coupling block. This gets thrown out again later,
168 * but nevertheless we have to add it.
169 */
171 dof_handler, sparsity_pattern_, affine_constraints_, false);
172#endif
173 }
174
175 /*
176 * We have to complete the local stencil to have consistent size over
177 * all MPI ranks. Otherwise, MPI synchronization in our
178 * SparseMatrixSIMD class will fail.
179 */
180
181 SparsityTools::distribute_sparsity_pattern(
182 sparsity_pattern_,
183 locally_owned,
184 mpi_ensemble_.ensemble_communicator(),
185 locally_relevant);
186 }
187
188
189 template <int dim, typename Number>
190 void OfflineData<dim, Number>::setup(const unsigned int problem_dimension,
191 const unsigned int n_precomputed_values)
192 {
193#ifdef DEBUG_OUTPUT
194 std::cout << "OfflineData<dim, Number>::setup()" << std::endl;
195#endif
196
197 /*
198 * Initialize dof handler:
199 */
200
201 const auto &triangulation = discretization_->triangulation();
202 if (!dof_handler_)
203 dof_handler_ = std::make_unique<dealii::DoFHandler<dim>>(triangulation);
204 auto &dof_handler = *dof_handler_;
205
206 dof_handler.distribute_dofs(discretization_->finite_element());
207
208 n_locally_owned_ = dof_handler.locally_owned_dofs().n_elements();
209
210 /*
211 * Renumbering:
212 */
213
214 DoFRenumbering::Cuthill_McKee(dof_handler);
215
216 /*
217 * Reorder all (individual) export indices at the beginning of the
218 * locally_internal index range to achieve a better packing:
219 *
220 * Note: This function might miss export indices that come from
221 * eliminating hanging node and periodicity constraints (which we do
222 * not know at this point because they depend on the renumbering...).
223 */
225 mpi_ensemble_.ensemble_communicator(),
226 n_locally_owned_,
227 1);
228
229 /*
230 * Group degrees of freedom that have the same stencil size in groups
231 * of multiples of the VectorizedArray<Number>::size().
232 *
233 * In order to determine the stencil size we have to create a first,
234 * temporary sparsity pattern:
235 */
236 create_constraints_and_sparsity_pattern();
237 n_locally_internal_ = DoFRenumbering::internal_range(
238 dof_handler, sparsity_pattern_, VectorizedArray<Number>::size());
239
240 /*
241 * Reorder all (strides of) locally internal indices that contain
242 * export indices to the start of the index range. This reordering
243 * preserves the binning introduced by
244 * DoFRenumbering::internal_range().
245 *
246 * Note: This function might miss export indices that come from
247 * eliminating hanging node and periodicity constraints (which we do
248 * not know at this point because they depend on the renumbering...).
249 * We therefore have to update n_export_indices_ later again.
250 */
251 n_export_indices_ = DoFRenumbering::export_indices_first(
252 dof_handler,
253 mpi_ensemble_.ensemble_communicator(),
254 n_locally_internal_,
255 VectorizedArray<Number>::size());
256
257 /*
258 * A small lambda to check for stride-level consistency of the internal
259 * index range:
260 */
261 const auto consistent_stride_range [[maybe_unused]] = [&]() {
262 constexpr auto group_size = VectorizedArray<Number>::size();
263 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
264 const auto offset = n_locally_owned_ != 0 ? *locally_owned.begin() : 0;
265
266 unsigned int group_row_length = 0;
267 unsigned int i = 0;
268 for (; i < n_locally_internal_; ++i) {
269 if (i % group_size == 0) {
270 group_row_length = sparsity_pattern_.row_length(offset + i);
271 } else {
272 if (group_row_length != sparsity_pattern_.row_length(offset + i)) {
273 break;
274 }
275 }
276 }
277 return i / group_size * group_size;
278 };
279
280 /*
281 * A small lambda that performs a "logical or" over all MPI ranks:
282 */
283 const auto mpi_allreduce_logical_or = [&](const bool local_value) {
284 std::function<bool(const bool &, const bool &)> comparator =
285 [](const bool &left, const bool &right) -> bool {
286 return left || right;
287 };
288 return Utilities::MPI::all_reduce(
289 local_value, mpi_ensemble_.ensemble_communicator(), comparator);
290 };
291
292 /*
293 * Create final sparsity pattern:
294 */
295
296 create_constraints_and_sparsity_pattern();
297
298 /*
299 * We have to ensure that the locally internal numbering range is still
300 * consistent, meaning that all strides have the same stencil size.
301 * This property might not hold any more after the elimination
302 * procedure of constrained degrees of freedom (periodicity, or hanging
303 * node constraints). Therefore, the following little dance:
304 */
305
306#if DEAL_II_VERSION_GTE(9, 5, 0)
307 if (mpi_allreduce_logical_or(affine_constraints_.n_constraints() > 0)) {
308 if (mpi_allreduce_logical_or( //
309 consistent_stride_range() != n_locally_internal_)) {
310 /*
311 * In this case we try to fix up the numbering by pushing affected
312 * strides to the end and slightly lowering the n_locally_internal_
313 * marker.
314 */
315 n_locally_internal_ = DoFRenumbering::inconsistent_strides_last(
316 dof_handler,
317 sparsity_pattern_,
318 n_locally_internal_,
319 VectorizedArray<Number>::size());
320 create_constraints_and_sparsity_pattern();
321 n_locally_internal_ = consistent_stride_range();
322 }
323 }
324#endif
325
326 /*
327 * Check that after all the dof manipulation and setup we still end up
328 * with indices in [0, locally_internal) that have uniform stencil size
329 * within a stride.
330 */
331 Assert(consistent_stride_range() == n_locally_internal_,
332 dealii::ExcInternalError());
333
334 /*
335 * Set up partitioner:
336 */
337
338 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
339 Assert(n_locally_owned_ == locally_owned.n_elements(),
340 dealii::ExcInternalError());
341
342 IndexSet locally_relevant;
343 DoFTools::extract_locally_relevant_dofs(dof_handler, locally_relevant);
344 /* Enlarge the locally relevant set to include all additional couplings: */
345 {
346 IndexSet additional_dofs(dof_handler.n_dofs());
347 for (auto &entry : sparsity_pattern_)
348 if (!locally_relevant.is_element(entry.column())) {
349 Assert(locally_owned.is_element(entry.row()), ExcInternalError());
350 additional_dofs.add_index(entry.column());
351 }
352 additional_dofs.compress();
353 locally_relevant.add_indices(additional_dofs);
354 locally_relevant.compress();
355 }
356
357 n_locally_relevant_ = locally_relevant.n_elements();
358
359 scalar_partitioner_ = std::make_shared<dealii::Utilities::MPI::Partitioner>(
360 locally_owned, locally_relevant, mpi_ensemble_.ensemble_communicator());
361
362 hyperbolic_vector_partitioner_ = Vectors::create_vector_partitioner(
363 scalar_partitioner_, problem_dimension);
364
365 precomputed_vector_partitioner_ = Vectors::create_vector_partitioner(
366 scalar_partitioner_, n_precomputed_values);
367
368 /*
369 * After eliminiating periodicity and hanging node constraints we need
370 * to update n_export_indices_ again. This happens because we need to
371 * call export_indices_first() with incomplete information (missing
372 * eliminated degrees of freedom).
373 */
374 if (mpi_allreduce_logical_or(affine_constraints_.n_constraints() > 0)) {
375 /*
376 * Recalculate n_export_indices_:
377 */
378 n_export_indices_ = 0;
379 for (const auto &it : scalar_partitioner_->import_indices())
380 if (it.second <= n_locally_internal_)
381 n_export_indices_ = std::max(n_export_indices_, it.second);
382
383 constexpr auto simd_length = VectorizedArray<Number>::size();
384 n_export_indices_ =
385 (n_export_indices_ + simd_length - 1) / simd_length * simd_length;
386 }
387
388#ifdef DEBUG
389 /* Check that n_export_indices_ is valid: */
390 unsigned int control = 0;
391 for (const auto &it : scalar_partitioner_->import_indices())
392 if (it.second <= n_locally_internal_)
393 control = std::max(control, it.second);
394
395 Assert(control <= n_export_indices_, ExcInternalError());
396 Assert(n_export_indices_ <= n_locally_internal_, ExcInternalError());
397#endif
398
399 /*
400 * Set up SIMD sparsity pattern in local numbering. Nota bene: The
401 * SparsityPatternSIMD::reinit() function will translates the pattern
402 * from global deal.II (typical) dof indexing to local indices.
403 */
404
405 sparsity_pattern_simd_.reinit(
406 n_locally_internal_, sparsity_pattern_, scalar_partitioner_);
407 }
408
409
410 template <int dim, typename Number>
411 void OfflineData<dim, Number>::create_matrices()
412 {
413#ifdef DEBUG_OUTPUT
414 std::cout << "OfflineData<dim, Number>::create_matrices()" << std::endl;
415#endif
416
417 /*
418 * First, (re)initialize all local matrices:
419 */
420
421 mass_matrix_.reinit(sparsity_pattern_simd_);
422 if (discretization_->have_discontinuous_ansatz())
423 mass_matrix_inverse_.reinit(sparsity_pattern_simd_);
424
425 lumped_mass_matrix_.reinit(scalar_partitioner_);
426 lumped_mass_matrix_inverse_.reinit(scalar_partitioner_);
427
428 cij_matrix_.reinit(sparsity_pattern_simd_);
429 if (discretization_->have_discontinuous_ansatz())
430 incidence_matrix_.reinit(sparsity_pattern_simd_);
431
432 /*
433 * Then, assemble:
434 */
435
436 auto &dof_handler = *dof_handler_;
437
438 measure_of_omega_ = 0.;
439
440#ifdef DEAL_II_WITH_TRILINOS
441 /* Variant using TrilinosWrappers::SparseMatrix with global numbering */
442
443 AffineConstraints<double> affine_constraints_assembly;
444 /* This small dance is necessary to translate from Number to double: */
445 affine_constraints_assembly.reinit(affine_constraints_.get_local_lines());
446 for (auto line : affine_constraints_.get_lines()) {
447 affine_constraints_assembly.add_line(line.index);
448 for (auto entry : line.entries)
449 affine_constraints_assembly.add_entry(
450 line.index, entry.first, entry.second);
451 affine_constraints_assembly.set_inhomogeneity(line.index,
452 line.inhomogeneity);
453 }
454 affine_constraints_assembly.close();
455
456 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
457 TrilinosWrappers::SparsityPattern trilinos_sparsity_pattern;
458 trilinos_sparsity_pattern.reinit(locally_owned,
459 sparsity_pattern_,
460 mpi_ensemble_.ensemble_communicator());
461
462 TrilinosWrappers::SparseMatrix mass_matrix_tmp;
463 TrilinosWrappers::SparseMatrix mass_matrix_inverse_tmp;
464 if (discretization_->have_discontinuous_ansatz())
465 mass_matrix_inverse_tmp.reinit(trilinos_sparsity_pattern);
466
467 std::array<TrilinosWrappers::SparseMatrix, dim> cij_matrix_tmp;
468
469 mass_matrix_tmp.reinit(trilinos_sparsity_pattern);
470 for (auto &matrix : cij_matrix_tmp)
471 matrix.reinit(trilinos_sparsity_pattern);
472
473#else
474 /* Variant using deal.II SparseMatrix with local numbering */
475
476 AffineConstraints<Number> affine_constraints_assembly;
477 affine_constraints_assembly.copy_from(affine_constraints_);
478 transform_to_local_range(*scalar_partitioner_, affine_constraints_assembly);
479
480 SparsityPattern sparsity_pattern_assembly;
481 {
482 DynamicSparsityPattern dsp(n_locally_relevant_, n_locally_relevant_);
483 for (const auto &entry : sparsity_pattern_) {
484 const auto i = scalar_partitioner_->global_to_local(entry.row());
485 const auto j = scalar_partitioner_->global_to_local(entry.column());
486 dsp.add(i, j);
487 }
488 sparsity_pattern_assembly.copy_from(dsp);
489 }
490
491 dealii::SparseMatrix<Number> mass_matrix_tmp;
492 dealii::SparseMatrix<Number> mass_matrix_inverse_tmp;
493 if (discretization_->have_discontinuous_ansatz())
494 mass_matrix_inverse_tmp.reinit(sparsity_pattern_assembly);
495
496 std::array<dealii::SparseMatrix<Number>, dim> cij_matrix_tmp;
497
498 mass_matrix_tmp.reinit(sparsity_pattern_assembly);
499 for (auto &matrix : cij_matrix_tmp)
500 matrix.reinit(sparsity_pattern_assembly);
501#endif
502
503 const unsigned int dofs_per_cell =
504 discretization_->finite_element().dofs_per_cell;
505
506 /*
507 * Now, assemble all matrices:
508 */
509
510 /* The local, per-cell assembly routine: */
511 const auto local_assemble_system = [&](const auto &cell,
512 auto &scratch,
513 auto &copy) {
514 /* iterate over locally owned cells and the ghost layer */
515
516 auto &is_locally_owned = copy.is_locally_owned_;
517 auto &local_dof_indices = copy.local_dof_indices_;
518 auto &neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
519
520 auto &cell_mass_matrix = copy.cell_mass_matrix_;
521 auto &cell_mass_matrix_inverse = copy.cell_mass_matrix_inverse_;
522 auto &cell_cij_matrix = copy.cell_cij_matrix_;
523 auto &interface_cij_matrix = copy.interface_cij_matrix_;
524 auto &cell_measure = copy.cell_measure_;
525
526 auto &fe_values = scratch.fe_values_;
527 auto &fe_face_values = scratch.fe_face_values_;
528 auto &fe_neighbor_face_values = scratch.fe_neighbor_face_values_;
529
530#ifdef DEAL_II_WITH_TRILINOS
531 is_locally_owned = cell->is_locally_owned();
532#else
533 /*
534 * When using a local dealii::SparseMatrix<Number> we don not
535 * have a compress(VectorOperation::add) available. In this case
536 * we assemble contributions over all locally relevant (non
537 * artificial) cells.
538 */
539 is_locally_owned = !cell->is_artificial();
540#endif
541 if (!is_locally_owned)
542 return;
543
544 cell_mass_matrix.reinit(dofs_per_cell, dofs_per_cell);
545 for (auto &matrix : cell_cij_matrix)
546 matrix.reinit(dofs_per_cell, dofs_per_cell);
547 if (discretization_->have_discontinuous_ansatz()) {
548 cell_mass_matrix_inverse.reinit(dofs_per_cell, dofs_per_cell);
549 for (auto &it : interface_cij_matrix)
550 for (auto &matrix : it)
551 matrix.reinit(dofs_per_cell, dofs_per_cell);
552 }
553
554 fe_values.reinit(cell);
555
556 local_dof_indices.resize(dofs_per_cell);
557 cell->get_dof_indices(local_dof_indices);
558
559 /* clear out copy data: */
560 cell_mass_matrix = 0.;
561 for (auto &matrix : cell_cij_matrix)
562 matrix = 0.;
563 if (discretization_->have_discontinuous_ansatz()) {
564 cell_mass_matrix_inverse = 0.;
565 for (auto &it : interface_cij_matrix)
566 for (auto &matrix : it)
567 matrix = 0.;
568 }
569 cell_measure = 0.;
570
571 for (unsigned int q : fe_values.quadrature_point_indices()) {
572 const auto JxW = fe_values.JxW(q);
573
574 if (cell->is_locally_owned())
575 cell_measure += Number(JxW);
576
577 for (unsigned int j : fe_values.dof_indices()) {
578 const auto value_JxW = fe_values.shape_value(j, q) * JxW;
579 const auto grad_JxW = fe_values.shape_grad(j, q) * JxW;
580
581 for (unsigned int i : fe_values.dof_indices()) {
582 const auto value = fe_values.shape_value(i, q);
583
584 cell_mass_matrix(i, j) += Number(value * value_JxW);
585 for (unsigned int d = 0; d < dim; ++d)
586 cell_cij_matrix[d](i, j) += Number((value * grad_JxW)[d]);
587 } /* for i */
588 } /* for j */
589 } /* for q */
590
591 /*
592 * For a discontinuous finite element ansatz we need to assemble
593 * additional face contributions:
594 */
595
596 if (!discretization_->have_discontinuous_ansatz())
597 return;
598
599 for (const auto f_index : cell->face_indices()) {
600 const auto &face = cell->face(f_index);
601
602 /* Skip faces without neighbors... */
603 const bool has_neighbor =
604 !face->at_boundary() || cell->has_periodic_neighbor(f_index);
605 if (!has_neighbor) {
606 // set the vector of local dof indices to 0 to indicate that
607 // there is nothing to do for this face:
608 neighbor_local_dof_indices[f_index].resize(0);
609 continue;
610 }
611
612 /* Avoid artificial cells: */
613 const auto neighbor_cell = cell->neighbor_or_periodic_neighbor(f_index);
614 if (neighbor_cell->is_artificial()) {
615 // set the vector of local dof indices to 0 to indicate that
616 // there is nothing to do for this face:
617 neighbor_local_dof_indices[f_index].resize(0);
618 continue;
619 }
620
621 fe_face_values.reinit(cell, f_index);
622
623 /* Face contribution: */
624
625 for (unsigned int q : fe_face_values.quadrature_point_indices()) {
626 const auto JxW = fe_face_values.JxW(q);
627 const auto &normal = fe_face_values.get_normal_vectors()[q];
628
629 for (unsigned int j : fe_face_values.dof_indices()) {
630 const auto value_JxW = fe_face_values.shape_value(j, q) * JxW;
631
632 for (unsigned int i : fe_face_values.dof_indices()) {
633 const auto value = fe_face_values.shape_value(i, q);
634
635 for (unsigned int d = 0; d < dim; ++d)
636 cell_cij_matrix[d](i, j) -=
637 Number(0.5 * normal[d] * value * value_JxW);
638 } /* for i */
639 } /* for j */
640 } /* for q */
641
642 /* Coupling part: */
643
644 const unsigned int f_index_neighbor =
645 cell->has_periodic_neighbor(f_index)
646 ? cell->periodic_neighbor_of_periodic_neighbor(f_index)
647 : cell->neighbor_of_neighbor(f_index);
648
649 neighbor_local_dof_indices[f_index].resize(dofs_per_cell);
650 neighbor_cell->get_dof_indices(neighbor_local_dof_indices[f_index]);
651
652 fe_neighbor_face_values.reinit(neighbor_cell, f_index_neighbor);
653
654 for (unsigned int q : fe_face_values.quadrature_point_indices()) {
655 const auto JxW = fe_face_values.JxW(q);
656 const auto &normal = fe_face_values.get_normal_vectors()[q];
657
658 /* index j for neighbor, index i for current cell: */
659 for (unsigned int j : fe_face_values.dof_indices()) {
660 const auto value_JxW =
661 fe_neighbor_face_values.shape_value(j, q) * JxW;
662
663 for (unsigned int i : fe_face_values.dof_indices()) {
664 const auto value = fe_face_values.shape_value(i, q);
665
666 for (unsigned int d = 0; d < dim; ++d)
667 interface_cij_matrix[f_index][d](i, j) +=
668 Number(0.5 * normal[d] * value * value_JxW);
669 } /* for i */
670 } /* for j */
671 } /* for q */
672 }
673
674 /*
675 * Compute block inverse of mass matrix:
676 */
677
678 if (discretization_->have_discontinuous_ansatz()) {
679 // FIXME: rewrite with CellwiseInverseMassMatrix
680 cell_mass_matrix_inverse.invert(cell_mass_matrix);
681 }
682 };
683
684 const auto copy_local_to_global = [&](const auto &copy) {
685 const auto &is_locally_owned = copy.is_locally_owned_;
686#ifdef DEAL_II_WITH_TRILINOS
687 const auto &local_dof_indices = copy.local_dof_indices_;
688 const auto &neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
689#else
690 /*
691 * We have to transform indices to the local index range
692 * [0, n_locally_relevant_) when using the dealii::SparseMatrix.
693 * Thus, copy all index vectors:
694 */
695 auto local_dof_indices = copy.local_dof_indices_;
696 auto neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
697#endif
698 const auto &cell_mass_matrix = copy.cell_mass_matrix_;
699 const auto &cell_mass_matrix_inverse = copy.cell_mass_matrix_inverse_;
700 const auto &cell_cij_matrix = copy.cell_cij_matrix_;
701 const auto &interface_cij_matrix = copy.interface_cij_matrix_;
702 const auto &cell_measure = copy.cell_measure_;
703
704 if (!is_locally_owned)
705 return;
706
707#ifndef DEAL_II_WITH_TRILINOS
708 transform_to_local_range(*scalar_partitioner_, local_dof_indices);
709 for (auto &indices : neighbor_local_dof_indices)
710 transform_to_local_range(*scalar_partitioner_, indices);
711#endif
712
713 affine_constraints_assembly.distribute_local_to_global(
714 cell_mass_matrix, local_dof_indices, mass_matrix_tmp);
715
716 for (int k = 0; k < dim; ++k) {
717 affine_constraints_assembly.distribute_local_to_global(
718 cell_cij_matrix[k], local_dof_indices, cij_matrix_tmp[k]);
719
720 for (unsigned int f_index = 0; f_index < copy.n_faces; ++f_index) {
721 if (neighbor_local_dof_indices[f_index].size() != 0) {
722 affine_constraints_assembly.distribute_local_to_global(
723 interface_cij_matrix[f_index][k],
724 local_dof_indices,
725 neighbor_local_dof_indices[f_index],
726 cij_matrix_tmp[k]);
727 }
728 }
729 }
730
731 if (discretization_->have_discontinuous_ansatz())
732 affine_constraints_assembly.distribute_local_to_global(
733 cell_mass_matrix_inverse,
734 local_dof_indices,
735 mass_matrix_inverse_tmp);
736
737 measure_of_omega_ += cell_measure;
738 };
739
740 WorkStream::run(dof_handler.begin_active(),
741 dof_handler.end(),
742 local_assemble_system,
743 copy_local_to_global,
744 AssemblyScratchData<dim>(*discretization_),
745#ifdef DEAL_II_WITH_TRILINOS
746 AssemblyCopyData<dim, double>());
747#else
748 AssemblyCopyData<dim, Number>());
749#endif
750
751#ifdef DEAL_II_WITH_TRILINOS
752 mass_matrix_tmp.compress(VectorOperation::add);
753 for (auto &it : cij_matrix_tmp)
754 it.compress(VectorOperation::add);
755
756 mass_matrix_.read_in(mass_matrix_tmp, /*locally_indexed*/ false);
757 if (discretization_->have_discontinuous_ansatz())
758 mass_matrix_inverse_.read_in(mass_matrix_inverse_tmp, /*loc_ind*/ false);
759 cij_matrix_.read_in(cij_matrix_tmp, /*locally_indexed*/ false);
760#else
761 mass_matrix_.read_in(mass_matrix_tmp, /*locally_indexed*/ true);
762 if (discretization_->have_discontinuous_ansatz())
763 mass_matrix_inverse_.read_in(mass_matrix_inverse_tmp, /*loc_ind*/ true);
764 cij_matrix_.read_in(cij_matrix_tmp, /*locally_indexed*/ true);
765#endif
766
767 mass_matrix_.update_ghost_rows();
768 if (discretization_->have_discontinuous_ansatz())
769 mass_matrix_inverse_.update_ghost_rows();
770 cij_matrix_.update_ghost_rows();
771
772 measure_of_omega_ = Utilities::MPI::sum(
773 measure_of_omega_, mpi_ensemble_.ensemble_communicator());
774
775 /*
776 * Create lumped mass matrix:
777 */
778
779 {
780#ifdef DEAL_II_WITH_TRILINOS
781 ScalarVector one(scalar_partitioner_);
782 one = 1.;
783 affine_constraints_assembly.set_zero(one);
784
785 ScalarVector local_lumped_mass_matrix(scalar_partitioner_);
786 mass_matrix_tmp.vmult(local_lumped_mass_matrix, one);
787 local_lumped_mass_matrix.compress(VectorOperation::add);
788
789 for (unsigned int i = 0; i < scalar_partitioner_->locally_owned_size();
790 ++i) {
791 lumped_mass_matrix_.local_element(i) =
792 local_lumped_mass_matrix.local_element(i);
793 lumped_mass_matrix_inverse_.local_element(i) =
794 1. / lumped_mass_matrix_.local_element(i);
795 }
796 lumped_mass_matrix_.update_ghost_values();
797 lumped_mass_matrix_inverse_.update_ghost_values();
798
799#else
800
801 dealii::Vector<Number> one(mass_matrix_tmp.m());
802 one = 1.;
803 affine_constraints_assembly.set_zero(one);
804
805 dealii::Vector<Number> local_lumped_mass_matrix(mass_matrix_tmp.m());
806 mass_matrix_tmp.vmult(local_lumped_mass_matrix, one);
807
808 for (unsigned int i = 0; i < scalar_partitioner_->locally_owned_size();
809 ++i) {
810 lumped_mass_matrix_.local_element(i) = local_lumped_mass_matrix(i);
811 lumped_mass_matrix_inverse_.local_element(i) =
812 1. / lumped_mass_matrix_.local_element(i);
813 }
814 lumped_mass_matrix_.update_ghost_values();
815 lumped_mass_matrix_inverse_.update_ghost_values();
816#endif
817 }
818
819 /*
820 * Assemble incidence matrix:
821 */
822
823 if (discretization_->have_discontinuous_ansatz()) {
824#ifdef DEAL_II_WITH_TRILINOS
825 TrilinosWrappers::SparseMatrix incidence_matrix_tmp;
826 incidence_matrix_tmp.reinit(trilinos_sparsity_pattern);
827#else
828 dealii::SparseMatrix<Number> incidence_matrix_tmp;
829 incidence_matrix_tmp.reinit(sparsity_pattern_assembly);
830#endif
831
832 /* The local, per-cell assembly routine: */
833 const auto local_assemble_system = [&](const auto &cell,
834 auto &scratch,
835 auto &copy) {
836 /* iterate over locally owned cells and the ghost layer */
837
838 auto &is_locally_owned = copy.is_locally_owned_;
839 auto &local_dof_indices = copy.local_dof_indices_;
840 auto &neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
841 auto &interface_incidence_matrix = copy.interface_incidence_matrix_;
842 auto &fe_face_values_nodal = scratch.fe_face_values_nodal_;
843 auto &fe_neighbor_face_values_nodal =
844 scratch.fe_neighbor_face_values_nodal_;
845
846#ifdef DEAL_II_WITH_TRILINOS
847 is_locally_owned = cell->is_locally_owned();
848#else
849 is_locally_owned = !cell->is_artificial();
850#endif
851 if (!is_locally_owned)
852 return;
853
854 for (auto &matrix : interface_incidence_matrix)
855 matrix.reinit(dofs_per_cell, dofs_per_cell);
856
857 local_dof_indices.resize(dofs_per_cell);
858 cell->get_dof_indices(local_dof_indices);
859
860 /* clear out copy data: */
861 for (auto &matrix : interface_incidence_matrix)
862 matrix = 0.;
863
864 for (const auto f_index : cell->face_indices()) {
865 const auto &face = cell->face(f_index);
866
867 /* Skip faces without neighbors... */
868 const bool has_neighbor =
869 !face->at_boundary() || cell->has_periodic_neighbor(f_index);
870 if (!has_neighbor) {
871 // set the vector of local dof indices to 0 to indicate that
872 // there is nothing to do for this face:
873 neighbor_local_dof_indices[f_index].resize(0);
874 continue;
875 }
876
877 /* Avoid artificial cells: */
878 const auto neighbor_cell =
879 cell->neighbor_or_periodic_neighbor(f_index);
880 if (neighbor_cell->is_artificial()) {
881 // set the vector of local dof indices to 0 to indicate that
882 // there is nothing to do for this face:
883 neighbor_local_dof_indices[f_index].resize(0);
884 continue;
885 }
886
887 const unsigned int f_index_neighbor =
888 cell->has_periodic_neighbor(f_index)
889 ? cell->periodic_neighbor_of_periodic_neighbor(f_index)
890 : cell->neighbor_of_neighbor(f_index);
891
892 neighbor_local_dof_indices[f_index].resize(dofs_per_cell);
893 neighbor_cell->get_dof_indices(neighbor_local_dof_indices[f_index]);
894
895 fe_face_values_nodal.reinit(cell, f_index);
896 fe_neighbor_face_values_nodal.reinit(neighbor_cell, f_index_neighbor);
897
898 /* Lumped incidence matrix: */
899
900 for (unsigned int q :
901 fe_face_values_nodal.quadrature_point_indices()) {
902 /* index j for neighbor, index i for current cell: */
903 for (unsigned int j : fe_neighbor_face_values_nodal.dof_indices()) {
904 const auto v_j = fe_neighbor_face_values_nodal.shape_value(j, q);
905 for (unsigned int i : fe_face_values_nodal.dof_indices()) {
906 const auto v_i = fe_face_values_nodal.shape_value(i, q);
907 constexpr auto eps = std::numeric_limits<Number>::epsilon();
908 if (std::abs(v_i * v_j) > 100. * eps) {
909 const auto &ansatz = discretization_->ansatz();
910
911 const auto glob_i = local_dof_indices[i];
912 const auto glob_j = neighbor_local_dof_indices[f_index][j];
913 const auto m_i = lumped_mass_matrix_[glob_i];
914 const auto m_j = lumped_mass_matrix_[glob_j];
915 const auto hd_ij =
916 Number(0.5) * (m_i + m_j) / measure_of_omega_;
917
918 Number r_ij = 1.0;
919
920 if (ansatz == Ansatz::dg_q2) {
921 /*
922 * For even polynomial degree we normalize the incidence
923 * matrix to (0.5 (m_i + m_j) / |Omega|) ^ (1.5 / d).
924 * Note, that we will visit every coupling
925 * pair of degrees of freedom (i, j) precisely once.
926 */
927 r_ij = std::pow(hd_ij, incidence_relaxation_even_ / dim);
928 } else {
929 /*
930 * For odd polynomial degree we normalize the incidence
931 * matrix to 1. Note, that we will visit every coupling
932 * pair of degrees of freedom (i, j) precisely once.
933 */
934 r_ij = std::pow(hd_ij, incidence_relaxation_odd_ / dim);
935 }
936
937 interface_incidence_matrix[f_index](i, j) += r_ij;
938 }
939 } /* for i */
940 } /* for j */
941 } /* for q */
942 }
943 };
944
945 const auto copy_local_to_global = [&](const auto &copy) {
946 const auto &is_locally_owned = copy.is_locally_owned_;
947#ifdef DEAL_II_WITH_TRILINOS
948 const auto &local_dof_indices = copy.local_dof_indices_;
949 const auto &neighbor_local_dof_indices =
950 copy.neighbor_local_dof_indices_;
951#else
952 /*
953 * We have to transform indices to the local index range
954 * [0, n_locally_relevant_) when using the dealii::SparseMatrix.
955 * Thus, copy all index vectors:
956 */
957 auto local_dof_indices = copy.local_dof_indices_;
958 auto neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
959#endif
960 const auto &interface_incidence_matrix =
961 copy.interface_incidence_matrix_;
962
963 if (!is_locally_owned)
964 return;
965
966#ifndef DEAL_II_WITH_TRILINOS
967 transform_to_local_range(*scalar_partitioner_, local_dof_indices);
968 for (auto &indices : neighbor_local_dof_indices)
969 transform_to_local_range(*scalar_partitioner_, indices);
970#endif
971
972 for (unsigned int f_index = 0; f_index < copy.n_faces; ++f_index) {
973 if (neighbor_local_dof_indices[f_index].size() != 0) {
974 affine_constraints_assembly.distribute_local_to_global(
975 interface_incidence_matrix[f_index],
976 local_dof_indices,
977 neighbor_local_dof_indices[f_index],
978 incidence_matrix_tmp);
979 }
980 }
981 };
982
983 WorkStream::run(dof_handler.begin_active(),
984 dof_handler.end(),
985 local_assemble_system,
986 copy_local_to_global,
987 AssemblyScratchData<dim>(*discretization_),
988#ifdef DEAL_II_WITH_TRILINOS
989 AssemblyCopyData<dim, double>());
990#else
991 AssemblyCopyData<dim, Number>());
992#endif
993
994#ifdef DEAL_II_WITH_TRILINOS
995 incidence_matrix_.read_in(incidence_matrix_tmp, /*locally_indexe*/ false);
996#else
997 incidence_matrix_.read_in(incidence_matrix_tmp, /*locally_indexed*/ true);
998#endif
999 incidence_matrix_.update_ghost_rows();
1000 }
1001
1002 /*
1003 * Populate boundary map and collect coupling boundary pairs:
1004 */
1005 {
1006 boundary_map_ = construct_boundary_map(
1007 dof_handler.begin_active(), dof_handler.end(), *scalar_partitioner_);
1008
1009 coupling_boundary_pairs_ = collect_coupling_boundary_pairs(
1010 dof_handler.begin_active(), dof_handler.end(), *scalar_partitioner_);
1011 }
1012
1013#ifdef DEBUG_SYMMETRY_CHECK
1014 /*
1015 * Verify that we have consistent mass:
1016 */
1017
1018 double total_mass = 0.;
1019 for (unsigned int i = 0; i < n_locally_owned_; ++i)
1020 total_mass += lumped_mass_matrix_.local_element(i);
1021 total_mass =
1022 Utilities::MPI::sum(total_mass, mpi_ensemble_.ensemble_communicator());
1023
1024 Assert(std::abs(measure_of_omega_ - total_mass) <
1025 1.e-12 * measure_of_omega_,
1026 dealii::ExcMessage(
1027 "Total mass differs from the measure of the domain."));
1028
1029 /*
1030 * Verify that the mij_matrix_ object is consistent:
1031 */
1032
1033 for (unsigned int i = 0; i < n_locally_owned_; ++i) {
1034 /* Skip constrained degrees of freedom: */
1035 const unsigned int row_length = sparsity_pattern_simd_.row_length(i);
1036 if (row_length == 1)
1037 continue;
1038
1039 auto sum =
1040 mass_matrix_.get_entry(i, 0) - lumped_mass_matrix_.local_element(i);
1041
1042 /* skip diagonal */
1043 constexpr auto simd_length = VectorizedArray<Number>::size();
1044 const unsigned int *js = sparsity_pattern_simd_.columns(i);
1045 for (unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1046 const auto j = *(i < n_locally_internal_ ? js + col_idx * simd_length
1047 : js + col_idx);
1048 Assert(j < n_locally_relevant_, dealii::ExcInternalError());
1049
1050 const auto m_ij = mass_matrix_.get_entry(i, col_idx);
1051 if (discretization_->have_discontinuous_ansatz()) {
1052 // Interfacial coupling terms are present in the stencil but zero
1053 // in the mass matrix
1054 Assert(std::abs(m_ij) > -1.e-12, dealii::ExcInternalError());
1055 } else {
1056 Assert(std::abs(m_ij) > 1.e-12, dealii::ExcInternalError());
1057 }
1058 sum += m_ij;
1059
1060 const auto m_ji = mass_matrix_.get_transposed_entry(i, col_idx);
1061 if (std::abs(m_ij - m_ji) >= 1.e-12) {
1062 // The m_ij matrix is not symmetric
1063 std::stringstream ss;
1064 ss << "m_ij matrix is not symmetric: " << m_ij << " <-> " << m_ji;
1065 Assert(false, dealii::ExcMessage(ss.str()));
1066 }
1067 }
1068
1069 Assert(std::abs(sum) < 1.e-12, dealii::ExcInternalError());
1070 }
1071
1072 /*
1073 * Verify that the cij_matrix_ object is consistent:
1074 */
1075
1076 for (unsigned int i = 0; i < n_locally_owned_; ++i) {
1077 /* Skip constrained degrees of freedom: */
1078 const unsigned int row_length = sparsity_pattern_simd_.row_length(i);
1079 if (row_length == 1)
1080 continue;
1081
1082 auto sum = cij_matrix_.get_tensor(i, 0);
1083
1084 /* skip diagonal */
1085 constexpr auto simd_length = VectorizedArray<Number>::size();
1086 const unsigned int *js = sparsity_pattern_simd_.columns(i);
1087 for (unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1088 const auto j = *(i < n_locally_internal_ ? js + col_idx * simd_length
1089 : js + col_idx);
1090 Assert(j < n_locally_relevant_, dealii::ExcInternalError());
1091
1092 const auto c_ij = cij_matrix_.get_tensor(i, col_idx);
1093 Assert(c_ij.norm() > 1.e-12, dealii::ExcInternalError());
1094 sum += c_ij;
1095
1096 const auto c_ji = cij_matrix_.get_transposed_tensor(i, col_idx);
1097 if ((c_ij + c_ji).norm() >= 1.e-12) {
1098 // The c_ij matrix is not symmetric, this can only happen if i
1099 // and j are both located on the boundary.
1100
1101 CouplingDescription coupling{i, col_idx, j};
1102 const auto it = std::find(coupling_boundary_pairs_.begin(),
1103 coupling_boundary_pairs_.end(),
1104 coupling);
1105 if (it == coupling_boundary_pairs_.end()) {
1106 std::stringstream ss;
1107 ss << "c_ij matrix is not anti-symmetric: " << c_ij << " <-> "
1108 << c_ji;
1109 Assert(false, dealii::ExcMessage(ss.str()));
1110 }
1111 }
1112 }
1113
1114 Assert(sum.norm() < 1.e-12, dealii::ExcInternalError());
1115 }
1116#endif
1117 }
1118
1119
1120 template <int dim, typename Number>
1121 void OfflineData<dim, Number>::create_multigrid_data()
1122 {
1123#ifdef DEBUG_OUTPUT
1124 std::cout << "OfflineData<dim, Number>::create_multigrid_data()"
1125 << std::endl;
1126#endif
1127
1128 auto &dof_handler = *dof_handler_;
1129
1130 dof_handler.distribute_mg_dofs();
1131
1132 const auto n_levels = dof_handler.get_triangulation().n_global_levels();
1133
1134 AffineConstraints<float> level_constraints;
1135 // TODO not yet thread-parallel and without periodicity
1136
1137 level_boundary_map_.resize(n_levels);
1138 level_lumped_mass_matrix_.resize(n_levels);
1139
1140 for (unsigned int level = 0; level < n_levels; ++level) {
1141 /* Assemble lumped mass matrix vector: */
1142
1143 IndexSet relevant_dofs;
1144 dealii::DoFTools::extract_locally_relevant_level_dofs(
1145 dof_handler, level, relevant_dofs);
1146 const auto partitioner = std::make_shared<Utilities::MPI::Partitioner>(
1147 dof_handler.locally_owned_mg_dofs(level),
1148 relevant_dofs,
1149 mpi_ensemble_.ensemble_communicator());
1150 level_lumped_mass_matrix_[level].reinit(partitioner);
1151 std::vector<types::global_dof_index> dof_indices(
1152 dof_handler.get_fe().dofs_per_cell);
1153 dealii::Vector<Number> mass_values(dof_handler.get_fe().dofs_per_cell);
1154 FEValues<dim> fe_values(discretization_->mapping(),
1155 discretization_->finite_element(),
1156 discretization_->quadrature(),
1157 update_values | update_JxW_values);
1158 for (const auto &cell : dof_handler.cell_iterators_on_level(level))
1159 // TODO for assembly with dealii::SparseMatrix and local
1160 // numbering this probably has to read !cell->is_artificial()
1161 if (cell->is_locally_owned_on_level()) {
1162 fe_values.reinit(cell);
1163 for (unsigned int i = 0; i < mass_values.size(); ++i) {
1164 double sum = 0;
1165 for (unsigned int q = 0; q < fe_values.n_quadrature_points; ++q)
1166 sum += fe_values.shape_value(i, q) * fe_values.JxW(q);
1167 mass_values(i) = sum;
1168 }
1169 cell->get_mg_dof_indices(dof_indices);
1170 level_constraints.distribute_local_to_global(
1171 mass_values, dof_indices, level_lumped_mass_matrix_[level]);
1172 }
1173 level_lumped_mass_matrix_[level].compress(VectorOperation::add);
1174
1175 /* Populate boundary map: */
1176
1177 level_boundary_map_[level] = construct_boundary_map(
1178 dof_handler.begin_mg(level), dof_handler.end_mg(level), *partitioner);
1179 }
1180 }
1181
1182
1183 template <int dim, typename Number>
1184 template <typename ITERATOR1, typename ITERATOR2>
1186 const ITERATOR1 &begin,
1187 const ITERATOR2 &end,
1188 const Utilities::MPI::Partitioner &partitioner) const -> BoundaryMap
1189 {
1190#ifdef DEBUG_OUTPUT
1191 std::cout << "OfflineData<dim, Number>::construct_boundary_map()"
1192 << std::endl;
1193#endif
1194
1195 /*
1196 * Create a temporary multimap with the (local) dof index as key:
1197 */
1198
1199 using BoundaryData = std::tuple<dealii::Tensor<1, dim, Number> /*normal*/,
1200 Number /*normal mass*/,
1201 Number /*boundary mass*/,
1202 dealii::types::boundary_id /*id*/,
1203 dealii::Point<dim>> /*position*/;
1204 std::multimap<unsigned int, BoundaryData> preliminary_map;
1205
1206 std::vector<dealii::types::global_dof_index> local_dof_indices;
1207
1208 const dealii::QGauss<dim - 1> face_quadrature(3);
1209 dealii::FEFaceValues<dim> fe_face_values(discretization_->mapping(),
1210 discretization_->finite_element(),
1211 face_quadrature,
1212 dealii::update_normal_vectors |
1213 dealii::update_values |
1214 dealii::update_JxW_values);
1215
1216 const unsigned int dofs_per_cell =
1217 discretization_->finite_element().dofs_per_cell;
1218
1219 const auto support_points =
1220 discretization_->finite_element().get_unit_support_points();
1221
1222 for (auto cell = begin; cell != end; ++cell) {
1223
1224 /*
1225 * Workaround: Make sure to iterate over the entire locally relevant
1226 * set so that we compute normals between cells with differing owners
1227 * correctly. This strategy works for 2D but will fail in 3D with
1228 * locally refined meshes and hanging nodes situated at the boundary.
1229 */
1230 if ((cell->is_active() && cell->is_artificial()) ||
1231 (!cell->is_active() && cell->is_artificial_on_level()))
1232 continue;
1233
1234 local_dof_indices.resize(dofs_per_cell);
1235 cell->get_active_or_mg_dof_indices(local_dof_indices);
1236
1237 for (auto f : cell->face_indices()) {
1238 const auto face = cell->face(f);
1239 const auto id = face->boundary_id();
1240
1241 if (!face->at_boundary())
1242 continue;
1243
1244 /*
1245 * Skip periodic boundary faces. For our algorithm these are
1246 * interior degrees of freedom (if not simultaneously located at
1247 * another boundary as well).
1248 */
1249 if (id == Boundary::periodic)
1250 continue;
1251
1252 fe_face_values.reinit(cell, f);
1253
1254 for (unsigned int j : fe_face_values.dof_indices()) {
1255 if (!discretization_->finite_element().has_support_on_face(j, f))
1256 continue;
1257
1258 Number boundary_mass = 0.;
1259 dealii::Tensor<1, dim, Number> normal;
1260
1261 for (unsigned int q : fe_face_values.quadrature_point_indices()) {
1262 const auto JxW = fe_face_values.JxW(q);
1263 const auto phi_i = fe_face_values.shape_value(j, q);
1264
1265 boundary_mass += phi_i * JxW;
1266 normal += phi_i * fe_face_values.normal_vector(q) * JxW;
1267 }
1268
1269 const auto global_index = local_dof_indices[j];
1270 const auto index = partitioner.global_to_local(global_index);
1271
1272 /* Skip nonlocal degrees of freedom: */
1273 if (index >= n_locally_owned_)
1274 continue;
1275
1276 /* Skip constrained degrees of freedom: */
1277 const unsigned int row_length =
1278 sparsity_pattern_simd_.row_length(index);
1279 if (row_length == 1)
1280 continue;
1281
1282 Point<dim> position =
1283 discretization_->mapping().transform_unit_to_real_cell(
1284 cell, support_points[j]);
1285
1286 /*
1287 * Temporarily insert a (wrong) boundary mass value for the
1288 * normal mass. We'll fix this later.
1289 */
1290 preliminary_map.insert(
1291 {index, {normal, boundary_mass, boundary_mass, id, position}});
1292 } /* j */
1293 } /* f */
1294 } /* cell */
1295
1296 /*
1297 * Filter boundary map:
1298 *
1299 * At this point we have collected multiple cell contributions for each
1300 * boundary degree of freedom. We now merge all entries that have the
1301 * same boundary id and whose normals describe an acute angle of about
1302 * 60 degrees or less.
1303 *
1304 * FIXME: is this robust in 3D?
1305 */
1306
1307 std::multimap<unsigned int, BoundaryData> filtered_map;
1308 std::set<dealii::types::global_dof_index> boundary_dofs;
1309 for (auto entry : preliminary_map) {
1310 bool inserted = false;
1311 const auto range = filtered_map.equal_range(entry.first);
1312 for (auto it = range.first; it != range.second; ++it) {
1313 auto &[new_normal,
1314 new_normal_mass,
1315 new_boundary_mass,
1316 new_id,
1317 new_point] = entry.second;
1318 auto &[normal, normal_mass, boundary_mass, id, point] = it->second;
1319
1320 if (id != new_id)
1321 continue;
1322
1323 Assert(point.distance(new_point) < 1.0e-14, dealii::ExcInternalError());
1324
1325 if (normal * new_normal / normal.norm() / new_normal.norm() > 0.50) {
1326 /*
1327 * Both normals describe an acute angle of 85 degrees or less.
1328 * Merge the entries and continue.
1329 */
1330 normal += new_normal;
1331 boundary_mass += new_boundary_mass;
1332 inserted = true;
1333 continue;
1334
1335 } else if constexpr (dim == 2) {
1336 /*
1337 * Workaround for 2D: When enforcing slip boundary conditions
1338 * with two noncollinear vectors the resulting momentum must be
1339 * 0. But the normals don't necessarily describe an orthonormal
1340 * basis and we cannot use orthogonal projection. Therefore,
1341 * simply set the boundary type to no slip:
1342 */
1343 if (new_id == Boundary::slip) {
1344 Assert(id == Boundary::slip, dealii::ExcInternalError());
1345 new_id = Boundary::no_slip;
1346 id = Boundary::no_slip;
1347 }
1348 }
1349 }
1350 if (!inserted)
1351 filtered_map.insert(entry);
1352 }
1353
1354 /*
1355 * Normalize all normal vectors and create final boundary_map:
1356 */
1357
1358 BoundaryMap boundary_map;
1359 std::transform(
1360 std::begin(filtered_map),
1361 std::end(filtered_map),
1362 std::back_inserter(boundary_map),
1363 [&](const auto &it) -> BoundaryDescription { //
1364 auto index = it.first;
1365 const auto &[normal, normal_mass, boundary_mass, id, point] =
1366 it.second;
1367
1368 const auto new_normal_mass =
1369 normal.norm() + std::numeric_limits<Number>::epsilon();
1370 const auto new_normal = normal / new_normal_mass;
1371
1372 return {index, new_normal, new_normal_mass, boundary_mass, id, point};
1373 });
1374
1375 return boundary_map;
1376 }
1377
1378
1379 template <int dim, typename Number>
1380 template <typename ITERATOR1, typename ITERATOR2>
1382 const ITERATOR1 &begin,
1383 const ITERATOR2 &end,
1384 const Utilities::MPI::Partitioner &partitioner) const
1385 -> CouplingBoundaryPairs
1386 {
1387#ifdef DEBUG_OUTPUT
1388 std::cout << "OfflineData<dim, Number>::collect_coupling_boundary_pairs()"
1389 << std::endl;
1390#endif
1391
1392 /*
1393 * First, collect *all* locally relevant degrees of freedom that are
1394 * located on a (non periodic) boundary. We also collect constrained
1395 * degrees of freedom for the time being (and filter afterwards).
1396 */
1397
1398 std::set<unsigned int> locally_relevant_boundary_indices;
1399
1400 std::vector<dealii::types::global_dof_index> local_dof_indices;
1401
1402 const auto &finite_element = discretization_->finite_element();
1403 const unsigned int dofs_per_cell = finite_element.dofs_per_cell;
1404 local_dof_indices.resize(dofs_per_cell);
1405
1406 for (auto cell = begin; cell != end; ++cell) {
1407
1408 /* Make sure to iterate over the entire locally relevant set: */
1409 if (cell->is_artificial())
1410 continue;
1411
1412 cell->get_active_or_mg_dof_indices(local_dof_indices);
1413
1414 for (auto f : cell->face_indices()) {
1415 const auto face = cell->face(f);
1416 const auto id = face->boundary_id();
1417
1418 if (!face->at_boundary())
1419 continue;
1420
1421 /* Skip periodic boundary faces; see above. */
1422 if (id == Boundary::periodic)
1423 continue;
1424
1425 for (unsigned int j = 0; j < dofs_per_cell; ++j) {
1426
1427 if (!finite_element.has_support_on_face(j, f))
1428 continue;
1429
1430 const auto global_index = local_dof_indices[j];
1431 const auto index = partitioner.global_to_local(global_index);
1432
1433 /* Skip irrelevant degrees of freedom: */
1434 if (index >= n_locally_relevant_)
1435 continue;
1436
1437 locally_relevant_boundary_indices.insert(index);
1438 } /* j */
1439 } /* f */
1440 } /* cell */
1441
1442 /*
1443 * Now, collect all coupling boundary pairs:
1444 */
1445
1446 CouplingBoundaryPairs result;
1447
1448 for (const auto i : locally_relevant_boundary_indices) {
1449
1450 /* Only record pairs with a left index that is locally owned: */
1451 if (i >= n_locally_owned_)
1452 continue;
1453
1454 const unsigned int row_length = sparsity_pattern_simd_.row_length(i);
1455
1456 /* Skip all constrained degrees of freedom: */
1457 if (row_length == 1)
1458 continue;
1459
1460 const unsigned int *js = sparsity_pattern_simd_.columns(i);
1461 constexpr auto simd_length = VectorizedArray<Number>::size();
1462 /* skip diagonal: */
1463 for (unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1464 const auto j = *(i < n_locally_internal_ ? js + col_idx * simd_length
1465 : js + col_idx);
1466
1467 if (locally_relevant_boundary_indices.count(j) != 0) {
1468 result.push_back({i, col_idx, j});
1469 }
1470 }
1471 }
1472
1473 return result;
1474 }
1475
1476} /* namespace ryujin */
std::tuple< unsigned int, dealii::Tensor< 1, dim, Number >, Number, Number, dealii::types::boundary_id, dealii::Point< dim > > BoundaryDescription
Definition: offline_data.h:76
OfflineData(const MPIEnsemble &mpi_ensemble, const Discretization< dim > &discretization, const std::string &subsection="/OfflineData")
void make_extended_sparsity_pattern(const dealii::DoFHandler< dim > &dof_handler, SPARSITY &dsp, const dealii::AffineConstraints< Number > &affine_constraints, bool keep_constrained)
void transform_to_local_range(const dealii::Utilities::MPI::Partitioner &partitioner, dealii::AffineConstraints< Number > &affine_constraints)
unsigned int internal_range(dealii::DoFHandler< dim > &dof_handler, const dealii::DynamicSparsityPattern &sparsity, const std::size_t group_size)
unsigned int export_indices_first(dealii::DoFHandler< dim > &dof_handler, const MPI_Comm &mpi_communicator, const unsigned int n_locally_internal, const std::size_t group_size)
unsigned int inconsistent_strides_last(dealii::DoFHandler< dim > &dof_handler, const dealii::DynamicSparsityPattern &sparsity, const unsigned int n_locally_internal, const std::size_t group_size)
void make_extended_sparsity_pattern_dg(const dealii::DoFHandler< dim > &dof_handler, SPARSITY &dsp, const dealii::AffineConstraints< Number > &affine_constraints, bool keep_constrained)
std::shared_ptr< const dealii::Utilities::MPI::Partitioner > create_vector_partitioner(const std::shared_ptr< const dealii::Utilities::MPI::Partitioner > &scalar_partitioner, const unsigned int n_components)
T pow(const T x, const T b)
dealii::LinearAlgebra::distributed::Vector< Number > ScalarVector
Definition: state_vector.h:31
DEAL_II_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)