Beispiel #1
0
def randoms(ns):
    zmin = 0
    zmax = ns.zmax

    dNdZ = read_Nz(ns.nz, ns.ncol, zmin, zmax)

    zedges = numpy.linspace(zmin, zmax, 12)
    zcenters = 0.5 * (zedges[1:] + zedges[:-1])

    dNdZ_ran = lambda z: dNdZ(z) * ns.boost

    icdf, Ntotal = geticdf(dNdZ_ran, numpy.linspace(zmin, zmax, 1024))

    # drop about 20% of samples to give it enough freedom.
    cat = RandomCatalog(csize=int(Ntotal), seed=SEED * 20 + 19)

    if cat.comm.rank == 0:
        cat.logger.info("Total = %d" % Ntotal)

    # generate points uniformly on a sphere.
    # FIXME: add uniform sphere to nbodykit's RandomCatalog
    z = cat.rng.uniform(-1., 1.)
    r = abs(1 - z**2)**0.5
    phi = cat.rng.uniform(0, 2 * numpy.pi)

    x = da.cos(phi) * r
    y = da.sin(phi) * r
    z = z

    ra, dec = transform.CartesianToEquatorial(da.stack((x, y, z), axis=1),
                                              frame='galactic')

    z = icdf(cat.rng.uniform(0, 1))

    cat['RA'] = ra.compute()
    cat['DEC'] = dec.compute()
    cat['Z'] = z
    cat['Aemit'] = (1 / (z + 1))

    ntarget = numpy.ones_like(z, dtype='i4')

    randoms = ArrayCatalog(
        {
            'RA': cat['RA'].compute().repeat(ntarget, axis=0),
            'DEC': cat['DEC'].compute().repeat(ntarget, axis=0),
            'Z': cat['Z'].compute().repeat(ntarget, axis=0),
            'Aemit': cat['Aemit'].compute().repeat(ntarget, axis=0),
        },
        comm=cat.comm)
    randoms = randoms.sort(('Aemit', ), usecols=['RA', 'DEC', 'Z', 'Aemit'])

    randoms.save(ns.output, dataset=ns.odataset)
