28 #ifndef PARSER_GMSH_HPP__ 29 #define PARSER_GMSH_HPP__ 31 #include <lifev/core/LifeV.hpp> 32 #include <lifev/core/mesh/BareMesh.hpp> 59 2, 3, 4, 4, 8, 6, 5, 3, 6, 9, 10, 27, 18, 14,
60 1, 8, 20, 15, 13, 9, 10, 12, 15, 15, 21, 4, 5, 6,
61 20, 35, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 64, 125
91 template <
typename shape,
int id>
103 template <
typename geoshape>
107 # define GMSH_REGISTER_SHAPE(shape, id) 108 template <> struct id_of<shape> : register_shape<shape, id> {} 125 # undef GMSH_REGISTER_SHAPE 128 template <
typename GeoShape>
135 static const int dim = GeoShape::S_nDimensions;
157 template <
typename GeoShape>
158 bool ReadGmshFile (
const std::string& filename,
159 LifeV::
BareMesh<GeoShape>& baremesh,
160 LifeV::ID regionFlag = 0,
161 bool verbose =
false)
166 std::ifstream ifile (filename.c_str(), std::ios::in | std::ios::binary);
170 std::cerr <<
"[ERROR:GmshIO] File '" 171 << filename <<
"' not found." << std::endl;
181 bool is_binary =
false, is_legacy =
false;
182 bool is_differ_endian =
false;
184 bool status_ok =
false;
186 std::getline (ifile, line);
187 if (line ==
"$MeshFormat")
190 std::getline (ifile, line);
191 std::stringstream iss (line);
195 iss >> version >> is_binary >> doublesize;
196 status_ok = iss.eof();
207 ifile.read (one.c,
sizeof (gmsh_int_t) );
209 is_differ_endian = (one.i != 1);
211 if (is_differ_endian)
213 std::cerr <<
"[ERROR:GmshIO] Different endianness of system" 214 <<
" and file not supported" << std::endl;
220 std::clog <<
"[INFO:GmshIO] Found gmsh header with " 221 << (is_binary ?
"binary" :
"ASCII")
222 <<
" data." << std::endl;
224 else if (line ==
"$NOD")
231 std::clog <<
"[INFO:GmshIO] Found LEGACY gmsh header" 238 std::cerr <<
"[ERROR:GmshIO] Error during parsing header" 249 ifile.seekg (0, std::ios::beg);
252 std::string node_tag = (is_legacy) ?
"$NOD" :
"$Nodes";
256 while (std::getline (ifile, line) && line != node_tag) {};
259 std::cerr <<
"[ERROR:GmshIO] Nodes section not found" << std::endl;
264 std::getline (ifile, line);
265 std::size_t num_nodes = atoi (line.c_str() );
268 std::cerr <<
"[ERROR:GmshIO] Number of nodes not correct" << std::endl;
273 baremesh.points.reshape (3, num_nodes);
274 baremesh.pointMarkers.resize (num_nodes);
276 std::stringstream iss;
279 for (std::size_t i = 0; i < num_nodes; ++i)
287 ifile.read (
reinterpret_cast<
char*> (&id),
sizeof (gmsh_int_t) );
288 ifile.read (
reinterpret_cast<
char*> (p), 3 *
sizeof (gmsh_float_t) );
293 std::getline (ifile, line);
295 iss.str (line.c_str() );
297 iss >> id >> p[0] >> p[1] >> p[2];
301 baremesh.points (0, id - 1) = p[0];
302 baremesh.points (1, id - 1) = p[1];
303 baremesh.points (2, id - 1) = p[2];
312 std::getline (ifile, line);
313 if (line !=
"$EndNodes" && line !=
"$ENDNOD")
315 std::cerr <<
"[ERROR:GmshIO] Something wrong with Nodes section" 321 std::clog <<
"[INFO:GmshIO] Found " << num_nodes <<
" nodes." 331 std::string elm_tag = (is_legacy) ?
"$ELM" :
"$Elements";
335 while (std::getline (ifile, line) && line != elm_tag) {};
338 std::cerr <<
"[ERROR:GmshIO] Elements section not found" << std::endl;
342 std::getline (ifile, line);
343 std::size_t num_elms = atoi (line.c_str() );
350 const short s_ids[4] =
359 std::deque<gmsh_elm_t> elems[4];
363 bool status_ok =
false;
372 ifile.read (
reinterpret_cast<
char*> (header), 3 *
sizeof (gmsh_int_t) );
374 short s_type =
static_cast<
short> (std::find (s_ids, s_ids + 4, header[0]) - s_ids);
379 std::cerr <<
"[ERROR:GmshIO] Found elements " 380 <<
" of type " << header[0]
381 <<
" , which is not an allowed shape." << std::endl;
386 gmsh_int_t nnodes = elm_nodes_num[header[0] - 1];
388 gmsh_int_t total_size = header[1] * (1 + header[2] + nnodes);
389 std::vector<gmsh_int_t> e (total_size);
391 ifile.read (
reinterpret_cast<
char*> (&e[0]), total_size *
sizeof (gmsh_int_t) );
393 elm_count += header[1];
398 tmp
.type = header[0];
400 tmp.tags.reserve (header[2]);
401 std::copy (e.begin(), e.begin() + header[2] + 1, std::back_inserter (tmp.tags) );
403 tmp.nodes.reserve (nnodes);
404 std::copy (e.begin() + header[2] + 1, e.end(), std::back_inserter (tmp.nodes) );
406 elems[s_type].push_back (tmp);
410 while (elm_count <
static_cast<
gmsh_int_t> (num_elms) );
417 typedef std::istream_iterator<gmsh_int_t> iss_it;
418 std::stringstream iss;
419 for (std::size_t i = 0; i < num_elms; ++i)
424 std::getline (ifile, line);
427 iss >> id >> type >> num_tags;
434 short s_type =
static_cast<
short> (std::find (s_ids, s_ids + 4, type) - s_ids);
439 std::cerr <<
"[ERROR:GmshIO] Element " << id
440 <<
", of type " << type
441 <<
", has a not allowed shape." << std::endl;
450 tmp.tags.resize (num_tags);
451 for (
int k = 0; k < num_tags; ++k)
456 int nnodes = elm_nodes_num[type - 1];
457 tmp.nodes.reserve (nnodes);
458 std::copy (iss_it (iss), iss_it(), std::back_inserter (tmp.nodes) );
460 elems[s_type].push_back (tmp);
461 status_ok = iss.eof();
465 std::getline (ifile, line);
466 if (!status_ok || (line !=
"$EndElements" && line !=
"$ENDELM") )
468 std::cerr <<
"[ERROR:GmshIO] Something wrong with Elements section." << std::endl;
474 std::clog <<
"[INFO:GmshIO] Highest dimension should be " 475 << baremesh.nDimensions <<
"." << std::endl;
476 if (!elems[0].size() )
478 std::cerr <<
"[ERROR:GmshIO] No " 479 << baremesh.nDimensions <<
"d elements found!" << std::endl;
484 LifeV::ArraySimple<LifeV::UInt>* bare_elm_ptr[3] =
490 std::vector<LifeV::ID>* bare_mrk_ptr[4] =
492 &baremesh.elementMarkers,
493 &baremesh.facetMarkers,
494 &baremesh.ridgeMarkers,
495 &baremesh.pointMarkers
498 for (LifeV::UInt s = 0; s < baremesh.nDimensions + 1; ++s)
501 LifeV::UInt num_s_elems = elems[s].size();
502 LifeV::UInt num_s_nodes = elm_nodes_num[s_ids[s] - 1];
506 bare_elm_ptr[s]->reshape (num_s_nodes, num_s_elems);
508 bare_mrk_ptr[s]->resize (num_s_elems);
511 std::clog <<
"[INFO:GmshIO] Found " << num_s_elems
512 <<
" marked " << (baremesh.nDimensions - s)
513 <<
"d elements." << std::endl;
516 for (LifeV::UInt i = 0; i < num_s_elems; ++i)
519 bare_mrk_ptr[s]->at (i) = elems[s][i].tags[1];
523 for (LifeV::UInt k = 0; k < num_s_nodes; ++k)
525 (*bare_elm_ptr[s]) (k, i) = elems[s][i].nodes[k] - 1;
534 baremesh.numBoundaryPoints = 0;
535 baremesh.numBoundaryFacets = baremesh.facets.numberOfColumns();
542 baremesh.regionMarkerID = regionFlag;
#define GMSH_REGISTER_SHAPE(shape, id)
std::vector< gmsh_int_t > nodes
elem_type::GeoBShape facet_type
friend detail::shape_type< shape > registered(detail::shape_id< id >)
facet_type::GeoBShape ridge_type
static const LifeV::UInt elm_nodes_num[]
ridge_type::GeoBShape peak_type
std::vector< gmsh_int_t > tags
A struct for a bare mesh.