dune-grid  2.2beta1
dgfug.hh
Go to the documentation of this file.
00001 #ifndef DUNE_GRID_IO_FILE_DGFPARSER_DGFUG_HH
00002 #define DUNE_GRID_IO_FILE_DGFPARSER_DGFUG_HH
00003 
00004 //- C++ includes
00005 #include <fstream>
00006 #include <istream>
00007 #include <string>
00008 #include <vector>
00009 
00010 //- dune-common includes
00011 #include <dune/common/exceptions.hh>
00012 #include <dune/common/fvector.hh>
00013 #include <dune/common/mpihelper.hh>
00014 
00015 //- dune-grid includes
00016 #include <dune/grid/common/intersection.hh>
00017 #include <dune/grid/uggrid.hh>
00018 
00019 //- local includes
00020 #include "dgfparser.hh"
00021 #include "blocks/gridparameter.hh"
00022 
00023 
00024 namespace Dune
00025 {
00026 
00027   namespace dgf
00028   {
00029 
00030     // UGGridParameterBlock
00031     // --------------------
00032 
00033     struct UGGridParameterBlock
00034     : public GridParameterBlock
00035     {
00037       explicit UGGridParameterBlock ( std::istream &input );
00038 
00040       bool noClosure () const { return noClosure_; }
00042       bool noCopy () const { return noCopy_; }
00044       size_t heapSize () const { return heapSize_; }
00045 
00046     protected:
00047       bool noClosure_;  // no closure for UGGrid 
00048       bool noCopy_;     // no copies  for UGGrid
00049       size_t heapSize_; // heap size  for UGGrid 
00050     };
00051 
00052   } // namespace dgf
00053 
00054 
00055 
00056 #if ENABLE_UG 
00057   template< int dim >
00058   struct DGFGridInfo< UGGrid< dim > >
00059   {
00060     static int refineStepsForHalf ()
00061     {
00062       return 1;
00063     }
00064 
00065     static double refineWeight ()
00066     {
00067       return -1.;
00068     }
00069   };
00070 
00071 
00072 
00073   // DGFGridFactory< UGGrid< dim > >
00074   // -------------------------------
00075 
00076   template< int dim >
00077   struct DGFGridFactory< UGGrid< dim > >
00078   {
00080     typedef UGGrid< dim > Grid;
00082     static const int dimension = dim;
00084     typedef MPIHelper::MPICommunicator MPICommunicatorType;
00085 
00087     explicit DGFGridFactory ( std::istream &input,
00088                               MPICommunicatorType comm = MPIHelper::getCommunicator() )
00089     : grid_( 0 ),
00090       factory_(),
00091       dgf_( rank( comm ), size( comm ) )
00092     { 
00093       generate( input );
00094     }
00095 
00097     explicit DGFGridFactory ( const std::string &filename,
00098                               MPICommunicatorType comm = MPIHelper::getCommunicator() )
00099     : grid_( 0 ),
00100       factory_(),
00101       dgf_( rank( comm ), size( comm ) )
00102     { 
00103       std::ifstream input( filename.c_str() );
00104       if ( !input )
00105         DUNE_THROW( DGFException, "Error: Macrofile " << filename << " not found" );
00106       generate( input );
00107     }
00108 
00110     Grid *grid ()
00111     {
00112       return grid_;
00113     }
00114 
00116     template< class GG, template< class > class II >
00117     bool wasInserted ( const Dune::Intersection< GG, II > &intersection ) const
00118     {
00119       return factory_.wasInserted( intersection );
00120     }
00121 
00123     template < class GG, template< class > class II >
00124     int boundaryId ( const Dune::Intersection< GG, II > &intersection ) const
00125     {
00126       return intersection.boundarySegmentIndex();
00127     }
00128 
00130     template< int codim >
00131     int numParameters () const
00132     {
00133       if( codim == 0 )
00134         return dgf_.nofelparams;
00135       else if( codim == dimension )
00136         return dgf_.nofvtxparams;
00137       else
00138         return 0;
00139     }
00140 
00142     template< class Entity >
00143     int numParameters ( const Entity & ) const
00144     {
00145       return numParameters< Entity::codimension >();
00146     }
00147 
00149     std::vector< double > &parameter ( const typename Grid::template Codim< 0 >::Entity &element )
00150     {
00151       if( numParameters< 0 >() <= 0 )
00152       {
00153         DUNE_THROW( InvalidStateException,
00154                     "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
00155       }
00156       return dgf_.elParams[ factory_.insertionIndex( element ) ];
00157     }
00158 
00160     std::vector< double > &parameter ( const typename Grid::template Codim< dimension >::Entity &vertex )
00161     {
00162       if( numParameters< dimension >() <= 0 )
00163       {
00164         DUNE_THROW( InvalidStateException,
00165                     "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
00166       }
00167       return dgf_.vtxParams[ factory_.insertionIndex( vertex ) ];
00168     }
00169 
00171     bool haveBoundaryParameters () const
00172     {
00173       return dgf_.haveBndParameters;
00174     }
00175 
00177     template < class GG, template< class > class II >
00178     const DGFBoundaryParameter::type &boundaryParameter ( const Dune::Intersection< GG, II > &intersection ) const
00179     {
00180       typedef Dune::Intersection< GG, II > Intersection;
00181       typename Intersection::EntityPointer inside = intersection.inside();
00182       const typename Intersection::Entity &entity = *inside;
00183       const int face = intersection.indexInInside();
00184 
00185       const GenericReferenceElement< double, dimension > &refElem 
00186         = GenericReferenceElements< double, dimension >::general( entity.type() );
00187       int corners = refElem.size( face, 1, dimension );
00188       std::vector< unsigned int > bound( corners );
00189       for( int i = 0; i < corners; ++i )
00190       {
00191         const int k = refElem.subEntity( face, 1, i, dimension );
00192         bound[ i ] = factory_.insertionIndex( *entity.template subEntity< dimension >( k ) );
00193       }
00194 
00195       DuneGridFormatParser::facemap_t::key_type key( bound, false );
00196       const DuneGridFormatParser::facemap_t::const_iterator pos = dgf_.facemap.find( key );
00197       if( pos != dgf_.facemap.end() )
00198         return dgf_.facemap.find( key )->second.second;
00199       else
00200         return DGFBoundaryParameter::defaultValue();
00201     }
00202 
00203   private:
00204     // create grid
00205     void generate ( std::istream &input );
00206 
00207     // return rank
00208     static int rank( MPICommunicatorType MPICOMM )
00209     {
00210       int rank = 0;
00211 #if HAVE_MPI
00212       MPI_Comm_rank( MPICOMM, &rank );
00213 #endif
00214       return rank;
00215     }
00216 
00217     // return size
00218     static int size( MPICommunicatorType MPICOMM )
00219     {
00220       int size = 1;
00221 #if HAVE_MPI
00222       MPI_Comm_size( MPICOMM, &size );
00223 #endif
00224       return size;
00225     }
00226 
00227     Grid *grid_;
00228     GridFactory< UGGrid< dim > > factory_;
00229     DuneGridFormatParser dgf_;
00230   };
00231 #endif // #if ENABLE_UG 
00232 
00233 } // namespace Dune
00234 
00235 #endif // #ifndef DUNE_GRID_IO_FILE_DGFPARSER_DGFUG_HH