86#define LOG(scope, level, fmt, ...) \
89 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
91 if ((int)(level) <= (int)get_log_level()) { \
93 PetscPrintf(comm, fmt, ##__VA_ARGS__); \
117#define LOG_DEFAULT(level, fmt, ...) \
120 MPI_Comm comm = MPI_COMM_WORLD; \
122 if ((int)(level) <= (int)get_log_level()) { \
124 PetscPrintf(comm, fmt, ##__VA_ARGS__); \
147#define LOG_SYNC(scope, level, fmt, ...) \
150 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
152 if ((int)(level) <= (int)get_log_level()) { \
154 PetscSynchronizedPrintf(comm, fmt, ##__VA_ARGS__); \
156 PetscSynchronizedFlush(comm, PETSC_STDOUT); \
180#define LOG_SYNC_DEFAULT(level, fmt, ...) \
182 if ((int)(level) <= (int)get_log_level()) { \
183 PetscSynchronizedPrintf(MPI_COMM_WORLD, fmt, ##__VA_ARGS__); \
184 PetscSynchronizedFlush(MPI_COMM_WORLD, PETSC_STDOUT); \
198#define LOG_LEVEL_ADMITS(level) \
199 ((int)(level) <= (int)get_log_level() && ((int)(level) <= (int)LOG_WARNING || is_function_allowed(__func__)))
214#define LOG_ALLOW(scope, level, fmt, ...) \
216 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
217 if (LOG_LEVEL_ADMITS(level)) { \
218 PetscPrintf(comm, "[%s] " fmt, __func__, ##__VA_ARGS__); \
273#define LOG_ALLOW_SYNC(scope, level, fmt, ...) \
279 if ((scope) == LOCAL) _comm = MPI_COMM_SELF; \
280 else if ((scope) == GLOBAL) _comm = MPI_COMM_WORLD; \
282 fprintf(stderr, "LOG_ALLOW_SYNC ERROR: invalid scope (%d) at %s:%d\n", \
283 (scope), __FILE__, __LINE__); \
284 MPI_Abort(MPI_COMM_WORLD, 1); \
290 PetscBool _doPrint = \
291 LOG_LEVEL_ADMITS(level); \
294 PetscSynchronizedPrintf(_comm, "[%s] " fmt, __func__, ##__VA_ARGS__); \
296 PetscSynchronizedFlush(_comm, PETSC_STDOUT); \
310 const PetscInt mid = lo + (hi - lo) / 2;
311 if (simCtx->
tracePIDs[mid] == pid)
return PETSC_TRUE;
312 if (simCtx->
tracePIDs[mid] < pid) lo = mid + 1;
319#define PARTICLE_LOG_VERBOSE_ENABLED() \
320 (PetscUnlikely((int)get_log_level() >= (int)LOG_VERBOSE) && is_function_allowed(__func__))
327#define PARTICLE_IS_TRACED(simCtx, pid) \
328 ((PetscBool)(PetscUnlikely((simCtx)->nTracePIDs > 0) && ParticleIsTraced((simCtx), (pid))))
335#define PARTICLE_LOG_ACTIVE(simCtx, pid) \
336 ((PetscBool)(PARTICLE_IS_TRACED((simCtx), (pid)) || PARTICLE_LOG_VERBOSE_ENABLED()))
355#define LOG_PARTICLE(simCtx, pid, stage, fmt, ...) \
357 const PetscBool _picurv_traced = PARTICLE_IS_TRACED((simCtx), (pid)); \
358 if (_picurv_traced || PARTICLE_LOG_VERBOSE_ENABLED()) { \
359 PetscPrintf(PETSC_COMM_SELF, "[%s pid=%lld step=%" PetscInt_FMT " %s] " fmt, \
360 _picurv_traced ? "trace" : __func__, (long long)(pid), (simCtx)->step, \
361 (stage), ##__VA_ARGS__); \
380#define LOG_LOOP_ALLOW(scope, level, iterVar, interval, fmt, ...) \
382 if (LOG_LEVEL_ADMITS(level)) { \
383 if ((iterVar) % (interval) == 0) { \
384 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
385 PetscPrintf(comm, "[%s] [%s=%" PetscInt_FMT "] " fmt, \
386 __func__, #iterVar, (iterVar), ##__VA_ARGS__); \
405#define LOG_LOOP_ALLOW_EXACT(scope, level, var, val, fmt, ...) \
408 if (LOG_LEVEL_ADMITS(level)) { \
410 if ((var) == (val)) { \
411 MPI_Comm comm = ((scope) == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
413 PetscPrintf(comm, "[%s] [%s=%" PetscInt_FMT "] " fmt, \
414 __func__, #var, (var), ##__VA_ARGS__); \
434#define LOG_ARRAY_ELEMENT_ALLOW(scope,level, arr, length, idx, fmt) \
436 if (LOG_LEVEL_ADMITS(level)) { \
437 if ((idx) >= 0 && (idx) < (length)) { \
438 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
439 PetscPrintf(comm, "[%s] arr[%" PetscInt_FMT "] = " fmt "\n", \
440 __func__, (PetscInt)(idx), (arr)[idx]); \
458#define LOG_ARRAY_SUBRANGE_ALLOW(scope,level, arr, length, start, end, fmt) \
460 if (LOG_LEVEL_ADMITS(level)) { \
461 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
462 PetscInt _start = (start) < 0 ? 0 : (start); \
463 PetscInt _end = (end) >= (length) ? (length) - 1 : (end); \
464 for (PetscInt i = _start; i <= _end; i++) { \
465 PetscPrintf(comm, "[%s] arr[%" PetscInt_FMT "] = " fmt "\n", __func__, i, (arr)[i]); \
810PetscErrorCode
DualKSPMonitor(KSP ksp, PetscInt it, PetscReal rnorm,
void *ctx);
889void PrintProgressBar(PetscInt step, PetscInt startStep, PetscInt totalSteps, PetscReal currentTime,
890 PetscReal seconds_per_step, PetscInt64 lost_cumulative);
983void _ProfilingRegister(
const char *func_name, PetscBool helper, PetscLogEvent *event);
1000#define PROFILE_FUNCTION_BEGIN \
1001 static PetscLogEvent _picurv_profile_event = -1; \
1002 if (PetscUnlikely(_picurv_profile_event < 0)) _ProfilingRegister(__func__, PETSC_FALSE, &_picurv_profile_event); \
1003 const PetscBool _picurv_profile_timed = (PetscBool)(_picurv_profile_event >= 0); \
1004 if (_picurv_profile_timed) (void)PetscLogEventBegin(_picurv_profile_event, 0, 0, 0, 0)
1014#define PROFILE_HELPER_BEGIN \
1015 static PetscLogEvent _picurv_profile_event = -1; \
1016 if (PetscUnlikely(_picurv_profile_event < 0)) _ProfilingRegister(__func__, PETSC_TRUE, &_picurv_profile_event); \
1017 const PetscBool _picurv_profile_timed = (PetscBool)(*_picurv_profile_helpers_on && _picurv_profile_event >= 0); \
1018 if (_picurv_profile_timed) (void)PetscLogEventBegin(_picurv_profile_event, 0, 0, 0, 0)
1029#define PROFILE_FUNCTION_END \
1030 do { if (_picurv_profile_timed) (void)PetscLogEventEnd(_picurv_profile_event, 0, 0, 0, 0); } while (0)
1033#define PROFILE_HELPER_END PROFILE_FUNCTION_END
1181PetscErrorCode
PicurvOpenRunCsv(
SimCtx *simCtx,
const char *dir,
const char *filename,
const char *header,
1198 const char *header, FILE **file)
FieldId
Compile-time identity for a catalogued Eulerian field.
PetscErrorCode LOG_FIELD_MIN_MAX(UserCtx *user, FieldId field_id)
Computes and logs the local and global min/max values of a 3-component vector field.
const char * LESTestFilterKernelToString(LESTestFilterKernel kernel)
Returns the user-facing name of an LES test-filter kernel.
void set_allowed_functions(const char **functionList, int count)
Sets the global list of function names that are allowed to log.
PetscErrorCode PicurvOpenRunCsv(SimCtx *simCtx, const char *dir, const char *filename, const char *header, PicurvCsvLifecycle lifecycle, PetscBool required, FILE **file)
Opens one of the run's CSV records (diagnostics, solver history, profiling) for writing.
PetscBool ShouldEmitPeriodicStatisticsConsoleSnapshot(const struct SimCtx *simCtx, PetscInt completed_step)
Reports whether a completed step falls on the console snapshot cadence.
PicurvCsvLifecycle
What a run's first write does to an existing CSV record (see PicurvOpenRunCsv()).
@ PICURV_CSV_TRUNCATE_ON_FRESH
A fresh run's first write empties the file; a continued run appends.
@ PICURV_CSV_APPEND
Every run appends: earlier runs' rows are kept.
const char * BCHandlerTypeToString(BCHandlerType handler_type)
Converts a BCHandlerType enum to its string representation.
PetscBool is_function_allowed(const char *functionName)
Checks if a given function is in the allow-list.
const LogLevel *const _picurv_log_level
Read-only view of the cached logging level; -1 until the first get_log_level() call.
PetscErrorCode DualMonitorDestroy(void **ctx)
Destroys the DualMonitorCtx.
PetscBool IsStatisticsConsoleSnapshotEnabled(const struct SimCtx *simCtx)
Reports whether the periodic statistics console snapshot is enabled.
void _ProfilingRegister(const char *func_name, PetscBool helper, PetscLogEvent *event)
Internal profiling hook: register a call site's function as a PETSc log event.
PetscErrorCode LOG_INTERPOLATION_ERROR(UserCtx *user)
Logs the interpolation error between the analytical and computed solutions.
const char * ParticleBoundaryPolicyToString(ParticleBoundaryPolicy policy)
Returns the configuration token for a particle boundary policy.
PetscBool ShouldEmitPeriodicParticleConsoleSnapshot(const SimCtx *simCtx, PetscInt completed_step)
Returns whether a particle console snapshot should be emitted for the completed timestep.
const char * BCFaceToString(BCFace face)
Returns the canonical log token for a boundary-face enum value.
static PetscErrorCode PicurvOpenDiagnosticsCsv(SimCtx *simCtx, const char *dir, const char *filename, const char *header, FILE **file)
Opens a per-step diagnostics CSV: PicurvOpenRunCsv() with the common settings.
PetscErrorCode FreeAllowedFunctions(char **funcs, PetscInt n)
Free an array previously returned by LoadAllowedFunctionsFromFile().
PetscBool IsParticleConsoleSnapshotEnabled(const SimCtx *simCtx)
Returns whether periodic particle console snapshots are enabled.
PetscErrorCode print_log_level(void)
Prints the current logging level to the console.
static PetscBool ParticleIsTraced(const SimCtx *simCtx, PetscInt64 pid)
True when pid is one of the run's traced particles (simCtx->tracePIDs, sorted).
PetscErrorCode EmitParticleConsoleSnapshot(UserCtx *user, SimCtx *simCtx, PetscInt step)
Emits one particle console snapshot into the main solver log.
void PrintProgressBar(PetscInt step, PetscInt startStep, PetscInt totalSteps, PetscReal currentTime, PetscReal seconds_per_step, PetscInt64 lost_cumulative)
Prints a progress bar to the console.
PetscErrorCode ProfilingFinalize(SimCtx *simCtx)
Writes the end-of-run profiling summary across all ranks; collective on PETSC_COMM_WORLD.
PetscErrorCode LoadAllowedFunctionsFromFile(const char filename[], char ***funcsOut, PetscInt *nOut)
Load function names from a text file.
PetscErrorCode EmitStatisticsConsoleSnapshot(UserCtx *user, const struct SimCtx *simCtx, PetscInt step)
Emits one console snapshot of window progress.
const char * WallFunctionModelToString(WallFunctionModel model)
Returns the user-facing name of a wall-function model.
PetscErrorCode RuntimeMemoryLogSample(SimCtx *simCtx, PetscInt step, const char *event, const char *reason)
Append a reduced runtime memory sample to the configured memory log.
PetscErrorCode ProfilingLogTimestepSummary(SimCtx *simCtx, PetscInt step)
Logs the performance summary for the current timestep and resets timers.
const char * LESFilterWidthModelToString(LESFilterWidthModel model)
Returns the user-facing name of an LES grid-filter-width model.
PetscErrorCode LOG_FACE_DISTANCES(PetscReal *d)
Prints the signed distances to each face of the cell.
PetscErrorCode LOG_PARTICLE_FIELDS(UserCtx *user, PetscInt printInterval)
Prints particle fields in a table that automatically adjusts its column widths.
const char * BCTypeToString(BCType type)
Returns the canonical log token for a boundary mathematical type.
PetscErrorCode CalculateAdvancedParticleMetrics(UserCtx *user)
Computes advanced particle statistics and stores them in SimCtx.
const char * ParticleLocationStatusToString(ParticleLocationStatus level)
A function that outputs the name of the current level in the ParticleLocation enum.
PetscErrorCode LOG_SCATTER_METRICS(UserCtx *user)
Logs particle-to-grid scatter verification metrics for the prescribed scalar truth path.
PetscErrorCode LOG_SOLUTION_CONVERGENCE(SimCtx *simCtx)
Logs physical solution-convergence metrics once per completed timestep.
PetscErrorCode DualKSPMonitor(KSP ksp, PetscInt it, PetscReal rnorm, void *ctx)
A custom KSP monitor that logs to a file and optionally to the console.
PetscErrorCode LOG_CONTINUITY_METRICS(UserCtx *user)
Logs continuity metrics for a single block to a file.
PetscErrorCode LOG_CORNER_FIELD_ANATOMY(UserCtx *user, FieldId corner_field_id, const char *stage_name)
Logs the node-layout anatomy of the transient center-to-corner interpolation field.
const char * LESAveragingModeToString(LESAveragingMode mode)
Returns the user-facing name of an LES coefficient-averaging mode.
PetscErrorCode LOG_FIELD_ANATOMY(UserCtx *user, FieldId field_id, const char *stage_name)
Logs the anatomy of a specified field at key boundary locations, respecting the solver's specific gri...
PetscErrorCode LOG_SEARCH_METRICS(UserCtx *user)
Writes compact runtime search metrics to CSV and optionally to console.
const char * InterpolationMethodToString(InterpolationMethod method)
Returns the display name of a grid-to-particle interpolation method.
const PetscBool *const _picurv_profile_helpers_on
Read-only view: whether profiling.helpers enables any helper timer.
PetscErrorCode ProfilingInitialize(SimCtx *simCtx)
Applies the profiling configuration from SimCtx to PICurv's PETSc log events.
LogLevel
Enumeration of logging levels.
@ LOG_ERROR
Critical errors that may halt the program.
@ LOG_TRACE
Very fine-grained tracing information for in-depth debugging.
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
@ LOG_VERBOSE
Extremely detailed logs, typically for development use only.
const char * LESModelToString(LESModelType LESFlag)
Returns the canonical log token for an LES model selector.
PetscErrorCode LOG_CELL_VERTICES(const Cell *cell, PetscMPIInt rank)
Prints the coordinates of a cell's vertices.
LogLevel _ResolveLogLevel(void)
Internal hook: read LOG_LEVEL from the environment and cache it, once.
PetscErrorCode ProfilingResetTimestepCounters(void)
Starts a new per-step profiling interval.
PetscErrorCode ResetSearchMetrics(SimCtx *simCtx)
Resets the aggregate per-timestep search instrumentation counters.
const char * LESClipModeToString(LESClipMode mode)
Returns the user-facing name of an LES coefficient-limiting mode.
const char * MomentumSolverTypeToString(MomentumSolverType SolverFlag)
Returns the canonical log token for a momentum-solver selector.
const char * ParticleInitializationToString(ParticleInitializationType ParticleInitialization)
Returns the canonical log token for a particle-initialization mode.
static LogLevel get_log_level(void)
Retrieves the current logging level from the environment variable LOG_LEVEL.
Context for a dual-purpose KSP monitor.
Window lifecycle, scheduling, and weighting for the field-statistics pipeline.
Main header file for a complex fluid dynamics solver.
LESModelType
Identifies the subgrid-scale closure evaluated during a timestep.
BCType
Defines the general mathematical/physical Category of a boundary.
ParticleInitializationType
Enumerator to identify the particle initialization strategy.
ParticleLocationStatus
Defines the state of a particle with respect to its location and migration status during the iterativ...
PetscInt nTracePIDs
Number of entries in tracePIDs (at most PICURV_MAX_TRACED_PARTICLES).
ParticleBoundaryPolicy
What happens to a particle that crosses a domain-boundary face.
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
LESClipMode
Selects the admissible range imposed on the dynamic model coefficient.
LESTestFilterKernel
Selects the discrete test-filter kernel used by the dynamic procedure.
WallFunctionModel
Selects the wall model applied on WALL faces.
MomentumSolverType
Enumerator to identify the implemented momentum solver strategies.
InterpolationMethod
Selects the grid-to-particle interpolation method.
LESAveragingMode
Selects the set over which the Germano contractions are averaged.
PetscInt64 * tracePIDs
Sorted, distinct IDs of the particles whose every stage is reported (-picurv_trace_particles); NULL w...
LESFilterWidthModel
Selects how a cell's grid filter width is derived from its metrics.
BCFace
Identifies the six logical faces of a structured computational block.
Defines the vertices of a single hexahedral grid cell.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.