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_nothing.h>
21#include <deal.II/fe/fe_values.h>
22#include <deal.II/grid/grid_tools.h>
23#include <deal.II/lac/dynamic_sparsity_pattern.h>
24#include <deal.II/lac/la_parallel_vector.h>
25#ifdef DEAL_II_WITH_TRILINOS
26#include <deal.II/lac/trilinos_sparse_matrix.h>
29#ifdef FORCE_DEAL_II_SPARSE_MATRIX
30#undef DEAL_II_WITH_TRILINOS
35 using namespace dealii;
38 template <
int dim,
typename Number>
42 const std::string &subsection )
43 : ParameterAcceptor(subsection)
44 , mpi_ensemble_(mpi_ensemble)
45 , discretization_(&discretization)
47 treat_fe_nothing_as_boundary_ =
true;
49 "treat fe_nothing as boundary",
50 treat_fe_nothing_as_boundary_,
51 "If set to true, we treat cell with where the active finite element is "
52 "set to FE_Nothing as boundary: We do not assemble an interior jump "
53 "over such elements and add vertices touching such an element in the "
54 "boundary map. In this case, the boundary id of the interior face is "
55 "set via the material id of the cell with active FE_Nothing element.");
57 incidence_relaxation_even_ = 0.5;
58 add_parameter(
"incidence matrix relaxation even degree",
59 incidence_relaxation_even_,
60 "Scaling exponent for incidence matrix used for "
61 "discontinuous finite elements with even degree. The default "
62 "value 0.5 scales the jump penalization with (h_i+h_j)^0.5.");
64 incidence_relaxation_odd_ = 0.0;
65 add_parameter(
"incidence matrix relaxation odd degree",
66 incidence_relaxation_odd_,
67 "Scaling exponent for incidence matrix used for "
68 "discontinuous finite elements with even degree. The default "
69 "value of 0.0 sets the jump penalization to a constant 1.");
73 template <
int dim,
typename Number>
82 auto &dof_handler = *dof_handler_;
83 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
85#if DEAL_II_VERSION_GTE(9, 6, 0)
86 const auto locally_relevant =
87 DoFTools::extract_locally_relevant_dofs(dof_handler);
89 IndexSet locally_relevant;
90 DoFTools::extract_locally_relevant_dofs(dof_handler, locally_relevant);
93#if DEAL_II_VERSION_GTE(9, 6, 0)
94 affine_constraints_.reinit(locally_owned, locally_relevant);
96 affine_constraints_.reinit(locally_relevant);
98 DoFTools::make_hanging_node_constraints(dof_handler, affine_constraints_);
100#ifndef DEAL_II_WITH_TRILINOS
101 AssertThrow(affine_constraints_.n_constraints() == 0,
102 ExcMessage(
"ryujin was built without Trilinos support - no "
103 "hanging node support available"));
111 const auto &periodic_faces =
112 discretization_->triangulation().get_periodic_face_map();
114 for (
const auto &[left, value] : periodic_faces) {
115 const auto &[right, orientation] = value;
117 typename DoFHandler<dim>::cell_iterator dof_cell_left(
118 &left.first->get_triangulation(),
123 typename DoFHandler<dim>::cell_iterator dof_cell_right(
124 &right.first->get_triangulation(),
125 right.first->level(),
126 right.first->index(),
129 if constexpr (std::is_same_v<Number, double>) {
130 DoFTools::make_periodicity_constraints(
131 dof_cell_left->face(left.second),
132 dof_cell_right->face(right.second),
135#
if DEAL_II_VERSION_GTE(9, 6, 0)
143 AssertThrow(
false, dealii::ExcNotImplemented());
148 affine_constraints_.close();
153 const std::vector<IndexSet> &locally_owned_dofs =
154 Utilities::MPI::all_gather(mpi_ensemble_.ensemble_communicator(),
155 dof_handler.locally_owned_dofs());
156 const IndexSet locally_active =
157 dealii::DoFTools::extract_locally_active_dofs(dof_handler);
158 Assert(affine_constraints_.is_consistent_in_parallel(
161 mpi_ensemble_.ensemble_communicator(),
167 sparsity_pattern_.reinit(
168 dof_handler.n_dofs(), dof_handler.n_dofs(), locally_relevant);
170 if (discretization_->have_discontinuous_ansatz()) {
175 dof_handler, sparsity_pattern_, affine_constraints_,
false);
180#ifdef DEAL_II_WITH_TRILINOS
181 DoFTools::make_sparsity_pattern(
182 dof_handler, sparsity_pattern_, affine_constraints_,
false);
191 dof_handler, sparsity_pattern_, affine_constraints_,
false);
201 SparsityTools::distribute_sparsity_pattern(
204 mpi_ensemble_.ensemble_communicator(),
209 template <
int dim,
typename Number>
210 void OfflineData<dim, Number>::setup(
const unsigned int problem_dimension,
211 const unsigned int n_precomputed_values)
214 std::cout <<
"OfflineData<dim, Number>::setup()" << std::endl;
221 const auto &triangulation = discretization_->triangulation();
223 dof_handler_ = std::make_unique<dealii::DoFHandler<dim>>(triangulation);
224 auto &dof_handler = *dof_handler_;
232 discretization_->selected_geometry().update_dof_handler(dof_handler);
234 dof_handler.distribute_dofs(discretization_->finite_element());
236 n_locally_owned_ = dof_handler.locally_owned_dofs().n_elements();
242 DoFRenumbering::Cuthill_McKee(dof_handler);
253 mpi_ensemble_.ensemble_communicator(),
264 create_constraints_and_sparsity_pattern();
266 dof_handler, sparsity_pattern_, VectorizedArray<Number>::size());
281 mpi_ensemble_.ensemble_communicator(),
283 VectorizedArray<Number>::size());
289 const auto consistent_stride_range [[maybe_unused]] = [&]() {
290 constexpr auto group_size = VectorizedArray<Number>::size();
291 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
292 const auto offset = n_locally_owned_ != 0 ? *locally_owned.begin() : 0;
294 unsigned int group_row_length = 0;
296 for (; i < n_locally_internal_; ++i) {
297 if (i % group_size == 0) {
298 group_row_length = sparsity_pattern_.row_length(offset + i);
300 if (group_row_length != sparsity_pattern_.row_length(offset + i)) {
305 return i / group_size * group_size;
311 const auto mpi_allreduce_logical_or = [&](
const bool local_value) {
312 std::function<bool(
const bool &,
const bool &)> comparator =
313 [](
const bool &left,
const bool &right) ->
bool {
314 return left || right;
316 return Utilities::MPI::all_reduce(
317 local_value, mpi_ensemble_.ensemble_communicator(), comparator);
324 create_constraints_and_sparsity_pattern();
334#if DEAL_II_VERSION_GTE(9, 5, 0)
335 if (mpi_allreduce_logical_or(affine_constraints_.n_constraints() > 0)) {
336 if (mpi_allreduce_logical_or(
337 consistent_stride_range() != n_locally_internal_)) {
347 VectorizedArray<Number>::size());
348 create_constraints_and_sparsity_pattern();
349 n_locally_internal_ = consistent_stride_range();
359 Assert(consistent_stride_range() == n_locally_internal_,
360 dealii::ExcInternalError());
366 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
367 Assert(n_locally_owned_ == locally_owned.n_elements(),
368 dealii::ExcInternalError());
370#if DEAL_II_VERSION_GTE(9, 6, 0)
371 auto locally_relevant =
372 DoFTools::extract_locally_relevant_dofs(dof_handler);
374 IndexSet locally_relevant;
375 DoFTools::extract_locally_relevant_dofs(dof_handler, locally_relevant);
379 IndexSet additional_dofs(dof_handler.n_dofs());
380 for (
auto &entry : sparsity_pattern_)
381 if (!locally_relevant.is_element(entry.column())) {
382 Assert(locally_owned.is_element(entry.row()), ExcInternalError());
383 additional_dofs.add_index(entry.column());
385 additional_dofs.compress();
386 locally_relevant.add_indices(additional_dofs);
387 locally_relevant.compress();
390 n_locally_relevant_ = locally_relevant.n_elements();
392 scalar_partitioner_ = std::make_shared<dealii::Utilities::MPI::Partitioner>(
393 locally_owned, locally_relevant, mpi_ensemble_.ensemble_communicator());
396 scalar_partitioner_, problem_dimension);
399 scalar_partitioner_, n_precomputed_values);
407 if (mpi_allreduce_logical_or(affine_constraints_.n_constraints() > 0)) {
411 n_export_indices_ = 0;
412 for (
const auto &it : scalar_partitioner_->import_indices())
413 if (it.second <= n_locally_internal_)
414 n_export_indices_ = std::max(n_export_indices_, it.second);
416 constexpr auto simd_length = VectorizedArray<Number>::size();
418 (n_export_indices_ + simd_length - 1) / simd_length * simd_length;
423 unsigned int control = 0;
424 for (
const auto &it : scalar_partitioner_->import_indices())
425 if (it.second <= n_locally_internal_)
426 control = std::max(control, it.second);
428 Assert(control <= n_export_indices_, ExcInternalError());
429 Assert(n_export_indices_ <= n_locally_internal_, ExcInternalError());
438 sparsity_pattern_simd_.reinit(
439 n_locally_internal_, sparsity_pattern_, scalar_partitioner_);
443 template <
int dim,
typename Number>
444 void OfflineData<dim, Number>::create_matrices()
447 std::cout <<
"OfflineData<dim, Number>::create_matrices()" << std::endl;
454 mass_matrix_.reinit(sparsity_pattern_simd_);
455 if (discretization_->have_discontinuous_ansatz())
456 mass_matrix_inverse_.reinit(sparsity_pattern_simd_);
458 lumped_mass_matrix_.reinit(scalar_partitioner_);
459 lumped_mass_matrix_inverse_.reinit(scalar_partitioner_);
461 betaij_matrix_.reinit(sparsity_pattern_simd_);
462 cij_matrix_.reinit(sparsity_pattern_simd_);
463 if (discretization_->have_discontinuous_ansatz())
464 incidence_matrix_.reinit(sparsity_pattern_simd_);
470 auto &dof_handler = *dof_handler_;
472 measure_of_omega_ = 0.;
474#ifdef DEAL_II_WITH_TRILINOS
477 AffineConstraints<double> affine_constraints_assembly;
479#if DEAL_II_VERSION_GTE(9, 6, 0)
480 affine_constraints_assembly.reinit(affine_constraints_.get_local_lines(),
481 affine_constraints_.get_local_lines());
483 affine_constraints_assembly.reinit(affine_constraints_.get_local_lines());
485 for (
auto line : affine_constraints_.get_lines()) {
486 affine_constraints_assembly.add_line(line.index);
487 for (
auto entry : line.entries)
488 affine_constraints_assembly.add_entry(
489 line.index, entry.first, entry.second);
490 affine_constraints_assembly.set_inhomogeneity(line.index,
493 affine_constraints_assembly.close();
495 const IndexSet &locally_owned = dof_handler.locally_owned_dofs();
496 TrilinosWrappers::SparsityPattern trilinos_sparsity_pattern;
497 trilinos_sparsity_pattern.reinit(locally_owned,
499 mpi_ensemble_.ensemble_communicator());
501 TrilinosWrappers::SparseMatrix mass_matrix_tmp;
502 TrilinosWrappers::SparseMatrix mass_matrix_inverse_tmp;
503 if (discretization_->have_discontinuous_ansatz())
504 mass_matrix_inverse_tmp.reinit(trilinos_sparsity_pattern);
506 TrilinosWrappers::SparseMatrix betaij_matrix_tmp;
507 std::array<TrilinosWrappers::SparseMatrix, dim> cij_matrix_tmp;
509 mass_matrix_tmp.reinit(trilinos_sparsity_pattern);
510 betaij_matrix_tmp.reinit(trilinos_sparsity_pattern);
511 for (
auto &matrix : cij_matrix_tmp)
512 matrix.reinit(trilinos_sparsity_pattern);
517 AffineConstraints<Number> affine_constraints_assembly;
518 affine_constraints_assembly.copy_from(affine_constraints_);
521 SparsityPattern sparsity_pattern_assembly;
523 DynamicSparsityPattern dsp(n_locally_relevant_, n_locally_relevant_);
524 for (
const auto &entry : sparsity_pattern_) {
525 const auto i = scalar_partitioner_->global_to_local(entry.row());
526 const auto j = scalar_partitioner_->global_to_local(entry.column());
529 sparsity_pattern_assembly.copy_from(dsp);
532 dealii::SparseMatrix<Number> mass_matrix_tmp;
533 dealii::SparseMatrix<Number> mass_matrix_inverse_tmp;
534 if (discretization_->have_discontinuous_ansatz())
535 mass_matrix_inverse_tmp.reinit(sparsity_pattern_assembly);
537 dealii::SparseMatrix<Number> betaij_matrix_tmp;
538 std::array<dealii::SparseMatrix<Number>, dim> cij_matrix_tmp;
540 mass_matrix_tmp.reinit(sparsity_pattern_assembly);
541 betaij_matrix_tmp.reinit(sparsity_pattern_assembly);
542 for (
auto &matrix : cij_matrix_tmp)
543 matrix.reinit(sparsity_pattern_assembly);
551 const auto local_assemble_system = [&](
const auto &cell,
556 auto &is_locally_owned = copy.is_locally_owned_;
557 auto &local_dof_indices = copy.local_dof_indices_;
558 auto &neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
560 auto &cell_mass_matrix = copy.cell_mass_matrix_;
561 auto &cell_mass_matrix_inverse = copy.cell_mass_matrix_inverse_;
562 auto &cell_betaij_matrix = copy.cell_betaij_matrix_;
563 auto &cell_cij_matrix = copy.cell_cij_matrix_;
564 auto &interface_cij_matrix = copy.interface_cij_matrix_;
565 auto &cell_measure = copy.cell_measure_;
567 auto &hp_fe_values = scratch.hp_fe_values_;
568 auto &hp_fe_face_values = scratch.hp_fe_face_values_;
569 auto &hp_fe_neighbor_face_values = scratch.hp_fe_neighbor_face_values_;
571#ifdef DEAL_II_WITH_TRILINOS
572 is_locally_owned = cell->is_locally_owned();
580 is_locally_owned = !cell->is_artificial();
582 if (!is_locally_owned)
585 const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell();
587 cell_mass_matrix.reinit(dofs_per_cell, dofs_per_cell);
588 cell_betaij_matrix.reinit(dofs_per_cell, dofs_per_cell);
589 for (
auto &matrix : cell_cij_matrix)
590 matrix.reinit(dofs_per_cell, dofs_per_cell);
591 if (discretization_->have_discontinuous_ansatz()) {
592 cell_mass_matrix_inverse.reinit(dofs_per_cell, dofs_per_cell);
595 hp_fe_values.reinit(cell);
596 const auto &fe_values = hp_fe_values.get_present_fe_values();
598 local_dof_indices.resize(dofs_per_cell);
599 cell->get_dof_indices(local_dof_indices);
603 cell_mass_matrix = 0.;
604 cell_betaij_matrix = 0.;
605 for (
auto &matrix : cell_cij_matrix)
607 if (discretization_->have_discontinuous_ansatz()) {
608 cell_mass_matrix_inverse = 0.;
612 for (
unsigned int q : fe_values.quadrature_point_indices()) {
613 const auto JxW = fe_values.JxW(q);
616 if (dofs_per_cell != 0 && cell->is_locally_owned())
617 cell_measure += Number(JxW);
619 for (
unsigned int j : fe_values.dof_indices()) {
620 const auto value_JxW = fe_values.shape_value(j, q) * JxW;
621 const auto grad_JxW = fe_values.shape_grad(j, q) * JxW;
623 for (
unsigned int i : fe_values.dof_indices()) {
624 const auto value = fe_values.shape_value(i, q);
625 const auto grad = fe_values.shape_grad(i, q);
627 cell_mass_matrix(i, j) += Number(value * value_JxW);
628 cell_betaij_matrix(i, j) += Number(grad * grad_JxW);
629 for (
unsigned int d = 0; d < dim; ++d)
630 cell_cij_matrix[d](i, j) += Number((value * grad_JxW)[d]);
640 if (!discretization_->have_discontinuous_ansatz())
643 for (
const auto f_index : cell->face_indices()) {
644 const auto &face = cell->face(f_index);
647 const bool has_neighbor =
648 !face->at_boundary() || cell->has_periodic_neighbor(f_index);
652 neighbor_local_dof_indices[f_index].resize(0);
657 const auto neighbor_cell = cell->neighbor_or_periodic_neighbor(f_index);
658 if (neighbor_cell->is_artificial()) {
661 neighbor_local_dof_indices[f_index].resize(0);
666 const bool neighbor_cell_has_fe_nothing =
667 treat_fe_nothing_as_boundary_ &&
668 (
dynamic_cast<const dealii::FE_Nothing<dim> *
>(
669 &neighbor_cell->get_fe()) !=
nullptr);
670 if (neighbor_cell_has_fe_nothing) {
673 neighbor_local_dof_indices[f_index].resize(0);
677 hp_fe_face_values.reinit(cell, f_index);
678 const auto &fe_face_values = hp_fe_face_values.get_present_fe_values();
682 for (
unsigned int q : fe_face_values.quadrature_point_indices()) {
683 const auto JxW = fe_face_values.JxW(q);
684 const auto &normal = fe_face_values.get_normal_vectors()[q];
686 for (
unsigned int j : fe_face_values.dof_indices()) {
687 const auto value_JxW = fe_face_values.shape_value(j, q) * JxW;
689 for (
unsigned int i : fe_face_values.dof_indices()) {
690 const auto value = fe_face_values.shape_value(i, q);
692 for (
unsigned int d = 0; d < dim; ++d)
693 cell_cij_matrix[d](i, j) -=
694 Number(0.5 * normal[d] * value * value_JxW);
701 const unsigned int f_index_neighbor =
702 cell->has_periodic_neighbor(f_index)
703 ? cell->periodic_neighbor_of_periodic_neighbor(f_index)
704 : cell->neighbor_of_neighbor(f_index);
706 const unsigned int neighbor_dofs_per_cell =
707 neighbor_cell->get_fe().n_dofs_per_cell();
708 neighbor_local_dof_indices[f_index].resize(neighbor_dofs_per_cell);
709 neighbor_cell->get_dof_indices(neighbor_local_dof_indices[f_index]);
711 for (
unsigned int k = 0; k < dim; ++k) {
712 interface_cij_matrix[f_index][k].reinit(dofs_per_cell,
713 neighbor_dofs_per_cell);
714 interface_cij_matrix[f_index][k] = 0.;
717 hp_fe_neighbor_face_values.reinit(neighbor_cell, f_index_neighbor);
718 const auto &fe_neighbor_face_values =
719 hp_fe_neighbor_face_values.get_present_fe_values();
721 for (
unsigned int q : fe_face_values.quadrature_point_indices()) {
722 const auto JxW = fe_face_values.JxW(q);
723 const auto &normal = fe_face_values.get_normal_vectors()[q];
726 for (
unsigned int j : fe_neighbor_face_values.dof_indices()) {
727 const auto value_JxW =
728 fe_neighbor_face_values.shape_value(j, q) * JxW;
730 for (
unsigned int i : fe_face_values.dof_indices()) {
731 const auto value = fe_face_values.shape_value(i, q);
733 for (
unsigned int d = 0; d < dim; ++d)
734 interface_cij_matrix[f_index][d](i, j) +=
735 Number(0.5 * normal[d] * value * value_JxW);
745 if (discretization_->have_discontinuous_ansatz()) {
747 if (!cell_mass_matrix_inverse.empty())
748 cell_mass_matrix_inverse.invert(cell_mass_matrix);
752 const auto copy_local_to_global = [&](
const auto ©) {
753 const auto &is_locally_owned = copy.is_locally_owned_;
754#ifdef DEAL_II_WITH_TRILINOS
755 const auto &local_dof_indices = copy.local_dof_indices_;
756 const auto &neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
763 auto local_dof_indices = copy.local_dof_indices_;
764 auto neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
766 const auto &cell_mass_matrix = copy.cell_mass_matrix_;
767 const auto &cell_mass_matrix_inverse = copy.cell_mass_matrix_inverse_;
768 const auto &cell_cij_matrix = copy.cell_cij_matrix_;
769 const auto &interface_cij_matrix = copy.interface_cij_matrix_;
770 const auto &cell_betaij_matrix = copy.cell_betaij_matrix_;
771 const auto &cell_measure = copy.cell_measure_;
773 if (!is_locally_owned)
776#ifndef DEAL_II_WITH_TRILINOS
778 for (
auto &indices : neighbor_local_dof_indices)
782 affine_constraints_assembly.distribute_local_to_global(
783 cell_mass_matrix, local_dof_indices, mass_matrix_tmp);
785 for (
int k = 0; k < dim; ++k) {
786 affine_constraints_assembly.distribute_local_to_global(
787 cell_cij_matrix[k], local_dof_indices, cij_matrix_tmp[k]);
793 if (local_dof_indices.size() != 0) {
794 for (
unsigned int f_index = 0; f_index < copy.n_faces; ++f_index) {
795 if (neighbor_local_dof_indices[f_index].size() != 0) {
796 affine_constraints_assembly.distribute_local_to_global(
797 interface_cij_matrix[f_index][k],
799 neighbor_local_dof_indices[f_index],
806 affine_constraints_assembly.distribute_local_to_global(
807 cell_betaij_matrix, local_dof_indices, betaij_matrix_tmp);
809 if (discretization_->have_discontinuous_ansatz())
810 affine_constraints_assembly.distribute_local_to_global(
811 cell_mass_matrix_inverse,
813 mass_matrix_inverse_tmp);
815 measure_of_omega_ += cell_measure;
818 WorkStream::run(dof_handler.begin_active(),
820 local_assemble_system,
821 copy_local_to_global,
822 AssemblyScratchData<dim>(*discretization_),
823#ifdef DEAL_II_WITH_TRILINOS
824 AssemblyCopyData<dim, double>());
826 AssemblyCopyData<dim, Number>());
829#ifdef DEAL_II_WITH_TRILINOS
832 for (
auto &it : cij_matrix_tmp)
835 betaij_matrix_.read_in(betaij_matrix_tmp,
false);
836 mass_matrix_.read_in(mass_matrix_tmp,
false);
837 if (discretization_->have_discontinuous_ansatz())
838 mass_matrix_inverse_.read_in(mass_matrix_inverse_tmp,
false);
839 cij_matrix_.read_in(cij_matrix_tmp,
false);
841 betaij_matrix_.read_in(betaij_matrix_tmp,
true);
842 mass_matrix_.read_in(mass_matrix_tmp,
true);
843 if (discretization_->have_discontinuous_ansatz())
844 mass_matrix_inverse_.read_in(mass_matrix_inverse_tmp,
true);
845 cij_matrix_.read_in(cij_matrix_tmp,
true);
848 betaij_matrix_.update_ghost_rows();
849 mass_matrix_.update_ghost_rows();
850 if (discretization_->have_discontinuous_ansatz())
851 mass_matrix_inverse_.update_ghost_rows();
852 cij_matrix_.update_ghost_rows();
854 measure_of_omega_ = Utilities::MPI::sum(
855 measure_of_omega_, mpi_ensemble_.ensemble_communicator());
862#ifdef DEAL_II_WITH_TRILINOS
865 affine_constraints_assembly.set_zero(one);
867 ScalarVector local_lumped_mass_matrix(scalar_partitioner_);
868 mass_matrix_tmp.vmult(local_lumped_mass_matrix, one);
871 for (
unsigned int i = 0; i < scalar_partitioner_->locally_owned_size();
873 lumped_mass_matrix_.local_element(i) =
874 local_lumped_mass_matrix.local_element(i);
875 lumped_mass_matrix_inverse_.local_element(i) =
876 1. / lumped_mass_matrix_.local_element(i);
878 lumped_mass_matrix_.update_ghost_values();
879 lumped_mass_matrix_inverse_.update_ghost_values();
883 dealii::Vector<Number> one(mass_matrix_tmp.m());
885 affine_constraints_assembly.set_zero(one);
887 dealii::Vector<Number> local_lumped_mass_matrix(mass_matrix_tmp.m());
888 mass_matrix_tmp.vmult(local_lumped_mass_matrix, one);
890 for (
unsigned int i = 0; i < scalar_partitioner_->locally_owned_size();
892 lumped_mass_matrix_.local_element(i) = local_lumped_mass_matrix(i);
893 lumped_mass_matrix_inverse_.local_element(i) =
894 1. / lumped_mass_matrix_.local_element(i);
896 lumped_mass_matrix_.update_ghost_values();
897 lumped_mass_matrix_inverse_.update_ghost_values();
905 if (discretization_->have_discontinuous_ansatz()) {
906#ifdef DEAL_II_WITH_TRILINOS
907 TrilinosWrappers::SparseMatrix incidence_matrix_tmp;
908 incidence_matrix_tmp.reinit(trilinos_sparsity_pattern);
910 dealii::SparseMatrix<Number> incidence_matrix_tmp;
911 incidence_matrix_tmp.reinit(sparsity_pattern_assembly);
915 const auto local_assemble_system = [&](
const auto &cell,
920 auto &is_locally_owned = copy.is_locally_owned_;
921 auto &local_dof_indices = copy.local_dof_indices_;
922 auto &neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
923 auto &interface_incidence_matrix = copy.interface_incidence_matrix_;
924 auto &hp_fe_face_values_nodal = scratch.hp_fe_face_values_nodal_;
925 auto &hp_fe_neighbor_face_values_nodal =
926 scratch.hp_fe_neighbor_face_values_nodal_;
928#ifdef DEAL_II_WITH_TRILINOS
929 is_locally_owned = cell->is_locally_owned();
931 is_locally_owned = !cell->is_artificial();
933 if (!is_locally_owned)
936 const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell();
938 for (
auto &matrix : interface_incidence_matrix)
939 matrix.reinit(dofs_per_cell, dofs_per_cell);
941 local_dof_indices.resize(dofs_per_cell);
942 cell->get_dof_indices(local_dof_indices);
945 for (
auto &matrix : interface_incidence_matrix)
948 for (
const auto f_index : cell->face_indices()) {
949 const auto &face = cell->face(f_index);
952 const bool has_neighbor =
953 !face->at_boundary() || cell->has_periodic_neighbor(f_index);
957 neighbor_local_dof_indices[f_index].resize(0);
962 const auto neighbor_cell =
963 cell->neighbor_or_periodic_neighbor(f_index);
964 if (neighbor_cell->is_artificial()) {
967 neighbor_local_dof_indices[f_index].resize(0);
972 const bool neighbor_cell_has_fe_nothing =
973 treat_fe_nothing_as_boundary_ &&
974 (
dynamic_cast<const dealii::FE_Nothing<dim> *
>(
975 &neighbor_cell->get_fe()) !=
nullptr);
976 if (neighbor_cell_has_fe_nothing) {
977 neighbor_local_dof_indices[f_index].resize(0);
981 const unsigned int neighbor_dofs_per_cell =
982 neighbor_cell->get_fe().n_dofs_per_cell();
983 neighbor_local_dof_indices[f_index].resize(neighbor_dofs_per_cell);
984 neighbor_cell->get_dof_indices(neighbor_local_dof_indices[f_index]);
986 const unsigned int f_index_neighbor =
987 cell->has_periodic_neighbor(f_index)
988 ? cell->periodic_neighbor_of_periodic_neighbor(f_index)
989 : cell->neighbor_of_neighbor(f_index);
991 hp_fe_face_values_nodal.reinit(cell, f_index);
992 const auto &fe_face_values_nodal =
993 hp_fe_face_values_nodal.get_present_fe_values();
994 hp_fe_neighbor_face_values_nodal.reinit(neighbor_cell,
996 const auto &fe_neighbor_face_values_nodal =
997 hp_fe_neighbor_face_values_nodal.get_present_fe_values();
1001 for (
unsigned int q :
1002 fe_face_values_nodal.quadrature_point_indices()) {
1004 for (
unsigned int j : fe_neighbor_face_values_nodal.dof_indices()) {
1005 const auto v_j = fe_neighbor_face_values_nodal.shape_value(j, q);
1006 for (
unsigned int i : fe_face_values_nodal.dof_indices()) {
1007 const auto v_i = fe_face_values_nodal.shape_value(i, q);
1008 constexpr auto eps = std::numeric_limits<Number>::epsilon();
1009 if (std::abs(v_i * v_j) > 100. * eps) {
1010 const auto &ansatz = discretization_->ansatz();
1012 const auto glob_i = local_dof_indices[i];
1013 const auto glob_j = neighbor_local_dof_indices[f_index][j];
1014 const auto m_i = lumped_mass_matrix_[glob_i];
1015 const auto m_j = lumped_mass_matrix_[glob_j];
1017 Number(0.5) * (m_i + m_j) / measure_of_omega_;
1028 r_ij =
std::pow(hd_ij, incidence_relaxation_even_ / dim);
1035 r_ij =
std::pow(hd_ij, incidence_relaxation_odd_ / dim);
1038 interface_incidence_matrix[f_index](i, j) += r_ij;
1046 const auto copy_local_to_global = [&](
const auto ©) {
1047 const auto &is_locally_owned = copy.is_locally_owned_;
1048#ifdef DEAL_II_WITH_TRILINOS
1049 const auto &local_dof_indices = copy.local_dof_indices_;
1050 const auto &neighbor_local_dof_indices =
1051 copy.neighbor_local_dof_indices_;
1058 auto local_dof_indices = copy.local_dof_indices_;
1059 auto neighbor_local_dof_indices = copy.neighbor_local_dof_indices_;
1061 const auto &interface_incidence_matrix =
1062 copy.interface_incidence_matrix_;
1064 if (!is_locally_owned)
1067#ifndef DEAL_II_WITH_TRILINOS
1069 for (
auto &indices : neighbor_local_dof_indices)
1077 if (local_dof_indices.size() != 0) {
1078 for (
unsigned int f_index = 0; f_index < copy.n_faces; ++f_index) {
1079 if (neighbor_local_dof_indices[f_index].size() != 0) {
1080 affine_constraints_assembly.distribute_local_to_global(
1081 interface_incidence_matrix[f_index],
1083 neighbor_local_dof_indices[f_index],
1084 incidence_matrix_tmp);
1090 WorkStream::run(dof_handler.begin_active(),
1092 local_assemble_system,
1093 copy_local_to_global,
1094 AssemblyScratchData<dim>(*discretization_),
1095#ifdef DEAL_II_WITH_TRILINOS
1096 AssemblyCopyData<dim, double>());
1098 AssemblyCopyData<dim, Number>());
1101#ifdef DEAL_II_WITH_TRILINOS
1102 incidence_matrix_.read_in(incidence_matrix_tmp,
false);
1104 incidence_matrix_.read_in(incidence_matrix_tmp,
true);
1106 incidence_matrix_.update_ghost_rows();
1113 boundary_map_ = construct_boundary_map(
1114 dof_handler.begin_active(), dof_handler.end(), *scalar_partitioner_);
1116 coupling_boundary_pairs_ = collect_coupling_boundary_pairs(
1117 dof_handler.begin_active(), dof_handler.end(), *scalar_partitioner_);
1120#ifdef DEBUG_SYMMETRY_CHECK
1125 double total_mass = 0.;
1126 for (
unsigned int i = 0; i < n_locally_owned_; ++i)
1127 total_mass += lumped_mass_matrix_.local_element(i);
1129 Utilities::MPI::sum(total_mass, mpi_ensemble_.ensemble_communicator());
1131 Assert(std::abs(measure_of_omega_ - total_mass) <
1132 1.e-12 * measure_of_omega_,
1134 "Total mass differs from the measure of the domain."));
1140 for (
unsigned int i = 0; i < n_locally_owned_; ++i) {
1142 const unsigned int row_length = sparsity_pattern_simd_.row_length(i);
1143 if (row_length == 1)
1147 mass_matrix_.get_entry(i, 0) - lumped_mass_matrix_.local_element(i);
1150 constexpr auto simd_length = VectorizedArray<Number>::size();
1151 const unsigned int *js = sparsity_pattern_simd_.columns(i);
1152 for (
unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1153 const auto j = *(i < n_locally_internal_ ? js + col_idx * simd_length
1155 Assert(j < n_locally_relevant_, dealii::ExcInternalError());
1157 const auto m_ij = mass_matrix_.get_entry(i, col_idx);
1158 if (discretization_->have_discontinuous_ansatz()) {
1161 Assert(std::abs(m_ij) > -1.e-12, dealii::ExcInternalError());
1163 Assert(std::abs(m_ij) > 1.e-12, dealii::ExcInternalError());
1167 const auto m_ji = mass_matrix_.get_transposed_entry(i, col_idx);
1168 if (std::abs(m_ij - m_ji) >= 1.e-12) {
1170 std::stringstream ss;
1171 ss <<
"m_ij matrix is not symmetric: " << m_ij <<
" <-> " << m_ji;
1172 Assert(
false, dealii::ExcMessage(ss.str()));
1176 Assert(std::abs(sum) < 1.e-12, dealii::ExcInternalError());
1183 for (
unsigned int i = 0; i < n_locally_owned_; ++i) {
1185 const unsigned int row_length = sparsity_pattern_simd_.row_length(i);
1186 if (row_length == 1)
1189 auto sum = cij_matrix_.get_tensor(i, 0);
1192 constexpr auto simd_length = VectorizedArray<Number>::size();
1193 const unsigned int *js = sparsity_pattern_simd_.columns(i);
1194 for (
unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1195 const auto j = *(i < n_locally_internal_ ? js + col_idx * simd_length
1197 Assert(j < n_locally_relevant_, dealii::ExcInternalError());
1199 const auto c_ij = cij_matrix_.get_tensor(i, col_idx);
1200 Assert(c_ij.norm() > 1.e-12, dealii::ExcInternalError());
1203 const auto c_ji = cij_matrix_.get_transposed_tensor(i, col_idx);
1204 if ((c_ij + c_ji).norm() >= 1.e-12) {
1208 CouplingDescription coupling{i, col_idx, j};
1209 const auto it = std::find(coupling_boundary_pairs_.begin(),
1210 coupling_boundary_pairs_.end(),
1212 if (it == coupling_boundary_pairs_.end()) {
1213 std::stringstream ss;
1214 ss <<
"c_ij matrix is not anti-symmetric: " << c_ij <<
" <-> "
1216 Assert(
false, dealii::ExcMessage(ss.str()));
1221 Assert(sum.norm() < 1.e-12, dealii::ExcInternalError());
1227 template <
int dim,
typename Number>
1228 void OfflineData<dim, Number>::create_multigrid_data()
1231 std::cout <<
"OfflineData<dim, Number>::create_multigrid_data()"
1235 auto &dof_handler = *dof_handler_;
1237 Assert(!dof_handler.has_hp_capabilities(), dealii::ExcInternalError());
1239 dof_handler.distribute_mg_dofs();
1241 const auto n_levels = dof_handler.get_triangulation().n_global_levels();
1243 AffineConstraints<float> level_constraints;
1246 level_boundary_map_.resize(n_levels);
1247 level_lumped_mass_matrix_.resize(n_levels);
1249 for (
unsigned int level = 0; level < n_levels; ++level) {
1252#if DEAL_II_VERSION_GTE(9, 6, 0)
1253 const auto relevant_dofs =
1254 dealii::DoFTools::extract_locally_relevant_level_dofs(dof_handler,
1257 IndexSet relevant_dofs;
1258 dealii::DoFTools::extract_locally_relevant_level_dofs(
1259 dof_handler, level, relevant_dofs);
1262 const auto partitioner = std::make_shared<Utilities::MPI::Partitioner>(
1263 dof_handler.locally_owned_mg_dofs(level),
1265 mpi_ensemble_.ensemble_communicator());
1266 level_lumped_mass_matrix_[level].reinit(partitioner);
1267 std::vector<types::global_dof_index> dof_indices(
1268 dof_handler.get_fe().dofs_per_cell);
1269 dealii::Vector<Number> mass_values(dof_handler.get_fe().dofs_per_cell);
1270 dealii::hp::FEValues<dim> hp_fe_values(discretization_->mapping(),
1271 discretization_->finite_element(),
1272 discretization_->quadrature(),
1273 update_values | update_JxW_values);
1274 for (
const auto &cell : dof_handler.cell_iterators_on_level(level))
1277 if (cell->is_locally_owned_on_level()) {
1278 hp_fe_values.reinit(cell);
1279 const auto &fe_values = hp_fe_values.get_present_fe_values();
1280 for (
unsigned int i = 0; i < mass_values.size(); ++i) {
1282 for (
unsigned int q = 0; q < fe_values.n_quadrature_points; ++q)
1283 sum += fe_values.shape_value(i, q) * fe_values.JxW(q);
1284 mass_values(i) = sum;
1286 cell->get_mg_dof_indices(dof_indices);
1287 level_constraints.distribute_local_to_global(
1288 mass_values, dof_indices, level_lumped_mass_matrix_[level]);
1294 level_boundary_map_[level] = construct_boundary_map(
1295 dof_handler.begin_mg(level), dof_handler.end_mg(level), *partitioner);
1300 template <
int dim,
typename Number>
1301 template <
typename ITERATOR1,
typename ITERATOR2>
1303 const ITERATOR1 &begin,
1304 const ITERATOR2 &end,
1305 const Utilities::MPI::Partitioner &partitioner)
const -> BoundaryMap
1308 std::cout <<
"OfflineData<dim, Number>::construct_boundary_map()"
1316 using BoundaryData = std::tuple<dealii::Tensor<1, dim, Number> ,
1319 dealii::types::boundary_id ,
1320 dealii::Point<dim>> ;
1321 std::multimap<unsigned int, BoundaryData> preliminary_map;
1323 std::vector<dealii::types::global_dof_index> local_dof_indices;
1325 dealii::hp::FEFaceValues<dim> hp_fe_face_values(
1326 discretization_->mapping(),
1327 discretization_->finite_element(),
1328 discretization_->face_quadrature(),
1329 dealii::update_normal_vectors | dealii::update_values |
1330 dealii::update_JxW_values);
1332 for (
auto cell = begin; cell != end; ++cell) {
1340 if ((cell->is_active() && cell->is_artificial()) ||
1341 (!cell->is_active() && cell->is_artificial_on_level()))
1344 const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell();
1345 const auto &support_points = cell->get_fe().get_unit_support_points();
1347 local_dof_indices.resize(dofs_per_cell);
1348 cell->get_active_or_mg_dof_indices(local_dof_indices);
1350 for (
auto f_index : cell->face_indices()) {
1351 const auto face = cell->face(f_index);
1352 auto id = face->boundary_id();
1365 const bool neighbor_cell_has_fe_nothing =
1366 treat_fe_nothing_as_boundary_ &&
1367 !face->at_boundary() &&
1368 cell->neighbor(f_index)->is_active() &&
1369 !cell->neighbor(f_index)->is_artificial() &&
1370 (
dynamic_cast<const dealii::FE_Nothing<dim> *
>(
1371 &cell->neighbor(f_index)->get_fe()) !=
nullptr);
1373 if (neighbor_cell_has_fe_nothing) {
1378 id = cell->neighbor(f_index)->material_id();
1380 }
else if (!face->at_boundary()) {
1385 hp_fe_face_values.reinit(cell, f_index);
1386 const auto &fe_face_values = hp_fe_face_values.get_present_fe_values();
1387 const auto &mapping =
1388 hp_fe_face_values.get_mapping_collection()[cell->active_fe_index()];
1390 for (
unsigned int j : fe_face_values.dof_indices()) {
1391 if (!cell->get_fe().has_support_on_face(j, f_index))
1394 Number boundary_mass = 0.;
1395 dealii::Tensor<1, dim, Number> normal;
1397 for (
unsigned int q : fe_face_values.quadrature_point_indices()) {
1398 const auto JxW = fe_face_values.JxW(q);
1399 const auto phi_i = fe_face_values.shape_value(j, q);
1401 boundary_mass += phi_i * JxW;
1402 normal += phi_i * fe_face_values.normal_vector(q) * JxW;
1405 const auto global_index = local_dof_indices[j];
1406 const auto index = partitioner.global_to_local(global_index);
1409 if (index >= n_locally_owned_)
1413 const unsigned int row_length =
1414 sparsity_pattern_simd_.row_length(index);
1415 if (row_length == 1)
1418 Point<dim> position =
1419 mapping.transform_unit_to_real_cell(cell, support_points[j]);
1425 preliminary_map.insert(
1426 {index, {normal, boundary_mass, boundary_mass, id, position}});
1442 std::multimap<unsigned int, BoundaryData> filtered_map;
1443 std::set<dealii::types::global_dof_index> boundary_dofs;
1444 for (
auto entry : preliminary_map) {
1445 bool inserted =
false;
1446 const auto range = filtered_map.equal_range(entry.first);
1447 for (
auto it = range.first; it != range.second; ++it) {
1452 new_point] = entry.second;
1453 auto &[normal, normal_mass, boundary_mass, id, point] = it->second;
1458 Assert(point.distance(new_point) < 1.0e-14, dealii::ExcInternalError());
1460 if (normal * new_normal / normal.norm() / new_normal.norm() > 0.50) {
1465 normal += new_normal;
1466 boundary_mass += new_boundary_mass;
1470 }
else if constexpr (dim == 2) {
1486 filtered_map.insert(entry);
1493 BoundaryMap boundary_map;
1495 std::begin(filtered_map),
1496 std::end(filtered_map),
1497 std::back_inserter(boundary_map),
1499 auto index = it.first;
1500 const auto &[normal, normal_mass, boundary_mass, id, point] =
1503 const auto new_normal_mass =
1504 normal.norm() + std::numeric_limits<Number>::epsilon();
1505 const auto new_normal = normal / new_normal_mass;
1507 return {index, new_normal, new_normal_mass, boundary_mass, id, point};
1510 return boundary_map;
1514 template <
int dim,
typename Number>
1515 template <
typename ITERATOR1,
typename ITERATOR2>
1517 const ITERATOR1 &begin,
1518 const ITERATOR2 &end,
1519 const Utilities::MPI::Partitioner &partitioner)
const
1520 -> CouplingBoundaryPairs
1523 std::cout <<
"OfflineData<dim, Number>::collect_coupling_boundary_pairs()"
1533 std::set<unsigned int> locally_relevant_boundary_indices;
1535 std::vector<dealii::types::global_dof_index> local_dof_indices;
1537 for (
auto cell = begin; cell != end; ++cell) {
1540 if (cell->is_artificial())
1543 const auto &finite_element = cell->get_fe();
1544 const unsigned int dofs_per_cell = finite_element.dofs_per_cell;
1545 local_dof_indices.resize(dofs_per_cell);
1546 cell->get_active_or_mg_dof_indices(local_dof_indices);
1548 for (
auto f_index : cell->face_indices()) {
1549 const auto face = cell->face(f_index);
1550 const auto id = face->boundary_id();
1559 const bool neighbor_cell_has_fe_nothing =
1560 treat_fe_nothing_as_boundary_ &&
1561 !face->at_boundary() &&
1562 cell->neighbor(f_index)->is_active() &&
1563 !cell->neighbor(f_index)->is_artificial() &&
1564 (
dynamic_cast<const dealii::FE_Nothing<dim> *
>(
1565 &cell->neighbor(f_index)->get_fe()) !=
nullptr);
1567 if (!neighbor_cell_has_fe_nothing && !face->at_boundary())
1570 for (
unsigned int j = 0; j < dofs_per_cell; ++j) {
1572 if (!cell->get_fe().has_support_on_face(j, f_index))
1575 const auto global_index = local_dof_indices[j];
1576 const auto index = partitioner.global_to_local(global_index);
1579 if (index >= n_locally_relevant_)
1582 locally_relevant_boundary_indices.insert(index);
1591 CouplingBoundaryPairs result;
1593 for (
const auto i : locally_relevant_boundary_indices) {
1596 if (i >= n_locally_owned_)
1599 const unsigned int row_length = sparsity_pattern_simd_.row_length(i);
1602 if (row_length == 1)
1605 const unsigned int *js = sparsity_pattern_simd_.columns(i);
1606 constexpr auto simd_length = VectorizedArray<Number>::size();
1608 for (
unsigned int col_idx = 1; col_idx < row_length; ++col_idx) {
1609 const auto j = *(i < n_locally_internal_ ? js + col_idx * simd_length
1612 if (locally_relevant_boundary_indices.count(j) != 0) {
1613 result.push_back({i, col_idx, j});
std::tuple< unsigned int, dealii::Tensor< 1, dim, Number >, Number, Number, dealii::types::boundary_id, dealii::Point< dim > > BoundaryDescription
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
DEAL_II_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)