www.mooseframework.org
MaterialDerivativeTestAction.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
16 
17 #include "Conversion.h"
18 #include "MooseEnum.h"
19 #include "FEProblemBase.h"
20 #include "MoosePreconditioner.h"
21 #include "NonlinearSystemBase.h"
22 
23 #include "libmesh/fe.h"
24 #include "libmesh/string_to_enum.h"
25 
26 template <>
29 {
31  params.addParam<std::vector<VariableName>>("args",
32  "Variables the tested material property depends on.");
33  params.addRequiredParam<MaterialPropertyName>(
34  "prop_name", "Name of the material property to test the derivatives of.");
35  MooseEnum prop_type_enum("Real RankTwoTensor RankFourTensor");
36  params.addParam<MooseEnum>(
37  "prop_type", prop_type_enum, "Type of the material property to test the derivatives of.");
38  params.addParam<unsigned int>(
39  "derivative_order", 0, "Highest order derivative to test derivatives of.");
40  return params;
41 }
42 
44  : Action(parameters),
45  _args(getParam<std::vector<VariableName>>("args")),
46  _prop_name(getParam<MaterialPropertyName>("prop_name")),
47  _prop_type(getParam<MooseEnum>("prop_type").getEnum<PropTypeEnum>()),
48  _derivative_order(getParam<unsigned int>("derivative_order")),
49  _second(false),
50  _derivatives({{_prop_name, {}}})
51 {
52  std::vector<std::vector<std::vector<VariableName>>> derivative_table(_derivative_order + 1);
53 
54  // 0th derivative is a (single) derivative w.r.t. to _no_ variables
55  derivative_table[0] = {{}};
56 
57  // build higher order derivatives
58  for (unsigned int n = 1; n <= _derivative_order; ++n)
59  for (const auto & function : derivative_table[n - 1])
60  for (const auto & var : _args)
61  {
62  // take previous order derivative and derive w.r.t. one of the args
63  auto derivative = std::vector<VariableName>(function);
64  derivative.push_back(var);
65 
66  // add derivative to list
67  derivative_table[n].push_back(derivative);
68  _derivatives.insert(std::make_pair(propertyName(_prop_name, derivative), derivative));
69  }
70 }
71 
72 void
74 {
75  // finite element type
76  auto fetype = FEType(Utility::string_to_enum<Order>(_second ? "SECOND" : "FIRST"),
77  Utility::string_to_enum<FEFamily>("LAGRANGE"));
78 
79  // build higher order derivatives
80  for (const auto & derivative : _derivatives)
81  {
82  // Create variables
83  if (_current_task == "add_variable")
84  {
85  switch (_prop_type)
86  {
87  case PropTypeEnum::REAL:
88  _problem->addVariable("var_" + derivative.first, fetype, 1.0, nullptr);
89  break;
90 
92  for (unsigned int i = 0; i < 3; ++i)
93  for (unsigned int j = 0; j < 3; ++j)
94  _problem->addVariable("var_" + derivative.first + "_" + Moose::stringify(i) + "_" +
96  fetype,
97  1.0,
98  nullptr);
99  break;
100 
102  for (unsigned int i = 0; i < 3; ++i)
103  for (unsigned int j = 0; j < 3; ++j)
104  for (unsigned int k = 0; k < 3; ++k)
105  for (unsigned int l = 0; l < 3; ++l)
106  _problem->addVariable("var_" + derivative.first + "_" + Moose::stringify(i) +
107  "_" + Moose::stringify(j) + "_" + Moose::stringify(k) +
108  "_" + Moose::stringify(l),
109  fetype,
110  1.0,
111  nullptr);
112  break;
113 
114  default:
115  mooseError("Unknown property type.");
116  }
117  }
118 
119  if (_current_task == "add_kernel")
120  {
121  switch (_prop_type)
122  {
123  case PropTypeEnum::REAL:
124  {
125  auto params = _factory.getValidParams("MaterialDerivativeTestKernel");
126  params.set<std::vector<VariableName>>("args") = _args;
127  params.set<std::vector<VariableName>>("derivative") = derivative.second;
128  params.set<MaterialPropertyName>("material_property") = _prop_name;
129  params.set<NonlinearVariableName>("variable") = "var_" + derivative.first;
130  _problem->addKernel("MaterialDerivativeTestKernel", "kernel_" + derivative.first, params);
131  break;
132  }
133 
135  {
136  auto params = _factory.getValidParams("MaterialDerivativeRankTwoTestKernel");
137  params.set<std::vector<VariableName>>("args") = _args;
138  params.set<std::vector<VariableName>>("derivative") = derivative.second;
139  params.set<MaterialPropertyName>("material_property") = _prop_name;
140  for (unsigned int i = 0; i < 3; ++i)
141  for (unsigned int j = 0; j < 3; ++j)
142  {
143  auto suffix =
144  derivative.first + "_" + Moose::stringify(i) + "_" + Moose::stringify(j);
145  params.set<NonlinearVariableName>("variable") = "var_" + suffix;
146  params.set<unsigned int>("i") = i;
147  params.set<unsigned int>("j") = j;
148  _problem->addKernel(
149  "MaterialDerivativeRankTwoTestKernel", "kernel_" + suffix, params);
150  }
151  break;
152  }
153 
155  {
156  auto params = _factory.getValidParams("MaterialDerivativeRankFourTestKernel");
157  params.set<std::vector<VariableName>>("args") = _args;
158  params.set<std::vector<VariableName>>("derivative") = derivative.second;
159  params.set<MaterialPropertyName>("material_property") = _prop_name;
160  for (unsigned int i = 0; i < 3; ++i)
161  for (unsigned int j = 0; j < 3; ++j)
162  for (unsigned int k = 0; k < 3; ++k)
163  for (unsigned int l = 0; l < 3; ++l)
164  {
165  auto suffix = derivative.first + "_" + Moose::stringify(i) + "_" +
166  Moose::stringify(j) + "_" + Moose::stringify(k) + "_" +
167  Moose::stringify(l);
168  params.set<NonlinearVariableName>("variable") = "var_" + suffix;
169  params.set<unsigned int>("i") = i;
170  params.set<unsigned int>("j") = j;
171  params.set<unsigned int>("k") = k;
172  params.set<unsigned int>("l") = l;
173  _problem->addKernel(
174  "MaterialDerivativeRankFourTestKernel", "kernel_" + suffix, params);
175  }
176  break;
177  }
178 
179  default:
180  mooseError("Unknown property type.");
181  }
182  }
183  }
184 
185  if (_current_task == "add_preconditioning")
186  {
187  auto params = _factory.getValidParams("SMP");
188  auto & row = params.set<std::vector<NonlinearVariableName>>("off_diag_row");
189  auto & col = params.set<std::vector<NonlinearVariableName>>("off_diag_column");
190 
191  for (const auto & derivative : _derivatives)
192  {
193  switch (_prop_type)
194  {
195  case PropTypeEnum::REAL:
196  for (auto & arg : _args)
197  {
198  row.push_back("var_" + derivative.first);
199  col.push_back(arg);
200  }
201  break;
202 
204  for (unsigned int i = 0; i < 3; ++i)
205  for (unsigned int j = 0; j < 3; ++j)
206  for (auto & arg : _args)
207  {
208  row.push_back("var_" + derivative.first + "_" + Moose::stringify(i) + "_" +
209  Moose::stringify(j));
210  col.push_back(arg);
211  }
212  break;
213 
215  for (unsigned int i = 0; i < 3; ++i)
216  for (unsigned int j = 0; j < 3; ++j)
217  for (unsigned int k = 0; k < 3; ++k)
218  for (unsigned int l = 0; l < 3; ++l)
219  for (auto & arg : _args)
220  {
221  row.push_back("var_" + derivative.first + "_" + Moose::stringify(i) + "_" +
222  Moose::stringify(j) + "_" + Moose::stringify(k) + "_" +
223  Moose::stringify(l));
224  col.push_back(arg);
225  }
226  break;
227 
228  default:
229  mooseError("Unknown property type.");
230  }
231  }
232 
233  if (_problem.get() != nullptr)
234  {
235  params.set<FEProblemBase *>("_fe_problem_base") = _problem.get();
236  std::shared_ptr<MoosePreconditioner> pc =
237  _factory.create<MoosePreconditioner>("SMP", "material_derivative_SMP", params);
238 
239  _problem->getNonlinearSystemBase().setPreconditioner(pc);
240  }
241  else
242  mooseError("_problem.get() returned nullptr");
243  }
244 }
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:46
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:26
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
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...
Factory & _factory
The Factory associated with the MooseApp.
Definition: Action.h:101
static PetscErrorCode Vec Mat Mat pc
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Base class for actions.
Definition: Action.h:39
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:37
const std::string & _current_task
The current action (even though we have seperate instances for each action)
Definition: Action.h:125
InputParameters validParams< MaterialDerivativeTestAction >()
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:66
std::map< MaterialPropertyName, std::vector< VariableName > > _derivatives
every derivative given by a list of variables to derive w.r.t
MaterialDerivativeTestAction(const InputParameters &parameters)
const MaterialPropertyName propertyName(const MaterialPropertyName &base, const std::vector< VariableName > &c) const
Helper functions to generate the material property names for the arbitrary derivatives.
PetscInt n
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:131
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
InputParameters validParams< Action >()
Definition: Action.C:23