OGS
editMaterialID.cpp File Reference

Detailed Description

Definition in file editMaterialID.cpp.

#include <tclap/CmdLine.h>
#include <mpi.h>
#include <memory>
#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 26 of file editMaterialID.cpp.

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