synapse-models 0.1.0

Biophysical synapse models for neural simulations with short-term plasticity
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Receptor dynamics and kinetic models.
//!
//! This module implements various postsynaptic receptor types with detailed
//! kinetic models based on experimental data.

use crate::error::{Result, SynapseError};

/// Trait for receptor dynamics.
pub trait ReceptorDynamics {
    /// Update receptor state given neurotransmitter concentration and membrane voltage.
    ///
    /// # Arguments
    /// * `nt_concentration` - Neurotransmitter concentration (mM)
    /// * `voltage` - Membrane voltage (mV)
    /// * `dt` - Time step (ms)
    fn update(&mut self, nt_concentration: f64, voltage: f64, dt: f64) -> Result<()>;

    /// Get the current open probability or conductance state.
    fn get_conductance(&self) -> f64;

    /// Get the reversal potential for this receptor (mV).
    fn reversal_potential(&self) -> f64;

    /// Reset receptor to resting state.
    fn reset(&mut self);
}

/// AMPA receptor - fast excitatory glutamate receptor.
///
/// AMPA receptors mediate the majority of fast excitatory synaptic transmission.
/// They have rapid kinetics with rise time ~0.2 ms and decay time ~2 ms.
///
/// Model: Two-state kinetic scheme
/// C <-> O (Closed <-> Open)
/// dr/dt = α[NT](1-r) - βr  (rise)
/// do/dt = r/τ_rise - o/τ_decay  (opening and decay)
#[derive(Debug, Clone)]
pub struct AMPAReceptor {
    /// Open probability (0 to 1).
    pub open_probability: f64,

    /// Rising phase variable (0 to 1).
    rise_state: f64,

    /// Rise time constant (ms).
    pub tau_rise: f64,

    /// Decay time constant (ms).
    pub tau_decay: f64,

    /// Forward binding rate (1/(mM·ms)).
    pub alpha: f64,

    /// Unbinding rate (1/ms).
    pub beta: f64,

    /// Reversal potential (mV).
    pub e_rev: f64,

    /// Maximum conductance (nS).
    pub g_max: f64,
}

impl Default for AMPAReceptor {
    fn default() -> Self {
        Self {
            open_probability: 0.0,
            rise_state: 0.0,
            tau_rise: 0.2,   // 0.2 ms rise time
            tau_decay: 2.0,  // 2 ms decay time
            alpha: 1.1,      // Fast binding
            beta: 0.19,      // Relatively slow unbinding
            e_rev: 0.0,      // Non-selective cation channel
            g_max: 1.0,      // Normalized conductance
        }
    }
}

impl AMPAReceptor {
    /// Create a new AMPA receptor with default parameters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create AMPA receptor with custom parameters.
    pub fn with_params(tau_rise: f64, tau_decay: f64, g_max: f64) -> Result<Self> {
        if tau_rise <= 0.0 {
            return Err(SynapseError::InvalidTimeConstant(tau_rise));
        }
        if tau_decay <= 0.0 {
            return Err(SynapseError::InvalidTimeConstant(tau_decay));
        }

        Ok(Self {
            tau_rise,
            tau_decay,
            g_max,
            ..Self::default()
        })
    }

    /// Get the synaptic current (pA).
    pub fn current(&self, voltage: f64) -> f64 {
        self.g_max * self.open_probability * (voltage - self.e_rev)
    }
}

impl ReceptorDynamics for AMPAReceptor {
    fn update(&mut self, nt_concentration: f64, _voltage: f64, dt: f64) -> Result<()> {
        // Update rise state: dr/dt = α[NT](1-r) - βr
        let dr = self.alpha * nt_concentration * (1.0 - self.rise_state)
                 - self.beta * self.rise_state;
        self.rise_state += dr * dt;
        self.rise_state = self.rise_state.clamp(0.0, 1.0);

        // Update open probability using exponential Euler for stability
        // do/dt = r/τ_rise - o/τ_decay
        let target = self.rise_state * self.tau_decay / (self.tau_rise + self.tau_decay);
        let tau_eff = self.tau_decay;
        self.open_probability += (target - self.open_probability) * (1.0 - (-dt / tau_eff).exp());
        self.open_probability = self.open_probability.clamp(0.0, 1.0);

        Ok(())
    }

    fn get_conductance(&self) -> f64 {
        self.g_max * self.open_probability
    }

    fn reversal_potential(&self) -> f64 {
        self.e_rev
    }

    fn reset(&mut self) {
        self.open_probability = 0.0;
        self.rise_state = 0.0;
    }
}

