Skip to main content

lindhard/
units.rs

1//! Unit conventions.
2//!
3//! All physical quantities are SI internally (metres, kilograms, seconds,
4//! joules, kelvin). Two places deliberately use customary atomic-physics units
5//! at the API boundary, and say so in their names: energies carry an `_ev`
6//! suffix (electronvolts) and table densities are quoted in g/cm³ in the
7//! source tables. The factors below convert between them; they are exact
8//! because the SI 2019 redefinition fixes the elementary charge and the gram is
9//! a defined fraction of the kilogram.
10
11use crate::constants::ELEMENTARY_CHARGE;
12
13/// Joules per electronvolt. One eV is the elementary charge times one volt
14/// (CODATA 2022 value of `e`, exact; see [`ELEMENTARY_CHARGE`]).
15pub const J_PER_EV: f64 = ELEMENTARY_CHARGE;
16
17/// Kilograms per cubic metre for a density of 1 g/cm³ (exact: 1e-3 kg / 1e-6 m³).
18pub const KG_M3_PER_G_CM3: f64 = 1.0e3;
19
20/// Metres per ångström (exact by definition of the prefix).
21pub const M_PER_ANGSTROM: f64 = 1.0e-10;
22
23/// Convert electronvolts to joules.
24#[inline]
25pub fn ev_to_j(e_ev: f64) -> f64 {
26    e_ev * J_PER_EV
27}
28
29/// Convert joules to electronvolts.
30#[inline]
31pub fn j_to_ev(e_j: f64) -> f64 {
32    e_j / J_PER_EV
33}
34
35/// Convert g/cm³ to kg/m³.
36#[inline]
37pub fn g_cm3_to_kg_m3(rho: f64) -> f64 {
38    rho * KG_M3_PER_G_CM3
39}
40
41/// Convert kg/m³ to g/cm³.
42#[inline]
43pub fn kg_m3_to_g_cm3(rho: f64) -> f64 {
44    rho / KG_M3_PER_G_CM3
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn round_trips() {
53        assert!((j_to_ev(ev_to_j(25.0)) - 25.0).abs() < 1e-12);
54        assert!((kg_m3_to_g_cm3(g_cm3_to_kg_m3(2.33)) - 2.33).abs() < 1e-12);
55    }
56}