|
| 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 | + */ |
| 26 | +class ActivationFunction { |
| 27 | + public: |
| 28 | + /** |
| 29 | + * @brief Construct activation function |
| 30 | + * |
| 31 | + * @param cardiac_period Cardiac cycle period |
| 32 | + * @param params Parameter definitions (name, InputParameter) for this activation function |
| 33 | + */ |
| 34 | + ActivationFunction( |
| 35 | + double cardiac_period, |
| 36 | + std::vector<std::pair<std::string, InputParameter>> params); |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Virtual destructor |
| 40 | + */ |
| 41 | + virtual ~ActivationFunction() = default; |
| 42 | + |
| 43 | + /** |
| 44 | + * @brief Compute activation value at given time |
| 45 | + * |
| 46 | + * @param time Current time |
| 47 | + * @return Activation value between 0 and 1 |
| 48 | + */ |
| 49 | + virtual double compute(double time) = 0; |
| 50 | + |
| 51 | + /** |
| 52 | + * @brief Create a default activation function from activation function type |
| 53 | + * |
| 54 | + * @param type_str One of: "half_cosine", "piecewise_cosine", "two_hill" |
| 55 | + * @param cardiac_period Cardiac cycle period |
| 56 | + * @return Unique pointer to the created activation function |
| 57 | + */ |
| 58 | + static std::unique_ptr<ActivationFunction> create_default( |
| 59 | + const std::string& type_str, double cardiac_period); |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief Set a scalar parameter value by name. |
| 63 | + * |
| 64 | + * Validates that name is in params_ and has a number schema, then stores |
| 65 | + * the value. No per-class logic needed. |
| 66 | + * |
| 67 | + * @param name Parameter name (must be a key in params_) |
| 68 | + * @param value Parameter value |
| 69 | + */ |
| 70 | + void set_param(const std::string& name, double value); |
| 71 | + |
| 72 | + /** |
| 73 | + * @brief Parameter definitions for validation/loading (analogous to Block::input_params). |
| 74 | + * |
| 75 | + * Returns (name, InputParameter) for each parameter. Built from params_. |
| 76 | + */ |
| 77 | + std::vector<std::pair<std::string, InputParameter>> get_input_params() const; |
| 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 | + double cardiac_period_; |
| 88 | + std::map<std::string, std::pair<InputParameter, double>> params_; |
| 89 | +}; |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief Half cosine activation function |
| 93 | + * |
| 94 | + * This implements the activation function used in the original |
| 95 | + * ChamberElastanceInductor. The activation follows a half cosine wave |
| 96 | + * during the contraction period. |
| 97 | + * |
| 98 | + * \f[ |
| 99 | + * A(t) = \begin{cases} |
| 100 | + * -\frac{1}{2}\cos(2\pi t_{contract}/t_{twitch}) + \frac{1}{2}, & \text{if } t_{contract} \le t_{twitch} \\ |
| 101 | + * 0, & \text{otherwise} |
| 102 | + * \end{cases} |
| 103 | + * \f] |
| 104 | + * |
| 105 | + * where \f$t_{contract} = \max(0, t_{in\_cycle} - t_{active})\f$ |
| 106 | + */ |
| 107 | +class HalfCosineActivation : public ActivationFunction { |
| 108 | + public: |
| 109 | + /** |
| 110 | + * @brief Construct with default parameter values (loader fills via set_param). |
| 111 | + * |
| 112 | + * @param cardiac_period Cardiac cycle period |
| 113 | + */ |
| 114 | + explicit HalfCosineActivation(double cardiac_period) |
| 115 | + : ActivationFunction(cardiac_period, {{"t_active", InputParameter()}, |
| 116 | + {"t_twitch", InputParameter()}}) {} |
| 117 | + |
| 118 | + double compute(double time) override; |
| 119 | +}; |
| 120 | + |
| 121 | + |
| 122 | +/** |
| 123 | + * @brief Piecewise cosine activation function |
| 124 | + * |
| 125 | + * This implements the activation function from the LinearElastanceChamber |
| 126 | + * (Regazzoni chamber model). The activation consists of separate contraction |
| 127 | + * and relaxation phases, each following a cosine curve. |
| 128 | + * |
| 129 | + * \f[ |
| 130 | + * \phi(t, t_C, t_R, T_C, T_R) = \begin{cases} |
| 131 | + * \frac{1}{2}\left[1 - \cos\left(\frac{\pi}{T_C} \bmod(t - t_C, T_{HB})\right)\right], |
| 132 | + * & \text{if } 0 \le \bmod(t - t_C, T_{HB}) < T_C \\ |
| 133 | + * \frac{1}{2}\left[1 + \cos\left(\frac{\pi}{T_R} \bmod(t - t_R, T_{HB})\right)\right], |
| 134 | + * & \text{if } 0 \le \bmod(t - t_R, T_{HB}) < T_R \\ |
| 135 | + * 0, & \text{otherwise} |
| 136 | + * \end{cases} |
| 137 | + * \f] |
| 138 | + */ |
| 139 | +class PiecewiseCosineActivation : public ActivationFunction { |
| 140 | + public: |
| 141 | + /** |
| 142 | + * @brief Construct with default parameter values (loader fills via set_param). |
| 143 | + * |
| 144 | + * @param cardiac_period Cardiac cycle period |
| 145 | + */ |
| 146 | + explicit PiecewiseCosineActivation(double cardiac_period) |
| 147 | + : ActivationFunction(cardiac_period, |
| 148 | + {{"contract_start", InputParameter()}, |
| 149 | + {"relax_start", InputParameter()}, |
| 150 | + {"contract_duration", InputParameter()}, |
| 151 | + {"relax_duration", InputParameter()}}) {} |
| 152 | + |
| 153 | + double compute(double time) override; |
| 154 | +}; |
| 155 | + |
| 156 | +/** |
| 157 | + * @brief Two hill activation function |
| 158 | + * |
| 159 | + * This implements the two-hill activation function which provides more |
| 160 | + * flexible and physiologically realistic waveforms. See |
| 161 | + * https://link.springer.com/article/10.1007/s10439-022-03047-3 |
| 162 | + * |
| 163 | + * The activation is computed as: |
| 164 | + * \f[ |
| 165 | + * A(t) = C \cdot \frac{g_1(t)}{1 + g_1(t)} \cdot \frac{1}{1 + g_2(t)} |
| 166 | + * \f] |
| 167 | + * |
| 168 | + * where: |
| 169 | + * \f[ |
| 170 | + * g_1(t) = \left(\frac{t_{shifted}}{\tau_1}\right)^{m_1}, \quad |
| 171 | + * g_2(t) = \left(\frac{t_{shifted}}{\tau_2}\right)^{m_2} |
| 172 | + * \f] |
| 173 | + * |
| 174 | + * and \f$t_{shifted} = (t - t_{shift}) \bmod T_{cardiac}\f$, and \f$C\f$ is a |
| 175 | + * normalization constant to ensure max activation is 1. |
| 176 | + */ |
| 177 | +class TwoHillActivation : public ActivationFunction { |
| 178 | + public: |
| 179 | + /** |
| 180 | + * @brief Construct with default parameter values (loader fills via set_param). |
| 181 | + * |
| 182 | + * Defaults for tau_1, tau_2, m1, m2 are 1.0 to avoid division by zero. |
| 183 | + * Call finalize() after all set_param to recompute normalization. |
| 184 | + * |
| 185 | + * @param cardiac_period Cardiac cycle period |
| 186 | + */ |
| 187 | + explicit TwoHillActivation(double cardiac_period) |
| 188 | + : ActivationFunction(cardiac_period, |
| 189 | + {{"t_shift", InputParameter()}, |
| 190 | + {"tau_1", InputParameter(false, false, true, 1.0)}, |
| 191 | + {"tau_2", InputParameter(false, false, true, 1.0)}, |
| 192 | + {"m1", InputParameter(false, false, true, 1.0)}, |
| 193 | + {"m2", InputParameter(false, false, true, 1.0)}}), |
| 194 | + normalization_factor_(1.0), |
| 195 | + normalization_initialized_(false) {} |
| 196 | + |
| 197 | + double compute(double time) override; |
| 198 | + |
| 199 | + void finalize() override; |
| 200 | + |
| 201 | + private: |
| 202 | + void calculate_normalization_factor(); |
| 203 | + |
| 204 | + double normalization_factor_; |
| 205 | + bool normalization_initialized_; |
| 206 | +}; |
| 207 | + |
| 208 | +#endif // SVZERODSOLVER_MODEL_ACTIVATIONFUNCTION_HPP_ |
0 commit comments