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#include "SBMInterfaceManager.h"
16
18
21{
23 params.addParam<UserObjectName>(
24 "builder", "SBMSurfaceMeshBuilder that provides the interface surface geometry.");
25 params.addParam<UserObjectName>(
26 "manager", "SBMInterfaceManager that provides geometry for multiple interfaces.");
27 params.addParam<SubdomainID>("subdomain_id_1", "First subdomain defining the interface.");
28 params.addParam<SubdomainID>("subdomain_id_2", "Second subdomain defining the interface.");
29
31 "Returns unsigned distance to a surface mesh using k-d tree nearest neighbor search. "
32 "The gradient returns the unit vector pointing from boundary to query point.");
33 return params;
34}
35
37 : Function(parameters)
38{
39 const bool has_builder = isParamValid("builder");
40 const bool has_manager = isParamValid("manager");
41 const bool has_first = isParamValid("subdomain_id_1");
42 const bool has_second = isParamValid("subdomain_id_2");
43 if (has_builder == has_manager)
44 paramError("builder", "Specify exactly one of 'builder' and 'manager'.");
45 if (has_manager && (!has_first || !has_second))
46 paramError("manager", "Manager mode requires subdomain_id_1 and subdomain_id_2.");
47 if (!has_manager && (has_first || has_second))
48 paramError("subdomain_id_1", "Subdomain IDs are only valid in manager mode.");
49 if (has_manager)
50 {
51 _subdomain_pair = {getParam<SubdomainID>("subdomain_id_1"),
52 getParam<SubdomainID>("subdomain_id_2")};
53 if (_subdomain_pair.first == _subdomain_pair.second)
54 paramError("subdomain_id_2", "The interface subdomain IDs must be distinct.");
55 }
56}
57
58void
60{
61 if (isParamValid("manager"))
62 {
63 _manager = &getUserObject<SBMInterfaceManager>("manager");
65 paramError("manager", "The requested interface was not detected by the manager.");
66 return;
67 }
68
69 const auto builder = &getUserObject<SBMSurfaceMeshBuilder>("builder");
70 if (!builder->hasKDTree())
71 mooseError("UnsignedDistanceToSurfaceMesh '",
72 name(),
73 "' requires SBMSurfaceMeshBuilder '",
74 builder->name(),
75 "' to be configured with 'build_kd_tree = true'.");
76
77 _kd_tree = &builder->getKDTree();
78 _boundary_elements = &builder->surfaceElementSet().elements();
79}
80
81const SurfaceElement &
83{
84 // k-d tree nearest neighbor search
85 std::vector<std::size_t> ret_index(1);
86 _kd_tree->neighborSearch(p, 1, ret_index);
87
88 return *_boundary_elements->at(ret_index.front()).get();
89}
90
91RealVectorValue
100
101Real
102UnsignedDistanceToSurfaceMesh::value(Real /*t*/, const Point & p) const
103{
104 return distanceVectorToSurface(p).norm();
105}
106
107RealGradient
108UnsignedDistanceToSurfaceMesh::gradient(Real /*t*/, const Point & p) const
109{
110 const RealVectorValue dv = distanceVectorToSurface(p);
111 const Real dist = dv.norm();
112
113 if (dist <= libMesh::TOLERANCE)
114 return RealGradient(0, 0, 0);
115
116 // dv points from the query point toward the nearest surface, so -dv points away from it.
117 // The gradient of a distance field points toward increasing distance (away from the surface),
118 // matching the signed-distance gradient convention used by SBMUtils::distanceVectorFromFunction.
119 return -dv / dist;
120}
121
122RealVectorValue
124{
125 if (_manager)
127
128 const SurfaceElement & elem = closestBoundaryElem(p);
129
130 RealVectorValue n = elem.normal();
131 const Real n_norm = n.norm();
132
133 return n / n_norm;
134}
const Real p
registerMooseObject("ShiftedBoundaryMethodApp", UnsignedDistanceToSurfaceMesh)
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, 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 paramError(const std::string &param, Args... args) const
void mooseError(Args &&... args) const
bool isParamValid(const std::string &name) const
InterfaceQueryResult queryInterface(SubdomainID first, SubdomainID second, const Point &point) const
Return distance and normal data for an interface. The normal points from first to second.
bool hasInterface(SubdomainID first, SubdomainID second) const
Whether an interface was detected between two subdomains.
const Point & normal() const
Computes the unsigned distance to a surface mesh using geometry supplied by SBMSurfaceMeshBuilder or ...
KDTree * _kd_tree
Cached pointers for per-interface builder mode.
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...
std::pair< SubdomainID, SubdomainID > _subdomain_pair
const SBMInterfaceManager * _manager
Optional single-mesh manager used instead of a per-interface builder.
const SurfaceElement & closestBoundaryElem(const Point &p) const
find closest boundary element using k-d tree
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
RealVectorValue distance
Shortest distance vector to the interface.
RealVectorValue normal
Unit normal vector of the interface (pointing from first to second subdomain)