Line data Source code
1 : #include "cfd/core/cfd_status.h"
2 : #include "cfd/core/cpu_features.h"
3 : #include "cfd/core/filesystem.h"
4 : #include "cfd/core/gpu_device.h"
5 : #include "cfd/core/grid.h"
6 : #include "cfd/core/logging.h"
7 : #include "cfd/core/memory.h"
8 : #include "cfd/solvers/navier_stokes_solver.h"
9 : #include "cfd/solvers/poisson_solver.h"
10 :
11 : #include "../solvers/navier_stokes/ns_convection_internal.h"
12 : #include "../solvers/navier_stokes/ns_pressure_internal.h"
13 : #include "../solvers/navier_stokes/ns_viscous_internal.h"
14 : #include "../solvers/navier_stokes/ns_simd_backend_internal.h"
15 :
16 :
17 : #ifdef _WIN32
18 : #define WIN32_LEAN_AND_MEAN
19 : #include <windows.h>
20 : #endif
21 :
22 : #include <math.h>
23 : #include <stddef.h>
24 : #include <stdio.h>
25 : #include <stdlib.h>
26 : #include <string.h>
27 :
28 : #ifdef CFD_ENABLE_OPENMP
29 : #include <omp.h>
30 : #endif
31 :
32 : /* Compute max velocity magnitude (sqrt(u^2+v^2+w^2)) and max |pressure| for
33 : * stats reporting. Iterates over the full nx*ny*nz field and includes the w
34 : * component so 3D runs report correct values (w is always allocated; it stays
35 : * zero for 2D nz=1 grids, so 2D results are unchanged). */
36 17089 : static void compute_max_velocity_pressure(const flow_field* field, double* max_vel,
37 : double* max_p) {
38 17089 : double mv = 0.0;
39 17089 : double mp = 0.0;
40 17089 : ptrdiff_t n = (ptrdiff_t)(field->nx * field->ny * field->nz);
41 17089 : ptrdiff_t i;
42 : #if _OPENMP >= 201107
43 17089 : #pragma omp parallel for reduction(max: mv, mp) schedule(static)
44 : #endif
45 : for (i = 0; i < n; i++) {
46 : double vel = sqrt((field->u[i] * field->u[i]) + (field->v[i] * field->v[i]) +
47 : (field->w[i] * field->w[i]));
48 : if (vel > mv) mv = vel;
49 : double ap = fabs(field->p[i]);
50 : if (ap > mp) mp = ap;
51 : }
52 17089 : *max_vel = mv;
53 17089 : *max_p = mp;
54 17089 : }
55 :
56 : /* Compute max T for stats reporting */
57 17089 : static double compute_max_temperature(const flow_field* field) {
58 17089 : double max_t = 0.0;
59 17089 : if (field->T) {
60 17089 : size_t n = field->nx * field->ny * field->nz;
61 17089 : max_t = field->T[0];
62 26592788 : for (size_t i = 1; i < n; i++) {
63 26575699 : if (field->T[i] > max_t) max_t = field->T[i];
64 : }
65 : }
66 17089 : return max_t;
67 : }
68 :
69 : /* Maximum eddy viscosity; nu_t is non-negative, so 0.0 means laminar. */
70 17089 : static double compute_max_nu_t(const flow_field* field) {
71 17089 : double max_nt = 0.0;
72 17089 : if (field->nu_t) {
73 17089 : size_t n = field->nx * field->ny * field->nz;
74 26609877 : for (size_t i = 0; i < n; i++) {
75 26592788 : if (field->nu_t[i] > max_nt) max_nt = field->nu_t[i];
76 : }
77 : }
78 17089 : return max_nt;
79 : }
80 :
81 : // Forward declarations for internal solver implementations
82 : // These are not part of the public API
83 : cfd_status_t explicit_euler_impl(flow_field* field, const grid* grid, const ns_solver_params_t* params);
84 : cfd_status_t rk2_impl(flow_field* field, const grid* grid, const ns_solver_params_t* params);
85 : cfd_status_t rk4_impl(flow_field* field, const grid* grid, const ns_solver_params_t* params);
86 : #ifdef CFD_ENABLE_OPENMP
87 : cfd_status_t explicit_euler_omp_impl(flow_field* field, const grid* grid,
88 : const ns_solver_params_t* params);
89 : cfd_status_t rk2_omp_impl(flow_field* field, const grid* grid,
90 : const ns_solver_params_t* params);
91 : cfd_status_t rk4_omp_impl(flow_field* field, const grid* grid,
92 : const ns_solver_params_t* params);
93 : cfd_status_t solve_projection_method_omp(flow_field* field, const grid* grid,
94 : const ns_solver_params_t* params,
95 : poisson_solver_t* pressure,
96 : poisson_solver_t* viscous);
97 : #endif
98 :
99 : // GPU solver functions
100 : cfd_status_t solve_projection_method_gpu(flow_field* field, const grid* grid,
101 : const ns_solver_params_t* params,
102 : const gpu_config_t* config);
103 : cfd_status_t solve_explicit_euler_method_gpu(flow_field* field, const grid* grid,
104 : const ns_solver_params_t* params,
105 : const gpu_config_t* config);
106 : cfd_status_t solve_rk2_method_gpu(flow_field* field, const grid* grid,
107 : const ns_solver_params_t* params,
108 : const gpu_config_t* config);
109 : cfd_status_t solve_rk4_method_gpu(flow_field* field, const grid* grid,
110 : const ns_solver_params_t* params,
111 : const gpu_config_t* config);
112 : int gpu_is_available(void);
113 :
114 : // SIMD solver functions
115 :
116 : cfd_status_t explicit_euler_simd_init(struct NSSolver* solver, const grid* grid,
117 : const ns_solver_params_t* params);
118 : void explicit_euler_simd_destroy(struct NSSolver* solver);
119 :
120 : cfd_status_t explicit_euler_simd_step(struct NSSolver* solver, flow_field* field, const grid* grid,
121 : const ns_solver_params_t* params, ns_solver_stats_t* stats);
122 :
123 :
124 : cfd_status_t projection_simd_init(ns_solver_t* solver, const grid* grid, const ns_solver_params_t* params);
125 : void projection_simd_destroy(ns_solver_t* solver);
126 :
127 : cfd_status_t projection_simd_step(ns_solver_t* solver, flow_field* field, const grid* grid,
128 : const ns_solver_params_t* params, ns_solver_stats_t* stats);
129 :
130 : cfd_status_t rk2_avx2_init(ns_solver_t* solver, const grid* grid,
131 : const ns_solver_params_t* params);
132 : void rk2_avx2_destroy(ns_solver_t* solver);
133 : cfd_status_t rk2_avx2_step(ns_solver_t* solver, flow_field* field, const grid* grid,
134 : const ns_solver_params_t* params, ns_solver_stats_t* stats);
135 : cfd_status_t rk2_avx2_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
136 : const ns_solver_params_t* params, ns_solver_stats_t* stats);
137 :
138 : cfd_status_t rk4_avx2_init(ns_solver_t* solver, const grid* grid,
139 : const ns_solver_params_t* params);
140 : void rk4_avx2_destroy(ns_solver_t* solver);
141 : cfd_status_t rk4_avx2_step(ns_solver_t* solver, flow_field* field, const grid* grid,
142 : const ns_solver_params_t* params, ns_solver_stats_t* stats);
143 : cfd_status_t rk4_avx2_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
144 : const ns_solver_params_t* params, ns_solver_stats_t* stats);
145 :
146 : #ifdef _WIN32
147 : #else
148 : #include <sys/time.h>
149 : #endif
150 :
151 : // Maximum number of registered solver types
152 : #define MAX_REGISTERED_SOLVERS 32
153 :
154 : // Registry entry
155 : typedef struct {
156 : char name[64];
157 : ns_solver_factory_func factory;
158 : const char* description;
159 : ns_solver_backend_t backend; // Backend type for efficient filtering
160 : } solver_registry_entry;
161 :
162 : // ns_solver_registry_t structure
163 : struct NSSolverRegistry {
164 : solver_registry_entry entries[MAX_REGISTERED_SOLVERS];
165 : int count;
166 : };
167 :
168 : // Forward declarations for built-in solver factories
169 : static ns_solver_t* create_explicit_euler_solver(void);
170 : static ns_solver_t* create_rk2_solver(void);
171 : static ns_solver_t* create_rk2_optimized_solver(void);
172 : static ns_solver_t* create_rk4_solver(void);
173 : static ns_solver_t* create_rk4_optimized_solver(void);
174 : static ns_solver_t* create_explicit_euler_optimized_solver(void);
175 : static ns_solver_t* create_projection_solver(void);
176 : static ns_solver_t* create_projection_optimized_solver(void);
177 : #ifdef CFD_HAS_CUDA
178 : static ns_solver_t* create_explicit_euler_gpu_solver(void);
179 : static ns_solver_t* create_projection_gpu_solver(void);
180 : static ns_solver_t* create_rk2_gpu_solver(void);
181 : static ns_solver_t* create_rk4_gpu_solver(void);
182 : #endif
183 : #ifdef CFD_ENABLE_OPENMP
184 : static ns_solver_t* create_explicit_euler_omp_solver(void);
185 : static ns_solver_t* create_projection_omp_solver(void);
186 : static ns_solver_t* create_rk2_omp_solver(void);
187 : static ns_solver_t* create_rk4_omp_solver(void);
188 : #endif
189 :
190 : // External projection method solver functions
191 : extern cfd_status_t solve_projection_method(flow_field* field, const grid* grid,
192 : const ns_solver_params_t* params,
193 : poisson_solver_t* pressure,
194 : poisson_solver_t* viscous);
195 : extern void solve_projection_method_optimized(flow_field* field, const grid* grid,
196 : const ns_solver_params_t* params);
197 :
198 : // External GPU solver functions (from solver_gpu.cu or solver_gpu_stub.c)
199 : #include "cfd/core/cfd_status.h"
200 : #include "cfd/core/gpu_device.h"
201 :
202 :
203 : // Helper to get current time in milliseconds
204 34194 : static double get_time_ms(void) {
205 : #ifdef _WIN32
206 : LARGE_INTEGER freq, counter;
207 : QueryPerformanceFrequency(&freq);
208 : QueryPerformanceCounter(&counter);
209 : return (double)counter.QuadPart * 1000.0 / (double)freq.QuadPart;
210 : #else
211 34194 : struct timeval tv;
212 34194 : gettimeofday(&tv, NULL);
213 34194 : return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
214 : #endif
215 : }
216 :
217 : /**
218 : * solver Registry Implementation
219 : */
220 :
221 495 : ns_solver_registry_t* cfd_registry_create(void) {
222 495 : ns_solver_registry_t* registry = (ns_solver_registry_t*)cfd_calloc(1, sizeof(ns_solver_registry_t));
223 495 : return registry;
224 : }
225 :
226 495 : void cfd_registry_destroy(ns_solver_registry_t* registry) {
227 495 : if (registry) {
228 495 : cfd_free(registry);
229 : }
230 495 : }
231 :
232 492 : void cfd_registry_register_defaults(ns_solver_registry_t* registry) {
233 492 : if (!registry) {
234 : return;
235 : }
236 :
237 : // Register built-in solvers
238 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_EXPLICIT_EULER, create_explicit_euler_solver);
239 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK2, create_rk2_solver);
240 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK2_OPTIMIZED, create_rk2_optimized_solver);
241 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK4, create_rk4_solver);
242 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK4_OPTIMIZED, create_rk4_optimized_solver);
243 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_EXPLICIT_EULER_OPTIMIZED,
244 : create_explicit_euler_optimized_solver);
245 :
246 : // Register projection method solvers
247 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_PROJECTION, create_projection_solver);
248 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_PROJECTION_OPTIMIZED,
249 : create_projection_optimized_solver);
250 :
251 : // Register GPU solvers (requires CUDA)
252 : #ifdef CFD_HAS_CUDA
253 : cfd_registry_register(registry, NS_SOLVER_TYPE_EXPLICIT_EULER_GPU,
254 : create_explicit_euler_gpu_solver);
255 : cfd_registry_register(registry, NS_SOLVER_TYPE_PROJECTION_GPU,
256 : create_projection_gpu_solver);
257 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK2_GPU, create_rk2_gpu_solver);
258 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK4_GPU, create_rk4_gpu_solver);
259 : #endif
260 :
261 : // Register OpenMP solvers
262 : #ifdef CFD_ENABLE_OPENMP
263 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_EXPLICIT_EULER_OMP,
264 : create_explicit_euler_omp_solver);
265 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_PROJECTION_OMP, create_projection_omp_solver);
266 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK2_OMP, create_rk2_omp_solver);
267 492 : cfd_registry_register(registry, NS_SOLVER_TYPE_RK4_OMP, create_rk4_omp_solver);
268 : #endif
269 : }
270 :
271 : /**
272 : * Infer backend from solver type name based on naming convention.
273 : * Returns the likely backend, or NS_SOLVER_BACKEND_SCALAR as default.
274 : * Note: Defined here (before first use) and also used by cfd_solver_create_checked.
275 : */
276 5945 : static ns_solver_backend_t infer_backend_from_type(const char* type_name) {
277 5945 : if (!type_name) {
278 : return NS_SOLVER_BACKEND_SCALAR;
279 : }
280 :
281 : /* Check for GPU suffix first (most specific) */
282 5945 : if (strstr(type_name, "_gpu") != NULL) {
283 : return NS_SOLVER_BACKEND_CUDA;
284 : }
285 :
286 : /* Check for OMP suffix */
287 5943 : if (strstr(type_name, "_omp") != NULL) {
288 : return NS_SOLVER_BACKEND_OMP;
289 : }
290 :
291 : /* Check for optimized suffix (SIMD) */
292 3973 : if (strstr(type_name, "_optimized") != NULL) {
293 1970 : return NS_SOLVER_BACKEND_SIMD;
294 : }
295 :
296 : /* Default to scalar */
297 : return NS_SOLVER_BACKEND_SCALAR;
298 : }
299 :
300 5940 : int cfd_registry_register(ns_solver_registry_t* registry, const char* type_name,
301 : ns_solver_factory_func factory) {
302 5940 : if (!registry || !type_name || !factory) {
303 2 : cfd_set_error(CFD_ERROR_INVALID, "Invalid arguments for solver registration");
304 2 : return -1;
305 : }
306 5938 : if (strlen(type_name) == 0) {
307 1 : cfd_set_error(CFD_ERROR_INVALID, "solver type name cannot be empty");
308 1 : return -1;
309 : }
310 5937 : if (registry->count >= MAX_REGISTERED_SOLVERS) {
311 1 : cfd_set_error(CFD_ERROR_LIMIT_EXCEEDED, "Max registered solvers limit reached");
312 1 : return -1;
313 : }
314 :
315 : /* Infer backend from type name for efficient filtering later */
316 5936 : ns_solver_backend_t backend = infer_backend_from_type(type_name);
317 :
318 : // Check if already registered
319 38904 : for (int i = 0; i < registry->count; i++) {
320 32968 : if (strcmp(registry->entries[i].name, type_name) == 0) {
321 : // Update existing entry
322 0 : registry->entries[i].factory = factory;
323 0 : registry->entries[i].backend = backend;
324 0 : return 0;
325 : }
326 : }
327 :
328 : // Add new entry
329 5936 : snprintf(registry->entries[registry->count].name,
330 : sizeof(registry->entries[registry->count].name), "%s", type_name);
331 5936 : registry->entries[registry->count].factory = factory;
332 5936 : registry->entries[registry->count].backend = backend;
333 5936 : registry->count++;
334 :
335 5936 : return 0;
336 : }
337 :
338 0 : int cfd_registry_unregister(ns_solver_registry_t* registry, const char* type_name) {
339 0 : if (!registry || !type_name) {
340 : return -1;
341 : }
342 :
343 0 : for (int i = 0; i < registry->count; i++) {
344 0 : if (strcmp(registry->entries[i].name, type_name) == 0) {
345 : // Shift remaining entries
346 0 : for (int j = i; j < registry->count - 1; j++) {
347 0 : registry->entries[j] = registry->entries[j + 1];
348 : }
349 0 : registry->count--;
350 0 : return 0;
351 : }
352 : }
353 : return -1;
354 : }
355 :
356 2 : int cfd_registry_list(ns_solver_registry_t* registry, const char** names, int max_count) {
357 2 : if (!registry) {
358 : return 0;
359 : }
360 :
361 2 : int count = (registry->count < max_count) ? registry->count : max_count;
362 2 : if (names) {
363 26 : for (int i = 0; i < count; i++) {
364 24 : names[i] = registry->entries[i].name;
365 : }
366 : }
367 : return registry->count;
368 : }
369 :
370 0 : int cfd_registry_has(ns_solver_registry_t* registry, const char* type_name) {
371 0 : if (!registry || !type_name) {
372 : return 0;
373 : }
374 :
375 0 : for (int i = 0; i < registry->count; i++) {
376 0 : if (strcmp(registry->entries[i].name, type_name) == 0) {
377 : return 1;
378 : }
379 : }
380 : return 0;
381 : }
382 :
383 0 : const char* cfd_registry_get_description(ns_solver_registry_t* registry, const char* type_name) {
384 0 : if (!registry || !type_name) {
385 : return NULL;
386 : }
387 :
388 : // Create a temporary solver to get its description
389 0 : ns_solver_t* solver = cfd_solver_create(registry, type_name);
390 0 : if (solver) {
391 0 : const char* desc = solver->description;
392 0 : solver_destroy(solver);
393 0 : return desc;
394 : }
395 : return NULL;
396 : }
397 :
398 : /**
399 : * solver Creation and Management
400 : */
401 :
402 790 : ns_solver_t* cfd_solver_create(ns_solver_registry_t* registry, const char* type_name) {
403 790 : if (!registry || !type_name) {
404 1 : cfd_set_error(CFD_ERROR_INVALID, "Invalid arguments for solver creation");
405 1 : return NULL;
406 : }
407 :
408 4518 : for (int i = 0; i < registry->count; i++) {
409 4472 : if (strcmp(registry->entries[i].name, type_name) == 0) {
410 743 : ns_solver_t* solver = registry->entries[i].factory();
411 743 : if (!solver) {
412 : /* Factory returned NULL - check if error was already set by the factory.
413 : * Factories should set specific errors:
414 : * - CFD_ERROR_UNSUPPORTED: backend not available (GPU, SIMD, etc.)
415 : * - CFD_ERROR_NOMEM: allocation failed
416 : * If no error was set, assume memory allocation failure. */
417 0 : if (cfd_get_last_status() == CFD_SUCCESS) {
418 0 : cfd_set_error(CFD_ERROR_NOMEM, "Failed to allocate solver");
419 : }
420 : }
421 743 : return solver;
422 : }
423 : }
424 :
425 : /* Solver type not found in registry - use CFD_ERROR_NOT_FOUND
426 : * to distinguish from CFD_ERROR_UNSUPPORTED (backend unavailable) */
427 46 : char error_msg[128];
428 46 : snprintf(error_msg, sizeof(error_msg), "Solver type '%s' not registered", type_name);
429 46 : cfd_set_error(CFD_ERROR_NOT_FOUND, error_msg);
430 46 : return NULL;
431 : }
432 :
433 744 : void solver_destroy(ns_solver_t* solver) {
434 744 : if (!solver) {
435 : return;
436 : }
437 :
438 743 : if (solver->destroy) {
439 743 : solver->destroy(solver);
440 : }
441 743 : cfd_free(solver);
442 : }
443 :
444 :
445 646 : cfd_status_t solver_init(ns_solver_t* solver, const grid* grid, const ns_solver_params_t* params) {
446 646 : if (!solver) {
447 : return CFD_ERROR_INVALID;
448 : }
449 1290 : cfd_status_t scheme_status = ns_check_viscous_scheme(
450 645 : params, (solver->capabilities & NS_SOLVER_CAP_IMPLICIT_VISCOUS) != 0);
451 645 : if (scheme_status != CFD_SUCCESS) {
452 : return scheme_status;
453 : }
454 613 : if (!solver->init) {
455 : return CFD_SUCCESS; // Optional
456 : }
457 :
458 613 : return solver->init(solver, grid, params);
459 : }
460 :
461 :
462 17117 : cfd_status_t solver_step(ns_solver_t* solver, flow_field* field, const grid* grid,
463 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
464 17117 : if (!solver || !field || !grid || !params) {
465 : return CFD_ERROR_INVALID;
466 : }
467 17113 : if (!solver->step) {
468 : return CFD_ERROR;
469 : }
470 : /* Checked on every call, not only at init: params are the caller's and may
471 : * have changed since, and an implicit scheme a solver ignored would run
472 : * explicit at a dt chosen because it no longer had to be stable. */
473 34226 : cfd_status_t scheme_status = ns_check_viscous_scheme(
474 17113 : params, (solver->capabilities & NS_SOLVER_CAP_IMPLICIT_VISCOUS) != 0);
475 17113 : if (scheme_status != CFD_SUCCESS) {
476 : return scheme_status;
477 : }
478 17107 : cfd_status_t dt_status = ns_check_dt(params);
479 17107 : if (dt_status != CFD_SUCCESS) {
480 : return dt_status;
481 : }
482 17071 : cfd_status_t segment_status = ns_check_turbulence_segments(params);
483 44 : if (segment_status != CFD_SUCCESS) {
484 : return segment_status;
485 : }
486 :
487 : /* Default; a solver that advances by a different step overrides it. */
488 17070 : if (stats) {
489 17070 : stats->dt_used = params->dt;
490 : }
491 :
492 17070 : double start_time = get_time_ms();
493 :
494 17070 : cfd_status_t status = solver->step(solver, field, grid, params, stats);
495 17070 : double end_time = get_time_ms();
496 :
497 17070 : if (stats) {
498 17070 : stats->elapsed_time_ms = end_time - start_time;
499 17070 : stats->status = status;
500 : }
501 :
502 : return status;
503 : }
504 :
505 :
506 70 : cfd_status_t solver_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
507 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
508 70 : if (!solver || !field || !grid || !params) {
509 : return CFD_ERROR_INVALID;
510 : }
511 69 : if (!solver->solve) {
512 : return CFD_ERROR;
513 : }
514 : /* Checked on every call, not only at init: params are the caller's and may
515 : * have changed since, and an implicit scheme a solver ignored would run
516 : * explicit at a dt chosen because it no longer had to be stable. */
517 138 : cfd_status_t scheme_status = ns_check_viscous_scheme(
518 69 : params, (solver->capabilities & NS_SOLVER_CAP_IMPLICIT_VISCOUS) != 0);
519 69 : if (scheme_status != CFD_SUCCESS) {
520 : return scheme_status;
521 : }
522 63 : cfd_status_t dt_status = ns_check_dt(params);
523 63 : if (dt_status != CFD_SUCCESS) {
524 : return dt_status;
525 : }
526 27 : cfd_status_t segment_status = ns_check_turbulence_segments(params);
527 0 : if (segment_status != CFD_SUCCESS) {
528 : return segment_status;
529 : }
530 :
531 : /* Default; a solver that advances by a different step overrides it. */
532 27 : if (stats) {
533 27 : stats->dt_used = params->dt;
534 : }
535 :
536 27 : double start_time = get_time_ms();
537 :
538 27 : cfd_status_t status = solver->solve(solver, field, grid, params, stats);
539 27 : double end_time = get_time_ms();
540 :
541 27 : if (stats) {
542 27 : stats->elapsed_time_ms = end_time - start_time;
543 27 : stats->status = status;
544 : }
545 :
546 : return status;
547 : }
548 :
549 1 : void solver_apply_boundary(ns_solver_t* solver, flow_field* field, const grid* grid) {
550 1 : if (!solver || !field || !grid) {
551 : return;
552 : }
553 :
554 0 : if (solver->apply_boundary) {
555 0 : solver->apply_boundary(solver, field, grid);
556 : } else {
557 : // Fall back to default boundary conditions
558 0 : apply_boundary_conditions(field, grid);
559 : }
560 : }
561 :
562 1 : double solver_compute_dt(ns_solver_t* solver, const flow_field* field, const grid* grid,
563 : const ns_solver_params_t* params) {
564 1 : if (!solver || !field || !grid || !params) {
565 : return 0.0;
566 : }
567 :
568 0 : if (solver->compute_dt) {
569 0 : return solver->compute_dt(solver, field, grid, params);
570 : }
571 :
572 : // Default implementation
573 0 : double max_vel = 0.0;
574 0 : double min_dx = grid->dx[0];
575 0 : double min_dy = grid->dy[0];
576 :
577 0 : for (size_t i = 0; i < grid->nx - 1; i++) {
578 0 : if (grid->dx[i] < min_dx) {
579 0 : min_dx = grid->dx[i];
580 : }
581 : }
582 0 : for (size_t j = 0; j < grid->ny - 1; j++) {
583 0 : if (grid->dy[j] < min_dy) {
584 0 : min_dy = grid->dy[j];
585 : }
586 : }
587 :
588 0 : for (size_t i = 0; i < field->nx * field->ny; i++) {
589 0 : double vel = sqrt((field->u[i] * field->u[i]) + (field->v[i] * field->v[i]));
590 0 : if (vel > max_vel) {
591 0 : max_vel = vel;
592 : }
593 : }
594 :
595 0 : if (max_vel < 1e-10) {
596 0 : max_vel = 1.0;
597 : }
598 :
599 0 : double dt = params->cfl * fmin(min_dx, min_dy) / max_vel;
600 0 : return fmin(fmax(dt, 1e-6), 0.01);
601 : }
602 :
603 : /**
604 : * Built-in solver: Explicit Euler
605 : * Wraps the existing solve_navier_stokes function
606 : */
607 :
608 : typedef struct {
609 : int initialized;
610 : } explicit_euler_context;
611 :
612 375 : static cfd_status_t explicit_euler_init(ns_solver_t* solver, const grid* grid,
613 : const ns_solver_params_t* params) {
614 375 : (void)grid;
615 :
616 375 : cfd_status_t scheme_status = ns_check_convection_scheme(params, 1);
617 375 : if (scheme_status != CFD_SUCCESS) {
618 : return scheme_status;
619 : }
620 :
621 369 : cfd_status_t turb_status = ns_check_turbulence_model(params, 1);
622 369 : if (turb_status != CFD_SUCCESS) {
623 : return turb_status;
624 : }
625 :
626 367 : cfd_status_t pressure_bc_status = ns_check_pressure_bc(params, 0);
627 367 : if (pressure_bc_status != CFD_SUCCESS) {
628 : return pressure_bc_status;
629 : }
630 :
631 355 : cfd_status_t pressure_solver_status = ns_check_pressure_solver(params, 0);
632 355 : if (pressure_solver_status != CFD_SUCCESS) {
633 : return pressure_solver_status;
634 : }
635 :
636 343 : explicit_euler_context* ctx =
637 343 : (explicit_euler_context*)cfd_malloc(sizeof(explicit_euler_context));
638 343 : if (!ctx) {
639 : return CFD_ERROR;
640 : }
641 :
642 343 : ctx->initialized = 1;
643 343 : solver->context = ctx;
644 343 : return CFD_SUCCESS;
645 : }
646 :
647 451 : static void explicit_euler_destroy(ns_solver_t* solver) {
648 451 : if (solver->context) {
649 343 : cfd_free(solver->context);
650 343 : solver->context = NULL;
651 : }
652 451 : }
653 :
654 6407 : static cfd_status_t explicit_euler_step(ns_solver_t* solver, flow_field* field, const grid* grid,
655 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
656 6407 : if (stats) {
657 : /* Euler clamps its own step; report what it really advanced by. */
658 1049 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
659 : }
660 :
661 6407 : (void)solver;
662 :
663 6407 : if (field->nx < 3 || field->ny < 3) {
664 : return CFD_ERROR_INVALID;
665 : }
666 :
667 : // Create params with single iteration
668 6407 : ns_solver_params_t step_params = *params;
669 6407 : step_params.max_iter = 1;
670 :
671 6407 : cfd_status_t status = explicit_euler_impl(field, grid, &step_params);
672 6407 : if (status != CFD_SUCCESS) {
673 : return status;
674 : }
675 :
676 6406 : if (stats) {
677 1048 : stats->iterations = 1;
678 :
679 : // Compute max velocity
680 1048 : double max_vel = 0.0;
681 1048 : double max_p = 0.0;
682 1048 : compute_max_velocity_pressure(field, &max_vel, &max_p);
683 1048 : stats->max_velocity = max_vel;
684 1048 : stats->max_pressure = max_p;
685 1048 : stats->max_temperature = compute_max_temperature(field);
686 2096 : stats->max_nu_t = compute_max_nu_t(field);
687 : }
688 :
689 : return CFD_SUCCESS;
690 : }
691 :
692 7 : static cfd_status_t explicit_euler_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
693 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
694 7 : if (stats) {
695 : /* Euler clamps its own step; report what it really advanced by. */
696 7 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
697 : }
698 :
699 7 : (void)solver;
700 :
701 7 : if (field->nx < 3 || field->ny < 3) {
702 : return CFD_ERROR_INVALID;
703 : }
704 :
705 7 : cfd_status_t status = explicit_euler_impl(field, grid, params);
706 7 : if (status != CFD_SUCCESS) {
707 : return status;
708 : }
709 :
710 6 : if (stats) {
711 6 : stats->iterations = params->max_iter;
712 :
713 6 : double max_vel = 0.0;
714 6 : double max_p = 0.0;
715 6 : compute_max_velocity_pressure(field, &max_vel, &max_p);
716 6 : stats->max_velocity = max_vel;
717 6 : stats->max_pressure = max_p;
718 6 : stats->max_temperature = compute_max_temperature(field);
719 12 : stats->max_nu_t = compute_max_nu_t(field);
720 : }
721 :
722 : return CFD_SUCCESS;
723 : }
724 :
725 174 : static ns_solver_t* create_explicit_euler_solver(void) {
726 174 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
727 174 : if (!s) {
728 : return NULL;
729 : }
730 :
731 174 : s->name = NS_SOLVER_TYPE_EXPLICIT_EULER;
732 174 : s->description = "Basic explicit Euler finite difference solver for 2D Navier-Stokes";
733 174 : s->version = "1.0.0";
734 174 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT;
735 174 : s->backend = NS_SOLVER_BACKEND_SCALAR;
736 :
737 174 : s->init = explicit_euler_init;
738 174 : s->destroy = explicit_euler_destroy;
739 174 : s->step = explicit_euler_step;
740 174 : s->solve = explicit_euler_solve;
741 174 : s->apply_boundary = NULL; // Use default
742 174 : s->compute_dt = NULL; // Use default
743 :
744 174 : return s;
745 : }
746 :
747 : /**
748 : * Built-in solver: RK2 (Heun's Method)
749 : * Second-order Runge-Kutta time integration
750 : */
751 :
752 4405 : static cfd_status_t rk2_step(ns_solver_t* solver, flow_field* field, const grid* grid,
753 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
754 4405 : (void)solver;
755 :
756 4405 : if (field->nx < 3 || field->ny < 3) {
757 : return CFD_ERROR_INVALID;
758 : }
759 :
760 4405 : ns_solver_params_t step_params = *params;
761 4405 : step_params.max_iter = 1;
762 :
763 4405 : cfd_status_t status = rk2_impl(field, grid, &step_params);
764 :
765 4405 : if (stats) {
766 1047 : stats->iterations = 1;
767 1047 : double max_vel = 0.0;
768 1047 : double max_p = 0.0;
769 1047 : compute_max_velocity_pressure(field, &max_vel, &max_p);
770 1047 : stats->max_velocity = max_vel;
771 1047 : stats->max_pressure = max_p;
772 1047 : stats->max_temperature = compute_max_temperature(field);
773 2094 : stats->max_nu_t = compute_max_nu_t(field);
774 : }
775 :
776 : return status;
777 : }
778 :
779 4 : static cfd_status_t rk2_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
780 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
781 4 : (void)solver;
782 :
783 4 : if (field->nx < 3 || field->ny < 3) {
784 : return CFD_ERROR_INVALID;
785 : }
786 :
787 4 : cfd_status_t status = rk2_impl(field, grid, params);
788 :
789 4 : if (stats) {
790 4 : stats->iterations = params->max_iter;
791 4 : double max_vel = 0.0;
792 4 : double max_p = 0.0;
793 4 : compute_max_velocity_pressure(field, &max_vel, &max_p);
794 4 : stats->max_velocity = max_vel;
795 4 : stats->max_pressure = max_p;
796 4 : stats->max_temperature = compute_max_temperature(field);
797 8 : stats->max_nu_t = compute_max_nu_t(field);
798 : }
799 :
800 : return status;
801 : }
802 :
803 103 : static ns_solver_t* create_rk2_solver(void) {
804 103 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
805 103 : if (!s) {
806 : return NULL;
807 : }
808 :
809 103 : s->name = NS_SOLVER_TYPE_RK2;
810 103 : s->description = "RK2 (Heun's method) - 2nd order explicit time integration";
811 103 : s->version = "1.0.0";
812 103 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT;
813 103 : s->backend = NS_SOLVER_BACKEND_SCALAR;
814 :
815 103 : s->init = explicit_euler_init;
816 103 : s->destroy = explicit_euler_destroy;
817 103 : s->step = rk2_step;
818 103 : s->solve = rk2_solve;
819 103 : s->apply_boundary = NULL;
820 103 : s->compute_dt = NULL;
821 :
822 103 : return s;
823 : }
824 :
825 24 : static ns_solver_t* create_rk2_optimized_solver(void) {
826 24 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
827 24 : if (!s) {
828 : return NULL;
829 : }
830 :
831 24 : s->name = NS_SOLVER_TYPE_RK2_OPTIMIZED;
832 24 : s->description = "AVX2/SIMD + OpenMP RK2 (Heun's method)";
833 24 : s->version = "1.0.0";
834 24 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT |
835 : NS_SOLVER_CAP_SIMD;
836 24 : s->backend = NS_SOLVER_BACKEND_SIMD;
837 :
838 24 : s->init = rk2_avx2_init;
839 24 : s->destroy = rk2_avx2_destroy;
840 24 : s->step = rk2_avx2_step;
841 24 : s->solve = rk2_avx2_solve;
842 24 : s->apply_boundary = NULL;
843 24 : s->compute_dt = NULL;
844 :
845 24 : return s;
846 : }
847 :
848 : /**
849 : * Built-in solver: RK4 (classical Runge-Kutta)
850 : * Fourth-order explicit time integration
851 : */
852 :
853 704 : static cfd_status_t rk4_step(ns_solver_t* solver, flow_field* field, const grid* grid,
854 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
855 704 : (void)solver;
856 :
857 704 : if (field->nx < 3 || field->ny < 3) {
858 : return CFD_ERROR_INVALID;
859 : }
860 :
861 704 : ns_solver_params_t step_params = *params;
862 704 : step_params.max_iter = 1;
863 :
864 704 : cfd_status_t status = rk4_impl(field, grid, &step_params);
865 :
866 704 : if (stats) {
867 704 : stats->iterations = 1;
868 704 : double max_vel = 0.0;
869 704 : double max_p = 0.0;
870 704 : compute_max_velocity_pressure(field, &max_vel, &max_p);
871 704 : stats->max_velocity = max_vel;
872 704 : stats->max_pressure = max_p;
873 704 : stats->max_temperature = compute_max_temperature(field);
874 1408 : stats->max_nu_t = compute_max_nu_t(field);
875 : }
876 :
877 : return status;
878 : }
879 :
880 0 : static cfd_status_t rk4_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
881 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
882 0 : (void)solver;
883 :
884 0 : if (field->nx < 3 || field->ny < 3) {
885 : return CFD_ERROR_INVALID;
886 : }
887 :
888 0 : cfd_status_t status = rk4_impl(field, grid, params);
889 :
890 0 : if (stats) {
891 0 : stats->iterations = params->max_iter;
892 0 : double max_vel = 0.0;
893 0 : double max_p = 0.0;
894 0 : compute_max_velocity_pressure(field, &max_vel, &max_p);
895 0 : stats->max_velocity = max_vel;
896 0 : stats->max_pressure = max_p;
897 0 : stats->max_temperature = compute_max_temperature(field);
898 0 : stats->max_nu_t = compute_max_nu_t(field);
899 : }
900 :
901 : return status;
902 : }
903 :
904 57 : static ns_solver_t* create_rk4_solver(void) {
905 57 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
906 57 : if (!s) {
907 : return NULL;
908 : }
909 :
910 57 : s->name = NS_SOLVER_TYPE_RK4;
911 57 : s->description = "RK4 (classical Runge-Kutta) - 4th order explicit time integration";
912 57 : s->version = "1.0.0";
913 57 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT;
914 57 : s->backend = NS_SOLVER_BACKEND_SCALAR;
915 :
916 57 : s->init = explicit_euler_init;
917 57 : s->destroy = explicit_euler_destroy;
918 57 : s->step = rk4_step;
919 57 : s->solve = rk4_solve;
920 57 : s->apply_boundary = NULL;
921 57 : s->compute_dt = NULL;
922 :
923 57 : return s;
924 : }
925 :
926 22 : static ns_solver_t* create_rk4_optimized_solver(void) {
927 22 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
928 22 : if (!s) {
929 : return NULL;
930 : }
931 :
932 22 : s->name = NS_SOLVER_TYPE_RK4_OPTIMIZED;
933 22 : s->description = "AVX2/SIMD + OpenMP RK4 (classical Runge-Kutta)";
934 22 : s->version = "1.0.0";
935 22 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT |
936 : NS_SOLVER_CAP_SIMD;
937 22 : s->backend = NS_SOLVER_BACKEND_SIMD;
938 :
939 22 : s->init = rk4_avx2_init;
940 22 : s->destroy = rk4_avx2_destroy;
941 22 : s->step = rk4_avx2_step;
942 22 : s->solve = rk4_avx2_solve;
943 22 : s->apply_boundary = NULL;
944 22 : s->compute_dt = NULL;
945 :
946 22 : return s;
947 : }
948 :
949 : /* Registry-level stats wrapper for the SIMD Euler step.
950 : *
951 : * Was named "_guarded" for a compile-time SIMD guard that now lives in
952 : * explicit_euler_simd_init(), leaving it a bare passthrough -- which is how it
953 : * came to be the one Euler entry point that never reported dt_used, while its
954 : * own solve() sibling twenty lines below did. */
955 1 : static cfd_status_t explicit_euler_simd_step_with_stats(ns_solver_t* solver, flow_field* field,
956 : const grid* grid,
957 : const ns_solver_params_t* params,
958 : ns_solver_stats_t* stats) {
959 1 : if (stats) {
960 : /* Euler clamps its own step; report what it really advanced by. */
961 1 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
962 : }
963 1 : return explicit_euler_simd_step(solver, field, grid, params, stats);
964 : }
965 :
966 0 : static cfd_status_t explicit_euler_simd_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
967 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
968 0 : if (stats) {
969 : /* Euler clamps its own step; report what it really advanced by. */
970 0 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
971 : }
972 :
973 0 : if (!solver || !field || !grid || !params) {
974 : return CFD_ERROR_INVALID;
975 : }
976 0 : for (int i = 0; i < params->max_iter; i++) {
977 0 : cfd_status_t status = explicit_euler_simd_step(solver, field, grid, params, NULL);
978 0 : if (status != CFD_SUCCESS) {
979 0 : return status;
980 : }
981 : }
982 :
983 0 : if (stats) {
984 0 : stats->iterations = params->max_iter;
985 0 : double max_vel = 0.0;
986 0 : double max_p = 0.0;
987 0 : compute_max_velocity_pressure(field, &max_vel, &max_p);
988 0 : stats->max_velocity = max_vel;
989 0 : stats->max_pressure = max_p;
990 0 : stats->max_temperature = compute_max_temperature(field);
991 0 : stats->max_nu_t = compute_max_nu_t(field);
992 : }
993 : return CFD_SUCCESS;
994 : }
995 :
996 30 : static ns_solver_t* create_explicit_euler_optimized_solver(void) {
997 30 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
998 30 : if (!s) {
999 : return NULL;
1000 : }
1001 :
1002 30 : s->name = NS_SOLVER_TYPE_EXPLICIT_EULER_OPTIMIZED;
1003 30 : s->description = "SIMD-optimized explicit Euler solver (AVX2)";
1004 30 : s->version = "1.0.0";
1005 30 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_SIMD;
1006 30 : s->backend = NS_SOLVER_BACKEND_SIMD;
1007 :
1008 30 : s->init = explicit_euler_simd_init;
1009 30 : s->destroy = explicit_euler_simd_destroy;
1010 30 : s->step = explicit_euler_simd_step_with_stats;
1011 30 : s->solve = explicit_euler_simd_solve;
1012 30 : s->apply_boundary = NULL;
1013 30 : s->compute_dt = NULL;
1014 :
1015 30 : return s;
1016 : }
1017 :
1018 : /**
1019 : * Built-in solver: Projection Method (Chorin's Method)
1020 : */
1021 :
1022 : typedef struct {
1023 : int initialized;
1024 : poisson_solver_t* pressure; /**< Owned across the solver's life; ns_pressure_internal.h */
1025 : poisson_solver_t* viscous; /**< Implicit viscous solve; NULL while the scheme is
1026 : explicit. ns_viscous_internal.h */
1027 : poisson_solver_backend_t pressure_backend; /**< Backend of both owned solvers */
1028 : } projection_context;
1029 :
1030 : static void projection_destroy(ns_solver_t* solver);
1031 : static cfd_status_t projection_viscous(ns_solver_t* solver, const grid* grid,
1032 : const ns_solver_params_t* params,
1033 : poisson_solver_t** out);
1034 :
1035 117 : static cfd_status_t projection_init(ns_solver_t* solver, const grid* grid, const ns_solver_params_t* params) {
1036 117 : if (!solver || !grid) {
1037 : return CFD_ERROR_INVALID;
1038 : }
1039 117 : cfd_status_t scheme_status = ns_check_convection_scheme(params, 1);
1040 117 : if (scheme_status != CFD_SUCCESS) {
1041 : return scheme_status;
1042 : }
1043 :
1044 116 : cfd_status_t turb_status = ns_check_turbulence_model(params, 1);
1045 116 : if (turb_status != CFD_SUCCESS) {
1046 : return turb_status;
1047 : }
1048 106 : cfd_status_t pressure_bc_status = ns_check_pressure_bc(params, 1);
1049 106 : if (pressure_bc_status != CFD_SUCCESS) {
1050 : return pressure_bc_status;
1051 : }
1052 :
1053 105 : cfd_status_t pressure_solver_status = ns_check_pressure_solver(params, 1);
1054 105 : if (pressure_solver_status != CFD_SUCCESS) {
1055 : return pressure_solver_status;
1056 : }
1057 104 : projection_context* ctx = (projection_context*)cfd_calloc(1, sizeof(projection_context));
1058 104 : if (!ctx) {
1059 : return CFD_ERROR;
1060 : }
1061 104 : ctx->pressure_backend = POISSON_BACKEND_SCALAR;
1062 :
1063 : /* Build the pressure solver now. This replaces the probe that created a
1064 : * multigrid solver purely to find out whether the grid was 2^k+1 and then
1065 : * destroyed it: building the real one answers the same question and keeps
1066 : * the answer, so an MG mode on a bad grid still fails at init. */
1067 104 : poisson_solver_config_t cfg;
1068 104 : cfd_status_t cfg_status = ns_pressure_config(params, ctx->pressure_backend, &cfg);
1069 104 : if (cfg_status != CFD_SUCCESS) {
1070 0 : cfd_free(ctx);
1071 0 : return cfg_status;
1072 : }
1073 104 : cfd_status_t pressure_status =
1074 104 : ns_pressure_ensure(&ctx->pressure, &cfg, grid);
1075 104 : if (pressure_status != CFD_SUCCESS) {
1076 4 : cfd_free(ctx);
1077 4 : return pressure_status;
1078 : }
1079 :
1080 100 : ctx->initialized = 1;
1081 100 : solver->context = ctx;
1082 :
1083 : /* Built now for the same reason the pressure solver is: an implicit scheme
1084 : * this backend cannot run -- or one combined with a turbulence model --
1085 : * fails at init, where the caller can still choose something else. */
1086 100 : poisson_solver_t* viscous = NULL;
1087 100 : cfd_status_t viscous_status = projection_viscous(solver, grid, params, &viscous);
1088 100 : if (viscous_status != CFD_SUCCESS) {
1089 1 : projection_destroy(solver);
1090 1 : return viscous_status;
1091 : }
1092 : return CFD_SUCCESS;
1093 : }
1094 :
1095 188 : static void projection_destroy(ns_solver_t* solver) {
1096 188 : if (solver->context) {
1097 134 : projection_context* ctx = (projection_context*)solver->context;
1098 134 : ns_pressure_release(&ctx->pressure);
1099 134 : ns_pressure_release(&ctx->viscous);
1100 134 : cfd_free(ctx);
1101 134 : solver->context = NULL;
1102 : }
1103 188 : }
1104 :
1105 : /**
1106 : * The pressure solver for this step.
1107 : *
1108 : * Re-derived from params every step rather than trusted from init, because
1109 : * pressure_solver and pressure_bc are read from params on each call and a caller
1110 : * may resize between steps. ns_pressure_ensure() rebuilds only when something
1111 : * actually moved, so the common case is a comparison. Solving on with a stale
1112 : * configuration would be the silent kind of wrong.
1113 : */
1114 13507 : static cfd_status_t projection_pressure(ns_solver_t* solver, const grid* grid,
1115 : const ns_solver_params_t* params,
1116 : poisson_solver_t** out) {
1117 13507 : projection_context* ctx = (projection_context*)solver->context;
1118 13507 : if (!ctx) {
1119 : return CFD_ERROR_INVALID;
1120 : }
1121 :
1122 13507 : poisson_solver_config_t cfg;
1123 13507 : cfd_status_t status = ns_pressure_config(params, ctx->pressure_backend, &cfg);
1124 13507 : if (status != CFD_SUCCESS) {
1125 : return status;
1126 : }
1127 13507 : status = ns_pressure_ensure(&ctx->pressure, &cfg, grid);
1128 13507 : if (status != CFD_SUCCESS) {
1129 : return status;
1130 : }
1131 :
1132 13507 : *out = ctx->pressure;
1133 13507 : return CFD_SUCCESS;
1134 : }
1135 :
1136 : /**
1137 : * The implicit viscous solver for this step, or NULL for an explicit scheme.
1138 : *
1139 : * The twin of projection_pressure(): sigma = 1/(theta*nu*dt) depends on dt, so
1140 : * it is re-derived every step and ns_pressure_ensure() rebuilds only when it
1141 : * moved. Released when the scheme goes back to explicit rather than kept, so a
1142 : * run that tried an implicit scheme once does not carry its vectors for good.
1143 : */
1144 13641 : static cfd_status_t projection_viscous(ns_solver_t* solver, const grid* grid,
1145 : const ns_solver_params_t* params,
1146 : poisson_solver_t** out) {
1147 13641 : *out = NULL;
1148 13641 : projection_context* ctx = (projection_context*)solver->context;
1149 13641 : if (!ctx) {
1150 : return CFD_ERROR_INVALID;
1151 : }
1152 :
1153 13641 : double theta = 0.0;
1154 13641 : cfd_status_t status = ns_viscous_theta(params, &theta);
1155 13641 : if (status != CFD_SUCCESS) {
1156 : return status;
1157 : }
1158 13637 : double sigma = (theta > 0.0) ? ns_viscous_shift(theta, params->mu, params->dt) : 0.0;
1159 808 : if (sigma == 0.0) {
1160 12829 : ns_pressure_release(&ctx->viscous);
1161 12829 : return CFD_SUCCESS;
1162 : }
1163 :
1164 808 : poisson_solver_config_t cfg = ns_viscous_config(ctx->pressure_backend, sigma, grid->nz);
1165 808 : status = ns_pressure_ensure(&ctx->viscous, &cfg, grid);
1166 808 : if (status != CFD_SUCCESS) {
1167 : return status;
1168 : }
1169 :
1170 808 : *out = ctx->viscous;
1171 808 : return CFD_SUCCESS;
1172 : }
1173 :
1174 13202 : static cfd_status_t projection_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1175 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1176 13202 : if (field->nx < 3 || field->ny < 3) {
1177 : return CFD_ERROR_INVALID;
1178 : }
1179 :
1180 13202 : poisson_solver_t* pressure = NULL;
1181 13202 : cfd_status_t status = projection_pressure(solver, grid, params, &pressure);
1182 13202 : if (status != CFD_SUCCESS) {
1183 : return status;
1184 : }
1185 13202 : poisson_solver_t* viscous = NULL;
1186 13202 : status = projection_viscous(solver, grid, params, &viscous);
1187 13202 : if (status != CFD_SUCCESS) {
1188 : return status;
1189 : }
1190 :
1191 13201 : ns_solver_params_t step_params = *params;
1192 13201 : step_params.max_iter = 1;
1193 :
1194 13201 : status = solve_projection_method(field, grid, &step_params, pressure, viscous);
1195 13201 : if (status != CFD_SUCCESS) {
1196 : return status;
1197 : }
1198 :
1199 13201 : if (stats) {
1200 13201 : stats->iterations = 1;
1201 : // Compute max velocity/pressure
1202 13201 : double max_vel = 0.0;
1203 13201 : double max_p = 0.0;
1204 13201 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1205 13201 : stats->max_velocity = max_vel;
1206 13201 : stats->max_pressure = max_p;
1207 13201 : stats->max_temperature = compute_max_temperature(field);
1208 26402 : stats->max_nu_t = compute_max_nu_t(field);
1209 : }
1210 : return CFD_SUCCESS;
1211 : }
1212 :
1213 9 : static cfd_status_t projection_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1214 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1215 9 : if (field->nx < 3 || field->ny < 3) {
1216 : return CFD_ERROR_INVALID;
1217 : }
1218 :
1219 9 : poisson_solver_t* pressure = NULL;
1220 9 : cfd_status_t status = projection_pressure(solver, grid, params, &pressure);
1221 9 : if (status != CFD_SUCCESS) {
1222 : return status;
1223 : }
1224 9 : poisson_solver_t* viscous = NULL;
1225 9 : status = projection_viscous(solver, grid, params, &viscous);
1226 9 : if (status != CFD_SUCCESS) {
1227 : return status;
1228 : }
1229 :
1230 9 : status = solve_projection_method(field, grid, params, pressure, viscous);
1231 9 : if (status != CFD_SUCCESS) {
1232 : return status;
1233 : }
1234 :
1235 9 : if (stats) {
1236 9 : stats->iterations = params->max_iter;
1237 : // Compute max velocity/pressure
1238 9 : double max_vel = 0.0;
1239 9 : double max_p = 0.0;
1240 9 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1241 9 : stats->max_velocity = max_vel;
1242 9 : stats->max_pressure = max_p;
1243 9 : stats->max_temperature = compute_max_temperature(field);
1244 18 : stats->max_nu_t = compute_max_nu_t(field);
1245 : }
1246 : return CFD_SUCCESS;
1247 : }
1248 :
1249 129 : static ns_solver_t* create_projection_solver(void) {
1250 129 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1251 129 : if (!s) {
1252 : return NULL;
1253 : }
1254 :
1255 129 : s->name = NS_SOLVER_TYPE_PROJECTION;
1256 129 : s->description = "Projection method (Chorin's method)";
1257 129 : s->version = "1.0.0";
1258 129 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT |
1259 : NS_SOLVER_CAP_IMPLICIT_VISCOUS;
1260 129 : s->backend = NS_SOLVER_BACKEND_SCALAR;
1261 :
1262 129 : s->init = projection_init;
1263 129 : s->destroy = projection_destroy;
1264 129 : s->step = projection_step;
1265 129 : s->solve = projection_solve;
1266 129 : s->apply_boundary = NULL;
1267 129 : s->compute_dt = NULL;
1268 :
1269 129 : return s;
1270 : }
1271 :
1272 1 : static cfd_status_t projection_simd_step_guarded(ns_solver_t* solver, flow_field* field,
1273 : const grid* grid, const ns_solver_params_t* params,
1274 : ns_solver_stats_t* stats) {
1275 1 : return projection_simd_step(solver, field, grid, params, stats);
1276 : }
1277 :
1278 0 : static cfd_status_t projection_simd_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1279 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1280 0 : if (!solver || !field || !grid || !params) {
1281 : return CFD_ERROR_INVALID;
1282 : }
1283 : // Use the step function which utilizes the persistent context
1284 0 : for (int i = 0; i < params->max_iter; i++) {
1285 0 : cfd_status_t status = projection_simd_step(solver, field, grid, params,
1286 : NULL); // Pass NULL stats for individual steps
1287 0 : if (status != CFD_SUCCESS) {
1288 0 : return status;
1289 : }
1290 : }
1291 :
1292 0 : if (stats) {
1293 0 : stats->iterations = params->max_iter;
1294 : // Compute max velocity/pressure
1295 0 : double max_vel = 0.0;
1296 0 : double max_p = 0.0;
1297 0 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1298 0 : stats->max_velocity = max_vel;
1299 0 : stats->max_pressure = max_p;
1300 0 : stats->max_temperature = compute_max_temperature(field);
1301 0 : stats->max_nu_t = compute_max_nu_t(field);
1302 : }
1303 : return CFD_SUCCESS;
1304 : }
1305 :
1306 30 : static ns_solver_t* create_projection_optimized_solver(void) {
1307 30 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1308 30 : if (!s) {
1309 : return NULL;
1310 : }
1311 :
1312 30 : s->name = NS_SOLVER_TYPE_PROJECTION_OPTIMIZED;
1313 30 : s->description = "SIMD-optimized Projection solver (AVX2)";
1314 30 : s->version = "1.0.0";
1315 30 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_SIMD;
1316 30 : s->backend = NS_SOLVER_BACKEND_SIMD;
1317 :
1318 30 : s->init = projection_simd_init;
1319 30 : s->destroy = projection_simd_destroy;
1320 30 : s->step = projection_simd_step_guarded;
1321 30 : s->solve = projection_simd_solve;
1322 30 : s->apply_boundary = NULL;
1323 30 : s->compute_dt = NULL;
1324 :
1325 30 : return s;
1326 : }
1327 :
1328 : #ifdef CFD_HAS_CUDA
1329 : /* Init for the stateless GPU explicit solvers (Euler, RK2, RK4): the GPU
1330 : * kernels implement central convection only. */
1331 : static cfd_status_t gpu_explicit_init(ns_solver_t* solver, const grid* grid,
1332 : const ns_solver_params_t* params) {
1333 : (void)solver;
1334 : (void)grid;
1335 : cfd_status_t scheme_status = ns_check_convection_scheme(params, 0);
1336 : if (scheme_status != CFD_SUCCESS) {
1337 : return scheme_status;
1338 : }
1339 :
1340 : cfd_status_t turb_status = ns_check_turbulence_model(params, 0);
1341 : if (turb_status != CFD_SUCCESS) {
1342 : return turb_status;
1343 : }
1344 : cfd_status_t pressure_bc_status = ns_check_pressure_bc(params, 0);
1345 : if (pressure_bc_status != CFD_SUCCESS) {
1346 : return pressure_bc_status;
1347 : }
1348 : return ns_check_pressure_solver(params, 0);
1349 : }
1350 :
1351 : /**
1352 : * Built-in solver: GPU-Accelerated Explicit Euler
1353 : *
1354 : * Forward-Euler momentum integrator on the device, sharing the RK GPU driver
1355 : * (solve_rk_gpu with order==1) — a genuine explicit Euler matching the scalar
1356 : * solver_explicit_euler.c (conservative_dt cap, per-step increment clamp,
1357 : * pressure-from-divergence update, periodic BCs, source + Boussinesq buoyancy,
1358 : * energy equation). Stateless like the RK2/RK4 GPU wrappers below.
1359 : */
1360 : static cfd_status_t gpu_euler_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1361 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1362 : if (stats) {
1363 : /* Euler clamps its own step; report what it really advanced by. */
1364 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
1365 : }
1366 :
1367 : (void)solver;
1368 : ns_solver_params_t step_params = *params;
1369 : step_params.max_iter = 1;
1370 : gpu_config_t cfg = gpu_config_default();
1371 : cfg.min_grid_size = 1;
1372 : cfg.min_steps = 1;
1373 : cfd_status_t rc = solve_explicit_euler_method_gpu(field, grid, &step_params, &cfg);
1374 : if (rc == CFD_SUCCESS && stats) {
1375 : stats->iterations = 1;
1376 : stats->max_temperature = compute_max_temperature(field);
1377 : stats->max_nu_t = compute_max_nu_t(field);
1378 : }
1379 : return rc;
1380 : }
1381 :
1382 : static cfd_status_t gpu_euler_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1383 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1384 : if (stats) {
1385 : /* Euler clamps its own step; report what it really advanced by. */
1386 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
1387 : }
1388 :
1389 : (void)solver;
1390 : gpu_config_t cfg = gpu_config_default();
1391 : cfg.min_grid_size = 1;
1392 : cfg.min_steps = 1;
1393 : cfd_status_t rc = solve_explicit_euler_method_gpu(field, grid, params, &cfg);
1394 : if (rc == CFD_SUCCESS && stats) {
1395 : /* solve_rk_gpu() runs exactly params->max_iter steps. */
1396 : stats->iterations = params->max_iter;
1397 : stats->max_temperature = compute_max_temperature(field);
1398 : stats->max_nu_t = compute_max_nu_t(field);
1399 : }
1400 : return rc;
1401 : }
1402 :
1403 : static ns_solver_t* create_explicit_euler_gpu_solver(void) {
1404 : /* Check if GPU is available at runtime before creating the solver */
1405 : if (!gpu_is_available()) {
1406 : cfd_set_error(CFD_ERROR_UNSUPPORTED, "CUDA GPU not available at runtime");
1407 : return NULL;
1408 : }
1409 :
1410 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1411 : if (!s) {
1412 : return NULL;
1413 : }
1414 :
1415 : s->name = NS_SOLVER_TYPE_EXPLICIT_EULER_GPU;
1416 : s->description = "GPU-accelerated explicit Euler solver (CUDA)";
1417 : s->version = "1.0.0";
1418 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_GPU;
1419 : s->backend = NS_SOLVER_BACKEND_CUDA;
1420 :
1421 : s->init = gpu_explicit_init;
1422 : s->destroy = NULL;
1423 : s->step = gpu_euler_step;
1424 : s->solve = gpu_euler_solve;
1425 : s->apply_boundary = NULL;
1426 : s->compute_dt = NULL;
1427 :
1428 : return s;
1429 : }
1430 :
1431 : /**
1432 : * Built-in solver: GPU-Accelerated Projection Method
1433 : */
1434 :
1435 : static cfd_status_t gpu_projection_init(ns_solver_t* solver, const grid* grid,
1436 : const ns_solver_params_t* params) {
1437 : (void)solver;
1438 : (void)grid;
1439 : cfd_status_t scheme_status = ns_check_convection_scheme(params, 0);
1440 : if (scheme_status != CFD_SUCCESS) {
1441 : return scheme_status;
1442 : }
1443 :
1444 : cfd_status_t turb_status = ns_check_turbulence_model(params, 0);
1445 : if (turb_status != CFD_SUCCESS) {
1446 : return turb_status;
1447 : }
1448 : cfd_status_t pressure_bc_status = ns_check_pressure_bc(params, 0);
1449 : if (pressure_bc_status != CFD_SUCCESS) {
1450 : return pressure_bc_status;
1451 : }
1452 :
1453 : cfd_status_t pressure_solver_status = ns_check_pressure_solver(params, 0);
1454 : if (pressure_solver_status != CFD_SUCCESS) {
1455 : return pressure_solver_status;
1456 : }
1457 : return CFD_SUCCESS;
1458 : }
1459 :
1460 : static cfd_status_t gpu_projection_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1461 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1462 : (void)solver;
1463 : /* The GPU projection backend implements the energy equation (advection-
1464 : * diffusion + Boussinesq buoyancy + per-face thermal BCs). Heat-source
1465 : * callbacks and unsupported thermal BC types are rejected inside
1466 : * solve_projection_method_gpu. */
1467 : ns_solver_params_t step_params = *params;
1468 : step_params.max_iter = 1;
1469 : /* Override thresholds to allow single-step GPU execution on small grids */
1470 : gpu_config_t cfg = gpu_config_default();
1471 : cfg.min_grid_size = 1;
1472 : cfg.min_steps = 1;
1473 : cfd_status_t rc = solve_projection_method_gpu(field, grid, &step_params, &cfg);
1474 : if (rc == CFD_SUCCESS && stats) {
1475 : stats->max_temperature = compute_max_temperature(field);
1476 : stats->max_nu_t = compute_max_nu_t(field);
1477 : }
1478 : return rc;
1479 : }
1480 :
1481 : static cfd_status_t gpu_projection_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1482 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1483 : (void)solver;
1484 : /* Override thresholds to allow GPU execution on small grids */
1485 : gpu_config_t cfg = gpu_config_default();
1486 : cfg.min_grid_size = 1;
1487 : cfg.min_steps = 1;
1488 : cfd_status_t rc = solve_projection_method_gpu(field, grid, params, &cfg);
1489 : if (rc == CFD_SUCCESS && stats) {
1490 : stats->max_temperature = compute_max_temperature(field);
1491 : stats->max_nu_t = compute_max_nu_t(field);
1492 : }
1493 : return rc;
1494 : }
1495 :
1496 : static ns_solver_t* create_projection_gpu_solver(void) {
1497 : /* Check if GPU is available at runtime before creating the solver */
1498 : if (!gpu_is_available()) {
1499 : cfd_set_error(CFD_ERROR_UNSUPPORTED, "CUDA GPU not available at runtime");
1500 : return NULL;
1501 : }
1502 :
1503 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1504 : if (!s) {
1505 : return NULL;
1506 : }
1507 :
1508 : s->name = NS_SOLVER_TYPE_PROJECTION_GPU;
1509 : s->description = "GPU-accelerated projection method with Conjugate Gradient pressure solve (CUDA)";
1510 : s->version = "1.0.0";
1511 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_GPU;
1512 : s->backend = NS_SOLVER_BACKEND_CUDA;
1513 :
1514 : s->init = gpu_projection_init; // Rejects non-default pressure_solver
1515 : s->destroy = NULL;
1516 : s->step = gpu_projection_step;
1517 : s->solve = gpu_projection_solve;
1518 : s->apply_boundary = NULL;
1519 : s->compute_dt = NULL;
1520 :
1521 : return s;
1522 : }
1523 :
1524 : /**
1525 : * Built-in solvers: GPU-accelerated RK2 / RK4 explicit time integrators.
1526 : * Stateless (init/destroy == NULL) — each step/solve uploads, runs the RK
1527 : * kernels, and downloads. The driver implements the energy equation, Boussinesq
1528 : * buoyancy, and thermal BCs; host source/heat-source callbacks and non-uniform
1529 : * grids are rejected with CFD_ERROR_UNSUPPORTED.
1530 : */
1531 :
1532 : static cfd_status_t gpu_rk2_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1533 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1534 : (void)solver;
1535 : ns_solver_params_t step_params = *params;
1536 : step_params.max_iter = 1;
1537 : gpu_config_t cfg = gpu_config_default();
1538 : cfg.min_grid_size = 1;
1539 : cfg.min_steps = 1;
1540 : cfd_status_t rc = solve_rk2_method_gpu(field, grid, &step_params, &cfg);
1541 : if (rc == CFD_SUCCESS && stats) {
1542 : stats->iterations = 1;
1543 : stats->max_temperature = compute_max_temperature(field);
1544 : stats->max_nu_t = compute_max_nu_t(field);
1545 : }
1546 : return rc;
1547 : }
1548 :
1549 : static cfd_status_t gpu_rk2_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1550 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1551 : (void)solver;
1552 : gpu_config_t cfg = gpu_config_default();
1553 : cfg.min_grid_size = 1;
1554 : cfg.min_steps = 1;
1555 : cfd_status_t rc = solve_rk2_method_gpu(field, grid, params, &cfg);
1556 : if (rc == CFD_SUCCESS && stats) {
1557 : stats->iterations = params->max_iter;
1558 : stats->max_temperature = compute_max_temperature(field);
1559 : stats->max_nu_t = compute_max_nu_t(field);
1560 : }
1561 : return rc;
1562 : }
1563 :
1564 : static cfd_status_t gpu_rk4_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1565 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1566 : (void)solver;
1567 : ns_solver_params_t step_params = *params;
1568 : step_params.max_iter = 1;
1569 : gpu_config_t cfg = gpu_config_default();
1570 : cfg.min_grid_size = 1;
1571 : cfg.min_steps = 1;
1572 : cfd_status_t rc = solve_rk4_method_gpu(field, grid, &step_params, &cfg);
1573 : if (rc == CFD_SUCCESS && stats) {
1574 : stats->iterations = 1;
1575 : stats->max_temperature = compute_max_temperature(field);
1576 : stats->max_nu_t = compute_max_nu_t(field);
1577 : }
1578 : return rc;
1579 : }
1580 :
1581 : static cfd_status_t gpu_rk4_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1582 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1583 : (void)solver;
1584 : gpu_config_t cfg = gpu_config_default();
1585 : cfg.min_grid_size = 1;
1586 : cfg.min_steps = 1;
1587 : cfd_status_t rc = solve_rk4_method_gpu(field, grid, params, &cfg);
1588 : if (rc == CFD_SUCCESS && stats) {
1589 : stats->iterations = params->max_iter;
1590 : stats->max_temperature = compute_max_temperature(field);
1591 : stats->max_nu_t = compute_max_nu_t(field);
1592 : }
1593 : return rc;
1594 : }
1595 :
1596 : static ns_solver_t* create_rk2_gpu_solver(void) {
1597 : if (!gpu_is_available()) {
1598 : cfd_set_error(CFD_ERROR_UNSUPPORTED, "CUDA GPU not available at runtime");
1599 : return NULL;
1600 : }
1601 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1602 : if (!s) {
1603 : return NULL;
1604 : }
1605 : s->name = NS_SOLVER_TYPE_RK2_GPU;
1606 : s->description = "GPU-accelerated RK2 (Heun's method) explicit time integration (CUDA)";
1607 : s->version = "1.0.0";
1608 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_GPU;
1609 : s->backend = NS_SOLVER_BACKEND_CUDA;
1610 : s->init = gpu_explicit_init;
1611 : s->destroy = NULL;
1612 : s->step = gpu_rk2_step;
1613 : s->solve = gpu_rk2_solve;
1614 : s->apply_boundary = NULL;
1615 : s->compute_dt = NULL;
1616 : return s;
1617 : }
1618 :
1619 : static ns_solver_t* create_rk4_gpu_solver(void) {
1620 : if (!gpu_is_available()) {
1621 : cfd_set_error(CFD_ERROR_UNSUPPORTED, "CUDA GPU not available at runtime");
1622 : return NULL;
1623 : }
1624 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1625 : if (!s) {
1626 : return NULL;
1627 : }
1628 : s->name = NS_SOLVER_TYPE_RK4_GPU;
1629 : s->description = "GPU-accelerated RK4 (classical Runge-Kutta) explicit time integration (CUDA)";
1630 : s->version = "1.0.0";
1631 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_GPU;
1632 : s->backend = NS_SOLVER_BACKEND_CUDA;
1633 : s->init = gpu_explicit_init;
1634 : s->destroy = NULL;
1635 : s->step = gpu_rk4_step;
1636 : s->solve = gpu_rk4_solve;
1637 : s->apply_boundary = NULL;
1638 : s->compute_dt = NULL;
1639 : return s;
1640 : }
1641 : #endif /* CFD_HAS_CUDA */
1642 :
1643 : #ifdef CFD_ENABLE_OPENMP
1644 : /**
1645 : * Built-in solver: Explicit Euler OpenMP
1646 : */
1647 :
1648 258 : static cfd_status_t explicit_euler_omp_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1649 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1650 258 : if (stats) {
1651 : /* Euler clamps its own step; report what it really advanced by. */
1652 258 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
1653 : }
1654 :
1655 258 : (void)solver;
1656 258 : if (field->nx < 3 || field->ny < 3) {
1657 : return CFD_ERROR_INVALID;
1658 : }
1659 258 : ns_solver_params_t step_params = *params;
1660 258 : step_params.max_iter = 1;
1661 :
1662 258 : cfd_status_t status = explicit_euler_omp_impl(field, grid, &step_params);
1663 258 : if (status != CFD_SUCCESS) {
1664 : return status;
1665 : }
1666 :
1667 257 : if (stats) {
1668 257 : stats->iterations = 1;
1669 257 : double max_vel = 0.0, max_p = 0.0;
1670 257 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1671 257 : stats->max_velocity = max_vel;
1672 257 : stats->max_pressure = max_p;
1673 257 : stats->max_temperature = compute_max_temperature(field);
1674 514 : stats->max_nu_t = compute_max_nu_t(field);
1675 : }
1676 : return CFD_SUCCESS;
1677 : }
1678 :
1679 3 : static cfd_status_t explicit_euler_omp_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1680 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1681 3 : if (stats) {
1682 : /* Euler clamps its own step; report what it really advanced by. */
1683 3 : stats->dt_used = fmin(params->dt, NS_EULER_DT_LIMIT);
1684 : }
1685 :
1686 3 : (void)solver;
1687 3 : if (field->nx < 3 || field->ny < 3) {
1688 : return CFD_ERROR_INVALID;
1689 : }
1690 3 : cfd_status_t status = explicit_euler_omp_impl(field, grid, params);
1691 3 : if (status != CFD_SUCCESS) {
1692 : return status;
1693 : }
1694 :
1695 2 : if (stats) {
1696 2 : stats->iterations = params->max_iter;
1697 2 : double max_vel = 0.0, max_p = 0.0;
1698 2 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1699 2 : stats->max_velocity = max_vel;
1700 2 : stats->max_pressure = max_p;
1701 2 : stats->max_temperature = compute_max_temperature(field);
1702 4 : stats->max_nu_t = compute_max_nu_t(field);
1703 : }
1704 : return CFD_SUCCESS;
1705 : }
1706 :
1707 43 : static ns_solver_t* create_explicit_euler_omp_solver(void) {
1708 43 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1709 43 : if (!s) {
1710 : return NULL;
1711 : }
1712 :
1713 43 : s->name = NS_SOLVER_TYPE_EXPLICIT_EULER_OMP;
1714 43 : s->description = "OpenMP-parallelized explicit Euler solver";
1715 43 : s->version = "1.0.0";
1716 43 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_PARALLEL;
1717 43 : s->backend = NS_SOLVER_BACKEND_OMP;
1718 :
1719 43 : s->init = explicit_euler_init; // Can reuse existing init
1720 43 : s->destroy = explicit_euler_destroy; // Can reuse existing destroy
1721 43 : s->step = explicit_euler_omp_step;
1722 43 : s->solve = explicit_euler_omp_solve;
1723 43 : s->apply_boundary = NULL;
1724 43 : s->compute_dt = NULL;
1725 :
1726 43 : return s;
1727 : }
1728 :
1729 : /**
1730 : * Built-in NSSolver: RK2 OpenMP
1731 : */
1732 :
1733 257 : static cfd_status_t rk2_omp_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1734 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1735 257 : (void)solver;
1736 257 : if (field->nx < 3 || field->ny < 3) {
1737 : return CFD_ERROR_INVALID;
1738 : }
1739 257 : ns_solver_params_t step_params = *params;
1740 257 : step_params.max_iter = 1;
1741 :
1742 257 : cfd_status_t status = rk2_omp_impl(field, grid, &step_params);
1743 :
1744 257 : if (stats) {
1745 257 : stats->iterations = 1;
1746 257 : double max_vel = 0.0, max_p = 0.0;
1747 257 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1748 257 : stats->max_velocity = max_vel;
1749 257 : stats->max_pressure = max_p;
1750 257 : stats->max_temperature = compute_max_temperature(field);
1751 514 : stats->max_nu_t = compute_max_nu_t(field);
1752 : }
1753 : return status;
1754 : }
1755 :
1756 2 : static cfd_status_t rk2_omp_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1757 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1758 2 : (void)solver;
1759 2 : if (field->nx < 3 || field->ny < 3) {
1760 : return CFD_ERROR_INVALID;
1761 : }
1762 2 : cfd_status_t status = rk2_omp_impl(field, grid, params);
1763 :
1764 2 : if (stats) {
1765 2 : stats->iterations = params->max_iter;
1766 2 : double max_vel = 0.0, max_p = 0.0;
1767 2 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1768 2 : stats->max_velocity = max_vel;
1769 2 : stats->max_pressure = max_p;
1770 2 : stats->max_temperature = compute_max_temperature(field);
1771 4 : stats->max_nu_t = compute_max_nu_t(field);
1772 : }
1773 : return status;
1774 : }
1775 :
1776 38 : static ns_solver_t* create_rk2_omp_solver(void) {
1777 38 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1778 38 : if (!s) {
1779 : return NULL;
1780 : }
1781 :
1782 38 : s->name = NS_SOLVER_TYPE_RK2_OMP;
1783 38 : s->description = "OpenMP-parallelized RK2 (Heun's method)";
1784 38 : s->version = "1.0.0";
1785 38 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_PARALLEL;
1786 38 : s->backend = NS_SOLVER_BACKEND_OMP;
1787 :
1788 38 : s->init = explicit_euler_init;
1789 38 : s->destroy = explicit_euler_destroy;
1790 38 : s->step = rk2_omp_step;
1791 38 : s->solve = rk2_omp_solve;
1792 38 : s->apply_boundary = NULL;
1793 38 : s->compute_dt = NULL;
1794 :
1795 38 : return s;
1796 : }
1797 :
1798 : /**
1799 : * Built-in NSSolver: RK4 OpenMP
1800 : */
1801 :
1802 257 : static cfd_status_t rk4_omp_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1803 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1804 257 : (void)solver;
1805 257 : if (field->nx < 3 || field->ny < 3) {
1806 : return CFD_ERROR_INVALID;
1807 : }
1808 257 : ns_solver_params_t step_params = *params;
1809 257 : step_params.max_iter = 1;
1810 :
1811 257 : cfd_status_t status = rk4_omp_impl(field, grid, &step_params);
1812 :
1813 257 : if (stats) {
1814 257 : stats->iterations = 1;
1815 257 : double max_vel = 0.0, max_p = 0.0;
1816 257 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1817 257 : stats->max_velocity = max_vel;
1818 257 : stats->max_pressure = max_p;
1819 257 : stats->max_temperature = compute_max_temperature(field);
1820 514 : stats->max_nu_t = compute_max_nu_t(field);
1821 : }
1822 : return status;
1823 : }
1824 :
1825 0 : static cfd_status_t rk4_omp_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1826 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1827 0 : (void)solver;
1828 0 : if (field->nx < 3 || field->ny < 3) {
1829 : return CFD_ERROR_INVALID;
1830 : }
1831 0 : cfd_status_t status = rk4_omp_impl(field, grid, params);
1832 :
1833 0 : if (stats) {
1834 0 : stats->iterations = params->max_iter;
1835 0 : double max_vel = 0.0, max_p = 0.0;
1836 0 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1837 0 : stats->max_velocity = max_vel;
1838 0 : stats->max_pressure = max_p;
1839 0 : stats->max_temperature = compute_max_temperature(field);
1840 0 : stats->max_nu_t = compute_max_nu_t(field);
1841 : }
1842 : return status;
1843 : }
1844 :
1845 36 : static ns_solver_t* create_rk4_omp_solver(void) {
1846 36 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
1847 36 : if (!s) {
1848 : return NULL;
1849 : }
1850 :
1851 36 : s->name = NS_SOLVER_TYPE_RK4_OMP;
1852 36 : s->description = "OpenMP-parallelized RK4 (classical Runge-Kutta)";
1853 36 : s->version = "1.0.0";
1854 36 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT | NS_SOLVER_CAP_PARALLEL;
1855 36 : s->backend = NS_SOLVER_BACKEND_OMP;
1856 :
1857 36 : s->init = explicit_euler_init;
1858 36 : s->destroy = explicit_euler_destroy;
1859 36 : s->step = rk4_omp_step;
1860 36 : s->solve = rk4_omp_solve;
1861 36 : s->apply_boundary = NULL;
1862 36 : s->compute_dt = NULL;
1863 :
1864 36 : return s;
1865 : }
1866 :
1867 : /**
1868 : * Built-in NSSolver: Projection OpenMP
1869 : */
1870 :
1871 40 : static cfd_status_t projection_omp_init(ns_solver_t* solver, const grid* grid,
1872 : const ns_solver_params_t* params) {
1873 40 : if (!solver || !grid) {
1874 : return CFD_ERROR_INVALID;
1875 : }
1876 40 : cfd_status_t scheme_status = ns_check_convection_scheme(params, 1);
1877 40 : if (scheme_status != CFD_SUCCESS) {
1878 : return scheme_status;
1879 : }
1880 :
1881 39 : cfd_status_t turb_status = ns_check_turbulence_model(params, 1);
1882 39 : if (turb_status != CFD_SUCCESS) {
1883 : return turb_status;
1884 : }
1885 :
1886 39 : cfd_status_t pressure_bc_status = ns_check_pressure_bc(params, 1);
1887 39 : if (pressure_bc_status != CFD_SUCCESS) {
1888 : return pressure_bc_status;
1889 : }
1890 :
1891 38 : cfd_status_t pressure_solver_status = ns_check_pressure_solver(params, 1);
1892 38 : if (pressure_solver_status != CFD_SUCCESS) {
1893 : return pressure_solver_status;
1894 : }
1895 38 : projection_context* ctx = (projection_context*)cfd_calloc(1, sizeof(projection_context));
1896 38 : if (!ctx) {
1897 : return CFD_ERROR_NOMEM;
1898 : }
1899 :
1900 : /* Every pressure mode here runs on an OpenMP Poisson backend, never a scalar
1901 : * one -- falling back would serialize the Poisson solve. Building the solver
1902 : * now replaces both the multigrid grid-dimension probe and the OMP CG
1903 : * availability probe, each of which created a solver to ask a question and
1904 : * then threw the answer away. */
1905 38 : ctx->pressure_backend = POISSON_BACKEND_OMP;
1906 38 : poisson_solver_config_t cfg;
1907 38 : cfd_status_t cfg_status = ns_pressure_config(params, ctx->pressure_backend, &cfg);
1908 38 : if (cfg_status != CFD_SUCCESS) {
1909 0 : cfd_free(ctx);
1910 0 : return cfg_status;
1911 : }
1912 38 : cfd_status_t pressure_status =
1913 38 : ns_pressure_ensure(&ctx->pressure, &cfg, grid);
1914 38 : if (pressure_status != CFD_SUCCESS) {
1915 4 : cfd_free(ctx);
1916 4 : return pressure_status;
1917 : }
1918 :
1919 34 : ctx->initialized = 1;
1920 34 : solver->context = ctx;
1921 :
1922 : /* Built now for the same reason the pressure solver is: an implicit scheme
1923 : * this backend cannot run -- or one combined with a turbulence model --
1924 : * fails at init, where the caller can still choose something else. */
1925 34 : poisson_solver_t* viscous = NULL;
1926 34 : cfd_status_t viscous_status = projection_viscous(solver, grid, params, &viscous);
1927 34 : if (viscous_status != CFD_SUCCESS) {
1928 1 : projection_destroy(solver);
1929 1 : return viscous_status;
1930 : }
1931 : return CFD_SUCCESS;
1932 : }
1933 :
1934 294 : static cfd_status_t projection_omp_step(ns_solver_t* solver, flow_field* field, const grid* grid,
1935 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1936 294 : if (field->nx < 3 || field->ny < 3) {
1937 : return CFD_ERROR_INVALID;
1938 : }
1939 :
1940 294 : poisson_solver_t* pressure = NULL;
1941 294 : cfd_status_t status = projection_pressure(solver, grid, params, &pressure);
1942 294 : if (status != CFD_SUCCESS) {
1943 : return status;
1944 : }
1945 294 : poisson_solver_t* viscous = NULL;
1946 294 : status = projection_viscous(solver, grid, params, &viscous);
1947 294 : if (status != CFD_SUCCESS) {
1948 : return status;
1949 : }
1950 :
1951 293 : ns_solver_params_t step_params = *params;
1952 293 : step_params.max_iter = 1;
1953 :
1954 293 : status = solve_projection_method_omp(field, grid, &step_params, pressure, viscous);
1955 293 : if (status != CFD_SUCCESS) {
1956 : return status;
1957 : }
1958 :
1959 293 : if (stats) {
1960 293 : stats->iterations = 1;
1961 293 : double max_vel = 0.0, max_p = 0.0;
1962 293 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1963 293 : stats->max_velocity = max_vel;
1964 293 : stats->max_pressure = max_p;
1965 293 : stats->max_temperature = compute_max_temperature(field);
1966 586 : stats->max_nu_t = compute_max_nu_t(field);
1967 : }
1968 : return CFD_SUCCESS;
1969 : }
1970 :
1971 2 : static cfd_status_t projection_omp_solve(ns_solver_t* solver, flow_field* field, const grid* grid,
1972 : const ns_solver_params_t* params, ns_solver_stats_t* stats) {
1973 2 : if (field->nx < 3 || field->ny < 3) {
1974 : return CFD_ERROR_INVALID;
1975 : }
1976 :
1977 2 : poisson_solver_t* pressure = NULL;
1978 2 : cfd_status_t status = projection_pressure(solver, grid, params, &pressure);
1979 2 : if (status != CFD_SUCCESS) {
1980 : return status;
1981 : }
1982 2 : poisson_solver_t* viscous = NULL;
1983 2 : status = projection_viscous(solver, grid, params, &viscous);
1984 2 : if (status != CFD_SUCCESS) {
1985 : return status;
1986 : }
1987 :
1988 2 : status = solve_projection_method_omp(field, grid, params, pressure, viscous);
1989 2 : if (status != CFD_SUCCESS) {
1990 : return status;
1991 : }
1992 :
1993 2 : if (stats) {
1994 2 : stats->iterations = params->max_iter;
1995 2 : double max_vel = 0.0, max_p = 0.0;
1996 2 : compute_max_velocity_pressure(field, &max_vel, &max_p);
1997 2 : stats->max_velocity = max_vel;
1998 2 : stats->max_pressure = max_p;
1999 2 : stats->max_temperature = compute_max_temperature(field);
2000 4 : stats->max_nu_t = compute_max_nu_t(field);
2001 : }
2002 : return CFD_SUCCESS;
2003 : }
2004 :
2005 57 : static ns_solver_t* create_projection_omp_solver(void) {
2006 57 : ns_solver_t* s = (ns_solver_t*)cfd_calloc(1, sizeof(*s));
2007 57 : if (!s) {
2008 : return NULL;
2009 : }
2010 :
2011 57 : s->name = NS_SOLVER_TYPE_PROJECTION_OMP;
2012 57 : s->description = "OpenMP-parallelized Projection solver";
2013 57 : s->version = "1.0.0";
2014 57 : s->capabilities = NS_SOLVER_CAP_INCOMPRESSIBLE | NS_SOLVER_CAP_TRANSIENT |
2015 : NS_SOLVER_CAP_PARALLEL | NS_SOLVER_CAP_IMPLICIT_VISCOUS;
2016 :
2017 57 : s->init = projection_omp_init; // Checks OMP CG and MG pressure-solver availability
2018 57 : s->destroy = projection_destroy;
2019 57 : s->step = projection_omp_step;
2020 57 : s->solve = projection_omp_solve;
2021 57 : s->apply_boundary = NULL;
2022 57 : s->compute_dt = NULL;
2023 57 : s->backend = NS_SOLVER_BACKEND_OMP;
2024 :
2025 57 : return s;
2026 : }
2027 : #endif
2028 :
2029 : //=============================================================================
2030 : // Backend Availability API
2031 : //=============================================================================
2032 :
2033 35 : int cfd_backend_is_available(ns_solver_backend_t backend) {
2034 35 : switch (backend) {
2035 : case NS_SOLVER_BACKEND_SCALAR:
2036 : return 1; // Always available
2037 :
2038 17 : case NS_SOLVER_BACKEND_SIMD:
2039 : /* Compiled-in AND supported at runtime. cfd_has_simd() alone reports a
2040 : * backend this build may not contain (AVX2 is off by default). */
2041 17 : return ns_simd_backend_available() ? 1 : 0;
2042 :
2043 : case NS_SOLVER_BACKEND_OMP:
2044 : #ifdef CFD_ENABLE_OPENMP
2045 : return 1;
2046 : #else
2047 : return 0;
2048 : #endif
2049 :
2050 4 : case NS_SOLVER_BACKEND_CUDA:
2051 4 : return gpu_is_available();
2052 :
2053 1 : default:
2054 1 : return 0;
2055 : }
2056 : }
2057 :
2058 10 : const char* cfd_backend_get_name(ns_solver_backend_t backend) {
2059 10 : switch (backend) {
2060 : case NS_SOLVER_BACKEND_SCALAR:
2061 : return "scalar";
2062 4 : case NS_SOLVER_BACKEND_SIMD:
2063 4 : return "simd";
2064 1 : case NS_SOLVER_BACKEND_OMP:
2065 1 : return "openmp";
2066 3 : case NS_SOLVER_BACKEND_CUDA:
2067 3 : return "cuda";
2068 1 : default:
2069 1 : return "unknown";
2070 : }
2071 : }
2072 :
2073 13 : int cfd_registry_list_by_backend(ns_solver_registry_t* registry, ns_solver_backend_t backend,
2074 : const char** names, int max_count) {
2075 13 : if (!registry) {
2076 : return 0;
2077 : }
2078 :
2079 : int count = 0;
2080 :
2081 : /* If names is NULL, just count total matches (discovery mode).
2082 : * Otherwise, fill the array up to max_count and return number filled. */
2083 144 : for (int i = 0; i < registry->count; i++) {
2084 : /* Use the stored backend instead of creating temporary solvers.
2085 : * This is much more efficient and avoids side effects from factory calls. */
2086 133 : if (registry->entries[i].backend == backend) {
2087 37 : if (names == NULL) {
2088 : /* Discovery mode: count all matches */
2089 4 : count++;
2090 33 : } else if (count < max_count) {
2091 : /* Fill mode: add to array if space available */
2092 32 : names[count] = registry->entries[i].name;
2093 32 : count++;
2094 : } else {
2095 : /* Array full, stop filling but don't count more */
2096 : break;
2097 : }
2098 : }
2099 : }
2100 :
2101 : return count;
2102 : }
2103 :
2104 11 : ns_solver_t* cfd_solver_create_checked(ns_solver_registry_t* registry, const char* type_name) {
2105 11 : if (!registry || !type_name) {
2106 2 : cfd_set_error(CFD_ERROR_INVALID, "Invalid arguments for solver creation");
2107 2 : return NULL;
2108 : }
2109 :
2110 : /* Check backend availability BEFORE creating the solver */
2111 9 : ns_solver_backend_t expected_backend = infer_backend_from_type(type_name);
2112 9 : if (!cfd_backend_is_available(expected_backend)) {
2113 4 : const char* backend_name = cfd_backend_get_name(expected_backend);
2114 4 : char error_msg[128];
2115 4 : snprintf(error_msg, sizeof(error_msg),
2116 : "Backend '%s' is not available on this system", backend_name);
2117 4 : cfd_set_error(CFD_ERROR_UNSUPPORTED, error_msg);
2118 4 : return NULL;
2119 : }
2120 :
2121 : /* Now create the solver - backend is available */
2122 5 : ns_solver_t* solver = cfd_solver_create(registry, type_name);
2123 5 : if (!solver) {
2124 : /* Error already set by cfd_solver_create */
2125 : return NULL;
2126 : }
2127 :
2128 : return solver;
2129 : }
|