Пример #1
0
def shifted_ZA(p, si):
    ''' ->> shift uniform grid after obtaining '''

    _do_linear_grid_ = False

    if _do_linear_grid_:
        lgrid = np.linspace(0, p.boxsize, p.nbin, endpoint=False)
        grid = mar.meshgrid(lgrid, lgrid, lgrid)[::-1, ...]
        print 'lgrid shape:', lgrid.shape, 'meshgrid shape:', grid.shape, si.shape
    else:
        _grid_ = np.random.uniform(0., p.boxsize, 3 * p.nbin)

        grid = mar.meshgrid(_grid_[:p.nbin], _grid_[p.nbin:2 * p.nbin],
                            _grid_[2 * p.nbin:])[::-1, ...]
        #print 'lgrid shape:', gridx.shape, 'meshgrid shape:', grid.shape, si.shape

    shifted = np.zeros((3, p.nbin, p.nbin, p.nbin))

    if displace_interpolation == False:

        for i in range(3):
            shift_ = grid[i] + si[i]

            # ->> periodic boundary <<- #
            shift_[np.where(
                shift_ < 0.)] = p.boxsize + shift_[np.where(shift_ < 0.)]
            shift_[np.where(shift_ > p.boxsize)] = shift_[np.where(
                shift_ > p.boxsize)] - p.boxsize
            shifted[i] = np.copy(shift_)

            print 'shifting axis-', i, 'is done.'
    else:

        print 'si shape', si.shape,
        shifted = pmv.particle_move(
            p, np.swapaxes(grid.reshape(3, p.nbin**3), 0, 1), si)
        shifted[np.where(
            shifted < 0.)] = p.boxsize + shifted[np.where(shifted < 0.)]
        shifted[np.where(shifted > p.boxsize)] = shifted[np.where(
            shifted > p.boxsize)] - p.boxsize

    if True:
        shift_ = shifted.reshape(p.nbin, p.nbin, p.nbin, 3)
        pl.plot(shift_[:, :, 100, 1], shift_[:, :, 100, 2], 'k.')

        #pl.plot(si[1,:,:,100], si[2,:,:,100], 'k.')
        #pl.plot(grid[1,:,:,100], grid[2,:,:,100], 'r.')
        pl.show()

    return shifted
Пример #2
0
def band_power_init(bp_init_type, fname=None, **pdict):

    if bp_init_type=='from_file':
        raise Exception('from_file band_power_init() NOT supported yet.')

    # ->>  <<- #
    if bp_init_type=='internal_log':
        
        kt_min, kt_max, kt_num = pdict['kt_list_para']
        kf_min, kf_max, kf_num = pdict['kf_list_para']

        _kt_l=np.linspace(kt_min, kt_max, kt_num+1)
        _kf_l=np.linspace(kf_min, kf_max, kf_num+1)

        # ->> middle point <<- #
	kt_list=10.**((_kt_l[:-1]+_kt_l[1:])/2.)
	kf_list=10.**((_kf_l[:-1]+_kf_l[1:])/2.)

        klist=mar.meshgrid(kt_list, kf_list) 
        Dk_list=mar.meshgrid(10.**_kt_l[1:]-10.**_kt_l[:-1], \
                             10.**_kf_l[1:]-10.**_kf_l[:-1])

        klist_low=mar.meshgrid(10.**_kt_l[:-1], 10.**_kf_l[:-1])
        klist_up =mar.meshgrid(10.**_kt_l[1:],  10.**_kf_l[1:])

        sk=klist.shape
        #sdk=Dk_list.shape

	print 'Dk_list shape:', Dk_list.shape

        return klist.reshape(2,sk[1]*sk[2]), klist_low.reshape(2,sk[1]*sk[2]), \
               klist_up.reshape(2,sk[1]*sk[2]), kt_list, kf_list, \
               Dk_list.reshape(2,sk[1]*sk[2])


    if bp_init_type=='FFT':
        try:
            dmap_res=pdict['dmap_res']
	    dmap_shape=pdict['dmap_shape']
	except:
	    raise Exception('FFT band power initialization error')

        rbsize=dmap_res*np.array(dmap_shape)
        # ->> assuming full FFT instead of rfft <<- #
        kdim=np.array(dmap_shape)
        k_min=2.*np.pi/np.array(rbsize)
        k_list=helper.klist_fft(rbsize, kdim)

        # ->> 2D klist <<- #
	klist=mar.meshgrid(k_list[0], k_list[1])
	sk=klist.shape

        # ->> boundary <<- #
        klist_low=mar.meshgrid(k_list[0]-k_min[0]/2., k_list[1]-k_min[1]/2.)
        klist_up=mar.meshgrid(k_list[0]+k_min[0]/2., k_list[1]+k_min[1]/2.)

	Dk_list=None

        return klist.reshape(2,sk[1]*sk[2]), klist_low.reshape(2,sk[1]*sk[2]), \
               klist_up.reshape(2,sk[1]*sk[2]), k_list[0], k_list[1], \
               Dk_list
