PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Macros | Functions | Variables
statistics_window.c File Reference

Window lifecycle, scheduling, and weighting for the field-statistics pipeline. More...

#include "statistics_window.h"
#include "variables.h"
#include "logging.h"
#include "statistics_accumulator.h"
#include "field_catalog.h"
#include "checksum.h"
Include dependency graph for statistics_window.c:

Go to the source code of this file.

Macros

#define WINDOW_TIME_EPSILON   1.0e-12
 Tolerance for treating two physical times as the same instant.
 

Functions

const char * PicurvWindowStateName (PicurvWindowState state)
 Implementation of PicurvWindowStateName().
 
PetscErrorCode PicurvWindowInit (PicurvWindow *window, const PicurvWindowDefinition *definition)
 Implementation of PicurvWindowInit().
 
static PetscBool WindowStateIsDue (const PicurvWindow *window, PetscInt step, PetscReal time)
 Internal helper: reports whether a state is due under the window's schedule.
 
static void WindowAdvanceTimeTarget (PicurvWindow *window, PetscReal time)
 Internal helper: advances the time-cadence target past the accepted time.
 
PetscErrorCode PicurvWindowOfferState (PicurvWindow *window, PetscInt step, PetscReal time, PetscBool *accepted, PetscReal *weight)
 Implementation of PicurvWindowOfferState().
 
const char * PicurvWindowHashGroupName (PetscInt group)
 Implementation of PicurvWindowHashGroupName().
 
static void SortRequestOrder (PetscInt count, const PetscInt *primary, const PetscInt *secondary, PetscInt *order)
 Internal helper: orders request indices so listing order cannot change the hash.
 
PetscErrorCode PicurvWindowComputeHash (const PicurvWindowDefinition *definition, char digest_hex[65], char group_digest_hex[][PICURV_WINDOW_HASH_GROUP_LENGTH])
 Implementation of PicurvWindowComputeHash().
 
PetscErrorCode PicurvWindowFirstHashDifference (const PicurvWindowDefinition *definition, const char *saved_group_digests, PetscInt *group)
 Implementation of PicurvWindowFirstHashDifference().
 
PetscReal PicurvWindowProgress (const PicurvWindow *window)
 Implementation of PicurvWindowProgress().
 
PetscBool FieldStatisticsIsActive (const SimCtx *simCtx)
 Implementation of FieldStatisticsIsActive().
 
PetscBool FieldStatisticsNeedsWorkspace (const SimCtx *simCtx)
 Implementation of FieldStatisticsNeedsWorkspace().
 
PetscErrorCode FieldStatisticsUpdateWindows (SimCtx *simCtx, PetscInt step, PetscReal time)
 Implementation of FieldStatisticsUpdateWindows().
 

Variables

static const char *const kHashGroupNames [PICURV_WINDOW_HASH_GROUP_COUNT]
 Stable names of the hashed property groups, in serialization order.
 

Detailed Description

Window lifecycle, scheduling, and weighting for the field-statistics pipeline.

Full API contract is documented with the declarations in include/statistics_window.h.

Definition in file statistics_window.c.

Macro Definition Documentation

◆ WINDOW_TIME_EPSILON

#define WINDOW_TIME_EPSILON   1.0e-12

Tolerance for treating two physical times as the same instant.

Definition at line 17 of file statistics_window.c.

Function Documentation

◆ PicurvWindowStateName()

const char * PicurvWindowStateName ( PicurvWindowState  state)

Implementation of PicurvWindowStateName().

Returns a stable human-readable name for a window state.

See also
PicurvWindowStateName()

Definition at line 23 of file statistics_window.c.

24{
26 switch (state) {
27 case PICURV_WINDOW_PENDING: PROFILE_FUNCTION_END; return "pending";
28 case PICURV_WINDOW_ACTIVE: PROFILE_FUNCTION_END; return "active";
29 case PICURV_WINDOW_COMPLETE: PROFILE_FUNCTION_END; return "complete";
30 default: PROFILE_FUNCTION_END; return "unknown";
31 }
33}
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
@ PICURV_WINDOW_PENDING
Requested start not yet reached.
@ PICURV_WINDOW_COMPLETE
Bounded end reached; accepts nothing further.
@ PICURV_WINDOW_ACTIVE
Accepting due states.
Here is the caller graph for this function:

