OGS
MeshLib::IO::Legacy::MeshIO Class Reference

Detailed Description

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

Definition at line 25 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 267 of file MeshIO.cpp.

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

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

428{
429 _mesh = mesh;
430}
const MeshLib::Mesh * _mesh
Definition MeshIO.h:44

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

390{
391 if (!_mesh)
392 {
393 WARN("MeshIO::write(): Cannot write: no mesh object specified.");
394 return false;
395 }
396
397 out << "#FEM_MSH\n"
398 << "$PCS_TYPE\n"
399 << " NO_PCS\n"
400 << "$NODES\n"
401 << " ";
402 const std::size_t n_nodes(_mesh->getNumberOfNodes());
403 out << n_nodes << "\n";
404 for (std::size_t i(0); i < n_nodes; ++i)
405 {
406 out << i << " " << *(_mesh->getNode(i)) << "\n";
407 }
408
409 out << "$ELEMENTS\n"
410 << " ";
411
412 if (!_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
413 {
414 writeElements(_mesh->getElements(), nullptr, out);
415 }
416 else
417 {
419 _mesh->getElements(),
420 _mesh->getProperties().getPropertyVector<int>("MaterialIDs"), out);
421 }
422 out << "#STOP\n";
423
424 return true;
425}
std::ostringstream out
The stream to write to.
Definition Writer.h:36
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, BaseLib::IO::Writer::out, and WARN().

Member Data Documentation

◆ _mesh

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

Definition at line 44 of file MeshIO.h.

44{nullptr};

Referenced by setMesh(), and write().


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