OGS
generateMatPropsFromMatID.cpp File Reference

Detailed Description

Implementation of the generateMatPropsFromMatID tool.

Author
Karsten Rink
Date
2011-12-19

Definition in file generateMatPropsFromMatID.cpp.

#include <tclap/CmdLine.h>
#include <mpi.h>
#include <fstream>
#include <memory>
#include "BaseLib/FileTools.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/Elements/Element.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
#include "MeshLib/Mesh.h"
Include dependency graph for generateMatPropsFromMatID.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 31 of file generateMatPropsFromMatID.cpp.

32{
33 TCLAP::CmdLine cmd(
34 "Creates a new file for material properties and sets the material ids "
35 "in the msh-file to 0\n\n"
36 "OpenGeoSys-6 software, version " +
38 ".\n"
39 "Copyright (c) 2012-2024, OpenGeoSys Community "
40 "(http://www.opengeosys.org)",
42
43 TCLAP::ValueArg<std::string> mesh_arg("m",
44 "mesh",
45 "the mesh to open from a file",
46 false,
47 "",
48 "filename for mesh input");
49 cmd.add(mesh_arg);
50
51 cmd.parse(argc, argv);
52
53#ifdef USE_PETSC
54 MPI_Init(&argc, &argv);
55#endif
56
57 // read mesh
58 std::unique_ptr<MeshLib::Mesh> mesh(
59 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
60
61 if (!mesh)
62 {
63 INFO("Could not read mesh from file '{:s}'.", mesh_arg.getValue());
64#ifdef USE_PETSC
65 MPI_Finalize();
66#endif
67 return EXIT_FAILURE;
68 }
69
70 auto const materialIds = materialIDs(*mesh);
71 if (!materialIds)
72 {
73 OGS_FATAL("Mesh contains no int-property vector named 'MaterialIDs'.");
74 }
75
76 std::size_t const n_properties(materialIds->size());
77 assert(n_properties != mesh->getNumberOfElements());
78
79 std::string const name =
81 // create file
82 std::string const new_matname(name + "_prop");
83 std::ofstream out_prop(new_matname.c_str(), std::ios::out);
84 if (out_prop.is_open())
85 {
86 for (std::size_t i = 0; i < n_properties; ++i)
87 {
88 out_prop << i << "\t" << (*materialIds)[i] << "\n";
89 }
90 out_prop.close();
91 }
92 else
93 {
94 ERR("Could not create property '{:s}' file.", new_matname);
95#ifdef USE_PETSC
96 MPI_Finalize();
97#endif
98 return EXIT_FAILURE;
99 }
100
101 mesh->getProperties().removePropertyVector("MaterialIDs");
102
103 std::string const new_mshname(name + "_new.vtu");
104 INFO("Writing mesh to file '{:s}'.", new_mshname);
105 MeshLib::IO::writeMeshToFile(*mesh, new_mshname);
106
107 INFO("New files '{:s}' and '{:s}' written.", new_mshname, new_matname);
108
109#ifdef USE_PETSC
110 MPI_Finalize();
111#endif
112 return EXIT_SUCCESS;
113}
#define OGS_FATAL(...)
Definition Error.h:26
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
std::string extractBaseNameWithoutExtension(std::string const &pathname)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:268

References ERR(), BaseLib::extractBaseNameWithoutExtension(), INFO(), OGS_FATAL, GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), and MeshLib::IO::writeMeshToFile().