OGS
ReorderMesh.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <memory>
13#include <string>
14
15#include "InfoLib/GitInfo.h"
19#include "MeshLib/Mesh.h"
20#include "MeshLib/Node.h"
21
22std::vector<std::size_t> generateBulkIDsReverseMapping(
23 std::vector<std::size_t> const& bulk_ids)
24{
25 std::vector<std::size_t> parallel_to_sequential_node_ids(bulk_ids.size());
26
27 std::size_t pos = 0;
28 for (auto const id : bulk_ids)
29 {
30 parallel_to_sequential_node_ids[id] = pos;
31 pos++;
32 }
33 return parallel_to_sequential_node_ids;
34}
35
36template <typename T>
38 std::vector<std::size_t> const& bulk_ids,
39 MeshLib::PropertyVector<T>& reordered_pv)
40{
41 if (original_pv.getNumberOfGlobalComponents() !=
42 reordered_pv.getNumberOfGlobalComponents())
43 {
45 "Could not reorder property {} since the number of components "
46 "isn't equal ({} != {}).",
47 original_pv.getPropertyName(),
48 original_pv.getNumberOfGlobalComponents(),
49 reordered_pv.getNumberOfGlobalComponents());
50 }
51
52 auto const number_of_components = original_pv.getNumberOfGlobalComponents();
53 std::size_t pos = 0;
54 for (auto const& id : bulk_ids)
55 {
56 for (int c = 0; c < number_of_components; c++)
57 {
58 reordered_pv.getComponent(id, c) = original_pv.getComponent(pos, c);
59 }
60 pos++;
61 }
62 return true;
63}
64
65template <typename T>
66bool reorderProperty(MeshLib::Properties const& original_properties,
67 std::vector<std::size_t> const& bulk_ids,
68 std::string const& property_name,
69 MeshLib::Properties& reordered_properties)
70{
71 if (!reordered_properties.existsPropertyVector<T>(property_name))
72 {
73 return false;
74 }
75 auto const& original_property =
76 *original_properties.getPropertyVector<T>(property_name);
77 auto& reordered_property =
78 *reordered_properties.getPropertyVector<T>(property_name);
79 return reorderProperty(original_property, bulk_ids, reordered_property);
80}
81
82void reorderProperties(MeshLib::Properties const& original_properties,
83 std::vector<std::size_t> const& bulk_ids,
84 std::vector<std::string> const& property_names,
85 MeshLib::Properties& reordered_properties)
86{
87 for (auto const& property_name : property_names)
88 {
89 bool const success =
90 reorderProperty<double>(original_properties, bulk_ids,
91 property_name, reordered_properties) ||
92 reorderProperty<float>(original_properties, bulk_ids, property_name,
93 reordered_properties) ||
94 reorderProperty<int>(original_properties, bulk_ids, property_name,
95 reordered_properties) ||
96 reorderProperty<long>(original_properties, bulk_ids, property_name,
97 reordered_properties) ||
98 reorderProperty<unsigned>(original_properties, bulk_ids,
99 property_name, reordered_properties) ||
100 reorderProperty<unsigned long>(original_properties, bulk_ids,
101 property_name,
102 reordered_properties) ||
103 reorderProperty<std::size_t>(original_properties, bulk_ids,
104 property_name, reordered_properties) ||
105 reorderProperty<char>(original_properties, bulk_ids, property_name,
106 reordered_properties) ||
107 reorderProperty<unsigned char>(original_properties, bulk_ids,
108 property_name, reordered_properties);
109 if (!success)
110 {
111 OGS_FATAL("Could not reorder PropertyVector '{}'.", property_name);
112 }
113 }
114}
115
116int main(int argc, char* argv[])
117{
118 TCLAP::CmdLine cmd(
119 "Reorder mesh nodes and cells.\n\n"
120 "OpenGeoSys-6 software, version " +
122 ".\n"
123 "Copyright (c) 2012-2024, OpenGeoSys Community "
124 "(http://www.opengeosys.org)",
126 TCLAP::ValueArg<std::string> mesh_in_arg(
127 "i", "input", "name of the input mesh file", true, "", "string");
128 cmd.add(mesh_in_arg);
129 TCLAP::ValueArg<std::string> mesh_out_arg(
130 "o", "output", "name of the output mesh file", true, "", "string");
131 cmd.add(mesh_out_arg);
132
133 cmd.parse(argc, argv);
134
135 const std::string filename(mesh_in_arg.getValue());
136
137 // read the mesh file
138 auto const mesh =
139 std::unique_ptr<MeshLib::Mesh>(MeshLib::IO::readMeshFromFile(filename));
140 if (!mesh)
141 {
142 return EXIT_FAILURE;
143 }
144
145 auto const* bulk_node_ids = MeshLib::bulkNodeIDs(*mesh);
146 if (!bulk_node_ids)
147 {
148 OGS_FATAL("Property / data array '{}' has not been found in the mesh.",
150 }
151
152 auto const& nodes = mesh->getNodes();
153 auto const node_ids_reverse_mapping(
154 generateBulkIDsReverseMapping(*bulk_node_ids));
155
156 std::vector<MeshLib::Node*> reordered_nodes(nodes.size());
157 std::size_t pos = 0;
158 for (auto const id : node_ids_reverse_mapping)
159 {
160 auto const& n = *(nodes[id]);
161 reordered_nodes[pos] = new MeshLib::Node(n[0], n[1], n[2], pos);
162 pos++;
163 }
164
165 auto const bulk_element_ids_string =
167 if (!mesh->getProperties().existsPropertyVector<std::size_t>(
168 bulk_element_ids_string))
169 {
170 OGS_FATAL("Property / data array '{}' has not been found in the mesh.",
171 bulk_element_ids_string);
172 }
173 auto const& bulk_element_ids =
174 *mesh->getProperties().getPropertyVector<std::size_t>(
175 bulk_element_ids_string);
176
177 std::vector<std::size_t> element_ids_reverse_mapping(
178 generateBulkIDsReverseMapping(bulk_element_ids));
179
180 auto const& elements = mesh->getElements();
181 std::vector<MeshLib::Element*> reordered_elements(elements.size());
182 pos = 0;
183 for (auto const id : element_ids_reverse_mapping)
184 {
185 auto const& e = *elements[id];
186 auto* reordered_element =
187 e.clone(); // use clone to have the same element type
188 // reset the nodes to the reordered nodes
189 auto const number_of_nodes = reordered_element->getNumberOfNodes();
190 for (std::size_t node_number = 0; node_number < number_of_nodes;
191 node_number++)
192 {
193 reordered_element->setNode(
194 node_number,
195 reordered_nodes[(
196 *bulk_node_ids)[e.getNode(node_number)->getID()]]);
197 }
198 reordered_elements[pos] = reordered_element;
199 pos++;
200 }
201 auto const& original_properties = mesh->getProperties();
202 // reordering of PropertyVector with following MeshItemTypes isn't supported
203 std::vector<MeshLib::MeshItemType> const exclude_property_vectors{
206 // Create reordered mesh - only the PropertyVectors for which the reordering
207 // is implemented are copied
208 MeshLib::Mesh reordered_mesh{
209 "reordered_mesh", reordered_nodes, reordered_elements,
210 false /* compute_element_neighbors */,
211 original_properties.excludeCopyProperties(exclude_property_vectors)};
212 auto& properties = reordered_mesh.getProperties();
213
214 // node based properties
215 auto const node_property_names =
216 mesh->getProperties().getPropertyVectorNames(
218 reorderProperties(original_properties, *bulk_node_ids, node_property_names,
219 properties);
220
221 // element based properties
222 auto const element_property_names =
223 mesh->getProperties().getPropertyVectorNames(
225 reorderProperties(original_properties, bulk_element_ids,
226 element_property_names, properties);
227 MeshLib::IO::writeMeshToFile(reordered_mesh, mesh_out_arg.getValue());
228
229 auto const number_of_properties =
230 mesh->getProperties().getPropertyVectorNames().size();
231 if (node_property_names.size() + element_property_names.size() <
232 number_of_properties)
233 {
234 WARN(
235 "Properties that are not assigened to nodes or elements are not "
236 "transferred to the reordered mesh.");
237 }
238
239 return EXIT_SUCCESS;
240}
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
Git information.
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the Mesh class.
Definition of the Node class.
int main(int argc, char *argv[])
void reorderProperties(MeshLib::Properties const &original_properties, std::vector< std::size_t > const &bulk_ids, std::vector< std::string > const &property_names, MeshLib::Properties &reordered_properties)
std::vector< std::size_t > generateBulkIDsReverseMapping(std::vector< std::size_t > const &bulk_ids)
bool reorderProperty(MeshLib::PropertyVector< T > const &original_pv, std::vector< std::size_t > const &bulk_ids, MeshLib::PropertyVector< T > &reordered_pv)
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition Properties.h:36
bool existsPropertyVector(std::string_view name) const
PropertyVector< T > const * getPropertyVector(std::string_view name) const
int getNumberOfGlobalComponents() const
std::string const & getPropertyName() const
PROP_VAL_TYPE & getComponent(std::size_t tuple_index, int component)
Returns the value for the given component stored in the given tuple.
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)
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:188
PropertyVector< std::size_t > const * bulkNodeIDs(Mesh const &mesh)
Definition Mesh.cpp:292
Definition of readMeshFromFile function.