def calc_power(mesh,
               second=None,
               mode='1d',
               k_bin_width=1.0,
               verbose=False,
               los=None,
               BoxSize=None):
    """
    Basically the same as nbodykit FFTPower, but specify a certain binning in k.
    """

    if BoxSize is None:
        BoxSize = mesh.attrs['BoxSize']
    if len(BoxSize) > 1:
        assert BoxSize[0] == BoxSize[1]
        assert BoxSize[0] == BoxSize[2]
    boxsize = BoxSize[0]
    dk = 2.0 * np.pi / boxsize * k_bin_width
    kmin = 2.0 * np.pi / boxsize / 2.0

    if mode == '1d':
        res = FFTPower(first=mesh, second=second, mode=mode, dk=dk, kmin=kmin)
    elif mode == '2d':
        res = FFTPower(first=mesh,
                       second=second,
                       mode=mode,
                       dk=dk,
                       kmin=kmin,
                       poles=[0, 2, 4],
                       Nmu=5,
                       los=los)
    else:
        raise Exception("Mode not implemented: %s" % mode)

    return res
예제 #2
0
    def calc_power(mesh, second=None, mode='1d', k_bin_width=1.0, verbose=False, los=None, poles=None):
        BoxSize = mesh.attrs['BoxSize']
        assert BoxSize[0] == BoxSize[1]
        assert BoxSize[0] == BoxSize[2]
        boxsize = BoxSize[0]
        dk = 2.0 * np.pi / boxsize * k_bin_width
        kmin = 2.0 * np.pi / boxsize / 2.0

        if mode == '1d':
            res = FFTPower(first=mesh,
                            second=second,
                            mode=mode,
                            dk=dk,
                            kmin=kmin)
        elif mode == '2d':
            if poles is None:
                poles = [0,2,4]
            res = FFTPower(first=mesh,
                                second=second,
                                mode=mode,
                                dk=dk,
                                kmin=kmin,
                                poles=poles,
                                Nmu=5,
                                los=los)
        else:
            raise Exception("Mode not implemented: %s" % mode)

        return res
예제 #3
0
def test_solve_fastpm_linear_growth():
    pm = ParticleMesh(BoxSize=1024.0, Nmesh=(128, 128, 128), dtype='f8')

    engine = FastPMEngine(pm)

    code = CodeSegment(engine)
    code.create_linear_field(whitenoise='whitenoise',
                             powerspectrum=pk,
                             dlinear_k='dlinear_k')
    code.solve_fastpm(pt=pt,
                      dlinear_k='dlinear_k',
                      asteps=[0.1, 0.5, 1.0],
                      s='s',
                      v='v',
                      s1='s1',
                      s2='s2')
    code.get_x(s='s', x='x')
    code.paint_simple(x='x', density='density')
    field = pm.generate_whitenoise(seed=1234, unitary=True).c2r()

    density, dlinear_k, s = code.compute(['density', 'dlinear_k', 's'],
                                         init={'whitenoise': field})
    density_k = density.r2c()
    p_lin = FFTPower(FieldMesh(dlinear_k), mode='1d')
    p_nonlin = FFTPower(FieldMesh(density), mode='1d')

    # the simulation shall do a linear growth
    t1 = abs((p_nonlin.power['power'] / p_lin.power['power'])**0.5)
    assert_allclose(t1[1:4], 1.0, rtol=5e-2)
예제 #4
0
def AnalyzeCatalog(cat):

    #line_of_sight = [0,0,1]
    #cat['RSDPosition'] = cat['Position'] + cat['VelocityOffset'] * line_of_sight
    #mesh = cat.to_mesh(resampler ='tsc', Nmesh=256, compensated=True, position='RSDPosition')

    mesh = cat.to_mesh(resampler='tsc', compensated=True, position='Position')
    
    r = FFTPower(mesh, mode='1d', dk=0.005, kmin=0.01)
    Pk = r.power
    print(Pk)
    
    print(Pk.coords)
    for k in Pk.attrs:
        print("%s = %s" %(k, str(Pk.attrs[k])))
        
        
    plt.switch_backend('Qt5Agg')
        
    
    #plt.loglog(Pk['k'], Pk['power'].real - Pk.attrs['shotnoise'])
    plt.loglog(Pk['k'], Pk['power'].real)

    # format the axes
    plt.xlabel(r"$k$ [$h \ \mathrm{Mpc}^{-1}$]")
    plt.ylabel(r"$P(k)$ [$h^{-3}\mathrm{Mpc}^3$]")
    plt.xlim(0.01, 0.6)
    
    plt.show()
