# read snapshot and find number of subfiles snapshot_root = '%s/output/'%run snapshot = snapshot_root + 'snapdir_%03d/snap_%03d'%(snapnum,snapnum) header = rs.snapshot_header(snapshot) redshift = header.redshift h = header.hubble if halo_num is not(None): xmin = x_max = y_min = y_max = z_min = z_max = None # only master reads halo catalogue if myrank==0: # read number of particles in halos and subhalos and number of subhalos print '\nReading halo catalogue...' halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupPos','GroupMass','Group_R_TopHat200']) halo_pos = halos['GroupPos']/1e3 #Mpc/h halo_R = halos['Group_R_TopHat200']/1e3 #Mpc/h halo_mass = halos['GroupMass']*1e10 #Msun/h del halos # find the coordinates of the region if halo_num is not (None): x_max, y_max, z_max = halo_pos[halo_num] + BoxSize_image/2.0 x_min, y_min, z_min = halo_pos[halo_num] - BoxSize_image/2.0 print 'M_halo = %.3e Msun/h'%(halo_mass[halo_num]) print 'R_halo = %.3e Mpc/h\n'%(halo_R[halo_num]) print '%6.3f < X < %6.3f'%(x_min, x_max) print '%6.3f < Y < %6.3f'%(y_min, y_max) print '%6.3f < Z < %6.3f'%(z_min, z_max)
filenum = header.filenum Omega_m = header.omega0 Omega_L = header.omegaL h = header.hubble print '\n' print 'BoxSize = %.3f Mpc/h'%BoxSize print 'Number of files = %d'%filenum print 'Omega_m = %.3f'%Omega_m print 'Omega_l = %.3f'%Omega_L print 'redshift = %.3f'%redshift if os.path.exists(fout[:-4]+'_z=%.3f.hdf5'%redshift): continue # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, num, fields=['GroupLenType','GroupVel']) halo_len = halos['GroupLenType'][:,0] halo_vel = halos['GroupVel']*(1.0+redshift) #km/s del halos # read HI bulk velocity of halos f = h5py.File('../HI_bulk_velocity/V_HI_75_1820_z=%d.hdf5'%round(redshift), 'r') V_HI = f['V_HI'][:] #km/s f.close() V_HI = V_HI.astype(np.float32) # define the M_HI and sigma_HI array M_HI = np.zeros(len(halo_len), dtype=np.float64) sigma2_HI = np.zeros(len(halo_len), dtype=np.float64)
Omega_m = 0.3089 Omega_l = 0.6911 h = 0.6774 z = 1.0 ####################################################################### H0 = 100.0 #km/s/(Mpc/h) Hz = 100.0*np.sqrt(Omega_m*(1.0+z)**3 + Omega_l) V_cell = BoxSize**3*(5.0/BoxSize)/dims**2 #(Mpc/h)^3 print '\nReading halo catalogue...' snapshot_root = '%s/output/'%run halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupPos','GroupMass','GroupVel']) halo_pos = halos['GroupPos']/1e3 #Mpc/h halo_vel = halos['GroupVel']*(1.0+z) #km/s halo_mass = halos['GroupMass']*1e10 #Msun/h del halos # move halo positions to redshift-space RSL.pos_redshift_space(halo_pos, halo_vel, BoxSize, Hz, z, axis) print np.min(halo_pos[:,0]),np.max(halo_pos[:,0]) print np.min(halo_pos[:,1]),np.max(halo_pos[:,1]) print np.min(halo_pos[:,2]),np.max(halo_pos[:,2]) indexes = np.where((halo_pos[:,2]>=0.0) & (halo_pos[:,2]<5.0))[0]
ratio = np.sqrt(np.dot(V1,V1)/np.dot(V2,V2)) return cos_a,ratio ############################## INPUT ######################################## run = '/n/hernquistfs3/IllustrisTNG/Runs/L75n1820TNG' snapshot_root = '%s/output/'%run snapnum = 17 ############################################################################# z_dict = {99:0, 17:5} FoF = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupLenType','GroupNsubs','GroupVel', 'GroupFirstSub','GroupMass']) halo_len = FoF['GroupLenType'][:,0] Nsubs = FoF['GroupNsubs'] halo_vel = FoF['GroupVel']*(1.0+z_dict[snapnum]) first_sub = FoF['GroupFirstSub'] halo_mass = FoF['GroupMass']*1e10 #Msun/h subhalos = groupcat.loadSubhalos(snapshot_root, snapnum, fields=['SubhaloGrNr','SubhaloMass', 'SubhaloVel']) sub_FoF = subhalos['SubhaloGrNr'][:] sub_mass = subhalos['SubhaloMass'][:]*1e10 #Msun/h sub_vel = subhalos['SubhaloVel'][:] del subhalos
if myrank==0: print '\nBoxSize = %.1f Mpc/h'%BoxSize print 'Number of files = %d'%filenum print 'Omega_m = %.3f'%Omega_m print 'Omega_l = %.3f'%Omega_L print 'redshift = %.3f'%redshift # find the output name fout1 = 'HI_profiles_%.1e-%.1e_%d_z=%.2f.hdf5'%(Mmin,Mmax,bins,redshift) fout2 = 'HI_profile_new_%.1e-%.1e-z=%.1f.txt'%(Mmin,Mmax,redshift) # read number of particles in halos and subhalos and number of subhalos if myrank==0: print '\nReading halo catalogue...' halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupPos','GroupMass', 'Group_R_TopHat200','Group_M_TopHat200']) halo_pos = halos['GroupPos']/1e3 #Mpc/h halo_R = halos['Group_R_TopHat200']/1e3 #Mpc/h halo_mass = halos['Group_M_TopHat200']*1e10 #Msun/h #halo_mass = halos['GroupMass']*1e10 #Msun/h del halos # consider only halos in the mass range and with R>0 indexes = np.where((halo_mass>Mmin) & (halo_mass<Mmax) & (halo_R>0.0))[0] halo_pos = halo_pos[indexes] halo_R = halo_R[indexes] halo_mass = halo_mass[indexes] if myrank==0: print 'Found %d halos with masses %.2e < M < %.2e'\
def __init__(self, snapshot_root, snapnum, halo_number, TREECOOL_file, ptype): # read header snapshot = snapshot_root + 'snapdir_%03d/snap_%03d'%(snapnum, snapnum) f = h5py.File(snapshot+'.0.hdf5', 'r') scale_factor = f['Header'].attrs[u'Time'] redshift = f['Header'].attrs[u'Redshift'] BoxSize = f['Header'].attrs[u'BoxSize']/1e3 #Mpc/h filenum = f['Header'].attrs[u'NumFilesPerSnapshot'] Omega_m = f['Header'].attrs[u'Omega0'] Omega_L = f['Header'].attrs[u'OmegaLambda'] h = f['Header'].attrs[u'HubbleParam'] Masses = f['Header'].attrs[u'MassTable']*1e10 #Msun/h f.close() print '\nBoxSize = %.1f Mpc/h'%BoxSize print 'Number of files = %d'%filenum print 'Omega_m = %.3f'%Omega_m print 'Omega_l = %.3f'%Omega_L print 'redshift = %.3f'%redshift # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupLenType','GroupPos', 'GroupMass']) halo_len = halos['GroupLenType'][:,ptype] halo_pos = halos['GroupPos']/1e3 halo_mass = halos['GroupMass']*1e10 del halos # find where the halo starts and ends in the file begin = np.sum(halo_len[:halo_number], dtype=np.int64) end = begin + halo_len[halo_number] length = end-begin print begin,end self.pos = np.zeros((length,3), dtype=np.float32) self.vel = np.zeros((length,3), dtype=np.float32) if ptype==0: self.M_HI = np.zeros(length, dtype=np.float32) begin_subfile, particles = 0, 0 for i in xrange(filenum): # find subfile name and read the number of particles in it snapshot = snapshot_root + 'snapdir_%03d/snap_%03d.%d.hdf5'%(snapnum, snapnum, i) f = h5py.File(snapshot, 'r') npart = f['Header'].attrs[u'NumPart_ThisFile'] f.close() end_subfile = begin_subfile + npart[ptype] # if all particles in the halo has been read exit loop if end<begin_subfile: break # if the subfile does not contain any particle move to next subfile if begin>end_subfile: begin_subfile = end_subfile; continue print i f = h5py.File(snapshot, 'r') print 'Working with subfile %03d'%i pos_i = (f['PartType%d/Coordinates'%ptype][:]/1e3).astype(np.float32) vel_i = f['PartType%d/Velocities'%ptype][:]*np.sqrt(scale_factor) if ptype==0: pos_i, M_HI_i = HIL.HI_mass_from_Illustris_snap(snapshot, TREECOOL_file) # find the indexes of current subfile that contribute to halo begin_array = begin - begin_subfile end_array = begin_array + length if end>end_subfile: end_array = end_subfile - begin_subfile begin = end_subfile length_array = end_array-begin_array self.pos[particles:particles+length_array] = pos_i[begin_array:end_array] self.vel[particles:particles+length_array] = vel_i[begin_array:end_array] if ptype==0: self.M_HI[particles:particles+length_array] = M_HI_i[begin_array:end_array] particles = particles+length_array begin_subfile = end_subfile f.close() print 'Halo mass = %.3e'%halo_mass[halo_number] print 'Halo pos =',halo_pos[halo_number] print 'Number of particles in the halo = %ld'%particles
import groupcat as GC import sublink as SL import numpy as np import lhalotree as LH import h5py import matplotlib.pyplot as plt basePath = '/home/illustris/Desktop/trabajo/Illustris-3lht' GroupFirstSub = GC.loadHalos(basePath, 135, fields=['GroupFirstSub']) Halos = GC.loadHalos(basePath, 135, fields=['Group_M_Crit200', 'GroupPos', 'GroupNsubs']) GroupNsubs = GC.loadHalos(basePath, 135, fields=['GroupNsubs']) Halo_mass = Halos['Group_M_Crit200'] Halo_Pos = Halos['GroupPos'] print(len(GroupNsubs)) fields = ['SnapNum', 'Group_M_Crit200'] start = 0 for i in range(start, start + 5): #if GroupNsubs[i] == 0: # continue tree = LH.loadTree(basePath, 135, GroupFirstSub[i], fields=fields, onlyMPB=True)
import numpy as np import sys, os, h5py import groupcat import HI_library as HIL ################################ INPUT ###################################### snapshot_root = '/n/hernquistfs3/IllustrisTNG/Runs/L75n1820TNG/output/' TREECOOL_file = '/n/home09/fvillaescusa/Illustris/HI/TREECOOL_fg_dec11' ############################################################################# for snapnum in [99, 50, 33, 25, 21, 17]: # read number of particles in halos and subhalos and number of subhalos print '\nReading halo catalogue...' halos = groupcat.loadHalos( snapshot_root, snapnum, fields=['GroupPos', 'GroupMass', 'GroupLenType']) halo_pos = halos['GroupPos'] / 1e3 #Mpc/h halo_mass = halos['GroupMass'] * 1e10 #Msun/h halo_len = halos['GroupLenType'][:, 0] del halos Nparticles = np.sum(halo_len) print 'Number of halos = %d' % len(halo_pos) print 'Number of particles in all halos = %d' % Nparticles # read snapshot header snapshot = '%s/snapdir_%03d/snap_%03d.0.hdf5'\ %(snapshot_root, snapnum, snapnum) f = h5py.File(snapshot, 'r') redshift = f['Header'].attrs[u'Redshift']
z_dict = {99: 0.000, 50: 0.997, 33: 2.002, 25: 3.008, 21: 4.008, 17: 4.996} for snapnum in [17, 21, 25, 33, 50, 99]: # read header snapshot = root + 'snapdir_%03d/snap_%03d' % (snapnum, snapnum) header = readgadget.header(snapshot) BoxSize = header.boxsize / 1e3 #Mpc/h redshift = header.redshift print 'L = %.1f Mpc/h' % BoxSize print 'z = %.1f' % redshift halos = groupcat.loadHalos( root, snapnum, fields=['GroupMassType', 'GroupMass', 'Group_R_TopHat200']) halo_mass = halos['GroupMassType'][:] * 1e10 #Msun/h Mass = halos['GroupMass'][:] * 1e10 #Msun/h R = halos['Group_R_TopHat200'][:] / 1e3 #Mpc/h f = h5py.File( '../HI_mass/HI_FoF_galaxies/M_HI_new_75_1820_z=%.3f.hdf5' % z_dict[snapnum], 'r') M_HI = f['M_HI'][:] f.close() indexes = np.where((R > 0.0) & (halo_mass[:, 0] > 0.0))[0] halo_mass = halo_mass[indexes] Mass = Mass[indexes] M_HI = M_HI[indexes]
import groupcat as GC import sublink as SL import numpy as np import matplotlib.pyplot as plt ###2### basePath = '/media/ycamargo/ILLUSTRIS-3/Illustris-2' GroupFirstSub = GC.loadHalos(basePath, 135, fields=['GroupFirstSub']) ###3### fields = ['SubhaloMass', 'SubfindID', 'SnapNum'] start = 0 for i in range(start, start + 5): tree = SL.loadTree(basePath, 135, GroupFirstSub[i], fields=fields, onlyMPB=True) ###4### def find_formation_time(sublinktree): final_mass = sublinktree['SubhaloMass'][0] initial_mass = sublinktree['SubhaloMass'][-1] n_points = len(sublinktree['SubhaloMass']) formation_snap = -1 for i in range(n_points - 1, -1, -1): if sublinktree['SubhaloMass'][i] > final_mass / 2.0: formation_snap = sublinktree['SnapNum'][i]
#fout = 'ratio_g_75Mpc_1820_UVB' ################################################################################ for snapnum in [17,21,25,33,50,99]: # read header snapshot = root + 'snapdir_%03d/snap_%03d'%(snapnum,snapnum) header = readgadget.header(snapshot) BoxSize = header.boxsize/1e3 #Mpc/h redshift = header.redshift print 'L = %.1f Mpc/h'%BoxSize print 'z = %.1f'%redshift halos = groupcat.loadHalos(root, snapnum, fields=['GroupMassType','GroupMass', 'Group_R_TopHat200']) halo_mass = halos['GroupMassType'][:]*1e10 #Msun/h Mass = halos['GroupMass'][:]*1e10 #Msun/h R = halos['Group_R_TopHat200'][:]/1e3 #Mpc/h indexes = np.where(R>0.0)[0] halo_mass = halo_mass[indexes] Mass = Mass[indexes] #ratio = (halo_mass[:,0]+halo_mass[:,4]+halo_mass[:,5])/Mass ratio = (halo_mass[:,0])/Mass M_bins = np.logspace(np.log10(Mmin), np.log10(Mmax), bins+1) M_mean = 10**(0.5*(np.log10(M_bins[1:]) + np.log10(M_bins[:-1])))
h = header.hubble if myrank == 0: print '\n' print 'BoxSize = %.3f Mpc/h' % BoxSize print 'Number of files = %d' % filenum print 'Omega_m = %.3f' % Omega_m print 'Omega_l = %.3f' % Omega_L print 'redshift = %.3f' % redshift if os.path.exists(fout[:-4] + '_z=%.3f.hdf5' % redshift): continue # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, num, fields=['GroupLenType', 'GroupNsubs']) halo_len = halos['GroupLenType'][:, 0] gal_in_halo = halos['GroupNsubs'] subhalos = groupcat.loadSubhalos( snapshot_root, num, fields=['SubhaloLenType', 'SubhaloMassType']) gal_len = subhalos['SubhaloLenType'][:, 0] mass_ratio = subhalos['SubhaloMassType'][:] * 1e10 #Msun/h total_mass = np.sum(mass_ratio, axis=1) #M_matter mass_ratio = mass_ratio[:, 1] / total_mass #M_CDM / M_matter del halos, subhalos # define the M_HI array M_HI = np.zeros(len(halo_len), dtype=np.float64) M_HI_gal = np.zeros(len(halo_len), dtype=np.float64) M_HI_cen = np.zeros(len(halo_len), dtype=np.float64)
dims = 64 ############################################################################# num = {0:99, 1:50, 2:33, 3:25, 4:21, 5:17} snapshot_root = '%s/output/'%run bins_histo = np.logspace(-3,3,100) dbins = bins_histo[1:] - bins_histo[:-1] bins_mean = 10**(0.5*(np.log10(bins_histo[1:]) + np.log10(bins_histo[:-1]))) for z in [0,1,2,3,4,5]: # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, num[z], fields=['GroupMass','GroupPos']) halo_mass = halos['GroupMass'][:]*1e10 #Msun/h halo_pos = halos['GroupPos'][:]/1e3 #Mpc/h del halos if z==0: alpha, M0, Mmin = 0.24, 4.3e10, 2e12 elif z==1: alpha, M0, Mmin = 0.53, 1.5e10, 6e11 elif z==2: alpha, M0, Mmin = 0.60, 1.3e10, 3.6e11 elif z==3: alpha, M0, Mmin = 0.76, 2.9e9, 6.7e10 elif z==4: alpha, M0, Mmin = 0.79, 1.4e9, 2.1e10 elif z==5:
def Illustris_halo(snapshot_root, snapnum, halo_number, TREECOOL_file, fout, ptype=0): # find snapshot name and read header snapshot = snapshot_root + 'snapdir_%03d/snap_%03d' % (snapnum, snapnum) header = rs.snapshot_header(snapshot) redshift = header.redshift BoxSize = header.boxsize / 1e3 #Mpc/h filenum = header.filenum Omega_m = header.omega0 Omega_L = header.omegaL h = header.hubble massarr = header.massarr * 1e10 #Msun/h print '\nBoxSize = %.1f Mpc/h' % BoxSize print 'Number of files = %d' % filenum print 'Omega_m = %.3f' % Omega_m print 'Omega_l = %.3f' % Omega_L print 'redshift = %.3f' % redshift # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos( snapshot_root, snapnum, fields=['GroupLenType', 'GroupPos', 'GroupMass']) halo_len = halos['GroupLenType'][:, ptype] halo_pos = halos['GroupPos'] / 1e3 halo_mass = halos['GroupMass'] * 1e10 del halos # find where the halo starts and ends in the file begin = np.sum(halo_len[:halo_number], dtype=np.int64) end = begin + halo_len[halo_number] print begin, end # do a loop over all snapshot subfiles f = h5py.File(fout, 'w') pos_f = f.create_dataset('pos', (0, 3), maxshape=(None, 3)) vel_f = f.create_dataset('vel', (0, 3), maxshape=(None, 3)) if ptype == 0: mass_f = f.create_dataset('mass', (0, ), maxshape=(None, )) MHI_f = f.create_dataset('M_HI', (0, ), maxshape=(None, )) radii_f = f.create_dataset('radii', (0, ), maxshape=(None, )) if ptype == 1: radii_f = f.create_dataset('radii', (0, ), maxshape=(None, )) mass_f = f.create_dataset('mass_c', (0, ), maxshape=(None, )) begin_subfile, particles = 0, 0 for i in xrange(filenum): # find subfile name and read the number of particles in it snapshot = snapshot_root + 'snapdir_%03d/snap_%03d.%d' % (snapnum, snapnum, i) header = rs.snapshot_header(snapshot) npart = header.npart end_subfile = begin_subfile + npart[ptype] # if all particles in the halo has been read exit loop if end < begin_subfile: break # if the subfile does not contain any particle move to next subfile if begin > end_subfile: begin_subfile = end_subfile continue print 'Working with subfile %03d' % i pos = rs.read_block(snapshot, 'POS ', parttype=ptype, verbose=False) / 1e3 pos = pos.astype(np.float32) vel = rs.read_block(snapshot, 'VEL ', parttype=ptype, verbose=False) / np.sqrt(1.0 + redshift) #km/s if ptype == 0: MHI = rs.read_block(snapshot, 'NH ', parttype=0, verbose=False) #HI/H mass = rs.read_block(snapshot, 'MASS', parttype=0, verbose=False) * 1e10 SFR = rs.read_block(snapshot, 'SFR ', parttype=0, verbose=False) indexes = np.where(SFR > 0.0)[0] del SFR # find the metallicity of star-forming particles metals = rs.read_block(snapshot, 'GZ ', parttype=0, verbose=False) metals = metals[indexes] / 0.0127 # find densities of star-forming particles: units of h^2 Msun/Mpc^3 rho = rs.read_block(snapshot, 'RHO ', parttype=0, verbose=False) * 1e19 Volume = mass / rho #(Mpc/h)^3 radii = (Volume / (4.0 * np.pi / 3.0))**(1.0 / 3.0) #Mpc/h # find density and radius of star-forming particles radii_SFR = radii[indexes] rho = rho[indexes] # find HI/H fraction for star-forming particles MHI[indexes] = HIL.Rahmati_HI_Illustris(rho, radii_SFR, metals, redshift, h, TREECOOL_file, Gamma=None, fac=1, correct_H2=True) #HI/H MHI *= (0.76 * mass) if ptype == 1: radii = rs.read_block(snapshot, 'SFHS', parttype=1, verbose=False) / 1e3 #Mpc/h mass = np.ones(len(radii)) * massarr[1] # find the indexes of current subfile that contribute to halo begin_array = begin - begin_subfile end_array = begin_array + (end - begin) if end > end_subfile: end_array = end_subfile - begin_subfile begin = end_subfile new_size = particles + (end_array - begin_array) pos_f.resize((new_size, 3)) pos_f[particles:] = pos[begin_array:end_array] vel_f.resize((new_size, 3)) vel_f[particles:] = vel[begin_array:end_array] if ptype == 0: mass_f.resize((new_size, )) mass_f[particles:] = mass[begin_array:end_array] MHI_f.resize((new_size, )) MHI_f[particles:] = MHI[begin_array:end_array] radii_f.resize((new_size, )) radii_f[particles:] = radii[begin_array:end_array] if ptype == 1: radii_f.resize((new_size, )) radii_f[particles:] = radii[begin_array:end_array] mass_f.resize((new_size, )) mass_f[particles:] = mass[begin_array:end_array] particles = new_size begin_subfile = end_subfile f.close() print 'Halo mass = %.3e' % halo_mass[halo_number] print 'Halo pos =', halo_pos[halo_number] print 'Number of particles in the halo = %ld' % particles
dims = 64 ############################################################################# num = {0: 99, 1: 50, 2: 33, 3: 25, 4: 21, 5: 17} snapshot_root = '%s/output/' % run bins_histo = np.logspace(-3, 3, 100) dbins = bins_histo[1:] - bins_histo[:-1] bins_mean = 10**(0.5 * (np.log10(bins_histo[1:]) + np.log10(bins_histo[:-1]))) for z in [0, 1, 2, 3, 4, 5]: # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, num[z], fields=['GroupMass', 'GroupPos']) halo_mass = halos['GroupMass'][:] * 1e10 #Msun/h halo_pos = halos['GroupPos'][:] / 1e3 #Mpc/h del halos if z == 0: alpha, M0, Mmin = 0.24, 4.3e10, 2e12 elif z == 1: alpha, M0, Mmin = 0.53, 1.5e10, 6e11 elif z == 2: alpha, M0, Mmin = 0.60, 1.3e10, 3.6e11 elif z == 3: alpha, M0, Mmin = 0.76, 2.9e9, 6.7e10 elif z == 4: alpha, M0, Mmin = 0.79, 1.4e9, 2.1e10
def __init__(self, snapshot_root, snapnum, halo_number, TREECOOL_file, ptype): # read header snapshot = snapshot_root + 'snapdir_%03d/snap_%03d' % (snapnum, snapnum) f = h5py.File(snapshot + '.0.hdf5', 'r') scale_factor = f['Header'].attrs[u'Time'] redshift = f['Header'].attrs[u'Redshift'] BoxSize = f['Header'].attrs[u'BoxSize'] / 1e3 #Mpc/h filenum = f['Header'].attrs[u'NumFilesPerSnapshot'] Omega_m = f['Header'].attrs[u'Omega0'] Omega_L = f['Header'].attrs[u'OmegaLambda'] h = f['Header'].attrs[u'HubbleParam'] Masses = f['Header'].attrs[u'MassTable'] * 1e10 #Msun/h f.close() print '\nBoxSize = %.1f Mpc/h' % BoxSize print 'Number of files = %d' % filenum print 'Omega_m = %.3f' % Omega_m print 'Omega_l = %.3f' % Omega_L print 'redshift = %.3f' % redshift # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos( snapshot_root, snapnum, fields=['GroupLenType', 'GroupPos', 'GroupMass']) halo_len = halos['GroupLenType'][:, ptype] halo_pos = halos['GroupPos'] / 1e3 halo_mass = halos['GroupMass'] * 1e10 del halos # find where the halo starts and ends in the file begin = np.sum(halo_len[:halo_number], dtype=np.int64) end = begin + halo_len[halo_number] length = end - begin print begin, end self.pos = np.zeros((length, 3), dtype=np.float32) self.vel = np.zeros((length, 3), dtype=np.float32) if ptype == 0: self.M_HI = np.zeros(length, dtype=np.float32) begin_subfile, particles = 0, 0 for i in xrange(filenum): # find subfile name and read the number of particles in it snapshot = snapshot_root + 'snapdir_%03d/snap_%03d.%d.hdf5' % ( snapnum, snapnum, i) f = h5py.File(snapshot, 'r') npart = f['Header'].attrs[u'NumPart_ThisFile'] f.close() end_subfile = begin_subfile + npart[ptype] # if all particles in the halo has been read exit loop if end < begin_subfile: break # if the subfile does not contain any particle move to next subfile if begin > end_subfile: begin_subfile = end_subfile continue print i f = h5py.File(snapshot, 'r') print 'Working with subfile %03d' % i pos_i = (f['PartType%d/Coordinates' % ptype][:] / 1e3).astype( np.float32) vel_i = f['PartType%d/Velocities' % ptype][:] * np.sqrt(scale_factor) if ptype == 0: pos_i, M_HI_i = HIL.HI_mass_from_Illustris_snap( snapshot, TREECOOL_file) # find the indexes of current subfile that contribute to halo begin_array = begin - begin_subfile end_array = begin_array + length if end > end_subfile: end_array = end_subfile - begin_subfile begin = end_subfile length_array = end_array - begin_array self.pos[particles:particles + length_array] = pos_i[begin_array:end_array] self.vel[particles:particles + length_array] = vel_i[begin_array:end_array] if ptype == 0: self.M_HI[particles:particles + length_array] = M_HI_i[begin_array:end_array] particles = particles + length_array begin_subfile = end_subfile f.close() print 'Halo mass = %.3e' % halo_mass[halo_number] print 'Halo pos =', halo_pos[halo_number] print 'Number of particles in the halo = %ld' % particles
# find output name if R_field=='Group_R_Crit200': M_field = 'Group_M_Crit200' fout = 'M_HI_SO_Crit200_z=%.1f.hdf5'%redshift elif R_field=='Group_R_TopHat200': M_field = 'Group_M_TopHat200' fout = 'M_HI_SO_TopHat200_z=%.1f.hdf5'%redshift elif R_field=='Group_R_Mean200': M_field = 'Group_M_Mean200' fout = 'M_HI_SO_Mean200_z=%.1f.hdf5'%redshift else: raise Exception("%s not allowed!"%R_field) # read number of particles in halos and subhalos and number of subhalos if myrank==0: print '\nReading halo catalogue...' halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupPos','GroupMass', R_field, M_field]) halo_pos = halos['GroupPos']/1e3 #Mpc/h halo_R = halos[R_field]/1e3 #Mpc/h halo_mass = halos['GroupMass']*1e10 #Msun/h halo_mass2 = halos[M_field]*1e10 #Msun/h del halos ####### CAUTION ###### #halo_R = halo_R*1.7 ###################### # consider only halos with radii larger than 0 indexes = np.where(halo_R>0.0)[0] halo_pos = halo_pos[indexes] halo_R = halo_R[indexes]
Omega_L = header.omegaL h = header.hubble if myrank==0: print '\n' print 'BoxSize = %.3f Mpc/h'%BoxSize print 'Number of files = %d'%filenum print 'Omega_m = %.3f'%Omega_m print 'Omega_l = %.3f'%Omega_L print 'redshift = %.3f'%redshift if os.path.exists(fout[:-4]+'_z=%.3f.hdf5'%redshift): continue # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, num, fields=['GroupLenType','GroupNsubs']) halo_len = halos['GroupLenType'][:,0] gal_in_halo = halos['GroupNsubs'] subhalos = groupcat.loadSubhalos(snapshot_root, num, fields=['SubhaloLenType','SubhaloMassType']) gal_len = subhalos['SubhaloLenType'][:,0] mass_ratio = subhalos['SubhaloMassType'][:]*1e10 #Msun/h total_mass = np.sum(mass_ratio, axis=1) #M_matter mass_ratio = mass_ratio[:,1]/total_mass #M_CDM / M_matter del halos, subhalos # define the M_HI array M_HI = np.zeros(len(halo_len), dtype=np.float64) M_HI_gal = np.zeros(len(halo_len), dtype=np.float64) M_HI_cen = np.zeros(len(halo_len), dtype=np.float64) M_HI_sat = np.zeros(len(halo_len), dtype=np.float64)
basePath = '../../data/illustris_3' ssNumber = 99 # snapshotnumber zl = 0.6 zs = 2.0 ''' cosmopara ''' class cosmopara: h = 0.704 OM = 0.27 mlow = 1e12 mhigh = 1e14 GroupFirstSub = groupcat.loadHalos(basePath,ssNumber,fields = ['GroupFirstSub']) GroupMass = groupcat.loadHalos(basePath,ssNumber,fields = ['GroupMass'])*1e10/cosmopara.h GroupFirstSub = GroupFirstSub[GroupMass>mlow] GroupMass = GroupMass[GroupMass>mlow] GroupFirstSub = GroupFirstSub[GroupMass<mhigh] #SubhaloMass = groupcat.loadSubhalos(basePath,ssNumber,fields = ['SubhaloMass']) SubhaloVelDisp = groupcat.loadSubhalos(basePath,ssNumber,fields = ['SubhaloVelDisp']) #mass_msun = np.zeros(len(GroupFirstSub)) sigma = np.zeros(len(GroupFirstSub)) j = 0 for i in GroupFirstSub:
import sys,os,h5py import groupcat ############################### INPUT ###################################### run = '/n/hernquistfs3/IllustrisTNG/Runs/L75n1820TNG' bins = 35 ############################################################################ snapshot_root = '%s/output/'%run for snapnum,z in zip([17,21,25,33,50,99],[4.996,4.008,3.008,2.002,0.997,0.000]): # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupLenType','GroupMass']) halo_len = halos['GroupLenType'][:,1] halo_mass = halos['GroupMass']*1e10 #Msun/h del halos f_HI = 'sigma_HI_75_1820_z=%.3f.hdf5'%z f_m = 'sigma_matter_75_1820_z=%.3f.hdf5'%z f = h5py.File(f_HI, 'r') M_HI = f['M_HI'][:] sigma2_HI = f['sigma2_HI'][:] f.close() # only consider halos with HI masses above 1e5 indexes = np.where(M_HI>1e5)[0] M_HI = M_HI[indexes]
def Illustris_halo(snapshot_root, snapnum, halo_number, TREECOOL_file, fout, ptype=0): # find snapshot name and read header snapshot = snapshot_root + 'snapdir_%03d/snap_%03d'%(snapnum, snapnum) header = rs.snapshot_header(snapshot) redshift = header.redshift BoxSize = header.boxsize/1e3 #Mpc/h filenum = header.filenum Omega_m = header.omega0 Omega_L = header.omegaL h = header.hubble massarr = header.massarr*1e10 #Msun/h print '\nBoxSize = %.1f Mpc/h'%BoxSize print 'Number of files = %d'%filenum print 'Omega_m = %.3f'%Omega_m print 'Omega_l = %.3f'%Omega_L print 'redshift = %.3f'%redshift # read number of particles in halos and subhalos and number of subhalos halos = groupcat.loadHalos(snapshot_root, snapnum, fields=['GroupLenType','GroupPos','GroupMass']) halo_len = halos['GroupLenType'][:,ptype] halo_pos = halos['GroupPos']/1e3 halo_mass = halos['GroupMass']*1e10 del halos # find where the halo starts and ends in the file begin = np.sum(halo_len[:halo_number], dtype=np.int64) end = begin + halo_len[halo_number] print begin,end # do a loop over all snapshot subfiles f = h5py.File(fout,'w') pos_f = f.create_dataset('pos', (0,3), maxshape=(None,3)) vel_f = f.create_dataset('vel', (0,3), maxshape=(None,3)) if ptype==0: mass_f = f.create_dataset('mass', (0,), maxshape=(None,)) MHI_f = f.create_dataset('M_HI', (0,), maxshape=(None,)) radii_f = f.create_dataset('radii', (0,), maxshape=(None,)) if ptype==1: radii_f = f.create_dataset('radii', (0,), maxshape=(None,)) mass_f = f.create_dataset('mass_c', (0,), maxshape=(None,)) begin_subfile, particles = 0, 0 for i in xrange(filenum): # find subfile name and read the number of particles in it snapshot = snapshot_root + 'snapdir_%03d/snap_%03d.%d'%(snapnum, snapnum, i) header = rs.snapshot_header(snapshot) npart = header.npart end_subfile = begin_subfile + npart[ptype] # if all particles in the halo has been read exit loop if end<begin_subfile: break # if the subfile does not contain any particle move to next subfile if begin>end_subfile: begin_subfile = end_subfile; continue print 'Working with subfile %03d'%i pos = rs.read_block(snapshot, 'POS ', parttype=ptype, verbose=False)/1e3 pos = pos.astype(np.float32) vel = rs.read_block(snapshot, 'VEL ', parttype=ptype, verbose=False)/np.sqrt(1.0+redshift) #km/s if ptype==0: MHI = rs.read_block(snapshot, 'NH ', parttype=0, verbose=False)#HI/H mass = rs.read_block(snapshot, 'MASS', parttype=0, verbose=False)*1e10 SFR = rs.read_block(snapshot, 'SFR ', parttype=0, verbose=False) indexes = np.where(SFR>0.0)[0]; del SFR # find the metallicity of star-forming particles metals = rs.read_block(snapshot, 'GZ ', parttype=0, verbose=False) metals = metals[indexes]/0.0127 # find densities of star-forming particles: units of h^2 Msun/Mpc^3 rho = rs.read_block(snapshot, 'RHO ', parttype=0, verbose=False)*1e19 Volume = mass/rho #(Mpc/h)^3 radii = (Volume/(4.0*np.pi/3.0))**(1.0/3.0) #Mpc/h # find density and radius of star-forming particles radii_SFR = radii[indexes] rho = rho[indexes] # find HI/H fraction for star-forming particles MHI[indexes] = HIL.Rahmati_HI_Illustris(rho, radii_SFR, metals, redshift, h, TREECOOL_file, Gamma=None, fac=1, correct_H2=True) #HI/H MHI *= (0.76*mass) if ptype==1: radii = rs.read_block(snapshot, 'SFHS', parttype=1, verbose=False)/1e3 #Mpc/h mass = np.ones(len(radii))*massarr[1] # find the indexes of current subfile that contribute to halo begin_array = begin - begin_subfile end_array = begin_array + (end-begin) if end>end_subfile: end_array = end_subfile - begin_subfile begin = end_subfile new_size = particles + (end_array - begin_array) pos_f.resize((new_size,3)) pos_f[particles:] = pos[begin_array:end_array] vel_f.resize((new_size,3)) vel_f[particles:] = vel[begin_array:end_array] if ptype==0: mass_f.resize((new_size,)) mass_f[particles:] = mass[begin_array:end_array] MHI_f.resize((new_size,)) MHI_f[particles:] = MHI[begin_array:end_array] radii_f.resize((new_size,)) radii_f[particles:] = radii[begin_array:end_array] if ptype==1: radii_f.resize((new_size,)) radii_f[particles:] = radii[begin_array:end_array] mass_f.resize((new_size,)) mass_f[particles:] = mass[begin_array:end_array] particles = new_size begin_subfile = end_subfile f.close() print 'Halo mass = %.3e'%halo_mass[halo_number] print 'Halo pos =',halo_pos[halo_number] print 'Number of particles in the halo = %ld'%particles