def createmesh_catalog(self, bs, nc, halocat, cencat, satcat, mode='halos', position='RSDpos', weight='HImass', tofield=False): '''use this to create mesh of HI ''' comm = halocat.comm if mode == 'halos': catalogs = [halocat] else: print('Mode not recognized') rankweight = sum([cat[weight].sum().compute() for cat in catalogs]) totweight = comm.allreduce(rankweight) for cat in catalogs: cat[weight] /= totweight / float(nc)**3 allcat = MultipleSpeciesCatalog( ['%d' % i for i in range(len(catalogs))], *catalogs) mesh = allcat.to_mesh(BoxSize=bs,Nmesh=[nc,nc,nc],\ position=position,weight=weight) if tofield: mesh = mesh.to_field() return mesh
def calc_pk1d(aa, suff): '''Compute the 1D redshift-space P(k) for the HI''' if rank == 0: print('Read in central/satellite catalogs') cencat = BigFileCatalog(scratch2 + sim + '/fastpm_%0.4f/cencat-16node' % aa) satcat = BigFileCatalog(scratch2 + sim + '/fastpm_%0.4f/satcat' % aa + suff) rsdfac = read_conversions(scratch1 + sim + '/fastpm_%0.4f/' % aa) # Compute the power spectrum los = [0, 0, 1] cencat['RSDpos'] = cencat['Position'] + cencat['Velocity'] * los * rsdfac satcat['RSDpos'] = satcat['Position'] + satcat['Velocity'] * los * rsdfac cencat['HImass'] = HI_hod(cencat['Mass'], aa) satcat['HImass'] = HI_hod(satcat['Mass'], aa) rankHImass = cencat['HImass'].sum().compute() +\ satcat['HImass'].sum().compute() totHImass = comm.allreduce(rankHImass) cencat['HImass'] /= totHImass / float(nc)**3 satcat['HImass'] /= totHImass / float(nc)**3 allcat = MultipleSpeciesCatalog(['cen', 'sat'], cencat, satcat) h1mesh = allcat.to_mesh(BoxSize=bs,Nmesh=[nc,nc,nc],\ position='RSDpos',weight='HImass') pkh1h1 = FFTPower(h1mesh, mode='1d', kmin=0.025, dk=0.0125).power # Extract the quantities we want and write the file. kk = pkh1h1['k'] sn = pkh1h1.attrs['shotnoise'] pk = np.abs(pkh1h1['power']) if rank == 0: fout = open("HI_pks_1d_{:6.4f}.txt".format(aa), "w") fout.write("# Subtracting SN={:15.5e}.\n".format(sn)) fout.write("# {:>8s} {:>15s}\n".format("k", "Pk_0_HI")) for i in range(kk.size): fout.write("{:10.5f} {:15.5e}\n".format(kk[i], pk[i] - sn)) fout.close()
def calc_bias(aa,mcut,suff): '''Compute the bias(es) for the HI''' print('Read in DM mesh') dm = BigFileMesh(project+sim+'/fastpm_%0.4f/'%aa+\ '/dmesh_N%04d'%nc,'1').paint() dm /= dm.cmean() pkmm = FFTPower(dm,mode='1d').power k,pkmm= pkmm['k'],pkmm['power'] # Ignore shotnoise. # print('Read in central/satellite catalogs') cencat = BigFileCatalog(project+sim+'/fastpm_%0.4f/cencat'%aa) satcat = BigFileCatalog(project+sim+'/fastpm_%0.4f/satcat'%aa+suff) # cencat['HImass'] = HI_hod(cencat['Mass'],aa,mcut) satcat['HImass'] = HI_hod(satcat['Mass'],aa,mcut) totHImass = cencat['HImass'].sum().compute() +\ satcat['HImass'].sum().compute() cencat['HImass']/= totHImass/float(nc)**3 satcat['HImass']/= totHImass/float(nc)**3 # allcat = MultipleSpeciesCatalog(['cen','sat'],cencat,satcat) # h1mesh = allcat.to_mesh(BoxSize=bs,Nmesh=[nc,nc,nc],weight='HImass') pkh1h1 = FFTPower(h1mesh,mode='1d').power pkh1h1 = pkh1h1['power']-pkh1h1.attrs['shotnoise'] pkh1mm = FFTPower(h1mesh,second=dm,mode='1d').power['power'] # Compute the biases. b1x = np.abs(pkh1mm/(pkmm+1e-10)) b1a = np.abs(pkh1h1/(pkmm+1e-10))**0.5 return(k,b1x,b1a,np.abs(pkmm))
def calc_pkmu(aa,suff): '''Compute the redshift-space P(k) for the HI in mu bins''' if rank ==0 : print('Read in central/satellite catalogs') cencat = BigFileCatalog(scratch2+sim+'/fastpm_%0.4f/cencat-16node'%aa) satcat = BigFileCatalog(scratch2+sim+'/fastpm_%0.4f/satcat'%aa+suff) rsdfac = read_conversions(scratch1+sim+'/fastpm_%0.4f/'%aa) # Compute P(k,mu). los = [0,0,1] cencat['RSDpos'] = cencat['Position']+cencat['Velocity']*los * rsdfac satcat['RSDpos'] = satcat['Position']+satcat['Velocity']*los * rsdfac cencat['HImass'] = HI_hod(cencat['Mass'],aa) satcat['HImass'] = HI_hod(satcat['Mass'],aa) totHImass = cencat['HImass'].sum().compute() +\ satcat['HImass'].sum().compute() cencat['HImass']/= totHImass/float(nc)**3 satcat['HImass']/= totHImass/float(nc)**3 allcat = MultipleSpeciesCatalog(['cen','sat'],cencat,satcat) h1mesh = allcat.to_mesh(BoxSize=bs,Nmesh=[nc,nc,nc],\ position='RSDpos',weight='HImass') #pkh1h1 = FFTPower(h1mesh,mode='2d',Nmu=4,los=los).power cenmesh = pm.paint(cencat['RSDpos'], mass=cencat['HImass']) satmesh = pm.paint(satcat['RSDpos'], mass=satcat['HImass']) h1mesh = cenmesh + satmesh pkh1h1 = FFTPower(h1mesh,mode='2d',Nmu=4,los=los).power # Extract what we want. kk = pkh1h1.coords['k'] sn = pkh1h1.attrs['shotnoise'] pk = pkh1h1['power'] # Write the results to a file. if rank ==0 : fout = open("../data/HI_pks_mu_{:06.4f}.txt".format(aa),"w") fout.write("# Redshift space power spectrum in mu bins.\n") fout.write("# Subtracting SN={:15.5e}.\n".format(sn)) ss = "# {:>8s}".format(r'k\mu') for i in range(pkh1h1.shape[1]): ss += " {:15.5f}".format(pkh1h1.coords['mu'][i]) fout.write(ss+"\n") for i in range(1,pk.shape[0]): ss = "{:10.5f}".format(kk[i]) for j in range(pk.shape[1]): ss += " {:15.5e}".format(np.abs(pk[i,j]-sn)) fout.write(ss+"\n") fout.close()
def calc_bias(aa, mcut, suff): '''Compute the bias(es) for the HI''' if rank == 0: print("Processing a={:.4f}...".format(aa)) print('Reading DM mesh...') dm = BigFileMesh(scratch1+sim+'/fastpm_%0.4f/'%aa+\ '/1-mesh/N%04d'%nc,'').paint() dm /= dm.cmean() if rank == 0: print('Computing DM P(k)...') pkmm = FFTPower(dm, mode='1d').power k, pkmm = pkmm['k'], pkmm['power'] # Ignore shotnoise. if rank == 0: print('Done.') # if rank == 0: print('Reading central/satellite catalogs...') cencat = BigFileCatalog(scratch2 + sim + '/fastpm_%0.4f/cencat-16node' % aa) satcat = BigFileCatalog(scratch2 + sim + '/fastpm_%0.4f/satcat' % aa + suff) if rank == 0: print('Catalogs read.') # if rank == 0: print('Computing HI masses...') cencat['HImass'] = HI_hod(cencat['Mass'], aa, mcut) satcat['HImass'] = HI_hod(satcat['Mass'], aa, mcut) rankHImass = cencat['HImass'].sum().compute() +\ satcat['HImass'].sum().compute() rankHImass = np.array([rankHImass]) totHImass = np.zeros(1, dtype='float') comm.Allreduce(rankHImass, totHImass, MPI.SUM) totHImass = totHImass[0] cencat['HImass'] /= totHImass / float(nc)**3 satcat['HImass'] /= totHImass / float(nc)**3 if rank == 0: print('HI masses done.') # if rank == 0: print('Combining catalogs and computing P(k)...') allcat = MultipleSpeciesCatalog(['cen', 'sat'], cencat, satcat) h1mesh = allcat.to_mesh(BoxSize=bs, Nmesh=[nc, nc, nc], weight='HImass') pkh1h1 = FFTPower(h1mesh, mode='1d').power pkh1h1 = pkh1h1['power'] - pkh1h1.attrs['shotnoise'] pkh1mm = FFTPower(h1mesh, second=dm, mode='1d').power['power'] if rank == 0: print('Done.') # Compute the biases. b1x = np.abs(pkh1mm / (pkmm + 1e-10)) b1a = np.abs(pkh1h1 / (pkmm + 1e-10))**0.5 if rank == 0: print("Finishing processing a={:.4f}.".format(aa)) return (k, b1x, b1a, np.abs(pkmm))
def calc_pkll(aa,suff): '''Compute the redshift-space P_ell(k) for the HI''' if rank ==0 : print('Read in central/satellite catalogs') cencat = BigFileCatalog(scratch2+sim+'/fastpm_%0.4f/cencat-16node'%aa) satcat = BigFileCatalog(scratch2+sim+'/fastpm_%0.4f/satcat'%aa+suff) rsdfac = read_conversions(scratch1+sim+'/fastpm_%0.4f/'%aa) # los = [0,0,1] cencat['RSDpos'] = cencat['Position']+cencat['Velocity']*los * rsdfac satcat['RSDpos'] = satcat['Position']+satcat['Velocity']*los * rsdfac cencat['HImass'] = HI_hod(cencat['Mass'],aa) satcat['HImass'] = HI_hod(satcat['Mass'],aa) totHImass = cencat['HImass'].sum().compute() +\ satcat['HImass'].sum().compute() cencat['HImass']/= totHImass/float(nc)**3 satcat['HImass']/= totHImass/float(nc)**3 allcat = MultipleSpeciesCatalog(['cen','sat'],cencat,satcat) h1mesh = allcat.to_mesh(BoxSize=bs,Nmesh=[nc,nc,nc],\ position='RSDpos',weight='HImass') #pkh1h1 = FFTPower(h1mesh,mode='2d',Nmu=8,los=los,poles=[0,2,4]).poles cenmesh = pm.paint(cencat['RSDpos'], mass=cencat['HImass']) satmesh = pm.paint(satcat['RSDpos'], mass=satcat['HImass']) h1mesh = cenmesh + satmesh pkh1h1 = FFTPower(h1mesh,mode='2d',Nmu=8,los=los,poles=[0,2,4]).poles # Extract the quantities of interest. kk = pkh1h1.coords['k'] sn = pkh1h1.attrs['shotnoise'] P0 = pkh1h1['power_0'].real - sn P2 = pkh1h1['power_2'].real P4 = pkh1h1['power_4'].real # Write the results to a file. if rank ==0 : fout = open("../data/HI_pks_ll_{:06.4f}.txt".format(aa),"w") fout.write("# Redshift space power spectrum multipoles.\n") fout.write("# Subtracting SN={:15.5e} from monopole.\n".format(sn)) fout.write("# {:>8s} {:>15s} {:>15s} {:>15s}\n".format("k","P0","P2","P4")) for i in range(1,kk.size): fout.write("{:10.5f} {:15.5e} {:15.5e} {:15.5e}\n".\ format(kk[i],P0[i],P2[i],P4[i])) fout.close()
def to_catalog(self, **kwargs): from nbodykit.lab import MultipleSpeciesCatalog names = [] sources = [] for spname, sp in self.species.items(): sources.append(sp.to_catalog()) names.append(spname) cat = MultipleSpeciesCatalog(names, *sources, **kwargs) return cat
def measurexi(N, edges): '''plot the power spectrum of halos and H1 with subsampling''' suff = '-m1_00p3mh-alpha-0p8-subvol' outfolder = ofolder + suff[1:] if bs == 1024: outfolder = outfolder + "-big" outfolder += "/%s/" % modelname for i, aa in enumerate(alist): dm = BigFileCatalog(scratchyf + sim + '/fastpm_%0.4f/' % aa, dataset='1') rng = np.random.RandomState(dm.comm.rank) rank = dm.comm.rank halocat = BigFileCatalog(scratchyf + sim + '/fastpm_%0.4f//' % aa, dataset='LL-0.200') mp = halocat.attrs['MassTable'][1] * 1e10 ## halocat['Mass'] = halocat['Length'].compute() * mp cencat = BigFileCatalog(scratchcm + sim + '/fastpm_%0.4f/cencat' % aa + suff) satcat = BigFileCatalog(scratchcm + sim + '/fastpm_%0.4f/satcat' % aa + suff) # HImodelz = HImodel(aa) los = [0, 0, 1] halocat['HImass'], cencat['HImass'], satcat[ 'HImass'] = HImodelz.assignHI(halocat, cencat, satcat) for cat in [halocat, cencat, satcat]: cat['Weight'] = cat['HImass'] dm['Weight'] = np.ones(dm.size) for cat in [dm, halocat, cencat, satcat]: # nbodykit bug in SimBox2PCF that asserts boxsize cat.attrs['BoxSize'] = np.broadcast_to(cat.attrs['BoxSize'], 3) # #Combine galaxies to halos comm = halocat.comm if mode == 'halos': catalogs = [halocat] elif mode == 'galaxies': catalogs = [cencat, satcat] elif mode == 'all': catalogs = [halocat, cencat, satcat] else: print('Mode not recognized') rankweight = sum([cat['Weight'].sum().compute() for cat in catalogs]) totweight = comm.allreduce(rankweight) for cat in catalogs: cat['Weight'] /= totweight / float(nc)**3 allcat = MultipleSpeciesCatalog( ['%d' % i for i in range(len(catalogs))], *catalogs) #Subsample if rank == 0: print('redshift = ', 1 / aa - 1) print('Number of dm particles = ', dm.csize) print('Number of halos particles = ', halocat.csize) def subsampler(cat, rng, N, rmax): # subsample such that we have at most N particles to rmax nbar = (cat.csize / cat.attrs['BoxSize'].prod()) ratio = (N / rmax**3) / nbar mask = rng.uniform(size=cat.size) < ratio cat1 = cat[mask] if rank == 0: print('truncating catalog from %d to %d' % (cat.csize, cat1.csize)) return cat1 if rank == 0: print('Create weight array') #halocat = subsampler(halocat, rng, N, edges.max()) dm = subsampler(dm, rng, N, edges.max()) if rank == 0: print("Correlation function for edges :\n", edges) start = time() #xim = SimulationBox2PCF('1d', data1=dm, edges=edges) end = time() if rank == 0: print('Time for matter = ', end - start) #Others mass weighted start = time() xih1mass = SimulationBox2PCF('1d', data1=halocat, weight='Weight', edges=edges) end = time() if rank == 0: print('Time for HI = ', end - start) start = time() ximxh1mass = SimulationBox2PCF('1d', data1=halocat, data2=dm, weight='Weight', edges=edges) end = time() if rank == 0: print('Time for Cross = ', end - start) def savebinned(path, binstat, header): r, xi = binstat.corr['r'].real, binstat.corr['corr'].real if rank == 0: try: os.makedirs(os.path.dirname(path)) except IOError: pass np.savetxt(path, np.stack((r, xi), axis=1), header=header) #savebinned(ofolder+'ximatter.txt', xim, header='r, xi(r)') savebinned(outfolder + "xih1mass_{:6.4f}.txt".format(aa), xih1mass, header='r, xi(r)') savebinned(outfolder + "ximxh1mass_{:6.4f}.txt".format(aa), ximxh1mass, header='r, xi(r)')