OGS
IntegrateBoreholesIntoMesh.cpp File Reference
#include <tclap/CmdLine.h>
#include <iterator>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/Elements/Element.h"
#include "MeshLib/Elements/Line.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
#include "MeshLib/Utils/DuplicateMeshComponents.h"
Include dependency graph for IntegrateBoreholesIntoMesh.cpp:

Go to the source code of this file.

Functions

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)
int main (int argc, char *argv[])

Function Documentation

◆ getNodes()

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 at line 26 of file IntegrateBoreholesIntoMesh.cpp.

32{
33 std::vector<std::size_t> pnt_nodes;
34 for (auto node : nodes)
35 {
36 double const eps = std::numeric_limits<double>::epsilon();
37 if (std::abs((*node)[0] - pnt[0]) < eps &&
38 std::abs((*node)[1] - pnt[1]) < eps)
39 {
40 auto const& elems = mesh.getElementsConnectedToNode(*node);
41 for (auto e : elems)
42 {
43 if (mat_ids[e->getID()] >= mat_limits.first &&
44 mat_ids[e->getID()] <= mat_limits.second &&
45 (*node)[2] >= elevation_limits.first &&
46 (*node)[2] <= elevation_limits.second)
47 {
48 pnt_nodes.push_back(node->getID());
49 break;
50 }
51 }
52 }
53 }
54 if (pnt_nodes.size() < 2)
55 {
56 WARN("No nodes found for point {:d}...", pnt.getID());
57 }
58 else
59 {
60 // sort found nodes from top to bottom (required for BHE simulations)
61 std::sort(pnt_nodes.begin(), pnt_nodes.end(),
62 [nodes](std::size_t a, std::size_t b)
63 { return (*nodes[a])[2] > (*nodes[b])[2]; });
64 }
65 return pnt_nodes;
66}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34

References MeshLib::Mesh::getElementsConnectedToNode(), MathLib::Point3dWithID::getID(), and WARN().

Referenced by MeshLib::MeshElementGrid::MeshElementGrid(), MeshGeoToolsLib::MeshNodeSearcher::MeshNodeSearcher(), ProjectData::ProjectData(), main(), and MeshLib::setMeshSpaceDimension().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 68 of file IntegrateBoreholesIntoMesh.cpp.