/// NMDA receptor - slow excitatory glutamate receptor with voltage dependence.
///
/// NMDA receptors have slower kinetics and are blocked by Mg2+ at resting potentials.
/// They are critical for synaptic plasticity and learning.
///
/// Key features:
/// - Voltage-dependent Mg2+ block
/// - Slow kinetics (rise ~2 ms, decay ~100 ms)
/// - High Ca2+ permeability
#[derive(Debug, Clone)]
pub struct NMDAReceptor {
    /// Open probability (0 to 1).
    pub open_probability: f64,

    /// Rising phase variable (0 to 1).
    rise_state: f64,

    /// Rise time constant (ms).
    pub tau_rise: f64,

    /// Decay time constant (ms).
    pub tau_decay: f64,

    /// Forward binding rate (1/(mM·ms)).
    pub alpha: f64,

    /// Unbinding rate (1/ms).
    pub beta: f64,

    /// Reversal potential (mV).
    pub e_rev: f64,

    /// Maximum conductance (nS).
    pub g_max: f64,

    /// Mg2+ concentration (mM).
    pub mg_concentration: f64,
}

impl Default for NMDAReceptor {
    fn default() -> Self {
        Self {
            open_probability: 0.0,
            rise_state: 0.0,
            tau_rise: 2.0,    // 2 ms rise time
            tau_decay: 100.0, // 100 ms decay time
            alpha: 0.72,      // Slower binding than AMPA
            beta: 0.0066,     // Very slow unbinding
            e_rev: 0.0,       // Non-selective cation channel
            g_max: 1.0,       // Normalized conductance
            mg_concentration: 1.0, // 1 mM external Mg2+
        }
    }
}

impl NMDAReceptor {
    /// Create a new NMDA receptor with default parameters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create NMDA receptor with custom parameters.
    pub fn with_params(tau_rise: f64, tau_decay: f64, g_max: f64) -> Result<Self> {
        if tau_rise <= 0.0 {
            return Err(SynapseError::InvalidTimeConstant(tau_rise));
        }
        if tau_decay <= 0.0 {
            return Err(SynapseError::InvalidTimeConstant(tau_decay));
        }

        Ok(Self {
            tau_rise,
            tau_decay,
            g_max,
            ..Self::default()
        })
    }

    /// Calculate voltage-dependent Mg2+ block.
    ///
    /// Year and Stevens (1990) model:
    /// B(V) = 1 / (1 + [Mg2+]/3.57 * exp(-0.062 * V))
    pub fn mg_block(&self, voltage: f64) -> f64 {
        1.0 / (1.0 + (self.mg_concentration / 3.57) * (-0.062 * voltage).exp())
    }

    /// Get the synaptic current (pA).
    pub fn current(&self, voltage: f64) -> f64 {
        let mg_block = self.mg_block(voltage);
        self.g_max * self.open_probability * mg_block * (voltage - self.e_rev)
    }
}

impl ReceptorDynamics for NMDAReceptor {
    fn update(&mut self, nt_concentration: f64, _voltage: f64, dt: f64) -> Result<()> {
        // Update rise state
        let dr = self.alpha * nt_concentration * (1.0 - self.rise_state)
                 - self.beta * self.rise_state;
        self.rise_state += dr * dt;
        self.rise_state = self.rise_state.clamp(0.0, 1.0);

        // Update open probability with exponential Euler
        let target = self.rise_state * self.tau_decay / (self.tau_rise + self.tau_decay);
        let tau_eff = self.tau_decay;
        self.open_probability += (target - self.open_probability) * (1.0 - (-dt / tau_eff).exp());
        self.open_probability = self.open_probability.clamp(0.0, 1.0);

        Ok(())
    }

    fn get_conductance(&self) -> f64 {
        self.g_max * self.open_probability
    }

    fn reversal_potential(&self) -> f64 {
        self.e_rev
    }

    fn reset(&mut self) {
        self.open_probability = 0.0;
        self.rise_state = 0.0;
    }
}

/// GABA-A receptor - fast inhibitory receptor.
///
/// GABA-A receptors are ionotropic chloride channels that mediate fast
/// inhibitory transmission with time constants similar to AMPA receptors.
#[derive(Debug, Clone)]
pub struct GABAAReceptor {
    /// Open probability (0 to 1).
    pub open_probability: f64,

    /// Rising phase variable (0 to 1).
    rise_state: f64,

    /// Rise time constant (ms).
    pub tau_rise: f64,

    /// Decay time constant (ms).
    pub tau_decay: f64,

    /// Forward binding rate (1/(mM·ms)).
    pub alpha: f64,

    /// Unbinding rate (1/ms).
    pub beta: f64,

    /// Reversal potential (mV) - depends on Cl- gradient.
    pub e_rev: f64,

    /// Maximum conductance (nS).
    pub g_max: f64,
}

