OGS
BoundaryElementsAtPoint.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/MPI.h"
7#include "GeoLib/Point.h"
8#include "MathLib/Point3d.h"
11#include "MeshLib/Mesh.h"
13#include "MeshLib/Node.h"
14
15namespace MeshGeoToolsLib
16{
18 MeshLib::Mesh const& mesh, MeshNodeSearcher const& mshNodeSearcher,
19 GeoLib::Point const& point, const bool multiple_nodes_allowed)
20 : _point(point)
21{
22 auto const node_ids = mshNodeSearcher.getMeshNodeIDs(_point);
23
24#ifdef USE_PETSC
25 std::size_t const number_of_found_nodes_at_rank = node_ids.size();
26 std::size_t const number_of_total_found_nodes = BaseLib::MPI::allreduce(
27 number_of_found_nodes_at_rank, MPI_SUM, BaseLib::MPI::Mpi{});
28
29 if (number_of_total_found_nodes == 0)
30 {
32 "BoundaryElementsAtPoint: the mesh node searcher was unable to "
33 "locate the point ({:f}, {:f}, {:f}) in the mesh.",
34 _point[0], _point[1], _point[2]);
35 }
36
37 if (number_of_found_nodes_at_rank == 0)
38 {
39 return;
40 }
41#else
42 if (node_ids.empty())
43 {
45 "BoundaryElementsAtPoint: the mesh node searcher was unable to "
46 "locate the point ({:f}, {:f}, {:f}) in the mesh.",
47 _point[0], _point[1], _point[2]);
48 }
49#endif
50
51 if (node_ids.size() == 1)
52 {
53 std::array<MeshLib::Node*, 1> const nodes = {
54 {const_cast<MeshLib::Node*>(mesh.getNode(node_ids[0]))}};
55
56 _boundary_elements.push_back(new MeshLib::Point{nodes, node_ids[0]});
57 return;
58 }
59
60 auto& mesh_nodes =
61 const_cast<std::vector<MeshLib::Node*>&>(mesh.getNodes());
62 std::size_t const nearest_node_id =
63 *std::min_element(node_ids.begin(), node_ids.end(),
64 [&mesh_nodes, &point](auto id0, auto id1)
65 {
66 return MathLib::sqrDist(*mesh_nodes[id0], point) <
67 MathLib::sqrDist(*mesh_nodes[id1], point);
68 });
69
70 if (!multiple_nodes_allowed)
71 {
73 "BoundaryElementsAtPoint: the mesh node searcher found {:d} points "
74 "near the requested point ({:f}, {:f}, {:f}) in the mesh, while "
75 "exactly one is expected. Node (id={:d}) ({:f}, {:f}, {:f}) has "
76 "distance {:f}.",
77 node_ids.size(), _point[0], _point[1], _point[2],
78 mesh_nodes[nearest_node_id]->getID(),
79 (*mesh_nodes[nearest_node_id])[0],
80 (*mesh_nodes[nearest_node_id])[1],
81 (*mesh_nodes[nearest_node_id])[2],
82 MathLib::sqrDist(*mesh_nodes[nearest_node_id], point));
83 }
84 WARN(
85 "BoundaryElementsAtPoint: the mesh node searcher found {:d} points "
86 "near the requested point ({:f}, {:f}, {:f}) in the mesh, while "
87 "exactly one is expected. Node (id={:d}) ({:f}, {:f}, {:f}) has "
88 "distance {:f}.",
89 node_ids.size(), _point[0], _point[1], _point[2],
90 mesh_nodes[nearest_node_id]->getID(), (*mesh_nodes[nearest_node_id])[0],
91 (*mesh_nodes[nearest_node_id])[1], (*mesh_nodes[nearest_node_id])[2],
92 MathLib::sqrDist(*mesh_nodes[nearest_node_id], point));
93
94 std::array<MeshLib::Node*, 1> const nodes = {
95 {const_cast<MeshLib::Node*>(mesh.getNode(nearest_node_id))}};
96
97 _boundary_elements.push_back(new MeshLib::Point{nodes, nearest_node_id});
98}
99
101{
102 for (auto p : _boundary_elements)
103 {
104 delete p;
105 }
106}
107} // namespace MeshGeoToolsLib
#define OGS_FATAL(...)
Definition Error.h:19
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
BoundaryElementsAtPoint(MeshLib::Mesh const &mesh, MeshNodeSearcher const &mshNodeSearcher, GeoLib::Point const &point, const bool multiple_nodes_allowed)
std::vector< MeshLib::Element * > _boundary_elements
std::vector< std::size_t > getMeshNodeIDs(GeoLib::GeoObject const &geoObj) const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition Mesh.h:82
static T allreduce(T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
Definition MPI.h:122
double sqrDist(MathLib::Point3d const &p0, MathLib::Point3d const &p1)
Definition Point3d.cpp:19
TemplateElement< PointRule1 > Point