dune-grid  2.4.1
geometrygrid/iterator.hh
Go to the documentation of this file.
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 
17 
18 namespace 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 >
41 
42  template< int codim, class 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 >
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 >
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 >
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 >
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 >
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:
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 
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 
251  {
252  return Dune::DefaultEntityPointer< Entity >( dereference() );
253  }
254 
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:
284 
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
317  = ReferenceElements< ctype, Traits::dimension >::general( hostElement.type() );
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 
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 
370  {
371  return Dune::DefaultEntityPointer< Entity >( dereference() );
372  }
373 
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:
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 
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 
441  {
443  }
444 
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
ReferenceElement< ctype, dimension > RefElement
Definition: geometrygrid/iterator.hh:52
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: geometrygrid/iterator.hh:100
Traits::template Codim< codimension >::Entity Entity
Definition: geometrygrid/iterator.hh:280
Definition: geometrygrid/entity.hh:57
ReferenceElement< ctype, dimension > RefElement
Definition: geometrygrid/iterator.hh:128
ReferenceElement< ctype, dimension > RefElement
Definition: geometrygrid/iterator.hh:98
Iterator(const Grid &grid, HostEntityIterator hostEntityIterator)
Definition: geometrygrid/iterator.hh:210
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: geometrygrid/iterator.hh:168
Traits::Grid Grid
Definition: geometrygrid/iterator.hh:276
ghost entities
Definition: gridenums.hh:33
bool equals(const Dune::DefaultEntityPointer< Entity > &rhs) const
Definition: geometrygrid/iterator.hh:255
Entity dereference() const
Definition: geometrygrid/iterator.hh:225
Entity dereference() const
Definition: geometrygrid/iterator.hh:427
Definition: common/entitypointer.hh:595
Definition: geometrygrid/iterator.hh:29
static void(*)(*)(*)(*)(*)(*) move(const double *)
Definition: partitiondisplay.cc:122
only ghost entities
Definition: gridenums.hh:140
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: geometrygrid/iterator.hh:97
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: geometrygrid/iterator.hh:78
Wrapper and interface class for a static iterator (EntityPointer)
only interior entities
Definition: gridenums.hh:135
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: geometrygrid/iterator.hh:51
bool equals(const Dune::DefaultEntityPointer< Entity > &rhs) const
Definition: geometrygrid/iterator.hh:445
on boundary between interior and overlap
Definition: gridenums.hh:30
Traits::Grid Grid
Definition: geometrygrid/iterator.hh:398
bool equals(const Iterator &rhs) const
Definition: geometrygrid/iterator.hh:220
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: geometrygrid/iterator.hh:81
Entity dereference() const
Definition: geometrygrid/iterator.hh:343
HierarchicIterator()
Definition: geometrygrid/iterator.hh:410
all interior entities
Definition: gridenums.hh:29
HierarchicIterator(const Grid &grid, HostEntityIterator hostEntityIterator)
Definition: geometrygrid/iterator.hh:412
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: geometrygrid/iterator.hh:54
bool equals(const Iterator &rhs) const
Definition: geometrygrid/iterator.hh:338
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:28
std::remove_const< Grid >::type::ctype ctype
Definition: geometrygrid/iterator.hh:126
static Iterator begin(const Grid &grid, const HostGridView &hostGridView)
Definition: geometrygrid/iterator.hh:356
ReferenceElement< ctype, dimension > RefElement
Definition: geometrygrid/iterator.hh:79
all entities
Definition: gridenums.hh:139
Traits::Grid Grid
Definition: geometrygrid/iterator.hh:194
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: geometrygrid/iterator.hh:149
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: geometrygrid/iterator.hh:165
const Grid & grid() const
Definition: geometrygrid/iterator.hh:232
static Iterator end(const Grid &grid, const HostGridView &hostGridView)
Definition: geometrygrid/iterator.hh:363
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: geometrygrid/iterator.hh:146
interior and border entities
Definition: gridenums.hh:136
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: geometrygrid/iterator.hh:127
const Grid & grid() const
Definition: geometrygrid/iterator.hh:434
ReferenceElement< ctype, dimension > RefElement
Definition: geometrygrid/iterator.hh:147
Grid abstract base classThis class is the base class for all grid implementations. Although no virtual functions are used we call it abstract since its methods do not contain an implementation but forward to the methods of the derived class via the Barton-Nackman trick.
Definition: common/grid.hh:388
Traits::template Codim< codimension >::Entity Entity
Definition: geometrygrid/iterator.hh:402
static Iterator begin(const Grid &grid, const HostGridView &hostGridView)
Definition: geometrygrid/iterator.hh:238
Iterator()
Definition: geometrygrid/iterator.hh:208
int level() const
Definition: geometrygrid/iterator.hh:432
std::remove_const< Grid >::type::ctype ctype
Definition: geometrygrid/iterator.hh:96
std::remove_const< Grid >::type::ctype ctype
Definition: geometrygrid/iterator.hh:164
all entities lying in the overlap zone
Definition: gridenums.hh:31
int level() const
Definition: geometrygrid/iterator.hh:230
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: geometrygrid/iterator.hh:130
void increment()
Definition: geometrygrid/iterator.hh:215
std::remove_const< Grid >::type::ctype ctype
Definition: geometrygrid/iterator.hh:145
interior, border, and overlap entities
Definition: gridenums.hh:137
void increment()
Definition: geometrygrid/iterator.hh:308
std::remove_const< Grid >::type::ctype ctype
Definition: geometrygrid/iterator.hh:50
static const int codimension
Definition: geometrygrid/iterator.hh:400
Iterator(const Grid &grid, HostElementIterator hostElementIterator, HostElementIterator hostEnd, const HostIndexSet &hostIndexSet)
Definition: geometrygrid/iterator.hh:294
Iterator()
Definition: geometrygrid/iterator.hh:292
bool equals(const HierarchicIterator &rhs) const
Definition: geometrygrid/iterator.hh:422
const Grid & grid() const
Definition: geometrygrid/iterator.hh:350
bool equals(const Dune::DefaultEntityPointer< Entity > &rhs) const
Definition: geometrygrid/iterator.hh:374
Definition: geometrygrid/iterator.hh:40
static Iterator end(const Grid &grid, const HostGridView &hostGridView)
Definition: geometrygrid/iterator.hh:244
PartitionIteratorType
Parameter to be used for the parallel level- and leaf iterators.
Definition: gridenums.hh:134
std::remove_const< Grid >::type::ctype ctype
Definition: geometrygrid/iterator.hh:77
ReferenceElement< ctype, dimension > RefElement
Definition: geometrygrid/iterator.hh:166
void increment()
Definition: geometrygrid/iterator.hh:417
DUNE-conform implementation of the entityThis class merely changes the template parameters of the ent...
Definition: geometrygrid/entity.hh:49
interior, border, overlap and front entities
Definition: gridenums.hh:138
int level() const
Definition: geometrygrid/iterator.hh:348
Traits::template Codim< codimension >::Entity Entity
Definition: geometrygrid/iterator.hh:198