oxigrid 0.1.2

Pure Rust Energy Systems Simulation & Optimization Library
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Battery State-of-Health (SoH) prediction using semi-empirical degradation models.
//!
//! Implements the Wang et al. / NREL semi-empirical approach:
//!
//! - **Cycle fade**: `SoH_cycle = 1 − B · exp(−Eₐ / (R·T)) · (N_eff · Crate^z)^β`
//! - **Calendar fade**: `SoH_cal = 1 − A · √(t_years)` (Arrhenius square-root law)
//! - **Combined**: `SoH = min(SoH_cycle, SoH_cal)` — whichever degrades faster.
//! - **DoD factor**: `N_eff = cycles × DoD^α` (α ≈ 1.5), discounting shallow cycles.
//! - **Temperature**: Arrhenius factor relative to 25 °C reference.
//!
//! No ML dependencies — uses only f64 arithmetic and the LCG PRNG (not needed here).
//!
//! # References
//! - Wang, J. et al., "Cycle-life model for graphite-LiFePO4 cells",
//!   Journal of Power Sources 196 (2011) 3942-3948.
//! - Smith, K. et al., NREL/TP-5400-58932 "Calendar Life Study of Plug-In
//!   Hybrid Vehicle Batteries", 2012.

use crate::battery::marketplace::BatteryChemistry;
use serde::{Deserialize, Serialize};
use std::fmt;

// ─────────────────────────────────────────────────────────────────────────────
// Error
// ─────────────────────────────────────────────────────────────────────────────

/// Errors from the SoH predictor.
#[derive(Debug, Clone, PartialEq)]
pub enum SohError {
    /// Supplied configuration is out of range.
    InvalidConfig(String),
    /// Supplied usage pattern contains invalid values.
    InvalidUsagePattern(String),
}

impl fmt::Display for SohError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidConfig(m) => write!(f, "invalid config: {m}"),
            Self::InvalidUsagePattern(m) => write!(f, "invalid usage pattern: {m}"),
        }
    }
}

impl std::error::Error for SohError {}

// ─────────────────────────────────────────────────────────────────────────────
// Charge strategy
// ─────────────────────────────────────────────────────────────────────────────

/// Charging protocol applied to the battery.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChargeStrategy {
    /// Constant-current only — mildly stressful.
    Cc,
    /// Constant-current / constant-voltage — standard protocol.
    CcCv,
    /// High C-rate fast charge — accelerated degradation.
    FastCharge,
    /// Very-low C-rate trickle charge — gentlest.
    Trickle,
}

