dune-grid  2.3.1
geometrygrid/grid.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GEOGRID_GRID_HH
4 #define DUNE_GEOGRID_GRID_HH
5 
6 #include <dune/common/nullptr.hh>
7 #include <dune/common/static_assert.hh>
8 
10 
17 
18 namespace Dune
19 {
20 
21  // DefaultCoordFunction
22  // --------------------
23 
24  template< class HostGrid >
26  : public IdenticalCoordFunction< typename HostGrid::ctype, HostGrid::dimensionworld >
27  {};
28 
29 
30 
31  // GeometryGrid
32  // ------------
33 
74  template< class HostGrid, class CoordFunction = DefaultCoordFunction< HostGrid >, class Allocator = std::allocator< void > >
78  < HostGrid::dimension, CoordFunction::dimRange, typename HostGrid::ctype,
79  GeoGrid::GridFamily< HostGrid, CoordFunction, Allocator > >,
80  public GeoGrid::ExportParams< HostGrid, CoordFunction >,
81  public GeoGrid::BackupRestoreFacilities< GeometryGrid< HostGrid, CoordFunction, Allocator > >
83  {
85 
87  < HostGrid::dimension, CoordFunction::dimRange, typename HostGrid::ctype,
89  Base;
90 
91  friend class GeoGrid::HierarchicIterator< const Grid >;
92 
93  template< int, class, bool > friend class GeoGrid::EntityBase;
94  template< class, bool > friend class GeoGrid::EntityPointer;
95  template< int, class > friend class GeoGrid::EntityProxy;
96  template< int, int, class > friend class GeoGrid::Geometry;
97  template< class, class, class, PartitionIteratorType > friend class GeoGrid::GridView;
98  template< class, class > friend class GeoGrid::Intersection;
99  template< class, class > friend class GeoGrid::IntersectionIterator;
100  template< class, class > friend class GeoGrid::IdSet;
101  template< class, class > friend class GeoGrid::IndexSet;
102  template< class > friend struct HostGridAccess;
103 
104  template< class, class > friend class GeoGrid::CommDataHandle;
105 
106  public:
114 
115  typedef typename GridFamily::Traits Traits;
116 
123  template< int codim >
124  struct Codim;
125 
131 
132  typedef typename Traits::HierarchicIterator HierarchicIterator;
134  typedef typename Traits::LeafIntersectionIterator LeafIntersectionIterator;
136  typedef typename Traits::LevelIntersectionIterator LevelIntersectionIterator;
137 
144  template< PartitionIteratorType pitype >
145  struct Partition
146  {
147  typedef typename GridFamily::Traits::template Partition< pitype >::LevelGridView
149  typedef typename GridFamily::Traits::template Partition< pitype >::LeafGridView
151  };
152 
156 
171  typedef typename Traits::LeafIndexSet LeafIndexSet;
172 
181  typedef typename Traits::LevelIndexSet LevelIndexSet;
182 
193  typedef typename Traits::GlobalIdSet GlobalIdSet;
194 
210  typedef typename Traits::LocalIdSet LocalIdSet;
211 
217 
218  typedef typename Traits::ctype ctype;
219 
221  typedef typename Traits::CollectiveCommunication CollectiveCommunication;
222 
237  GeometryGrid ( HostGrid &hostGrid, CoordFunction &coordFunction, const Allocator &allocator = Allocator() )
238  : hostGrid_( &hostGrid ),
239  coordFunction_( coordFunction ),
240  removeHostGrid_( false ),
241  levelIndexSets_( hostGrid_->maxLevel()+1, nullptr, allocator ),
242  storageAllocator_( allocator )
243  {}
244 
254  GeometryGrid ( HostGrid *hostGrid, CoordFunction *coordFunction, const Allocator &allocator = Allocator() )
255  : hostGrid_( hostGrid ),
256  coordFunction_( *coordFunction ),
257  removeHostGrid_( true ),
258  levelIndexSets_( hostGrid_->maxLevel()+1, nullptr, allocator ),
259  storageAllocator_( allocator )
260  {}
261 
265  {
266  for( unsigned int i = 0; i < levelIndexSets_.size(); ++i )
267  {
268  if( levelIndexSets_[ i ] )
269  delete( levelIndexSets_[ i ] );
270  }
271 
272  if( removeHostGrid_ )
273  {
274  delete &coordFunction_;
275  delete hostGrid_;
276  }
277  }
278 
291  int maxLevel () const
292  {
293  return hostGrid().maxLevel();
294  }
295 
304  int size ( int level, int codim ) const
305  {
306  return levelView( level ).size( codim );
307  }
308 
315  int size ( int codim ) const
316  {
317  return leafView().size( codim );
318  }
319 
328  int size ( int level, GeometryType type ) const
329  {
330  return levelView( level ).size( type );
331  }
332 
337  int size ( GeometryType type ) const
338  {
339  return leafView().size( type );
340  }
341 
346  size_t numBoundarySegments () const
347  {
348  return hostGrid().numBoundarySegments( );
349  }
352  template< int codim >
353  typename Codim< codim >::LevelIterator lbegin ( int level ) const
354  {
355  return levelView( level ).template begin< codim >();
356  }
357 
358  template< int codim >
359  typename Codim< codim >::LevelIterator lend ( int level ) const
360  {
361  return levelView( level ).template end< codim >();
362  }
363 
364  template< int codim, PartitionIteratorType pitype >
365  typename Codim< codim >::template Partition< pitype >::LevelIterator
366  lbegin ( int level ) const
367  {
368  return levelView( level ).template begin< codim, pitype >();
369  }
370 
371  template< int codim, PartitionIteratorType pitype >
372  typename Codim< codim >::template Partition< pitype >::LevelIterator
373  lend ( int level ) const
374  {
375  return levelView( level ).template end< codim, pitype >();
376  }
377 
378  template< int codim >
380  {
381  return leafView().template begin< codim >();
382  }
383 
384  template< int codim >
386  {
387  return leafView().template end< codim >();
388  }
389 
390  template< int codim, PartitionIteratorType pitype >
391  typename Codim< codim >::template Partition< pitype >::LeafIterator
392  leafbegin () const
393  {
394  return leafView().template begin< codim, pitype >();
395  }
396 
397  template< int codim, PartitionIteratorType pitype >
398  typename Codim< codim >::template Partition< pitype >::LeafIterator
399  leafend () const
400  {
401  return leafView().template end< codim, pitype >();
402  }
403 
404  const GlobalIdSet &globalIdSet () const
405  {
406  if( !globalIdSet_ )
407  globalIdSet_ = GlobalIdSet( hostGrid().globalIdSet() );
408  assert( globalIdSet_ );
409  return globalIdSet_;
410  }
411 
412  const LocalIdSet &localIdSet () const
413  {
414  if( !localIdSet_ )
415  localIdSet_ = LocalIdSet( hostGrid().localIdSet() );
416  assert( localIdSet_ );
417  return localIdSet_;
418  }
419 
420  const LevelIndexSet &levelIndexSet ( int level ) const
421  {
422  assert( levelIndexSets_.size() == (size_t)(maxLevel()+1) );
423  if( (level < 0) || (level > maxLevel()) )
424  {
425  DUNE_THROW( GridError, "LevelIndexSet for nonexisting level " << level
426  << " requested." );
427  }
428 
429  LevelIndexSet *&levelIndexSet = levelIndexSets_[ level ];
430  if( !levelIndexSet )
431  levelIndexSet = new LevelIndexSet( hostGrid().levelIndexSet( level ) );
432  assert( levelIndexSet );
433  return *levelIndexSet;
434  }
435 
436  const LeafIndexSet &leafIndexSet () const
437  {
438  if( !leafIndexSet_ )
439  leafIndexSet_ = LeafIndexSet( hostGrid().leafIndexSet() );
440  assert( leafIndexSet_ );
441  return leafIndexSet_;
442  }
443 
444  void globalRefine ( int refCount )
445  {
446  hostGrid().globalRefine( refCount );
447  update();
448  }
449 
450  bool mark ( int refCount, const typename Codim< 0 >::Entity &entity )
451  {
452  return hostGrid().mark( refCount, getHostEntity< 0 >( entity ) );
453  }
454 
455  int getMark ( const typename Codim< 0 >::Entity &entity ) const
456  {
457  return hostGrid().getMark( getHostEntity< 0 >( entity ) );
458  }
459 
460  bool preAdapt ()
461  {
462  return hostGrid().preAdapt();
463  }
464 
465  bool adapt ()
466  {
467  bool ret = hostGrid().adapt();
468  update();
469  return ret;
470  }
471 
472  void postAdapt ()
473  {
474  hostGrid().postAdapt();
475  }
476 
484  int overlapSize ( int codim ) const
485  {
486  return leafView().overlapSize( codim );
487  }
488 
493  int ghostSize( int codim ) const
494  {
495  return leafView().ghostSize( codim );
496  }
497 
503  int overlapSize ( int level, int codim ) const
504  {
505  return levelView( level ).overlapSize( codim );
506  }
507 
513  int ghostSize ( int level, int codim ) const
514  {
515  return levelView( level ).ghostSize( codim );
516  }
517 
531  template< class DataHandle, class Data >
533  InterfaceType interface,
534  CommunicationDirection direction,
535  int level ) const
536  {
537  levelView( level ).communicate( dataHandle, interface, direction );
538  }
539 
552  template< class DataHandle, class Data >
554  InterfaceType interface,
555  CommunicationDirection direction ) const
556  {
557  leafView().communicate( dataHandle, interface, direction );
558  }
559 
569  {
570  return hostGrid().comm();
571  }
572 
573 #if 0
574  // data handle interface different between geo and interface
575 
585  bool loadBalance ()
586  {
587  const bool gridChanged= hostGrid().loadBalance();
588  if( gridChanged )
589  update();
590  return gridChanged;
591  }
592 
608  template< class DataHandle, class Data >
609  bool loadBalance ( CommDataHandleIF< DataHandle, Data > &datahandle )
610  {
611  typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
612  typedef GeoGrid :: CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
613 
614  WrappedDataHandle wrappedDataHandle( *this, datahandle );
615  const bool gridChanged = hostGrid().loadBalance( wrappedDataHandle );
616  if( gridChanged )
617  update();
618  return gridChanged;
619  }
620 #endif
621 
623  template< class EntitySeed >
624  typename Traits::template Codim< EntitySeed::codimension >::EntityPointer
625  entityPointer ( const EntitySeed &seed ) const
626  {
627  typedef typename Traits::template Codim< EntitySeed::codimension >::EntityPointerImpl EntityPointerImpl;
628  return EntityPointerImpl( *this, seed );
629  }
630 
637  template< PartitionIteratorType pitype >
638  typename Partition< pitype >::LevelGridView levelView ( int level ) const
639  {
640  typedef typename Partition< pitype >::LevelGridView View;
641  typedef typename View::GridViewImp ViewImp;
642  return View( ViewImp( *this, hostGrid().levelView( level ) ) );
643  }
644 
645  template< PartitionIteratorType pitype >
647  {
648  return levelView<pitype>(level);
649  }
650 
652  template< PartitionIteratorType pitype >
654  {
655  typedef typename Traits::template Partition< pitype >::LeafGridView View;
656  typedef typename View::GridViewImp ViewImp;
657  return View( ViewImp( *this, hostGrid().leafView() ) );
658  }
659 
660  template< PartitionIteratorType pitype >
662  {
663  return leafGridView<pitype>();
664  }
665 
667  LevelGridView levelView ( int level ) const
668  {
669  typedef typename LevelGridView::GridViewImp ViewImp;
670  return LevelGridView( ViewImp( *this, hostGrid().levelView( level ) ) );
671  }
672 
673 
674  LevelGridView levelGridView ( int level ) const
675  {
676  return levelView(level);
677  }
678 
681  {
682  typedef typename LeafGridView::GridViewImp ViewImp;
683  return LeafGridView( ViewImp( *this, hostGrid().leafView() ) );
684  }
685 
687  {
688  return leafView();
689  }
690 
696  const HostGrid &hostGrid () const
697  {
698  return *hostGrid_;
699  }
700 
701  HostGrid &hostGrid ()
702  {
703  return *hostGrid_;
704  }
705 
714  void update ()
715  {
716  // adapt the coordinate function
718 
719  const int newNumLevels = maxLevel()+1;
720  const int oldNumLevels = levelIndexSets_.size();
721 
722  for( int i = newNumLevels; i < oldNumLevels; ++i )
723  {
724  if( levelIndexSets_[ i ] )
725  delete levelIndexSets_[ i ];
726  }
727  levelIndexSets_.resize( newNumLevels, nullptr );
728  }
729 
733 
734  protected:
735  const CoordFunction &coordFunction () const
736  {
737  return coordFunction_;
738  }
739 
740  template< int codim >
741  static const typename HostGrid::template Codim< codim >::Entity &
742  getHostEntity( const typename Codim< codim >::Entity &entity )
743  {
744  return getRealImplementation( entity ).hostEntity();
745  }
746 
747  void *allocateStorage ( std::size_t size ) const
748  {
749  return storageAllocator_.allocate( size );
750  }
751 
752  void deallocateStorage ( void *p, std::size_t size ) const
753  {
754  storageAllocator_.deallocate( (char *)p, size );
755  }
756 
757  private:
758  HostGrid *const hostGrid_;
759  CoordFunction &coordFunction_;
760  bool removeHostGrid_;
761  mutable std::vector< LevelIndexSet *, typename Allocator::template rebind< LevelIndexSet * >::other > levelIndexSets_;
762  mutable LeafIndexSet leafIndexSet_;
763  mutable GlobalIdSet globalIdSet_;
764  mutable LocalIdSet localIdSet_;
765  mutable typename Allocator::template rebind< char >::other storageAllocator_;
766  };
767 
768 
769 
770  // GeometryGrid::Codim
771  // -------------------
772 
773  template< class HostGrid, class CoordFunction, class Allocator >
774  template< int codim >
775  struct GeometryGrid< HostGrid, CoordFunction, Allocator >::Codim
776  : public Base::template Codim< codim >
777  {
785  typedef typename Traits::template Codim< codim >::Entity Entity;
786 
791  typedef typename Traits::template Codim< codim >::EntityPointer EntityPointer;
792 
806  typedef typename Traits::template Codim< codim >::Geometry Geometry;
807 
816  typedef typename Traits::template Codim< codim >::LocalGeometry LocalGeometry;
817 
823  template< PartitionIteratorType pitype >
824  struct Partition
825  {
826  typedef typename Traits::template Codim< codim >
829  typedef typename Traits::template Codim< codim >
832  };
833 
842 
851 
853  };
854 
855 } // namespace Dune
856 
857 #endif // #ifndef DUNE_GEOGRID_GRID_HH