https://mooseframework.inl.gov
Loading...
Searching...
No Matches
FunctorNodalCorrector.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 "NonlinearSystemBase.h"
13
14#include "libmesh/numeric_vector.h"
15#include "libmesh/int_range.h"
16
18
21{
25 "Set (all or some) values of (one or more) nonlinear variable(s) using functor evaluations");
26 params.addRequiredCoupledVar("variables_to_correct", "Variables to change the value of.");
27 params.addRequiredParam<std::vector<MooseFunctorName>>(
28 "functors", "Functors to compute the new variable values with");
29
30 // More options for evaluations
31 params.addParam<std::vector<BoundaryName>>(
32 "excluded_nodesets", {}, "Vector of nodesets to skip execution on");
33
34 // Implement others as needed
35 MooseEnum techniques("node_arg", "node_arg");
36 params.addParam<MooseEnum>(
37 "functor_evaluation_technique", techniques, "How to evaluate the functor");
38
39 // No need to evaluate functor multiple times
40 params.set<bool>("unique_node_execute") = true;
41 params.addPrivateParam<bool>("unique_node_execute");
42
43 params.registerBase("Corrector");
44 return params;
45}
46
48 : NodalUserObject(parameters),
50 _mesh(_fe_problem.mesh()),
51 _var_names(getParam<std::vector<VariableName>>("variables_to_correct")),
52 _functor_names(getParam<std::vector<MooseFunctorName>>("functors")),
53 _functor_evaluation_technique(getParam<MooseEnum>("functor_evaluation_technique"))
54{
55 for (const auto & var_name : _var_names)
56 {
57 auto & var = _fe_problem.getVariable(0, var_name);
58
59 if (_sys.number() != var.sys().system().number())
60 paramError("variables_to_correct", "Variables must be all in the non-linear system.");
61
62 _var_numbers.push_back(var.number());
63 }
64
65 if (_functor_names.size() != _var_names.size())
66 paramError("functors",
67 "Functor list size (" + std::to_string(_functor_names.size()) +
68 ") should be the same as variable list size (" +
69 std::to_string(_var_names.size()));
70
71 // Retrieve a pointer to the functors
72 _functors.resize(_functor_names.size());
73 for (const auto i : index_range(_functor_names))
74 _functors[i] = &getFunctor<Real>(_functor_names[i], 0);
75
76 const auto & excluded_nodesets = getParam<std::vector<BoundaryName>>("excluded_nodesets");
78}
79
80void
82{
83 auto & dof_map = _sys.system().get_dof_map();
84 const auto local_dof_begin = dof_map.first_dof();
85 const auto local_dof_end = dof_map.end_dof();
86
87 // Skip on excluded boundaries
88 const auto & binfo = _mesh.getMesh().get_boundary_info();
89 for (const auto exc_bid : _excluded_nodeset_ids)
90 if (binfo.has_boundary_id(_current_node, exc_bid))
91 return;
92
93 std::vector<std::vector<dof_id_type>> dof_indices(_var_numbers.size());
94 std::vector<Real> functor_values(_var_numbers.size());
95
96 // Pre-form the functor arguments
97 const auto state_arg = Moose::currentState();
99
101 paramError("Only nodal evaluation of functors has been implemented at this time");
102
103 // prepare variable dofs
104 for (const auto i : index_range(_var_numbers))
105 {
106 dof_map.dof_indices(_current_node, dof_indices[i], _var_numbers[i]);
107 mooseAssert(dof_indices[i].size() <= 1,
108 "Corrector has not been implemented for this variable type");
109 }
110
111 // only doing current for now
112 auto & solution = _sys.solutionState(0);
113
114 // loop over all DOFs
115 for (const auto j : index_range(dof_indices[0]))
116 {
117 // check if the first variable's DOFs are local (if they are all other variables should
118 // have local DOFS as well)
119 if (dof_indices[0][j] > local_dof_end || dof_indices[0][j] < local_dof_begin)
120 continue;
121
122 // evaluate functors
123
124 for (const auto i : index_range(_var_numbers))
125 functor_values[i] = (*_functors[i])(node_arg, state_arg);
126
127 // replace the variable DOF value
128 for (const auto i : index_range(_var_numbers))
129 solution.set(dof_indices[i][j], functor_values[i]);
130 }
131}
132
133void
registerMooseObject("MooseApp", FunctorNodalCorrector)
virtual const MooseVariableFieldBase & getVariable(const THREAD_ID tid, const std::string &var_name, Moose::VarKindType expected_var_type=Moose::VarKindType::VAR_ANY, Moose::VarFieldType expected_var_field_type=Moose::VarFieldType::VAR_FIELD_ANY) const override
Returns the variable reference for requested variable which must be of the expected_var_type (Nonline...
Set (all or some) values of (one or more) nonlinear variable(s) using functor evaluations.
std::vector< const Moose::Functor< Real > * > _functors
Pointers to the functors.
FunctorNodalCorrector(const InputParameters &parameters)
const MooseEnum _functor_evaluation_technique
Evaluation method (argument)
const std::vector< VariableName > & _var_names
names of the variables to set using functors
MooseMesh & _mesh
reference to the mesh
void finalize() override
Finalize.
std::vector< unsigned int > _var_numbers
internal ID numbers of the variables to smooth
const std::vector< MooseFunctorName > & _functor_names
Name of the functors.
void execute() override
Execute method.
std::vector< BoundaryID > _excluded_nodeset_ids
Vector of boundaries to not execute on.
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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...
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System.
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.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
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 ...
Definition MooseBase.h:457
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition MooseMesh.C:3559
A user object that runs over all the nodes and does an aggregation step to compute a single value.
const Node *const & _current_node
Reference to current node pointer.
static InputParameters validParams()
An interface for accessing Moose::Functors for systems that do not care about automatic differentiati...
static InputParameters validParams()
unsigned int number() const
Gets the number of this system.
virtual NumericVector< Number > & solutionState(const unsigned int state, Moose::SolutionIterationType iteration_type=Moose::SolutionIterationType::Time)
Get a state of the solution (0 = current, 1 = old, 2 = older, etc).
void update()
Update the system (doing libMesh magic)
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
SystemBase & _sys
Reference to the system object for this user object.
dof_id_type first_dof(const processor_id_type proc) const
virtual void set(const numeric_index_type i, const T value)=0
std::unique_ptr< NumericVector< Number > > solution
const DofMap & get_dof_map() const
MeshBase & mesh
std::vector< BoundaryID > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown, const std::set< BoundaryID > &mesh_boundary_ids)
Gets the boundary IDs with their names.
StateArg currentState()
static const std::set< SubdomainID > undefined_subdomain_connection
A static member that can be used when the connection of a node to subdomains is unknown.