Skip to content

Commit

Permalink
Merge branch 'f3/ninterp-0.2.0' into f3/thermal-scary-panic
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Jan 23, 2025
2 parents 20e244c + 120999e commit 9914ea1
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 204 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pt_type:
- 0.921423
- 0.919576
strategy: Linear
extrapolate: Error
extrapolate: Clamp
eff_interp_inputs: CRateTemperature
min_soc: 0.0
max_soc: 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pt_type:
- 0.921423
- 0.919576
strategy: Linear
extrapolate: Error
extrapolate: Clamp
eff_interp_inputs: CRateTemperature
min_soc: 0.5
max_soc: 0.95
Expand Down
2 changes: 1 addition & 1 deletion fastsim-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ toml = { version = "0.8.12", optional = true }
derive_more = { version = "1.0.0", features = ["from_str", "from", "is_variant", "try_into"] }
ureq = { version = "2.9.1", optional = true }
url = { version = "2.5.0", optional = true }
ninterp = { version = "0.1.0", features = ["serde"] }
ninterp = { version = "0.2.0", features = ["serde"] }

[dev-dependencies]
pretty_assertions = "1.4.1"
Expand Down
8 changes: 4 additions & 4 deletions fastsim-core/src/drive_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ impl Init for Cycle {
)
.collect();
// println!("{:?}", self.dist);
self.grade_interp = Some(Interpolator::Interp1D(Interp1D::new(
self.grade_interp = Some(Interpolator::new_1d(
self.dist.iter().map(|x| x.get::<si::meter>()).collect(),
self.grade.iter().map(|y| y.get::<si::ratio>()).collect(),
Strategy::Linear,
Extrapolate::Error,
)?));
)?);

self.elev_interp = Some(Interpolator::Interp1D(Interp1D::new(
self.elev_interp = Some(Interpolator::new_1d(
self.dist.iter().map(|x| x.get::<si::meter>()).collect(),
self.elev.iter().map(|y| y.get::<si::meter>()).collect(),
Strategy::Linear,
Extrapolate::Error,
)?));
)?);

