Skip to content

Commit 17ac92c

Browse files
committed
reinstated units: eV (and multiples), hartree_energy, amu, au.
1 parent bd7f0ab commit 17ac92c

File tree

7 files changed

+137
-77
lines changed

7 files changed

+137
-77
lines changed

quantities/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,3 @@
8787
from units import *
8888

8989
import constants
90-
from constants import *

quantities/constants/constants.py

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,11 @@
88
from quantities.unitquantity import UnitConstant
99

1010

11-
# temporary units without uncertainty required for some other constants
12-
# we will redefine them with uncertainty later
13-
eV = electron_volt = UnitConstant(
14-
'electron_volt',
15-
_cd('electron volt').view(Quantity),
16-
symbol='eV'
17-
)
18-
MeV = UnitConstant(
19-
'MeV',
20-
1e6*eV,
21-
)
22-
GeV = UnitConstant(
23-
'GeV',
24-
1e9*eV,
25-
)
26-
E_h = Hartree_energy = UnitConstant(
27-
'Hartree_energy',
28-
_cd('Hartree energy').view(Quantity),
29-
symbol='E_h'
30-
)
31-
u = unified_atomic_mass_unit = UnitConstant(
32-
'unified_atomic_mass_unit',
33-
_cd('unified atomic mass unit').view(Quantity),
34-
symbol='u'
35-
)
36-
3711
d_220 = a_Si_220 = silicon_220_lattice_spacing = UnitConstant(
3812
'silicon_220_lattice_spacing',
3913
_cd('{220} lattice spacing of silicon'),
4014
symbol='d_220',
41-
u_symbol='d₂₂₀',
42-
aliases=['a_Si_220']
15+
u_symbol='d₂₂₀'
4316
)
4417
m_alpha = alpha_particle_mass = UnitConstant(
4518
'alpha_particle_mass',
@@ -56,8 +29,7 @@
5629
au = astronomical_unit = UnitConstant(
5730
'astronomical_unit',
5831
UncertainQuantity(149597870691, 'm', 30),
59-
symbol='au',
60-
aliases=['astronomical_units'],
32+
symbol='au'
6133
) # http://en.wikipedia.org/wiki/Astronomical_unit
6234
amu = atomic_mass_constant = UnitConstant(
6335
'atomic_mass_constant',
@@ -288,8 +260,7 @@
288260
'electric_constant',
289261
_cd('electric constant'),
290262
symbol='epsilon_0',
291-
u_symbol='ε₀',
292-
aliases=['vacuum_permittivity']
263+
u_symbol='ε₀'
293264
)
294265
electron_charge_to_mass_quotient = UnitConstant(
295266
'electron_charge_to_mass_quotient',
@@ -333,6 +304,11 @@
333304
symbol='m_e',
334305
u_symbol='mₑ'
335306
)
307+
eV = electron_volt = UnitConstant(
308+
'electron_volt',
309+
_cd('electron volt'),
310+
symbol='eV'
311+
)
336312
e = elementary_charge = UnitConstant(
337313
'elementary_charge',
338314
_cd('elementary charge'),
@@ -374,6 +350,11 @@
374350
symbol='c_1L',
375351
u_symbol='c₁_L'
376352
)
353+
E_h = Hartree_energy = UnitConstant(
354+
'Hartree_energy',
355+
_cd('Hartree energy'),
356+
symbol='E_h'
357+
)
377358
m_h = helion_mass = UnitConstant(
378359
'helion_mass',
379360
_cd('helion mass'),
@@ -752,6 +733,11 @@
752733
_cd('triton mass'),
753734
symbol='m_t'
754735
)
736+
u = unified_atomic_mass_unit = UnitConstant(
737+
'unified_atomic_mass_unit',
738+
_cd('unified atomic mass unit'),
739+
symbol='u'
740+
)
755741
R_K = von_Klitzing_constant = UnitConstant(
756742
'von_Klitzing_constant',
757743
_cd('von Klitzing constant'),
@@ -773,37 +759,5 @@
773759
symbol='b'
774760
)
775761

776-
eV = electron_volt = UnitConstant(
777-
'electron_volt',
778-
_cd('electron volt'),
779-
symbol='eV'
780-
)
781-
meV = UnitConstant(
782-
'meV',
783-
eV/1000,
784-
)
785-
keV = UnitConstant(
786-
'keV',
787-
1000*eV,
788-
)
789-
MeV = UnitConstant(
790-
'MeV',
791-
1000*keV,
792-
)
793-
GeV = beV = UnitConstant(
794-
'GeV',
795-
1000*MeV,
796-
aliases=['beV']
797-
)
798-
E_h = Hartree_energy = UnitConstant(
799-
'Hartree_energy',
800-
_cd('Hartree energy'),
801-
symbol='E_h'
802-
)
803-
u = unified_atomic_mass_unit = UnitConstant(
804-
'unified_atomic_mass_unit',
805-
_cd('unified atomic mass unit'),
806-
symbol='u'
807-
)
808762

809763
del UnitConstant, Quantity, UncertainQuantity, _cd

quantities/unitquantity.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class UnitQuantity(Quantity):
1919

2020
_primary_order = 99
2121
_secondary_order = 0
22+
_reference_quantity = None
2223

2324
__array_priority__ = 20
2425

@@ -54,13 +55,17 @@ def __new__(
5455
ret._format_order = (ret._primary_order, ret._secondary_order)
5556
ret.__class__._secondary_order += 1
5657

57-
unit_registry[name] = ret
58+
return ret
59+
60+
def __init__(
61+
self, name, reference_quantity=None, symbol=None, u_symbol=None,
62+
aliases=[], note=None
63+
):
64+
unit_registry[name] = self
5865
if symbol:
59-
unit_registry[symbol] = ret
66+
unit_registry[symbol] = self
6067
for alias in aliases:
61-
unit_registry[alias] = ret
62-
63-
return ret
68+
unit_registry[alias] = self
6469

6570
def __repr__(self):
6671
if self.u_symbol != self.name:
@@ -183,11 +188,38 @@ def u_symbol(self):
183188
def units(self):
184189
return self
185190

191+
@classmethod
192+
def set_reference_unit(cls, unit):
193+
if cls.__name__ in ('UnitConstant', 'UnitQuantity', 'Dimensionless'):
194+
raise ValueError(
195+
'can not set alternate reference unit for type %'% cls.__name__
196+
)
197+
if isinstance(unit, str):
198+
unit = unit_registry[unit]
199+
try:
200+
assert type(unit) == cls or unit is None
201+
except:
202+
raise TypeError('unit must be of same type or "None"')
203+
204+
cls._alt_reference_unit = unit
205+
206+
@property
207+
def alt_reference_unit(self):
208+
return self.__class__._alt_reference_unit
209+
210+
186211
unit_registry['UnitQuantity'] = UnitQuantity
187212

188213

189214
class UnitConstant(UnitQuantity):
190215

216+
def __init__(
217+
self, name, reference_quantity=None, symbol=None, u_symbol=None,
218+
aliases=[], note=None
219+
):
220+
# we dont want to register constants in the unit registry
221+
return
222+
191223
_primary_order = 0
192224

193225

quantities/units/electromagnetism.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from quantities.units.length import m
99
from quantities.units.energy import J
1010
from quantities.units.velocity import c
11+
from quantities.units.force import N
1112
from math import pi
1213

1314

@@ -137,6 +138,30 @@
137138
1e-8*V,
138139
aliases=['abvolts']
139140
)
141+
e = elementary_charge = UnitQuantity(
142+
'elementary_charge',
143+
1.602176487e-19*C,
144+
symbol='e',
145+
note='relative uncertainty = 6.64e-8'
146+
)
147+
chemical_faraday = UnitQuantity(
148+
'chemical_faraday',
149+
9.64957e4*C
150+
)
151+
physical_faraday = physical_faradays = UnitQuantity(
152+
'physical_faraday',
153+
9.65219e4*C
154+
)
155+
faraday = C12_faraday = UnitQuantity(
156+
'faraday',
157+
96485.3399*C,
158+
symbol='F',
159+
aliases=['faradays']
160+
)
161+
gamma = UnitQuantity(
162+
'gamma',
163+
1e-9*T
164+
)
140165
gauss = UnitQuantity(
141166
'gauss',
142167
1e-4*T,
@@ -187,6 +212,20 @@
187212
'unit_pole',
188213
1.256637e-7*Wb
189214
)
215+
vacuum_permeability = mu_0 = magnetic_constant = UnitQuantity(
216+
'magnetic_constant',
217+
4*pi*10**-7*N/A**2,
218+
symbol='epsilon_0',
219+
u_symbol='μ₀',
220+
aliases=['vacuum_permeability']
221+
)
222+
vacuum_permittivity = epsilon_0 = electric_constant = UnitQuantity(
223+
'electric_constant',
224+
1/(mu_0*c**2),
225+
symbol='epsilon_0',
226+
u_symbol='ε₀',
227+
aliases=['vacuum_permittivity']
228+
)
190229

191230
cd = candle = candela = UnitLuminousIntensity(
192231
'candela',

quantities/units/energy.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@
2121
J*1.05505585262e3,
2222
symbol='BTU'
2323
)
24+
eV = electron_volt = UnitQuantity(
25+
'electron_volt',
26+
J*1.60217653e-19,
27+
symbol='eV',
28+
aliases=['electron_volts']
29+
)
30+
meV = UnitQuantity(
31+
'meV',
32+
eV/1000
33+
)
34+
keV = UnitQuantity(
35+
'keV',
36+
1000*eV
37+
)
38+
MeV = UnitQuantity(
39+
'MeV',
40+
1000*keV
41+
)
42+
bev = GeV = UnitQuantity(
43+
'GeV',
44+
1000*MeV
45+
)
2446
thm = therm = EC_therm = UnitQuantity(
2547
'EC_therm',
2648
100000*BTU,
@@ -70,5 +92,12 @@
7092
symbol='GWh',
7193
aliases=['gigawatthour', 'gigawatthours', 'gigawatt_hours']
7294
)
95+
E_h = hartree = hartree_energy = UnitQuantity(
96+
'hartree',
97+
4.35974394e-18*J,
98+
symbol='E_h',
99+
aliases=['hartrees', 'hartree_energy', 'Hartree_energy'],
100+
note='relative uncertainty = 2.1e-6'
101+
)
73102

74103
del UnitQuantity, dyne, N, cm, m, s, h

quantities/units/length.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@
104104
symbol='ly',
105105
aliases=['light_years']
106106
)
107+
au = astronomical_unit = UnitLength(
108+
'astronomical_unit',
109+
149597870691*m,
110+
symbol='au',
111+
aliases=['astronomical_units'],
112+
note='uncertainty ± 30 m'
113+
)
107114

108115
nmi = nautical_mile = UnitLength(
109116
'nautical_mile',

quantities/units/mass.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@
9393
'apothecary_pounds'
9494
]
9595
)
96-
#u = amu = atomic_mass_unit = UnitMass(
97-
# 'atomic_mass_unit',
98-
# 1.660538782e-27*kg,
99-
# symbol='u',
100-
# aliases=['amu'],
101-
# note='relative uncertainty = 5e-8'
102-
#) # TODO: needs uncertainty: 0.000000083e-27*kg
96+
u = amu = atomic_mass_unit = UnitMass(
97+
'atomic_mass_unit',
98+
1.660538782e-27*kg,
99+
symbol='u',
100+
aliases=['amu'],
101+
note='relative uncertainty = 5e-8'
102+
)
103103
scruple = UnitMass(
104104
'scruple',
105105
20*gr,

0 commit comments

Comments
 (0)