OGS
editMaterialID.cpp File Reference
#include <spdlog/fmt/ranges.h>
#include <tclap/CmdLine.h>
#include <memory>
#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 "MeshToolsLib/MeshEditing/ElementValueModification.h"
#include "MeshToolsLib/MeshInformation.h"
Include dependency graph for editMaterialID.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 20 of file editMaterialID.cpp.

21{
22 TCLAP::CmdLine cmd(
23 "Edit material IDs of mesh elements.\n\n"
24 "OpenGeoSys-6 software, version " +
26 ".\n"
27 "Copyright (c) 2012-2026, OpenGeoSys Community "
28 "(http://www.opengeosys.org)",
30 TCLAP::SwitchArg replaceArg("r", "replace", "replace material IDs", false);
31 TCLAP::SwitchArg condenseArg("c", "condense", "condense material IDs",
32 false);
33 TCLAP::SwitchArg specifyArg(
34 "s", "specify", "specify material IDs by element types (-e)", false);
35 std::vector<TCLAP::Arg*> vec_xors;
36 vec_xors.push_back(&replaceArg);
37 vec_xors.push_back(&condenseArg);
38 vec_xors.push_back(&specifyArg);
39 cmd.xorAdd(vec_xors);
40 TCLAP::ValueArg<std::string> mesh_in(
41 "i", "mesh-input-file",
42 "Input (.vtu | .msh). The name of the file containing the input mesh",
43 true, "", "INPUT_FILE");
44 cmd.add(mesh_in);
45 TCLAP::ValueArg<std::string> mesh_out(
46 "o", "mesh-output-file",
47 "Output (.vtu | .msh). The name of the file the mesh will be written "
48 "to",
49 true, "", "OUTPUT_FILE");
50 cmd.add(mesh_out);
51 TCLAP::MultiArg<unsigned> matIDArg("m", "current-material-id",
52 "current material id to be replaced",
53 false, "CURRENT_MATERIAL_ID");
54 cmd.add(matIDArg);
55 TCLAP::ValueArg<unsigned> newIDArg(
56 "n", "new-material-id", "new material id", false, 0, "NEW_MATERIAL_ID");
57 cmd.add(newIDArg);
58
59 // TODO: FIND A BETTER SOLUTION FOR ALLOWED ELEM TYPES DEFINITION
60 std::vector<std::string> eleList(MeshLib::getMeshElemTypeStringsShort());
61 TCLAP::ValuesConstraint<std::string> allowedVals(eleList);
62 std::vector<std::string> allowed_elems_vector{
63 "point", "line", "quad", "hex", "tri", "tet", "pris", "pyra"};
64 TCLAP::ValuesConstraint<std::string> allowed_elems(allowed_elems_vector);
65 TCLAP::ValueArg<std::string> eleTypeArg("e", "element-type", "element type",
66 false, "", &allowed_elems);
67 cmd.add(eleTypeArg);
68 auto log_level_arg = BaseLib::makeLogLevelArg();
69 cmd.add(log_level_arg);
70
71 cmd.parse(argc, argv);
72
73 BaseLib::MPI::Setup mpi_setup(argc, argv);
74 BaseLib::initOGSLogger(log_level_arg.getValue());
75
76 if (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet())
77 {
78 INFO("Please select editing mode: -r or -c or -s");
79 return 0;
80 }
81 if (replaceArg.isSet() && condenseArg.isSet())
82 {
83 INFO("Please select only one editing mode: -r or -c or -s");
84 return 0;
85 }
86 if (replaceArg.isSet())
87 {
88 if (!matIDArg.isSet() || !newIDArg.isSet())
89 {
90 INFO(
91 "current and new material IDs must be provided for "
92 "replacement");
93 return 0;
94 }
95 }
96 else if (specifyArg.isSet())
97 {
98 if (!eleTypeArg.isSet() || !newIDArg.isSet())
99 {
100 INFO(
101 "element type and new material IDs must be provided to specify "
102 "elements");
103 return 0;
104 }
105 }
106
107 std::unique_ptr<MeshLib::Mesh> mesh(
108 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
109 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
110 mesh->getNumberOfElements());
111
112 if (condenseArg.isSet())
113 {
114 INFO("Condensing material ID...");
115 INFO("The MaterialIDs of the input file: [{}]",
117 ", "));
119 INFO("The MaterialIDs of the output file: [{}]",
121 ", "));
122 }
123 else if (replaceArg.isSet())
124 {
125 INFO("Replacing material ID...");
126 INFO("The MaterialIDs of the input file: [{}]",
128 ", "));
129
130 const auto vecOldID = matIDArg.getValue();
131 const unsigned newID = newIDArg.getValue();
132 for (auto oldID : vecOldID)
133 {
134 INFO("{:d} -> {:d}", oldID, newID);
136 true);
137 }
138 INFO("The MaterialIDs of the output file: [{}]",
140 ", "));
141 }
142 else if (specifyArg.isSet())
143 {
144 INFO("Specifying material ID...");
145 INFO("The MaterialIDs of the input file: [{}]",
147 ", "));
148
149 const std::string eleTypeName(eleTypeArg.getValue());
150 const MeshLib::MeshElemType eleType =
151 MeshLib::String2MeshElemType(eleTypeName);
152 const unsigned newID = newIDArg.getValue();
154 *mesh, eleType, newID);
155 INFO("updated {:d} elements", cnt);
156 INFO("The MaterialIDs of the output file: [{}]",
158 ", "));
159 }
160 MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());
161
162 return EXIT_SUCCESS;
163}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
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.
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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:84
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:37

References MeshToolsLib::ElementValueModification::condense(), MeshToolsLib::MeshInformation::getMaterialIDs(), MeshLib::getMeshElemTypeStringsShort(), INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), MeshToolsLib::ElementValueModification::replace(), MeshToolsLib::ElementValueModification::setByElementType(), MeshLib::String2MeshElemType(), and MeshLib::IO::writeMeshToFile().