Loading [MathJax]/extensions/tex2jax.js
OGS
MeshLib::IO::Legacy::MeshIO Class Reference

Detailed Description

Interface for handling mesh files from OGS-5 and below. (*.msh files)

Definition at line 36 of file MeshIO.h.

#include <MeshIO.h>

Inheritance diagram for MeshLib::IO::Legacy::MeshIO:
[legend]
Collaboration diagram for MeshLib::IO::Legacy::MeshIO:
[legend]

Public Member Functions

 MeshIO ()
 Constructor.
 
 ~MeshIO () override=default
 
MeshLib::MeshloadMeshFromFile (const std::string &file_name)
 Read mesh from file.
 
void setMesh (const MeshLib::Mesh *mesh)
 Set mesh for writing.
 
- Public Member Functions inherited from BaseLib::IO::Writer
 Writer ()
 
virtual ~Writer ()=default
 
std::string writeToString ()
 Writes the object to a string.
 

Protected Member Functions

bool write () override
 Write mesh to stream.
 
- Protected Member Functions inherited from BaseLib::IO::Writer

Private Attributes

const MeshLib::Mesh_mesh {nullptr}
 

Additional Inherited Members

- Protected Attributes inherited from BaseLib::IO::Writer
std::ostringstream out
 The stream to write to.
 

Constructor & Destructor Documentation

◆ MeshIO()

MeshLib::IO::Legacy::MeshIO::MeshIO ( )
default

Constructor.

◆ ~MeshIO()

MeshLib::IO::Legacy::MeshIO::~MeshIO ( )
overridedefault

Member Function Documentation

◆ loadMeshFromFile()

MeshLib::Mesh * MeshLib::IO::Legacy::MeshIO::loadMeshFromFile ( const std::string & file_name)

Read mesh from file.

Definition at line 277 of file MeshIO.cpp.

278{
279 INFO("Reading OGS legacy mesh ... ");
280
281 std::ifstream in(file_name.c_str(), std::ios::in);
282 if (!in.is_open())
283 {
284 WARN("MeshIO::loadMeshFromFile() - Could not open file {:s}.",
285 file_name);
286 return nullptr;
287 }
288
289 std::string line_string;
290 std::getline(in, line_string);
291
292 if (line_string.find("#FEM_MSH") != std::string::npos) // OGS mesh file
293 {
294 std::vector<MeshLib::Node*> nodes;
295 std::vector<MeshLib::Element*> elements;
296 std::vector<int> materials;
297
298 while (!in.eof())
299 {
300 std::getline(in, line_string);
301
302 // check keywords
303 if (line_string.find("#STOP") != std::string::npos)
304 {
305 break;
306 }
307 if (line_string.find("$NODES") != std::string::npos)
308 {
309 std::getline(in, line_string);
310 BaseLib::trim(line_string);
311 unsigned nNodes = atoi(line_string.c_str());
312 for (unsigned i = 0; i < nNodes; ++i)
313 {
314 std::getline(in, line_string);
315 std::stringstream iss(line_string);
316 unsigned idx;
317 double x;
318 double y;
319 double z;
320 iss >> idx >> x >> y >> z;
321 auto* node(new MeshLib::Node(x, y, z, idx));
322 nodes.push_back(node);
323 std::string s;
324 iss >> s;
325 if (s.find("$AREA") != std::string::npos)
326 {
327 double double_dummy;
328 iss >> double_dummy;
329 }
330 }
331 }
332 else if (line_string.find("$ELEMENTS") != std::string::npos)
333 {
334 std::getline(in, line_string);
335 BaseLib::trim(line_string);
336 unsigned nElements = atoi(line_string.c_str());
337 for (unsigned i = 0; i < nElements; ++i)
338 {
339 std::getline(in, line_string);
340 std::stringstream ss(line_string);
341 materials.push_back(readMaterialID(ss));
342 MeshLib::Element* elem(readElement(ss, nodes));
343 if (elem == nullptr)
344 {
345 ERR("Reading mesh element {:d} from file '{:s}' "
346 "failed.",
347 i, file_name);
348 // clean up the elements vector
349 std::for_each(elements.begin(), elements.end(),
350 std::default_delete<MeshLib::Element>());
351 // clean up the nodes vector
352 std::for_each(nodes.begin(), nodes.end(),
353 std::default_delete<MeshLib::Node>());
354 return nullptr;
355 }
356 elements.push_back(elem);
357 }
358 }
359 }
360
361 if (elements.empty())
362 {
363 ERR("MeshIO::loadMeshFromFile() - File did not contain element "
364 "information.");
365 for (auto& node : nodes)
366 {
367 delete node;
368 }
369 return nullptr;
370 }
371
374 elements, true /* compute_element_neighbors */));
375
376 auto* const material_ids =
377 mesh->getProperties().createNewPropertyVector<int>(
378 "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
379 if (!material_ids)
380 {
381 WARN("Could not create PropertyVector for MaterialIDs in Mesh.");
382 }
383 else
384 {
385 material_ids->insert(material_ids->end(), materials.cbegin(),
386 materials.cend());
387 }
388 INFO("\t... finished.");
389 INFO("Nr. Nodes: {:d}.", nodes.size());
390 INFO("Nr. Elements: {:d}.", elements.size());
391
392 in.close();
393 return mesh;
394 }
395
396 in.close();
397 return nullptr;
398}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void trim(std::string &str, char ch)
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 const is_created_with_gmsh2)
int readMaterialID(std::istream &in)
Definition MeshIO.cpp:31

