https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseVariableBase.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 "MooseVariableBase.h"
11
12#include "AddVariableAction.h"
13#include "SubProblem.h"
14#include "SystemBase.h"
15#include "MooseMesh.h"
16#include "MooseApp.h"
18#include "BlockRestrictable.h"
19#include "MooseUtils.h"
20
21#include "libmesh/variable.h"
22#include "libmesh/dof_map.h"
23#include "libmesh/system.h"
24#include "libmesh/fe_type.h"
25#include "libmesh/string_to_enum.h"
26
27// Users should never actually create this object
29
32{
36
39
40 // ArrayVariable capability
41 params.addRangeCheckedParam<unsigned int>(
42 "components", 1, "components>0", "Number of components for an array variable");
43
44 // Advanced input options
45 params.transferParam<std::vector<Real>>(AddVariableAction::validParams(), "scaling");
46 params.addParam<bool>("eigen", false, "True to make this variable an eigen variable");
47 params.addParam<bool>("fv", false, "True to make this variable a finite volume variable");
48 params.addParam<bool>("array",
49 false,
50 "True to make this variable a array variable regardless of number of "
51 "components. If 'components' > 1, this will automatically be set to true.");
52
53 params.addParam<std::vector<std::string>>(
54 "array_var_component_names",
55 "Only for use with array variables, allows setting custom names for each array variable "
56 "component. If this not set, the default name for each array variable componenet is "
57 "`base_name`+'_'+component number. If used, a name must be provided for each component and "
58 "the values are used to name the components as `base_name`+'_'+ "
59 "`array_var_component_names[component]`.");
60
61 params.addParam<SolverSystemName>("solver_sys",
62 "nl0",
63 "If this variable is a solver variable, this is the "
64 "solver system to which it should be added.");
65 params.transferParam<bool>(AddVariableAction::validParams(), "p_refinement");
66 params.transferParam<bool>(AddVariableAction::validParams(), "disable_p_refinement");
67
68 params.addParam<bool>("use_dual", false, "True to use dual basis for Lagrange multipliers");
69 params.transferParam<std::vector<Real>>(AddVariableAction::validParams(), "initial_condition");
70 params.transferParam<std::string>(AddVariableAction::validParams(), "initial_from_file_var");
71 params.addParamNamesToGroup("scaling eigen use_dual", "Advanced");
72
73 params.registerBase("MooseVariableBase");
74 params.addPrivateParam<SystemBase *>("_system_base");
75 params.addPrivateParam<FEProblemBase *>("_fe_problem_base");
76 params.addPrivateParam<Moose::VarKindType>("_var_kind");
77 params.addPrivateParam<unsigned int>("_var_num");
78 params.addPrivateParam<THREAD_ID>("tid");
79
81 "Base class for Moose variables. This should never be the terminal object type");
82 return params;
83}
84
86 : MooseObject(parameters),
88 OutputInterface(parameters),
89 SetupInterface(this),
90 _sys(*getParam<SystemBase *>("_system_base")), // TODO: get from _fe_problem_base
91 _fe_type(MooseUtils::variableFEType(parameters)),
92 _var_num(getParam<unsigned int>("_var_num")),
93 _is_eigen(getParam<bool>("eigen")),
94 _var_kind(getParam<Moose::VarKindType>("_var_kind")),
95 _subproblem(_sys.subproblem()),
96 _variable(_sys.system().variable(_var_num)),
97 _assembly(_subproblem.assembly(getParam<THREAD_ID>("_tid"),
98 (_var_kind == Moose::VAR_SOLVER) ? _sys.number() : 0)),
99 _dof_map(_sys.dofMap()),
100 _mesh(_subproblem.mesh()),
101 _tid(getParam<THREAD_ID>("tid")),
102 _count(getParam<unsigned int>("components")),
103 _scaling_factor(_count, 1.0),
104 _use_dual(getParam<bool>("use_dual"))
105{
106 scalingFactor(isParamValid("scaling") ? getParam<std::vector<Real>>("scaling")
107 : std::vector<Real>(_count, 1.));
108 if (getParam<bool>("fv") && getParam<bool>("eigen"))
109 paramError("eigen", "finite volume (fv=true) variables do not have eigen support");
110 if (getParam<bool>("fv") && _fe_type.family != MONOMIAL)
111 paramError("family", "finite volume (fv=true) variables must be have MONOMIAL family");
112 if (getParam<bool>("fv") && _fe_type.order != 0)
113 paramError("order", "finite volume (fv=true) variables currently support CONST order only");
114 if (getParam<bool>("fv") && _fe_type.p_refinement)
115 paramError("p_refinement", "finite volume (fv=true) variables do not support p-refinement");
116
117 if (isParamValid("array_var_component_names"))
118 {
119 auto name0 = _sys.system().variable(_var_num).name();
120 std::size_t found = name0.find_last_of("_");
121 if (found == std::string::npos)
122 mooseError("Error creating ArrayMooseVariable name with base name ", name0);
123 const auto name_base = name0.substr(0, found);
124 const auto & name_endings = getParam<std::vector<std::string>>("array_var_component_names");
125 for (const auto & name : name_endings)
126 _array_var_component_names.push_back(name_base + '_' + name);
127 }
128 else if (_count != 1)
129 mooseError("Component size of normal variable (_count) must be one; equals " +
130 std::to_string(_count) + "");
131
132 // check parameters set automatically by SystemBase related to array variables
133 mooseAssert(
134 isArray() ? _count == _array_var_component_names.size() : true,
135 "An inconsistent numer of names or no names were provided for array variable components");
136 if (_count > 1)
137 mooseAssert(isArray(), "Must be true with component > 1");
138}
139
140const std::string &
142{
143 mooseAssert(
145 "Requested array variable component number is greater than the number of component names.");
147}
148
149const std::vector<dof_id_type> &
151{
152 const auto it = _sys.subproblem()._var_dof_map.find(name());
153 if (it != _sys.subproblem()._var_dof_map.end())
154 return it->second;
155 else
156 mooseError("VariableAllDoFMap not prepared for ",
157 name(),
158 " . Check nonlocal coupling requirement for the variable.");
159}
160
161Order
163{
164 return _fe_type.order;
165}
166
167std::vector<dof_id_type>
168MooseVariableBase::componentDofIndices(const std::vector<dof_id_type> & dof_indices,
169 unsigned int component) const
170{
171 mooseAssert(dof_indices.size() % this->count() == 0,
172 "The dof indices container must be a multiple of count");
173 std::vector<dof_id_type> new_dof_indices(dof_indices.size() / this->count());
174 for (const auto i : index_range(new_dof_indices))
175 new_dof_indices[i] = dof_indices[component * new_dof_indices.size() + i];
176 return new_dof_indices;
177}
178
179void
180MooseVariableBase::scalingFactor(const std::vector<Real> & factor)
181{
182 mooseAssert(factor.size() == _count, "Inconsistent scaling factor size");
183 for (const auto i : make_range(_count))
184 _scaling_factor[i] = factor[i];
185}
186
187void
189{
190 // Currently the scaling vector is only used through AD residual computing objects
193 (std::find_if(_scaling_factor.begin(),
194 _scaling_factor.end(),
195 [](const Real element)
196 { return !MooseUtils::absoluteFuzzyEqual(element, 1.); }) !=
197 _scaling_factor.end())))
198
200}
201
202const NumericVector<Number> &
204{
205 // It's not safe to use solutionState(0) because it returns the libMesh System solution member
206 // which is wrong during things like finite difference Jacobian evaluation, e.g. when PETSc
207 // perturbs the solution vector we feed these perturbations into the current_local_solution
208 // while the libMesh solution is frozen in the non-perturbed state
209 return (state.state == 0) ? *this->_sys.currentSolution()
210 : this->_sys.solutionState(state.state, state.iteration_type);
211}
212
213bool
215{
216 mooseAssert(false,
217 "Base class should not assume anything regarding this. In an ideal world this would "
218 "be pure virtual but we can't do that because we register MooseVariableBase as an "
219 "available variable type for the user");
220 return {};
221}
222
223bool
225{
226 mooseAssert(false,
227 "Base class should not assume anything regarding this. In an ideal world this would "
228 "be pure virtual but we can't do that because we register MooseVariableBase as an "
229 "available variable type for the user");
230 return {};
231}
232
233const std::set<SubdomainID> &
238
239bool
241{
242 return this->_sys.system().variable(_var_num).active_on_subdomain(subdomain);
243}
244
245bool
246MooseVariableBase::activeOnSubdomains(const std::set<SubdomainID> & subdomains) const
247{
248 const auto & active_subs = activeSubdomains();
249 return std::includes(
250 active_subs.begin(), active_subs.end(), subdomains.begin(), subdomains.end());
251}
subdomain_id_type SubdomainID
unsigned int THREAD_ID
Definition MooseTypes.h:237
registerMooseObject("MooseApp", MooseVariableBase)
void ErrorVector unsigned int
static InputParameters validParams()
An interface that restricts an object to subdomains via the 'blocks' input parameter.
static InputParameters validParams()
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
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 transferParam(const InputParameters &source_param, const std::string &name, const std::string &new_name="", const std::string &new_description="")
A routine to transfer a parameter from one class' validParams to another.
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.
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
static InputParameters validParams()
Definition MooseObject.C:25
Base variable class.
Moose::VarKindType _var_kind
Variable type (see MooseTypes.h)
virtual bool supportsGeometricInfoBasedLoops() const
Whether this variable supports geometric-info-based loops, such as ElemInfo/FaceInfo loops.
unsigned int _var_num
variable number (from libMesh)
std::vector< dof_id_type > componentDofIndices(const std::vector< dof_id_type > &dof_indices, unsigned int component) const
Obtain DoF indices of a component with the indices of the 0th component.
bool activeOnSubdomains(const std::set< SubdomainID > &subdomains) const
Is the variable active on the subdomains?
std::vector< std::string > _array_var_component_names
Array variable names when the variable is an array variable.
Real scalingFactor() const
Get the scaling factor for this variable.
virtual bool isArray() const
SubProblem & _subproblem
Problem this variable is part of.
libMesh::FEType _fe_type
The FEType associated with this variable.
static InputParameters validParams()
const std::set< SubdomainID > & activeSubdomains() const
The subdomains the variable is active on.
MooseVariableBase(const InputParameters &parameters)
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
const libMesh::NumericVector< libMesh::Number > & getSolution(const Moose::StateArg &state) const
Get the solution corresponding to the provided state.
const std::string & arrayVariableComponent(const unsigned int i) const
Returns the variable name of a component of an array variable.
const std::vector< dof_id_type > & allDofIndices() const
Get all global dofindices for the variable.
std::vector< Real > _scaling_factor
scaling factor for this variable
const unsigned int _count
Number of variables in the array.
libMesh::Order order() const
Get the order of this variable Note: Order enum can be implicitly converted to unsigned int.
SystemBase & _sys
System this variable is part of.
bool activeOnSubdomain(SubdomainID subdomain) const
Is the variable active on the subdomain?
virtual bool supportsQpBasedLoops() const
Whether this variable supports QP-indexed loops.
A class to provide an common interface to objects requiring "outputs" option.
static InputParameters validParams()
std::map< std::string, std::vector< dof_id_type > > _var_dof_map
Definition SubProblem.h:682
virtual void haveADObjects(bool have_ad_objects)
Method for setting whether we have any ad objects.
Definition SubProblem.h:775
virtual void automaticScaling(bool automatic_scaling)
Automatic scaling setter.
Base class for a system (of equations)
Definition SystemBase.h:87
virtual const NumericVector< Number > *const & currentSolution() const =0
The solution vector that is currently being operated on.
SubProblem & subproblem()
Definition SystemBase.h:102
void addScalingVector()
Add the scaling factor vector to the 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).
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
OrderWrapper order
const Variable & variable(unsigned int var) const
const std::set< subdomain_id_type > & active_subdomains() const
const std::string & name() const
bool active_on_subdomain(subdomain_id_type sid) const
MeshBase & mesh
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
VarKindType
Framework-wide stuff.
Definition MooseTypes.h:769
@ VAR_SOLVER
Definition MooseTypes.h:770
State argument for evaluating functors.
SolutionIterationType iteration_type
The solution iteration type, e.g. time or nonlinear.
unsigned int state
The state.