def bias_from_files_cross(file1, file2, f_out, dims, n_halos):

    #read the DM-halos cross-power spectrum file
    k_cross, Pk_cross = [], []
    f = open(file1, 'r')
    for line in f.readlines():
        a = line.split()
        k_cross.append(float(a[0]))
        Pk_cross.append(float(a[1]))
    f.close()
    k_cross = np.array(k_cross)
    Pk_cross = np.array(Pk_cross)

    #read the DM power spectrum file
    k_DM, Pk_DM = [], []
    f = open(file2, 'r')
    for line in f.readlines():
        a = line.split()
        k_DM.append(float(a[0]))
        Pk_DM.append(float(a[1]))
    f.close()
    k_DM = np.array(k_DM)
    Pk_DM = np.array(Pk_DM)

    #check that k-arrays are the same
    if np.any(k_cross != k_DM):
        print 'k arrays are different'
        sys.exit()

    #compute the bias
    b = Pk_cross / Pk_DM

    #count the number of modes in the k-bins
    bins_r = int(np.sqrt(3 * int(0.5 * (dims + 1))**2)) + 1
    [array, k] = PSL.CIC_correction(dims)
    count = PSL.lin_histogram(bins_r, 0.0, bins_r * 1.0, k)[1:]

    #compute the error on the bias
    db = 1.0 / np.sqrt(n_halos * count * Pk_DM)

    #write output file
    f = open(f_out, 'w')
    for i in range(len(k_DM)):
        f.write(str(k_DM[i]) + ' ' + str(b[i]) + ' ' + str(db[i]) + '\n')
    f.close()
Exemplo n.º 2
0
Omega_CDM = Nall[1]*Masses[1]/BoxSize**3/rho_crit
Omega_NU  = Nall[2]*Masses[2]/BoxSize**3/rho_crit
print 'Omega_m   = %.4f'%Omega_m
print 'Omega_CDM = %.4f'%Omega_CDM
print 'Omega_NU  = %.4f'%Omega_NU
print 'Omega_b   = %.4f'%(Omega_m-Omega_CDM-Omega_NU)

#compute the mean mass per cell
mean_mass = np.sum(mass,dtype=np.float64)/dims**3

#compute the mass within each cell
delta_m = np.zeros(dims**3,dtype=np.float32)
CIC.CIC_serial(pos,dims,BoxSize,delta_m,weights=mass)
print '%.6e should be equal to \n%.6e\n'\
    %(np.sum(delta_m,dtype=np.float64),np.sum(mass,dtype=np.float64))
delta_m = delta_m/mean_mass
print np.min(delta_m),'< delta_m <',np.max(delta_m)
#################################################################################



#compute P_HI(k), P_m(k), P_HI-m(k)
[k,Pk_cross,Pk_HI,Pk_m] = PSL.cross_power_spectrum_given_delta(delta_HI,delta_m,
                               dims,BoxSize,aliasing_method1='CIC',
                                            aliasing_method2='CIC')
                                                               
#save results to file
np.savetxt(f_Pk_HI,    np.transpose([k,Pk_HI]))
np.savetxt(f_Pk_m,     np.transpose([k,Pk_m]))
np.savetxt(f_Pk_cross, np.transpose([k,Pk_cross]))
Exemplo n.º 3
0
    RSD(pos[:, axis], vel[:, axis], Hubble, redshift)

#read the masses of all the particles
M = readsnap.read_block(snapshot_fname, "MASS", parttype=-1) * 1e10  #Msun/h
print '%.3e < M [Msun/h] < %.3e' % (np.min(M), np.max(M))
print 'Omega_m = %.3f\n' % (np.sum(M, dtype=np.float64) / rho_crit /
                            BoxSize**3)

#compute the mean mass per grid cell
mean_M = np.sum(M, dtype=np.float64) / dims**3

#compute the mass within each grid cell
delta = np.zeros(dims**3, dtype=np.float32)
CIC.CIC_serial(pos, dims, BoxSize, delta, M)
del pos
print '%.6e should be equal to \n%.6e\n'\
    %(np.sum(M,dtype=np.float64),np.sum(delta,dtype=np.float64))
