4 #ifndef DUNE_GMSHREADER_HH
5 #define DUNE_GMSHREADER_HH
18 #include <dune/common/exceptions.hh>
19 #include <dune/common/fvector.hh>
21 #include <dune/geometry/type.hh>
48 template<
int dimension,
int dimWorld = dimension >
49 class GmshReaderQuadraticBoundarySegment
64 template<
int dimWorld >
65 struct GmshReaderQuadraticBoundarySegment< 2,
dimWorld >
68 typedef Dune::FieldVector< double, dimWorld >
GlobalVector;
71 : p0(p0_), p1(p1_), p2(p2_)
78 alpha=d1.two_norm()/(d1.two_norm()+d2.two_norm());
79 if (alpha<1E-6 || alpha>1-1E-6)
80 DUNE_THROW(Dune::IOError,
"ration in quadratic boundary segment bad");
83 virtual GlobalVector operator() (
const Dune::FieldVector<double,1> &local )
const
87 y.axpy((local[0]-alpha)*(local[0]-1.0)/alpha,p0);
88 y.axpy(local[0]*(local[0]-1.0)/(alpha*(alpha-1.0)),p1);
89 y.axpy(local[0]*(local[0]-alpha)/(1.0-alpha),p2);
123 class GmshReaderQuadraticBoundarySegment< 3, 3 >
127 GmshReaderQuadraticBoundarySegment (Dune::FieldVector<double,3> p0_, Dune::FieldVector<double,3> p1_,
128 Dune::FieldVector<double,3> p2_, Dune::FieldVector<double,3> p3_,
129 Dune::FieldVector<double,3> p4_, Dune::FieldVector<double,3> p5_)
130 : p0(p0_), p1(p1_), p2(p2_), p3(p3_), p4(p4_), p5(p5_)
133 Dune::FieldVector<double,3> d1,d2;
137 alpha=d1.two_norm()/(d1.two_norm()+d2.two_norm());
138 if (alpha<1E-6 || alpha>1-1E-6)
139 DUNE_THROW(Dune::IOError,
"alpha in quadratic boundary segment bad");
143 beta=d1.two_norm()/(d1.two_norm()+d2.two_norm());
144 if (beta<1E-6 || beta>1-1E-6)
145 DUNE_THROW(Dune::IOError,
"beta in quadratic boundary segment bad");
149 gamma=sqrt2*(d1.two_norm()/(d1.two_norm()+d2.two_norm()));
150 if (gamma<1E-6 || gamma>1-1E-6)
151 DUNE_THROW(Dune::IOError,
"gamma in quadratic boundary segment bad");
154 virtual Dune::FieldVector<double,3> operator() (
const Dune::FieldVector<double,2>& local)
const
156 Dune::FieldVector<double,3> y;
158 y.axpy(phi0(local),p0);
159 y.axpy(phi1(local),p1);
160 y.axpy(phi2(local),p2);
161 y.axpy(phi3(local),p3);
162 y.axpy(phi4(local),p4);
163 y.axpy(phi5(local),p5);
171 double phi0 (
const Dune::FieldVector<double,2>& local)
const
173 return (alpha*beta-beta*local[0]-alpha*local[1])*(1-local[0]-local[1])/(alpha*beta);
175 double phi3 (
const Dune::FieldVector<double,2>& local)
const
177 return local[0]*(1-local[0]-local[1])/(alpha*(1-alpha));
179 double phi1 (
const Dune::FieldVector<double,2>& local)
const
181 return local[0]*(gamma*local[0]-(sqrt2-gamma-sqrt2*alpha)*local[1]-alpha*gamma)/(gamma*(1-alpha));
183 double phi5 (
const Dune::FieldVector<double,2>& local)
const
185 return local[1]*(1-local[0]-local[1])/(beta*(1-beta));
187 double phi4 (
const Dune::FieldVector<double,2>& local)
const
189 return local[0]*local[1]/((1-gamma/sqrt2)*gamma/sqrt2);
191 double phi2 (
const Dune::FieldVector<double,2>& local)
const
193 return local[1]*(beta*(1-gamma/sqrt2)-local[0]*(beta-gamma/sqrt2)-local[1]*(1-gamma/sqrt2))/((1-gamma/sqrt2)*(beta-1));
196 Dune::FieldVector<double,3> p0,p1,p2,p3,p4,p5;
197 double alpha,beta,gamma,sqrt2;
203 template<
typename Gr
idType>
222 static const int dim = GridType::dimension;
223 static const int dimWorld = GridType::dimensionworld;
233 void readfile(FILE * file,
int cnt,
const char * format,
234 void* t1,
void* t2 = 0,
void* t3 = 0,
void* t4 = 0,
235 void* t5 = 0,
void* t6 = 0,
void* t7 = 0,
void* t8 = 0,
236 void* t9 = 0,
void* t10 = 0)
238 off_t pos = ftello(file);
239 int c = fscanf(file, format, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
241 DUNE_THROW(Dune::IOError,
"Error parsing " <<
fileName <<
" "
243 <<
": Expected '" << format <<
"', only read " << c <<
" entries instead of " << cnt <<
".");
251 c = std::fgetc(file);
252 }
while(c !=
'\n' && c != EOF);
270 void read (
const std::string& f)
272 if (
verbose) std::cout <<
"Reading " <<
dim <<
"d Gmsh grid..." << std::endl;
276 FILE* file = fopen(
fileName.c_str(),
"r");
278 DUNE_THROW(Dune::IOError,
"Could not open " <<
fileName);
290 double version_number;
291 int file_type, data_size;
294 if (strcmp(
buf,
"$MeshFormat")!=0)
295 DUNE_THROW(Dune::IOError,
"expected $MeshFormat in first line");
296 readfile(file,3,
"%lg %d %d\n",&version_number,&file_type,&data_size);
297 if( (version_number < 2.0) || (version_number > 2.2) )
298 DUNE_THROW(Dune::IOError,
"can only read Gmsh version 2 files");
299 if (
verbose) std::cout <<
"version " << version_number <<
" Gmsh file detected" << std::endl;
301 if (strcmp(
buf,
"$EndMeshFormat")!=0)
302 DUNE_THROW(Dune::IOError,
"expected $EndMeshFormat");
308 if (strcmp(
buf,
"$Nodes")!=0)
309 DUNE_THROW(Dune::IOError,
"expected $Nodes");
310 readfile(file,1,
"%d\n",&number_of_nodes);
311 if (
verbose) std::cout <<
"file contains " << number_of_nodes <<
" nodes" << std::endl;
314 std::vector< GlobalVector > nodes( number_of_nodes+1 );
318 for(
int i = 1; i <= number_of_nodes; ++i )
320 readfile(file,4,
"%d %lg %lg %lg\n", &
id, &x[ 0 ], &x[ 1 ], &x[ 2 ] );
322 DUNE_THROW( Dune::IOError,
"Expected id " << i <<
"(got id " <<
id <<
"." );
326 nodes[ i ][ j ] = x[ j ];
329 if (strcmp(
buf,
"$EndNodes")!=0)
330 DUNE_THROW(Dune::IOError,
"expected $EndNodes");
335 if (strcmp(
buf,
"$Elements")!=0)
336 DUNE_THROW(Dune::IOError,
"expected $Elements");
337 int number_of_elements;
338 readfile(file,1,
"%d\n",&number_of_elements);
339 if (
verbose) std::cout <<
"file contains " << number_of_elements <<
" elements" << std::endl;
345 long section_element_offset = ftell(file);
346 std::map<int,unsigned int>
renumber;
347 for (
int i=1; i<=number_of_elements; i++)
349 int id, elm_type, number_of_tags;
350 readfile(file,3,
"%d %d %d ",&
id,&elm_type,&number_of_tags);
351 for (
int k=1; k<=number_of_tags; k++)
369 if (strcmp(
buf,
"$EndElements")!=0)
370 DUNE_THROW(Dune::IOError,
"expected $EndElements");
378 fseek(file, section_element_offset, SEEK_SET);
381 for (
int i=1; i<=number_of_elements; i++)
383 int id, elm_type, number_of_tags;
384 readfile(file,3,
"%d %d %d ",&
id,&elm_type,&number_of_tags);
385 int physical_entity = -1;
386 std::vector<int> mesh_partitions;
387 if ( version_number < 2.2 )
389 mesh_partitions.resize(1);
391 for (
int k=1; k<=number_of_tags; k++)
395 if (k==1) physical_entity = blub;
397 if ( version_number < 2.2 )
399 if (k==3) mesh_partitions[0] = blub;
404 mesh_partitions[k-4] = blub;
406 mesh_partitions.resize(blub);
412 if (strcmp(
buf,
"$EndElements")!=0)
413 DUNE_THROW(Dune::IOError,
"expected $EndElements");
420 std::map<int,unsigned int> &
renumber,
421 const std::vector< GlobalVector > & nodes)
424 const int nDofs[12] = {-1, 2, 3, 4, 4, 8, 6, 5, 3, 6, -1, 10};
425 const int nVertices[12] = {-1, 2, 3, 4, 4, 8, 6, 5, 2, 3, -1, 4};
426 const int elementDim[12] = {-1, 1, 2, 2, 3, 3, 3, 3, 1, 2, -1, 3};
429 if ( not (elm_type >= 0 && elm_type < 12
430 && (elementDim[elm_type] ==
dim || elementDim[elm_type] == (
dim-1) ) ) )
437 std::string formatString =
"%d";
438 for (
int i=1; i<nDofs[elm_type]; i++)
439 formatString +=
" %d";
440 formatString +=
"\n";
443 std::vector<int> elementDofs(10);
445 readfile(file,nDofs[elm_type], formatString.c_str(),
446 &(elementDofs[0]),&(elementDofs[1]),&(elementDofs[2]),
447 &(elementDofs[3]),&(elementDofs[4]),&(elementDofs[5]),
448 &(elementDofs[6]),&(elementDofs[7]),&(elementDofs[8]),
452 for (
int i=0; i<nVertices[elm_type]; i++)
453 if (renumber.find(elementDofs[i])==renumber.end())
456 factory.insertVertex(nodes[elementDofs[i]]);
460 if (elementDim[elm_type] ==
dim)
470 template <
class E,
class V,
class V2>
473 const E& elementDofs,
477 DUNE_THROW(Dune::IOError,
"tried to create a 3D boundary segment in a non-3D Grid");
481 template <
class E,
class V>
483 const std::vector<FieldVector<double, 3> >& nodes,
484 const E& elementDofs,
488 array<FieldVector<double,dimWorld>, 6> v;
489 for (
int i=0; i<6; i++)
491 v[i][j] = nodes[elementDofs[i]][j];
497 factory.insertBoundarySegment( vertices,
506 std::map<int,unsigned int> &
renumber,
507 const std::vector< GlobalVector > & nodes,
508 const int physical_entity)
511 const int nDofs[12] = {-1, 2, 3, 4, 4, 8, 6, 5, 3, 6, -1, 10};
512 const int nVertices[12] = {-1, 2, 3, 4, 4, 8, 6, 5, 2, 3, -1, 4};
513 const int elementDim[12] = {-1, 1, 2, 2, 3, 3, 3, 3, 1, 2, -1, 3};
516 if ( not (elm_type >= 0 && elm_type < 12
517 && (elementDim[elm_type] ==
dim || elementDim[elm_type] == (
dim-1) ) ) )
524 std::string formatString =
"%d";
525 for (
int i=1; i<nDofs[elm_type]; i++)
526 formatString +=
" %d";
527 formatString +=
"\n";
530 std::vector<int> elementDofs(10);
532 readfile(file,nDofs[elm_type], formatString.c_str(),
533 &(elementDofs[0]),&(elementDofs[1]),&(elementDofs[2]),
534 &(elementDofs[3]),&(elementDofs[4]),&(elementDofs[5]),
535 &(elementDofs[6]),&(elementDofs[7]),&(elementDofs[8]),
542 std::swap(elementDofs[2],elementDofs[3]);
545 std::swap(elementDofs[2],elementDofs[3]);
546 std::swap(elementDofs[6],elementDofs[7]);
549 std::swap(elementDofs[2],elementDofs[3]);
555 std::vector<unsigned int> vertices(nVertices[elm_type]);
557 for (
int i=0; i<nVertices[elm_type]; i++)
558 vertices[i] = renumber[elementDofs[i]];
561 if (elementDim[elm_type] ==
dim) {
601 factory.insertBoundarySegment(vertices);
605 factory.insertBoundarySegment(vertices);
609 array<FieldVector<double,dimWorld>, 3> v;
611 v[0][i] = nodes[elementDofs[0]][i];
612 v[1][i] = nodes[elementDofs[2]][i];
613 v[2][i] = nodes[elementDofs[1]][i];
617 factory.insertBoundarySegment(vertices,
632 if (elementDim[elm_type] ==
dim) {
660 template<
typename Gr
idType>
667 static Grid*
read (
const std::string& fileName,
bool verbose =
true,
bool insert_boundary_segments=
true)
674 parser.
read(fileName);
681 std::vector<int>& boundary_id_to_physical_entity,
682 std::vector<int>& element_index_to_physical_entity,
683 bool verbose =
true,
bool insert_boundary_segments=
true)
690 parser.
read(fileName);
700 bool verbose =
true,
bool insert_boundary_segments=
true)
704 parser.
read(fileName);
709 const std::string& fileName,
710 std::vector<int>& boundary_id_to_physical_entity,
711 std::vector<int>& element_index_to_physical_entity,
712 bool verbose =
true,
bool insert_boundary_segments=
true)
716 parser.
read(fileName);