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

F3/api handles option #153

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ opt-level = 0
opt-level = 3

[workspace.dependencies]
# TODO: uncomment and correct version `version = "~0.0.1"`
document-features = "0.2.10"
fastsim-core = { path = "fastsim-core" }
anyhow = "1.0.71"
Expand Down
9 changes: 3 additions & 6 deletions benchmarks/f3-save-int-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

@profile(precision=3)
def build_and_run_sim_drive():
veh = fsim.Vehicle.from_file(
# TODO: figure out why `str` is needed here
str(fsim.package_root() / "../../tests/assets/2012_Ford_Fusion.yaml")
)
veh = fsim.Vehicle.from_resource("2012_Ford_Fusion.yaml")
fsim.set_param_from_path(veh, "save_interval", 1)
cyc = fsim.Cycle.from_resource("udds.csv")
sd = fsim.SimDrive(veh, cyc)
Expand All @@ -23,8 +20,8 @@ def build_and_run_sim_drive():
# 5 61.562 MiB 61.562 MiB 1 @profile(precision=3)
# 6 def build_and_run_sim_drive():
# 7 62.125 MiB 0.562 MiB 2 veh = fsim.Vehicle.from_file(
# 8 # TODO: figure out why `str` is needed here
# 9 61.562 MiB 0.000 MiB 1 str(fsim.package_root() / "../../tests/assets/2012_Ford_Fusion.yaml")
# 8
# 9 61.562 MiB 0.000 MiB 1 fsim.package_root() / "../../tests/assets/2012_Ford_Fusion.yaml"
# 10 )
# 11 62.125 MiB 0.000 MiB 1 veh.save_interval = 1
# 12 62.312 MiB 0.188 MiB 1 cyc = fsim.Cycle.from_resource("udds.csv")
Expand Down
9 changes: 3 additions & 6 deletions benchmarks/f3-save-int-none.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

@profile(precision=3)
def build_and_run_sim_drive():
veh = fsim.Vehicle.from_file(
# TODO: figure out why `str` is needed here
str(fsim.package_root() / "../../tests/assets/2012_Ford_Fusion.yaml")
)
veh = fsim.Vehicle.from_resource("2012_Ford_Fusion.yaml")
fsim.set_param_from_path(veh, "save_interval", None)
cyc = fsim.Cycle.from_resource("udds.csv")
sd = fsim.SimDrive(veh, cyc)
Expand All @@ -23,8 +20,8 @@ def build_and_run_sim_drive():
# 5 61.203 MiB 61.203 MiB 1 @profile(precision=3)
# 6 def build_and_run_sim_drive():
# 7 61.766 MiB 0.562 MiB 2 veh = fsim.Vehicle.from_file(
# 8 # TODO: figure out why `str` is needed here
# 9 61.203 MiB 0.000 MiB 1 str(fsim.package_root() / "../../tests/assets/2012_Ford_Fusion.yaml")
# 8
# 9 61.203 MiB 0.000 MiB 1 fsim.package_root() / "../../tests/assets/2012_Ford_Fusion.yaml"
# 10 )
# 11 61.766 MiB 0.000 MiB 1 veh.save_interval = None
# 12 61.938 MiB 0.172 MiB 1 cyc = fsim.Cycle.from_resource("udds.csv")
Expand Down
2 changes: 0 additions & 2 deletions fastsim-core/fastsim-proc-macros/src/cumu_method_derive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::imports::*;

// TODO: make sure this macro is propagated and implementd everywhere it's needed

