36 #ifndef OPM_INDEXSETS_HEADER 37 #define OPM_INDEXSETS_HEADER 39 #include <dune/geometry/type.hh> 40 #include <opm/grid/utility/ErrorMacros.hpp> 42 #include "GlobalIdMapping.hpp" 43 #include "Intersection.hpp" 46 #include <unordered_map> 63 static constexpr
int dimension = 3;
71 typedef std::vector<GeometryType>
Types;
78 IndexSet(std::size_t numCells, std::size_t numPoints)
80 geom_types_[0].emplace_back(Dune::GeometryTypes::cube(3));
81 geom_types_[3].emplace_back(Dune::GeometryTypes::cube(0));
82 size_codim_map_[0] = numCells;
83 size_codim_map_[3] = numPoints;
96 return geom_types_[codim];
105 return geom_types_[codim];
112 int size(GeometryType type)
const 115 return size(3 - type.dim());
128 return size_codim_map_[codim];
148 template<
class EntityType>
162 return index(e.template subEntity<cc>(i));
176 DUNE_THROW(NotImplemented,
"subIndex not implemented for codim" 177 << codim <<
"entities.");
184 template <
class EntityType>
188 return index(e) >= 0 &&
index(e) < this->
size(EntityType::codimension);
193 Types geom_types_[4];
194 std::array<int,4> size_codim_map_{0,0,0,0};
204 typedef std::int64_t IdType;
206 static constexpr
int dimension = 3;
218 IdType id(
const typename Codim<cd>::Entity& e)
const 220 if constexpr (cd == 0)
221 return computeId_cell(e);
222 else if constexpr (cd == 3)
223 return computeId_point(e);
225 static_assert(AlwaysFalse<index_constant<cd>>::value,
226 "
IdSet::
id not implemented for codims other than 0, and 3.");
229 template<class EntityType>
230 IdType
id(const EntityType& e)
const 232 return id<EntityType::codimension>(e);
242 IdType idLevelZero(
const Entity<codim>& e)
const =
delete;
247 return intersection.id();
253 return id(e.template subEntity<cc>(i));
260 template<
class EntityType>
261 IdType computeId(
const EntityType& e)
const 264 for(
int c=0; c<EntityType::codimension; ++c )
266 return myId + e.index();
269 const CpGridData& grid_;
271 IdType computeId_cell(
const cpgrid::Entity<0>& e)
const 275 if (grid_.levelData().size() > 1) {
276 const auto& gridIdx = grid_.getGridIdx();
278 if ( gridIdx == 0 ) {
279 return myId + e.index();
282 if ( (gridIdx>0) && (gridIdx < static_cast<int>(grid_.levelData().size() -1)) ) {
283 if ((e.level() != gridIdx)) {
284 return grid_.levelData()[e.level()]->localIdSet().id(e.getLevelElem());
289 for (
int lowerLevel = 0; lowerLevel< gridIdx; ++lowerLevel) {
290 for(
int c=0; c<4; ++c ) {
291 myId += grid_.levelData()[lowerLevel]->indexSet().size( c );
294 return myId + e.index();
298 assert( grid_.getGridIdx() == (
static_cast<int>(grid_.levelData().size()) -1) );
301 const std::array<int,2> level_levelIdx = grid_.leaf_to_level_cells_[e.index()];
302 const auto& levelEntity = cpgrid::Entity<0>(*(grid_.levelData()[level_levelIdx[0]]), level_levelIdx[1],
true);
303 return grid_.levelData()[level_levelIdx[0]]->local_id_set_ ->id(levelEntity);
307 return myId + e.index();
311 IdType computeId_point(
const cpgrid::Entity<3>& e)
const 315 if (grid_.levelData().size() > 1) {
316 const auto& gridIdx = grid_.getGridIdx();
318 if ( gridIdx == 0 ) {
320 for(
int c=0; c<3; ++c ) {
321 myId += grid_.indexSet().size( c );
323 return myId + e.index();
326 if ( (gridIdx>0) && (gridIdx < static_cast<int>(grid_.levelData().size() -1)) ) {
327 const auto& level_levelIdx = grid_.corner_history_[e.index()];
328 if(level_levelIdx[0] != -1) {
329 const auto& levelEntity = cpgrid::Entity<3>(*(grid_.levelData()[level_levelIdx[0]]), level_levelIdx[1],
true);
330 return grid_.levelData()[level_levelIdx[0]]->localIdSet().id(levelEntity);
335 for (
int lowerLevel = 0; lowerLevel< gridIdx; ++lowerLevel) {
336 for(
int c=0; c<4; ++c ) {
337 myId += grid_.levelData()[lowerLevel]->indexSet().size( c );
341 for(
int c=0; c<3; ++c ) {
342 myId += grid_.indexSet().size( c );
344 return myId + e.index();
348 assert( grid_.getGridIdx() == (
static_cast<int>(grid_.levelData().size()) -1) );
351 const std::array<int,2> level_levelIdx = grid_.corner_history_[e.index()];
352 const auto& levelEntity = cpgrid::Entity<3>(*(grid_.levelData()[level_levelIdx[0]]), level_levelIdx[1],
true);
353 return grid_.levelData()[level_levelIdx[0]]->local_id_set_ ->id(levelEntity);
357 for(
int c=0; c<3; ++c ) {
358 myId += grid_.indexSet().size( c );
360 return myId + e.index();
371 typedef std::int64_t IdType;
373 static constexpr
int dimension = 3;
379 void swap(std::vector<int>& cellMapping,
380 std::vector<int>& faceMapping,
381 std::vector<int>& pointMapping)
389 : idSet_(
std::move(ids)), view_(view)
395 IdType id(
const typename Codim<codim>::Entity& e)
const 397 assert(view_ == e.pgrid_);
402 return idSet_->id(e);
406 return this->
template getMapping<codim>()[e.index()];
410 IdType idLevelZero(
const EntityRep<codim>& e)
const 413 return idSet_->idLevelZero(e);
415 return this->
template getMapping<codim>()[e.index()];
419 IdType idLevelZero(
const Entity<codim>& e)
const =
delete;
421 template<
class EntityType>
422 IdType id(
const EntityType& e)
const 424 return id<EntityType::codimension>(e);
428 IdType subId(
const cpgrid::Entity<0>& e,
int i)
const 430 assert(view_ == e.pgrid_);
431 return id(e.template subEntity<cc>(i));
434 IdType subId(
const cpgrid::Entity<0>& e,
int i,
int cc)
const;
437 IdType getMaxCodimGlobalId()
const 441 IdType max_codim_id = 0;
443 for (
int elemIdx = 0; elemIdx < view_-> size(0); ++elemIdx) {
444 const auto& element= cpgrid::Entity<0>(*view_, elemIdx,
true);
445 max_codim_id = std::max(max_codim_id, idSet_->id(element));
449 for (
int pointIdx = 0; pointIdx < view_->size(3); ++pointIdx) {
450 const auto& point = cpgrid::Entity<3>(*view_, pointIdx,
true);
451 max_codim_id = std::max(max_codim_id, idSet_->id(point));
459 const auto max_elem_codim =
460 std::ranges::max_element(this->
template getMapping<codim>());
461 return *max_elem_codim;
465 IdType getMaxGlobalId()
const 468 return std::max(getMaxCodimGlobalId<0>(), getMaxCodimGlobalId<3>());
472 std::shared_ptr<const IdSet> idSet_;
473 const CpGridData* view_;
486 using IdType =
typename LevelGlobalIdSet::IdType;
488 static constexpr
int dimension = 3;
499 return levelIdSet(e.pgrid_).id(e);
502 template<
class EntityType>
503 IdType id(
const EntityType& e)
const 505 return id<EntityType::codimension>(e);
509 IdType subId(
const typename Codim<0>::Entity& e,
int i)
const 511 return levelIdSet(e.pgrid_).template subId<cc>(e, i);
514 IdType subId(
const typename Codim<0>::Entity& e,
int i,
int cc)
const;
516 void insertIdSet(
const CpGridData& view);
519 const LevelGlobalIdSet& levelIdSet(
const CpGridData*
const data)
const 521 auto candidate = idSets_.find(data);
522 assert(candidate != idSets_.end());
523 return *candidate->second;
526 std::map<const CpGridData* const, std::shared_ptr<const LevelGlobalIdSet>> idSets_;
536 grid_ = &(idSet.idSet_->grid_);
540 mapping_.reset(
new std::unordered_map<int,int>);
542 for (
const auto& globalId: idSet.template getMapping<3>())
543 (*mapping_)[globalId] = localId++;
546 int operator[](
int i)
const 550 return(*mapping_)[i];
554 return i - grid_->size(0) - grid_->size(1) - grid_->size(2);
557 OPM_THROW(std::runtime_error,
"No grid or mapping. Should not be here!");
561 mapping_.reset(
nullptr);
564 std::unique_ptr<std::unordered_map<int,int> > mapping_;
571 #endif // OPM_INDEXSETS_HEADER
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:125
typename Impl::CodimTraits< cd > Codim
Export supported entity types.
Definition: Indexsets.hpp:492
const IndexSet & indexSet() const
Get the index set.
Definition: CpGridData.hpp:570
int size(int codim) const
Definition: Indexsets.hpp:126
IndexSet()
Definition: Indexsets.hpp:76
The namespace Dune is the main namespace for all Dune code.
Definition: CartesianIndexMapper.hpp:9
Definition: Indexsets.hpp:366
Definition: Indexsets.hpp:198
Definition: Intersection.hpp:329
Definition: Indexsets.hpp:529
Definition: Intersection.hpp:62
Definition: Entity.hpp:66
typename LevelGlobalIdSet::IdType IdType
The type of the id.
Definition: Indexsets.hpp:486
std::vector< GeometryType > Types
Definition: Indexsets.hpp:71
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:117
typename Impl::CodimTraits< cd > Codim
Export supported entity types.
Definition: Indexsets.hpp:210
void swap(std::vector< int > &cellMapping, std::vector< int > &faceMapping, std::vector< int > &pointMapping)
Swap data for initialization.
Definition: GlobalIdMapping.hpp:38
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i) const
Definition: Indexsets.hpp:160
const Types & geomTypes(int codim) const
Definition: Indexsets.hpp:94
std::int64_t IndexType
Definition: Indexsets.hpp:61
typename Impl::CodimTraits< cd > Codim
Export supported entity types.
Definition: Indexsets.hpp:377
IdType id(const cpgrid::Intersection &intersection) const
return id of intersection (here face number)
Definition: Indexsets.hpp:245
int size(GeometryType type) const
Definition: Indexsets.hpp:112
Class managing the mappings of local indices to global ids.
Definition: GlobalIdMapping.hpp:30
IndexType index(const EntityType &e) const
Definition: Indexsets.hpp:149
const Types & types(int codim) const
Definition: Indexsets.hpp:103
typename Impl::CodimTraits< cd > Codim
Export supported entity types.
Definition: Indexsets.hpp:67
The global id set for Dune.
Definition: Indexsets.hpp:482
IndexType index(const cpgrid::Entity< cd > &e) const
Definition: Indexsets.hpp:138
Represents an entity of a given codim, with positive or negative orientation.
Definition: CpGridData.hpp:96
Definition: Indexsets.hpp:56
bool contains(const EntityType &e) const
Definition: Indexsets.hpp:185
~IndexSet()
Destructor.
Definition: Indexsets.hpp:87