forked from idaholab/moose
/
MaterialOutputAction.C
568 lines (485 loc) · 21.5 KB
/
MaterialOutputAction.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
// MOOSE includes
#include "MaterialOutputAction.h"
#include "FEProblem.h"
#include "FEProblemBase.h"
#include "MooseApp.h"
#include "AddOutputAction.h"
#include "MaterialBase.h"
#include "RankTwoTensor.h"
#include "RankFourTensor.h"
#include "MooseEnum.h"
#include "MooseVariableConstMonomial.h"
#include "FunctorMaterial.h"
#include "libmesh/utility.h"
// Declare the output helper specializations
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<Real>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<ADReal>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<RealVectorValue>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<ADRealVectorValue>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<RealTensorValue>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<RankTwoTensor>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<ADRankTwoTensor>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::materialOutputHelper<RankFourTensor>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::functorMaterialOutputHelper<Real>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
template <>
std::vector<std::string> MaterialOutputAction::functorMaterialOutputHelper<RealVectorValue>(
const std::string & material_name, const MaterialBase & material, bool get_names_only);
registerMooseAction("MooseApp", MaterialOutputAction, "add_output_aux_variables");
registerMooseAction("MooseApp", MaterialOutputAction, "add_aux_kernel");
InputParameters
MaterialOutputAction::validParams()
{
InputParameters params = Action::validParams();
return params;
}
MaterialOutputAction::MaterialOutputAction(const InputParameters & params)
: Action(params),
_block_material_data(nullptr),
_boundary_material_data(nullptr),
_output_warehouse(_app.getOutputWarehouse()),
_output_only_on_timestep_end(_app.parameters().get<bool>("use_legacy_material_output"))
{
}
void
MaterialOutputAction::act()
{
mooseAssert(_problem,
"FEProblemBase pointer is nullptr, it is needed for auto material property output");
// Do nothing if the application does not have output
if (!_app.actionWarehouse().hasActions("add_output"))
return;
bool get_names_only = _current_task == "add_output_aux_variables" ? true : false;
// Set the pointers to the MaterialData objects (Note, these pointers are not available at
// construction)
_block_material_data = &_problem->getMaterialData(Moose::BLOCK_MATERIAL_DATA);
_boundary_material_data = &_problem->getMaterialData(Moose::BOUNDARY_MATERIAL_DATA);
// A complete list of all MaterialBase objects
const auto & material_ptrs = _problem->getMaterialWarehouse().getObjects();
// Handle setting of material property output in [Outputs] sub-blocks
// Output objects can enable material property output, the following code examines the parameters
// for each Output object and sets a flag if any Output object has output set and also builds a
// list if the
// properties are limited via the 'show_material_properties' parameters
bool outputs_has_properties = false;
std::set<std::string> output_object_properties;
const auto & output_actions = _app.actionWarehouse().getActionListByName("add_output");
for (const auto & act : output_actions)
{
// Extract the Output action
AddOutputAction * action = dynamic_cast<AddOutputAction *>(act);
if (!action)
continue;
// Add the material property names from the output object parameters to the list of properties
// to output
InputParameters & params = action->getObjectParams();
if (params.isParamValid("output_material_properties") &&
params.get<bool>("output_material_properties"))
{
outputs_has_properties = true;
std::vector<std::string> prop_names =
params.get<std::vector<std::string>>("show_material_properties");
output_object_properties.insert(prop_names.begin(), prop_names.end());
}
}
// Loop through each material object
std::set<std::string> material_names;
std::set<std::string> unsupported_names;
for (const auto & mat : material_ptrs)
{
// Extract the names of the output objects to which the material properties will be exported
std::set<OutputName> outputs = mat->getOutputs();
// Extract the property names that will actually be output
std::vector<std::string> output_properties =
mat->getParam<std::vector<std::string>>("output_properties");
// Append the properties listed in the Outputs block
if (outputs_has_properties)
output_properties.insert(output_properties.end(),
output_object_properties.begin(),
output_object_properties.end());
// Clear the list of variable names for the current material object, this list will be populated
// with all the
// variables names for the current material object and is needed for purposes of controlling the
// which output objects
// show the material property data
_material_variable_names.clear();
// Create necessary outputs for the properties if:
// (1) The Outputs block has material output enabled
// (2) If the MaterialBase object itself has set the 'outputs' parameter
if (outputs_has_properties || outputs.find("none") == outputs.end())
{
// Get all material properties supplied by this material as a starting point
std::set<std::string> names = mat->getSuppliedItems();
if (dynamic_cast<const FunctorMaterial *>(mat.get()))
{
const auto fmat_ptr = dynamic_cast<const FunctorMaterial *>(mat.get());
names.insert(fmat_ptr->getSuppliedFunctors().begin(),
fmat_ptr->getSuppliedFunctors().end());
}
for (const auto & name : names)
{
// Output the property only if the name is contained in the 'output_properties'
// list or if the list is empty (all properties)
if (output_properties.empty() ||
std::find(output_properties.begin(), output_properties.end(), name) !=
output_properties.end())
{
// Add the material property for output
auto curr_material_names = materialOutput(name, *mat, get_names_only);
if (curr_material_names.size() == 0)
unsupported_names.insert(name);
material_names.insert(curr_material_names.begin(), curr_material_names.end());
}
}
// If the material object has limited outputs, store the variables associated with the
// output objects
if (!outputs.empty())
for (const auto & output_name : outputs)
_material_variable_names_map[output_name].insert(_material_variable_names.begin(),
_material_variable_names.end());
}
}
if (unsupported_names.size() > 0 && get_names_only)
{
std::ostringstream oss;
for (const auto & name : unsupported_names)
oss << "\n " << name;
mooseWarning("The types for total ",
unsupported_names.size(),
" material properties:",
oss.str(),
"\nare not supported for automatic output by ",
type(),
".");
}
if (_current_task == "add_output_aux_variables")
{
auto params = _factory.getValidParams("MooseVariableConstMonomial");
// currently only elemental variables are supported for material property output
params.set<MooseEnum>("order") = "CONSTANT";
params.set<MooseEnum>("family") = "MONOMIAL";
// Create the AuxVariables
std::ostringstream oss;
for (const auto & var_name : material_names)
{
oss << "\n " << var_name;
if (_problem->hasVariable(var_name))
mooseError("The material property output " + var_name +
" has the same name as an existing variable, either use the material"
" declare_suffix parameter to disambiguate or the output_properties parameter"
" to restrict the material properties to output");
_problem->addAuxVariable("MooseVariableConstMonomial", var_name, params);
}
if (material_names.size() > 0)
_console << COLOR_CYAN << "The following total " << material_names.size()
<< " aux variables:" << oss.str() << "\nare added for automatic output by " << type()
<< "." << COLOR_DEFAULT << std::endl;
}
else
{
// When a MaterialBase object has 'output_properties' defined all other properties not listed
// must be added to the hide list for the output objects so that properties that are not desired
// do not appear.
for (const auto & it : _material_variable_names_map)
{
std::set<std::string> hide;
std::set_difference(material_names.begin(),
material_names.end(),
it.second.begin(),
it.second.end(),
std::inserter(hide, hide.begin()));
_output_warehouse.addInterfaceHideVariables(it.first, hide);
}
}
}
std::vector<std::string>
MaterialOutputAction::materialOutput(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names;
if (hasProperty<Real>(property_name))
names = materialOutputHelper<Real>(property_name, material, get_names_only);
else if (hasADProperty<Real>(property_name))
names = materialOutputHelper<ADReal>(property_name, material, get_names_only);
else if (hasProperty<RealVectorValue>(property_name))
names = materialOutputHelper<RealVectorValue>(property_name, material, get_names_only);
else if (hasADProperty<RealVectorValue>(property_name))
names = materialOutputHelper<ADRealVectorValue>(property_name, material, get_names_only);
else if (hasProperty<RealTensorValue>(property_name))
names = materialOutputHelper<RealTensorValue>(property_name, material, get_names_only);
else if (hasProperty<RankTwoTensor>(property_name))
names = materialOutputHelper<RankTwoTensor>(property_name, material, get_names_only);
else if (hasADProperty<RankTwoTensor>(property_name))
names = materialOutputHelper<ADRankTwoTensor>(property_name, material, get_names_only);
else if (hasProperty<RankFourTensor>(property_name))
names = materialOutputHelper<RankFourTensor>(property_name, material, get_names_only);
else if (hasFunctorProperty<Real>(property_name))
names = functorMaterialOutputHelper<Real>(property_name, material, get_names_only);
else if (hasFunctorProperty<ADReal>(property_name))
names = functorMaterialOutputHelper<Real>(property_name, material, get_names_only);
else if (hasFunctorProperty<RealVectorValue>(property_name))
names = functorMaterialOutputHelper<RealVectorValue>(property_name, material, get_names_only);
else if (hasFunctorProperty<ADRealVectorValue>(property_name))
names = functorMaterialOutputHelper<RealVectorValue>(property_name, material, get_names_only);
return names;
}
InputParameters
MaterialOutputAction::getParams(const std::string & type,
const std::string & property_name,
const std::string & variable_name,
const MaterialBase & material)
{
// Append the list of output variables for the current material
_material_variable_names.insert(variable_name);
// Set the action parameters
InputParameters params = _factory.getValidParams(type);
// Adapt for regular or functor materials
if (type.find("Material") != std::string::npos)
params.set<MaterialPropertyName>("property") = property_name;
else
params.set<MooseFunctorName>("functor") = property_name;
params.set<AuxVariableName>("variable") = variable_name;
if (_output_only_on_timestep_end)
params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_END;
else
params.set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
if (material.boundaryRestricted())
params.set<std::vector<BoundaryName>>("boundary") = material.boundaryNames();
else
params.set<std::vector<SubdomainName>>("block") = material.blocks();
return params;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<Real>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names = {property_name};
if (!get_names_only)
{
auto params = getParams("MaterialRealAux", property_name, property_name, material);
_problem->addAuxKernel("MaterialRealAux", material.name() + property_name, params);
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<ADReal>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names = {property_name};
if (!get_names_only)
{
auto params = getParams("ADMaterialRealAux", property_name, property_name, material);
_problem->addAuxKernel("ADMaterialRealAux", material.name() + property_name, params);
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<RealVectorValue>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::array<char, 3> suffix = {{'x', 'y', 'z'}};
std::vector<std::string> names(3);
for (const auto i : make_range(Moose::dim))
{
std::ostringstream oss;
oss << property_name << "_" << suffix[i];
names[i] = oss.str();
if (!get_names_only)
{
auto params = getParams("MaterialRealVectorValueAux", property_name, oss.str(), material);
params.set<unsigned int>("component") = i;
_problem->addAuxKernel("MaterialRealVectorValueAux", material.name() + oss.str(), params);
}
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<ADRealVectorValue>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::array<char, 3> suffix = {{'x', 'y', 'z'}};
std::vector<std::string> names(3);
for (const auto i : make_range(Moose::dim))
{
std::ostringstream oss;
oss << property_name << "_" << suffix[i];
names[i] = oss.str();
if (!get_names_only)
{
auto params = getParams("ADMaterialRealVectorValueAux", property_name, oss.str(), material);
params.set<unsigned int>("component") = i;
_problem->addAuxKernel("ADMaterialRealVectorValueAux", material.name() + oss.str(), params);
}
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<RealTensorValue>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names(Moose::dim * Moose::dim);
for (const auto i : make_range(Moose::dim))
for (const auto j : make_range(Moose::dim))
{
std::ostringstream oss;
oss << property_name << "_" << i << j;
names[i * Moose::dim + j] = oss.str();
if (!get_names_only)
{
auto params = getParams("MaterialRealTensorValueAux", property_name, oss.str(), material);
params.set<unsigned int>("row") = i;
params.set<unsigned int>("column") = j;
_problem->addAuxKernel("MaterialRealTensorValueAux", material.name() + oss.str(), params);
}
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<RankTwoTensor>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names(Moose::dim * Moose::dim);
for (const auto i : make_range(Moose::dim))
for (const auto j : make_range(Moose::dim))
{
std::ostringstream oss;
oss << property_name << "_" << i << j;
names[i * Moose::dim + j] = oss.str();
if (!get_names_only)
{
auto params = getParams("MaterialRankTwoTensorAux", property_name, oss.str(), material);
params.set<unsigned int>("i") = i;
params.set<unsigned int>("j") = j;
_problem->addAuxKernel("MaterialRankTwoTensorAux", material.name() + oss.str(), params);
}
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<ADRankTwoTensor>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names(Moose::dim * Moose::dim);
for (const auto i : make_range(Moose::dim))
for (const auto j : make_range(Moose::dim))
{
std::ostringstream oss;
oss << property_name << "_" << i << j;
names[i * Moose::dim + j] = oss.str();
if (!get_names_only)
{
auto params = getParams("ADMaterialRankTwoTensorAux", property_name, oss.str(), material);
params.set<unsigned int>("i") = i;
params.set<unsigned int>("j") = j;
_problem->addAuxKernel("ADMaterialRankTwoTensorAux", material.name() + oss.str(), params);
}
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::materialOutputHelper<RankFourTensor>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names(Utility::pow<4>(Moose::dim));
unsigned int counter = 0;
for (const auto i : make_range(Moose::dim))
for (const auto j : make_range(Moose::dim))
for (const auto k : make_range(Moose::dim))
for (const auto l : make_range(Moose::dim))
{
std::ostringstream oss;
oss << property_name << "_" << i << j << k << l;
names[counter++] = oss.str();
if (!get_names_only)
{
auto params =
getParams("MaterialRankFourTensorAux", property_name, oss.str(), material);
params.set<unsigned int>("i") = i;
params.set<unsigned int>("j") = j;
params.set<unsigned int>("k") = k;
params.set<unsigned int>("l") = l;
_problem->addAuxKernel(
"MaterialRankFourTensorAux", material.name() + oss.str(), params);
}
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::functorMaterialOutputHelper<Real>(const std::string & property_name,
const MaterialBase & material,
bool get_names_only)
{
std::vector<std::string> names = {property_name + "_out"};
if (!get_names_only)
{
// add a '_out' suffix to avoid name conflicts between the functor property and aux-variable
auto params = getParams("FunctorAux", property_name, property_name + "_out", material);
_problem->addAuxKernel("FunctorAux", material.name() + property_name, params);
}
return names;
}
template <>
std::vector<std::string>
MaterialOutputAction::functorMaterialOutputHelper<RealVectorValue>(
const std::string & property_name, const MaterialBase & material, bool get_names_only)
{
std::array<char, 3> suffix = {{'x', 'y', 'z'}};
std::vector<std::string> names(3);
for (const auto i : make_range(Moose::dim))
{
std::ostringstream oss;
// add a '_out' suffix to avoid name conflicts between the functor property and aux-variable
oss << property_name << "_out_" << suffix[i];
names[i] = oss.str();
if (!get_names_only)
{
auto params = getParams("FunctorVectorElementalAux", property_name, oss.str(), material);
params.set<unsigned int>("component") = i;
_problem->addAuxKernel("FunctorVectorElementalAux", material.name() + oss.str(), params);
}
}
return names;
}