rs_sci/
consts.rs

1use crate::units::*;
2
3#[derive(Debug, Clone)]
4pub struct Const;
5
6impl Const {
7    // mathematical constants
8    pub const PI: f64 = std::f64::consts::PI;
9    pub const E: f64 = std::f64::consts::E;
10    pub const GOLDEN_RATIO: f64 = 1.618033988749895;
11    pub const SQRT_2: f64 = 1.4142135623730951;
12    pub const LN_2: f64 = 0.6931471805599453;
13    pub const LN_10: f64 = 2.302585092994046;
14    pub const EULER_MASCHERONI: f64 = 0.5772156649015329;
15
16    // physical constants
17    pub const SPEED_OF_LIGHT: f64 = 299_792_458.0; // m/s
18    pub const GRAVITATIONAL_CONSTANT: f64 = 6.674_30e-11; // m^3/kg·s^2
19    pub const PLANCK_CONSTANT: f64 = 6.626_070_15e-34; // J·s
20    pub const REDUCED_PLANCK_CONSTANT: f64 = Self::PLANCK_CONSTANT / (2.0 * Self::PI); // J·s
21    pub const BOLTZMANN_CONSTANT: f64 = 1.380_649e-23; // J/K
22    pub const ELEMENTARY_CHARGE: f64 = 1.602_176_634e-19; // C
23    pub const ELECTRON_MASS: f64 = 9.109_383_701_5e-31; // kg
24    pub const PROTON_MASS: f64 = 1.672_621_923_69e-27; // kg
25    pub const NEUTRON_MASS: f64 = 1.674_927_471_4e-27; // kg
26    pub const AVOGADRO_CONSTANT: f64 = 6.022_140_76e23; // mol^-1
27    pub const VACUUM_PERMITTIVITY: f64 = 8.854_187_812_8e-12; // F/m
28    pub const VACUUM_PERMEABILITY: f64 = 1.256_637_062_12e-6; // H/m
29    pub const FINE_STRUCTURE_CONSTANT: f64 = 7.297_352_5693e-3; // dimensionless
30    pub const RYDBERG_CONSTANT: f64 = 10_973_731.568_160; // m^-1
31    pub const ATOMIC_MASS_UNIT: f64 = 1.660_539_066_60e-27; // kg
32    pub const FARADAY_CONSTANT: f64 = 96_485.332_123_1; // C/mol
33    pub const GAS_CONSTANT: f64 = 8.314_462_618_153_24; // J/(mol·K)
34    pub const STEFAN_BOLTZMANN_CONSTANT: f64 = 5.670_374_419e-8; // W/(m^2·K^4)
35    pub const WIEN_DISPLACEMENT_CONSTANT: f64 = 2.897_771_955e-3; // m·K
36
37    // astronomical constants
38    pub const ASTRONOMICAL_UNIT: f64 = 149_597_870_700.0; // m
39    pub const LIGHT_YEAR: f64 = 9.460_730_472_580_8e15; // m
40    pub const PARSEC: f64 = 3.085_677_581_491_367e16; // m
41    pub const SOLAR_MASS: f64 = 1.988_847e30; // kg
42    pub const EARTH_MASS: f64 = 5.972_167e24; // kg
43    pub const EARTH_RADIUS: f64 = 6.371e6; // m
44    pub const SOLAR_RADIUS: f64 = 6.957e8; // m
45    pub const HUBBLE_CONSTANT: f64 = 67.4; // (km/s)/Mpc
46    pub fn pi(precision: Option<u32>) -> f64 {
47        match precision {
48            Some(p) => {
49                let scale = 10f64.powi(p as i32);
50                (Self::PI * scale).round() / scale
51            }
52            None => Self::PI,
53        }
54    }
55
56    pub fn e(precision: Option<u32>) -> f64 {
57        match precision {
58            Some(p) => {
59                let scale = 10f64.powi(p as i32);
60                (Self::E * scale).round() / scale
61            }
62            None => Self::E,
63        }
64    }
65
66    pub fn golden_ratio(precision: Option<u32>) -> f64 {
67        match precision {
68            Some(p) => {
69                let scale = 10f64.powi(p as i32);
70                (Self::GOLDEN_RATIO * scale).round() / scale
71            }
72            None => Self::GOLDEN_RATIO,
73        }
74    }
75
76    pub fn sqrt_2(precision: Option<u32>) -> f64 {
77        match precision {
78            Some(p) => {
79                let scale = 10f64.powi(p as i32);
80                (Self::SQRT_2 * scale).round() / scale
81            }
82            None => Self::SQRT_2,
83        }
84    }
85
86    pub fn ln_2(precision: Option<u32>) -> f64 {
87        match precision {
88            Some(p) => {
89                let scale = 10f64.powi(p as i32);
90                (Self::LN_2 * scale).round() / scale
91            }
92            None => Self::LN_2,
93        }
94    }
95
96    pub fn ln_10(precision: Option<u32>) -> f64 {
97        match precision {
98            Some(p) => {
99                let scale = 10f64.powi(p as i32);
100                (Self::LN_10 * scale).round() / scale
101            }
102            None => Self::LN_10,
103        }
104    }
105
106    pub fn euler_mascheroni(precision: Option<u32>) -> f64 {
107        match precision {
108            Some(p) => {
109                let scale = 10f64.powi(p as i32);
110                (Self::EULER_MASCHERONI * scale).round() / scale
111            }
112            None => Self::EULER_MASCHERONI,
113        }
114    }
115
116    // Physical constant methods (existing)
117    pub fn speed_of_light(unit: Speed) -> Speed {
118        Speed::C.convert_to(unit)
119    }
120
121    pub fn gravitational_constant(unit: Force) -> Force {
122        Force::Custom(
123            Mass::Kg,
124            Length::Custom(Self::GRAVITATIONAL_CONSTANT, Box::new(Length::Meter)),
125            Time::Second,
126        )
127        .convert_to(unit)
128    }
129
130    pub fn planck_constant(unit: Energy) -> Energy {
131        Energy::Custom(
132            Force::Custom(
133                Mass::Custom(Self::PLANCK_CONSTANT, Box::new(Mass::Kg)),
134                Length::Meter,
135                Time::Second,
136            ),
137            Length::Meter,
138        )
139        .convert_to(unit)
140    }
141
142    pub fn reduced_planck_constant(unit: Energy) -> Energy {
143        Energy::Custom(
144            Force::Custom(
145                Mass::Custom(Self::REDUCED_PLANCK_CONSTANT, Box::new(Mass::Kg)),
146                Length::Meter,
147                Time::Second,
148            ),
149            Length::Meter,
150        )
151        .convert_to(unit)
152    }
153
154    pub fn boltzmann_constant(unit: Energy) -> Energy {
155        Energy::Custom(
156            Force::Custom(
157                Mass::Custom(Self::BOLTZMANN_CONSTANT, Box::new(Mass::Kg)),
158                Length::Meter,
159                Time::Second,
160            ),
161            Length::Meter,
162        )
163        .convert_to(unit)
164    }
165
166    pub fn elementary_charge(unit: ElectricCharge) -> ElectricCharge {
167        ElectricCharge::Custom(
168            Box::new(Current::Custom(
169                ElectricCharge::Coulomb,
170                Time::Custom(Self::ELEMENTARY_CHARGE, Box::new(Time::Second)),
171            )),
172            Time::Second,
173        )
174        .convert_to(unit)
175    }
176
177    pub fn electron_mass(unit: Mass) -> Mass {
178        Mass::Custom(Self::ELECTRON_MASS, Box::new(Mass::Kg)).convert_to(unit)
179    }
180
181    pub fn proton_mass(unit: Mass) -> Mass {
182        Mass::Custom(Self::PROTON_MASS, Box::new(Mass::Kg)).convert_to(unit)
183    }
184
185    pub fn avogadro_constant(unit: Frequency) -> Frequency {
186        Frequency::Custom(Time::Custom(
187            1.0 / Self::AVOGADRO_CONSTANT,
188            Box::new(Time::Second),
189        ))
190        .convert_to(unit)
191    }
192
193    pub fn fine_structure_constant() -> f64 {
194        Self::FINE_STRUCTURE_CONSTANT
195    }
196
197    pub fn rydberg_constant(unit: Frequency) -> Frequency {
198        Frequency::Custom(Time::Custom(Self::RYDBERG_CONSTANT, Box::new(Time::Second)))
199            .convert_to(unit)
200    }
201
202    pub fn atomic_mass_unit(unit: Mass) -> Mass {
203        Mass::Custom(Self::ATOMIC_MASS_UNIT, Box::new(Mass::Kg)).convert_to(unit)
204    }
205
206    pub fn faraday_constant(unit: ElectricCharge) -> ElectricCharge {
207        ElectricCharge::Custom(
208            Box::new(Current::Custom(
209                ElectricCharge::Coulomb,
210                Time::Custom(Self::FARADAY_CONSTANT, Box::new(Time::Second)),
211            )),
212            Time::Second,
213        )
214        .convert_to(unit)
215    }
216
217    pub fn gas_constant(unit: Energy) -> Energy {
218        Energy::Custom(
219            Force::Custom(
220                Mass::Custom(Self::GAS_CONSTANT, Box::new(Mass::Kg)),
221                Length::Meter,
222                Time::Second,
223            ),
224            Length::Meter,
225        )
226        .convert_to(unit)
227    }
228}