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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/// Smart EV charging algorithms: uncontrolled, TOU-optimized, V2G, frequency regulation.
///
/// Implements single-vehicle scheduling using greedy price sorting (TOU),
/// analytic dynamic programming (V2G), and AGC signal tracking (freq. reg).
use crate::error::OxiGridError;
use serde::{Deserialize, Serialize};
/// A single EV charging session at a charger.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvSession {
/// Unique vehicle identifier.
pub vehicle_id: usize,
/// Arrival time [hours from day start, e.g. 18.0 = 6 PM].
pub arrival_time: f64,
/// Departure time [hours from day start].
pub departure_time: f64,
/// State of charge on arrival (0–1).
pub soc_arrival: f64,
/// Required SoC at departure (0–1).
pub soc_target: f64,
/// Usable battery capacity \[kWh\].
pub battery_kwh: f64,
/// Maximum AC charge rate \[kW\].
pub max_charge_kw: f64,
/// Maximum V2G discharge rate \[kW\] (0.0 = no V2G capability).
pub max_discharge_kw: f64,
/// Charge efficiency η_c (energy stored / energy drawn from grid).
pub eta_charge: f64,
/// Discharge efficiency η_d (energy delivered to grid / energy drawn from battery).
pub eta_discharge: f64,
/// Battery degradation cost per kWh cycled [$/kWh].
pub degradation_cost: f64,
}
impl Default for EvSession {
fn default() -> Self {
Self {
vehicle_id: 0,
arrival_time: 18.0,
departure_time: 7.0 + 24.0, // next morning 7 AM
soc_arrival: 0.3,
soc_target: 0.8,
battery_kwh: 60.0,
max_charge_kw: 11.0,
max_discharge_kw: 7.4,
eta_charge: 0.92,
eta_discharge: 0.92,
degradation_cost: 0.05,
}
}
}
impl EvSession {
/// Net energy needed to reach `soc_target` from `soc_arrival` \[kWh\].
/// Accounts for charge efficiency.
pub fn energy_needed_kwh(&self) -> f64 {
let delta_soc = (self.soc_target - self.soc_arrival).max(0.0);
// Grid must supply energy / eta_c to store enough
(delta_soc * self.battery_kwh) / self.eta_charge
}
/// Total charging window \[hours\].
pub fn window_hours(&self) -> f64 {
self.departure_time - self.arrival_time
}
/// Maximum energy that can be discharged via V2G without going below soc_target \[kWh\].
/// This is the "headroom" above soc_target for ancillary services.
pub fn v2g_headroom_kwh(&self) -> f64 {
// After reaching soc_target, additional charge above target is headroom
// We assume V2G draws from the buffer above soc_target
let soc_upper = 1.0_f64.min(self.soc_arrival.max(self.soc_target));
(soc_upper - self.soc_target).max(0.0) * self.battery_kwh
}
/// Minimum charge time \[hours\] needed to reach soc_target (at max charge rate).
pub fn min_charge_hours(&self) -> f64 {
let e_need = self.energy_needed_kwh();
if self.max_charge_kw > 1e-9 {
e_need / self.max_charge_kw
} else {
f64::INFINITY
}
}
/// Check whether the charging requirement is feasible given the window.
pub fn is_feasible(&self) -> bool {
self.window_hours() >= self.min_charge_hours() - 1e-9
}
}
/// Smart charging result for one EV session.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChargingSchedule {
/// Vehicle identifier.
pub vehicle_id: usize,
/// Time at the start of each slot [hours from day start].
pub time_slots: Vec<f64>,
/// Power \[kW\] at each slot: positive = charging, negative = V2G discharge.
pub power_kw: Vec<f64>,
/// SoC at the *start* of each slot (last element = SoC at departure).
pub soc_trajectory: Vec<f64>,
/// Total electricity cost paid [$ = Σ price * max(P,0) * dt].
pub energy_cost: f64,
/// Revenue earned from V2G [$ = Σ price * max(-P,0) * dt].
pub v2g_revenue: f64,
/// Battery degradation cost [$ = Σ degradation_cost/kwh * |P| * dt].
pub degradation_cost: f64,
/// Net cost = energy_cost - v2g_revenue + degradation_cost.
pub net_cost: f64,
}
/// Smart charger controller for a single vehicle.
///
/// Holds the time resolution, day-ahead price profile, and charger capacity.
/// All algorithms operate on the same grid of `n_slots = 24 / dt_hours` slots.
pub struct SmartCharger {
/// Time slot duration \[hours\] (default 0.25 = 15 min).
pub dt_hours: f64,
/// Day-ahead electricity price [$/kWh] per time slot.
/// Length must equal `(24.0 / dt_hours).ceil() as usize`.
pub price_profile: Vec<f64>,
/// Maximum grid import/export power at this charger \[kW\].
pub grid_capacity_kw: f64,
}
impl SmartCharger {
/// Construct a `SmartCharger`.
///
/// # Arguments
/// - `dt_hours` — slot duration in hours (e.g. 0.25)
/// - `price_profile` — $/kWh per slot (length = 24/dt_hours)
/// - `grid_capacity_kw` — max charger power \[kW\]
pub fn new(dt_hours: f64, price_profile: Vec<f64>, grid_capacity_kw: f64) -> Self {
Self {
dt_hours,
price_profile,
grid_capacity_kw,
}
}
/// Total number of time slots in the price profile.
pub fn n_slots(&self) -> usize {
self.price_profile.len()
}
/// Convert a time \[hours\] to a slot index (clamped to valid range).
fn time_to_slot(&self, t_hours: f64) -> usize {
let idx = (t_hours / self.dt_hours).floor() as isize;
idx.max(0).min(self.n_slots() as isize - 1) as usize
}
/// Collect slots that fall inside [arrival, departure).
pub fn session_slots(&self, session: &EvSession) -> Vec<usize> {
let start = self.time_to_slot(session.arrival_time);
let end = self.time_to_slot(session.departure_time);
(start..end.min(self.n_slots())).collect()
}
/// Compute metrics (cost, revenue, degradation) from a power vector and SoC trajectory.
pub fn compute_metrics(
&self,
power_kw: &[f64],
slot_indices: &[usize],
degradation_cost_per_kwh: f64,
dt: f64,
) -> (f64, f64, f64) {
let mut energy_cost = 0.0_f64;
let mut v2g_rev = 0.0_f64;
let mut deg_cost = 0.0_f64;
for (k, &p) in power_kw.iter().enumerate() {
let slot_idx = slot_indices[k];
let price = self.price_profile.get(slot_idx).copied().unwrap_or(0.0);
if p > 0.0 {
energy_cost += price * p * dt;
} else if p < 0.0 {
v2g_rev += price * p.abs() * dt;
}
deg_cost += degradation_cost_per_kwh * p.abs() * dt;
}
(energy_cost, v2g_rev, deg_cost)
}
/// Simulate SoC forward given power schedule.
/// Returns SoC trajectory of length `n+1` (SoC at start of each slot + final).
pub fn simulate_soc(
&self,
power_kw: &[f64],
soc_init: f64,
battery_kwh: f64,
eta_charge: f64,
eta_discharge: f64,
) -> Vec<f64> {
let mut soc = soc_init;
let mut traj = Vec::with_capacity(power_kw.len() + 1);
traj.push(soc);
for &p in power_kw {
let delta_soc = if p > 0.0 {
// Charging: store η_c * P * dt / E_batt
eta_charge * p * self.dt_hours / battery_kwh
} else {
// Discharging: draw P * dt / (η_d * E_batt) from battery
p * self.dt_hours / (eta_discharge * battery_kwh)
};
soc = (soc + delta_soc).clamp(0.0, 1.0);
traj.push(soc);
}
traj
}
// ──────────────────────────────────────────────────────────────────────────
// Public charging strategies
// ──────────────────────────────────────────────────────────────────────────
/// **Uncontrolled (dumb) charging** — charge at maximum rate from arrival until target is met.
///
/// The vehicle charges at `min(max_charge_kw, grid_capacity_kw)` every slot
/// until `soc_target` is reached, then idles.
pub fn uncontrolled(&self, session: &EvSession) -> Result<ChargingSchedule, OxiGridError> {
if !session.is_feasible() {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: infeasible — need {:.2} h but window is {:.2} h",
session.vehicle_id,
session.min_charge_hours(),
session.window_hours()
)));
}
let slots = self.session_slots(session);
if slots.is_empty() {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: no time slots in charging window [{:.2}, {:.2})",
session.vehicle_id, session.arrival_time, session.departure_time
)));
}
let n = slots.len();
let p_max = session.max_charge_kw.min(self.grid_capacity_kw);
let mut power_kw = vec![0.0_f64; n];
let mut soc = session.soc_arrival;
#[allow(clippy::needless_range_loop)]
for k in 0..n {
if soc >= session.soc_target - 1e-9 {
break;
}
// Maximum energy we can add this slot
let soc_headroom = (session.soc_target - soc).max(0.0);
let e_max_soc = soc_headroom * session.battery_kwh / session.eta_charge;
let p_slot = p_max.min(e_max_soc / self.dt_hours);
power_kw[k] = p_slot;
soc += session.eta_charge * p_slot * self.dt_hours / session.battery_kwh;
soc = soc.clamp(0.0, 1.0);
}
let soc_traj = self.simulate_soc(
&power_kw,
session.soc_arrival,
session.battery_kwh,
session.eta_charge,
session.eta_discharge,
);
let time_slots: Vec<f64> = slots.iter().map(|&i| i as f64 * self.dt_hours).collect();
let (ec, vr, dc) =
self.compute_metrics(&power_kw, &slots, session.degradation_cost, self.dt_hours);
Ok(ChargingSchedule {
vehicle_id: session.vehicle_id,
time_slots,
power_kw,
soc_trajectory: soc_traj,
energy_cost: ec,
v2g_revenue: vr,
degradation_cost: dc,
net_cost: ec - vr + dc,
})
}
/// **TOU-optimized charging** — sort available slots by price, charge cheapest first.
///
/// Greedy assignment: fill slots with ascending electricity price until
/// `soc_target` is met. No V2G. Returns error if infeasible.
pub fn tou_optimized(&self, session: &EvSession) -> Result<ChargingSchedule, OxiGridError> {
if !session.is_feasible() {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: infeasible — need {:.2} h but window is {:.2} h",
session.vehicle_id,
session.min_charge_hours(),
session.window_hours()
)));
}
let slots = self.session_slots(session);
if slots.is_empty() {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: no slots in window",
session.vehicle_id
)));
}
let n = slots.len();
let p_max = session.max_charge_kw.min(self.grid_capacity_kw);
let dt = self.dt_hours;
// Sort slot positions by price (ascending) — break ties by earlier slot
let mut slot_order: Vec<usize> = (0..n).collect();
slot_order.sort_by(|&a, &b| {
let pa = self.price_profile.get(slots[a]).copied().unwrap_or(0.0);
let pb = self.price_profile.get(slots[b]).copied().unwrap_or(0.0);
pa.partial_cmp(&pb)
.unwrap_or(std::cmp::Ordering::Equal)
.then(a.cmp(&b))
});
let mut power_kw = vec![0.0_f64; n];
// Track cumulative energy stored (to know when target is reached)
let e_needed = session.energy_needed_kwh(); // grid-side kWh needed
let mut e_assigned = 0.0_f64;
for &k in &slot_order {
if e_assigned >= e_needed - 1e-9 {
break;
}
let e_remaining = e_needed - e_assigned;
let e_slot_max = p_max * dt; // max grid-side energy this slot
let e_this = e_slot_max.min(e_remaining);
power_kw[k] = e_this / dt;
e_assigned += e_this;
}
// Verify feasibility (energy-wise, should be guaranteed by is_feasible)
if e_assigned < e_needed - 1e-6 {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: TOU scheduling failed to assign enough energy",
session.vehicle_id
)));
}
let soc_traj = self.simulate_soc(
&power_kw,
session.soc_arrival,
session.battery_kwh,
session.eta_charge,
session.eta_discharge,
);
let time_slots: Vec<f64> = slots.iter().map(|&i| i as f64 * dt).collect();
let (ec, vr, dc) = self.compute_metrics(&power_kw, &slots, session.degradation_cost, dt);
Ok(ChargingSchedule {
vehicle_id: session.vehicle_id,
time_slots,
power_kw,
soc_trajectory: soc_traj,
energy_cost: ec,
v2g_revenue: vr,
degradation_cost: dc,
net_cost: ec - vr + dc,
})
}
/// **V2G bi-directional optimization** via analytic Dynamic Programming.
///
/// Minimises:
/// Σ_t [ price\[t\] * P\[t\] * dt + degradation * |P\[t\]| * dt ]
///
/// subject to SoC dynamics, box constraints on P\[t\] and SoC\[t\],
/// and the terminal constraint SoC\[T_dep\] ≥ soc_target.
///
/// # Algorithm
/// Single-vehicle 1-D DP: backward pass computes optimal cost-to-go from
/// each state; forward pass reconstructs optimal power at each slot.
/// SoC is discretised into `N_SOC = 200` bins for tractability.
pub fn v2g_optimized(&self, session: &EvSession) -> Result<ChargingSchedule, OxiGridError> {
if !session.is_feasible() {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: infeasible — need {:.2} h but window is {:.2} h",
session.vehicle_id,
session.min_charge_hours(),
session.window_hours()
)));
}
let slots = self.session_slots(session);
if slots.is_empty() {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: no slots in window",
session.vehicle_id
)));
}
let n = slots.len();
let dt = self.dt_hours;
let e_batt = session.battery_kwh;
let eta_c = session.eta_charge;
let eta_d = session.eta_discharge;
let p_chg = session.max_charge_kw.min(self.grid_capacity_kw);
let p_dis = session.max_discharge_kw.min(self.grid_capacity_kw);
let deg = session.degradation_cost;
// SoC discretization
const N_SOC: usize = 200;
let soc_min = 0.0_f64;
let soc_max = 1.0_f64;
let dsoc = (soc_max - soc_min) / (N_SOC - 1) as f64;
let soc_idx_of = |s: f64| -> usize {
let idx = ((s - soc_min) / dsoc).round() as isize;
idx.max(0).min(N_SOC as isize - 1) as usize
};
let soc_of = |i: usize| -> f64 { soc_min + i as f64 * dsoc };
// Terminal constraint: SoC at step n must be >= soc_target
let target_idx = soc_idx_of(session.soc_target);
// Cost-to-go: V[t][s] = minimum cost from slot t to end, given SoC = soc_of(s)
let inf = f64::INFINITY;
let mut v_next = vec![inf; N_SOC];
// Terminal cost: 0 if SoC >= target, else infinity
for (s, v) in v_next.iter_mut().enumerate() {
if s >= target_idx {
*v = 0.0;
}
}
// Discretized power actions: charge levels + discharge levels + idle
// Use 20 actions per direction for resolution
const N_ACT: usize = 41; // -20..=+20 relative to p_max
let actions: Vec<f64> = (0..N_ACT)
.map(|i| {
let frac = i as f64 / (N_ACT - 1) as f64; // 0..1
-p_dis + frac * (p_chg + p_dis) // -p_dis .. +p_chg
})
.collect();
// Backward DP
let mut v_table: Vec<Vec<f64>> = vec![vec![inf; N_SOC]; n + 1];
v_table[n] = v_next.clone();
for t_back in (0..n).rev() {
let slot_idx = slots[t_back];
let price = self.price_profile.get(slot_idx).copied().unwrap_or(0.0);
let mut v_cur = vec![inf; N_SOC];
for (s_cur, v_slot) in v_cur.iter_mut().enumerate() {
let soc_cur = soc_of(s_cur);
let mut best = inf;
for &p in &actions {
// SoC transition
let delta_soc = if p >= 0.0 {
eta_c * p * dt / e_batt
} else {
p * dt / (eta_d * e_batt) // negative
};
let soc_next = soc_cur + delta_soc;
if soc_next < soc_min - 1e-9 || soc_next > soc_max + 1e-9 {
continue; // infeasible transition
}
let s_next = soc_idx_of(soc_next.clamp(soc_min, soc_max));
let v_fut = v_table[t_back + 1][s_next];
if v_fut >= inf {
continue;
}
// Immediate cost: grid cost + degradation
let grid_cost = if p >= 0.0 {
price * p * dt
} else {
-price * p.abs() * dt // revenue (negative cost)
};
let deg_cost = deg * p.abs() * dt;
let total_cost = grid_cost + deg_cost + v_fut;
if total_cost < best {
best = total_cost;
}
}
*v_slot = best;
}
v_table[t_back] = v_cur;
}
// Forward pass: reconstruct optimal actions
let mut power_kw = vec![0.0_f64; n];
let mut soc_cur = session.soc_arrival;
for t in 0..n {
let slot_idx = slots[t];
let price = self.price_profile.get(slot_idx).copied().unwrap_or(0.0);
let _s_cur = soc_idx_of(soc_cur);
let mut best_cost = inf;
let mut best_p = 0.0_f64;
for &p in &actions {
let delta_soc = if p >= 0.0 {
eta_c * p * dt / e_batt
} else {
p * dt / (eta_d * e_batt)
};
let soc_next = soc_cur + delta_soc;
if soc_next < soc_min - 1e-9 || soc_next > soc_max + 1e-9 {
continue;
}
let s_next = soc_idx_of(soc_next.clamp(soc_min, soc_max));
let v_fut = v_table[t + 1][s_next];
if v_fut >= inf {
continue;
}
let grid_cost = if p >= 0.0 {
price * p * dt
} else {
-price * p.abs() * dt
};
let deg_cost = deg * p.abs() * dt;
let total = grid_cost + deg_cost + v_fut;
if total < best_cost {
best_cost = total;
best_p = p;
}
}
// If still infeasible at current state, fall back to max charge
if best_cost >= inf {
best_p = p_chg;
}
power_kw[t] = best_p;
let delta = if best_p >= 0.0 {
eta_c * best_p * dt / e_batt
} else {
best_p * dt / (eta_d * e_batt)
};
soc_cur = (soc_cur + delta).clamp(0.0, 1.0);
}
let soc_traj = self.simulate_soc(
&power_kw,
session.soc_arrival,
session.battery_kwh,
eta_c,
eta_d,
);
let time_slots: Vec<f64> = slots.iter().map(|&i| i as f64 * dt).collect();
let (ec, vr, dc) = self.compute_metrics(&power_kw, &slots, session.degradation_cost, dt);
Ok(ChargingSchedule {
vehicle_id: session.vehicle_id,
time_slots,
power_kw,
soc_trajectory: soc_traj,
energy_cost: ec,
v2g_revenue: vr,
degradation_cost: dc,
net_cost: ec - vr + dc,
})
}
/// **Frequency regulation** — modulate charging around a base schedule
/// following an Automatic Generation Control (AGC) signal.
///
/// The AGC signal is normalised to [−1, 1]; ±1 means full power adjustment
/// from the base schedule. SoC is kept within [soc_min=0.2, soc_max=0.95].
///
/// # Arguments
/// - `session` — the EV session parameters
/// - `regulation_signal` — AGC signal per slot (normalised, len = n_slots in window)
/// - `base_schedule` — baseline schedule (typically from `tou_optimized`)
pub fn frequency_regulation(
&self,
session: &EvSession,
regulation_signal: &[f64],
base_schedule: &ChargingSchedule,
) -> Result<ChargingSchedule, OxiGridError> {
let slots = self.session_slots(session);
let n = slots.len();
if base_schedule.power_kw.len() != n {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: base schedule length {} != window slots {}",
session.vehicle_id,
base_schedule.power_kw.len(),
n
)));
}
if regulation_signal.len() < n {
return Err(OxiGridError::InvalidParameter(format!(
"EV {}: regulation_signal length {} < slots {}",
session.vehicle_id,
regulation_signal.len(),
n
)));
}
let soc_reg_min = 0.2_f64;
let soc_reg_max = 0.95_f64;
let dt = self.dt_hours;
let e_batt = session.battery_kwh;
let eta_c = session.eta_charge;
let eta_d = session.eta_discharge;
let p_chg = session.max_charge_kw.min(self.grid_capacity_kw);
let p_dis = session.max_discharge_kw.min(self.grid_capacity_kw);
let mut power_kw = Vec::with_capacity(n);
let mut soc = session.soc_arrival;
for (base_p, &sig) in base_schedule
.power_kw
.iter()
.zip(regulation_signal.iter())
.take(n)
{
let base_p = *base_p;
let signal = sig.clamp(-1.0, 1.0);
// Modulation band: signal > 0 → increase charging; signal < 0 → decrease/discharge
let adjust = if signal >= 0.0 {
signal * (p_chg - base_p).max(0.0)
} else {
signal * (base_p + p_dis).max(0.0) // reduce / go negative
};
let p_candidate = (base_p + adjust).clamp(-p_dis, p_chg);
// Enforce SoC constraints: if SoC too low, prevent further discharge;
// if SoC too high, prevent further charge
let p_actual = if (soc <= soc_reg_min + 1e-9 && p_candidate < 0.0)
|| (soc >= soc_reg_max - 1e-9 && p_candidate > 0.0)
{
0.0
} else {
p_candidate
};
power_kw.push(p_actual);
let delta = if p_actual >= 0.0 {
eta_c * p_actual * dt / e_batt
} else {
p_actual * dt / (eta_d * e_batt)
};
soc = (soc + delta).clamp(0.0, 1.0);
}
// Check that we still end near soc_target (best-effort)
let soc_traj = self.simulate_soc(&power_kw, session.soc_arrival, e_batt, eta_c, eta_d);
let time_slots: Vec<f64> = slots.iter().map(|&i| i as f64 * dt).collect();
let (ec, vr, dc) = self.compute_metrics(&power_kw, &slots, session.degradation_cost, dt);
Ok(ChargingSchedule {
vehicle_id: session.vehicle_id,
time_slots,
power_kw,
soc_trajectory: soc_traj,
energy_cost: ec,
v2g_revenue: vr,
degradation_cost: dc,
net_cost: ec - vr + dc,
})
}
}
// ──────────────────────────────────────────────────────────────────────────────
// Unit tests
// ──────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
fn make_charger() -> SmartCharger {
// 15-min slots, 96 per day
// Cheap at night (slots 0-31 = 00:00-08:00 and 76-95 = 19:00-24:00)
// Expensive during day (slots 32-75)
let n_slots = 96usize;
let prices: Vec<f64> = (0..n_slots)
.map(|i| {
if !(32..76).contains(&i) {
0.05_f64 // cheap night
} else {
0.25_f64 // expensive day
}
})
.collect();
SmartCharger::new(0.25, prices, 22.0)
}
fn make_session(arrival: f64, departure: f64) -> EvSession {
EvSession {
vehicle_id: 1,
arrival_time: arrival,
departure_time: departure,
soc_arrival: 0.3,
soc_target: 0.8,
battery_kwh: 60.0,
max_charge_kw: 11.0,
max_discharge_kw: 7.4,
eta_charge: 0.92,
eta_discharge: 0.92,
degradation_cost: 0.05,
}
}
#[test]
fn test_ev_session_energy_needed() {
let session = EvSession {
soc_arrival: 0.3,
soc_target: 0.8,
battery_kwh: 60.0,
eta_charge: 1.0, // perfect efficiency for simple math
..Default::default()
};
// (0.8 - 0.3) * 60.0 / 1.0 = 30 kWh
assert!((session.energy_needed_kwh() - 30.0).abs() < 0.01);
}
#[test]
fn test_uncontrolled_charging_meets_target() {
let charger = make_charger();
let session = make_session(18.0, 31.0); // 18:00 → next morning 07:00 (31h window)
let sched = charger
.uncontrolled(&session)
.expect("uncontrolled should succeed");
let final_soc = *sched.soc_trajectory.last().expect("trajectory non-empty");
assert!(
final_soc >= session.soc_target - 1e-3,
"Final SoC {:.4} < target {:.4}",
final_soc,
session.soc_target
);
}
#[test]
fn test_tou_charges_during_cheap_hours() {
let charger = make_charger();
// Arrive at 08:00 (slot 32, start of expensive), depart 31h later
let session = make_session(8.0, 31.0);
let sched = charger.tou_optimized(&session).expect("tou should succeed");
// Count energy assigned to cheap slots (price=0.05) vs expensive (price=0.25)
let dt = 0.25_f64;
let mut cheap_kwh = 0.0_f64;
let mut exp_kwh = 0.0_f64;
for (k, &p) in sched.power_kw.iter().enumerate() {
if p <= 0.0 {
continue;
}
let t = sched.time_slots[k];
let slot = (t / dt).round() as usize;
if !(32..76).contains(&slot) {
cheap_kwh += p * dt;
} else {
exp_kwh += p * dt;
}
}
assert!(
cheap_kwh > exp_kwh,
"TOU should prefer cheap slots: cheap={:.2} exp={:.2}",
cheap_kwh,
exp_kwh
);
}
#[test]
fn test_soc_trajectory_bounded() {
let charger = make_charger();
let session = make_session(18.0, 31.0);
let sched = charger.v2g_optimized(&session).expect("v2g should succeed");
for &s in &sched.soc_trajectory {
assert!(
(-1e-6..=1.0 + 1e-6).contains(&s),
"SoC out of bounds: {:.4}",
s
);
}
}
#[test]
fn test_charging_window_feasibility() {
let charger = make_charger();
// 5 min window (0.083h), need >> that to charge from 0.3→0.8 on 60kWh bat
let session = make_session(8.0, 8.083);
let result = charger.uncontrolled(&session);
assert!(result.is_err(), "Should return error for infeasible window");
}
#[test]
fn test_frequency_regulation_soc_bounded() {
let charger = make_charger();
let session = make_session(18.0, 31.0);
let base = charger.tou_optimized(&session).expect("tou base");
let n = base.power_kw.len();
// Aggressive regulation signal: alternating +1 and -1
let signal: Vec<f64> = (0..n)
.map(|i| if i % 2 == 0 { 1.0 } else { -1.0 })
.collect();
let sched = charger
.frequency_regulation(&session, &signal, &base)
.expect("freq reg");
for &s in &sched.soc_trajectory {
assert!((-1e-6..=1.0 + 1e-6).contains(&s), "SoC OOB: {:.4}", s);
}
}
// ── new tests ─────────────────────────────────────────────────────────────
#[test]
fn test_ev_session_window_hours() {
let session = make_session(18.0, 31.0);
// 31.0 - 18.0 = 13.0 hours
assert!((session.window_hours() - 13.0).abs() < 1e-10);
}
#[test]
fn test_ev_session_v2g_headroom_no_excess() {
// When soc_arrival < soc_target there is no headroom above target
let session = EvSession {
soc_arrival: 0.3,
soc_target: 0.8,
battery_kwh: 60.0,
..Default::default()
};
// soc_upper = max(0.3, 0.8) = 0.8 = soc_target → headroom = 0
assert!(session.v2g_headroom_kwh().abs() < 1e-10);
}
#[test]
fn test_ev_session_v2g_headroom_with_excess() {
// When soc_arrival > soc_target there is headroom
let session = EvSession {
soc_arrival: 0.95,
soc_target: 0.6,
battery_kwh: 100.0,
..Default::default()
};
// soc_upper = max(0.95, 0.6) = 0.95; headroom = (0.95 - 0.6)*100 = 35 kWh
let expected = 35.0_f64;
assert!(
(session.v2g_headroom_kwh() - expected).abs() < 1e-9,
"expected {expected} kWh headroom, got {}",
session.v2g_headroom_kwh()
);
}
#[test]
fn test_ev_session_min_charge_hours_zero_power() {
// A vehicle with zero max charge rate should return INFINITY
let session = EvSession {
max_charge_kw: 0.0,
..Default::default()
};
assert!(
session.min_charge_hours().is_infinite(),
"expected INFINITY for zero max_charge_kw"
);
}
#[test]
fn test_smart_charger_n_slots_and_simulate_soc() {
let charger = make_charger(); // 96 slots
assert_eq!(charger.n_slots(), 96);
// Simulate charging: 11 kW for 1 slot (0.25 h) into a 60 kWh battery at η=0.92
let power = vec![11.0_f64];
let soc_traj = charger.simulate_soc(&power, 0.3, 60.0, 0.92, 0.92);
// delta_soc = 0.92 * 11.0 * 0.25 / 60.0 ≈ 0.04217
let expected_soc = 0.3 + 0.92 * 11.0 * 0.25 / 60.0;
assert_eq!(soc_traj.len(), 2, "trajectory must have n+1 entries");
assert!(
(soc_traj[1] - expected_soc).abs() < 1e-10,
"SoC after charging: expected {expected_soc:.6}, got {:.6}",
soc_traj[1]
);
// Simulate discharging: negative power lowers SoC
let power_dis = vec![-7.4_f64];
let soc_dis = charger.simulate_soc(&power_dis, 0.9, 60.0, 0.92, 0.92);
assert!(soc_dis[1] < 0.9, "discharge must reduce SoC");
}
#[test]
fn test_compute_metrics_charge_discharge() {
let charger = make_charger();
// Use slot 0 (cheap, 0.05 $/kWh) and slot 40 (expensive, 0.25 $/kWh)
// power_kw: [+11.0 (charge), -7.4 (discharge)]
let power_kw = vec![11.0_f64, -7.4_f64];
let slot_indices = vec![0usize, 40usize];
let deg_cost_per_kwh = 0.05_f64;
let dt = 0.25_f64;
let (ec, vr, dc) = charger.compute_metrics(&power_kw, &slot_indices, deg_cost_per_kwh, dt);
// energy cost: 0.05 * 11.0 * 0.25 = 0.1375
let expected_ec = 0.05 * 11.0 * 0.25;
// v2g revenue: 0.25 * 7.4 * 0.25 = 0.4625
let expected_vr = 0.25 * 7.4 * 0.25;
// degradation: 0.05 * (11.0 + 7.4) * 0.25 = 0.23
let expected_dc = 0.05 * (11.0 + 7.4) * 0.25;
assert!(
(ec - expected_ec).abs() < 1e-10,
"energy_cost mismatch: {ec}"
);
assert!(
(vr - expected_vr).abs() < 1e-10,
"v2g_revenue mismatch: {vr}"
);
assert!((dc - expected_dc).abs() < 1e-10, "deg_cost mismatch: {dc}");
}
#[test]
fn test_frequency_regulation_error_on_mismatched_base() {
let charger = make_charger();
let session = make_session(18.0, 31.0);
// Build a base schedule with the wrong number of slots
let bad_base = ChargingSchedule {
vehicle_id: 1,
time_slots: vec![18.0],
power_kw: vec![5.0], // length 1, but window has many slots
soc_trajectory: vec![0.3, 0.35],
energy_cost: 0.0,
v2g_revenue: 0.0,
degradation_cost: 0.0,
net_cost: 0.0,
};
let n_slots = charger.session_slots(&session).len();
let signal = vec![0.0_f64; n_slots];
let result = charger.frequency_regulation(&session, &signal, &bad_base);
assert!(
result.is_err(),
"frequency_regulation should error when base schedule length mismatches"
);
}
#[test]
fn test_v2g_optimized_net_cost_structure() {
let charger = make_charger();
let session = make_session(18.0, 31.0);
let sched = charger
.v2g_optimized(&session)
.expect("v2g_optimized should succeed");
// net_cost must equal energy_cost - v2g_revenue + degradation_cost
let expected_net = sched.energy_cost - sched.v2g_revenue + sched.degradation_cost;
assert!(
(sched.net_cost - expected_net).abs() < 1e-10,
"net_cost {:.6} != energy_cost - v2g_revenue + deg {:.6}",
sched.net_cost,
expected_net
);
// time_slots and power_kw must have the same length
assert_eq!(
sched.time_slots.len(),
sched.power_kw.len(),
"time_slots and power_kw length mismatch"
);
// soc_trajectory must be power_kw.len() + 1
assert_eq!(
sched.soc_trajectory.len(),
sched.power_kw.len() + 1,
"soc_trajectory must be power_kw.len() + 1"
);
}
}