mastishk 1.1.0

Mastishk — computational neuroscience: neurotransmitters, neural circuits, sleep stages, HPA axis, default mode network
Documentation
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Receptor dynamics — subtypes, availability, desensitization, upregulation.
//!
//! Models receptor availability changes over time in response to agonist/antagonist
//! occupancy. Chronic agonist exposure desensitizes receptors (reduces availability);
//! chronic antagonist exposure or agonist withdrawal causes upregulation (increased
//! availability, potentially above baseline — rebound).

use serde::{Deserialize, Serialize};

use crate::error::{MastishkError, validate_dt};

/// Neurotransmitter reuptake transporter identifiers.
///
/// Transporters are the primary target of reuptake inhibitor drugs (SSRIs, SNRIs,
/// stimulants). Distinct from receptors — blocking a transporter increases synaptic
/// concentration of the corresponding neurotransmitter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum TransporterType {
    /// Serotonin transporter — target of SSRIs (fluoxetine, sertraline).
    Sert,
    /// Dopamine transporter — target of stimulants (methylphenidate), cocaine.
    Dat,
    /// Norepinephrine transporter — target of SNRIs, stimulants (amphetamine).
    Net,
}

/// Receptor subtype identifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ReceptorSubtype {
    /// 5-HT1A — serotonin autoreceptor (raphe) + postsynaptic (hippocampus, PFC).
    Ht1a,
    /// 5-HT2A — serotonin postsynaptic (cortex, amygdala). Pro-excitatory, salience.
    Ht2a,
    /// Dopamine D1 — Go pathway. Motivational drive, reward-seeking.
    D1,
    /// Dopamine D2 — No-Go pathway. Impulse brake, antipsychotic target.
    D2,
    /// Adrenergic alpha-1 — arousal, attention, vasoconstriction.
    Alpha1,
    /// Adrenergic alpha-2 — autoreceptor, inhibitory feedback on NE.
    Alpha2,
    /// Adrenergic beta — arousal, fight-or-flight, cardiac.
    Beta,
    /// GABA-A — fast inhibition (ionotropic Cl⁻ channel). Benzodiazepine target.
    GabaA,
    /// GABA-B — slow inhibition (metabotropic). Muscle relaxation, presynaptic.
    GabaB,
    /// CB1 — cannabinoid receptor 1. Retrograde signaling, stress buffer, pain modulation.
    Cb1,
    /// Mu-opioid (μ) — pain/endorphin system. Analgesia, euphoria, opioid drug target.
    MuOpioid,
    /// NMDA — glutamate ionotropic receptor. Learning/LTP, ketamine target.
    Nmda,
    /// AMPA — primary fast excitatory glutamate receptor. >90% of fast glutamatergic transmission.
    /// Required for NMDA activation (Mg2+ block removal via depolarization).
    Ampa,
    /// Orexin OX1 — wakefulness, arousal. Expressed in locus coeruleus.
    Ox1,
    /// Orexin OX2 — sleep-wake stability. Primary target of suvorexant (insomnia drug).
    Ox2,
    /// 5-HT2C — serotonin. Rate-limiting for SSRI therapeutic lag (Dremencov 2009).
    Ht2c,
    /// Nicotinic ACh alpha4beta2 — high affinity, nicotine target, desensitizes.
    NicotinicA4b2,
    /// Nicotinic ACh alpha7 — fast, low affinity, cognitive enhancement.
    NicotinicA7,
    /// Histamine H1 — wakefulness. Antihistamines (diphenhydramine) cause drowsiness.
    H1,
}

/// A single receptor's dynamic state — tracks availability and adaptation.
///
/// Availability starts at `baseline` (typically 1.0) and changes in response to
/// chronic agonist exposure (desensitization, availability drops) or chronic
/// antagonist/withdrawal (upregulation, availability rises, may overshoot).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReceptorState {
    /// Current receptor availability (0.0 = fully desensitized, 1.0 = normal, >1.0 = upregulated).
    pub availability: f32,
    /// Baseline availability the system trends toward.
    pub baseline: f32,
    /// Exponential moving average of recent occupancy (tracks chronic exposure).
    pub occupancy_ema: f32,
    /// Receptor turnover time constant (seconds). Controls return-to-baseline speed.
    pub tau_turnover: f32,
    /// Desensitization rate constant (agonist-driven internalization).
    pub k_des: f32,
    /// Upregulation rate constant (antagonism/withdrawal-driven synthesis).
    pub k_up: f32,
}

