dune-grid  2.11
iterator.hh
Go to the documentation of this file.
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 
18 
19 namespace 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 >
42 
43  template< int codim, class 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 >
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 >
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 >
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 >
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 >
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:
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 
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:
275 
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 
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:
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 
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
ghost entities
Definition: gridenums.hh:35
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:30
concept Entity
Model of a grid entity.
Definition: concepts/entity.hh:119
Traits::template Codim< codimension >::Entity Entity
Definition: iterator.hh:271
Traits::Grid Grid
Definition: iterator.hh:378
HierarchicIterator(const Grid &grid, HostEntityIterator hostEntityIterator)
Definition: iterator.hh:392
ReferenceElements< ctype, dimension >::ReferenceElement RefElement
Definition: iterator.hh:148
int level() const
Definition: iterator.hh:412
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: iterator.hh:147
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: iterator.hh:52
on boundary between interior and overlap
Definition: gridenums.hh:32
interior, border, overlap and front entities
Definition: gridenums.hh:140
const Grid & grid() const
Definition: iterator.hh:340
ReferenceElements< ctype, dimension >::ReferenceElement RefElement
Definition: iterator.hh:167
only interior entities
Definition: gridenums.hh:137
ReferenceElements< ctype, dimension >::ReferenceElement RefElement
Definition: iterator.hh:53
ReferenceElements< ctype, dimension >::ReferenceElement RefElement
Definition: iterator.hh:129
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: iterator.hh:101
std::remove_const< Grid >::type::ctype ctype
Definition: iterator.hh:78
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:375
void increment()
Definition: iterator.hh:397
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: iterator.hh:166
static Iterator begin(const Grid &grid, const HostGridView &hostGridView)
Definition: iterator.hh:239
all entities
Definition: gridenums.hh:141
Entity dereference() const
Definition: iterator.hh:226
Entity dereference() const
Definition: iterator.hh:333
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: iterator.hh:55
all interior entities
Definition: gridenums.hh:31
only ghost entities
Definition: gridenums.hh:142
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: iterator.hh:150
STL namespace.
std::remove_const< Grid >::type::ctype ctype
Definition: iterator.hh:165
Traits::template Codim< codimension >::Entity Entity
Definition: iterator.hh:382
bool equals(const HierarchicIterator &rhs) const
Definition: iterator.hh:402
Definition: geometrygrid/entity.hh:57
static Iterator begin(const Grid &grid, const HostGridView &hostGridView)
Definition: iterator.hh:346
ReferenceElements< ctype, dimension >::ReferenceElement RefElement
Definition: iterator.hh:99
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: iterator.hh:131
all entities lying in the overlap zone
Definition: gridenums.hh:33
const Grid & grid() const
Definition: iterator.hh:233
Definition: iterator.hh:41
std::remove_const< Grid >::type::ctype ctype
Definition: iterator.hh:51
Include standard header files.
Definition: agrid.hh:59
concept ReferenceElement
Definition: concepts/geometry.hh:18
PartitionIteratorType
Parameter to be used for the parallel level- and leaf iterators.
Definition: gridenums.hh:136
static Iterator end(const Grid &grid, const HostGridView &hostGridView)
Definition: iterator.hh:353
HierarchicIterator()
Definition: iterator.hh:390
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: iterator.hh:98
Entity dereference() const
Definition: iterator.hh:407
Iterator(const Grid &grid, HostElementIterator hostElementIterator, HostElementIterator hostEnd, const HostIndexSet &hostIndexSet)
Definition: iterator.hh:285
interior and border entities
Definition: gridenums.hh:138
concept Grid
Requirements for implementations of the Dune::Grid interface.The Grid concept defines interface requi...
Definition: concepts/grid.hh:109
bool equals(const Iterator &rhs) const
Definition: iterator.hh:328
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: iterator.hh:128
ReferenceElements< ctype, dimension >::ReferenceElement RefElement
Definition: iterator.hh:80
bool equals(const Iterator &rhs) const
Definition: iterator.hh:221
std::remove_const< Grid >::type::ctype ctype
Definition: iterator.hh:97
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: iterator.hh:82
std::remove_const< Grid >::type::ctype ctype
Definition: iterator.hh:146
interior, border, and overlap entities
Definition: gridenums.hh:139
DUNE-conform implementation of the entityThis class merely changes the template parameters of the ent...
Definition: geometrygrid/entity.hh:49
static const int codimension
Definition: iterator.hh:380
static Iterator end(const Grid &grid, const HostGridView &hostGridView)
Definition: iterator.hh:245
Definition: iterator.hh:30
std::remove_const< Grid >::type::Traits::template Codim< 0 >::Entity Element
Definition: iterator.hh:79
Iterator(const Grid &grid, HostEntityIterator hostEntityIterator)
Definition: iterator.hh:211
std::remove_const< Grid >::type::ctype ctype
Definition: iterator.hh:127
concept IndexSet
Model of an index set.
Definition: concepts/indexidset.hh:55
Traits::template Codim< codimension >::Entity Entity
Definition: iterator.hh:199
const Grid & grid() const
Definition: iterator.hh:414
static bool apply(const RefElement &refElement, const Element &element, int subEntity)
Definition: iterator.hh:169