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/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.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 27 of file CreateBoundaryConditionsAlongPolylines.cpp.

31{
32 // copy data
33 std::vector<GeoLib::Point*> pnts;
35 std::size_t cnt(0);
36 for (std::size_t id : node_ids)
37 {
38 pnts.push_back(new GeoLib::Point(*(nodes[id]), cnt));
39 pnt_names[geo_name + "-PNT-" + std::to_string(cnt)] = cnt;
40 cnt++;
41 }
42
43 // create data structures for geometry
44 geometry_sets.addPointVec(std::move(pnts), geo_name, std::move(pnt_names));
45}
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:30

References GeoLib::GEOObjects::addPointVec().

Referenced by main().

◆ main()

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

Definition at line 119 of file CreateBoundaryConditionsAlongPolylines.cpp.

120{
121 TCLAP::CmdLine cmd(
122 "Creates boundary conditions for mesh nodes along polylines."
123 "The documentation is available at "
124 "https://docs.opengeosys.org/docs/tools/model-preparation/"
125 "create-boundary-conditions-along-a-polyline.\n\n"
126 "OpenGeoSys-6 software, version " +
128 ".\n"
129 "Copyright (c) 2012-2026, OpenGeoSys Community "
130 "(http://www.opengeosys.org)",
132 TCLAP::SwitchArg gml_arg("", "gml", "Write found nodes to gml file.");
133 cmd.add(gml_arg);
134
135 TCLAP::ValueArg<std::string> output_base_fname(
136 "o", "output-base-file-name",
137 "Output (.gli | .bc). The base name of the file the output (geometry "
138 "(gli) and boundary condition (bc)) will be written to",
139 true, "", "BASE_FILENAME_OUTPUT");
140 cmd.add(output_base_fname);
141
142 std::vector<std::string> allowed_types_vector{"LIQUID_FLOW",
143 "GROUNDWATER_FLOW"};
144 TCLAP::ValuesConstraint<std::string> allowed_types(allowed_types_vector);
145 TCLAP::ValueArg<std::string> bc_type(
146 "t", "type",
147 "the process type the boundary condition will be written for currently "
148 "LIQUID_FLOW (primary variable PRESSURE1) and GROUNDWATER_FLOW "
149 "(primary variable HEAD, default) are supported, ",
150 true, "", &allowed_types);
151 cmd.add(bc_type);
152
153 TCLAP::ValueArg<double> search_length_arg(
154 "s", "search-length", "The size of the search length ", false,
155 std::numeric_limits<double>::epsilon(), "SEARCH_LENGTH");
156 cmd.add(search_length_arg);
157
158 TCLAP::ValueArg<std::string> geometry_fname(
159 "i", "input-geometry",
160 "Input (.gml | .gli). The name of the input file containing the "
161 "geometry",
162 true, "", "INPUT_FILE");
163 cmd.add(geometry_fname);
164
165 TCLAP::ValueArg<std::string> mesh_arg(
166 "m", "mesh-file",
167 "Input (.vtu). The name of the input file containing the mesh", true,
168 "", "INPUT_FILE");
169 cmd.add(mesh_arg);
170
171 TCLAP::ValueArg<std::string> gmsh_path_arg(
172 "g", "gmsh-path", "Input (.msh). The path to the gmsh binary", false,
173 "", "INPUT_FILE");
174 cmd.add(gmsh_path_arg);
175
176 auto log_level_arg = BaseLib::makeLogLevelArg();
177 cmd.add(log_level_arg);
178 cmd.parse(argc, argv);
179
180 BaseLib::MPI::Setup mpi_setup(argc, argv);
181 BaseLib::initOGSLogger(log_level_arg.getValue());
182
183 // *** read mesh
184 INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
185 std::unique_ptr<MeshLib::Mesh> subsurface_mesh(
186 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
187 INFO("done.");
188 INFO("Extracting top surface of mesh '{:s}' ... ", mesh_arg.getValue());
189 Eigen::Vector3d const dir({0, 0, -1});
190 double const angle(90);
191 std::unique_ptr<MeshLib::Mesh> surface_mesh(
193 dir, angle));
194 INFO("done.");
195 subsurface_mesh.reset(nullptr);
196
197 // *** read geometry
198 GeoLib::GEOObjects geometries;
199 FileIO::readGeometryFromFile(geometry_fname.getValue(), geometries,
200 gmsh_path_arg.getValue());
201
202 auto const geo_name = geometries.getGeometryNames()[0];
203
204 // *** check if the data is usable
205 // *** get vector of polylines
206 std::vector<GeoLib::Polyline*> const* plys(
207 geometries.getPolylineVec(geo_name));
208 if (!plys)
209 {
210 ERR("Could not get vector of polylines out of geometry '{:s}'.",
211 geo_name);
212 return EXIT_FAILURE;
213 }
214
215 auto search_length_strategy =
216 std::make_unique<MeshGeoToolsLib::SearchLength>();
217 if (search_length_arg.isSet())
218 {
219 search_length_strategy.reset(
220 new MeshGeoToolsLib::SearchLength(search_length_arg.getValue()));
221 }
222
223 GeoLib::GEOObjects geometry_sets;
225 *surface_mesh, std::move(search_length_strategy),
227 for (std::size_t k(0); k < plys->size(); k++)
228 {
229 auto const& ids = mesh_searcher.getMeshNodeIDs(*((*plys)[k]));
230 if (ids.empty())
231 {
232 continue;
233 }
234 std::string polyline_name("Polyline-" + std::to_string(k));
235 convertMeshNodesToGeometry(surface_mesh->getNodes(), ids, polyline_name,
236 geometry_sets);
237 }
238
239 // merge all together
240 auto const geo_names = geometry_sets.getGeometryNames();
241 if (geo_names.empty())
242 {
243 ERR("Did not find mesh nodes along polylines.");
244 return EXIT_FAILURE;
245 }
246
247 std::string merge_name("AllMeshNodesAlongPolylines");
248 if (geometry_sets.mergeGeometries(geo_names, merge_name) == 2)
249 {
250 merge_name = geo_names[0];
251 }
252
253 GeoLib::PointVec const* pnt_vec(geometry_sets.getPointVecObj(merge_name));
254 auto const& merged_pnts(pnt_vec->getVector());
255
256 std::vector<GeoLib::Point> pnts_with_id;
257 const std::size_t n_merged_pnts(merged_pnts.size());
258 for (std::size_t k(0); k < n_merged_pnts; ++k)
259 {
260 pnts_with_id.emplace_back(*(merged_pnts[k]), k);
261 }
262
263 std::sort(pnts_with_id.begin(), pnts_with_id.end(),
264 [](GeoLib::Point const& p0, GeoLib::Point const& p1)
265 { return p0 < p1; });
266
267 double const eps(std::numeric_limits<double>::epsilon());
268 std::vector<GeoLib::Point*> surface_pnts;
269 GeoLib::PointVec::NameIdMap name_id_map;
270
271 // insert first point
272 surface_pnts.push_back(
273 new GeoLib::Point(pnts_with_id[0], surface_pnts.size()));
274 {
275 std::string element_name;
276 pnt_vec->getNameOfElementByID(0, element_name);
277 name_id_map[element_name] = 0;
278 }
279 for (std::size_t k(1); k < n_merged_pnts; ++k)
280 {
281 const GeoLib::Point& p0(pnts_with_id[k - 1]);
282 const GeoLib::Point& p1(pnts_with_id[k]);
283 if (std::abs(p0[0] - p1[0]) > eps || std::abs(p0[1] - p1[1]) > eps)
284 {
285 surface_pnts.push_back(
286 new GeoLib::Point(pnts_with_id[k], surface_pnts.size()));
287 std::string element_name;
288 pnt_vec->getNameOfElementByID(k, element_name);
289 name_id_map[element_name] = surface_pnts.size() - 1;
290 }
291 }
292
293 std::string surface_name(BaseLib::dropFileExtension(mesh_arg.getValue()) +
294 "-MeshNodesAlongPolylines");
295 geometry_sets.addPointVec(std::move(surface_pnts), surface_name,
296 std::move(name_id_map), 1e-6);
297
298 // write the BCs and the merged geometry set to file
299 std::string const base_fname(
300 BaseLib::dropFileExtension(output_base_fname.getValue()));
301 writeBCsAndGeometry(geometry_sets, surface_name, base_fname,
302 bc_type.getValue(), gml_arg.getValue());
303 return EXIT_SUCCESS;
304}
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:28
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 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:25
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="")
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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:218

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(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), 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 77 of file CreateBoundaryConditionsAlongPolylines.cpp.

