www.mooseframework.org
AddVariableAction.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 // Standard includes
16 #include <sstream>
17 #include <stdexcept>
18 
19 // MOOSE includes
20 #include "AddVariableAction.h"
21 #include "FEProblem.h"
22 #include "Factory.h"
23 #include "MooseEnum.h"
24 #include "MooseEigenSystem.h"
25 #include "MooseObjectAction.h"
26 #include "MooseMesh.h"
27 
28 #include "libmesh/libmesh.h"
29 #include "libmesh/exodusII_io.h"
30 #include "libmesh/equation_systems.h"
31 #include "libmesh/nonlinear_implicit_system.h"
32 #include "libmesh/explicit_system.h"
33 #include "libmesh/string_to_enum.h"
34 
35 // class static initialization
36 const Real AddVariableAction::_abs_zero_tol = 1e-12;
37 
38 template <>
41 {
42  // Get MooseEnums for the possible order/family options for this variable
45 
46  // Define the general input options
48  params += validParams<OutputInterface>();
49  params.addParam<MooseEnum>(
50  "family", families, "Specifies the family of FE shape functions to use for this variable");
51  params.addParam<MooseEnum>("order",
52  orders,
53  "Specifies the order of the FE shape function to use "
54  "for this variable (additional orders not listed are "
55  "allowed)");
56  params.addParam<Real>("initial_condition", "Specifies the initial condition for this variable");
57  params.addParam<std::vector<SubdomainName>>("block", "The block id where this variable lives");
58  params.addParam<bool>("eigen", false, "True to make this variable an eigen variable");
59 
60  // Advanced input options
61  params.addParam<Real>("scaling", 1.0, "Specifies a scaling factor to apply to this variable");
62  params.addParamNamesToGroup("scaling eigen", "Advanced");
63 
64  return params;
65 }
66 
68  : Action(params),
69  OutputInterface(params, false),
70  _fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
71  Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family"))),
72  _scalar_var(_fe_type.family == SCALAR)
73 {
74 }
75 
78 {
79  return MooseEnum("LAGRANGE MONOMIAL HERMITE SCALAR HIERARCHIC CLOUGH XYZ SZABAB BERNSTEIN "
80  "L2_LAGRANGE L2_HIERARCHIC",
81  "LAGRANGE");
82 }
83 
86 {
87  return MooseEnum("CONSTANT FIRST SECOND THIRD FOURTH", "FIRST", true);
88 }
89 
90 void
92 {
93  // Get necessary data for creating a variable
94  std::string var_name = name();
95  addVariable(var_name);
96 
97  // Set the initial condition
98  if (isParamValid("initial_condition"))
100 }
101 
102 void
104 {
105  // Variable name
106  std::string var_name = name();
107 
108  // Create the object name
109  std::string long_name("");
110  long_name += var_name;
111  long_name += "_moose";
112 
113  // Set the parameters for the action
114  InputParameters action_params = _action_factory.getValidParams("AddOutputAction");
115  action_params.set<ActionWarehouse *>("awh") = &_awh;
116 
117  if (_scalar_var)
118  action_params.set<std::string>("type") = "ScalarConstantIC";
119  else
120  action_params.set<std::string>("type") = "ConstantIC";
121 
122  // Create the action
123  std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
124  _action_factory.create("AddInitialConditionAction", long_name, action_params));
125 
126  // Set the required parameters for the object to be created
127  action->getObjectParams().set<VariableName>("variable") = var_name;
128  action->getObjectParams().set<Real>("value") = getParam<Real>("initial_condition");
129 
130  // Store the action in the ActionWarehouse
131  _awh.addActionBlock(action);
132 }
133 
134 void
135 AddVariableAction::addVariable(std::string & var_name)
136 {
137  std::set<SubdomainID> blocks = getSubdomainIDs();
138  Real scale_factor = isParamValid("scaling") ? getParam<Real>("scaling") : 1;
139 
140  // Scalar variable
141  if (_scalar_var)
142  _problem->addScalarVariable(var_name, _fe_type.order, scale_factor);
143 
144  // Block restricted variable
145  else if (blocks.empty())
146  _problem->addVariable(var_name, _fe_type, scale_factor);
147 
148  // Non-block restricted variable
149  else
150  _problem->addVariable(var_name, _fe_type, scale_factor, &blocks);
151 
152  if (getParam<bool>("eigen"))
153  {
154  MooseEigenSystem & esys(static_cast<MooseEigenSystem &>(_problem->getNonlinearSystemBase()));
155  esys.markEigenVariable(var_name);
156  }
157 }
158 
159 std::set<SubdomainID>
161 {
162  // Extract and return the block ids supplied in the input
163  std::set<SubdomainID> blocks;
164  std::vector<SubdomainName> block_param = getParam<std::vector<SubdomainName>>("block");
165  for (const auto & subdomain_name : block_param)
166  {
167  SubdomainID blk_id = _problem->mesh().getSubdomainID(subdomain_name);
168  blocks.insert(blk_id);
169  }
170  return blocks;
171 }
AddVariableAction(InputParameters params)
Class constructor.
std::set< SubdomainID > getSubdomainIDs()
Get the block ids from the input parameters.
bool _scalar_var
True if the variable being created is a scalar.
virtual void act() override
ActionWarehouse & _awh
Reference to ActionWarehouse where we store object build by actions.
Definition: Action.h:122
ActionFactory & _action_factory
Builds Actions.
Definition: Action.h:104
InputParameters validParams< OutputInterface >()
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:74
subdomain_id_type SubdomainID
Definition: MooseTypes.h:77
FEType _fe_type
FEType for the variable being created.
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
A class to provide an common interface to objects requiring "outputs" option.
void addVariable(std::string &var_name)
Adds a nonlinear variable to the system.
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
Base class for actions.
Definition: Action.h:39
static MooseEnum getNonlinearVariableFamilies()
Get the possible variable families.
InputParameters & getObjectParams()
Retreive the parameters of the object to be created by this action.
static MooseEnum getNonlinearVariableOrders()
Get the possible variable orders.
static const Real _abs_zero_tol
Absolute zero tolerance.
InputParameters validParams< AddVariableAction >()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
virtual void markEigenVariable(const VariableName &var_name)
Mark a variable as a variable of the eigen system.
bool isParamValid(const std::string &name) const
Definition: Action.h:80
const std::string & name() const
The name of the action.
Definition: Action.h:51
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:26
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:131
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void createInitialConditionAction()
Create the action to generate the InitialCondition object.
InputParameters validParams< Action >()
Definition: Action.C:23