mmiterator.hh 11 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1 2 3 4 5 6 7
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_MULTIMESH_ITERATOR_HH
#define DUNE_MULTIMESH_ITERATOR_HH

#include <numeric>
#include <stack>
8
#include <variant>
Praetorius, Simon's avatar
Praetorius, Simon committed
9 10

#include <dune/common/iteratorfacades.hh>
11
#include <dune/common/std/type_traits.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
12 13 14
#include <dune/grid/common/exceptions.hh>
#include <dune/grid/common/gridenums.hh>

Praetorius, Simon's avatar
Praetorius, Simon committed
15
#include "mmentity.hh"
Praetorius, Simon's avatar
Praetorius, Simon committed
16 17 18

namespace Dune
{
19 20 21 22 23
  namespace tag
  {
    struct with_gridview {};
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
24 25 26
  /** \brief Iterator over all entities of a given codimension and level of a grid.
   *  \ingroup MultiMesh
   */
27
  template <int codim, PartitionIteratorType pitype, 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
  class MultiMeshIterator
  {
  public:
    template <class... Args>
    MultiMeshIterator(Args&&... args)
    {
      DUNE_THROW(NotImplemented, "Not implemented for entity of codim != 0!");
    }

    int operator*() const { return 0; }
    MultiMeshIterator& operator++() { return *this; }
    MultiMeshIterator operator++(int) { return *this; }
    bool operator!=(MultiMeshIterator const&) const { return false; }
  };


