def pylians3Bispectrum(delta, k1, k2, theta): ''' Get data from pylians3 Bispectrum measurements. returns: array_like k1/kF | k2/kF | k3/kF | P(k1) | P(k2) | P(k3) | B(k1,k2,k3) | Number of triangles delta : array_like input data, shape = (numfiles, gridsize, gridsize, gridsize) k1,k2: fixed wavenumber in units of the fundamental frequency. integer number. theta: angle between k1 and k2. ''' Bk = PKL.Bk(delta, BoxSize, k1 * kf, k2 * kf, theta, MAS, threads) BSk_pyl = Bk.B #bispectrm triangle_conf = Bk.triangles #triangle counts k3_pyl = Bk.k[2:] #k-bins for power spectrum Pk1_pyl = Bk.Pk[0] #power spectrum PS(k1) Pk2_pyl = Bk.Pk[1] # ´´ PS(k2) Pk3_pyl = Bk.Pk[2:] # ´´ PS(k3) lenn = len(theta) return np.vstack([ np.full(lenn, k1), np.full(lenn, k2), k3_pyl / kf, np.full(lenn, Pk1_pyl), np.full(lenn, Pk2_pyl), Pk3_pyl, BSk_pyl, triangle_conf ])
def res (pars, **kwargs): A = pars['A'].value B = pars['B'].value C = pars['C'].value # displacing particles draw_particles(kwargs['pos'], kwargs['mass'], kwargs['npart'],kwargs['r'], kwargs['theta'], kwargs['phi'], A, B, C, kwargs['z']) pos_collapsed = np.repeat(kwargs['pos'], kwargs['npart'], axis=0) \ + np.transpose([kwargs['r']*np.sin(kwargs['theta'])*np.cos(kwargs['phi']), \ kwargs['r']*np.sin(kwargs['theta'])*np.sin(kwargs['phi']), \ kwargs['r']*np.cos(kwargs['theta'])]) pos_collapsed /= params.boxsize wrapPositions(pos_collapsed) pos_collapsed *= params.boxsize # Computing delta delta = np.copy(delta_uncollapsed) MASL.MA(pos_collapsed, delta, params.boxsize, 'CIC', verbose=False) Pk = PKL.Pk(delta, params.boxsize, 0, 'CIC', 1, verbose=False) # Getting only half of the values size = Pk.Nmodes3D.size//2 sigma = Pk.Pk[:size,0] * np.sqrt( (2.0/Pk.Nmodes3D[:size]) + kwargs['sigma_target']**2 ) return np.sum( ((Pk.Pk[:size,0] - kwargs['target']) / sigma)**2 )
def compute_powerspectrum(self, other, save=True): #{{{ if other is None: self.__compute_powerspectrum() if save: self._save_powerspectrum() else: assert isinstance(other, Field) assert np.allclose(self.BoxSize, other.BoxSize) Pk = PKL.XPk([self.data, other.data], self.BoxSize, 0, [self.MAS, other.MAS], ARGS.threads) self.powerspectrum = { 'k': Pk.k3D, 'P': Pk.Pk[:, 0, 0], } other.powerspectrum = { 'k': Pk.k3D, 'P': Pk.Pk[:, 0, 1], } self.crosspower = { 'k': Pk.k3D, 'r': Pk.XPk[:, 0, 0] / np.sqrt(Pk.Pk[:, 0, 0] * Pk.Pk[:, 0, 1]), } if save: self._save_powerspectrum() other._save_powerspectrum() np.savez(self.summary_path + '%s.npz' % self.crosspowername, **self.crosspower) if ARGS.verbose: print 'Computed powerspectrum in Field(%s)' % self.mode
def Pk_comp(snapshot_fname,ptype,dims,do_RSD,axis,cpus,folder_out): # read relevant paramaters on the header print 'Computing power spectrum...' head = readsnap.snapshot_header(snapshot_fname) BoxSize = head.boxsize/1e3 #Mpc/h Masses = head.massarr*1e10 #Msun/h Nall = head.nall; Ntotal = np.sum(Nall,dtype=np.int64) Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift Hubble = 100.0*np.sqrt(Omega_m*(1.0+redshift)**3+Omega_l) #km/s/(Mpc/h) z = '%.3f'%redshift # find output file name fout = folder_out+'/Pk_' + name_dict[str(ptype)] if do_RSD: fout += ('_RS_axis=' + str(axis) + '_z=' + z + '.dat') else: fout += ('_z=' + z + '.dat') # read the positions of the particles pos = readsnap.read_block(snapshot_fname,"POS ",parttype=ptype)/1e3 #Mpc/h print '%.3f < X [Mpc/h] < %.3f'%(np.min(pos[:,0]),np.max(pos[:,0])) print '%.3f < Y [Mpc/h] < %.3f'%(np.min(pos[:,1]),np.max(pos[:,1])) print '%.3f < Z [Mpc/h] < %.3f\n'%(np.min(pos[:,2]),np.max(pos[:,2])) # read the velocities of the particles if do_RSD: print 'moving particles to redshift-space...' vel = readsnap.read_block(snapshot_fname,"VEL ",parttype=ptype) #km/s RSL.pos_redshift_space(pos,vel,BoxSize,Hubble,redshift,axis) del vel; print 'done' # define delta array delta = np.zeros((dims,dims,dims),dtype=np.float32) # when dealing with all particles take into account their different masses if ptype==-1: if Nall[0]==0: #if not hydro M = np.zeros(Ntotal,dtype=np.float32) #define the mass array offset = 0 for ptype in [0,1,2,3,4,5]: M[offset:offset+Nall[ptype]] = Masses[ptype] offset += Nall[ptype] else: M = readsnap.read_block(snapshot_fname,"MASS",parttype=-1)*1e10 mean = np.sum(M,dtype=np.float64)/dims**3 MASL.MA(pos,delta,BoxSize,'CIC',M); del pos,M else: mean = len(pos)*1.0/dims**3 MASL.MA(pos,delta,BoxSize,'CIC'); del pos # compute the P(k) and save results to file delta /= mean; delta -= 1.0 Pk = PKL.Pk(delta,BoxSize,axis=axis,MAS='CIC',threads=cpus); del delta np.savetxt(fout,np.transpose([Pk.k3D, Pk.Pk[:,0], Pk.Pk[:,1], Pk.Pk[:,2], Pk.Nmodes3D]))
def compute_matter_bispectrum(vols, cubedim=64, k1=0.1, k2=0.5): thetas = np.linspace(0.0, 2.5, 10) bks = [] for i in xrange(vols.shape[0]): with HiddenPrints(): bis = PKL.Bk(vols[i] - np.mean(vols[i]), float(cubedim), k1, k2, thetas) bks.append(bis.B) return bks, thetas
def getG3power(sim, kmax): if z == 99: snap = sim + "/output/ics" elif z == 49: snap = sim + "/output/snapdir_000/PART_000" elif z == 9: snap = sim + "/output/snapdir_001/PART_001" elif z == 4: snap = sim + "/output/snapdir_002/PART_002" elif z == 3: snap = sim + "/output/snapdir_003/PART_003" elif z == 2: snap = sim + "/output/snapdir_005/PART_005" else: print("Don't have data for that redshift") quit() head = readsnap.snapshot_header(snap) rho_crit = 2.77536627e11 BoxSize = head.boxsize / 1e3 #Mpc/h Nall = head.nall Masses = head.massarr * 1e10 #Msun/h Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift ## Pylian params grid = 512 ptypes = [1] MAS = 'CIC' do_RSD = False axis = 0 threads = 1 assert (z - redshift) < 0.001, "Redshift requested: %s, redshift found: %s" % ( z, redshift) Omega_cdm = Nall[1] * Masses[1] / BoxSize**3 / rho_crit Omega_b = Omega_m - Omega_cdm ## Calculate fractions f_b = Omega_b / (Omega_cdm + Omega_b) f_c = Omega_cdm / (Omega_cdm + Omega_b) ## CDM delta = MASL.density_field_gadget(snap, ptypes, grid, MAS, do_RSD, axis) delta /= np.mean(delta, dtype=np.float64) delta -= 1.0 print("Mean after normalising = %.2e" % np.mean(delta, dtype=np.float64)) Pk = PKL.Pk(delta, BoxSize, axis, MAS, threads) # Calculate power k_sim = Pk.k3D Pk_sim = Pk.Pk[:, 0] Nmodes1D = Pk.Nmodes1D return k_sim[np.where(k_sim < kmax)], Pk_sim[np.where( k_sim < kmax)], BoxSize
def compute_Pk(snapshot, grid, MAS, threads, ptype, root_out): # read header if not(os.path.exists(snapshot)): return 0 head = readgadget.header(snapshot) BoxSize = head.boxsize/1e3 #Mpc/h Nall = head.nall #Total number of particles Masses = head.massarr*1e10 #Masses of the particles in Msun/h Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift Hubble = 100.0*np.sqrt(Omega_m*(1.0+redshift)**3+Omega_l)#km/s/(Mpc/h) h = head.hubble Ntot = np.sum(Nall[ptype], dtype=np.int64) # get the name of the output file fout = '%s/Pk_%s_z=%.2f.txt'%(root_out, Pk_suffix(ptype), redshift) if os.path.exists(fout): return 0 # define the arrays containing the number positions and masses of the particles pos = np.zeros((Ntot,3), dtype=np.float32) mass = np.zeros(Ntot, dtype=np.float32) # read data for the different particle types f = h5py.File(snapshot, 'r'); offset = 0 for pt in ptype: # sometimes there are not black-holes or stars... if 'PartType%d'%pt not in f.keys(): continue # read positions pos_pt = f['PartType%d/Coordinates'%pt][:]/1e3 #Mpc/h if pos_pt.dtype==np.float64: pos_pt = pos_pt.astype(np.float32) # read masses if 'PartType%d/Masses'%pt in f: mass_pt = f['PartType%d/Masses'%pt][:]*1e10 #Msun/h else: mass_pt = np.ones(pos_pt.shape[0], dtype=np.float32)*Masses[1] #Msun/h # fill pos and mass arrays length = len(pos_pt) pos[offset:offset+length] = pos_pt mass[offset:offset+length] = mass_pt offset += length f.close() if offset!=Ntot: raise Exception('Not all particles counted') # calculate density field delta = np.zeros((grid,grid,grid), dtype=np.float32) MASL.MA(pos, delta, BoxSize, MAS, W=mass) delta /= np.mean(delta, dtype=np.float64); delta -= 1.0 # compute Pk and save results to file axis = 0 Pk = PKL.Pk(delta, BoxSize, axis, MAS, threads) np.savetxt(fout, np.transpose([Pk.k3D, Pk.Pk[:,0]]), delimiter='\t')
def Pk_binning(fin, BoxSize, grid): # read input file k_in, Pk_in = np.loadtxt(fin, dtype=np.float32, unpack=True) # compute expected Pk k, Pk, Nmodes = PKL.expected_Pk(k_in, Pk_in, BoxSize, grid) Pk = np.asarray(Pk) Nmodes = np.asarray(Nmodes) return k, Pk, Nmodes
def compute_bispectrum(self, save=True): #{{{ # (1) triangle configurations k = 1, 3, varying angles _k1 = 1.0 _k2 = 3.0 _theta = np.linspace(0.0, np.pi, num=20) Bk1 = PKL.Bk(self.data, self.BoxSize, _k1, _k2, _theta, self.MAS, ARGS.threads).Q if ARGS.verbose: print 'Computed Bk1 in Field(%s)' % self.mode # (2) triangle configurations k = 0.1, 0.3, varying angles _k1 = 0.2 _k2 = 0.3 _theta = np.linspace(0.0, np.pi, num=20) Bk2 = PKL.Bk(self.data, self.BoxSize, _k1, _k2, _theta, self.MAS, ARGS.threads).Q if ARGS.verbose: print 'Computed Bk2 in Field(%s)' % self.mode # (3) equilateral triangle configurations _k = 10.0**np.linspace(np.log10(0.2), np.log10(3.0), num=20) _theta = np.array([np.pi / 3.0]) Bk3 = [] for k in _k: Bk3.append( PKL.Bk(self.data, self.BoxSize, k, k, _theta, self.MAS, ARGS.threads).Q) if ARGS.verbose: print 'Computed Bk3 in Field(%s)' % self.mode self.bispectrum = { 'Bk1': Bk1, 'Bk2': Bk2, 'Bk3': Bk3, } if save: np.savez(self.summary_path + '%s.npz' % self.bispectrumname, **self.bispectrum) if ARGS.verbose: print 'Computed bispectrum in Field(%s)' % self.mode
def compute_Pk(T_maps, BoxSize, fout): Pk = np.zeros((T_maps.shape[0], 45), dtype=np.float64) for i in range(T_maps.shape[0]): Pk_real = PKL.Pk_plane(T_maps[i], BoxSize, MAS='None', threads=1, verbose=False) k, Pk[i] = Pk_real.k, Pk_real.Pk np.savetxt(fout, np.transpose([k, np.mean(Pk, axis=0), np.std(Pk, axis=0)])) return k, np.mean(Pk, axis=0), np.std(Pk, axis=0)
def find_CF(snapshot, snapnum, grid, MAS, do_RSD, axis, threads, ptype, fcf, save_multipoles): if os.path.exists(fcf): return 0 # read header head = readgadget.header(snapshot) BoxSize = head.boxsize / 1e3 #Mpc/h Nall = head.nall #Total number of particles Masses = head.massarr * 1e10 #Masses of the particles in Msun/h Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift Hubble = 100.0 * np.sqrt(Omega_m * (1.0 + redshift)**3 + Omega_l) #km/s/(Mpc/h) h = head.hubble # read snapshot pos = readgadget.read_block(snapshot, "POS ", ptype) / 1e3 #Mpc/h # move particles to redshift-space if do_RSD: vel = readgadget.read_block(snapshot, "VEL ", ptype) #km/s RSL.pos_redshift_space(pos, vel, BoxSize, Hubble, redshift, axis) # calculate CF delta = np.zeros((grid, grid, grid), dtype=np.float32) if len(ptype) > 1: #for multiple particles read masses mass = np.zeros(pos.shape[0], dtype=np.float32) offset = 0 for j in ptype: mass[offset:offset + Nall[j]] = Masses[j] offset += Nall[j] MASL.MA(pos, delta, BoxSize, MAS, W=mass) else: MASL.MA(pos, delta, BoxSize, MAS) delta /= np.mean(delta, dtype=np.float64) delta -= 1.0 CF = PKL.Xi(delta, BoxSize, MAS, axis, threads) # save results to file if save_multipoles: np.savetxt(fcf, np.transpose( [CF.r3D, CF.xi[:, 0], CF.xi[:, 1], CF.xi[:, 2]]), delimiter='\t') else: np.savetxt(fcf, np.transpose([CF.r3D, CF.xi[:, 0]]), delimiter='\t')
def computePower(delta, realization): ''' Measure Powerspectrum. The output is saved in a txt file in the folder specified by output in initializeGlobals. delta : array_like input data, shape = (gridsize, gridsize, gridsize) realization: integer file format: k | P0(k) ''' filename = OutputDir + 'powerspectrum%i.dat' % realization print('\n Computing power, saving at ' + filename) Pkpyl = PKL.Pk(delta, BoxSize, axis, MAS, threads, verbose) np.savetxt(filename, np.vstack([Pkpyl.k3D, Pkpyl.Pk[:, 0]]).T)
def find_Pk(folder, snapdir, snapnum, grid, MAS, do_RSD, axis, threads, fixed_Mmin, Mmin, Nhalos, fpk, save_multipoles): if os.path.exists(fpk): return 0 # read header head = readgadget.header(snapdir) BoxSize = head.boxsize/1e3 #Mpc/h Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift Hubble = 100.0*np.sqrt(Omega_m*(1.0+redshift)**3+Omega_l)#km/s/(Mpc/h) h = head.hubble # read halo catalogue FoF = readfof.FoF_catalog(folder, snapnum, long_ids=False, swap=False, SFR=False, read_IDs=False) pos_h = FoF.GroupPos/1e3 #Mpc/h mass = FoF.GroupMass*1e10 #Msun/h vel_h = FoF.GroupVel*(1.0+redshift) #km/s if fixed_Mmin: indexes = np.where(mass>Mmin)[0] pos_h = pos_h[indexes]; vel_h = vel_h[indexes]; del indexes else: indexes = np.argsort(mass)[-Nhalos:] #take the Nhalos most massive halos pos_h = pos_h[indexes]; vel_h = vel_h[indexes]; del indexes # move halos to redshift-space if do_RSD: RSL.pos_redshift_space(pos_h, vel_h, BoxSize, Hubble, redshift, axis) # calculate Pk delta = np.zeros((grid,grid,grid), dtype=np.float32) MASL.MA(pos_h, delta, BoxSize, MAS) delta /= np.mean(delta, dtype=np.float64); delta -= 1.0 Pk = PKL.Pk(delta, BoxSize, axis, MAS, threads) # save results to file hdr = ('Nhalos=%i BoxSize=%.3f'%(pos_h.shape[0],BoxSize)) if save_multipoles: np.savetxt(fpk, np.transpose([Pk.k3D, Pk.Pk[:,0], Pk.Pk[:,1], Pk.Pk[:,2]]), delimiter='\t', header=hdr) else: np.savetxt(fpk, np.transpose([Pk.k3D, Pk.Pk[:,0]]), delimiter='\t', header=hdr)
def compute_Pk_ICs(snapshot, grid, MAS, threads, ptype, root_out): if not(os.path.exists(snapshot)) and not(os.path.exists(snapshot+'.0')): return 0 # read header head = readgadget.header(snapshot) BoxSize = head.boxsize/1e3 #Mpc/h redshift = head.redshift # get the name of the file fout = '%s/Pk_%s_z=%.2f.txt'%(root_out, Pk_suffix(ptype), redshift) if os.path.exists(fout): return 0 # compute overdensity field do_RSD, axis = False, 0 delta = MASL.density_field_gadget(snapshot, ptype, grid, MAS, do_RSD, axis) delta /= np.mean(delta, dtype=np.float64); delta -= 1.0 # compute Pk and save results to file Pk = PKL.Pk(delta, BoxSize, axis, MAS, threads) np.savetxt(fout, np.transpose([Pk.k3D, Pk.Pk[:,0]]), delimiter='\t')
def Mk(galaxy_pos, Filter, R, p, ds, BoxSize, grid, MAS, threads): ''' Measure the marked spectrum using the `Pylians3` package Input: galaxy_pos: (N,3) array FIlter: 'Top-Hat' or 'Gaussian' R: parameter of the mark: scale to define local density p: parameter of the mark ds: parameter of the mark BoxSize grid: scalar: size of the grid where we compute the density MAS: 'CIC' threads: scalar Output: Pk: object with power spectrum: k = Pk.k3D P0 = Pk.Pk[:,0] P2 = Pk.Pk[:,1] P4 = Pk.Pk[:,2] ''' # calculate delta delta = np.zeros((grid,grid,grid), dtype=np.float32) MASL.MA(galaxy_pos, delta, BoxSize, MAS) delta /= np.mean(delta, dtype=np.float64); delta -= 1.0 # smooth delta W_k = SL.FT_filter(BoxSize, R, grid, Filter, threads) delta_smoothed = SL.field_smoothing(delta, W_k, threads) # marks weight = np.zeros(galaxy_pos.shape[0], dtype=np.float32) MASL.CIC_interp(delta_smoothed, BoxSize, galaxy_pos, weight) mark = func_mark(weight,ds,p) delta_m = np.zeros((grid,grid,grid), dtype=np.float32) MASL.MA(galaxy_pos,delta_m,BoxSize,MAS,W=mark) delta_m /= np.mean(delta_m,dtype=np.float32); delta_m -= 1.0 # compute marked Pk Pk = PKL.Pk(delta_m, BoxSize, axis, MAS, threads) return Pk
import sys snapshot = sys.argv[1] #'fR5_mnu016_DUSTGRAIN_snap_463' BoxSize = float(sys.argv[2]) #2000.0 #Mpc/h grid = long(sys.argv[3]) #1024 ptypes = map(int,sys.argv[4].split(",")) MAS = 'CIC' do_RSD = False axis = 0 threads=1 ## First do the total Power Spectrum delta = MASL.density_field_gadget(snapshot, ptypes, grid, MAS, do_RSD, axis) delta /= np.mean(delta, dtype=np.float64); delta -= 1.0 Pk = PKL.Pk(delta, BoxSize, axis, MAS, threads) # 1D P(k) k1D = Pk.k1D Pk1D = Pk.Pk1D Nmodes1D = Pk.Nmodes1D # 2D P(k) kpar = Pk.kpar kper = Pk.kper Pk2D = Pk.Pk2D Nmodes2D = Pk.Nmodes2D # 3D P(k) k = Pk.k3D Pk0 = Pk.Pk[:,0] #monopole Pk2 = Pk.Pk[:,1] #quadrupole
# do a loop over all the maps for i in numbers: if i % 10000 == 0: print(i) # get the value of the Pk Pk_in = A[i] * k_in**B # generate density field maps_partial[i] = DFL.gaussian_field_2D(grid, k_in, Pk_in, Rayleigh_sampling, seed[i], BoxSize, threads, verbose) # compute power spectrum Pk_partial[i] = PKL.Pk_plane(maps_partial[i], BoxSize, MAS, threads, verbose).Pk #np.savetxt('Pk1.txt', np.transpose([Pk.k, Pk.Pk, Pk.Nmodes])) #print(1.0/np.sqrt(np.sum(Pk.Nmodes))) # make some statistics #print(np.mean(maps[i]), np.min(maps[i]), np.max(maps[i])) # make image """ fig=figure() ax1=fig.add_subplot(111) cax = ax1.imshow(df,cmap=get_cmap('jet'),origin='lower',interpolation='spline36', extent=[0, BoxSize, 0, BoxSize]) #vmin=min_density,vmax=max_density) #norm = LogNorm(vmin=min_density,vmax=max_density))
def getG3power(sim, kmax): ## Get desired redshift if z == 99: snap = sim + "/output/ics" elif z == 49: snap = sim + "/output/snapdir_000/PART_000" elif z == 9: snap = sim + "/output/snapdir_001/PART_001" elif z == 4: snap = sim + "/output/snapdir_002/PART_002" elif z == 3: snap = sim + "/output/snapdir_003/PART_003" elif z == 2: snap = sim + "/output/snapdir_005/PART_005" else: print("Don't have data for that redshift") quit() head = readsnap.snapshot_header(snap) rho_crit = 2.77536627e11 BoxSize = head.boxsize / 1e3 #Mpc/h Nall = head.nall Masses = head.massarr * 1e10 #Msun/h Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift ## Pylian params grid = 256 ptypes = [1] MAS = 'CIC' do_RSD = False axis = 0 threads = 1 assert (z - redshift) < 0.001, "Redshift requested: %s, redshift found: %s" % ( z, redshift) Omega_cdm = Nall[1] * Masses[1] / BoxSize**3 / rho_crit Omega_b = Omega_m - Omega_cdm ## Calculate fractions f_b = Omega_b / (Omega_cdm + Omega_b) f_c = Omega_cdm / (Omega_cdm + Omega_b) ## CDM delta = MASL.density_field_gadget(snap, ptypes, grid, MAS, do_RSD, axis) delta /= np.mean(delta, dtype=np.float64) delta -= 1.0 print("Mean after normalising = %.2e" % np.mean(delta, dtype=np.float64)) Pk = PKL.Pk(delta, BoxSize, axis, MAS, threads) # Calculate power k_sim = Pk.k3D Pk_sim = Pk.Pk[:, 0] Nmodes1D = Pk.Nmodes1D ## Baryons deltaby = MASL.density_field_gadget(snap, [0], grid, MAS, do_RSD, axis) deltaby /= np.mean(deltaby, dtype=np.float64) deltaby -= 1.0 print("Mean after normalising = %.2e" % np.mean(delta, dtype=np.float64)) Pkby = PKL.Pk(deltaby, BoxSize, axis, MAS, threads) # Calculate power k_simby = Pkby.k3D Pk_simby = Pkby.Pk[:, 0] Nmodes1D = Pk.Nmodes1D ## Make sure we can find total matter assert (np.mean(k_simby - k_sim) ) == 0.0, "k-arrays not equal, can't find total matter power" Pk_av = Pk_sim * f_c**2 + Pk_simby * f_b**2 + 2 * f_c * f_b * np.sqrt( Pk_sim * Pk_simby) return k_sim[np.where(k_sim < kmax)], Pk_simby[np.where( k_sim < kmax)], Pk_sim[np.where(k_sim < kmax)], Pk_av[np.where( k_sim < kmax)], BoxSize
print '\nREADING SNAPSHOTS PROPERTIES' head = readsnap.snapshot_header(snapshot_fname) BoxSize = head.boxsize / 1e3 #Mpc/h Nall = head.nall Masses = head.massarr * 1e10 #Msun/h Omega_m = head.omega_m Omega_l = head.omega_l redshift = head.redshift Hubble = 100.0 * np.sqrt(Omega_m * (1.0 + redshift)**3 + Omega_l) #km/s/(Mpc/h) h = head.hubble fout = 'CF_CDM_z=%.3f.txt' % redshift # read the positions and masses of the CDM particles pos = readsnap.read_block(snapshot_fname, "POS ", parttype=1) / 1e3 #Mpc/h # compute delta_CDM delta = np.zeros((dims, dims, dims), dtype=np.float32) MASL.MA(pos, delta, BoxSize, MAS) print '%.6e should be equal to\n%.6e'\ %(np.sum(delta,dtype=np.float64),len(pos)) delta /= np.mean(delta, dtype=np.float64) delta -= 1.0 #compute the correlation function CF = PKL.Xi(delta, BoxSize, MAS, threads=8) #save results to file np.savetxt(fout, np.transpose([CF.r3D, CF.xi]))
axis) delta_c /= np.mean(delta_c, dtype=np.float64) delta_c -= 1.0 # compute delta_h delta_h = np.zeros((grid, grid, grid), dtype=np.float32) FoF = readfof.FoF_catalog(f2, snapnum, long_ids=False, swap=False, SFR=False, read_IDs=False) pos_h = FoF.GroupPos / 1e3 #Mpc/h mass = FoF.GroupMass * 1e10 #Msun/h indexes = np.where(mass > Mmin)[0] pos_h = pos_h[indexes] del indexes MASL.MA(pos_h, delta_h, BoxSize, MAS) delta_h /= np.mean(delta_h, dtype=np.float64) delta_h -= 1.0 # compute power spectra Pk = PKL.XPk([delta_c, delta_h], BoxSize, axis, [MAS, MAS], threads) b[2 * i + pair] = (Pk.Pk[:, 0, 1] - BoxSize**3 / len(pos_h)) / Pk.Pk[:, 0, 0] fout = '%s/b_z=0.txt' % (cosmo) np.savetxt(fout, np.transpose([Pk.k3D, np.mean(b, axis=0), np.std(b, axis=0)]))
for axis in [-1, 0, 1, 2]: # read delta_c delta_c = f[obj[axis]['name']][:] delta_c /= np.mean(delta_c, dtype=np.float64) delta_c -= 1.0 # read delta_n delta_n = g[obj[axis]['name']][:] delta_n /= np.mean(delta_n, dtype=np.float64) delta_n -= 1.0 # compute auto- and cross-Pk Pk = PKL.XPk([delta_c, delta_n], BoxSize, axis=obj[axis]['axis'], MAS=['CIC', 'CIC'], threads=16) del delta_c, delta_n # save results to file np.savetxt( 'Pk_c%sz=%.1f.txt' % (obj[axis]['output'], z), np.transpose( [Pk.k3D, Pk.Pk[:, 0, 0], Pk.Pk[:, 1, 0], Pk.Pk[:, 2, 0]])) np.savetxt( 'Pk_n%sz=%.1f.txt' % (obj[axis]['output'], z), np.transpose( [Pk.k3D, Pk.Pk[:, 0, 1], Pk.Pk[:, 1, 1], Pk.Pk[:, 2, 1]])) np.savetxt( 'Pk_cn%sz=%.1f.txt' % (obj[axis]['output'], z),
# read the positions and velocities of the particles pos = readsnap.read_block(snapshot_fname,"POS ",parttype=1)/1e3 #Mpc/h vel = readsnap.read_block(snapshot_fname,"VEL ",parttype=1) #km/s # move particles to redshift-space RSL.pos_redshift_space(pos, vel, BoxSize, Hubble, redshift, axis) # compute density field in redshift-space delta = np.zeros((dims, dims, dims), dtype=np.float32) #this should be your density field MASL.MA(pos, delta, BoxSize, MAS='CIC') # computes the density in each cell of the grid delta = delta/np.mean(delta) - 1.0 # compute power spectra Pk = PKL.Pk(delta, BoxSize, axis, MAS='CIC', threads=cores) #Pk here is a class with all power spectra # 3D Pk k = Pk.k3D Pk0 = Pk.Pk[:,0] #monopole Pk2 = Pk.Pk[:,1] #quadrupole Pk4 = Pk.Pk[:,2] #hexadecapole Nmodes = Pk.Nmodes3D #number of modes in each Pk bin # 2D Pk kpar = Pk.kpar kperp = Pk.kper Pk2D = Pk.Pk2D Nmodes2D = Pk.Nmodes2D
#~ fid_file.close() #~ else : #~ with open(cname, 'a') as fid_file: #~ fid_file.write('%.8g %.8g %.8g %.8g\n' % ( np.sum(Hmass_a),np.sum(Hmass_b), np.sum(Hmass_c), np.sum(Hmass_d))) #~ fid_file.close() ############################################################### ######## fourth mass range ############################################################### delta1d = np.zeros((dims, dims, dims), dtype=np.float32) MASL.MA(pos[Hmass_ind_d], delta1d, BoxSize, MAS='CIC', W=None) delta1d = delta1d / np.mean(delta1d, dtype=np.float64) - 1.0 # compute power spectra Pk1d = PKL.Pk(delta1d, BoxSize, axis=0, MAS='CIC', threads=4) #Pk here is a class with all power spectra #shot noise Pshot_m4 = 1 / (len(Hmass_d) / BoxSize**3) # 3D Pk k_m4 = Pk1d.k3D #~ Pk0_m4 = (Pk1d.Pk[:,0] + Pk2d.Pk[:,0] + Pk3d.Pk[:,0] + Pk5d.Pk[:,0] + Pk6d.Pk[:,0] + Pk7d.Pk[:,0] + Pk8d.Pk[:,0])/7 #monopole #~ Pk2_m4 = (Pk1d.Pk[:,1] + Pk2d.Pk[:,1] + Pk3d.Pk[:,1] + Pk5d.Pk[:,1] + Pk6d.Pk[:,1] + Pk7d.Pk[:,1] + Pk8d.Pk[:,1])/7 #quadrupole #~ Pk4_m4 = (Pk1d.Pk[:,2] + Pk2d.Pk[:,2] + Pk3d.Pk[:,2] + Pk5d.Pk[:,2] + Pk6d.Pk[:,2] + Pk7d.Pk[:,2] + Pk8d.Pk[:,2])/7 #hexadecapole #~ Nmodes_m4 = (Pk1d.Nmodes3D + Pk2d.Nmodes3D + Pk3d.Nmodes3D + Pk5d.Nmodes3D + Pk6d.Nmodes3D + Pk7d.Nmodes3D + Pk8d.Nmodes3D)/7 #number of modes in each Pk bin #~ temp4 = np.array([Pk1d.Pk[:,0], Pk2d.Pk[:,0], Pk3d.Pk[:,0], Pk5d.Pk[:,0], Pk6d.Pk[:,0], Pk7d.Pk[:,0], Pk8d.Pk[:,0]]) #~ std4 = np.std(temp4, axis=0) cname = '/home/dvalcin/plots/Phh4_realisation_' + str( Mnu) + '_z=' + str(z) + '.txt'
import numpy as np import Pk_library as PKL ############################# INPUT ###################################### BoxSize = 1000.0 #size of the box in Mpc/h # N-GenIC generated files f_coordinates = 'Coordinates_ptype_0' f_amplitudes = 'Amplitudes_ptype_0' f_phases = 'Phases_ptype_0' #no needed for the P(k) ########################################################################## # compute the Pk of the density field k, Pk, Nmodes = PKL.Pk_NGenIC_IC_field(f_coordinates, f_amplitudes, BoxSize) np.savetxt('Pk_linear_df_m_z=127.txt', np.transpose([k, Pk, Nmodes]))
import Pk_library as PKL import numpy as np import h5py import sys test_cube = np.load('../dat/processed/test_cube_target.npy') pred_cube = np.load('../dat/processed/test_cube_final_prediction.npy') benchmark_cube = np.load('../dat/processed/benchmark_cube.npy') BoxSize = 31.82373046875 #Size of the density field in Mpc/h MAS = None threads = 16 axis = 0 # compute the correlation function CF = PKL.Xi(test_cube, BoxSize, MAS, axis, threads) r = CF.r3D #radii in Mpc/h xi0 = CF.xi[:, 0] #correlation function (monopole) #xi2 = CF.xi[:,1] #correlation function (quadrupole) #xi4 = CF.xi[:,2] #correlation function (hexadecapole) # save correlation function and r np.save('../processed/target_r_values.npy', r) np.save('../processed/target_xi0_values.npy', xi0) # compute dm2gal corr func CF = PKL.Xi(pred_cube, BoxSize, MAS, axis, threads) r = CF.r3D #radii in Mpc/h xi0 = CF.xi[:, 0] #correlation function (monopole) #xi2 = CF.xi[:,1] #correlation function (quadrupole) #xi4 = CF.xi[:,2] #correlation function (hexadecapole)
import cupy import Pk_library as PKL import time dimensions = 768 threads = 1 random_array = np.random.random( (dimensions, dimensions, dimensions)).astype(np.float32) cupy_array = cupy.array(random_array) print(random_array.shape, random_array.dtype) print(cupy_array.shape, cupy_array.dtype) start = time.time() cupy_fft = cupy.fft.rfftn(cupy_array, s=None, axes=None, norm=None) print('Time take for cupy fft = %.3f seconds' % (time.time() - start)) start = time.time() Paco_fft = PKL.FFT3Dr_f(random_array, threads) print('Time take for Paco fft = %.3f seconds' % (time.time() - start)) cupy_fft_cpu = cupy.asnumpy(cupy_fft) cupy_modulus = np.absolute(cupy_fft_cpu) Paco_modulus = np.absolute(Paco_fft) ratio = cupy_modulus / Paco_modulus #print(ratio) print(np.min(ratio), np.max(ratio))
import Pk_library as PKL ################################## INPUT ###################################### snapshot_fname = ['../ics', '../snapdir_000/snap_000', '../snapdir_001/snap_001', '../snapdir_002/snap_002', '../snapdir_003/snap_003'] dims = 1024 particle_type = [1,2] #list with particle types. [-1] for total matter cpus = 14 ############################################################################### # do a loop over the different snapshots for snapshot in snapshot_fname: ######## REAL-SPACE ######## do_RSD = False; axis = 0 PKL.Pk_Gadget(snapshot,dims,particle_type,do_RSD,axis,cpus) ###### REDSHIFT-SPACE ###### do_RSD = True for axis in [0,1,2]: PKL.Pk_Gadget(snapshot,dims,particle_type,do_RSD,axis,cpus)
grid = 256 #grid size, chose a small one for now because of RAM issues ptypes = [1] #we investigate the CDM + baryon power spectrum MAS = 'CIC' #Cloud-in-Cell do_RSD = False #dont do redshif-space distortions axis = 0 #axis along which place RSD; not used here verbose = True #whether print information on the progress BoxSize = 512 threads = 4 #calculate Pk for the 0.0eV reference run ref_delta = MASL.density_field_gadget(ref, ptypes, grid, MAS, do_RSD, axis, verbose) ref_delta /= np.mean(ref_delta, dtype=np.float64) ref_delta -= 1.0 ref_Pk = PKL.Pk(ref_delta, BoxSize, axis, MAS, threads, verbose) ref_k = ref_Pk.k3D ref_Pk0 = ref_Pk.Pk[:, 0] #calculate Pk for the reference run with given neutrino mass ref_mass_delta = MASL.density_field_gadget(ref_mass, ptypes, grid, MAS, do_RSD, axis, verbose) ref_mass_delta /= np.mean(ref_mass_delta, dtype=np.float64) ref_mass_delta -= 1.0 ref_mass_Pk = PKL.Pk(ref_mass_delta, BoxSize, axis, MAS, threads, verbose) ref_mass_k = ref_mass_Pk.k3D ref_mass_Pk0 = ref_mass_Pk.Pk[:, 0] #set up the figure fig, ax = plt.subplots(2, 2, figsize=(7, 7)) fig.suptitle(mass[:-1])
snapshot_root = '%s/output/' % run halos = groupcat.loadHalos( snapshot_root, snapnum, fields=['GroupPos', 'GroupMass', 'GroupVel']) halo_pos = halos['GroupPos'] / 1e3 #Mpc/h halo_mass = halos['GroupMass'] * 1e10 #Msun/h halo_vel = halos['GroupVel'] * (1.0 + z) #km/s del halos # move halo positions to redshift-space RSL.pos_redshift_space(halo_pos, halo_vel, BoxSize, Hubble, 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]) M_HI = M0 * (halo_mass / Mmin)**alpha * np.exp(-(Mmin / halo_mass)**(0.35)) delta_HI = np.zeros((dims, dims, dims), dtype=np.float32) MASL.MA(halo_pos, delta_HI, BoxSize, MAS, W=M_HI) delta_HI /= np.mean(delta_HI, dtype=np.float64) delta_HI -= 1.0 Pk = PKL.Pk(delta_HI, BoxSize, axis, MAS, 8) np.savetxt('Pk_HI_Nbody_redshift_space_%d_z=%.1f.txt' % (axis, z), np.transpose([Pk.k3D, Pk.Pk[:, 0]]))
import numpy as np import Pk_library as PKL import sys,os #################################### INPUT ########################################### BoxSize = 512.0 #Mpc/h grid = 1024 fin = '../../param_files/0.15eV/reps_files/0.15eV_Pm_rescaled_z127.0000.txt' fout = 'Pk_binned_CLASS_0.15eV_matter_z=127.txt' #fin = '../../param_files/0.15eV/reps_files/0.15eV_Pcb_rescaled_z127.0000.txt' #fout = 'Pk_binned_CLASS_0.15eV_cb_z=127.txt' #fin = '../../param_files/0.15eV/reps_files/0.15eV_Pn_rescaled_z127.0000.txt' #fout = 'Pk_binned_CLASS_0.15eV_n_z=127.txt' ###################################################################################### # read Pk k, Pk = np.loadtxt(fin, unpack=True) k = k.astype(np.float32) Pk = Pk.astype(np.float32) # compute binned Pk and save results to file k, Pk, Nmodes = PKL.expected_Pk(k, Pk, BoxSize, grid) np.savetxt(fout, np.transpose([k, Pk]))