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,
41 orig_pv->getNumberOfTuples(), 1);
42 if (!new_pv)
43 {
44 return std::make_pair(false,
45 "Could not create new property vector '" +
46 property_vector_name_in + "' not found.");
47 }
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-2025, OpenGeoSys Community "
64 "(http://www.opengeosys.org)",
66
67 std::vector<std::string> allowed_types_vector{"int", "double"};
68 TCLAP::ValuesConstraint<std::string> allowed_types(allowed_types_vector);
69 TCLAP::ValueArg<std::string> new_property_data_type_arg(
70 "t",
71 "new-property-data-type",
72 "the name of the data type as string",
73 false,
74 "int",
75 &allowed_types);
76 cmd.add(new_property_data_type_arg);
77
78 TCLAP::ValueArg<std::string> new_property_arg(
79 "n",
80 "new-property-name",
81 "the name of the new cell data array (PropertyVector) the values are "
82 "stored",
83 false,
84 "MaterialIDs",
85 "NEW_PROP_NAME");
86 cmd.add(new_property_arg);
87
88 TCLAP::ValueArg<std::string> out_mesh_arg("o", "out-mesh",
89 "Output (.vtk) mesh file name",
90 true, "", "OUTPUT_FILE");
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 "EXISTING_PROP_NAME");
100 cmd.add(property_arg);
101
102 TCLAP::ValueArg<std::string> mesh_arg(
103 "i", "in-mesh", "Input (.vtk) mesh file name", true, "", "INPUT_FILE");
104 cmd.add(mesh_arg);
105
106 cmd.parse(argc, argv);
107
108 BaseLib::MPI::Setup mpi_setup(argc, argv);
109
110 std::unique_ptr<MeshLib::Mesh> mesh(
111 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
112
113 if (!mesh)
114 {
115 return -1;
116 }
117
118 bool success = false;
119 std::string err_msg = "Could not find cell data array '" +
120 property_arg.getValue() + "' in the mesh '" +
121 mesh_arg.getValue() + "'";
122
123 if (new_property_data_type_arg.getValue() == "int")
124 {
125 if (mesh->getProperties().existsPropertyVector<double>(
126 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
127 {
128 std::tie(success, err_msg) =
130 mesh->getProperties(),
131 property_arg.getValue(),
132 new_property_arg.getValue());
133 }
134
135 if (mesh->getProperties().existsPropertyVector<float>(
136 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
137 {
138 std::tie(success, err_msg) =
140 mesh->getProperties(),
141 property_arg.getValue(),
142 new_property_arg.getValue());
143 }
144 }
145 if (new_property_data_type_arg.getValue() == "double")
146 {
147 if (mesh->getProperties().existsPropertyVector<float>(
148 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
149 {
150 std::tie(success, err_msg) =
152 mesh->getProperties(),
153 property_arg.getValue(),
154 new_property_arg.getValue());
155 }
156 }
157
158 if (!success)
159 {
160 ERR("{:s}", err_msg);
161 return -1;
162 }
163
164 MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue());
165
166 return EXIT_SUCCESS;
167}
Git information.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of the Mesh class.
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition Properties.h:33
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.