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