43 INFO(
"Reading OGS legacy mesh ... ");
45 std::ifstream in(file_name.c_str(), std::ios::in);
48 WARN(
"MeshIO::loadMeshFromFile() - Could not open file {:s}.",
53 std::string line_string;
54 getline(in, line_string);
56 std::vector<MeshLib::Node*> nodes;
57 std::vector<MeshLib::Element*> elements;
58 std::vector<std::size_t> materials;
60 if (line_string.find(
"#FEM_MSH") != std::string::npos)
64 getline(in, line_string);
67 if (line_string.find(
"#STOP") != std::string::npos)
71 if (line_string.find(
"$NODES") != std::string::npos)
78 getline(in, line_string);
80 unsigned nNodes = atoi(line_string.c_str());
82 for (
unsigned i = 0; i < nNodes; ++i)
84 getline(in, line_string);
85 std::stringstream iss(line_string);
86 iss >> idx >> x >> y >> z;
88 nodes.push_back(node);
90 if (s.find(
"$AREA") != std::string::npos)
96 else if (line_string.find(
"$ELEMENTS") != std::string::npos)
98 getline(in, line_string);
100 unsigned nElements = atoi(line_string.c_str());
101 for (
unsigned i = 0; i < nElements; ++i)
103 getline(in, line_string);
104 std::stringstream ss(line_string);
109 ERR(
"Reading mesh element {:d} from file '{:s}' "
113 std::for_each(elements.begin(), elements.end(),
114 std::default_delete<MeshLib::Element>());
116 std::for_each(nodes.begin(), nodes.end(),
117 std::default_delete<MeshLib::Node>());
120 elements.push_back(elem);
125 if (elements.empty())
127 ERR(
"MeshIO::loadMeshFromFile() - File did not contain element "
129 for (
auto& node : nodes)
140 auto*
const material_ids =
145 WARN(
"Could not create PropertyVector for MaterialIDs in Mesh.");
149 material_ids->insert(material_ids->end(), materials.cbegin(),
152 INFO(
"\t... finished.");
153 INFO(
"Nr. Nodes: {:d}.", nodes.size());
154 INFO(
"Nr. Elements: {:d}.", elements.size());
167 unsigned material_id;
168 if (!(in >> index >> material_id))
170 return std::numeric_limits<std::size_t>::max();
176 std::istream& in,
const std::vector<MeshLib::Node*>& nodes)
const
178 std::string elem_type_str;
183 if (!(in >> elem_type_str))
190 auto* idx =
new unsigned[8];
197 for (
int i = 0; i < 2; ++i)
206 for (
unsigned k(0); k < 2; ++k)
208 edge_nodes[k] = nodes[idx[k]];
215 for (
int i = 0; i < 3; ++i)
223 for (
unsigned k(0); k < 3; ++k)
225 tri_nodes[k] = nodes[idx[k]];
232 for (
int i = 0; i < 4; ++i)
240 for (
unsigned k(0); k < 4; ++k)
242 quad_nodes[k] = nodes[idx[k]];
249 for (
int i = 0; i < 4; ++i)
257 for (
unsigned k(0); k < 4; ++k)
259 tet_nodes[k] = nodes[idx[k]];
266 for (
int i = 0; i < 8; ++i)
274 for (
unsigned k(0); k < 8; ++k)
276 hex_nodes[k] = nodes[idx[k]];
283 for (
int i = 0; i < 5; ++i)
291 for (
unsigned k(0); k < 5; ++k)
293 pyramid_nodes[k] = nodes[idx[k]];
300 for (
int i = 0; i < 6; ++i)
308 for (
unsigned k(0); k < 6; ++k)
310 prism_nodes[k] = nodes[idx[k]];
329 WARN(
"MeshIO::write(): Cannot write: no mesh object specified.");
339 out << n_nodes <<
"\n";
340 for (std::size_t i(0); i < n_nodes; ++i)
369 std::vector<MeshLib::Element*>
const& ele_vec,
371 std::ostream& out)
const
373 const std::size_t ele_vector_size(ele_vec.size());
375 out << ele_vector_size <<
"\n";
376 for (std::size_t i(0); i < ele_vector_size; ++i)
385 out << (*material_ids)[i] <<
" ";
388 unsigned nElemNodes(ele_vec[i]->getNumberOfBaseNodes());
389 for (std::size_t j = 0; j < nElemNodes; ++j)
391 out << ele_vec[i]->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.
Properties & getProperties()
std::size_t getNumberOfNodes() const
Get the number of nodes.
const Node * getNode(std::size_t idx) const
Get the node with the given index.
PropertyVector< T > const * getPropertyVector(std::string const &name) const
bool existsPropertyVector(std::string const &name) const
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
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