예제 #5
0
def analyze(pm, r):
    from nbodykit.algorithms.fftpower import FFTPower
    from nbodykit.source import ArrayCatalog
    from nbodykit.source import MemoryMesh

    DataPM = numpy.empty(len(r.Q), dtype=[('Position', ('f8', 3))])
    DataPM['Position'][:] = r.Q + r.S

    Data1LPT = DataPM.copy()
    Data1LPT['Position'][:] = r.Q + r.DX1

    DataPM = ArrayCatalog(DataPM, BoxSize=pm.BoxSize, Nmesh=pm.Nmesh)
    Data1LPT = ArrayCatalog(Data1LPT, BoxSize=pm.BoxSize, Nmesh=pm.Nmesh)
    DataLinear = MemoryMesh(r.dlinear)
    r = Result()
    r.Ppm = FFTPower(DataPM, mode='1d')
    r.P1lpt = FFTPower(Data1LPT, mode='1d')
    r.Pl = FFTPower(DataLinear, mode='1d')
    return r
예제 #6
0
def foo():
    with open('./binary-example.dat', 'wb') as ff:
        pos = numpy.random.random(size=(1024, 3)) # fake Position column
        vel = numpy.random.random(size=(1024, 3)) # fake Velocity column
        pos.tofile(ff); vel.tofile(ff); ff.seek(0)

    # create the binary catalog
    f = BinaryCatalog(ff.name, [('Position', ('f8', 3)), ('Velocity', ('f8', 3))], size=1024)

    print(f)
    print("columns = ", f.columns) # default Weight,Selection also present
    print("total size = ", f.csize)
    
    ps = FFTPower(f.to_mesh(Nmesh=100, BoxSize=10, dtype='f8'), '1d')
    r = ps.run()
    print(r)
    print(r[0])
    print(r[0].shape)
    print(r[0].coords)
    print(r[0]['power'])
예제 #7
0
def calc_power(mesh, second=None, mode='1d', k_bin_width=1.0, verbose=False):
    BoxSize = mesh.attrs['BoxSize']
    assert BoxSize[0] == BoxSize[1]
    assert BoxSize[0] == BoxSize[2]
    boxsize = BoxSize[0]
    dk = 2.0 * np.pi / boxsize * k_bin_width
    kmin = 2.0 * np.pi / boxsize / 2.0

    if mode == '1d':
        res = FFTPower(first=mesh, second=second, mode=mode, dk=dk, kmin=kmin)
        if verbose and mesh.comm.rank == 0:
            print('power: ', res.power['power'])
            #print(res.attrs)
        return res
    else:
        raise Exception("Mode not implemented: %s" % mode)
예제 #8
0
def Pk(pos, mode='1d', Nmesh=None, BoxSize=None, **kwargs):
    ''' Wrapper for nbodykit.algorithms.fftpower.FFTPower in the 
    nbodykit package. Given xyz positions of objects in a **periodic
    box**, this code will calculate the powerspectrum. 
    
    pos : (3, N_particles)
    '''
    n_part = pos.shape[1]  # number of particles
    # generate CatalogSource object
    cat = CatalogSource()
    cat._size = n_part
    cat._csize = cat.comm.allreduce(cat.size)
    cat['Position'] = pos.T
    # measure powerspectrum
    pique = FFTPower(cat, mode, Nmesh=Nmesh, BoxSize=BoxSize, **kwargs)
    return pique.power
