From 101e9492dd5183adec720786434706b58ee67585 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Tue, 7 Dec 2021 21:58:42 +0100 Subject: [PATCH] Coninue --- source/distributed/fully_distributed_tria.cc | 4 + source/grid/tria.cc | 5 +- source/grid/tria_description.cc | 21 ++- .../repartitioning_08.cc | 153 ++++++++++++++++++ ...tioning_08.with_p4est=true.mpirun=4.output | 63 ++++++++ 5 files changed, 234 insertions(+), 12 deletions(-) create mode 100644 tests/fullydistributed_grids/repartitioning_08.cc create mode 100644 tests/fullydistributed_grids/repartitioning_08.with_p4est=true.mpirun=4.output diff --git a/source/distributed/fully_distributed_tria.cc b/source/distributed/fully_distributed_tria.cc index 2a7496d5ec..7e6df2b815 100644 --- a/source/distributed/fully_distributed_tria.cc +++ b/source/distributed/fully_distributed_tria.cc @@ -335,6 +335,10 @@ namespace parallel this->partitioner_distributed->partition(*this), this->settings); + this->clear(); + this->coarse_cell_id_to_coarse_cell_index_vector.clear(); + this->coarse_cell_index_to_coarse_cell_id_vector.clear(); + this->create_triangulation(construction_data); this->signals.post_distributed_repartition(); diff --git a/source/grid/tria.cc b/source/grid/tria.cc index 57fce4ad70..76b33db228 100644 --- a/source/grid/tria.cc +++ b/source/grid/tria.cc @@ -11871,11 +11871,8 @@ Triangulation::create_triangulation( // boundary ids for (auto pair : cell_info->boundary_ids) - { - Assert(cell->at_boundary(pair.first), - ExcMessage("Cell face is not on the boundary!")); + if (cell->face(pair.first)->at_boundary()) cell->face(pair.first)->set_boundary_id(pair.second); - } } } } diff --git a/source/grid/tria_description.cc b/source/grid/tria_description.cc index 1606c0df64..e8c5041c0b 100644 --- a/source/grid/tria_description.cc +++ b/source/grid/tria_description.cc @@ -163,9 +163,6 @@ namespace TriangulationDescription const auto comp = [](const auto &a, const auto &b) { const double tolerance = 1e-10; - if (a.distance(b) < tolerance) - return false; - for (unsigned int i = 0; i < spacedim; ++i) if (std::abs(a[i] - b[i]) > tolerance) return a[i] < b[i]; @@ -191,15 +188,17 @@ namespace TriangulationDescription else map_i[p.first] = map[p.second]; - for (auto cell : other.coarse_cells) + auto other_coarse_cells_copy = other.coarse_cells; + + for (auto &cell : other_coarse_cells_copy) { for (auto &v : cell.vertices) v = map_i[v]; } this->coarse_cells.insert(this->coarse_cells.end(), - other.coarse_cells.begin(), - other.coarse_cells.end()); + other_coarse_cells_copy.begin(), + other_coarse_cells_copy.end()); } else { @@ -280,7 +279,13 @@ namespace TriangulationDescription std::unique(this->coarse_cell_vertices.begin(), this->coarse_cell_vertices.end(), [](const auto &a, const auto &b) { - return a.first == b.first; + if (a.first == b.first) + { + Assert(a.second.distance(b.second) < 10e-8, + ExcInternalError()); + return true; + } + return false; }), this->coarse_cell_vertices.end()); } @@ -1083,7 +1088,7 @@ namespace TriangulationDescription partition.get_mpi_communicator(), dynamic_cast< const parallel::fullydistributed::Triangulation *>( - &tria) != nullptr); + &tria) == nullptr); // remove redundant entries description_merged.reduce(); diff --git a/tests/fullydistributed_grids/repartitioning_08.cc b/tests/fullydistributed_grids/repartitioning_08.cc new file mode 100644 index 0000000000..5905486ef5 --- /dev/null +++ b/tests/fullydistributed_grids/repartitioning_08.cc @@ -0,0 +1,153 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2021 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Test +// TriangulationDescription::Utilities::create_description_from_triangulation() +// with repartitioning capabilities. + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#include "./tests.h" + +using namespace dealii; + + +template +class MyPolicy : public RepartitioningPolicyTools::Base +{ +public: + MyPolicy(const MPI_Comm &comm, const unsigned int direction) + : comm(comm) + , direction(direction) + {} + + virtual LinearAlgebra::distributed::Vector + partition(const Triangulation &tria_in) const override + { + const auto tria = + dynamic_cast *>( + &tria_in); + + Assert(tria, ExcNotImplemented()); + + LinearAlgebra::distributed::Vector partition( + tria->global_active_cell_index_partitioner().lock()); + + const unsigned int n_partitions = Utilities::MPI::n_mpi_processes(comm); + + for (const auto &cell : + tria_in.active_cell_iterators() | IteratorFilters::LocallyOwnedCell()) + partition[cell->global_active_cell_index()] = + std::floor(cell->center()[direction] * n_partitions); + + partition.update_ghost_values(); + + return partition; + } + +private: + const MPI_Comm & comm; + const unsigned int direction; +}; + + +template +void +test(const MPI_Comm comm) +{ + parallel::distributed::Triangulation tria( + comm, + Triangulation::none, + parallel::distributed::Triangulation::construct_multigrid_hierarchy); + GridGenerator::subdivided_hyper_cube(tria, 4); + tria.refine_global(2); + + MyPolicy policy_0(comm, 0); + MyPolicy policy_1(comm, 1); + + const auto partition_0 = policy_0.partition(tria); + + const auto settings = + TriangulationDescription::Settings::construct_multigrid_hierarchy; + + // repartition triangulation so that it has strided partitioning + const auto construction_data = + TriangulationDescription::Utilities::create_description_from_triangulation( + tria, partition_0, settings); + + parallel::fullydistributed::Triangulation tria_pft(comm); + tria_pft.create_triangulation(construction_data); + + { + FE_Q fe(2); + DoFHandler dof_handler(tria_pft); + dof_handler.distribute_dofs(fe); + dof_handler.distribute_mg_dofs(); + + // print statistics + print_statistics(tria_pft); + print_statistics(dof_handler); + } + + GridOut go; + go.write_mesh_per_processor_as_vtu(tria_pft, "mesh_old"); + + tria_pft.set_partitioner(policy_1, settings); + + tria_pft.repartition(); + + go.write_mesh_per_processor_as_vtu(tria_pft, "mesh_new"); + + { + FE_Q fe(2); + DoFHandler dof_handler(tria_pft); + dof_handler.distribute_dofs(fe); + dof_handler.distribute_mg_dofs(); + + // print statistics + print_statistics(tria_pft); + print_statistics(dof_handler); + } +} + + + +int +main(int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi(argc, argv, 1); + MPILogInitAll all; + + MPI_Comm comm = MPI_COMM_WORLD; + + test<2>(comm); + deallog.pop(); +} diff --git a/tests/fullydistributed_grids/repartitioning_08.with_p4est=true.mpirun=4.output b/tests/fullydistributed_grids/repartitioning_08.with_p4est=true.mpirun=4.output new file mode 100644 index 0000000000..658d881721 --- /dev/null +++ b/tests/fullydistributed_grids/repartitioning_08.with_p4est=true.mpirun=4.output @@ -0,0 +1,63 @@ + +DEAL:0::n_levels: 3 +DEAL:0::n_cells: 136 +DEAL:0::n_active_cells: 104 +DEAL:0:: +DEAL:0::n_dofs: 1089 +DEAL:0::n_locally_owned_dofs: 297 +DEAL:0:: +DEAL:0::n_levels: 3 +DEAL:0::n_cells: 136 +DEAL:0::n_active_cells: 104 +DEAL:0:: +DEAL:0::n_dofs: 1089 +DEAL:0::n_locally_owned_dofs: 297 +DEAL:0:: + +DEAL:1::n_levels: 3 +DEAL:1::n_cells: 188 +DEAL:1::n_active_cells: 144 +DEAL:1:: +DEAL:1::n_dofs: 1089 +DEAL:1::n_locally_owned_dofs: 264 +DEAL:1:: +DEAL:1::n_levels: 3 +DEAL:1::n_cells: 188 +DEAL:1::n_active_cells: 144 +DEAL:1:: +DEAL:1::n_dofs: 1089 +DEAL:1::n_locally_owned_dofs: 264 +DEAL:1:: + + +DEAL:2::n_levels: 3 +DEAL:2::n_cells: 188 +DEAL:2::n_active_cells: 144 +DEAL:2:: +DEAL:2::n_dofs: 1089 +DEAL:2::n_locally_owned_dofs: 264 +DEAL:2:: +DEAL:2::n_levels: 3 +DEAL:2::n_cells: 188 +DEAL:2::n_active_cells: 144 +DEAL:2:: +DEAL:2::n_dofs: 1089 +DEAL:2::n_locally_owned_dofs: 264 +DEAL:2:: + + +DEAL:3::n_levels: 3 +DEAL:3::n_cells: 136 +DEAL:3::n_active_cells: 104 +DEAL:3:: +DEAL:3::n_dofs: 1089 +DEAL:3::n_locally_owned_dofs: 264 +DEAL:3:: +DEAL:3::n_levels: 3 +DEAL:3::n_cells: 136 +DEAL:3::n_active_cells: 104 +DEAL:3:: +DEAL:3::n_dofs: 1089 +DEAL:3::n_locally_owned_dofs: 264 +DEAL:3:: + -- 2.39.5