del M

#compute the density constrast within each grid cell
#delta=delta/mean_M - 1.0
delta /= mean_M
delta -= 1.0
print '%.3e < delta < %.3e\n' % (np.min(delta), np.max(delta))

#compute the P(k)
Pk = PSL.power_spectrum_given_delta(delta, dims, BoxSize)

#write P(k) to output file
np.savetxt(f_out, np.transpose([Pk[0], Pk[1]]))
    pos_nu  = readsnap.read_block(snapshot_fname,"POS ",parttype=2)/1e3  #Mpc/h
    mass_nu = readsnap.read_block(snapshot_fname,"MASS",parttype=2)*1e10 #Msun/h
    print 'Omega_NU  = %.4f'\
        %(np.sum(mass_nu,dtype=np.float64)/BoxSize**3/rho_crit)
    pos  = np.vstack([pos,pos_nu]);    del pos_nu
    mass = np.hstack([mass,mass_nu]);  del mass_nu
    if do_RSD:
        vel_nu = readsnap.read_block(snapshot_fname,"VEL ",parttype=2) #km/s
        vel    = np.vstack([vel,vel_nu]);  del vel_nu
print 'Omega_m   = %.4f'%(np.sum(mass,dtype=np.float64)/BoxSize**3/rho_crit)

if do_RSD:
    RSL.pos_redshift_space(pos,vel,BoxSize,Hubble,redshift,axis)

#compute the mean mass in each cell
mean_mass = np.sum(mass,dtype=np.float64)*1.0/dims3

#compute the deltas
CIC.CIC_serial(pos,dims,BoxSize,delta,mass)
print '%.6e should be equal to\n%.6e'\
    %(np.sum(delta,dtype=np.float64),np.sum(mass,dtype=np.float64))
del pos,mass
delta = delta/mean_mass - 1.0
print '%.6e should be close to 0'%np.mean(delta,dtype=np.float64)

#compute the correlation function
r,xi = PSL.CF_Taruya(delta,dims,BoxSize,bins_CF,MAS)

#save results to file
np.savetxt(fout,np.transpose([r,xi]))
Rmax = 500.0  #Mpc/h
bins = 1000
binning = 'log'  #choose between 'linear' and 'log'

Omega_CDM = None  #set only to compute the CDM+B xi(r)
Omega_B = None  #set only to compute the CDM+B xi(r)
ns = None  #set only to extrapole the P(k) on very large scales

#choose one or several from: 'm','c','b','n','cb','c-b','c-n','b-n','cb-n'
object_type = ['m']

fout = ['CF_m_z=0.dat']
##############################################################################

#compute the different power spectra from the CAMB files
CAMB = PSL.CAMB_Pk(f_Pk, f_transfer, Omega_CDM, Omega_B)

#dictionary to read the different power spectra
k = CAMB.k
Pk_dict = {
    'm': CAMB.Pk_m,
    'c': CAMB.Pk_c,
    'b': CAMB.Pk_b,
    'n': CAMB.Pk_n,
    'c-b': CAMB.Pk_x_c_b,
    'c-n': CAMB.Pk_x_c_n,
    'b-n': CAMB.Pk_x_b_n
}

if Omega_CDM != None and Omega_B != None:
    Pk_dict.update({'cb': CAMB.Pk_cb, 'cb-n': CAMB.Pk_x_cb_n})
