dune-grid  2.2.0
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:
168  GatherScatterBaseImpl(const GridType & grid, MakeableEntityType & en,
169  RealEntityType & realEntity , DataCollectorType & dc)
170  : grid_(grid), entity_(en), realEntity_(realEntity)
171  , dc_(dc) , variableSize_ ( ! dc_.fixedsize( EntityType :: dimension, codim ))
172  {}
173 
174  // return true if dim,codim combination is contained in data set
175  bool contains(int dim, int codim) const
176  {
177  return dc_.contains(dim,codim);
178  }
179 
180  // return true if item might from entity belonging to data set
181  virtual bool containsItem (const HElementType & elem) const
182  {
183  return elem.isLeafEntity();
184  }
185 
186  // return true if item might from entity belonging to data set
187  virtual bool containsItem (const HGhostType & ghost) const = 0;
188 
190  void sendData ( ObjectStreamType & str , const HElementType & elem )
191  {
192  assert( this->containsItem(elem) );
193  realEntity_.setElement( const_cast<HElementType &> (elem) );
194 
195  // write size in case of variable size
196  writeSize( str, entity_);
197  // gather data
198  dc_.gather(str, entity_);
199  }
200 
202  void sendData ( ObjectStreamType & str , const HGhostType& ghost)
203  {
204  assert( this->containsItem( ghost ) );
205 
206  // set ghost as entity
207  realEntity_.setGhost( const_cast <HGhostType &> (ghost) );
208 
209  // write size in case of variable size
210  writeSize( str, entity_);
211  // gather data
212  dc_.gather(str, entity_);
213  }
214 
216  void recvData ( ObjectStreamType & str , HElementType & elem )
217  {
218  assert( this->containsItem( elem ) );
219  realEntity_.setElement( elem );
220 
221  size_t size = getSize(str, entity_);
222  dc_.scatter(str, entity_, size);
223  }
224 
226  void recvData ( ObjectStreamType & str , HGhostType & ghost )
227  {
228  assert( this->containsItem( ghost ) );
229 
230  // set ghost as entity
231  realEntity_.setGhost( ghost );
232 
233  size_t size = getSize(str , entity_ );
234  dc_.scatter(str, entity_, size );
235  }
236 
237 protected:
238  size_t getSize(ObjectStreamType & str, EntityType & en)
239  {
240  if(variableSize_)
241  {
242  size_t size;
243  str.read(size);
244  return size;
245  }
246  else
247  return dc_.size(en);
248  }
249 
250  // write variable size to stream
251  void writeSize(ObjectStreamType & str, EntityType & en)
252  {
253  if( variableSize_ )
254  {
255  size_t size = dc_.size( en );
256  str.write( size );
257  }
258  }
259 };
260 
261 #if ALU3DGRID_PARALLEL
262 
263 template< class GridType, class DataCollectorType, int codim >
264 class GatherScatterLeafData
265 : public GatherScatterBaseImpl< GridType, DataCollectorType, codim >
266 {
267  enum { dim = GridType :: dimension };
268 
269  typedef GatherScatterBaseImpl<GridType,DataCollectorType,codim> BaseType;
270  typedef typename GridType::template Codim<codim>::Entity EntityType;
272  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
273  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
274 
275  typedef typename GridType::MPICommunicatorType Comm;
276 
277  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
278  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
279  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
280 
281  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
282 
283  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
284  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
285 
286  typedef typename ImplTraits::PllElementType PllElementType;
287 
288 public:
290  GatherScatterLeafData(const GridType & grid, MakeableEntityType & en,
291  RealEntityType & realEntity , DataCollectorType & dc)
292  : BaseType(grid,en,realEntity,dc)
293  {
294  // if leaf vertices are communicated,
295  // make sure that vertex list is up2date
296  // but only do this, if vertex data contained,
297  // because the list update is expensive
298  if( (codim == 3) && dc.contains(dim,codim) )
299  {
300  // call of this method forces update of list,
301  // if list is not up to date
302  grid.getLeafVertexList();
303  }
304  }
305 
306  // returns true, if element is contained in set of comm interface
307  bool containsItem (const HElementType & elem) const
308  {
309  return elem.isLeafEntity();
310  }
311 
312  // returns true, if element is contained in set of comm interface
313  bool containsItem (const HGhostType & ghost) const
314  {
315  return ghost.isLeafEntity();
316  }
317 
318  // returns true, if interior element is contained in set of comm interface
319  bool containsInterior (const HFaceType & face, PllElementType & pll) const
320  {
321  return face.isInteriorLeaf();
322  }
323 
324  // returns true, if ghost is contianed in set of comm interface
325  bool containsGhost (const HFaceType & face , PllElementType & pll) const
326  {
327  return pll.ghostLeaf();
328  }
329 
330  // set elem to realEntity
331  void setElement(const HElementType & elem)
332  {
333  this->realEntity_.setElement(elem);
334  }
335 };
336 
338 template <class GridType, class DataCollectorType , int codim >
339 class GatherScatterLevelData
340 : public GatherScatterBaseImpl<GridType,DataCollectorType,codim>
341 {
342  typedef GatherScatterBaseImpl<GridType,DataCollectorType,codim> BaseType;
343  typedef typename GridType::template Codim<codim>::Entity EntityType;
345  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
346  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
347 
348  typedef typename GridType::MPICommunicatorType Comm;
349 
350  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
351  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
352  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
353 
354  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
355 
356  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
357  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
358 
359  typedef typename ImplTraits::PllElementType PllElementType;
360 
361  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
362 
363  const LevelIndexSetImp & levelSet_;
364  const int level_;
365 public:
367  GatherScatterLevelData(const GridType & grid, MakeableEntityType & en,
368  RealEntityType & realEntity , DataCollectorType & dc,
369  const LevelIndexSetImp & levelSet, const int level)
370  : BaseType(grid,en,realEntity,dc) , levelSet_(levelSet) , level_(level)
371  {
372  }
373 
374  // returns true, if element is contained in set of comm interface
375  bool containsItem (const HElementType & elem) const
376  {
377  return levelSet_.containsIndex(codim, elem.getIndex() );
378  }
379 
380  // set elem to realEntity
381  void setElement(const HElementType & elem)
382  {
383  this->realEntity_.setElement(elem,level_);
384  }
385 
386 };
387 
388 
390 template <class GridType, class DataCollectorType>
391 class GatherScatterLevelData<GridType,DataCollectorType,0>
392 : public GatherScatterBaseImpl<GridType,DataCollectorType,0>
393 {
394  enum { codim = 0 };
395  typedef GatherScatterBaseImpl<GridType,DataCollectorType,codim> BaseType;
396  typedef typename GridType::template Codim<codim>::Entity EntityType;
398  typename GridType::template Codim<codim>::Entity> MakeableEntityType;
399  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
400 
401  typedef typename GridType::MPICommunicatorType Comm;
402 
403  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
404  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
405  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
406 
407  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
408 
409  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
410  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
411 
412  typedef typename ImplTraits::PllElementType PllElementType;
413 
414  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
415 
416  const LevelIndexSetImp & levelSet_;
417  const int level_;
418 public:
420  GatherScatterLevelData(const GridType & grid, MakeableEntityType & en,
421  RealEntityType & realEntity , DataCollectorType & dc,
422  const LevelIndexSetImp & levelSet, const int level)
423  : BaseType(grid,en,realEntity,dc) , levelSet_(levelSet) , level_(level) {}
424 
425  // returns true, if element is contained in set of comm interface
426  bool containsItem (const HElementType & elem) const
427  {
428  return levelSet_.containsIndex(codim, elem.getIndex() );
429  }
430 
431  // returns true, if element is contained in set of comm interface
432  bool containsItem (const HGhostType & ghost) const
433  {
434  assert( ghost.getGhost().first );
435  return containsItem( * (ghost.getGhost().first) );
436  }
437 
438  // returns true, if interior element is contained in set of comm interface
439  bool containsInterior (const HFaceType & face, PllElementType & pll) const
440  {
441  // if face level is not level_ then interior cannot be contained
442  if(face.level() != level_) return false;
443 
444  typedef Gitter::helement_STI HElementType;
445  typedef Gitter::hbndseg_STI HBndSegType;
446 
447  // check interior element here, might have a coarser level
448  pair< HElementType *, HBndSegType * > p( (HElementType *)0, (HBndSegType *)0 );
449  pll.getAttachedElement( p );
450  assert( p.first );
451  // check inside level
452  bool contained = (p.first->level() == level_);
453  assert( contained == this->containsItem( *p.first ));
454  return contained;
455  }
456 
457  // returns true, if ghost is contianed in set of comm interface
458  bool containsGhost (const HFaceType & face, PllElementType & pll) const
459  {
460  // if face level is not level_ then ghost cannot be contained
461  if(face.level() != level_) return false;
462  // otherwise check ghost level
463  return (pll.ghostLevel() == level_);
464  }
465 };
466 #endif // #if ALU3DGRID_PARALLEL
467 
469 template <class GridType, class DataCollectorType, class IndexOperatorType>
470 class GatherScatterLoadBalance : public GatherScatter
471 {
472 protected:
473  enum { codim = 0 };
474  typedef typename GridType::template Codim<0>::Entity EntityType;
476  typename GridType::template Codim<0>::Entity> MakeableEntityType;
478 
479  typedef typename GridType::MPICommunicatorType Comm;
480 
481  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
482  typedef typename ImplTraits::template Codim< codim >::ImplementationType IMPLElementType;
483  typedef typename ImplTraits::template Codim< codim >::InterfaceType HElementType;
484 
485  typedef typename ImplTraits::template Codim< 1 >::InterfaceType HFaceType;
486 
487  typedef typename ImplTraits::template Codim< 0 >::GhostInterfaceType HGhostType;
488  typedef typename ImplTraits::template Codim< 0 >::GhostImplementationType ImplGhostType;
489 
490  typedef typename ImplTraits::PllElementType PllElementType;
491 
492  GridType & grid_;
493 
496 
497  // data handle
498  DataCollectorType & dc_;
499  IndexOperatorType & idxOp_;
500 
501  // used MessageBuffer
502  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
503 
504 public:
507  RealEntityType & realEntity , DataCollectorType & dc, IndexOperatorType & idxOp )
508  : grid_(grid), entity_(en), realEntity_(realEntity)
509  , dc_(dc) , idxOp_(idxOp)
510  {}
511 
512  // return true if dim,codim combination is contained in data set
513  bool contains(int dim, int codim) const
514  {
515  return true;
516  }
517 
521  void inlineData ( ObjectStreamType & str , HElementType & elem )
522  {
523  str.write(grid_.maxLevel());
524  // set element and then start
525  assert( elem.level () == 0 );
526  realEntity_.setElement(elem);
527  dc_.inlineData(str,entity_);
528  }
529 
533  void xtractData ( ObjectStreamType & str , HElementType & elem )
534  {
535  assert( elem.level () == 0 );
536  int mxl;
537  str.read(mxl);
538  // set element and then start
539  grid_.setMaxLevel(mxl);
540 
541  // reserve memory for new elements
542  size_t elChunk = idxOp_.newElements();
543  assert( elChunk > 0 );
544 
545  realEntity_.setElement(elem);
546  dc_.xtractData(str,entity_, elChunk);
547  }
548 
550  void compress ()
551  {
552  dc_.compress();
553  }
554 };
555 
557 //
558 // --AdaptRestrictProlong
559 //
561 template< class GridType, class AdaptDataHandle >
563 : public AdaptRestrictProlongType
564 {
565  GridType & grid_;
566  typedef typename GridType::template Codim<0>::Entity EntityType;
568  typename GridType::template Codim<0>::Entity> MakeableEntityType;
569  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
570 
571  EntityType & reFather_;
572  EntityType & reSon_;
573  RealEntityType & realFather_;
574  RealEntityType & realSon_;
575 
576  AdaptDataHandle &rp_;
577 
578  typedef typename GridType::MPICommunicatorType Comm;
579 
580  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
581  typedef typename ImplTraits::HElementType HElementType;
582  typedef typename ImplTraits::HBndSegType HBndSegType;
583  typedef typename ImplTraits::BNDFaceType BNDFaceType;
584 
585 public:
587  AdaptRestrictProlongImpl ( GridType &grid,
588  MakeableEntityType &f, RealEntityType &rf,
589  MakeableEntityType &s, RealEntityType &rs,
590  AdaptDataHandle &rp )
591  : grid_(grid)
592  , reFather_(f)
593  , reSon_(s)
594  , realFather_(rf)
595  , realSon_(rs)
596  , rp_(rp)
597  {
598  }
599 
601  {
602  }
603 
605  int preCoarsening ( HElementType & father )
606  {
607  realFather_.setElement( father );
608  rp_.preCoarsening( reFather_ );
609 
610  // reset refinement marker
611  father.resetRefinedTag();
612  return 0;
613  }
614 
616  int postRefinement ( HElementType & father )
617  {
618  realFather_.setElement( father );
619  rp_.postRefinement( reFather_ );
620 
621  // resert refinement markers
622  father.resetRefinedTag();
623  for( HElementType *son = father.down(); son ; son = son->next() )
624  son->resetRefinedTag();
625 
626  return 0;
627  }
628 
630  int preCoarsening ( HBndSegType & ghost )
631  {
632  /*
633  assert( ghost.bndtype() == ALU3DSPACE ProcessorBoundary_t );
634  realFather_.setGhost( ghost );
635  rp_.preCoarsening( reFather_ );
636  */
637  return 0;
638  }
639 
640 
642  int postRefinement ( HBndSegType & ghost )
643  {
644  /*
645  assert( ghost.bndtype() == ALU3DSPACE ProcessorBoundary_t );
646  realFather_.setGhost( ghost );
647  rp_.postRefinement( reFather_ );
648  */
649  return 0;
650  }
651 };
652 
653 
654 
655 template< class GridType, class AdaptDataHandle, class GlobalIdSetImp >
657 : public AdaptRestrictProlongImpl< GridType, AdaptDataHandle >
658 {
660  GlobalIdSetImp & set_;
661  typedef typename GridType::template Codim<0>::Entity EntityType;
663  typename GridType::template Codim<0>::Entity> MakeableEntityType;
664  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
665 
666  typedef typename GridType::MPICommunicatorType Comm;
667 
668  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
669  typedef typename ImplTraits::HElementType HElementType;
670  typedef typename ImplTraits::HBndSegType HBndSegType;
671 
672 public:
674  AdaptRestrictProlongGlSet ( GridType &grid,
675  MakeableEntityType &f, RealEntityType &rf,
676  MakeableEntityType &s, RealEntityType &rs,
677  AdaptDataHandle &rp,
678  GlobalIdSetImp & set )
679  : BaseType( grid, f, rf, s, rs, rp ),
680  set_( set )
681  {}
682 
684 
686  int postRefinement ( HElementType & elem )
687  {
688  set_.postRefinement( elem );
689  return BaseType :: postRefinement(elem );
690  }
691 };
692 
693 // this class is for counting the tree depth of the
694 // element when unpacking data from load balance
695 template <class GridType , class DataHandleType>
696 class LoadBalanceElementCount : public AdaptRestrictProlongType
697 {
698  GridType & grid_;
699  typedef typename GridType::template Codim<0>::Entity EntityType;
701  typename GridType::template Codim<0>::Entity> MakeableEntityType;
702  typedef typename MakeableEntityType :: ImplementationType RealEntityType;
703 
704  typedef typename GridType::Traits::LeafIndexSet LeafIndexSetType;
705 
706  EntityType & reFather_;
707  EntityType & reSon_;
708  RealEntityType & realFather_;
709  RealEntityType & realSon_;
710 
711  DataHandleType & dh_;
712 
713  typedef typename GridType::MPICommunicatorType Comm;
714 
715  typedef Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits;
716  typedef typename ImplTraits::HElementType HElementType;
717  typedef typename ImplTraits::HBndSegType HBndSegType;
718 
719  int newMemSize_;
720 public:
722  LoadBalanceElementCount (GridType & grid,
723  MakeableEntityType & f, RealEntityType & rf,
724  MakeableEntityType & s, RealEntityType & rs,
725  DataHandleType & dh)
726  : grid_(grid)
727  , reFather_(f)
728  , reSon_(s)
729  , realFather_(rf)
730  , realSon_(rs)
731  , dh_(dh)
732  , newMemSize_ (1) // we have at least one element (the macro element)
733  {
734  }
735 
737 
739  int postRefinement ( HElementType & elem )
740  {
741  // when called for a macro element, then a new tree is starting
742  // set to 1 because for only macro elements this method is not called
743  if( elem.level() == 0 ) newMemSize_ = 1;
744 
745  for( HElementType * son = elem.down() ; son ; son= son->next())
746  {
747  ++ newMemSize_;
748  }
749  return 0;
750  }
751 
753  int preCoarsening ( HElementType & elem )
754  {
755  return 0;
756  }
757 
760  int preCoarsening ( HBndSegType & el )
761  {
762  return 0;
763  }
764 
769  int postRefinement ( HBndSegType & el )
770  {
771  return 0;
772  }
773 
774  int newElements () const { return newMemSize_; }
775 };
776 
777 } // end namespace ALUGridSpace
778 
779 #endif // #ifndef DUNE_ALU3DGRIDDATAHANDLE_HH