◆ PicurvWindowInit()

PetscErrorCode PicurvWindowInit ( PicurvWindow *  window,
const PicurvWindowDefinition *  definition 
)

Implementation of PicurvWindowInit().

Validates a definition and initializes a window to the pending state.

See also
PicurvWindowInit()

Definition at line 39 of file statistics_window.c.

40{
41 PetscFunctionBeginUser;
43 PetscCheck(window != NULL && definition != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
44 "Window and definition are required.");
45 PetscCheck(definition->name[0] != '\0', PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
46 "Window name must not be empty.");
47 if (definition->cadence_kind == PICURV_CADENCE_STEP) {
48 PetscCheck(definition->step_cadence > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
49 "Window '%s' needs a positive step cadence, got %" PetscInt_FMT ".",
50 definition->name, definition->step_cadence);
51 } else {
52 PetscCheck(definition->time_cadence > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
53 "Window '%s' needs a positive time cadence, got %g.",
54 definition->name, (double)definition->time_cadence);
55 }
56 PetscCheck(!definition->bounded || definition->end_time > definition->start_time,
57 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
58 "Window '%s' must end after it starts.", definition->name);
59
60 PetscCall(PetscMemzero(window, sizeof(*window)));
61 window->definition = *definition;
63 /* Anchoring the interval origin at the requested start, rather than at the
64 * first accepted state, is what lets the first sample represent the interval
65 * back to the requested bound instead of silently dropping it. */
66 window->effective_start = definition->start_time;
67 window->effective_end = definition->start_time;
68 window->last_accepted_time = definition->start_time;
69 window->activation_step = -1;
70 window->last_event_step = PETSC_MIN_INT;
71 window->next_time_target = 0;
73 PetscFunctionReturn(0);
74}
PetscInt last_event_step
Guards against a step being offered twice.
PetscReal effective_start
Origin of the first represented interval.
PetscReal last_accepted_time
Right edge of the last represented interval.
PicurvWindowState state
PetscReal time_cadence
Used when cadence_kind is time; must be positive.
PetscReal effective_end
End of the last represented interval.
PetscReal end_time
Requested end; ignored when bounded is false.
PicurvCadenceKind cadence_kind
PetscInt step_cadence
Used when cadence_kind is step; must be positive.
PetscBool bounded
False for an open-ended window.
PicurvWindowDefinition definition
PetscInt next_time_target
k in effective_start + k*time_cadence.
PetscReal start_time
Requested start.
PetscInt activation_step
Step at which the window became active.
@ PICURV_CADENCE_STEP
Every n completed steps from activation.
Here is the caller graph for this function:

◆ WindowStateIsDue()

static PetscBool WindowStateIsDue ( const PicurvWindow *  window,
PetscInt  step,
PetscReal  time 
)
static

Internal helper: reports whether a state is due under the window's schedule.

Local to this translation unit. Advances no state.

Definition at line 80 of file statistics_window.c.

81{
84 /* The activation step is itself due; subsequent ones follow the stride. */
85 if (window->activation_step < 0) { PROFILE_HELPER_END; return PETSC_TRUE; }
87 return (PetscBool)(((step - window->activation_step) % window->definition.step_cadence) == 0);
88 }
89 /* Time cadence: targets sit on the absolute grid effective_start + k*cadence, so
90 * the schedule cannot drift as dt varies. A state is due once it reaches the
91 * next outstanding target. */
92 {
93 const PetscReal target = window->effective_start +
94 (PetscReal)window->next_time_target * window->definition.time_cadence;
96 return (PetscBool)(time >= target - WINDOW_TIME_EPSILON);
97 }
99}
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
Definition logging.h:1014
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
Definition logging.h:1033
#define WINDOW_TIME_EPSILON
Tolerance for treating two physical times as the same instant.
Here is the caller graph for this function:

◆ WindowAdvanceTimeTarget()

static void WindowAdvanceTimeTarget ( PicurvWindow *  window,
PetscReal  time 
)
static

Internal helper: advances the time-cadence target past the accepted time.

Local to this translation unit. A single large step may overshoot several targets; they are consumed at once, because the accepted state's weight is the actual elapsed interval and so already accounts for the whole span.

Definition at line 107 of file statistics_window.c.