Beispiel #2
0
def make_galcat(aa, mmin, m1, alpha=0.9, censuff=None, satsuff=None, ofolder=None, seed=3333):
    '''Assign 0s to 
    '''
    zz = atoz(aa)
    #halocat = readincatalog(aa)
    halocat = BigFileCatalog(dpath + '/fastpm_%0.4f/'%aa, dataset='LL-0.200')
    rank = halocat.comm.rank

    halocat.attrs['BoxSize'] = np.broadcast_to(halocat.attrs['BoxSize'], 3)

    ghid = halocat.Index.compute()
    halocat['GlobalIndex'] = ghid
    mp = halocat.attrs['MassTable'][1]*1e10
    halocat['Mass'] = halocat['Length'] * mp
    halocat['Position'] = halocat['Position']%bs # Wrapping positions assuming periodic boundary conditions
    rank = halocat.comm.rank
    
    halocat = halocat.to_subvolumes()

    if rank == 0: print('\n ############## Redshift = %0.2f ############## \n'%zz)

    hmass = halocat['Mass'].compute()
    hpos = halocat['Position'].compute()
    hvel = halocat['Velocity'].compute()
    rvir = HaloRadius(hmass, cosmo, 1/aa-1).compute()/aa
    vdisp = HaloVelocityDispersion(hmass, cosmo, 1/aa-1).compute()
    ghid = halocat['GlobalIndex'].compute()

    # Select halos that have galaxies
    rands = np.random.uniform(size=len(hmass))
    ncen = hod.ncen(mh=hmass,mcutc=mmin,sigma=0.2)
    ws = (rands < ncen)
    
    hmass = hmass[ws]
    hpos = hpos[ws]
    hvel = hvel[ws]
    rvir = rvir[ws]
    vdisp = vdisp[ws]
    ghid = ghid[ws]
    
    print('In rank = %d, Catalog size = '%rank, hmass.size)
    #Do hod    
    start = time()
    ncen = np.ones_like(hmass)
    #nsat = hod.nsat_martin(msat = mmin, mh=hmass, m1f=m1f, alpha=alpha).astype(int)
    nsat = hod.nsat_zheng(mh=hmass, m0=mmin, m1=20*mmin, alpha=alpha).astype(int)
    
    #Centrals
    cpos, cvel, gchid, chid = hpos, hvel, ghid, np.arange(ncen.size)
    spos, svel, shid = hod.mksat(nsat, pos=hpos, vel=hvel, 
                                 vdisp=vdisp, conc=7, rvir=rvir, vsat=0.5, seed=seed)
    gshid = ghid[shid]
    svelh1 = svel*2/3 + cvel[shid]/3.

    smmax = hmass[shid]/10.
    smmin = np.ones_like(smmax)*mmin
    mask = smmin > smmax/3. #Some fudge that should be discussed
    smmin[mask] = smmax[mask]/3.
    smass = hod.get_msat(hmass[shid], smmax, smmin, alpha)

    
    sathmass = np.zeros_like(hmass)
    tot = np.bincount(shid, smass)
    sathmass[np.unique(shid)] = tot

    cmass = hmass - sathmass    # assign remaining mass in centrals

    print('In rank = %d, Time taken = '%rank, time()-start)
    print('In rank = %d, Number of centrals & satellites = '%rank, ncen.sum(), nsat.sum())
    print('In rank = %d, Satellite occupancy: Max and mean = '%rank, nsat.max(), nsat.mean())
    #
    #Save
    cencat = ArrayCatalog({'Position':cpos, 'Velocity':cvel, 'Mass':cmass,  'GlobalID':gchid, 
                           'Nsat':nsat, 'HaloMass':hmass}, 
                          BoxSize=halocat.attrs['BoxSize'], Nmesh=halocat.attrs['NC'])
    minid, maxid = cencat['GlobalID'].compute().min(), cencat['GlobalID'].compute().max() 
    if minid < 0 or maxid < 0:
        print('before ', rank, minid, maxid)
    cencat = cencat.sort('GlobalID')
    minid, maxid = cencat['GlobalID'].compute().min(), cencat['GlobalID'].compute().max() 
    if minid < 0 or maxid < 0:
        print('after ', rank, minid, maxid)

    if censuff is not None:
        colsave = [cols for cols in cencat.columns]
        cencat.save(ofolder+'cencat'+censuff, colsave)
    

    satcat = ArrayCatalog({'Position':spos, 'Velocity':svel, 'Velocity_HI':svelh1, 'Mass':smass,  
                           'GlobalID':gshid, 'HaloMass':hmass[shid]}, 
                          BoxSize=halocat.attrs['BoxSize'], Nmesh=halocat.attrs['NC'])
    minid, maxid = satcat['GlobalID'].compute().min(), satcat['GlobalID'].compute().max() 
    if minid < 0 or maxid < 0:
        print('before ', rank, minid, maxid)
    satcat = satcat.sort('GlobalID')
    minid, maxid = satcat['GlobalID'].compute().min(), satcat['GlobalID'].compute().max() 
    if minid < 0 or maxid < 0:
        print('after ', rank, minid, maxid)

    if satsuff is not None:
        colsave = [cols for cols in satcat.columns]
        satcat.save(ofolder+'satcat'+satsuff, colsave)
Beispiel #3
0
def make_galcat(aa,
                mmin,
                m1f,
                alpha=-1,
                censuff=None,
                satsuff=None,
                ofolder=None,
                seed=3333):
    '''Assign 0s to 
    '''
    zz = 1 / aa - 1
    #halocat = readincatalog(aa)
    halocat = BigFileCatalog(args['halofilez'] % aa,
                             dataset=args['halodataset'])
    rank = halocat.comm.rank

    mpart, Lbox, rsdfac, acheck = read_conversions(args['headerfilez'] % aa)
    halocat.attrs['BoxSize'] = [bs, bs, bs]
    halocat.attrs['NC'] = nc

    ghid = halocat.Index.compute()
    halocat['GlobalIndex'] = ghid
    halocat['Mass'] = halocat['Length'] * mpart
    halocat['Position'] = halocat[
        'Position'] % bs  # Wrapping positions assuming periodic boundary conditions
    rank = halocat.comm.rank

    halocat = halocat.to_subvolumes()

    if rank == 0:
        print('\n ############## Redshift = %0.2f ############## \n' % zz)

    hmass = halocat['Mass'].compute()
    hpos = halocat['Position'].compute()
    hvel = halocat['Velocity'].compute()
    rvir = HaloRadius(hmass, cosmo, 1 / aa - 1).compute() / aa
    vdisp = HaloVelocityDispersion(hmass, cosmo, 1 / aa - 1).compute()
    ghid = halocat['GlobalIndex'].compute()

    print('In rank = %d, Catalog size = ' % rank, hmass.size)
    #Do hod
    start = time()
    ncen = np.ones_like(hmass)
    nsat = hod.nsat_martin(msat=mmin, mh=hmass, m1f=m1f,
                           alpha=alpha).astype(int)

    #Centrals
    cpos, cvel, gchid, chid = hpos, hvel, ghid, np.arange(ncen.size)
    spos, svel, shid = hod.mksat(nsat,
                                 pos=hpos,
                                 vel=hvel,
                                 vdisp=vdisp,
                                 conc=7,
                                 rvir=rvir,
                                 vsat=0.5,
                                 seed=seed)
    gshid = ghid[shid]
    svelh1 = svel * 2 / 3 + cvel[shid] / 3.

    smmax = hmass[shid] / 10.
    smmin = np.ones_like(smmax) * mmin
    mask = smmin > smmax / 3.  #If Mmin and Mmax are too close for satellites, adjust Mmin
    smmin[mask] = smmax[mask] / 3.
    smass = hod.get_msat(hmass[shid], smmax, smmin, alpha)

    sathmass = np.zeros_like(hmass)
    tot = np.bincount(shid, smass)
    sathmass[np.unique(shid)] = tot

    cmass = hmass - sathmass  # assign remaining mass in centrals

    print('In rank = %d, Time taken = ' % rank, time() - start)
    print('In rank = %d, Number of centrals & satellites = ' % rank,
          ncen.sum(), nsat.sum())
    print('In rank = %d, Satellite occupancy: Max and mean = ' % rank,
          nsat.max(), nsat.mean())
    #
    #Save
    cencat = ArrayCatalog(
        {
            'Position': cpos,
            'Velocity': cvel,
            'Mass': cmass,
            'GlobalID': gchid,
            'Nsat': nsat,
            'HaloMass': hmass
        },
        BoxSize=halocat.attrs['BoxSize'],
        Nmesh=halocat.attrs['NC'])
    minid, maxid = cencat['GlobalID'].compute().min(
    ), cencat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('before ', rank, minid, maxid)
    cencat = cencat.sort('GlobalID')
    minid, maxid = cencat['GlobalID'].compute().min(
    ), cencat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('after ', rank, minid, maxid)

    if censuff is not None:
        colsave = [cols for cols in cencat.columns]
        cencat.save(ofolder + 'cencat' + censuff, colsave)

    satcat = ArrayCatalog(
        {
            'Position': spos,
            'Velocity': svel,
            'Velocity_HI': svelh1,
            'Mass': smass,
            'GlobalID': gshid,
            'HaloMass': hmass[shid]
        },
        BoxSize=halocat.attrs['BoxSize'],
        Nmesh=halocat.attrs['NC'])
    minid, maxid = satcat['GlobalID'].compute().min(
    ), satcat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('before ', rank, minid, maxid)
    satcat = satcat.sort('GlobalID')
    minid, maxid = satcat['GlobalID'].compute().min(
    ), satcat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('after ', rank, minid, maxid)

    if satsuff is not None:
        colsave = [cols for cols in satcat.columns]
        satcat.save(ofolder + 'satcat' + satsuff, colsave)
