svZeroDSolver
Loading...
Searching...
No Matches
ActivationFunction.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
2// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
3
4/**
5 * @file ActivationFunction.h
6 * @brief Activation function classes for cardiac chamber models
7 */
8
9#ifndef SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_
10#define SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_
11
12#include <map>
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "Parameter.h"
18
19/**
20 * @brief Base class for activation functions
21 *
22 * Activation functions compute the activation value (between 0 and 1) at a
23 * given time point within a cardiac cycle. These are used to modulate
24 * chamber elastance over time.
25 */
27 public:
28 /**
29 * @brief Properties of the input parameters for this activation function
30 * [(name, InputParameter), ...]
31 */
32 const std::vector<std::pair<std::string, InputParameter>>
34
35 /**
36 * @brief Construct activation function
37 *
38 * @param cardiac_period Cardiac cycle period
39 * @param input_param_properties Properties of the input parameters
40 * [(name, InputParameter), ...] for this activation function
41 */
42 ActivationFunction(double cardiac_period,
43 const std::vector<std::pair<std::string, InputParameter>>&
45
46 /**
47 * @brief Virtual destructor
48 */
49 virtual ~ActivationFunction() = default;
50
51 /**
52 * @brief Compute activation value at given time
53 *
54 * @param time Current time
55 * @return Activation value between 0 and 1
56 */
57 virtual double compute(double time) = 0;
58
59 /**
60 * @brief Create a default activation function from activation function type
61 *
62 * @param type_str One of: "half_cosine", "piecewise_cosine", "two_hill"
63 * @param cardiac_period Cardiac cycle period
64 * @return Unique pointer to the created activation function
65 */
66 static std::unique_ptr<ActivationFunction> create_default(
67 const std::string& type_str, double cardiac_period);
68
69 /**
70 * @brief Set a scalar parameter value by name.
71 *
72 * Calling function must validate the parameter name and value
73 *
74 * @param name Parameter name
75 * @param value Parameter value
76 */
77 void set_param(const std::string& name, double value);
78
79 /**
80 * @brief Called after all parameters are set (e.g. by loader).
81 *
82 * Default no-op. TwoHillActivation overrides to recompute normalization.
83 */
84 virtual void finalize() {}
85
86 protected:
87 /**
88 * @brief Time duration of one cardiac cycle
89 */
91
92 /**
93 * @brief Map of parameter names to their values
94 */
95 std::map<std::string, double> params_;
96};
97
98/**
99 * @brief Half cosine activation function
100 *
101 * This implements the activation function used in the original
102 * ChamberElastanceInductor. The activation follows a half cosine wave
103 * during the contraction period.
104 *
105 * \f[
106 * A(t) = \begin{cases}
107 * -\frac{1}{2}\cos(2\pi t_{contract}/t_{twitch}) + \frac{1}{2}, & \text{if }
108 * t_{contract} \le t_{twitch} \\ 0, & \text{otherwise}
109 * \end{cases}
110 * \f]
111 *
112 * where \f$t_{contract} = \max(0, t_{in\_cycle} - t_{active})\f$
113 */
115 public:
116 /**
117 * @brief Construct with default parameter values (loader fills via
118 * set_param).
119 *
120 * @param cardiac_period Cardiac cycle period
121 */
122 explicit HalfCosineActivation(double cardiac_period)
123 : ActivationFunction(cardiac_period, {{"t_active", InputParameter()},
124 {"t_twitch", InputParameter()}}) {}
125
126 double compute(double time) override;
127};
128
129/**
130 * @brief Piecewise cosine activation function
131 *
132 * This implements the activation function from the LinearElastanceChamber
133 * (Regazzoni chamber model). The activation consists of separate contraction
134 * and relaxation phases, each following a cosine curve.
135 *
136 * \f[
137 * \phi(t, t_C, t_R, T_C, T_R) = \begin{cases}
138 * \frac{1}{2}\left[1 - \cos\left(\frac{\pi}{T_C} \bmod(t - t_C,
139 * T_{HB})\right)\right],
140 * & \text{if } 0 \le \bmod(t - t_C, T_{HB}) < T_C \\
141 * \frac{1}{2}\left[1 + \cos\left(\frac{\pi}{T_R} \bmod(t - t_R,
142 * T_{HB})\right)\right],
143 * & \text{if } 0 \le \bmod(t - t_R, T_{HB}) < T_R \\
144 * 0, & \text{otherwise}
145 * \end{cases}
146 * \f]
147 */
149 public:
150 /**
151 * @brief Construct with default parameter values (loader fills via
152 * set_param).
153 *
154 * @param cardiac_period Cardiac cycle period
155 */
156 explicit PiecewiseCosineActivation(double cardiac_period)
157 : ActivationFunction(cardiac_period,
158 {{"contract_start", InputParameter()},
159 {"relax_start", InputParameter()},
160 {"contract_duration", InputParameter()},
161 {"relax_duration", InputParameter()}}) {}
162
163 double compute(double time) override;
164};
165
166/**
167 * @brief Two hill activation function
168 *
169 * This implements the two-hill activation function which provides more
170 * flexible and physiologically realistic waveforms. See
171 * https://link.springer.com/article/10.1007/s10439-022-03047-3
172 *
173 * The activation is computed as:
174 * \f[
175 * A(t) = C \cdot \frac{g_1(t)}{1 + g_1(t)} \cdot \frac{1}{1 + g_2(t)}
176 * \f]
177 *
178 * where:
179 * \f[
180 * g_1(t) = \left(\frac{t_{shifted}}{\tau_1}\right)^{m_1}, \quad
181 * g_2(t) = \left(\frac{t_{shifted}}{\tau_2}\right)^{m_2}
182 * \f]
183 *
184 * and \f$t_{shifted} = (t - t_{shift}) \bmod T_{cardiac}\f$, and \f$C\f$ is a
185 * normalization constant to ensure max activation is 1.
186 */
188 public:
189 /**
190 * @brief Construct with default parameter values (loader fills via
191 * set_param).
192 *
193 * @param cardiac_period Cardiac cycle period
194 */
195 explicit TwoHillActivation(double cardiac_period)
196 : ActivationFunction(cardiac_period, {{"t_shift", InputParameter()},
197 {"tau_1", InputParameter()},
198 {"tau_2", InputParameter()},
199 {"m1", InputParameter()},
200 {"m2", InputParameter()}}),
201 normalization_factor_(1.0),
202 normalization_initialized_(false) {}
203
204 double compute(double time) override;
205
206 void finalize() override;
207
208 private:
209 void calculate_normalization_factor();
210
211 double normalization_factor_;
212 bool normalization_initialized_;
213};
214
215#endif // SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_
model::Parameter source file
static std::unique_ptr< ActivationFunction > create_default(const std::string &type_str, double cardiac_period)
Create a default activation function from activation function type.
Definition ActivationFunction.cpp:33
double cardiac_period_
Time duration of one cardiac cycle.
Definition ActivationFunction.h:90
virtual double compute(double time)=0
Compute activation value at given time.
virtual ~ActivationFunction()=default
Virtual destructor.
void set_param(const std::string &name, double value)
Set a scalar parameter value by name.
Definition ActivationFunction.cpp:29
ActivationFunction(double cardiac_period, const std::vector< std::pair< std::string, InputParameter > > &input_param_properties)
Construct activation function.
Definition ActivationFunction.cpp:14
const std::vector< std::pair< std::string, InputParameter > > input_param_properties
Properties of the input parameters for this activation function [(name, InputParameter),...
Definition ActivationFunction.h:33
std::map< std::string, double > params_
Map of parameter names to their values.
Definition ActivationFunction.h:95
virtual void finalize()
Called after all parameters are set (e.g. by loader).
Definition ActivationFunction.h:84
HalfCosineActivation(double cardiac_period)
Construct with default parameter values (loader fills via set_param).
Definition ActivationFunction.h:122
double compute(double time) override
Compute activation value at given time.
Definition ActivationFunction.cpp:49
double compute(double time) override
Compute activation value at given time.
Definition ActivationFunction.cpp:67
PiecewiseCosineActivation(double cardiac_period)
Construct with default parameter values (loader fills via set_param).
Definition ActivationFunction.h:156
TwoHillActivation(double cardiac_period)
Construct with default parameter values (loader fills via set_param).
Definition ActivationFunction.h:195
double compute(double time) override
Compute activation value at given time.
Definition ActivationFunction.cpp:130
void finalize() override
Called after all parameters are set (e.g. by loader).
Definition ActivationFunction.cpp:128
Handles the properties of input parameters.
Definition Parameter.h:100