OGS
generateMatPropsFromMatID.cpp File Reference
#include <tclap/CmdLine.h>
#include <fstream>
#include <memory>
#include "BaseLib/FileTools.h"
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.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 19 of file generateMatPropsFromMatID.cpp.

20{
21 TCLAP::CmdLine cmd(
22 "Creates a new file for material properties and sets the material ids "
23 "in the msh-file to 0\n\n"
24 "OpenGeoSys-6 software, version " +
26 ".\n"
27 "Copyright (c) 2012-2026, OpenGeoSys Community "
28 "(http://www.opengeosys.org)",
30
31 TCLAP::ValueArg<std::string> mesh_arg(
32 "m",
33 "mesh",
34 "Input (.msh). The mesh to open from a file",
35 false,
36 "",
37 "INPUT_FILE");
38 cmd.add(mesh_arg);
39 auto log_level_arg = BaseLib::makeLogLevelArg();
40 cmd.add(log_level_arg);
41
42 cmd.parse(argc, argv);
43
44 BaseLib::MPI::Setup mpi_setup(argc, argv);
45 BaseLib::initOGSLogger(log_level_arg.getValue());
46
47 // read mesh
48 std::unique_ptr<MeshLib::Mesh> mesh(
49 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
50
51 if (!mesh)
52 {
53 INFO("Could not read mesh from file '{:s}'.", mesh_arg.getValue());
54 return EXIT_FAILURE;
55 }
56
57 auto const materialIds = materialIDs(*mesh);
58 if (!materialIds)
59 {
60 OGS_FATAL("Mesh contains no int-property vector named 'MaterialIDs'.");
61 }
62
63 std::size_t const n_properties(materialIds->size());
64 assert(n_properties != mesh->getNumberOfElements());
65
66 std::string const name =
68 // create file
69 std::string const new_matname(name + "_prop");
70 std::ofstream out_prop(new_matname.c_str(), std::ios::out);
71 if (out_prop.is_open())
72 {
73 for (std::size_t i = 0; i < n_properties; ++i)
74 {
75 out_prop << i << "\t" << (*materialIds)[i] << "\n";
76 }
77 out_prop.close();
78 }
79 else
80 {
81 ERR("Could not create property '{:s}' file.", new_matname);
82 return EXIT_FAILURE;
83 }
84
85 mesh->getProperties().removePropertyVector("MaterialIDs");
86
87 std::string const new_mshname(name + "_new.vtu");
88 INFO("Writing mesh to file '{:s}'.", new_mshname);
89 MeshLib::IO::writeMeshToFile(*mesh, new_mshname);
90
91 INFO("New files '{:s}' and '{:s}' written.", new_mshname, new_matname);
92
93 return EXIT_SUCCESS;
94}
#define OGS_FATAL(...)
Definition Error.h:19
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
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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:258

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