OGS
createNeumannBc.cpp
Go to the documentation of this file.
1 
10 #include <tclap/CmdLine.h>
11 
12 #include <fstream>
13 
14 #include "InfoLib/GitInfo.h"
18 #include "MeshLib/Mesh.h"
19 #include "MeshLib/Node.h"
20 
31  const MeshLib::Mesh& mesh, std::string const& prop_name)
32 {
33  if (mesh.getDimension() != 2)
34  {
35  ERR("Error in "
36  "MeshSurfaceExtraction::getSurfaceIntegratedValuesForNodes() - "
37  "Given mesh is no surface mesh (dimension != 2).");
38  return std::vector<double>();
39  }
40 
41  if (!mesh.getProperties().existsPropertyVector<double>(prop_name))
42  {
43  ERR("Need element property, but the property '{:s}' is not available.",
44  prop_name);
45  return std::vector<double>();
46  }
47  auto const* const elem_pv = mesh.getProperties().getPropertyVector<double>(
48  prop_name, MeshLib::MeshItemType::Cell, 1);
49 
50  std::vector<double> integrated_node_area_vec;
51  double total_area(0);
52 
53  for (auto const* node : mesh.getNodes())
54  {
55  double node_area(0);
56  double integrated_node_area(0);
57  for (auto const& connected_elem :
58  mesh.getElementsConnectedToNode(*node))
59  {
60  double const area = connected_elem->getContent() /
61  connected_elem->getNumberOfBaseNodes();
62  node_area += area;
63  integrated_node_area += area * (*elem_pv)[connected_elem->getID()];
64  total_area += area;
65  }
66 
67  integrated_node_area_vec.push_back(integrated_node_area);
68  }
69 
70  INFO("Total surface area: {:g}", total_area);
71 
72  return integrated_node_area_vec;
73 }
74 
75 int main(int argc, char* argv[])
76 {
77  TCLAP::CmdLine cmd(
78  "Integrates the given element property and outputs an OGS-5 direct "
79  "Neumann boundary condition. The mesh has to contain a property "
80  "'bulk_node_ids' that stores the original subsurface mesh node ids. "
81  "Such surface meshes can be created using the OGS-6 tool "
82  "ExtractSurface.\n\n"
83  "OpenGeoSys-6 software, version " +
85  ".\n"
86  "Copyright (c) 2012-2021, OpenGeoSys Community "
87  "(http://www.opengeosys.org)",
89 
90  TCLAP::ValueArg<std::string> in_mesh(
91  "i",
92  "in-mesh",
93  "the surface mesh that has an element property for the Neumann "
94  "boundary condition",
95  true,
96  "",
97  "filename for surface mesh input");
98  cmd.add(in_mesh);
99 
100  TCLAP::ValueArg<std::string> property_in_arg(
101  "p",
102  "property-in-name",
103  "name of an element property used for the computation of the Neumann "
104  "boundary condition",
105  true,
106  "",
107  "string (property name)");
108  cmd.add(property_in_arg);
109 
110  TCLAP::ValueArg<std::string> property_out_arg(
111  "",
112  "property-out-name",
113  "name of the node based property used for the output of the Neumann "
114  "boundary condition",
115  true,
116  "",
117  "string (property name)");
118  cmd.add(property_out_arg);
119 
120  TCLAP::ValueArg<std::string> result_file(
121  "o",
122  "result-out",
123  "the file name the result will be written to ",
124  true,
125  "",
126  "output file name");
127  cmd.add(result_file);
128  cmd.parse(argc, argv);
129 
130  // read surface mesh
131  std::unique_ptr<MeshLib::Mesh> surface_mesh(
132  MeshLib::IO::readMeshFromFile(in_mesh.getValue()));
133 
134  auto const* const node_id_pv =
136  {
137  try
138  {
139  return surface_mesh->getProperties().getPropertyVector<std::size_t>(
140  "bulk_node_ids", MeshLib::MeshItemType::Node, 1);
141  }
142  catch (std::runtime_error const& e)
143  {
144  WARN("{:s}", e.what());
145  return nullptr;
146  }
147  }();
148  if (!node_id_pv)
149  {
150  return EXIT_FAILURE;
151  }
152 
153  std::vector<double> integrated_values = getSurfaceIntegratedValuesForNodes(
154  *surface_mesh, property_in_arg.getValue());
155  std::vector<std::pair<std::size_t, double>> direct_values;
156  direct_values.reserve(surface_mesh->getNumberOfNodes());
157 
158  for (auto const* node : surface_mesh->getNodes())
159  {
160  auto const id(node->getID());
161  auto const subsurface_node_id((*node_id_pv)[id]);
162  auto const val(integrated_values[id]);
163  direct_values.emplace_back(subsurface_node_id, val);
164  }
165 
166  auto* const pv =
167  surface_mesh->getProperties().createNewPropertyVector<double>(
168  property_out_arg.getValue(), MeshLib::MeshItemType::Node, 1);
169  pv->resize(surface_mesh->getNodes().size());
170  for (std::size_t k(0); k < surface_mesh->getNodes().size(); ++k)
171  {
172  (*pv)[k] = direct_values[k].second;
173  }
174 
175  MeshLib::IO::writeMeshToFile(*surface_mesh, result_file.getValue());
176 
177  std::ofstream result_out(result_file.getValue() + ".txt");
178  result_out.precision(std::numeric_limits<double>::digits10);
179  for (auto const& p : direct_values)
180  {
181  result_out << p.first << " " << p.second << "\n";
182  }
183 
184  return EXIT_SUCCESS;
185 }
Definition of the Element 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
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Definition of the Mesh class.
Definition of the Node class.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:95
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition: Mesh.h:71
Properties & getProperties()
Definition: Mesh.h:123
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition: Mesh.cpp:232
PropertyVector< T > const * getPropertyVector(std::string const &name) const
bool existsPropertyVector(std::string const &name) const
int main(int argc, char *argv[])
std::vector< double > getSurfaceIntegratedValuesForNodes(const MeshLib::Mesh &mesh, std::string const &prop_name)
GITINFOLIB_EXPORT const std::string ogs_version
static const double p
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, [[maybe_unused]] std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.