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