Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary clones #71

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub fn main() -> anyhow::Result<()> {
&& adopt_hd_str_lc != false_string;
(true, adopt_hd_string.clone(), adopt_hd_has_cycle)
} else {
(false, String::from(""), false)
(false, String::default(), false)
};
let cyc = if let Some(cyc_file_path) = fastsim_api.cyc_file {
if cyc_file_path == *"coastdown" {
Expand Down Expand Up @@ -231,7 +231,7 @@ pub fn main() -> anyhow::Result<()> {
vec![0.0],
vec![0.0],
vec![0.0],
String::from(""),
"",
))
}?;

Expand Down
6 changes: 3 additions & 3 deletions rust/fastsim-core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ fn main() {
// path when building using build_and_test.sh
let build_path = "../../python/fastsim/resources".to_string();

let prepath: String = match PathBuf::from(publish_path.clone()).exists() {
let prepath: String = match PathBuf::from(&publish_path).exists() {
true => publish_path,
false => build_path,
};

if !PathBuf::from(prepath.clone()).exists() {
if !PathBuf::from(&prepath).exists() {
// no need for further checks since this indicates that it's
// likely that python fastsim is not available and thus
// fastsim-core is likely being compiled as a dependency
Expand Down Expand Up @@ -59,7 +59,7 @@ fn main() {
for (tf, cf) in truth_files.iter().zip(compare_files) {
let tfc = fs::read_to_string(tf).unwrap_or_else(|_| panic!("{tf} does not exist."));

let cfc = fs::read_to_string(cf.clone()).unwrap_or_else(|_| panic!("{cf} does not exist."));
let cfc = fs::read_to_string(&cf).unwrap_or_else(|_| panic!("{cf} does not exist."));

if tfc != cfc {
panic!("Reference file {tf} does not match file being compared: {cf}. Copy {tf} to {cf} to fix this.")
Expand Down
2 changes: 1 addition & 1 deletion rust/fastsim-core/src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Default for AirProperties {
.into(),
);

let pr_array = mu_array.clone() * c_p_array.clone() / k_array.clone();
let pr_array = &mu_array * &c_p_array / &k_array;

Self {
te_array_degc,
Expand Down
31 changes: 13 additions & 18 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn trapz_distance_over_range(cyc: &RustCycle, i_start: usize, i_end: usize)
pub fn time_spent_moving(cyc: &RustCycle, stopped_speed_m_per_s: Option<f64>) -> f64 {
let mut t_move_s = 0.0;
let stopped_speed_m_per_s = stopped_speed_m_per_s.unwrap_or(0.0);
for idx in 1..cyc.time_s.len() {
for idx in 1..cyc.len() {
let dt = cyc.time_s[idx] - cyc.time_s[idx - 1];
let vavg = (cyc.mps[idx] + cyc.mps[idx - 1]) / 2.0;
if vavg > stopped_speed_m_per_s {
Expand Down Expand Up @@ -229,7 +229,7 @@ pub fn to_microtrips(cycle: &RustCycle, stop_speed_m_per_s: Option<f64>) -> Vec<
mt_vs.clone(),
mt_gs.clone(),
mt_rs.clone(),
cycle.name.clone(),
&cycle.name,
));
mt_ts = vec![last_t];
mt_vs = vec![last_v];
Expand All @@ -244,13 +244,7 @@ pub fn to_microtrips(cycle: &RustCycle, stop_speed_m_per_s: Option<f64>) -> Vec<
}
if !mt_ts.is_empty() {
mt_ts = mt_ts.iter().map(|t| -> f64 { t - mt_ts[0] }).collect();
microtrips.push(RustCycle::new(
mt_ts,
mt_vs,
mt_gs,
mt_rs,
cycle.name.clone(),
));
microtrips.push(RustCycle::new(mt_ts, mt_vs, mt_gs, mt_rs, &cycle.name));
}
microtrips
}
Expand Down Expand Up @@ -344,7 +338,7 @@ pub fn extend_cycle(
rs.push(0.0);
idx += 1;
}
RustCycle::new(ts, vs, gs, rs, cyc.name.clone())
RustCycle::new(ts, vs, gs, rs, &cyc.name)
}

#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -399,7 +393,7 @@ impl SerdeAPI for RustCycleCache {}
impl RustCycleCache {
pub fn new(cyc: &RustCycle) -> Self {
let tol = 1e-6;
let num_items = cyc.time_s.len();
let num_items = cyc.len();
let grade_all_zero = cyc.grade.iter().all(|g| *g == 0.0);
let trapz_step_distances_m = trapz_step_distances(cyc);
let trapz_distances_m = ndarrcumsum(&trapz_step_distances_m);
Expand Down Expand Up @@ -686,17 +680,18 @@ impl SerdeAPI for RustCycle {

/// pure Rust methods that need to be separate due to pymethods incompatibility
impl RustCycle {
pub fn new(
pub fn new<S: AsRef<str>>(
time_s: Vec<f64>,
mps: Vec<f64>,
grade: Vec<f64>,
road_type: Vec<f64>,
name: String,
name: S,
) -> Self {
let time_s = Array::from_vec(time_s);
let mps = Array::from_vec(mps);
let grade = Array::from_vec(grade);
let road_type = Array::from_vec(road_type);
let name = name.as_ref().to_string();
Self {
time_s,
mps,
Expand Down Expand Up @@ -1014,7 +1009,7 @@ impl RustCycle {

/// elevation change w.r.t. to initial
pub fn delta_elev_m(&self) -> Array1<f64> {
ndarrcumsum(&(self.dist_m() * self.grade.clone()))
ndarrcumsum(&(self.dist_m() * &self.grade))
}
}

Expand Down Expand Up @@ -1051,7 +1046,7 @@ pub fn detect_passing(
i: usize,
dist_tol_m: Option<f64>,
) -> PassingInfo {
if i >= cyc.time_s.len() {
if i >= cyc.len() {
return PassingInfo {
has_collision: false,
idx: 0,
Expand Down Expand Up @@ -1127,7 +1122,7 @@ mod tests {
let speed_mps = vec![0.0, 10.0, 10.0, 0.0, 0.0];
let grade = Array::zeros(5).to_vec();
let road_type = Array::zeros(5).to_vec();
let name = String::from("test");
let name = "test";
let cyc = RustCycle::new(time_s, speed_mps, grade, road_type, name);
let avg_mps = average_step_speeds(&cyc);
let expected_avg_mps = Array::from_vec(vec![0.0, 5.0, 10.0, 5.0, 0.0]);
Expand All @@ -1148,10 +1143,10 @@ mod tests {
let cyc_file_path = resources_path().join("cycles/udds.csv");
let expected_udds_length: usize = 1370;
let cyc = RustCycle::from_csv_file(cyc_file_path).unwrap();
let num_entries = cyc.time_s.len();
let num_entries = cyc.len();
assert_eq!(cyc.name, String::from("udds"));
assert!(num_entries > 0);
assert_eq!(num_entries, cyc.time_s.len());
assert_eq!(num_entries, cyc.len());
assert_eq!(num_entries, cyc.mps.len());
assert_eq!(num_entries, cyc.grade.len());
assert_eq!(num_entries, cyc.road_type.len());
Expand Down
4 changes: 2 additions & 2 deletions rust/fastsim-core/src/simdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ impl Default for RustSimDriveParams {

#[getter]
pub fn get_fs_cumu_mj_out_ach(&self) -> Pyo3ArrayF64 {
Pyo3ArrayF64::new(ndarrcumsum(&(self.fs_kw_out_ach.clone() * self.cyc.dt_s() * 1e-3)))
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.clone() * self.cyc.dt_s() * 1e-3)))
Pyo3ArrayF64::new(ndarrcumsum(&(&self.fc_kw_out_ach * self.cyc.dt_s() * 1e-3)))
}
)]
pub struct RustSimDrive {
Expand Down
19 changes: 9 additions & 10 deletions rust/fastsim-core/src/simdrive/cyc_mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,13 @@ impl RustSimDrive {
let a_brake = self.sim_params.coast_brake_accel_m_per_s2;
assert![a_brake <= 0.0];
let ds = &self.cyc0_cache.trapz_distances_m;
let gs = self.cyc0.grade.clone();
let d0 = trapz_step_start_distance(&self.cyc, i);
let mut distances_m: Vec<f64> = Vec::with_capacity(ds.len());
let mut grade_by_distance: Vec<f64> = Vec::with_capacity(ds.len());
for idx in 0..ds.len() {
if ds[idx] >= d0 {
distances_m.push(ds[idx] - d0);
grade_by_distance.push(gs[idx])
grade_by_distance.push(self.cyc0.grade[idx])
}
}
if distances_m.is_empty() {
Expand Down Expand Up @@ -754,7 +753,7 @@ impl RustSimDrive {
fn set_coast_delay(&mut self, i: usize) {
let speed_tol = 0.01; // m/s
let dist_tol = 0.1; // meters
for idx in i..self.cyc.time_s.len() {
for idx in i..self.cyc.len() {
self.coast_delay_index[idx] = 0; // clear all future coast-delays
}
let mut coast_delay: Option<i32> = None;
Expand Down Expand Up @@ -848,7 +847,7 @@ impl RustSimDrive {
if n < 2 {
break;
}
if (idx - 1 + full_brake_steps) >= self.cyc.time_s.len() {
if (idx - 1 + full_brake_steps) >= self.cyc.len() {
break;
}
let dt = collision.time_step_duration_s;
Expand All @@ -873,7 +872,7 @@ impl RustSimDrive {
let mut accels_m_per_s2: Vec<f64> = vec![];
let mut trace_accels_m_per_s2: Vec<f64> = vec![];
for ni in 0..n {
if (ni + idx + full_brake_steps) >= self.cyc.time_s.len() {
if (ni + idx + full_brake_steps) >= self.cyc.len() {
break;
}
accels_m_per_s2.push(accel_for_constant_jerk(
Expand All @@ -889,9 +888,9 @@ impl RustSimDrive {
);
}
let all_sub_coast: bool = trace_accels_m_per_s2
.clone()
.into_iter()
.zip(accels_m_per_s2.clone().into_iter())
.iter()
.copied()
.zip(accels_m_per_s2.iter().copied())
.fold(
true,
|all_sc_flag: bool, (trace_accel, accel): (f64, f64)| {
Expand Down Expand Up @@ -934,7 +933,7 @@ impl RustSimDrive {
return self.prevent_collisions(i, Some(new_passing_tol_m));
}
for fbs in 0..best.full_brake_steps {
if (best.idx + fbs) >= self.cyc.time_s.len() {
if (best.idx + fbs) >= self.cyc.len() {
break;
}
let dt = self.cyc.time_s[best.idx + fbs] - self.cyc.time_s[best.idx - 1];
Expand Down Expand Up @@ -1037,7 +1036,7 @@ impl RustSimDrive {
i,
None,
)?;
for idx in i..self.cyc.time_s.len() {
for idx in i..self.cyc.len() {
self.impose_coast[idx] = idx < (i + num_steps);
}
adjusted_current_speed = true;
Expand Down
Loading
Loading