dgfgeogrid.hh

Go to the documentation of this file.
00001 #ifndef DUNE_DGFGEOGRID_HH
00002 #define DUNE_DGFGEOGRID_HH
00003 
00004 #include <dune/common/typetraits.hh>
00005 
00006 #include <dune/grid/geometrygrid.hh>
00007 #include <dune/grid/io/file/dgfparser/dgfparser.hh>
00008 #include <dune/grid/io/file/dgfparser/dgfprojectionblock.hh>
00009 #include <dune/grid/utility/hostgridaccess.hh>
00010 
00011 namespace Dune
00012 {
00013 
00014   /************************************************************************
00015    * Warning:
00016    * Reading DGF files directly into a GeometryGrid is a dirty hack for
00017    * two reasons:
00018    *   1) The host grid and coordinate function are never deleted (dangling
00019    *      pointers).
00020    *   2) The coordinate function has to provide a default constructor
00021    ************************************************************************/
00022 
00023 
00024   // DGFCoordFunction
00025   // ----------------
00026 
00027   template< int dimD, int dimR > 
00028   class DGFCoordFunction
00029   : public AnalyticalCoordFunction< double, dimD, dimR, DGFCoordFunction< dimD, dimR > >
00030   {
00031     typedef DGFCoordFunction< dimD, dimR > This;
00032     typedef AnalyticalCoordFunction< double, dimD, dimR, This > Base;
00033 
00034   public:
00035     typedef typename Base::DomainVector DomainVector;
00036     typedef typename Base::RangeVector RangeVector;
00037 
00038     typedef dgf::ProjectionBlock::Expression Expression;
00039 
00040     DGFCoordFunction ( const Expression *expression )
00041     : expression_( expression )
00042     {}
00043 
00044     void evaluate ( const DomainVector &x, RangeVector &y ) const
00045     {
00046       std::vector< double > vx( dimD );
00047       std::vector< double > vy;
00048       for( int i = 0; i < dimD; ++i )
00049         vx[ i ] = x[ i ];
00050       expression_->evaluate( vx, vy );
00051       assert( vy.size() == size_t( dimR ) );
00052       for( int i = 0; i < dimR; ++i )
00053         y[ i ] = vy[ i ];
00054     }
00055 
00056   private:
00057     const Expression *expression_;
00058   };
00059 
00060 
00061 
00062   // DGFCoordFunctionFactory
00063   // -----------------------
00064 
00065   template< class HostGrid, class CoordFunction,
00066             bool discrete = GeoGrid::isDiscreteCoordFunctionInterface< typename CoordFunction::Interface >::value >
00067   struct DGFCoordFunctionFactory;
00068 
00069 
00070   template< class HostGrid, class CoordFunction >
00071   struct DGFCoordFunctionFactory< HostGrid, CoordFunction, false >
00072   {
00073     static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
00074     {
00075       return new CoordFunction;
00076     }
00077   };
00078 
00079 
00080   template< class HostGrid, class CoordFunction >
00081   struct DGFCoordFunctionFactory< HostGrid, CoordFunction, true >
00082   {
00083     static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
00084     {
00085       return new CoordFunction( hostGrid );
00086     }
00087   };
00088 
00089 
00090   template< class HostGrid, int dimD, int dimR > 
00091   struct DGFCoordFunctionFactory< HostGrid, DGFCoordFunction< dimD, dimR >, false >
00092   {
00093     typedef DGFCoordFunction< dimD, dimR > CoordFunction;
00094 
00095     static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
00096     {
00097       dgf::ProjectionBlock projectionBlock( input, dimR );
00098       const typename CoordFunction::Expression *expression = projectionBlock.function( "coordfunction" );
00099       if( expression == 0 )
00100         DUNE_THROW( DGFException, "no coordfunction specified in DGF file." );
00101       return new CoordFunction( expression );
00102     }
00103   };
00104 
00105 
00106 
00107   // DGFGridFactory for GeometryGrid
00108   // -------------------------------
00109 
00110   template< class HostGrid, class CoordFunction, class Allocator >
00111   struct DGFGridFactory< GeometryGrid< HostGrid, CoordFunction, Allocator > >
00112   {
00113     typedef GeometryGrid< HostGrid, CoordFunction, Allocator > Grid;
00114 
00115     const static int dimension = Grid::dimension;
00116     typedef MPIHelper::MPICommunicator MPICommunicator;
00117     typedef typename Grid::template Codim<0>::Entity Element;
00118     typedef typename Grid::template Codim<dimension>::Entity Vertex;
00119 
00120     typedef DGFCoordFunctionFactory< HostGrid, CoordFunction > CoordFunctionFactory;
00121 
00122     explicit DGFGridFactory ( std::istream &input,
00123                               MPICommunicator comm = MPIHelper::getCommunicator() )
00124     : dgfHostFactory_( input, comm ),
00125       grid_( 0 )
00126     {
00127       HostGrid *hostGrid = dgfHostFactory_.grid();
00128       assert( hostGrid != 0 );
00129       CoordFunction *coordFunction = CoordFunctionFactory::create( input, *hostGrid );
00130       grid_ = new Grid( *hostGrid, *coordFunction );
00131     }
00132 
00133     explicit DGFGridFactory ( const std::string &filename, 
00134                               MPICommunicator comm = MPIHelper::getCommunicator() )
00135     : dgfHostFactory_( filename, comm ),
00136       grid_( 0 )
00137     {
00138       HostGrid *hostGrid = dgfHostFactory_.grid();
00139       assert( hostGrid != 0 );
00140       std::ifstream input( filename.c_str() );
00141       CoordFunction *coordFunction = CoordFunctionFactory::create( input, *hostGrid );
00142       grid_ = new Grid( *hostGrid, *coordFunction );
00143     }
00144 
00145     Grid *grid () const
00146     {
00147       return grid_;
00148     }
00149 
00150     template< class Intersection >
00151     bool wasInserted ( const Intersection &intersection ) const
00152     {
00153       return dgfHostFactory_.wasInserted( HostGridAccess< Grid >::hostIntersection( intersection ) ); 
00154     }
00155 
00156     template< class Intersection >
00157     int boundaryId ( const Intersection &intersection ) const
00158     {
00159       return dgfHostFactory_.boundaryId( HostGridAccess< Grid >::hostIntersection( intersection ) ); 
00160     }
00161 
00162     template< int codim >
00163     int numParameters () const
00164     {
00165       return dgfHostFactory_.template numParameters< codim >();
00166     }
00167 
00168     template< class Entity >
00169     std::vector< double > &parameter ( const Entity &entity )
00170     {
00171       return dgfHostFactory_.parameter( HostGridAccess< Grid >::hostEntity( entity ) );
00172     }
00173 
00174   private:
00175     DGFGridFactory< HostGrid > dgfHostFactory_;
00176     Grid *grid_;
00177   };
00178 
00179 
00180 
00181   // DGFGridInfo for GeometryGrid
00182   // ----------------------------
00183 
00184   template< class HostGrid, class CoordFunction, class Allocator >
00185   struct DGFGridInfo< GeometryGrid< HostGrid, CoordFunction, Allocator > >
00186   {
00187     static int refineStepsForHalf ()
00188     {
00189       return DGFGridInfo< HostGrid >::refineStepsForHalf();
00190     }
00191 
00192     static double refineWeight ()
00193     {
00194       return -1.0;
00195     }
00196   };
00197   
00198 }
00199 
00200 #endif // #ifndef DUNE_DGFGEOGRID_HH

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].