Coverage for ase/units.py: 100.00%
48 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""ase.units
3Physical constants and units derived from CODATA for converting
4to and from ase internal units.
6Examples
7--------
8>>> from ase.units import kcal, mol
9>>> print(kcal / mol)
100.04336410390059322
12"""
14from math import pi, sqrt
16# the version we actually use
17__codata_version__ = '2014'
20# Instead of a plain dict, if the units are in the __dict__ of a
21# dict subclass, they can be accessed as attributes in a similar way
22# to a module.
23class Units(dict):
24 """Dictionary for units that supports .attribute access."""
26 def __init__(self, *args, **kwargs):
27 super().__init__(*args, **kwargs)
28 self.__dict__ = self
31# this is the hard-coded CODATA values
32# all other units are dynamically derived from these values upon import of the
33# module
34CODATA = {
35 # the "original" CODATA version ase used ever since
36 # Constants from Konrad Hinsen's PhysicalQuantities module (1986 CODATA)
37 # Add the constant pi used to define the mu0 and hbar here for reference
38 # as well
39 '1986': {
40 '_c': 299792458.0, # speed of light, m/s
41 '_mu0': 4.0e-7 * pi, # permeability of vacuum
42 '_Grav': 6.67259e-11, # gravitational constant
43 '_hplanck': 6.6260755e-34, # Planck constant, J s
44 '_e': 1.60217733e-19, # elementary charge
45 '_me': 9.1093897e-31, # electron mass
46 '_mp': 1.6726231e-27, # proton mass
47 '_Nav': 6.0221367e23, # Avogadro number
48 '_k': 1.380658e-23, # Boltzmann constant, J/K
49 '_amu': 1.6605402e-27,
50 }, # atomic mass unit, kg
51 # CODATA 1998 taken from
52 # https://doi.org/10.1103/RevModPhys.72.351
53 '1998': {
54 '_c': 299792458.0,
55 '_mu0': 4.0e-7 * pi,
56 '_Grav': 6.673e-11,
57 '_hplanck': 6.62606876e-34,
58 '_e': 1.602176462e-19,
59 '_me': 9.10938188e-31,
60 '_mp': 1.67262158e-27,
61 '_Nav': 6.02214199e23,
62 '_k': 1.3806503e-23,
63 '_amu': 1.66053873e-27,
64 },
65 # CODATA 2002 taken from
66 # https://doi.org/10.1103/RevModPhys.77.1
67 '2002': {
68 '_c': 299792458.0,
69 '_mu0': 4.0e-7 * pi,
70 '_Grav': 6.6742e-11,
71 '_hplanck': 6.6260693e-34,
72 '_e': 1.60217653e-19,
73 '_me': 9.1093826e-31,
74 '_mp': 1.67262171e-27,
75 '_Nav': 6.0221415e23,
76 '_k': 1.3806505e-23,
77 '_amu': 1.66053886e-27,
78 },
79 # CODATA 2006 taken from
80 # https://doi.org/10.1103/RevModPhys.80.633
81 '2006': {
82 '_c': 299792458.0,
83 '_mu0': 4.0e-7 * pi,
84 '_Grav': 6.67428e-11,
85 '_hplanck': 6.62606896e-34,
86 '_e': 1.602176487e-19,
87 '_me': 9.10938215e-31,
88 '_mp': 1.672621637e-27,
89 '_Nav': 6.02214179e23,
90 '_k': 1.3806504e-23,
91 '_amu': 1.660538782e-27,
92 },
93 # CODATA 2010 taken from
94 # https://doi.org/10.1103/RevModPhys.84.1527
95 '2010': {
96 '_c': 299792458.0,
97 '_mu0': 4.0e-7 * pi,
98 '_Grav': 6.67384e-11,
99 '_hplanck': 6.62606957e-34,
100 '_e': 1.602176565e-19,
101 '_me': 9.10938291e-31,
102 '_mp': 1.672621777e-27,
103 '_Nav': 6.02214129e23,
104 '_k': 1.3806488e-23,
105 '_amu': 1.660538921e-27,
106 },
107 # CODATA 2014 taken from
108 # http://arxiv.org/pdf/1507.07956.pdf
109 '2014': {
110 '_c': 299_792_458.0, # Exact
111 '_mu0': 4.0e-7 * pi, # Exact
112 '_Grav': 6.674_08e-11, # +/- 0.000_31e-11
113 '_hplanck': 6.626_070_040e-34, # +/- 0.000_000_081e-34
114 '_e': 1.602_176_6208e-19, # +/- 0.000_000_0098e-19
115 '_me': 9.109_383_56e-31, # +/- 0.000_000_11e-31
116 '_mp': 1.672_621_898e-27, # +/- 0.000_000_021e-27
117 '_Nav': 6.022_140_857e23, # +/- 0.000_000_074e23
118 '_k': 1.380_648_52e-23, # +/- 0.000_000_79e-23
119 '_amu': 1.660_539_040e-27,
120 }, # +/- 0.000_000_020e-27
121 # CODATA 2018 taken from
122 # https://physics.nist.gov/cuu/pdf/all_2018.pdf
123 '2018': {
124 '_c': 299_792_458.0, # Exact
125 '_mu0': 1.256_637_062_12e-6, # +/- 0.000_000_000_19e−6
126 '_Grav': 6.674_30e-11, # +/- 0.000_15e-11
127 '_hplanck': 6.626_070_15e-34, # Exact
128 '_e': 1.602_176_634e-19, # Exact
129 '_me': 9.109_383_7015e-31, # +/- 0.000_000_0028e-31
130 '_mp': 1.672_621_923_69e-27, # +/- 0.000_000_000_51e-27
131 '_Nav': 6.022_140_76e23, # Exact
132 '_k': 1.380_649e-23, # Exact
133 '_amu': 1.660_539_066_60e-27,
134 }, # +/- 0.000_000_000_50e-27
135 # CODATA 2022 (current) taken from
136 # https://physics.nist.gov/cuu/Constants/index.html
137 '2022': {
138 '_c': 299_792_458.0, # Exact
139 '_mu0': 1.256_637_061_27e-6, # +/- 0.000_000_000_20e−6
140 '_Grav': 6.674_30e-11, # +/- 0.000_15e-11
141 '_hplanck': 6.626_070_15e-34, # Exact
142 '_e': 1.602_176_634e-19, # Exact
143 '_me': 9.109_383_7139e-31, # +/- 0.000_000_0028e−31
144 '_mp': 1.672_621_925_95e-27, # +/- 0.000_000_000_52e−27
145 '_Nav': 6.022_140_76e23, # Exact
146 '_k': 1.380_649e-23, # Exact
147 '_amu': 1.660_539_068_92e-27,
148 }, # +/- 0.000_000_000_52e−27
149}
152def create_units(codata_version):
153 """
154 Function that creates a dictionary containing all units previously hard
155 coded in ase.units depending on a certain CODATA version. Note that
156 returned dict has attribute access it can be used in place of the module
157 or to update your local or global namespace.
159 Parameters
160 ----------
162 codata_version: str
163 The CODATA version to be used. Implemented are
165 * '1986'
166 * '1998'
167 * '2002'
168 * '2006'
169 * '2010'
170 * '2014'
171 * '2018'
172 * '2022'
174 Returns
175 -------
177 units: dict
178 Dictionary that contains all formerly hard coded variables from
179 ase.units as key-value pairs. The dict supports attribute access.
181 Raises
182 ------
184 NotImplementedError
185 If the required CODATA version is not known.
186 """
188 try:
189 u = Units(CODATA[codata_version])
190 except KeyError:
191 raise NotImplementedError(
192 'CODATA version "{}" not implemented'.format(codata_version)
193 )
195 # derived from the CODATA values
196 u['_eps0'] = 1 / u['_mu0'] / u['_c'] ** 2 # permittivity of vacuum
197 u['_hbar'] = u['_hplanck'] / (2 * pi) # Planck constant / 2pi, J s
199 u['Ang'] = u['Angstrom'] = 1.0
200 u['nm'] = 10.0
201 u['Bohr'] = (
202 4e10 * pi * u['_eps0'] * u['_hbar'] ** 2 / u['_me'] / u['_e'] ** 2
203 ) # Bohr radius
205 u['eV'] = 1.0
206 u['Hartree'] = (
207 u['_me'] * u['_e'] ** 3 / 16 / pi**2 / u['_eps0'] ** 2 / u['_hbar'] ** 2
208 )
209 u['kJ'] = 1000.0 / u['_e']
210 u['kcal'] = 4.184 * u['kJ']
211 u['mol'] = u['_Nav']
212 u['Rydberg'] = 0.5 * u['Hartree']
213 u['Ry'] = u['Rydberg']
214 u['Ha'] = u['Hartree']
216 u['second'] = 1e10 * sqrt(u['_e'] / u['_amu'])
217 u['fs'] = 1e-15 * u['second']
219 u['kB'] = u['_k'] / u['_e'] # Boltzmann constant, eV/K
221 u['Pascal'] = (1 / u['_e']) / 1e30 # J/m^3
222 u['GPa'] = 1e9 * u['Pascal']
223 u['bar'] = 1e5 * u['Pascal']
225 u['Debye'] = 1.0 / 1e11 / u['_e'] / u['_c']
226 u['alpha'] = (
227 u['_e'] ** 2 / (4 * pi * u['_eps0']) / u['_hbar'] / u['_c']
228 ) # fine structure constant
229 u['invcm'] = 100 * u['_c'] * u['_hplanck'] / u['_e'] # cm^-1 energy unit
231 # Derived atomic units that have no assigned name:
232 # atomic unit of time, s:
233 u['_aut'] = u['_hbar'] / (u['alpha'] ** 2 * u['_me'] * u['_c'] ** 2)
234 # atomic unit of velocity, m/s:
235 u['_auv'] = u['_e'] ** 2 / u['_hbar'] / (4 * pi * u['_eps0'])
236 # atomic unit of force, N:
237 u['_auf'] = u['alpha'] ** 3 * u['_me'] ** 2 * u['_c'] ** 3 / u['_hbar']
238 # atomic unit of pressure, Pa:
239 u['_aup'] = u['alpha'] ** 5 * u['_me'] ** 4 * u['_c'] ** 5 / u['_hbar'] ** 3
241 u['AUT'] = u['second'] * u['_aut']
243 # SI units
244 u['m'] = 1e10 * u['Ang'] # metre
245 u['kg'] = 1.0 / u['_amu'] # kilogram
246 u['s'] = u['second'] # second
247 u['A'] = 1.0 / u['_e'] / u['s'] # ampere
248 # derived
249 u['J'] = u['kJ'] / 1000 # Joule = kg * m**2 / s**2
250 u['C'] = 1.0 / u['_e'] # Coulomb = A * s
252 return u
255# Define all the expected symbols with dummy values so that introspection
256# will know that they exist when the module is imported, even though their
257# values are immediately overwritten.
258# pylint: disable=invalid-name
259(
260 _Grav,
261 _Nav,
262 _amu,
263 _auf,
264 _aup,
265 _aut,
266 _auv,
267 _c,
268 _e,
269 _eps0,
270 _hbar,
271 _hplanck,
272 _k,
273 _me,
274 _mp,
275 _mu0,
276 alpha,
277 eV,
278 fs,
279 invcm,
280 kB,
281 kJ,
282 kcal,
283 kg,
284 m,
285 mol,
286 nm,
287 s,
288 second,
289 A,
290 AUT,
291 Ang,
292 Angstrom,
293 Bohr,
294 C,
295 Debye,
296 GPa,
297 Ha,
298 Hartree,
299 J,
300 Pascal,
301 bar,
302 Ry,
303 Rydberg,
304) = [0.0] * 44
306# Now update the module scope:
307globals().update(create_units(__codata_version__))