OGS
generateMatPropsFromMatID.cpp
Go to the documentation of this file.
1
15#include <tclap/CmdLine.h>
16
17#ifdef USE_PETSC
18#include <mpi.h>
19#endif
20
21#include <fstream>
22#include <memory>
23
24#include "BaseLib/FileTools.h"
25#include "InfoLib/GitInfo.h"
29#include "MeshLib/Mesh.h"
30
31int main(int argc, char* argv[])
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}
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:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Mesh class.
int main(int argc, char *argv[])
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.