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