www.mooseframework.org
AddVariableAction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // Standard includes
11 #include <sstream>
12 #include <stdexcept>
13 
14 // MOOSE includes
15 #include "AddVariableAction.h"
16 #include "FEProblem.h"
17 #include "Factory.h"
18 #include "MooseEnum.h"
19 #include "MooseEigenSystem.h"
20 #include "MooseObjectAction.h"
21 #include "MooseMesh.h"
22 
23 #include "libmesh/libmesh.h"
24 #include "libmesh/exodusII_io.h"
25 #include "libmesh/equation_systems.h"
26 #include "libmesh/nonlinear_implicit_system.h"
27 #include "libmesh/explicit_system.h"
28 #include "libmesh/string_to_enum.h"
29 
30 // class static initialization
31 const Real AddVariableAction::_abs_zero_tol = 1e-12;
32 
33 registerMooseAction("MooseApp", AddVariableAction, "add_variable");
34 
35 template <>
38 {
39  // Get MooseEnums for the possible order/family options for this variable
42 
43  // Define the general input options
45  params += validParams<OutputInterface>();
46  params.addParam<MooseEnum>(
47  "family", families, "Specifies the family of FE shape functions to use for this variable");
48  params.addParam<MooseEnum>("order",
49  orders,
50  "Specifies the order of the FE shape function to use "
51  "for this variable (additional orders not listed are "
52  "allowed)");
53  params.addParam<Real>("initial_condition", "Specifies the initial condition for this variable");
54  params.addParam<std::vector<SubdomainName>>("block", "The block id where this variable lives");
55  params.addParam<bool>("eigen", false, "True to make this variable an eigen variable");
56 
57  // Advanced input options
58  params.addParam<Real>("scaling", 1.0, "Specifies a scaling factor to apply to this variable");
59  params.addParamNamesToGroup("scaling eigen", "Advanced");
60 
61  return params;
62 }
63 
65  : Action(params),
66  OutputInterface(params, false),
67  _fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
68  Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family"))),
69  _scalar_var(_fe_type.family == SCALAR)
70 {
71 }
72 
75 {
76  return MooseEnum("LAGRANGE MONOMIAL HERMITE SCALAR HIERARCHIC CLOUGH XYZ SZABAB BERNSTEIN "
77  "L2_LAGRANGE L2_HIERARCHIC NEDELEC_ONE LAGRANGE_VEC",
78  "LAGRANGE");
79 }
80 
83 {
84  return MooseEnum("CONSTANT FIRST SECOND THIRD FOURTH", "FIRST", true);
85 }
86 
87 void
89 {
90  // Get necessary data for creating a variable
91  std::string var_name = name();
92  addVariable(var_name);
93 
94  // Set the initial condition
95  if (isParamValid("initial_condition"))
97 }
98 
99 void
101 {
102  // Variable name
103  std::string var_name = name();
104 
105  // Create the object name
106  std::string long_name("");
107  long_name += var_name;
108  long_name += "_moose";
109 
110  // Set the parameters for the action
111  InputParameters action_params = _action_factory.getValidParams("AddOutputAction");
112  action_params.set<ActionWarehouse *>("awh") = &_awh;
113 
114  if (_scalar_var)
115  action_params.set<std::string>("type") = "ScalarConstantIC";
116  else
117  action_params.set<std::string>("type") = "ConstantIC";
118 
119  // Create the action
120  std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
121  _action_factory.create("AddInitialConditionAction", long_name, action_params));
122 
123  // Set the required parameters for the object to be created
124  action->getObjectParams().set<VariableName>("variable") = var_name;
125  action->getObjectParams().set<Real>("value") = getParam<Real>("initial_condition");
126 
127  // Store the action in the ActionWarehouse
128  _awh.addActionBlock(action);
129 }
130 
131 void
132 AddVariableAction::addVariable(const std::string & var_name)
133 {
134  std::set<SubdomainID> blocks = getSubdomainIDs();
135  Real scale_factor = isParamValid("scaling") ? getParam<Real>("scaling") : 1;
136 
137  // Scalar variable
138  if (_scalar_var)
139  _problem->addScalarVariable(var_name, _fe_type.order, scale_factor);
140 
141  // Block restricted variable
142  else if (blocks.empty())
143  _problem->addVariable(var_name, _fe_type, scale_factor);
144 
145  // Non-block restricted variable
146  else
147  _problem->addVariable(var_name, _fe_type, scale_factor, &blocks);
148 
149  if (getParam<bool>("eigen"))
150  {
151  MooseEigenSystem & esys(static_cast<MooseEigenSystem &>(_problem->getNonlinearSystemBase()));
152  esys.markEigenVariable(var_name);
153  }
154 }
155 
156 std::set<SubdomainID>
158 {
159  // Extract and return the block ids supplied in the input
160  std::set<SubdomainID> blocks;
161  std::vector<SubdomainName> block_param = getParam<std::vector<SubdomainName>>("block");
162  for (const auto & subdomain_name : block_param)
163  {
164  SubdomainID blk_id = _problem->mesh().getSubdomainID(subdomain_name);
165  blocks.insert(blk_id);
166  }
167  return blocks;
168 }
bool isParamValid(const std::string &name) const
Definition: Action.h:115
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
Method to add objects to the simulation or perform other setup tasks.
ActionWarehouse & _awh
Reference to ActionWarehouse where we store object build by actions.
Definition: Action.h:207
ActionFactory & _action_factory
Builds Actions.
Definition: Action.h:189
const std::string & name() const
The name of the action.
Definition: Action.h:76
Adds nonlinear variable.
InputParameters validParams< OutputInterface >()
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:70
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.
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.
registerMooseAction("MooseApp", AddVariableAction, "add_variable")
Base class for actions.
Definition: Action.h:35
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:31
virtual void markEigenVariable(const VariableName &var_name)
Mark a variable as a variable of the eigen system.
subdomain_id_type SubdomainID
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:41
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:216
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 addVariable(const std::string &var_name)
Adds a nonlinear variable to the system.
void createInitialConditionAction()
Create the action to generate the InitialCondition object.
InputParameters validParams< Action >()
Definition: Action.C:22