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 <opm/grid/utility/ErrorMacros.hpp>
73 #include <climits>
74 //#include <boost/algorithm/minmax_element.hpp>
75 #include <vector>
76 
78 namespace Dune
79 {
80  namespace cpgrid
81  {
82 
96 
97  template <int codim>
98  class EntityRep
99  {
100  public:
101  enum{ codimension=codim};
102 
105  : entityrep_(0)
106  {
107  }
111  EntityRep(int index_arg, bool orientation_arg)
112  : entityrep_(orientation_arg ? index_arg : ~index_arg)
113  {
114  assert(index_arg >= 0);
115  }
119  void setValue(int index_arg, bool orientation_arg)
120  {
121  assert(index_arg >= 0);
122  entityrep_ = orientation_arg ? index_arg : ~index_arg;
123  }
126  int index() const
127  {
128  return entityrep_ < 0 ? ~entityrep_ : entityrep_;
129  }
130 
135  bool orientation() const
136  {
137  return entityrep_ >= 0;
138  }
139 
143  {
144  return EntityRep(~entityrep_);
145  }
146 
148  void increment()
149  {
150  if (entityrep_ < 0) {
151  --entityrep_;
152  } else {
153  ++entityrep_;
154  }
155  }
156 
162  bool operator<(const EntityRep& other) const
163  {
164  int i1 = index();
165  int i2 = other.index();
166  if (i1 < i2) return true;
167  if (orientation() && !other.orientation()) return true;
168  return false;
169  }
170 
174  bool operator==(const EntityRep& other) const
175  {
176  return entityrep_ == other.entityrep_;
177  }
178 
182  bool operator!=(const EntityRep& other) const
183  {
184  return !operator==(other);
185  }
186 
187  enum { InvalidIndex = INT_MAX };
188 
189  private:
197  explicit EntityRep(int erep)
198  : entityrep_(erep)
199  {
200  }
201 
202  // Interior representation is documented in class main comment.
203  int entityrep_;
204  };
205 
206 
207 
212  template <typename T>
213  class EntityVariableBase : private std::vector<T>
214  {
215  friend class CpGridData;
216  public:
217  typedef std::vector<T> V;
218  typedef typename std::vector<T>::iterator iterator;
219  typedef typename std::vector<T>::const_iterator const_iterator;
220 
221  using V::empty;
222  using V::size;
223  using V::assign;
224  using V::begin;
225 
228  {
229  }
230 
231  const T& get(int i) const
232  {
233  return V::operator[](i);
234  }
235  };
236 
237 
238 
239 
247  template <typename T, int codim>
249  {
250  public:
253  {
254  }
258  const T& operator[](const EntityRep<codim>& e) const
259  {
260  return EntityVariableBase<T>::get(e.index());
261  }
262  };
263 
264 
265 
266 
267 
275  template <typename T, int codim>
277  {
278  public:
281  {
282  }
286  const T operator[](const EntityRep<codim>& e) const
287  {
288  return e.orientation() ?
289  EntityVariableBase<T>::get(e.index()) :
290  -EntityVariableBase<T>::get(e.index());
291  }
292  };
293 
294 
295  } // namespace cpgrid
296 } // namespace Dune
297 
298 
299 
300 
301 #endif // OPM_ENTITYREP_HEADER
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:126
Base class for EntityVariable and SignedEntityVariable.
Definition: EntityRep.hpp:213
const T operator[](const EntityRep< codim > &e) const
Random access to the variable through an EntityRep.
Definition: EntityRep.hpp:286
bool orientation() const
Returns true if the entity has positive orientation.
Definition: EntityRep.hpp:135
EntityRep opposite() const
Returns an EntityRep with opposite orientation.
Definition: EntityRep.hpp:142
EntityVariableBase()
Default constructor.
Definition: EntityRep.hpp:227
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:276
Holds the implementation of the CpGrid as a pimple.
Definition: OpmParserIncludes.hpp:42
bool operator<(const EntityRep &other) const
Ordering relation used for maps etc.
Definition: EntityRep.hpp:162
const T & operator[](const EntityRep< codim > &e) const
Random access to the variable through an EntityRep.
Definition: EntityRep.hpp:258
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:105
bool operator==(const EntityRep &other) const
Equality operator.
Definition: EntityRep.hpp:174
SignedEntityVariable()
Default constructor.
Definition: EntityRep.hpp:280
EntityVariable()
Default constructor.
Definition: EntityRep.hpp:252
void setValue(int index_arg, bool orientation_arg)
Set entity value.
Definition: EntityRep.hpp:119
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:248
Represents an entity of a given codim, with positive or negative orientation.
Definition: CpGridData.hpp:94
void increment()
Increments the entityrep&#39;s index() by one.
Definition: EntityRep.hpp:148
bool operator!=(const EntityRep &other) const
Inequality operator.
Definition: EntityRep.hpp:182
EntityRep(int index_arg, bool orientation_arg)
Constructor taking an entity index and an orientation.
Definition: EntityRep.hpp:111
EntityRep()
Default constructor.
Definition: EntityRep.hpp:104