Exemplo n.º 6
0
def compute_Pk(snapshot_fname,dims,do_RSD,axis,hydro):

    # read snapshot head and obtain BoxSize, Omega_m and Omega_L
    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)  #h*km/s/Mpc
    h        = head.hubble

    z = '%.3f'%redshift
    f_out = 'Pk_m_z='+z+'.dat'

    # compute the values of Omega_CDM and Omega_B
    Omega_cdm = Nall[1]*Masses[1]/BoxSize**3/rho_crit
    Omega_nu  = Nall[2]*Masses[2]/BoxSize**3/rho_crit
    Omega_b   = Omega_m-Omega_cdm-Omega_nu
    print '\nOmega_CDM = %.4f\nOmega_B   = %0.4f\nOmega_NU  = %.4f'\
        %(Omega_cdm,Omega_b,Omega_nu)
    print 'Omega_M   = %.4f'%(Omega_m)

    # read the positions of all the particles
    pos = readsnap.read_block(snapshot_fname,"POS ",parttype=-1)/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]))

    if do_RSD:
        print 'moving particles to redshift-space'
        # read the velocities of all the particles
        vel = readsnap.read_block(snapshot_fname,"VEL ",parttype=-1) #km/s
        RSL.pos_redshift_space(pos,vel,BoxSize,Hubble,redshift,axis);  del vel

    # read the masses of all the particles
    if not(hydro):
        Ntotal = np.sum(Nall,dtype=np.int64)   #compute the number of particles
        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 #Msun/h
    print '%.3e < M [Msun/h] < %.3e'%(np.min(M),np.max(M))
    print 'Omega_M = %.4f\n'%(np.sum(M,dtype=np.float64)/rho_crit/BoxSize**3)

    # compute the mean mass per grid cell
    mean_M = np.sum(M,dtype=np.float64)/dims**3

    # compute the mass within each grid cell
    delta = np.zeros(dims**3,dtype=np.float32)
    CIC.CIC_serial(pos,dims,BoxSize,delta,M); del pos
    print '%.6e should be equal to \n%.6e\n'\
        %(np.sum(M,dtype=np.float64),np.sum(delta,dtype=np.float64)); del M

    # compute the density constrast within each grid cell
    delta/=mean_M; delta-=1.0
    print '%.3e < delta < %.3e\n'%(np.min(delta),np.max(delta))

    # compute the P(k)
    Pk = PSL.power_spectrum_given_delta(delta,dims,BoxSize)

    # write P(k) to output file
    np.savetxt(f_out,np.transpose([Pk[0],Pk[1]]))
Exemplo n.º 7
0
        CIC.CIC_serial(pos_RSD,dims,BoxSize,M_HI_grid,M_HI); del pos_RSD
    else:
        CIC.CIC_serial(pos,dims,BoxSize,M_HI_grid,M_HI); del pos
    print 'Omega_HI = %e'\
        %(np.sum(M_HI_grid,dtype=np.float64)/BoxSize**3/rho_crit)

    #we assume that Ts>>T_CMB
    delta_Tb = mean_delta_Tb*M_HI_grid/mean_M_HI
    #delta_Tb = M_HI_grid/mean_M_HI# - 1
    print 'delta_Tb [mK] =',delta_Tb
    print '%.4f < delta_Tb [mK] < %.4f'%(np.min(delta_Tb),np.max(delta_Tb))
    print '<delta_Tb> = %.4e'%np.mean(delta_Tb)

    #compute 21 cm P(k)
    print '\nComputing monopole'
    Pk=PSL.power_spectrum_given_delta(delta_Tb,dims,BoxSize)
    Pk_axis.append(Pk[1])

    #write P(k) to output file
    #np.savetxt(f_out[axis],np.transpose([Pk[0],Pk[1]]))
    
    if do_RSD:

        #compute P(k,\mu)
        #k2D,mu2D,Pk2D,modes2D=PSL.power_spectrum_2D(delta_Tb,dims,BoxSize,axis,bins_mu,
        #                                            do_k_mu=True,aliasing_method='CIC')
        #f=open('Pk_2D_axis='+str(axis)+'.dat','w')
        #for i in xrange(len(k2D)-1):
        #    f.write(str(k2D[i])+' '+str(k2D[i+1])+' ')
        #    for j in xrange(len(mu2D)-1):
        #        f.write(str(Pk2D[i,j])+' ')
Exemplo n.º 8
0
    print 'moving particles to redshift-space'
    RSD(pos[:,axis],vel[:,axis],Hubble,redshift)

