From aed6f6450b2bde9ff67c561fc0603b8a7ec58fb9 Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Mon, 14 Jul 2025 15:50:39 +0200 Subject: [PATCH 1/6] Avoid implicit conversion from Entity to EntityRep when calling id, rebase --- CMakeLists.txt | 1 + CMakeLists_files.cmake | 1 + opm/grid/cpgrid/Indexsets.hpp | 109 +++-- ...or_index_for_entity_and_entityRep_test.cpp | 402 ++++++++++++++++++ 4 files changed, 468 insertions(+), 45 deletions(-) create mode 100644 tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f34438b5c5..875ffabadf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ if(MPI_FOUND) add_test(distribute_level_zero_from_grid_with_lgrs_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/distribute_level_zero_from_grid_with_lgrs_test) add_test(distribution_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/distribution_test) add_test(grid_global_id_set_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/grid_global_id_set_test) + add_test(id_or_index_for_entity_and_entityRep_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/id_or_index_for_entity_and_entityRep_test) add_test(lgr_cell_id_sync_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/lgr_cell_id_sync_test) add_test(logicalCartesianSize_and_refinement_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/logicalCartesianSize_and_refinement_test) if(Boost_VERSION_STRING VERSION_GREATER 1.53) diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index c8ca7ec6bc..b462b1b93e 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -84,6 +84,7 @@ list(APPEND TEST_SOURCE_FILES tests/cpgrid/lgr/addLgrsOnDistributedGrid_test.cpp tests/cpgrid/lgr/communicate_distributed_grid_with_lgrs_test.cpp tests/cpgrid/lgr/distribute_level_zero_from_grid_with_lgrs_and_wells_test.cpp + tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp tests/cpgrid/lgr/distribute_level_zero_from_grid_with_lgrs_test.cpp tests/cpgrid/lgr/global_refine_test.cpp tests/cpgrid/lgr/grid_global_id_set_test.cpp diff --git a/opm/grid/cpgrid/Indexsets.hpp b/opm/grid/cpgrid/Indexsets.hpp index 4d36ab5908..5e3853c816 100644 --- a/opm/grid/cpgrid/Indexsets.hpp +++ b/opm/grid/cpgrid/Indexsets.hpp @@ -15,7 +15,7 @@ /* Copyright 2009, 2010 SINTEF ICT, Applied Mathematics. -Copyright 2009, 2010, 2022 Equinor ASA. +Copyright 2009, 2010, 2022, 2025 Equinor ASA. This file is part of The Open Porous Media project (OPM). @@ -42,6 +42,7 @@ along with OPM. If not, see . #include "Intersection.hpp" #include +#include #include #include @@ -114,7 +115,7 @@ namespace Dune int size(GeometryType type) const { if (type.isCube()) { - return size(3 - type.dim()); // return grid_.size(type); + return size(3 - type.dim()); } else { return 0; } @@ -127,7 +128,7 @@ namespace Dune /// @return int size(int codim) const { - return size_codim_map_[codim]; //grid_.size(codim) + return size_codim_map_[codim]; } @@ -186,12 +187,10 @@ namespace Dune template bool contains(const EntityType& e) const { - // return index(e) >= 0 && index(e) < grid_.size(EntityType::codimension); //EntityType::codimension == 0; return index(e) >= 0 && index(e) < this->size(EntityType::codimension); } private: - // const CpGridData& grid_; Types geom_types_[4]; std::array size_codim_map_{0,0,0,0}; }; @@ -201,7 +200,6 @@ namespace Dune { friend class ReversePointGlobalIdSet; friend class Dune::cpgrid::CpGridData; - //friend class Dune::cpgrid::LevelGlobalIdSet; Not needed due to repeated code in LevelGlobalIdSet (computeId_cell and computeId_point) public: typedef std::int64_t IdType; @@ -219,30 +217,23 @@ namespace Dune { } - template - IdType id(const typename Codim::Entity& e) const - { - if constexpr (cd == 0) - return computeId_cell(e); - else if constexpr (cd == 1) - return computeId(e); - else if constexpr (cd == 3) - return computeId_point(e); - else - static_assert(AlwaysFalse>::value, - "IdSet::id not implemented for codims other thatn 0, 1, and 3."); - } - + // Avoid implicit derived-to-base conversion: use Entity instead of EntityRep. + // Also, ensure the correct ids are used for Entity<0> and Entity<3> in CpGrid with LGRs. template IdType id(const EntityType& e) const { - return id(e); - } - - template - IdType id(const cpgrid::EntityRep& e) const - { - return computeId(e); + if constexpr (std::is_same_v>){ + return computeId_cell(e); + } + else if constexpr (std::is_same_v>) { + return computeId_point(e); + } + else if (std::is_same_v>) { + OPM_THROW(std::logic_error, "IdSet::id not implemented for codims other thatn 0, 1, and 3."); + } + else { // Entity<1> and EntityRep fall in this case. + return computeId(e); + } } /// return id of intersection (here face number) @@ -398,8 +389,11 @@ namespace Dune LevelGlobalIdSet() : idSet_(), view_() {} - template - IdType id(const typename Codim::Entity& e) const + + // Avoid implicit derived-to-base conversion (use Entity instead of EntityRep), + // by overloading id() with explicit types Entity, codim = 0,1, and 3. + // Ensure the correct ids are used for Entity<0> and Entity<3> in CpGrid with LGRs. + IdType id(const cpgrid::Entity<0>& e) const { assert(view_ == e.pgrid_); // We need to ask the local id set with the full entity @@ -410,22 +404,44 @@ namespace Dune else // This a parallel grid and we need to use the mapping // build from the ids of the sequential grid - return this->template getMapping()[e.index()]; + return this->template getMapping<0>()[e.index()]; } - - template - IdType id(const EntityRep& e) const + + IdType id(const cpgrid::Entity<1>& e) const + { + assert(view_ == e.pgrid_); + // Entity<1> is not supported for CpGrid, so it's impossible to determine the level + // or other refinement-related information. As a result, implicit conversion to + // EntityRep<1> will occur, and id(EntityRep<1>) will be used. + if(idSet_) + return idSet_->id(e); + else + // This a parallel grid and we need to use the mapping + // build from the ids of the sequential grid + return this->template getMapping<1>()[e.index()]; + } + + IdType id(const cpgrid::Entity<3>& e) const { + assert(view_ == e.pgrid_); + // We need to ask the local id set with the full entity + // as it needs to be able to determine the level and other + // things that are not available in EntityRep. if(idSet_) return idSet_->id(e); else - return this->template getMapping()[e.index()]; + // This a parallel grid and we need to use the mapping + // build from the ids of the sequential grid + return this->template getMapping<3>()[e.index()]; } - template - IdType id(const EntityType& e) const + template + IdType id(const cpgrid::EntityRep& e) const { - return id(e); + if(idSet_) + return idSet_->id(e); + else + return computeId(e); } template @@ -475,6 +491,15 @@ namespace Dune private: std::shared_ptr idSet_; const CpGridData* view_; + + template + IdType computeId(const cpgrid::EntityRep& e) const + { + IdType myId = 0; + for( int c=0; cindexSet().size( c ); + return myId + e.index(); + } }; /*! @@ -500,18 +525,12 @@ namespace Dune explicit GlobalIdSet(const CpGridData& view); - template - IdType id(const typename Codim::Entity& e) const + template + IdType id(const cpgrid::Entity& e) const { return levelIdSet(e.pgrid_).id(e); } - template - IdType id(const EntityType& e) const - { - return id(e); - } - template IdType subId(const typename Codim<0>::Entity& e, int i) const { diff --git a/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp b/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp new file mode 100644 index 0000000000..8c6c99baab --- /dev/null +++ b/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp @@ -0,0 +1,402 @@ +//=========================================================================== +/* + Copyright 2025 Equinor ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ +#include "config.h" + +#define BOOST_TEST_MODULE TemplateOverloadsForIdAndIndexTests +#include + +#include + +#include + +struct Fixture +{ + Fixture() + { + int m_argc = boost::unit_test::framework::master_test_suite().argc; + char** m_argv = boost::unit_test::framework::master_test_suite().argv; + Dune::MPIHelper::instance(m_argc, m_argv); + Opm::OpmLog::setupSimpleDefaultLogging(); + } +}; + +BOOST_GLOBAL_FIXTURE(Fixture); + +template +void checkEntityRepAndEntityIndices(const EntityRange& entities, const IndexSet& indexSet) +{ + for (const auto& entity : entities) { + const auto entityRep = Dune::cpgrid::EntityRep(entity.index(), true); + BOOST_CHECK_EQUAL(indexSet.index(entityRep), indexSet.index(entity)); + } +} + +template +bool checkEntityRepAndEntityIds(const EntityRange& entities, const IdSet& idSet) +{ + for (const auto& entity : entities) { + const auto entityRep = Dune::cpgrid::EntityRep(entity.index(), true); + if (idSet.id(entityRep) != idSet.id(entity)){ + return false; + } + } + return true; +} + +void entityRepIndexAndEntityIndexCoincide(const Dune::CpGrid& grid) +{ + const auto& leafIndexSet = grid.leafIndexSet(); + const auto leafView = grid.leafGridView(); + checkEntityRepAndEntityIndices<0>(Dune::elements(leafView), leafIndexSet); + checkEntityRepAndEntityIndices<3>(Dune::vertices(leafView), leafIndexSet); + + for (int level = 0; level <= grid.maxLevel(); ++level) { + const auto& levelIndexSet = grid.levelIndexSet(level); + const auto levelView = grid.levelGridView(level); + checkEntityRepAndEntityIndices<0>(Dune::elements(levelView), levelIndexSet); + checkEntityRepAndEntityIndices<3>(Dune::vertices(levelView), levelIndexSet); + } +} + +void globalIdsEntityRepAndEntityInLevelZeroGrid(const Dune::CpGrid& grid, bool coincide) +{ + const auto levelZeroView = grid.levelGridView(0); + const auto& levelZeroGlobalIdSet = grid.currentData()[0]->globalIdSet(); + BOOST_CHECK_EQUAL( checkEntityRepAndEntityIds<0>(Dune::elements(levelZeroView), levelZeroGlobalIdSet), coincide); + BOOST_CHECK_EQUAL( checkEntityRepAndEntityIds<3>(Dune::vertices(levelZeroView), levelZeroGlobalIdSet), coincide); +} + +void localIdsEntityRepAndEntityInLevelZeroGrid(const Dune::CpGrid& grid, bool coincide) +{ + const auto levelZeroView = grid.levelGridView(0); + const auto& levelZeroLocalIdSet = grid.currentData()[0]->localIdSet(); + BOOST_CHECK_EQUAL( checkEntityRepAndEntityIds<0>(Dune::elements(levelZeroView), levelZeroLocalIdSet), coincide); + BOOST_CHECK_EQUAL( checkEntityRepAndEntityIds<3>(Dune::vertices(levelZeroView), levelZeroLocalIdSet), coincide); +} + +void localIdsEntityRepAndEntityInRefinedLevelGrids(const Dune::CpGrid& grid, bool coincide) +{ + for (int level = 1; level <= grid.maxLevel(); ++level) { + const auto levelView = grid.levelGridView(level); + if (levelView.size(0)>0) { + const auto& levelLocalIdSet = grid.currentData()[level]->localIdSet(); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(levelView), levelLocalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(levelView), levelLocalIdSet), coincide); + } + } +} + +void localIdsEntityRepAndEntityInLeafGrid(const Dune::CpGrid& grid, bool coincide) +{ + const auto leafView = grid.leafGridView(); + const auto& leafLocalIdSet = grid.currentData().back()->localIdSet(); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(leafView), leafLocalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(leafView), leafLocalIdSet), coincide); +} + +void globalIdsEntityRepAndEntityInRefinedLevelGrids(const Dune::CpGrid& grid, bool coincide) +{ + for (int level = 1; level <= grid.maxLevel(); ++level) { + const auto levelView = grid.levelGridView(level); + if (levelView.size(0)>0) { + const auto& levelGlobalIdSet = grid.currentData()[level]->globalIdSet(); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(levelView), levelGlobalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(levelView), levelGlobalIdSet), coincide); + } + } +} + +void globalIdsEntityRepAndEntityInLeafGrid(const Dune::CpGrid& grid, bool coincide) +{ + const auto leafView = grid.leafGridView(); + if (leafView.size(0)>0) { + const auto& leafGlobalIdSet = grid.currentData().back()->globalIdSet(); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(leafView), leafGlobalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(leafView), leafGlobalIdSet), coincide); + } +} + +BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids_viaAddLgrs) +{ + Dune::CpGrid grid; + grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); + + /** BEFORE adding LGRs */ + // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + if (grid.comm().size() == 1) { // Serial + grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, + /* startIJK = */ {{1,1,1}}, + /* endIJK = */ {{3,2,2}}, // block cell indices = {17, 18} + /* lgr_name = */ {"LGR1"}); + /** After adding LGRs in serial */ + // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + } + else { // Parallel + grid.loadBalance(); + grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, + /* startIJK = */ {{1,1,1}}, + /* endIJK = */ {{3,2,2}}, // block cell indices = {17, 18} + /* lgr_name = */ {"LGR1"}); + grid.switchToGlobalView(); + // Synchronization of cell ids required that both the global and the distributed view + // contained the same LGRs. + grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, + /* startIJK = */ {{1,1,1}}, + /* endIJK = */ {{3,2,2}}, // block cell global ids = {17, 18} + /* lgr_name = */ {"LGR1"}); + grid.switchToDistributedView(); + + /** AFTER adding LGRs in parallel and BEFORE synchronozing cell ids */ + // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + grid.syncDistributedGlobalCellIds(); + + /** AFTER adding LGRs in parallel and AFTER synchronozing cell ids */ + // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + } + // Serial & Parallel, after adding LGRs in the global or distributed grid. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide! */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide! */); + + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); +} + + +BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids_viaAdapt) +{ + Dune::CpGrid grid; + grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); + + /** BEFORE calling adapt() */ + // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + if (grid.comm().size()>1) { + grid.loadBalance(); + } + + std::unordered_set markedCells = {17,18}; // parent cell global ids + // Mark selected elements for refinement. In this moment, level zero and leaf grids coincide. + for (const auto& element : elements(grid.leafGridView())) { + const auto& id = grid.globalIdSet().id(element); + if (markedCells.count(id) > 0) { + grid.mark(1, element); + } + } + grid.preAdapt(); + grid.adapt(); // Default subdivisions per cell 2x2x2 in x-,y-, and z-direction. + grid.postAdapt(); + + if (grid.comm().size() == 1) { + // Serial: Glocal ids in refined level and leaf grids for Entity and EntityRep + // with codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide! */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide! */); + + /** After refinement via adapt() in serial */ + // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + } + else { + /** AFTER refinement via adapt() in parallel and BEFORE synchronozing cell ids */ + // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + grid.switchToGlobalView(); + // Note: Synchronization of cell ids required that both the global and the distributed view + // contained the same LGRs. + // Mark selected elements for refinement. From level zero grid. + for (const auto& element : elements(grid.levelGridView(0))) { + const auto& id = grid.globalIdSet().id(element); + if (markedCells.count(id) > 0) { + grid.mark(1, element); + } + } + grid.preAdapt(); + grid.adapt(); // Default subdivisions per cell 2x2x2 in x-,y-, and z-direction. + grid.postAdapt(); + grid.switchToDistributedView(); + + /** AFTER refinement via adapt() in parallel and BEFORE synchronozing cell ids */ + // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + } +} + +BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids_viaGlobalRefine) +{ + Dune::CpGrid grid; + grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); + + /** BEFORE calling globalRefine(1) */ + // Note: in distributed grid, globalRefine(n) with n>1 is not supported yet. + // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + if (grid.comm().size()>1) { + grid.loadBalance(); + } + + grid.globalRefine(1); + + if (grid.comm().size() == 1) { + /** After global refinement in serial */ + // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + } + else { + /** AFTER global refinement in parallel and BEFORE synchronozing cell ids */ + // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + grid.switchToGlobalView(); + // Note: Synchronization of cell ids required that both the global and the distributed view + // contained the same LGRs. + grid.globalRefine(1); + grid.switchToDistributedView(); + + /** AFTER global refinement in parallel and AFTER synchronozing cell ids */ + // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + + // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + } +} From 4877b3c1c4064f188c66fc60d2b8633cb63e8f22 Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Fri, 25 Jul 2025 11:42:25 +0200 Subject: [PATCH 2/6] Gather checks for serial/parallel before/after refinement --- ...or_index_for_entity_and_entityRep_test.cpp | 301 +++++++----------- 1 file changed, 113 insertions(+), 188 deletions(-) diff --git a/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp b/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp index 8c6c99baab..9b49780f8a 100644 --- a/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp +++ b/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp @@ -55,7 +55,7 @@ bool checkEntityRepAndEntityIds(const EntityRange& entities, const IdSet& idSet) const auto entityRep = Dune::cpgrid::EntityRep(entity.index(), true); if (idSet.id(entityRep) != idSet.id(entity)){ return false; - } + } } return true; } @@ -133,118 +133,118 @@ void globalIdsEntityRepAndEntityInLeafGrid(const Dune::CpGrid& grid, bool coinci } } -BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids_viaAddLgrs) +void serialBeforeRefinementChecks(const Dune::CpGrid& grid) { - Dune::CpGrid grid; - grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); - - /** BEFORE adding LGRs */ - // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. + /** BEFORE refinement via addLgrsUpdateLeafView, adapt, or globalRefine(1) in serial */ + // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. entityRepIndexAndEntityIndexCoincide(grid); localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); - + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); globalIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); +} + +void parallelBeforeRefinementChecks(const Dune::CpGrid& grid) +{ + /** BEFORE refinement via addLgrsUpdateLeafView, adapt, or globalRefine(1) in parallel */ + // All indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. + entityRepIndexAndEntityIndexCoincide(grid); + + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); + + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); + /** In constrast to serial, in level zero grid id(Entity) != id(EntityRep) */ + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); + /** In contrast to the serial, in leaf grid (here, level zero) id(Entity) != id(EntityRep) */ + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* coincide */); +} + + +void serialAfterRefinementChecks(const Dune::CpGrid& grid) +{ + /** After refinement via addLgrsUpdateLeafView, adapt, or globalRefine(1) in serial */ + entityRepIndexAndEntityIndexCoincide(grid); + + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); +} + +void parallelAfterRefinementChecks(const Dune::CpGrid& grid) +{ + /** After refinement via addLgrsUpdateLeafView, adapt, or globalRefine(1) in parallel */ + entityRepIndexAndEntityIndexCoincide(grid); + + localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); + localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + + globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); +} + +BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids_viaAddLgrs) +{ + Dune::CpGrid grid; + grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); + serialBeforeRefinementChecks(grid); + + if (grid.comm().size()>1) { + grid.loadBalance(); + parallelBeforeRefinementChecks(grid); + } + + grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, + /* startIJK = */ {{1,1,1}}, + /* endIJK = */ {{3,2,2}}, // block cell indices = {17, 18} + /* lgr_name = */ {"LGR1"}); if (grid.comm().size() == 1) { // Serial - grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, - /* startIJK = */ {{1,1,1}}, - /* endIJK = */ {{3,2,2}}, // block cell indices = {17, 18} - /* lgr_name = */ {"LGR1"}); - /** After adding LGRs in serial */ - // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + serialAfterRefinementChecks(grid); } else { // Parallel - grid.loadBalance(); - grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, - /* startIJK = */ {{1,1,1}}, - /* endIJK = */ {{3,2,2}}, // block cell indices = {17, 18} - /* lgr_name = */ {"LGR1"}); + // BEFORE applying the same refinement to the global view. + parallelAfterRefinementChecks(grid); + grid.switchToGlobalView(); - // Synchronization of cell ids required that both the global and the distributed view - // contained the same LGRs. + // Synchronizing cell ids requires that both the global + // and distributed views undergo the same refinement process. grid.addLgrsUpdateLeafView( /* cells_per_dim = */ {{2,2,2}}, /* startIJK = */ {{1,1,1}}, /* endIJK = */ {{3,2,2}}, // block cell global ids = {17, 18} /* lgr_name = */ {"LGR1"}); grid.switchToDistributedView(); - - /** AFTER adding LGRs in parallel and BEFORE synchronozing cell ids */ - // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - + + // BEFORE synchronizing cell ids + parallelAfterRefinementChecks(grid); + grid.syncDistributedGlobalCellIds(); - /** AFTER adding LGRs in parallel and AFTER synchronozing cell ids */ - // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + // AFTER synchronizing cell ids + parallelAfterRefinementChecks(grid); } - // Serial & Parallel, after adding LGRs in the global or distributed grid. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide! */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide! */); - - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); } - BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids_viaAdapt) { Dune::CpGrid grid; grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); + serialBeforeRefinementChecks(grid); - /** BEFORE calling adapt() */ - // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); - - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); - if (grid.comm().size()>1) { grid.loadBalance(); + parallelBeforeRefinementChecks(grid); } std::unordered_set markedCells = {17,18}; // parent cell global ids @@ -259,45 +259,16 @@ BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids grid.adapt(); // Default subdivisions per cell 2x2x2 in x-,y-, and z-direction. grid.postAdapt(); - if (grid.comm().size() == 1) { - // Serial: Glocal ids in refined level and leaf grids for Entity and EntityRep - // with codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide! */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide! */); - - /** After refinement via adapt() in serial */ - // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + if (grid.comm().size() == 1) { // Serial + serialAfterRefinementChecks(grid); } - else { - /** AFTER refinement via adapt() in parallel and BEFORE synchronozing cell ids */ - // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + else { // Parallel + // BEFORE applying the same refinement to the global view + parallelAfterRefinementChecks(grid); grid.switchToGlobalView(); - // Note: Synchronization of cell ids required that both the global and the distributed view - // contained the same LGRs. - // Mark selected elements for refinement. From level zero grid. + // Synchronizing cell ids requires that both the global + // and distributed views undergo the same refinement process. for (const auto& element : elements(grid.levelGridView(0))) { const auto& id = grid.globalIdSet().id(element); if (markedCells.count(id) > 0) { @@ -309,19 +280,13 @@ BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids grid.postAdapt(); grid.switchToDistributedView(); - /** AFTER refinement via adapt() in parallel and BEFORE synchronozing cell ids */ - // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + // BEFORE synchronizing cell ids + parallelAfterRefinementChecks(grid); + + //grid.syncDistributedGlobalCellIds(); + + // AFTER synchronozing cell ids + parallelAfterRefinementChecks(grid); } } @@ -329,74 +294,34 @@ BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids { Dune::CpGrid grid; grid.createCartesian(/* grid_dim = */ {4,3,3}, /* cell_sizes = */ {1.0, 1.0, 1.0}); + serialBeforeRefinementChecks(grid); - /** BEFORE calling globalRefine(1) */ - // Note: in distributed grid, globalRefine(n) with n>1 is not supported yet. - // All indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); - - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, true /* coincide */); - if (grid.comm().size()>1) { grid.loadBalance(); + parallelBeforeRefinementChecks(grid); } grid.globalRefine(1); - if (grid.comm().size() == 1) { - /** After global refinement in serial */ - // Indices and local/global ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide.ww - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + if (grid.comm().size() == 1) { // Serial + serialAfterRefinementChecks(grid); } - else { - /** AFTER global refinement in parallel and BEFORE synchronozing cell ids */ - // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - + else { // Parallel + // BEFORE applying the same refinement to the global view. + parallelAfterRefinementChecks(grid); + grid.switchToGlobalView(); - // Note: Synchronization of cell ids required that both the global and the distributed view - // contained the same LGRs. + // Synchronizing cell ids requires that both the global + // and distributed views undergo the same refinement process. grid.globalRefine(1); grid.switchToDistributedView(); - /** AFTER global refinement in parallel and AFTER synchronozing cell ids */ - // Indices and local ids for Entity and EntityRep codim = 0 and 3 coincide. - entityRepIndexAndEntityIndexCoincide(grid); - localIdsEntityRepAndEntityInLevelZeroGrid(grid, true /* coincide */); - - // Local ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - localIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - localIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); - - // Global ids Entity and EntityRep codim = 0 and 3 DO NOT coincide. - globalIdsEntityRepAndEntityInLevelZeroGrid(grid, false /* coincide */); // IN SERIAL TRUE - globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); - globalIdsEntityRepAndEntityInLeafGrid(grid, false /* DO NOT coincide */); + // BEFORE synchronizing cell ids + parallelAfterRefinementChecks(grid); + + grid.syncDistributedGlobalCellIds(); + + // AFTER synchronizing cell ids + parallelAfterRefinementChecks(grid); } } From 9ebccd1c2be1562a4a8f449d9a090eb1a7744878 Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Fri, 1 Aug 2025 14:43:56 +0200 Subject: [PATCH 3/6] Fix empty pointMapping_ when synchrCellIds in adapt() calls --- opm/grid/cpgrid/CpGrid.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/opm/grid/cpgrid/CpGrid.cpp b/opm/grid/cpgrid/CpGrid.cpp index 8eaac4e59e..26f757d700 100644 --- a/opm/grid/cpgrid/CpGrid.cpp +++ b/opm/grid/cpgrid/CpGrid.cpp @@ -2695,15 +2695,17 @@ void CpGrid::syncDistributedGlobalCellIds() const int maxLevel = this->maxLevel(); - // Preallocate syncCellIds + // Preallocate syncCellIds (and vertexIds, which will NOT be synchrinized) std::vector> syncCellIds(maxLevel); + std::vector> vertexIds(maxLevel); for (int level = 1; level <= maxLevel; ++level) { syncCellIds[level-1].resize(currentData()[level]->size(0)); + vertexIds[level-1].resize(currentData()[level]->size(3)); } const auto& globalIdSet = this->globalIdSet(); - // Populate for interior cells + // Populate syncCellIds and vertexIds for (int level = 1; level <= maxLevel; ++level) { const auto& elements = Dune::elements(levelGridView(level)); for (const auto& element : elements) { @@ -2714,16 +2716,19 @@ void CpGrid::syncDistributedGlobalCellIds() syncCellIds[element.level()-1][element.index()] = new_elem_globalId; } + + for (const auto& vertex : Dune::vertices(levelGridView(level))){ + vertexIds[level-1][vertex.index()] = globalIdSet.id(vertex); + } } // Re-assign new cell global ids for all refined level grids std::vector faceIds; // empty for all for (int level = 1; level <= maxLevel; ++level) { if(currentData()[level]->size(0)) { // Check if LGR is active in currect process. - auto vertexIds = currentData()[level]->global_id_set_-> getMapping<3>(); currentData()[level]->global_id_set_->swap(syncCellIds[level-1], faceIds, - vertexIds); + vertexIds[level-1]); populateCellIndexSetRefinedGrid(level); // Insert the new id sets into the grid global_id_set_ptr_ From 4ccf5be544b7cdf7c337a13a770c479f864078ba Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Fri, 1 Aug 2025 14:45:30 +0200 Subject: [PATCH 4/6] Synchronize cell ids also in adapt() test case --- tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp b/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp index 9b49780f8a..c13f11bc67 100644 --- a/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp +++ b/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp @@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(idEntityRep_and_idEntity_differ_in_refinedLevelAndLeafGrids // BEFORE synchronizing cell ids parallelAfterRefinementChecks(grid); - //grid.syncDistributedGlobalCellIds(); + grid.syncDistributedGlobalCellIds(); // AFTER synchronozing cell ids parallelAfterRefinementChecks(grid); From c80f8d96b50c637eec902585206f88cfc0739c26 Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Mon, 4 Aug 2025 09:01:08 +0200 Subject: [PATCH 5/6] Throw IdSet::id for Entity<1> --- opm/grid/cpgrid/Indexsets.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opm/grid/cpgrid/Indexsets.hpp b/opm/grid/cpgrid/Indexsets.hpp index 5e3853c816..55aa67defa 100644 --- a/opm/grid/cpgrid/Indexsets.hpp +++ b/opm/grid/cpgrid/Indexsets.hpp @@ -228,10 +228,10 @@ namespace Dune else if constexpr (std::is_same_v>) { return computeId_point(e); } - else if (std::is_same_v>) { - OPM_THROW(std::logic_error, "IdSet::id not implemented for codims other thatn 0, 1, and 3."); + else if (std::is_same_v> || std::is_same_v>) { + OPM_THROW(std::logic_error, "IdSet::id not implemented for codims other than 0 and 3."); } - else { // Entity<1> and EntityRep fall in this case. + else { // EntityRep fall in this case. return computeId(e); } } From ea615d70e2e4a46132a0b829e4fca5463ca84f82 Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Fri, 8 Aug 2025 08:49:54 +0200 Subject: [PATCH 6/6] Move test to lgr folder --- CMakeLists_files.cmake | 2 +- .../{ => lgr}/id_or_index_for_entity_and_entityRep_test.cpp | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/cpgrid/{ => lgr}/id_or_index_for_entity_and_entityRep_test.cpp (100%) diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index b462b1b93e..1ebb0d01c7 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -84,10 +84,10 @@ list(APPEND TEST_SOURCE_FILES tests/cpgrid/lgr/addLgrsOnDistributedGrid_test.cpp tests/cpgrid/lgr/communicate_distributed_grid_with_lgrs_test.cpp tests/cpgrid/lgr/distribute_level_zero_from_grid_with_lgrs_and_wells_test.cpp - tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp tests/cpgrid/lgr/distribute_level_zero_from_grid_with_lgrs_test.cpp tests/cpgrid/lgr/global_refine_test.cpp tests/cpgrid/lgr/grid_global_id_set_test.cpp + tests/cpgrid/lgr/id_or_index_for_entity_and_entityRep_test.cpp tests/cpgrid/lgr/lgr_cell_id_sync_test.cpp tests/cpgrid/lgr/logicalCartesianSize_and_refinement_test.cpp tests/cpgrid/orientedentitytable_test.cpp diff --git a/tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp b/tests/cpgrid/lgr/id_or_index_for_entity_and_entityRep_test.cpp similarity index 100% rename from tests/cpgrid/id_or_index_for_entity_and_entityRep_test.cpp rename to tests/cpgrid/lgr/id_or_index_for_entity_and_entityRep_test.cpp