-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathActivationFunction.h
More file actions
307 lines (276 loc) · 9.41 KB
/
Copy pathActivationFunction.h
File metadata and controls
307 lines (276 loc) · 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
/**
* @file ActivationFunction.h
* @brief Activation function classes for cardiac chamber models
*/
#ifndef SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_
#define SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "Parameter.h"
/**
* @brief Base class for activation functions
*
* Activation functions compute the activation value (between 0 and 1) at a
* given time point within a cardiac cycle. These are used to modulate
* chamber elastance over time.
*/
class ActivationFunction {
public:
/**
* @brief Properties of the input parameters for this activation function
* [(name, InputParameter), ...]
*/
const std::vector<std::pair<std::string, InputParameter>>
input_param_properties;
/**
* @brief Construct activation function
*
* @param cardiac_period Cardiac cycle period
* @param input_param_properties Properties of the input parameters
* [(name, InputParameter), ...] for this activation function
*/
ActivationFunction(double cardiac_period,
const std::vector<std::pair<std::string, InputParameter>>&
input_param_properties);
/**
* @brief Virtual destructor
*/
virtual ~ActivationFunction() = default;
/**
* @brief Compute activation value at given time
*
* @param time Current time
* @return Activation value between 0 and 1
*/
virtual double compute(double time) = 0;
/**
* @brief Create a default activation function from activation function type
*
* @param type_str One of: "half_cosine", "piecewise_cosine", "two_hill",
* "double_tanh", "wrapping_cosine", "fourier"
* @param cardiac_period Cardiac cycle period
* @return Unique pointer to the created activation function
*/
static std::unique_ptr<ActivationFunction> create_default(
const std::string& type_str, double cardiac_period);
/**
* @brief Set a scalar parameter value by name.
*
* Calling function must validate the parameter name and value
*
* @param name Parameter name
* @param value Parameter value
*/
void set_param(const std::string& name, double value);
/**
* @brief Called after all parameters are set (e.g. by loader).
*
* Default no-op. TwoHillActivation overrides to recompute normalization.
*/
virtual void finalize() {}
protected:
/**
* @brief Time duration of one cardiac cycle
*/
double cardiac_period_;
/**
* @brief Map of parameter names to their values
*/
std::map<std::string, double> params_;
};
/**
* @brief Half cosine activation function
*
* This implements the activation function used in the original
* ChamberElastanceInductor. The activation follows a half cosine wave
* during the contraction period.
*
* \f[
* A(t) = \begin{cases}
* -\frac{1}{2}\cos(2\pi t_{contract}/t_{twitch}) + \frac{1}{2}, & \text{if }
* t_{contract} \le t_{twitch} \\ 0, & \text{otherwise}
* \end{cases}
* \f]
*
* where \f$t_{contract} = \max(0, t_{in\_cycle} - t_{active})\f$
*/
class HalfCosineActivation : public ActivationFunction {
public:
/**
* @brief Construct with default parameter values (loader fills via
* set_param).
*
* @param cardiac_period Cardiac cycle period
*/
explicit HalfCosineActivation(double cardiac_period)
: ActivationFunction(cardiac_period, {{"t_active", InputParameter()},
{"t_twitch", InputParameter()}}) {}
double compute(double time) override;
};
/**
* @brief Piecewise cosine activation function
*
* This implements the activation function from the LinearElastanceChamber
* (Regazzoni chamber model). The activation consists of separate contraction
* and relaxation phases, each following a cosine curve.
*
* \f[
* \phi(t, t_C, t_R, T_C, T_R) = \begin{cases}
* \frac{1}{2}\left[1 - \cos\left(\frac{\pi}{T_C} \bmod(t - t_C,
* T_{HB})\right)\right],
* & \text{if } 0 \le \bmod(t - t_C, T_{HB}) < T_C \\
* \frac{1}{2}\left[1 + \cos\left(\frac{\pi}{T_R} \bmod(t - t_R,
* T_{HB})\right)\right],
* & \text{if } 0 \le \bmod(t - t_R, T_{HB}) < T_R \\
* 0, & \text{otherwise}
* \end{cases}
* \f]
*/
class PiecewiseCosineActivation : public ActivationFunction {
public:
/**
* @brief Construct with default parameter values (loader fills via
* set_param).
*
* @param cardiac_period Cardiac cycle period
*/
explicit PiecewiseCosineActivation(double cardiac_period)
: ActivationFunction(cardiac_period,
{{"contract_start", InputParameter()},
{"relax_start", InputParameter()},
{"contract_duration", InputParameter()},
{"relax_duration", InputParameter()}}) {}
double compute(double time) override;
};
/**
* @brief Two hill activation function
*
* This implements the two-hill activation function which provides more
* flexible and physiologically realistic waveforms. See
* https://link.springer.com/article/10.1007/s10439-022-03047-3
*
* The activation is computed as:
* \f[
* A(t) = C \cdot \frac{g_1(t)}{1 + g_1(t)} \cdot \frac{1}{1 + g_2(t)}
* \f]
*
* where:
* \f[
* g_1(t) = \left(\frac{t_{shifted}}{\tau_1}\right)^{m_1}, \quad
* g_2(t) = \left(\frac{t_{shifted}}{\tau_2}\right)^{m_2}
* \f]
*
* and \f$t_{shifted} = (t - t_{shift}) \bmod T_{cardiac}\f$, and \f$C\f$ is a
* normalization constant to ensure max activation is 1.
*/
class TwoHillActivation : public ActivationFunction {
public:
/**
* @brief Construct with default parameter values (loader fills via
* set_param).
*
* @param cardiac_period Cardiac cycle period
*/
explicit TwoHillActivation(double cardiac_period)
: ActivationFunction(cardiac_period, {{"t_shift", InputParameter()},
{"tau_1", InputParameter()},
{"tau_2", InputParameter()},
{"m1", InputParameter()},
{"m2", InputParameter()}}),
normalization_factor_(1.0),
normalization_initialized_(false) {}
double compute(double time) override;
void finalize() override;
private:
void calculate_normalization_factor();
double normalization_factor_;
bool normalization_initialized_;
};
/**
* @brief Double tanh (systole/diastole sigmoid product) activation function
*
* This implements the original ChamberSphere activation: a smooth indicator
* function built from the product of two tanh sigmoids, one rising at
* systole and one falling at diastole.
*
* \f[
* f(t) = S_+ \cdot S_-, \quad S_\pm = \frac{1}{2} \left(1.0 \pm
* \text{tanh}\left( \frac{t_{in\_cycle} - t_\text{sys/dias}} {\gamma}
* \right) \right)
* \f]
*/
class DoubleTanhActivation : public ActivationFunction {
public:
/**
* @brief Construct with default parameter values (loader fills via
* set_param).
*
* @param cardiac_period Cardiac cycle period
*/
explicit DoubleTanhActivation(double cardiac_period)
: ActivationFunction(cardiac_period, {{"tsys", InputParameter()},
{"tdias", InputParameter()},
{"steepness", InputParameter()}}) {}
double compute(double time) override;
};
/**
* @brief Wrapping cosine activation function
*
* Reproduces the atrial activation AA(t) from \cite sankaran2012patient
* and \cite menon2023predictors. The cosine wraps across the cardiac
* cycle boundary, which half_cosine and piecewise_cosine cannot do.
*
* Parameters:
* * `Tsa` — Atrial systole time fraction (multiplied by cardiac period)
* * `tpwave` — P-wave divisor (cardiac period is divided by this)
*/
class WrappingCosineActivation : public ActivationFunction {
public:
/**
* @brief Construct a new WrappingCosineActivation object
*
* @param cardiac_period Cardiac cycle period
*/
explicit WrappingCosineActivation(double cardiac_period)
: ActivationFunction(cardiac_period, {{"Tsa", InputParameter()},
{"tpwave", InputParameter()}}) {}
double compute(double time) override;
};
/**
* @brief Fourier series activation function
*
* 25-mode Fourier series for ventricular elastance activation, based on
* J. Tran's tuning framework (\cite menon2023predictors). The series is
* normalized so the output ranges from 0 (diastole) to 1 (peak systole).
* No user parameters are needed; the Fourier coefficients are hardcoded.
*
* Combined with ChamberElastanceInductor using
* Emax = Fourier_peak * E_scale, Emin = Fourier_trough * E_scale
* this reproduces E(t) = Fourier(t) * E_scale exactly.
*/
class FourierActivation : public ActivationFunction {
public:
/**
* @brief Construct a new FourierActivation object
*
* @param cardiac_period Cardiac cycle period
*/
explicit FourierActivation(double cardiac_period)
: ActivationFunction(cardiac_period, {}),
norm_min_(0.0),
norm_range_(1.0),
normalization_initialized_(false) {}
double compute(double time) override;
void finalize() override;
private:
void calculate_normalization();
double compute_raw(double t_in_cycle) const;
double norm_min_;
double norm_range_;
bool normalization_initialized_;
};
#endif // SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_