Coverage for src/algolib/physics/constants.py: 100%
24 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-20 19:37 +0000
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-20 19:37 +0000
1# algolib/physics/constants.py
2"""
3CODATA-based physical constants in SI units.
5Notes
6-----
7- All values are provided in SI base units.
8- `rel_unc` is the relative standard uncertainty. A value of `0.0` indicates an
9 exact value as defined by the 2019 SI redefinition (e.g., `c`, `h`, `e`,
10 `k_B`, `N_A`).
11- Source label reflects the CODATA release used to curate the numbers.
12"""
14CODATA_VINTAGE = "CODATA 2022"
16from dataclasses import dataclass
18@dataclass(frozen=True, slots=True)
19class PhysConst:
20 """Container for a physical constant in SI units.
21 Parameters
22 ----------
23 value : float
24 Numerical value in SI base units.
25 unit : str
26 Unit string, e.g. "m s^-1", "J s", "C".
27 rel_unc : float | None
28 Relative standard uncertainty (e.g. ``1.5e-10``); use ``0.0`` for
29 exact constants per the 2019 SI definitions.
30 source : str
31 Provenance label, e.g. "CODATA 2022".
32 symbol : str
33 Conventional symbol such as ``c``, ``h``, ``k_B``.
34 """
35 value: float # numerical value (SI base units)
36 unit: str # unit string, e.g. "m s^-1"
37 rel_unc: float | None # relative uncertainty (0.0 for exact constants)
38 source: str # provenance label, e.g. "CODATA 2022"
39 symbol: str # conventional symbol ("c", "h", etc.)
43# — SI defining constants (exact since the 2019 redefinition) —
45c = PhysConst(299792458, "m s^-1", 0.0, "CODATA 2022", "c")
46h = PhysConst(6.62607015e-34, "J s", 0.0, "CODATA 2022", "h")
47e = PhysConst(1.602176634e-19, "C", 0.0, "CODATA 2022", "e")
48k_B = PhysConst(1.380649e-23, "J K^-1", 0.0, "CODATA 2022", "k_B")
49N_A = PhysConst(6.02214076e23, "mol^-1", 0.0, "CODATA 2022", "N_A")
51# Common derived/experimental constants (some with nonzero uncertainty; values shown as examples)
52hbar = PhysConst(1.054571817e-34, "J s", 0.0, "CODATA 2022", "ħ") # derived as h / (2π); exact by definition
53R = PhysConst(8.314462618, "J mol^-1 K^-1", 0.0, "CODATA 2022", "R") # R = N_A * k_B (exact by definition)
55# Examples with nonzero uncertainties (values are placeholders until a CODATA vintage is chosen)
56alpha = PhysConst(7.2973525643e-3, "1", 1.6e-10, "CODATA 2022", "α")
57m_e = PhysConst(9.1093837139e-31, "kg", 3.1e-10, "CODATA 2022", "m_e")
58m_p = PhysConst(1.67262192595e-27, "kg", 3.1e-10, "CODATA 2022", "m_p")
59m_n = PhysConst(1.67492750056e-27, "kg", 5.1e-10, "CODATA 2022", "m_n")
60m_u = PhysConst(1.66053906892e-27, "kg", 3.1e-10, "CODATA 2022", "m_u") # defined as 1/12 of the mass of a 12C atom (not exact after 2019 SI)
62R_inf = PhysConst(1.0973731568157e7, "m^-1", 1.1e-12, "CODATA 2022", "R_∞")
63a0 = PhysConst(5.29177210544e-11, "m", 1.6e-10, "CODATA 2022", "a0")
64sigmaSB = PhysConst(5.670374419e-8, "W m^-2 K^-4", 1.5e-6, "CODATA 2022", "σ")
66# Note: After the 2019 SI redefinition, μ0 and ε0 are not exact. Prefer deriving them from α, h, c, and e; do not hard-code as exact.