OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
openswmm::mathexpr Namespace Reference

Classes

struct  Expression
 
struct  Token
 

Enumerations

enum class  TokenType : int {
  NUMBER = 0 ,
  VARIABLE = 1 ,
  ADD = 2 ,
  SUB = 3 ,
  MUL = 4 ,
  DIV = 5 ,
  POW = 6 ,
  NEG = 7 ,
  LPAREN = 8 ,
  RPAREN = 9 ,
  FUNC_ABS = 10 ,
  FUNC_SGN = 11 ,
  FUNC_SQRT = 12 ,
  FUNC_LOG = 13 ,
  FUNC_EXP = 14 ,
  FUNC_SIN = 15 ,
  FUNC_COS = 16 ,
  FUNC_TAN = 17 ,
  FUNC_ASIN = 18 ,
  FUNC_ACOS = 19 ,
  FUNC_ATAN = 20 ,
  FUNC_STEP = 21 ,
  FUNC_MIN = 22 ,
  FUNC_MAX = 23 ,
  COMMA = 24 ,
  FUNC_COT = 25 ,
  FUNC_SINH = 26 ,
  FUNC_COSH = 27 ,
  FUNC_TANH = 28 ,
  FUNC_COTH = 29 ,
  FUNC_LOG10 = 30 ,
  FUNC_ACOT = 31
}
 

Functions

int parse (const std::string &expr_str, Expression &result)
 Parse an infix expression string into a postfix Expression.
 
double evaluate (const Expression &expr, const std::function< double(const std::string &)> &var_lookup)
 Evaluate a compiled expression with named variable lookup.
 
double evaluate (const Expression &expr, const double *vars, int n_vars)
 Evaluate with a flat variable array (by index).
 
int bind_variables (Expression &expr, const char *const *name_table, int n_vars)
 Bind variable names in a compiled expression to integer indices.
 
int compute_max_stack_depth (const Expression &expr)
 Compute the maximum stack depth needed to evaluate an expression.
 
double evaluate_fast (const Expression &expr, const double *vars) noexcept
 Fast evaluate using pre-bound variable indices and stack-free evaluation.
 

Enumeration Type Documentation

◆ TokenType

enum class openswmm::mathexpr::TokenType : int
strong
Enumerator
NUMBER 
VARIABLE 
ADD 
SUB 
MUL 
DIV 
POW 
NEG 
LPAREN 
RPAREN 
FUNC_ABS 
FUNC_SGN 
FUNC_SQRT 
FUNC_LOG 
FUNC_EXP 
FUNC_SIN 
FUNC_COS 
FUNC_TAN 
FUNC_ASIN 
FUNC_ACOS 
FUNC_ATAN 
FUNC_STEP 
FUNC_MIN 
FUNC_MAX 
COMMA 
FUNC_COT 

cot(x) = 1/tan(x)

FUNC_SINH 

sinh(x)

FUNC_COSH 

cosh(x)

FUNC_TANH 

tanh(x)

FUNC_COTH 

coth(x) = 1/tanh(x)

FUNC_LOG10 

log10(x)

FUNC_ACOT 

acot(x) = atan(1/x)

Function Documentation

◆ bind_variables()

int openswmm::mathexpr::bind_variables ( Expression expr,
const char *const *  name_table,
int  n_vars 
)

Bind variable names in a compiled expression to integer indices.

Resolves each VARIABLE token's var_name to a var_idx using the provided name→index table. After binding, evaluate_fast() can look up variables by index instead of by string comparison.

Parameters
exprExpression to bind (modified in place).
name_tableArray of variable names, indexed 0..n_vars-1.
n_varsNumber of entries in name_table.
Returns
Number of variables successfully bound.
Here is the call graph for this function:

◆ compute_max_stack_depth()

int openswmm::mathexpr::compute_max_stack_depth ( const Expression expr)

Compute the maximum stack depth needed to evaluate an expression.

Parameters
exprCompiled expression.
Returns
Max stack depth (0 if expression is empty).

◆ evaluate() [1/2]

double openswmm::mathexpr::evaluate ( const Expression expr,
const double *  vars,
int  n_vars 
)

Evaluate with a flat variable array (by index).

Parameters
exprCompiled expression.
varsVariable values indexed by var_idx.
n_varsSize of vars array.
Returns
Expression result.
Here is the call graph for this function:

◆ evaluate() [2/2]

double openswmm::mathexpr::evaluate ( const Expression expr,
const std::function< double(const std::string &)> &  var_lookup 
)

Evaluate a compiled expression with named variable lookup.

Parameters
exprCompiled expression.
var_lookupFunction that maps variable name → value.
Returns
Expression result.
Here is the caller graph for this function:

◆ evaluate_fast()

double openswmm::mathexpr::evaluate_fast ( const Expression expr,
const double *  vars 
)
noexcept

Fast evaluate using pre-bound variable indices and stack-free evaluation.

Uses a fixed-size stack array on the C stack (no heap allocation) and reads variables via vars[tok.var_idx] (no string comparison). Variables must be pre-bound via bind_variables().

Parameters
exprCompiled expression with bound variable indices.
varsVariable values array. vars[tok.var_idx] is read for each variable.
Returns
Expression result, or 0.0 if expression is invalid.

◆ parse()

int openswmm::mathexpr::parse ( const std::string &  expr_str,
Expression result 
)

Parse an infix expression string into a postfix Expression.

Parameters
expr_strInfix expression (e.g., "x * exp(-k * t)").
result[out] Compiled postfix expression.
Returns
0 on success, -1 on parse error.
Here is the caller graph for this function: