91#define LOG(scope, level, fmt, ...) \
94 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
96 if ((int)(level) <= (int)get_log_level()) { \
98 PetscPrintf(comm, fmt, ##__VA_ARGS__); \
122#define LOG_DEFAULT(level, fmt, ...) \
125 MPI_Comm comm = MPI_COMM_WORLD; \
127 if ((int)(level) <= (int)get_log_level()) { \
129 PetscPrintf(comm, fmt, ##__VA_ARGS__); \
152#define LOG_SYNC(scope, level, fmt, ...) \
155 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
157 if ((int)(level) <= (int)get_log_level()) { \
159 PetscSynchronizedPrintf(comm, fmt, ##__VA_ARGS__); \
161 PetscSynchronizedFlush(comm, PETSC_STDOUT); \
185#define LOG_SYNC_DEFAULT(level, fmt, ...) \
187 if ((int)(level) <= (int)get_log_level()) { \
188 PetscSynchronizedPrintf(MPI_COMM_WORLD, fmt, ##__VA_ARGS__); \
189 PetscSynchronizedFlush(MPI_COMM_WORLD, PETSC_STDOUT); \
207#define LOG_ALLOW(scope, level, fmt, ...) \
209 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
210 if ((int)(level) <= (int)get_log_level() && is_function_allowed(__func__)) { \
211 PetscPrintf(comm, "[%s] " fmt, __func__, ##__VA_ARGS__); \
274#define LOG_ALLOW_SYNC(scope, level, fmt, ...) \
280 if ((scope) == LOCAL) _comm = MPI_COMM_SELF; \
281 else if ((scope) == GLOBAL) _comm = MPI_COMM_WORLD; \
283 fprintf(stderr, "LOG_ALLOW_SYNC ERROR: invalid scope (%d) at %s:%d\n", \
284 (scope), __FILE__, __LINE__); \
285 MPI_Abort(MPI_COMM_WORLD, 1); \
291 PetscBool _doPrint = \
292 is_function_allowed(__func__) && ((int)(level) <= (int)get_log_level()); \
295 PetscSynchronizedPrintf(_comm, "[%s] " fmt, __func__, ##__VA_ARGS__); \
301 PetscSynchronizedFlush(_comm, PETSC_STDOUT); \
319#define LOG_LOOP_ALLOW(scope, level, iterVar, interval, fmt, ...) \
321 if (is_function_allowed(__func__) && (int)(level) <= (int)get_log_level()) { \
322 if ((iterVar) % (interval) == 0) { \
323 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
324 PetscPrintf(comm, "[%s] [%s=%d] " fmt, \
325 __func__, #iterVar, (iterVar), ##__VA_ARGS__); \
355#define LOG_LOOP_ALLOW_EXACT(scope, level, var, val, fmt, ...) \
358 if (is_function_allowed(__func__) && (int)(level) <= (int)get_log_level()) { \
360 if ((var) == (val)) { \
361 MPI_Comm comm = ((scope) == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
363 PetscPrintf(comm, "[%s] [%s=%d] " fmt, \
364 __func__, #var, (var), ##__VA_ARGS__); \
384#define LOG_ARRAY_ELEMENT_ALLOW(scope,level, arr, length, idx, fmt) \
386 if (is_function_allowed(__func__) && (int)(level) <= (int)get_log_level()) { \
387 if ((idx) >= 0 && (idx) < (length)) { \
388 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
389 PetscPrintf(comm, "[%s] arr[%d] = " fmt "\n", \
390 __func__, (idx), (arr)[idx]); \
408#define LOG_ARRAY_SUBRANGE_ALLOW(scope,level, arr, length, start, end, fmt) \
410 if (is_function_allowed(__func__) && (int)(level) <= (int)get_log_level()) { \
411 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
412 PetscInt _start = (start) < 0 ? 0 : (start); \
413 PetscInt _end = (end) >= (length) ? (length) - 1 : (end); \
414 for (PetscInt i = _start; i <= _end; i++) { \
415 PetscPrintf(comm, "[%s] arr[%d] = " fmt "\n", __func__, i, (arr)[i]); \
436#define LOG_FUNC_TIMER_BEGIN_EVENT(eventID, scope) \
437 double __funcTimerStart = 0.0; \
438 PetscBool __funcTimerActive = PETSC_FALSE; \
440 if (is_function_allowed(__func__) && (int)(LOG_PROFILE) == (int)get_log_level()) { \
441 PetscLogDouble _timeStamp = 0.0; \
442 PetscTime(&_timeStamp); \
443 __funcTimerStart = (double)_timeStamp; \
444 __funcTimerActive = PETSC_TRUE; \
446 (void)PetscLogEventBegin(eventID, 0, 0, 0, 0); \
462#define LOG_FUNC_TIMER_END_EVENT(eventID, scope) \
464 if (__funcTimerActive == PETSC_TRUE) { \
466 (void)PetscLogEventEnd(eventID, 0, 0, 0, 0); \
468 if (is_function_allowed(__func__) && (int)(LOG_PROFILE) == (int)get_log_level()) { \
469 PetscLogDouble _timeEnd = 0.0; \
470 PetscTime(&_timeEnd); \
471 double elapsed = (double)_timeEnd - __funcTimerStart; \
472 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
473 PetscPrintf(comm, "[%s] Elapsed Time: %f seconds\n", __func__, elapsed); \
479#define LOG_PROFILE_MSG(scope, fmt, ...) \
481 if ((int)(LOG_PROFILE) <= (int)get_log_level()) { \
482 MPI_Comm comm = (scope == LOCAL) ? MPI_COMM_SELF : MPI_COMM_WORLD; \
483 PetscPrintf(comm, "[PROFILE] " fmt, ##__VA_ARGS__); \
668PetscErrorCode
DualKSPMonitor(KSP ksp, PetscInt it, PetscReal rnorm,
void *ctx);
758#define PROFILE_FUNCTION_BEGIN \
759 _ProfilingStart(__FUNCT__)
767#define PROFILE_FUNCTION_END \
768 _ProfilingEnd(__FUNCT__)
PetscLogEvent EVENT_walkingsearch
void set_allowed_functions(const char **functionList, int count)
Sets the global list of function names that are allowed to log.
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.
PetscErrorCode DualMonitorDestroy(void **ctx)
Destroys the DualMonitorCtx.
const char * BCFaceToString(BCFace face)
Helper function to convert BCFace enum to a string representation.
PetscErrorCode FreeAllowedFunctions(char **funcs, PetscInt n)
Free an array previously returned by LoadAllowedFunctionsFromFile().
PetscErrorCode print_log_level(void)
Prints the current logging level to the console.
PetscLogEvent EVENT_IndividualLocation
PetscErrorCode LoadAllowedFunctionsFromFile(const char filename[], char ***funcsOut, PetscInt *nOut)
Load function names from a text file.
LogLevel get_log_level()
Retrieves the current logging level from the environment variable LOG_LEVEL.
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.
void _ProfilingEnd(const char *func_name)
const char * BCTypeToString(BCType type)
Helper function to convert BCType enum to a string representation.
const char * ParticleLocationStatusToString(ParticleLocationStatus level)
A function that outputs the name of the current level in the ParticleLocation enum.
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 ProfilingFinalize(void)
Prints the final, cumulative performance summary and cleans up resources.
PetscErrorCode ProfilingInitialize(SimCtx *simCtx)
Initializes the custom profiling system using configuration from SimCtx.
LogLevel
Enumeration of logging levels.
@ LOG_ERROR
Critical errors that may halt the program.
@ LOG_PROFILE
Exclusive log level for performance timing and profiling.
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
PetscErrorCode LOG_CELL_VERTICES(const Cell *cell, PetscMPIInt rank)
Prints the coordinates of a cell's vertices.
PetscLogEvent EVENT_GlobalParticleLocation
PetscLogEvent EVENT_Individualwalkingsearch
PetscErrorCode ProfilingLogTimestepSummary(PetscInt step)
Logs the performance summary for the current timestep and resets timers.
void _ProfilingStart(const char *func_name)
Context for a dual-purpose KSP monitor.
Main header file for a complex fluid dynamics solver.
BCType
Defines the general mathematical/physical category of a boundary.
ParticleLocationStatus
Defines the state of a particle with respect to its location and migration status during the iterativ...
BCHandlerType
Defines the specific computational "strategy" for a boundary handler.
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.