43 namespace TriangulationImplementation
52 template <
int dim,
int spacedim>
59 &cell) -> std::uint8_t {
60 if (cell->refine_flag_set())
62 if (cell->coarsen_flag_set())
70 const std::uint8_t &flag) ->
void {
71 cell->clear_coarsen_flag();
72 cell->clear_refine_flag();
74 cell->set_refine_flag();
76 cell->set_coarsen_flag();
79 GridTools::exchange_cell_data_to_ghosts<std::uint8_t>(tria,
90#ifdef DEAL_II_WITH_P4EST
94 template <
int dim,
int spacedim>
96 get_vertex_to_cell_mappings(
98 std::vector<unsigned int> &vertex_touch_count,
99 std::vector<std::list<
101 unsigned int>>> &vertex_to_cell)
103 vertex_touch_count.resize(triangulation.
n_vertices());
104 vertex_to_cell.resize(triangulation.
n_vertices());
106 for (
const auto &cell : triangulation.active_cell_iterators())
109 ++vertex_touch_count[cell->vertex_index(v)];
110 vertex_to_cell[cell->vertex_index(v)].emplace_back(cell, v);
116 template <
int dim,
int spacedim>
118 get_edge_to_cell_mappings(
120 std::vector<unsigned int> &edge_touch_count,
121 std::vector<std::list<
123 unsigned int>>> &edge_to_cell)
130 for (
const auto &cell : triangulation.active_cell_iterators())
131 for (unsigned
int l = 0; l < GeometryInfo<dim>::lines_per_cell; ++
l)
133 ++edge_touch_count[cell->line(l)->index()];
134 edge_to_cell[cell->line(l)->index()].emplace_back(cell, l);
144 template <
int dim,
int spacedim>
146 set_vertex_and_cell_info(
148 const std::vector<unsigned int> &vertex_touch_count,
149 const std::vector<std::list<
151 unsigned int>>> &vertex_to_cell,
152 const std::vector<types::global_dof_index>
153 &coarse_cell_to_p4est_tree_permutation,
154 const bool set_vertex_info,
170 if (set_vertex_info ==
true)
171 for (
unsigned int v = 0; v < triangulation.
n_vertices(); ++v)
173 connectivity->vertices[3 * v] = triangulation.
get_vertices()[v][0];
174 connectivity->vertices[3 * v + 1] =
176 connectivity->vertices[3 * v + 2] =
177 (spacedim == 2 ? 0 : triangulation.
get_vertices()[v][2]);
187 endc = triangulation.
end();
188 for (; cell != endc; ++cell)
190 const unsigned int index =
191 coarse_cell_to_p4est_tree_permutation[cell->index()];
195 if (set_vertex_info ==
true)
198 v] = cell->vertex_index(v);
201 v] = cell->vertex_index(v);
207 if (cell->face(f)->at_boundary() == false)
210 coarse_cell_to_p4est_tree_permutation[cell->neighbor(f)->
index()];
214 coarse_cell_to_p4est_tree_permutation[cell->index()];
219 if (cell->face(f)->at_boundary() == false)
225 connectivity->tree_to_face
227 cell->neighbor_of_neighbor(f);
246 connectivity->tree_to_face[
index * 6 + f] =
247 cell->neighbor_of_neighbor(f);
249 unsigned int face_idx_list[2] = {
250 f, cell->neighbor_of_neighbor(f)};
252 cell_list[2] = {cell, cell->neighbor(f)};
253 unsigned int smaller_idx = 0;
255 if (f > cell->neighbor_of_neighbor(f))
258 unsigned int larger_idx = (smaller_idx + 1) % 2;
266 unsigned int g_idx = cell_list[smaller_idx]->vertex_index(
268 face_idx_list[smaller_idx],
270 cell_list[smaller_idx]->face_orientation(
271 face_idx_list[smaller_idx]),
272 cell_list[smaller_idx]->face_flip(
273 face_idx_list[smaller_idx]),
274 cell_list[smaller_idx]->face_rotation(
275 face_idx_list[smaller_idx])));
279 for (
unsigned int i = 0;
280 i < GeometryInfo<dim>::vertices_per_face;
284 cell_list[larger_idx]->vertex_index(
286 face_idx_list[larger_idx], i));
295 connectivity->tree_to_face[
index * 6 + f] += 6 * v;
309 connectivity->ctt_offset[0] = 0;
310 std::partial_sum(vertex_touch_count.begin(),
311 vertex_touch_count.end(),
312 &connectivity->ctt_offset[1]);
315 num_vtt = std::accumulate(vertex_touch_count.begin(),
316 vertex_touch_count.end(),
321 for (
unsigned int v = 0; v < triangulation.
n_vertices(); ++v)
323 Assert(vertex_to_cell[v].
size() == vertex_touch_count[v],
327 std::pair<typename Triangulation<dim, spacedim>::active_cell_iterator,
328 unsigned int>>::const_iterator p =
329 vertex_to_cell[v].begin();
330 for (
unsigned int c = 0; c < vertex_touch_count[v]; ++c, ++p)
332 connectivity->corner_to_tree[connectivity->ctt_offset[v] + c] =
333 coarse_cell_to_p4est_tree_permutation[p->first->index()];
334 connectivity->corner_to_corner[connectivity->ctt_offset[v] + c] =
342 template <
int dim,
int spacedim>
348 Assert(coarse_grid_cell < parallel_forest->connectivity->num_trees,
350 return ((coarse_grid_cell >= parallel_forest->first_local_tree) &&
351 (coarse_grid_cell <= parallel_forest->last_local_tree));
355 template <
int dim,
int spacedim>
357 delete_all_children_and_self(
360 if (cell->has_children())
361 for (
unsigned int c = 0; c < cell->n_children(); ++c)
362 delete_all_children_and_self<dim, spacedim>(cell->child(c));
364 cell->set_coarsen_flag();
369 template <
int dim,
int spacedim>
374 if (cell->has_children())
375 for (
unsigned int c = 0; c < cell->n_children(); ++c)
376 delete_all_children_and_self<dim, spacedim>(cell->child(c));
380 template <
int dim,
int spacedim>
382 determine_level_subdomain_id_recursively(
389 const std::vector<std::vector<bool>> &marked_vertices)
398 if (marked_vertices[dealii_cell->level()]
399 [dealii_cell->vertex_index(v)])
418 if (!used && dealii_cell->is_active() &&
419 dealii_cell->is_artificial() ==
false &&
420 dealii_cell->level() + 1 <
static_cast<int>(marked_vertices.size()))
424 if (marked_vertices[dealii_cell->level() + 1]
425 [dealii_cell->vertex_index(v)])
434 if (!used && dealii_cell->is_active() &&
435 dealii_cell->is_artificial() ==
false && dealii_cell->level() > 0)
439 if (marked_vertices[dealii_cell->level() - 1]
440 [dealii_cell->vertex_index(v)])
451 &forest, tree_index, &p4est_cell, my_subdomain);
452 Assert((owner != -2) && (owner != -1),
454 dealii_cell->set_level_subdomain_id(owner);
458 if (dealii_cell->has_children())
462 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
470 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
473 determine_level_subdomain_id_recursively<dim, spacedim>(
476 dealii_cell->child(c),
486 template <
int dim,
int spacedim>
488 match_tree_recursively(
496 if (sc_array_bsearch(
const_cast<sc_array_t *
>(&tree.quadrants),
502 delete_all_children<dim, spacedim>(dealii_cell);
503 if (dealii_cell->is_active())
504 dealii_cell->set_subdomain_id(my_subdomain);
513 if (dealii_cell->is_active())
514 dealii_cell->set_refine_flag();
519 for (
unsigned int c = 0;
520 c < GeometryInfo<dim>::max_children_per_cell;
527 for (
unsigned int c = 0;
528 c < GeometryInfo<dim>::max_children_per_cell;
533 &p4est_child[c]) ==
false)
539 delete_all_children<dim, spacedim>(dealii_cell->child(c));
540 dealii_cell->child(c)->recursively_set_subdomain_id(
547 match_tree_recursively<dim, spacedim>(tree,
548 dealii_cell->child(c),
558 template <
int dim,
int spacedim>
561 const ::Triangulation<dim, spacedim> *tria,
562 unsigned int dealii_index,
566 const int l = ghost_quadrant.level;
568 for (
int i = 0; i <
l; ++i)
573 if (cell->is_active())
575 cell->clear_coarsen_flag();
576 cell->set_refine_flag();
583 dealii_index = cell->child_index(child_id);
589 if (cell->has_children())
590 delete_all_children<dim, spacedim>(cell);
593 cell->clear_coarsen_flag();
594 cell->set_subdomain_id(ghost_owner);
599 class PartitionSearch
607 PartitionSearch(
const PartitionSearch<dim> &other) =
delete;
609 PartitionSearch<dim> &
610 operator=(
const PartitionSearch<dim> &other) =
delete;
666 quad_length_on_level);
669 initialize_mapping();
672 map_real_to_unit_cell(
const Point<dim> &p)
const;
675 is_in_this_quadrant(
const Point<dim> &p)
const;
678 std::vector<Point<dim>> cell_vertices;
686 bool are_vertices_initialized;
688 bool is_reference_mapping_initialized;
694 QuadrantData quadrant_data;
701 PartitionSearch<dim>::local_quadrant_fn(
714 PartitionSearch<dim> *this_object =
715 reinterpret_cast<PartitionSearch<dim> *
>(forest->user_pointer);
719 quad_length_on_level =
721 (dim == 2 ? P4EST_MAXLEVEL : P8EST_MAXLEVEL)) -
725 this_object->quadrant_data.set_cell_vertices(forest,
728 quad_length_on_level);
731 this_object->quadrant_data.initialize_mapping();
741 PartitionSearch<dim>::local_point_fn(
754 PartitionSearch<dim> *this_object =
755 reinterpret_cast<PartitionSearch<dim> *
>(forest->user_pointer);
758 double *this_point_dptr =
static_cast<double *
>(
point);
761 (dim == 2 ?
Point<dim>(this_point_dptr[0], this_point_dptr[1]) :
762 Point<dim>(this_point_dptr[0],
764 this_point_dptr[2]));
767 const bool is_in_this_quadrant =
768 this_object->quadrant_data.is_in_this_quadrant(this_point);
772 if (!is_in_this_quadrant)
781 if (rank_begin < rank_end)
789 this_point_dptr[dim] =
static_cast<double>(rank_begin);
799 PartitionSearch<dim>::QuadrantData::is_in_this_quadrant(
802 const Point<dim> p_ref = map_real_to_unit_cell(p);
811 PartitionSearch<dim>::QuadrantData::map_real_to_unit_cell(
814 Assert(is_reference_mapping_initialized,
816 "Cell vertices and mapping coefficients must be fully "
817 "initialized before transforming a point to the unit cell."));
823 for (
unsigned int alpha = 0;
824 alpha < GeometryInfo<dim>::vertices_per_cell;
830 p_out += (quadrant_mapping_matrix(alpha, 0) +
831 quadrant_mapping_matrix(alpha, 1) * p(0) +
832 quadrant_mapping_matrix(alpha, 2) * p(1) +
833 quadrant_mapping_matrix(alpha, 3) * p(0) * p(1)) *
839 for (
unsigned int alpha = 0;
840 alpha < GeometryInfo<dim>::vertices_per_cell;
846 p_out += (quadrant_mapping_matrix(alpha, 0) +
847 quadrant_mapping_matrix(alpha, 1) * p(0) +
848 quadrant_mapping_matrix(alpha, 2) * p(1) +
849 quadrant_mapping_matrix(alpha, 3) * p(2) +
850 quadrant_mapping_matrix(alpha, 4) * p(0) * p(1) +
851 quadrant_mapping_matrix(alpha, 5) * p(1) * p(2) +
852 quadrant_mapping_matrix(alpha, 6) * p(0) * p(2) +
853 quadrant_mapping_matrix(alpha, 7) * p(0) * p(1) * p(2)) *
863 PartitionSearch<dim>::QuadrantData::QuadrantData()
865 , quadrant_mapping_matrix(
GeometryInfo<dim>::vertices_per_cell,
867 , are_vertices_initialized(false)
868 , is_reference_mapping_initialized(false)
875 PartitionSearch<dim>::QuadrantData::initialize_mapping()
878 are_vertices_initialized,
880 "Cell vertices must be initialized before the cell mapping can be filled."));
887 for (
unsigned int alpha = 0;
888 alpha < GeometryInfo<dim>::vertices_per_cell;
892 point_matrix(0, alpha) = 1;
893 point_matrix(1, alpha) = cell_vertices[alpha](0);
894 point_matrix(2, alpha) = cell_vertices[alpha](1);
895 point_matrix(3, alpha) =
896 cell_vertices[alpha](0) * cell_vertices[alpha](1);
903 quadrant_mapping_matrix.invert(point_matrix);
907 for (
unsigned int alpha = 0;
908 alpha < GeometryInfo<dim>::vertices_per_cell;
912 point_matrix(0, alpha) = 1;
913 point_matrix(1, alpha) = cell_vertices[alpha](0);
914 point_matrix(2, alpha) = cell_vertices[alpha](1);
915 point_matrix(3, alpha) = cell_vertices[alpha](2);
916 point_matrix(4, alpha) =
917 cell_vertices[alpha](0) * cell_vertices[alpha](1);
918 point_matrix(5, alpha) =
919 cell_vertices[alpha](1) * cell_vertices[alpha](2);
920 point_matrix(6, alpha) =
921 cell_vertices[alpha](0) * cell_vertices[alpha](2);
922 point_matrix(7, alpha) = cell_vertices[alpha](0) *
923 cell_vertices[alpha](1) *
924 cell_vertices[alpha](2);
931 quadrant_mapping_matrix.invert(point_matrix);
934 is_reference_mapping_initialized =
true;
941 PartitionSearch<2>::QuadrantData::set_cell_vertices(
946 quad_length_on_level)
948 constexpr unsigned int dim = 2;
952 double corner_point[dim + 1] = {0};
955 const auto copy_vertex = [&](
unsigned int vertex_index) ->
void {
957 for (
unsigned int d = 0;
d < dim; ++
d)
959 cell_vertices[vertex_index](
d) = corner_point[d];
969 unsigned int vertex_index = 0;
971 forest->connectivity, which_tree, quadrant->x, quadrant->y, corner_point);
974 copy_vertex(vertex_index);
981 forest->connectivity,
983 quadrant->x + quad_length_on_level,
988 copy_vertex(vertex_index);
995 forest->connectivity,
998 quadrant->y + quad_length_on_level,
1002 copy_vertex(vertex_index);
1009 forest->connectivity,
1011 quadrant->x + quad_length_on_level,
1012 quadrant->y + quad_length_on_level,
1016 copy_vertex(vertex_index);
1018 are_vertices_initialized =
true;
1025 PartitionSearch<3>::QuadrantData::set_cell_vertices(
1030 quad_length_on_level)
1032 constexpr unsigned int dim = 3;
1034 double corner_point[dim] = {0};
1037 auto copy_vertex = [&](
unsigned int vertex_index) ->
void {
1039 for (
unsigned int d = 0;
d < dim; ++
d)
1041 cell_vertices[vertex_index](
d) = corner_point[d];
1043 corner_point[
d] = 0;
1051 unsigned int vertex_index = 0;
1053 forest->connectivity,
1061 copy_vertex(vertex_index);
1069 forest->connectivity,
1071 quadrant->x + quad_length_on_level,
1077 copy_vertex(vertex_index);
1084 forest->connectivity,
1087 quadrant->y + quad_length_on_level,
1092 copy_vertex(vertex_index);
1099 forest->connectivity,
1101 quadrant->x + quad_length_on_level,
1102 quadrant->y + quad_length_on_level,
1107 copy_vertex(vertex_index);
1114 forest->connectivity,
1118 quadrant->z + quad_length_on_level,
1122 copy_vertex(vertex_index);
1129 forest->connectivity,
1131 quadrant->x + quad_length_on_level,
1133 quadrant->z + quad_length_on_level,
1137 copy_vertex(vertex_index);
1144 forest->connectivity,
1147 quadrant->y + quad_length_on_level,
1148 quadrant->z + quad_length_on_level,
1152 copy_vertex(vertex_index);
1159 forest->connectivity,
1161 quadrant->x + quad_length_on_level,
1162 quadrant->y + quad_length_on_level,
1163 quadrant->z + quad_length_on_level,
1167 copy_vertex(vertex_index);
1170 are_vertices_initialized =
true;
1180 template <
int dim,
int spacedim>
1181 class RefineAndCoarsenList
1185 const std::vector<types::global_dof_index>
1186 &p4est_tree_to_coarse_cell_permutation,
1214 pointers_are_at_end()
const;
1217 std::vector<typename internal::p4est::types<dim>::quadrant> refine_list;
1218 typename std::vector<typename internal::p4est::types<dim>::quadrant>::
1219 const_iterator current_refine_pointer;
1221 std::vector<typename internal::p4est::types<dim>::quadrant> coarsen_list;
1222 typename std::vector<typename internal::p4est::types<dim>::quadrant>::
1223 const_iterator current_coarsen_pointer;
1234 template <
int dim,
int spacedim>
1236 RefineAndCoarsenList<dim, spacedim>::pointers_are_at_end()
const
1238 return ((current_refine_pointer == refine_list.end()) &&
1239 (current_coarsen_pointer == coarsen_list.end()));
1244 template <
int dim,
int spacedim>
1245 RefineAndCoarsenList<dim, spacedim>::RefineAndCoarsenList(
1247 const std::vector<types::global_dof_index>
1248 &p4est_tree_to_coarse_cell_permutation,
1252 unsigned int n_refine_flags = 0, n_coarsen_flags = 0;
1253 for (
const auto &cell : triangulation.active_cell_iterators())
1256 if (cell->subdomain_id() != my_subdomain)
1259 if (cell->refine_flag_set())
1261 else if (cell->coarsen_flag_set())
1265 refine_list.reserve(n_refine_flags);
1266 coarsen_list.reserve(n_coarsen_flags);
1276 for (
unsigned int c = 0; c < triangulation.
n_cells(0); ++c)
1278 unsigned int coarse_cell_index =
1279 p4est_tree_to_coarse_cell_permutation[c];
1282 &triangulation, 0, coarse_cell_index);
1288 p4est_cell.p.which_tree = c;
1289 build_lists(cell, p4est_cell, my_subdomain);
1297 for (
unsigned int i = 1; i < refine_list.size(); ++i)
1298 Assert(refine_list[i].p.which_tree >= refine_list[i - 1].p.which_tree,
1300 for (
unsigned int i = 1; i < coarsen_list.size(); ++i)
1301 Assert(coarsen_list[i].p.which_tree >= coarsen_list[i - 1].p.which_tree,
1304 current_refine_pointer = refine_list.begin();
1305 current_coarsen_pointer = coarsen_list.begin();
1310 template <
int dim,
int spacedim>
1312 RefineAndCoarsenList<dim, spacedim>::build_lists(
1317 if (cell->is_active())
1319 if (cell->subdomain_id() == my_subdomain)
1321 if (cell->refine_flag_set())
1322 refine_list.push_back(p4est_cell);
1323 else if (cell->coarsen_flag_set())
1324 coarsen_list.push_back(p4est_cell);
1331 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
1336 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
1339 p4est_child[c].p.which_tree = p4est_cell.p.which_tree;
1340 build_lists(cell->child(c), p4est_child[c], my_subdomain);
1346 template <
int dim,
int spacedim>
1348 RefineAndCoarsenList<dim, spacedim>::refine_callback(
1353 RefineAndCoarsenList<dim, spacedim> *this_object =
1354 reinterpret_cast<RefineAndCoarsenList<dim, spacedim> *
>(
1355 forest->user_pointer);
1359 if (this_object->current_refine_pointer == this_object->refine_list.end())
1362 Assert(coarse_cell_index <=
1363 this_object->current_refine_pointer->p.which_tree,
1368 if (coarse_cell_index < this_object->current_refine_pointer->p.which_tree)
1372 Assert(coarse_cell_index <=
1373 this_object->current_refine_pointer->p.which_tree,
1379 quadrant, &*this_object->current_refine_pointer) <= 0,
1384 quadrant, &*this_object->current_refine_pointer))
1386 ++this_object->current_refine_pointer;
1396 template <
int dim,
int spacedim>
1398 RefineAndCoarsenList<dim, spacedim>::coarsen_callback(
1403 RefineAndCoarsenList<dim, spacedim> *this_object =
1404 reinterpret_cast<RefineAndCoarsenList<dim, spacedim> *
>(
1405 forest->user_pointer);
1409 if (this_object->current_coarsen_pointer == this_object->coarsen_list.end())
1412 Assert(coarse_cell_index <=
1413 this_object->current_coarsen_pointer->p.which_tree,
1418 if (coarse_cell_index < this_object->current_coarsen_pointer->p.which_tree)
1422 Assert(coarse_cell_index <=
1423 this_object->current_coarsen_pointer->p.which_tree,
1429 children[0], &*this_object->current_coarsen_pointer) <= 0,
1435 children[0], &*this_object->current_coarsen_pointer))
1438 ++this_object->current_coarsen_pointer;
1442 for (
unsigned int c = 1; c < GeometryInfo<dim>::max_children_per_cell;
1446 children[c], &*this_object->current_coarsen_pointer),
1448 ++this_object->current_coarsen_pointer;
1466 template <
int dim,
int spacedim>
1467 class PartitionWeights
1475 explicit PartitionWeights(
const std::vector<unsigned int> &cell_weights);
1490 std::vector<unsigned int> cell_weights_list;
1491 std::vector<unsigned int>::const_iterator current_pointer;
1495 template <
int dim,
int spacedim>
1496 PartitionWeights<dim, spacedim>::PartitionWeights(
1497 const std::vector<unsigned int> &cell_weights)
1498 : cell_weights_list(cell_weights)
1502 current_pointer = cell_weights_list.begin();
1506 template <
int dim,
int spacedim>
1508 PartitionWeights<dim, spacedim>::cell_weight(
1517 PartitionWeights<dim, spacedim> *this_object =
1518 reinterpret_cast<PartitionWeights<dim, spacedim> *
>(forest->user_pointer);
1520 Assert(this_object->current_pointer >=
1521 this_object->cell_weights_list.begin(),
1523 Assert(this_object->current_pointer < this_object->cell_weights_list.end(),
1529 const unsigned int weight = *this_object->current_pointer;
1530 ++this_object->current_pointer;
1532 Assert(weight <
static_cast<unsigned int>(std::numeric_limits<int>::max()),
1533 ExcMessage(
"p4est uses 'signed int' to represent the partition "
1534 "weights for cells. The weight provided here exceeds "
1535 "the maximum value represented as a 'signed int'."));
1536 return static_cast<int>(weight);
1539 template <
int dim,
int spacedim>
1540 using cell_relation_t =
typename std::pair<
1541 typename ::Triangulation<dim, spacedim>::cell_iterator,
1553 template <
int dim,
int spacedim>
1555 add_single_cell_relation(
1556 std::vector<cell_relation_t<dim, spacedim>> &cell_rel,
1557 const typename ::internal::p4est::types<dim>::tree &tree,
1558 const unsigned int idx,
1562 const unsigned int local_quadrant_index = tree.quadrants_offset + idx;
1568 cell_rel[local_quadrant_index] = std::make_pair(dealii_cell, status);
1582 template <
int dim,
int spacedim>
1584 update_cell_relations_recursively(
1585 std::vector<cell_relation_t<dim, spacedim>> &cell_rel,
1586 const typename ::internal::p4est::types<dim>::tree &tree,
1588 const typename ::internal::p4est::types<dim>::quadrant &p4est_cell)
1591 const int idx = sc_array_bsearch(
1592 const_cast<sc_array_t *
>(&tree.quadrants),
1597 const_cast<typename ::internal::p4est::types<dim>::tree *
>(
1599 &p4est_cell) ==
false))
1604 const bool p4est_has_children = (idx == -1);
1605 if (p4est_has_children && dealii_cell->has_children())
1608 typename ::internal::p4est::types<dim>::quadrant
1611 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
1616 &p4est_cell, p4est_child);
1618 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
1621 update_cell_relations_recursively<dim, spacedim>(
1622 cell_rel, tree, dealii_cell->child(c), p4est_child[c]);
1625 else if (!p4est_has_children && !dealii_cell->has_children())
1629 add_single_cell_relation<dim, spacedim>(
1632 else if (p4est_has_children)
1640 typename ::internal::p4est::types<dim>::quadrant
1642 for (
unsigned int c = 0; c < GeometryInfo<dim>::max_children_per_cell;
1647 &p4est_cell, p4est_child);
1655 for (
unsigned int i = 0; i < GeometryInfo<dim>::max_children_per_cell;
1658 child_idx = sc_array_bsearch(
1659 const_cast<sc_array_t *
>(&tree.quadrants),
1666 add_single_cell_relation<dim, spacedim>(
1667 cell_rel, tree, child_idx, dealii_cell, cell_status);
1675 add_single_cell_relation<dim, spacedim>(
1689 namespace distributed
1692 template <
int dim,
int spacedim>
1704 (settings & construct_multigrid_hierarchy) ?
1708 Triangulation<dim, spacedim>::limit_level_difference_at_vertices) :
1711 , settings(settings)
1712 , triangulation_has_content(false)
1713 , connectivity(
nullptr)
1714 , parallel_forest(
nullptr)
1716 parallel_ghost =
nullptr;
1721 template <
int dim,
int spacedim>
1743 template <
int dim,
int spacedim>
1746 const
std::vector<
Point<spacedim>> &vertices,
1753 vertices, cells, subcelldata);
1756 const typename ::Triangulation<dim, spacedim>::DistortedCellList
1765 this->all_reference_cells_are_hyper_cube(),
1767 "The class parallel::distributed::Triangulation only supports meshes "
1768 "consisting only of hypercube-like cells."));
1773 triangulation_has_content =
true;
1775 setup_coarse_cell_to_p4est_tree_permutation();
1777 copy_new_triangulation_to_p4est(std::integral_constant<int, dim>());
1781 copy_local_forest_to_triangulation();
1790 this->update_periodic_face_map();
1791 this->update_number_cache();
1796 template <
int dim,
int spacedim>
1807 template <
int dim,
int spacedim>
1811 triangulation_has_content =
false;
1813 if (parallel_ghost !=
nullptr)
1817 parallel_ghost =
nullptr;
1820 if (parallel_forest !=
nullptr)
1823 parallel_forest =
nullptr;
1826 if (connectivity !=
nullptr)
1830 connectivity =
nullptr;
1833 coarse_cell_to_p4est_tree_permutation.resize(0);
1834 p4est_tree_to_coarse_cell_permutation.resize(0);
1838 this->update_number_cache();
1843 template <
int dim,
int spacedim>
1854 template <
int dim,
int spacedim>
1865 template <
int dim,
int spacedim>
1871 *previous_global_first_quadrant)
1873 Assert(this->data_serializer.sizes_fixed_cumulative.size() > 0,
1877 this->data_serializer.dest_data_fixed.resize(
1878 parallel_forest->local_num_quadrants *
1879 this->data_serializer.sizes_fixed_cumulative.back());
1882 typename ::internal::p4est::types<dim>::transfer_context
1886 parallel_forest->global_first_quadrant,
1887 previous_global_first_quadrant,
1888 parallel_forest->mpicomm,
1890 this->data_serializer.dest_data_fixed.data(),
1891 this->data_serializer.src_data_fixed.data(),
1892 this->data_serializer.sizes_fixed_cumulative.back());
1894 if (this->data_serializer.variable_size_data_stored)
1897 this->data_serializer.dest_sizes_variable.resize(
1898 parallel_forest->local_num_quadrants);
1903 parallel_forest->global_first_quadrant,
1904 previous_global_first_quadrant,
1905 parallel_forest->mpicomm,
1907 this->data_serializer.dest_sizes_variable.data(),
1908 this->data_serializer.src_sizes_variable.data(),
1909 sizeof(
unsigned int));
1915 this->data_serializer.src_data_fixed.clear();
1916 this->data_serializer.src_data_fixed.shrink_to_fit();
1918 if (this->data_serializer.variable_size_data_stored)
1921 this->data_serializer.dest_data_variable.resize(
1922 std::accumulate(this->data_serializer.dest_sizes_variable.begin(),
1923 this->data_serializer.dest_sizes_variable.end(),
1924 std::vector<int>::size_type(0)));
1928 parallel_forest->global_first_quadrant,
1929 previous_global_first_quadrant,
1930 parallel_forest->mpicomm,
1932 this->data_serializer.dest_data_variable.data(),
1933 this->data_serializer.dest_sizes_variable.data(),
1934 this->data_serializer.src_data_variable.data(),
1935 this->data_serializer.src_sizes_variable.data());
1938 this->data_serializer.src_sizes_variable.clear();
1939 this->data_serializer.src_sizes_variable.shrink_to_fit();
1940 this->data_serializer.src_data_variable.clear();
1941 this->data_serializer.src_data_variable.shrink_to_fit();
1947 template <
int dim,
int spacedim>
1950 spacedim>::setup_coarse_cell_to_p4est_tree_permutation()
1955 coarse_cell_to_p4est_tree_permutation.resize(this->n_cells(0));
1957 cell_connectivity, coarse_cell_to_p4est_tree_permutation);
1959 p4est_tree_to_coarse_cell_permutation =
1965 template <
int dim,
int spacedim>
1968 const
std::
string &file_basename)
const
1970 Assert(parallel_forest !=
nullptr,
1971 ExcMessage(
"Can't produce output when no forest is created yet."));
1975 "To use this function the triangulation's flag "
1976 "Settings::communicate_vertices_to_p4est must be set."));
1979 parallel_forest,
nullptr, file_basename.c_str());
1984 template <
int dim,
int spacedim>
1987 const
std::
string &file_basename)
const
1990 this->cell_attached_data.n_attached_deserialize == 0,
1992 "Not all SolutionTransfer objects have been deserialized after the last call to load()."));
1993 Assert(this->n_cells() > 0,
1994 ExcMessage(
"Can not save() an empty Triangulation."));
2000 this->signals.pre_distributed_save();
2002 if (this->my_subdomain == 0)
2004 std::string fname = file_basename +
".info";
2005 std::ofstream f(fname);
2006 f <<
"version nproc n_attached_fixed_size_objs n_attached_variable_size_objs n_coarse_cells"
2010 << this->cell_attached_data.pack_callbacks_fixed.size() <<
" "
2011 << this->cell_attached_data.pack_callbacks_variable.size() <<
" "
2012 << this->n_cells(0) << std::endl;
2016 for ([[maybe_unused]]
const auto &cell_rel : this->local_cell_relations)
2018 Assert((cell_rel.second ==
2024 this->save_attached_data(parallel_forest->global_first_quadrant[myrank],
2025 parallel_forest->global_num_quadrants,
2033 this->signals.post_distributed_save();
2038 template <
int dim,
int spacedim>
2043 this->n_cells() > 0,
2045 "load() only works if the Triangulation already contains a coarse mesh!"));
2047 this->n_levels() == 1,
2049 "Triangulation may only contain coarse cells when calling load()."));
2055 this->signals.pre_distributed_load();
2057 if (parallel_ghost !=
nullptr)
2061 parallel_ghost =
nullptr;
2064 parallel_forest =
nullptr;
2067 connectivity =
nullptr;
2069 unsigned int version, numcpus, attached_count_fixed,
2070 attached_count_variable, n_coarse_cells;
2072 std::string fname = std::string(file_basename) +
".info";
2073 std::ifstream f(fname);
2075 std::string firstline;
2076 getline(f, firstline);
2077 f >> version >> numcpus >> attached_count_fixed >>
2078 attached_count_variable >> n_coarse_cells;
2082 ExcMessage(
"Incompatible version found in .info file."));
2083 Assert(this->n_cells(0) == n_coarse_cells,
2084 ExcMessage(
"Number of coarse cells differ!"));
2088 this->cell_attached_data.n_attached_data_sets = 0;
2089 this->cell_attached_data.n_attached_deserialize =
2090 attached_count_fixed + attached_count_variable;
2093 file_basename.c_str(),
2094 this->mpi_communicator,
2112 copy_local_forest_to_triangulation();
2122 this->load_attached_data(parallel_forest->global_first_quadrant[myrank],
2123 parallel_forest->global_num_quadrants,
2124 parallel_forest->local_num_quadrants,
2126 attached_count_fixed,
2127 attached_count_variable);
2130 this->signals.post_distributed_load();
2132 this->update_periodic_face_map();
2133 this->update_number_cache();
2138 template <
int dim,
int spacedim>
2141 const typename ::
internal::p4est::
types<dim>::forest *forest)
2143 Assert(this->n_cells() > 0,
2145 "load() only works if the Triangulation already contains "
2147 Assert(this->n_cells() == forest->trees->elem_count,
2149 "Coarse mesh of the Triangulation does not match the one "
2150 "of the provided forest!"));
2153 if (parallel_ghost !=
nullptr)
2157 parallel_ghost =
nullptr;
2160 parallel_forest =
nullptr;
2166 typename ::internal::p4est::types<dim>::forest *temp =
2167 const_cast<typename ::internal::p4est::types<dim>::forest *
>(
2171 parallel_forest->connectivity = connectivity;
2172 parallel_forest->user_pointer =
this;
2176 copy_local_forest_to_triangulation();
2185 this->update_periodic_face_map();
2186 this->update_number_cache();
2191 template <
int dim,
int spacedim>
2195 Assert(parallel_forest !=
nullptr,
2197 "Can't produce a check sum when no forest is created yet."));
2202# if !DEAL_II_P4EST_VERSION_GTE(2, 8, 6, 0)
2218 template <
int dim,
int spacedim>
2220 const typename ::internal::p4est::types<dim>::forest
2223 Assert(parallel_forest !=
nullptr,
2224 ExcMessage(
"The forest has not been allocated yet."));
2225 return parallel_forest;
2230 template <
int dim,
int spacedim>
2232 typename ::internal::p4est::types<dim>::tree
2234 const int dealii_coarse_cell_index)
const
2236 const unsigned int tree_index =
2237 coarse_cell_to_p4est_tree_permutation[dealii_coarse_cell_index];
2238 typename ::internal::p4est::types<dim>::tree *tree =
2239 static_cast<typename ::internal::p4est::types<dim>::tree *
>(
2240 sc_array_index(parallel_forest->trees, tree_index));
2254 std::integral_constant<int, 2>)
2256 const unsigned int dim = 2, spacedim = 2;
2264 std::vector<unsigned int> vertex_touch_count;
2266 std::list<std::pair<Triangulation<dim, spacedim>::active_cell_iterator,
2269 get_vertex_to_cell_mappings(*
this, vertex_touch_count, vertex_to_cell);
2270 const ::internal::p4est::types<2>::locidx num_vtt =
2271 std::accumulate(vertex_touch_count.begin(),
2272 vertex_touch_count.end(),
2278 const bool set_vertex_info = this->are_vertices_communicated_to_p4est();
2281 (set_vertex_info ==
true ? this->n_vertices() : 0),
2286 set_vertex_and_cell_info(*
this,
2289 coarse_cell_to_p4est_tree_permutation,
2293 Assert(p4est_connectivity_is_valid(connectivity) == 1,
2298 this->mpi_communicator,
2315 std::integral_constant<int, 2>)
2317 const unsigned int dim = 2, spacedim = 3;
2325 std::vector<unsigned int> vertex_touch_count;
2327 std::list<std::pair<Triangulation<dim, spacedim>::active_cell_iterator,
2330 get_vertex_to_cell_mappings(*
this, vertex_touch_count, vertex_to_cell);
2331 const ::internal::p4est::types<2>::locidx num_vtt =
2332 std::accumulate(vertex_touch_count.begin(),
2333 vertex_touch_count.end(),
2339 const bool set_vertex_info = this->are_vertices_communicated_to_p4est();
2342 (set_vertex_info ==
true ? this->n_vertices() : 0),
2347 set_vertex_and_cell_info(*
this,
2350 coarse_cell_to_p4est_tree_permutation,
2354 Assert(p4est_connectivity_is_valid(connectivity) == 1,
2359 this->mpi_communicator,
2374 std::integral_constant<int, 3>)
2376 const int dim = 3, spacedim = 3;
2384 std::vector<unsigned int> vertex_touch_count;
2385 std::vector<std::list<
2386 std::pair<Triangulation<3>::active_cell_iterator,
unsigned int>>>
2388 get_vertex_to_cell_mappings(*
this, vertex_touch_count, vertex_to_cell);
2389 const ::internal::p4est::types<2>::locidx num_vtt =
2390 std::accumulate(vertex_touch_count.begin(),
2391 vertex_touch_count.end(),
2394 std::vector<unsigned int> edge_touch_count;
2395 std::vector<std::list<
2396 std::pair<Triangulation<3>::active_cell_iterator,
unsigned int>>>
2398 get_edge_to_cell_mappings(*
this, edge_touch_count, edge_to_cell);
2399 const ::internal::p4est::types<2>::locidx num_ett =
2400 std::accumulate(edge_touch_count.begin(), edge_touch_count.end(), 0u);
2403 const bool set_vertex_info = this->are_vertices_communicated_to_p4est();
2406 (set_vertex_info ==
true ? this->n_vertices() : 0),
2408 this->n_active_lines(),
2413 set_vertex_and_cell_info(*
this,
2416 coarse_cell_to_p4est_tree_permutation,
2445 const unsigned int deal_to_p4est_line_index[12] = {
2446 4, 5, 0, 1, 6, 7, 2, 3, 8, 9, 10, 11};
2448 for (
const auto &cell : this->active_cell_iterators())
2450 const unsigned int index =
2451 coarse_cell_to_p4est_tree_permutation[cell->index()];
2452 for (
unsigned int e = 0;
e < cell->n_lines(); ++
e)
2454 deal_to_p4est_line_index[e]] =
2455 cell->line(e)->index();
2460 connectivity->ett_offset[0] = 0;
2461 std::partial_sum(edge_touch_count.begin(),
2462 edge_touch_count.end(),
2463 &connectivity->ett_offset[1]);
2465 Assert(connectivity->ett_offset[this->n_active_lines()] == num_ett,
2468 for (
unsigned int v = 0; v < this->n_active_lines(); ++v)
2470 Assert(edge_to_cell[v].
size() == edge_touch_count[v],
2474 std::pair<Triangulation<dim, spacedim>::active_cell_iterator,
2476 edge_to_cell[v].begin();
2477 for (
unsigned int c = 0; c < edge_touch_count[v]; ++c, ++p)
2479 connectivity->edge_to_tree[connectivity->ett_offset[v] + c] =
2480 coarse_cell_to_p4est_tree_permutation[p->first->index()];
2481 connectivity->edge_to_edge[connectivity->ett_offset[v] + c] =
2482 deal_to_p4est_line_index[p->second];
2486 Assert(p8est_connectivity_is_valid(connectivity) == 1,
2491 this->mpi_communicator,
2508 template <
int dim,
int spacedim>
2510 enforce_mesh_balance_over_periodic_boundaries(
2516 std::vector<bool> flags_before[2];
2520 std::vector<unsigned int> topological_vertex_numbering(
2522 for (
unsigned int i = 0; i < topological_vertex_numbering.size(); ++i)
2523 topological_vertex_numbering[i] = i;
2541 using cell_iterator =
2543 for (
const auto &it : tria.get_periodic_face_map())
2545 const cell_iterator &cell_1 = it.first.first;
2546 const unsigned int face_no_1 = it.first.second;
2547 const cell_iterator &cell_2 = it.second.first.first;
2548 const unsigned int face_no_2 = it.second.first.second;
2549 const auto combined_orientation = it.second.second;
2551 if (cell_1->level() == cell_2->level())
2553 for (
const unsigned int v :
2558 const unsigned int vface1 =
2559 cell_1->reference_cell().standard_to_real_face_vertex(
2560 v, face_no_1, combined_orientation);
2561 const unsigned int vi1 =
2562 topological_vertex_numbering[cell_1->face(face_no_1)
2563 ->vertex_index(vface1)];
2564 const unsigned int vi2 =
2565 topological_vertex_numbering[cell_2->face(face_no_2)
2567 const unsigned int min_index =
std::min(vi1, vi2);
2568 topological_vertex_numbering[cell_1->face(face_no_1)
2569 ->vertex_index(vface1)] =
2570 topological_vertex_numbering[cell_2->face(face_no_2)
2571 ->vertex_index(v)] =
2580 for (
unsigned int i = 0; i < topological_vertex_numbering.size();
2583 const unsigned int j = topological_vertex_numbering[i];
2584 Assert(j == i || topological_vertex_numbering[j] == j,
2586 "Got inconclusive constraints with chain: " +
2587 std::to_string(i) +
" vs " + std::to_string(j) +
2588 " which should be equal to " +
2589 std::to_string(topological_vertex_numbering[j])));
2596 bool continue_iterating =
true;
2597 std::vector<int> vertex_level(tria.
n_vertices());
2598 while (continue_iterating)
2602 std::fill(vertex_level.begin(), vertex_level.end(), 0);
2606 for (; cell != endc; ++cell)
2608 if (cell->refine_flag_set())
2609 for (
const unsigned int vertex :
2611 vertex_level[topological_vertex_numbering
2612 [cell->vertex_index(vertex)]] =
2613 std::
max(vertex_level[topological_vertex_numbering
2614 [cell->vertex_index(vertex)]],
2616 else if (!cell->coarsen_flag_set())
2617 for (
const unsigned int vertex :
2619 vertex_level[topological_vertex_numbering
2620 [cell->vertex_index(vertex)]] =
2621 std::
max(vertex_level[topological_vertex_numbering
2622 [cell->vertex_index(vertex)]],
2633 for (
const unsigned int vertex :
2635 vertex_level[topological_vertex_numbering
2636 [cell->vertex_index(vertex)]] =
2637 std::
max(vertex_level[topological_vertex_numbering
2638 [cell->vertex_index(vertex)]],
2643 continue_iterating =
false;
2654 for (cell = tria.
last_active(); cell != endc; --cell)
2655 if (cell->refine_flag_set() ==
false)
2657 for (
const unsigned int vertex :
2659 if (vertex_level[topological_vertex_numbering
2660 [cell->vertex_index(vertex)]] >=
2664 cell->clear_coarsen_flag();
2669 if (vertex_level[topological_vertex_numbering
2670 [cell->vertex_index(vertex)]] >
2673 cell->set_refine_flag();
2674 continue_iterating =
true;
2676 for (
const unsigned int v :
2678 vertex_level[topological_vertex_numbering
2679 [cell->vertex_index(v)]] =
2681 vertex_level[topological_vertex_numbering
2682 [cell->vertex_index(v)]],
2693 for (
const auto &cell : tria.cell_iterators())
2696 if (cell->is_active())
2699 const unsigned int n_children = cell->n_children();
2700 unsigned int flagged_children = 0;
2701 for (
unsigned int child = 0; child < n_children; ++child)
2702 if (cell->child(child)->is_active() &&
2703 cell->child(child)->coarsen_flag_set())
2708 if (flagged_children < n_children)
2709 for (
unsigned int child = 0; child < n_children; ++child)
2710 if (cell->child(child)->is_active())
2711 cell->child(child)->clear_coarsen_flag();
2714 std::vector<bool> flags_after[2];
2717 return ((flags_before[0] != flags_after[0]) ||
2718 (flags_before[1] != flags_after[1]));
2724 template <
int dim,
int spacedim>
2743 template <
int dim,
int spacedim>
2764 if (settings & construct_multigrid_hierarchy)
2767 spacedim>::limit_level_difference_at_vertices;
2771 bool mesh_changed =
false;
2779 if (settings & mesh_reconstruction_after_repartitioning)
2780 while (this->n_levels() > 1)
2787 for (
const auto &cell :
2788 this->active_cell_iterators_on_level(this->n_levels() - 1))
2790 cell->set_coarsen_flag();
2808 if (parallel_ghost !=
nullptr)
2812 parallel_ghost =
nullptr;
2816 (dim == 2 ? typename ::internal::p4est::types<dim>::balance_type(
2817 P4EST_CONNECT_CORNER) :
2818 typename ::internal::p4est::types<dim>::balance_type(
2819 P8EST_CONNECT_CORNER)));
2826 for (
const auto &cell : this->cell_iterators_on_level(0))
2831 for (
const auto &cell : this->cell_iterators_on_level(0))
2836 if (tree_exists_locally<dim, spacedim>(
2838 coarse_cell_to_p4est_tree_permutation[cell->index()]) ==
2841 delete_all_children<dim, spacedim>(cell);
2842 if (cell->is_active())
2851 typename ::internal::p4est::types<dim>::quadrant
2853 typename ::internal::p4est::types<dim>::tree *tree =
2854 init_tree(cell->index());
2856 ::internal::p4est::init_coarse_quadrant<dim>(
2859 match_tree_recursively<dim, spacedim>(*tree,
2863 this->my_subdomain);
2870 typename ::internal::p4est::types<dim>::quadrant *quadr;
2872 typename ::internal::p4est::types<dim>::topidx ghost_tree = 0;
2874 for (
unsigned int g_idx = 0;
2875 g_idx < parallel_ghost->ghosts.elem_count;
2878 while (g_idx >=
static_cast<unsigned int>(
2879 parallel_ghost->proc_offsets[ghost_owner + 1]))
2881 while (g_idx >=
static_cast<unsigned int>(
2882 parallel_ghost->tree_offsets[ghost_tree + 1]))
2885 quadr =
static_cast<
2886 typename ::internal::p4est::types<dim>::quadrant *
>(
2887 sc_array_index(¶llel_ghost->ghosts, g_idx));
2889 unsigned int coarse_cell_index =
2890 p4est_tree_to_coarse_cell_permutation[ghost_tree];
2892 match_quadrant<dim, spacedim>(
this,
2907 bool mesh_changed =
true;
2908 unsigned int loop_counter = 0;
2915 this->update_periodic_face_map();
2918 enforce_mesh_balance_over_periodic_boundaries(*
this);
2930 "parallel::distributed::Triangulation::copy_local_forest_to_triangulation() "
2931 "for periodic boundaries detected. Aborting."));
2933 while (mesh_changed);
2938 std::any_of(this->begin_active(),
2941 return cell.refine_flag_set() ||
2942 cell.coarsen_flag_set();
2960 while (mesh_changed);
2965 unsigned int num_ghosts = 0;
2967 for (
const auto &cell : this->active_cell_iterators())
2969 if (cell->subdomain_id() != this->my_subdomain &&
2974 Assert(num_ghosts == parallel_ghost->ghosts.elem_count,
2985 if (settings & construct_multigrid_hierarchy)
2991 for (
unsigned int lvl = this->n_levels(); lvl > 0;)
2994 for (
const auto &cell : this->cell_iterators_on_level(lvl))
2996 if ((cell->is_active() &&
2997 cell->subdomain_id() ==
2998 this->locally_owned_subdomain()) ||
2999 (cell->has_children() &&
3000 cell->child(0)->level_subdomain_id() ==
3001 this->locally_owned_subdomain()))
3002 cell->set_level_subdomain_id(
3003 this->locally_owned_subdomain());
3007 cell->set_level_subdomain_id(
3015 std::vector<std::vector<bool>> marked_vertices(this->n_levels());
3016 for (
unsigned int lvl = 0; lvl < this->n_levels(); ++lvl)
3017 marked_vertices[lvl] = mark_locally_active_vertices_on_level(lvl);
3019 for (
const auto &cell : this->cell_iterators_on_level(0))
3021 typename ::internal::p4est::types<dim>::quadrant
3023 const unsigned int tree_index =
3024 coarse_cell_to_p4est_tree_permutation[cell->index()];
3025 typename ::internal::p4est::types<dim>::tree *tree =
3026 init_tree(cell->index());
3028 ::internal::p4est::init_coarse_quadrant<dim>(
3031 determine_level_subdomain_id_recursively<dim, spacedim>(
3042 for (
unsigned int lvl = this->n_levels(); lvl > 0;)
3045 for (
const auto &cell : this->cell_iterators_on_level(lvl))
3047 if (cell->has_children())
3048 for (
unsigned int c = 0;
3049 c < GeometryInfo<dim>::max_children_per_cell;
3052 if (cell->child(c)->level_subdomain_id() ==
3053 this->locally_owned_subdomain())
3058 cell->child(0)->level_subdomain_id();
3062 cell->set_level_subdomain_id(mark);
3081 const unsigned int total_local_cells = this->n_active_cells();
3086 Assert(
static_cast<unsigned int>(
3087 parallel_forest->local_num_quadrants) ==
3093 Assert(
static_cast<unsigned int>(
3094 parallel_forest->local_num_quadrants) <=
3100 unsigned int n_owned = 0;
3101 for (
const auto &cell : this->active_cell_iterators())
3103 if (cell->subdomain_id() == this->my_subdomain)
3107 Assert(
static_cast<unsigned int>(
3108 parallel_forest->local_num_quadrants) == n_owned,
3113 this->smooth_grid = save_smooth;
3119 update_cell_relations();
3124 template <
int dim,
int spacedim>
3130 std::vector<Point<dim>> point{p};
3131 std::vector<types::subdomain_id> owner = find_point_owner_rank(point);
3138 template <
int dim,
int spacedim>
3141 find_point_owner_rank(const
std::vector<
Point<dim>> &points)
3144 AssertThrow(this->are_vertices_communicated_to_p4est(),
3146 "Vertices need to be communicated to p4est to use this "
3147 "function. This must explicitly be turned on in the "
3148 "settings of the triangulation's constructor."));
3151 for (
const auto &manifold_id : this->get_manifold_ids())
3156 "This function can only be used if the triangulation "
3157 "has no other manifold than a Cartesian (flat) manifold attached."));
3161 PartitionSearch<dim> partition_search;
3167 parallel_forest->user_pointer = &partition_search;
3173 sc_array_t *point_sc_array;
3177 sc_array_new_count(
sizeof(
double[dim + 1]), points.size());
3180 for (
size_t i = 0; i < points.size(); ++i)
3185 double *this_sc_point =
3186 static_cast<double *
>(sc_array_index_ssize_t(point_sc_array, i));
3188 for (
unsigned int d = 0; d < dim; ++d)
3190 this_sc_point[d] = p(d);
3192 this_sc_point[dim] = -1.0;
3198 static_cast<int>(
false),
3199 &PartitionSearch<dim>::local_quadrant_fn,
3200 &PartitionSearch<dim>::local_point_fn,
3204 std::vector<types::subdomain_id> owner_rank(
3208 for (
size_t i = 0; i < points.size(); ++i)
3211 double *this_sc_point =
3212 static_cast<double *
>(sc_array_index_ssize_t(point_sc_array, i));
3213 Assert(this_sc_point[dim] >= 0. || this_sc_point[dim] == -1.,
3215 if (this_sc_point[dim] < 0.)
3223 parallel_forest->user_pointer =
this;
3226 sc_array_destroy_null(&point_sc_array);
3233 template <
int dim,
int spacedim>
3240 for (
const auto &cell : this->active_cell_iterators())
3241 if (cell->is_locally_owned() && cell->refine_flag_set())
3242 Assert(cell->refine_flag_set() ==
3245 "This class does not support anisotropic refinement"));
3250 if (this->n_levels() ==
3254 cell = this->begin_active(
3262 !(cell->refine_flag_set()),
3264 "Fatal Error: maximum refinement level of p4est reached."));
3268 this->prepare_coarsening_and_refinement();
3271 this->signals.pre_distributed_refinement();
3276 for (
const auto &cell : this->active_cell_iterators())
3277 if (cell->is_ghost() || cell->is_artificial())
3279 cell->clear_refine_flag();
3280 cell->clear_coarsen_flag();
3286 RefineAndCoarsenList<dim, spacedim> refine_and_coarsen_list(
3287 *
this, p4est_tree_to_coarse_cell_permutation, this->my_subdomain);
3294 parallel_forest->user_pointer = &refine_and_coarsen_list;
3296 if (parallel_ghost !=
nullptr)
3300 parallel_ghost =
nullptr;
3305 &RefineAndCoarsenList<dim, spacedim>::refine_callback,
3310 &RefineAndCoarsenList<dim, spacedim>::coarsen_callback,
3317 parallel_forest->user_pointer =
this;
3323 (dim == 2 ? typename ::internal::p4est::types<dim>::balance_type(
3324 P4EST_CONNECT_FULL) :
3325 typename ::internal::p4est::types<dim>::balance_type(
3326 P8EST_CONNECT_FULL)),
3331 update_cell_relations();
3335 this->signals.post_p4est_refinement();
3339 std::vector<typename ::internal::p4est::types<dim>::gloidx>
3340 previous_global_first_quadrant;
3342 if (this->cell_attached_data.n_attached_data_sets > 0)
3344 previous_global_first_quadrant.resize(parallel_forest->mpisize + 1);
3345 std::memcpy(previous_global_first_quadrant.data(),
3346 parallel_forest->global_first_quadrant,
3348 typename ::internal::p4est::types<dim>::gloidx) *
3349 (parallel_forest->mpisize + 1));
3352 if (!(settings & no_automatic_repartitioning))
3356 if (this->signals.weight.empty())
3364 const std::vector<unsigned int> cell_weights = get_cell_weights();
3370 this->mpi_communicator) > 0,
3372 "The global sum of weights over all active cells "
3373 "is zero. Please verify how you generate weights."));
3375 PartitionWeights<dim, spacedim> partition_weights(cell_weights);
3380 parallel_forest->user_pointer = &partition_weights;
3386 &PartitionWeights<dim, spacedim>::cell_weight);
3390 parallel_forest, 0,
nullptr,
nullptr);
3392 parallel_forest->user_pointer =
this;
3397 if (this->cell_attached_data.n_attached_data_sets > 0)
3399 this->data_serializer.pack_data(
3400 this->local_cell_relations,
3401 this->cell_attached_data.pack_callbacks_fixed,
3402 this->cell_attached_data.pack_callbacks_variable,
3403 this->get_mpi_communicator());
3409 for (
const auto &cell : this->active_cell_iterators())
3411 cell->clear_refine_flag();
3412 cell->clear_coarsen_flag();
3417 copy_local_forest_to_triangulation();
3427 if (this->cell_attached_data.n_attached_data_sets > 0)
3429 this->execute_transfer(parallel_forest,
3430 previous_global_first_quadrant.data());
3433 this->data_serializer.unpack_cell_status(this->local_cell_relations);
3455 if (settings & construct_multigrid_hierarchy)
3457 for (
unsigned int lvl = 0; lvl < this->n_global_levels(); ++lvl)
3459 std::vector<bool> active_verts =
3460 this->mark_locally_active_vertices_on_level(lvl);
3462 const unsigned int maybe_coarser_lvl =
3463 (lvl > 0) ? (lvl - 1) : lvl;
3465 cell = this->
begin(maybe_coarser_lvl),
3466 endc = this->
end(lvl);
3467 for (; cell != endc; ++cell)
3468 if (cell->level() ==
static_cast<int>(lvl) ||
3471 const bool is_level_artificial =
3472 (cell->level_subdomain_id() ==
3474 bool need_to_know =
false;
3475 for (
const unsigned int vertex :
3477 if (active_verts[cell->vertex_index(vertex)])
3479 need_to_know =
true;
3484 !need_to_know || !is_level_artificial,
3486 "Internal error: the owner of cell" +
3487 cell->id().to_string() +
3488 " is unknown even though it is needed for geometric multigrid."));
3494 this->update_periodic_face_map();
3495 this->update_number_cache();
3498 this->signals.post_distributed_refinement();
3503 template <
int dim,
int spacedim>
3509 for (
const auto &cell : this->active_cell_iterators())
3510 if (cell->is_locally_owned())
3512 !cell->refine_flag_set() && !cell->coarsen_flag_set(),
3514 "Error: There shouldn't be any cells flagged for coarsening/refinement when calling repartition()."));
3518 this->signals.pre_distributed_repartition();
3522 std::vector<typename ::internal::p4est::types<dim>::gloidx>
3523 previous_global_first_quadrant;
3525 if (this->cell_attached_data.n_attached_data_sets > 0)
3527 previous_global_first_quadrant.resize(parallel_forest->mpisize + 1);
3528 std::memcpy(previous_global_first_quadrant.data(),
3529 parallel_forest->global_first_quadrant,
3531 typename ::internal::p4est::types<dim>::gloidx) *
3532 (parallel_forest->mpisize + 1));
3535 if (this->signals.weight.empty())
3547 const std::vector<unsigned int> cell_weights = get_cell_weights();
3553 this->mpi_communicator) > 0,
3555 "The global sum of weights over all active cells "
3556 "is zero. Please verify how you generate weights."));
3558 PartitionWeights<dim, spacedim> partition_weights(cell_weights);
3563 parallel_forest->user_pointer = &partition_weights;
3569 &PartitionWeights<dim, spacedim>::cell_weight);
3572 parallel_forest->user_pointer =
this;
3576 if (this->cell_attached_data.n_attached_data_sets > 0)
3578 this->data_serializer.pack_data(
3579 this->local_cell_relations,
3580 this->cell_attached_data.pack_callbacks_fixed,
3581 this->cell_attached_data.pack_callbacks_variable,
3582 this->get_mpi_communicator());
3587 copy_local_forest_to_triangulation();
3597 if (this->cell_attached_data.n_attached_data_sets > 0)
3599 this->execute_transfer(parallel_forest,
3600 previous_global_first_quadrant.data());
3603 this->update_periodic_face_map();
3606 this->update_number_cache();
3609 this->signals.post_distributed_repartition();
3614 template <
int dim,
int spacedim>
3616 const std::vector<types::global_dof_index>
3620 return p4est_tree_to_coarse_cell_permutation;
3625 template <
int dim,
int spacedim>
3627 const std::vector<types::global_dof_index>
3631 return coarse_cell_to_p4est_tree_permutation;
3636 template <
int dim,
int spacedim>
3639 mark_locally_active_vertices_on_level(const
int level)
const
3643 std::vector<bool> marked_vertices(this->n_vertices(),
false);
3644 for (
const auto &cell : this->cell_iterators_on_level(
level))
3645 if (cell->level_subdomain_id() == this->locally_owned_subdomain())
3647 marked_vertices[cell->vertex_index(v)] =
true;
3664 for (
unsigned int repetition = 0; repetition < dim; ++repetition)
3665 for (
const auto &it : this->get_periodic_face_map())
3668 const unsigned int face_no_1 = it.first.second;
3670 const unsigned int face_no_2 = it.second.first.second;
3671 const auto combined_orientation = it.second.second;
3672 const auto [orientation, rotation, flip] =
3675 if (cell_1->level() ==
level && cell_2->level() ==
level)
3677 for (
unsigned int v = 0;
3683 const unsigned int vface0 =
3685 v, orientation, flip, rotation);
3686 if (marked_vertices[cell_1->face(face_no_1)->vertex_index(
3688 marked_vertices[cell_2->face(face_no_2)->vertex_index(
3690 marked_vertices[cell_1->face(face_no_1)->vertex_index(
3692 marked_vertices[cell_2->face(face_no_2)->vertex_index(
3698 return marked_vertices;
3703 template <
int dim,
int spacedim>
3706 coarse_cell_id_to_coarse_cell_index(
3707 const
types::coarse_cell_id coarse_cell_id)
const
3709 return p4est_tree_to_coarse_cell_permutation[coarse_cell_id];
3714 template <
int dim,
int spacedim>
3718 const unsigned int coarse_cell_index)
const
3720 return coarse_cell_to_p4est_tree_permutation[coarse_cell_index];
3725 template <
int dim,
int spacedim>
3729 &periodicity_vector)
3731 Assert(triangulation_has_content ==
true,
3733 Assert(this->n_levels() == 1,
3734 ExcMessage(
"The triangulation is refined!"));
3741 const auto reference_cell = ReferenceCells::get_hypercube<dim>();
3743 for (
const auto &face_pair : periodicity_vector)
3747 const unsigned int face_left = face_pair.face_idx[0];
3748 const unsigned int face_right = face_pair.face_idx[1];
3751 const unsigned int tree_left =
3752 coarse_cell_to_p4est_tree_permutation[first_cell->index()];
3753 const unsigned int tree_right =
3754 coarse_cell_to_p4est_tree_permutation[second_cell->index()];
3762 unsigned int p4est_orientation = 0;
3766 p4est_orientation = face_pair.orientation ==
3773 const unsigned int face_idx_list[] = {face_left, face_right};
3774 const cell_iterator cell_list[] = {first_cell, second_cell};
3775 unsigned int lower_idx, higher_idx;
3777 if (face_left <= face_right)
3782 face_reference_cell.get_inverse_combined_orientation(
3783 face_pair.orientation);
3789 orientation = face_pair.orientation;
3794 unsigned int first_p4est_idx_on_cell =
3795 p8est_face_corners[face_idx_list[lower_idx]][0];
3796 unsigned int first_dealii_idx_on_face =
3798 for (
unsigned int i = 0; i < GeometryInfo<dim>::vertices_per_face;
3801 const unsigned int first_dealii_idx_on_cell =
3803 face_idx_list[lower_idx],
3805 cell_list[lower_idx]->face_orientation(
3806 face_idx_list[lower_idx]),
3807 cell_list[lower_idx]->face_flip(face_idx_list[lower_idx]),
3808 cell_list[lower_idx]->face_rotation(
3809 face_idx_list[lower_idx]));
3810 if (first_p4est_idx_on_cell == first_dealii_idx_on_cell)
3812 first_dealii_idx_on_face = i;
3820 const unsigned int second_dealii_idx_on_face =
3821 reference_cell.standard_to_real_face_vertex(
3822 first_dealii_idx_on_face,
3823 face_idx_list[lower_idx],
3825 const unsigned int second_dealii_idx_on_cell =
3826 reference_cell.face_to_cell_vertices(
3827 face_idx_list[higher_idx],
3828 second_dealii_idx_on_face,
3829 cell_list[higher_idx]->combined_face_orientation(
3830 face_idx_list[higher_idx]));
3832 const unsigned int second_p4est_idx_on_face =
3833 p8est_corner_face_corners[second_dealii_idx_on_cell]
3834 [face_idx_list[higher_idx]];
3835 p4est_orientation = second_p4est_idx_on_face;
3855 this->mpi_communicator,
3866 copy_local_forest_to_triangulation();
3877 this->update_number_cache();
3882 template <
int dim,
int spacedim>
3893 this->cell_attached_data.n_attached_data_sets) +
3900 coarse_cell_to_p4est_tree_permutation) +
3902 p4est_tree_to_coarse_cell_permutation) +
3903 memory_consumption_p4est();
3910 template <
int dim,
int spacedim>
3914 return ::internal::p4est::functions<dim>::forest_memory_used(
3922 template <
int dim,
int spacedim>
3927 if (const ::parallel::distributed::Triangulation<dim, spacedim>
3928 *other_distributed =
3929 dynamic_cast<const ::parallel::distributed::
3930 Triangulation<dim, spacedim> *
>(&other_tria))
3931 copy_triangulation(other_tria, other_distributed->settings);
3933 copy_triangulation(other_tria, default_setting);
3938 template <
int dim,
int spacedim>
3946 const ::parallel::distributed::Triangulation<dim, spacedim> *
>(
3948 (other_tria.n_global_levels() == 1),
3959 const typename ::Triangulation<dim, spacedim>::DistortedCellList
3967 if (const ::parallel::distributed::Triangulation<dim, spacedim>
3968 *other_distributed =
3969 dynamic_cast<const ::parallel::distributed::
3970 Triangulation<dim, spacedim> *
>(&other_tria))
3973 this->settings = settings;
3975 triangulation_has_content =
3976 other_distributed->triangulation_has_content;
3977 coarse_cell_to_p4est_tree_permutation =
3978 other_distributed->coarse_cell_to_p4est_tree_permutation;
3979 p4est_tree_to_coarse_cell_permutation =
3980 other_distributed->p4est_tree_to_coarse_cell_permutation;
3983 typename ::internal::p4est::types<dim>::connectivity
3984 *temp_connectivity =
const_cast<
3985 typename ::internal::p4est::types<dim>::connectivity *
>(
3986 other_distributed->connectivity);
3988 ::internal::p4est::copy_connectivity<dim>(temp_connectivity);
3991 typename ::internal::p4est::types<dim>::forest *temp_forest =
3992 const_cast<typename ::internal::p4est::types<dim>::forest *
>(
3993 other_distributed->parallel_forest);
3997 parallel_forest->connectivity = connectivity;
3998 parallel_forest->user_pointer =
this;
4002 triangulation_has_content =
true;
4003 setup_coarse_cell_to_p4est_tree_permutation();
4004 copy_new_triangulation_to_p4est(std::integral_constant<int, dim>());
4009 copy_local_forest_to_triangulation();
4018 this->update_periodic_face_map();
4019 this->update_number_cache();
4024 template <
int dim,
int spacedim>
4029 this->local_cell_relations.resize(parallel_forest->local_num_quadrants);
4030 this->local_cell_relations.shrink_to_fit();
4033 for (
const auto &cell : this->cell_iterators_on_level(0))
4036 if (tree_exists_locally<dim, spacedim>(
4038 coarse_cell_to_p4est_tree_permutation[cell->index()]) ==
false)
4042 typename ::internal::p4est::types<dim>::quadrant
4044 ::internal::p4est::init_coarse_quadrant<dim>(p4est_coarse_cell);
4047 typename ::internal::p4est::types<dim>::tree *tree =
4048 init_tree(cell->index());
4050 update_cell_relations_recursively<dim, spacedim>(
4051 this->local_cell_relations, *tree, cell, p4est_coarse_cell);
4057 template <
int dim,
int spacedim>
4064 Assert(this->local_cell_relations.size() ==
4065 static_cast<unsigned int>(parallel_forest->local_num_quadrants),
4070 std::vector<unsigned int> weights;
4071 weights.reserve(this->local_cell_relations.size());
4079 for (
const auto &[cell_it, cell_status] : this->local_cell_relations)
4081 weights.push_back(this->signals.weight(cell_it, cell_status));
4089 template <
int spacedim>
4105 template <
int spacedim>
4114 template <
int spacedim>
4116 const std::vector<types::global_dof_index>
4120 return p4est_tree_to_coarse_cell_permutation;
4125 template <
int spacedim>
4127 std::map<
unsigned int,
4130 const unsigned int )
const
4134 return std::map<unsigned int, std::set<::types::subdomain_id>>();
4139 template <
int spacedim>
4142 mark_locally_active_vertices_on_level(const
unsigned int)
const
4145 return std::vector<bool>();
4150 template <
int spacedim>
4153 coarse_cell_id_to_coarse_cell_index(const
types::coarse_cell_id)
const
4161 template <
int spacedim>
4165 const unsigned int)
const
4173 template <
int spacedim>
4182 template <
int spacedim>
4191 template <
int spacedim>
4201 template <
int spacedim>
4211 template <
int spacedim>
4228 namespace distributed
4230 template <
int dim,
int spacedim>
4238#ifdef DEAL_II_WITH_P4EST
4246 for (
const auto &[cell, status] :
4253 cell->clear_refine_flag();
4254 cell->clear_coarsen_flag();
4259 cell->clear_coarsen_flag();
4260 cell->set_refine_flag();
4265 for (
const auto &child : cell->child_iterators())
4267 child->clear_refine_flag();
4268 child->set_coarsen_flag();
4287 template <
int dim,
int spacedim>
4290#ifdef DEAL_II_WITH_P4EST
4291 if (distributed_tria)
4294 distributed_tria->load_coarsen_flags(saved_coarsen_flags);
4295 distributed_tria->load_refine_flags(saved_refine_flags);
4299 (void)distributed_tria;
4308#include "distributed/tria.inst"
* * for(const auto &cell :triangulation.active_cell_iterators())
* * const_iterator()=default
@ children_will_be_coarsened
virtual void add_periodicity(const std::vector< GridTools::PeriodicFacePair< cell_iterator > > &)
active_cell_iterator last_active() const
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
const std::vector< Point< spacedim > > & get_vertices() const
unsigned int n_active_lines() const
unsigned int n_levels() const
cell_iterator end() const
virtual types::coarse_cell_id coarse_cell_index_to_coarse_cell_id(const unsigned int coarse_cell_index) const
virtual void execute_coarsening_and_refinement()
unsigned int n_cells() const
virtual bool prepare_coarsening_and_refinement()
const std::vector< bool > & get_used_vertices() const
void save_refine_flags(std::ostream &out) const
unsigned int n_vertices() const
const std::map< std::pair< cell_iterator, unsigned int >, std::pair< std::pair< cell_iterator, unsigned int >, types::geometric_orientation > > & get_periodic_face_map() const
void save_coarsen_flags(std::ostream &out) const
active_cell_iterator begin_active(const unsigned int level=0) const
virtual std::size_t memory_consumption() const override
virtual void clear() override
virtual void copy_triangulation(const ::Triangulation< dim, spacedim > &old_tria) override
~TemporarilyMatchRefineFlags()
const ObserverPointer< ::parallel::distributed::Triangulation< dim, spacedim > > distributed_tria
std::vector< bool > saved_refine_flags
std::vector< bool > saved_coarsen_flags
virtual void clear() override
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ASSERT_UNREACHABLE()
#define DEAL_II_NOT_IMPLEMENTED()
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertNothrow(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
typename ::Triangulation< dim, spacedim >::cell_iterator cell_iterator
typename ::Triangulation< dim, spacedim >::active_cell_iterator active_cell_iterator
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
* * if(update_pressure &update_flags) * compute_pressure(constitutive_request
* * * * std::vector< Number > ThermoPlasticMaterial< dim, ViscoplasticYieldLaw, Number >::get_state_parameters const
constexpr const ReferenceCell< dim > & get_hypercube()
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
T broadcast(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
std::vector< Integer > invert_permutation(const std::vector< Integer > &permutation)
bool tree_exists_locally(const typename types< dim >::forest *parallel_forest, const typename types< dim >::topidx coarse_grid_cell)
void exchange_refinement_flags(::parallel::distributed::Triangulation< dim, spacedim > &tria)
std::tuple< bool, bool, bool > split_face_orientation(const types::geometric_orientation combined_orientation)
constexpr unsigned int invalid_unsigned_int
constexpr types::manifold_id flat_manifold_id
constexpr types::subdomain_id artificial_subdomain_id
constexpr types::subdomain_id invalid_subdomain_id
constexpr types::geometric_orientation default_geometric_orientation
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
std::uint8_t geometric_orientation
static unsigned int standard_to_real_face_vertex(const unsigned int vertex, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static unsigned int face_to_cell_vertices(const unsigned int face, const unsigned int vertex, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static std_cxx20::ranges::iota_view< unsigned int, unsigned int > vertex_indices()
static bool is_inside_unit_cell(const Point< dim > &p)
static Point< dim > unit_cell_vertex(const unsigned int vertex)