-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_thermo_slabs.py
370 lines (302 loc) · 14.4 KB
/
plot_thermo_slabs.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
####################################################
# Plot a large 12 panel plot with thin slab
# projection, phase plots, SFR, and disk mass growth
####################################################
import yt
yt.enable_parallelism()
from unyt.physical_constants import gravitational_constant_cgs as G
import matplotlib.pyplot as plt
from matplotlib import rcParams
from matplotlib.colors import LogNorm, SymLogNorm
from matplotlib.ticker import MultipleLocator
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
import wquantiles as wq
from calc_enclosed_mass import *
rcParams.update({'font.size': 14})
masses = np.genfromtxt('masses_over_time.txt')
sfr = np.genfromtxt('sfr.txt')
datasets = yt.load('DD????/DD????')
for ds in datasets.piter(dynamic=False, ):
center = ds.quan(0.5,'code_length')
rs = ds.quan(3.5,'kpc')
widths = [
ds.quan(100,'kpc'),
ds.quan(100,'kpc'),
ds.quan(200,'kpc'),
ds.quan(400,'kpc'),
# ds.quan(800,'kpc')
]
thicknesses = [
rs,
rs,
rs,
rs,
# rs*2
]
labels = [
'100kpc',
'100kpc_face',
'200kpc',
'400kpc',
# '800kpc'
]
for width, thickness, label in zip(widths, thicknesses, labels):
fig, ax = plt.subplots(nrows=3, ncols=4, figsize=(24.5,15.5))
if 'face' in label:
view = 'z'
# Thin slab is used for projections so I can avoid weighting by e.g. mass
rect = ds.region([center, center, center],
[center-width/2, center-width/2, center-thickness/2],
[center+width/2, center+width/2, center+thickness/2])
else:
view = 'x'
# Thin slab is used for projections so I can avoid weighting by e.g. mass
rect = ds.region([center, center, center],
[center-thickness/2, center-width/2, center-width/2],
[center+thickness/2, center+width/2, center+width/2])
assert (rect.get_field_parameter("center") == center).all()
# Sphere is used for calculating average tff
sph = ds.sphere([center, center, center], width)
#
# Projection Plots
#
fields = ['density','temperature','pressure','entropy',
'radial_velocity','cooling_time']
p = yt.ProjectionPlot(ds, view, fields, width=width,
data_source=rect, weight_field ='ones')
# Extract fixed resolution buffers
p_frb = p.data_source.to_frb(width, 512)
d_arr = np.array(p_frb['density'])
t_arr = np.array(p_frb['temperature'])
p_arr = np.array(p_frb['pressure'])
k_arr = np.array(p_frb['entropy'])
v_arr = np.array(p_frb['radial_velocity']) / 1e5 # km/s
c_arr = np.array(p_frb['cooling_time']) / 3.16887e8 / 1e9 # Gyr
# Plot FRBs
extent = (-width/2, width/2, -width/2, width/2)
d_im = ax[0,0].imshow(d_arr, origin='lower', norm=LogNorm(1e-32,1e-24),
extent=extent)
t_im = ax[1,0].imshow(t_arr, origin='lower', norm=LogNorm(1e3,1e8),
extent=extent, cmap='magma')
p_im = ax[0,1].imshow(p_arr, origin='lower', norm=LogNorm(1e-21,1e-13),
extent=extent, cmap='inferno')
k_im = ax[1,1].imshow(k_arr, origin='lower', norm=LogNorm(1e-2,1e6),
extent=extent, cmap='cividis')
v_im = ax[0,2].imshow(v_arr, origin='lower',
norm=SymLogNorm(1, linscale=0.3, base=10,
vmin=-3e3, vmax=3e3),
extent=extent, cmap='coolwarm')
c_im = ax[1,2].imshow(c_arr, origin='lower', norm=LogNorm(1e-2,1e4),
extent=extent, cmap='twilight')
# Add colorbars
d_cb = fig.colorbar(d_im, ax=ax[0,0], pad=0.01, shrink=0.9)
t_cb = fig.colorbar(t_im, ax=ax[1,0], pad=0.01, shrink=0.9)
p_cb = fig.colorbar(p_im, ax=ax[0,1], pad=0.01, shrink=0.9)
k_cb = fig.colorbar(k_im, ax=ax[1,1], pad=0.01, shrink=0.9)
v_cb = fig.colorbar(v_im, ax=ax[0,2], pad=0.01, shrink=0.9)
c_cb = fig.colorbar(c_im, ax=ax[1,2], pad=0.01, shrink=0.9)
# Add colorbar labels
d_cb.set_label(r'< Density > [g cm$^{-3}$]')
t_cb.set_label(r'< Temperature > [K]')
p_cb.set_label(r'< Pressure > [dyn cm$^{-2}$]')
k_cb.set_label(r'< Entropy > [keV cm$^2$]')
v_cb.set_label(r'< Radial Velocity > [km/s]')
c_cb.set_label(r'< Cooling Time > [Gyr]')
#
# Phase Diagrams
#
dt_ph = yt.PhasePlot(rect, 'density', 'temperature', 'cell_mass').profile
pk_ph = yt.PhasePlot(rect, 'pressure', 'entropy', 'cell_mass').profile
rc_ph = yt.PhasePlot(rect, 'radius', 'cooling_time', 'cell_mass').profile
rk_ph = yt.PhasePlot(rect, 'radius', 'entropy', 'cell_mass').profile
# 16th, 50th, and 84th precentile 1D profiles
dt_med = np.ones(dt_ph.x_bins.size-1) * np.nan
dt_16 = np.ones(dt_ph.x_bins.size-1) * np.nan
dt_84 = np.ones(dt_ph.x_bins.size-1) * np.nan
d_binner = np.digitize(rect['density'], dt_ph.x_bins)
for i in range(1, dt_ph.x_bins.size):
this_bin = d_binner==i
try:
dt_med[i-1] = wq.median(rect['temperature'][this_bin],
rect['cell_mass'][this_bin])
dt_16[i-1] = wq.quantile(rect['temperature'][this_bin],
rect['cell_mass'][this_bin],
0.16)
dt_84[i-1] = wq.quantile(rect['temperature'][this_bin],
rect['cell_mass'][this_bin],
0.84)
except ValueError:
continue
pk_med = np.ones(pk_ph.x_bins.size-1) * np.nan
pk_16 = np.ones(pk_ph.x_bins.size-1) * np.nan
pk_84 = np.ones(pk_ph.x_bins.size-1) * np.nan
p_binner = np.digitize(rect['pressure'], pk_ph.x_bins)
for i in range(1, pk_ph.x_bins.size):
this_bin = p_binner==i
try:
pk_med[i-1] = wq.median(rect['entropy'][this_bin],
rect['cell_mass'][this_bin])
pk_16[i-1] = wq.quantile(rect['entropy'][this_bin],
rect['cell_mass'][this_bin],
0.16)
pk_84[i-1] = wq.quantile(rect['entropy'][this_bin],
rect['cell_mass'][this_bin],
0.84)
except ValueError:
continue
rc_med = np.ones(rc_ph.x_bins.size-1) * np.nan
rc_16 = np.ones(rc_ph.x_bins.size-1) * np.nan
rc_84 = np.ones(rc_ph.x_bins.size-1) * np.nan
r_binner = np.digitize(rect['radius'], rc_ph.x_bins)
for i in range(1, rc_ph.x_bins.size):
this_bin = r_binner==i
try:
rc_med[i-1] = wq.median(rect['cooling_time'][this_bin].to("Gyr"),
rect['cell_mass'][this_bin])
rc_16[i-1] = wq.quantile(rect['cooling_time'][this_bin].to("Gyr"),
rect['cell_mass'][this_bin],
0.16)
rc_84[i-1] = wq.quantile(rect['cooling_time'][this_bin].to("Gyr"),
rect['cell_mass'][this_bin],
0.84)
except ValueError:
continue
rk_med = np.ones(rk_ph.x_bins.size-1) * np.nan
rk_16 = np.ones(rk_ph.x_bins.size-1) * np.nan
rk_84 = np.ones(rk_ph.x_bins.size-1) * np.nan
r_binner = np.digitize(rect['radius'], rk_ph.x_bins)
for i in range(1, rk_ph.x_bins.size):
this_bin = r_binner==i
try:
rk_med[i-1] = wq.median(rect['entropy'][this_bin],
rect['cell_mass'][this_bin])
rk_16[i-1] = wq.quantile(rect['entropy'][this_bin],
rect['cell_mass'][this_bin],
0.16)
rk_84[i-1] = wq.quantile(rect['entropy'][this_bin],
rect['cell_mass'][this_bin],
0.84)
except ValueError:
continue
# Calculate tff for tcool vs tff plot
# tff is calculated for material enclosed within a sphere
# as MN_accel averages the stellar potential over samples of a sphere
# (technically of a circle b/c of azimuthal symmetry)
Mtot_r = NFW_mass_enclosed(rc_ph.x) + \
cell_and_particle_mass_enclosed(sph, rc_ph.x_bins.v)
g_r = G * Mtot_r / np.power(rc_ph.x, 2)
g_r = g_r.to('cm/s**2') + MN_accel(rc_ph.x)
t_ff = np.sqrt( 2*rc_ph.x / g_r )
# Plot phase diagrams
dt_im = ax[2,0].pcolormesh(dt_ph.x_bins, dt_ph.y_bins,
dt_ph['cell_mass'].T.to('Msun'),
norm=LogNorm(1e-4,1e6), cmap='viridis')
pk_im = ax[2,1].pcolormesh(pk_ph.x_bins, pk_ph.y_bins,
pk_ph['cell_mass'].T.to('Msun'),
norm=LogNorm(1e-4,1e6), cmap='cividis')
rc_im = ax[2,2].pcolormesh(rc_ph.x_bins.to('kpc'), rc_ph.y_bins.to('Gyr'),
rc_ph['cell_mass'].T.to('Msun'),
norm=LogNorm(1e-3,1e7), cmap='inferno')
rk_im = ax[2,3].pcolormesh(rk_ph.x_bins.to('kpc'), rk_ph.y_bins,
rk_ph['cell_mass'].T.to('Msun'),
norm=LogNorm(1e-3,1e7), cmap='magma')
# Plot percentiles
ax[2,0].plot(dt_ph.x, dt_med, 'k-')
ax[2,0].plot(dt_ph.x, dt_16, 'k--')
ax[2,0].plot(dt_ph.x, dt_84, 'k--')
ax[2,1].plot(pk_ph.x, pk_med, 'k-')
ax[2,1].plot(pk_ph.x, pk_16, 'k--')
ax[2,1].plot(pk_ph.x, pk_84, 'k--')
ax[2,2].plot(rc_ph.x.to('kpc'), rc_med, 'k-')
ax[2,2].plot(rc_ph.x.to('kpc'), rc_16, 'k--')
ax[2,2].plot(rc_ph.x.to('kpc'), rc_84, 'k--')
ax[2,3].plot(rk_ph.x.to('kpc'), rk_med, 'k-')
ax[2,3].plot(rk_ph.x.to('kpc'), rk_16, 'k--')
ax[2,3].plot(rk_ph.x.to('kpc'), rk_84, 'k--')
# Plot tff
ax[2,2].plot(rc_ph.x.to('kpc'), t_ff.to('Gyr'), 'g-')
ax[2,2].plot(rc_ph.x.to('kpc'), 10*t_ff.to('Gyr'), 'g-.')
# Adjust limits
ax[2,0].set_xlim(1e-34, 1e-21)
ax[2,0].set_xlabel(r'Density [g cm$^{-3}$]')
ax[2,0].set_ylim(1e1, 1e9)
ax[2,0].set_ylabel('Temperature [K]')
ax[2,1].set_xlim(1e-22, 1e-10)
ax[2,1].set_xlabel(r'Pressure [dyn cm$^{-2}$]')
ax[2,1].set_ylim(1e-7, 1e7)
ax[2,1].set_ylabel(r'Entropy [keV cm$^2$]')
ax[2,2].set_xlim(1e-1, 1e3)
ax[2,2].set_xlabel('Radius [kpc]')
ax[2,2].set_ylim(1e-6, 1e8)
ax[2,2].set_ylabel('Cooling Time [Gyr]')
ax[2,3].set_xlim(1e-1, 1e3)
ax[2,3].set_xlabel('Radius [kpc]')
ax[2,3].set_ylim(1e-7, 1e7)
ax[2,3].set_ylabel(r'Entropy [keV cm$^2$]')
# Add colorbars and labels
dt_cb = fig.colorbar(dt_im, ax=ax[2,0], pad=0.01, shrink=0.9)
pk_cb = fig.colorbar(pk_im, ax=ax[2,1], pad=0.01, shrink=0.9)
rc_cb = fig.colorbar(rc_im, ax=ax[2,2], pad=0.01, shrink=0.9)
rk_cb = fig.colorbar(rk_im, ax=ax[2,3], pad=0.01, shrink=0.9)
dt_cb.set_label(r'Cell Mass [M$_\odot$]')
pk_cb.set_label(r'Cell Mass [M$_\odot$]')
rc_cb.set_label(r'Cell Mass [M$_\odot$]')
rk_cb.set_label(r'Cell Mass [M$_\odot$]')
# Set plot view stuff
for i in range(4):
ax[2,i].set_xscale('log')
ax[2,i].set_yscale('log')
ax[2,i].set_aspect( 1.0 / ax[2,i].get_data_ratio() )
#
# Plots over time
#
# Translate output number to index
index = int(ds.basename[-4:])
# Plot SFR up to this point
time = masses[index,0]
past_sfr = sfr[:,0] <= time
ax[0,3].plot(sfr[past_sfr,0], sfr[past_sfr, 1], 'k')
ax[0,3].set_xlim(0, 4000)
ax[0,3].set_xlabel('Time [Myr]')
ax[0,3].set_ylim(0, 47)
ax[0,3].set_ylabel(r'SFR [M$_\odot$/yr]')
# Plot sum of SFR up to this point
sum_ax = ax[0,3].twinx()
sum_ax.plot(sfr[past_sfr,0], np.nancumsum(sfr[past_sfr, 1]),
color='C0', ls='--')
sum_ax.set_ylim(0, 10000)
sum_ax.tick_params(axis='y', labelcolor='C0')
sum_ax.set_ylabel(r'$\sum$SFR [M$_\odot$/yr]', color='C0')
ax[0,3].xaxis.tick_top()
ax[0,3].xaxis.set_label_position('top')
ax[0,3].yaxis.set_major_locator(MultipleLocator(5))
# Plot disk masses up to this point
ax[1,3].plot(masses[:index,0], masses[:index,1],
ls='--', label=r'$\rm M_{disk}$')
ax[1,3].plot(masses[:index,0], masses[:index,2],
ls='-.', label=r'$\rm M_\ast$')
ax[1,3].plot(masses[:index,0], masses[:index,3],
ls='-', label=r'$\rm M_{sum}$')
ax[1,3].set_xlim(0, 4000)
ax[1,3].set_xlabel('Time [Myr]')
ax[1,3].set_ylim(0, 1e10)
ax[1,3].set_ylabel(r'Mass [M$_\odot$]')
ax[1,3].legend()
#
# Final details & save
#
if 'face' in label:
ax[0,0].set_xlabel('x (kpc)')
ax[0,0].set_ylabel('y (kpc)')
else:
ax[0,0].set_xlabel('y (kpc)')
ax[0,0].set_ylabel('z (kpc)')
time = '{:.0f} Myr'.format(np.round(ds.current_time.to('Myr'),0))
ax[0,0].text(0.03, 0.03, time, transform=ax[0,0].transAxes,
color='white', size='x-large', weight='bold')
fig.suptitle("{} width, {} thickness".format(width, thickness), fontsize=28)
fig.tight_layout(rect=[0, 0, 1, 0.97])
fig.subplots_adjust(hspace=0.1, wspace=0.3)
fig.savefig('{}_thermo_slab_proj_{}.png'.format(ds.basename, label))
plt.close(fig)