17#define STATISTICS_OPTION_NAME_LENGTH 128
20#define STATISTICS_OPTION_VALUE_LENGTH 256
28 PetscBool particle_source, PetscInt *field_id)
32 PetscFunctionBeginUser;
34 PetscCheck(field_name != NULL && field_name[0] !=
'\0', PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
35 "Statistics window '%s' names an empty field.", window_name);
36 if (particle_source) {
45 PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
46 "Statistics ensemble '%s' names particle field '%s', which has no Eulerian "
47 "scatter target to accumulate onto.", window_name, field_name);
52 *field_id = (PetscInt)resolved;
54 PetscFunctionReturn(0);
67 PetscFunctionBeginUser;
70 while (cursor && *cursor) {
71 char *comma = strchr(cursor,
',');
73 if (comma) *comma =
'\0';
75 if (cursor[0] !=
'\0') {
76 PetscCheck(*count < capacity, PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
77 "A statistics list holds at most %" PetscInt_FMT
" entries.", capacity);
78 tokens[(*count)++] = cursor;
80 cursor = comma ? comma + 1 : NULL;
83 PetscFunctionReturn(0);
94 PetscBool found = PETSC_FALSE;
96 PetscFunctionBeginUser;
98 PetscCall(PetscSNPrintf(name,
sizeof(name),
99 "-field_statistics_%s_%" PetscInt_FMT
"_field_count", kind, window));
100 PetscCall(PetscOptionsGetInt(NULL, NULL, name, &definition->
field_count, &found));
101 PetscCheck(found && definition->
field_count > 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
102 "Statistics window '%s' requests no field.", definition->
name);
104 PETSC_ERR_ARG_OUTOFRANGE,
105 "Statistics window '%s' requests %" PetscInt_FMT
" fields; at most %d are supported.",
108 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
112 PetscInt token_count = 0;
114 PetscCall(PetscSNPrintf(name,
sizeof(name),
115 "-field_statistics_%s_%" PetscInt_FMT
"_field_%" PetscInt_FMT
"_name",
116 kind, window, field_index));
117 PetscCall(PetscOptionsGetString(NULL, NULL, name, field_name,
sizeof(field_name), &found));
118 PetscCheck(found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
119 "Statistics window '%s' field %" PetscInt_FMT
" has no name.",
120 definition->
name, field_index);
124 PetscCall(PetscSNPrintf(name,
sizeof(name),
125 "-field_statistics_%s_%" PetscInt_FMT
"_field_%" PetscInt_FMT
"_moments",
126 kind, window, field_index));
127 PetscCall(PetscOptionsGetString(NULL, NULL, name, moments,
sizeof(moments), &found));
128 PetscCheck(found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
129 "Statistics window '%s' field '%s' requests no moment.", definition->
name, field_name);
131 PetscCheck(token_count > 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
132 "Statistics window '%s' field '%s' requests no moment.", definition->
name, field_name);
135 for (PetscInt t = 0; t < token_count; ++t) {
136 if (!strcmp(tokens[t],
"first"))
continue;
137 if (!strcmp(tokens[t],
"second")) {
141 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
142 "Statistics window '%s' field '%s' requests unknown moment '%s'; "
143 "the accumulable moments are 'first' and 'second'.",
144 definition->
name, field_name, tokens[t]);
148 PetscFunctionReturn(0);
159 PetscBool found = PETSC_FALSE;
161 PetscFunctionBeginUser;
163 PetscCall(PetscSNPrintf(name,
sizeof(name),
164 "-field_statistics_%s_%" PetscInt_FMT
"_covariance_count", kind, window));
165 PetscCall(PetscOptionsGetInt(NULL, NULL, name, &definition->
covariance_count, &found));
169 PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
170 "Statistics window '%s' requests %" PetscInt_FMT
" covariances; at most %d are supported.",
173 for (PetscInt pair_index = 0; pair_index < definition->
covariance_count; ++pair_index) {
176 PetscInt token_count = 0;
177 PetscBool first_present = PETSC_FALSE;
178 PetscBool second_present = PETSC_FALSE;
180 PetscCall(PetscSNPrintf(name,
sizeof(name),
181 "-field_statistics_%s_%" PetscInt_FMT
"_covariance_%" PetscInt_FMT,
182 kind, window, pair_index));
183 PetscCall(PetscOptionsGetString(NULL, NULL, name, pair,
sizeof(pair), &found));
184 PetscCheck(found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
185 "Statistics window '%s' covariance %" PetscInt_FMT
" has no members.",
186 definition->
name, pair_index);
188 PetscCheck(token_count == 2, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
189 "Statistics window '%s' covariance %" PetscInt_FMT
" needs exactly two members.",
190 definition->
name, pair_index);
194 PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
195 "Statistics window '%s' pairs '%s' with itself; that is the self-product already "
196 "requested through moments: [second].", definition->
name, tokens[0]);
200 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
204 PetscCheck(first_present && second_present, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
205 "Statistics window '%s' pairs '%s' with '%s', but both must also appear in its "
206 "field list so their means exist to center against.",
207 definition->
name, tokens[0], tokens[1]);
210 PetscFunctionReturn(0);
221 PetscBool found = PETSC_FALSE;
222 PetscBool step_found = PETSC_FALSE;
223 PetscBool time_found = PETSC_FALSE;
225 PetscFunctionBeginUser;
227 PetscCall(PetscSNPrintf(name,
sizeof(name),
228 "-field_statistics_window_%" PetscInt_FMT
"_start_time", window));
229 PetscCall(PetscOptionsGetReal(NULL, NULL, name, &definition->
start_time, &found));
230 PetscCheck(found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
231 "Statistics window '%s' has no start time.", definition->
name);
235 PetscCall(PetscSNPrintf(name,
sizeof(name),
236 "-field_statistics_window_%" PetscInt_FMT
"_end_time", window));
237 PetscCall(PetscOptionsGetReal(NULL, NULL, name, &definition->
end_time, &definition->
bounded));
239 PetscCall(PetscSNPrintf(name,
sizeof(name),
240 "-field_statistics_window_%" PetscInt_FMT
"_weighting", window));
241 PetscCall(PetscOptionsGetString(NULL, NULL, name, weighting,
sizeof(weighting), &found));
242 PetscCheck(found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
243 "Statistics window '%s' has no weighting.", definition->
name);
244 if (!strcmp(weighting,
"sample")) {
246 }
else if (!strcmp(weighting,
"physical_time")) {
249 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
250 "Statistics window '%s' has weighting '%s'; only 'sample' and 'physical_time' exist.",
251 definition->
name, weighting);
254 PetscCall(PetscSNPrintf(name,
sizeof(name),
255 "-field_statistics_window_%" PetscInt_FMT
"_step_cadence", window));
256 PetscCall(PetscOptionsGetInt(NULL, NULL, name, &definition->
step_cadence, &step_found));
257 PetscCall(PetscSNPrintf(name,
sizeof(name),
258 "-field_statistics_window_%" PetscInt_FMT
"_time_cadence", window));
259 PetscCall(PetscOptionsGetReal(NULL, NULL, name, &definition->
time_cadence, &time_found));
260 PetscCheck(step_found != time_found, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
261 "Statistics window '%s' must select exactly one of step_cadence and time_cadence.",
265 PetscFunctionReturn(0);
279 PetscFunctionBeginUser;
281 for (PetscInt field_index = 0; field_index < definition->
field_count; ++field_index) {
282 PetscCall(PetscStrlen(fields, &used));
283 PetscCall(PetscSNPrintf(fields + used,
sizeof(fields) - used,
"%s%s%s",
284 field_index ?
" " :
"",
290 "Statistics window '%s': t=[%.6g,%s] weighting=%s every %d step(s), fields: %s, "
291 "%d covariance(s).\n",
293 definition->
bounded ?
"bounded" :
"open",
298 "Statistics window '%s': t=[%.6g,%s] weighting=%s every %.6g of time, fields: %s, "
299 "%d covariance(s).\n",
301 definition->
bounded ?
"bounded" :
"open",
310 PetscFunctionReturn(0);
321 PetscBool found = PETSC_FALSE;
323 PetscFunctionBeginUser;
325 PetscCheck(simCtx != NULL && simCtx->
pps != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
326 "SimCtx and its post-processing parameters are required.");
328 PetscCall(PetscOptionsGetInt(NULL, NULL,
"-field_statistics_ensemble_count", &count, &found));
334 for (PetscInt index = 0; index < count; ++index) {
338 PetscBool from_particles = PETSC_FALSE;
340 PetscCall(PetscMemzero(&definition,
sizeof(definition)));
341 PetscCall(PetscSNPrintf(option_name,
sizeof(option_name),
342 "-field_statistics_ensemble_%" PetscInt_FMT
"_name", index));
343 PetscCall(PetscOptionsGetString(NULL, NULL, option_name, definition.
name,
344 sizeof(definition.
name), &found));
345 PetscCheck(found && definition.
name[0] !=
'\0', PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
346 "Statistics ensemble %" PetscInt_FMT
" has no name.", index);
349 PetscBool clash = PETSC_FALSE;
352 PetscCheck(!clash, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
353 "Statistics ensemble '%s' has the name of a run window.", definition.
name);
355 for (PetscInt prior = 0; prior < index; ++prior) {
356 PetscBool clash = PETSC_FALSE;
359 PetscCheck(!clash, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
360 "Statistics ensemble name '%s' is used more than once.", definition.
name);
363 PetscCall(PetscSNPrintf(option_name,
sizeof(option_name),
364 "-field_statistics_ensemble_%" PetscInt_FMT
"_source", index));
365 PetscCall(PetscOptionsGetString(NULL, NULL, option_name, source,
sizeof(source), &found));
366 PetscCheck(found && (!strcmp(source,
"particles") || !strcmp(source,
"eulerian")),
367 PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
368 "Statistics ensemble '%s' needs source 'particles' or 'eulerian'.", definition.
name);
369 from_particles = (PetscBool)!strcmp(source,
"particles");
384 PetscFunctionReturn(0);
395 PetscInt window_count = 0;
396 PetscBool found = PETSC_FALSE;
398 PetscFunctionBeginUser;
400 PetscCheck(simCtx != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
"SimCtx cannot be NULL.");
402 PetscCall(PetscOptionsGetInt(NULL, NULL,
"-statistics_console_output_freq",
404 PetscCall(PetscOptionsGetBool(NULL, NULL,
"-field_statistics_enabled",
409 PetscFunctionReturn(0);
412 PetscCall(PetscOptionsGetInt(NULL, NULL,
"-field_statistics_window_count", &window_count, &found));
413 PetscCheck(found && window_count > 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
414 "Field statistics are enabled but no window is configured.");
418 for (PetscInt window_index = 0; window_index < window_count; ++window_index) {
422 PetscCall(PetscMemzero(&definition,
sizeof(definition)));
423 PetscCall(PetscSNPrintf(option_name,
sizeof(option_name),
424 "-field_statistics_window_%" PetscInt_FMT
"_name", window_index));
425 PetscCall(PetscOptionsGetString(NULL, NULL, option_name, definition.
name,
426 sizeof(definition.
name), &found));
427 PetscCheck(found && definition.
name[0] !=
'\0', PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
428 "Statistics window %" PetscInt_FMT
" has no name.", window_index);
432 for (PetscInt prior = 0; prior < window_index; ++prior) {
433 PetscBool clash = PETSC_FALSE;
436 definition.
name, &clash));
437 PetscCheck(!clash, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
438 "Statistics window name '%s' is used more than once; names identify saved "
439 "state across a restart and must be unique.", definition.
name);
451 PetscFunctionReturn(0);
462 PetscFunctionBeginUser;
475 PetscFunctionReturn(0);
Authoritative identities and storage metadata for persistent Eulerian fields.
PetscErrorCode FieldIdFromName(const char *field_name, FieldId *field_id)
Resolve a user-facing field name once into its typed identity.
const char * FieldCanonicalName(FieldId field_id)
Return the canonical printable name for an ID.
FieldId
Compile-time identity for a catalogued Eulerian field.
Public interface for data input/output routines.
void TrimWhitespace(char *str)
Removes leading and trailing ASCII whitespace from a mutable string.
Logging utilities and macros for PETSc-based applications.
#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_INFO
Informational messages about program execution.
@ 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.
ParticleFieldId
Compile-time identity for a persistent solver-particle field.
@ PARTICLE_FIELD_ID_INVALID
unsigned int capabilities
PetscErrorCode ParticleFieldIdFromName(const char *field_name, ParticleFieldId *field_id)
Resolve a canonical name or registered alias at a text-ingress boundary.
@ PARTICLE_FIELD_CAPABILITY_EULERIAN_SCATTER
PetscErrorCode ParticleFieldGetDescriptor(ParticleFieldId field_id, const ParticleFieldDescriptor **descriptor)
Return immutable metadata for a valid particle field ID.
FieldId eulerian_scatter_target
Immutable metadata for one persistent particle field.
PetscErrorCode DestroyFieldStatisticsConfig(SimCtx *simCtx)
Implementation of DestroyFieldStatisticsConfig().
static PetscErrorCode ResolveStatisticsField(const char *window_name, const char *field_name, PetscBool particle_source, PetscInt *field_id)
Internal helper: resolves one field name against the typed catalog.
#define STATISTICS_OPTION_NAME_LENGTH
Longest constructed option name this module builds.
#define STATISTICS_OPTION_VALUE_LENGTH
Longest option value this module reads, sized for a moment or pair list.
PetscErrorCode ParseFieldStatisticsConfig(SimCtx *simCtx)
Implementation of ParseFieldStatisticsConfig().
static PetscErrorCode LogResolvedWindow(const PicurvWindowDefinition *definition)
Internal helper: reports one resolved window definition to the log.
PetscErrorCode ParsePostStatisticsEnsembles(SimCtx *simCtx)
Implementation of ParsePostStatisticsEnsembles().
static PetscErrorCode ParseStatisticsFields(const char *kind, PetscInt window, PetscBool particle_source, PicurvWindowDefinition *definition)
Internal helper: reads one window's field list into its definition.
static PetscErrorCode ParseStatisticsWindowSchedule(PetscInt window, PicurvWindowDefinition *definition)
Internal helper: reads one window's scheduling and weighting properties.
static PetscErrorCode ParseStatisticsCovariances(const char *kind, PetscInt window, PetscBool particle_source, PicurvWindowDefinition *definition)
Internal helper: reads one window's covariance list into its definition.
static PetscErrorCode SplitStatisticsList(char *value, char **tokens, PetscInt capacity, PetscInt *count)
Internal helper: splits a comma-separated value into trimmed tokens.
Control ingress for the field-statistics pipeline.
Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
PetscInt first
First member; must also appear in the field list.
PicurvWindowFieldRequest fields[16]
PetscReal time_cadence
Used when cadence_kind is time; must be positive.
PetscReal end_time
Requested end; ignored when bounded is false.
PicurvCadenceKind cadence_kind
PetscInt step_cadence
Used when cadence_kind is step; must be positive.
PetscErrorCode PicurvWindowInit(PicurvWindow *window, const PicurvWindowDefinition *definition)
Validates a definition and initializes a window to the pending state.
PetscBool want_second
Also keep the centered second moment.
PetscInt second
Second member; must also appear in the field list.
PicurvWindowCovarianceRequest covariances[16]
PetscBool bounded
False for an open-ended window.
PicurvWindowDefinition definition
PetscReal start_time
Requested start.
PetscInt covariance_count
PetscInt field_id
Catalogued Eulerian field identity.
#define PICURV_WINDOW_MAX_REQUESTS
Maximum fields or covariance pairs one window may request.
@ PICURV_WEIGHTING_PHYSICAL_TIME
Weight is the represented interval.
@ PICURV_WEIGHTING_SAMPLE
Equal weight per accepted state.
@ PICURV_CADENCE_TIME
First state at or past each nominal time target.
@ PICURV_CADENCE_STEP
Every n completed steps from activation.
PicurvWeighting weighting
The scientifically immutable definition of one window.
PetscInt fieldStatisticsWindowCount
PetscInt statisticsConsoleOutputFreq
struct PicurvWindow * field_statistics_ensembles
PetscBool fieldStatisticsEnabled
struct PicurvWindow * fieldStatisticsWindows
PetscBool * field_statistics_ensemble_from_particles
Whether each ensemble samples the particles in a cell rather than the cell's state.
PetscInt field_statistics_ensemble_count
Recipe-defined single-instant windows, derived at every processed step.
Holds all configuration parameters for a post-processing run.
The master context for the entire simulation.