From f2a59f59e822074c84bda1afd15ce6128d6dce5d Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Wed, 20 Nov 2024 17:38:23 +0100 Subject: [PATCH] Test point ids. Room for improvement: duplicated cases in edges. --- .../cpgrid/addLgrsOnDistributedGrid_test.cpp | 284 ++++++++++++++---- 1 file changed, 221 insertions(+), 63 deletions(-) diff --git a/tests/cpgrid/addLgrsOnDistributedGrid_test.cpp b/tests/cpgrid/addLgrsOnDistributedGrid_test.cpp index fadf94958..4e661ef70 100644 --- a/tests/cpgrid/addLgrsOnDistributedGrid_test.cpp +++ b/tests/cpgrid/addLgrsOnDistributedGrid_test.cpp @@ -368,46 +368,16 @@ void refinePatch_and_check(Dune::CpGrid& coarse_grid, int local_interior_cell_count = 0; interior_cell_global_ids.reserve(coarse_grid.currentData()[level]->size(0)); - std::unordered_map> cellId_to_pointIds; for (const auto& element: elements(coarse_grid.levelGridView(level), Dune::Partitions::interior)){ - - const auto& cell_global_id = coarse_grid.currentData()[level]->globalIdSet().id(element); interior_cell_global_ids.push_back(cell_global_id); ++local_interior_cell_count; - - std::vector point_ids_vec; - for (int corner = 0; corner < 8; ++corner) { - const auto& point = element.subEntity<3>(corner); - point_ids_vec.push_back(coarse_grid.currentData()[level]->globalIdSet().id(point)); - } - cellId_to_pointIds[cell_global_id] = point_ids_vec; - } - - /*for (const auto& [cell_global_id, point_ids_vec] : cellId_to_pointIds) - { - auto [all_point_ids, displPoint ] = Opm::allGatherv(point_ids_vec, coarse_grid.comm()); - const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); - BOOST_CHECK(all_point_ids_set.size() == 8); } - */ + auto global_level_interior_cell_count = coarse_grid.comm().sum(local_interior_cell_count); auto [all_level_interior_cell_global_ids, displ] = Opm::allGatherv(interior_cell_global_ids, coarse_grid.comm()); const std::set all_level_interior_cell_global_ids_set(all_level_interior_cell_global_ids.begin(), all_level_interior_cell_global_ids.end()); BOOST_CHECK( all_level_interior_cell_global_ids.size() == all_level_interior_cell_global_ids_set.size() ); BOOST_CHECK( global_level_interior_cell_count == static_cast(all_level_interior_cell_global_ids_set.size()) ); - - /* // Check global id is not duplicated for interior points in each refined level grid. - std::vector point_global_ids; - int local_interior_point_count = 0; - interior_point_global_ids.reserve(coarse_grid.currentData()[level]->size(3)); - for (const auto& point : vertices(coarse_grid.levelGridView(level), Dune::Partitions::interior)){ - interior_point_global_ids.push_back(coarse_grid.currentData()[level]->globalIdSet().id(point)); - ++local_interior_point_count; - } - auto global_level_interior_point_count = coarse_grid.comm().sum(local_interior_point_count); - auto [all_level_interior_point_global_ids, displ_point] = Opm::allGatherv(interior_point_global_ids, coarse_grid.comm()); - const std::set all_level_interior_point_global_ids_set(all_level_interior_point_global_ids.begin(), all_level_interior_point_global_ids.end()); - BOOST_CHECK( static_cast(global_level_interior_point_count) == all_level_interior_point_global_ids_set.size() );*/ } // Check global id is not duplicated for interior cells @@ -428,21 +398,6 @@ void refinePatch_and_check(Dune::CpGrid& coarse_grid, BOOST_CHECK( static_cast(allGlobalIds_cells.size()) == global_cells_count); BOOST_CHECK( allGlobalIds_cells.size() == allGlobalIds_cells_set.size() ); - // Check global id is not duplicated for interior points - std::vector localInteriorPointIds_vec; - localInteriorPointIds_vec.reserve(data.back()->size(3)); // more than actually needed since only care about interior points - int local_interior_points_count = 0; - for (const auto& point: vertices(coarse_grid.leafGridView(), Dune::Partitions::interior)) { - localInteriorPointIds_vec.push_back(data.back()->globalIdSet().id(point)); - ++local_interior_points_count; - } - auto global_point_count = coarse_grid.comm().sum(local_interior_points_count); - auto [allGlobalIds_points, displ_point] = Opm::allGatherv(localInteriorPointIds_vec, coarse_grid.comm()); - - const std::set allGlobalIds_points_set(allGlobalIds_points.begin(), allGlobalIds_points.end()); - BOOST_CHECK( static_cast(allGlobalIds_points.size()) == global_point_count); - // BOOST_CHECK( allGlobalIds_points.size() == allGlobalIds_points_set.size() ); - // Local/Global id sets for level grids (level 0, 1, ..., maxLevel). For level grids, local might differ from global id. for (int level = 0; level < coarse_grid.maxLevel() +1; ++level) { @@ -539,6 +494,26 @@ BOOST_AUTO_TEST_CASE(threeLgrs) // Leaf grid view total amount of cells: 10x8x8 -6 + 32 + 27 + 64 = 757 refinePatch_and_check(grid, cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); + // Check global id is not duplicated for points for each LGR + // LGR1 dim 4x2x4 -> 5x3x5 = 75 points + // LGR2 dim 3x3x3 -? 4x4x4 = 64 points + // LGR3 dim 4x4x4 -> 5x5x5 = 125 points + const std::vector& expected_point_ids_per_lgr = { 75, 64, 125}; + for (int lgr = 1; lgr < 4; ++lgr) { + std::vector local_point_ids; + local_point_ids.reserve(expected_point_ids_per_lgr[lgr-1]); + for (const auto& element : elements(grid.levelGridView(lgr))) { + for (int corner = 0; corner < 8; ++corner) + { + const auto& point = element.subEntity<3>(corner); + local_point_ids.push_back(grid.currentData()[lgr]->globalIdSet().id(point)); + } + } + auto [all_point_ids, displPoint ] = Opm::allGatherv(local_point_ids, grid.comm()); + const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); + BOOST_CHECK( static_cast(all_point_ids_set.size()) == expected_point_ids_per_lgr[lgr-1]); + } + // Check global id is not duplicated for points std::vector localPointIds_vec; localPointIds_vec.reserve(grid.currentData().back()->size(3)); @@ -586,13 +561,34 @@ BOOST_AUTO_TEST_CASE(atLeastOneLgr_per_process_attempt) // LGR4 element indices = 27, 31 in rank 3.Total 16 refined cells, 45 points (45-12 = 33 with new global id). grid.addLgrsUpdateLeafView(cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); - // LGR1 dim 1x2x1 (16 refined cells) - // LGR2 dim 1x1x1 (27 refined cells) - // LGR3 dim 1x1x1 (64 refined cells) - // LGR4 dim 1x2x1 (16 refined cells) 3x5x3 + // LGR1 dim 2x4x2 (16 refined cells) + // LGR2 dim 3x3x3 (27 refined cells) + // LGR3 dim 4x4x4 (64 refined cells) + // LGR4 dim 2x4x2 (16 refined cells) // Total global ids in leaf grid view for cells: 36-(6 marked cells) + 16 + 27 + 64 + 16 = 153 // Total global ids in leaf grid view for points: 80 + 33 + 56 + 117 + 33 = 319 refinePatch_and_check(grid, cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); + + // Check global id is not duplicated for points for each LGR + // LGR1 dim 2x4x2 -> 3x5x3 = 45 points + // LGR2 dim 3x3x3 -> 4x4x4 = 64 points + // LGR3 dim 4x4x4 -> 5x5x5 = 125 points + // LGR4 dim 2x4x2 -> 3x5x3 = 45 points + const std::vector& expected_point_ids_per_lgr = { 45, 64, 125, 45}; + for (int lgr = 1; lgr < 5; ++lgr) { + std::vector local_point_ids; + local_point_ids.reserve(expected_point_ids_per_lgr[lgr-1]); + for (const auto& element : elements(grid.levelGridView(lgr))) { + for (int corner = 0; corner < 8; ++corner) + { + const auto& point = element.subEntity<3>(corner); + local_point_ids.push_back(grid.currentData()[lgr]->globalIdSet().id(point)); + } + } + auto [all_point_ids, displPoint ] = Opm::allGatherv(local_point_ids, grid.comm()); + const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); + BOOST_CHECK( static_cast(all_point_ids_set.size()) == expected_point_ids_per_lgr[lgr-1]); + } // Check global id is not duplicated for points std::vector localPointIds_vec; @@ -603,7 +599,6 @@ BOOST_AUTO_TEST_CASE(atLeastOneLgr_per_process_attempt) } auto [allGlobalIds_points, displPoint ] = Opm::allGatherv(localPointIds_vec, grid.comm()); const std::set allGlobalIds_points_set(allGlobalIds_points.begin(), allGlobalIds_points.end()); - // Total global ids in leaf grid view for points: 80 + 33 + 56 + 117 + 33 = 319 BOOST_CHECK( allGlobalIds_points_set.size() == 319 ); } @@ -642,6 +637,39 @@ BOOST_AUTO_TEST_CASE(throw_not_fully_interior_lgr) grid.addLgrsUpdateLeafView(cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); refinePatch_and_check(grid, cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); + + // Check global id is not duplicated for points for each LGR + // LGR1 dim 2x4x2 -> 3x5x3 = 45 points + // LGR2 dim 3x3x3 -> 4x4x4 = 64 points + // LGR3 dim 4x4x4 -> 5x5x5 = 125 points + // LGR4 dim 2x4x2 -> 3x5x3 = 45 points + const std::vector& expected_point_ids_per_lgr = { 45, 64, 125, 45}; + for (int lgr = 1; lgr < 5; ++lgr) { + std::vector local_point_ids; + local_point_ids.reserve(expected_point_ids_per_lgr[lgr-1]); + for (const auto& element : elements(grid.levelGridView(lgr))) { + for (int corner = 0; corner < 8; ++corner) + { + const auto& point = element.subEntity<3>(corner); + local_point_ids.push_back(grid.currentData()[lgr]->globalIdSet().id(point)); + } + } + auto [all_point_ids, displPoint ] = Opm::allGatherv(local_point_ids, grid.comm()); + const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); + BOOST_CHECK( static_cast(all_point_ids_set.size()) == expected_point_ids_per_lgr[lgr-1]); + } + + // Check global id is not duplicated for points + std::vector localPointIds_vec; + localPointIds_vec.reserve(grid.currentData().back()->size(3)); + for (const auto& point : vertices(grid.leafGridView())) { + // Notice that all partition type points are pushed back. Selecting only interior points does not bring us to the expected value. + localPointIds_vec.push_back(grid.currentData().back()->globalIdSet().id(point)); + } + auto [allGlobalIds_points, displPoint ] = Opm::allGatherv(localPointIds_vec, grid.comm()); + const std::set allGlobalIds_points_set(allGlobalIds_points.begin(), allGlobalIds_points.end()); + // Total global ids in leaf grid view for points: 80 + 33 + 56 + 117 + 33 = 319 + BOOST_CHECK( allGlobalIds_points_set.size() == 319 ); } } @@ -664,9 +692,6 @@ BOOST_AUTO_TEST_CASE(globalRefine2) BOOST_AUTO_TEST_CASE(distributed_lgr) { - // Only for testing assignment of new global ids for refined entities (cells and point belonging to - // refined level grids). - // Create a grid Dune::CpGrid grid; const std::array cell_sizes = {1.0, 1.0, 1.0}; @@ -692,19 +717,66 @@ BOOST_AUTO_TEST_CASE(distributed_lgr) const std::vector> endIJK_vec = {{3,1,1}}; const std::vector lgr_name_vec = {"LGR1"}; // LGR1 element indices = 1 (rank 0), 2 (rank 2). Total 16 refined cells, 45 points (45-12 = 33 with new global id). - // LGR1 dim 2x1x1 (16 refined cells) (45 points - only 33 new points) + // LGR1 dim 4x2x2 (16 refined cells) (45 points - only 33 new points) grid.addLgrsUpdateLeafView(cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); refinePatch_and_check(grid, cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); + + // Check global id is not duplicated for points for each LGR + // LGR1 dim 4x2x2 -> 5x3x3 = 45 points + std::vector local_point_ids; + local_point_ids.reserve(45); // expected_point_ids in LGR1 + for (const auto& element : elements(grid.levelGridView(1))) { + for (int corner = 0; corner < 8; ++corner) + { + const auto& point = element.subEntity<3>(corner); + local_point_ids.push_back(grid.currentData()[1]->globalIdSet().id(point)); + } + } + auto [all_point_ids, displPoint ] = Opm::allGatherv(local_point_ids, grid.comm()); + const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); + // Coarse cells 1 in rank 0 and 2 in rank 2 share an I_FACE where (LGR1_dim[1]+1)*(LGR1_dim[2]+ 1), + // here (2 +1)*(2 +1) = 9 points lying on, 4 of them being the 4 corners of the coarse I_FACE shared + // by cell 1 and cell 2. Leaving us with 9 - 4 = 5 duplicated ids. That means that for points lying on + // shared faces of cells to be refined present duplicated ids. In this case, the "unfortunate" expected + // point ids count is 45 (desired value) + 5 (duplicated laying on shared I_FACE) = 50. + BOOST_CHECK( static_cast(all_point_ids_set.size()) == 50); + // + // Global cell ids of cells to be refined = {1, 2} + // cell 1 = { interior in P0, overlap in P1, overlap in P2, does not exist in P3} + // cell 2 = { overlap in P0, does not exist in P1, interior in P2, overlap in P3} + // Remark: the LGR is distributed in only two of the four processes, P0 and P2. However, + // - P1 does NOT see cell 2, but sees cell 1 + // - P3 does NOT see cell 1, but sees cell 2 + // For completition: + // - P0 does NOT see the LGR at all. + // - P1 does NOT see cell 2, but sees cell 1 + // - P2 does NOT see the LGR at all. + // - P3 does NOT see cell 1, but sees cell 2 + /** Current approach avoids duplicated point ids when + // 1. the LGR is distributed in P_{i_0}, ..., P_{i_n}, with n+1 < grid.comm().size(), + // AND + // 2. there is no coarse cell seen by a process P with P != P_{i_j}, j = 0, ..., n. + // Otherwise, there will be duplicated point ids. + // + // TO DO: improve communication step to correct ids to avoid duplicated point ids when at least on of the process.*/ + + // Check global id is not duplicated for points + std::vector localPointIds_vec; + localPointIds_vec.reserve(grid.currentData().back()->size(3)); + for (const auto& point : vertices(grid.leafGridView())) { + // Notice that all partition type points are pushed back. Selecting only interior points does not bring us to the expected value. + localPointIds_vec.push_back(grid.currentData().back()->globalIdSet().id(point)); + } + auto [allGlobalIds_points, displPointLeaf ] = Opm::allGatherv(localPointIds_vec, grid.comm()); + const std::set allGlobalIds_points_set(allGlobalIds_points.begin(), allGlobalIds_points.end()); + // Total global ids in leaf grid view for points: 80 + (45 - 12) + 5 (undesired duplicated ids on shared I_FACE) = 118 + BOOST_CHECK( allGlobalIds_points_set.size() == 118 ); } } - BOOST_AUTO_TEST_CASE(distributed_lgr_II) { - // Only for testing assignment of new global ids for refined entities (cells and point belonging to - // refined level grids). - // Create a grid Dune::CpGrid grid; const std::array cell_sizes = {1.0, 1.0, 1.0}; @@ -733,14 +805,48 @@ BOOST_AUTO_TEST_CASE(distributed_lgr_II) grid.addLgrsUpdateLeafView(cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); refinePatch_and_check(grid, cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); + + // Check global id is not duplicated for points for each LGR + // LGR1 dim 6x2x2 -> 7x3x3 = 63 points + std::vector local_point_ids; + local_point_ids.reserve(63); // expected_point_ids in LGR1 + for (const auto& element : elements(grid.levelGridView(1))) { + for (int corner = 0; corner < 8; ++corner) + { + const auto& point = element.subEntity<3>(corner); + local_point_ids.push_back(grid.currentData()[1]->globalIdSet().id(point)); + } + } + auto [all_point_ids, displPoint ] = Opm::allGatherv(local_point_ids, grid.comm()); + const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); + BOOST_CHECK( static_cast(all_point_ids_set.size()) == 63); + // Difference with preveious test case: + // Global cell ids of cells to be refined = {8, 9, 10} + // cell 8 = { interior in P0, does not exist in P1, does not exist in P2, does not exist in P3} + // cell 9 = { interior in P0, does not exist in P1, overlap in P2, does not exist in P3} + // cell 10 = { overlap in P0, does not exist in P1, interior in P2, does not exist in P3} + // Remark: the LGR is distributed only in 2 processes which are the only two processes seeing these cells. + // - P0 sees the entire LGR. + // - P1 does NOT see the LGR at all. + // - P2 does NOT see cell 8, but NO OTHER process sees cell 8 since it's fully inteior of P0. + // - P3 does NOT see the LGR at all. + + // Check global id is not duplicated for points + std::vector localPointIds_vec; + localPointIds_vec.reserve(grid.currentData().back()->size(3)); + for (const auto& point : vertices(grid.leafGridView())) { + // Notice that all partition type points are pushed back. Selecting only interior points does not bring us to the expected value. + localPointIds_vec.push_back(grid.currentData().back()->globalIdSet().id(point)); + } + auto [allGlobalIds_points, displPointLeaf ] = Opm::allGatherv(localPointIds_vec, grid.comm()); + const std::set allGlobalIds_points_set(allGlobalIds_points.begin(), allGlobalIds_points.end()); + // Total global ids in leaf grid view for points: 80 + (63 - 16) = 127 + BOOST_CHECK( allGlobalIds_points_set.size() == 127 ); } } BOOST_AUTO_TEST_CASE(distributed_in_all_ranks_lgr) { - // Only for testing assignment of new global ids for refined entities (cells and point belonging to - // refined level grids). - // Create a grid Dune::CpGrid grid; const std::array cell_sizes = {1.0, 1.0, 1.0}; @@ -763,7 +869,7 @@ BOOST_AUTO_TEST_CASE(distributed_in_all_ranks_lgr) const std::vector> startIJK_vec = {{1,0,0}}; const std::vector> endIJK_vec = {{3,2,2}}; const std::vector lgr_name_vec = {"LGR1"}; - // LGR1 element indices = {1,2,5,6,13,14,17,18} where + // LGR1 element indices = {1,2,5,6,13,14,17,18www} where // 1,5 in rank 0, // 13,17 in rank 1, // 2,6,18 in rank 2, @@ -772,6 +878,58 @@ BOOST_AUTO_TEST_CASE(distributed_in_all_ranks_lgr) // 64 new refined cells. 5x5x5 = 125 points (only 98 = 125 - 3x3x3 parent corners new points - new global ids). grid.addLgrsUpdateLeafView(cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); refinePatch_and_check(grid, cells_per_dim_vec, startIJK_vec, endIJK_vec, lgr_name_vec); + + // Check global id is not duplicated for points for each LGR + // LGR1 dim 4x4x4 -> 5x5x5 = 125 points + std::vector local_point_ids; + local_point_ids.reserve(125); // expected_point_ids in LGR1 + for (const auto& element : elements(grid.levelGridView(1))) { + for (int corner = 0; corner < 8; ++corner) + { + const auto& point = element.subEntity<3>(corner); + local_point_ids.push_back(grid.currentData()[1]->globalIdSet().id(point)); + } + } + auto [all_point_ids, displPoint ] = Opm::allGatherv(local_point_ids, grid.comm()); + const std::set all_point_ids_set(all_point_ids.begin(), all_point_ids.end()); + std::cout<< all_point_ids_set.size() << std::endl; + // Difference with preveious test case: + // Global cell ids of cells to be refined = {1,2,5,6,13,14,17,18} + // cell 1 = { interior in P0, overlap in P1, overlap in P2, does not exist in P3} + // cell 2 = { overlap in P0, does not exist in P1, interior in P2, overlap in P3} + // cell 5 = { interior in P0, overlap in P1, overlap in P2, does not exist in P3} + // cell 6 = { overlap in P0, does not exist in P1, interior in P2, does not exist in P3} + // cell 13 = { overlap in P0, interior in P1, does not exist in P2, overlap in P3} + // cell 14 = { does not exist in P0, overlap in P1, overlap in P2, interior in P3} + // cell 17 = { overlap in P0, interior in P1, overlap in P2, does not exist in P3} + // cell 18 = { does not exist in P0, overlap in P1, interior in P2, overlap in P3} + // Remark: the LGR is distributed in ALL processes BUT + // - P0 does NOT see cell 18, but sees all the others. + // - P1 does NOT see cells 2 and 6, but sees the rest of them. + // - P2 does NOT see cell 13, but sees all the others. + // - P3 does NOT see cell 1, 5 and 17, but sees all the others. + // Remark: the LGR is distributed only in 2 processes which are the only two processes seeing these cells. + // P0 creates +1 duplicated id (middle point edge) + // P1 creates +2 duplicated ids (middle point of an edge in cell 2, cell 6 respectively). + // P2 creates +1 duplicated id (middle point edge) + // P3 creates: + // +5 duplicated ids on {I_FACE, false} cell 2 (seen in P3) [equivalent face: {I_FACE, true} of unseen-in-P3 cell 1] + // +1 duplicated id on edge unseen-in-P3 cell 5 + // +5 duplicated ids on {J_FACE, true} cell 13 (seen in P3) [equivalent face: {J_FACE, false} of unseen-in-P3 cell 17] + // +5 duplicated ids on {I_FACE, false} cell 18 (seen in P3) [equivalent face: {I_FACE, true} of unseen-in-P3 cell 17] + BOOST_CHECK( static_cast(all_point_ids_set.size()) == 145); // Desired value: 125; 20 (=1+2+1+5+1+5+5) duplicated ids. + + // Check global id is not duplicated for points + std::vector localPointIds_vec; + localPointIds_vec.reserve(grid.currentData().back()->size(3)); + for (const auto& point : vertices(grid.leafGridView())) { + // Notice that all partition type points are pushed back. Selecting only interior points does not bring us to the expected value. + localPointIds_vec.push_back(grid.currentData().back()->globalIdSet().id(point)); + } + auto [allGlobalIds_points, displPointLeaf ] = Opm::allGatherv(localPointIds_vec, grid.comm()); + const std::set allGlobalIds_points_set(allGlobalIds_points.begin(), allGlobalIds_points.end()); + // Total global ids in leaf grid view for points: 80 + (125 - 27) = 178 desired value; +20 duplicated ids. + BOOST_CHECK( allGlobalIds_points_set.size() == 198 ); } }