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