LCOV - code coverage report
Current view: top level - solvers/navier_stokes/avx2 - solver_rk4_avx2.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 18.4 % 38 7
Test Date: 2026-10-05 04:47:18 Functions: 50.0 % 4 2

            Line data    Source code
       1              : /**
       2              :  * @file solver_rk4_avx2.c
       3              :  * @brief RK4 (classical Runge-Kutta) - AVX2 + OpenMP implementation
       4              :  *
       5              :  * AVX2-only backend. Returns CFD_ERROR_UNSUPPORTED if AVX2 is unavailable.
       6              :  * Uses persistent aligned buffers in context to avoid per-step allocation.
       7              :  *
       8              :  * SIMD strategy per row (fixed j, k):
       9              :  *   i = 1       : scalar (periodic il wrap)
      10              :  *   i = 2..nx-4 : AVX2 vectorized (interior, no wrapping)
      11              :  *   i = nx-2    : scalar (periodic ir wrap)
      12              :  *   Remainder between AVX2 end and nx-2: scalar (no wrapping)
      13              :  *
      14              :  * Algorithm: identical to scalar/OMP RK4.
      15              :  *   k1 = RHS(Q^n)
      16              :  *   k2 = RHS(Q^n + (dt/2)*k1)
      17              :  *   k3 = RHS(Q^n + (dt/2)*k2)
      18              :  *   k4 = RHS(Q^n + dt*k3)
      19              :  *   Q^{n+1} = Q^n + (dt/6)*(k1 + 2*k2 + 2*k3 + k4)
      20              :  *   BCs applied to final state only.
      21              :  *
      22              :  * Branch-free 3D: when nz==1, stride_z=0 and inv_2dz/inv_dz2=0.0 cause all
      23              :  * z-terms to vanish, producing bit-identical results to the 2D code path.
      24              :  */
      25              : 
      26              : #define _POSIX_C_SOURCE 200809L
      27              : #define _ISOC11_SOURCE
      28              : #define _USE_MATH_DEFINES
      29              : 
      30              : #include "cfd/core/cfd_status.h"
      31              : #include "cfd/core/grid.h"
      32              : #include "cfd/core/indexing.h"
      33              : #include "cfd/core/logging.h"
      34              : #include "cfd/core/memory.h"
      35              : #include "cfd/solvers/navier_stokes_solver.h"
      36              : #include "cfd/solvers/energy_solver.h"
      37              : #include "cfd/solvers/turbulence_solver.h"
      38              : #include "../../energy/energy_solver_internal.h"
      39              : #include "../../turbulence/turbulence_solver_internal.h"
      40              : 
      41              : #include <math.h>
      42              : #include <stddef.h>
      43              : #include <stdio.h>
      44              : #include <string.h>
      45              : 
      46              : #ifndef M_PI
      47              : #define M_PI 3.14159265358979323846
      48              : #endif
      49              : 
      50              : #ifdef _OPENMP
      51              : #include <omp.h>
      52              : #endif
      53              : 
      54              : #if defined(CFD_HAS_AVX2)
      55              : #include <immintrin.h>
      56              : #define USE_AVX2 1
      57              : #else
      58              : #define USE_AVX2 0
      59              : #endif
      60              : 
      61              : /* Velocity clamp used by the time-step update (not the RHS kernel).
      62              :  * The RHS clamp limits live in ../momentum_rhs/ns_momentum_rhs_avx2.h. */
      63              : #define MAX_VELOCITY_LIMIT          100.0
      64              : 
      65              : /* ============================================================================
      66              :  * CONTEXT
      67              :  * ============================================================================ */
      68              : 
      69              : typedef struct {
      70              :     double* k1_u; double* k1_v; double* k1_w; double* k1_p;
      71              :     double* k2_u; double* k2_v; double* k2_w; double* k2_p;
      72              :     double* k3_u; double* k3_v; double* k3_w; double* k3_p;
      73              :     double* k4_u; double* k4_v; double* k4_w; double* k4_p;
      74              :     double* u0;   double* v0;   double* w0;   double* p0;
      75              :     double* T_ws;    /* Reusable scratch for the energy step (avoids per-step alloc) */
      76              :     double* turb_ws; /* Reusable scratch for the turbulence step; always allocated */
      77              :     double* dx_inv;  /* 1/(2*dx[i]) for i = 0..nx-1 */
      78              :     double* dy_inv;  /* 1/(2*dy[j]) for j = 0..ny-1 */
      79              :     size_t nx, ny, nz;
      80              :     size_t stride_z;
      81              :     size_t k_start, k_end;
      82              :     double inv_2dz, inv_dz2;
      83              :     int initialized;
      84              : } rk4_avx2_context_t;
      85              : 
      86              : /* Shared AVX2 momentum RHS kernel (also #include-d by solver_rk2_avx2.c).
      87              :  * RHS_CTX_T selects this solver's context type for the vectorized kernels;
      88              :  * USE_AVX2 and <immintrin.h> are already set above. Provides ns_rhs_point,
      89              :  * avx2_clamp, compute_rhs_row, compute_rhs_avx2 and the shared RHS clamp
      90              :  * constants. */
      91              : #define RHS_CTX_T rk4_avx2_context_t
      92              : #include "../momentum_rhs/ns_momentum_rhs_avx2.h"
      93              : #include "../ns_simd_backend_internal.h"
      94              : #undef RHS_CTX_T
      95              : 
      96              : /* ============================================================================
      97              :  * PUBLIC API DECLARATIONS
      98              :  * ============================================================================ */
      99              : 
     100              : cfd_status_t rk4_avx2_init(ns_solver_t* solver, const grid* g,
     101              :                              const ns_solver_params_t* params);
     102              : void         rk4_avx2_destroy(ns_solver_t* solver);
     103              : cfd_status_t rk4_avx2_step(ns_solver_t* solver, flow_field* field, const grid* g,
     104              :                              const ns_solver_params_t* params, ns_solver_stats_t* stats);
     105              : 
     106              : 
     107              : /* ============================================================================
     108              :  * AVX2 STAGE UPDATE + MAIN LOOP  (compiled only when AVX2 is available;
     109              :  * the vectorized RHS kernels live in ../momentum_rhs/ns_momentum_rhs_avx2.h)
     110              :  * ============================================================================ */
     111              : 
     112              : #if USE_AVX2
     113              : 
     114              : /* Intermediate stage update (parallelized): field = Q^n + factor * k_stage,
     115              :  * with velocity clamping. */
     116              : static void apply_stage_update_avx2(
     117              :     flow_field* field, const rk4_avx2_context_t* ctx,
     118              :     const double* ku, const double* kv, const double* kw, const double* kp,
     119              :     double factor, ptrdiff_t n_int)
     120              : {
     121              :     ptrdiff_t kk;
     122              : #ifdef _OPENMP
     123              :     #pragma omp parallel for schedule(static)
     124              : #endif
     125              :     for (kk = 0; kk < n_int; kk++) {
     126              :         field->u[kk] = fmax(-MAX_VELOCITY_LIMIT,
     127              :                       fmin( MAX_VELOCITY_LIMIT, ctx->u0[kk] + factor * ku[kk]));
     128              :         field->v[kk] = fmax(-MAX_VELOCITY_LIMIT,
     129              :                       fmin( MAX_VELOCITY_LIMIT, ctx->v0[kk] + factor * kv[kk]));
     130              :         field->w[kk] = fmax(-MAX_VELOCITY_LIMIT,
     131              :                       fmin( MAX_VELOCITY_LIMIT, ctx->w0[kk] + factor * kw[kk]));
     132              :         field->p[kk] = ctx->p0[kk] + factor * kp[kk];
     133              :     }
     134              : }
     135              : 
     136              : /* ============================================================================
     137              :  * RK4 AVX2 MAIN LOOP
     138              :  * ============================================================================ */
     139              : 
     140              : static cfd_status_t rk4_avx2_impl(flow_field* field, rk4_avx2_context_t* ctx,
     141              :                                     const grid* g, const ns_solver_params_t* params)
     142              : {
     143              :     size_t n     = ctx->nx * ctx->ny * ctx->nz;
     144              :     size_t bytes = n * sizeof(double);
     145              :     double dt    = params->dt;
     146              :     ptrdiff_t n_int = (ptrdiff_t)n;
     147              :     cfd_status_t status = CFD_SUCCESS;
     148              : 
     149              :     for (int iter = 0; iter < params->max_iter; iter++) {
     150              :         /* Save Q^n */
     151              :         memcpy(ctx->u0, field->u, bytes);
     152              :         memcpy(ctx->v0, field->v, bytes);
     153              :         memcpy(ctx->w0, field->w, bytes);
     154              :         memcpy(ctx->p0, field->p, bytes);
     155              : 
     156              :         /* ---- Stage 1: k1 = RHS(Q^n) ---- */
     157              :         memset(ctx->k1_u, 0, bytes);
     158              :         memset(ctx->k1_v, 0, bytes);
     159              :         memset(ctx->k1_w, 0, bytes);
     160              :         memset(ctx->k1_p, 0, bytes);
     161              :         compute_rhs_avx2(field->u, field->v, field->w, field->p, field->rho, field->T,
     162              :                          field->nu_t,
     163              :                          ctx->k1_u, ctx->k1_v, ctx->k1_w, ctx->k1_p,
     164              :                          ctx, g, params, iter, dt);
     165              : 
     166              :         /* ---- Stage 2: k2 = RHS(Q^n + (dt/2)*k1) ---- */
     167              :         apply_stage_update_avx2(field, ctx, ctx->k1_u, ctx->k1_v, ctx->k1_w, ctx->k1_p,
     168              :                                 0.5 * dt, n_int);
     169              :         memset(ctx->k2_u, 0, bytes);
     170              :         memset(ctx->k2_v, 0, bytes);
     171              :         memset(ctx->k2_w, 0, bytes);
     172              :         memset(ctx->k2_p, 0, bytes);
     173              :         compute_rhs_avx2(field->u, field->v, field->w, field->p, field->rho, field->T,
     174              :                          field->nu_t,
     175              :                          ctx->k2_u, ctx->k2_v, ctx->k2_w, ctx->k2_p,
     176              :                          ctx, g, params, iter, dt);
     177              : 
     178              :         /* ---- Stage 3: k3 = RHS(Q^n + (dt/2)*k2) ---- */
     179              :         apply_stage_update_avx2(field, ctx, ctx->k2_u, ctx->k2_v, ctx->k2_w, ctx->k2_p,
     180              :                                 0.5 * dt, n_int);
     181              :         memset(ctx->k3_u, 0, bytes);
     182              :         memset(ctx->k3_v, 0, bytes);
     183              :         memset(ctx->k3_w, 0, bytes);
     184              :         memset(ctx->k3_p, 0, bytes);
     185              :         compute_rhs_avx2(field->u, field->v, field->w, field->p, field->rho, field->T,
     186              :                          field->nu_t,
     187              :                          ctx->k3_u, ctx->k3_v, ctx->k3_w, ctx->k3_p,
     188              :                          ctx, g, params, iter, dt);
     189              : 
     190              :         /* ---- Stage 4: k4 = RHS(Q^n + dt*k3) ---- */
     191              :         apply_stage_update_avx2(field, ctx, ctx->k3_u, ctx->k3_v, ctx->k3_w, ctx->k3_p,
     192              :                                 dt, n_int);
     193              :         memset(ctx->k4_u, 0, bytes);
     194              :         memset(ctx->k4_v, 0, bytes);
     195              :         memset(ctx->k4_w, 0, bytes);
     196              :         memset(ctx->k4_p, 0, bytes);
     197              :         compute_rhs_avx2(field->u, field->v, field->w, field->p, field->rho, field->T,
     198              :                          field->nu_t,
     199              :                          ctx->k4_u, ctx->k4_v, ctx->k4_w, ctx->k4_p,
     200              :                          ctx, g, params, iter, dt);
     201              : 
     202              :         /* NOTE: Do NOT apply BCs between RK stages.
     203              :          * The ghost cells carry zero-derivative evolution (k[ghost]=0),
     204              :          * consistent with the semi-discrete ODE. Applying BCs here would
     205              :          * reduce RK4 to first-order temporal accuracy. */
     206              : 
     207              :         /* ---- Final update: Q^{n+1} = Q^n + (dt/6)*(k1 + 2*k2 + 2*k3 + k4) ---- */
     208              :         {
     209              :             double sixth_dt = dt / 6.0;
     210              :             ptrdiff_t kk;
     211              : #ifdef _OPENMP
     212              :             #pragma omp parallel for schedule(static)
     213              : #endif
     214              :             for (kk = 0; kk < n_int; kk++) {
     215              :                 field->u[kk] = fmax(-MAX_VELOCITY_LIMIT,
     216              :                               fmin( MAX_VELOCITY_LIMIT,
     217              :                                     ctx->u0[kk] + sixth_dt * (ctx->k1_u[kk] + 2.0 * ctx->k2_u[kk] + 2.0 * ctx->k3_u[kk] + ctx->k4_u[kk])));
     218              :                 field->v[kk] = fmax(-MAX_VELOCITY_LIMIT,
     219              :                               fmin( MAX_VELOCITY_LIMIT,
     220              :                                     ctx->v0[kk] + sixth_dt * (ctx->k1_v[kk] + 2.0 * ctx->k2_v[kk] + 2.0 * ctx->k3_v[kk] + ctx->k4_v[kk])));
     221              :                 field->w[kk] = fmax(-MAX_VELOCITY_LIMIT,
     222              :                               fmin( MAX_VELOCITY_LIMIT,
     223              :                                     ctx->w0[kk] + sixth_dt * (ctx->k1_w[kk] + 2.0 * ctx->k2_w[kk] + 2.0 * ctx->k3_w[kk] + ctx->k4_w[kk])));
     224              :                 field->p[kk] = ctx->p0[kk] + sixth_dt * (ctx->k1_p[kk] + 2.0 * ctx->k2_p[kk] + 2.0 * ctx->k3_p[kk] + ctx->k4_p[kk]);
     225              :             }
     226              :         }
     227              : 
     228              :         /* Energy equation: advance temperature after the full RK4 step */
     229              :         {
     230              :             cfd_status_t energy_status = energy_step_explicit_avx2_with_workspace(
     231              :                 field, g, params, dt, iter * dt, ctx->T_ws, n);
     232              :             if (energy_status != CFD_SUCCESS) {
     233              :                 status = energy_status;
     234              :                 goto cleanup;
     235              :             }
     236              :         }
     237              : 
     238              :         /* Apply BCs to final state only, then configured thermal BCs */
     239              :         apply_boundary_conditions(field, g);
     240              :         status = energy_apply_thermal_bcs(field, params);
     241              :         if (status != CFD_SUCCESS) {
     242              :             goto cleanup;
     243              :         }
     244              : 
     245              :         /* Turbulence transport: advance k-eps/SA with the updated velocity,
     246              :          * then apply turbulence BCs. nu_t is frozen across RK stages and
     247              :          * updated once per full step. */
     248              :         {
     249              :             const int turb_on = (params->turb_model != TURB_MODEL_NONE);
     250              :             cfd_status_t turb_status = turbulence_step_explicit_avx2_with_workspace(
     251              :                 field, g, params, dt, iter * dt, ctx->turb_ws,
     252              :                 turb_on ? TURB_WORKSPACE_SIZE(n) : 0);
     253              :             if (turb_status == CFD_SUCCESS) {
     254              :                 turb_status = turbulence_apply_bcs(field, g, params);
     255              :             }
     256              :             if (turb_status != CFD_SUCCESS) {
     257              :                 status = turb_status;
     258              :                 goto cleanup;
     259              :             }
     260              :         }
     261              : 
     262              :         /* NaN / Inf check (parallelized) */
     263              :         {
     264              :             int has_nan = 0;
     265              :             ptrdiff_t kk;
     266              : #ifdef _OPENMP
     267              :             #pragma omp parallel for reduction(|:has_nan) schedule(static)
     268              : #endif
     269              :             for (kk = 0; kk < n_int; kk++) {
     270              :                 if (!isfinite(field->u[kk]) || !isfinite(field->v[kk]) ||
     271              :                     !isfinite(field->w[kk]) || !isfinite(field->p[kk])) {
     272              :                     has_nan = 1;
     273              :                 }
     274              :             }
     275              :             if (has_nan) {
     276              :                 status = CFD_ERROR_DIVERGED;
     277              :                 goto cleanup;
     278              :             }
     279              :         }
     280              :     }
     281              : 
     282              : cleanup:
     283              :     return status;
     284              : }
     285              : 
     286              : #endif /* USE_AVX2 */
     287              : 
     288              : /* ============================================================================
     289              :  * PUBLIC API IMPLEMENTATIONS
     290              :  * ============================================================================ */
     291              : 
     292           17 : cfd_status_t rk4_avx2_init(ns_solver_t* solver, const grid* g,
     293              :                              const ns_solver_params_t* params)
     294              : {
     295              :     /* Validate the parameters before reporting a build without AVX2, so an
     296              :      * invalid scheme is reported as such in every build. */
     297           17 :     cfd_status_t scheme_status = ns_check_convection_scheme(params, 1);
     298           17 :     if (scheme_status != CFD_SUCCESS) {
     299              :         return scheme_status;
     300              :     }
     301              : 
     302              :     /* Requires both the compiled-in kernels and a CPU that supports them.
     303              :      * The runtime half matters even in an AVX2 build: without it this init
     304              :      * succeeds on a pre-AVX2 CPU and the kernels then fault. */
     305           16 :     cfd_status_t simd_status = ns_check_simd_backend();
     306           16 :     if (simd_status != CFD_SUCCESS) {
     307              :         return simd_status;
     308              :     }
     309              : 
     310            0 :     cfd_status_t turb_status = ns_check_turbulence_model(params, 1);
     311            0 :     if (turb_status != CFD_SUCCESS) {
     312              :         return turb_status;
     313              :     }
     314              : 
     315            0 :     cfd_status_t pressure_bc_status = ns_check_pressure_bc(params, 0);
     316            0 :     if (pressure_bc_status != CFD_SUCCESS) {
     317              :         return pressure_bc_status;
     318              :     }
     319              : 
     320            0 :     cfd_status_t pressure_solver_status = ns_check_pressure_solver(params, 0);
     321            0 :     if (pressure_solver_status != CFD_SUCCESS) {
     322            0 :         return pressure_solver_status;
     323              :     }
     324              : 
     325              : #if !USE_AVX2
     326              :     /* Unreachable: ns_check_simd_backend() fails in a build without AVX2. */
     327              :     (void)solver;
     328              :     (void)g;
     329              :     return CFD_ERROR_UNSUPPORTED;
     330              : #else
     331              :     if (!solver || !g) {
     332              :         return CFD_ERROR_INVALID;
     333              :     }
     334              :     if (g->nx < 3 || g->ny < 3 || (g->nz > 1 && g->nz < 3)) {
     335              :         return CFD_ERROR_INVALID;
     336              :     }
     337              : 
     338              :     rk4_avx2_context_t* ctx =
     339              :         (rk4_avx2_context_t*)cfd_calloc(1, sizeof(rk4_avx2_context_t));
     340              :     if (!ctx) {
     341              :         return CFD_ERROR_NOMEM;
     342              :     }
     343              : 
     344              :     ctx->nx = g->nx;
     345              :     ctx->ny = g->ny;
     346              :     ctx->nz = g->nz;
     347              :     size_t bytes = ctx->nx * ctx->ny * g->nz * sizeof(double);
     348              : 
     349              :     /* Reject non-uniform z-spacing (solver uses constant inv_2dz/inv_dz2) */
     350              :     if (g->nz > 1 && g->dz) {
     351              :         for (size_t kk = 1; kk < g->nz - 1; kk++) {
     352              :             if (fabs(g->dz[kk] - g->dz[0]) > 1e-14) {
     353              :                 cfd_free(ctx);
     354              :                 return CFD_ERROR_INVALID;
     355              :             }
     356              :         }
     357              :     }
     358              : 
     359              :     /* Branch-free 3D constants */
     360              :     size_t plane   = ctx->nx * ctx->ny;
     361              :     ctx->stride_z  = (g->nz > 1) ? plane : 0;
     362              :     ctx->k_start   = (g->nz > 1) ? 1 : 0;
     363              :     ctx->k_end     = (g->nz > 1) ? (g->nz - 1) : 1;
     364              :     ctx->inv_2dz   = (g->nz > 1 && g->dz) ? 1.0 / (2.0 * g->dz[0]) : 0.0;
     365              :     ctx->inv_dz2   = (g->nz > 1 && g->dz) ? 1.0 / (g->dz[0] * g->dz[0]) : 0.0;
     366              : 
     367              :     size_t n_total = ctx->nx * ctx->ny * ctx->nz;
     368              :     ctx->k1_u   = (double*)cfd_aligned_malloc(bytes);
     369              :     ctx->k1_v   = (double*)cfd_aligned_malloc(bytes);
     370              :     ctx->k1_w   = (double*)cfd_aligned_malloc(bytes);
     371              :     ctx->k1_p   = (double*)cfd_aligned_malloc(bytes);
     372              :     ctx->k2_u   = (double*)cfd_aligned_malloc(bytes);
     373              :     ctx->k2_v   = (double*)cfd_aligned_malloc(bytes);
     374              :     ctx->k2_w   = (double*)cfd_aligned_malloc(bytes);
     375              :     ctx->k2_p   = (double*)cfd_aligned_malloc(bytes);
     376              :     ctx->k3_u   = (double*)cfd_aligned_malloc(bytes);
     377              :     ctx->k3_v   = (double*)cfd_aligned_malloc(bytes);
     378              :     ctx->k3_w   = (double*)cfd_aligned_malloc(bytes);
     379              :     ctx->k3_p   = (double*)cfd_aligned_malloc(bytes);
     380              :     ctx->k4_u   = (double*)cfd_aligned_malloc(bytes);
     381              :     ctx->k4_v   = (double*)cfd_aligned_malloc(bytes);
     382              :     ctx->k4_w   = (double*)cfd_aligned_malloc(bytes);
     383              :     ctx->k4_p   = (double*)cfd_aligned_malloc(bytes);
     384              :     ctx->u0     = (double*)cfd_aligned_malloc(bytes);
     385              :     ctx->v0     = (double*)cfd_aligned_malloc(bytes);
     386              :     ctx->w0     = (double*)cfd_aligned_malloc(bytes);
     387              :     ctx->p0     = (double*)cfd_aligned_malloc(bytes);
     388              :     ctx->T_ws   = (double*)cfd_aligned_malloc(bytes);
     389              :     ctx->turb_ws = (double*)cfd_calloc(TURB_WORKSPACE_SIZE(n_total), sizeof(double));
     390              :     ctx->dx_inv = (double*)cfd_aligned_malloc(ctx->nx * sizeof(double));
     391              :     ctx->dy_inv = (double*)cfd_aligned_malloc(ctx->ny * sizeof(double));
     392              : 
     393              :     if (!ctx->k1_u || !ctx->k1_v || !ctx->k1_w || !ctx->k1_p ||
     394              :         !ctx->k2_u || !ctx->k2_v || !ctx->k2_w || !ctx->k2_p ||
     395              :         !ctx->k3_u || !ctx->k3_v || !ctx->k3_w || !ctx->k3_p ||
     396              :         !ctx->k4_u || !ctx->k4_v || !ctx->k4_w || !ctx->k4_p ||
     397              :         !ctx->u0   || !ctx->v0   || !ctx->w0   || !ctx->p0   ||
     398              :         !ctx->T_ws || !ctx->turb_ws || !ctx->dx_inv || !ctx->dy_inv) {
     399              :         cfd_aligned_free(ctx->k1_u); cfd_aligned_free(ctx->k1_v);
     400              :         cfd_aligned_free(ctx->k1_w); cfd_aligned_free(ctx->k1_p);
     401              :         cfd_aligned_free(ctx->k2_u); cfd_aligned_free(ctx->k2_v);
     402              :         cfd_aligned_free(ctx->k2_w); cfd_aligned_free(ctx->k2_p);
     403              :         cfd_aligned_free(ctx->k3_u); cfd_aligned_free(ctx->k3_v);
     404              :         cfd_aligned_free(ctx->k3_w); cfd_aligned_free(ctx->k3_p);
     405              :         cfd_aligned_free(ctx->k4_u); cfd_aligned_free(ctx->k4_v);
     406              :         cfd_aligned_free(ctx->k4_w); cfd_aligned_free(ctx->k4_p);
     407              :         cfd_aligned_free(ctx->u0);   cfd_aligned_free(ctx->v0);
     408              :         cfd_aligned_free(ctx->w0);   cfd_aligned_free(ctx->p0);
     409              :         cfd_aligned_free(ctx->T_ws);
     410              :         cfd_free(ctx->turb_ws);
     411              :         cfd_aligned_free(ctx->dx_inv);
     412              :         cfd_aligned_free(ctx->dy_inv);
     413              :         cfd_free(ctx);
     414              :         return CFD_ERROR_NOMEM;
     415              :     }
     416              : 
     417              :     for (size_t i = 0; i < ctx->nx; i++) {
     418              :         ctx->dx_inv[i] = (g->dx[i] > 1e-10) ? 1.0 / (2.0 * g->dx[i]) : 0.0;
     419              :     }
     420              :     for (size_t j = 0; j < ctx->ny; j++) {
     421              :         ctx->dy_inv[j] = (g->dy[j] > 1e-10) ? 1.0 / (2.0 * g->dy[j]) : 0.0;
     422              :     }
     423              : 
     424              :     ctx->initialized = 1;
     425              :     solver->context  = ctx;
     426              : 
     427              : #ifdef _OPENMP
     428              :     CFD_LOG_INFO("solver", "RK4 SIMD: AVX2 + OpenMP enabled (%d threads)", omp_get_max_threads());
     429              : #else
     430              :     CFD_LOG_INFO("solver", "RK4 SIMD: AVX2 enabled (OpenMP disabled)");
     431              : #endif
     432              : 
     433              :     return CFD_SUCCESS;
     434              : #endif
     435              : }
     436              : 
     437           22 : void rk4_avx2_destroy(ns_solver_t* solver)
     438              : {
     439           22 :     if (!solver || !solver->context) {
     440              :         return;
     441              :     }
     442            0 :     rk4_avx2_context_t* ctx = (rk4_avx2_context_t*)solver->context;
     443            0 :     if (ctx->initialized) {
     444            0 :         cfd_aligned_free(ctx->k1_u); cfd_aligned_free(ctx->k1_v);
     445            0 :         cfd_aligned_free(ctx->k1_w); cfd_aligned_free(ctx->k1_p);
     446            0 :         cfd_aligned_free(ctx->k2_u); cfd_aligned_free(ctx->k2_v);
     447            0 :         cfd_aligned_free(ctx->k2_w); cfd_aligned_free(ctx->k2_p);
     448            0 :         cfd_aligned_free(ctx->k3_u); cfd_aligned_free(ctx->k3_v);
     449            0 :         cfd_aligned_free(ctx->k3_w); cfd_aligned_free(ctx->k3_p);
     450            0 :         cfd_aligned_free(ctx->k4_u); cfd_aligned_free(ctx->k4_v);
     451            0 :         cfd_aligned_free(ctx->k4_w); cfd_aligned_free(ctx->k4_p);
     452            0 :         cfd_aligned_free(ctx->u0);   cfd_aligned_free(ctx->v0);
     453            0 :         cfd_aligned_free(ctx->w0);   cfd_aligned_free(ctx->p0);
     454            0 :         cfd_aligned_free(ctx->T_ws);
     455            0 :         cfd_free(ctx->turb_ws);
     456            0 :         cfd_aligned_free(ctx->dx_inv);
     457            0 :         cfd_aligned_free(ctx->dy_inv);
     458              :     }
     459            0 :     cfd_free(ctx);
     460            0 :     solver->context = NULL;
     461              : }
     462              : 
     463            0 : cfd_status_t rk4_avx2_step(ns_solver_t* solver, flow_field* field, const grid* g,
     464              :                              const ns_solver_params_t* params, ns_solver_stats_t* stats)
     465              : {
     466              : #if !USE_AVX2
     467            0 :     (void)solver; (void)field; (void)g; (void)params; (void)stats;
     468            0 :     return CFD_ERROR_UNSUPPORTED;
     469              : #else
     470              :     if (!solver || !solver->context || !field || !g || !params) {
     471              :         return CFD_ERROR_INVALID;
     472              :     }
     473              :     if (field->nx < 3 || field->ny < 3 || (field->nz > 1 && field->nz < 3)) {
     474              :         return CFD_ERROR_INVALID;
     475              :     }
     476              : 
     477              :     rk4_avx2_context_t* ctx = (rk4_avx2_context_t*)solver->context;
     478              :     if (field->nx != ctx->nx || field->ny != ctx->ny || field->nz != ctx->nz) {
     479              :         return CFD_ERROR_INVALID;
     480              :     }
     481              : 
     482              :     ns_solver_params_t step_params = *params;
     483              :     step_params.max_iter = 1;
     484              : 
     485              :     cfd_status_t status = rk4_avx2_impl(field, ctx, g, &step_params);
     486              : 
     487              :     if (stats) {
     488              :         stats->iterations = 1;
     489              :         double max_vel = 0.0, max_p = 0.0;
     490              :         ptrdiff_t n_s = (ptrdiff_t)(field->nx * field->ny * field->nz);
     491              :         double max_t = (field->T && n_s > 0) ? field->T[0] : 0.0;
     492              :         double max_nt = 0.0;
     493              :         ptrdiff_t ks;
     494              : #if defined(_OPENMP) && (_OPENMP >= 201107)
     495              :         #pragma omp parallel for reduction(max: max_vel, max_p, max_t, max_nt) schedule(static)
     496              : #endif
     497              :         for (ks = 0; ks < n_s; ks++) {
     498              :             double vel = sqrt(field->u[ks] * field->u[ks] +
     499              :                               field->v[ks] * field->v[ks] +
     500              :                               field->w[ks] * field->w[ks]);
     501              :             if (vel > max_vel) max_vel = vel;
     502              :             double ap = fabs(field->p[ks]);
     503              :             if (ap > max_p) max_p = ap;
     504              :             if (field->T && field->T[ks] > max_t) max_t = field->T[ks];
     505              :             if (field->nu_t && field->nu_t[ks] > max_nt) max_nt = field->nu_t[ks];
     506              :         }
     507              :         stats->max_velocity = max_vel;
     508              :         stats->max_pressure = max_p;
     509              :         stats->max_temperature = max_t;
     510              :         stats->max_nu_t = max_nt;
     511              :     }
     512              : 
     513              :     return status;
     514              : #endif
     515              : }
     516              : 
     517            0 : cfd_status_t rk4_avx2_solve(ns_solver_t* solver, flow_field* field, const grid* g,
     518              :                               const ns_solver_params_t* params, ns_solver_stats_t* stats)
     519              : {
     520              : #if !USE_AVX2
     521            0 :     (void)solver; (void)field; (void)g; (void)params; (void)stats;
     522            0 :     return CFD_ERROR_UNSUPPORTED;
     523              : #else
     524              :     if (!solver || !solver->context || !field || !g || !params) {
     525              :         return CFD_ERROR_INVALID;
     526              :     }
     527              :     if (field->nx < 3 || field->ny < 3 || (field->nz > 1 && field->nz < 3)) {
     528              :         return CFD_ERROR_INVALID;
     529              :     }
     530              : 
     531              :     rk4_avx2_context_t* ctx = (rk4_avx2_context_t*)solver->context;
     532              :     if (field->nx != ctx->nx || field->ny != ctx->ny || field->nz != ctx->nz) {
     533              :         return CFD_ERROR_INVALID;
     534              :     }
     535              : 
     536              :     /* Call impl directly so its internal loop runs iter=0..max_iter-1,
     537              :      * giving compute_source_terms the correct iteration index. */
     538              :     cfd_status_t status = rk4_avx2_impl(field, ctx, g, params);
     539              : 
     540              :     if (stats) {
     541              :         stats->iterations = params->max_iter;
     542              :         double max_vel = 0.0, max_p = 0.0;
     543              :         ptrdiff_t n_s = (ptrdiff_t)(field->nx * field->ny * field->nz);
     544              :         double max_t = (field->T && n_s > 0) ? field->T[0] : 0.0;
     545              :         double max_nt = 0.0;
     546              :         ptrdiff_t ks;
     547              : #if defined(_OPENMP) && (_OPENMP >= 201107)
     548              :         #pragma omp parallel for reduction(max: max_vel, max_p, max_t, max_nt) schedule(static)
     549              : #endif
     550              :         for (ks = 0; ks < n_s; ks++) {
     551              :             double vel = sqrt(field->u[ks] * field->u[ks] +
     552              :                               field->v[ks] * field->v[ks] +
     553              :                               field->w[ks] * field->w[ks]);
     554              :             if (vel > max_vel) max_vel = vel;
     555              :             double ap = fabs(field->p[ks]);
     556              :             if (ap > max_p) max_p = ap;
     557              :             if (field->T && field->T[ks] > max_t) max_t = field->T[ks];
     558              :             if (field->nu_t && field->nu_t[ks] > max_nt) max_nt = field->nu_t[ks];
     559              :         }
     560              :         stats->max_velocity = max_vel;
     561              :         stats->max_pressure = max_p;
     562              :         stats->max_temperature = max_t;
     563              :         stats->max_nu_t = max_nt;
     564              :     }
     565              :     return status;
     566              : #endif
     567              : }
        

Generated by: LCOV version 2.0-1