예제 #9
0
def Plk(galaxies, Lbox=1000., Ngrid=360, k_bin_width=1):
    ''' Measure the powerspectrum multipoles using the nbodykit package
   

    Parameters
    ----------
    galaxies : GalaxyCatalog object

    
    Return
    ------
    pk_moms_arr: an array containing values of k and power spectrum moments

    '''

    dk = 2.0 * np.pi / boxsize * k_bin_width
    kmin = 2.0 * np.pi / boxsize / 2.0

    # Set line of sight direction
    LOS = numpy.array([0,0,1])
    
    # paint galaxies to mesh
    delta_mesh = FieldMesh(galaxies.to_mesh(Nmesh=Ngrid, BoxSize=Lbox,
        window='cic', interlaced=False, compensated=False).compute()-1)

    #compute the power spectrum moments using nbodykit 
    if poles is None:
      poles = [0,2,4]
      pk_moms = FFTPower(first=delta_mesh,
                          second=None,
                          mode='2d',
                          dk=dk,
                          kmin=kmin,
                          poles=poles,
                          Nmu=5,
                          los=LOS)

    # if we wanted to return an array of k and pkl values, we can return PkMoms_arr
    for ell in pk_moms.attrs['poles']:
            mydtype = [('k', 'f8'), ('P', 'f8')]
            PkMoms_arr = np.empty(shape=pk_moms.poles['k'].shape, dtype=mydtype)
            PkMoms_arr['k'] = pk_moms.poles['k']
            PkMoms_arr['P'] = pk_moms.poles['power_%d'%ell].real
            
    return PkMoms_arr
예제 #10
0
def calculate_power_spectrum_of_simulation_box():

    #simulation_box_filename = "/share/testde/ucapwhi/lfi_project/runsI/run016/run.00100"
    #num_objects = 1259712000
    
    simulation_box_filename = "/share/splinter/ucapwhi/lfi_project/experiments/gpu_512_256_1000/run.00100"
    num_objects = 134217728
    
    binary_output_filename = simulation_box_filename + ".dat"
    
    if False:
        print("{} Reading file {}...".format(datetime.datetime.now().time(), simulation_box_filename))
        d = utility.read_one_box(simulation_box_filename)
        print("{} Finished reading file.".format(datetime.datetime.now().time()))
        
        e = np.column_stack((d['x']+0.5, d['y']+0.5, d['z']+0.5))
        print(e.shape[0])
        
        print("{} Writing file {}...".format(datetime.datetime.now().time(), binary_output_filename))
        # See https://nbodykit.readthedocs.io/en/latest/catalogs/reading.html#Binary-Data
        with open(binary_output_filename, 'wb') as ff:
            e.tofile(ff)
            ff.seek(0)
        print("{} Finished writing file.".format(datetime.datetime.now().time()))
    
    print("{} Reading file {}...".format(datetime.datetime.now().time(), binary_output_filename))
    f = BinaryCatalog(binary_output_filename, [('Position', ('f8', 3))], size=num_objects)
    print("{} Finished reading file.".format(datetime.datetime.now().time()))
    
    print(f)
    print("columns = ", f.columns) # default Weight,Selection also present
    print("total size = ", f.csize)
    
    print("{} About to create mesh".format(datetime.datetime.now().time()))
    mesh = f.to_mesh(Nmesh=32, BoxSize=1, dtype='f4')
    print(mesh)
    print("{} Finished creating mesh".format(datetime.datetime.now().time()))
    
    print("{} About to create FFTPower object".format(datetime.datetime.now().time()))
    r = FFTPower(mesh, mode='1d', dk=0.005, kmin=0.01)
    print("{} Finished creating FFTPower object".format(datetime.datetime.now().time()))
    
    Pk = r.power
    print(Pk)
예제 #11
0
def monitor(action, ai, ac, af, state, event):
    if pm.comm.rank == 0:
        print(state.a['S'], state.a['P'], state.a['F'], state.S[0], state.P[0],
              action, ai, ac, af)


state1 = solver.nbody(lpt.copy(), leapfrog(stages), monitor=monitor)

state2 = solver_ncdm.nbody(lpt.copy(), leapfrog(stages), monitor=monitor)

if pm.comm.rank == 0:
    print('----------------')

