OGS
convertVtkDataArrayToVtkDataArray.cpp File Reference

Detailed Description

Definition in file convertVtkDataArrayToVtkDataArray.cpp.

#include <tclap/CmdLine.h>
#include <mpi.h>
#include <algorithm>
#include <cmath>
#include <memory>
#include <numeric>
#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 29 of file convertVtkDataArrayToVtkDataArray.cpp.

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}
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().

◆ main()

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

Definition at line 58 of file convertVtkDataArrayToVtkDataArray.cpp.

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}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
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 MeshLib::Cell, ERR(), GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), and MeshLib::IO::writeMeshToFile().