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