dune-grid  2.2.1
alugrid/2d/intersection.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALU2DGRID_INTERSECTION_HH
2 #define DUNE_ALU2DGRID_INTERSECTION_HH
3 
4 // System includes
5 #include <stack>
6 #include <utility>
7 
8 // Dune includes
10 
14 
15 namespace Dune
16 {
17 
18  // Forward declarations
19  template<int cd, int dim, class GridImp>
20  class ALU2dGridEntity;
21  template<int cd, PartitionIteratorType pitype, class GridImp >
22  class ALU2dGridLevelIterator;
23  template<int cd, class GridImp >
24  class ALU2dGridEntityPointer;
25  template<int mydim, int coorddim, class GridImp>
26  class ALU2dGridGeometry;
27  template<class GridImp>
28  class ALU2dGridHierarchicIterator;
29  template<class GridImp>
30  class ALU2dGridIntersectionBase;
31  template<class GridImp>
32  class ALU2dGridLeafIntersectionIterator;
33  template<class GridImp>
34  class ALU2dGridLevelIntersectionIterator;
35  template<int codim, PartitionIteratorType pitype, class GridImp>
36  class ALU2dGridLeafIterator;
37  template< int dim, int dimworld, ALU2DSPACE ElementType eltype >
38  class ALU2dGrid;
39 
40 
41 
42  // ALU2DIntersectionGeometryStorage
43  // --------------------------------
44 
45  template< class LocalGeometryImpl >
47  {
49 
50  // one geometry for each face and twist 0 and 1
51  LocalGeometryImpl geoms_[ 2 ][ 4 ][ 2 ];
52  //std::vector< LocalGeometryImpl > geoms_[ 2 ][ 4 ];
53 
54  private:
56 
57  public:
58  // return reference to local geometry
59  const LocalGeometryImpl &localGeom ( const int aluFace, const int twist, const int corners ) const
60  {
61  assert( corners == 3 || corners == 4 );
62  assert( 0 <= aluFace && aluFace < corners );
63  assert( twist == 0 || twist == 1 );
64  return geoms_[ corners-3 ][ aluFace ][ twist ];
65  }
66 
67  // return static instance
68  static const ThisType &instance ()
69  {
70  static const ThisType geomStorage;
71  return geomStorage;
72  }
73  };
74 
75 
76 
77  //**********************************************************************
78  //
79  // --ALU2dGridIntersectionBase
80  // --IntersectionBase
81  //**********************************************************************
90  template<class GridImp>
91  class ALU2dGridIntersectionBase
92  {
93  static const int dim = GridImp::dimension;
94  static const int dimworld = GridImp::dimensionworld;
95  static const ALU2DSPACE ElementType eltype = GridImp::elementType;
96 
97  typedef typename GridImp::Traits::template Codim< 1 >::GeometryImpl GeometryImpl;
98  typedef typename GridImp::Traits::template Codim< 1 >::LocalGeometryImpl LocalGeometryImpl;
99 
100  public:
101  typedef typename GridImp :: GridObjectFactoryType FactoryType;
102 
106 
107  enum { dimension = GridImp::dimension };
108  enum { dimensionworld = GridImp::dimensionworld };
109 
110  typedef typename GridImp::template Codim<0>::Entity Entity;
111  typedef typename GridImp::template Codim<0>::EntityPointer EntityPointer;
112 
113  typedef typename GridImp::template Codim<1>::Geometry Geometry;
114  typedef typename GridImp::template Codim<1>::LocalGeometry LocalGeometry;
116  typedef FieldVector< alu2d_ctype, dimworld > NormalType;
117  typedef FieldVector< alu2d_ctype, dim-1 > LocalCoordinate;
118 
120 
124 
125  // type of local geometry storage
127 
129  friend class LevelIntersectionIteratorWrapper<GridImp>;
130  friend class LeafIntersectionIteratorWrapper<GridImp>;
131 
132  friend class IntersectionIteratorWrapper<GridImp,ThisType>;
133 
134  protected:
135  struct impl
136  {
137  explicit impl( HElementType *inside = 0 )
138  : index_(0), useOutside_(false)
139  {
140  setInside( inside );
141  setOutside( 0, -1 );
142  }
143 
145  {
146  return inside_;
147  }
148 
149  int nFaces () const
150  {
151  return nFaces_;
152  }
153 
154  bool isBoundary () const
155  {
156  assert( inside() && (index_ < nFaces()) && inside()->neighbour( index_ ) );
157  return inside()->neighbour( index_ )->thinis( ThinelementType::bndel_like );
158  }
159 
161  {
162  assert( isBoundary() );
163  return (HBndElType *)inside()->neighbour( index_ );
164  }
165 
167  {
168  return outside_;
169  }
170 
171  int opposite () const
172  {
173  return opposite_;
174  }
175 
177  {
178  inside_ = inside;
179  nFaces_ = (inside != 0 ? inside->numfaces() : 0);
180  }
181 
183  {
184  outside_ = outside;
185  opposite_ = opposite;
186  }
187 
188  private:
189  // current element from which we started the intersection iterator
190  HElementType *inside_;
191  HElementType *outside_;
192  int nFaces_;
193  int opposite_;
194 
195  public:
196  mutable int index_;
197  mutable bool useOutside_;
198  } current;
199 
200  public:
202  ALU2dGridIntersectionBase(const FactoryType& factory, int wLevel);
203 
205  ALU2dGridIntersectionBase(const ThisType & org);
206 
208 
210  void assign (const ThisType & org);
211 
212  const Intersection &dereference () const
213  {
214  return reinterpret_cast< const Intersection & >( *this );
215  }
216 
218  bool equals (const ThisType & i) const;
219 
221  int level () const;
222 
224  bool boundary() const;
225 
227  int boundaryId () const;
228 
230  size_t boundarySegmentIndex() const;
231 
233  bool neighbor () const;
234 
236  EntityPointer inside() const;
237 
239  EntityPointer outside() const;
240 
242  int indexInInside () const;
243 
245  int indexInOutside () const;
246 
247  int twistInInside () const;
248  int twistInOutside () const;
249 
250  // deprecated methods
251  int twistInSelf () const { return twistInInside(); }
252  // deprecated methods
253  int twistInNeighbor () const { return twistInOutside(); }
254 
255  NormalType outerNormal ( const LocalCoordinate &local ) const;
256  NormalType integrationOuterNormal ( const LocalCoordinate &local ) const;
257  NormalType unitOuterNormal ( const LocalCoordinate &local ) const;
258 
261  Geometry geometry () const;
262 
264  GeometryType type () const;
265 
266  protected:
267  const GridImp& grid() const { return factory_.grid(); }
268 
269  virtual bool conforming() const = 0;
270 
272  void checkValid () ;
273 
274  // set interator to end iterator
275  void done ( const HElementType *inside );
276  void done ( const EntityImp &en ) { done( &en.getItem() ); }
277 
278  // invalidate status of internal objects
279  void unsetUp2Date() ;
280 
281  // reset IntersectionIterator to first neighbour
282  void first ( const EntityImp &en, int wLevel );
283 
284  // reset IntersectionIterator to first neighbour
285  virtual void setFirstItem ( const HElementType &elem, int wLevel ) = 0;
286 
287  // the local geometries
288  mutable GeometryImpl intersectionGlobal_;
289  mutable LocalGeometryImpl intersectionSelfLocal_;
290  mutable LocalGeometryImpl intersectionNeighborLocal_;
291 
292  // reference to factory
295 
296  mutable int walkLevel_;
297  }; // end ALU2dGridIntersectionBase
298 
299 
300 
301 
302  //**********************************************************************
303  //
304  // --ALU2dGridLevelIntersectionIterator
305  // --IntersectionLevelIterator
306  //**********************************************************************
307 
308  template< class GridImp >
310  : public ALU2dGridIntersectionBase< GridImp >
311  {
313  typedef ALU2dGridIntersectionBase< GridImp > BaseType;
314 
315  static const int dim = GridImp::dimension;
316  static const int dimworld = GridImp::dimensionworld;
317  static const ALU2DSPACE ElementType eltype = GridImp::elementType;
318 
319  typedef typename ALU2dImplTraits< dimworld, eltype >::ThinelementType ThinelementType;
320  typedef typename BaseType::HElementType HElementType;
321  typedef typename ALU2dImplTraits< dimworld, eltype >::HBndElType HBndElType;
322  typedef typename ALU2dImplTraits< dimworld, eltype >::PeriodicBndElType PeriodicBndElType;
323 
324  friend class LevelIntersectionIteratorWrapper<GridImp>;
325 
326  friend class IntersectionIteratorWrapper<GridImp,ThisType>;
327 
328  typedef std::pair< HElementType *, int > IntersectionInfo;
329 
330  public:
331  typedef typename GridImp :: GridObjectFactoryType FactoryType;
333 
334  enum { dimension = GridImp::dimension };
335  enum { dimensionworld = GridImp::dimensionworld };
336 
337  typedef typename GridImp::template Codim<0>::Entity Entity;
338 
339  typedef typename GridImp::template Codim<1>::Geometry Geometry;
340  typedef typename GridImp::template Codim<1>::LocalGeometry LocalGeometry;
342  typedef ALU2dGridGeometry<dim-1,dimworld,GridImp> GeometryImp;
343  typedef ALU2dGridGeometry<dim-1,dim,GridImp> LocalGeometryImp;
344  typedef FieldVector<alu2d_ctype, dimworld> NormalType;
346 
348 
350  ALU2dGridLevelIntersectionIterator(const FactoryType& factory, int wLevel);
351 
353  ALU2dGridLevelIntersectionIterator(const FactoryType& factory, const HElementType* el, int wLevel, bool end=true);
354 
357 
359 
361  void increment ();
362 
363  public:
366  bool conforming () const
367  {
368  return (this->grid().nonConform() || isConform());
369  }
370 
371  protected:
372  bool isConform() const
373  {
374  return (!current.outside() || (current.outside() == current.inside()->neighbour( current.index_ )));
375  }
376 
377  private:
378  void doIncrement ();
379 
380  // reset IntersectionIterator to first neighbour
381  void setFirstItem(const HElementType & elem, int wLevel);
382 
383  // reset IntersectionIterator to first neighbour
384  template <class EntityType>
385  void first(const EntityType & en, int wLevel);
386 
387  void addNeighboursToStack();
388 
389  static int getOppositeInFather ( int nrInChild, int nrOfChild );
390  static int getOppositeInChild ( int nrInFather, int nrOfChild );
391 
392  void setupIntersection ();
393 
394  protected:
395  using BaseType::done;
396 
397  using BaseType::current;
398  using BaseType::walkLevel_;
399 
400  private:
401  mutable std::stack<IntersectionInfo> nbStack_;
402  }; // end ALU2dGridLevelIntersectionIterator
403 
404 
405 
406  //********************************************************************
407  //
408  // --ALU2dGridLeafIntersectionIterator
409  //
410  //
411  //********************************************************************
412 
413  template< class GridImp >
414  class ALU2dGridLeafIntersectionIterator
415  : public ALU2dGridIntersectionBase< GridImp >
416  {
417  typedef ALU2dGridLeafIntersectionIterator< GridImp > ThisType;
418  typedef ALU2dGridIntersectionBase< GridImp > BaseType;
419 
420  friend class LeafIntersectionIteratorWrapper<GridImp>;
421  friend class IntersectionIteratorWrapper<GridImp,ThisType>;
422 
423  static const int dim = GridImp::dimension;
424  static const int dimworld = GridImp::dimensionworld;
425  static const ALU2DSPACE ElementType eltype = GridImp::elementType;
426 
427  public:
428  typedef typename GridImp :: GridObjectFactoryType FactoryType;
430 
431  enum { dimension = GridImp::dimension };
432  enum { dimensionworld = GridImp::dimensionworld };
433 
434  private:
435  typedef typename ALU2dImplTraits< dimworld, eltype >::ThinelementType ThinelementType;
436  typedef typename BaseType::HElementType HElementType;
437  typedef typename ALU2dImplTraits< dimworld, eltype >::HBndElType HBndElType;
438  typedef typename ALU2dImplTraits< dimworld, eltype >::PeriodicBndElType PeriodicBndElType;
439 
440  typedef std::pair< HElementType *, int > IntersectionInfo;
441 
442  public:
443  typedef typename GridImp::template Codim<0>::Entity Entity;
444  typedef typename GridImp::template Codim<1>::Geometry Geometry;
445  typedef typename GridImp::template Codim<1>::LocalGeometry LocalGeometry;
447  typedef ALU2dGridGeometry<dim-1,dimworld,GridImp> GeometryImp;
448  typedef ALU2dGridGeometry<dim-1,dim,GridImp> LocalGeometryImp;
449  typedef FieldVector<alu2d_ctype, dimworld> NormalType;
451 
453 
455  ALU2dGridLeafIntersectionIterator(const FactoryType& factory, int wLevel);
456 
458  ALU2dGridLeafIntersectionIterator(const FactoryType& factory, const HElementType* el, int wLevel, bool end=true);
459 
462 
464 
466  void increment ();
467 
468  public:
470  bool conforming () const
471  {
472  return (!this->grid().nonConform() || isConform());
473  }
474 
475  protected:
476  bool isConform() const
477  {
478  return (!current.outside() || (current.outside()->level() == current.inside()->level()) );
479  }
480 
481  private:
482  void doIncrement ();
483  // reset IntersectionIterator to first neighbour
484  void setFirstItem(const HElementType & elem, int wLevel);
485 
486  // reset IntersectionIterator to first neighbour
487  template <class EntityType>
488  void first(const EntityType & en, int wLevel);
489 
490  void setupIntersection ();
491 
492  protected:
493  using BaseType::done;
494 
495  using BaseType::current;
496  using BaseType::walkLevel_;
497 
498  private:
499  std::stack<IntersectionInfo> nbStack_;
500 
501  }; // end ALU2dGridLeafIntersectionIterator
502 
503 } // end namespace Dune
504 
505 #include "intersection_imp.cc"
506 #if COMPILE_ALU2DGRID_INLINE
507  #include "intersection.cc"
508 #endif
509 
510 #endif // #ifndef DUNE_ALU2DGRID_INTERSECTION_HH