Skip to content

Commit

Permalink
remove some redundancies and very minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Carow authored and Kyle Carow committed Nov 8, 2023
1 parent 7655b39 commit c4fba56
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 66 deletions.
3 changes: 0 additions & 3 deletions python/fastsim/fastsimrust.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ class RustSimDrive(SerdeAPI):
cur_max_fs_kw_out: Pyo3ArrayF64
cur_max_mc_elec_kw_in: Pyo3ArrayF64
cur_max_mc_kw_out: Pyo3ArrayF64
cur_max_mech_mc_kw_in: Pyo3ArrayF64
cur_max_roadway_chg_kw: Pyo3ArrayF64
cur_max_trac_kw: Pyo3ArrayF64
cur_max_trans_kw_out: Pyo3ArrayF64
Expand Down Expand Up @@ -452,13 +451,11 @@ class RustSimDrive(SerdeAPI):
ess_regen_buff_dischg_kw: Pyo3ArrayF64
fc_forced_on: Pyo3ArrayBool
fc_forced_state: Pyo3ArrayU32
fc_fs_lim_kw: Pyo3ArrayF64
fc_kj: float
fc_kw_gap_fr_eff: Pyo3ArrayF64
fc_kw_in_ach: Pyo3ArrayF64
fc_kw_out_ach: Pyo3ArrayF64
fc_kw_out_ach_pct: Pyo3ArrayF64
fc_max_kw_in: Pyo3ArrayF64
fc_time_on: Pyo3ArrayF64
fc_trans_lim_kw: Pyo3ArrayF64
fs_cumu_mj_out_ach: Pyo3ArrayF64
Expand Down
26 changes: 9 additions & 17 deletions python/fastsim/simdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ def init_arrays(self):
# Component Limits -- calculated dynamically
self.cur_max_fs_kw_out = np.zeros(cyc_len, dtype=np.float64)
self.fc_trans_lim_kw = np.zeros(cyc_len, dtype=np.float64)
self.fc_fs_lim_kw = np.zeros(cyc_len, dtype=np.float64)
self.fc_max_kw_in = np.zeros(cyc_len, dtype=np.float64)
self.cur_max_fc_kw_out = np.zeros(cyc_len, dtype=np.float64)
self.ess_cap_lim_dischg_kw = np.zeros(cyc_len, dtype=np.float64)
self.cur_ess_max_kw_out = np.zeros(cyc_len, dtype=np.float64)
Expand All @@ -229,7 +227,6 @@ def init_arrays(self):
self.ess_lim_mc_regen_perc_kw = np.zeros(
cyc_len, dtype=np.float64)
self.ess_lim_mc_regen_kw = np.zeros(cyc_len, dtype=np.float64)
self.cur_max_mech_mc_kw_in = np.zeros(cyc_len, dtype=np.float64)
self.cur_max_trans_kw_out = np.zeros(cyc_len, dtype=np.float64)

# Drive Train
Expand Down Expand Up @@ -720,11 +717,8 @@ def set_comp_lims(self, i):
self.veh.fc_max_kw / self.veh.fc_sec_to_peak_pwr * self.cyc.dt_s_at_i(i)
)

self.fc_max_kw_in[i] = min(
self.cur_max_fs_kw_out[i], self.veh.fs_max_kw)
self.fc_fs_lim_kw[i] = self.veh.fc_max_kw
self.cur_max_fc_kw_out[i] = min(
self.veh.fc_max_kw, self.fc_fs_lim_kw[i], self.fc_trans_lim_kw[i])
self.veh.fc_max_kw, self.fc_trans_lim_kw[i])

if self.veh.ess_max_kwh == 0 or self.soc[i-1] < self.veh.min_soc:
self.ess_cap_lim_dischg_kw[i] = 0.0
Expand Down Expand Up @@ -835,8 +829,6 @@ def set_comp_lims(self, i):
]
)

self.cur_max_mech_mc_kw_in[i] = min(
self.ess_lim_mc_regen_kw[i], self.veh.mc_max_kw)
self.cur_max_trac_kw[i] = (
self.veh.wheel_coef_of_fric * self.veh.drive_axle_weight_frac *
self.veh.veh_kg * self.props.a_grav_mps2
Expand Down Expand Up @@ -920,7 +912,7 @@ def set_power_calcs(self, i):
self.regen_contrl_lim_kw_perc[i] = self.veh.max_regen / (1 + self.veh.regen_a * np.exp(-self.veh.regen_b * (
(self.cyc.mph[i] + self.mps_ach[i-1] * params.MPH_PER_MPS) / 2.0 + 1.0)))
self.cyc_regen_brake_kw[i] = max(min(
self.cur_max_mech_mc_kw_in[i] * self.veh.trans_eff,
self.ess_lim_mc_regen_kw[i] * self.veh.trans_eff,
self.regen_contrl_lim_kw_perc[i] * -self.cyc_whl_kw_req[i]),
0
)
Expand Down Expand Up @@ -948,14 +940,14 @@ def set_power_calcs(self, i):

if self.veh.fc_eff_type == H2FC:
self.min_mc_kw_2help_fc[i] = max(
self.trans_kw_in_ach[i], -self.cur_max_mech_mc_kw_in[i])
self.trans_kw_in_ach[i], -self.ess_lim_mc_regen_kw[i])

