Loading [MathJax]/extensions/tex2jax.js
OGS
AddLayerToMesh.cpp
Go to the documentation of this file.
1
13#include "AddLayerToMesh.h"
14
15#include <algorithm>
16#include <map>
17#include <memory>
18#include <numeric>
19#include <range/v3/algorithm/max_element.hpp>
20#include <range/v3/view/any_view.hpp>
21#include <range/v3/view/common.hpp>
22#include <range/v3/view/concat.hpp>
23#include <range/v3/view/iota.hpp>
24#include <range/v3/view/repeat_n.hpp>
25#include <vector>
26
27#include "BaseLib/Logging.h"
28#include "FlipElements.h"
30#include "MeshLib/Mesh.h"
31#include "MeshLib/Node.h"
34
35namespace MeshToolsLib
36{
51 std::vector<MeshLib::Node*> const& subsfc_nodes,
52 MeshLib::Element const& sfc_elem,
53 MeshLib::PropertyVector<std::size_t> const& sfc_to_subsfc_id_map,
54 std::map<std::size_t, std::size_t> const& subsfc_sfc_id_map)
55{
56 if (sfc_elem.getDimension() > 2)
57 {
58 return nullptr;
59 }
60
61 const unsigned nElemNodes(sfc_elem.getNumberOfBaseNodes());
62 auto new_nodes =
63 std::unique_ptr<MeshLib::Node*[]>{new MeshLib::Node*[2 * nElemNodes]};
64
65 for (unsigned j = 0; j < nElemNodes; ++j)
66 {
67 std::size_t const subsfc_id(
68 sfc_to_subsfc_id_map[sfc_elem.getNode(j)->getID()]);
69 new_nodes[j] = subsfc_nodes[subsfc_id];
70 std::size_t new_idx = (nElemNodes == 2) ? (3 - j) : (nElemNodes + j);
71 new_nodes[new_idx] = subsfc_nodes[subsfc_sfc_id_map.at(subsfc_id)];
72 }
73
75 {
76 return new MeshLib::Quad(new_nodes.release());
77 }
79 {
80 return new MeshLib::Prism(new_nodes.release());
81 }
83 {
84 return new MeshLib::Hex(new_nodes.release());
85 }
86
87 return nullptr;
88}
89
90MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double const thickness,
91 std::string const& name, bool const on_top,
92 bool const copy_material_ids,
93 std::optional<int> const layer_id)
94{
95 INFO("Extracting top surface of mesh '{:s}' ... ", mesh.getName());
96 double const flag = on_top ? -1.0 : 1.0;
97 Eigen::Vector3d const dir({0, 0, flag});
98 double const angle(90);
99 std::unique_ptr<MeshLib::Mesh> sfc_mesh(nullptr);
100
101 auto const prop_name =
103
104 if (mesh.getDimension() == 3)
105 {
107 mesh, dir, angle, prop_name));
108 }
109 else
110 {
111 sfc_mesh = (on_top) ? std::make_unique<MeshLib::Mesh>(mesh)
112 : std::unique_ptr<MeshLib::Mesh>(
114 // add property storing node ids
115 auto* const pv =
116 sfc_mesh->getProperties().createNewPropertyVector<std::size_t>(
118 sfc_mesh->getNumberOfNodes(), 1);
119 if (pv)
120 {
121 pv->assign(ranges::views::iota(0u, sfc_mesh->getNumberOfNodes()));
122 }
123 else
124 {
125 ERR("Could not create and initialize property '{}'.", prop_name);
126 return nullptr;
127 }
128 }
129 INFO("done.");
130
131 // *** add new surface nodes
132 std::vector<MeshLib::Node*> subsfc_nodes =
134 std::vector<MeshLib::Element*> subsfc_elements =
135 MeshLib::copyElementVector(mesh.getElements(), subsfc_nodes);
136
137 std::size_t const n_subsfc_nodes(subsfc_nodes.size());
138
139 std::vector<MeshLib::Node*> const& sfc_nodes(sfc_mesh->getNodes());
140 std::size_t const n_sfc_nodes(sfc_nodes.size());
141
142 if (!sfc_mesh->getProperties().existsPropertyVector<std::size_t>(prop_name))
143 {
144 ERR("Need subsurface node ids, but the property '{:s}' is not "
145 "available.",
146 prop_name);
147 return nullptr;
148 }
149 // fetch subsurface node ids PropertyVector
150 auto const* const node_id_pv =
151 sfc_mesh->getProperties().getPropertyVector<std::size_t>(prop_name);
152
153 // *** copy sfc nodes to subsfc mesh node
154 std::map<std::size_t, std::size_t> subsfc_sfc_id_map;
155 for (std::size_t k(0); k < n_sfc_nodes; ++k)
156 {
157 std::size_t const subsfc_id((*node_id_pv)[k]);
158 std::size_t const sfc_id(k + n_subsfc_nodes);
159 subsfc_sfc_id_map.insert(std::make_pair(subsfc_id, sfc_id));
160 MeshLib::Node const& node(*sfc_nodes[k]);
161 subsfc_nodes.push_back(new MeshLib::Node(
162 node[0], node[1], node[2] - (flag * thickness), sfc_id));
163 }
164
165 // *** insert new layer elements into subsfc_mesh
166 std::vector<MeshLib::Element*> const& sfc_elements(sfc_mesh->getElements());
167 std::size_t const n_sfc_elements(sfc_elements.size());
168 for (std::size_t k(0); k < n_sfc_elements; ++k)
169 {
170 subsfc_elements.push_back(extrudeElement(
171 subsfc_nodes, *sfc_elements[k], *node_id_pv, subsfc_sfc_id_map));
172 }
173
174 auto new_mesh = new MeshLib::Mesh(name, subsfc_nodes, subsfc_elements,
175 true /* compute_element_neighbors */);
176
177 if (!mesh.getProperties().existsPropertyVector<int>("MaterialIDs"))
178 {
179 ERR("Could not copy the property 'MaterialIDs' since the original mesh "
180 "does not contain such a property.");
181 return new_mesh;
182 }
183 auto const* const materials =
184 mesh.getProperties().getPropertyVector<int>("MaterialIDs");
185
186 auto* const new_materials =
187 new_mesh->getProperties().createNewPropertyVector<int>(
188 "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
189 if (!new_materials)
190 {
191 ERR("Can not set material properties for new layer");
192 return new_mesh;
193 }
194
195 int next_material_id = 0; // default, if materials are not available.
196
197 if (materials != nullptr)
198 {
199 next_material_id = *ranges::max_element(*materials) + 1;
200 }
201
202 auto initial_values =
203 materials ? ranges::any_view<int const>{*materials}
204 : ranges::views::repeat_n(layer_id.value_or(next_material_id),
205 mesh.getNumberOfElements());
206
207 auto additional_values = [&]() -> ranges::any_view<int const>
208 {
209 if (copy_material_ids &&
210 sfc_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
211 {
212 return ranges::any_view<int const>{
213 *sfc_mesh->getProperties().getPropertyVector<int>(
214 "MaterialIDs")};
215 }
216 else
217 {
218 int const new_layer_id = layer_id.value_or(next_material_id);
219 auto const n_new_props =
220 subsfc_elements.size() - mesh.getNumberOfElements();
221 return ranges::views::repeat_n(new_layer_id, n_new_props);
222 }
223 }();
224
225 new_materials->assign(ranges::views::common(
226 ranges::views::concat(initial_values, additional_values)));
227 return new_mesh;
228}
229
230} // namespace MeshToolsLib
Definition of AddLayerToMesh class.
Definition of Duplicate functions.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the MeshSurfaceExtraction class.
Definition of the Mesh class.
Definition of the Node class.
std::size_t getID() const
virtual MeshElemType getGeomType() const =0
virtual unsigned getNumberOfBaseNodes() const =0
virtual const Node * getNode(unsigned idx) const =0
virtual constexpr unsigned getDimension() const =0
Get dimension of the mesh element.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:108
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:90
Properties & getProperties()
Definition Mesh.h:136
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:105
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:99
bool existsPropertyVector(std::string_view name) const
Definition Properties.h:93
PropertyVector< T > const * getPropertyVector(std::string_view name) const
static MeshLib::Mesh * getMeshSurface(const MeshLib::Mesh &subsfc_mesh, Eigen::Vector3d const &dir, double angle, std::string_view subsfc_node_id_prop_name="", std::string_view subsfc_element_id_prop_name="", std::string_view face_id_prop_name="")
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:191
TemplateElement< MeshLib::QuadRule4 > Quad
Definition Quad.h:28
TemplateElement< MeshLib::PrismRule6 > Prism
Definition Prism.h:25
std::vector< Element * > copyElementVector(std::vector< Element * > const &elements, std::vector< Node * > const &new_nodes, std::vector< std::size_t > const *const node_id_map)
TemplateElement< MeshLib::HexRule8 > Hex
Definition Hex.h:25
std::unique_ptr< MeshLib::Mesh > createFlippedMesh(MeshLib::Mesh const &mesh)
MeshLib::Mesh * addLayerToMesh(MeshLib::Mesh const &mesh, double const thickness, std::string const &name, bool const on_top, bool const copy_material_ids, std::optional< int > const layer_id)
MeshLib::Element * extrudeElement(std::vector< MeshLib::Node * > const &subsfc_nodes, MeshLib::Element const &sfc_elem, MeshLib::PropertyVector< std::size_t > const &sfc_to_subsfc_id_map, std::map< std::size_t, std::size_t > const &subsfc_sfc_id_map)