Loading [MathJax]/extensions/MathMenu.js
OGS
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
ComputeNodeAreasFromSurfaceMesh.cpp File Reference

Detailed Description

Computes the areas associated nodes of the surface mesh.

Definition in file ComputeNodeAreasFromSurfaceMesh.cpp.

#include <tclap/CmdLine.h>
#include <fstream>
#include <memory>
#include <numeric>
#include <string>
#include <vector>
#include "BaseLib/Error.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/MPI.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
#include "MeshToolsLib/MeshSurfaceExtraction.h"
Include dependency graph for ComputeNodeAreasFromSurfaceMesh.cpp:

Go to the source code of this file.

Functions

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)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 60 of file ComputeNodeAreasFromSurfaceMesh.cpp.

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 "the name of the file containing the input mesh", true, "",
74 "file name of input mesh");
75 cmd.add(mesh_in);
76 TCLAP::ValueArg<std::string> id_prop_name(
77 "", "id-prop-name",
78 "the name of the property containing the id information", false,
80 "property name");
81 cmd.add(id_prop_name);
82 TCLAP::ValueArg<std::string> out_base_fname(
83 "p", "output-base-name",
84 "the path and base file name the output will be written to", false, "",
85 "output path and base name as one string");
86 cmd.add(out_base_fname);
87
88 cmd.parse(argc, argv);
89
90 BaseLib::MPI::Setup mpi_setup(argc, argv);
91
92 std::unique_ptr<MeshLib::Mesh> surface_mesh(
93 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
94 INFO("Mesh read: {:d} nodes, {:d} elements.",
95 surface_mesh->getNumberOfNodes(), surface_mesh->getNumberOfElements());
96 // ToDo check if mesh is read correct and if the mesh is a surface mesh
97
98 MeshLib::PropertyVector<std::size_t>* orig_node_ids(nullptr);
99 // check if a node property containing the subsurface ids is available
100 // if the node property is not available generate it
101 if (!surface_mesh->getProperties().existsPropertyVector<std::size_t>(
102 id_prop_name.getValue()))
103 {
104 orig_node_ids =
105 surface_mesh->getProperties().createNewPropertyVector<std::size_t>(
106 id_prop_name.getValue(), MeshLib::MeshItemType::Node, 1);
107 if (!orig_node_ids)
108 {
109 ERR("Fatal error: could not create property.");
110 return EXIT_FAILURE;
111 }
112 orig_node_ids->resize(surface_mesh->getNumberOfNodes());
113 std::iota(orig_node_ids->begin(), orig_node_ids->end(), 0);
114 }
115 else
116 {
117 orig_node_ids =
118 surface_mesh->getProperties().getPropertyVector<std::size_t>(
119 id_prop_name.getValue());
120 }
121
122 std::vector<double> areas(
124 *surface_mesh));
125
126 // pack area and node id together
127 std::vector<std::pair<std::size_t, double>> ids_and_areas;
128 std::transform(orig_node_ids->cbegin(), orig_node_ids->cend(),
129 areas.cbegin(), std::back_inserter(ids_and_areas),
130 std::make_pair<std::size_t const&, double const&>);
131
132 // generate file names for output
133 std::string path(out_base_fname.getValue());
134 if (path.empty())
135 {
136 path = BaseLib::dropFileExtension(mesh_in.getValue());
137 }
138 std::string const id_and_area_fname(path + ".txt");
139 std::string const csv_fname(path + ".csv");
140
141 writeToFile(id_and_area_fname, csv_fname, ids_and_areas,
142 surface_mesh->getNodes());
143
144 return EXIT_SUCCESS;
145}
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)
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
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:185

References BaseLib::dropFileExtension(), ERR(), MeshLib::getBulkIDString(), MeshToolsLib::MeshSurfaceExtraction::getSurfaceAreaForNodes(), INFO(), MeshLib::Node, GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), and writeToFile().

◆ writeToFile()

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 )
static

Definition at line 29 of file ComputeNodeAreasFromSurfaceMesh.cpp.

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}
#define OGS_FATAL(...)
Definition Error.h:26

References OGS_FATAL.

Referenced by main().