dune-grid  2.2.1
geometrygrid/grid.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOGRID_GRID_HH
2 #define DUNE_GEOGRID_GRID_HH
3 
4 #include <dune/common/nullptr.hh>
5 #include <dune/common/static_assert.hh>
6 
8 
15 
16 namespace Dune
17 {
18 
19  // DefaultCoordFunction
20  // --------------------
21 
22  template< class HostGrid >
24  : public IdenticalCoordFunction< typename HostGrid::ctype, HostGrid::dimensionworld >
25  {};
26 
27 
28 
29  // GeometryGrid
30  // ------------
31 
72  template< class HostGrid, class CoordFunction = DefaultCoordFunction< HostGrid >, class Allocator = std::allocator< void > >
76  < HostGrid::dimension, CoordFunction::dimRange, typename HostGrid::ctype,
77  GeoGrid::GridFamily< HostGrid, CoordFunction, Allocator > >,
78  public GeoGrid::ExportParams< HostGrid, CoordFunction >,
79  public GeoGrid::BackupRestoreFacilities< GeometryGrid< HostGrid, CoordFunction, Allocator > >
81  {
83 
85  < HostGrid::dimension, CoordFunction::dimRange, typename HostGrid::ctype,
87  Base;
88 
89  friend class GeoGrid::HierarchicIterator< const Grid >;
90 
91  template< int, class, bool > friend class GeoGrid::EntityBase;
92  template< class, bool > friend class GeoGrid::EntityPointer;
93  template< int, class > friend class GeoGrid::EntityProxy;
94  template< int, int, class > friend class GeoGrid::Geometry;
95  template< class, class > friend class GeoGrid::Intersection;
96  template< class > friend class GeoGrid::IntersectionIterator;
97  template< class, class > friend class GeoGrid::IdSet;
98  template< class, class > friend class GeoGrid::IndexSet;
99  template< class > friend struct HostGridAccess;
100 
101  template< class, class > friend class GeoGrid::CommDataHandle;
102 
103  template< int, PartitionIteratorType, class > friend struct GeoGrid::LevelIteratorTraits;
104  template< int, PartitionIteratorType, class > friend struct GeoGrid::LeafIteratorTraits;
105 
106  template< class, class, class > friend class PersistentContainer;
107 
108  public:
116 
117  typedef typename GridFamily::Traits Traits;
118 
125  template< int codim >
126  struct Codim;
127 
133 
134  typedef typename Traits::HierarchicIterator HierarchicIterator;
136  typedef typename Traits::LeafIntersectionIterator LeafIntersectionIterator;
138  typedef typename Traits::LevelIntersectionIterator LevelIntersectionIterator;
139 
153  typedef typename Traits::LeafIndexSet LeafIndexSet;
154 
163  typedef typename Traits::LevelIndexSet LevelIndexSet;
164 
175  typedef typename Traits::GlobalIdSet GlobalIdSet;
176 
192  typedef typename Traits::LocalIdSet LocalIdSet;
193 
199 
200  typedef typename Traits::ctype ctype;
201 
203  typedef typename Traits::CollectiveCommunication CollectiveCommunication;
204 
219  GeometryGrid ( HostGrid &hostGrid, CoordFunction &coordFunction, const Allocator &allocator = Allocator() )
220  : hostGrid_( &hostGrid ),
221  coordFunction_( coordFunction ),
222  removeHostGrid_( false ),
223  levelIndexSets_( hostGrid_->maxLevel()+1, nullptr, allocator ),
224  storageAllocator_( allocator )
225  {}
226 
236  GeometryGrid ( HostGrid *hostGrid, CoordFunction *coordFunction, const Allocator &allocator = Allocator() )
237  : hostGrid_( hostGrid ),
238  coordFunction_( *coordFunction ),
239  removeHostGrid_( true ),
240  levelIndexSets_( hostGrid_->maxLevel()+1, nullptr, allocator ),
241  storageAllocator_( allocator )
242  {}
243 
247  {
248  for( unsigned int i = 0; i < levelIndexSets_.size(); ++i )
249  {
250  if( levelIndexSets_[ i ] )
251  delete( levelIndexSets_[ i ] );
252  }
253 
254  if( removeHostGrid_ )
255  {
256  delete &coordFunction_;
257  delete hostGrid_;
258  }
259  }
260 
273  int maxLevel () const
274  {
275  return hostGrid().maxLevel();
276  }
277 
286  int size ( int level, int codim ) const
287  {
288  return hostGrid().size( level, codim );
289  }
290 
297  int size ( int codim ) const
298  {
299  return hostGrid().size( codim );
300  }
301 
310  int size ( int level, GeometryType type ) const
311  {
312  return hostGrid().size( level, type );
313  }
314 
319  int size ( GeometryType type ) const
320  {
321  return hostGrid().size( type );
322  }
323 
328  size_t numBoundarySegments () const
329  {
330  return hostGrid().numBoundarySegments( );
331  }
334  template< int codim >
335  typename Codim< codim >::LevelIterator lbegin ( int level ) const
336  {
337  return lbegin< codim, All_Partition >( level );
338  }
339 
340  template< int codim >
341  typename Codim< codim >::LevelIterator lend ( int level ) const
342  {
343  return lend< codim, All_Partition >( level );
344  }
345 
346  template< int codim, PartitionIteratorType pitype >
347  typename Codim< codim >::template Partition< pitype >::LevelIterator
348  lbegin ( int level ) const
349  {
350  typedef typename Traits::template Codim< codim >::template Partition< pitype >::LevelIteratorTraits IteratorTraits;
351  return GeoGrid::Iterator< IteratorTraits >( *this, level, IteratorTraits::begin );
352  }
353 
354  template< int codim, PartitionIteratorType pitype >
355  typename Codim< codim >::template Partition< pitype >::LevelIterator
356  lend ( int level ) const
357  {
358  typedef typename Traits::template Codim< codim >::template Partition< pitype >::LevelIteratorTraits IteratorTraits;
359  return GeoGrid::Iterator< IteratorTraits >( *this, level, IteratorTraits::end );
360  }
361 
362  template< int codim >
364  {
365  return leafbegin< codim, All_Partition >();
366  }
367 
368  template< int codim >
370  {
371  return leafend< codim, All_Partition >();
372  }
373 
374  template< int codim, PartitionIteratorType pitype >
375  typename Codim< codim >::template Partition< pitype >::LeafIterator
376  leafbegin () const
377  {
378  typedef typename Traits::template Codim< codim >::template Partition< pitype >::LeafIteratorTraits IteratorTraits;
379  return GeoGrid::Iterator< IteratorTraits >( *this, -1, IteratorTraits::begin );
380  }
381 
382  template< int codim, PartitionIteratorType pitype >
383  typename Codim< codim >::template Partition< pitype >::LeafIterator
384  leafend () const
385  {
386  typedef typename Traits::template Codim< codim >::template Partition< pitype >::LeafIteratorTraits IteratorTraits;
387  return GeoGrid::Iterator< IteratorTraits >( *this, -1, IteratorTraits::end );
388  }
389 
390  const GlobalIdSet &globalIdSet () const
391  {
392  if( !globalIdSet_ )
393  globalIdSet_ = GlobalIdSet( hostGrid().globalIdSet() );
394  assert( globalIdSet_ );
395  return globalIdSet_;
396  }
397 
398  const LocalIdSet &localIdSet () const
399  {
400  if( !localIdSet_ )
401  localIdSet_ = LocalIdSet( hostGrid().localIdSet() );
402  assert( localIdSet_ );
403  return localIdSet_;
404  }
405 
406  const LevelIndexSet &levelIndexSet ( int level ) const
407  {
408  assert( levelIndexSets_.size() == (size_t)(maxLevel()+1) );
409  if( (level < 0) || (level > maxLevel()) )
410  {
411  DUNE_THROW( GridError, "LevelIndexSet for nonexisting level " << level
412  << " requested." );
413  }
414 
415  LevelIndexSet *&levelIndexSet = levelIndexSets_[ level ];
416  if( !levelIndexSet )
417  levelIndexSet = new LevelIndexSet( hostGrid().levelIndexSet( level ) );
418  assert( levelIndexSet );
419  return *levelIndexSet;
420  }
421 
422  const LeafIndexSet &leafIndexSet () const
423  {
424  if( !leafIndexSet_ )
425  leafIndexSet_ = LeafIndexSet( hostGrid().leafIndexSet() );
426  assert( leafIndexSet_ );
427  return leafIndexSet_;
428  }
429 
430  void globalRefine ( int refCount )
431  {
432  hostGrid().globalRefine( refCount );
433  update();
434  }
435 
436  bool mark ( int refCount, const typename Codim< 0 >::Entity &entity )
437  {
438  return hostGrid().mark( refCount, getHostEntity< 0 >( entity ) );
439  }
440 
441  int getMark ( const typename Codim< 0 >::Entity &entity ) const
442  {
443  return hostGrid().getMark( getHostEntity< 0 >( entity ) );
444  }
445 
446  bool preAdapt ()
447  {
448  return hostGrid().preAdapt();
449  }
450 
451  bool adapt ()
452  {
453  bool ret = hostGrid().adapt();
454  update();
455  return ret;
456  }
457 
458  void postAdapt ()
459  {
460  hostGrid().postAdapt();
461  }
462 
470  int overlapSize ( int codim ) const
471  {
472  return hostGrid().overlapSize( codim );
473  }
474 
479  int ghostSize( int codim ) const
480  {
481  return hostGrid().ghostSize( codim );
482  }
483 
489  int overlapSize ( int level, int codim ) const
490  {
491  return hostGrid().overlapSize( level, codim );
492  }
493 
499  int ghostSize ( int level, int codim ) const
500  {
501  return hostGrid().ghostSize( level, codim );
502  }
503 
517  template< class DataHandle, class Data >
519  InterfaceType interface,
520  CommunicationDirection direction,
521  int level ) const
522  {
523  typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
524  typedef GeoGrid::CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
525 
526  WrappedDataHandle wrappedDataHandle( *this, datahandle );
527  hostGrid().communicate( wrappedDataHandle, interface, direction, level );
528  }
529 
542  template< class DataHandle, class Data >
544  InterfaceType interface,
545  CommunicationDirection direction ) const
546  {
547  typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
548  typedef GeoGrid::CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
549 
550  WrappedDataHandle wrappedDataHandle( *this, datahandle );
551  hostGrid().communicate( wrappedDataHandle, interface, direction );
552  }
553 
563  {
564  return hostGrid().comm();
565  }
566 
567 #if 0
568  // data handle interface different between geo and interface
569 
579  bool loadBalance ()
580  {
581  const bool gridChanged= hostGrid().loadBalance();
582  if( gridChanged )
583  update();
584  return gridChanged;
585  }
586 
602  template< class DataHandle, class Data >
603  bool loadBalance ( CommDataHandleIF< DataHandle, Data > &datahandle )
604  {
605  typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
606  typedef GeoGrid :: CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
607 
608  WrappedDataHandle wrappedDataHandle( *this, datahandle );
609  const bool gridChanged = hostGrid().loadBalance( wrappedDataHandle );
610  if( gridChanged )
611  update();
612  return gridChanged;
613  }
614 #endif
615 
617  template< class EntitySeed >
618  typename Traits::template Codim< EntitySeed::codimension >::EntityPointer
619  entityPointer ( const EntitySeed &seed ) const
620  {
621  typedef typename Traits::template Codim< EntitySeed::codimension >::EntityPointerImpl EntityPointerImpl;
622  return EntityPointerImpl( *this, seed );
623  }
624 
630  const HostGrid &hostGrid () const
631  {
632  return *hostGrid_;
633  }
634 
635  HostGrid &hostGrid ()
636  {
637  return *hostGrid_;
638  }
639 
648  void update ()
649  {
650  // adapt the coordinate function
652 
653  const int newNumLevels = maxLevel()+1;
654  const int oldNumLevels = levelIndexSets_.size();
655 
656  for( int i = newNumLevels; i < oldNumLevels; ++i )
657  {
658  if( levelIndexSets_[ i ] )
659  delete levelIndexSets_[ i ];
660  }
661  levelIndexSets_.resize( newNumLevels, nullptr );
662  }
663 
667 
668 protected:
669  const CoordFunction &coordFunction () const
670  {
671  return coordFunction_;
672  }
673 
674  template< int codim >
675  static const typename HostGrid::template Codim< codim >::Entity &
676  getHostEntity( const typename Codim< codim >::Entity &entity )
677  {
678  return getRealImplementation( entity ).hostEntity();
679  }
680 
681  template< int codim >
682  char *allocateMappingStorage ( const GeometryType &gt ) const
683  {
684  assert( gt.dim() == Traits::dimension - codim );
686  typedef typename MappingFamily::template Codim< codim >::MappingProvider MappingProvider;
687  return storageAllocator_.allocate( MappingProvider::mappingSize( gt.id() ) );
688  }
689 
690  template< int codim >
691  void deallocateMappingStorage ( const GeometryType &gt, char *p ) const
692  {
693  assert( gt.dim() == Traits::dimension - codim );
695  typedef typename MappingFamily::template Codim< codim >::MappingProvider MappingProvider;
696  storageAllocator_.deallocate( p, MappingProvider::mappingSize( gt.id() ) );
697  }
698 
699  private:
700  HostGrid *const hostGrid_;
701  CoordFunction &coordFunction_;
702  bool removeHostGrid_;
703  mutable std::vector< LevelIndexSet *, typename Allocator::template rebind< LevelIndexSet * >::other > levelIndexSets_;
704  mutable LeafIndexSet leafIndexSet_;
705  mutable GlobalIdSet globalIdSet_;
706  mutable LocalIdSet localIdSet_;
707  mutable typename Allocator::template rebind< char >::other storageAllocator_;
708  };
709 
710 
711 
712  // GeometryGrid::Codim
713  // -------------------
714 
715  template< class HostGrid, class CoordFunction, class Allocator >
716  template< int codim >
717  struct GeometryGrid< HostGrid, CoordFunction, Allocator >::Codim
718  : public Base::template Codim< codim >
719  {
727  typedef typename Traits::template Codim< codim >::Entity Entity;
728 
733  typedef typename Traits::template Codim< codim >::EntityPointer EntityPointer;
734 
748  typedef typename Traits::template Codim< codim >::Geometry Geometry;
749 
758  typedef typename Traits::template Codim< codim >::LocalGeometry LocalGeometry;
759 
765  template< PartitionIteratorType pitype >
766  struct Partition
767  {
768  typedef typename Traits::template Codim< codim >
771  typedef typename Traits::template Codim< codim >
774  };
775 
784 
793 
795  };
796 
797 } // namespace Dune
798 
799 #endif // #ifndef DUNE_GEOGRID_GRID_HH