OGS
editMaterialID.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <memory>
13
14#include "BaseLib/MPI.h"
15#include "InfoLib/GitInfo.h"
19#include "MeshLib/Mesh.h"
22
23int main(int argc, char* argv[])
24{
25 TCLAP::CmdLine cmd(
26 "Edit material IDs of mesh elements.\n\n"
27 "OpenGeoSys-6 software, version " +
29 ".\n"
30 "Copyright (c) 2012-2024, OpenGeoSys Community "
31 "(http://www.opengeosys.org)",
33 TCLAP::SwitchArg replaceArg("r", "replace", "replace material IDs", false);
34 TCLAP::SwitchArg condenseArg("c", "condense", "condense material IDs",
35 false);
36 TCLAP::SwitchArg specifyArg(
37 "s", "specify", "specify material IDs by element types (-e)", false);
38 std::vector<TCLAP::Arg*> vec_xors;
39 vec_xors.push_back(&replaceArg);
40 vec_xors.push_back(&condenseArg);
41 vec_xors.push_back(&specifyArg);
42 cmd.xorAdd(vec_xors);
43 TCLAP::ValueArg<std::string> mesh_in(
44 "i", "mesh-input-file",
45 "the name of the file containing the input mesh", true, "",
46 "file name");
47 cmd.add(mesh_in);
48 TCLAP::ValueArg<std::string> mesh_out(
49 "o", "mesh-output-file",
50 "the name of the file the mesh will be written to", true, "",
51 "file name");
52 cmd.add(mesh_out);
53 TCLAP::MultiArg<unsigned> matIDArg("m", "current-material-id",
54 "current material id to be replaced",
55 false, "number");
56 cmd.add(matIDArg);
57 TCLAP::ValueArg<unsigned> newIDArg("n", "new-material-id",
58 "new material id", false, 0, "number");
59 cmd.add(newIDArg);
60 std::vector<std::string> eleList(MeshLib::getMeshElemTypeStringsShort());
61 TCLAP::ValuesConstraint<std::string> allowedVals(eleList);
62 TCLAP::ValueArg<std::string> eleTypeArg("e", "element-type", "element type",
63 false, "", &allowedVals);
64 cmd.add(eleTypeArg);
65
66 cmd.parse(argc, argv);
67
68 BaseLib::MPI::Setup mpi_setup(argc, argv);
69
70 if (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet())
71 {
72 INFO("Please select editing mode: -r or -c or -s");
73 return 0;
74 }
75 if (replaceArg.isSet() && condenseArg.isSet())
76 {
77 INFO("Please select only one editing mode: -r or -c or -s");
78 return 0;
79 }
80 if (replaceArg.isSet())
81 {
82 if (!matIDArg.isSet() || !newIDArg.isSet())
83 {
84 INFO(
85 "current and new material IDs must be provided for "
86 "replacement");
87 return 0;
88 }
89 }
90 else if (specifyArg.isSet())
91 {
92 if (!eleTypeArg.isSet() || !newIDArg.isSet())
93 {
94 INFO(
95 "element type and new material IDs must be provided to specify "
96 "elements");
97 return 0;
98 }
99 }
100
101 std::unique_ptr<MeshLib::Mesh> mesh(
102 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
103 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
104 mesh->getNumberOfElements());
105
106 if (condenseArg.isSet())
107 {
108 INFO("Condensing material ID...");
109 INFO("The MaterialIDs of the input file: [{}]",
111 ", "));
113 INFO("The MaterialIDs of the output file: [{}]",
115 ", "));
116 }
117 else if (replaceArg.isSet())
118 {
119 INFO("Replacing material ID...");
120 INFO("The MaterialIDs of the input file: [{}]",
122 ", "));
123
124 const auto vecOldID = matIDArg.getValue();
125 const unsigned newID = newIDArg.getValue();
126 for (auto oldID : vecOldID)
127 {
128 INFO("{:d} -> {:d}", oldID, newID);
130 true);
131 }
132 INFO("The MaterialIDs of the output file: [{}]",
134 ", "));
135 }
136 else if (specifyArg.isSet())
137 {
138 INFO("Specifying material ID...");
139 INFO("The MaterialIDs of the input file: [{}]",
141 ", "));
142
143 const std::string eleTypeName(eleTypeArg.getValue());
144 const MeshLib::MeshElemType eleType =
145 MeshLib::String2MeshElemType(eleTypeName);
146 const unsigned newID = newIDArg.getValue();
148 *mesh, eleType, newID);
149 INFO("updated {:d} elements", cnt);
150 INFO("The MaterialIDs of the output file: [{}]",
152 ", "));
153 }
154 MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());
155
156 return EXIT_SUCCESS;
157}
Definition of the ElementValueModification class.
Definition of the Element class.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
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[])
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:27
Definition of readMeshFromFile function.