36 if (!(in >> index >> material_id))
38 return std::numeric_limits<std::size_t>::max();
44 const std::vector<MeshLib::Node*>& nodes)
46 std::string elem_type_str;
51 if (!(in >> elem_type_str))
58 auto* idx =
new unsigned[8];
65 for (
int i = 0; i < 2; ++i)
74 for (
unsigned k(0); k < 2; ++k)
76 edge_nodes[k] = nodes[idx[k]];
83 for (
int i = 0; i < 3; ++i)
91 for (
unsigned k(0); k < 3; ++k)
93 tri_nodes[k] = nodes[idx[k]];
100 for (
int i = 0; i < 4; ++i)
108 for (
unsigned k(0); k < 4; ++k)
110 quad_nodes[k] = nodes[idx[k]];
117 for (
int i = 0; i < 4; ++i)
125 for (
unsigned k(0); k < 4; ++k)
127 tet_nodes[k] = nodes[idx[k]];
134 for (
int i = 0; i < 8; ++i)
142 for (
unsigned k(0); k < 8; ++k)
144 hex_nodes[k] = nodes[idx[k]];
151 for (
int i = 0; i < 5; ++i)
159 for (
unsigned k(0); k < 5; ++k)
161 pyramid_nodes[k] = nodes[idx[k]];
168 for (
int i = 0; i < 6; ++i)
176 for (
unsigned k(0); k < 6; ++k)
178 prism_nodes[k] = nodes[idx[k]];
230 const std::size_t ele_vector_size(ele_vec.size());
232 out << ele_vector_size <<
"\n";
233 for (std::size_t i(0); i < ele_vector_size; ++i)
235 auto const& element = *ele_vec[i];
236 if (element.getNumberOfBaseNodes() != element.getNumberOfNodes())
239 "Found high order element in the mesh that is not required by "
240 "OGS 5 input. High order elements are generated in OGS 5 on "
251 out << (*material_ids)[i] <<
" ";
254 unsigned nElemNodes(element.getNumberOfBaseNodes());
255 for (std::size_t j = 0; j < nElemNodes; ++j)
257 out << element.getNode(j)->getID() <<
" ";
275 INFO(
"Reading OGS legacy mesh ... ");
277 std::ifstream in(file_name.c_str(), std::ios::in);
280 WARN(
"MeshIO::loadMeshFromFile() - Could not open file {:s}.",
285 std::string line_string;
286 std::getline(in, line_string);
288 if (line_string.find(
"#FEM_MSH") != std::string::npos)
290 std::vector<MeshLib::Node*> nodes;
291 std::vector<MeshLib::Element*> elements;
292 std::vector<std::size_t> materials;
296 std::getline(in, line_string);
299 if (line_string.find(
"#STOP") != std::string::npos)
303 if (line_string.find(
"$NODES") != std::string::npos)
305 std::getline(in, line_string);
307 unsigned nNodes = atoi(line_string.c_str());
308 for (
unsigned i = 0; i < nNodes; ++i)
310 std::getline(in, line_string);
311 std::stringstream iss(line_string);
316 iss >> idx >> x >> y >> z;
318 nodes.push_back(node);
321 if (s.find(
"$AREA") != std::string::npos)
328 else if (line_string.find(
"$ELEMENTS") != std::string::npos)
330 std::getline(in, line_string);
332 unsigned nElements = atoi(line_string.c_str());
333 for (
unsigned i = 0; i < nElements; ++i)
335 std::getline(in, line_string);
336 std::stringstream ss(line_string);
337 materials.push_back(readMaterialID(ss));
341 ERR(
"Reading mesh element {:d} from file '{:s}' "
345 std::for_each(elements.begin(), elements.end(),
346 std::default_delete<MeshLib::Element>());
348 std::for_each(nodes.begin(), nodes.end(),
349 std::default_delete<MeshLib::Node>());
352 elements.push_back(elem);
357 if (elements.empty())
359 ERR(
"MeshIO::loadMeshFromFile() - File did not contain element "
361 for (
auto& node : nodes)
372 auto*
const material_ids =
377 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
381 material_ids->insert(material_ids->end(), materials.cbegin(),
384 INFO(
"\t... finished.");
385 INFO(
"Nr. Nodes: {:d}.", nodes.size());
386 INFO(
"Nr. Elements: {:d}.", elements.size());
400 WARN(
"MeshIO::write(): Cannot write: no mesh object specified.");
410 out << n_nodes <<
"\n";
411 for (std::size_t i(0); i < n_nodes; ++i)
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 MeshIO class.
Definition of the Node class.
std::ostringstream out
The stream to write to.
bool write() override
Write mesh to stream.
MeshLib::Mesh * loadMeshFromFile(const std::string &file_name)
Read mesh from file.
const MeshLib::Mesh * _mesh
void setMesh(const MeshLib::Mesh *mesh)
Set mesh for writing.
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Properties & getProperties()
std::size_t getNumberOfNodes() const
Get the number of nodes.
bool existsPropertyVector(std::string_view name) const
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
PropertyVector< T > const * getPropertyVector(std::string_view name) const
void trim(std::string &str, char ch)
std::string extractBaseNameWithoutExtension(std::string const &pathname)
MeshElemType String2MeshElemType(const std::string &s)
Given a string of the shortened name of the element type, this returns the corresponding MeshElemType...
TemplateElement< MeshLib::TetRule4 > Tet
TemplateElement< MeshLib::LineRule2 > Line
TemplateElement< MeshLib::QuadRule4 > Quad
TemplateElement< MeshLib::TriRule3 > Tri
TemplateElement< MeshLib::PyramidRule5 > Pyramid
TemplateElement< MeshLib::PrismRule6 > Prism
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
TemplateElement< MeshLib::HexRule8 > Hex
MeshLib::Element * readElement(std::istream &in, const std::vector< MeshLib::Node * > &nodes)
std::string ElemType2StringOutput(const MeshLib::MeshElemType t)
void writeElements(std::vector< MeshLib::Element * > const &ele_vec, MeshLib::PropertyVector< int > const *const material_ids, std::ostream &out)
std::size_t readMaterialID(std::istream &in)