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

C unit tests for per-window accumulator storage and pointwise application. More...

#include "test_support.h"
#include "statistics_accumulator.h"
#include "statistics_moments.h"
#include "field_catalog.h"
#include "statistics_window.h"
#include "statistics_target.h"
Include dependency graph for test_statistics_accumulator.c:

Go to the source code of this file.

Macros

#define ACC_N   6 /* fixture size; cell domain is 5x5x5 */
 
#define ACC_CELLS   125
 

Functions

static PicurvWindowDefinition AccDefinition (PetscBool want_second)
 Builds a definition requesting Ucat and P with second moments.
 
static PetscErrorCode SetUniform (UserCtx *user, PetscReal x, PetscReal y, PetscReal z, PetscReal p)
 Sets Ucat to a uniform vector and P to a uniform scalar.
 
static PetscErrorCode ReadScalarAt (UserCtx *user, Vec v, PetscInt i, PetscInt j, PetscInt k, PetscReal *out)
 Reads one interior point of a scalar accumulator vector.
 
static PetscErrorCode ReadComponentAt (UserCtx *user, Vec v, PetscInt components, PetscInt component, PetscInt i, PetscInt j, PetscInt k, PetscReal *out)
 Reads one component of a multi-component accumulator vector at an interior point.
 
static PetscErrorCode TestComponentCounts (void)
 Component counts follow the documented product shapes.
 
static PetscErrorCode TestStorageShape (void)
 Storage allocates one mean per field and the right number of product components.
 
static PetscErrorCode TestKnownAccumulationAcrossField (void)
 Three accepted states reproduce the analytically known moments at every point.
 
static PetscErrorCode TestValidFractionRange (void)
 The valid-fraction range reports mask coverage, including a never-valid point.
 
static PetscErrorCode TestValidFractionDetectsNeverValidPoint (void)
 A point blanked from the start is reported as never valid.
 
static PetscErrorCode TestCovarianceAccumulation (void)
 A vector-scalar covariance reproduces its analytically known components.
 
static PetscErrorCode TestScalarSelfCovarianceMatchesSecondMoment (void)
 A field's covariance with itself reduces exactly to its own second moment.
 
static PetscErrorCode TestCovarianceRequiresFieldMembership (void)
 A covariance member missing from the field list is rejected, not silently skipped.
 
static PetscErrorCode TestDerivedQuantities (void)
 Derived quantities reproduce the analytic values the accumulated state implies.
 
static PetscErrorCode TestSpatialMeanExcludesUnsampledPoints (void)
 The spatial mean divides by sampled points, not by the whole vector.
 
static PetscErrorCode TestPayloadEnumeration (void)
 Payload enumeration covers every vector exactly once with stable names.
 
static PetscErrorCode TestWeightedAccumulation (void)
 Unequal weights reproduce the weighted result the kernels define.
 
static PetscErrorCode TestMaskedPointsAreExcluded (void)
 Masked points accumulate nothing, and remain distinguishable from unsampled ones.
 
static PetscErrorCode TestConstantFieldZeroProduct (void)
 A constant field yields exactly zero product at every point.
 
static PetscErrorCode TestRunloopDriverAppliesScheduledStates (void)
 The runloop entry point applies exactly the states the schedule accepts.
 
static void PlaceParticle (PetscReal *velocity, PetscReal *psi, PetscInt *cell, PetscInt p, PetscInt ci, PetscInt cj, PetscInt ck, const PetscReal v[3], PetscReal value)
 Places one particle in a geometric cell with the given velocity and scalar.
 
static PetscErrorCode TestParticleEnsembleMatchesStreamingState (void)
 A cell's particles form the same state the streaming accumulator builds.
 
static PetscReal ReductionScalar (PetscInt i, PetscInt j, PetscInt k, PetscInt s)
 Closed-form test field: a scalar varying in every direction and over samples.
 
static PetscErrorCode TestReductionEqualsPooledMoments (void)
 A homogeneous merge equals the moments of the slot's pooled sample set.
 
static PetscErrorCode SetParticleState (UserCtx *user, PetscReal psi_value, PetscReal particles)
 Sets Psi and the particle count uniformly, so a state is occupied or not everywhere.
 
static PetscErrorCode TestParticleSampledFieldUsesOccupiedStates (void)
 A particle-sampled field is accumulated over the occupied states only.
 
static PetscErrorCode TestReductionPoolsOnlyOccupiedSamples (void)
 A homogeneous merge of a particle-sampled field pools only its occupied samples.
 
int main (int argc, char **argv)
 Entry point for the accumulator suite.
 

Detailed Description

C unit tests for per-window accumulator storage and pointwise application.

The field series reuses the values verified analytically by the moment-kernel suite, so a failure here isolates the field-to-accumulator wiring rather than the numerics: three samples of (1,2,3), (3,6,5), (5,4,7) have means (3,4,5) and symmetric centered products (8,4,8,8,4,8) in (xx,xy,xz,yy,yz,zz) order.

Definition in file test_statistics_accumulator.c.

Macro Definition Documentation

◆ ACC_N

#define ACC_N   6 /* fixture size; cell domain is 5x5x5 */

Definition at line 19 of file test_statistics_accumulator.c.

◆ ACC_CELLS

#define ACC_CELLS   125

Definition at line 20 of file test_statistics_accumulator.c.

Function Documentation

◆ AccDefinition()

static PicurvWindowDefinition AccDefinition ( PetscBool  want_second)
static

Builds a definition requesting Ucat and P with second moments.

Definition at line 23 of file test_statistics_accumulator.c.

24{
26 memset(&d, 0, sizeof(d));
27 strncpy(d.name, "acc", PICURV_WINDOW_NAME_LENGTH - 1);
30 d.step_cadence = 1;
31 d.field_count = 2;
32 d.fields[0].field_id = FIELD_ID_UCAT; d.fields[0].want_second = want_second;
33 d.fields[1].field_id = FIELD_ID_P; d.fields[1].want_second = want_second;
34 return d;
35}
@ FIELD_ID_UCAT
@ FIELD_ID_P
PicurvWindowFieldRequest fields[16]
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.
PetscBool want_second
Also keep the centered second moment.
PetscInt field_id
Catalogued Eulerian field identity.
@ PICURV_WEIGHTING_SAMPLE
Equal weight per accepted state.
@ PICURV_CADENCE_STEP
Every n completed steps from activation.
The scientifically immutable definition of one window.
Here is the caller graph for this function:

◆ SetUniform()

static PetscErrorCode SetUniform ( UserCtx *  user,
PetscReal  x,
PetscReal  y,
PetscReal  z,
PetscReal  p 
)
static

Sets Ucat to a uniform vector and P to a uniform scalar.

Definition at line 38 of file test_statistics_accumulator.c.

39{
40 Cmpnts ***ucat = NULL;
41 PetscReal ***pp = NULL;
42 const DMDALocalInfo info = user->info;
43
44 PetscFunctionBeginUser;
45 PetscCall(DMDAVecGetArray(user->fda, user->Ucat, &ucat));
46 PetscCall(DMDAVecGetArray(user->da, user->P, &pp));
47 for (PetscInt k = info.zs; k < info.zs + info.zm; ++k)
48 for (PetscInt j = info.ys; j < info.ys + info.ym; ++j)
49 for (PetscInt i = info.xs; i < info.xs + info.xm; ++i) {
50 ucat[k][j][i].x = x; ucat[k][j][i].y = y; ucat[k][j][i].z = z;
51 pp[k][j][i] = p;
52 }
53 PetscCall(DMDAVecRestoreArray(user->da, user->P, &pp));
54 PetscCall(DMDAVecRestoreArray(user->fda, user->Ucat, &ucat));
55 PetscFunctionReturn(0);
56}
PetscScalar x
Definition variables.h:145
PetscScalar z
Definition variables.h:145
DMDALocalInfo info
Definition variables.h:1169
PetscScalar y
Definition variables.h:145
A 3D point or vector with PetscScalar components.
Definition variables.h:144
Here is the caller graph for this function:

◆ ReadScalarAt()

static PetscErrorCode ReadScalarAt ( UserCtx *  user,
Vec  v,
PetscInt  i,
PetscInt  j,
PetscInt  k,
PetscReal *  out 
)
static

Reads one interior point of a scalar accumulator vector.

Definition at line 59 of file test_statistics_accumulator.c.

60{
61 PetscReal ***a = NULL;
62 PetscFunctionBeginUser;
63 PetscCall(DMDAVecGetArrayRead(user->da, v, &a));
64 *out = a[k][j][i];
65 PetscCall(DMDAVecRestoreArrayRead(user->da, v, &a));
66 PetscFunctionReturn(0);
67}
Here is the caller graph for this function:

◆ ReadComponentAt()

static PetscErrorCode ReadComponentAt ( UserCtx *  user,
Vec  v,
PetscInt  components,
PetscInt  component,
PetscInt  i,
PetscInt  j,
PetscInt  k,
PetscReal *  out 
)
static

Reads one component of a multi-component accumulator vector at an interior point.

Definition at line 70 of file test_statistics_accumulator.c.

72{
73 PetscScalar ****a = NULL;
74 DM dm = NULL;
75 PetscFunctionBeginUser;
76 PetscCall(PicurvStatisticsComponentDM(user, components, &dm));
77 PetscCall(DMDAVecGetArrayDOFRead(dm, v, &a));
78 *out = a[k][j][i][component];
79 PetscCall(DMDAVecRestoreArrayDOFRead(dm, v, &a));
80 PetscFunctionReturn(0);
81}
PetscErrorCode PicurvStatisticsComponentDM(UserCtx *user, PetscInt components, DM *dm)
Resolves the DM carrying a given number of accumulator components.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestComponentCounts()

static PetscErrorCode TestComponentCounts ( void  )
static

Component counts follow the documented product shapes.

Definition at line 84 of file test_statistics_accumulator.c.

85{
86 PetscInt n = 0;
87 PetscErrorCode bad = 0;
88
89 PetscFunctionBeginUser;
90 PetscCall(PicurvProductComponentCount(1, &n));
91 PetscCall(PicurvAssertIntEqual(1, n, "a scalar self-product has one component"));
92 PetscCall(PicurvProductComponentCount(3, &n));
93 PetscCall(PicurvAssertIntEqual(6, n, "a three-vector self-product has six symmetric components"));
94 PetscCall(PicurvCovarianceComponentCount(3, 1, &n));
95 PetscCall(PicurvAssertIntEqual(3, n, "a vector-scalar covariance has three components"));
96 PetscCall(PicurvCovarianceComponentCount(1, 1, &n));
97 PetscCall(PicurvAssertIntEqual(1, n, "a scalar-scalar covariance has one component"));
98
99 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
100 bad = PicurvCovarianceComponentCount(3, 3, &n);
101 PetscCall(PetscPopErrorHandler());
102 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_OUTOFRANGE, bad,
103 "vector-vector cross products are an explicit non-goal"));
104 PetscFunctionReturn(0);
105}
PetscErrorCode PicurvProductComponentCount(PetscInt dof, PetscInt *count)
Reports how many symmetric product components a field's second moment needs.
PetscErrorCode PicurvCovarianceComponentCount(PetscInt dof_a, PetscInt dof_b, PetscInt *count)
Reports how many components a covariance between two fields needs.
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestStorageShape()

static PetscErrorCode TestStorageShape ( void  )
static

Storage allocates one mean per field and the right number of product components.

Definition at line 108 of file test_statistics_accumulator.c.

109{
110 SimCtx *simCtx = NULL;
111 UserCtx *user = NULL;
112 PicurvWindowStorage storage;
114 PetscInt bs = 0;
115
116 PetscFunctionBeginUser;
117 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
118 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
119
120 PetscCall(PicurvAssertIntEqual(2, storage.field_count, "two fields requested"));
121
122 /* Each product is one vector carrying all of its components, so the symmetric
123 * second-order tensor stays a single object rather than six scalars. */
124 PetscCall(VecGetBlockSize(storage.m2[0], &bs));
125 PetscCall(PicurvAssertIntEqual(6, bs, "the Ucat product is one six-component tensor"));
126 PetscCall(VecGetBlockSize(storage.m2[1], &bs));
127 PetscCall(PicurvAssertIntEqual(1, bs, "the P product is a single scalar"));
128
129 /* Means inherit their source field's layout. */
130 PetscCall(VecGetBlockSize(storage.mean[0], &bs));
131 PetscCall(PicurvAssertIntEqual(3, bs, "the Ucat mean is a three-vector"));
132 PetscCall(VecGetBlockSize(storage.mean[1], &bs));
133 PetscCall(PicurvAssertIntEqual(1, bs, "the P mean is a scalar"));
134
135 PetscCall(PicurvWindowStorageDestroy(&storage));
136 PetscCall(PicurvAssertIntEqual(0, storage.field_count, "destroy zeroes the storage"));
137 PetscCall(PicurvAssertBool((PetscBool)(storage.mean == NULL), "destroy releases the mean array"));
138 /* Destroying zeroed storage is safe. */
139 PetscCall(PicurvWindowStorageDestroy(&storage));
140
141 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
142 PetscFunctionReturn(0);
143}
PetscInt field_count
Fields accumulated.
Vec * mean
One per field, matching that field's layout.
PetscErrorCode PicurvWindowStorageCreate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage)
Allocates the accumulator state one window owns on one block.
Vec * m2
One per field; NULL when no second moment was requested.
PetscErrorCode PicurvWindowStorageDestroy(PicurvWindowStorage *storage)
Releases accumulator state previously created for one window.
Independent accumulator state for one window on one block.
static PicurvWindowDefinition AccDefinition(PetscBool want_second)
Builds a definition requesting Ucat and P with second moments.
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Builds minimal SimCtx and UserCtx fixtures for C unit tests.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Destroys minimal SimCtx/UserCtx fixtures and all owned PETSc objects.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
The master context for the entire simulation.
Definition variables.h:916
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:

◆ TestKnownAccumulationAcrossField()

static PetscErrorCode TestKnownAccumulationAcrossField ( void  )
static

Three accepted states reproduce the analytically known moments at every point.

Definition at line 146 of file test_statistics_accumulator.c.

147{
148 SimCtx *simCtx = NULL;
149 UserCtx *user = NULL;
150 PicurvWindowStorage storage;
152 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
153 const PetscReal scalars[3] = {1.0, 2.0, 6.0};
154 const PetscReal expected_product[6] = {8.0, 4.0, 8.0, 8.0, 4.0, 8.0};
155 const char *labels[6] = {"xx", "xy", "xz", "yy", "yz", "zz"};
156 Cmpnts ***mean_vec = NULL;
157 PetscReal value = 0.0;
158 char context[128];
159
160 PetscFunctionBeginUser;
161 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
162 PetscCall(VecSet(user->Nvert, 0.0));
163 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
164
165 for (PetscInt s = 0; s < 3; ++s) {
166 PetscCall(SetUniform(user, series[s][0], series[s][1], series[s][2], scalars[s]));
167 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
168 }
169
170 /* Occupancy: every fluid point saw all three states. */
171 PetscCall(ReadScalarAt(user, storage.count, 2, 2, 2, &value));
172 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "per-point sample count"));
173 PetscCall(ReadScalarAt(user, storage.weight, 2, 2, 2, &value));
174 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "per-point total weight"));
175 PetscCall(ReadScalarAt(user, storage.weight_sq, 2, 2, 2, &value));
176 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "per-point squared-weight sum"));
177
178 /* Vector mean. */
179 PetscCall(DMDAVecGetArrayRead(user->fda, storage.mean[0], &mean_vec));
180 PetscCall(PicurvAssertRealNear(3.0, mean_vec[2][2][2].x, 1.0e-12, "Ucat mean x"));
181 PetscCall(PicurvAssertRealNear(4.0, mean_vec[2][2][2].y, 1.0e-12, "Ucat mean y"));
182 PetscCall(PicurvAssertRealNear(5.0, mean_vec[2][2][2].z, 1.0e-12, "Ucat mean z"));
183 PetscCall(DMDAVecRestoreArrayRead(user->fda, storage.mean[0], &mean_vec));
184
185 /* All six symmetric centered products, in fixed catalog order. */
186 for (PetscInt c = 0; c < 6; ++c) {
187 PetscCall(ReadComponentAt(user, storage.m2[0], 6, c, 2, 2, 2, &value));
188 PetscCall(PetscSNPrintf(context, sizeof(context), "Ucat centered product %s", labels[c]));
189 PetscCall(PicurvAssertRealNear(expected_product[c], value, 1.0e-11, context));
190 }
191
192 /* Scalar mean and second moment. */
193 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 2, 2, &value));
194 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "P mean"));
195 PetscCall(ReadScalarAt(user, storage.m2[1], 2, 2, 2, &value));
196 PetscCall(PicurvAssertRealNear(14.0, value, 1.0e-11, "P centered second moment"));
197
198 PetscCall(PicurvWindowStorageDestroy(&storage));
199 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
200 PetscFunctionReturn(0);
201}
Vec weight
Per-point valid weight.
Vec weight_sq
Per-point squared-weight sum.
Vec count
Per-point accepted sample count.
PetscErrorCode PicurvWindowAccumulate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage, PetscReal weight)
Applies one accepted completed state to a window's accumulators.
static PetscErrorCode ReadComponentAt(UserCtx *user, Vec v, PetscInt components, PetscInt component, PetscInt i, PetscInt j, PetscInt k, PetscReal *out)
Reads one component of a multi-component accumulator vector at an interior point.
static PetscErrorCode SetUniform(UserCtx *user, PetscReal x, PetscReal y, PetscReal z, PetscReal p)
Sets Ucat to a uniform vector and P to a uniform scalar.
static PetscErrorCode ReadScalarAt(UserCtx *user, Vec v, PetscInt i, PetscInt j, PetscInt k, PetscReal *out)
Reads one interior point of a scalar accumulator vector.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
Vec Nvert
Definition variables.h:1197
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestValidFractionRange()

static PetscErrorCode TestValidFractionRange ( void  )
static

The valid-fraction range reports mask coverage, including a never-valid point.

This is the mask-health indicator the console snapshot prints, and it is the only signal that distinguishes a mean built from every state from one built from none. A blanked point keeps a zero mean that looks like a legitimate value, so the range has to surface it.

Definition at line 211 of file test_statistics_accumulator.c.

212{
213 SimCtx *simCtx = NULL;
214 UserCtx *user = NULL;
215 PicurvWindowStorage storage;
216 PicurvWindowDefinition d = AccDefinition(PETSC_FALSE);
217 PetscReal ***nvert = NULL;
218 PetscReal lowest = 0.0, highest = 0.0;
219
220 PetscFunctionBeginUser;
221 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
222 PetscCall(VecSet(user->Nvert, 0.0));
223 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
224 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 7.0));
225
226 /* Before any sample the range is the documented degenerate, not a division. */
227 PetscCall(PicurvWindowValidFractionRange(user, &d, &storage, 0, &lowest, &highest));
228 PetscCall(PicurvAssertRealNear(1.0, lowest, 1.0e-12, "an unsampled window reports full coverage"));
229 PetscCall(PicurvAssertRealNear(0.0, highest, 1.0e-12, "an unsampled window reports no maximum"));
230
231 /* An unobstructed domain: every targeted point saw every state. */
232 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
233 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
234 PetscCall(PicurvWindowValidFractionRange(user, &d, &storage, 2, &lowest, &highest));
235 PetscCall(PicurvAssertRealNear(1.0, lowest, 1.0e-12, "a clear domain is fully covered"));
236 PetscCall(PicurvAssertRealNear(1.0, highest, 1.0e-12, "a clear domain has no over-counted point"));
237
238 /* Blank one interior cell and take a third state: that point now trails. */
239 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
240 nvert[3][3][3] = 1.0;
241 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
242 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
243 PetscCall(PicurvWindowValidFractionRange(user, &d, &storage, 3, &lowest, &highest));
244 PetscCall(PicurvAssertRealNear(2.0 / 3.0, lowest, 1.0e-12,
245 "a point blanked for one state trails the rest"));
246 PetscCall(PicurvAssertRealNear(1.0, highest, 1.0e-12, "unobstructed points stay fully covered"));
247
248 PetscCall(PicurvWindowStorageDestroy(&storage));
249 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
250 PetscFunctionReturn(0);
251}
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestValidFractionDetectsNeverValidPoint()

static PetscErrorCode TestValidFractionDetectsNeverValidPoint ( void  )
static

A point blanked from the start is reported as never valid.

Definition at line 256 of file test_statistics_accumulator.c.

257{
258 SimCtx *simCtx = NULL;
259 UserCtx *user = NULL;
260 PicurvWindowStorage storage;
261 PicurvWindowDefinition d = AccDefinition(PETSC_FALSE);
262 PetscReal ***nvert = NULL;
263 PetscReal lowest = 1.0, highest = 0.0;
264
265 PetscFunctionBeginUser;
266 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
267 PetscCall(VecSet(user->Nvert, 0.0));
268 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
269 nvert[3][3][3] = 1.0;
270 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
271
272 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
273 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 7.0));
274 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
275 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
276
277 PetscCall(PicurvWindowValidFractionRange(user, &d, &storage, 2, &lowest, &highest));
278 PetscCall(PicurvAssertRealNear(0.0, lowest, 1.0e-12,
279 "a permanently blanked point reports zero coverage"));
280 PetscCall(PicurvAssertRealNear(1.0, highest, 1.0e-12, "the rest of the domain is unaffected"));
281
282 PetscCall(PicurvWindowStorageDestroy(&storage));
283 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
284 PetscFunctionReturn(0);
285}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestCovarianceAccumulation()

static PetscErrorCode TestCovarianceAccumulation ( void  )
static

A vector-scalar covariance reproduces its analytically known components.

Definition at line 288 of file test_statistics_accumulator.c.

289{
290 SimCtx *simCtx = NULL;
291 UserCtx *user = NULL;
292 PicurvWindowStorage storage;
294 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
295 const PetscReal scalars[3] = {1.0, 2.0, 6.0};
296 /* Means are (3,4,5) and 3, so the centered sums are
297 * x: (-2)(-2)+(0)(-1)+(2)(3) = 10, y: (-2)(-2)+(2)(-1)+(0)(3) = 2,
298 * z: (-2)(-2)+(0)(-1)+(2)(3) = 10. */
299 const PetscReal expected[3] = {10.0, 2.0, 10.0};
300 const char *labels[3] = {"x", "y", "z"};
301 PetscReal value = 0.0;
302 char context[128];
303
304 PetscFunctionBeginUser;
305 d.covariance_count = 1;
308
309 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
310 PetscCall(VecSet(user->Nvert, 0.0));
311 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
312 {
313 PetscInt bs = 0;
314 PetscCall(VecGetBlockSize(storage.cm[0], &bs));
315 PetscCall(PicurvAssertIntEqual(3, bs, "a vector-scalar pair is one three-component vector"));
316 }
317
318 for (PetscInt s = 0; s < 3; ++s) {
319 PetscCall(SetUniform(user, series[s][0], series[s][1], series[s][2], scalars[s]));
320 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
321 }
322
323 for (PetscInt c = 0; c < 3; ++c) {
324 PetscCall(ReadComponentAt(user, storage.cm[0], 3, c, 2, 2, 2, &value));
325 PetscCall(PetscSNPrintf(context, sizeof(context), "Ucat-P co-moment %s", labels[c]));
326 PetscCall(PicurvAssertRealNear(expected[c], value, 1.0e-11, context));
327 }
328
329 /* The co-moment pass must not disturb the moments computed alongside it. */
330 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 2, 2, &value));
331 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "P mean is unaffected by the covariance pass"));
332 PetscCall(ReadComponentAt(user, storage.m2[0], 6, 0, 2, 2, 2, &value));
333 PetscCall(PicurvAssertRealNear(8.0, value, 1.0e-11, "Ucat xx product is unaffected by the covariance pass"));
334
335 PetscCall(PicurvWindowStorageDestroy(&storage));
336 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
337 PetscFunctionReturn(0);
338}
Vec * cm
One per covariance pair.
PetscInt first
First member; must also appear in the field list.
PetscInt second
Second member; must also appear in the field list.
PicurvWindowCovarianceRequest covariances[16]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestScalarSelfCovarianceMatchesSecondMoment()

static PetscErrorCode TestScalarSelfCovarianceMatchesSecondMoment ( void  )
static

A field's covariance with itself reduces exactly to its own second moment.

Definition at line 341 of file test_statistics_accumulator.c.

342{
343 SimCtx *simCtx = NULL;
344 UserCtx *user = NULL;
345 PicurvWindowStorage storage;
347 const PetscReal scalars[3] = {1.0, 2.0, 6.0};
348 PetscReal co_moment = 0.0;
349 PetscReal second_moment = 0.0;
350
351 PetscFunctionBeginUser;
352 /* Pairing P with itself makes the co-moment pass and the moment pass compute the
353 * same quantity by different routes, so any drift between them shows up here
354 * rather than only in a cross-field result no closed form covers. */
355 d.covariance_count = 1;
358
359 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
360 PetscCall(VecSet(user->Nvert, 0.0));
361 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
362
363 for (PetscInt s = 0; s < 3; ++s) {
364 PetscCall(SetUniform(user, 0.0, 0.0, 0.0, scalars[s]));
365 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
366 }
367
368 PetscCall(ReadScalarAt(user, storage.cm[0], 2, 2, 2, &co_moment));
369 PetscCall(ReadScalarAt(user, storage.m2[1], 2, 2, 2, &second_moment));
370 PetscCall(PicurvAssertRealNear(14.0, co_moment, 1.0e-11, "P self-covariance equals its centered sum"));
371 PetscCall(PicurvAssertBool((PetscBool)(co_moment == second_moment),
372 "self-covariance and second moment agree bit for bit"));
373
374 PetscCall(PicurvWindowStorageDestroy(&storage));
375 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
376 PetscFunctionReturn(0);
377}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestCovarianceRequiresFieldMembership()

static PetscErrorCode TestCovarianceRequiresFieldMembership ( void  )
static

A covariance member missing from the field list is rejected, not silently skipped.

Definition at line 380 of file test_statistics_accumulator.c.

381{
382 SimCtx *simCtx = NULL;
383 UserCtx *user = NULL;
384 PicurvWindowStorage storage;
385 PicurvWindowDefinition d = AccDefinition(PETSC_FALSE);
386 PetscErrorCode bad = 0;
387
388 PetscFunctionBeginUser;
389 /* Nvert is catalogued but is not in the window's field list, so no running mean
390 * exists for it and the co-moment update has nothing to center against. */
391 d.covariance_count = 1;
394
395 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
396 PetscCall(VecSet(user->Nvert, 0.0));
397 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
398 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 4.0));
399
400 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
401 bad = PicurvWindowAccumulate(user, &d, &storage, 1.0);
402 PetscCall(PetscPopErrorHandler());
403 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_WRONGSTATE, bad,
404 "a covariance member outside the field list is rejected"));
405
406 PetscCall(PicurvWindowStorageDestroy(&storage));
407 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
408 PetscFunctionReturn(0);
409}
@ FIELD_ID_NVERT
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestDerivedQuantities()

static PetscErrorCode TestDerivedQuantities ( void  )
static

Derived quantities reproduce the analytic values the accumulated state implies.

The series is the one the moment suite verifies by hand, so a failure here isolates the normalization and component selection rather than the accumulation: with three unit-weight samples, R_ij = C_ij / 3, and the diagonal of the Ucat tensor is 8/3 in each direction, giving TKE = 4 and an RMS of sqrt(8/3) per component.

Definition at line 419 of file test_statistics_accumulator.c.

