From f17b1893fbdd1cc68e6d6cb60bf2792bab186d8e Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Mon, 28 Jul 2025 18:11:11 +0200 Subject: [PATCH 1/3] [cleanup] Do not support subId/subIndex for unsupported codims. --- opm/grid/cpgrid/Indexsets.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/opm/grid/cpgrid/Indexsets.cpp b/opm/grid/cpgrid/Indexsets.cpp index 1ea159312..efbeec40f 100644 --- a/opm/grid/cpgrid/Indexsets.cpp +++ b/opm/grid/cpgrid/Indexsets.cpp @@ -48,8 +48,6 @@ IndexSet::IndexType IndexSet::subIndex(const cpgrid::Entity<0>& e, int i, unsign { switch(cc) { case 0: return index(e.subEntity<0>(i)); - case 1: return index(e.subEntity<1>(i)); - // case 2: return index(e.subEntity<2>(i)); // Not supported in this grid case 3: return index(e.subEntity<3>(i)); default: OPM_THROW(std::runtime_error, "Codimension " + std::to_string(cc) + " not supported."); @@ -60,8 +58,6 @@ IdSet::IdType IdSet::subId(const cpgrid::Entity<0>& e, int i, int cc) const { switch (cc) { case 0: return id(e.subEntity<0>(i)); - case 1: return id(e.subEntity<1>(i)); - // case 2: return id(e.subEntity<2>(i)); // Not supported in this grid case 3: return id(e.subEntity<3>(i)); default: OPM_THROW(std::runtime_error, "Cannot get subId of codimension " + std::to_string(cc)); @@ -75,8 +71,6 @@ LevelGlobalIdSet::IdType LevelGlobalIdSet::subId(const cpgrid::Entity<0>& e, int switch (cc) { case 0: return id(e.subEntity<0>(i)); - //case 1: return id(*e.subEntity<1>(i)); - //case 2: return id(*e.subEntity<2>(i)); case 3: return id(e.subEntity<3>(i)); default: OPM_THROW(std::runtime_error, "Cannot get subId of codimension " + std::to_string(cc)); From 0153a467ebf5bea5c5b7a512fe479921008c1cae Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Mon, 28 Jul 2025 18:55:12 +0200 Subject: [PATCH 2/3] [wip&messy] Try to rename id method taking EntityRep. This is not finished but might serve as inspiration. --- opm/grid/cpgrid/CpGridData.cpp | 14 +++++++------- opm/grid/cpgrid/Indexsets.hpp | 16 ++++++++++------ tests/cpgrid/distribution_test.cpp | 2 ++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/opm/grid/cpgrid/CpGridData.cpp b/opm/grid/cpgrid/CpGridData.cpp index 0c5b29b3c..f9e12ba2f 100644 --- a/opm/grid/cpgrid/CpGridData.cpp +++ b/opm/grid/cpgrid/CpGridData.cpp @@ -114,7 +114,7 @@ void CpGridData::populateGlobalCellIndexSet() auto& cell_indexset = cellIndexSet(); cell_indexset.beginResize(); for (int index = 0, end = size(0); index != end ; ++index){ - cell_indexset.add(global_id_set_->id(Entity<0>(*this, EntityRep<0>(index, true))), + cell_indexset.add(global_id_set_->idLevelZero(Entity<0>(*this, EntityRep<0>(index, true))), ParallelIndexSet::LocalIndex(index, AttributeSet::owner, true)); } cell_indexset.endResize(); @@ -586,7 +586,7 @@ struct Cell2PointsDataHandle const auto& points = globalCell2Points_[i]; std::for_each(points.begin(), points.end(), [&buffer, this](const int& point){ - buffer.write(globalIds_.id(EntityRep<3>(point, true)));}); + buffer.write(globalIds_.idLevelZero(EntityRep<3>(point, true)));}); for (const auto& point: globalAdditionalPointIds_[i]) { buffer.write(point); @@ -703,7 +703,7 @@ struct SparseTableDataHandle void gather(B& buffer, const T& t) { const auto& entries = global_[t.index()]; - std::for_each(entries.begin(), entries.end(), [&buffer, this](const DataType& i){buffer.write(globalIds_.id(EntityRep<3>(i, true)));}); + std::for_each(entries.begin(), entries.end(), [&buffer, this](const DataType& i){buffer.write(globalIds_.idLevelZero(EntityRep<3>(i, true)));}); } template void scatter(B& buffer, const T& t, std::size_t ) @@ -774,7 +774,7 @@ struct OrientedEntityTableDataHandle { std::for_each(entries.begin(), entries.end(), [&buffer, this](const ToEntity& i){ - int id = globalIds_->id(i); + int id = globalIds_->idLevelZero(i); if (!i.orientation()) id = ~id; buffer.write(id);}); @@ -845,7 +845,7 @@ struct IndexSet2IdSet map_[entry.local()] = entry.global(); } template - int id(const T& t) const + int idLevelZero(const T& t) const { return map_[t.index()]; } @@ -1362,7 +1362,7 @@ std::vector > computeAdditionalFacePoints(const std::vector(point,true))); + additionalFacePoints[c].insert(globalIds.idLevelZero(EntityRep<3>(point,true))); } } return additionalFacePoints; @@ -1465,7 +1465,7 @@ std::map computeCell2Point(const CpGrid& grid, createInterfaceList(procCellLists, globalCell2Points, globalAdditionalPoints, [&globalIds](int i){ - return globalIds.id(EntityRep<3>(i, true)); + return globalIds.idLevelZero(EntityRep<3>(i, true)); }, globalMap2Local, pointInterfaces[procCellLists.first]); diff --git a/opm/grid/cpgrid/Indexsets.hpp b/opm/grid/cpgrid/Indexsets.hpp index 4d36ab590..120c2634c 100644 --- a/opm/grid/cpgrid/Indexsets.hpp +++ b/opm/grid/cpgrid/Indexsets.hpp @@ -233,14 +233,18 @@ namespace Dune "IdSet::id not implemented for codims other thatn 0, 1, and 3."); } - template - IdType id(const EntityType& e) const + IdType id(const Entity<0>& e) const { - return id(e); + return id<0>(e); + } + + IdType id(const Entity<3>& e) const + { + return id<3>(e); } template - IdType id(const cpgrid::EntityRep& e) const + IdType idLevelZero(const cpgrid::EntityRep& e) const { return computeId(e); } @@ -414,10 +418,10 @@ namespace Dune } template - IdType id(const EntityRep& e) const + IdType idLevelZero(const EntityRep& e) const { if(idSet_) - return idSet_->id(e); + return idSet_->idLevelZero(e); else return this->template getMapping()[e.index()]; } diff --git a/tests/cpgrid/distribution_test.cpp b/tests/cpgrid/distribution_test.cpp index 3f6782729..733ec4a71 100644 --- a/tests/cpgrid/distribution_test.cpp +++ b/tests/cpgrid/distribution_test.cpp @@ -500,8 +500,10 @@ BOOST_AUTO_TEST_CASE(compareWithSequential) using namespace Dune::cpgrid; auto face = grid.cellFace(eIt->index(), f); auto seqFace = seqGrid.cellFace(seqEIt->index(), f); + /* BOOST_REQUIRE(idSet.id(Dune::createEntity<1>(grid, face, true)) == seqIdSet.id(Dune::createEntity<1>(seqGrid, seqFace, true))); + */ int vertices = grid.numFaceVertices(face); BOOST_REQUIRE(vertices == seqGrid.numFaceVertices(seqFace)); for (int v = 0; v < vertices; ++v) From 69089eddcf90d0f090c1af97d06c9e496b4f7654 Mon Sep 17 00:00:00 2001 From: Antonella Ritorto Date: Thu, 31 Jul 2025 13:17:20 +0200 Subject: [PATCH 3/3] Add a test for idEntity vs idLevelZeroEntityRep, rebase --- CMakeLists.txt | 1 + CMakeLists_files.cmake | 1 + .../idEntity_idLevelZeroEntityRep_test.cpp | 430 ++++++++++++++++++ 3 files changed, 432 insertions(+) create mode 100644 tests/cpgrid/idEntity_idLevelZeroEntityRep_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f34438b5c..aaac841aa 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(idEntity_idLevelZeroEntityRep_test_parallel ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} 4 bin/idEntity_idLevelZeroEntityRep_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 c8ca7ec6b..9ec70fd7a 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/idEntity_idLevelZeroEntityRep_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/tests/cpgrid/idEntity_idLevelZeroEntityRep_test.cpp b/tests/cpgrid/idEntity_idLevelZeroEntityRep_test.cpp new file mode 100644 index 000000000..dbb613900 --- /dev/null +++ b/tests/cpgrid/idEntity_idLevelZeroEntityRep_test.cpp @@ -0,0 +1,430 @@ +/* + 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 IdEntityIdLevelZeroEntityRepTests +#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.idLevelZero(entityRep) != idSet.id(entity)){ + return false; + } + } + return true; +} + +template +bool checkEntityRepAndEntityIds(const EntityRange& entities, const IdSet& idSet, const GridIdSet& gridIdSet) +{ + for (const auto& entity : entities) { + const auto entityRep = Dune::cpgrid::EntityRep(entity.index(), true); + if (idSet.idLevelZero(entityRep) != gridIdSet.id(entity)){ + return false; + } + } + return true; +} + +void entityRepIndexAndEntityIndexCoincide(const Dune::CpGrid& grid) +{ + const auto& leafIndexSet = grid.leafIndexSet(); + const auto leafView = grid.leafGridView(); + if (leafView.size(0)) { + 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); + if (levelView.size(0)) { + 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(); + if (levelZeroView.size(0)) { + 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(); + if (levelZeroView.size(0)) { + 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 localIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(const Dune::CpGrid& grid, bool coincide) +{ + const auto& gridLocalIdSet = grid.localIdSet(); + 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, gridLocalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(levelView), levelLocalIdSet, gridLocalIdSet), coincide); + } + } +} + +void localIdsEntityRepAndEntityInLeafGrid(const Dune::CpGrid& grid, bool coincide) +{ + const auto leafView = grid.leafGridView(); + const auto& leafLocalIdSet = grid.currentData().back()->localIdSet(); + if (leafView.size(0)) { + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(leafView), leafLocalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(leafView), leafLocalIdSet), coincide); + } +} + +void localIdsEntityRepAndEntityInLeafGridGridIdSet(const Dune::CpGrid& grid, bool coincide) +{ + const auto leafView = grid.leafGridView(); + const auto& leafLocalIdSet = grid.currentData().back()->localIdSet(); + const auto& gridLocalIdSet = grid.localIdSet(); + if (leafView.size(0)) { + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(leafView), leafLocalIdSet, gridLocalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(leafView), leafLocalIdSet, gridLocalIdSet), 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 globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(const Dune::CpGrid& grid, bool coincide) +{ + const auto& gridGlobalIdSet = grid.globalIdSet(); + 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, gridGlobalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(levelView), levelGlobalIdSet, gridGlobalIdSet), 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); + } +} + +void globalIdsEntityRepAndEntityInLeafGridGridIdSet(const Dune::CpGrid& grid, bool coincide) +{ + const auto leafView = grid.leafGridView(); + const auto& gridGlobalIdSet = grid.globalIdSet(); + if (leafView.size(0)>0) { + const auto& leafGlobalIdSet = grid.currentData().back()->globalIdSet(); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<0>(Dune::elements(leafView), leafGlobalIdSet, gridGlobalIdSet), coincide); + BOOST_CHECK_EQUAL(checkEntityRepAndEntityIds<3>(Dune::vertices(leafView), leafGlobalIdSet, gridGlobalIdSet), coincide); + } +} + +void beforeRefinementChecks(const Dune::CpGrid& grid) +{ + /** BEFORE refinement via addLgrsUpdateLeafView, adapt, or globalRefine(1) in serial/parallel */ + // 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 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, true /* coincide */); + /** Global ids EntityRep and Entity should not coincide in level refined grids. + Refinement via addLgrsUpdateLeafView(...) and globalRefine(1) fail (->coincide). + Refinement via adapt() does not fail (-> do not coincide). */ + // globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* should NOT coincide */); + // globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, false);ww + /** Global ids EntityRep and Entity should not coincide in leaf grid. + Refinement via addLgrsUpdateLeafView(...), adapt(), and globalRefine(1) fail (->coincide).*/ + // globalIdsEntityRepAndEntityInLeafGrid(grid, false /* should NOT coincide */); + // globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, false); + + localIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, false); + localIdsEntityRepAndEntityInLeafGridGridIdSet(grid, false); +} + +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}); + beforeRefinementChecks(grid); + + if (grid.comm().size()>1) { + grid.loadBalance(); + beforeRefinementChecks(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 + serialAfterRefinementChecks(grid); + } + else { // Parallel + // BEFORE applying the same refinement to the global view. + parallelAfterRefinementChecks(grid); + /** /!\ Global ids from EntityRep and Entity should NOT coincide in refined and leaf grids. */ + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, true); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + + grid.switchToGlobalView(); + // 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(); + + // BEFORE synchronizing cell ids + parallelAfterRefinementChecks(grid); + /** /!\ Global ids from EntityRep and Entity should NOT coincide in refined and leaf grids. */ + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, true); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + + grid.syncDistributedGlobalCellIds(); + + // AFTER synchronizing cell ids + parallelAfterRefinementChecks(grid); + /** /!\ Global ids from EntityRep and Entity should NOT coincide in refined and leaf grids. */ + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, true); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + } +} + +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}); + beforeRefinementChecks(grid); + + if (grid.comm().size()>1) { + grid.loadBalance(); + beforeRefinementChecks(grid); + } + + 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 + serialAfterRefinementChecks(grid); + } + else { // Parallel + // BEFORE applying the same refinement to the global view + parallelAfterRefinementChecks(grid); + + grid.switchToGlobalView(); + // 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) { + grid.mark(1, element); + } + } + grid.preAdapt(); + grid.adapt(); // Default subdivisions per cell 2x2x2 in x-,y-, and z-direction. + grid.postAdapt(); + grid.switchToDistributedView(); + + // BEFORE synchronizing cell ids + parallelAfterRefinementChecks(grid); + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, false /* DO NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, false); + + /** /!\ Global ids from EntityRep and Entity should NOT coincide in leaf grid. */ + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + + // grid.syncDistributedGlobalCellIds(); + + // AFTER synchronozing cell ids + parallelAfterRefinementChecks(grid); + } +} + +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}); + beforeRefinementChecks(grid); + + if (grid.comm().size()>1) { + grid.loadBalance(); + beforeRefinementChecks(grid); + } + + grid.globalRefine(1); + + if (grid.comm().size() == 1) { // Serial + serialAfterRefinementChecks(grid); + } + else { // Parallel + // BEFORE applying the same refinement to the global view. + parallelAfterRefinementChecks(grid); + /** /!\ Global ids from EntityRep and Entity should NOT coincide in refined and leaf grids. */ + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, true); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + + grid.switchToGlobalView(); + // Synchronizing cell ids requires that both the global + // and distributed views undergo the same refinement process. + grid.globalRefine(1); + grid.switchToDistributedView(); + + // BEFORE synchronizing cell ids + parallelAfterRefinementChecks(grid); + /** /!\ Global ids from EntityRep and Entity should NOT coincide in refined and leaf grids. */ + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, true); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + + grid.syncDistributedGlobalCellIds(); + + // AFTER synchronizing cell ids + parallelAfterRefinementChecks(grid); + /** /!\ Global ids from EntityRep and Entity should NOT coincide in refined and leaf grids. */ + globalIdsEntityRepAndEntityInRefinedLevelGrids(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInRefinedLevelGridsGridIdSet(grid, true); + globalIdsEntityRepAndEntityInLeafGrid(grid, true /* should NOT coincide */); + globalIdsEntityRepAndEntityInLeafGridGridIdSet(grid, true /* should NOT coincide */); + /** /!\ end */ + } +}