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

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

Generated by: LCOV version 2.0-1