unit_definitions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. from sympy.physics.units.definitions.dimension_definitions import current, temperature, amount_of_substance, \
  2. luminous_intensity, angle, charge, voltage, impedance, conductance, capacitance, inductance, magnetic_density, \
  3. magnetic_flux, information
  4. from sympy.core.numbers import (Rational, pi)
  5. from sympy.core.singleton import S as S_singleton
  6. from sympy.physics.units.prefixes import kilo, mega, milli, micro, deci, centi, nano, pico, kibi, mebi, gibi, tebi, pebi, exbi
  7. from sympy.physics.units.quantities import PhysicalConstant, Quantity
  8. One = S_singleton.One
  9. #### UNITS ####
  10. # Dimensionless:
  11. percent = percents = Quantity("percent", latex_repr=r"\%")
  12. percent.set_global_relative_scale_factor(Rational(1, 100), One)
  13. permille = Quantity("permille")
  14. permille.set_global_relative_scale_factor(Rational(1, 1000), One)
  15. # Angular units (dimensionless)
  16. rad = radian = radians = Quantity("radian", abbrev="rad")
  17. radian.set_global_dimension(angle)
  18. deg = degree = degrees = Quantity("degree", abbrev="deg", latex_repr=r"^\circ")
  19. degree.set_global_relative_scale_factor(pi/180, radian)
  20. sr = steradian = steradians = Quantity("steradian", abbrev="sr")
  21. mil = angular_mil = angular_mils = Quantity("angular_mil", abbrev="mil")
  22. # Base units:
  23. m = meter = meters = Quantity("meter", abbrev="m")
  24. # gram; used to define its prefixed units
  25. g = gram = grams = Quantity("gram", abbrev="g")
  26. # NOTE: the `kilogram` has scale factor 1000. In SI, kg is a base unit, but
  27. # nonetheless we are trying to be compatible with the `kilo` prefix. In a
  28. # similar manner, people using CGS or gaussian units could argue that the
  29. # `centimeter` rather than `meter` is the fundamental unit for length, but the
  30. # scale factor of `centimeter` will be kept as 1/100 to be compatible with the
  31. # `centi` prefix. The current state of the code assumes SI unit dimensions, in
  32. # the future this module will be modified in order to be unit system-neutral
  33. # (that is, support all kinds of unit systems).
  34. kg = kilogram = kilograms = Quantity("kilogram", abbrev="kg")
  35. kg.set_global_relative_scale_factor(kilo, gram)
  36. s = second = seconds = Quantity("second", abbrev="s")
  37. A = ampere = amperes = Quantity("ampere", abbrev='A')
  38. ampere.set_global_dimension(current)
  39. K = kelvin = kelvins = Quantity("kelvin", abbrev='K')
  40. kelvin.set_global_dimension(temperature)
  41. mol = mole = moles = Quantity("mole", abbrev="mol")
  42. mole.set_global_dimension(amount_of_substance)
  43. cd = candela = candelas = Quantity("candela", abbrev="cd")
  44. candela.set_global_dimension(luminous_intensity)
  45. # derived units
  46. newton = newtons = N = Quantity("newton", abbrev="N")
  47. kilonewton = kilonewtons = kN = Quantity("kilonewton", abbrev="kN")
  48. kilonewton.set_global_relative_scale_factor(kilo, newton)
  49. meganewton = meganewtons = MN = Quantity("meganewton", abbrev="MN")
  50. meganewton.set_global_relative_scale_factor(mega, newton)
  51. joule = joules = J = Quantity("joule", abbrev="J")
  52. watt = watts = W = Quantity("watt", abbrev="W")
  53. pascal = pascals = Pa = pa = Quantity("pascal", abbrev="Pa")
  54. hertz = hz = Hz = Quantity("hertz", abbrev="Hz")
  55. # CGS derived units:
  56. dyne = Quantity("dyne")
  57. dyne.set_global_relative_scale_factor(One/10**5, newton)
  58. erg = Quantity("erg")
  59. erg.set_global_relative_scale_factor(One/10**7, joule)
  60. # MKSA extension to MKS: derived units
  61. coulomb = coulombs = C = Quantity("coulomb", abbrev='C')
  62. coulomb.set_global_dimension(charge)
  63. volt = volts = v = V = Quantity("volt", abbrev='V')
  64. volt.set_global_dimension(voltage)
  65. ohm = ohms = Quantity("ohm", abbrev='ohm', latex_repr=r"\Omega")
  66. ohm.set_global_dimension(impedance)
  67. siemens = S = mho = mhos = Quantity("siemens", abbrev='S')
  68. siemens.set_global_dimension(conductance)
  69. farad = farads = F = Quantity("farad", abbrev='F')
  70. farad.set_global_dimension(capacitance)
  71. henry = henrys = H = Quantity("henry", abbrev='H')
  72. henry.set_global_dimension(inductance)
  73. tesla = teslas = T = Quantity("tesla", abbrev='T')
  74. tesla.set_global_dimension(magnetic_density)
  75. weber = webers = Wb = wb = Quantity("weber", abbrev='Wb')
  76. weber.set_global_dimension(magnetic_flux)
  77. # CGS units for electromagnetic quantities:
  78. statampere = Quantity("statampere")
  79. statcoulomb = statC = franklin = Quantity("statcoulomb", abbrev="statC")
  80. statvolt = Quantity("statvolt")
  81. gauss = Quantity("gauss")
  82. maxwell = Quantity("maxwell")
  83. debye = Quantity("debye")
  84. oersted = Quantity("oersted")
  85. # Other derived units:
  86. optical_power = dioptre = diopter = D = Quantity("dioptre")
  87. lux = lx = Quantity("lux", abbrev="lx")
  88. # katal is the SI unit of catalytic activity
  89. katal = kat = Quantity("katal", abbrev="kat")
  90. # gray is the SI unit of absorbed dose
  91. gray = Gy = Quantity("gray")
  92. # becquerel is the SI unit of radioactivity
  93. becquerel = Bq = Quantity("becquerel", abbrev="Bq")
  94. # Common mass units
  95. mg = milligram = milligrams = Quantity("milligram", abbrev="mg")
  96. mg.set_global_relative_scale_factor(milli, gram)
  97. ug = microgram = micrograms = Quantity("microgram", abbrev="ug", latex_repr=r"\mu\text{g}")
  98. ug.set_global_relative_scale_factor(micro, gram)
  99. # Atomic mass constant
  100. Da = dalton = amu = amus = atomic_mass_unit = atomic_mass_constant = PhysicalConstant("atomic_mass_constant")
  101. t = metric_ton = tonne = Quantity("tonne", abbrev="t")
  102. tonne.set_global_relative_scale_factor(mega, gram)
  103. # Electron rest mass
  104. me = electron_rest_mass = Quantity("electron_rest_mass", abbrev="me")
  105. # Common length units
  106. km = kilometer = kilometers = Quantity("kilometer", abbrev="km")
  107. km.set_global_relative_scale_factor(kilo, meter)
  108. dm = decimeter = decimeters = Quantity("decimeter", abbrev="dm")
  109. dm.set_global_relative_scale_factor(deci, meter)
  110. cm = centimeter = centimeters = Quantity("centimeter", abbrev="cm")
  111. cm.set_global_relative_scale_factor(centi, meter)
  112. mm = millimeter = millimeters = Quantity("millimeter", abbrev="mm")
  113. mm.set_global_relative_scale_factor(milli, meter)
  114. um = micrometer = micrometers = micron = microns = \
  115. Quantity("micrometer", abbrev="um", latex_repr=r'\mu\text{m}')
  116. um.set_global_relative_scale_factor(micro, meter)
  117. nm = nanometer = nanometers = Quantity("nanometer", abbrev="nm")
  118. nm.set_global_relative_scale_factor(nano, meter)
  119. pm = picometer = picometers = Quantity("picometer", abbrev="pm")
  120. pm.set_global_relative_scale_factor(pico, meter)
  121. ft = foot = feet = Quantity("foot", abbrev="ft")
  122. ft.set_global_relative_scale_factor(Rational(3048, 10000), meter)
  123. inch = inches = Quantity("inch")
  124. inch.set_global_relative_scale_factor(Rational(1, 12), foot)
  125. yd = yard = yards = Quantity("yard", abbrev="yd")
  126. yd.set_global_relative_scale_factor(3, feet)
  127. mi = mile = miles = Quantity("mile")
  128. mi.set_global_relative_scale_factor(5280, feet)
  129. nmi = nautical_mile = nautical_miles = Quantity("nautical_mile")
  130. nmi.set_global_relative_scale_factor(6076, feet)
  131. angstrom = angstroms = Quantity("angstrom", latex_repr=r'\r{A}')
  132. angstrom.set_global_relative_scale_factor(Rational(1, 10**10), meter)
  133. # Common volume and area units
  134. ha = hectare = Quantity("hectare", abbrev="ha")
  135. l = L = liter = liters = Quantity("liter", abbrev="l")
  136. dl = dL = deciliter = deciliters = Quantity("deciliter", abbrev="dl")
  137. dl.set_global_relative_scale_factor(Rational(1, 10), liter)
  138. cl = cL = centiliter = centiliters = Quantity("centiliter", abbrev="cl")
  139. cl.set_global_relative_scale_factor(Rational(1, 100), liter)
  140. ml = mL = milliliter = milliliters = Quantity("milliliter", abbrev="ml")
  141. ml.set_global_relative_scale_factor(Rational(1, 1000), liter)
  142. # Common time units
  143. ms = millisecond = milliseconds = Quantity("millisecond", abbrev="ms")
  144. millisecond.set_global_relative_scale_factor(milli, second)
  145. us = microsecond = microseconds = Quantity("microsecond", abbrev="us", latex_repr=r'\mu\text{s}')
  146. microsecond.set_global_relative_scale_factor(micro, second)
  147. ns = nanosecond = nanoseconds = Quantity("nanosecond", abbrev="ns")
  148. nanosecond.set_global_relative_scale_factor(nano, second)
  149. ps = picosecond = picoseconds = Quantity("picosecond", abbrev="ps")
  150. picosecond.set_global_relative_scale_factor(pico, second)
  151. minute = minutes = Quantity("minute")
  152. minute.set_global_relative_scale_factor(60, second)
  153. h = hour = hours = Quantity("hour")
  154. hour.set_global_relative_scale_factor(60, minute)
  155. day = days = Quantity("day")
  156. day.set_global_relative_scale_factor(24, hour)
  157. anomalistic_year = anomalistic_years = Quantity("anomalistic_year")
  158. anomalistic_year.set_global_relative_scale_factor(365.259636, day)
  159. sidereal_year = sidereal_years = Quantity("sidereal_year")
  160. sidereal_year.set_global_relative_scale_factor(31558149.540, seconds)
  161. tropical_year = tropical_years = Quantity("tropical_year")
  162. tropical_year.set_global_relative_scale_factor(365.24219, day)
  163. common_year = common_years = Quantity("common_year")
  164. common_year.set_global_relative_scale_factor(365, day)
  165. julian_year = julian_years = Quantity("julian_year")
  166. julian_year.set_global_relative_scale_factor((365 + One/4), day)
  167. draconic_year = draconic_years = Quantity("draconic_year")
  168. draconic_year.set_global_relative_scale_factor(346.62, day)
  169. gaussian_year = gaussian_years = Quantity("gaussian_year")
  170. gaussian_year.set_global_relative_scale_factor(365.2568983, day)
  171. full_moon_cycle = full_moon_cycles = Quantity("full_moon_cycle")
  172. full_moon_cycle.set_global_relative_scale_factor(411.78443029, day)
  173. year = years = tropical_year
  174. #### CONSTANTS ####
  175. # Newton constant
  176. G = gravitational_constant = PhysicalConstant("gravitational_constant", abbrev="G")
  177. # speed of light
  178. c = speed_of_light = PhysicalConstant("speed_of_light", abbrev="c")
  179. # elementary charge
  180. elementary_charge = PhysicalConstant("elementary_charge", abbrev="e")
  181. # Planck constant
  182. planck = PhysicalConstant("planck", abbrev="h")
  183. # Reduced Planck constant
  184. hbar = PhysicalConstant("hbar", abbrev="hbar")
  185. # Electronvolt
  186. eV = electronvolt = electronvolts = PhysicalConstant("electronvolt", abbrev="eV")
  187. # Avogadro number
  188. avogadro_number = PhysicalConstant("avogadro_number")
  189. # Avogadro constant
  190. avogadro = avogadro_constant = PhysicalConstant("avogadro_constant")
  191. # Boltzmann constant
  192. boltzmann = boltzmann_constant = PhysicalConstant("boltzmann_constant")
  193. # Stefan-Boltzmann constant
  194. stefan = stefan_boltzmann_constant = PhysicalConstant("stefan_boltzmann_constant")
  195. # Molar gas constant
  196. R = molar_gas_constant = PhysicalConstant("molar_gas_constant", abbrev="R")
  197. # Faraday constant
  198. faraday_constant = PhysicalConstant("faraday_constant")
  199. # Josephson constant
  200. josephson_constant = PhysicalConstant("josephson_constant", abbrev="K_j")
  201. # Von Klitzing constant
  202. von_klitzing_constant = PhysicalConstant("von_klitzing_constant", abbrev="R_k")
  203. # Acceleration due to gravity (on the Earth surface)
  204. gee = gees = acceleration_due_to_gravity = PhysicalConstant("acceleration_due_to_gravity", abbrev="g")
  205. # magnetic constant:
  206. u0 = magnetic_constant = vacuum_permeability = PhysicalConstant("magnetic_constant")
  207. # electric constat:
  208. e0 = electric_constant = vacuum_permittivity = PhysicalConstant("vacuum_permittivity")
  209. # vacuum impedance:
  210. Z0 = vacuum_impedance = PhysicalConstant("vacuum_impedance", abbrev='Z_0', latex_repr=r'Z_{0}')
  211. # Coulomb's constant:
  212. coulomb_constant = coulombs_constant = electric_force_constant = \
  213. PhysicalConstant("coulomb_constant", abbrev="k_e")
  214. atmosphere = atmospheres = atm = Quantity("atmosphere", abbrev="atm")
  215. kPa = kilopascal = Quantity("kilopascal", abbrev="kPa")
  216. kilopascal.set_global_relative_scale_factor(kilo, Pa)
  217. bar = bars = Quantity("bar", abbrev="bar")
  218. pound = pounds = Quantity("pound") # exact
  219. psi = Quantity("psi")
  220. dHg0 = 13.5951 # approx value at 0 C
  221. mmHg = torr = Quantity("mmHg")
  222. atmosphere.set_global_relative_scale_factor(101325, pascal)
  223. bar.set_global_relative_scale_factor(100, kPa)
  224. pound.set_global_relative_scale_factor(Rational(45359237, 100000000), kg)
  225. mmu = mmus = milli_mass_unit = Quantity("milli_mass_unit")
  226. quart = quarts = Quantity("quart")
  227. # Other convenient units and magnitudes
  228. ly = lightyear = lightyears = Quantity("lightyear", abbrev="ly")
  229. au = astronomical_unit = astronomical_units = Quantity("astronomical_unit", abbrev="AU")
  230. # Fundamental Planck units:
  231. planck_mass = Quantity("planck_mass", abbrev="m_P", latex_repr=r'm_\text{P}')
  232. planck_time = Quantity("planck_time", abbrev="t_P", latex_repr=r't_\text{P}')
  233. planck_temperature = Quantity("planck_temperature", abbrev="T_P",
  234. latex_repr=r'T_\text{P}')
  235. planck_length = Quantity("planck_length", abbrev="l_P", latex_repr=r'l_\text{P}')
  236. planck_charge = Quantity("planck_charge", abbrev="q_P", latex_repr=r'q_\text{P}')
  237. # Derived Planck units:
  238. planck_area = Quantity("planck_area")
  239. planck_volume = Quantity("planck_volume")
  240. planck_momentum = Quantity("planck_momentum")
  241. planck_energy = Quantity("planck_energy", abbrev="E_P", latex_repr=r'E_\text{P}')
  242. planck_force = Quantity("planck_force", abbrev="F_P", latex_repr=r'F_\text{P}')
  243. planck_power = Quantity("planck_power", abbrev="P_P", latex_repr=r'P_\text{P}')
  244. planck_density = Quantity("planck_density", abbrev="rho_P", latex_repr=r'\rho_\text{P}')
  245. planck_energy_density = Quantity("planck_energy_density", abbrev="rho^E_P")
  246. planck_intensity = Quantity("planck_intensity", abbrev="I_P", latex_repr=r'I_\text{P}')
  247. planck_angular_frequency = Quantity("planck_angular_frequency", abbrev="omega_P",
  248. latex_repr=r'\omega_\text{P}')
  249. planck_pressure = Quantity("planck_pressure", abbrev="p_P", latex_repr=r'p_\text{P}')
  250. planck_current = Quantity("planck_current", abbrev="I_P", latex_repr=r'I_\text{P}')
  251. planck_voltage = Quantity("planck_voltage", abbrev="V_P", latex_repr=r'V_\text{P}')
  252. planck_impedance = Quantity("planck_impedance", abbrev="Z_P", latex_repr=r'Z_\text{P}')
  253. planck_acceleration = Quantity("planck_acceleration", abbrev="a_P",
  254. latex_repr=r'a_\text{P}')
  255. # Information theory units:
  256. bit = bits = Quantity("bit")
  257. bit.set_global_dimension(information)
  258. byte = bytes = Quantity("byte")
  259. kibibyte = kibibytes = Quantity("kibibyte")
  260. mebibyte = mebibytes = Quantity("mebibyte")
  261. gibibyte = gibibytes = Quantity("gibibyte")
  262. tebibyte = tebibytes = Quantity("tebibyte")
  263. pebibyte = pebibytes = Quantity("pebibyte")
  264. exbibyte = exbibytes = Quantity("exbibyte")
  265. byte.set_global_relative_scale_factor(8, bit)
  266. kibibyte.set_global_relative_scale_factor(kibi, byte)
  267. mebibyte.set_global_relative_scale_factor(mebi, byte)
  268. gibibyte.set_global_relative_scale_factor(gibi, byte)
  269. tebibyte.set_global_relative_scale_factor(tebi, byte)
  270. pebibyte.set_global_relative_scale_factor(pebi, byte)
  271. exbibyte.set_global_relative_scale_factor(exbi, byte)
  272. # Older units for radioactivity
  273. curie = Ci = Quantity("curie", abbrev="Ci")
  274. rutherford = Rd = Quantity("rutherford", abbrev="Rd")