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

209 pyo3 simdrivelabel #25

Merged
merged 33 commits into from
Jul 18, 2023
Merged

209 pyo3 simdrivelabel #25

merged 33 commits into from
Jul 18, 2023

Conversation

Romia87
Copy link
Collaborator

@Romia87 Romia87 commented Jun 12, 2023

No description provided.

@calbaker calbaker marked this pull request as draft June 13, 2023 15:40
@@ -49,45 +51,26 @@ lazy_static! {
self.mc_peak_eff()
}

// TODO: refactor this to have a non-py and `_py` version
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Romia87 , I made some edits to your changes and the code is correct now. I had actually left that TODO note for myself months (maybe even years ???) ago and forgotten about it so it's good you got this to happen!

.iter()
.map(|e: &f64| -> f64 { e * (new_peak / mc_max_full_eff) })
.collect();
#[setter("mc_peak_eff")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

#[setter("mc_peak_eff")] makes it so that can run veh.mc_peak_eff = 0.44 in Python. set_mc_peak_eff_py then calls set_mc_peak_eff, the pure Rust version.

@@ -37,6 +37,8 @@ lazy_static! {
#[add_pyo3_api(
Copy link
Collaborator

@calbaker calbaker Jun 13, 2023

Choose a reason for hiding this comment

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

@Romia87 , any arguments (e.g. function definitions) that get provided to add_pyo3_api do not get compiled unless the fastsim-py crate is being compiled because of this feature. As such, we want the pure rust versions of functions to live elsewhere. I'll drop a note on that next.

@@ -578,6 +562,32 @@ impl RustVehicle {
arrmax(&self.fc_eff_array)
}

pub fn set_mc_peak_eff(&mut self, new_peak: f64) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Romia87 , this where the pure rust functions live, and in this location, they get compiled every time no matter what features are enabled or disabled.

We created the add_pyo3_api attribute-style procedural macro because it allows us to automate a lot of the pyo3 features for exposing struct fields to python. It also allow use to pass the pymethods block as an argument so that all the pymethods definitions can live in a single impl block, which is a requirement of pyo3. It took months and lots of tedious troubleshooting to get this worked out.

@@ -704,7 +709,7 @@ pub fn get_label_fe_phev(
};
}

Ok(phev_calcs)
return Ok(phev_calcs);
Copy link
Collaborator

@calbaker calbaker Jun 20, 2023

Choose a reason for hiding this comment

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

The idiomatic way to return in rust is Ok(phev_calcs) so please change return Ok(phev_calcs); back to Ok(phev_calcs) since it will otherwise give warnings whenever we run cargo clippy.

@Romia87 Romia87 marked this pull request as ready for review June 27, 2023 17:41
@calbaker
Copy link
Collaborator

calbaker commented Jun 28, 2023

@Romia87, this pymethods attribute and this pymethods attribute are redundant, unnecessary, and not allowed because that's taken care of in the add_pyo3_api procedural macro.

Also, none of these setters and getters, thees getters and setters, or these getters and setters are necessary because inside of Rust, the fields of PHEVCycleCalc are all pub so we can do stuff like:

let mut phev_cycle_calc = PHEVCycleCalc::default();
phev_cycle_calc.cd_fs_gal = 2.2;
println!("{}", phev_cycle_calc.cd_fs_gal);

without needing getters and setters.

Additionally, the add_pyo3_api procedural macro automatically generates the setters and getters for the python side here automatically.

@calbaker calbaker marked this pull request as draft June 28, 2023 21:23
@calbaker calbaker marked this pull request as ready for review July 18, 2023 18:01
@calbaker calbaker merged commit f93dc6e into fastsim-2 Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants