dune-grid  2.2.1
alugrid/3d/entityseed.hh
Go to the documentation of this file.
1 #ifndef ALU3DGRID_ENTITYKEY_HH
2 #define ALU3DGRID_ENTITYKEY_HH
3 
4 namespace Dune
5 {
6 
7 template<int cd, class GridImp>
8 class ALU3dGridEntitySeed ;
9 
10 //**********************************************************************
11 //
12 // --ALU3dGridEntitySeed
13 // --EntitySeed
14 //**********************************************************************
15 template< int codim, class GridImp >
17 {
18 protected:
20  enum { dim = GridImp::dimension };
21  enum { dimworld = GridImp::dimensionworld };
22 
23 
24  typedef typename GridImp::MPICommunicatorType Comm;
25 
26  friend class ALU3dGridEntity<codim,dim,GridImp>;
27  friend class ALU3dGridEntity< 0,dim,GridImp>;
28  friend class ALU3dGrid < GridImp::elementType, Comm >;
29 
30  typedef ALU3dImplTraits<GridImp::elementType, Comm > ImplTraits;
31  typedef typename ImplTraits::template Codim<codim>::ImplementationType ImplementationType;
32  typedef typename ImplTraits::template Codim<codim>::InterfaceType HElementType;
33  typedef typename ImplTraits::template Codim<codim>::EntitySeedType KeyType ;
34 
35  typedef typename ImplTraits::BNDFaceType BNDFaceType;
36  typedef typename ImplTraits::HBndSegType HBndSegType;
37 
38  template <int cd, class Key>
39  struct Bnd
40  {
41  static Key* toKey(const HBndSegType*)
42  {
43  return (Key*) 0;
44  }
45  static HElementType* getItem(KeyType* key)
46  {
47  return static_cast< HElementType* > ( key );
48  }
49  static bool isGhost(KeyType*) { return false; }
50  static BNDFaceType* ghost( KeyType* ) { return ( BNDFaceType* ) 0; }
51  };
52  template <class Key>
53  struct Bnd<0, Key>
54  {
55  static Key* toKey(const HBndSegType* ghostFace)
56  {
57  return static_cast< KeyType* > (const_cast< BNDFaceType* >( static_cast<const BNDFaceType*> (ghostFace)));
58  }
59  static HElementType* getItem(KeyType* key)
60  {
61  if( key )
62  {
63  if( key->isboundary() )
64  {
65  return ((static_cast< BNDFaceType* > ( key ))->getGhost().first);
66  }
67  else
68  {
69  // we cannot cast to HElement here, since only the implementation is derived
70  // from hasFace
71  return static_cast< HElementType * > (static_cast< ImplementationType* > (key));
72  }
73  }
74  else
75  return static_cast< HElementType * > (0) ;
76  }
77  static bool isGhost(KeyType* key) { assert( key ); return key->isboundary(); }
78  static BNDFaceType* ghost( KeyType* key ) { assert( key ); return (static_cast< BNDFaceType* > ( key )); }
79  };
80 public:
81  static const int defaultValue = -1 ;
82  static const int defaultTwist = 0 ;
83 
84  enum { codimension = codim };
85 
87  typedef typename GridImp::template Codim<codimension>::Entity Entity;
91 
94 
97 
100  {
101 #ifndef NDEBUG
102  // clear pointer
103  clear();
104 #endif
105  }
106 
109 
112 
114  ALU3dGridEntitySeedBase(const HElementType* item, const HBndSegType* ghostFace );
115 
117  ALU3dGridEntitySeedBase(const HBndSegType& ghostFace );
118 
120  //
121  // interface methods
122  //
126 
129  {
130  return equals( i );
131  }
132 
135  {
136  return ! equals( i );
137  }
138 
140  ThisType & operator = (const ThisType & org);
141 
143  //
144  // non-interface methods
145  //
148  bool equals (const ALU3dGridEntitySeedType& i) const;
149 
151  void clear()
152  {
153  item_ = 0;
154  }
155 
158 
161  {
162  assert( ! isGhost() );
163  return static_cast< HElementType * > (static_cast< ImplementationType* > (item_));
164  }
165 
167  bool isGhost() const { return Bnd<codim,KeyType>::isGhost( item_ ); }
168  BNDFaceType* ghost() const
169  {
170  assert( isGhost() );
171  return Bnd<codim,KeyType>::ghost( item_ );
172  }
173 
175  {
176  return static_cast< KeyType* > (const_cast< ImplementationType* > (static_cast<const ImplementationType* > (item)));
177  }
178 
179  void set(const HElementType& item)
180  {
181  item_ = toKey( &item );
182  }
183 
184  KeyType* toKey( const HBndSegType* ghostFace )
185  {
186  return Bnd<codim,KeyType>::toKey( ghostFace );
187  }
188 
189  void set(const HBndSegType& ghostFace)
190  {
191  item_ = toKey( &ghostFace );
192  }
193 
194  int level () const { return defaultValue; }
195  int twist () const { return defaultTwist; }
196  int face () const { return defaultValue; }
197 
198 protected:
199  // pointer to item
200  mutable KeyType* item_;
201 };
202 
203 template<int cd, class GridImp>
205 public ALU3dGridEntitySeedBase<cd,GridImp>
206 {
208 
210  enum { dim = GridImp::dimension };
211  enum { dimworld = GridImp::dimensionworld };
212 
213  typedef typename GridImp::MPICommunicatorType Comm;
214 
215  friend class ALU3dGridEntity<cd,dim,GridImp>;
216  friend class ALU3dGridEntity< 0,dim,GridImp>;
217  friend class ALU3dGrid < GridImp::elementType, Comm >;
218 
219  typedef ALU3dImplTraits< GridImp::elementType, Comm > ImplTraits;
220  typedef typename ImplTraits::template Codim<cd>::ImplementationType ImplementationType;
221  typedef typename ImplTraits::template Codim<cd>::InterfaceType HElementType;
222 
223  typedef typename ImplTraits::BNDFaceType BNDFaceType;
224  typedef ALU3dGridEntity<cd,dim,GridImp> ALU3dGridEntityType;
225 
226 public:
227  using BaseType :: defaultValue ;
228  using BaseType :: defaultTwist ;
229 
231  typedef typename GridImp::template Codim<cd>::Entity Entity;
232 
235 
237  ALU3dGridEntitySeed(const ImplementationType & item)
238  {
239  // this constructor should only be called by codim=0 entity keys
240  assert( false );
241  abort();
242  }
243 
245  ALU3dGridEntitySeed(const HElementType & item,
246  const int level,
247  const int twist = defaultTwist,
248  const int duneFace = defaultValue
249  );
250 
254 
257  : ALU3dGridEntitySeedBase<cd,GridImp> (entity.getItem()),
259  {}
260 
263 
265  ThisType & operator = (const ThisType & org);
266 
268  void clear();
269 
271  void set(const HElementType & item, const int level )
272  {
274  level_ = level ;
275  }
276 
278  int level () const { return level_ ; }
280  int twist () const { return twist_ ; }
282  int face () const { return face_ ; }
283 
284  using BaseType :: set ;
285 
287  {
288  return equals( i );
289  }
290 
292  {
293  return ! equals( i );
294  }
295 
297  bool equals (const ALU3dGridEntitySeedType& key) const
298  {
299  // only compare the item pointer, this is the real key
300  return BaseType :: equals( key ) && (level() == key.level());
301  }
302 
303 protected:
304  // level of entity
305  int level_;
306  // twist of face, for codim 1 only
307  int twist_;
308  // face number, for codim 1 only
309  int face_;
310 };
311 
315 template<class GridImp>
316 class ALU3dGridEntitySeed<0,GridImp> :
317 public ALU3dGridEntitySeedBase<0,GridImp>
318 {
319 protected:
321 
322  enum { cd = 0 };
324  enum { dim = GridImp::dimension };
325  enum { dimworld = GridImp::dimensionworld };
326 
327  typedef typename GridImp::MPICommunicatorType Comm;
328 
329  friend class ALU3dGridEntity<cd,dim,GridImp>;
330  friend class ALU3dGridEntity< 0,dim,GridImp>;
331  friend class ALU3dGrid < GridImp::elementType, Comm >;
332 
333  typedef ALU3dImplTraits<GridImp::elementType, Comm > ImplTraits;
334  typedef typename ImplTraits::template Codim<cd>::ImplementationType ImplementationType;
335  typedef typename ImplTraits::template Codim<cd>::InterfaceType HElementType;
336 
337  typedef typename ImplTraits::BNDFaceType BNDFaceType;
338  typedef typename ImplTraits::HBndSegType HBndSegType;
339 
340  typedef ALU3dGridEntity< 0,dim,GridImp> ALU3dGridEntityType ;
341 
342 public:
343  using BaseType :: defaultValue ;
344  using BaseType :: defaultTwist ;
345 
347  typedef typename GridImp::template Codim<cd>::Entity Entity;
348 
351 
354 
357  : ALU3dGridEntitySeedBase<cd,GridImp> (item) {}
358 
360  ALU3dGridEntitySeed(const HElementType& item, int , int , int )
361  : ALU3dGridEntitySeedBase<cd,GridImp> (item) {}
362 
364  ALU3dGridEntitySeed(const HBndSegType& ghostFace )
365  : ALU3dGridEntitySeedBase<cd,GridImp> ( ghostFace ) {}
366 
369  : ALU3dGridEntitySeedBase<cd,GridImp> (org)
370  {
371  }
372 };
373 
374 
376 template <int cd, class GridImp>
377 inline std :: ostream &operator<< ( std :: ostream &out,
379 {
380  out << key.item() << " " << key.level() << " " << key.twist() << " " << key.face();
381  return out;
382 }
383 
384 
385 //*******************************************************************
386 //
387 // Implementation
388 //
389 //*******************************************************************
390 template<int codim, class GridImp >
393  : item_( 0 )
394 {
395 }
396 
397 template<int codim, class GridImp >
400  : item_( toKey(&item) )
401 {
402 }
403 
404 template<int codim, class GridImp >
407  : item_( toKey(&ghostFace) )
408 {
409 }
410 
411 template<int codim, class GridImp >
414  : item_(org.item_)
415 {
416 }
417 
418 template<int codim, class GridImp >
422 {
423  item_ = org.item_;
424  return *this;
425 }
426 
427 template<int codim, class GridImp >
430 {
431  // check equality of underlying items
432  return (item_ == i.item_);
433 }
434 
436 //
437 // specialisation for higher codims
438 //
440 
441 template<int codim, class GridImp >
443 ALU3dGridEntitySeed(const HElementType &item,
444  const int level,
445  const int twist,
446  const int duneFace )
447  : ALU3dGridEntitySeedBase<codim,GridImp> (item)
448  , level_(level)
449  , twist_ (twist)
450  , face_(duneFace) // duneFace can be -1 when face was created by Face Iterator
451 {
452 }
453 
454 template<int codim, class GridImp >
457  : ALU3dGridEntitySeedBase<codim,GridImp>(org)
458  , level_(org.level_)
459  , twist_(org.twist_)
460  , face_(org.face_)
461 {
462 }
463 
464 template<int codim, class GridImp >
468 {
469  // docu and cleanup
470  BaseType :: operator = ( org );
471 
472  // clone other stuff
473  level_ = org.level_;
474  twist_ = org.twist_;
475  face_ = org.face_;
476  return *this;
477 }
478 
479 template<int codim, class GridImp >
480 inline void
482 {
483  BaseType :: clear();
484  level_ = defaultValue ;
485  twist_ = defaultTwist ;
486  face_ = defaultValue ;
487 }
488 
489 } // end namespace Dune
490 #endif