multimesh.hh 13.5 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_MULTIMESH_GRID_HH
#define DUNE_MULTIMESH_GRID_HH

#include <string>
#include <map>

#include <dune/common/deprecated.hh>
#include <dune/common/parallel/collectivecommunication.hh>
#include <dune/grid/common/capabilities.hh>
#include <dune/grid/common/grid.hh>
#include <dune/grid/common/gridfactory.hh>

// The components of the MultiMesh interface
Praetorius, Simon's avatar
Praetorius, Simon committed
16
#include "mmentity.hh"
Praetorius, Simon's avatar
Praetorius, Simon committed
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
#include "mmentityseed.hh"
#include "mmgridview.hh"
#include "mmiterator.hh"

namespace Dune
{
  // Forward declaration
  template <class HostGrid>
  class MultiMesh;

  template <int dim, class HostGrid>
  struct MultiMeshFamily
  {
    struct Traits : HostGrid::GridFamily::Traits
    {
      /// The type that implements the grid.
      using Grid = Dune::MultiMesh<HostGrid>;

      /// Traits associated with a specific codim.
      template <int cd>
      struct Codim
          : public HostGrid::GridFamily::Traits::template Codim<cd>
      {
        /// The type of the entity seed of this codim.
        using EntitySeed = Dune::EntitySeed<const Grid, MultiMeshEntitySeed<cd, const Grid> >;

        /// Traits associated with a specific grid partition type.
        template <PartitionIteratorType pitype>
        struct Partition
        {
          /// The type of the iterator over the level entities of this codim on this partition.
          // using LevelIterator = Dune::EntityIterator<cd, const Grid, MultiMeshIterator<cd, pitype, const Grid> >;
49
          using LevelIterator = MultiMeshIterator<cd, pitype, HostGrid>;
Praetorius, Simon's avatar
Praetorius, Simon committed
50
51
52

          /// The type of the iterator over the leaf entities of this codim on this partition.
          // using LeafIterator = Dune::EntityIterator<cd, const Grid, MultiMeshIterator<cd, pitype, const Grid> >;
53
          using LeafIterator = MultiMeshIterator<cd, pitype, HostGrid>;
Praetorius, Simon's avatar
Praetorius, Simon committed
54
55
56
57
58
59
60
61
62
        };

        /// The type of the iterator over all leaf entities of this codim.
        using LeafIterator = typename Partition<All_Partition>::LeafIterator;

        /// The type of the entity pointer for entities of this codim.
        using LevelIterator = typename Partition<All_Partition>::LevelIterator;

      private:
63
        // friend class HostGrid::GridFamily::Traits::template Codim<cd>::Entity;
Praetorius, Simon's avatar
Praetorius, Simon committed
64
65
66
67
68
69
70
71
72
73
      };

      /// The type of view for leaf grid
      using LeafGridView = Dune::GridView<MultiMeshLeafGridViewTraits<const Grid> >;

      /// The type of view for level grid
      using LevelGridView = Dune::GridView<MultiMeshLevelGridViewTraits<const Grid> >;
    };
  };

74
  /// \brief Provides a meta grid that encapsulates n versions of its host grid
Praetorius, Simon's avatar
Praetorius, Simon committed
75
76
77
78
  /**
   * \ingroup GridImplementations
   * \ingroup MultiMesh
   *
79
   * \tparam  HostGrid  The host grid type wrapped by the MultiMesh
Praetorius, Simon's avatar
Praetorius, Simon committed
80
81
82
83
84
85
   */
  template <class HostGrid>
  class MultiMesh
      : public GridDefaultImplementation<HostGrid::dimension, HostGrid::dimensionworld,
          typename HostGrid::ctype, MultiMeshFamily<HostGrid::dimension, HostGrid> >
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
86
    template <int codim, PartitionIteratorType pitype, class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
87
88
    friend class MultiMeshIterator;

Praetorius, Simon's avatar
Praetorius, Simon committed
89
    template <class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
90
91
    friend class MultiMeshLevelGridView;

Praetorius, Simon's avatar
Praetorius, Simon committed
92
    template <class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
93
94
    friend class MultiMeshLeafGridView;

Praetorius, Simon's avatar
Praetorius, Simon committed
95
96
97
    template <class GridImp>
    friend class MultiEntity;

Praetorius, Simon's avatar
Praetorius, Simon committed
98
99
100
101
102
103
    friend class GridFactory<MultiMesh<HostGrid> >;

    using Super = GridDefaultImplementation<HostGrid::dimension, HostGrid::dimensionworld,
      typename HostGrid::ctype, MultiMeshFamily<HostGrid::dimension, HostGrid> >;