Beispiel #4
0
def make_galcat(aa,
                nc,
                seed,
                bs=1536,
                T=40,
                B=2,
                mmin=10**12.5,
                m1=20 * 10**12.5,
                alpha=0.9,
                censuff=None,
                satsuff=None,
                simpath=sc_simpath,
                outpath=sc_outpath):
    '''Assign 0s to 
    '''

    fname = get_filename(nc, seed, T=T, B=B)
    spath = simpath + fname
    opath = outpath + fname + 'hod/'

    try:
        os.makedirs(opath)
    except:
        pass

    zz = atoz(aa)
    #halocat = readincatalog(aa)
    halocat = BigFileCatalog(spath + '/fastpm_%0.4f/' % aa, dataset='LL-0.200')
    rank = halocat.comm.rank

    halocat.attrs['BoxSize'] = np.broadcast_to(halocat.attrs['BoxSize'], 3)

    ghid = halocat.Index.compute()
    halocat['GlobalIndex'] = ghid
    mp = halocat.attrs['MassTable'][1] * 1e10
    halocat['Mass'] = halocat['Length'] * mp
    halocat['Position'] = halocat[
        'Position'] % bs  # Wrapping positions assuming periodic boundary conditions
    rank = halocat.comm.rank

    halocat = halocat.to_subvolumes()

    if rank == 0:
        print('\n ############## Redshift = %0.2f ############## \n' % zz)

    hmass = halocat['Mass'].compute()
    hpos = halocat['Position'].compute()
    hvel = halocat['Velocity'].compute()
    rvir = HaloRadius(hmass, cosmo, 1 / aa - 1).compute() / aa
    vdisp = HaloVelocityDispersion(hmass, cosmo, 1 / aa - 1).compute()
    ghid = halocat['GlobalIndex'].compute()

    # Select halos that have galaxies
    rands = np.random.uniform(size=len(hmass))
    ncen = hod.ncen(mh=hmass, mcutc=mmin, sigma=0.2)
    ws = (rands < ncen)

    hmass = hmass[ws]
    hpos = hpos[ws]
    hvel = hvel[ws]
    rvir = rvir[ws]
    vdisp = vdisp[ws]
    ghid = ghid[ws]

    print('In rank = %d, Catalog size = ' % rank, hmass.size)
    #Do hod
    start = time()
    ncen = np.ones_like(hmass)
    #nsat = hod.nsat_martin(msat = mmin, mh=hmass, m1f=m1f, alpha=alpha).astype(int)
    nsat = hod.nsat_zheng(mh=hmass, m0=mmin, m1=20 * mmin,
                          alpha=alpha).astype(int)

    #Centrals
    cpos, cvel, gchid, chid = hpos, hvel, ghid, np.arange(ncen.size)
    spos, svel, shid = hod.mksat(nsat,
                                 pos=hpos,
                                 vel=hvel,
                                 vdisp=vdisp,
                                 conc=7,
                                 rvir=rvir,
                                 vsat=0.5,
                                 seed=seed)
    gshid = ghid[shid]

    print('In rank = %d, Time taken = ' % rank, time() - start)
    print('In rank = %d, Number of centrals & satellites = ' % rank,
          ncen.sum(), nsat.sum())
    print('In rank = %d, Satellite occupancy: Max and mean = ' % rank,
          nsat.max(), nsat.mean())
    #
    #Save
    cencat = ArrayCatalog({
        'Position': cpos,
        'GlobalID': gchid,
        'Nsat': nsat
    },
                          BoxSize=halocat.attrs['BoxSize'],
                          Nmesh=halocat.attrs['NC'])
    minid, maxid = cencat['GlobalID'].compute().min(
    ), cencat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('before ', rank, minid, maxid)
    cencat = cencat.sort('GlobalID')
    minid, maxid = cencat['GlobalID'].compute().min(
    ), cencat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('after ', rank, minid, maxid)

    if censuff is not None:
        colsave = [cols for cols in cencat.columns]
        print("Saving centrals.")
        cencat.save(opath + 'cencat' + censuff, colsave)

    #satcat = ArrayCatalog({'Position':spos, 'Velocity':svel, 'Velocity_HI':svelh1, 'Mass':smass,
    #  'GlobalID':gshid, 'HaloMass':hmass[shid]},
    # BoxSize=halocat.attrs['BoxSize'], Nmesh=halocat.attrs['NC'])
    satcat = ArrayCatalog({
        'Position': spos,
        'GlobalID': gshid
    },
                          BoxSize=halocat.attrs['BoxSize'],
                          Nmesh=halocat.attrs['NC'])
    minid, maxid = satcat['GlobalID'].compute().min(
    ), satcat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('before ', rank, minid, maxid)
    satcat = satcat.sort('GlobalID')
    minid, maxid = satcat['GlobalID'].compute().min(
    ), satcat['GlobalID'].compute().max()
    if minid < 0 or maxid < 0:
        print('after ', rank, minid, maxid)

    if satsuff is not None:
        colsave = [cols for cols in satcat.columns]
        print("Saving sats.")
        satcat.save(opath + 'satcat' + satsuff, colsave)