cat1 = ArrayCatalog({'Position': state1.X}, BoxSize=pm.BoxSize, Nmesh=pm.Nmesh)
cat2 = ArrayCatalog({'Position': state2.X}, BoxSize=pm.BoxSize, Nmesh=pm.Nmesh)
r1 = FFTPower(cat1, mode='1d')
r2 = FFTPower(cat2, mode='1d')

cat1.save('NCDM/core', ('Position', ))
cat2.save('NCDM/ncdm', ('Position', ))
r1.save('NCDM/core-power.json')
r2.save('NCDM/ncdm-power.json')

if pm.comm.rank == 0:
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg

    fig = Figure()
    canvas = FigureCanvasAgg(fig)

    ax = fig.add_subplot(111)
예제 #12
0
                      a=stages[0],
                      order=2)

state2 = solver_multi.nbody(ic.copy(), leapfrog(stages), monitor=monitor_multi)

if pm.comm.rank == 0:
    print('----------------')

cat1 = state1.to_catalog(Nmesh=pm.Nmesh)
cat2 = state2.to_catalog(Nmesh=pm.Nmesh)
cat2_cdm = state2['1'].to_catalog(Nmesh=pm.Nmesh)
cat2_b = state2['0'].to_catalog(Nmesh=pm.Nmesh)
cat2_ncdm = state2['4'].to_catalog(Nmesh=pm.Nmesh)
print(cat2.attrs)

r1 = FFTPower(cat1, mode='1d')
r2 = FFTPower(cat2, mode='1d')
r2_cdm = FFTPower(cat2_cdm, mode='1d')
r2_b = FFTPower(cat2_b, mode='1d')
r2_ncdm = FFTPower(cat2_ncdm, mode='1d')

cat1.save('MULTI/core', ('Position', ))
cat2.save('MULTI/mult', ('0/Position', '1/Position', '4/Position'))

r1.save('MULTI/core-power.json')
r2.save('MULTI/mult-power.json')

if pm.comm.rank == 0:
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg
예제 #13
0
# Project catalogue onto mesh
array_cat = ArrayCatalog({'Position': halo_cat})
#array_cat2 = ArrayCatalog({'Position': halo_cat2})
mesh_cat = array_cat.to_mesh(Nmesh=box.N,
                             BoxSize=(box.Lx, box.Ly, box.Lz),
                             window='tsc',
                             compensated=True)
#mesh_cat2 = array_cat2.to_mesh(Nmesh=box.N, BoxSize=(box.Lx, box.Ly, box.Lz),
#                               window='tsc', compensated=True)

# Put density field onto mesh
mesh_delta = ArrayMesh(box.delta_x, BoxSize=(box.Lx, box.Ly, box.Lz))
#mesh_delta = ArrayMesh(delta_ln, BoxSize=(box.Lx, box.Ly, box.Lz))

# Calculate power spectra
pspec1 = FFTPower(first=mesh_cat, mode='1d', second=None, los=[0, 0, 1], Nmu=5)
pspec1.run()
#pspec1a = FFTPower(first=mesh_cat, mode='1d', second=mesh_cat2, los=[0, 0, 1], Nmu=5)
#pspec1a.run()
pspec2 = FFTPower(first=mesh_delta,
                  mode='1d',
                  second=None,
                  los=[0, 0, 1],
                  Nmu=5)
pspec2.run()
pspec3 = FFTPower(first=mesh_delta,
                  mode='1d',
                  second=mesh_cat,
                  los=[0, 0, 1],
                  Nmu=5)
