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

adding new from_github vehicle method #86

Merged
merged 28 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fc74da8
adding new from_github vehicle method
Dec 19, 2023
7f2d22f
merging in create_project_subdir function
robinsteuteville Dec 21, 2023
5d2c1d4
adding from_url and to_cache methods to SerdeAPI
robinsteuteville Dec 21, 2023
84754c3
fixing caching functions
robinsteuteville Dec 25, 2023
c81eca6
fixing from_url function
robinsteuteville Dec 26, 2023
f68d055
updating from_url and from_str
robinsteuteville Dec 26, 2023
8d556f3
adding from_github_or_url method
robinsteuteville Jan 2, 2024
f6896f6
updating error messages
robinsteuteville Jan 3, 2024
1ff4316
merge fastsim-2 into vehicle-fetching-caching
Jan 3, 2024
474df42
adding fetch_github_list function
robinsteuteville Jan 3, 2024
ea631fd
updating fetch_github_list
robinsteuteville Jan 3, 2024
5275708
adding caching of vehicle if not downloaded to from_github_or_url
robinsteuteville Jan 4, 2024
24a11ac
updating from_github_or_url doc string
robinsteuteville Jan 4, 2024
7e9a51c
updating doc strings
robinsteuteville Jan 4, 2024
05fea86
adding temporary tests/file to support the tests
robinsteuteville Jan 5, 2024
da1f8a4
updating test to produce test file for comparison
robinsteuteville Jan 5, 2024
1301482
troubleshooting fetch_github_list
robinsteuteville Jan 6, 2024
f6c607f
pulling fastsim-2 updates to get tests to pass
robinsteuteville Jan 10, 2024
d729966
updating fetch_github_list
robinsteuteville Jan 18, 2024
364395a
fixing looping problem with fetch_github_list
robinsteuteville Jan 18, 2024
49ba054
fixing fetch_github_list test so it runs
robinsteuteville Jan 18, 2024
1f9860e
adding path_to_cache and from_cache
robinsteuteville Jan 19, 2024
6086641
ading clear cache function
robinsteuteville Jan 23, 2024
4c4f1d9
updating tests so they pass
robinsteuteville Jan 23, 2024
512284a
adding 403 error to retry errors for fetch_github_list
robinsteuteville Jan 23, 2024
7960663
deleted deprecated comment
calbaker Feb 6, 2024
4fb10f3
minor tweaks
calbaker Feb 6, 2024
867f72f
reorganize reference vehicle for github caching
Feb 6, 2024
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
3 changes: 3 additions & 0 deletions rust/fastsim-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ include_dir = "0.7.3"
itertools = "0.12.0"
ndarray-stats = "0.5.1"
tempfile = "3.8.1"
url = "2.5.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylecarow, could you feature gate this stuff?

ureq = "2.9.1"
isahc = "1.7.2"

