dune-grid  2.2.0
geometrygrid/geometry.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOGRID_GEOMETRY_HH
2 #define DUNE_GEOGRID_GEOMETRY_HH
3 
4 #include <dune/common/nullptr.hh>
5 #include <dune/common/typetraits.hh>
6 
7 #include <dune/geometry/referenceelements.hh>
8 #include <dune/geometry/genericgeometry/mappingprovider.hh>
9 
12 
13 namespace Dune
14 {
15 
16  namespace GeoGrid
17  {
18 
19  // GeometryTraits
20  // --------------
21 
22  template< int cdim, class Grid >
24  {
25  typedef typename remove_const< Grid >::type::Traits Traits;
26 
27  typedef GenericGeometry::DuneCoordTraits< typename Traits::ctype > CoordTraits;
28 
29  static const int dimWorld = cdim;
30 
31  template< class Topology >
32  struct Mapping
33  {
35  typedef GenericGeometry::CornerMapping< CoordTraits, Topology, dimWorld, CornerStorage > type;
36  };
37 
38  struct Caching
39  {
40  static const GenericGeometry::EvaluationType evaluateJacobianTransposed = GenericGeometry::ComputeOnDemand;
41  static const GenericGeometry::EvaluationType evaluateJacobianInverseTransposed = GenericGeometry::ComputeOnDemand;
42  static const GenericGeometry::EvaluationType evaluateIntegrationElement = GenericGeometry::ComputeOnDemand;
43  static const GenericGeometry::EvaluationType evaluateNormal = GenericGeometry::ComputeOnDemand;
44  };
45 
46  struct UserData
47  {
48  UserData () : refCount_( 0 ) {}
49 
50  void addReference () { ++refCount_; }
51  bool removeReference () { return (--refCount_ == 0); }
52 
53  private:
54  unsigned int refCount_;
55  };
56  };
57 
58 
59 
60  // MappingFamily
61  // -------------
62 
63  template< int cdim, class Grid >
65  {
66  typedef typename remove_const< Grid >::type::Traits Traits;
67 
68  public:
70 
71  static const int dimension = Traits::dimension;
72 
73  private:
74  template< bool >
75  struct Hybrid
76  {
77  typedef GenericGeometry::HybridMapping< dimension, GeometryTraits > ElementMapping;
78  };
79 
80  template< bool >
81  struct NonHybrid
82  {
83  static const unsigned int topologyId = Capabilities::hasSingleGeometryType< Grid >::topologyId;
84  typedef typename GenericGeometry::Topology< topologyId, dimension >::type Topology;
85  typedef GenericGeometry::NonHybridMapping< Topology, GeometryTraits > ElementMapping;
86  };
87 
88  public:
89  typedef typename SelectType< Capabilities::hasSingleGeometryType< Grid >::v, NonHybrid< true >, Hybrid< false > >::Type::ElementMapping ElementMapping;
90 
91  template< int codim >
92  struct Codim
93  {
94  typedef GenericGeometry::MappingProvider< ElementMapping, codim > MappingProvider;
95  typedef typename MappingProvider::Mapping Mapping;
96 
97  static const unsigned int maxMappingSize = MappingProvider::maxMappingSize;
98  };
99  };
100 
101 
102 
103  // Geometry
104  // --------
105 
106  template< int mydim, int cdim, class Grid >
107  class Geometry
108  {
110 
112  typedef typename remove_const< Grid >::type::Traits Traits;
113 
114  template< int, int, class > friend class Geometry;
115 
116  public:
117  static const int mydimension = mydim;
118  static const int coorddimension = cdim;
120  static const int codimension = dimension - mydimension;
121 
122  protected:
123  typedef typename MappingFamily::template Codim< codimension >::MappingProvider MappingProvider;
124  typedef typename MappingFamily::template Codim< codimension >::Mapping Mapping;
125 
126  public:
127  typedef typename Mapping::FieldType ctype;
128 
129  typedef FieldVector< ctype, mydimension > LocalCoordinate;
130  typedef FieldVector< ctype, coorddimension > GlobalCoordinate;
131 
132  typedef typename Mapping::JacobianTransposed JacobianTransposed;
133  typedef typename Mapping::JacobianInverseTransposed JacobianInverseTransposed;
134 
135  // for cenvencience: Jacobian is the name of the type in the geometry interface
137 
138  Geometry ( const Grid &grid )
139  : grid_( &grid ),
140  mapping_( nullptr )
141  {}
142 
143  template< class CoordVector >
144  Geometry ( const Grid &grid, const GeometryType &type, const CoordVector &coords )
145  : grid_( &grid )
146  {
147  char *mappingStorage = grid.template allocateMappingStorage< codimension >( type );
148  mapping_ = MappingProvider::construct( type.id(), coords, mappingStorage );
149  mapping_->userData().addReference();
150  }
151 
152  template< int fatherdim >
154  : grid_( father.grid_ )
155  {
156  const unsigned int codim = fatherdim - mydim;
157  char *mappingStorage = grid().template allocateMappingStorage< codimension >( type );
158  mapping_ = father.mapping_->template trace< codim >( i, mappingStorage );
159  mapping_->userData().addReference();
160  }
161 
162  Geometry ( const This &other )
163  : grid_( other.grid_ ),
164  mapping_( other.mapping_ )
165  {
166  if( mapping_ )
167  mapping_->userData().addReference();
168  }
169 
171  {
172  if( mapping_ && mapping_->userData().removeReference() )
173  destroyMapping();
174  }
175 
176  const This &operator= ( const This &other )
177  {
178  if( other.mapping_ )
179  other.mapping_->userData().addReference();
180  if( mapping_ && mapping_->userData().removeReference() )
181  destroyMapping();
182  grid_ = other.grid_;
183  mapping_ = other.mapping_;
184  return *this;
185  }
186 
187  operator bool () const { return bool( mapping_ ); }
188 
189  bool affine () const { return mapping_->affine(); }
190  GeometryType type () const { return mapping_->type(); }
191 
192  int corners () const { return mapping_->numCorners(); }
193  GlobalCoordinate corner ( const int i ) const { return mapping_->corner( i ); }
194  GlobalCoordinate center () const { return mapping_->center(); }
195 
196  GlobalCoordinate global ( const LocalCoordinate &local ) const { return mapping_->global( local ); }
197  LocalCoordinate local ( const GlobalCoordinate &global ) const { return mapping_->local( global ); }
198 
199  ctype integrationElement ( const LocalCoordinate &local ) const { return mapping_->integrationElement( local ); }
200  ctype volume () const { return mapping_->volume(); }
201 
202  const JacobianTransposed &jacobianTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianTransposed( local ); }
203  const JacobianInverseTransposed &jacobianInverseTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianInverseTransposed( local ); }
204 
205  const Grid &grid () const { return *grid_; }
206 
207  private:
208  void destroyMapping ()
209  {
210  const GeometryType gt = type();
211  mapping_->~Mapping();
212  grid().template deallocateMappingStorage< codimension >( gt, (char *)mapping_ );
213  }
214 
215  const Grid *grid_;
216  Mapping* mapping_;
217  };
218 
219  } // namespace GeoGrid
220 
221 
222 
223  // FacadeOptions
224  // -------------
225 
226  namespace FacadeOptions
227  {
228 
229  template< int mydim, int cdim, class Grid >
230  struct StoreGeometryReference< mydim, cdim, Grid, GeoGrid::Geometry >
231  {
232  static const bool v = false;
233  };
234 
235  } // namespace FacadeOptions
236 
237 } // namespace Dune
238 
239 #endif // #ifndef DUNE_GEOGRID_GEOMETRY_HH