PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Data Structures | Functions
statistics_moments.h File Reference

Weighted centered-moment kernels for the field-statistics pipeline. More...

#include <petscsys.h>
Include dependency graph for statistics_moments.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PicurvMomentState
 Weighted centered state for one scalar quantity at one point. More...
 
struct  PicurvCoMomentState
 Weighted centered co-moment state for one ordered pair of quantities. More...
 

Functions

void PicurvMomentStateReset (PicurvMomentState *state)
 Resets a scalar moment accumulator to the empty state.
 
void PicurvCoMomentStateReset (PicurvCoMomentState *state)
 Resets a co-moment accumulator to the empty state.
 
PetscErrorCode PicurvMomentStateUpdate (PicurvMomentState *state, PetscReal value, PetscReal weight)
 Applies one weighted sample to a scalar moment accumulator.
 
PetscErrorCode PicurvCoMomentStateUpdate (PicurvCoMomentState *state, PetscReal value_x, PetscReal value_y, PetscReal weight)
 Applies one weighted paired sample to a co-moment accumulator.
 
PetscErrorCode PicurvMomentStateMerge (PicurvMomentState *result, const PicurvMomentState *a, const PicurvMomentState *b)
 Merges two independently accumulated scalar states.
 
PetscErrorCode PicurvCoMomentStateMerge (PicurvCoMomentState *result, const PicurvCoMomentState *a, const PicurvCoMomentState *b)
 Merges two independently accumulated co-moment states.
 
PetscReal PicurvMomentStateVariance (const PicurvMomentState *state)
 Returns the weighted variance M2/W, or zero when no weight accumulated.
 
PetscReal PicurvCoMomentStateCovariance (const PicurvCoMomentState *state)
 Returns the weighted covariance C/W, or zero when no weight accumulated.
 
PetscReal PicurvMomentStateEffectiveCount (const PicurvMomentState *state)
 Returns Kish effective sample size W^2/W2, or zero when no weight accumulated.
 

Detailed Description

Weighted centered-moment kernels for the field-statistics pipeline.

These are pure numerical kernels: no configuration, no PETSc vectors, and no knowledge of windows, fields, or scheduling. They implement the accumulator contract described in 4.1 Per-Point State and Update.

Centered state is used rather than raw sums because it retains everything the first and second moments need while staying numerically stable for high-mean/low-fluctuation signals. Raw sums are recoverable as S_x = W*mu and Q_xx = M2 + W*mu^2.

Definition in file statistics_moments.h.


Data Structure Documentation

◆ PicurvMomentState

struct PicurvMomentState

Weighted centered state for one scalar quantity at one point.

weight_sq is retained because physical-time weighting produces unequal weights, so an effective sample size cannot be inferred from count alone.

Definition at line 26 of file statistics_moments.h.

Data Fields
PetscReal count Number of accepted samples.
PetscReal weight Total weight W.
PetscReal weight_sq Sum of squared weights W2.
PetscReal mean Weighted mean mu.
PetscReal m2 Centered second-moment sum M2.

◆ PicurvCoMomentState

struct PicurvCoMomentState

Weighted centered co-moment state for one ordered pair of quantities.

The two means are tracked inside the pair rather than referenced from two PicurvMomentState values, so a co-moment update is self-contained and cannot be corrupted by update ordering between the members.

Definition at line 41 of file statistics_moments.h.

Data Fields
PetscReal count Number of accepted samples.
PetscReal weight Total weight W.
PetscReal weight_sq Sum of squared weights W2.
PetscReal mean_x Weighted mean of the first member.
PetscReal mean_y Weighted mean of the second member.
PetscReal cm Centered co-moment sum C.

Function Documentation

◆ PicurvMomentStateReset()

void PicurvMomentStateReset ( PicurvMomentState *  state)

Resets a scalar moment accumulator to the empty state.

Parameters
[out]stateAccumulator to clear; ignored when NULL.

Resets a scalar moment accumulator to the empty state.

See also
PicurvMomentStateReset()

Definition at line 15 of file statistics_moments.c.

16{
17 if (state == NULL) return;
18 state->count = 0.0;
19 state->weight = 0.0;
20 state->weight_sq = 0.0;
21 state->mean = 0.0;
22 state->m2 = 0.0;
23}
PetscReal weight_sq
Sum of squared weights W2.
PetscReal weight
Total weight W.
PetscReal m2
Centered second-moment sum M2.
PetscReal mean
Weighted mean mu.
PetscReal count
Number of accepted samples.
Here is the caller graph for this function:

◆ PicurvCoMomentStateReset()

void PicurvCoMomentStateReset ( PicurvCoMomentState *  state)

Resets a co-moment accumulator to the empty state.

Parameters
[out]stateAccumulator to clear; ignored when NULL.

Resets a co-moment accumulator to the empty state.

See also
PicurvCoMomentStateReset()

Definition at line 29 of file statistics_moments.c.

