From 92f6a431e0fdb80e14fab7da525c0c5514bb3b19 Mon Sep 17 00:00:00 2001 From: Kyle Carow Date: Fri, 5 Jan 2024 14:45:08 -0500 Subject: [PATCH] remove initialized field --- rust/fastsim-cli/src/bin/fastsim-cli.rs | 1 - rust/fastsim-core/src/cycle.rs | 60 +++++++++---------------- rust/fastsim-core/src/simdrivelabel.rs | 1 - rust/fastsim-core/src/vehicle.rs | 39 ++++------------ rust/fastsim-core/src/vehicle_utils.rs | 1 - 5 files changed, 30 insertions(+), 72 deletions(-) diff --git a/rust/fastsim-cli/src/bin/fastsim-cli.rs b/rust/fastsim-cli/src/bin/fastsim-cli.rs index 385e7561..3d73845a 100644 --- a/rust/fastsim-cli/src/bin/fastsim-cli.rs +++ b/rust/fastsim-cli/src/bin/fastsim-cli.rs @@ -234,7 +234,6 @@ pub fn main() -> anyhow::Result<()> { grade: array![0.0], road_type: array![0.0], name: String::default(), - initialized: true, orphaned: false, }) }?; diff --git a/rust/fastsim-core/src/cycle.rs b/rust/fastsim-core/src/cycle.rs index bda99f0a..eb9d1979 100644 --- a/rust/fastsim-core/src/cycle.rs +++ b/rust/fastsim-core/src/cycle.rs @@ -227,7 +227,6 @@ pub fn to_microtrips(cycle: &RustCycle, stop_speed_m_per_s: Option) -> Vec< grade: Array::from_vec(mt_gs), road_type: Array::from_vec(mt_rs), name: cycle.name.clone(), - initialized: true, orphaned: false, }); mt_ts = vec![last_t]; @@ -249,7 +248,6 @@ pub fn to_microtrips(cycle: &RustCycle, stop_speed_m_per_s: Option) -> Vec< grade: Array::from_vec(mt_gs), road_type: Array::from_vec(mt_rs), name: cycle.name.clone(), - initialized: true, orphaned: false, }); } @@ -351,7 +349,6 @@ pub fn extend_cycle( grade: Array::from_vec(gs), road_type: Array::from_vec(rs), name: cyc.name.clone(), - initialized: true, orphaned: false, } } @@ -516,7 +513,6 @@ impl RustCycleCache { Array::default(cyc_len) }, name: PyAny::get_item(dict, "name").and_then(String::extract).unwrap_or_default(), - initialized: false, orphaned: false, }; cyc.init()?; @@ -629,8 +625,6 @@ pub struct RustCycle { pub road_type: Array1, pub name: String, #[serde(skip)] - pub initialized: bool, - #[serde(skip)] pub orphaned: bool, } @@ -639,23 +633,20 @@ impl SerdeAPI for RustCycle { const ACCEPTED_STR_FORMATS: &'static [&'static str] = &["yaml", "json", "csv"]; fn init(&mut self) -> anyhow::Result<()> { - if !self.initialized { - ensure!(!self.is_empty(), "Deserialized cycle is empty"); - let cyc_len = self.len(); - ensure!( - self.mps.len() == cyc_len, - "Length of `mps` does not match length of `time_s`" - ); - ensure!( - self.grade.len() == cyc_len, - "Length of `grade` does not match length of `time_s`" - ); - ensure!( - self.road_type.len() == cyc_len, - "Length of `road_type` does not match length of `time_s`" - ); - self.initialized = true; - } + ensure!(!self.is_empty(), "Deserialized cycle is empty"); + let cyc_len = self.len(); + ensure!( + self.mps.len() == cyc_len, + "Length of `mps` does not match length of `time_s`" + ); + ensure!( + self.grade.len() == cyc_len, + "Length of `grade` does not match length of `time_s`" + ); + ensure!( + self.road_type.len() == cyc_len, + "Length of `road_type` does not match length of `time_s`" + ); Ok(()) } @@ -724,10 +715,8 @@ impl SerdeAPI for RustCycle { let mut cyc = Self::default(); let mut rdr = csv::Reader::from_reader(rdr); for result in rdr.deserialize() { - cyc.initialized = true; // skip init checks until finished - cyc.push(result?)?; + cyc.push(result?); } - cyc.initialized = false; cyc } _ => { @@ -774,7 +763,6 @@ impl TryFrom>> for RustCycle { .to_owned(), ), name: String::default(), - initialized: false, orphaned: false, }; cyc.init()?; @@ -798,14 +786,11 @@ impl RustCycle { /// Load cycle from CSV file, parsing name from filepath pub fn from_csv_file>(filepath: P) -> anyhow::Result { let filepath = filepath.as_ref(); - let name = String::from( - filepath - .file_stem() - .and_then(OsStr::to_str) - .with_context(|| { - format!("Could not parse cycle name from filepath: {filepath:?}") - })?, - ); + let name = filepath + .file_stem() + .and_then(OsStr::to_str) + .with_context(|| format!("Could not parse cycle name from filepath: {filepath:?}"))? + .to_string(); let file = File::open(filepath).with_context(|| { if !filepath.exists() { format!("File not found: {filepath:?}") @@ -843,7 +828,7 @@ impl RustCycle { RustCycleCache::new(self) } - pub fn push(&mut self, cyc_elem: RustCycleElement) -> anyhow::Result<()> { + pub fn push(&mut self, cyc_elem: RustCycleElement) { self.time_s .append(Axis(0), array![cyc_elem.time_s].view()) .unwrap(); @@ -858,7 +843,6 @@ impl RustCycle { .append(Axis(0), array![road_type].view()) .unwrap(); } - self.init() } pub fn len(&self) -> usize { @@ -876,7 +860,6 @@ impl RustCycle { grade: Array::zeros(10), road_type: Array::zeros(10), name: String::from("test"), - initialized: true, orphaned: false, } } @@ -1224,7 +1207,6 @@ mod tests { grade: Array::zeros(5), road_type: Array::zeros(5), name: String::from("test"), - initialized: true, orphaned: false, }; let avg_mps = average_step_speeds(&cyc); diff --git a/rust/fastsim-core/src/simdrivelabel.rs b/rust/fastsim-core/src/simdrivelabel.rs index ce6b675a..34f6167f 100644 --- a/rust/fastsim-core/src/simdrivelabel.rs +++ b/rust/fastsim-core/src/simdrivelabel.rs @@ -133,7 +133,6 @@ pub fn make_accel_trace() -> RustCycle { grade: Array::zeros(cyc_len), road_type: Array::zeros(cyc_len), name: String::from("accel"), - initialized: true, orphaned: false, } } diff --git a/rust/fastsim-core/src/vehicle.rs b/rust/fastsim-core/src/vehicle.rs index dc579fab..61968488 100644 --- a/rust/fastsim-core/src/vehicle.rs +++ b/rust/fastsim-core/src/vehicle.rs @@ -541,10 +541,6 @@ pub struct RustVehicle { #[doc(hidden)] #[doc_field(skip_doc)] pub max_regen_kwh: f64, - #[serde(skip)] - #[doc(hidden)] - #[doc_field(skip_doc)] - pub initialized: bool, } /// RustVehicle rust methods @@ -720,25 +716,25 @@ impl RustVehicle { // self.max_roadway_chg_kw = Array1::from_vec(vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]); // self.charging_on = false; - // # Checking if a vehicle has any hybrid components + // Checking if a vehicle has any hybrid components self.no_elec_sys = (self.ess_max_kwh == 0.0) || (self.ess_max_kw == 0.0) || (self.mc_max_kw == 0.0); - // # Checking if aux loads go through an alternator + // Checking if aux loads go through an alternator self.no_elec_aux = self.no_elec_sys || (self.mc_max_kw <= self.aux_kw) || self.force_aux_on_fc; self.fc_perc_out_array = FC_PERC_OUT_ARRAY.clone().to_vec(); - // # discrete array of possible engine power outputs + // discrete array of possible engine power outputs self.input_kw_out_array = &self.fc_pwr_out_perc * self.fc_max_kw; - // # Relatively continuous array of possible engine power outputs + // Relatively continuous array of possible engine power outputs self.fc_kw_out_array = self .fc_perc_out_array .iter() .map(|n| n * self.fc_max_kw) .collect(); - // # Creates relatively continuous array for fc_eff + // Creates relatively continuous array for fc_eff if self.fc_eff_array.is_empty() { self.fc_eff_array = self .fc_perc_out_array @@ -753,9 +749,10 @@ impl RustVehicle { }) .collect(); } - self.modern_max = MODERN_MAX; + if self.modern_max == 0.0 { + self.modern_max = MODERN_MAX; + } - // NOTE: unused because the first part of if/else commented below is unused let modern_diff = self.modern_max - arrmax(&LARGE_BASELINE_EFF); let large_baseline_eff_adj: Vec = LARGE_BASELINE_EFF.iter().map(|x| x + modern_diff).collect(); @@ -768,15 +765,6 @@ impl RustVehicle { ), ); - // NOTE: it should not be possible to have `None in self.mc_eff_map` in Rust (although NaN is possible...). - // if we want to express that mc_eff_map should be calculated in some cases, but not others, - // we may need some sort of option type ? - //if None in self.mc_eff_map: - // self.mc_eff_array = mc_kw_adj_perc * large_baseline_eff_adj + \ - // (1 - mc_kw_adj_perc) * self.small_baseline_eff - // self.mc_eff_map = self.mc_eff_array - //else: - // self.mc_eff_array = self.mc_eff_map if self.mc_eff_map == Array1::::zeros(LARGE_BASELINE_EFF.len()) { self.mc_eff_map = large_baseline_eff_adj .iter() @@ -785,10 +773,6 @@ impl RustVehicle { .collect(); } self.mc_eff_array = self.mc_eff_map.clone(); - // println!("{:?}",self.mc_eff_map); - // self.mc_eff_array = mc_kw_adj_perc * large_baseline_eff_adj - // + (1.0 - mc_kw_adj_perc) * &self.small_baseline_eff; - // self.mc_eff_map = self.mc_eff_array.clone(); self.mc_perc_out_array = MC_PERC_OUT_ARRAY.clone().to_vec(); @@ -1035,7 +1019,6 @@ impl RustVehicle { ess_to_fuel_ok_error_doc: Default::default(), fc_peak_eff_override_doc: Default::default(), mc_peak_eff_override_doc: Default::default(), - initialized: false, }; v.set_derived().unwrap(); v @@ -1069,11 +1052,7 @@ impl Default for RustVehicle { impl SerdeAPI for RustVehicle { fn init(&mut self) -> anyhow::Result<()> { - if !self.initialized { - self.set_derived()?; - self.initialized = true; - } - Ok(()) + self.set_derived() } } diff --git a/rust/fastsim-core/src/vehicle_utils.rs b/rust/fastsim-core/src/vehicle_utils.rs index f43b8413..a34b99a8 100644 --- a/rust/fastsim-core/src/vehicle_utils.rs +++ b/rust/fastsim-core/src/vehicle_utils.rs @@ -1153,7 +1153,6 @@ pub fn abc_to_drag_coeffs( grade: Array::zeros(cd_len), road_type: Array::zeros(cd_len), name: String::from("cycle"), - initialized: true, orphaned: false, };