OGS
CreateBoundaryConditionsAlongPolylines.cpp File Reference
#include <tclap/CmdLine.h>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include "Applications/FileIO/readGeometryFromFile.h"
#include "Applications/FileIO/writeGeometryToFile.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/MPI.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/Point.h"
#include "InfoLib/GitInfo.h"
#include "MeshGeoToolsLib/MeshNodeSearcher.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
#include "MeshToolsLib/MeshSurfaceExtraction.h"
Include dependency graph for CreateBoundaryConditionsAlongPolylines.cpp:

Go to the source code of this file.

Functions

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 writeGroundwaterFlowPointBC (std::ostream &bc_out, std::string const &pnt_name, double head_value)
 
void writeLiquidFlowPointBC (std::ostream &bc_out, std::string const &pnt_name)
 
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)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ convertMeshNodesToGeometry()

void convertMeshNodesToGeometry ( std::vector< MeshLib::Node * > const & nodes,
std::vector< std::size_t > const & node_ids,
std::string & geo_name,
GeoLib::GEOObjects & geometry_sets )

Definition at line 34 of file CreateBoundaryConditionsAlongPolylines.cpp.

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}
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()))
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:41

References GeoLib::GEOObjects::addPointVec().

Referenced by main().

◆ main()

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

Definition at line 126 of file CreateBoundaryConditionsAlongPolylines.cpp.

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-2025, 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 "Output. The base name of the file the output (geometry (gli) and "
145 "boundary "
146 "condition (bc)) will be written to",
147 true, "", "BASE_FILENAME_OUTPUT");
148 cmd.add(output_base_fname);
149
150 std::vector<std::string> allowed_types_vector{"LIQUID_FLOW",
151 "GROUNDWATER_FLOW"};
152 TCLAP::ValuesConstraint<std::string> allowed_types(allowed_types_vector);
153 TCLAP::ValueArg<std::string> bc_type(
154 "t", "type",
155 "the process type the boundary condition will be written for currently "
156 "LIQUID_FLOW (primary variable PRESSURE1) and GROUNDWATER_FLOW "
157 "(primary variable HEAD, default) are supported, ",
158 true, "", &allowed_types);
159 cmd.add(bc_type);
160
161 TCLAP::ValueArg<double> search_length_arg(
162 "s", "search-length", "The size of the search length ", false,
163 std::numeric_limits<double>::epsilon(), "SEARCH_LENGTH");
164 cmd.add(search_length_arg);
165
166 TCLAP::ValueArg<std::string> geometry_fname(
167 "i", "input-geometry",
168 "Input (.gml | .gli). The name of the input file containing the "
169 "geometry",
170 true, "", "INPUT_FILE");
171 cmd.add(geometry_fname);
172
173 TCLAP::ValueArg<std::string> mesh_arg(
174 "m", "mesh-file",
175 "Input (.vtu). The name of the input file containing the mesh", true,
176 "", "INPUT_FILE");
177 cmd.add(mesh_arg);
178
179 TCLAP::ValueArg<std::string> gmsh_path_arg(
180 "g", "gmsh-path", "Input (.msh). The path to the gmsh binary", false,
181 "", "INPUT_FILE");
182 cmd.add(gmsh_path_arg);
183
184 cmd.parse(argc, argv);
185
186 BaseLib::MPI::Setup mpi_setup(argc, argv);
187
188 // *** read mesh
189 INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
190 std::unique_ptr<MeshLib::Mesh> subsurface_mesh(
191 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
192 INFO("done.");
193 INFO("Extracting top surface of mesh '{:s}' ... ", mesh_arg.getValue());
194 Eigen::Vector3d const dir({0, 0, -1});
195 double const angle(90);
196 std::unique_ptr<MeshLib::Mesh> surface_mesh(
198 dir, angle));
199 INFO("done.");
200 subsurface_mesh.reset(nullptr);
201
202 // *** read geometry
203 GeoLib::GEOObjects geometries;
204 FileIO::readGeometryFromFile(geometry_fname.getValue(), geometries,
205 gmsh_path_arg.getValue());
206
207 auto const geo_name = geometries.getGeometryNames()[0];
208
209 // *** check if the data is usable
210 // *** get vector of polylines
211 std::vector<GeoLib::Polyline*> const* plys(
212 geometries.getPolylineVec(geo_name));
213 if (!plys)
214 {
215 ERR("Could not get vector of polylines out of geometry '{:s}'.",
216 geo_name);
217 return EXIT_FAILURE;
218 }
219
220 auto search_length_strategy =
221 std::make_unique<MeshGeoToolsLib::SearchLength>();
222 if (search_length_arg.isSet())
223 {
224 search_length_strategy.reset(
225 new MeshGeoToolsLib::SearchLength(search_length_arg.getValue()));
226 }
227
228 GeoLib::GEOObjects geometry_sets;
230 *surface_mesh, std::move(search_length_strategy),
232 for (std::size_t k(0); k < plys->size(); k++)
233 {
234 auto const& ids = mesh_searcher.getMeshNodeIDs(*((*plys)[k]));
235 if (ids.empty())
236 {
237 continue;
238 }
239 std::string polyline_name("Polyline-" + std::to_string(k));
240 convertMeshNodesToGeometry(surface_mesh->getNodes(), ids, polyline_name,
241 geometry_sets);
242 }
243
244 // merge all together
245 auto const geo_names = geometry_sets.getGeometryNames();
246 if (geo_names.empty())
247 {
248 ERR("Did not find mesh nodes along polylines.");
249 return EXIT_FAILURE;
250 }
251
252 std::string merge_name("AllMeshNodesAlongPolylines");
253 if (geometry_sets.mergeGeometries(geo_names, merge_name) == 2)
254 {
255 merge_name = geo_names[0];
256 }
257
258 GeoLib::PointVec const* pnt_vec(geometry_sets.getPointVecObj(merge_name));
259 auto const& merged_pnts(pnt_vec->getVector());
260
261 std::vector<GeoLib::Point> pnts_with_id;
262 const std::size_t n_merged_pnts(merged_pnts.size());
263 for (std::size_t k(0); k < n_merged_pnts; ++k)
264 {
265 pnts_with_id.emplace_back(*(merged_pnts[k]), k);
266 }
267
268 std::sort(pnts_with_id.begin(), pnts_with_id.end(),
269 [](GeoLib::Point const& p0, GeoLib::Point const& p1)
270 { return p0 < p1; });
271
272 double const eps(std::numeric_limits<double>::epsilon());
273 std::vector<GeoLib::Point*> surface_pnts;
274 GeoLib::PointVec::NameIdMap name_id_map;
275
276 // insert first point
277 surface_pnts.push_back(
278 new GeoLib::Point(pnts_with_id[0], surface_pnts.size()));
279 {
280 std::string element_name;
281 pnt_vec->getNameOfElementByID(0, element_name);
282 name_id_map[element_name] = 0;
283 }
284 for (std::size_t k(1); k < n_merged_pnts; ++k)
285 {
286 const GeoLib::Point& p0(pnts_with_id[k - 1]);
287 const GeoLib::Point& p1(pnts_with_id[k]);
288 if (std::abs(p0[0] - p1[0]) > eps || std::abs(p0[1] - p1[1]) > eps)
289 {
290 surface_pnts.push_back(
291 new GeoLib::Point(pnts_with_id[k], surface_pnts.size()));
292 std::string element_name;
293 pnt_vec->getNameOfElementByID(k, element_name);
294 name_id_map[element_name] = surface_pnts.size() - 1;
295 }
296 }
297
298 std::string surface_name(BaseLib::dropFileExtension(mesh_arg.getValue()) +
299 "-MeshNodesAlongPolylines");
300 geometry_sets.addPointVec(std::move(surface_pnts), surface_name,
301 std::move(name_id_map), 1e-6);
302
303 // write the BCs and the merged geometry set to file
304 std::string const base_fname(
305 BaseLib::dropFileExtension(output_base_fname.getValue()));
306 writeBCsAndGeometry(geometry_sets, surface_name, base_fname,
307 bc_type.getValue(), gml_arg.getValue());
308 return EXIT_SUCCESS;
309}
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 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
Container class for geometric objects.
Definition GEOObjects.h:57
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
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
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)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:227

