PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
statistics_moments.h
Go to the documentation of this file.
1/**
2 * @file statistics_moments.h
3 * @brief Weighted centered-moment kernels for the field-statistics pipeline.
4 *
5 * These are pure numerical kernels: no configuration, no PETSc vectors, and no
6 * knowledge of windows, fields, or scheduling. They implement the accumulator
7 * contract fixed in
8 * @ref 60_Field_Statistics_Phase2_Implementation_Specification section 5, which
9 * in turn follows @ref 58_Turbulence_Statistics_Pipeline_Specification section 7.
10 *
11 * Centered state is used rather than raw sums because it retains everything the
12 * first and second moments need while staying numerically stable for
13 * high-mean/low-fluctuation signals. Raw sums are recoverable as
14 * `S_x = W*mu` and `Q_xx = M2 + W*mu^2`.
15 */
16
17#ifndef PICURV_STATISTICS_MOMENTS_H
18#define PICURV_STATISTICS_MOMENTS_H
19
20#include <petscsys.h>
21
22/**
23 * @brief Weighted centered state for one scalar quantity at one point.
24 *
25 * `weight_sq` is retained because physical-time weighting produces unequal
26 * weights, so an effective sample size cannot be inferred from `count` alone.
27 */
28typedef struct {
29 PetscReal count; /**< Number of accepted samples. */
30 PetscReal weight; /**< Total weight W. */
31 PetscReal weight_sq; /**< Sum of squared weights W2. */
32 PetscReal mean; /**< Weighted mean mu. */
33 PetscReal m2; /**< Centered second-moment sum M2. */
35
36/**
37 * @brief Weighted centered co-moment state for one ordered pair of quantities.
38 *
39 * The two means are tracked inside the pair rather than referenced from two
40 * `PicurvMomentState` values, so a co-moment update is self-contained and cannot
41 * be corrupted by update ordering between the members.
42 */
43typedef struct {
44 PetscReal count; /**< Number of accepted samples. */
45 PetscReal weight; /**< Total weight W. */
46 PetscReal weight_sq; /**< Sum of squared weights W2. */
47 PetscReal mean_x; /**< Weighted mean of the first member. */
48 PetscReal mean_y; /**< Weighted mean of the second member. */
49 PetscReal cm; /**< Centered co-moment sum C. */
51
52/**
53 * @brief Resets a scalar moment accumulator to the empty state.
54 * @param[out] state Accumulator to clear; ignored when NULL.
55 */
57
58/**
59 * @brief Resets a co-moment accumulator to the empty state.
60 * @param[out] state Accumulator to clear; ignored when NULL.
61 */
63
64/**
65 * @brief Applies one weighted sample to a scalar moment accumulator.
66 *
67 * Implements the stable weighted update
68 * `W' = W+w`, `d = x-mu`, `mu' = mu + (w/W')d`, `M2' = M2 + w*d*(x-mu')`.
69 *
70 * @param[in,out] state Accumulator to update.
71 * @param[in] value Sample value x.
72 * @param[in] weight Sample weight w; must be strictly positive.
73 * @return Zero on success, or `PETSC_ERR_ARG_OUTOFRANGE` for a non-positive weight.
74 */
75PetscErrorCode PicurvMomentStateUpdate(PicurvMomentState *state, PetscReal value, PetscReal weight);
76
77/**
78 * @brief Applies one weighted paired sample to a co-moment accumulator.
79 *
80 * Uses the pre-update mean of x and the post-update mean of y, which is the form
81 * that reduces exactly to the `M2` update when x and y are the same signal.
82 *
83 * @param[in,out] state Accumulator to update.
84 * @param[in] value_x Sample value of the first member.
85 * @param[in] value_y Sample value of the second member.
86 * @param[in] weight Sample weight w; must be strictly positive.
87 * @return Zero on success, or `PETSC_ERR_ARG_OUTOFRANGE` for a non-positive weight.
88 */
90 PetscReal value_x, PetscReal value_y, PetscReal weight);
91
92/**
93 * @brief Merges two independently accumulated scalar states.
94 *
95 * Implements the stable weighted parallel combination
96 * `d = mu_b - mu_a`, `W = W_a + W_b`, `mu = mu_a + (W_b/W)d`,
97 * `M2 = M2_a + M2_b + (W_a*W_b/W)d^2`.
98 * Merging an empty state is a no-op, so partitions that received no samples are
99 * safe to combine.
100 *
101 * @param[out] result Merged state; may alias @p a or @p b.
102 * @param[in] a First partition.
103 * @param[in] b Second partition.
104 * @return Zero on success, or `PETSC_ERR_ARG_NULL` when any argument is NULL.
105 */
106PetscErrorCode PicurvMomentStateMerge(PicurvMomentState *result,
107 const PicurvMomentState *a, const PicurvMomentState *b);
108
109/**
110 * @brief Merges two independently accumulated co-moment states.
111 * @param[out] result Merged state; may alias @p a or @p b.
112 * @param[in] a First partition.
113 * @param[in] b Second partition.
114 * @return Zero on success, or `PETSC_ERR_ARG_NULL` when any argument is NULL.
115 */
116PetscErrorCode PicurvCoMomentStateMerge(PicurvCoMomentState *result,
117 const PicurvCoMomentState *a, const PicurvCoMomentState *b);
118
119/**
120 * @brief Returns the weighted variance `M2/W`, or zero when no weight accumulated.
121 *
122 * This is the population (weight-normalized) variance the postprocessing contract
123 * uses for `R_ii`. It is never negative for a state built only through
124 * `PicurvMomentStateUpdate`, but callers taking a square root must still clamp,
125 * because cancellation can drive `M2` slightly negative for degenerate inputs.
126 *
127 * @param[in] state Accumulator to read.
128 * @return Weighted variance, or zero for a NULL or unsampled accumulator.
129 */
130PetscReal PicurvMomentStateVariance(const PicurvMomentState *state);
131
132/**
133 * @brief Returns the weighted covariance `C/W`, or zero when no weight accumulated.
134 * @param[in] state Accumulator to read.
135 * @return Weighted covariance, or zero for a NULL or unsampled accumulator.
136 */
138
139/**
140 * @brief Returns Kish effective sample size `W^2/W2`, or zero when no weight accumulated.
141 *
142 * Equals the sample count exactly under equal weights, and degrades toward one as
143 * the weight distribution becomes dominated by a single sample.
144 *
145 * @param[in] state Accumulator to read.
146 * @return Effective sample size, or zero for a NULL or unsampled accumulator.
147 */
149
150#endif /* PICURV_STATISTICS_MOMENTS_H */
PetscErrorCode PicurvMomentStateMerge(PicurvMomentState *result, const PicurvMomentState *a, const PicurvMomentState *b)
Merges two independently accumulated scalar states.
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.
PetscErrorCode PicurvCoMomentStateMerge(PicurvCoMomentState *result, const PicurvCoMomentState *a, const PicurvCoMomentState *b)
Merges two independently accumulated co-moment states.
PetscReal PicurvMomentStateEffectiveCount(const PicurvMomentState *state)
Returns Kish effective sample size W^2/W2, or zero when no weight accumulated.
void PicurvCoMomentStateReset(PicurvCoMomentState *state)
Resets a co-moment accumulator to the empty state.
PetscReal PicurvMomentStateVariance(const PicurvMomentState *state)
Returns the weighted variance M2/W, or zero when no weight accumulated.
PetscReal m2
Centered second-moment sum M2.
PetscReal PicurvCoMomentStateCovariance(const PicurvCoMomentState *state)
Returns the weighted covariance C/W, or zero when no weight accumulated.
void PicurvMomentStateReset(PicurvMomentState *state)
Resets a scalar moment accumulator to the empty state.
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.