69{
70 TCLAP::CmdLine cmd(
71 "Integrates line elements representing boreholes into a pre-existing "
72 "3D mesh. Corresponding nodes matching the (x,y)-coordinates given in "
73 "the gml-file are found in the mesh and connected from top to bottom "
74 "via line elements. Each borehole (i.e. all points at a given "
75 "(x,y)-location but at different depths) is assigned a unique material "
76 "ID. Vertical limits of boreholes can be specified via Material IDs "
77 "and/or elevation. Point not matching any mesh nodes or located outside"
78 " the mesh are ignored.\n\n"
79 "OpenGeoSys-6 software, version " +
81 ".\n"
82 "Copyright (c) 2012-2026, OpenGeoSys Community "
83 "(http://www.opengeosys.org)",
85
86 double const dmax = std::numeric_limits<double>::max();
87 TCLAP::ValueArg<double> max_elevation_arg(
88 "", "max-elevation",
89 "Maximum elevation for an integrated borehole, "
90 "(min = 0)",
91 false, 0, "MAX_ELEVATION");
92 cmd.add(max_elevation_arg);
93 TCLAP::ValueArg<double> min_elevation_arg(
94 "", "min-elevation",
95 "Minimum elevation for an integrated borehole, "
96 "(min = 0)",
97 false, 0, "MIN_ELEVATION");
98 cmd.add(min_elevation_arg);
99 TCLAP::ValueArg<int> max_id_arg(
100 "", "max-id", "Maximum MaterialID for an integrated borehole", false,
101 -1, "MAX_ID");
102 cmd.add(max_id_arg);
103 TCLAP::ValueArg<int> min_id_arg(
104 "", "min-id", "Minimum MaterialID for an integrated borehole", false,
105 -1, "MIN_ID");
106 cmd.add(min_id_arg);
107 TCLAP::ValueArg<std::string> geo_arg(
108 "g", "geo", "Input (.gml). Name of the geometry file", true, "",
109 "INPUT_FILE");
110 cmd.add(geo_arg);
111 TCLAP::ValueArg<std::string> output_arg(
112 "o", "output", "Output (.vtu). Name of the mesh file", true, "",
113 "OUTPUT_FILE");
114 cmd.add(output_arg);
115 TCLAP::ValueArg<std::string> input_arg(
116 "i", "input", "Input (.vtu). Name of the mesh file", true, "",
117 "INPUT_FILE");
118 cmd.add(input_arg);
119 auto log_level_arg = BaseLib::makeLogLevelArg();
120 cmd.add(log_level_arg);
121 cmd.parse(argc, argv);
122
123 BaseLib::MPI::Setup mpi_setup(argc, argv);
124 BaseLib::initOGSLogger(log_level_arg.getValue());
125
126 std::pair<int, int> mat_limits(0, std::numeric_limits<int>::max());
127 std::pair<double, double> elevation_limits(
128 std::numeric_limits<double>::lowest(), dmax);
129
130 if (min_id_arg.isSet() != max_id_arg.isSet())
131 {
132 ERR("If minimum MaterialID is set, maximum ID must be set, too (and "
133 "vice versa).");
134 return EXIT_FAILURE;
135 }
136 if (min_id_arg.isSet() && max_id_arg.isSet())
137 {
138 mat_limits =
139 std::make_pair(min_id_arg.getValue(), max_id_arg.getValue());
140 }
141 if (mat_limits.first > mat_limits.second)
142 {
143 std::swap(mat_limits.first, mat_limits.second);
144 }
145 if (min_id_arg.isSet() && (mat_limits.first < 0 || mat_limits.second < 0))
146 {
147 ERR("Specified MaterialIDs must have non-negative values.");
148 return EXIT_FAILURE;
149 }
150 if (min_elevation_arg.isSet() != max_elevation_arg.isSet())
151 {
152 ERR("If minimum elevation is set, maximum elevation must be set, too "
153 "(and vice versa).");
154 return EXIT_FAILURE;
155 }
156 if (min_elevation_arg.isSet() && max_elevation_arg.isSet())
157 {
158 elevation_limits = std::make_pair(min_elevation_arg.getValue(),
159 max_elevation_arg.getValue());
160 }
161 if (elevation_limits.first > elevation_limits.second)
162 {
163 std::swap(elevation_limits.first, elevation_limits.second);
164 }
165
166 std::string const& mesh_name = input_arg.getValue();
167 std::string const& output_name = output_arg.getValue();
168 std::string const& geo_name = geo_arg.getValue();
169
172 if (!xml_io.readFile(geo_name))
173 {
174 ERR("Failed to read geometry file `{:s}'.", geo_name);
175 return EXIT_FAILURE;
176 }
177 std::vector<GeoLib::Point*> const& points =
178 *geo.getPointVec(geo.getGeometryNames()[0]);
179
180 std::unique_ptr<MeshLib::Mesh> const mesh(
182 if (mesh == nullptr)
183 {
184 ERR("Failed to read input mesh file `{:s}'.", mesh_name);
185 return EXIT_FAILURE;
186 }
187 if (mesh->getDimension() != 3)
188 {
189 ERR("Method can only be applied to 3D meshes.");
190 return EXIT_FAILURE;
191 }
192
193 auto const& nodes = mesh->getNodes();
194 auto const& mat_ids = MeshLib::materialIDs(*mesh);
195 if (mat_ids == nullptr)
196 {
197 ERR("Mesh is required to have MaterialIDs");
198 return EXIT_FAILURE;
199 }
200
201 auto const& elems = mesh->getElements();
203 auto new_mat_ids = props.createNewPropertyVector<int>(
204 "MaterialIDs", MeshLib::MeshItemType::Cell);
205 std::copy(mat_ids->cbegin(), mat_ids->cend(),
206 std::back_inserter(*new_mat_ids));
207 int const max_id = *std::max_element(mat_ids->begin(), mat_ids->end());
208 std::vector<MeshLib::Node*> new_nodes = MeshLib::copyNodeVector(nodes);
209 std::size_t const n_points = points.size();
210 std::vector<MeshLib::Element*> new_elems =
211 MeshLib::copyElementVector(elems, new_nodes);
212
213 for (std::size_t i = 0; i < n_points; ++i)
214 {
215 std::vector<std::size_t> const& line_nodes = getNodes(
216 *points[i], nodes, *mat_ids, mat_limits, elevation_limits, *mesh);
217 std::size_t const n_line_nodes = line_nodes.size();
218 if (n_line_nodes < 2)
219 {
220 continue;
221 }
222 for (std::size_t j = 0; j < n_line_nodes - 1; ++j)
223 {
224 new_elems.push_back(new MeshLib::Line(
225 {new_nodes[line_nodes[j]], new_nodes[line_nodes[j + 1]]},
226 elems.size()));
227 new_mat_ids->push_back(max_id + i + 1);
228 }
229 }
230
231 MeshLib::Mesh const result("result", new_nodes, new_elems,
232 true /* compute_element_neighbors */, props);
233 MeshLib::IO::VtuInterface vtu(&result);
234 vtu.writeToFile(output_name);
235 return EXIT_SUCCESS;
236}
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)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Container class for geometric objects.
Definition GEOObjects.h:46
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
const std::vector< Point * > * getPointVec(const std::string &name) const
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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:258
TemplateElement< MeshLib::LineRule2 > Line
Definition Line.h:14
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)

References MeshLib::Cell, MeshLib::copyElementVector(), MeshLib::copyNodeVector(), MeshLib::Properties::createNewPropertyVector(), ERR(), GeoLib::GEOObjects::getGeometryNames(), getNodes(), GeoLib::GEOObjects::getPointVec(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), MeshLib::materialIDs(), GitInfoLib::GitInfo::ogs_version, GeoLib::IO::BoostXmlGmlInterface::readFile(), MeshLib::IO::readMeshFromFile(), and MeshLib::IO::VtuInterface::writeToFile().