OGS
HeuristicSearchLength.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
5
6#include "BaseLib/Logging.h"
8
9namespace MeshGeoToolsLib
10{
12 LengthType length_type)
13 : _mesh(mesh)
14{
15 double sum(0.0);
16 double sum_of_sqr(0.0); // total length of edges
17
18 std::size_t n_sampling(0); // total length of edges squared
19 std::vector<MeshLib::Element*> const& elements(_mesh.getElements());
20
21 if (length_type == LengthType::Edge)
22 {
23 for (auto element : elements)
24 {
25 std::size_t const n_edges(element->getNumberOfEdges());
26 for (std::size_t k(0); k < n_edges; k++)
27 {
28 auto edge =
29 element->getEdge(k); // allocation inside getEdge().
30 double const len = edge->getContent();
31 delete edge;
32 sum += len;
33 sum_of_sqr += len * len;
34 }
35 n_sampling += n_edges;
36 }
37 }
38 else
39 {
40 for (const MeshLib::Element* e : elements)
41 {
42 auto const [min, max] =
44 sum += std::sqrt(min);
45 sum_of_sqr += min;
46 }
47 n_sampling = _mesh.getNumberOfElements();
48 }
49
50 const double mean(sum / n_sampling);
51 const double variance((sum_of_sqr - (sum * sum) / n_sampling) /
52 (n_sampling - 1));
53
54 // Set the search length for the case of non-positive variance (which can
55 // happen due to numerics).
56 _search_length = mean / 2;
57
58 if (variance > 0)
59 {
60 if (variance < mean * mean / 4)
61 {
62 _search_length -= std::sqrt(variance);
63 }
64 else
65 {
66 _search_length = std::numeric_limits<double>::epsilon();
67 }
68 }
69
70 DBUG(
71 "[MeshNodeSearcher::MeshNodeSearcher] Calculated search length for "
72 "mesh '{:s}' is {:f}.",
73 _mesh.getName(), _search_length);
74}
75
76} // end namespace MeshGeoToolsLib
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
HeuristicSearchLength(MeshLib::Mesh const &mesh, LengthType length_type=LengthType::Edge)
std::pair< double, double > computeSqrNodeDistanceRange(MeshLib::Element const &element, bool const check_allnodes)
Compute the minimum and maximum node distances for this element.
Definition Element.cpp:143