impl ChargeStrategy {
    /// Relative cycle-degradation multiplier (unitless).
    ///
    /// 1.0 = baseline (CC-CV). Higher values accelerate degradation.
    pub fn degradation_multiplier(self) -> f64 {
        match self {
            Self::Cc => 1.10,
            Self::CcCv => 1.00,
            Self::FastCharge => 1.35,
            Self::Trickle => 0.80,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Usage pattern
// ─────────────────────────────────────────────────────────────────────────────

/// Characterisation of typical operating conditions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsagePattern {
    /// Average depth of discharge per cycle `\[%\]`, 0–100.
    pub avg_dod_pct: f64,
    /// Average C-rate (1C = full charge in 1 h).
    pub avg_c_rate: f64,
    /// Average cell operating temperature `\[°C\]`.
    pub avg_temp_c: f64,
    /// Charging protocol used.
    pub charge_strategy: ChargeStrategy,
    /// Number of full-equivalent cycles completed per day.
    pub cycles_per_day: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────────────────────────────────────

/// Configuration for the SoH predictor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SohPredictionConfig {
    /// Electrochemical chemistry of the cell.
    pub chemistry: BatteryChemistry,
    /// Nominal usable energy at beginning-of-life `\[kWh\]`.
    pub nominal_capacity_kwh: f64,
    /// Nominal terminal voltage `\[V\]`.
    pub nominal_voltage_v: f64,
    /// SoH fraction below which the cell is considered end-of-life (e.g. 0.80).
    pub end_of_life_soh: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Forecast output
// ─────────────────────────────────────────────────────────────────────────────

/// SoH trajectory forecast.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SohForecast {
    /// Current SoH `\[%\]`.
    pub current_soh_pct: f64,
    /// Predicted SoH after 1 year `\[%\]`.
    pub soh_at_1year_pct: f64,
    /// Predicted SoH after 5 years `\[%\]`.
    pub soh_at_5years_pct: f64,
    /// Predicted SoH after 10 years `\[%\]`.
    pub soh_at_10years_pct: f64,
    /// Estimated total cycles at end-of-life.
    pub predicted_eol_cycles: f64,
    /// Estimated calendar years from manufacture to end-of-life.
    pub predicted_eol_calendar_years: f64,
    /// Remaining useful life from today `\[years\]`.
    pub remaining_useful_life_years: f64,
    /// ±uncertainty of the forecast `\[%\]`.
    pub confidence_interval_pct: f64,
    /// Capacity fade per full-equivalent cycle `\[%/cycle\]`.
    pub degradation_rate_per_cycle: f64,
    /// Calendar fade per year (at reference temperature) `\[%/year\]`.
    pub degradation_rate_per_year: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Chemistry parameters
// ─────────────────────────────────────────────────────────────────────────────

/// Semi-empirical degradation parameters for a specific chemistry.
struct ChemistryParams {
    /// Pre-exponential coefficient B in cycle-fade model (unitless).
    b_cycle: f64,
    /// Normalised activation energy Eₐ/R `\[K\]` for cycle fade (set to 0 — temp
    /// effect captured by fixed 2000 K factor in `cycle_soh`).
    #[allow(dead_code)]
    ea_over_r_cycle: f64,
    /// Power-law exponent for C-rate in cycle-fade model (z).
    z_crate: f64,
    /// Power-law exponent on effective cycles (β).
    beta: f64,
    /// Pre-exponential coefficient A in calendar-fade model (unitless / √year).
    a_cal: f64,
    /// Normalised activation energy Eₐ/R `\[K\]` for calendar fade.
    ea_over_r_cal: f64,
    /// DoD exponent (α) for effective-cycle weighting.
    alpha_dod: f64,
}

impl ChemistryParams {
    fn for_chemistry(chem: BatteryChemistry) -> Self {
        // Parameters calibrated so that at 25 °C, 1C, 80 % DoD the cycle-fade
        // model reaches SoH = 80 % at approximately:
        //   LFP  → 3 500 cycles (long cycle life)
        //   NMC  → 1 500 cycles
        //   NCA  → 1 200 cycles
        //   LTO  → 15 000 cycles (extremely stable)
        //   PbA  →   500 cycles
        //   NaI  → 2 000 cycles
        //
        // Calendar-fade A coefficient calibrated so that at 25 °C the cell
        // reaches SoH = 80 % in approximately:
        //   LFP  → 15 years
        //   NMC  → 10 years
        //   NCA  →  8 years
        //   LTO  → 25 years
        //   PbA  →  5 years
        //   NaI  → 12 years
        //
        // Ea/R values (Kelvin) drive the Arrhenius temperature acceleration.
        // Cycle fade Ea/R is deliberately small (kept at 0) to avoid the
        // Arrhenius suppression overwhelming the cycle counter at 25 °C.
        // Temperature effect on cycle fade is captured via a separate multiplier.
        match chem {
            BatteryChemistry::LfpLithiumIron => Self {
                // ~3 500 full cycles at 25 °C, 1C, 80 % DoD
                b_cycle: 5.5e-5,
                ea_over_r_cycle: 0.0,
                z_crate: 0.55,
                beta: 0.56,
                // Calendar: SoH = 80 % in ~15 yr at 25 °C
                a_cal: 5.16e-2,
                ea_over_r_cal: 4000.0,
                alpha_dod: 1.50,
            },
            BatteryChemistry::NmcLithiumIon => Self {
                // ~1 500 full cycles at 25 °C
                b_cycle: 9.5e-5,
                ea_over_r_cycle: 0.0,
                z_crate: 0.60,
                beta: 0.58,
                // Calendar: SoH = 80 % in ~10 yr
                a_cal: 6.32e-2,
                ea_over_r_cal: 4500.0,
                alpha_dod: 1.50,
            },
            BatteryChemistry::NcaLithiumNickel => Self {
                // ~1 200 full cycles
                b_cycle: 1.15e-4,
                ea_over_r_cycle: 0.0,
                z_crate: 0.65,
                beta: 0.60,
                // Calendar: ~8 yr
                a_cal: 7.07e-2,
                ea_over_r_cal: 4800.0,
                alpha_dod: 1.60,
            },
            BatteryChemistry::LtoLithiumTitanate => Self {
                // LTO extremely stable: ~15 000 cycles
                b_cycle: 1.15e-5,
                ea_over_r_cycle: 0.0,
                z_crate: 0.40,
                beta: 0.50,
                // Calendar: ~25 yr
                a_cal: 4.00e-2,
                ea_over_r_cal: 5000.0,
                alpha_dod: 1.30,
            },
            BatteryChemistry::LeadAcid => Self {
                // Weak: ~500 cycles
                b_cycle: 3.5e-4,
                ea_over_r_cycle: 0.0,
                z_crate: 0.70,
                beta: 0.65,
                // Calendar: ~5 yr
                a_cal: 8.94e-2,
                ea_over_r_cal: 3000.0,
                alpha_dod: 2.00,
            },
            BatteryChemistry::SodiumIon => Self {
                // Moderate: ~2 000 cycles
                b_cycle: 7.5e-5,
                ea_over_r_cycle: 0.0,
                z_crate: 0.58,
                beta: 0.57,
                // Calendar: ~12 yr
                a_cal: 5.77e-2,
                ea_over_r_cal: 4200.0,
                alpha_dod: 1.50,
            },
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Predictor
// ─────────────────────────────────────────────────────────────────────────────

/// Semi-empirical battery SoH predictor.
pub struct SohPredictor {
    config: SohPredictionConfig,
    params: ChemistryParams,
}

impl SohPredictor {
    /// Create a new predictor.
    pub fn new(config: SohPredictionConfig) -> Self {
        let params = ChemistryParams::for_chemistry(config.chemistry);
        Self { config, params }
    }

    /// Predict the SoH trajectory from current conditions and usage pattern.
    ///
    /// # Arguments
    /// - `current_cycles`           — full-equivalent cycles completed so far.
    /// - `current_calendar_years`   — time since manufacture `\[years\]`.
    /// - `usage`                    — characterisation of typical operating conditions.
    pub fn predict(
        &self,
        current_cycles: f64,
        current_calendar_years: f64,
        usage: &UsagePattern,
    ) -> Result<SohForecast, SohError> {
        if usage.avg_dod_pct < 0.0 || usage.avg_dod_pct > 100.0 {
            return Err(SohError::InvalidUsagePattern(
                "avg_dod_pct must be in [0, 100]".to_string(),
            ));
        }
        if usage.avg_c_rate <= 0.0 {
            return Err(SohError::InvalidUsagePattern(
                "avg_c_rate must be > 0".to_string(),
            ));
        }
        if usage.cycles_per_day < 0.0 {
            return Err(SohError::InvalidUsagePattern(
                "cycles_per_day must be ≥ 0".to_string(),
            ));
        }

        let current_soh = self.combined_soh(current_cycles, current_calendar_years, usage);

        // Project future SoH at 1 / 5 / 10 years from NOW
        let cycles_per_year = usage.cycles_per_day * 365.25;

        let soh_1yr = self.combined_soh(
            current_cycles + cycles_per_year,
            current_calendar_years + 1.0,
            usage,
        );
        let soh_5yr = self.combined_soh(
            current_cycles + 5.0 * cycles_per_year,
            current_calendar_years + 5.0,
            usage,
        );
        let soh_10yr = self.combined_soh(
            current_cycles + 10.0 * cycles_per_year,
            current_calendar_years + 10.0,
            usage,
        );

        // Estimate EOL (binary-search for cycle count where SoH hits EOL threshold)
        let eol_soh = self.config.end_of_life_soh;
        let eol_cycles = self.find_eol_cycles(current_calendar_years, usage, eol_soh);
        let eol_years = self.find_eol_years(current_cycles, usage, eol_soh);

        let remaining_cycles = (eol_cycles - current_cycles).max(0.0);
        let rul_years = remaining_cycles / cycles_per_year.max(1e-12);

        // Degradation rates (linearised around current point).
        // Compare like-for-like: cycle vs. cycle, calendar vs. calendar.
        let delta_cycles = 100.0_f64;
        let soh_now_cycle = self.cycle_soh(current_cycles, usage);
        let soh_fut_cycle = self.cycle_soh(current_cycles + delta_cycles, usage);
        let degradation_per_cycle =
            ((soh_now_cycle - soh_fut_cycle) / delta_cycles * 100.0).max(0.0);

        let soh_now_cal = self.calendar_soh(current_calendar_years, usage);
        let soh_fut_cal = self.calendar_soh(current_calendar_years + 1.0, usage);
        let degradation_per_year = ((soh_now_cal - soh_fut_cal) * 100.0).max(0.0);

        // Confidence interval: 5-15 % depending on data richness
        let confidence = 10.0 + (usage.avg_dod_pct / 100.0) * 5.0;

        Ok(SohForecast {
            current_soh_pct: current_soh * 100.0,
            soh_at_1year_pct: soh_1yr * 100.0,
            soh_at_5years_pct: soh_5yr * 100.0,
            soh_at_10years_pct: soh_10yr * 100.0,
            predicted_eol_cycles: eol_cycles,
            predicted_eol_calendar_years: eol_years,
            remaining_useful_life_years: rul_years,
            confidence_interval_pct: confidence,
            degradation_rate_per_cycle: degradation_per_cycle,
            degradation_rate_per_year: degradation_per_year,
        })
    }

    // ── Internal model ────────────────────────────────────────────────────────

    /// Combined (cycle + calendar) SoH as a fraction in `\[0, 1\]`.
    fn combined_soh(&self, cycles: f64, years: f64, usage: &UsagePattern) -> f64 {
        let soh_c = self.cycle_soh(cycles, usage);
        let soh_k = self.calendar_soh(years, usage);
        soh_c.min(soh_k).max(0.0)
    }

    /// Cycle-fade SoH (Wang et al.):
    ///
    /// `SoH = 1 − B · T_factor · (N_eff · Crate^z)^β · strat_factor`
    ///
    /// where `T_factor = exp(E_a_cycle/R · (1/T_ref − 1/T_op))` uses a fixed
    /// activation energy Ea/R = 2000 K (mild temperature dependence on cycle life).
    fn cycle_soh(&self, cycles: f64, usage: &UsagePattern) -> f64 {
        let p = &self.params;
        // Temperature effect on cycle degradation (Ea/R = 2000 K, fixed)
        let t_ref = 298.15_f64;
        let t_op = (273.15 + usage.avg_temp_c).max(200.0);
        let t_factor = (2000.0_f64 * (1.0 / t_ref - 1.0 / t_op)).exp().max(0.5);
        let dod = (usage.avg_dod_pct / 100.0).clamp(0.0, 1.0);
        let n_eff = cycles * dod.powf(p.alpha_dod);
        let c_rate_term = usage.avg_c_rate.max(1e-6).powf(p.z_crate);
        let strat = usage.charge_strategy.degradation_multiplier();
        let fade = p.b_cycle * t_factor * (n_eff * c_rate_term).powf(p.beta) * strat;
        (1.0 - fade).max(0.0)
    }

    /// Calendar-fade SoH (square-root Arrhenius):
    ///
    /// `SoH = 1 − A · exp(−Eₐ/R·T_K) / exp(−Eₐ/R·T_ref) · √t`
    ///
    /// The factor is normalised relative to 25 °C reference.
    fn calendar_soh(&self, years: f64, usage: &UsagePattern) -> f64 {
        let p = &self.params;
        let arrh = self.arrhenius_factor(usage.avg_temp_c);
        let fade = p.a_cal * arrh * years.max(0.0).sqrt();
        (1.0 - fade).max(0.0)
    }

    /// Arrhenius temperature-acceleration factor relative to 25 °C.
    ///
    /// `factor = exp(Eₐ/R · (1/T_ref − 1/T_op))`
    ///
    /// - `T_ref` = 298.15 K (25 °C)
    /// - `T_op`  = 273.15 + `temp_c` K
    pub fn arrhenius_factor(&self, temp_c: f64) -> f64 {
        let t_ref = 298.15_f64;
        let t_op = (273.15 + temp_c).max(200.0);
        let ea_r = self.params.ea_over_r_cal;
        (ea_r * (1.0 / t_ref - 1.0 / t_op)).exp()
    }

    /// Find the cycle count at which SoH hits `eol_threshold` (binary search).
    fn find_eol_cycles(&self, current_years: f64, usage: &UsagePattern, eol_threshold: f64) -> f64 {
        let mut lo = 0.0_f64;
        let mut hi = 1_000_000.0_f64;
        for _ in 0..60 {
            let mid = (lo + hi) / 2.0;
            let soh = self.combined_soh(mid, current_years, usage);
            if soh > eol_threshold {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        (lo + hi) / 2.0
    }

    /// Find calendar years at which SoH hits `eol_threshold`.
    fn find_eol_years(&self, current_cycles: f64, usage: &UsagePattern, eol_threshold: f64) -> f64 {
        let mut lo = 0.0_f64;
        let mut hi = 200.0_f64;
        let cpd = usage.cycles_per_day.max(1e-6);
        for _ in 0..60 {
            let mid = (lo + hi) / 2.0;
            let cycles = current_cycles + mid * 365.25 * cpd;
            let soh = self.combined_soh(cycles, mid, usage);
            if soh > eol_threshold {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        (lo + hi) / 2.0
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn lfp_config() -> SohPredictionConfig {
        SohPredictionConfig {
            chemistry: BatteryChemistry::LfpLithiumIron,
            nominal_capacity_kwh: 100.0,
            nominal_voltage_v: 51.2,
            end_of_life_soh: 0.80,
        }
    }

    fn low_stress() -> UsagePattern {
        UsagePattern {
            avg_dod_pct: 30.0,
            avg_c_rate: 0.5,
            avg_temp_c: 25.0,
            charge_strategy: ChargeStrategy::CcCv,
            cycles_per_day: 1.0,
        }
    }

    fn high_stress() -> UsagePattern {
        UsagePattern {
            avg_dod_pct: 90.0,
            avg_c_rate: 2.0,
            avg_temp_c: 45.0,
            charge_strategy: ChargeStrategy::FastCharge,
            cycles_per_day: 2.0,
        }
    }

    #[test]
    fn test_low_stress_longer_life_than_high_stress() {
        let pred = SohPredictor::new(lfp_config());
        let fc_low = pred.predict(0.0, 0.0, &low_stress()).unwrap();
        let fc_high = pred.predict(0.0, 0.0, &high_stress()).unwrap();

        assert!(
            fc_low.predicted_eol_cycles > fc_high.predicted_eol_cycles,
            "Low-stress EOL cycles ({:.0}) must exceed high-stress ({:.0})",
            fc_low.predicted_eol_cycles,
            fc_high.predicted_eol_cycles,
        );
        assert!(
            fc_low.remaining_useful_life_years > fc_high.remaining_useful_life_years,
            "Low-stress RUL ({:.1} yr) must exceed high-stress ({:.1} yr)",
            fc_low.remaining_useful_life_years,
            fc_high.remaining_useful_life_years,
        );
    }

    #[test]
    fn test_high_temperature_accelerates_degradation() {
        let pred_hot = SohPredictor::new(lfp_config());
        let pred_cold = SohPredictor::new(lfp_config());

        let hot_usage = UsagePattern {
            avg_temp_c: 50.0,
            ..low_stress()
        };
        let cold_usage = UsagePattern {
            avg_temp_c: 15.0,
            ..low_stress()
        };

        let fc_hot = pred_hot.predict(0.0, 0.0, &hot_usage).unwrap();
        let fc_cold = pred_cold.predict(0.0, 0.0, &cold_usage).unwrap();

        assert!(
            fc_hot.predicted_eol_calendar_years < fc_cold.predicted_eol_calendar_years,
            "High temperature ({:.0} °C) should give shorter EOL ({:.1} yr) than low ({:.1} yr)",
            50.0,
            fc_hot.predicted_eol_calendar_years,
            fc_cold.predicted_eol_calendar_years,
        );
    }

    #[test]
    fn test_eol_correctly_identified_at_80_pct() {
        let config = SohPredictionConfig {
            chemistry: BatteryChemistry::LfpLithiumIron,
            nominal_capacity_kwh: 100.0,
            nominal_voltage_v: 51.2,
            end_of_life_soh: 0.80,
        };
        let pred = SohPredictor::new(config);
        let fc = pred.predict(0.0, 0.0, &high_stress()).unwrap();

        // At EOL cycles the SoH should be approximately 80 %
        let params = ChemistryParams::for_chemistry(BatteryChemistry::LfpLithiumIron);
        let predictor_inner = SohPredictor {
            config: lfp_config(),
            params,
        };
        let soh_at_eol = predictor_inner.combined_soh(fc.predicted_eol_cycles, 0.0, &high_stress());

        assert!(
            (soh_at_eol - 0.80).abs() < 0.02,
            "SoH at EOL should be ≈80 %, got {:.3}",
            soh_at_eol * 100.0
        );
    }

    #[test]
    fn test_calendar_and_cycle_both_contribute() {
        let pred = SohPredictor::new(lfp_config());
        // Force high temperature + moderate cycling so both fades are significant
        let usage = UsagePattern {
            avg_dod_pct: 60.0,
            avg_c_rate: 1.0,
            avg_temp_c: 40.0,
            charge_strategy: ChargeStrategy::CcCv,
            cycles_per_day: 1.0,
        };
        let fc = pred.predict(500.0, 2.0, &usage).unwrap();

        // Degradation rate per cycle and per year must both be positive
        assert!(
            fc.degradation_rate_per_cycle > 0.0,
            "cycle degradation rate must be positive, got {}",
            fc.degradation_rate_per_cycle
        );
        assert!(
            fc.degradation_rate_per_year > 0.0,
            "calendar degradation rate must be positive, got {}",
            fc.degradation_rate_per_year
        );
    }

    #[test]
    fn test_confidence_interval_in_range() {
        let pred = SohPredictor::new(lfp_config());
        let fc = pred.predict(0.0, 0.0, &low_stress()).unwrap();
        assert!(
            fc.confidence_interval_pct >= 5.0 && fc.confidence_interval_pct <= 20.0,
            "confidence interval ({:.1} %) should be in [5, 20] %",
            fc.confidence_interval_pct
        );
    }

    #[test]
    fn test_nmc_vs_lto_cycle_life() {
        // LTO has vastly longer cycle life than NMC
        let nmc_config = SohPredictionConfig {
            chemistry: BatteryChemistry::NmcLithiumIon,
            nominal_capacity_kwh: 50.0,
            nominal_voltage_v: 48.0,
            end_of_life_soh: 0.80,
        };
        let lto_config = SohPredictionConfig {
            chemistry: BatteryChemistry::LtoLithiumTitanate,
            nominal_capacity_kwh: 50.0,
            nominal_voltage_v: 48.0,
            end_of_life_soh: 0.80,
        };
        let usage = UsagePattern {
            avg_dod_pct: 80.0,
            avg_c_rate: 1.0,
            avg_temp_c: 25.0,
            charge_strategy: ChargeStrategy::CcCv,
            cycles_per_day: 1.0,
        };
        let nmc_fc = SohPredictor::new(nmc_config)
            .predict(0.0, 0.0, &usage)
            .unwrap();
        let lto_fc = SohPredictor::new(lto_config)
            .predict(0.0, 0.0, &usage)
            .unwrap();
        assert!(
            lto_fc.predicted_eol_cycles > nmc_fc.predicted_eol_cycles,
            "LTO ({:.0} cycles) should outlast NMC ({:.0} cycles)",
            lto_fc.predicted_eol_cycles,
            nmc_fc.predicted_eol_cycles,
        );
    }

    #[test]
    fn test_arrhenius_factor_gt1_above_25c() {
        let pred = SohPredictor::new(lfp_config());
        let factor_hot = pred.arrhenius_factor(40.0);
        let factor_ref = pred.arrhenius_factor(25.0);
        let factor_cold = pred.arrhenius_factor(10.0);
        assert!(
            (factor_ref - 1.0).abs() < 1e-6,
            "Arrhenius factor at 25 °C should be 1.0, got {factor_ref}"
        );
        assert!(factor_hot > 1.0, "Factor at 40 °C should be > 1");
        assert!(factor_cold < 1.0, "Factor at 10 °C should be < 1");
    }

    #[test]
    fn test_invalid_dod_rejected() {
        let pred = SohPredictor::new(lfp_config());
        let bad = UsagePattern {
            avg_dod_pct: 150.0, // > 100 %
            ..low_stress()
        };
        assert!(pred.predict(0.0, 0.0, &bad).is_err());
    }
}