OGS
GMSHInterface.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <fstream>
7#include <memory>
8#include <vector>
9
15#include "BaseLib/Algorithm.h"
16#include "BaseLib/FileTools.h"
17#include "BaseLib/Logging.h"
19#include "GeoLib/GEOObjects.h"
20#include "GeoLib/Polygon.h"
23#include "GeoLib/Station.h"
24#include "InfoLib/GitInfo.h"
25
26namespace FileIO
27{
28namespace GMSH
29{
30static std::ostream& operator<<(std::ostream& os,
31 std::vector<GMSHPoint*> const& points)
32{
33 for (auto const* const point : points)
34 {
35 if (point)
36 {
37 os << *point << "\n";
38 }
39 }
40 return os;
41}
42
44 GeoLib::GEOObjects& geo_objs, bool /*include_stations_as_constraints*/,
45 GMSH::MeshDensityAlgorithm const mesh_density_algorithm,
46 double const pnt_density, double const station_density,
47 std::size_t const max_pnts_per_leaf,
48 std::vector<std::string> const& selected_geometries, bool const rotate,
49 bool const keep_preprocessed_geometry)
50 : _geo_objs(geo_objs),
51 _selected_geometries(selected_geometries),
52 _rotate(rotate),
53 _keep_preprocessed_geometry(keep_preprocessed_geometry)
54{
55 switch (mesh_density_algorithm)
56 {
59 std::make_unique<GMSH::GMSHFixedMeshDensity>(pnt_density);
60 break;
63 std::make_unique<GMSH::GMSHAdaptiveMeshDensity>(
64 pnt_density, station_density, max_pnts_per_leaf);
65 break;
66 }
67}
68
70{
72 for (auto const* polygon_tree : _polygon_tree_list)
73 {
74 delete polygon_tree;
75 }
76}
77
79{
80 out << "// GMSH input file created by OpenGeoSys "
82 out << "\n\n";
83
84 return writeGMSHInputFile(out) <= 0;
85}
86
88{
89 DBUG("GMSHInterface::writeGMSHInputFile(): get data from GEOObjects.");
90
91 if (_selected_geometries.empty())
92 {
93 return 1;
94 }
95
96 // *** get and merge data from _geo_objs
97 if (_selected_geometries.size() > 1)
98 {
99 _gmsh_geo_name = "GMSHGeometry";
100 if (_geo_objs.mergeGeometries(_selected_geometries, _gmsh_geo_name))
101 {
102 return 2;
103 }
104 }
105 else
106 {
109 }
110
111 auto* merged_pnts(const_cast<std::vector<GeoLib::Point*>*>(
112 _geo_objs.getPointVec(_gmsh_geo_name)));
113 if (!merged_pnts)
114 {
115 ERR("GMSHInterface::writeGMSHInputFile(): Did not found any points.");
116 return 2;
117 }
118
119 if (_rotate)
120 {
121 // Rotate points to the x-y-plane.
123 // Compute inverse rotation matrix to reverse the rotation later on.
124 _inverse_rot_mat.transposeInPlace();
125 }
126 else
127 {
128 // project data on the x-y-plane
129 _inverse_rot_mat = Eigen::Matrix3d::Identity();
130 for (auto pnt : *merged_pnts)
131 {
132 (*pnt)[2] = 0.0;
133 }
134 }
135
136 std::vector<GeoLib::Polyline*> const* merged_plys(
137 _geo_objs.getPolylineVec(_gmsh_geo_name));
138 DBUG("GMSHInterface::writeGMSHInputFile(): Obtained data.");
139
140 if (!merged_plys)
141 {
142 ERR("GMSHInterface::writeGMSHInputFile(): Did not find any polylines.");
143 return 2;
144 }
145
146 // *** compute and insert all intersection points between polylines
147 GeoLib::PointVec& pnt_vec(*const_cast<GeoLib::PointVec*>(
148 _geo_objs.getPointVecObj(_gmsh_geo_name)));
150 pnt_vec, *(const_cast<std::vector<GeoLib::Polyline*>*>(merged_plys)));
151
152 std::vector<GeoLib::Polyline*> polygons;
153 // for each closed polyline add a PolygonWithSegmentMarker object into
154 // polygons
155 for (auto polyline : *merged_plys)
156 {
157 if (!polyline->isClosed())
158 {
159 continue;
160 }
161 polygons.push_back(new GeoLib::PolygonWithSegmentMarker(*polyline));
162 }
163 if (polygons.empty())
164 {
165 OGS_FATAL("GMSHInterface::writeGMSHInputFile(): no polygons found.");
166 }
167 // let the polygon memory management be done by GEOObjects
168 _geo_objs.appendPolylineVec(polygons, _gmsh_geo_name);
169 // create for each polygon a PolygonTree
170 std::transform(
171 polygons.begin(), polygons.end(),
172 std::back_inserter(_polygon_tree_list),
173 [this](auto const& polygon)
174 {
175 return new GMSH::GMSHPolygonTree(
176 dynamic_cast<GeoLib::PolygonWithSegmentMarker*>(polygon),
177 nullptr, _geo_objs, _gmsh_geo_name, *_mesh_density_strategy);
178 });
179 DBUG(
180 "GMSHInterface::writeGMSHInputFile(): Computed topological hierarchy - "
181 "detected {:d} polygons.",
182 _polygon_tree_list.size());
183 // compute topological hierarchy of polygons
185 DBUG(
186 "GMSHInterface::writeGMSHInputFile(): Computed topological hierarchy - "
187 "calculated {:d} polygon trees.",
188 _polygon_tree_list.size());
189
190 // *** Mark in each polygon tree the segments shared by two polygons.
191 for (auto* polygon_tree : _polygon_tree_list)
192 {
193 polygon_tree->markSharedSegments();
194 }
195
196 // *** insert stations and polylines (except polygons) in the appropriate
197 // object of
198 // class GMSHPolygonTree
199 // *** insert stations
200 std::vector<GeoLib::Point*> gmsh_stations{};
201 for (auto const& geometry_name : _selected_geometries)
202 {
203 auto const* stations(_geo_objs.getStationVec(geometry_name));
204 if (stations)
205 {
206 for (auto* station : *stations)
207 {
208 bool found(false);
209 for (auto it(_polygon_tree_list.begin());
210 it != _polygon_tree_list.end() && !found;
211 ++it)
212 {
213 gmsh_stations.emplace_back(new GeoLib::Station(
214 *static_cast<GeoLib::Station*>(station)));
215 if ((*it)->insertStation(gmsh_stations.back()))
216 {
217 found = true;
218 }
219 }
220 }
221 }
222 }
223 std::string gmsh_stations_name(_gmsh_geo_name + "-Stations");
224 if (!gmsh_stations.empty())
225 {
226 _geo_objs.addStationVec(std::move(gmsh_stations), gmsh_stations_name);
227 }
228
229 // *** insert polylines
230 for (auto polyline : *merged_plys)
231 {
232 if (!polyline->isClosed())
233 {
234 for (auto* polygon_tree : _polygon_tree_list)
235 {
236 auto polyline_with_segment_marker =
238 polygon_tree->insertPolyline(polyline_with_segment_marker);
239 }
240 }
241 }
242
243 // *** init mesh density strategies
244 for (auto& polygon_tree : _polygon_tree_list)
245 {
246 polygon_tree->initMeshDensityStrategy();
247 }
248
249 // *** create GMSH data structures
250 const std::size_t n_merged_pnts(merged_pnts->size());
251 _gmsh_pnts.resize(n_merged_pnts);
252 for (std::size_t k(0); k < n_merged_pnts; k++)
253 {
254 _gmsh_pnts[k] = nullptr;
255 }
256 for (auto& polygon_tree : _polygon_tree_list)
257 {
258 polygon_tree->createGMSHPoints(_gmsh_pnts);
259 }
260
261 std::stringstream error_messages;
262 error_messages.precision(std::numeric_limits<double>::max_digits10);
263 for (std::size_t k = 0; k < _gmsh_pnts.size(); ++k)
264 {
265 if (_gmsh_pnts[k] == nullptr)
266 {
267 error_messages
268 << "The point at (" << *(*merged_pnts)[k]
269 << ") is not part of a polyline, and won't be used in the "
270 "meshing as a constraint. If you want to include it in the "
271 "mesh please create a observation/measurement station for "
272 "the point and include it additional in the meshing "
273 "process.\n";
274 }
275 }
276 auto const error_message = error_messages.str();
277 if (!error_message.empty())
278 {
279 OGS_FATAL("{}", error_message);
280 }
281
282 // *** finally write data :-)
284 out << _gmsh_pnts;
285
286 std::size_t pnt_id_offset(_gmsh_pnts.size());
287 for (auto* polygon_tree : _polygon_tree_list)
288 {
289 polygon_tree->writeLineLoop(_n_lines, _n_plane_sfc, out,
291 polygon_tree->writeSubPolygonsAsLineConstraints(_n_lines,
292 _n_plane_sfc - 1, out);
293 polygon_tree->writeLineConstraints(_n_lines, _n_plane_sfc - 1, out);
294 polygon_tree->writeStations(pnt_id_offset, _n_plane_sfc - 1, out);
295 polygon_tree->writeAdditionalPointData(pnt_id_offset, _n_plane_sfc - 1,
296 out);
297 }
298
300 {
301 _geo_objs.removeSurfaceVec(_gmsh_geo_name);
302 _geo_objs.removePolylineVec(_gmsh_geo_name);
303 _geo_objs.removePointVec(_gmsh_geo_name);
304 _geo_objs.removeStationVec(gmsh_stations_name);
305 }
306
307 return 0;
308}
309
310} // end namespace GMSH
311} // end namespace FileIO
#define OGS_FATAL(...)
Definition Error.h:10
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::ostringstream out
The stream to write to.
Definition Writer.h:36
GMSHInterface(GeoLib::GEOObjects &geo_objs, bool include_stations_as_constraints, GMSH::MeshDensityAlgorithm mesh_density_algorithm, double pnt_density, double station_density, std::size_t max_pnts_per_leaf, std::vector< std::string > const &selected_geometries, bool rotate, bool keep_preprocessed_geometry)
std::unique_ptr< GMSH::GMSHMeshDensityStrategy > _mesh_density_strategy
Eigen::Matrix3d _inverse_rot_mat
std::vector< GMSH::GMSHPoint * > _gmsh_pnts
int writeGMSHInputFile(std::ostream &out)
std::vector< std::string > const & _selected_geometries
GeoLib::GEOObjects & _geo_objs
bool write() override
Writes the object to the internal stream. This method must be implemented by a subclass....
std::list< GMSH::GMSHPolygonTree * > _polygon_tree_list
Container class for geometric objects.
Definition GEOObjects.h:46
This class manages pointers to Points in a std::vector along with a name. It also handles the deletio...
Definition PointVec.h:25
A Station (observation site) is basically a Point with some additional information.
Definition Station.h:26
void cleanupVectorElements(std::vector< T * > &items)
Definition Algorithm.h:274
@ AdaptiveMeshDensity
computing the mesh density employing a QuadTree
@ FixedMeshDensity
set the parameter with a fixed value
static std::ostream & operator<<(std::ostream &os, std::vector< GMSHPoint * > const &points)
void createPolygonTrees(std::list< POLYGONTREETYPE * > &list_of_simple_polygon_hierarchies)
void computeAndInsertAllIntersectionPoints(GeoLib::PointVec &pnt_vec, std::vector< GeoLib::Polyline * > &plys)
void rotatePoints(Eigen::Matrix3d const &rot_mat, InputIterator pnts_begin, InputIterator pnts_end)
Eigen::Matrix3d rotatePointsToXY(InputIterator1 p_pnts_begin, InputIterator1 p_pnts_end, InputIterator2 r_pnts_begin, InputIterator2 r_pnts_end)
GITINFOLIB_EXPORT const std::string ogs_version