Line data Source code
1 : /**
2 : * Boundary Conditions - Public API and Dispatcher
3 : *
4 : * Technology-agnostic dispatcher for boundary conditions.
5 : * This file contains ONLY:
6 : * - Public API functions
7 : * - Backend selection and dispatch logic
8 : *
9 : * The dispatcher uses function pointer tables provided by each backend.
10 : * It has NO knowledge of SIMD, OpenMP, or other technologies.
11 : *
12 : * Backend implementations are in separate folders:
13 : * - cpu/boundary_conditions_scalar.c (baseline, single-threaded)
14 : * - omp/boundary_conditions_omp.c (OpenMP, multi-threaded scalar loops)
15 : * - simd/boundary_conditions_simd.c (SIMD: AVX2 on x86, NEON on ARM)
16 : */
17 :
18 : #include "boundary_conditions_internal.h"
19 : #include "cfd/core/cfd_status.h"
20 : #include "cfd/core/cpu_features.h"
21 : #include "cfd/core/logging.h"
22 : #include <stdbool.h>
23 : #include <stddef.h>
24 : #include <stdio.h>
25 :
26 : /* ============================================================================
27 : * Error Handler State and Implementation
28 : * ============================================================================ */
29 :
30 : static bc_error_handler_t g_error_handler = NULL;
31 : static void* g_error_handler_user_data = NULL;
32 :
33 : /**
34 : * Default error handler - prints to stderr.
35 : */
36 0 : static void default_error_handler(bc_error_code_t error_code,
37 : const char* function,
38 : const char* message,
39 : void* user_data) {
40 0 : (void)user_data; /* Unused in default handler */
41 0 : CFD_LOG_ERROR("boundary", "BC [%d] in %s: %s", (int)error_code, function, message);
42 0 : }
43 :
44 0 : void bc_set_error_handler(bc_error_handler_t handler, void* user_data) {
45 0 : g_error_handler = handler;
46 0 : g_error_handler_user_data = user_data;
47 0 : }
48 :
49 0 : bc_error_handler_t bc_get_error_handler(void) {
50 0 : return g_error_handler;
51 : }
52 :
53 0 : void bc_report_error(bc_error_code_t error_code, const char* function, const char* message) {
54 0 : if (g_error_handler != NULL) {
55 0 : g_error_handler(error_code, function, message, g_error_handler_user_data);
56 : } else {
57 0 : default_error_handler(error_code, function, message, NULL);
58 : }
59 0 : }
60 :
61 : /* ============================================================================
62 : * Backend State and Selection
63 : * ============================================================================ */
64 :
65 : static bc_backend_t g_current_backend = BC_BACKEND_AUTO;
66 :
67 : /**
68 : * Get the implementation table for a given backend.
69 : * Returns NULL if the backend is not available.
70 : */
71 34418 : static const bc_backend_impl_t* get_backend_impl(bc_backend_t backend) {
72 34387 : switch (backend) {
73 : case BC_BACKEND_SCALAR:
74 : return &bc_impl_scalar;
75 22 : case BC_BACKEND_OMP:
76 7 : return (bc_impl_omp.apply_neumann != NULL) ? &bc_impl_omp : NULL;
77 16 : case BC_BACKEND_SIMD:
78 : /* Use runtime check - bc_impl_simd has non-NULL dispatchers,
79 : * but the underlying SIMD backend may not be available */
80 17 : return bc_simd_backend_available() ? &bc_impl_simd : NULL;
81 : case BC_BACKEND_CUDA:
82 : /* Unavailable here by design, not for want of an implementation.
83 : * The GPU boundary kernels exist and are in production use, but they
84 : * take device pointers and a CUDA stream, while bc_backend_impl_t is
85 : * a host-pointer table with no stream. Routing them through it would
86 : * mean either a host round-trip per call -- which is exactly what the
87 : * GPU solvers are built to avoid -- or reinterpreting host pointers
88 : * as device pointers, which is undefined behavior.
89 : *
90 : * Call them directly from device code via
91 : * cfd/boundary/boundary_conditions_gpu.cuh. */
92 : return NULL;
93 34373 : case BC_BACKEND_AUTO:
94 : default:
95 : /* Auto: priority SIMD > OMP > Scalar (with runtime detection) */
96 34373 : if (bc_simd_backend_available()) {
97 : return &bc_impl_simd;
98 : }
99 34373 : if (bc_impl_omp.apply_neumann != NULL) {
100 : return &bc_impl_omp;
101 : }
102 : return &bc_impl_scalar;
103 : }
104 : }
105 :
106 193 : bool bc_backend_available(bc_backend_t backend) {
107 193 : return get_backend_impl(backend) != NULL;
108 : }
109 :
110 0 : bc_backend_t bc_get_backend(void) {
111 0 : return g_current_backend;
112 : }
113 :
114 0 : const char* bc_get_backend_name(void) {
115 0 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
116 :
117 0 : if (impl == &bc_impl_simd) {
118 : /* Report which SIMD variant is active based on runtime detection.
119 : * Use enum comparison for robustness instead of string comparison. */
120 0 : cfd_simd_arch_t arch = cfd_detect_simd_arch();
121 0 : if (g_current_backend == BC_BACKEND_AUTO) {
122 : /* Auto mode selected SIMD */
123 0 : switch (arch) {
124 : case CFD_SIMD_AVX2:
125 : return "auto (simd/avx2)";
126 0 : case CFD_SIMD_NEON:
127 0 : return "auto (simd/neon)";
128 0 : default:
129 : /* Should not happen: impl == bc_impl_simd implies SIMD available */
130 0 : return "auto (simd)";
131 : }
132 : } else {
133 : /* Explicit SIMD selection */
134 0 : switch (arch) {
135 : case CFD_SIMD_AVX2:
136 : return "simd (avx2)";
137 0 : case CFD_SIMD_NEON:
138 0 : return "simd (neon)";
139 0 : default:
140 : /* Should not happen: impl == bc_impl_simd implies SIMD available */
141 0 : return "simd";
142 : }
143 : }
144 : }
145 0 : if (impl == &bc_impl_omp) {
146 0 : return (g_current_backend == BC_BACKEND_AUTO) ? "auto (omp)" : "omp";
147 : }
148 0 : return (g_current_backend == BC_BACKEND_AUTO) ? "auto (scalar)" : "scalar";
149 : }
150 :
151 179 : bool bc_set_backend(bc_backend_t backend) {
152 179 : if (!bc_backend_available(backend) && backend != BC_BACKEND_AUTO) {
153 0 : g_current_backend = backend;
154 0 : return false;
155 : }
156 179 : g_current_backend = backend;
157 179 : return true;
158 : }
159 :
160 : /* ============================================================================
161 : * Internal Dispatch Helpers
162 : *
163 : * All helpers accept 3D parameters (nz, stride_z).
164 : * 2D public API functions call these with nz=1, stride_z=0.
165 : * ============================================================================ */
166 :
167 : /** Validate 3D layout parameters.
168 : * nz must be 1 (2D) or >= 3 (true 3D); for nz > 1, stride_z must be >= nx*ny
169 : * to prevent aliasing. nz==2 is rejected as degenerate (consistent with NS solvers). */
170 1879388 : static inline bool validate_3d_layout(size_t nx, size_t ny,
171 : size_t nz, size_t stride_z) {
172 1879360 : if (nz == 0 || nz == 2) return false;
173 1879388 : if (nz > 1 && stride_z < nx * ny) return false;
174 : return true;
175 : }
176 :
177 1853211 : static cfd_status_t apply_neumann_with_backend(double* field, size_t nx, size_t ny,
178 : size_t nz, size_t stride_z,
179 : const bc_backend_impl_t* impl) {
180 1853211 : if (impl == NULL || impl->apply_neumann == NULL) {
181 : return CFD_ERROR_UNSUPPORTED;
182 : }
183 1853211 : impl->apply_neumann(field, nx, ny, nz, stride_z);
184 1853211 : return CFD_SUCCESS;
185 : }
186 :
187 26149 : static cfd_status_t apply_periodic_with_backend(double* field, size_t nx, size_t ny,
188 : size_t nz, size_t stride_z,
189 : const bc_backend_impl_t* impl) {
190 26149 : if (impl == NULL || impl->apply_periodic == NULL) {
191 : return CFD_ERROR_UNSUPPORTED;
192 : }
193 26149 : impl->apply_periodic(field, nx, ny, nz, stride_z);
194 26149 : return CFD_SUCCESS;
195 : }
196 :
197 8052 : static cfd_status_t apply_dirichlet_with_backend(double* field, size_t nx, size_t ny,
198 : size_t nz, size_t stride_z,
199 : const bc_dirichlet_values_t* values,
200 : const bc_backend_impl_t* impl) {
201 4036 : if (impl == NULL || impl->apply_dirichlet == NULL) {
202 : return CFD_ERROR_UNSUPPORTED;
203 : }
204 24 : if (!validate_3d_layout(nx, ny, nz, stride_z)) {
205 : return CFD_ERROR_INVALID;
206 : }
207 4009 : impl->apply_dirichlet(field, nx, ny, nz, stride_z, values);
208 4043 : return CFD_SUCCESS;
209 : }
210 :
211 1879360 : static cfd_status_t apply_scalar_field_bc(double* field, size_t nx, size_t ny,
212 : size_t nz, size_t stride_z,
213 : bc_type_t type, const bc_backend_impl_t* impl) {
214 1879360 : if (!field || nx < 3 || ny < 3 || !validate_3d_layout(nx, ny, nz, stride_z)) {
215 : return CFD_ERROR_INVALID;
216 : }
217 :
218 1879360 : switch (type) {
219 : case BC_TYPE_NEUMANN:
220 1853211 : return apply_neumann_with_backend(field, nx, ny, nz, stride_z, impl);
221 :
222 : case BC_TYPE_PERIODIC:
223 26149 : return apply_periodic_with_backend(field, nx, ny, nz, stride_z, impl);
224 :
225 0 : case BC_TYPE_DIRICHLET:
226 0 : cfd_warning("BC_TYPE_DIRICHLET requires bc_apply_dirichlet_*() functions with values");
227 0 : return CFD_ERROR_INVALID;
228 :
229 0 : case BC_TYPE_NOSLIP:
230 : /* No-slip is Dirichlet with all zeros - use dedicated bc_apply_noslip() */
231 0 : cfd_warning("BC_TYPE_NOSLIP requires bc_apply_noslip() for velocity fields");
232 0 : return CFD_ERROR_INVALID;
233 :
234 : /* Both are implemented on every backend, but they carry a config struct
235 : * that this type-enum path has nowhere to put -- the same reason
236 : * DIRICHLET and NOSLIP above route to their own entry points. INVALID,
237 : * not UNSUPPORTED: the capability exists, the call is using the wrong
238 : * door, and UNSUPPORTED is what tests key on to skip a missing backend. */
239 0 : case BC_TYPE_INLET:
240 0 : cfd_warning("BC_TYPE_INLET requires bc_apply_inlet() with a bc_inlet_config_t");
241 0 : return CFD_ERROR_INVALID;
242 :
243 0 : case BC_TYPE_OUTLET:
244 0 : cfd_warning("BC_TYPE_OUTLET requires bc_apply_outlet() with a bc_outlet_config_t");
245 0 : return CFD_ERROR_INVALID;
246 :
247 0 : default:
248 0 : cfd_warning("Unknown BC type requested");
249 0 : return CFD_ERROR_INVALID;
250 : }
251 : }
252 :
253 : /* ============================================================================
254 : * Public API - Global Backend (2D)
255 : * ============================================================================ */
256 :
257 30156 : cfd_status_t bc_apply_scalar(double* field, size_t nx, size_t ny, bc_type_t type) {
258 30156 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
259 30156 : return apply_scalar_field_bc(field, nx, ny, 1, 0, type, impl);
260 : }
261 :
262 0 : cfd_status_t bc_apply_velocity(double* u, double* v, size_t nx, size_t ny, bc_type_t type) {
263 0 : if (!u || !v || nx < 3 || ny < 3) {
264 : return CFD_ERROR_INVALID;
265 : }
266 0 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
267 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, 1, 0, type, impl);
268 0 : if (status != CFD_SUCCESS) {
269 : return status;
270 : }
271 0 : return apply_scalar_field_bc(v, nx, ny, 1, 0, type, impl);
272 : }
273 :
274 : /* ============================================================================
275 : * Public API - Explicit Backend Selection (2D)
276 : * ============================================================================ */
277 :
278 1849198 : cfd_status_t bc_apply_scalar_cpu(double* field, size_t nx, size_t ny, bc_type_t type) {
279 1849198 : return apply_scalar_field_bc(field, nx, ny, 1, 0, type, &bc_impl_scalar);
280 : }
281 :
282 0 : cfd_status_t bc_apply_scalar_simd(double* field, size_t nx, size_t ny, bc_type_t type) {
283 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
284 0 : if (impl == NULL) {
285 : return CFD_ERROR_UNSUPPORTED;
286 : }
287 0 : return apply_scalar_field_bc(field, nx, ny, 1, 0, type, impl);
288 : }
289 :
290 3 : cfd_status_t bc_apply_scalar_omp(double* field, size_t nx, size_t ny, bc_type_t type) {
291 3 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
292 3 : if (impl == NULL) {
293 : return CFD_ERROR_UNSUPPORTED;
294 : }
295 3 : return apply_scalar_field_bc(field, nx, ny, 1, 0, type, impl);
296 : }
297 :
298 0 : cfd_status_t bc_apply_velocity_cpu(double* u, double* v, size_t nx, size_t ny, bc_type_t type) {
299 0 : if (!u || !v || nx < 3 || ny < 3) {
300 : return CFD_ERROR_INVALID;
301 : }
302 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, 1, 0, type, &bc_impl_scalar);
303 0 : if (status != CFD_SUCCESS) {
304 : return status;
305 : }
306 0 : return apply_scalar_field_bc(v, nx, ny, 1, 0, type, &bc_impl_scalar);
307 : }
308 :
309 0 : cfd_status_t bc_apply_velocity_simd(double* u, double* v, size_t nx, size_t ny, bc_type_t type) {
310 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
311 0 : if (impl == NULL) {
312 : return CFD_ERROR_UNSUPPORTED;
313 : }
314 0 : if (!u || !v || nx < 3 || ny < 3) {
315 : return CFD_ERROR_INVALID;
316 : }
317 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, 1, 0, type, impl);
318 0 : if (status != CFD_SUCCESS) {
319 : return status;
320 : }
321 0 : return apply_scalar_field_bc(v, nx, ny, 1, 0, type, impl);
322 : }
323 :
324 0 : cfd_status_t bc_apply_velocity_omp(double* u, double* v, size_t nx, size_t ny, bc_type_t type) {
325 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
326 0 : if (impl == NULL) {
327 : return CFD_ERROR_UNSUPPORTED;
328 : }
329 0 : if (!u || !v || nx < 3 || ny < 3) {
330 : return CFD_ERROR_INVALID;
331 : }
332 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, 1, 0, type, impl);
333 0 : if (status != CFD_SUCCESS) {
334 : return status;
335 : }
336 0 : return apply_scalar_field_bc(v, nx, ny, 1, 0, type, impl);
337 : }
338 :
339 : /* ============================================================================
340 : * Public API - Dirichlet Boundary Conditions (2D)
341 : * ============================================================================ */
342 :
343 6 : cfd_status_t bc_apply_dirichlet_scalar(double* field, size_t nx, size_t ny,
344 : const bc_dirichlet_values_t* values) {
345 6 : if (!field || !values || nx < 3 || ny < 3) {
346 : return CFD_ERROR_INVALID;
347 : }
348 3 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
349 3 : return apply_dirichlet_with_backend(field, nx, ny, 1, 0, values, impl);
350 : }
351 :
352 4009 : cfd_status_t bc_apply_dirichlet_velocity(double* u, double* v, size_t nx, size_t ny,
353 : const bc_dirichlet_values_t* u_values,
354 : const bc_dirichlet_values_t* v_values) {
355 4009 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) {
356 : return CFD_ERROR_INVALID;
357 : }
358 4009 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
359 4009 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, 1, 0, u_values, impl);
360 4009 : if (status != CFD_SUCCESS) {
361 : return status;
362 : }
363 4009 : return apply_dirichlet_with_backend(v, nx, ny, 1, 0, v_values, impl);
364 : }
365 :
366 : /* Backend-specific Dirichlet implementations (2D) */
367 :
368 7 : cfd_status_t bc_apply_dirichlet_scalar_cpu(double* field, size_t nx, size_t ny,
369 : const bc_dirichlet_values_t* values) {
370 7 : if (!field || !values || nx < 3 || ny < 3) {
371 : return CFD_ERROR_INVALID;
372 : }
373 5 : return apply_dirichlet_with_backend(field, nx, ny, 1, 0, values, &bc_impl_scalar);
374 : }
375 :
376 1 : cfd_status_t bc_apply_dirichlet_scalar_simd(double* field, size_t nx, size_t ny,
377 : const bc_dirichlet_values_t* values) {
378 1 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
379 0 : if (impl == NULL) {
380 : return CFD_ERROR_UNSUPPORTED;
381 : }
382 0 : if (!field || !values || nx < 3 || ny < 3) {
383 : return CFD_ERROR_INVALID;
384 : }
385 0 : return apply_dirichlet_with_backend(field, nx, ny, 1, 0, values, impl);
386 : }
387 :
388 3 : cfd_status_t bc_apply_dirichlet_scalar_omp(double* field, size_t nx, size_t ny,
389 : const bc_dirichlet_values_t* values) {
390 3 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
391 3 : if (impl == NULL) {
392 : return CFD_ERROR_UNSUPPORTED;
393 : }
394 3 : if (!field || !values || nx < 3 || ny < 3) {
395 : return CFD_ERROR_INVALID;
396 : }
397 2 : return apply_dirichlet_with_backend(field, nx, ny, 1, 0, values, impl);
398 : }
399 :
400 0 : cfd_status_t bc_apply_dirichlet_velocity_cpu(double* u, double* v, size_t nx, size_t ny,
401 : const bc_dirichlet_values_t* u_values,
402 : const bc_dirichlet_values_t* v_values) {
403 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) {
404 : return CFD_ERROR_INVALID;
405 : }
406 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, 1, 0, u_values, &bc_impl_scalar);
407 0 : if (status != CFD_SUCCESS) {
408 : return status;
409 : }
410 0 : return apply_dirichlet_with_backend(v, nx, ny, 1, 0, v_values, &bc_impl_scalar);
411 : }
412 :
413 0 : cfd_status_t bc_apply_dirichlet_velocity_simd(double* u, double* v, size_t nx, size_t ny,
414 : const bc_dirichlet_values_t* u_values,
415 : const bc_dirichlet_values_t* v_values) {
416 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
417 0 : if (impl == NULL) {
418 : return CFD_ERROR_UNSUPPORTED;
419 : }
420 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) {
421 : return CFD_ERROR_INVALID;
422 : }
423 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, 1, 0, u_values, impl);
424 0 : if (status != CFD_SUCCESS) {
425 : return status;
426 : }
427 0 : return apply_dirichlet_with_backend(v, nx, ny, 1, 0, v_values, impl);
428 : }
429 :
430 0 : cfd_status_t bc_apply_dirichlet_velocity_omp(double* u, double* v, size_t nx, size_t ny,
431 : const bc_dirichlet_values_t* u_values,
432 : const bc_dirichlet_values_t* v_values) {
433 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
434 0 : if (impl == NULL) {
435 : return CFD_ERROR_UNSUPPORTED;
436 : }
437 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) {
438 : return CFD_ERROR_INVALID;
439 : }
440 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, 1, 0, u_values, impl);
441 0 : if (status != CFD_SUCCESS) {
442 : return status;
443 : }
444 0 : return apply_dirichlet_with_backend(v, nx, ny, 1, 0, v_values, impl);
445 : }
446 :
447 : /* ============================================================================
448 : * Public API - No-Slip Wall Boundary Conditions (2D)
449 : *
450 : * No-slip conditions set velocity to zero at all boundaries.
451 : * Implemented using Dirichlet BCs with zero values for efficiency.
452 : * ============================================================================ */
453 :
454 : /** Static zero-valued Dirichlet BC for no-slip walls */
455 : static const bc_dirichlet_values_t g_noslip_zero = {
456 : .left = 0.0,
457 : .right = 0.0,
458 : .top = 0.0,
459 : .bottom = 0.0,
460 : .front = 0.0,
461 : .back = 0.0
462 : };
463 :
464 : /** Helper: apply no-slip to velocity components using specified backend */
465 11 : static cfd_status_t apply_noslip_with_backend(double* u, double* v, double* w,
466 : size_t nx, size_t ny,
467 : size_t nz, size_t stride_z,
468 : const bc_backend_impl_t* impl) {
469 11 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, nz, stride_z, &g_noslip_zero, impl);
470 11 : if (status != CFD_SUCCESS) {
471 : return status;
472 : }
473 11 : status = apply_dirichlet_with_backend(v, nx, ny, nz, stride_z, &g_noslip_zero, impl);
474 11 : if (status != CFD_SUCCESS) {
475 : return status;
476 : }
477 11 : if (w && nz > 1) {
478 1 : status = apply_dirichlet_with_backend(w, nx, ny, nz, stride_z, &g_noslip_zero, impl);
479 : }
480 : return status;
481 : }
482 :
483 8 : cfd_status_t bc_apply_noslip(double* u, double* v, size_t nx, size_t ny) {
484 8 : if (!u || !v || nx < 3 || ny < 3) {
485 : return CFD_ERROR_INVALID;
486 : }
487 4 : return apply_noslip_with_backend(u, v, NULL, nx, ny, 1, 0, get_backend_impl(g_current_backend));
488 : }
489 :
490 4 : cfd_status_t bc_apply_noslip_cpu(double* u, double* v, size_t nx, size_t ny) {
491 4 : if (!u || !v || nx < 3 || ny < 3) {
492 : return CFD_ERROR_INVALID;
493 : }
494 4 : return apply_noslip_with_backend(u, v, NULL, nx, ny, 1, 0, &bc_impl_scalar);
495 : }
496 :
497 0 : cfd_status_t bc_apply_noslip_simd(double* u, double* v, size_t nx, size_t ny) {
498 0 : if (!u || !v || nx < 3 || ny < 3) {
499 : return CFD_ERROR_INVALID;
500 : }
501 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
502 0 : if (impl == NULL) {
503 : return CFD_ERROR_UNSUPPORTED;
504 : }
505 0 : return apply_noslip_with_backend(u, v, NULL, nx, ny, 1, 0, impl);
506 : }
507 :
508 2 : cfd_status_t bc_apply_noslip_omp(double* u, double* v, size_t nx, size_t ny) {
509 2 : if (!u || !v || nx < 3 || ny < 3) {
510 : return CFD_ERROR_INVALID;
511 : }
512 2 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
513 2 : if (impl == NULL) {
514 : return CFD_ERROR_UNSUPPORTED;
515 : }
516 2 : return apply_noslip_with_backend(u, v, NULL, nx, ny, 1, 0, impl);
517 : }
518 :
519 : /* ============================================================================
520 : * Public API - Inlet Velocity Boundary Conditions (2D)
521 : *
522 : * Inlet conditions specify velocity at inflow boundaries.
523 : * ============================================================================ */
524 :
525 : /** Helper: apply inlet BC with specified backend */
526 65 : static cfd_status_t apply_inlet_with_backend(double* u, double* v, double* w,
527 : size_t nx, size_t ny,
528 : size_t nz, size_t stride_z,
529 : const bc_inlet_config_t* config,
530 : const bc_backend_impl_t* impl) {
531 7 : if (impl == NULL || impl->apply_inlet == NULL) {
532 : return CFD_ERROR_UNSUPPORTED;
533 : }
534 2 : if (!validate_3d_layout(nx, ny, nz, stride_z)) {
535 : return CFD_ERROR_INVALID;
536 : }
537 65 : return impl->apply_inlet(u, v, w, nx, ny, nz, stride_z, config);
538 : }
539 :
540 : /* Inlet configuration factory functions */
541 :
542 51 : bc_inlet_config_t bc_inlet_config_uniform(double u_velocity, double v_velocity) {
543 51 : bc_inlet_config_t config = {0};
544 51 : config.edge = BC_EDGE_LEFT; /* Default to left boundary */
545 51 : config.profile = BC_INLET_PROFILE_UNIFORM;
546 51 : config.spec_type = BC_INLET_SPEC_VELOCITY;
547 51 : config.spec.velocity.u = u_velocity;
548 51 : config.spec.velocity.v = v_velocity;
549 51 : config.custom_profile = NULL;
550 51 : config.custom_profile_user_data = NULL;
551 51 : return config;
552 : }
553 :
554 11 : bc_inlet_config_t bc_inlet_config_parabolic(double max_velocity) {
555 11 : bc_inlet_config_t config = {0};
556 11 : config.edge = BC_EDGE_LEFT; /* Default to left boundary */
557 11 : config.profile = BC_INLET_PROFILE_PARABOLIC;
558 11 : config.spec_type = BC_INLET_SPEC_VELOCITY;
559 : /* For left boundary, parabolic flow is in +x direction */
560 11 : config.spec.velocity.u = max_velocity;
561 11 : config.spec.velocity.v = 0.0;
562 11 : config.custom_profile = NULL;
563 11 : config.custom_profile_user_data = NULL;
564 11 : return config;
565 : }
566 :
567 3 : bc_inlet_config_t bc_inlet_config_magnitude_dir(double magnitude, double direction) {
568 3 : bc_inlet_config_t config = {0};
569 3 : config.edge = BC_EDGE_LEFT; /* Default to left boundary */
570 3 : config.profile = BC_INLET_PROFILE_UNIFORM;
571 3 : config.spec_type = BC_INLET_SPEC_MAGNITUDE_DIR;
572 3 : config.spec.magnitude_dir.magnitude = magnitude;
573 3 : config.spec.magnitude_dir.direction = direction;
574 3 : config.custom_profile = NULL;
575 3 : config.custom_profile_user_data = NULL;
576 3 : return config;
577 : }
578 :
579 11 : bc_inlet_config_t bc_inlet_config_mass_flow(double mass_flow_rate, double density, double inlet_length) {
580 11 : bc_inlet_config_t config = {0};
581 11 : config.edge = BC_EDGE_LEFT; /* Default to left boundary */
582 11 : config.profile = BC_INLET_PROFILE_UNIFORM;
583 11 : config.spec_type = BC_INLET_SPEC_MASS_FLOW;
584 11 : config.spec.mass_flow.mass_flow_rate = mass_flow_rate;
585 11 : config.spec.mass_flow.density = density;
586 11 : config.spec.mass_flow.inlet_length = inlet_length;
587 11 : config.custom_profile = NULL;
588 11 : config.custom_profile_user_data = NULL;
589 11 : return config;
590 : }
591 :
592 2 : bc_inlet_config_t bc_inlet_config_custom(bc_inlet_profile_fn callback, void* user_data) {
593 2 : bc_inlet_config_t config = {0};
594 2 : config.edge = BC_EDGE_LEFT; /* Default to left boundary */
595 2 : config.profile = BC_INLET_PROFILE_CUSTOM;
596 2 : config.spec_type = BC_INLET_SPEC_VELOCITY;
597 2 : config.spec.velocity.u = 0.0;
598 2 : config.spec.velocity.v = 0.0;
599 2 : config.custom_profile = callback;
600 2 : config.custom_profile_user_data = user_data;
601 2 : return config;
602 : }
603 :
604 58 : void bc_inlet_set_edge(bc_inlet_config_t* config, bc_edge_t edge) {
605 58 : if (config != NULL) {
606 58 : config->edge = edge;
607 : }
608 58 : }
609 :
610 12 : cfd_status_t bc_inlet_set_range(bc_inlet_config_t* config, double start, double end) {
611 : /* Written as the negation of the valid case so a NaN bound is refused too */
612 12 : if (!config || !(start >= 0.0 && start < end && end <= 1.0)) {
613 : return CFD_ERROR_INVALID;
614 : }
615 5 : config->range.enabled = true;
616 5 : config->range.start = start;
617 5 : config->range.end = end;
618 5 : return CFD_SUCCESS;
619 : }
620 :
621 : /* Inlet BC application functions (2D) */
622 :
623 5 : cfd_status_t bc_apply_inlet(double* u, double* v, size_t nx, size_t ny,
624 : const bc_inlet_config_t* config) {
625 5 : if (!u || !v || !config || nx < 3 || ny < 3) {
626 : return CFD_ERROR_INVALID;
627 : }
628 5 : return apply_inlet_with_backend(u, v, NULL, nx, ny, 1, 0, config, get_backend_impl(g_current_backend));
629 : }
630 :
631 55 : cfd_status_t bc_apply_inlet_cpu(double* u, double* v, size_t nx, size_t ny,
632 : const bc_inlet_config_t* config) {
633 55 : if (!u || !v || !config || nx < 3 || ny < 3) {
634 : return CFD_ERROR_INVALID;
635 : }
636 52 : return apply_inlet_with_backend(u, v, NULL, nx, ny, 1, 0, config, &bc_impl_scalar);
637 : }
638 :
639 3 : cfd_status_t bc_apply_inlet_simd(double* u, double* v, size_t nx, size_t ny,
640 : const bc_inlet_config_t* config) {
641 3 : if (!u || !v || !config || nx < 3 || ny < 3) {
642 : return CFD_ERROR_INVALID;
643 : }
644 3 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
645 0 : if (impl == NULL) {
646 : return CFD_ERROR_UNSUPPORTED;
647 : }
648 0 : return apply_inlet_with_backend(u, v, NULL, nx, ny, 1, 0, config, impl);
649 : }
650 :
651 6 : cfd_status_t bc_apply_inlet_omp(double* u, double* v, size_t nx, size_t ny,
652 : const bc_inlet_config_t* config) {
653 6 : if (!u || !v || !config || nx < 3 || ny < 3) {
654 : return CFD_ERROR_INVALID;
655 : }
656 6 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
657 6 : if (impl == NULL) {
658 : return CFD_ERROR_UNSUPPORTED;
659 : }
660 6 : return apply_inlet_with_backend(u, v, NULL, nx, ny, 1, 0, config, impl);
661 : }
662 :
663 : /* ============================================================================
664 : * Public API - Time-Varying Inlet Boundary Conditions
665 : * ============================================================================ */
666 :
667 : /* Time-varying inlet configuration factory functions */
668 :
669 7 : bc_inlet_config_t bc_inlet_config_time_sinusoidal(
670 : double u_velocity, double v_velocity,
671 : double frequency, double amplitude, double phase, double offset) {
672 7 : bc_inlet_config_t config = bc_inlet_config_uniform(u_velocity, v_velocity);
673 7 : config.time_config.profile = BC_TIME_PROFILE_SINUSOIDAL;
674 7 : config.time_config.params.sinusoidal.frequency = frequency;
675 7 : config.time_config.params.sinusoidal.amplitude = amplitude;
676 7 : config.time_config.params.sinusoidal.phase = phase;
677 7 : config.time_config.params.sinusoidal.offset = offset;
678 7 : return config;
679 : }
680 :
681 4 : bc_inlet_config_t bc_inlet_config_time_ramp(
682 : double u_velocity, double v_velocity,
683 : double t_start, double t_end,
684 : double value_start, double value_end) {
685 4 : bc_inlet_config_t config = bc_inlet_config_uniform(u_velocity, v_velocity);
686 :
687 : // Validate that t_end > t_start to prevent division by zero
688 4 : if (t_end <= t_start) {
689 : // Swap values if they are in wrong order
690 0 : double temp = t_start;
691 0 : t_start = t_end;
692 0 : t_end = temp;
693 :
694 : // Also swap corresponding values to maintain the intended behavior
695 0 : temp = value_start;
696 0 : value_start = value_end;
697 0 : value_end = temp;
698 :
699 0 : bc_report_error(BC_ERROR_INVALID, "bc_inlet_config_time_ramp",
700 : "t_start >= t_end: parameters have been swapped to ensure t_end > t_start");
701 : }
702 :
703 4 : config.time_config.profile = BC_TIME_PROFILE_RAMP;
704 4 : config.time_config.params.ramp.t_start = t_start;
705 4 : config.time_config.params.ramp.t_end = t_end;
706 4 : config.time_config.params.ramp.value_start = value_start;
707 4 : config.time_config.params.ramp.value_end = value_end;
708 4 : return config;
709 : }
710 :
711 4 : bc_inlet_config_t bc_inlet_config_time_step(
712 : double u_velocity, double v_velocity,
713 : double t_step, double value_before, double value_after) {
714 4 : bc_inlet_config_t config = bc_inlet_config_uniform(u_velocity, v_velocity);
715 4 : config.time_config.profile = BC_TIME_PROFILE_STEP;
716 4 : config.time_config.params.step.t_step = t_step;
717 4 : config.time_config.params.step.value_before = value_before;
718 4 : config.time_config.params.step.value_after = value_after;
719 4 : return config;
720 : }
721 :
722 2 : bc_inlet_config_t bc_inlet_config_time_custom(
723 : bc_inlet_profile_time_fn callback, void* user_data) {
724 2 : bc_inlet_config_t config = {0};
725 2 : config.edge = BC_EDGE_LEFT;
726 2 : config.profile = BC_INLET_PROFILE_UNIFORM;
727 2 : config.spec_type = BC_INLET_SPEC_VELOCITY;
728 2 : config.spec.velocity.u = 1.0; /* Default base velocity */
729 2 : config.spec.velocity.v = 0.0;
730 2 : config.custom_profile_time = callback;
731 2 : config.custom_profile_time_user_data = user_data;
732 2 : return config;
733 : }
734 :
735 2 : void bc_inlet_set_time_sinusoidal(
736 : bc_inlet_config_t* config,
737 : double frequency, double amplitude, double phase, double offset) {
738 2 : if (config != NULL) {
739 2 : config->time_config.profile = BC_TIME_PROFILE_SINUSOIDAL;
740 2 : config->time_config.params.sinusoidal.frequency = frequency;
741 2 : config->time_config.params.sinusoidal.amplitude = amplitude;
742 2 : config->time_config.params.sinusoidal.phase = phase;
743 2 : config->time_config.params.sinusoidal.offset = offset;
744 : }
745 2 : }
746 :
747 2 : void bc_inlet_set_time_ramp(
748 : bc_inlet_config_t* config,
749 : double t_start, double t_end,
750 : double value_start, double value_end) {
751 2 : if (config != NULL) {
752 2 : config->time_config.profile = BC_TIME_PROFILE_RAMP;
753 2 : config->time_config.params.ramp.t_start = t_start;
754 2 : config->time_config.params.ramp.t_end = t_end;
755 2 : config->time_config.params.ramp.value_start = value_start;
756 2 : config->time_config.params.ramp.value_end = value_end;
757 : }
758 2 : }
759 :
760 1 : void bc_inlet_set_time_step(
761 : bc_inlet_config_t* config,
762 : double t_step, double value_before, double value_after) {
763 1 : if (config != NULL) {
764 1 : config->time_config.profile = BC_TIME_PROFILE_STEP;
765 1 : config->time_config.params.step.t_step = t_step;
766 1 : config->time_config.params.step.value_before = value_before;
767 1 : config->time_config.params.step.value_after = value_after;
768 : }
769 1 : }
770 :
771 : /* Time-varying inlet application functions (2D) */
772 :
773 4 : cfd_status_t bc_apply_inlet_time(
774 : double* u, double* v, size_t nx, size_t ny,
775 : const bc_inlet_config_t* config,
776 : const bc_time_context_t* time_ctx) {
777 4 : if (!u || !v || !config || nx < 3 || ny < 3) {
778 : return CFD_ERROR_INVALID;
779 : }
780 : /* If no time variation, delegate to standard inlet function */
781 4 : if (config->time_config.profile == BC_TIME_PROFILE_CONSTANT &&
782 2 : config->custom_profile_time == NULL) {
783 2 : return bc_apply_inlet(u, v, nx, ny, config);
784 : }
785 2 : return bc_apply_inlet_time_scalar_impl(u, v, NULL, nx, ny, 1, 0, config, time_ctx);
786 : }
787 :
788 17 : cfd_status_t bc_apply_inlet_time_cpu(
789 : double* u, double* v, size_t nx, size_t ny,
790 : const bc_inlet_config_t* config,
791 : const bc_time_context_t* time_ctx) {
792 17 : return bc_apply_inlet_time_scalar_impl(u, v, NULL, nx, ny, 1, 0, config, time_ctx);
793 : }
794 :
795 0 : cfd_status_t bc_apply_inlet_time_simd(
796 : double* u, double* v, size_t nx, size_t ny,
797 : const bc_inlet_config_t* config,
798 : const bc_time_context_t* time_ctx) {
799 : /* SIMD implementation not yet available, fall back to CPU */
800 0 : return bc_apply_inlet_time_scalar_impl(u, v, NULL, nx, ny, 1, 0, config, time_ctx);
801 : }
802 :
803 0 : cfd_status_t bc_apply_inlet_time_omp(
804 : double* u, double* v, size_t nx, size_t ny,
805 : const bc_inlet_config_t* config,
806 : const bc_time_context_t* time_ctx) {
807 : /* OpenMP implementation not yet available, fall back to CPU */
808 0 : return bc_apply_inlet_time_scalar_impl(u, v, NULL, nx, ny, 1, 0, config, time_ctx);
809 : }
810 :
811 : /* ============================================================================
812 : * Public API - Outlet Boundary Conditions (2D)
813 : *
814 : * Outlet conditions specify conditions at outflow boundaries.
815 : * Supports zero-gradient (Neumann) and convective outlet types.
816 : * ============================================================================ */
817 :
818 : /** Helper: apply outlet BC with specified backend */
819 61 : static cfd_status_t apply_outlet_with_backend(double* field, size_t nx, size_t ny,
820 : size_t nz, size_t stride_z,
821 : const bc_outlet_config_t* config,
822 : const bc_backend_impl_t* impl) {
823 7 : if (impl == NULL || impl->apply_outlet == NULL) {
824 : return CFD_ERROR_UNSUPPORTED;
825 : }
826 1 : if (!validate_3d_layout(nx, ny, nz, stride_z)) {
827 : return CFD_ERROR_INVALID;
828 : }
829 57 : return impl->apply_outlet(field, nx, ny, nz, stride_z, config);
830 : }
831 :
832 : /* Outlet configuration factory functions */
833 :
834 40 : bc_outlet_config_t bc_outlet_config_zero_gradient(void) {
835 40 : bc_outlet_config_t config = {0};
836 40 : config.edge = BC_EDGE_RIGHT; /* Default to right boundary */
837 40 : config.type = BC_OUTLET_ZERO_GRADIENT;
838 40 : config.advection_velocity = 0.0;
839 40 : return config;
840 : }
841 :
842 5 : bc_outlet_config_t bc_outlet_config_convective(double advection_velocity) {
843 5 : bc_outlet_config_t config = {0};
844 5 : config.edge = BC_EDGE_RIGHT; /* Default to right boundary */
845 5 : config.type = BC_OUTLET_CONVECTIVE;
846 5 : config.advection_velocity = advection_velocity;
847 5 : return config;
848 : }
849 :
850 44 : void bc_outlet_set_edge(bc_outlet_config_t* config, bc_edge_t edge) {
851 44 : if (config != NULL) {
852 44 : config->edge = edge;
853 : }
854 44 : }
855 :
856 : /* Outlet BC application functions (2D) */
857 :
858 6 : cfd_status_t bc_apply_outlet_scalar(double* field, size_t nx, size_t ny,
859 : const bc_outlet_config_t* config) {
860 6 : if (!field || !config || nx < 3 || ny < 3) {
861 : return CFD_ERROR_INVALID;
862 : }
863 6 : return apply_outlet_with_backend(field, nx, ny, 1, 0, config, get_backend_impl(g_current_backend));
864 : }
865 :
866 0 : cfd_status_t bc_apply_outlet_velocity(double* u, double* v, size_t nx, size_t ny,
867 : const bc_outlet_config_t* config) {
868 0 : if (!u || !v || !config || nx < 3 || ny < 3) {
869 : return CFD_ERROR_INVALID;
870 : }
871 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, 1, 0, config, get_backend_impl(g_current_backend));
872 0 : if (status != CFD_SUCCESS) {
873 0 : return status;
874 : }
875 0 : return apply_outlet_with_backend(v, nx, ny, 1, 0, config, get_backend_impl(g_current_backend));
876 : }
877 :
878 43 : cfd_status_t bc_apply_outlet_scalar_cpu(double* field, size_t nx, size_t ny,
879 : const bc_outlet_config_t* config) {
880 43 : if (!field || !config || nx < 3 || ny < 3) {
881 : return CFD_ERROR_INVALID;
882 : }
883 40 : return apply_outlet_with_backend(field, nx, ny, 1, 0, config, &bc_impl_scalar);
884 : }
885 :
886 6 : cfd_status_t bc_apply_outlet_scalar_simd(double* field, size_t nx, size_t ny,
887 : const bc_outlet_config_t* config) {
888 6 : if (!field || !config || nx < 3 || ny < 3) {
889 : return CFD_ERROR_INVALID;
890 : }
891 6 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
892 0 : if (impl == NULL) {
893 : return CFD_ERROR_UNSUPPORTED;
894 : }
895 0 : return apply_outlet_with_backend(field, nx, ny, 1, 0, config, impl);
896 : }
897 :
898 6 : cfd_status_t bc_apply_outlet_scalar_omp(double* field, size_t nx, size_t ny,
899 : const bc_outlet_config_t* config) {
900 6 : if (!field || !config || nx < 3 || ny < 3) {
901 : return CFD_ERROR_INVALID;
902 : }
903 6 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
904 6 : if (impl == NULL) {
905 : return CFD_ERROR_UNSUPPORTED;
906 : }
907 6 : return apply_outlet_with_backend(field, nx, ny, 1, 0, config, impl);
908 : }
909 :
910 4 : cfd_status_t bc_apply_outlet_velocity_cpu(double* u, double* v, size_t nx, size_t ny,
911 : const bc_outlet_config_t* config) {
912 4 : if (!u || !v || !config || nx < 3 || ny < 3) {
913 : return CFD_ERROR_INVALID;
914 : }
915 4 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, 1, 0, config, &bc_impl_scalar);
916 4 : if (status != CFD_SUCCESS) {
917 0 : return status;
918 : }
919 4 : return apply_outlet_with_backend(v, nx, ny, 1, 0, config, &bc_impl_scalar);
920 : }
921 :
922 0 : cfd_status_t bc_apply_outlet_velocity_simd(double* u, double* v, size_t nx, size_t ny,
923 : const bc_outlet_config_t* config) {
924 0 : if (!u || !v || !config || nx < 3 || ny < 3) {
925 : return CFD_ERROR_INVALID;
926 : }
927 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
928 0 : if (impl == NULL) {
929 : return CFD_ERROR_UNSUPPORTED;
930 : }
931 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, 1, 0, config, impl);
932 0 : if (status != CFD_SUCCESS) {
933 0 : return status;
934 : }
935 0 : return apply_outlet_with_backend(v, nx, ny, 1, 0, config, impl);
936 : }
937 :
938 0 : cfd_status_t bc_apply_outlet_velocity_omp(double* u, double* v, size_t nx, size_t ny,
939 : const bc_outlet_config_t* config) {
940 0 : if (!u || !v || !config || nx < 3 || ny < 3) {
941 : return CFD_ERROR_INVALID;
942 : }
943 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
944 0 : if (impl == NULL) {
945 : return CFD_ERROR_UNSUPPORTED;
946 : }
947 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, 1, 0, config, impl);
948 0 : if (status != CFD_SUCCESS) {
949 0 : return status;
950 : }
951 0 : return apply_outlet_with_backend(v, nx, ny, 1, 0, config, impl);
952 : }
953 :
954 : /* ============================================================================
955 : * Public API - Symmetry Boundary Conditions (2D)
956 : * ============================================================================ */
957 :
958 : /** Helper: apply symmetry BC with specified backend */
959 1 : static cfd_status_t apply_symmetry_with_backend(double* u, double* v, double* w,
960 : size_t nx, size_t ny,
961 : size_t nz, size_t stride_z,
962 : const bc_symmetry_config_t* config,
963 : const bc_backend_impl_t* impl) {
964 0 : if (impl == NULL || impl->apply_symmetry == NULL) {
965 : return CFD_ERROR_UNSUPPORTED;
966 : }
967 0 : if (!validate_3d_layout(nx, ny, nz, stride_z)) {
968 : return CFD_ERROR_INVALID;
969 : }
970 0 : return impl->apply_symmetry(u, v, w, nx, ny, nz, stride_z, config);
971 : }
972 :
973 2 : cfd_status_t bc_apply_symmetry(double* u, double* v, size_t nx, size_t ny,
974 : const bc_symmetry_config_t* config) {
975 2 : if (!u || !v || !config || nx < 3 || ny < 3) {
976 : return CFD_ERROR_INVALID;
977 : }
978 2 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
979 2 : if (impl == NULL) {
980 : return CFD_ERROR_UNSUPPORTED;
981 : }
982 : /* If selected backend doesn't support symmetry, fall back to scalar */
983 2 : if (impl->apply_symmetry == NULL) {
984 2 : return bc_apply_symmetry_scalar_impl(u, v, NULL, nx, ny, 1, 0, config);
985 : }
986 0 : return impl->apply_symmetry(u, v, NULL, nx, ny, 1, 0, config);
987 : }
988 :
989 18 : cfd_status_t bc_apply_symmetry_cpu(double* u, double* v, size_t nx, size_t ny,
990 : const bc_symmetry_config_t* config) {
991 18 : if (!u || !v || !config || nx < 3 || ny < 3) {
992 : return CFD_ERROR_INVALID;
993 : }
994 14 : return bc_apply_symmetry_scalar_impl(u, v, NULL, nx, ny, 1, 0, config);
995 : }
996 :
997 0 : cfd_status_t bc_apply_symmetry_simd(double* u, double* v, size_t nx, size_t ny,
998 : const bc_symmetry_config_t* config) {
999 0 : if (!u || !v || !config || nx < 3 || ny < 3) {
1000 : return CFD_ERROR_INVALID;
1001 : }
1002 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1003 0 : if (impl == NULL) {
1004 : return CFD_ERROR_UNSUPPORTED;
1005 : }
1006 0 : return apply_symmetry_with_backend(u, v, NULL, nx, ny, 1, 0, config, impl);
1007 : }
1008 :
1009 1 : cfd_status_t bc_apply_symmetry_omp(double* u, double* v, size_t nx, size_t ny,
1010 : const bc_symmetry_config_t* config) {
1011 1 : if (!u || !v || !config || nx < 3 || ny < 3) {
1012 : return CFD_ERROR_INVALID;
1013 : }
1014 1 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1015 1 : if (impl == NULL) {
1016 : return CFD_ERROR_UNSUPPORTED;
1017 : }
1018 1 : return apply_symmetry_with_backend(u, v, NULL, nx, ny, 1, 0, config, impl);
1019 : }
1020 :
1021 : /* ============================================================================
1022 : * Public API - 3D Boundary Conditions
1023 : *
1024 : * These functions support 3D fields with z-dimension.
1025 : * Fields are stored as contiguous z-planes: field[k * stride_z + j * nx + i].
1026 : * ============================================================================ */
1027 :
1028 : /* --- 3D Scalar / Velocity Field BCs --- */
1029 :
1030 3 : cfd_status_t bc_apply_scalar_3d(double* field, size_t nx, size_t ny,
1031 : size_t nz, size_t stride_z, bc_type_t type) {
1032 3 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
1033 3 : return apply_scalar_field_bc(field, nx, ny, nz, stride_z, type, impl);
1034 : }
1035 :
1036 0 : cfd_status_t bc_apply_velocity_3d(double* u, double* v, double* w,
1037 : size_t nx, size_t ny,
1038 : size_t nz, size_t stride_z, bc_type_t type) {
1039 0 : if (!u || !v || nx < 3 || ny < 3) {
1040 : return CFD_ERROR_INVALID;
1041 : }
1042 0 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
1043 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, nz, stride_z, type, impl);
1044 0 : if (status != CFD_SUCCESS) {
1045 : return status;
1046 : }
1047 0 : status = apply_scalar_field_bc(v, nx, ny, nz, stride_z, type, impl);
1048 0 : if (status != CFD_SUCCESS) {
1049 : return status;
1050 : }
1051 0 : if (w && nz > 1) {
1052 0 : status = apply_scalar_field_bc(w, nx, ny, nz, stride_z, type, impl);
1053 : }
1054 : return status;
1055 : }
1056 :
1057 0 : cfd_status_t bc_apply_scalar_cpu_3d(double* field, size_t nx, size_t ny,
1058 : size_t nz, size_t stride_z, bc_type_t type) {
1059 0 : return apply_scalar_field_bc(field, nx, ny, nz, stride_z, type, &bc_impl_scalar);
1060 : }
1061 :
1062 0 : cfd_status_t bc_apply_scalar_simd_3d(double* field, size_t nx, size_t ny,
1063 : size_t nz, size_t stride_z, bc_type_t type) {
1064 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1065 0 : if (impl == NULL) {
1066 : return CFD_ERROR_UNSUPPORTED;
1067 : }
1068 0 : return apply_scalar_field_bc(field, nx, ny, nz, stride_z, type, impl);
1069 : }
1070 :
1071 0 : cfd_status_t bc_apply_scalar_omp_3d(double* field, size_t nx, size_t ny,
1072 : size_t nz, size_t stride_z, bc_type_t type) {
1073 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1074 0 : if (impl == NULL) {
1075 : return CFD_ERROR_UNSUPPORTED;
1076 : }
1077 0 : return apply_scalar_field_bc(field, nx, ny, nz, stride_z, type, impl);
1078 : }
1079 :
1080 0 : cfd_status_t bc_apply_velocity_cpu_3d(double* u, double* v, double* w,
1081 : size_t nx, size_t ny,
1082 : size_t nz, size_t stride_z, bc_type_t type) {
1083 0 : if (!u || !v || nx < 3 || ny < 3) {
1084 : return CFD_ERROR_INVALID;
1085 : }
1086 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, nz, stride_z, type, &bc_impl_scalar);
1087 0 : if (status != CFD_SUCCESS) return status;
1088 0 : status = apply_scalar_field_bc(v, nx, ny, nz, stride_z, type, &bc_impl_scalar);
1089 0 : if (status != CFD_SUCCESS) return status;
1090 0 : if (w && nz > 1) {
1091 0 : status = apply_scalar_field_bc(w, nx, ny, nz, stride_z, type, &bc_impl_scalar);
1092 : }
1093 : return status;
1094 : }
1095 :
1096 0 : cfd_status_t bc_apply_velocity_simd_3d(double* u, double* v, double* w,
1097 : size_t nx, size_t ny,
1098 : size_t nz, size_t stride_z, bc_type_t type) {
1099 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1100 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1101 0 : if (!u || !v || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1102 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, nz, stride_z, type, impl);
1103 0 : if (status != CFD_SUCCESS) return status;
1104 0 : status = apply_scalar_field_bc(v, nx, ny, nz, stride_z, type, impl);
1105 0 : if (status != CFD_SUCCESS) return status;
1106 0 : if (w && nz > 1) {
1107 0 : status = apply_scalar_field_bc(w, nx, ny, nz, stride_z, type, impl);
1108 : }
1109 : return status;
1110 : }
1111 :
1112 0 : cfd_status_t bc_apply_velocity_omp_3d(double* u, double* v, double* w,
1113 : size_t nx, size_t ny,
1114 : size_t nz, size_t stride_z, bc_type_t type) {
1115 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1116 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1117 0 : if (!u || !v || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1118 0 : cfd_status_t status = apply_scalar_field_bc(u, nx, ny, nz, stride_z, type, impl);
1119 0 : if (status != CFD_SUCCESS) return status;
1120 0 : status = apply_scalar_field_bc(v, nx, ny, nz, stride_z, type, impl);
1121 0 : if (status != CFD_SUCCESS) return status;
1122 0 : if (w && nz > 1) {
1123 0 : status = apply_scalar_field_bc(w, nx, ny, nz, stride_z, type, impl);
1124 : }
1125 : return status;
1126 : }
1127 :
1128 : /* --- 3D Dirichlet BCs --- */
1129 :
1130 1 : cfd_status_t bc_apply_dirichlet_scalar_3d(double* field, size_t nx, size_t ny,
1131 : size_t nz, size_t stride_z,
1132 : const bc_dirichlet_values_t* values) {
1133 1 : if (!field || !values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1134 1 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
1135 1 : return apply_dirichlet_with_backend(field, nx, ny, nz, stride_z, values, impl);
1136 : }
1137 :
1138 0 : cfd_status_t bc_apply_dirichlet_velocity_3d(double* u, double* v, double* w,
1139 : size_t nx, size_t ny,
1140 : size_t nz, size_t stride_z,
1141 : const bc_dirichlet_values_t* u_values,
1142 : const bc_dirichlet_values_t* v_values,
1143 : const bc_dirichlet_values_t* w_values) {
1144 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1145 0 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
1146 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, nz, stride_z, u_values, impl);
1147 0 : if (status != CFD_SUCCESS) return status;
1148 0 : status = apply_dirichlet_with_backend(v, nx, ny, nz, stride_z, v_values, impl);
1149 0 : if (status != CFD_SUCCESS) return status;
1150 0 : if (w && w_values && nz > 1) {
1151 0 : status = apply_dirichlet_with_backend(w, nx, ny, nz, stride_z, w_values, impl);
1152 : }
1153 : return status;
1154 : }
1155 :
1156 0 : cfd_status_t bc_apply_dirichlet_scalar_cpu_3d(double* field, size_t nx, size_t ny,
1157 : size_t nz, size_t stride_z,
1158 : const bc_dirichlet_values_t* values) {
1159 0 : if (!field || !values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1160 0 : return apply_dirichlet_with_backend(field, nx, ny, nz, stride_z, values, &bc_impl_scalar);
1161 : }
1162 :
1163 0 : cfd_status_t bc_apply_dirichlet_scalar_simd_3d(double* field, size_t nx, size_t ny,
1164 : size_t nz, size_t stride_z,
1165 : const bc_dirichlet_values_t* values) {
1166 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1167 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1168 0 : if (!field || !values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1169 0 : return apply_dirichlet_with_backend(field, nx, ny, nz, stride_z, values, impl);
1170 : }
1171 :
1172 0 : cfd_status_t bc_apply_dirichlet_scalar_omp_3d(double* field, size_t nx, size_t ny,
1173 : size_t nz, size_t stride_z,
1174 : const bc_dirichlet_values_t* values) {
1175 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1176 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1177 0 : if (!field || !values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1178 0 : return apply_dirichlet_with_backend(field, nx, ny, nz, stride_z, values, impl);
1179 : }
1180 :
1181 0 : cfd_status_t bc_apply_dirichlet_velocity_cpu_3d(double* u, double* v, double* w,
1182 : size_t nx, size_t ny,
1183 : size_t nz, size_t stride_z,
1184 : const bc_dirichlet_values_t* u_values,
1185 : const bc_dirichlet_values_t* v_values,
1186 : const bc_dirichlet_values_t* w_values) {
1187 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1188 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, nz, stride_z, u_values, &bc_impl_scalar);
1189 0 : if (status != CFD_SUCCESS) return status;
1190 0 : status = apply_dirichlet_with_backend(v, nx, ny, nz, stride_z, v_values, &bc_impl_scalar);
1191 0 : if (status != CFD_SUCCESS) return status;
1192 0 : if (w && w_values && nz > 1) {
1193 0 : status = apply_dirichlet_with_backend(w, nx, ny, nz, stride_z, w_values, &bc_impl_scalar);
1194 : }
1195 : return status;
1196 : }
1197 :
1198 0 : cfd_status_t bc_apply_dirichlet_velocity_simd_3d(double* u, double* v, double* w,
1199 : size_t nx, size_t ny,
1200 : size_t nz, size_t stride_z,
1201 : const bc_dirichlet_values_t* u_values,
1202 : const bc_dirichlet_values_t* v_values,
1203 : const bc_dirichlet_values_t* w_values) {
1204 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1205 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1206 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1207 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, nz, stride_z, u_values, impl);
1208 0 : if (status != CFD_SUCCESS) return status;
1209 0 : status = apply_dirichlet_with_backend(v, nx, ny, nz, stride_z, v_values, impl);
1210 0 : if (status != CFD_SUCCESS) return status;
1211 0 : if (w && w_values && nz > 1) {
1212 0 : status = apply_dirichlet_with_backend(w, nx, ny, nz, stride_z, w_values, impl);
1213 : }
1214 : return status;
1215 : }
1216 :
1217 0 : cfd_status_t bc_apply_dirichlet_velocity_omp_3d(double* u, double* v, double* w,
1218 : size_t nx, size_t ny,
1219 : size_t nz, size_t stride_z,
1220 : const bc_dirichlet_values_t* u_values,
1221 : const bc_dirichlet_values_t* v_values,
1222 : const bc_dirichlet_values_t* w_values) {
1223 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1224 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1225 0 : if (!u || !v || !u_values || !v_values || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1226 0 : cfd_status_t status = apply_dirichlet_with_backend(u, nx, ny, nz, stride_z, u_values, impl);
1227 0 : if (status != CFD_SUCCESS) return status;
1228 0 : status = apply_dirichlet_with_backend(v, nx, ny, nz, stride_z, v_values, impl);
1229 0 : if (status != CFD_SUCCESS) return status;
1230 0 : if (w && w_values && nz > 1) {
1231 0 : status = apply_dirichlet_with_backend(w, nx, ny, nz, stride_z, w_values, impl);
1232 : }
1233 : return status;
1234 : }
1235 :
1236 : /* --- 3D No-Slip Wall BCs --- */
1237 :
1238 1 : cfd_status_t bc_apply_noslip_3d(double* u, double* v, double* w,
1239 : size_t nx, size_t ny,
1240 : size_t nz, size_t stride_z) {
1241 1 : if (!u || !v || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1242 1 : return apply_noslip_with_backend(u, v, w, nx, ny, nz, stride_z, get_backend_impl(g_current_backend));
1243 : }
1244 :
1245 0 : cfd_status_t bc_apply_noslip_cpu_3d(double* u, double* v, double* w,
1246 : size_t nx, size_t ny,
1247 : size_t nz, size_t stride_z) {
1248 0 : if (!u || !v || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1249 0 : return apply_noslip_with_backend(u, v, w, nx, ny, nz, stride_z, &bc_impl_scalar);
1250 : }
1251 :
1252 0 : cfd_status_t bc_apply_noslip_simd_3d(double* u, double* v, double* w,
1253 : size_t nx, size_t ny,
1254 : size_t nz, size_t stride_z) {
1255 0 : if (!u || !v || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1256 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1257 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1258 0 : return apply_noslip_with_backend(u, v, w, nx, ny, nz, stride_z, impl);
1259 : }
1260 :
1261 0 : cfd_status_t bc_apply_noslip_omp_3d(double* u, double* v, double* w,
1262 : size_t nx, size_t ny,
1263 : size_t nz, size_t stride_z) {
1264 0 : if (!u || !v || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1265 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1266 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1267 0 : return apply_noslip_with_backend(u, v, w, nx, ny, nz, stride_z, impl);
1268 : }
1269 :
1270 : /* --- 3D Inlet BCs --- */
1271 :
1272 2 : cfd_status_t bc_apply_inlet_3d(double* u, double* v, double* w,
1273 : size_t nx, size_t ny,
1274 : size_t nz, size_t stride_z,
1275 : const bc_inlet_config_t* config) {
1276 2 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1277 2 : return apply_inlet_with_backend(u, v, w, nx, ny, nz, stride_z, config, get_backend_impl(g_current_backend));
1278 : }
1279 :
1280 0 : cfd_status_t bc_apply_inlet_cpu_3d(double* u, double* v, double* w,
1281 : size_t nx, size_t ny,
1282 : size_t nz, size_t stride_z,
1283 : const bc_inlet_config_t* config) {
1284 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1285 0 : return apply_inlet_with_backend(u, v, w, nx, ny, nz, stride_z, config, &bc_impl_scalar);
1286 : }
1287 :
1288 0 : cfd_status_t bc_apply_inlet_simd_3d(double* u, double* v, double* w,
1289 : size_t nx, size_t ny,
1290 : size_t nz, size_t stride_z,
1291 : const bc_inlet_config_t* config) {
1292 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1293 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1294 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1295 0 : return apply_inlet_with_backend(u, v, w, nx, ny, nz, stride_z, config, impl);
1296 : }
1297 :
1298 0 : cfd_status_t bc_apply_inlet_omp_3d(double* u, double* v, double* w,
1299 : size_t nx, size_t ny,
1300 : size_t nz, size_t stride_z,
1301 : const bc_inlet_config_t* config) {
1302 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1303 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1304 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1305 0 : return apply_inlet_with_backend(u, v, w, nx, ny, nz, stride_z, config, impl);
1306 : }
1307 :
1308 : /* --- 3D Time-Varying Inlet BCs --- */
1309 :
1310 0 : cfd_status_t bc_apply_inlet_time_3d(
1311 : double* u, double* v, double* w,
1312 : size_t nx, size_t ny, size_t nz, size_t stride_z,
1313 : const bc_inlet_config_t* config,
1314 : const bc_time_context_t* time_ctx) {
1315 0 : if (!u || !v || !config || nx < 3 || ny < 3 || !validate_3d_layout(nx, ny, nz, stride_z))
1316 : return CFD_ERROR_INVALID;
1317 0 : if (config->time_config.profile == BC_TIME_PROFILE_CONSTANT &&
1318 0 : config->custom_profile_time == NULL) {
1319 0 : return bc_apply_inlet_3d(u, v, w, nx, ny, nz, stride_z, config);
1320 : }
1321 0 : return bc_apply_inlet_time_scalar_impl(u, v, w, nx, ny, nz, stride_z, config, time_ctx);
1322 : }
1323 :
1324 0 : cfd_status_t bc_apply_inlet_time_cpu_3d(
1325 : double* u, double* v, double* w,
1326 : size_t nx, size_t ny, size_t nz, size_t stride_z,
1327 : const bc_inlet_config_t* config,
1328 : const bc_time_context_t* time_ctx) {
1329 0 : if (!u || !v || !config || nx < 3 || ny < 3 || !validate_3d_layout(nx, ny, nz, stride_z))
1330 : return CFD_ERROR_INVALID;
1331 0 : return bc_apply_inlet_time_scalar_impl(u, v, w, nx, ny, nz, stride_z, config, time_ctx);
1332 : }
1333 :
1334 0 : cfd_status_t bc_apply_inlet_time_simd_3d(
1335 : double* u, double* v, double* w,
1336 : size_t nx, size_t ny, size_t nz, size_t stride_z,
1337 : const bc_inlet_config_t* config,
1338 : const bc_time_context_t* time_ctx) {
1339 0 : if (!u || !v || !config || nx < 3 || ny < 3 || !validate_3d_layout(nx, ny, nz, stride_z))
1340 : return CFD_ERROR_INVALID;
1341 0 : return bc_apply_inlet_time_scalar_impl(u, v, w, nx, ny, nz, stride_z, config, time_ctx);
1342 : }
1343 :
1344 0 : cfd_status_t bc_apply_inlet_time_omp_3d(
1345 : double* u, double* v, double* w,
1346 : size_t nx, size_t ny, size_t nz, size_t stride_z,
1347 : const bc_inlet_config_t* config,
1348 : const bc_time_context_t* time_ctx) {
1349 0 : if (!u || !v || !config || nx < 3 || ny < 3 || !validate_3d_layout(nx, ny, nz, stride_z))
1350 : return CFD_ERROR_INVALID;
1351 0 : return bc_apply_inlet_time_scalar_impl(u, v, w, nx, ny, nz, stride_z, config, time_ctx);
1352 : }
1353 :
1354 : /* --- 3D Outlet BCs --- */
1355 :
1356 1 : cfd_status_t bc_apply_outlet_scalar_3d(double* field, size_t nx, size_t ny,
1357 : size_t nz, size_t stride_z,
1358 : const bc_outlet_config_t* config) {
1359 1 : if (!field || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1360 1 : return apply_outlet_with_backend(field, nx, ny, nz, stride_z, config, get_backend_impl(g_current_backend));
1361 : }
1362 :
1363 0 : cfd_status_t bc_apply_outlet_velocity_3d(double* u, double* v, double* w,
1364 : size_t nx, size_t ny,
1365 : size_t nz, size_t stride_z,
1366 : const bc_outlet_config_t* config) {
1367 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1368 0 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
1369 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, nz, stride_z, config, impl);
1370 0 : if (status != CFD_SUCCESS) return status;
1371 0 : status = apply_outlet_with_backend(v, nx, ny, nz, stride_z, config, impl);
1372 0 : if (status != CFD_SUCCESS) return status;
1373 0 : if (w && nz > 1) {
1374 0 : status = apply_outlet_with_backend(w, nx, ny, nz, stride_z, config, impl);
1375 : }
1376 : return status;
1377 : }
1378 :
1379 0 : cfd_status_t bc_apply_outlet_scalar_cpu_3d(double* field, size_t nx, size_t ny,
1380 : size_t nz, size_t stride_z,
1381 : const bc_outlet_config_t* config) {
1382 0 : if (!field || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1383 0 : return apply_outlet_with_backend(field, nx, ny, nz, stride_z, config, &bc_impl_scalar);
1384 : }
1385 :
1386 0 : cfd_status_t bc_apply_outlet_scalar_simd_3d(double* field, size_t nx, size_t ny,
1387 : size_t nz, size_t stride_z,
1388 : const bc_outlet_config_t* config) {
1389 0 : if (!field || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1390 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1391 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1392 0 : return apply_outlet_with_backend(field, nx, ny, nz, stride_z, config, impl);
1393 : }
1394 :
1395 0 : cfd_status_t bc_apply_outlet_scalar_omp_3d(double* field, size_t nx, size_t ny,
1396 : size_t nz, size_t stride_z,
1397 : const bc_outlet_config_t* config) {
1398 0 : if (!field || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1399 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1400 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1401 0 : return apply_outlet_with_backend(field, nx, ny, nz, stride_z, config, impl);
1402 : }
1403 :
1404 0 : cfd_status_t bc_apply_outlet_velocity_cpu_3d(double* u, double* v, double* w,
1405 : size_t nx, size_t ny,
1406 : size_t nz, size_t stride_z,
1407 : const bc_outlet_config_t* config) {
1408 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1409 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, nz, stride_z, config, &bc_impl_scalar);
1410 0 : if (status != CFD_SUCCESS) return status;
1411 0 : status = apply_outlet_with_backend(v, nx, ny, nz, stride_z, config, &bc_impl_scalar);
1412 0 : if (status != CFD_SUCCESS) return status;
1413 0 : if (w && nz > 1) {
1414 0 : status = apply_outlet_with_backend(w, nx, ny, nz, stride_z, config, &bc_impl_scalar);
1415 : }
1416 : return status;
1417 : }
1418 :
1419 0 : cfd_status_t bc_apply_outlet_velocity_simd_3d(double* u, double* v, double* w,
1420 : size_t nx, size_t ny,
1421 : size_t nz, size_t stride_z,
1422 : const bc_outlet_config_t* config) {
1423 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1424 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1425 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1426 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, nz, stride_z, config, impl);
1427 0 : if (status != CFD_SUCCESS) return status;
1428 0 : status = apply_outlet_with_backend(v, nx, ny, nz, stride_z, config, impl);
1429 0 : if (status != CFD_SUCCESS) return status;
1430 0 : if (w && nz > 1) {
1431 0 : status = apply_outlet_with_backend(w, nx, ny, nz, stride_z, config, impl);
1432 : }
1433 : return status;
1434 : }
1435 :
1436 0 : cfd_status_t bc_apply_outlet_velocity_omp_3d(double* u, double* v, double* w,
1437 : size_t nx, size_t ny,
1438 : size_t nz, size_t stride_z,
1439 : const bc_outlet_config_t* config) {
1440 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1441 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1442 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1443 0 : cfd_status_t status = apply_outlet_with_backend(u, nx, ny, nz, stride_z, config, impl);
1444 0 : if (status != CFD_SUCCESS) return status;
1445 0 : status = apply_outlet_with_backend(v, nx, ny, nz, stride_z, config, impl);
1446 0 : if (status != CFD_SUCCESS) return status;
1447 0 : if (w && nz > 1) {
1448 0 : status = apply_outlet_with_backend(w, nx, ny, nz, stride_z, config, impl);
1449 : }
1450 : return status;
1451 : }
1452 :
1453 : /* --- 3D Symmetry BCs --- */
1454 :
1455 1 : cfd_status_t bc_apply_symmetry_3d(double* u, double* v, double* w,
1456 : size_t nx, size_t ny,
1457 : size_t nz, size_t stride_z,
1458 : const bc_symmetry_config_t* config) {
1459 1 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1460 1 : if (!validate_3d_layout(nx, ny, nz, stride_z)) return CFD_ERROR_INVALID;
1461 1 : const bc_backend_impl_t* impl = get_backend_impl(g_current_backend);
1462 1 : if (impl == NULL || impl->apply_symmetry == NULL) {
1463 : /* Fall back to scalar implementation when backend lacks symmetry */
1464 1 : return bc_apply_symmetry_scalar_impl(u, v, w, nx, ny, nz, stride_z, config);
1465 : }
1466 0 : return impl->apply_symmetry(u, v, w, nx, ny, nz, stride_z, config);
1467 : }
1468 :
1469 0 : cfd_status_t bc_apply_symmetry_cpu_3d(double* u, double* v, double* w,
1470 : size_t nx, size_t ny,
1471 : size_t nz, size_t stride_z,
1472 : const bc_symmetry_config_t* config) {
1473 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1474 0 : return apply_symmetry_with_backend(u, v, w, nx, ny, nz, stride_z, config, &bc_impl_scalar);
1475 : }
1476 :
1477 0 : cfd_status_t bc_apply_symmetry_simd_3d(double* u, double* v, double* w,
1478 : size_t nx, size_t ny,
1479 : size_t nz, size_t stride_z,
1480 : const bc_symmetry_config_t* config) {
1481 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1482 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_SIMD);
1483 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1484 0 : return apply_symmetry_with_backend(u, v, w, nx, ny, nz, stride_z, config, impl);
1485 : }
1486 :
1487 0 : cfd_status_t bc_apply_symmetry_omp_3d(double* u, double* v, double* w,
1488 : size_t nx, size_t ny,
1489 : size_t nz, size_t stride_z,
1490 : const bc_symmetry_config_t* config) {
1491 0 : if (!u || !v || !config || nx < 3 || ny < 3) return CFD_ERROR_INVALID;
1492 0 : const bc_backend_impl_t* impl = get_backend_impl(BC_BACKEND_OMP);
1493 0 : if (impl == NULL) return CFD_ERROR_UNSUPPORTED;
1494 0 : return apply_symmetry_with_backend(u, v, w, nx, ny, nz, stride_z, config, impl);
1495 : }
|