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