pspec3.run()
예제 #14
0
def triangle_plot(filename, cleftname, field_array, z_late):
    '''
    Makes a giant triangle plot comparing the late-time fields to a CLEFT prediction, making the measurements required
    assuming the input fields are correctly structured. 

    Input:
        -filename: directory where you want the triangle plot to be saved
        -cleftname: directory where you will pull the CLEFT predictions for comparison
        -field_array: list of the component late-time fields we will use to make comparisons.

    Output:
        -pdf plot at filename
        -Component spectra 

    Note: field_array assumes the following ordering (See Modi, Chen, White 2020 for definitions):
        delta_1, delta_delta_L, delta_delta^2, delta_s^2
    '''
    field_late, field_linlate, field_sqlate, tide_sqlate = field_array

    Lbox = field_late.BoxSize[0]
    nmesh = field_late.Nmesh[0]
    pk = np.loadtxt(cleftname)
    fig, axes = plt.subplots(figsize=(10, 10), sharex=True, ncols=4, nrows=4)
    for i in range(4):
        for j in range(4):
            if i < j:
                axes[i, j].axis('off')

    field_dict = {
        '1': field_late,
        r'$\delta_L$': field_linlate,
        r'$\delta^2$': field_sqlate,
        r'$s^2$': tide_sqlate
    }

    b1sq = FFTPower(field_dict[r'$\delta_L$'],
                    '1d',
                    second=field_dict[r'$\delta_L$'],
                    BoxSize=Lbox,
                    Nmesh=nmesh)
    b1power = b1sq.power['power'].real

    axes[0, 0].loglog(b1sq.power['k'], b1power, label=r'Nbody', lw=2)
    axes[0, 0].loglog(pk[0], pk[5], lw=2, label=r'CLEFT')
    axes[0, 0].set_title(r'$b_1^2$ component')
    axes[0, 0].set_xlim(pk[0][1], pk[0][-1])

    b2sq = FFTPower(field_dict[r'$\delta^2$'],
                    '1d',
                    second=field_dict[r'$\delta^2$'],
                    BoxSize=Lbox,
                    Nmesh=nmesh)
    b2power = b2sq.power['power'].real
    axes[1, 0].loglog(b2sq.power['k'], b2power, label=r'Nbody', lw=2)
    axes[1, 0].loglog(pk[0], pk[8], lw=2, label=r'CLEFT')
    axes[1, 0].set_title(r'$b_2^2$ component')

    b2b1 = FFTPower(field_dict[r'$\delta_L$'],
                    '1d',
                    second=field_dict[r'$\delta^2$'],
                    BoxSize=Lbox,
                    Nmesh=nmesh)
    b2b1power = b2b1.power['power'].real

    axes[1, 1].loglog(b2b1.power['k'], b2b1power, label=r'Nbody', lw=2)
    axes[1, 1].loglog(pk[0], pk[7], lw=2, label=r'CLEFT')
    axes[1, 1].set_title('$b_2b_1$ component')

    fig.suptitle(r'CLEFT v.s. ZAemulus at $z=%s$, $N_{mesh} = %s$' %
                 (z_late, nmesh),
                 y=0.92)

    bs2 = FFTPower(field_dict[r'$s^2$'],
                   '1d',
                   second=field_dict[r'$s^2$'],
                   BoxSize=Lbox,
                   Nmesh=nmesh)
    bs2power = bs2.power['power'].real
    axes[2, 0].loglog(bs2.power['k'], bs2power, label=r'Nbody', lw=2)
    axes[2, 0].loglog(pk[0], pk[12], lw=2, label=r'CLEFT')
    axes[2, 0].set_title(r'$b_s^2$ component')

    bsb1 = FFTPower(field_dict[r'$s^2$'],
                    '1d',
                    second=field_dict[r'$\delta_L$'],
                    BoxSize=Lbox,
                    Nmesh=nmesh)
    bsb1power = bsb1.power['power'].real

    axes[2, 1].loglog(bsb1.power['k'], np.abs(bsb1power), label=r'Nbody', lw=2)
    axes[2, 1].loglog(pk[0], np.abs(pk[10]), lw=2, label=r'CLEFT')
    axes[2, 1].set_title('$b_sb_1$ component')

    bsb2 = FFTPower(field_dict[r'$s^2$'],
                    '1d',
                    second=field_dict[r'$\delta^2$'],
                    BoxSize=Lbox,
                    Nmesh=nmesh)
    bsb2power = bsb2.power['power'].real

    axes[2, 2].loglog(bsb2.power['k'], np.abs(bsb2power), label=r'Nbody', lw=2)
    axes[2, 2].loglog(pk[0], np.abs(pk[11]), lw=2, label=r'CLEFT')
    axes[2, 2].set_title('$b_sb_2$ component')

    nonlinsq = FFTPower(field_dict['1'],
                        '1d',
                        second=field_dict['1'],
                        BoxSize=Lbox,
                        Nmesh=nmesh)
    nonlinsqpower = nonlinsq.power['power'].real
    axes[3, 0].loglog(nonlinsq.power['k'],
                      np.abs(nonlinsqpower),
                      label=r'Nbody',
                      lw=2)
    axes[3, 0].loglog(pk[0], pk[1] + pk[2] + pk[3], label=r'CLEFT')
    axes[3, 0].set_title('$(1,1)$ component')

    nonlinb1 = FFTPower(field_dict['1'],
                        '1d',
                        second=field_dict[r'$\delta_L$'],
                        BoxSize=Lbox,
                        Nmesh=nmesh)
    nonlinb1power = nonlinb1.power['power'].real
    axes[3, 1].loglog(nonlinb1.power['k'],
                      np.abs(nonlinb1power),
                      label=r'Nbody',
                      lw=2)
    axes[3, 1].loglog(pk[0], pk[4], label=r'CLEFT')
    axes[3, 1].set_title('$(1,b_1)$ component')

    nonlinbs = FFTPower(field_dict['1'],
                        '1d',
                        second=field_dict[r'$s^2$'],
                        BoxSize=Lbox,
                        Nmesh=nmesh)
    nonlinbspower = nonlinbs.power['power'].real
    axes[3, 2].loglog(nonlinbs.power['k'],
                      np.abs(nonlinbspower),
                      label=r'Nbody',
                      lw=2)
    axes[3, 2].loglog(pk[0], pk[9], label=r'CLEFT')
    axes[3, 2].set_title('$(1,b_s)$ component')

    nonlinb2 = FFTPower(field_dict['1'],
                        '1d',
                        second=field_dict[r'$\delta^2$'],
                        BoxSize=Lbox,
                        Nmesh=nmesh)
    nonlinb2power = nonlinb2.power['power'].real
    axes[3, 3].loglog(nonlinb2.power['k'],
                      np.abs(nonlinb2power),
                      label=r'Nbody',
                      lw=2)
    axes[3, 3].loglog(pk[0], pk[6], label=r'CLEFT')
    axes[3, 3].set_title('$(1,b_2)$ component')

    axes[0, 0].legend()
    axes[0, 0].legend()

    fig.savefig(filename, dpi=300, format='pdf')
    #Note we're assuming they all end up being measured at the same k (which is fine?)
    return [
        nonlinb2.power['k'], b1power, b2power, b2b1power, bs2power, bsb1power,
        bsb2power, nonlinsqpower, nonlinb1power, nonlinbspower, nonlinb2power
    ]
