https://mooseframework.inl.gov
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Moose::Kokkos::RPNBuilder Class Reference

Reverse Polish Notation (RPN) builder. More...

#include <KokkosFunctionParser.h>

Classes

struct  Instruction
 RPN instruction. More...
 
struct  Variable
 Variable identifier. More...
 

Public Types

enum  Opcode {
  Opcode::NUM, Opcode::VAR, Opcode::NEG, Opcode::NOT,
  Opcode::ADD, Opcode::SUB, Opcode::MUL, Opcode::DIV,
  Opcode::AND, Opcode::OR, Opcode::EQ, Opcode::NEQ,
  Opcode::LT, Opcode::LEQ, Opcode::GT, Opcode::GEQ,
  Opcode::ABS, Opcode::ACOS, Opcode::ACOSH, Opcode::ASIN,
  Opcode::ASINH, Opcode::ATAN, Opcode::ATAN2, Opcode::ATANH,
  Opcode::CBRT, Opcode::CEIL, Opcode::COS, Opcode::COSH,
  Opcode::COT, Opcode::CSC, Opcode::EXP, Opcode::EXP2,
  Opcode::FLOOR, Opcode::HYPOT, Opcode::IF, Opcode::INT,
  Opcode::LOG, Opcode::LOG2, Opcode::LOG10, Opcode::MAX,
  Opcode::MIN, Opcode::POW, Opcode::SEC, Opcode::SIN,
  Opcode::SINH, Opcode::SQRT, Opcode::TAN, Opcode::TANH,
  Opcode::TRUNC
}
 RPN opcode. More...
 

Public Member Functions

 RPNBuilder (const std::string &expression, const ConsoleStream *console=nullptr)
 Constructor. More...
 
void printRPN (const ConsoleStream &console) const
 Print RPN sequence for debugging. More...
 
const std::vector< Instruction > & getRPN () const
 Get RPN sequence. More...
 
void addDefaultVariables ()
 Add default variables. More...
 
bool hasDefaultVariables () const
 Get whether default variables were added. More...
 
unsigned int addNumber (Real number)
 Add a parsed function constant. More...
 
unsigned int addVariable (const std::string &name)
 Add a parsed function variable. More...
 
void associateScalar (const std::string &name, const Real *scalar)
 Associate a variable with a scalar value. More...
 
void associateField (const std::string &name, const VariableValue *field)
 Associate a variable with a field variable. More...
 
void associateProperty (const std::string &name, const MaterialProperty< Real > *property)
 Associate a variable with a material property. More...
 
void associateFunction (const std::string &name, const Function *function)
 Associate a variable with a function. More...
 
const std::vector< Real > & getNumbers () const
 Get numbers used in the expression. More...
 
const std::unordered_map< std::string, Variable > & getVariables () const
 Get variables used in the expression. More...
 
void finalize ()
 Finalize the builder and prevent further changes. More...
 
bool finalized () const
 Get whether the builder was finalized. More...
 
void build (const peg::Ast &ast)
 Build RPN from AST. More...
 
void build ()
 

Private Member Functions

void builderError (const peg::Ast &ast, const std::string &message) const
 Print a pretty error showing the position of error. More...
 
void checkFinalized ()
 Error on attempts to update the builder after finalization. More...
 

Private Attributes

PEGParser _parser
 PEG parser. More...
 
std::vector< Instruction_rpn
 RPN sequence. More...
 
std::vector< Real_numbers
 Numbers used in the function. More...
 
std::unordered_map< std::string, Variable_variables
 Variables used in the function. More...
 
bool _has_default_variables = false
 Whether default variables were added. More...
 
bool _finalized = false
 Whether builder was finalized. More...
 

Static Private Attributes

static const std::map< std::string, Opcode_unary_opcode_map
 Map from unary operators to opcodes. More...
 
static const std::map< std::string, Opcode_binary_opcode_map
 Map from binary operators to opcodes. More...
 
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. More...
 

Detailed Description

Reverse Polish Notation (RPN) builder.

Definition at line 69 of file KokkosFunctionParser.h.

Member Enumeration Documentation

◆ Opcode

RPN opcode.

Enumerator
NUM 
VAR 
NEG 
NOT 
ADD 
SUB 
MUL 
DIV 
AND 
OR 
EQ 
NEQ 
LT 
LEQ 
GT 
GEQ 
ABS 
ACOS 
ACOSH 
ASIN 
ASINH 
ATAN 
ATAN2 
ATANH 
CBRT 
CEIL 
COS 
COSH 
COT 
CSC 
EXP 
EXP2 
FLOOR 
HYPOT 
IF 
INT 
LOG 
LOG2 
LOG10 
MAX 
MIN 
POW 
SEC 
SIN 
SINH 
SQRT 
TAN 
TANH 
TRUNC 

Definition at line 85 of file KokkosFunctionParser.h.

