OGS
editMaterialID.cpp
Go to the documentation of this file.
1
10#include <spdlog/fmt/ranges.h>
11#include <tclap/CmdLine.h>
12
13#include <memory>
14
15#include "BaseLib/Logging.h"
16#include "BaseLib/MPI.h"
18#include "InfoLib/GitInfo.h"
22#include "MeshLib/Mesh.h"
25
26int main(int argc, char* argv[])
27{
28 TCLAP::CmdLine cmd(
29 "Edit material IDs of mesh elements.\n\n"
30 "OpenGeoSys-6 software, version " +
32 ".\n"
33 "Copyright (c) 2012-2025, OpenGeoSys Community "
34 "(http://www.opengeosys.org)",
36 TCLAP::SwitchArg replaceArg("r", "replace", "replace material IDs", false);
37 TCLAP::SwitchArg condenseArg("c", "condense", "condense material IDs",
38 false);
39 TCLAP::SwitchArg specifyArg(
40 "s", "specify", "specify material IDs by element types (-e)", false);
41 std::vector<TCLAP::Arg*> vec_xors;
42 vec_xors.push_back(&replaceArg);
43 vec_xors.push_back(&condenseArg);
44 vec_xors.push_back(&specifyArg);
45 cmd.xorAdd(vec_xors);
46 TCLAP::ValueArg<std::string> mesh_in(
47 "i", "mesh-input-file",
48 "Input (.vtu | .msh). The name of the file containing the input mesh",
49 true, "", "INPUT_FILE");
50 cmd.add(mesh_in);
51 TCLAP::ValueArg<std::string> mesh_out(
52 "o", "mesh-output-file",
53 "Output (.vtu | .msh). The name of the file the mesh will be written "
54 "to",
55 true, "", "OUTPUT_FILE");
56 cmd.add(mesh_out);
57 TCLAP::MultiArg<unsigned> matIDArg("m", "current-material-id",
58 "current material id to be replaced",
59 false, "CURRENT_MATERIAL_ID");
60 cmd.add(matIDArg);
61 TCLAP::ValueArg<unsigned> newIDArg(
62 "n", "new-material-id", "new material id", false, 0, "NEW_MATERIAL_ID");
63 cmd.add(newIDArg);
64
65 // TODO: FIND A BETTER SOLUTION FOR ALLOWED ELEM TYPES DEFINITION
66 std::vector<std::string> eleList(MeshLib::getMeshElemTypeStringsShort());
67 TCLAP::ValuesConstraint<std::string> allowedVals(eleList);
68 std::vector<std::string> allowed_elems_vector{
69 "point", "line", "quad", "hex", "tri", "tet", "pris", "pyra"};
70 TCLAP::ValuesConstraint<std::string> allowed_elems(allowed_elems_vector);
71 TCLAP::ValueArg<std::string> eleTypeArg("e", "element-type", "element type",
72 false, "", &allowed_elems);
73 cmd.add(eleTypeArg);
74 auto log_level_arg = BaseLib::makeLogLevelArg();
75 cmd.add(log_level_arg);
76
77 cmd.parse(argc, argv);
78
79 BaseLib::MPI::Setup mpi_setup(argc, argv);
80 BaseLib::initOGSLogger(log_level_arg.getValue());
81
82 if (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet())
83 {
84 INFO("Please select editing mode: -r or -c or -s");
85 return 0;
86 }
87 if (replaceArg.isSet() && condenseArg.isSet())
88 {
89 INFO("Please select only one editing mode: -r or -c or -s");
90 return 0;
91 }
92 if (replaceArg.isSet())
93 {
94 if (!matIDArg.isSet() || !newIDArg.isSet())
95 {
96 INFO(
97 "current and new material IDs must be provided for "
98 "replacement");
99 return 0;
100 }
101 }
102 else if (specifyArg.isSet())
103 {
104 if (!eleTypeArg.isSet() || !newIDArg.isSet())
105 {
106 INFO(
107 "element type and new material IDs must be provided to specify "
108 "elements");
109 return 0;
110 }
111 }
112
113 std::unique_ptr<MeshLib::Mesh> mesh(
114 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
115 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
116 mesh->getNumberOfElements());
117
118 if (condenseArg.isSet())
119 {
120 INFO("Condensing material ID...");
121 INFO("The MaterialIDs of the input file: [{}]",
123 ", "));
125 INFO("The MaterialIDs of the output file: [{}]",
127 ", "));
128 }
129 else if (replaceArg.isSet())
130 {
131 INFO("Replacing material ID...");
132 INFO("The MaterialIDs of the input file: [{}]",
134 ", "));
135
136 const auto vecOldID = matIDArg.getValue();
137 const unsigned newID = newIDArg.getValue();
138 for (auto oldID : vecOldID)
139 {
140 INFO("{:d} -> {:d}", oldID, newID);
142 true);
143 }
144 INFO("The MaterialIDs of the output file: [{}]",
146 ", "));
147 }
148 else if (specifyArg.isSet())
149 {
150 INFO("Specifying material ID...");
151 INFO("The MaterialIDs of the input file: [{}]",
153 ", "));
154
155 const std::string eleTypeName(eleTypeArg.getValue());
156 const MeshLib::MeshElemType eleType =
157 MeshLib::String2MeshElemType(eleTypeName);
158 const unsigned newID = newIDArg.getValue();
160 *mesh, eleType, newID);
161 INFO("updated {:d} elements", cnt);
162 INFO("The MaterialIDs of the output file: [{}]",
164 ", "));
165 }
166 MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());
167
168 return EXIT_SUCCESS;
169}
Definition of the ElementValueModification class.
Definition of the Element class.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
Definition of the MeshInformation class.
Definition of the Mesh class.
static std::size_t setByElementType(MeshLib::Mesh &mesh, MeshLib::MeshElemType ele_type, int const new_value)
static bool replace(MeshLib::Mesh &mesh, int const old_value, int const new_value, bool replace_if_exists=false)
static std::size_t condense(MeshLib::Mesh &mesh)
static std::vector< int > getMaterialIDs(const MeshLib::Mesh &mesh)
writes out a list of all material IDs that occur in the mesh.
int main(int argc, char *argv[])
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
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)
MeshElemType String2MeshElemType(const std::string &s)
Given a string of the shortened name of the element type, this returns the corresponding MeshElemType...
Definition MeshEnums.cpp:95
std::vector< std::string > getMeshElemTypeStringsShort()
Returns a vector of strings of mesh element types.
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:48
Definition of readMeshFromFile function.