420{
421 SimCtx *simCtx = NULL;
422 UserCtx *user = NULL;
423 PicurvWindowStorage storage;
425 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
426 const PetscReal scalars[3] = {1.0, 2.0, 6.0};
427 Vec scalar_target = NULL, vector_target = NULL;
428 PetscInt count = 0;
429 PetscReal value = 0.0;
430
431 PetscFunctionBeginUser;
432 d.covariance_count = 1;
435
436 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
437 PetscCall(VecSet(user->Nvert, 0.0));
438 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
439 for (PetscInt s = 0; s < 3; ++s) {
440 PetscCall(SetUniform(user, series[s][0], series[s][1], series[s][2], scalars[s]));
441 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
442 }
443 PetscCall(DMCreateGlobalVector(user->da, &scalar_target));
444 PetscCall(DMCreateGlobalVector(user->fda, &vector_target));
445
446 /* Requesting every output resolves each kind against what the window carries. */
447 PetscCall(PicurvWindowDerivedCount(&d, &storage, "mean,reynolds_stress,rms,tke,flux", &count));
448 /* 2 means + (6 Ucat + 1 P) stresses + (3 Ucat + 1 P) RMS + 1 TKE + 1 flux. */
449 PetscCall(PicurvAssertIntEqual(15, count, "every requested output is enumerated"));
450
451 for (PetscInt index = 0; index < count; ++index) {
452 PicurvDerivedField field;
453 PetscBool is_tke = PETSC_FALSE, is_rxx = PETSC_FALSE, is_rmsx = PETSC_FALSE;
454 PetscBool is_flux = PETSC_FALSE, is_pvar = PETSC_FALSE;
455
456 PetscCall(PicurvWindowDerive(user, &d, &storage, "mean,reynolds_stress,rms,tke,flux",
457 index, scalar_target, vector_target, &field));
458 PetscCall(PetscStrcmp(field.name, "acc_Ucat_tke", &is_tke));
459 PetscCall(PetscStrcmp(field.name, "acc_Ucat_R_xx", &is_rxx));
460 PetscCall(PetscStrcmp(field.name, "acc_Ucat_rmsx", &is_rmsx));
461 PetscCall(PetscStrcmp(field.name, "acc_Ucat_P_flux", &is_flux));
462 PetscCall(PetscStrcmp(field.name, "acc_P_variance", &is_pvar));
463
464 if (is_rxx) {
465 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &value));
466 PetscCall(PicurvAssertRealNear(8.0 / 3.0, value, 1.0e-11, "R_xx is C_xx over the weight"));
467 } else if (is_rmsx) {
468 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &value));
469 PetscCall(PicurvAssertRealNear(PetscSqrtReal(8.0 / 3.0), value, 1.0e-11,
470 "RMS is the root of the normal stress"));
471 } else if (is_tke) {
472 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &value));
473 PetscCall(PicurvAssertRealNear(4.0, value, 1.0e-11, "TKE is half the trace"));
474 } else if (is_pvar) {
475 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &value));
476 PetscCall(PicurvAssertRealNear(14.0 / 3.0, value, 1.0e-11, "a scalar product normalizes too"));
477 } else if (is_flux) {
478 Cmpnts ***flux = NULL;
479
480 PetscCall(DMDAVecGetArrayRead(user->fda, vector_target, &flux));
481 PetscCall(PicurvAssertRealNear(10.0 / 3.0, flux[2][2][2].x, 1.0e-11, "flux x"));
482 PetscCall(PicurvAssertRealNear(2.0 / 3.0, flux[2][2][2].y, 1.0e-11, "flux y"));
483 PetscCall(PicurvAssertRealNear(10.0 / 3.0, flux[2][2][2].z, 1.0e-11, "flux z"));
484 PetscCall(DMDAVecRestoreArrayRead(user->fda, vector_target, &flux));
485 }
486 }
487
488 /* A narrower request produces only what it asked for. */
489 PetscCall(PicurvWindowDerivedCount(&d, &storage, "tke", &count));
490 PetscCall(PicurvAssertIntEqual(1, count, "a single output kind enumerates once"));
491
492 /* An unsampled point is left at zero rather than divided by a zero weight. */
493 {
494 PicurvDerivedField field;
495 PetscReal ***nvert = NULL;
496
497 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
498 nvert[3][3][3] = 1.0;
499 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
500 PetscCall(PicurvWindowStorageDestroy(&storage));
501 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
502 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 4.0));
503 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
504 PetscCall(PicurvWindowDerive(user, &d, &storage, "tke", 0, scalar_target, vector_target, &field));
505 PetscCall(ReadScalarAt(user, scalar_target, 3, 3, 3, &value));
506 PetscCall(PicurvAssertRealNear(0.0, value, 1.0e-12,
507 "a never-sampled point derives to zero, not a division"));
508 }
509
510 /* Dimensionalization raises the field's own scale to the kind's own power: a mean
511 * is linear in it and a covariance is quadratic. One blanket velocity factor would
512 * be right for two of the five kinds and wrong for the other three, which is why
513 * derived statistics were left non-dimensional before this existed. */
514 {
515 PicurvDerivedField field;
516 PetscReal nondimensional_mean = 0.0, nondimensional_tke = 0.0;
517 PetscReal dimensional_mean = 0.0, dimensional_tke = 0.0;
518 const PetscReal velocity = 3.0;
519
520 PetscCall(PicurvWindowStorageDestroy(&storage));
521 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
522 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 4.0));
523 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
524 PetscCall(SetUniform(user, 3.0, 4.0, 5.0, 6.0));
525 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
526
527 /* The minimal fixture carries no post-processing settings; the harness teardown
528 * already frees them, so allocating here is symmetric. */
529 if (!user->simCtx->pps) PetscCall(PetscCalloc1(1, &user->simCtx->pps));
530 user->simCtx->pps->dimensionalize = PETSC_FALSE;
531 PetscCall(PicurvWindowDerive(user, &d, &storage, "mean", 0, scalar_target, vector_target, &field));
532 PetscCall(ReadComponentAt(user, vector_target, 3, 0, 2, 2, 2, &nondimensional_mean));
533 PetscCall(PicurvWindowDerive(user, &d, &storage, "tke", 0, scalar_target, vector_target, &field));
534 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &nondimensional_tke));
535
536 user->simCtx->scaling.U_ref = velocity;
537 user->simCtx->pps->dimensionalize = PETSC_TRUE;
538 PetscCall(PicurvWindowDerive(user, &d, &storage, "mean", 0, scalar_target, vector_target, &field));
539 PetscCall(ReadComponentAt(user, vector_target, 3, 0, 2, 2, 2, &dimensional_mean));
540 PetscCall(PicurvWindowDerive(user, &d, &storage, "tke", 0, scalar_target, vector_target, &field));
541 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &dimensional_tke));
542
543 PetscCall(PicurvAssertRealNear(velocity * nondimensional_mean, dimensional_mean, 1.0e-11,
544 "a mean carries the field's scale once"));
545 PetscCall(PicurvAssertRealNear(velocity * velocity * nondimensional_tke, dimensional_tke,
546 1.0e-11, "a turbulent kinetic energy carries it squared"));
547
548 /* An ensemble of an already scaled load is not scaled again. */
549 storage.physical_units = PETSC_TRUE;
550 PetscCall(PicurvWindowDerive(user, &d, &storage, "tke", 0, scalar_target, vector_target, &field));
551 PetscCall(ReadScalarAt(user, scalar_target, 2, 2, 2, &dimensional_tke));
552 PetscCall(PicurvAssertRealNear(nondimensional_tke, dimensional_tke, 1.0e-12,
553 "storage in physical units derives without a scale"));
554 user->simCtx->pps->dimensionalize = PETSC_FALSE;
555 user->simCtx->scaling.U_ref = 1.0;
556 }
557
558 /* An unknown output kind is refused rather than silently ignored. */
559 {
560 PetscErrorCode bad = 0;
561
562 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
563 bad = PicurvWindowDerivedCount(&d, &storage, "skewness", &count);
564 PetscCall(PetscPopErrorHandler());
565 PetscCall(PicurvAssertIntEqual(PETSC_ERR_ARG_WRONG, bad, "an unknown output kind is refused"));
566 }
567
568 PetscCall(VecDestroy(&vector_target));
569 PetscCall(VecDestroy(&scalar_target));
570 PetscCall(PicurvWindowStorageDestroy(&storage));
571 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
572 PetscFunctionReturn(0);
573}
char name[96]
Output field name, window qualified.
PetscErrorCode PicurvWindowDerive(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt index, Vec scalar_target, Vec vector_target, PicurvDerivedField *field)
Derives one output field from centered accumulator state.
PetscErrorCode PicurvWindowDerivedCount(const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt *count)
Reports how many derived fields a requested output set produces.
PetscBool physical_units
Holds physical units, as an ensemble of a dimensionalized load does, so derivation applies no scale; ...
One derived output field, resolved by enumeration index.
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
PetscBool dimensionalize
Whether derived output leaves non-dimensional form, from global_operations.dimensionalize.
Definition variables.h:828
ScalingCtx scaling
Definition variables.h:1003
PostProcessParams * pps
Definition variables.h:1131
PetscReal U_ref
Definition variables.h:899
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestSpatialMeanExcludesUnsampledPoints()

static PetscErrorCode TestSpatialMeanExcludesUnsampledPoints ( void  )
static

The spatial mean divides by sampled points, not by the whole vector.

A derived field is zero outside the target domain and wherever the mask never admitted a point. Those zeros are absences, and averaging over them silently scales the answer down by the fraction of the vector the window never covered — a wrong number that still looks plausible.

Definition at line 583 of file test_statistics_accumulator.c.

584{
585 SimCtx *simCtx = NULL;
586 UserCtx *user = NULL;
587 PicurvWindowStorage storage;
588 PicurvWindowDefinition d = AccDefinition(PETSC_FALSE);
590 PetscReal ***nvert = NULL;
591 PetscReal mean = 0.0;
592 PetscReal whole_vector_mean = 0.0;
593 PetscReal sum = 0.0;
594 PetscInt vector_size = 0;
595 PetscInt targeted = 0;
596
597 PetscFunctionBeginUser;
598 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
599 PetscCall(VecSet(user->Nvert, 0.0));
600 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
601 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 4.0));
602 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
603
604 /* A field holding exactly one over every sampled point: its masked mean must be
605 * one, whatever fraction of the vector the target domain occupies. */
606 PetscCall(VecZeroEntries(user->PostScalar));
608 PetscCall(SpatialTargetPlanGlobalPointCount(&plan, PETSC_COMM_WORLD, &targeted));
609 {
610 PetscReal ***values = NULL;
611
612 PetscCall(DMDAVecGetArray(user->da, user->PostScalar, &values));
613 for (PetscInt k = plan.start[2]; k < plan.end[2]; ++k)
614 for (PetscInt j = plan.start[1]; j < plan.end[1]; ++j)
615 for (PetscInt i = plan.start[0]; i < plan.end[0]; ++i) values[k][j][i] = 1.0;
616 PetscCall(DMDAVecRestoreArray(user->da, user->PostScalar, &values));
617 }
618
619 PetscCall(PicurvWindowSpatialMean(user, &d, &storage, user->PostScalar, &mean));
620 PetscCall(PicurvAssertRealNear(1.0, mean, 1.0e-12,
621 "the mean over sampled points ignores untargeted entries"));
622
623 /* The whole-vector average is genuinely different, which is what makes the
624 * distinction worth asserting rather than assuming. */
625 PetscCall(VecSum(user->PostScalar, &sum));
626 PetscCall(VecGetSize(user->PostScalar, &vector_size));
627 whole_vector_mean = sum / (PetscReal)vector_size;
628 PetscCall(PicurvAssertBool((PetscBool)(vector_size > targeted),
629 "the vector is larger than the targeted domain"));
630 PetscCall(PicurvAssertBool((PetscBool)(whole_vector_mean < 0.9),
631 "a whole-vector average would understate the result"));
632
633 /* Blanking a point removes it from both the sum and the count, so a uniform
634 * field still averages to its own value rather than being diluted. */
635 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
636 nvert[3][3][3] = 1.0;
637 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
638 PetscCall(PicurvWindowStorageDestroy(&storage));
639 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
640 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
641 PetscCall(PicurvWindowSpatialMean(user, &d, &storage, user->PostScalar, &mean));
642 PetscCall(PicurvAssertRealNear(1.0, mean, 1.0e-12,
643 "a never-sampled point is excluded from the average"));
644
645 PetscCall(PicurvWindowStorageDestroy(&storage));
646 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
647 PetscFunctionReturn(0);
648}
PetscErrorCode PicurvWindowSpatialMean(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, Vec field, PetscReal *mean)
Reports the spatial mean of a derived field over the points a window sampled.
@ PICURV_STATISTICS_MASK_FLUID
PetscInt end[3]
Exclusive end per dimension (i, j, k).
PetscInt start[3]
Inclusive start per dimension (i, j, k).
PetscErrorCode SpatialTargetPlanGlobalPointCount(const SpatialTargetPlan *plan, MPI_Comm comm, PetscInt *count)
Counts the points contributed across a communicator.
PetscErrorCode SpatialTargetPlanCreate(UserCtx *user, FieldId field_id, PicurvStatisticsMask mask, SpatialTargetPlan *plan)
Resolves the iteration domain for one field on one block.
Resolved iteration domain for one field on one block.
Vec PostScalar
Definition variables.h:1219
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestPayloadEnumeration()

