32 std::ifstream input(fname.c_str());
36 ERR(
"isGMSHMeshFile(): Could not open file {:s}.", fname);
40 std::string header_first_line;
41 input >> header_first_line;
42 if (header_first_line.find(
"$MeshFormat") != std::string::npos)
46 std::getline(input, version);
47 std::getline(input, version);
48 INFO(
"isGMSHMeshFile(): Found GMSH mesh file version: {:s}.", version);
57 std::vector<unsigned>& node_ids,
58 std::map<unsigned, unsigned>
const& id_map)
61 for (
unsigned i = 0; i < n_nodes; i++)
64 node_ids.push_back(id_map.at(idx));
68template <
typename ElementType>
70 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
71 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
73 std::vector<unsigned> node_ids;
74 readNodeIDs(in, ElementType::n_all_nodes, node_ids, id_map);
76 std::array<MeshLib::Node*, ElementType::n_all_nodes> element_nodes;
78 std::transform(begin(node_ids), end(node_ids), begin(element_nodes),
79 [&nodes](
auto const id) {
return nodes[id]; });
81 return std::make_pair(
new ElementType(element_nodes), mat_id);
86 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
87 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
89 std::vector<unsigned> node_ids;
92 std::array<MeshLib::Node*, 3> element_nodes;
94 std::transform(std::rbegin(node_ids), std::rend(node_ids),
96 [&nodes](
auto const id) {
return nodes[id]; });
98 return std::make_pair(
new MeshLib::Tri(element_nodes), mat_id);
103 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
104 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
106 std::vector<unsigned> node_ids;
109 std::swap(node_ids[8], node_ids[9]);
111 std::array<MeshLib::Node*, MeshLib::Tet10::n_all_nodes> element_nodes;
113 std::transform(begin(node_ids), end(node_ids), begin(element_nodes),
114 [&nodes](
auto const id) {
return nodes[id]; });
121 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
122 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
124 std::vector<unsigned> node_ids;
127 std::array<MeshLib::Node*, MeshLib::Hex20::n_all_nodes> element_nodes;
129 constexpr std::array node_order = {0, 1, 2, 3, 4, 5, 6, 7, 8, 11,
130 13, 9, 16, 18, 19, 17, 10, 12, 14, 15};
132 std::transform(begin(node_order), end(node_order), begin(element_nodes),
133 [&node_ids, &nodes](
auto const id)
134 {
return nodes[node_ids[id]]; });
141 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
142 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
144 std::vector<unsigned> node_ids;
147 std::array<MeshLib::Node*, MeshLib::Prism15::n_all_nodes> element_nodes;
149 constexpr std::array node_order = {0, 1, 2, 3, 4, 5, 6, 9,
150 7, 12, 14, 13, 8, 10, 11};
152 std::transform(begin(node_order), end(node_order), begin(element_nodes),
153 [&node_ids, &nodes](
auto const id)
154 {
return nodes[node_ids[id]]; });
161 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
162 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
164 std::vector<unsigned> node_ids;
166 std::array<MeshLib::Node*, MeshLib::Pyramid13::n_all_nodes> element_nodes;
168 constexpr std::array node_order = {0, 1, 2, 3, 4, 5, 8,
169 10, 6, 7, 9, 11, 12};
171 std::transform(begin(node_order), end(node_order), begin(element_nodes),
172 [&node_ids, &nodes](
auto const id)
173 {
return nodes[node_ids[id]]; });
179 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
180 std::map<unsigned, unsigned>
const& id_map,
181 bool const is_created_with_gmsh2)
196 in >> idx >> type >> n_tags;
198 if (!is_created_with_gmsh2)
200 in >> mat_id >> dummy;
204 in >> dummy >> mat_id;
273 WARN(
"readGMSHMesh(): Unknown element type {:d}.", type);
276 return std::make_pair(
nullptr, -1);
280 bool const is_created_with_gmsh2)
282 if (!is_created_with_gmsh2)
285 "If the mesh is generated with Gmsh version 2 and it is saved"
286 " (or exported) as \"Version 2 ASCII\" format, the flag"
287 " --gmsh2_physical_id must be used for a correct conversion from "
288 "physical id to MaterialIDs.");
292 std::ifstream in(fname.c_str(), std::ios::in);
295 WARN(
"readGMSHMesh() - Could not open file {:s}.", fname);
299 std::getline(in, line);
300 if (line.find(
"$MeshFormat") == std::string::npos)
303 WARN(
"No GMSH file format recognized.");
307 std::getline(in, line);
308 if (line.substr(0, 3) !=
"2.2")
310 WARN(
"Wrong gmsh file format version '{:s}'.", line.substr(0, 3));
316 WARN(
"Currently reading gmsh binary file type is not supported.");
319 std::getline(in, line);
321 std::vector<MeshLib::Node*> nodes;
322 std::vector<MeshLib::Element*> elements;
323 std::vector<int> materials;
324 std::map<unsigned, unsigned> id_map;
325 while (line.find(
"$EndElements") == std::string::npos)
328 std::getline(in, line);
329 if (line.find(
"$Nodes") != std::string::npos)
331 std::size_t n_nodes(0);
336 in >> n_nodes >> std::ws;
337 nodes.resize(n_nodes);
338 for (std::size_t i = 0; i < n_nodes; i++)
340 in >>
id >> x >> y >> z >> std::ws;
341 id_map.insert(std::map<unsigned, unsigned>::value_type(
id, i));
344 std::getline(in, line);
348 if (line.find(
"$Elements") != std::string::npos)
350 std::size_t n_elements(0);
351 if (!(in >> n_elements >> std::ws))
353 ERR(
"Read GMSH mesh does not contain any elements");
355 elements.reserve(n_elements);
356 materials.reserve(n_elements);
357 for (std::size_t i = 0; i < n_elements; i++)
361 std::tie(elem, mat_id) =
362 readElement(in, nodes, id_map, is_created_with_gmsh2);
366 elements.push_back(elem);
367 materials.push_back(mat_id);
370 std::getline(in, line);
373 if (line.find(
"PhysicalNames") != std::string::npos)
375 std::size_t n_lines(0);
376 in >> n_lines >> std::ws;
377 for (std::size_t i = 0; i < n_lines; i++)
379 std::getline(in, line);
381 std::getline(in, line);
385 if (elements.empty())
387 for (
auto& node : nodes)
398 auto*
const material_ids =
403 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
407 material_ids->assign(materials);
412 INFO(
"\t... finished.");
413 INFO(
"Nr. Nodes: {:d}.", nodes.size());
414 INFO(
"Nr. Elements: {:d}.", elements.size());
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Properties & getProperties()
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
static const unsigned n_all_nodes
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::pair< MeshLib::Element *, int > createElement< MeshLib::Tri >(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, int const mat_id, std::map< unsigned, unsigned > const &id_map)
MeshLib::Mesh * readGMSHMesh(std::string const &fname, bool const is_created_with_gmsh2)
std::pair< MeshLib::Element *, int > createElement< MeshLib::Prism15 >(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, int const mat_id, std::map< unsigned, unsigned > const &id_map)
std::pair< MeshLib::Element *, int > readElement(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, std::map< unsigned, unsigned > const &id_map, bool const is_created_with_gmsh2)
std::pair< MeshLib::Element *, int > createElement< MeshLib::Pyramid13 >(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, int const mat_id, std::map< unsigned, unsigned > const &id_map)
bool isGMSHMeshFile(const std::string &fname)
std::pair< MeshLib::Element *, int > createElement< MeshLib::Tet10 >(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, int const mat_id, std::map< unsigned, unsigned > const &id_map)
std::pair< MeshLib::Element *, int > createElement< MeshLib::Hex20 >(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, int const mat_id, std::map< unsigned, unsigned > const &id_map)
std::pair< MeshLib::Element *, int > createElement(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, int const mat_id, std::map< unsigned, unsigned > const &id_map)
void readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector< unsigned > &node_ids, std::map< unsigned, unsigned > const &id_map)
TemplateElement< MeshLib::TetRule10 > Tet10
TemplateElement< MeshLib::HexRule20 > Hex20
TemplateElement< MeshLib::PyramidRule13 > Pyramid13
TemplateElement< MeshLib::TriRule3 > Tri
TemplateElement< MeshLib::PrismRule15 > Prism15