37 if (!(in >> index >> material_id) ||
38 material_id >
static_cast<unsigned>(std::numeric_limits<int>::max()))
42 return std::numeric_limits<int>::max();
45 return static_cast<int>(material_id);
49 const std::vector<MeshLib::Node*>& nodes)
51 std::string elem_type_str;
56 if (!(in >> elem_type_str))
63 auto* idx =
new unsigned[8];
70 for (
int i = 0; i < 2; ++i)
79 for (
unsigned k(0); k < 2; ++k)
81 edge_nodes[k] = nodes[idx[k]];
88 for (
int i = 0; i < 3; ++i)
96 for (
unsigned k(0); k < 3; ++k)
98 tri_nodes[k] = nodes[idx[k]];
105 for (
int i = 0; i < 4; ++i)
113 for (
unsigned k(0); k < 4; ++k)
115 quad_nodes[k] = nodes[idx[k]];
122 for (
int i = 0; i < 4; ++i)
130 for (
unsigned k(0); k < 4; ++k)
132 tet_nodes[k] = nodes[idx[k]];
139 for (
int i = 0; i < 8; ++i)
147 for (
unsigned k(0); k < 8; ++k)
149 hex_nodes[k] = nodes[idx[k]];
156 for (
int i = 0; i < 5; ++i)
164 for (
unsigned k(0); k < 5; ++k)
166 pyramid_nodes[k] = nodes[idx[k]];
173 for (
int i = 0; i < 6; ++i)
181 for (
unsigned k(0); k < 6; ++k)
183 prism_nodes[k] = nodes[idx[k]];
235 const std::size_t ele_vector_size(ele_vec.size());
237 out << ele_vector_size <<
"\n";
238 for (std::size_t i(0); i < ele_vector_size; ++i)
240 auto const& element = *ele_vec[i];
241 if (element.getNumberOfBaseNodes() != element.getNumberOfNodes())
244 "Found high order element in the mesh that is not required by "
245 "OGS 5 input. High order elements are generated in OGS 5 on "
256 out << (*material_ids)[i] <<
" ";
259 unsigned nElemNodes(element.getNumberOfBaseNodes());
260 for (std::size_t j = 0; j < nElemNodes; ++j)
262 out << element.getNode(j)->getID() <<
" ";
280 INFO(
"Reading OGS legacy mesh ... ");
282 std::ifstream in(file_name.c_str(), std::ios::in);
285 WARN(
"MeshIO::loadMeshFromFile() - Could not open file {:s}.",
290 std::string line_string;
291 std::getline(in, line_string);
293 if (line_string.find(
"#FEM_MSH") != std::string::npos)
295 std::vector<MeshLib::Node*> nodes;
296 std::vector<MeshLib::Element*> elements;
297 std::vector<int> materials;
301 std::getline(in, line_string);
304 if (line_string.find(
"#STOP") != std::string::npos)
308 if (line_string.find(
"$NODES") != std::string::npos)
310 std::getline(in, line_string);
312 unsigned nNodes = atoi(line_string.c_str());
313 for (
unsigned i = 0; i < nNodes; ++i)
315 std::getline(in, line_string);
316 std::stringstream iss(line_string);
321 iss >> idx >> x >> y >> z;
323 nodes.push_back(node);
326 if (s.find(
"$AREA") != std::string::npos)
333 else if (line_string.find(
"$ELEMENTS") != std::string::npos)
335 std::getline(in, line_string);
337 unsigned nElements = atoi(line_string.c_str());
338 for (
unsigned i = 0; i < nElements; ++i)
340 std::getline(in, line_string);
341 std::stringstream ss(line_string);
342 materials.push_back(readMaterialID(ss));
346 ERR(
"Reading mesh element {:d} from file '{:s}' "
350 std::for_each(elements.begin(), elements.end(),
351 std::default_delete<MeshLib::Element>());
353 std::for_each(nodes.begin(), nodes.end(),
354 std::default_delete<MeshLib::Node>());
357 elements.push_back(elem);
362 if (elements.empty())
364 ERR(
"MeshIO::loadMeshFromFile() - File did not contain element "
366 for (
auto& node : nodes)
377 auto*
const material_ids =
382 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
386 material_ids->insert(material_ids->end(), materials.cbegin(),
389 INFO(
"\t... finished.");
390 INFO(
"Nr. Nodes: {:d}.", nodes.size());
391 INFO(
"Nr. Elements: {:d}.", elements.size());
405 WARN(
"MeshIO::write(): Cannot write: no mesh object specified.");
415 out << n_nodes <<
"\n";
416 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)
int readMaterialID(std::istream &in)