References GeoLib::GEOObjects::addPointVec(), convertMeshNodesToGeometry(), BaseLib::dropFileExtension(), ERR(), GeoLib::GEOObjects::getGeometryNames(), MeshGeoToolsLib::MeshNodeSearcher::getMeshNodeIDs(), MeshToolsLib::MeshSurfaceExtraction::getMeshSurface(), GeoLib::TemplateVec< T >::getNameOfElementByID(), GeoLib::GEOObjects::getPointVecObj(), GeoLib::GEOObjects::getPolylineVec(), GeoLib::TemplateVec< T >::getVector(), INFO(), GeoLib::GEOObjects::mergeGeometries(), GitInfoLib::GitInfo::ogs_version, FileIO::readGeometryFromFile(), MeshLib::IO::readMeshFromFile(), writeBCsAndGeometry(), and MeshGeoToolsLib::Yes.

◆ writeBCsAndGeometry()

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 )

Definition at line 84 of file CreateBoundaryConditionsAlongPolylines.cpp.

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}
void writeGroundwaterFlowPointBC(std::ostream &bc_out, std::string const &pnt_name, double head_value)
void writeLiquidFlowPointBC(std::ostream &bc_out, std::string const &pnt_name)
void writeGeometryToFile(std::string const &geo_name, GeoLib::GEOObjects &geo_objs, std::string const &fname)

References GeoLib::PointVec::getItemNameByID(), GeoLib::GEOObjects::getPointVecObj(), GeoLib::TemplateVec< T >::getVector(), INFO(), FileIO::writeGeometryToFile(), writeGroundwaterFlowPointBC(), and writeLiquidFlowPointBC().

Referenced by main().

◆ writeGroundwaterFlowPointBC()

void writeGroundwaterFlowPointBC ( std::ostream & bc_out,
std::string const & pnt_name,
double head_value )

Definition at line 54 of file CreateBoundaryConditionsAlongPolylines.cpp.

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}

Referenced by writeBCsAndGeometry().

◆ writeLiquidFlowPointBC()

void writeLiquidFlowPointBC ( std::ostream & bc_out,
std::string const & pnt_name )

Definition at line 68 of file CreateBoundaryConditionsAlongPolylines.cpp.

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}

Referenced by writeBCsAndGeometry().