OGS
ResetPropertiesInPolygonalRegion.cpp
Go to the documentation of this file.
1/*
2 * \file
3 * \brief Reset material properties in meshes in a polygonal region.
4 *
5 * \copyright
6 * Copyright (c) 2012-2025, OpenGeoSys Community (http://www.opengeosys.org)
7 * Distributed under a Modified BSD License.
8 * See accompanying file LICENSE.txt or
9 * http://www.opengeosys.org/project/license
10 *
11 */
12
13#include <tclap/CmdLine.h>
14
15#include <algorithm>
16#include <cstdlib>
17#include <vector>
18
20#include "BaseLib/Logging.h"
21#include "BaseLib/MPI.h"
23#include "GeoLib/GEOObjects.h"
24#include "GeoLib/Polygon.h"
25#include "InfoLib/GitInfo.h"
29#include "MeshLib/Mesh.h"
31
32int main(int argc, char* argv[])
33{
34 TCLAP::CmdLine cmd(
35 "Sets the property value of a mesh element to a given new value iff at "
36 "least one node of the element is within a polygonal region that is "
37 "given by a polygon. The documentation is available at "
38 "https://docs.opengeosys.org/docs/tools/model-preparation/"
39 "set-properties-in-polygonal-region.\n\n"
40 "OpenGeoSys-6 software, version " +
42 ".\n"
43 "Copyright (c) 2012-2025, OpenGeoSys Community "
44 "(http://www.opengeosys.org)",
46 TCLAP::ValueArg<std::string> mesh_out(
47 "o", "mesh-output-file",
48 "Output. The name of the file the mesh will be written to, format "
49 "depends on "
50 "the given file name extension",
51 true, "", "OUTPUT_FILE");
52 cmd.add(mesh_out);
53 TCLAP::ValueArg<std::string> polygon_name_arg(
54 "p", "polygon-name", "name of polygon in the geometry", true, "",
55 "string");
56 cmd.add(polygon_name_arg);
57 TCLAP::ValueArg<std::string> geometry_fname(
58 "g", "geometry",
59 "Input (.gli | .gml). The name of the file containing the input "
60 "geometry",
61 true, "", "INPUT_FILE");
62 cmd.add(geometry_fname);
63 TCLAP::SwitchArg any_of_arg(
64 "", "any_of",
65 "all nodes of an element has to be inside the polygon (default "
66 "behaviour without switch) or any node of an element has to be inside "
67 "(switch is given)",
68 false);
69 cmd.add(any_of_arg);
70 TCLAP::ValueArg<char> char_property_arg(
71 "c", "char-property-value", "new property value (data type char)",
72 false, 'A', "CHAR_PROPERTY_VALUE");
73 cmd.add(char_property_arg);
74 TCLAP::ValueArg<int> int_property_arg("i", "int-property-value",
75 "new property value (data type int)",
76 false, 0, "INT_PROPERTY_VALUE");
77 cmd.add(int_property_arg);
78 TCLAP::ValueArg<std::string> property_name_arg(
79 "n", "property-name", "name of property in the mesh", false,
80 "MaterialIDs", "PROPERTY_NAME");
81 cmd.add(property_name_arg);
82 TCLAP::ValueArg<int> restrict_arg(
83 "r", "restrict-to-MaterialID",
84 "Restrict resetting the property to the material id", false, -1,
85 "RESTRICT_MATERIAL_ID");
86 cmd.add(restrict_arg);
87 TCLAP::ValueArg<std::string> mesh_in(
88 "m", "mesh-input-file",
89 "Input (.vtu | .vtk | .msh). The name of the file containing the input "
90 "mesh",
91 true, "", "INPUT_FILE");
92 cmd.add(mesh_in);
93 TCLAP::ValueArg<std::string> gmsh_path_arg(
94 "", "gmsh-path", "Input (.msh). The path to the input binary", false,
95 "", "INPUT_FILE");
96 cmd.add(gmsh_path_arg);
97 auto log_level_arg = BaseLib::makeLogLevelArg();
98 cmd.add(log_level_arg);
99 cmd.parse(argc, argv);
100
101 BaseLib::MPI::Setup mpi_setup(argc, argv);
102 BaseLib::initOGSLogger(log_level_arg.getValue());
103
104 // *** read geometry
105 GeoLib::GEOObjects geometries;
106 FileIO::readGeometryFromFile(geometry_fname.getValue(), geometries,
107 gmsh_path_arg.getValue());
108
109 auto const geo_name = geometries.getGeometryNames()[0];
110
111 // *** check if the data is usable
112 // *** get vector of polylines
113 GeoLib::PolylineVec const* plys(geometries.getPolylineVecObj(geo_name));
114 if (!plys)
115 {
116 ERR("Could not get vector of polylines out of geometry '{:s}'.",
117 geo_name);
118 return EXIT_FAILURE;
119 }
120
121 // *** get polygon
122 GeoLib::Polyline const* ply(
123 plys->getElementByName(polygon_name_arg.getValue()));
124 if (!ply)
125 {
126 ERR("Polyline '{:s}' not found.", polygon_name_arg.getValue());
127 return EXIT_FAILURE;
128 }
129
130 // *** check if the polyline is closed (i.e. is a polygon)
131 if (!ply->isClosed())
132 {
133 ERR("Polyline '{:s}' is not closed, i.e. does not describe a region.",
134 polygon_name_arg.getValue());
135 return EXIT_FAILURE;
136 }
137
138 GeoLib::Polygon const polygon(*(ply));
139
140 // *** read mesh
141 auto mesh = std::unique_ptr<MeshLib::Mesh>(
142 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
143 if (!mesh)
144 {
145 // error message written already by readMeshFromFile
146 return EXIT_FAILURE;
147 }
148 std::string const& property_name(property_name_arg.getValue());
149
150 if (char_property_arg.isSet())
151 {
153 *mesh, polygon, property_name, char_property_arg.getValue(),
154 restrict_arg.getValue(), any_of_arg.getValue());
155 }
156
157 if (int_property_arg.isSet())
158 {
160 *mesh, polygon, property_name, int_property_arg.getValue(),
161 restrict_arg.getValue(), any_of_arg.getValue());
162 }
163
165
166 MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());
167
168 return EXIT_SUCCESS;
169}
Definition of the GEOObjects class.
Git information.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of the MeshInformation class.
Definition of the Mesh class.
Definition of the Polygon class.
int main(int argc, char *argv[])
Container class for geometric objects.
Definition GEOObjects.h:57
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
const PolylineVec * getPolylineVecObj(const std::string &name) const
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition Polyline.h:40
bool isClosed() const
Definition Polyline.cpp:119
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
static void writePropertyVectorInformation(const MeshLib::Mesh &mesh)
writes out property vector information
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
void readGeometryFromFile(std::string const &fname, GeoLib::GEOObjects &geo_objs, std::string const &gmsh_path)
GITINFOLIB_EXPORT const std::string ogs_version
void resetMeshElementProperty(MeshLib::Mesh &mesh, GeoLib::Polygon const &polygon, std::string const &property_name, PT new_property_value, int restrict_to_material_id, bool const any_of)
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)
Definition of readMeshFromFile function.