www.mooseframework.org
MaterialOutputAction.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 
15 // MOOSE includes
16 #include "MaterialOutputAction.h"
17 #include "FEProblem.h"
18 #include "MooseApp.h"
19 #include "AddOutputAction.h"
20 #include "Material.h"
21 
22 // Declare the output helper specializations
23 template <>
24 void MaterialOutputAction::materialOutputHelper<Real>(const std::string & material_name,
25  std::shared_ptr<Material> material);
26 
27 template <>
28 void
29 MaterialOutputAction::materialOutputHelper<RealVectorValue>(const std::string & material_name,
30  std::shared_ptr<Material> material);
31 
32 template <>
33 void
34 MaterialOutputAction::materialOutputHelper<RealTensorValue>(const std::string & material_name,
35  std::shared_ptr<Material> material);
36 
37 template <>
40 {
42  return params;
43 }
44 
46  : Action(params), _output_warehouse(_app.getOutputWarehouse())
47 {
48 }
49 
50 void
52 {
53  // Error if _problem is NULL, I don't know how this would happen
54  if (_problem.get() == NULL)
55  mooseError("FEProblemBase pointer is NULL, it is needed for auto material property output");
56 
58 }
59 
60 void
62 {
63  // Do nothing if the application does not have output
64  if (!_app.actionWarehouse().hasActions("add_output"))
65  return;
66 
67  // Set the pointers to the MaterialData objects (Note, these pointers are not available at
68  // construction)
71 
72  // A complete list of all Material objects
73  const std::vector<std::shared_ptr<Material>> & materials =
74  problem_ptr->getMaterialWarehouse().getObjects();
75 
76  // Handle setting of material property output in [Outputs] sub-blocks
77  // Output objects can enable material property output, the following code examines the parameters
78  // for each Output object and sets a flag if any Output object has output set and also builds a
79  // list if the
80  // properties are limited via the 'show_material_properties' parameters
81  bool outputs_has_properties = false;
82  std::set<std::string> output_object_properties;
83 
84  const auto & output_actions = _app.actionWarehouse().getActionListByName("add_output");
85  for (const auto & act : output_actions)
86  {
87  // Extract the Output action
88  AddOutputAction * action = dynamic_cast<AddOutputAction *>(act);
89  mooseAssert(action != NULL, "No AddOutputAction with the name " << act->name() << " exists");
90 
91  // Add the material property names from the output object parameters to the list of properties
92  // to output
93  InputParameters & params = action->getObjectParams();
94  if (params.isParamValid("output_material_properties") &&
95  params.get<bool>("output_material_properties"))
96  {
97  outputs_has_properties = true;
98  std::vector<std::string> prop_names =
99  params.get<std::vector<std::string>>("show_material_properties");
100  output_object_properties.insert(prop_names.begin(), prop_names.end());
101  }
102  }
103 
104  // Loop through each material object
105  for (const auto & mat : materials)
106  {
107  // Extract the names of the output objects to which the material properties will be exported
108  std::set<OutputName> outputs = mat->getOutputs();
109 
110  // Extract the property names that will actually be output
111  std::vector<std::string> output_properties =
112  mat->getParam<std::vector<std::string>>("output_properties");
113 
114  // Append the properties listed in the Outputs block
115  if (outputs_has_properties)
116  output_properties.insert(output_properties.end(),
117  output_object_properties.begin(),
118  output_object_properties.end());
119 
120  // Clear the list of variable names for the current material object, this list will be populated
121  // with all the
122  // variables names for the current material object and is needed for purposes of controlling the
123  // which output objects
124  // show the material property data
125  _material_variable_names.clear();
126 
127  // Create necessary outputs for the properties if:
128  // (1) The Outputs block has material output enabled
129  // (2) If the Material object itself has set the 'outputs' parameter
130  if (outputs_has_properties || outputs.find("none") == outputs.end())
131  {
132  // Add the material property for output if the name is contained in the 'output_properties'
133  // list
134  // or if the list is empty (all properties)
135  const std::set<std::string> names = mat->getSuppliedItems();
136  for (const auto & name : names)
137  {
138  // Add the material property for output
139  if (output_properties.empty() ||
140  std::find(output_properties.begin(), output_properties.end(), name) !=
141  output_properties.end())
142  {
143  if (hasProperty<Real>(name))
144  materialOutputHelper<Real>(name, mat);
145 
146  else if (hasProperty<RealVectorValue>(name))
147  materialOutputHelper<RealVectorValue>(name, mat);
148 
149  else if (hasProperty<RealTensorValue>(name))
150  materialOutputHelper<RealTensorValue>(name, mat);
151 
152  else
153  mooseWarning("The type for material property '",
154  name,
155  "' is not supported for automatic output.");
156  }
157 
158  // If the material object as limited outputs, store the variables associated with the output
159  // objects
160  if (!outputs.empty())
161  for (const auto & output_name : outputs)
162  _material_variable_names_map[output_name].insert(_material_variable_names.begin(),
164  }
165  }
166  }
167 
168  // Create the AuxVariables
169  FEType fe_type(
170  CONSTANT,
171  MONOMIAL); // currently only elemental variables are support for material property output
172  for (const auto & var_name : _variable_names)
173  problem_ptr->addAuxVariable(var_name, fe_type);
174 
175  // When a Material object has 'output_properties' defined all other properties not listed must be
176  // added to
177  // the hide list for the output objects so that properties that are not desired do not appear.
178  for (const auto & it : _material_variable_names_map)
179  {
180  std::set<std::string> hide;
181  std::set_difference(_variable_names.begin(),
182  _variable_names.end(),
183  it.second.begin(),
184  it.second.end(),
185  std::inserter(hide, hide.begin()));
186 
188  }
189 }
190 
191 std::shared_ptr<MooseObjectAction>
193  const std::string & property_name,
194  const std::string & variable_name,
195  std::shared_ptr<Material> material)
196 {
197  // Append the list of variables to create
198  _variable_names.insert(variable_name);
199 
200  // Append the list of output variables for the current material
201  _material_variable_names.insert(variable_name);
202 
203  // Generate the name
204  std::ostringstream name;
205  name << material->name() << "_" << variable_name;
206 
207  // Set the action parameters
208  InputParameters action_params = _action_factory.getValidParams("AddKernelAction");
209  action_params.set<std::string>("type") = type;
210  action_params.set<ActionWarehouse *>("awh") = &_awh;
211  action_params.set<std::string>("task") = "add_aux_kernel";
212 
213  // Create the action
214  std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
215  _action_factory.create("AddKernelAction", name.str(), action_params));
216 
217  // Set the object parameters
218  InputParameters & object_params = action->getObjectParams();
219  object_params.set<MaterialPropertyName>("property") = property_name;
220  object_params.set<AuxVariableName>("variable") = variable_name;
221  object_params.set<MultiMooseEnum>("execute_on") = "timestep_end";
222 
223  if (material->boundaryRestricted())
224  object_params.set<std::vector<BoundaryName>>("boundary") = material->boundaryNames();
225  else
226  object_params.set<std::vector<SubdomainName>>("block") = material->blocks();
227 
228  // Return the created action
229  return action;
230 }
231 
232 template <>
233 void
234 MaterialOutputAction::materialOutputHelper<Real>(const std::string & property_name,
235  std::shared_ptr<Material> material)
236 {
237  _awh.addActionBlock(createAction("MaterialRealAux", property_name, property_name, material));
238 }
239 
240 template <>
241 void
242 MaterialOutputAction::materialOutputHelper<RealVectorValue>(const std::string & property_name,
243  std::shared_ptr<Material> material)
244 {
245  char suffix[3] = {'x', 'y', 'z'};
246 
247  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
248  {
249  std::ostringstream oss;
250  oss << property_name << "_" << suffix[i];
251 
252  std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
253  createAction("MaterialRealVectorValueAux", property_name, oss.str(), material));
254  action->getObjectParams().set<unsigned int>("component") = i;
255  _awh.addActionBlock(action);
256  }
257 }
258 
259 template <>
260 void
261 MaterialOutputAction::materialOutputHelper<RealTensorValue>(const std::string & property_name,
262  std::shared_ptr<Material> material)
263 {
264  for (unsigned int i = 0; i < LIBMESH_DIM; ++i)
265  for (unsigned int j = 0; j < LIBMESH_DIM; ++j)
266  {
267  std::ostringstream oss;
268  oss << property_name << "_" << i << j;
269 
270  std::shared_ptr<MooseObjectAction> action = std::static_pointer_cast<MooseObjectAction>(
271  createAction("MaterialRealTensorValueAux", property_name, oss.str(), material));
272  action->getObjectParams().set<unsigned int>("row") = i;
273  action->getObjectParams().set<unsigned int>("column") = j;
274  _awh.addActionBlock(action);
275  }
276 }
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.
ActionWarehouse & _awh
Reference to ActionWarehouse where we store object build by actions.
Definition: Action.h:122
ActionFactory & _action_factory
Builds Actions.
Definition: Action.h:104
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:74
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.
std::set< std::string > _material_variable_names
List of variables for the current Material object.
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
InputParameters validParams< MaterialOutputAction >()
Storage for action instances.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void addActionBlock(std::shared_ptr< Action > blk)
This method add an Action instance to the warehouse.
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Base class for actions.
Definition: Action.h:39
std::shared_ptr< MaterialData > _boundary_material_data
Pointer the MaterialData object storing the boundary restricted materials.
std::shared_ptr< MaterialData > _block_material_data
Pointer the MaterialData object storing the block restricted materials.
const std::string & type() const
Definition: Action.h:61
Action for creating output objects.
InputParameters & getObjectParams()
Retreive the parameters of the object to be created by this action.
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:130
std::shared_ptr< MaterialData > getMaterialData(Moose::MaterialDataType type, THREAD_ID tid=0)
std::shared_ptr< MooseObjectAction > createAction(const std::string &type, const std::string &property_name, const std::string &variable_name, std::shared_ptr< Material > material)
A method for creating an AuxVariable and associated action.
const MaterialWarehouse & getMaterialWarehouse()
const std::string & name() const
The name of the action.
Definition: Action.h:51
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:26
MaterialOutputAction(InputParameters params)
Class constructor.
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:131
void buildMaterialOutputObjects(FEProblemBase *problem_ptr)
Builds the objects necessary for material property output.
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
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
const std::vector< std::shared_ptr< T > > & getObjects(THREAD_ID tid=0) const
Retrieve complete vector to the all/block/boundary restricted objects for a given thread...
MooseApp & _app
The MOOSE application this is associated with.
Definition: Action.h:98
std::set< std::string > _variable_names
Set of variable names for boundary.
InputParameters validParams< Action >()
Definition: Action.C:23
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194
void addAuxVariable(const std::string &var_name, const FEType &type, const std::set< SubdomainID > *const active_subdomains=NULL)