https://mooseframework.inl.gov
ParsedVectorRealReductionReporter.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 
13 
16 {
18  params.addClassDescription(
19  "Use a parsed function to iterate through a vector and reduce it to a scalar.");
20  params.addRequiredParam<ReporterName>("reporter_name", "Reporter name with vector to reduce.");
21  params.addRequiredParam<Real>("initial_value", "Value to intialize the reduction with.");
22 
23  // reporter_symbols are the two symbols for reduction value and current value for the reduction
24  // operation, these symbols are enforced in the constructor with a mooseError
25  params.set<std::vector<std::string>>("reporter_symbols") = {"reduction_value", "indexed_value"};
26  params.suppressParameter<std::vector<std::string>>("reporter_symbols");
27 
28  // This reporter is for postprocessing optimization results and should be exectuted at the end of
29  // execution
30  params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_END;
31  return params;
32 }
33 
35  const InputParameters & parameters)
36  : ParsedReporterBase(parameters),
37  _initial_value(getParam<Real>("initial_value")),
38  _reporter_data(
39  getReporterValueByName<std::vector<Real>>(getParam<ReporterName>("reporter_name"))),
40  _output_reporter(declareValueByName<Real>(getParam<std::string>("name"), REPORTER_MODE_ROOT))
41 {
42  // parse function
43  std::string function = getParam<std::string>("expression");
44  // make sure the expression has the two required variables, vi and vplus
45  if (function.find("reduction_value") == std::string::npos ||
46  function.find("indexed_value") == std::string::npos)
47  paramError(
48  "expression",
49  "Parsed function must contain the two symbols 'reduction_value' and 'indexed_value'.");
50 }
51 
52 void
54 {
55  Real reduction = _initial_value;
56  for (const auto i : index_range(_reporter_data))
57  {
58  _func_params[0] = reduction;
60 
61  if (_use_t)
62  _func_params[2] = _t;
63 
64  reduction = evaluate(_func_F);
65  }
66  _output_reporter = reduction;
67 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Reporter performing a reduction on a vector using a parsed function.
const ReporterMode REPORTER_MODE_ROOT
T & set(const std::string &name, bool quiet_mode=false)
Real & _output_reporter
output containing reduction of vector into a scalar
const ExecFlagType EXEC_TIMESTEP_END
const std::vector< Real > & _reporter_data
Vector being operated on.
SymFunctionPtr _func_F
function parser object
void addRequiredParam(const std::string &name, const std::string &doc_string)
void suppressParameter(const std::string &name)
ParsedVectorRealReductionReporter(const InputParameters &parameters)
registerMooseObject("OptimizationApp", ParsedVectorRealReductionReporter)
static InputParameters validParams()
void paramError(const std::string &param, Args... args) const
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
void addClassDescription(const std::string &doc_string)
Reporter containing operation between vectors from another Reporter.
auto index_range(const T &sizable)