impl Default for GABAAReceptor {
    fn default() -> Self {
        Self {
            open_probability: 0.0,
            rise_state: 0.0,
            tau_rise: 0.5,   // 0.5 ms rise time
            tau_decay: 5.0,  // 5 ms decay time
            alpha: 5.0,      // Fast binding
            beta: 0.18,      // Moderate unbinding
            e_rev: -70.0,    // Chloride reversal (can vary)
            g_max: 1.0,      // Normalized conductance
        }
    }
}

impl GABAAReceptor {
    /// Create a new GABA-A receptor with default parameters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the synaptic current (pA).
    pub fn current(&self, voltage: f64) -> f64 {
        self.g_max * self.open_probability * (voltage - self.e_rev)
    }
}

impl ReceptorDynamics for GABAAReceptor {
    fn update(&mut self, nt_concentration: f64, _voltage: f64, dt: f64) -> Result<()> {
        // Update rise state
        let dr = self.alpha * nt_concentration * (1.0 - self.rise_state)
                 - self.beta * self.rise_state;
        self.rise_state += dr * dt;
        self.rise_state = self.rise_state.clamp(0.0, 1.0);

        // Update open probability
        let target = self.rise_state * self.tau_decay / (self.tau_rise + self.tau_decay);
        let tau_eff = self.tau_decay;
        self.open_probability += (target - self.open_probability) * (1.0 - (-dt / tau_eff).exp());
        self.open_probability = self.open_probability.clamp(0.0, 1.0);

        Ok(())
    }

    fn get_conductance(&self) -> f64 {
        self.g_max * self.open_probability
    }

    fn reversal_potential(&self) -> f64 {
        self.e_rev
    }

    fn reset(&mut self) {
        self.open_probability = 0.0;
        self.rise_state = 0.0;
    }
}

/// GABA-B receptor - slow inhibitory metabotropic receptor.
///
/// GABA-B receptors are G-protein coupled receptors that activate K+ channels,
/// producing slow, long-lasting inhibition.
#[derive(Debug, Clone)]
pub struct GABABReceptor {
    /// Receptor activation state (0 to 1).
    pub activation: f64,

    /// G-protein activation state (0 to 1).
    pub g_protein: f64,

    /// Rise time constant (ms).
    pub tau_rise: f64,

    /// Decay time constant (ms).
    pub tau_decay: f64,

    /// G-protein activation time constant (ms).
    pub tau_gprotein: f64,

    /// Forward binding rate (1/(mM·ms)).
    pub alpha: f64,

    /// Unbinding rate (1/ms).
    pub beta: f64,

    /// Reversal potential (mV) - K+ reversal.
    pub e_rev: f64,

    /// Maximum conductance (nS).
    pub g_max: f64,
}

impl Default for GABABReceptor {
    fn default() -> Self {
        Self {
            activation: 0.0,
            g_protein: 0.0,
            tau_rise: 50.0,   // 50 ms rise time
            tau_decay: 200.0, // 200 ms decay time
            tau_gprotein: 100.0, // G-protein time constant
            alpha: 0.09,      // Slow binding
            beta: 0.0012,     // Very slow unbinding
            e_rev: -90.0,     // K+ reversal potential
            g_max: 1.0,       // Normalized conductance
        }
    }
}

impl GABABReceptor {
    /// Create a new GABA-B receptor with default parameters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the synaptic current (pA).
    pub fn current(&self, voltage: f64) -> f64 {
        // Current depends on G-protein activation
        self.g_max * self.g_protein * (voltage - self.e_rev)
    }
}

impl ReceptorDynamics for GABABReceptor {
    fn update(&mut self, nt_concentration: f64, _voltage: f64, dt: f64) -> Result<()> {
        // Update receptor activation
        let dr = self.alpha * nt_concentration * (1.0 - self.activation)
                 - self.beta * self.activation;
        self.activation += dr * dt;
        self.activation = self.activation.clamp(0.0, 1.0);

        // G-protein activation follows receptor activation with delay
        let dg = (self.activation - self.g_protein) / self.tau_gprotein;
        self.g_protein += dg * dt;
        self.g_protein = self.g_protein.clamp(0.0, 1.0);

        Ok(())
    }

    fn get_conductance(&self) -> f64 {
        self.g_max * self.g_protein
    }

    fn reversal_potential(&self) -> f64 {
        self.e_rev
    }

    fn reset(&mut self) {
        self.activation = 0.0;
        self.g_protein = 0.0;
    }
}

/// Metabotropic glutamate receptor (mGluR).
///
/// mGluRs are G-protein coupled receptors that modulate neuronal excitability
/// through various second messenger pathways.
#[derive(Debug, Clone)]
pub struct MetabotropicGlutamateReceptor {
    /// Receptor activation state (0 to 1).
    pub activation: f64,

