23 memset(&d, 0,
sizeof(d));
32 PetscReal *total_weight, PetscInt *count)
34 PetscFunctionBeginUser;
35 for (PetscInt s = 0; s <= steps; ++s) {
36 PetscBool accepted = PETSC_FALSE;
37 PetscReal weight = 0.0;
42 PetscFunctionReturn(0);
55 PetscReal sample_weight = 0.0, time_weight = 0.0;
56 PetscInt sample_count = 0, time_count = 0;
57 const PetscReal dt = 0.25;
58 const PetscInt steps = 20;
60 PetscFunctionBeginUser;
69 PetscCall(
RunUniform(&sample_w, steps, dt, &sample_weight, &sample_count));
70 PetscCall(
RunUniform(&time_w, steps, dt, &time_weight, &time_count));
73 PetscCall(
PicurvAssertIntEqual(steps, sample_count,
"sample weighting counts one per interval"));
76 "both weightings must accept exactly the same states"));
77 PetscCall(
PicurvAssertRealNear((PetscReal)steps, sample_weight, 1.0e-12,
"sample total weight"));
78 PetscCall(
PicurvAssertRealNear((PetscReal)steps * dt, time_weight, 1.0e-12,
"time total weight"));
82 "weights differ only by the constant timestep"));
84 "represented time spans the whole run"));
85 PetscFunctionReturn(0);
94 PetscBool accepted = PETSC_FALSE;
95 PetscReal weight = 0.0;
97 PetscFunctionBeginUser;
103 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"states before the start are ignored"));
108 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"the origin state is not a sample"));
114 PetscCall(
PicurvAssertBool(accepted,
"the state after the origin is a sample"));
115 PetscCall(
PicurvAssertRealNear(0.5, weight, 1.0e-12,
"first sample carries the interval from the origin"));
116 PetscFunctionReturn(0);
125 PetscBool accepted = PETSC_FALSE;
126 PetscReal weight = 0.0;
128 PetscFunctionBeginUser;
132 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"the first observed state anchors the moved origin"));
134 "effective start moves to the first observed time"));
137 PetscCall(
PicurvAssertRealNear(0.5, weight, 1.0e-12,
"weight is measured from the moved origin"));
139 "a window never claims time it did not observe"));
140 PetscFunctionReturn(0);
149 const PetscReal times[4] = {0.0, 0.5, 2.0, 2.25};
150 const PetscReal expected[4] = {0.0, 0.5, 1.5, 0.25};
151 PetscBool accepted = PETSC_FALSE;
152 PetscReal weight = 0.0;
154 PetscFunctionBeginUser;
156 for (PetscInt i = 0; i < 4; ++i) {
159 "variable-dt weight equals the elapsed interval"));
163 PetscFunctionReturn(0);
172 PetscBool accepted = PETSC_FALSE;
173 PetscReal weight = 0.0;
174 const PetscReal dt = 0.1;
176 PetscFunctionBeginUser;
178 for (PetscInt s = 0; s <= 6; ++s) {
183 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"off-schedule states are not sampled"));
185 "an off-schedule state changes no scientific state"));
187 "an off-schedule state records no sample"));
193 "strided weights still cover the full elapsed span"));
194 PetscFunctionReturn(0);
203 PetscBool accepted = PETSC_FALSE;
204 PetscReal weight = 0.0;
206 PetscFunctionBeginUser;
216 PetscCall(
PicurvAssertBool(accepted,
"the overshooting state still contributes its clipped interval"));
217 PetscCall(
PicurvAssertRealNear(0.2, weight, 1.0e-12,
"final interval clips to the requested end"));
220 "represented time equals the requested span exactly"));
225 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"a complete window accepts nothing further"));
227 PetscFunctionReturn(0);
236 PetscBool accepted = PETSC_FALSE;
237 PetscReal weight = 0.0;
239 PetscFunctionBeginUser;
246 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"the same step must not be counted twice"));
249 PetscFunctionReturn(0);
263 PetscBool accepted = PETSC_FALSE;
264 PetscReal weight = 0.0;
266 PetscFunctionBeginUser;
267 memset(&d, 0,
sizeof(d));
275 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"origin anchors under time cadence too"));
279 PetscCall(
PicurvAssertBool(accepted,
"the overshooting state is accepted"));
281 "weight is the actual elapsed interval, not the nominal cadence"));
286 PetscCall(
PicurvAssertBool((PetscBool)!accepted,
"the next target has not been reached yet"));
288 PetscCall(
PicurvAssertBool(accepted,
"the state reaching the 0.5 target is accepted"));
289 PetscCall(
PicurvAssertRealNear(0.06, weight, 1.0e-12,
"weight resumes from the last accepted state"));
291 "no represented time is lost or double counted across the overshoot"));
292 PetscFunctionReturn(0);
299 PetscErrorCode ierr_cadence = 0, ierr_name = 0, ierr_span = 0;
301 PetscFunctionBeginUser;
304 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
306 PetscCall(PetscPopErrorHandler());
308 PetscCall(
PicurvAssertIntEqual(PETSC_ERR_ARG_OUTOFRANGE, ierr_cadence,
"non-positive stride is rejected"));
311 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
313 PetscCall(PetscPopErrorHandler());
315 PetscCall(
PicurvAssertIntEqual(PETSC_ERR_ARG_OUTOFRANGE, ierr_name,
"an empty window name is rejected"));
318 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
320 PetscCall(PetscPopErrorHandler());
323 PetscFunctionReturn(0);
329 PetscBool *same, PetscInt *first_difference)
331 char base_digest[65], variant_digest[65];
335 PetscFunctionBeginUser;
338 PetscCall(PetscStrcmp(base_digest, variant_digest, same));
339 *first_difference = -1;
341 PetscBool group_same = PETSC_FALSE;
343 PetscCall(PetscStrcmp(base_groups[group], variant_groups[group], &group_same));
344 if (!group_same) { *first_difference = group;
break; }
346 PetscFunctionReturn(0);
368 PetscBool same = PETSC_FALSE;
371 PetscFunctionBeginUser;
375 PetscCall(
PicurvAssertBool(same,
"hashing an unchanged definition is stable"));
381 PetscCall(
PicurvAssertBool(same,
"extending end_time does not change the identity"));
388 PetscCall(
PicurvAssertBool(same,
"reordering the field list does not change the identity"));
395 PetscCall(
PicurvAssertBool(same,
"swapping covariance members does not change the identity"));
396 PetscFunctionReturn(0);
404 PetscBool same = PETSC_FALSE;
406 PetscBool named = PETSC_FALSE;
408 PetscFunctionBeginUser;
412 PetscCall(
PicurvAssertBool((PetscBool)!same,
"a renamed window is a different window"));
418 PetscCall(
PicurvAssertBool((PetscBool)!same,
"a moved start is a different window"));
424 PetscCall(
PicurvAssertBool((PetscBool)!same,
"a changed weighting is a different window"));
430 PetscCall(
PicurvAssertBool((PetscBool)!same,
"a changed cadence is a different window"));
437 PetscCall(
PicurvAssertBool((PetscBool)!same,
"a changed moment set is a different window"));
443 PetscCall(
PicurvAssertBool((PetscBool)!same,
"a dropped covariance is a different window"));
449 PetscCall(
PicurvAssertBool((PetscBool)!named,
"every hashed group has a stable name"));
452 PetscCall(
PicurvAssertBool(named,
"an out-of-range group index is named unknown"));
453 PetscFunctionReturn(0);
458 char *out,
size_t out_size)
464 PetscFunctionBeginUser;
468 PetscCall(PetscStrlen(out, &used));
469 PetscCall(PetscSNPrintf(out + used, out_size - used,
"%s%s", group ?
"," :
"", groups[group]));
471 PetscFunctionReturn(0);
488 PetscFunctionBeginUser;
499 PetscCall(
PicurvAssertIntEqual(2, group,
"a changed weighting localizes to the weighting group"));
504 PetscCall(
PicurvAssertIntEqual(5, group,
"a dropped covariance localizes to the covariances group"));
525 "a truncated digest list reports no property rather than a wrong one"));
526 PetscFunctionReturn(0);
550 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv statistics window tests");
551 if (ierr)
return (
int)ierr;
552 ierr =
PicurvRunTests(
"unit-statistics-window", cases,
sizeof(cases) /
sizeof(cases[0]));
553 if (ierr) { PetscFinalize();
return (
int)ierr; }
554 ierr = PetscFinalize();
Authoritative identities and storage metadata for persistent Eulerian fields.
Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
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_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.
const char * PicurvWindowHashGroupName(PetscInt group)
Returns the stable name of one hashed property group.
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 end_time
Requested end; ignored when bounded is false.
PicurvCadenceKind cadence_kind
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.
@ 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.
PicurvWindowCovarianceRequest covariances[16]
PetscBool bounded
False for an open-ended window.
PetscReal start_time
Requested start.
PetscInt covariance_count
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.
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.
@ 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.
PicurvWeighting weighting
Runtime state of one window.
The scientifically immutable definition of one window.
static PetscErrorCode TestStrideAndOffScheduleNoOp(void)
Stride skips states, and the accepted weight still spans the whole gap.
static PetscErrorCode TestEndClippingAndCompletion(void)
A bounded window clips its final interval and then accepts nothing.
static PetscErrorCode HashAndCompare(const PicurvWindowDefinition *baseline, const PicurvWindowDefinition *variant, PetscBool *same, PetscInt *first_difference)
Hashes a definition and reports which property group changed against a baseline.
static PetscErrorCode TestDuplicateEventRejected(void)
The same completed step offered twice is counted once.
int main(int argc, char **argv)
Entry point for the window lifecycle suite.
static PetscErrorCode TestInvalidDefinitionsRejected(void)
Invalid definitions are rejected at initialization.
static PetscErrorCode TestOriginStateAnchorsWithoutSampling(void)
A state at the window origin anchors without becoming a sample.
static PetscErrorCode TestLateFirstObservationMovesOrigin(void)
A window first seen after its requested start moves its origin forward.
static PicurvWindowDefinition HashWindow(void)
Builds the hash fixture: a Ucat/P window with a second moment and a covariance.
static PetscErrorCode SerializeGroupDigests(const PicurvWindowDefinition *definition, char *out, size_t out_size)
Serializes a definition's group digests the way a checkpoint records them.
static PetscErrorCode TestHashExclusionsAndStability(void)
The hash is stable, and excludes exactly the properties the spec excludes.
static PetscErrorCode TestVariableTimestepWeighting(void)
Variable timestep weighting follows the actual elapsed intervals.
static PicurvWindowDefinition StepWindow(const char *name, PetscReal start, PetscReal end, PetscBool bounded, PicurvWeighting weighting, PetscInt cadence)
Builds a step-cadence definition.
static PetscErrorCode TestFirstHashDifferenceLocalization(void)
Saved group digests must localize a change to the property that caused it.
static PetscErrorCode TestHashDetectsEachProperty(void)
Every hashed property changes the digest and is named by its group digest.
static PetscErrorCode TestTimeCadenceOvershootAcceptedOnce(void)
A step overshooting several time targets is accepted once, losing no time.
static PetscErrorCode TestWeightingsAgreeAtConstantTimestep(void)
The two weightings must agree on a constant-timestep run.
static PetscErrorCode RunUniform(PicurvWindow *w, PetscInt steps, PetscReal dt, PetscReal *total_weight, PetscInt *count)
Drives a uniform-dt sequence and returns the accumulated weight and count.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Runs a named C test suite and prints pass/fail progress markers.
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
Shared declarations for the PICurv C test fixture and assertion layer.
Named test case descriptor consumed by PicurvRunTests.