fastsim_core/
simdrive.rs

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
//! Module containing vehicle struct and related functions.
// crate local
use crate::cycle::{RustCycle, RustCycleCache};
use crate::imports::*;
use crate::params::RustPhysicalProperties;
use crate::proc_macros::add_pyo3_api;
#[cfg(feature = "pyo3")]
use crate::pyo3imports::*;
use crate::vehicle::*;
pub mod cyc_mods;
pub mod simdrive_impl;
pub mod simdrive_iter;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[add_pyo3_api(
    pub fn __getnewargs__(&self) {
        todo!();
    }
)]
/// Struct containing time trace data
pub struct RustSimDriveParams {
    /// if true, accuracy will be favored over performance for grade per step estimates
    /// Specifically, for performance, grade for a step will be assumed to be the grade
    /// looked up at step start distance. For accuracy, the actual elevations will be
    /// used. This distinciton only makes a difference for CAV maneuvers.
    pub favor_grade_accuracy: bool,
    /// if true, missed trace correction is active, default = False.  If missed
    /// trace correction is active, time step will be "dilated" to be long enough for
    /// vehicle to "catch up" with trace.
    pub missed_trace_correction: bool,
    /// maximum time dilation factor to "catch up" with trace -- e.g. 1.0 means 100% increase in step size
    pub max_time_dilation: f64,
    /// minimum time dilation margin to let trace "catch up" -- e.g. -0.5 means 50% reduction in step size
    pub min_time_dilation: f64,
    /// convergence criteria for time dilation in iterating on time step size to achieve distance parity
    pub time_dilation_tol: f64,
    /// number of iterations to achieve time dilation correction
    pub max_trace_miss_iters: u32,
    /// threshold for triggering warning log message if vehicle speed deficit [m/s]
    /// relative to prescribed speed exceeds this amount
    pub trace_miss_speed_mps_tol: f64,
    /// threshold for triggering warning log message if achieved elapsed time
    /// relative to prescribed elapsed time exceeds this fractional amount
    pub trace_miss_time_tol: f64,
    /// threshold for triggering warning log message if achieved distance
    /// relative to prescribed distance exceeds this fractional amount
    pub trace_miss_dist_tol: f64,
    /// max allowable number of HEV SOC iterations
    pub sim_count_max: usize,
    /// newton solver gain
    pub newton_gain: f64,
    /// newton solver max iterations
    pub newton_max_iter: u32,
    /// newton solver tolerance
    pub newton_xtol: f64,
    /// tolerance for energy audit error warning, i.e. 0.1%
    pub energy_audit_error_tol: f64,
    // Eco-Coasting Maneuver Parameters
    /// if true, coasting to stops are allowed
    pub coast_allow: bool,
    /// if true, coasting vehicle can eclipse the shadow trace (i.e., reference vehicle in front)
    pub coast_allow_passing: bool,
    /// maximum allowable speed under coast (m/s)
    pub coast_max_speed_m_per_s: f64,
    /// acceleration assumed during braking for coast maneuvers (m/s2). note: should be negative
    pub coast_brake_accel_m_per_s2: f64,
    /// speed when friction braking will initiate during coasting maneuvers (m/s)
    pub coast_brake_start_speed_m_per_s: f64,
    /// initiates coast when vehicle hits this speed if > 0; this is mainly for forceing coasting to initiate for testing. (m/s)
    pub coast_start_speed_m_per_s: f64,
    /// "look-ahead" time for speed changes to be considered to feature coasting to hit a given stopping distance mark (s)
    pub coast_time_horizon_for_adjustment_s: f64,
    // IDM - Intelligent Driver Model, Adaptive Cruise Control version
    /// if true, initiates the IDM - Intelligent Driver Model, Adaptive Cruise Control version
    pub idm_allow: bool,
    /// IDM algorithm: desired speed (m/s)
    pub idm_v_desired_m_per_s: f64,
    /// IDM algorithm: headway time desired to vehicle in front (s)
    pub idm_dt_headway_s: f64,
    /// IDM algorithm: minimum desired gap between vehicle and lead vehicle (m)
    pub idm_minimum_gap_m: f64,
    /// IDM algorithm: delta parameter
    pub idm_delta: f64,
    /// IDM algorithm: acceleration parameter
    pub idm_accel_m_per_s2: f64,
    /// IDM algorithm: deceleration parameter
    pub idm_decel_m_per_s2: f64,
    /// IDM algorithm: a way to specify desired speed by course distance
    /// traveled. Can simulate changing speed limits over a driving cycle
    /// optional list of (distance (m), desired speed (m/s))
    pub idm_v_desired_in_m_per_s_by_distance_m: Option<Vec<(f64, f64)>>,
    // Other, Misc.
    /// EPA fuel economy adjustment parameters; maximum EPA adjustment factor
    pub max_epa_adj: f64,
    #[serde(skip)]
    pub orphaned: bool,
}