  // Implemented for codim 0 entities only
45 46 47 48 49
  template <PartitionIteratorType pitype, class HostGrid>
  class MultiMeshIterator<0, pitype, HostGrid>
      : public ForwardIteratorFacade<MultiMeshIterator<0,pitype,HostGrid>,
            MultiEntity<HostGrid>,
            MultiEntity<HostGrid> const&>
Praetorius, Simon's avatar
Praetorius, Simon committed
50 51 52 53
  {
  private:
    // LevelIterator to the equivalent entity in the host grid
    using HostGridLevelIterator =
54
      typename HostGrid::template Codim<0>::template Partition<pitype>::LevelIterator;
Praetorius, Simon's avatar
Praetorius, Simon committed
55

56
    using HostEntity = typename HostGrid::template Codim<0>::Entity;
Praetorius, Simon's avatar
Praetorius, Simon committed
57

58 59
    using EntityTest = std::function<bool(HostEntity)>;

Praetorius, Simon's avatar
Praetorius, Simon committed
60 61
    struct EntityStackEntry
    {
62
      template <class Entity>
Praetorius, Simon's avatar
Praetorius, Simon committed
63
      explicit EntityStackEntry (Entity&& entity)
64 65
        : it(entity.hbegin(entity.level()+1))
        , end(entity.hend(entity.level()+1))
Praetorius, Simon's avatar
Praetorius, Simon committed
66 67 68 69 70 71 72 73 74 75 76 77
      {}

      typename HostEntity::HierarchicIterator it;
      typename HostEntity::HierarchicIterator end;
    };

    class EntityStack
        : public std::stack<EntityStackEntry, std::vector<EntityStackEntry>>
    {
      using Super = std::stack<EntityStackEntry, std::vector<EntityStackEntry>>;

    public:
78
      explicit EntityStack (int maxLevel)
Praetorius, Simon's avatar
Praetorius, Simon committed
79
      {
80
        c.reserve(maxLevel);
Praetorius, Simon's avatar
Praetorius, Simon committed
81 82
      }

83
      // return true if all levels >= l are finished, i.e. hierarchic iterators it+1 == end
Praetorius, Simon's avatar
Praetorius, Simon committed
84
      bool finished (std::size_t l = 0) const
Praetorius, Simon's avatar
Praetorius, Simon committed
85 86
      {
        bool f = true;
87 88
        for (auto tmp = c[l].it; f && l < c.size(); ++l) {
          tmp = c[l].it;
Praetorius, Simon's avatar
Praetorius, Simon committed
89
          f = f && (++tmp) == c[l].end;
90
        }
Praetorius, Simon's avatar
Praetorius, Simon committed
91 92 93 94 95 96 97 98 99
        return f;
      }

    protected:
      using Super::c;
    };

  public:
    /// Constructor. Stores a pointer to the grid
100
    template <class GridImp>
101 102 103 104
    MultiMeshIterator (const GridImp* multiMesh, int level)
      : incrementAllowed_(multiMesh->size(), true)
      , contains_(multiMesh->size(), EntityTest{[level](const HostEntity& entity) { return entity.level() == level; }})
      , maxLevel_(multiMesh->size(), level)
105
      , multiEntity_(multiMesh->size())
Praetorius, Simon's avatar
Praetorius, Simon committed
106
    {
107
      for (auto const& grid : multiMesh->grids_) {
Praetorius, Simon's avatar
Praetorius, Simon committed
108 109 110 111 112
        macroIterators_.push_back(grid->levelGridView(0).template begin<0,pitype>());
        macroEndIterators_.push_back(grid->levelGridView(0).template end<0,pitype>());
      }

      // go to first leaf entity on all grids
113 114 115 116
      entityStacks_.reserve(multiMesh->size());
      for (std::size_t i = 0; i < multiMesh->size(); ++i) {
        maxLevel_[i] = (*multiMesh)[i].maxLevel();
        entityStacks_.emplace_back((*multiMesh)[i].maxLevel());
Praetorius, Simon's avatar
Praetorius, Simon committed
117
        initialIncrement(i);
118
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
119 120 121 122 123 124 125
    }

    /// Constructor which create the end iterator
    /**
     * \param  multiMesh  Pointer to grid instance
     * \param  endDummy   Here only to distinguish it from the other constructor
     */
126
    template <class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
127 128
    MultiMeshIterator (const GridImp* multiMesh, int level, bool endDummy)
    {
129
      for (auto const& grid : multiMesh->grids_)
Praetorius, Simon's avatar
Praetorius, Simon committed
130 131 132
        macroIterators_.push_back(grid->levelGridView(0).template end<0,pitype>());
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
133
    /// Constructor which create the leaf-iterator
134
    template <class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
135 136 137 138
    MultiMeshIterator (const GridImp* multiMesh)
      : MultiMeshIterator{multiMesh, -1}
    {}

Praetorius, Simon's avatar
Praetorius, Simon committed
139
    /// Constructor which create the end leaf-iterator
140
    template <class GridImp>
Praetorius, Simon's avatar
Praetorius, Simon committed
141 142 143 144
    MultiMeshIterator (const GridImp* multiMesh, bool endDummy)
      : MultiMeshIterator{multiMesh, -1, endDummy}
    {}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    template <class... GridViews,
      std::enable_if_t<not Std::disjunction<std::is_same<GridViews,bool>...>::value, int> = 0>
    MultiMeshIterator (tag::with_gridview, GridViews const&... gridViews)
      : incrementAllowed_(sizeof...(GridViews), true)
      , maxLevel_{gridViews.grid().maxLevel()...}
      , multiEntity_(sizeof...(GridViews))
      , macroIterators_{gridViews.grid().levelGridView(0).template begin<0,pitype>()...}
      , macroEndIterators_{gridViews.grid().levelGridView(0).template end<0,pitype>()...}
      , entityStacks_{EntityStack{gridViews.grid().maxLevel()}...}
    {
      contains_.reserve(sizeof...(GridViews));
      Hybrid::forEach(std::forward_as_tuple(gridViews...), [this](auto const& gv) {
        contains_.emplace_back([gv](const HostEntity& entity) { return gv.contains(entity); });
      });

      for (std::size_t i = 0; i < sizeof...(GridViews); ++i)
        initialIncrement(i);
    }

    template <class... GridViews>
    MultiMeshIterator (tag::with_gridview, bool endDummy, GridViews const&... gridViews)
      : macroIterators_{gridViews.grid().levelGridView(0).template end<0,pitype>()...}
    {}

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
#if DUNE_HAVE_CXX_VARIANT
    template <class... GV>
    MultiMeshIterator (const std::vector<std::variant<GV...>>& gridViews)
      : incrementAllowed_(gridViews.size(), true)
      , multiEntity_(gridViews.size())
    {
      contains_.reserve(gridViews.size());
      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));
        contains_.emplace_back(std::visit([](auto const& gv) {
          return EntityTest{[gv](const HostEntity& entity) { return gv.contains(entity); }}; }, 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));
      }

      // go to first leaf entity on all grids
      for (std::size_t i = 0; i < entityStacks_.size(); ++i)
        initialIncrement(i);
    }

