Line data Source code
1 : /**
2 : * @file boundary_conditions_time.h
3 : * @brief Time modulation helper functions for time-varying boundary conditions
4 : *
5 : * This internal header provides inline helper functions for computing
6 : * time-dependent modulation factors for boundary conditions.
7 : */
8 :
9 : #ifndef CFD_BOUNDARY_CONDITIONS_TIME_H
10 : #define CFD_BOUNDARY_CONDITIONS_TIME_H
11 :
12 : #include "cfd/boundary/boundary_conditions.h"
13 : #include <math.h>
14 :
15 : #ifndef M_PI
16 : #define M_PI 3.14159265358979323846
17 : #endif
18 :
19 : /**
20 : * Compute sinusoidal time profile value.
21 : *
22 : * @param cfg Sinusoidal configuration
23 : * @param t Current time
24 : * @return Modulation factor: offset + amplitude * sin(2*pi*frequency*t + phase)
25 : */
26 112 : static inline double bc_time_sinusoidal_value(const bc_time_sinusoidal_t* cfg, double t) {
27 112 : return cfg->offset + cfg->amplitude * sin(2.0 * M_PI * cfg->frequency * t + cfg->phase);
28 : }
29 :
30 : /**
31 : * Compute ramp time profile value.
32 : *
33 : * @param cfg Ramp configuration
34 : * @param t Current time
35 : * @return Modulation factor: linear interpolation between value_start and value_end
36 : *
37 : * @note If t_end <= t_start (invalid configuration), returns value_end to avoid
38 : * division by zero. Callers should ensure t_start < t_end.
39 : */
40 48 : static inline double bc_time_ramp_value(const bc_time_ramp_t* cfg, double t) {
41 48 : if (t <= cfg->t_start) {
42 16 : return cfg->value_start;
43 : }
44 32 : if (t >= cfg->t_end) {
45 16 : return cfg->value_end;
46 : }
47 : /* Guard against division by zero from invalid configuration */
48 16 : if (cfg->t_end <= cfg->t_start) {
49 0 : return cfg->value_end;
50 : }
51 : /* Linear interpolation */
52 16 : double frac = (t - cfg->t_start) / (cfg->t_end - cfg->t_start);
53 16 : return cfg->value_start + frac * (cfg->value_end - cfg->value_start);
54 : }
55 :
56 : /**
57 : * Compute step time profile value.
58 : *
59 : * @param cfg Step configuration
60 : * @param t Current time
61 : * @return Modulation factor: value_before if t < t_step, else value_after
62 : */
63 48 : static inline double bc_time_step_value(const bc_time_step_t* cfg, double t) {
64 48 : return (t < cfg->t_step) ? cfg->value_before : cfg->value_after;
65 : }
66 :
67 : /**
68 : * Compute time modulation factor from a time configuration.
69 : *
70 : * This is the main entry point for getting the time-dependent multiplier
71 : * that should be applied to base velocity values.
72 : *
73 : * @param cfg Time configuration (profile type and parameters)
74 : * @param t Current simulation time
75 : * @param dt Current time step size
76 : * @return Modulation factor (1.0 for CONSTANT profile or NULL config)
77 : */
78 224 : static inline double bc_time_get_modulator(const bc_time_config_t* cfg, double t, double dt) {
79 224 : if (!cfg) {
80 : return 1.0;
81 : }
82 :
83 224 : switch (cfg->profile) {
84 : case BC_TIME_PROFILE_CONSTANT:
85 : return 1.0;
86 :
87 112 : case BC_TIME_PROFILE_SINUSOIDAL:
88 112 : return bc_time_sinusoidal_value(&cfg->params.sinusoidal, t);
89 :
90 48 : case BC_TIME_PROFILE_RAMP:
91 48 : return bc_time_ramp_value(&cfg->params.ramp, t);
92 :
93 48 : case BC_TIME_PROFILE_STEP:
94 48 : return bc_time_step_value(&cfg->params.step, t);
95 :
96 0 : case BC_TIME_PROFILE_CUSTOM:
97 0 : if (cfg->custom_fn) {
98 0 : return cfg->custom_fn(t, dt, cfg->custom_user_data);
99 : }
100 : return 1.0;
101 :
102 : default:
103 : return 1.0;
104 : }
105 : }
106 :
107 : /**
108 : * Check if an inlet configuration has time variation enabled.
109 : *
110 : * @param config Inlet configuration
111 : * @return true if time variation is configured, false otherwise
112 : */
113 : static inline bool bc_inlet_has_time_variation(const bc_inlet_config_t* config) {
114 : if (!config) {
115 : return false;
116 : }
117 : /* Has time variation if profile is not CONSTANT or if time callback is set */
118 : return (config->time_config.profile != BC_TIME_PROFILE_CONSTANT) ||
119 : (config->custom_profile_time != NULL);
120 : }
121 :
122 : #endif /* CFD_BOUNDARY_CONDITIONS_TIME_H */
|