Пример #3
0
        pmax = p.boxsize / (int(p.nbin)) * (p.nbin - 1)
        print 'pmax=', pmax
        #->>
        pos_init = pos_init.reshape(pos.shape)
        disp = pos - pos_init

        disp[np.where(disp > pmax)] = disp[np.where(disp > pmax)] - pmax
        disp[np.where(disp < -pmax)] = disp[np.where(disp < -pmax)] + pmax

        print 'pos/pos_init/disp shape:', pos.shape, pos_init.shape, disp.shape
        print 'disp: ', disp.min(), disp.max()

        #->> another way of calculating displacement field <<- #
        lgrid = np.linspace(0, p.boxsize, p.nbin, endpoint=False)
        grid = np.rollaxis(mar.meshgrid(lgrid, lgrid, lgrid)[::-1, ...], 0, 4)
        print 'lgrid shape:', lgrid.shape, 'meshgrid shape:', grid.shape

        err = pos_init - grid
        print 'err: ', err.max(), err.min()

        disp_uni = pos - grid
        disp_uni[np.where(
            disp_uni > pmax)] = disp_uni[np.where(disp_uni > pmax)] - pmax
        disp_uni[np.where(
            disp_uni < -pmax)] = disp_uni[np.where(disp_uni < -pmax)] + pmax

        if True:
            nplots = 2
            ax = mpl.mysubplots(nplots, ncol_max=2, subp_size=5)
            n_bin = 500
Пример #4
0
    _output_bp_dcov_ = True
    if _output_bp_dcov_:
        fn_out_dcov = root + 'result/dcov_out.dat'
        fn_out_plist = root + 'result/plist.dat'

        (qe.dcov[:, :qe.m_dim[0], :qe.m_dim[1]]).tofile(fn_out_dcov)
        (pk2d.flatten()).tofile(fn_out_plist)

    print 'corf shape:', corf.shape, cor_fft.shape

    _show_ = True
    if _show_:
        _t = np.arange(qe.m_dim[0]) * qe.dmap_res[0]
        _f = np.arange(qe.m_dim[1]) * qe.dmap_res[1]

        t, f = mar.meshgrid(_t, _f)

        nplt, ncol = 2, 2
        fig,ax=mpl.mysubplots(nplt,ncol_max=ncol,subp_size=5.,\
                              gap_size=0.5,return_figure=True)

        cb1 = ax[0].pcolormesh(t, f, cor_fft, shading='gouraud')
        cb1.set_edgecolor('face')
        #pl.colorbar(cb1)

        cb2 = ax[1].pcolormesh(t, f, corf, shading='gouraud')
        cb2.set_edgecolor('face')
        #pl.colorbar(cb2)

        #cb3=ax[2].imshow(pk2d, norm=colors.LogNorm())
        #pl.colorbar(cb3)
