OGS
convertVtkDataArrayToVtkDataArray.cpp File Reference
#include <tclap/CmdLine.h>
#include <algorithm>
#include <cmath>
#include <memory>
#include <numeric>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
#include "MeshLib/Mesh.h"
Include dependency graph for convertVtkDataArrayToVtkDataArray.cpp:

Go to the source code of this file.

Functions

template<typename T1, typename T2>
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[])

Function Documentation

◆ castPropertyVectorToPropertyVector()

template<typename T1, typename T2>
std::pair< bool, std::string > castPropertyVectorToPropertyVector ( MeshLib::Properties & properties,
std::string const & property_vector_name_in,
std::string const & property_vector_name_out )

Definition at line 20 of file convertVtkDataArrayToVtkDataArray.cpp.

24{
25 auto const* const orig_pv = properties.getPropertyVector<T1>(
26 property_vector_name_in, MeshLib::MeshItemType::Cell, 1);
27 if (!orig_pv)
28 {
29 return std::make_pair(false,
30 "Original property vector '" +
31 property_vector_name_in + "' not found.");
32 }
33 auto* new_pv = properties.createNewPropertyVector<T2>(
34 property_vector_name_out, MeshLib::MeshItemType::Cell,
35 orig_pv->getNumberOfTuples(), 1);
36 if (!new_pv)
37 {
38 return std::make_pair(false,
39 "Could not create new property vector '" +
40 property_vector_name_in + "' not found.");
41 }
42 for (std::size_t i(0); i < new_pv->getNumberOfTuples(); ++i)
43 {
44 (*new_pv)[i] = static_cast<T2>((*orig_pv)[i]);
45 }
46 return std::make_pair(true, "");
47}
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

References MeshLib::Cell, MeshLib::Properties::createNewPropertyVector(), and MeshLib::Properties::getPropertyVector().

Referenced by main().

◆ main()

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

Definition at line 49 of file convertVtkDataArrayToVtkDataArray.cpp.

50{
51 TCLAP::CmdLine cmd(
52 "Converts a double or floating point cell data array of a vtk "
53 "unstructured grid into a int or double cell data array.\n\n"
54 "OpenGeoSys-6 software, version " +
56 ".\n"
57 "Copyright (c) 2012-2026, OpenGeoSys Community "
58 "(http://www.opengeosys.org)",
60
61 std::vector<std::string> allowed_types_vector{"int", "double"};
62 TCLAP::ValuesConstraint<std::string> allowed_types(allowed_types_vector);
63 TCLAP::ValueArg<std::string> new_property_data_type_arg(
64 "t",
65 "new-property-data-type",
66 "the name of the data type as string",
67 false,
68 "int",
69 &allowed_types);
70 cmd.add(new_property_data_type_arg);
71
72 TCLAP::ValueArg<std::string> new_property_arg(
73 "n",
74 "new-property-name",
75 "the name of the new cell data array (PropertyVector) the values are "
76 "stored",
77 false,
78 "MaterialIDs",
79 "NEW_PROP_NAME");
80 cmd.add(new_property_arg);
81
82 TCLAP::ValueArg<std::string> out_mesh_arg("o", "out-mesh",
83 "Output (.vtk) mesh file name",
84 true, "", "OUTPUT_FILE");
85 cmd.add(out_mesh_arg);
86
87 TCLAP::ValueArg<std::string> property_arg(
88 "e",
89 "existing-property-name",
90 "the name of the existing cell data array (PropertyVector)",
91 true,
92 "",
93 "EXISTING_PROP_NAME");
94 cmd.add(property_arg);
95
96 TCLAP::ValueArg<std::string> mesh_arg(
97 "i", "in-mesh", "Input (.vtk) mesh file name", true, "", "INPUT_FILE");
98 cmd.add(mesh_arg);
99
100 auto log_level_arg = BaseLib::makeLogLevelArg();
101 cmd.add(log_level_arg);
102 cmd.parse(argc, argv);
103
104 BaseLib::MPI::Setup mpi_setup(argc, argv);
105 BaseLib::initOGSLogger(log_level_arg.getValue());
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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::pair< bool, std::string > castPropertyVectorToPropertyVector(MeshLib::Properties &properties, std::string const &property_vector_name_in, std::string const &property_vector_name_out)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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)

References castPropertyVectorToPropertyVector(), MeshLib::Cell, ERR(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), and MeshLib::IO::writeMeshToFile().