Skip to content

Commit

Permalink
Merge pull request #156 from NREL/f2/vehicle-in-resources
Browse files Browse the repository at this point in the history
added a single vehicle to the `from_resources`
  • Loading branch information
calbaker authored Sep 20, 2024
2 parents 82e5b8a + 0577b9b commit 8359f14
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 57 deletions.
4 changes: 4 additions & 0 deletions python/fastsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def package_root() -> Path:
"""Returns the package root directory."""
return Path(__file__).parent

def resources_root() -> Path:
"""Returns the resources root directory."""
return Path(__file__).parent / "resources"


DEFAULT_LOGGING_CONFIG = dict(
format = "%(asctime)s.%(msecs)03d | %(filename)s:%(lineno)s | %(levelname)s: %(message)s",
Expand Down
8 changes: 1 addition & 7 deletions python/fastsim/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ def test_list_resources_for_cycle(self):

def test_list_resources_for_vehicles(self):
"check if list_resources works for RustVehicle"
# NOTE: at the time of writing this test,
# there are no vehicle assets in resources.
# Therefore, we expect to get an empty vector.
# If resources are committed, this test should
# fail and we should use the following assert:
# self.assertTrue(len(resources) > 0)
rv = vehicle.Vehicle.from_vehdb(1).to_rust()
resources = rv.list_resources()
self.assertTrue(len(resources) == 0)
self.assertTrue(len(resources) == 1)
6 changes: 3 additions & 3 deletions rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ pub fn calculate_mpgge_for_h2_diesel_ice(
dist_mi: f64,
max_fc_power_kw: f64,
kwh_per_gge: f64,
fc_kw_out_ach: &Vec<f64>,
fs_kwh_out_ach: &Vec<f64>,
fc_kw_out_ach: &[f64],
fs_kwh_out_ach: &[f64],
fc_pwr_out_perc: &Vec<f64>,
h2share: &Vec<f64>,
) -> anyhow::Result<H2AndDieselResults> {
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn calculate_mpgge_for_h2_diesel_ice(
})
}

