https://mooseframework.inl.gov
ParsedConvergence.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 "ParsedConvergence.h"
11 #include "MooseUtils.h"
12 #include "Function.h"
13 #include "FEProblemBase.h"
14 
16 
19 {
22 
23  params.addClassDescription("Evaluates convergence from a parsed expression.");
24 
25  params.addRequiredCustomTypeParam<std::string>(
26  "convergence_expression", "FunctionExpression", "Expression to parse for convergence");
27  params.addCustomTypeParam<std::string>(
28  "divergence_expression", "FunctionExpression", "Expression to parse for divergence");
29  params.addParam<std::vector<std::string>>(
30  "symbol_names", {}, "Symbol names to use in the parsed expressions");
31  params.addParam<std::vector<std::string>>(
32  "symbol_values",
33  {},
34  "Values (Convergence names, Postprocessor names, Function names, and constants) "
35  "corresponding to each entry in 'symbol_names'");
36 
37  return params;
38 }
39 
41  : Convergence(parameters),
42  FunctionParserUtils<false>(parameters),
43  _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
44  _symbol_names(getParam<std::vector<std::string>>("symbol_names")),
45  _symbol_values(getParam<std::vector<std::string>>("symbol_values")),
46  _convergence_function_params(_symbol_names.size(), 0.0),
47  _divergence_function_params(_symbol_names.size(), 0.0)
48 {
49  if (_symbol_names.size() != _symbol_values.size())
50  mooseError("The parameters 'symbol_names' and 'symbol_values' must have the same size.");
51 }
52 
53 void
55 {
57 
59 
60  const auto convergence_expression = getParam<std::string>("convergence_expression");
61  _convergence_function = makeParsedFunction(convergence_expression);
62 
63  const auto divergence_expression = isParamValid("divergence_expression")
64  ? getParam<std::string>("divergence_expression")
66  if (divergence_expression.size())
67  _divergence_function = makeParsedFunction(divergence_expression);
68 }
69 
70 void
72 {
73  for (const auto i : index_range(_symbol_values))
74  {
75  ReporterName reporter_name(_symbol_values[i], "value");
82  else
84  }
85 }
86 
87 void
89 {
91  _pp_values.push_back(&pp_value);
92  _pp_indices.push_back(i);
93 }
94 
95 void
97 {
99  _functions.push_back(&function);
100  _function_indices.push_back(i);
101 }
102 
103 void
105 {
107  _convergences.push_back(&convergence);
109  _convergence_indices.push_back(i);
110 }
111 
112 void
114 {
115  try
116  {
117  const Real value = MooseUtils::convert<Real>(_symbol_values[i], true);
120  }
121  catch (const std::invalid_argument & e)
122  {
123  mooseError(
124  "The 'symbol_values' entry '",
125  _symbol_values[i],
126  "' is not a constant value or the name of a Convergence, Postprocessor, or Function.",
127  e.what());
128  }
129 }
130 
132 ParsedConvergence::makeParsedFunction(const std::string & expression)
133 {
134  // Create parsed function
135  auto sym_function = std::make_shared<SymFunction>();
136  parsedFunctionSetup(sym_function,
137  expression,
139  {"pi", "e"},
140  {std::to_string(libMesh::pi), std::to_string(std::exp(Real(1)))},
141  comm());
142 
143  return sym_function;
144 }
145 
148 {
149  updateSymbolValues(iter);
150 
152  const Real diverged_real =
154  : 0;
155 
156  if (convertRealToBool(diverged_real, "divergence_expression"))
158  else if (convertRealToBool(converged_real, "convergence_expression"))
160  else
162 }
163 
164 void
166 {
170 }
171 
172 void
174 {
175  for (const auto i : index_range(_pp_indices))
176  {
179  }
180 }
181 
182 void
184 {
185  for (const auto i : index_range(_function_indices))
186  {
187  const Real function_value = _functions[i]->value(_t, Point(0, 0, 0));
188  _convergence_function_params[_function_indices[i]] = function_value;
189  _divergence_function_params[_function_indices[i]] = function_value;
190  }
191 }
192 
193 void
195 {
196  for (const auto i : index_range(_convergence_indices))
197  {
198  const auto status = _convergences[i]->checkConvergence(iter);
203  }
204 }
205 
206 bool
207 ParsedConvergence::convertRealToBool(Real value, const std::string & param) const
208 {
210  return true;
211  else if (MooseUtils::absoluteFuzzyEqual(value, 0.0))
212  return false;
213  else
214  mooseError("The expression parameter '",
215  param,
216  "' evaluated to the value ",
217  value,
218  ", but it must only evaluate to either 0 or 1.");
219 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
Python-like join function for strings over an iterator range.
Definition: MooseUtils.h:142
THREAD_ID _tid
Thread ID.
Definition: Convergence.h:80
Base class for function objects.
Definition: Function.h:36
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
Definition: Convergence.h:45
std::shared_ptr< SymFunction > SymFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:380
auto exp(const T &)
std::vector< unsigned int > _function_indices
std::vector< std::string > _convergence_symbol_names
std::vector< Real > _convergence_function_params
Convergence function parameters.
static InputParameters validParams()
void addCustomTypeParam(const std::string &name, const T &value, const std::string &custom_type, const std::string &doc_string)
SymFunctionPtr _divergence_function
Parsed function for divergence.
void updateSymbolValues(unsigned int iter)
Updates non-constant symbol values.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
Evaluates convergence from a parsed expression.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
void updatePostprocessorSymbolValues()
bool convertRealToBool(Real value, const std::string &param) const
Converts a Real value to a bool.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
void initializeFunctionSymbol(unsigned int i)
MPI_Status status
void initializePostprocessorSymbol(unsigned int i)
virtual Function & getFunction(const std::string &name, const THREAD_ID tid=0)
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:99
const ReporterData & getReporterData() const
Provides const access the ReporterData object.
std::vector< unsigned int > _pp_indices
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Base class for convergence criteria.
Definition: Convergence.h:21
virtual Convergence & getConvergence(const std::string &name, const THREAD_ID tid=0) const
Gets a Convergence object.
void updateConvergenceSymbolValues(unsigned int iter)
ParsedConvergence(const InputParameters &parameters)
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:202
registerMooseObject("MooseApp", ParsedConvergence)
virtual bool hasConvergence(const std::string &name, const THREAD_ID tid=0) const
Returns true if the problem has a Convergence object of the given name.
std::vector< const PostprocessorValue * > _pp_values
Post-processor values in the provided symbols.
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.
std::vector< unsigned int > _convergence_indices
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name, std::size_t t_index=0) const
Get a read-only reference to the value associated with a Postprocessor that exists.
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition: Convergence.h:33
std::vector< std::string > _symbol_values
Corresponding symbol values (Convergence, Function, Postprocessor, or constant)
std::vector< Convergence * > _convergences
Convergences in the provided symbols.
virtual MooseConvergenceStatus checkConvergence(unsigned int iter) override
Returns convergence status.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void initializeSymbols()
Initializes symbols used in the parsed expression.
SymFunctionPtr makeParsedFunction(const std::string &expression)
Makes a parsed function.
static InputParameters validParams()
Definition: Convergence.C:16
void initializeConstantSymbol(unsigned int i)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:267
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 initializeConvergenceSymbol(unsigned int i)
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...
bool hasReporterValue(const ReporterName &reporter_name) const
Return True if a Reporter value with the given type and name have been created.
Definition: ReporterData.h:445
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:195
std::vector< Real > _divergence_function_params
Divergence function parameters.
FEProblemBase & _fe_problem
virtual bool hasFunction(const std::string &name, const THREAD_ID tid=0)
std::vector< Function * > _functions
Functions in the provided symbols.
auto index_range(const T &sizable)
The Reporter system is comprised of objects that can contain any number of data values.
Definition: ReporterName.h:30
SymFunctionPtr _convergence_function
Parsed function for convergence.
std::vector< std::string > _symbol_names
User-defined symbols to use in parsed expression.
const Real pi