www.mooseframework.org
MaterialOutputAction.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 // MOOSE includes
11 #include "MaterialOutputAction.h"
12 #include "FEProblem.h"
13 #include "MooseApp.h"
14 #include "AddOutputAction.h"
15 #include "Material.h"
16 #include "RankTwoTensor.h"
17 #include "RankFourTensor.h"
18 
19 #include "libmesh/utility.h"
20 
21 // Declare the output helper specializations
22 template <>
23 std::vector<std::string> MaterialOutputAction::materialOutputHelper<Real>(
24  const std::string & material_name, const Material & material, bool get_names_only);
25 
26 template <>
27 std::vector<std::string> MaterialOutputAction::materialOutputHelper<RealVectorValue>(
28  const std::string & material_name, const Material & material, bool get_names_only);
29 
30 template <>
31 std::vector<std::string> MaterialOutputAction::materialOutputHelper<RealTensorValue>(
32  const std::string & material_name, const Material & material, bool get_names_only);
33 
34 template <>
35 std::vector<std::string> MaterialOutputAction::materialOutputHelper<RankTwoTensor>(
36  const std::string & material_name, const Material & material, bool get_names_only);
37 
38 template <>
39 std::vector<std::string> MaterialOutputAction::materialOutputHelper<RankFourTensor>(
40  const std::string & material_name, const Material & material, bool get_names_only);
41 
42 registerMooseAction("MooseApp", MaterialOutputAction, "add_output_aux_variables");
43 
44 registerMooseAction("MooseApp", MaterialOutputAction, "add_aux_kernel");
45 
46 template <>
49 {
51  return params;
52 }
53 
55  : Action(params), _output_warehouse(_app.getOutputWarehouse())
56 {
57 }
58 
59 void
61 {
62  mooseAssert(_problem,
63  "FEProblemBase pointer is nullptr, it is needed for auto material property output");
64 
65  // Do nothing if the application does not have output
66  if (!_app.actionWarehouse().hasActions("add_output"))
67  return;
68 
69  bool get_names_only = _current_task == "add_output_aux_variables" ? true : false;
70 
71  // Set the pointers to the MaterialData objects (Note, these pointers are not available at
72  // construction)
75 
76  // A complete list of all Material objects
77  const auto & material_ptrs = _problem->getMaterialWarehouse().getObjects();
78 
79  // Handle setting of material property output in [Outputs] sub-blocks
80  // Output objects can enable material property output, the following code examines the parameters
81  // for each Output object and sets a flag if any Output object has output set and also builds a
82  // list if the
83  // properties are limited via the 'show_material_properties' parameters
84  bool outputs_has_properties = false;
85  std::set<std::string> output_object_properties;
86 
87  const auto & output_actions = _app.actionWarehouse().getActionListByName("add_output");
88  for (const auto & act : output_actions)
89  {
90  // Extract the Output action
91  AddOutputAction * action = dynamic_cast<AddOutputAction *>(act);
92  if (!action)
93  continue;
94 
95  // Add the material property names from the output object parameters to the list of properties
96  // to output
97  InputParameters & params = action->getObjectParams();
98  if (params.isParamValid("output_material_properties") &&
99  params.get<bool>("output_material_properties"))
100  {
101  outputs_has_properties = true;
102  std::vector<std::string> prop_names =
103  params.get<std::vector<std::string>>("show_material_properties");
104  output_object_properties.insert(prop_names.begin(), prop_names.end());
105  }
106  }
107 
108  // Loop through each material object
109  std::set<std::string> material_names;
110  for (const auto & mat : material_ptrs)
111  {
112  // Extract the names of the output objects to which the material properties will be exported
113  std::set<OutputName> outputs = mat->getOutputs();
114 
115  // Extract the property names that will actually be output
116  std::vector<std::string> output_properties =
117  mat->getParam<std::vector<std::string>>("output_properties");
118 
119  // Append the properties listed in the Outputs block
120  if (outputs_has_properties)
121  output_properties.insert(output_properties.end(),
122  output_object_properties.begin(),
123  output_object_properties.end());
124 
125  // Clear the list of variable names for the current material object, this list will be populated
126  // with all the
127  // variables names for the current material object and is needed for purposes of controlling the
128  // which output objects
129  // show the material property data
130  _material_variable_names.clear();
131 
132  // Create necessary outputs for the properties if:
133  // (1) The Outputs block has material output enabled
134  // (2) If the Material object itself has set the 'outputs' parameter
135  if (outputs_has_properties || outputs.find("none") == outputs.end())
136  {
137  // Add the material property for output if the name is contained in the 'output_properties'
138  // list
139  // or if the list is empty (all properties)
140  const std::set<std::string> names = mat->getSuppliedItems();
141  std::vector<std::string> curr_material_names;
142  for (const auto & name : names)
143  {
144  // Add the material property for output
145  if (output_properties.empty() ||
146  std::find(output_properties.begin(), output_properties.end(), name) !=
147  output_properties.end())
148  {
149  if (hasProperty<Real>(name))
150  {
151  curr_material_names = materialOutputHelper<Real>(name, *mat, get_names_only);
152  material_names.insert(curr_material_names.begin(), curr_material_names.end());
153  }
154 
155  else if (hasProperty<RealVectorValue>(name))
156  {
157  curr_material_names = materialOutputHelper<RealVectorValue>(name, *mat, get_names_only);
158  material_names.insert(curr_material_names.begin(), curr_material_names.end());
159  }
160 
161  else if (hasProperty<RealTensorValue>(name))
162  {
163  curr_material_names = materialOutputHelper<RealTensorValue>(name, *mat, get_names_only);
164  material_names.insert(curr_material_names.begin(), curr_material_names.end());
165  }
166 
167  else if (hasProperty<RankTwoTensor>(name))
168  {
169  curr_material_names = materialOutputHelper<RankTwoTensor>(name, *mat, get_names_only);
170  material_names.insert(curr_material_names.begin(), curr_material_names.end());
171  }
172 
173  else if (hasProperty<RankFourTensor>(name))
174  {
175  curr_material_names = materialOutputHelper<RankFourTensor>(name, *mat, get_names_only);
176  material_names.insert(curr_material_names.begin(), curr_material_names.end());
177  }
178 
179  else
180  mooseWarning("The type for material property '",
181  name,
182  "' is not supported for automatic output.");
183  }
184 
185  // If the material object as limited outputs, store the variables associated with the output
186  // objects
187  if (!outputs.empty())
188  for (const auto & output_name : outputs)
189  _material_variable_names_map[output_name].insert(_material_variable_names.begin(),
191  }
192  }
193  }
194 
195  if (_current_task == "add_output_aux_variables")
196  {
197 
198  // Create the AuxVariables
199  FEType fe_type(
200  CONSTANT,
201  MONOMIAL); // currently only elemental variables are support for material property output
202  for (const auto & var_name : material_names)
203  _problem->addAuxVariable(var_name, fe_type);
204  }
205  else
206  {
207  // When a Material object has 'output_properties' defined all other properties not listed must
208  // be added to the hide list for the output objects so that properties that are not desired do
209  // not appear.
210  for (const auto & it : _material_variable_names_map)
211  {
212  std::set<std::string> hide;
213  std::set_difference(material_names.begin(),
214  material_names.end(),
215  it.second.begin(),
216  it.second.end(),
217  std::inserter(hide, hide.begin()));
218 
220  }
221  }
222 }
223 
226  const std::string & property_name,
227  const std::string & variable_name,
228  const Material & material)
229 {
230  // Append the list of output variables for the current material
231  _material_variable_names.insert(variable_name);
232 
233  // Set the action parameters
235 
236  params.set<MaterialPropertyName>("property") = property_name;
237  params.set<AuxVariableName>("variable") = variable_name;
238  params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_END;
239 
240  if (material.boundaryRestricted())
241  params.set<std::vector<BoundaryName>>("boundary") = material.boundaryNames();
242  else
243  params.set<std::vector<SubdomainName>>("block") = material.blocks();
244 
245  return params;
246 }
247 
248 template <>
249 std::vector<std::string>
250 MaterialOutputAction::materialOutputHelper<Real>(const std::string & property_name,
251  const Material & material,
252  bool get_names_only)
253 {
254  std::vector<std::string> names = {property_name};
255  if (!get_names_only)
256  {
257  auto params = getParams("MaterialRealAux", property_name, property_name, material);
258  _problem->addAuxKernel("MaterialRealAux", material.name() + property_name, params);
259  }
260 
261  return names;
262 }
263 
264 template <>
265 std::vector<std::string>
266 MaterialOutputAction::materialOutputHelper<RealVectorValue>(const std::string & property_name,
267  const Material & material,
268  bool get_names_only)
269 {
270  std::array<char, 3> suffix = {{'x', 'y', 'z'}};
271  std::vector<std::string> names(3);
272  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
273  {
274  std::ostringstream oss;
275  oss << property_name << "_" << suffix[i];
276  names[i] = oss.str();
277 
278  if (!get_names_only)
279  {
280  auto params = getParams("MaterialRealVectorValueAux", property_name, oss.str(), material);
281  params.set<unsigned int>("component") = i;
282  _problem->addAuxKernel("MaterialRealVectorValueAux", material.name() + oss.str(), params);
283  }
284  }
285 
286  return names;
287 }
288 
289 template <>
290 std::vector<std::string>
291 MaterialOutputAction::materialOutputHelper<RealTensorValue>(const std::string & property_name,
292  const Material & material,
293  bool get_names_only)
294 {
295  std::vector<std::string> names(LIBMESH_DIM * LIBMESH_DIM);
296 
297  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
298  for (unsigned int j = 0; j < LIBMESH_DIM; ++j)
299  {
300  std::ostringstream oss;
301  oss << property_name << "_" << i << j;
302  names[i * LIBMESH_DIM + j] = oss.str();
303 
304  if (!get_names_only)
305  {
306  auto params = getParams("MaterialRealTensorValueAux", property_name, oss.str(), material);
307  params.set<unsigned int>("row") = i;
308  params.set<unsigned int>("column") = j;
309  _problem->addAuxKernel("MaterialRealTensorValueAux", material.name() + oss.str(), params);
310  }
311  }
312 
313  return names;
314 }
315 
316 template <>
317 std::vector<std::string>
318 MaterialOutputAction::materialOutputHelper<RankTwoTensor>(const std::string & property_name,
319  const Material & material,
320  bool get_names_only)
321 {
322  std::vector<std::string> names(LIBMESH_DIM * LIBMESH_DIM);
323 
324  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
325  for (unsigned int j = 0; j < LIBMESH_DIM; ++j)
326  {
327  std::ostringstream oss;
328  oss << property_name << "_" << i << j;
329  names[i * LIBMESH_DIM + j] = oss.str();
330 
331  if (!get_names_only)
332  {
333  auto params = getParams("MaterialRankTwoTensorAux", property_name, oss.str(), material);
334  params.set<unsigned int>("i") = i;
335  params.set<unsigned int>("j") = j;
336  _problem->addAuxKernel("MaterialRankTwoTensorAux", material.name() + oss.str(), params);
337  }
338  }
339 
340  return names;
341 }
342 
343 template <>
344 std::vector<std::string>
345 MaterialOutputAction::materialOutputHelper<RankFourTensor>(const std::string & property_name,
346  const Material & material,
347  bool get_names_only)
348 {
349  std::vector<std::string> names(Utility::pow<4>(LIBMESH_DIM));
350 
351  unsigned int counter = 0;
352  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
353  for (unsigned int j = 0; j < LIBMESH_DIM; ++j)
354  for (unsigned int k = 0; k < LIBMESH_DIM; ++k)
355  for (unsigned int l = 0; l < LIBMESH_DIM; ++l)
356  {
357  std::ostringstream oss;
358  oss << property_name << "_" << i << j << k << l;
359  names[counter++] = oss.str();
360 
361  if (!get_names_only)
362  {
363  auto params =
364  getParams("MaterialRankFourTensorAux", property_name, oss.str(), material);
365  params.set<unsigned int>("i") = i;
366  params.set<unsigned int>("j") = j;
367  params.set<unsigned int>("k") = k;
368  params.set<unsigned int>("l") = l;
369  _problem->addAuxKernel(
370  "MaterialRankFourTensorAux", material.name() + oss.str(), params);
371  }
372  }
373 
374  return names;
375 }
std::map< OutputName, std::set< std::string > > _material_variable_names_map
Map of output names and list of variables associated with the output.
OutputWarehouse & _output_warehouse
Reference to the OutputWarehouse.
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
virtual bool boundaryRestricted() const
Returns true if this object has been restricted to a boundary.
const std::string & name() const
The name of the action.
Definition: Action.h:76
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:67
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:219
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
std::set< std::string > _material_variable_names
List of variables for the current Material object.
InputParameters validParams< MaterialOutputAction >()
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:186
const ExecFlagType EXEC_TIMESTEP_END
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
Base class for actions.
Definition: Action.h:35
InputParameters getParams(const std::string &type, const std::string &property_name, const std::string &variable_name, const Material &material)
A method for retrieving and partially filling the InputParameters object for an AuxVariable.
std::shared_ptr< MaterialData > _boundary_material_data
Pointer the MaterialData object storing the boundary restricted materials.
Creates AuxVariables and AuxKernels for automatic output of material properties.
std::shared_ptr< MaterialData > _block_material_data
Pointer the MaterialData object storing the block restricted materials.
Action for creating output objects.
InputParameters & getObjectParams()
Retreive the parameters of the object to be created by this action.
const std::string & _current_task
The current action (even though we have seperate instances for each action)
Definition: Action.h:210
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:146
Materials compute MaterialProperties.
Definition: Material.h:46
MatType type
const std::vector< SubdomainName > & blocks() const
Return the block names for this object.
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
const std::string & type() const
Definition: Action.h:86
MaterialOutputAction(InputParameters params)
Class constructor.
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:216
void addInterfaceHideVariables(const std::string &output_name, const std::set< std::string > &variable_names)
Insert variable names for hiding via the OutoutInterface.
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
MooseApp & _app
The MOOSE application this is associated with.
Definition: Action.h:183
const std::vector< BoundaryName > & boundaryNames() const
Return the boundary names for this object.
registerMooseAction("MooseApp", MaterialOutputAction, "add_output_aux_variables")
InputParameters validParams< Action >()
Definition: Action.C:22
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.