OGS
IntegrateBoreholesIntoMesh.cpp
Go to the documentation of this file.
1
10#include <iterator>
11#include <limits>
12#include <memory>
13#include <string>
14#include <vector>
15
16// ThirdParty
17#include <tclap/CmdLine.h>
18
19#include "BaseLib/MPI.h"
20#include "GeoLib/GEOObjects.h"
22#include "InfoLib/GitInfo.h"
27#include "MeshLib/Mesh.h"
28#include "MeshLib/Node.h"
30
31std::vector<std::size_t> getNodes(
32 GeoLib::Point const& pnt, std::vector<MeshLib::Node*> const& nodes,
33 MeshLib::PropertyVector<int> const& mat_ids,
34 std::pair<int, int> const& mat_limits,
35 std::pair<double, double> const& elevation_limits,
36 MeshLib::Mesh const& mesh)
37{
38 std::vector<std::size_t> pnt_nodes;
39 for (auto node : nodes)
40 {
41 double const eps = std::numeric_limits<double>::epsilon();
42 if (std::abs((*node)[0] - pnt[0]) < eps &&
43 std::abs((*node)[1] - pnt[1]) < eps)
44 {
45 auto const& elems = mesh.getElementsConnectedToNode(*node);
46 for (auto e : elems)
47 {
48 if (mat_ids[e->getID()] >= mat_limits.first &&
49 mat_ids[e->getID()] <= mat_limits.second &&
50 (*node)[2] >= elevation_limits.first &&
51 (*node)[2] <= elevation_limits.second)
52 {
53 pnt_nodes.push_back(node->getID());
54 break;
55 }
56 }
57 }
58 }
59 if (pnt_nodes.size() < 2)
60 {
61 WARN("No nodes found for point {:d}...", pnt.getID());
62 }
63 else
64 {
65 // sort found nodes from top to bottom (required for BHE simulations)
66 std::sort(pnt_nodes.begin(), pnt_nodes.end(),
67 [nodes](std::size_t a, std::size_t b)
68 { return (*nodes[a])[2] > (*nodes[b])[2]; });
69 }
70 return pnt_nodes;
71}
72
73int main(int argc, char* argv[])
74{
75 TCLAP::CmdLine cmd(
76 "Integrates line elements representing boreholes into a pre-existing "
77 "3D mesh. Corresponding nodes matching the (x,y)-coordinates given in "
78 "the gml-file are found in the mesh and connected from top to bottom "
79 "via line elements. Each borehole (i.e. all points at a given "
80 "(x,y)-location but at different depths) is assigned a unique material "
81 "ID. Vertical limits of boreholes can be specified via Material IDs "
82 "and/or elevation. Point not matching any mesh nodes or located outside"
83 " the mesh are ignored.\n\n"
84 "OpenGeoSys-6 software, version " +
86 ".\n"
87 "Copyright (c) 2012-2024, OpenGeoSys Community "
88 "(http://www.opengeosys.org)",
90
91 double const dmax = std::numeric_limits<double>::max();
92 TCLAP::ValueArg<double> max_elevation_arg(
93 "", "max-elevation", "Maximum elevation for an integrated borehole",
94 false, 0, "a number");
95 cmd.add(max_elevation_arg);
96 TCLAP::ValueArg<double> min_elevation_arg(
97 "", "min-elevation", "Minimum elevation for an integrated borehole",
98 false, 0, "a number");
99 cmd.add(min_elevation_arg);
100 TCLAP::ValueArg<int> max_id_arg(
101 "", "max-id", "Maximum MaterialID for an integrated borehole", false,
102 -1, "a number");
103 cmd.add(max_id_arg);
104 TCLAP::ValueArg<int> min_id_arg(
105 "", "min-id", "Minimum MaterialID for an integrated borehole", false,
106 -1, "a number");
107 cmd.add(min_id_arg);
108 TCLAP::ValueArg<std::string> geo_arg("g", "geo",
109 "Name of the geometry file (*.gml)",
110 true, "", "geometry file name");
111 cmd.add(geo_arg);
112 TCLAP::ValueArg<std::string> output_arg("o", "output",
113 "Name of the output mesh (*.vtu)",
114 true, "", "output file name");
115 cmd.add(output_arg);
116 TCLAP::ValueArg<std::string> input_arg("i", "input",
117 "Name of the input mesh (*.vtu)",
118 true, "", "input file name");
119 cmd.add(input_arg);
120 cmd.parse(argc, argv);
121
122 BaseLib::MPI::Setup mpi_setup(argc, argv);
123
124 std::pair<int, int> mat_limits(0, std::numeric_limits<int>::max());
125 std::pair<double, double> elevation_limits(
126 std::numeric_limits<double>::lowest(), dmax);
127
128 if (min_id_arg.isSet() != max_id_arg.isSet())
129 {
130 ERR("If minimum MaterialID is set, maximum ID must be set, too (and "
131 "vice versa).");
132 return EXIT_FAILURE;
133 }
134 if (min_id_arg.isSet() && max_id_arg.isSet())
135 {
136 mat_limits =
137 std::make_pair(min_id_arg.getValue(), max_id_arg.getValue());
138 }
139 if (mat_limits.first > mat_limits.second)
140 {
141 std::swap(mat_limits.first, mat_limits.second);
142 }
143 if (min_id_arg.isSet() && (mat_limits.first < 0 || mat_limits.second < 0))
144 {
145 ERR("Specified MaterialIDs must have non-negative values.");
146 return EXIT_FAILURE;
147 }
148 if (min_elevation_arg.isSet() != max_elevation_arg.isSet())
149 {
150 ERR("If minimum elevation is set, maximum elevation must be set, too "
151 "(and vice versa).");
152 return EXIT_FAILURE;
153 }
154 if (min_elevation_arg.isSet() && max_elevation_arg.isSet())
155 {
156 elevation_limits = std::make_pair(min_elevation_arg.getValue(),
157 max_elevation_arg.getValue());
158 }
159 if (elevation_limits.first > elevation_limits.second)
160 {
161 std::swap(elevation_limits.first, elevation_limits.second);
162 }
163
164 std::string const& mesh_name = input_arg.getValue();
165 std::string const& output_name = output_arg.getValue();
166 std::string const& geo_name = geo_arg.getValue();
167
170 if (!xml_io.readFile(geo_name))
171 {
172 ERR("Failed to read geometry file `{:s}'.", geo_name);
173 return EXIT_FAILURE;
174 }
175 std::vector<GeoLib::Point*> const& points =
176 *geo.getPointVec(geo.getGeometryNames()[0]);
177
178 std::unique_ptr<MeshLib::Mesh> const mesh(
180 if (mesh == nullptr)
181 {
182 ERR("Failed to read input mesh file `{:s}'.", mesh_name);
183 return EXIT_FAILURE;
184 }
185 if (mesh->getDimension() != 3)
186 {
187 ERR("Method can only be applied to 3D meshes.");
188 return EXIT_FAILURE;
189 }
190
191 auto const& nodes = mesh->getNodes();
192 auto const& mat_ids = MeshLib::materialIDs(*mesh);
193 if (mat_ids == nullptr)
194 {
195 ERR("Mesh is required to have MaterialIDs");
196 return EXIT_FAILURE;
197 }
198
199 auto const& elems = mesh->getElements();
201 auto new_mat_ids = props.createNewPropertyVector<int>(
202 "MaterialIDs", MeshLib::MeshItemType::Cell);
203 std::copy(mat_ids->cbegin(), mat_ids->cend(),
204 std::back_inserter(*new_mat_ids));
205 int const max_id = *std::max_element(mat_ids->begin(), mat_ids->end());
206 std::vector<MeshLib::Node*> new_nodes = MeshLib::copyNodeVector(nodes);
207 std::size_t const n_points = points.size();
208 std::vector<MeshLib::Element*> new_elems =
209 MeshLib::copyElementVector(elems, new_nodes);
210
211 for (std::size_t i = 0; i < n_points; ++i)
212 {
213 std::vector<std::size_t> const& line_nodes = getNodes(
214 *points[i], nodes, *mat_ids, mat_limits, elevation_limits, *mesh);
215 std::size_t const n_line_nodes = line_nodes.size();
216 if (n_line_nodes < 2)
217 {
218 continue;
219 }
220 for (std::size_t j = 0; j < n_line_nodes - 1; ++j)
221 {
222 new_elems.push_back(new MeshLib::Line(
223 {new_nodes[line_nodes[j]], new_nodes[line_nodes[j + 1]]},
224 elems.size()));
225 new_mat_ids->push_back(max_id + i + 1);
226 }
227 }
228
229 MeshLib::Mesh const result("result", new_nodes, new_elems,
230 true /* compute_element_neighbors */, props);
231 MeshLib::IO::VtuInterface vtu(&result);
232 vtu.writeToFile(output_name);
233 return EXIT_SUCCESS;
234}
Definition of the BoostXmlGmlInterface class.
Definition of Duplicate functions.
Definition of the Element class.
Definition of the GEOObjects class.
Git information.
int main(int argc, char *argv[])
std::vector< std::size_t > getNodes(GeoLib::Point const &pnt, std::vector< MeshLib::Node * > const &nodes, MeshLib::PropertyVector< int > const &mat_ids, std::pair< int, int > const &mat_limits, std::pair< double, double > const &elevation_limits, MeshLib::Mesh const &mesh)
Definition of the Line class.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the Mesh class.
Definition of the Node class.
Implementation of the VtuInterface class.
Container class for geometric objects.
Definition GEOObjects.h:57
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
const std::vector< Point * > * getPointVec(const std::string &name) const
bool readFile(const std::string &fname) override
Reads an xml-file containing OGS geometry.
std::size_t getID() const
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition Mesh.cpp:256
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition Properties.h:36
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:268
std::vector< Element * > copyElementVector(std::vector< Element * > const &elements, std::vector< Node * > const &new_nodes, std::vector< std::size_t > const *const node_id_map)
Definition of readMeshFromFile function.