The expression language shared by every configured field: a parser, a postfix compiler, and a pure stack evaluator. More...
#include "expression.h"#include "Metric.h"#include "Boundaries.h"#include "grid.h"#include <ctype.h>#include <math.h>#include <stdint.h>#include <stdlib.h>#include <string.h>#include "logging.h"Go to the source code of this file.
Data Structures | |
| struct | ExpressionNode |
| One tree node; children index the node pool. More... | |
| struct | ExpressionInstruction |
| One postfix instruction. More... | |
| struct | PicurvExpression |
| struct | ExpressionParser |
| Parser state: the text, a cursor, the node pool, and the names in scope. More... | |
Macros | |
| #define | EXPRESSION_MAX_SERIES_DEPTH 4 |
| Series that may nest inside one another, and terms one series may add. | |
| #define | EXPRESSION_MAX_SERIES_TERMS 1000000 |
| #define | PICURV_SOURCE_EXPRESSION_LENGTH 4096 |
Enumerations | |
| enum | ExpressionOp { OP_CONST = 0 , OP_VAR , OP_NEG , OP_POS , OP_NOT , OP_ADD , OP_SUB , OP_MUL , OP_DIV , OP_MOD , OP_POW , OP_EQ , OP_NE , OP_LT , OP_LE , OP_GT , OP_GE , OP_AND , OP_OR , OP_ABS , OP_COS , OP_ERF , OP_EXP , OP_SIN , OP_SQRT , OP_TAN , OP_TANH , OP_COSH , OP_SINH , OP_LOG , OP_FLOOR , OP_ATAN2 , OP_MAXIMUM , OP_MINIMUM , OP_WHERE , OP_UNIFORM , OP_NORMAL , OP_BESSELJ , OP_BESSELY , OP_BER , OP_BEI , OP_SUM , OP_SUM_START , OP_SUM_HI , OP_SUM_ACC , OP_LOCAL } |
Functions | |
| static void | ParserFail (ExpressionParser *parser, const char *message) |
| Record the first parse error with its position; later ones are consequences. | |
| static void | SkipSpace (ExpressionParser *parser) |
| Skip whitespace before the next token. | |
| static PetscBool | Accept (ExpressionParser *parser, const char *token) |
Consume token if it is next, and report whether it was. | |
| static PetscInt | NewNode (ExpressionParser *parser, ExpressionOp op, PetscInt nchild, PetscInt a, PetscInt b, PetscInt c) |
| Append a node to the pool and return its index, or -1 once an error is set. | |
| static PetscInt | ParseOr (ExpressionParser *parser) |
| and ('or' and)* | |
| static PetscBool | SeriesBound (const ExpressionParser *parser, PetscInt index, PetscReal *bound) |
| The value of a series bound: an integer literal, possibly negated or parenthesized. | |
| static PetscInt | ParseSeries (ExpressionParser *parser) |
sum( has been read: index name, integer bounds, then the body with the index in scope. | |
| static PetscInt | ParseAtom (ExpressionParser *parser) |
| number | name | name(args) | sum(index, lo, hi, body) | (expression) | |
| static PetscInt | ParseUnary (ExpressionParser *parser) |
| Parses a sign prefix recursively and wraps the operand in a negate or identity node. | |
| static PetscInt | ParsePower (ExpressionParser *parser) |
| Parses an atom and, when a power operator follows, appends a power node. | |
| static PetscInt | ParseTerm (ExpressionParser *parser) |
Parses a left-associative chain of *, /, %, folding each operator into a node. | |
| static PetscInt | ParseSum (ExpressionParser *parser) |
Parses a left-associative chain of + and -, folding each operator into a node. | |
| static PetscInt | ParseComparison (ExpressionParser *parser) |
sum (comparison sum)*; a chain a < b < c is (a < b) and (b < c). | |
| static PetscInt | ParseNot (ExpressionParser *parser) |
| 'not' not | comparison | |
| static PetscInt | ParseAnd (ExpressionParser *parser) |
| not ('and' not)* | |
| static PetscInt | CountCode (const ExpressionParser *parser, PetscInt index) |
| Count the instructions a subtree emits (shared subtrees count per use). | |
| static void | EmitNode (const ExpressionParser *parser, PetscInt index, PicurvExpression *expression, PetscInt *depth) |
| Emit postfix code for a subtree and track the stack depth it needs. | |
| PetscErrorCode | PicurvExpressionCompile (const char *text, const char *const *names, PetscInt name_count, PetscBool allow_random, PicurvExpression **expression) |
| Implementation of PicurvExpressionCompile(). | |
| uint64_t | PicurvMix64 (uint64_t z) |
| Implementation of PicurvMix64(). | |
| PetscReal | PicurvUnitFromHash (uint64_t hash) |
| Implementation of PicurvUnitFromHash(). | |
| uint64_t | PicurvParticleDrawHash (PetscInt64 seed, PicurvDrawSpace space, uint64_t stream, PetscInt64 pid, PetscInt64 salt) |
| Implementation of PicurvParticleDrawHash(). | |
| static PetscReal | Draw (const PicurvExpressionDrawKey *key, const ExpressionInstruction *instruction) |
| A draw keyed on the particle, the event, the stream, and the function. | |
| static PetscReal | Kelvin (PetscReal x, PetscBool imaginary) |
Kelvin functions of order zero, ber(x) + i bei(x) = J0(x e^{3 pi i / 4}). | |
| static PetscReal | Bessel (PetscReal order, PetscReal x, PetscBool second) |
| Bessel function of the first or second kind of integer order; NaN for another order. | |
| static PetscReal | Extreme (PetscReal a, PetscReal b, PetscBool maximum) |
| Maximum or minimum that propagates NaN, as numpy does. | |
| PetscErrorCode | PicurvExpressionEvaluate (const PicurvExpression *expression, const PetscReal *values, const PicurvExpressionDrawKey *key, PetscReal *result) |
| Implementation of PicurvExpressionEvaluate(). | |
| PetscErrorCode | PicurvExpressionDestroy (PicurvExpression **expression) |
| Implementation of PicurvExpressionDestroy(). | |
| PetscErrorCode | PicurvExpressionUsesVariable (const PicurvExpression *expression, PetscInt index, PetscBool *uses) |
| Implementation of PicurvExpressionUsesVariable(). | |
| PetscErrorCode | PicurvVelocitySourceCreate (const char *prefix, PetscBool on_face, PetscBool has_data, PicurvVelocitySource *source) |
| Implementation of PicurvVelocitySourceCreate(). | |
| PetscErrorCode | PicurvVelocitySourceBulkFactor (const PicurvVelocitySource *source, PetscReal physical_time, const PetscReal local[3], const char *where, PetscReal *factor) |
| Implementation of PicurvVelocitySourceBulkFactor(). | |
| PetscErrorCode | PicurvVelocitySourceEvaluate (const PicurvVelocitySource *source, const PetscReal *values, const Cmpnts edges[3], Cmpnts *value) |
| Implementation of PicurvVelocitySourceEvaluate(). | |
| PetscErrorCode | PicurvSourceCellBulkFactor (UserCtx *user, const PicurvVelocitySource *source, Vec fraction, Vec length, PetscReal physical_time, PetscReal *factor) |
| Implementation of PicurvSourceCellBulkFactor(). | |
| PetscErrorCode | PicurvSourceAddFluxes (UserCtx *user, const PicurvVelocitySource *source, Vec fraction, Vec length, PetscReal physical_time, PetscReal factor, PetscBool boundary_faces) |
| Implementation of PicurvSourceAddFluxes(). | |
| PetscErrorCode | PicurvSourceSetCellPressure (UserCtx *user, const PicurvExpression *pressure, Vec fraction, Vec length, PetscReal physical_time) |
| Implementation of PicurvSourceSetCellPressure(). | |
| PetscErrorCode | PicurvSourceFaceSlotsCreate (UserCtx *user, BCFace face_id, Vec fraction, Vec length, PicurvSourceFaceSlots *slots) |
| Implementation of PicurvSourceFaceSlotsCreate(). | |
| PetscErrorCode | PicurvSourceFaceSlotsDestroy (PicurvSourceFaceSlots *slots) |
| Implementation of PicurvSourceFaceSlotsDestroy(). | |
| PetscErrorCode | PicurvVelocitySourceDestroy (PicurvVelocitySource *source) |
| Implementation of PicurvVelocitySourceDestroy(). | |
Variables | ||
| struct { | ||
| const char * | name | |
| int | min_args | |
| int | max_args | |
| ExpressionOp | op | |
| PetscBool | random | |
| } | kFunctions [] | |
| Name, arity, and opcode of every callable function. | ||
| const char *const | kPicurvSourceVariableNames [PICURV_SOURCE_VAR_COUNT] | |
| Names of PicurvSourceVariable, in order. | ||
The expression language shared by every configured field: a parser, a postfix compiler, and a pure stack evaluator.
Expressions are parsed into a small tree, because a chained comparison evaluates its middle operand twice, and compiled to postfix code that a stack evaluator runs once per point. A series sum(k, lo, hi, body) compiles its body once and loops over it, with the index in a slot of its nesting level. Evaluation is pure: random draws are hashed from a caller-supplied key, so a value does not depend on rank count, evaluation order, or how often it is evaluated.
Definition in file expression.c.
| struct ExpressionNode |
One tree node; children index the node pool.
Definition at line 69 of file expression.c.
| Data Fields | ||
|---|---|---|
| ExpressionOp | op | |
| PetscReal | value | |
| PetscReal | upper | |
| PetscInt | var | |
| PetscInt | child[3] | |
| PetscInt | nchild | |
| struct ExpressionInstruction |
One postfix instruction.
Definition at line 79 of file expression.c.
| Data Fields | ||
|---|---|---|
| ExpressionOp | op | |
| PetscReal | value | |
| PetscInt | arg | |
| struct PicurvExpression |
Definition at line 85 of file expression.c.
| Data Fields | ||
|---|---|---|
| ExpressionInstruction * | code | |
| PetscInt | length | |
| PetscInt | max_depth | |
| PetscBool | draws | |
| struct ExpressionParser |
Parser state: the text, a cursor, the node pool, and the names in scope.
Definition at line 93 of file expression.c.
| Data Fields | ||
|---|---|---|
| const char * | text | |
| size_t | pos | |
| ExpressionNode * | nodes | |
| PetscInt | count | |
| PetscInt | capacity | |
| const char *const * | names | |
| PetscInt | name_count | |
| PetscBool | allow_random | |
| char | series_index[4][64] | |
| PetscInt | series_depth | |
| char | error[256] | |
| #define EXPRESSION_MAX_SERIES_DEPTH 4 |
Series that may nest inside one another, and terms one series may add.
Definition at line 65 of file expression.c.
| #define EXPRESSION_MAX_SERIES_TERMS 1000000 |
Definition at line 66 of file expression.c.
| #define PICURV_SOURCE_EXPRESSION_LENGTH 4096 |
Definition at line 824 of file expression.c.
| enum ExpressionOp |
Definition at line 29 of file expression.c.
|
static |
Record the first parse error with its position; later ones are consequences.
Definition at line 108 of file expression.c.
|
static |
Skip whitespace before the next token.
Definition at line 119 of file expression.c.
|
static |
Consume token if it is next, and report whether it was.
Definition at line 127 of file expression.c.
|
static |
Append a node to the pool and return its index, or -1 once an error is set.
Definition at line 150 of file expression.c.
|
static |
and ('or' and)*
Definition at line 489 of file expression.c.
|
static |
The value of a series bound: an integer literal, possibly negated or parenthesized.
Definition at line 174 of file expression.c.
|
static |
sum( has been read: index name, integer bounds, then the body with the index in scope.
Definition at line 189 of file expression.c.
|
static |
number | name | name(args) | sum(index, lo, hi, body) | (expression)
Definition at line 247 of file expression.c.
|
static |
Parses a sign prefix recursively and wraps the operand in a negate or identity node.
| [in,out] | parser | Parser state; its position advances past the operand. |
Definition at line 399 of file expression.c.
|
static |
Parses an atom and, when a power operator follows, appends a power node.
Grammar: ‘atom [’**' unary]. The exponent is parsed as a unary, so2**-1is valid anda**b**cgroups to the right; a unary minus on the left is parsed by the caller, so-2**2` is -4.
| [in,out] | parser | Parser state; its position advances past the power. |
Definition at line 385 of file expression.c.
|
static |
Parses a left-associative chain of *, /, %, folding each operator into a node.
// is refused explicitly, since the Python side does not accept floor division.
| [in,out] | parser | Parser state; its position advances past the term. |
Definition at line 414 of file expression.c.
|
static |
Parses a left-associative chain of + and -, folding each operator into a node.
| [in,out] | parser | Parser state; its position advances past the sum. |
Definition at line 434 of file expression.c.
|
static |
sum (comparison sum)*; a chain a < b < c is (a < b) and (b < c).
Definition at line 447 of file expression.c.
|
static |
'not' not | comparison
Definition at line 470 of file expression.c.
|
static |
not ('and' not)*
Definition at line 479 of file expression.c.
|
static |
Count the instructions a subtree emits (shared subtrees count per use).
Definition at line 534 of file expression.c.
|
static |
Emit postfix code for a subtree and track the stack depth it needs.
Definition at line 501 of file expression.c.
| PetscErrorCode PicurvExpressionCompile | ( | const char * | text, |
| const char *const * | names, | ||
| PetscInt | name_count, | ||
| PetscBool | allow_random, | ||
| PicurvExpression ** | expression | ||
| ) |
Implementation of PicurvExpressionCompile().
Compile one expression against a list of variable names.
Definition at line 549 of file expression.c.
| uint64_t PicurvMix64 | ( | uint64_t | z | ) |
Implementation of PicurvMix64().
splitmix64 finalizer: a bijective mix of 64 bits.
Definition at line 587 of file expression.c.
| PetscReal PicurvUnitFromHash | ( | uint64_t | hash | ) |
Implementation of PicurvUnitFromHash().
Uniform on [0, 1) from the top 53 bits of a hash.
Definition at line 599 of file expression.c.
| uint64_t PicurvParticleDrawHash | ( | PetscInt64 | seed, |
| PicurvDrawSpace | space, | ||
| uint64_t | stream, | ||
| PetscInt64 | pid, | ||
| PetscInt64 | salt | ||
| ) |
Implementation of PicurvParticleDrawHash().
Hash that keys a particle draw: a pure function of its arguments, so a draw does not depend on the rank a particle lives on, the order particles are visited, or what was drawn before it.
Definition at line 608 of file expression.c.
|
static |
A draw keyed on the particle, the event, the stream, and the function.
Definition at line 619 of file expression.c.
|
static |
Kelvin functions of order zero, ber(x) + i bei(x) = J0(x e^{3 pi i / 4}).
From their power series below |x| = 30. Its terms grow to about exp(|x| / sqrt(2)) before it converges, losing digits to cancellation: 1e-13 relative at 30, 1e-9 at 60, and none left by 150. From 30 up, the asymptotic expansion in 1/|x|, whose error at 30 is 7e-15 and falls from there. Both functions are even.
Definition at line 643 of file expression.c.
|
static |
Bessel function of the first or second kind of integer order; NaN for another order.
Definition at line 673 of file expression.c.
|
static |
Maximum or minimum that propagates NaN, as numpy does.
Definition at line 683 of file expression.c.
| PetscErrorCode PicurvExpressionEvaluate | ( | const PicurvExpression * | expression, |
| const PetscReal * | values, | ||
| const PicurvExpressionDrawKey * | key, | ||
| PetscReal * | result | ||
| ) |
Implementation of PicurvExpressionEvaluate().
Evaluate a compiled expression.
Definition at line 695 of file expression.c.
| PetscErrorCode PicurvExpressionDestroy | ( | PicurvExpression ** | expression | ) |
Implementation of PicurvExpressionDestroy().
Free a compiled expression.
Definition at line 789 of file expression.c.
| PetscErrorCode PicurvExpressionUsesVariable | ( | const PicurvExpression * | expression, |
| PetscInt | index, | ||
| PetscBool * | uses | ||
| ) |
Implementation of PicurvExpressionUsesVariable().
Whether a compiled expression reads one of its variables.
Definition at line 805 of file expression.c.
| PetscErrorCode PicurvVelocitySourceCreate | ( | const char * | prefix, |
| PetscBool | on_face, | ||
| PetscBool | has_data, | ||
| PicurvVelocitySource * | source | ||
| ) |
Implementation of PicurvVelocitySourceCreate().
Read and compile a velocity source from the options database.
Definition at line 830 of file expression.c.
| PetscErrorCode PicurvVelocitySourceBulkFactor | ( | const PicurvVelocitySource * | source, |
| PetscReal | physical_time, | ||
| const PetscReal | local[3], | ||
| const char * | where, | ||
| PetscReal * | factor | ||
| ) |
Implementation of PicurvVelocitySourceBulkFactor().
Factor on a source's expressions that makes a target's whole mean equal its bulk velocity.
Definition at line 882 of file expression.c.
| PetscErrorCode PicurvVelocitySourceEvaluate | ( | const PicurvVelocitySource * | source, |
| const PetscReal * | values, | ||
| const Cmpnts | edges[3], | ||
| Cmpnts * | value | ||
| ) |
Implementation of PicurvVelocitySourceEvaluate().
Evaluate a source at one point.
Definition at line 907 of file expression.c.
| PetscErrorCode PicurvSourceCellBulkFactor | ( | UserCtx * | user, |
| const PicurvVelocitySource * | source, | ||
| Vec | fraction, | ||
| Vec | length, | ||
| PetscReal | physical_time, | ||
| PetscReal * | factor | ||
| ) |
Implementation of PicurvSourceCellBulkFactor().
Factor on a source's expressions that makes the volume mean of the whole field, along the source's own direction at each cell, equal its bulk velocity.
Definition at line 944 of file expression.c.
| PetscErrorCode PicurvSourceAddFluxes | ( | UserCtx * | user, |
| const PicurvVelocitySource * | source, | ||
| Vec | fraction, | ||
| Vec | length, | ||
| PetscReal | physical_time, | ||
| PetscReal | factor, | ||
| PetscBool | boundary_faces | ||
| ) |
Implementation of PicurvSourceAddFluxes().
Add a source's flux to every owned face of Ucont, evaluating it at each face centre.
Definition at line 998 of file expression.c.
| PetscErrorCode PicurvSourceSetCellPressure | ( | UserCtx * | user, |
| const PicurvExpression * | pressure, | ||
| Vec | fraction, | ||
| Vec | length, | ||
| PetscReal | physical_time | ||
| ) |
Implementation of PicurvSourceSetCellPressure().
Set a block's pressure at every interior cell centre from an expression in Pa.
Definition at line 1086 of file expression.c.
| PetscErrorCode PicurvSourceFaceSlotsCreate | ( | UserCtx * | user, |
| BCFace | face_id, | ||
| Vec | fraction, | ||
| Vec | length, | ||
| PicurvSourceFaceSlots * | slots | ||
| ) |
Implementation of PicurvSourceFaceSlotsCreate().
Gather each owned slot of a boundary face: face-centre position, the grid-line fractions and lengths of the interior cell behind it, and its directions.
Definition at line 1124 of file expression.c.
| PetscErrorCode PicurvSourceFaceSlotsDestroy | ( | PicurvSourceFaceSlots * | slots | ) |
Implementation of PicurvSourceFaceSlotsDestroy().
Release what PicurvSourceFaceSlotsCreate() allocated.
Definition at line 1203 of file expression.c.
| PetscErrorCode PicurvVelocitySourceDestroy | ( | PicurvVelocitySource * | source | ) |
Implementation of PicurvVelocitySourceDestroy().
Free a source's compiled expressions and bulk velocity.
Definition at line 1216 of file expression.c.
| const struct { ... } kFunctions[] |
| const char* const kPicurvSourceVariableNames[PICURV_SOURCE_VAR_COUNT] |
Names of PicurvSourceVariable, in order.
Definition at line 821 of file expression.c.