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 278 of file MeshIO.cpp.

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

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 439 of file MeshIO.cpp.

440{
441 _mesh = mesh;
442}
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 401 of file MeshIO.cpp.

402{
403 if (!_mesh)
404 {
405 WARN("MeshIO::write(): Cannot write: no mesh object specified.");
406 return false;
407 }
408
409 out << "#FEM_MSH\n"
410 << "$PCS_TYPE\n"
411 << " NO_PCS\n"
412 << "$NODES\n"
413 << " ";
414 const std::size_t n_nodes(_mesh->getNumberOfNodes());
415 out << n_nodes << "\n";
416 for (std::size_t i(0); i < n_nodes; ++i)
417 {
418 out << i << " " << *(_mesh->getNode(i)) << "\n";
419 }
420
421 out << "$ELEMENTS\n"
422 << " ";
423
424 if (!_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
425 {
426 writeElements(_mesh->getElements(), nullptr, out);
427 }
428 else
429 {
432 _mesh->getProperties().getPropertyVector<int>("MaterialIDs"), out);
433 }
434 out << "#STOP\n";
435
436 return true;
437}
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:109
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition Mesh.h:91
Properties & getProperties()
Definition Mesh.h:134
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
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: