OGS 6.2.1-97-g73d1aeda3
ConvertToLinearMesh.cpp
Go to the documentation of this file.
1 
10 #include "ConvertToLinearMesh.h"
11 
13 #include "MeshLib/Elements/Hex.h"
14 #include "MeshLib/Elements/Line.h"
15 #include "MeshLib/Elements/Quad.h"
16 #include "MeshLib/Elements/Tet.h"
17 #include "MeshLib/Elements/Tri.h"
18 #include "MeshLib/Elements/Utils.h"
19 #include "MeshLib/Mesh.h"
21 #include "MeshLib/Node.h"
22 #include "MeshLib/Properties.h"
23 #include "MeshLib/PropertyVector.h"
24 
25 namespace MeshLib
26 {
27 namespace
28 {
29 template <typename T_ELEMENT>
31  std::vector<MeshLib::Node*> const& vec_new_nodes)
32 {
33  auto const n_base_nodes = T_ELEMENT::n_base_nodes;
34  auto** nodes = new MeshLib::Node*[n_base_nodes];
35  for (unsigned i = 0; i < e->getNumberOfBaseNodes(); i++)
36  {
37  auto const it = find_if(
38  begin(vec_new_nodes), end(vec_new_nodes),
39  [node_i = e->getNode(i)](Node* const new_node) {
40  return *node_i ==
41  *new_node; // coordinate comparison up to epsilon
42  });
43  if (it == end(vec_new_nodes))
44  {
45  OGS_FATAL(
46  "A base node %d (with original global node id %d) not found in "
47  "the list for element %d.",
48  i, e->getNode(i)->getID(), e->getID());
49  }
50  nodes[i] = const_cast<MeshLib::Node*>(*it);
51  }
52  return new T_ELEMENT(nodes);
53 }
54 
55 } // unnamed namespace
56 
57 std::unique_ptr<MeshLib::Mesh> convertToLinearMesh(
58  MeshLib::Mesh const& org_mesh, std::string const& new_mesh_name)
59 {
60  std::vector<MeshLib::Node*> vec_new_nodes =
62 
63  // create new elements with the quadratic nodes
64  std::vector<MeshLib::Element*> vec_new_eles;
65  for (MeshLib::Element const* e : org_mesh.getElements())
66  {
67  if (e->getCellType() == MeshLib::CellType::LINE3)
68  {
69  vec_new_eles.push_back(
70  createLinearElement<MeshLib::Line>(e, vec_new_nodes));
71  }
72  else if (e->getCellType() == MeshLib::CellType::QUAD8)
73  {
74  vec_new_eles.push_back(
75  createLinearElement<MeshLib::Quad>(e, vec_new_nodes));
76  }
77  else if (e->getCellType() == MeshLib::CellType::TRI6)
78  {
79  vec_new_eles.push_back(
80  createLinearElement<MeshLib::Tri>(e, vec_new_nodes));
81  }
82  else if (e->getCellType() == MeshLib::CellType::HEX20)
83  {
84  vec_new_eles.push_back(
85  createLinearElement<MeshLib::Hex>(e, vec_new_nodes));
86  }
87  else if (e->getCellType() == MeshLib::CellType::TET10)
88  {
89  vec_new_eles.push_back(
90  createLinearElement<MeshLib::Tet>(e, vec_new_nodes));
91  }
92  else
93  {
94  OGS_FATAL("Mesh element type %s is not supported",
95  MeshLib::CellType2String(e->getCellType()).c_str());
96  }
97  }
98 
99  auto new_mesh = std::make_unique<MeshLib::Mesh>(
100  new_mesh_name, vec_new_nodes, vec_new_eles,
102  std::vector<MeshLib::MeshItemType>(1,
104 
105  // copy property vectors for nodes
106  MeshLib::Properties const& src_properties = org_mesh.getProperties();
107  for (auto name : src_properties.getPropertyVectorNames())
108  {
109  if (!src_properties.existsPropertyVector<double>(name))
110  {
111  continue;
112  }
113  auto const* src_prop = src_properties.getPropertyVector<double>(name);
114  if (src_prop->getMeshItemType() != MeshLib::MeshItemType::Node)
115  {
116  continue;
117  }
118 
119  auto const n_src_comp = src_prop->getNumberOfComponents();
120  auto new_prop =
121  new_mesh->getProperties().createNewPropertyVector<double>(
122  name, MeshLib::MeshItemType::Node, n_src_comp);
123  new_prop->resize(new_mesh->getNumberOfNodes() * n_src_comp);
124 
125  // copy only base node values
126  for (unsigned i = 0; i < org_mesh.getNumberOfBaseNodes(); i++)
127  {
128  for (int j = 0; j < n_src_comp; j++)
129  {
130  (*new_prop)[i * n_src_comp + j] =
131  (*src_prop)[i * n_src_comp + j];
132  }
133  }
134  }
135 
136  return new_mesh;
137 }
138 
139 } // end namespace MeshLib
virtual unsigned getNumberOfBaseNodes() const =0
std::vector< Node * > getBaseNodes(std::vector< Element *> const &elements)
Definition: Utils.h:25
Definition of the Line class.
std::size_t getNumberOfBaseNodes() const
Get the number of base nodes.
Definition: Mesh.h:126
Definition of the Node class.
Definition of the Tet class.
MeshLib::Properties & getProperties()
Definition: Mesh.h:134
Definition of the Mesh class.
std::size_t getID() const
Definition: Point3dWithID.h:62
std::unique_ptr< MeshLib::Mesh > convertToLinearMesh(MeshLib::Mesh const &org_mesh, std::string const &new_mesh_name)
Definition of the Tri class.
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties...
Definition: Properties.h:37
Properties excludeCopyProperties(std::vector< std::size_t > const &exclude_elem_ids, std::vector< std::size_t > const &exclude_node_ids) const
Definition: Properties.cpp:61
Interface for heuristic search length strategy.
Definition: ProjectData.h:30
Definition of Duplicate functions.
std::vector< MeshLib::Node * > copyNodeVector(const std::vector< MeshLib::Node *> &nodes)
Creates a deep copy of a Node vector.
PropertyVector< T > const * getPropertyVector(std::string const &name) const
Definition: Properties.h:119
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:108
Definition of the Quad class.
Definition of the class Properties that implements a container of properties.
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:90
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
Definition of the Element class.
Definition of the Hex class.
T_ELEMENT * createLinearElement(MeshLib::Element const *e, std::vector< MeshLib::Node *> const &vec_new_nodes)
const Node * getNode(unsigned i) const
Definition: Element.cpp:158
std::string CellType2String(const CellType t)
Given a MeshElemType this returns the appropriate string.
Definition: MeshEnums.cpp:153