Indexsets.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: Indexsets.hpp
4//
5// Created: Fri May 29 23:30:01 2009
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// Bård Skaflestad <bard.skaflestad@sintef.no>
9//
10// $Date$
11//
12// $Revision$
13//
14//===========================================================================
15
16/*
17Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18Copyright 2009, 2010, 2022 Equinor ASA.
19
20This file is part of The Open Porous Media project (OPM).
21
22OPM is free software: you can redistribute it and/or modify
23it under the terms of the GNU General Public License as published by
24the Free Software Foundation, either version 3 of the License, or
25(at your option) any later version.
26
27OPM is distributed in the hope that it will be useful,
28but WITHOUT ANY WARRANTY; without even the implied warranty of
29MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30GNU General Public License for more details.
31
32You should have received a copy of the GNU General Public License
33along with OPM. If not, see <http://www.gnu.org/licenses/>.
34*/
35
36#ifndef OPM_INDEXSETS_HEADER
37#define OPM_INDEXSETS_HEADER
38
39#include <dune/geometry/type.hh>
40#include <opm/common/ErrorMacros.hpp>
41#include "GlobalIdMapping.hpp"
42#include "Intersection.hpp"
43
44#include <unordered_map>
45namespace Dune
46{
47 namespace cpgrid
48 {
49 // forward declaration
50 //class CpGridData;
55 {
56 //friend class Dune::cpgrid::CpGridData;
57 public:
60 typedef int IndexType;
61
63 template <int cc>
64 struct Codim
65 {
67 };
68
71 typedef std::vector<GeometryType> Types;
72
76 IndexSet() : IndexSet(0,0){}
77
78 IndexSet(std::size_t numCells, std::size_t numPoints)
79 {
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;
84 }
85
88 {}
89
94 const Types& geomTypes(int codim) const
95 {
96 return geom_types_[codim];
97 }
98
103 const Types& types(int codim) const
104 {
105 return geom_types_[codim];
106 }
107
112 int size(GeometryType type) const
113 {
114 if (type.isCube()) {
115 return size(3 - type.dim()); // return grid_.size(type);
116 } else {
117 return 0;
118 }
119 }
120
121
126 int size(int codim) const
127 {
128 return size_codim_map_[codim]; //grid_.size(codim)
129 }
130
131
137 template<int cd>
139 {
140 return e.index();
141 }
142
148 template<class EntityType>
149 IndexType index(const EntityType& e) const
150 {
151 return e.index();
152 }
153
159 template <int cc>
160 IndexType subIndex(const cpgrid::Entity<0>& e, int i) const
161 {
162 return index(e.template subEntity<cc>(i));
163 }
164
170 IndexType subIndex(const cpgrid::Entity<0>& e, int i, unsigned int cc) const;
171
172
173 template<int codim>
174 IndexType subIndex(const cpgrid::Entity<codim>& /* e */, int /* i */, unsigned int /* cc */) const
175 {
176 DUNE_THROW(NotImplemented, "subIndex not implemented for codim"
177 << codim << "entities.");
178 }
184 template <class EntityType>
185 bool contains(const EntityType& e) const
186 {
187 // return index(e) >= 0 && index(e) < grid_.size(EntityType::codimension); //EntityType::codimension == 0;
188 return index(e) >= 0 && index(e) < this->size(EntityType::codimension);
189 }
190
191 private:
192 // const CpGridData& grid_;
193 Types geom_types_[4];
194 std::array<int,4> size_codim_map_{0,0,0,0};
195 };
196
197
198 class IdSet
199 {
201 public:
202 typedef int IdType;
203
204 IdSet(const CpGridData& grid)
205 : grid_(grid)
206 {
207 }
208
209 template<int cc>
211 {
212 return computeId(e);
213 }
214
215 template<class EntityType>
216 IdType id(const EntityType& e) const
217 {
218 return computeId(e);
219 }
220
222 IdType id( const cpgrid::Intersection& intersection ) const
223 {
224 return intersection.id();
225 }
226
227 template<int cc>
228 IdType subId(const cpgrid::Entity<0>& e, int i) const
229 {
230 return id(e.template subEntity<cc>(i));
231 }
232
233 IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const;
234 private:
235 template<class EntityType>
236 IdType computeId(const EntityType& e) const
237 {
238 IdType myId = 0;
239 for( int c=0; c<EntityType::codimension; ++c )
240 myId += grid_.indexSet().size( c );
241 return myId + e.index();
242 }
243 const CpGridData& grid_;
244 };
245
246
248 {
249 friend class CpGridData;
251 public:
252 typedef int IdType;
253
254 void swap(std::vector<int>& cellMapping,
255 std::vector<int>& faceMapping,
256 std::vector<int>& pointMapping)
257 {
258 idSet_=nullptr;
259 GlobalIdMapping::swap(cellMapping,
260 faceMapping,
261 pointMapping);
262 }
263 LevelGlobalIdSet(std::shared_ptr<const IdSet> ids, const CpGridData* view)
264 : idSet_(ids), view_(view)
265 {}
267 : idSet_(), view_()
268 {}
269 template<int codim>
270 IdType id(const Entity<codim>& e) const
271 {
272 assert(view_ == e.pgrid_);
273 return id(static_cast<const EntityRep<codim>&>(e));
274 }
275 template<int codim>
276 IdType id(const EntityRep<codim>& e) const
277 {
278 if(idSet_)
279 return idSet_->id(e);
280 else
281 return this->template getMapping<codim>()[e.index()];
282 }
283
284 template<int cc>
285 IdType subId(const cpgrid::Entity<0>& e, int i) const
286 {
287 assert(view_ == e.pgrid_);
288 return id(e.template subEntity<cc>(i));
289 }
290
291 IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const;
292 private:
293 std::shared_ptr<const IdSet> idSet_;
294 const CpGridData* view_;
295 };
296
304 {
305 public:
308
310
311 template<int codim>
312 IdType id(const Entity<codim>& e) const
313 {
314 return levelIdSet(e.pgrid_).id(e);
315 }
316
317 template<int cc>
318 IdType subId(const cpgrid::Entity<0>& e, int i) const
319 {
320 return levelIdSet(e.pgrid_).template subId<cc>(e, i);
321 }
322
323 IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const;
324
325 void insertIdSet(const CpGridData& view);
326 private:
328 const LevelGlobalIdSet& levelIdSet(const CpGridData* const data) const
329 {
330 auto candidate = idSets_.find(data);
331 assert(candidate != idSets_.end());
332 return *candidate->second;
333 }
335 std::map<const CpGridData* const, std::shared_ptr<const LevelGlobalIdSet>> idSets_;
336 };
337
339 {
340 public:
342 {
343 if(idSet.idSet_)
344 {
345 grid_ = &(idSet.idSet_->grid_);
346 }
347 else
348 {
349 mapping_.reset(new std::unordered_map<int,int>);
350 int localId = 0;
351 for (const auto& globalId: idSet.template getMapping<3>())
352 (*mapping_)[globalId] = localId++;
353 }
354 }
355 int operator[](int i) const
356 {
357 if (mapping_)
358 {
359 return(*mapping_)[i];
360 }
361 else if (grid_)
362 {
363 return i - grid_->size(0) - grid_->size(1) - grid_->size(2);
364 }
365
366 OPM_THROW(std::runtime_error, "No grid or mapping. Should not be here!");
367 }
368 void release()
369 {
370 mapping_.reset(nullptr);
371 }
372 private:
373 std::unique_ptr<std::unordered_map<int,int> > mapping_;
374 const CpGridData* grid_ = nullptr;
375 };
376
377 } // namespace cpgrid
378} // namespace Dune
379
380#endif // OPM_INDEXSETS_HEADER
DataHandle & data
Definition: CpGridData.hpp:1085
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:131
int size(int codim) const
number of leaf entities per codim in this process
const IndexSet & indexSet() const
Definition: CpGridData.hpp:509
Definition: Entity.hpp:65
const CpGridData * pgrid_
Definition: Entity.hpp:276
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:99
int index() const
The (positive) index of an entity. Not a Dune interface method.
Definition: EntityRep.hpp:126
Class managing the mappings of local indices to global ids.
Definition: GlobalIdMapping.hpp:31
void swap(std::vector< int > &cellMapping, std::vector< int > &faceMapping, std::vector< int > &pointMapping)
Swap data for initialization.
Definition: GlobalIdMapping.hpp:38
The global id set for Dune.
Definition: Indexsets.hpp:304
typename LevelGlobalIdSet::IdType IdType
The type of the id.
Definition: Indexsets.hpp:307
void insertIdSet(const CpGridData &view)
GlobalIdSet(const CpGridData &view)
IdType subId(const cpgrid::Entity< 0 > &e, int i, int cc) const
IdType id(const Entity< codim > &e) const
Definition: Indexsets.hpp:312
IdType subId(const cpgrid::Entity< 0 > &e, int i) const
Definition: Indexsets.hpp:318
Definition: Indexsets.hpp:199
IdType subId(const cpgrid::Entity< 0 > &e, int i) const
Definition: Indexsets.hpp:228
IdType id(const EntityType &e) const
Definition: Indexsets.hpp:216
int IdType
Definition: Indexsets.hpp:202
IdSet(const CpGridData &grid)
Definition: Indexsets.hpp:204
IdType id(const cpgrid::Intersection &intersection) const
return id of intersection (here face number)
Definition: Indexsets.hpp:222
IdType subId(const cpgrid::Entity< 0 > &e, int i, int cc) const
IdType id(const cpgrid::Entity< cc > &e) const
Definition: Indexsets.hpp:210
Definition: Indexsets.hpp:55
bool contains(const EntityType &e) const
Definition: Indexsets.hpp:185
IndexSet(std::size_t numCells, std::size_t numPoints)
Definition: Indexsets.hpp:78
int size(int codim) const
Definition: Indexsets.hpp:126
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i) const
Definition: Indexsets.hpp:160
const Types & types(int codim) const
Definition: Indexsets.hpp:103
~IndexSet()
Destructor.
Definition: Indexsets.hpp:87
std::vector< GeometryType > Types
Definition: Indexsets.hpp:71
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i, unsigned int cc) const
IndexType index(const EntityType &e) const
Definition: Indexsets.hpp:149
IndexType index(const cpgrid::Entity< cd > &e) const
Definition: Indexsets.hpp:138
int size(GeometryType type) const
Definition: Indexsets.hpp:112
IndexSet()
Definition: Indexsets.hpp:76
const Types & geomTypes(int codim) const
Definition: Indexsets.hpp:94
int IndexType
Definition: Indexsets.hpp:60
IndexType subIndex(const cpgrid::Entity< codim > &, int, unsigned int) const
Definition: Indexsets.hpp:174
Definition: Intersection.hpp:66
int id() const
Definition: Intersection.hpp:234
Definition: Indexsets.hpp:248
void swap(std::vector< int > &cellMapping, std::vector< int > &faceMapping, std::vector< int > &pointMapping)
Definition: Indexsets.hpp:254
LevelGlobalIdSet()
Definition: Indexsets.hpp:266
IdType subId(const cpgrid::Entity< 0 > &e, int i, int cc) const
int IdType
Definition: Indexsets.hpp:252
IdType id(const Entity< codim > &e) const
Definition: Indexsets.hpp:270
IdType subId(const cpgrid::Entity< 0 > &e, int i) const
Definition: Indexsets.hpp:285
IdType id(const EntityRep< codim > &e) const
Definition: Indexsets.hpp:276
LevelGlobalIdSet(std::shared_ptr< const IdSet > ids, const CpGridData *view)
Definition: Indexsets.hpp:263
Definition: Indexsets.hpp:339
ReversePointGlobalIdSet(const LevelGlobalIdSet &idSet)
Definition: Indexsets.hpp:341
int operator[](int i) const
Definition: Indexsets.hpp:355
void release()
Definition: Indexsets.hpp:368
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
int numCells(const Dune::CpGrid &grid)
Get the number of cells of a grid.
Export the type of the entity used as parameter in the index(...) method.
Definition: Indexsets.hpp:65
cpgrid::Entity< cc > Entity
Definition: Indexsets.hpp:66