impl ReceptorState {
    /// Create a new receptor state at baseline availability.
    #[must_use]
    pub fn new(baseline: f32, tau_turnover: f32, k_des: f32, k_up: f32) -> Self {
        Self {
            availability: baseline,
            baseline,
            occupancy_ema: 0.0,
            tau_turnover,
            k_des,
            k_up,
        }
    }

    /// Tick the receptor state given current occupancy.
    ///
    /// # Errors
    /// Returns [`MastishkError::NegativeTimeDelta`] if `dt < 0.0`.
    #[inline]
    pub fn tick(&mut self, occupancy: f32, dt: f32) -> Result<(), MastishkError> {
        validate_dt(dt)?;
        self.tick_unchecked(occupancy, dt);
        Ok(())
    }

    /// Tick without validating dt. Updates occupancy EMA then applies the
    /// desensitization ODE: `d(avail)/dt = (baseline - avail) / tau_turnover
    /// - k_des * occupancy_ema + k_up * (1 - occupancy_ema) * max(0, baseline - avail)`.
    #[inline]
    pub(crate) fn tick_unchecked(&mut self, occupancy: f32, dt: f32) {
        // Update occupancy EMA (time constant ~300s ≈ 5 minutes)
        let ema_alpha = 1.0 - (-dt / 300.0).exp();
        self.occupancy_ema += (occupancy - self.occupancy_ema) * ema_alpha;

        // Desensitization ODE
        let baseline_return = (self.baseline - self.availability) / self.tau_turnover;
        let desensitization = self.k_des * self.occupancy_ema;
        let upregulation =
            self.k_up * (1.0 - self.occupancy_ema) * (self.baseline - self.availability).max(0.0);

        self.availability += (baseline_return - desensitization + upregulation) * dt;
        self.availability = self.availability.clamp(0.0, 1.5);
    }
}

/// Occupancy values for all receptor subtypes (output of drug interaction computation).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ReceptorOccupancies {
    pub ht1a: f32,
    pub ht2a: f32,
    pub d1: f32,
    pub d2: f32,
    pub alpha1: f32,
    pub alpha2: f32,
    pub beta: f32,
    pub gaba_a: f32,
    pub gaba_b: f32,
    pub cb1: f32,
    pub mu_opioid: f32,
    pub nmda: f32,
    pub ampa: f32,
    pub ox1: f32,
    pub ox2: f32,
    pub ht2c: f32,
    pub nicotinic_a4b2: f32,
    pub nicotinic_a7: f32,
    pub h1: f32,
}

impl ReceptorOccupancies {
    /// Get occupancy for a specific subtype.
    #[inline]
    #[must_use]
    pub fn get(&self, subtype: ReceptorSubtype) -> f32 {
        match subtype {
            ReceptorSubtype::Ht1a => self.ht1a,
            ReceptorSubtype::Ht2a => self.ht2a,
            ReceptorSubtype::D1 => self.d1,
            ReceptorSubtype::D2 => self.d2,
            ReceptorSubtype::Alpha1 => self.alpha1,
            ReceptorSubtype::Alpha2 => self.alpha2,
            ReceptorSubtype::Beta => self.beta,
            ReceptorSubtype::GabaA => self.gaba_a,
            ReceptorSubtype::GabaB => self.gaba_b,
            ReceptorSubtype::Cb1 => self.cb1,
            ReceptorSubtype::MuOpioid => self.mu_opioid,
            ReceptorSubtype::Nmda => self.nmda,
            ReceptorSubtype::Ampa => self.ampa,
            ReceptorSubtype::Ox1 => self.ox1,
            ReceptorSubtype::Ox2 => self.ox2,
            ReceptorSubtype::Ht2c => self.ht2c,
            ReceptorSubtype::NicotinicA4b2 => self.nicotinic_a4b2,
            ReceptorSubtype::NicotinicA7 => self.nicotinic_a7,
            ReceptorSubtype::H1 => self.h1,
        }
    }

