www.mooseframework.org
ScalarCoupleable.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "ScalarCoupleable.h"
11 
12 // MOOSE includes
13 #include "FEProblem.h"
14 #include "MooseVariableFEBase.h"
15 #include "MooseVariableScalar.h"
16 #include "Problem.h"
17 #include "SubProblem.h"
18 
20  : _sc_parameters(moose_object->parameters()),
21  _sc_name(_sc_parameters.get<std::string>("_object_name")),
22  _sc_fe_problem(*_sc_parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
23  _sc_is_implicit(_sc_parameters.have_parameter<bool>("implicit")
24  ? _sc_parameters.get<bool>("implicit")
25  : true),
26  _coupleable_params(_sc_parameters),
27  _sc_tid(_sc_parameters.isParamValid("_tid") ? _sc_parameters.get<THREAD_ID>("_tid") : 0),
28  _real_zero(_sc_fe_problem._real_zero[_sc_tid]),
29  _scalar_zero(_sc_fe_problem._scalar_zero[_sc_tid]),
30  _point_zero(_sc_fe_problem._point_zero[_sc_tid])
31 {
32  SubProblem & problem = *_sc_parameters.getCheckedPointerParam<SubProblem *>("_subproblem");
33 
34  // Coupling
35  for (std::set<std::string>::const_iterator iter = _sc_parameters.coupledVarsBegin();
37  ++iter)
38  {
39  std::string name = *iter;
40  if (_sc_parameters.getVecMooseType(*iter) != std::vector<std::string>())
41  {
42  std::vector<std::string> vars = _sc_parameters.getVecMooseType(*iter);
43  for (const auto & coupled_var_name : vars)
44  {
45  if (problem.hasScalarVariable(coupled_var_name))
46  {
47  MooseVariableScalar * scalar_var = &problem.getScalarVariable(_sc_tid, coupled_var_name);
48  _coupled_scalar_vars[name].push_back(scalar_var);
49  _coupled_moose_scalar_vars.push_back(scalar_var);
50  }
51  else if (problem.hasVariable(coupled_var_name))
52  {
53  MooseVariableFEBase * moose_var =
54  &problem.getVariable(_sc_tid,
55  coupled_var_name,
58  _sc_coupled_vars[name].push_back(moose_var);
59  }
60  else
61  mooseError(_sc_name, ": Coupled variable '", coupled_var_name, "' was not found");
62  }
63  }
64  }
65 }
66 
68 {
69  for (auto & it : _default_value)
70  {
71  it.second->release();
72  delete it.second;
73  }
74 }
75 
76 const std::vector<MooseVariableScalar *> &
78 {
80 }
81 
82 bool
83 ScalarCoupleable::isCoupledScalar(const std::string & var_name, unsigned int i)
84 {
85  std::map<std::string, std::vector<MooseVariableScalar *>>::iterator it =
86  _coupled_scalar_vars.find(var_name);
87  if (it != _coupled_scalar_vars.end())
88  return (i < it->second.size());
89  else
90  {
91  // Make sure the user originally requested this value in the InputParameter syntax
92  if (!_coupleable_params.hasCoupledValue(var_name))
94  ": The coupled scalar variable \"",
95  var_name,
96  "\" was never added to this objects's "
97  "InputParameters, please double-check "
98  "your spelling");
99 
100  return false;
101  }
102 }
103 
104 unsigned int
105 ScalarCoupleable::coupledScalar(const std::string & var_name, unsigned int comp)
106 {
107  checkVar(var_name);
108  return getScalarVar(var_name, comp)->number();
109 }
110 
111 Order
112 ScalarCoupleable::coupledScalarOrder(const std::string & var_name, unsigned int comp)
113 {
114  checkVar(var_name);
115  if (!isCoupledScalar(var_name, comp))
117 
118  return getScalarVar(var_name, comp)->order();
119 }
120 
122 ScalarCoupleable::getDefaultValue(const std::string & var_name)
123 {
124  std::map<std::string, VariableValue *>::iterator default_value_it = _default_value.find(var_name);
125  if (default_value_it == _default_value.end())
126  {
129  default_value_it = _default_value.insert(std::make_pair(var_name, value)).first;
130  }
131 
132  return default_value_it->second;
133 }
134 
136 ScalarCoupleable::coupledScalarValue(const std::string & var_name, unsigned int comp)
137 {
138  checkVar(var_name);
139  if (!isCoupledScalar(var_name, comp))
140  return *getDefaultValue(var_name);
141 
142  MooseVariableScalar * var = getScalarVar(var_name, comp);
143  return (_sc_is_implicit) ? var->sln() : var->slnOld();
144 }
145 
148  TagID tag,
149  unsigned int comp)
150 {
151  checkVar(var_name);
152  if (!isCoupledScalar(var_name, comp))
153  return *getDefaultValue(var_name);
154 
156 
157  MooseVariableScalar * var = getScalarVar(var_name, comp);
158  return var->vectorTagSln(tag);
159 }
160 
163  TagID tag,
164  unsigned int comp)
165 {
166  checkVar(var_name);
167  if (!isCoupledScalar(var_name, comp))
168  return *getDefaultValue(var_name);
169 
171 
172  MooseVariableScalar * var = getScalarVar(var_name, comp);
173  return var->matrixTagSln(tag);
174 }
175 
177 ScalarCoupleable::coupledScalarValueOld(const std::string & var_name, unsigned int comp)
178 {
179  checkVar(var_name);
180  if (!isCoupledScalar(var_name, comp))
181  return *getDefaultValue(var_name);
182 
183  validateExecutionerType(var_name, "coupledScalarValueOld");
184  MooseVariableScalar * var = getScalarVar(var_name, comp);
185  return (_sc_is_implicit) ? var->slnOld() : var->slnOlder();
186 }
187 
189 ScalarCoupleable::coupledScalarValueOlder(const std::string & var_name, unsigned int comp)
190 {
191  checkVar(var_name);
192  if (!isCoupledScalar(var_name, comp))
193  return *getDefaultValue(var_name);
194 
195  validateExecutionerType(var_name, "coupledScalarValueOlder");
196  MooseVariableScalar * var = getScalarVar(var_name, comp);
197  if (_sc_is_implicit)
198  return var->slnOlder();
199  else
200  mooseError("Older values not available for explicit schemes");
201 }
202 
204 ScalarCoupleable::coupledScalarDot(const std::string & var_name, unsigned int comp)
205 {
206  checkVar(var_name);
207  validateExecutionerType(var_name, "coupledScalarDot");
208  MooseVariableScalar * var = getScalarVar(var_name, comp);
209  return var->uDot();
210 }
211 
213 ScalarCoupleable::coupledScalarDotDot(const std::string & var_name, unsigned int comp)
214 {
215  checkVar(var_name);
216  validateExecutionerType(var_name, "coupledScalarDotDot");
217  MooseVariableScalar * var = getScalarVar(var_name, comp);
218  return var->uDotDot();
219 }
220 
222 ScalarCoupleable::coupledScalarDotOld(const std::string & var_name, unsigned int comp)
223 {
224  checkVar(var_name);
225  validateExecutionerType(var_name, "coupledScalarDotOld");
226  MooseVariableScalar * var = getScalarVar(var_name, comp);
227  return var->uDotOld();
228 }
229 
231 ScalarCoupleable::coupledScalarDotDotOld(const std::string & var_name, unsigned int comp)
232 {
233  checkVar(var_name);
234  validateExecutionerType(var_name, "coupledScalarDotDotOld");
235  MooseVariableScalar * var = getScalarVar(var_name, comp);
236  return var->uDotDotOld();
237 }
239 ScalarCoupleable::coupledScalarDotDu(const std::string & var_name, unsigned int comp)
240 {
241  checkVar(var_name);
242  validateExecutionerType(var_name, "coupledScalarDotDu");
243  MooseVariableScalar * var = getScalarVar(var_name, comp);
244  return var->duDotDu();
245 }
246 
248 ScalarCoupleable::coupledScalarDotDotDu(const std::string & var_name, unsigned int comp)
249 {
250  checkVar(var_name);
251  validateExecutionerType(var_name, "coupledScalarDotDotDu");
252  MooseVariableScalar * var = getScalarVar(var_name, comp);
253  return var->duDotDotDu();
254 }
255 
256 void
257 ScalarCoupleable::checkVar(const std::string & var_name)
258 {
259  auto it = _sc_coupled_vars.find(var_name);
260  if (it != _sc_coupled_vars.end())
261  {
262  std::string cvars;
263  for (auto jt : it->second)
264  cvars += " " + jt->name();
266  ": Trying to couple a field variable where scalar variable is expected, '",
267  var_name,
268  " =",
269  cvars,
270  "'");
271  }
272  // NOTE: non-existent variables are handled in the constructor
273 }
274 
276 ScalarCoupleable::getScalarVar(const std::string & var_name, unsigned int comp)
277 {
278  if (_coupled_scalar_vars.find(var_name) != _coupled_scalar_vars.end())
279  {
280  if (comp < _coupled_scalar_vars[var_name].size())
281  return _coupled_scalar_vars[var_name][comp];
282  else
283  mooseError(_sc_name, ": Trying to get a non-existent component of variable '", var_name, "'");
284  }
285  else
286  mooseError(_sc_name, ": Trying to get a non-existent variable '", var_name, "'");
287 }
288 
289 void
291  const std::string & fn_name) const
292 {
295  ": Calling '",
296  fn_name,
297  "' on variable \"",
298  name,
299  "\" when using a \"Steady\" executioner is not allowed. This value is available "
300  "only in transient simulations.");
301 }
302 
303 unsigned int
304 ScalarCoupleable::coupledScalarComponents(const std::string & var_name)
305 {
306  return _coupled_scalar_vars[var_name].size();
307 }
std::map< std::string, std::vector< MooseVariableFEBase * > > _sc_coupled_vars
Field variables coupled into this object (for error checking)
bool hasCoupledValue(const std::string &coupling_name) const
Return whether or not the coupled variable exists.
VariableValue & sln()
const std::vector< MooseVariableScalar * > & getCoupledMooseScalarVars()
Get the list of coupled scalar variables.
virtual unsigned int coupledScalarComponents(const std::string &var_name)
Return the number of components to the coupled scalar variable.
unsigned int TagID
Definition: MooseTypes.h:162
virtual bool isCoupledScalar(const std::string &var_name, unsigned int i=0)
Returns true if a variables has been coupled_as name.
virtual VariableValue & coupledScalarDotDot(const std::string &var_name, unsigned int comp=0)
Returns the second time derivative of a scalar coupled variable.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
unsigned int number() const
Get variable number coming from libMesh.
virtual ~ScalarCoupleable()
Destructor for object.
virtual VariableValue & coupledScalarDotDotOld(const std::string &var_name, unsigned int comp=0)
Returns the old second time derivative of a scalar coupled variable.
virtual MooseVariableFEBase & getVariable(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)=0
Returns the variable reference for requested variable which must be of the expected_var_type (Nonline...
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name)=0
Returns the scalar variable reference from whichever system contains it.
T getCheckedPointerParam(const std::string &name, const std::string &error_string="") const
Verifies that the requested parameter exists and is not NULL and returns it to the caller...
virtual VariableValue & coupledScalarDotOld(const std::string &var_name, unsigned int comp=0)
Returns the old time derivative of a scalar coupled variable.
std::set< std::string >::const_iterator coupledVarsBegin() const
Methods returning iterators to the coupled variables names stored in this InputParameters object...
void checkVar(const std::string &var_name)
Check that the right kind of variable is being coupled in.
const std::string & _sc_name
The name of the object this interface is part of.
virtual VariableValue & coupledScalarDotDu(const std::string &var_name, unsigned int comp=0)
Time derivative of a scalar coupled variable with respect to the coefficients.
FEProblemBase & _sc_fe_problem
void validateExecutionerType(const std::string &name, const std::string &fn_name) const
Checks to make sure that the current Executioner has set "_is_transient" when old/older values are co...
VariableValue & uDot()
VariableValue & duDotDotDu()
VariableValue * getDefaultValue(const std::string &var_name)
Helper method to return (and insert if necessary) the default value for an uncoupled variable...
virtual unsigned int coupledScalar(const std::string &var_name, unsigned int comp=0)
Returns the index for a scalar coupled variable by name.
virtual VariableValue & coupledMatrixTagScalarValue(const std::string &var_name, TagID tag, unsigned int comp=0)
Returns value of a scalar coupled variable.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
const InputParameters & _coupleable_params
Local InputParameters.
THREAD_ID _sc_tid
Thread ID of the thread using this object.
VariableValue & duDotDu()
MooseVariableScalar * getScalarVar(const std::string &var_name, unsigned int comp)
Extract pointer to a scalar coupled variable.
bool _sc_is_implicit
True if implicit value is required.
virtual VariableValue & coupledScalarValueOld(const std::string &var_name, unsigned int comp=0)
Returns the old (previous time step) value of a scalar coupled variable.
std::set< std::string >::const_iterator coupledVarsEnd() const
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
Real defaultCoupledValue(const std::string &coupling_name, unsigned int i=0) const
Get the default value for an optionally coupled variable.
virtual bool hasScalarVariable(const std::string &var_name) const =0
Returns a Boolean indicating whether any system contains a variable with the name provided...
virtual VariableValue & coupledVectorTagScalarValue(const std::string &var_name, TagID tag, unsigned int comp=0)
Returns value of a scalar coupled variable.
VariableValue & matrixTagSln(TagID tag)
virtual Order coupledScalarOrder(const std::string &var_name, unsigned int comp=0)
Returns the order for a scalar coupled variable by name.
virtual bool hasVariable(const std::string &var_name) const =0
void addScalarVariableCoupleableVectorTag(TagID tag)
std::map< std::string, std::vector< MooseVariableScalar * > > _coupled_scalar_vars
Coupled vars whose values we provide.
virtual VariableValue & coupledScalarValue(const std::string &var_name, unsigned int comp=0)
Returns value of a scalar coupled variable.
const InputParameters & _sc_parameters
std::map< std::string, VariableValue * > _default_value
Will hold the default value for optional coupled scalar variables.
OutputTools< Real >::VariableValue VariableValue
Definition: MooseTypes.h:197
Order order() const
Get the order of this variable Note: Order enum can be implicitly converted to unsigned int...
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:59
ScalarCoupleable(const MooseObject *moose_object)
Constructing the object.
VariableValue & uDotDotOld()
std::vector< MooseVariableScalar * > _coupled_moose_scalar_vars
Vector of coupled variables.
Class for scalar variables (they are different).
virtual VariableValue & coupledScalarDot(const std::string &var_name, unsigned int comp=0)
Returns the time derivative of a scalar coupled variable.
virtual VariableValue & coupledScalarDotDotDu(const std::string &var_name, unsigned int comp=0)
Second time derivative of a scalar coupled variable with respect to the coefficients.
VariableValue & uDotDot()
VariableValue & slnOlder()
virtual bool isTransient() const override
VariableValue & vectorTagSln(TagID tag)
std::vector< std::string > getVecMooseType(const std::string &name) const
VariableValue & uDotOld()
VariableValue & slnOld()
void addScalarVariableCoupleableMatrixTag(TagID tag)
Order getMaxScalarOrder() const
unsigned int THREAD_ID
Definition: MooseTypes.h:161
virtual VariableValue & coupledScalarValueOlder(const std::string &var_name, unsigned int comp=0)
Returns the older (two time steps previous) value of a scalar coupled variable.