-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.py
409 lines (344 loc) · 15 KB
/
analyzer.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"""
ANALYZER
This program is meant to read ORION I & II data outputs and
analyze them in the context of protostellar disk misalignment.
The pertinent information in each data output are the star
particles' indices, masses, postions, and angular momenta
directions, as well as the angular momentum and structure of
the gas within ~100-150 AU of each star particle.
Created by Drummond Fielding
on June 1, 2013
in Berkeley, California
while eating a peach
"""
#==============================================================================#
#==============================================================================#
"""
The function STAR_GATHERER takes all the data from an output
and returns:
nstars = number of stars
indices = array containing the index of each star particle
masses = array containing the mass of each star particle
positions = array containing the (x,y,z) position of each star
particle
L_star = array containing the unit angular momentum vector of
each star particle
"""
def STAR_GATHERER(pf,data):
indices = data['particle_id']
masses = data['particle_mass']
nstars = len(masses)
positions = np.zeros((nstars, 3))
L_star = np.zeros((nstars, 3))
for i in xrange(nstars):
positions[i] = [data['particle_position_' + direction][i] for direction in ['x', 'y', 'z']]
L_star_vec = np.array([data['particle_angmomen_' + direction][i] for direction in ['x', 'y', 'z']])
L_star[i] = L_star_vec/np.sqrt(np.square(L_star_vec).sum())
return nstars, indices, masses, positions, L_star
#==============================================================================#
"""
The function STAR_CLEANER takes the results of STAR_GATHERER
and removes the stars that do not match the given criterion.
M_min = minimum mass in units of M_sun
D_min = minumum distance between stars that have mass above
M_min but maybe be too close to be distinguished from
a nearby companion star. Returns only the most massive
of the two. D_min should be in AU
TO DO: Distance cutoff
"""
def STAR_CLEANER(M_min, D_min, nstars, indices, masses, positions, L_star):
# Mass Cleaning
clean_nstars = 0
clean_indices = np.array([])
clean_masses = np.array([])
for i in xrange(nstars):
if masses[i]/M_sun >= M_min:
clean_nstars +=1
clean_indices = np.append(clean_indices, indices[i])
clean_masses = np.append(clean_masses, masses[i])
clean_positions = np.zeros((clean_nstars,3))
clean_L_star = np.zeros((clean_nstars,3))
for i in xrange(clean_nstars):
for j in xrange(nstars):
if clean_indices[i] == indices[j]:
clean_positions[i] = positions[j]
clean_L_star[i] = L_star[j]
# # Distance Cleaning, tc stands for totally clean
# tc_nstars = 0
# tc_indices = np.array([])
# tc_masses = np.array([])
# for i in xrange(clean_nstars):
# for j in xrange(clean_nstars):
# if i != j and np.sqrt(np.sum( (clean_positions[i]-clean_positions[j])**2 )) <= D_min*1.5e13:
# if clean_masses[i] > clean_masses[j]:
# tc_nstars +=1
# tc_indices = np.append()
# else:
# break
# tc_positions = np.zeros((tc_nstars,3))
# tc_L_star = np.zeros((tc_nstars,3))
return clean_nstars, clean_indices, clean_masses, clean_positions, \
clean_L_star
#==============================================================================#
"""
The function DISK_HUNTER looks at the circumstellar material
and determines the misalignment angle as a function of radius,
as well as the mass profile as a function of R and PHI (the
polar azimuthal angle). The two mass profiles allow drastic
changes in misalignment to be distinguished from over/under
densities near the star that may be transient inflowing or
outflowing material.
TO DO: Angular mass profile
for now the angular mass profile will be left out because
defining cylindrical-wedge shaped objects is non trivial.
"""
def DISK_HUNTER(pf, position, L_star, radii):
mass_profile = np.array([])
angle_profile = np.array([])
for radius in radii:
sp = pf.h.sphere(position, radius)
L_disk = -1.*sp.quantities['AngularMomentumVector']()
angle_profile = np.append(angle_profile,np.arccos(np.dot(L_disk, L_star)))
mass_profile = np.append(mass_profile,sp.quantities["TotalQuantity"]("CellMass"))
return angle_profile, mass_profile
#==============================================================================#
"""
The function FILE_COUNTER returns the number of orion outputs are in the
directory supplied.
"""
def FILE_COUNTER(directory):
filelist = np.array([])
for file in os.listdir(directory):
if fnmatch.fnmatch(file, '*.orion') or fnmatch.fnmatch(file, '*.hdf5'):
filelist = np.append(filelist, file)
nfiles = int(len(filelist))
if nfiles == 0:
print "no files found, make sure they end with .orion or .hdf5 \
and are in the directory given"
return nfiles
#==============================================================================#
from yt.pmods import *
import glob
import fnmatch
day = 8.64e4 # seconds
year = 365.2425 * day # seconds
M_sun = 1.9891e33 # gm
# This is for use on the NERSC machines
# import matplotlib
# matplotlib.use('Agg')
import matplotlib.pyplot as plt
# import pylab as plt
#from matplotlib import rc
#rc('text', usetex=True)
#rc('font', family='serif')
#########################################################################
num_procs = 24 # make sure to change this when using different computers #
#########################################################################
nfiles = FILE_COUNTER("/clusterfs/henyey/dfielding/andrew/")
if num_procs < 2.0*nfiles:
n_parallel = 1
if num_procs >= 2.0*nfiles:
n_parallel = num_procs//nfiles
print 'n_parallel = ' + str(n_parallel)
t0=time.time()
ts = TimeSeriesData.from_filenames("/clusterfs/henyey/dfielding/andrew/data.*.hdf5", parallel = n_parallel)
if len(ts) != nfiles:
print "UH OH the number of files do not match!"
nradii = 40
min_radii = 5.
max_radii = 200.
radii = np.logspace(np.log10(min_radii*1.5e13), np.log10(max_radii*1.5e13),nradii)
my_rank = ytcfg.getint("yt", "__topcomm_parallel_rank")
if my_rank == 0:
print 'the number of files in the time series is ' + str(nfiles)
my_storage = {}
for sto, pf in ts.piter(storage = my_storage):
print 'working on', pf.parameter_filename, 'which is at time:', pf.current_time/year
data = pf.h.all_data()
nstars, indices, masses, positions, L_star = STAR_GATHERER(pf,data)
nstars, indices, masses, positions, L_star = STAR_CLEANER(0.05, 0., nstars, indices, masses, positions, L_star)
angle_profiles = np.zeros((nstars,nradii))
mass_profiles = np.zeros((nstars,nradii))
for i in xrange(int(nstars)):
print "I am processor "+str( my_rank )+" and I am working on " + str(i+1)+' out of '+str(nstars)
angle_profiles[i], mass_profiles[i] = DISK_HUNTER(pf,positions[i], L_star[i], radii)
print 'processor '+str(my_rank)+' is done'
sto.result = nstars, indices, masses, positions, L_star, angle_profiles, mass_profiles, pf.current_time
t1=time.time()
if my_rank == 0:
print 'the time it took to gather and clean all stars, and hunt their disks was:',t1-t0, 'seconds'
max_nstar = 0
uniq_indices = np.array([])
for i in range(nfiles):
uniq_indices = np.append(uniq_indices, my_storage[i][1])
if my_storage[i][0] > max_nstar:
max_nstar = my_storage[i][0]
uniq_indices = np.unique(uniq_indices)
if my_rank == 0:
print 'most number of stars in an output: ', max_nstar
print 'the unique indices are:', uniq_indices
t2 = time.time()
stars = {}
for i in xrange(len(uniq_indices)):
ntimes=0
matches1 = np.array([])
matches2 = np.array([])
for j in xrange(nfiles):
for k in xrange(int(my_storage[j][0])):
if uniq_indices[i] == my_storage[j][1][k]:
ntimes += 1
matches1 = np.append(matches1, j)
matches2 = np.append(matches2, k)
index = uniq_indices[i]
mass_hist = np.zeros(ntimes)
position_hist = np.zeros((ntimes, 3))
L_star_hist = np.zeros((ntimes, 3))
angle_profile_hist = np.zeros((ntimes, nradii))
mass_profile_hist = np.zeros((ntimes, nradii))
age = np.zeros(ntimes)
for k in xrange(ntimes):
mass_hist[k] = my_storage[matches1[k]][2][matches2[k]]
position_hist[k] = my_storage[matches1[k]][3][matches2[k]]
L_star_hist[k] = my_storage[matches1[k]][4][matches2[k]]
angle_profile_hist[k] = my_storage[matches1[k]][5][matches2[k]]
mass_profile_hist[k] = my_storage[matches1[k]][6][matches2[k]]
age[k] = my_storage[matches1[k]][7]
stars[i] = (ntimes, index, mass_hist, position_hist, L_star_hist,angle_profile_hist,mass_profile_hist,age)
t3 = time.time()
if my_rank == 1:
print 'the time it took arrange the stars in their dictionary was:',t3-t2, 'seconds'
for i in range(0+my_rank,len(stars),num_procs):
ntimes = int(stars[i][0])
star_masses=np.zeros(nradii)
star_ages = np.zeros(nradii)
misalignment_angle_profiles = np.zeros((ntimes, nradii))
mass_profiles = np.zeros((ntimes, nradii))
for j in range(ntimes):
star_masses[j] = stars[i][2][j]
misalignment_angle_profiles[j] = stars[i][5][j]
mass_profiles[j] = stars[i][6][j]
star_ages[j] = stars[i][7][j] / year
filename='star_'+str(stars[i][1])+'_misalignment_mass_profile.txt'
np.savetxt(filename,np.c_[star_ages, star_masses, np.transpose(misalignment_angle_profiles),np.transpose(mass_profiles),radii/1.5e13], header = 'disk star misalignment analysis of myers data. column 0: age(years), column 1: masses(g), column 2-2+ntimes: misalignment profiles, column 3+ntimes - 3+2ntimes: mass profiles, column 4+2ntimes: radii(AU)')
# my_storage = {}
# for sto, pf in ts.piter(storage = my_storage):
# print 'I am processor '+str(my_rank)
# print 'I am processor '+str(my_rank)+' and I am working on', pf.parameter_filename, 'which is at time:', pf.current_time/year
# data = pf.h.all_data()
# nstars, indices, masses, positions, L_star = STAR_GATHERER(pf,data)
# nstars, indices, masses, positions, L_star = STAR_CLEANER(2.05, 0., nstars, indices, masses, positions, L_star)
# angle_profiles = np.zeros((nstars,nradii))
# mass_profiles = np.zeros((nstars,nradii))
# for i in xrange(int(nstars)):
# print "I am processor "+str( my_rank )+" and I am working on " + str(i+1)+' out of '+str(nstars)
# angle_profiles[i], mass_profiles[i] = DISK_HUNTER(pf,positions[i], L_star[i], radii)
# sto.result = nstars, indices, masses, positions, L_star, angle_profiles, mass_profiles, pf.current_time
# t1=time.time()
# if ytcfg.getint("yt", "__topcomm_parallel_rank") == 0:
# print 'the time it took to gather and clean all stars, and hunt their disks was:',t1-t0, 'seconds'
# max_nstar = 0
# uniq_indices = np.array([])
# for i in range(nfiles):
# # print 'file '+str(i)+' has '+ str(my_storage[i][0]) + ' star(s)'
# # print 'which have the following indices'
# # for j in range(int(my_storage[i][0])):
# # print my_storage[i][1][j]
# uniq_indices = np.append(uniq_indices, my_storage[i][1])
# # print 'and have the following masses', my_storage[i][2]/M_sun
# # print 'and have the following positions', my_storage[i][3]
# # print 'and have the following angular momenta', my_storage[i][4]
# if my_storage[i][0] > max_nstar:
# max_nstar = my_storage[i][0]
# uniq_indices = np.unique(uniq_indices)
# if ytcfg.getint("yt", "__topcomm_parallel_rank") == 0:
# print 'most number of stars in an output: ', max_nstar
# print 'the unique indices are:', uniq_indices
# t2 = time.time()
# stars = {}
# for i in xrange(len(uniq_indices)):
# ntimes=0
# matches1 = np.array([])
# matches2 = np.array([])
# for j in xrange(nfiles):
# for k in xrange(int(my_storage[j][0])):
# if uniq_indices[i] == my_storage[j][1][k]:
# ntimes += 1
# matches1 = np.append(matches1, j)
# matches2 = np.append(matches2, k)
# index = uniq_indices[i]
# mass_hist = np.zeros(ntimes)
# position_hist = np.zeros((ntimes, 3))
# L_star_hist = np.zeros((ntimes, 3))
# angle_profile_hist = np.zeros((ntimes, nradii))
# mass_profile_hist = np.zeros((ntimes, nradii))
# age = np.zeros(ntimes)
# for k in xrange(ntimes):
# mass_hist[k] = my_storage[matches1[k]][2][matches2[k]]
# position_hist[k] = my_storage[matches1[k]][3][matches2[k]]
# L_star_hist[k] = my_storage[matches1[k]][4][matches2[k]]
# angle_profile_hist[k] = my_storage[matches1[k]][5][matches2[k]]
# mass_profile_hist[k] = my_storage[matches1[k]][6][matches2[k]]
# age[k] = my_storage[matches1[k]][7]
# stars[i] = (ntimes, index, mass_hist, position_hist, L_star_hist,angle_profile_hist,mass_profile_hist,age)
# t3 = time.time()
# if ytcfg.getint("yt", "__topcomm_parallel_rank") == 1:
# print 'the time it took arrange the stars in their dictionary was:',t3-t2, 'seconds'
# my_rank = ytcfg.getint("yt", "__topcomm_parallel_rank")
# for i in range(0+my_rank,len(stars),num_procs):
# ntimes = int(stars[i][0])
# for j in range(ntimes):
# star_mass = stars[i][2][j]
# misalignment_angle_profile = stars[i][5][j]
# mass_profile = stars[i][6][j]
# age = stars[i][7][j] / year
# ax1 = plt.subplot(211)
# plt.ylabel(r'$ \mathrm{misalignment \/ angle \/}(^{\circ})$')
# ax2 = plt.subplot(212, sharex = ax1)
# plt.ylabel(r'$M_{enc}/M_\odot$')
# ax1.plot(radii/1.5e13, 180.0 * misalignment_angle_profile / np.pi, label = r'$\mathrm{time\/=\/}'+str(round(age,1)) + ' \mathrm{\/years}$')
# ax2.plot(radii/1.5e13, mass_profile/ M_sun, label = r'$\mathrm{star\/mass} ='+str(round(star_mass/M_sun,4)) + '\/M_\odot$')
# ax2.legend(loc='upper left')
# ax1.legend()
# ax1.set_ylim((0.,180.))
# plt.xlabel(r'$\mathrm{Radius \/ (AU)}$')
# plt.savefig('test_star_'+str(stars[i][1])+'_misalignment_mass_profile.png')
# plt.clf()
# # my_rank = ytcfg.getint("yt", "__topcomm_parallel_rank")
# # for i in range(0+my_rank,len(stars),num_procs):
# # ntimes = int(stars[i][0])
# # for j in range(ntimes):
# # misalignment_angle_profile = stars[i][5][j]
# # mass_profile = stars[i][6][j]
# # print max(mass_profile), stars[i][3]
# # age = stars[i][7][j] / year
# # plt.plot(radii/1.5e13, mass_profile/ M_sun, label = str(age) + ' years')
# # plt.ylabel(r'$M_{enc}/M_\odot$')
# # plt.legend(loc='upper left')
# # plt.xlabel(r'Radius (AU)')
# # plt.savefig('star_'+str(stars[i][1])+'_mass_profile.png')
# # # for j in range(int(stars[i][0])):
# # # print j, str(ytcfg.getint("yt", "__topcomm_parallel_rank"))
# #==============================================================================#
# """
# The function CAN_OPENER finds, then opens the output files in the
# supplied directory and outputs the parameterfile (pf) and all the data (dd)
# This might be better done using yt's built in TimeSeriesData
# """
# def CAN_OPENER(directory):
# filelist = np.array([])
# for file in os.listdir(directory):
# if fnmatch.fnmatch(file, '*.orion') or fnmatch.fnmatch(file, '*.hdf5'):
# filelist = np.append(filelist, file)
# nfiles = int(len(filelist))
# if nfiles == 0:
# print "no files found, make sure they end with .orion or .hdf5 \
# and are in the directory given"
# pfs = np.array([])
# all_data = np.array([])
# for i in xrange(nfiles):
# pf = load(directory+filelist)
# data = pf.h.all_data()
# pfs = np.append(pfs,pf)
# all_data = np.append(all_data,data)
# return pfs, all_data
# #==============================================================================#