OGS
ComputeNodeAreasFromSurfaceMesh.cpp
Go to the documentation of this file.
1
12#include <tclap/CmdLine.h>
13
14#ifdef USE_PETSC
15#include <mpi.h>
16#endif
17
18#include <fstream>
19#include <memory>
20#include <numeric>
21#include <string>
22#include <vector>
23
24#include "BaseLib/Error.h"
25#include "BaseLib/FileTools.h"
26#include "InfoLib/GitInfo.h"
28#include "MeshLib/Mesh.h"
29#include "MeshLib/Node.h"
31
32static void writeToFile(
33 std::string const& id_area_fname, std::string const& csv_fname,
34 std::vector<std::pair<std::size_t, double>> const& ids_and_areas,
35 std::vector<MeshLib::Node*> const& mesh_nodes)
36{
37 std::ofstream ids_and_area_out(id_area_fname);
38 if (!ids_and_area_out)
39 {
40 OGS_FATAL("Unable to open the file '{:s}' - aborting.", id_area_fname);
41 }
42 std::ofstream csv_out(csv_fname);
43 if (!csv_out)
44 {
45 OGS_FATAL("Unable to open the file '{:s}' - aborting.", csv_fname);
46 }
47
48 ids_and_area_out.precision(std::numeric_limits<double>::max_digits10);
49 csv_out.precision(std::numeric_limits<double>::max_digits10);
50
51 csv_out << "ID x y z area node_id\n"; // CSV header
52 for (std::size_t k(0); k < ids_and_areas.size(); k++)
53 {
54 ids_and_area_out << ids_and_areas[k].first << " "
55 << ids_and_areas[k].second << "\n";
56 csv_out << k << " " << *(mesh_nodes[k]) << ids_and_areas[k].second
57 << " " << ids_and_areas[k].first << "\n";
58 }
59 ids_and_area_out << "\n";
60 csv_out << "\n";
61}
62
63int main(int argc, char* argv[])
64{
65 TCLAP::CmdLine cmd(
66 "The tool computes the area per node of the surface mesh and writes "
67 "the information as txt and csv data.\n\n"
68 "OpenGeoSys-6 software, version " +
70 ".\n"
71 "Copyright (c) 2012-2024, OpenGeoSys Community "
72 "(http://www.opengeosys.org)",
74 TCLAP::ValueArg<std::string> mesh_in(
75 "i", "mesh-input-file",
76 "the name of the file containing the input mesh", true, "",
77 "file name of input mesh");
78 cmd.add(mesh_in);
79 TCLAP::ValueArg<std::string> id_prop_name(
80 "", "id-prop-name",
81 "the name of the property containing the id information", false,
83 "property name");
84 cmd.add(id_prop_name);
85 TCLAP::ValueArg<std::string> out_base_fname(
86 "p", "output-base-name",
87 "the path and base file name the output will be written to", false, "",
88 "output path and base name as one string");
89 cmd.add(out_base_fname);
90
91 cmd.parse(argc, argv);
92
93#ifdef USE_PETSC
94 MPI_Init(&argc, &argv);
95#endif
96
97 std::unique_ptr<MeshLib::Mesh> surface_mesh(
98 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
99 INFO("Mesh read: {:d} nodes, {:d} elements.",
100 surface_mesh->getNumberOfNodes(), surface_mesh->getNumberOfElements());
101 // ToDo check if mesh is read correct and if the mesh is a surface mesh
102
103 MeshLib::PropertyVector<std::size_t>* orig_node_ids(nullptr);
104 // check if a node property containing the subsurface ids is available
105 // if the node property is not available generate it
106 if (!surface_mesh->getProperties().existsPropertyVector<std::size_t>(
107 id_prop_name.getValue()))
108 {
109 orig_node_ids =
110 surface_mesh->getProperties().createNewPropertyVector<std::size_t>(
111 id_prop_name.getValue(), MeshLib::MeshItemType::Node, 1);
112 if (!orig_node_ids)
113 {
114 ERR("Fatal error: could not create property.");
115#ifdef USE_PETSC
116 MPI_Finalize();
117#endif
118 return EXIT_FAILURE;
119 }
120 orig_node_ids->resize(surface_mesh->getNumberOfNodes());
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#ifdef USE_PETSC
153 MPI_Finalize();
154#endif
155 return EXIT_SUCCESS;
156}
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:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the MeshSurfaceExtraction class.
Definition of the Mesh class.
Definition of the Node class.
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)
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:188
Definition of readMeshFromFile function.