44 INFO(
"Reading OGS legacy mesh ... ");
46 std::ifstream in(file_name.c_str(), std::ios::in);
49 WARN(
"MeshIO::loadMeshFromFile() - Could not open file {:s}.",
54 std::string line_string;
55 getline(in, line_string);
57 std::vector<MeshLib::Node*> nodes;
58 std::vector<MeshLib::Element*> elements;
59 std::vector<std::size_t> materials;
61 if (line_string.find(
"#FEM_MSH") != std::string::npos)
65 getline(in, line_string);
68 if (line_string.find(
"#STOP") != std::string::npos)
72 if (line_string.find(
"$NODES") != std::string::npos)
79 getline(in, line_string);
81 unsigned nNodes = atoi(line_string.c_str());
83 for (
unsigned i = 0; i < nNodes; ++i)
85 getline(in, line_string);
86 std::stringstream iss(line_string);
87 iss >> idx >> x >> y >> z;
89 nodes.push_back(node);
91 if (
s.find(
"$AREA") != std::string::npos)
97 else if (line_string.find(
"$ELEMENTS") != std::string::npos)
99 getline(in, line_string);
101 unsigned nElements = atoi(line_string.c_str());
102 for (
unsigned i = 0; i < nElements; ++i)
104 getline(in, line_string);
105 std::stringstream ss(line_string);
110 ERR(
"Reading mesh element {:d} from file '{:s}' "
114 std::for_each(elements.begin(), elements.end(),
115 std::default_delete<MeshLib::Element>());
117 std::for_each(nodes.begin(), nodes.end(),
118 std::default_delete<MeshLib::Node>());
121 elements.push_back(elem);
126 if (elements.empty())
128 ERR(
"MeshIO::loadMeshFromFile() - File did not contain element "
130 for (
auto& node : nodes)
141 auto*
const material_ids =
146 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
150 material_ids->insert(material_ids->end(), materials.cbegin(),
153 INFO(
"\t... finished.");
154 INFO(
"Nr. Nodes: {:d}.", nodes.size());
155 INFO(
"Nr. Elements: {:d}.", elements.size());
168 unsigned material_id;
169 if (!(in >> index >> material_id))
171 return std::numeric_limits<std::size_t>::max();
177 std::istream& in,
const std::vector<MeshLib::Node*>& nodes)
const
179 std::string elem_type_str;
184 if (!(in >> elem_type_str))
191 auto* idx =
new unsigned[8];
198 for (
int i = 0; i < 2; ++i)
207 for (
unsigned k(0); k < 2; ++k)
209 edge_nodes[k] = nodes[idx[k]];
216 for (
int i = 0; i < 3; ++i)
224 for (
unsigned k(0); k < 3; ++k)
226 tri_nodes[k] = nodes[idx[k]];
233 for (
int i = 0; i < 4; ++i)
241 for (
unsigned k(0); k < 4; ++k)
243 quad_nodes[k] = nodes[idx[k]];
250 for (
int i = 0; i < 4; ++i)
258 for (
unsigned k(0); k < 4; ++k)
260 tet_nodes[k] = nodes[idx[k]];
267 for (
int i = 0; i < 8; ++i)
275 for (
unsigned k(0); k < 8; ++k)
277 hex_nodes[k] = nodes[idx[k]];
284 for (
int i = 0; i < 5; ++i)
292 for (
unsigned k(0); k < 5; ++k)
294 pyramid_nodes[k] = nodes[idx[k]];
301 for (
int i = 0; i < 6; ++i)
309 for (
unsigned k(0); k < 6; ++k)
311 prism_nodes[k] = nodes[idx[k]];
330 WARN(
"MeshIO::write(): Cannot write: no mesh object specified.");
340 out << n_nodes <<
"\n";
341 for (std::size_t i(0); i < n_nodes; ++i)
370 std::vector<MeshLib::Element*>
const& ele_vec,
372 std::ostream& out)
const
374 const std::size_t ele_vector_size(ele_vec.size());
376 out << ele_vector_size <<
"\n";
377 for (std::size_t i(0); i < ele_vector_size; ++i)
379 auto const& element = *ele_vec[i];
380 if (element.getNumberOfBaseNodes() != element.getNumberOfNodes())
383 "Found high order element in the mesh that is not required by "
384 "OGS 5 input. High order elements are generated in OGS 5 on "
395 out << (*material_ids)[i] <<
" ";
398 unsigned nElemNodes(element.getNumberOfBaseNodes());
399 for (std::size_t j = 0; j < nElemNodes; ++j)
401 out << element.getNode(j)->getID() <<
" ";
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 MeshIO class.
Definition of the Node class.
std::ostringstream out
The stream to write to.
bool write() override
Write mesh to stream.
MeshLib::Element * readElement(std::istream &in, const std::vector< MeshLib::Node * > &nodes) const
MeshLib::Mesh * loadMeshFromFile(const std::string &file_name)
Read mesh from file.
const MeshLib::Mesh * _mesh
static std::size_t readMaterialID(std::istream &in)
void setMesh(const MeshLib::Mesh *mesh)
Set mesh for writing.
void writeElements(std::vector< MeshLib::Element * > const &ele_vec, MeshLib::PropertyVector< int > const *const material_ids, std::ostream &out) const
static std::string ElemType2StringOutput(const MeshLib::MeshElemType t)
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.
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
bool existsPropertyVector(std::string const &name) const
PropertyVector< T > const * getPropertyVector(std::string const &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::TriRule3 > Tri
TemplateElement< MeshLib::PrismRule6 > Prism
TemplateElement< MeshLib::PyramidRule5 > Pyramid
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
TemplateElement< MeshLib::HexRule8 > Hex
TemplateElement< MeshLib::QuadRule4 > Quad