30{
31 if (state == NULL) return;
32 state->count = 0.0;
33 state->weight = 0.0;
34 state->weight_sq = 0.0;
35 state->mean_x = 0.0;
36 state->mean_y = 0.0;
37 state->cm = 0.0;
38}
PetscReal weight_sq
Sum of squared weights W2.
PetscReal mean_y
Weighted mean of the second member.
PetscReal count
Number of accepted samples.
PetscReal cm
Centered co-moment sum C.
PetscReal mean_x
Weighted mean of the first member.
PetscReal weight
Total weight W.
Here is the caller graph for this function:

◆ PicurvMomentStateUpdate()

PetscErrorCode PicurvMomentStateUpdate ( PicurvMomentState *  state,
PetscReal  value,
PetscReal  weight 
)

Applies one weighted sample to a scalar moment accumulator.

Implements the stable weighted update ‘W’ = W+w,d = x-mu,mu' = mu + (w/W')d,M2' = M2 + w*d*(x-mu')`.

Parameters
[in,out]stateAccumulator to update.
[in]valueSample value x.
[in]weightSample weight w; must be strictly positive.
Returns
Zero on success, or PETSC_ERR_ARG_OUTOFRANGE for a non-positive weight.

Applies one weighted sample to a scalar moment accumulator.

See also
PicurvMomentStateUpdate()

Definition at line 44 of file statistics_moments.c.

45{
46 PetscReal new_weight = 0.0;
47 PetscReal delta = 0.0;
48
49 PetscFunctionBeginUser;
50 PetscCheck(state != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
51 "Moment accumulator is required.");
52 PetscCheck(weight > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
53 "Sample weight must be strictly positive, got %g.", (double)weight);
54
55 new_weight = state->weight + weight;
56 delta = value - state->mean;
57 state->mean += (weight / new_weight) * delta;
58 /* Uses the post-update mean deliberately: this is the pairing that keeps the
59 * centered sum stable when the mean is large relative to the fluctuation. */
60 state->m2 += weight * delta * (value - state->mean);
61 state->weight = new_weight;
62 state->weight_sq += weight * weight;
63 state->count += 1.0;
64 PetscFunctionReturn(0);
65}
Here is the caller graph for this function:

◆ PicurvCoMomentStateUpdate()

PetscErrorCode PicurvCoMomentStateUpdate ( PicurvCoMomentState *  state,
PetscReal  value_x,
PetscReal  value_y,
PetscReal  weight 
)

Applies one weighted paired sample to a co-moment accumulator.

Uses the pre-update mean of x and the post-update mean of y, which is the form that reduces exactly to the M2 update when x and y are the same signal.

Parameters
[in,out]stateAccumulator to update.
[in]value_xSample value of the first member.
[in]value_ySample value of the second member.
[in]weightSample weight w; must be strictly positive.
Returns
Zero on success, or PETSC_ERR_ARG_OUTOFRANGE for a non-positive weight.

Applies one weighted paired sample to a co-moment accumulator.

See also
PicurvCoMomentStateUpdate()

Definition at line 71 of file statistics_moments.c.

73{
74 PetscReal new_weight = 0.0;
75 PetscReal delta_x = 0.0;
76
77 PetscFunctionBeginUser;
78 PetscCheck(state != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
79 "Co-moment accumulator is required.");
80 PetscCheck(weight > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE,
81 "Sample weight must be strictly positive, got %g.", (double)weight);
82
83 new_weight = state->weight + weight;
84 delta_x = value_x - state->mean_x;
85 state->mean_x += (weight / new_weight) * delta_x;
86 state->mean_y += (weight / new_weight) * (value_y - state->mean_y);
87 /* Pre-update mean of x against post-update mean of y. With x == y this
88 * reduces exactly to the M2 update, which the unit tests assert. */
89 state->cm += weight * delta_x * (value_y - state->mean_y);
90 state->weight = new_weight;
91 state->weight_sq += weight * weight;
92 state->count += 1.0;
93 PetscFunctionReturn(0);
94}
Here is the caller graph for this function:

◆ PicurvMomentStateMerge()

PetscErrorCode PicurvMomentStateMerge ( PicurvMomentState *  result,
const PicurvMomentState *  a,
const PicurvMomentState *  b 
)

Merges two independently accumulated scalar states.

Implements the stable weighted parallel combination d = mu_b - mu_a, W = W_a + W_b, mu = mu_a + (W_b/W)d, M2 = M2_a + M2_b + (W_a*W_b/W)d^2. Merging an empty state is a no-op, so partitions that received no samples are safe to combine.

Parameters
[out]resultMerged state; may alias a or b.
[in]aFirst partition.
[in]bSecond partition.
Returns
Zero on success, or PETSC_ERR_ARG_NULL when any argument is NULL.

Merges two independently accumulated scalar states.

See also
PicurvMomentStateMerge()

Definition at line 100 of file statistics_moments.c.

102{
103 PetscReal total_weight = 0.0;
104 PetscReal delta = 0.0;
105 PicurvMomentState merged;
106
107 PetscFunctionBeginUser;
108 PetscCheck(result != NULL && a != NULL && b != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
109 "Merge requires a destination and two source accumulators.");
110
111 if (b->weight == 0.0) { *result = *a; PetscFunctionReturn(0); }
112 if (a->weight == 0.0) { *result = *b; PetscFunctionReturn(0); }
113
114 total_weight = a->weight + b->weight;
115 delta = b->mean - a->mean;
116 merged.count = a->count + b->count;
117 merged.weight = total_weight;
118 merged.weight_sq = a->weight_sq + b->weight_sq;
119 merged.mean = a->mean + (b->weight / total_weight) * delta;
120 merged.m2 = a->m2 + b->m2 + (a->weight * b->weight / total_weight) * delta * delta;
121 *result = merged;
122 PetscFunctionReturn(0);
123}
Weighted centered state for one scalar quantity at one point.
Here is the caller graph for this function:

◆ PicurvCoMomentStateMerge()

PetscErrorCode PicurvCoMomentStateMerge ( PicurvCoMomentState *  result,
const PicurvCoMomentState *  a,
const PicurvCoMomentState *  b 
)

Merges two independently accumulated co-moment states.

Parameters
[out]resultMerged state; may alias a or b.
[in]aFirst partition.
[in]bSecond partition.
Returns
Zero on success, or PETSC_ERR_ARG_NULL when any argument is NULL.

Merges two independently accumulated co-moment states.

See also
PicurvCoMomentStateMerge()

Definition at line 129 of file statistics_moments.c.

131{
132 PetscReal total_weight = 0.0;
133 PetscReal delta_x = 0.0;
134 PetscReal delta_y = 0.0;
135 PicurvCoMomentState merged;
136
137 PetscFunctionBeginUser;
138 PetscCheck(result != NULL && a != NULL && b != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
139 "Merge requires a destination and two source accumulators.");
140
141 if (b->weight == 0.0) { *result = *a; PetscFunctionReturn(0); }
142 if (a->weight == 0.0) { *result = *b; PetscFunctionReturn(0); }
143
144 total_weight = a->weight + b->weight;
145 delta_x = b->mean_x - a->mean_x;
146 delta_y = b->mean_y - a->mean_y;
147 merged.count = a->count + b->count;
148 merged.weight = total_weight;
149 merged.weight_sq = a->weight_sq + b->weight_sq;
150 merged.mean_x = a->mean_x + (b->weight / total_weight) * delta_x;
151 merged.mean_y = a->mean_y + (b->weight / total_weight) * delta_y;
152 merged.cm = a->cm + b->cm + (a->weight * b->weight / total_weight) * delta_x * delta_y;
153 *result = merged;
154 PetscFunctionReturn(0);
155}
Weighted centered co-moment state for one ordered pair of quantities.
Here is the caller graph for this function:

◆ PicurvMomentStateVariance()

PetscReal PicurvMomentStateVariance ( const PicurvMomentState *  state)

Returns the weighted variance M2/W, or zero when no weight accumulated.

This is the population (weight-normalized) variance the postprocessing contract uses for R_ii. It is never negative for a state built only through PicurvMomentStateUpdate, but callers taking a square root must still clamp, because cancellation can drive M2 slightly negative for degenerate inputs.

Parameters
[in]stateAccumulator to read.
Returns
Weighted variance, or zero for a NULL or unsampled accumulator.

Returns the weighted variance M2/W, or zero when no weight accumulated.

See also
PicurvMomentStateVariance()

Definition at line 161 of file statistics_moments.c.

162{
163 if (state == NULL || state->weight == 0.0) return 0.0;
164 return state->m2 / state->weight;
165}
Here is the caller graph for this function:

◆ PicurvCoMomentStateCovariance()

PetscReal PicurvCoMomentStateCovariance ( const PicurvCoMomentState *  state)

Returns the weighted covariance C/W, or zero when no weight accumulated.

Parameters
[in]stateAccumulator to read.
Returns
Weighted covariance, or zero for a NULL or unsampled accumulator.

Returns the weighted covariance C/W, or zero when no weight accumulated.

See also
PicurvCoMomentStateCovariance()

Definition at line 171 of file statistics_moments.c.

172{
173 if (state == NULL || state->weight == 0.0) return 0.0;
174 return state->cm / state->weight;
175}
Here is the caller graph for this function:

◆ PicurvMomentStateEffectiveCount()

PetscReal PicurvMomentStateEffectiveCount ( const PicurvMomentState *  state)

Returns Kish effective sample size W^2/W2, or zero when no weight accumulated.

Equals the sample count exactly under equal weights, and degrades toward one as the weight distribution becomes dominated by a single sample.

Parameters
[in]stateAccumulator to read.
Returns
Effective sample size, or zero for a NULL or unsampled accumulator.

Returns Kish effective sample size W^2/W2, or zero when no weight accumulated.

See also
PicurvMomentStateEffectiveCount()

Definition at line 181 of file statistics_moments.c.

182{
183 if (state == NULL || state->weight_sq == 0.0) return 0.0;
184 return (state->weight * state->weight) / state->weight_sq;
185}
Here is the caller graph for this function: