entity.hh 10.8 KB
Newer Older
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
1
2
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
Praetorius, Simon's avatar
Praetorius, Simon committed
3
4
#ifndef DUNE_CURVED_SURFACE_GRID_ENTITY_HH
#define DUNE_CURVED_SURFACE_GRID_ENTITY_HH
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
5

6
#include <dune/common/std/optional.hh>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
7
8
9
10
11
12
#include <dune/geometry/referenceelements.hh>

#include <dune/grid/common/grid.hh>

namespace Dune
{
Praetorius, Simon's avatar
Praetorius, Simon committed
13
  namespace CGeo
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
14
15
16
17
18
19
20
  {

    // Internal Forward Declarations
    // -----------------------------

    /** \class EntityBase
     *  \brief actual implementation of the entity
Praetorius, Simon's avatar
Praetorius, Simon committed
21
     *  \ingroup CurvedGeo
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
22
23
24
25
     *
     *  \tparam  codim  codimension of the entity
     *  \tparam  Grid   CurvedSurfaceGrid, this entity belongs to
     */
Praetorius, Simon's avatar
Praetorius, Simon committed
26
    template< int codim, class Grid >
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
27
28
29
30
    class EntityBase;

    /** \class Entity
     *  \brief DUNE-conform implementation of the entity
Praetorius, Simon's avatar
Praetorius, Simon committed
31
     *  \ingroup CurvedGeo
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
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
     *
     *  This class merely changes the template parameters of the entity to make
     *  DUNE happy. The actual implementation of the entity can be found in
     *  EntityBase.
     *
     *  \tparam  codim  codimension of the entity
     *  \tparam  dim    dimension of the Grid (redundant information)
     *  \tparam  Grid   CurvedSurfaceGrid, this entity belongs to
     */
    template< int codim, int dim, class Grid >
    class Entity;



    // External Forward Declarations
    // -----------------------------

    template< class Grid >
    class HierarchicIterator;

    template< class Grid, class HostIntersectionIterator >
    class IntersectionIterator;



    // EntityBase (real)
    // -----------------

    /** \copydoc EntityBase
     *
     *  This specialization implements the case, where the host grid provides
     *  the entity for this codimension, i.e., \em fake = \b false.
     *
     *  \nosubgrouping
     */
    template< int codim, class Grid >
Praetorius, Simon's avatar
Praetorius, Simon committed
68
    class EntityBase
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
69
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
70
      using Traits = typename std::remove_const_t<Grid>::Traits;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    public:
      /** \name Attributes
       *  \{ */

      //! codimensioon of the entity
      static const int codimension = codim;
      //! dimension of the grid
      static const int dimension = Traits::dimension;
      //! dimension of the entity
      static const int mydimension = dimension - codimension;
      //! dimension of the world
      static const int dimensionworld = Traits::dimensionworld;

      /** \} */

      /** \name Types Required by DUNE
       *  \{ */

      //! coordinate type of the grid
Praetorius, Simon's avatar
Praetorius, Simon committed
91
      using ctype = typename Traits::ctype;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
92
93

      //! type of corresponding geometry
Praetorius, Simon's avatar
Praetorius, Simon committed
94
95
      using Geometry = typename Traits::template Codim<codimension>::Geometry;

Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
96
97
98
      /** \} */

    private:
Praetorius, Simon's avatar
Praetorius, Simon committed
99
      using HostGrid = typename Traits::HostGrid;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
100
101
102
103
104
105

    public:
      /** \name Host Types
       *  \{ */

      //! type of corresponding host entity
Praetorius, Simon's avatar
Praetorius, Simon committed
106
      using HostEntity = typename HostGrid::template Codim<codimension>::Entity;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
107
108

      //! type of corresponding entity seed
Praetorius, Simon's avatar
Praetorius, Simon committed
109
      using EntitySeed = typename Traits::template Codim<codimension>::EntitySeed;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
110
111

      //! type of host elements, i.e., of host entities of codimension 0
Praetorius, Simon's avatar
Praetorius, Simon committed
112
      using HostElement = typename HostGrid::template Codim<0>::Entity;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
113

Praetorius, Simon's avatar
Praetorius, Simon committed
114
      /** \} */
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
115
116

    private:
Praetorius, Simon's avatar
Praetorius, Simon committed
117
118
      using GeometryImpl = typename Traits::template Codim<codimension>::GeometryImpl;
      using HostGeometry = typename HostGrid::template Codim<codimension>::Geometry;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
119
120
121
122
123

    public:
      /** \name Construction, Initialization and Destruction
       *  \{ */

Praetorius, Simon's avatar
Praetorius, Simon committed
124
      EntityBase () = default;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
125

Praetorius, Simon's avatar
Praetorius, Simon committed
126
127
128
129
      // Construct the entity from an entity seed
      EntityBase (const Grid& grid, const EntitySeed& seed)
        : hostEntity_(grid.hostGrid().entity(seed.impl().hostEntitySeed()))
        , grid_(&grid)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
130
131
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
132
133
134
135
      // construct the entity from a subentity of a host-entity
      EntityBase (const Grid& grid, const HostElement& hostElement, int i)
        : hostEntity_(hostElement.template subEntity<codim>(i))
        , grid_(&grid)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
136
137
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
138
139
140
141
      // construct the entity from a host-entity
      EntityBase (const Grid& grid, const HostEntity& hostEntity)
        : hostEntity_(hostEntity)
        , grid_(&grid)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
142
143
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
144
145
146
147
      // construct the entity from a host-entity
      EntityBase (const Grid& grid, HostEntity&& hostEntity)
        : hostEntity_(std::move(hostEntity))
        , grid_(&grid)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
148
149
150
151
      {}

      /** \} */

Praetorius, Simon's avatar
Praetorius, Simon committed
152
153
      //! compare two entities
      bool equals (const EntityBase& other) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
154
155
156
157
158
159
160
161
      {
        return hostEntity_ == other.hostEntity_;
      }

    public:
      /** \name Methods Shared by Entities of All Codimensions
       *  \{ */

Praetorius, Simon's avatar
Praetorius, Simon committed
162
163
      //! obtain the name of the corresponding reference element
      /**
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
164
165
166
167
168
169
170
       *  This type can be used to access the DUNE reference element.
       */
      GeometryType type () const
      {
        return hostEntity().type();
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
171
      //! obtain the level of this entity
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
172
173
174
175
176
      int level () const
      {
        return hostEntity().level();
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
177
      //! obtain the partition type of this entity
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
178
179
180
181
182
      PartitionType partitionType () const
      {
        return hostEntity().partitionType();
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
183
184
      //! obtain the geometry of this entity
      /**
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
185
186
187
188
       *  Each DUNE entity encapsulates a geometry object, representing the map
       *  from the reference element to world coordinates. Wrapping the geometry
       *  is the main objective of the CurvedSurfaceGrid.
       *
Praetorius, Simon's avatar
Praetorius, Simon committed
189
190
       *  The CurvedSurfaceGrid provides geometries by parametrization with local basis
       *  functions, using the CurvedGeometry.
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
191
       *
Praetorius, Simon's avatar
Praetorius, Simon committed
192
       *  \returns a new curvedgeometry object
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
193
194
195
       */
      Geometry geometry () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
196
        if (!geo_) {
197
198
199
200
201
202
          // mapping from local to curved global coordinates
          auto ff = [f=grid().coordFunction(),geo=hostEntity().geometry()](const auto& local) {
            return f(geo.global(local));
          };

          if (grid_->useGeometryCaching()) {
Praetorius, Simon's avatar
Praetorius, Simon committed
203
            auto const& idSet = grid_->hostGrid().localIdSet();
204
205
206
207
208
209
            auto id = idSet.id(hostEntity());

            auto& cache = std::get<codim>(grid_->geometryCache_);
            auto [it,inserted] = cache.try_emplace(id, type(), ff);

            geo_ = it->second;
Stenger, Florian's avatar
Stenger, Florian committed
210
          }
211
212
          else {
            geo_ = GeometryImpl(type(), ff);
Stenger, Florian's avatar
Stenger, Florian committed
213
          }
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
214
215
        }

Praetorius, Simon's avatar
Praetorius, Simon committed
216
        return Geometry(*geo_);
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
217
218
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
219
220
      //! obtain number of sub-entities of the current entity
      unsigned int subEntities (unsigned int cc) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
221
      {
222
        return hostEntity().subEntities(cc);
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
223
224
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
225
226
      //! return EntitySeed of host grid entity
      EntitySeed seed () const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
227
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
228
        return typename EntitySeed::Implementation(hostEntity().seed());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
229
230
231
232
233
      }

      /** \} */


Praetorius, Simon's avatar
Praetorius, Simon committed
234
      /** \name Methods Supporting the Grid Implementation
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
235
236
       *  \{ */

Praetorius, Simon's avatar
Praetorius, Simon committed
237
      const Grid& grid () const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
238
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
239
240
        assert(grid_);
        return *grid_;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
241
242
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
243
244
      //! return the wrapped host-entity
      const HostEntity& hostEntity () const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
245
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
246
        return hostEntity_;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
247
248
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
249
      /** \} */
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
250
251

    private:
Praetorius, Simon's avatar
Praetorius, Simon committed
252
253
      HostEntity hostEntity_ = {};
      const Grid* grid_ = nullptr;
254
      mutable Std::optional<GeometryImpl> geo_;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
255
256
257
258
259
260
261
262
    };


    // Entity
    // ------

    template< int codim, int dim, class Grid >
    class Entity
Praetorius, Simon's avatar
Praetorius, Simon committed
263
        : public EntityBase<codim, Grid>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
264
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
265
      using Super = EntityBase<codim, Grid>;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
266
267

    public:
Praetorius, Simon's avatar
Praetorius, Simon committed
268
269
      // import constructors from base class
      using Super::Super;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
270
271
272
273
274
275
276
    };


    // Entity for codimension 0
    // ------------------------

    template< int dim, class Grid >
Praetorius, Simon's avatar
Praetorius, Simon committed
277
278
    class Entity<0, dim, Grid>
        : public EntityBase<0, Grid>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
279
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
280
      using Super = EntityBase<0, Grid>;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
281

Praetorius, Simon's avatar
Praetorius, Simon committed
282
283
      using Traits = typename std::remove_const_t<Grid>::Traits;
      using HostGrid = typename Traits::HostGrid;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
284
285
286
287
288
289
290

    public:

      /** \name Types Required by DUNE
       *  \{ */

      //! type of corresponding local geometry
Praetorius, Simon's avatar
Praetorius, Simon committed
291
      using LocalGeometry = typename Traits::template Codim<0>::LocalGeometry;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
292

Praetorius, Simon's avatar
Praetorius, Simon committed
293
294
      //! facade type for entities
      using EntityFacade = Dune::Entity<0, dim, Grid, Dune::CGeo::Entity>;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
295
296

      //! type of hierarchic iterator
Praetorius, Simon's avatar
Praetorius, Simon committed
297
298
      using HierarchicIterator = typename Traits::HierarchicIterator;

Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
299
      //! type of leaf intersection iterator
Praetorius, Simon's avatar
Praetorius, Simon committed
300
301
      using LeafIntersectionIterator = typename Traits::LeafIntersectionIterator;

Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
302
      //! type of level intersection iterator
Praetorius, Simon's avatar
Praetorius, Simon committed
303
      using LevelIntersectionIterator = typename Traits::LevelIntersectionIterator;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
304
305
306

      /** \} */

Praetorius, Simon's avatar
Praetorius, Simon committed
307
308
      // import constructors from base class
      using Super::Super;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
309
310

      template< int codim >
Praetorius, Simon's avatar
Praetorius, Simon committed
311
      typename Grid::template Codim<codim>::Entity subEntity (int i) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
312
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
313
314
        using EntityImpl = typename Traits::template Codim<codim>::EntityImpl;
        return EntityImpl(Super::grid(), Super::hostEntity(), i);
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
315
316
317
318
      }

      LevelIntersectionIterator ilevelbegin () const
      {
319
320
        using IteratorImpl = CGeo::IntersectionIterator<Grid, typename HostGrid::LevelIntersectionIterator>;
        return IteratorImpl(*this, Super::hostEntity().ilevelbegin());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
321
322
323
324
      }

      LevelIntersectionIterator ilevelend () const
      {
325
326
        using IteratorImpl = CGeo::IntersectionIterator<Grid, typename HostGrid::LevelIntersectionIterator>;
        return IteratorImpl(*this, Super::hostEntity().ilevelend());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
327
328
329
330
      }

      LeafIntersectionIterator ileafbegin () const
      {
331
332
        using IteratorImpl = CGeo::IntersectionIterator<Grid, typename HostGrid::LeafIntersectionIterator>;
        return IteratorImpl(*this, Super::hostEntity().ileafbegin());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
333
334
335
336
      }

      LeafIntersectionIterator ileafend () const
      {
337
338
        using IteratorImpl = CGeo::IntersectionIterator<Grid, typename HostGrid::LeafIntersectionIterator>;
        return IteratorImpl(*this, Super::hostEntity().ileafend());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
339
340
341
342
      }

      bool hasBoundaryIntersections () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
343
        return Super::hostEntity().hasBoundaryIntersections();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
344
345
346
347
      }

