multimesh.hh 12.8 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
// -*- 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

/** \file
 * \brief The MultiMesh class
 */

#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;

  // External forward declarations
  template <class Grid>
  struct HostGridAccess;

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

  /// \brief Provides a meta grid that encapsulates n versions of its host
  /**
   * \ingroup GridImplementations
   * \ingroup MultiMesh
   *
   * \tparam HostGrid The host grid type wrapped by the MultiMesh
   */
  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 struct HostGridAccess<MultiMesh<HostGrid> >;
    friend class GridFactory<MultiMesh<HostGrid> >;

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

  public:

    /// type of the used GridFamily for this grid
    using GridFamily = MultiMeshFamily<HostGrid::dimension,HostGrid>;

    /// the Traits
    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
    /**
     * \param n The number of host grids to handle by the MultiMesh
     */
    template <class... Args>
    explicit MultiMesh(std::size_t n, Args&&... args)
    {
      grids_.reserve(n);
      for (std::size_t i = 0; i < n; ++i)
        grids_.emplace_back(std::make_unique<HostGrid>(std::forward<Args>(args)...));
    }


    /// Returns the i'th grid managed by this MultiMesh
    HostGridType& grid (std::size_t i)
    {
      return *grids_[i];
    }

    /// Returns the i'th grid managed by this MultiMesh
    HostGridType const& grid (std::size_t i) const
    {
      return *grids_[i];
    }


    /** \brief Return maximum level defined in all grid.
     *
     * 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();
    }

    const typename HostGrid::GlobalIdSet& globalIdSet () const
    {
      assert(false && "Should be called on the i'th grid: grid(i).globalIdSet()");
      return globalIdSet(0);
    }


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

    const typename HostGrid::LocalIdSet& localIdSet () const
    {
      assert(false && "Should be called on the i'th grid: grid(i).localIdSet()");
      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);
    }

    const typename HostGrid::LevelIndexSet& levelIndexSet (int level) const
    {
      assert(false && "Should be called on the i'th grid: grid(i).levelIndexSet()");
      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
    {
      assert(false && "Should be called on the i'th grid: grid(i).leafIndexSet()");
      return leafIndexSet(0);
    }

    /// View for a grid level for All_Partition
    typename HostGrid::LevelGridView levelGridView(std::size_t idx, int level) const
    {
      return grids_[idx].levelGridView(level);
    }

    using Super::levelGridView;

    /// View for the leaf grid for All_Partition
    typename HostGrid::LeafGridView leafGridView(std::size_t idx) const
    {
      return grids_[idx].leafGridView();
    }

    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 */
    /*@{*/

    /// Global refinement
    void globalRefine (int refCount)
    {
      for (auto& grid : grids_)
        grid->globalRefine(refCount);
    }

    /// Returns true, if at least one entity is marked for adaption
    bool preAdapt()
    {
      return std::accumulate(grids_.begin(), grids_.end(), false,
        [](bool b, auto& grid) { return grid->preAdapt() || b; });
    }

    /// Triggers the grid refinement process
    bool adapt()
    {
      return std::accumulate(grids_.begin(), grids_.end(), false,
        [](bool b, auto& grid) { return grid->adapt() || b; });
    }

    /// Clean up refinement markers
    void postAdapt()
    {
      for (auto& grid : grids_)
        grid->postAdapt();
    }

    /*@}*/


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

    /// The grids to handle by this multimesh grid-adapter
    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;
    };

    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;
    };
  } // end namespace Capabilities

} // end namespace Dune

#endif // DUNE_MULTIMESH_GRID_HH