    /// Set occupancy for a specific subtype, clamping to 0.0..=1.0.
    #[inline]
    pub fn add(&mut self, subtype: ReceptorSubtype, value: f32) {
        let field = match subtype {
            ReceptorSubtype::Ht1a => &mut self.ht1a,
            ReceptorSubtype::Ht2a => &mut self.ht2a,
            ReceptorSubtype::D1 => &mut self.d1,
            ReceptorSubtype::D2 => &mut self.d2,
            ReceptorSubtype::Alpha1 => &mut self.alpha1,
            ReceptorSubtype::Alpha2 => &mut self.alpha2,
            ReceptorSubtype::Beta => &mut self.beta,
            ReceptorSubtype::GabaA => &mut self.gaba_a,
            ReceptorSubtype::GabaB => &mut self.gaba_b,
            ReceptorSubtype::Cb1 => &mut self.cb1,
            ReceptorSubtype::MuOpioid => &mut self.mu_opioid,
            ReceptorSubtype::Nmda => &mut self.nmda,
            ReceptorSubtype::Ampa => &mut self.ampa,
            ReceptorSubtype::Ox1 => &mut self.ox1,
            ReceptorSubtype::Ox2 => &mut self.ox2,
            ReceptorSubtype::Ht2c => &mut self.ht2c,
            ReceptorSubtype::NicotinicA4b2 => &mut self.nicotinic_a4b2,
            ReceptorSubtype::NicotinicA7 => &mut self.nicotinic_a7,
            ReceptorSubtype::H1 => &mut self.h1,
        };
        *field = (*field + value).min(1.0);
    }
}

/// Complete set of receptor states for a brain.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReceptorMap {
    /// 5-HT1A — serotonin autoreceptor. Slow turnover (~7 days).
    pub ht1a: ReceptorState,
    /// 5-HT2A — serotonin postsynaptic. Moderate turnover (~4 days).
    pub ht2a: ReceptorState,
    /// Dopamine D1. Moderate turnover (~5 days).
    pub d1: ReceptorState,
    /// Dopamine D2. Slow turnover (~14 days, antipsychotic upregulation risk).
    pub d2: ReceptorState,
    /// Adrenergic alpha-1.
    pub alpha1: ReceptorState,
    /// Adrenergic alpha-2 (autoreceptor). Fast turnover (~2 days).
    pub alpha2: ReceptorState,
    /// Adrenergic beta.
    pub beta: ReceptorState,
    /// GABA-A. Fast turnover (~3 days, rapid BZD tolerance).
    pub gaba_a: ReceptorState,
    /// GABA-B. Moderate turnover (~5 days).
    pub gaba_b: ReceptorState,
    /// CB1 — cannabinoid receptor 1. Moderate turnover (~5 days).
    #[serde(default = "default_cb1")]
    pub cb1: ReceptorState,
    /// Mu-opioid — pain/endorphin system. Moderate turnover (~5 days).
    #[serde(default = "default_mu_opioid")]
    pub mu_opioid: ReceptorState,
    /// NMDA — glutamate learning/LTP. Moderate turnover (~5 days).
    #[serde(default = "default_nmda")]
    pub nmda: ReceptorState,
    /// AMPA — fast excitatory glutamate. Fast turnover (~2 days).
    #[serde(default = "default_ampa")]
    pub ampa: ReceptorState,
    /// Orexin OX1. Moderate turnover (~4 days).
    #[serde(default = "default_ox1")]
    pub ox1: ReceptorState,
    /// Orexin OX2. Moderate turnover (~4 days).
    #[serde(default = "default_ox2")]
    pub ox2: ReceptorState,
    /// 5-HT2C. Slow turnover (~10 days, rate-limiting for SSRI lag).
    #[serde(default = "default_ht2c")]
    pub ht2c: ReceptorState,
    /// Nicotinic ACh alpha4beta2. Fast desensitization (~2 days).
    #[serde(default = "default_nic_a4b2")]
    pub nicotinic_a4b2: ReceptorState,
    /// Nicotinic ACh alpha7. Fast kinetics (~2 days).
    #[serde(default = "default_nic_a7")]
    pub nicotinic_a7: ReceptorState,
    /// Histamine H1. Moderate turnover (~4 days).
    #[serde(default = "default_h1")]
    pub h1: ReceptorState,
}

