www.mooseframework.org
MaterialDerivativeTestAction.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 
11 
12 #include "Conversion.h"
13 #include "MooseEnum.h"
14 #include "FEProblemBase.h"
15 #include "MoosePreconditioner.h"
16 #include "NonlinearSystemBase.h"
17 
18 #include "libmesh/fe.h"
19 #include "libmesh/string_to_enum.h"
20 
21 registerMooseAction("MooseApp", MaterialDerivativeTestAction, "add_variable");
22 
23 registerMooseAction("MooseApp", MaterialDerivativeTestAction, "add_kernel");
24 
25 registerMooseAction("MooseApp", MaterialDerivativeTestAction, "add_preconditioning");
26 
27 template <>
30 {
32  params.addParam<std::vector<VariableName>>("args",
33  "Variables the tested material property depends on.");
34  params.addRequiredParam<MaterialPropertyName>(
35  "prop_name", "Name of the material property to test the derivatives of.");
36  MooseEnum prop_type_enum("Real RankTwoTensor RankFourTensor");
37  params.addParam<MooseEnum>(
38  "prop_type", prop_type_enum, "Type of the material property to test the derivatives of.");
39  params.addParam<unsigned int>(
40  "derivative_order", 0, "Highest order derivative to test derivatives of.");
41  return params;
42 }
43 
45  : Action(parameters),
46  _args(getParam<std::vector<VariableName>>("args")),
47  _prop_name(getParam<MaterialPropertyName>("prop_name")),
48  _prop_type(getParam<MooseEnum>("prop_type").getEnum<PropTypeEnum>()),
49  _derivative_order(getParam<unsigned int>("derivative_order")),
50  _second(false),
51  _derivatives({{_prop_name, {}}})
52 {
53  std::vector<std::vector<std::vector<VariableName>>> derivative_table(_derivative_order + 1);
54 
55  // 0th derivative is a (single) derivative w.r.t. to _no_ variables
56  derivative_table[0] = {{}};
57 
58  // build higher order derivatives
59  for (unsigned int n = 1; n <= _derivative_order; ++n)
60  for (const auto & function : derivative_table[n - 1])
61  for (const auto & var : _args)
62  {
63  // take previous order derivative and derive w.r.t. one of the args
64  auto derivative = std::vector<VariableName>(function);
65  derivative.push_back(var);
66 
67  // add derivative to list
68  derivative_table[n].push_back(derivative);
69  _derivatives.insert(
70  std::make_pair(derivativePropertyName(_prop_name, derivative), derivative));
71  }
72 }
73 
74 void
76 {
77  // finite element type
78  auto fetype = FEType(Utility::string_to_enum<Order>(_second ? "SECOND" : "FIRST"),
79  Utility::string_to_enum<FEFamily>("LAGRANGE"));
80 
81  // build higher order derivatives
82  for (const auto & derivative : _derivatives)
83  {
84  // Create variables
85  if (_current_task == "add_variable")
86  {
87  switch (_prop_type)
88  {
89  case PropTypeEnum::REAL:
90  _problem->addVariable("var_" + derivative.first, fetype, 1.0, nullptr);
91  break;
92 
94  for (unsigned int i = 0; i < 3; ++i)
95  for (unsigned int j = 0; j < 3; ++j)
96  _problem->addVariable("var_" + derivative.first + "_" + Moose::stringify(i) + "_" +
98  fetype,
99  1.0,
100  nullptr);
101  break;
102 
104  for (unsigned int i = 0; i < 3; ++i)
105  for (unsigned int j = 0; j < 3; ++j)
106  for (unsigned int k = 0; k < 3; ++k)
107  for (unsigned int l = 0; l < 3; ++l)
108  _problem->addVariable("var_" + derivative.first + "_" + Moose::stringify(i) +
109  "_" + Moose::stringify(j) + "_" + Moose::stringify(k) +
110  "_" + Moose::stringify(l),
111  fetype,
112  1.0,
113  nullptr);
114  break;
115 
116  default:
117  mooseError("Unknown property type.");
118  }
119  }
120 
121  if (_current_task == "add_kernel")
122  {
123  switch (_prop_type)
124  {
125  case PropTypeEnum::REAL:
126  {
127  auto params = _factory.getValidParams("MaterialDerivativeTestKernel");
128  params.set<std::vector<VariableName>>("args") = _args;
129  params.set<std::vector<VariableName>>("derivative") = derivative.second;
130  params.set<MaterialPropertyName>("material_property") = _prop_name;
131  params.set<NonlinearVariableName>("variable") = "var_" + derivative.first;
132  _problem->addKernel("MaterialDerivativeTestKernel", "kernel_" + derivative.first, params);
133  break;
134  }
135 
137  {
138  auto params = _factory.getValidParams("MaterialDerivativeRankTwoTestKernel");
139  params.set<std::vector<VariableName>>("args") = _args;
140  params.set<std::vector<VariableName>>("derivative") = derivative.second;
141  params.set<MaterialPropertyName>("material_property") = _prop_name;
142  for (unsigned int i = 0; i < 3; ++i)
143  for (unsigned int j = 0; j < 3; ++j)
144  {
145  auto suffix =
146  derivative.first + "_" + Moose::stringify(i) + "_" + Moose::stringify(j);
147  params.set<NonlinearVariableName>("variable") = "var_" + suffix;
148  params.set<unsigned int>("i") = i;
149  params.set<unsigned int>("j") = j;
150  _problem->addKernel(
151  "MaterialDerivativeRankTwoTestKernel", "kernel_" + suffix, params);
152  }
153  break;
154  }
155 
157  {
158  auto params = _factory.getValidParams("MaterialDerivativeRankFourTestKernel");
159  params.set<std::vector<VariableName>>("args") = _args;
160  params.set<std::vector<VariableName>>("derivative") = derivative.second;
161  params.set<MaterialPropertyName>("material_property") = _prop_name;
162  for (unsigned int i = 0; i < 3; ++i)
163  for (unsigned int j = 0; j < 3; ++j)
164  for (unsigned int k = 0; k < 3; ++k)
165  for (unsigned int l = 0; l < 3; ++l)
166  {
167  auto suffix = derivative.first + "_" + Moose::stringify(i) + "_" +
168  Moose::stringify(j) + "_" + Moose::stringify(k) + "_" +
169  Moose::stringify(l);
170  params.set<NonlinearVariableName>("variable") = "var_" + suffix;
171  params.set<unsigned int>("i") = i;
172  params.set<unsigned int>("j") = j;
173  params.set<unsigned int>("k") = k;
174  params.set<unsigned int>("l") = l;
175  _problem->addKernel(
176  "MaterialDerivativeRankFourTestKernel", "kernel_" + suffix, params);
177  }
178  break;
179  }
180 
181  default:
182  mooseError("Unknown property type.");
183  }
184  }
185  }
186 
187  if (_current_task == "add_preconditioning")
188  {
189  auto params = _factory.getValidParams("SMP");
190  auto & row = params.set<std::vector<NonlinearVariableName>>("off_diag_row");
191  auto & col = params.set<std::vector<NonlinearVariableName>>("off_diag_column");
192 
193  for (const auto & derivative : _derivatives)
194  {
195  switch (_prop_type)
196  {
197  case PropTypeEnum::REAL:
198  for (auto & arg : _args)
199  {
200  row.push_back("var_" + derivative.first);
201  col.push_back(arg);
202  }
203  break;
204 
206  for (unsigned int i = 0; i < 3; ++i)
207  for (unsigned int j = 0; j < 3; ++j)
208  for (auto & arg : _args)
209  {
210  row.push_back("var_" + derivative.first + "_" + Moose::stringify(i) + "_" +
211  Moose::stringify(j));
212  col.push_back(arg);
213  }
214  break;
215 
217  for (unsigned int i = 0; i < 3; ++i)
218  for (unsigned int j = 0; j < 3; ++j)
219  for (unsigned int k = 0; k < 3; ++k)
220  for (unsigned int l = 0; l < 3; ++l)
221  for (auto & arg : _args)
222  {
223  row.push_back("var_" + derivative.first + "_" + Moose::stringify(i) + "_" +
224  Moose::stringify(j) + "_" + Moose::stringify(k) + "_" +
225  Moose::stringify(l));
226  col.push_back(arg);
227  }
228  break;
229 
230  default:
231  mooseError("Unknown property type.");
232  }
233  }
234 
235  if (_problem.get() != nullptr)
236  {
237  std::shared_ptr<MoosePreconditioner> pc =
238  _factory.create<MoosePreconditioner>("SMP", "material_derivative_SMP", params);
239 
240  _problem->getNonlinearSystemBase().setPreconditioner(pc);
241  }
242  else
243  mooseError("_problem.get() returned nullptr");
244  }
245 }
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, InputParameters parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:87
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
Sets up variables and Kernels to test the derivatives of material properties via the Jacobian checker...
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:67
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
registerMooseAction("MooseApp", MaterialDerivativeTestAction, "add_variable")
Factory & _factory
The Factory associated with the MooseApp.
Definition: Action.h:186
static PetscErrorCode Vec Mat Mat pc
Base class for actions.
Definition: Action.h:35
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
Base class for MOOSE preconditioners.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
const std::string & _current_task
The current action (even though we have seperate instances for each action)
Definition: Action.h:210
InputParameters validParams< MaterialDerivativeTestAction >()
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
std::map< MaterialPropertyName, std::vector< VariableName > > _derivatives
every derivative given by a list of variables to derive w.r.t
MaterialDerivativeTestAction(const InputParameters &parameters)
PetscInt n
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...
enum MaterialDerivativeTestAction::PropTypeEnum _prop_type
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
InputParameters validParams< Action >()
Definition: Action.C:22