else:
self.min_mc_kw_2help_fc[i] = max(
self.trans_kw_in_ach[i] - self.cur_max_fc_kw_out[i], -self.cur_max_mech_mc_kw_in[i])
self.trans_kw_in_ach[i] - self.cur_max_fc_kw_out[i], -self.ess_lim_mc_regen_kw[i])
else:
self.min_mc_kw_2help_fc[i] = max(
self.cur_max_mc_kw_out[i], -self.cur_max_mech_mc_kw_in[i])
self.cur_max_mc_kw_out[i], -self.ess_lim_mc_regen_kw[i])

def set_ach_speed(self, i):
"""
Expand Down Expand Up @@ -1361,7 +1353,7 @@ def set_hybrid_cont_decisions(self, i):

self.er_kw_if_fc_req[i] = max(0,
min(
self.cur_max_roadway_chg_kw[i], self.cur_max_mech_mc_kw_in[i],
self.cur_max_roadway_chg_kw[i], self.ess_lim_mc_regen_kw[i],
self.ess_kw_if_fc_req[i] -
self.mc_elec_in_lim_kw[i] +
self.aux_in_kw[i]
Expand Down Expand Up @@ -1415,17 +1407,17 @@ def set_hybrid_cont_decisions(self, i):
if self.veh.fc_eff_type != H2FC and self.veh.fc_max_kw > 0:
if self.can_pwr_all_elec[i] == 1:
self.mc_mech_kw_out_ach[i] = - \
min(self.cur_max_mech_mc_kw_in[i], -
min(self.ess_lim_mc_regen_kw[i], -
self.trans_kw_in_ach[i])
else:
self.mc_mech_kw_out_ach[i] = min(
-min(self.cur_max_mech_mc_kw_in[i], -self.trans_kw_in_ach[i]),
-min(self.ess_lim_mc_regen_kw[i], -self.trans_kw_in_ach[i]),
max(-self.cur_max_fc_kw_out[i],
self.mc_kw_if_fc_req[i])
)
else:
self.mc_mech_kw_out_ach[i] = min(
-min(self.cur_max_mech_mc_kw_in[i], -self.trans_kw_in_ach[i]),
-min(self.ess_lim_mc_regen_kw[i], -self.trans_kw_in_ach[i]),
-self.trans_kw_in_ach[i]
)

Expand Down
4 changes: 0 additions & 4 deletions python/fastsim/tests/test_simdrive_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ def test_post_diagnostics(self):
"cur_ess_max_kw_out",
"cur_max_fs_kw_out",
"cur_max_mc_elec_kw_in",
"cur_max_mech_mc_kw_in",
"cur_max_trac_kw",
"cur_max_trans_kw_out",
"cyc_fric_brake_kw",
Expand All @@ -226,9 +225,6 @@ def test_post_diagnostics(self):
"ess_cap_lim_dischg_kw",
"ess_lim_mc_regen_kw",
"ess_lim_mc_regen_perc_kw",
"fc_fs_lim_kw",
"fc_max_kw_in",
"fc_max_kw_in",
"fc_trans_lim_kw",
"high_acc_fc_on_tag",
"max_trac_mps",
Expand Down
6 changes: 0 additions & 6 deletions rust/fastsim-core/src/simdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,6 @@ pub struct RustSimDrive {
/// 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>,
/// REDUNDANT: always equal to `veh.fc_max_kw`
pub fc_fs_lim_kw: Array1<f64>,
/// REDUNDANT: always equal to `cur_max_fs_kw_out`
pub fc_max_kw_in: 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>,
Expand Down Expand Up @@ -414,8 +410,6 @@ pub struct RustSimDrive {
/// ESS limit on electricity regeneration,
/// considering `veh.mc_max_kw`, or `cur_max_ess_chg_kw` and motor efficiency
pub ess_lim_mc_regen_kw: Array1<f64>,
/// REDUNDANT: always equal to `ess_lim_mc_regen_kw`
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`
Expand Down
39 changes: 11 additions & 28 deletions rust/fastsim-core/src/simdrive/simdrive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ impl RustSimDrive {
let cyc_len = cyc.time_s.len(); //get_len() as usize;
let cur_max_fs_kw_out = Array::zeros(cyc_len);
let fc_trans_lim_kw = Array::zeros(cyc_len);
let fc_fs_lim_kw = Array::zeros(cyc_len);
let fc_max_kw_in = Array::zeros(cyc_len);
let cur_max_fc_kw_out = Array::zeros(cyc_len);
let ess_cap_lim_dischg_kw = Array::zeros(cyc_len);
let cur_ess_max_kw_out = Array::zeros(cyc_len);
Expand All @@ -49,7 +47,6 @@ impl RustSimDrive {
let cur_max_mc_kw_out = Array::zeros(cyc_len);
let ess_lim_mc_regen_perc_kw = Array::zeros(cyc_len);
let ess_lim_mc_regen_kw = Array::zeros(cyc_len);
let cur_max_mech_mc_kw_in = Array::zeros(cyc_len);
let cur_max_trans_kw_out = Array::zeros(cyc_len);
let cyc_trac_kw_req = Array::zeros(cyc_len);
let cur_max_trac_kw = Array::zeros(cyc_len);
Expand Down Expand Up @@ -159,8 +156,6 @@ impl RustSimDrive {
i, // 1 # initialize step counter for possible use outside sim_drive_walk()
cur_max_fs_kw_out,
fc_trans_lim_kw,
fc_fs_lim_kw,
fc_max_kw_in,
cur_max_fc_kw_out,
ess_cap_lim_dischg_kw,
cur_ess_max_kw_out,
Expand All @@ -173,7 +168,6 @@ impl RustSimDrive {
cur_max_mc_kw_out,
ess_lim_mc_regen_perc_kw,
ess_lim_mc_regen_kw,
cur_max_mech_mc_kw_in,
cur_max_trans_kw_out,
cyc_trac_kw_req,
cur_max_trac_kw,
Expand Down Expand Up @@ -291,8 +285,6 @@ impl RustSimDrive {
// Component Limits -- calculated dynamically
self.cur_max_fs_kw_out = Array::zeros(cyc_len);
self.fc_trans_lim_kw = Array::zeros(cyc_len);
self.fc_fs_lim_kw = Array::zeros(cyc_len);
self.fc_max_kw_in = Array::zeros(cyc_len);
self.cur_max_fc_kw_out = Array::zeros(cyc_len);
self.ess_cap_lim_dischg_kw = Array::zeros(cyc_len);
self.cur_ess_max_kw_out = Array::zeros(cyc_len);
Expand All @@ -305,7 +297,6 @@ impl RustSimDrive {
self.cur_max_mc_kw_out = Array::zeros(cyc_len);
self.ess_lim_mc_regen_perc_kw = Array::zeros(cyc_len);
self.ess_lim_mc_regen_kw = Array::zeros(cyc_len);
self.cur_max_mech_mc_kw_in = Array::zeros(cyc_len);
self.cur_max_trans_kw_out = Array::zeros(cyc_len);

// Drive Train
Expand Down Expand Up @@ -658,12 +649,7 @@ impl RustSimDrive {
self.fc_trans_lim_kw[i] = self.fc_kw_out_ach[i - 1]
+ (self.veh.fc_max_kw / self.veh.fc_sec_to_peak_pwr * self.cyc.dt_s_at_i(i));

self.fc_max_kw_in[i] = min(self.cur_max_fs_kw_out[i], self.veh.fs_max_kw);
self.fc_fs_lim_kw[i] = self.veh.fc_max_kw;
self.cur_max_fc_kw_out[i] = min(
self.veh.fc_max_kw,
min(self.fc_fs_lim_kw[i], self.fc_trans_lim_kw[i]),
);
self.cur_max_fc_kw_out[i] = min(self.veh.fc_max_kw, self.fc_trans_lim_kw[i]);

if self.veh.ess_max_kwh == 0.0 || self.soc[i - 1] < self.veh.min_soc {
self.ess_cap_lim_dischg_kw[i] = 0.0;
Expand Down Expand Up @@ -801,7 +787,6 @@ impl RustSimDrive {
)],
);
}
self.cur_max_mech_mc_kw_in[i] = min(self.ess_lim_mc_regen_kw[i], self.veh.mc_max_kw);

self.cur_max_trac_kw[i] = self.veh.wheel_coef_of_fric
* self.veh.drive_axle_weight_frac
Expand Down Expand Up @@ -903,7 +888,7 @@ impl RustSimDrive {
.exp());
self.cyc_regen_brake_kw[i] = max(
min(
self.cur_max_mech_mc_kw_in[i] * self.veh.trans_eff,
self.ess_lim_mc_regen_kw[i] * self.veh.trans_eff,
self.regen_contrl_lim_kw_perc[i] * -self.cyc_whl_kw_req[i],
),
0.0,
Expand All @@ -928,16 +913,16 @@ impl RustSimDrive {
if self.cyc_met[i] {
if self.veh.fc_eff_type == H2FC {
self.min_mc_kw_2help_fc[i] =
max(self.trans_kw_in_ach[i], -self.cur_max_mech_mc_kw_in[i]);
max(self.trans_kw_in_ach[i], -self.ess_lim_mc_regen_kw[i]);
} else {
self.min_mc_kw_2help_fc[i] = max(
self.trans_kw_in_ach[i] - self.cur_max_fc_kw_out[i],
-self.cur_max_mech_mc_kw_in[i],
-self.ess_lim_mc_regen_kw[i],
);
}
} else {
self.min_mc_kw_2help_fc[i] =
max(self.cur_max_mc_kw_out[i], -self.cur_max_mech_mc_kw_in[i]);
max(self.cur_max_mc_kw_out[i], -self.ess_lim_mc_regen_kw[i]);
}
Ok(())
}
Expand Down Expand Up @@ -1101,7 +1086,7 @@ impl RustSimDrive {
self.regen_buff_soc[i] = 0.0;
} else if self.veh.charging_on {
self.regen_buff_soc[i] = max(
self.veh.max_soc - (self.veh.max_regen_kwh() / self.veh.ess_max_kwh),
self.veh.max_soc - (self.veh.max_regen_kwh / self.veh.ess_max_kwh),
(self.veh.max_soc + self.veh.min_soc) / 2.0,
);
} else {
Expand Down Expand Up @@ -1147,10 +1132,8 @@ impl RustSimDrive {
* min(
self.veh.max_accel_buffer_perc_of_useable_soc
* (self.veh.max_soc - self.veh.min_soc),
self.veh.max_regen_kwh() / self.veh.ess_max_kwh,
self.veh.max_regen_kwh / self.veh.ess_max_kwh,
)
* self.veh.ess_max_kwh
/ self.veh.ess_max_kwh
+ self.veh.min_soc,
self.veh.min_soc,
),
Expand Down Expand Up @@ -1488,7 +1471,7 @@ impl RustSimDrive {
min(
self.cur_max_roadway_chg_kw[i],
min(
self.cur_max_mech_mc_kw_in[i],
self.ess_lim_mc_regen_kw[i],
self.ess_kw_if_fc_req[i] - self.mc_elec_in_lim_kw[i] + self.aux_in_kw[i],
),
),
Expand Down Expand Up @@ -1557,16 +1540,16 @@ impl RustSimDrive {
if self.veh.fc_eff_type != H2FC && self.veh.fc_max_kw > 0.0 {
if self.can_pwr_all_elec[i] {
self.mc_mech_kw_out_ach[i] =
-min(self.cur_max_mech_mc_kw_in[i], -self.trans_kw_in_ach[i]);
-min(self.ess_lim_mc_regen_kw[i], -self.trans_kw_in_ach[i]);
} else {
self.mc_mech_kw_out_ach[i] = min(
-min(self.cur_max_mech_mc_kw_in[i], -self.trans_kw_in_ach[i]),
-min(self.ess_lim_mc_regen_kw[i], -self.trans_kw_in_ach[i]),
max(-self.cur_max_fc_kw_out[i], self.mc_kw_if_fc_req[i]),
);
}
} else {
self.mc_mech_kw_out_ach[i] = min(
-min(self.cur_max_mech_mc_kw_in[i], -self.trans_kw_in_ach[i]),
-min(self.ess_lim_mc_regen_kw[i], -self.trans_kw_in_ach[i]),
-self.trans_kw_in_ach[i],
);
}
Expand Down
14 changes: 6 additions & 8 deletions rust/fastsim-core/src/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ lazy_static! {
self.set_veh_mass()
}
pub fn get_max_regen_kwh(&self) -> f64 {
self.max_regen_kwh()
}
#[getter]
pub fn get_mc_peak_eff(&self) -> f64 {
self.mc_peak_eff()
Expand Down Expand Up @@ -541,6 +537,10 @@ pub struct RustVehicle {
#[doc(hidden)]
#[doc_field(skip_doc)]
pub orphaned: bool,
#[serde(skip)]
#[doc(hidden)]
#[doc_field(skip_doc)]
pub max_regen_kwh: f64,
}

/// RustVehicle rust methods
Expand Down Expand Up @@ -606,10 +606,6 @@ impl RustVehicle {
0.99
}

pub fn max_regen_kwh(&self) -> f64 {
0.5 * self.veh_kg * (27.0 * 27.0) / (3_600.0 * 1_000.0)
}

pub fn mc_peak_eff(&self) -> f64 {
arrmax(&self.mc_full_eff_array)
}
Expand Down Expand Up @@ -850,6 +846,8 @@ impl RustVehicle {

self.set_veh_mass();

self.max_regen_kwh = 0.5 * self.veh_kg * (27.0 * 27.0) / (3_600.0 * 1_000.0);

Ok(())
}

Expand Down

0 comments on commit c4fba56

Please sign in to comment.