Dune Core Modules (2.9.0)

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