OGS
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 "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/MeshSurfaceExtraction.h"
#include "MeshLib/Node.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 59 of file ComputeNodeAreasFromSurfaceMesh.cpp.

60 {
61  TCLAP::CmdLine cmd(
62  "The tool computes the area per node of the surface mesh and writes "
63  "the information as txt and csv data.\n\n"
64  "OpenGeoSys-6 software, version " +
66  ".\n"
67  "Copyright (c) 2012-2021, OpenGeoSys Community "
68  "(http://www.opengeosys.org)",
70  TCLAP::ValueArg<std::string> mesh_in(
71  "i", "mesh-input-file",
72  "the name of the file containing the input mesh", true, "",
73  "file name of input mesh");
74  cmd.add(mesh_in);
75  TCLAP::ValueArg<std::string> id_prop_name(
76  "", "id-prop-name",
77  "the name of the property containing the id information", false,
78  "bulk_node_ids", "property name");
79  cmd.add(id_prop_name);
80  TCLAP::ValueArg<std::string> out_base_fname(
81  "p", "output-base-name",
82  "the path and base file name the output will be written to", false, "",
83  "output path and base name as one string");
84  cmd.add(out_base_fname);
85 
86  cmd.parse(argc, argv);
87 
88  std::unique_ptr<MeshLib::Mesh> surface_mesh(
89  MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
90  INFO("Mesh read: {:d} nodes, {:d} elements.",
91  surface_mesh->getNumberOfNodes(), surface_mesh->getNumberOfElements());
92  // ToDo check if mesh is read correct and if the mesh is a surface mesh
93 
94  MeshLib::PropertyVector<std::size_t>* orig_node_ids(nullptr);
95  // check if a node property containing the subsurface ids is available
96  // if the node property is not available generate it
97  if (!surface_mesh->getProperties().existsPropertyVector<std::size_t>(
98  id_prop_name.getValue()))
99  {
100  orig_node_ids =
101  surface_mesh->getProperties().createNewPropertyVector<std::size_t>(
102  id_prop_name.getValue(), MeshLib::MeshItemType::Node, 1);
103  if (!orig_node_ids)
104  {
105  ERR("Fatal error: could not create property.");
106  return EXIT_FAILURE;
107  }
108  orig_node_ids->resize(surface_mesh->getNumberOfNodes());
109  std::iota(orig_node_ids->begin(), orig_node_ids->end(), 0);
110  }
111  else
112  {
113  orig_node_ids =
114  surface_mesh->getProperties().getPropertyVector<std::size_t>(
115  id_prop_name.getValue());
116  }
117 
118  std::vector<double> areas(
120 
121  // pack area and node id together
122  std::vector<std::pair<std::size_t, double>> ids_and_areas;
123  std::transform(orig_node_ids->cbegin(), orig_node_ids->cend(),
124  areas.cbegin(), std::back_inserter(ids_and_areas),
125  std::make_pair<std::size_t const&, double const&>);
126 
127  // generate file names for output
128  std::string path(out_base_fname.getValue());
129  if (path.empty())
130  {
131  path = BaseLib::dropFileExtension(mesh_in.getValue());
132  }
133  std::string const id_and_area_fname(path + ".txt");
134  std::string const csv_fname(path + ".csv");
135 
136  writeToFile(id_and_area_fname, csv_fname, ids_and_areas,
137  surface_mesh->getNodes());
138 
139  return EXIT_SUCCESS;
140 }
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(char const *fmt, Args const &... args)
Definition: Logging.h:32
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
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)
Definition: FileTools.cpp:169
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)

References BaseLib::dropFileExtension(), ERR(), MeshLib::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 28 of file ComputeNodeAreasFromSurfaceMesh.cpp.

32 {
33  std::ofstream ids_and_area_out(id_area_fname);
34  if (!ids_and_area_out)
35  {
36  OGS_FATAL("Unable to open the file '{:s}' - aborting.", id_area_fname);
37  }
38  std::ofstream csv_out(csv_fname);
39  if (!csv_out)
40  {
41  OGS_FATAL("Unable to open the file '{:s}' - aborting.", csv_fname);
42  }
43 
44  ids_and_area_out.precision(std::numeric_limits<double>::digits10);
45  csv_out.precision(std::numeric_limits<double>::digits10);
46 
47  csv_out << "ID x y z area node_id\n"; // CSV header
48  for (std::size_t k(0); k < ids_and_areas.size(); k++)
49  {
50  ids_and_area_out << ids_and_areas[k].first << " "
51  << ids_and_areas[k].second << "\n";
52  csv_out << k << " " << *(mesh_nodes[k]) << ids_and_areas[k].second
53  << " " << ids_and_areas[k].first << "\n";
54  }
55  ids_and_area_out << "\n";
56  csv_out << "\n";
57 }
#define OGS_FATAL(...)
Definition: Error.h:26

References OGS_FATAL.

Referenced by main().