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-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}
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:225

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