LCOV - code coverage report
Current view: top level - api - output_registry.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.3 % 83 70
Test Date: 2026-10-05 04:47:18 Functions: 86.7 % 15 13

            Line data    Source code
       1              : #include "cfd/io/output_registry.h"
       2              : #include "../io/csv_output_internal.h"
       3              : #include "../io/vtk_output_internal.h"
       4              : #include "cfd/api/simulation_api.h"
       5              : #include "cfd/core/filesystem.h"
       6              : #include "cfd/core/logging.h"
       7              : #include "cfd/core/memory.h"
       8              : 
       9              : 
      10              : #include <string.h>
      11              : 
      12              : //=============================================================================
      13              : // OUTPUT REGISTRY STRUCTURE
      14              : //=============================================================================
      15              : 
      16              : #define MAX_OUTPUT_CONFIGS 16
      17              : 
      18              : struct OutputRegistry {
      19              :     output_config configs[MAX_OUTPUT_CONFIGS];
      20              :     int count;
      21              :     char run_dir[512];    // Cached run directory path
      22              :     int run_dir_created;  // Flag to track if run directory was created
      23              : };
      24              : 
      25              : //=============================================================================
      26              : // OUTPUT REGISTRY LIFECYCLE
      27              : //=============================================================================
      28              : 
      29           94 : output_registry* output_registry_create(void) {
      30           94 :     output_registry* reg = (output_registry*)cfd_calloc(1, sizeof(struct OutputRegistry));
      31           94 :     if (reg) {
      32           94 :         reg->count = 0;
      33           94 :         reg->run_dir[0] = '\0';
      34           94 :         reg->run_dir_created = 0;
      35              :     }
      36           94 :     return reg;
      37              : }
      38              : 
      39           95 : void output_registry_destroy(output_registry* reg) {
      40           95 :     if (reg) {
      41           94 :         cfd_free(reg);
      42              :     }
      43           95 : }
      44              : 
      45              : //=============================================================================
      46              : // OUTPUT REGISTRY CONFIGURATION
      47              : //=============================================================================
      48              : 
      49           19 : void output_registry_add(output_registry* reg, output_field_type field_type, int interval,
      50              :                          const char* prefix) {
      51           19 :     if (!reg) {
      52              :         return;
      53              :     }
      54              : 
      55              :     // Check if we have space
      56           18 :     if (reg->count >= MAX_OUTPUT_CONFIGS) {
      57            0 :         cfd_warning("Maximum number of output configurations reached");
      58            0 :         return;
      59              :     }
      60              : 
      61              :     // Add new configuration
      62           18 :     reg->configs[reg->count].field_type = field_type;
      63           18 :     reg->configs[reg->count].interval = interval;
      64           18 :     reg->configs[reg->count].prefix = prefix;
      65           18 :     reg->count++;
      66              : }
      67              : 
      68            9 : void output_registry_clear(output_registry* reg) {
      69            9 :     if (reg) {
      70            8 :         reg->count = 0;
      71              :     }
      72            9 : }
      73              : 
      74            7 : int output_registry_count(const output_registry* reg) {
      75            7 :     return reg ? reg->count : 0;
      76              : }
      77              : 
      78           51 : int output_registry_has_type(const output_registry* reg, output_field_type field_type) {
      79           51 :     if (!reg) {
      80              :         return 0;
      81              :     }
      82              : 
      83           80 :     for (int i = 0; i < reg->count; i++) {
      84           44 :         if (reg->configs[i].field_type == field_type) {
      85              :             return 1;
      86              :         }
      87              :     }
      88              :     return 0;
      89              : }
      90              : 
      91              : //=============================================================================
      92              : // RUN DIRECTORY MANAGEMENT
      93              : //=============================================================================
      94              : 
      95            9 : const char* output_registry_get_run_dir(output_registry* reg, const char* base_dir,
      96              :                                         const char* run_prefix, size_t nx, size_t ny) {
      97            9 :     if (!reg) {
      98              :         return NULL;
      99              :     }
     100              : 
     101              :     // Only create once
     102            9 :     if (reg->run_dir_created) {
     103            4 :         return reg->run_dir;
     104              :     }
     105              : 
     106              :     // Create run directory with prefix using custom base directory (re-entrant)
     107            5 :     const char* prefix = run_prefix ? run_prefix : "sim";
     108            5 :     cfd_create_run_directory_ex_with_base(reg->run_dir, sizeof(reg->run_dir), base_dir, prefix, nx,
     109              :                                           ny);
     110            5 :     if (reg->run_dir[0] == '\0') {
     111              :         return NULL;  // Path too long (already reported); retried on the next call
     112              :     }
     113              : 
     114            5 :     reg->run_dir_created = 1;
     115            5 :     return reg->run_dir;
     116              : }
     117              : 
     118            3 : void output_registry_reset_run_dir(output_registry* reg) {
     119            3 :     if (!reg) {
     120              :         return;
     121              :     }
     122            3 :     reg->run_dir_created = 0;
     123            3 :     reg->run_dir[0] = '\0';
     124              : }
     125              : 
     126              : //=============================================================================
     127              : // OUTPUT DISPATCH
     128              : //=============================================================================
     129              : 
     130              : // Function pointer type for output dispatchers (with derived fields)
     131              : typedef void (*output_dispatch_func)(const char* run_dir, const char* prefix, int step,
     132              :                                      double current_time, const flow_field* field,
     133              :                                      const derived_fields* derived, const grid* grid,
     134              :                                      const ns_solver_params_t* params, const ns_solver_stats_t* stats);
     135              : 
     136              : // VTK output wrappers
     137            1 : static void dispatch_vtk_velocity_magnitude(const char* run_dir, const char* prefix, int step,
     138              :                                             double current_time, const flow_field* field,
     139              :                                             const derived_fields* derived, const grid* grid,
     140              :                                             const ns_solver_params_t* params,
     141              :                                             const ns_solver_stats_t* stats) {
     142            1 :     (void)current_time;
     143            1 :     (void)field;
     144            1 :     (void)params;
     145            1 :     (void)stats;
     146              :     // Use pre-computed velocity magnitude from derived fields
     147            1 :     if (derived && derived->velocity_magnitude) {
     148            1 :         vtk_write_scalar_field(run_dir, prefix, step, "velocity_magnitude",
     149              :                                derived->velocity_magnitude, grid);
     150              :     }
     151            1 : }
     152              : 
     153            1 : static void dispatch_vtk_velocity(const char* run_dir, const char* prefix, int step,
     154              :                                   double current_time, const flow_field* field,
     155              :                                   const derived_fields* derived, const grid* grid,
     156              :                                   const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     157            1 :     (void)current_time;
     158            1 :     (void)derived;
     159            1 :     (void)params;
     160            1 :     (void)stats;
     161            1 :     vtk_dispatch_output(VTK_OUTPUT_VELOCITY, run_dir, prefix, step, field, grid);
     162            1 : }
     163              : 
     164            0 : static void dispatch_vtk_full_field(const char* run_dir, const char* prefix, int step,
     165              :                                     double current_time, const flow_field* field,
     166              :                                     const derived_fields* derived, const grid* grid,
     167              :                                     const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     168            0 :     (void)current_time;
     169            0 :     (void)derived;
     170            0 :     (void)params;
     171            0 :     (void)stats;
     172            0 :     vtk_dispatch_output(VTK_OUTPUT_FULL_FIELD, run_dir, prefix, step, field, grid);
     173            0 : }
     174              : 
     175              : // CSV output wrappers (pass derived fields)
     176            1 : static void dispatch_csv_timeseries(const char* run_dir, const char* prefix, int step,
     177              :                                     double current_time, const flow_field* field,
     178              :                                     const derived_fields* derived, const grid* grid,
     179              :                                     const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     180            1 :     csv_dispatch_output(CSV_OUTPUT_TIMESERIES, run_dir, prefix, step, current_time, field, derived,
     181              :                         grid, params, stats);
     182            1 : }
     183              : 
     184            0 : static void dispatch_csv_centerline(const char* run_dir, const char* prefix, int step,
     185              :                                     double current_time, const flow_field* field,
     186              :                                     const derived_fields* derived, const grid* grid,
     187              :                                     const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     188            0 :     csv_dispatch_output(CSV_OUTPUT_CENTERLINE, run_dir, prefix, step, current_time, field, derived,
     189              :                         grid, params, stats);
     190            0 : }
     191              : 
     192            1 : static void dispatch_csv_statistics(const char* run_dir, const char* prefix, int step,
     193              :                                     double current_time, const flow_field* field,
     194              :                                     const derived_fields* derived, const grid* grid,
     195              :                                     const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     196            1 :     csv_dispatch_output(CSV_OUTPUT_STATISTICS, run_dir, prefix, step, current_time, field, derived,
     197              :                         grid, params, stats);
     198            1 : }
     199              : 
     200              : // Output dispatch table - indexed by output_field_type
     201              : // This provides O(1) lookup with no branch prediction issues
     202              : static const output_dispatch_func output_dispatch_table[] = {
     203              :     dispatch_vtk_velocity_magnitude,  // OUTPUT_VELOCITY_MAGNITUDE = 0
     204              :     dispatch_vtk_velocity,            // OUTPUT_VELOCITY = 1
     205              :     dispatch_vtk_full_field,          // OUTPUT_FULL_FIELD = 2
     206              :     dispatch_csv_timeseries,          // OUTPUT_CSV_TIMESERIES = 3
     207              :     dispatch_csv_centerline,          // OUTPUT_CSV_CENTERLINE = 4
     208              :     dispatch_csv_statistics           // OUTPUT_CSV_STATISTICS = 5
     209              : };
     210              : 
     211              : #define OUTPUT_DISPATCH_TABLE_SIZE \
     212              :     (sizeof(output_dispatch_table) / sizeof(output_dispatch_table[0]))
     213              : 
     214              : //=============================================================================
     215              : // OUTPUT WRITING
     216              : //=============================================================================
     217              : 
     218            9 : void output_registry_write_outputs(output_registry* reg, const char* run_dir, int step,
     219              :                                    double current_time, const flow_field* field,
     220              :                                    const derived_fields* derived, const grid* grid,
     221              :                                    const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     222            9 :     if (!reg || !run_dir) {
     223              :         return;
     224              :     }
     225              : 
     226              :     // Process each registered output
     227           17 :     for (int i = 0; i < reg->count; i++) {
     228            8 :         output_config* config = &reg->configs[i];
     229              : 
     230              :         // Skip if not time to output
     231            8 :         if (config->interval <= 0 || step % config->interval != 0) {
     232            4 :             continue;
     233              :         }
     234              : 
     235              :         // Bounds check and dispatch via function table
     236              :         // This is cacheable and avoids branch prediction issues
     237            4 :         if (config->field_type < OUTPUT_DISPATCH_TABLE_SIZE) {
     238            4 :             output_dispatch_table[config->field_type](run_dir, config->prefix, step, current_time,
     239              :                                                       field, derived, grid, params, stats);
     240              :         } else {
     241            0 :             cfd_warning("Unknown output type, skipping");
     242              :         }
     243              :     }
     244              : }
        

Generated by: LCOV version 2.0-1