  public:
Praetorius, Simon's avatar
Praetorius, Simon committed
104
    using HostGridType = HostGrid;
Praetorius, Simon's avatar
Praetorius, Simon committed
105

106
    /// Type of the used GridFamily for this grid
Praetorius, Simon's avatar
Praetorius, Simon committed
107
108
    using GridFamily = MultiMeshFamily<HostGrid::dimension,HostGrid>;

109
    /// The Traits
Praetorius, Simon's avatar
Praetorius, Simon committed
110
111
112
113
114
115
116
117
    using Traits = typename MultiMeshFamily<HostGrid::dimension,HostGrid>::Traits;

    /// The type used to store coordinates, inherited from the HostGrid
    using ctype = typename HostGrid::ctype;


    /// \brief Constructor, stores n copies of the hostgrid
    /**
118
     * \param  n  The number of host grids to handle by the MultiMesh
Praetorius, Simon's avatar
Praetorius, Simon committed
119
120
     */
    template <class... Args>
121
    explicit MultiMesh (std::size_t n, Args&&... args)
Praetorius, Simon's avatar
Praetorius, Simon committed
122
123
124
125
126
127
    {
      grids_.reserve(n);
      for (std::size_t i = 0; i < n; ++i)
        grids_.emplace_back(std::make_unique<HostGrid>(std::forward<Args>(args)...));
    }

128
    // Initialize an empty MultiMesh
129
130
    MultiMesh () = default;

131
    /// Return the number of grids handled by this MultiMesh
132
    std::size_t size() const
133
134
135
    {
      return grids_.size();
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
136
137

    /// Returns the i'th grid managed by this MultiMesh
138
    HostGridType& operator[] (std::size_t i)
Praetorius, Simon's avatar
Praetorius, Simon committed
139
    {
140
      assert(i < grids_.size());
Praetorius, Simon's avatar
Praetorius, Simon committed
141
142
143
144
      return *grids_[i];
    }

    /// Returns the i'th grid managed by this MultiMesh
145
    HostGridType const& operator[] (std::size_t i) const
Praetorius, Simon's avatar
Praetorius, Simon committed
146
    {
147
      assert(i < grids_.size());
Praetorius, Simon's avatar
Praetorius, Simon committed
148
149
150
151
      return *grids_[i];
    }


152
153
    /// \brief Return maximum level defined in all grid.
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
154
155
156
157
158
159
160
161
162
163
164
165
166
     * Levels are numbered 0 ... maxlevel with 0 the coarsest level.
     */
    int maxLevel () const
    {
      return std::accumulate(grids_.begin(), grids_.end(), 0,
        [](int level, auto const& grid) { return std::max(level, grid->maxLevel()); });
    }


    /// Iterator to first entity of given codim on level
    template <int codim, PartitionIteratorType PiType = All_Partition>
    typename Traits::template Codim<codim>::template Partition<PiType>::LevelIterator lbegin (int level) const
    {
167
      return MultiMeshIterator<codim,PiType, HostGrid>(this, level);
Praetorius, Simon's avatar
Praetorius, Simon committed
168
169
170
171
172
173
    }

    /// one past the end on this level
    template <int codim, PartitionIteratorType PiType = All_Partition>
    typename Traits::template Codim<codim>::template Partition<PiType>::LevelIterator lend (int level) const
    {
174
      return MultiMeshIterator<codim,PiType, HostGrid>(this, level, true);
Praetorius, Simon's avatar
Praetorius, Simon committed
175
176
177
178
179
180
    }

    /// Iterator to first leaf entity of given codim
    template <int codim, PartitionIteratorType PiType = All_Partition>
    typename Traits::template Codim<codim>::template Partition<PiType>::LeafIterator leafbegin () const
    {
181
      return MultiMeshIterator<codim,PiType, HostGrid>(this);
Praetorius, Simon's avatar
Praetorius, Simon committed
182
183
184
185
186
187
    }

    /// one past the end of the sequence of leaf entities
    template <int codim, PartitionIteratorType PiType = All_Partition>
    typename Traits::template Codim<codim>::template Partition<PiType>::LeafIterator leafend () const
    {
188
      return MultiMeshIterator<codim,PiType, HostGrid>(this, true);
Praetorius, Simon's avatar
Praetorius, Simon committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    }


    /// Number of grid entities per level and codim of the first grid
    int size (int level, int codim) const
    {
      assert(false && "Should be called on the i'th grid: grid(i).size(level,codim)");
      return grids_[0]->size(level, codim);
    }

    /// Returns the number of boundary segments within the macro grid
    std::size_t numBoundarySegments () const
    {
      assert(false && "Should be called on the i'th grid: grid(i).numBoundarySegments()");
      return grids_[0]->numBoundarySegments();
    }

    /// Number of leaf entities per codim in this process
    int size (int codim) const
    {
      assert(false && "Should be called on the i'th grid: grid(i).size(codim)");
      return leafIndexSet().size(codim);
    }

    /// Number of entities per level, codim and geometry type in this process
    int size (std::size_t idx, int level, GeometryType type) const
    {
      return levelIndexSet(level).size(type);
    }

    /// Number of leaf entities per codim and geometry type in this process
    int size (std::size_t idx, GeometryType type) const
    {
      return leafIndexSet().size(type);
    }


    /// Access to the GlobalIdSet
    const typename HostGrid::GlobalIdSet& globalIdSet (std::size_t idx) const
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
229
      return grids_[idx]->globalIdSet();
Praetorius, Simon's avatar
Praetorius, Simon committed
230
231
    }

232
    /// Return the globalIdSet for grid 0
Praetorius, Simon's avatar
Praetorius, Simon committed
233
234
    const typename HostGrid::GlobalIdSet& globalIdSet () const
    {
235
      assert(false && "Should be called on the i'th grid: globalIdSet(i)");
Praetorius, Simon's avatar
Praetorius, Simon committed
236
237
238
239
240
241
242
      return globalIdSet(0);
    }