108{
111 while (window->effective_start +
112 (PetscReal)window->next_time_target * window->definition.time_cadence
113 <= time + WINDOW_TIME_EPSILON) {
114 window->next_time_target += 1;
115 }
117}
@ PICURV_CADENCE_TIME
First state at or past each nominal time target.
Here is the caller graph for this function:

◆ PicurvWindowOfferState()

PetscErrorCode PicurvWindowOfferState ( PicurvWindow *  window,
PetscInt  step,
PetscReal  time,
PetscBool *  accepted,
PetscReal *  weight 
)

Implementation of PicurvWindowOfferState().

Offers one completed state to a window and reports the decision.

See also
PicurvWindowOfferState()

Definition at line 123 of file statistics_window.c.

125{
126 PetscReal interval = 0.0;
127 PetscReal right_edge = 0.0;
128 PetscBool closes = PETSC_FALSE;
129
130 PetscFunctionBeginUser;
132 PetscCheck(window != NULL && accepted != NULL && weight != NULL,
133 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
134 "Window, acceptance flag, and weight output are required.");
135 *accepted = PETSC_FALSE;
136 *weight = 0.0;
137
138 if (window->state == PICURV_WINDOW_COMPLETE) { PROFILE_FUNCTION_END; PetscFunctionReturn(0); }
139 /* A completed state is accepted at most once, so a step already seen is
140 * rejected outright rather than double counted. */
141 if (step == window->last_event_step) { PROFILE_FUNCTION_END; PetscFunctionReturn(0); }
142 if (time < window->definition.start_time - WINDOW_TIME_EPSILON) { PROFILE_FUNCTION_END; PetscFunctionReturn(0); }
143
144 /* First observation: a window resumed later than its requested start cannot
145 * claim represented time it never saw, so the origin moves forward. */
146 if (window->state == PICURV_WINDOW_PENDING) {
147 if (time > window->definition.start_time + WINDOW_TIME_EPSILON) {
148 window->effective_start = time;
149 window->last_accepted_time = time;
150 }
151 window->state = PICURV_WINDOW_ACTIVE;
152 window->activation_step = step;
153 window->effective_end = window->effective_start;
154 window->next_time_target = 0;
156 }
157
158 if (!WindowStateIsDue(window, step, time)) {
159 /* An active off-schedule state changes no scientific state. */
161 PetscFunctionReturn(0);
162 }
163 window->last_event_step = step;
164
165 /* Clip the final represented interval to the requested bound. */
166 right_edge = time;
167 if (window->definition.bounded && time >= window->definition.end_time - WINDOW_TIME_EPSILON) {
168 right_edge = window->definition.end_time;
169 closes = PETSC_TRUE;
170 }
171
172 interval = right_edge - window->last_accepted_time;
173 WindowAdvanceTimeTarget(window, time);
174
175 if (interval <= WINDOW_TIME_EPSILON) {
176 /* A zero-length interval is not a sample. It still anchors the origin, which
177 * is what makes initial-state handling identical under both weightings. */
178 window->last_accepted_time = right_edge;
179 window->effective_end = right_edge;
180 if (closes) window->state = PICURV_WINDOW_COMPLETE;
182 PetscFunctionReturn(0);
183 }
184
185 *weight = (window->definition.weighting == PICURV_WEIGHTING_SAMPLE) ? 1.0 : interval;
186 *accepted = PETSC_TRUE;
187
188 window->last_accepted_time = right_edge;
189 window->effective_end = right_edge;
190 window->sample_count += 1;
191 window->total_weight += *weight;
192 window->represented_time += interval;
193 if (closes) window->state = PICURV_WINDOW_COMPLETE;
195 PetscFunctionReturn(0);
196}
static void WindowAdvanceTimeTarget(PicurvWindow *window, PetscReal time)
Internal helper: advances the time-cadence target past the accepted time.
static PetscBool WindowStateIsDue(const PicurvWindow *window, PetscInt step, PetscReal time)
Internal helper: reports whether a state is due under the window's schedule.
PetscInt sample_count
PetscReal total_weight
@ PICURV_WEIGHTING_SAMPLE
Equal weight per accepted state.
PetscReal represented_time
Physical time the window covers.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PicurvWindowHashGroupName()

const char * PicurvWindowHashGroupName ( PetscInt  group)

