Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
hierarchicleafiterator.hh
Go to the documentation of this file.
1// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set ts=8 sw=4 et sts=4:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef HIERARCHIC_LEAF_ITERATOR_HH
8#define HIERARCHIC_LEAF_ITERATOR_HH
9
12
15
23template <class GridImp>
25 public Dune::ForwardIteratorFacade<HierarchicLeafIterator<GridImp>, const typename GridImp::template Codim<0>::Entity>
26{
27 typedef typename GridImp::template Codim<0>::Entity Element;
28 typedef typename GridImp::HierarchicIterator HierarchicIterator;
29 enum {dim = GridImp::dimension};
30 enum {dimworld = GridImp::dimensionworld};
31public:
33
35
36 HierarchicLeafIterator(const GridImp& grid, const Element& element,
37 PositionFlag flag, bool nested = true)
38 : element_(element), maxlevel_(grid.maxLevel()), hIt_(element.hend(maxlevel_)),
39 flag_(flag), nested_(nested)
40 {
41
42 // if the element itself is leaf, then we don't have to iterate over the children
43 if (flag==begin && !element_.isLeaf()) {
44 hIt_ = element_.hbegin(maxlevel_);
45
46 //NOTE This class by now assumes that possible non-nestedness of the grid levels only arises
47 // due to boundary parametrisation
48 if (!nested_) {
49 // Check if the element is a boundary element, and set the nested flag correspondingly
50 // If the element is not at the boundary, then we can neglect the nested flag
51 typename GridImp::LevelGridView levelView = grid.levelGridView(element_.level());
52 nested_ = true;
53
54 for (const auto lIt : intersections(levelView,element_))
55 if (lIt.boundary()) {
56 nested_ = false;
57 break;
58 }
59 }
60
61 if (!hIt_->isLeaf())
62 increment();
63 else
64 leafChild_ = *hIt_;
65 }
66 }
67
69 bool equals(const HierarchicLeafIterator<GridImp>& other) const {
70 return (element_ == other.element_) &&
71 ((flag_==other.flag_ && flag_==end) || (hIt_ == other.hIt_ && flag_==begin && other.flag_==flag_));
72 }
73
75 void increment() {
76
77 HierarchicIterator hEndIt = element_.hend(maxlevel_);
78
79 if (hIt_ == hEndIt) {
80 flag_ = end;
81 return;
82 }
83
84 // Increment until we hit a leaf child element
85 do {
86 ++hIt_;
87 } while(hIt_ != hEndIt && (!hIt_->isLeaf()));
88
89 if (hIt_ == hEndIt)
90 flag_ = end;
91 else
92 leafChild_ = *hIt_;
93 }
94
96 const Element& dereference() const {
97 if (flag_ == end)
98 DUNE_THROW(Dune::Exception,"HierarchicLeafIterator: Cannot dereference end iterator!");
99 return (element_.isLeaf()) ? element_ : leafChild_;
100 }
101
104
105 //NOTE: We assume here that the type of the child and the ancestors are the same!
106 auto ref = Dune::ReferenceElements<typename GridImp::ctype,dim>::general(element_.type());
107
108 // store local coordinates of the leaf child element within the coarse starting element
110 for (int i=0; i<corners.size(); i++)
111 corners[i] = ref.position(i,dim);
112
113 // If the element is leaf, then return an Identity mapping
114 if (element_.isLeaf())
115 return LocalGeometry(ref,corners);
116
117 if (nested_) {
118 const typename Element::Geometry leafGeom = leafChild_->geometry();
119 const typename Element::Geometry coarseGeom = element_.geometry();
120
121 for (int i=0; i<corners.size();i++)
122 corners[i] = coarseGeom.local(leafGeom.corner(i));
123 return LocalGeometry(ref,corners);
124 }
125
126 Element father(leafChild_);
127 while (father != element_) {
128
129 const typename Element::LocalGeometry fatherGeom = father.geometryInFather();
130 father = father->father();
131
132 for (int i=0; i<corners.size(); i++)
133 corners[i] = fatherGeom.global(corners[i]);
134 }
135
136 return LocalGeometry(ref,corners);
137 }
138
139private:
140
142 const Element& element_;
143
145 int maxlevel_;
146
148 HierarchicIterator hIt_;
149
151 Element leafChild_;
152
154 PositionFlag flag_;
155
157 bool nested_;
158
159};
160#endif
int maxLevel() const
#define DUNE_THROW(E,...)
LevelGridView levelGridView(int level) const
Hierarchic leaf iterator.
Definition hierarchicleafiterator.hh:26
bool equals(const HierarchicLeafIterator< GridImp > &other) const
Equality.
Definition hierarchicleafiterator.hh:69
LocalGeometry geometryInAncestor()
Compute the local geometry mapping from the leaf child to the starting element.
Definition hierarchicleafiterator.hh:103
PositionFlag
Definition hierarchicleafiterator.hh:34
@ begin
Definition hierarchicleafiterator.hh:34
@ end
Definition hierarchicleafiterator.hh:34
Dune::CachedMultiLinearGeometry< typename GridImp::ctype, dim, dimworld > LocalGeometry
Definition hierarchicleafiterator.hh:32
const Element & dereference() const
Dereferencing.
Definition hierarchicleafiterator.hh:96
HierarchicLeafIterator(const GridImp &grid, const Element &element, PositionFlag flag, bool nested=true)
Definition hierarchicleafiterator.hh:36
void increment()
Prefix increment.
Definition hierarchicleafiterator.hh:75
T size(T... args)