Dune Core Modules (2.4.2)

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