Dune Core Modules (2.6.0)

yaspgridhierarchiciterator.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GRID_YASPGRIDHIERARCHICITERATOR_HH
4 #define DUNE_GRID_YASPGRIDHIERARCHICITERATOR_HH
5 
12 namespace Dune {
13 
16  template<class GridImp>
18  {
19  enum { dim=GridImp::dimension };
20 
21  typedef YaspEntity<0,GridImp::dimension,GridImp> YaspEntityImp;
22 
23  public:
24  // types used from grids
25  typedef typename GridImp::YGridLevelIterator YGLI;
26  typedef typename GridImp::YGrid::Iterator I;
27  typedef typename GridImp::template Codim<0>::Entity Entity;
28 
30  YaspHierarchicIterator (const YGLI& g, const I& it, int maxlevel) :
31  _entity(YaspEntity<0, dim, GridImp>(g,it))
32  {
33  // store reference to entity implementation for better readability
34  YaspEntityImp& entity = entityImplementation();
35  // now iterator points to current cell
36  StackElem se(entity._g);
37  std::copy(entity._it.coord().begin(), entity._it.coord().end(), se.coord.begin());
38  stack.push(se);
39 
40  // determine maximum level
41  _maxlevel = std::min(maxlevel,entity._g->mg->maxLevel());
42 
43  // if maxlevel not reached then push yourself and sons
44  if (entity._g->level()<_maxlevel)
45  {
46  push_sons();
47  }
48 
49  // and make iterator point to first son if stack is not empty
50  if (!stack.empty())
51  pop_tos();
52  }
53 
56  _entity(it._entity),
57  _maxlevel(it._maxlevel), stack(it.stack)
58  {}
59 
61  void increment ()
62  {
63  // sanity check: do nothing when stack is empty
64  if (stack.empty()) return;
65 
66  // if maxlevel not reached then push sons
67  if (entityImplementation()._g->level()<_maxlevel)
68  push_sons();
69 
70  // in any case pop one element
71  pop_tos();
72  }
73 
75  bool equals (const YaspHierarchicIterator& rhs) const
76  {
77  return (_entity == rhs._entity);
78  }
79 
81  const Entity& dereference() const
82  {
83  return _entity;
84  }
85 
86  void print (std::ostream& s) const
87  {
88  // store reference to entity implementation for better readability
89  YaspEntityImp& entity = entityImplementation();
90  s << "HIER: " << "level=" << entity._g.level()
91  << " position=" << entity._it.coord()
92  << " superindex=" << entity._it.superindex()
93  << " maxlevel=" << entity._maxlevel
94  << " stacksize=" << stack.size()
95  << std::endl;
96  }
97 
98  private:
99  Entity _entity;
100 
101  int _maxlevel;
102 
103  struct StackElem {
104  YGLI g; // grid level of the element
105  std::array<int,dim> coord; // and the coordinates
106  StackElem(YGLI gg) : g(gg) {}
107  };
108  std::stack<StackElem> stack;
109 
110  // push sons of current element on the stack
111  void push_sons ()
112  {
113  // store reference to entity implementation for better readability
114  YaspEntityImp& entity = entityImplementation();
115 
116  // yes, process all 1<<dim sons
117  YGLI finer = entity._g;
118  ++finer;
119  StackElem se(finer);
120  for (int i=0; i<(1<<dim); i++)
121  {
122  for (int k=0; k<dim; k++)
123  if (i&(1<<k))
124  se.coord[k] = entity._it.coord(k)*2+1;
125  else
126  se.coord[k] = entity._it.coord(k)*2;
127  // not all entities have 2^d subentities due to refineOptions with keep_ovlp==false
128  bool exists = true;
129  for (int k=0; k<dim; k++)
130  if ((se.coord[k] < finer->overlap[0].dataBegin()->origin(k)) || (se.coord[k] >= finer->overlap[0].dataBegin()->origin(k)+finer->overlap[0].dataBegin()->size(k)))
131  exists = false;
132  if (exists)
133  stack.push(se);
134  }
135  }
136 
137  YaspEntityImp& entityImplementation()
138  {
139  return GridImp::getRealImplementation(this->_entity);
140  }
141 
142  // make TOS the current element
143  void pop_tos ()
144  {
145  StackElem se = stack.top();
146  stack.pop();
147  entityImplementation()._g = se.g;
148  entityImplementation()._it.reinit(entityImplementation()._g->overlap[0],se.coord);
149  }
150  };
151 
152 } // namespace Dune
153 
154 #endif // DUNE_GRID_YASPGRIDHIERARCHICITERATOR_HH
YaspHierarchicIterator enables iteration over son entities of codim 0.
Definition: yaspgridhierarchiciterator.hh:18
YaspHierarchicIterator(const YaspHierarchicIterator &it)
constructor
Definition: yaspgridhierarchiciterator.hh:55
const Entity & dereference() const
dereferencing
Definition: yaspgridhierarchiciterator.hh:81
void increment()
increment
Definition: yaspgridhierarchiciterator.hh:61
bool equals(const YaspHierarchicIterator &rhs) const
equality
Definition: yaspgridhierarchiciterator.hh:75
YaspHierarchicIterator(const YGLI &g, const I &it, int maxlevel)
constructor
Definition: yaspgridhierarchiciterator.hh:30
Dune namespace.
Definition: alignedallocator.hh:10
Static tag representing a codimension.
Definition: dimension.hh:22
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 1, 22:29, 2024)