https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
14#include "MooseVariableScalar.h"
15#include "MooseTypes.h"
16
18
19void
20VariableWarehouse::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
64void
69
70void
71VariableWarehouse::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
78void
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
90VariableWarehouse::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
100VariableWarehouse::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
109const std::vector<VariableName> &
111{
112 return _names;
113}
114
115const std::vector<MooseVariableFEBase *> &
117{
118 return _vars;
119}
120
121const std::vector<MooseVariableScalar *> &
123{
124 return _scalar_vars;
125}
126
127const std::set<const MooseVariableFEBase *> &
129{
130 return _boundary_vars.find(bnd)->second;
131}
132
133namespace
134{
135template <typename T, typename Map, typename Key>
137getFieldVariableHelper(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
151template <typename T>
153VariableWarehouse::getFieldVariable(const std::string & var_name)
154{
155 return getFieldVariableHelper<T>(_regular_vars_by_name, var_name, "_regular_vars_by_name");
156}
157
158template <typename T>
161{
162 return getFieldVariableHelper<T>(_regular_vars_by_number, var_number, "_regular_vars_by_number");
163}
164
165template <>
167VariableWarehouse::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
173template <>
175VariableWarehouse::getFieldVariable<RealVectorValue>(unsigned int var_number)
176{
177 return getFieldVariableHelper<RealVectorValue>(
178 _vector_vars_by_number, var_number, "_vector_vars_by_number");
179}
180
181template <>
183VariableWarehouse::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
189template <>
191VariableWarehouse::getFieldVariable<RealEigenVector>(unsigned int var_number)
192{
193 return getFieldVariableHelper<RealEigenVector>(
194 _array_vars_by_number, var_number, "_array_vars_by_number");
195}
196
197template MooseVariableFE<Real> *
198VariableWarehouse::getFieldVariable<Real>(const std::string & var_name);
199template MooseVariableFE<Real> * VariableWarehouse::getFieldVariable<Real>(unsigned int var_number);
200
201template <typename T>
203VariableWarehouse::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
214template <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
227template <>
229VariableWarehouse::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
236template <>
238VariableWarehouse::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
245template <>
247VariableWarehouse::getActualFieldVariable<RealEigenVector>(const std::string & var_name)
248{
249 return getFieldVariable<RealEigenVector>(var_name);
250}
251
252template <>
254VariableWarehouse::getActualFieldVariable<RealEigenVector>(unsigned int var_number)
255{
256 return getFieldVariable<RealEigenVector>(var_number);
257}
258
259void
261{
262 for (auto & pair : _all_objects)
263 pair.second->initialSetup();
264}
265
266void
268{
269 for (auto & pair : _all_objects)
270 pair.second->timestepSetup();
271}
272
273void
275{
276 for (auto & pair : _all_objects)
277 pair.second->customSetup(exec_type);
278}
279
280void
282{
283 for (auto & pair : _all_objects)
284 pair.second->subdomainSetup();
285}
286
287void
289{
290 for (auto & pair : _all_objects)
291 pair.second->jacobianSetup();
292}
293
294void
296{
297 for (auto & pair : _all_objects)
298 pair.second->residualSetup();
299}
300
301void
303{
304 for (auto * var : _vars)
305 var->clearAllDofIndices();
306}
307
308void
310{
311 for (auto * var : _vars)
312 var->setActiveTags(vtags);
313}
314
315void
317{
318 for (auto * var : _scalar_vars)
319 var->setActiveTags(vtags);
320}
321
323VariableWarehouse::getActualFieldVariable<Real>(const std::string & var_name);
325VariableWarehouse::getActualFieldVariable<Real>(unsigned int var_number);
boundary_id_type BoundaryID
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
char ** vars
Class for containing MooseEnum item information.
Base variable class.
This class provides an interface for common operations on field variables of both FE and FV types wit...
Class for stuff related to variables.
Class for scalar variables (they are different).
HashMap< std::string, VectorMooseVariable * > _vector_vars_by_name
map of vector finite element variables with name keys
MooseVariableBase * getVariable(const std::string &var_name) const
Get a variable from the warehouse.
const std::set< const MooseVariableFieldBase * > & boundaryVars(BoundaryID bnd) const
Get the list of variables that needs to be reinitialized on a given boundary.
void jacobianSetup()
Call jacobianSetup for all variables.
const std::vector< MooseVariableScalar * > & scalars() const
Get the list of scalar variables.
HashMap< unsigned, VectorMooseVariable * > _vector_vars_by_number
map of vector finite element variables with unsigned keys
void subdomainSetup()
Call subdomainSetup for all variables.
MooseVariableFE< T > * getFieldVariable(const std::string &var_name)
Get a finite element variable from the warehouse of either Real or RealVectorValue type.
void initialSetup()
Call initialSetup for all variables.
std::vector< MooseVariableFieldBase * > _vars
list of finite element variables
std::vector< MooseVariableScalar * > _scalar_vars
list of all scalar, non-finite element variables
void add(const std::string &var_name, std::shared_ptr< MooseVariableBase > var)
Add a variable.
void setActiveVariableCoupleableVectorTags(const std::set< TagID > &vtags)
Set the active vector tags for the variables.
HashMap< std::string, MooseVariable * > _regular_vars_by_name
map of non-vector finite element variables with name keys
HashMap< std::string, MooseLinearVariableFVReal * > _linear_fv_vars_by_name
map of non-vector linear finite volume 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, MooseLinearVariableFVReal * > _linear_fv_vars_by_number
map of non-vector finite element variables with unsigned keys
HashMap< unsigned, ArrayMooseVariable * > _array_vars_by_number
map of vector finite element variables with unsigned keys
void addBoundaryVar(BoundaryID bnd, const MooseVariableFieldBase *var)
Add a boundary variable.
HashMap< unsigned, MooseVariable * > _regular_vars_by_number
map of non-vector finite element variables with unsigned keys
HashMap< unsigned, MooseVariableFVReal * > _fv_vars_by_number
map of non-vector finite element variables with unsigned keys
void timestepSetup()
Call timestepSetup for all variables.
void clearAllDofIndices()
Clear all dof indices from each variable.
std::map< std::string, MooseVariableBase * > _var_name
Name to variable mapping.
const std::vector< MooseVariableFieldBase * > & fieldVariables() const
Get the list of variables.
MooseVariableField< T > * getActualFieldVariable(const std::string &var_name)
This should be called getFieldVariable, but that name is already taken by a legacy function.
std::vector< VariableName > _names
list of variable names
void residualSetup()
Call residualSetup for all variables.
std::map< unsigned int, std::shared_ptr< MooseVariableBase > > _all_objects
All instances of objects.
HashMap< std::string, ArrayMooseVariable * > _array_vars_by_name
map of vector finite element variables with name keys
const std::vector< VariableName > & names() const
Get the list of all variable names.
void customSetup(const ExecFlagType &exec_type)
Call setup on a particular execute flag for all variables.
HashMap< std::string, MooseVariableFVReal * > _fv_vars_by_name
map of non-vector finite element variables with name keys
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 setActiveScalarVariableCoupleableVectorTags(const std::set< TagID > &vtags)
Set the active vector tags for the variables.