Skip to content

Commit

Permalink
remove initialized field
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Carow authored and Kyle Carow committed Jan 5, 2024
1 parent 2f4f433 commit 92f6a43
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 72 deletions.
1 change: 0 additions & 1 deletion rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}?;
Expand Down
60 changes: 21 additions & 39 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ pub fn to_microtrips(cycle: &RustCycle, stop_speed_m_per_s: Option<f64>) -> 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];
Expand All @@ -249,7 +248,6 @@ pub fn to_microtrips(cycle: &RustCycle, stop_speed_m_per_s: Option<f64>) -> Vec<
grade: Array::from_vec(mt_gs),
road_type: Array::from_vec(mt_rs),
name: cycle.name.clone(),
initialized: true,
orphaned: false,
});
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -629,8 +625,6 @@ pub struct RustCycle {
pub road_type: Array1<f64>,
pub name: String,
#[serde(skip)]
pub initialized: bool,
#[serde(skip)]
pub orphaned: bool,
}

Expand All @@ -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(())
}

Expand Down Expand Up @@ -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
}
_ => {
Expand Down Expand Up @@ -774,7 +763,6 @@ impl TryFrom<HashMap<String, Vec<f64>>> for RustCycle {
.to_owned(),
),
name: String::default(),
initialized: false,
orphaned: false,
};
cyc.init()?;
Expand All @@ -798,14 +786,11 @@ impl RustCycle {
/// Load cycle from CSV file, parsing name from filepath
pub fn from_csv_file<P: AsRef<Path>>(filepath: P) -> anyhow::Result<Self> {
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:?}")
Expand Down Expand Up @@ -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();
Expand All @@ -858,7 +843,6 @@ impl RustCycle {
.append(Axis(0), array![road_type].view())
.unwrap();
}
self.init()
}

pub fn len(&self) -> usize {
Expand All @@ -876,7 +860,6 @@ impl RustCycle {
grade: Array::zeros(10),
road_type: Array::zeros(10),
name: String::from("test"),
initialized: true,
orphaned: false,
}
}
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion rust/fastsim-core/src/simdrivelabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
39 changes: 9 additions & 30 deletions rust/fastsim-core/src/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<f64> =
LARGE_BASELINE_EFF.iter().map(|x| x + modern_diff).collect();
Expand All @@ -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::<f64>::zeros(LARGE_BASELINE_EFF.len()) {
self.mc_eff_map = large_baseline_eff_adj
.iter()
Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down
1 change: 0 additions & 1 deletion rust/fastsim-core/src/vehicle_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down

0 comments on commit 92f6a43

Please sign in to comment.