36#ifndef OPM_INDEXSETS_HEADER
37#define OPM_INDEXSETS_HEADER
39#include <dune/geometry/type.hh>
40#include <opm/common/ErrorMacros.hpp>
41#include "GlobalIdMapping.hpp"
42#include "Intersection.hpp"
45#include <unordered_map>
62 static constexpr int dimension = 3;
73 typedef std::vector<GeometryType>
Types;
80 IndexSet(std::size_t numCells, std::size_t numPoints)
82 geom_types_[0].emplace_back(Dune::GeometryTypes::cube(3));
83 geom_types_[3].emplace_back(Dune::GeometryTypes::cube(0));
84 size_codim_map_[0] = numCells;
85 size_codim_map_[3] = numPoints;
98 return geom_types_[codim];
107 return geom_types_[codim];
114 int size(GeometryType type)
const
117 return size(3 - type.dim());
130 return size_codim_map_[codim];
150 template<
class EntityType>
164 return index(e.template subEntity<cc>(i));
178 DUNE_THROW(NotImplemented,
"subIndex not implemented for codim"
179 << codim <<
"entities.");
186 template <
class EntityType>
190 return index(e) >= 0 &&
index(e) < this->
size(EntityType::codimension);
195 Types geom_types_[4];
196 std::array<int,4> size_codim_map_{0,0,0,0};
202 friend class ReversePointGlobalIdSet;
206 typedef std::int64_t IdType;
208 static constexpr int dimension = 3;
217 explicit IdSet(
const CpGridData& grid)
223 IdType id(
const typename Codim<cd>::Entity& e)
const
225 if constexpr (cd == 0)
226 return computeId_cell(e);
227 else if constexpr (cd == 1)
229 else if constexpr (cd == 3)
230 return computeId_point(e);
232 static_assert(AlwaysFalse<index_constant<cd>>::value,
233 "IdSet::id not implemented for codims other thatn 0, 1, and 3.");
236 template<
class EntityType>
237 IdType id(
const EntityType& e)
const
239 return id<EntityType::codimension>(e);
243 IdType id(
const cpgrid::EntityRep<codim>& e)
const
251 return intersection.id();
257 return id(e.template subEntity<cc>(i));
264 template<
class EntityType>
265 IdType computeId(
const EntityType& e)
const
268 for(
int c=0; c<EntityType::codimension; ++c )
270 return myId + e.index();
273 const CpGridData& grid_;
275 IdType computeId_cell(
const cpgrid::Entity<0>& e)
const
279 if (grid_.levelData().size() > 1) {
280 const auto& gridIdx = grid_.getGridIdx();
282 if ( gridIdx == 0 ) {
283 return myId + e.index();
286 if ( (gridIdx>0) && (gridIdx <
static_cast<int>(grid_.levelData().size() -1)) ) {
287 if ((e.level() != gridIdx)) {
288 return grid_.levelData()[e.level()]->localIdSet().id(e.getLevelElem());
293 for (
int lowerLevel = 0; lowerLevel< gridIdx; ++lowerLevel) {
294 for(
int c=0; c<4; ++c ) {
295 myId += grid_.levelData()[lowerLevel]->indexSet().size( c );
298 return myId + e.index();
302 assert( grid_.getGridIdx() == (
static_cast<int>(grid_.levelData().size()) -1) );
305 const std::array<int,2> level_levelIdx = grid_.leaf_to_level_cells_[e.index()];
306 const auto& levelEntity = cpgrid::Entity<0>(*(grid_.levelData()[level_levelIdx[0]]), level_levelIdx[1],
true);
307 return grid_.levelData()[level_levelIdx[0]]->local_id_set_ ->id(levelEntity);
311 return myId + e.index();
315 IdType computeId_point(
const cpgrid::Entity<3>& e)
const
319 if (grid_.levelData().size() > 1) {
320 const auto& gridIdx = grid_.getGridIdx();
322 if ( gridIdx == 0 ) {
324 for(
int c=0; c<3; ++c ) {
325 myId += grid_.indexSet().size( c );
327 return myId + e.index();
330 if ( (gridIdx>0) && (gridIdx <
static_cast<int>(grid_.levelData().size() -1)) ) {
331 const auto& level_levelIdx = grid_.corner_history_[e.index()];
332 if(level_levelIdx[0] != -1) {
333 const auto& levelEntity = cpgrid::Entity<3>(*(grid_.levelData()[level_levelIdx[0]]), level_levelIdx[1],
true);
334 return grid_.levelData()[level_levelIdx[0]]->localIdSet().id(levelEntity);
339 for (
int lowerLevel = 0; lowerLevel< gridIdx; ++lowerLevel) {
340 for(
int c=0; c<4; ++c ) {
341 myId += grid_.levelData()[lowerLevel]->indexSet().size( c );
345 for(
int c=0; c<3; ++c ) {
346 myId += grid_.indexSet().size( c );
348 return myId + e.index();
352 assert( grid_.getGridIdx() == (
static_cast<int>(grid_.levelData().size()) -1) );
355 const std::array<int,2> level_levelIdx = grid_.corner_history_[e.index()];
356 const auto& levelEntity = cpgrid::Entity<3>(*(grid_.levelData()[level_levelIdx[0]]), level_levelIdx[1],
true);
357 return grid_.levelData()[level_levelIdx[0]]->local_id_set_ ->id(levelEntity);
361 for(
int c=0; c<3; ++c ) {
362 myId += grid_.indexSet().size( c );
364 return myId + e.index();
372 friend class CpGridData;
373 friend class ReversePointGlobalIdSet;
375 typedef std::int64_t IdType;
377 static constexpr int dimension = 3;
386 void swap(std::vector<int>& cellMapping,
387 std::vector<int>& faceMapping,
388 std::vector<int>& pointMapping)
396 : idSet_(std::move(ids)), view_(view)
402 IdType id(
const typename Codim<codim>::Entity& e)
const
404 assert(view_ == e.pgrid_);
409 return idSet_->id(e);
417 IdType id(
const EntityRep<codim>& e)
const
420 return idSet_->id(e);
425 template<
class EntityType>
426 IdType id(
const EntityType& e)
const
428 return id<EntityType::codimension>(e);
432 IdType subId(
const cpgrid::Entity<0>& e,
int i)
const
434 assert(view_ == e.pgrid_);
435 return id(e.template subEntity<cc>(i));
438 IdType subId(
const cpgrid::Entity<0>& e,
int i,
int cc)
const;
441 IdType getMaxCodimGlobalId()
const
445 IdType max_codim_id = 0;
447 for (
int elemIdx = 0; elemIdx < view_-> size(0); ++elemIdx) {
448 const auto& element= cpgrid::Entity<0>(*view_, elemIdx,
true);
449 max_codim_id = std::max(max_codim_id, idSet_->id(element));
453 for (
int pointIdx = 0; pointIdx < view_->size(3); ++pointIdx) {
454 const auto& point = cpgrid::Entity<3>(*view_, pointIdx,
true);
455 max_codim_id = std::max(max_codim_id, idSet_->id(point));
463 auto max_elem_codim = std::max_element(this->
template getMapping<codim>().begin(),
465 return *max_elem_codim;
469 IdType getMaxGlobalId()
const
472 return std::max(getMaxCodimGlobalId<0>(), getMaxCodimGlobalId<3>());
476 std::shared_ptr<const IdSet> idSet_;
477 const CpGridData* view_;
490 using IdType =
typename LevelGlobalIdSet::IdType;
492 static constexpr int dimension = 3;
504 IdType id(
const typename Codim<codim>::Entity& e)
const
506 return levelIdSet(e.pgrid_).id(e);
509 template<
class EntityType>
510 IdType id(
const EntityType& e)
const
512 return id<EntityType::codimension>(e);
516 IdType subId(
const typename Codim<0>::Entity& e,
int i)
const
518 return levelIdSet(e.pgrid_).template subId<cc>(e, i);
521 IdType subId(
const typename Codim<0>::Entity& e,
int i,
int cc)
const;
523 void insertIdSet(
const CpGridData& view);
526 const LevelGlobalIdSet& levelIdSet(
const CpGridData*
const data)
const
528 auto candidate = idSets_.find(data);
529 assert(candidate != idSets_.end());
530 return *candidate->second;
533 std::map<const CpGridData* const, std::shared_ptr<const LevelGlobalIdSet>> idSets_;
536 class ReversePointGlobalIdSet
543 grid_ = &(idSet.idSet_->grid_);
547 mapping_.reset(
new std::unordered_map<int,int>);
549 for (
const auto& globalId: idSet.template getMapping<3>())
550 (*mapping_)[globalId] = localId++;
553 int operator[](
int i)
const
557 return(*mapping_)[i];
561 return i - grid_->size(0) - grid_->size(1) - grid_->size(2);
564 OPM_THROW(std::runtime_error,
"No grid or mapping. Should not be here!");
568 mapping_.reset(
nullptr);
571 std::unique_ptr<std::unordered_map<int,int> > mapping_;
Struct that hods all the data needed to represent a Cpgrid.
Definition CpGridData.hpp:118
const IndexSet & indexSet() const
Get the index set.
Definition CpGridData.hpp:569
int index() const
The (positive) index of an entity.
Definition EntityRep.hpp:126
Class managing the mappings of local indices to global ids.
Definition GlobalIdMapping.hpp:31
std::vector< int > & getMapping()
Get the vector with the mappings for a codimension.
Definition GlobalIdMapping.hpp:49
void swap(std::vector< int > &cellMapping, std::vector< int > &faceMapping, std::vector< int > &pointMapping)
Swap data for initialization.
Definition GlobalIdMapping.hpp:38
typename LevelGlobalIdSet::IdType IdType
The type of the id.
Definition Indexsets.hpp:490
IdType id(const cpgrid::Intersection &intersection) const
return id of intersection (here face number)
Definition Indexsets.hpp:249
bool contains(const EntityType &e) const
Definition Indexsets.hpp:187
int size(int codim) const
Definition Indexsets.hpp:128
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i) const
Definition Indexsets.hpp:162
const Types & types(int codim) const
Definition Indexsets.hpp:105
~IndexSet()
Destructor.
Definition Indexsets.hpp:89
std::vector< GeometryType > Types
Definition Indexsets.hpp:73
std::int64_t IndexType
Definition Indexsets.hpp:60
IndexType index(const EntityType &e) const
Definition Indexsets.hpp:151
IndexType index(const cpgrid::Entity< cd > &e) const
Definition Indexsets.hpp:140
int size(GeometryType type) const
Definition Indexsets.hpp:114
IndexSet()
Definition Indexsets.hpp:78
const Types & geomTypes(int codim) const
Definition Indexsets.hpp:96
Definition Intersection.hpp:66
Definition Indexsets.hpp:371
Copyright 2019 Equinor AS.
Definition GridPartitioning.cpp:673
The namespace Dune is the main namespace for all Dune code.
Definition CartesianIndexMapper.hpp:10
Export supported entity types.
Definition Indexsets.hpp:497
Export supported entity types.
Definition Indexsets.hpp:213
Export supported entity types.
Definition Indexsets.hpp:67
Export supported entity types.
Definition Indexsets.hpp:382