      bool isLeaf () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
348
        return Super::hostEntity().isLeaf();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
349
350
351
352
      }

      EntityFacade father () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
353
        return Entity(Super::grid(), Super::hostEntity().father());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
354
355
356
357
      }

      bool hasFather () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
358
        return Super::hostEntity().hasFather();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
359
360
361
362
      }

      LocalGeometry geometryInFather () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
363
        return Super::hostEntity().geometryInFather();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
364
365
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
366
      HierarchicIterator hbegin (int maxLevel) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
367
      {
368
369
        using IteratorImpl = CGeo::HierarchicIterator<Grid>;
        return IteratorImpl(Super::grid(), Super::hostEntity().hbegin(maxLevel));
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
370
371
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
372
      HierarchicIterator hend (int maxLevel) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
373
      {
374
375
        using IteratorImpl = CGeo::HierarchicIterator<Grid>;
        return IteratorImpl(Super::grid(), Super::hostEntity().hend(maxLevel));
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
376
377
378
379
      }

      bool isRegular () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
380
        return Super::hostEntity().isRegular();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
381
382
383
384
      }

      bool isNew () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
385
        return Super::hostEntity().isNew();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
386
387
388
389
      }

      bool mightVanish () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
390
        return Super::hostEntity().mightVanish();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
391
392
393
      }
    };

Praetorius, Simon's avatar
Praetorius, Simon committed
394
  } // namespace CGeo
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
395
396
} // namespace Dune

Praetorius, Simon's avatar
Praetorius, Simon committed
397
#endif // DUNE_CURVED_SURFACE_GRID_ENTITY_HH