dgfparserblocks.hh

Go to the documentation of this file.
00001 #ifndef DUNE_MACROGRIDPARSERBLOCKS_HH
00002 #define DUNE_MACROGRIDPARSERBLOCKS_HH
00003 
00004 #include <cassert>
00005 #include <iostream>
00006 #include <map>
00007 #include <set>
00008 #include <vector>
00009 
00010 #include <dune/common/stdstreams.hh>
00011 #include <dune/grid/io/file/dgfparser/entitykey.hh>
00012 
00013 namespace Dune
00014 {
00015 
00016 // *************************************************************
00017 // Read one block with given identifier from disk
00018 // and allows the line-wise extraction from this block
00019 // *************************************************************
00020 namespace dgf {
00021 
00022 inline void makeupcase( std :: string &s )
00023 {
00024   for (size_t i=0;i<s.size();i++) 
00025     s[i]=toupper(s[i]);
00026 }
00027 
00028 
00029 class BasicBlock
00030 {
00031   int pos;                   // line number
00032   bool active;               // block was found
00033   bool empty;                // block was found but was empty
00034   std::string identifier;    // identifier of this block
00035   int linecount;             // total number of lines in the block
00036   std::stringstream block;   // the block itself
00037   std::string oneline;       // the active line in the block
00038 
00039   // get the block (if it exists)
00040   void getblock ( std::istream &in );
00041   // count the number of lines in the block
00042   // int countlines ();
00043 
00044 protected:
00045   std::stringstream line; // the active line as string buffer 
00046                           // for use in the derived classes
00047 
00048   // go back to beginning of block
00049   void reset ()
00050   {
00051     pos = -1;
00052     block.clear();
00053     block.seekg( 0 );
00054   }
00055 
00056   // get next line and store in string stream
00057   bool getnextline ();
00058   
00059   // get next entry in line
00060   template< class ENTRY >
00061   bool getnextentry( ENTRY &entry )
00062   {
00063     line >> entry;
00064     return line;
00065   }
00066 
00067   bool gettokenparam ( std :: string token, std :: string &entry );
00068   bool findtoken( std :: string token );
00069   
00070 public:
00071   // search for block in file and store in buffer
00072   BasicBlock ( std::istream &in, const char* id );
00073 
00074   // some information on this block
00075   bool isactive ()
00076   {
00077     return active;
00078   }
00079   
00080   bool isempty ()
00081   {
00082     return empty;
00083   }
00084   
00085   int &noflines ()
00086   {
00087     return linecount;
00088   }
00089   
00090   int linenumber ()
00091   {
00092     return pos;
00093   }
00094   
00095   // for error messages
00096   friend std :: ostream &operator<< ( std :: ostream &os, const BasicBlock &b )
00097   {
00098     return os << "block " << b.identifier << " (line " << b.pos << ")";
00099   }
00100 };
00101 
00102 
00103 // *************************************************************
00104 // derived classes for each block in grid file
00105 // *************************************************************
00106   class VertexBlock
00107   : public BasicBlock
00108   {
00109     int dimvertex;         // the dimension of the vertices (determined from DGF file)
00110     int dimworld;          // the dimension of the world (either dimvertex or given by user)
00111     bool goodline;         // active line describes a vertex
00112     int vtxoffset;
00113     int nofParam;
00114     
00115   public:
00116     static const char* ID;
00117 
00118     // initialize vertex block
00119     VertexBlock ( std :: istream &in, int &pdimworld );
00120 
00121     int get ( std :: vector< std :: vector< double > > &vtx,
00122               std :: vector< std :: vector< double > > &param,
00123               int &nofp );
00124 
00125     // some information
00126     bool ok () const
00127     {
00128       return goodline;
00129     }
00130 
00131     int offset () const
00132     {
00133       return vtxoffset;
00134     }
00135 
00136   private:
00137     // get dimworld
00138     int getDimWorld ();
00139 
00140     // get next vertex
00141     bool next ( std :: vector< double > &point, std :: vector< double > &param );
00142   };
00143 
00144 
00145 // *************************************************************
00146 class SimplexGenerationBlock
00147 : public BasicBlock
00148 {
00149   double area_;
00150   double angle_;
00151   bool display_;
00152   std::string path_;
00153   bool haspath_;
00154   std::string filename_;
00155   std::string filetype_;
00156   std::string parameter_;
00157   std::string dumpfilename_;
00158   bool hasfile_;
00159   int dimension_;
00160   
00161 public:
00162   const static char* ID;
00163   SimplexGenerationBlock ( std :: istream &in );
00164 
00165   double maxArea ()
00166   {
00167     return area_;
00168   }
00169   
00170   double minAngle ()
00171   {
00172     return angle_;
00173   }
00174   
00175   bool display ()
00176   {
00177     return display_;
00178   }
00179   
00180   bool haspath ()
00181   {
00182     return haspath_;
00183   }
00184   
00185   std :: string path ()
00186   {
00187     return path_;
00188   }
00189   
00190   bool hasfile ()
00191   {
00192     return hasfile_;
00193   }
00194   
00195   std :: string filename ()
00196   {
00197     return filename_;
00198   }
00199   
00200   std :: string filetype ()
00201   {
00202     return filetype_;
00203   }
00204   
00205   int dimension ()
00206   {
00207     return dimension_;
00208   }
00209   
00210   std :: string parameter ()
00211   {
00212     return parameter_;
00213   }
00214 
00215   const std::string dumpFileName ( ) const 
00216   {
00217     return dumpfilename_;
00218   }
00219 };
00220 
00221 
00222   // SimplexBlock
00223   // ------------
00224 
00225   class SimplexBlock
00226   : public BasicBlock
00227   {
00228     unsigned int nofvtx;
00229     int vtxoffset;
00230     int dimgrid;
00231     bool goodline;                 // active line describes a vertex
00232     int nofparams;                 // nof parameters
00233 
00234   public:
00235     const static char* ID;
00236 
00237     SimplexBlock ( std :: istream &in, int pnofvtx, int pvtxoffset, int &pdimgrid );
00238 
00239     int get ( std :: vector< std :: vector< unsigned int > > &simplex,
00240               std :: vector< std :: vector< double > > &params,
00241               int &nofp );
00242     
00243     // cubes -> simplex
00244     static int
00245     cube2simplex ( std :: vector< std :: vector< double > > &vtx,
00246                    std :: vector< std :: vector< unsigned int > > &elements,
00247                    std :: vector< std :: vector< double > > &params );
00248 
00249     // some information
00250     bool ok ()
00251     {
00252       return goodline;
00253     }
00254     
00255     int nofsimplex ()
00256     {
00257       return noflines();
00258     }
00259 
00260   private:
00261     // get the dimension of the grid
00262     int getDimGrid ();
00263     // get next simplex
00264     bool next ( std :: vector< unsigned int > &simplex,
00265                 std :: vector< double > &param );
00266   };
00267 
00268 
00269 
00270   // CubeBlock
00271   // ---------
00272 
00273   class CubeBlock
00274   : public BasicBlock
00275   {
00276     unsigned int nofvtx;
00277     int dimgrid; 
00278     bool goodline;        // active line describes a vertex
00279     std :: vector< unsigned int > map; // active vertex
00280     int nofparams;
00281     int vtxoffset;
00282     
00283    public:
00284     static const char* ID;
00285     
00286     CubeBlock ( std :: istream &in, int pnofvtx, int pvtxoffset, int &pdimgrid );
00287     
00288     int get ( std :: vector< std :: vector< unsigned int> > &simplex,
00289               std :: vector< std :: vector< double > > &params,
00290               int &nofp );
00291     
00292     // some information
00293     bool ok ()
00294     {
00295       return goodline;
00296     }
00297     
00298     int nofsimplex ()
00299     {
00300       return noflines();
00301     }
00302 
00303   private:
00304     // get the dimension of the grid
00305     int getDimGrid ();
00306     // get next simplex
00307     bool next ( std :: vector< unsigned int > &simplex,
00308                 std :: vector< double > &param );
00309   };
00310 
00311 
00312 #ifdef EXPERIMENTAL_GRID_EXTENSIONS
00313   // GeneralBlock
00314   // ---------
00315 
00316   class GeneralBlock
00317   : public BasicBlock
00318   {
00319     unsigned int nofvtx;
00320     int dimgrid; 
00321     bool goodline;        // active line describes a vertex
00322     std :: vector< unsigned int > map; // active vertex
00323     int nofparams;
00324     int vtxoffset;
00325     
00326    public:
00327     static const char* ID;
00328     
00329     GeneralBlock ( std :: istream &in, int pnofvtx, int pvtxoffset, int &pdimgrid );
00330     
00331     int get ( std :: vector< std :: vector< unsigned int> > &simplex,
00332               std :: vector< std :: vector< double > > &params,
00333               int &nofp );
00334     
00335     // some information
00336     bool ok ()
00337     {
00338       return goodline;
00339     }
00340     
00341     int nofsimplex ()
00342     {
00343       return noflines();
00344     }
00345 
00346   private:
00347     // get the dimension of the grid
00348     int getDimGrid ();
00349     // get next simplex
00350     bool next ( std :: vector< unsigned int > &simplex,
00351                 std :: vector< double > &param );
00352   };
00353 #endif
00354 
00355 // *************************************************************
00356 // the block BoundaryDomBlock looks for a domain which is characterized by two points in R^dimworld
00357 class BoundaryDomBlock : public BasicBlock {
00358   int dimworld;        // the dimesnsion of the vertices (is given  from user)
00359   bool goodline;       // active line describes a vertex
00360   std::vector<double> p1,p2;    // active vertex
00361   int bndid;
00362   bool withdefault;
00363   int defaultvalue;
00364  public:
00365   static const char* ID;
00366   // initialize vertex block and get first vertex
00367   BoundaryDomBlock(std::istream& in,int cdimworld );
00368   bool next ();
00369   bool inside(const std::vector<double>& v) const;
00370   int id() const {
00371     return bndid;
00372   }
00373   bool defaultValueGiven() {
00374     return withdefault;
00375   }
00376   int defaultValue() {
00377     return defaultvalue;
00378   }
00379   // some information
00380   bool ok() {
00381     return goodline;
00382   }
00383   int nofdombound() {
00384     return noflines();
00385   }
00386 };
00387 
00388 
00389 // *************************************************************
00390 // the BoundarySegBlock looks for given boundary values unless they aren't given they got the value zero 
00391 class BoundarySegBlock : public BasicBlock {
00392   int dimworld;                    // the dimesnsion of the vertices (is given  from user)
00393   bool goodline;                   // active line describes a vertex
00394   std :: vector< unsigned int > p; // active vertex
00395   int bndid;
00396   bool simplexgrid;
00397  public:
00398   static const char* ID;
00399   // initialize vertex block and get first vertex
00400   BoundarySegBlock ( std :: istream &in, int pnofvtx,
00401                      int pdimworld, bool psimplexgrid );
00402 
00403   // some information
00404   int get( std :: map< DGFEntityKey< unsigned int>, int > &facemap,
00405            bool fixedsize,
00406            int vtxoffset );
00407   bool ok() {
00408     return goodline;
00409   }
00410   int nofbound() {
00411     return noflines();
00412   }
00413 private:
00414   bool next();
00415   // get coordinates of active vertex
00416   int operator[](int i) {
00417     assert(ok());
00418     assert(linenumber()>=0);
00419     assert(0<=i && i<dimworld+1);
00420     return p[i];
00421   }
00422   int size() {
00423     return p.size();
00424   }
00425 };
00426 
00427 
00428 // *************************************************************
00429 class DimBlock : public BasicBlock {
00430   int _dimworld;     // dimension of world
00431   int _dim;          // dimension of grid
00432  public:
00433   const static char* ID;
00434   // initialize block and get dimension of world
00435   DimBlock ( std :: istream &in );
00436   // get dimension of world found in block
00437   int dim() {
00438     return _dim;
00439   }
00440   int dimworld() {
00441     return _dimworld;
00442   }
00443   // some information 
00444   bool ok() {
00445     return true;
00446   }
00447 };
00448 
00449 
00450 //-  *************************************************************
00451 
00465 class GridParameterBlock
00466 : public BasicBlock 
00467 {
00468 public:
00469   typedef unsigned int Flags;
00470 
00471   static const Flags foundName = 1 << 0;
00472   static const Flags foundDumpFileName = 1 << 1;
00473   static const Flags foundLongestEdge = 1 << 5;
00474 
00475 protected:
00476   Flags foundFlags_; // supportFlags, this block was created with
00477   std::string name_; // name of the grid
00478   std::string dumpFileName_; // name of the grid
00479   bool markLongestEdge_; // Mark longest edge for AlbertaGrid 
00480 
00481 private:
00482   // copy not implemented
00483   GridParameterBlock(const GridParameterBlock&);
00484   
00485 public:
00486   const static char* ID;
00488   GridParameterBlock ( std::istream &in );
00489 
00491   const std::string &name ( const std::string &defaultValue ) const
00492   {
00493     if( (foundFlags_ & foundName) == 0 )
00494     {
00495       dwarn << "GridParameterBlock: Parameter 'name' not specified, "
00496             << "defaulting to '" << defaultValue << "'." << std::endl;
00497       return defaultValue;
00498     }
00499     else
00500       return name_;
00501   }
00502 
00503   const std::string &dumpFileName ( ) const 
00504   {
00505     if( (foundFlags_ & foundDumpFileName) != 0 )
00506     {
00507       dwarn << "GridParameterBlock: found Parameter 'dumpfilename', "
00508             << "dumping file to `" << dumpFileName_ << "'" << std::endl;
00509     }
00510     return dumpFileName_;
00511   }
00512 
00514   bool markLongestEdge () const
00515   {
00516     if( (foundFlags_ & foundLongestEdge) == 0 )
00517     {
00518       dwarn << "GridParameterBlock: Parameter 'refinementedge' not specified, "
00519             << "defaulting to 'ARBITRARY'." << std::endl;
00520     }
00521     return markLongestEdge_;
00522   }
00523 
00524   // some information 
00525   bool ok() 
00526   {
00527     return true;
00528   }
00529 };
00530 
00531 
00532 
00533     // IntervalBlock
00534     // -------------
00535 
00536     struct IntervalBlock
00537     : public BasicBlock
00538     {
00539       struct Interval
00540       {
00541         std::vector< double > p[ 2 ]; // lower and upper boundary points
00542         std::vector< double > h;      // width of the cells in each direction
00543         std::vector< int > n;         // number of cells in each direction
00544       };
00545 
00546     private:
00547       std::vector< Interval > intervals_;
00548       bool good_;                      //data read correctly
00549       int dimw_;                       //dimension of world
00550 
00551     public:
00552       const static char* ID;
00553       explicit IntervalBlock ( std::istream &in );
00554 
00555       void get ( std::vector< std::vector< double > > &vtx, int &nofvtx,
00556                  std::vector< std::vector< unsigned int > > &simplex, int &nofsimpl )
00557       {
00558         for( size_t i = 0; i < intervals_.size(); ++i )
00559         {
00560           int oldvtx = nofvtx;
00561           nofvtx += getVtx( i, vtx );
00562           nofsimpl += getHexa( i, simplex, oldvtx );
00563         }
00564       }
00565 
00566       void get ( std::vector< std::vector< double > > &vtx, int &nofvtx )
00567       {
00568         for( size_t i = 0; i < intervals_.size(); ++i )
00569           nofvtx += getVtx( i, vtx );
00570       }
00571 
00572       const Interval &get ( int block ) const
00573       {
00574         return intervals_[ block ];
00575       }
00576 
00577       int numIntervals () const
00578       {
00579         return intervals_.size();
00580       }
00581 
00582       int dimw () const
00583       {
00584         return dimw_;
00585       }
00586 
00587       int getVtx ( int block, std::vector< std::vector< double > > &vtx ) const;
00588       int getHexa ( int block, std::vector< std::vector< unsigned int > > &cubes,
00589                     int offset = 0 ) const;
00590 
00591       int nofvtx ( int block ) const
00592       {
00593         const Interval &interval = get( block );
00594         int n = 1;
00595         for( int i = 0; i < dimw_; ++i )
00596           n *= (interval.n[ i ] + 1);
00597         return n;
00598       }
00599 
00600       int nofhexa ( int block ) const
00601       {
00602         const Interval &interval = get( block );
00603         int n = 1;
00604         for( int i = 0; i < dimw_; ++i )
00605           n *= interval.n[ i ];
00606         return n;
00607       }
00608 
00609     private:
00610       template< class T >
00611       void parseLine ( std::vector< T > &v );
00612 
00613       bool next ();
00614     };
00615 
00616 
00617     inline std::ostream &
00618     operator<< ( std::ostream &out, const IntervalBlock::Interval &interval )
00619     {
00620       if( interval.p[ 0 ].empty() || interval.p[ 1 ].empty() || interval.n.empty() )
00621         return out << "Interval {}";
00622 
00623       out << "Interval { p0 = (" << interval.p[ 0 ][ 0 ];
00624       for( size_t i = 1; i < interval.p[ 0 ].size(); ++i )
00625         out << ", " << interval.p[ 0 ][ i ];
00626       out << "), p1 = (" << interval.p[ 1 ][ 0 ];
00627       for( size_t i = 1; i < interval.p[ 1 ].size(); ++i )
00628         out << ", " << interval.p[ 1 ][ i ];
00629       out << "), n = (" << interval.n[ 0 ];
00630       for( size_t i = 1; i < interval.n.size(); ++i )
00631         out << ", " << interval.n[ i ];
00632       return out << ") }";
00633     }
00634 
00635 
00636 
00637     // PeriodicFaceTransformationBlock
00638     // -------------------------------
00639 
00640     struct PeriodicFaceTransformationBlock
00641     : public BasicBlock 
00642     {
00643       template< class T >
00644       class Matrix;
00645 
00646       struct AffineTransformation;
00647 
00648     private:
00649       std::vector< AffineTransformation > transformations_;
00650 
00651       // copy not implemented
00652       PeriodicFaceTransformationBlock ( const PeriodicFaceTransformationBlock & );
00653       
00654     public:
00655       static const char *ID;
00656 
00657       // initialize block and get dimension of world
00658       PeriodicFaceTransformationBlock ( std::istream &in, int dimworld );
00659 
00660       const AffineTransformation &transformation ( int i ) const
00661       {
00662         assert( i < numTransformations() );
00663         return transformations_[ i ];
00664       }
00665 
00666       int numTransformations () const
00667       {
00668         return transformations_.size();
00669       }
00670 
00671     private:
00672       void match ( char what );
00673     };
00674 
00675 
00676 
00677     // PeriodicFaceTransformationBlock::Matrix
00678     // ---------------------------------------
00679 
00680     template< class T >
00681     class PeriodicFaceTransformationBlock::Matrix
00682     {
00683       int rows_;
00684       int cols_;
00685       std::vector< T > fields_;
00686 
00687     public:
00688       Matrix ( int rows, int cols )
00689       : rows_( rows ),
00690         cols_( cols ),
00691         fields_( rows * cols )
00692       {}
00693 
00694       const T &operator() ( int i, int j ) const
00695       {
00696         return fields_[ i * cols_ + j ];
00697       }
00698 
00699       T &operator() ( int i, int j )
00700       {
00701         return fields_[ i * cols_ + j ];
00702       }
00703 
00704       int rows () const
00705       {
00706         return rows_;
00707       }
00708 
00709       int cols () const
00710       {
00711         return cols_;
00712       }
00713     };
00714 
00715 
00716     // PeriodicFaceTransformationBlock::AffineTransformation
00717     // -----------------------------------------------------
00718 
00719     struct PeriodicFaceTransformationBlock::AffineTransformation
00720     {
00721       Matrix< double > matrix;
00722       std::vector< double > shift;
00723 
00724       explicit AffineTransformation ( int dimworld )
00725       : matrix( dimworld, dimworld ),
00726         shift( dimworld )
00727       {}
00728     };
00729 
00730 
00731 
00732     inline std::ostream &
00733     operator<< ( std::ostream &out, const PeriodicFaceTransformationBlock::AffineTransformation &trafo )
00734     {
00735       for( int i = 0; i < trafo.matrix.rows(); ++i )
00736       {
00737         out << (i > 0 ? ", " : "");
00738         for( int j = 0; j < trafo.matrix.cols(); ++j )
00739           out << (j > 0 ? " " : "") << trafo.matrix( i, j );
00740       }
00741       out << " +";
00742       for( unsigned int i = 0; i < trafo.shift.size(); ++i )
00743         out << " " << trafo.shift[ i ];
00744       return out;
00745     }
00746 
00747 
00748 } // end namespace dgf
00749 
00750 } // end namespace Dune
00751 
00752 #endif

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