geometry.hh 6.68 KB
Newer Older
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
1
2
3
4
5
6
7
8
9
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_CRVSRF_GEOMETRY_HH
#define DUNE_CRVSRF_GEOMETRY_HH

#include <utility>

#include <dune/common/typetraits.hh>

10
#include <dune/curvedgeometry/curvedgeometry.hh>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <dune/geometry/referenceelements.hh>
#include <dune/geometry/type.hh>

#include <dune/grid/common/capabilities.hh>
#include <dune/curvedsurfacegrid/coordprovider.hh>

namespace Dune
{

  namespace crvsrf
  {

    // InferHasSingleGeometryType
    // --------------------------

    template< class hasSingleGeometryType, int dim, int mydim >
    struct InferHasSingleGeometryType
    {
    private:
      static const unsigned int id = hasSingleGeometryType::topologyId;
      static const unsigned int idMask = (1u << mydim) - 1u;

    public:
      static const bool v = hasSingleGeometryType::v && ((mydim == dim) || ((id | 1u) == 1u) || ((id | 1u) == idMask));
      static const unsigned int topologyId = (v ? id & idMask : ~0u);
    };

    template< class hasSingleGeometryType, int dim >
    struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 1 >
    {
      static const bool v = true;
      static const unsigned int topologyId = Impl::CubeTopology< 1 >::type::id;
    };

    template< class hasSingleGeometryType, int dim >
    struct InferHasSingleGeometryType< hasSingleGeometryType, dim, 0 >
    {
      static const bool v = true;
      static const unsigned int topologyId = Impl::CubeTopology< 0 >::type::id;
    };

    // GeometryTraits
    // --------------

    template< class Grid >
    struct GeometryTraits
    {
      typedef typename std::remove_const< Grid >::type::Traits Traits;

      typedef typename Traits::ctype ctype;

      typedef Impl::FieldMatrixHelper< ctype > MatrixHelper;

      static ctype tolerance () { return 16 * std::numeric_limits< ctype >::epsilon(); }

      template< int mydim >
      struct hasSingleGeometryType
        : public InferHasSingleGeometryType< Capabilities::hasSingleGeometryType< Grid >, Traits::dimension, mydim >
      {};
    };



    // Geometry
    // --------

    template< int mydim, int cdim, class Grid >
    class Geometry
    {
      typedef Geometry< mydim, cdim, Grid > This;

      typedef typename std::remove_const< Grid >::type::Traits Traits;

      template< int, int, class > friend class Geometry;

    public:
      typedef typename Traits::ctype ctype;

      static const int mydimension = mydim;
      static const int coorddimension = cdim;
      static const int dimension = Traits::dimension;
      static const int codimension = dimension - mydimension;

    protected:
95
      typedef CurvedGeometry<ctype, mydimension, coorddimension, Grid::order> BasicMapping;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
96
97
98
99
100
101
102
103

      struct Mapping
        : public BasicMapping
      {
        typedef typename BasicMapping::GlobalCoordinate GlobalCoordinate;

        template< class CoordVector >
        Mapping ( const GeometryType &type, const CoordVector &coords )
104
          : BasicMapping( type, coords ),
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            refCount_( 0 )
        {}

        void addReference () { ++refCount_; }
        bool removeReference () { return (--refCount_ == 0); }

      private:
        unsigned int refCount_;
      };

    public:
      typedef typename Mapping::LocalCoordinate LocalCoordinate;
      typedef typename Mapping::GlobalCoordinate GlobalCoordinate;

      typedef typename Mapping::JacobianTransposed JacobianTransposed;
      typedef typename Mapping::JacobianInverseTransposed JacobianInverseTransposed;

      Geometry () : grid_( nullptr ), mapping_( nullptr ) {}

      explicit Geometry ( const Grid &grid ) : grid_( &grid ), mapping_( nullptr ) {}

      template< class CoordVector >
      Geometry ( const Grid &grid, const GeometryType &type, const CoordVector &coords )
        : grid_( &grid )
      {
        assert( int( type.dim() ) == mydimension );
131
132
        std::vector<GlobalCoordinate> vertices;
        coords.calculate( vertices );
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
133
        void *mappingStorage = grid.allocateStorage( sizeof( Mapping ) );
134
        mapping_ = new( mappingStorage ) Mapping( type, vertices );
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        mapping_->addReference();
      }

      Geometry ( const This &other )
        : grid_( other.grid_ ),
          mapping_( other.mapping_ )
      {
        if( mapping_ )
          mapping_->addReference();
      }

      Geometry ( This&& other )
        : grid_( other.grid_ ),
          mapping_( other.mapping_ )
      {
        other.grid_ = nullptr;
        other.mapping_ = nullptr;
      }

      ~Geometry ()
      {
        if( mapping_ && mapping_->removeReference() )
          destroyMapping();
      }

      const This &operator= ( const This &other )
      {
        if( other.mapping_ )
          other.mapping_->addReference();
        if( mapping_ && mapping_->removeReference() )
          destroyMapping();
        grid_ = other.grid_;
        mapping_ = other.mapping_;
        return *this;
      }

      const This &operator= ( This&& other )
      {
        using std::swap;
        swap( grid_, other.grid_ );
        swap( mapping_, other.mapping_ );
        return *this;
      }

      explicit operator bool () const { return bool( mapping_ ); }

      bool affine () const { return mapping_->affine(); }
      GeometryType type () const { return mapping_->type(); }

184
185
      std::vector<GlobalCoordinate> interpolatoryVertices() const { return mapping_->vertices(); }
      int corners () const { return mapping_->corners(); }
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
186
187
188
189
      GlobalCoordinate corner ( const int i ) const { return mapping_->corner( i ); }
      GlobalCoordinate center () const { return mapping_->center(); }

      GlobalCoordinate global ( const LocalCoordinate &local ) const { return mapping_->global( local ); }
190
191
      LocalCoordinate local ( const GlobalCoordinate &global ) const
      {
192
        return mapping_->local(global);
193
      }
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
194

195
196
      GlobalCoordinate normal ( const LocalCoordinate &local ) const { return mapping_->normal( local ); }

Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
197
      ctype integrationElement ( const LocalCoordinate &local ) const { return mapping_->integrationElement( local ); }
198
      ctype volume () const { return mapping_->volume(); }
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

      JacobianTransposed jacobianTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianTransposed( local ); }
      JacobianInverseTransposed jacobianInverseTransposed ( const LocalCoordinate &local ) const { return mapping_->jacobianInverseTransposed( local ); }

      const Grid &grid () const { assert( grid_ ); return *grid_; }

    private:
      void destroyMapping ()
      {
        mapping_->~Mapping();
        grid().deallocateStorage( mapping_, sizeof( Mapping ) );
      }

      const Grid *grid_;
      Mapping* mapping_;
    };

  } // namespace crvsrf

} // namespace Dune

#endif // #ifndef DUNE_CRVSRF_GEOMETRY_HH