entity.hh 12.7 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

Praetorius, Simon's avatar
Praetorius, Simon committed
6
7
#include <optional>

Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
8
9
10
#include <dune/geometry/referenceelements.hh>

#include <dune/grid/common/grid.hh>
11
#include <dune/curvedsurfacegrid/geometry.hh>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
12
13
14

namespace Dune
{
15
  namespace Curved
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
16
17
18
19
20
21
22
  {

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

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

    /** \class Entity
     *  \brief DUNE-conform implementation of the entity
Praetorius, Simon's avatar
Praetorius, Simon committed
33
     *  \ingroup CurvedGeo
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
     *
     *  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;


58
    template< int codim, class G >
Praetorius, Simon's avatar
Praetorius, Simon committed
59
    class EntityBase
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
60
    {
61
      using Traits = typename std::remove_const_t<G>::Traits;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    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
82
      using ctype = typename Traits::ctype;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
83
84
85
86
87
88
89
90

      /** \} */

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
93
      /** \} */
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
94
95

    public:
96
97
98
99
100
      using Grid = typename Traits::Grid;
      using GridFunction = typename Traits::GridFunction;

      //! type of the host grid
      using HostGrid = typename Traits::HostGrid;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
101

102
103
104
105
106
107
108
      //! type of corresponding host entity
      using HostEntity = typename HostGrid::template Codim<codim>::Entity;

      //! type of host elements, i.e., of host entities of codimension 0
      using HostElement = typename HostGrid::template Codim<0>::Entity;

    public:
Praetorius, Simon's avatar
Praetorius, Simon committed
109
      EntityBase () = default;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
110

Praetorius, Simon's avatar
Praetorius, Simon committed
111
112
113
      // Construct the entity from an entity seed
      EntityBase (const Grid& grid, const EntitySeed& seed)
        : hostEntity_(grid.hostGrid().entity(seed.impl().hostEntitySeed()))
114
        , gridFunction_(&grid.gridFunction())
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
115
116
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
117
      // construct the entity from a subentity of a host-entity
118
      EntityBase (const GridFunction& gridFunction, const HostElement& hostElement, int i)
Praetorius, Simon's avatar
Praetorius, Simon committed
119
        : hostEntity_(hostElement.template subEntity<codim>(i))
120
        , gridFunction_(&gridFunction)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
121
122
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
123
      // construct the entity from a host-entity
124
      EntityBase (const GridFunction& gridFunction, const HostEntity& hostEntity)
Praetorius, Simon's avatar
Praetorius, Simon committed
125
        : hostEntity_(hostEntity)
126
        , gridFunction_(&gridFunction)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
127
128
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
129
      // construct the entity from a host-entity
130
      EntityBase (const GridFunction& gridFunction, HostEntity&& hostEntity)
Praetorius, Simon's avatar
Praetorius, Simon committed
131
        : hostEntity_(std::move(hostEntity))
132
        , gridFunction_(&gridFunction)
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
133
134
      {}

Praetorius, Simon's avatar
Praetorius, Simon committed
135
136
      //! compare two entities
      bool equals (const EntityBase& other) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
137
138
139
140
141
142
143
144
      {
        return hostEntity_ == other.hostEntity_;
      }

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

Praetorius, Simon's avatar
Praetorius, Simon committed
145
146
      //! obtain the name of the corresponding reference element
      /**
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
147
148
149
150
151
152
153
       *  This type can be used to access the DUNE reference element.
       */
      GeometryType type () const
      {
        return hostEntity().type();
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
154
      //! obtain the level of this entity
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
155
156
157
158
159
      int level () const
      {
        return hostEntity().level();
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
160
      //! obtain the partition type of this entity
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
161
162
163
164
165
      PartitionType partitionType () const
      {
        return hostEntity().partitionType();
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
166
167
      //! 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
168
      {
169
        return hostEntity().subEntities(cc);
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
170
171
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
172
173
      //! return EntitySeed of host grid entity
      EntitySeed seed () const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
174
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
175
        return typename EntitySeed::Implementation(hostEntity().seed());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
176
177
178
      }


Praetorius, Simon's avatar
Praetorius, Simon committed
179
      /** \name Methods Supporting the Grid Implementation
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
180
181
       *  \{ */

182
      const GridFunction& gridFunction () const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
183
      {
184
        return *gridFunction_;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
185
186
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
187
188
      //! return the wrapped host-entity
      const HostEntity& hostEntity () const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
189
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
190
        return hostEntity_;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
191
192
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
193
      /** \} */
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
194
195

    private:
196
197
198
199
200
201
202
203
204
205
206
207
208
      HostEntity hostEntity_;
      const GridFunction* gridFunction_ = nullptr;
    };



    template< int codim, class G >
    class EntityBaseGeometry
        : public EntityBase<codim, G>
    {
      using Super = EntityBase<codim, G>;
      using Traits = typename std::remove_const_t<G>::Traits;

209
210
211
212
      //! type of host elements, i.e., of host entities of codimension 0
      using HostElement = typename Traits::HostGrid::template Codim<0>::Entity;
      using GridFunction = typename Super::GridFunction;

213
214
215
    public:
      //! type of corresponding geometry
      using Geometry = typename Traits::template Codim<codim>::Geometry;
216
      using ctype = typename Geometry::ctype;
217
218
219
220

    public:
      using Super::Super;

221
222
223
224
225
226
227
      // construct the entity from a subentity of a host-entity
      EntityBaseGeometry (const GridFunction& gridFunction, const HostElement& hostElement, int i)
        : Super(gridFunction, hostElement, i)
        , hostElement_(hostElement)
        , subEntity_(i)
      {}

228
229
230
231
      //! obtain the geometry of this entity
      Geometry geometry () const
      {
        if (!geo_) {
232
          if (hostElement_) {
Praetorius, Simon's avatar
Praetorius, Simon committed
233
            auto localFct = localFunction(Super::gridFunction());
234
235
236
237
            localFct.bind(*hostElement_);

            auto refElem = referenceElement<ctype, Super::dimension>(hostElement_->type());
            auto localGeometry = refElem.template geometry<codim>(subEntity_);
Praetorius, Simon's avatar
Praetorius, Simon committed
238
            geo_ = std::make_shared<GeometryImpl>(Super::type(), std::move(localFct), localGeometry);
239
240
241
242
          }
          else {
            DUNE_THROW(Dune::NotImplemented, "Geometry of entities of codim!=0 not implemented");
          }
243
244
245
246
247
248
        }

        return Geometry(*geo_);
      }

    private:
Praetorius, Simon's avatar
Praetorius, Simon committed
249
      std::optional<HostElement> hostElement_;
250
251
      int subEntity_ = -1;

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
      using GeometryImpl = typename Traits::template Codim<codim>::GeometryImpl;
      mutable std::shared_ptr<GeometryImpl> geo_;
    };


    template< class G >
    class EntityBaseGeometry<0, G>
        : public EntityBase<0, G>
    {
      using Super = EntityBase<0, G>;
      using Traits = typename std::remove_const_t<G>::Traits;

    public:
      //! type of corresponding geometry
      using Geometry = typename Traits::template Codim<0>::Geometry;

268
269
270
      //! type of corresponding local geometry
      using LocalGeometry = typename Traits::template Codim<0>::LocalGeometry;

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    public:
      using Super::Super;

      //! obtain the geometry of this entity
      /**
       *  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.
       *
       *  The CurvedSurfaceGrid provides geometries by parametrization with local basis
       *  functions, using the CurvedGeometry.
       *
       *  \returns a new curvedgeometry object
       */
      Geometry geometry () const
      {
        if (!geo_) {
          auto localFct = localFunction(Super::gridFunction());
          localFct.bind(Super::hostEntity());
290
          auto fakeDefaultGeometry = Dune::DefaultLocalGeometry<typename Super::ctype, Super::mydimension, Super::mydimension>{};
Praetorius, Simon's avatar
Praetorius, Simon committed
291
          geo_ = std::make_shared<GeometryImpl>(Super::type(), std::move(localFct), fakeDefaultGeometry);
292
293
294
295
296
297
298
299
        }

        return Geometry(*geo_);
      }

    private:
      using GeometryImpl = typename Traits::template Codim<0>::GeometryImpl;
      mutable std::shared_ptr<GeometryImpl> geo_;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
300
301
302
303
304
305
    };


    // Entity
    // ------

306
    template< int codim, int dim, class G >
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
307
    class Entity
308
        : public EntityBaseGeometry<codim, G>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
309
    {
310
      using Super = EntityBaseGeometry<codim, G>;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
311
312

    public:
Praetorius, Simon's avatar
Praetorius, Simon committed
313
314
      // import constructors from base class
      using Super::Super;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
315
316
317
318
319
320
    };


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

321
322
323
    template< int dim, class G >
    class Entity<0, dim, G>
        : public EntityBaseGeometry<0, G>
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
324
    {
325
      using Super = EntityBaseGeometry<0, G>;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
326

327
328
329
      using Traits = typename std::remove_const_t<G>::Traits;
      using Grid = typename Traits::Grid;
      using GridFunction = typename Grid::GridFunction;
Praetorius, Simon's avatar
Praetorius, Simon committed
330
      using HostGrid = typename Traits::HostGrid;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
331
332
333
334
335
336
337

    public:

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
340
      //! facade type for entities
341
      using EntityFacade = Dune::Entity<0, dim, G, Dune::Curved::Entity>;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
342
343

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

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

Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
349
      //! type of level intersection iterator
Praetorius, Simon's avatar
Praetorius, Simon committed
350
      using LevelIntersectionIterator = typename Traits::LevelIntersectionIterator;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
351
352
353

      /** \} */

Praetorius, Simon's avatar
Praetorius, Simon committed
354
355
      // import constructors from base class
      using Super::Super;
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
356
357

      template< int codim >
Praetorius, Simon's avatar
Praetorius, Simon committed
358
      typename Grid::template Codim<codim>::Entity subEntity (int i) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
359
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
360
        using EntityImpl = typename Traits::template Codim<codim>::EntityImpl;
361
        return EntityImpl(Super::gridFunction(), Super::hostEntity(), i);
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
362
363
364
365
      }

      LevelIntersectionIterator ilevelbegin () const
      {
366
        using IteratorImpl = Curved::IntersectionIterator<Grid, typename HostGrid::LevelIntersectionIterator>;
367
        return IteratorImpl(*this, Super::hostEntity().ilevelbegin());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
368
369
370
371
      }

      LevelIntersectionIterator ilevelend () const
      {
372
        using IteratorImpl = Curved::IntersectionIterator<Grid, typename HostGrid::LevelIntersectionIterator>;
373
        return IteratorImpl(*this, Super::hostEntity().ilevelend());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
374
375
376
377
      }

      LeafIntersectionIterator ileafbegin () const
      {
378
        using IteratorImpl = Curved::IntersectionIterator<Grid, typename HostGrid::LeafIntersectionIterator>;
379
        return IteratorImpl(*this, Super::hostEntity().ileafbegin());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
380
381
382
383
      }

      LeafIntersectionIterator ileafend () const
      {
384
        using IteratorImpl = Curved::IntersectionIterator<Grid, typename HostGrid::LeafIntersectionIterator>;
385
        return IteratorImpl(*this, Super::hostEntity().ileafend());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
386
387
388
389
      }

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

      bool isLeaf () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
395
        return Super::hostEntity().isLeaf();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
396
397
398
399
      }

      EntityFacade father () const
      {
400
        return Entity(Super::gridFunction(), Super::hostEntity().father());
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
401
402
403
404
      }

      bool hasFather () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
405
        return Super::hostEntity().hasFather();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
406
407
408
409
      }

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

Praetorius, Simon's avatar
Praetorius, Simon committed
413
      HierarchicIterator hbegin (int maxLevel) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
414
      {
415
        using IteratorImpl = Curved::HierarchicIterator<G>;
416
        return IteratorImpl(Super::gridFunction(), Super::hostEntity().hbegin(maxLevel));
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
417
418
      }

Praetorius, Simon's avatar
Praetorius, Simon committed
419
      HierarchicIterator hend (int maxLevel) const
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
420
      {
421
        using IteratorImpl = Curved::HierarchicIterator<G>;
422
        return IteratorImpl(Super::gridFunction(), Super::hostEntity().hend(maxLevel));
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
423
424
425
426
      }

      bool isRegular () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
427
        return Super::hostEntity().isRegular();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
428
429
430
431
      }

      bool isNew () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
432
        return Super::hostEntity().isNew();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
433
434
435
436
      }

      bool mightVanish () const
      {
Praetorius, Simon's avatar
Praetorius, Simon committed
437
        return Super::hostEntity().mightVanish();
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
438
439
440
      }
    };

441
  } // namespace Curved
Stenger, Florian's avatar
v0.1.0  
Stenger, Florian committed
442
443
} // namespace Dune

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