86  {
87  NUM,
88  VAR,
89  NEG,
90  NOT,
91  // Binary operators
92  ADD,
93  SUB,
94  MUL,
95  DIV,
96  AND,
97  OR,
98  EQ,
99  NEQ,
100  LT,
101  LEQ,
102  GT,
103  GEQ,
104  // Functions
105  ABS,
106  ACOS,
107  ACOSH,
108  ASIN,
109  ASINH,
110  ATAN,
111  ATAN2,
112  ATANH,
113  CBRT,
114  CEIL,
115  COS,
116  COSH,
117  COT,
118  CSC,
119  EXP,
120  EXP2,
121  FLOOR,
122  HYPOT,
123  IF,
124  INT,
125  LOG,
126  LOG2,
127  LOG10,
128  MAX,
129  MIN,
130  POW,
131  SEC,
132  SIN,
133  SINH,
134  SQRT,
135  TAN,
136  TANH,
137  TRUNC
138  };

Constructor & Destructor Documentation

◆ RPNBuilder()

Moose::Kokkos::RPNBuilder::RPNBuilder ( const std::string &  expression,
const ConsoleStream console = nullptr 
)
inline

Constructor.

Parameters
expressionThe function expression
consoleThe console object

Definition at line 77 of file KokkosFunctionParser.h.

78  : _parser(expression, console)
79  {
80  }
PEGParser _parser
PEG parser.

Member Function Documentation

◆ addDefaultVariables()

void Moose::Kokkos::RPNBuilder::addDefaultVariables ( )

Add default variables.

◆ addNumber()

unsigned int Moose::Kokkos::RPNBuilder::addNumber ( Real  number)

Add a parsed function constant.

Parameters
numberThe constant
Returns
The constant index

◆ addVariable()

unsigned int Moose::Kokkos::RPNBuilder::addVariable ( const std::string &  name)

Add a parsed function variable.

Parameters
nameThe variable name
Returns
The variable index

◆ associateField()

void Moose::Kokkos::RPNBuilder::associateField ( const std::string &  name,
const VariableValue field 
)

Associate a variable with a field variable.

Parameters
nameThe variable name
variableThe pointer to the field variable

◆ associateFunction()

void Moose::Kokkos::RPNBuilder::associateFunction ( const std::string &  name,
const Function function 
)

Associate a variable with a function.

Parameters
nameThe variable name
functionThe pointer to the function

◆ associateProperty()

void Moose::Kokkos::RPNBuilder::associateProperty ( const std::string &  name,
const MaterialProperty< Real > *  property 
)

Associate a variable with a material property.

Parameters
nameThe variable name
variableThe pointer to the material property

◆ associateScalar()

void Moose::Kokkos::RPNBuilder::associateScalar ( const std::string &  name,
const Real scalar 
)

Associate a variable with a scalar value.

Parameters
nameThe variable name
valueThe pointer to the scalar value

◆ build() [1/2]

void Moose::Kokkos::RPNBuilder::build ( const peg::Ast &  ast)

Build RPN from AST.

Parameters
astThe current node

◆ build() [2/2]

void Moose::Kokkos::RPNBuilder::build ( )
inline

Definition at line 178 of file KokkosFunctionParser.h.

Referenced by build().

◆ builderError()

void Moose::Kokkos::RPNBuilder::builderError ( const peg::Ast &  ast,
const std::string &  message 
) const
private

Print a pretty error showing the position of error.

Parameters
astThe erroneous AST node
messageThe error message

◆ checkFinalized()

void Moose::Kokkos::RPNBuilder::checkFinalized ( )
private

Error on attempts to update the builder after finalization.

◆ finalize()

void Moose::Kokkos::RPNBuilder::finalize ( )
inline

Finalize the builder and prevent further changes.

Definition at line 253 of file KokkosFunctionParser.h.

253 { _finalized = true; }
bool _finalized
Whether builder was finalized.

◆ finalized()

bool Moose::Kokkos::RPNBuilder::finalized ( ) const
inline

Get whether the builder was finalized.

Returns
Whether the builder was finalized

Definition at line 258 of file KokkosFunctionParser.h.

258 { return _finalized; }
bool _finalized
Whether builder was finalized.

◆ getNumbers()

const std::vector<Real>& Moose::Kokkos::RPNBuilder::getNumbers ( ) const
inline

Get numbers used in the expression.

Returns
The numbers

Definition at line 243 of file KokkosFunctionParser.h.

243 { return _numbers; }
std::vector< Real > _numbers
Numbers used in the function.

◆ getRPN()

const std::vector<Instruction>& Moose::Kokkos::RPNBuilder::getRPN ( ) const
inline

Get RPN sequence.

Returns
The RPN sequence

Definition at line 190 of file KokkosFunctionParser.h.

190 { return _rpn; }
std::vector< Instruction > _rpn
RPN sequence.

◆ getVariables()

const std::unordered_map<std::string, Variable>& Moose::Kokkos::RPNBuilder::getVariables ( ) const
inline

Get variables used in the expression.

Returns
The variables

Definition at line 248 of file KokkosFunctionParser.h.

248 { return _variables; }
std::unordered_map< std::string, Variable > _variables
Variables used in the function.

