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/MPI.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 26 of file convertVtkDataArrayToVtkDataArray.cpp.

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, 1);
41 if (!new_pv)
42 {
43 return std::make_pair(false,
44 "Could not create new property vector '" +
45 property_vector_name_in + "' not found.");
46 }
47 new_pv->resize(orig_pv->getNumberOfTuples());
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}
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 55 of file convertVtkDataArrayToVtkDataArray.cpp.

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-2024, OpenGeoSys Community "
64 "(http://www.opengeosys.org)",
66
67 TCLAP::ValueArg<std::string> new_property_data_type_arg(
68 "t",
69 "new-property-data-type",
70 "the name of the data type as string (int or double)",
71 false,
72 "int",
73 "data type as string");
74 cmd.add(new_property_data_type_arg);
75
76 TCLAP::ValueArg<std::string> new_property_arg(
77 "n",
78 "new-property-name",
79 "the name of the new cell data array (PropertyVector) the values are "
80 "stored",
81 false,
82 "MaterialIDs",
83 "name of the new cell data array (PropertyVector) as string");
84 cmd.add(new_property_arg);
85
86 TCLAP::ValueArg<std::string> out_mesh_arg(
87 "o", "out-mesh", "output mesh file name", true, "", "file name");
88 cmd.add(out_mesh_arg);
89
90 TCLAP::ValueArg<std::string> property_arg(
91 "e",
92 "existing-property-name",
93 "the name of the existing cell data array (PropertyVector)",
94 true,
95 "",
96 "property name as string");
97 cmd.add(property_arg);
98
99 TCLAP::ValueArg<std::string> mesh_arg(
100 "i", "in-mesh", "input mesh file name", true, "", "file name");
101 cmd.add(mesh_arg);
102
103 cmd.parse(argc, argv);
104
105 BaseLib::MPI::Setup mpi_setup(argc, argv);
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:45
std::pair< bool, std::string > castPropertyVectorToPropertyVector(MeshLib::Properties &properties, std::string const &property_vector_name_in, std::string const &property_vector_name_out)
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(), GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), and MeshLib::IO::writeMeshToFile().