https://mooseframework.inl.gov
ParsedVectorVectorRealReductionReporter.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 #include <cstddef>
12 
14 
17 {
19  params.addClassDescription("Use a parsed function to iterate through a rows of a vector of "
20  "vector and reduce it to a vector.");
21  params.addRequiredParam<ReporterName>("reporter_name",
22  "Reporter name with vector of vectors to reduce.");
23  params.addRequiredParam<Real>("initial_value", "Value to intialize the reduction with.");
24 
25  // reporter_symbols are the two symbols for reduction value and current value for the reduction
26  // operation, these symbols are enforced in the constructor with a mooseError
27  params.set<std::vector<std::string>>("reporter_symbols") = {"reduction_value", "indexed_value"};
28  params.suppressParameter<std::vector<std::string>>("reporter_symbols");
29 
30  // This reporter is for postprocessing optimization results and should be exectuted at the end of
31  // execution
32  params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_END;
33  return params;
34 }
35 
37  const InputParameters & parameters)
38  : ParsedReporterBase(parameters),
39  _initial_value(getParam<Real>("initial_value")),
40  _vec_of_vec_name(getParam<ReporterName>("reporter_name")),
41  _output_reporter(
42  declareValueByName<std::vector<Real>>(getParam<std::string>("name"), REPORTER_MODE_ROOT)),
43  _reporter_data(getReporterValueByName<std::vector<std::vector<Real>>>(
44  getParam<ReporterName>("reporter_name")))
45 {
46  // parse function
47  std::string function = getParam<std::string>("expression");
48  // make sure the expression has the two required variables, vi and vplus
49  if (function.find("reduction_value") == std::string::npos ||
50  function.find("indexed_value") == std::string::npos)
51  paramError(
52  "expression",
53  "Parsed function must contain the two symbols 'reduction_value' and 'indexed_value'.");
54 }
55 
56 void
58 {
59  std::size_t ncols = _reporter_data.size();
60  std::size_t nrows = 0;
61  if (!_reporter_data.empty())
62  nrows = _reporter_data[0].size();
63 
64  for (auto & reporter_vector : _reporter_data)
65  {
66  if (reporter_vector.size() != nrows)
67  mooseError("Every vector in 'reporter_name=",
69  "' must be the same size.",
70  "\nFirst Vector size = ",
71  nrows,
72  "\nCurrent Vector size = ",
73  reporter_vector.size());
74  }
75 
76  _output_reporter.clear();
77  _output_reporter.resize(nrows, _initial_value);
78  for (const auto i_row : make_range(nrows))
79  {
80  Real reduction = _initial_value;
81  for (const auto j_col : make_range(ncols))
82  {
83  _func_params[0] = reduction;
84  _func_params[1] = _reporter_data[j_col][i_row];
85 
86  if (_use_t)
87  _func_params[2] = _t;
88 
89  reduction = evaluate(_func_F);
90  }
91  _output_reporter[i_row] = reduction;
92  }
93 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
const std::vector< std::vector< Real > > & _reporter_data
Vector being operated on.
const ReporterMode REPORTER_MODE_ROOT
T & set(const std::string &name, bool quiet_mode=false)
const ExecFlagType EXEC_TIMESTEP_END
SymFunctionPtr _func_F
function parser object
void addRequiredParam(const std::string &name, const std::string &doc_string)
void suppressParameter(const std::string &name)
ParsedVectorVectorRealReductionReporter(const InputParameters &parameters)
std::vector< Real > & _output_reporter
output containing reduction of vector of vector into a vector
static InputParameters validParams()
void paramError(const std::string &param, Args... args) const
registerMooseObject("OptimizationApp", ParsedVectorVectorRealReductionReporter)
Reporter containing row sum of a vector of vectors from another Reporter.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< GenericReal< is_ad > > _func_params
const bool _use_t
whether time is part of the parsed expression
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
Reporter containing operation between vectors from another Reporter.