dune-alugrid  2.6-git
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 #include <type_traits>
7 
8 #include <dune/common/version.hh>
9 
10 #include <dune/grid/common/grid.hh>
11 #include <dune/grid/common/adaptcallback.hh>
12 
15 
16 //- local includes
17 #include "alu3dinclude.hh"
18 
19 namespace ALUGrid
20 {
21 
23  template< class GridType, class DataCollectorType, int codim >
25  : public GatherScatter
26  {
27  protected:
28  enum { dimension = GridType::dimension };
29  const GridType & grid_;
30  typedef typename GridType::template Codim<codim>::Entity EntityType;
31  typedef typename GridType::template Codim<codim>::EntityImp RealEntityType;
32 
33  typedef typename GridType::MPICommunicatorType Comm;
34 
36  typedef typename ImplTraits::template Codim< dimension, codim >::ImplementationType ImplElementType;
37  typedef typename ImplTraits::template Codim< dimension, codim >::InterfaceType HElementType;
38 
41 
42  DataCollectorType & dc_;
43 
44  const bool variableSize_;
45 
46  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
47 
48  typedef typename DataCollectorType:: DataType DataType;
49 
50  using GatherScatter :: setData ;
51  using GatherScatter :: sendData ;
52  using GatherScatter :: recvData ;
53  using GatherScatter :: containsItem ;
54 
55  public:
57  GatherScatterBaseImpl(const GridType & grid, EntityType & en,
58  RealEntityType & realEntity , DataCollectorType & dc)
59  : grid_(grid), entity_(en), realEntity_(realEntity) , dc_(dc)
60  , variableSize_( ! dc_.fixedsize(EntityType::dimension,codim) )
61  {
62  }
63 
64  //This method is called in gitter_dune_pll_impl.cc with arguments 3,codimension
65  //So we have to adapt things to the user view, that writes it with
66  // 2,codimension
67  // return true if dim,codim combination is contained in data set
68  bool contains(int dim, int cd) const
69  {
70  //dimension is GridImp::dimension
71  if(dim == dimension)
72  {
73  //the original call
74  return dc_.contains(dim,cd);
75  }
76  //adaptation for 2d
77  else if(dimension == 2)
78  {
79  //we do not want to transmit edge data
80  if(cd == 2)
81  return false;
82  else if (cd == 3)
83  return dc_.contains(dimension, 2);
84  else
85  return dc_.contains(dimension, cd);
86  }
87  //
88  else
89  {
90  std::cerr << "DataHandle.contains called with non-matching dim and codim" << std::endl;
91  return false;
92  }
93  }
94 
95  // returns true, if element is contained in set of comm interface
96  // this method must be overlaoded by the impl classes
97  virtual bool containsItem (const HElementType & elem) const = 0;
98 
99  // set elem to realEntity
100  virtual void setElement(const HElementType & elem) = 0;
101 
102  void setData ( ObjectStreamType & str , HElementType & elem )
103  {
104  // one of this should be either true
105  alugrid_assert ( this->containsItem( elem ) || elem.isGhost() );
106 
107  // set element and then start
108  setElement(elem);
109 
110  // make sure partition type is set correct
111  alugrid_assert ( elem.isGhost() == (entity_.partitionType() == Dune :: GhostEntity) );
112 
113  size_t size = getSize(str, entity_);
114  // use normal scatter method
115  dc_.scatter(str,entity_, size );
116  }
117 
119  void sendData ( ObjectStreamType & str , HElementType & elem )
120  {
121  // make sure element is contained in communication interface
122  //alugrid_assert ( this->containsItem( elem ) );
123  setElement(elem);
124 
125  // if varaible size, also send size
126  if( variableSize_ )
127  {
128  size_t size = dc_.size( entity_ );
129  str.write( size );
130  }
131 
132  dc_.gather(str, entity_ );
133  }
134 
136  void recvData ( ObjectStreamType & str , HElementType & elem )
137  {
138  alugrid_assert ( this->containsItem( elem ) );
139  setElement( elem );
140 
141  size_t size = getSize(str, entity_);
142  dc_.scatter(str,entity_, size );
143  }
144 
145  protected:
146  size_t getSize(ObjectStreamType & str, EntityType & en)
147  {
148  if(variableSize_)
149  {
150  size_t size;
151  str.read(size);
152  return size;
153  }
154  else
155  return dc_.size(en);
156  }
157  };
158 
159  //***********************************************************
160  //
161  // --specialisation for codim 0
162  //
163  //***********************************************************
164 
166  template <class GridType, class DataCollectorType >
167  class GatherScatterBaseImpl<GridType,DataCollectorType,0> : public GatherScatter
168  {
169  protected:
170  enum { codim = 0 };
171  enum { dim = GridType::dimension };
172  const GridType & grid_;
173  typedef typename GridType::template Codim<0>::Entity EntityType;
174  typedef typename GridType::template Codim<0>::EntityImp RealEntityType;
175 
176  typedef typename GridType::MPICommunicatorType Comm;
177 
179  typedef typename ImplTraits::template Codim< dim, codim >::ImplementationType ImplElementType;
180  typedef typename ImplTraits::template Codim< dim, codim >::InterfaceType HElementType;
181 
182  typedef typename ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType;
183 
184  typedef typename ImplTraits::template Codim< dim, codim >::GhostInterfaceType HGhostType;
185  typedef typename ImplTraits::template Codim< dim, codim >::GhostImplementationType ImplGhostType;
186 
187  typedef typename ImplTraits::PllElementType PllElementType;
188 
191 
192  // data handle
193  DataCollectorType & dc_;
194 
195  const bool variableSize_;
196 
197  // used MessageBuffer
198  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
199 
200  // use all other containsItem from the base class
201  using GatherScatter :: setData ;
202  using GatherScatter :: sendData ;
203  using GatherScatter :: recvData ;
204 
205  public:
206  // use all other containsItem from the base class
207  using GatherScatter :: containsItem ;
208 
210  GatherScatterBaseImpl(const GridType & grid, EntityType & en,
211  RealEntityType & realEntity , DataCollectorType & dc)
212  : grid_(grid), entity_(en), realEntity_(realEntity)
213  , dc_(dc) , variableSize_ ( ! dc_.fixedsize( EntityType :: dimension, codim ))
214  {}
215 
216  // return true if dim,codim combination is contained in data set
217  bool contains(int dim, int codim) const
218  {
219  return dc_.contains(dim,codim);
220  }
221 
222  // return true if item might from entity belonging to data set
223  virtual bool containsItem (const HElementType & elem) const
224  {
225  return elem.isLeafEntity();
226  }
227 
228  // return true if item might from entity belonging to data set
229  virtual bool containsItem (const HGhostType & ghost) const = 0;
230 
232  void sendData ( ObjectStreamType & str , const HElementType & elem )
233  {
234  alugrid_assert ( this->containsItem(elem) );
235  realEntity_.setElement( const_cast<HElementType &> (elem) );
236 
237  // write size in case of variable size
238  writeSize( str, entity_);
239  // gather data
240  dc_.gather(str, entity_);
241  }
242 
244  void sendData ( ObjectStreamType & str , const HGhostType& ghost)
245  {
246  alugrid_assert ( this->containsItem( ghost ) );
247 
248  // set ghost as entity
249  realEntity_.setGhost( const_cast <HGhostType &> (ghost) );
250 
251  // write size in case of variable size
252  writeSize( str, entity_);
253  // gather data
254  dc_.gather(str, entity_);
255  }
256 
258  void recvData ( ObjectStreamType & str , HElementType & elem )
259  {
260  // alugrid_assert ( this->containsItem( elem ) );
261  realEntity_.setElement( elem );
262 
263  size_t size = getSize(str, entity_);
264  dc_.scatter(str, entity_, size);
265  }
266 
268  void recvData ( ObjectStreamType & str , HGhostType & ghost )
269  {
270  alugrid_assert ( this->containsItem( ghost ) );
271 
272  // set ghost as entity
273  realEntity_.setGhost( ghost );
274 
275  size_t size = getSize(str , entity_ );
276  dc_.scatter(str, entity_, size );
277  }
278 
279  protected:
280  size_t getSize(ObjectStreamType & str, EntityType & en)
281  {
282  if(variableSize_)
283  {
284  size_t size;
285  str.read(size);
286  return size;
287  }
288  else
289  return dc_.size(en);
290  }
291 
292  // write variable size to stream
294  {
295  if( variableSize_ )
296  {
297  size_t size = dc_.size( en );
298  str.write( size );
299  }
300  }
301  };
302 
304  template< class GridType, class DataCollectorType, int codim >
306  : public GatherScatterBaseImpl< GridType, DataCollectorType, codim >
307  {
308  enum { dim = GridType :: dimension };
309 
311  typedef typename GridType::template Codim<codim>::Entity EntityType;
312  typedef typename GridType::template Codim<codim>::EntityImp RealEntityType;
313 
314  typedef typename GridType::MPICommunicatorType Comm;
315 
317  typedef typename ImplTraits::template Codim< dim, codim >::ImplementationType IMPLElementType;
318  typedef typename ImplTraits::template Codim< dim, codim >::InterfaceType HElementType;
319 
320  typedef typename ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType;
321 
322  typedef typename ImplTraits::template Codim< dim, 0 >::GhostInterfaceType HGhostType;
323  typedef typename ImplTraits::template Codim< dim, 0 >::GhostImplementationType ImplGhostType;
324 
325  typedef typename ImplTraits::PllElementType PllElementType;
326 
327  using BaseType :: grid_;
328  public:
329  // use all other containsItem methods from the base class
331 
333  GatherScatterLeafData(const GridType & grid, EntityType & en,
334  RealEntityType & realEntity , DataCollectorType & dc)
335  : BaseType(grid,en,realEntity,dc)
336  {
337  // if leaf vertices are communicated,
338  // make sure that vertex list is up2date
339  // but only do this, if vertex data contained,
340  // because the list update is expensive
341  if( (codim == dim) && dc.contains(dim,codim) )
342  {
343  // call of this method forces update of list,
344  // if list is not up to date
345  grid.getLeafVertexList();
346  }
347  }
348 
349  // returns true, if element is contained in set of comm interface
350  bool containsItem (const HElementType & elem) const
351  {
352  return (dim == 2 ? elem.is2d() : true) && elem.isLeafEntity();
353  }
354 
355  // returns true, if element is contained in set of comm interface
356  bool containsItem (const HGhostType & ghost) const
357  {
358  //in 2d ghosts are always 2d, as they are codim 0
359  //so we do not need to adapt the switch
360  return ghost.isLeafEntity();
361  }
362 
363  // returns true, if interior element is contained in set of comm interface
364  bool containsInterior (const HFaceType & face, PllElementType & pll) const
365  {
366  return (dim == 2 ? face.is2d() : true) && face.isInteriorLeaf();
367  }
368 
369  // returns true, if ghost is contianed in set of comm interface
370  bool containsGhost (const HFaceType & face , PllElementType & pll) const
371  {
372  return (dim == 2 ? face.is2d() : true) && pll.ghostLeaf();
373  }
374 
375  // set elem to realEntity
376  void setElement(const HElementType & elem)
377  {
378  this->realEntity_.setElement(elem, grid_);
379  }
380  };
381 
383  template <class GridType, class DataCollectorType , int codim >
385  : public GatherScatterBaseImpl<GridType,DataCollectorType,codim>
386  {
387  enum { dim = GridType::dimension };
389  typedef typename GridType::template Codim<codim>::Entity EntityType;
390  typedef typename GridType::template Codim<codim>::EntityImp RealEntityType;
391 
392  typedef typename GridType::MPICommunicatorType Comm;
393 
395  typedef typename ImplTraits::template Codim< dim, codim >::ImplementationType IMPLElementType;
396  typedef typename ImplTraits::template Codim< dim, codim >::InterfaceType HElementType;
397 
398  typedef typename ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType;
399 
400  typedef typename ImplTraits::template Codim< dim, 0 >::GhostInterfaceType HGhostType;
401  typedef typename ImplTraits::template Codim< dim, 0 >::GhostImplementationType ImplGhostType;
402 
403  typedef typename ImplTraits::PllElementType PllElementType;
404 
405  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
406 
407  const LevelIndexSetImp & levelSet_;
408  const int level_;
409  public:
410  // use containsItem for ghost element from BaseType
412 
414  GatherScatterLevelData(const GridType & grid, EntityType & en,
415  RealEntityType & realEntity , DataCollectorType & dc,
416  const LevelIndexSetImp & levelSet, const int level)
417  : BaseType(grid,en,realEntity,dc) , levelSet_(levelSet) , level_(level)
418  {
419  }
420 
421  // returns true, if element is contained in set of comm interface
422  bool containsItem (const HElementType & elem) const
423  {
424  return (dim == 2 ? elem.is2d() : true) && levelSet_.containsIndex(codim, elem.getIndex() );
425  }
426 
427  // set elem to realEntity
428  void setElement(const HElementType & elem)
429  {
430  this->realEntity_.setElement(elem,level_);
431  }
432 
433  };
434 
436  // this class is for the 2d grid - it masks out the edgeGatherScatter
437  template <class GridType, class DataCollectorType , int codim >
439  : public GatherScatterBaseImpl<GridType,DataCollectorType,codim>
440  {
441  enum { dim = GridType::dimension };
443  typedef typename GridType::template Codim<codim>::Entity EntityType;
444  typedef typename GridType::template Codim<codim>::EntityImp RealEntityType;
445 
446  typedef typename GridType::MPICommunicatorType Comm;
447 
449 
450  typedef typename ImplTraits::template Codim< 2, codim >::ImplementationType IMPLElementType;
451  typedef typename ImplTraits::template Codim< 2, codim >::InterfaceType HElementType;
452  //We want to have the real 3d no data gatherscatter so set dim to 3 here
453  typedef typename ImplTraits::template Codim< 3, codim >::ImplementationType RealIMPLElementType;
454  typedef typename ImplTraits::template Codim< 3, codim >::InterfaceType RealHElementType;
455 
456  typedef typename ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType;
457 
458  typedef typename ImplTraits::template Codim< dim, 0 >::GhostInterfaceType HGhostType;
459  typedef typename ImplTraits::template Codim< dim, 0 >::GhostImplementationType ImplGhostType;
460 
461  typedef typename ImplTraits::PllElementType PllElementType;
462 
463  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
464 
465  public:
466  // use containsItem for ghost element from BaseType
468 
470  GatherScatterNoData(const GridType & grid, EntityType & en,
471  RealEntityType & realEntity , DataCollectorType & dc,
472  const LevelIndexSetImp & levelSet, const int level)
473  : BaseType(grid,en,realEntity,dc)
474  {
475  }
476 
478  GatherScatterNoData(const GridType & grid, EntityType & en,
479  RealEntityType & realEntity , DataCollectorType & dc)
480  : BaseType(grid,en,realEntity,dc)
481  {
482  }
483 
484  // returns true, if element is contained in set of comm interface
485  bool containsItem (const HElementType & elem) const
486  {
487  return false;
488  }
489 
490  // returns true, if element is contained in set of comm interface
491  bool containsItem (const RealHElementType & elem) const
492  {
493  return false;
494  }
495 
496  // set elem to realEntity
497  void setElement(const HElementType & elem)
498  {
499  //we should not get here hopefully
500  alugrid_assert(false);
501  return;
502  }
503 
504  // set elem to realEntity
505  void setElement(const RealHElementType & elem)
506  {
507  //we should not get here hopefully
508  alugrid_assert(false);
509  return;
510  }
511 
512  };
513 
514 
516  template <class GridType, class DataCollectorType>
517  class GatherScatterLevelData<GridType,DataCollectorType,0>
518  : public GatherScatterBaseImpl<GridType,DataCollectorType,0>
519  {
520  enum { codim = 0 };
521  enum { dim = GridType:: dimension };
523  typedef typename GridType::template Codim<codim>::Entity EntityType;
524  typedef typename GridType::template Codim<codim>::EntityImp RealEntityType;
525 
526  typedef typename GridType::MPICommunicatorType Comm;
527 
529  typedef typename ImplTraits::template Codim< dim, codim >::ImplementationType IMPLElementType;
530  typedef typename ImplTraits::template Codim< dim, codim >::InterfaceType HElementType;
531 
532  typedef typename ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType;
533 
534  typedef typename ImplTraits::template Codim< dim, 0 >::GhostInterfaceType HGhostType;
535  typedef typename ImplTraits::template Codim< dim, 0 >::GhostImplementationType ImplGhostType;
536 
537  typedef typename ImplTraits::PllElementType PllElementType;
538 
539  typedef typename GridType::LevelIndexSetImp LevelIndexSetImp;
540 
541  const LevelIndexSetImp & levelSet_;
542  const int level_;
543  public:
545  GatherScatterLevelData(const GridType & grid, EntityType & en,
546  RealEntityType & realEntity , DataCollectorType & dc,
547  const LevelIndexSetImp & levelSet, const int level)
548  : BaseType(grid,en,realEntity,dc) , levelSet_(levelSet) , level_(level) {}
549 
550  // returns true, if element is contained in set of comm interface
551  bool containsItem (const HElementType & elem) const
552  {
553  return levelSet_.containsIndex(codim, elem.getIndex() );
554  }
555 
556  // returns true, if element is contained in set of comm interface
557  bool containsItem (const HGhostType & ghost) const
558  {
559  alugrid_assert ( ghost.getGhost().first );
560  return containsItem( * (ghost.getGhost().first) );
561  }
562 
563  // returns true, if interior element is contained in set of comm interface
564  bool containsInterior (const HFaceType & face, PllElementType & pll) const
565  {
566  // if face level is not level_ then interior cannot be contained
567  if(face.level() != level_) return false;
568 
569  typedef Gitter::helement_STI HElementType;
570  typedef Gitter::hbndseg_STI HBndSegType;
571 
572  // check interior element here, might have a coarser level
573  std::pair< HElementType *, HBndSegType * > p( (HElementType *)0, (HBndSegType *)0 );
574  pll.getAttachedElement( p );
575  alugrid_assert ( p.first );
576  // check inside level
577  bool contained = (p.first->level() == level_);
578  alugrid_assert ( contained == this->containsItem( *p.first ));
579  return contained;
580  }
581 
582  // returns true, if ghost is contianed in set of comm interface
583  bool containsGhost (const HFaceType & face, PllElementType & pll) const
584  {
585  // if face level is not level_ then ghost cannot be contained
586  if(face.level() != level_) return false;
587  // otherwise check ghost level
588  return (pll.ghostLevel() == level_);
589  }
590  };
591 
592 
594  //
595  // --GatherScatterLoadBalance: ALU data handle implementation for user defined load balance
596  //
598  template <class GridType, class LoadBalanceHandleType, bool useExternal>
599  class GatherScatterLoadBalance : public GatherScatter
600  {
601  protected:
602  typedef typename GridType::MPICommunicatorType Comm;
603 
605  typedef typename ImplTraits::template Codim< GridType::dimension, 0 >::InterfaceType HElementType;
606 
607  typedef typename GridType :: template Codim< 0 > :: Entity EntityType ;
608  typedef typename GridType :: template Codim< 0 > :: EntityImp EntityImpType ;
609 
610  template < bool useHandlerOpts, typename D = void>
612  {
613  bool importRank( const LoadBalanceHandleType &lb,
614  std::set<int>& ranks ) const
615  {
616  return lb.importRanks( ranks );
617  }
618  int destination( const LoadBalanceHandleType &lb,
619  const EntityType& entity ) const
620  {
621  return lb( entity );
622  }
623  int loadWeight( const LoadBalanceHandleType &lb,
624  const EntityType& entity ) const
625  {
626  return lb( entity );
627  }
628  };
629  template <typename D>
630  struct UseExternalHandlerOpts< false, D >
631  {
632  bool importRank( const LoadBalanceHandleType &lb,
633  std::set<int>& ranks ) const
634  {
635  return false;
636  }
637  int destination( const LoadBalanceHandleType &lb,
638  const EntityType& entity ) const
639  {
640  std::abort();
641  return -1;
642  }
643  int loadWeight( const LoadBalanceHandleType &lb,
644  const EntityType& entity ) const
645  {
646  std::abort();
647  return -1;
648  }
649  };
650 
651  private:
652  // no copying
654 
655  protected:
656  GridType & grid_;
657 
659 
660  // pointer to load balancing user interface (if NULL internal load balancing is used)
661  LoadBalanceHandleType* ldbHandle_;
662 
663  // true if userDefinedPartitioning is used, false if loadWeights is used
664  // both are disabled if ldbHandle_ is NULL
665 
666  public:
668  GatherScatterLoadBalance( GridType & grid,
669  LoadBalanceHandleType& ldb)
670  : grid_(grid),
671  entity_( EntityImpType() ),
672  ldbHandle_( &ldb )
673  {}
674 
676  explicit GatherScatterLoadBalance( GridType & grid )
677  : grid_(grid),
678  entity_( EntityImpType() ),
679  ldbHandle_( 0 )
680  {}
681 
682  // return false, since no user dataHandle is present
683  bool hasUserData() const { return false ; }
684 
685  // return true if user defined partitioning methods should be used
687  {
688  return useExternal && ldbHandle_ ;
689  }
690 
691  // return true if user defined load balancing weights are provided
693  {
694  return ! useExternal && ldbHandle_ ;
695  }
696 
697  // returns true if user defined partitioning needs to be readjusted
698  bool repartition ()
699  {
700  return userDefinedPartitioning(); // Note: user calls repartition() before calling loadBalance on the grid
701  }
702 
703  // return set of ranks data is imported from during load balance
704  // this method is only used for user defined repartitioning
705  bool importRanks( std::set<int>& ranks ) const
706  {
709  }
710 
711  // return set of ranks data is exported to during load balance
712  // this method is only used for user defined repartitioning
713  bool exportRanks( std::set<int>& ranks ) const
714  {
715  // NOTE: This feature is not yet include in the user interface
716  //alugrid_assert( userDefinedPartitioning() );
717  //return ldbHandle().exportRanks( ranks );
718  return false ;
719  }
720 
721  // return destination (i.e. rank) where the given element should be moved to
722  // this needs the methods userDefinedPartitioning to return true
723  int destination ( HElementType &elem )
724  {
725  // make sure userDefinedPartitioning is enabled
726  alugrid_assert ( elem.level () == 0 );
729  }
730 
731  // return load weight of given element
732  int loadWeight ( HElementType &elem )
733  {
734  // make sure userDefinedLoadWeights is enabled
736  alugrid_assert ( elem.level() == 0 );
737  static const bool useWeights = std::is_same<LoadBalanceHandleType, GatherScatter> :: value == false ;
739  }
740 
741  protected:
743  {
744  GridType::getRealImplementation( entity_ ).setElement( elem );
745  return entity_ ;
746  }
747 
748  LoadBalanceHandleType& ldbHandle()
749  {
751  return *ldbHandle_;
752  }
753 
754  const LoadBalanceHandleType& ldbHandle() const
755  {
757  return *ldbHandle_;
758  }
759 
760  };
761 
763  //
764  // --GatherScatterLoadBalance: ALU data handle implementation for CommDataHandleIF
765  //
767  template <class GridType, class LoadBalanceHandleType, class DataHandleImpl, class Data, bool useExternal>
769  : public GatherScatterLoadBalance< GridType, LoadBalanceHandleType, useExternal >
770  {
771  // no copying
773 
775  protected:
776  static const int dimension = GridType :: dimension ;
777  typedef typename GridType :: Traits :: HierarchicIterator HierarchicIterator;
778 
779  template< int codim >
780  struct Codim
781  {
782  typedef typename GridType :: Traits :: template Codim< codim > :: Entity Entity;
783  };
784 
785  typedef typename GridType::MPICommunicatorType Comm;
787  typedef typename ImplTraits::template Codim< GridType::dimension, 0 >::InterfaceType HElementType;
788 
790 
791  typedef Dune::CommDataHandleIF< DataHandleImpl, Data > DataHandleType;
792 
793  template <class DH, bool>
795  {
796  static DataHandleImpl& asImp( DH& dh ) { return static_cast<DataHandleImpl &> (dh); }
797 
798  static void reserveMemory( DH& dataHandle, const size_t newElements )
799  {
800  asImp( dataHandle ).reserveMemory( newElements );
801  }
802  static void compress( DH& dataHandle )
803  {
804  asImp( dataHandle ).compress();
805  }
806  };
807 
808  template <class DH>
809  struct CompressAndReserve< DH, false >
810  {
811  static void reserveMemory( DH& dataHandle, const size_t newElements ) {}
812  static void compress( DH& dataHandle ) {}
813  };
814 
815  // check whether DataHandleImpl is derived from LoadBalanceHandleWithReserveAndCompress
816  static const bool hasCompressAndReserve = std::is_base_of< LoadBalanceHandleWithReserveAndCompress, DataHandleImpl >::value;
817  // don't transmit size in case we have special DataHandleImpl
818  static const bool transmitSize = ! hasCompressAndReserve ;
819 
821 
822  // data handle (CommDataHandleIF)
824 
825  // used MessageBuffer
826  typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
827 
828  using BaseType :: grid_ ;
829  using BaseType :: setEntity ;
830  using BaseType :: entity_ ;
831 
832  // return true if maxLevel is the same on all cores
833  bool maxLevelConsistency() const
834  {
835  int maxLevel = grid_.maxLevel();
836  maxLevel = grid_.comm().max( maxLevel );
837  return maxLevel == grid_.maxLevel();
838  }
839  public:
842  DataHandleType& dh,
843  LoadBalanceHandleType& ldb)
844  : BaseType( grid, ldb ),
845  dataHandle_( dh )
846  {
848  }
849 
852  : BaseType( grid ),
853  dataHandle_( dh )
854  {
856  }
857 
858  // return true if dim,codim combination is contained in data set
859  bool contains(int dim, int cd) const
860  {
861  //dimension is GridImp::dimension
862  if(dim == dimension)
863  {
864  //the original call
865  return dataHandle_.contains(dim,cd);
866  }
867  //adaptation for 2d
868  else if(dimension == 2)
869  {
870  //we do not want to transmit edge data
871  if(cd == 2)
872  return false;
873  else if (cd == 3)
874  return dataHandle_.contains(dimension, 2);
875  else
876  return dataHandle_.contains(dimension, cd);
877  }
878  //
879  else
880  {
881  std::cerr << "DataHandle.contains called with non-matching dim and codim" << std::endl;
882  return false;
883  }
884  }
885 
886  // return true if user dataHandle is present which is the case here
887  bool hasUserData() const { return true ; }
888 
891  void inlineData ( ObjectStreamType & str , HElementType & elem, const int estimatedElements )
892  {
893  // store number of elements to be written (for restore)
894  str.write(estimatedElements);
895  // set element and then start
896  alugrid_assert ( elem.level () == 0 );
897 
898  // pack data for the whole hierarchy
899  inlineHierarchy( str, elem );
900  }
901 
904  void xtractData ( ObjectStreamType & str , HElementType & elem )
905  {
906  alugrid_assert ( elem.level () == 0 );
907 
908  // read number of elements to be restored
909  int newElements = 0 ;
910  str.read( newElements );
911 
912  // if data handle provides reserve feature, reserve memory
913  // the data handle has to be derived from LoadBalanceHandleWithReserveAndCompress
915 
916  // unpack data for the hierarchy
917  xtractHierarchy( str, elem );
918  }
919 
921  void compress ()
922  {
923  // if data handle provides compress, do compress here
924  // the data handle has to be derived from LoadBalanceHandleWithReserveAndCompress
926  }
927 
928  protected:
929  // inline data for the hierarchy
931  {
932  // pack elements data
933  inlineElementData( str, setEntity( elem ) );
934  // pack using deep first strategy
935  for( HElementType* son = elem.down(); son ; son = son->next() )
936  inlineHierarchy( str, *son );
937  }
938 
939  // inline data for the hierarchy
941  {
942  xtractElementData( str, setEntity( elem ) );
943  // reset element is new flag
944  elem.resetRefinedTag();
945  // unpack using deep first strategy
946  for( HElementType* son = elem.down(); son ; son = son->next() )
947  xtractHierarchy( str, *son );
948  }
949 
950  void inlineElementData ( ObjectStreamType &stream, const EntityType &element )
951  {
952  // call element data direct without creating entity pointer
953  if( dataHandle_.contains( dimension, 0 ) )
954  {
955  inlineEntityData<0>( stream, element );
956  }
957 
958  // now call all higher codims
959  inlineCodimData< 1 >( stream, element );
960  inlineCodimData< 2 >( stream, element );
961  if(dimension == 3)
962  inlineCodimData< dimension >( stream, element );
963  }
964 
965  void xtractElementData ( ObjectStreamType &stream, const EntityType &element )
966  {
967  // call element data direct without creating entity pointer
968  if( dataHandle_.contains( dimension, 0 ) )
969  {
970  xtractEntityData<0>( stream, element );
971  }
972 
973  // now call all higher codims
974  xtractCodimData< 1 >( stream, element );
975  xtractCodimData< 2 >( stream, element );
976  if(dimension == 3)
977  xtractCodimData< dimension >( stream, element );
978  }
979 
980  template <int codim>
981  int subEntities( const EntityType &element ) const
982  {
983  return element.subEntities( codim );
984  }
985 
986  template< int codim >
987  void inlineCodimData ( ObjectStreamType &stream, const EntityType &element ) const
988  {
989  if( dataHandle_.contains( dimension, codim ) )
990  {
991  const int numSubEntities = this->template subEntities< codim >( element );
992  for( int i = 0; i < numSubEntities; ++i )
993  {
994  inlineEntityData< codim >( stream, element.template subEntity< codim >( i ) );
995  }
996  }
997  }
998 
999  template< int codim >
1000  void xtractCodimData ( ObjectStreamType &stream, const EntityType &element )
1001  {
1002  if( dataHandle_.contains( dimension, codim ) )
1003  {
1004  const int numSubEntities = this->template subEntities< codim >( element );
1005  for( int i = 0; i < numSubEntities; ++i )
1006  {
1007  xtractEntityData< codim >( stream, element.template subEntity< codim >( i ) );
1008  }
1009  }
1010  }
1011 
1012  template< int codim >
1014  const typename Codim< codim > :: Entity &entity ) const
1015  {
1016  if( transmitSize )
1017  {
1018  const size_t size = dataHandle_.size( entity );
1019  stream.write( size );
1020  }
1021  dataHandle_.gather( stream, entity );
1022  }
1023 
1024  template< int codim >
1026  const typename Codim< codim > :: Entity &entity )
1027  {
1028  size_t size = 0;
1029  if( transmitSize )
1030  {
1031  stream.read( size );
1032  }
1033  dataHandle_.scatter( stream, entity, size );
1034  }
1035  };
1036 
1038  //
1039  // --AdaptRestrictProlong
1040  //
1042  template< class GridType, class AdaptDataHandle >
1044  : public AdaptRestrictProlongType
1045  {
1046  GridType & grid_;
1047  typedef typename GridType::template Codim<0>::Entity EntityType;
1048  typedef typename GridType::template Codim<0>::EntityImp RealEntityType;
1049 
1050  EntityType entity_;
1051 
1052  AdaptDataHandle &rp_;
1053 
1054  typedef typename GridType::MPICommunicatorType Comm;
1055 
1057  typedef typename ImplTraits::HElementType HElementType;
1058  typedef typename ImplTraits::HBndSegType HBndSegType;
1059  typedef typename ImplTraits::BNDFaceType BNDFaceType;
1060 
1061  //using AdaptRestrictProlongType :: postRefinement ;
1062  //using AdaptRestrictProlongType :: preCoarsening ;
1063 
1064  public:
1066  AdaptRestrictProlongImpl ( GridType &grid,
1067  AdaptDataHandle &rp )
1068  : grid_(grid)
1069  , entity_( RealEntityType() )
1070  , rp_(rp)
1071  {
1072  }
1073 
1075  {
1076  }
1077 
1079  int preCoarsening ( HElementType & father )
1080  {
1081  grid_.getRealImplementation( entity_ ).setElement( father );
1082  rp_.preCoarsening( entity_ );
1083 
1084  // reset refinement marker
1085  father.resetRefinedTag();
1086  return 0;
1087  }
1088 
1090  int postRefinement ( HElementType & father )
1091  {
1092  grid_.getRealImplementation( entity_ ).setElement( father );
1093  rp_.postRefinement( entity_ );
1094 
1095  // reset refinement markers
1096  father.resetRefinedTag();
1097  for( HElementType *son = father.down(); son ; son = son->next() )
1098  son->resetRefinedTag();
1099 
1100  return 0;
1101  }
1102 
1104  int preCoarsening ( HBndSegType & ghost ) { return 0; }
1105 
1106 
1108  int postRefinement ( HBndSegType & ghost ) { return 0; }
1109  };
1110 
1111 
1112 
1113  template< class GridType, class AdaptDataHandle, class GlobalIdSetImp >
1116  {
1118  GlobalIdSetImp & set_;
1119  typedef typename GridType::template Codim<0>::Entity EntityType;
1120  typedef typename GridType::template Codim<0>::EntityImp RealEntityType;
1121 
1122  typedef typename GridType::MPICommunicatorType Comm;
1123 
1125  typedef typename ImplTraits::HElementType HElementType;
1126  typedef typename ImplTraits::HBndSegType HBndSegType;
1127 
1128  using AdaptRestrictProlongType :: postRefinement ;
1129  using AdaptRestrictProlongType :: preCoarsening ;
1130 
1131  public:
1133  AdaptRestrictProlongGlSet ( GridType &grid,
1134  AdaptDataHandle &rp,
1135  GlobalIdSetImp & set )
1136  : BaseType( grid, rp ),
1137  set_( set )
1138  {}
1139 
1141 
1143  int postRefinement ( HElementType & elem )
1144  {
1145  set_.postRefinement( elem );
1146  return BaseType :: postRefinement(elem );
1147  }
1148  };
1149 
1150 } // namespace ALUGrid
1151 
1152 #endif // #ifndef DUNE_ALU3DGRIDDATAHANDLE_HH
#define alugrid_assert(EX)
Definition: alugrid_assert.hh:20
Definition: alu3dinclude.hh:50
Gitter::AdaptRestrictProlong AdaptRestrictProlongType
Definition: alu3dinclude.hh:52
Definition: alu3dinclude.hh:259
the corresponding interface class is defined in bsinclude.hh
Definition: datahandle.hh:26
virtual void setElement(const HElementType &elem)=0
ImplTraits::template Codim< dimension, codim >::ImplementationType ImplElementType
Definition: datahandle.hh:36
GridType::MPICommunicatorType Comm
Definition: datahandle.hh:33
const bool variableSize_
Definition: datahandle.hh:44
ImplTraits::template Codim< dimension, codim >::InterfaceType HElementType
Definition: datahandle.hh:37
GridType::template Codim< codim >::Entity EntityType
Definition: datahandle.hh:30
bool contains(int dim, int cd) const
Definition: datahandle.hh:68
Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits
Definition: datahandle.hh:35
EntityType & entity_
Definition: datahandle.hh:39
GatherScatter ::ObjectStreamType ObjectStreamType
Definition: datahandle.hh:46
RealEntityType & realEntity_
Definition: datahandle.hh:40
void recvData(ObjectStreamType &str, HElementType &elem)
read Data of one element from stream
Definition: datahandle.hh:136
void setData(ObjectStreamType &str, HElementType &elem)
Definition: datahandle.hh:102
DataCollectorType & dc_
Definition: datahandle.hh:42
GatherScatterBaseImpl(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc)
Constructor.
Definition: datahandle.hh:57
GridType::template Codim< codim >::EntityImp RealEntityType
Definition: datahandle.hh:31
const GridType & grid_
Definition: datahandle.hh:29
DataCollectorType::DataType DataType
Definition: datahandle.hh:48
size_t getSize(ObjectStreamType &str, EntityType &en)
Definition: datahandle.hh:146
@ dimension
Definition: datahandle.hh:28
void sendData(ObjectStreamType &str, HElementType &elem)
write Data of one element to stream
Definition: datahandle.hh:119
virtual bool containsItem(const HElementType &elem) const =0
RealEntityType & realEntity_
Definition: datahandle.hh:190
GatherScatterBaseImpl(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc)
Constructor.
Definition: datahandle.hh:210
size_t getSize(ObjectStreamType &str, EntityType &en)
Definition: datahandle.hh:280
const bool variableSize_
Definition: datahandle.hh:195
void sendData(ObjectStreamType &str, const HGhostType &ghost)
write Data of one ghost element to stream
Definition: datahandle.hh:244
const GridType & grid_
Definition: datahandle.hh:172
GridType::template Codim< 0 >::Entity EntityType
Definition: datahandle.hh:173
ImplTraits::template Codim< dim, codim >::GhostInterfaceType HGhostType
Definition: datahandle.hh:184
void recvData(ObjectStreamType &str, HGhostType &ghost)
read Data of one element from stream
Definition: datahandle.hh:268
ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType
Definition: datahandle.hh:182
Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits
Definition: datahandle.hh:178
virtual bool containsItem(const HElementType &elem) const
Definition: datahandle.hh:223
EntityType & entity_
Definition: datahandle.hh:189
GridType::template Codim< 0 >::EntityImp RealEntityType
Definition: datahandle.hh:174
ImplTraits::PllElementType PllElementType
Definition: datahandle.hh:187
ImplTraits::template Codim< dim, codim >::ImplementationType ImplElementType
Definition: datahandle.hh:179
GridType::MPICommunicatorType Comm
Definition: datahandle.hh:176
ImplTraits::template Codim< dim, codim >::InterfaceType HElementType
Definition: datahandle.hh:180
GatherScatter ::ObjectStreamType ObjectStreamType
Definition: datahandle.hh:198
virtual bool containsItem(const HGhostType &ghost) const =0
ImplTraits::template Codim< dim, codim >::GhostImplementationType ImplGhostType
Definition: datahandle.hh:185
void sendData(ObjectStreamType &str, const HElementType &elem)
write Data of one element to stream
Definition: datahandle.hh:232
void writeSize(ObjectStreamType &str, EntityType &en)
Definition: datahandle.hh:293
DataCollectorType & dc_
Definition: datahandle.hh:193
void recvData(ObjectStreamType &str, HElementType &elem)
read Data of one element from stream
Definition: datahandle.hh:258
bool contains(int dim, int codim) const
Definition: datahandle.hh:217
the corresponding interface class is defined in bsinclude.hh
Definition: datahandle.hh:307
GatherScatterLeafData(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc)
Constructor.
Definition: datahandle.hh:333
bool containsItem(const HGhostType &ghost) const
Definition: datahandle.hh:356
bool containsInterior(const HFaceType &face, PllElementType &pll) const
Definition: datahandle.hh:364
bool containsGhost(const HFaceType &face, PllElementType &pll) const
Definition: datahandle.hh:370
void setElement(const HElementType &elem)
Definition: datahandle.hh:376
bool containsItem(const HElementType &elem) const
Definition: datahandle.hh:350
the corresponding interface class is defined in bsinclude.hh
Definition: datahandle.hh:386
bool containsItem(const HElementType &elem) const
Definition: datahandle.hh:422
GatherScatterLevelData(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc, const LevelIndexSetImp &levelSet, const int level)
Constructor.
Definition: datahandle.hh:414
void setElement(const HElementType &elem)
Definition: datahandle.hh:428
the corresponding interface class is defined in bsinclude.hh
Definition: datahandle.hh:440
void setElement(const HElementType &elem)
Definition: datahandle.hh:497
bool containsItem(const HElementType &elem) const
Definition: datahandle.hh:485
GatherScatterNoData(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc)
Leaf Constructor.
Definition: datahandle.hh:478
GatherScatterNoData(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc, const LevelIndexSetImp &levelSet, const int level)
Level Constructor.
Definition: datahandle.hh:470
void setElement(const RealHElementType &elem)
Definition: datahandle.hh:505
bool containsItem(const RealHElementType &elem) const
Definition: datahandle.hh:491
GatherScatterLevelData(const GridType &grid, EntityType &en, RealEntityType &realEntity, DataCollectorType &dc, const LevelIndexSetImp &levelSet, const int level)
Constructor.
Definition: datahandle.hh:545
bool containsGhost(const HFaceType &face, PllElementType &pll) const
Definition: datahandle.hh:583
bool containsInterior(const HFaceType &face, PllElementType &pll) const
Definition: datahandle.hh:564
bool containsItem(const HGhostType &ghost) const
Definition: datahandle.hh:557
bool containsItem(const HElementType &elem) const
Definition: datahandle.hh:551
Definition: datahandle.hh:600
EntityType entity_
Definition: datahandle.hh:658
bool userDefinedLoadWeights() const
Definition: datahandle.hh:692
LoadBalanceHandleType * ldbHandle_
Definition: datahandle.hh:661
GridType ::template Codim< 0 >::Entity EntityType
Definition: datahandle.hh:607
bool userDefinedPartitioning() const
Definition: datahandle.hh:686
GridType & grid_
Definition: datahandle.hh:656
const LoadBalanceHandleType & ldbHandle() const
Definition: datahandle.hh:754
ImplTraits::template Codim< GridType::dimension, 0 >::InterfaceType HElementType
Definition: datahandle.hh:605
GatherScatterLoadBalance(GridType &grid)
Constructor.
Definition: datahandle.hh:676
Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits
Definition: datahandle.hh:604
int loadWeight(HElementType &elem)
Definition: datahandle.hh:732
GridType::MPICommunicatorType Comm
Definition: datahandle.hh:602
EntityType & setEntity(HElementType &elem)
Definition: datahandle.hh:742
GridType ::template Codim< 0 >::EntityImp EntityImpType
Definition: datahandle.hh:608
int destination(HElementType &elem)
Definition: datahandle.hh:723
bool exportRanks(std::set< int > &ranks) const
Definition: datahandle.hh:713
bool importRanks(std::set< int > &ranks) const
Definition: datahandle.hh:705
LoadBalanceHandleType & ldbHandle()
Definition: datahandle.hh:748
bool hasUserData() const
Definition: datahandle.hh:683
bool repartition()
Definition: datahandle.hh:698
GatherScatterLoadBalance(GridType &grid, LoadBalanceHandleType &ldb)
Constructor.
Definition: datahandle.hh:668
bool importRank(const LoadBalanceHandleType &lb, std::set< int > &ranks) const
Definition: datahandle.hh:613
int loadWeight(const LoadBalanceHandleType &lb, const EntityType &entity) const
Definition: datahandle.hh:623
int destination(const LoadBalanceHandleType &lb, const EntityType &entity) const
Definition: datahandle.hh:618
int loadWeight(const LoadBalanceHandleType &lb, const EntityType &entity) const
Definition: datahandle.hh:643
int destination(const LoadBalanceHandleType &lb, const EntityType &entity) const
Definition: datahandle.hh:637
bool importRank(const LoadBalanceHandleType &lb, std::set< int > &ranks) const
Definition: datahandle.hh:632
Definition: datahandle.hh:770
void inlineElementData(ObjectStreamType &stream, const EntityType &element)
Definition: datahandle.hh:950
GatherScatterLoadBalanceDataHandle(GridType &grid, DataHandleType &dh, LoadBalanceHandleType &ldb)
Constructor taking load balance handle and data handle.
Definition: datahandle.hh:841
GatherScatterLoadBalanceDataHandle(GridType &grid, DataHandleType &dh)
Constructor for DataHandle only.
Definition: datahandle.hh:851
void inlineHierarchy(ObjectStreamType &str, HElementType &elem)
Definition: datahandle.hh:930
void xtractCodimData(ObjectStreamType &stream, const EntityType &element)
Definition: datahandle.hh:1000
DataHandleType & dataHandle_
Definition: datahandle.hh:823
bool contains(int dim, int cd) const
Definition: datahandle.hh:859
ImplTraits::template Codim< GridType::dimension, 0 >::InterfaceType HElementType
Definition: datahandle.hh:787
int subEntities(const EntityType &element) const
Definition: datahandle.hh:981
Dune::CommDataHandleIF< DataHandleImpl, Data > DataHandleType
Definition: datahandle.hh:791
static const int dimension
Definition: datahandle.hh:776
void inlineCodimData(ObjectStreamType &stream, const EntityType &element) const
Definition: datahandle.hh:987
bool hasUserData() const
Definition: datahandle.hh:887
void compress()
call compress on data
Definition: datahandle.hh:921
void inlineEntityData(ObjectStreamType &stream, const typename Codim< codim > ::Entity &entity) const
Definition: datahandle.hh:1013
GridType ::Traits ::HierarchicIterator HierarchicIterator
Definition: datahandle.hh:777
CompressAndReserve< DataHandleType, hasCompressAndReserve > CompressAndReserveType
Definition: datahandle.hh:820
Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits
Definition: datahandle.hh:786
void xtractHierarchy(ObjectStreamType &str, HElementType &elem)
Definition: datahandle.hh:940
bool maxLevelConsistency() const
Definition: datahandle.hh:833
GatherScatter ::ObjectStreamType ObjectStreamType
Definition: datahandle.hh:826
void xtractElementData(ObjectStreamType &stream, const EntityType &element)
Definition: datahandle.hh:965
void xtractData(ObjectStreamType &str, HElementType &elem)
Definition: datahandle.hh:904
GridType::MPICommunicatorType Comm
Definition: datahandle.hh:785
BaseType ::EntityType EntityType
Definition: datahandle.hh:789
void xtractEntityData(ObjectStreamType &stream, const typename Codim< codim > ::Entity &entity)
Definition: datahandle.hh:1025
static const bool transmitSize
Definition: datahandle.hh:818
static const bool hasCompressAndReserve
Definition: datahandle.hh:816
void inlineData(ObjectStreamType &str, HElementType &elem, const int estimatedElements)
Definition: datahandle.hh:891
GridType ::Traits ::template Codim< codim >::Entity Entity
Definition: datahandle.hh:782
static void reserveMemory(DH &dataHandle, const size_t newElements)
Definition: datahandle.hh:798
static DataHandleImpl & asImp(DH &dh)
Definition: datahandle.hh:796
static void compress(DH &dataHandle)
Definition: datahandle.hh:802
static void reserveMemory(DH &dataHandle, const size_t newElements)
Definition: datahandle.hh:811
static void compress(DH &dataHandle)
Definition: datahandle.hh:812
Definition: datahandle.hh:1045
AdaptRestrictProlongImpl(GridType &grid, AdaptDataHandle &rp)
Constructor.
Definition: datahandle.hh:1066
int preCoarsening(HBndSegType &ghost)
restrict data for ghost elements
Definition: datahandle.hh:1104
int postRefinement(HElementType &father)
prolong data for elements
Definition: datahandle.hh:1090
int preCoarsening(HElementType &father)
restrict data for elements
Definition: datahandle.hh:1079
virtual ~AdaptRestrictProlongImpl()
Definition: datahandle.hh:1074
int postRefinement(HBndSegType &ghost)
prolong data for ghost elements
Definition: datahandle.hh:1108
Definition: datahandle.hh:1116
AdaptRestrictProlongGlSet(GridType &grid, AdaptDataHandle &rp, GlobalIdSetImp &set)
Constructor.
Definition: datahandle.hh:1133
virtual ~AdaptRestrictProlongGlSet()
Definition: datahandle.hh:1140
int postRefinement(HElementType &elem)
prolong data, elem is the father
Definition: datahandle.hh:1143