https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
53void
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")
65 : MooseUtils::join(_convergence_symbol_names, "|");
66 if (divergence_expression.size())
67 _divergence_function = makeParsedFunction(divergence_expression);
68}
69
70void
86
87void
89{
91 _pp_values.push_back(&pp_value);
92 _pp_indices.push_back(i);
93}
94
95void
97{
99 _functions.push_back(&function);
100 _function_indices.push_back(i);
101}
102
103void
105{
107 _convergences.push_back(&convergence);
109 _convergence_indices.push_back(i);
110}
111
112void
114{
115 try
116 {
117 const Real value = MooseUtils::convert<Real>(_symbol_values[i], true);
120 }
121 catch (const std::invalid_argument & e)
122 {
124 "The 'symbol_values' entry '",
126 "' is not a constant value or the name of a Convergence, Postprocessor, or Function.",
127 e.what());
128 }
129}
130
132ParsedConvergence::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(n_iter);
150
151 const Real converged_real = evaluate(_convergence_function, _convergence_function_params, name());
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
164void
171
172void
181
182void
184{
185 for (const auto i : index_range(_function_indices))
186 {
187 const Real function_value = _functions[i]->value(_t, Point(0, 0, 0));
190 }
191}
192
193void
195{
196 for (const auto i : index_range(_convergence_indices))
197 {
198 const auto status = _convergences[i]->checkConvergence(n_iter);
203 }
204}
205
206bool
207ParsedConvergence::convertRealToBool(Real value, const std::string & param) const
208{
209 if (MooseUtils::absoluteFuzzyEqual(value, 1.0))
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}
Real PostprocessorValue
various MOOSE typedefs
Definition MooseTypes.h:230
registerMooseObject("MooseApp", ParsedConvergence)
Base class for convergence criteria.
Definition Convergence.h:26
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
THREAD_ID _tid
Thread ID.
Definition Convergence.h:83
static InputParameters validParams()
Definition Convergence.C:16
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition Convergence.h:34
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
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.
const ReporterData & getReporterData() const
Provides const access the ReporterData object.
virtual Convergence & getConvergence(const std::string &name, const THREAD_ID tid=0) const
Gets a Convergence object.
virtual bool hasFunction(const std::string &name, const THREAD_ID tid=0)
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.
virtual Function & getFunction(const std::string &name, const THREAD_ID tid=0)
std::shared_ptr< SymFunction > SymFunctionPtr
Shorthand for an smart pointer to an autodiff function parser 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.
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
static InputParameters validParams()
Base class for function objects.
Definition Function.h:30
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addCustomTypeParam(const std::string &name, const T &value, const std::string &custom_type, const std::string &doc_string)
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.
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.
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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:271
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
Evaluates convergence from a parsed expression.
bool convertRealToBool(Real value, const std::string &param) const
Converts a Real value to a bool.
std::vector< unsigned int > _pp_indices
void updatePostprocessorSymbolValues()
std::vector< const PostprocessorValue * > _pp_values
Post-processor values in the provided symbols.
void initializeFunctionSymbol(unsigned int i)
std::vector< std::string > _symbol_names
User-defined symbols to use in parsed expression.
std::vector< Convergence * > _convergences
Convergences in the provided symbols.
std::vector< unsigned int > _function_indices
std::vector< Real > _convergence_function_params
Convergence function parameters.
SymFunctionPtr _divergence_function
Parsed function for divergence.
virtual MooseConvergenceStatus checkConvergence(unsigned int n_iter) override
Returns convergence status.
void initializePostprocessorSymbol(unsigned int i)
std::vector< std::string > _convergence_symbol_names
void updateConvergenceSymbolValues(unsigned int n_iter)
std::vector< unsigned int > _convergence_indices
void updateSymbolValues(unsigned int n_iter)
Updates non-constant symbol values.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
std::vector< Function * > _functions
Functions in the provided symbols.
void initializeConstantSymbol(unsigned int i)
ParsedConvergence(const InputParameters &parameters)
SymFunctionPtr _convergence_function
Parsed function for convergence.
void initializeSymbols()
Initializes symbols used in the parsed expression.
void initializeConvergenceSymbol(unsigned int i)
std::vector< Real > _divergence_function_params
Divergence function parameters.
SymFunctionPtr makeParsedFunction(const std::string &expression)
Makes a parsed function.
FEProblemBase & _fe_problem
static InputParameters validParams()
std::vector< std::string > _symbol_values
Corresponding symbol values (Convergence, Function, Postprocessor, or constant)
bool hasReporterValue(const ReporterName &reporter_name) const
Return True if a Reporter value with the given type and name have been created.
The Reporter system is comprised of objects that can contain any number of data values.
const Parallel::Communicator & comm() const
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64
const Real pi