◆ hasDefaultVariables()

bool Moose::Kokkos::RPNBuilder::hasDefaultVariables ( ) const
inline

Get whether default variables were added.

Returns
Whether default variables were added

Definition at line 200 of file KokkosFunctionParser.h.

200 { return _has_default_variables; }
bool _has_default_variables
Whether default variables were added.

◆ printRPN()

void Moose::Kokkos::RPNBuilder::printRPN ( const ConsoleStream console) const

Print RPN sequence for debugging.

Parameters
consoleThe console object

Member Data Documentation

◆ _binary_opcode_map

const std::map<std::string, Opcode> Moose::Kokkos::RPNBuilder::_binary_opcode_map
inlinestaticprivate
Initial value:
= {{"+", Opcode::ADD},
{"-", Opcode::SUB},
{"*", Opcode::MUL},
{"/", Opcode::DIV},
{"^", Opcode::POW},
{"&", Opcode::AND},
{"|", Opcode::OR},
{"=", Opcode::EQ},
{"!=", Opcode::NEQ},
{"<", Opcode::LT},
{"<=", Opcode::LEQ},
{">", Opcode::GT},
{">=", Opcode::GEQ}}

Map from binary operators to opcodes.

Definition at line 270 of file KokkosFunctionParser.h.

◆ _finalized

bool Moose::Kokkos::RPNBuilder::_finalized = false
private

Whether builder was finalized.

Definition at line 323 of file KokkosFunctionParser.h.

Referenced by finalize(), and finalized().

◆ _function_opcode_map

const std::map<std::string, std::pair<Opcode, unsigned int> > Moose::Kokkos::RPNBuilder::_function_opcode_map
inlinestaticprivate
Initial value:
=
{{"abs", {Opcode::ABS, 1}}, {"acos", {Opcode::ACOS, 1}}, {"acosh", {Opcode::ACOSH, 1}},
{"asin", {Opcode::ASIN, 1}}, {"asinh", {Opcode::ASINH, 1}}, {"atan", {Opcode::ATAN, 1}},
{"atan2", {Opcode::ATAN2, 2}}, {"atanh", {Opcode::ATANH, 1}}, {"cbrt", {Opcode::CBRT, 1}},
{"ceil", {Opcode::CEIL, 1}}, {"cos", {Opcode::COS, 1}}, {"cosh", {Opcode::COSH, 1}},
{"cot", {Opcode::COT, 1}}, {"csc", {Opcode::CSC, 1}}, {"exp", {Opcode::EXP, 1}},
{"exp2", {Opcode::EXP2, 1}}, {"floor", {Opcode::FLOOR, 1}}, {"hypot", {Opcode::HYPOT, 2}},
{"if", {Opcode::IF, 3}}, {"int", {Opcode::INT, 1}}, {"log", {Opcode::LOG, 1}},
{"log2", {Opcode::LOG2, 1}}, {"log10", {Opcode::LOG10, 1}}, {"max", {Opcode::MAX, 2}},
{"min", {Opcode::MIN, 2}}, {"pow", {Opcode::POW, 2}}, {"sec", {Opcode::SEC, 1}},
{"sin", {Opcode::SIN, 1}}, {"sinh", {Opcode::SINH, 1}}, {"sqrt", {Opcode::SQRT, 1}},
{"tan", {Opcode::TAN, 1}}, {"tanh", {Opcode::TANH, 1}}, {"trunc", {Opcode::TRUNC, 1}}}

Map from functions to opcodes and the expected number of arguments.

Definition at line 287 of file KokkosFunctionParser.h.

◆ _has_default_variables

bool Moose::Kokkos::RPNBuilder::_has_default_variables = false
private

Whether default variables were added.

Definition at line 319 of file KokkosFunctionParser.h.

Referenced by hasDefaultVariables().

◆ _numbers

std::vector<Real> Moose::Kokkos::RPNBuilder::_numbers
private

Numbers used in the function.

Definition at line 311 of file KokkosFunctionParser.h.

Referenced by getNumbers().

◆ _parser

PEGParser Moose::Kokkos::RPNBuilder::_parser
private

PEG parser.

Definition at line 303 of file KokkosFunctionParser.h.

Referenced by build().

◆ _rpn

std::vector<Instruction> Moose::Kokkos::RPNBuilder::_rpn
private

RPN sequence.

Definition at line 307 of file KokkosFunctionParser.h.

Referenced by getRPN().

◆ _unary_opcode_map

const std::map<std::string, Opcode> Moose::Kokkos::RPNBuilder::_unary_opcode_map
inlinestaticprivate
Initial value:
= {{"-", Opcode::NEG},
{"!", Opcode::NOT}}

Map from unary operators to opcodes.

Definition at line 264 of file KokkosFunctionParser.h.

◆ _variables

std::unordered_map<std::string, Variable> Moose::Kokkos::RPNBuilder::_variables
private

Variables used in the function.

Definition at line 315 of file KokkosFunctionParser.h.

Referenced by getVariables().


The documentation for this class was generated from the following file: