Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Aug 16, 2024
1 parent 20bfaaf commit eb37c68
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
27 changes: 13 additions & 14 deletions fastsim-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ macro_rules! impl_get_set_eff_max_min {
}

/// Scales eff_interp by ratio of new `eff_max` per current calculated max
pub fn set_eff_max(&mut self, eff_max: f64) -> anyhow::Result<(), String> {
pub fn set_eff_max(&mut self, eff_max: f64) -> anyhow::Result<()> {
if (0.0..=1.0).contains(&eff_max) {
let old_max = self.get_eff_max();
self.eff_interp_fwd = self
.eff_interp_fwd
.f_x()
.with_context(|| "eff_interp_fwd does not have f_x field")?
.iter()
.map(|x| x * eff_max / old_max)
.collect();
let old_max = self.get_eff_max()?;
match &mut self.eff_interp_fwd {
Interpolator::Interp1D(interp1d) => {
interp1d.f_x = ;
},
_ => bail!("{}\n", "Only `Interpolator::Interp1D` is allowed.")
}
Ok(())
} else {
Err(format!(
Err(anyhow!(
"`eff_max` ({:.3}) must be between 0.0 and 1.0",
eff_max,
))
Expand All @@ -50,7 +49,7 @@ macro_rules! impl_get_set_eff_range {
() => {
/// Max value of `eff_interp` minus min value of `eff_interp`.
pub fn get_eff_range(&self) -> anyhow::Result<f64> {
Ok(self.get_eff_max() - self.get_eff_min())
Ok(self.get_eff_max()? - self.get_eff_min()?)
}

/// Scales values of `eff_interp` without changing max such that max - min
Expand All @@ -71,10 +70,10 @@ macro_rules! impl_get_set_eff_range {
self.eff_interp_fwd = eff_interp_fwd;
Ok(())
} else if (0.0..=1.0).contains(&eff_range) {
let old_min = self.get_eff_min();
let old_range = self.get_eff_max() - old_min;
let old_min = self.get_eff_min()?;
let old_range = self.get_eff_max()? - old_min;
if old_range == 0.0 {
return anyhow!(format!(
return Err(anyhow!(
"`eff_range` is already zero so it cannot be modified."
));
}
Expand Down
17 changes: 0 additions & 17 deletions fastsim-core/src/vehicle/powertrain/electric_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,6 @@ impl ElectricMachine {
stringify!(ElectricMachine::get_cur_pwr_tract_out_max)
);

// is this necessary since this same code is also in init()?
if let None = self.eff_interp_bwd {
// sets eff_interp_bwd to eff_interp_fwd, but changes the x-value.
// TODO: do the extrapolate and strategy fields also need to be changed?
let mut eff_interp_bwd = self.eff_interp_fwd;
eff_interp_bwd.set_x(
self.eff_interp_fwd
.x()?
.iter()
.zip(self.eff_interp_fwd.f_x()?.iter())
.map(|(x, y)| x / y)
.collect(),
);
self.eff_interp_bwd = Some(eff_interp_bwd);
// self.set_pwr_in_frac_interp()
// .with_context(|| format_dbg!())?;
}
// should this use the forward or backwards intepolator? If it uses the forward interpolator,
// the x-value will be wrong, but it uses 'pwr_in_fwd_max'
// TODO: make sure `fwd` and `bwd` are clearly documented somewhere
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ impl ReversibleEnergyStorage {
interp2d.interpolate(&[
state.pwr_out_electrical.get::<si::watt>(),
state.soc.get::<si::ratio>(),
]) * uc::R
])? * uc::R
}
Interpolator::Interp3D(interp3d) => {
interp3d.interpolate(&[
state.pwr_out_electrical.get::<si::watt>(),
state.soc.get::<si::ratio>(),
state.temperature_celsius.get::<si::degree_celsius>(),
]) * uc::R
state.temperature_celsius,
])? * uc::R
}
_ => bail!("Invalid interpolator. See docs for `ReversibleEnergyStorage::eff_interp`"),
};
Expand Down

0 comments on commit eb37c68

Please sign in to comment.