From: Peter Munch Date: Wed, 4 Dec 2024 06:35:02 +0000 (+0100) Subject: Add new test for repartitioning of p:f:T X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F17907%2Fhead;p=dealii.git Add new test for repartitioning of p:f:T --- diff --git a/tests/fullydistributed_grids/repartitioning_09.cc b/tests/fullydistributed_grids/repartitioning_09.cc new file mode 100644 index 0000000000..02123704ed --- /dev/null +++ b/tests/fullydistributed_grids/repartitioning_09.cc @@ -0,0 +1,167 @@ +// ------------------------------------------------------------------------ +// +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2025 by the deal.II authors +// +// This file is part of the deal.II library. +// +// Part of the source code is dual licensed under Apache-2.0 WITH +// LLVM-exception OR LGPL-2.1-or-later. Detailed license information +// governing the source code and code contributions can be found in +// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. +// +// ------------------------------------------------------------------------ + + +// Test +// TriangulationDescription::Utilities::create_description_from_triangulation() +// with repartitioning capabilities (partition immersed mesh as the background +// mesh). + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include +#include + +#include "../grid/tests.h" + + + +template +class MyPolicy : public RepartitioningPolicyTools::Base +{ +public: + MyPolicy(const Triangulation &tria_background) + : tria_background(tria_background) + {} + + virtual LinearAlgebra::distributed::Vector + partition(const Triangulation &tria_immersed) const override + { + // 1) collect centers of immeresed mesh + std::vector> points; + + for (const auto &cell : tria_immersed.active_cell_iterators()) + if (cell->is_locally_owned()) + points.push_back(cell->center()); + + // 2) determine owner on background mesh + Utilities::MPI::RemotePointEvaluation rpe; + Vector ranks(tria_background.n_active_cells()); + ranks = + Utilities::MPI::this_mpi_process(tria_background.get_communicator()); + + const auto point_ranks = + VectorTools::point_values<1>(mapping, + tria_background, + ranks, + points, + rpe, + VectorTools::EvaluationFlags::min); + + const auto tria = + dynamic_cast *>( + &tria_immersed); + + Assert(tria, ExcNotImplemented()); + + // 3) set partitioning + LinearAlgebra::distributed::Vector partition( + tria->global_active_cell_index_partitioner().lock()); + + unsigned int counter = 0; + for (const auto &cell : tria_immersed.active_cell_iterators()) + if (cell->is_locally_owned()) + partition[cell->global_active_cell_index()] = point_ranks[counter++]; + + partition.update_ghost_values(); + + return partition; + } + +private: + const Triangulation &tria_background; + const MappingQ1 mapping; // TODO +}; + + +template +void +output_mesh(const Triangulation &tria_background, const std::string label) +{ + DataOut data_out_background; + data_out_background.attach_triangulation(tria_background); + + Vector ranks(tria_background.n_active_cells()); + ranks = Utilities::MPI::this_mpi_process(tria_background.get_communicator()); + data_out_background.add_data_vector(ranks, "ranks"); + data_out_background.build_patches(); + data_out_background.write_vtu_in_parallel(label, + tria_background.get_communicator()); +} + + +template +void +test() +{ + const MPI_Comm comm = MPI_COMM_WORLD; + + // create background mesh + parallel::distributed::Triangulation tria_background(comm); + GridGenerator::hyper_cube(tria_background, -1, +1); + tria_background.refine_global(5); + + // create immersed mesh (default partitioning) + parallel::distributed::Triangulation tria_immersed_old(comm); + GridGenerator::hyper_ball(tria_immersed_old, Point(0.1, 0.2), 0.5); + tria_immersed_old.refine_global(5); + + // create immersed mesh with partitioning as in the case of the + // background mesh + MyPolicy policy_0(tria_background); + const auto partition_0 = policy_0.partition(tria_immersed_old); + + const auto construction_data = + TriangulationDescription::Utilities::create_description_from_triangulation( + tria_immersed_old, partition_0); + + parallel::fullydistributed::Triangulation tria_immersed_new(comm); + for (const auto i : tria_immersed_old.get_manifold_ids()) + if (i != numbers::flat_manifold_id) + tria_immersed_new.set_manifold(i, tria_immersed_old.get_manifold(i)); + + tria_immersed_new.create_triangulation(construction_data); + + // output meshes + output_mesh(tria_background, "mesh_background.vtu"); + output_mesh(tria_immersed_old, "mesh_immersed_old.vtu"); + output_mesh(tria_immersed_new, "mesh_immersed_new.vtu"); + + // print statistics + print_statistics(tria_immersed_new); +} + + + +int +main(int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi(argc, argv, 1); + MPILogInitAll all; + + test<2>(); +} diff --git a/tests/fullydistributed_grids/repartitioning_09.with_p4est=true.mpirun=4.output b/tests/fullydistributed_grids/repartitioning_09.with_p4est=true.mpirun=4.output new file mode 100644 index 0000000000..c7c762ad2e --- /dev/null +++ b/tests/fullydistributed_grids/repartitioning_09.with_p4est=true.mpirun=4.output @@ -0,0 +1,27 @@ + +DEAL:0::n_levels: 6 +DEAL:0::n_cells: 762 +DEAL:0::n_active_cells: 572 +DEAL:0::has_hanging_nodes: false +DEAL:0:: + +DEAL:1::n_levels: 6 +DEAL:1::n_cells: 1222 +DEAL:1::n_active_cells: 917 +DEAL:1::has_hanging_nodes: false +DEAL:1:: + + +DEAL:2::n_levels: 6 +DEAL:2::n_cells: 2300 +DEAL:2::n_active_cells: 1726 +DEAL:2::has_hanging_nodes: false +DEAL:2:: + + +DEAL:3::n_levels: 6 +DEAL:3::n_cells: 3976 +DEAL:3::n_active_cells: 2983 +DEAL:3::has_hanging_nodes: false +DEAL:3:: +