dune-alugrid 3.0.0
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
19namespace 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:
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:
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
330 using BaseType :: containsItem ;
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
411 using BaseType :: containsItem ;
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
467 using BaseType :: containsItem ;
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),
672 ldbHandle_( &ldb )
673 {}
674
676 explicit GatherScatterLoadBalance( GridType & grid )
677 : grid_(grid),
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
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
724 {
725 // make sure userDefinedPartitioning is enabled
726 alugrid_assert ( elem.level () == 0 );
729 }
730
731 // return load weight of given element
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 typedef typename GridType :: Traits :: template Codim< codim > :: EntityPointer
785 };
786
787 typedef typename GridType::MPICommunicatorType Comm;
790
791 typedef typename BaseType :: EntityType EntityType ;
792
793 typedef Dune::CommDataHandleIF< DataHandleImpl, Data > DataHandleType;
794
795 template <class DH, bool>
797 {
798 static DataHandleImpl& asImp( DH& dh ) { return static_cast<DataHandleImpl &> (dh); }
799
800 static void reserveMemory( DH& dataHandle, const size_t newElements )
801 {
802 asImp( dataHandle ).reserveMemory( newElements );
803 }
804 static void compress( DH& dataHandle )
805 {
806 asImp( dataHandle ).compress();
807 }
808 };
809
810 template <class DH>
811 struct CompressAndReserve< DH, false >
812 {
813 static void reserveMemory( DH& dataHandle, const size_t newElements ) {}
814 static void compress( DH& dataHandle ) {}
815 };
816
817 // check whether DataHandleImpl is derived from LoadBalanceHandleWithReserveAndCompress
818 static const bool hasCompressAndReserve = std::is_base_of< LoadBalanceHandleWithReserveAndCompress, DataHandleImpl >::value;
819 // don't transmit size in case we have special DataHandleImpl
820 static const bool transmitSize = ! hasCompressAndReserve ;
821
823
824 // data handle (CommDataHandleIF)
826
827 // used MessageBuffer
828 typedef typename GatherScatter :: ObjectStreamType ObjectStreamType;
829
830 using BaseType :: grid_ ;
831 using BaseType :: setEntity ;
832 using BaseType :: entity_ ;
833
834 // return true if maxLevel is the same on all cores
836 {
837 int maxLevel = grid_.maxLevel();
838 maxLevel = grid_.comm().max( maxLevel );
839 return maxLevel == grid_.maxLevel();
840 }
841 public:
844 DataHandleType& dh,
845 LoadBalanceHandleType& ldb)
846 : BaseType( grid, ldb ),
847 dataHandle_( dh )
848 {
850 }
851
854 : BaseType( grid ),
855 dataHandle_( dh )
856 {
858 }
859
860 // return true if dim,codim combination is contained in data set
861 bool contains(int dim, int cd) const
862 {
863 //dimension is GridImp::dimension
864 if(dim == dimension)
865 {
866 //the original call
867 return dataHandle_.contains(dim,cd);
868 }
869 //adaptation for 2d
870 else if(dimension == 2)
871 {
872 //we do not want to transmit edge data
873 if(cd == 2)
874 return false;
875 else if (cd == 3)
876 return dataHandle_.contains(dimension, 2);
877 else
878 return dataHandle_.contains(dimension, cd);
879 }
880 //
881 else
882 {
883 std::cerr << "DataHandle.contains called with non-matching dim and codim" << std::endl;
884 return false;
885 }
886 }
887
888 // return true if user dataHandle is present which is the case here
889 bool hasUserData() const { return true ; }
890
893 void inlineData ( ObjectStreamType & str , HElementType & elem, const int estimatedElements )
894 {
895 // store number of elements to be written (for restore)
896 str.write(estimatedElements);
897 // set element and then start
898 alugrid_assert ( elem.level () == 0 );
899
900 // pack data for the whole hierarchy
901 inlineHierarchy( str, elem );
902 }
903
907 {
908 alugrid_assert ( elem.level () == 0 );
909
910 // read number of elements to be restored
911 int newElements = 0 ;
912 str.read( newElements );
913
914 // if data handle provides reserve feature, reserve memory
915 // the data handle has to be derived from LoadBalanceHandleWithReserveAndCompress
916 CompressAndReserveType :: reserveMemory( dataHandle_, newElements );
917
918 // unpack data for the hierarchy
919 xtractHierarchy( str, elem );
920 }
921
923 void compress ()
924 {
925 // if data handle provides compress, do compress here
926 // the data handle has to be derived from LoadBalanceHandleWithReserveAndCompress
927 CompressAndReserveType :: compress( dataHandle_ );
928 }
929
930 protected:
931 // inline data for the hierarchy
933 {
934 // pack elements data
935 inlineElementData( str, setEntity( elem ) );
936 // pack using deep first strategy
937 for( HElementType* son = elem.down(); son ; son = son->next() )
938 inlineHierarchy( str, *son );
939 }
940
941 // inline data for the hierarchy
943 {
944 xtractElementData( str, setEntity( elem ) );
945 // reset element is new flag
946 elem.resetRefinedTag();
947 // unpack using deep first strategy
948 for( HElementType* son = elem.down(); son ; son = son->next() )
949 xtractHierarchy( str, *son );
950 }
951
952 void inlineElementData ( ObjectStreamType &stream, const EntityType &element )
953 {
954 // call element data direct without creating entity pointer
955 if( dataHandle_.contains( dimension, 0 ) )
956 {
957 inlineEntityData<0>( stream, element );
958 }
959
960 // now call all higher codims
961 inlineCodimData< 1 >( stream, element );
962 inlineCodimData< 2 >( stream, element );
963 if(dimension == 3)
964 inlineCodimData< dimension >( stream, element );
965 }
966
967 void xtractElementData ( ObjectStreamType &stream, const EntityType &element )
968 {
969 // call element data direct without creating entity pointer
970 if( dataHandle_.contains( dimension, 0 ) )
971 {
972 xtractEntityData<0>( stream, element );
973 }
974
975 // now call all higher codims
976 xtractCodimData< 1 >( stream, element );
977 xtractCodimData< 2 >( stream, element );
978 if(dimension == 3)
979 xtractCodimData< dimension >( stream, element );
980 }
981
982 template <int codim>
983 int subEntities( const EntityType &element ) const
984 {
985#if DUNE_VERSION_NEWER_REV(DUNE_GRID,2,4,0)
986 return element.subEntities( codim );
987#else
988 return element.template count< codim > ();
989#endif
990 }
991
992 template< int codim >
993 void inlineCodimData ( ObjectStreamType &stream, const EntityType &element ) const
994 {
995 if( dataHandle_.contains( dimension, codim ) )
996 {
997 const int numSubEntities = this->template subEntities< codim >( element );
998 for( int i = 0; i < numSubEntities; ++i )
999 {
1000#if DUNE_VERSION_NEWER(DUNE_GRID,2,4)
1001 inlineEntityData< codim >( stream, element.template subEntity< codim >( i ) );
1002#else
1003 typedef typename Codim< codim > :: EntityPointer EntityPointer;
1004 const EntityPointer pEntity = element.template subEntity< codim >( i );
1005 inlineEntityData< codim >( stream, *pEntity );
1006#endif
1007 }
1008 }
1009 }
1010
1011 template< int codim >
1012 void xtractCodimData ( ObjectStreamType &stream, const EntityType &element )
1013 {
1014 if( dataHandle_.contains( dimension, codim ) )
1015 {
1016 const int numSubEntities = this->template subEntities< codim >( element );
1017 for( int i = 0; i < numSubEntities; ++i )
1018 {
1019#if DUNE_VERSION_NEWER(DUNE_GRID,2,4)
1020 xtractEntityData< codim >( stream, element.template subEntity< codim >( i ) );
1021#else
1022 typedef typename Codim< codim > :: EntityPointer EntityPointer;
1023 const EntityPointer pEntity = element.template subEntity< codim >( i );
1024 xtractEntityData< codim >( stream, *pEntity );
1025#endif
1026 }
1027 }
1028 }
1029
1030 template< int codim >
1032 const typename Codim< codim > :: Entity &entity ) const
1033 {
1034 if( transmitSize )
1035 {
1036 const size_t size = dataHandle_.size( entity );
1037 stream.write( size );
1038 }
1039 dataHandle_.gather( stream, entity );
1040 }
1041
1042 template< int codim >
1044 const typename Codim< codim > :: Entity &entity )
1045 {
1046 size_t size = 0;
1047 if( transmitSize )
1048 {
1049 stream.read( size );
1050 }
1051 dataHandle_.scatter( stream, entity, size );
1052 }
1053 };
1054
1056 //
1057 // --AdaptRestrictProlong
1058 //
1060 template< class GridType, class AdaptDataHandle >
1063 {
1064 GridType & grid_;
1065 typedef typename GridType::template Codim<0>::Entity EntityType;
1066 typedef typename GridType::template Codim<0>::EntityImp RealEntityType;
1067
1068 EntityType entity_;
1069
1070 AdaptDataHandle &rp_;
1071
1072 typedef typename GridType::MPICommunicatorType Comm;
1073
1075 typedef typename ImplTraits::HElementType HElementType;
1076 typedef typename ImplTraits::HBndSegType HBndSegType;
1077 typedef typename ImplTraits::BNDFaceType BNDFaceType;
1078
1079 //using AdaptRestrictProlongType :: postRefinement ;
1080 //using AdaptRestrictProlongType :: preCoarsening ;
1081
1082 public:
1085 AdaptDataHandle &rp )
1086 : grid_(grid)
1087 , entity_( RealEntityType() )
1088 , rp_(rp)
1089 {
1090 }
1091
1093 {
1094 }
1095
1097 int preCoarsening ( HElementType & father )
1098 {
1099 grid_.getRealImplementation( entity_ ).setElement( father );
1100 rp_.preCoarsening( entity_ );
1101
1102 // reset refinement marker
1103 father.resetRefinedTag();
1104 return 0;
1105 }
1106
1108 int postRefinement ( HElementType & father )
1109 {
1110 grid_.getRealImplementation( entity_ ).setElement( father );
1111 rp_.postRefinement( entity_ );
1112
1113 // reset refinement markers
1114 father.resetRefinedTag();
1115 for( HElementType *son = father.down(); son ; son = son->next() )
1116 son->resetRefinedTag();
1117
1118 return 0;
1119 }
1120
1122 int preCoarsening ( HBndSegType & ghost ) { return 0; }
1123
1124
1126 int postRefinement ( HBndSegType & ghost ) { return 0; }
1127 };
1128
1129
1130
1131 template< class GridType, class AdaptDataHandle, class GlobalIdSetImp >
1134 {
1136 GlobalIdSetImp & set_;
1137 typedef typename GridType::template Codim<0>::Entity EntityType;
1138 typedef typename GridType::template Codim<0>::EntityImp RealEntityType;
1139
1140 typedef typename GridType::MPICommunicatorType Comm;
1141
1143 typedef typename ImplTraits::HElementType HElementType;
1144 typedef typename ImplTraits::HBndSegType HBndSegType;
1145
1146 using AdaptRestrictProlongType :: postRefinement ;
1147 using AdaptRestrictProlongType :: preCoarsening ;
1148
1149 public:
1152 AdaptDataHandle &rp,
1153 GlobalIdSetImp & set )
1154 : BaseType( grid, rp ),
1155 set_( set )
1156 {}
1157
1159
1161 int postRefinement ( HElementType & elem )
1162 {
1163 set_.postRefinement( elem );
1164 return BaseType :: postRefinement(elem );
1165 }
1166 };
1167
1168} // namespace ALUGrid
1169
1170#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
DataCollectorType::DataType DataType
Definition datahandle.hh:48
GridType::MPICommunicatorType Comm
Definition datahandle.hh:33
const bool variableSize_
Definition datahandle.hh:44
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
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
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
ImplTraits::template Codim< dimension, codim >::ImplementationType ImplElementType
Definition datahandle.hh:36
virtual bool containsItem(const HElementType &elem) const =0
ImplTraits::template Codim< dimension, codim >::InterfaceType HElementType
Definition datahandle.hh:37
GatherScatter::ObjectStreamType ObjectStreamType
Definition datahandle.hh:46
ImplTraits::template Codim< dim, codim >::GhostImplementationType ImplGhostType
Definition datahandle.hh:185
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
void sendData(ObjectStreamType &str, const HGhostType &ghost)
write Data of one ghost element to stream
Definition datahandle.hh:244
ImplTraits::template Codim< dim, codim >::InterfaceType HElementType
Definition datahandle.hh:180
const GridType & grid_
Definition datahandle.hh:172
GridType::template Codim< 0 >::Entity EntityType
Definition datahandle.hh:173
void recvData(ObjectStreamType &str, HGhostType &ghost)
read Data of one element from stream
Definition datahandle.hh:268
ImplTraits::template Codim< dim, codim >::ImplementationType ImplElementType
Definition datahandle.hh:179
Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits
Definition datahandle.hh:178
virtual bool containsItem(const HElementType &elem) const
Definition datahandle.hh:223
GridType::template Codim< 0 >::EntityImp RealEntityType
Definition datahandle.hh:174
ImplTraits::PllElementType PllElementType
Definition datahandle.hh:187
GridType::MPICommunicatorType Comm
Definition datahandle.hh:176
GatherScatter::ObjectStreamType ObjectStreamType
Definition datahandle.hh:198
virtual bool containsItem(const HGhostType &ghost) const =0
ImplTraits::template Codim< dim, codim >::GhostInterfaceType HGhostType
Definition datahandle.hh:184
ImplTraits::template Codim< dim, 1 >::InterfaceType HFaceType
Definition datahandle.hh:182
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
GridType::template Codim< 0 >::Entity EntityType
Definition datahandle.hh:607
ImplTraits::template Codim< GridType::dimension, 0 >::InterfaceType HElementType
Definition datahandle.hh:605
EntityType entity_
Definition datahandle.hh:658
EntityType & setEntity(HElementType &elem)
Definition datahandle.hh:742
GridType::template Codim< 0 >::EntityImp EntityImpType
Definition datahandle.hh:608
LoadBalanceHandleType & ldbHandle()
Definition datahandle.hh:748
const LoadBalanceHandleType & ldbHandle() const
Definition datahandle.hh:754
bool userDefinedLoadWeights() const
Definition datahandle.hh:692
LoadBalanceHandleType * ldbHandle_
Definition datahandle.hh:661
bool userDefinedPartitioning() const
Definition datahandle.hh:686
GridType & grid_
Definition datahandle.hh:656
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
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
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
void inlineElementData(ObjectStreamType &stream, const EntityType &element)
Definition datahandle.hh:952
GatherScatterLoadBalanceDataHandle(GridType &grid, DataHandleType &dh, LoadBalanceHandleType &ldb)
Constructor taking load balance handle and data handle.
Definition datahandle.hh:843
GatherScatterLoadBalanceDataHandle(GridType &grid, DataHandleType &dh)
Constructor for DataHandle only.
Definition datahandle.hh:853
void inlineHierarchy(ObjectStreamType &str, HElementType &elem)
Definition datahandle.hh:932
void xtractCodimData(ObjectStreamType &stream, const EntityType &element)
Definition datahandle.hh:1012
DataHandleType & dataHandle_
Definition datahandle.hh:825
bool contains(int dim, int cd) const
Definition datahandle.hh:861
int subEntities(const EntityType &element) const
Definition datahandle.hh:983
ImplTraits::template Codim< GridType::dimension, 0 >::InterfaceType HElementType
Definition datahandle.hh:789
Dune::CommDataHandleIF< DataHandleImpl, Data > DataHandleType
Definition datahandle.hh:793
GatherScatter::ObjectStreamType ObjectStreamType
Definition datahandle.hh:828
static const int dimension
Definition datahandle.hh:776
void inlineCodimData(ObjectStreamType &stream, const EntityType &element) const
Definition datahandle.hh:993
BaseType::EntityType EntityType
Definition datahandle.hh:791
bool hasUserData() const
Definition datahandle.hh:889
void compress()
call compress on data
Definition datahandle.hh:923
void inlineEntityData(ObjectStreamType &stream, const typename Codim< codim > ::Entity &entity) const
Definition datahandle.hh:1031
CompressAndReserve< DataHandleType, hasCompressAndReserve > CompressAndReserveType
Definition datahandle.hh:822
Dune::ALU3dImplTraits< GridType::elementType, Comm > ImplTraits
Definition datahandle.hh:788
void xtractHierarchy(ObjectStreamType &str, HElementType &elem)
Definition datahandle.hh:942
bool maxLevelConsistency() const
Definition datahandle.hh:835
void xtractElementData(ObjectStreamType &stream, const EntityType &element)
Definition datahandle.hh:967
void xtractData(ObjectStreamType &str, HElementType &elem)
Definition datahandle.hh:906
GridType::MPICommunicatorType Comm
Definition datahandle.hh:787
void xtractEntityData(ObjectStreamType &stream, const typename Codim< codim > ::Entity &entity)
Definition datahandle.hh:1043
static const bool transmitSize
Definition datahandle.hh:820
static const bool hasCompressAndReserve
Definition datahandle.hh:818
GridType::Traits::HierarchicIterator HierarchicIterator
Definition datahandle.hh:777
void inlineData(ObjectStreamType &str, HElementType &elem, const int estimatedElements)
Definition datahandle.hh:893
GridType::Traits::template Codim< codim >::Entity Entity
Definition datahandle.hh:782
GridType::Traits::template Codim< codim >::EntityPointer EntityPointer
Definition datahandle.hh:784
static void reserveMemory(DH &dataHandle, const size_t newElements)
Definition datahandle.hh:800
static DataHandleImpl & asImp(DH &dh)
Definition datahandle.hh:798
static void compress(DH &dataHandle)
Definition datahandle.hh:804
static void reserveMemory(DH &dataHandle, const size_t newElements)
Definition datahandle.hh:813
static void compress(DH &dataHandle)
Definition datahandle.hh:814
Definition datahandle.hh:1063
AdaptRestrictProlongImpl(GridType &grid, AdaptDataHandle &rp)
Constructor.
Definition datahandle.hh:1084
int preCoarsening(HBndSegType &ghost)
restrict data for ghost elements
Definition datahandle.hh:1122
int postRefinement(HElementType &father)
prolong data for elements
Definition datahandle.hh:1108
int preCoarsening(HElementType &father)
restrict data for elements
Definition datahandle.hh:1097
virtual ~AdaptRestrictProlongImpl()
Definition datahandle.hh:1092
int postRefinement(HBndSegType &ghost)
prolong data for ghost elements
Definition datahandle.hh:1126
Definition datahandle.hh:1134
AdaptRestrictProlongGlSet(GridType &grid, AdaptDataHandle &rp, GlobalIdSetImp &set)
Constructor.
Definition datahandle.hh:1151
virtual ~AdaptRestrictProlongGlSet()
Definition datahandle.hh:1158
int postRefinement(HElementType &elem)
prolong data, elem is the father
Definition datahandle.hh:1161