https://mooseframework.inl.gov
NearestNodeNumberUO.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 
10 #include "NearestNodeNumberUO.h"
11 #include <limits>
12 
14 
17 {
19  params.addRequiredParam<Point>("point", "The point");
20  params.addClassDescription("Finds and outputs the nearest node number to a point");
21  return params;
22 }
23 
25  : NodalUserObject(parameters),
26  _my_pid(processor_id()),
27  _point(getParam<Point>("point")),
28  _node_found(false),
29  _min_distance(std::numeric_limits<Real>::max()),
30  _closest_node(nullptr),
31  _overall_best_id(0)
32 {
33 }
34 
35 void
37 {
38  _node_found = false;
41  _closest_node = nullptr;
42 }
43 
44 void
46 {
47  if (!_node_found)
48  {
50  _closest_node = nullptr;
51  }
52 }
53 
54 void
56 {
57  if (_node_found)
58  return;
59  const Real dist = ((*_current_node) - _point).norm();
60  if (dist < _min_distance || (dist == _min_distance && _current_node->id() < _closest_node->id()))
61  {
62  _min_distance = dist;
64  }
65 }
66 
67 void
69 {
70  Real overall_min_distance = _min_distance;
71  gatherMin(overall_min_distance);
72  _overall_best_id = (overall_min_distance == _min_distance)
73  ? _closest_node->id()
76  _node_found = true;
77 }
78 
81 {
82  return _overall_best_id;
83 }
84 
85 const Node *
87 {
88  if (!_closest_node) // probably no evaluation has occurred
89  return nullptr;
90  if (_closest_node->id() == _overall_best_id && _closest_node->processor_id() == _my_pid)
91  return _closest_node;
92  return nullptr;
93 }
94 
95 void
97 {
98  const auto & nnn = static_cast<const NearestNodeNumberUO &>(y);
99  if (!nnn._closest_node)
100  return;
101  if (nnn._min_distance < _min_distance ||
102  (nnn._min_distance == _min_distance && nnn._closest_node->id() < _closest_node->id()))
103  {
104  _min_distance = nnn._min_distance;
105  _closest_node = nnn._closest_node;
106  }
107 }
virtual void execute() override
Execute method.
bool _node_found
Whether the nearest node on this processor has been found (this is used to reduce unnecessary re-comp...
const Node *const & _current_node
Reference to current node pointer.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
virtual void meshChanged() override
Called on this object when the mesh changes.
void gatherMin(T &value)
Gather the parallel min of the variable passed in.
Definition: UserObject.h:150
dof_id_type _overall_best_id
Node number of closest node, over the whole mesh.
const Node * getClosestNode() const
Returns a const pointer to the closest node over the entire mesh, if that node is owned by this proce...
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
auto max(const L &left, const R &right)
A user object that runs over all the nodes and does an aggregation step to compute a single value...
NearestNodeNumberUO(const InputParameters &parameters)
registerMooseObject("MooseApp", NearestNodeNumberUO)
Finds the nearest node to a given point.
virtual void threadJoin(const UserObject &y) override
Must override.
auto norm(const T &a) -> decltype(std::abs(a))
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void finalize() override
Finalize.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
Real _min_distance
Minimum distance for nodes on this processor.
dof_id_type getClosestNodeId() const
Returns the ID of the nearest node.
const Node * _closest_node
Nearest node on this processor.
const Point & _point
The point.
Base class for user-specific data.
Definition: UserObject.h:40
uint8_t dof_id_type
static InputParameters validParams()
const processor_id_type _my_pid
processor ID of this object