PolyDEAL
Loading...
Searching...
No Matches
agglomeration_handler.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) XXXX - YYYY by the polyDEAL authors
5//
6// This file is part of the polyDEAL library.
7//
8// Detailed license information governing the source code
9// can be found in LICENSE.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14
16
18
20#include <fe_agglodgp.h>
21
22template <int dim, int spacedim>
24 const GridTools::Cache<dim, spacedim> &cache_tria)
25 : cached_tria(std::make_unique<GridTools::Cache<dim, spacedim>>(
26 cache_tria.get_triangulation(),
27 cache_tria.get_mapping()))
28 , communicator(cache_tria.get_triangulation().get_mpi_communicator())
29{
30 Assert(dim == spacedim, ExcNotImplemented("Not available with codim > 0"));
31 Assert(dim == 2 || dim == 3, ExcImpossibleInDim(1));
33 &cached_tria->get_triangulation()) == nullptr),
34 ExcNotImplemented());
35 Assert(cached_tria->get_triangulation().n_active_cells() > 0,
36 ExcMessage(
37 "The triangulation must not be empty upon calling this function."));
38
40 hybrid_mesh = false;
42}
43
44
45
46template <int dim, int spacedim>
49 const AgglomerationContainer &cells)
50{
51 Assert(cells.size() > 0, ExcMessage("No cells to be agglomerated."));
52
53 if (cells.size() == 1)
54 hybrid_mesh = true; // mesh is made also by classical cells
55
56 // First index drives the selection of the master cell. After that, store the
57 // master cell.
58 const types::global_cell_index global_master_idx =
59 cells[0]->global_active_cell_index();
60 const types::global_cell_index master_idx = cells[0]->active_cell_index();
61 master_cells_container.push_back(cells[0]);
62 master_slave_relationships[global_master_idx] = -1;
63
64 const typename DoFHandler<dim>::active_cell_iterator cell_dh =
65 cells[0]->as_dof_handler_iterator(agglo_dh);
66 cell_dh->set_active_fe_index(CellAgglomerationType::master);
67
68 // Store slave cells and save the relationship with the parent
69 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>
70 slaves;
71 slaves.reserve(cells.size() - 1);
72 // exclude first cell since it's the master cell
73 for (auto it = ++cells.begin(); it != cells.end(); ++it)
74 {
75 slaves.push_back(*it);
76 master_slave_relationships[(*it)->global_active_cell_index()] =
77 global_master_idx; // mark each slave
78 master_slave_relationships_iterators[(*it)->active_cell_index()] =
79 cells[0];
80
81 const typename DoFHandler<dim>::active_cell_iterator cell =
82 (*it)->as_dof_handler_iterator(agglo_dh);
83 cell->set_active_fe_index(CellAgglomerationType::slave); // slave cell
84
85 // If we have a p::d::T, check that all cells are in the same subdomain.
86 // If serial, just check that the subdomain_id is invalid.
87 Assert(((*it)->subdomain_id() == tria->locally_owned_subdomain() ||
88 tria->locally_owned_subdomain() == numbers::invalid_subdomain_id),
89 ExcInternalError());
90 }
91
93 cells[0]; // set iterator to master cell
94
95 // Store the slaves of each master
96 master2slaves[master_idx] = slaves;
97 // Save to which polygon this agglomerate correspond
98 master2polygon[master_idx] = n_agglomerations;
99
100 ++n_agglomerations; // an agglomeration has been performed, record it
101
102 create_bounding_box(cells); // fill the vector of bboxes
103
104 // Finally, return a polygonal iterator to the polytope just constructed.
105 return {cells[0], this};
106}
107
108template <int dim, int spacedim>
111 const AgglomerationContainer &cells,
112 const unsigned int fecollection_size)
113{
114 Assert(cells.size() > 0, ExcMessage("No cells to be agglomerated."));
115
116 if (cells.size() == 1)
117 hybrid_mesh = true; // mesh is made also by classical cells
118
119 // First index drives the selection of the master cell. After that, store the
120 // master cell.
121 const types::global_cell_index global_master_idx =
122 cells[0]->global_active_cell_index();
123 const types::global_cell_index master_idx = cells[0]->active_cell_index();
124 master_cells_container.push_back(cells[0]);
125 master_slave_relationships[global_master_idx] = -1;
126
127 const typename DoFHandler<dim>::active_cell_iterator cell_dh =
128 cells[0]->as_dof_handler_iterator(agglo_dh);
129 cell_dh->set_active_fe_index(CellAgglomerationType::master);
130
131 // Store slave cells and save the relationship with the parent
132 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>
133 slaves;
134 slaves.reserve(cells.size() - 1);
135 // exclude first cell since it's the master cell
136 for (auto it = ++cells.begin(); it != cells.end(); ++it)
137 {
138 slaves.push_back(*it);
139 master_slave_relationships[(*it)->global_active_cell_index()] =
140 global_master_idx; // mark each slave
141 master_slave_relationships_iterators[(*it)->active_cell_index()] =
142 cells[0];
143
144 const typename DoFHandler<dim>::active_cell_iterator cell =
145 (*it)->as_dof_handler_iterator(agglo_dh);
146 cell->set_active_fe_index(
147 fecollection_size); // slave cell (the last index)
148
149 // If we have a p::d::T, check that all cells are in the same subdomain.
150 // If serial, just check that the subdomain_id is invalid.
151 Assert(((*it)->subdomain_id() == tria->locally_owned_subdomain() ||
152 tria->locally_owned_subdomain() == numbers::invalid_subdomain_id),
153 ExcInternalError());
154 }
155
157 cells[0]; // set iterator to master cell
158
159 // Store the slaves of each master
160 master2slaves[master_idx] = slaves;
161 // Save to which polygon this agglomerate correspond
162 master2polygon[master_idx] = n_agglomerations;
163
164 ++n_agglomerations; // an agglomeration has been performed, record it
165
166 create_bounding_box(cells); // fill the vector of bboxes
167
168 // Finally, return a polygonal iterator to the polytope just constructed.
169 return {cells[0], this};
170}
171
172
173
174template <int dim, int spacedim>
175void
177 const AgglomerationContainer &cells)
178{
179 Assert(cells.size() > 0, ExcMessage("No cells to be agglomerated."));
180
181 // Compute the graph associated to the agglomerate
182 Utils::Graph g;
184
185 // The following vector will be filled with connected components
186 std::vector<std::vector<types::global_cell_index>> connected_components;
187 Utils::compute_connected_components(g, connected_components);
188
189 // Be verbose in debug mode
190#ifdef AGGLO_DEBUG
191 if (connected_components.size() > 1)
192 std::cout << "Disconnected elements. Connected components will be "
193 "computed and agglomerated together."
194 << std::endl;
195#endif
196
197 // Get connected components and define one agglomerate for each one of them.
198 std::vector<typename Triangulation<dim>::active_cell_iterator> agglomerate;
199 for (const auto &comp : connected_components)
200 {
201 agglomerate.reserve(comp.size());
202 for (const types::global_cell_index local_idx : comp)
203 agglomerate.push_back(cells[local_idx]);
204
205 // Perform agglomeration
206 define_agglomerate(agglomerate);
207 agglomerate.clear();
208 }
209}
210
211
212template <int dim, int spacedim>
213void
215 const Quadrature<dim> &cell_quadrature,
216 const UpdateFlags &flags,
217 const Quadrature<dim - 1> &face_quadrature,
218 const UpdateFlags &face_flags)
219{
220 agglomeration_quad = cell_quadrature;
221 agglomeration_flags = flags;
222 agglomeration_face_quad = face_quadrature;
224
225
226 no_values =
227 std::make_unique<FEValues<dim>>(*mapping,
228 dummy_fe,
231 update_JxW_values); // only for quadrature
232 no_face_values = std::make_unique<FEFaceValues<dim>>(
233 *mapping,
234 dummy_fe,
237 update_normal_vectors); // only for quadrature
238}
239
240template <int dim, int spacedim>
241void
243 const hp::QCollection<dim> &cell_qcollection,
244 const UpdateFlags &flags,
245 const hp::QCollection<dim - 1> &face_qcollection,
246 const UpdateFlags &face_flags)
247{
248 agglomeration_quad_collection = cell_qcollection;
250 agglomeration_face_quad_collection = face_qcollection;
252
255 hp_no_values = std::make_unique<hp::FEValues<dim>>(
259 update_quadrature_points | update_JxW_values); // only for quadrature
260
261 hp_no_face_values = std::make_unique<hp::FEFaceValues<dim>>(
266 update_normal_vectors); // only for quadrature
267}
268
269
270
271template <int dim, int spacedim>
272unsigned int
275{
276 unsigned int n_neighbors = 0;
277 for (const auto &f : cell->face_indices())
278 {
279 const auto &neighboring_cell = cell->neighbor(f);
280 if ((cell->face(f)->at_boundary()) ||
281 (neighboring_cell->is_active() &&
282 !are_cells_agglomerated(cell, neighboring_cell)))
283 {
284 ++n_neighbors;
285 }
286 }
287 return n_neighbors;
288}
289
291
292template <int dim, int spacedim>
293void
295 const std::unique_ptr<GridTools::Cache<dim, spacedim>> &cache_tria)
296{
297 tria = &cache_tria->get_triangulation();
298 mapping = &cache_tria->get_mapping();
299
300 agglo_dh.reinit(*tria);
301
302 if (const auto parallel_tria = dynamic_cast<
303 const dealii::parallel::TriangulationBase<dim, spacedim> *>(&*tria))
304 {
305 const std::weak_ptr<const Utilities::MPI::Partitioner> cells_partitioner =
306 parallel_tria->global_active_cell_index_partitioner();
308 cells_partitioner.lock()->locally_owned_range(), communicator);
309 }
310 else
311 {
312 master_slave_relationships.reinit(tria->n_active_cells(), MPI_COMM_SELF);
313 }
314
315 polytope_cache.clear();
316 bboxes.clear();
317
318 // First, update the pointer
319 cached_tria = std::make_unique<GridTools::Cache<dim, spacedim>>(
320 cache_tria->get_triangulation(), cache_tria->get_mapping());
321
322 connect_to_tria_signals();
323 n_agglomerations = 0;
324}
325
326
328template <int dim, int spacedim>
329void
331 const FiniteElement<dim> &fe_space)
332{
333 if (dynamic_cast<const FE_DGQ<dim> *>(&fe_space))
334 fe = std::make_unique<FE_DGQ<dim>>(fe_space.degree);
335 else if (dynamic_cast<const FE_AggloDGP<dim> *>(&fe_space))
336 fe = std::make_unique<FE_AggloDGP<dim>>(fe_space.degree);
337 else if (dynamic_cast<const FE_SimplexDGP<dim> *>(&fe_space))
338 fe = std::make_unique<FE_SimplexDGP<dim>>(fe_space.degree);
339 else
341 false,
342 ExcNotImplemented(
343 "Currently, this interface supports only DGQ and DGP bases."));
344
345 box_mapping = std::make_unique<MappingBox<dim>>(
346 bboxes,
347 master2polygon); // construct bounding box mapping
348
349 if (hybrid_mesh)
350 {
351 // the mesh is composed by standard and agglomerate cells. initialize
352 // classes needed for standard cells in order to treat that finite
353 // element space as defined on a standard shape and not on the
354 // BoundingBox.
356 std::make_unique<ScratchData>(*mapping,
357 *fe,
358 QGauss<dim>(2 * fe_space.degree + 2),
360 }
361
362
363 fe_collection.push_back(*fe); // master
364 fe_collection.push_back(
365 FE_Nothing<dim, spacedim>(fe->reference_cell())); // slave
366
368
369 // in case the tria is distributed, communicate ghost information with
370 // neighboring ranks
371 const bool needs_ghost_info =
372 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&*tria) !=
373 nullptr;
374 if (needs_ghost_info)
376
378
379 if (needs_ghost_info)
381}
382
383template <int dim, int spacedim>
384void
386 const hp::FECollection<dim, spacedim> &fe_collection_in)
387{
388 is_hp_collection = true;
389
390 hp_fe_collection = std::make_unique<hp::FECollection<dim, spacedim>>(
391 fe_collection_in); // copy the input collection
392
393 box_mapping = std::make_unique<MappingBox<dim>>(
394 bboxes,
395 master2polygon); // construct bounding box mapping
396
397
398 if (hybrid_mesh)
399 {
400 AssertThrow(false,
401 ExcNotImplemented(
402 "Hybrid mesh is not implemented for hp::FECollection."));
403 }
404
405 for (unsigned int i = 0; i < fe_collection_in.size(); ++i)
406 {
407 if (dynamic_cast<const FESystem<dim> *>(&fe_collection_in[i]))
408 {
409 // System case
410 for (unsigned int b = 0; b < fe_collection_in[i].n_base_elements();
411 ++b)
412 {
413 if (!(dynamic_cast<const FE_DGQ<dim> *>(
414 &fe_collection_in[i].base_element(b)) ||
415 dynamic_cast<const FE_AggloDGP<dim> *>(
416 &fe_collection_in[i].base_element(b)) ||
417 dynamic_cast<const FE_SimplexDGP<dim> *>(
418 &fe_collection_in[i].base_element(b)) ||
419 dynamic_cast<const FE_Nothing<dim> *>(
420 &fe_collection_in[i].base_element(b))))
422 false,
423 ExcNotImplemented(
424 "Currently, this interface supports only DGQ and DGP bases."));
426 }
427 else
428 {
429 // Scalar case
430 if (!(dynamic_cast<const FE_DGQ<dim> *>(&fe_collection_in[i]) ||
431 dynamic_cast<const FE_AggloDGP<dim> *>(&fe_collection_in[i]) ||
432 dynamic_cast<const FE_SimplexDGP<dim> *>(&fe_collection_in[i])))
434 false,
435 ExcNotImplemented(
436 "Currently, this interface supports only DGQ and DGP bases."));
437 }
438 fe_collection.push_back(fe_collection_in[i]);
439 }
440
441 Assert(fe_collection[0].n_components() >= 1,
442 ExcMessage("Invalid FE: must have at least one component."));
443 if (fe_collection[0].n_components() == 1)
444 {
445 fe_collection.push_back(FE_Nothing<dim, spacedim>());
446 }
447 else if (fe_collection[0].n_components() > 1)
448 {
449 std::vector<const FiniteElement<dim, spacedim> *> base_elements;
450 std::vector<unsigned int> multiplicities;
451 for (unsigned int b = 0; b < fe_collection[0].n_base_elements(); ++b)
452 {
453 base_elements.push_back(new FE_Nothing<dim, spacedim>());
454 multiplicities.push_back(fe_collection[0].element_multiplicity(b));
455 }
456 FESystem<dim, spacedim> fe_system_nothing(base_elements, multiplicities);
457 for (const auto *ptr : base_elements)
458 delete ptr;
459 fe_collection.push_back(fe_system_nothing);
460 }
463
464 // in case the tria is distributed, communicate ghost information with
465 // neighboring ranks
466 const bool needs_ghost_info =
467 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&*tria) !=
468 nullptr;
469 if (needs_ghost_info)
471
473
474 if (needs_ghost_info)
476}
477
478template <int dim, int spacedim>
479void
481 const AgglomerationContainer &polytope)
482{
484 ExcMessage("No agglomeration has been performed."));
485 Assert(dim > 1, ExcNotImplemented());
486
487 std::vector<Point<spacedim>> pts; // store all the vertices
488 for (const auto &cell : polytope)
489 for (const auto i : cell->vertex_indices())
490 pts.push_back(cell->vertex(i));
491
492 bboxes.emplace_back(pts);
493}
494
495
496
497template <int dim, int spacedim>
498void
500{
502 ExcMessage("No agglomeration has been performed."));
503 Assert(
504 agglo_dh.n_dofs() > 0,
505 ExcMessage(
506 "The DoFHandler associated to the agglomeration has not been initialized."
507 "It's likely that you forgot to distribute the DoFs. You may want"
508 "to check if a call to `initialize_hp_structure()` has been done."));
509
511 for (const auto &cell : master_cells_container)
512 {
515 }
516
518 {
519 // communicate the number of faces
520 recv_n_faces = Utilities::MPI::some_to_some(communicator, local_n_faces);
521
522 // send information about boundaries and neighboring polytopes id
523 recv_bdary_info =
524 Utilities::MPI::some_to_some(communicator, local_bdary_info);
525
526 recv_ghosted_master_id =
527 Utilities::MPI::some_to_some(communicator, local_ghosted_master_id);
528 }
529}
530
531
532
533template <int dim, int spacedim>
534void
536{
537 const unsigned int dofs_per_cell = fe->dofs_per_cell;
538 for (const auto &polytope : polytope_iterators())
539 {
540 if (polytope->is_locally_owned())
541 {
542 const unsigned int n_faces = polytope->n_faces();
543 for (unsigned int f = 0; f < n_faces; ++f)
544 {
545 if (!polytope->at_boundary(f))
546 {
547 const auto &neigh_polytope = polytope->neighbor(f);
548 if (!neigh_polytope->is_locally_owned())
549 {
550 // Neighboring polytope is ghosted.
551
552 // Compute shape functions at the interface
553 const auto &current_fe = reinit(polytope, f);
554
555 std::vector<Point<spacedim>> qpoints_to_send =
556 current_fe.get_quadrature_points();
557
558 const std::vector<double> &jxws_to_send =
559 current_fe.get_JxW_values();
560
561 const std::vector<Tensor<1, spacedim>> &normals_to_send =
562 current_fe.get_normal_vectors();
563
564
565 const types::subdomain_id neigh_rank =
566 neigh_polytope->subdomain_id();
567
568 std::pair<CellId, unsigned int> cell_and_face{
569 polytope->id(), f};
570 // Prepare data to send
571 local_qpoints[neigh_rank].emplace(cell_and_face,
572 qpoints_to_send);
573
574 local_jxws[neigh_rank].emplace(cell_and_face,
575 jxws_to_send);
576
577 local_normals[neigh_rank].emplace(cell_and_face,
578 normals_to_send);
579
580
581 const unsigned int n_qpoints = qpoints_to_send.size();
582
583 // TODO: check `agglomeration_flags` before computing
584 // values and gradients.
585 std::vector<std::vector<double>> values_per_qpoints(
586 dofs_per_cell);
587
588 std::vector<std::vector<Tensor<1, spacedim>>>
589 gradients_per_qpoints(dofs_per_cell);
590
591 for (unsigned int i = 0; i < dofs_per_cell; ++i)
592 {
593 values_per_qpoints[i].resize(n_qpoints);
594 gradients_per_qpoints[i].resize(n_qpoints);
595 for (unsigned int q = 0; q < n_qpoints; ++q)
596 {
597 values_per_qpoints[i][q] =
598 current_fe.shape_value(i, q);
599 gradients_per_qpoints[i][q] =
600 current_fe.shape_grad(i, q);
601 }
602 }
603
604 local_values[neigh_rank].emplace(cell_and_face,
605 values_per_qpoints);
606 local_gradients[neigh_rank].emplace(
607 cell_and_face, gradients_per_qpoints);
608 }
609 }
610 }
611 }
612 }
613
614 // Finally, exchange with neighboring ranks
615 recv_qpoints = Utilities::MPI::some_to_some(communicator, local_qpoints);
616 recv_jxws = Utilities::MPI::some_to_some(communicator, local_jxws);
617 recv_normals = Utilities::MPI::some_to_some(communicator, local_normals);
618 recv_values = Utilities::MPI::some_to_some(communicator, local_values);
619 recv_gradients = Utilities::MPI::some_to_some(communicator, local_gradients);
620}
621
622
623
624template <int dim, int spacedim>
628 &cells,
630 &master_cell) const
631{
632 Assert(is_master_cell(master_cell),
633 ExcMessage("This must be a master cell."));
634
635 std::vector<Point<dim>> vec_pts;
636 std::vector<double> vec_JxWs;
637
638 if (!is_hp_collection)
639 {
640 // Original version: handle case without hp::FECollection
641 for (const auto &dummy_cell : cells)
642 {
643 no_values->reinit(dummy_cell);
644 auto q_points = no_values->get_quadrature_points(); // real qpoints
645 const auto &JxWs = no_values->get_JxW_values();
646
647 std::transform(q_points.begin(),
648 q_points.end(),
649 std::back_inserter(vec_pts),
650 [&](const Point<spacedim> &p) { return p; });
651 std::transform(JxWs.begin(),
652 JxWs.end(),
653 std::back_inserter(vec_JxWs),
654 [&](const double w) { return w; });
655 }
656 }
657 else
658 {
659 // Handle the hp::FECollection case
660 const auto &master_cell_as_dh_iterator =
661 master_cell->as_dof_handler_iterator(agglo_dh);
662 for (const auto &dummy_cell : cells)
663 {
664 // The following verbose call is necessary to handle cases where
665 // different slave cells on different polytopes use different
666 // quadrature rules. If the hp::QCollection contains multiple
667 // elements, calling hp_no_values->reinit(dummy_cell) won't work
668 // because it cannot infer the correct quadrature rule. By explicitly
669 // passing the active FE index as q_index, and setting mapping_index
670 // and fe_index to 0, we ensure that the dummy cell uses the same
671 // quadrature rule as its corresponding master cell. This assumes a
672 // one-to-one correspondence between hp::QCollection and
673 // hp::FECollection, which is the convention in deal.II. However, this
674 // implementation does not support cases where hp::QCollection and
675 // hp::FECollection have different sizes.
676 // TODO: Refactor the architecture to better handle numerical
677 // integration for hp::QCollection.
678 hp_no_values->reinit(dummy_cell,
679 master_cell_as_dh_iterator->active_fe_index(),
680 0,
681 0);
682 auto q_points = hp_no_values->get_present_fe_values()
683 .get_quadrature_points(); // real qpoints
684 const auto &JxWs =
685 hp_no_values->get_present_fe_values().get_JxW_values();
686
687 std::transform(q_points.begin(),
688 q_points.end(),
689 std::back_inserter(vec_pts),
690 [&](const Point<spacedim> &p) { return p; });
691 std::transform(JxWs.begin(),
692 JxWs.end(),
693 std::back_inserter(vec_JxWs),
694 [&](const double w) { return w; });
695 }
696 }
697
698 // Map back each point in real space by using the map associated to the
699 // bounding box.
700 std::vector<Point<dim>> unit_points(vec_pts.size());
701 const auto &bbox =
702 bboxes[master2polygon.at(master_cell->active_cell_index())];
703 unit_points.reserve(vec_pts.size());
704
705 for (unsigned int i = 0; i < vec_pts.size(); i++)
706 unit_points[i] = bbox.real_to_unit(vec_pts[i]);
707
708 return Quadrature<dim>(unit_points, vec_JxWs);
709}
710
711
712
713template <int dim, int spacedim>
714void
716{
717 Assert(agglo_dh.get_triangulation().n_cells() > 0,
718 ExcMessage(
719 "Triangulation must not be empty upon calling this function."));
721 ExcMessage("No agglomeration has been performed."));
722
723 agglo_dh.distribute_dofs(fe_collection);
724 // euler_mapping = std::make_unique<
725 // MappingFEField<dim, spacedim,
726 // LinearAlgebra::distributed::Vector<double>>>( euler_dh, euler_vector);
727}
728
729
730
731template <int dim, int spacedim>
734 const AgglomerationIterator<dim, spacedim> &polytope) const
735{
736 // Assert(euler_mapping,
737 // ExcMessage("The mapping describing the physical element stemming
738 // from "
739 // "agglomeration has not been set up."));
740
741 const auto &deal_cell = polytope->as_dof_handler_iterator(agglo_dh);
742
743 // First check if the polytope is made just by a single cell. If so, use
744 // classical FEValues
745 // if (polytope->n_background_cells() == 1)
746 // return standard_scratch->reinit(deal_cell);
747
748 const auto &agglo_cells = polytope->get_agglomerate();
749
750 Quadrature<dim> agglo_quad = agglomerated_quadrature(agglo_cells, deal_cell);
751
752 if (!is_hp_collection)
753 {
754 // Original version: handle case without hp::FECollection
755 agglomerated_scratch = std::make_unique<ScratchData>(*box_mapping,
756 fe_collection[0],
757 agglo_quad,
759 }
760 else
761 {
762 // Handle the hp::FECollection case
763 agglomerated_scratch = std::make_unique<ScratchData>(*box_mapping,
764 polytope->get_fe(),
765 agglo_quad,
767 }
768 return agglomerated_scratch->reinit(deal_cell);
769}
770
771
772
773template <int dim, int spacedim>
777 const unsigned int face_index,
779 &agglo_isv_ptr) const
780{
782 reinit_master(cell, face_index, agglo_isv_ptr, *this);
783}
784
785
786
787template <int dim, int spacedim>
791 const unsigned int face_index) const
792{
793 // Assert(euler_mapping,
794 // ExcMessage("The mapping describing the physical element stemming
795 // from "
796 // "agglomeration has not been set up."));
797
798 const auto &deal_cell = polytope->as_dof_handler_iterator(agglo_dh);
799 Assert(is_master_cell(deal_cell), ExcMessage("This should be true."));
800
802 reinit_master(deal_cell, face_index, agglomerated_isv_bdary, *this);
803}
804
805
806
807template <int dim, int spacedim>
808std::pair<const FEValuesBase<dim, spacedim> &,
811 const AgglomerationIterator<dim, spacedim> &polytope_in,
812 const AgglomerationIterator<dim, spacedim> &neigh_polytope,
813 const unsigned int local_in,
814 const unsigned int local_neigh) const
815{
816 // If current and neighboring polytopes are both locally owned, then compute
817 // the jump in the classical way without needing information about ghosted
818 // entities.
819 if (polytope_in->is_locally_owned() && neigh_polytope->is_locally_owned())
820 {
821 const auto &cell_in = polytope_in->as_dof_handler_iterator(agglo_dh);
822 const auto &neigh_cell =
823 neigh_polytope->as_dof_handler_iterator(agglo_dh);
824
825 const auto &fe_in =
827 reinit_master(cell_in, local_in, agglomerated_isv, *this);
828 const auto &fe_out =
830 reinit_master(neigh_cell, local_neigh, agglomerated_isv_neigh, *this);
831 std::pair<const FEValuesBase<dim, spacedim> &,
833 my_p(fe_in, fe_out);
834
835 return my_p;
836 }
837 else
838 {
839 Assert((polytope_in->is_locally_owned() &&
840 !neigh_polytope->is_locally_owned()),
841 ExcInternalError());
842
843 const auto &cell = polytope_in->as_dof_handler_iterator(agglo_dh);
844 const auto &bbox = bboxes[master2polygon.at(cell->active_cell_index())];
845 // const double bbox_measure = bbox.volume();
846
847 const unsigned int neigh_rank = neigh_polytope->subdomain_id();
848 const CellId &neigh_id = neigh_polytope->id();
849
850 // Retrieve qpoints,JxWs, normals sent previously from the neighboring
851 // rank.
852 std::vector<Point<spacedim>> &real_qpoints =
853 recv_qpoints.at(neigh_rank).at({neigh_id, local_neigh});
854
855 const auto &JxWs = recv_jxws.at(neigh_rank).at({neigh_id, local_neigh});
856
857 std::vector<Tensor<1, spacedim>> &normals =
858 recv_normals.at(neigh_rank).at({neigh_id, local_neigh});
859
860 // Apply the necessary scalings due to the bbox.
861 std::vector<Point<spacedim>> final_unit_q_points;
862 std::transform(real_qpoints.begin(),
863 real_qpoints.end(),
864 std::back_inserter(final_unit_q_points),
865 [&](const Point<spacedim> &p) {
866 return bbox.real_to_unit(p);
867 });
868
869 // std::vector<double> scale_factors(final_unit_q_points.size());
870 // std::vector<double> scaled_weights(final_unit_q_points.size());
871 // std::vector<Tensor<1, dim>> scaled_normals(final_unit_q_points.size());
872
873 // Since we received normal vectors from a neighbor, we have to swap
874 // the
875 // // sign of the vector in order to have outward normals.
876 // for (unsigned int q = 0; q < final_unit_q_points.size(); ++q)
877 // {
878 // for (unsigned int direction = 0; direction < spacedim; ++direction)
879 // scaled_normals[q][direction] =
880 // normals[q][direction] * (bbox.side_length(direction));
881
882 // scaled_normals[q] *= -1;
883
884 // scaled_weights[q] =
885 // (JxWs[q] * scaled_normals[q].norm()) / bbox_measure;
886 // scaled_normals[q] /= scaled_normals[q].norm();
887 // }
888 for (unsigned int q = 0; q < final_unit_q_points.size(); ++q)
889 normals[q] *= -1;
890
891
893 final_unit_q_points, JxWs, normals);
894
896 std::make_unique<NonMatching::FEImmersedSurfaceValues<spacedim>>(
897 *box_mapping, *fe, surface_quad, agglomeration_face_flags);
898
899
900 agglomerated_isv->reinit(cell);
901
902 std::pair<const FEValuesBase<dim, spacedim> &,
905
906 return my_p;
907 }
908}
909
910
911
912template <int dim, int spacedim>
913template <typename SparsityPatternType, typename Number>
914void
916 SparsityPatternType &dsp,
917 const AffineConstraints<Number> &constraints,
918 const bool keep_constrained_dofs,
919 const types::subdomain_id subdomain_id)
920{
922 ExcMessage("The agglomeration has not been set up correctly."));
923 Assert(dsp.empty(),
924 ExcMessage(
925 "The Sparsity pattern must be empty upon calling this function."));
926
927 const IndexSet &locally_owned_dofs = agglo_dh.locally_owned_dofs();
928 const IndexSet locally_relevant_dofs =
930
931 if constexpr (std::is_same_v<SparsityPatternType, DynamicSparsityPattern>)
932 dsp.reinit(locally_owned_dofs.size(),
933 locally_owned_dofs.size(),
934 locally_relevant_dofs);
935 else if constexpr (std::is_same_v<SparsityPatternType,
937 dsp.reinit(locally_owned_dofs, communicator);
938 else
939 AssertThrow(false, ExcNotImplemented());
940
941 // Create the sparsity pattern corresponding only to volumetric terms. The
942 // fluxes needed by DG methods will be filled later.
944 agglo_dh, dsp, constraints, keep_constrained_dofs, subdomain_id);
945
946
947 if (!is_hp_collection)
948 {
949 // Original version: handle case without hp::FECollection
950 const unsigned int dofs_per_cell = agglo_dh.get_fe(0).n_dofs_per_cell();
951 std::vector<types::global_dof_index> current_dof_indices(dofs_per_cell);
952 std::vector<types::global_dof_index> neighbor_dof_indices(dofs_per_cell);
953
954 // Loop over all locally owned polytopes, find the neighbor (also ghosted)
955 // and add fluxes to the sparsity pattern.
956 for (const auto &polytope : polytope_iterators())
957 {
958 if (polytope->is_locally_owned())
959 {
960 const unsigned int n_current_faces = polytope->n_faces();
961 polytope->get_dof_indices(current_dof_indices);
962 for (unsigned int f = 0; f < n_current_faces; ++f)
963 {
964 const auto &neigh_polytope = polytope->neighbor(f);
965 if (neigh_polytope.state() == IteratorState::valid)
966 {
967 neigh_polytope->get_dof_indices(neighbor_dof_indices);
968 constraints.add_entries_local_to_global(
969 current_dof_indices,
970 neighbor_dof_indices,
971 dsp,
972 keep_constrained_dofs,
973 {});
974 }
975 }
976 }
977 }
978 }
979 else
980 {
981 // Handle the hp::FECollection case
982
983 // Loop over all locally owned polytopes, find the neighbor (also ghosted)
984 // and add fluxes to the sparsity pattern.
985 for (const auto &polytope : polytope_iterators())
986 {
987 if (polytope->is_locally_owned())
988 {
989 const unsigned int current_dofs_per_cell =
990 polytope->get_fe().dofs_per_cell;
991 std::vector<types::global_dof_index> current_dof_indices(
992 current_dofs_per_cell);
993
994 const unsigned int n_current_faces = polytope->n_faces();
995 polytope->get_dof_indices(current_dof_indices);
996 for (unsigned int f = 0; f < n_current_faces; ++f)
997 {
998 const auto &neigh_polytope = polytope->neighbor(f);
999 if (neigh_polytope.state() == IteratorState::valid)
1000 {
1001 const unsigned int neighbor_dofs_per_cell =
1002 neigh_polytope->get_fe().dofs_per_cell;
1003 std::vector<types::global_dof_index> neighbor_dof_indices(
1004 neighbor_dofs_per_cell);
1005
1006 neigh_polytope->get_dof_indices(neighbor_dof_indices);
1007 constraints.add_entries_local_to_global(
1008 current_dof_indices,
1009 neighbor_dof_indices,
1010 dsp,
1011 keep_constrained_dofs,
1012 {});
1013 }
1014 }
1015 }
1016 }
1017 }
1018
1019
1020
1021 if constexpr (std::is_same_v<SparsityPatternType,
1023 dsp.compress();
1024}
1025
1026
1027
1028template <int dim, int spacedim>
1029void
1031{
1032 [[maybe_unused]] const auto parallel_triangulation =
1033 dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(&*tria);
1034 Assert(parallel_triangulation != nullptr, ExcInternalError());
1035
1036 const unsigned int n_dofs_per_cell = fe->dofs_per_cell;
1037 std::vector<types::global_dof_index> global_dof_indices(n_dofs_per_cell);
1038 for (const auto &polytope : polytope_iterators())
1039 if (polytope->is_locally_owned())
1040 {
1041 const CellId &master_cell_id = polytope->id();
1042
1043 const auto polytope_dh = polytope->as_dof_handler_iterator(agglo_dh);
1044 polytope_dh->get_dof_indices(global_dof_indices);
1045
1046
1047 const auto &agglomerate = polytope->get_agglomerate();
1048
1049 for (const auto &cell : agglomerate)
1050 {
1051 // interior, locally owned, cell
1052 for (const auto &f : cell->face_indices())
1053 {
1054 if (!cell->at_boundary(f))
1055 {
1056 const auto &neighbor = cell->neighbor(f);
1057 if (neighbor->is_ghost())
1058 {
1059 // key of the map: the rank to which send the data
1060 const types::subdomain_id neigh_rank =
1061 neighbor->subdomain_id();
1062
1063 // inform the "standard" neighbor about the neighboring
1064 // id and its master cell
1065 local_cell_ids_neigh_cell[neigh_rank].emplace(
1066 cell->id(), master_cell_id);
1067
1068 // inform the neighboring rank that this master cell
1069 // (hence polytope) has the following DoF indices
1070 local_ghost_dofs[neigh_rank].emplace(
1071 master_cell_id, global_dof_indices);
1072
1073 // ...same for bounding boxes
1074 const auto &bbox = bboxes[polytope->index()];
1075 local_ghosted_bbox[neigh_rank].emplace(master_cell_id,
1076 bbox);
1077 }
1078 }
1079 }
1080 }
1081 }
1082
1083 recv_cell_ids_neigh_cell =
1084 Utilities::MPI::some_to_some(communicator, local_cell_ids_neigh_cell);
1085
1086 // Exchange with neighboring ranks the neighboring bounding boxes
1087 recv_ghosted_bbox =
1088 Utilities::MPI::some_to_some(communicator, local_ghosted_bbox);
1089
1090 // Exchange with neighboring ranks the neighboring ghosted DoFs
1091 recv_ghost_dofs =
1092 Utilities::MPI::some_to_some(communicator, local_ghost_dofs);
1093}
1094
1095
1096
1097namespace dealii
1098{
1099 namespace internal
1100 {
1101 template <int dim, int spacedim>
1103 {
1104 public:
1105 static const FEValuesBase<dim, spacedim> &
1108 const unsigned int face_index,
1110 &agglo_isv_ptr,
1112 {
1113 Assert(handler.is_master_cell(cell),
1114 ExcMessage("This cell must be a master one."));
1115
1116 AgglomerationIterator<dim, spacedim> it{cell, &handler};
1117 const auto &neigh_polytope = it->neighbor(face_index);
1118
1119 const CellId polytope_in_id = cell->id();
1120
1121 // Retrieve the bounding box of the agglomeration
1122 const auto &bbox =
1123 handler.bboxes[handler.master2polygon.at(cell->active_cell_index())];
1124
1125 CellId polytope_out_id;
1126 if (neigh_polytope.state() == IteratorState::valid)
1127 polytope_out_id = neigh_polytope->id();
1128 else
1129 polytope_out_id = polytope_in_id; // on the boundary. Same id
1130
1131 const auto &common_face = handler.polytope_cache.interface.at(
1132 {polytope_in_id, polytope_out_id});
1133
1134 std::vector<Point<spacedim>> final_unit_q_points;
1135 std::vector<double> final_weights;
1136 std::vector<Tensor<1, dim>> final_normals;
1137
1138 if (!handler.is_hp_collection)
1139 {
1140 // Original version: handle case without hp::FECollection
1141 const unsigned int expected_qpoints =
1142 common_face.size() * handler.agglomeration_face_quad.size();
1143 final_unit_q_points.reserve(expected_qpoints);
1144 final_weights.reserve(expected_qpoints);
1145 final_normals.reserve(expected_qpoints);
1146
1147
1148 for (const auto &[deal_cell, local_face_idx] : common_face)
1149 {
1150 handler.no_face_values->reinit(deal_cell, local_face_idx);
1151
1152 const auto &q_points =
1153 handler.no_face_values->get_quadrature_points();
1154 const auto &JxWs = handler.no_face_values->get_JxW_values();
1155 const auto &normals =
1156 handler.no_face_values->get_normal_vectors();
1157
1158 const unsigned int n_qpoints_agglo = q_points.size();
1159
1160 for (unsigned int q = 0; q < n_qpoints_agglo; ++q)
1161 {
1162 final_unit_q_points.push_back(
1163 bbox.real_to_unit(q_points[q]));
1164 final_weights.push_back(JxWs[q]);
1165 final_normals.push_back(normals[q]);
1166 }
1167 }
1168 }
1169 else
1170 {
1171 // Handle the hp::FECollection case
1172 unsigned int higher_order_quad_index = cell->active_fe_index();
1173 if (neigh_polytope.state() == IteratorState::valid)
1174 if (handler
1175 .agglomeration_face_quad_collection[cell->active_fe_index()]
1176 .size() <
1177 handler
1179 ->active_fe_index()]
1180 .size())
1181 higher_order_quad_index = neigh_polytope->active_fe_index();
1182
1183 const unsigned int expected_qpoints =
1184 common_face.size() *
1185 handler
1186 .agglomeration_face_quad_collection[higher_order_quad_index]
1187 .size();
1188 final_unit_q_points.reserve(expected_qpoints);
1189 final_weights.reserve(expected_qpoints);
1190 final_normals.reserve(expected_qpoints);
1191
1192 for (const auto &[deal_cell, local_face_idx] : common_face)
1193 {
1194 handler.hp_no_face_values->reinit(
1195 deal_cell, local_face_idx, higher_order_quad_index, 0, 0);
1196
1197 const auto &q_points =
1198 handler.hp_no_face_values->get_present_fe_values()
1199 .get_quadrature_points();
1200 const auto &JxWs =
1201 handler.hp_no_face_values->get_present_fe_values()
1202 .get_JxW_values();
1203 const auto &normals =
1204 handler.hp_no_face_values->get_present_fe_values()
1205 .get_normal_vectors();
1206
1207 const unsigned int n_qpoints_agglo = q_points.size();
1208
1209 for (unsigned int q = 0; q < n_qpoints_agglo; ++q)
1210 {
1211 final_unit_q_points.push_back(
1212 bbox.real_to_unit(q_points[q]));
1213 final_weights.push_back(JxWs[q]);
1214 final_normals.push_back(normals[q]);
1215 }
1216 }
1217 }
1218
1219
1221 final_unit_q_points, final_weights, final_normals);
1222
1223 if (!handler.is_hp_collection)
1224 {
1225 agglo_isv_ptr =
1226 std::make_unique<NonMatching::FEImmersedSurfaceValues<spacedim>>(
1227 *(handler.box_mapping),
1228 *(handler.fe),
1229 surface_quad,
1230 handler.agglomeration_face_flags);
1231 }
1232 else
1233 {
1234 agglo_isv_ptr =
1235 std::make_unique<NonMatching::FEImmersedSurfaceValues<spacedim>>(
1236 *(handler.box_mapping),
1237 cell->get_fe(),
1238 surface_quad,
1239 handler.agglomeration_face_flags);
1240 }
1241
1242 agglo_isv_ptr->reinit(cell);
1243
1244 return *agglo_isv_ptr;
1245 }
1246
1247
1248
1255 static void
1258 &master_cell,
1260 {
1261 Assert(
1262 handler.master_slave_relationships[master_cell
1263 ->global_active_cell_index()] ==
1264 -1,
1265 ExcMessage("The present cell with index " +
1266 std::to_string(master_cell->global_active_cell_index()) +
1267 "is not a master one."));
1268
1269 const auto &agglomeration = handler.get_agglomerate(master_cell);
1270 const types::global_cell_index current_polytope_index =
1271 handler.master2polygon.at(master_cell->active_cell_index());
1272
1273 CellId current_polytope_id = master_cell->id();
1274
1275
1276 std::set<types::global_cell_index> visited_polygonal_neighbors;
1277
1278 std::map<unsigned int, CellId> face_to_neigh_id;
1279
1280 std::map<unsigned int, bool> is_face_at_boundary;
1281
1282 // same as above, but with CellId
1283 std::set<CellId> visited_polygonal_neighbors_id;
1284 unsigned int ghost_counter = 0;
1285
1286 for (const auto &cell : agglomeration)
1287 {
1289 cell->active_cell_index();
1290
1291 const CellId cell_id = cell->id();
1292
1293 for (const auto f : cell->face_indices())
1294 {
1295 const auto &neighboring_cell = cell->neighbor(f);
1296
1297 const bool valid_neighbor =
1298 neighboring_cell.state() == IteratorState::valid;
1299
1300 if (valid_neighbor)
1301 {
1302 if (neighboring_cell->is_locally_owned() &&
1303 !handler.are_cells_agglomerated(cell, neighboring_cell))
1304 {
1305 // - cell is not on the boundary,
1306 // - it's not agglomerated with the neighbor. If so,
1307 // it's a neighbor of the present agglomeration
1308 // std::cout << " (from rank) "
1309 // << Utilities::MPI::this_mpi_process(
1310 // handler.communicator)
1311 // << std::endl;
1312
1313 // std::cout
1314 // << "neighbor locally owned? " << std::boolalpha
1315 // << neighboring_cell->is_locally_owned() <<
1316 // std::endl;
1317 // if (neighboring_cell->is_ghost())
1318 // handler.ghosted_indices.push_back(
1319 // neighboring_cell->active_cell_index());
1320
1321 // a new face of the agglomeration has been
1322 // discovered.
1323 handler.polygon_boundary[master_cell].push_back(
1324 cell->face(f));
1325
1326 // global index of neighboring deal.II cell
1327 const types::global_cell_index neighboring_cell_index =
1328 neighboring_cell->active_cell_index();
1329
1330 // master cell for the neighboring polytope
1331 const auto &master_of_neighbor =
1333 neighboring_cell_index);
1334
1335 const auto nof = cell->neighbor_of_neighbor(f);
1336
1337 if (handler.is_slave_cell(neighboring_cell))
1338 {
1339 // index of the neighboring polytope
1341 neighbor_polytope_index =
1342 handler.master2polygon.at(
1343 master_of_neighbor->active_cell_index());
1344
1345 CellId neighbor_polytope_id =
1346 master_of_neighbor->id();
1347
1348 if (visited_polygonal_neighbors.find(
1349 neighbor_polytope_index) ==
1350 std::end(visited_polygonal_neighbors))
1351 {
1352 // found a neighbor
1353
1354 const unsigned int n_face =
1356 [current_polytope_index];
1357
1358 handler.polytope_cache.cell_face_at_boundary[{
1359 current_polytope_index, n_face}] = {
1360 false, master_of_neighbor};
1361
1362 is_face_at_boundary[n_face] = true;
1363
1365 [current_polytope_index];
1366
1367 visited_polygonal_neighbors.insert(
1368 neighbor_polytope_index);
1369 }
1370
1371
1372 if (handler.polytope_cache.visited_cell_and_faces
1373 .find({cell_index, f}) ==
1374 std::end(handler.polytope_cache
1375 .visited_cell_and_faces))
1376 {
1377 handler.polytope_cache
1378 .interface[{
1379 current_polytope_id, neighbor_polytope_id}]
1380 .emplace_back(cell, f);
1381
1382 handler.polytope_cache.visited_cell_and_faces
1383 .insert({cell_index, f});
1384 }
1385
1386
1387 if (handler.polytope_cache.visited_cell_and_faces
1388 .find({neighboring_cell_index, nof}) ==
1389 std::end(handler.polytope_cache
1390 .visited_cell_and_faces))
1391 {
1392 handler.polytope_cache
1393 .interface[{
1394 neighbor_polytope_id, current_polytope_id}]
1395 .emplace_back(neighboring_cell, nof);
1396
1397 handler.polytope_cache.visited_cell_and_faces
1398 .insert({neighboring_cell_index, nof});
1399 }
1400 }
1401 else
1402 {
1403 // neighboring cell is a master
1404
1405 // save the pair of neighboring cells
1407 neighbor_polytope_index =
1408 handler.master2polygon.at(
1409 neighboring_cell_index);
1410
1411 CellId neighbor_polytope_id =
1412 neighboring_cell->id();
1413
1414 if (visited_polygonal_neighbors.find(
1415 neighbor_polytope_index) ==
1416 std::end(visited_polygonal_neighbors))
1417 {
1418 // found a neighbor
1419 const unsigned int n_face =
1421 [current_polytope_index];
1422
1423
1424 handler.polytope_cache.cell_face_at_boundary[{
1425 current_polytope_index, n_face}] = {
1426 false, neighboring_cell};
1427
1428 is_face_at_boundary[n_face] = true;
1429
1431 [current_polytope_index];
1432
1433 visited_polygonal_neighbors.insert(
1434 neighbor_polytope_index);
1435 }
1436
1437
1438
1439 if (handler.polytope_cache.visited_cell_and_faces
1440 .find({cell_index, f}) ==
1441 std::end(handler.polytope_cache
1442 .visited_cell_and_faces))
1443 {
1444 handler.polytope_cache
1445 .interface[{
1446 current_polytope_id, neighbor_polytope_id}]
1447 .emplace_back(cell, f);
1448
1449 handler.polytope_cache.visited_cell_and_faces
1450 .insert({cell_index, f});
1451 }
1452
1453 if (handler.polytope_cache.visited_cell_and_faces
1454 .find({neighboring_cell_index, nof}) ==
1455 std::end(handler.polytope_cache
1456 .visited_cell_and_faces))
1457 {
1458 handler.polytope_cache
1459 .interface[{
1460 neighbor_polytope_id, current_polytope_id}]
1461 .emplace_back(neighboring_cell, nof);
1462
1463 handler.polytope_cache.visited_cell_and_faces
1464 .insert({neighboring_cell_index, nof});
1465 }
1466 }
1467 }
1468 else if (neighboring_cell->is_ghost())
1469 {
1470 const auto nof = cell->neighbor_of_neighbor(f);
1471
1472 // from neighboring rank,receive the association
1473 // between standard cell ids and neighboring polytope.
1474 // This tells to the current rank that the
1475 // neighboring cell has the following CellId as master
1476 // cell.
1477 const auto &check_neigh_poly_ids =
1478 handler.recv_cell_ids_neigh_cell.at(
1479 neighboring_cell->subdomain_id());
1480
1481 const CellId neighboring_cell_id =
1482 neighboring_cell->id();
1483
1484 const CellId &check_neigh_polytope_id =
1485 check_neigh_poly_ids.at(neighboring_cell_id);
1486
1487 // const auto master_index =
1488 // master_indices[ghost_counter];
1489
1490 if (visited_polygonal_neighbors_id.find(
1491 check_neigh_polytope_id) ==
1492 std::end(visited_polygonal_neighbors_id))
1493 {
1494 handler.polytope_cache.cell_face_at_boundary[{
1495 current_polytope_index,
1497 [current_polytope_index]}] = {false,
1498 neighboring_cell};
1499
1500
1501 // record the cell id of the neighboring polytope
1502 handler.polytope_cache.ghosted_master_id[{
1503 current_polytope_id,
1505 [current_polytope_index]}] =
1506 check_neigh_polytope_id;
1507
1508
1509 const unsigned int n_face =
1511 [current_polytope_index];
1512
1513 face_to_neigh_id[n_face] = check_neigh_polytope_id;
1514
1515 is_face_at_boundary[n_face] = false;
1516
1517
1518 // increment number of faces
1520 [current_polytope_index];
1521
1522 visited_polygonal_neighbors_id.insert(
1523 check_neigh_polytope_id);
1524
1525 // ghosted polytope has been found, increment
1526 // ghost counter
1527 ++ghost_counter;
1528 }
1529
1530
1531
1532 if (handler.polytope_cache.visited_cell_and_faces_id
1533 .find({cell_id, f}) ==
1534 std::end(
1535 handler.polytope_cache.visited_cell_and_faces_id))
1536 {
1537 handler.polytope_cache
1538 .interface[{
1539 current_polytope_id, check_neigh_polytope_id}]
1540 .emplace_back(cell, f);
1541
1542 // std::cout << "ADDED ("
1543 // << cell->active_cell_index() << ")
1544 // BETWEEN "
1545 // << current_polytope_id << " e "
1546 // << check_neigh_polytope_id <<
1547 // std::endl;
1548
1549 handler.polytope_cache.visited_cell_and_faces_id
1550 .insert({cell_id, f});
1551 }
1552
1553
1554 if (handler.polytope_cache.visited_cell_and_faces_id
1555 .find({neighboring_cell_id, nof}) ==
1556 std::end(
1557 handler.polytope_cache.visited_cell_and_faces_id))
1558 {
1559 handler.polytope_cache
1560 .interface[{
1561 check_neigh_polytope_id, current_polytope_id}]
1562 .emplace_back(neighboring_cell, nof);
1563
1564 handler.polytope_cache.visited_cell_and_faces_id
1565 .insert({neighboring_cell_id, nof});
1566 }
1567 }
1568 }
1569 else if (cell->face(f)->at_boundary())
1570 {
1571 // Boundary face of a boundary cell.
1572 // Note that the neighboring cell must be invalid.
1573
1574 handler.polygon_boundary[master_cell].push_back(
1575 cell->face(f));
1576
1577 if (visited_polygonal_neighbors.find(
1578 std::numeric_limits<unsigned int>::max()) ==
1579 std::end(visited_polygonal_neighbors))
1580 {
1581 // boundary face. Notice that `neighboring_cell` is
1582 // invalid here.
1583 handler.polytope_cache.cell_face_at_boundary[{
1584 current_polytope_index,
1586 [current_polytope_index]}] = {true,
1587 neighboring_cell};
1588
1589 const unsigned int n_face =
1591 [current_polytope_index];
1592
1593 is_face_at_boundary[n_face] = true;
1594
1596 [current_polytope_index];
1597
1598 visited_polygonal_neighbors.insert(
1599 std::numeric_limits<unsigned int>::max());
1600 }
1601
1602
1603
1604 if (handler.polytope_cache.visited_cell_and_faces.find(
1605 {cell_index, f}) ==
1606 std::end(handler.polytope_cache.visited_cell_and_faces))
1607 {
1608 handler.polytope_cache
1609 .interface[{
1610 current_polytope_id, current_polytope_id}]
1611 .emplace_back(cell, f);
1612
1613 handler.polytope_cache.visited_cell_and_faces.insert(
1614 {cell_index, f});
1615 }
1616 }
1617 } // loop over faces
1618 } // loop over all cells of agglomerate
1619
1620
1621
1622 if (ghost_counter > 0)
1623 {
1624 const auto parallel_triangulation = dynamic_cast<
1625 const dealii::parallel::TriangulationBase<dim, spacedim> *>(
1626 &(*handler.tria));
1627
1628 const unsigned int n_faces_current_poly =
1629 handler.number_of_agglomerated_faces[current_polytope_index];
1630
1631 // Communicate to neighboring ranks that current_polytope_id has
1632 // a number of faces equal to n_faces_current_poly faces:
1633 // current_polytope_id -> n_faces_current_poly
1634 for (const unsigned int neigh_rank :
1635 parallel_triangulation->ghost_owners())
1636 {
1637 handler.local_n_faces[neigh_rank].emplace(current_polytope_id,
1638 n_faces_current_poly);
1639
1640 handler.local_bdary_info[neigh_rank].emplace(
1641 current_polytope_id, is_face_at_boundary);
1642
1643 handler.local_ghosted_master_id[neigh_rank].emplace(
1644 current_polytope_id, face_to_neigh_id);
1645 }
1646 }
1647 }
1648 };
1649
1650
1651
1652 } // namespace internal
1653} // namespace dealii
1654
1655
1656
1657template class AgglomerationHandler<1>;
1658template void
1660 DynamicSparsityPattern &sparsity_pattern,
1661 const AffineConstraints<double> &constraints,
1662 const bool keep_constrained_dofs,
1663 const types::subdomain_id subdomain_id);
1664
1665template void
1667 TrilinosWrappers::SparsityPattern &sparsity_pattern,
1668 const AffineConstraints<double> &constraints,
1669 const bool keep_constrained_dofs,
1670 const types::subdomain_id subdomain_id);
1671
1672template class AgglomerationHandler<2>;
1673template void
1675 DynamicSparsityPattern &sparsity_pattern,
1676 const AffineConstraints<double> &constraints,
1677 const bool keep_constrained_dofs,
1678 const types::subdomain_id subdomain_id);
1679
1680template void
1682 TrilinosWrappers::SparsityPattern &sparsity_pattern,
1683 const AffineConstraints<double> &constraints,
1684 const bool keep_constrained_dofs,
1685 const types::subdomain_id subdomain_id);
1686
1687template class AgglomerationHandler<3>;
1688template void
1690 DynamicSparsityPattern &sparsity_pattern,
1691 const AffineConstraints<double> &constraints,
1692 const bool keep_constrained_dofs,
1693 const types::subdomain_id subdomain_id);
1694
1695template void
1697 TrilinosWrappers::SparsityPattern &sparsity_pattern,
1698 const AffineConstraints<double> &constraints,
1699 const bool keep_constrained_dofs,
1700 const types::subdomain_id subdomain_id);
void add_entries_local_to_global(const std::vector< size_type > &local_dof_indices, SparsityPatternBase &sparsity_pattern, const bool keep_constrained_entries=true, const Table< 2, bool > &dof_mask=Table< 2, bool >()) const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
const unsigned int degree
size_type size() const
unsigned int size() const
AgglomerationContainer get_agglomerate() const
const AgglomerationIterator< dim, spacedim > neighbor(const unsigned int f) const
const FiniteElement< dim, spacedim > & get_fe() const
DoFHandler< dim, spacedim >::active_cell_iterator as_dof_handler_iterator(const DoFHandler< dim, spacedim > &dof_handler) const
types::subdomain_id subdomain_id() const
unsigned int n_dofs_per_cell() const noexcept
AgglomerationHandler()=default
LinearAlgebra::distributed::Vector< float > master_slave_relationships
const FEValues< dim, spacedim > & reinit(const AgglomerationIterator< dim, spacedim > &polytope) const
FE_Nothing< dim, spacedim > dummy_fe
std::unique_ptr< NonMatching::FEImmersedSurfaceValues< spacedim > > agglomerated_isv_bdary
std::unique_ptr< hp::FEValues< dim, spacedim > > hp_no_values
bool is_slave_cell(const CellIterator &cell) const
std::unique_ptr< NonMatching::FEImmersedSurfaceValues< spacedim > > agglomerated_isv
std::unique_ptr< ScratchData > standard_scratch
Quadrature< dim > agglomeration_quad
typename AgglomerationIterator< dim, spacedim >::AgglomerationContainer AgglomerationContainer
std::map< types::subdomain_id, std::map< std::pair< CellId, unsigned int >, std::vector< double > > > recv_jxws
std::map< types::subdomain_id, std::map< std::pair< CellId, unsigned int >, std::vector< Tensor< 1, spacedim > > > > recv_normals
Quadrature< dim - 1 > agglomeration_face_quad
std::unique_ptr< FEValues< dim, spacedim > > no_values
std::unique_ptr< hp::FEFaceValues< dim, spacedim > > hp_no_face_values
bool are_cells_agglomerated(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell, const typename Triangulation< dim, spacedim >::active_cell_iterator &other_cell) const
Quadrature< dim > agglomerated_quadrature(const AgglomerationContainer &cells, const typename Triangulation< dim, spacedim >::active_cell_iterator &master_cell) const
AgglomerationIterator< dim, spacedim > agglomeration_iterator
hp::QCollection< dim - 1 > agglomeration_face_quad_collection
std::map< const typename Triangulation< dim, spacedim >::active_cell_iterator, std::vector< typename Triangulation< dim >::active_face_iterator > > polygon_boundary
std::unordered_map< types::global_cell_index, std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > > master2slaves
void create_bounding_box(const AgglomerationContainer &polytope)
std::map< types::global_cell_index, types::global_cell_index > master2polygon
std::map< types::subdomain_id, std::map< std::pair< CellId, unsigned int >, std::vector< Point< spacedim > > > > recv_qpoints
std::pair< const FEValuesBase< dim, spacedim > &, const FEValuesBase< dim, spacedim > & > reinit_interface(const AgglomerationIterator< dim, spacedim > &polytope_in, const AgglomerationIterator< dim, spacedim > &neigh_polytope, const unsigned int local_in, const unsigned int local_outside) const
std::map< types::subdomain_id, std::map< std::pair< CellId, unsigned int >, std::vector< std::vector< Tensor< 1, spacedim > > > > > recv_gradients
const Triangulation< dim, spacedim > & get_triangulation() const
void create_agglomeration_sparsity_pattern(SparsityPatternType &sparsity_pattern, const AffineConstraints< Number > &constraints=AffineConstraints< Number >(), const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
const Mapping< dim > & get_mapping() const
std::map< types::global_cell_index, typename Triangulation< dim, spacedim >::active_cell_iterator > master_slave_relationships_iterators
std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > get_agglomerate(const typename Triangulation< dim, spacedim >::active_cell_iterator &master_cell) const
unsigned int n_agglomerated_faces_per_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell) const
const UpdateFlags internal_agglomeration_face_flags
void distribute_agglomerated_dofs(const FiniteElement< dim > &fe_space)
hp::FECollection< dim, spacedim > dummy_fe_collection
std::vector< types::global_cell_index > number_of_agglomerated_faces
const UpdateFlags internal_agglomeration_flags
std::unique_ptr< FEFaceValues< dim, spacedim > > no_face_values
const FEValuesBase< dim, spacedim > & reinit_master(const typename DoFHandler< dim, spacedim >::active_cell_iterator &cell, const unsigned int face_number, std::unique_ptr< NonMatching::FEImmersedSurfaceValues< spacedim > > &agglo_isv_ptr) const
std::vector< typename Triangulation< dim, spacedim >::active_cell_iterator > master_cells_container
std::unique_ptr< hp::FECollection< dim, spacedim > > hp_fe_collection
hp::QCollection< dim > agglomeration_quad_collection
bool is_master_cell(const CellIterator &cell) const
IteratorRange< agglomeration_iterator > polytope_iterators() const
void initialize_agglomeration_data(const std::unique_ptr< GridTools::Cache< dim, spacedim > > &cache_tria)
agglomeration_iterator define_agglomerate(const AgglomerationContainer &cells)
void initialize_fe_values(const Quadrature< dim > &cell_quadrature=QGauss< dim >(1), const UpdateFlags &flags=UpdateFlags::update_default, const Quadrature< dim - 1 > &face_quadrature=QGauss< dim - 1 >(1), const UpdateFlags &face_flags=UpdateFlags::update_default)
std::unique_ptr< NonMatching::FEImmersedSurfaceValues< spacedim > > agglomerated_isv_neigh
void define_agglomerate_with_check(const AgglomerationContainer &cells)
std::unique_ptr< ScratchData > agglomerated_scratch
DoFHandler< dim, spacedim > agglo_dh
std::unique_ptr< MappingBox< dim > > box_mapping
internal::PolytopeCache< dim, spacedim > polytope_cache
hp::MappingCollection< dim > mapping_collection
static void setup_master_neighbor_connectivity(const typename Triangulation< dim, spacedim >::active_cell_iterator &master_cell, const AgglomerationHandler< dim, spacedim > &handler)
static const FEValuesBase< dim, spacedim > & reinit_master(const typename DoFHandler< dim, spacedim >::active_cell_iterator &cell, const unsigned int face_index, std::unique_ptr< NonMatching::FEImmersedSurfaceValues< spacedim > > &agglo_isv_ptr, const AgglomerationHandler< dim, spacedim > &handler)
unsigned int size() const
unsigned int cell_index
#define Assert(cond, exc)
#define AssertThrow(cond, exc)
TriaActiveIterator< CellAccessor< dim, spacedim > > active_cell_iterator
typename ActiveSelector::active_cell_iterator active_cell_iterator
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity_pattern, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
UpdateFlags
update_normal_vectors
update_JxW_values
update_quadrature_points
IndexSet extract_locally_relevant_dofs(const DoFHandler< dim, spacedim > &dof_handler)
Tensor< 2, dim, Number > w(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
std::map< unsigned int, T > some_to_some(const MPI_Comm comm, const std::map< unsigned int, T > &objects_to_send)
bool job_supports_mpi()
void create_graph_from_agglomerate(const std::vector< typename Triangulation< dim >::active_cell_iterator > &cells, Graph &g)
Definition utils.h:2142
void compute_connected_components(Graph &g, std::vector< std::vector< types::global_cell_index > > &connected_components)
Definition utils.h:2192
constexpr types::subdomain_id invalid_subdomain_id
unsigned int subdomain_id
unsigned int global_cell_index