forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddVariableAction.C
200 lines (172 loc) · 6.48 KB
/
AddVariableAction.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
// Standard includes
#include <sstream>
#include <stdexcept>
// MOOSE includes
#include "AddVariableAction.h"
#include "FEProblem.h"
#include "Factory.h"
#include "MooseEnum.h"
#include "MooseEigenSystem.h"
#include "MooseObjectAction.h"
#include "MooseMesh.h"
#include "libmesh/libmesh.h"
#include "libmesh/exodusII_io.h"
#include "libmesh/equation_systems.h"
#include "libmesh/nonlinear_implicit_system.h"
#include "libmesh/explicit_system.h"
#include "libmesh/string_to_enum.h"
// class static initialization
const Real AddVariableAction::_abs_zero_tol = 1e-12;
registerMooseAction("MooseApp", AddVariableAction, "add_variable");
template <>
InputParameters
validParams<AddVariableAction>()
{
// Get MooseEnums for the possible order/family options for this variable
MooseEnum families(AddVariableAction::getNonlinearVariableFamilies());
MooseEnum orders(AddVariableAction::getNonlinearVariableOrders());
// Define the general input options
InputParameters params = validParams<Action>();
params += validParams<OutputInterface>();
params.addParam<MooseEnum>(
"family", families, "Specifies the family of FE shape functions to use for this variable");
params.addParam<MooseEnum>("order",
orders,
"Specifies the order of the FE shape function to use "
"for this variable (additional orders not listed are "
"allowed)");
params.addParam<unsigned int>("component", 1, "Number of component for an array variable");
params.addParam<std::vector<Real>>("initial_condition",
"Specifies the initial condition for this variable");
params.addParam<std::vector<SubdomainName>>("block", "The block id where this variable lives");
params.addParam<bool>("eigen", false, "True to make this variable an eigen variable");
// Advanced input options
params.addParam<std::vector<Real>>("scaling",
"Specifies a scaling factor to apply to this variable");
params.addParamNamesToGroup("scaling eigen", "Advanced");
return params;
}
AddVariableAction::AddVariableAction(InputParameters params)
: Action(params),
OutputInterface(params, false),
_fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family"))),
_scalar_var(_fe_type.family == SCALAR),
_component(getParam<unsigned int>("component"))
{
}
MooseEnum
AddVariableAction::getNonlinearVariableFamilies()
{
return MooseEnum("LAGRANGE MONOMIAL HERMITE SCALAR HIERARCHIC CLOUGH XYZ SZABAB BERNSTEIN "
"L2_LAGRANGE L2_HIERARCHIC NEDELEC_ONE LAGRANGE_VEC",
"LAGRANGE");
}
MooseEnum
AddVariableAction::getNonlinearVariableOrders()
{
return MooseEnum("CONSTANT FIRST SECOND THIRD FOURTH", "FIRST", true);
}
void
AddVariableAction::act()
{
// Get necessary data for creating a variable
std::string var_name = name();
addVariable(var_name);
// Set the initial condition
if (isParamValid("initial_condition"))
createInitialConditionAction();
}
void
AddVariableAction::createInitialConditionAction()
{
// Variable name
std::string var_name = name();
// Create the object name
std::string long_name("");
long_name += var_name;
long_name += "_moose";
// Set the parameters for the action
InputParameters action_params = _action_factory.getValidParams("AddOutputAction");
action_params.set<ActionWarehouse *>("awh") = &_awh;
if (_scalar_var)
action_params.set<std::string>("type") = "ScalarConstantIC";
else if (_component == 1)
action_params.set<std::string>("type") = "ConstantIC";
else
action_params.set<std::string>("type") = "ArrayConstantIC";
// Create the action
std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
_action_factory.create("AddInitialConditionAction", long_name, action_params));
// Set the required parameters for the object to be created
action->getObjectParams().set<VariableName>("variable") = var_name;
auto value = getParam<std::vector<Real>>("initial_condition");
if (value.size() != _component)
mooseError("Size of 'initial_condition' is not consistent");
if (_component > 1)
{
RealArrayValue v(_component);
for (unsigned int i = 0; i < _component; ++i)
v(i) = value[i];
action->getObjectParams().set<RealArrayValue>("value") = v;
}
else
action->getObjectParams().set<Real>("value") = value[0];
// Store the action in the ActionWarehouse
_awh.addActionBlock(action);
}
void
AddVariableAction::addVariable(const std::string & var_name)
{
std::set<SubdomainID> blocks = getSubdomainIDs();
std::vector<Real> scale_factor = isParamValid("scaling") ? getParam<std::vector<Real>>("scaling")
: std::vector<Real>(_component, 1);
if (scale_factor.size() != _component)
mooseError("Size of 'scaling' is not consistent");
// Scalar variable
if (_scalar_var)
_problem->addScalarVariable(var_name, _fe_type.order, scale_factor[0]);
// Block restricted variable
else if (_component == 1)
{
if (blocks.empty())
_problem->addVariable(var_name, _fe_type, scale_factor[0]);
// Non-block restricted variable
else
_problem->addVariable(var_name, _fe_type, scale_factor[0], &blocks);
}
else
{
if (blocks.empty())
_problem->addArrayVariable(var_name, _fe_type, _component, scale_factor);
// Non-block restricted variable
else
_problem->addArrayVariable(var_name, _fe_type, _component, scale_factor, &blocks);
}
if (getParam<bool>("eigen"))
{
MooseEigenSystem & esys(static_cast<MooseEigenSystem &>(_problem->getNonlinearSystemBase()));
esys.markEigenVariable(var_name);
}
}
std::set<SubdomainID>
AddVariableAction::getSubdomainIDs()
{
// Extract and return the block ids supplied in the input
std::set<SubdomainID> blocks;
std::vector<SubdomainName> block_param = getParam<std::vector<SubdomainName>>("block");
for (const auto & subdomain_name : block_param)
{
SubdomainID blk_id = _problem->mesh().getSubdomainID(subdomain_name);
blocks.insert(blk_id);
}
return blocks;
}