unit_conversion/energy/
mod.rs

1use std::collections::HashMap;
2use lazy_static::lazy_static;
3
4/// This module offers all sorts of energy unit conversion functions.
5/// Conversion factors are taken from [here](https://physics.nist.gov/cuu/Constants/energy.html)
6
7const EV_REC_CENTIMETRES_CONVERSION_FACTOR: f64 = 8_065.543_937;
8const REC_CENTIMETRES_EV_CONVERSION_FACTOR: f64 = 1.239_841_984_332 * 10e-8;
9
10type Callback  = fn(f64) -> f64;
11
12fn unity(energy_in_arb: f64 ) -> f64 { energy_in_arb }
13
14fn ev_2_rcm(energy_in_ev: f64) -> f64 {
15   energy_in_ev * EV_REC_CENTIMETRES_CONVERSION_FACTOR
16}
17
18fn rcm_2_ev(energy_in_rcm: f64) -> f64 {
19   energy_in_rcm * REC_CENTIMETRES_EV_CONVERSION_FACTOR
20}
21
22
23lazy_static! {
24    pub static ref CONVERT_2_RCM_FROM: HashMap<&'static str, Callback> = {
25        let mut t = HashMap::new();
26        t.insert("rcm", unity as Callback);
27        t.insert("eV", ev_2_rcm as Callback);
28    t
29    };
30}
31
32lazy_static! {
33    pub static ref CONVERT_2_EV_FROM: HashMap<&'static str, Callback> = {
34        let mut t = HashMap::new();
35        t.insert("eV", unity as Callback);
36        t.insert("rcm", rcm_2_ev as Callback);
37    t
38    };
39}
40
41
42#[cfg(test)]
43mod tests {
44    use approx::assert_relative_eq;
45
46    use super::*;
47
48    #[test]
49    fn it_works() {
50        assert_eq!(
51            2.0_f64 * EV_REC_CENTIMETRES_CONVERSION_FACTOR, CONVERT_2_RCM_FROM["eV"](2.0_f64)
52        );
53        assert_relative_eq!(
54            7.439_051_905_992_01*10e-5, CONVERT_2_EV_FROM["rcm"](6000.0),
55        )
56    }
57}