-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarcluster_shrinker.py
More file actions
236 lines (209 loc) · 11.3 KB
/
starcluster_shrinker.py
File metadata and controls
236 lines (209 loc) · 11.3 KB
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
# This file is similar to shrinker, but fits only to the stellar component of objects from shrinker.
# Gives a tight ellipsoid containing all stars in the star cluster, as well as associated gas. Useful for Kennicut-Schmidt relation.
import snapHDF5
import numpy as np
from scipy.spatial.distance import cdist
from annikaEllipsoid import *
import readsubfHDF5
import pickle
import time
import progressbar
def dx_wrap(dx,box):
#wraps to account for period boundary conditions. This mutates the original entry
idx = dx > +box/2.0
dx[idx] -= box
idx = dx < -box/2.0
dx[idx] += box
return dx
def dist2(dx,dy,dz,box):
#Calculates distance taking into account periodic boundary conditions
return dx_wrap(dx,box)**2 + dx_wrap(dy,box)**2 + dx_wrap(dz,box)**2
widgets = [
"Progress :",
' ', progressbar.Percentage(),
' ', progressbar.GranularBar(),
' ', progressbar.AdaptiveETA(),
] # For progress bar
res = '14Mpc'
vel = 'Sig0'
s_res = res
s_vel = vel
filename = "Documents/SF_Sig0/"
COMsInOrderMultiZ = []
starLocationsInOrderMultiZ = []
starIDsInOrderMultiZ = []
mStar_ellipsoid_multiZ = []
radii_ellipsoid_multiZ = []
rotation_ellipsoid_multiZ = []
cm_ellipsoid_multiZ = []
snapkey = [] #Fill this in with the snap numbers to use
for snapnum2 in snapkey:
snapnum = snapnum2
snapfile = filename+"snap_"+str(snapnum).zfill(3)+".hdf5"
with open(filename+'shrinker'+res+'_'+vel+'_'+str(snapnum)+'.dat','rb') as f:
shrunken = pickle.load(f)
filename2 = filename + "GasOnly_FOF" #Used for readsubfHDF5
filename3 = filename2 + "/snap-groupordered_" + str(snapnum).zfill(3) #Used for snapHDF5
cat = readsubfHDF5.subfind_catalog(filename2, snapnum)
header = snapHDF5.snapshot_header(filename3)
red = header.redshift
atime = header.time
boxSize = header.boxsize
print(boxSize)
#COMsInOrder = []
pGas= snapHDF5.read_block(filename3,"POS ", parttype=0)
mGas= snapHDF5.read_block(filename3,"MASS", parttype=0)
pStar = snapHDF5.read_block(filename3,"POS ", parttype=4)
mStar = snapHDF5.read_block(filename3,"MASS", parttype=4)
IDGas = snapHDF5.read_block(filename3,"ID ", parttype=0)
IDStar = snapHDF5.read_block(filename3,"ID ", parttype=4)
halo100_indices= np.where((cat.GroupLenType[:,0]+cat.GroupLenType[:,4]) >100)[0]
startAllGas = []
endAllGas = []
for i in halo100_indices:
startAllGas += [np.sum(cat.GroupLenType[:i,0])]
endAllGas += [startAllGas[-1] + cat.GroupLenType[i,0]]
radii_ellipsoid = []
rotation_ellipsoid = []
cm_ellipsoid = []
mGas_ellipsoid = []
mStar_ellipsoid = []
gasIDs = []
starindices = []
starIDs = []
print(halo100_indices)
with progressbar.ProgressBar(max_value=len(halo100_indices), widgets=widgets) as bar:
progressCounter = -1
for i in halo100_indices:
progressCounter += 1
if progressCounter % 20 == 0:
bar.update(i)
cm = shrunken['cm'][i]
rotation = shrunken['rotation'][i]
radii = shrunken['radii'][i]
mStarSIGO = shrunken['mStar'][i]
starIDsSIGO = shrunken['starIDs'][i]
gasIDsSIGO = shrunken['gasIDs'][i]
if len(starIDsSIGO) > 1:
pSIGO = np.isin(IDStar, list(starIDsSIGO))
locStars = pStar[pSIGO]
COM = np.mean(locStars,axis=0)
initialStarLocations = np.array(locStars)
starLocations = []
tempStarIDs = []
if len(initialStarLocations) > 0:
Precentered = np.array(dx_wrap(locStars - COM,boxSize))
dists = np.sqrt(dist2(Precentered[:,0],Precentered[:,1],Precentered[:,2],boxSize))
maxAxis = np.max(np.abs(dists))
maxSeparation = maxAxis
print(maxSeparation)
inEll = (Precentered[:,0]**2./maxSeparation**2. + Precentered[:,1]**2./maxSeparation**2 + Precentered[:,2]**2./maxSeparation**2)<=1.
starLocations.append(list(locStars[inEll]))
tempStarIDs.append(list((IDStar[pSIGO])[inEll]))
xCoords = []
yCoords = []
COM = np.mean(locStars[inEll],axis=0)
P = locStars[inEll]
M = (mStar[pSIGO])[inEll]
if len(P) > 0:
cm = np.average(P,axis=0)#,weights = R
Precentered = dx_wrap(P - cm,boxSize)
dists = np.sqrt(dist2(P[:,0]-cm[0],P[:,1]-cm[1],P[:,2]-cm[2],boxSize))
maxAxis = np.max(np.abs(dists))
ratios, evecs = axis(Precentered,maxAxis,axes_out=True,quiet=True)
#Shrink ellipsoid by increments of .5% of the maximum axis until density ratio of
#lengths of axes of shrunken ellipse to the original ellipse is greater than the ratio of
#the number of gas cells enclosed in the shrunken ellipose to the original ellipse
#or until 20% of the total number of gas cells are removed
if ratios[0] > 0. and ratios[1] > 0.: #accounts for erros in fit
evecs = np.array(evecs)
Precentered = np.array([np.dot(pp,evecs.T) for pp in Precentered])
tempAxis = maxAxis
inEll = Precentered[:,0]**2/ratios[0]**2+Precentered[:,1]**2/ratios[1]**2+Precentered[:,2]**2 <= maxAxis**2
numGasOrig = np.sum(inEll)
skipThis=False
if maxAxis == 0. or numGasOrig == 0.:
skipThis = True
if not skipThis:
radii = np.array([tempAxis*ratios[0], tempAxis*ratios[1], tempAxis])
rotation = np.array(evecs)
radii_ellipsoid += [list(radii)]
rotation_ellipsoid += [[list(r) for r in rotation]]
cm_ellipsoid += [list(cm)]
#Calculate Star mass
tempPosStar = dx_wrap(locStars-cm,boxSize)
#Only look for stars within the sphere generated by the max ellipsoid axis
nearidx, = np.where(dist2(locStars[:,0]-cm[0],locStars[:,1]-cm[1],locStars[:,2]-cm[2],boxSize)<=tempAxis**2)
starindices += [nearidx]
starIDs += [(IDStar[pSIGO])[nearidx]]
tempPosStar = tempPosStar[nearidx]
tempPosStar = np.array([np.dot(pp,evecs.T) for pp in tempPosStar])
stellarmass = 0
if len(tempPosStar) == 0:
#SIGO has no Stars
mStar_ellipsoid += [0.]
else:
StarinEll = tempPosStar[:,0]**2/ratios[0]**2 + tempPosStar[:,1]**2/ratios[1]**2 + tempPosStar[:,2]**2 <= tempAxis**2
stellarmass = np.sum(mStar[pSIGO][nearidx][StarinEll])
mStar_ellipsoid += [stellarmass]
#Calculate Gas mass
pGasSIGO = np.isin(IDGas, list(gasIDsSIGO))
locGas = pGas[pGasSIGO]
tempPosGas = dx_wrap(locGas-cm,boxSize)
#Only look for gas within the sphere generated by the max ellipsoid axis
nearidx, = np.where(dist2(locGas[:,0]-cm[0],locGas[:,1]-cm[1],locGas[:,2]-cm[2],boxSize)<=tempAxis**2)
gasIDs += [IDGas[pGasSIGO][nearidx]]
tempPosGas = tempPosGas[nearidx]
tempPosGas = np.array([np.dot(pp,evecs.T) for pp in tempPosGas])
gasmass = 0
if len(tempPosGas) == 0:
#SIGO has no Gas
mGas_ellipsoid += [0.]
else:
GasinEll = tempPosGas[:,0]**2/ratios[0]**2 + tempPosGas[:,1]**2/ratios[1]**2 + tempPosGas[:,2]**2 <= tempAxis**2
gasmass = np.sum(mGas[pGasSIGO][nearidx][GasinEll])
mGas_ellipsoid += [gasmass]
#mStar_ellipsoid += [1.*np.sum(StarinEll)*massStarParticle]
else:
#overRadii += [-1.]
radii_ellipsoid += [[-1.,-1.,-1.]]
rotation_ellipsoid +=[[[-1.,-1.,-1.],[-1.,-1.,-1.],[-1.,-1.,-1.]]]
cm_ellipsoid += [[-1.,-1.,-1.]]
mGas_ellipsoid += [-1.]
mStar_ellipsoid += [-1.]
gasIDs += [[-1]]
starindices += [[-1]]
starIDs += [[-1]]
else:
print(i, ' no stars in initial ellipsoid')
#overRadii += [-1.]
radii_ellipsoid += [[-1.,-1.,-1.]]
rotation_ellipsoid +=[[[-1.,-1.,-1.],[-1.,-1.,-1.],[-1.,-1.,-1.]]]
cm_ellipsoid += [[-1.,-1.,-1.]]
mGas_ellipsoid += [-1.]
mStar_ellipsoid += [-1.]
gasIDs += [[-1]]
starindices += [[-1]]
starIDs += [[-1]]
else:
radii_ellipsoid += [[-1.,-1.,-1.]]
rotation_ellipsoid +=[[[-1.,-1.,-1.],[-1.,-1.,-1.],[-1.,-1.,-1.]]]
cm_ellipsoid += [[-1.,-1.,-1.]]
mGas_ellipsoid += [-1.]
mStar_ellipsoid += [-1.]
gasIDs += [[-1]]
starindices += [[-1]]
starIDs += [[-1]]
shrunken = {} #Initialize dict of results
shrunken['radii'] = np.array(radii_ellipsoid) #each ellipsoid axis value from min to max
shrunken['rotation'] = np.array(rotation_ellipsoid) #rotation matrix to get into principal frame
shrunken['cm'] = np.array(cm_ellipsoid) #center of mass
shrunken['mGas'] = np.array(mGas_ellipsoid) #gas mass in the ellipsoid
shrunken['starindices'] = np.array(starindices) #star indices in the ellipsoid
shrunken['gasIDs'] = np.array(gasIDs) #gas indices in the ellipsoid
shrunken['starIDs'] = np.array(starIDs) #star indices in the ellipsoid
shrunken['mStar'] = np.array(mStar_ellipsoid) #star mass in the ellipsoid
print(radii_ellipsoid)
print(mGas_ellipsoid)
with open(filename+'star_only_shrinker'+res+'_'+vel+'_'+str(snapnum)+'.dat','wb') as f:
pickle.dump(shrunken, f)