Skip to main content

yatrosci_constants/
lib.rs

1//! # scirs-constants
2//!
3//! Physical constants based on CODATA 2018 recommended values.
4//!
5//! # Referencias
6//!
7//! - Tiesinga, E., Mohr, P. J., Newell, D. B., & Taylor, B. N. (2021). CODATA recommended values of the fundamental physical constants: 2018. *Reviews of Modern Physics*, 93(2), 025010. DOI: [10.1103/RevModPhys.93.025010](https://doi.org/10.1103/RevModPhys.93.025010)
8//!
9//! This crate provides fundamental physical constants with their exact values
10//! and uncertainties as defined by the Committee on Data for Science and
11//! Technology (CODATA) 2018 adjustment.
12//!
13//! ## Usage
14//!
15//! ```rust
16//! use yatrosci_constants::{SPEED_OF_LIGHT, PLANCK_CONSTANT, BOLTZMANN_CONSTANT};
17//!
18//! // Speed of light in vacuum
19//! let c = SPEED_OF_LIGHT;
20//!
21//! // Planck constant
22//! let h = PLANCK_CONSTANT;
23//!
24//! // Calculate photon energy: E = hf
25//! let frequency = 5e14; // Hz (visible light)
26//! let energy = h * frequency;
27//! ```
28//!
29//! ## Categories
30//!
31//! Constants are organized into modules:
32//! - [`universal`] - Universal constants (c, G, h, etc.)
33//! - [`electromagnetic`] - Electromagnetic constants (e, mu_0, epsilon_0, etc.)
34//! - [`atomic`] - Atomic and nuclear constants (m_e, m_p, a_0, etc.)
35//! - [`physico_chemical`] - Physico-chemical constants (N_A, k_B, R, etc.)
36//! - [`adopted`] - Adopted values and conversion factors
37//!
38//! ## Uncertainties
39//!
40//! For constants with uncertainties, companion `_UNCERTAINTY` constants are provided:
41//!
42//! ```rust
43//! use yatrosci_constants::{GRAVITATIONAL_CONSTANT, GRAVITATIONAL_CONSTANT_UNCERTAINTY};
44//!
45//! let g = GRAVITATIONAL_CONSTANT;
46//! let delta_g = GRAVITATIONAL_CONSTANT_UNCERTAINTY;
47//! ```
48
49#![warn(missing_docs)]
50
51pub mod adopted;
52pub mod atomic;
53pub mod electromagnetic;
54pub mod physico_chemical;
55pub mod universal;
56
57// Re-export commonly used constants at crate root
58pub use universal::{
59    GRAVITATIONAL_CONSTANT, GRAVITATIONAL_CONSTANT_UNCERTAINTY, HBAR, PLANCK_CONSTANT,
60    REDUCED_PLANCK_CONSTANT, SPEED_OF_LIGHT, SPEED_OF_LIGHT_VACUUM,
61};
62
63pub use electromagnetic::{
64    CONDUCTANCE_QUANTUM, COULOMB_CONSTANT, ELEMENTARY_CHARGE, EPSILON_0, IMPEDANCE_OF_FREE_SPACE,
65    MAGNETIC_FLUX_QUANTUM, MU_0, VACUUM_PERMEABILITY, VACUUM_PERMITTIVITY, VON_KLITZING_CONSTANT,
66};
67
68pub use atomic::{
69    ALPHA, ATOMIC_MASS_CONSTANT, BOHR_MAGNETON, BOHR_RADIUS, ELECTRON_G_FACTOR, ELECTRON_MASS,
70    FINE_STRUCTURE_CONSTANT, NEUTRON_MASS, NUCLEAR_MAGNETON, PROTON_MASS, RYDBERG_CONSTANT,
71};
72
73pub use physico_chemical::{
74    AVOGADRO_CONSTANT, BOLTZMANN_CONSTANT, FARADAY_CONSTANT, FIRST_RADIATION_CONSTANT,
75    GAS_CONSTANT, LOSCHMIDT_CONSTANT, MOLAR_GAS_CONSTANT, SECOND_RADIATION_CONSTANT,
76    STEFAN_BOLTZMANN_CONSTANT, WIEN_DISPLACEMENT_CONSTANT,
77};
78
79pub use adopted::{
80    ANGSTROM, ASTRONOMICAL_UNIT, ELECTRONVOLT, LIGHT_YEAR, MOLAR_VOLUME_IDEAL_GAS_NTP,
81    MOLAR_VOLUME_IDEAL_GAS_STP, PARSEC, STANDARD_ATMOSPHERE, STANDARD_GRAVITY,
82    STANDARD_STATE_PRESSURE, UNIFIED_ATOMIC_MASS_UNIT,
83};
84
85/// Temperature conversions and `physical_constants` dict API.
86pub mod conversions;
87
88pub use conversions::{
89    convert_temperature, convert_temperature_str, physical_constants, ConstantEntry, TempScale,
90    BTU, CALORIE, FOOT, HORSEPOWER, INCH, MILE, NAUTICAL_MILE, OUNCE, POUND, PSI, YARD,
91};
92
93/// Mathematical constants used in physics calculations
94pub mod math {
95    /// Pi (Archimedes' constant)
96    pub const PI: f64 = std::f64::consts::PI;
97
98    /// Tau = 2*pi
99    pub const TAU: f64 = std::f64::consts::TAU;
100
101    /// Euler's number e
102    pub const E: f64 = std::f64::consts::E;
103
104    /// Square root of 2
105    pub const SQRT_2: f64 = std::f64::consts::SQRT_2;
106
107    /// Square root of 3
108    pub const SQRT_3: f64 = 1.732_050_807_568_877_2;
109
110    /// Natural logarithm of 2
111    pub const LN_2: f64 = std::f64::consts::LN_2;
112
113    /// Natural logarithm of 10
114    pub const LN_10: f64 = std::f64::consts::LN_10;
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn test_speed_of_light() {
123        assert_eq!(SPEED_OF_LIGHT, 299_792_458.0);
124    }
125
126    #[test]
127    fn test_planck_constant() {
128        // h = 6.62607015e-34 J*s (exact by definition)
129        assert!((PLANCK_CONSTANT - 6.62607015e-34).abs() < 1e-44);
130    }
131
132    #[test]
133    fn test_reduced_planck() {
134        // hbar = h / (2*pi)
135        let expected_hbar = PLANCK_CONSTANT / (2.0 * std::f64::consts::PI);
136        assert!((REDUCED_PLANCK_CONSTANT - expected_hbar).abs() < 1e-44);
137    }
138
139    #[test]
140    fn test_fine_structure_constant() {
141        // alpha ~ 1/137
142        assert!((1.0 / FINE_STRUCTURE_CONSTANT - 137.036).abs() < 0.001);
143    }
144
145    #[test]
146    fn test_electron_mass_energy() {
147        // E = mc^2 should give ~0.511 MeV
148        let energy_joules = ELECTRON_MASS * SPEED_OF_LIGHT * SPEED_OF_LIGHT;
149        let energy_ev = energy_joules / ELEMENTARY_CHARGE;
150        let energy_mev = energy_ev / 1e6;
151        assert!((energy_mev - 0.511).abs() < 0.001);
152    }
153}