dgfparserblocks.hh

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

Generated on Sun Nov 15 22:28:39 2009 for dune-grid by  doxygen 1.5.6