    /// Access to the LocalIdSet
    const typename HostGrid::LocalIdSet& localIdSet (std::size_t idx) const
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
243
      return grids_[idx]->localIdSet();
Praetorius, Simon's avatar
Praetorius, Simon committed
244
245
    }

246
    /// Return the localIdSet for grid 0
Praetorius, Simon's avatar
Praetorius, Simon committed
247
248
    const typename HostGrid::LocalIdSet& localIdSet () const
    {
249
      assert(false && "Should be called on the i'th grid: localIdSet(i)");
Praetorius, Simon's avatar
Praetorius, Simon committed
250
251
252
253
254
255
256
257
258
259
      return localIdSet(0);
    }


    /// Access to the LevelIndexSets
    const typename HostGrid::LevelIndexSet& levelIndexSet (std::size_t idx, int level) const
    {
      if (level < 0 || level > grids_[idx].maxLevel()) {
        DUNE_THROW(GridError, "levelIndexSet of nonexisting level " << level << " requested!");
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
260
      return grids_[idx]->levelIndexSet(level);
Praetorius, Simon's avatar
Praetorius, Simon committed
261
262
    }

263
    /// Return the levelIndexSet for grid 0
Praetorius, Simon's avatar
Praetorius, Simon committed
264
265
    const typename HostGrid::LevelIndexSet& levelIndexSet (int level) const
    {
266
      assert(false && "Should be called on the i'th grid: levelIndexSet(i,level)");
Praetorius, Simon's avatar
Praetorius, Simon committed
267
268
269
270
271
272
273
      return levelIndexSet(0, level);
    }


    /// Access to the LeafIndexSet
    const typename HostGrid::LeafIndexSet& leafIndexSet(std::size_t idx) const
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
274
      return grids_[idx]->leafIndexSet();
Praetorius, Simon's avatar
Praetorius, Simon committed
275
276
277
278
279
    }

    /// Return the leafIndexSet for grid 0
    const typename HostGrid::LeafIndexSet& leafIndexSet() const
    {
280
      assert(false && "Should be called on the i'th grid: leafIndexSet(i)");
Praetorius, Simon's avatar
Praetorius, Simon committed
281
282
283
      return leafIndexSet(0);
    }

284
    /// View for a grid level for All_Partition of the idx'th grid
Praetorius, Simon's avatar
Praetorius, Simon committed
285
286
    typename HostGrid::LevelGridView levelGridView(std::size_t idx, int level) const
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
287
      return grids_[idx]->levelGridView(level);
Praetorius, Simon's avatar
Praetorius, Simon committed
288
289
    }

290
291
    /// Return \ref MultiMeshLevelGridView, i.e. a view on the combined
    /// level of all grids
Praetorius, Simon's avatar
Praetorius, Simon committed
292
293
    using Super::levelGridView;

294
    /// View for the leaf grid for All_Partition of the idx'th grid
Praetorius, Simon's avatar
Praetorius, Simon committed
295
296
    typename HostGrid::LeafGridView leafGridView(std::size_t idx) const
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
297
      return grids_[idx]->leafGridView();
Praetorius, Simon's avatar
Praetorius, Simon committed
298
299
    }

300
301
    /// Return \ref MultiMeshLeafGridView, i.e. a view on the combined
    /// leaf-level of all grids
