OGS
MeshValidation.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "MeshValidation.h"
5
6#include <algorithm>
7#include <numeric>
8#include <stack>
9
10#include "BaseLib/Logging.h"
12#include "MeshLib/Mesh.h"
14#include "MeshLib/Node.h"
17
18namespace MeshToolsLib
19{
29static void trackSurface(MeshLib::Element const* const element,
30 std::vector<unsigned>& sfc_idx,
31 unsigned const current_index)
32{
33 std::stack<MeshLib::Element const*> elem_stack;
34 elem_stack.push(element);
35 while (!elem_stack.empty())
36 {
37 MeshLib::Element const* const elem = elem_stack.top();
38 elem_stack.pop();
39 sfc_idx[elem->getID()] = current_index;
40 std::size_t const n_neighbors(elem->getNumberOfNeighbors());
41 for (std::size_t i = 0; i < n_neighbors; ++i)
42 {
43 MeshLib::Element const* neighbor(elem->getNeighbor(i));
44 if (neighbor != nullptr && sfc_idx[neighbor->getID()] ==
45 std::numeric_limits<unsigned>::max())
46 {
47 elem_stack.push(neighbor);
48 }
49 }
50 }
51}
52
54{
55 INFO("Looking for unused nodes...");
56 MeshLib::NodeSearch ns(mesh);
57 ns.searchUnused();
58 if (!ns.getSearchedNodeIDs().empty())
59 {
60 INFO("{:d} unused mesh nodes found.", ns.getSearchedNodeIDs().size());
61 return false;
62 }
63 return true;
64}
65
67 double const eps)
68{
69 MeshRevision const rev(mesh);
70 INFO("Found {:d} potentially collapsible nodes.",
72 return (rev.getNumberOfCollapsibleNodes() > 0);
73}
74
76{
77 const std::vector<ElementErrorCode> codes(
79 std::array<std::string,
80 static_cast<std::size_t>(ElementErrorFlag::MaxValue)>
82 for (auto& i : output_str)
83 {
84 INFO("{:s}", i);
85 }
86}
87
88std::vector<ElementErrorCode> MeshValidation::testElementGeometry(
89 const MeshLib::Mesh& mesh, double min_volume)
90{
91 INFO("Testing mesh element geometry:");
92 const auto nErrorCodes(
93 static_cast<std::size_t>(ElementErrorFlag::MaxValue));
94 unsigned error_count[nErrorCodes];
95 std::fill_n(error_count, 4, 0);
96 const std::size_t nElements(mesh.getNumberOfElements());
97 const std::vector<MeshLib::Element*>& elements(mesh.getElements());
98 std::vector<ElementErrorCode> error_code_vector;
99 error_code_vector.reserve(nElements);
100
101 for (std::size_t i = 0; i < nElements; ++i)
102 {
103 const ElementErrorCode e = elements[i]->validate();
104 error_code_vector.push_back(e);
105 if (e.none())
106 {
107 continue;
108 }
109
110 // increment error statistics
111 const std::bitset<static_cast<std::size_t>(ElementErrorFlag::MaxValue)>
112 flags(static_cast<std::bitset<static_cast<std::size_t>(
114 for (unsigned j = 0; j < nErrorCodes; ++j)
115 {
116 error_count[j] += flags[j];
117 }
118 }
119
120 // if a larger volume threshold is given, evaluate elements again to add
121 // them even if they are formally okay
122 if (min_volume > std::numeric_limits<double>::epsilon())
123 {
124 for (std::size_t i = 0; i < nElements; ++i)
125 {
126 if (elements[i]->getContent() < min_volume)
127 {
128 error_code_vector[i].set(ElementErrorFlag::ZeroVolume);
129 }
130 }
131 }
132
133 // output
134 const auto error_sum(static_cast<unsigned>(
135 std::accumulate(error_count, error_count + nErrorCodes, 0.0)));
136 if (error_sum != 0)
137 {
138 ElementErrorFlag const flags[nErrorCodes] = {
141 for (std::size_t i = 0; i < nErrorCodes; ++i)
142 {
143 if (error_count[i])
144 {
145 INFO("{:d} elements found with {:s}.",
146 error_count[i],
148 }
149 }
150 }
151 else
152 {
153 INFO("No errors found.");
154 }
155 return error_code_vector;
156}
157
158std::array<std::string, static_cast<std::size_t>(ElementErrorFlag::MaxValue)>
160 const std::vector<ElementErrorCode>& error_codes)
161{
162 const auto nErrorFlags(
163 static_cast<std::size_t>(ElementErrorFlag::MaxValue));
164 const ElementErrorFlag flags[nErrorFlags] = {
167 const std::size_t nElements(error_codes.size());
168 std::array<std::string,
169 static_cast<std::size_t>(ElementErrorFlag::MaxValue)>
170 output;
171
172 for (std::size_t i = 0; i < nErrorFlags; ++i)
173 {
174 unsigned count(0);
175 std::string elementIdStr;
176
177 for (std::size_t j = 0; j < nElements; ++j)
178 {
179 if (error_codes[j][flags[i]])
180 {
181 elementIdStr += (std::to_string(j) + ", ");
182 count++;
183 }
184 }
185 const std::string nErrorsStr = (count) ? std::to_string(count) : "No";
186 output[i] = (nErrorsStr + " elements found with " +
187 ElementErrorCode::toString(flags[i]) + ".\n");
188
189 if (count)
190 {
191 output[i] += ("ElementIDs: " + elementIdStr + "\n");
192 }
193 }
194 return output;
195}
196
198{
199 if (mesh.getDimension() == 1)
200 {
201 return 0;
202 }
203
204 auto boundary_mesh =
206 mesh,
210 std::vector<MeshLib::Element*> const& elements(
211 boundary_mesh->getElements());
212
213 std::vector<unsigned> sfc_idx(elements.size(),
214 std::numeric_limits<unsigned>::max());
215 unsigned current_surface_id(0);
216 auto it = sfc_idx.cbegin();
217
218 while (it != sfc_idx.cend())
219 {
220 std::size_t const idx =
221 static_cast<std::size_t>(std::distance(sfc_idx.cbegin(), it));
222 trackSurface(elements[idx], sfc_idx, current_surface_id++);
223 it = std::find(sfc_idx.cbegin(),
224 sfc_idx.cend(),
225 std::numeric_limits<unsigned>::max());
226 }
227
228 // Subtract "1" from the number of surfaces found to get the number of
229 // holes.
230 return (--current_surface_id);
231}
232} // namespace MeshToolsLib
ElementErrorFlag
Possible error flags for mesh elements.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
Collects error flags for mesh elements.
static std::string toString(const ElementErrorFlag e)
Returns a string output for a specific error flag.
virtual unsigned getNumberOfNeighbors() const =0
Get the number of neighbors for this element.
std::size_t getID() const
Returns the ID of the element.
Definition Element.h:80
virtual const Element * getNeighbor(unsigned i) const =0
Get the specified neighbor.
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:101
unsigned getDimension() const
Definition Mesh.h:80
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:89
Node search class.
Definition NodeSearch.h:18
const std::vector< std::size_t > & getSearchedNodeIDs() const
return marked node IDs
Definition NodeSearch.h:23
std::size_t searchUnused()
Marks all unused nodes.
unsigned getNumberOfCollapsibleNodes(double eps=std::numeric_limits< double >::epsilon()) const
Returns the number of potentially collapsible nodes.
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
std::unique_ptr< MeshLib::Mesh > getBoundaryElementsAsMesh(MeshLib::Mesh const &bulk_mesh, std::string_view subsfc_node_id_prop_name, std::string_view subsfc_element_id_prop_name, std::string_view face_id_prop_name)
static void trackSurface(MeshLib::Element const *const element, std::vector< unsigned > &sfc_idx, unsigned const current_index)
static unsigned detectHoles(MeshLib::Mesh const &mesh)
static std::array< std::string, static_cast< std::size_t >(ElementErrorFlag::MaxValue)> ElementErrorCodeOutput(const std::vector< ElementErrorCode > &error_codes)
static bool existCollapsibleNodes(MeshLib::Mesh &mesh, double const eps)
static bool allNodesUsed(MeshLib::Mesh const &mesh)
static std::vector< ElementErrorCode > testElementGeometry(const MeshLib::Mesh &mesh, double min_volume=std::numeric_limits< double >::epsilon())
static void evaluateElementGeometry(MeshLib::Mesh const &mesh)