impl SerdeAPI for RustSimDriveParams {}

impl Default for RustSimDriveParams {
    fn default() -> Self {
        // if True, accuracy will be favored over performance for grade per step estimates
        // Specifically, for performance, grade for a step will be assumed to be the grade
        // looked up at step start distance. For accuracy, the actual elevations will be
        // used. This distinciton only makes a difference for CAV maneuvers.
        let favor_grade_accuracy = true;
        // if true, missed trace correction is active, default = false
        let missed_trace_correction = false;
        // maximum time dilation factor to "catch up" with trace -- e.g. 1.0 means 100% increase in step size
        let max_time_dilation = 1.0;
        // minimum time dilation margin to let trace "catch up" -- e.g. -0.5 means 50% reduction in step size
        let min_time_dilation = -0.5;
        let time_dilation_tol = 5e-4; // convergence criteria for time dilation
        let max_trace_miss_iters = 5; // number of iterations to achieve time dilation correction
        let trace_miss_speed_mps_tol = 1.0; // # threshold of error in speed [m/s] that triggers warning
        let trace_miss_time_tol = 1e-3; // threshold for printing warning when time dilation is active
        let trace_miss_dist_tol = 1e-3; // threshold of fractional eror in distance that triggers warning
        let sim_count_max = 30; // max allowable number of HEV SOC iterations
        let newton_gain = 0.9; // newton solver gain
        let newton_max_iter = 100; // newton solver max iterations
        let newton_xtol = 1e-9; // newton solver tolerance
        let energy_audit_error_tol = 0.002; // tolerance for energy audit error warning, i.e. 0.1%
                                            // Coasting
        let coast_allow = false;
        let coast_allow_passing = false;
        let coast_max_speed_m_per_s = 40.0;
        let coast_brake_accel_m_per_s2 = -2.5;
        let coast_brake_start_speed_m_per_s = 7.5;
        let coast_start_speed_m_per_s = 0.0; // m/s, if > 0, initiates coast when vehicle hits this speed; mostly for testing
        let coast_time_horizon_for_adjustment_s = 20.0;
        // Following
        let idm_allow = false;
        // IDM - Intelligent Driver Model, Adaptive Cruise Control version
        let idm_v_desired_m_per_s = 33.33;
        let idm_dt_headway_s = 1.0;
        let idm_minimum_gap_m = 2.0;
        let idm_delta = 4.0;
        let idm_accel_m_per_s2 = 1.0;
        let idm_decel_m_per_s2 = 1.5;
        let idm_v_desired_in_m_per_s_by_distance_m = None;
        // EPA fuel economy adjustment parameters
        let max_epa_adj = 0.3; // maximum EPA adjustment factor
        Self {
            favor_grade_accuracy,
            missed_trace_correction,
            max_time_dilation,
            min_time_dilation,
            time_dilation_tol,
            max_trace_miss_iters,
            trace_miss_speed_mps_tol,
            trace_miss_time_tol,
            trace_miss_dist_tol,
            sim_count_max,
            newton_gain,
            newton_max_iter,
            newton_xtol,
            energy_audit_error_tol,
            coast_allow,
            coast_allow_passing,
            coast_max_speed_m_per_s,
            coast_brake_accel_m_per_s2,
            coast_brake_start_speed_m_per_s,
            coast_start_speed_m_per_s,
            coast_time_horizon_for_adjustment_s,
            idm_allow,
            idm_v_desired_m_per_s,
            idm_dt_headway_s,
            idm_minimum_gap_m,
            idm_delta,
            idm_accel_m_per_s2,
            idm_decel_m_per_s2,
            idm_v_desired_in_m_per_s_by_distance_m,
            max_epa_adj,
            orphaned: false,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[add_pyo3_api(
    /// method for instantiating SimDriveRust
    #[new]
    pub fn __new__(cyc: RustCycle, veh: RustVehicle) -> Self {
        Self::new(cyc, veh)
    }

    pub fn __getnewargs__(&self) {
        todo!();
    }

    // wrappers for core methods

    #[pyo3(name = "gap_to_lead_vehicle_m")]
    /// Provides the gap-with lead vehicle from start to finish
    pub fn gap_to_lead_vehicle_m_py(&self) -> anyhow::Result<Vec<f64>> {
        Ok(self.gap_to_lead_vehicle_m().to_vec())
    }

    #[pyo3(name = "sim_drive")]
    #[pyo3(signature = (init_soc=None, aux_in_kw_override=None))]
    /// Initialize and run sim_drive_walk as appropriate for vehicle attribute vehPtType.
    /// Arguments
    /// ------------
    /// init_soc: initial SOC for electrified vehicles.
    /// aux_in_kw: aux_in_kw override.  Array of same length as cyc.time_s.
    ///     Default of None causes veh.aux_kw to be used.
    pub fn sim_drive_py(
        &mut self,
        init_soc: Option<f64>,
        aux_in_kw_override: Option<Vec<f64>>,
    ) -> anyhow::Result<()> {
        let aux_in_kw_override = aux_in_kw_override.map(Array1::from);
        self.sim_drive(init_soc, aux_in_kw_override)
    }

    #[pyo3(signature = (init_soc, aux_in_kw_override=None))]
    /// Receives second-by-second cycle information, vehicle properties,
    /// and an initial state of charge and runs sim_drive_step to perform a
    /// backward facing powertrain simulation. Method 'sim_drive' runs this
    /// iteratively to achieve correct SOC initial and final conditions, as
    /// needed.
    ///
    /// Arguments
    /// ------------
    /// init_soc (optional): initial battery state-of-charge (SOC) for electrified vehicles
    /// aux_in_kw: aux_in_kw override.  Array of same length as cyc.time_s.
    ///         None causes veh.aux_kw to be used.
    pub fn sim_drive_walk(
        &mut self,
        init_soc: f64,
        aux_in_kw_override: Option<Vec<f64>>,
    ) -> anyhow::Result<()> {
        let aux_in_kw_override = aux_in_kw_override.map(Array1::from);
        self.walk(init_soc, aux_in_kw_override)
    }

    #[pyo3(signature = (by_microtrip=None, extend_fraction=None, blend_factor=None, min_target_speed_m_per_s=None))]
    /// Sets the intelligent driver model parameters for an eco-cruise driving trajectory.
    /// This is a convenience method instead of setting the sim_params.idm* parameters yourself.
    /// - by_microtrip: bool, if True, target speed is set by microtrip, else by cycle
    /// - extend_fraction: float, the fraction of time to extend the cycle to allow for catch-up
    ///     of the following vehicle
    /// - blend_factor: float, a value between 0 and 1; only used of by_microtrip is True, blends
    ///     between microtrip average speed and microtrip average speed when moving. Must be
    ///     between 0 and 1 inclusive
    pub fn activate_eco_cruise(
        &mut self,
        by_microtrip: Option<bool>,
        extend_fraction: Option<f64>,
        blend_factor: Option<f64>,
        min_target_speed_m_per_s: Option<f64>,
    ) -> anyhow::Result<()> {
        let by_microtrip = by_microtrip.unwrap_or(false);
        let extend_fraction = extend_fraction.unwrap_or(0.1);
        let blend_factor = blend_factor.unwrap_or(0.0);
        let min_target_speed_m_per_s = min_target_speed_m_per_s.unwrap_or(8.0);
            self.activate_eco_cruise_rust(
                by_microtrip, extend_fraction, blend_factor, min_target_speed_m_per_s)
    }

    #[pyo3(name = "init_for_step")]
    #[pyo3(signature = (init_soc, aux_in_kw_override=None))]
    /// This is a specialty method which should be called prior to using
    /// sim_drive_step in a loop.
    /// Arguments
    /// ------------
    /// init_soc: initial battery state-of-charge (SOC) for electrified vehicles
    /// aux_in_kw: aux_in_kw override.  Array of same length as cyc.time_s.
    ///         Default of None causes veh.aux_kw to be used.
    pub fn init_for_step_py(
        &mut self,
        init_soc:f64,
        aux_in_kw_override: Option<Vec<f64>>
    ) -> anyhow::Result<()> {
        let aux_in_kw_override = aux_in_kw_override.map(Array1::from);
        self.init_for_step(init_soc, aux_in_kw_override)
    }

    /// Step through 1 time step.
    pub fn sim_drive_step(&mut self) -> anyhow::Result<()> {
        self.step()
    }

    #[pyo3(name = "solve_step")]
    /// Perform all the calculations to solve 1 time step.
    pub fn solve_step_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.solve_step(i)
    }

    #[pyo3(name = "set_misc_calcs")]
    /// Sets misc. calculations at time step 'i'
    /// Arguments:
    /// ----------
    /// i: index of time step
    pub fn set_misc_calcs_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_misc_calcs(i)
    }

    #[pyo3(name = "set_comp_lims")]
    // Calculate actual speed achieved if vehicle hardware cannot achieve trace speed.
    // Arguments
    // ------------
    // i: index of time step
    pub fn set_comp_lims_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_comp_lims(i)
    }

    #[pyo3(name = "set_power_calcs")]
    /// Calculate power requirements to meet cycle and determine if
    /// cycle can be met.
    /// Arguments
    /// ------------
    /// i: index of time step
    pub fn set_power_calcs_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_power_calcs(i)
    }

    #[pyo3(name = "set_ach_speed")]
    // Calculate actual speed achieved if vehicle hardware cannot achieve trace speed.
    // Arguments
    // ------------
    // i: index of time step
    pub fn set_ach_speed_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_ach_speed(i)
    }

    #[pyo3(name = "set_hybrid_cont_calcs")]
    /// Hybrid control calculations.
    /// Arguments
    /// ------------
    /// i: index of time step
    pub fn set_hybrid_cont_calcs_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_hybrid_cont_calcs(i)
    }

    #[pyo3(name = "set_fc_forced_state")]
    /// Calculate control variables related to engine on/off state
    /// Arguments
    /// ------------
    /// i: index of time step
    /// `_py` extension is needed to avoid name collision with getter/setter methods
    pub fn set_fc_forced_state_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_fc_forced_state_rust(i)
    }

    #[pyo3(name = "set_hybrid_cont_decisions")]
    /// Hybrid control decisions.
    /// Arguments
    /// ------------
    /// i: index of time step
    pub fn set_hybrid_cont_decisions_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_hybrid_cont_decisions(i)
    }

    #[pyo3(name = "set_fc_power")]
    /// Sets power consumption values for the current time step.
    /// Arguments
    /// ------------
    /// i: index of time step
    pub fn set_fc_power_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_fc_power(i)
    }

    #[pyo3(name = "set_time_dilation")]
    /// Sets the time dilation for the current step.
    /// Arguments
    /// ------------
    /// i: index of time step
    pub fn set_time_dilation_py(&mut self, i: usize) -> anyhow::Result<()> {
        self.set_time_dilation(i)
    }

    #[pyo3(name = "set_post_scalars")]
    /// Sets scalar variables that can be calculated after a cycle is run.
    /// This includes mpgge, various energy metrics, and others
    pub fn set_post_scalars_py(&mut self) -> anyhow::Result<()> {
        self.set_post_scalars()
    }

    #[pyo3(name = "len")]
    pub fn len_py(&self) -> usize {
        self.len()
    }

    #[pyo3(name = "is_empty")]
    pub fn is_empty_py(&self) -> bool {
        self.is_empty()
    }

    #[getter]
    pub fn get_fs_cumu_mj_out_ach(&self) -> Pyo3ArrayF64 {
        Pyo3ArrayF64::new(ndarrcumsum(&(&self.fs_kw_out_ach * self.cyc.dt_s() * 1e-3)))
    }
    #[getter]
    pub fn get_fc_cumu_mj_out_ach(&self) -> Pyo3ArrayF64 {
        Pyo3ArrayF64::new(ndarrcumsum(&(&self.fc_kw_out_ach * self.cyc.dt_s() * 1e-3)))
    }
)]
pub struct RustSimDrive {
    pub hev_sim_count: usize,
    #[api(has_orphaned)]
    pub veh: RustVehicle,
    #[api(has_orphaned)]
    pub cyc: RustCycle,
    #[api(has_orphaned)]
    pub cyc0: RustCycle,
    #[api(has_orphaned)]
    pub sim_params: RustSimDriveParams,
    #[serde(skip)]
    #[api(has_orphaned)]
    pub props: RustPhysicalProperties,
    pub i: usize, // 1 # initialize step counter for possible use outside sim_drive_walk()
    /// Current maximum fuel storage output power,
    /// considering `veh.fs_max_kw` and transient limit,
    /// as determined by achieved fuel storage power output and `veh.fs_secs_to_peak_pwr`
    pub cur_max_fs_kw_out: Array1<f64>,
    /// Transient fuel converter output power limit,
    /// as determined by achieved fuel converter power output, `veh.fc_max_kw`, and `veh.fs_secs_to_peak_pwr`
    pub fc_trans_lim_kw: Array1<f64>,
    /// Current maximum fuel converter output power,
    /// considering `veh.fc_max_kw` and transient limit `fc_trans_lim_kw`
    pub cur_max_fc_kw_out: Array1<f64>,
    /// ESS discharging power limit,
    /// considering remaining ESS energy and ESS efficiency
    pub ess_cap_lim_dischg_kw: Array1<f64>,
    /// Current maximum ESS output power,
    /// considering `ess_cap_lim_dischg_kw` and `veh.ess_max_kw`
    pub cur_ess_max_kw_out: Array1<f64>,
    /// Current maximum electrical power that can go toward propulsion,
    /// `cur_max_elec_kw` limited by the maximum theoretical motor input power `veh.mc_max_elec_in_kw`
    pub cur_max_avail_elec_kw: Array1<f64>,
    /// ESS charging power limit,
    /// considering unused energy capacity and ESS efficiency
    pub ess_cap_lim_chg_kw: Array1<f64>,
    /// ESS charging power limit,
    /// considering `ess_cap_lim_chg_kw` and `veh.ess_max_kw`
    pub cur_max_ess_chg_kw: Array1<f64>,
    /// Current maximum electrical power that can go toward propulsion:
    /// if FCEV, equal to `cur_max_fc_kw_out` + `cur_max_roadway_chg_kw` + `cur_ess_max_kw_out` - `aux_in_kw`,
    /// otherwise equal to `cur_max_roadway_chg_kw` + `cur_ess_max_kw_out` - `aux_in_kw`
    pub cur_max_elec_kw: Array1<f64>,
    pub mc_elec_in_lim_kw: Array1<f64>,
    /// Transient electric motor output power limit,
    /// as determined by achieved motor mechanical power output, `veh.mc_max_kw`, and `veh.ms_secs_to_peak_pwr`
    pub mc_transi_lim_kw: Array1<f64>,
    pub cur_max_mc_kw_out: Array1<f64>,
    pub ess_lim_mc_regen_perc_kw: Array1<f64>,
    /// ESS limit on electricity regeneration,
    /// considering `veh.mc_max_kw`, or `cur_max_ess_chg_kw` and motor efficiency
    pub cur_max_mech_mc_kw_in: Array1<f64>,
    pub cur_max_trans_kw_out: Array1<f64>,
    /// Required tractive power to meet cycle,
    /// equal to `drag_kw` + `accel_kw` + `ascent_kw`
    pub cyc_trac_kw_req: Array1<f64>,
    pub cur_max_trac_kw: Array1<f64>,
    pub spare_trac_kw: Array1<f64>,
    pub cyc_whl_rad_per_sec: Array1<f64>,
    /// Power to change wheel rotational speed,
    /// calculated with `veh.wheel_inertia_kg_m2` and `veh.num_wheels`
    pub cyc_tire_inertia_kw: Array1<f64>,
    /// Required power to wheels to meet cycle,
    /// equal to `cyc_trac_kw_req` + `rr_kw` + `cyc_tire_inertia_kw`
    pub cyc_whl_kw_req: Array1<f64>,
    pub regen_contrl_lim_kw_perc: Array1<f64>,
    pub cyc_regen_brake_kw: Array1<f64>,
    /// Power lost to friction braking,
    /// only nonzero when `cyc_whl_kw_req` is negative and regenerative braking cannot provide enough braking,
    pub cyc_fric_brake_kw: Array1<f64>,
    /// Required transmission output power to meet cycle,
    /// equal to `cyc_whl_kw_req` + `cyc_fric_brake_kw`
    pub cyc_trans_kw_out_req: Array1<f64>,
    /// `true` if `cyc_trans_kw_out_req` <= `cur_max_trans_kw_out`
    pub cyc_met: Array1<bool>,
    /// Achieved transmission output power,
    /// either `cyc_trans_kw_out_req` if cycle is met,
    /// or `cur_max_trans_kw_out` if it is not
    pub trans_kw_out_ach: Array1<f64>,
    /// Achieved transmission input power, accounting for `veh.trans_eff`
    pub trans_kw_in_ach: Array1<f64>,
    pub cur_soc_target: Array1<f64>,
    pub min_mc_kw_2help_fc: Array1<f64>,
    /// Achieved electric motor mechanical output power to transmission
    pub mc_mech_kw_out_ach: Array1<f64>,
    /// Achieved electric motor electrical input power,
    /// accounting for electric motor efficiency
    pub mc_elec_kw_in_ach: Array1<f64>,
    /// Auxiliary power load,
    /// optionally overridden with an input array,
    /// or if aux loads are forced to go through alternator (when `veh.no_elec_aux` is `true`) equal to `veh.aux_kw` / `veh.alt_eff`
    /// otherwise equal to `veh.aux_kw`
    pub aux_in_kw: Array1<f64>,
    pub impose_coast: Array1<bool>,
    pub roadway_chg_kw_out_ach: Array1<f64>,
    pub min_ess_kw_2help_fc: Array1<f64>,
    pub ess_kw_out_ach: Array1<f64>,
    pub fc_kw_out_ach: Array1<f64>,
    pub fc_kw_out_ach_pct: Array1<f64>,
    pub fc_kw_in_ach: Array1<f64>,
    pub fs_kw_out_ach: Array1<f64>,
    pub fs_kwh_out_ach: Array1<f64>,
    pub ess_cur_kwh: Array1<f64>,
    /// Current ESS state of charge,
    /// multiply by `veh.ess_max_kwh` to calculate remaining ESS energy
    pub soc: Array1<f64>,
    pub regen_buff_soc: Array1<f64>,
    pub ess_regen_buff_dischg_kw: Array1<f64>,
    pub max_ess_regen_buff_chg_kw: Array1<f64>,
    pub ess_accel_buff_chg_kw: Array1<f64>,
    pub accel_buff_soc: Array1<f64>,
    pub max_ess_accell_buff_dischg_kw: Array1<f64>,
    pub ess_accel_regen_dischg_kw: Array1<f64>,
    pub mc_elec_in_kw_for_max_fc_eff: Array1<f64>,
    /// Electrical power requirement for all-electric operation,
    /// only applicable if vehicle has electrified powertrain,
    /// equal to `aux_in_kw` + `trans_kw_in_ach` / motor efficiency
    pub elec_kw_req_4ae: Array1<f64>,
    pub can_pwr_all_elec: Array1<bool>,
    pub desired_ess_kw_out_for_ae: Array1<f64>,
    pub ess_ae_kw_out: Array1<f64>,
    /// Charging power received from electric roadway (er), if enabled,
    /// for all electric (ae) operation.
    pub er_ae_kw_out: Array1<f64>,
    pub ess_desired_kw_4fc_eff: Array1<f64>,
    pub ess_kw_if_fc_req: Array1<f64>,
    pub cur_max_mc_elec_kw_in: Array1<f64>,
    pub fc_kw_gap_fr_eff: Array1<f64>,
    pub er_kw_if_fc_req: Array1<f64>,
    pub mc_elec_kw_in_if_fc_req: Array1<f64>,
    pub mc_kw_if_fc_req: Array1<f64>,
    pub fc_forced_on: Array1<bool>,
    pub fc_forced_state: Array1<u32>,
    /// Power the motor (mc) must provide if the engine (fc) is being
    /// forced on. If the engine just turned on and triggers a regen
    /// event, it'll be negative.
    pub mc_mech_kw_4forced_fc: Array1<f64>,
    pub fc_time_on: Array1<f64>,
    pub prev_fc_time_on: Array1<f64>,
    pub mps_ach: Array1<f64>,
    pub mph_ach: Array1<f64>,
    pub dist_m: Array1<f64>,
    pub dist_mi: Array1<f64>,
    pub high_acc_fc_on_tag: Array1<bool>,
    pub reached_buff: Array1<bool>,
    pub max_trac_mps: Array1<f64>,
    pub add_kwh: Array1<f64>,
    pub dod_cycs: Array1<f64>,
    pub ess_perc_dead: Array1<f64>,
    /// Power lost to aerodynamic drag according to the drag equation, `1/2 * rho * Cd * A * v_avg³ / 1000`
    pub drag_kw: Array1<f64>,
    pub ess_loss_kw: Array1<f64>,
    /// Power to accelerate, `veh.veh_kg * (v_current² - v_prev²)/2 / dt / 1000`
    pub accel_kw: Array1<f64>,
    /// Power expended to ascend a grade, `sin(atan(grade)) * props.a_grav_mps2 * veh.veh_kg * v_avg / 1000`
    pub ascent_kw: Array1<f64>,
    /// Power lost to rolling resistance, `normal force * veh.wheel_rr_coef * v_avg / 1000`,
    /// with normal force calculated as `cos(atan(grade)) * veh.veh_kg * props.a_grav_mps2`
    pub rr_kw: Array1<f64>,
    pub cur_max_roadway_chg_kw: Array1<f64>,
    pub trace_miss_iters: Array1<u32>,
    pub newton_iters: Array1<u32>,
    pub fuel_kj: f64,
    pub ess_dischg_kj: f64,
    pub energy_audit_error: f64,
    pub mpgge: f64,
    pub roadway_chg_kj: f64,
    pub battery_kwh_per_mi: f64,
    pub electric_kwh_per_mi: f64,
    pub ess2fuel_kwh: f64,
    pub drag_kj: f64,
    pub ascent_kj: f64,
    pub rr_kj: f64,
    pub brake_kj: f64,
    pub trans_kj: f64,
    pub mc_kj: f64,
    pub ess_eff_kj: f64,
    pub aux_kj: f64,
    pub fc_kj: f64,
    pub net_kj: f64,
    pub ke_kj: f64,
    /// `true` when the vehicle misses the prescribed speed trace
    pub trace_miss: bool,
    /// fractional difference between achieved cumulative distance
    /// and prescribed cumulative distance
    pub trace_miss_dist_frac: f64,
    /// fractional difference between achieved time when trace miss is
    /// and prescribed cumulative distance
    pub trace_miss_time_frac: f64,
    /// Maximum speed by which vehicle's speed falls behind prescribed
    /// speed trace
    pub trace_miss_speed_mps: f64,
    #[serde(skip)]
    pub orphaned: bool,
    pub coast_delay_index: Array1<i32>,
    pub idm_target_speed_m_per_s: Array1<f64>,
    #[serde(skip)]
    pub cyc0_cache: RustCycleCache,
    #[api(skip_get, skip_set)]
    #[serde(skip)]
    aux_in_kw_override: Option<Vec<f64>>,
}

impl SerdeAPI for RustSimDrive {
    fn init(&mut self) -> anyhow::Result<()> {
        self.veh.init()?;
        Ok(())
    }
}

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

//     #[test]
//     fn test_walk() {
//         // CYCLE
//         let cyc = RustCycle::test_cyc();
//         let cycle_length = cyc.len();

//         // VEHICLE

//         let veh = RustVehicle::test_veh();

//         // SIM DRIVE
//         let mut sd = RustSimDrive::__new__(cyc, veh);
//         let init_soc = 0.5;
//         sd.walk(init_soc);

//         let expected_final_i = cycle_length;
//         assert_eq!(sd.i, expected_final_i);
//     }
// }