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.
 

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

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

435{
436 _mesh = mesh;
437}
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 396 of file MeshIO.cpp.

397{
398 if (!_mesh)
399 {
400 WARN("MeshIO::write(): Cannot write: no mesh object specified.");
401 return false;
402 }
403
404 out << "#FEM_MSH\n"
405 << "$PCS_TYPE\n"
406 << " NO_PCS\n"
407 << "$NODES\n"
408 << " ";
409 const std::size_t n_nodes(_mesh->getNumberOfNodes());
410 out << n_nodes << "\n";
411 for (std::size_t i(0); i < n_nodes; ++i)
412 {
413 out << i << " " << *(_mesh->getNode(i)) << "\n";
414 }
415
416 out << "$ELEMENTS\n"
417 << " ";
418
419 if (!_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
420 {
421 writeElements(_mesh->getElements(), nullptr, out);
422 }
423 else
424 {
427 _mesh->getProperties().getPropertyVector<int>("MaterialIDs"), out);
428 }
429 out << "#STOP\n";
430
431 return true;
432}
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
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: