My Project
Indexsets.hpp
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 /*
17 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18 Copyright 2009, 2010 Statoil ASA.
19 
20 This file is part of The Open Porous Media project (OPM).
21 
22 OPM is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, either version 3 of the License, or
25 (at your option) any later version.
26 
27 OPM is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31 
32 You should have received a copy of the GNU General Public License
33 along 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/grid/utility/ErrorMacros.hpp>
41 #include "GlobalIdMapping.hpp"
42 #include "Intersection.hpp"
43 
44 #include <unordered_map>
45 namespace Dune
46 {
47  namespace cpgrid
48  {
49 
53  class IndexSet
54  {
55  public:
58  typedef int IndexType;
59 
61  template <int cc>
62  struct Codim
63  {
65  };
66 
69  typedef std::vector<GeometryType> Types;
70 
74  IndexSet(const CpGridData& grid)
75  : grid_(grid)
76  {
77  geom_types_[0].emplace_back(Dune::GeometryTypes::cube(3));
78  geom_types_[3].emplace_back(Dune::GeometryTypes::cube(0));
79  }
80 
83  {}
84 
89  const Types& geomTypes(int codim) const
90  {
91  return geom_types_[codim];
92  }
93 
98  const Types& types(int codim) const
99  {
100  return geom_types_[codim];
101  }
102 
107  int size(GeometryType type) const
108  {
109  return grid_.size(type);
110  }
111 
112 
117  int size(int codim) const
118  {
119  return grid_.size(codim);
120  }
121 
122 
128  template<int cd>
130  {
131  return e.index();
132  }
133 
139  template<class EntityType>
140  IndexType index(const EntityType& e) const
141  {
142  return e.index();
143  }
144 
150  template <int cc>
151  IndexType subIndex(const cpgrid::Entity<0>& e, int i) const
152  {
153  return index(e.template subEntity<cc>(i));
154  }
155 
161  IndexType subIndex(const cpgrid::Entity<0>& e, int i, unsigned int cc) const
162  {
163  switch(cc) {
164  case 0: return index(e.subEntity<0>(i));
165  case 1: return index(e.subEntity<1>(i));
166  case 2: return index(e.subEntity<2>(i));
167  case 3: return index(e.subEntity<3>(i));
168  default: OPM_THROW(std::runtime_error, "Codimension " << cc << " not supported.");
169  }
170 
171  }
172 
173 
174  template<int codim>
175  IndexType subIndex(const cpgrid::Entity<codim>& /* e */, int /* i */, unsigned int /* cc */) const
176  {
177  DUNE_THROW(NotImplemented, "subIndex not implemented for codim"
178  << codim << "entities.");
179  }
185  template <class EntityType>
186  bool contains(const EntityType& e) const
187  {
188  return index(e) >= 0 && index(e) < grid_.size(EntityType::codimension); //EntityType::codimension == 0;
189  }
190 
191  private:
192  const CpGridData& grid_;
193  Types geom_types_[4];
194  };
195 
196 
197  class IdSet
198  {
199  friend class ReversePointGlobalIdSet;
200  public:
201  typedef int IdType;
202 
203  IdSet(const CpGridData& grid)
204  : grid_(grid)
205  {
206  }
207 
208  template<int cc>
209  IdType id(const cpgrid::Entity<cc>& e) const
210  {
211  return computeId(e);
212  }
213 
214  template<class EntityType>
215  IdType id(const EntityType& e) const
216  {
217  return computeId(e);
218  }
219 
221  IdType id( const cpgrid::Intersection& intersection ) const
222  {
223  return intersection.id();
224  }
225 
226  template<int cc>
227  IdType subId(const cpgrid::Entity<0>& e, int i) const
228  {
229  return id(e.template subEntity<cc>(i));
230  }
231 
232  IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const
233  {
234  switch (cc) {
235  case 0: return id(e.subEntity<0>(i));
236  case 1: return id(e.subEntity<1>(i));
237  case 2: return id(e.subEntity<2>(i));
238  case 3: return id(e.subEntity<3>(i));
239  default: OPM_THROW(std::runtime_error, "Cannot get subId of codimension " << cc);
240  }
241  return -1;
242  }
243  private:
244  template<class EntityType>
245  IdType computeId(const EntityType& e) const
246  {
247  IdType myId = 0;
248  for( int c=0; c<EntityType::codimension; ++c )
249  myId += grid_.indexSet().size( c );
250  return myId + e.index();
251  }
252  const CpGridData& grid_;
253  };
254 
255 
257  {
258  friend class CpGridData;
259  friend class ReversePointGlobalIdSet;
260  public:
261  typedef int IdType;
262 
263  void swap(std::vector<int>& cellMapping,
264  std::vector<int>& faceMapping,
265  std::vector<int>& pointMapping)
266  {
267  idSet_=nullptr;
268  GlobalIdMapping::swap(cellMapping,
269  faceMapping,
270  pointMapping);
271  }
272  LevelGlobalIdSet(const IdSet* ids, const CpGridData* view)
273  : idSet_(ids), view_(view)
274  {}
276  : idSet_(), view_()
277  {}
278  template<int codim>
279  IdType id(const Entity<codim>& e) const
280  {
281  assert(view_ == e.pgrid_);
282  return id(static_cast<const EntityRep<codim>&>(e));
283  }
284  template<int codim>
285  IdType id(const EntityRep<codim>& e) const
286  {
287  if(idSet_)
288  return idSet_->id(e);
289  else
290  return this->template getMapping<codim>()[e.index()];
291  }
292 
293  template<int cc>
294  IdType subId(const cpgrid::Entity<0>& e, int i) const
295  {
296  assert(view_ == e.pgrid_);
297  return id(e.template subEntity<cc>(i));
298  }
299 
300  IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const
301  {
302  assert(view_ == e.pgrid_);
303 
304  switch (cc) {
305  case 0: return id(*e.subEntity<0>(i));
306  //case 1: return id(*e.subEntity<1>(i));
307  //case 2: return id(*e.subEntity<2>(i));
308  case 3: return id(*e.subEntity<3>(i));
309  default: OPM_THROW(std::runtime_error, "Cannot get subId of codimension " << cc);
310  }
311  return -1;
312  }
313  private:
314  const IdSet* idSet_;
315  const CpGridData* view_;
316  };
317 
325  {
326  public:
328  using IdType = typename LevelGlobalIdSet::IdType;
329 
330  GlobalIdSet(const CpGridData& view)
331  {
332  idSets_.insert(std::make_pair(&view,view.global_id_set_));
333  }
334 
335  template<int codim>
336  IdType id(const Entity<codim>& e) const
337  {
338  return levelIdSet(e.pgrid_).id(e);
339  }
340 
341  template<int cc>
342  IdType subId(const cpgrid::Entity<0>& e, int i) const
343  {
344  return levelIdSet(e.pgrid_).template subId<cc>(e, i);
345  }
346 
347  IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const
348  {
349  return levelIdSet(e.pgrid_).subId(e, i, cc);
350  }
351 
352  void insertIdSet(const CpGridData& view)
353  {
354  idSets_.insert(std::make_pair(&view,view.global_id_set_));
355  }
356  private:
358  const LevelGlobalIdSet& levelIdSet(const CpGridData* const data) const
359  {
360  auto candidate = idSets_.find(data);
361  assert(candidate != idSets_.end());
362  return *candidate->second;
363  }
365  std::map<const CpGridData* const, const LevelGlobalIdSet*> idSets_;
366  };
367 
369  {
370  public:
372  {
373  if(idSet.idSet_)
374  {
375  grid_ = &(idSet.idSet_->grid_);
376  }
377  else
378  {
379  mapping_.reset(new std::unordered_map<int,int>);
380  int localId = 0;
381  for (const auto& globalId: idSet.template getMapping<3>())
382  (*mapping_)[globalId] = localId++;
383  }
384  }
385  int operator[](int i) const
386  {
387  if (mapping_)
388  {
389  return(*mapping_)[i];
390  }
391  else if (grid_)
392  {
393  return i - grid_->size(0) - grid_->size(1) - grid_->size(2);
394  }
395 
396  OPM_THROW(std::runtime_error, "No grid or mapping. Should not be here!");
397  }
398  void release()
399  {
400  mapping_.reset(nullptr);
401  }
402  private:
403  std::unique_ptr<std::unordered_map<int,int> > mapping_;
404  const CpGridData* grid_ = nullptr;
405  };
406 
407  } // namespace cpgrid
408 } // namespace Dune
409 
410 #endif // OPM_INDEXSETS_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:123
int size(int codim) const
number of leaf entities per codim in this process
Definition: CpGridData.cpp:144
const IndexSet & indexSet() const
Get the index set.
Definition: CpGridData.hpp:274
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:98
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:125
Definition: Entity.hpp:70
Codim< cc >::EntityPointer subEntity(int i) const
Obtain subentity.
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:325
typename LevelGlobalIdSet::IdType IdType
The type of the id.
Definition: Indexsets.hpp:328
Definition: Indexsets.hpp:198
IdType id(const cpgrid::Intersection &intersection) const
return id of intersection (here face number)
Definition: Indexsets.hpp:221
Definition: Indexsets.hpp:54
const Types & geomTypes(int codim) const
Definition: Indexsets.hpp:89
bool contains(const EntityType &e) const
Definition: Indexsets.hpp:186
int size(int codim) const
Definition: Indexsets.hpp:117
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i) const
Definition: Indexsets.hpp:151
~IndexSet()
Destructor.
Definition: Indexsets.hpp:82
std::vector< GeometryType > Types
Definition: Indexsets.hpp:69
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i, unsigned int cc) const
Definition: Indexsets.hpp:161
IndexSet(const CpGridData &grid)
Definition: Indexsets.hpp:74
IndexType index(const EntityType &e) const
Definition: Indexsets.hpp:140
IndexType index(const cpgrid::Entity< cd > &e) const
Definition: Indexsets.hpp:129
int size(GeometryType type) const
Definition: Indexsets.hpp:107
const Types & types(int codim) const
Definition: Indexsets.hpp:98
int IndexType
Definition: Indexsets.hpp:58
Definition: Intersection.hpp:68
Definition: Indexsets.hpp:257
Definition: Indexsets.hpp:369
Copyright 2019 Equinor AS.
Definition: CartesianIndexMapper.hpp:10
Export the type of the entity used as parameter in the index(...) method.
Definition: Indexsets.hpp:63