예제 #15
0
# Trim negative values
y = np.log10(1.+box.delta_x[0])
y[np.isnan(y)] = 0.
plt.matshow(y, vmin=-2., vmax=3., cmap='cividis')
plt.title("Density field")
plt.colorbar()

# Log-normal field and power spectrum
delta_log = box.lognormal(box.delta_x)
logn_k, logn_pk, logn_stddev = box.binned_power_spectrum(delta_x=delta_log)


# NBodyKit version of power spectrum
mesh = ArrayMesh(delta_log, BoxSize=(box.Lx, box.Ly, box.Lz))
fft_power = FFTPower(mesh, mode='1d', Nmesh=None, 
                     BoxSize=(box.Lx, box.Ly, box.Lz), 
                     los=[0, 0, 1], Nmu=5, dk=None, kmin=0.0, kmax=None, poles=[])
lnpk_data, _ = fft_power.run()


plt.matshow(np.log10(1.+delta_log[0]), vmin=-2., vmax=3., cmap='cividis')
plt.title("Log-normal field")
plt.colorbar()

#plt.plot(th_k, th_pk, 'k-')
#plt.plot(re_k, re_pk, 'r.')
#plt.plot(logn_k, logn_pk, 'bx')
#plt.plot(lnpk_data['k'], lnpk_data['power'], 'g+')
#plt.xscale('log')
#plt.yscale('log')
plt.show()