0
# Constants
1
2
Physical and astronomical constants with proper units and uncertainty handling, including support for different constant systems (CODATA, IAU).
3
4
## Capabilities
5
6
### Constant Classes
7
8
Base classes for physical and astronomical constants with units, uncertainties, and version control.
9
10
```python { .api }
11
class Constant:
12
"""
13
Physical or astronomical constant with units and uncertainty.
14
15
Parameters:
16
- abbrev: constant abbreviation
17
- name: full constant name
18
- value: numerical value
19
- unit: astropy unit
20
- uncertainty: uncertainty in the value
21
- reference: literature reference
22
- system: constant system (e.g., 'codata2018')
23
"""
24
def __init__(self, abbrev, name, value, unit, uncertainty, reference=None, system=None): ...
25
26
@property
27
def value(self):
28
"""Numerical value of constant."""
29
30
@property
31
def unit(self):
32
"""Unit of constant."""
33
34
@property
35
def uncertainty(self):
36
"""Uncertainty in constant value."""
37
38
@property
39
def name(self):
40
"""Full name of constant."""
41
42
@property
43
def abbrev(self):
44
"""Abbreviation for constant."""
45
46
@property
47
def reference(self):
48
"""Literature reference."""
49
50
def __float__(self):
51
"""Convert to float value."""
52
53
def __array__(self):
54
"""Convert to numpy array."""
55
56
class EMConstant(Constant):
57
"""
58
Electromagnetic constant with system-dependent values.
59
60
Handles constants that have different values in different unit systems
61
(e.g., CGS-ESU vs CGS-Gaussian vs SI).
62
"""
63
def __init__(self, abbrev, name, value, unit, uncertainty, reference=None, system=None): ...
64
```
65
66
### Physical Constants (CODATA)
67
68
Fundamental physical constants from CODATA (Committee on Data for Science and Technology).
69
70
```python { .api }
71
# Import physical constants
72
from astropy.constants import c, h, hbar, k_B, sigma_sb, a0, m_e, m_p, m_n, u, G, e, eps0, mu0, alpha, R, N_A
73
74
# Speed of light
75
c: Constant # 299792458.0 m / s
76
77
# Planck constants
78
h: Constant # 6.62607015e-34 J s (Planck constant)
79
hbar: Constant # 1.0545718176e-34 J s (reduced Planck constant)
80
81
# Boltzmann constant
82
k_B: Constant # 1.380649e-23 J / K
83
84
# Stefan-Boltzmann constant
85
sigma_sb: Constant # 5.670374419e-08 W / (K4 m2)
86
87
# Bohr radius
88
a0: Constant # 5.29177210903e-11 m
89
90
# Particle masses
91
m_e: Constant # 9.1093837015e-31 kg (electron mass)
92
m_p: Constant # 1.67262192369e-27 kg (proton mass)
93
m_n: Constant # 1.67492749804e-27 kg (neutron mass)
94
u: Constant # 1.66053906660e-27 kg (atomic mass unit)
95
96
# Gravitational constant
97
G: Constant # 6.67430e-11 m3 / (kg s2)
98
99
# Elementary charge
100
e: Constant # 1.602176634e-19 C
101
102
# Electric permittivity and permeability
103
eps0: Constant # 8.8541878128e-12 F / m (vacuum permittivity)
104
mu0: Constant # 1.25663706212e-06 H / m (vacuum permeability)
105
106
# Fine structure constant
107
alpha: Constant # 0.0072973525693 (dimensionless)
108
109
# Gas constant
110
R: Constant # 8.314462618 J / (K mol)
111
112
# Avogadro constant
113
N_A: Constant # 6.02214076e23 1 / mol
114
```
115
116
### Astronomical Constants (IAU)
117
118
Astronomical constants from the International Astronomical Union.
119
120
```python { .api }
121
# Import astronomical constants
122
from astropy.constants import L_sun, M_sun, R_sun, M_earth, R_earth, M_jup, R_jup, au, pc, kpc
123
124
# Solar constants
125
L_sun: Constant # 3.828e26 W (solar luminosity)
126
M_sun: Constant # 1.9891e30 kg (solar mass)
127
R_sun: Constant # 695700000.0 m (solar radius)
128
129
# Earth constants
130
M_earth: Constant # 5.9722e24 kg (Earth mass)
131
R_earth: Constant # 6378136.6 m (Earth equatorial radius)
132
133
# Jupiter constants
134
M_jup: Constant # 1.8982e27 kg (Jupiter mass)
135
R_jup: Constant # 71492000.0 m (Jupiter equatorial radius)
136
137
# Distance units
138
au: Constant # 149597870700.0 m (astronomical unit)
139
pc: Constant # 3.0856775814913674e16 m (parsec)
140
kpc: Constant # 3.0856775814913674e19 m (kiloparsec)
141
```
142
143
### Constant Systems and Versions
144
145
Support for different versions of physical and astronomical constant systems.
146
147
```python { .api }
148
# Access different CODATA versions
149
import astropy.constants.codata2018 as codata2018
150
import astropy.constants.codata2014 as codata2014
151
import astropy.constants.codata2010 as codata2010
152
153
# Access different IAU versions
154
import astropy.constants.iau2015 as iau2015
155
import astropy.constants.iau2012 as iau2012
156
157
# Set default constant systems globally
158
from astropy import physical_constants, astronomical_constants
159
160
# Set CODATA version
161
physical_constants.set('codata2018') # 'codata2018', 'codata2014', 'codata2010'
162
163
# Set IAU version
164
astronomical_constants.set('iau2015') # 'iau2015', 'iau2012'
165
166
# Context managers for temporary constant systems
167
with physical_constants.set('codata2014'):
168
# Use CODATA 2014 constants temporarily
169
old_c = c
170
171
with astronomical_constants.set('iau2012'):
172
# Use IAU 2012 constants temporarily
173
old_au = au
174
```
175
176
### Unit System Support
177
178
Constants available in different unit systems (SI, CGS, etc.).
179
180
```python { .api }
181
# SI units (default)
182
import astropy.constants.si as si
183
si.c # Speed of light in m/s
184
si.G # Gravitational constant in m^3 kg^-1 s^-2
185
186
# CGS units
187
import astropy.constants.cgs as cgs
188
cgs.c # Speed of light in cm/s
189
cgs.G # Gravitational constant in cm^3 g^-1 s^-2
190
191
# Access via main constants module
192
from astropy.constants import c
193
c.si # c in SI units
194
c.cgs # c in CGS units
195
```
196
197
## Usage Examples
198
199
### Basic Constant Usage
200
201
```python
202
from astropy.constants import c, h, k_B, G, M_sun, L_sun
203
import astropy.units as u
204
205
# Use constants in calculations
206
photon_energy = h * (c / (500 * u.nm))
207
print(f"500 nm photon energy: {photon_energy.to(u.eV)}")
208
209
# Thermal energy at room temperature
210
T_room = 300 * u.K
211
thermal_energy = k_B * T_room
212
print(f"Thermal energy at 300K: {thermal_energy.to(u.eV)}")
213
214
# Solar surface gravity
215
g_sun = G * M_sun / (695700 * u.km)**2
216
print(f"Solar surface gravity: {g_sun.to(u.m/u.s**2)}")
217
```
218
219
### Astronomical Calculations
220
221
```python
222
from astropy.constants import c, G, M_sun, R_sun, L_sun, sigma_sb
223
import astropy.units as u
224
225
# Solar escape velocity
226
v_escape_sun = (2 * G * M_sun / R_sun)**0.5
227
print(f"Solar escape velocity: {v_escape_sun.to(u.km/u.s)}")
228
229
# Solar effective temperature from luminosity
230
T_eff_sun = (L_sun / (4 * np.pi * R_sun**2 * sigma_sb))**(1/4)
231
print(f"Solar effective temperature: {T_eff_sun.to(u.K)}")
232
233
# Schwarzschild radius for solar mass black hole
234
r_schwarzschild = 2 * G * M_sun / c**2
235
print(f"Solar mass Schwarzschild radius: {r_schwarzschild.to(u.km)}")
236
```
237
238
### Constant Uncertainties
239
240
```python
241
from astropy.constants import G, h
242
243
# Access uncertainty information
244
print(f"G = {G.value} ± {G.uncertainty} {G.unit}")
245
print(f"Relative uncertainty in G: {G.uncertainty/G.value:.2e}")
246
247
print(f"h = {h.value} (exact since 2019 SI redefinition)")
248
print(f"h uncertainty: {h.uncertainty}") # Should be 0.0 for exact constants
249
```
250
251
### Unit System Conversions
252
253
```python
254
from astropy.constants import c, G, k_B
255
import astropy.units as u
256
257
# Compare values in different unit systems
258
print(f"Speed of light:")
259
print(f" SI: {c.si}")
260
print(f" CGS: {c.cgs}")
261
262
print(f"Gravitational constant:")
263
print(f" SI: {G.si}")
264
print(f" CGS: {G.cgs}")
265
266
# Use in calculations with unit conversion
267
velocity = 0.1 * c
268
kinetic_energy_cgs = 0.5 * (1 * u.g) * velocity.cgs**2
269
print(f"Kinetic energy (CGS): {kinetic_energy_cgs}")
270
```
271
272
### Working with Different Constant Versions
273
274
```python
275
from astropy import physical_constants
276
from astropy.constants import c, G
277
278
# Check current constant system
279
print(f"Current CODATA version: {physical_constants.get()}")
280
print(f"Current G: {G}")
281
282
# Use different constant version
283
with physical_constants.set('codata2014'):
284
print(f"CODATA 2014 G: {G}")
285
# Calculations here use CODATA 2014 values
286
287
# Direct access to specific versions
288
import astropy.constants.codata2018 as c18
289
import astropy.constants.codata2014 as c14
290
291
print(f"G (CODATA 2018): {c18.G}")
292
print(f"G (CODATA 2014): {c14.G}")
293
print(f"Difference: {(c18.G - c14.G).to(c18.G.unit)}")
294
```
295
296
### Constants in Astrophysical Applications
297
298
```python
299
from astropy.constants import c, h, k_B, sigma_sb, a0, m_e, m_p
300
import astropy.units as u
301
import numpy as np
302
303
# Planck function for blackbody radiation
304
def planck_function(wavelength, temperature):
305
\"\"\"Planck function for blackbody spectral radiance.\"\"\"
306
return (2 * h * c**2 / wavelength**5) / (np.exp(h * c / (wavelength * k_B * temperature)) - 1)
307
308
# Calculate for solar spectrum
309
wavelength = 500 * u.nm
310
T_sun = 5778 * u.K
311
intensity = planck_function(wavelength, T_sun)
312
print(f"Solar intensity at 500 nm: {intensity.to(u.W / u.m**2 / u.nm / u.sr)}")
313
314
# Hydrogen ionization energy
315
E_ionization = 13.6 * u.eV # Rydberg constant * h * c
316
classical_electron_radius = e**2 / (4 * np.pi * eps0 * m_e * c**2)
317
print(f"Classical electron radius: {classical_electron_radius.to(u.fm)}")
318
```