multimesh.hh 13.4 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
#include "mmentityseed.hh"
#include "mmgridview.hh"
#include "mmiterator.hh"

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

27
  template <class HostGrid>
Praetorius, Simon's avatar
Praetorius, Simon committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  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 = MultiMeshLevelIterator<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 = MultiMeshLeafIterator<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
   */
  template <class HostGrid>
  class MultiMesh
      : public GridDefaultImplementation<HostGrid::dimension, HostGrid::dimensionworld,
84
          typename HostGrid::ctype, MultiMeshFamily<HostGrid> >
Praetorius, Simon's avatar
Praetorius, Simon committed
85
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
86
    template <class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
87 88
    friend class MultiMeshLevelGridView;

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
95 96 97
    friend class GridFactory<MultiMesh<HostGrid> >;

    using Super = GridDefaultImplementation<HostGrid::dimension, HostGrid::dimensionworld,
98
      typename HostGrid::ctype, MultiMeshFamily<HostGrid> >;
Praetorius, Simon's avatar
Praetorius, Simon committed
99 100

  public:
Praetorius, Simon's avatar
Praetorius, Simon committed
101
    using HostGridType = HostGrid;
Praetorius, Simon's avatar
Praetorius, Simon committed
102

103
    /// Type of the used GridFamily for this grid
104
    using GridFamily = MultiMeshFamily<HostGrid>;
Praetorius, Simon's avatar
Praetorius, Simon committed
105

106
    /// The Traits
107
    using Traits = typename GridFamily::Traits;
Praetorius, Simon's avatar
Praetorius, Simon committed
108 109 110 111 112 113 114

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


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

126
    // Initialize an empty MultiMesh
127 128
    MultiMesh () = default;

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

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

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


150 151
    /// \brief Return maximum level defined in all grid.
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
152 153 154 155 156 157 158 159 160 161 162 163 164
     * 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
    {
165
      return MultiMeshLevelIterator<codim, PiType, HostGrid>(tag::begin_iterator{}, this, level);
Praetorius, Simon's avatar
Praetorius, Simon committed
166 167 168 169 170 171
    }

    /// 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
    {
172
      return MultiMeshLevelIterator<codim, PiType, HostGrid>(tag::end_iterator{}, this);
Praetorius, Simon's avatar
Praetorius, Simon committed
173 174 175 176 177 178
    }

    /// 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
    {
179
      return MultiMeshLeafIterator<codim, PiType, HostGrid>(tag::begin_iterator{}, this);
Praetorius, Simon's avatar
Praetorius, Simon committed
180 181 182 183 184 185
    }

    /// 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
    {
186
      return MultiMeshLeafIterator<codim, PiType, HostGrid>(tag::end_iterator{}, this);
Praetorius, Simon's avatar
Praetorius, Simon committed
187 188 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
    }


    /// 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
227
      return grids_[idx]->globalIdSet();
Praetorius, Simon's avatar
Praetorius, Simon committed
228 229
    }

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


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

244
    /// Return the localIdSet for grid 0
Praetorius, Simon's avatar
Praetorius, Simon committed
245 246
    const typename HostGrid::LocalIdSet& localIdSet () const
    {
247
      assert(false && "Should be called on the i'th grid: localIdSet(i)");
Praetorius, Simon's avatar
Praetorius, Simon committed
248 249 250 251 252 253 254 255 256 257
      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
258
      return grids_[idx]->levelIndexSet(level);
Praetorius, Simon's avatar
Praetorius, Simon committed
259 260
    }

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


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

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

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

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

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

298 299
    /// Return \ref MultiMeshLeafGridView, i.e. a view on the combined
    /// leaf-level of all grids
Praetorius, Simon's avatar
Praetorius, Simon committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313
    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 */
314
    /** @{ */
Praetorius, Simon's avatar
Praetorius, Simon committed
315

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

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

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

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

345
    /** @} */
Praetorius, Simon's avatar
Praetorius, Simon committed
346 347 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


    /// 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:

393
    /// The grids to handle by this MultiMesh
Praetorius, Simon's avatar
Praetorius, Simon committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    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;
    };

410 411 412
    /** \brief has entity-iterator for some codimensions as host grid
     * \ingroup MultiMesh
     */
Praetorius, Simon's avatar
Praetorius, Simon committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    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;
    };

437
  } // end namespace Capabilities
Praetorius, Simon's avatar
Praetorius, Simon committed
438 439 440
} // end namespace Dune

#endif // DUNE_MULTIMESH_GRID_HH