https://mooseframework.inl.gov
ElementExtremeMaterialPropertyReporter.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 "metaphysicl/raw_type.h"
13 #include "libmesh/parallel.h"
14 #include "libmesh/parallel_algebra.h"
15 
16 #include <algorithm>
17 #include <limits>
18 
21 
22 template <bool is_ad>
25 {
27 
28  params.addRequiredParam<MaterialPropertyName>("material_property",
29  "Material property for which to find the extreme. "
30  "The value of this property is always reported.");
31  MooseEnum type_options("max=0 min=1");
32  params.addRequiredParam<MooseEnum>("value_type",
33  type_options,
34  "Type of extreme value to report: 'max' "
35  "reports the maximum value and 'min' reports "
36  "the minimum value.");
37 
38  params.addParam<std::vector<MaterialPropertyName>>(
39  "additional_reported_properties",
40  {},
41  "Additional material properties reported at the location of the extreme value");
42  params.addClassDescription(
43  "Determines the location of the minimum or maximum value of a material property over a "
44  "volume, and provides its coordinates and optionally other requested data at that location.");
45 
46  return params;
47 }
48 
49 template <bool is_ad>
51  const InputParameters & parameters)
52  : ElementReporter(parameters),
53  _mat_prop(getGenericMaterialProperty<Real, is_ad>("material_property")),
54  _type(getParam<MooseEnum>("value_type").template getEnum<ExtremeType>()),
55  _extreme_value(declareValueByName<Real>("extreme_value")),
56  _coordinates(declareValueByName<Point>("coordinates")),
57  _qp(0)
58 {
59  const auto & mat_prop_names =
60  getParam<std::vector<MaterialPropertyName>>("additional_reported_properties");
61  // TODO: Add more options for the type of reported properties. Currently these have to be
62  // either Real (if using the non-AD version of this, or ADReal if using the AD version).
63  // ElementMaterialSampler can get multiple types (int, real, unsigned), maybe do
64  // something like that and also get AD and non-AD verisons.
65  for (const auto & mpn : mat_prop_names)
66  {
67  _additional_reported_properties.push_back(&getGenericMaterialPropertyByName<Real, is_ad>(mpn));
68  _additional_reported_property_values.push_back(&declareValueByName<Real>(mpn));
69  }
70 }
71 
72 template <bool is_ad>
73 void
75 {
76  switch (_type)
77  {
78  case ExtremeType::MAX:
79  _extreme_value = -std::numeric_limits<Real>::max();
80  break;
81 
82  case ExtremeType::MIN:
83  _extreme_value = std::numeric_limits<Real>::max();
84  break;
85  }
86  _coordinates = Point(0., 0., 0.);
87  for (const auto i : index_range(_additional_reported_properties))
88  *_additional_reported_property_values[i] = 0.0;
89 }
90 
91 template <bool is_ad>
92 void
94 {
95  for (_qp = 0; _qp < _qrule->n_points(); ++_qp)
96  computeQpValue();
97 }
98 
99 template <bool is_ad>
100 void
102 {
103  const Real raw_mat_val = MetaPhysicL::raw_value(_mat_prop[_qp]);
104  switch (_type)
105  {
106  case ExtremeType::MAX:
107  if (raw_mat_val > _extreme_value)
108  {
109  _extreme_value = raw_mat_val;
110  _coordinates = _q_point[_qp];
111  for (const auto i : index_range(_additional_reported_properties))
112  *_additional_reported_property_values[i] =
113  MetaPhysicL::raw_value((*_additional_reported_properties[i])[_qp]);
114  }
115  break;
116 
117  case ExtremeType::MIN:
118  if (raw_mat_val < _extreme_value)
119  {
120  _extreme_value = raw_mat_val;
121  _coordinates = _q_point[_qp];
122  for (const auto i : index_range(_additional_reported_properties))
123  *_additional_reported_property_values[i] =
124  MetaPhysicL::raw_value((*_additional_reported_properties[i])[_qp]);
125  }
126  break;
127  }
128 }
129 
130 template <bool is_ad>
131 void
133 {
134  unsigned int rank = 0;
135 
136  switch (_type)
137  {
138  case ExtremeType::MAX:
139  _communicator.maxloc(_extreme_value, rank);
140  break;
141  case ExtremeType::MIN:
142  _communicator.minloc(_extreme_value, rank);
143  break;
144  }
145 
146  const auto prop_size = _additional_reported_property_values.size();
147  std::vector<Real> ev_rep_prop_vals(prop_size, 0.);
148 
149  _communicator.broadcast(_coordinates, rank);
150  if (rank == processor_id())
151  for (const auto i : make_range(prop_size))
152  ev_rep_prop_vals[i] = *_additional_reported_property_values[i];
153  _communicator.broadcast(ev_rep_prop_vals, rank, /*identical_sizes=*/true);
154  for (const auto i : make_range(prop_size))
155  *_additional_reported_property_values[i] = ev_rep_prop_vals[i];
156 }
157 
158 template <bool is_ad>
159 void
161 {
162  const auto & rpt = static_cast<const ElementExtremeMaterialPropertyReporterTempl<is_ad> &>(uo);
163  const auto prop_size = _additional_reported_property_values.size();
164 
165  switch (_type)
166  {
167  case ExtremeType::MAX:
168  if (rpt._extreme_value > _extreme_value)
169  {
170  _extreme_value = rpt._extreme_value;
171  _coordinates = rpt._coordinates;
172  for (const auto i : make_range(prop_size))
173  *_additional_reported_property_values[i] = *(rpt._additional_reported_property_values[i]);
174  }
175  break;
176 
177  case ExtremeType::MIN:
178  if (rpt._extreme_value < _extreme_value)
179  {
180  _extreme_value = rpt._extreme_value;
181  _coordinates = rpt._coordinates;
182  for (const auto i : make_range(prop_size))
183  *_additional_reported_property_values[i] = *(rpt._additional_reported_property_values[i]);
184  }
185  break;
186  }
187 }
188 
static InputParameters validParams()
virtual void threadJoin(const UserObject &uo) override
Must override.
std::vector< const GenericMaterialProperty< Real, is_ad > * > _additional_reported_properties
Pointers to other reported material property objects.
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:100
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
registerMooseObject("MooseApp", ElementExtremeMaterialPropertyReporter)
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...
auto max(const L &left, const R &right)
ElementExtremeMaterialPropertyReporterTempl(const InputParameters &parameters)
Determines the location of the extreme (minimum or maximum) value of a material property, as well as values of a specified set of properties at that location.
void computeQpValue()
Check a given quadrature point for the extreme value, and capture the extreme value as well as the ot...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
auto index_range(const T &sizable)
Base class for user-specific data.
Definition: UserObject.h:19
std::vector< Real * > _additional_reported_property_values
Values of other reported material properties.