158 merge(
const DescriptionTemp<dim, spacedim> &other,
159 const bool vertices_have_unique_ids)
162 std::max(other.cell_infos.size(), this->cell_infos.size()));
164 if (vertices_have_unique_ids ==
false)
167 std::map<Point<spacedim>,
170 map_point_to_local_vertex_index(
180 std::map<unsigned int, unsigned int>
181 map_old_to_new_local_vertex_index;
185 for (
const auto &p : other.coarse_cell_vertices)
186 if (map_point_to_local_vertex_index.find(p.second) ==
187 map_point_to_local_vertex_index.end())
190 map_point_to_local_vertex_index[p.second] =
191 map_old_to_new_local_vertex_index[p.first] = counter++;
194 map_old_to_new_local_vertex_index[p.first] =
195 map_point_to_local_vertex_index[p.second];
198 auto other_coarse_cells_copy = other.coarse_cells;
200 for (
auto &cell : other_coarse_cells_copy)
201 for (
auto &v : cell.vertices)
202 v = map_old_to_new_local_vertex_index[v];
205 other_coarse_cells_copy.begin(),
206 other_coarse_cells_copy.end());
211 other.coarse_cells.begin(),
212 other.coarse_cells.end());
215 other.coarse_cell_vertices.begin(),
216 other.coarse_cell_vertices.end());
221 other.coarse_cell_index_to_coarse_cell_id.begin(),
222 other.coarse_cell_index_to_coarse_cell_id.end());
224 for (
unsigned int i = 0; i < this->
cell_infos.size(); ++i)
226 other.cell_infos[i].begin(),
227 other.cell_infos[i].end());
244 for (
unsigned int i = 0; i < this->
coarse_cells.size(); ++i)
245 temp.emplace_back(this->coarse_cell_index_to_coarse_cell_id[i],
246 this->coarse_cells[i],
249 std::sort(temp.begin(),
251 [](
const auto &a,
const auto &b) {
252 return std::get<0>(a) < std::get<0>(b);
254 temp.erase(std::unique(temp.begin(),
256 [](
const auto &a,
const auto &b) {
257 return std::get<0>(a) == std::get<0>(b);
260 std::sort(temp.begin(),
262 [](
const auto &a,
const auto &b) {
263 return std::get<2>(a) < std::get<2>(b);
269 for (
unsigned int i = 0; i < temp.size(); ++i)
272 std::get<0>(temp[i]);
280 this->coarse_cell_vertices.end(),
283 return a.first < b.first;
288 this->coarse_cell_vertices.end(),
291 if (a.first == b.first)
293 Assert(a.second.distance(b.second) <=
295 std::max(a.second.norm(), b.second.norm()),
297 "In the process of merging the vertices of "
298 "the coarse meshes used on different processes, "
299 "there were two processes that used the same "
300 "vertex index for points that are not the same. "
301 "This suggests that you are using different "
302 "coarse meshes on different processes. This "
303 "should not happen."));
312 for (
unsigned int i = 0; i < this->
cell_infos.size(); ++i)
314 if (this->cell_infos[i].empty())
317 std::sort(this->cell_infos[i].
begin(),
318 this->cell_infos[i].
end(),
319 [](
const auto &a,
const auto &b) {
323 std::vector<CellData<dim>> temp;
324 temp.push_back(this->cell_infos[i][0]);
326 for (
unsigned int j = 1; j < this->
cell_infos[i].size(); ++j)
327 if (temp.back().id == cell_infos[i][j].id)
329 temp.back().subdomain_id =
331 this->cell_infos[i][j].subdomain_id);
332 temp.back().level_subdomain_id =
333 std::min(temp.back().level_subdomain_id,
334 this->cell_infos[i][j].level_subdomain_id);
338 temp.push_back(this->cell_infos[i][j]);
350 Description<dim, spacedim>
356 Description<dim, spacedim> description;
359 description.comm =
comm;
361 description.settings = settings;
364 description.smoothing = mesh_smoothing;
366 std::map<unsigned int, unsigned int> map;
370 description.coarse_cell_vertices.push_back(
378 for (unsigned
int v = 0; v < cell.vertices.size(); ++v)
379 cell.
vertices[v] = map[cell.vertices[v]];
381 description.coarse_cell_index_to_coarse_cell_id =
390 std::vector<std::pair<unsigned int, Point<spacedim>>>
401 template <
int dim,
int spacedim>
403 mark_cell_and_its_parents(
405 std::vector<std::vector<bool>> &cell_marked)
407 cell_marked[cell->level()][cell->index()] =
true;
408 if (cell->level() != 0)
409 mark_cell_and_its_parents(cell->parent(), cell_marked);
417 template <
typename DescriptionType,
int dim,
int spacedim>
419 create_description_for_rank(
420 const ::Triangulation<dim, spacedim> &tria,
422 const typename ::Triangulation<dim, spacedim>::cell_iterator &)>
423 &subdomain_id_function,
425 const typename ::Triangulation<dim, spacedim>::cell_iterator &)>
426 &level_subdomain_id_function,
427 const std::map<
unsigned int, std::vector<unsigned int>>
428 &coinciding_vertex_groups,
429 const std::map<unsigned int, unsigned int>
430 &vertex_to_coinciding_vertex_group,
436 std::is_same_v<DescriptionType, Description<dim, spacedim>> ||
437 std::is_same_v<DescriptionType, DescriptionTemp<dim, spacedim>>,
438 "Wrong template type.");
442 (tria.get_mesh_smoothing() &
443 Triangulation<dim, spacedim>::limit_level_difference_at_vertices),
445 "Source triangulation has to be set up with "
446 "limit_level_difference_at_vertices if the construction of the "
447 "multigrid hierarchy is requested!"));
449 const bool construct_multigrid =
453 DescriptionType construction_data;
454 if constexpr (std::is_same_v<DescriptionType,
455 Description<dim, spacedim>>)
457 construction_data.comm =
comm;
458 construction_data.smoothing = tria.get_mesh_smoothing();
459 construction_data.settings = settings;
468 add_vertices_of_cell_to_vertices_owned_by_locally_owned_cells =
469 [&coinciding_vertex_groups, &vertex_to_coinciding_vertex_group](
470 const typename ::Triangulation<dim, spacedim>::cell_iterator
472 std::vector<bool> &vertices_on_locally_owned_cells) {
478 const auto coinciding_vertex_group =
479 vertex_to_coinciding_vertex_group.find(global_vertex_index);
480 if (coinciding_vertex_group !=
481 vertex_to_coinciding_vertex_group.end())
482 for (
const auto &co_vertex : coinciding_vertex_groups.at(
483 coinciding_vertex_group->
second))
484 vertices_on_locally_owned_cells[co_vertex] = true;
488 const auto add_vertices =
489 [&tria](
const std::vector<bool> &vertices_locally_relevant,
490 DescriptionType &construction_data) {
491 if constexpr (std::is_same_v<DescriptionType,
492 Description<dim, spacedim>>)
494 std::vector<unsigned int> vertices_locally_relevant_indices(
495 vertices_locally_relevant.size());
498 unsigned int vertex_counter = 0;
499 for (
unsigned int i = 0; i < vertices_locally_relevant.size();
501 if (vertices_locally_relevant[i])
503 construction_data.coarse_cell_vertices.push_back(
504 tria.get_vertices()[i]);
505 vertices_locally_relevant_indices[i] = vertex_counter++;
510 for (unsigned
int v = 0; v < cell.vertices.size(); ++v)
512 vertices_locally_relevant_indices[cell.vertices[v]];
516 for (
unsigned int i = 0; i < vertices_locally_relevant.size();
518 if (vertices_locally_relevant[i])
519 construction_data.coarse_cell_vertices.emplace_back(
520 i, tria.get_vertices()[i]);
527 std::vector<std::vector<bool>> cell_marked(tria.n_levels());
528 for (
unsigned int l = 0;
l < tria.n_levels(); ++
l)
529 cell_marked[l].resize(tria.n_raw_cells(l));
531 for (
int level = tria.get_triangulation().n_global_levels() - 1;
537 std::vector<bool> vertices_owned_by_locally_owned_cells_on_level(
539 for (
const auto &cell : tria.cell_iterators_on_level(
level))
540 if (construct_multigrid &&
541 (level_subdomain_id_function(cell) ==
my_rank))
542 add_vertices_of_cell_to_vertices_owned_by_locally_owned_cells(
543 cell, vertices_owned_by_locally_owned_cells_on_level);
545 for (
const auto &cell : tria.active_cell_iterators())
546 if (subdomain_id_function(cell) ==
my_rank)
547 add_vertices_of_cell_to_vertices_owned_by_locally_owned_cells(
548 cell, vertices_owned_by_locally_owned_cells_on_level);
553 const auto is_locally_relevant_on_level = [&](
const auto &cell) {
555 if (vertices_owned_by_locally_owned_cells_on_level
556 [cell->vertex_index(v)])
562 for (
const auto &cell : tria.cell_iterators_on_level(
level))
563 if (is_locally_relevant_on_level(cell))
564 mark_cell_and_its_parents(cell, cell_marked);
569 std::vector<bool> vertices_locally_relevant(tria.n_vertices(),
false);
572 for (
const auto &cell : tria.cell_iterators_on_level(0))
574 if (!cell_marked[cell->level()][cell->index()])
579 cell_data.material_id = cell->material_id();
580 cell_data.manifold_id = cell->manifold_id();
582 cell_data.vertices[v] = cell->vertex_index(v);
583 construction_data.coarse_cells.push_back(cell_data);
587 vertices_locally_relevant[cell->vertex_index(v)] = true;
590 construction_data.coarse_cell_index_to_coarse_cell_id.push_back(
591 cell->id().get_coarse_cell_id());
594 add_vertices(vertices_locally_relevant, construction_data);
599 construction_data.cell_infos.resize(
600 tria.get_triangulation().n_global_levels());
603 std::vector<bool> vertices_owned_by_locally_owned_active_cells(
605 for (
const auto &cell : tria.active_cell_iterators())
606 if (subdomain_id_function(cell) ==
my_rank)
607 add_vertices_of_cell_to_vertices_owned_by_locally_owned_cells(
608 cell, vertices_owned_by_locally_owned_active_cells);
612 const auto is_locally_relevant_on_active_level = [&](
const auto &cell) {
613 if (cell->is_active())
615 if (vertices_owned_by_locally_owned_active_cells
616 [cell->vertex_index(v)])
621 for (
unsigned int level = 0;
622 level < tria.get_triangulation().n_global_levels();
626 std::vector<bool> vertices_owned_by_locally_owned_cells_on_level(
628 for (
const auto &cell : tria.cell_iterators_on_level(
level))
629 if ((construct_multigrid &&
630 (level_subdomain_id_function(cell) ==
my_rank)) ||
631 (cell->is_active() && subdomain_id_function(cell) ==
my_rank))
632 add_vertices_of_cell_to_vertices_owned_by_locally_owned_cells(
633 cell, vertices_owned_by_locally_owned_cells_on_level);
637 const auto is_locally_relevant_on_level = [&](
const auto &cell) {
639 if (vertices_owned_by_locally_owned_cells_on_level
640 [cell->vertex_index(v)])
645 auto &level_cell_infos = construction_data.cell_infos[
level];
646 for (
const auto &cell : tria.cell_iterators_on_level(
level))
649 if (!cell_marked[cell->level()][cell->index()])
655 cell_info.id = cell->id().template to_binary<dim>();
658 for (
const auto f : cell->face_indices())
661 cell->face(f)->boundary_id();
663 cell_info.boundary_ids.emplace_back(f, boundary_ind);
675 for (
const auto line : cell->line_indices())
676 cell_info.manifold_line_ids[line] =
681 for (
const auto f : cell->face_indices())
682 cell_info.manifold_quad_ids[f] =
690 if (is_locally_relevant_on_active_level(cell))
692 cell_info.subdomain_id = subdomain_id_function(cell);
694 cell_info.level_subdomain_id =
695 level_subdomain_id_function(cell);
697 else if (is_locally_relevant_on_level(cell))
699 cell_info.level_subdomain_id =
700 level_subdomain_id_function(cell);
707 level_cell_infos.emplace_back(cell_info);
711 return construction_data;
716 template <
int dim,
int spacedim>
717 Description<dim, spacedim>
719 const ::Triangulation<dim, spacedim> &tria,
722 const unsigned int my_rank_in)
724 if (
const auto ptria =
729 ExcMessage(
"MPI communicators do not match."));
733 "For creation from a parallel::Triangulation, "
734 "my_rank has to equal the rank of the current process "
735 "in the given communicator."));
747 const unsigned int n_mpi_processes =
749 for (
const auto &cell : tria.active_cell_iterators())
750 Assert(cell->subdomain_id() < n_mpi_processes,
752 "You can't have a cell with subdomain_id of " +
753 std::to_string(cell->subdomain_id()) +
754 " when splitting the triangulation using an MPI "
755 " communicator with only " +
756 std::to_string(n_mpi_processes) +
" processes."));
767 const auto subdomain_id_function = [](
const auto &cell) {
768 return cell->subdomain_id();
771 const auto level_subdomain_id_function = [](
const auto &cell) {
772 return cell->level_subdomain_id();
775 std::map<unsigned int, std::vector<unsigned int>>
776 coinciding_vertex_groups;
777 std::map<unsigned int, unsigned int> vertex_to_coinciding_vertex_group;
779 coinciding_vertex_groups,
780 vertex_to_coinciding_vertex_group);
782 return create_description_for_rank<Description<dim, spacedim>>(
784 subdomain_id_function,
785 level_subdomain_id_function,
786 coinciding_vertex_groups,
787 vertex_to_coinciding_vertex_group,
795 template <
int dim,
int spacedim>
799 &serial_grid_generator,
802 const unsigned int)> &serial_grid_partitioner,
804 const int group_size,
808#ifndef DEAL_II_WITH_MPI
809 (void)serial_grid_generator;
810 (void)serial_grid_partitioner;
820 const unsigned int group_root = (
my_rank / group_size) * group_size;
833 typename ::Triangulation<dim, spacedim>::MeshSmoothing
>(
836 spacedim>::limit_level_difference_at_vertices) :
838 serial_grid_generator(tria);
841 serial_grid_partitioner(tria,
comm, group_size);
848 const unsigned int end_group =
856 for (
unsigned int other_rank = group_root + 1; other_rank < end_group;
860 const auto construction_data =
866 std::vector<char> buffer;
870 const auto ierr = MPI_Send(buffer.data(),
892 auto ierr = MPI_Probe(group_root, mpi_tag,
comm, &status);
896 MPI_Get_count(&status, MPI_CHAR, &len);
898 std::vector<char> buf(len);
899 ierr = MPI_Recv(buf.data(),
910 auto construction_data =
911 ::Utilities::template unpack<Description<dim, spacedim>>(
916 construction_data.comm =
comm;
918 return construction_data;
925 template <
int dim,
int spacedim>
932 const bool construct_multigrid =
933 (partition.size() > 0) &&
938 construct_multigrid ==
false ||
942 "Source triangulation has to be set up with "
943 "limit_level_difference_at_vertices if the construction of the "
944 "multigrid hierarchy is requested!"));
946 std::vector<LinearAlgebra::distributed::Vector<double>> partitions_mg;
953 if (construct_multigrid)
955 const auto tria_parallel =
963 partitions_mg[l].reinit(
964 tria_parallel->global_level_cell_index_partitioner(l).lock());
974 partition.update_ghost_values();
979 if (cell->is_locally_owned_on_level())
981 if (cell->is_active())
982 partitions_mg[
level][cell->global_level_cell_index()] =
983 partition[cell->global_active_cell_index()];
985 partitions_mg[
level][cell->global_level_cell_index()] =
986 partitions_mg[
level + 1]
988 ->global_level_cell_index()];
996 partitions_mg[
level].update_ghost_values();
1009 template <
int dim,
int spacedim>
1018#ifdef DEAL_II_WITH_MPI
1023 if (partition.size() == 0)
1032 partition.update_ghost_values();
1033 for (
const auto &partition : partitions_mg)
1034 partition.update_ghost_values();
1044 const std::vector<unsigned int> future_owners_of_locally_owned_cells =
1045 [&partition, &partitions_mg]() {
1046 std::set<unsigned int> relevant_process_set;
1048 const unsigned int n_mpi_ranks =
1050 partition.get_mpi_communicator());
1052 for (
unsigned int i = 0; i < partition.locally_owned_size(); ++i)
1054 Assert(
static_cast<unsigned int>(partition.local_element(i)) ==
1055 partition.local_element(i),
1057 "The elements of a partition vector must be integers."));
1059 partition.local_element(i) < n_mpi_ranks,
1061 "The elements of a partition vector must be between zero "
1062 "and the number of processes in the communicator "
1063 "to be used for partitioning the triangulation."));
1064 relevant_process_set.insert(
1065 static_cast<unsigned int>(partition.local_element(i)));
1068 for (
const auto &partition : partitions_mg)
1069 for (
unsigned int i = 0; i < partition.locally_owned_size(); ++i)
1072 static_cast<unsigned int>(partition.local_element(i)) ==
1073 partition.local_element(i),
1075 "The elements of a partition vector must be integers."));
1077 partition.local_element(i) < n_mpi_ranks,
1079 "The elements of a partition vector must be between zero "
1080 "and the number of processes in the communicator "
1081 "to be used for partitioning the triangulation."));
1082 relevant_process_set.insert(
1083 static_cast<unsigned int>(partition.local_element(i)));
1086 return std::vector<unsigned int>(relevant_process_set.begin(),
1087 relevant_process_set.end());
1090 const bool construct_multigrid = (partitions_mg.size() > 0);
1093 (construct_multigrid ?
1103 const auto cell_to_future_owner =
1105 if ((cell->is_active() && (cell->is_artificial() ==
false)))
1107 partition[cell->global_active_cell_index()]);
1112 const auto mg_cell_to_future_owner =
1113 [&construct_multigrid,
1115 if (construct_multigrid && (cell->is_artificial_on_level() ==
false))
1117 partitions_mg[cell->level()][cell->global_level_cell_index()]);
1126 std::vector<DescriptionTemp<dim, spacedim>> descriptions_per_rank;
1127 descriptions_per_rank.reserve(
1128 future_owners_of_locally_owned_cells.size());
1130 std::map<unsigned int, std::vector<unsigned int>>
1131 coinciding_vertex_groups;
1132 std::map<unsigned int, unsigned int> vertex_to_coinciding_vertex_group;
1134 coinciding_vertex_groups,
1135 vertex_to_coinciding_vertex_group);
1137 for (
const auto rank : future_owners_of_locally_owned_cells)
1138 descriptions_per_rank.emplace_back(
1139 create_description_for_rank<DescriptionTemp<dim, spacedim>>(
1141 cell_to_future_owner,
1142 mg_cell_to_future_owner,
1143 coinciding_vertex_groups,
1144 vertex_to_coinciding_vertex_group,
1151 DescriptionTemp<dim, spacedim> description_merged;
1152 description_merged.collect(
1153 future_owners_of_locally_owned_cells,
1154 descriptions_per_rank,
1155 partition.get_mpi_communicator(),
1161 description_merged.reduce();
1164 return description_merged.convert(partition.get_mpi_communicator(),
1175#include "grid/tria_description.inst"