Пример #5
0
def Poisson3d(d,
              boxsize=None,
              return_hessian=False,
              return_gradient=False,
              smooth_R=None,
              smooth_type=None):
    ''' ->> Poisson solver of given field 'd', also return gradient or hessian field <<- 
    '''
    #dk=np.fft.fftn(d)
    dk = sfft.fftn(d)

    sk = dk.shape
    ng = d.shape[0]
    ndim = len(d.shape)
    kmin = 2. * np.pi / float(boxsize)
    #kmin = 2.*np.pi/float(ng)

    #print 'kmin:', kmin

    # ->> Fourier space arguments <<- #
    '''
    kx, ky, kz=np.mgrid[0:ng, 0:ng, 0:ng].astype(float)
    ki = 2.*np.array([np.sin(kx*kmin/2.), np.sin(ky*kmin/2.), np.sin(kz*kmin/2.)])
    k2=4.*(np.sin(kx*kmin/2.)**2.+np.sin(ky*kmin/2.)**2.+np.sin(kz*kmin/2.)**2.)
    if boxsize!=None:
        k2*=(float(ng)/float(boxsize))**2.
        ki*=float(ng)/float(boxsize)
    '''

    ki_list = [
        sfft.fftfreq(sk[i], d=1. / float(sk[i])) * kmin for i in range(ndim)
    ]
    kx, ky, kz = mar.meshgrid(*ki_list)

    #ki = 2.*np.array([np.sin(kx/2.), np.sin(ky/2.), np.sin(kz/2.)])
    #k2=4.*(np.sin(kx/2.)**2.+np.sin(ky/2.)**2.+np.sin(kz/2.)**2.)

    ki = np.array([kx, ky, kz])
    k2 = ki[0]**2. + ki[1]**2. + ki[2]**2.

    print 'ki shape:', ki.shape

    # ->> if do_smoothing <<- #
    if (smooth_R != None) & (smooth_type != None):
        do_smooth = True

        if smooth_type == 'Gaussian':
            W = np.exp(-k2 * smooth_R**2. / 2.)
        else:
            raise Exception
    else:
        do_smooth = False
        W = 1.

    print 'do_smooth=', do_smooth

    # ->> Fourier transform & phi <<- #
    phik = -dk * W / k2
    phik[0, 0, 0] = 0

    # ->> get Phi <<- #
    #phi = np.fft.ifftn(phik)
    phi = sfft.ifftn(phik)

    # ->> vector & tensors <<- #
    if return_hessian == True:
        phi_ij = np.zeros([ndim, ndim] + list(d.shape))

        for i in range(ndim):
            for j in range(ndim):
                #phi_ij[i,j] = np.fft.ifftn(-phik*ki[i]*ki[j])
                phi_ij[i, j] = sfft.ifftn(-phik * ki[i] * ki[j])

    if return_gradient == True:
        phi_i = np.zeros([ndim] + list(d.shape))

        for i in range(ndim):
            #phi_i[i] = np.fft.ifftn(1.j*phik*ki[i])
            phi_i[i] = sfft.ifftn(1.j * phik * ki[i])
        #print 'phi_i real part:',np.min(phi_i[i].real) , np.max(phi_i[i].real)
        #print 'phi_i imag part:',np.min(phi_i[i].imag) , np.max(phi_i[i].imag)

    # ->> return <<- #
    if (return_hessian == False) & (return_gradient == False):
        return phi.astype(float)

    elif (return_hessian == True) & (return_gradient == False):
        return phi.astype(float), phi_ij.astype(float)

    elif (return_hessian == False) & (return_gradient == True):
        return phi.astype(float), phi_i.astype(float)

    elif (return_hessian == True) & (return_gradient == True):
        return phi.astype(float), phi_i.astype(float), phi_ij.astype(float)
Пример #6
0
def one_dim_band_power_init(bp_init_type, fname=None, **pdict):

    if bp_init_type == 'from_file':
        raise Exception('from_file band_power_init() NOT supported yet.')

    # ->>  <<- #
    if bp_init_type == 'internal_log':
        raise Exception('NOT SUPPORTED NOW YET, NEED TO MODIFY BEFORE USE.')

        kt_min, kt_max, kt_num = pdict['kt_list_para']
        kf_min, kf_max, kf_num = pdict['kf_list_para']

        _kt_l = np.linspace(kt_min, kt_max, kt_num + 1)
        _kf_l = np.linspace(kf_min, kf_max, kf_num + 1)

        # ->> middle point <<- #
        kt_list = 10.**((_kt_l[:-1] + _kt_l[1:]) / 2.)
        kf_list = 10.**((_kf_l[:-1] + _kf_l[1:]) / 2.)

        klist = mar.meshgrid(kt_list, kf_list)
        Dk_list=mar.meshgrid(10.**_kt_l[1:]-10.**_kt_l[:-1], \
                             10.**_kf_l[1:]-10.**_kf_l[:-1])

        klist_low = mar.meshgrid(10.**_kt_l[:-1], 10.**_kf_l[:-1])
        klist_up = mar.meshgrid(10.**_kt_l[1:], 10.**_kf_l[1:])

        sk = klist.shape
        #sdk=Dk_list.shape

        print 'Dk_list shape:', Dk_list.shape

        return klist.reshape(2,sk[1]*sk[2]), klist_low.reshape(2,sk[1]*sk[2]), \
               klist_up.reshape(2,sk[1]*sk[2]), kt_list, kf_list, \
               Dk_list.reshape(2,sk[1]*sk[2])

    if bp_init_type == 'FFT':
        try:
            dmap_res = pdict['dmap_res']
            dmap_shape = pdict['dmap_shape']
        except:
            raise Exception('FFT band power initialization error')

        rbsize = dmap_res * np.array(dmap_shape)
        # ->> assuming full FFT instead of rfft <<- #
        kdim = np.array(dmap_shape)
        k_min = 2. * np.pi / np.array(rbsize)

        print 'kdim', kdim

        klist = helper.klist_fft(rbsize, kdim)[0, int(kdim[0] / 2) + 1:]

        # ->> boundary <<- #
        klist_low = klist - k_min / 2.
        klist_up = klist + k_min / 2.

        #print 'klist_low', klist_low
        #print 'klist_up', klist_up

        #klist_low[0]=0.

        return klist, klist_low, klist_up
