use std::any::Any;
use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::physics::basic::{
Coef, Density, MassFlowRate, MassFlux, MassFluxType, PhysicalQuantity, Pressure, Velocity,
};
const G_PER_M2_S_TO_KG: f64 = 1e-3;
const G_PER_CM2_S_TO_KG: f64 = 10.0;
impl Default for MassFlux {
fn default() -> Self {
Self::from_kg_per_m2_s(0.0)
}
}
impl PhysicalQuantity for MassFlux {
fn as_any(&self) -> &dyn Any {
self
}
fn is_zero(&self) -> bool {
self.v == 0.0
}
fn default_unit_value(&self) -> f64 {
self.as_kg_per_m2_s()
}
#[cfg(feature = "tmr")]
fn raw_value(&self) -> f64 {
self.v
}
fn set_value(&mut self, value: f64) {
self.v = value;
}
fn unit_display_name(&self) -> String {
"MassFlux (kg/(m²·s))".to_string()
}
}
impl MassFlux {
pub fn from_kg_per_m2_s(v: f64) -> Self {
Self {
default_type: MassFluxType::KilogramPerSquareMeterSecond,
v,
}
}
pub fn from_g_per_m2_s(v: f64) -> Self {
Self {
default_type: MassFluxType::GramPerSquareMeterSecond,
v,
}
}
pub fn from_g_per_cm2_s(v: f64) -> Self {
Self {
default_type: MassFluxType::GramPerSquareCentiMeterSecond,
v,
}
}
pub fn as_kg_per_m2_s(&self) -> f64 {
match self.default_type {
MassFluxType::KilogramPerSquareMeterSecond => self.v,
MassFluxType::GramPerSquareMeterSecond => self.v * G_PER_M2_S_TO_KG,
MassFluxType::GramPerSquareCentiMeterSecond => self.v * G_PER_CM2_S_TO_KG,
}
}
pub fn as_g_per_m2_s(&self) -> f64 {
match self.default_type {
MassFluxType::KilogramPerSquareMeterSecond => self.v / G_PER_M2_S_TO_KG,
MassFluxType::GramPerSquareMeterSecond => self.v,
MassFluxType::GramPerSquareCentiMeterSecond => {
self.v * G_PER_CM2_S_TO_KG / G_PER_M2_S_TO_KG
}
}
}
pub fn as_g_per_cm2_s(&self) -> f64 {
match self.default_type {
MassFluxType::KilogramPerSquareMeterSecond => self.v / G_PER_CM2_S_TO_KG,
MassFluxType::GramPerSquareMeterSecond => {
self.v * G_PER_M2_S_TO_KG / G_PER_CM2_S_TO_KG
}
MassFluxType::GramPerSquareCentiMeterSecond => self.v,
}
}
pub fn dynamic_pressure(&self, velocity: Velocity) -> Pressure {
Pressure::from_pa(self.as_kg_per_m2_s() * velocity.as_m_per_sec())
}
}
impl Add for MassFlux {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from_kg_per_m2_s(self.as_kg_per_m2_s() + rhs.as_kg_per_m2_s())
}
}
impl<'a, 'b> Add<&'b MassFlux> for &'a MassFlux {
type Output = MassFlux;
fn add(self, rhs: &'b MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_kg_per_m2_s() + rhs.as_kg_per_m2_s())
}
}
impl<'a> Add<&'a MassFlux> for MassFlux {
type Output = MassFlux;
fn add(self, rhs: &'a MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_kg_per_m2_s() + rhs.as_kg_per_m2_s())
}
}
impl<'a> Add<MassFlux> for &'a MassFlux {
type Output = MassFlux;
fn add(self, rhs: MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_kg_per_m2_s() + rhs.as_kg_per_m2_s())
}
}
impl Sub for MassFlux {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::from_kg_per_m2_s(self.as_kg_per_m2_s() - rhs.as_kg_per_m2_s())
}
}
impl<'a, 'b> Sub<&'b MassFlux> for &'a MassFlux {
type Output = MassFlux;
fn sub(self, rhs: &'b MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_kg_per_m2_s() - rhs.as_kg_per_m2_s())
}
}
impl<'a> Sub<&'a MassFlux> for MassFlux {
type Output = MassFlux;
fn sub(self, rhs: &'a MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_kg_per_m2_s() - rhs.as_kg_per_m2_s())
}
}
impl<'a> Sub<MassFlux> for &'a MassFlux {
type Output = MassFlux;
fn sub(self, rhs: MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_kg_per_m2_s() - rhs.as_kg_per_m2_s())
}
}
impl Neg for MassFlux {
type Output = Self;
fn neg(self) -> Self::Output {
Self::from_kg_per_m2_s(-self.as_kg_per_m2_s())
}
}
impl Mul<f64> for MassFlux {
type Output = Self;
fn mul(self, rhs: f64) -> Self::Output {
Self::from_kg_per_m2_s(self.as_kg_per_m2_s() * rhs)
}
}
impl Div<f64> for MassFlux {
type Output = Self;
fn div(self, rhs: f64) -> Self::Output {
Self::from_kg_per_m2_s(self.as_kg_per_m2_s() / rhs)
}
}
impl Mul<MassFlux> for f64 {
type Output = MassFlux;
fn mul(self, rhs: MassFlux) -> Self::Output {
MassFlux::from_kg_per_m2_s(self * rhs.as_kg_per_m2_s())
}
}
impl Mul<Coef> for MassFlux {
type Output = Self;
fn mul(self, rhs: Coef) -> Self::Output {
Self::from_kg_per_m2_s(self.as_kg_per_m2_s() * rhs.get_value())
}
}
impl Div<Coef> for MassFlux {
type Output = Self;
fn div(self, rhs: Coef) -> Self::Output {
Self::from_kg_per_m2_s(self.as_kg_per_m2_s() / rhs.get_value())
}
}
impl Mul<Velocity> for MassFlux {
type Output = Pressure;
fn mul(self, rhs: Velocity) -> Self::Output {
Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
}
}
impl Mul<Velocity> for &MassFlux {
type Output = Pressure;
fn mul(self, rhs: Velocity) -> Self::Output {
Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
}
}
impl<'a> Mul<&'a Velocity> for MassFlux {
type Output = Pressure;
fn mul(self, rhs: &'a Velocity) -> Self::Output {
Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
}
}
impl<'a, 'b> Mul<&'b Velocity> for &'a MassFlux {
type Output = Pressure;
fn mul(self, rhs: &'b Velocity) -> Self::Output {
Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
}
}
impl Div<Velocity> for MassFlux {
type Output = Density;
fn div(self, rhs: Velocity) -> Self::Output {
Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
}
}
impl Div<Velocity> for &MassFlux {
type Output = Density;
fn div(self, rhs: Velocity) -> Self::Output {
Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
}
}
impl<'a> Div<&'a Velocity> for MassFlux {
type Output = Density;
fn div(self, rhs: &'a Velocity) -> Self::Output {
Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
}
}
impl<'a, 'b> Div<&'b Velocity> for &'a MassFlux {
type Output = Density;
fn div(self, rhs: &'b Velocity) -> Self::Output {
Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
}
}
impl Div<Velocity> for Pressure {
type Output = MassFlux;
fn div(self, rhs: Velocity) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
}
}
impl Div<Velocity> for &Pressure {
type Output = MassFlux;
fn div(self, rhs: Velocity) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
}
}
impl<'a> Div<&'a Velocity> for Pressure {
type Output = MassFlux;
fn div(self, rhs: &'a Velocity) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
}
}
impl<'a, 'b> Div<&'b Velocity> for &'a Pressure {
type Output = MassFlux;
fn div(self, rhs: &'b Velocity) -> Self::Output {
MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
}
}
impl Mul<crate::physics::basic::Area> for MassFlux {
type Output = MassFlowRate;
fn mul(self, rhs: crate::physics::basic::Area) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
}
}
impl Mul<crate::physics::basic::Area> for &MassFlux {
type Output = MassFlowRate;
fn mul(self, rhs: crate::physics::basic::Area) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
}
}
impl<'a> Mul<&'a crate::physics::basic::Area> for MassFlux {
type Output = MassFlowRate;
fn mul(self, rhs: &'a crate::physics::basic::Area) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
}
}
impl<'a, 'b> Mul<&'b crate::physics::basic::Area> for &'a MassFlux {
type Output = MassFlowRate;
fn mul(self, rhs: &'b crate::physics::basic::Area) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
}
}
impl Mul<MassFlux> for crate::physics::basic::Area {
type Output = MassFlowRate;
fn mul(self, rhs: MassFlux) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
}
}
impl<'a> Mul<MassFlux> for &'a crate::physics::basic::Area {
type Output = MassFlowRate;
fn mul(self, rhs: MassFlux) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
}
}
impl<'a> Mul<&'a MassFlux> for crate::physics::basic::Area {
type Output = MassFlowRate;
fn mul(self, rhs: &'a MassFlux) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
}
}
impl<'a, 'b> Mul<&'b MassFlux> for &'a crate::physics::basic::Area {
type Output = MassFlowRate;
fn mul(self, rhs: &'b MassFlux) -> Self::Output {
MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
#[test]
fn test_construction_and_units() {
let mf = MassFlux::from_kg_per_m2_s(10.0);
assert_relative_eq!(mf.as_kg_per_m2_s(), 10.0, epsilon = 1e-12);
assert_relative_eq!(mf.as_g_per_m2_s(), 10_000.0, epsilon = 1e-6);
assert_relative_eq!(mf.as_g_per_cm2_s(), 1.0, epsilon = 1e-12);
assert_relative_eq!(MassFlux::from_g_per_m2_s(1000.0).as_kg_per_m2_s(), 1.0, epsilon = 1e-12);
assert_relative_eq!(MassFlux::from_g_per_cm2_s(1.0).as_kg_per_m2_s(), 10.0, epsilon = 1e-12);
}
#[test]
fn test_cm2_unit_is_10_not_1e_minus3() {
let cm2 = MassFlux::from_g_per_cm2_s(1.0);
let m2 = MassFlux::from_g_per_m2_s(1.0);
assert_relative_eq!(cm2.as_kg_per_m2_s() / m2.as_kg_per_m2_s(), 10_000.0, epsilon = 1e-9);
}
#[test]
fn test_all_unit_branches() {
let a = MassFlux::from_kg_per_m2_s(25.0);
let b = MassFlux::from_g_per_m2_s(25_000.0);
let c = MassFlux::from_g_per_cm2_s(2.5);
for mf in [a, b, c] {
assert_relative_eq!(mf.as_kg_per_m2_s(), 25.0, epsilon = 1e-9);
assert_relative_eq!(mf.as_g_per_m2_s(), 25_000.0, epsilon = 1e-4);
assert_relative_eq!(mf.as_g_per_cm2_s(), 2.5, epsilon = 1e-9);
}
}
#[test]
fn test_unit_round_trip() {
for v in [0.0, 1e-6, 0.5, 25.0, 1e4] {
let mf = MassFlux::from_kg_per_m2_s(v);
assert_relative_eq!(
MassFlux::from_g_per_m2_s(mf.as_g_per_m2_s()).as_kg_per_m2_s(),
v,
epsilon = 1e-9
);
assert_relative_eq!(
MassFlux::from_g_per_cm2_s(mf.as_g_per_cm2_s()).as_kg_per_m2_s(),
v,
epsilon = 1e-9
);
}
}
#[test]
fn test_arith() {
let a = MassFlux::from_kg_per_m2_s(10.0);
let b = MassFlux::from_g_per_cm2_s(1.0);
assert_relative_eq!((a + b).as_kg_per_m2_s(), 20.0, epsilon = 1e-9);
assert_relative_eq!((&a + &b).as_kg_per_m2_s(), 20.0, epsilon = 1e-9);
assert_relative_eq!((a + &b).as_kg_per_m2_s(), 20.0, epsilon = 1e-9);
assert_relative_eq!((&a + b).as_kg_per_m2_s(), 20.0, epsilon = 1e-9);
assert_relative_eq!((a - b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
assert_relative_eq!((&a - &b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
assert_relative_eq!((a - &b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
assert_relative_eq!((&a - b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
assert_relative_eq!((a * 2.0).as_kg_per_m2_s(), 20.0, epsilon = 1e-9);
assert_relative_eq!((a / 4.0).as_kg_per_m2_s(), 2.5, epsilon = 1e-9);
assert_relative_eq!((0.5 * a).as_kg_per_m2_s(), 5.0, epsilon = 1e-9);
assert_relative_eq!((a * Coef::new(3.0)).as_kg_per_m2_s(), 30.0, epsilon = 1e-9);
assert_relative_eq!((a / Coef::new(2.0)).as_kg_per_m2_s(), 5.0, epsilon = 1e-9);
assert_relative_eq!((-a).as_kg_per_m2_s(), -10.0, epsilon = 1e-9);
}
#[test]
fn test_mul_velocity_gives_pressure() {
let mf = MassFlux::from_kg_per_m2_s(245.0);
let v = Velocity::from_m_per_sec(200.0);
let q = mf * v;
assert_relative_eq!(q.as_pa(), 49_000.0, epsilon = 1e-9);
assert_relative_eq!((&mf * v).as_pa(), 49_000.0, epsilon = 1e-9);
assert_relative_eq!((mf * &v).as_pa(), 49_000.0, epsilon = 1e-9);
assert_relative_eq!((&mf * &v).as_pa(), 49_000.0, epsilon = 1e-9);
assert_relative_eq!(mf.dynamic_pressure(v).as_pa(), 49_000.0, epsilon = 1e-9);
assert_relative_eq!((mf * v * Coef::new(0.5)).as_pa(), 24_500.0, epsilon = 1e-9);
}
#[test]
fn test_inverse_operations() {
let mf = MassFlux::from_kg_per_m2_s(245.0);
let v = Velocity::from_m_per_sec(200.0);
let rho = mf / v;
assert_relative_eq!(rho.as_kg_per_m3(), 1.225, epsilon = 1e-9);
assert_relative_eq!((&mf / &v).as_kg_per_m3(), 1.225, epsilon = 1e-9);
assert_relative_eq!((mf / &v).as_kg_per_m3(), 1.225, epsilon = 1e-9);
assert_relative_eq!((&mf / v).as_kg_per_m3(), 1.225, epsilon = 1e-9);
let p = Pressure::from_pa(49_000.0);
let back = p / v;
assert_relative_eq!(back.as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
assert_relative_eq!((&p / &v).as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
assert_relative_eq!((p / &v).as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
assert_relative_eq!((&p / v).as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
}
#[test]
fn test_chain_matches_single_step_formula() {
use crate::physics::basic::Density;
let rho = Density::from_kg_per_m3(1.225);
let v = Velocity::from_m_per_sec(200.0);
let two_step = (rho * v) * v;
let one_step = rho.as_kg_per_m3() * v.as_m_per_sec() * v.as_m_per_sec();
assert_relative_eq!(two_step.as_pa(), one_step, epsilon = 1e-9);
assert_relative_eq!(two_step.as_pa(), 49_000.0, epsilon = 1e-9);
}
#[test]
fn test_mul_area_gives_mass_flow_rate() {
let mf = MassFlux::from_kg_per_m2_s(245.0);
let area = crate::physics::basic::Area::from_m2(0.5);
assert_relative_eq!((mf * area).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((&mf * area).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((mf * &area).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((&mf * &area).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((area * mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((&area * mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((area * &mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
assert_relative_eq!((&area * &mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
}
#[test]
fn test_full_mass_flow_chain() {
let rho = Density::from_kg_per_m3(1.225);
let v = Velocity::from_m_per_sec(200.0);
let area = crate::physics::basic::Area::from_m2(0.5);
let flux = rho * v;
assert_relative_eq!(flux.as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
let rate = flux * area;
assert_relative_eq!(rate.as_kg_per_s(), 122.5, epsilon = 1e-9);
let mass = rate * chrono::TimeDelta::seconds(10);
assert_relative_eq!(mass.as_kg(), 1225.0, epsilon = 1e-9);
}
#[test]
fn test_physical_quantity_contract() {
let mf = MassFlux::default();
assert!(mf.is_zero());
assert_eq!(mf.default_unit_value(), 0.0);
assert_eq!(mf.unit_display_name(), "MassFlux (kg/(m²·s))");
let mut mf2 = MassFlux::default();
mf2.set_value(25.0);
assert_relative_eq!(mf2.as_kg_per_m2_s(), 25.0, epsilon = 1e-12);
assert!(!mf2.is_zero());
assert!(mf2.as_any().downcast_ref::<MassFlux>().is_some());
}
}