OGS
NodeReordering.cpp
Go to the documentation of this file.
1 
12 #include <tclap/CmdLine.h>
13 
14 #include <algorithm>
15 #include <array>
16 #include <memory>
17 #include <vector>
18 
19 #include "BaseLib/Algorithm.h"
20 #include "InfoLib/GitInfo.h"
24 #include "MeshLib/Mesh.h"
25 #include "MeshLib/Node.h"
26 
29 void reorderNodes(std::vector<MeshLib::Element*>& elements)
30 {
31  std::size_t n_corrected_elements = 0;
32  std::size_t const nElements(elements.size());
33  for (std::size_t i = 0; i < nElements; ++i)
34  {
35  if (elements[i]->testElementNodeOrder())
36  {
37  continue;
38  }
39  n_corrected_elements++;
40 
41  const unsigned nElemNodes(elements[i]->getNumberOfBaseNodes());
42  std::vector<MeshLib::Node*> nodes(elements[i]->getNodes(),
43  elements[i]->getNodes() + nElemNodes);
44 
45  switch (elements[i]->getGeomType())
46  {
48  for (std::size_t j = 0; j < 4; ++j)
49  {
50  elements[i]->setNode(j, nodes[(j + 1) % 4]);
51  }
52  break;
54  elements[i]->setNode(0, nodes[1]);
55  elements[i]->setNode(1, nodes[0]);
56  elements[i]->setNode(2, nodes[3]);
57  elements[i]->setNode(3, nodes[2]);
58  break;
60  for (std::size_t j = 0; j < 3; ++j)
61  {
62  elements[i]->setNode(j, nodes[j + 3]);
63  elements[i]->setNode(j + 3, nodes[j]);
64  }
65  break;
67  for (std::size_t j = 0; j < 4; ++j)
68  {
69  elements[i]->setNode(j, nodes[j + 4]);
70  elements[i]->setNode(j + 4, nodes[j]);
71  }
72  break;
73  default:
74  for (std::size_t j = 0; j < nElemNodes; ++j)
75  {
76  elements[i]->setNode(j, nodes[nElemNodes - j - 1]);
77  }
78  }
79  }
80 
81  INFO("Corrected {:d} elements.", n_corrected_elements);
82 }
83 
86 void reorderNodes2(std::vector<MeshLib::Element*>& elements)
87 {
88  std::size_t const nElements(elements.size());
89  for (std::size_t i = 0; i < nElements; ++i)
90  {
91  const unsigned nElemNodes(elements[i]->getNumberOfBaseNodes());
92  std::vector<MeshLib::Node*> nodes(elements[i]->getNodes(),
93  elements[i]->getNodes() + nElemNodes);
94 
95  for (std::size_t j = 0; j < nElemNodes; ++j)
96  {
97  if (elements[i]->getGeomType() == MeshLib::MeshElemType::PRISM)
98  {
99  for (std::size_t k = 0; k < 3; ++k)
100  {
101  elements[i]->setNode(k, nodes[k + 3]);
102  elements[i]->setNode(k + 3, nodes[k]);
103  }
104  break;
105  }
106  }
107  }
108 }
109 
111 {
112  std::vector<MeshLib::Node*> base_nodes;
113  std::vector<MeshLib::Node*> nonlinear_nodes;
114  for (MeshLib::Element const* e : mesh.getElements())
115  {
116  for (unsigned i = 0; i < e->getNumberOfBaseNodes(); i++)
117  {
118  base_nodes.push_back(const_cast<MeshLib::Node*>(e->getNode(i)));
119  }
120  for (unsigned i = e->getNumberOfBaseNodes(); i < e->getNumberOfNodes();
121  i++)
122  {
123  nonlinear_nodes.push_back(
124  const_cast<MeshLib::Node*>(e->getNode(i)));
125  }
126  }
127 
128  BaseLib::makeVectorUnique(base_nodes,
129  [](MeshLib::Node* a, MeshLib::Node* b)
130  { return a->getID() < b->getID(); });
131  BaseLib::makeVectorUnique(nonlinear_nodes,
132  [](MeshLib::Node* a, MeshLib::Node* b)
133  { return a->getID() < b->getID(); });
134 
135  std::vector<MeshLib::Node*>& allnodes =
136  const_cast<std::vector<MeshLib::Node*>&>(mesh.getNodes());
137  allnodes.clear();
138 
139  allnodes.insert(allnodes.end(), base_nodes.begin(), base_nodes.end());
140  allnodes.insert(allnodes.end(), nonlinear_nodes.begin(),
141  nonlinear_nodes.end());
142 
143  mesh.resetNodeIDs();
144 }
145 
146 int main(int argc, char* argv[])
147 {
148  TCLAP::CmdLine cmd(
149  "Reorders mesh nodes in elements to make old or incorrectly ordered "
150  "meshes compatible with OGS6.\n"
151  "Three options are available:\n"
152  "Method 1: Re-ordering between DataExplorer 5 and DataExplorer 6 "
153  "(mostly reverses order of the nodes for all element types\n"
154  "Method 2: Re-ordering after introducing InSitu-Lib to OGS6 (only "
155  "adjusts to top and bottom surfaces of prism elements\n"
156  "Method 3: Re-ordering of mesh node vector such that all base nodes "
157  "are sorted before all nonlinear nodes.\n\n"
158  "OpenGeoSys-6 software, version " +
160  ".\n"
161  "Copyright (c) 2012-2021, OpenGeoSys Community "
162  "(http://www.opengeosys.org)",
164 
165  std::vector<int> method_ids{1, 2, 3};
166  TCLAP::ValuesConstraint<int> allowed_values(method_ids);
167  TCLAP::ValueArg<int> method_arg("m", "method",
168  "reordering method selection", false, 1,
169  &allowed_values);
170  cmd.add(method_arg);
171  TCLAP::ValueArg<std::string> output_mesh_arg(
172  "o", "output_mesh", "the name of the output mesh file", true, "",
173  "filename");
174  cmd.add(output_mesh_arg);
175  TCLAP::ValueArg<std::string> input_mesh_arg(
176  "i", "input_mesh", "the name of the input mesh file", true, "",
177  "filename");
178  cmd.add(input_mesh_arg);
179  cmd.parse(argc, argv);
180 
181  std::unique_ptr<MeshLib::Mesh> mesh(
182  MeshLib::IO::readMeshFromFile(input_mesh_arg.getValue()));
183 
184  if (!mesh)
185  {
186  return EXIT_FAILURE;
187  }
188 
189  INFO("Reordering nodes... ");
190  if (!method_arg.isSet() || method_arg.getValue() == 1)
191  {
192  reorderNodes(
193  const_cast<std::vector<MeshLib::Element*>&>(mesh->getElements()));
194  }
195  else if (method_arg.getValue() == 2)
196  {
198  const_cast<std::vector<MeshLib::Element*>&>(mesh->getElements()));
199  }
200  else if (method_arg.getValue() == 3)
201  {
202  reorderNonlinearNodes(*mesh);
203  }
204 
205  MeshLib::IO::writeMeshToFile(*mesh, output_mesh_arg.getValue());
206 
207  INFO("VTU file written.");
208 
209  return EXIT_SUCCESS;
210 }
Definition of the Element class.
Git information.
std::vector< std::size_t > getNodes(GeoLib::Point const &pnt, std::vector< MeshLib::Node * > const &nodes, MeshLib::PropertyVector< int > const &mat_ids, std::pair< int, int > const &mat_limits, std::pair< double, double > const &elevation_limits, MeshLib::Mesh const &mesh)
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
Definition of the Mesh class.
int main(int argc, char *argv[])
void reorderNodes(std::vector< MeshLib::Element * > &elements)
void reorderNodes2(std::vector< MeshLib::Element * > &elements)
void reorderNonlinearNodes(MeshLib::Mesh &mesh)
Definition of the Node class.
std::size_t getID() const
Definition: Point3dWithID.h:62
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:95
void resetNodeIDs()
Resets the IDs of all mesh-nodes to their position in the node vector.
Definition: Mesh.cpp:136
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
void makeVectorUnique(std::vector< T > &v)
Definition: Algorithm.h:209
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, [[maybe_unused]] std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.