OGS
createNeumannBc.cpp
Go to the documentation of this file.
1
11#include <tclap/CmdLine.h>
12
13#include <fstream>
14
15#include "BaseLib/MPI.h"
16#include "InfoLib/GitInfo.h"
20#include "MeshLib/Mesh.h"
21#include "MeshLib/Node.h"
22
33 const MeshLib::Mesh& mesh, std::string const& prop_name)
34{
35 if (mesh.getDimension() != 2)
36 {
37 ERR("Error in "
38 "MeshSurfaceExtraction::getSurfaceIntegratedValuesForNodes() - "
39 "Given mesh is no surface mesh (dimension != 2).");
40 return std::vector<double>();
41 }
42
43 if (!mesh.getProperties().existsPropertyVector<double>(prop_name))
44 {
45 ERR("Need element property, but the property '{:s}' is not available.",
46 prop_name);
47 return std::vector<double>();
48 }
49 auto const* const elem_pv = mesh.getProperties().getPropertyVector<double>(
50 prop_name, MeshLib::MeshItemType::Cell, 1);
51
52 std::vector<double> integrated_node_area_vec;
53 double total_area(0);
54
55 for (auto const* node : mesh.getNodes())
56 {
57 double integrated_node_area(0);
58 for (auto const& connected_elem :
60 {
61 double const area = connected_elem->getContent() /
62 connected_elem->getNumberOfBaseNodes();
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
75int 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-2024, 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 BaseLib::MPI::Setup mpi_setup(argc, argv);
131
132 // read surface mesh
133 std::unique_ptr<MeshLib::Mesh> surface_mesh(
134 MeshLib::IO::readMeshFromFile(in_mesh.getValue()));
135
136 auto const* const node_id_pv =
138 {
139 try
140 {
141 return surface_mesh->getProperties().getPropertyVector<std::size_t>(
144 }
145 catch (std::runtime_error const& e)
146 {
147 WARN("{:s}", e.what());
148 return nullptr;
149 }
150 }();
151 if (!node_id_pv)
152 {
153 return EXIT_FAILURE;
154 }
155
156 std::vector<double> integrated_values = getSurfaceIntegratedValuesForNodes(
157 *surface_mesh, property_in_arg.getValue());
158 std::vector<std::pair<std::size_t, double>> direct_values;
159 direct_values.reserve(surface_mesh->getNumberOfNodes());
160
161 for (auto const* node : surface_mesh->getNodes())
162 {
163 auto const id(node->getID());
164 auto const subsurface_node_id((*node_id_pv)[id]);
165 auto const val(integrated_values[id]);
166 direct_values.emplace_back(subsurface_node_id, val);
167 }
168
169 auto* const pv =
170 surface_mesh->getProperties().createNewPropertyVector<double>(
171 property_out_arg.getValue(), MeshLib::MeshItemType::Node, 1);
172 pv->resize(surface_mesh->getNodes().size());
173 for (std::size_t k(0); k < surface_mesh->getNodes().size(); ++k)
174 {
175 (*pv)[k] = direct_values[k].second;
176 }
177
178 MeshLib::IO::writeMeshToFile(*surface_mesh, result_file.getValue());
179
180 std::ofstream result_out(result_file.getValue() + ".txt");
181 result_out.precision(std::numeric_limits<double>::max_digits10);
182 for (auto const& p : direct_values)
183 {
184 result_out << p.first << " " << p.second << "\n";
185 }
186
187 return EXIT_SUCCESS;
188}
Definition of the Element class.
Git information.
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
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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:106
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:88
Properties & getProperties()
Definition Mesh.h:134
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition Mesh.cpp:256
bool existsPropertyVector(std::string_view name) const
Definition Properties.h:74
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)
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)
Definition Properties.h:188
Definition of readMeshFromFile function.