fn default_ht2c() -> ReceptorState {
    ReceptorState::new(1.0, 864_000.0, 0.000_001_5, 0.000_000_8) // 10 days, very slow
}
fn default_nic_a4b2() -> ReceptorState {
    ReceptorState::new(1.0, 172_800.0, 0.000_004, 0.000_002) // 2 days, fast desensitization
}
fn default_nic_a7() -> ReceptorState {
    ReceptorState::new(1.0, 172_800.0, 0.000_003, 0.000_002)
}
fn default_h1() -> ReceptorState {
    ReceptorState::new(1.0, 345_600.0, 0.000_002, 0.000_001)
}

fn default_ox1() -> ReceptorState {
    ReceptorState::new(1.0, 345_600.0, 0.000_002, 0.000_001)
}

fn default_ox2() -> ReceptorState {
    ReceptorState::new(1.0, 345_600.0, 0.000_002, 0.000_001)
}

fn default_ampa() -> ReceptorState {
    ReceptorState::new(1.0, 172_800.0, 0.000_003, 0.000_002)
}

fn default_mu_opioid() -> ReceptorState {
    ReceptorState::new(1.0, 432_000.0, 0.000_003, 0.000_001)
}

fn default_nmda() -> ReceptorState {
    ReceptorState::new(1.0, 432_000.0, 0.000_002, 0.000_001)
}

fn default_cb1() -> ReceptorState {
    ReceptorState::new(1.0, 432_000.0, 0.000_002, 0.000_001)
}

impl Default for ReceptorMap {
    fn default() -> Self {
        Self {
            //                              baseline  tau_turnover    k_des     k_up
            ht1a: ReceptorState::new(1.0, 604_800.0, 0.000_002, 0.000_001),
            ht2a: ReceptorState::new(1.0, 345_600.0, 0.000_003, 0.000_001_5),
            d1: ReceptorState::new(1.0, 432_000.0, 0.000_002_5, 0.000_001),
            d2: ReceptorState::new(1.0, 1_209_600.0, 0.000_001_5, 0.000_000_8),
            alpha1: ReceptorState::new(1.0, 345_600.0, 0.000_002, 0.000_001),
            alpha2: ReceptorState::new(1.0, 172_800.0, 0.000_003, 0.000_002),
            beta: ReceptorState::new(1.0, 345_600.0, 0.000_002, 0.000_001),
            gaba_a: ReceptorState::new(1.0, 518_400.0, 0.000_003, 0.000_001_5),
            gaba_b: ReceptorState::new(1.0, 432_000.0, 0.000_002, 0.000_001),
            cb1: default_cb1(),
            mu_opioid: default_mu_opioid(),
            nmda: default_nmda(),
            ampa: default_ampa(),
            ox1: default_ox1(),
            ox2: default_ox2(),
            ht2c: default_ht2c(),
            nicotinic_a4b2: default_nic_a4b2(),
            nicotinic_a7: default_nic_a7(),
            h1: default_h1(),
        }
    }
}

impl ReceptorMap {
    /// Get a receptor state by subtype.
    #[inline]
    #[must_use]
    pub fn get(&self, subtype: ReceptorSubtype) -> &ReceptorState {
        match subtype {
            ReceptorSubtype::Ht1a => &self.ht1a,
            ReceptorSubtype::Ht2a => &self.ht2a,
            ReceptorSubtype::D1 => &self.d1,
            ReceptorSubtype::D2 => &self.d2,
            ReceptorSubtype::Alpha1 => &self.alpha1,
            ReceptorSubtype::Alpha2 => &self.alpha2,
            ReceptorSubtype::Beta => &self.beta,
            ReceptorSubtype::GabaA => &self.gaba_a,
            ReceptorSubtype::GabaB => &self.gaba_b,
            ReceptorSubtype::Cb1 => &self.cb1,
            ReceptorSubtype::MuOpioid => &self.mu_opioid,
            ReceptorSubtype::Nmda => &self.nmda,
            ReceptorSubtype::Ampa => &self.ampa,
            ReceptorSubtype::Ox1 => &self.ox1,
            ReceptorSubtype::Ox2 => &self.ox2,
            ReceptorSubtype::Ht2c => &self.ht2c,
            ReceptorSubtype::NicotinicA4b2 => &self.nicotinic_a4b2,
            ReceptorSubtype::NicotinicA7 => &self.nicotinic_a7,
            ReceptorSubtype::H1 => &self.h1,
        }
    }

