Line data Source code
1 : /**
2 : * Time-Varying Inlet Boundary Conditions - Scalar (CPU) Implementation
3 : *
4 : * Baseline scalar implementation of time-varying inlet velocity boundary
5 : * conditions. Extends the standard inlet BC with time modulation support.
6 : *
7 : * Supports:
8 : * - All standard inlet profiles (uniform, parabolic, custom)
9 : * - Time modulation: sinusoidal, ramp, step
10 : * - Custom time-varying profile callback
11 : * - 3D z-face inlets (FRONT/BACK edges)
12 : */
13 :
14 : #include "../boundary_conditions_inlet_common.h"
15 : #include "../boundary_conditions_time.h"
16 :
17 : /**
18 : * Compute inlet velocity with time modulation at given position.
19 : *
20 : * This combines the spatial profile (uniform, parabolic, custom) with
21 : * time modulation (sinusoidal, ramp, step, custom).
22 : *
23 : * @param config Inlet configuration with time settings
24 : * @param position Normalized position along the edge [0, 1]
25 : * @param time Current simulation time
26 : * @param dt Current time step
27 : * @param u_out Output: u velocity component
28 : * @param v_out Output: v velocity component
29 : */
30 249 : static void bc_inlet_compute_velocity_time(const bc_inlet_config_t* config,
31 : double position,
32 : double time, double dt,
33 : double* u_out, double* v_out) {
34 : /* Check for custom time-varying profile callback first */
35 249 : if (config->custom_profile_time != NULL) {
36 16 : config->custom_profile_time(position, time, dt, u_out, v_out,
37 16 : config->custom_profile_time_user_data);
38 16 : return;
39 : }
40 :
41 : /* Compute base velocity from spatial profile */
42 233 : double u_base, v_base;
43 233 : bc_inlet_compute_velocity(config, position, &u_base, &v_base);
44 :
45 : /* Apply time modulation */
46 233 : double modulator = bc_time_get_modulator(&config->time_config, time, dt);
47 233 : *u_out = u_base * modulator;
48 233 : *v_out = v_base * modulator;
49 : }
50 :
51 20 : cfd_status_t bc_apply_inlet_time_scalar_impl(double* u, double* v, double* w,
52 : size_t nx, size_t ny,
53 : size_t nz, size_t stride_z,
54 : const bc_inlet_config_t* config,
55 : const bc_time_context_t* time_ctx) {
56 20 : if (!u || !v || !config || nx < 3 || ny < 3) {
57 : return CFD_ERROR_INVALID;
58 : }
59 17 : if (nz == 0 || nz == 2 || (nz > 1 && stride_z < nx * ny)) {
60 : return CFD_ERROR_INVALID;
61 : }
62 :
63 17 : if (!time_ctx) {
64 : /* If no time context provided, use time=0, dt=0 */
65 1 : bc_time_context_t default_ctx = {0.0, 0.0};
66 1 : return bc_apply_inlet_time_scalar_impl(u, v, w, nx, ny, nz, stride_z,
67 : config, &default_ctx);
68 : }
69 :
70 32 : if (!bc_inlet_is_valid_edge(config->edge)) {
71 : return CFD_ERROR_INVALID;
72 : }
73 :
74 16 : int edge_idx = bc_inlet_edge_to_index(config->edge);
75 16 : const bc_inlet_edge_loop_t* loop = &bc_inlet_edge_loops[edge_idx];
76 19 : if (!bc_inlet_range_is_valid(config, loop->is_z_face)) {
77 : return CFD_ERROR_INVALID;
78 : }
79 :
80 16 : if (loop->is_z_face) {
81 : /* Z-face inlet (FRONT or BACK): loop over the full xy-plane */
82 0 : if (nz <= 1 || !w) {
83 0 : return CFD_ERROR_INVALID;
84 : }
85 0 : size_t z_plane = (config->edge == BC_EDGE_FRONT)
86 0 : ? (nz - 1) * stride_z
87 0 : : 0;
88 0 : double w_val = bc_inlet_compute_w(config);
89 : /* Apply time modulation to w as well */
90 0 : double modulator = bc_time_get_modulator(&config->time_config,
91 0 : time_ctx->time, time_ctx->dt);
92 0 : w_val *= modulator;
93 :
94 : /* Velocity is uniform across the z-face — compute once. */
95 0 : double u_val, v_val;
96 0 : bc_inlet_compute_velocity_time(config, 0.5,
97 0 : time_ctx->time, time_ctx->dt,
98 : &u_val, &v_val);
99 :
100 0 : for (size_t j = 0; j < ny; j++) {
101 0 : for (size_t i = 0; i < nx; i++) {
102 0 : size_t idx = z_plane + IDX_2D(i, j, nx);
103 0 : u[idx] = u_val;
104 0 : v[idx] = v_val;
105 0 : w[idx] = w_val;
106 : }
107 : }
108 : } else {
109 : /* X/Y-face inlet: loop over each z-plane */
110 16 : size_t count = loop->use_ny_for_count ? ny : nx;
111 :
112 32 : for (size_t k = 0; k < nz; k++) {
113 16 : size_t base = k * stride_z;
114 273 : for (size_t i = 0; i < count; i++) {
115 257 : double position;
116 257 : if (!bc_inlet_node_position(config, i, count, &position)) {
117 8 : continue;
118 : }
119 249 : double u_val, v_val;
120 249 : bc_inlet_compute_velocity_time(config, position,
121 249 : time_ctx->time, time_ctx->dt,
122 : &u_val, &v_val);
123 :
124 249 : size_t idx = base + loop->idx_fn(i, nx, ny);
125 249 : u[idx] = u_val;
126 249 : v[idx] = v_val;
127 249 : if (w) {
128 0 : w[idx] = 0.0;
129 : }
130 : }
131 : }
132 : }
133 :
134 : return CFD_SUCCESS;
135 : }
|