use crate::{Quantity, Unit};
use qtty_derive::Unit;
pub use crate::dimension::Pressure;
pub trait PressureUnit: Unit<Dim = Pressure> {}
impl<T: Unit<Dim = Pressure>> PressureUnit for T {}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "Pa", dimension = Pressure, ratio = 1.0)]
pub struct Pascal;
pub type Pa = Pascal;
pub type Pascals = Quantity<Pa>;
pub const PASCAL: Pascals = Pascals::new(1.0);
macro_rules! si_pascal {
($name:ident, $sym:literal, $ratio:expr, $alias:ident, $qty:ident, $one:ident) => {
#[doc = concat!("SI-prefixed pascal unit (", stringify!($ratio), " Pa).")]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = $sym, dimension = Pressure, ratio = $ratio)]
pub struct $name;
#[doc = concat!("Type alias shorthand for [`", stringify!($name), "`].")]
pub type $alias = $name;
#[doc = concat!("A quantity measured in ", stringify!($name), "s.")]
pub type $qty = Quantity<$alias>;
#[doc = concat!("One ", stringify!($name), ".")]
pub const $one: $qty = $qty::new(1.0);
};
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "hPa", dimension = Pressure, ratio = 1e2)]
pub struct Hectopascal;
pub type HPa = Hectopascal;
pub type Hectopascals = Quantity<HPa>;
pub const HECTOPASCAL: Hectopascals = Hectopascals::new(1.0);
si_pascal!(Millipascal, "mPa", 1e-3, MilliPa, Millipascals, MILLIPASCAL);
si_pascal!(Kilopascal, "kPa", 1e3, KPa, Kilopascals, KILOPASCAL);
si_pascal!(Megapascal, "MPa", 1e6, MPa, Megapascals, MEGAPASCAL);
si_pascal!(Gigapascal, "GPa", 1e9, GPa, Gigapascals, GIGAPASCAL);
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "bar", dimension = Pressure, ratio = 1e5)]
pub struct Bar;
pub type Bars = Quantity<Bar>;
pub const BAR: Bars = Bars::new(1.0);
#[cfg(feature = "customary")]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "atm", dimension = Pressure, ratio = 101_325.0)]
pub struct Atmosphere;
#[cfg(feature = "customary")]
pub type Atm = Atmosphere;
#[cfg(feature = "customary")]
pub type Atmospheres = Quantity<Atm>;
#[cfg(feature = "customary")]
pub const ATMOSPHERE: Atmospheres = Atmospheres::new(1.0);
#[cfg(feature = "customary")]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "Torr", dimension = Pressure, ratio = 101_325.0 / 760.0)]
pub struct Torr;
#[cfg(feature = "customary")]
pub type Torrs = Quantity<Torr>;
#[cfg(feature = "customary")]
pub const TORR: Torrs = Torrs::new(1.0);
#[cfg(feature = "customary")]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "mmHg", dimension = Pressure, ratio = 101_325.0 / 760.0)]
pub struct MillimeterOfMercury;
#[cfg(feature = "customary")]
pub type MmHg = MillimeterOfMercury;
#[cfg(feature = "customary")]
pub type MillimetersOfMercury = Quantity<MmHg>;
#[cfg(feature = "customary")]
pub const MILLIMETER_OF_MERCURY: MillimetersOfMercury = MillimetersOfMercury::new(1.0);
#[cfg(feature = "customary")]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "psi", dimension = Pressure, ratio = 6_894.757_293)]
pub struct PoundPerSquareInch;
#[cfg(feature = "customary")]
pub type Psi = PoundPerSquareInch;
#[cfg(feature = "customary")]
pub type PoundsPerSquareInch = Quantity<Psi>;
#[cfg(feature = "customary")]
pub const PSI: PoundsPerSquareInch = PoundsPerSquareInch::new(1.0);
#[cfg(feature = "customary")]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Unit)]
#[unit(symbol = "inHg", dimension = Pressure, ratio = 3_386.389)]
pub struct InchOfMercury;
#[cfg(feature = "customary")]
pub type InHg = InchOfMercury;
#[cfg(feature = "customary")]
pub type InchesOfMercury = Quantity<InHg>;
#[cfg(feature = "customary")]
pub const INCH_OF_MERCURY: InchesOfMercury = InchesOfMercury::new(1.0);
#[macro_export]
#[doc(hidden)]
macro_rules! pressure_units {
($cb:path) => {
$cb!(
Pascal,
Millipascal,
Hectopascal,
Kilopascal,
Megapascal,
Gigapascal,
Bar
);
};
}
#[cfg(feature = "customary")]
#[macro_export]
#[doc(hidden)]
macro_rules! pressure_customary_units {
($cb:path) => {
$cb!(
Atmosphere,
Torr,
MillimeterOfMercury,
PoundPerSquareInch,
InchOfMercury
);
};
}
pressure_units!(crate::impl_unit_from_conversions);
#[cfg(feature = "cross-unit-ops")]
pressure_units!(crate::impl_unit_cross_unit_ops);
#[cfg(feature = "customary")]
crate::impl_unit_from_conversions_between!(
Pascal, Millipascal, Hectopascal, Kilopascal, Megapascal, Gigapascal, Bar;
Atmosphere, Torr, MillimeterOfMercury, PoundPerSquareInch, InchOfMercury
);
#[cfg(all(feature = "customary", feature = "cross-unit-ops"))]
crate::impl_unit_cross_unit_ops_between!(
Pascal, Millipascal, Hectopascal, Kilopascal, Megapascal, Gigapascal, Bar;
Atmosphere, Torr, MillimeterOfMercury, PoundPerSquareInch, InchOfMercury
);
#[cfg(test)]
pressure_units!(crate::assert_units_are_builtin);
#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
use approx::assert_abs_diff_eq;
#[test]
fn pascal_to_hectopascal() {
let p = Pascals::new(101_325.0);
let hpa: Hectopascals = p.to();
assert_abs_diff_eq!(hpa.value(), 1013.25, epsilon = 1e-9);
}
#[test]
fn hectopascal_to_pascal() {
let hpa = Hectopascals::new(1013.25);
let p: Pascals = hpa.to();
assert_abs_diff_eq!(p.value(), 101_325.0, epsilon = 1e-9);
}
#[test]
fn pascal_to_kilopascal() {
let p = Pascals::new(1000.0);
let kpa: Kilopascals = p.to();
assert_abs_diff_eq!(kpa.value(), 1.0, epsilon = 1e-12);
}
#[test]
fn pascal_to_bar() {
let p = Pascals::new(100_000.0);
let bar: Bars = p.to();
assert_abs_diff_eq!(bar.value(), 1.0, epsilon = 1e-12);
}
#[test]
fn bar_to_hectopascal() {
let bar = Bars::new(1.0);
let hpa: Hectopascals = bar.to();
assert_abs_diff_eq!(hpa.value(), 1000.0, epsilon = 1e-9);
}
#[test]
fn pascals_addition() {
let a = Pascals::new(50.0);
let b = Pascals::new(50.0);
let c = a + b;
assert_abs_diff_eq!(c.value(), 100.0, epsilon = 1e-12);
}
#[test]
#[cfg(feature = "customary")]
fn atmosphere_to_pascal() {
let atm = Atmospheres::new(1.0);
let p: Pascals = atm.to();
assert_abs_diff_eq!(p.value(), 101_325.0, epsilon = 1e-9);
}
#[test]
#[cfg(feature = "customary")]
fn torr_to_atmosphere() {
let t = Torrs::new(760.0);
let a: Atmospheres = t.to();
assert_abs_diff_eq!(a.value(), 1.0, epsilon = 1e-12);
}
#[test]
#[cfg(feature = "customary")]
fn psi_to_pascal() {
let p = PoundsPerSquareInch::new(1.0);
let pa: Pascals = p.to();
assert_abs_diff_eq!(pa.value(), 6_894.757_293, epsilon = 1e-6);
}
}