Implementation of PicurvWindowHashGroupName().

Returns the stable name of one hashed property group.

See also
PicurvWindowHashGroupName()

Definition at line 207 of file statistics_window.c.

208{
210 if (group < 0 || group >= PICURV_WINDOW_HASH_GROUP_COUNT) { PROFILE_HELPER_END; return "unknown"; }
212 return kHashGroupNames[group];
213}
static const char *const kHashGroupNames[PICURV_WINDOW_HASH_GROUP_COUNT]
Stable names of the hashed property groups, in serialization order.
#define PICURV_WINDOW_HASH_GROUP_COUNT
Number of independently hashed property groups in a window definition.
Here is the caller graph for this function:

◆ SortRequestOrder()

static void SortRequestOrder ( PetscInt  count,
const PetscInt *  primary,
const PetscInt *  secondary,
PetscInt *  order 
)
static

Internal helper: orders request indices so listing order cannot change the hash.

Local to this translation unit. Insertion sort over at most PICURV_WINDOW_MAX_REQUESTS entries, keyed on a primary and secondary integer so the same routine serves both fields and covariance pairs.

Definition at line 221 of file statistics_window.c.

223{
225 for (PetscInt n = 0; n < count; ++n) order[n] = n;
226 for (PetscInt n = 1; n < count; ++n) {
227 const PetscInt candidate = order[n];
228 PetscInt m = n - 1;
229
230 while (m >= 0 &&
231 (primary[order[m]] > primary[candidate] ||
232 (primary[order[m]] == primary[candidate] &&
233 secondary[order[m]] > secondary[candidate]))) {
234 order[m + 1] = order[m];
235 --m;
236 }
237 order[m + 1] = candidate;
238 }
240}
Here is the caller graph for this function:

◆ PicurvWindowComputeHash()

PetscErrorCode PicurvWindowComputeHash ( const PicurvWindowDefinition *  definition,
char  digest_hex[65],
char  group_digest_hex[][PICURV_WINDOW_HASH_GROUP_LENGTH] 
)

Implementation of PicurvWindowComputeHash().

See also
PicurvWindowComputeHash()

Definition at line 246 of file statistics_window.c.

