Line data Source code
1 : /**
2 : * Inlet Boundary Conditions - Scalar (CPU) Implementation
3 : *
4 : * Baseline scalar implementation of inlet velocity boundary conditions.
5 : * No SIMD, no OpenMP - pure C loops.
6 : *
7 : * Supports:
8 : * - Uniform velocity profile
9 : * - Parabolic profile (fully-developed flow)
10 : * - Custom user-defined profiles via callback
11 : * - Velocity specification by components, magnitude+direction, or mass flow rate
12 : * - 3D z-face inlets (FRONT/BACK edges)
13 : */
14 :
15 : #include "../boundary_conditions_inlet_common.h"
16 :
17 65 : cfd_status_t bc_apply_inlet_scalar_impl(double* u, double* v, double* w,
18 : size_t nx, size_t ny,
19 : size_t nz, size_t stride_z,
20 : const bc_inlet_config_t* config) {
21 65 : if (!u || !v || !config || nx < 3 || ny < 3) {
22 : return CFD_ERROR_INVALID;
23 : }
24 :
25 116 : if (!bc_inlet_is_valid_edge(config->edge)) {
26 : return CFD_ERROR_INVALID;
27 : }
28 :
29 51 : int edge_idx = bc_inlet_edge_to_index(config->edge);
30 51 : const bc_inlet_edge_loop_t* loop = &bc_inlet_edge_loops[edge_idx];
31 70 : if (!bc_inlet_range_is_valid(config, loop->is_z_face)) {
32 : return CFD_ERROR_INVALID;
33 : }
34 :
35 48 : if (loop->is_z_face) {
36 : /* Z-face inlet (FRONT or BACK): loop over the full xy-plane */
37 3 : if (nz <= 1 || !w) {
38 2 : return CFD_ERROR_INVALID;
39 : }
40 : /* FRONT = k=nz-1, BACK = k=0 */
41 2 : size_t z_plane = (config->edge == BC_EDGE_FRONT)
42 1 : ? (nz - 1) * stride_z
43 1 : : 0;
44 1 : double w_val = bc_inlet_compute_w(config);
45 :
46 : /* For z-face inlets, 1D profiles are not meaningful.
47 : * Velocity is uniform across the plane — compute once. */
48 1 : double u_val, v_val;
49 1 : bc_inlet_compute_velocity(config, 0.5, &u_val, &v_val);
50 :
51 6 : for (size_t j = 0; j < ny; j++) {
52 30 : for (size_t i = 0; i < nx; i++) {
53 25 : size_t idx = z_plane + IDX_2D(i, j, nx);
54 25 : u[idx] = u_val;
55 25 : v[idx] = v_val;
56 25 : w[idx] = w_val;
57 : }
58 : }
59 : } else {
60 : /* X/Y-face inlet: loop over each z-plane */
61 45 : size_t count = loop->use_ny_for_count ? ny : nx;
62 :
63 90 : for (size_t k = 0; k < nz; k++) {
64 45 : size_t base = k * stride_z;
65 631 : for (size_t i = 0; i < count; i++) {
66 586 : double position;
67 586 : if (!bc_inlet_node_position(config, i, count, &position)) {
68 24 : continue;
69 : }
70 562 : double u_val, v_val;
71 562 : bc_inlet_compute_velocity(config, position, &u_val, &v_val);
72 :
73 562 : size_t idx = base + loop->idx_fn(i, nx, ny);
74 562 : u[idx] = u_val;
75 562 : v[idx] = v_val;
76 562 : if (w) {
77 0 : w[idx] = 0.0;
78 : }
79 : }
80 : }
81 : }
82 :
83 : return CFD_SUCCESS;
84 : }
|