dune-grid  2.1.1
albertagrid/dgfparser.hh
Go to the documentation of this file.
00001 #ifndef DUNE_ALBERTA_DGFPARSER_HH
00002 #define DUNE_ALBERTA_DGFPARSER_HH
00003 
00004 #include <dune/grid/albertagrid.hh>
00005 #include <dune/grid/albertagrid/gridfactory.hh>
00006 
00007 #include <dune/grid/io/file/dgfparser/dgfparser.hh>
00008 #include <dune/grid/io/file/dgfparser/dgfprojectionblock.hh>
00009 
00010 #if HAVE_ALBERTA 
00011 
00012 namespace Dune
00013 {
00014 
00015   // DGFGridFactory for AlbertaGrid
00016   // ------------------------------
00017 
00018   template< int dim, int dimworld >
00019   struct DGFGridFactory< AlbertaGrid< dim, dimworld > >
00020   {
00021     typedef AlbertaGrid<dim,dimworld>  Grid;
00022     const static int dimension = Grid::dimension;
00023     typedef MPIHelper::MPICommunicator MPICommunicatorType;
00024     typedef typename Grid::template Codim<0>::Entity Element;
00025     typedef typename Grid::template Codim<dimension>::Entity Vertex;
00026     typedef Dune::GridFactory<Grid> GridFactory;
00027 
00028     explicit DGFGridFactory ( std::istream &input,
00029                               MPICommunicatorType comm = MPIHelper::getCommunicator() );
00030     explicit DGFGridFactory ( const std::string &filename, 
00031                               MPICommunicatorType comm = MPIHelper::getCommunicator() );
00032 
00033     Grid *grid () const
00034     {
00035       return grid_;
00036     }
00037 
00038     template< class Intersection >
00039     bool wasInserted ( const Intersection &intersection ) const
00040     {
00041       return factory_.wasInserted( intersection );
00042     }
00043 
00044     template< class Intersection >
00045     int boundaryId ( const Intersection &intersection ) const
00046     {
00047       return Grid::getRealImplementation( intersection ).boundaryId();
00048     }
00049 
00050     template< int codim >
00051     int numParameters () const
00052     {
00053       if( codim == 0 )
00054         return dgf_.nofelparams;
00055       else if( codim == dimension )
00056         return dgf_.nofvtxparams;
00057       else
00058         return 0;
00059     }
00060 
00061     std::vector< double > &parameter ( const Element &element )
00062     {
00063       if( numParameters< 0 >() <= 0 )
00064       {
00065         DUNE_THROW( InvalidStateException,
00066                     "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
00067       }
00068       return dgf_.elParams[ factory_.insertionIndex( element ) ];
00069     }
00070 
00071     std::vector< double > &parameter ( const Vertex &vertex )
00072     {
00073       if( numParameters< dimension >() <= 0 )
00074       {
00075         DUNE_THROW( InvalidStateException,
00076                     "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
00077       }
00078       return dgf_.vtxParams[ factory_.insertionIndex( vertex ) ];
00079     }
00080 
00081   private:
00082     bool generate( std::istream &input );
00083 
00084     Grid *grid_;
00085     GridFactory factory_;
00086     DuneGridFormatParser dgf_;
00087   };
00088 
00089 
00090 
00091   // DGFGridInfo for AlbertaGrid
00092   // ---------------------------
00093 
00094   template< int dim, int dimworld >
00095   struct DGFGridInfo< AlbertaGrid< dim, dimworld > >
00096   {
00097     static int refineStepsForHalf ()
00098     {
00099       return dim;
00100     }
00101 
00102     static double refineWeight ()
00103     {
00104       return 0.5;
00105     }
00106   };
00107 
00108 
00109 
00110   // Implementation of DGFGridFactory for AlbertaGrid
00111   // ------------------------------------------------
00112 
00113   template< int dim, int dimworld >
00114   inline DGFGridFactory< AlbertaGrid< dim, dimworld > >
00115     ::DGFGridFactory ( std::istream &input, MPICommunicatorType comm )
00116   : dgf_( 0, 1 )
00117   {
00118     input.clear();
00119     input.seekg( 0 );
00120     if( !input )
00121       DUNE_THROW(DGFException, "Error resetting input stream." );
00122     generate( input );
00123   }
00124 
00125 
00126   template< int dim, int dimworld >
00127   inline DGFGridFactory< AlbertaGrid< dim, dimworld > >
00128     ::DGFGridFactory ( const std::string &filename, MPICommunicatorType comm )
00129   : dgf_( 0, 1 )
00130   {
00131     std::ifstream input( filename.c_str() );
00132     if( !input )
00133       DUNE_THROW( DGFException, "Macrofile " << filename << " not found." );
00134     if( !generate( input ) )
00135       grid_ = new AlbertaGrid< dim, dimworld >( filename.c_str() );
00136     input.close();
00137   }
00138 
00139 }
00140 
00141 #endif // #if HAVE_ALBERTA
00142 
00143 #endif // #ifndef DUNE_ALBERTA_DGFPARSER_HH