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

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().