opm-grid
EntityRep.hpp
1 //===========================================================================
2 //
3 // File: EntityRep.hpp
4 //
5 // Created: Tue Jun 9 11:11:24 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_ENTITYREP_HEADER
37 #define OPM_ENTITYREP_HEADER
38 
39 
40 // -------------------------------------------------------------------
41 // -> Layering violation. --------------------------------------------
42 //
43 // We need a unary operator-() for class Dune::FieldVector<K,n>
44 // within method Dune::SignedEntityVariable<T,codim>::operator[](),
45 // but Dune::FieldVector<K,n> does not provide such an operator.
46 //
47 
48 // Warning suppression for Dune includes.
49 #include <opm/grid/utility/platform_dependent/disable_warnings.h>
50 #include <dune/common/fvector.hh>
51 #include <opm/grid/utility/platform_dependent/reenable_warnings.h>
52 
53 namespace Dune
54 {
55  template<typename K, int n>
56  FieldVector<K,n>
57  operator- (const FieldVector<K,n>& v)
58  {
59  // Assume 'K' supports a single parameter constructor. The
60  // assumption holds for all standard C++ built-in arithmetic
61  // types such as 'int', 'float', and 'complex<double>'.
62  //
63  return FieldVector<K,n>(K(0)) - v;
64  }
65 }
66 //
67 // <- Layering violation. --------------------------------------------
68 // -------------------------------------------------------------------
69 
70 
71 //#include <opm/core/utility/SparseTable.hpp>
72 #include <climits>
73 #include <vector>
74 
76 namespace Dune
77 {
78 
79  namespace cpgrid
80  {
81 
95 
96  template <int codim>
97  class EntityRep
98  {
99  public:
100  enum{ codimension=codim};
101 
104  : entityrep_(0)
105  {
106  }
110  EntityRep(int index_arg, bool orientation_arg)
111  : entityrep_(orientation_arg ? index_arg : ~index_arg)
112  {
113  assert(index_arg >= 0);
114  }
118  void setValue(int index_arg, bool orientation_arg)
119  {
120  assert(index_arg >= 0);
121  entityrep_ = orientation_arg ? index_arg : ~index_arg;
122  }
125  int index() const
126  {
127  return entityrep_ < 0 ? ~entityrep_ : entityrep_;
128  }
129 
131  int signedIndex() const
132  {
133  return entityrep_;
134  }
139  bool orientation() const
140  {
141  return entityrep_ >= 0;
142  }
143 
147  {
148  return EntityRep(~entityrep_);
149  }
150 
152  void increment()
153  {
154  if (entityrep_ < 0) {
155  --entityrep_;
156  } else {
157  ++entityrep_;
158  }
159  }
160 
166  bool operator<(const EntityRep& other) const
167  {
168  int i1 = index();
169  int i2 = other.index();
170  if (i1 < i2) return true;
171  if (orientation() && !other.orientation()) return true;
172  return false;
173  }
174 
178  bool operator==(const EntityRep& other) const
179  {
180  return entityrep_ == other.entityrep_;
181  }
182 
186  bool operator!=(const EntityRep& other) const
187  {
188  return !operator==(other);
189  }
190 
191  enum { InvalidIndex = INT_MAX };
192 
193  private:
201  explicit EntityRep(int erep)
202  : entityrep_(erep)
203  {
204  }
205 
206  // Interior representation is documented in class main comment.
207  int entityrep_;
208  };
209 
210 
211 
216  template <typename T>
217  class EntityVariableBase : private std::vector<T>
218  {
219  friend class CpGridData;
220  public:
221  typedef std::vector<T> V;
222  typedef typename std::vector<T>::iterator iterator;
223  typedef typename std::vector<T>::const_iterator const_iterator;
224 
225  using V::empty;
226  using V::size;
227  using V::assign;
228  using V::begin;
229  using V::end;
230  using typename V::value_type;
231  using V::reserve;
232  using V::push_back;
233  using V::data;
234  using V::operator[];
235  using V::resize;
236 
239  {
240  }
241 
242  const T& get(int i) const
243  {
244  return V::operator[](i);
245  }
246 
247  T& get(int i)
248  {
249  return V::operator[](i);
250  }
251 
252  void swap(EntityVariableBase& other)
253  {
254  V::swap(static_cast<V&>(other));
255  }
256 
257  };
258 
259 
260 
261 
269  template <typename T, int codim>
271  {
272  public:
275  {
276  }
280  const T& operator[](const EntityRep<codim>& e) const
281  {
282  return EntityVariableBase<T>::get(e.index());
283  }
288  {
289  return EntityVariableBase<T>::get(e.index());
290  }
291  };
292 
293 
294 
295 
296 
304  template <typename T, int codim>
306  {
307  public:
310  {
311  }
315  const T operator[](const EntityRep<codim>& e) const
316  {
317  return e.orientation() ?
318  EntityVariableBase<T>::get(e.index()) :
319  -EntityVariableBase<T>::get(e.index());
320  }
321  };
322 
323 
324  } // namespace cpgrid
325 } // namespace Dune
326 
327 
328 
329 
330 #endif // OPM_ENTITYREP_HEADER
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:125
Base class for EntityVariable and SignedEntityVariable.
Definition: EntityRep.hpp:217
const T operator[](const EntityRep< codim > &e) const
Random access to the variable through an EntityRep.
Definition: EntityRep.hpp:315
bool orientation() const
Returns true if the entity has positive orientation.
Definition: EntityRep.hpp:139
EntityRep opposite() const
Returns an EntityRep with opposite orientation.
Definition: EntityRep.hpp:146
EntityVariableBase()
Default constructor.
Definition: EntityRep.hpp:238
A class design to hold a variable with a value for each entity of the given codimension, where the variable is changing in sign with orientation.
Definition: EntityRep.hpp:305
The namespace Dune is the main namespace for all Dune code.
Definition: CartesianIndexMapper.hpp:9
bool operator<(const EntityRep &other) const
Ordering relation used for maps etc.
Definition: EntityRep.hpp:166
const T & operator[](const EntityRep< codim > &e) const
Random access to the variable through an EntityRep.
Definition: EntityRep.hpp:280
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:117
bool operator==(const EntityRep &other) const
Equality operator.
Definition: EntityRep.hpp:178
SignedEntityVariable()
Default constructor.
Definition: EntityRep.hpp:309
EntityVariable()
Default constructor.
Definition: EntityRep.hpp:274
void setValue(int index_arg, bool orientation_arg)
Set entity value.
Definition: EntityRep.hpp:118
A class design to hold a variable with a value for each entity of the given codimension, where the variable is not changing in sign with orientation.
Definition: EntityRep.hpp:270
Represents an entity of a given codim, with positive or negative orientation.
Definition: CpGridData.hpp:96
void increment()
Increments the entityrep&#39;s index() by one.
Definition: EntityRep.hpp:152
int signedIndex() const
The signed index that also tells us the orientation.
Definition: EntityRep.hpp:131
bool operator!=(const EntityRep &other) const
Inequality operator.
Definition: EntityRep.hpp:186
EntityRep(int index_arg, bool orientation_arg)
Constructor taking an entity index and an orientation.
Definition: EntityRep.hpp:110
EntityRep()
Default constructor.
Definition: EntityRep.hpp:103
T & operator[](const EntityRep< codim > &e)
Random access to the variable through an EntityRep.
Definition: EntityRep.hpp:287