    /// G-protein activation state (0 to 1).
    pub g_protein: f64,

    /// Activation time constant (ms).
    pub tau_activation: f64,

    /// Deactivation time constant (ms).
    pub tau_deactivation: f64,

    /// Forward binding rate (1/(mM·ms)).
    pub alpha: f64,

    /// Unbinding rate (1/ms).
    pub beta: f64,
}

impl Default for MetabotropicGlutamateReceptor {
    fn default() -> Self {
        Self {
            activation: 0.0,
            g_protein: 0.0,
            tau_activation: 100.0,   // 100 ms activation
            tau_deactivation: 500.0, // 500 ms deactivation
            alpha: 0.05,             // Slow binding
            beta: 0.002,             // Very slow unbinding
        }
    }
}

impl MetabotropicGlutamateReceptor {
    /// Create a new mGluR with default parameters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get G-protein activation level.
    pub fn get_gprotein_activation(&self) -> f64 {
        self.g_protein
    }
}

impl ReceptorDynamics for MetabotropicGlutamateReceptor {
    fn update(&mut self, nt_concentration: f64, _voltage: f64, dt: f64) -> Result<()> {
        // Update receptor activation
        let dr = self.alpha * nt_concentration * (1.0 - self.activation)
                 - self.beta * self.activation;
        self.activation += dr * dt;
        self.activation = self.activation.clamp(0.0, 1.0);

        // G-protein activation/deactivation
        let tau = if self.activation > self.g_protein {
            self.tau_activation
        } else {
            self.tau_deactivation
        };
        let dg = (self.activation - self.g_protein) / tau;
        self.g_protein += dg * dt;
        self.g_protein = self.g_protein.clamp(0.0, 1.0);

        Ok(())
    }

    fn get_conductance(&self) -> f64 {
        // Metabotropic receptors don't directly contribute to conductance
        0.0
    }

    fn reversal_potential(&self) -> f64 {
        0.0
    }

    fn reset(&mut self) {
        self.activation = 0.0;
        self.g_protein = 0.0;
    }
}

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

    #[test]
    fn test_ampa_receptor_creation() {
        let ampa = AMPAReceptor::new();
        assert_eq!(ampa.open_probability, 0.0);
        assert_eq!(ampa.e_rev, 0.0);
    }

    #[test]
    fn test_ampa_receptor_activation() {
        let mut ampa = AMPAReceptor::new();
        let nt_conc = 1.0; // 1 mM
        let voltage = -65.0;
        let dt = 0.1;

        // Simulate activation
        for _ in 0..100 {
            ampa.update(nt_conc, voltage, dt).unwrap();
        }

        assert!(ampa.open_probability > 0.0);
        assert!(ampa.open_probability <= 1.0);
    }

    #[test]
    fn test_nmda_mg_block() {
        let nmda = NMDAReceptor::new();

        // At -70 mV, strong block
        let block_hyperpol = nmda.mg_block(-70.0);
        assert!(block_hyperpol < 0.1);

        // At 0 mV, partial relief
        let block_depol = nmda.mg_block(0.0);
        assert!(block_depol > block_hyperpol);

        // At +40 mV, nearly complete relief
        let block_strong_depol = nmda.mg_block(40.0);
        assert!(block_strong_depol > 0.8);
    }

    #[test]
    fn test_nmda_receptor_kinetics() {
        let mut nmda = NMDAReceptor::new();
        let nt_conc = 0.5;
        let voltage = 0.0;
        let dt = 0.1;

        // Simulate activation
        for _ in 0..1000 {
            nmda.update(nt_conc, voltage, dt).unwrap();
        }

        assert!(nmda.open_probability > 0.0);
    }

    #[test]
    fn test_gabaa_receptor() {
        let mut gabaa = GABAAReceptor::new();
        assert_eq!(gabaa.e_rev, -70.0);

        gabaa.update(1.0, -65.0, 0.1).unwrap();
        assert!(gabaa.open_probability >= 0.0);
    }

    #[test]
    fn test_gabab_receptor_gprotein() {
        let mut gabab = GABABReceptor::new();

        // Activate with GABA
        for _ in 0..1000 {
            gabab.update(1.0, -65.0, 0.1).unwrap();
        }

        // G-protein should be activated
        assert!(gabab.g_protein > 0.0);
        assert!(gabab.activation > 0.0);
    }

    #[test]
    fn test_receptor_reset() {
        let mut ampa = AMPAReceptor::new();
        ampa.update(1.0, -65.0, 0.1).unwrap();

        ampa.reset();
        assert_eq!(ampa.open_probability, 0.0);
        assert_eq!(ampa.rise_state, 0.0);
    }
}