OGS
CreateBoundaryConditionsAlongPolylines.cpp
Go to the documentation of this file.
1/*
2 * \file
3 * \date 2014-09-30
4 * \brief Create BoundaryConditions from a polylines.
5 *
6 * \copyright
7 * Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
8 * Distributed under a Modified BSD License.
9 * See accompanying file LICENSE.txt or
10 * http://www.opengeosys.org/project/license
11 */
12
13#include <tclap/CmdLine.h>
14
15#include <fstream>
16#include <map>
17#include <string>
18#include <vector>
19
22#include "BaseLib/FileTools.h"
23#include "BaseLib/MPI.h"
24#include "GeoLib/GEOObjects.h"
25#include "GeoLib/Point.h"
26#include "InfoLib/GitInfo.h"
30#include "MeshLib/Mesh.h"
31#include "MeshLib/Node.h"
33
34void convertMeshNodesToGeometry(std::vector<MeshLib::Node*> const& nodes,
35 std::vector<std::size_t> const& node_ids,
36 std::string& geo_name,
37 GeoLib::GEOObjects& geometry_sets)
38{
39 // copy data
40 std::vector<GeoLib::Point*> pnts;
42 std::size_t cnt(0);
43 for (std::size_t id : node_ids)
44 {
45 pnts.push_back(new GeoLib::Point(*(nodes[id]), cnt));
46 pnt_names[geo_name + "-PNT-" + std::to_string(cnt)] = cnt;
47 cnt++;
48 }
49
50 // create data structures for geometry
51 geometry_sets.addPointVec(std::move(pnts), geo_name, std::move(pnt_names));
52}
53
54void writeGroundwaterFlowPointBC(std::ostream& bc_out,
55 std::string const& pnt_name, double head_value)
56{
57 bc_out << "#BOUNDARY_CONDITION\n";
58 bc_out << " $PCS_TYPE\n";
59 bc_out << " GROUNDWATER_FLOW\n";
60 bc_out << " $PRIMARY_VARIABLE\n";
61 bc_out << " HEAD\n";
62 bc_out << " $GEO_TYPE\n";
63 bc_out << " POINT " << pnt_name << "\n";
64 bc_out << " $DIS_TYPE\n";
65 bc_out << " CONSTANT " << head_value << "\n";
66}
67
68void writeLiquidFlowPointBC(std::ostream& bc_out, std::string const& pnt_name)
69{
70 bc_out << "#BOUNDARY_CONDITION\n";
71 bc_out << " $PCS_TYPE\n";
72 bc_out << " LIQUID_FLOW\n";
73 bc_out << " $PRIMARY_VARIABLE\n";
74 bc_out << " PRESSURE1\n";
75 bc_out << " $GEO_TYPE\n";
76 bc_out << " POINT " << pnt_name << "\n";
77 bc_out << " $DIS_TYPE\n";
78 bc_out << " CONSTANT 0.0\n";
79}
80
81// geometry_sets contains the geometric points the boundary conditions will be
82// set on, geo_name is the name the geometry can be accessed with, out_fname is
83// the base file name the gli and bc as well as the gml file will be written to.
85 std::string const& geo_name,
86 std::string const& out_fname,
87 std::string const& bc_type, bool const write_gml)
88{
89 if (write_gml)
90 {
91 INFO("write points to '{:s}.gml'.", geo_name);
92 FileIO::writeGeometryToFile(geo_name, geometry_sets,
93 out_fname + ".gml");
94 }
95 FileIO::writeGeometryToFile(geo_name, geometry_sets, out_fname + ".gli");
96
97 bool liquid_flow(false);
98 if (bc_type == "LIQUID_FLOW")
99 {
100 liquid_flow = true;
101 }
102
103 GeoLib::PointVec const* pnt_vec_objs(
104 geometry_sets.getPointVecObj(geo_name));
105 auto const& pnts(pnt_vec_objs->getVector());
106 std::ofstream bc_out(out_fname + ".bc");
107 for (std::size_t k(0); k < pnts.size(); k++)
108 {
109 std::string const& pnt_name(pnt_vec_objs->getItemNameByID(k));
110 if (!pnt_name.empty())
111 {
112 if (liquid_flow)
113 {
114 writeLiquidFlowPointBC(bc_out, pnt_name);
115 }
116 else
117 {
118 writeGroundwaterFlowPointBC(bc_out, pnt_name, (*pnts[k])[2]);
119 }
120 }
121 }
122 bc_out << "#STOP\n";
123 bc_out.close();
124}
125
126int main(int argc, char* argv[])
127{
128 TCLAP::CmdLine cmd(
129 "Creates boundary conditions for mesh nodes along polylines."
130 "The documentation is available at "
131 "https://docs.opengeosys.org/docs/tools/model-preparation/"
132 "create-boundary-conditions-along-a-polyline.\n\n"
133 "OpenGeoSys-6 software, version " +
135 ".\n"
136 "Copyright (c) 2012-2024, OpenGeoSys Community "
137 "(http://www.opengeosys.org)",
139 TCLAP::SwitchArg gml_arg("", "gml", "Write found nodes to gml file.");
140 cmd.add(gml_arg);
141
142 TCLAP::ValueArg<std::string> output_base_fname(
143 "o", "output-base-file-name",
144 "the base name of the file the output (geometry (gli) and boundary "
145 "condition (bc)) will be written to",
146 true, "", "file name");
147 cmd.add(output_base_fname);
148
149 TCLAP::ValueArg<std::string> bc_type(
150 "t", "type",
151 "the process type the boundary condition will be written for currently "
152 "LIQUID_FLOW (primary variable PRESSURE1) and GROUNDWATER_FLOW "
153 "(primary variable HEAD, default) are supported",
154 true, "",
155 "process type as string (LIQUID_FLOW or GROUNDWATER_FLOW (default))");
156 cmd.add(bc_type);
157
158 TCLAP::ValueArg<double> search_length_arg(
159 "s", "search-length",
160 "The size of the search length. The default value is "
161 "std::numeric_limits<double>::epsilon()",
162 false, std::numeric_limits<double>::epsilon(), "floating point number");
163 cmd.add(search_length_arg);
164
165 TCLAP::ValueArg<std::string> geometry_fname(
166 "i", "input-geometry",
167 "the name of the file containing the input geometry", true, "",
168 "file name");
169 cmd.add(geometry_fname);
170
171 TCLAP::ValueArg<std::string> mesh_arg(
172 "m", "mesh-file", "the name of the file containing the mesh", true, "",
173 "file name");
174 cmd.add(mesh_arg);
175
176 TCLAP::ValueArg<std::string> gmsh_path_arg("g", "gmsh-path",
177 "the path to the gmsh binary",
178 false, "", "path as string");
179 cmd.add(gmsh_path_arg);
180
181 cmd.parse(argc, argv);
182
183 BaseLib::MPI::Setup mpi_setup(argc, argv);
184
185 // *** read mesh
186 INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
187 std::unique_ptr<MeshLib::Mesh> subsurface_mesh(
188 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
189 INFO("done.");
190 INFO("Extracting top surface of mesh '{:s}' ... ", mesh_arg.getValue());
191 Eigen::Vector3d const dir({0, 0, -1});
192 double const angle(90);
193 std::unique_ptr<MeshLib::Mesh> surface_mesh(
195 dir, angle));
196 INFO("done.");
197 subsurface_mesh.reset(nullptr);
198
199 // *** read geometry
200 GeoLib::GEOObjects geometries;
201 FileIO::readGeometryFromFile(geometry_fname.getValue(), geometries,
202 gmsh_path_arg.getValue());
203
204 auto const geo_name = geometries.getGeometryNames()[0];
205
206 // *** check if the data is usable
207 // *** get vector of polylines
208 std::vector<GeoLib::Polyline*> const* plys(
209 geometries.getPolylineVec(geo_name));
210 if (!plys)
211 {
212 ERR("Could not get vector of polylines out of geometry '{:s}'.",
213 geo_name);
214 return EXIT_FAILURE;
215 }
216
217 auto search_length_strategy =
218 std::make_unique<MeshGeoToolsLib::SearchLength>();
219 if (search_length_arg.isSet())
220 {
221 search_length_strategy.reset(
222 new MeshGeoToolsLib::SearchLength(search_length_arg.getValue()));
223 }
224
225 GeoLib::GEOObjects geometry_sets;
227 *surface_mesh, std::move(search_length_strategy),
229 for (std::size_t k(0); k < plys->size(); k++)
230 {
231 auto const& ids = mesh_searcher.getMeshNodeIDs(*((*plys)[k]));
232 if (ids.empty())
233 {
234 continue;
235 }
236 std::string polyline_name("Polyline-" + std::to_string(k));
237 convertMeshNodesToGeometry(surface_mesh->getNodes(), ids, polyline_name,
238 geometry_sets);
239 }
240
241 // merge all together
242 auto const geo_names = geometry_sets.getGeometryNames();
243 if (geo_names.empty())
244 {
245 ERR("Did not find mesh nodes along polylines.");
246 return EXIT_FAILURE;
247 }
248
249 std::string merge_name("AllMeshNodesAlongPolylines");
250 if (geometry_sets.mergeGeometries(geo_names, merge_name) == 2)
251 {
252 merge_name = geo_names[0];
253 }
254
255 GeoLib::PointVec const* pnt_vec(geometry_sets.getPointVecObj(merge_name));
256 auto const& merged_pnts(pnt_vec->getVector());
257
258 std::vector<GeoLib::Point> pnts_with_id;
259 const std::size_t n_merged_pnts(merged_pnts.size());
260 for (std::size_t k(0); k < n_merged_pnts; ++k)
261 {
262 pnts_with_id.emplace_back(*(merged_pnts[k]), k);
263 }
264
265 std::sort(pnts_with_id.begin(), pnts_with_id.end(),
266 [](GeoLib::Point const& p0, GeoLib::Point const& p1)
267 { return p0 < p1; });
268
269 double const eps(std::numeric_limits<double>::epsilon());
270 std::vector<GeoLib::Point*> surface_pnts;
271 GeoLib::PointVec::NameIdMap name_id_map;
272
273 // insert first point
274 surface_pnts.push_back(
275 new GeoLib::Point(pnts_with_id[0], surface_pnts.size()));
276 {
277 std::string element_name;
278 pnt_vec->getNameOfElementByID(0, element_name);
279 name_id_map[element_name] = 0;
280 }
281 for (std::size_t k(1); k < n_merged_pnts; ++k)
282 {
283 const GeoLib::Point& p0(pnts_with_id[k - 1]);
284 const GeoLib::Point& p1(pnts_with_id[k]);
285 if (std::abs(p0[0] - p1[0]) > eps || std::abs(p0[1] - p1[1]) > eps)
286 {
287 surface_pnts.push_back(
288 new GeoLib::Point(pnts_with_id[k], surface_pnts.size()));
289 std::string element_name;
290 pnt_vec->getNameOfElementByID(k, element_name);
291 name_id_map[element_name] = surface_pnts.size() - 1;
292 }
293 }
294
295 std::string surface_name(BaseLib::dropFileExtension(mesh_arg.getValue()) +
296 "-MeshNodesAlongPolylines");
297 geometry_sets.addPointVec(std::move(surface_pnts), surface_name,
298 std::move(name_id_map), 1e-6);
299
300 // write the BCs and the merged geometry set to file
301 std::string const base_fname(
302 BaseLib::dropFileExtension(output_base_fname.getValue()));
303 writeBCsAndGeometry(geometry_sets, surface_name, base_fname,
304 bc_type.getValue(), gml_arg.getValue());
305 return EXIT_SUCCESS;
306}
int main(int argc, char *argv[])
void writeGroundwaterFlowPointBC(std::ostream &bc_out, std::string const &pnt_name, double head_value)
void writeBCsAndGeometry(GeoLib::GEOObjects &geometry_sets, std::string const &geo_name, std::string const &out_fname, std::string const &bc_type, bool const write_gml)
void convertMeshNodesToGeometry(std::vector< MeshLib::Node * > const &nodes, std::vector< std::size_t > const &node_ids, std::string &geo_name, GeoLib::GEOObjects &geometry_sets)
void writeLiquidFlowPointBC(std::ostream &bc_out, std::string const &pnt_name)
Filename manipulation routines.
Definition of the GEOObjects class.
Definition of the Point class.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the MeshSurfaceExtraction class.
Definition of the Mesh class.
Definition of the Node class.
Container class for geometric objects.
Definition GEOObjects.h:57
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
void addPointVec(std::vector< Point * > &&points, std::string &name, PointVec::NameIdMap &&pnt_id_name_map, double const eps=std::sqrt(std::numeric_limits< double >::epsilon()))
const PointVec * getPointVecObj(const std::string &name) const
int mergeGeometries(std::vector< std::string > const &geo_names, std::string &merged_geo_name)
const std::vector< Polyline * > * getPolylineVec(const std::string &name) const
This class manages pointers to Points in a std::vector along with a name. It also handles the deletio...
Definition PointVec.h:36
std::string const & getItemNameByID(std::size_t id) const
Definition PointVec.cpp:248
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:41
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
std::vector< T * > const & getVector() const
std::vector< std::size_t > getMeshNodeIDs(GeoLib::GeoObject const &geoObj) const
static MeshLib::Mesh * getMeshSurface(const MeshLib::Mesh &subsfc_mesh, Eigen::Vector3d const &dir, double angle, std::string_view subsfc_node_id_prop_name="", std::string_view subsfc_element_id_prop_name="", std::string_view face_id_prop_name="")
std::string dropFileExtension(std::string const &filename)
void readGeometryFromFile(std::string const &fname, GeoLib::GEOObjects &geo_objs, std::string const &gmsh_path)
void writeGeometryToFile(std::string const &geo_name, GeoLib::GEOObjects &geo_objs, std::string const &fname)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
Definition of readMeshFromFile function.