17#define WINDOW_TIME_EPSILON 1.0e-12
29 default:
return "unknown";
39 PetscFunctionBeginUser;
40 PetscCheck(window != NULL && definition != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
41 "Window and definition are required.");
42 PetscCheck(definition->
name[0] !=
'\0', PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
43 "Window name must not be empty.");
45 PetscCheck(definition->
step_cadence > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
46 "Window '%s' needs a positive step cadence, got %" PetscInt_FMT
".",
49 PetscCheck(definition->
time_cadence > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
50 "Window '%s' needs a positive time cadence, got %g.",
54 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
55 "Window '%s' must end after it starts.", definition->
name);
57 PetscCall(PetscMemzero(window,
sizeof(*window)));
69 PetscFunctionReturn(0);
114 PetscBool *accepted, PetscReal *weight)
116 PetscReal interval = 0.0;
117 PetscReal right_edge = 0.0;
118 PetscBool closes = PETSC_FALSE;
120 PetscFunctionBeginUser;
121 PetscCheck(window != NULL && accepted != NULL && weight != NULL,
122 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
123 "Window, acceptance flag, and weight output are required.");
124 *accepted = PETSC_FALSE;
149 PetscFunctionReturn(0);
169 PetscFunctionReturn(0);
173 *accepted = PETSC_TRUE;
181 PetscFunctionReturn(0);
186 "name",
"start_time",
"weighting",
"cadence",
"fields",
"covariances",
"mask",
"target"
205static void SortRequestOrder(PetscInt count,
const PetscInt *primary,
const PetscInt *secondary,
208 for (PetscInt n = 0; n < count; ++n) order[n] = n;
209 for (PetscInt n = 1; n < count; ++n) {
210 const PetscInt candidate = order[n];
214 (primary[order[m]] > primary[candidate] ||
215 (primary[order[m]] == primary[candidate] &&
216 secondary[order[m]] > secondary[candidate]))) {
217 order[m + 1] = order[m];
220 order[m + 1] = candidate;
240 PetscFunctionBeginUser;
241 PetscCheck(definition != NULL && digest_hex != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
242 "Definition and digest output are required.");
244 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
245 "Window '%s' requests %" PetscInt_FMT
" fields; at most %d are supported.",
249 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
250 "Window '%s' requests %" PetscInt_FMT
" covariances; at most %d are supported.",
253 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
254 field_primary[field_index] = definition->
fields[field_index].
field_id;
255 field_secondary[field_index] = 0;
258 for (PetscInt pair_index = 0; pair_index < definition->
covariance_count; ++pair_index) {
264 pair_primary[pair_index] = PetscMin(a, b);
265 pair_secondary[pair_index] = PetscMax(a, b);
272 char group_digest[65];
280 PetscCall(PetscSNPrintf(text,
sizeof(text),
"name=%s\n", definition->
name));
283 PetscCall(PetscSNPrintf(text,
sizeof(text),
"start_time=%.17g\n",
287 PetscCall(PetscSNPrintf(text,
sizeof(text),
"weighting=%s\n",
289 ?
"sample" :
"physical_time"));
293 PetscCall(PetscSNPrintf(text,
sizeof(text),
"cadence=step:%" PetscInt_FMT
"\n",
296 PetscCall(PetscSNPrintf(text,
sizeof(text),
"cadence=time:%.17g\n",
301 for (PetscInt n = 0; n < definition->
field_count; ++n) {
305 PetscCheck(name != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
306 "Window '%s' requests unknown field id %" PetscInt_FMT
".",
308 PetscCall(PetscStrlen(text, &used));
309 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"field=%s:%s\n",
310 name, request->
want_second ?
"first,second" :
"first"));
315 const PetscInt index = pair_order[n];
319 PetscCheck(first != NULL && second != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
320 "Window '%s' requests a covariance over an unknown field id.",
322 PetscCall(PetscStrlen(text, &used));
323 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"covariance=%s,%s\n",
331 PetscCall(PetscSNPrintf(text,
sizeof(text),
"mask=fluid\n"));
334 PetscCall(PetscSNPrintf(text,
sizeof(text),
"target=pointwise\n"));
335 for (PetscInt n = 0; n < definition->
field_count; ++n) {
340 PetscCall(PetscStrlen(text, &used));
341 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"layout=%s:%s\n",
348 PetscCall(PetscStrlen(text, &length));
350 if (group_digest_hex) {
354 PetscCall(PetscStrncpy(group_digest_hex[group], group_digest,
359 PetscFunctionReturn(0);
367 const char *saved_group_digests,
372 const char *cursor = saved_group_digests;
374 PetscFunctionBeginUser;
375 PetscCheck(definition != NULL && group != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
376 "Definition and group output are required.");
378 if (!saved_group_digests) PetscFunctionReturn(0);
382 const char *comma = strchr(cursor,
',');
383 const size_t length = comma ? (size_t)(comma - cursor) : strlen(cursor);
386 PetscCall(PetscStrlen(current[index], &expected));
391 if (length != expected) PetscFunctionReturn(0);
392 if (strncmp(cursor, current[index], length)) {
394 PetscFunctionReturn(0);
396 if (!comma) PetscFunctionReturn(0);
399 PetscFunctionReturn(0);
408 PetscReal span = 0.0;
412 if (span <= 0.0)
return 0.0;
433 PetscFunctionBeginUser;
434 PetscCheck(simCtx != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"SimCtx cannot be NULL.");
440 PetscBool accepted = PETSC_FALSE;
441 PetscReal weight = 0.0;
447 "Statistics window '%s' active from t=%.6g.\n",
453 for (PetscInt bi = 0; bi < simCtx->
block_number; ++bi) {
460 "Statistics window '%s' accepted step %d at t=%.6g with weight %.6g "
461 "(samples=%d, represented=%.6g).\n",
469 "Statistics window '%s' did not sample step %d at t=%.6g "
470 "(last accepted t=%.6g, samples=%d).\n",
476 "Statistics window '%s' complete: %d sample(s), total weight %.6g, "
477 "represented time %.6g.\n",
482 "Statistics window '%s' completed without accepting any sample.\n",
492 PetscReal lowest = 1.0, highest = 0.0;
499 "Statistics window '%s' completed with points that were never "
500 "valid; their mean and moments are undefined.\n",
504 "Statistics window '%s' per-point valid fraction spans [%.3f, %.3f].\n",
511 PetscFunctionReturn(0);
Small dependency-free SHA-256 utility for persistent metadata identity.
void PicurvSHA256Init(PicurvSHA256Context *context)
Initialize an incremental SHA-256 calculation.
void PicurvSHA256Update(PicurvSHA256Context *context, const void *data, size_t length)
Add bytes to an incremental SHA-256 calculation.
void PicurvSHA256FinalHex(PicurvSHA256Context *context, char digest_hex[65])
Finish a SHA-256 calculation and return a lowercase hexadecimal digest.
Incremental SHA-256 state.
Authoritative identities and storage metadata for persistent Eulerian fields.
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.
Logging utilities and macros for PETSc-based applications.
#define GLOBAL
Scope for global logging across all processes.
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
Per-window PETSc accumulator storage and pointwise application.
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.
#define WINDOW_TIME_EPSILON
Tolerance for treating two physical times as the same instant.
PetscErrorCode FieldStatisticsUpdateWindows(SimCtx *simCtx, PetscInt step, PetscReal time)
Implementation of FieldStatisticsUpdateWindows().
PetscErrorCode PicurvWindowComputeHash(const PicurvWindowDefinition *definition, char digest_hex[65], char group_digest_hex[][PICURV_WINDOW_HASH_GROUP_LENGTH])
Implementation of PicurvWindowComputeHash().
const char * PicurvWindowHashGroupName(PetscInt group)
Implementation of PicurvWindowHashGroupName().
static void WindowAdvanceTimeTarget(PicurvWindow *window, PetscReal time)
Internal helper: advances the time-cadence target past the accepted time.
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.
static PetscBool WindowStateIsDue(const PicurvWindow *window, PetscInt step, PetscReal time)
Internal helper: reports whether a state is due under the window's schedule.
static const char *const kHashGroupNames[PICURV_WINDOW_HASH_GROUP_COUNT]
Stable names of the hashed property groups, in serialization order.
PetscBool FieldStatisticsIsActive(const SimCtx *simCtx)
Implementation of FieldStatisticsIsActive().
PetscErrorCode PicurvWindowInit(PicurvWindow *window, const PicurvWindowDefinition *definition)
Implementation of PicurvWindowInit().
PetscErrorCode PicurvWindowOfferState(PicurvWindow *window, PetscInt step, PetscReal time, PetscBool *accepted, PetscReal *weight)
Implementation of PicurvWindowOfferState().
const char * PicurvWindowStateName(PicurvWindowState state)
Implementation of PicurvWindowStateName().
PetscReal PicurvWindowProgress(const PicurvWindow *window)
Implementation of PicurvWindowProgress().
PetscErrorCode PicurvWindowFirstHashDifference(const PicurvWindowDefinition *definition, const char *saved_group_digests, PetscInt *group)
Implementation of PicurvWindowFirstHashDifference().
Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
PetscInt last_event_step
Guards against a step being offered twice.
#define PICURV_WINDOW_HASH_GROUP_COUNT
Number of independently hashed property groups in a 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.
PetscReal last_accepted_time
Right edge of the last represented interval.
PetscInt first
First member; must also appear in the field list.
PicurvWindowFieldRequest fields[16]
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.
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.
PetscBool want_second
Also keep the centered second moment.
PetscInt second
Second member; must also appear in the field list.
PicurvWindowCovarianceRequest covariances[16]
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 covariance_count
PetscInt activation_step
Step at which the window became active.
PetscInt field_id
Catalogued Eulerian field identity.
#define PICURV_WINDOW_MAX_REQUESTS
Maximum fields or covariance pairs one window may request.
@ PICURV_WEIGHTING_SAMPLE
Equal weight per accepted state.
@ 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.
PicurvWeighting weighting
Runtime state of one window.
The scientifically immutable definition of one window.
One field a window accumulates.
Main header file for a complex fluid dynamics solver.
PetscInt fieldStatisticsWindowCount
PetscBool fieldStatisticsEnabled
struct PicurvWindow * fieldStatisticsWindows
struct PicurvWindowStorage * fieldStatisticsStorage
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.