static PetscErrorCode TestPayloadEnumeration ( void  )
static

Payload enumeration covers every vector exactly once with stable names.

Definition at line 651 of file test_statistics_accumulator.c.

652{
653 SimCtx *simCtx = NULL;
654 UserCtx *user = NULL;
655 PicurvWindowStorage storage;
657 /* 3 occupancy + 2 means + 2 products + 1 co-moment. Each product and co-moment
658 * is one payload carrying all of its components. */
659 const char *expected[8] = {
660 "count", "weight", "weight_sq",
661 "Ucat_mean", "P_mean",
662 "Ucat_m2", "P_m2",
663 "Ucat_P_cm"
664 };
665 const PetscInt expected_components[8] = {1, 1, 1, 3, 1, 6, 1, 3};
666 PetscInt count = 0;
667 PetscErrorCode bad = 0;
668
669 PetscFunctionBeginUser;
670 d.covariance_count = 1;
673
674 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
675 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
676 PetscCall(PicurvWindowStoragePayloadCount(&storage, &count));
677 PetscCall(PicurvAssertIntEqual(8, count, "every accumulator vector is enumerated once"));
678
679 for (PetscInt index = 0; index < count; ++index) {
681 PetscBool matches = PETSC_FALSE;
682 PetscInt block_size = 0;
683 char context[160];
684
685 PetscCall(PicurvWindowStoragePayload(user, &d, &storage, index, &payload));
686 PetscCall(PetscStrcmp(payload.name, expected[index], &matches));
687 PetscCall(PetscSNPrintf(context, sizeof(context), "payload %d is named '%s', got '%s'",
688 (int)index, expected[index], payload.name));
689 PetscCall(PicurvAssertBool(matches, context));
690 PetscCall(PicurvAssertBool((PetscBool)(payload.vec != NULL), "an enumerated payload has a vector"));
691 PetscCall(PicurvAssertBool((PetscBool)(payload.role != NULL), "an enumerated payload has a role"));
692
693 /* The declared component count must match the vector actually enumerated,
694 * because the manifest inventory records it and the reader trusts it. */
695 PetscCall(VecGetBlockSize(payload.vec, &block_size));
696 PetscCall(PetscSNPrintf(context, sizeof(context), "payload '%s' declares %d components",
697 payload.name, (int)expected_components[index]));
698 PetscCall(PicurvAssertIntEqual(expected_components[index], payload.components, context));
699 PetscCall(PicurvAssertIntEqual(payload.components, block_size,
700 "the declared component count matches the vector"));
701 }
702
703 /* A field without a requested second moment contributes no product payload. */
704 {
705 PicurvWindowDefinition partial = AccDefinition(PETSC_FALSE);
707
708 partial.fields[0].want_second = PETSC_TRUE; /* Ucat keeps its product, P does not. */
709 PetscCall(PicurvWindowStorageCreate(user, &partial, &lean));
710 PetscCall(PicurvWindowStoragePayloadCount(&lean, &count));
711 PetscCall(PicurvAssertIntEqual(6, count, "a field without a second moment adds no payload"));
712 PetscCall(PicurvWindowStorageDestroy(&lean));
713 }
714
715 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
716 bad = PicurvWindowStoragePayload(user, &d, &storage, count, NULL);
717 PetscCall(PetscPopErrorHandler());
718 PetscCall(PicurvAssertBool((PetscBool)(bad != 0), "an out-of-range payload index is rejected"));
719
720 PetscCall(PicurvWindowStorageDestroy(&storage));
721 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
722 PetscFunctionReturn(0);
723}
PetscInt components
Degrees of freedom the vector carries.
Vec vec
Borrowed accumulator vector; never owned by the caller.
PetscErrorCode PicurvWindowStoragePayload(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, PetscInt index, PicurvStatisticsPayload *payload)
Resolves one enumerated payload of a window's storage.
const char * role
Inventory role: occupancy, mean, second_moment, co_moment.
char name[96]
File basename, no extension.
PetscErrorCode PicurvWindowStoragePayloadCount(const PicurvWindowStorage *storage, PetscInt *count)
Reports how many checkpointable vectors one window's storage holds.
One checkpointable accumulator vector, resolved by enumeration index.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestWeightedAccumulation()

static PetscErrorCode TestWeightedAccumulation ( void  )
static

Unequal weights reproduce the weighted result the kernels define.

Definition at line 726 of file test_statistics_accumulator.c.

727{
728 SimCtx *simCtx = NULL;
729 UserCtx *user = NULL;
730 PicurvWindowStorage storage;
732 PetscReal value = 0.0;
733
734 PetscFunctionBeginUser;
735 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
736 PetscCall(VecSet(user->Nvert, 0.0));
737 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
738
739 /* P = 1 with weight 1, then P = 3 with weight 3: mean 2.5, M2 = 3. */
740 PetscCall(SetUniform(user, 0.0, 0.0, 0.0, 1.0));
741 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
742 PetscCall(SetUniform(user, 0.0, 0.0, 0.0, 3.0));
743 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 3.0));
744
745 PetscCall(ReadScalarAt(user, storage.weight, 2, 2, 2, &value));
746 PetscCall(PicurvAssertRealNear(4.0, value, 1.0e-12, "weighted total weight"));
747 PetscCall(ReadScalarAt(user, storage.weight_sq, 2, 2, 2, &value));
748 PetscCall(PicurvAssertRealNear(10.0, value, 1.0e-12, "weighted squared-weight sum"));
749 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 2, 2, &value));
750 PetscCall(PicurvAssertRealNear(2.5, value, 1.0e-12, "weighted mean"));
751 PetscCall(ReadScalarAt(user, storage.m2[1], 2, 2, 2, &value));
752 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-11, "weighted centered second moment"));
753
754 PetscCall(PicurvWindowStorageDestroy(&storage));
755 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
756 PetscFunctionReturn(0);
757}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestMaskedPointsAreExcluded()

static PetscErrorCode TestMaskedPointsAreExcluded ( void  )
static

Masked points accumulate nothing, and remain distinguishable from unsampled ones.

Definition at line 760 of file test_statistics_accumulator.c.

761{
762 SimCtx *simCtx = NULL;
763 UserCtx *user = NULL;
764 PicurvWindowStorage storage;
765 PicurvWindowDefinition d = AccDefinition(PETSC_FALSE);
766 PetscReal ***nvert = NULL;
767 PetscReal value = 0.0;
768
769 PetscFunctionBeginUser;
770 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
771 PetscCall(VecSet(user->Nvert, 0.0));
772 /* Blank a single interior cell. */
773 PetscCall(DMDAVecGetArray(user->da, user->Nvert, &nvert));
774 nvert[3][3][3] = 1.0;
775 PetscCall(DMDAVecRestoreArray(user->da, user->Nvert, &nvert));
776
777 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
778 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, 7.0));
779 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
780 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
781
782 PetscCall(ReadScalarAt(user, storage.count, 2, 2, 2, &value));
783 PetscCall(PicurvAssertRealNear(2.0, value, 1.0e-12, "a fluid point sees every state"));
784 PetscCall(ReadScalarAt(user, storage.count, 3, 3, 3, &value));
785 PetscCall(PicurvAssertRealNear(0.0, value, 1.0e-12, "a blanked point accumulates no sample"));
786 PetscCall(ReadScalarAt(user, storage.weight, 3, 3, 3, &value));
787 PetscCall(PicurvAssertRealNear(0.0, value, 1.0e-12, "a blanked point accumulates no weight"));
788 PetscCall(ReadScalarAt(user, storage.mean[1], 3, 3, 3, &value));
789 PetscCall(PicurvAssertRealNear(0.0, value, 1.0e-12, "a blanked point keeps an untouched mean"));
790
791 PetscCall(PicurvWindowStorageDestroy(&storage));
792 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
793 PetscFunctionReturn(0);
794}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestConstantFieldZeroProduct()

static PetscErrorCode TestConstantFieldZeroProduct ( void  )
static

A constant field yields exactly zero product at every point.

Definition at line 797 of file test_statistics_accumulator.c.

798{
799 SimCtx *simCtx = NULL;
800 UserCtx *user = NULL;
801 PicurvWindowStorage storage;
803 PetscReal value = 0.0;
804
805 PetscFunctionBeginUser;
806 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
807 PetscCall(VecSet(user->Nvert, 0.0));
808 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
809
810 for (PetscInt s = 0; s < 5; ++s) {
811 PetscCall(SetUniform(user, 2.5, -1.5, 4.0, 9.0));
812 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0 + 0.5 * (PetscReal)s));
813 }
814 for (PetscInt c = 0; c < 6; ++c) {
815 PetscCall(ReadComponentAt(user, storage.m2[0], 6, c, 2, 2, 2, &value));
816 PetscCall(PicurvAssertBool((PetscBool)(value == 0.0),
817 "a constant field gives bitwise-zero products"));
818 }
819 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 2, 2, &value));
820 PetscCall(PicurvAssertRealNear(9.0, value, 1.0e-12, "a constant field keeps its value as the mean"));
821
822 PetscCall(PicurvWindowStorageDestroy(&storage));
823 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
824 PetscFunctionReturn(0);
825}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestRunloopDriverAppliesScheduledStates()

static PetscErrorCode TestRunloopDriverAppliesScheduledStates ( void  )
static

The runloop entry point applies exactly the states the schedule accepts.