Пример #7
0
def xi(d, boxsize=1000.):

    ndim = 3

    s = d.shape
    dk = np.fft.fftn(d)

    dk2 = dk * np.conjugate(dk) / np.prod(s)
    dk2[0, 0, 0] = 0.

    _xi = np.fft.ifftn(dk2).flatten()

    # ->>
    sxi = dk2.shape
    rmin = boxsize / float(s[0])

    ri_list = [
        sfft.fftfreq(sxi[i], d=1. / float(sxi[i])) * rmin for i in range(ndim)
    ]
    ri = mar.meshgrid(*ri_list)
    r2 = ri[0]**2. + ri[1]**2. + ri[2]**2.

    _r = np.sqrt(r2).flatten()
    rmax = _r.max()
    index = np.argsort(_r)
    r = _r[index]
    _xi_ = _xi[index]

    # ->> bin edges, only linear now <<- #
    bedges = np.arange(rmin, rmax, rmin)

    #->> cuts <<- #
    cuts = np.searchsorted(r, bedges)

    numinbin = np.zeros(bedges.shape)
    xi = np.zeros(bedges.shape)
    rmean = np.zeros(bedges.shape)
    nbins = len(bedges)

    rz0 = np.ones(ri[-1].flatten().shape)[index]

    #->> average over given k amplitude <<- #
    for i in range(len(bedges) - 1):
        #->>
        if (cuts[i + 1] > cuts[i]):
            numinbin[i] = np.sum(rz0[cuts[i]:cuts[i + 1]])
            xi[i] = np.sum(rz0[cuts[i]:cuts[i + 1]] *
                           _xi_[cuts[i]:cuts[i + 1]].real)
            rmean[i] = np.sum(rz0[cuts[i]:cuts[i + 1]] *
                              r[cuts[i]:cuts[i + 1]])

    wn0 = np.where(numinbin > 0.)[0]
    xi = xi[wn0]
    rmean = rmean[wn0]
    numinbin = numinbin[wn0]
    xi /= numinbin
    rmean /= numinbin

    #xi *= boxsize**3/np.prod(np.array(s).astype(float))**2

    return rmean, xi
Пример #8
0
def pk(d, boxsize=1000., kspace='linear'):
    # ->> obtain the angular averaged power spectrum <<- #

    # ->> FFT first <<- #
    dk = np.fft.fftn(d)
    s = d.shape
    sk = dk.shape

    _dk2 = (dk * np.conjugate(dk)).flatten()  #.astype(np.float32)

    # ->>  Fourier mode <<- #
    ng = d.shape[0]
    ndim = len(d.shape)
    kmin = 2. * np.pi / float(boxsize)

    # ->> Fourier space arguments <<- #
    ki_list = [
        sfft.fftfreq(sk[i], d=1. / float(sk[i])) * kmin for i in range(ndim)
    ]
    ki = mar.meshgrid(*ki_list)
    k2 = ki[0]**2. + ki[1]**2. + ki[2]**2.

    _k = np.sqrt(k2).flatten()
    kmax = _k.max()
    index = np.argsort(_k)
    k = _k[index]
    dk2 = _dk2[index]

    # ->> k bins <<- #
    if kspace == 'log_linear':
        bin2f = 1. / 16.
        bedges = kmin * 2.**np.arange(-bin2f / 2.,
                                      np.log(kmax / kmin) / np.log(2.), bin2f)
    elif kspace == 'linear':
        bedges = np.arange(kmin, kmax, kmin)

    #print 'k-space edges', len(bedges), kmin, kmax, k[-1]

    #->> cuts <<- #
    cuts = np.searchsorted(k, bedges)

    numinbin = np.zeros(bedges.shape)
    pk = np.zeros(bedges.shape)
    kmean = np.zeros(bedges.shape)
    nbins = len(bedges)

    kz0 = np.ones(ki[-1].flatten().shape)[index]

    #->> average over given k amplitude <<- #
    for i in range(len(bedges) - 1):
        #->>
        if (cuts[i + 1] > cuts[i]):
            numinbin[i] = np.sum(kz0[cuts[i]:cuts[i + 1]])
            pk[i] = np.sum(kz0[cuts[i]:cuts[i + 1]] *
                           dk2[cuts[i]:cuts[i + 1]].real)
            kmean[i] = np.sum(kz0[cuts[i]:cuts[i + 1]] *
                              k[cuts[i]:cuts[i + 1]])

    wn0 = np.where(numinbin > 0.)[0]
    pk = pk[wn0]
    kmean = kmean[wn0]
    numinbin = numinbin[wn0]
    pk /= numinbin
    kmean /= numinbin

    pk *= boxsize**3 / np.prod(np.array(s).astype(float))**2

    return kmean, pk