Praetorius, Simon's avatar
Praetorius, Simon committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
    using Super::leafGridView;


    /// Create Entity from EntitySeed
    template <class EntitySeed>
    typename Traits::template Codim<EntitySeed::codimension>::Entity
    entity(const EntitySeed& seed) const
    {
      std::size_t gridIdx = this->getRealImplementation(seed).gridIndex();
      return grids_[gridIdx]->entity(this->getRealImplementation(seed).hostEntitySeed());
    }


    /** @name Grid Refinement Methods */
316
    /** @{ */
Praetorius, Simon's avatar
Praetorius, Simon committed
317

318
    /// Global refinement of all handled grids
Praetorius, Simon's avatar
Praetorius, Simon committed
319
320
321
322
323
324
    void globalRefine (int refCount)
    {
      for (auto& grid : grids_)
        grid->globalRefine(refCount);
    }

325
326
    /// Returns true, if at least one entity is marked for adaption in any of the
    /// handled grids
Praetorius, Simon's avatar
Praetorius, Simon committed
327
328
329
330
331
332
    bool preAdapt()
    {
      return std::accumulate(grids_.begin(), grids_.end(), false,
        [](bool b, auto& grid) { return grid->preAdapt() || b; });
    }

333
    /// Triggers the grid refinement process on all handled grids
Praetorius, Simon's avatar
Praetorius, Simon committed
334
335
336
337
338
339
    bool adapt()
    {
      return std::accumulate(grids_.begin(), grids_.end(), false,
        [](bool b, auto& grid) { return grid->adapt() || b; });
    }

340
    /// Clean up refinement markers on all handled grids
Praetorius, Simon's avatar
Praetorius, Simon committed
341
342
343
344
345
346
    void postAdapt()
    {
      for (auto& grid : grids_)
        grid->postAdapt();
    }

347
    /** @} */
Praetorius, Simon's avatar
Praetorius, Simon committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394


    /// Size of the overlap on the leaf level
    unsigned int overlapSize (int codim) const
    {
      return 0u; // TODO: implement
    }

    /// Size of the ghost cell layer on the leaf level
    unsigned int ghostSize (int codim) const
    {
      return 0u; // TODO: implement
    }

    /// Size of the overlap on a given level
    unsigned int overlapSize (int level, int codim) const
    {
      return 0u; // TODO: implement
    }

    /// Size of the ghost cell layer on a given level
    unsigned int ghostSize (int level, int codim) const
    {
      return 0u; // TODO: implement
    }

    /// dummy collective communication
    const auto& comm () const
    {
      assert(false && "Should be called on the i'th grid: grid(i).comm()");
      return comm(0);
    }


  public: // implementation details

    /// Returns the hostgrid entity encapsulated in given MultiMesh entity
    template <int codim>
    const typename HostGrid::Traits::template Codim<codim>::Entity&
    getHostEntity (const typename Traits::template Codim<codim>::Entity& e) const
    {
      return this->getRealImplementation(e).hostEntity_;
    }


  protected:

395
    /// The grids to handle by this MultiMesh
Praetorius, Simon's avatar
Praetorius, Simon committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    std::vector<std::unique_ptr<HostGrid>> grids_;

  }; // end Class MultiMesh


  namespace Capabilities
  {
    /** \brief has entities for some codimensions as host grid
     * \ingroup MultiMesh
     */
    template <class HostGrid, int codim>
    struct hasEntity<MultiMesh<HostGrid>, codim>
    {
      static const bool v = hasEntity<HostGrid,codim>::v;
    };

412
413
414
    /** \brief has entity-iterator for some codimensions as host grid
     * \ingroup MultiMesh
     */
Praetorius, Simon's avatar
Praetorius, Simon committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
    template <class HostGrid, int codim>
    struct hasEntityIterator<MultiMesh<HostGrid>, codim>
    {
      static const bool v = hasEntityIterator<HostGrid, codim>::v;
    };

    /** \brief has conforming level grids when host grid has
     * \ingroup MultiMesh
     */
    template <class HostGrid>
    struct isLevelwiseConforming<MultiMesh<HostGrid> >
    {
      static const bool v = isLevelwiseConforming<HostGrid>::v;
    };

    /** \brief has conforming leaf grids when host grid has
     * \ingroup MultiMesh
     */
    template <class HostGrid>
    struct isLeafwiseConforming<MultiMesh<HostGrid> >
    {
      static const bool v = isLeafwiseConforming<HostGrid>::v;
    };

439
  } // end namespace Capabilities
Praetorius, Simon's avatar
Praetorius, Simon committed
440
441
442
} // end namespace Dune

#endif // DUNE_MULTIMESH_GRID_HH