OGS
editMaterialID.cpp File Reference

Detailed Description

Definition in file editMaterialID.cpp.

#include <spdlog/fmt/bundled/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 "the name of the file containing the input mesh", true, "",
47 "file name");
48 cmd.add(mesh_in);
49 TCLAP::ValueArg<std::string> mesh_out(
50 "o", "mesh-output-file",
51 "the name of the file the mesh will be written to", true, "",
52 "file name");
53 cmd.add(mesh_out);
54 TCLAP::MultiArg<unsigned> matIDArg("m", "current-material-id",
55 "current material id to be replaced",
56 false, "number");
57 cmd.add(matIDArg);
58 TCLAP::ValueArg<unsigned> newIDArg("n", "new-material-id",
59 "new material id", false, 0, "number");
60 cmd.add(newIDArg);
61 std::vector<std::string> eleList(MeshLib::getMeshElemTypeStringsShort());
62 TCLAP::ValuesConstraint<std::string> allowedVals(eleList);
63 TCLAP::ValueArg<std::string> eleTypeArg("e", "element-type", "element type",
64 false, "", &allowedVals);
65 cmd.add(eleTypeArg);
66
67 cmd.parse(argc, argv);
68
69 BaseLib::MPI::Setup mpi_setup(argc, argv);
70
71 if (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet())
72 {
73 INFO("Please select editing mode: -r or -c or -s");
74 return 0;
75 }
76 if (replaceArg.isSet() && condenseArg.isSet())
77 {
78 INFO("Please select only one editing mode: -r or -c or -s");
79 return 0;
80 }
81 if (replaceArg.isSet())
82 {
83 if (!matIDArg.isSet() || !newIDArg.isSet())
84 {
85 INFO(
86 "current and new material IDs must be provided for "
87 "replacement");
88 return 0;
89 }
90 }
91 else if (specifyArg.isSet())
92 {
93 if (!eleTypeArg.isSet() || !newIDArg.isSet())
94 {
95 INFO(
96 "element type and new material IDs must be provided to specify "
97 "elements");
98 return 0;
99 }
100 }
101
102 std::unique_ptr<MeshLib::Mesh> mesh(
103 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
104 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
105 mesh->getNumberOfElements());
106
107 if (condenseArg.isSet())
108 {
109 INFO("Condensing material ID...");
110 INFO("The MaterialIDs of the input file: [{}]",
112 ", "));
114 INFO("The MaterialIDs of the output file: [{}]",
116 ", "));
117 }
118 else if (replaceArg.isSet())
119 {
120 INFO("Replacing material ID...");
121 INFO("The MaterialIDs of the input file: [{}]",
123 ", "));
124
125 const auto vecOldID = matIDArg.getValue();
126 const unsigned newID = newIDArg.getValue();
127 for (auto oldID : vecOldID)
128 {
129 INFO("{:d} -> {:d}", oldID, newID);
131 true);
132 }
133 INFO("The MaterialIDs of the output file: [{}]",
135 ", "));
136 }
137 else if (specifyArg.isSet())
138 {
139 INFO("Specifying material ID...");
140 INFO("The MaterialIDs of the input file: [{}]",
142 ", "));
143
144 const std::string eleTypeName(eleTypeArg.getValue());
145 const MeshLib::MeshElemType eleType =
146 MeshLib::String2MeshElemType(eleTypeName);
147 const unsigned newID = newIDArg.getValue();
149 *mesh, eleType, newID);
150 INFO("updated {:d} elements", cnt);
151 INFO("The MaterialIDs of the output file: [{}]",
153 ", "));
154 }
155 MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());
156
157 return EXIT_SUCCESS;
158}
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:27

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().