4#ifndef DUNE_GMSHREADER_HH
5#define DUNE_GMSHREADER_HH
48 template<
int dimension,
int dimWorld = dimension >
49 class GmshReaderQuadraticBoundarySegment
64 template<
int dimWorld >
65 struct GmshReaderQuadraticBoundarySegment< 2, dimWorld >
70 GmshReaderQuadraticBoundarySegment (
const GlobalVector &p0_,
const GlobalVector &p1_,
const GlobalVector &p2_)
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)
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);
94 GlobalVector p0,p1,p2;
123 class GmshReaderQuadraticBoundarySegment< 3, 3 >
130 : p0(p0_), p1(p1_), p2(p2_), p3(p3_), p4(p4_), p5(p5_)
138 if (alpha<1E-6 || alpha>1-1E-6)
144 if (beta<1E-6 || beta>1-1E-6)
150 if (gamma<1E-6 || gamma>1-1E-6)
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);
173 return (alpha*beta-beta*local[0]-alpha*local[1])*(1-local[0]-local[1])/(alpha*beta);
177 return local[0]*(1-local[0]-local[1])/(alpha*(1-alpha));
181 return local[0]*(gamma*local[0]-(sqrt2-gamma-sqrt2*alpha)*local[1]-alpha*gamma)/(gamma*(1-alpha));
185 return local[1]*(1-local[0]-local[1])/(beta*(1-beta));
189 return local[0]*local[1]/((1-gamma/sqrt2)*gamma/sqrt2);
193 return local[1]*(beta*(1-gamma/sqrt2)-local[0]*(beta-gamma/sqrt2)-local[1]*(1-gamma/sqrt2))/((1-gamma/sqrt2)*(beta-1));
197 double alpha,beta,gamma,sqrt2;
203 template<
typename Gr
idType>
210 bool insert_boundary_segments;
211 unsigned int number_of_real_vertices;
212 int boundary_element_count;
216 std::string fileName;
218 std::vector<int> boundary_id_to_physical_entity;
219 std::vector<int> element_index_to_physical_entity;
222 static const int dim = GridType::dimension;
223 static const int dimWorld = GridType::dimensionworld;
224 dune_static_assert( (dimWorld <= 3),
"GmshReader requires dimWorld <= 3." );
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);
243 <<
": Expected '" << format <<
"', only read " << c <<
" entries instead of " << cnt <<
".");
247 void skipline(FILE * file)
251 c = std::fgetc(file);
252 }
while(c !=
'\n' && c != EOF);
258 factory(_factory), verbose(v), insert_boundary_segments(i) {}
260 std::vector<int> & boundaryIdMap()
262 return boundary_id_to_physical_entity;
265 std::vector<int> & elementIndexMap()
267 return element_index_to_physical_entity;
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");
285 number_of_real_vertices = 0;
286 boundary_element_count = 0;
290 double version_number;
291 int file_type, data_size;
293 readfile(file,1,
"%s\n",buf);
294 if (strcmp(buf,
"$MeshFormat")!=0)
296 readfile(file,3,
"%lg %d %d\n",&version_number,&file_type,&data_size);
297 if( (version_number < 2.0) || (version_number > 2.2) )
299 if (verbose) std::cout <<
"version " << version_number <<
" Gmsh file detected" << std::endl;
300 readfile(file,1,
"%s\n",buf);
301 if (strcmp(buf,
"$EndMeshFormat")!=0)
307 readfile(file,1,
"%s\n",buf);
308 if (strcmp(buf,
"$Nodes")!=0)
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 ] );
325 for(
int j = 0; j < dimWorld; ++j )
326 nodes[ i ][ j ] = x[ j ];
328 readfile(file,1,
"%s\n",buf);
329 if (strcmp(buf,
"$EndNodes")!=0)
334 readfile(file,1,
"%s\n",buf);
335 if (strcmp(buf,
"$Elements")!=0)
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++)
354 readfile(file,1,
"%d ",&blub);
363 pass1HandleElement(file, elm_type, renumber, nodes);
365 if (verbose) std::cout <<
"number of real vertices = " << number_of_real_vertices << std::endl;
366 if (verbose) std::cout <<
"number of boundary elements = " << boundary_element_count << std::endl;
367 if (verbose) std::cout <<
"number of elements = " << element_count << std::endl;
368 readfile(file,1,
"%s\n",buf);
369 if (strcmp(buf,
"$EndElements")!=0)
371 boundary_id_to_physical_entity.resize(boundary_element_count);
372 element_index_to_physical_entity.resize(element_count);
378 fseek(file, section_element_offset, SEEK_SET);
379 boundary_element_count = 0;
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++)
394 readfile(file,1,
"%d ",&blub);
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);
409 pass2HandleElement(file, elm_type, renumber, nodes, physical_entity);
411 readfile(file,1,
"%s\n",buf);
412 if (strcmp(buf,
"$EndElements")!=0)
419 void pass1HandleElement(FILE* file,
const int elm_type,
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())
455 renumber[elementDofs[i]] = number_of_real_vertices++;
460 if (elementDim[elm_type] == dim)
463 boundary_element_count++;
470 template <
class E,
class V,
class V2>
471 void boundarysegment_insert(
473 const E& elementDofs,
481 template <
class E,
class V>
482 void boundarysegment_insert(
484 const E& elementDofs,
489 for (
int i=0; i<6; i++)
490 for (
int j=0; j<dimWorld; j++)
491 v[i][j] = nodes[elementDofs[i]][j];
505 virtual void pass2HandleElement(FILE* file,
const int elm_type,
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) {
596 if (insert_boundary_segments) {
610 for (
int i=0; i<dimWorld; i++) {
611 v[0][i] = nodes[elementDofs[0]][i];
612 v[1][i] = nodes[elementDofs[2]][i];
613 v[2][i] = nodes[elementDofs[1]][i];
622 boundarysegment_insert(nodes, elementDofs, vertices);
632 if (elementDim[elm_type] == dim) {
633 element_index_to_physical_entity[element_count] = physical_entity;
636 boundary_id_to_physical_entity[boundary_element_count] = physical_entity;
637 boundary_element_count++;
660 template<
typename Gr
idType>
664 typedef GridType Grid;
667 static Grid*
read (
const std::string& fileName,
bool verbose =
true,
bool insert_boundary_segments=
true)
674 parser.read(fileName);
680 static Grid*
read (
const std::string& 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);
692 boundary_id_to_physical_entity.swap(parser.boundaryIdMap());
693 element_index_to_physical_entity.swap(parser.elementIndexMap());
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);
718 boundary_id_to_physical_entity.swap(parser.boundaryIdMap());
719 element_index_to_physical_entity.swap(parser.elementIndexMap());
Base class for grid boundary segments of arbitrary geometry.
FieldTraits< value_type >::real_type two_norm() const
two norm sqrt(sum over squared values of entries)
Definition: densevector.hh:521
derived_type & axpy(const value_type &a, const DenseVector< Other > &y)
vector space axpy operation ( *this += a y )
Definition: densevector.hh:456
vector space out of a tensor product of fields.
Definition: fvector.hh:92
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:25
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:31
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:30
@ pyramid
Four sided pyramid in three dimensions.
Definition: type.hh:32
@ prism
Prism element in three dimensions.
Definition: type.hh:33
dimension independent parts for GmshReaderParser
Definition: gmshreader.hh:205
Read Gmsh mesh file.
Definition: gmshreader.hh:662
static Grid * read(const std::string &fileName, bool verbose=true, bool insert_boundary_segments=true)
Definition: gmshreader.hh:667
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, std::vector< int > &boundary_id_to_physical_entity, std::vector< int > &element_index_to_physical_entity, bool verbose=true, bool insert_boundary_segments=true)
Definition: gmshreader.hh:708
static void read(Dune::GridFactory< Grid > &factory, const std::string &fileName, bool verbose=true, bool insert_boundary_segments=true)
Definition: gmshreader.hh:699
static Grid * read(const std::string &fileName, std::vector< int > &boundary_id_to_physical_entity, std::vector< int > &element_index_to_physical_entity, bool verbose=true, bool insert_boundary_segments=true)
Definition: gmshreader.hh:680
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:263
virtual void insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Insert an element into the coarse grid.
Definition: gridfactory.hh:290
virtual GridType * createGrid()
Finalize grid creation and hand over the grid.
Definition: gridfactory.hh:316
virtual void insertVertex(const FieldVector< ctype, dimworld > &pos)
Insert a vertex into the coarse grid.
Definition: gridfactory.hh:279
virtual void insertBoundarySegment(const std::vector< unsigned int > &vertices)
insert a boundary segment
Definition: gridfactory.hh:308
Default exception class for I/O errors.
Definition: exceptions.hh:257
Simple fixed size array class. This replaces std::array, if that is not available.
Definition: array.hh:40
A reference counting smart pointer.
Definition: shared_ptr.hh:64
Provide a generic factory class for unstructured grids.
A few common exception classes.
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Dune namespace.
Definition: alignment.hh:14
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:30
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
A unique label for each type of element that can occur in a grid.