OGS
convertVtkDataArrayToVtkDataArray.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 <algorithm>
19#include <cmath>
20#include <memory>
21#include <numeric>
22
23#include "InfoLib/GitInfo.h"
26#include "MeshLib/Mesh.h"
27
28template <typename T1, typename T2>
29std::pair<bool, std::string> castPropertyVectorToPropertyVector(
30 MeshLib::Properties& properties,
31 std::string const& property_vector_name_in,
32 std::string const& property_vector_name_out)
33{
34 auto const* const orig_pv = properties.getPropertyVector<T1>(
35 property_vector_name_in, MeshLib::MeshItemType::Cell, 1);
36 if (!orig_pv)
37 {
38 return std::make_pair(false,
39 "Original property vector '" +
40 property_vector_name_in + "' not found.");
41 }
42 auto* new_pv = properties.createNewPropertyVector<T2>(
43 property_vector_name_out, MeshLib::MeshItemType::Cell, 1);
44 if (!new_pv)
45 {
46 return std::make_pair(false,
47 "Could not create new property vector '" +
48 property_vector_name_in + "' not found.");
49 }
50 new_pv->resize(orig_pv->getNumberOfTuples());
51 for (std::size_t i(0); i < new_pv->getNumberOfTuples(); ++i)
52 {
53 (*new_pv)[i] = static_cast<T2>((*orig_pv)[i]);
54 }
55 return std::make_pair(true, "");
56}
57
58int main(int argc, char* argv[])
59{
60 TCLAP::CmdLine cmd(
61 "Converts a double or floating point cell data array of a vtk "
62 "unstructured grid into a int or double cell data array.\n\n"
63 "OpenGeoSys-6 software, version " +
65 ".\n"
66 "Copyright (c) 2012-2024, OpenGeoSys Community "
67 "(http://www.opengeosys.org)",
69
70 TCLAP::ValueArg<std::string> new_property_data_type_arg(
71 "t",
72 "new-property-data-type",
73 "the name of the data type as string (int or double)",
74 false,
75 "int",
76 "data type as string");
77 cmd.add(new_property_data_type_arg);
78
79 TCLAP::ValueArg<std::string> new_property_arg(
80 "n",
81 "new-property-name",
82 "the name of the new cell data array (PropertyVector) the values are "
83 "stored",
84 false,
85 "MaterialIDs",
86 "name of the new cell data array (PropertyVector) as string");
87 cmd.add(new_property_arg);
88
89 TCLAP::ValueArg<std::string> out_mesh_arg(
90 "o", "out-mesh", "output mesh file name", true, "", "file name");
91 cmd.add(out_mesh_arg);
92
93 TCLAP::ValueArg<std::string> property_arg(
94 "e",
95 "existing-property-name",
96 "the name of the existing cell data array (PropertyVector)",
97 true,
98 "",
99 "property name as string");
100 cmd.add(property_arg);
101
102 TCLAP::ValueArg<std::string> mesh_arg(
103 "i", "in-mesh", "input mesh file name", true, "", "file name");
104 cmd.add(mesh_arg);
105
106 cmd.parse(argc, argv);
107
108#ifdef USE_PETSC
109 MPI_Init(&argc, &argv);
110#endif
111
112 std::unique_ptr<MeshLib::Mesh> mesh(
113 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
114
115 if (!mesh)
116 {
117#ifdef USE_PETSC
118 MPI_Finalize();
119#endif
120 return -1;
121 }
122
123 bool success = false;
124 std::string err_msg = "Could not find cell data array '" +
125 property_arg.getValue() + "' in the mesh '" +
126 mesh_arg.getValue() + "'";
127
128 if (new_property_data_type_arg.getValue() == "int")
129 {
130 if (mesh->getProperties().existsPropertyVector<double>(
131 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
132 {
133 std::tie(success, err_msg) =
134 castPropertyVectorToPropertyVector<double, int>(
135 mesh->getProperties(),
136 property_arg.getValue(),
137 new_property_arg.getValue());
138 }
139
140 if (mesh->getProperties().existsPropertyVector<float>(
141 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
142 {
143 std::tie(success, err_msg) =
144 castPropertyVectorToPropertyVector<float, int>(
145 mesh->getProperties(),
146 property_arg.getValue(),
147 new_property_arg.getValue());
148 }
149 }
150 if (new_property_data_type_arg.getValue() == "double")
151 {
152 if (mesh->getProperties().existsPropertyVector<float>(
153 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
154 {
155 std::tie(success, err_msg) =
156 castPropertyVectorToPropertyVector<float, double>(
157 mesh->getProperties(),
158 property_arg.getValue(),
159 new_property_arg.getValue());
160 }
161 }
162
163 if (!success)
164 {
165 ERR("{:s}", err_msg);
166#ifdef USE_PETSC
167 MPI_Finalize();
168#endif
169 return -1;
170 }
171
172 MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue());
173
174#ifdef USE_PETSC
175 MPI_Finalize();
176#endif
177 return EXIT_SUCCESS;
178}
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.