Dune Core Modules (2.7.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
12namespace 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 () : _entity(), _maxlevel(-1), stack() {}
31
33 YaspHierarchicIterator (const YGLI& g, const I& it, int maxlevel) :
34 _entity(YaspEntity<0, dim, GridImp>(g,it))
35 {
36 // store reference to entity implementation for better readability
37 YaspEntityImp& entity = _entity.impl();
38 // now iterator points to current cell
39 StackElem se(entity._g);
40 std::copy(entity._it.coord().begin(), entity._it.coord().end(), se.coord.begin());
41 stack.push(se);
42
43 // determine maximum level
44 _maxlevel = std::min(maxlevel,entity._g->mg->maxLevel());
45
46 // if maxlevel not reached then push yourself and sons
47 if (entity._g->level()<_maxlevel)
48 {
49 push_sons();
50 }
51
52 // and make iterator point to first son if stack is not empty
53 if (!stack.empty())
54 pop_tos();
55 }
56
59 _entity(it._entity),
60 _maxlevel(it._maxlevel), stack(it.stack)
61 {}
62
64 void increment ()
65 {
66 // sanity check: do nothing when stack is empty
67 if (stack.empty()) return;
68
69 // if maxlevel not reached then push sons
70 if (_entity.impl()._g->level()<_maxlevel)
71 push_sons();
72
73 // in any case pop one element
74 pop_tos();
75 }
76
78 bool equals (const YaspHierarchicIterator& rhs) const
79 {
80 return (_entity == rhs._entity);
81 }
82
84 const Entity& dereference() const
85 {
86 return _entity;
87 }
88
89 void print (std::ostream& s) const
90 {
91 // store reference to entity implementation for better readability
92 YaspEntityImp& entity = _entity.impl();
93 s << "HIER: " << "level=" << entity._g.level()
94 << " position=" << entity._it.coord()
95 << " superindex=" << entity._it.superindex()
96 << " maxlevel=" << entity._maxlevel
97 << " stacksize=" << stack.size()
98 << std::endl;
99 }
100
101 private:
102 Entity _entity;
103
104 int _maxlevel;
105
106 struct StackElem {
107 YGLI g; // grid level of the element
108 std::array<int,dim> coord; // and the coordinates
109 StackElem(YGLI gg) : g(gg) {}
110 };
111 std::stack<StackElem> stack;
112
113 // push sons of current element on the stack
114 void push_sons ()
115 {
116 // store reference to entity implementation for better readability
117 YaspEntityImp& entity = _entity.impl();
118
119 // yes, process all 1<<dim sons
120 YGLI finer = entity._g;
121 ++finer;
122 StackElem se(finer);
123 for (int i=0; i<(1<<dim); i++)
124 {
125 for (int k=0; k<dim; k++)
126 if (i&(1<<k))
127 se.coord[k] = entity._it.coord(k)*2+1;
128 else
129 se.coord[k] = entity._it.coord(k)*2;
130 // not all entities have 2^d subentities due to refineOptions with keep_ovlp==false
131 bool exists = true;
132 for (int k=0; k<dim; k++)
133 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)))
134 exists = false;
135 if (exists)
136 stack.push(se);
137 }
138 }
139
140 // make TOS the current element
141 void pop_tos ()
142 {
143 StackElem se = stack.top();
144 stack.pop();
145 YaspEntityImp& entity = _entity.impl();
146 entity._g = se.g;
147 entity._it.reinit(entity._g->overlap[0],se.coord);
148 }
149 };
150
151} // namespace Dune
152
153#endif // DUNE_GRID_YASPGRIDHIERARCHICITERATOR_HH
YaspHierarchicIterator enables iteration over son entities of codim 0.
Definition: yaspgridhierarchiciterator.hh:18
YaspHierarchicIterator()
default constructor creating empty iterator
Definition: yaspgridhierarchiciterator.hh:30
YaspHierarchicIterator(const YaspHierarchicIterator &it)
constructor
Definition: yaspgridhierarchiciterator.hh:58
const Entity & dereference() const
dereferencing
Definition: yaspgridhierarchiciterator.hh:84
void increment()
increment
Definition: yaspgridhierarchiciterator.hh:64
bool equals(const YaspHierarchicIterator &rhs) const
equality
Definition: yaspgridhierarchiciterator.hh:78
YaspHierarchicIterator(const YGLI &g, const I &it, int maxlevel)
constructor
Definition: yaspgridhierarchiciterator.hh:33
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition: defaults.hh:87
Dune namespace.
Definition: alignedallocator.hh:14
Static tag representing a codimension.
Definition: dimension.hh:22
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)