dune-grid  2.2.0
entity_inline.hh
Go to the documentation of this file.
1 #include <dune/common/exceptions.hh>
2 
3 #include "geometry.hh"
4 #include "grid.hh"
5 
6 namespace Dune {
7 
8  template<int cd, int dim, class GridImp>
10  reset( int l )
11  {
12  item_ = 0;
13  level_ = l;
14  twist_ = 0;
15  face_ = -1;
16  }
17 
18  template<int cd, int dim, class GridImp>
21  {
22  item_ = 0;
23  geo_.invalidate();
24  }
25 
26  template<int cd, int dim, class GridImp>
29  {
30  return (item_ == org.item_);
31  }
32 
33  template<int cd, int dim, class GridImp>
35  {
36  return gIndex_;
37  }
38 
39  template<int cd, int dim, class GridImp>
41  {
42  return level_;
43  }
44 
45  template<int cd, int dim, class GridImp>
48  {
49  return partitionType_;
50  }
51 
52  template<int cd, int dim, class GridImp>
53  inline GeometryType
55  {
56  return geo_.type();
57  }
58 
60  //
61  // --Entity0
62  // --Codim0Entity
63  //
65  template<int dim, class GridImp>
68  {
69  item_ = 0;
70  ghost_ = 0;
71  geo_.invalidate();
72  }
73 
74  template<int dim, class GridImp>
76  reset (int walkLevel )
77  {
78  item_ = 0;
79  ghost_ = 0;
80  level_ = -1;
81  isLeaf_ = false;
82 
83  // reset geometry information
84  geo_.invalidate();
85  }
86 
87  // works like assignment
88  template<int dim, class GridImp>
89  inline void
91  {
92  item_ = org.item_;
93  ghost_ = org.ghost_;
94  level_ = org.level_;
95  isLeaf_ = org.isLeaf_;
96 
97  // reset geometry information
98  geo_.invalidate();
99  }
100 
101  template<int dim, class GridImp>
102  inline void
105  {
106  if( ! key.isGhost() )
107  setElement( *key.interior() );
108  else
109  setGhost( *key.ghost() );
110  }
111 
112  template<int dim, class GridImp>
113  inline void
115  setElement(HElementType & element)
116  {
117  item_ = static_cast<IMPLElementType *> (&element);
118  assert( item_ );
119  // make sure this method is not called for ghosts
120  assert( ! item_->isGhost() );
121  ghost_ = 0;
122  level_ = (*item_).level();
123  isLeaf_ = ((*item_).down() == 0);
124 
125  // reset geometry information
126  geo_.invalidate();
127  }
128 
129  template<int dim, class GridImp>
130  inline void
132  {
133  // use element as ghost
134  item_ = static_cast<IMPLElementType *> ( ghost.getGhost().first );
135 
136  // method getGhost can return 0, but then is something wrong
137  assert(item_);
138  assert(item_->isGhost());
139 
140  level_ = item_->level();
141  // remember pointer to ghost face
142  ghost_ = static_cast<BNDFaceType *> (&ghost);
143  assert( ghost_ );
144 
145  BNDFaceType * dwn = static_cast<BNDFaceType *> (ghost.down());
146  if ( ! dwn ) isLeaf_ = true;
147  else
148  {
149  assert( ghost.level() == level_ );
150  if(dwn->ghostLevel() == level_)
151  isLeaf_ = true;
152  else
153  isLeaf_ = false;
154  }
155  // check wether ghost is leaf or not, ghost leaf means
156  // that this is the ghost that we want in the leaf iterator
157  // not necessarily is real leaf element
158  // see Intersection Iterator, same story
159 
160  // reset geometry information
161  geo_.invalidate();
162  }
163 
164  template<int dim, class GridImp>
165  inline int
167  {
168  return level_;
169  }
170 
171  template<int dim, class GridImp>
174  {
175  return (item_ == org.item_);
176  }
177 
178  template<int dim, class GridImp>
179  inline GeometryType
181  {
182  return geo_.type();
183  }
184 
185  template<int dim, class GridImp>
187  {
188  assert( item_ );
189  return (*item_).getIndex();
190  }
191 
192  template<int dim, class GridImp>
193  template<int cc>
195  {
196  return grid().referenceElement().size(cc);
197  }
198 
199  template<int dim, class GridImp>
202  {
203  assert( item_ );
204  // make sure we really got a ghost
205  assert( (isGhost()) ? item_->isGhost() : true );
206  return (isGhost() ? GhostEntity : InteriorEntity);
207  }
208 
209  template<int dim, class GridImp>
211  {
212  return isLeaf_;
213  }
214 
215  template<int dim, class GridImp>
218  {
219  assert(item_ != 0);
220  // if isGhost is true the end iterator will be returned
221  if( isGhost() )
222  {
223  return ALU3dGridHierarchicIterator<GridImp>(factory_,*ghost_,maxlevel, isLeaf() );
224  }
225  return ALU3dGridHierarchicIterator<GridImp>(factory_,*item_,maxlevel, isLeaf() );
226  }
227 
228  template<int dim, class GridImp>
230  {
231  assert(item_ != 0);
232  return ALU3dGridHierarchicIterator<GridImp> (factory_, *item_, maxlevel, true);
233  }
234 
235  template<int dim, class GridImp>
236  inline typename ALU3dGridEntity<0,dim,GridImp> :: ALU3dGridLeafIntersectionIteratorType
238  {
239  assert(item_ != 0);
240  return ALU3dGridIntersectionIteratorType (*this, this->level(), false );
241  }
242 
243  template<int dim, class GridImp>
244  inline typename ALU3dGridEntity<0,dim,GridImp> :: ALU3dGridLeafIntersectionIteratorType
246  {
247  assert(item_ != 0);
248  return ALU3dGridLeafIntersectionIteratorType (*this , this->level(), true);
249  }
250 
251  template<int dim, class GridImp>
252  inline typename ALU3dGridEntity<0,dim,GridImp> :: ALU3dGridLevelIntersectionIteratorType
254  {
255  assert(item_ != 0);
256  return ALU3dGridLevelIntersectionIteratorType (*this, this->level(), false );
257  }
258 
259  template<int dim, class GridImp>
260  inline typename ALU3dGridEntity<0,dim,GridImp> :: ALU3dGridLevelIntersectionIteratorType
262  {
263  assert(item_ != 0);
264  return ALU3dGridLevelIntersectionIteratorType (*this, this->level(),true);
265  }
266 
267  // Adaptation methods
268  template<int dim, class GridImp>
270  {
271  assert( item_ );
272  return item_->hasBeenRefined();
273  }
274 
275  template<int dim, class GridImp>
277  {
278  assert( item_ );
279  return ((*item_).requestrule() == coarse_element_t);
280  }
281 
282  //*******************************************************************
283  //
284  // --EntityPointer
285  // --EnPointer
286  //
287  //*******************************************************************
288  template<int codim, class GridImp >
291  const HElementType &item)
292  : factory_(factory)
293  , seed_( item )
294  , entity_( 0 )
295  , locked_ ( false ) // entity can be released
296  {
297  }
298 
299  template<int codim, class GridImp >
302  const HBndSegType & ghostFace )
303  : factory_(factory)
304  , seed_( ghostFace )
305  , entity_ ( factory_.template getNewEntity<codim> ( ghostFace.level() ))
306  , locked_( true ) // entity should not be released, otherwise is ghost info lost
307  {
308  // sets entity and item pointer
309  updateGhostPointer( const_cast<HBndSegType &> (ghostFace) );
310  }
311 
312  template<int codim, class GridImp >
315  const ALU3dGridEntitySeedType& key )
316  : factory_(factory)
317  , seed_( key )
318  , entity_ ( 0 )
319  , locked_( false )
320  {
321  }
322 
323  // constructor Level,Leaf and HierarchicIterator
324  template<int codim, class GridImp >
326  ALU3dGridEntityPointerBase(const FactoryType& factory, int level )
327  : factory_(factory)
328  , seed_()
329  , entity_ ( factory_.template getNewEntity<codim> ( level ) )
330  , locked_ ( false ) // entity can be released
331  {
332  // this needs to be called
333  // have to investigate why
334  entityImp().reset(level);
335  }
336 
337 
338  template<int codim, class GridImp >
341  const HElementType &item,
342  const int level,
343  const int twist,
344  const int duneFace )
345  : factory_(factory)
346  , seed_( item, level, twist, duneFace )
347  , entity_( 0 )
348  , locked_ ( false ) // entity can be released
349  {}
350 
351  template<int codim, class GridImp >
354  : factory_(org.factory_)
355  , seed_( org.seed_ )
356  , entity_( 0 )
357  , locked_( org.locked_ )
358  {
359  // if entity exists then copy entity
360  getEntity( org );
361  }
362 
363  template<int codim, class GridImp >
366  {
367  // if entity existed for original pointer then copy
368  if( org.entity_ )
369  {
370  assert( entity_ == 0 );
371  entity_ = factory_.template getNewEntity<codim> ();
372  // set entity right away
373  entityImp().setEntity( org.entityImp() );
374  }
375  }
376 
377  template<int codim, class GridImp >
381  {
382  clone( org );
383  return *this;
384  }
385 
386  template<int codim, class GridImp >
387  inline void
390  {
391  assert( &factory_ == &org.factory_ );
392 
393  // set item
394  seed_ = org.seed_;
395 
396  HElementType* item = seed_.item();
397  // copy locked info
398  locked_ = org.locked_;
399 
400  if( item )
401  {
402  // if no entity check org entity
403  // if no org entity then nothing is done
404  if( !entity_ )
405  {
406  getEntity(org);
407  }
408  else
409  {
410  // in case of ghost element use different set method
411  if( item->isGhost() )
412  {
413  // on ghosts entity pointers entity always exists
414  assert( org.entity_ );
415  entityImp().setEntity( org.entityImp() );
416  locked_ = true ;
417  }
418  else
419  {
420  // otherwise item is set
421  entityImp().setElement( seed_ );
422  }
423  }
424  }
425  else
426  {
427  this->done();
428  }
429  return ;
430  }
431 
432  template<int codim, class GridImp >
435  {
436  this->done();
437  }
438 
439  template<int codim, class GridImp >
441  {
442  seed_.clear();
443  locked_ = false;
444  // free entity
445  freeEntity();
446  }
447 
448  template<int codim, class GridImp >
450  {
451  // sets entity pointer in the status of an empty entity
452  if( entity_ )
453  {
454  entityImp().removeElement();
455  factory_.template freeEntity<codim> ( (EntityObject *) entity_ );
456  entity_ = 0;
457  }
458  }
459 
460  template<int codim, class GridImp >
463  {
464  // check equality of underlying items
465  return (seed_.equals( i.seed_ ));
466  }
467 
468  template<int codim, class GridImp >
471  {
472  // don't dereference empty entity pointer
473  assert( seed_.item() );
474  assert( (seed_.item()->isGhost()) ? locked_ :true );
475  assert( (locked_) ? (entity_ != 0) : true);
476  if( ! entity_ )
477  {
478  entity_ = factory_.template getNewEntity<codim> ();
479  entityImp().setElement( seed_ );
480  }
481  assert( seed_.item() == & entityImp().getItem() );
482  return (*entity_);
483  }
484 
485  template<int codim, class GridImp >
487  {
488  assert( seed_.item() );
489  return seed_.item()->level();
490  }
491 
492  template<int codim, class GridImp >
494  updateGhostPointer( HBndSegType & ghostFace )
495  {
496  assert( entity_ );
497  entityImp().setGhost( ghostFace );
498  // inside the method setGhost the method getGhost of the ghostFace is
499  // called and set as item
500  seed_.set( ghostFace );
501  }
502 
503  template<int codim, class GridImp >
505  updateEntityPointer( HElementType * item , int )
506  {
507  seed_.set( *item );
508  if( item && entity_ )
509  {
510  entityImp().setElement( seed_ );
511  }
512  }
513 
515  //
516  // specialisation for higher codims
517  //
519 
520  template<int codim, class GridImp >
523  const int level,
524  const HElementType &item,
525  const int twist,
526  const int duneFace )
527  : ALU3dGridEntityPointerBase<codim,GridImp> (factory,item,level,twist,duneFace)
528  {
529  }
530 
531  template<int codim, class GridImp >
534  : ALU3dGridEntityPointerBase<codim,GridImp>(org)
535  {
536  }
537 
538  template<int codim, class GridImp >
542  {
543  // clone pointer
544  clone(org);
545  return *this;
546  }
547 
548  template<int codim, class GridImp >
549  inline void
552  {
553  // copy key
554  seed_ = org.seed_;
555 
556  assert( &factory_ == &org.factory_ );
557  // copy lock status
558  this->locked_ = org.locked_;
559 
560  // if entity exists, just remove item pointer
561  if( seed_.item() )
562  {
563  if( ! entity_ )
564  getEntity(org);
565  else
566  entityImp().setElement( seed_ );
567  }
568  else
569  this->done();
570  return ;
571  }
572 
573  template<int codim, class GridImp >
576  {
577  // don't dereference empty entity pointer
578  assert( seed_.item() );
579  if( ! entity_ )
580  {
581  entity_ = factory_.template getNewEntity<codim> ();
582  entityImp().setElement( seed_ );
583  }
584  assert( seed_.item() == & entityImp().getItem() );
585  return (*entity_);
586  }
587 
588  template<int codim, class GridImp >
590  {
591  return seed_.level();
592  }
593 
594  template<int codim, class GridImp >
597  {
598  seed_.set( *item, level );
599  if( item && entity_ )
600  {
601  entityImp().setElement( seed_ );
602  }
603  }
604 
605 
606 } // end namespace Dune