    /// Get a mutable receptor state by subtype.
    #[inline]
    pub fn get_mut(&mut self, subtype: ReceptorSubtype) -> &mut ReceptorState {
        match subtype {
            ReceptorSubtype::Ht1a => &mut self.ht1a,
            ReceptorSubtype::Ht2a => &mut self.ht2a,
            ReceptorSubtype::D1 => &mut self.d1,
            ReceptorSubtype::D2 => &mut self.d2,
            ReceptorSubtype::Alpha1 => &mut self.alpha1,
            ReceptorSubtype::Alpha2 => &mut self.alpha2,
            ReceptorSubtype::Beta => &mut self.beta,
            ReceptorSubtype::GabaA => &mut self.gaba_a,
            ReceptorSubtype::GabaB => &mut self.gaba_b,
            ReceptorSubtype::Cb1 => &mut self.cb1,
            ReceptorSubtype::MuOpioid => &mut self.mu_opioid,
            ReceptorSubtype::Nmda => &mut self.nmda,
            ReceptorSubtype::Ampa => &mut self.ampa,
            ReceptorSubtype::Ox1 => &mut self.ox1,
            ReceptorSubtype::Ox2 => &mut self.ox2,
            ReceptorSubtype::Ht2c => &mut self.ht2c,
            ReceptorSubtype::NicotinicA4b2 => &mut self.nicotinic_a4b2,
            ReceptorSubtype::NicotinicA7 => &mut self.nicotinic_a7,
            ReceptorSubtype::H1 => &mut self.h1,
        }
    }

