OGS
editMaterialID.cpp File Reference

Detailed Description

Definition in file editMaterialID.cpp.

#include <spdlog/fmt/ranges.h>
#include <tclap/CmdLine.h>
#include <memory>
#include "BaseLib/MPI.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 24 of file editMaterialID.cpp.

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

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