    template <class... GV>
    MultiMeshIterator (const std::vector<std::variant<GV...>>& gridViews, bool endDummy)
    {
      for (auto const& gvs : gridViews) {
        macroIterators_.push_back(std::visit([](auto const& gv) {
          return gv.grid().levelGridView(0).template end<0,pitype>(); }, gvs));
      }
    }
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217

    /// prefix increment
    void increment ()
    {
      for (std::size_t i = 0; i < entityStacks_.size(); ++i)
        incrementAllowed_[i] = incrementAllowed(i);

      for (std::size_t i = 0; i < entityStacks_.size(); ++i) {
        if (incrementAllowed_[i])
          increment(i);
      }
    }

    /// dereferencing
218
    MultiEntity<HostGrid> const& dereference () const
Praetorius, Simon's avatar
Praetorius, Simon committed
219
    {
220 221 222 223 224 225
      // update entries in multiEntity that have changed
      for (std::size_t i = 0; i < entityStacks_.size(); ++i) {
        if (incrementAllowed_[i])
          multiEntity_[i] = dereference(i);
      }
      return multiEntity_;
Praetorius, Simon's avatar
Praetorius, Simon committed
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
    }

    /// equality
    bool equals (const MultiMeshIterator& that) const
    {
      return macroIterators_ == that.macroIterators_;
    }

  protected:
    // got to next entity in grid i
    void 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;
      }

      // 3. go down in tree until leaf entity
261
      for (auto child = dereference(i); !entityTest(i,child);
262
                child = dereference(i)) {
263
        entityStack.emplace(child);
264
        assert( entityStack.size() <= maxLevel_[i] );
265
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    }

    /// 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,
        [i,size](bool allowed, auto const& entityStack) {
          return allowed && (entityStack.size() <= size || entityStack.finished(size));
        });
    }

    // got to first leaf entity on grid i
    void 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;

      // 2. go down in tree until leaf entity
290
      for (auto child = dereference(i); !entityTest(i,child);
291
                child = dereference(i)) {
292
        entityStack.emplace(child);
293
        assert( entityStack.size() <= maxLevel_[i] );
294
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
295 296 297 298
    }

    HostEntity dereference (std::size_t i) const
    {
299 300
      if (entityStacks_[i].empty()) {
        assert(macroIterators_[i] != macroEndIterators_[i]);
301
        return *macroIterators_[i];
302 303
      } else {
        assert(entityStacks_[i].top().it != entityStacks_[i].top().end);
304
        return *entityStacks_[i].top().it;
305
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
306 307
    }

308
    bool entityTest (std::size_t i, HostEntity const& entity) const
Praetorius, Simon's avatar
Praetorius, Simon committed
309
    {
310
      return contains_[i](entity) || entity.isLeaf() || !entity.isRegular();
Praetorius, Simon's avatar
Praetorius, Simon committed
311 312 313
    }

  private:
314 315 316 317
    std::vector<bool> incrementAllowed_;
    std::vector<EntityTest> contains_;
    std::vector<int> maxLevel_;

318
    mutable MultiEntity<HostGrid> multiEntity_;
Praetorius, Simon's avatar
Praetorius, Simon committed
319 320 321 322 323 324

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

325 326 327 328 329 330
  template <class> class MultiMesh;

  template <class... GridViews>
  inline auto multi_elements(GridViews const&... gridViews)
  {
    using GridView0 = std::tuple_element_t<0,std::tuple<GridViews...>>;
Praetorius, Simon's avatar
Praetorius, Simon committed
331
    using Iterator = MultiMeshIterator<0,All_Partition,typename GridView0::Grid>;
332 333 334 335 336
    using Range = IteratorRange<Iterator>;
    return Range{ Iterator{tag::with_gridview{}, gridViews...},
                  Iterator{tag::with_gridview{}, true, gridViews...} };
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
337 338 339
}  // end namespace Dune

#endif // DUNE_MULTIMESH_ITERATOR_HH