PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
ParticleInitialConditions.h
Go to the documentation of this file.
1/**
2 * @file ParticleInitialConditions.h
3 * @brief Configured initial values of particle-carried fields, and the expression
4 * language that defines them.
5 *
6 * A plan binds each configured particle field to one compiled expression per component.
7 * Expressions are written in physical units: `x`, `y`, `z` are physical coordinates,
8 * `t` is physical time, and a value is divided by its field's catalog reference scale
9 * when written. The plan is applied to a contiguous local range of swarm entries, so the
10 * t=0 population and, later, particles injected at a boundary share one path.
11 *
12 * The language is the one `generators/ic.gen` validates and evaluates for Eulerian
13 * expressions; `tests/fixtures/expression_conformance.txt` holds cases both
14 * implementations must agree on.
15 */
16
17#ifndef PARTICLE_INITIAL_CONDITIONS_H
18#define PARTICLE_INITIAL_CONDITIONS_H
19
20#include "variables.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/** @brief A compiled expression: postfix code for a small stack evaluator. */
29
30/** @brief What a random draw is keyed on, besides the particle and its stream. */
31typedef struct {
32 PetscInt64 seed; /**< Run seed (`-particle_random_seed`). */
33 PetscInt64 pid; /**< Particle identity; draws are pure functions of it. */
34 PetscInt64 salt; /**< Event that produced the particle: 0 for the t=0 population. */
35 PetscInt64 field; /**< Field whose expression draws, the default stream's namespace. */
37
38/**
39 * @brief Compile one expression against a list of variable names.
40 * @details The grammar is Python's expression syntax restricted to numbers, the given
41 * names, `pi`, arithmetic (`+ - * / % **`), comparisons (chained ones included),
42 * `and`/`or`/`not`, and the functions `abs cos exp maximum minimum sin sqrt tan
43 * where`, plus `uniform` and `normal` when @p allow_random is true. Comparisons
44 * and logical operators yield 1 or 0; `%` takes the sign of the divisor; `**`
45 * binds tighter than a unary minus on its left and groups to the right.
46 * @param[in] text Expression text.
47 * @param[in] names Variable names, in the order evaluation supplies their values.
48 * @param[in] name_count Number of names.
49 * @param[in] allow_random Whether `uniform` and `normal` may be called.
50 * @param[out] expression Compiled expression, owned by the caller.
51 * @return Zero on success; `PETSC_ERR_ARG_WRONG` for a syntax error or an unknown name
52 * or function, with the offending position in the message.
53 */
54PetscErrorCode PicurvExpressionCompile(const char *text, const char *const *names, PetscInt name_count,
55 PetscBool allow_random, PicurvExpression **expression);
56
57/**
58 * @brief Evaluate a compiled expression.
59 * @param[in] expression Compiled expression.
60 * @param[in] values One value per compiled name, in compile order.
61 * @param[in] key Draw key, required when the expression draws; may be NULL otherwise.
62 * @param[out] result Value of the expression; may be non-finite, which the caller judges.
63 * @return Zero on success.
64 */
65PetscErrorCode PicurvExpressionEvaluate(const PicurvExpression *expression, const PetscReal *values,
66 const PicurvExpressionDrawKey *key, PetscReal *result);
67
68/**
69 * @brief Free a compiled expression.
70 * @param[in,out] expression Expression to free; set to NULL.
71 * @return Zero on success.
72 */
73PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression);
74
75/** @brief Compiled initial values for every configured particle field. */
77
78/** @brief When and why a plan is applied. */
79typedef struct {
80 PetscReal physical_time; /**< Time, in seconds, the evaluated particles appear at. */
81 PetscInt64 salt; /**< Event identity keying random draws: 0 for the t=0 population. */
83
84/**
85 * @brief Read a plan from the options database.
86 * @details Reads `-particle_fields_count`, and for each field `i`,
87 * `-particle_fields_<i>_name` and one expression per component,
88 * `-particle_fields_<i>_expr_<c>`. Every named field must be a particle-carried
89 * field of the catalog (capability `USER_INITIALIZE`).
90 * @param[out] plan Compiled plan, or NULL when no field is configured.
91 * @return Zero on success; `PETSC_ERR_ARG_WRONG` for an unknown or non-settable field or a
92 * malformed expression.
93 */
94PetscErrorCode ParticleFieldPlanCreate(ParticleFieldPlan **plan);
95
96/**
97 * @brief Write the plan's values into swarm entries `[first, end)` of this rank.
98 * @details Not collective: each rank evaluates its own entries, and the domain bounds for
99 * `xn`, `yn`, `zn` come from the replicated rank bounding boxes. A value that is not
100 * finite is an error naming the particle and its position.
101 * @param[in,out] user Block owning the swarm.
102 * @param[in] plan Plan to apply; NULL does nothing.
103 * @param[in] event Time and identity of the event producing these particles.
104 * @param[in] first First local entry.
105 * @param[in] end One past the last local entry.
106 * @return Zero on success.
107 */
108PetscErrorCode ParticleFieldPlanApply(UserCtx *user, const ParticleFieldPlan *plan,
109 const ParticleFieldEvent *event, PetscInt first, PetscInt end);
110
111/**
112 * @brief Log and record the realized initial values of every planned field.
113 * @details Collective. Reports count, mean, variance, minimum, maximum, and a ten-bin
114 * histogram per field component, in physical units, to the log and to
115 * `particle_initial_fields.csv` in the run's metrics directory.
116 * @param[in] user Block owning the swarm.
117 * @param[in] plan Plan whose fields are summarized; NULL does nothing.
118 * @return Zero on success.
119 */
120PetscErrorCode ParticleFieldPlanSummarize(UserCtx *user, const ParticleFieldPlan *plan);
121
122/**
123 * @brief Free a plan and its compiled expressions.
124 * @param[in,out] plan Plan to free; set to NULL.
125 * @return Zero on success.
126 */
127PetscErrorCode ParticleFieldPlanDestroy(ParticleFieldPlan **plan);
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif /* PARTICLE_INITIAL_CONDITIONS_H */
PetscErrorCode PicurvExpressionEvaluate(const PicurvExpression *expression, const PetscReal *values, const PicurvExpressionDrawKey *key, PetscReal *result)
Evaluate a compiled expression.
PetscErrorCode PicurvExpressionDestroy(PicurvExpression **expression)
Free a compiled expression.
PetscErrorCode ParticleFieldPlanCreate(ParticleFieldPlan **plan)
Read a plan from the options database.
PetscErrorCode ParticleFieldPlanSummarize(UserCtx *user, const ParticleFieldPlan *plan)
Log and record the realized initial values of every planned field.
PetscInt64 pid
Particle identity; draws are pure functions of it.
PetscInt64 salt
Event identity keying random draws: 0 for the t=0 population.
PetscErrorCode ParticleFieldPlanDestroy(ParticleFieldPlan **plan)
Free a plan and its compiled expressions.
PetscErrorCode PicurvExpressionCompile(const char *text, const char *const *names, PetscInt name_count, PetscBool allow_random, PicurvExpression **expression)
Compile one expression against a list of variable names.
PetscInt64 salt
Event that produced the particle: 0 for the t=0 population.
PetscInt64 field
Field whose expression draws, the default stream's namespace.
PetscErrorCode ParticleFieldPlanApply(UserCtx *user, const ParticleFieldPlan *plan, const ParticleFieldEvent *event, PetscInt first, PetscInt end)
Write the plan's values into swarm entries [first, end) of this rank.
PetscReal physical_time
Time, in seconds, the evaluated particles appear at.
PetscInt64 seed
Run seed (-particle_random_seed).
When and why a plan is applied.
What a random draw is keyed on, besides the particle and its stream.
Typed identities and metadata for persistent solver-particle fields.
Main header file for a complex fluid dynamics solver.
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1071