OGS
convertVtkDataArrayToVtkDataArray.cpp File Reference

Detailed Description

Definition in file convertVtkDataArrayToVtkDataArray.cpp.

#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 27 of file convertVtkDataArrayToVtkDataArray.cpp.

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

57{
58 TCLAP::CmdLine cmd(
59 "Converts a double or floating point cell data array of a vtk "
60 "unstructured grid into a int or double cell data array.\n\n"
61 "OpenGeoSys-6 software, version " +
63 ".\n"
64 "Copyright (c) 2012-2025, OpenGeoSys Community "
65 "(http://www.opengeosys.org)",
67
68 std::vector<std::string> allowed_types_vector{"int", "double"};
69 TCLAP::ValuesConstraint<std::string> allowed_types(allowed_types_vector);
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",
74 false,
75 "int",
76 &allowed_types);
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 "NEW_PROP_NAME");
87 cmd.add(new_property_arg);
88
89 TCLAP::ValueArg<std::string> out_mesh_arg("o", "out-mesh",
90 "Output (.vtk) mesh file name",
91 true, "", "OUTPUT_FILE");
92 cmd.add(out_mesh_arg);
93
94 TCLAP::ValueArg<std::string> property_arg(
95 "e",
96 "existing-property-name",
97 "the name of the existing cell data array (PropertyVector)",
98 true,
99 "",
100 "EXISTING_PROP_NAME");
101 cmd.add(property_arg);
102
103 TCLAP::ValueArg<std::string> mesh_arg(
104 "i", "in-mesh", "Input (.vtk) mesh file name", true, "", "INPUT_FILE");
105 cmd.add(mesh_arg);
106
107 auto log_level_arg = BaseLib::makeLogLevelArg();
108 cmd.add(log_level_arg);
109 cmd.parse(argc, argv);
110
111 BaseLib::MPI::Setup mpi_setup(argc, argv);
112 BaseLib::initOGSLogger(log_level_arg.getValue());
113
114 std::unique_ptr<MeshLib::Mesh> mesh(
115 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
116
117 if (!mesh)
118 {
119 return -1;
120 }
121
122 bool success = false;
123 std::string err_msg = "Could not find cell data array '" +
124 property_arg.getValue() + "' in the mesh '" +
125 mesh_arg.getValue() + "'";
126
127 if (new_property_data_type_arg.getValue() == "int")
128 {
129 if (mesh->getProperties().existsPropertyVector<double>(
130 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
131 {
132 std::tie(success, err_msg) =
134 mesh->getProperties(),
135 property_arg.getValue(),
136 new_property_arg.getValue());
137 }
138
139 if (mesh->getProperties().existsPropertyVector<float>(
140 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
141 {
142 std::tie(success, err_msg) =
144 mesh->getProperties(),
145 property_arg.getValue(),
146 new_property_arg.getValue());
147 }
148 }
149 if (new_property_data_type_arg.getValue() == "double")
150 {
151 if (mesh->getProperties().existsPropertyVector<float>(
152 property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
153 {
154 std::tie(success, err_msg) =
156 mesh->getProperties(),
157 property_arg.getValue(),
158 new_property_arg.getValue());
159 }
160 }
161
162 if (!success)
163 {
164 ERR("{:s}", err_msg);
165 return -1;
166 }
167
168 MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue());
169
170 return EXIT_SUCCESS;
171}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
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:64
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().