Drives FieldStatisticsUpdateWindows directly, so the scheduling decision and the field accumulation are exercised through the same path the solver uses rather than being tested only in isolation.

Definition at line 834 of file test_statistics_accumulator.c.

835{
836 SimCtx *simCtx = NULL;
837 UserCtx *user = NULL;
838 PicurvWindow window;
840 PicurvWindowStorage storage;
841 PetscReal value = 0.0;
842 const PetscReal dt = 0.5;
843
844 PetscFunctionBeginUser;
845 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
846 PetscCall(VecSet(user->Nvert, 0.0));
847
848 /* Stride two, physical-time weighting, starting at the origin. */
850 d.step_cadence = 2;
851 PetscCall(PicurvWindowInit(&window, &d));
852 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
853
854 simCtx->fieldStatisticsEnabled = PETSC_TRUE;
855 simCtx->fieldStatisticsWindowCount = 1;
856 simCtx->fieldStatisticsWindows = &window;
857 user->fieldStatisticsStorage = &storage;
858
859 /* Steps 0,2,4 are due. Step 0 anchors; steps 2 and 4 each represent 1.0 of time. */
860 for (PetscInt step = 0; step <= 4; ++step) {
861 PetscCall(SetUniform(user, 1.0, 2.0, 3.0, (PetscReal)(step + 1)));
862 PetscCall(FieldStatisticsUpdateWindows(simCtx, step, (PetscReal)step * dt));
863 }
864
865 PetscCall(PicurvAssertIntEqual(2, window.sample_count, "stride two over five steps accepts two samples"));
866 PetscCall(PicurvAssertRealNear(2.0, window.total_weight, 1.0e-12, "accepted weights span the elapsed time"));
867
868 /* Only the due states reached the field accumulators. */
869 PetscCall(ReadScalarAt(user, storage.count, 2, 2, 2, &value));
870 PetscCall(PicurvAssertRealNear(2.0, value, 1.0e-12, "per-point count matches the accepted samples"));
871 PetscCall(ReadScalarAt(user, storage.weight, 2, 2, 2, &value));
872 PetscCall(PicurvAssertRealNear(2.0, value, 1.0e-12, "per-point weight matches the accepted weights"));
873
874 /* P carried 3 at step 2 and 5 at step 4, each with weight 1: mean 4, M2 = 2. */
875 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 2, 2, &value));
876 PetscCall(PicurvAssertRealNear(4.0, value, 1.0e-12, "only the scheduled samples contribute to the mean"));
877 PetscCall(ReadScalarAt(user, storage.m2[1], 2, 2, 2, &value));
878 PetscCall(PicurvAssertRealNear(2.0, value, 1.0e-11, "second moment reflects only the scheduled samples"));
879
880 /* A disabled subsystem is inert on the same path. */
881 simCtx->fieldStatisticsEnabled = PETSC_FALSE;
882 PetscCall(FieldStatisticsUpdateWindows(simCtx, 6, 3.0));
883 PetscCall(PicurvAssertIntEqual(2, window.sample_count, "a disabled subsystem accepts nothing"));
884
885 simCtx->fieldStatisticsWindows = NULL;
886 simCtx->fieldStatisticsWindowCount = 0;
887 user->fieldStatisticsStorage = NULL;
888 PetscCall(PicurvWindowStorageDestroy(&storage));
889 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
890 PetscFunctionReturn(0);
891}
PetscInt sample_count
PetscErrorCode FieldStatisticsUpdateWindows(struct SimCtx *simCtx, PetscInt step, PetscReal time)
Offers one completed state to every configured window.
PetscReal total_weight
PetscErrorCode PicurvWindowInit(PicurvWindow *window, const PicurvWindowDefinition *definition)
Validates a definition and initializes a window to the pending state.
@ PICURV_WEIGHTING_PHYSICAL_TIME
Weight is the represented interval.
Runtime state of one window.
PetscInt fieldStatisticsWindowCount
Definition variables.h:989
PetscBool fieldStatisticsEnabled
Definition variables.h:988
struct PicurvWindow * fieldStatisticsWindows
Definition variables.h:990
struct PicurvWindowStorage * fieldStatisticsStorage
Definition variables.h:1222
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PlaceParticle()

static void PlaceParticle ( PetscReal *  velocity,
PetscReal *  psi,
PetscInt *  cell,
PetscInt  p,
PetscInt  ci,
PetscInt  cj,
PetscInt  ck,
const PetscReal  v[3],
PetscReal  value 
)
static

Places one particle in a geometric cell with the given velocity and scalar.

Definition at line 896 of file test_statistics_accumulator.c.

898{
899 for (PetscInt c = 0; c < 3; ++c) velocity[3 * p + c] = v[c];
900 psi[p] = value;
901 cell[3 * p + 0] = ci; cell[3 * p + 1] = cj; cell[3 * p + 2] = ck;
902}
Here is the caller graph for this function:

◆ TestParticleEnsembleMatchesStreamingState()

static PetscErrorCode TestParticleEnsembleMatchesStreamingState ( void  )
static

A cell's particles form the same state the streaming accumulator builds.

Three particles carry the suite's reference series in one cell, so the two-pass ensemble must reproduce the means, symmetric products and vector-scalar co-moment that the streaming tests assert for the same samples. A lone particle in another cell has its own values as mean and no spread; an empty cell stays unsampled.

Definition at line 912 of file test_statistics_accumulator.c.

913{
914 SimCtx *simCtx = NULL;
915 UserCtx *user = NULL;
916 PicurvWindowStorage storage;
918 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
919 const PetscReal scalars[3] = {1.0, 2.0, 6.0};
920 const PetscReal products[6] = {8.0, 4.0, 8.0, 8.0, 4.0, 8.0};
921 const PetscReal co_moment[3] = {10.0, 2.0, 10.0};
922 const PetscReal lone[3] = {-1.0, 0.5, 2.0};
923 PetscReal *velocity = NULL, *psi = NULL, value = 0.0;
924 PetscInt *cell = NULL, nlocal = 0;
925 PetscMPIInt rank = 0;
926 char context[128];
927
928 PetscFunctionBeginUser;
929 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
930 memset(&d, 0, sizeof(d));
931 strncpy(d.name, "cells", PICURV_WINDOW_NAME_LENGTH - 1);
934 d.step_cadence = 1;
935 d.field_count = 2;
936 d.fields[0].field_id = FIELD_ID_UCAT; d.fields[0].want_second = PETSC_TRUE;
937 d.fields[1].field_id = FIELD_ID_PSI; d.fields[1].want_second = PETSC_TRUE;
938 d.covariance_count = 1;
941
942 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
943 PetscCall(VecSet(user->Nvert, 0.0));
944 nlocal = (rank == 0) ? 4 : 0;
945 PetscCall(PicurvCreateSwarmPair(user, nlocal, "unused"));
946 PetscCall(DMSwarmGetField(user->swarm, "velocity", NULL, NULL, (void **)&velocity));
947 PetscCall(DMSwarmGetField(user->swarm, "Psi", NULL, NULL, (void **)&psi));
948 PetscCall(DMSwarmGetField(user->swarm, "DMSwarm_CellID", NULL, NULL, (void **)&cell));
949 if (nlocal) {
950 for (PetscInt p = 0; p < 3; ++p) PlaceParticle(velocity, psi, cell, p, 1, 2, 3, series[p], scalars[p]);
951 PlaceParticle(velocity, psi, cell, 3, 0, 0, 0, lone, 4.0);
952 }
953 PetscCall(DMSwarmRestoreField(user->swarm, "DMSwarm_CellID", NULL, NULL, (void **)&cell));
954 PetscCall(DMSwarmRestoreField(user->swarm, "Psi", NULL, NULL, (void **)&psi));
955 PetscCall(DMSwarmRestoreField(user->swarm, "velocity", NULL, NULL, (void **)&velocity));
956
957 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
958 PetscCall(PicurvWindowStorageFromParticles(user, &d, &storage));
959
960 /* Geometric cell (1,2,3) is storage point (2,3,4). */
961 PetscCall(ReadScalarAt(user, storage.count, 2, 3, 4, &value));
962 PetscCall(PicurvAssertRealNear(3.0, value, 0.0, "the cell holds three samples"));
963 for (PetscInt c = 0; c < 3; ++c) {
964 PetscCall(ReadComponentAt(user, storage.mean[0], 3, c, 2, 3, 4, &value));
965 PetscCall(PicurvAssertRealNear(3.0 + c, value, 1.0e-12, "velocity ensemble mean"));
966 PetscCall(ReadComponentAt(user, storage.cm[0], 3, c, 2, 3, 4, &value));
967 PetscCall(PetscSNPrintf(context, sizeof(context), "velocity-Psi co-moment component %d", (int)c));
968 PetscCall(PicurvAssertRealNear(co_moment[c], value, 1.0e-11, context));
969 }
970 for (PetscInt c = 0; c < 6; ++c) {
971 PetscCall(ReadComponentAt(user, storage.m2[0], 6, c, 2, 3, 4, &value));
972 PetscCall(PetscSNPrintf(context, sizeof(context), "velocity product component %d", (int)c));
973 PetscCall(PicurvAssertRealNear(products[c], value, 1.0e-11, context));
974 }
975 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 3, 4, &value));
976 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "Psi ensemble mean"));
977 PetscCall(ReadScalarAt(user, storage.m2[1], 2, 3, 4, &value));
978 PetscCall(PicurvAssertRealNear(14.0, value, 1.0e-11, "Psi centred sum (4 + 1 + 9)"));
979
980 PetscCall(ReadComponentAt(user, storage.mean[0], 3, 0, 1, 1, 1, &value));
981 PetscCall(PicurvAssertRealNear(-1.0, value, 1.0e-12, "a lone particle is its cell's mean"));
982 PetscCall(ReadScalarAt(user, storage.m2[1], 1, 1, 1, &value));
983 PetscCall(PicurvAssertRealNear(0.0, value, 0.0, "a lone particle has no spread"));
984 PetscCall(ReadScalarAt(user, storage.weight, 3, 3, 3, &value));
985 PetscCall(PicurvAssertRealNear(0.0, value, 0.0, "an empty cell is unsampled"));
986
987 PetscCall(PicurvWindowStorageDestroy(&storage));
988 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
989 PetscFunctionReturn(0);
990}
@ FIELD_ID_PSI
PetscErrorCode PicurvWindowStorageFromParticles(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage)
Fills a window's storage from the particles sampling each cell at one instant.
static void PlaceParticle(PetscReal *velocity, PetscReal *psi, PetscInt *cell, PetscInt p, PetscInt ci, PetscInt cj, PetscInt ck, const PetscReal v[3], PetscReal value)
Places one particle in a geometric cell with the given velocity and scalar.
PetscErrorCode PicurvCreateSwarmPair(UserCtx *user, PetscInt nlocal, const char *post_field_name)
Creates matched solver and post-processing swarms for tests.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReductionScalar()

