dune-grid  3.0-git
geometrygrid/geometry.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GEOGRID_GEOMETRY_HH
4 #define DUNE_GEOGRID_GEOMETRY_HH
5 
6 #include <utility>
7 
8 #include <dune/common/typetraits.hh>
9 
10 #include <dune/geometry/referenceelements.hh>
11 #include <dune/geometry/multilineargeometry.hh>
12 
15 
16 namespace Dune
17 {
18 
19  namespace GeoGrid
20  {
21 
22  // InferHasSingleGeometryType
23  // --------------------------
24 
25  template< class hasSingleGeometryType, int dim, int mydim >
27  {
28  private:
29  static const unsigned int id = hasSingleGeometryType::topologyId;
30  static const unsigned int idMask = (1u << mydim) - 1u;
31 
32  public:
33  static const bool v = hasSingleGeometryType::v && ((mydim == dim) || ((id | 1u) == 1u) || ((id | 1u) == idMask));
34  static const unsigned int topologyId = (v ? id & idMask : ~0u);
35  };
36 
37  template< class hasSingleGeometryType, int dim >
38  struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 1 >
39  {
40  static const bool v = true;
41  static const unsigned int topologyId = GenericGeometry::CubeTopology< 1 >::type::id;
42  };
43 
44  template< class hasSingleGeometryType, int dim >
45  struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 0 >
46  {
47  static const bool v = true;
48  static const unsigned int topologyId = GenericGeometry::CubeTopology< 0 >::type::id;
49  };
50 
51 
52 
53  // GeometryTraits
54  // --------------
55 
56  template< class Grid >
58  {
59  typedef typename std::remove_const< Grid >::type::Traits Traits;
60 
61  typedef typename Traits::ctype ctype;
62 
63  typedef GenericGeometry::MatrixHelper< GenericGeometry::DuneCoordTraits< ctype > > MatrixHelper;
64 
65  static ctype tolerance () { return 16 * std::numeric_limits< ctype >::epsilon(); }
66 
67  template< int mydim, int cdim >
69  {
71  };
72 
73  template< int mydim >
75  : public InferHasSingleGeometryType< Capabilities::hasSingleGeometryType< Grid >, Traits::dimension, mydim >
76  {};
77  };
78 
79 
80 
81  // Geometry
82  // --------
83 
84  template< int mydim, int cdim, class Grid >
85  class Geometry
86  {
88 
89  typedef typename std::remove_const< Grid >::type::Traits Traits;
90 
91  template< int, int, class > friend class Geometry;
92 
93  public:
94  typedef typename Traits::ctype ctype;
95 
96  static const int mydimension = mydim;
97  static const int coorddimension = cdim;
98  static const int dimension = Traits::dimension;
99  static const int codimension = dimension - mydimension;
100 
101  protected:
102  typedef CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping;
103 
104  struct Mapping
105  : public BasicMapping
106  {
107  template< class CoordVector >
108  Mapping ( const GeometryType &type, const CoordVector &coords )
109  : BasicMapping( type, coords ),
110  refCount_( 0 )
111  {}
112 
113  void addReference () { ++refCount_; }
114  bool removeReference () { return (--refCount_ == 0); }
115 
116  private:
117  unsigned int refCount_;
118  };
119 
120  public:
121  typedef typename Mapping::LocalCoordinate LocalCoordinate;
122  typedef typename Mapping::GlobalCoordinate GlobalCoordinate;
123 
124  typedef typename Mapping::JacobianTransposed JacobianTransposed;
125  typedef typename Mapping::JacobianInverseTransposed JacobianInverseTransposed;
126 
127  Geometry () : grid_( nullptr ), mapping_( nullptr ) {}
128 
129  explicit Geometry ( const Grid &grid ) : grid_( &grid ), mapping_( nullptr ) {}
130 
131  template< class CoordVector >
132  Geometry ( const Grid &grid, const GeometryType &type, const CoordVector &coords )
133  : grid_( &grid )
134  {
135  assert( int( type.dim() ) == mydimension );
136  void *mappingStorage = grid.allocateStorage( sizeof( Mapping ) );
137  mapping_ = new( mappingStorage ) Mapping( type, coords );
138  mapping_->addReference();
139  }
140 
141  Geometry ( const This &other )
142  : grid_( other.grid_ ),
143  mapping_( other.mapping_ )
144  {
145  if( mapping_ )
146  mapping_->addReference();
147  }
148 
149  Geometry ( This&& other )
150  : grid_( other.grid_ ),
151  mapping_( other.mapping_ )
152  {
153  other.grid_ = nullptr;
154  other.mapping_ = nullptr;
155  }
156 
158  {
159  if( mapping_ && mapping_->removeReference() )
160  destroyMapping();
161  }
162 
163  const This &operator= ( const This &other )
164  {
165  if( other.mapping_ )
166  other.mapping_->addReference();
167  if( mapping_ && mapping_->removeReference() )
168  destroyMapping();
169  grid_ = other.grid_;
170  mapping_ = other.mapping_;
171  return *this;
172  }
173 
174  const This &operator= ( This&& other )
175  {
176  using std::swap;
177  swap( grid_, other.grid_ );
178  swap( mapping_, other.mapping_ );
179  return *this;
180  }
181 
182  operator bool () const { return bool( mapping_ ); }
183 
184  bool affine () const { return mapping_->affine(); }
185  GeometryType type () const { return mapping_->type(); }
186 
187  int corners () const { return mapping_->corners(); }
188  GlobalCoordinate corner ( const int i ) const { return mapping_->corner( i ); }
189  GlobalCoordinate center () const { return mapping_->center(); }
190 
191  GlobalCoordinate global ( const LocalCoordinate &local ) const { return mapping_->global( local ); }
192  LocalCoordinate local ( const GlobalCoordinate &global ) const { return mapping_->local( global ); }
193 
194  ctype integrationElement ( const LocalCoordinate &local ) const { return mapping_->integrationElement( local ); }
195  ctype volume () const { return mapping_->volume(); }
196 
197  JacobianTransposed jacobianTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianTransposed( local ); }
198  JacobianInverseTransposed jacobianInverseTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianInverseTransposed( local ); }
199 
200  const Grid &grid () const { assert( grid_ ); return *grid_; }
201 
202  private:
203  void destroyMapping ()
204  {
205  mapping_->~Mapping();
206  grid().deallocateStorage( mapping_, sizeof( Mapping ) );
207  }
208 
209  const Grid *grid_;
210  Mapping* mapping_;
211  };
212 
213  } // namespace GeoGrid
214 
215 } // namespace Dune
216 
217 #endif // #ifndef DUNE_GEOGRID_GEOMETRY_HH
Dune::GeoGrid::Geometry::JacobianInverseTransposed
Mapping::JacobianInverseTransposed JacobianInverseTransposed
Definition: geometrygrid/geometry.hh:125
Dune::GeoGrid::Geometry::Mapping::addReference
void addReference()
Definition: geometrygrid/geometry.hh:113
Dune::GeoGrid::Geometry::mydimension
static const int mydimension
Definition: geometrygrid/geometry.hh:96
Dune::GeoGrid::Geometry::LocalCoordinate
Mapping::LocalCoordinate LocalCoordinate
Definition: geometrygrid/geometry.hh:121
Dune::GeoGrid::InferHasSingleGeometryType::v
static const bool v
Definition: geometrygrid/geometry.hh:33
Dune::GeoGrid::Geometry::BasicMapping
CachedMultiLinearGeometry< ctype, mydimension, coorddimension, GeometryTraits< Grid > > BasicMapping
Definition: geometrygrid/geometry.hh:102
Dune
Include standard header files.
Definition: agrid.hh:59
Dune::GeoGrid::GeometryTraits::hasSingleGeometryType
Definition: geometrygrid/geometry.hh:74
Dune::GeoGrid::Geometry::grid
const Grid & grid() const
Definition: geometrygrid/geometry.hh:200
Dune::GeoGrid::Geometry::jacobianTransposed
JacobianTransposed jacobianTransposed(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:197
Dune::GeoGrid::Geometry::codimension
static const int codimension
Definition: geometrygrid/geometry.hh:99
Dune::GeoGrid::GeometryTraits::ctype
Traits::ctype ctype
Definition: geometrygrid/geometry.hh:61
Dune::GeoGrid::Geometry::Geometry
Geometry(const This &other)
Definition: geometrygrid/geometry.hh:141
Dune::GeoGrid::InferHasSingleGeometryType
Definition: geometrygrid/geometry.hh:26
Dune::GeoGrid::Geometry::type
GeometryType type() const
Definition: geometrygrid/geometry.hh:185
Dune::GeoGrid::GeometryTraits::CornerStorage::Type
GeoGrid::CornerStorage< mydim, cdim, Grid > Type
Definition: geometrygrid/geometry.hh:70
Dune::GeoGrid::Geometry::Mapping::removeReference
bool removeReference()
Definition: geometrygrid/geometry.hh:114
Dune::GeoGrid::Geometry::~Geometry
~Geometry()
Definition: geometrygrid/geometry.hh:157
Dune::GeoGrid::CornerStorage
Definition: cornerstorage.hh:172
Dune::GeoGrid::Geometry::global
GlobalCoordinate global(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:191
Dune::VTK::GeometryType
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
Dune::GeoGrid::Geometry::coorddimension
static const int coorddimension
Definition: geometrygrid/geometry.hh:97
cornerstorage.hh
Dune::GeoGrid::Geometry::corners
int corners() const
Definition: geometrygrid/geometry.hh:187
Dune::GeoGrid::Geometry::Mapping
Definition: geometrygrid/geometry.hh:104
Dune::GeoGrid::Geometry::jacobianInverseTransposed
JacobianInverseTransposed jacobianInverseTransposed(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:198
Dune::GeoGrid::GeometryTraits::CornerStorage
Definition: geometrygrid/geometry.hh:68
Dune::GeoGrid::Geometry::GlobalCoordinate
Mapping::GlobalCoordinate GlobalCoordinate
Definition: geometrygrid/geometry.hh:122
Dune::GeoGrid::CoordVector
Definition: cornerstorage.hh:20
Dune::GeoGrid::Geometry::Geometry
Geometry(const Grid &grid, const GeometryType &type, const CoordVector &coords)
Definition: geometrygrid/geometry.hh:132
Dune::GeoGrid::Geometry::corner
GlobalCoordinate corner(const int i) const
Definition: geometrygrid/geometry.hh:188
Dune::GeoGrid::Geometry::volume
ctype volume() const
Definition: geometrygrid/geometry.hh:195
Dune::GeoGrid::Geometry
Definition: geometrygrid/geometry.hh:85
Dune::Grid
Grid abstract base class.
Definition: common/grid.hh:373
capabilities.hh
A set of traits classes to store static information about grid implementation.
Dune::GeoGrid::GeometryTraits
Definition: geometrygrid/geometry.hh:57
Dune::GeoGrid::Geometry::Geometry
Geometry()
Definition: geometrygrid/geometry.hh:127
Dune::GeoGrid::GeometryTraits::tolerance
static ctype tolerance()
Definition: geometrygrid/geometry.hh:65
Dune::GeoGrid::GeometryTraits::Traits
std::remove_const< Grid >::type::Traits Traits
Definition: geometrygrid/geometry.hh:59
Dune::GeoGrid::GeometryTraits::MatrixHelper
GenericGeometry::MatrixHelper< GenericGeometry::DuneCoordTraits< ctype > > MatrixHelper
Definition: geometrygrid/geometry.hh:63
Dune::GeoGrid::Geometry::affine
bool affine() const
Definition: geometrygrid/geometry.hh:184
Dune::GeoGrid::Geometry::Mapping::Mapping
Mapping(const GeometryType &type, const CoordVector &coords)
Definition: geometrygrid/geometry.hh:108
Dune::GeoGrid::InferHasSingleGeometryType::topologyId
static const unsigned int topologyId
Definition: geometrygrid/geometry.hh:34
Dune::GeoGrid::Geometry::operator=
const This & operator=(const This &other)
Definition: geometrygrid/geometry.hh:163
Dune::GeoGrid::Geometry::center
GlobalCoordinate center() const
Definition: geometrygrid/geometry.hh:189
Dune::GeoGrid::Geometry::Geometry
Geometry(const Grid &grid)
Definition: geometrygrid/geometry.hh:129
Dune::GeoGrid::Geometry::Geometry
Geometry(This &&other)
Definition: geometrygrid/geometry.hh:149
Dune::GeoGrid::Geometry::integrationElement
ctype integrationElement(const LocalCoordinate &local) const
Definition: geometrygrid/geometry.hh:194
Dune::GeoGrid::Geometry::dimension
static const int dimension
Definition: geometrygrid/geometry.hh:98
Dune::GeoGrid::Geometry::JacobianTransposed
Mapping::JacobianTransposed JacobianTransposed
Definition: geometrygrid/geometry.hh:124
Dune::GeoGrid::Geometry::ctype
Traits::ctype ctype
Definition: geometrygrid/geometry.hh:94