-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrator_test.py
222 lines (189 loc) · 6.66 KB
/
integrator_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import scipy.interpolate as intr
import matplotlib.pyplot as pl
import astropy.constants as co
import astropy.units as un
import pandas as pd
import numpy as np
import tardis
def ratiocompare():
pl.plot(lam,in_L_lam/mc_L_lam)
pl.xlabel(u"A")
pl.title("Ratio")
pl.show()
tar = tardis.run_tardis("simple_example.yml")
freq = tar.runner.spectrum.frequency
lam = tar.runner.spectrum_virtual.wavelength
mc_L_nu = tar.runner.spectrum_virtual.luminosity_density_nu
mc_L_lam = tar.runner.spectrum_virtual.luminosity_density_lambda
in_L_nu = tar.runner.L_nu
in_L_lam = co.c.to("Angstrom/s")/lam**2 * in_L_nu
BB = tardis.util.intensity_black_body(tar.runner.spectrum_virtual.frequency,6416)
Fnu = BB*np.pi
Lbb_nu = 4*np.pi*tar.runner.r_inner_cgs.min()**2 * Fnu
Lbb_lam = Lbb_nu * co.c.to("Angstrom/s")/lam**2
if True:
print "Integrator Luminosity {:.6e}".format(np.trapz(in_L_nu*un.erg,freq.cgs))
print "MC Luminosity {:.6e}".format(np.trapz(mc_L_nu.cgs,freq.cgs) )
print "BB Luminosity {:.6e}".format(np.trapz(Lbb_nu*un.erg,freq.cgs))
ratiocompare()
def saveit(name):
saved = {"lam":lam.value,"mc_L_lam":mc_L_lam.value,"in_L_lam":in_L_lam.value}
np.savez(name,**saved)
def saverateio(name):
an = np.loadtxt("analytic_solution.dat")
np.save(name,in_L_lam.value/an[:,1]/Lbb_lam.value)
def pure_absorb_plot():
lam_start,lam_end = tar.tardis_config.spectrum.start,tar.tardis_config.spectrum.end
bins = tar.runner.spectrum.wavelength[::-1]
noint_mask = tar.runner.virt_packet_last_interaction_type == -1
noint_lam = (co.c.cgs / (tar.runner.virt_packet_nus[noint_mask] * un.Hz)).to("AA")
noint_ws = tar.runner.virt_packet_energies[noint_mask] * un.erg / tar.time_of_simulation
Lnorm = noint_ws[(noint_lam >= lam_start)*(noint_lam<=lam_end)].sum()
pl.plot(lam,in_L_lam,label="Source",color="red")
ax = pl.gca()
ret = ax.hist(noint_lam, weights=noint_ws, bins=bins, normed=True)
for reti in ret[-1]:
reti.set_facecolor("blue")
reti.set_edgecolor("blue")
reti.set_linewidth(0)
reti.set_height(reti.get_height() * Lnorm.value)
pl.plot(lam,in_L_lam,label="Source",color="red")
pl.show()
def compare():
pl.plot(lam,in_L_lam,label="Source")
pl.step(lam,mc_L_lam,label="MC")
pl.xlabel(u"A")
pl.ylabel("erg/A/s")
pl.title("Comparison")
pl.ylim(0,4e38)
pl.legend(loc="best")
pl.show()
def plot_srcfun(mdl):
sel = mdl.runner.wave[:,0].argsort()
[pl.axvline(x=X,ymin = .85,ymax=.95,color='r') for X in tar.atom_data.lines.wavelength]
pl.stem(1e8*mdl.runner.wave[sel],mdl.runner.att_S_ul)
pl.title("Source function")
pl.gca().set_yscale('log')
pl.gca().set_xscale('log')
pl.ylim(0,1e-3)
pl.xlabel("A")
pl.show()
def complot():
p10lam = np.load("10plam.npy")
p20lam = np.load("20plam.npy")
p50lam = np.load("50plam.npy")
p100lam = np.load("100plam.npy")
p200lam = np.load("200plam.npy")
lam = np.load("lam.npy")
mc_L_lam = np.load("MClam.npy")
pl.plot(lam,p10lam,label="10 p")
pl.plot(lam,p20lam,label="20 p")
pl.plot(lam,p50lam,label="50 p")
pl.plot(lam,p100lam,label="100 p")
pl.plot(lam,p100lam,label="200 p")
pl.plot(lam,mc_L_lam,linewidth=1.2,label="MC")
pl.xlabel(u"A")
pl.ylabel("erg/A/s")
pl.title("Comparison")
pl.ylim(0,3.7e38)
pl.legend(loc="best")
pl.show()
def summary():
lam = np.load("lam.npy")
mc_L_lam = np.load("MClam.npy")
p10lam = np.load("10plam.npy")
p20lam = np.load("20plam.npy")
p50lam = np.load("50plam.npy")
p100lam = np.load("100plam.npy")
p200lam = np.load("200plam.npy")
srcs = [p10lam, p20lam, p50lam, p100lam, p200lam]
labels = ["10 p","20 p","50 p","100 p","200 p"]
for i,src in enumerate(srcs):
pl.plot(lam,src,label=labels[i])
pl.plot(lam,mc_L_lam,linewidth=1.2,label="MC")
pl.ylim(0,6e38)
pl.xlabel(u"A")
pl.ylabel("erg/A/s")
pl.title(labels[i])
pl.legend(loc="best")
pl.show()
def lam_vary_summary():
lam05 = np.load("lam.npy")
lam15 = np.load("1500lam.npy")
lam55 = np.load("5500lam.npy")
lam100 = np.load("10000lam.npy")
int05 = np.load("20plam.npy")
int15 = np.load("1500lam20p.npy")
int55 = np.load("5500lam20p.npy")
int100 = np.load("10000lam20p.npy")
lams = [lam05, lam15, lam55, lam100]
ints = [int05, int15, int55, int100]
labels = ["500 lambda", "1.5k lambda","5.5k lambda","100k lambda"]
for i,(lam,src) in enumerate(zip(lams,ints)):
pl.plot(lam,src,label=labels[i])
pl.ylim(0,4e38)
pl.xlabel(u"A")
pl.ylabel("erg/A/s")
pl.title("Varying number of wavelengths")
pl.legend(loc="best")
pl.show()
def lam_p_vary_summary():
fnames = ["10p15clam","20p15clam","50p15clam","100p15clam","200p15clam","20pr55clam","200pr55clam",]
for fil in fnames:
dat = np.load(fil+".npz")
pl.semilogy(dat["lam"],dat["in_L_lam"],label=fil)
pl.semilogy(dat["lam"],dat["mc_L_lam"],label="MC")
pl.xlabel("A")
pl.ylabel("erg/A/s")
pl.title("Varying number of wavelengths and ps")
pl.legend(loc="best")
pl.show()
def lam_p_vary_20():
dat15 = np.load("20pr55clam.npz")
dat55 = np.load("200pr55clam.npz")
pl.plot(dat15["lam"],dat15["in_L_lam"],label="20p 5500 lam")
pl.plot(dat55["lam"],dat55["in_L_lam"],label="200p 5500 lam")
pl.xlabel("A")
pl.ylabel("erg/A/s")
pl.title("Varying number of wavelengths and ps")
pl.legend(loc="best")
pl.show()
def BBcomp():
pl.plot(lam,in_L_lam,label="Source")
pl.plot(lam,mc_L_lam,label="MC")
pl.plot(lam,Lbb_lam,label="BB")
pl.ylabel("erg/A/s")
pl.xlabel(u"A")
pl.title("Comparison")
pl.legend(loc="best")
pl.show()
def BBnorm():
# an = np.loadtxt("analytic_solution.dat")
pl.plot(lam,in_L_lam/Lbb_lam,label="Source")
pl.plot(lam,mc_L_lam/Lbb_lam,label="MC")
# pl.plot(an[:,0],an[:,1],label="Exact")
pl.ylabel("erg/A/s")
pl.xlabel(u"A")
pl.title("Comparison")
pl.legend(loc="best")
pl.ylim(0,2)
pl.show()
def Hsellnorm():
an = np.loadtxt("analytic_solution.dat")
pl.plot(lam,in_L_lam/an[:,1]/Lbb_lam)
pl.show()
def geotest():
r1 = np.load("4sh40p.npy")
r2 = np.load("10sh40p.npy")
r3 = np.load("40sh40p.npy")
r4 = np.load("70sh40p.npy")
r5 = np.load("40sh100p.npy")
pl.plot(lam,r1,label="4 sh 40 p")
pl.plot(lam,r2,label="10 sh 40 p")
pl.plot(lam,r3,label="40 sh 40 p")
pl.plot(lam,r4,label="70 sh 40 p")
pl.plot(lam,r5,label="40 sh 100 p")
pl.xlabel(u"A")
pl.title("Comparison")
pl.legend(loc="best")
pl.show()