Skip to main content

use_chemical_constants/
lib.rs

1#![forbid(unsafe_code)]
2
3//! Reusable chemical and thermodynamic constants expressed as plain `f64` values.
4
5/// Derived SI constant for the molar gas constant, represented here as a rounded `f64` in joules per mole kelvin.
6pub const GAS_CONSTANT: f64 = 8.314_462_618_153_24;
7
8/// Derived SI constant for the Faraday constant, represented here as a rounded `f64` in coulombs per mole.
9#[allow(clippy::excessive_precision)]
10pub const FARADAY_CONSTANT: f64 = 96_485.332_123_310_018;
11
12/// Conventional standard atmosphere, in pascals.
13pub const STANDARD_ATMOSPHERE: f64 = 101_325.0;
14
15/// Conventional standard temperature, in kelvin.
16pub const STANDARD_TEMPERATURE_KELVIN: f64 = 273.15;
17
18/// Conventional standard-state pressure, in pascals.
19pub const STANDARD_PRESSURE_PASCAL: f64 = 100_000.0;
20
21#[cfg(test)]
22mod tests {
23    use super::{FARADAY_CONSTANT, GAS_CONSTANT, STANDARD_ATMOSPHERE, STANDARD_PRESSURE_PASCAL};
24
25    const AVOGADRO_CONSTANT: f64 = 6.022_140_76e23;
26    const BOLTZMANN_CONSTANT: f64 = 1.380_649e-23;
27    const ELEMENTARY_CHARGE: f64 = 1.602_176_634e-19;
28
29    fn runtime(value: f64) -> f64 {
30        value
31    }
32
33    fn approx_eq(left: f64, right: f64, relative_tolerance: f64) {
34        let scale = left.abs().max(right.abs()).max(1.0);
35        let delta = (left - right).abs();
36
37        assert!(
38            delta <= relative_tolerance * scale,
39            "left={left:e} right={right:e} delta={delta:e} rel_tol={relative_tolerance:e}"
40        );
41    }
42
43    #[test]
44    fn faraday_matches_avogadro_times_charge() {
45        approx_eq(
46            FARADAY_CONSTANT,
47            AVOGADRO_CONSTANT * ELEMENTARY_CHARGE,
48            1.0e-12,
49        );
50    }
51
52    #[test]
53    fn gas_constant_matches_avogadro_times_boltzmann() {
54        approx_eq(
55            GAS_CONSTANT,
56            AVOGADRO_CONSTANT * BOLTZMANN_CONSTANT,
57            1.0e-12,
58        );
59    }
60
61    #[test]
62    fn conventional_reference_pressures_are_positive() {
63        assert!(runtime(STANDARD_ATMOSPHERE) > runtime(STANDARD_PRESSURE_PASCAL));
64        assert!(runtime(STANDARD_PRESSURE_PASCAL) > 0.0);
65    }
66}