4 #ifndef DUNE_GMSHREADER_HH
5 #define DUNE_GMSHREADER_HH
17 #include <dune/common/exceptions.hh>
18 #include <dune/common/fvector.hh>
19 #include <dune/common/to_unique_ptr.hh>
21 #include <dune/geometry/type.hh>
48 template<
int dimension,
int dimWorld = dimension >
49 class GmshReaderQuadraticBoundarySegment
53 static void registerFactory() {}
68 template<
int dimWorld >
69 struct GmshReaderQuadraticBoundarySegment< 2,
dimWorld >
72 typedef GmshReaderQuadraticBoundarySegment< 2, dimWorld > ThisType;
74 typedef Dune::FieldVector< double, dimWorld >
GlobalVector;
77 : p0(p0_), p1(p1_), p2(p2_)
82 GmshReaderQuadraticBoundarySegment( ObjectStreamType& in )
85 const int bytes =
sizeof(double)*
dimWorld;
86 in.read( (
char *) &p0[ 0 ], bytes );
87 in.read( (
char *) &p1[ 0 ], bytes );
88 in.read( (
char *) &p2[ 0 ], bytes );
92 static void registerFactory()
100 virtual GlobalVector operator() (
const Dune::FieldVector<double,1> &local )
const
104 y.axpy((local[0]-alpha)*(local[0]-1.0)/alpha,p0);
105 y.axpy(local[0]*(local[0]-1.0)/(alpha*(alpha-1.0)),p1);
106 y.axpy(local[0]*(local[0]-alpha)/(1.0-alpha),p2);
110 void backup( ObjectStreamType& out )
const
113 out.write( (
const char *) &key(),
sizeof(
int ) );
115 const int bytes =
sizeof(double)*
dimWorld;
116 out.write( (
const char*) &p0[ 0 ], bytes );
117 out.write( (
const char*) &p1[ 0 ], bytes );
118 out.write( (
const char*) &p2[ 0 ], bytes );
129 alpha=d1.two_norm()/(d1.two_norm()+d2.two_norm());
130 if (alpha<1E-6 || alpha>1-1E-6)
131 DUNE_THROW(Dune::IOError,
"ration in quadratic boundary segment bad");
169 class GmshReaderQuadraticBoundarySegment< 3, 3 >
172 typedef GmshReaderQuadraticBoundarySegment< 3, 3 > ThisType;
175 GmshReaderQuadraticBoundarySegment (Dune::FieldVector<double,3> p0_, Dune::FieldVector<double,3> p1_,
176 Dune::FieldVector<double,3> p2_, Dune::FieldVector<double,3> p3_,
177 Dune::FieldVector<double,3> p4_, Dune::FieldVector<double,3> p5_)
178 : p0(p0_), p1(p1_), p2(p2_), p3(p3_), p4(p4_), p5(p5_)
183 GmshReaderQuadraticBoundarySegment( ObjectStreamType& in )
185 const int bytes =
sizeof(double)*3;
186 in.read( (
char *) &p0[ 0 ], bytes );
187 in.read( (
char *) &p1[ 0 ], bytes );
188 in.read( (
char *) &p2[ 0 ], bytes );
189 in.read( (
char *) &p3[ 0 ], bytes );
190 in.read( (
char *) &p4[ 0 ], bytes );
191 in.read( (
char *) &p5[ 0 ], bytes );
195 static void registerFactory()
203 virtual Dune::FieldVector<double,3> operator() (
const Dune::FieldVector<double,2>& local)
const
205 Dune::FieldVector<double,3> y;
207 y.axpy(phi0(local),p0);
208 y.axpy(phi1(local),p1);
209 y.axpy(phi2(local),p2);
210 y.axpy(phi3(local),p3);
211 y.axpy(phi4(local),p4);
212 y.axpy(phi5(local),p5);
216 void backup( ObjectStreamType& out )
const
219 out.write( (
const char*) &key(),
sizeof(
int ) );
221 const int bytes =
sizeof(double)*3;
222 out.write( (
const char*) &p0[ 0 ], bytes );
223 out.write( (
const char*) &p1[ 0 ], bytes );
224 out.write( (
const char*) &p2[ 0 ], bytes );
225 out.write( (
const char*) &p3[ 0 ], bytes );
226 out.write( (
const char*) &p4[ 0 ], bytes );
227 out.write( (
const char*) &p5[ 0 ], bytes );
234 Dune::FieldVector<double,3> d1,d2;
238 alpha=d1.two_norm()/(d1.two_norm()+d2.two_norm());
239 if (alpha<1E-6 || alpha>1-1E-6)
240 DUNE_THROW(Dune::IOError,
"alpha in quadratic boundary segment bad");
244 beta=d1.two_norm()/(d1.two_norm()+d2.two_norm());
245 if (beta<1E-6 || beta>1-1E-6)
246 DUNE_THROW(Dune::IOError,
"beta in quadratic boundary segment bad");
250 gamma=sqrt2*(d1.two_norm()/(d1.two_norm()+d2.two_norm()));
251 if (gamma<1E-6 || gamma>1-1E-6)
252 DUNE_THROW(Dune::IOError,
"gamma in quadratic boundary segment bad");
264 double phi0 (
const Dune::FieldVector<double,2>& local)
const
266 return (alpha*beta-beta*local[0]-alpha*local[1])*(1-local[0]-local[1])/(alpha*beta);
268 double phi3 (
const Dune::FieldVector<double,2>& local)
const
270 return local[0]*(1-local[0]-local[1])/(alpha*(1-alpha));
272 double phi1 (
const Dune::FieldVector<double,2>& local)
const
274 return local[0]*(gamma*local[0]-(sqrt2-gamma-sqrt2*alpha)*local[1]-alpha*gamma)/(gamma*(1-alpha));
276 double phi5 (
const Dune::FieldVector<double,2>& local)
const
278 return local[1]*(1-local[0]-local[1])/(beta*(1-beta));
280 double phi4 (
const Dune::FieldVector<double,2>& local)
const
282 return local[0]*local[1]/((1-gamma/sqrt2)*gamma/sqrt2);
284 double phi2 (
const Dune::FieldVector<double,2>& local)
const
286 return local[1]*(beta*(1-gamma/sqrt2)-local[0]*(beta-gamma/sqrt2)-local[1]*(1-gamma/sqrt2))/((1-gamma/sqrt2)*(beta-1));
289 Dune::FieldVector<double,3> p0,p1,p2,p3,p4,p5;
290 double alpha,beta,gamma,sqrt2;
296 template<
typename Gr
idType>
315 static const int dim = GridType::dimension;
316 static const int dimWorld = GridType::dimensionworld;
317 static_assert( (
dimWorld <= 3),
"GmshReader requires dimWorld <= 3." );
326 void readfile(FILE * file,
int cnt,
const char * format,
327 void* t1,
void* t2 = 0,
void* t3 = 0,
void* t4 = 0,
328 void* t5 = 0,
void* t6 = 0,
void* t7 = 0,
void* t8 = 0,
329 void* t9 = 0,
void* t10 = 0)
331 off_t pos = ftello(file);
332 int c = fscanf(file, format, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
334 DUNE_THROW(Dune::IOError,
"Error parsing " <<
fileName <<
" "
336 <<
": Expected '" << format <<
"', only read " << c <<
" entries instead of " << cnt <<
".");
344 c = std::fgetc(file);
345 }
while(c !=
'\n' && c != EOF);
363 void read (
const std::string& f)
365 if (
verbose) std::cout <<
"Reading " <<
dim <<
"d Gmsh grid..." << std::endl;
369 FILE* file = fopen(
fileName.c_str(),
"rb");
371 DUNE_THROW(Dune::IOError,
"Could not open " <<
fileName);
383 double version_number;
384 int file_type, data_size;
387 if (strcmp(
buf,
"$MeshFormat")!=0)
388 DUNE_THROW(Dune::IOError,
"expected $MeshFormat in first line");
389 readfile(file,3,
"%lg %d %d\n",&version_number,&file_type,&data_size);
390 if( (version_number < 2.0) || (version_number > 2.2) )
391 DUNE_THROW(Dune::IOError,
"can only read Gmsh version 2 files");
392 if (
verbose) std::cout <<
"version " << version_number <<
" Gmsh file detected" << std::endl;
394 if (strcmp(
buf,
"$EndMeshFormat")!=0)
395 DUNE_THROW(Dune::IOError,
"expected $EndMeshFormat");
401 if (strcmp(
buf,
"$Nodes")!=0)
402 DUNE_THROW(Dune::IOError,
"expected $Nodes");
403 readfile(file,1,
"%d\n",&number_of_nodes);
404 if (
verbose) std::cout <<
"file contains " << number_of_nodes <<
" nodes" << std::endl;
408 std::vector< GlobalVector > nodes( number_of_nodes+1 );
412 for(
int i = 1; i <= number_of_nodes; ++i )
414 readfile(file,4,
"%d %lg %lg %lg\n", &
id, &x[ 0 ], &x[ 1 ], &x[ 2 ] );
416 if (
id > number_of_nodes) {
417 DUNE_THROW(Dune::IOError,
418 "Only dense sequences of node indices are currently supported (node index "
419 <<
id <<
" is invalid).");
424 nodes[
id ][ j ] = x[ j ];
427 if (strcmp(
buf,
"$EndNodes")!=0)
428 DUNE_THROW(Dune::IOError,
"expected $EndNodes");
433 if (strcmp(
buf,
"$Elements")!=0)
434 DUNE_THROW(Dune::IOError,
"expected $Elements");
435 int number_of_elements;
436 readfile(file,1,
"%d\n",&number_of_elements);
437 if (
verbose) std::cout <<
"file contains " << number_of_elements <<
" elements" << std::endl;
444 off_t section_element_offset = ftello(file);
445 std::map<int,unsigned int>
renumber;
446 for (
int i=1; i<=number_of_elements; i++)
448 int id, elm_type, number_of_tags;
449 readfile(file,3,
"%d %d %d ",&
id,&elm_type,&number_of_tags);
450 for (
int k=1; k<=number_of_tags; k++)
468 if (strcmp(
buf,
"$EndElements")!=0)
469 DUNE_THROW(Dune::IOError,
"expected $EndElements");
477 fseeko(file, section_element_offset, SEEK_SET);
480 for (
int i=1; i<=number_of_elements; i++)
482 int id, elm_type, number_of_tags;
483 readfile(file,3,
"%d %d %d ",&
id,&elm_type,&number_of_tags);
484 int physical_entity = -1;
486 for (
int k=1; k<=number_of_tags; k++)
490 if (k==1) physical_entity = blub;
495 if (strcmp(
buf,
"$EndElements")!=0)
496 DUNE_THROW(Dune::IOError,
"expected $EndElements");
507 std::map<int,unsigned int> &
renumber,
508 const std::vector< GlobalVector > & nodes)
511 const int nDofs[16] = {-1, 2, 3, 4, 4, 8, 6, 5, 3, 6, -1, 10, -1, -1, -1, 1};
512 const int nVertices[16] = {-1, 2, 3, 4, 4, 8, 6, 5, 2, 3, -1, 4, -1, -1, -1, 1};
513 const int elementDim[16] = {-1, 1, 2, 2, 3, 3, 3, 3, 1, 2, -1, 3, -1, -1, -1, 0};
516 if ( not (elm_type > 0 && elm_type <= 15
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]),
539 for (
int i=0; i<nVertices[elm_type]; i++)
543 factory.insertVertex(nodes[elementDofs[i]]);
547 if (elementDim[elm_type] ==
dim)
557 template <
class E,
class V,
class V2>
560 const E& elementDofs,
564 DUNE_THROW(Dune::IOError,
"tried to create a 3D boundary segment in a non-3D Grid");
568 template <
class E,
class V>
570 const std::vector<FieldVector<double, 3> >& nodes,
571 const E& elementDofs,
575 std::array<FieldVector<double,dimWorld>, 6> v;
576 for (
int i=0; i<6; i++)
578 v[i][j] = nodes[elementDofs[i]][j];
584 factory.insertBoundarySegment( vertices,
595 std::map<int,unsigned int> &
renumber,
596 const std::vector< GlobalVector > & nodes,
597 const int physical_entity)
600 const int nDofs[16] = {-1, 2, 3, 4, 4, 8, 6, 5, 3, 6, -1, 10, -1, -1, -1, 1};
601 const int nVertices[16] = {-1, 2, 3, 4, 4, 8, 6, 5, 2, 3, -1, 4, -1, -1, -1, 1};
602 const int elementDim[16] = {-1, 1, 2, 2, 3, 3, 3, 3, 1, 2, -1, 3, -1, -1, -1, 0};
605 if ( not (elm_type > 0 && elm_type <= 15
606 && (elementDim[elm_type] ==
dim || elementDim[elm_type] == (
dim-1) ) ) )
613 std::string formatString =
"%d";
614 for (
int i=1; i<nDofs[elm_type]; i++)
615 formatString +=
" %d";
616 formatString +=
"\n";
619 std::vector<int> elementDofs(10);
621 readfile(file,nDofs[elm_type], formatString.c_str(),
622 &(elementDofs[0]),&(elementDofs[1]),&(elementDofs[2]),
623 &(elementDofs[3]),&(elementDofs[4]),&(elementDofs[5]),
624 &(elementDofs[6]),&(elementDofs[7]),&(elementDofs[8]),
631 std::swap(elementDofs[2],elementDofs[3]);
634 std::swap(elementDofs[2],elementDofs[3]);
635 std::swap(elementDofs[6],elementDofs[7]);
638 std::swap(elementDofs[2],elementDofs[3]);
644 std::vector<unsigned int> vertices(nVertices[elm_type]);
646 for (
int i=0; i<nVertices[elm_type]; i++)
647 vertices[i] =
renumber[elementDofs[i]];
650 if (elementDim[elm_type] ==
dim) {
690 factory.insertBoundarySegment(vertices);
694 factory.insertBoundarySegment(vertices);
698 factory.insertBoundarySegment(vertices);
702 factory.insertBoundarySegment(vertices);
706 std::array<FieldVector<double,dimWorld>, 3> v;
708 v[0][i] = nodes[elementDofs[0]][i];
709 v[1][i] = nodes[elementDofs[2]][i];
710 v[2][i] = nodes[elementDofs[1]][i];
714 factory.insertBoundarySegment(vertices,
729 if (elementDim[elm_type] ==
dim) {
765 template<
typename Gr
idType>
772 GmshReaderQuadraticBoundarySegment< Grid::dimension, Grid::dimensionworld >::registerFactory();
776 factory.
comm().barrier();
788 static ToUniquePtr<Grid>
read (
const std::string& fileName,
bool verbose =
true,
bool insertBoundarySegments=
true)
794 registerFactory( factory );
797 if (factory.
comm().rank() == 0)
800 parser.
read(fileName);
812 static ToUniquePtr<Grid>
read (
const std::string& fileName,
813 std::vector<int>& boundarySegmentToPhysicalEntity,
814 std::vector<int>& elementToPhysicalEntity,
815 bool verbose =
true,
bool insertBoundarySegments=
true)
821 registerFactory( factory );
824 if (factory.
comm().rank() == 0)
827 parser.
read(fileName);
829 boundarySegmentToPhysicalEntity.swap(parser.
boundaryIdMap());
838 bool verbose =
true,
bool insertBoundarySegments=
true)
841 registerFactory( factory );
844 if (factory.
comm().rank() == 0)
847 parser.
read(fileName);
853 const std::string& fileName,
854 std::vector<int>& boundarySegmentToPhysicalEntity,
855 std::vector<int>& elementToPhysicalEntity,
856 bool verbose =
true,
bool insertBoundarySegments=
true)
859 registerFactory( factory );
862 if (factory.
comm().rank() == 0)
865 parser.
read(fileName);
867 boundarySegmentToPhysicalEntity.swap(parser.
boundaryIdMap());
Base class for grid boundary segments of arbitrary geometry.
Include standard header files.
Definition: agrid.hh:59
static const int dimWorld
Definition: misc.hh:44
ALBERTA REAL_D GlobalVector
Definition: misc.hh:48
int renumber(const Dune::GeometryType &t, int i)
renumber VTK <-> Dune
Definition: common.hh:234
@ line
Definition: common.hh:182
@ pyramid
Definition: common.hh:189
@ quadrilateral
Definition: common.hh:185
@ tetrahedron
Definition: common.hh:186
@ prism
Definition: common.hh:188
@ hexahedron
Definition: common.hh:187
@ triangle
Definition: common.hh:183
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:92
Communication comm() const
Return the Communication used by the grid factory.
Definition: common/gridfactory.hh:252
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:269
virtual ToUniquePtr< GridType > createGrid()
Finalize grid creation and hand over the grid.
Definition: common/gridfactory.hh:327
Options for read operation.
Definition: gmshreader.hh:36
GeometryOrder
Definition: gmshreader.hh:37
@ firstOrder
edges are straight lines.
Definition: gmshreader.hh:39
@ secondOrder
quadratic boundary approximation.
Definition: gmshreader.hh:41
dimension independent parts for GmshReaderParser
Definition: gmshreader.hh:298
void pass1HandleElement(FILE *file, const int elm_type, std::map< int, unsigned int > &renumber, const std::vector< GlobalVector > &nodes)
Process one element during the first pass through the list of all elements.
Definition: gmshreader.hh:506
static const int dimWorld
Definition: gmshreader.hh:316
Dune::GridFactory< GridType > & factory
Definition: gmshreader.hh:301
std::vector< int > & elementIndexMap()
Definition: gmshreader.hh:358
void boundarysegment_insert(const V &nodes, const E &elementDofs, const V2 &vertices)
Definition: gmshreader.hh:558
unsigned int number_of_real_vertices
Definition: gmshreader.hh:304
void boundarysegment_insert(const std::vector< FieldVector< double, 3 > > &nodes, const E &elementDofs, const V &vertices)
Definition: gmshreader.hh:569
GmshReaderParser(Dune::GridFactory< GridType > &_factory, bool v, bool i)
Definition: gmshreader.hh:350
int element_count
Definition: gmshreader.hh:306
void read(const std::string &f)
Definition: gmshreader.hh:363
void skipline(FILE *file)
Definition: gmshreader.hh:340
void readfile(FILE *file, int cnt, const char *format, void *t1, void *t2=0, void *t3=0, void *t4=0, void *t5=0, void *t6=0, void *t7=0, void *t8=0, void *t9=0, void *t10=0)
Definition: gmshreader.hh:326
std::vector< int > element_index_to_physical_entity
Definition: gmshreader.hh:312
virtual void pass2HandleElement(FILE *file, const int elm_type, std::map< int, unsigned int > &renumber, const std::vector< GlobalVector > &nodes, const int physical_entity)
Process one element during the second pass through the list of all elements.
Definition: gmshreader.hh:594
std::vector< int > & boundaryIdMap()
Definition: gmshreader.hh:353
static const int dim
Definition: gmshreader.hh:315
FieldVector< double, dimWorld > GlobalVector
Definition: gmshreader.hh:317
std::string fileName
Definition: gmshreader.hh:309
int boundary_element_count
Definition: gmshreader.hh:305
bool verbose
Definition: gmshreader.hh:302
std::vector< int > boundary_id_to_physical_entity
Definition: gmshreader.hh:311
char buf[512]
Definition: gmshreader.hh:308
bool insert_boundary_segments
Definition: gmshreader.hh:303
Read Gmsh mesh file.
Definition: gmshreader.hh:767
static ToUniquePtr< Grid > read(const std::string &fileName, std::vector< int > &boundarySegmentToPhysicalEntity, std::vector< int > &elementToPhysicalEntity, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:812
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:837
GridType Grid
Definition: gmshreader.hh:780
static ToUniquePtr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:788
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, std::vector< int > &boundarySegmentToPhysicalEntity, std::vector< int > &elementToPhysicalEntity, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:852
Provide a generic factory class for unstructured grids.