OGS
convertVtkDataArrayToVtkDataArray.cpp
Go to the documentation of this file.
1
12#include <tclap/CmdLine.h>
13
14#include <algorithm>
15#include <cmath>
16#include <memory>
17#include <numeric>
18
19#include "BaseLib/MPI.h"
20#include "InfoLib/GitInfo.h"
23#include "MeshLib/Mesh.h"
24
25template <typename T1, typename T2>
26std::pair<bool, std::string> castPropertyVectorToPropertyVector(
27 MeshLib::Properties& properties,
28 std::string const& property_vector_name_in,
29 std::string const& property_vector_name_out)
30{
31 auto const* const orig_pv = properties.getPropertyVector<T1>(
32 property_vector_name_in, MeshLib::MeshItemType::Cell, 1);
33 if (!orig_pv)
34 {
35 return std::make_pair(false,
36 "Original property vector '" +
37 property_vector_name_in + "' not found.");
38 }
39 auto* new_pv = properties.createNewPropertyVector<T2>(
40 property_vector_name_out, MeshLib::MeshItemType::Cell, 1);
41 if (!new_pv)
42 {
43 return std::make_pair(false,
44 "Could not create new property vector '" +
45 property_vector_name_in + "' not found.");
46 }
47 new_pv->resize(orig_pv->getNumberOfTuples());
48 for (std::size_t i(0); i < new_pv->getNumberOfTuples(); ++i)
49 {
50 (*new_pv)[i] = static_cast<T2>((*orig_pv)[i]);
51 }
52 return std::make_pair(true, "");
53}
54
55int main(int argc, char* argv[])
56{
57 TCLAP::CmdLine cmd(
58 "Converts a double or floating point cell data array of a vtk "
59 "unstructured grid into a int or double cell data array.\n\n"
60 "OpenGeoSys-6 software, version " +
62 ".\n"
63 "Copyright (c) 2012-2024, OpenGeoSys Community "
64 "(http://www.opengeosys.org)",
66
67 TCLAP::ValueArg<std::string> new_property_data_type_arg(
68 "t",
69 "new-property-data-type",
70 "the name of the data type as string (int or double)",
71 false,
72 "int",
73 "data type as string");
74 cmd.add(new_property_data_type_arg);
75
76 TCLAP::ValueArg<std::string> new_property_arg(
77 "n",
78 "new-property-name",
79 "the name of the new cell data array (PropertyVector) the values are "
80 "stored",
81 false,
82 "MaterialIDs",
83 "name of the new cell data array (PropertyVector) as string");
84 cmd.add(new_property_arg);
85
86 TCLAP::ValueArg<std::string> out_mesh_arg(
87 "o", "out-mesh", "output mesh file name", true, "", "file name");
88 cmd.add(out_mesh_arg);
89
90 TCLAP::ValueArg<std::string> property_arg(
91 "e",
92 "existing-property-name",
93 "the name of the existing cell data array (PropertyVector)",
94 true,
95 "",
96 "property name as string");
97 cmd.add(property_arg);
98
99 TCLAP::ValueArg<std::string> mesh_arg(
100 "i", "in-mesh", "input mesh file name", true, "", "file name");
101 cmd.add(mesh_arg);
102
103 cmd.parse(argc, argv);
104
105 BaseLib::MPI::Setup mpi_setup(argc, argv);
106
107 std::unique_ptr<MeshLib::Mesh> mesh(
108 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
109
110 if (!mesh)
111 {
112 return -1;
113 }
114
115 bool success = false;
116 std::string err_msg = "Could not find cell data array '" +
117 property_arg.getValue() + "' in the mesh '" +
118 mesh_arg.getValue() + "'";
119
120 if (new_property_data_type_arg.getValue() == "int")
121 {
122 if (mesh->getProperties().existsPropertyVector<double>(
123 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
124 {
125 std::tie(success, err_msg) =
127 mesh->getProperties(),
128 property_arg.getValue(),
129 new_property_arg.getValue());
130 }
131
132 if (mesh->getProperties().existsPropertyVector<float>(
133 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
134 {
135 std::tie(success, err_msg) =
137 mesh->getProperties(),
138 property_arg.getValue(),
139 new_property_arg.getValue());
140 }
141 }
142 if (new_property_data_type_arg.getValue() == "double")
143 {
144 if (mesh->getProperties().existsPropertyVector<float>(
145 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
146 {
147 std::tie(success, err_msg) =
149 mesh->getProperties(),
150 property_arg.getValue(),
151 new_property_arg.getValue());
152 }
153 }
154
155 if (!success)
156 {
157 ERR("{:s}", err_msg);
158 return -1;
159 }
160
161 MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue());
162
163 return EXIT_SUCCESS;
164}
Git information.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Mesh class.
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition Properties.h:36
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
PropertyVector< T > const * getPropertyVector(std::string_view name) const
std::pair< bool, std::string > castPropertyVectorToPropertyVector(MeshLib::Properties &properties, std::string const &property_vector_name_in, std::string const &property_vector_name_out)
int main(int argc, char *argv[])
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.