Ok(())
}
Expand Down
46 changes: 23 additions & 23 deletions fastsim-core/src/gas_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ lazy_static! {
#[pyo3(name = "get_te_from_h")]
#[staticmethod]
pub fn get_te_from_h_py(h: f64) -> anyhow::Result<f64> {
Ok(Self::get_te_from_h(h * uc::J_PER_KG)?.get::<si::degree_celsius>())
Ok(Self::get_te_from_h(h * uc::J_PER_KG)?.get::<si::kelvin>() - uc::CELSIUS_TO_KELVIN.value)
}
/// Returns temperature [°C] of air
Expand All @@ -104,7 +104,7 @@ lazy_static! {
#[pyo3(name = "get_te_from_u")]
#[staticmethod]
pub fn get_te_from_u_py(u: f64) -> anyhow::Result<f64> {
Ok(Self::get_te_from_u(u * uc::J_PER_KG)?.get::<si::degree_celsius>())
Ok(Self::get_te_from_u(u * uc::J_PER_KG)?.get::<si::kelvin>() - uc::CELSIUS_TO_KELVIN.value)
}
)]
Expand Down Expand Up @@ -273,18 +273,18 @@ mod air_static_props {
.iter()
.map(|x| *x * uc::KELVIN + *uc::CELSIUS_TO_KELVIN)
.collect();
pub static ref TEMP_FROM_ENTHALPY: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref TEMP_FROM_ENTHALPY: Interpolator = Interpolator::new_1d(
ENTHALPY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
pub static ref TEMP_FROM_ENERGY: Interpolator= Interpolator::Interp1D(Interp1D::new(
).unwrap();
pub static ref TEMP_FROM_ENERGY: Interpolator = Interpolator::new_1d(
ENERGY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
/// Thermal conductivity values of air corresponding to temperature values
static ref THERMAL_CONDUCTIVITY_VALUES: Vec<si::ThermalConductivity> = [
0.019597,
Expand Down Expand Up @@ -316,12 +316,12 @@ mod air_static_props {
.iter()
.map(|x| *x * uc::WATT_PER_METER_KELVIN)
.collect();
pub static ref THERMAL_CONDUCTIVITY_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref THERMAL_CONDUCTIVITY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
THERMAL_CONDUCTIVITY_VALUES.iter().map(|x| x.get::<si::watt_per_meter_degree_celsius>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
/// Specific heat values of air corresponding to temperature values
static ref C_P_VALUES: Vec<si::SpecificHeatCapacity> = [
1006.2,
Expand Down Expand Up @@ -353,12 +353,12 @@ mod air_static_props {
.iter()
.map(|x| *x * uc::J_PER_KG_K)
.collect();
pub static ref C_P_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref C_P_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
C_P_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram_kelvin>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
static ref ENTHALPY_VALUES: Vec<si::SpecificEnergy> = [
338940.,
341930.,
Expand Down Expand Up @@ -389,12 +389,12 @@ mod air_static_props {
.iter()
.map(|x| *x * uc::J_PER_KG)
.collect();
pub static ref ENTHALPY_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref ENTHALPY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
ENTHALPY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
pub static ref ENERGY_VALUES: Vec<si::SpecificEnergy> = [
277880.,
280000.,
Expand Down Expand Up @@ -425,12 +425,12 @@ mod air_static_props {
.iter()
.map(|x| *x * uc::J_PER_KG)
.collect();
pub static ref ENERGY_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref ENERGY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
ENERGY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
static ref DYN_VISCOSITY_VALUES: Vec<si::DynamicViscosity> = [
1.4067e-05,
1.4230e-05,
Expand Down Expand Up @@ -461,24 +461,24 @@ mod air_static_props {
.iter()
.map(|x| *x * uc::PASCAL_SECOND)
.collect();
pub static ref DYN_VISC_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref DYN_VISC_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
DYN_VISCOSITY_VALUES.iter().map(|x| x.get::<si::pascal_second>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
static ref PRANDTL_VALUES: Vec<si::Ratio> = DYN_VISCOSITY_VALUES
.iter()
.zip(C_P_VALUES.iter())
.zip(THERMAL_CONDUCTIVITY_VALUES.iter())
.map(|((mu, c_p), k)| -> si::Ratio {*mu * *c_p / *k})
.collect::<Vec<si::Ratio>>();
pub static ref PRANDTL_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref PRANDTL_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
PRANDTL_VALUES.iter().map(|x| x.get::<si::ratio>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
}
}

Expand Down Expand Up @@ -551,12 +551,12 @@ mod octane_static_props {
.iter()
.map(|x| *x * uc::KELVIN + *uc::CELSIUS_TO_KELVIN)
.collect();
pub static ref TEMP_FROM_ENERGY: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref TEMP_FROM_ENERGY: Interpolator = Interpolator::new_1d(
ENERGY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
pub static ref ENERGY_VALUES: Vec<si::SpecificEnergy> = [
-3.8247e+05,
-3.7645e+05,
Expand Down Expand Up @@ -587,12 +587,12 @@ mod octane_static_props {
.iter()
.map(|x| *x * uc::J_PER_KG)
.collect();
pub static ref ENERGY_INTERP: Interpolator = Interpolator::Interp1D(Interp1D::new(
pub static ref ENERGY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
ENERGY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
).unwrap());
).unwrap();
}
}

Expand All @@ -612,7 +612,7 @@ mod octane_static_props {
#[pyo3(name = "get_te_from_u")]
#[staticmethod]
pub fn get_te_from_u_py(u: f64) -> anyhow::Result<f64> {
Ok(Self::get_te_from_u(u * uc::J_PER_KG)?.get::<si::degree_celsius>())
Ok(Self::get_te_from_u(u * uc::J_PER_KG)?.get::<si::kelvin>() - uc::CELSIUS_TO_KELVIN.value)
}
)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, HistoryMethods)]
Expand Down
2 changes: 1 addition & 1 deletion fastsim-core/src/si.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use si::specific_heat_capacity::{
joule_per_kilogram_degree_celsius, joule_per_kilogram_kelvin,
};
pub use si::specific_power::{kilowatt_per_kilogram, watt_per_kilogram};
pub use si::temperature_interval::{degree_celsius, kelvin};
pub use si::temperature_interval::kelvin;
pub use si::thermal_conductance::watt_per_kelvin;
pub use si::thermal_conductivity::{watt_per_meter_degree_celsius, watt_per_meter_kelvin};
pub use si::time::{hour, second};
Expand Down
20 changes: 10 additions & 10 deletions fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ impl Min for Interpolator {
fn min(&self) -> anyhow::Result<f64> {
match self {
Interpolator::Interp0D(value) => Ok(*value),
Interpolator::Interp1D(interp) => interp.f_x().min(),
Interpolator::Interp2D(interp) => interp.f_xy().min(),
Interpolator::Interp3D(interp) => interp.f_xyz().min(),
Interpolator::InterpND(interp) => Ok(interp
.values()
Interpolator::Interp1D(..) => self.f_x()?.min(),
Interpolator::Interp2D(..) => self.f_xy()?.min(),
Interpolator::Interp3D(..) => self.f_xyz()?.min(),
Interpolator::InterpND(..) => Ok(self
.values()?
.iter()
.fold(f64::INFINITY, |acc, x| acc.min(*x))),
}
Expand Down Expand Up @@ -138,11 +138,11 @@ impl Max for Interpolator {
fn max(&self) -> anyhow::Result<f64> {
match self {
Interpolator::Interp0D(value) => Ok(*value),
Interpolator::Interp1D(interp) => interp.f_x().max(),
Interpolator::Interp2D(interp) => interp.f_xy().max(),
Interpolator::Interp3D(interp) => interp.f_xyz().max(),
Interpolator::InterpND(interp) => Ok(interp
.values()
Interpolator::Interp1D(..) => self.f_x()?.max(),
Interpolator::Interp2D(..) => self.f_xy()?.max(),
Interpolator::Interp3D(..) => self.f_xyz()?.max(),
Interpolator::InterpND(..) => Ok(self
.values()?
.iter()
.fold(f64::NEG_INFINITY, |acc, x| acc.max(*x))),
}
Expand Down
57 changes: 22 additions & 35 deletions fastsim-core/src/utils/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ impl InterpolatorMethods for Interpolator {
*value = min;
Ok(())
}
Interpolator::Interp1D(interp) => {
Interpolator::Interp1D(..) => {
todo!()
}
Interpolator::Interp2D(interp) => {
Interpolator::Interp2D(..) => {
todo!()
}
Interpolator::Interp3D(interp) => {
Interpolator::Interp3D(..) => {
todo!()
}
Interpolator::InterpND(interp) => {
Interpolator::InterpND(..) => {
todo!()
}
}
Expand All @@ -38,19 +38,17 @@ impl InterpolatorMethods for Interpolator {
*value = max;
Ok(())
}
Interpolator::Interp1D(interp) => {
Ok(interp.set_f_x(interp.f_x().iter().map(|x| x * max / old_max).collect())?)
Interpolator::Interp1D(..) => {
Ok(self.set_f_x(self.f_x()?.iter().map(|x| x * max / old_max).collect())?)
}
Interpolator::Interp2D(interp) => Ok(interp.set_f_xy(
interp
.f_xy()
Interpolator::Interp2D(..) => Ok(self.set_f_xy(
self.f_xy()?
.iter()
.map(|v| v.iter().map(|x| x * max / old_max).collect())
.collect(),
)?),
Interpolator::Interp3D(interp) => Ok(interp.set_f_xyz(
interp
.f_xyz()
Interpolator::Interp3D(..) => Ok(self.set_f_xyz(
self.f_xyz()?
.iter()
.map(|v0| {
v0.iter()
Expand All @@ -59,8 +57,8 @@ impl InterpolatorMethods for Interpolator {
})
.collect(),
)?),
Interpolator::InterpND(interp) => {
Ok(interp.set_values(interp.values().map(|x| x * max / old_max))?)
Interpolator::InterpND(..) => {
Ok(self.set_values(self.values()?.map(|x| x * max / old_max))?)
}
}
}
Expand All @@ -70,17 +68,15 @@ impl InterpolatorMethods for Interpolator {
let old_range = old_max - self.min()?;
ensure!(old_range != 0., "Cannot modify range when min == max");
match self {
Interpolator::Interp0D(_value) => unreachable!("The above `ensure` should trigger"),
Interpolator::Interp1D(interp) => Ok(interp.set_f_x(
interp
.f_x()
Interpolator::Interp0D(..) => unreachable!("The above `ensure` should trigger"),
Interpolator::Interp1D(..) => Ok(self.set_f_x(
self.f_x()?
.iter()
.map(|x| old_max + (x - old_max) * range / old_range)
.collect(),
)?),
Interpolator::Interp2D(interp) => Ok(interp.set_f_xy(
interp
.f_xy()
Interpolator::Interp2D(..) => Ok(self.set_f_xy(
self.f_xy()?
.iter()
.map(|v| {
v.iter()
Expand All @@ -89,9 +85,8 @@ impl InterpolatorMethods for Interpolator {
})
.collect(),
)?),
Interpolator::Interp3D(interp) => Ok(interp.set_f_xyz(
interp
.f_xyz()
Interpolator::Interp3D(..) => Ok(self.set_f_xyz(
self.f_xyz()?
.iter()
.map(|v0| {
v0.iter()
Expand All @@ -104,9 +99,8 @@ impl InterpolatorMethods for Interpolator {
})
.collect(),
)?),
Interpolator::InterpND(interp) => Ok(interp.set_values(
interp
.values()
Interpolator::InterpND(..) => Ok(self.set_values(
self.values()?
.map(|x| old_max + (x - old_max) * range / old_range),
)?),
}
Expand All @@ -115,14 +109,7 @@ impl InterpolatorMethods for Interpolator {

impl Init for Interpolator {
fn init(&mut self) -> anyhow::Result<()> {
match self {
Self::Interp0D(_) => {}
Self::Interp1D(interp) => interp.validate()?,
Self::Interp2D(interp) => interp.validate()?,
Self::Interp3D(interp) => interp.validate()?,
Self::InterpND(interp) => interp.validate()?,
}
Ok(())
Ok(self.validate()?)
}
}
impl SerdeAPI for Interpolator {
Expand Down
Loading

0 comments on commit 9914ea1

Please sign in to comment.