Skip to content
Open
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
65 changes: 52 additions & 13 deletions fastsim-core/src/simdrivelabel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
// crate local
use crate::drive_cycle::{Cycle, CYC_ACCEL};
use crate::imports::*;
use crate::simdrive::SimDrive;
use crate::simdrive::{params::SimParams, SimDrive};
use crate::vehicle::{PowertrainType, Vehicle};

/// Return first index of `arr` greater than `cut`
Expand Down Expand Up @@ -54,8 +54,15 @@ pub fn get_0_to_60_time_from_accel_data(accel_data: &AccelData) -> anyhow::Resul
}

/// Run the acceleration test and return the time/speed trace.
pub fn run_accel(veh: &Vehicle) -> anyhow::Result<AccelData> {
let mut sd_accel = SimDrive::new(veh.clone(), CYC_ACCEL.clone(), None);
pub fn run_accel(
veh: &Vehicle,
sim_params: &HashMap<&'static str, SimParams>,
) -> anyhow::Result<AccelData> {
let mut sd_accel = SimDrive::new(
veh.clone(),
CYC_ACCEL.clone(),
sim_params.get("accel").cloned(),
);
sd_accel.sim_params.trace_miss_opts = TraceMissOptions::Allow;
sd_accel.walk_once().map_err(|e| {
anyhow!(
Expand Down Expand Up @@ -1005,7 +1012,8 @@ pub fn run_label_simulations(
// max_epa_adj: Option<f64>,
fuel_props: Option<FuelProperties>,
phev_utilization_params: Option<PhevUtilizationParams>,
) -> anyhow::Result<(SimulationDataForLabel, HashMap<&str, SimDrive>)> {
sim_params: &HashMap<&'static str, SimParams>,
) -> anyhow::Result<(SimulationDataForLabel, HashMap<&'static str, SimDrive>)> {
// let max_epa_adj = max_epa_adj.unwrap_or(0.3);
let phev_utilization_params = &phev_utilization_params.unwrap_or_default();
let fuel_props = fuel_props.unwrap_or_default();
Expand All @@ -1030,9 +1038,20 @@ pub fn run_label_simulations(
// run simdrive for non-phev powertrains
sd.insert(
"udds",
SimDrive::new(veh.clone(), cyc["udds"].clone(), None),
SimDrive::new(
veh.clone(),
cyc["udds"].clone(),
sim_params.get("udds").cloned(),
),
);
sd.insert(
"hwy",
SimDrive::new(
veh.clone(),
cyc["hwy"].clone(),
sim_params.get("hwy").cloned(),
),
);
sd.insert("hwy", SimDrive::new(veh.clone(), cyc["hwy"].clone(), None));

for (k, val) in sd.iter_mut() {
val.walk().map_err(|e| {
Expand Down Expand Up @@ -1311,19 +1330,23 @@ pub fn get_label_fe(
full_detail: bool,
fuel_props: Option<FuelProperties>,
phev_utilization_params: Option<PhevUtilizationParams>,
sim_params: Option<HashMap<&'static str, SimParams>>,
verbose: bool,
) -> anyhow::Result<(LabelFe, Option<HashMap<&str, SimDrive>>)> {
) -> anyhow::Result<(LabelFe, Option<HashMap<&'static str, SimDrive>>)> {
let max_epa_adj = max_epa_adj.unwrap_or(0.3);
let phev_utilization_params = &phev_utilization_params.unwrap_or_default();
let fuel_props = fuel_props.unwrap_or_default();
let veh_copy = veh.clone();

let sim_params = sim_params.unwrap_or_default();

let (sim_data, sd) = run_label_simulations(
veh,
Some(fuel_props.clone()),
Some(phev_utilization_params.clone()),
&sim_params,
)?;
let accel_data = run_accel(&veh_copy)?;
let accel_data = run_accel(&veh_copy, &sim_params)?;
let mut label_fe = calculate_label_fuel_economy(
&fuel_props,
phev_utilization_params,
Expand Down Expand Up @@ -1351,7 +1374,7 @@ pub fn get_label_fe(
#[cfg_attr(
feature = "pyo3",
pyo3(signature = (
veh, max_epa_adj=None, full_detail=None, fuel_props=None, phev_utilization_params=None, verbose=None))
veh, max_epa_adj=None, full_detail=None, fuel_props=None, phev_utilization_params=None, sim_params=None, verbose=None))
)]
/// pyo3 version of [get_label_fe]
pub fn get_label_fe_py(
Expand All @@ -1360,6 +1383,7 @@ pub fn get_label_fe_py(
full_detail: Option<bool>,
fuel_props: Option<FuelProperties>,
phev_utilization_params: Option<PhevUtilizationParams>,
sim_params: Option<HashMap<String, SimParams>>,
verbose: Option<bool>,
) -> anyhow::Result<LabelFe> {
let (label_fe, _) = get_label_fe(
Expand All @@ -1368,6 +1392,21 @@ pub fn get_label_fe_py(
full_detail.unwrap_or_default(),
fuel_props,
phev_utilization_params,
sim_params
.map(|m| {
m.into_iter()
.map(|(k, v)| -> anyhow::Result<(&'static str, SimParams)> {
let key = match k.as_str() {
"udds" => "udds",
"hwy" => "hwy",
"accel" => "accel",
other => bail!("Unknown sim_params key: {other:?}"),
};
Ok((key, v))
})
.collect()
})
.transpose()?,
verbose.unwrap_or_default(),
)?;
Ok(label_fe)
Expand Down Expand Up @@ -2027,7 +2066,7 @@ mod tests {
let mut veh = Vehicle::try_from(f2veh.clone()).unwrap();

// Get FASTSim-3 label FE results
let (label_fe_f3, _) = get_label_fe(&mut veh, None, false, None, None, false)
let (label_fe_f3, _) = get_label_fe(&mut veh, None, false, None, None, None, false)
.with_context(|| format_dbg!())
.unwrap();

Expand Down Expand Up @@ -2062,7 +2101,7 @@ mod tests {
let mut veh = Vehicle::try_from(f2veh.clone()).unwrap();

// Get FASTSim-3 label FE results
let (label_fe_f3, _) = get_label_fe(&mut veh, None, false, None, None, false)
let (label_fe_f3, _) = get_label_fe(&mut veh, None, false, None, None, None, false)
.with_context(|| format_dbg!())
.unwrap();

Expand Down Expand Up @@ -2096,7 +2135,7 @@ mod tests {
let mut veh = Vehicle::try_from(f2veh.clone()).unwrap();

// Get FASTSim-3 label FE results
let (label_fe_f3, _) = get_label_fe(&mut veh, None, false, None, None, false)
let (label_fe_f3, _) = get_label_fe(&mut veh, None, false, None, None, None, false)
.with_context(|| format_dbg!())
.unwrap();

Expand Down Expand Up @@ -2153,7 +2192,7 @@ mod tests {
);

// Get FASTSim-3 label FE results (if PHEV functionality is implemented)
let label_fe_f3 = get_label_fe(&mut veh, None, false, None, None, false)
let label_fe_f3 = get_label_fe(&mut veh, None, false, None, None, None, false)
.unwrap()
.0;

Expand Down
Loading