Nektar++
|
Concrete implementation of the interface defined in Interpreter. More...
Classes | |
class | AnalyticExpression |
struct | CopyState |
struct | EvalAbs |
struct | EvalAcos |
struct | EvalAng |
struct | EvalAsin |
struct | EvalAtan |
struct | EvalAtan2 |
struct | EvalAWGN |
struct | EvalBessel |
struct | EvalCeil |
struct | EvalCos |
struct | EvalCosh |
struct | EvalDiv |
struct | EvalExp |
struct | EvalFabs |
struct | EvalFloor |
struct | EvalFmod |
struct | EvalLog |
struct | EvalLog10 |
struct | EvalLogicalEqual |
struct | EvalLogicalGeq |
struct | EvalLogicalGreater |
struct | EvalLogicalLeq |
struct | EvalLogicalLess |
struct | EvalMax |
struct | EvalMin |
struct | EvalMod |
struct | EvalMul |
struct | EvalNeg |
struct | EvalPow |
struct | EvalRad |
struct | EvalSign |
struct | EvalSin |
struct | EvalSinh |
struct | EvalSqrt |
struct | EvalSub |
struct | EvalSum |
struct | EvalTan |
struct | EvalTanh |
struct | EvaluationStep |
Function objects (functors) More... | |
struct | StoreConst |
struct | StorePrm |
struct | StoreVar |
Public Types | |
typedef std::map< std::string, int > | VariableMap |
typedef std::map< std::string, int > | ConstantMap |
typedef std::map< std::string, int > | ParameterMap |
typedef std::map< std::string, int > | ExpressionMap |
typedef std::map< std::string, int > | FunctionNameMap |
typedef std::vector< EvaluationStep * > | ExecutionStack |
typedef std::pair< bool, NekDouble > | PrecomputedValue |
typedef NekDouble(* | OneArgFunc) (NekDouble) |
typedef NekDouble(* | TwoArgFunc) (NekDouble, NekDouble) |
typedef bsp::tree_parse_info< std::string::const_iterator, bsp::node_val_data_factory< NekDouble > > | ParsedTreeInfo |
typedef bsp::tree_match< std::string::const_iterator, bsp::node_val_data_factory< NekDouble > >::tree_iterator | ParsedTreeIterator |
typedef std::vector< const Array< OneD, const NekDouble > * > | VariableArray |
Public Member Functions | |
ExpressionEvaluator () | |
Initializes the evaluator. More... | |
~ExpressionEvaluator (void) | |
Destructor that removes all entries from the execution stack. More... | |
void | SetRandomSeed (unsigned int seed) |
Sets the random seed for the pseudorandom number generator. More... | |
void | AddConstants (std::map< std::string, NekDouble > const &constants) |
Set constants to be evaluated. More... | |
int | AddConstant (std::string const &name, NekDouble value) |
Set constants to be evaluated. More... | |
NekDouble | GetConstant (std::string const &name) |
Return the value of a constant. More... | |
void | SetParameters (std::map< std::string, NekDouble > const ¶ms) |
Set parameter values. More... | |
void | SetParameter (std::string const &name, NekDouble value) |
Set parameter values. More... | |
NekDouble | GetParameter (std::string const &name) |
Get the value of a parameter. More... | |
NekDouble | GetTime () const |
Returns the total walltime spent in evaluation procedures in seconds. More... | |
int | DefineFunction (const std::string &vlist, const std::string &expr) |
Defines a function for the purposes of evaluation. More... | |
NekDouble | Evaluate (const int id) |
Evaluate a function which depends only on constants and/or parameters. More... | |
NekDouble | Evaluate (const int id, const NekDouble x, const NekDouble y, const NekDouble z, const NekDouble t) |
Evaluate a function which depends only on constants and/or parameters. More... | |
NekDouble | EvaluateAtPoint (const int id, const std::vector< NekDouble > point) |
Evaluate a function which depends on zero or more variables. More... | |
void | Evaluate (const int id, const Array< OneD, const NekDouble > &x, const Array< OneD, const NekDouble > &y, const Array< OneD, const NekDouble > &z, const Array< OneD, const NekDouble > &t, Array< OneD, NekDouble > &result) |
Evaluate a function which depends only on constants and/or parameters. More... | |
void | Evaluate (const int id, const std::vector< Array< OneD, const NekDouble > > &points, Array< OneD, NekDouble > &result) |
Evaluate a function which depends only on constants and/or parameters. More... | |
PrecomputedValue | PrepareExecutionAsYouParse (const ParsedTreeIterator &location, ExecutionStack &stack, VariableMap &variableMap, int stateIndex) |
Prepares an execution stack for the evaluation of a function. More... | |
Public Attributes | |
bsp::symbols< NekDouble > | m_constantsParser |
Private Types | |
enum | EvaluationStepType { E_ABS , E_ASIN , E_ACOS , E_ATAN , E_ATAN2 , E_ANG , E_CEIL , E_COS , E_COSH , E_EXP , E_FABS , E_FLOOR , E_FMOD , E_LOG , E_LOG10 , E_MAX , E_MIN , E_POW , E_RAD , E_SIN , E_SINH , E_SQRT , E_TAN , E_TANH , E_SIGN , E_AWGN , E_BESSEL } |
typedef std::vector< NekDouble > & | vr |
Short names to minimise the infractructural code mess in defining functors below. More... | |
typedef const std::vector< NekDouble > & | cvr |
typedef const int | ci |
typedef std::mt19937 & | rgt |
Private Member Functions | |
template<typename StepType > | |
EvaluationStep * | makeStep (ci dest, ci src_left=0, ci src_right=0) |
Factory method which makes code little less messy. More... | |
Private Attributes | |
ExpressionMap | m_parsedMapExprToExecStackId |
These vector and map store pre-processed evaluation sequences for the analytic expressions. Each ExecutionStack is an ordered container of steps of sequential execution process which evaluates an analytic expression. More... | |
std::vector< ExecutionStack > | m_executionStack |
std::vector< VariableMap > | m_stackVariableMap |
Keeping map of variables individually per each analytic expression allows correctly handling expressions which depend on different number of variables. More... | |
ParameterMap | m_parameterMapNameToId |
The following data structures hold input data to be used on evaluation stage. There are three types of input data: More... | |
ConstantMap | m_constantMapNameToId |
VariableMap | m_expressionVariableMap |
std::vector< NekDouble > | m_parameter |
std::vector< NekDouble > | m_constant |
std::vector< NekDouble > | m_variable |
std::vector< NekDouble > | m_state |
This vector stores the execution state (memory) used by the sequential execution process. More... | |
std::vector< int > | m_state_sizes |
Vector of state sizes per each. More... | |
int | m_state_size |
This counter is used by PrepareExecutionAsYouParse for finding the minimal state size necessary for evaluation of function parsed. More... | |
Timer | m_timer |
Timer and sum of evaluation times. More... | |
NekDouble | m_total_eval_time |
std::mt19937 | m_generator |
FunctionNameMap | m_functionMapNameToInstanceType |
std::map< int, OneArgFunc > | m_function |
std::map< int, TwoArgFunc > | m_function2 |
Concrete implementation of the interface defined in Interpreter.
Definition at line 156 of file Interpreter/Interpreter.cpp.
|
private |
Definition at line 1327 of file Interpreter/Interpreter.cpp.
typedef std::map<std::string, int> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::ConstantMap |
Definition at line 163 of file Interpreter/Interpreter.cpp.
|
private |
Definition at line 1326 of file Interpreter/Interpreter.cpp.
typedef std::vector<EvaluationStep *> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::ExecutionStack |
Definition at line 167 of file Interpreter/Interpreter.cpp.
typedef std::map<std::string, int> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::ExpressionMap |
Definition at line 165 of file Interpreter/Interpreter.cpp.
typedef std::map<std::string, int> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::FunctionNameMap |
Definition at line 166 of file Interpreter/Interpreter.cpp.
Definition at line 169 of file Interpreter/Interpreter.cpp.
typedef std::map<std::string, int> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::ParameterMap |
Definition at line 164 of file Interpreter/Interpreter.cpp.
typedef bsp::tree_parse_info<std::string::const_iterator, bsp::node_val_data_factory<NekDouble> > Nektar::LibUtilities::Interpreter::ExpressionEvaluator::ParsedTreeInfo |
Definition at line 174 of file Interpreter/Interpreter.cpp.
typedef bsp::tree_match<std::string::const_iterator,bsp::node_val_data_factory<NekDouble>>::tree_iterator Nektar::LibUtilities::Interpreter::ExpressionEvaluator::ParsedTreeIterator |
Definition at line 177 of file Interpreter/Interpreter.cpp.
typedef std::pair<bool, NekDouble> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::PrecomputedValue |
Definition at line 168 of file Interpreter/Interpreter.cpp.
|
private |
Definition at line 1328 of file Interpreter/Interpreter.cpp.
typedef NekDouble(* Nektar::LibUtilities::Interpreter::ExpressionEvaluator::TwoArgFunc) (NekDouble, NekDouble) |
Definition at line 170 of file Interpreter/Interpreter.cpp.
typedef std::vector<const Array<OneD, const NekDouble> *> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::VariableArray |
Definition at line 179 of file Interpreter/Interpreter.cpp.
typedef std::map<std::string, int> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::VariableMap |
Definition at line 162 of file Interpreter/Interpreter.cpp.
|
private |
Short names to minimise the infractructural code mess in defining functors below.
Definition at line 1325 of file Interpreter/Interpreter.cpp.
Enumerator | |
---|---|
E_ABS | |
E_ASIN | |
E_ACOS | |
E_ATAN | |
E_ATAN2 | |
E_ANG | |
E_CEIL | |
E_COS | |
E_COSH | |
E_EXP | |
E_FABS | |
E_FLOOR | |
E_FMOD | |
E_LOG | |
E_LOG10 | |
E_MAX | |
E_MIN | |
E_POW | |
E_RAD | |
E_SIN | |
E_SINH | |
E_SQRT | |
E_TAN | |
E_TANH | |
E_SIGN | |
E_AWGN | |
E_BESSEL |
Definition at line 1338 of file Interpreter/Interpreter.cpp.
|
inline |
Initializes the evaluator.
This routine will initialize the evaluator with some basic default constants,
Definition at line 187 of file Interpreter/Interpreter.cpp.
References tinysimd::abs(), AddConstant(), Nektar::LibUtilities::ang(), E_ABS, E_ACOS, E_ANG, E_ASIN, E_ATAN, E_ATAN2, E_AWGN, E_BESSEL, E_CEIL, E_COS, E_COSH, E_EXP, E_FABS, E_FLOOR, E_FMOD, E_LOG, E_LOG10, E_MAX, E_MIN, E_RAD, E_SIGN, E_SIN, E_SINH, E_SQRT, E_TAN, E_TANH, tinysimd::log(), m_function, m_function2, m_functionMapNameToInstanceType, m_state_size, Nektar::LibUtilities::rad(), Nektar::LibUtilities::sign(), and tinysimd::sqrt().
|
inline |
Destructor that removes all entries from the execution stack.
Definition at line 274 of file Interpreter/Interpreter.cpp.
References m_executionStack.
|
inline |
Set constants to be evaluated.
This function behaves in the same way as AddConstants, but it only adds one constant at a time. If the constant existed previously, an exception will be thrown stating the fact. If it did not exist previously, it will be added to the global constants and will be used the next time DefineFunction is called.
Definition at line 309 of file Interpreter/Interpreter.cpp.
References m_constant, m_constantMapNameToId, m_constantsParser, and CellMLToNektar.pycml::name.
Referenced by AddConstants(), DefineFunction(), ExpressionEvaluator(), and PrepareExecutionAsYouParse().
|
inline |
Set constants to be evaluated.
Constants are evaluated and inserted into the function at the time it is parsed when calling the DefineFunction function. After parsing, if a constant is changed, it will not be reflected in the function when Evaluate is called. This also means that if a function with an unknown constant is added, and then the constant is added, the function will not see the added constant and through an exception.
This function will add all of the constants in the constants
parameter to the global internal constants. If a constant was already loaded previously, it will throw an exception stating which constants in the map had this issue. It will add all of the constants it can from constants
and output the constants it couldn't add in the string exception.
constants | A std::map with string names for the constants (which will be evalauted in the expression) and their NekDouble value. |
Definition at line 298 of file Interpreter/Interpreter.cpp.
References AddConstant().
|
inline |
Defines a function for the purposes of evaluation.
This function allows one to define a function to evaluate. The vlist
argument should define a list of space-separated variables that the expression defined in function
is dependent upon. For example, if function
is defined as the string x + y
, then vlist should most likely be x y
, unless you are defining x
or y
as parameters with the SetParameters function.
vlist | List of variable names separated with spaces. |
expr | String definition of the function to be evaluated. |
Definition at line 397 of file Interpreter/Interpreter.cpp.
References AddConstant(), ASSERTL1, m_constantsParser, m_executionStack, m_parsedMapExprToExecStackId, m_stackVariableMap, m_state_size, m_state_sizes, and PrepareExecutionAsYouParse().
|
inline |
Evaluate a function which depends only on constants and/or parameters.
id | The ID returned from DefineFunction representing the function to be evaluated. |
Definition at line 492 of file Interpreter/Interpreter.cpp.
References ASSERTL1, m_executionStack, m_state, m_state_sizes, m_timer, m_total_eval_time, Nektar::LibUtilities::Timer::Start(), Nektar::LibUtilities::Timer::Stop(), and Nektar::LibUtilities::Timer::TimePerTest().
Referenced by Evaluate().
|
inline |
Evaluate a function which depends only on constants and/or parameters.
id | The ID returned from DefineFunction representing the function to be evaluated. |
Definition at line 599 of file Interpreter/Interpreter.cpp.
References Evaluate(), and Nektar::UnitTests::z().
|
inline |
Evaluate a function which depends only on constants and/or parameters.
id | The ID returned from DefineFunction representing the function to be evaluated. |
Definition at line 517 of file Interpreter/Interpreter.cpp.
References ASSERTL1, m_executionStack, m_state, m_state_sizes, m_timer, m_total_eval_time, m_variable, Nektar::LibUtilities::Timer::Start(), Nektar::LibUtilities::Timer::Stop(), Nektar::LibUtilities::Timer::TimePerTest(), and Nektar::UnitTests::z().
|
inline |
Evaluate a function which depends only on constants and/or parameters.
id | The ID returned from DefineFunction representing the function to be evaluated. |
If number of points tends to 10^6, one may end up with up to ~0.5Gb data allocated for m_state only. Lets split the work into cache-sized chunks. Ahtung, magic constant!
Definition at line 612 of file Interpreter/Interpreter.cpp.
References ASSERTL1, m_executionStack, m_state, m_state_sizes, m_timer, m_total_eval_time, m_variable, Nektar::LibUtilities::Timer::Start(), Nektar::LibUtilities::Timer::Stop(), and Nektar::LibUtilities::Timer::TimePerTest().
|
inline |
Evaluate a function which depends on zero or more variables.
This is suitable for expressions depending on more than 4 variables or for the dynamic setting of some variables as parameters (there is currently no interface method for removing a variable from parameter map however).
id | The ID returned from DefineFunction representing the function to be evaluated. |
point | A std::vector of points to be evaluated. |
Definition at line 559 of file Interpreter/Interpreter.cpp.
References ASSERTL1, m_executionStack, m_stackVariableMap, m_state, m_state_sizes, m_timer, m_total_eval_time, m_variable, Nektar::LibUtilities::Timer::Start(), Nektar::LibUtilities::Timer::Stop(), and Nektar::LibUtilities::Timer::TimePerTest().
|
inline |
Return the value of a constant.
If a constant with the specified name name
exists, this function returns the NekDouble value that the constant stores. If the constant doesn't exist, this throws an exception.
name | Name of constant to return. |
Definition at line 339 of file Interpreter/Interpreter.cpp.
References ASSERTL1, Nektar::StdRegions::find(), m_constantsParser, and CellMLToNektar.pycml::name.
|
inline |
Get the value of a parameter.
If a parameter with the specified name
exists, it returns the NekDouble value that the parameter stores. If the parameter doesn't exist, it throws an exception.
name | Name of the parameter to query. |
Definition at line 378 of file Interpreter/Interpreter.cpp.
References ASSERTL1, m_parameter, m_parameterMapNameToId, and CellMLToNektar.pycml::name.
|
inline |
Returns the total walltime spent in evaluation procedures in seconds.
Definition at line 389 of file Interpreter/Interpreter.cpp.
References m_total_eval_time.
|
inlineprivate |
Factory method which makes code little less messy.
Definition at line 1332 of file Interpreter/Interpreter.cpp.
References m_constant, m_generator, m_parameter, m_state, and m_variable.
|
inline |
Prepares an execution stack for the evaluation of a function.
This method prepares the execution stack (an ordered sequence of operators that perform the evaluation) for the parsed evaluation tree. In order to do this, it unrolls the binary tree representing the recursive evaluation into an ordered sequence of commands. That ordered sequence of commands is equivalent to a bottom-up walk up the evaluation tree, but this allows not to form tree explicitly.
This approach requires to introduce explicitly an execution state (memory) shared by commands in the evaluation sequence: recursively dependent commands need to pass data between each other. Such state for the recursive evaluation is passed via return values of a recursive evaluation function — which is bad if one wants to implement vectorized evaluator.
On the other hand, to run through a sequential container of functors is faster than to walk the tree and at each node to check the node type.
root | Iterator generated by boost::spirit. |
stack | Initially empty sequential container of evaluation steps. |
varMap | Maps variable names to their ids. |
stateIndex | An index in the state[] array where an evaluation step corresponding to the current tree node is allowed to write. |
(true, value)
if all sub-tree down the current node evaluates to constant, or flags the opposite via (false, 0)
. Definition at line 712 of file Interpreter/Interpreter.cpp.
References AddConstant(), ASSERTL0, ASSERTL1, Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::constantID, E_ABS, E_ACOS, E_ANG, E_ASIN, E_ATAN, E_ATAN2, E_AWGN, E_BESSEL, E_CEIL, E_COS, E_COSH, E_EXP, E_FABS, E_FLOOR, E_FMOD, E_LOG, E_LOG10, E_MAX, E_MIN, E_RAD, E_SIGN, E_SIN, E_SINH, E_SQRT, E_TAN, E_TANH, Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::functionID, m_constant, m_constantMapNameToId, m_function, m_function2, m_functionMapNameToInstanceType, m_parameterMapNameToId, m_state_size, Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::numberID, Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::operatorID, Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::parameterID, PrepareExecutionAsYouParse(), Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::unaryID, and Nektar::LibUtilities::Interpreter::ExpressionEvaluator::AnalyticExpression::variableID.
Referenced by DefineFunction(), and PrepareExecutionAsYouParse().
|
inline |
Set parameter values.
This function behaves in the same way as SetParameters, but it only adds one parameter and it does not delete the others. If the parameter name
existed previously, it will be overridden and replaced with the new value. If it did not exist previously, it will be added to the current parameters.
name | Name of the parameter to define. |
value | The parameter's value. |
Definition at line 360 of file Interpreter/Interpreter.cpp.
References m_parameter, m_parameterMapNameToId, and CellMLToNektar.pycml::name.
Referenced by SetParameters().
|
inline |
Set parameter values.
Parameters are functionally similar to constants, but they are inserted into the function at the time that Evaluate is called, instead of when the function is parsed. This function can therefore be called at any time, and it will take effect in the next call to Evaluate. This function will delete all of the parameters, and replace all of them with only the ones in the map argument.
Definition at line 349 of file Interpreter/Interpreter.cpp.
References SetParameter().
|
inline |
Sets the random seed for the pseudorandom number generator.
This allows for e.g. different ranks to be given different seeds to ensure appropriate entropy in noise generation.
Definition at line 290 of file Interpreter/Interpreter.cpp.
References m_generator.
|
private |
Definition at line 1291 of file Interpreter/Interpreter.cpp.
Referenced by AddConstant(), makeStep(), and PrepareExecutionAsYouParse().
|
private |
Definition at line 1287 of file Interpreter/Interpreter.cpp.
Referenced by AddConstant(), and PrepareExecutionAsYouParse().
bsp::symbols<NekDouble> Nektar::LibUtilities::Interpreter::ExpressionEvaluator::m_constantsParser |
This is a parser for spirit that parses the CONSTANT values. The default constants are those that are in math.h without the M_ prefix and they are initialized in the AnalyticExpressionEvaluator constructor.
Definition at line 1100 of file Interpreter/Interpreter.cpp.
Referenced by AddConstant(), DefineFunction(), and GetConstant().
|
private |
Definition at line 1261 of file Interpreter/Interpreter.cpp.
Referenced by DefineFunction(), Evaluate(), EvaluateAtPoint(), and ~ExpressionEvaluator().
|
private |
Definition at line 1288 of file Interpreter/Interpreter.cpp.
|
private |
Definition at line 1316 of file Interpreter/Interpreter.cpp.
Referenced by ExpressionEvaluator(), and PrepareExecutionAsYouParse().
|
private |
Definition at line 1317 of file Interpreter/Interpreter.cpp.
Referenced by ExpressionEvaluator(), and PrepareExecutionAsYouParse().
|
private |
Definition at line 1315 of file Interpreter/Interpreter.cpp.
Referenced by ExpressionEvaluator(), and PrepareExecutionAsYouParse().
|
private |
Definition at line 1309 of file Interpreter/Interpreter.cpp.
Referenced by makeStep(), and SetRandomSeed().
|
private |
Definition at line 1290 of file Interpreter/Interpreter.cpp.
Referenced by GetParameter(), makeStep(), and SetParameter().
|
private |
The following data structures hold input data to be used on evaluation stage. There are three types of input data:
Definition at line 1286 of file Interpreter/Interpreter.cpp.
Referenced by GetParameter(), PrepareExecutionAsYouParse(), and SetParameter().
|
private |
These vector and map store pre-processed evaluation sequences for the analytic expressions. Each ExecutionStack is an ordered container of steps of sequential execution process which evaluates an analytic expression.
Definition at line 1260 of file Interpreter/Interpreter.cpp.
Referenced by DefineFunction().
|
private |
Keeping map of variables individually per each analytic expression allows correctly handling expressions which depend on different number of variables.
Definition at line 1267 of file Interpreter/Interpreter.cpp.
Referenced by DefineFunction(), and EvaluateAtPoint().
|
private |
This vector stores the execution state (memory) used by the sequential execution process.
Definition at line 1296 of file Interpreter/Interpreter.cpp.
Referenced by Evaluate(), EvaluateAtPoint(), and makeStep().
|
private |
This counter is used by PrepareExecutionAsYouParse for finding the minimal state size necessary for evaluation of function parsed.
Definition at line 1303 of file Interpreter/Interpreter.cpp.
Referenced by DefineFunction(), ExpressionEvaluator(), and PrepareExecutionAsYouParse().
|
private |
Vector of state sizes per each.
Definition at line 1299 of file Interpreter/Interpreter.cpp.
Referenced by DefineFunction(), Evaluate(), and EvaluateAtPoint().
|
private |
Timer and sum of evaluation times.
Definition at line 1306 of file Interpreter/Interpreter.cpp.
Referenced by Evaluate(), and EvaluateAtPoint().
|
private |
Definition at line 1307 of file Interpreter/Interpreter.cpp.
Referenced by Evaluate(), EvaluateAtPoint(), and GetTime().
|
private |
Definition at line 1292 of file Interpreter/Interpreter.cpp.
Referenced by Evaluate(), EvaluateAtPoint(), and makeStep().