Skip to content

Commit

Permalink
incremental changes
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Aug 16, 2024
1 parent c484c65 commit 20bfaaf
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 132 deletions.
7 changes: 2 additions & 5 deletions fastsim-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ macro_rules! impl_get_set_eff_max_min {
pub fn set_eff_max(&mut self, eff_max: f64) -> anyhow::Result<(), String> {
if (0.0..=1.0).contains(&eff_max) {
let old_max = self.get_eff_max();
self.eff_interp = self
self.eff_interp_fwd = self
.eff_interp_fwd
.f_x()
.with_context(|| "eff_interp_fwd does not have f_x field")?
Expand Down Expand Up @@ -62,12 +62,11 @@ macro_rules! impl_get_set_eff_range {
let f_x = vec![
eff_max;
self.eff_interp_fwd
.0
.f_x()
.with_context(|| "eff_interp_fwd does not have f_x field")?
.len()
];
let eff_interp_fwd = self.eff_interp_fwd.0;
let eff_interp_fwd = self.eff_interp_fwd;
eff_interp_fwd.set_f_x(f_x)?;
self.eff_interp_fwd = eff_interp_fwd;
Ok(())
Expand All @@ -81,7 +80,6 @@ macro_rules! impl_get_set_eff_range {
}
self.eff_interp = self
.eff_interp_fwd
.0
.f_x()
.with_context(|| "eff_interp_fwd does not have f_x field")?
.iter()
Expand All @@ -91,7 +89,6 @@ macro_rules! impl_get_set_eff_range {
let x_neg = self.get_eff_min();
self.eff_interp = self
.eff_interp_fwd
.0
.f_x()
.with_context(|| "eff_interp_fwd does not have f_x field")?
.iter()
Expand Down
85 changes: 0 additions & 85 deletions fastsim-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,91 +292,6 @@ pub fn check_monotonicity(data: &[f64]) -> anyhow::Result<()> {
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_interp3d() {
let point = [0.5, 0.5, 0.5];
let grid = [vec![0.0, 1.0], vec![0.0, 1.0], vec![0.0, 1.0]];
let values = vec![
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
];
match interp3d(&point, &grid, &values) {
Ok(i) => assert!(i == 0.5),
Err(e) => panic!("test failed with: {e}"),
};
}

#[test]
fn test_interp3d_offset() {
let point = [0.75, 0.25, 0.5];
let grid = [vec![0.0, 1.0], vec![0.0, 1.0], vec![0.0, 1.0]];
let values = vec![
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
];
match interp3d(&point, &grid, &values) {
Ok(i) => assert!(i == 0.75),
Err(e) => panic!("test failed with: {e}"),
};
}

#[test]
fn test_interp3d_exact_value_lower() {
let point = [0.0, 0.0, 0.0];
let grid = [vec![0.0, 1.0], vec![0.0, 1.0], vec![0.0, 1.0]];
let values = vec![
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
];
match interp3d(&point, &grid, &values) {
Ok(i) => assert!(i == 0.0),
Err(e) => panic!("test failed with: {e}"),
};
}

#[test]
fn test_interp3d_below_value_lower() {
let point = [-1.0, -1.0, -1.0];
let grid = [vec![0.0, 1.0], vec![0.0, 1.0], vec![0.0, 1.0]];
let values = vec![
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
];
match interp3d(&point, &grid, &values) {
Ok(i) => assert!(i == 0.0),
Err(e) => panic!("test failed with: {e}"),
};
}

#[test]
fn test_interp3d_above_value_upper() {
let point = [2.0, 2.0, 2.0];
let grid = [vec![0.0, 1.0], vec![0.0, 1.0], vec![0.0, 1.0]];
let values = vec![
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
];
match interp3d(&point, &grid, &values) {
Ok(i) => assert!(i == 1.0),
Err(e) => panic!("test failed with: {e}"),
};
}

#[test]
fn test_interp3d_exact_value_upper() {
let point = [1.0, 1.0, 1.0];
let grid = [vec![0.0, 1.0], vec![0.0, 1.0], vec![0.0, 1.0]];
let values = vec![
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
vec![vec![0.0, 0.0], vec![1.0, 1.0]],
];
match interp3d(&point, &grid, &values) {
Ok(i) => assert!(i == 1.0),
Err(e) => panic!("test failed with: {e}"),
};
}

#[test]
fn test_linspace() {
assert_eq!(Vec::linspace(0.0, 1.0, 3), vec![0.0, 0.5, 1.0]);
Expand Down
4 changes: 2 additions & 2 deletions fastsim-core/src/vehicle/powertrain/electric_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ impl ElectricMachine {
// }

// TODO: figure out why these macro invocations are ornery if uncommented
// impl_get_set_eff_max_min!();
// impl_get_set_eff_range!();
impl_get_set_eff_max_min!();
impl_get_set_eff_range!();
}

impl SerdeAPI for ElectricMachine {}
Expand Down
40 changes: 0 additions & 40 deletions fastsim-core/src/vehicle/powertrain/reversible_energy_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,46 +432,6 @@ impl ReversibleEnergyStorage {
self.min_soc + 0.05 * uc::R
}

#[allow(clippy::too_many_arguments)]
pub fn new(
pwr_out_max_watts: f64,
energy_capacity_joules: f64,
min_soc: f64,
max_soc: f64,
initial_soc: f64,
initial_temperature_celcius: f64,
soc_hi_ramp_start: Option<f64>,
soc_lo_ramp_start: Option<f64>,
save_interval: Option<usize>,
) -> anyhow::Result<Self> {
ensure!(
min_soc <= initial_soc || initial_soc <= max_soc,
format!(
"{}\ninitial soc must be between min and max soc, inclusive",
format_dbg!(min_soc <= initial_soc || initial_soc <= max_soc)
)
);

let initial_state = ReversibleEnergyStorageState {
soc: uc::R * initial_soc,
temperature_celsius: initial_temperature_celcius,
..Default::default()
};
Ok(ReversibleEnergyStorage {
pwr_out_max: uc::W * pwr_out_max_watts,
energy_capacity: uc::J * energy_capacity_joules,
min_soc: uc::R * min_soc,
max_soc: uc::R * max_soc,
soc_hi_ramp_start: soc_hi_ramp_start.map(|val| val * uc::R),
soc_lo_ramp_start: soc_lo_ramp_start.map(|val| val * uc::R),
state: initial_state,
save_interval,
history: ReversibleEnergyStorageStateHistoryVec::new(),
mass: None,
specific_energy: None,
})
}

/// Sets specific energy and either mass or energy capacity of battery
/// # Arguments
/// - `specific_energy`: specific energy of battery
Expand Down

0 comments on commit 20bfaaf

Please sign in to comment.