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 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
// -*- 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
#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> >;
          using LevelIterator = MultiMeshIterator<cd, pitype, const Grid>;

          /// 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> >;
          using LeafIterator = MultiMeshIterator<cd, pitype, const Grid>;
        };

        /// 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:
        friend class HostGrid::GridFamily::Traits::template Codim<cd>::Entity;
      };

      /// 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> >;
    };
  };

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

    template <class GridImp_>
    friend class MultiMeshLevelGridView;

    template <class GridImp_>
    friend class MultiMeshLeafGridView;

    friend class GridFactory<MultiMesh<HostGrid> >;

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

  public:

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

105
    /// The Traits
Praetorius, Simon's avatar
Praetorius, Simon committed
106 107 108 109 110 111 112 113
    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
    /**
114
     * \param  n  The number of host grids to handle by the MultiMesh
Praetorius, Simon's avatar
Praetorius, Simon committed
115 116
     */
    template <class... Args>
117
    explicit MultiMesh (std::size_t n, Args&&... args)
Praetorius, Simon's avatar
Praetorius, Simon committed
118 119 120 121 122 123
    {
      grids_.reserve(n);
      for (std::size_t i = 0; i < n; ++i)
        grids_.emplace_back(std::make_unique<HostGrid>(std::forward<Args>(args)...));
    }

124
    // Initialize an empty MultiMesh
125 126
    MultiMesh () = default;

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

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

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


148 149
    /// \brief Return maximum level defined in all grid.
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
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 184 185 186 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 227
     * 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
    {
      return MultiMeshIterator<codim,PiType, const MultiMesh<HostGrid> >(this, level);
    }

    /// 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
    {
      return MultiMeshIterator<codim,PiType, const MultiMesh<HostGrid> >(this, level, true);
    }

    /// 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
    {
      return MultiMeshIterator<codim,PiType, const MultiMesh<HostGrid> >(this);
    }

    /// 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
    {
      return MultiMeshIterator<codim,PiType, const MultiMesh<HostGrid> >(this, true);
    }


    /// 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
    {
      return grids_[idx].globalIdSet();
    }

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


    /// Access to the LocalIdSet
    const typename HostGrid::LocalIdSet& localIdSet (std::size_t idx) const
    {
      return grids_[idx].localIdSet();
    }

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

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


    /// Access to the LeafIndexSet
    const typename HostGrid::LeafIndexSet& leafIndexSet(std::size_t idx) const
    {
      return grids_[idx].leafIndexSet();
    }

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

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

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

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

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

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

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

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

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

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


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

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

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

435
  } // end namespace Capabilities
Praetorius, Simon's avatar
Praetorius, Simon committed
436 437 438
} // end namespace Dune

#endif // DUNE_MULTIMESH_GRID_HH