53 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
57 const
Point<spacedim> &p,
58 const
std::vector<
bool> &marked_vertices)
66 const std::vector<Point<spacedim>> &vertices = tria.
get_vertices();
69 marked_vertices.empty(),
71 marked_vertices.size()));
81 marked_vertices.empty() ||
82 std::equal(marked_vertices.begin(),
83 marked_vertices.end(),
85 [](
bool p,
bool q) { return !p || q; }),
87 "marked_vertices should be a subset of used vertices in the triangulation "
88 "but marked_vertices contains one or more vertices that are not used vertices!"));
96 const std::vector<bool> &used =
101 std::vector<bool>::const_iterator
first =
102 std::find(used.begin(), used.end(),
true);
108 unsigned int best_vertex = std::distance(used.begin(),
first);
109 double best_dist = (p - vertices[best_vertex]).norm_square();
113 for (
unsigned int j = best_vertex + 1; j < vertices.size(); ++j)
116 double dist = (p - vertices[j]).norm_square();
117 if (dist < best_dist)
129 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
133 const MeshType<dim, spacedim> &mesh,
134 const
Point<spacedim> &p,
135 const
std::vector<
bool> &marked_vertices)
138 if (mapping.preserves_vertex_locations() ==
true)
150 marked_vertices.empty(),
152 marked_vertices.size()));
162 marked_vertices.empty() ||
163 std::equal(marked_vertices.begin(),
164 marked_vertices.end(),
166 [](
bool p,
bool q) { return !p || q; }),
168 "marked_vertices should be a subset of used vertices in the triangulation "
169 "but marked_vertices contains one or more vertices that are not used vertices!"));
172 if (marked_vertices.size())
173 for (
auto it = vertices.begin(); it != vertices.end();)
175 if (marked_vertices[it->first] ==
false)
177 vertices.erase(it++);
190 template <
typename MeshType>
193 std::vector<typename MeshType::active_cell_iterator>
196 typename ::internal::ActiveCellIterator<MeshType::dimension,
197 MeshType::space_dimension,
201 const unsigned int vertex)
203 const int dim = MeshType::dimension;
204 const int spacedim = MeshType::space_dimension;
210 Assert(mesh.get_triangulation().get_used_vertices()[vertex],
216 std::set<typename ::internal::
217 ActiveCellIterator<dim, spacedim, MeshType>::type>
220 typename ::internal::ActiveCellIterator<dim, spacedim, MeshType>::type
221 cell = mesh.begin_active(),
257 for (; cell != endc; ++cell)
259 for (
const unsigned int v : cell->vertex_indices())
260 if (cell->vertex_index(v) == vertex)
273 const auto reference_cell = cell->reference_cell();
274 for (
const auto face :
275 reference_cell.faces_for_given_vertex(v))
276 if (!cell->at_boundary(face) &&
277 cell->neighbor(face)->is_active())
300 for (
unsigned int e = 0; e < cell->n_lines(); ++e)
301 if (cell->line(e)->has_children())
305 if (cell->line(e)->child(0)->vertex_index(1) == vertex)
328 return std::vector<typename ::internal::
329 ActiveCellIterator<dim, spacedim, MeshType>::type>(
337 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
340 void find_active_cell_around_point_internal(
341 const MeshType<dim, spacedim> &mesh,
343 std::set<typename MeshType<dim, spacedim>::active_cell_iterator>
345 std::set<typename MeshType<dim, spacedim>::active_cell_iterator>
349 typename ::internal::
350 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>
354 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>
359 using cell_iterator =
360 typename MeshType<dim, spacedim>::active_cell_iterator;
362 using cell_iterator = typename ::internal::
363 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type;
371 std::set<cell_iterator> adjacent_cells_new;
375 std::vector<cell_iterator> active_neighbors;
376 get_active_neighbors<MeshType<dim, spacedim>>(cell, active_neighbors);
377 for (
unsigned int i = 0; i < active_neighbors.size(); ++i)
378 if (searched_cells.find(active_neighbors[i]) ==
379 searched_cells.end())
380 adjacent_cells_new.insert(active_neighbors[i]);
384 adjacent_cells_new.end());
393 cell_iterator it = mesh.begin_active();
394 for (; it != mesh.end(); ++it)
395 if (searched_cells.find(it) == searched_cells.end())
406 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
410 typename MeshType<dim, spacedim>::active_cell_iterator
412 typename ::internal::
413 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type
417 const std::vector<bool> &marked_vertices,
418 const double tolerance)
420 return find_active_cell_around_point<dim, MeshType, spacedim>(
431 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
435 std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
Point<dim>>
437 std::pair<typename ::internal::
438 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
442 const MeshType<dim, spacedim> &mesh,
444 const std::vector<bool> &marked_vertices,
445 const double tolerance)
447 using active_cell_iterator = typename ::internal::
448 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type;
454 double best_distance = tolerance;
456 std::pair<active_cell_iterator, Point<dim>> best_cell;
459 best_cell.first = mesh.end();
463 std::vector<active_cell_iterator> adjacent_cells_tmp =
472 std::set<active_cell_iterator>
adjacent_cells(adjacent_cells_tmp.begin(),
473 adjacent_cells_tmp.end());
474 std::set<active_cell_iterator> searched_cells;
482 const auto n_active_cells = mesh.get_triangulation().n_active_cells();
484 unsigned int cells_searched = 0;
485 while (!found && cells_searched < n_active_cells)
489 if (cell->is_artificial() ==
false)
492 if (marked_vertices.size() > 0)
494 bool any_vertex_marked =
false;
495 for (
const auto &v : cell->vertex_indices())
497 if (marked_vertices[cell->vertex_index(v)])
499 any_vertex_marked =
true;
503 if (!any_vertex_marked)
515 cell->reference_cell().closest_point(p_cell).distance(
522 if ((dist < best_distance) ||
523 ((dist == best_distance) &&
524 (cell->level() > best_level)))
527 best_distance = dist;
528 best_level = cell->level();
529 best_cell = std::make_pair(cell, p_cell);
560 if (!found && cells_searched < n_active_cells)
562 find_active_cell_around_point_internal<dim, MeshType, spacedim>(
572 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
576 std::vector<std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
579 std::vector<std::pair<
580 typename ::internal::
581 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
585 const MeshType<dim, spacedim> &mesh,
587 const double tolerance,
588 const std::vector<bool> &marked_vertices)
591 mapping, mesh, p, marked_vertices, tolerance);
593 if (cell_and_point.first == mesh.end())
597 mapping, mesh, p, tolerance, cell_and_point);
602 template <
int dim,
template <
int,
int>
class MeshType,
int spacedim>
606 std::vector<std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
609 std::vector<std::pair<
610 typename ::internal::
611 ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
616 const MeshType<dim, spacedim> &mesh,
618 const double tolerance,
619 const std::pair<
typename MeshType<dim, spacedim>::active_cell_iterator,
622 std::set<
typename MeshType<dim, spacedim>::active_cell_iterator>>
626 std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
631 cells_and_points.push_back(first_cell);
633 const Point<dim> unit_point = cells_and_points.front().second;
634 const auto my_cell = cells_and_points.front().first;
636 std::vector<typename MeshType<dim, spacedim>::active_cell_iterator>
639 if (my_cell->reference_cell().is_hyper_cube())
645 unsigned int n_dirs_at_threshold = 0;
647 for (
unsigned int d = 0; d < dim; ++d)
649 distance_to_center[d] =
std::abs(unit_point[d] - 0.5);
650 if (distance_to_center[d] > 0.5 - tolerance)
652 ++n_dirs_at_threshold;
653 last_point_at_threshold = d;
658 if (n_dirs_at_threshold == 1)
660 unsigned int neighbor_index =
661 2 * last_point_at_threshold +
662 (unit_point[last_point_at_threshold] > 0.5 ? 1 : 0);
663 if (!my_cell->at_boundary(neighbor_index))
665 const auto neighbor_cell = my_cell->neighbor(neighbor_index);
667 if (neighbor_cell->is_active())
668 cells_to_add.push_back(neighbor_cell);
670 for (
const auto &child_cell :
671 neighbor_cell->child_iterators())
673 if (child_cell->is_active())
674 cells_to_add.push_back(child_cell);
679 else if (n_dirs_at_threshold == dim)
681 unsigned int local_vertex_index = 0;
682 for (
unsigned int d = 0; d < dim; ++d)
683 local_vertex_index += (unit_point[d] > 0.5 ? 1 : 0) << d;
685 const auto fu = [&](
const auto &tentative_cells) {
686 for (
const auto &cell : tentative_cells)
688 cells_to_add.push_back(cell);
691 const auto vertex_index = my_cell->vertex_index(local_vertex_index);
693 if (vertex_to_cells !=
nullptr)
694 fu((*vertex_to_cells)[vertex_index]);
703 else if (n_dirs_at_threshold == 2)
706 unsigned int count_vertex_indices = 0;
708 for (
unsigned int d = 0; d < dim; ++d)
710 if (distance_to_center[d] > 0.5 - tolerance)
714 unit_point[d] > 0.5 ? 1 : 0;
715 ++count_vertex_indices;
725 const unsigned int first_vertex =
728 for (
unsigned int d = 0; d < 2; ++d)
730 const auto fu = [&](
const auto &tentative_cells) {
731 for (
const auto &cell : tentative_cells)
733 bool cell_not_yet_present =
true;
734 for (
const auto &other_cell : cells_to_add)
735 if (cell == other_cell)
737 cell_not_yet_present =
false;
740 if (cell_not_yet_present)
741 cells_to_add.push_back(cell);
745 const auto vertex_index =
746 my_cell->vertex_index(first_vertex + (d << free_direction));
748 if (vertex_to_cells !=
nullptr)
749 fu((*vertex_to_cells)[vertex_index]);
763 for (
const auto v : my_cell->vertex_indices())
765 const auto fu = [&](
const auto &tentative_cells) {
766 for (
const auto &cell : tentative_cells)
768 bool cell_not_yet_present =
true;
769 for (
const auto &other_cell : cells_to_add)
770 if (cell == other_cell)
772 cell_not_yet_present =
false;
775 if (cell_not_yet_present)
776 cells_to_add.push_back(cell);
780 const auto vertex_index = my_cell->vertex_index(v);
782 if (vertex_to_cells !=
nullptr)
783 fu((*vertex_to_cells)[vertex_index]);
789 for (
const auto &cell : cells_to_add)
796 if (cell->reference_cell().contains_point(p_unit, tolerance))
797 cells_and_points.emplace_back(cell, p_unit);
804 cells_and_points.begin(),
805 cells_and_points.end(),
806 [](
const std::pair<
typename MeshType<dim, spacedim>::active_cell_iterator,
808 const std::pair<
typename MeshType<dim, spacedim>::active_cell_iterator,
809 Point<dim>> &b) { return a.first < b.first; });
811 return cells_and_points;
816 template <
typename MeshType>
820 const MeshType &mesh,
821 const std::function<
bool(
const typename MeshType::active_cell_iterator &)>
824 std::vector<typename MeshType::active_cell_iterator> active_halo_layer;
825 std::vector<bool> locally_active_vertices_on_subdomain(
826 mesh.get_triangulation().n_vertices(),
false);
828 std::map<unsigned int, std::vector<unsigned int>> coinciding_vertex_groups;
829 std::map<unsigned int, unsigned int> vertex_to_coinciding_vertex_group;
831 coinciding_vertex_groups,
832 vertex_to_coinciding_vertex_group);
837 for (
const auto &cell : mesh.active_cell_iterators())
839 for (
const auto v : cell->vertex_indices())
841 locally_active_vertices_on_subdomain[cell->vertex_index(v)] =
true;
844 vertex_to_coinciding_vertex_group.find(cell->vertex_index(v));
845 if (group != vertex_to_coinciding_vertex_group.end())
846 for (
const auto vv : coinciding_vertex_groups[group->second])
847 locally_active_vertices_on_subdomain[vv] =
true;
853 for (
const auto &cell : mesh.active_cell_iterators())
854 if (!predicate(cell))
855 for (
const auto v : cell->vertex_indices())
856 if (locally_active_vertices_on_subdomain[cell->vertex_index(v)] ==
859 active_halo_layer.push_back(cell);
863 return active_halo_layer;
868 template <
typename MeshType>
872 const MeshType &mesh,
873 const std::function<
bool(
const typename MeshType::cell_iterator &)>
875 const unsigned int level)
877 std::vector<typename MeshType::cell_iterator> level_halo_layer;
878 std::vector<bool> locally_active_vertices_on_level_subdomain(
879 mesh.get_triangulation().n_vertices(),
false);
884 for (
typename MeshType::cell_iterator cell = mesh.begin(
level);
885 cell != mesh.end(
level);
888 for (
const unsigned int v : cell->vertex_indices())
889 locally_active_vertices_on_level_subdomain[cell->vertex_index(v)] =
895 for (
typename MeshType::cell_iterator cell = mesh.begin(
level);
896 cell != mesh.end(
level);
898 if (!predicate(cell))
899 for (
const unsigned int v : cell->vertex_indices())
900 if (locally_active_vertices_on_level_subdomain[cell->vertex_index(
903 level_halo_layer.push_back(cell);
907 return level_halo_layer;
913 template <
typename MeshType>
915 bool contains_locally_owned_cells(
916 const std::vector<typename MeshType::active_cell_iterator> &cells)
918 for (
typename std::vector<
919 typename MeshType::active_cell_iterator>::const_iterator it =
924 if ((*it)->is_locally_owned())
930 template <
typename MeshType>
932 bool contains_artificial_cells(
933 const std::vector<typename MeshType::active_cell_iterator> &cells)
935 for (
typename std::vector<
936 typename MeshType::active_cell_iterator>::const_iterator it =
941 if ((*it)->is_artificial())
950 template <
typename MeshType>
956 std::function<
bool(
const typename MeshType::active_cell_iterator &)>
959 const std::vector<typename MeshType::active_cell_iterator>
964 Assert(contains_locally_owned_cells<MeshType>(active_halo_layer) ==
false,
965 ExcMessage(
"Halo layer contains locally owned cells"));
966 Assert(contains_artificial_cells<MeshType>(active_halo_layer) ==
false,
967 ExcMessage(
"Halo layer contains artificial cells"));
969 return active_halo_layer;
974 template <
typename MeshType>
978 const MeshType &mesh,
979 const std::function<
bool(
const typename MeshType::active_cell_iterator &)>
981 const double layer_thickness)
983 std::vector<typename MeshType::active_cell_iterator>
984 subdomain_boundary_cells, active_cell_layer_within_distance;
985 std::vector<bool> vertices_outside_subdomain(
986 mesh.get_triangulation().n_vertices(),
false);
988 const unsigned int spacedim = MeshType::space_dimension;
990 unsigned int n_non_predicate_cells = 0;
998 for (
const auto &cell : mesh.active_cell_iterators())
999 if (!predicate(cell))
1001 for (
const unsigned int v : cell->vertex_indices())
1002 vertices_outside_subdomain[cell->vertex_index(v)] =
true;
1003 ++n_non_predicate_cells;
1010 if (n_non_predicate_cells == 0 ||
1011 n_non_predicate_cells == mesh.get_triangulation().n_active_cells())
1012 return std::vector<typename MeshType::active_cell_iterator>();
1016 for (
const auto &cell : mesh.active_cell_iterators())
1017 if (predicate(cell))
1019 for (
const unsigned int v : cell->vertex_indices())
1020 if (vertices_outside_subdomain[cell->vertex_index(v)] ==
true)
1022 subdomain_boundary_cells.push_back(cell);
1032 const double DOUBLE_EPSILON = 100. * std::numeric_limits<double>::epsilon();
1035 for (
unsigned int d = 0; d < spacedim; ++d)
1037 bounding_box.first[d] -= (layer_thickness + DOUBLE_EPSILON);
1038 bounding_box.second[d] += (layer_thickness + DOUBLE_EPSILON);
1041 std::vector<Point<spacedim>>
1042 subdomain_boundary_cells_centers;
1045 subdomain_boundary_cells_radii;
1047 subdomain_boundary_cells_centers.reserve(subdomain_boundary_cells.size());
1048 subdomain_boundary_cells_radii.reserve(subdomain_boundary_cells.size());
1050 for (
typename std::vector<typename MeshType::active_cell_iterator>::
1052 subdomain_boundary_cells.begin();
1053 subdomain_boundary_cell_iterator != subdomain_boundary_cells.end();
1054 ++subdomain_boundary_cell_iterator)
1056 const std::pair<Point<spacedim>,
double>
1057 &subdomain_boundary_cell_enclosing_ball =
1058 (*subdomain_boundary_cell_iterator)->enclosing_ball();
1060 subdomain_boundary_cells_centers.push_back(
1061 subdomain_boundary_cell_enclosing_ball.first);
1062 subdomain_boundary_cells_radii.push_back(
1063 subdomain_boundary_cell_enclosing_ball.second);
1065 AssertThrow(subdomain_boundary_cells_radii.size() ==
1066 subdomain_boundary_cells_centers.size(),
1075 for (
const auto &cell : mesh.active_cell_iterators())
1078 if (predicate(cell))
1081 const std::pair<Point<spacedim>,
double> &cell_enclosing_ball =
1082 cell->enclosing_ball();
1085 cell_enclosing_ball.first;
1086 const double cell_enclosing_ball_radius = cell_enclosing_ball.second;
1088 bool cell_inside =
true;
1090 for (
unsigned int d = 0; d < spacedim; ++d)
1092 (cell_enclosing_ball_center[d] + cell_enclosing_ball_radius >
1093 bounding_box.first[d]) &&
1094 (cell_enclosing_ball_center[d] - cell_enclosing_ball_radius <
1095 bounding_box.second[d]);
1101 for (
unsigned int i = 0; i < subdomain_boundary_cells_radii.size();
1104 subdomain_boundary_cells_centers[i]) <
1105 Utilities::fixed_power<2>(cell_enclosing_ball_radius +
1106 subdomain_boundary_cells_radii[i] +
1107 layer_thickness + DOUBLE_EPSILON))
1109 active_cell_layer_within_distance.push_back(cell);
1114 return active_cell_layer_within_distance;
1119 template <
typename MeshType>
1129 std::function<
bool(
const typename MeshType::active_cell_iterator &)>
1130 predicate(locally_owned_cell_predicate);
1132 const std::vector<typename MeshType::active_cell_iterator>
1133 ghost_cell_layer_within_distance =
1141 contains_locally_owned_cells<MeshType>(
1142 ghost_cell_layer_within_distance) ==
false,
1144 "Ghost cells within layer_thickness contains locally owned cells."));
1146 contains_artificial_cells<MeshType>(ghost_cell_layer_within_distance) ==
1149 "Ghost cells within layer_thickness contains artificial cells. "
1150 "The function compute_ghost_cell_layer_within_distance "
1151 "is probably called while using parallel::distributed::Triangulation. "
1152 "In such case please refer to the description of this function."));
1154 return ghost_cell_layer_within_distance;
1159 template <
typename MeshType>
1165 const std::function<
bool(
1166 const typename MeshType::
1167 active_cell_iterator &)>
1170 std::vector<bool> locally_active_vertices_on_subdomain(
1171 mesh.get_triangulation().n_vertices(),
false);
1173 const unsigned int spacedim = MeshType::space_dimension;
1180 for (
const auto &cell : mesh.active_cell_iterators())
1181 if (predicate(cell))
1183 minp = cell->center();
1184 maxp = cell->center();
1190 for (
const auto &cell : mesh.active_cell_iterators())
1191 if (predicate(cell))
1192 for (
const unsigned int v : cell->vertex_indices())
1193 if (locally_active_vertices_on_subdomain[cell->vertex_index(v)] ==
1196 locally_active_vertices_on_subdomain[cell->vertex_index(v)] =
1198 for (
unsigned int d = 0; d < spacedim; ++d)
1200 minp[d] =
std::min(minp[d], cell->vertex(v)[d]);
1201 maxp[d] =
std::max(maxp[d], cell->vertex(v)[d]);
1205 return std::make_pair(minp, maxp);
1210 template <
typename MeshType>
1212 std::list<std::pair<
1213 typename MeshType::cell_iterator,
1220 ExcMessage(
"The two meshes must be represent triangulations that "
1221 "have the same coarse meshes"));
1228 bool remove_ghost_cells =
false;
1229#ifdef DEAL_II_WITH_MPI
1231 constexpr int dim = MeshType::dimension;
1232 constexpr int spacedim = MeshType::space_dimension;
1234 *
>(&mesh_1.get_triangulation()) !=
nullptr ||
1236 *
>(&mesh_2.get_triangulation()) !=
nullptr)
1238 Assert(&mesh_1.get_triangulation() == &mesh_2.get_triangulation(),
1239 ExcMessage(
"This function can only be used with meshes "
1240 "corresponding to distributed Triangulations when "
1241 "both Triangulations are equal."));
1242 remove_ghost_cells =
true;
1254 using CellList = std::list<std::pair<
typename MeshType::cell_iterator,
1255 typename MeshType::cell_iterator>>;
1259 typename MeshType::cell_iterator cell_1 = mesh_1.begin(0),
1260 cell_2 = mesh_2.begin(0);
1261 for (; cell_1 != mesh_1.end(0); ++cell_1, ++cell_2)
1262 cell_list.emplace_back(cell_1, cell_2);
1265 typename CellList::iterator cell_pair = cell_list.begin();
1266 while (cell_pair != cell_list.end())
1270 if (cell_pair->first->has_children() &&
1271 cell_pair->second->has_children())
1273 Assert(cell_pair->first->refinement_case() ==
1274 cell_pair->second->refinement_case(),
1276 for (
unsigned int c = 0; c < cell_pair->first->n_children(); ++c)
1277 cell_list.emplace_back(cell_pair->first->child(c),
1278 cell_pair->second->child(c));
1283 const auto previous_cell_pair = cell_pair;
1285 cell_list.erase(previous_cell_pair);
1290 if (remove_ghost_cells &&
1291 ((cell_pair->first->is_active() &&
1292 !cell_pair->first->is_locally_owned()) ||
1293 (cell_pair->second->is_active() &&
1294 !cell_pair->second->is_locally_owned())))
1297 const auto previous_cell_pair = cell_pair;
1299 cell_list.erase(previous_cell_pair);
1308 for (cell_pair = cell_list.begin(); cell_pair != cell_list.end();
1310 Assert(cell_pair->first->is_active() || cell_pair->second->is_active() ||
1311 (cell_pair->first->refinement_case() !=
1312 cell_pair->second->refinement_case()),
1320 template <
int dim,
int spacedim>
1337 endc = mesh_1.
end(0);
1338 for (; cell_1 != endc; ++cell_1, ++cell_2)
1340 if (cell_1->n_vertices() != cell_2->n_vertices())
1342 for (
const unsigned int v : cell_1->vertex_indices())
1343 if (cell_1->vertex(v) != cell_2->vertex(v))
1356 template <
typename MeshType>
1361 mesh_2.get_triangulation());
1366 template <
int dim,
int spacedim>
1367 std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator,
1373 const double tolerance)
1377 ExcMessage(
"Mapping collection needs to have either size 1 "
1378 "or size equal to the number of elements in "
1379 "the FECollection."));
1381 using cell_iterator =
1384 std::pair<cell_iterator, Point<dim>> best_cell;
1388 if (mapping.
size() == 1)
1390 const std::vector<bool> marked_vertices = {};
1392 mapping[0], mesh, p, marked_vertices, tolerance);
1399 double best_distance = tolerance;
1400 int best_level = -1;
1407 std::vector<cell_iterator> adjacent_cells_tmp =
1415 std::set<cell_iterator>
adjacent_cells(adjacent_cells_tmp.begin(),
1416 adjacent_cells_tmp.end());
1417 std::set<cell_iterator> searched_cells;
1427 unsigned int cells_searched = 0;
1428 while (!found && cells_searched < n_cells)
1435 mapping[cell->active_fe_index()]
1436 .transform_real_to_unit_cell(cell, p);
1442 cell->reference_cell().closest_point(p_cell).distance(
1449 if (dist < best_distance ||
1450 (dist == best_distance && cell->level() > best_level))
1453 best_distance = dist;
1454 best_level = cell->level();
1455 best_cell = std::make_pair(cell, p_cell);
1481 if (!found && cells_searched < n_cells)
1483 find_active_cell_around_point_internal<dim,
1495 template <
typename MeshType>
1498 const typename MeshType::active_cell_iterator &cell)
1500 Assert(cell->is_locally_owned(),
1501 ExcMessage(
"This function only makes sense if the cell for "
1502 "which you are asking for a patch, is locally "
1505 std::vector<typename MeshType::active_cell_iterator> patch;
1506 patch.push_back(cell);
1507 for (
const unsigned int face_number : cell->face_indices())
1508 if (cell->face(face_number)->at_boundary() ==
false)
1510 if (cell->neighbor(face_number)->has_children() ==
false)
1511 patch.push_back(cell->neighbor(face_number));
1516 if (MeshType::dimension > 1)
1518 for (
unsigned int subface = 0;
1519 subface < cell->face(face_number)->n_children();
1522 cell->neighbor_child_on_subface(face_number, subface));
1528 typename MeshType::cell_iterator neighbor =
1529 cell->neighbor(face_number);
1530 while (neighbor->has_children())
1531 neighbor = neighbor->child(1 - face_number);
1533 Assert(neighbor->neighbor(1 - face_number) == cell,
1535 patch.push_back(neighbor);
1543 template <
class Container>
1544 std::vector<typename Container::cell_iterator>
1546 const std::vector<typename Container::active_cell_iterator> &patch)
1550 "Vector containing patch cells should not be an empty vector!"));
1554 int min_level = patch[0]->level();
1556 for (
unsigned int i = 0; i < patch.size(); ++i)
1558 std::set<typename Container::cell_iterator> uniform_cells;
1559 typename std::vector<
1560 typename Container::active_cell_iterator>::const_iterator patch_cell;
1562 for (patch_cell = patch.begin(); patch_cell != patch.end(); ++patch_cell)
1567 if ((*patch_cell)->level() == min_level)
1568 uniform_cells.insert(*patch_cell);
1575 typename Container::cell_iterator parent = *patch_cell;
1577 while (parent->level() > min_level)
1578 parent = parent->parent();
1579 uniform_cells.insert(parent);
1583 return std::vector<typename Container::cell_iterator>(uniform_cells.begin(),
1584 uniform_cells.end());
1589 template <
class Container>
1592 const std::vector<typename Container::active_cell_iterator> &patch,
1594 &local_triangulation,
1597 Container::space_dimension>::active_cell_iterator,
1598 typename Container::active_cell_iterator> &patch_to_global_tria_map)
1601 const std::vector<typename Container::cell_iterator> uniform_cells =
1602 get_cells_at_coarsest_common_level<Container>(patch);
1604 local_triangulation.
clear();
1605 std::vector<Point<Container::space_dimension>> vertices;
1606 const unsigned int n_uniform_cells = uniform_cells.size();
1607 std::vector<CellData<Container::dimension>> cells(n_uniform_cells);
1610 typename std::vector<typename Container::cell_iterator>::const_iterator
1612 for (uniform_cell = uniform_cells.begin();
1613 uniform_cell != uniform_cells.end();
1616 for (
const unsigned int v : (*uniform_cell)->vertex_indices())
1619 (*uniform_cell)->vertex(v);
1620 bool repeat_vertex =
false;
1622 for (
unsigned int m = 0; m < i; ++m)
1624 if (position == vertices[m])
1626 repeat_vertex =
true;
1627 cells[k].vertices[v] = m;
1631 if (repeat_vertex ==
false)
1633 vertices.push_back(position);
1634 cells[k].vertices[v] = i;
1645 unsigned int index = 0;
1648 Container::space_dimension>::cell_iterator,
1649 typename Container::cell_iterator>
1650 patch_to_global_tria_map_tmp;
1652 Container::space_dimension>::cell_iterator
1653 coarse_cell = local_triangulation.
begin();
1654 coarse_cell != local_triangulation.
end();
1655 ++coarse_cell, ++index)
1657 patch_to_global_tria_map_tmp.insert(
1658 std::make_pair(coarse_cell, uniform_cells[index]));
1662 Assert(coarse_cell->center().distance(uniform_cells[index]->center()) <=
1663 1e-15 * coarse_cell->diameter(),
1666 bool refinement_necessary;
1671 refinement_necessary =
false;
1672 for (
const auto &active_tria_cell :
1675 if (patch_to_global_tria_map_tmp[active_tria_cell]->has_children())
1677 active_tria_cell->set_refine_flag();
1678 refinement_necessary =
true;
1681 for (
unsigned int i = 0; i < patch.size(); ++i)
1686 if (patch_to_global_tria_map_tmp[active_tria_cell] ==
1691 for (
const unsigned int v :
1692 active_tria_cell->vertex_indices())
1693 active_tria_cell->vertex(v) = patch[i]->vertex(v);
1695 Assert(active_tria_cell->center().distance(
1696 patch_to_global_tria_map_tmp[active_tria_cell]
1698 1e-15 * active_tria_cell->diameter(),
1701 active_tria_cell->set_user_flag();
1707 if (refinement_necessary)
1712 Container::dimension,
1713 Container::space_dimension>::cell_iterator cell =
1714 local_triangulation.
begin();
1715 cell != local_triangulation.
end();
1718 if (patch_to_global_tria_map_tmp.find(cell) !=
1719 patch_to_global_tria_map_tmp.end())
1721 if (cell->has_children())
1728 for (
unsigned int c = 0; c < cell->n_children(); ++c)
1730 if (patch_to_global_tria_map_tmp.find(cell->child(
1731 c)) == patch_to_global_tria_map_tmp.end())
1733 patch_to_global_tria_map_tmp.insert(
1736 patch_to_global_tria_map_tmp[cell]->child(
1758 patch_to_global_tria_map_tmp.erase(cell);
1764 while (refinement_necessary);
1770 Container::space_dimension>::cell_iterator
1771 cell = local_triangulation.
begin();
1772 cell != local_triangulation.
end();
1775 if (cell->user_flag_set())
1777 Assert(patch_to_global_tria_map_tmp.find(cell) !=
1778 patch_to_global_tria_map_tmp.end(),
1781 Assert(cell->center().distance(
1782 patch_to_global_tria_map_tmp[cell]->center()) <=
1783 1e-15 * cell->diameter(),
1791 Container::space_dimension>::cell_iterator,
1792 typename Container::cell_iterator>::iterator
1793 map_tmp_it = patch_to_global_tria_map_tmp.
begin(),
1794 map_tmp_end = patch_to_global_tria_map_tmp.end();
1798 for (; map_tmp_it != map_tmp_end; ++map_tmp_it)
1799 patch_to_global_tria_map[map_tmp_it->first] = map_tmp_it->second;
1804 template <
int dim,
int spacedim>
1807 std::vector<typename DoFHandler<dim, spacedim>::active_cell_iterator>>
1821 std::set<typename DoFHandler<dim, spacedim>::active_cell_iterator>>
1822 dof_to_set_of_cells_map;
1824 std::vector<types::global_dof_index> local_dof_indices;
1825 std::vector<types::global_dof_index> local_face_dof_indices;
1826 std::vector<types::global_dof_index> local_line_dof_indices;
1830 std::vector<bool> user_flags;
1835 std::map<typename DoFHandler<dim, spacedim>::active_line_iterator,
1837 lines_to_parent_lines_map;
1844 .clear_user_flags();
1849 endc = dof_handler.
end();
1850 for (; cell != endc; ++cell)
1856 if (cell->is_artificial() ==
false)
1858 for (
unsigned int l = 0; l < cell->n_lines(); ++l)
1859 if (cell->line(l)->has_children())
1860 for (
unsigned int c = 0; c < cell->line(l)->n_children();
1863 lines_to_parent_lines_map[cell->line(l)->child(c)] =
1867 cell->line(l)->child(c)->set_user_flag();
1882 endc = dof_handler.
end();
1883 for (; cell != endc; ++cell)
1888 if (cell->is_artificial() ==
false)
1890 const unsigned int n_dofs_per_cell =
1892 local_dof_indices.resize(n_dofs_per_cell);
1896 cell->get_dof_indices(local_dof_indices);
1897 for (
unsigned int i = 0; i < n_dofs_per_cell; ++i)
1898 dof_to_set_of_cells_map[local_dof_indices[i]].insert(cell);
1908 for (
const unsigned int f : cell->face_indices())
1910 if (cell->face(f)->has_children())
1912 for (
unsigned int c = 0; c < cell->face(f)->n_children();
1927 Assert(cell->face(f)->child(c)->has_children() ==
false,
1930 const unsigned int n_dofs_per_face =
1932 local_face_dof_indices.resize(n_dofs_per_face);
1934 cell->face(f)->child(c)->get_dof_indices(
1935 local_face_dof_indices);
1936 for (
unsigned int i = 0; i < n_dofs_per_face; ++i)
1937 dof_to_set_of_cells_map[local_face_dof_indices[i]]
1941 else if ((cell->face(f)->at_boundary() ==
false) &&
1942 (cell->neighbor_is_coarser(f)))
1959 auto [face_no, subface] =
1960 cell->neighbor_of_coarser_neighbor(f);
1963 const unsigned int n_dofs_per_face =
1965 local_face_dof_indices.resize(n_dofs_per_face);
1967 cell->neighbor(f)->face(face_no)->get_dof_indices(
1968 local_face_dof_indices);
1969 for (
unsigned int i = 0; i < n_dofs_per_face; ++i)
1970 dof_to_set_of_cells_map[local_face_dof_indices[i]].insert(
1975 for (
unsigned int c = 0;
1976 c < cell->neighbor(f)->face(face_no)->n_children();
1982 const unsigned int n_dofs_per_face =
1984 local_face_dof_indices.resize(n_dofs_per_face);
1989 ->has_children() ==
false,
1994 ->get_dof_indices(local_face_dof_indices);
1995 for (
unsigned int i = 0; i < n_dofs_per_face; ++i)
1996 dof_to_set_of_cells_map[local_face_dof_indices[i]]
2013 for (
unsigned int l = 0; l < cell->n_lines(); ++l)
2015 if (cell->line(l)->has_children())
2017 for (
unsigned int c = 0;
2018 c < cell->line(l)->n_children();
2021 Assert(cell->line(l)->child(c)->has_children() ==
2027 const unsigned int n_dofs_per_line =
2030 local_line_dof_indices.resize(n_dofs_per_line);
2032 cell->line(l)->child(c)->get_dof_indices(
2033 local_line_dof_indices);
2034 for (
unsigned int i = 0; i < n_dofs_per_line; ++i)
2035 dof_to_set_of_cells_map[local_line_dof_indices[i]]
2043 else if (cell->line(l)->user_flag_set() ==
true)
2047 lines_to_parent_lines_map[cell->line(l)];
2052 const unsigned int n_dofs_per_line =
2055 local_line_dof_indices.resize(n_dofs_per_line);
2057 parent_line->get_dof_indices(local_line_dof_indices);
2058 for (
unsigned int i = 0; i < n_dofs_per_line; ++i)
2059 dof_to_set_of_cells_map[local_line_dof_indices[i]]
2062 for (
unsigned int c = 0; c < parent_line->n_children();
2065 Assert(parent_line->child(c)->has_children() ==
2069 const unsigned int n_dofs_per_line =
2072 local_line_dof_indices.resize(n_dofs_per_line);
2074 parent_line->child(c)->get_dof_indices(
2075 local_line_dof_indices);
2076 for (
unsigned int i = 0; i < n_dofs_per_line; ++i)
2077 dof_to_set_of_cells_map[local_line_dof_indices[i]]
2094 .load_user_flags(user_flags);
2101 std::vector<typename DoFHandler<dim, spacedim>::active_cell_iterator>>
2102 dof_to_cell_patches;
2106 std::set<typename DoFHandler<dim, spacedim>::active_cell_iterator>>::
2107 iterator it = dof_to_set_of_cells_map.begin(),
2108 it_end = dof_to_set_of_cells_map.end();
2109 for (; it != it_end; ++it)
2110 dof_to_cell_patches[it->first].assign(it->second.begin(),
2113 return dof_to_cell_patches;
2119 template <
typename CellIterator>
2122 std::set<std::pair<CellIterator, unsigned int>> &pairs1,
2125 const unsigned int direction,
2127 const ::Tensor<1, CellIterator::AccessorType::space_dimension>
2130 const double abs_tol = 1e-10)
2132 static const int space_dim = CellIterator::AccessorType::space_dimension;
2138 constexpr int dim = CellIterator::AccessorType::dimension;
2139 constexpr int spacedim = CellIterator::AccessorType::space_dimension;
2144 if (!(((pairs1.size() > 0) &&
2145 (
dynamic_cast<const parallel::fullydistributed::
2146 Triangulation<dim, spacedim> *
>(
2147 &pairs1.begin()->first->get_triangulation()) !=
nullptr)) ||
2148 ((pairs2.size() > 0) &&
2149 (
dynamic_cast<const parallel::fullydistributed::
2150 Triangulation<dim, spacedim> *
>(
2151 &pairs2.begin()->first->get_triangulation()) !=
nullptr))))
2152 Assert(pairs1.size() == pairs2.size(),
2153 ExcMessage(
"Unmatched faces on periodic boundaries"));
2157 unsigned int n_matches = 0;
2160 using PairIterator =
2161 typename std::set<std::pair<CellIterator, unsigned int>>::const_iterator;
2162 for (PairIterator it1 = pairs1.begin(); it1 != pairs1.end(); ++it1)
2164 for (PairIterator it2 = pairs2.begin(); it2 != pairs2.end(); ++it2)
2166 const CellIterator cell1 = it1->first;
2167 const CellIterator cell2 = it2->first;
2168 const unsigned int face_idx1 = it1->second;
2169 const unsigned int face_idx2 = it2->second;
2170 if (
const std::optional<types::geometric_orientation> orientation =
2172 cell2->face(face_idx2),
2183 {face_idx1, face_idx2},
2184 orientation.value(),
2186 matched_pairs.push_back(matched_face);
2200 constexpr int dim = CellIterator::AccessorType::dimension;
2201 constexpr int spacedim = CellIterator::AccessorType::space_dimension;
2202 if (!(((pairs1.size() > 0) &&
2203 (
dynamic_cast<const parallel::fullydistributed::
2204 Triangulation<dim, spacedim> *
>(
2205 &pairs1.begin()->first->get_triangulation()) !=
nullptr)) ||
2206 ((pairs2.size() > 0) &&
2209 *
>(&pairs2.begin()->first->get_triangulation()) !=
nullptr))))
2210 AssertThrow(n_matches == pairs1.size() && pairs2.empty(),
2211 ExcMessage(
"Unmatched faces on periodic boundaries"));
2217 template <
typename MeshType>
2220 const MeshType &mesh,
2221 const
types::boundary_id b_id,
2222 const
unsigned int direction,
2225 const
Tensor<1, MeshType::space_dimension> &offset,
2227 const
double abs_tol)
2229 static const int dim = MeshType::dimension;
2230 static const int space_dim = MeshType::space_dimension;
2237 std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs1;
2238 std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs2;
2240 for (
typename MeshType::cell_iterator cell = mesh.begin(0);
2241 cell != mesh.end(0);
2244 const typename MeshType::face_iterator face_1 =
2245 cell->face(2 * direction);
2246 const typename MeshType::face_iterator face_2 =
2247 cell->face(2 * direction + 1);
2249 if (face_1->at_boundary() && face_1->boundary_id() == b_id)
2251 const std::pair<typename MeshType::cell_iterator, unsigned int>
2252 pair1 = std::make_pair(cell, 2 * direction);
2253 pairs1.insert(pair1);
2256 if (face_2->at_boundary() && face_2->boundary_id() == b_id)
2258 const std::pair<typename MeshType::cell_iterator, unsigned int>
2259 pair2 = std::make_pair(cell, 2 * direction + 1);
2260 pairs2.insert(pair2);
2264 Assert(pairs1.size() == pairs2.size(),
2265 ExcMessage(
"Unmatched faces on periodic boundaries"));
2267 Assert(pairs1.size() > 0,
2268 ExcMessage(
"No new periodic face pairs have been found. "
2269 "Are you sure that you've selected the correct boundary "
2270 "id's and that the coarsest level mesh is colorized?"));
2272 [[maybe_unused]]
const unsigned int size_old = matched_pairs.size();
2276 pairs1, pairs2, direction, matched_pairs, offset, matrix, abs_tol);
2281 const unsigned int size_new = matched_pairs.size();
2282 for (
unsigned int i = size_old; i < size_new; ++i)
2284 Assert(matched_pairs[i].orientation ==
2287 "Found a face match with non standard orientation. "
2288 "This function is only suitable for meshes with cells "
2289 "in default orientation"));
2296 template <
typename MeshType>
2299 const MeshType &mesh,
2300 const
types::boundary_id b_id1,
2301 const
types::boundary_id b_id2,
2302 const
unsigned int direction,
2305 const
Tensor<1, MeshType::space_dimension> &offset,
2307 const
double abs_tol)
2309 static const int dim = MeshType::dimension;
2310 static const int space_dim = MeshType::space_dimension;
2316 std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs1;
2317 std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs2;
2319 for (
typename MeshType::cell_iterator cell = mesh.begin(0);
2320 cell != mesh.end(0);
2323 for (
const unsigned int i : cell->face_indices())
2325 const typename MeshType::face_iterator face = cell->face(i);
2326 if (face->at_boundary() && face->boundary_id() == b_id1)
2328 const std::pair<typename MeshType::cell_iterator, unsigned int>
2329 pair1 = std::make_pair(cell, i);
2330 pairs1.insert(pair1);
2333 if (face->at_boundary() && face->boundary_id() == b_id2)
2335 const std::pair<typename MeshType::cell_iterator, unsigned int>
2336 pair2 = std::make_pair(cell, i);
2337 pairs2.insert(pair2);
2348 if (!(((pairs1.size() > 0) &&
2351 *
>(&pairs1.begin()->first->get_triangulation()) !=
nullptr)) ||
2352 ((pairs2.size() > 0) &&
2355 *
>(&pairs2.begin()->first->get_triangulation()) !=
nullptr))))
2356 Assert(pairs1.size() == pairs2.size(),
2357 ExcMessage(
"Unmatched faces on periodic boundaries"));
2360 (pairs1.size() > 0 ||
2363 &mesh.begin()->get_triangulation()) !=
nullptr)),
2364 ExcMessage(
"No new periodic face pairs have been found. "
2365 "Are you sure that you've selected the correct boundary "
2366 "id's and that the coarsest level mesh is colorized?"));
2370 pairs1, pairs2, direction, matched_pairs, offset, matrix, abs_tol);
2384 template <
int spacedim>
2388 const unsigned int direction,
2391 const double abs_tol = 1e-10)
2399 if (matrix.m() == spacedim)
2400 for (
unsigned int i = 0; i < spacedim; ++i)
2401 for (
unsigned int j = 0; j < spacedim; ++j)
2402 distance[i] += matrix(i, j) * point1[j];
2406 distance += offset - point2;
2408 for (
unsigned int i = 0; i < spacedim; ++i)
2414 if (
std::abs(distance[i]) > abs_tol)
2423 template <
typename FaceIterator>
2424 std::optional<types::geometric_orientation>
2426 const FaceIterator &face1,
2427 const FaceIterator &face2,
2428 const unsigned int direction,
2431 const double abs_tol)
2433 Assert(matrix.m() == matrix.n(),
2434 ExcMessage(
"The supplied matrix must be a square matrix"));
2435 Assert(face1->reference_cell() == face2->reference_cell(),
2437 "The faces to be matched must have the same reference cell."));
2442 std::vector<unsigned int> face1_vertices(face1->n_vertices(),
2446 std::set<unsigned int> face2_vertices_set;
2447 for (
unsigned int i = 0; i < face1->n_vertices(); ++i)
2448 face2_vertices_set.insert(i);
2450 for (
unsigned int i = 0; i < face1->n_vertices(); ++i)
2452 for (
auto it = face2_vertices_set.begin();
2453 it != face2_vertices_set.end();
2463 face1_vertices[i] = *it;
2464 face2_vertices[i] = i;
2465 face2_vertices_set.erase(it);
2471 if (face2_vertices_set.empty())
2474 Assert(face1_vertices.end() ==
2475 std::find(face1_vertices.begin(),
2476 face1_vertices.begin() + face1->n_vertices(),
2479 Assert(face2_vertices.end() ==
2480 std::find(face2_vertices.begin(),
2481 face2_vertices.begin() + face1->n_vertices(),
2485 const auto reference_cell = face1->reference_cell();
2488 return std::make_optional(reference_cell.get_combined_orientation(
2490 face2_vertices.cbegin() + face2->n_vertices()),
2492 face1_vertices.cbegin() + face1->n_vertices())));
2495 return std::nullopt;
2500#include "grid/grid_tools_dof_handlers.inst"
* x_component_mask set(0, true)
* * const_iterator()=default
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
cell_iterator end() const
const hp::FECollection< dim, spacedim > & get_fe_collection() const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
const Triangulation< dim, spacedim > & get_triangulation() const
active_cell_iterator begin_active(const unsigned int level=0) const
unsigned int n_dofs_per_vertex() const
unsigned int n_dofs_per_cell() const
unsigned int n_dofs_per_line() const
unsigned int n_dofs_per_face(unsigned int face_no=0, unsigned int child=0) const
Abstract base class for mapping classes.
virtual Point< dim > transform_real_to_unit_cell(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const Point< spacedim > &p) const =0
constexpr numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
cell_iterator begin(const unsigned int level=0) const
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
unsigned int n_active_cells() const
void save_user_flags(std::ostream &out) const
const std::vector< Point< spacedim > > & get_vertices() const
cell_iterator end() const
virtual void execute_coarsening_and_refinement()
unsigned int n_cells() const
const std::vector< bool > & get_used_vertices() const
Triangulation< dim, spacedim > & get_triangulation()
unsigned int size() const
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
IteratorRange< active_cell_iterator > active_cell_iterators() const
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcVertexNotUsed(unsigned int arg1)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
typename ActiveSelector::line_iterator line_iterator
typename ActiveSelector::active_cell_iterator active_cell_iterator
const Mapping< dim, spacedim > & get_default_linear_mapping(const Triangulation< dim, spacedim > &triangulation)
constexpr unsigned int invalid_unsigned_int
constexpr types::geometric_orientation default_geometric_orientation
typename type_identity< T >::type type_identity_t
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index