59 std::shared_ptr<peg::Ast>
_ast;
176 void build(
const peg::Ast & ast);
190 const std::vector<Instruction> &
getRPN()
const {
return _rpn; }
213 unsigned int addVariable(
const std::string & name);
330 [[noreturn]]
void builderError(
const peg::Ast & ast,
const std::string & message)
const;
369 KOKKOS_FUNCTION
Real eval(
const Real t,
371 const unsigned int qp = 0,
372 Datum * datum =
nullptr)
const;
456 #define KOKKOS_FPARSER_COMPARE(code, op, epsilon) \ 458 stack[head - 2] = stack[head - 2] - stack[head - 1] op epsilon; \ 462 #define KOKKOS_FPARSER_BINARY(code, op) \ 464 stack[head - 2] = stack[head - 2] op stack[head - 1]; \ 468 #define KOKKOS_FPARSER_FUNCTION_1(code, func) \ 470 stack[head - 1] = ::Kokkos::func(stack[head - 1]); \ 473 #define KOKKOS_FPARSER_FUNCTION_2(code, func) \ 475 stack[head - 2] = ::Kokkos::func(stack[head - 2], stack[head - 1]); \ 479 #define KOKKOS_FPARSER_FUNCTION_INV_1(code, func) \ 481 stack[head - 1] = 1.0 / ::Kokkos::func(stack[head - 1]); \ 484 KOKKOS_FUNCTION
inline Real 490 unsigned int head = 0;
492 for (
unsigned int pos = 0; pos <
_rpn.size(); ++pos)
496 const auto inst =
_rpn[pos];
498 switch (
_rpn[pos].op)
506 stack[head] = p(inst.arg);
507 else if (inst.arg == 3)
521 KOKKOS_ASSERT(datum);
528 KOKKOS_ASSERT(datum);
541 stack[head - 1] = -stack[head - 1];
563 stack[head - 3] =
::Kokkos::round(stack[head - 3]) ? stack[head - 2] : stack[head - 1];
566 KOKKOS_FPARSER_COMPARE(LT, <, -
_epsilon);
567 KOKKOS_FPARSER_COMPARE(LEQ, <=,
_epsilon);
568 KOKKOS_FPARSER_COMPARE(GT, >,
_epsilon);
569 KOKKOS_FPARSER_COMPARE(GEQ, >=, -
_epsilon);
570 KOKKOS_FPARSER_BINARY(ADD, +);
571 KOKKOS_FPARSER_BINARY(SUB, -);
572 KOKKOS_FPARSER_BINARY(MUL, *);
573 KOKKOS_FPARSER_BINARY(DIV, /);
574 KOKKOS_FPARSER_FUNCTION_1(ABS,
abs);
575 KOKKOS_FPARSER_FUNCTION_1(ACOS, acos);
576 KOKKOS_FPARSER_FUNCTION_1(ACOSH, acosh);
577 KOKKOS_FPARSER_FUNCTION_1(ASIN, asin);
578 KOKKOS_FPARSER_FUNCTION_1(ASINH, asinh);
579 KOKKOS_FPARSER_FUNCTION_1(ATAN, atan);
580 KOKKOS_FPARSER_FUNCTION_2(ATAN2, atan2);
581 KOKKOS_FPARSER_FUNCTION_1(ATANH, atanh);
582 KOKKOS_FPARSER_FUNCTION_1(CBRT, cbrt);
583 KOKKOS_FPARSER_FUNCTION_1(CEIL, ceil);
584 KOKKOS_FPARSER_FUNCTION_1(COS,
cos);
585 KOKKOS_FPARSER_FUNCTION_1(COSH,
cosh);
586 KOKKOS_FPARSER_FUNCTION_INV_1(COT, tan);
587 KOKKOS_FPARSER_FUNCTION_INV_1(CSC,
sin);
588 KOKKOS_FPARSER_FUNCTION_1(EXP,
exp);
589 KOKKOS_FPARSER_FUNCTION_1(EXP2, exp2);
590 KOKKOS_FPARSER_FUNCTION_1(FLOOR, floor);
591 KOKKOS_FPARSER_FUNCTION_2(HYPOT, hypot);
592 KOKKOS_FPARSER_FUNCTION_1(INT,
round);
593 KOKKOS_FPARSER_FUNCTION_1(LOG,
log);
594 KOKKOS_FPARSER_FUNCTION_1(LOG2, log2);
595 KOKKOS_FPARSER_FUNCTION_1(LOG10, log10);
596 KOKKOS_FPARSER_FUNCTION_2(MAX,
max);
597 KOKKOS_FPARSER_FUNCTION_2(MIN,
min);
598 KOKKOS_FPARSER_FUNCTION_2(POW,
pow);
599 KOKKOS_FPARSER_FUNCTION_INV_1(SEC,
cos);
600 KOKKOS_FPARSER_FUNCTION_1(SIN,
sin);
601 KOKKOS_FPARSER_FUNCTION_1(SINH,
sinh);
602 KOKKOS_FPARSER_FUNCTION_1(SQRT,
sqrt);
603 KOKKOS_FPARSER_FUNCTION_1(TAN, tan);
604 KOKKOS_FPARSER_FUNCTION_1(TANH, tanh);
605 KOKKOS_FPARSER_FUNCTION_1(TRUNC, trunc);
609 KOKKOS_ASSERT(head == 1);
611 return stack[head - 1];
void associateFunction(const std::string &name, const Function *function)
Associate a variable with a function.
PEGParser(const std::string &expression, const ConsoleStream *console=nullptr)
Constructor.
const std::unordered_map< std::string, Variable > & getVariables() const
Get variables used in the expression.
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
A helper class for re-directing output streams to Console output objects form MooseObjects.
void addDefaultVariables()
Add default variables.
KOKKOS_FUNCTION Real eval(const Real t, const Real3 p, const unsigned int qp=0, Datum *datum=nullptr) const
Evaluate RPN at point (t,x,y,z)
The Kokkos object that holds thread-private data in the parallel operations of any Kokkos object...
void finalize()
Finalize the builder and prevent further changes.
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sin(_arg) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tan
unsigned int _num_functions
Number of functions.
const unsigned int invalid_uint
VariableType
Types of variables.
const std::vector< Real > & getNumbers() const
Get numbers used in the expression.
static constexpr double _epsilon
Epsilon for equality comparison.
bool _has_default_variables
Whether default variables were added.
std::vector< Instruction > _rpn
RPN sequence.
static const std::map< std::string, Opcode > _unary_opcode_map
Map from unary operators to opcodes.
unsigned int addNumber(Real number)
Add a parsed function constant.
const std::vector< Instruction > & getRPN() const
Get RPN sequence.
Array< Function > _functions
Functions used in the function.
KOKKOS_FUNCTION T & first() const
Get the first element.
const std::string & expression() const
Get input expression.
unsigned int _num_scalars
Number of scalar variables.
Array< VariableValue > _fields
Field variables used in the function.
void builderError(const peg::Ast &ast, const std::string &message) const
Print a pretty error showing the position of error.
Array< Real > _numbers
Numbers used in the function.
static const std::map< std::string, std::pair< Opcode, unsigned int > > _function_opcode_map
Map from functions to opcodes and the expected number of arguments.
Parsing Expression Grammar (PEG)
std::shared_ptr< peg::Ast > _ast
Abstract Syntax Tree (AST)
auto max(const L &left, const R &right)
static const std::map< std::string, Opcode > _binary_opcode_map
Map from binary operators to opcodes.
The abstract class that provides polymorphic interfaces for a function.
Array< Real > _scalars
Scalar variables used in the function.
peg::parser _parser
Parser object.
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template cos(_arg) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(cos
RPNEvaluator()=default
Default constructor.
Reverse Polish Notation (RPN) builder.
void init(const RPNBuilder &builder)
Initialize RPN evaluator from an RPN builder.
unsigned int addVariable(const std::string &name)
Add a parsed function variable.
bool finalized() const
Get whether the builder was finalized.
RPNBuilder(const std::string &expression, const ConsoleStream *console=nullptr)
Constructor.
The Kokkos wrapper classes for MOOSE-like variable value access.
Reverse Polish Notation (RPN) evaluator.
static constexpr unsigned int _stack_size
Fixed stack size.
void associateScalar(const std::string &name, const Real *scalar)
Associate a variable with a scalar value.
std::vector< const void * > _pointers
Pointers to the associated quantities of variables.
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template sinh(_arg) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(erf
unsigned int _num_properties
Number of material properties.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
bool hasDefaultVariables() const
Get whether default variables were added.
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
std::vector< Real > _numbers
Numbers used in the function.
Array< Instruction > _rpn
RPN sequence.
void associateProperty(const std::string &name, const MaterialProperty< Real > *property)
Associate a variable with a material property.
bool _finalized
Whether builder was finalized.
void checkFinalized()
Error on attempts to update the builder after finalization.
const std::string _expression
Input expression.
void printRPN(const ConsoleStream &console) const
Print RPN sequence for debugging.
Array< MaterialProperty< Real > > _properties
Material properties used in the function.
const VariableValue * field
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template cosh(_arg) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(cosh
Array<::Kokkos::pair< VariableType, unsigned int > > _variables
Types of variables and indices.
auto min(const L &left, const R &right)
unsigned int _num_fields
Number of field variables.
void associateField(const std::string &name, const VariableValue *field)
Associate a variable with a field variable.
std::unordered_map< std::string, Variable > _variables
Variables used in the function.
PEGParser _parser
PEG parser.