    /// Tick all receptors with the given occupancies.
    ///
    /// # Errors
    /// Returns [`MastishkError::NegativeTimeDelta`] if `dt < 0.0`.
    #[inline]
    pub fn tick_all(
        &mut self,
        occupancies: &ReceptorOccupancies,
        dt: f32,
    ) -> Result<(), MastishkError> {
        validate_dt(dt)?;
        self.ht1a.tick_unchecked(occupancies.ht1a, dt);
        self.ht2a.tick_unchecked(occupancies.ht2a, dt);
        self.d1.tick_unchecked(occupancies.d1, dt);
        self.d2.tick_unchecked(occupancies.d2, dt);
        self.alpha1.tick_unchecked(occupancies.alpha1, dt);
        self.alpha2.tick_unchecked(occupancies.alpha2, dt);
        self.beta.tick_unchecked(occupancies.beta, dt);
        self.gaba_a.tick_unchecked(occupancies.gaba_a, dt);
        self.gaba_b.tick_unchecked(occupancies.gaba_b, dt);
        self.cb1.tick_unchecked(occupancies.cb1, dt);
        self.mu_opioid.tick_unchecked(occupancies.mu_opioid, dt);
        self.nmda.tick_unchecked(occupancies.nmda, dt);
        self.ampa.tick_unchecked(occupancies.ampa, dt);
        self.ox1.tick_unchecked(occupancies.ox1, dt);
        self.ox2.tick_unchecked(occupancies.ox2, dt);
        self.ht2c.tick_unchecked(occupancies.ht2c, dt);
        self.nicotinic_a4b2
            .tick_unchecked(occupancies.nicotinic_a4b2, dt);
        self.nicotinic_a7
            .tick_unchecked(occupancies.nicotinic_a7, dt);
        self.h1.tick_unchecked(occupancies.h1, dt);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_receptor_state_at_baseline() {
        let r = ReceptorState::new(1.0, 604_800.0, 0.000_002, 0.000_001);
        assert!((r.availability - 1.0).abs() < f32::EPSILON);
        assert!((r.occupancy_ema - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_desensitization_under_chronic_agonist() {
        let mut r = ReceptorState::new(1.0, 259_200.0, 0.000_01, 0.000_001);
        // Chronic high occupancy for simulated days
        for _ in 0..86400 {
            // 1 day in 1-second steps
            r.tick_unchecked(0.8, 1.0);
        }
        // Availability should have decreased
        assert!(r.availability < 1.0, "avail={}", r.availability);
    }

    #[test]
    fn test_upregulation_on_withdrawal() {
        let mut r = ReceptorState::new(1.0, 259_200.0, 0.000_01, 0.000_005);
        // First desensitize
        for _ in 0..86400 {
            r.tick_unchecked(0.9, 1.0);
        }
        let desensitized = r.availability;
        assert!(desensitized < 1.0);

        // Then withdraw (zero occupancy)
        for _ in 0..172800 {
            // 2 days recovery
            r.tick_unchecked(0.0, 1.0);
        }
        // Should have recovered toward baseline
        assert!(r.availability > desensitized);
    }

    #[test]
    fn test_availability_clamped() {
        let mut r = ReceptorState::new(1.0, 100.0, 0.0, 0.1);
        // High upregulation with no occupancy, fast turnover
        for _ in 0..1000 {
            r.tick_unchecked(0.0, 1.0);
        }
        assert!(r.availability <= 1.5);
    }

    #[test]
    fn test_negative_dt_rejected() {
        let mut r = ReceptorState::new(1.0, 604_800.0, 0.000_002, 0.000_001);
        assert!(r.tick(0.5, -1.0).is_err());
    }

    #[test]
    fn test_receptor_map_default_all_available() {
        let map = ReceptorMap::default();
        assert!((map.ht1a.availability - 1.0).abs() < f32::EPSILON);
        assert!((map.d2.availability - 1.0).abs() < f32::EPSILON);
        assert!((map.gaba_a.availability - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_receptor_map_get() {
        let map = ReceptorMap::default();
        assert!((map.get(ReceptorSubtype::Ht1a).availability - 1.0).abs() < f32::EPSILON);
        assert!((map.get(ReceptorSubtype::GabaA).tau_turnover - 518_400.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_receptor_map_tick_all() {
        let mut map = ReceptorMap::default();
        let occ = ReceptorOccupancies {
            gaba_a: 0.8,
            ..Default::default()
        };
        // Tick for simulated days
        for _ in 0..86400 {
            map.tick_all(&occ, 1.0).unwrap();
        }
        // GABA-A should desensitize under chronic occupancy
        assert!(map.gaba_a.availability < 1.0);
        // Others should be near baseline (zero occupancy)
        assert!((map.ht1a.availability - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_occupancies_add_clamps() {
        let mut occ = ReceptorOccupancies::default();
        occ.add(ReceptorSubtype::GabaA, 0.6);
        occ.add(ReceptorSubtype::GabaA, 0.6); // should clamp to 1.0
        assert!((occ.gaba_a - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_upregulation_does_not_apply_when_above_baseline() {
        // When availability > baseline, upregulation term should be zero
        // (the .max(0.0) in the ODE ensures this)
        let mut r = ReceptorState::new(1.0, 259_200.0, 0.0, 0.01);
        r.availability = 1.3; // artificially above baseline
        r.tick_unchecked(0.0, 86400.0); // 1 day, no occupancy
        // Should trend back toward baseline, not keep rising
        assert!(r.availability < 1.3);
        assert!(r.availability >= 1.0); // should approach but not undershoot baseline
    }

    #[test]
    fn test_zero_occupancy_maintains_baseline() {
        let mut r = ReceptorState::new(1.0, 604_800.0, 0.000_002, 0.000_001);
        for _ in 0..1000 {
            r.tick_unchecked(0.0, 1.0);
        }
        // With no occupancy, availability should stay near baseline
        assert!((r.availability - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_serde_roundtrip() {
        let map = ReceptorMap::default();
        let json = serde_json::to_string(&map).unwrap();
        let map2: ReceptorMap = serde_json::from_str(&json).unwrap();
        assert!((map2.ht1a.availability - map.ht1a.availability).abs() < f32::EPSILON);
    }
}