35 std::ifstream input(fname.c_str());
39 ERR(
"isGMSHMeshFile(): Could not open file {:s}.", fname);
43 std::string header_first_line;
44 input >> header_first_line;
45 if (header_first_line.find(
"$MeshFormat") != std::string::npos)
49 getline(input, version);
50 getline(input, version);
51 INFO(
"isGMSHMeshFile(): Found GMSH mesh file version: {:s}.", version);
60 std::vector<unsigned>& node_ids,
61 std::map<unsigned, unsigned>
const& id_map)
64 for (
unsigned i = 0; i < n_nodes; i++)
67 node_ids.push_back(id_map.at(idx));
72 std::ifstream& in, std::vector<MeshLib::Node*>
const& nodes,
73 std::map<unsigned, unsigned>
const& id_map)
80 std::vector<unsigned> node_ids;
84 in >> idx >> type >> n_tags >> dummy >> mat_id;
93 edge_nodes[0] = nodes[node_ids[0]];
94 edge_nodes[1] = nodes[node_ids[1]];
101 tri_nodes[0] = nodes[node_ids[2]];
102 tri_nodes[1] = nodes[node_ids[1]];
103 tri_nodes[2] = nodes[node_ids[0]];
104 return std::make_pair(
new MeshLib::Tri(tri_nodes), mat_id);
110 for (
unsigned k(0); k < 4; k++)
112 quad_nodes[k] = nodes[node_ids[k]];
114 return std::make_pair(
new MeshLib::Quad(quad_nodes), mat_id);
120 for (
unsigned k(0); k < 4; k++)
122 tet_nodes[k] = nodes[node_ids[k]];
124 return std::make_pair(
new MeshLib::Tet(tet_nodes), mat_id);
130 for (
unsigned k(0); k < 8; k++)
132 hex_nodes[k] = nodes[node_ids[k]];
134 return std::make_pair(
new MeshLib::Hex(hex_nodes), mat_id);
140 for (
unsigned k(0); k < 6; k++)
142 prism_nodes[k] = nodes[node_ids[k]];
150 for (
unsigned k(0); k < 5; k++)
152 pyramid_nodes[k] = nodes[node_ids[k]];
160 WARN(
"readGMSHMesh(): Unknown element type {:d}.", type);
163 return std::make_pair(
nullptr, -1);
169 std::ifstream in(fname.c_str(), std::ios::in);
172 WARN(
"readGMSHMesh() - Could not open file {:s}.", fname);
177 if (line.find(
"$MeshFormat") == std::string::npos)
180 WARN(
"No GMSH file format recognized.");
185 if (line.substr(0, 3) !=
"2.2")
187 WARN(
"Wrong gmsh file format version '{:s}'.", line.substr(0, 3));
193 WARN(
"Currently reading gmsh binary file type is not supported.");
198 std::vector<MeshLib::Node*> nodes;
199 std::vector<MeshLib::Element*> elements;
200 std::vector<int> materials;
201 std::map<unsigned, unsigned> id_map;
202 while (line.find(
"$EndElements") == std::string::npos)
206 if (line.find(
"$Nodes") != std::string::npos)
208 std::size_t n_nodes(0);
213 in >> n_nodes >> std::ws;
214 nodes.resize(n_nodes);
215 for (std::size_t i = 0; i < n_nodes; i++)
217 in >>
id >> x >> y >> z >> std::ws;
218 id_map.insert(std::map<unsigned, unsigned>::value_type(
id, i));
225 if (line.find(
"$Elements") != std::string::npos)
227 std::size_t n_elements(0);
228 if (!(in >> n_elements >> std::ws))
230 ERR(
"Read GMSH mesh does not contain any elements");
232 elements.reserve(n_elements);
233 materials.reserve(n_elements);
234 for (std::size_t i = 0; i < n_elements; i++)
238 std::tie(elem, mat_id) =
readElement(in, nodes, id_map);
242 elements.push_back(elem);
243 materials.push_back(mat_id);
249 if (line.find(
"PhysicalNames") != std::string::npos)
251 std::size_t n_lines(0);
252 in >> n_lines >> std::ws;
253 for (std::size_t i = 0; i < n_lines; i++)
261 if (elements.empty())
263 for (
auto& node : nodes)
273 auto*
const material_ids =
278 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
282 material_ids->insert(material_ids->end(), materials.cbegin(),
288 INFO(
"\t... finished.");
289 INFO(
"Nr. Nodes: {:d}.", nodes.size());
290 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(char const *fmt, Args const &... args)
void ERR(char const *fmt, Args const &... args)
void WARN(char const *fmt, Args const &... 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.
static std::size_t condense(MeshLib::Mesh &mesh)
Properties & getProperties()
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::pair< MeshLib::Element *, int > readElement(std::ifstream &in, std::vector< MeshLib::Node * > const &nodes, std::map< unsigned, unsigned > const &id_map)
bool isGMSHMeshFile(const std::string &fname)
MeshLib::Mesh * readGMSHMesh(std::string const &fname)
void readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector< unsigned > &node_ids, std::map< unsigned, unsigned > const &id_map)