39 std::ifstream input(fname.c_str());
43 ERR(
"isGMSHMeshFile(): Could not open file {:s}.", fname);
47 std::string header_first_line;
48 input >> header_first_line;
49 if (header_first_line.find(
"$MeshFormat") != std::string::npos)
53 std::getline(input, version);
54 std::getline(input, version);
55 INFO(
"isGMSHMeshFile(): Found GMSH mesh file version: {:s}.", version);
64 std::vector<unsigned>& node_ids,
65 std::map<unsigned, unsigned>
const& id_map)
68 for (
unsigned i = 0; i < n_nodes; i++)
71 node_ids.push_back(id_map.at(idx));
75template <
typename ElementType>
77 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
78 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
80 std::vector<unsigned> node_ids;
81 readNodeIDs(in, ElementType::n_all_nodes, node_ids, id_map);
83 std::array<MeshLib::Node*, ElementType::n_all_nodes> element_nodes;
85 std::transform(begin(node_ids), end(node_ids), begin(element_nodes),
86 [&nodes](
auto const id) {
return nodes[id]; });
88 return std::make_pair(
new ElementType(element_nodes), mat_id);
93 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
94 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
96 std::vector<unsigned> node_ids;
99 std::array<MeshLib::Node*, 3> element_nodes;
101 std::transform(std::rbegin(node_ids), std::rend(node_ids),
102 begin(element_nodes),
103 [&nodes](
auto const id) {
return nodes[id]; });
105 return std::make_pair(
new MeshLib::Tri(element_nodes), mat_id);
110 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
111 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
113 std::vector<unsigned> node_ids;
116 std::swap(node_ids[8], node_ids[9]);
118 std::array<MeshLib::Node*, MeshLib::Tet10::n_all_nodes> element_nodes;
120 std::transform(begin(node_ids), end(node_ids), begin(element_nodes),
121 [&nodes](
auto const id) {
return nodes[id]; });
128 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
129 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
131 std::vector<unsigned> node_ids;
134 std::array<MeshLib::Node*, MeshLib::Hex20::n_all_nodes> element_nodes;
136 constexpr std::array node_order = {0, 1, 2, 3, 4, 5, 6, 7, 8, 11,
137 13, 9, 16, 18, 19, 17, 10, 12, 14, 15};
139 std::transform(begin(node_order), end(node_order), begin(element_nodes),
140 [&node_ids, &nodes](
auto const id)
141 {
return nodes[node_ids[id]]; });
148 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
149 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
151 std::vector<unsigned> node_ids;
154 std::array<MeshLib::Node*, MeshLib::Prism15::n_all_nodes> element_nodes;
156 constexpr std::array node_order = {0, 1, 2, 3, 4, 5, 6, 9,
157 7, 12, 14, 13, 8, 10, 11};
159 std::transform(begin(node_order), end(node_order), begin(element_nodes),
160 [&node_ids, &nodes](
auto const id)
161 {
return nodes[node_ids[id]]; });
168 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
169 int const mat_id, std::map<unsigned, unsigned>
const& id_map)
171 std::vector<unsigned> node_ids;
173 std::array<MeshLib::Node*, MeshLib::Pyramid13::n_all_nodes> element_nodes;
175 constexpr std::array node_order = {0, 1, 2, 3, 4, 5, 8,
176 10, 6, 7, 9, 11, 12};
178 std::transform(begin(node_order), end(node_order), begin(element_nodes),
179 [&node_ids, &nodes](
auto const id)
180 {
return nodes[node_ids[id]]; });
186 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
187 std::map<unsigned, unsigned>
const& id_map)
197 in >> idx >> type >> n_tags >> dummy >> mat_id;
203 return createElement<MeshLib::Line>(in, nodes, mat_id, id_map);
211 return createElement<MeshLib::Quad>(in, nodes, mat_id, id_map);
215 return createElement<MeshLib::Tet>(in, nodes, mat_id, id_map);
219 return createElement<MeshLib::Hex>(in, nodes, mat_id, id_map);
223 return createElement<MeshLib::Prism>(in, nodes, mat_id, id_map);
227 return createElement<MeshLib::Pyramid>(in, nodes, mat_id, id_map);
231 return createElement<MeshLib::Line3>(in, nodes, mat_id, id_map);
235 return createElement<MeshLib::Tri6>(in, nodes, mat_id, id_map);
239 return createElement<MeshLib::Quad9>(in, nodes, mat_id, id_map);
247 return createElement<MeshLib::Quad8>(in, nodes, mat_id, id_map);
265 WARN(
"readGMSHMesh(): Unknown element type {:d}.", type);
268 return std::make_pair(
nullptr, -1);
274 std::ifstream in(fname.c_str(), std::ios::in);
277 WARN(
"readGMSHMesh() - Could not open file {:s}.", fname);
281 std::getline(in, line);
282 if (line.find(
"$MeshFormat") == std::string::npos)
285 WARN(
"No GMSH file format recognized.");
289 std::getline(in, line);
290 if (line.substr(0, 3) !=
"2.2")
292 WARN(
"Wrong gmsh file format version '{:s}'.", line.substr(0, 3));
298 WARN(
"Currently reading gmsh binary file type is not supported.");
301 std::getline(in, line);
303 std::vector<MeshLib::Node*> nodes;
304 std::vector<MeshLib::Element*> elements;
305 std::vector<int> materials;
306 std::map<unsigned, unsigned> id_map;
307 while (line.find(
"$EndElements") == std::string::npos)
310 std::getline(in, line);
311 if (line.find(
"$Nodes") != std::string::npos)
313 std::size_t n_nodes(0);
318 in >> n_nodes >> std::ws;
319 nodes.resize(n_nodes);
320 for (std::size_t i = 0; i < n_nodes; i++)
322 in >>
id >> x >> y >> z >> std::ws;
323 id_map.insert(std::map<unsigned, unsigned>::value_type(
id, i));
326 std::getline(in, line);
330 if (line.find(
"$Elements") != std::string::npos)
332 std::size_t n_elements(0);
333 if (!(in >> n_elements >> std::ws))
335 ERR(
"Read GMSH mesh does not contain any elements");
337 elements.reserve(n_elements);
338 materials.reserve(n_elements);
339 for (std::size_t i = 0; i < n_elements; i++)
343 std::tie(elem, mat_id) =
readElement(in, nodes, id_map);
347 elements.push_back(elem);
348 materials.push_back(mat_id);
351 std::getline(in, line);
354 if (line.find(
"PhysicalNames") != std::string::npos)
356 std::size_t n_lines(0);
357 in >> n_lines >> std::ws;
358 for (std::size_t i = 0; i < n_lines; i++)
360 std::getline(in, line);
362 std::getline(in, line);
366 if (elements.empty())
368 for (
auto& node : nodes)
379 auto*
const material_ids =
384 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
388 material_ids->insert(material_ids->end(), materials.cbegin(),
394 INFO(
"\t... finished.");
395 INFO(
"Nr. Nodes: {:d}.", nodes.size());
396 INFO(
"Nr. Elements: {:d}.", elements.size());
Definition of the ElementValueModification class.
Definition of the Element class.
Definition of the Hex class.
Definition of the Line class.
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)
Definition of the Mesh class.
Definition of the Node class.
Definition of the Prism class.
Definition of the Pyramid class.
Definition of the Quad class.
Definition of the Tet class.
Definition of the Tri class.
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
Constant: The number of all nodes for this element.
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)
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 > 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)
MeshLib::Mesh * readGMSHMesh(std::string const &fname)
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 > readElement(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, 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)