LCOV - code coverage report
Current view: top level - boundary - boundary_conditions_inlet_common.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 82.6 % 92 76
Test Date: 2026-10-05 04:47:18 Functions: 100.0 % 9 9

            Line data    Source code
       1              : /**
       2              :  * Inlet Boundary Conditions - Common Definitions
       3              :  *
       4              :  * Shared helper functions and data structures used by all inlet BC
       5              :  * backend implementations (scalar, OMP, AVX2, NEON).
       6              :  *
       7              :  * This header is internal and should only be included by inlet BC
       8              :  * implementation files.
       9              :  */
      10              : 
      11              : #ifndef CFD_BOUNDARY_CONDITIONS_INLET_COMMON_H
      12              : #define CFD_BOUNDARY_CONDITIONS_INLET_COMMON_H
      13              : 
      14              : #include "bc_edge_range.h"
      15              : #include "boundary_conditions_internal.h"
      16              : #include "cfd/core/indexing.h"
      17              : #include <math.h>
      18              : 
      19              : /* ============================================================================
      20              :  * Edge validation and index conversion
      21              :  *
      22              :  * bc_edge_t uses bit flags (0x01..0x20) for potential combining.
      23              :  * We convert these to sequential indices (0-5) for array lookup.
      24              :  * ============================================================================ */
      25              : 
      26              : /**
      27              :  * Check if edge value is valid (exactly one of the six valid edges).
      28              :  */
      29          157 : static inline bool bc_inlet_is_valid_edge(bc_edge_t edge) {
      30           81 :     return edge == BC_EDGE_LEFT || edge == BC_EDGE_RIGHT ||
      31              :            edge == BC_EDGE_BOTTOM || edge == BC_EDGE_TOP ||
      32              :            edge == BC_EDGE_FRONT || edge == BC_EDGE_BACK;
      33              : }
      34              : 
      35              : /**
      36              :  * Convert bc_edge_t bit flag to sequential array index (0-5).
      37              :  * BC_EDGE_LEFT (0x01) -> 0, BC_EDGE_RIGHT (0x02) -> 1,
      38              :  * BC_EDGE_BOTTOM (0x04) -> 2, BC_EDGE_TOP (0x08) -> 3,
      39              :  * BC_EDGE_FRONT (0x10) -> 4, BC_EDGE_BACK (0x20) -> 5
      40              :  *
      41              :  * Uses bit manipulation to find the position of the least significant set bit
      42              :  * by counting trailing zeros in the bit flag (clamped to the range 0-5).
      43              :  * The behavior is only defined for valid, single-bit edges; callers must
      44              :  * ensure validity via bc_inlet_is_valid_edge() before calling.
      45              :  */
      46           48 : static inline int bc_inlet_edge_to_index(bc_edge_t edge) {
      47           48 :     int idx = 0;
      48           48 :     unsigned int e = (unsigned int)edge;
      49          186 :     while ((e & 1) == 0 && idx < 5) {
      50           71 :         e >>= 1;
      51           71 :         idx++;
      52              :     }
      53           67 :     return idx;
      54              : }
      55              : 
      56              : /* ============================================================================
      57              :  * Edge sub-range (bc_inlet_set_range); the convention lives in bc_edge_range.h
      58              :  * ============================================================================ */
      59              : 
      60              : /**
      61              :  * Check the range on a config, which may have been filled in by hand rather
      62              :  * than by bc_inlet_set_range(). A z-face has no 1D position to restrict.
      63              :  */
      64           67 : static inline bool bc_inlet_range_is_valid(const bc_inlet_config_t* config, bool is_z_face) {
      65           67 :     if (!config->range.enabled) {
      66              :         return true;
      67              :     }
      68            6 :     return !is_z_face && config->range.start >= 0.0 &&
      69           13 :            config->range.start < config->range.end && config->range.end <= 1.0;
      70              : }
      71              : 
      72              : /**
      73              :  * Map node i of `count` along an x/y edge to its profile position.
      74              :  *
      75              :  * Returns false for a node outside the configured range, which the caller must
      76              :  * leave untouched. Inside it, the position is rescaled so the profile spans the
      77              :  * range rather than the whole edge.
      78              :  */
      79          843 : static inline bool bc_inlet_node_position(const bc_inlet_config_t* config,
      80              :                                           size_t i, size_t count, double* position) {
      81          843 :     double t = bc_edge_node_t(i, count);
      82          843 :     if (!config->range.enabled) {
      83          776 :         *position = t;
      84          776 :         return true;
      85              :     }
      86           67 :     return bc_edge_range_position(t, config->range.start, config->range.end, position);
      87              : }
      88              : 
      89              : /* ============================================================================
      90              :  * Table-driven edge configuration (indexed 0-5)
      91              :  * ============================================================================ */
      92              : 
      93              : /**
      94              :  * Mass flow velocity direction by edge.
      95              :  */
      96              : typedef struct {
      97              :     int u_sign;     /* Sign of u velocity for mass flow (+1 or -1, 0 if not u-direction) */
      98              :     int v_sign;     /* Sign of v velocity for mass flow (+1 or -1, 0 if not v-direction) */
      99              :     int w_sign;     /* Sign of w velocity for mass flow (+1 or -1, 0 if not w-direction) */
     100              : } bc_inlet_mass_flow_dir_t;
     101              : 
     102              : /**
     103              :  * Mass flow velocity direction by edge index (0=left, 1=right, 2=bottom, 3=top, 4=front, 5=back).
     104              :  */
     105              : static const bc_inlet_mass_flow_dir_t bc_inlet_mass_flow_dir[6] = {
     106              :     { .u_sign = +1, .v_sign =  0, .w_sign =  0 },  /* LEFT: Flow into domain (+x) */
     107              :     { .u_sign = -1, .v_sign =  0, .w_sign =  0 },  /* RIGHT: Flow into domain (-x) */
     108              :     { .u_sign =  0, .v_sign = +1, .w_sign =  0 },  /* BOTTOM: Flow into domain (+y) */
     109              :     { .u_sign =  0, .v_sign = -1, .w_sign =  0 },  /* TOP: Flow into domain (-y) */
     110              :     { .u_sign =  0, .v_sign =  0, .w_sign = -1 },  /* FRONT: Flow into domain (-z) */
     111              :     { .u_sign =  0, .v_sign =  0, .w_sign = +1 },  /* BACK: Flow into domain (+z) */
     112              : };
     113              : 
     114              : /* ============================================================================
     115              :  * Index computation functions for each edge (2D: within a single xy-plane)
     116              :  * ============================================================================ */
     117              : 
     118              : typedef size_t (*bc_inlet_idx_func_t)(size_t i, size_t nx, size_t ny);
     119              : 
     120          589 : static inline size_t bc_inlet_idx_left(size_t j, size_t nx, size_t ny) {
     121          589 :     (void)ny;
     122          589 :     return IDX_2D(0, j, nx);
     123              : }
     124              : 
     125           28 : static inline size_t bc_inlet_idx_right(size_t j, size_t nx, size_t ny) {
     126           28 :     (void)ny;
     127           28 :     return IDX_2D(nx - 1, j, nx);
     128              : }
     129              : 
     130          165 : static inline size_t bc_inlet_idx_bottom(size_t i, size_t nx, size_t ny) {
     131          165 :     (void)nx; (void)ny;
     132          165 :     return i;
     133              : }
     134              : 
     135           29 : static inline size_t bc_inlet_idx_top(size_t i, size_t nx, size_t ny) {
     136           29 :     return IDX_2D(i, ny - 1, nx);
     137              : }
     138              : 
     139              : /**
     140              :  * Edge loop configuration for table-driven boundary application.
     141              :  * For 2D x/y edges: use_ny_for_count selects the loop count.
     142              :  * For 3D z-face edges: is_z_face is set and count is nx*ny (full plane).
     143              :  */
     144              : typedef struct {
     145              :     bc_inlet_idx_func_t idx_fn;     /* Index computation function (NULL for z-faces) */
     146              :     int use_ny_for_count;           /* 1 if loop count is ny, 0 if nx (ignored for z-faces) */
     147              :     int is_z_face;                  /* 1 for front/back z-face edges */
     148              : } bc_inlet_edge_loop_t;
     149              : 
     150              : /**
     151              :  * Edge loop configuration indexed 0-5 (left, right, bottom, top, front, back).
     152              :  */
     153              : static const bc_inlet_edge_loop_t bc_inlet_edge_loops[6] = {
     154              :     { .idx_fn = bc_inlet_idx_left,   .use_ny_for_count = 1, .is_z_face = 0 },  /* LEFT */
     155              :     { .idx_fn = bc_inlet_idx_right,  .use_ny_for_count = 1, .is_z_face = 0 },  /* RIGHT */
     156              :     { .idx_fn = bc_inlet_idx_bottom, .use_ny_for_count = 0, .is_z_face = 0 },  /* BOTTOM */
     157              :     { .idx_fn = bc_inlet_idx_top,    .use_ny_for_count = 0, .is_z_face = 0 },  /* TOP */
     158              :     { .idx_fn = NULL,                .use_ny_for_count = 0, .is_z_face = 1 },  /* FRONT */
     159              :     { .idx_fn = NULL,                .use_ny_for_count = 0, .is_z_face = 1 },  /* BACK */
     160              : };
     161              : 
     162              : /* ============================================================================
     163              :  * Velocity computation helpers
     164              :  * ============================================================================ */
     165              : 
     166              : /**
     167              :  * Get base velocity components from inlet configuration.
     168              :  */
     169          796 : static inline void bc_inlet_get_base_velocity(const bc_inlet_config_t* config,
     170              :                                                double* u_base, double* v_base) {
     171          796 :     switch (config->spec_type) {
     172          712 :         case BC_INLET_SPEC_VELOCITY:
     173          712 :             *u_base = config->spec.velocity.u;
     174          712 :             *v_base = config->spec.velocity.v;
     175          712 :             break;
     176              : 
     177            8 :         case BC_INLET_SPEC_MAGNITUDE_DIR:
     178            8 :             *u_base = config->spec.magnitude_dir.magnitude * cos(config->spec.magnitude_dir.direction);
     179            8 :             *v_base = config->spec.magnitude_dir.magnitude * sin(config->spec.magnitude_dir.direction);
     180            8 :             break;
     181              : 
     182           76 :         case BC_INLET_SPEC_MASS_FLOW: {
     183              :             /* Validate edge before computing direction-dependent velocity */
     184          152 :             if (!bc_inlet_is_valid_edge(config->edge)) {
     185            0 :                 *u_base = 0.0;
     186            0 :                 *v_base = 0.0;
     187            0 :                 return;
     188              :             }
     189              :             /* For 2D per unit depth: velocity = mass_flow / (density * inlet_length)
     190              :              * where mass_flow is kg/(s*m) and density*length gives kg/m^2 */
     191           76 :             double rho_L = config->spec.mass_flow.density * config->spec.mass_flow.inlet_length;
     192           76 :             if (rho_L <= 0.0) {
     193              :                 /* Invalid density or inlet_length - return zero velocity */
     194           28 :                 *u_base = 0.0;
     195           28 :                 *v_base = 0.0;
     196           28 :                 return;
     197              :             }
     198           48 :             double avg_velocity = config->spec.mass_flow.mass_flow_rate / rho_L;
     199           48 :             int idx = bc_inlet_edge_to_index(config->edge);
     200           48 :             *u_base = avg_velocity * bc_inlet_mass_flow_dir[idx].u_sign;
     201           48 :             *v_base = avg_velocity * bc_inlet_mass_flow_dir[idx].v_sign;
     202           48 :             break;
     203              :         }
     204              : 
     205            0 :         default:
     206            0 :             *u_base = 0.0;
     207            0 :             *v_base = 0.0;
     208            0 :             break;
     209              :     }
     210              : }
     211              : 
     212              : /**
     213              :  * Apply profile shape to base velocity.
     214              :  */
     215          796 : static inline void bc_inlet_apply_profile(const bc_inlet_config_t* config,
     216              :                                            double u_base, double v_base,
     217              :                                            double position,
     218              :                                            double* u_out, double* v_out) {
     219          796 :     switch (config->profile) {
     220          406 :         case BC_INLET_PROFILE_UNIFORM:
     221          406 :             *u_out = u_base;
     222          406 :             *v_out = v_base;
     223          406 :             break;
     224              : 
     225          358 :         case BC_INLET_PROFILE_PARABOLIC: {
     226          358 :             double profile_factor = 4.0 * position * (1.0 - position);
     227          358 :             *u_out = u_base * profile_factor;
     228          358 :             *v_out = v_base * profile_factor;
     229          358 :             break;
     230              :         }
     231              : 
     232           32 :         case BC_INLET_PROFILE_CUSTOM:
     233           32 :             if (config->custom_profile != NULL) {
     234           16 :                 config->custom_profile(position, u_out, v_out, config->custom_profile_user_data);
     235              :             } else {
     236           16 :                 *u_out = u_base;
     237           16 :                 *v_out = v_base;
     238              :             }
     239              :             break;
     240              : 
     241            0 :         default:
     242            0 :             *u_out = u_base;
     243            0 :             *v_out = v_base;
     244            0 :             break;
     245              :     }
     246          796 : }
     247              : 
     248              : /**
     249              :  * Compute inlet velocity at given position (combines base velocity + profile).
     250              :  */
     251          796 : static inline void bc_inlet_compute_velocity(const bc_inlet_config_t* config,
     252              :                                               double position,
     253              :                                               double* u_out, double* v_out) {
     254          796 :     double u_base, v_base;
     255          796 :     bc_inlet_get_base_velocity(config, &u_base, &v_base);
     256          796 :     bc_inlet_apply_profile(config, u_base, v_base, position, u_out, v_out);
     257          796 : }
     258              : 
     259              : /**
     260              :  * Compute w-velocity for z-face inlets from mass flow specification.
     261              :  * For non-mass-flow specs on z-faces, returns 0.0 (u,v are set by compute_velocity).
     262              :  */
     263            1 : static inline double bc_inlet_compute_w(const bc_inlet_config_t* config) {
     264            1 :     if (config->spec_type == BC_INLET_SPEC_MASS_FLOW) {
     265            0 :         double rho_L = config->spec.mass_flow.density * config->spec.mass_flow.inlet_length;
     266            0 :         if (rho_L <= 0.0) return 0.0;
     267            0 :         double avg_velocity = config->spec.mass_flow.mass_flow_rate / rho_L;
     268            0 :         int idx = bc_inlet_edge_to_index(config->edge);
     269            0 :         return avg_velocity * bc_inlet_mass_flow_dir[idx].w_sign;
     270              :     }
     271              :     return 0.0;
     272              : }
     273              : 
     274              : #endif /* CFD_BOUNDARY_CONDITIONS_INLET_COMMON_H */
        

Generated by: LCOV version 2.0-1