https://mooseframework.inl.gov
NearestNodeValueAux.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 "NearestNodeValueAux.h"
11 
12 #include "SystemBase.h"
13 #include "NearestNodeLocator.h"
14 
16 
19 {
21  params.addClassDescription("Retrieves a field value from the closest node on the paired boundary "
22  "and stores it on this boundary or block.");
23  params.set<bool>("_dual_restrictable") = true;
24  params.addRequiredParam<BoundaryName>("paired_boundary", "The boundary to get the value from.");
25  params.addRequiredCoupledVar("paired_variable", "The variable to get the value of.");
26  params.set<bool>("use_displaced_mesh") = true;
27  return params;
28 }
29 
31  : AuxKernel(parameters),
32  _nearest_node(
33  getNearestNodeLocator(parameters.get<BoundaryName>("paired_boundary"), boundaryNames()[0])),
34  _serialized_solution(_nl_sys.currentSolution()),
35  _paired_variable(coupled("paired_variable"))
36 {
37  if (boundaryNames().size() > 1)
38  mooseError("NearestNodeValueAux can only be used with one boundary at a time!");
39 
40  // Check that the paired variable is from the solution system
41  if (_subproblem.hasAuxiliaryVariable("paired_variable"))
42  paramError("paired_variable", "Paired variable should not be auxiliary");
43  // Check that the paired variable is nodal
44  if (!getVar("paired_variable", 0)->hasDoFsOnNodes())
45  paramError("paired_variable", "Paired variable does not have degrees of freedom on nodes");
46 
47  // TODO: avoid the runtime checks by making sure the paired variable is defined on the boundaries
48 }
49 
50 Real
52 {
53  if (isNodal())
54  {
55  // Assumes the variable you are coupling to is from the nonlinear system for now.
56  const Node * nearest = _nearest_node.nearestNode(_current_node->id());
57  if (nearest == nullptr)
58  mooseError("Could not locate the nearest node from node: ", _current_node->get_info());
59  dof_id_type dof_number = nearest->dof_number(_nl_sys.number(), _paired_variable, 0);
60  if (dof_number == libMesh::DofObject::invalid_id)
61  mooseError("Paired variable does not seem to be defined on nearest node: ",
62  nearest->get_info());
63  return (*_serialized_solution)(dof_number);
64  }
65  else
66  {
67  // Get a value for all the nodes on the boundary, then average them for the centroid value
68  Real average = 0;
69  Real sum_inv_dist = 0;
70  for (const auto & node : _current_elem->node_ref_range())
71  {
72  const Node * nearest = _nearest_node.nearestNode(node.id());
73  // Some of the element's nodes wont be on the boundary
74  if (!nearest)
75  continue;
76  const auto dof_number = nearest->dof_number(_nl_sys.number(), _paired_variable, 0);
77  const auto distance = _nearest_node.distance(node.id());
78 
79  if (dof_number == libMesh::DofObject::invalid_id)
80  mooseError("Paired variable does not seem to be defined on nearest node: ",
81  nearest->get_info());
82 
83  // inverse distance weighting should be a fine default
84  if (distance > 0)
85  {
86  average += (*_serialized_solution)(dof_number) / distance;
87  sum_inv_dist += 1. / distance;
88  }
89  // if node and nearest nodes coincide, weight with 1
90  else
91  {
92  average += (*_serialized_solution)(dof_number);
93  sum_inv_dist += 1.;
94  }
95  }
96 
97  if (sum_inv_dist > 0)
98  return average / sum_inv_dist;
99  else
100  return 0.;
101  }
102 }
static InputParameters validParams()
const NumericVector< Number > *const & _serialized_solution
registerMooseObject("MooseApp", NearestNodeValueAux)
const Node *const & _current_node
Current node (valid only for nodal kernels)
Definition: AuxKernel.h:214
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Real distance(dof_id_type node_id)
Valid to call this after findNodes() has been called to get the distance to the nearest node...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
MooseVariable * getVar(const std::string &var_name, unsigned int comp)
Extract pointer to a coupled variable.
Definition: Coupleable.C:288
bool hasDoFsOnNodes() const override
Does this variable have DoFs on nodes.
Real distance(const Point &p)
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...
SystemBase & _nl_sys
Definition: AuxKernel.h:167
static const dof_id_type invalid_id
NearestNodeLocator & _nearest_node
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1159
virtual Real computeValue() override
Compute and return the value of the aux variable.
Finds the closest node on a paired boundary to the current node or element and stores a corresponding...
NearestNodeValueAux(const InputParameters &parameters)
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Node * nearestNode(dof_id_type node_id)
Valid to call this after findNodes() has been called to get a pointer to the nearest node...
SubProblem & _subproblem
Subproblem this kernel is part of.
Definition: AuxKernel.h:164
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const Elem *const & _current_elem
Current element (valid only for elemental kernels)
Definition: AuxKernel.h:204
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...
static InputParameters validParams()
Definition: AuxKernel.C:27
unsigned int _paired_variable
const std::vector< BoundaryName > & boundaryNames() const
Return the boundary names for this object.
bool isNodal() const
Nodal or elemental kernel?
Definition: AuxKernel.h:86
virtual bool hasAuxiliaryVariable(const std::string &var_name) const
Whether or not this problem has this auxiliary variable.
Definition: SubProblem.C:811
uint8_t dof_id_type