constants.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """
  2. Collection of physical constants and conversion factors.
  3. Most constants are in SI units, so you can do
  4. print '10 mile per minute is', 10*mile/minute, 'm/s or', 10*mile/(minute*knot), 'knots'
  5. The list is not meant to be comprehensive, but just a convenient list for everyday use.
  6. """
  7. from __future__ import division, print_function, absolute_import
  8. """
  9. BasSw 2006
  10. physical constants: imported from CODATA
  11. unit conversion: see e.g. NIST special publication 811
  12. Use at own risk: double-check values before calculating your Mars orbit-insertion burn.
  13. Some constants exist in a few variants, which are marked with suffixes.
  14. The ones without any suffix should be the most common one.
  15. """
  16. import math as _math
  17. from .codata import value as _cd
  18. import numpy as _np
  19. # mathematical constants
  20. pi = _math.pi
  21. golden = golden_ratio = (1 + _math.sqrt(5)) / 2
  22. # SI prefixes
  23. yotta = 1e24
  24. zetta = 1e21
  25. exa = 1e18
  26. peta = 1e15
  27. tera = 1e12
  28. giga = 1e9
  29. mega = 1e6
  30. kilo = 1e3
  31. hecto = 1e2
  32. deka = 1e1
  33. deci = 1e-1
  34. centi = 1e-2
  35. milli = 1e-3
  36. micro = 1e-6
  37. nano = 1e-9
  38. pico = 1e-12
  39. femto = 1e-15
  40. atto = 1e-18
  41. zepto = 1e-21
  42. # binary prefixes
  43. kibi = 2**10
  44. mebi = 2**20
  45. gibi = 2**30
  46. tebi = 2**40
  47. pebi = 2**50
  48. exbi = 2**60
  49. zebi = 2**70
  50. yobi = 2**80
  51. # physical constants
  52. c = speed_of_light = _cd('speed of light in vacuum')
  53. mu_0 = 4e-7*pi
  54. epsilon_0 = 1 / (mu_0*c*c)
  55. h = Planck = _cd('Planck constant')
  56. hbar = h / (2 * pi)
  57. G = gravitational_constant = _cd('Newtonian constant of gravitation')
  58. g = _cd('standard acceleration of gravity')
  59. e = elementary_charge = _cd('elementary charge')
  60. R = gas_constant = _cd('molar gas constant')
  61. alpha = fine_structure = _cd('fine-structure constant')
  62. N_A = Avogadro = _cd('Avogadro constant')
  63. k = Boltzmann = _cd('Boltzmann constant')
  64. sigma = Stefan_Boltzmann = _cd('Stefan-Boltzmann constant')
  65. Wien = _cd('Wien wavelength displacement law constant')
  66. Rydberg = _cd('Rydberg constant')
  67. # mass in kg
  68. gram = 1e-3
  69. metric_ton = 1e3
  70. grain = 64.79891e-6
  71. lb = pound = 7000 * grain # avoirdupois
  72. blob = slinch = pound * g / 0.0254 # lbf*s**2/in (added in 1.0.0)
  73. slug = blob / 12 # lbf*s**2/foot (added in 1.0.0)
  74. oz = ounce = pound / 16
  75. stone = 14 * pound
  76. long_ton = 2240 * pound
  77. short_ton = 2000 * pound
  78. troy_ounce = 480 * grain # only for metals / gems
  79. troy_pound = 12 * troy_ounce
  80. carat = 200e-6
  81. m_e = electron_mass = _cd('electron mass')
  82. m_p = proton_mass = _cd('proton mass')
  83. m_n = neutron_mass = _cd('neutron mass')
  84. m_u = u = atomic_mass = _cd('atomic mass constant')
  85. # angle in rad
  86. degree = pi / 180
  87. arcmin = arcminute = degree / 60
  88. arcsec = arcsecond = arcmin / 60
  89. # time in second
  90. minute = 60.0
  91. hour = 60 * minute
  92. day = 24 * hour
  93. week = 7 * day
  94. year = 365 * day
  95. Julian_year = 365.25 * day
  96. # length in meter
  97. inch = 0.0254
  98. foot = 12 * inch
  99. yard = 3 * foot
  100. mile = 1760 * yard
  101. mil = inch / 1000
  102. pt = point = inch / 72 # typography
  103. survey_foot = 1200.0 / 3937
  104. survey_mile = 5280 * survey_foot
  105. nautical_mile = 1852.0
  106. fermi = 1e-15
  107. angstrom = 1e-10
  108. micron = 1e-6
  109. au = astronomical_unit = 149597870691.0
  110. light_year = Julian_year * c
  111. parsec = au / arcsec
  112. # pressure in pascal
  113. atm = atmosphere = _cd('standard atmosphere')
  114. bar = 1e5
  115. torr = mmHg = atm / 760
  116. psi = pound * g / (inch * inch)
  117. # area in meter**2
  118. hectare = 1e4
  119. acre = 43560 * foot**2
  120. # volume in meter**3
  121. litre = liter = 1e-3
  122. gallon = gallon_US = 231 * inch**3 # US
  123. # pint = gallon_US / 8
  124. fluid_ounce = fluid_ounce_US = gallon_US / 128
  125. bbl = barrel = 42 * gallon_US # for oil
  126. gallon_imp = 4.54609e-3 # UK
  127. fluid_ounce_imp = gallon_imp / 160
  128. # speed in meter per second
  129. kmh = 1e3 / hour
  130. mph = mile / hour
  131. mach = speed_of_sound = 340.5 # approx value at 15 degrees in 1 atm. is this a common value?
  132. knot = nautical_mile / hour
  133. # temperature in kelvin
  134. zero_Celsius = 273.15
  135. degree_Fahrenheit = 1/1.8 # only for differences
  136. # energy in joule
  137. eV = electron_volt = elementary_charge # * 1 Volt
  138. calorie = calorie_th = 4.184
  139. calorie_IT = 4.1868
  140. erg = 1e-7
  141. Btu_th = pound * degree_Fahrenheit * calorie_th / gram
  142. Btu = Btu_IT = pound * degree_Fahrenheit * calorie_IT / gram
  143. ton_TNT = 1e9 * calorie_th
  144. # Wh = watt_hour
  145. # power in watt
  146. hp = horsepower = 550 * foot * pound * g
  147. # force in newton
  148. dyn = dyne = 1e-5
  149. lbf = pound_force = pound * g
  150. kgf = kilogram_force = g # * 1 kg
  151. # functions for conversions that are not linear
  152. def convert_temperature(val, old_scale, new_scale):
  153. """
  154. Convert from a temperature scale to another one among Celsius, Kelvin,
  155. Fahrenheit and Rankine scales.
  156. Parameters
  157. ----------
  158. val : array_like
  159. Value(s) of the temperature(s) to be converted expressed in the
  160. original scale.
  161. old_scale: str
  162. Specifies as a string the original scale from which the temperature
  163. value(s) will be converted. Supported scales are Celsius ('Celsius',
  164. 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
  165. Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f') and Rankine
  166. ('Rankine', 'rankine', 'R', 'r').
  167. new_scale: str
  168. Specifies as a string the new scale to which the temperature
  169. value(s) will be converted. Supported scales are Celsius ('Celsius',
  170. 'celsius', 'C' or 'c'), Kelvin ('Kelvin', 'kelvin', 'K', 'k'),
  171. Fahrenheit ('Fahrenheit', 'fahrenheit', 'F' or 'f') and Rankine
  172. ('Rankine', 'rankine', 'R', 'r').
  173. Returns
  174. -------
  175. res : float or array of floats
  176. Value(s) of the converted temperature(s) expressed in the new scale.
  177. Notes
  178. -----
  179. .. versionadded:: 0.18.0
  180. Examples
  181. --------
  182. >>> from scipy.constants import convert_temperature
  183. >>> convert_temperature(np.array([-40, 40.0]), 'Celsius', 'Kelvin')
  184. array([ 233.15, 313.15])
  185. """
  186. # Convert from `old_scale` to Kelvin
  187. if old_scale.lower() in ['celsius', 'c']:
  188. tempo = _np.asanyarray(val) + zero_Celsius
  189. elif old_scale.lower() in ['kelvin', 'k']:
  190. tempo = _np.asanyarray(val)
  191. elif old_scale.lower() in ['fahrenheit', 'f']:
  192. tempo = (_np.asanyarray(val) - 32.) * 5. / 9. + zero_Celsius
  193. elif old_scale.lower() in ['rankine', 'r']:
  194. tempo = _np.asanyarray(val) * 5. / 9.
  195. else:
  196. raise NotImplementedError("%s scale is unsupported: supported scales "
  197. "are Celsius, Kelvin, Fahrenheit and "
  198. "Rankine" % old_scale)
  199. # and from Kelvin to `new_scale`.
  200. if new_scale.lower() in ['celsius', 'c']:
  201. res = tempo - zero_Celsius
  202. elif new_scale.lower() in ['kelvin', 'k']:
  203. res = tempo
  204. elif new_scale.lower() in ['fahrenheit', 'f']:
  205. res = (tempo - zero_Celsius) * 9. / 5. + 32.
  206. elif new_scale.lower() in ['rankine', 'r']:
  207. res = tempo * 9. / 5.
  208. else:
  209. raise NotImplementedError("'%s' scale is unsupported: supported "
  210. "scales are 'Celsius', 'Kelvin', "
  211. "'Fahrenheit' and 'Rankine'" % new_scale)
  212. return res
  213. # optics
  214. def lambda2nu(lambda_):
  215. """
  216. Convert wavelength to optical frequency
  217. Parameters
  218. ----------
  219. lambda_ : array_like
  220. Wavelength(s) to be converted.
  221. Returns
  222. -------
  223. nu : float or array of floats
  224. Equivalent optical frequency.
  225. Notes
  226. -----
  227. Computes ``nu = c / lambda`` where c = 299792458.0, i.e., the
  228. (vacuum) speed of light in meters/second.
  229. Examples
  230. --------
  231. >>> from scipy.constants import lambda2nu, speed_of_light
  232. >>> lambda2nu(np.array((1, speed_of_light)))
  233. array([ 2.99792458e+08, 1.00000000e+00])
  234. """
  235. return _np.asanyarray(c) / lambda_
  236. def nu2lambda(nu):
  237. """
  238. Convert optical frequency to wavelength.
  239. Parameters
  240. ----------
  241. nu : array_like
  242. Optical frequency to be converted.
  243. Returns
  244. -------
  245. lambda : float or array of floats
  246. Equivalent wavelength(s).
  247. Notes
  248. -----
  249. Computes ``lambda = c / nu`` where c = 299792458.0, i.e., the
  250. (vacuum) speed of light in meters/second.
  251. Examples
  252. --------
  253. >>> from scipy.constants import nu2lambda, speed_of_light
  254. >>> nu2lambda(np.array((1, speed_of_light)))
  255. array([ 2.99792458e+08, 1.00000000e+00])
  256. """
  257. return c / _np.asanyarray(nu)