pub fn integrate_power_to_kwh(dts_s: &Vec<f64>, ps_kw: &Vec<f64>) -> anyhow::Result<Vec<f64>> {
pub fn integrate_power_to_kwh(dts_s: &[f64], ps_kw: &[f64]) -> anyhow::Result<Vec<f64>> {
anyhow::ensure!(dts_s.len() == ps_kw.len());
let mut energy_kwh = Vec::<f64>::with_capacity(dts_s.len());
for idx in 0..dts_s.len() {
Expand Down
14 changes: 14 additions & 0 deletions rust/fastsim-core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ fn main() {
return;
}

check_files_consistent(&prepath);
}

/// Checks if rust and python resource files are consistent
fn check_files_consistent(prepath: &String) {
let truth_files = [
format!(
"{}/{}/vehdb/2017_Toyota_Highlander_3.5_L.yaml",
env::current_dir().unwrap().as_os_str().to_str().unwrap(),
prepath
),
format!(
"{}/{}/longparams.json",
env::current_dir().unwrap().as_os_str().to_str().unwrap(),
Expand All @@ -42,6 +52,10 @@ fn main() {
];

let compare_files = [
format!(
"{}/resources/vehicles/2017_Toyota_Highlander_3.5_L.yaml",
env::current_dir().unwrap().as_os_str().to_str().unwrap()
),
format!(
"{}/resources/longparams.json",
env::current_dir().unwrap().as_os_str().to_str().unwrap()
Expand Down
168 changes: 168 additions & 0 deletions rust/fastsim-core/resources/vehicles/2017_Toyota_Highlander_3.5_L.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
scenario_name: 2017 Toyota Highlander 3.5 L
selection: 0
veh_year: 2017
veh_pt_type: Conv
drag_coef: 0.1966
frontal_area_m2: 6.0
glider_kg: 791.0702892
veh_cg_m: 0.53
drive_axle_weight_frac: 0.59
wheel_base_m: 2.72
cargo_kg: 136.0
veh_override_kg: 2270.0
comp_mass_multiplier: 1.4
fs_max_kw: 1000.0
fs_secs_to_peak_pwr: 1.0
fs_kwh: 590.0
fs_kwh_per_kg: 9.89473291
fc_max_kw: 220.0
fc_pwr_out_perc:
v: 1
dim:
- 12
data:
- 0.0
- 0.005
- 0.015
- 0.04
- 0.06
- 0.1
- 0.14
- 0.2
- 0.4
- 0.6
- 0.8
- 1.0
fc_eff_map:
v: 1
dim:
- 12
data:
- 0.1
- 0.12
- 0.16
- 0.22
- 0.28
- 0.33
- 0.35
- 0.36
- 0.35
- 0.34
- 0.32
- 0.3
fc_eff_type: SI
fc_sec_to_peak_pwr: 6.0
fc_base_kg: 61.0
fc_kw_per_kg: 0.47
min_fc_time_on: 30.0
idle_fc_kw: 0.0
mc_max_kw: 0.0
mc_pwr_out_perc:
v: 1
dim:
- 11
data:
- 0.0
- 0.02
- 0.04
- 0.06
- 0.08
- 0.1
- 0.2
- 0.4
- 0.6
- 0.8
- 1.0
mc_eff_map:
v: 1
dim:
- 11
data:
- 0.12
- 0.16
- 0.21
- 0.29
- 0.35
- 0.42
- 0.75
- 0.92
- 0.93
- 0.93
- 0.92
mc_sec_to_peak_pwr: 5.0
mc_pe_kg_per_kw: 0.83
mc_pe_base_kg: 21.6
ess_max_kw: 0.0
ess_max_kwh: 0.0
ess_kg_per_kwh: 44.0
ess_base_kg: 0.0
ess_round_trip_eff: 0.834
ess_life_coef_a: 110.0
ess_life_coef_b: -0.68
min_soc: 0.4
max_soc: 0.8
ess_dischg_to_fc_max_eff_perc: 0.0
ess_chg_to_fc_max_eff_perc: 0.0
wheel_inertia_kg_m2: 0.82
num_wheels: 4.0
wheel_rr_coef: 0.009449
wheel_radius_m: 0.326
wheel_coef_of_fric: 0.7
max_accel_buffer_mph: 60.0
max_accel_buffer_perc_of_useable_soc: 0.2
perc_high_acc_buf: 0.0
mph_fc_on: 47.0
kw_demand_fc_on: 100.0
max_regen: 0.9
stop_start: false
force_aux_on_fc: false
alt_eff: 1.0
chg_eff: 0.86
aux_kw: 0.7
trans_kg: 114.0
trans_eff: 0.875
ess_to_fuel_ok_error: 0.005
small_motor_power_kw: 7.5
large_motor_power_kw: 75.0
regen_a: 500.0
regen_b: 0.99
charging_on: false
max_roadway_chg_kw:
v: 1
dim:
- 6
data:
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
- 0.0
modern_max: 0.95
veh_kg: 2270.0
max_trac_mps2: 3.5652415399547075
ess_mass_kg: 0.0
mc_mass_kg: 0.0
fc_mass_kg: 0.0
fs_mass_kg: 0.0
val_udds_mpgge: 22.0
val_hwy_mpgge: 32.0
val_comb_mpgge: 25.6
val_udds_kwh_per_mile: .nan
val_hwy_kwh_per_mile: .nan
val_comb_kwh_per_mile: .nan
val_cd_range_mi: .nan
val_const65_mph_kwh_per_mile: .nan
val_const60_mph_kwh_per_mile: .nan
val_const55_mph_kwh_per_mile: .nan
val_const45_mph_kwh_per_mile: .nan
val_unadj_udds_kwh_per_mile: .nan
val_unadj_hwy_kwh_per_mile: .nan
val0_to60_mph: .nan
val_ess_life_miles: .nan
val_range_miles: .nan
val_veh_base_cost: .nan
val_msrp: .nan
fc_peak_eff_override: ~
mc_peak_eff_override: ~
1 change: 0 additions & 1 deletion rust/fastsim-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub mod params;
pub mod pyo3imports;
pub mod simdrive;
pub use simdrive::simdrive_impl;
pub mod resources;
pub mod simdrivelabel;
pub mod thermal;
pub mod traits;
Expand Down
41 changes: 0 additions & 41 deletions rust/fastsim-core/src/resources.rs

This file was deleted.

1 change: 0 additions & 1 deletion rust/fastsim-core/src/simdrive/simdrive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,6 @@ impl RustSimDrive {
(self.dist_m.sum() - dist_m).abs() / dist_m
} else {
bail!("Vehicle did not move forward.");
0.0
};
self.trace_miss_time_frac = (self
.cyc
Expand Down
48 changes: 44 additions & 4 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::{imports::*, resources};
use crate::imports::*;
use include_dir::{include_dir, Dir};
use std::collections::HashMap;
use std::path::PathBuf;
use ureq;

#[cfg(feature = "resources")]
pub const RESOURCES_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/resources");

pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = &["yaml", "json", "toml", "bin"];
const ACCEPTED_STR_FORMATS: &'static [&'static str] = &["yaml", "json", "toml"];
Expand All @@ -14,11 +19,25 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
}

/// List available (compiled) resources (stored in the rust binary)
/// RESULT:
/// # RESULT
/// vector of string of resource names that can be loaded
#[cfg(feature = "resources")]
fn list_resources() -> Vec<String> {
resources::list_resources(Self::RESOURCE_PREFIX)
if Self::RESOURCE_PREFIX.is_empty() {
Vec::<String>::new()
} else if let Some(resources_path) = RESOURCES_DIR.get_dir(Self::RESOURCE_PREFIX) {
let mut file_names: Vec<String> = resources_path
.files()
.filter_map(|entry| entry.path().file_name()?.to_str().map(String::from))
.collect();
file_names.retain(|f| {
Self::ACCEPTED_STR_FORMATS.contains(&f.split(".").last().unwrap_or_default())
});
file_names.sort();
file_names
} else {
Vec::<String>::new()
}
}

/// Read (deserialize) an object from a resource file packaged with the `fastsim-core` crate
Expand All @@ -33,7 +52,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
.extension()
.and_then(OsStr::to_str)
.with_context(|| format!("File extension could not be parsed: {filepath:?}"))?;
let file = crate::resources::RESOURCES_DIR
let file = RESOURCES_DIR
.get_file(&filepath)
.with_context(|| format!("File not found in resources: {filepath:?}"))?;
Self::from_reader(file.contents(), extension, skip_init)
Expand Down Expand Up @@ -434,3 +453,24 @@ impl IterMaxMin<f64> for Array1<f64> {
})
}
}

#[cfg(test)]
mod tests {
use crate::imports::SerdeAPI;

#[test]
#[cfg(feature = "resources")]
fn test_list_resources() {
let cyc_resource_list = crate::cycle::RustCycle::list_resources();
assert!(cyc_resource_list.len() == 3);
assert!(cyc_resource_list[0] == "HHDDTCruiseSmooth.csv");
// NOTE: at the time of writing this test, there is no
// vehicles subdirectory. The agreed-upon behavior in
// that case is that list_resources should return an
// empty vector of string.
let veh_resource_list = crate::vehicle::RustVehicle::list_resources();
println!("{:?}", veh_resource_list);
assert!(veh_resource_list.len() == 1);
assert!(veh_resource_list[0] == "2017_Toyota_Highlander_3.5_L.yaml")
}
}

0 comments on commit 8359f14

Please sign in to comment.