dune-grid  2.2.0
geometrygrid/grid.hh
Go to the documentation of this file.
00001 #ifndef DUNE_GEOGRID_GRID_HH
00002 #define DUNE_GEOGRID_GRID_HH
00003 
00004 #include <dune/common/nullptr.hh>
00005 #include <dune/common/static_assert.hh>
00006 
00007 #include <dune/grid/common/grid.hh>
00008 
00009 #include <dune/grid/geometrygrid/backuprestore.hh>
00010 #include <dune/grid/geometrygrid/capabilities.hh>
00011 #include <dune/grid/geometrygrid/datahandle.hh>
00012 #include <dune/grid/geometrygrid/gridfamily.hh>
00013 #include <dune/grid/geometrygrid/identity.hh>
00014 #include <dune/grid/geometrygrid/persistentcontainer.hh>
00015 
00016 namespace Dune
00017 {
00018 
00019   // DefaultCoordFunction
00020   // --------------------
00021   
00022   template< class HostGrid >
00023   class DefaultCoordFunction
00024   : public IdenticalCoordFunction< typename HostGrid::ctype, HostGrid::dimensionworld >
00025   {};
00026 
00027 
00028 
00029   // GeometryGrid
00030   // ------------
00031 
00072   template< class HostGrid, class CoordFunction = DefaultCoordFunction< HostGrid >, class Allocator = std::allocator< void > >
00073   class GeometryGrid
00075   : public GridDefaultImplementation
00076       < HostGrid::dimension, CoordFunction::dimRange, typename HostGrid::ctype,
00077         GeoGrid::GridFamily< HostGrid, CoordFunction, Allocator > >,
00078     public GeoGrid::ExportParams< HostGrid, CoordFunction >,
00079     public GeoGrid::BackupRestoreFacilities< GeometryGrid< HostGrid, CoordFunction, Allocator > >
00081   {
00082     typedef GeometryGrid< HostGrid, CoordFunction, Allocator > Grid;
00083 
00084     typedef GridDefaultImplementation
00085       < HostGrid::dimension, CoordFunction::dimRange, typename HostGrid::ctype,
00086         GeoGrid::GridFamily< HostGrid, CoordFunction, Allocator > >
00087       Base;
00088 
00089     friend class GeoGrid::HierarchicIterator< const Grid >;
00090 
00091     template< int, class, bool > friend class GeoGrid::EntityBase;
00092     template< class, bool > friend class GeoGrid::EntityPointer;
00093     template< int, class > friend class GeoGrid::EntityProxy;
00094     template< int, int, class > friend class GeoGrid::Geometry;
00095     template< class, class > friend class GeoGrid::Intersection;
00096     template< class > friend class GeoGrid::IntersectionIterator;
00097     template< class, class > friend class GeoGrid::IdSet;
00098     template< class, class > friend class GeoGrid::IndexSet;
00099     template< class > friend class HostGridAccess;
00100 
00101     template< class, class > friend class GeoGrid::CommDataHandle;
00102 
00103     template< int, PartitionIteratorType, class > friend class GeoGrid::LevelIteratorTraits;
00104     template< int, PartitionIteratorType, class > friend class GeoGrid::LeafIteratorTraits;
00105 
00106     template< class, class, class > friend class PersistentContainer;
00107 
00108   public:
00110     typedef GeoGrid::GridFamily< HostGrid, CoordFunction, Allocator > GridFamily;
00116 
00117     typedef typename GridFamily::Traits Traits;
00118     
00125     template< int codim >
00126     struct Codim;
00127 
00133 
00134     typedef typename Traits::HierarchicIterator HierarchicIterator;
00136     typedef typename Traits::LeafIntersectionIterator LeafIntersectionIterator;
00138     typedef typename Traits::LevelIntersectionIterator LevelIntersectionIterator;
00139 
00153     typedef typename Traits::LeafIndexSet LeafIndexSet;
00154     
00163     typedef typename Traits::LevelIndexSet LevelIndexSet;
00164 
00175     typedef typename Traits::GlobalIdSet GlobalIdSet;
00176 
00192     typedef typename Traits::LocalIdSet LocalIdSet;
00193 
00199 
00200     typedef typename Traits::ctype ctype;
00201 
00203     typedef typename Traits::CollectiveCommunication CollectiveCommunication;
00204 
00219     GeometryGrid ( HostGrid &hostGrid, CoordFunction &coordFunction, const Allocator &allocator = Allocator() )
00220     : hostGrid_( &hostGrid ),
00221       coordFunction_( coordFunction ),
00222       removeHostGrid_( false ),
00223       levelIndexSets_( hostGrid_->maxLevel()+1, nullptr, allocator ),
00224       storageAllocator_( allocator )
00225     {}
00226 
00236     GeometryGrid ( HostGrid *hostGrid, CoordFunction *coordFunction, const Allocator &allocator = Allocator() )
00237     : hostGrid_( hostGrid ),
00238       coordFunction_( *coordFunction ),
00239       removeHostGrid_( true ),
00240       levelIndexSets_( hostGrid_->maxLevel()+1, nullptr, allocator ),
00241       storageAllocator_( allocator )
00242     {}
00243    
00246     ~GeometryGrid ()
00247     {
00248       for( unsigned int i = 0; i < levelIndexSets_.size(); ++i )
00249       {
00250         if( levelIndexSets_[ i ] )
00251           delete( levelIndexSets_[ i ] );
00252       }
00253 
00254       if( removeHostGrid_ )
00255       {
00256         delete &coordFunction_;
00257         delete hostGrid_;
00258       }
00259     }
00260 
00273     int maxLevel () const
00274     {
00275       return hostGrid().maxLevel();
00276     }
00277    
00286     int size ( int level, int codim ) const
00287     {
00288       return hostGrid().size( level, codim );        
00289     }
00290     
00297     int size ( int codim ) const
00298     {
00299       return hostGrid().size( codim );
00300     }
00301 
00310     int size ( int level, GeometryType type ) const
00311     {
00312       return hostGrid().size( level, type );
00313     }
00314     
00319     int size ( GeometryType type ) const
00320     {
00321       return hostGrid().size( type );
00322     }
00323 
00328     size_t numBoundarySegments () const
00329     {
00330       return hostGrid().numBoundarySegments( );
00331     }
00334     template< int codim >
00335     typename Codim< codim >::LevelIterator lbegin ( int level ) const
00336     {
00337       return lbegin< codim, All_Partition >( level );
00338     }
00339 
00340     template< int codim >
00341     typename Codim< codim >::LevelIterator lend ( int level ) const
00342     {
00343       return lend< codim, All_Partition >( level );
00344     }
00345     
00346     template< int codim, PartitionIteratorType pitype >
00347     typename Codim< codim >::template Partition< pitype >::LevelIterator
00348     lbegin ( int level ) const
00349     {
00350       typedef typename Traits::template Codim< codim >::template Partition< pitype >::LevelIteratorTraits IteratorTraits;
00351       return GeoGrid::Iterator< IteratorTraits >( *this, level, IteratorTraits::begin );
00352     }
00353     
00354     template< int codim, PartitionIteratorType pitype >
00355     typename Codim< codim >::template Partition< pitype >::LevelIterator
00356     lend ( int level ) const
00357     {
00358       typedef typename Traits::template Codim< codim >::template Partition< pitype >::LevelIteratorTraits IteratorTraits;
00359       return GeoGrid::Iterator< IteratorTraits >( *this, level, IteratorTraits::end );
00360     }
00361     
00362     template< int codim >
00363     typename Codim< codim >::LeafIterator leafbegin () const
00364     {
00365       return leafbegin< codim, All_Partition >();
00366     }
00367     
00368     template< int codim >
00369     typename Codim< codim >::LeafIterator leafend () const
00370     {
00371       return leafend< codim, All_Partition >();
00372     }
00373 
00374     template< int codim, PartitionIteratorType pitype >
00375     typename Codim< codim >::template Partition< pitype >::LeafIterator
00376     leafbegin () const
00377     {
00378       typedef typename Traits::template Codim< codim >::template Partition< pitype >::LeafIteratorTraits IteratorTraits;
00379       return GeoGrid::Iterator< IteratorTraits >( *this, -1, IteratorTraits::begin );
00380     }
00381 
00382     template< int codim, PartitionIteratorType pitype >
00383     typename Codim< codim >::template Partition< pitype >::LeafIterator
00384     leafend () const
00385     {
00386       typedef typename Traits::template Codim< codim >::template Partition< pitype >::LeafIteratorTraits IteratorTraits;
00387       return GeoGrid::Iterator< IteratorTraits >( *this, -1, IteratorTraits::end );
00388     }
00389     
00390     const GlobalIdSet &globalIdSet () const
00391     {
00392       if( !globalIdSet_ )
00393         globalIdSet_ = GlobalIdSet( hostGrid().globalIdSet() );
00394       assert( globalIdSet_ );
00395       return globalIdSet_;
00396     }
00397     
00398     const LocalIdSet &localIdSet () const
00399     {
00400       if( !localIdSet_ )
00401         localIdSet_ = LocalIdSet( hostGrid().localIdSet() );
00402       assert( localIdSet_ );
00403       return localIdSet_;
00404     }
00405     
00406     const LevelIndexSet &levelIndexSet ( int level ) const
00407     {
00408       assert( levelIndexSets_.size() == (size_t)(maxLevel()+1) );
00409       if( (level < 0) || (level > maxLevel()) )
00410       {
00411         DUNE_THROW( GridError, "LevelIndexSet for nonexisting level " << level
00412                                << " requested." );
00413       }
00414 
00415       LevelIndexSet *&levelIndexSet = levelIndexSets_[ level ];
00416       if( !levelIndexSet )
00417         levelIndexSet = new LevelIndexSet( hostGrid().levelIndexSet( level ) );
00418       assert( levelIndexSet );
00419       return *levelIndexSet;
00420     }
00421     
00422     const LeafIndexSet &leafIndexSet () const
00423     {
00424       if( !leafIndexSet_ )
00425         leafIndexSet_ = LeafIndexSet( hostGrid().leafIndexSet() );
00426       assert( leafIndexSet_ );
00427       return leafIndexSet_;
00428     }
00429     
00430     void globalRefine ( int refCount )
00431     {
00432       hostGrid().globalRefine( refCount );
00433       update();
00434     }
00435 
00436     bool mark ( int refCount, const typename Codim< 0 >::Entity &entity )
00437     {
00438       return hostGrid().mark( refCount, getHostEntity< 0 >( entity ) );
00439     }
00440 
00441     int getMark ( const typename Codim< 0 >::Entity &entity ) const
00442     {
00443       return hostGrid().getMark( getHostEntity< 0 >( entity ) );
00444     }
00445 
00446     bool preAdapt ()
00447     {
00448       return hostGrid().preAdapt();
00449     }
00450     
00451     bool adapt ()
00452     {
00453       bool ret = hostGrid().adapt();
00454       update();
00455       return ret;
00456     }
00457 
00458     void postAdapt ()
00459     {
00460       hostGrid().postAdapt();
00461     }
00462 
00470     int overlapSize ( int codim ) const
00471     {
00472       return hostGrid().overlapSize( codim );
00473     }
00474     
00479     int ghostSize( int codim ) const
00480     {
00481       return hostGrid().ghostSize( codim );
00482     }
00483     
00489     int overlapSize ( int level, int codim ) const
00490     {
00491       return hostGrid().overlapSize( level, codim );
00492     }
00493     
00499     int ghostSize ( int level, int codim ) const
00500     {
00501       return hostGrid().ghostSize( level, codim );
00502     }
00503         
00517     template< class DataHandle, class Data >
00518     void communicate ( CommDataHandleIF< DataHandle, Data > &datahandle,
00519                        InterfaceType interface,
00520                        CommunicationDirection direction,
00521                        int level ) const
00522     {
00523       typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
00524       typedef GeoGrid::CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
00525 
00526       WrappedDataHandle wrappedDataHandle( *this, datahandle );
00527       hostGrid().communicate( wrappedDataHandle, interface, direction, level );
00528     }
00529     
00542     template< class DataHandle, class Data >
00543     void communicate ( CommDataHandleIF< DataHandle, Data > &datahandle,
00544                        InterfaceType interface,
00545                        CommunicationDirection direction ) const
00546     {
00547       typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
00548       typedef GeoGrid::CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
00549 
00550       WrappedDataHandle wrappedDataHandle( *this, datahandle );
00551       hostGrid().communicate( wrappedDataHandle, interface, direction );
00552     }
00553 
00562     const CollectiveCommunication &comm () const
00563     {
00564       return hostGrid().comm();
00565     }
00566 
00567 #if 0
00568     // data handle interface different between geo and interface
00569 
00579     bool loadBalance ()
00580     {
00581       const bool gridChanged= hostGrid().loadBalance();
00582       if( gridChanged )
00583         update();
00584       return gridChanged;
00585     }
00586     
00602     template< class DataHandle, class Data >
00603     bool loadBalance ( CommDataHandleIF< DataHandle, Data > &datahandle )
00604     {
00605       typedef CommDataHandleIF< DataHandle, Data > DataHandleIF;
00606       typedef GeoGrid :: CommDataHandle< Grid, DataHandleIF > WrappedDataHandle;
00607 
00608       WrappedDataHandle wrappedDataHandle( *this, datahandle );
00609       const bool gridChanged = hostGrid().loadBalance( wrappedDataHandle );
00610       if( gridChanged )
00611         update();
00612       return gridChanged;
00613     }
00614 #endif
00615 
00617     template< class EntitySeed >
00618     typename Traits::template Codim< EntitySeed::codimension >::EntityPointer
00619     entityPointer ( const EntitySeed &seed ) const
00620     {
00621       typedef typename Traits::template Codim< EntitySeed::codimension >::EntityPointerImpl EntityPointerImpl;
00622       return EntityPointerImpl( *this, seed );
00623     }
00624 
00630     const HostGrid &hostGrid () const
00631     {
00632       return *hostGrid_;
00633     }
00634 
00635     HostGrid &hostGrid ()
00636     {
00637       return *hostGrid_;
00638     }
00639 
00648     void update ()
00649     {
00650       // adapt the coordinate function
00651       GeoGrid::AdaptCoordFunction< typename CoordFunction::Interface >::adapt( coordFunction_ );
00652 
00653       const int newNumLevels = maxLevel()+1;
00654       const int oldNumLevels = levelIndexSets_.size();
00655 
00656       for( int i = newNumLevels; i < oldNumLevels; ++i )
00657       {
00658         if( levelIndexSets_[ i ] )
00659           delete levelIndexSets_[ i ];
00660       }
00661       levelIndexSets_.resize( newNumLevels, nullptr );
00662     }
00663 
00666     using Base::getRealImplementation;
00667 
00668 protected:
00669     const CoordFunction &coordFunction () const
00670     {
00671       return coordFunction_;
00672     }
00673 
00674     template< int codim >
00675     static const typename HostGrid::template Codim< codim >::Entity &
00676     getHostEntity( const typename Codim< codim >::Entity &entity )
00677     {
00678       return getRealImplementation( entity ).hostEntity();
00679     }
00680 
00681     template< int codim >
00682     char *allocateMappingStorage ( const GeometryType &gt ) const
00683     {
00684       assert( gt.dim() == Traits::dimension - codim );
00685       typedef GeoGrid::MappingFamily< Traits::dimensionworld, const Grid > MappingFamily;
00686       typedef typename MappingFamily::template Codim< codim >::MappingProvider MappingProvider;
00687       return storageAllocator_.allocate( MappingProvider::mappingSize( gt.id() ) );
00688     }
00689 
00690     template< int codim >
00691     void deallocateMappingStorage ( const GeometryType &gt, char *p ) const
00692     {
00693       assert( gt.dim() == Traits::dimension - codim );
00694       typedef GeoGrid::MappingFamily< Traits::dimensionworld, const Grid > MappingFamily;
00695       typedef typename MappingFamily::template Codim< codim >::MappingProvider MappingProvider;
00696       storageAllocator_.deallocate( p, MappingProvider::mappingSize( gt.id() ) );
00697     }
00698 
00699   private:
00700     HostGrid *const hostGrid_;
00701     CoordFunction &coordFunction_;
00702     bool removeHostGrid_;
00703     mutable std::vector< LevelIndexSet *, typename Allocator::template rebind< LevelIndexSet * >::other > levelIndexSets_;
00704     mutable LeafIndexSet leafIndexSet_;
00705     mutable GlobalIdSet globalIdSet_;
00706     mutable LocalIdSet localIdSet_;
00707     mutable typename Allocator::template rebind< char >::other storageAllocator_;
00708   };
00709 
00710 
00711 
00712   // GeometryGrid::Codim
00713   // -------------------
00714 
00715   template< class HostGrid, class CoordFunction, class Allocator >
00716   template< int codim >
00717   struct GeometryGrid< HostGrid, CoordFunction, Allocator >::Codim
00718   : public Base::template Codim< codim >
00719   {
00727     typedef typename Traits::template Codim< codim >::Entity Entity;
00728     
00733     typedef typename Traits::template Codim< codim >::EntityPointer EntityPointer;
00734 
00748     typedef typename Traits::template Codim< codim >::Geometry Geometry;
00749 
00758     typedef typename Traits::template Codim< codim >::LocalGeometry LocalGeometry;
00759 
00765     template< PartitionIteratorType pitype >
00766     struct Partition
00767     {
00768       typedef typename Traits::template Codim< codim >
00769         ::template Partition< pitype >::LeafIterator
00770         LeafIterator;
00771       typedef typename Traits::template Codim< codim >
00772         ::template Partition< pitype >::LevelIterator
00773         LevelIterator;
00774     };
00775 
00783     typedef typename Partition< All_Partition >::LeafIterator LeafIterator;
00784 
00792     typedef typename Partition< All_Partition >::LevelIterator LevelIterator;
00793 
00795   };
00796 
00797 } // namespace Dune
00798 
00799 #endif // #ifndef DUNE_GEOGRID_GRID_HH