unit_definitions.py 14 KB

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