https://mooseframework.inl.gov
ParsedPostprocessor.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 "ParsedPostprocessor.h"
11 
13 
16 {
19 
20  params.addRequiredCustomTypeParam<std::string>(
21  "function", "FunctionExpression", "function expression");
22  params.deprecateParam("function", "expression", "05/01/2025");
23 
24  params.addParam<std::vector<PostprocessorName>>("pp_names", {}, "Post-processors arguments");
25  params.addParam<std::vector<std::string>>(
26  "pp_symbols", {}, "Symbol associated with each post-processor argument");
27  params.addParam<std::vector<std::string>>(
28  "constant_names",
29  {},
30  "Vector of constants used in the parsed function (use this for kB etc.)");
31  params.addParam<std::vector<std::string>>(
32  "constant_expressions",
33  {},
34  "Vector of values for the constants in constant_names (can be an FParser expression)");
35  params.addParam<bool>(
36  "use_t", false, "Make time (t) variable available in the function expression.");
37 
38  params.addClassDescription("Computes a parsed expression with post-processors");
39  return params;
40 }
41 
43  : GeneralPostprocessor(parameters),
44  FunctionParserUtils(parameters),
45  _n_pp(coupledPostprocessors("pp_names")),
46  _use_t(getParam<bool>("use_t")),
47  _value(0.0)
48 {
49  // build postprocessors argument
50  std::string postprocessors;
51 
52  const std::vector<std::string> pp_symbols = getParam<std::vector<std::string>>("pp_symbols");
53  // sanity checks
54  if (!pp_symbols.empty() && (pp_symbols.size() != _n_pp))
55  paramError("pp_symbols", "pp_symbols must be the same length as pp_names.");
56 
57  // coupled postprocessors with capacity for symbol inputs
58  std::vector<PostprocessorName> pp_names = getParam<std::vector<PostprocessorName>>("pp_names");
59  if (pp_symbols.empty())
60  {
61  for (std::size_t i = 0; i < _n_pp; ++i)
62  postprocessors += (i == 0 ? "" : ",") + pp_names[i];
63  }
64  else
65  postprocessors = MooseUtils::stringJoin(pp_symbols, ",");
66 
67  // add time if required
68  if (_use_t)
69  postprocessors += (postprocessors.empty() ? "" : ",") + std::string("t");
70 
71  // Create parsed function
72  _func_F = std::make_shared<SymFunction>();
74  getParam<std::string>("expression"),
75  postprocessors,
76  getParam<std::vector<std::string>>("constant_names"),
77  getParam<std::vector<std::string>>("constant_expressions"),
78  comm());
79 
80  // reserve storage for parameter passing buffer
81  _func_params.resize(_n_pp + _use_t);
82  _pp_values.resize(_n_pp);
83  for (unsigned int i = 0; i < _n_pp; i++)
84  _pp_values[i] = &getPostprocessorValue("pp_names", i);
85 }
86 
87 void
89 {
90 }
91 
92 void
94 {
95 }
96 
97 void
99 {
100  for (unsigned int i = 0; i < _n_pp; i++)
101  _func_params[i] = *_pp_values[i];
102 
103  if (_use_t)
104  _func_params[_n_pp] = _t;
105 
107 }
108 
111 {
112  return _value;
113 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
std::vector< const PostprocessorValue * > _pp_values
values of the postprocessors part of the parsed expression
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
virtual void finalize() override final
This is called after execute() and after threadJoin()! This is probably where you want to do MPI comm...
This class is here to combine the Postprocessor interface and the base class Postprocessor object alo...
const PostprocessorValue & getPostprocessorValue(const std::string &param_name, const unsigned int index=0) const
doco-normal-methods-begin Retrieve the value of a Postprocessor or one of it&#39;s old or older values ...
registerMooseObject("MooseApp", ParsedPostprocessor)
const bool _use_t
whether time is part of the parsed expression
static InputParameters validParams()
const unsigned int _n_pp
number of postprocessors in parsed expression
void deprecateParam(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
Real _value
This post-processor value.
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:202
virtual void initialize() override final
Called before execute() is ever called so that data can be cleared.
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
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.
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 ...
virtual void execute() override final
Execute method.
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
SymFunctionPtr _func_F
function parser object for the resudual and on-diagonal Jacobian
Postprocessor that evaluates a parsed function expression.
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...
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...
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...
static InputParameters validParams()
std::string stringJoin(const std::vector< std::string > &values, const std::string &separator=" ")
Concatenates value into a single string separated by separator.
Definition: MooseUtils.C:1050
ParsedPostprocessor(const InputParameters &parameters)
virtual PostprocessorValue getValue() const override final
This will get called to actually grab the final value the postprocessor has calculated.