22#define STATISTICS_DERIVED_OUTPUT_LENGTH 256
43_Static_assert(offsetof(
SymTensor, xx) == 0 *
sizeof(PetscReal),
"SymTensor order: xx is component 0");
44_Static_assert(offsetof(
SymTensor, xy) == 1 *
sizeof(PetscReal),
"SymTensor order: xy is component 1");
45_Static_assert(offsetof(
SymTensor, xz) == 2 *
sizeof(PetscReal),
"SymTensor order: xz is component 2");
46_Static_assert(offsetof(
SymTensor, yy) == 3 *
sizeof(PetscReal),
"SymTensor order: yy is component 3");
47_Static_assert(offsetof(
SymTensor, yz) == 4 *
sizeof(PetscReal),
"SymTensor order: yz is component 4");
48_Static_assert(offsetof(
SymTensor, zz) == 5 *
sizeof(PetscReal),
"SymTensor order: zz is component 5");
51static const char *
const kAxisName[3] = {
"x",
"y",
"z"};
62 for (PetscInt c = 0; c < 6; ++c) {
76 PetscFunctionBeginUser;
78 PetscCheck(index >= 0 && index < 6, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
79 "Product component %" PetscInt_FMT
" is outside the symmetric set.", index);
83 PetscFunctionReturn(0);
92 PetscFunctionBeginUser;
94 PetscCheck(count != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Count output is required.");
95 PetscCheck(dof == 1 || dof == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
96 "Self-products are supported for scalar and three-vector fields, got dof %" PetscInt_FMT
".", dof);
97 *count = (dof == 1) ? 1 : 6;
99 PetscFunctionReturn(0);
108 PetscFunctionBeginUser;
110 PetscCheck(count != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Count output is required.");
113 PetscCheck((dof_a == 1 && dof_b == 1) || (dof_a == 3 && dof_b == 1) || (dof_a == 1 && dof_b == 3),
114 PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
115 "Covariance is supported for scalar-scalar and vector-scalar pairs, got dof %" PetscInt_FMT
116 " and %" PetscInt_FMT
".", dof_a, dof_b);
117 *count = (dof_a == 3 || dof_b == 3) ? 3 : 1;
119 PetscFunctionReturn(0);
128 PetscFunctionBeginUser;
130 PetscCheck(user != NULL && dm != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
131 "Context and DM output are required.");
132 switch (components) {
133 case 1: *dm = user->
da;
break;
134 case 3: *dm = user->
fda;
break;
135 case 6: *dm = user->
fda6;
break;
137 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
138 "No block DM carries %" PetscInt_FMT
" accumulator components.", components);
140 PetscCheck(*dm != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
141 "The block DM for %" PetscInt_FMT
" accumulator components was never created.",
144 PetscFunctionReturn(0);
193 Vec *count, Vec *weight, Vec *weight_sq)
216 return storage->
mean[slot];
226 PetscFunctionBeginUser;
228 PetscCheck(user != NULL && definition != NULL && storage != NULL,
229 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, definition, and storage are required.");
230 PetscCall(PetscMemzero(storage,
sizeof(*storage)));
235 PetscCall(DMCreateGlobalVector(user->
da, &storage->
count));
236 PetscCall(VecSet(storage->
count, 0.0));
237 PetscCall(DMCreateGlobalVector(user->
da, &storage->
weight));
238 PetscCall(VecSet(storage->
weight, 0.0));
239 PetscCall(DMCreateGlobalVector(user->
da, &storage->
weight_sq));
240 PetscCall(VecSet(storage->
weight_sq, 0.0));
243 PetscCall(PetscCalloc1((
size_t)definition->
field_count, &storage->
mean));
244 PetscCall(PetscCalloc1((
size_t)definition->
field_count, &storage->
m2));
246 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
248 PetscInt components = 0;
249 DM product_dm = NULL;
254 PetscCall(DMCreateGlobalVector(view.
dm, &storage->
mean[field_index]));
255 PetscCall(VecSet(storage->
mean[field_index], 0.0));
259 PetscCall(DMCreateGlobalVector(product_dm, &storage->
m2[field_index]));
260 PetscCall(VecSet(storage->
m2[field_index], 0.0));
267 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
278 for (PetscInt pair_index = 0; pair_index < definition->
covariance_count; ++pair_index) {
280 PetscInt components = 0;
287 PetscCall(DMCreateGlobalVector(pair_dm, &storage->
cm[pair_index]));
288 PetscCall(VecSet(storage->
cm[pair_index], 0.0));
309 PetscInt payloads = 0;
317 "Statistics window '%s' block %d: %d accumulator vector(s) over %d point(s).\n",
318 definition->
name, (
int)user->
_this, (
int)payloads, (
int)points);
321 PetscFunctionReturn(0);
330 PetscFunctionBeginUser;
333 if (storage->
count) PetscCall(VecDestroy(&storage->
count));
334 if (storage->
weight) PetscCall(VecDestroy(&storage->
weight));
336 for (PetscInt field_index = 0; storage->
mean && field_index < storage->
field_count; ++field_index) {
337 if (storage->
mean[field_index]) PetscCall(VecDestroy(&storage->
mean[field_index]));
339 for (PetscInt field_index = 0; storage->
m2 && field_index < storage->
field_count; ++field_index) {
340 if (storage->
m2[field_index]) PetscCall(VecDestroy(&storage->
m2[field_index]));
342 for (PetscInt pair_index = 0; storage->
cm && pair_index < storage->
covariance_count; ++pair_index) {
343 if (storage->
cm[pair_index]) PetscCall(VecDestroy(&storage->
cm[pair_index]));
352 if (storage->
mean) PetscCall(PetscFree(storage->
mean));
353 if (storage->
m2) PetscCall(PetscFree(storage->
m2));
354 if (storage->
cm) PetscCall(PetscFree(storage->
cm));
355 PetscCall(PetscMemzero(storage,
sizeof(*storage)));
357 PetscFunctionReturn(0);
369 PetscFunctionBeginUser;
372 for (PetscInt field_index = 0; field_index < definition->
field_count && *slot < 0; ++field_index) {
373 if (definition->
fields[field_index].
field_id == field_id) *slot = field_index;
375 PetscCheck(*slot >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE,
376 "Window '%s' requests a covariance over field '%s', which is not in its field list.",
379 PetscFunctionReturn(0);
388 PetscFunctionBeginUser;
390 PetscCheck(storage != NULL && count != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
391 "Storage and count output are required.");
398 for (PetscInt field_index = 0; storage->
m2 && field_index < storage->
field_count; ++field_index) {
399 if (storage->
m2[field_index]) *count += 1;
402 PetscFunctionReturn(0);
414 PetscInt cursor = index;
416 PetscFunctionBeginUser;
418 PetscCheck(user != NULL && definition != NULL && storage != NULL && payload != NULL,
419 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
420 "Context, definition, storage, and payload output are required.");
422 PetscCheck(index >= 0 && index < total, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
423 "Payload index %" PetscInt_FMT
" is outside [0, %" PetscInt_FMT
").", index, total);
424 PetscCall(PetscMemzero(payload,
sizeof(*payload)));
431 static const char *
const occupancy_name[6] = {
"count",
"weight",
"weight_sq",
"occupied_count",
432 "occupied_weight",
"occupied_weight_sq"};
437 if (cursor < occupancy_count) {
438 PetscCall(PetscStrncpy(payload->
name, occupancy_name[cursor],
sizeof(payload->
name)));
439 payload->
vec = occupancy[cursor];
441 payload->
role =
"occupancy";
444 PetscFunctionReturn(0);
446 cursor -= occupancy_count;
449 if (cursor < storage->field_count) {
453 PetscCall(PetscSNPrintf(payload->
name,
sizeof(payload->
name),
"%s_mean",
455 payload->
vec = storage->
mean[cursor];
457 payload->
role =
"mean";
460 PetscFunctionReturn(0);
467 if (cursor-- != 0)
continue;
469 PetscCall(PetscSNPrintf(payload->
name,
sizeof(payload->
name),
"%s_occupied_mean",
473 payload->
role =
"mean";
476 PetscFunctionReturn(0);
480 PetscInt product_count = 0;
483 for (PetscInt field_index = 0; storage->
m2 && field_index < storage->
field_count; ++field_index) {
484 if (storage->
m2[field_index]) ++product_count;
486 if (cursor < product_count) {
487 for (PetscInt field_index = 0; field_index < storage->
field_count; ++field_index) {
490 if (!storage->
m2[field_index])
continue;
491 if (seen++ != cursor)
continue;
493 PetscCall(PetscSNPrintf(payload->
name,
sizeof(payload->
name),
"%s_m2",
495 payload->
vec = storage->
m2[field_index];
497 payload->
role =
"second_moment";
500 PetscFunctionReturn(0);
503 cursor -= product_count;
510 PetscCheck(cursor >= 0 && cursor < storage->covariance_count, PETSC_COMM_SELF, PETSC_ERR_PLIB,
511 "Payload index %" PetscInt_FMT
" fell through the storage enumeration.", index);
514 PetscCall(PetscSNPrintf(payload->
name,
sizeof(payload->
name),
"%s_%s_cm",
516 payload->
vec = storage->
cm[cursor];
518 payload->
role =
"co_moment";
522 PetscFunctionReturn(0);
538 "mean",
"reynolds_stress",
"rms",
"tke",
"flux",
"point_variance"
568 char *cursor = buffer;
570 PetscFunctionBeginUser;
573 PetscCheck(outputs != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Output list is required.");
574 PetscCall(PetscStrncpy(buffer, outputs,
sizeof(buffer)));
576 while (cursor && *cursor) {
577 char *comma = strchr(cursor,
',');
578 PetscBool matched = PETSC_FALSE;
580 if (comma) *comma =
'\0';
582 if (cursor[0] !=
'\0') {
585 wanted[k] = PETSC_TRUE;
586 matched = PETSC_TRUE;
589 PetscCheck(matched, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
590 "Unknown field-statistics output '%s'. Available outputs are "
591 "mean, reynolds_stress, rms, tke, flux, and point_variance.", cursor);
593 cursor = comma ? comma + 1 : NULL;
596 PetscFunctionReturn(0);
609 PetscFunctionBeginUser;
619 for (PetscInt field_index = 0; storage->
m2 && field_index < storage->
field_count; ++field_index) {
621 PetscInt components = 0;
623 if (!storage->
m2[field_index])
continue;
634 for (PetscInt field_index = 0; storage->
m2 && field_index < storage->
field_count; ++field_index) {
637 if (!storage->
m2[field_index])
continue;
639 if (descriptor->
dof == 3) *count += 1;
647 PetscFunctionReturn(0);
656 const char *outputs, PetscInt *count)
660 PetscFunctionBeginUser;
662 PetscCheck(definition != NULL && storage != NULL && count != NULL,
663 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Definition, storage, and count are required.");
669 if (!wanted[k])
continue;
674 PetscFunctionReturn(0);
686 PetscFunctionBeginUser;
688 if (variance >= 0.0) {
689 *result = PetscSqrtReal(variance);
691 PetscFunctionReturn(0);
694 "Derived variance for '%s' is %g, which is too negative to be floating-point "
695 "cancellation; the accumulated state is inconsistent.", label, (
double)variance);
698 PetscFunctionReturn(0);
709 PetscInt index,
DerivedKind *kind, PetscInt *offset)
711 PetscFunctionBeginUser;
716 if (!wanted[k])
continue;
718 if (index < extent) {
722 PetscFunctionReturn(0);
726 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
727 "Derived index is past the end of the requested output set.");
737 PetscInt index, Vec scalar_target, Vec vector_target,
743 PetscReal ***weight_arr = NULL, ***weight_sq_arr = NULL, ***count_arr = NULL;
744 PetscScalar ****source = NULL, ****target = NULL;
747 Vec source_vec = NULL;
748 PetscInt offset = 0, slot = 0, member = 0;
750 Vec count_vec = NULL, weight_vec = NULL, weight_sq_vec = NULL;
753 PetscReal dimensional_factor = 1.0;
756 PetscInt source_components = 0;
758 PetscFunctionBeginUser;
760 PetscCheck(user != NULL && definition != NULL && storage != NULL && field != NULL,
761 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
762 "Context, definition, storage, and output are required.");
763 PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
764 "Derived index %" PetscInt_FMT
" is negative.", index);
765 PetscCall(PetscMemzero(field,
sizeof(*field)));
777 source_vec = storage->
mean[slot];
780 source_components = descriptor->
dof;
781 PetscCall(PetscSNPrintf(field->
name,
sizeof(field->
name),
"%s_%s_mean",
785 for (slot = 0; slot < storage->
field_count; ++slot) {
786 if (!storage->
m2[slot])
continue;
788 if (descriptor->
dof != 3)
continue;
789 if (offset-- == 0)
break;
791 source_vec = storage->
m2[slot];
795 PetscCall(PetscSNPrintf(field->
name,
sizeof(field->
name),
"%s_%s_tke",
808 source_vec = storage->
cm[slot];
811 PetscReal first_scale = 1.0, second_scale = 1.0;
815 &first_scale, NULL, 0));
817 &second_scale, NULL, 0));
818 dimensional_factor = first_scale * second_scale;
820 PetscCall(PetscSNPrintf(field->
name,
sizeof(field->
name),
"%s_%s_%s_flux",
827 for (slot = 0; slot < storage->
field_count; ++slot) {
830 if (!storage->
m2[slot])
continue;
834 if (offset < extent) { member = offset;
break; }
837 source_vec = storage->
m2[slot];
842 PetscCall(PetscSNPrintf(field->
name,
sizeof(field->
name),
"%s_%s_%s%s",
847 }
else if (descriptor->
dof == 1) {
848 PetscCall(PetscSNPrintf(field->
name,
sizeof(field->
name),
"%s_%s_variance",
854 PetscCall(PetscSNPrintf(field->
name,
sizeof(field->
name),
"%s_%s_R_%s",
864 Vec destination = (field->
components == 1) ? scalar_target : vector_target;
865 DM destination_dm = NULL;
867 PetscCheck(destination != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
868 "Derived field '%s' needs a %d-component destination.",
871 PetscCall(VecZeroEntries(destination));
875 PetscCall(DMDAVecGetArrayRead(user->
da, weight_vec, &weight_arr));
876 PetscCall(DMDAVecGetArrayRead(user->
da, weight_sq_vec, &weight_sq_arr));
877 PetscCall(DMDAVecGetArrayRead(user->
da, count_vec, &count_arr));
878 PetscCall(DMDAVecGetArrayDOFRead(source_dm, source_vec, &source));
879 PetscCall(DMDAVecGetArrayDOF(destination_dm, destination, &target));
881 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k) {
882 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j) {
883 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
888 if (weight_arr[k][j][i] <= 0.0)
continue;
889 pair.
count = count_arr[k][j][i];
890 pair.
weight = weight_arr[k][j][i];
896 for (PetscInt c = 0; c < field->
components; ++c) {
897 target[k][j][i][c] = source[k][j][i][c];
902 PetscReal trace = 0.0;
904 for (PetscInt c = 0; c < 3; ++c) {
908 target[k][j][i][0] = 0.5 * trace;
910 PetscReal deviation = 0.0;
912 pair.
cm = source[k][j][i][(descriptor->
dof == 1)
915 field->
name, &deviation));
916 target[k][j][i][0] = (kind ==
DERIVED_RMS) ? deviation : deviation * deviation;
918 for (PetscInt c = 0; c < field->
components; ++c) {
919 pair.
cm = source[k][j][i][c];
923 pair.
cm = source[k][j][i][member];
930 PetscCall(DMDAVecRestoreArrayDOF(destination_dm, destination, &target));
931 PetscCall(DMDAVecRestoreArrayDOFRead(source_dm, source_vec, &source));
932 PetscCall(DMDAVecRestoreArrayRead(user->
da, count_vec, &count_arr));
933 PetscCall(DMDAVecRestoreArrayRead(user->
da, weight_sq_vec, &weight_sq_arr));
934 PetscCall(DMDAVecRestoreArrayRead(user->
da, weight_vec, &weight_arr));
940 PetscReal base = 1.0;
946 if (PetscAbsReal(dimensional_factor - 1.0) > PETSC_MACHINE_EPSILON) {
947 PetscCall(VecScale(destination, dimensional_factor));
954 PetscFunctionReturn(0);
967 const PetscBool whole_domain[3] = {PETSC_TRUE, PETSC_TRUE, PETSC_TRUE};
969 PetscFunctionBeginUser;
971 PetscCheck(user != NULL && definition != NULL && storage != NULL && field != NULL && mean != NULL,
972 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
973 "Context, definition, storage, field, and output are required.");
985 whole_domain, PETSC_COMM_WORLD, NULL, mean));
987 PetscFunctionReturn(0);
996 PetscReal *minimum, PetscReal *maximum)
999 PetscReal ***nvert = NULL;
1000 PetscReal ***count_arr = NULL;
1001 PetscReal local_min = PETSC_MAX_REAL;
1002 PetscReal local_max = 0.0;
1003 PetscReal reduced_min = 0.0, reduced_max = 0.0;
1005 PetscFunctionBeginUser;
1007 PetscCheck(user != NULL && definition != NULL && storage != NULL &&
1008 minimum != NULL && maximum != NULL,
1009 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, definition, storage, and outputs are required.");
1016 PetscCall(DMDAVecGetArrayRead(user->
da, user->
Nvert, &nvert));
1017 PetscCall(DMDAVecGetArrayRead(user->
da, storage->
count, &count_arr));
1018 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k) {
1019 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j) {
1020 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
1024 const PetscReal fraction = count_arr[k][j][i] / (PetscReal)sample_count;
1026 local_min = PetscMin(local_min, fraction);
1027 local_max = PetscMax(local_max, fraction);
1031 PetscCall(DMDAVecRestoreArrayRead(user->
da, storage->
count, &count_arr));
1032 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
Nvert, &nvert));
1036 PetscCallMPI(MPI_Allreduce(&local_min, &reduced_min, 1, MPIU_REAL, MPIU_MIN, PETSC_COMM_WORLD));
1037 PetscCallMPI(MPI_Allreduce(&local_max, &reduced_max, 1, MPIU_REAL, MPIU_MAX, PETSC_COMM_WORLD));
1038 *minimum = (reduced_min == PETSC_MAX_REAL) ? 1.0 : reduced_min;
1039 *maximum = reduced_max;
1041 PetscFunctionReturn(0);
1052 PetscReal ***nvert = NULL, ***particles = NULL;
1054 PetscReal ***count_arr[2] = {NULL, NULL}, ***weight_arr[2] = {NULL, NULL}, ***weight_sq_arr[2] = {NULL, NULL};
1055 const PetscBool occupied_set = (PetscBool)(storage != NULL && storage->
occupied_count != NULL);
1057 PetscFunctionBeginUser;
1059 PetscCheck(user != NULL && definition != NULL && storage != NULL,
1060 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, definition, and storage are required.");
1061 PetscCheck(weight > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
1062 "Accepted states carry a positive weight, got %g.", (
double)weight);
1070 PetscCall(DMDAVecGetArrayRead(user->
da, user->
Nvert, &nvert));
1071 for (PetscInt set = 0; set < (occupied_set ? 2 : 1); ++set) {
1072 Vec count = NULL, sum = NULL, sum_sq = NULL;
1075 PetscCall(DMDAVecGetArray(user->
da, count, &count_arr[set]));
1076 PetscCall(DMDAVecGetArray(user->
da, sum, &weight_arr[set]));
1077 PetscCall(DMDAVecGetArray(user->
da, sum_sq, &weight_sq_arr[set]));
1081 if (occupied_set) PetscCall(DMDAVecGetArrayRead(user->
da, user->
ParticleCount, &particles));
1086 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k) {
1087 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j) {
1088 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
1090 for (PetscInt set = 0; set < (occupied_set ? 2 : 1); ++set) {
1092 count_arr[set][k][j][i] += 1.0;
1093 weight_arr[set][k][j][i] += weight;
1094 weight_sq_arr[set][k][j][i] += weight * weight;
1106 PetscScalar ****src_a = NULL, ****mean_a = NULL;
1107 PetscScalar ****src_b = NULL, ****mean_b = NULL;
1108 PetscScalar ****co_moment = NULL;
1110 PetscInt slot_a = 0, slot_b = 0, dof_a = 0, dof_b = 0, components = 0;
1124 PetscCall(DMDAVecGetArrayDOFRead(view_a.
dm, view_a.
global_vec, &src_a));
1125 PetscCall(DMDAVecGetArrayDOFRead(view_a.
dm,
SampleSetMean(storage, set, slot_a), &mean_a));
1126 PetscCall(DMDAVecGetArrayDOFRead(view_b.
dm, view_b.
global_vec, &src_b));
1127 PetscCall(DMDAVecGetArrayDOFRead(view_b.
dm,
SampleSetMean(storage, set, slot_b), &mean_b));
1128 PetscCall(DMDAVecGetArrayDOF(pair_dm, storage->
cm[pair], &co_moment));
1130 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k) {
1131 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j) {
1132 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
1135 for (PetscInt c = 0; c < components; ++c) {
1139 const PetscInt component_a = (dof_a == 3) ? c : 0;
1140 const PetscInt component_b = (dof_b == 3) ? c : 0;
1143 state.
count = count_arr[set][k][j][i] - 1.0;
1144 state.
weight = weight_arr[set][k][j][i] - weight;
1145 state.
weight_sq = weight_sq_arr[set][k][j][i] - weight * weight;
1146 state.
mean_x = mean_a[k][j][i][component_a];
1147 state.
mean_y = mean_b[k][j][i][component_b];
1148 state.
cm = co_moment[k][j][i][c];
1150 src_a[k][j][i][component_a],
1151 src_b[k][j][i][component_b], weight));
1152 co_moment[k][j][i][c] = state.
cm;
1158 PetscCall(DMDAVecRestoreArrayDOF(pair_dm, storage->
cm[pair], &co_moment));
1159 PetscCall(DMDAVecRestoreArrayDOFRead(view_b.
dm,
SampleSetMean(storage, set, slot_b), &mean_b));
1160 PetscCall(DMDAVecRestoreArrayDOFRead(view_b.
dm, view_b.
global_vec, &src_b));
1161 PetscCall(DMDAVecRestoreArrayDOFRead(view_a.
dm,
SampleSetMean(storage, set, slot_a), &mean_a));
1162 PetscCall(DMDAVecRestoreArrayDOFRead(view_a.
dm, view_a.
global_vec, &src_a));
1170 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
1175 PetscScalar ****src = NULL, ****mean = NULL, ****product = NULL, ****occupied = NULL;
1176 DM product_dm = NULL;
1177 PetscInt dof = 0, components = 0;
1181 PetscCall(DMDAVecGetArrayDOFRead(view.
dm, view.
global_vec, &src));
1182 PetscCall(DMDAVecGetArrayDOF(view.
dm, storage->
mean[field_index], &mean));
1183 if (occupied_mean) PetscCall(DMDAVecGetArrayDOF(view.
dm, occupied_mean, &occupied));
1187 PetscCall(DMDAVecGetArrayDOF(product_dm, storage->
m2[field_index], &product));
1190 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k) {
1191 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j) {
1192 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
1193 PetscReal prior_weight = 0.0;
1194 PetscReal prior_weight_sq = 0.0;
1195 PetscReal prior_count = 0.0;
1198 if (occupied && particles[k][j][i] > 0.0) {
1199 for (PetscInt c = 0; c < dof; ++c) {
1205 moment.
mean = occupied[k][j][i][c];
1208 occupied[k][j][i][c] = moment.
mean;
1213 prior_weight = weight_arr[set][k][j][i] - weight;
1214 prior_weight_sq = weight_sq_arr[set][k][j][i] - weight * weight;
1215 prior_count = count_arr[set][k][j][i] - 1.0;
1219 for (PetscInt c = 0; c < components; ++c) {
1224 state.
count = prior_count;
1225 state.
weight = prior_weight;
1227 state.
mean_x = mean[k][j][i][a];
1228 state.
mean_y = mean[k][j][i][b];
1229 state.
cm = product[k][j][i][c];
1231 src[k][j][i][b], weight));
1232 product[k][j][i][c] = state.
cm;
1235 for (PetscInt c = 0; c < dof; ++c) {
1238 moment.
count = prior_count;
1239 moment.
weight = prior_weight;
1241 moment.
mean = mean[k][j][i][c];
1244 mean[k][j][i][c] = moment.
mean;
1250 if (second) PetscCall(DMDAVecRestoreArrayDOF(product_dm, storage->
m2[field_index], &product));
1251 if (occupied_mean) PetscCall(DMDAVecRestoreArrayDOF(view.
dm, occupied_mean, &occupied));
1252 PetscCall(DMDAVecRestoreArrayDOF(view.
dm, storage->
mean[field_index], &mean));
1253 PetscCall(DMDAVecRestoreArrayDOFRead(view.
dm, view.
global_vec, &src));
1256 if (occupied_set) PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
ParticleCount, &particles));
1257 for (PetscInt set = (occupied_set ? 1 : 0); set >= 0; --set) {
1258 Vec count = NULL, sum = NULL, sum_sq = NULL;
1261 PetscCall(DMDAVecRestoreArray(user->
da, sum_sq, &weight_sq_arr[set]));
1262 PetscCall(DMDAVecRestoreArray(user->
da, sum, &weight_arr[set]));
1263 PetscCall(DMDAVecRestoreArray(user->
da, count, &count_arr[set]));
1265 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
Nvert, &nvert));
1267 PetscFunctionReturn(0);
1277 PetscInt first[6], PetscInt second[6])
1280 for (PetscInt c = 0; c < components; ++c) {
1281 first[c] = self ? ((dof_a == 1) ? 0 :
kProductFirst[c]) : ((dof_a == 3) ? c : 0);
1282 second[c] = self ? ((dof_a == 1) ? 0 :
kProductSecond[c]) : ((dof_b == 3) ? c : 0);
1297 DM dm_b, Vec mean_b, PetscInt components,
1298 const PetscInt first[6],
const PetscInt second[6],
1299 DM product_dm, Vec product)
1302 const PetscReal *values_a = NULL, *values_b = NULL;
1303 const PetscInt *cell = NULL;
1304 PetscScalar ****centre_a = NULL, ****centre_b = NULL, ****sum = NULL;
1305 PetscInt nlocal = 0, gxs, gys, gzs, gxm, gym, gzm;
1306 Vec local_sum = NULL;
1308 PetscFunctionBeginUser;
1312 PetscCall(DMDAGetGhostCorners(product_dm, &gxs, &gys, &gzs, &gxm, &gym, &gzm));
1313 PetscCall(DMGetLocalVector(product_dm, &local_sum));
1314 PetscCall(VecZeroEntries(local_sum));
1316 PetscCall(DMSwarmGetLocalSize(user->
swarm, &nlocal));
1317 PetscCall(DMSwarmGetField(user->
swarm, descriptor_a->
canonical_name, NULL, NULL, (
void **)&values_a));
1319 if (field_b == field_a) values_b = values_a;
1320 else PetscCall(DMSwarmGetField(user->
swarm, descriptor_b->canonical_name, NULL, NULL, (
void **)&values_b));
1322 PetscCall(DMDAVecGetArrayDOFRead(dm_a, mean_a, ¢re_a));
1323 PetscCall(DMDAVecGetArrayDOFRead(dm_b, mean_b, ¢re_b));
1324 PetscCall(DMDAVecGetArrayDOF(product_dm, local_sum, &sum));
1326 for (PetscInt p = 0; p < nlocal; ++p) {
1328 const PetscInt i = cell[3 * p + 0] + 1, j = cell[3 * p + 1] + 1, k = cell[3 * p + 2] + 1;
1329 const PetscReal *a = &values_a[p * descriptor_a->
components];
1330 const PetscReal *b = &values_b[p * descriptor_b->components];
1332 if (i < gxs || i >= gxs + gxm || j < gys || j >= gys + gym || k < gzs || k >= gzs + gzm)
continue;
1333 for (PetscInt c = 0; c < components; ++c) {
1334 sum[k][j][i][c] += (a[first[c]] - centre_a[k][j][i][first[c]]) *
1335 (b[second[c]] - centre_b[k][j][i][second[c]]);
1339 PetscCall(DMDAVecRestoreArrayDOF(product_dm, local_sum, &sum));
1340 PetscCall(DMDAVecRestoreArrayDOFRead(dm_b, mean_b, ¢re_b));
1341 PetscCall(DMDAVecRestoreArrayDOFRead(dm_a, mean_a, ¢re_a));
1343 if (field_b != field_a) PetscCall(DMSwarmRestoreField(user->
swarm, descriptor_b->canonical_name, NULL, NULL, (
void **)&values_b));
1344 PetscCall(DMSwarmRestoreField(user->
swarm, descriptor_a->
canonical_name, NULL, NULL, (
void **)&values_a));
1346 PetscCall(DMLocalToGlobalBegin(product_dm, local_sum, ADD_VALUES, product));
1347 PetscCall(DMLocalToGlobalEnd(product_dm, local_sum, ADD_VALUES, product));
1348 PetscCall(DMRestoreLocalVector(product_dm, &local_sum));
1350 PetscFunctionReturn(0);
1364 PetscFunctionBeginUser;
1366 PetscCheck(user != NULL && definition != NULL && storage != NULL && user->
swarm != NULL,
1367 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, swarm, definition, and storage are required.");
1372 for (PetscInt set = 0; set < (storage->
occupied_count ? 2 : 1); ++set) {
1373 Vec count = NULL, weight = NULL, weight_sq = NULL;
1382 for (PetscInt f = 0; f < definition->
field_count; ++f) {
1390 PetscCall(DMCreateLocalVector(view[f].dm, ¢re[f]));
1391 PetscCall(DMGlobalToLocalBegin(view[f].dm, storage->
mean[f], INSERT_VALUES, centre[f]));
1392 PetscCall(DMGlobalToLocalEnd(view[f].dm, storage->
mean[f], INSERT_VALUES, centre[f]));
1396 for (PetscInt f = 0; f < definition->
field_count; ++f) {
1398 PetscInt components = 0, first[6], second[6];
1399 DM product_dm = NULL;
1406 view[f].dm, centre[f], components, first, second,
1407 product_dm, storage->
m2[f]));
1410 PetscInt a = 0, b = 0, components = 0, first[6], second[6];
1419 view[b].dm, centre[b], components, first, second,
1420 pair_dm, storage->
cm[pair]));
1423 for (PetscInt f = 0; f < definition->
field_count; ++f) PetscCall(VecDestroy(¢re[f]));
1425 PetscFunctionReturn(0);
1435 Vec point_mean, Vec weight,
const PetscBool average_direction[3],
1438 PetscScalar ****mean = NULL, ****weighted = NULL;
1439 PetscReal ***w = NULL;
1442 PetscFunctionBeginUser;
1444 PetscCall(VecDuplicate(point_mean, &scratch));
1445 PetscCall(DMDAVecGetArrayRead(user->
da, weight, &w));
1446 PetscCall(DMDAVecGetArrayDOFRead(dm, point_mean, &mean));
1447 PetscCall(DMDAVecGetArrayDOF(dm, scratch, &weighted));
1448 for (PetscInt k = plan->
start[2]; k < plan->
end[2]; ++k)
1449 for (PetscInt j = plan->
start[1]; j < plan->
end[1]; ++j)
1450 for (PetscInt i = plan->
start[0]; i < plan->
end[0]; ++i) {
1451 for (PetscInt c = 0; c < dof; ++c) weighted[k][j][i][c] = w[k][j][i] * mean[k][j][i][c];
1453 PetscCall(DMDAVecRestoreArrayDOF(dm, scratch, &weighted));
1454 PetscCall(DMDAVecRestoreArrayDOFRead(dm, point_mean, &mean));
1455 PetscCall(DMDAVecRestoreArrayRead(user->
da, weight, &w));
1457 PETSC_COMM_WORLD, slot_mean, NULL));
1458 PetscCall(VecDestroy(&scratch));
1460 PetscFunctionReturn(0);
1469 const PetscBool average_direction[3],
1473 const PetscInt set_count = (storage && storage->
occupied_count) ? 2 : 1;
1475 PetscFunctionBeginUser;
1477 PetscCheck(user != NULL && definition != NULL && storage != NULL && reduced != NULL && average_direction != NULL,
1478 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"Context, definition, and both storages are required.");
1486 for (PetscInt f = 0; f < definition->
field_count; ++f) {
1488 Vec count = NULL, weight = NULL, weight_sq = NULL;
1493 average_direction, reduced->
mean[f]));
1504 const PetscBool self = (PetscBool)(index < storage->field_count);
1505 const PetscInt pair = index - storage->
field_count;
1507 PetscInt a = index, b = index, components = 0, first[6], second[6];
1509 PetscScalar ****point_a = NULL, ****point_b = NULL, ****slot_a = NULL, ****slot_b = NULL;
1510 PetscScalar ****moment = NULL, ****total = NULL;
1511 PetscReal ***weight = NULL;
1512 Vec source = NULL, target = NULL, scratch = NULL, count_vec = NULL, weight_vec = NULL, weight_sq_vec = NULL;
1513 DM product_dm = NULL;
1516 if (!storage->
m2[index])
continue;
1517 source = storage->
m2[index];
1518 target = reduced->
m2[index];
1522 source = storage->
cm[pair];
1523 target = reduced->
cm[pair];
1528 PetscCall(VecGetDM(source, &product_dm));
1529 PetscCall(DMDAGetInfo(product_dm, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &components,
1530 NULL, NULL, NULL, NULL, NULL));
1533 PetscCall(VecDuplicate(source, &scratch));
1534 PetscCall(DMDAVecGetArrayRead(user->
da, weight_vec, &weight));
1535 PetscCall(DMDAVecGetArrayDOFRead(view_a.
dm,
SampleSetMean(storage, set, a), &point_a));
1536 PetscCall(DMDAVecGetArrayDOFRead(view_b.
dm,
SampleSetMean(storage, set, b), &point_b));
1537 PetscCall(DMDAVecGetArrayDOFRead(view_a.
dm,
SampleSetMean(reduced, set, a), &slot_a));
1538 PetscCall(DMDAVecGetArrayDOFRead(view_b.
dm,
SampleSetMean(reduced, set, b), &slot_b));
1539 PetscCall(DMDAVecGetArrayDOFRead(product_dm, source, &moment));
1540 PetscCall(DMDAVecGetArrayDOF(product_dm, scratch, &total));
1541 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k)
1542 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j)
1543 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
1544 for (PetscInt c = 0; c < components; ++c) {
1545 total[k][j][i][c] = moment[k][j][i][c] + weight[k][j][i] *
1546 (point_a[k][j][i][first[c]] - slot_a[k][j][i][first[c]]) *
1547 (point_b[k][j][i][second[c]] - slot_b[k][j][i][second[c]]);
1550 PetscCall(DMDAVecRestoreArrayDOF(product_dm, scratch, &total));
1551 PetscCall(DMDAVecRestoreArrayDOFRead(product_dm, source, &moment));
1552 PetscCall(DMDAVecRestoreArrayDOFRead(view_b.
dm,
SampleSetMean(reduced, set, b), &slot_b));
1553 PetscCall(DMDAVecRestoreArrayDOFRead(view_a.
dm,
SampleSetMean(reduced, set, a), &slot_a));
1554 PetscCall(DMDAVecRestoreArrayDOFRead(view_b.
dm,
SampleSetMean(storage, set, b), &point_b));
1555 PetscCall(DMDAVecRestoreArrayDOFRead(view_a.
dm,
SampleSetMean(storage, set, a), &point_a));
1556 PetscCall(DMDAVecRestoreArrayRead(user->
da, weight_vec, &weight));
1558 average_direction, PETSC_COMM_WORLD, target, NULL));
1559 PetscCall(VecDestroy(&scratch));
1564 for (PetscInt set = 0; set < set_count; ++set) {
1565 Vec count = NULL, weight = NULL, weight_sq = NULL, merged_count = NULL, merged = NULL, merged_sq = NULL;
1566 PetscReal ***occupied = NULL;
1571 PETSC_COMM_WORLD, merged, NULL));
1572 PetscCall(DMDAVecGetArray(user->
da, merged, &occupied));
1573 for (PetscInt k = plan.
start[2]; k < plan.
end[2]; ++k)
1574 for (PetscInt j = plan.
start[1]; j < plan.
end[1]; ++j)
1575 for (PetscInt i = plan.
start[0]; i < plan.
end[0]; ++i) {
1576 occupied[k][j][i] = (occupied[k][j][i] > 0.0) ? 1.0 : 0.0;
1578 PetscCall(DMDAVecRestoreArray(user->
da, merged, &occupied));
1579 PetscCall(VecCopy(merged, merged_count));
1580 PetscCall(VecCopy(merged, merged_sq));
1583 PetscFunctionReturn(0);
Header file for Particle Motion and migration related functions.
PetscErrorCode CalculateParticleCountPerCell(UserCtx *user)
Counts particles in each cell of the DMDA 'da' and stores the result in user->ParticleCount.
Authoritative identities and storage metadata for persistent Eulerian fields.
@ FIELD_CAPABILITY_PARTICLE_SAMPLED
Defined only where a cell holds particles; elsewhere the stored value is a placeholder,...
unsigned int capabilities
const FieldDescriptor * descriptor
const char * FieldCanonicalName(FieldId field_id)
Return the canonical printable name for an ID.
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
@ FIELD_LAYOUT_CELL_CENTERED
const char * canonical_name
const char * FieldLayoutName(FieldLayout layout)
Return a stable printable label for a field layout.
PetscErrorCode FieldGetDescriptor(FieldId field_id, const FieldDescriptor **descriptor)
Return immutable metadata for a valid field identifier.
FieldId
Compile-time identity for a catalogued Eulerian field.
Immutable metadata for one field identity.
Non-owning runtime objects resolved for one field and UserCtx.
PetscErrorCode ScatterParticleFieldCellMean(UserCtx *user, ParticleFieldId particle_field_id, DM dataDM, Vec countVec, PetscBool recount, Vec meanVec)
Cell mean of a particle field: the field summed over each cell's particles, divided by the cell's cou...
Public interface for data input/output routines.
PetscErrorCode PicurvFieldReferenceScale(SimCtx *simCtx, const char *field_name, PetscReal *scale, char *description, size_t description_length)
Physical scale one field is multiplied by to leave non-dimensional form.
void TrimWhitespace(char *str)
Removes leading and trailing ASCII whitespace from a mutable string.
Logging utilities and macros for PETSc-based applications.
#define LOCAL
Logging scope definitions for controlling message output.
#define GLOBAL
Scope for global logging across all processes.
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
@ LOG_DEBUG
Detailed debugging information.
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Typed identities and metadata for persistent solver-particle fields.
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
ParticleFieldId
Compile-time identity for a persistent solver-particle field.
@ PARTICLE_FIELD_ID_CELL_ID
PetscErrorCode ParticleFieldIdFromScatterTarget(FieldId target, ParticleFieldId *field_id)
Find the particle field that scatters to an Eulerian field.
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
const char * canonical_name
Immutable metadata for one persistent particle field.
static PetscErrorCode ReduceSampleSetMean(UserCtx *user, const SpatialTargetPlan *plan, DM dm, PetscInt dof, Vec point_mean, Vec weight, const PetscBool average_direction[3], Vec slot_mean)
Internal helper: merges one point-mean field into its slot means over a sample set.
static const PetscInt kProductFirst[6]
Upper-triangular row-major component pairs for a three-vector self-product.
PetscErrorCode PicurvProductComponentCount(PetscInt dof, PetscInt *count)
Implementation of PicurvProductComponentCount().
static const char *const kDerivedKindName[DERIVED_KIND_COUNT]
Recipe spellings of the output kinds.
static const char *const kAxisName[3]
Axis labels indexing the pair table above.
#define STATISTICS_DERIVED_OUTPUT_LENGTH
Longest output list a recipe may request.
PetscErrorCode PicurvWindowStorageReduce(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const PetscBool average_direction[3], PicurvWindowStorage *reduced)
Implementation of PicurvWindowStorageReduce().
static PetscErrorCode ParseDerivedKinds(const char *outputs, PetscBool wanted[DERIVED_KIND_COUNT])
Internal helper: reports which output kinds a recipe requested.
static PetscInt ProductDiagonalIndex(PetscInt component)
Internal helper: the product index carrying one component's own variance.
PetscErrorCode PicurvWindowDerive(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt index, Vec scalar_target, Vec vector_target, PicurvDerivedField *field)
Implementation of PicurvWindowDerive().
SampleSet
The sample set a field or pair is measured over.
@ SAMPLE_SET_ALL
Every accepted state at the point.
@ SAMPLE_SET_OCCUPIED
The accepted states in which the cell held particles.
static void ProductComponents(PetscBool self, PetscInt dof_a, PetscInt dof_b, PetscInt components, PetscInt first[6], PetscInt second[6])
Internal helper: the component pairs one stored product carries.
static PetscErrorCode FindFieldSlot(const PicurvWindowDefinition *definition, PetscInt field_id, PetscInt *slot)
Internal helper: locates the storage slot holding one field's running mean.
static PetscErrorCode ResolveDerivedIndex(const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const PetscBool wanted[DERIVED_KIND_COUNT], PetscInt index, DerivedKind *kind, PetscInt *offset)
Internal helper: resolves which kind and member one derived index selects.
DerivedKind
Output kinds a postprocessing recipe may request, in enumeration order.
@ DERIVED_REYNOLDS_STRESS
PetscErrorCode PicurvWindowDerivedCount(const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, const char *outputs, PetscInt *count)
Implementation of PicurvWindowDerivedCount().
static PetscErrorCode ProductComponentLabel(PetscInt index, char *out, size_t size)
Internal helper: writes the two-axis label of one product component.
PetscErrorCode PicurvWindowStorageCreate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage)
Implementation of PicurvWindowStorageCreate().
PetscErrorCode PicurvCovarianceComponentCount(PetscInt dof_a, PetscInt dof_b, PetscInt *count)
Implementation of PicurvCovarianceComponentCount().
PetscErrorCode PicurvWindowStorageFromParticles(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage)
Implementation of PicurvWindowStorageFromParticles().
PetscErrorCode PicurvWindowStoragePayload(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, PetscInt index, PicurvStatisticsPayload *payload)
Implementation of PicurvWindowStoragePayload().
static const PetscInt kProductSecond[6]
PetscErrorCode PicurvWindowValidFractionRange(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, PetscInt sample_count, PetscReal *minimum, PetscReal *maximum)
Implementation of PicurvWindowValidFractionRange().
static const PetscInt kDerivedKindScaleExponent[DERIVED_KIND_COUNT]
Power of the source field's reference scale each derived kind carries.
static PetscErrorCode SafeStandardDeviation(PetscReal variance, const char *label, PetscReal *result)
Internal helper: takes a square root of a variance that may be barely negative.
PetscErrorCode PicurvStatisticsComponentDM(UserCtx *user, PetscInt components, DM *dm)
Implementation of PicurvStatisticsComponentDM().
PetscErrorCode PicurvWindowSpatialMean(UserCtx *user, const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, Vec field, PetscReal *mean)
Implementation of PicurvWindowSpatialMean().
static PetscErrorCode DerivedKindExtent(const PicurvWindowDefinition *definition, const PicurvWindowStorage *storage, DerivedKind kind, PetscInt *count)
Internal helper: counts the derived fields each kind contributes.
PetscErrorCode PicurvWindowAccumulate(UserCtx *user, const PicurvWindowDefinition *definition, PicurvWindowStorage *storage, PetscReal weight)
Implementation of PicurvWindowAccumulate().
static PetscErrorCode AccumulateParticleCentredProducts(UserCtx *user, ParticleFieldId field_a, ParticleFieldId field_b, DM dm_a, Vec mean_a, DM dm_b, Vec mean_b, PetscInt components, const PetscInt first[6], const PetscInt second[6], DM product_dm, Vec product)
Internal helper: adds every particle's centred product into its cell.
PetscErrorCode PicurvWindowStorageDestroy(PicurvWindowStorage *storage)
Implementation of PicurvWindowStorageDestroy().
PetscErrorCode PicurvWindowStoragePayloadCount(const PicurvWindowStorage *storage, PetscInt *count)
Implementation of PicurvWindowStoragePayloadCount().
static PetscBool FieldIsParticleSampled(PetscInt field_id)
Internal helper: whether a catalogued field is measured only where particles are.
static Vec SampleSetMean(const PicurvWindowStorage *storage, SampleSet set, PetscInt slot)
Internal helper: the mean a field slot is centred on within one sample set.
static void SampleSetOccupancy(const PicurvWindowStorage *storage, SampleSet set, Vec *count, Vec *weight, Vec *weight_sq)
Internal helper: the occupancy vectors of one sample set.
static SampleSet RequestSampleSet(const PicurvWindowDefinition *definition, PetscInt slot, PetscInt pair)
Internal helper: the sample set one field slot, or one covariance pair, uses.
Per-window PETSc accumulator storage and pointwise application.
Vec weight
Per-point valid weight.
PetscInt components
Degrees of freedom the vector carries.
PetscInt components
One or three.
Vec occupied_weight_sq
Occupied-state squared weight; NULL likewise.
Vec weight_sq
Per-point squared-weight sum.
PetscInt field_count
Fields accumulated.
PetscInt covariance_count
Covariance pairs accumulated.
char name[96]
Output field name, window qualified.
#define PICURV_STATISTICS_VARIANCE_FLOOR
Tolerance within which a negative variance is treated as floating-point noise.
Vec occupied_count
Occupied-state count; NULL without a particle-sampled field.
Vec * mean
One per field, matching that field's layout.
PetscBool point_averaged
Reduced over homogeneous directions as the equal-weight mean of point values, not from the pooled sam...
Vec occupied_weight
Occupied-state weight; NULL without a particle-sampled field.
Vec vec
Borrowed accumulator vector; never owned by the caller.
Vec * m2
One per field; NULL when no second moment was requested.
const char * role
Inventory role: occupancy, mean, second_moment, co_moment.
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...
Vec count
Per-point accepted sample count.
char name[96]
File basename, no extension.
const char * layout
Catalog layout name for the inventory entry.
Vec * cm
One per covariance pair.
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.
One checkpointable accumulator vector, resolved by enumeration index.
Independent accumulator state for one window on one block.
Weighted centered-moment kernels for the field-statistics pipeline.
PetscReal weight_sq
Sum of squared weights W2.
PetscReal weight_sq
Sum of squared weights W2.
PetscErrorCode PicurvCoMomentStateUpdate(PicurvCoMomentState *state, PetscReal value_x, PetscReal value_y, PetscReal weight)
Applies one weighted paired sample to a co-moment accumulator.
PetscReal mean_y
Weighted mean of the second member.
PetscReal weight
Total weight W.
PetscReal count
Number of accepted samples.
PetscReal cm
Centered co-moment sum C.
PetscReal m2
Centered second-moment sum M2.
PetscReal PicurvCoMomentStateCovariance(const PicurvCoMomentState *state)
Returns the weighted covariance C/W, or zero when no weight accumulated.
PetscReal mean
Weighted mean mu.
PetscReal mean_x
Weighted mean of the first member.
PetscErrorCode PicurvMomentStateUpdate(PicurvMomentState *state, PetscReal value, PetscReal weight)
Applies one weighted sample to a scalar moment accumulator.
PetscReal weight
Total weight W.
PetscReal count
Number of accepted samples.
Weighted centered co-moment state for one ordered pair of quantities.
Weighted centered state for one scalar quantity at one point.
Spatial target resolution for the field-statistics pipeline.
PetscBool SpatialTargetPlanMaskAllows(const SpatialTargetPlan *plan, PetscReal nvert_value)
Reports whether a point passes the plan's mask.
@ 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.
PetscErrorCode PicurvSpatialRatioAverage(UserCtx *user, const SpatialTargetPlan *plan, Vec numerator, Vec denominator, Vec inclusion, const PetscBool average_direction[3], MPI_Comm comm, Vec ratio, PetscReal *reduced)
Averages two fields over a target domain and divides the results.
Resolved iteration domain for one field on one block.
PetscInt first
First member; must also appear in the field list.
PicurvWindowFieldRequest fields[16]
PetscBool want_second
Also keep the centered second moment.
PetscInt second
Second member; must also appear in the field list.
PicurvWindowCovarianceRequest covariances[16]
PetscInt covariance_count
PetscInt field_id
Catalogued Eulerian field identity.
#define PICURV_WINDOW_MAX_REQUESTS
Maximum fields or covariance pairs one window may request.
The scientifically immutable definition of one window.
SimCtx * simCtx
Back-pointer to the master simulation context.
PetscBool dimensionalize
Whether derived output leaves non-dimensional form, from global_operations.dimensionalize.
A symmetric second-order tensor stored by its six independent components.
User-defined context containing data specific to a single computational grid level.