www.mooseframework.org
ScalarCoupleable.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "ScalarCoupleable.h"
16 
17 // MOOSE includes
18 #include "FEProblem.h"
19 #include "MooseVariableScalar.h"
20 #include "Problem.h"
21 #include "SubProblem.h"
22 
24  : _sc_parameters(moose_object->parameters()),
25  _sc_fe_problem(*_sc_parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
26  _sc_is_implicit(_sc_parameters.have_parameter<bool>("implicit")
27  ? _sc_parameters.get<bool>("implicit")
28  : true),
29  _coupleable_params(_sc_parameters)
30 {
31  SubProblem & problem = *_sc_parameters.get<SubProblem *>("_subproblem");
32 
33  THREAD_ID tid =
34  _sc_parameters.have_parameter<THREAD_ID>("_tid") ? _sc_parameters.get<THREAD_ID>("_tid") : 0;
35 
36  // Coupling
37  for (std::set<std::string>::const_iterator iter = _sc_parameters.coupledVarsBegin();
39  ++iter)
40  {
41  std::string name = *iter;
42  if (_sc_parameters.getVecMooseType(*iter) != std::vector<std::string>())
43  {
44  std::vector<std::string> vars = _sc_parameters.getVecMooseType(*iter);
45  for (const auto & coupled_var_name : vars)
46  {
47  if (problem.hasScalarVariable(coupled_var_name))
48  {
49  MooseVariableScalar * scalar_var = &problem.getScalarVariable(tid, coupled_var_name);
50  _coupled_scalar_vars[name].push_back(scalar_var);
51  _coupled_moose_scalar_vars.push_back(scalar_var);
52  }
53  else if (problem.hasVariable(coupled_var_name))
54  ; // ignore normal variables
55  else
56  mooseError("Coupled variable '" + coupled_var_name + "' was not found\n");
57  }
58  }
59  }
60 }
61 
63 {
64  for (auto & it : _default_value)
65  {
66  it.second->release();
67  delete it.second;
68  }
69 }
70 
71 const std::vector<MooseVariableScalar *> &
73 {
75 }
76 
77 bool
78 ScalarCoupleable::isCoupledScalar(const std::string & var_name, unsigned int i)
79 {
80  std::map<std::string, std::vector<MooseVariableScalar *>>::iterator it =
81  _coupled_scalar_vars.find(var_name);
82  if (it != _coupled_scalar_vars.end())
83  return (i < it->second.size());
84  else
85  {
86  // Make sure the user originally requested this value in the InputParameter syntax
87  if (!_coupleable_params.hasCoupledValue(var_name))
88  mooseError("The coupled scalar variable \"",
89  var_name,
90  "\" was never added to this objects's "
91  "InputParameters, please double-check "
92  "your spelling");
93 
94  return false;
95  }
96 }
97 
98 unsigned int
99 ScalarCoupleable::coupledScalar(const std::string & var_name, unsigned int comp)
100 {
101  return getScalarVar(var_name, comp)->number();
102 }
103 
104 Order
105 ScalarCoupleable::coupledScalarOrder(const std::string & var_name, unsigned int comp)
106 {
107  if (!isCoupledScalar(var_name, comp))
109 
110  return getScalarVar(var_name, comp)->order();
111 }
112 
114 ScalarCoupleable::getDefaultValue(const std::string & var_name)
115 {
116  std::map<std::string, VariableValue *>::iterator default_value_it = _default_value.find(var_name);
117  if (default_value_it == _default_value.end())
118  {
121  default_value_it = _default_value.insert(std::make_pair(var_name, value)).first;
122  }
123 
124  return default_value_it->second;
125 }
126 
128 ScalarCoupleable::coupledScalarValue(const std::string & var_name, unsigned int comp)
129 {
130  if (!isCoupledScalar(var_name, comp))
131  return *getDefaultValue(var_name);
132 
133  MooseVariableScalar * var = getScalarVar(var_name, comp);
134  return (_sc_is_implicit) ? var->sln() : var->slnOld();
135 }
136 
138 ScalarCoupleable::coupledScalarValueOld(const std::string & var_name, unsigned int comp)
139 {
140  if (!isCoupledScalar(var_name, comp))
141  return *getDefaultValue(var_name);
142 
143  MooseVariableScalar * var = getScalarVar(var_name, comp);
144  return (_sc_is_implicit) ? var->slnOld() : var->slnOlder();
145 }
146 
148 ScalarCoupleable::coupledScalarValueOlder(const std::string & var_name, unsigned int comp)
149 {
150  if (!isCoupledScalar(var_name, comp))
151  return *getDefaultValue(var_name);
152 
153  MooseVariableScalar * var = getScalarVar(var_name, comp);
154  if (_sc_is_implicit)
155  return var->slnOlder();
156  else
157  mooseError("Older values not available for explicit schemes");
158 }
159 
161 ScalarCoupleable::coupledScalarDot(const std::string & var_name, unsigned int comp)
162 {
163  MooseVariableScalar * var = getScalarVar(var_name, comp);
164  return var->uDot();
165 }
166 
168 ScalarCoupleable::coupledScalarDotDu(const std::string & var_name, unsigned int comp)
169 {
170  MooseVariableScalar * var = getScalarVar(var_name, comp);
171  return var->duDotDu();
172 }
173 
175 ScalarCoupleable::getScalarVar(const std::string & var_name, unsigned int comp)
176 {
177  if (_coupled_scalar_vars.find(var_name) != _coupled_scalar_vars.end())
178  {
179  if (comp < _coupled_scalar_vars[var_name].size())
180  return _coupled_scalar_vars[var_name][comp];
181  else
182  mooseError("Trying to get a non-existent component of variable '" + var_name + "'");
183  }
184  else
185  mooseError("Trying to get a non-existent variable '" + var_name + "'");
186 }
187 
188 unsigned int
189 ScalarCoupleable::coupledScalarComponents(const std::string & var_name)
190 {
191  return _coupled_scalar_vars[var_name].size();
192 }
virtual bool hasVariable(const std::string &var_name)=0
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.
bool hasCoupledValue(const std::string &coupling_name) const
Return whether or not the coupled variable exists.
std::set< std::string >::const_iterator coupledVarsEnd() const
virtual bool isCoupledScalar(const std::string &var_name, unsigned int i=0)
Returns true if a variables has been coupled_as name.
MooseArray< Real > VariableValue
virtual ~ScalarCoupleable()
Destructor for object.
std::vector< std::string > getVecMooseType(const std::string &name) const
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name)=0
Returns the scalar variable reference from whichever system contains it.
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
VariableValue & uDot()
VariableValue * getDefaultValue(const std::string &var_name)
Helper method to return (and insert if necessary) the default value for an uncoupled variable...
Order getMaxScalarOrder() const
virtual unsigned int coupledScalar(const std::string &var_name, unsigned int comp=0)
Returns the index for a scalar coupled variable by name.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
const InputParameters & _coupleable_params
Local InputParameters.
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.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:36
Order order() const
Get the order of this variable Note: Order enum can be implicitly converted to unsigned int...
virtual Order coupledScalarOrder(const std::string &var_name, unsigned int comp=0)
Returns the order for a scalar coupled variable by name.
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.
Real defaultCoupledValue(const std::string &coupling_name) const
Get the default value for an optionally coupled variable.
unsigned int number() const
Get variable number coming from libMesh.
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:53
ScalarCoupleable(const MooseObject *moose_object)
Constructing the object.
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.
std::set< std::string >::const_iterator coupledVarsBegin() const
Methods returning iterators to the coupled variables names stored in this InputParameters object...
VariableValue & slnOlder()
virtual bool hasScalarVariable(const std::string &var_name)=0
Returns a Boolean indicating whether any system contains a variable with the name provided...
VariableValue & slnOld()
unsigned int THREAD_ID
Definition: MooseTypes.h:79
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.