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
//! HPA axis — hypothalamic-pituitary-adrenal stress response.
//!
//! Models the CRH → ACTH → cortisol cascade with negative feedback,
//! chronic stress adaptation, and allostatic load accumulation.
use crate::error::{MastishkError, validate_dt};
use serde::{Deserialize, Serialize};
/// HPA axis state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HpaState {
/// CRH (corticotropin-releasing hormone) level (0.0–1.0).
pub crh: f32,
/// ACTH (adrenocorticotropic hormone) level (0.0–1.0).
pub acth: f32,
/// Cortisol level (0.0–1.0).
pub cortisol: f32,
/// Cortisol baseline — chronic stress raises this.
pub cortisol_baseline: f32,
/// Allostatic load — cumulative wear from chronic stress (0.0+).
pub allostatic_load: f32,
/// Negative feedback strength (higher = faster cortisol suppresses CRH).
pub feedback_gain: f32,
/// Stress sensitization / kindling (0.0–1.0). Chronic stress lowers the
/// threshold for future HPA activation (Post 1992 kindling model).
/// Driven by allostatic_load accumulation.
#[serde(default)]
pub sensitization: f32,
}
impl Default for HpaState {
fn default() -> Self {
Self {
crh: 0.1,
acth: 0.1,
cortisol: 0.2,
cortisol_baseline: 0.2,
allostatic_load: 0.0,
feedback_gain: 0.5,
sensitization: 0.0,
}
}
}
impl HpaState {
/// Apply a stressor (0.0–1.0 intensity). Triggers CRH release.
///
/// Sensitization amplifies the effective intensity: repeated stress makes
/// the HPA axis more reactive to future stressors (kindling effect).
#[inline]
pub fn stress(&mut self, intensity: f32) {
let effective = intensity * (1.0 + self.sensitization * 0.5);
self.crh = (self.crh + effective * 0.3).min(1.0);
tracing::debug!(intensity, effective, crh = self.crh, "stressor applied");
}
/// Tick the cascade: CRH drives ACTH, ACTH drives cortisol,
/// cortisol feeds back to suppress CRH.
///
/// # Errors
/// Returns [`MastishkError::NegativeTimeDelta`] if `dt < 0.0`.
#[inline]
pub fn tick(&mut self, dt: f32) -> Result<(), MastishkError> {
validate_dt(dt)?;
tracing::trace!(
dt,
cortisol = self.cortisol,
crh = self.crh,
allostatic_load = self.allostatic_load,
"ticking HPA axis"
);
// CRH → ACTH (exponential approach, tau ≈ 300s / ~5 min)
let acth_rate = 1.0 / 300.0;
let acth_target = (self.crh * 1.67).min(1.0); // CRH drives ACTH target
let acth_alpha = 1.0 - (-acth_rate * dt).exp();
self.acth += (acth_target - self.acth) * acth_alpha;
self.acth = self.acth.clamp(0.0, 1.0);
// ACTH → cortisol (exponential approach, tau ≈ 600s / ~10 min)
let cort_rate = 1.0 / 600.0;
let cort_target = (self.cortisol_baseline + self.acth * 0.8).min(1.0);
let cort_alpha = 1.0 - (-cort_rate * dt).exp();
self.cortisol += (cort_target - self.cortisol) * cort_alpha;
self.cortisol = self.cortisol.clamp(0.0, 1.0);
// Negative feedback: cortisol suppresses CRH (tau ≈ 900s / ~15 min)
let fb_rate = self.cortisol * self.feedback_gain * (1.0 / 900.0);
self.crh *= (-fb_rate * dt).exp();
self.crh = self.crh.clamp(0.0, 1.0);
// Allostatic load accumulates when cortisol is above baseline
if self.cortisol > self.cortisol_baseline + 0.1 {
self.allostatic_load += (self.cortisol - self.cortisol_baseline) * 0.01 * dt;
}
// Slow recovery when cortisol is low
if self.cortisol < self.cortisol_baseline + 0.05 {
self.allostatic_load = (self.allostatic_load - 0.002 * dt).max(0.0);
}
// Stress sensitization driven by allostatic load (Post 1992 kindling)
// High load → increased sensitization, low load → slow recovery
let sens_target = (self.allostatic_load / 3.0).min(1.0);
let sens_alpha = 1.0 - (-0.0001 * dt).exp(); // very slow (days timescale)
self.sensitization += (sens_target - self.sensitization) * sens_alpha;
self.sensitization = self.sensitization.clamp(0.0, 1.0);
Ok(())
}
/// Whether the HPA axis is in an acute stress response.
#[inline]
#[must_use]
pub fn is_stressed(&self) -> bool {
self.cortisol > self.cortisol_baseline + 0.15
}
/// Chronic stress indicator — allostatic load above threshold.
#[inline]
#[must_use]
pub fn is_chronic(&self) -> bool {
self.allostatic_load > 1.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stress_response() {
let mut h = HpaState::default();
h.stress(0.8);
assert!(h.crh > 0.2);
// Tick 20 minutes (cascade needs ~15 min to propagate)
for _ in 0..1200 {
h.tick(1.0).unwrap();
}
assert!(h.cortisol > h.cortisol_baseline);
}
#[test]
fn test_negative_feedback() {
let mut h = HpaState::default();
h.stress(1.0);
// 30 minutes for feedback to take effect
for _ in 0..1800 {
h.tick(1.0).unwrap();
}
// CRH should be suppressed by cortisol feedback
assert!(h.crh < 0.5);
}
#[test]
fn test_allostatic_load() {
let mut h = HpaState::default();
// Repeated stress over 1 hour
for _ in 0..3600 {
h.stress(0.5);
h.tick(1.0).unwrap();
}
assert!(h.allostatic_load > 0.0);
}
#[test]
fn test_serde_roundtrip() {
let h = HpaState::default();
let json = serde_json::to_string(&h).unwrap();
let h2: HpaState = serde_json::from_str(&json).unwrap();
assert!((h2.cortisol - h.cortisol).abs() < f32::EPSILON);
}
#[test]
fn test_negative_dt_rejected() {
let mut h = HpaState::default();
assert!(h.tick(-1.0).is_err());
}
#[test]
fn test_is_stressed() {
let mut h = HpaState::default();
assert!(!h.is_stressed());
h.stress(1.0);
// 20 minutes for cortisol to rise
for _ in 0..1200 {
h.tick(1.0).unwrap();
}
assert!(h.is_stressed());
}
#[test]
fn test_is_chronic() {
let mut h = HpaState::default();
assert!(!h.is_chronic());
// Sustained stress for 2 hours
for _ in 0..7200 {
h.stress(0.8);
h.tick(1.0).unwrap();
}
assert!(h.is_chronic());
}
#[test]
fn test_allostatic_load_recovers() {
let mut h = HpaState::default();
// Build up load: stress every minute for 1 hour
for _ in 0..60 {
h.stress(0.8);
h.tick(60.0).unwrap();
}
let peak_load = h.allostatic_load;
assert!(peak_load > 0.0, "load should accumulate");
// Let it recover: 12 hours no stress (in 1-minute steps)
for _ in 0..720 {
h.tick(60.0).unwrap();
}
assert!(
h.allostatic_load < peak_load,
"load={}, peak={}",
h.allostatic_load,
peak_load
);
}
}