Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
255 views
1
'''
2
blackbody.py - Color of thermal blackbodies.
3
4
Description:
5
6
Calculate the spectrum of a thermal blackbody at an arbitrary temperature.
7
8
Constants:
9
10
PLANCK_CONSTANT - Planck's constant, in J-sec
11
SPEED_OF_LIGHT - Speed of light, in m/sec
12
BOLTZMAN_CONSTANT - Boltzman's constant, in J/K
13
SUN_TEMPERATURE - Surface temperature of the Sun, in K
14
15
Functions:
16
17
blackbody_specific_intensity (wl_nm, T_K) -
18
Get the monochromatic specific intensity for a blackbody -
19
wl_nm = wavelength [nm]
20
T_K = temperature [K]
21
This is the energy radiated per second per unit wavelength per unit solid angle.
22
Reference - Shu, eq. 4.6, p. 78.
23
24
blackbody_spectrum (T_K) -
25
Get the spectrum of a blackbody, as a numpy array.
26
27
blackbody_color (T_K) -
28
Given a temperature (K), return the xyz color of a thermal blackbody.
29
30
Plots:
31
32
blackbody_patch_plot (T_list, title, filename) -
33
Draw a patch plot of blackbody colors for the given temperature range.
34
35
blackbody_color_vs_temperature_plot (T_list, title, filename) -
36
Draw a color vs temperature plot for the given temperature range.
37
38
blackbody_spectrum_plot (T_K) -
39
Draw the spectrum of a blackbody at the given temperature.
40
41
References:
42
43
Frank H. Shu, The Physical Universe. An Introduction to Astronomy,
44
University Science Books, Mill Valley, California. 1982. ISBN 0-935702-05-9.
45
46
Charles Kittel and Herbert Kroemer, Thermal Physics, 2nd edition,
47
W. H. Freeman, New York, 1980. ISBN 0-7167-1088-9.
48
49
License:
50
51
Copyright (C) 2008 Mark Kness
52
53
Author - Mark Kness - [email protected]
54
55
This file is part of ColorPy.
56
57
ColorPy is free software: you can redistribute it and/or modify
58
it under the terms of the GNU Lesser General Public License as
59
published by the Free Software Foundation, either version 3 of
60
the License, or (at your option) any later version.
61
62
ColorPy is distributed in the hope that it will be useful,
63
but WITHOUT ANY WARRANTY; without even the implied warranty of
64
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65
GNU Lesser General Public License for more details.
66
67
You should have received a copy of the GNU Lesser General Public License
68
along with ColorPy. If not, see <http://www.gnu.org/licenses/>.
69
'''
70
import math, numpy, pylab
71
72
import colormodels
73
import ciexyz
74
import plots
75
76
# Physical constants in mks units
77
PLANCK_CONSTANT = 6.6237e-34 # J-sec
78
SPEED_OF_LIGHT = 2.997925e+08 # m/sec
79
BOLTZMAN_CONSTANT = 1.3802e-23 # J/K
80
SUN_TEMPERATURE = 5778.0 # K
81
82
def blackbody_specific_intensity (wl_nm, T_K):
83
'''Get the monochromatic specific intensity for a blackbody -
84
wl_nm = wavelength [nm]
85
T_K = temperature [K]
86
This is the energy radiated per second per unit wavelength per unit solid angle.
87
Reference - Shu, eq. 4.6, p. 78.'''
88
# precalculations that could be made global
89
a = (PLANCK_CONSTANT * SPEED_OF_LIGHT) / (BOLTZMAN_CONSTANT)
90
b = (2.0 * PLANCK_CONSTANT * SPEED_OF_LIGHT * SPEED_OF_LIGHT)
91
wl_m = wl_nm * 1.0e-9
92
try:
93
exponent = a / (wl_m * T_K)
94
except ZeroDivisionError:
95
# treat same as large exponent
96
return 0.0
97
if exponent > 500.0:
98
# so large that the final result is nearly zero - avoid the giant intermediate
99
return 0.0
100
specific_intensity = b / (math.pow (wl_m, 5) * (math.exp (exponent) - 1.0))
101
return specific_intensity
102
103
def blackbody_spectrum (T_K):
104
'''Get the spectrum of a blackbody, as a numpy array.'''
105
spectrum = ciexyz.empty_spectrum()
106
(num_rows, num_cols) = spectrum.shape
107
for i in xrange (0, num_rows):
108
specific_intensity = blackbody_specific_intensity (spectrum [i][0], T_K)
109
# scale by size of wavelength interval
110
spectrum [i][1] = specific_intensity * ciexyz.delta_wl_nm * 1.0e-9
111
return spectrum
112
113
def blackbody_color (T_K):
114
'''Given a temperature (K), return the xyz color of a thermal blackbody.'''
115
spectrum = blackbody_spectrum (T_K)
116
xyz = ciexyz.xyz_from_spectrum (spectrum)
117
return xyz
118
119
#
120
# Figures
121
#
122
123
def blackbody_patch_plot (T_list, title, filename):
124
'''Draw a patch plot of blackbody colors for the given temperature range.'''
125
xyz_colors = []
126
color_names = []
127
for Ti in T_list:
128
xyz = blackbody_color (Ti)
129
xyz_colors.append (xyz)
130
name = '%g K' % (Ti)
131
color_names.append (name)
132
plots.xyz_patch_plot (xyz_colors, color_names, title, filename)
133
134
def blackbody_color_vs_temperature_plot (T_list, title, filename):
135
'''Draw a color vs temperature plot for the given temperature range.'''
136
num_T = len (T_list)
137
rgb_list = numpy.empty ((num_T, 3))
138
for i in xrange (0, num_T):
139
T_i = T_list [i]
140
xyz = blackbody_color (T_i)
141
rgb_list [i] = colormodels.rgb_from_xyz (xyz)
142
# note that b and g become negative for low T - MatPlotLib skips those on the semilog plot.
143
plots.color_vs_param_plot (
144
T_list,
145
rgb_list,
146
title,
147
filename,
148
plotfunc = pylab.semilogy,
149
tight = True,
150
xlabel = r'Temperature (K)',
151
ylabel = r'RGB Color')
152
153
def blackbody_spectrum_plot (T_K):
154
'''Draw the spectrum of a blackbody at the given temperature.'''
155
spectrum = blackbody_spectrum (T_K)
156
title = 'Blackbody Spectrum - T %d K' % (int (T_K))
157
filename = 'BlackbodySpectrum-%dK' % (int (T_K))
158
plots.spectrum_plot (
159
spectrum,
160
title,
161
filename,
162
xlabel = 'Wavelength (nm)',
163
ylabel = 'Specific Intensity')
164
#ylabel = 'Intensity ($W/m^2$)') # with LaTex symbols, the axis text gets too big...
165
166
# Create sample figures
167
168
def figures ():
169
'''Create some blackbody plots.'''
170
# patch plots
171
T_list_0 = plots.log_interpolate ( 1200.0, 20000.0, 48)
172
T_list_hot = plots.log_interpolate (10000.0, 40000.0, 24)
173
T_list_cool = plots.log_interpolate ( 950.0, 1200.0, 24)
174
blackbody_patch_plot (T_list_0, 'Blackbody Colors', 'Blackbody-Patch')
175
blackbody_patch_plot (T_list_hot, 'Hot Blackbody Colors', 'Blackbody-HotPatch')
176
blackbody_patch_plot (T_list_cool, 'Cool Blackbody Colors', 'Blackbody-CoolPatch')
177
178
# color vs temperature
179
blackbody_color_vs_temperature_plot (range (1200, 16000, 50), 'Blackbody Colors', 'Blackbody-Colors')
180
blackbody_color_vs_temperature_plot (range (10000, 40000, 100), 'Hot Blackbody Colors', 'Blackbody-HotColors')
181
blackbody_color_vs_temperature_plot (range (950, 1200, 1), 'Cool Blackbody Colors', 'Blackbody-CoolColors')
182
183
# spectrum of specific temperatures
184
blackbody_spectrum_plot (2000.0)
185
blackbody_spectrum_plot (3000.0) # Proxima Centauri
186
blackbody_spectrum_plot (SUN_TEMPERATURE) # Sun
187
blackbody_spectrum_plot (11000.0) # Rigel
188
blackbody_spectrum_plot (15000.0)
189
190
191