https://mooseframework.inl.gov
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"
12 #include "SBMSurfaceMeshBuilder.h"
13 
14 registerMooseObject("ShiftedBoundaryMethodApp", UnsignedDistanceToSurfaceMesh);
15 
18 {
20  params.addRequiredParam<UserObjectName>(
21  "builder", "SBMSurfaceMeshBuilder that provides KDTree, elem_id_map, and boundary elements.");
22 
23  params.addClassDescription(
24  "Returns unsigned distance to a surface mesh using KDTree nearest neighbor search. "
25  "The gradient returns the unit vector pointing from boundary to query point.");
26  return params;
27 }
28 
30  : Function(parameters)
31 {
32 }
33 
34 void
36 {
37  const auto builder = &getUserObject<SBMSurfaceMeshBuilder>("builder");
38 
39  if (!builder->hasKDTree())
40  mooseError("UnsignedDistanceToSurfaceMesh '",
41  name(),
42  "' requires SBMSurfaceMeshBuilder '",
43  builder->name(),
44  "' to be configured with 'build_kd_tree = true'.");
45 
46  _kd_tree = &builder->getKDTree();
47  _boundary_elements = &builder->getBoundaryElements();
48 }
49 
50 const SBMBndElementBase &
52 {
53  // KDTree nearest neighbor search
54  std::vector<std::size_t> ret_index(1);
55  _kd_tree->neighborSearch(p, 1, ret_index);
56 
57  return *_boundary_elements->at(ret_index.front()).get();
58 }
59 
62 {
63  const SBMBndElementBase & elem = closestBoundaryElem(p);
64  return elem.distanceFrom(p);
65 }
66 
67 Real
68 UnsignedDistanceToSurfaceMesh::value(Real /*t*/, const Point & p) const
69 {
70  return distanceVectorToSurface(p).norm();
71 }
72 
74 UnsignedDistanceToSurfaceMesh::gradient(Real /*t*/, const Point & p) const
75 {
77  const Real dist = dv.norm();
78 
79  if (dist <= libMesh::TOLERANCE)
80  return RealGradient(0, 0, 0);
81 
82  // dv points from the query point toward the nearest surface, so -dv points away from it.
83  // The gradient of a distance field points toward increasing distance (away from the surface),
84  // matching the signed-distance gradient convention used by SBMUtils::distanceVectorFromFunction.
85  return -dv / dist;
86 }
87 
90 {
91  const SBMBndElementBase & elem = closestBoundaryElem(p);
92 
93  RealVectorValue n = elem.normal();
94  const Real n_norm = n.norm();
95 
96  return n / n_norm;
97 }
auto norm() const
Computes the unsigned distance to a surface mesh using KDTree nearest neighbor lookup from SBMSurface...
static constexpr Real TOLERANCE
RealGradient gradient(Real t, const Point &p) const override
Return the spatial gradient of the unsigned distance function.
const std::vector< std::unique_ptr< SBMBndElementBase > > * _boundary_elements
void addRequiredParam(const std::string &name, const std::string &doc_string)
Real value(Real t, const Point &p) const override
Return unsigned distance value.
const std::string & name() const
Base class for SBM boundary elements.
registerMooseObject("ShiftedBoundaryMethodApp", UnsignedDistanceToSurfaceMesh)
void neighborSearch(const Point &query_point, unsigned int patch_size, std::vector< std::size_t > &return_index)
KDTree * _kd_tree
Cached pointers for speed.
const Point & normal() const
Getter for the normal vector.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Point distanceFrom(const Point &pt) const
Compute the distance vector from an arbitrary point to this boundary element.
const SBMBndElementBase & closestBoundaryElem(const Point &p) const
find closest boundary element using KDTree
const Real p
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
UnsignedDistanceToSurfaceMesh(const InputParameters &parameters)
RealVectorValue distanceVectorToSurface(const Point &p) const
find closest boundary element and return its distance vector
static InputParameters validParams()
RealVectorValue surfaceNormal(const Point &p) const
Return the true boundary surface normal evaluated at the closest point obtained by projecting the que...