https://mooseframework.inl.gov
Loading...
Searching...
No Matches
GrainGrowthAction.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 "GrainGrowthAction.h"
11
12// MOOSE includes
13#include "AddVariableAction.h"
14#include "Conversion.h"
15#include "FEProblem.h"
16#include "Factory.h"
17#include "MooseObjectAction.h"
18#include "MooseMesh.h"
19#include "NonlinearSystemBase.h"
20
21#include "libmesh/string_to_enum.h"
22
23using namespace libMesh;
24
25registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_aux_variable");
26registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_aux_kernel");
27registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_variable");
28registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_kernel");
29registerMooseAction("PhaseFieldApp", GrainGrowthAction, "copy_nodal_vars");
30registerMooseAction("PhaseFieldApp", GrainGrowthAction, "check_copy_nodal_vars");
31
34{
37 "Set up the variable and the kernels needed for a grain growth simulation");
38 params.addRequiredParam<unsigned int>("op_num",
39 "specifies the number of order parameters to create");
40 params.addRequiredParam<std::string>("var_name_base", "specifies the base name of the variables");
41 params.addParam<Real>(
42 "scaling", 1.0, "Specifies a scaling factor to apply to the order parameters");
43 params.addParam<bool>(
44 "initial_from_file",
45 false,
46 "Take the initial condition of all polycrystal variables from the mesh file");
47
48 // Get MooseEnums for the possible order/family options for this variable
51 params.addParam<MooseEnum>("family",
52 families,
53 "Specifies the family of FE "
54 "shape function to use for the order parameters");
55 params.addParam<MooseEnum>("order",
56 orders,
57 "Specifies the order of the FE "
58 "shape function to use for the order parameters");
59
60 params.addParam<MaterialPropertyName>(
61 "mobility", "L", "The isotropic mobility used with the kernels");
62 params.addParam<MaterialPropertyName>("kappa", "kappa_op", "The kappa used with the kernels");
63
64 params.addParam<VariableName>("c", "Name of coupled concentration variable");
65
66 params.addParam<Real>("en_ratio", 1.0, "Ratio of surface to GB energy");
67 params.addParam<unsigned int>("ndef", 0, "Specifies the number of deformed grains to create");
68 params.addParam<bool>("variable_mobility",
69 true,
70 "The mobility is a function of any MOOSE variable (if "
71 "this is set to false, L must be constant over the "
72 "entire domain!)");
73 params.addCoupledVar("coupled_variables",
74 "Vector of nonlinear variable arguments that L depends on");
75
76 params.addParam<bool>("implicit", true, "Whether kernels are implicit or not");
77 params.addParam<bool>(
78 "use_displaced_mesh", false, "Whether to use displaced mesh in the kernels");
79 params.addParam<bool>("use_automatic_differentiation",
80 false,
81 "Flag to use automatic differentiation (AD) objects when possible");
82 params.addParam<std::vector<SubdomainName>>(
83 "block", {}, "Block restriction for the variables and kernels");
84
85 params.addParamNamesToGroup("scaling implicit use_displaced_mesh", "Advanced");
86 params.addParamNamesToGroup("c en_ratio ndef", "Multiphysics");
87
88 return params;
89}
90
92 : Action(params),
93 _op_num(getParam<unsigned int>("op_num")),
94 _var_name_base(getParam<std::string>("var_name_base")),
95 _fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
96 Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family"))),
97 _initial_from_file(getParam<bool>("initial_from_file")),
98 _use_ad(getParam<bool>("use_automatic_differentiation"))
99{
100}
101
102void
104{
105 // Add Variables
106 addVariables();
107
108 // Add Kernels
109 for (unsigned int op = 0; op < _op_num; op++)
110 {
111 // Create variable name
112 std::string var_name = _var_name_base + Moose::stringify(op);
113
114 // Add the kernels for each grain growth variable
115 if (_current_task == "add_kernel")
116 {
117 //
118 // Add time derivative kernel
119 //
120
121 {
122 std::string kernel_type = _use_ad ? "ADTimeDerivative" : "TimeDerivative";
123
124 std::string kernel_name = var_name + "_" + kernel_type;
125 InputParameters params = _factory.getValidParams(kernel_type);
126 params.set<NonlinearVariableName>("variable") = var_name;
127 params.applyParameters(parameters());
128
129 _problem->addKernel(kernel_type, kernel_name, params);
130 }
131
132 //
133 // Add ACGrGrPoly kernel
134 //
135
136 {
137 std::string kernel_type = _use_ad ? "ADGrainGrowth" : "ACGrGrPoly";
138
139 // Make vector of order parameter names, excluding this one
140 std::vector<VariableName> v;
141 v.resize(_op_num - 1);
142
143 unsigned int ind = 0;
144 for (unsigned int j = 0; j < _op_num; ++j)
145 if (j != op)
146 v[ind++] = _var_name_base + Moose::stringify(j);
147
148 std::string kernel_name = var_name + "_" + kernel_type;
149 InputParameters params = _factory.getValidParams(kernel_type);
150 params.set<NonlinearVariableName>("variable") = var_name;
151 params.set<std::vector<VariableName>>("v") = v;
152 params.set<MaterialPropertyName>("mob_name") = getParam<MaterialPropertyName>("mobility");
153 params.applyParameters(parameters());
154
155 _problem->addKernel(kernel_type, kernel_name, params);
156 }
157
158 //
159 // Add ACInterface kernel
160 //
161
162 {
163 std::string kernel_type = _use_ad ? "ADACInterface" : "ACInterface";
164
165 std::string kernel_name = var_name + "_" + kernel_type;
166 InputParameters params = _factory.getValidParams(kernel_type);
167 params.set<NonlinearVariableName>("variable") = var_name;
168 params.set<MaterialPropertyName>("mob_name") = getParam<MaterialPropertyName>("mobility");
169 params.set<MaterialPropertyName>("kappa_name") = getParam<MaterialPropertyName>("kappa");
170 params.set<bool>("variable_L") = getParam<bool>("variable_mobility");
171 params.applyParameters(parameters());
172
173 _problem->addKernel(kernel_type, kernel_name, params);
174 }
175
176 //
177 // Set up optional ACGBPoly bubble interaction kernels
178 //
179
180 if (isParamValid("c"))
181 {
182 if (_use_ad)
183 mooseError("AD version of ACGBPoly is not implemented");
184
185 std::string kernel_type = "ACGBPoly";
186
187 std::string kernel_name = var_name + "_" + kernel_type;
188 InputParameters params = _factory.getValidParams(kernel_type);
189 params.set<NonlinearVariableName>("variable") = var_name;
190 params.set<std::vector<VariableName>>("c") = {getParam<VariableName>("c")};
191 params.applyParameters(parameters());
192
193 _problem->addKernel(kernel_type, kernel_name, params);
194 }
195 }
196 }
197
198 // Add AuxVriable and AuxKernel for Bnds variable
200}
201
202void
204{
205 for (unsigned int op = 0; op < _op_num; op++)
206 {
207 // Create variable name
208 std::string var_name = _var_name_base + Moose::stringify(op);
209
210 // Setup initial from file if requested
212 {
213 if (_current_task == "check_copy_nodal_vars")
215
216 if (_current_task == "copy_nodal_vars")
217 {
218 auto * system = &_problem->getNonlinearSystemBase(/*nl_sys_num=*/0);
219 system->addVariableToCopy(var_name, var_name, "LATEST");
220 }
221 }
222
223 // Add each grain growth variable
224 if (_current_task == "add_variable")
225 {
227 auto var_params = _factory.getValidParams(type);
228
229 var_params.applySpecificParameters(_pars, {"family", "order", "block"});
230 var_params.set<std::vector<Real>>("scaling") = {getParam<Real>("scaling")};
231
232 // Add variable
233 _problem->addVariable(type, var_name, var_params);
234 }
235 }
236}
237
238void
239GrainGrowthAction::addBnds(const std::string & name_base)
240{
241 // Create auxvariable
242 if (_current_task == "add_aux_variable")
243 {
244 auto var_params = _factory.getValidParams("MooseVariable");
245 var_params.set<MooseEnum>("family") = "LAGRANGE";
246 var_params.set<MooseEnum>("order") = "FIRST";
247 var_params.applySpecificParameters(_pars, {"block"});
248 _problem->addAuxVariable("MooseVariable", "bnds", var_params);
249 }
250 // Create auxkernel
251 else if (_current_task == "add_aux_kernel")
252 {
253 // Make vector of order parameter names, excluding this one std::vector<VariableName> v;
254 std::vector<VariableName> v;
255 v.resize(_op_num);
256
257 for (unsigned int j = 0; j < _op_num; ++j)
258 v[j] = name_base + Moose::stringify(j);
259
260 std::string aux_kernel_type = "BndsCalcAux";
261
262 std::string aux_kernel_name = "bnds_" + aux_kernel_type;
263 InputParameters params = _factory.getValidParams(aux_kernel_type);
264 params.set<AuxVariableName>("variable") = "bnds";
265 params.set<std::vector<VariableName>>("v") = v;
266 params.set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
267 params.applyParameters(parameters());
268
269 _problem->addAuxKernel(aux_kernel_type, aux_kernel_name, params);
270 }
271}
const double v
registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_aux_variable")
const ExecFlagType EXEC_TIMESTEP_END
const ExecFlagType EXEC_INITIAL
void ErrorVector unsigned int
static InputParameters validParams()
MooseApp & _app
std::shared_ptr< FEProblemBase > & _problem
const std::string & _current_task
static MooseEnum getNonlinearVariableFamilies()
static MooseEnum getNonlinearVariableOrders()
static std::string variableType(const libMesh::FEType &fe_type, const bool is_fv=false, const bool is_array=false)
const bool _initial_from_file
Take initial values from file?
const bool _use_ad
use AD objects where possible
GrainGrowthAction(const InputParameters &params)
const std::string _var_name_base
static InputParameters validParams()
const unsigned int _op_num
number of variables and variable name base for variable creation
void addBnds(const std::string &name_base)
const libMesh::FEType _fe_type
FEType for the variable being created.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
T & set(const std::string &name, bool quiet_mode=false)
void addCoupledVar(const std::string &name, const std::string &doc_string)
void applyParameters(const InputParameters &common, const std::vector< std::string > &exclude={}, const bool allow_private=false)
void setExodusFileRestart(bool flag)
const InputParameters & parameters() const
const std::string & type() const
void mooseError(Args &&... args) const
const InputParameters & _pars
bool isParamValid(const std::string &name) const
Factory & _factory
std::string stringify(const T &t)
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real