Coverage for install/scipp/constants/__init__.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-04-28 01:28 +0000

1# ruff: noqa: E501 

2r""" 

3Physical and mathematical constants with units. 

4This module a wrapper around `scipy.constants <https://docs.scipy.org/doc/scipy/reference/constants.html>`_ 

5and requires the ``scipy`` package to be installed. 

6 

7Mathematical constants: 

8 

9================ ================================================================= 

10``pi`` Pi 

11``golden`` Golden ratio 

12``golden_ratio`` Golden ratio 

13================ ================================================================= 

14 

15Physical constants: 

16 

17=========================== ============================================= 

18``c`` speed of light in vacuum 

19``speed_of_light`` speed of light in vacuum 

20``mu_0`` the magnetic constant :math:`\mu_0` 

21``epsilon_0`` the electric constant (vacuum permittivity), 

22 :math:`\epsilon_0` 

23``h`` the Planck constant :math:`h` 

24``Planck`` the Planck constant :math:`h` 

25``hbar`` :math:`\hbar = h/(2\pi)` 

26``G`` Newtonian constant of gravitation 

27``gravitational_constant`` Newtonian constant of gravitation 

28``g`` standard acceleration of gravity 

29``e`` elementary charge 

30``elementary_charge`` elementary charge 

31``R`` molar gas constant 

32``gas_constant`` molar gas constant 

33``alpha`` fine-structure constant 

34``fine_structure`` fine-structure constant 

35``N_A`` Avogadro constant 

36``Avogadro`` Avogadro constant 

37``k`` Boltzmann constant 

38``Boltzmann`` Boltzmann constant 

39``sigma`` Stefan-Boltzmann constant :math:`\sigma` 

40``Stefan_Boltzmann`` Stefan-Boltzmann constant :math:`\sigma` 

41``Wien`` Wien displacement law constant 

42``Rydberg`` Rydberg constant 

43``m_e`` electron mass 

44``electron_mass`` electron mass 

45``m_p`` proton mass 

46``proton_mass`` proton mass 

47``m_n`` neutron mass 

48``neutron_mass`` neutron mass 

49=========================== ============================================= 

50 

51In addition to the above variables, :mod:`scipp.constants` also contains the 

522018 CODATA recommended values [CODATA2018]_ database containing more physical 

53constants. 

54The database is accessed using :py:func:`scipp.constants.physical_constants`. 

55 

56.. [CODATA2018] CODATA Recommended Values of the Fundamental 

57 Physical Constants 2018. 

58 https://physics.nist.gov/cuu/Constants/ 

59""" 

60 

61import math as _math 

62 

63from .. import Variable, as_const, scalar 

64 

65 

66def physical_constants(key: str, with_variance: bool = False) -> Variable: 

67 """ 

68 Returns the CODATA recommended value with unit of the requested physical constant. 

69 

70 :param key: Key of the requested constant. See `scipy.constants.physical_constants <https://docs.scipy.org/doc/scipy/reference/constants.html#scipy.constants.physical_constants>`_ for an overview. 

71 :param with_variance: Optional, if True, the uncertainty if the constant is 

72 included as the variance. Default is False. 

73 :returns: Scalar variable with unit and optional variance. 

74 :rtype: Variable 

75 """ 

76 from scipy.constants import physical_constants as _cd 

77 

78 value, unit, uncertainty = _cd[key] 

79 args = {'value': value, 'unit': unit.replace(' ', '*')} 

80 if with_variance: 

81 stddev = uncertainty 

82 args['variance'] = stddev * stddev 

83 return as_const(scalar(**args)) 

84 

85 

86# mathematical constants 

87pi = scalar(_math.pi) 

88golden = golden_ratio = scalar((1 + _math.sqrt(5)) / 2) 

89 

90# physical constants 

91c = speed_of_light = physical_constants('speed of light in vacuum') 

92mu_0 = physical_constants('vacuum mag. permeability') 

93epsilon_0 = physical_constants('vacuum electric permittivity') 

94h = Planck = physical_constants('Planck constant') 

95hbar = h / (2 * pi) 

96G = gravitational_constant = physical_constants('Newtonian constant of gravitation') 

97g = physical_constants('standard acceleration of gravity') 

98e = elementary_charge = physical_constants('elementary charge') 

99R = gas_constant = physical_constants('molar gas constant') 

100alpha = fine_structure = physical_constants('fine-structure constant') 

101N_A = Avogadro = physical_constants('Avogadro constant') 

102k = Boltzmann = physical_constants('Boltzmann constant') 

103sigma = Stefan_Boltzmann = physical_constants('Stefan-Boltzmann constant') 

104Wien = physical_constants('Wien wavelength displacement law constant') 

105Rydberg = physical_constants('Rydberg constant') 

106 

107m_e = electron_mass = physical_constants('electron mass') 

108m_p = proton_mass = physical_constants('proton mass') 

109m_n = neutron_mass = physical_constants('neutron mass') 

110m_u = u = atomic_mass = physical_constants('atomic mass constant')