OGS
ComputeNodeAreasFromSurfaceMesh.cpp
Go to the documentation of this file.
1
11#include <tclap/CmdLine.h>
12
13#include <fstream>
14#include <memory>
15#include <numeric>
16#include <string>
17#include <vector>
18
19#include "BaseLib/Error.h"
20#include "BaseLib/FileTools.h"
21#include "BaseLib/Logging.h"
22#include "BaseLib/MPI.h"
24#include "InfoLib/GitInfo.h"
26#include "MeshLib/Mesh.h"
27#include "MeshLib/Node.h"
29
30static void writeToFile(
31 std::string const& id_area_fname, std::string const& csv_fname,
32 std::vector<std::pair<std::size_t, double>> const& ids_and_areas,
33 std::vector<MeshLib::Node*> const& mesh_nodes)
34{
35 std::ofstream ids_and_area_out(id_area_fname);
36 if (!ids_and_area_out)
37 {
38 OGS_FATAL("Unable to open the file '{:s}' - aborting.", id_area_fname);
39 }
40 std::ofstream csv_out(csv_fname);
41 if (!csv_out)
42 {
43 OGS_FATAL("Unable to open the file '{:s}' - aborting.", csv_fname);
44 }
45
46 ids_and_area_out.precision(std::numeric_limits<double>::max_digits10);
47 csv_out.precision(std::numeric_limits<double>::max_digits10);
48
49 csv_out << "ID x y z area node_id\n"; // CSV header
50 for (std::size_t k(0); k < ids_and_areas.size(); k++)
51 {
52 ids_and_area_out << ids_and_areas[k].first << " "
53 << ids_and_areas[k].second << "\n";
54 csv_out << k << " " << *(mesh_nodes[k]) << ids_and_areas[k].second
55 << " " << ids_and_areas[k].first << "\n";
56 }
57 ids_and_area_out << "\n";
58 csv_out << "\n";
59}
60
61int main(int argc, char* argv[])
62{
63 TCLAP::CmdLine cmd(
64 "The tool computes the area per node of the surface mesh and writes "
65 "the information as txt and csv data.\n\n"
66 "OpenGeoSys-6 software, version " +
68 ".\n"
69 "Copyright (c) 2012-2025, OpenGeoSys Community "
70 "(http://www.opengeosys.org)",
72 TCLAP::ValueArg<std::string> mesh_in(
73 "i", "mesh-input-file",
74 "Input (.vtu). The name of the file containing the input mesh", true,
75 "", "INPUT_FILE");
76 cmd.add(mesh_in);
77
78 std::vector<std::string> allowed_types_vector{
79 "bulk_node_ids", "bulk_element_ids", "bulk_edge_ids", "bulk_face_ids"};
80 TCLAP::ValuesConstraint<std::string> allowed_types(allowed_types_vector);
81 TCLAP::ValueArg<std::string> id_prop_name(
82 "", "id-prop-name",
83 "the name of the property containing the id information", false,
84 "bulk_node_ids", &allowed_types);
85 cmd.add(id_prop_name);
86 TCLAP::ValueArg<std::string> out_base_fname(
87 "p", "output-base-name",
88 "Output (.csv | .txt). The path and base file name the output will be "
89 "written to",
90 false, "", "BASE_FILENAME_OUTPUT");
91 cmd.add(out_base_fname);
92
93 auto log_level_arg = BaseLib::makeLogLevelArg();
94 cmd.add(log_level_arg);
95 cmd.parse(argc, argv);
96
97 BaseLib::MPI::Setup mpi_setup(argc, argv);
98 BaseLib::initOGSLogger(log_level_arg.getValue());
99
100 std::unique_ptr<MeshLib::Mesh> surface_mesh(
101 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
102 INFO("Mesh read: {:d} nodes, {:d} elements.",
103 surface_mesh->getNumberOfNodes(), surface_mesh->getNumberOfElements());
104 // ToDo check if mesh is read correct and if the mesh is a surface mesh
105
106 MeshLib::PropertyVector<std::size_t>* orig_node_ids(nullptr);
107 // check if a node property containing the subsurface ids is available
108 // if the node property is not available generate it
109 if (!surface_mesh->getProperties().existsPropertyVector<std::size_t>(
110 id_prop_name.getValue()))
111 {
112 orig_node_ids =
113 surface_mesh->getProperties().createNewPropertyVector<std::size_t>(
114 id_prop_name.getValue(), MeshLib::MeshItemType::Node,
115 surface_mesh->getNumberOfNodes(), 1);
116 if (!orig_node_ids)
117 {
118 ERR("Fatal error: could not create property.");
119 return EXIT_FAILURE;
120 }
121 std::iota(orig_node_ids->begin(), orig_node_ids->end(), 0);
122 }
123 else
124 {
125 orig_node_ids =
126 surface_mesh->getProperties().getPropertyVector<std::size_t>(
127 id_prop_name.getValue());
128 }
129
130 std::vector<double> areas(
132 *surface_mesh));
133
134 // pack area and node id together
135 std::vector<std::pair<std::size_t, double>> ids_and_areas;
136 std::transform(orig_node_ids->cbegin(), orig_node_ids->cend(),
137 areas.cbegin(), std::back_inserter(ids_and_areas),
138 std::make_pair<std::size_t const&, double const&>);
139
140 // generate file names for output
141 std::string path(out_base_fname.getValue());
142 if (path.empty())
143 {
144 path = BaseLib::dropFileExtension(mesh_in.getValue());
145 }
146 std::string const id_and_area_fname(path + ".txt");
147 std::string const csv_fname(path + ".csv");
148
149 writeToFile(id_and_area_fname, csv_fname, ids_and_areas,
150 surface_mesh->getNodes());
151
152 return EXIT_SUCCESS;
153}
int main(int argc, char *argv[])
static void writeToFile(std::string const &id_area_fname, std::string const &csv_fname, std::vector< std::pair< std::size_t, double > > const &ids_and_areas, std::vector< MeshLib::Node * > const &mesh_nodes)
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of the MeshSurfaceExtraction class.
Definition of the Mesh class.
Definition of the Node class.
constexpr PROP_VAL_TYPE * end()
constexpr const PROP_VAL_TYPE * cend() const
constexpr const PROP_VAL_TYPE * cbegin() const
constexpr PROP_VAL_TYPE * begin()
static std::vector< double > getSurfaceAreaForNodes(const MeshLib::Mesh &mesh)
Returns a vector of the areas assigned to each node on a surface mesh.
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
Definition of readMeshFromFile function.