#read the masses of all the particles
M=readsnap.read_block(snapshot_fname,"MASS",parttype=-1)*1e10 #Msun/h
print '%.3e < M [Msun/h] < %.3e'%(np.min(M),np.max(M))
print 'Omega_m = %.3f\n'%(np.sum(M,dtype=np.float64)/rho_crit/BoxSize**3)

#compute the mean mass per grid cell
mean_M=np.sum(M,dtype=np.float64)/dims**3

#compute the mass within each grid cell
delta=np.zeros(dims**3,dtype=np.float32)
CIC.CIC_serial(pos,dims,BoxSize,delta,M); del pos
print '%.6e should be equal to \n%.6e\n'\
    %(np.sum(M,dtype=np.float64),np.sum(delta,dtype=np.float64)); del M

#compute the density constrast within each grid cell
#delta=delta/mean_M - 1.0
delta/=mean_M; delta-=1.0
print '%.3e < delta < %.3e\n'%(np.min(delta),np.max(delta))

#compute the P(k)
Pk=PSL.power_spectrum_given_delta(delta,dims,BoxSize)

#write P(k) to output file
np.savetxt(f_out,np.transpose([Pk[0],Pk[1]]))



    'CAMB_TABLES/ics_matterpow_20.dat', 'CAMB_TABLES/ics_matterpow_99.dat'
]

f_outs = [
    'expected_Pk_z=0.txt', 'expected_Pk_z=0.5.txt', 'expected_Pk_z=1.txt',
    'expected_Pk_z=2.txt', 'expected_Pk_z=5.txt', 'expected_Pk_z=9.txt',
    'expected_Pk_z=20.txt', 'expected_Pk_z=99.txt'
]
#############################################

BoxSize = 1000.0  #Mpc/h
dims = 1024
#############################################################################

# compute the value of |k| in each cell of the grid
[array, k] = PSL.CIC_correction(dims)
del array