Пример #9
0
def pk_rfft(d, boxsize=1000.):
    ''' ->> obtain the angular averaged power spectrum <<- 
                  Using rfft instead of fft
          !!!! It seems there're some error here !!!!
    '''

    # ->> FFT first <<- #
    dk = np.fft.rfftn(d)
    s = d.shape
    sk = dk.shape

    _dk2 = (dk * np.conjugate(dk)).flatten()  #.astype(np.float32)

    # ->>  Fourier mode <<- #
    ng = d.shape[0]
    ndim = len(d.shape)
    kmin = 2. * np.pi / float(boxsize)

    # ->> Fourier space arguments <<- #
    #kx, ky, kz=np.mgrid[0:ng, 0:ng, 0:ng/2+1].astype(float)
    #ki = 2.*np.array([np.sin(kx*kmin/2.), np.sin(ky*kmin/2.), np.sin(kz*kmin/2.)])
    #k2=4.*(np.sin(kx*kmin/2.)**2.+np.sin(ky*kmin/2.)**2.+np.sin(kz*kmin/2.)**2.)

    #ki = np.array([kx*kmin, ky*kmin, kz*kmin])
    #k2=(kx*kmin)**2.+(ky*kmin)**2.+(kz*kmin)**2.


    ki_list = [ sfft.fftfreq(sk[i],d=1./float(sk[i]))*kmin for i in range(2) ]\
              + [sfft.rfftfreq(sk[-1], d=1./float(sk[-1]))*kmin ]
    print 'kx, ky, kz max:', np.max(ki_list[0]), np.max(ki_list[1]), np.max(
        ki_list[2])

    ki = mar.meshgrid(*ki_list)
    k2 = ki[0]**2. + ki[1]**2. + ki[2]**2.

    print 'ki shape:', ki[0].shape, ki[1].shape, ki[2].shape
    print 'ki_list shape:', ki_list[0].shape, ki_list[1].shape, ki_list[
        2].shape

    _k = np.sqrt(k2).flatten()
    kmax = _k.max()
    index = np.argsort(_k)
    print 'index shape:', index.shape, _k.shape, _dk2.shape, dk.shape

    k = _k[index]
    dk2 = _dk2[index]

    print 'kmin/kmax=', kmin, kmax, k[-1]
    print 'k2.shape=', k2.shape, 'k.shape=', k.shape
    print index.shape

    # ->> k bins <<- #
    bin2f = 1. / 16.
    bedges = kmin * 2.**np.arange(-bin2f / 2.,
                                  np.log(kmax / kmin) / np.log(2.), bin2f)
    print 'k-space edges', len(bedges), kmin, bin2f, kmax, k[-1]

    #->> cuts <<- #
    cuts = np.searchsorted(k, bedges)
    #print 'error: ', (k[cuts]-bedges)/bedges

    # ->>
    numinbin = np.zeros(bedges.shape)
    pk = np.zeros(bedges.shape)
    kmean = np.zeros(bedges.shape)
    nbins = len(bedges)

    _kz0 = np.ones(ki[-1].flatten().shape)
    _kz0[np.where(ki[-1].flatten() == 0.)] -= 0.5
    kz0 = _kz0[index]

    #->> average over given k amplitude <<- #
    for i in range(len(bedges) - 1):
        #->>
        if (cuts[i + 1] > cuts[i]):
            numinbin[i] = np.sum(kz0[cuts[i]:cuts[i + 1]])
            pk[i] = np.sum(kz0[cuts[i]:cuts[i + 1]] * dk2[cuts[i]:cuts[i + 1]])
            kmean[i] = np.sum(kz0[cuts[i]:cuts[i + 1]] *
                              k[cuts[i]:cuts[i + 1]])

    #print 'numinbin:', numinbin, len(numinbin)
    #quit()

    wn0 = np.where(numinbin > 0.)[0]
    pk = pk[wn0]
    kmean = kmean[wn0]
    numinbin = numinbin[wn0]
    pk /= numinbin
    kmean /= numinbin

    #print 'wn0:', wn0

    #quit()

    pk *= boxsize**3 / np.prod(np.array(s).astype(float))**2

    return kmean, pk