oxictl 0.1.1

Pure Rust Real-Time Control Systems Framework
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
use crate::core::scalar::ControlScalar;

/// Errors returned by energy storage models.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EnergyStorageError {
    /// A parameter was out of its valid range.
    InvalidParameter,
    /// Supercapacitor voltage exceeded its maximum rating.
    OverVoltage,
    /// Supercapacitor voltage went below zero.
    UnderVoltage,
    /// Battery state of charge exhausted (over-discharged).
    OverDischarge,
}

impl core::fmt::Display for EnergyStorageError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            EnergyStorageError::InvalidParameter => {
                write!(f, "EnergyStorageError: invalid parameter")
            }
            EnergyStorageError::OverVoltage => {
                write!(f, "EnergyStorageError: over-voltage on supercapacitor")
            }
            EnergyStorageError::UnderVoltage => {
                write!(f, "EnergyStorageError: under-voltage on supercapacitor")
            }
            EnergyStorageError::OverDischarge => {
                write!(f, "EnergyStorageError: battery over-discharged")
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Supercapacitor
// ---------------------------------------------------------------------------

/// Supercapacitor (ultracapacitor) simple RC model.
///
/// ## Model
///
/// ```text
///   I_sc ──┬── R_sc ──┬──  terminal
///          │          │
///         C_sc       V_sc
///          │          │
///   GND ───┴──────────┘
/// ```
///
/// **State**: internal capacitor voltage `V_sc` \[V\].
///
/// **Dynamics** (Euler integration):
/// ```text
/// V_sc_dot = I_sc / C_sc − V_sc / (R_sc · C_sc)
/// ```
///
/// **Terminal voltage**:
/// ```text
/// V_term = V_sc − I_sc · R_sc     (ESR drop)
/// ```
///
/// Positive current `I_sc` means **charging** (current flows into the SC).
///
/// ## Energy
/// ```text
/// E = 0.5 · C_sc · V_sc²     [J]
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Supercapacitor<S: ControlScalar> {
    /// Internal (capacitor) voltage \[V\].
    v: S,
    /// Capacitance \[F\].
    c_sc: S,
    /// Equivalent series resistance (ESR) \[Ω\].
    r_sc: S,
    /// Maximum allowable voltage \[V\].
    v_max: S,
    /// Integration time step \[s\].
    dt: S,
}

impl<S: ControlScalar> Supercapacitor<S> {
    /// Create a new supercapacitor model.
    ///
    /// # Parameters
    /// - `c_sc`   — Capacitance \[F\], must be > 0
    /// - `r_sc`   — ESR \[Ω\], must be ≥ 0
    /// - `v_max`  — Maximum voltage rating \[V\], must be > 0
    /// - `v_init` — Initial voltage \[V\], must be in \[0, v_max\]
    /// - `dt`     — Integration step \[s\], must be > 0
    ///
    /// # Errors
    /// [`EnergyStorageError::InvalidParameter`] if any constraint fails.
    pub fn new(c_sc: S, r_sc: S, v_max: S, v_init: S, dt: S) -> Result<Self, EnergyStorageError> {
        if c_sc <= S::ZERO || r_sc < S::ZERO || v_max <= S::ZERO || dt <= S::ZERO {
            return Err(EnergyStorageError::InvalidParameter);
        }
        if v_init < S::ZERO || v_init > v_max {
            return Err(EnergyStorageError::InvalidParameter);
        }
        Ok(Self {
            v: v_init,
            c_sc,
            r_sc,
            v_max,
            dt,
        })
    }

    /// Advance the supercapacitor by one time step.
    ///
    /// # Parameters
    /// - `i_sc` — Current into the supercapacitor \[A\].
    ///   Positive = charging, negative = discharging.
    ///
    /// # Returns
    /// Terminal voltage \[V\] on success.
    ///
    /// # Errors
    /// - [`EnergyStorageError::OverVoltage`] if the internal voltage exceeds `v_max`.
    pub fn step(&mut self, i_sc: S) -> Result<S, EnergyStorageError> {
        // dV/dt = I/C - V/(R*C)   [second term absent when R ≈ 0]
        let v_dot = if self.r_sc > S::EPSILON {
            i_sc / self.c_sc - self.v / (self.r_sc * self.c_sc)
        } else {
            i_sc / self.c_sc
        };

        self.v += v_dot * self.dt;

        // Clamp at zero (capacitor cannot have negative voltage in this model)
        if self.v < S::ZERO {
            self.v = S::ZERO;
        }

        if self.v > self.v_max {
            return Err(EnergyStorageError::OverVoltage);
        }

        // Terminal voltage accounts for ESR drop
        let v_term = self.v - i_sc * self.r_sc;
        Ok(v_term)
    }

    /// Internal capacitor voltage \[V\].
    #[inline]
    pub fn voltage(&self) -> S {
        self.v
    }

    /// State of charge = V_sc / V_max, clamped to \[0, 1\].
    #[inline]
    pub fn soc(&self) -> S {
        (self.v / self.v_max).clamp_val(S::ZERO, S::ONE)
    }

    /// Stored energy \[J\] = 0.5 · C · V².
    #[inline]
    pub fn energy(&self) -> S {
        S::HALF * self.c_sc * self.v * self.v
    }

    /// Maximum voltage rating \[V\].
    #[inline]
    pub fn v_max(&self) -> S {
        self.v_max
    }

    /// Capacitance \[F\].
    #[inline]
    pub fn capacitance(&self) -> S {
        self.c_sc
    }
}

// ---------------------------------------------------------------------------
// Simplified first-order battery (private, used only by HybridEnergyStorage)
// ---------------------------------------------------------------------------

/// First-order simplified battery model.
///
/// Models SOC via Coulomb counting and terminal voltage via a static IR model.
/// No dynamic RC branch (use [`crate::sim::battery::TheveninBattery`] for that).
///
/// **Dynamics**:
/// ```text
/// soc_dot = −I / (3600 · Q_nom)
/// V_term  = V_nom − R_bat · I
/// ```
///
/// Positive current = discharge.
#[derive(Debug, Clone, Copy)]
struct SimpleBattery<S: ControlScalar> {
    soc: S,
    q_nom: S, // nominal capacity [Ah]
    r_bat: S, // internal resistance [Ω]
    v_nom: S, // nominal voltage [V]
    dt: S,
}

impl<S: ControlScalar> SimpleBattery<S> {
    fn new(q_nom: S, r_bat: S, v_nom: S, soc_init: S, dt: S) -> Result<Self, EnergyStorageError> {
        if q_nom <= S::ZERO || r_bat < S::ZERO || v_nom <= S::ZERO || dt <= S::ZERO {
            return Err(EnergyStorageError::InvalidParameter);
        }
        if soc_init < S::ZERO || soc_init > S::ONE {
            return Err(EnergyStorageError::InvalidParameter);
        }
        Ok(Self {
            soc: soc_init,
            q_nom,
            r_bat,
            v_nom,
            dt,
        })
    }

    /// Step the simplified battery.
    ///
    /// SOC is clamped to \[0, 1\]; no error is returned for SOC limits.
    fn step(&mut self, current: S) -> S {
        let soc_dot = -current / (S::from_f64(3600.0) * self.q_nom);
        self.soc = (self.soc + soc_dot * self.dt).clamp_val(S::ZERO, S::ONE);
        self.v_nom - self.r_bat * current
    }

    #[inline]
    fn soc(&self) -> S {
        self.soc
    }
}

// ---------------------------------------------------------------------------
// Hybrid Energy Storage System
// ---------------------------------------------------------------------------

/// Hybrid Energy Storage System (HESS): battery + supercapacitor.
///
/// ## Power split strategy
///
/// A rule-based split allocates current between the supercapacitor (SC) and
/// battery based on the SC state of charge:
///
/// | SC SoC         | Split ratio α (SC fraction) |
/// |----------------|-----------------------------|
/// | SoC > 0.8      | `α_base × 1.5` (SC-heavy)  |
/// | SoC < 0.2      | `α_base × 0.5` (bat-heavy)  |
/// | otherwise      | `α_base`                    |
///
/// `α` is always clamped to \[0, 1\].
///
/// ```text
/// I_sc  = I_total × α
/// I_bat = I_total × (1 − α)
/// ```
///
/// Positive total current = discharge (power delivered to the load).
///
/// ## Architecture
///
/// ```text
/// Load ←── I_total ──┬── I_bat ──[ SimpleBattery ]
//////                    └── I_sc  ──[ Supercapacitor ]
/// ```
#[derive(Debug, Clone, Copy)]
pub struct HybridEnergyStorage<S: ControlScalar> {
    battery: SimpleBattery<S>,
    sc: Supercapacitor<S>,
    /// Base fraction of total current sent to the supercapacitor \[0, 1\].
    alpha_base: S,
}

impl<S: ControlScalar> HybridEnergyStorage<S> {
    /// Create a new hybrid energy storage system.
    ///
    /// # Parameters
    /// - `q_nom`       — Battery nominal capacity \[Ah\]
    /// - `r_bat`       — Battery internal resistance \[Ω\]
    /// - `v_nom`       — Battery nominal voltage \[V\]
    /// - `bat_soc_init`— Battery initial SOC \[0, 1\]
    /// - `c_sc`        — SC capacitance \[F\]
    /// - `r_sc`        — SC ESR \[Ω\]
    /// - `v_sc_max`    — SC maximum voltage \[V\]
    /// - `v_sc_init`   — SC initial voltage \[V\]
    /// - `alpha_base`  — Base SC power split ratio \[0, 1\]
    /// - `dt`          — Integration step \[s\]
    ///
    /// # Errors
    /// [`EnergyStorageError::InvalidParameter`] if any constraint is violated.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        q_nom: S,
        r_bat: S,
        v_nom: S,
        bat_soc_init: S,
        c_sc: S,
        r_sc: S,
        v_sc_max: S,
        v_sc_init: S,
        alpha_base: S,
        dt: S,
    ) -> Result<Self, EnergyStorageError> {
        if alpha_base < S::ZERO || alpha_base > S::ONE || dt <= S::ZERO {
            return Err(EnergyStorageError::InvalidParameter);
        }
        let battery = SimpleBattery::new(q_nom, r_bat, v_nom, bat_soc_init, dt)?;
        let sc = Supercapacitor::new(c_sc, r_sc, v_sc_max, v_sc_init, dt)?;
        Ok(Self {
            battery,
            sc,
            alpha_base,
        })
    }

    /// Convenience constructor: small EV-scale hybrid storage.
    ///
    /// - Battery: 50 Ah, 0.05 Ω, 400 V nominal, SOC = 0.8
    /// - SC: 100 F, 0.01 Ω ESR, 48 V max, initial 40 V
    /// - α_base = 0.3, dt = 1 ms
    pub fn default_hybrid() -> Result<Self, EnergyStorageError> {
        Self::new(
            S::from_f64(50.0),
            S::from_f64(0.05),
            S::from_f64(400.0),
            S::from_f64(0.8),
            S::from_f64(100.0),
            S::from_f64(0.01),
            S::from_f64(48.0),
            S::from_f64(40.0),
            S::from_f64(0.3),
            S::from_f64(1e-3),
        )
    }

    /// Advance the hybrid system by one step.
    ///
    /// # Parameters
    /// - `i_total` — Total discharge current \[A\].
    ///
    /// # Returns
    /// `(v_bat, v_sc)` — Battery and SC terminal voltages \[V\].
    ///
    /// # Errors
    /// Propagates SC voltage errors (e.g., [`EnergyStorageError::OverVoltage`]).
    pub fn step(&mut self, i_total: S) -> Result<(S, S), EnergyStorageError> {
        let (i_sc, i_bat) = self.split_current(i_total);
        let v_bat = self.battery.step(i_bat);
        let v_sc = self.sc.step(i_sc)?;
        Ok((v_bat, v_sc))
    }

    /// Compute the current split between SC and battery.
    ///
    /// Returns `(i_sc, i_bat)` such that `i_sc + i_bat = i_total`.
    pub fn split_current(&self, i_total: S) -> (S, S) {
        let sc_soc = self.sc.soc();
        let alpha = if sc_soc > S::from_f64(0.8) {
            self.alpha_base * S::from_f64(1.5)
        } else if sc_soc < S::from_f64(0.2) {
            self.alpha_base * S::from_f64(0.5)
        } else {
            self.alpha_base
        };
        let alpha_clamped = alpha.clamp_val(S::ZERO, S::ONE);
        let i_sc = i_total * alpha_clamped;
        let i_bat = i_total * (S::ONE - alpha_clamped);
        (i_sc, i_bat)
    }

    /// Battery state of charge \[0, 1\].
    #[inline]
    pub fn battery_soc(&self) -> S {
        self.battery.soc()
    }

    /// Supercapacitor state of charge \[0, 1\].
    #[inline]
    pub fn sc_soc(&self) -> S {
        self.sc.soc()
    }

    /// Supercapacitor stored energy \[J\].
    #[inline]
    pub fn sc_energy(&self) -> S {
        self.sc.energy()
    }

    /// Supercapacitor internal voltage \[V\].
    #[inline]
    pub fn sc_voltage(&self) -> S {
        self.sc.voltage()
    }

    /// Base SC split ratio α_base.
    #[inline]
    pub fn alpha_base(&self) -> S {
        self.alpha_base
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // ------------------------------------------------------------------
    // Supercapacitor tests
    // ------------------------------------------------------------------

    #[test]
    fn sc_charges_and_discharges() {
        // C=1F, R=0 (ideal SC), v_max=20V, v_init=10V, dt=0.1s
        let mut sc =
            Supercapacitor::<f64>::new(1.0, 0.0, 20.0, 10.0, 0.1).expect("SC construction");

        // Charge: positive current increases voltage
        for _ in 0..5 {
            sc.step(5.0).expect("charge step");
        }
        let v_after_charge = sc.voltage();
        assert!(
            v_after_charge > 10.0,
            "Voltage should increase during charging, got {v_after_charge}"
        );

        // Discharge: negative current decreases voltage
        let v_before_discharge = sc.voltage();
        for _ in 0..5 {
            sc.step(-5.0).expect("discharge step");
        }
        let v_after_discharge = sc.voltage();
        assert!(
            v_after_discharge < v_before_discharge,
            "Voltage should decrease during discharging: {v_after_discharge} < {v_before_discharge}"
        );
    }

    #[test]
    fn sc_energy_correct() {
        // C=1F, V=10V → E = 0.5 * 1 * 100 = 50 J
        let sc = Supercapacitor::<f64>::new(1.0, 0.0, 20.0, 10.0, 0.1).expect("SC construction");
        let energy = sc.energy();
        assert!(
            (energy - 50.0).abs() < 1e-10,
            "Energy should be 50 J, got {energy}"
        );
    }

    #[test]
    fn sc_over_voltage_detected() {
        // C=1F, v_max=12V, v_init=11.9V, dt=0.1s, R=0 (ideal SC)
        // Each step with 5A: dV = 5/1 * 0.1 = 0.5V → 12.4V > 12V → OverVoltage
        let mut sc =
            Supercapacitor::<f64>::new(1.0, 0.0, 12.0, 11.9, 0.1).expect("SC construction");

        // Each step with 5A charges: dV = 5/1 * 0.1 = 0.5V → 12.4V > 12V → error
        let result = sc.step(5.0);
        assert!(
            matches!(result, Err(EnergyStorageError::OverVoltage)),
            "Expected OverVoltage, got {result:?}"
        );
    }

    #[test]
    fn sc_soc_in_range() {
        let sc = Supercapacitor::<f64>::new(100.0, 0.01, 48.0, 40.0, 1e-3).expect("SC");
        let soc = sc.soc();
        assert!((0.0..=1.0).contains(&soc), "SoC {soc} must be in [0, 1]");
        // 40/48 ≈ 0.833
        assert!(
            (soc - 40.0 / 48.0).abs() < 1e-10,
            "Expected SoC ≈ {:.4}, got {soc:.4}",
            40.0 / 48.0
        );
    }

    // ------------------------------------------------------------------
    // Hybrid Energy Storage tests
    // ------------------------------------------------------------------

    #[test]
    fn hybrid_step_returns_two_voltages() {
        let mut hess = HybridEnergyStorage::<f64>::default_hybrid().expect("default_hybrid");
        let result = hess.step(10.0);
        assert!(result.is_ok(), "Hybrid step should succeed, got {result:?}");
        let (v_bat, v_sc) = result.unwrap();
        assert!(
            v_bat > 0.0,
            "Battery voltage should be positive, got {v_bat}"
        );
        assert!(v_sc > 0.0, "SC voltage should be positive, got {v_sc}");
    }

    #[test]
    fn split_ratio_shifts_with_sc_soc() {
        // alpha_base = 0.4
        // High SC SoC (> 0.8): alpha = 0.4 * 1.5 = 0.6
        // Low SC SoC (< 0.2):  alpha = 0.4 * 0.5 = 0.2

        // Create HESS with SC at high SoC (v_init = v_max → SoC = 1.0 > 0.8)
        let hess_high = HybridEnergyStorage::<f64>::new(
            50.0, 0.05, 400.0, 0.8, 100.0, 0.01, 48.0,
            48.0, // v_sc_init = v_max → SoC = 1.0 > 0.8
            0.4,  // alpha_base
            1e-3,
        )
        .expect("high-SoC HESS");

        let i_total = 100.0_f64;
        let (i_sc_high, i_bat_high) = hess_high.split_current(i_total);
        // alpha = 0.4 * 1.5 = 0.6 → i_sc = 60, i_bat = 40
        assert!(
            (i_sc_high - 60.0).abs() < 1e-10,
            "i_sc at high SoC should be 60, got {i_sc_high}"
        );
        assert!(
            (i_bat_high - 40.0).abs() < 1e-10,
            "i_bat at high SoC should be 40, got {i_bat_high}"
        );
        // Verify sum
        assert!(
            (i_sc_high + i_bat_high - i_total).abs() < 1e-10,
            "Currents must sum to i_total"
        );

        // Create HESS with SC at low SoC (v_init ≈ 0 → SoC = 0 < 0.2)
        let hess_low = HybridEnergyStorage::<f64>::new(
            50.0, 0.05, 400.0, 0.8, 100.0, 0.01, 48.0,
            1.0, // v_sc_init very low → SoC ≈ 1/48 ≈ 0.02 < 0.2
            0.4, // alpha_base
            1e-3,
        )
        .expect("low-SoC HESS");

        let (i_sc_low, i_bat_low) = hess_low.split_current(i_total);
        // alpha = 0.4 * 0.5 = 0.2 → i_sc = 20, i_bat = 80
        assert!(
            (i_sc_low - 20.0).abs() < 1e-10,
            "i_sc at low SoC should be 20, got {i_sc_low}"
        );
        assert!(
            (i_bat_low - 80.0).abs() < 1e-10,
            "i_bat at low SoC should be 80, got {i_bat_low}"
        );
        assert!(
            (i_sc_low + i_bat_low - i_total).abs() < 1e-10,
            "Currents must sum to i_total"
        );
    }

    #[test]
    fn hybrid_battery_soc_decreases_on_discharge() {
        let mut hess = HybridEnergyStorage::<f64>::default_hybrid().expect("default_hybrid");
        let initial_bat_soc = hess.battery_soc();
        for _ in 0..100 {
            hess.step(50.0).expect("hybrid step");
        }
        assert!(
            hess.battery_soc() < initial_bat_soc,
            "Battery SoC should decrease during discharge: {} < {}",
            hess.battery_soc(),
            initial_bat_soc
        );
    }

    #[test]
    fn invalid_params_rejected() {
        // alpha_base > 1
        let res = HybridEnergyStorage::<f64>::new(
            50.0, 0.05, 400.0, 0.8, 100.0, 0.01, 48.0, 40.0, 1.5, 1e-3,
        );
        assert!(
            matches!(res, Err(EnergyStorageError::InvalidParameter)),
            "Expected InvalidParameter for alpha_base > 1"
        );

        // dt = 0
        let res = HybridEnergyStorage::<f64>::new(
            50.0, 0.05, 400.0, 0.8, 100.0, 0.01, 48.0, 40.0, 0.3, 0.0,
        );
        assert!(
            matches!(res, Err(EnergyStorageError::InvalidParameter)),
            "Expected InvalidParameter for dt = 0"
        );

        // SC: c_sc = 0
        let res = Supercapacitor::<f64>::new(0.0, 0.01, 48.0, 40.0, 1e-3);
        assert!(
            matches!(res, Err(EnergyStorageError::InvalidParameter)),
            "Expected InvalidParameter for c_sc = 0"
        );
    }

    #[test]
    fn sc_zero_current_stable() {
        // With zero current, SC voltage should remain constant
        let mut sc = Supercapacitor::<f64>::new(10.0, 0.1, 50.0, 25.0, 1e-3).expect("SC");
        let v_init = sc.voltage();
        // With R > 0 and zero current: dV = -V/(R*C) → voltage decays
        // After short time it should just be close to initial (small decay)
        for _ in 0..100 {
            sc.step(0.0).expect("zero current step");
        }
        // Decay: τ = R*C = 0.1*10 = 1s, after 0.1s ≈ 90% of initial
        let v_after = sc.voltage();
        assert!(
            v_after < v_init && v_after > v_init * 0.5,
            "SC voltage should decay slowly with zero current: {v_after} vs init {v_init}"
        );
    }
}