Skip to content

Commit

Permalink
pyo3=0.22.4
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Jan 3, 2025
1 parent 97c89af commit bf60c11
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 12 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ codegen-units = 1 # optimize connection between modules
[workspace.dependencies]
anyhow = "1.0.57"
ndarray = { version = "0.15.4", features = ["serde"] }
pyo3 = "0.21"
pyo3 = "0.22.4"
pyo3-log = "*"
serde = "1.0.143"
serde_json = "1.0.83"
Expand Down
5 changes: 5 additions & 0 deletions rust/fastsim-core/fastsim-proc-macros/src/add_pyo3_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
#[cfg(feature = "resources")]
#[staticmethod]
#[pyo3(name = "from_resource")]
#[pyo3(signature = (filepath, skip_init=None))]
pub fn from_resource_py(filepath: &Bound<PyAny>, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_resource(
PathBuf::extract_bound(filepath)?,
Expand Down Expand Up @@ -246,6 +247,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_file")]
#[pyo3(signature = (filepath, skip_init=None))]
pub fn from_file_py(filepath: &Bound<PyAny>, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_file(
PathBuf::extract_bound(filepath)?,
Expand Down Expand Up @@ -274,6 +276,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_str")]
#[pyo3(signature = (contents, format, skip_init=None))]
pub fn from_str_py(contents: &str, format: &str, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_str(contents, format, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}
Expand All @@ -292,6 +295,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_json")]
#[pyo3(signature = (json_str, skip_init=None))]
pub fn from_json_py(json_str: &str, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_json(json_str, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}
Expand All @@ -310,6 +314,7 @@ pub fn add_pyo3_api(attr: TokenStream, item: TokenStream) -> TokenStream {
///
#[staticmethod]
#[pyo3(name = "from_yaml")]
#[pyo3(signature = (yaml_str, skip_init=None))]
pub fn from_yaml_py(yaml_str: &str, skip_init: Option<bool>) -> PyResult<Self> {
Self::from_yaml(yaml_str, skip_init.unwrap_or_default()).map_err(|e| PyIOError::new_err(format!("{:?}", e)))
}
Expand Down
14 changes: 4 additions & 10 deletions rust/fastsim-core/src/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ impl RustCycleCache {
#[staticmethod]
#[pyo3(name = "from_csv")]
#[pyo3(signature = (filepath, skip_init=None))]
pub fn from_csv_py(filepath: &Bound<PyAny>, skip_init: Option<bool>) -> anyhow::Result<Self> {
Self::from_csv_file(PathBuf::extract_bound(filepath)?, skip_init.unwrap_or_default())
}
Expand All @@ -496,6 +497,7 @@ impl RustCycleCache {
}
#[staticmethod]
#[pyo3(signature = (dict, skip_init=None))]
pub fn from_dict(dict: &Bound<PyDict>, skip_init: Option<bool>) -> PyResult<Self> {
let time_s = Array::from_vec(dict.get_item("time_s")?.with_context(|| format_dbg!())?.extract()?);
let cyc_len = time_s.len();
Expand All @@ -520,17 +522,8 @@ impl RustCycleCache {
} else {
Array::default(cyc_len)
},
// name: PyAny::get_item(dict, "name").and_then(String::extract).unwrap_or_default(),
name: if let Ok(Some(item_res)) = dict.get_item("name") {
if let Ok(name) = item_res.extract() {
if let Ok(name_str) = String::extract(name) {
name_str
} else {
Default::default()
}
} else {
Default::default()
}
String::extract_bound(&item_res).unwrap_or_default()
} else {
Default::default()
},
Expand Down Expand Up @@ -569,6 +562,7 @@ impl RustCycleCache {
}
#[pyo3(name = "modify_with_braking_trajectory")]
#[pyo3(signature = (brake_accel_m_per_s2, idx, dts_m=None))]
pub fn modify_with_braking_trajectory_py(
&mut self,
brake_accel_m_per_s2: f64,
Expand Down
4 changes: 4 additions & 0 deletions rust/fastsim-core/src/simdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl Default for RustSimDriveParams {
}
#[pyo3(name = "sim_drive")]
#[pyo3(signature = (init_soc=None, aux_in_kw_override=None))]
/// Initialize and run sim_drive_walk as appropriate for vehicle attribute vehPtType.
/// Arguments
/// ------------
Expand All @@ -213,6 +214,7 @@ impl Default for RustSimDriveParams {
self.sim_drive(init_soc, aux_in_kw_override)
}
#[pyo3(signature = (init_soc, aux_in_kw_override=None))]
/// Receives second-by-second cycle information, vehicle properties,
/// and an initial state of charge and runs sim_drive_step to perform a
/// backward facing powertrain simulation. Method 'sim_drive' runs this
Expand All @@ -233,6 +235,7 @@ impl Default for RustSimDriveParams {
self.walk(init_soc, aux_in_kw_override)
}
#[pyo3(signature = (by_microtrip=None, extend_fraction=None, blend_factor=None, min_target_speed_m_per_s=None))]
/// Sets the intelligent driver model parameters for an eco-cruise driving trajectory.
/// This is a convenience method instead of setting the sim_params.idm* parameters yourself.
/// - by_microtrip: bool, if True, target speed is set by microtrip, else by cycle
Expand All @@ -257,6 +260,7 @@ impl Default for RustSimDriveParams {
}
#[pyo3(name = "init_for_step")]
#[pyo3(signature = (init_soc, aux_in_kw_override=None))]
/// This is a specialty method which should be called prior to using
/// sim_drive_step in a loop.
/// Arguments
Expand Down
1 change: 1 addition & 0 deletions rust/fastsim-core/src/simdrive/simdrive_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rayon::prelude::*;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[add_pyo3_api(
#[pyo3(name = "sim_drive")]
#[pyo3(signature = (parallelize=None))]
/// Calls `sim_drive` method for each simdrive instance in vec.
/// # Arguments:
/// * parallelize: whether to parallelize `sim_drive` calls, defaults to `true`
Expand Down
1 change: 1 addition & 0 deletions rust/fastsim-core/src/simdrivelabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ pub fn get_label_fe(

#[cfg(feature = "pyo3")]
#[pyfunction(name = "get_label_fe")]
#[pyo3(signature = (veh, full_detail=None, verbose=None))]
/// pyo3 version of [get_label_fe]
pub fn get_label_fe_py(
veh: &vehicle::RustVehicle,
Expand Down
11 changes: 11 additions & 0 deletions rust/fastsim-core/src/thermal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::vehicle_thermal::*;
#[add_pyo3_api(
/// method for instantiating SimDriveHot
#[new]
#[pyo3(signature = (cyc, veh, vehthrm, init_state=None, amb_te_deg_c=None))]
pub fn __new__(
cyc: cycle::RustCycle,
veh: vehicle::RustVehicle,
Expand All @@ -30,6 +31,7 @@ use crate::vehicle_thermal::*;
Ok(self.gap_to_lead_vehicle_m().to_vec())
}
#[pyo3(name = "sim_drive")]
#[pyo3(signature = (init_soc=None, aux_in_kw_override=None))]
/// Initialize and run sim_drive_walk as appropriate for vehicle attribute vehPtType.
/// Arguments
/// ------------
Expand All @@ -45,6 +47,7 @@ use crate::vehicle_thermal::*;
self.sim_drive(init_soc, aux_in_kw_override)
}
#[pyo3(signature = (init_soc, aux_in_kw_override=None))]
/// Receives second-by-second cycle information, vehicle properties,
/// and an initial state of charge and runs sim_drive_step to perform a
/// backward facing powertrain simulation. Method 'sim_drive' runs this
Expand All @@ -66,6 +69,7 @@ use crate::vehicle_thermal::*;
}
#[pyo3(name = "init_for_step")]
#[pyo3(signature = (init_soc, aux_in_kw_override=None))]
/// This is a specialty method which should be called prior to using
/// sim_drive_step in a loop.
/// Arguments
Expand Down Expand Up @@ -983,6 +987,13 @@ impl SimDriveHot {

#[add_pyo3_api(
#[new]
#[pyo3(signature = (
amb_te_deg_c=None,
fc_te_deg_c_init=None,
cab_te_deg_c_init=None,
exhport_te_deg_c_init=None,
cat_te_deg_c_init=None,
))]
pub fn __new__(
amb_te_deg_c: Option<f64>,
fc_te_deg_c_init: Option<f64>,
Expand Down
38 changes: 37 additions & 1 deletion rust/fastsim-core/src/vehicle_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ pub struct VehicleDataEPA {
impl SerdeAPI for VehicleDataEPA {}

#[cfg_attr(feature = "pyo3", pyfunction)]
#[pyo3(signature = (
year,
make,
model,
cache_url=None,
data_dir=None,
))]
/// Gets options from fueleconomy.gov for the given vehicle year, make, and model
///
/// Arguments:
Expand Down Expand Up @@ -335,6 +342,12 @@ pub fn get_options_for_year_make_model(
}

#[cfg_attr(feature = "pyo3", pyfunction)]
#[pyo3(signature = (
id,
year,
cache_url=None,
data_dir=None,
))]
pub fn get_vehicle_data_for_id(
id: i32,
year: &str,
Expand Down Expand Up @@ -720,6 +733,15 @@ fn match_epatest_with_fegov(
#[derive(Default, PartialEq, Clone, Debug, Deserialize, Serialize)]
#[add_pyo3_api(
#[new]
#[pyo3(signature = (
vehicle_width_in,
vehicle_height_in,
fuel_tank_gal,
ess_max_kwh,
mc_max_kw,
ess_max_kw,
fc_max_kw=None
))]
pub fn __new__(
vehicle_width_in: f64,
vehicle_height_in: f64,
Expand Down Expand Up @@ -753,6 +775,13 @@ pub struct OtherVehicleInputs {
impl SerdeAPI for OtherVehicleInputs {}

#[cfg_attr(feature = "pyo3", pyfunction)]
#[pyo3(signature = (
vehicle_id,
year,
other_inputs,
cache_url=None,
data_dir=None,
))]
/// Creates RustVehicle for the given vehicle using data from fueleconomy.gov and EPA databases
/// The created RustVehicle is also written as a yaml file
///
Expand Down Expand Up @@ -1287,7 +1316,14 @@ fn load_fegov_data_for_given_years<P: AsRef<Path>>(
Ok(data)
}
#[cfg_attr(feature = "pyo3", pyfunction)]

#[pyo3(signature = (
year,
make,
model,
other_inputs,
cache_url=None,
data_dir=None,
))]
/// Import All Vehicles for the given Year, Make, and Model and supplied other inputs
pub fn import_all_vehicles(
year: u32,
Expand Down
11 changes: 11 additions & 0 deletions rust/fastsim-core/src/vehicle_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ pub const NETWORK_TEST_DISABLE_ENV_VAR_NAME: &str = "FASTSIM_DISABLE_NETWORK_TES
#[cfg_attr(feature = "pyo3", pyfunction)]
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "default")]
#[pyo3(signature = (
veh,
a_lbf,
b_lbf__mph,
c_lbf__mph2,
custom_rho=None,
custom_rho_temp_degC=None,
custom_rho_elevation_m=None,
simdrive_optimize=None,
_show_plots=None,
))]
pub fn abc_to_drag_coeffs(
veh: &mut RustVehicle,
a_lbf: f64,
Expand Down

0 comments on commit bf60c11

Please sign in to comment.