mmiteratorbase.hh 8.13 KB
Newer Older
1 2 3 4 5 6 7 8
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_MULTIMESH_ITERATOR_BASE_HH
#define DUNE_MULTIMESH_ITERATOR_BASE_HH

#include <numeric>
#include <stack>
#include <type_traits>
9 10 11
#if DUNE_HAVE_CXX_VARIANT
#include <variant>
#endif
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

#include <dune/common/std/type_traits.hh>
#include <dune/grid/common/gridenums.hh>

#include "mmentity.hh"
#include "mmhierarchiciterator.hh"
#include "mmiteratorinterface.hh"

namespace Dune
{
  template <PartitionIteratorType pitype, class HostGrid, class Derived>
  class MultiMeshIteratorBase
      : public MultiMeshIteratorInterface<HostGrid,Derived>
  {
    using Super = MultiMeshIteratorInterface<HostGrid,Derived>;

  protected:
    // LevelIterator to the equivalent entity in the host grid
    using HostGridLevelIterator =
      typename HostGrid::template Codim<0>::template Partition<pitype>::LevelIterator;

    template <class GV>
    using IsGridView = Std::disjunction<
      std::is_same<std::decay_t<GV>,typename HostGrid::LeafGridView>,
      std::is_same<std::decay_t<GV>,typename HostGrid::LevelGridView> >;

    using HostEntity = typename HostGrid::template Codim<0>::Entity;

    using EntityTest = typename MultiMeshHierarchicIterator<HostGrid>::EntityTest;
    using EntityStack = typename MultiMeshHierarchicIterator<HostGrid>::EntityStack;

  public:

    /// Constructor which creates the begin iterator
    /**
     * \param  multiMesh  Pointer to grid instance
     */
    template <class GridImp>
    MultiMeshIteratorBase (tag::begin_iterator, const GridImp* multiMesh)
      : maxLevel_(multiMesh->size())
    {
      for (std::size_t i = 0; i < multiMesh->size(); ++i) {
        macroIterators_.push_back(multiMesh->levelGridView(i,0).template begin<0,pitype>());
        macroEndIterators_.push_back(multiMesh->levelGridView(i,0).template end<0,pitype>());
      }

      // go to first leaf entity on all grids
      entityStacks_.reserve(multiMesh->size());
      for (std::size_t i = 0; i < multiMesh->size(); ++i) {
61 62
        maxLevel_[i] = multiMesh->maxLevel(i);
        entityStacks_.emplace_back(multiMesh->maxLevel(i));
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
      }
    }

    /// Constructor which creates the end iterator
    /**
     * \param  multiMesh  Pointer to grid instance
     */
    template <class GridImp>
    MultiMeshIteratorBase (tag::end_iterator, const GridImp* multiMesh)
    {
      for (std::size_t i = 0; i < multiMesh->size(); ++i)
        macroIterators_.push_back(multiMesh->levelGridView(i,0).template end<0,pitype>());
    }

    /// Construct an iterator from n gridViews of possibly different type
    template <class... GridViews,
      std::enable_if_t<Std::conjunction<IsGridView<GridViews>...>::value, int> = 0>
    MultiMeshIteratorBase (tag::begin_iterator, GridViews const&... gridViews)
      : maxLevel_{gridViews.grid().maxLevel()...}
      , macroIterators_{gridViews.grid().levelGridView(0).template begin<0,pitype>()...}
      , macroEndIterators_{gridViews.grid().levelGridView(0).template end<0,pitype>()...}
      , entityStacks_{EntityStack{gridViews.grid().maxLevel()}...}
    {}

    template <class... GridViews,
      std::enable_if_t<Std::conjunction<IsGridView<GridViews>...>::value, int> = 0>
    MultiMeshIteratorBase (tag::end_iterator, GridViews const&... gridViews)
      : macroIterators_{gridViews.grid().levelGridView(0).template end<0,pitype>()...}
    {}

#if DUNE_HAVE_CXX_VARIANT
    /// Construct an iterator from a vector of GridViews, where each GridView can be
    /// either a leaf- or a level-gridView, thus we need to use a variant.
    template <class... GV>
    MultiMeshIteratorBase (tag::begin_iterator, const std::vector<std::variant<GV...>>& gridViews)
    {
      entityStacks_.reserve(gridViews.size());
      maxLevel_.reserve(gridViews.size());

      for (auto const& gvs : gridViews) {
        macroIterators_.push_back(std::visit([](auto const& gv) {
          return gv.grid().levelGridView(0).template begin<0,pitype>(); }, gvs));
        macroEndIterators_.push_back(std::visit([](auto const& gv) {
          return gv.grid().levelGridView(0).template end<0,pitype>(); }, gvs));
        maxLevel_.push_back(std::visit([](auto const& gv) {
          return gv.grid().maxLevel(); }, gvs));
        entityStacks_.emplace_back(std::visit([](auto const& gv) {
          return gv.grid().maxLevel(); }, gvs));
      }
    }

    template <class... GV>
    MultiMeshIteratorBase (tag::end_iterator, const std::vector<std::variant<GV...>>& gridViews)
    {
      for (auto const& gvs : gridViews) {
        macroIterators_.push_back(std::visit([](auto const& gv) {
          return gv.grid().levelGridView(0).template end<0,pitype>(); }, gvs));
      }
    }
#endif

    /// equality
    bool equals_impl (const MultiMeshIteratorBase& that) const
    {
      return macroIterators_ == that.macroIterators_;
    }

    // got to next entity in grid i
    int increment (std::size_t i)
    {
      auto& entityStack = entityStacks_[i];
      auto& macroIt = macroIterators_[i];
      auto const& macroEnd = macroEndIterators_[i];

      // 1. go up in tree or to next entity on current level until we can go down again
      while (!entityStack.empty()) {
        auto& top = entityStack.top();
        ++top.it;
        if (top.it == top.end) {
          entityStack.pop();
        } else {
          break;
        }
      }

      // 2. if entityStack is empty, go to next macroElement
      if (entityStack.empty()) {
        ++macroIt;
        if (macroIt == macroEnd)
          return 0;
      }

      // 3. go down in tree until leaf entity
      auto child = dereference(i);
      for (; !this->levelReached(i, child); child = dereference(i)) {
        entityStack.emplace(child);
159
        assert(int(entityStack.size()) <= maxLevel_[i]);
160 161
      }

162 163 164 165 166 167 168
      // 4. go up in tree again to the first regular entity, since
      // irregular element can not be traversed in a multi-mesh sense
      while (!child.isRegular() && !entityStack.empty()) {
        entityStack.pop();
        child = dereference(i);
      }

169 170 171 172 173 174 175 176 177
      return entityStack.size();
    }
    using Super::increment;

    /// Return true, if all stacks with size > stack[i].size are finished
    bool incrementAllowed (std::size_t i) const
    {
      std::size_t size = entityStacks_[i].size();
      return std::accumulate(entityStacks_.begin(), entityStacks_.end(), true,
178
        [size](bool allowed, auto const& entityStack) {
179 180 181 182
          return allowed && (entityStack.size() <= size || entityStack.finished(size));
        });
    }

183
    // go to first entity on grid i on the desired level
184 185 186 187 188 189 190 191 192 193 194 195 196 197
    int initialIncrement (std::size_t i)
    {
      auto& entityStack = entityStacks_[i];
      auto& macroIt = macroIterators_[i];
      auto const& macroEnd = macroEndIterators_[i];

      assert(entityStack.empty());
      if (macroIt == macroEnd)
        return 0;

      // 1. go down in tree until desired level is reached
      auto child = dereference(i);
      for (; !this->levelReached(i, child); child = dereference(i)) {
        entityStack.emplace(child);
198
        assert(int(entityStack.size()) <= maxLevel_[i]);
199 200
      }

201 202 203 204 205 206 207
      // 2. go up in tree again to the first regular entity, since
      // irregular element can not be traversed in a multi-mesh sense
      while (!child.isRegular() && !entityStack.empty()) {
        entityStack.pop();
        child = dereference(i);
      }

208 209 210 211
      return entityStack.size();
    }
    using Super::initialIncrement;

212
    // Return the current entity the hierarchic iterator or macro iterator pointing to
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
    HostEntity dereference (std::size_t i) const
    {
      if (entityStacks_[i].empty()) {
        assert(macroIterators_[i] != macroEndIterators_[i]);
        return *macroIterators_[i];
      } else {
        assert(entityStacks_[i].top().it != entityStacks_[i].top().end);
        return *entityStacks_[i].top().it;
      }
    }
    using Super::dereference;

    std::size_t size () const
    {
      return entityStacks_.size();
    }

  private:
    std::vector<int> maxLevel_;

    std::vector<HostGridLevelIterator> macroIterators_;
    std::vector<HostGridLevelIterator> macroEndIterators_;
    std::vector<EntityStack> entityStacks_;
  };

}  // end namespace Dune

#endif // DUNE_MULTIMESH_ITERATOR_BASE_HH