17#define WINDOW_TIME_EPSILON 1.0e-12
29 default:
return "unknown";
34#define __FUNCT__ "PicurvWindowInit"
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.");
48 PetscCheck(definition->
step_cadence > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
49 "Window '%s' needs a positive step cadence, got %" PetscInt_FMT
".",
52 PetscCheck(definition->
time_cadence > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
53 "Window '%s' needs a positive time cadence, got %g.",
57 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
58 "Window '%s' must end after it starts.", definition->
name);
60 PetscCall(PetscMemzero(window,
sizeof(*window)));
73 PetscFunctionReturn(0);
118 PetscBool *accepted, PetscReal *weight)
120 PetscReal interval = 0.0;
121 PetscReal right_edge = 0.0;
122 PetscBool closes = PETSC_FALSE;
124 PetscFunctionBeginUser;
125 PetscCheck(window != NULL && accepted != NULL && weight != NULL,
126 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
127 "Window, acceptance flag, and weight output are required.");
128 *accepted = PETSC_FALSE;
153 PetscFunctionReturn(0);
173 PetscFunctionReturn(0);
177 *accepted = PETSC_TRUE;
185 PetscFunctionReturn(0);
190 "name",
"start_time",
"weighting",
"cadence",
"fields",
"covariances",
"mask",
"target"
209static void SortRequestOrder(PetscInt count,
const PetscInt *primary,
const PetscInt *secondary,
212 for (PetscInt n = 0; n < count; ++n) order[n] = n;
213 for (PetscInt n = 1; n < count; ++n) {
214 const PetscInt candidate = order[n];
218 (primary[order[m]] > primary[candidate] ||
219 (primary[order[m]] == primary[candidate] &&
220 secondary[order[m]] > secondary[candidate]))) {
221 order[m + 1] = order[m];
224 order[m + 1] = candidate;
229#define __FUNCT__ "PicurvWindowComputeHash"
246 PetscFunctionBeginUser;
248 PetscCheck(definition != NULL && digest_hex != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
249 "Definition and digest output are required.");
251 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
252 "Window '%s' requests %" PetscInt_FMT
" fields; at most %d are supported.",
256 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
257 "Window '%s' requests %" PetscInt_FMT
" covariances; at most %d are supported.",
260 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
261 field_primary[field_index] = definition->
fields[field_index].
field_id;
262 field_secondary[field_index] = 0;
265 for (PetscInt pair_index = 0; pair_index < definition->
covariance_count; ++pair_index) {
271 pair_primary[pair_index] = PetscMin(a, b);
272 pair_secondary[pair_index] = PetscMax(a, b);
279 char group_digest[65];
287 PetscCall(PetscSNPrintf(text,
sizeof(text),
"name=%s\n", definition->
name));
290 PetscCall(PetscSNPrintf(text,
sizeof(text),
"start_time=%.17g\n",
294 PetscCall(PetscSNPrintf(text,
sizeof(text),
"weighting=%s\n",
296 ?
"sample" :
"physical_time"));
300 PetscCall(PetscSNPrintf(text,
sizeof(text),
"cadence=step:%" PetscInt_FMT
"\n",
303 PetscCall(PetscSNPrintf(text,
sizeof(text),
"cadence=time:%.17g\n",
308 for (PetscInt n = 0; n < definition->
field_count; ++n) {
312 PetscCheck(name != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
313 "Window '%s' requests unknown field id %" PetscInt_FMT
".",
315 PetscCall(PetscStrlen(text, &used));
316 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"field=%s:%s\n",
317 name, request->
want_second ?
"first,second" :
"first"));
322 const PetscInt index = pair_order[n];
326 PetscCheck(first != NULL && second != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
327 "Window '%s' requests a covariance over an unknown field id.",
329 PetscCall(PetscStrlen(text, &used));
330 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"covariance=%s,%s\n",
338 PetscCall(PetscSNPrintf(text,
sizeof(text),
"mask=fluid\n"));
341 PetscCall(PetscSNPrintf(text,
sizeof(text),
"target=pointwise\n"));
342 for (PetscInt n = 0; n < definition->
field_count; ++n) {
347 PetscCall(PetscStrlen(text, &used));
348 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"layout=%s:%s\n",
355 PetscCall(PetscStrlen(text, &length));
357 if (group_digest_hex) {
361 PetscCall(PetscStrncpy(group_digest_hex[group], group_digest,
367 PetscFunctionReturn(0);
375 const char *saved_group_digests,
380 const char *cursor = saved_group_digests;
382 PetscFunctionBeginUser;
383 PetscCheck(definition != NULL && group != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
384 "Definition and group output are required.");
386 if (!saved_group_digests) PetscFunctionReturn(0);
390 const char *comma = strchr(cursor,
',');
391 const size_t length = comma ? (size_t)(comma - cursor) : strlen(cursor);
394 PetscCall(PetscStrlen(current[index], &expected));
399 if (length != expected) PetscFunctionReturn(0);
400 if (strncmp(cursor, current[index], length)) {
402 PetscFunctionReturn(0);
404 if (!comma) PetscFunctionReturn(0);
407 PetscFunctionReturn(0);
416 PetscReal span = 0.0;
420 if (span <= 0.0)
return 0.0;
436#define __FUNCT__ "FieldStatisticsUpdateWindows"
443 PetscFunctionBeginUser;
445 PetscCheck(simCtx != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"SimCtx cannot be NULL.");
451 PetscBool accepted = PETSC_FALSE;
452 PetscReal weight = 0.0;
458 "Statistics window '%s' active from t=%.6g.\n",
464 for (PetscInt bi = 0; bi < simCtx->
block_number; ++bi) {
471 "Statistics window '%s' accepted step %d at t=%.6g with weight %.6g "
472 "(samples=%d, represented=%.6g).\n",
480 "Statistics window '%s' did not sample step %d at t=%.6g "
481 "(last accepted t=%.6g, samples=%d).\n",
487 "Statistics window '%s' complete: %d sample(s), total weight %.6g, "
488 "represented time %.6g.\n",
493 "Statistics window '%s' completed without accepting any sample.\n",
503 PetscReal lowest = 1.0, highest = 0.0;
510 "Statistics window '%s' completed with points that were never "
511 "valid; their mean and moments are undefined.\n",
515 "Statistics window '%s' per-point valid fraction spans [%.3f, %.3f].\n",
523 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...
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled code block (typically a function).
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.