DUNE-FEM (unstable)

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