17#define WINDOW_TIME_EPSILON 1.0e-12
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);
124 PetscBool *accepted, PetscReal *weight)
126 PetscReal interval = 0.0;
127 PetscReal right_edge = 0.0;
128 PetscBool closes = PETSC_FALSE;
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;
161 PetscFunctionReturn(0);
182 PetscFunctionReturn(0);
186 *accepted = PETSC_TRUE;
195 PetscFunctionReturn(0);
200 "name",
"start_time",
"weighting",
"cadence",
"fields",
"covariances",
"mask",
"target"
221static void SortRequestOrder(PetscInt count,
const PetscInt *primary,
const PetscInt *secondary,
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];
231 (primary[order[m]] > primary[candidate] ||
232 (primary[order[m]] == primary[candidate] &&
233 secondary[order[m]] > secondary[candidate]))) {
234 order[m + 1] = order[m];
237 order[m + 1] = candidate;
258 PetscFunctionBeginUser;
260 PetscCheck(definition != NULL && digest_hex != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
261 "Definition and digest output are required.");
263 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
264 "Window '%s' requests %" PetscInt_FMT
" fields; at most %d are supported.",
268 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
269 "Window '%s' requests %" PetscInt_FMT
" covariances; at most %d are supported.",
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;
277 for (PetscInt pair_index = 0; pair_index < definition->
covariance_count; ++pair_index) {
283 pair_primary[pair_index] = PetscMin(a, b);
284 pair_secondary[pair_index] = PetscMax(a, b);
291 char group_digest[65];
299 PetscCall(PetscSNPrintf(text,
sizeof(text),
"name=%s\n", definition->
name));
302 PetscCall(PetscSNPrintf(text,
sizeof(text),
"start_time=%.17g\n",
306 PetscCall(PetscSNPrintf(text,
sizeof(text),
"weighting=%s\n",
308 ?
"sample" :
"physical_time"));
312 PetscCall(PetscSNPrintf(text,
sizeof(text),
"cadence=step:%" PetscInt_FMT
"\n",
315 PetscCall(PetscSNPrintf(text,
sizeof(text),
"cadence=time:%.17g\n",
320 for (PetscInt n = 0; n < definition->
field_count; ++n) {
324 PetscCheck(name != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
325 "Window '%s' requests unknown field id %" PetscInt_FMT
".",
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"));
334 const PetscInt index = pair_order[n];
338 PetscCheck(first != NULL && second != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
339 "Window '%s' requests a covariance over an unknown field id.",
341 PetscCall(PetscStrlen(text, &used));
342 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"covariance=%s,%s\n",
353 PetscCall(PetscSNPrintf(text,
sizeof(text),
"mask=fluid\n"));
354 for (PetscInt n = 0; n < definition->
field_count; ++n) {
359 PetscCall(PetscStrlen(text, &used));
360 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"valid=%s:particles\n",
365 PetscCall(PetscSNPrintf(text,
sizeof(text),
"target=pointwise\n"));
366 for (PetscInt n = 0; n < definition->
field_count; ++n) {
371 PetscCall(PetscStrlen(text, &used));
372 PetscCall(PetscSNPrintf(text + used,
sizeof(text) - used,
"layout=%s:%s\n",
379 PetscCall(PetscStrlen(text, &length));
381 if (group_digest_hex) {
385 PetscCall(PetscStrncpy(group_digest_hex[group], group_digest,
391 PetscFunctionReturn(0);
399 const char *saved_group_digests,
404 const char *cursor = saved_group_digests;
406 PetscFunctionBeginUser;
408 PetscCheck(definition != NULL && group != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
409 "Definition and group output are required.");
415 const char *comma = strchr(cursor,
',');
416 const size_t length = comma ? (size_t)(comma - cursor) : strlen(cursor);
419 PetscCall(PetscStrlen(current[index], &expected));
425 if (strncmp(cursor, current[index], length)) {
428 PetscFunctionReturn(0);
434 PetscFunctionReturn(0);
444 PetscReal span = 0.0;
484 PetscFunctionBeginUser;
486 PetscCheck(simCtx != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"SimCtx cannot be NULL.");
492 PetscBool accepted = PETSC_FALSE;
493 PetscReal weight = 0.0;
499 "Statistics window '%s' active from t=%.6g.\n",
505 for (PetscInt bi = 0; bi < simCtx->
block_number; ++bi) {
512 "Statistics window '%s' accepted step %" PetscInt_FMT
" at t=%.6g with weight %.6g "
513 "(samples=%" PetscInt_FMT
", represented=%.6g).\n",
521 "Statistics window '%s' did not sample step %" PetscInt_FMT
" at t=%.6g "
522 "(last accepted t=%.6g, samples=%" PetscInt_FMT
").\n",
528 "Statistics window '%s' complete: %" PetscInt_FMT
" sample(s), total weight %.6g, "
529 "represented time %.6g.\n",
534 "Statistics window '%s' completed without accepting any sample.\n",
544 PetscReal lowest = 1.0, highest = 0.0;
551 "Statistics window '%s' completed with points that were never "
552 "valid; their mean and moments are undefined.\n",
556 "Statistics window '%s' per-point valid fraction spans [%.3f, %.3f].\n",
564 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.
@ 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.
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_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
#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 function: an always-on aggregate timer.
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.
PetscBool FieldStatisticsNeedsWorkspace(const SimCtx *simCtx)
Implementation of FieldStatisticsNeedsWorkspace().
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
PetscInt field_statistics_ensemble_count
Recipe-defined single-instant windows, derived at every processed step.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.