Skip to content

Commit

Permalink
Merge pull request #117 from NREL/f2/resource-prefix
Browse files Browse the repository at this point in the history
Create implied prefix for RustCycle and RustVehicle resources
  • Loading branch information
calbaker authored May 14, 2024
2 parents 8bd2923 + 9a4adee commit cecb74e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion rust/fastsim-cli/src/bin/fastsim-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub fn main() -> anyhow::Result<()> {
let cyc = if adopt_hd_has_cycle {
cyc
} else {
RustCycle::from_resource("cycles/HHDDTCruiseSmooth.csv", false)?
RustCycle::from_resource("HHDDTCruiseSmooth.csv", false)?
};
let mut sim_drive = RustSimDrive::new(cyc, veh.clone());
sim_drive.sim_drive(None, None)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
/// # Arguments:
///
/// * `filepath`: `str | pathlib.Path` - Filepath, relative to the top of the `resources` folder, from which to read the object
/// * `filepath`: `str | pathlib.Path` - Filepath, relative to the top of the `resources` folder (excluding any relevant prefix), from which to read the object
///
#[cfg(feature = "resources")]
#[staticmethod]
Expand Down
3 changes: 2 additions & 1 deletion rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ pub struct RustCycle {
impl SerdeAPI for RustCycle {
const ACCEPTED_BYTE_FORMATS: &'static [&'static str] = &["yaml", "json", "toml", "bin", "csv"];
const ACCEPTED_STR_FORMATS: &'static [&'static str] = &["yaml", "json", "toml", "csv"];
const CACHE_FOLDER: &'static str = &"cycles";
const RESOURCE_PREFIX: &'static str = "cycles";
const CACHE_FOLDER: &'static str = "cycles";

fn init(&mut self) -> anyhow::Result<()> {
self.init_checks()
Expand Down
4 changes: 2 additions & 2 deletions rust/fastsim-core/src/simdrivelabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ pub fn get_label_fe(
// load the cycles and intstantiate simdrive objects
cyc.insert("accel", make_accel_trace());

cyc.insert("udds", RustCycle::from_resource("cycles/udds.csv", false)?);
cyc.insert("hwy", RustCycle::from_resource("cycles/hwfet.csv", false)?);
cyc.insert("udds", RustCycle::from_resource("udds.csv", false)?);
cyc.insert("hwy", RustCycle::from_resource("hwfet.csv", false)?);

// run simdrive for non-phev powertrains
sd.insert("udds", RustSimDrive::new(cyc["udds"].clone(), veh.clone()));
Expand Down
9 changes: 5 additions & 4 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use ureq;
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"];
const CACHE_FOLDER: &'static str = &"";
const RESOURCE_PREFIX: &'static str = "";
const CACHE_FOLDER: &'static str = "";

/// Specialized code to execute upon initialization
fn init(&mut self) -> anyhow::Result<()> {
Expand All @@ -16,16 +17,16 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
///
/// # Arguments:
///
/// * `filepath` - Filepath, relative to the top of the `resources` folder, from which to read the object
/// * `filepath` - Filepath, relative to the top of the `resources` folder (excluding any relevant prefix), from which to read the object
#[cfg(feature = "resources")]
fn from_resource<P: AsRef<Path>>(filepath: P, skip_init: bool) -> anyhow::Result<Self> {
let filepath = filepath.as_ref();
let filepath = Path::new(Self::RESOURCE_PREFIX).join(filepath);
let extension = filepath
.extension()
.and_then(OsStr::to_str)
.with_context(|| format!("File extension could not be parsed: {filepath:?}"))?;
let file = crate::resources::RESOURCES_DIR
.get_file(filepath)
.get_file(&filepath)
.with_context(|| format!("File not found in resources: {filepath:?}"))?;
Self::from_reader(file.contents(), extension, skip_init)
}
Expand Down
3 changes: 2 additions & 1 deletion rust/fastsim-core/src/vehicle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,8 @@ impl Default for RustVehicle {
}

impl SerdeAPI for RustVehicle {
const CACHE_FOLDER: &'static str = &"vehicles";
const RESOURCE_PREFIX: &'static str = "vehicles";
const CACHE_FOLDER: &'static str = "vehicles";

fn init(&mut self) -> anyhow::Result<()> {
self.set_derived()
Expand Down

0 comments on commit cecb74e

Please sign in to comment.