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