https://mooseframework.inl.gov
ParsedReporterBase.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 
10 #include "ParsedReporterBase.h"
11 
14 {
17  params.addRequiredCustomTypeParam<std::string>(
18  "expression", "FunctionExpression", "function expression");
19  params.addParam<std::string>("name", "result", "Name of output reporter.");
20  params.addParam<std::vector<std::string>>(
21  "vector_reporter_symbols", {}, "Expression symbol for each reporter");
22  params.addParam<std::vector<std::string>>(
23  "scalar_reporter_symbols",
24  {},
25  "Expression symbol for each scalar reporter, i.e. postprocessors");
26  params.addParam<std::vector<std::string>>(
27  "constant_names",
28  {},
29  "Vector of constants used in the parsed function (use this for kB etc.)");
30  params.addParam<std::vector<ReporterName>>("scalar_reporter_names",
31  "Scalar reporter names to apply function to.");
32  params.addParam<std::vector<std::string>>(
33  "constant_expressions",
34  {},
35  "Vector of values for the constants in constant_names (can be an FParser expression)");
36  params.addParam<bool>(
37  "use_t", false, "Make time (t) variables available in the function expression.");
38  return params;
39 }
40 
42  : GeneralReporter(parameters),
43  FunctionParserUtils(parameters),
44  _use_t(getParam<bool>("use_t")),
45  _vector_reporter_symbols(getParam<std::vector<std::string>>("vector_reporter_symbols")),
46  _scalar_reporter_symbols(getParam<std::vector<std::string>>("scalar_reporter_symbols"))
47 {
48  // checking that symbols and names vectors are the same size
49  if (parameters.isParamValid("scalar_reporter_names"))
50  {
51  // get scalar reporter can be checked and gotten here if input
52  // Vector reporters must be handled differently by each derived class
53  const std::vector<ReporterName> scalar_reporter_names(
54  getParam<std::vector<ReporterName>>("scalar_reporter_names"));
55 
56  if (scalar_reporter_names.size() != _scalar_reporter_symbols.size())
57  paramError("scalar_reporter_names",
58  "scalar_reporter_names and scalar_reporter_symbols must be the same size: Number "
59  "of scalar_reporter_names=",
60  scalar_reporter_names.size(),
61  "; Number of scalar_reporter_symbols=",
63  _scalar_reporter_data.resize(scalar_reporter_names.size());
64  for (const auto rep_index : index_range(_scalar_reporter_data))
65  _scalar_reporter_data[rep_index] =
66  &getReporterValueByName<Real>(scalar_reporter_names[rep_index], REPORTER_MODE_ROOT);
67  }
68 
69  // build reporters argument; order in derived classes must use this order
70  // first add vector reporter symbols
71  std::string symbol_str;
72  for (const auto i : index_range(_vector_reporter_symbols))
73  symbol_str += (symbol_str.empty() ? "" : ",") + _vector_reporter_symbols[i];
74  // next add scalar reporter symbols
75  for (const auto i : index_range(_scalar_reporter_symbols))
76  symbol_str += (symbol_str.empty() ? "" : ",") + _scalar_reporter_symbols[i];
77 
78  // add time if required
79  if (_use_t)
80  symbol_str += (symbol_str.empty() ? "" : ",") + std::string("t");
81 
82  // Create parsed function
83  _func_F = std::make_shared<SymFunction>();
85  getParam<std::string>("expression"),
86  symbol_str,
87  getParam<std::vector<std::string>>("constant_names"),
88  getParam<std::vector<std::string>>("constant_expressions"),
89  comm());
90 
91  // reserve storage for parameter passing buffer
93 }
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:439
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:388
Reporter object that has a single execution of the "execute" method for for each execute flag...
const ReporterMode REPORTER_MODE_ROOT
std::vector< const Real * > _scalar_reporter_data
input scalar reporter vectors
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
static InputParameters validParams()
SymFunctionPtr _func_F
function parser object
const std::vector< std::string > _scalar_reporter_symbols
static InputParameters validParams()
void parsedFunctionSetup(SymFunctionPtr &function, const std::string &expression, const std::string &variables, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions, const libMesh::Parallel::Communicator &comm) const
Performs setup steps on a SymFunction.
const std::vector< std::string > _vector_reporter_symbols
Get symbols to corresponding reporter names need symbols because reporter names have a "/" and that w...
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
const bool _use_t
whether time is part of the parsed expression
ParsedReporterBase(const InputParameters &parameters)
void addRequiredCustomTypeParam(const std::string &name, const std::string &custom_type, const std::string &doc_string)
These methods add an option parameter and with a customer type to the InputParameters object...
static InputParameters validParams()
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)
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.