static PetscReal ReductionScalar ( PetscInt  i,
PetscInt  j,
PetscInt  k,
PetscInt  s 
)
static

Closed-form test field: a scalar varying in every direction and over samples.

Definition at line 993 of file test_statistics_accumulator.c.

994{
995 return 0.3 * i - 0.2 * k * k + 0.1 * i * j + (s ? 0.5 * (i + 2 * k) : -0.25 * j);
996}
Here is the caller graph for this function:

◆ TestReductionEqualsPooledMoments()

static PetscErrorCode TestReductionEqualsPooledMoments ( void  )
static

A homogeneous merge equals the moments of the slot's pooled sample set.

Two states of a field varying across the averaged plane are accumulated, then the state is merged over i and k. Each j slot must hold the mean and variance of all of its points' samples pooled, which differs from the average of the point variances by the spread of the point means. The merged state also derives through the unchanged derivation.

Definition at line 1007 of file test_statistics_accumulator.c.

1008{
1009 SimCtx *simCtx = NULL;
1010 UserCtx *user = NULL;
1011 PicurvWindowStorage storage, reduced;
1012 PicurvWindowDefinition d = AccDefinition(PETSC_TRUE);
1013 const PetscBool over_ik[3] = {PETSC_TRUE, PETSC_FALSE, PETSC_TRUE};
1014 SpatialTargetPlan plan;
1015 PicurvDerivedField derived;
1016 PetscReal value = 0.0;
1017
1018 PetscFunctionBeginUser;
1019 d.field_count = 1;
1020 d.fields[0].field_id = FIELD_ID_P;
1021 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
1022 PetscCall(VecSet(user->Nvert, 0.0));
1023 PetscCall(VecSet(user->lNvert, 0.0));
1024 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
1025 PetscCall(PicurvWindowStorageCreate(user, &d, &reduced));
1026 for (PetscInt s = 0; s < 2; ++s) {
1027 PetscReal ***p = NULL;
1028
1029 PetscCall(DMDAVecGetArray(user->da, user->P, &p));
1030 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
1031 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
1032 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i)
1033 p[k][j][i] = ReductionScalar(i, j, k, s);
1034 PetscCall(DMDAVecRestoreArray(user->da, user->P, &p));
1035 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
1036 }
1037 PetscCall(PicurvWindowStorageReduce(user, &d, &storage, over_ik, &reduced));
1039
1040 for (PetscInt j = plan.start[1]; j < plan.end[1]; ++j) {
1041 PetscReal sum = 0.0, sum_sq = 0.0, n = 0.0, mean = 0.0;
1042 char context[128];
1043
1044 /* A single rank owns the whole plane, so the plan spans its physical points. */
1045 for (PetscInt k = plan.start[2]; k < plan.end[2]; ++k)
1046 for (PetscInt i = plan.start[0]; i < plan.end[0]; ++i)
1047 for (PetscInt s = 0; s < 2; ++s) { sum += ReductionScalar(i, j, k, s); n += 1.0; }
1048 mean = sum / n;
1049 for (PetscInt k = plan.start[2]; k < plan.end[2]; ++k)
1050 for (PetscInt i = plan.start[0]; i < plan.end[0]; ++i)
1051 for (PetscInt s = 0; s < 2; ++s) sum_sq += PetscSqr(ReductionScalar(i, j, k, s) - mean);
1052
1053 PetscCall(ReadScalarAt(user, reduced.mean[0], 3, j, 2, &value));
1054 PetscCall(PetscSNPrintf(context, sizeof(context), "pooled mean of slot j=%d", (int)j));
1055 PetscCall(PicurvAssertRealNear(mean, value, 1.0e-12, context));
1056 PetscCall(ReadScalarAt(user, reduced.m2[0], 1, j, 3, &value));
1057 PetscCall(PetscSNPrintf(context, sizeof(context), "pooled variance of slot j=%d", (int)j));
1058 PetscCall(PicurvAssertRealNear(sum_sq / n, value, 1.0e-12, context));
1059 PetscCall(ReadScalarAt(user, reduced.weight, 2, j, 2, &value));
1060 PetscCall(PicurvAssertRealNear(1.0, value, 0.0, "a sampled slot carries unit weight"));
1061 if (j == 2) {
1062 PetscCall(PicurvWindowDerive(user, &d, &reduced, "rms", 0, user->PostScalar, user->PostVector, &derived));
1063 PetscCall(ReadScalarAt(user, user->PostScalar, 2, j, 1, &value));
1064 PetscCall(PicurvAssertRealNear(PetscSqrtReal(sum_sq / n), value, 1.0e-12,
1065 "the merged state derives through the unchanged derivation"));
1066 }
1067 }
1068
1069 PetscCall(PicurvWindowStorageDestroy(&reduced));
1070 PetscCall(PicurvWindowStorageDestroy(&storage));
1071 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
1072 PetscFunctionReturn(0);
1073}
PetscErrorCode PicurvWindowStorageReduce(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const PetscBool average_direction[3], PicurvWindowStorage *reduced)
Merges a window's pointwise state over homogeneous directions.
static PetscReal ReductionScalar(PetscInt i, PetscInt j, PetscInt k, PetscInt s)
Closed-form test field: a scalar varying in every direction and over samples.
Vec lNvert
Definition variables.h:1197
Vec PostVector
Definition variables.h:1220
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetParticleState()

static PetscErrorCode SetParticleState ( UserCtx *  user,
PetscReal  psi_value,
PetscReal  particles 
)
static

Sets Psi and the particle count uniformly, so a state is occupied or not everywhere.

Definition at line 1076 of file test_statistics_accumulator.c.

1077{
1078 PetscFunctionBeginUser;
1079 PetscCall(VecSet(user->Psi, psi_value));
1080 PetscCall(VecSet(user->ParticleCount, particles));
1081 PetscFunctionReturn(0);
1082}
Vec ParticleCount
Definition variables.h:1276
Here is the caller graph for this function:

◆ TestParticleSampledFieldUsesOccupiedStates()

static PetscErrorCode TestParticleSampledFieldUsesOccupiedStates ( void  )
static

A particle-sampled field is accumulated over the occupied states only.

Three states of the suite's reference series, the second with no particle in any cell. Ucat is ordinary, so it keeps all three samples; Psi and the Ucat-Psi pair keep the first and third. The pair is centred on Ucat's mean over those two states, (3,3,5), not on its all-state mean, (3,4,5). Psi's outputs are flagged as occupied-set outputs.

Definition at line 1092 of file test_statistics_accumulator.c.

1093{
1094 SimCtx *simCtx = NULL;
1095 UserCtx *user = NULL;
1096 PicurvWindowStorage storage;
1097 PicurvWindowDefinition d = AccDefinition(PETSC_TRUE);
1098 const PetscReal series[3][3] = {{1.0, 2.0, 3.0}, {3.0, 6.0, 5.0}, {5.0, 4.0, 7.0}};
1099 const PetscReal scalars[3] = {1.0, 2.0, 6.0};
1100 const PetscReal occupied[3] = {1.0, 0.0, 3.0};
1101 /* Over states 0 and 2: Psi mean 3.5 and centred sum 2 * 2.5^2; the co-moment against
1102 * Ucat about (3,3,5) is (-2)(-2.5) + (2)(2.5) = 10, (-1)(-2.5) + (1)(2.5) = 5, and 10. */
1103 const PetscReal co_moment[3] = {10.0, 5.0, 10.0};
1104 PicurvDerivedField derived;
1105 PetscReal value = 0.0;
1106
1107 PetscFunctionBeginUser;
1109 d.covariance_count = 1;
1112 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
1113 PetscCall(VecSet(user->Nvert, 0.0));
1114 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
1115 PetscCall(PicurvAssertBool((PetscBool)(storage.occupied_count && storage.occupied_mean[0] &&
1116 !storage.occupied_mean[1]),
1117 "the occupied set and Ucat's occupied-state mean exist; Psi needs none"));
1118 for (PetscInt s = 0; s < 3; ++s) {
1119 PetscCall(SetUniform(user, series[s][0], series[s][1], series[s][2], 0.0));
1120 PetscCall(SetParticleState(user, scalars[s], occupied[s]));
1121 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
1122 }
1123
1124 PetscCall(ReadScalarAt(user, storage.count, 2, 2, 2, &value));
1125 PetscCall(PicurvAssertRealNear(3.0, value, 0.0, "every state is a sample of the ordinary set"));
1126 PetscCall(ReadScalarAt(user, storage.occupied_count, 2, 2, 2, &value));
1127 PetscCall(PicurvAssertRealNear(2.0, value, 0.0, "only the occupied states are samples of Psi"));
1128 PetscCall(ReadComponentAt(user, storage.mean[0], 3, 1, 2, 2, 2, &value));
1129 PetscCall(PicurvAssertRealNear(4.0, value, 1.0e-12, "Ucat's own mean keeps every state"));
1130 PetscCall(ReadComponentAt(user, storage.occupied_mean[0], 3, 1, 2, 2, 2, &value));
1131 PetscCall(PicurvAssertRealNear(3.0, value, 1.0e-12, "Ucat's occupied-state mean keeps states 0 and 2"));
1132 PetscCall(ReadScalarAt(user, storage.mean[1], 2, 2, 2, &value));
1133 PetscCall(PicurvAssertRealNear(3.5, value, 1.0e-12, "Psi's mean skips the empty state"));
1134 PetscCall(ReadScalarAt(user, storage.m2[1], 2, 2, 2, &value));
1135 PetscCall(PicurvAssertRealNear(12.5, value, 1.0e-11, "Psi's centred sum skips the empty state"));
1136 for (PetscInt c = 0; c < 3; ++c) {
1137 PetscCall(ReadComponentAt(user, storage.cm[0], 3, c, 2, 2, 2, &value));
1138 PetscCall(PicurvAssertRealNear(co_moment[c], value, 1.0e-11, "the pair is centred on occupied means"));
1139 }
1140
1141 /* Outputs: index 0 is Ucat's mean, 1 Psi's, then the flux (mean, flux only). */
1142 PetscCall(PicurvWindowDerive(user, &d, &storage, "mean,flux", 0, user->PostScalar, user->PostVector, &derived));
1143 PetscCall(PicurvAssertBool((PetscBool)!derived.occupied, "Ucat's mean is an all-state output"));
1144 PetscCall(PicurvWindowDerive(user, &d, &storage, "mean,flux", 2, user->PostScalar, user->PostVector, &derived));
1145 PetscCall(PicurvAssertBool(derived.occupied, "the Ucat-Psi flux is an occupied-set output"));
1146 PetscCall(ReadComponentAt(user, user->PostVector, 3, 1, 2, 2, 2, &value));
1147 PetscCall(PicurvAssertRealNear(2.5, value, 1.0e-12, "the flux divides by the occupied weight"));
1148
1149 PetscCall(PicurvWindowStorageDestroy(&storage));
1150 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
1151 PetscFunctionReturn(0);
1152}
Vec occupied_count
Occupied-state count; NULL without a particle-sampled field.
PetscBool occupied
Measured over the occupied states, so undefined where they are absent.
Vec * occupied_mean
Per field: its mean over the occupied states, for an ordinary field paired with a particle-sampled on...
static PetscErrorCode SetParticleState(UserCtx *user, PetscReal psi_value, PetscReal particles)
Sets Psi and the particle count uniformly, so a state is occupied or not everywhere.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TestReductionPoolsOnlyOccupiedSamples()

