LCOV - code coverage report
Current view: top level - src/functions - UnsignedDistanceToSurfaceMesh.C (source / functions) Hit Total Coverage
Test: idaholab/moose shifted_boundary_method: #33416 (b10b36) with base 9fbd27 Lines: 33 34 97.1 %
Date: 2026-07-23 16:20:15 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      10             : #include "UnsignedDistanceToSurfaceMesh.h"
      11             : #include "MooseError.h"
      12             : #include "SBMSurfaceMeshBuilder.h"
      13             : 
      14             : registerMooseObject("ShiftedBoundaryMethodApp", UnsignedDistanceToSurfaceMesh);
      15             : 
      16             : InputParameters
      17          96 : UnsignedDistanceToSurfaceMesh::validParams()
      18             : {
      19          96 :   InputParameters params = Function::validParams();
      20         192 :   params.addRequiredParam<UserObjectName>(
      21             :       "builder", "SBMSurfaceMeshBuilder that provides KDTree, elem_id_map, and boundary elements.");
      22             : 
      23          96 :   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          96 :   return params;
      27           0 : }
      28             : 
      29          54 : UnsignedDistanceToSurfaceMesh::UnsignedDistanceToSurfaceMesh(const InputParameters & parameters)
      30          54 :   : Function(parameters)
      31             : {
      32          54 : }
      33             : 
      34             : void
      35          38 : UnsignedDistanceToSurfaceMesh::initialSetup()
      36             : {
      37          76 :   const auto builder = &getUserObject<SBMSurfaceMeshBuilder>("builder");
      38             : 
      39          38 :   if (!builder->hasKDTree())
      40           2 :     mooseError("UnsignedDistanceToSurfaceMesh '",
      41             :                name(),
      42             :                "' requires SBMSurfaceMeshBuilder '",
      43             :                builder->name(),
      44             :                "' to be configured with 'build_kd_tree = true'.");
      45             : 
      46          36 :   _kd_tree = &builder->getKDTree();
      47          36 :   _boundary_elements = &builder->getBoundaryElements();
      48          36 : }
      49             : 
      50             : const SBMBndElementBase &
      51     3687232 : UnsignedDistanceToSurfaceMesh::closestBoundaryElem(const Point & p) const
      52             : {
      53             :   // KDTree nearest neighbor search
      54     3687232 :   std::vector<std::size_t> ret_index(1);
      55     3687232 :   _kd_tree->neighborSearch(p, 1, ret_index);
      56             : 
      57     7374464 :   return *_boundary_elements->at(ret_index.front()).get();
      58     3687232 : }
      59             : 
      60             : RealVectorValue
      61     3685184 : UnsignedDistanceToSurfaceMesh::distanceVectorToSurface(const Point & p) const
      62             : {
      63     3685184 :   const SBMBndElementBase & elem = closestBoundaryElem(p);
      64     3685184 :   return elem.distanceFrom(p);
      65             : }
      66             : 
      67             : Real
      68     1842592 : UnsignedDistanceToSurfaceMesh::value(Real /*t*/, const Point & p) const
      69             : {
      70     1842592 :   return distanceVectorToSurface(p).norm();
      71             : }
      72             : 
      73             : RealGradient
      74     1842592 : UnsignedDistanceToSurfaceMesh::gradient(Real /*t*/, const Point & p) const
      75             : {
      76     1842592 :   const RealVectorValue dv = distanceVectorToSurface(p);
      77     1842592 :   const Real dist = dv.norm();
      78             : 
      79     1842592 :   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             : 
      88             : RealVectorValue
      89        2048 : UnsignedDistanceToSurfaceMesh::surfaceNormal(const Point & p) const
      90             : {
      91        2048 :   const SBMBndElementBase & elem = closestBoundaryElem(p);
      92             : 
      93             :   RealVectorValue n = elem.normal();
      94        2048 :   const Real n_norm = n.norm();
      95             : 
      96        2048 :   return n / n_norm;
      97             : }

Generated by: LCOV version 1.14