Loading [MathJax]/extensions/tex2jax.js
OGS
ConvertToLinearMesh.cpp
Go to the documentation of this file.
1 
11 #include "ConvertToLinearMesh.h"
12 
14 #include "MeshLib/Elements/Hex.h"
15 #include "MeshLib/Elements/Line.h"
16 #include "MeshLib/Elements/Quad.h"
17 #include "MeshLib/Elements/Tet.h"
18 #include "MeshLib/Elements/Tri.h"
19 #include "MeshLib/Elements/Utils.h"
20 #include "MeshLib/Mesh.h"
22 #include "MeshLib/Node.h"
23 #include "MeshLib/Properties.h"
24 #include "MeshLib/PropertyVector.h"
25 
26 namespace MeshLib
27 {
28 namespace
29 {
30 template <typename T_ELEMENT>
32  std::vector<MeshLib::Node*> const& vec_new_nodes)
33 {
34  auto const n_base_nodes = T_ELEMENT::n_base_nodes;
35  auto** nodes = new MeshLib::Node*[n_base_nodes];
36  for (unsigned i = 0; i < e->getNumberOfBaseNodes(); i++)
37  {
38  auto const it = find_if(
39  begin(vec_new_nodes), end(vec_new_nodes),
40  [node_i = e->getNode(i)](Node* const new_node)
41  {
42  return *node_i ==
43  *new_node; // coordinate comparison up to epsilon
44  });
45  if (it == end(vec_new_nodes))
46  {
47  OGS_FATAL(
48  "A base node {:d} (with original global node id {:d}) not "
49  "found in the list for element {:d}.",
50  i, e->getNode(i)->getID(), e->getID());
51  }
52  nodes[i] = const_cast<MeshLib::Node*>(*it);
53  }
54  return new T_ELEMENT(nodes);
55 }
56 
57 } // unnamed namespace
58 
59 std::unique_ptr<MeshLib::Mesh> convertToLinearMesh(
60  MeshLib::Mesh const& org_mesh, std::string const& new_mesh_name)
61 {
62  std::vector<MeshLib::Node*> vec_new_nodes =
64 
65  // create new elements with the quadratic nodes
66  std::vector<MeshLib::Element*> vec_new_eles;
67  for (MeshLib::Element const* e : org_mesh.getElements())
68  {
69  if (e->getCellType() == MeshLib::CellType::LINE3)
70  {
71  vec_new_eles.push_back(
72  createLinearElement<MeshLib::Line>(e, vec_new_nodes));
73  }
74  else if (e->getCellType() == MeshLib::CellType::QUAD8)
75  {
76  vec_new_eles.push_back(
77  createLinearElement<MeshLib::Quad>(e, vec_new_nodes));
78  }
79  else if (e->getCellType() == MeshLib::CellType::TRI6)
80  {
81  vec_new_eles.push_back(
82  createLinearElement<MeshLib::Tri>(e, vec_new_nodes));
83  }
84  else if (e->getCellType() == MeshLib::CellType::HEX20)
85  {
86  vec_new_eles.push_back(
87  createLinearElement<MeshLib::Hex>(e, vec_new_nodes));
88  }
89  else if (e->getCellType() == MeshLib::CellType::TET10)
90  {
91  vec_new_eles.push_back(
92  createLinearElement<MeshLib::Tet>(e, vec_new_nodes));
93  }
94  else
95  {
96  OGS_FATAL("Mesh element type {:s} is not supported",
97  MeshLib::CellType2String(e->getCellType()));
98  }
99  }
100 
101  auto new_mesh = std::make_unique<MeshLib::Mesh>(
102  new_mesh_name, vec_new_nodes, vec_new_eles,
104  std::vector<MeshLib::MeshItemType>(1,
106 
107  // copy property vectors for nodes
108  for (auto [name, property] : org_mesh.getProperties())
109  {
110  if (property->getMeshItemType() != MeshLib::MeshItemType::Node)
111  {
112  continue;
113  }
114  auto double_property = dynamic_cast<PropertyVector<double>*>(property);
115  if (double_property == nullptr)
116  {
117  continue;
118  }
119  auto const n_src_comp = double_property->getNumberOfGlobalComponents();
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  (*double_property)[i * n_src_comp + j];
132  }
133  }
134  }
135 
136  return new_mesh;
137 }
138 
139 } // end namespace MeshLib
Definition of Duplicate functions.
Definition of the Element class.
#define OGS_FATAL(...)
Definition: Error.h:26
Definition of the Hex class.
Definition of the Line class.
Definition of the class Properties that implements a container of properties.
Definition of the Mesh class.
Definition of the Node class.
Definition of the Quad class.
Definition of the Tet class.
Definition of the Tri class.
std::size_t getID() const
Definition: Point3dWithID.h:62
virtual const Node * getNode(unsigned idx) const =0
virtual unsigned getNumberOfBaseNodes() const =0
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:82
std::size_t getNumberOfBaseNodes() const
Get the number of base nodes.
Definition: Mesh.cpp:214
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
Properties excludeCopyProperties(std::vector< std::size_t > const &exclude_elem_ids, std::vector< std::size_t > const &exclude_node_ids) const
Definition: Properties.cpp:58
int getNumberOfGlobalComponents() const
T_ELEMENT * createLinearElement(MeshLib::Element const *e, std::vector< MeshLib::Node * > const &vec_new_nodes)
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
std::string CellType2String(const CellType t)
Given a MeshElemType this returns the appropriate string.
Definition: MeshEnums.cpp:157
std::unique_ptr< MeshLib::Mesh > convertToLinearMesh(MeshLib::Mesh const &org_mesh, std::string const &new_mesh_name)
std::vector< Node * > getBaseNodes(std::vector< Element * > const &elements)
Definition: Utils.h:26