pub(crate) fn cumu_method_derive(input: TokenStream) -> TokenStream {
let item_struct = syn::parse_macro_input!(input as syn::ItemStruct);
let ident = &item_struct.ident;
Expand Down
2 changes: 1 addition & 1 deletion fastsim-core/fastsim-proc-macros/src/cycle_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn cycle_derive(input: TokenStream) -> TokenStream {
.unwrap();
generated.append_all(quote! {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[pyo3_api(
#[fastsim_api(
#[pyo3(name = "len")]
fn len_py(&self) -> usize {
self.len()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::imports::*;
mod pyo3_api_utils;
mod fastsim_api_utils;
use crate::utilities::parse_ts_as_fn_defs;
use pyo3_api_utils::*;
use fastsim_api_utils::*;

pub(crate) fn pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
pub(crate) fn fastsim_api(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut ast = syn::parse_macro_input!(item as syn::ItemStruct);
let ident = &ast.ident;
// println!("{}", String::from("*").repeat(30));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,34 @@ fn extract_type_path(ty: &syn::Type) -> Option<&syn::Path> {
}
}

#[allow(unused)]
/// adapted from https://stackoverflow.com/questions/55271857/how-can-i-get-the-t-from-an-optiont-when-using-syn
fn extract_type_from_option(ty: &syn::Type) -> Option<&syn::Type> {
use syn::{GenericArgument, Path, PathArguments, PathSegment};

// TODO store (with lazy static) the vec of string
// TODO maybe optimization, reverse the order of segments
fn extract_option_segment(path: &Path) -> Option<&PathSegment> {
let idents_of_path = path.segments.iter().fold(String::new(), |mut acc, v| {
acc.push_str(&v.ident.to_string());
acc.push('|');
acc
});
vec!["Option|", "std|option|Option|", "core|option|Option|"]
.into_iter()
.find(|s| idents_of_path == *s)
.and_then(|_| path.segments.last())
fn extract_option_argument(path: &Path) -> Option<&GenericArgument> {
let mut ident_path = String::new();
for segment in &path.segments {
ident_path.push_str(&segment.ident.to_string());

// Exit when the inner brackets are found
match &segment.arguments {
syn::PathArguments::AngleBracketed(params) => {
return match ident_path.as_str() {
"Option" | "std::option::Option" | "core::option::Option" => {
params.args.first()
}
_ => None,
};
}
syn::PathArguments::None => {}
_ => return None,
}

ident_path.push_str("::");
}
None
}

extract_type_path(ty)
.and_then(extract_option_segment)
.and_then(|path_seg| {
let type_params = &path_seg.arguments;
// It should have only on angle-bracketed param ("<String>"):
match *type_params {
PathArguments::AngleBracketed(ref params) => params.args.first(),
_ => None,
}
})
.and_then(extract_option_argument)
.and_then(|generic_arg| match *generic_arg {
GenericArgument::Type(ref ty) => Some(ty),
_ => None,
Expand Down Expand Up @@ -310,11 +309,9 @@ pub(crate) fn impl_getters_and_setters(
let mut vec_layers: u8 = 0;
let mut inner_type = &ftype;

// TODO: figure this out and uncomment. Then check that all `Option` fields are handled appropriately.
// pull out `inner_type` from `Option<inner_type>`
// if let Some(opt_inner_type) = extract_type_from_option(inner_type) {
// inner_type = opt_inner_type;
// }
if let Some(opt_inner_type) = extract_type_from_option(inner_type) {
inner_type = opt_inner_type;
}

// pull out `inner_type` from `Vec<inner_type>`, recursively if there is any nesting
while let Some(vec_inner_type) = extract_type_from_vec(inner_type) {
Expand Down Expand Up @@ -346,6 +343,7 @@ pub(crate) fn impl_getters_and_setters(
"Mass" => extract_units!(uom::si::mass::kilogram),
"MomentOfInertia" => extract_units!(uom::si::moment_of_inertia::kilogram_square_meter),
"Power" => extract_units!(uom::si::power::watt),
"SpecificPower" => extract_units!(uom::si::specific_power::watt_per_kilogram),
"PowerRate" => extract_units!(uom::si::power_rate::watt_per_second),
"Pressure" => extract_units!(uom::si::pressure::kilopascal, uom::si::pressure::bar),
"Ratio" => extract_units!(uom::si::ratio::ratio),
Expand Down
2 changes: 1 addition & 1 deletion fastsim-core/fastsim-proc-macros/src/history_vec_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn history_vec_derive(input: TokenStream) -> TokenStream {
.unwrap();
generated.append_all(quote! {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[pyo3_api(
#[fastsim_api(
#[pyo3(name = "len")]
fn len_py(&self) -> usize {
self.len()
Expand Down
4 changes: 3 additions & 1 deletion fastsim-core/fastsim-proc-macros/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ pub(crate) use proc_macro2::TokenStream as TokenStream2;
pub(crate) use proc_macro_error::{abort, abort_call_site, emit_error, proc_macro_error};
pub(crate) use quote::{quote, ToTokens, TokenStreamExt}; // ToTokens is implicitly used as a trait
pub(crate) use regex::Regex;
pub(crate) use syn::{spanned::Spanned, Ident, MetaNameValue, Meta, NestedMeta};
pub(crate) use syn::{
spanned::Spanned, GenericArgument, Ident, Meta, MetaNameValue, NestedMeta, Path,
};
6 changes: 3 additions & 3 deletions fastsim-core/fastsim-proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ mod imports;
use imports::*;
mod cumu_method_derive;
mod cycle_derive;
mod fastsim_api;
mod history_vec_derive;
mod hm_derive;
mod pyo3_api;
mod timer;
mod utilities;

#[proc_macro_error]
#[proc_macro_attribute]
/// macro for creating appropriate setters and getters for pyo3 struct attributes
/// and other, non-python API functionality
pub fn pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
pyo3_api::pyo3_api(attr, item)
pub fn fastsim_api(attr: TokenStream, item: TokenStream) -> TokenStream {
fastsim_api::fastsim_api(attr, item)
}

#[proc_macro_error]
Expand Down
8 changes: 4 additions & 4 deletions fastsim-core/resources/vehicles/2012_Ford_Fusion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pt_type:
pwr_ramp_lag_seconds: 1.0
energy_capacity_joules: 2124000000.0
fc:
mass: ~
specific_pwr: ~
mass_kilograms: ~
specific_pwr_watts_per_kilogram: ~
pwr_out_max_watts: 130500.0
pwr_out_max_init_watts: 21750.0
pwr_ramp_lag_seconds: 6.0
Expand Down Expand Up @@ -53,13 +53,13 @@ chassis:
wheel_rr_coef: 0.007
wheel_inertia_kilogram_square_meters: 0.82
num_wheels: 4
wheel_radius: 0.326
wheel_radius_meters: 0.326
cg_height_meters: 0.53
wheel_fric_coef: 0.7
drive_type: FWD
drive_axle_weight_frac: 0.59
wheel_base_meters: 2.72
mass: 1644.2724500334996
mass_kilograms: 1644.2724500334996
pwr_aux_watts: 700.0
trans_eff: 0.875
save_interval: 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ year: 2016
pt_type:
HybridElectricVehicle:
res:
mass: ~
specific_energy: ~
mass_kilograms: ~
specific_energy_joules_per_kilogram: ~
pwr_out_max_watts: 100000.0
energy_capacity_joules: 2700000.0
min_soc: 0.25
Expand All @@ -16,8 +16,8 @@ pt_type:
pwr_ramp_lag_seconds: 1.1
energy_capacity_joules: 1392840000.0
fc:
mass: ~
specific_pwr: ~
mass_kilograms: ~
specific_pwr_watts_per_kilogram: ~
pwr_out_max_watts: 71000.0
pwr_out_max_init_watts: 11639.344262295082
pwr_ramp_lag_seconds: 6.1
Expand Down Expand Up @@ -79,8 +79,8 @@ pt_type:
- 0.93
- 0.92
pwr_out_max_watts: 53000.0
specific_pwr: ~
mass: ~
specific_pwr_watts_per_kilogram: ~
mass_kilograms: ~
save_interval: 1
hev_controls: RESGreedy
mass: ~
Expand All @@ -90,13 +90,13 @@ chassis:
wheel_rr_coef: 0.0064
wheel_inertia_kilogram_square_meters: 0.815
num_wheels: 4
wheel_radius: 0.3175
wheel_radius_meters: 0.3175
cg_height_meters: 0.53
wheel_fric_coef: 0.7
drive_type: FWD
drive_axle_weight_frac: 0.59
wheel_base_meters: 2.7
mass: 1635.0
mass_kilograms: 1635.0
pwr_aux_watts: 1050.0
trans_eff: 0.98
save_interval: 1
2 changes: 1 addition & 1 deletion fastsim-core/src/bin/f3-2012-ford-fusion-udds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Binary for running the 2012 Ford Fusion in fastsim-3 with `cargo build
/// Binary for profiling the 2012 Ford Fusion in fastsim-3 with `cargo build
/// --profile profiling --bin f3-2012-ford-fusion-udds`. To run this for
/// profiling, install [samply] (https://github.com/mstange/samply) and then run
/// `samply record ./target/profiling/f3-2012-ford-fusion-udds`
Expand Down
8 changes: 4 additions & 4 deletions fastsim-core/src/drive_cycle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::imports::*;
use fastsim_2::cycle::RustCycle as Cycle2;

#[pyo3_api(
#[fastsim_api(
fn __len__(&self) -> usize {
self.len()
}
Expand Down Expand Up @@ -372,7 +372,7 @@ impl Cycle {
}
}

#[pyo3_api]
#[fastsim_api]
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone)]
/// Element of `Cycle`. Used for vec-like operations.
pub struct CycleElement {
Expand All @@ -382,12 +382,12 @@ pub struct CycleElement {
/// simulation power \[W\]
#[serde(alias = "speed_mps", alias = "cycMps")]
speed: si::Velocity,
// TODO: make `pyo3_api` handle Option or write custom getter/setter
// TODO: make `fastsim_api` handle Option or write custom getter/setter
#[api(skip_get, skip_set)]
/// road grade
#[serde(skip_serializing_if = "Option::is_none", alias = "cycGrade")]
pub grade: Option<si::Ratio>,
// TODO: make `pyo3_api` handle Option or write custom getter/setter
// TODO: make `fastsim_api` handle Option or write custom getter/setter
#[api(skip_get, skip_set)]
/// road charging/discharing capacity
pub pwr_max_charge: Option<si::Power>,
Expand Down
4 changes: 3 additions & 1 deletion fastsim-core/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub(crate) use crate::utils::{
pub(crate) use crate::utils::{Pyo3Vec2Wrapper, Pyo3Vec3Wrapper, Pyo3VecWrapper};
pub(crate) use derive_more::IsVariant;
pub(crate) use eng_fmt::FormatEng;
pub(crate) use fastsim_proc_macros::{pyo3_api, HistoryMethods, HistoryVec, SetCumulative, timer};
pub(crate) use fastsim_proc_macros::{
fastsim_api, timer, HistoryMethods, HistoryVec, SetCumulative,
};

pub(crate) use anyhow::{anyhow, bail, ensure, Context};
pub(crate) use duplicate::duplicate_item;
Expand Down
11 changes: 5 additions & 6 deletions fastsim-core/src/simdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use super::vehicle::Vehicle;
use crate::air_properties as air;
use crate::imports::*;

#[pyo3_api]
#[fastsim_api]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, HistoryMethods)]
/// Solver parameters
pub struct SimParams {
pub ach_speed_max_iter: u32,
pub ach_speed_tol: si::Ratio,
pub ach_speed_solver_gain: f64,
#[api(skip_get, skip_set)] // TODO: manually write out getter and setter
#[api(skip_get, skip_set)]
pub trace_miss_tol: TraceMissTolerance,
}

Expand All @@ -30,7 +30,7 @@ impl Default for SimParams {
}
}

#[pyo3_api(
#[fastsim_api(
#[new]
fn __new__(veh: Vehicle, cyc: Cycle, sim_params: Option<SimParams>) -> anyhow::Result<Self> {
Ok(SimDrive{
Expand Down Expand Up @@ -80,7 +80,6 @@ impl SimDrive {
}
}

// #[timer] TODO: fix and uncomment
pub fn walk(&mut self) -> anyhow::Result<()> {
ensure!(self.cyc.len() >= 2, format_dbg!(self.cyc.len() < 2));
self.save_state();
Expand Down Expand Up @@ -398,7 +397,7 @@ mod tests {
#[test]
#[cfg(feature = "resources")]
fn test_sim_drive_conv() {
let _veh = mock_f2_conv_veh();
let _veh = mock_conv_veh();
let _cyc = Cycle::from_resource("udds.csv", false).unwrap();
let mut sd = SimDrive {
veh: _veh,
Expand All @@ -413,7 +412,7 @@ mod tests {
#[test]
#[cfg(feature = "resources")]
fn test_sim_drive_hev() {
let _veh = mock_f2_hev();
let _veh = mock_hev();
let _cyc = Cycle::from_resource("udds.csv", false).unwrap();
let mut sd = SimDrive {
veh: _veh,
Expand Down
2 changes: 1 addition & 1 deletion fastsim-core/src/utils/interp/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use super::*;

#[pyo3_api(
#[fastsim_api(
/// Function to get x variable from enum variants
#[getter("x")]
pub fn x_py(&self) -> anyhow::Result<Vec<f64>> {
Expand Down
Loading
Loading