def surface_density_bycells(S, agecut, poscut, a, h, cell_length=0.3, surface_frac=0.8, density_max_cell_center=True): #r = np.random.randn(100,3) r = [S['p'][:,0][agecut][poscut], S['p'][:,1][agecut][poscut], S['p'][:,2][agecut][poscut]] minx = np.min(S['p'][:,0][agecut][poscut])*a/h maxx = np.max(S['p'][:,0][agecut][poscut])*a/h miny = np.min(S['p'][:,1][agecut][poscut])*a/h maxy = np.max(S['p'][:,1][agecut][poscut])*a/h minz = np.min(S['p'][:,2][agecut][poscut])*a/h maxz = np.max(S['p'][:,2][agecut][poscut])*a/h numxcells = int((maxx-minx)/cell_length)+1 numycells = int((maxy-miny)/cell_length)+1 numzcells = int((maxz-minz)/cell_length)+1 xrange = [minx*h/a, (minx*h/a + float(numxcells)*cell_length*h/a)] yrange = [miny*h/a, (miny*h/a + float(numycells)*cell_length*h/a)] zrange = [minz*h/a, (minz*h/a + float(numzcells)*cell_length*h/a)] # print 'diagnostic x',(xrange[1] - xrange[0])/(cell_length*h/a), numxcells # print 'diagnostic y',(yrange[1] - yrange[0])/(cell_length*h/a), numycells # print 'diagnostic x',(zrange[1] - zrange[0])/(cell_length*h/a), numzcells #rad should be in physical kpc #num_cells=int(rad*2.0/cell_length) H, edges = np.histogramdd(r, bins = (numxcells, numycells, numzcells), range=(xrange,yrange,zrange), weights=S['m'][agecut][poscut]) cut = H>0 ix, iy, iz = np.where(cut) count = 0 Nums = [] cenx = [] ceny = [] cenz = [] #print 'iy ', iy #print 'len iy ',len(iy) #print 'edges ',edges #print 'len edges ',len(edges) while (count < len(ix)): theNum = H[ix[count],iy[count],iz[count]] thecenx = (edges[0][ix[count]] + edges[0][ix[count]+1])/2.0 theceny = (edges[1][iy[count]] + edges[1][iy[count]+1])/2.0 thecenz = (edges[2][iz[count]] + edges[2][iz[count]+1])/2.0 if (density_max_cell_center): cellxmin_cut = S['p'][:,0][agecut][poscut] > edges[0][ix[count]] cellxmax_cut = S['p'][:,0][agecut][poscut] < edges[0][ix[count]+1] cellymin_cut = S['p'][:,1][agecut][poscut] > edges[1][iy[count]] cellymax_cut = S['p'][:,1][agecut][poscut] < edges[1][iy[count]+1] cellzmin_cut = S['p'][:,2][agecut][poscut] > edges[2][iz[count]] cellzmax_cut = S['p'][:,2][agecut][poscut] < edges[2][iz[count]+1] allcut = cellxmin_cut* cellxmax_cut* cellymin_cut* cellymax_cut * cellzmin_cut * cellzmax_cut #print 'test test ',len(S['p'][:,0][agecut][poscut][allcut]), S['p'][:,0][agecut][poscut][allcut] if (len(S['p'][:,0][agecut][poscut][allcut]) > 5): #print 'first center ',[thecenx, theceny, thecenz], theNum S_COM = SF.gaussian_KDE_center(S['p'][:,0][agecut][poscut][allcut], S['p'][:,1][agecut][poscut][allcut] , S['p'][:,2][agecut][poscut][allcut] , downsample=False) [thecenx, theceny, thecenz] = S_COM #print 'adjusting center ',[thecenx, theceny, thecenz] Nums.append(theNum) cenx.append(thecenx) ceny.append(theceny) cenz.append(thecenz) count+=1 cenx = np.array(cenx) ceny = np.array(ceny) cenz = np.array(cenz) Nums = np.array(Nums) order_of_densities = np.argsort(Nums)[::-1] #cumsumdens = np.cumsum(Nums[order_of_densities]) sum_of_densities = np.sum(Nums) current_sum = 0 count = 0 final_x = [] final_y = [] final_z = [] final_Nums = [] #print 'densities in order ',Nums[order_of_densities] while (current_sum <= surface_frac * sum_of_densities): current_sum += Nums[order_of_densities][count] final_x.append(cenx[order_of_densities][count]) final_y.append(ceny[order_of_densities][count]) final_z.append(cenz[order_of_densities][count]) final_Nums.append( Nums[order_of_densities][count]) count+=1 return [final_x, final_y, final_z, final_Nums]
omega_L = S['header'][11] h = S['header'][12] AHF_haloX = x[count] AHF_haloY = y[count] AHF_haloZ = z[count] AHF_Rvir = Rvir[count] AHF_a = 1.0 / (1.0 + Zs[count]) if (adaptive_Rstar): Rstars = Inner_SF_thresh * Rvir[count] RstarsPhys = Rstars * AHF_a / h Sdists = SF.calcdist2(AHF_haloX, AHF_haloY, AHF_haloZ, S['p'][:, 0], S['p'][:, 1], S['p'][:, 2], boxsize) Sinner = Sdists < Rstars S_COM = SF.gaussian_KDE_center(S['p'][:, 0][Sinner], S['p'][:, 1][Sinner], S['p'][:, 2][Sinner], downsample=True) Shalo = Sdists < AHF_Rvir S_COM_grand = SF.gaussian_KDE_center(S['p'][:, 0][Shalo], S['p'][:, 1][Shalo], S['p'][:, 2][Shalo], downsample=True) iSinX.append(S_COM[0]) iSinY.append(S_COM[1]) iSinZ.append(S_COM[2]) ShalX.append(S_COM_grand[0]) ShalY.append(S_COM_grand[1]) ShalZ.append(S_COM_grand[2]) D = readsnap(the_snapdir,
def surface_density_bycells(S, agecut, poscut, a, h, cell_length=0.3, surface_frac=0.8, density_max_cell_center=True): #r = np.random.randn(100,3) r = [ S['p'][:, 0][agecut][poscut], S['p'][:, 1][agecut][poscut], S['p'][:, 2][agecut][poscut] ] minx = np.min(S['p'][:, 0][agecut][poscut]) * a / h maxx = np.max(S['p'][:, 0][agecut][poscut]) * a / h miny = np.min(S['p'][:, 1][agecut][poscut]) * a / h maxy = np.max(S['p'][:, 1][agecut][poscut]) * a / h minz = np.min(S['p'][:, 2][agecut][poscut]) * a / h maxz = np.max(S['p'][:, 2][agecut][poscut]) * a / h numxcells = int((maxx - minx) / cell_length) + 1 numycells = int((maxy - miny) / cell_length) + 1 numzcells = int((maxz - minz) / cell_length) + 1 xrange = [ minx * h / a, (minx * h / a + float(numxcells) * cell_length * h / a) ] yrange = [ miny * h / a, (miny * h / a + float(numycells) * cell_length * h / a) ] zrange = [ minz * h / a, (minz * h / a + float(numzcells) * cell_length * h / a) ] # print 'diagnostic x',(xrange[1] - xrange[0])/(cell_length*h/a), numxcells # print 'diagnostic y',(yrange[1] - yrange[0])/(cell_length*h/a), numycells # print 'diagnostic x',(zrange[1] - zrange[0])/(cell_length*h/a), numzcells #rad should be in physical kpc #num_cells=int(rad*2.0/cell_length) H, edges = np.histogramdd(r, bins=(numxcells, numycells, numzcells), range=(xrange, yrange, zrange), weights=S['m'][agecut][poscut]) cut = H > 0 ix, iy, iz = np.where(cut) count = 0 Nums = [] cenx = [] ceny = [] cenz = [] #print 'iy ', iy #print 'len iy ',len(iy) #print 'edges ',edges #print 'len edges ',len(edges) while (count < len(ix)): theNum = H[ix[count], iy[count], iz[count]] thecenx = (edges[0][ix[count]] + edges[0][ix[count] + 1]) / 2.0 theceny = (edges[1][iy[count]] + edges[1][iy[count] + 1]) / 2.0 thecenz = (edges[2][iz[count]] + edges[2][iz[count] + 1]) / 2.0 if (density_max_cell_center): cellxmin_cut = S['p'][:, 0][agecut][poscut] > edges[0][ix[count]] cellxmax_cut = S['p'][:, 0][agecut][poscut] < edges[0][ix[count] + 1] cellymin_cut = S['p'][:, 1][agecut][poscut] > edges[1][iy[count]] cellymax_cut = S['p'][:, 1][agecut][poscut] < edges[1][iy[count] + 1] cellzmin_cut = S['p'][:, 2][agecut][poscut] > edges[2][iz[count]] cellzmax_cut = S['p'][:, 2][agecut][poscut] < edges[2][iz[count] + 1] allcut = cellxmin_cut * cellxmax_cut * cellymin_cut * cellymax_cut * cellzmin_cut * cellzmax_cut #print 'test test ',len(S['p'][:,0][agecut][poscut][allcut]), S['p'][:,0][agecut][poscut][allcut] if (len(S['p'][:, 0][agecut][poscut][allcut]) > 5): #print 'first center ',[thecenx, theceny, thecenz], theNum S_COM = SF.gaussian_KDE_center( S['p'][:, 0][agecut][poscut][allcut], S['p'][:, 1][agecut][poscut][allcut], S['p'][:, 2][agecut][poscut][allcut], downsample=False) [thecenx, theceny, thecenz] = S_COM #print 'adjusting center ',[thecenx, theceny, thecenz] Nums.append(theNum) cenx.append(thecenx) ceny.append(theceny) cenz.append(thecenz) count += 1 cenx = np.array(cenx) ceny = np.array(ceny) cenz = np.array(cenz) Nums = np.array(Nums) order_of_densities = np.argsort(Nums)[::-1] #cumsumdens = np.cumsum(Nums[order_of_densities]) sum_of_densities = np.sum(Nums) current_sum = 0 count = 0 final_x = [] final_y = [] final_z = [] final_Nums = [] #print 'densities in order ',Nums[order_of_densities] while (current_sum <= surface_frac * sum_of_densities): current_sum += Nums[order_of_densities][count] final_x.append(cenx[order_of_densities][count]) final_y.append(ceny[order_of_densities][count]) final_z.append(cenz[order_of_densities][count]) final_Nums.append(Nums[order_of_densities][count]) count += 1 return [final_x, final_y, final_z, final_Nums]
omega_matter = S['header'][10] omega_L = S['header'][11] h = S['header'][12] AHF_haloX = x[count] AHF_haloY = y[count] AHF_haloZ = z[count] AHF_Rvir = Rvir[count] AHF_a = 1.0 / (1.0 + Zs[count]) if (adaptive_Rstar): Rstars = Inner_SF_thresh*Rvir[count] RstarsPhys = Rstars * AHF_a / h Sdists = SF.calcdist2(AHF_haloX, AHF_haloY, AHF_haloZ, S['p'][:,0], S['p'][:,1], S['p'][:,2], boxsize) Sinner = Sdists<Rstars S_COM = SF.gaussian_KDE_center(S['p'][:,0][Sinner], S['p'][:,1][Sinner], S['p'][:,2][Sinner], downsample=True) Shalo = Sdists < AHF_Rvir S_COM_grand = SF.gaussian_KDE_center(S['p'][:,0][Shalo], S['p'][:,1][Shalo], S['p'][:,2][Shalo], downsample=True) iSinX.append(S_COM[0]) iSinY.append(S_COM[1]) iSinZ.append(S_COM[2]) ShalX.append(S_COM_grand[0]) ShalY.append(S_COM_grand[1]) ShalZ.append(S_COM_grand[2]) D = readsnap(the_snapdir, Nsnapstring, 1, snapshot_name=the_prefix, extension=the_suffix) Ddists = SF.calcdist2(AHF_haloX, AHF_haloY, AHF_haloZ, D['p'][:,0], D['p'][:,1], D['p'][:,2], boxsize) Dinner = Ddists < Rstars Dhalo = Ddists < AHF_Rvir D_COM_inner = SF.gaussian_KDE_center(D['p'][:,0][Dinner], D['p'][:,1][Dinner], D['p'][:,2][Dinner], downsample=True)
Rvir, Vsig, M = SF.check_Rvir_growth(halo_to_do[workcount], a, Rvir, Vsig, M, therod=use_fixed_halos) if (adaptive_Rstar): Rstars = Inner_SF_thresh*Rvir RstarsPhys = Rstars * a / h Sdists = SF.calcdist2(haloX, haloY, haloZ, S['p'][:,0], S['p'][:,1], S['p'][:,2], boxsize) Sinner_young = Sdists[Syoung] < Rstars if (use_KDE_cent): Sinner = Sdists<Rstars S_COM = SF.gaussian_KDE_center(S['p'][:,0][Sinner], S['p'][:,1][Sinner], S['p'][:,2][Sinner], downsample=True) [haloX, haloY, haloZ] = [S_COM[0], S_COM[1], S_COM[2]] Sdists = SF.calcdist2(haloX, haloY, haloZ, S['p'][:,0], S['p'][:,1], S['p'][:,2], boxsize) Sinner_young = Sdists[Syoung] < Rstars elif (use_darkKDE_cent): #dark matter fun Ddists = SF.calcdist2(haloX, haloY, haloZ, D['p'][:,0], D['p'][:,1], D['p'][:,2], boxsize) Dinner = Ddists<Rstars D_COM = SF.gaussian_KDE_center(D['p'][:,0][Dinner], D['p'][:,1][Dinner], D['p'][:,2][Dinner], downsample=True) [haloX, haloY, haloZ] = [D_COM[0], D_COM[1], D_COM[2]] Sdists = SF.calcdist2(haloX, haloY, haloZ, S['p'][:,0], S['p'][:,1], S['p'][:,2], boxsize) Sinner_young = Sdists[Syoung] < Rstars [x_cells, y_cells, z_cells, cellmass ]= potter.surface_density_bycells(S, Syoung, Sinner_young, a, h, surface_frac=the_surface_frac) Nnewstars = len(Sdists[Syoung][Sinner_young])