https://mooseframework.inl.gov
VariableWarehouse.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 "VariableWarehouse.h"
11 #include "MooseVariableFE.h"
12 #include "MooseVariableFV.h"
13 #include "MooseLinearVariableFV.h"
14 #include "MooseVariableScalar.h"
15 #include "MooseTypes.h"
16 
18 
19 void
20 VariableWarehouse::add(const std::string & var_name, std::shared_ptr<MooseVariableBase> var)
21 {
22  _names.push_back(var_name);
23  auto * raw_var = var.get();
24  _all_objects[var->number()] = var;
25  _var_name[var_name] = raw_var;
26 
27  if (auto * tmp_var = dynamic_cast<MooseVariableFieldBase *>(raw_var))
28  {
29  _vars.push_back(tmp_var);
30  if (auto * tmp_var = dynamic_cast<MooseVariable *>(raw_var))
31  {
32  _regular_vars_by_number[tmp_var->number()] = tmp_var;
33  _regular_vars_by_name[var_name] = tmp_var;
34  }
35  else if (auto * tmp_var = dynamic_cast<MooseVariableFVReal *>(raw_var))
36  {
37  _fv_vars_by_number[tmp_var->number()] = tmp_var;
38  _fv_vars_by_name[var_name] = tmp_var;
39  }
40  else if (auto * tmp_var = dynamic_cast<MooseLinearVariableFVReal *>(raw_var))
41  {
42  _linear_fv_vars_by_number[tmp_var->number()] = tmp_var;
43  _linear_fv_vars_by_name[var_name] = tmp_var;
44  }
45  else if (auto * tmp_var = dynamic_cast<VectorMooseVariable *>(raw_var))
46  {
47  _vector_vars_by_number[tmp_var->number()] = tmp_var;
48  _vector_vars_by_name[var_name] = tmp_var;
49  }
50  else if (auto * tmp_var = dynamic_cast<ArrayMooseVariable *>(raw_var))
51  {
52  _array_vars_by_number[tmp_var->number()] = tmp_var;
53  _array_vars_by_name[var_name] = tmp_var;
54  }
55  else
56  mooseError("Unknown variable class passed into VariableWarehouse. Attempt to hack us?");
57  }
58  else if (auto * tmp_var = dynamic_cast<MooseVariableScalar *>(raw_var))
59  _scalar_vars.push_back(tmp_var);
60  else
61  mooseError("Unknown variable class passed into VariableWarehouse. Attempt to hack us?");
62 }
63 
64 void
66 {
67  _boundary_vars[bnd].insert(var);
68 }
69 
70 void
71 VariableWarehouse::addBoundaryVar(const std::set<BoundaryID> & boundary_ids,
72  const MooseVariableFEBase * var)
73 {
74  for (const auto & bid : boundary_ids)
75  addBoundaryVar(bid, var);
76 }
77 
78 void
80  const std::set<BoundaryID> & boundary_ids,
81  const std::unordered_map<std::string, std::vector<MooseVariableFEBase *>> & vars)
82 {
83  for (const auto & bid : boundary_ids)
84  for (const auto & it : vars)
85  for (const auto & var : it.second)
86  addBoundaryVar(bid, var);
87 }
88 
90 VariableWarehouse::getVariable(const std::string & var_name) const
91 {
92  auto it = _var_name.find(var_name);
93  if (it != _var_name.end())
94  return it->second;
95  else
96  return nullptr;
97 }
98 
100 VariableWarehouse::getVariable(unsigned int var_number) const
101 {
102  auto it = _all_objects.find(var_number);
103  if (it != _all_objects.end())
104  return it->second.get();
105  else
106  return nullptr;
107 }
108 
109 const std::vector<VariableName> &
111 {
112  return _names;
113 }
114 
115 const std::vector<MooseVariableFEBase *> &
117 {
118  return _vars;
119 }
120 
121 const std::vector<MooseVariableScalar *> &
123 {
124  return _scalar_vars;
125 }
126 
127 const std::set<const MooseVariableFEBase *> &
129 {
130  return _boundary_vars.find(bnd)->second;
131 }
132 
133 namespace
134 {
135 template <typename T, typename Map, typename Key>
137 getFieldVariableHelper(const Map & map, const Key & key, const std::string & container_name)
138 {
139  // TODO: the requested variable might be an FV variable - how to we
140  // reconcile this since this function returns an FE (not Field) pointer?
141  // crap tons of objects depend on this.
142 
143  auto it = map.find(key);
144  if (it == map.end())
145  mooseError("Key '", key, "' not found in VariableWarehouse container '", container_name, "'");
146 
147  return it->second;
148 }
149 }
150 
151 template <typename T>
153 VariableWarehouse::getFieldVariable(const std::string & var_name)
154 {
155  return getFieldVariableHelper<T>(_regular_vars_by_name, var_name, "_regular_vars_by_name");
156 }
157 
158 template <typename T>
160 VariableWarehouse::getFieldVariable(unsigned int var_number)
161 {
162  return getFieldVariableHelper<T>(_regular_vars_by_number, var_number, "_regular_vars_by_number");
163 }
164 
165 template <>
167 VariableWarehouse::getFieldVariable<RealVectorValue>(const std::string & var_name)
168 {
169  return getFieldVariableHelper<RealVectorValue>(
170  _vector_vars_by_name, var_name, "_vector_vars_by_name");
171 }
172 
173 template <>
175 VariableWarehouse::getFieldVariable<RealVectorValue>(unsigned int var_number)
176 {
177  return getFieldVariableHelper<RealVectorValue>(
178  _vector_vars_by_number, var_number, "_vector_vars_by_number");
179 }
180 
181 template <>
183 VariableWarehouse::getFieldVariable<RealEigenVector>(const std::string & var_name)
184 {
185  return getFieldVariableHelper<RealEigenVector>(
186  _array_vars_by_name, var_name, "_array_vars_by_name");
187 }
188 
189 template <>
191 VariableWarehouse::getFieldVariable<RealEigenVector>(unsigned int var_number)
192 {
193  return getFieldVariableHelper<RealEigenVector>(
194  _array_vars_by_number, var_number, "_array_vars_by_number");
195 }
196 
197 template MooseVariableFE<Real> *
198 VariableWarehouse::getFieldVariable<Real>(const std::string & var_name);
199 template MooseVariableFE<Real> * VariableWarehouse::getFieldVariable<Real>(unsigned int var_number);
200 
201 template <typename T>
203 VariableWarehouse::getActualFieldVariable(const std::string & var_name)
204 {
205  auto it = _regular_vars_by_name.find(var_name);
206  if (it != _regular_vars_by_name.end())
207  return it->second;
208  auto it_fv = _fv_vars_by_name.find(var_name);
209  if (it_fv != _fv_vars_by_name.end())
210  return it_fv->second;
211  return _linear_fv_vars_by_name.at(var_name);
212 }
213 
214 template <typename T>
217 {
218  auto it = _regular_vars_by_number.find(var_number);
219  if (it != _regular_vars_by_number.end())
220  return it->second;
221  auto it_fv = _fv_vars_by_number.find(var_number);
222  if (it_fv != _fv_vars_by_number.end())
223  return it_fv->second;
224  return _linear_fv_vars_by_number.at(var_number);
225 }
226 
227 template <>
229 VariableWarehouse::getActualFieldVariable<RealVectorValue>(const std::string & var_name)
230 {
231  // TODO: when necessary, add the if check to see if we have an FV vector var
232  // before just returning nothing as found in FE vars list.
233  return getFieldVariable<RealVectorValue>(var_name);
234 }
235 
236 template <>
238 VariableWarehouse::getActualFieldVariable<RealVectorValue>(unsigned int var_number)
239 {
240  // TODO: when necessary, add the if check to see if we have an FV vector var
241  // before just returning nothing as found in FE vars list.
242  return getFieldVariable<RealVectorValue>(var_number);
243 }
244 
245 template <>
247 VariableWarehouse::getActualFieldVariable<RealEigenVector>(const std::string & var_name)
248 {
249  return getFieldVariable<RealEigenVector>(var_name);
250 }
251 
252 template <>
254 VariableWarehouse::getActualFieldVariable<RealEigenVector>(unsigned int var_number)
255 {
256  return getFieldVariable<RealEigenVector>(var_number);
257 }
258 
259 void
261 {
262  for (auto & pair : _all_objects)
263  pair.second->initialSetup();
264 }
265 
266 void
268 {
269  for (auto & pair : _all_objects)
270  pair.second->timestepSetup();
271 }
272 
273 void
275 {
276  for (auto & pair : _all_objects)
277  pair.second->customSetup(exec_type);
278 }
279 
280 void
282 {
283  for (auto & pair : _all_objects)
284  pair.second->subdomainSetup();
285 }
286 
287 void
289 {
290  for (auto & pair : _all_objects)
291  pair.second->jacobianSetup();
292 }
293 
294 void
296 {
297  for (auto & pair : _all_objects)
298  pair.second->residualSetup();
299 }
300 
301 void
303 {
304  for (auto * var : _vars)
305  var->clearAllDofIndices();
306 }
307 
308 void
310 {
311  for (auto * var : _vars)
312  var->setActiveTags(vtags);
313 }
314 
315 void
317 {
318  for (auto * var : _scalar_vars)
319  var->setActiveTags(vtags);
320 }
321 
322 template MooseVariableField<Real> *
323 VariableWarehouse::getActualFieldVariable<Real>(const std::string & var_name);
324 template MooseVariableField<Real> *
325 VariableWarehouse::getActualFieldVariable<Real>(unsigned int var_number);
const std::vector< VariableName > & names() const
Get the list of all variable names.
void addBoundaryVar(BoundaryID bnd, const MooseVariableFieldBase *var)
Add a boundary variable.
MooseVariableBase * getVariable(const std::string &var_name) const
Get a variable from the warehouse.
void residualSetup()
Call residualSetup for all variables.
void setActiveScalarVariableCoupleableVectorTags(const std::set< TagID > &vtags)
Set the active vector tags for the variables.
void addBoundaryVars(const std::set< BoundaryID > &boundary_ids, const std::unordered_map< std::string, std::vector< MooseVariableFieldBase *>> &vars)
Add a map of variables to a set of boundaries.
void add(const std::string &var_name, std::shared_ptr< MooseVariableBase > var)
Add a variable.
std::vector< MooseVariableScalar * > _scalar_vars
list of all scalar, non-finite element variables
void jacobianSetup()
Call jacobianSetup for all variables.
void setActiveVariableCoupleableVectorTags(const std::set< TagID > &vtags)
Set the active vector tags for the variables.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
char ** vars
void initialSetup()
Call initialSetup for all variables.
void timestepSetup()
Call timestepSetup for all variables.
HashMap< unsigned, ArrayMooseVariable * > _array_vars_by_number
map of vector finite element variables with unsigned keys
This class provides an interface for common operations on field variables of both FE and FV types wit...
const std::vector< MooseVariableScalar * > & scalars() const
Get the list of scalar variables.
std::map< std::string, MooseVariableBase * > _var_name
Name to variable mapping.
HashMap< std::string, VectorMooseVariable * > _vector_vars_by_name
map of vector finite element variables with name keys
void clearAllDofIndices()
Clear all dof indices from each variable.
HashMap< std::string, MooseLinearVariableFVReal * > _linear_fv_vars_by_name
map of non-vector linear finite volume variables with name keys
boundary_id_type BoundaryID
std::map< unsigned int, std::shared_ptr< MooseVariableBase > > _all_objects
All instances of objects.
MooseVariableField< T > * getActualFieldVariable(const std::string &var_name)
This should be called getFieldVariable, but that name is already taken by a legacy function...
HashMap< unsigned, MooseVariableFVReal * > _fv_vars_by_number
map of non-vector finite element variables with unsigned keys
std::vector< MooseVariableFieldBase * > _vars
list of finite element variables
HashMap< std::string, ArrayMooseVariable * > _array_vars_by_name
map of vector finite element variables with name keys
std::map< BoundaryID, std::set< const MooseVariableFieldBase * > > _boundary_vars
Map to variables that need to be evaluated on a boundary.
HashMap< unsigned, MooseVariable * > _regular_vars_by_number
map of non-vector finite element variables with unsigned keys
std::vector< VariableName > _names
list of variable names
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
const std::set< const MooseVariableFieldBase * > & boundaryVars(BoundaryID bnd) const
Get the list of variables that needs to be reinitialized on a given boundary.
void subdomainSetup()
Call subdomainSetup for all variables.
HashMap< std::string, MooseVariable * > _regular_vars_by_name
map of non-vector finite element variables with name keys
MooseVariableFE< T > * getFieldVariable(const std::string &var_name)
Get a finite element variable from the warehouse of either Real or RealVectorValue type...
HashMap< unsigned, MooseLinearVariableFVReal * > _linear_fv_vars_by_number
map of non-vector finite element variables with unsigned keys
HashMap< unsigned, VectorMooseVariable * > _vector_vars_by_number
map of vector finite element variables with unsigned keys
const std::vector< MooseVariableFieldBase * > & fieldVariables() const
Get the list of variables.
HashMap< std::string, MooseVariableFVReal * > _fv_vars_by_name
map of non-vector finite element variables with name keys
void customSetup(const ExecFlagType &exec_type)
Call setup on a particular execute flag for all variables.