if obj_type == 'Pk':

    # here k is in dimensionless units: k=0,1,sqrt(2),sqrt(3),2....
    bins_r = int(np.sqrt(3 * int(0.5 * (dims + 1))**2)) + 1
    k_N = np.pi * dims / BoxSize  #Nyquist frequency

    # count modes
    count = PSL.lin_histogram(bins_r, 0.0, bins_r * 1.0, k)

    # define k-binning and value of k in it and give physical units
    bins_k = np.linspace(0.0, bins_r, bins_r + 1)
    k = k.astype(np.float64)  #to avoid problems with np.histogram
    k_bin = np.histogram(k, bins_k,
if do_lognormal:
    sigma_G = np.sqrt(np.mean(delta_r**2,dtype=np.float64))
    print 'sigma_G =',sigma_G
    delta_r = np.exp(delta_r-sigma_G**2/2.0) - 1.0
    print '<delta_NL(r)> =',np.mean(delta_r,dtype=np.float64)
    print np.min(delta_r),'< delta_NL(r) <',np.max(delta_r)


#save density and velocity field to file
data = np.hstack([delta_r,Vx_r,Vy_r,Vz_r])
f=open(fout_df,'wb'); f.write(data);  f.close(); del data


############ Final checks ###########
#Compute the power spectrum of the density field 
[k,Pk]=PSL.power_spectrum_given_delta(delta_r,dims,BoxSize,
                                      aliasing_method='None')
np.savetxt(Pk_df,np.transpose([k,Pk]))

#Compute the correlation function of the density field
r,xi = PSL.CF_Taruya(delta_r,dims,BoxSize)
np.savetxt(CF_df,np.transpose([r,xi]));  del delta_r

"""
#compute the power spectrum of the theta=i\grad V
Vx_r = np.reshape(Vx_r,(dims,dims,dims))
Vy_r = np.reshape(Vy_r,(dims,dims,dims))
Vz_r = np.reshape(Vz_r,(dims,dims,dims))

Vx_k = scipy.fftpack.ifftn(Vx_r,overwrite_x=True); del Vx_r
Vy_k = scipy.fftpack.ifftn(Vy_r,overwrite_x=True); del Vy_r
Vz_k = scipy.fftpack.ifftn(Vz_r,overwrite_x=True); del Vz_r
    print 'sigma_G =', sigma_G
    delta_r = np.exp(delta_r - sigma_G**2 / 2.0) - 1.0
    print '<delta_NL(r)> =', np.mean(delta_r, dtype=np.float64)
    print np.min(delta_r), '< delta_NL(r) <', np.max(delta_r)

#save density and velocity field to file
data = np.hstack([delta_r, Vx_r, Vy_r, Vz_r])
f = open(fout_df, 'wb')
f.write(data)
f.close()
del data

############ Final checks ###########
#Compute the power spectrum of the density field
[k, Pk] = PSL.power_spectrum_given_delta(delta_r,
                                         dims,
                                         BoxSize,
                                         aliasing_method='None')
np.savetxt(Pk_df, np.transpose([k, Pk]))

#Compute the correlation function of the density field
r, xi = PSL.CF_Taruya(delta_r, dims, BoxSize)
np.savetxt(CF_df, np.transpose([r, xi]))
del delta_r
"""
#compute the power spectrum of the theta=i\grad V
Vx_r = np.reshape(Vx_r,(dims,dims,dims))
Vy_r = np.reshape(Vy_r,(dims,dims,dims))
Vz_r = np.reshape(Vz_r,(dims,dims,dims))

Vx_k = scipy.fftpack.ifftn(Vx_r,overwrite_x=True); del Vx_r
Vy_k = scipy.fftpack.ifftn(Vy_r,overwrite_x=True); del Vy_r
    # compute the mean mass per cell
    mean_mass = np.sum(mass, dtype=np.float64) / dims**3

    # compute the mass within each cell
    delta_m = np.zeros(dims**3, dtype=np.float32)
    CIC.CIC_serial(pos_m, dims, BoxSize, delta_m, weights=mass)
    print '%.6e should be equal to \n%.6e\n'\
        %(np.sum(delta_m,dtype=np.float64),np.sum(mass,dtype=np.float64))
    delta_m = delta_m / mean_mass - 1.0
    del mass, pos_m
    print np.min(delta_m), '< delta_m <', np.max(delta_m)

    # compute P_HI(k), P_m(k), P_HI-m(k)
    [k,Pk_cross,Pk_HI,Pk_m] = \
        PSL.cross_power_spectrum_given_delta(delta_HI,delta_m,dims,BoxSize,
                                             aliasing_method1='CIC',
                                             aliasing_method2='CIC')
    del delta_HI, delta_m

    # save results to file
    np.savetxt(f_Pk_HI_real_space, np.transpose([k, Pk_HI]))
    np.savetxt(f_Pk_21cm_real_space,
               np.transpose([k, Pk_HI * mean_delta_Tb**2]))
    np.savetxt(f_Pk_matter, np.transpose([k, Pk_m]))
    np.savetxt(f_Pk_cross, np.transpose([k, Pk_cross]))
    np.savetxt(f_HI_bias, np.transpose([k, Pk_cross / Pk_m]))

if Pk_HI_real_space and not (HI_matter_bias):
    k, Pk = PSL.power_spectrum_given_delta(delta_HI, dims, BoxSize)
    np.savetxt(f_Pk_HI_real_space, np.transpose([k, Pk]))
    np.savetxt(f_Pk_21cm_real_space, np.transpose([k, Pk * mean_delta_Tb**2]))
Exemplo n.º 13
0
    # compute the mean mass per cell
    mean_mass = np.sum(mass,dtype=np.float64)/dims**3

    # compute the mass within each cell
    delta_m = np.zeros(dims**3,dtype=np.float32)
    CIC.CIC_serial(pos_m,dims,BoxSize,delta_m,weights=mass)
    print '%.6e should be equal to \n%.6e\n'\
        %(np.sum(delta_m,dtype=np.float64),np.sum(mass,dtype=np.float64))
    delta_m = delta_m/mean_mass - 1.0;  del mass,pos_m
    print np.min(delta_m),'< delta_m <',np.max(delta_m)

    # compute P_HI(k), P_m(k), P_HI-m(k)
    [k,Pk_cross,Pk_HI,Pk_m] = \
        PSL.cross_power_spectrum_given_delta(delta_HI,delta_m,dims,BoxSize,
                                             aliasing_method1='CIC',
                                             aliasing_method2='CIC')
    del delta_HI,delta_m
                                                               
    # save results to file
    np.savetxt(f_Pk_HI_real_space,   np.transpose([k,Pk_HI]))
    np.savetxt(f_Pk_21cm_real_space, np.transpose([k,Pk_HI*mean_delta_Tb**2]))
    np.savetxt(f_Pk_matter,          np.transpose([k,Pk_m]))
    np.savetxt(f_Pk_cross,           np.transpose([k,Pk_cross]))
    np.savetxt(f_HI_bias,            np.transpose([k,Pk_cross/Pk_m]))

if Pk_HI_real_space and not(HI_matter_bias): 
    k,Pk = PSL.power_spectrum_given_delta(delta_HI,dims,BoxSize)
    np.savetxt(f_Pk_HI_real_space,   np.transpose([k,Pk]))
    np.savetxt(f_Pk_21cm_real_space, np.transpose([k,Pk*mean_delta_Tb**2]))
    del delta_HI
#compute the density in the grid cells
delta = np.zeros(dims**3, dtype=np.float32)
#CIC.CIC_serial(pos,dims,BoxSize,delta) #computes the density
#CIC.NGP_serial(pos,dims,BoxSize,delta) #computes the density
CIC.SPH_gas(pos, radii, divisions, dims, BoxSize, threads, delta, weights=None)

print delta
print np.sum(delta, dtype=np.float64)
print len(pos)

#compute the mean number of particles per grid cell
mean_density = len(pos) * 1.0 / dims**3

#compute the value of delta = density / <density> - 1
delta = delta / mean_density - 1.0  #computes delta
print 'numbers should be equal:', np.sum(delta, dtype=np.float64), 0.0
print np.min(delta), '< delta <', np.max(delta)

#compute the PS
Pk = PSL.power_spectrum_given_delta(delta,
                                    dims,
                                    BoxSize,
                                    do_CIC_correction=False)

#write total HI P(k) file
f = open(f_out, 'w')
for i in range(len(Pk[0])):
    f.write(str(Pk[0][i]) + ' ' + str(Pk[1][i]) + '\n')
f.close()
Exemplo n.º 15
0
M_HI_Total=Omega_HI_ref*BoxSize**3*rho_crit 

#compute the value of Omega_HI
print 'Omega_HI (enviroment) = %e\n'\
    %(np.sum(M_HI,dtype=np.float64)/BoxSize**3/rho_crit)
    
#compute the mean neutral hydrogen mass per grid point from the expected HI
mean_M_HI=M_HI_Total/dims3
#mean_M_HI=np.sum(M_HI,dtype=np.float64)/dims3
print 'mean HI mass per grid point=',mean_M_HI,'\n'

#compute the value of delta_HI = rho_HI / <rho_HI> - 1
delta_HI=np.zeros(dims3,dtype=np.float32)
CIC.CIC_serial(pos,dims,BoxSize,delta_HI,M_HI) 
print '%e should be equal to:\n%e' %(np.sum(M_HI,dtype=np.float64),
                                     np.sum(delta_HI,dtype=np.float64))
delta_HI=delta_HI/mean_M_HI-1.0  #computes delta
print 'numbers may be equal:',np.sum(delta_HI,dtype=np.float64),0.0
print np.min(delta_HI),'< delta_HI <',np.max(delta_HI)

#compute the HI PS
Pk=PSL.power_spectrum_given_delta(delta_HI,dims,BoxSize,do_CIC_correction=True)

#write total HI P(k) file
f=open(f_out,'w')
for i in range(len(Pk[0])):
    f.write(str(Pk[0][i])+' '+str(Pk[1][i])+'\n')
f.close()


Exemplo n.º 16
0
def compute_Pk(snapshot_fname, dims, do_RSD, axis, hydro):

    # read snapshot head and obtain BoxSize, Omega_m and Omega_L
    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)  #h*km/s/Mpc
    h = head.hubble

    z = '%.3f' % redshift
    f_out = 'Pk_m_z=' + z + '.dat'

    # compute the values of Omega_CDM and Omega_B
    Omega_cdm = Nall[1] * Masses[1] / BoxSize**3 / rho_crit
    Omega_nu = Nall[2] * Masses[2] / BoxSize**3 / rho_crit
    Omega_b = Omega_m - Omega_cdm - Omega_nu
    print '\nOmega_CDM = %.4f\nOmega_B   = %0.4f\nOmega_NU  = %.4f'\
        %(Omega_cdm,Omega_b,Omega_nu)
    print 'Omega_M   = %.4f' % (Omega_m)

    # read the positions of all the particles
    pos = readsnap.read_block(snapshot_fname, "POS ",
                              parttype=-1) / 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]))

    if do_RSD:
        print 'moving particles to redshift-space'
        # read the velocities of all the particles
        vel = readsnap.read_block(snapshot_fname, "VEL ", parttype=-1)  #km/s
        RSL.pos_redshift_space(pos, vel, BoxSize, Hubble, redshift, axis)
        del vel

    # read the masses of all the particles
    if not (hydro):
        Ntotal = np.sum(Nall, dtype=np.int64)  #compute the number of particles
        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  #Msun/h
    print '%.3e < M [Msun/h] < %.3e' % (np.min(M), np.max(M))
    print 'Omega_M = %.4f\n' % (np.sum(M, dtype=np.float64) / rho_crit /
                                BoxSize**3)

    # compute the mean mass per grid cell
    mean_M = np.sum(M, dtype=np.float64) / dims**3

    # compute the mass within each grid cell
    delta = np.zeros(dims**3, dtype=np.float32)
    CIC.CIC_serial(pos, dims, BoxSize, delta, M)
    del pos
    print '%.6e should be equal to \n%.6e\n'\
        %(np.sum(M,dtype=np.float64),np.sum(delta,dtype=np.float64))
    del M

    # compute the density constrast within each grid cell
    delta /= mean_M
    delta -= 1.0
    print '%.3e < delta < %.3e\n' % (np.min(delta), np.max(delta))

    # compute the P(k)
    Pk = PSL.power_spectrum_given_delta(delta, dims, BoxSize)

    # write P(k) to output file
    np.savetxt(f_out, np.transpose([Pk[0], Pk[1]]))
Exemplo n.º 17
0
    #compute the density constrast within each grid cell
    delta[index] = delta[index] * dims3 * 1.0 / len(pos) - 1.0
    del pos
    print '%.3e < delta < %.3e\n' % (np.min(delta[index]), np.max(
        delta[index]))
########################################################################

########################################################################
#compute the auto-power spectrum when there is only one component
if len(particle_type) == 1:

    ptype = particle_type[0]
    index = index_dict[ptype]
    fout = 'Pk_' + root_fout[str(ptype)] + '_z=' + z + '.dat'
    print '\nComputing the power spectrum of the particle type: ', ptype
    data = PSL.power_spectrum_given_delta(delta[index], dims, BoxSize)
    k = data[0]
    Pk[index][index] = data[1]
    del data
    np.savetxt(fout, np.transpose([k, Pk[index][index]]))
    print '\n'
    sys.exit()
########################################################################

########################################################################
#if there are two or more particles compute auto- and cross-power spectra
for i, ptype1 in enumerate(particle_type):
    for ptype2 in particle_type[i + 1:]:

        #find the indexes of the particle types
        index1 = index_dict[ptype1]