Skip to main content

lib_jsl_core/
codata.rs

1//! The CODATA 2022 recommended values of the fundamental physical constants, looked up by name
2//! with their units and standard uncertainties (SciPy's `scipy.constants.physical_constants`,
3//! `value`, `unit`, `precision` and `find`).
4//!
5//! The table holds SciPy's 445 entries: the 355 constants of CODATA 2022 and 90 names kept from
6//! earlier adjustments (with their older values), which are flagged
7//! [`PhysicalConstant::obsolete`] and left out of [`find`].
8//!
9//! ```
10//! use lib_jsl_core::codata::{find, physical_constant, value};
11//!
12//! let me = physical_constant("electron mass").unwrap();
13//! assert_eq!(me.value, 9.1093837139e-31);
14//! assert_eq!(me.unit, "kg");
15//! assert_eq!(me.uncertainty, 2.8e-40);
16//! assert_eq!(value("speed of light in vacuum"), Some(299792458.0));
17//! assert!(find("Bohr radius").contains(&"Bohr radius"));
18//! ```
19//!
20//! See the book entry [Shared types, constants and utilities](https://jwkunz.github.io/lib_jsl/book/foundations/core-utils.html#codata-constants-by-name).
21
22#[path = "codata_table.rs"]
23mod codata_table;
24use codata_table::TABLE;
25
26// ANCHOR: codata
27/// One entry of the CODATA table: SciPy's `physical_constants[name]` as a record.
28#[derive(Clone, Copy, Debug, PartialEq)]
29#[non_exhaustive]
30pub struct PhysicalConstant {
31    /// The name as CODATA spells it, e.g. `"electron mass"`.
32    pub name: &'static str,
33    /// The recommended value, in `unit`.
34    pub value: f64,
35    /// The SI unit, e.g. `"kg"` or `"J T^-1"`; empty for dimensionless constants.
36    pub unit: &'static str,
37    /// The standard (one-sigma) absolute uncertainty; 0 for exact constants.
38    pub uncertainty: f64,
39    /// Whether the name comes from an earlier CODATA adjustment and is no longer current.
40    pub obsolete: bool,
41}
42
43impl PhysicalConstant {
44    /// The relative uncertainty `uncertainty / |value|` (SciPy's `precision`, which divides by
45    /// the signed value).
46    pub fn precision(&self) -> f64 {
47        self.uncertainty / self.value.abs()
48    }
49
50    /// Whether the value is exact by definition (zero uncertainty).
51    pub fn is_exact(&self) -> bool {
52        self.uncertainty == 0.0
53    }
54}
55
56fn entry(row: &'static (&'static str, f64, &'static str, f64, bool)) -> PhysicalConstant {
57    PhysicalConstant { name: row.0, value: row.1, unit: row.2, uncertainty: row.3, obsolete: row.4 }
58}
59
60/// The constant called `name` (exact, case-sensitive match), or `None`.
61pub fn physical_constant(name: &str) -> Option<PhysicalConstant> {
62    TABLE.iter().find(|r| r.0 == name).map(entry)
63}
64
65/// The value of the constant called `name` (SciPy's `value`).
66pub fn value(name: &str) -> Option<f64> {
67    physical_constant(name).map(|c| c.value)
68}
69
70/// The unit of the constant called `name` (SciPy's `unit`).
71pub fn unit(name: &str) -> Option<&'static str> {
72    physical_constant(name).map(|c| c.unit)
73}
74
75/// The relative uncertainty of the constant called `name`.
76pub fn precision(name: &str) -> Option<f64> {
77    physical_constant(name).map(|c| c.precision())
78}
79
80/// The names of the current (CODATA 2022) constants that contain `sub`, ignoring case, in
81/// sorted order (SciPy's `find`). An empty `sub` lists them all.
82pub fn find(sub: &str) -> Vec<&'static str> {
83    let sub = sub.to_lowercase();
84    let mut out: Vec<&'static str> = TABLE.iter().filter(|r| !r.4 && r.0.to_lowercase().contains(&sub)).map(|r| r.0).collect();
85    out.sort_unstable();
86    out
87}
88
89/// Every entry of the table, current and obsolete, in SciPy's order.
90pub fn all() -> impl Iterator<Item = PhysicalConstant> {
91    TABLE.iter().map(entry)
92}
93// ANCHOR_END: codata
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::constants::physics;
99
100    #[test]
101    fn table_and_lookups() {
102        assert_eq!(all().count(), 445);
103        assert_eq!(all().filter(|c| !c.obsolete).count(), 355);
104        assert_eq!(find("").len(), 355);
105        assert_eq!(find("radius"), vec!["Bohr radius", "alpha particle rms charge radius", "classical electron radius", "deuteron rms charge radius", "proton rms charge radius"]);
106        assert!(physical_constant("Planck constant").unwrap().is_exact());
107        assert_eq!(value("Planck constant"), Some(physics::H));
108        assert_eq!(unit("Boltzmann constant"), Some("J K^-1"));
109        assert!((precision("proton mass").unwrap() - 3.1e-10).abs() < 1e-11);
110        assert!(physical_constant("mag. constant").unwrap().obsolete);
111        assert_eq!(value("no such constant"), None);
112    }
113}