-
Notifications
You must be signed in to change notification settings - Fork 5
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
Serde revamp and anyhowization #58
Conversation
@calbaker check out the serde api changes ive been playing with! This PR kind of ballooned into more than just Improvements:
But wait, there's more!New methods (not yet Python exposed):
|
@calbaker I would be fine with changing the tests that return a result back to returning |
I have found a lot of functions that return results, when they don't contain any fallible code. This happens in a lot of the methods called by For example: This code /// Perform all the calculations to solve 1 time step.
pub fn solve_step(&mut self, i: usize) -> anyhow::Result<()> {
self.set_misc_calcs(i)?;
self.set_comp_lims(i)?;
self.set_power_calcs(i)?;
self.set_ach_speed(i)?;
self.set_hybrid_cont_calcs(i)?;
self.set_fc_forced_state_rust(i)?;
self.set_hybrid_cont_decisions(i)?;
self.set_fc_power(i)?;
Ok(())
}
/// Sets misc. calculations at time step 'i'
/// Arguments:
/// ----------
/// i: index of time step
pub fn set_misc_calcs(&mut self, i: usize) -> anyhow::Result<()> {
// if cycle iteration is used, auxInKw must be re-zeroed to trigger the below if statement
// TODO: this is probably computationally expensive and was probably a workaround for numba
// figure out a way to not need this
if self.aux_in_kw.slice(s![i..]).iter().all(|&x| x == 0.0) {
// if all elements after i-1 are zero, trigger default behavior; otherwise, use override value
if self.veh.no_elec_aux {
self.aux_in_kw[i] = self.veh.aux_kw / self.veh.alt_eff;
} else {
self.aux_in_kw[i] = self.veh.aux_kw;
}
}
// Is SOC below min threshold?
self.reached_buff[i] = self.soc[i - 1] >= (self.veh.min_soc + self.veh.perc_high_acc_buf);
// Does the engine need to be on for low SOC or high acceleration
self.high_acc_fc_on_tag[i] = self.soc[i - 1] < self.veh.min_soc
|| (self.high_acc_fc_on_tag[i - 1] && !(self.reached_buff[i]));
self.max_trac_mps[i] =
self.mps_ach[i - 1] + (self.veh.max_trac_mps2 * self.cyc.dt_s_at_i(i));
Ok(())
} and this code /// Perform all the calculations to solve 1 time step.
pub fn solve_step(&mut self, i: usize) -> anyhow::Result<()> {
self.set_misc_calcs(i);
self.set_comp_lims(i)?;
self.set_power_calcs(i)?;
self.set_ach_speed(i)?;
self.set_hybrid_cont_calcs(i)?;
self.set_fc_forced_state_rust(i)?;
self.set_hybrid_cont_decisions(i)?;
self.set_fc_power(i)?;
Ok(())
}
/// Sets misc. calculations at time step 'i'
/// Arguments:
/// ----------
/// i: index of time step
pub fn set_misc_calcs(&mut self, i: usize) {
// if cycle iteration is used, auxInKw must be re-zeroed to trigger the below if statement
// TODO: this is probably computationally expensive and was probably a workaround for numba
// figure out a way to not need this
if self.aux_in_kw.slice(s![i..]).iter().all(|&x| x == 0.0) {
// if all elements after i-1 are zero, trigger default behavior; otherwise, use override value
if self.veh.no_elec_aux {
self.aux_in_kw[i] = self.veh.aux_kw / self.veh.alt_eff;
} else {
self.aux_in_kw[i] = self.veh.aux_kw;
}
}
// Is SOC below min threshold?
self.reached_buff[i] = self.soc[i - 1] >= (self.veh.min_soc + self.veh.perc_high_acc_buf);
// Does the engine need to be on for low SOC or high acceleration
self.high_acc_fc_on_tag[i] = self.soc[i - 1] < self.veh.min_soc
|| (self.high_acc_fc_on_tag[i - 1] && !(self.reached_buff[i]));
self.max_trac_mps[i] =
self.mps_ach[i - 1] + (self.veh.max_trac_mps2 * self.cyc.dt_s_at_i(i));
} Achieve exactly the same thing, and results aren't giving us any benefit What's your opinion on removing the |
@kylecarow, I'd say keep it as is! On numerous occasions, I've ended up changing functions that do not contain fallible code to contain fallible code, and returning a result that does nothing future proofs for that without imposing any meaningful computational costs, which means that if the function/method body changes, no other api changes are needed. |
@calbaker Sounds reasonable to me. I marked this as ready for review, feel free to merge when tests pass and you're happy with the changes. |
@calbaker didnt you say something about wanting to be able to read/write bincode objects? I could set that up to work with a |
@calbaker feel free to merge when you're happy with the changes, i added an approving review to let the merge happen |
…es for serde formats, better format matching
@calbaker I used fastsim/rust/fastsim-core/src/traits.rs Lines 110 to 115 in b8bdfa2
|
I added an implementation of |
I'll go ahead and merge this into see https://github.nrel.gov/MBAP/fastsim/issues/279 for details |
No description provided.