[package.metadata]
include = [
Expand Down
226 changes: 226 additions & 0 deletions rust/fastsim-core/resources/1110_2022_Tesla_Model_Y_RWD_opt45017.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
---
scenario_name: 2022 Tesla Model Y RWD
veh_year: 2022
veh_pt_type: BEV
drag_coef: 0.21716617209403616
frontal_area_m2: 3.121538772737215
glider_kg: 306.4316324129186
veh_cg_m: -0.53
drive_axle_weight_frac: 0.59
wheel_base_m: 2.6
cargo_kg: 136.0
veh_override_kg: ~
comp_mass_multiplier: 1.4
fs_max_kw: 0.0
fs_secs_to_peak_pwr: 1.0
fs_kwh: 0.0
fs_kwh_per_kg: 9.89
fc_max_kw: 0.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.28
- 0.35
- 0.38
- 0.39
- 0.4
- 0.4
- 0.38
- 0.37
- 0.36
- 0.35
fc_eff_type: SI
fc_sec_to_peak_pwr: 6.0
fc_base_kg: 61.0
fc_kw_per_kg: 2.13
min_fc_time_on: 30.0
idle_fc_kw: 0.0
mc_max_kw: 208.79628939165707
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.41
- 0.45
- 0.48
- 0.54
- 0.58
- 0.62
- 0.83
- 0.93
- 0.94
- 0.93
- 0.92
mc_sec_to_peak_pwr: 4.0
mc_pe_kg_per_kw: 0.833
mc_pe_base_kg: 21.6
ess_max_kw: 219.23610386123994
ess_max_kwh: 81.0
ess_kg_per_kwh: 8.0
ess_base_kg: 75.0
ess_round_trip_eff: 0.97
ess_life_coef_a: 110.0
ess_life_coef_b: -0.6811
min_soc: 0.05
max_soc: 0.98
ess_dischg_to_fc_max_eff_perc: 0.0
ess_chg_to_fc_max_eff_perc: 0.0
wheel_inertia_kg_m2: 0.815
num_wheels: 4.0
wheel_rr_coef: 0.007752538840328322
wheel_radius_m: 0.336
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: 1.0
kw_demand_fc_on: 100.0
max_regen: 0.98
stop_start: false
force_aux_on_fc: false
alt_eff: 1.0
chg_eff: 0.86
aux_kw: 0.25
trans_kg: 114.0
trans_eff: 0.98
ess_to_fuel_ok_error: 0.005
fc_eff_array:
- 0.1
- 0.10400000000000001
- 0.108
- 0.112
- 0.11599999999999999
- 0.12
- 0.124
- 0.128
- 0.132
- 0.136
- 0.14
- 0.14400000000000002
- 0.14800000000000002
- 0.15200000000000002
- 0.15600000000000003
- 0.16
- 0.16240000000000002
- 0.1648
- 0.16720000000000002
- 0.1696
- 0.17200000000000001
- 0.1744
- 0.1768
- 0.1792
- 0.1816
- 0.184
- 0.1864
- 0.1888
- 0.1912
- 0.1936
- 0.196
- 0.20800000000000002
- 0.22
- 0.23500000000000001
- 0.25
- 0.265
- 0.28
- 0.28625
- 0.29250000000000004
- 0.30500000000000005
- 0.3175
- 0.33
- 0.335
- 0.33999999999999997
- 0.345
- 0.35
- 0.3516666666666666
- 0.35333333333333333
- 0.355
- 0.35666666666666663
- 0.35833333333333334
- 0.36
- 0.3595
- 0.359
- 0.3585
- 0.358
- 0.3575
- 0.357
- 0.3565
- 0.356
- 0.3555
- 0.355
- 0.3545
- 0.354
- 0.3535
- 0.353
- 0.3525
- 0.352
- 0.3515
- 0.351
- 0.3505
- 0.35
- 0.3495
- 0.349
- 0.3485
- 0.348
- 0.3475
- 0.347
- 0.3465
- 0.346
- 0.3455
- 0.345
- 0.34450000000000003
- 0.34400000000000003
- 0.3435
- 0.343
- 0.3425
- 0.342
- 0.3415
- 0.341
- 0.3405
- 0.34
- 0.335
- 0.33
- 0.325
- 0.32
- 0.315
- 0.31
- 0.305
- 0.3
veh_kg: 1887.969865101469
2 changes: 2 additions & 0 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ pub struct RustCycle {
impl SerdeAPI for RustCycle {
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = &["yaml", "json", "bin", "csv"];
const ACCEPTED_STR_FORMATS: &'static [&'static str] = &["yaml", "json", "csv"];
// is this enough, or do I have to copy paste in the whole to_cache mathod?
calbaker marked this conversation as resolved.
Show resolved Hide resolved
const CACHE_FOLDER: &'static str = &"cycles";

fn init(&mut self) -> anyhow::Result<()> {
ensure!(!self.is_empty(), "Deserialized cycle is empty");
Expand Down
1 change: 1 addition & 0 deletions rust/fastsim-core/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pub(crate) use std::path::PathBuf;

pub(crate) use crate::traits::*;
pub(crate) use crate::utils::*;
pub(crate) use crate::vehicle_utils::*;
118 changes: 118 additions & 0 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::imports::*;
use std::collections::HashMap;
use ureq;

pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = &["yaml", "json", "bin"];
const ACCEPTED_STR_FORMATS: &'static [&'static str] = &["yaml", "json"];
const CACHE_FOLDER: &'static str = &"";

/// Specialized code to execute upon initialization
fn init(&mut self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -185,6 +187,84 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
bincode_de.init()?;
Ok(bincode_de)
}

/// instantiates an object from a url
/// accepts yaml and json file types
/// # Arguments
/// - url: URL (either as a string or url type) to object
/// Note: The URL needs to be a URL pointing directly to a file, for example
/// a raw github URL.
fn from_url<S: AsRef<str>>(url: S) -> anyhow::Result<Self> {
let url = url::Url::parse(url.as_ref())?;
let format = url
.path_segments()
.and_then(|segments| segments.last())
.and_then(|filename| Path::new(filename).extension())
.and_then(OsStr::to_str)
.with_context(|| "Could not parse file format from URL: {url:?}")?;
let response = ureq::get(url.as_ref()).call()?.into_reader();
Self::from_reader(response, format)
}

/// takes an instantiated Rust object and saves it in the FASTSim data directory in
/// a rust_objects folder
/// WARNING: If there is a file already in the data subdirectory with the
/// same name, it will be replaced by the new file.
/// # Arguments
/// - self (rust object)
/// - file_path: path to file within subdirectory. If only the file name is
/// listed, file will sit directly within the subdirectory of
/// the FASTSim data directory. If a path is given, the file will live
/// within the path specified, within the subdirectory CACHE_FOLDER of the
/// FASTSim data directory.
fn to_cache<P: AsRef<Path>>(&self, file_path: P) -> anyhow::Result<()> {
let file_name = file_path
.as_ref()
.file_name()
.with_context(|| "Could not determine file name")?
.to_str()
.context("Could not determine file name.")?;
let mut subpath = PathBuf::new();
let file_path_internal = file_path
.as_ref()
.to_str()
.context("Could not determine file name.")?;
if file_name == file_path_internal {
subpath = PathBuf::from(Self::CACHE_FOLDER);
} else {
subpath = Path::new(Self::CACHE_FOLDER).join(
file_path_internal
.strip_suffix(file_name)
.context("Could not determine path to subdirectory.")?,
);
}
let data_subdirectory = create_project_subdir(subpath)
.with_context(|| "Could not find or build Fastsim data subdirectory.")?;
let file_path = data_subdirectory.join(file_name);
self.to_file(file_path)
}

/// Instantiates a Rust object from the subdirectory within the FASTSim data
/// directory corresponding to the Rust Object ("vehices" for a RustVehice,
/// "cycles" for a RustCycle, and the root folder of the data directory for
/// all other objects).
/// # Arguments
/// - file_path: subpath to object, including file name, within subdirectory.
/// If the file sits directly in the subdirectory, this will just be the
/// file name.
/// Note: This function will work for all objects cached using the
/// to_cache() method. If a file has been saved manually to a different
/// subdirectory than the correct one for the object type (for instance a
/// RustVehicle saved within a subdirectory other than "vehicles" using the
/// utils::url_to_cache() function), then from_cache() will not be able to
/// find and instantiate the object. Instead, use the from_file method, and
/// use the utils::path_to_cache() to find the FASTSim data directory
/// location if needed.
fn from_cache<P: AsRef<Path>>(file_path: P) -> anyhow::Result<Self> {
let full_file_path = Path::new(Self::CACHE_FOLDER).join(file_path);
let path_including_directory = path_to_cache()?.join(full_file_path);
Self::from_file(path_including_directory)
}
}

pub trait ApproxEq<Rhs = Self> {
Expand Down Expand Up @@ -274,3 +354,41 @@ where
.all(|(key, value)| other.get(key).map_or(false, |v| value.approx_eq(v, tol)));
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_cache() {
let test_path = "1110_2022_Tesla_Model_Y_RWD_opt45017_from_cache.yaml";
let comparison_vehicle =
crate::vehicle::RustVehicle::from_resource("1110_2022_Tesla_Model_Y_RWD_opt45017.yaml")
.unwrap();
crate::vehicle::RustVehicle::to_cache(&comparison_vehicle, test_path).unwrap();
let vehicle = crate::vehicle::RustVehicle::from_cache(test_path).unwrap();
assert_eq!(comparison_vehicle, vehicle);
let full_file_path = Path::new("vehicles").join(test_path);
let path_including_directory = path_to_cache().unwrap().join(full_file_path);
std::fs::remove_file(path_including_directory).unwrap();
}

#[test]
fn test_to_cache() {
let comparison_vehicle =
crate::vehicle::RustVehicle::from_resource("1110_2022_Tesla_Model_Y_RWD_opt45017.yaml")
.unwrap();
crate::vehicle::RustVehicle::to_cache(
&comparison_vehicle,
"1110_2022_Tesla_Model_Y_RWD_opt45017.yaml",
)
.unwrap();
let data_subdirectory = create_project_subdir("vehicles").unwrap();
let file_path = data_subdirectory.join("1110_2022_Tesla_Model_Y_RWD_opt45017.yaml");
println!("{}", file_path.to_string_lossy());
println!("{}", crate::vehicle::RustVehicle::CACHE_FOLDER);
let vehicle_b = crate::vehicle::RustVehicle::from_file(&file_path).unwrap();
assert_eq!(comparison_vehicle, vehicle_b);
std::fs::remove_file(file_path).unwrap();
}
}
Loading
Loading