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 namespace Dune
45 {
46  namespace cpgrid
47  {
48 
52  class IndexSet
53  {
54  public:
57  typedef int IndexType;
58 
61  typedef std::vector<GeometryType> Types;
62 
66  IndexSet(const CpGridData& grid)
67  : grid_(grid)
68  {
69  GeometryType t;
70  t.makeCube(3);
71  geom_types_[0].push_back(t);
72  t.makeCube(0);
73  geom_types_[3].push_back(t);
74  }
75 
78  {}
79 
84  const Types& geomTypes(int codim) const
85  {
86  return geom_types_[codim];
87  }
88 
93  const Types& types(int codim) const
94  {
95  return geom_types_[codim];
96  }
97 
102  int size(GeometryType type) const
103  {
104  return grid_.size(type);
105  }
106 
107 
112  int size(int codim) const
113  {
114  return grid_.size(codim);
115  }
116 
117 
123  template<int cd>
125  {
126  return e.index();
127  }
128 
134  template<class EntityType>
135  IndexType index(const EntityType& e) const
136  {
137  return e.index();
138  }
139 
145  template <int cc>
146  IndexType subIndex(const cpgrid::Entity<0>& e, int i) const
147  {
148  return index(e.template subEntity<cc>(i));
149  }
150 
156  IndexType subIndex(const cpgrid::Entity<0>& e, int i, unsigned int cc) const
157  {
158  switch(cc) {
159  case 0: return index(e.subEntity<0>(i));
160  case 1: return index(e.subEntity<1>(i));
161  case 2: return index(e.subEntity<2>(i));
162  case 3: return index(e.subEntity<3>(i));
163  default: OPM_THROW(std::runtime_error, "Codimension " << cc << " not supported.");
164  }
165 
166  }
167 
168 
169  template<int codim>
170  IndexType subIndex(const cpgrid::Entity<codim>& /* e */, int /* i */, unsigned int /* cc */) const
171  {
172  DUNE_THROW(NotImplemented, "subIndex not implemented for codim"
173  << codim << "entities.");
174  }
180  template <class EntityType>
181  bool contains(const EntityType& e) const
182  {
183  return index(e) >= 0 && index(e) < grid_.size(EntityType::codimension); //EntityType::codimension == 0;
184  }
185 
186  private:
187  const CpGridData& grid_;
188  Types geom_types_[4];
189  };
190 
191 
192  class IdSet
193  {
194  public:
195  typedef int IdType;
196 
197  IdSet(const CpGridData& grid)
198  : grid_(grid)
199  {
200  }
201 
202  template<int cc>
203  IdType id(const cpgrid::Entity<cc>& e) const
204  {
205  return computeId(e);
206  }
207 
208  template<class EntityType>
209  IdType id(const EntityType& e) const
210  {
211  return computeId(e);
212  }
213 
215  IdType id( const cpgrid::Intersection& intersection ) const
216  {
217  return intersection.id();
218  }
219 
220  template<int cc>
221  IdType subId(const cpgrid::Entity<0>& e, int i) const
222  {
223  return id(e.template subEntity<cc>(i));
224  }
225 
226  IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const
227  {
228  switch (cc) {
229  case 0: return id(e.subEntity<0>(i));
230  case 1: return id(e.subEntity<1>(i));
231  case 2: return id(e.subEntity<2>(i));
232  case 3: return id(e.subEntity<3>(i));
233  default: OPM_THROW(std::runtime_error, "Cannot get subId of codimension " << cc);
234  }
235  return -1;
236  }
237  private:
238  template<class EntityType>
239  IdType computeId(const EntityType& e) const
240  {
241  IdType myId = 0;
242  for( int c=0; c<EntityType::codimension; ++c )
243  myId += grid_.indexSet().size( c );
244  return myId + e.index();
245  }
246  const CpGridData& grid_;
247  };
248 
249 
251  {
252  friend class CpGridData;
253  public:
254  typedef int IdType;
255 
256  void swap(std::vector<int>& cellMapping,
257  std::vector<int>& faceMapping,
258  std::vector<int>& pointMapping)
259  {
260  idSet_=nullptr;
261  GlobalIdMapping::swap(cellMapping,
262  faceMapping,
263  pointMapping);
264  }
265  GlobalIdSet(const IdSet* ids)
266  : idSet_(ids)
267  {}
268  GlobalIdSet()
269  : idSet_()
270  {}
271  template<class EntityType>
272  IdType id(const EntityType& e) const
273  {
274  if(idSet_)
275  return idSet_->id(e);
276  else
277  return this->template getMapping<EntityType::codimension>()[e.index()];
278  }
279 
280  template<int cc>
281  IdType subId(const cpgrid::Entity<0>& e, int i) const
282  {
283  return id(e.template subEntity<cc>(i));
284  }
285 
286  IdType subId(const cpgrid::Entity<0>& e, int i, int cc) const
287  {
288  switch (cc) {
289  case 0: return id(*e.subEntity<0>(i));
290  //case 1: return id(*e.subEntity<1>(i));
291  //case 2: return id(*e.subEntity<2>(i));
292  case 3: return id(*e.subEntity<3>(i));
293  default: OPM_THROW(std::runtime_error, "Cannot get subId of codimension " << cc);
294  }
295  return -1;
296  }
297  private:
298  const IdSet* idSet_;
299  };
300 
301 
302  } // namespace cpgrid
303 } // namespace Dune
304 
305 #endif // OPM_INDEXSETS_HEADER
Definition: CpGridData.hpp:93
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:126
const IndexSet & indexSet() const
Get the index set.
Definition: CpGridData.hpp:233
int size(int codim) const
Definition: Indexsets.hpp:112
int IndexType
Definition: Indexsets.hpp:57
Holds the implementation of the CpGrid as a pimple.
Definition: OpmParserIncludes.hpp:42
Definition: Indexsets.hpp:192
Definition: Intersection.hpp:67
std::vector< GeometryType > Types
Definition: Indexsets.hpp:61
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:105
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:146
const Types & geomTypes(int codim) const
Definition: Indexsets.hpp:84
IdType id(const cpgrid::Intersection &intersection) const
return id of intersection (here face number)
Definition: Indexsets.hpp:215
int size(GeometryType type) const
Definition: Indexsets.hpp:102
IndexSet(const CpGridData &grid)
Definition: Indexsets.hpp:66
Class managing the mappings of local indices to global ids.
Definition: GlobalIdMapping.hpp:30
IndexType index(const EntityType &e) const
Definition: Indexsets.hpp:135
const Types & types(int codim) const
Definition: Indexsets.hpp:93
int size(int codim) const
number of leaf entities per codim in this process
Definition: CpGridData.cpp:132
IndexType subIndex(const cpgrid::Entity< 0 > &e, int i, unsigned int cc) const
Definition: Indexsets.hpp:156
Definition: Indexsets.hpp:250
IndexType index(const cpgrid::Entity< cd > &e) const
Definition: Indexsets.hpp:124
Definition: Indexsets.hpp:52
bool contains(const EntityType &e) const
Definition: Indexsets.hpp:181
Codim< cc >::EntityPointer subEntity(int i) const
Obtain subentity.
~IndexSet()
Destructor.
Definition: Indexsets.hpp:77