OGS
ElementStatus.cpp
Go to the documentation of this file.
1
15#include "ElementStatus.h"
16
17#include "Elements/Element.h"
18#include "Mesh.h"
19#include "MeshLib/Node.h"
20
21namespace MeshLib
22{
23ElementStatus::ElementStatus(Mesh const* const mesh, bool hasAnyInactive)
24 : _mesh(mesh),
25 _element_status(mesh->getNumberOfElements(), true),
26 _hasAnyInactive(hasAnyInactive)
27{
28 const std::vector<MeshLib::Node*>& nodes(_mesh->getNodes());
29 std::transform(begin(nodes), end(nodes), back_inserter(_active_nodes),
30 [this](Node const* const n)
31 { return _mesh->getElementsConnectedToNode(*n).size(); });
32}
33
35 std::vector<int> const& vec_inactive_matIDs)
36 : ElementStatus(mesh, !vec_inactive_matIDs.empty())
37{
38 if (mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
39 {
40 auto* const materialIds =
41 mesh->getProperties().getPropertyVector<int>("MaterialIDs");
42 for (auto material_id : vec_inactive_matIDs)
43 {
44 for (auto e : _mesh->getElements())
45 {
46 if ((*materialIds)[e->getID()] == material_id)
47 {
48 setElementStatus(e->getID(), false);
49 }
50 }
51 }
52 }
53
54 _vec_active_eles.reserve(getNumberOfActiveElements());
55 const std::size_t nElems(_mesh->getNumberOfElements());
56 for (std::size_t i = 0; i < nElems; ++i)
57 {
58 if (_element_status[i])
59 {
60 _vec_active_eles.push_back(
61 const_cast<MeshLib::Element*>(_mesh->getElement(i)));
62 }
63 }
64
65 _vec_active_nodes.reserve(this->getNumberOfActiveNodes());
66 const std::size_t nNodes(_mesh->getNumberOfNodes());
67 for (std::size_t i = 0; i < nNodes; ++i)
68 {
69 if (_active_nodes[i] > 0)
70 {
71 _vec_active_nodes.push_back(
72 const_cast<MeshLib::Node*>(_mesh->getNode(i)));
73 }
74 }
75
76 DBUG(
77 "Deactivated {:d} materials and resulting active {:d} nodes and {:d} "
78 "elements",
79 vec_inactive_matIDs.size(),
80 _vec_active_nodes.size(),
81 _vec_active_eles.size());
82}
83
84std::vector<MeshLib::Element*> const& ElementStatus::getActiveElements() const
85{
86 if (_hasAnyInactive)
87 {
88 return _vec_active_eles;
89 }
90
91 return _mesh->getElements();
92}
93
94std::vector<MeshLib::Node*> const& ElementStatus::getActiveNodes() const
95{
96 if (_hasAnyInactive)
97 {
98 return _vec_active_nodes;
99 }
100
101 return _mesh->getNodes();
102}
103
104std::size_t ElementStatus::getNumberOfActiveNodes() const
105{
106 return _active_nodes.size() -
107 std::count(_active_nodes.cbegin(), _active_nodes.cend(), 0);
108}
109
110std::size_t ElementStatus::getNumberOfActiveElements() const
111{
112 return static_cast<std::size_t>(
113 std::count(_element_status.cbegin(), _element_status.cend(), true));
114}
115
116void ElementStatus::setElementStatus(std::size_t i, bool status)
117{
118 if (_element_status[i] != status)
119 {
120 const int change = (status) ? 1 : -1;
121 _element_status[i] = status;
122 const unsigned nElemNodes(_mesh->getElement(i)->getNumberOfNodes());
123 MeshLib::Node const* const* const nodes =
124 _mesh->getElement(i)->getNodes();
125 for (unsigned j = 0; j < nElemNodes; ++j)
126 {
127 assert(_active_nodes[j] < 255); // if one node has >255 connected
128 // elements the data type is too
129 // small
130 _active_nodes[nodes[j]->getID()] += change;
131 }
132 }
133}
134
135bool ElementStatus::isActiveNode(MeshLib::Node const* node) const
136{
137 return _active_nodes[node->getID()] > 0;
138}
139
140} // namespace MeshLib
Definition of the ElementStatus class.
Definition of the Element class.
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
Definition of the Node class.
std::size_t getID() const
MeshLib::Mesh const *const _mesh
The mesh for which the element status is administrated.
ElementStatus(MeshLib::Mesh const *const mesh, bool hasAnyInactive=false)
Constructor assuming all nodes and elements.
std::vector< unsigned char > _active_nodes
Node status for each mesh node (value = number of active elements connected to node,...
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
Properties & getProperties()
Definition Mesh.h:134
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition Mesh.cpp:256
bool existsPropertyVector(std::string_view name) const
Definition Properties.h:74