Dune Core Modules (2.6.0)

iterator.hh
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_GEOGRID_ITERATOR_HH
4#define DUNE_GEOGRID_ITERATOR_HH
5
6#include <cassert>
7
8#include <type_traits>
9#include <utility>
10
11#include <dune/geometry/referenceelements.hh>
12
13#include <dune/grid/geometrygrid/capabilities.hh>
14#include <dune/grid/geometrygrid/declaration.hh>
15#include <dune/grid/geometrygrid/entity.hh>
16
17namespace Dune
18{
19
20 namespace GeoGrid
21 {
22
23 // Internal Forward Declarations
24 // -----------------------------
25
26 template< class HostGridView, int codim, PartitionIteratorType pitype, class Grid,
27 bool fake = !Capabilities::hasHostEntity< Grid, codim >::v >
28 class Iterator;
29
30 template< class Grid >
31 class HierarchicIterator;
32
33
34
35 // PartitionIteratorFilter
36 // -----------------------
37
38 template< int codim, PartitionIteratorType pitype, class Grid >
39 struct PartitionIteratorFilter;
40
41 template< int codim, class Grid >
42 struct PartitionIteratorFilter< codim, Interior_Partition, Grid >
43 {
44 static const int dimension = std::remove_const< Grid >::type::dimension;
45 static const int codimension = codim;
46
47 static const PartitionIteratorType Element_Partition = Interior_Partition;
48
49 typedef typename std::remove_const< Grid >::type::ctype ctype;
50 typedef typename std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element;
51 typedef Transitional::ReferenceElement< ctype, Dim<dimension> > RefElement;
52
53 static bool apply ( const RefElement &refElement,
54 const Element &element, int subEntity )
55 {
56 const int size = refElement.size( subEntity, codim, dimension );
57 for( int i = 0; i < size; ++i )
58 {
59 const int j = refElement.subEntity( subEntity, codim, i, dimension );
60 PartitionType type = element.template subEntity< dimension >( j ).partitionType();
61 if( type == InteriorEntity )
62 return true;
63 }
64 return false;
65 }
66 };
67
68 template< int codim, class Grid >
69 struct PartitionIteratorFilter< codim, InteriorBorder_Partition, Grid >
70 {
71 static const int dimension = std::remove_const< Grid >::type::dimension;
72 static const int codimension = codim;
73
74 static const PartitionIteratorType Element_Partition = Interior_Partition;
75
76 typedef typename std::remove_const< Grid >::type::ctype ctype;
77 typedef typename std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element;
78 typedef Transitional::ReferenceElement< ctype, Dim<dimension> > RefElement;
79
80 static bool apply ( const RefElement &refElement,
81 const Element &element, int subEntity )
82 {
83 return true;
84 }
85 };
86
87 template< int codim, class Grid >
88 struct PartitionIteratorFilter< codim, Overlap_Partition, Grid >
89 {
90 static const int dimension = std::remove_const< Grid >::type::dimension;
91 static const int codimension = codim;
92
93 static const PartitionIteratorType Element_Partition = Overlap_Partition;
94
95 typedef typename std::remove_const< Grid >::type::ctype ctype;
96 typedef typename std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element;
97 typedef Transitional::ReferenceElement< ctype, Dim<dimension> > RefElement;
98
99 static bool apply ( const RefElement &refElement,
100 const Element &element, int subEntity )
101 {
102 if( element.partitionType() == InteriorEntity )
103 return true;
104
105 const int size = refElement.size( subEntity, codim, dimension );
106 for( int i = 0; i < size; ++i )
107 {
108 const int j = refElement.subEntity( subEntity, codim, i, dimension );
109 PartitionType type = element.template subEntity< dimension >( j ).partitionType();
110 if( (type == OverlapEntity) || (type == BorderEntity) )
111 return true;
112 }
113 return false;
114 }
115 };
116
117 template< int codim, class Grid >
118 struct PartitionIteratorFilter< codim, OverlapFront_Partition, Grid >
119 {
120 static const int dimension = std::remove_const< Grid >::type::dimension;
121 static const int codimension = codim;
122
123 static const PartitionIteratorType Element_Partition = Overlap_Partition;
124
125 typedef typename std::remove_const< Grid >::type::ctype ctype;
126 typedef typename std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element;
127 typedef Transitional::ReferenceElement< ctype, Dim<dimension> > RefElement;
128
129 static bool apply ( const RefElement &refElement,
130 const Element &element, int subEntity )
131 {
132 return true;
133 }
134 };
135
136 template< int codim, class Grid >
137 struct PartitionIteratorFilter< codim, All_Partition, Grid >
138 {
139 static const int dimension = std::remove_const< Grid >::type::dimension;
140 static const int codimension = codim;
141
142 static const PartitionIteratorType Element_Partition = All_Partition;
143
144 typedef typename std::remove_const< Grid >::type::ctype ctype;
145 typedef typename std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element;
146 typedef Transitional::ReferenceElement< ctype, Dim<dimension> > RefElement;
147
148 static bool apply ( const RefElement &refElement,
149 const Element &element, int subEntity )
150 {
151 return true;
152 }
153 };
154
155 template< int codim, class Grid >
156 struct PartitionIteratorFilter< codim, Ghost_Partition, Grid >
157 {
158 static const int dimension = std::remove_const< Grid >::type::dimension;
159 static const int codimension = codim;
160
161 static const PartitionIteratorType Element_Partition = Ghost_Partition;
162
163 typedef typename std::remove_const< Grid >::type::ctype ctype;
164 typedef typename std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element;
165 typedef Transitional::ReferenceElement< ctype, Dim<dimension> > RefElement;
166
167 static bool apply ( const RefElement &refElement,
168 const Element &element, int subEntity )
169 {
170 const int size = refElement.size( subEntity, codim, dimension );
171 for( int i = 0; i < size; ++i )
172 {
173 const int j = refElement.subEntity( subEntity, codim, i, dimension );
174 PartitionType type = element.template subEntity< dimension >( j ).partitionType();
175 if( type == GhostEntity )
176 return true;
177 }
178 return false;
179 }
180 };
181
182
183
184 // Iterator (real)
185 // ---------------
186
187 template< class HostGridView, int codim, PartitionIteratorType pitype, class G >
188 class Iterator< HostGridView, codim, pitype, G, false >
189 {
190 typedef typename std::remove_const< G >::type::Traits Traits;
191
192 public:
193 typedef typename Traits::Grid Grid;
194
195 static const int codimension = codim;
196
197 typedef typename Traits::template Codim< codimension >::Entity Entity;
198
199 static const bool fake = false;
200
201 private:
202 typedef GeoGrid::Entity< codimension, Traits::dimension, G > EntityImpl;
203
204 typedef typename HostGridView::template Codim< codim >::template Partition< pitype >::Iterator HostEntityIterator;
205
206 public:
207 Iterator () : grid_( nullptr ) {}
208
209 Iterator ( const Grid &grid, HostEntityIterator hostEntityIterator )
210 : grid_( &grid ),
211 hostEntityIterator_( std::move( hostEntityIterator ) )
212 {}
213
214 void increment ()
215 {
216 ++hostEntityIterator_;
217 }
218
219 bool equals ( const Iterator &rhs ) const
220 {
221 return hostEntityIterator_ == rhs.hostEntityIterator_;
222 }
223
224 Entity dereference () const
225 {
226 return EntityImpl( grid(), *hostEntityIterator_ );
227 }
228
229 int level () const { return hostEntityIterator_.level(); }
230
231 const Grid &grid () const
232 {
233 assert( grid_ );
234 return *grid_;
235 }
236
237 static Iterator begin ( const Grid &grid, const HostGridView &hostGridView )
238 {
239 HostEntityIterator hostEntityIterator = hostGridView.template begin< codimension, pitype >();
240 return Iterator( grid, std::move( hostEntityIterator ) );
241 }
242
243 static Iterator end ( const Grid &grid, const HostGridView &hostGridView )
244 {
245 HostEntityIterator hostEntityIterator = hostGridView.template end< codimension, pitype >();
246 return Iterator( grid, std::move( hostEntityIterator ) );
247 }
248
249 private:
250 const Grid *grid_;
251 HostEntityIterator hostEntityIterator_;
252 };
253
254
255
256 // Iterator (fake)
257 // ---------------
258
259 template< class HostGridView, int codim, PartitionIteratorType pitype, class G >
260 class Iterator< HostGridView, codim, pitype, G, true >
261 {
262 typedef typename std::remove_const< G >::type::Traits Traits;
263
264 public:
265 typedef typename Traits::Grid Grid;
266
267 static const int codimension = codim;
268
269 typedef typename Traits::template Codim< codimension >::Entity Entity;
270
271 private:
272 typedef GeoGrid::Entity< codimension, Traits::dimension, G > EntityImpl;
273
274 typedef PartitionIteratorFilter< codim, pitype, typename HostGridView::Grid > Filter;
275
276 typedef typename HostGridView::template Codim<0>::template Partition< Filter::Element_Partition >::Iterator HostElementIterator;
277 typedef typename HostElementIterator::Entity HostElement;
278 typedef typename HostGridView::IndexSet HostIndexSet;
279
280 public:
281 Iterator () : grid_( nullptr ), subEntity_( -1 ), hostIndexSet_( nullptr ) {}
282
283 Iterator ( const Grid &grid, HostElementIterator hostElementIterator, HostElementIterator hostEnd, const HostIndexSet &hostIndexSet )
284 : grid_( &grid ),
285 hostElementIterator_( hostElementIterator ),
286 hostEnd_( hostEnd ),
287 subEntity_( -1 ),
288 hostIndexSet_( &hostIndexSet )
289 {
290 if( hostElementIterator_ != hostEnd_ )
291 {
292 visited_.resize( hostIndexSet_->size( codimension ), false );
293 increment();
294 }
295 }
296
297 void increment ()
298 {
299 typedef typename Traits::ctype ctype;
300
301 while( hostElementIterator_ != hostEnd_ )
302 {
303 const HostElement &hostElement = *hostElementIterator_;
304
305 auto refElement = referenceElement< ctype, Traits::dimension >( hostElement.type() );
306
307 ++subEntity_;
308 const int count = refElement.size( codimension );
309 for( ; subEntity_ < count; ++subEntity_ )
310 {
311 if( !Filter::apply( refElement, hostElement, subEntity_ ) )
312 continue;
313
314 const size_t index = hostIndexSet_->subIndex( hostElement, subEntity_, codimension );
315 if( !visited_[ index ] )
316 {
317 visited_[ index ] = true;
318 return;
319 }
320 }
321 ++hostElementIterator_;
322 subEntity_ = -1;
323 }
324 }
325
326 bool equals ( const Iterator &rhs ) const
327 {
328 return hostElementIterator_ == rhs.hostElementIterator_ && ( hostElementIterator_ == hostEnd_ || subEntity_ == rhs.subEntity_ );
329 }
330
331 Entity dereference () const
332 {
333 return EntityImpl( grid(), *hostElementIterator_, subEntity_ );
334 }
335
336 int level () const { return hostElementIterator_.level(); }
337
338 const Grid &grid () const
339 {
340 assert( grid_ );
341 return *grid_;
342 }
343
344 static Iterator begin ( const Grid &grid, const HostGridView &hostGridView )
345 {
346 HostElementIterator first = hostGridView.template begin< 0, Filter::Element_Partition >();
347 HostElementIterator last = hostGridView.template end< 0, Filter::Element_Partition >();
348 return Iterator( grid, std::move( first ), std::move( last ), hostGridView.indexSet() );
349 }
350
351 static Iterator end ( const Grid &grid, const HostGridView &hostGridView )
352 {
353 HostElementIterator last = hostGridView.template end< 0, Filter::Element_Partition >();
354 return Iterator( grid, last, last, hostGridView.indexSet() );
355 }
356
357 private:
358 const Grid *grid_;
359 HostElementIterator hostElementIterator_, hostEnd_;
360 int subEntity_;
361 const HostIndexSet *hostIndexSet_;
362 std::vector< bool > visited_;
363 };
364
365
366
367 // HierarchicIterator
368 // ------------------
369
370 template< class G >
371 class HierarchicIterator
372 {
373 typedef typename std::remove_const< G >::type::Traits Traits;
374
375 public:
376 typedef typename Traits::Grid Grid;
377
378 static const int codimension = 0;
379
380 typedef typename Traits::template Codim< codimension >::Entity Entity;
381
382 private:
383 typedef GeoGrid::Entity< codimension, Traits::dimension, G > EntityImpl;
384
385 typedef typename Grid::HostGrid::HierarchicIterator HostEntityIterator;
386
387 public:
388 HierarchicIterator () : grid_( nullptr ) {}
389
390 HierarchicIterator ( const Grid &grid, HostEntityIterator hostEntityIterator )
391 : grid_( &grid ),
392 hostEntityIterator_( std::move( hostEntityIterator ) )
393 {}
394
395 void increment ()
396 {
397 ++hostEntityIterator_;
398 }
399
400 bool equals ( const HierarchicIterator &rhs ) const
401 {
402 return hostEntityIterator_ == rhs.hostEntityIterator_;
403 }
404
405 Entity dereference () const
406 {
407 return EntityImpl( grid(), *hostEntityIterator_ );
408 }
409
410 int level () const { return hostEntityIterator_.level(); }
411
412 const Grid &grid () const
413 {
414 assert( grid_ );
415 return *grid_;
416 }
417
418 private:
419 const Grid *grid_;
420 HostEntityIterator hostEntityIterator_;
421 };
422
423 } // namespace GeoGrid
424
425} // namespace Dune
426
427#endif // #ifndef DUNE_GEOGRID_ITERATOR_HH
decltype(auto) apply(F &&f, ArgTuple &&args)
Apply function with arguments given as tuple.
Definition: apply.hh:58
PartitionIteratorType
Parameter to be used for the parallel level- and leaf iterators.
Definition: gridenums.hh:134
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:28
@ All_Partition
all entities
Definition: gridenums.hh:139
@ OverlapFront_Partition
interior, border, overlap and front entities
Definition: gridenums.hh:138
@ Interior_Partition
only interior entities
Definition: gridenums.hh:135
@ InteriorBorder_Partition
interior and border entities
Definition: gridenums.hh:136
@ Overlap_Partition
interior, border, and overlap entities
Definition: gridenums.hh:137
@ Ghost_Partition
only ghost entities
Definition: gridenums.hh:140
@ InteriorEntity
all interior entities
Definition: gridenums.hh:29
@ GhostEntity
ghost entities
Definition: gridenums.hh:33
@ BorderEntity
on boundary between interior and overlap
Definition: gridenums.hh:30
@ OverlapEntity
all entities lying in the overlap zone
Definition: gridenums.hh:31
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:435
Dune namespace.
Definition: alignedallocator.hh:10
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)