Configured initial values of particle-carried fields, and the expression language that defines them. More...
#include "ParticleInitialConditions.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... | |
| struct | ParticleFieldBinding |
| One configured field: its identity and one expression per component. More... | |
| struct | ParticleFieldPlan |
Macros | |
| #define | PARTICLE_VARIABLE_COUNT ((PetscInt)(sizeof(kParticleVariables) / sizeof(kParticleVariables[0]))) |
| #define | PARTICLE_FIELD_MAX_COMPONENTS 3 |
| #define | PARTICLE_FIELD_EXPRESSION_LENGTH 4096 |
| #define | PARTICLE_FIELD_HISTOGRAM_BINS 10 |
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_EXP , OP_SIN , OP_SQRT , OP_TAN , OP_MAXIMUM , OP_MINIMUM , OP_WHERE , OP_UNIFORM , OP_NORMAL } |
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 PetscInt | ParseAtom (ExpressionParser *parser) |
| number | name | name(args) | (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 void | EmitNode (const ExpressionParser *parser, PetscInt index, PicurvExpression *expression, PetscInt *depth) |
| Emit postfix code for a subtree and track the stack depth it needs. | |
| static PetscInt | CountCode (const ExpressionParser *parser, PetscInt index) |
| Count the instructions a subtree emits (shared subtrees count per use). | |
| PetscErrorCode | PicurvExpressionCompile (const char *text, const char *const *names, PetscInt name_count, PetscBool allow_random, PicurvExpression **expression) |
| Implementation of PicurvExpressionCompile(). | |
| static uint64_t | Mix64 (uint64_t z) |
| splitmix64 finalizer: a bijective mix of 64 bits. | |
| static PetscReal | UnitFromHash (uint64_t hash) |
| Uniform on [0, 1) from the top 53 bits of a hash. | |
| static PetscReal | Draw (const PicurvExpressionDrawKey *key, const ExpressionInstruction *instruction) |
| A draw keyed on the particle, the event, the stream, and the function. | |
| 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 | ParticleFieldPlanCreate (ParticleFieldPlan **plan) |
| Implementation of ParticleFieldPlanCreate(). | |
| static PetscErrorCode | DomainBounds (const SimCtx *simCtx, Cmpnts *lower, Cmpnts *upper) |
| Global domain bounds, in solver units, from the replicated rank bounding boxes. | |
| static PetscReal | Normalized (PetscReal value, PetscReal lower, PetscReal upper) |
| Position within the domain along one axis, in [0, 1]; 0 across a flat axis. | |
| PetscErrorCode | ParticleFieldPlanApply (UserCtx *user, const ParticleFieldPlan *plan, const ParticleFieldEvent *event, PetscInt first, PetscInt end) |
| Implementation of ParticleFieldPlanApply(). | |
| PetscErrorCode | ParticleFieldPlanSummarize (UserCtx *user, const ParticleFieldPlan *plan) |
| Implementation of ParticleFieldPlanSummarize(). | |
| PetscErrorCode | ParticleFieldPlanDestroy (ParticleFieldPlan **plan) |
| Implementation of ParticleFieldPlanDestroy(). | |
Variables | ||
| struct { | ||
| const char * | name | |
| int | min_args | |
| int | max_args | |
| ExpressionOp | op | |
| PetscBool | random | |
| } | kFunctions [] | |
| Name, arity, and opcode of every callable function. | ||
| static const char *const | kParticleVariables [] = {"x", "y", "z", "xn", "yn", "zn", "pid", "t"} | |
| Names a particle expression may use, in the order their values are supplied. | ||
Configured initial values of particle-carried fields, and the expression language that defines them.
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 particle. Evaluation is pure: random draws are hashed from the particle's identity, so a value does not depend on rank count, particle order, or how often it is evaluated.
Definition in file ParticleInitialConditions.c.
| struct ExpressionNode |
One tree node; children index the node pool.
Definition at line 51 of file ParticleInitialConditions.c.
| Data Fields | ||
|---|---|---|
| ExpressionOp | op | |
| PetscReal | value | |
| PetscInt | var | |
| PetscInt | child[3] | |
| PetscInt | nchild | |
| struct ExpressionInstruction |
One postfix instruction.
Definition at line 60 of file ParticleInitialConditions.c.
| Data Fields | ||
|---|---|---|
| ExpressionOp | op | |
| PetscReal | value | |
| PetscInt | arg | |
| struct PicurvExpression |
Definition at line 66 of file ParticleInitialConditions.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 74 of file ParticleInitialConditions.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 | error[256] | |
| struct ParticleFieldBinding |
One configured field: its identity and one expression per component.
Definition at line 548 of file ParticleInitialConditions.c.
| Data Fields | ||
|---|---|---|
| ParticleFieldId | field | |
| PetscInt | components | |
| PicurvExpression * | component[3] | |
| struct ParticleFieldPlan |
Definition at line 554 of file ParticleInitialConditions.c.
| Data Fields | ||
|---|---|---|
| PetscInt | count | |
| ParticleFieldBinding * | bindings | |
| #define PARTICLE_VARIABLE_COUNT ((PetscInt)(sizeof(kParticleVariables) / sizeof(kParticleVariables[0]))) |
Definition at line 542 of file ParticleInitialConditions.c.
| #define PARTICLE_FIELD_MAX_COMPONENTS 3 |
Definition at line 543 of file ParticleInitialConditions.c.
| #define PARTICLE_FIELD_EXPRESSION_LENGTH 4096 |
Definition at line 544 of file ParticleInitialConditions.c.
| #define PARTICLE_FIELD_HISTOGRAM_BINS 10 |
Definition at line 545 of file ParticleInitialConditions.c.
| enum ExpressionOp |
Definition at line 26 of file ParticleInitialConditions.c.
|
static |
Record the first parse error with its position; later ones are consequences.
Definition at line 87 of file ParticleInitialConditions.c.
|
static |
|
static |
Consume token if it is next, and report whether it was.
Definition at line 102 of file ParticleInitialConditions.c.
|
static |
Append a node to the pool and return its index, or -1 once an error is set.
Definition at line 122 of file ParticleInitialConditions.c.
|
static |
and ('or' and)*
Definition at line 348 of file ParticleInitialConditions.c.
|
static |
number | name | name(args) | (expression)
Definition at line 143 of file ParticleInitialConditions.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 270 of file ParticleInitialConditions.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 258 of file ParticleInitialConditions.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 283 of file ParticleInitialConditions.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 301 of file ParticleInitialConditions.c.
|
static |
sum (comparison sum)*; a chain a < b < c is (a < b) and (b < c).
Definition at line 312 of file ParticleInitialConditions.c.
|
static |
'not' not | comparison
Definition at line 333 of file ParticleInitialConditions.c.
|
static |
not ('and' not)*
Definition at line 340 of file ParticleInitialConditions.c.
|
static |
Emit postfix code for a subtree and track the stack depth it needs.
Definition at line 356 of file ParticleInitialConditions.c.
|
static |
Count the instructions a subtree emits (shared subtrees count per use).
Definition at line 372 of file ParticleInitialConditions.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 385 of file ParticleInitialConditions.c.
|
static |
splitmix64 finalizer: a bijective mix of 64 bits.
Definition at line 418 of file ParticleInitialConditions.c.
|
static |
Uniform on [0, 1) from the top 53 bits of a hash.
Definition at line 427 of file ParticleInitialConditions.c.
|
static |
A draw keyed on the particle, the event, the stream, and the function.
Definition at line 433 of file ParticleInitialConditions.c.
|
static |
Maximum or minimum that propagates NaN, as numpy does.
Definition at line 453 of file ParticleInitialConditions.c.
| PetscErrorCode PicurvExpressionEvaluate | ( | const PicurvExpression * | expression, |
| const PetscReal * | values, | ||
| const PicurvExpressionDrawKey * | key, | ||
| PetscReal * | result | ||
| ) |
Implementation of PicurvExpressionEvaluate().
Evaluate a compiled expression.
Definition at line 463 of file ParticleInitialConditions.c.
| PetscErrorCode PicurvExpressionDestroy | ( | PicurvExpression ** | expression | ) |
Implementation of PicurvExpressionDestroy().
Free a compiled expression.
Definition at line 527 of file ParticleInitialConditions.c.
| PetscErrorCode ParticleFieldPlanCreate | ( | ParticleFieldPlan ** | plan | ) |
Implementation of ParticleFieldPlanCreate().
Read a plan from the options database.
Definition at line 563 of file ParticleInitialConditions.c.
Global domain bounds, in solver units, from the replicated rank bounding boxes.
Definition at line 618 of file ParticleInitialConditions.c.
|
static |
Position within the domain along one axis, in [0, 1]; 0 across a flat axis.
Definition at line 640 of file ParticleInitialConditions.c.
| PetscErrorCode ParticleFieldPlanApply | ( | UserCtx * | user, |
| const ParticleFieldPlan * | plan, | ||
| const ParticleFieldEvent * | event, | ||
| PetscInt | first, | ||
| PetscInt | end | ||
| ) |
Implementation of ParticleFieldPlanApply().
Write the plan's values into swarm entries [first, end) of this rank.
Definition at line 649 of file ParticleInitialConditions.c.
| PetscErrorCode ParticleFieldPlanSummarize | ( | UserCtx * | user, |
| const ParticleFieldPlan * | plan | ||
| ) |
Implementation of ParticleFieldPlanSummarize().
Log and record the realized initial values of every planned field.
Definition at line 708 of file ParticleInitialConditions.c.
| PetscErrorCode ParticleFieldPlanDestroy | ( | ParticleFieldPlan ** | plan | ) |
Implementation of ParticleFieldPlanDestroy().
Free a plan and its compiled expressions.
Definition at line 790 of file ParticleInitialConditions.c.
| const struct { ... } kFunctions[] |
Name, arity, and opcode of every callable function.
|
static |
Names a particle expression may use, in the order their values are supplied.
Definition at line 541 of file ParticleInitialConditions.c.