63 std::vector<std::vector<
64 std::pair<types::global_dof_index, types::global_dof_index>>>
66 std::vector<std::vector<
67 std::pair<types::global_dof_index, types::global_dof_index>>>
68 ©_indices_global_mine,
69 std::vector<std::vector<
70 std::pair<types::global_dof_index, types::global_dof_index>>>
71 ©_indices_level_mine,
72 const bool skip_interface_dofs)
85 std::vector<DoFPair> send_data_temp;
87 const unsigned int n_levels =
89 copy_indices.resize(n_levels);
90 copy_indices_global_mine.resize(n_levels);
91 copy_indices_level_mine.resize(n_levels);
95 std::vector<types::global_dof_index> global_dof_indices(dofs_per_cell);
96 std::vector<types::global_dof_index> level_dof_indices(dofs_per_cell);
100 std::vector<bool> dof_touched(owned_dofs.
n_elements(),
false);
108 std::vector<types::global_dof_index> unrolled_copy_indices;
111 copy_indices_global_mine[
level].clear();
113 for (
const auto &level_cell :
118 (level_cell->level_subdomain_id() ==
120 level_cell->subdomain_id() ==
124 unrolled_copy_indices.resize(owned_dofs.
n_elements(),
129 level_cell->get_dof_indices(global_dof_indices);
130 level_cell->get_mg_dof_indices(level_dof_indices);
132 for (
unsigned int i = 0; i < dofs_per_cell; ++i)
136 if (skip_interface_dofs && mg_constrained_dofs !=
nullptr &&
138 level, level_dof_indices[i]))
150 owned_level_dofs.
is_element(level_dof_indices[i]);
152 if (global_mine && level_mine)
157 unrolled_copy_indices[global_index_in_set] =
158 level_dof_indices[i];
160 else if (global_mine &&
161 dof_touched[global_index_in_set] ==
false)
163 copy_indices_global_mine[
level].emplace_back(
164 global_dof_indices[i], level_dof_indices[i]);
167 send_data_temp.emplace_back(
level,
168 global_dof_indices[i],
169 level_dof_indices[i]);
170 dof_touched[global_index_in_set] =
true;
181 if (!unrolled_copy_indices.empty())
183 copy_indices[
level].clear();
187 if (copy_indices_global_mine[
level].empty())
188 copy_indices[
level].reserve(unrolled_copy_indices.size());
193 for (
unsigned int i = 0; i < unrolled_copy_indices.size(); ++i)
195 copy_indices[
level].emplace_back(
200 const ::parallel::TriangulationBase<dim, spacedim> *tria =
201 (
dynamic_cast<const ::parallel::TriangulationBase<dim, spacedim>
204 send_data_temp.empty() || tria !=
nullptr,
206 "We should only be sending information with a parallel Triangulation!"));
208#ifdef DEAL_II_WITH_MPI
210 tria->get_mpi_communicator()) > 0)
212 const std::set<types::subdomain_id> &neighbors =
213 tria->level_ghost_owners();
214 std::map<int, std::vector<DoFPair>> send_data;
216 std::sort(send_data_temp.begin(),
217 send_data_temp.end(),
219 if (lhs.level < rhs.level)
221 if (lhs.level > rhs.level)
224 if (lhs.level_dof_index < rhs.level_dof_index)
226 if (lhs.level_dof_index > rhs.level_dof_index)
229 if (lhs.global_dof_index < rhs.global_dof_index)
234 send_data_temp.erase(
235 std::unique(send_data_temp.begin(),
236 send_data_temp.end(),
238 return (lhs.level == rhs.level) &&
239 (lhs.level_dof_index == rhs.level_dof_index) &&
240 (lhs.global_dof_index == rhs.global_dof_index);
242 send_data_temp.end());
249 std::vector<types::global_dof_index> level_dof_indices;
250 std::vector<types::global_dof_index> global_dof_indices;
251 for (
const auto &dofpair : send_data_temp)
252 if (dofpair.level ==
level)
254 level_dof_indices.push_back(dofpair.level_dof_index);
255 global_dof_indices.push_back(dofpair.global_dof_index);
260 level_dof_indices.end());
266 owned_level_dofs, is_ghost, tria->get_mpi_communicator());
268 AssertThrow(level_dof_indices.size() == index_owner.size(),
271 for (
unsigned int i = 0; i < index_owner.size(); ++i)
272 send_data[index_owner[i]].emplace_back(
level,
273 global_dof_indices[i],
274 level_dof_indices[i]);
281 mutex, tria->get_mpi_communicator());
287 std::vector<MPI_Request> requests;
289 for (
const auto dest : neighbors)
291 requests.push_back(MPI_Request());
292 std::vector<DoFPair> &
data = send_data[dest];
295 MPI_Isend(
data.data(),
296 data.size() *
sizeof(
decltype(*
data.data())),
300 tria->get_mpi_communicator(),
301 &*requests.rbegin());
309 std::vector<DoFPair> receive_buffer;
310 for (
unsigned int counter = 0; counter < neighbors.size();
314 int ierr = MPI_Probe(MPI_ANY_SOURCE,
316 tria->get_mpi_communicator(),
320 ierr = MPI_Get_count(&status, MPI_BYTE, &len);
325 ierr = MPI_Recv(
nullptr,
330 tria->get_mpi_communicator(),
336 int count = len /
sizeof(
DoFPair);
339 receive_buffer.resize(count);
341 void *ptr = receive_buffer.data();
347 tria->get_mpi_communicator(),
351 for (
const auto &dof_pair : receive_buffer)
353 copy_indices_level_mine[dof_pair.level].emplace_back(
354 dof_pair.global_dof_index, dof_pair.level_dof_index);
360 if (requests.size() > 0)
362 const int ierr = MPI_Waitall(requests.size(),
364 MPI_STATUSES_IGNORE);
372 const int ierr = MPI_Barrier(tria->get_mpi_communicator());
382 std::less<std::pair<types::global_dof_index, types::global_dof_index>>
384 for (
auto &level_indices : copy_indices_level_mine)
385 std::sort(level_indices.begin(), level_indices.end(), compare);
386 for (
auto &level_indices : copy_indices_global_mine)
387 std::sort(level_indices.begin(), level_indices.end(), compare);
397 std::vector<types::global_dof_index> &ghosted_level_dofs,
398 const std::shared_ptr<const Utilities::MPI::Partitioner>
399 &external_partitioner,
401 std::shared_ptr<const Utilities::MPI::Partitioner> &target_partitioner,
404 std::sort(ghosted_level_dofs.begin(), ghosted_level_dofs.end());
406 ghosted_dofs.
add_indices(ghosted_level_dofs.begin(),
407 ghosted_level_dofs.end());
411 if (target_partitioner.get() !=
nullptr &&
412 target_partitioner->size() == locally_owned.
size())
414 ghosted_dofs.
add_indices(target_partitioner->ghost_indices());
419 const int ghosts_locally_contained =
420 (external_partitioner.get() !=
nullptr &&
421 (external_partitioner->ghost_indices() & ghosted_dofs) ==
425 if (external_partitioner.get() !=
nullptr &&
431 if (target_partitioner.get() !=
nullptr &&
432 target_partitioner->size() == locally_owned.
size())
433 for (
unsigned int i = 0; i < copy_indices_global_mine.n_cols(); ++i)
434 copy_indices_global_mine(1, i) =
435 external_partitioner->global_to_local(
436 target_partitioner->local_to_global(
437 copy_indices_global_mine(1, i)));
438 target_partitioner = external_partitioner;
442 if (target_partitioner.get() !=
nullptr &&
443 target_partitioner->size() == locally_owned.
size())
444 for (
unsigned int i = 0; i < copy_indices_global_mine.n_cols(); ++i)
445 copy_indices_global_mine(1, i) =
448 target_partitioner->local_to_global(
449 copy_indices_global_mine(1, i)));
451 std::make_shared<Utilities::MPI::Partitioner>(locally_owned,
618 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
619 &external_partitioners,
621 std::vector<std::vector<unsigned int>> &level_dof_indices,
622 std::vector<std::vector<std::pair<unsigned int, unsigned int>>>
623 &parent_child_connect,
624 std::vector<unsigned int> &n_owned_level_cells,
625 std::vector<std::vector<std::vector<unsigned short>>> &dirichlet_indices,
626 std::vector<std::vector<Number>> &weights_on_refined,
628 MGLevelObject<std::shared_ptr<const Utilities::MPI::Partitioner>>
629 &target_partitioners)
631 level_dof_indices.clear();
632 parent_child_connect.clear();
633 n_owned_level_cells.clear();
634 dirichlet_indices.clear();
635 weights_on_refined.clear();
638 if (mg_constrained_dofs)
640 const unsigned int n_levels =
643 for (
unsigned int l = 0; l < n_levels; ++l)
645 const auto &constraints =
651 for (
const auto dof : constraints.get_local_lines())
653 const auto *entries_ptr =
654 constraints.get_constraint_entries(dof);
656 if (entries_ptr ==
nullptr)
660 Assert((entries_ptr->size() == 0) ||
661 ((entries_ptr->size() == 1) &&
662 (
std::abs((*entries_ptr)[0].second - 1.) <
663 100 * std::numeric_limits<double>::epsilon())),
682 const std::size_t template_starts = fe_name.find_first_of(
'<');
683 Assert(fe_name[template_starts + 1] ==
684 (dim == 1 ?
'1' : (dim == 2 ?
'2' :
'3')),
686 fe_name[template_starts + 1] =
'1';
688 const std::unique_ptr<FiniteElement<1>> fe(
689 FETools::get_fe_by_name<1, 1>(fe_name));
695 const unsigned int n_levels = tria.n_global_levels();
696 level_dof_indices.resize(n_levels);
697 parent_child_connect.resize(n_levels - 1);
698 n_owned_level_cells.resize(n_levels - 1);
699 std::vector<std::vector<unsigned int>> coarse_level_indices(n_levels - 1);
700 for (
unsigned int level = 0;
703 coarse_level_indices[
level].resize(tria.n_raw_cells(
level),
705 std::vector<types::global_dof_index> local_dof_indices(
707 dirichlet_indices.resize(n_levels - 1);
710 Assert(external_partitioners.empty() ||
711 external_partitioners.size() == n_levels,
716 unsigned int counter = 0;
717 std::vector<types::global_dof_index> global_level_dof_indices;
718 std::vector<types::global_dof_index> global_level_dof_indices_remote;
719 std::vector<types::global_dof_index> ghosted_level_dofs;
720 std::vector<types::global_dof_index> global_level_dof_indices_l0;
721 std::vector<types::global_dof_index> ghosted_level_dofs_l0;
726 for (cell = dof_handler.
begin(
level - 1); cell != endc; ++cell)
730 if (!cell->has_children())
734 (tria.locally_owned_subdomain() ==
736 cell->level_subdomain_id() == tria.locally_owned_subdomain());
741 const bool cell_is_remote = !consider_cell;
742 for (
unsigned int c = 0;
743 c < GeometryInfo<dim>::max_children_per_cell;
745 if (cell->child(c)->level_subdomain_id() ==
746 tria.locally_owned_subdomain())
748 consider_cell =
true;
763 std::vector<types::global_dof_index> &next_indices =
764 cell_is_remote ? global_level_dof_indices_remote :
765 global_level_dof_indices;
766 const std::size_t start_index = next_indices.size();
769 for (
unsigned int c = 0;
770 c < GeometryInfo<dim>::max_children_per_cell;
773 if (cell_is_remote && cell->child(c)->level_subdomain_id() !=
774 tria.locally_owned_subdomain())
776 cell->child(c)->get_mg_dof_indices(local_dof_indices);
784 for (
const auto local_dof_index : local_dof_indices)
785 if (!owned_level_dofs.
is_element(local_dof_index))
786 ghosted_level_dofs.push_back(local_dof_index);
788 add_child_indices<dim>(c,
789 fe->n_dofs_per_cell() -
790 fe->n_dofs_per_vertex(),
794 &next_indices[start_index]);
797 if (cell->child(c)->has_children() &&
798 (tria.locally_owned_subdomain() ==
800 cell->child(c)->level_subdomain_id() ==
801 tria.locally_owned_subdomain()))
803 const unsigned int child_index =
804 coarse_level_indices[
level][cell->child(c)->index()];
807 unsigned int parent_index = counter;
818 parent_child_connect[
level][child_index] =
819 std::make_pair(parent_index, c);
821 static_cast<unsigned short>(-1));
825 if (mg_constrained_dofs !=
nullptr)
826 for (
unsigned int i = 0;
827 i < dof_handler.
get_fe().n_dofs_per_cell();
833 dirichlet_indices[
level][child_index].push_back(i);
839 coarse_level_indices[
level - 1].size());
840 coarse_level_indices[
level - 1][cell->index()] = counter++;
847 if (
level == 1 && !cell_is_remote)
849 cell->get_mg_dof_indices(local_dof_indices);
855 const IndexSet &owned_level_dofs_l0 =
857 for (
const auto local_dof_index : local_dof_indices)
858 if (!owned_level_dofs_l0.
is_element(local_dof_index))
859 ghosted_level_dofs_l0.push_back(local_dof_index);
861 const std::size_t start_index =
862 global_level_dof_indices_l0.
size();
863 global_level_dof_indices_l0.resize(
866 add_child_indices<dim>(
868 fe->n_dofs_per_cell() - fe->n_dofs_per_vertex(),
872 &global_level_dof_indices_l0[start_index]);
874 dirichlet_indices[0].emplace_back();
875 if (mg_constrained_dofs !=
nullptr)
876 for (
unsigned int i = 0;
877 i < dof_handler.
get_fe().n_dofs_per_cell();
881 local_dof_indices[elem_info
883 dirichlet_indices[0].back().push_back(i);
891 global_level_dof_indices.size());
892 n_owned_level_cells[
level - 1] = counter;
893 dirichlet_indices[
level - 1].resize(counter);
894 parent_child_connect[
level - 1].resize(
902 if (
level < n_levels - 1)
903 for (std::vector<std::pair<unsigned int, unsigned int>>::iterator
904 i = parent_child_connect[
level].begin();
905 i != parent_child_connect[
level].end();
907 if (i->first >= tria.n_cells(
level))
909 i->first -= tria.n_cells(
level);
921 external_partitioners.empty() ?
923 external_partitioners[
level],
924 tria.get_mpi_communicator(),
925 target_partitioners[
level],
926 copy_indices_global_mine[
level]);
929 global_level_dof_indices,
930 global_level_dof_indices_remote,
931 level_dof_indices[
level]);
935 for (
unsigned int i = 0; i < parent_child_connect[0].size(); ++i)
936 parent_child_connect[0][i] = std::make_pair(i, 0U);
939 ghosted_level_dofs_l0,
940 external_partitioners.empty() ?
942 external_partitioners[0],
943 tria.get_mpi_communicator(),
944 target_partitioners[0],
945 copy_indices_global_mine[0]);
948 *target_partitioners[0],
949 global_level_dof_indices_l0,
950 std::vector<types::global_dof_index>(),
951 level_dof_indices[0]);
957 const unsigned int n_child_dofs_1d =
958 fe->degree + 1 + fe->n_dofs_per_cell() - fe->n_dofs_per_vertex();
962 weights_on_refined.resize(n_levels - 1);
966 target_partitioners[
level]);
967 for (
unsigned int c = 0; c < n_owned_level_cells[
level - 1]; ++c)
975 std::vector<unsigned int> degree_to_3(n_child_dofs_1d);
977 for (
unsigned int i = 1; i < n_child_dofs_1d - 1; ++i)
979 degree_to_3.back() = 2;
983 weights_on_refined[
level - 1].resize(n_owned_level_cells[
level - 1] *
984 Utilities::fixed_power<dim>(3));
985 for (
unsigned int c = 0; c < n_owned_level_cells[
level - 1]; ++c)
986 for (
unsigned int k = 0, m = 0; k < (dim > 2 ? n_child_dofs_1d : 1);
988 for (
unsigned int j = 0; j < (dim > 1 ? n_child_dofs_1d : 1); ++j)
990 unsigned int shift = 9 * degree_to_3[k] + 3 * degree_to_3[j];
991 for (
unsigned int i = 0; i < n_child_dofs_1d; ++i, ++m)
992 weights_on_refined[
level -
993 1][c * Utilities::fixed_power<dim>(3) +
994 shift + degree_to_3[i]] =
997 level_dof_indices[
level]