dune-grid  2.2.1
alugrid/3d/datahandle.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALU3DGRIDDATAHANDLE_HH
2 #define DUNE_ALU3DGRIDDATAHANDLE_HH
3 
4 //- system includes
5 #include <iostream>
6 
9 
10 //- local includes
11 #include "alu3dinclude.hh"
12 
13 using std::endl;
14 using std::cout;
15 using std::flush;
16 
17 namespace ALUGridSpace
18 {
19 
21 template <class GridType, class DataCollectorType, int codim >
23 : public GatherScatter
24 {
25 protected:
26  const GridType & grid_;
27  typedef typename GridType::template Codim<codim>::Entity EntityType;
29  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
31 
32  typedef typename GridType::MPICommunicatorType Comm;
33 
34  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
35  typedef typename ImplTraits::template Codim< codim >::ImplementationType ImplElementType;
36  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
37 
40 
41  DataCollectorType & dc_;
42 
43  const bool variableSize_;
44 
45  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
46 
47  typedef typename DataCollectorType:: DataType DataType;
48 public:
50  GatherScatterBaseImpl(const GridType & grid, MakeableEntityType & en,
51  RealEntityType & realEntity , DataCollectorType & dc)
52  : grid_(grid), entity_(en), realEntity_(realEntity) , dc_(dc)
53  , variableSize_( ! dc_.fixedsize(EntityType::dimension,codim) )
54  {
55  }
56 
58  bool contains(int dim, int cd) const { return dc_.contains(dim,cd); }
59 
60  // returns true, if element is contained in set of comm interface
61  // this method must be overlaoded by the impl classes
62  virtual bool containsItem (const HElementType & elem) const = 0;
63 
64  // set elem to realEntity
65  virtual void setElement(const HElementType & elem) = 0;
66 
67  void setData ( ObjectStreamType & str , HElementType & elem )
68  {
69  // one of this should be either true
70  assert( this->containsItem( elem ) || elem.isGhost() );
71 
72  // set element and then start
73  setElement(elem);
74 
75  // make sure partition type is set correct
76  assert( elem.isGhost() == (entity_.partitionType() == Dune :: GhostEntity) );
77 
78  size_t size = getSize(str, entity_);
79  // use normal scatter method
80  dc_.scatter(str,entity_, size );
81  }
82 
84  void sendData ( ObjectStreamType & str , HElementType & elem )
85  {
86  // make sure element is contained in communication interface
87  //assert( this->containsItem( elem ) );
88  setElement(elem);
89 
90  // if varaible size, also send size
91  if( variableSize_ )
92  {
93  size_t size = dc_.size( entity_ );
94  str.write( size );
95  }
96 
97  dc_.gather(str, entity_ );
98  }
99 
101  void recvData ( ObjectStreamType & str , HElementType & elem )
102  {
103  assert( this->containsItem( elem ) );
104  setElement( elem );
105 
106  size_t size = getSize(str, entity_);
107  dc_.scatter(str,entity_, size );
108  }
109 
110 protected:
111  size_t getSize(ObjectStreamType & str, EntityType & en)
112  {
113  if(variableSize_)
114  {
115  size_t size;
116  str.read(size);
117  return size;
118  }
119  else
120  return dc_.size(en);
121  }
122 };
123 
124 //***********************************************************
125 //
126 // --specialisation for codim 0
127 //
128 //***********************************************************
129 
131 template <class GridType, class DataCollectorType >
132 class GatherScatterBaseImpl<GridType,DataCollectorType,0> : public GatherScatter
133 {
134 protected:
135  enum { codim = 0 };
136  const GridType & grid_;
137  typedef typename GridType::template Codim<0>::Entity EntityType;
139  typename GridType::template Codim<0>::Entity> MakeableEntityType;
141 
142  typedef typename GridType::MPICommunicatorType Comm;
143 
144  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
145  typedef typename ImplTraits::template Codim< codim >::ImplementationType ImplElementType;
146  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
147 
148  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
149 
150  typedef typename ImplTraits::template Codim< codim >::GhostInterfaceType HGhostType;
151  typedef typename ImplTraits::template Codim< codim >::GhostImplementationType ImplGhostType;
152 
153  typedef typename ImplTraits::PllElementType PllElementType;
154 
157 
158  // data handle
159  DataCollectorType & dc_;
160 
161  const bool variableSize_;
162 
163  // used MessageBuffer
164  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
165 
166 public:
167  // use all other containsItem from the base class
169 
171  GatherScatterBaseImpl(const GridType & grid, MakeableEntityType & en,
172  RealEntityType & realEntity , DataCollectorType & dc)
173  : grid_(grid), entity_(en), realEntity_(realEntity)
174  , dc_(dc) , variableSize_ ( ! dc_.fixedsize( EntityType :: dimension, codim ))
175  {}
176 
177  // return true if dim,codim combination is contained in data set
178  bool contains(int dim, int codim) const
179  {
180  return dc_.contains(dim,codim);
181  }
182 
183  // return true if item might from entity belonging to data set
184  virtual bool containsItem (const HElementType & elem) const
185  {
186  return elem.isLeafEntity();
187  }
188 
189  // return true if item might from entity belonging to data set
190  virtual bool containsItem (const HGhostType & ghost) const = 0;
191 
193  void sendData ( ObjectStreamType & str , const HElementType & elem )
194  {
195  assert( this->containsItem(elem) );
196  realEntity_.setElement( const_cast<HElementType &> (elem) );
197 
198  // write size in case of variable size
199  writeSize( str, entity_);
200  // gather data
201  dc_.gather(str, entity_);
202  }
203 
205  void sendData ( ObjectStreamType & str , const HGhostType& ghost)
206  {
207  assert( this->containsItem( ghost ) );
208 
209  // set ghost as entity
210  realEntity_.setGhost( const_cast <HGhostType &> (ghost) );
211 
212  // write size in case of variable size
213  writeSize( str, entity_);
214  // gather data
215  dc_.gather(str, entity_);
216  }
217 
219  void recvData ( ObjectStreamType & str , HElementType & elem )
220  {
221  // assert( this->containsItem( elem ) );
222  realEntity_.setElement( elem );
223 
224  size_t size = getSize(str, entity_);
225  dc_.scatter(str, entity_, size);
226  }
227 
229  void recvData ( ObjectStreamType & str , HGhostType & ghost )
230  {
231  assert( this->containsItem( ghost ) );
232 
233  // set ghost as entity
234  realEntity_.setGhost( ghost );
235 
236  size_t size = getSize(str , entity_ );
237  dc_.scatter(str, entity_, size );
238  }
239 
240 protected:
241  size_t getSize(ObjectStreamType & str, EntityType & en)
242  {
243  if(variableSize_)
244  {
245  size_t size;
246  str.read(size);
247  return size;
248  }
249  else
250  return dc_.size(en);
251  }
252 
253  // write variable size to stream
254  void writeSize(ObjectStreamType & str, EntityType & en)
255  {
256  if( variableSize_ )
257  {
258  size_t size = dc_.size( en );
259  str.write( size );
260  }
261  }
262 };
263 
264 #if ALU3DGRID_PARALLEL
265 
266 template< class GridType, class DataCollectorType, int codim >
267 class GatherScatterLeafData
268 : public GatherScatterBaseImpl< GridType, DataCollectorType, codim >
269 {
270  enum { dim = GridType :: dimension };
271 
272  typedef GatherScatterBaseImpl<GridType,DataCollectorType,codim> BaseType;
273  typedef typename GridType::template Codim<codim>::Entity EntityType;
275  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
276  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
277 
278  typedef typename GridType::MPICommunicatorType Comm;
279 
280  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
281  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
282  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
283 
284  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
285 
286  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
287  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
288 
289  typedef typename ImplTraits::PllElementType PllElementType;
290 
291 public:
292  // use all other containsItem methods from the base class
293  using BaseType :: containsItem ;
294 
296  GatherScatterLeafData(const GridType & grid, MakeableEntityType & en,
297  RealEntityType & realEntity , DataCollectorType & dc)
298  : BaseType(grid,en,realEntity,dc)
299  {
300  // if leaf vertices are communicated,
301  // make sure that vertex list is up2date
302  // but only do this, if vertex data contained,
303  // because the list update is expensive
304  if( (codim == 3) && dc.contains(dim,codim) )
305  {
306  // call of this method forces update of list,
307  // if list is not up to date
308  grid.getLeafVertexList();
309  }
310  }
311 
312  // returns true, if element is contained in set of comm interface
313  bool containsItem (const HElementType & elem) const
314  {
315  return elem.isLeafEntity();
316  }
317 
318  // returns true, if element is contained in set of comm interface
319  bool containsItem (const HGhostType & ghost) const
320  {
321  return ghost.isLeafEntity();
322  }
323 
324  // returns true, if interior element is contained in set of comm interface
325  bool containsInterior (const HFaceType & face, PllElementType & pll) const
326  {
327  return face.isInteriorLeaf();
328  }
329 
330  // returns true, if ghost is contianed in set of comm interface
331  bool containsGhost (const HFaceType & face , PllElementType & pll) const
332  {
333  return pll.ghostLeaf();
334  }
335 
336  // set elem to realEntity
337  void setElement(const HElementType & elem)
338  {
339  this->realEntity_.setElement(elem);
340  }
341 };
342 
344 template <class GridType, class DataCollectorType , int codim >
345 class GatherScatterLevelData
346 : public GatherScatterBaseImpl<GridType,DataCollectorType,codim>
347 {
348  typedef GatherScatterBaseImpl<GridType,DataCollectorType,codim> BaseType;
349  typedef typename GridType::template Codim<codim>::Entity EntityType;
351  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
352  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
353 
354  typedef typename GridType::MPICommunicatorType Comm;
355 
356  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
357  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
358  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
359 
360  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
361 
362  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
363  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
364 
365  typedef typename ImplTraits::PllElementType PllElementType;
366 
367  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
368 
369  const LevelIndexSetImp & levelSet_;
370  const int level_;
371 public:
372  // use containsItem for ghost element from BaseType
373  using BaseType :: containsItem ;
374 
376  GatherScatterLevelData(const GridType & grid, MakeableEntityType & en,
377  RealEntityType & realEntity , DataCollectorType & dc,
378  const LevelIndexSetImp & levelSet, const int level)
379  : BaseType(grid,en,realEntity,dc) , levelSet_(levelSet) , level_(level)
380  {
381  }
382 
383  // returns true, if element is contained in set of comm interface
384  bool containsItem (const HElementType & elem) const
385  {
386  return levelSet_.containsIndex(codim, elem.getIndex() );
387  }
388 
389  // set elem to realEntity
390  void setElement(const HElementType & elem)
391  {
392  this->realEntity_.setElement(elem,level_);
393  }
394 
395 };
396 
397 
399 template <class GridType, class DataCollectorType>
400 class GatherScatterLevelData<GridType,DataCollectorType,0>
401 : public GatherScatterBaseImpl<GridType,DataCollectorType,0>
402 {
403  enum { codim = 0 };
404  typedef GatherScatterBaseImpl<GridType,DataCollectorType,codim> BaseType;
405  typedef typename GridType::template Codim<codim>::Entity EntityType;
407  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
408  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
409 
410  typedef typename GridType::MPICommunicatorType Comm;
411 
412  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
413  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
414  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
415 
416  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
417 
418  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
419  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
420 
421  typedef typename ImplTraits::PllElementType PllElementType;
422 
423  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
424 
425  const LevelIndexSetImp & levelSet_;
426  const int level_;
427 public:
429  GatherScatterLevelData(const GridType & grid, MakeableEntityType & en,
430  RealEntityType & realEntity , DataCollectorType & dc,
431  const LevelIndexSetImp & levelSet, const int level)
432  : BaseType(grid,en,realEntity,dc) , levelSet_(levelSet) , level_(level) {}
433 
434  // returns true, if element is contained in set of comm interface
435  bool containsItem (const HElementType & elem) const
436  {
437  return levelSet_.containsIndex(codim, elem.getIndex() );
438  }
439 
440  // returns true, if element is contained in set of comm interface
441  bool containsItem (const HGhostType & ghost) const
442  {
443  assert( ghost.getGhost().first );
444  return containsItem( * (ghost.getGhost().first) );
445  }
446 
447  // returns true, if interior element is contained in set of comm interface
448  bool containsInterior (const HFaceType & face, PllElementType & pll) const
449  {
450  // if face level is not level_ then interior cannot be contained
451  if(face.level() != level_) return false;
452 
453  typedef Gitter::helement_STI HElementType;
454  typedef Gitter::hbndseg_STI HBndSegType;
455 
456  // check interior element here, might have a coarser level
457  pair< HElementType *, HBndSegType * > p( (HElementType *)0, (HBndSegType *)0 );
458  pll.getAttachedElement( p );
459  assert( p.first );
460  // check inside level
461  bool contained = (p.first->level() == level_);
462  assert( contained == this->containsItem( *p.first ));
463  return contained;
464  }
465 
466  // returns true, if ghost is contianed in set of comm interface
467  bool containsGhost (const HFaceType & face, PllElementType & pll) const
468  {
469  // if face level is not level_ then ghost cannot be contained
470  if(face.level() != level_) return false;
471  // otherwise check ghost level
472  return (pll.ghostLevel() == level_);
473  }
474 };
475 #endif // #if ALU3DGRID_PARALLEL
476 
478 template <class GridType, class DataCollectorType, class IndexOperatorType>
479 class GatherScatterLoadBalance : public GatherScatter
480 {
481 protected:
482  enum { codim = 0 };
483  typedef typename GridType::template Codim<0>::Entity EntityType;
485  typename GridType::template Codim<0>::Entity> MakeableEntityType;
487 
488  typedef typename GridType::MPICommunicatorType Comm;
489 
490  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
491  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
492  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
493 
494  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
495 
496  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
497  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
498 
499  typedef typename ImplTraits::PllElementType PllElementType;
500 
501  GridType & grid_;
502 
505 
506  // data handle
507  DataCollectorType & dc_;
508  IndexOperatorType & idxOp_;
509 
510  // used MessageBuffer
511  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
512 
513 public:
516  RealEntityType & realEntity , DataCollectorType & dc, IndexOperatorType & idxOp )
517  : grid_(grid), entity_(en), realEntity_(realEntity)
518  , dc_(dc) , idxOp_(idxOp)
519  {}
520 
521  // return true if dim,codim combination is contained in data set
522  bool contains(int dim, int codim) const
523  {
524  return true;
525  }
526 
530  void inlineData ( ObjectStreamType & str , HElementType & elem )
531  {
532  str.write(grid_.maxLevel());
533  // set element and then start
534  assert( elem.level () == 0 );
535  realEntity_.setElement(elem);
536  dc_.inlineData(str,entity_);
537  }
538 
542  void xtractData ( ObjectStreamType & str , HElementType & elem )
543  {
544  assert( elem.level () == 0 );
545  int mxl;
546  str.read(mxl);
547  // set element and then start
548  grid_.setMaxLevel(mxl);
549 
550  // reserve memory for new elements
551  size_t elChunk = idxOp_.newElements();
552  assert( elChunk > 0 );
553 
554  realEntity_.setElement(elem);
555  dc_.xtractData(str,entity_, elChunk);
556  }
557 
559  void compress ()
560  {
561  dc_.compress();
562  }
563 };
564 
566 //
567 // --AdaptRestrictProlong
568 //
570 template< class GridType, class AdaptDataHandle >
572 : public AdaptRestrictProlongType
573 {
574  GridType & grid_;
575  typedef typename GridType::template Codim<0>::Entity EntityType;
577  typename GridType::template Codim<0>::Entity> MakeableEntityType;
578  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
579 
580  EntityType & reFather_;
581  EntityType & reSon_;
582  RealEntityType & realFather_;
583  RealEntityType & realSon_;
584 
585  AdaptDataHandle &rp_;
586 
587  typedef typename GridType::MPICommunicatorType Comm;
588 
589  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
590  typedef typename ImplTraits::HElementType HElementType;
591  typedef typename ImplTraits::HBndSegType HBndSegType;
592  typedef typename ImplTraits::BNDFaceType BNDFaceType;
593 
594 public:
596  AdaptRestrictProlongImpl ( GridType &grid,
597  MakeableEntityType &f, RealEntityType &rf,
598  MakeableEntityType &s, RealEntityType &rs,
599  AdaptDataHandle &rp )
600  : grid_(grid)
601  , reFather_(f)
602  , reSon_(s)
603  , realFather_(rf)
604  , realSon_(rs)
605  , rp_(rp)
606  {
607  }
608 
610  {
611  }
612 
614  int preCoarsening ( HElementType & father )
615  {
616  realFather_.setElement( father );
617  rp_.preCoarsening( reFather_ );
618 
619  // reset refinement marker
620  father.resetRefinedTag();
621  return 0;
622  }
623 
625  int postRefinement ( HElementType & father )
626  {
627  realFather_.setElement( father );
628  rp_.postRefinement( reFather_ );
629 
630  // resert refinement markers
631  father.resetRefinedTag();
632  for( HElementType *son = father.down(); son ; son = son->next() )
633  son->resetRefinedTag();
634 
635  return 0;
636  }
637 
639  int preCoarsening ( HBndSegType & ghost )
640  {
641  /*
642  assert( ghost.bndtype() == ALU3DSPACE ProcessorBoundary_t );
643  realFather_.setGhost( ghost );
644  rp_.preCoarsening( reFather_ );
645  */
646  return 0;
647  }
648 
649 
651  int postRefinement ( HBndSegType & ghost )
652  {
653  /*
654  assert( ghost.bndtype() == ALU3DSPACE ProcessorBoundary_t );
655  realFather_.setGhost( ghost );
656  rp_.postRefinement( reFather_ );
657  */
658  return 0;
659  }
660 };
661 
662 
663 
664 template< class GridType, class AdaptDataHandle, class GlobalIdSetImp >
666 : public AdaptRestrictProlongImpl< GridType, AdaptDataHandle >
667 {
669  GlobalIdSetImp & set_;
670  typedef typename GridType::template Codim<0>::Entity EntityType;
672  typename GridType::template Codim<0>::Entity> MakeableEntityType;
673  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
674 
675  typedef typename GridType::MPICommunicatorType Comm;
676 
677  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
678  typedef typename ImplTraits::HElementType HElementType;
679  typedef typename ImplTraits::HBndSegType HBndSegType;
680 
681 public:
683  AdaptRestrictProlongGlSet ( GridType &grid,
684  MakeableEntityType &f, RealEntityType &rf,
685  MakeableEntityType &s, RealEntityType &rs,
686  AdaptDataHandle &rp,
687  GlobalIdSetImp & set )
688  : BaseType( grid, f, rf, s, rs, rp ),
689  set_( set )
690  {}
691 
693 
695  int postRefinement ( HElementType & elem )
696  {
697  set_.postRefinement( elem );
698  return BaseType :: postRefinement(elem );
699  }
700 };
701 
702 // this class is for counting the tree depth of the
703 // element when unpacking data from load balance
704 template <class GridType , class DataHandleType>
705 class LoadBalanceElementCount : public AdaptRestrictProlongType
706 {
707  GridType & grid_;
708  typedef typename GridType::template Codim<0>::Entity EntityType;
710  typename GridType::template Codim<0>::Entity> MakeableEntityType;
711  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
712 
713  typedef typename GridType::Traits::LeafIndexSet LeafIndexSetType;
714 
715  EntityType & reFather_;
716  EntityType & reSon_;
717  RealEntityType & realFather_;
718  RealEntityType & realSon_;
719 
720  DataHandleType & dh_;
721 
722  typedef typename GridType::MPICommunicatorType Comm;
723 
724  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
725  typedef typename ImplTraits::HElementType HElementType;
726  typedef typename ImplTraits::HBndSegType HBndSegType;
727 
728  int newMemSize_;
729 public:
731  LoadBalanceElementCount (GridType & grid,
732  MakeableEntityType & f, RealEntityType & rf,
733  MakeableEntityType & s, RealEntityType & rs,
734  DataHandleType & dh)
735  : grid_(grid)
736  , reFather_(f)
737  , reSon_(s)
738  , realFather_(rf)
739  , realSon_(rs)
740  , dh_(dh)
741  , newMemSize_ (1) // we have at least one element (the macro element)
742  {
743  }
744 
746 
748  int postRefinement ( HElementType & elem )
749  {
750  // when called for a macro element, then a new tree is starting
751  // set to 1 because for only macro elements this method is not called
752  if( elem.level() == 0 ) newMemSize_ = 1;
753 
754  for( HElementType * son = elem.down() ; son ; son= son->next())
755  {
756  ++ newMemSize_;
757  }
758  return 0;
759  }
760 
762  int preCoarsening ( HElementType & elem )
763  {
764  return 0;
765  }
766 
769  int preCoarsening ( HBndSegType & el )
770  {
771  return 0;
772  }
773 
778  int postRefinement ( HBndSegType & el )
779  {
780  return 0;
781  }
782 
783  int newElements () const { return newMemSize_; }
784 };
785 
786 } // end namespace ALUGridSpace
787 
788 #endif // #ifndef DUNE_ALU3DGRIDDATAHANDLE_HH