81{
82 if (write_gml)
83 {
84 INFO("write points to '{:s}.gml'.", geo_name);
85 FileIO::writeGeometryToFile(geo_name, geometry_sets,
86 out_fname + ".gml");
87 }
88 FileIO::writeGeometryToFile(geo_name, geometry_sets, out_fname + ".gli");
89
90 bool liquid_flow(false);
91 if (bc_type == "LIQUID_FLOW")
92 {
93 liquid_flow = true;
94 }
95
96 GeoLib::PointVec const* pnt_vec_objs(
97 geometry_sets.getPointVecObj(geo_name));
98 auto const& pnts(pnt_vec_objs->getVector());
99 std::ofstream bc_out(out_fname + ".bc");
100 for (std::size_t k(0); k < pnts.size(); k++)
101 {
102 std::string const& pnt_name(pnt_vec_objs->getItemNameByID(k));
103 if (!pnt_name.empty())
104 {
105 if (liquid_flow)
106 {
107 writeLiquidFlowPointBC(bc_out, pnt_name);
108 }
109 else
110 {
111 writeGroundwaterFlowPointBC(bc_out, pnt_name, (*pnts[k])[2]);
112 }
113 }
114 }
115 bc_out << "#STOP\n";
116 bc_out.close();
117}
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 47 of file CreateBoundaryConditionsAlongPolylines.cpp.

49{
50 bc_out << "#BOUNDARY_CONDITION\n";
51 bc_out << " $PCS_TYPE\n";
52 bc_out << " GROUNDWATER_FLOW\n";
53 bc_out << " $PRIMARY_VARIABLE\n";
54 bc_out << " HEAD\n";
55 bc_out << " $GEO_TYPE\n";
56 bc_out << " POINT " << pnt_name << "\n";
57 bc_out << " $DIS_TYPE\n";
58 bc_out << " CONSTANT " << head_value << "\n";
59}

Referenced by writeBCsAndGeometry().

◆ writeLiquidFlowPointBC()

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

Definition at line 61 of file CreateBoundaryConditionsAlongPolylines.cpp.

62{
63 bc_out << "#BOUNDARY_CONDITION\n";
64 bc_out << " $PCS_TYPE\n";
65 bc_out << " LIQUID_FLOW\n";
66 bc_out << " $PRIMARY_VARIABLE\n";
67 bc_out << " PRESSURE1\n";
68 bc_out << " $GEO_TYPE\n";
69 bc_out << " POINT " << pnt_name << "\n";
70 bc_out << " $DIS_TYPE\n";
71 bc_out << " CONSTANT 0.0\n";
72}

Referenced by writeBCsAndGeometry().