PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
statistics_window.h
Go to the documentation of this file.
1/**
2 * @file statistics_window.h
3 * @brief Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
4 *
5 * Implements the window semantics fixed in
6 * @ref 60_Field_Statistics_Phase2_Implementation_Specification sections 2 and 4:
7 * right-rectangle weighting, final-interval clipping, and the rule that a state
8 * representing a zero-length interval is not a sample.
9 *
10 * This module decides **whether** a completed state is accepted and **what weight**
11 * it carries. It holds no PETSc objects and performs no field accumulation; the
12 * caller applies the returned weight through the moment kernels.
13 */
14
15#ifndef PICURV_STATISTICS_WINDOW_H
16#define PICURV_STATISTICS_WINDOW_H
17
18#include <petscsys.h>
19
20struct SimCtx;
21
22/** @brief Maximum stored length of a window name, including the terminator. */
23#define PICURV_WINDOW_NAME_LENGTH 64
24
25/** @brief Lifecycle state of one window. */
26typedef enum {
27 PICURV_WINDOW_PENDING = 0, /**< Requested start not yet reached. */
28 PICURV_WINDOW_ACTIVE, /**< Accepting due states. */
29 PICURV_WINDOW_COMPLETE /**< Bounded end reached; accepts nothing further. */
31
32/** @brief How an accepted state's weight is determined. */
33typedef enum {
34 PICURV_WEIGHTING_SAMPLE = 0, /**< Equal weight per accepted state. */
35 PICURV_WEIGHTING_PHYSICAL_TIME /**< Weight is the represented interval. */
37
38/** @brief Which schedule selects due states. Exactly one is used. */
39typedef enum {
40 PICURV_CADENCE_STEP = 0, /**< Every n completed steps from activation. */
41 PICURV_CADENCE_TIME /**< First state at or past each nominal time target. */
43
44/** @brief Maximum fields or covariance pairs one window may request. */
45#define PICURV_WINDOW_MAX_REQUESTS 16
46
47/** @brief One field a window accumulates. The first moment is always kept. */
48typedef struct {
49 PetscInt field_id; /**< Catalogued Eulerian field identity. */
50 PetscBool want_second; /**< Also keep the centered second moment. */
52
53/** @brief One cross-field covariance a window accumulates. */
54typedef struct {
55 PetscInt first; /**< First member; must also appear in the field list. */
56 PetscInt second; /**< Second member; must also appear in the field list. */
58
59/** @brief The scientifically immutable definition of one window. */
60typedef struct {
62 PetscReal start_time; /**< Requested start. */
63 PetscReal end_time; /**< Requested end; ignored when @c bounded is false. */
64 PetscBool bounded; /**< False for an open-ended window. */
67 PetscInt step_cadence; /**< Used when cadence_kind is step; must be positive. */
68 PetscReal time_cadence; /**< Used when cadence_kind is time; must be positive. */
69 PetscInt field_count;
74
75/** @brief Runtime state of one window. */
76typedef struct PicurvWindow {
79 PetscReal effective_start; /**< Origin of the first represented interval. */
80 PetscReal effective_end; /**< End of the last represented interval. */
81 PetscReal last_accepted_time; /**< Right edge of the last represented interval. */
82 PetscInt sample_count;
83 PetscReal total_weight;
84 PetscReal represented_time; /**< Physical time the window covers. */
85 PetscInt activation_step; /**< Step at which the window became active. */
86 PetscInt last_event_step; /**< Guards against a step being offered twice. */
87 PetscInt next_time_target; /**< k in effective_start + k*time_cadence. */
88 PetscInt restart_count; /**< Restart segments this state descends from. */
90
91/**
92 * @brief Validates a definition and initializes a window to the pending state.
93 * @param[out] window Window to initialize.
94 * @param[in] definition Requested definition; copied into the window.
95 * @return Zero on success, or `PETSC_ERR_ARG_OUTOFRANGE` for a non-positive cadence,
96 * an empty name, or a bounded window whose end does not exceed its start.
97 */
98PetscErrorCode PicurvWindowInit(PicurvWindow *window, const PicurvWindowDefinition *definition);
99
100/**
101 * @brief Offers one completed state to a window and reports the decision.
102 *
103 * Applies the interval convention in full: the state carries the interval ending
104 * at it, measured from the previous accepted state or from the effective start;
105 * a zero-length interval is not a sample; and a bounded window clips its final
106 * interval to the requested end and then completes.
107 *
108 * When @p accepted is returned true the window's bookkeeping has already been
109 * advanced, and the caller applies @p weight through the moment kernels. When it
110 * is false the window is scientifically unchanged.
111 *
112 * Offering the same step twice is rejected, so a completed state cannot be
113 * counted more than once.
114 *
115 * @param[in,out] window Window to offer the state to.
116 * @param[in] step Completed step number.
117 * @param[in] time Physical time of the completed state.
118 * @param[out] accepted Whether the state became a sample.
119 * @param[out] weight Weight to apply; zero when not accepted.
120 * @return Zero on success, or a PETSc error for a null argument.
121 */
122PetscErrorCode PicurvWindowOfferState(PicurvWindow *window, PetscInt step, PetscReal time,
123 PetscBool *accepted, PetscReal *weight);
124
125/** @brief Number of independently hashed property groups in a window definition. */
126#define PICURV_WINDOW_HASH_GROUP_COUNT 8
127
128/** @brief Stored length of one truncated group digest, including the terminator. */
129#define PICURV_WINDOW_HASH_GROUP_LENGTH 17
130
131/**
132 * @brief Computes the resolved identity hash of one window definition.
133 *
134 * Hashes the canonical serialization defined in
135 * @ref 60_Field_Statistics_Phase2_Implementation_Specification section 7, in that
136 * fixed order, so a saved window can be matched against a resolved one without
137 * storing the definition itself.
138 *
139 * `end_time` and the enabled flag are deliberately excluded, which is what lets a
140 * bounded window be extended forward and lets statistics be switched off and on
141 * without invalidating saved state.
142 *
143 * Field and covariance entries are serialized in catalog order rather than the
144 * order the user listed them, so a reordered but otherwise identical configuration
145 * continues rather than being rejected.
146 *
147 * Each property group is additionally hashed on its own. A restart that finds a
148 * mismatched full digest compares the group digests to name the first differing
149 * property, which a single digest could not do.
150 *
151 * @param[in] definition Window definition to hash.
152 * @param[out] digest_hex Full 64-character digest plus terminator.
153 * @param[out] group_digest_hex Optional per-group truncated digests; pass NULL to skip.
154 * @return Zero on success, or a PETSc error for a null argument or unknown field.
155 */
156PetscErrorCode PicurvWindowComputeHash(const PicurvWindowDefinition *definition,
157 char digest_hex[65],
158 char group_digest_hex[][PICURV_WINDOW_HASH_GROUP_LENGTH]);
159
160/**
161 * @brief Reports which hashed property group first differs from saved group digests.
162 *
163 * A checkpoint stores the group digests but never the definition itself, so this is
164 * what turns "two hashes differ" into a message naming the property that changed.
165 *
166 * @param[in] definition Resolved definition to compare against.
167 * @param[in] saved_group_digests Comma-separated group digests from a checkpoint.
168 * @param[out] group First differing group index, or -1 when the saved
169 * digests match or are too malformed to compare.
170 * @return Zero on success, or a PETSc error for a null argument or unknown field.
171 */
172PetscErrorCode PicurvWindowFirstHashDifference(const PicurvWindowDefinition *definition,
173 const char *saved_group_digests,
174 PetscInt *group);
175
176/**
177 * @brief Returns the stable name of one hashed property group.
178 * @param[in] group Group index in `[0, PICURV_WINDOW_HASH_GROUP_COUNT)`.
179 * @return Static string; `"unknown"` for an out-of-range index, never NULL.
180 */
181const char *PicurvWindowHashGroupName(PetscInt group);
182
183/**
184 * @brief Reports the fraction of a bounded window's span that has been represented.
185 * @param[in] window Window to query.
186 * @return Value in [0,1] for a bounded window, or zero for an open one.
187 */
188PetscReal PicurvWindowProgress(const PicurvWindow *window);
189
190/**
191 * @brief Returns a stable human-readable name for a window state.
192 * @param[in] state Window lifecycle state.
193 * @return Static string; never NULL.
194 */
196
197/**
198 * @brief Reports whether this run has live field-statistics state.
199 *
200 * The subsystem is active only when it is enabled, at least one window is
201 * configured, and the window array exists. Every caller that touches window or
202 * accumulator state asks this rather than restating the condition, so the three
203 * parts cannot drift apart between the runloop, the checkpoint writer, and the
204 * console monitor.
205 *
206 * @param[in] simCtx Simulation context; may be NULL.
207 * @return `PETSC_TRUE` when window state exists and may be touched.
208 */
209PetscBool FieldStatisticsIsActive(const struct SimCtx *simCtx);
210
211/**
212 * @brief Offers one completed state to every configured window.
213 *
214 * Called once per completed step from the runloop. Each due window advances its
215 * own bookkeeping independently; windows share the source state but never share
216 * accumulator state. Does nothing when field statistics are disabled or no
217 * window is configured, which is the case until configuration ingress exists.
218 *
219 * @param[in,out] simCtx Simulation context carrying the window array.
220 * @param[in] step Completed step number.
221 * @param[in] time Physical time of the completed state.
222 * @return Zero on success, or a PETSc error propagated from a window update.
223 */
224PetscErrorCode FieldStatisticsUpdateWindows(struct SimCtx *simCtx, PetscInt step, PetscReal time);
225
226#endif /* PICURV_STATISTICS_WINDOW_H */
PetscInt last_event_step
Guards against a step being offered twice.
PetscErrorCode PicurvWindowComputeHash(const PicurvWindowDefinition *definition, char digest_hex[65], char group_digest_hex[][17])
Computes the resolved identity hash of one window definition.
#define PICURV_WINDOW_HASH_GROUP_LENGTH
Stored length of one truncated group digest, including the terminator.
PetscReal effective_start
Origin of the first represented interval.
const char * PicurvWindowHashGroupName(PetscInt group)
Returns the stable name of one hashed property group.
PetscInt sample_count
PetscReal last_accepted_time
Right edge of the last represented interval.
PicurvWindowState state
PetscInt first
First member; must also appear in the field list.
PetscReal time_cadence
Used when cadence_kind is time; must be positive.
PetscInt restart_count
Restart segments this state descends from.
PetscReal effective_end
End of the last represented interval.
PetscErrorCode FieldStatisticsUpdateWindows(struct SimCtx *simCtx, PetscInt step, PetscReal time)
Offers one completed state to every configured window.
PetscReal end_time
Requested end; ignored when bounded is false.
PicurvCadenceKind cadence_kind
PetscReal total_weight
PetscInt step_cadence
Used when cadence_kind is step; must be positive.
#define PICURV_WINDOW_NAME_LENGTH
Maximum stored length of a window name, including the terminator.
PicurvWindowState
Lifecycle state of one window.
@ PICURV_WINDOW_PENDING
Requested start not yet reached.
@ PICURV_WINDOW_COMPLETE
Bounded end reached; accepts nothing further.
@ PICURV_WINDOW_ACTIVE
Accepting due states.
PetscErrorCode PicurvWindowInit(PicurvWindow *window, const PicurvWindowDefinition *definition)
Validates a definition and initializes a window to the pending state.
PetscErrorCode PicurvWindowOfferState(PicurvWindow *window, PetscInt step, PetscReal time, PetscBool *accepted, PetscReal *weight)
Offers one completed state to a window and reports the decision.
PetscBool want_second
Also keep the centered second moment.
PetscInt second
Second member; must also appear in the field list.
const char * PicurvWindowStateName(PicurvWindowState state)
Returns a stable human-readable name for a window state.
PetscBool bounded
False for an open-ended window.
PicurvWindowDefinition definition
PetscInt next_time_target
k in effective_start + k*time_cadence.
PetscBool FieldStatisticsIsActive(const struct SimCtx *simCtx)
Reports whether this run has live field-statistics state.
PetscReal start_time
Requested start.
PetscInt activation_step
Step at which the window became active.
PetscInt field_id
Catalogued Eulerian field identity.
PetscReal PicurvWindowProgress(const PicurvWindow *window)
Reports the fraction of a bounded window's span that has been represented.
#define PICURV_WINDOW_MAX_REQUESTS
Maximum fields or covariance pairs one window may request.
PicurvWeighting
How an accepted state's weight is determined.
@ PICURV_WEIGHTING_PHYSICAL_TIME
Weight is the represented interval.
@ PICURV_WEIGHTING_SAMPLE
Equal weight per accepted state.
PicurvCadenceKind
Which schedule selects due states.
@ PICURV_CADENCE_TIME
First state at or past each nominal time target.
@ PICURV_CADENCE_STEP
Every n completed steps from activation.
PetscReal represented_time
Physical time the window covers.
PetscErrorCode PicurvWindowFirstHashDifference(const PicurvWindowDefinition *definition, const char *saved_group_digests, PetscInt *group)
Reports which hashed property group first differs from saved group digests.
Runtime state of one window.
One cross-field covariance a window accumulates.
The scientifically immutable definition of one window.
One field a window accumulates.
The master context for the entire simulation.
Definition variables.h:695