Skip to content

Commit

Permalink
restored TemperatureInterval and made much more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Jan 24, 2025
1 parent 442c430 commit 92b3b21
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cabin:
width_meters: 2
hvac:
LumpedCabinAndRES:
te_set_kelvin: 295.15
te_set_degrees_Celsius: 295.15
te_deadband_kelvin: 1.5
p_cabin_watts_per_kelvin: 0.0
i_cabin: 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pt_type:
htc_to_amb_stop_watts_per_square_meter_degree_celsius: 50.0
conductance_from_comb_watts_per_kelvin: 5.0
max_frac_from_comb: 0.5
tstat_te_sto_kelvin: 358.15
tstat_te_sto_kelvin: 85.0
tstat_te_delta_kelvin: 5.0
tstat_interp:
Interp1D:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ pub(crate) fn impl_getters_and_setters(field: &mut syn::Field) -> Option<()> {
uom::si::heat_capacity::joule_per_degree_celsius
)
}
"Temperature" => extract_units!(uom::si::temperature_interval::kelvin),
"TemperatureInterval" => extract_units!(uom::si::temperature_interval::kelvin),
"Temperature" => {
extract_units!(uom::si::temperature_interval::degree_celsius)
}
"ThermalConductance" => {
extract_units!(uom::si::thermal_conductance::watt_per_kelvin)
}
Expand Down
4 changes: 2 additions & 2 deletions fastsim-core/resources/vehicles/2016_TOYOTA_Prius_Two.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,8 @@ pt_type:
frac_of_most_eff_pwr_to_run_fc: 1.0
frac_res_chrg_for_fc: 0.0
frac_res_dschrg_for_fc: 1.0
temp_fc_forced_on_kelvin: ~
temp_fc_allowed_off_kelvin: ~
temp_fc_forced_on_degrees_Celsius: ~
temp_fc_allowed_off_degrees_Celsius: ~
aux_cntrl: AuxOnResPriority
mass_kilograms: ~
sim_params:
Expand Down
74 changes: 37 additions & 37 deletions fastsim-core/src/gas_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ lazy_static! {
#[pyo3(name = "get_therm_cond")]
#[staticmethod]
pub fn get_therm_cond_py(te_air: f64) -> anyhow::Result<f64> {
Ok(Self::get_therm_cond(te_air * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::watt_per_meter_kelvin>())
Ok(Self::get_therm_cond((te_air + uc::CELSIUS_TO_KELVIN) * uc::KELVIN)?.get::<si::watt_per_meter_kelvin>())
}
/// Returns constant pressure specific heat [J/(kg*K)] of air
Expand All @@ -54,7 +54,7 @@ lazy_static! {
#[pyo3(name = "get_specific_heat_cp")]
#[staticmethod]
pub fn get_specific_heat_cp_py(te_air: f64) -> anyhow::Result<f64> {
Ok(Self::get_specific_heat_cp(te_air * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::joule_per_kilogram_kelvin>())
Ok(Self::get_specific_heat_cp((te_air + uc::CELSIUS_TO_KELVIN) * uc::KELVIN)?.get::<si::joule_per_kilogram_kelvin>())
}
/// Returns specific enthalpy [J/kg] of air
Expand All @@ -63,7 +63,7 @@ lazy_static! {
#[pyo3(name = "get_specific_enthalpy")]
#[staticmethod]
pub fn get_specific_enthalpy_py(te_air: f64) -> anyhow::Result<f64> {
Ok(Self::get_specific_enthalpy(te_air * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::joule_per_kilogram>())
Ok(Self::get_specific_enthalpy((te_air - uc::CELSIUS_TO_KELVIN) * uc::KELVIN)?.get::<si::joule_per_kilogram>())
}
/// Returns specific energy [J/kg] of air
Expand All @@ -72,7 +72,7 @@ lazy_static! {
#[pyo3(name = "get_specific_energy")]
#[staticmethod]
pub fn get_specific_energy_py(te_air: f64) -> anyhow::Result<f64> {
Ok(Self::get_specific_energy(te_air * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::joule_per_kilogram>())
Ok(Self::get_specific_energy((te_air - uc::CELSIUS_TO_KELVIN) * uc::KELVIN)?.get::<si::joule_per_kilogram>())
}
/// Returns thermal Prandtl number of air
Expand All @@ -81,7 +81,7 @@ lazy_static! {
#[pyo3(name = "get_pr")]
#[staticmethod]
pub fn get_pr_py(te_air: f64) -> anyhow::Result<f64> {
Ok(Self::get_pr(te_air * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::ratio>())
Ok(Self::get_pr((te_air - uc::CELSIUS_TO_KELVIN) * uc::KELVIN )?.get::<si::ratio>())
}
/// Returns dynamic viscosity \[Pa*s\] of air
Expand All @@ -90,7 +90,7 @@ lazy_static! {
#[pyo3(name = "get_dyn_visc")]
#[staticmethod]
pub fn get_dyn_visc_py(te_air: f64) -> anyhow::Result<f64> {
Ok(Self::get_dyn_visc(te_air * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::pascal_second>())
Ok(Self::get_dyn_visc((te_air - uc::CELSIUS_TO_KELVIN) * uc::KELVIN)?.get::<si::pascal_second>())
}
/// Returns temperature [°C] of air
Expand All @@ -99,7 +99,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::kelvin>() - uc::CELSIUS_TO_KELVIN.value)
Ok(Self::get_te_from_h(h * uc::J_PER_KG)?.get::<si::degree_celsius>())
}
/// Returns temperature [°C] of air
Expand All @@ -108,7 +108,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::kelvin>() - uc::CELSIUS_TO_KELVIN.value)
Ok(Self::get_te_from_u(u * uc::J_PER_KG)?.get::<si::degree_celsius>())
}
)]
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Air {
/// - `te_air`: temperature of air
pub fn get_therm_cond(te_air: si::Temperature) -> anyhow::Result<si::ThermalConductivity> {
Ok(
asp::THERMAL_CONDUCTIVITY_INTERP.interpolate(&[te_air.get::<si::kelvin>()])?
asp::THERMAL_CONDUCTIVITY_INTERP.interpolate(&[te_air.get::<si::degree_celsius>()])?
* uc::WATT_PER_METER_KELVIN,
)
}
Expand All @@ -162,35 +162,38 @@ impl Air {
pub fn get_specific_heat_cp(
te_air: si::Temperature,
) -> anyhow::Result<si::SpecificHeatCapacity> {
Ok(asp::C_P_INTERP.interpolate(&[te_air.get::<si::kelvin>()])? * uc::J_PER_KG_K)
Ok(asp::C_P_INTERP.interpolate(&[te_air.get::<si::degree_celsius>()])? * uc::J_PER_KG_K)
}

/// Returns specific enthalpy of air
/// # Arguments
/// - `te_air`: temperature of air
pub fn get_specific_enthalpy(te_air: si::Temperature) -> anyhow::Result<si::SpecificEnergy> {
Ok(asp::ENTHALPY_INTERP.interpolate(&[te_air.get::<si::kelvin>()])? * uc::J_PER_KG)
Ok(asp::ENTHALPY_INTERP.interpolate(&[te_air.get::<si::degree_celsius>()])? * uc::J_PER_KG)
}

/// Returns specific energy of air
/// # Arguments
/// - `te_air`: temperature of air
pub fn get_specific_energy(te_air: si::Temperature) -> anyhow::Result<si::SpecificEnergy> {
Ok(asp::ENERGY_INTERP.interpolate(&[te_air.get::<si::kelvin>()])? * uc::J_PER_KG)
Ok(asp::ENERGY_INTERP.interpolate(&[te_air.get::<si::degree_celsius>()])? * uc::J_PER_KG)
}

/// Returns thermal Prandtl number of air
/// # Arguments
/// - `te_air`: temperature of air
pub fn get_pr(te_air: si::Temperature) -> anyhow::Result<si::Ratio> {
Ok(asp::PRANDTL_INTERP.interpolate(&[te_air.get::<si::kelvin>()])? * uc::R)
Ok(asp::PRANDTL_INTERP.interpolate(&[te_air.get::<si::degree_celsius>()])? * uc::R)
}

/// Returns dynamic viscosity \[Pa*s\] of air
/// # Arguments
/// te_air: temperature of air
pub fn get_dyn_visc(te_air: si::Temperature) -> anyhow::Result<si::DynamicViscosity> {
Ok(asp::DYN_VISC_INTERP.interpolate(&[te_air.get::<si::kelvin>()])? * uc::PASCAL_SECOND)
Ok(
asp::DYN_VISC_INTERP.interpolate(&[te_air.get::<si::degree_celsius>()])?
* uc::PASCAL_SECOND,
)
}

/// Returns temperature of air
Expand Down Expand Up @@ -247,7 +250,7 @@ mod air_static_props {
use super::*;
lazy_static! {
/// Array of temperatures at which properties are evaluated
static ref TEMPERATURE_VALUES: Vec<si::Temperature> = [
static ref TEMPERATURE_DEG_C_VALUES: Vec<f64> = vec![
-60.,
-57.03690616,
-53.1958198,
Expand All @@ -273,19 +276,16 @@ mod air_static_props {
2947.10642291,
3841.10336915,
5000.
]
.iter()
.map(|x| *x * uc::KELVIN + *uc::CELSIUS_TO_KELVIN)
.collect();
];
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>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
Strategy::Linear,
Extrapolate::Error
).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>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
Strategy::Linear,
Extrapolate::Error
).unwrap();
Expand Down Expand Up @@ -321,7 +321,7 @@ mod air_static_props {
.map(|x| *x * uc::WATT_PER_METER_KELVIN)
.collect();
pub static ref THERMAL_CONDUCTIVITY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
THERMAL_CONDUCTIVITY_VALUES.iter().map(|x| x.get::<si::watt_per_meter_degree_celsius>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand Down Expand Up @@ -358,7 +358,7 @@ mod air_static_props {
.map(|x| *x * uc::J_PER_KG_K)
.collect();
pub static ref C_P_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
C_P_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram_kelvin>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand Down Expand Up @@ -394,7 +394,7 @@ mod air_static_props {
.map(|x| *x * uc::J_PER_KG)
.collect();
pub static ref ENTHALPY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
ENTHALPY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand Down Expand Up @@ -430,7 +430,7 @@ mod air_static_props {
.map(|x| *x * uc::J_PER_KG)
.collect();
pub static ref ENERGY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
ENERGY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand Down Expand Up @@ -466,7 +466,7 @@ mod air_static_props {
.map(|x| *x * uc::PASCAL_SECOND)
.collect();
pub static ref DYN_VISC_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
DYN_VISCOSITY_VALUES.iter().map(|x| x.get::<si::pascal_second>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand All @@ -478,7 +478,7 @@ mod air_static_props {
.map(|((mu, c_p), k)| -> si::Ratio {*mu * *c_p / *k})
.collect::<Vec<si::Ratio>>();
pub static ref PRANDTL_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
PRANDTL_VALUES.iter().map(|x| x.get::<si::ratio>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand Down Expand Up @@ -525,7 +525,7 @@ mod octane_static_props {
use super::*;
lazy_static! {
/// Array of temperatures at which properties are evaluated
static ref TEMPERATURE_VALUES: Vec<si::Temperature> = [
static ref TEMPERATURE_DEG_C_VALUES: Vec<f64> = vec![
-4.00000000e+01,
-3.70369062e+01,
-3.31958198e+01,
Expand All @@ -551,13 +551,10 @@ mod octane_static_props {
2.96710642e+03,
3.86110337e+03,
5.02000000e+03
]
.iter()
.map(|x| *x * uc::KELVIN + *uc::CELSIUS_TO_KELVIN)
.collect();
];
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>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
Strategy::Linear,
Extrapolate::Error
).unwrap();
Expand Down Expand Up @@ -592,7 +589,7 @@ mod octane_static_props {
.map(|x| *x * uc::J_PER_KG)
.collect();
pub static ref ENERGY_INTERP: Interpolator = Interpolator::new_1d(
TEMPERATURE_VALUES.iter().map(|x| x.get::<si::kelvin>()).collect::<Vec<f64>>(),
TEMPERATURE_DEG_C_VALUES.clone(),
ENERGY_VALUES.iter().map(|x| x.get::<si::joule_per_kilogram>()).collect::<Vec<f64>>(),
Strategy::Linear,
Extrapolate::Error
Expand All @@ -607,7 +604,7 @@ mod octane_static_props {
#[pyo3(name = "get_specific_energy")]
#[staticmethod]
pub fn get_specific_energy_py(te_octane: f64) -> anyhow::Result<f64> {
Ok(Self::get_specific_energy(te_octane * uc::KELVIN - *uc::CELSIUS_TO_KELVIN)?.get::<si::joule_per_kilogram>())
Ok(Self::get_specific_energy((te_octane - uc::CELSIUS_TO_KELVIN) * uc::KELVIN )?.get::<si::joule_per_kilogram>())
}
/// Returns temperature [°C] of octane
Expand All @@ -616,7 +613,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::kelvin>() - uc::CELSIUS_TO_KELVIN.value)
Ok(Self::get_te_from_u(u * uc::J_PER_KG)?.get::<si::degree_celsius>())
}
)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, HistoryMethods)]
Expand All @@ -629,7 +626,10 @@ impl Octane {
/// # Arguments
/// - `te_octane`: temperature of octane
pub fn get_specific_energy(te_octane: si::Temperature) -> anyhow::Result<si::SpecificEnergy> {
Ok(osp::ENERGY_INTERP.interpolate(&[te_octane.get::<si::kelvin>()])? * uc::J_PER_KG)
Ok(
osp::ENERGY_INTERP.interpolate(&[te_octane.get::<si::degree_celsius>()])?
* uc::J_PER_KG,
)
}

/// Returns temperature of octane
Expand Down
5 changes: 3 additions & 2 deletions fastsim-core/src/si.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub use si::f64::{
Acceleration, Angle, Area, AvailableEnergy as SpecificEnergy, Curvature, DynamicViscosity,
Energy, Force, Frequency, HeatCapacity, HeatTransfer as HeatTransferCoeff, InverseVelocity,
Length, Mass, MassDensity, MomentOfInertia, Power, PowerRate, Pressure, Ratio,
SpecificHeatCapacity, SpecificPower, TemperatureInterval as Temperature, ThermalConductance,
ThermalConductivity, Time, Velocity, Volume,
SpecificHeatCapacity, SpecificPower, TemperatureInterval, ThermalConductance,
ThermalConductivity, ThermodynamicTemperature as Temperature, Time, Velocity, Volume,
};
pub use si::force::{newton, pound_force};
pub use si::heat_capacity::{joule_per_degree_celsius, joule_per_kelvin};
Expand All @@ -33,6 +33,7 @@ pub use si::specific_power::{kilowatt_per_kilogram, watt_per_kilogram};
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::thermodynamic_temperature::{degree_celsius, kelvin as kelvin_abs};
pub use si::time::{hour, second};
pub use si::velocity::{meter_per_second, mile_per_hour};
pub use si::volume::cubic_meter;
6 changes: 3 additions & 3 deletions fastsim-core/src/uc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ unit_const!(
9.80
);

lazy_static::lazy_static! {
pub static ref CELSIUS_TO_KELVIN: crate::si::Temperature = 273.15 * KELVIN;
}
unit_const!(KELVIN, Temperature, 1.0);
unit_const!(KELVIN_INT, TemperatureInterval, 1.0);
unit_const!(J_PER_KG_K, SpecificHeatCapacity, 1.0);
unit_const!(J_PER_K, HeatCapacity, 1.0);
unit_const!(J_PER_KG, SpecificEnergy, 1.0);
unit_const!(PASCAL_SECOND, DynamicViscosity, 1.0);
unit_const!(PASCAL, Pressure, 1.0);
unit_const!(WATT_PER_METER_SQUARED_KELVIN, ThermalConductance, 1.0);
unit_const!(WATT_PER_METER_KELVIN, ThermalConductivity, 1.0);

pub const CELSIUS_TO_KELVIN: f64 = 273.15;
14 changes: 11 additions & 3 deletions fastsim-core/src/vehicle/cabin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl LumpedCabin {
self.state.pwr_thrml_to_res = pwr_thrml_to_res;
// flat plate model for isothermal, mixed-flow from Incropera and deWitt, Fundamentals of Heat and Mass
// Transfer, 7th Edition
let cab_te_film_ext = 0.5 * (self.state.temperature + te_amb_air);
let cab_te_film_ext: si::Temperature = 0.5
* (self.state.temperature.get::<si::kelvin_abs>() + te_amb_air.get::<si::kelvin_abs>())
* uc::KELVIN;
self.state.reynolds_for_plate =
Air::get_density(Some(cab_te_film_ext), Some(veh_state.elev_curr))
* veh_state.speed_ach
Expand Down Expand Up @@ -139,11 +141,17 @@ impl LumpedCabin {
* Air::get_therm_cond(cab_te_film_ext).with_context(|| format_dbg!())?
/ self.length)
+ 1.0 / self.cab_shell_htc_to_amb);
(self.length * self.width) * htc_overall_moving * (te_amb_air - self.state.temperature)
(self.length * self.width)
* htc_overall_moving
* (te_amb_air.get::<si::degree_celsius>()
- self.state.temperature.get::<si::degree_celsius>())
* uc::KELVIN_INT
} else {
(self.length * self.width)
/ (1.0 / self.cab_htc_to_amb_stop + 1.0 / self.cab_shell_htc_to_amb)
* (te_amb_air - self.state.temperature)
* (te_amb_air.get::<si::degree_celsius>()
- self.state.temperature.get::<si::degree_celsius>())
* uc::KELVIN_INT
};

self.state.temp_prev = self.state.temperature;
Expand Down
Loading

0 comments on commit 92b3b21

Please sign in to comment.