Dune Core Modules (2.10.0)

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