References MeshLib::Cell, MeshLib::Properties::createNewPropertyVector(), ERR(), BaseLib::extractBaseNameWithoutExtension(), MeshLib::Mesh::getProperties(), INFO(), BaseLib::trim(), and WARN().

Referenced by OGSFileConverter::convertMSH2VTU(), and anonymous_namespace{readMeshFromFile.cpp}::readMeshFromFileSerial().

◆ setMesh()

void MeshLib::IO::Legacy::MeshIO::setMesh ( const MeshLib::Mesh * mesh)

Set mesh for writing.

Definition at line 438 of file MeshIO.cpp.

439{
440 _mesh = mesh;
441}
const MeshLib::Mesh * _mesh
Definition MeshIO.h:55

References _mesh.

Referenced by SaveMeshDialog::accept(), OGSFileConverter::convertVTU2MSH(), main(), and MeshLib::IO::writeMeshToFile().

◆ write()

bool MeshLib::IO::Legacy::MeshIO::write ( )
overrideprotectedvirtual

Write mesh to stream.

Implements BaseLib::IO::Writer.

Definition at line 400 of file MeshIO.cpp.

401{
402 if (!_mesh)
403 {
404 WARN("MeshIO::write(): Cannot write: no mesh object specified.");
405 return false;
406 }
407
408 out << "#FEM_MSH\n"
409 << "$PCS_TYPE\n"
410 << " NO_PCS\n"
411 << "$NODES\n"
412 << " ";
413 const std::size_t n_nodes(_mesh->getNumberOfNodes());
414 out << n_nodes << "\n";
415 for (std::size_t i(0); i < n_nodes; ++i)
416 {
417 out << i << " " << *(_mesh->getNode(i)) << "\n";
418 }
419
420 out << "$ELEMENTS\n"
421 << " ";
422
423 if (!_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
424 {
425 writeElements(_mesh->getElements(), nullptr, out);
426 }
427 else
428 {
431 _mesh->getProperties().getPropertyVector<int>("MaterialIDs"), out);
432 }
433 out << "#STOP\n";
434
435 return true;
436}
std::ostringstream out
The stream to write to.
Definition Writer.h:47
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition Mesh.h:93
Properties & getProperties()
Definition Mesh.h:136
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:102
bool existsPropertyVector(std::string_view name) const
Definition Properties.h:74
PropertyVector< T > const * getPropertyVector(std::string_view name) const
void writeElements(std::string const &file_name_base, std::vector< Partition > const &partitions, std::vector< long > const &regular_element_offsets, std::vector< long > const &ghost_element_offsets)

References _mesh, MeshLib::Properties::existsPropertyVector(), MeshLib::Mesh::getElements(), MeshLib::Mesh::getNode(), MeshLib::Mesh::getNumberOfNodes(), MeshLib::Mesh::getProperties(), MeshLib::Properties::getPropertyVector(), BaseLib::IO::Writer::out, and WARN().

Member Data Documentation

◆ _mesh

const MeshLib::Mesh* MeshLib::IO::Legacy::MeshIO::_mesh {nullptr}
private

Definition at line 55 of file MeshIO.h.

55{nullptr};

Referenced by setMesh(), and write().


The documentation for this class was generated from the following files: