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