249{
251 PetscInt field_primary[PICURV_WINDOW_MAX_REQUESTS];
252 PetscInt field_secondary[PICURV_WINDOW_MAX_REQUESTS];
253 PetscInt field_order[PICURV_WINDOW_MAX_REQUESTS];
254 PetscInt pair_primary[PICURV_WINDOW_MAX_REQUESTS];
255 PetscInt pair_secondary[PICURV_WINDOW_MAX_REQUESTS];
256 PetscInt pair_order[PICURV_WINDOW_MAX_REQUESTS];
257
258 PetscFunctionBeginUser;
260 PetscCheck(definition != NULL && digest_hex != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
261 "Definition and digest output are required.");
262 PetscCheck(definition->field_count >= 0 && definition->field_count <= PICURV_WINDOW_MAX_REQUESTS,
263 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
264 "Window '%s' requests %" PetscInt_FMT " fields; at most %d are supported.",
265 definition->name, definition->field_count, PICURV_WINDOW_MAX_REQUESTS);
266 PetscCheck(definition->covariance_count >= 0 &&
268 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
269 "Window '%s' requests %" PetscInt_FMT " covariances; at most %d are supported.",
270 definition->name, definition->covariance_count, PICURV_WINDOW_MAX_REQUESTS);
271
272 for (PetscInt field_index = 0; field_index < definition->field_count; ++field_index) {
273 field_primary[field_index] = definition->fields[field_index].field_id;
274 field_secondary[field_index] = 0;
275 }
276 SortRequestOrder(definition->field_count, field_primary, field_secondary, field_order);
277 for (PetscInt pair_index = 0; pair_index < definition->covariance_count; ++pair_index) {
278 const PetscInt a = definition->covariances[pair_index].first;
279 const PetscInt b = definition->covariances[pair_index].second;
280
281 /* Covariance is symmetric, so the pair is canonicalized here rather than
282 * requiring the user to list its members in catalog order. */
283 pair_primary[pair_index] = PetscMin(a, b);
284 pair_secondary[pair_index] = PetscMax(a, b);
285 }
286 SortRequestOrder(definition->covariance_count, pair_primary, pair_secondary, pair_order);
287
288 PicurvSHA256Init(&whole);
289 for (PetscInt group = 0; group < PICURV_WINDOW_HASH_GROUP_COUNT; ++group) {
290 char text[1024];
291 char group_digest[65];
293 size_t used = 0;
294 size_t length = 0;
295
296 text[0] = '\0';
297 switch (group) {
298 case 0:
299 PetscCall(PetscSNPrintf(text, sizeof(text), "name=%s\n", definition->name));
300 break;
301 case 1:
302 PetscCall(PetscSNPrintf(text, sizeof(text), "start_time=%.17g\n",
303 (double)definition->start_time));
304 break;
305 case 2:
306 PetscCall(PetscSNPrintf(text, sizeof(text), "weighting=%s\n",
308 ? "sample" : "physical_time"));
309 break;
310 case 3:
311 if (definition->cadence_kind == PICURV_CADENCE_STEP) {
312 PetscCall(PetscSNPrintf(text, sizeof(text), "cadence=step:%" PetscInt_FMT "\n",
313 definition->step_cadence));
314 } else {
315 PetscCall(PetscSNPrintf(text, sizeof(text), "cadence=time:%.17g\n",
316 (double)definition->time_cadence));
317 }
318 break;
319 case 4:
320 for (PetscInt n = 0; n < definition->field_count; ++n) {
321 const PicurvWindowFieldRequest *request = &definition->fields[field_order[n]];
322 const char *name = FieldCanonicalName((FieldId)request->field_id);
323
324 PetscCheck(name != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
325 "Window '%s' requests unknown field id %" PetscInt_FMT ".",
326 definition->name, request->field_id);
327 PetscCall(PetscStrlen(text, &used));
328 PetscCall(PetscSNPrintf(text + used, sizeof(text) - used, "field=%s:%s\n",
329 name, request->want_second ? "first,second" : "first"));
330 }
331 break;
332 case 5:
333 for (PetscInt n = 0; n < definition->covariance_count; ++n) {
334 const PetscInt index = pair_order[n];
335 const char *first = FieldCanonicalName((FieldId)pair_primary[index]);
336 const char *second = FieldCanonicalName((FieldId)pair_secondary[index]);
337
338 PetscCheck(first != NULL && second != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
339 "Window '%s' requests a covariance over an unknown field id.",
340 definition->name);
341 PetscCall(PetscStrlen(text, &used));
342 PetscCall(PetscSNPrintf(text + used, sizeof(text) - used, "covariance=%s,%s\n",
343 first, second));
344 }
345 break;
346 case 6:
347 /* One point mask is resolved, so this group starts from a constant. It
348 * exists so that a future mask key extends this group instead of
349 * renumbering every group after it and invalidating saved state. A
350 * particle-sampled field is valid only in occupied states, which is such a
351 * key: listing it changes what earlier state for that field means, while a
352 * window without one hashes exactly as before. */
353 PetscCall(PetscSNPrintf(text, sizeof(text), "mask=fluid\n"));
354 for (PetscInt n = 0; n < definition->field_count; ++n) {
355 const FieldDescriptor *descriptor = NULL;
356
357 PetscCall(FieldGetDescriptor((FieldId)definition->fields[field_order[n]].field_id, &descriptor));
358 if (!(descriptor->capabilities & FIELD_CAPABILITY_PARTICLE_SAMPLED)) continue;
359 PetscCall(PetscStrlen(text, &used));
360 PetscCall(PetscSNPrintf(text + used, sizeof(text) - used, "valid=%s:particles\n",
361 descriptor->canonical_name));
362 }
363 break;
364 default:
365 PetscCall(PetscSNPrintf(text, sizeof(text), "target=pointwise\n"));
366 for (PetscInt n = 0; n < definition->field_count; ++n) {
367 const PicurvWindowFieldRequest *request = &definition->fields[field_order[n]];
368 const FieldDescriptor *descriptor = NULL;
369
370 PetscCall(FieldGetDescriptor((FieldId)request->field_id, &descriptor));
371 PetscCall(PetscStrlen(text, &used));
372 PetscCall(PetscSNPrintf(text + used, sizeof(text) - used, "layout=%s:%s\n",
373 descriptor->canonical_name,
374 FieldLayoutName(descriptor->layout)));
375 }
376 break;
377 }
378
379 PetscCall(PetscStrlen(text, &length));
380 PicurvSHA256Update(&whole, text, length);
381 if (group_digest_hex) {
382 PicurvSHA256Init(&part);
383 PicurvSHA256Update(&part, text, length);
384 PicurvSHA256FinalHex(&part, group_digest);
385 PetscCall(PetscStrncpy(group_digest_hex[group], group_digest,
387 }
388 }
389 PicurvSHA256FinalHex(&whole, digest_hex);
391 PetscFunctionReturn(0);
392}
void PicurvSHA256Init(PicurvSHA256Context *context)
Initialize an incremental SHA-256 calculation.
Definition checksum.c:62
void PicurvSHA256Update(PicurvSHA256Context *context, const void *data, size_t length)
Add bytes to an incremental SHA-256 calculation.
Definition checksum.c:74
void PicurvSHA256FinalHex(PicurvSHA256Context *context, char digest_hex[65])
Finish a SHA-256 calculation and return a lowercase hexadecimal digest.
Definition checksum.c:98
Incremental SHA-256 state.
Definition checksum.h:14
FieldLayout layout
@ FIELD_CAPABILITY_PARTICLE_SAMPLED
Defined only where a cell holds particles; elsewhere the stored value is a placeholder,...
unsigned int capabilities
const char * FieldCanonicalName(FieldId field_id)
Return the canonical printable name for an ID.
const char * canonical_name
const char * FieldLayoutName(FieldLayout layout)
Return a stable printable label for a field layout.
PetscErrorCode FieldGetDescriptor(FieldId field_id, const FieldDescriptor **descriptor)
Return immutable metadata for a valid field identifier.
FieldId
Compile-time identity for a catalogued Eulerian field.
Immutable metadata for one field identity.
static void SortRequestOrder(PetscInt count, const PetscInt *primary, const PetscInt *secondary, PetscInt *order)
Internal helper: orders request indices so listing order cannot change the hash.
#define PICURV_WINDOW_HASH_GROUP_LENGTH
Stored length of one truncated group digest, including the terminator.
PetscInt first
First member; must also appear in the field list.
PicurvWindowFieldRequest fields[16]
PetscBool want_second
Also keep the centered second moment.
PetscInt second
Second member; must also appear in the field list.
PicurvWindowCovarianceRequest covariances[16]
PetscInt field_id
Catalogued Eulerian field identity.
#define PICURV_WINDOW_MAX_REQUESTS
Maximum fields or covariance pairs one window may request.
One field a window accumulates.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PicurvWindowFirstHashDifference()

PetscErrorCode PicurvWindowFirstHashDifference ( const PicurvWindowDefinition *  definition,
const char *  saved_group_digests,
PetscInt *  group 
)

Implementation of PicurvWindowFirstHashDifference().

Reports which hashed property group first differs from saved group digests.

See also
PicurvWindowFirstHashDifference()

Definition at line 398 of file statistics_window.c.

401{
402 char digest[65];
404 const char *cursor = saved_group_digests;
405
406 PetscFunctionBeginUser;
408 PetscCheck(definition != NULL && group != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
409 "Definition and group output are required.");
410 *group = -1;
411 if (!saved_group_digests) { PROFILE_HELPER_END; PetscFunctionReturn(0); }
412 PetscCall(PicurvWindowComputeHash(definition, digest, current));
413
414 for (PetscInt index = 0; index < PICURV_WINDOW_HASH_GROUP_COUNT; ++index) {
415 const char *comma = strchr(cursor, ',');
416 const size_t length = comma ? (size_t)(comma - cursor) : strlen(cursor);
417 size_t expected = 0;
418
419 PetscCall(PetscStrlen(current[index], &expected));
420 /* Every well-formed digest is fixed width, so a short segment means the
421 * saved list is malformed rather than that this property changed. A
422 * malformed list localizes to nothing: reporting the wrong property would
423 * be worse than reporting none. */
424 if (length != expected) { PROFILE_HELPER_END; PetscFunctionReturn(0); }
425 if (strncmp(cursor, current[index], length)) {
426 *group = index;
428 PetscFunctionReturn(0);
429 }
430 if (!comma) { PROFILE_HELPER_END; PetscFunctionReturn(0); }
431 cursor = comma + 1;
432 }
434 PetscFunctionReturn(0);
435}
PetscErrorCode PicurvWindowComputeHash(const PicurvWindowDefinition *definition, char digest_hex[65], char group_digest_hex[][PICURV_WINDOW_HASH_GROUP_LENGTH])
Implementation of PicurvWindowComputeHash().
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PicurvWindowProgress()

PetscReal PicurvWindowProgress ( const PicurvWindow *  window)

Implementation of PicurvWindowProgress().

Reports the fraction of a bounded window's span that has been represented.

See also
PicurvWindowProgress()

Definition at line 441 of file statistics_window.c.

442{
444 PetscReal span = 0.0;
445
446 if (window == NULL || !window->definition.bounded) { PROFILE_HELPER_END; return 0.0; }
447 span = window->definition.end_time - window->effective_start;
448 if (span <= 0.0) { PROFILE_HELPER_END; return 0.0; }
450 return PetscMin(1.0, window->represented_time / span);
451}
Here is the caller graph for this function:

◆ FieldStatisticsIsActive()

PetscBool FieldStatisticsIsActive ( const SimCtx *  simCtx)

Implementation of FieldStatisticsIsActive().

See also
FieldStatisticsIsActive()

Definition at line 457 of file statistics_window.c.

458{
460 if (!simCtx || !simCtx->fieldStatisticsEnabled) { PROFILE_FUNCTION_END; return PETSC_FALSE; }
462 return (PetscBool)(simCtx->fieldStatisticsWindowCount > 0 &&
463 simCtx->fieldStatisticsWindows != NULL);
464}
PetscInt fieldStatisticsWindowCount
Definition variables.h:989
PetscBool fieldStatisticsEnabled
Definition variables.h:988
struct PicurvWindow * fieldStatisticsWindows
Definition variables.h:990
Here is the caller graph for this function:

◆ FieldStatisticsNeedsWorkspace()

PetscBool FieldStatisticsNeedsWorkspace ( const SimCtx *  simCtx)

Implementation of FieldStatisticsNeedsWorkspace().

See also
FieldStatisticsNeedsWorkspace()

Definition at line 470 of file statistics_window.c.

471{
473 if (FieldStatisticsIsActive(simCtx)) { PROFILE_FUNCTION_END; return PETSC_TRUE; }
475 return (PetscBool)(simCtx && simCtx->pps && simCtx->pps->field_statistics_ensemble_count > 0);
476}
PetscBool FieldStatisticsIsActive(const SimCtx *simCtx)
Implementation of FieldStatisticsIsActive().
PetscInt field_statistics_ensemble_count
Recipe-defined single-instant windows, derived at every processed step.
Definition variables.h:834
PostProcessParams * pps
Definition variables.h:1131
Here is the call graph for this function:

◆ FieldStatisticsUpdateWindows()

PetscErrorCode FieldStatisticsUpdateWindows ( SimCtx *  simCtx,
PetscInt  step,
PetscReal  time 
)

Implementation of FieldStatisticsUpdateWindows().

Offers one completed state to every configured window.

See also
FieldStatisticsUpdateWindows()

Definition at line 482 of file statistics_window.c.

483{
484 PetscFunctionBeginUser;
486 PetscCheck(simCtx != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "SimCtx cannot be NULL.");
487 if (!FieldStatisticsIsActive(simCtx)) { PROFILE_FUNCTION_END; PetscFunctionReturn(0); }
488
489 for (PetscInt window_index = 0; window_index < simCtx->fieldStatisticsWindowCount; ++window_index) {
490 PicurvWindow *window = &simCtx->fieldStatisticsWindows[window_index];
491 const PicurvWindowState before = window->state;
492 PetscBool accepted = PETSC_FALSE;
493 PetscReal weight = 0.0;
494
495 PetscCall(PicurvWindowOfferState(window, step, time, &accepted, &weight));
496
497 if (before == PICURV_WINDOW_PENDING && window->state == PICURV_WINDOW_ACTIVE) {
499 "Statistics window '%s' active from t=%.6g.\n",
500 window->definition.name, (double)window->effective_start);
501 }
502 if (accepted) {
503 /* Apply the accepted weight to every block's accumulators. Windows share
504 * the source state but own independent accumulator state. */
505 for (PetscInt bi = 0; bi < simCtx->block_number; ++bi) {
506 UserCtx *user = &simCtx->usermg.mgctx[simCtx->usermg.mglevels - 1].user[bi];
507 if (!user->fieldStatisticsStorage) continue;
508 PetscCall(PicurvWindowAccumulate(user, &window->definition,
509 &user->fieldStatisticsStorage[window_index], weight));
510 }
512 "Statistics window '%s' accepted step %" PetscInt_FMT " at t=%.6g with weight %.6g "
513 "(samples=%" PetscInt_FMT ", represented=%.6g).\n",
514 window->definition.name, step, (double)time, (double)weight,
515 window->sample_count, (double)window->represented_time);
516 } else if (window->state == PICURV_WINDOW_ACTIVE) {
517 /* An active window that took nothing was either off schedule or offered a
518 * zero-length interval. Both are expected, so this is the level that
519 * explains a sample count without implying anything is wrong. */
521 "Statistics window '%s' did not sample step %" PetscInt_FMT " at t=%.6g "
522 "(last accepted t=%.6g, samples=%" PetscInt_FMT ").\n",
523 window->definition.name, step, (double)time,
524 (double)window->last_accepted_time, window->sample_count);
525 }
526 if (before != PICURV_WINDOW_COMPLETE && window->state == PICURV_WINDOW_COMPLETE) {
528 "Statistics window '%s' complete: %" PetscInt_FMT " sample(s), total weight %.6g, "
529 "represented time %.6g.\n",
530 window->definition.name, window->sample_count,
531 (double)window->total_weight, (double)window->represented_time);
532 if (window->sample_count == 0) {
534 "Statistics window '%s' completed without accepting any sample.\n",
535 window->definition.name);
536 } else {
537 UserCtx *user = simCtx->usermg.mgctx[simCtx->usermg.mglevels - 1].user;
538
539 /* A point that was never valid carries no average at all, which a
540 * reader of the mean field cannot see. Report it once, at the only
541 * moment the final coverage is known. The reduction is collective and
542 * runs once per window, not per step. */
543 if (user && user->fieldStatisticsStorage) {
544 PetscReal lowest = 1.0, highest = 0.0;
545
546 PetscCall(PicurvWindowValidFractionRange(user, &window->definition,
547 &user->fieldStatisticsStorage[window_index],
548 window->sample_count, &lowest, &highest));
549 if (lowest <= 0.0) {
551 "Statistics window '%s' completed with points that were never "
552 "valid; their mean and moments are undefined.\n",
553 window->definition.name);
554 } else {
556 "Statistics window '%s' per-point valid fraction spans [%.3f, %.3f].\n",
557 window->definition.name, (double)lowest, (double)highest);
558 }
559 }
560 }
561 }
562 }
564 PetscFunctionReturn(0);
565}
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
Definition logging.h:214
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
PetscErrorCode PicurvWindowValidFractionRange(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, PetscInt sample_count, PetscReal *minimum, PetscReal *maximum)
Reports the range of per-point valid fraction across a window's domain.
PetscErrorCode PicurvWindowAccumulate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage, PetscReal weight)
Applies one accepted completed state to a window's accumulators.
PetscErrorCode PicurvWindowOfferState(PicurvWindow *window, PetscInt step, PetscReal time, PetscBool *accepted, PetscReal *weight)
Implementation of PicurvWindowOfferState().
PicurvWindowState
Lifecycle state of one window.
Runtime state of one window.
UserCtx * user
Definition variables.h:768
PetscInt block_number
Definition variables.h:1009
UserMG usermg
Definition variables.h:1082
PetscInt mglevels
Definition variables.h:775
struct PicurvWindowStorage * fieldStatisticsStorage
Definition variables.h:1222
MGCtx * mgctx
Definition variables.h:778
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ kHashGroupNames

const char* const kHashGroupNames[PICURV_WINDOW_HASH_GROUP_COUNT]
static
Initial value:
= {
"name", "start_time", "weighting", "cadence", "fields", "covariances", "mask", "target"
}

Stable names of the hashed property groups, in serialization order.

Definition at line 199 of file statistics_window.c.

199 {
200 "name", "start_time", "weighting", "cadence", "fields", "covariances", "mask", "target"
201};