LCOV - code coverage report
Current view: top level - io - csv_output.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 87.7 % 138 121
Test Date: 2026-10-05 04:47:18 Functions: 87.5 % 8 7

            Line data    Source code
       1              : #include "cfd/io/csv_output.h"
       2              : #include "cfd/core/derived_fields.h"
       3              : #include "cfd/core/grid.h"
       4              : #include "cfd/core/indexing.h"
       5              : #include "cfd/core/logging.h"
       6              : #include "cfd/solvers/navier_stokes_solver.h"
       7              : #include "csv_output_internal.h"
       8              : 
       9              : 
      10              : #include <math.h>
      11              : #include <stdio.h>
      12              : #include <string.h>
      13              : #include <sys/stat.h>
      14              : 
      15              : //=============================================================================
      16              : // UTILITY FUNCTIONS
      17              : //=============================================================================
      18              : 
      19              : // Helper to build filepath
      20            2 : static void build_filepath(char* filepath, size_t filepath_size, const char* run_dir,
      21              :                            const char* filename) {
      22              : #ifdef _WIN32
      23              :     snprintf(filepath, filepath_size, "%s\\%s", run_dir, filename);
      24              : #else
      25            2 :     snprintf(filepath, filepath_size, "%s/%s", run_dir, filename);
      26              : #endif
      27            2 : }
      28              : 
      29              : // Check if file exists
      30            5 : static int file_exists(const char* filename) {
      31            5 :     struct stat buffer;
      32            5 :     return (stat(filename, &buffer) == 0);
      33              : }
      34              : 
      35              : //=============================================================================
      36              : // CSV OUTPUT DISPATCH
      37              : //=============================================================================
      38              : 
      39              : // Function pointer type for CSV output handlers
      40              : typedef void (*csv_output_handler)(const char* run_dir, const char* prefix, int step,
      41              :                                    double current_time, const flow_field* field,
      42              :                                    const derived_fields* derived, const grid* grid,
      43              :                                    const ns_solver_params_t* params, const ns_solver_stats_t* stats);
      44              : 
      45              : // CSV output handler functions
      46            1 : static void write_timeseries_csv(const char* run_dir, const char* prefix, int step,
      47              :                                  double current_time, const flow_field* field,
      48              :                                  const derived_fields* derived, const grid* grid,
      49              :                                  const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
      50            1 :     const char* name = prefix ? prefix : "timeseries";
      51            1 :     char filename[256], filepath[512];
      52              : 
      53            1 :     snprintf(filename, sizeof(filename), "%s.csv", name);
      54              :     build_filepath(filepath, sizeof(filepath), run_dir, filename);
      55              : 
      56            1 :     int create_new = (step == 0);
      57            1 :     write_csv_timeseries(filepath, step, current_time, field, derived, params, stats, grid->nx,
      58            1 :                          grid->ny, create_new);
      59            1 : }
      60              : 
      61            0 : static void write_centerline_csv(const char* run_dir, const char* prefix, int step,
      62              :                                  double current_time, const flow_field* field,
      63              :                                  const derived_fields* derived, const grid* grid,
      64              :                                  const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
      65            0 :     (void)current_time;
      66            0 :     (void)params;
      67            0 :     (void)stats;  // Unused for centerline
      68            0 :     const char* name = prefix ? prefix : "centerline";
      69            0 :     char filename[256], filepath[512];
      70              : 
      71            0 :     snprintf(filename, sizeof(filename), "%s_%03d.csv", name, step);
      72            0 :     build_filepath(filepath, sizeof(filepath), run_dir, filename);
      73              : 
      74            0 :     write_csv_centerline(filepath, field, derived, grid->x, grid->y, grid->nx, grid->ny,
      75              :                          PROFILE_HORIZONTAL);
      76            0 : }
      77              : 
      78            1 : static void write_statistics_csv(const char* run_dir, const char* prefix, int step,
      79              :                                  double current_time, const flow_field* field,
      80              :                                  const derived_fields* derived, const grid* grid,
      81              :                                  const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
      82            1 :     (void)params;
      83            1 :     (void)stats;  // Unused for statistics
      84            1 :     const char* name = prefix ? prefix : "statistics";
      85            1 :     char filename[256], filepath[512];
      86              : 
      87            1 :     snprintf(filename, sizeof(filename), "%s.csv", name);
      88            1 :     build_filepath(filepath, sizeof(filepath), run_dir, filename);
      89              : 
      90            1 :     int create_new = (step == 0);
      91            1 :     write_csv_statistics(filepath, step, current_time, field, derived, grid->nx, grid->ny,
      92              :                          create_new);
      93            1 : }
      94              : 
      95              : // CSV output handler table - indexed by csv_output_type
      96              : static const csv_output_handler csv_output_table[] = {
      97              :     write_timeseries_csv,  // CSV_OUTPUT_TIMESERIES = 0
      98              :     write_centerline_csv,  // CSV_OUTPUT_CENTERLINE = 1
      99              :     write_statistics_csv   // CSV_OUTPUT_STATISTICS = 2
     100              : };
     101              : 
     102              : #define CSV_OUTPUT_TABLE_SIZE (sizeof(csv_output_table) / sizeof(csv_output_table[0]))
     103              : 
     104            2 : void csv_dispatch_output(csv_output_type csv_type, const char* run_dir, const char* prefix,
     105              :                          int step, double current_time, const flow_field* field,
     106              :                          const derived_fields* derived, const grid* grid,
     107              :                          const ns_solver_params_t* params, const ns_solver_stats_t* stats) {
     108              :     // Bounds check and dispatch via function table
     109            2 :     if (csv_type < CSV_OUTPUT_TABLE_SIZE) {
     110            2 :         csv_output_table[csv_type](run_dir, prefix, step, current_time, field, derived, grid,
     111              :                                    params, stats);
     112              :     } else {
     113            0 :         cfd_warning("Unknown CSV output type");
     114              :     }
     115            2 : }
     116              : 
     117              : //=============================================================================
     118              : // CSV TIMESERIES OUTPUT
     119              : //=============================================================================
     120              : 
     121           13 : void write_csv_timeseries(const char* filename, int step, double time, const flow_field* field,
     122              :                           const derived_fields* derived, const ns_solver_params_t* params,
     123              :                           const ns_solver_stats_t* stats, size_t nx, size_t ny, int create_new) {
     124           13 :     (void)field;  // Statistics come from derived
     125           13 :     (void)nx;
     126           13 :     (void)ny;
     127              : 
     128           13 :     if (!filename || !derived || !derived->stats_computed || !params || !stats) {
     129              :         return;
     130              :     }
     131              : 
     132           10 :     int write_header = create_new || !file_exists(filename);
     133            8 :     const char* mode = write_header ? "w" : "a";
     134            8 :     int has_vel_mag = derived->velocity_magnitude != NULL;
     135              : 
     136            8 :     FILE* fp = fopen(filename, mode);
     137            8 :     if (!fp) {
     138            0 :         cfd_warning("Failed to open CSV timeseries file for writing");
     139            0 :         return;
     140              :     }
     141              : 
     142              :     // Write header if new file
     143            8 :     if (write_header) {
     144            6 :         fprintf(fp, "step,time,dt,max_u,max_v,max_w,max_p,avg_u,avg_v,avg_w,avg_p");
     145            6 :         if (has_vel_mag) {
     146            6 :             fprintf(fp, ",max_vel_mag,avg_vel_mag");
     147              :         }
     148            6 :         fprintf(fp, ",iterations,residual,elapsed_ms\n");
     149              :     }
     150              : 
     151              :     /* The time column accumulates stats->dt_used -- the step the solver really
     152              :      * took -- so this column has to report that same step, or a row contradicts
     153              :      * itself (time=1e-4 next to dt=5e-3 under the Euler solvers' own cap).
     154              :      * Direct callers that leave dt_used at its ns_solver_stats_default() zero
     155              :      * still get params->dt. */
     156            8 :     double dt_reported = (stats->dt_used > 0.0) ? stats->dt_used : params->dt;
     157              : 
     158              :     // Write data row using pre-computed statistics
     159           16 :     fprintf(fp, "%d,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e", step, time, dt_reported,
     160            8 :             derived->u_stats.max_val, derived->v_stats.max_val, derived->w_stats.max_val,
     161            8 :             derived->p_stats.max_val, derived->u_stats.avg_val, derived->v_stats.avg_val,
     162            8 :             derived->w_stats.avg_val, derived->p_stats.avg_val);
     163              : 
     164            8 :     if (has_vel_mag) {
     165            8 :         fprintf(fp, ",%.6e,%.6e", derived->vel_mag_stats.max_val, derived->vel_mag_stats.avg_val);
     166              :     }
     167              : 
     168            8 :     fprintf(fp, ",%d,%.6e,%.2f\n", stats->iterations, stats->residual, stats->elapsed_time_ms);
     169              : 
     170            8 :     fclose(fp);
     171              : }
     172              : 
     173              : //=============================================================================
     174              : // CSV CENTERLINE PROFILE OUTPUT
     175              : //=============================================================================
     176              : 
     177            6 : void write_csv_centerline(const char* filename, const flow_field* field,
     178              :                           const derived_fields* derived, const double* x_coords,
     179              :                           const double* y_coords, size_t nx, size_t ny,
     180              :                           profile_direction direction) {
     181            6 :     if (!filename || !field || !x_coords || !y_coords) {
     182              :         return;
     183              :     }
     184              : 
     185            2 :     int has_vel_mag = derived && derived->velocity_magnitude;
     186              : 
     187            2 :     FILE* fp = fopen(filename, "w");
     188            2 :     if (!fp) {
     189            0 :         cfd_warning("Failed to open CSV centerline file for writing");
     190            0 :         return;
     191              :     }
     192              : 
     193            2 :     if (direction == PROFILE_HORIZONTAL) {
     194              :         // Horizontal centerline: along x at y = ny/2
     195            1 :         size_t j_mid = ny / 2;
     196              : 
     197            1 :         fprintf(fp, "x,u,v,w,p,rho,T,turb_k,turb_eps,nu_t");
     198            1 :         if (has_vel_mag) {
     199            1 :             fprintf(fp, ",vel_mag");
     200              :         }
     201            1 :         fprintf(fp, "\n");
     202              : 
     203           11 :         for (size_t i = 0; i < nx; i++) {
     204           10 :             size_t idx = IDX_2D(i, j_mid, nx);
     205           10 :             double w_val = field->w ? field->w[idx] : 0.0;
     206           20 :             fprintf(fp, "%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e", x_coords[i], field->u[idx],
     207           10 :                     field->v[idx], w_val, field->p[idx], field->rho[idx], field->T[idx]);
     208           70 :             fprintf(fp, ",%.6e,%.6e,%.6e",
     209           10 :                     field->turb_k ? field->turb_k[idx] : 0.0,
     210           10 :                     field->turb_eps ? field->turb_eps[idx] : 0.0,
     211           10 :                     field->nu_t ? field->nu_t[idx] : 0.0);
     212           10 :             if (has_vel_mag) {
     213           10 :                 fprintf(fp, ",%.6e", derived->velocity_magnitude[idx]);
     214              :             }
     215           10 :             fprintf(fp, "\n");
     216              :         }
     217              :     } else {
     218              :         // Vertical centerline: along y at x = nx/2
     219            1 :         size_t i_mid = nx / 2;
     220              : 
     221            1 :         fprintf(fp, "y,u,v,w,p,rho,T,turb_k,turb_eps,nu_t");
     222            1 :         if (has_vel_mag) {
     223            1 :             fprintf(fp, ",vel_mag");
     224              :         }
     225            1 :         fprintf(fp, "\n");
     226              : 
     227           11 :         for (size_t j = 0; j < ny; j++) {
     228           10 :             size_t idx = IDX_2D(i_mid, j, nx);
     229           10 :             double w_val = field->w ? field->w[idx] : 0.0;
     230           20 :             fprintf(fp, "%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e", y_coords[j], field->u[idx],
     231           10 :                     field->v[idx], w_val, field->p[idx], field->rho[idx], field->T[idx]);
     232           70 :             fprintf(fp, ",%.6e,%.6e,%.6e",
     233           10 :                     field->turb_k ? field->turb_k[idx] : 0.0,
     234           10 :                     field->turb_eps ? field->turb_eps[idx] : 0.0,
     235           10 :                     field->nu_t ? field->nu_t[idx] : 0.0);
     236           10 :             if (has_vel_mag) {
     237           10 :                 fprintf(fp, ",%.6e", derived->velocity_magnitude[idx]);
     238              :             }
     239           10 :             fprintf(fp, "\n");
     240              :         }
     241              :     }
     242              : 
     243            2 :     fclose(fp);
     244              : }
     245              : 
     246              : //=============================================================================
     247              : // CSV STATISTICS OUTPUT
     248              : //=============================================================================
     249              : 
     250           10 : void write_csv_statistics(const char* filename, int step, double time, const flow_field* field,
     251              :                           const derived_fields* derived, size_t nx, size_t ny, int create_new) {
     252           10 :     (void)field;  // Statistics come from derived
     253           10 :     (void)nx;
     254           10 :     (void)ny;
     255              : 
     256           10 :     if (!filename || !derived || !derived->stats_computed) {
     257              :         return;
     258              :     }
     259              : 
     260           10 :     int write_header = create_new || !file_exists(filename);
     261            7 :     const char* mode = write_header ? "w" : "a";
     262            7 :     int has_vel_mag = derived->velocity_magnitude != NULL;
     263              : 
     264            7 :     FILE* fp = fopen(filename, mode);
     265            7 :     if (!fp) {
     266            0 :         cfd_warning("Failed to open CSV statistics file for writing");
     267            0 :         return;
     268              :     }
     269              : 
     270              :     // Write header if new file
     271            7 :     if (write_header) {
     272            4 :         fprintf(fp, "step,time,min_u,max_u,avg_u,min_v,max_v,avg_v,min_w,max_w,avg_w,"
     273              :                     "min_p,max_p,avg_p,min_rho,max_rho,avg_rho,min_T,max_T,avg_T");
     274            4 :         if (has_vel_mag) {
     275            4 :             fprintf(fp, ",min_vel_mag,max_vel_mag,avg_vel_mag");
     276              :         }
     277            4 :         fprintf(fp, "\n");
     278              :     }
     279              : 
     280              :     // Write data row using pre-computed statistics
     281           14 :     fprintf(fp,
     282              :             "%d,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,"
     283              :             "%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e,%.6e",
     284            7 :             step, time, derived->u_stats.min_val, derived->u_stats.max_val,
     285            7 :             derived->u_stats.avg_val, derived->v_stats.min_val, derived->v_stats.max_val,
     286            7 :             derived->v_stats.avg_val, derived->w_stats.min_val, derived->w_stats.max_val,
     287            7 :             derived->w_stats.avg_val, derived->p_stats.min_val, derived->p_stats.max_val,
     288            7 :             derived->p_stats.avg_val, derived->rho_stats.min_val, derived->rho_stats.max_val,
     289            7 :             derived->rho_stats.avg_val, derived->T_stats.min_val, derived->T_stats.max_val,
     290            7 :             derived->T_stats.avg_val);
     291              : 
     292            7 :     if (has_vel_mag) {
     293            7 :         fprintf(fp, ",%.6e,%.6e,%.6e", derived->vel_mag_stats.min_val,
     294            7 :                 derived->vel_mag_stats.max_val, derived->vel_mag_stats.avg_val);
     295              :     }
     296              : 
     297            7 :     fprintf(fp, "\n");
     298              : 
     299            7 :     fclose(fp);
     300              : }
        

Generated by: LCOV version 2.0-1