00001 #ifndef DUNE_MACROGRIDPARSER_HH
00002 #define DUNE_MACROGRIDPARSER_HH
00003
00004 #include <iostream>
00005 #include <fstream>
00006
00007 #include <sstream>
00008 #include <string>
00009 #include <cstring>
00010 #include <vector>
00011 #include <memory>
00012 #include <map>
00013 #include <assert.h>
00014 #include <cmath>
00015
00016
00017 #include <dune/common/mpihelper.hh>
00018 #include <dune/grid/common/referenceelements.hh>
00019 #include <dune/common/stdstreams.hh>
00020
00021
00022 #include "dgfexception.hh"
00023 #include "entitykey.hh"
00024 #include "dgfparserblocks.hh"
00025
00026 namespace Dune
00027 {
00028
00029 class DGFPrintInfo;
00030
00033 class DuneGridFormatParser
00034 {
00035 public:
00036 typedef enum {Simplex,Cube,General} element_t;
00037 typedef enum {counterclockwise=1,clockwise=-1} orientation_t;
00038
00040 DuneGridFormatParser ();
00041
00046 bool readDuneGrid( std :: istream &, int dimG = -1, int dimW = -1 );
00047
00049 void writeTetgenPoly ( std :: string &, std :: string & );
00050 void writeTetgenPoly ( std::ostream & );
00052 void writeAlu ( std :: ostream & );
00054 void writeAlberta ( std::ostream & );
00055
00056 protected:
00057
00058 int dimw, dimgrid;
00059
00060 std::vector < std::vector <double> > vtx;
00061 int nofvtx;
00062 int vtxoffset;
00063 double minVertexDistance;
00064
00065 std :: vector< std :: vector< unsigned int > > elements;
00066 int nofelements;
00067
00068 std::vector < std::vector <int> > bound;
00069 int nofbound;
00070
00071 typedef std :: map< DGFEntityKey< unsigned int >, int > facemap_t;
00072 facemap_t facemap;
00073
00074 element_t element;
00075
00076
00077 bool simplexgrid;
00078
00079 bool cube2simplex;
00080
00081 int nofvtxparams,nofelparams;
00082 std::vector<std::vector<double> > vtxParams,elParams;
00083
00084 DGFPrintInfo* info;
00085
00086 void generateBoundaries ( std::istream &, bool );
00087
00088
00089 void generateSimplexGrid ( std::istream & );
00090 void readTetgenTriangle ( const std :: string & );
00091
00092
00093 void removeCopies ();
00094 void setOrientation ( int use1, int use2,
00095 orientation_t orientation=counterclockwise );
00096 void setRefinement ( int use1, int use2, int is1=-1, int is2=-1 );
00097 double testTriang ( int snr );
00098
00099 std :: vector< double > &getElParam ( int i, std::vector< double > &coord );
00100 std :: vector< double > &getVtxParam ( int i, std::vector< double > &coord );
00101 };
00102
00103
00104 class MacroGrid : protected DuneGridFormatParser
00105 {
00106 public:
00107 typedef MPIHelper::MPICommunicator MPICommunicatorType;
00108
00109 protected:
00111 MacroGrid(const char* filename, MPICommunicatorType MPICOMM = MPIHelper::getCommunicator())
00112 : DuneGridFormatParser()
00113 , filename_(filename)
00114 , MPICOMM_(MPICOMM) {}
00115
00117 MacroGrid()
00118 : DuneGridFormatParser()
00119 , filename_(0)
00120 , MPICOMM_(MPIHelper::getCommunicator()) {}
00121
00123 template <class GridType>
00124 inline GridType * createGrid () {
00125 return Impl<GridType>::generate(*this,filename_,MPICOMM_);
00126 }
00127 private:
00139 template< class GridType >
00140 class Impl;
00141
00142 const char* filename_;
00143 MPICommunicatorType MPICOMM_;
00144 };
00145
00158 template <class GridType>
00159 class GridPtr : public MacroGrid {
00160
00161
00162
00163
00164 public:
00165 typedef MPIHelper::MPICommunicator MPICommunicatorType;
00167 GridPtr(const std::string filename, MPICommunicatorType MPICOMM = MPIHelper::getCommunicator()) :
00168 MacroGrid(filename.c_str(),MPICOMM),
00169 gridptr_(this->template createGrid<GridType>()),
00170 emptyParam(),
00171 elParam(0), vtxParam(0), nofElParam_(0), nofVtxParam_(0) {
00172 if (nofelparams>0) {
00173 nofElParam_ = nofelparams;
00174 for (size_t i=0;i<elements.size();i++) {
00175 std::vector<double> coord;
00176 DomainType p (0);
00177 std::vector<double>& param = this->getElParam(i,coord);
00178 for (int k=0;k<dimw;k++)
00179 p[k] = coord[k];
00180 elParam.push_back(make_pair(p,param));
00181 }
00182 }
00183 if (nofvtxparams>0) {
00184 nofVtxParam_ = nofvtxparams;
00185 for (size_t i=0;i<vtx.size();i++) {
00186 std::vector<double> coord;
00187 DomainType p (0);
00188 std::vector<double>& param = getVtxParam(i,coord);
00189 for (int k=0;k<dimw;k++)
00190 p[k] = coord[k];
00191 vtxParam.push_back(make_pair(p,param));
00192 }
00193 }
00194 }
00195
00197 GridPtr() : MacroGrid() , gridptr_() ,
00198 emptyParam(), elParam(0), vtxParam(0),
00199 nofElParam_(0), nofVtxParam_(0) {}
00200
00202 GridPtr(GridType * grd) : MacroGrid() , gridptr_(grd),
00203 emptyParam(), elParam(0), vtxParam(0),
00204 nofElParam_(0), nofVtxParam_(0) {}
00205
00207 GridPtr(const GridPtr & org) : gridptr_(org.gridptr_),
00208 emptyParam(),
00209 elParam(org.elParam), vtxParam(org.vtxParam),
00210 nofElParam_(org.nofElParam_),
00211 nofVtxParam_(org.nofVtxParam_) {}
00212
00214 GridType& operator*() {
00215 return *gridptr_;
00216 }
00218 GridType* operator->() {
00219 return gridptr_.operator -> ();
00220 }
00221
00223 const GridType& operator*() const {
00224 return *gridptr_;
00225 }
00226
00228 const GridType* operator->() const {
00229 return gridptr_.operator -> ();
00230 }
00231
00233 GridType* release () {
00234 return gridptr_.release();
00235 }
00236
00238 GridPtr & operator = (const GridPtr & org)
00239 {
00240 gridptr_ = org.gridptr_;
00241 elParam = org.elParam;
00242 vtxParam = org.vtxParam;
00243 nofVtxParam_ = org.nofVtxParam_;
00244 nofElParam_ = org.nofElParam_;
00245 return *this;
00246 }
00247
00249 GridPtr & operator = (GridType * grd)
00250 {
00251 gridptr_ = std::auto_ptr<GridType>(grd);
00252 return *this;
00253 }
00255 int nofParameters(int cdim) {
00256 switch (cdim) {
00257 case 0: return nofElParam_; break;
00258 case GridType::dimension: return nofVtxParam_; break;
00259 }
00260 return 0;
00261 }
00263 template <class Entity>
00264 std::vector<double>& parameters(const Entity& en) {
00265 if (Entity::codimension==0) {
00266 if (elParam.size()==0)
00267 return emptyParam;
00268 DomainType coord(0);
00269 typedef typename Entity::Geometry GeometryType;
00270 const GeometryType& geo=en.geometry();
00271 for (int i=0;i<geo.corners();++i)
00272 coord += geo[i];
00273 coord/=double(geo.corners());
00274 return elementParams(coord);
00275 } else if (int(Entity::codimension) == int(GridType::dimension)) {
00276 if (vtxParam.size()==0)
00277 return emptyParam;
00278 DomainType coord;
00279 typedef typename Entity::Geometry GeometryType;
00280 const GeometryType& geo=en.geometry();
00281 coord = geo[0];
00282 return vertexParams(coord);
00283 } else {
00284 return emptyParam;
00285 }
00286 }
00287 protected:
00288 typedef FieldVector<typename GridType::ctype,GridType::dimensionworld> DomainType;
00289 inline std::vector<double>& elementParams(DomainType& coord) {
00290 int idx=0;
00291 double min=1e10;
00292 for (size_t i=0;i<elParam.size();++i) {
00293 DomainType p(coord);
00294 p -= elParam[i].first;
00295 double len=p.two_norm();
00296 if (min>len) {
00297 min=len;
00298 idx=i;
00299 }
00300 }
00301 if (idx<0)
00302 return emptyParam;
00303 else
00304 return elParam[idx].second;
00305 }
00306 inline std::vector<double>& vertexParams(DomainType& coord) {
00307 int idx=0;
00308 double min=1e10;
00309 for (size_t i=0;i<vtxParam.size();++i) {
00310 DomainType p(coord);
00311 p -= vtxParam[i].first;
00312 double len=p.two_norm();
00313 if (min>len) {
00314 min=len;
00315 idx=i;
00316 }
00317 }
00318 if (idx<0)
00319 return emptyParam;
00320 else
00321 return vtxParam[idx].second;
00322 }
00323
00324 mutable std::auto_ptr<GridType> gridptr_;
00325 std::vector<double> emptyParam;
00326
00327 std::vector<std::pair<DomainType,std::vector<double> > > elParam,vtxParam;
00328 int nofElParam_,nofVtxParam_;
00329 };
00330
00331 }
00332
00951
00952
00953
00954
00955
00956
00957
00958
00959 namespace Dune {
00960
00963 template <class GridType>
00964 struct DGFGridInfo
00965 {
00967 static int refineStepsForHalf();
00970 static double refineWeight();
00971 };
00972
00973 }
00974 #endif