OGS
removeMeshElements.cpp
Go to the documentation of this file.
1
14#include <tclap/CmdLine.h>
15
16#include <memory>
17
18#include "BaseLib/Logging.h"
19#include "BaseLib/MPI.h"
21#include "InfoLib/GitInfo.h"
25#include "MeshLib/Mesh.h"
26#include "MeshLib/MeshEnums.h"
28#include "MeshLib/Node.h"
31
32template <typename PROPERTY_TYPE>
33void searchByPropertyValue(std::string const& property_name,
34 std::vector<PROPERTY_TYPE> const& property_values,
35 MeshLib::ElementSearch& searcher)
36{
37 for (auto const& property_value : property_values)
38 {
39 std::size_t n_marked_elements = searcher.searchByPropertyValue<double>(
40 property_name, property_value);
41 if (n_marked_elements == 0)
42 {
43 n_marked_elements = searcher.searchByPropertyValue<int>(
44 property_name, property_value);
45 }
46
47 INFO("{:d} elements with property value {:s} found.", n_marked_elements,
48 std::to_string(property_value));
49 }
50}
51
52void searchByPropertyRange(std::string const& property_name,
53 double const& min_value, double const& max_value,
54 bool const& outside,
55 MeshLib::ElementSearch& searcher)
56{
57 std::size_t n_marked_elements = searcher.searchByPropertyValueRange<double>(
58 property_name, min_value, max_value, outside);
59
60 if (n_marked_elements == 0)
61 {
62 n_marked_elements = searcher.searchByPropertyValueRange<int>(
63 property_name, static_cast<int>(min_value),
64 static_cast<int>(max_value), outside);
65 }
66
67 // add checks for other data types here (if n_marked_elements remains 0)
68
69 INFO("{:d} elements in range [{:s}, {:s}] found.", n_marked_elements,
70 std::to_string(min_value), std::to_string(max_value));
71}
72
73void outputAABB(MeshLib::Mesh const& mesh)
74{
76 auto const [min, max] = aabb.getMinMaxPoints();
77
78 INFO(
79 "Bounding box of \"{:s}\" is\nx = [{:f},{:f}]\ny = [{:f},{:f}]\nz = "
80 "[{:f},{:f}]",
81 mesh.getName(), min.x(), max.x(), min.y(), max.y(), min.z(), max.z());
82}
83
84int main(int argc, char* argv[])
85{
86 TCLAP::CmdLine cmd(
87 "Removes mesh elements based on element type, element volume, scalar "
88 "arrays, or bounding box . The documentation is available at "
89 "https://docs.opengeosys.org/docs/tools/meshing/"
90 "remove-mesh-elements.\n\n"
91 "OpenGeoSys-6 software, version " +
93 ".\n"
94 "Copyright (c) 2012-2025, OpenGeoSys Community "
95 "(http://www.opengeosys.org)",
97
98 // Bounding box params
99 TCLAP::SwitchArg invert_bounding_box_arg(
100 "", "invert", "inverts the specified bounding box", false);
101 cmd.add(invert_bounding_box_arg);
102 TCLAP::ValueArg<double> zLargeArg(
103 "", "z-max", "largest allowed extent in z-dimension", false,
104 std::numeric_limits<double>::max(), "MAX_EXTENT_Z");
105 cmd.add(zLargeArg);
106 TCLAP::ValueArg<double> zSmallArg(
107 "", "z-min", "smallest allowed extent in z-dimension", false,
108 -1 * std::numeric_limits<double>::max(), "MIN_EXTENT_Z");
109 cmd.add(zSmallArg);
110 TCLAP::ValueArg<double> yLargeArg(
111 "", "y-max", "largest allowed extent in y-dimension", false,
112 std::numeric_limits<double>::max(), "MAX_EXTENT_Y");
113 cmd.add(yLargeArg);
114 TCLAP::ValueArg<double> ySmallArg(
115 "", "y-min", "smallest allowed extent in y-dimension", false,
116 -1 * std::numeric_limits<double>::max(), "MIN_EXTENT_Y");
117 cmd.add(ySmallArg);
118 TCLAP::ValueArg<double> xLargeArg(
119 "", "x-max", "largest allowed extent in x-dimension", false,
120 std::numeric_limits<double>::max(), "MAX_EXTENT_X");
121 cmd.add(xLargeArg);
122 TCLAP::ValueArg<double> xSmallArg(
123 "", "x-min", "smallest allowed extent in x-dimension", false,
124 -1 * std::numeric_limits<double>::max(), "MIN_EXTENT_X");
125 cmd.add(xSmallArg);
126
127 // Non-bounding-box params
128 TCLAP::SwitchArg zveArg("z", "zero-volume", "remove zero volume elements",
129 false);
130 cmd.add(zveArg);
131
132 std::vector<std::string> allowed_ele_types{
133 "point", "line", "tri", "quad", "hex", "prism", "tet", "pyramid"};
134 TCLAP::ValuesConstraint<std::string> allowedVals{allowed_ele_types};
135 TCLAP::MultiArg<std::string> eleTypeArg(
136 "t", "element-type", "element type to be removed", false, &allowedVals);
137 cmd.add(eleTypeArg);
138
139 // scalar array params
140 TCLAP::ValueArg<std::string> property_name_arg(
141 "n", "property-name", "name of property in the mesh", false,
142 "MaterialIDs", "PROPERTY_NAME");
143 cmd.add(property_name_arg);
144
145 TCLAP::MultiArg<int> property_arg(
146 "", "property-value", "value of selected property to be removed", false,
147 "PROPERTY_VALUE");
148 cmd.add(property_arg);
149
150 TCLAP::ValueArg<double> min_property_arg(
151 "", "min-value", "minimum value of range for selected property", false,
152 0, "MIN_RANGE");
153 cmd.add(min_property_arg);
154
155 TCLAP::ValueArg<double> max_property_arg(
156 "", "max-value", "maximum value of range for selected property", false,
157 0, "MAX_RANGE");
158 cmd.add(max_property_arg);
159
160 TCLAP::SwitchArg outside_property_arg(
161 "", "outside", "remove all elements outside the given property range");
162 cmd.add(outside_property_arg);
163
164 TCLAP::SwitchArg inside_property_arg(
165 "", "inside", "remove all elements inside the given property range");
166 cmd.add(inside_property_arg);
167
168 // I/O params
169 TCLAP::ValueArg<std::string> mesh_out(
170 "o", "mesh-output-file",
171 "Output (.vtu). The name of the file the mesh will be written to", true,
172 "", "OUTPUT_FILE");
173 cmd.add(mesh_out);
174 TCLAP::ValueArg<std::string> mesh_in(
175 "i", "mesh-input-file",
176 "Input (.vtu). The name of the file containing the input mesh", true,
177 "", "INPUT_FILE");
178 cmd.add(mesh_in);
179 auto log_level_arg = BaseLib::makeLogLevelArg();
180 cmd.add(log_level_arg);
181 cmd.parse(argc, argv);
182
183 BaseLib::MPI::Setup mpi_setup(argc, argv);
184 BaseLib::initOGSLogger(log_level_arg.getValue());
185
186 std::unique_ptr<MeshLib::Mesh const> mesh(
187 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
188 if (mesh == nullptr)
189 {
190 return EXIT_FAILURE;
191 }
192
193 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
194 mesh->getNumberOfElements());
195 MeshLib::ElementSearch searcher(*mesh);
196
197 // search elements IDs to be removed
198 if (zveArg.isSet())
199 {
200 INFO("{:d} zero volume elements found.", searcher.searchByContent());
201 }
202 if (eleTypeArg.isSet())
203 {
204 const std::vector<std::string> eleTypeNames = eleTypeArg.getValue();
205 for (const auto& typeName : eleTypeNames)
206 {
207 const MeshLib::MeshElemType type =
210 {
211 continue;
212 }
213 INFO("{:d} {:s} elements found.",
214 searcher.searchByElementType(type), typeName);
215 }
216 }
217
218 if (property_name_arg.isSet() || property_arg.isSet() ||
219 min_property_arg.isSet() || max_property_arg.isSet())
220 {
221 if ((property_arg.isSet() || min_property_arg.isSet() ||
222 max_property_arg.isSet()) &&
223 !property_name_arg.isSet())
224 {
225 ERR("Specify a property name for the value/range selected.");
226 return EXIT_FAILURE;
227 }
228
229 if (property_name_arg.isSet() &&
230 !((min_property_arg.isSet() && max_property_arg.isSet()) ||
231 property_arg.isSet()))
232 {
233 ERR("Specify a value or range ('-min-value' and '-max_value') for "
234 "the property selected.");
235 return EXIT_FAILURE;
236 }
237
238 // name + value
239 if (property_arg.isSet() && property_name_arg.isSet())
240 {
241 searchByPropertyValue(property_name_arg.getValue(),
242 property_arg.getValue(), searcher);
243 }
244
245 // name + range
246 if (property_name_arg.isSet() && min_property_arg.isSet() &&
247 max_property_arg.isSet())
248 {
249 if ((!outside_property_arg.isSet() &&
250 !inside_property_arg.isSet()) ||
251 (outside_property_arg.isSet() && inside_property_arg.isSet()))
252 {
253 ERR("Specify if the inside or the outside of the selected "
254 "range should be removed.");
255 return EXIT_FAILURE;
256 }
257
258 bool const outside = outside_property_arg.isSet();
260 property_name_arg.getValue(), min_property_arg.getValue(),
261 max_property_arg.getValue(), outside, searcher);
262 }
263 }
264
265 if (xSmallArg.isSet() || xLargeArg.isSet() || ySmallArg.isSet() ||
266 yLargeArg.isSet() || zSmallArg.isSet() || zLargeArg.isSet())
267 {
268 outputAABB(*mesh);
269 bool aabb_error(false);
270 if (xSmallArg.getValue() >= xLargeArg.getValue())
271 {
272 ERR("Minimum x-extent larger than maximum x-extent.");
273 aabb_error = true;
274 }
275 if (ySmallArg.getValue() >= yLargeArg.getValue())
276 {
277 ERR("Minimum y-extent larger than maximum y-extent.");
278 aabb_error = true;
279 }
280 if (zSmallArg.getValue() >= zLargeArg.getValue())
281 {
282 ERR("Minimum z-extent larger than maximum z-extent.");
283 aabb_error = true;
284 }
285 if (aabb_error)
286 {
287 return EXIT_FAILURE;
288 }
289
290 std::array<MathLib::Point3d, 2> extent(
291 {{MathLib::Point3d(std::array<double, 3>{{xSmallArg.getValue(),
292 ySmallArg.getValue(),
293 zSmallArg.getValue()}}),
294 MathLib::Point3d(std::array<double, 3>{
295 {xLargeArg.getValue(), yLargeArg.getValue(),
296 zLargeArg.getValue()}})}});
297 INFO("{:d} elements found.",
298 searcher.searchByBoundingBox(
299 GeoLib::AABB(extent.begin(), extent.end()),
300 invert_bounding_box_arg.getValue()));
301 }
302
303 // remove the elements and create a new mesh object.
304 std::unique_ptr<MeshLib::Mesh const> new_mesh(MeshToolsLib::removeElements(
305 *mesh, searcher.getSearchedElementIDs(), mesh->getName()));
306
307 if (new_mesh == nullptr)
308 {
309 return EXIT_FAILURE;
310 }
311
312 // write into a file
313 MeshLib::IO::writeMeshToFile(*new_mesh, mesh_out.getValue());
314
315 return EXIT_SUCCESS;
316}
Definition of the Element class.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of mesh-related Enumerations.
Definition of the MeshInformation class.
Definition of the Mesh class.
Definition of the Node class.
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:56
Element search class.
std::size_t searchByBoundingBox(GeoLib::AABB const &aabb, bool const invert=false)
const std::vector< std::size_t > & getSearchedElementIDs() const
return marked elements
std::size_t searchByElementType(MeshElemType eleType)
Marks all elements of the given element type.
std::size_t searchByPropertyValueRange(std::string const &property_name, PROPERTY_TYPE const min_property_value, PROPERTY_TYPE const max_property_value, bool outside_of)
std::size_t searchByContent(double eps=std::numeric_limits< double >::epsilon())
Marks all elements with a volume smaller than eps.
std::size_t searchByPropertyValue(std::string const &property_name, PROPERTY_TYPE const property_value)
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:105
static GeoLib::AABB getBoundingBox(const MeshLib::Mesh &mesh)
Returns the bounding box of the mesh.
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
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
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:48
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)
Definition of readMeshFromFile function.
int main(int argc, char *argv[])
void outputAABB(MeshLib::Mesh const &mesh)
void searchByPropertyValue(std::string const &property_name, std::vector< PROPERTY_TYPE > const &property_values, MeshLib::ElementSearch &searcher)
void searchByPropertyRange(std::string const &property_name, double const &min_value, double const &max_value, bool const &outside, MeshLib::ElementSearch &searcher)