https://mooseframework.inl.gov
Loading...
Searching...
No Matches
UnsignedDistanceToSurfaceMesh.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11#include "MooseError.h"
13#include "SBMSurfaceDistance.h"
14#include "SurfaceElement.h"
15
17
20{
22 params.addRequiredParam<UserObjectName>(
23 "builder", "SBMSurfaceMeshBuilder that provides KDTree, elem_id_map, and boundary elements.");
24
26 "Returns unsigned distance to a surface mesh using KDTree nearest neighbor search. "
27 "The gradient returns the unit vector pointing from boundary to query point.");
28 return params;
29}
30
35
36void
38{
39 const auto builder = &getUserObject<SBMSurfaceMeshBuilder>("builder");
40
41 if (!builder->hasKDTree())
42 mooseError("UnsignedDistanceToSurfaceMesh '",
43 name(),
44 "' requires SBMSurfaceMeshBuilder '",
45 builder->name(),
46 "' to be configured with 'build_kd_tree = true'.");
47
48 _kd_tree = &builder->getKDTree();
49 _boundary_elements = &builder->surfaceElementSet().elements();
50}
51
52const SurfaceElement &
54{
55 // KDTree nearest neighbor search
56 std::vector<std::size_t> ret_index(1);
57 _kd_tree->neighborSearch(p, 1, ret_index);
58
59 return *_boundary_elements->at(ret_index.front()).get();
60}
61
62RealVectorValue
68
69Real
70UnsignedDistanceToSurfaceMesh::value(Real /*t*/, const Point & p) const
71{
72 return distanceVectorToSurface(p).norm();
73}
74
75RealGradient
76UnsignedDistanceToSurfaceMesh::gradient(Real /*t*/, const Point & p) const
77{
78 const RealVectorValue dv = distanceVectorToSurface(p);
79 const Real dist = dv.norm();
80
81 if (dist <= libMesh::TOLERANCE)
82 return RealGradient(0, 0, 0);
83
84 // dv points from the query point toward the nearest surface, so -dv points away from it.
85 // The gradient of a distance field points toward increasing distance (away from the surface),
86 // matching the signed-distance gradient convention used by SBMUtils::distanceVectorFromFunction.
87 return -dv / dist;
88}
89
90RealVectorValue
92{
93 const SurfaceElement & elem = closestBoundaryElem(p);
94
95 RealVectorValue n = elem.normal();
96 const Real n_norm = n.norm();
97
98 return n / n_norm;
99}
const Real p
registerMooseObject("ShiftedBoundaryMethodApp", UnsignedDistanceToSurfaceMesh)
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void neighborSearch(const Point &query_point, unsigned int patch_size, std::vector< std::size_t > &return_index)
const std::string & name() const
void mooseError(Args &&... args) const
const Point & normal() const
Computes the unsigned distance to a surface mesh using KDTree nearest neighbor lookup from SBMSurface...
KDTree * _kd_tree
Cached pointers for speed.
const std::vector< std::unique_ptr< SurfaceElement > > * _boundary_elements
RealVectorValue surfaceNormal(const Point &p) const
Return the true boundary surface normal evaluated at the closest point obtained by projecting the que...
const SurfaceElement & closestBoundaryElem(const Point &p) const
find closest boundary element using KDTree
Real value(Real t, const Point &p) const override
Return unsigned distance value.
RealGradient gradient(Real t, const Point &p) const override
Return the spatial gradient of the unsigned distance function.
RealVectorValue distanceVectorToSurface(const Point &p) const
find closest boundary element and return its distance vector
UnsignedDistanceToSurfaceMesh(const InputParameters &parameters)
libMesh::Point distanceFrom(const SurfaceElement &surface_elem, const libMesh::Point &pt)
Returns the vector from pt to the nearest point on the surface element: the normal projection if it f...
static constexpr Real TOLERANCE