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