static PetscErrorCode TestReductionPoolsOnlyOccupiedSamples ( void  )
static

A homogeneous merge of a particle-sampled field pools only its occupied samples.

Psi takes the value i at cell i while occupied, and the cells with i odd never hold a particle; their zeros must not enter the plane mean.

Definition at line 1160 of file test_statistics_accumulator.c.

1161{
1162 SimCtx *simCtx = NULL;
1163 UserCtx *user = NULL;
1164 PicurvWindowStorage storage, reduced;
1165 PicurvWindowDefinition d = AccDefinition(PETSC_FALSE);
1166 const PetscBool over_ik[3] = {PETSC_TRUE, PETSC_FALSE, PETSC_TRUE};
1167 SpatialTargetPlan plan;
1168 PetscReal ***psi = NULL, ***particles = NULL, value = 0.0, sum = 0.0, n = 0.0;
1169
1170 PetscFunctionBeginUser;
1171 d.field_count = 1;
1173 PetscCall(PicurvCreateMinimalContexts(&simCtx, &user, ACC_N, ACC_N, ACC_N));
1174 PetscCall(VecSet(user->Nvert, 0.0));
1175 PetscCall(VecSet(user->lNvert, 0.0));
1176 PetscCall(DMDAVecGetArray(user->da, user->Psi, &psi));
1177 PetscCall(DMDAVecGetArray(user->da, user->ParticleCount, &particles));
1178 for (PetscInt k = user->info.zs; k < user->info.zs + user->info.zm; ++k)
1179 for (PetscInt j = user->info.ys; j < user->info.ys + user->info.ym; ++j)
1180 for (PetscInt i = user->info.xs; i < user->info.xs + user->info.xm; ++i) {
1181 particles[k][j][i] = (i % 2) ? 0.0 : 4.0;
1182 psi[k][j][i] = (i % 2) ? 0.0 : (PetscReal)i;
1183 }
1184 PetscCall(DMDAVecRestoreArray(user->da, user->ParticleCount, &particles));
1185 PetscCall(DMDAVecRestoreArray(user->da, user->Psi, &psi));
1186 PetscCall(PicurvWindowStorageCreate(user, &d, &storage));
1187 PetscCall(PicurvWindowStorageCreate(user, &d, &reduced));
1188 PetscCall(PicurvWindowAccumulate(user, &d, &storage, 1.0));
1189 PetscCall(PicurvWindowStorageReduce(user, &d, &storage, over_ik, &reduced));
1190
1192 for (PetscInt i = plan.start[0]; i < plan.end[0]; ++i) {
1193 if (i % 2 == 0) { sum += (PetscReal)i; n += 1.0; }
1194 }
1195 PetscCall(ReadScalarAt(user, reduced.mean[0], 1, 2, 3, &value));
1196 PetscCall(PicurvAssertRealNear(sum / n, value, 1.0e-12, "the plane mean pools occupied cells only"));
1197 PetscCall(ReadScalarAt(user, reduced.occupied_weight, 1, 2, 3, &value));
1198 PetscCall(PicurvAssertRealNear(1.0, value, 0.0, "the merged occupied set is sampled in the plane"));
1199
1200 PetscCall(PicurvWindowStorageDestroy(&reduced));
1201 PetscCall(PicurvWindowStorageDestroy(&storage));
1202 PetscCall(PicurvDestroyMinimalContexts(&simCtx, &user));
1203 PetscFunctionReturn(0);
1204}
Vec occupied_weight
Occupied-state weight; NULL without a particle-sampled field.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Entry point for the accumulator suite.

Definition at line 1209 of file test_statistics_accumulator.c.

1210{
1211 PetscErrorCode ierr;
1212 const PicurvTestCase cases[] = {
1213 {"component-counts", TestComponentCounts},
1214 {"storage-shape", TestStorageShape},
1215 {"known-accumulation-across-field", TestKnownAccumulationAcrossField},
1216 {"weighted-accumulation", TestWeightedAccumulation},
1217 {"masked-points-excluded", TestMaskedPointsAreExcluded},
1218 {"constant-field-zero-product", TestConstantFieldZeroProduct},
1219 {"valid-fraction-range", TestValidFractionRange},
1220 {"valid-fraction-detects-never-valid-point", TestValidFractionDetectsNeverValidPoint},
1221 {"covariance-accumulation", TestCovarianceAccumulation},
1222 {"scalar-self-covariance-matches-second-moment", TestScalarSelfCovarianceMatchesSecondMoment},
1223 {"covariance-requires-field-membership", TestCovarianceRequiresFieldMembership},
1224 {"payload-enumeration", TestPayloadEnumeration},
1225 {"derived-quantities", TestDerivedQuantities},
1226 {"spatial-mean-excludes-unsampled-points", TestSpatialMeanExcludesUnsampledPoints},
1227 {"runloop-driver-applies-scheduled-states", TestRunloopDriverAppliesScheduledStates},
1228 {"particle-ensemble-matches-streaming-state", TestParticleEnsembleMatchesStreamingState},
1229 {"reduction-equals-pooled-moments", TestReductionEqualsPooledMoments},
1230 {"particle-sampled-field-uses-occupied-states", TestParticleSampledFieldUsesOccupiedStates},
1231 {"reduction-pools-only-occupied-samples", TestReductionPoolsOnlyOccupiedSamples},
1232 };
1233
1234 ierr = PetscInitialize(&argc, &argv, NULL, "PICurv statistics accumulator tests");
1235 if (ierr) return (int)ierr;
1236 ierr = PicurvRunTests("unit-statistics-accumulator", cases, sizeof(cases) / sizeof(cases[0]));
1237 if (ierr) { PetscFinalize(); return (int)ierr; }
1238 ierr = PetscFinalize();
1239 return (int)ierr;
1240}
static PetscErrorCode TestCovarianceAccumulation(void)
A vector-scalar covariance reproduces its analytically known components.
static PetscErrorCode TestWeightedAccumulation(void)
Unequal weights reproduce the weighted result the kernels define.
static PetscErrorCode TestStorageShape(void)
Storage allocates one mean per field and the right number of product components.
static PetscErrorCode TestValidFractionRange(void)
The valid-fraction range reports mask coverage, including a never-valid point.
static PetscErrorCode TestParticleEnsembleMatchesStreamingState(void)
A cell's particles form the same state the streaming accumulator builds.
static PetscErrorCode TestValidFractionDetectsNeverValidPoint(void)
A point blanked from the start is reported as never valid.
static PetscErrorCode TestRunloopDriverAppliesScheduledStates(void)
The runloop entry point applies exactly the states the schedule accepts.
static PetscErrorCode TestDerivedQuantities(void)
Derived quantities reproduce the analytic values the accumulated state implies.
static PetscErrorCode TestParticleSampledFieldUsesOccupiedStates(void)
A particle-sampled field is accumulated over the occupied states only.
static PetscErrorCode TestMaskedPointsAreExcluded(void)
Masked points accumulate nothing, and remain distinguishable from unsampled ones.
static PetscErrorCode TestReductionPoolsOnlyOccupiedSamples(void)
A homogeneous merge of a particle-sampled field pools only its occupied samples.
static PetscErrorCode TestKnownAccumulationAcrossField(void)
Three accepted states reproduce the analytically known moments at every point.
static PetscErrorCode TestScalarSelfCovarianceMatchesSecondMoment(void)
A field's covariance with itself reduces exactly to its own second moment.
static PetscErrorCode TestConstantFieldZeroProduct(void)
A constant field yields exactly zero product at every point.
static PetscErrorCode TestSpatialMeanExcludesUnsampledPoints(void)
The spatial mean divides by sampled points, not by the whole vector.
static PetscErrorCode TestPayloadEnumeration(void)
Payload enumeration covers every vector exactly once with stable names.
static PetscErrorCode TestCovarianceRequiresFieldMembership(void)
A covariance member missing from the field list is rejected, not silently skipped.
static PetscErrorCode TestComponentCounts(void)
Component counts follow the documented product shapes.
static PetscErrorCode TestReductionEqualsPooledMoments(void)
A homogeneous merge equals the moments of the slot's pooled sample set.
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.
Named test case descriptor consumed by PicurvRunTests.
Here is the call graph for this function: