コード例 #1
0
    def __init__(self, res, vel, snapnum):
        #self.res = res
        #self.vel = vel
        #self.snapnum = snapnum
        if res == "1.12Mpc":
            s_res = '112Mpc'
        elif res == "1.4Mpc":
            s_res = '14Mpc'
        if vel == "Sig0":
            s_vel = "Sig0"
        elif vel == "11.8kms":
            s_vel = '118kms'
        snapnum = int(snapnum)

        filename = "/n/hernquistfs3/mvogelsberger/GlobularClusters/InterfaceWArepo_All_" + res + '_' + vel + "/output/"
        filename2 = filename + "GasOnly_FOF"  #Used for readsubfHDF5
        ########## CHANGED FILENAME3 TO GROUPORDERED IN GAS ONLY
        filename3 = filename2 + "/snap-groupordered_" + str(snapnum).zfill(
            3)  #Used for hdf5lib, snapHDF5
        #### Not sure if this works with change but don't care about 2.8
        if res == '2.8Mpc':
            filename3 = filename + "snapdir_" + str(snapnum).zfill(
                3) + "/snap_" + str(snapnum).zfill(3)

        #Units
        GRAVITY_cgs = 6.672e-8
        UnitLength_in_cm = 3.085678e21  # code length unit in cm/h
        UnitMass_in_g = 1.989e43  # code length unit in g/h
        UnitVelocity_in_cm_per_s = 1.0e5
        UnitTime_in_s = UnitLength_in_cm / UnitVelocity_in_cm_per_s
        UnitDensity_in_cgs = UnitMass_in_g / np.power(UnitLength_in_cm, 3)
        UnitPressure_in_cgs = UnitMass_in_g / UnitLength_in_cm / np.power(
            UnitTime_in_s, 2)
        UnitEnergy_in_cgs = UnitMass_in_g * np.power(
            UnitLength_in_cm, 2) / np.power(UnitTime_in_s, 2)
        GCONST = GRAVITY_cgs / np.power(
            UnitLength_in_cm, 3) * UnitMass_in_g * np.power(UnitTime_in_s, 2)
        critical_density = 3.0 * .1 * .1 / 8.0 / np.pi / GCONST  #.1 is for 1/Mpc to 1/kpc, also in units of h^2

        header = snap.snapshot_header(filename3)
        if res == "2.8Mpc":
            fs = hdf5lib.OpenFile(filename3 + ".0.hdf5")
        else:
            fs = hdf5lib.OpenFile(filename3 + ".hdf5")
        red = hdf5lib.GetAttr(fs, "Header", "Redshift")
        atime = hdf5lib.GetAttr(fs, "Header", "Time")
        boxSize = hdf5lib.GetAttr(fs, "Header", "BoxSize")
        boxSize *= atime  #convert from ckpc/h to kpc/h
        Omega0 = hdf5lib.GetAttr(fs, "Header", "Omega0")
        OmegaLambda = hdf5lib.GetAttr(fs, "Header", "OmegaLambda")
        fs.close()
        cat = readsubfHDF5.subfind_catalog(filename2, snapnum)
        Omega_a = Omega0 / (Omega0 + OmegaLambda * atime * atime * atime)
        critical_density *= (Omega0 / Omega_a)
        r200 = cat.Group_R_Crit200
        r200 *= atime  #convert from ckpc/h to kpc/h
        m200 = cat.Group_M_Crit200
        haloCMvel = cat.GroupVel
        haloCMvel *= 1. / atime  #convert from km/s/a to km/s
        haloPos = cat.GroupPos
        haloPos *= atime  #convert from ckpc/h to kpc/h

        #Read in particles
        #read in all simulation masses to calculate cosmic baryon fraction
        massgassim = snap.read_block(filename + "snap_" +
                                     str(snapnum).zfill(3),
                                     "MASS",
                                     parttype=0)
        massdmsim = snap.read_block(filename + "snap_" + str(snapnum).zfill(3),
                                    "MASS",
                                    parttype=1)
        massgas = snap.read_block(filename3, "MASS", parttype=0)
        massdm = snap.read_block(filename3, "MASS", parttype=1)
        posgas = snap.read_block(filename3, "POS ", parttype=0)
        posdm = snap.read_block(filename3, "POS ", parttype=1)
        velgas = snap.read_block(filename3, "VEL ", parttype=0)
        veldm = snap.read_block(filename3, "VEL ", parttype=1)
        #redefine position units from ckpc/h to kpc/h
        posgas *= atime
        posdm *= atime
        #redefine velocity units from kmsqrt(a)/s to km/s
        velgas *= np.sqrt(atime)
        veldm *= np.sqrt(atime)

        fb = massgassim.sum(dtype="float64") / (
            massgassim.sum(dtype="float64") + massdmsim.sum(dtype="float64"))
        gaslimit = .4  # Set the limit for gas fraction in plots

        #boxSize hubble flow correction for halo CM velocity subtraction
        boxSizeVel = boxSize * .1 * UnitLength_in_cm / UnitVelocity_in_cm_per_s * np.sqrt(
            Omega0 / atime / atime / atime + OmegaLambda)

        #load particle indices
        pGas = snap.read_block(filename3, "POS ", parttype=0)
        mGas = snap.read_block(filename3, "MASS", parttype=0)
        pDM = snap.read_block(filename3, "POS ", parttype=1)
        halo100_indices = np.where(cat.GroupLenType[:, 0] > 100)[0]
        startAllGas = []
        endAllGas = []
        for i in halo100_indices:
            startAllGas += [np.sum(cat.GroupLenType[:i, 0])]
            endAllGas += [startAllGas[-1] + cat.GroupLenType[i, 0]]
        #Initialize arrays
        spinparam = np.zeros(np.size(halo100_indices))
        jsptotspinparam = np.zeros(np.size(halo100_indices))
        jspgasspinparam = np.zeros(np.size(halo100_indices))
        jspdmspinparam = np.zeros(np.size(halo100_indices))
        gasfrac = np.zeros(np.size(halo100_indices))
        costheta = np.zeros(np.size(halo100_indices))  #misalignment angle
        v200 = np.zeros(np.size(halo100_indices))
        velgasall = np.zeros(np.size(halo100_indices))
        veldmall = np.zeros(np.size(halo100_indices))
        virialratio = np.zeros(np.size(halo100_indices))
        numGas = np.zeros(np.size(halo100_indices))
        numDM = np.zeros(np.size(halo100_indices))

        j200gas = np.zeros(np.size(halo100_indices))
        j200dm = np.zeros(np.size(halo100_indices))
        j200 = np.zeros(np.size(halo100_indices))
        totmass = np.zeros(np.size(halo100_indices))
        gasmass = np.zeros(np.size(halo100_indices))
        DMmass = np.zeros(np.size(halo100_indices))
        rmax = np.zeros(np.size(halo100_indices))
        rmin = np.zeros(np.size(halo100_indices))
        j200gasNoNorm = np.zeros(np.size(halo100_indices))
        closestm200 = np.zeros(np.size(halo100_indices))
        #some radii are errors and  negative, will have a value of 1 to be excluded
        negradii = np.zeros(np.size(halo100_indices))

        #Indexing for global variable works because halos are ordered from largest to smallest so <100 particles are at the end and not counted.
        for i in halo100_indices:
            exec("cm = cm_%s_%s_%d[0][i]" % (s_res, s_vel, snapnum))
            exec("rotation = rotation_%s_%s_%d[0][i]" %
                 (s_res, s_vel, snapnum))
            exec("radii = radii_%s_%s_%d[0][i]" % (s_res, s_vel, snapnum))
            #some radii are errors and  negative, will have a value of 1 to be excluded
            if radii[0] < 0.:
                negradii[i] = 1.
            else:
                maxrad = radii[2]
                maxrad *= atime  #convert from ckpc to kpc
                exec("mDM=mDM_%s_%s_%d[0][i]" % (s_res, s_vel, snapnum))
                exec("DMinEll=DMindices_%s_%s_%d[0][i]" %
                     (s_res, s_vel, snapnum))
                exec("m200dm = M200dm_%s_%s[snapnum-10][i]" % (s_res, s_vel))
                #Check if CM is buggy
                if np.sum(cm == np.array([0., 0., 0.])) == 3:
                    # it's probbaly an error; recompute com
                    totalGas = np.sum(mGas[startAllGas[i]:endAllGas[i]])
                    cm = np.array([
                        np.sum(pGas[startAllGas[i]:endAllGas[i], j] *
                               mGas[startAllGas[i]:endAllGas[i]]) / totalGas
                        for j in range(3)
                    ])

                # Get positions of gas particles
                P = pGas[startAllGas[i]:endAllGas[i]]
                # Shift coordinate system to center on the center of the ellipsoid
                Precentered = dx_wrap(P - cm, boxSize / atime)
                # Rotate coordinated to the the axes point along x,y,z directions:
                Precentered = np.array(
                    [np.dot(pp, rotation.T) for pp in Precentered])
                # Figure out which particles are inside the ellipsoid
                inEll = (Precentered[:, 0]**2. / radii[0]**2. +
                         Precentered[:, 1]**2. / radii[1]**2 +
                         Precentered[:, 2]**2. / radii[2]**2) <= 1.

                #remove halo CM velocity
                tempvelgas = dx_wrap(
                    velgas[startAllGas[i]:endAllGas[i]][inEll] - haloCMvel[i],
                    boxSizeVel)
                tempveldm = dx_wrap(veldm[DMinEll] - haloCMvel[i], boxSizeVel)
                #redefine positions wrt COM
                tempposgas = dx_wrap(
                    posgas[startAllGas[i]:endAllGas[i]][inEll] - haloPos[i],
                    boxSize)
                tempposdm = dx_wrap(posdm[DMinEll] - haloPos[i], boxSize)
                numDM[i] = np.size(tempposdm)
                numGas[i] = np.size(tempposgas)
                #Calculating j200
                #j200 of all particles
                j200vecgas = np.sum(
                    np.cross(tempposgas, tempvelgas) *
                    massgas[startAllGas[i]:endAllGas[i]][inEll][:, np.newaxis],
                    axis=0)
                j200vecdm = np.sum(np.cross(tempposdm, tempveldm) *
                                   massdm[DMinEll][:, np.newaxis],
                                   axis=0)
                #if np.size(tempveldm)!=0: #can be no dm particles!
                #	costheta[i] = np.dot(j200vecgas,j200vecdm)/np.linalg.norm(j200vecgas)/np.linalg.norm(j200vecdm)
                j200vec = j200vecgas + j200vecdm
                j200[i] = np.linalg.norm(j200vec)
                j200dm[i] = np.linalg.norm(j200vecdm)

                j200gas[i] = np.linalg.norm(j200vecgas)
                j200gasNoNorm[i] = np.linalg.norm(j200vecgas)
                gasmass[i] = np.sum(
                    massgas[startAllGas[i]:endAllGas[i]][inEll])
                totmass[i] = gasmass[i] + mDM
                DMmass[i] = mDM
                rmax[i] = radii[2]
                rmin[i] = radii[0]
                closestm200[i] = m200dm
                #using fudicial m200~6mgas
                #get r200 from analytic formula in Barkana,Loeb 01 review
                if gasmass[i] != 0.:  #Some ellpsoids fit nothing
                    m200fid = 6. * gasmass[i]
                    omgz = .27 * atime**(-3.) / (.27 * atime**(-3.) + .73)
                    dfact = omgz - 1.
                    delc = 18. * np.pi**2. + 82 * dfact - 39. * dfact**2.
                    r200fid = .784 * (m200fid * 100.)**(1. / 3.) * (
                        .27 / omgz * delc / 18. / np.pi**2)**(-1. /
                                                              3.) * 10 * atime
                    v200fid = np.sqrt(GCONST * (m200fid) / r200fid)
                    j200gas[i] *= 1. / np.sqrt(2) / (
                        gasmass[i]) / v200fid / r200fid
                    j200[i] *= 1. / np.sqrt(2) / (
                        totmass[i]) / v200fid / r200fid
                    if mDM != 0.:
                        j200dm[i] *= 1. / np.sqrt(2) / mDM / v200fid / r200fid
                    gasfrac[i] = gasmass[i] / totmass[i]

        #Reindex to account for shrunken ellipsoids with gas particles >100

        goodidx, = np.where(np.logical_and(numGas > 100, negradii == 0.))

        self.j200gas = j200gas[goodidx]
        self.j200dm = j200dm[goodidx]
        self.j200 = j200[goodidx]
        self.j200gasNoNorm = j200gasNoNorm[goodidx]
        self.gasfrac = gasfrac[goodidx]
        self.totmass = totmass[goodidx]
        self.totmass *= 10**10
        #costheta = costheta[goodidx]
        self.rmax = rmax[goodidx]
        self.rmin = rmin[goodidx]
        #thetadeg = np.arccos(costheta)*180./np.pi
        self.gasmass = gasmass[goodidx]
        self.closestm200 = closestm200[goodidx]

        #Reindex the Rmin, R200_DM params
        exec("self.rclosest = Rmin_%s_%s[snapnum-10][goodidx]" %
             (s_res, s_vel))
        exec("self.R200dm = R200dm_%s_%s[snapnum-10][goodidx]" %
             (s_res, s_vel))
コード例 #2
0
fp_result = open(
    '/home/nakazatoyr/arepo-c/Analysis/ArepoPostProcessing-H/snapnum_ID_matching_'
    + res + '_' + vel + '_' + species + '.dat', 'w')
SIGO_data = np.loadtxt(
    '/home/nakazatoyr/arepo-c/Analysis/ArepoPostProcessing-H/SIGOs_info_' +
    res + '_' + vel + '_' + species + '.dat')
ID_posi = np.loadtxt(
    '/home/nakazatoyr/arepo-c/Analysis/ArepoPostProcessing-H/SIGO_IDinfo_' +
    res + '_' + vel + '_' + species + '.dat')
filename = "/xc-work/chiakign/arepo-c/" + res + vel + species + "/"
filename2 = filename + "GasOnly_FOF"  #Used for readsubfHDF5
#filename3_032 = filename2 + "/snap-groupordered_" + str(32).zfill(3) #Used for snapHDF5
filename3_030 = filename2 + "/snap-groupordered_" + str(30).zfill(
    3)  #Used for snapHDF5
print(filename3_030)
iGas = snapHDF5.read_block(filename3_030, "ID  ",
                           parttype=0)  #load particle indices and catalogs
print('loading data done!')

#get unique ID
#for i in range(ID_posi.shape[0]):
for i in [0]:
    if i > 10:
        break
    else:
        idx = int(ID_posi[i, 0])  #SIGO_idx
        startidx = int(ID_posi[i, 1])  #startAllGas
        endidx = int(ID_posi[i, 2])  #endAllGas
        ID = iGas[startidx:endidx]
        fp_result = open(
            '/home/nakazatoyr/arepo-c/Analysis/ArepoPostProcessing-H/halo' +
            str(i) + '_ID_matching_' + res + '_' + vel + '_' + species +
コード例 #3
0
#Read header information
header = snapHDF5.snapshot_header(filename3)
red = header.redshift
atime = header.time
boxSize = header.boxsize
Omega0 = header.omega0
OmegaLambda = header.omegaL
massDMParticle = header.massarr[1] #all DM particles have same mass

#redshift evolution of critical_density
critical_density *= Omega0 + atime**3 * OmegaLambda
critical_density_gas = critical_density * baryonfraction

#load particle indices and catalogs
pGas= snapHDF5.read_block(filename3,"POS ", parttype=0)
mGas= snapHDF5.read_block(filename3,"MASS", parttype=0)
pDM = snapHDF5.read_block(filename3,"POS ", parttype=1)
cat = readsubfHDF5.subfind_catalog(filename2, snapnum)

halo100_indices= np.where(cat.GroupLenType[:,0] >100)[0]		
startAllGas = []
endAllGas   = []
for i in halo100_indices:
	startAllGas += [np.sum(cat.GroupLenType[:i,0])]
	endAllGas   += [startAllGas[-1] + cat.GroupLenType[i,0]]


SIGOidx = []

#Load shrinker and match data
コード例 #4
0
dir = "/media/ethan/Watson/research/data/metaldiff/res880/"

# num = 500 # 132
# snap_name = "snapshot_"

# fname = Base_dir + sim_name + "/" + snap_name + "%03d" % (num)

fname = dir + "snapshot_600"

## Example: get info from header:
# header = snap.snapshot_header(fname)
# Ngas = header.nall[0]
# print("Num of gas=%s" % Ngas)
# ##----

# ## Example, read gas density:
# rho = snap.read_block(fname,"RHO ",parttype=0)

## Example, read stellar positions and mass:
pos_str = snap.read_block(
    fname, "POS ",
    parttype=4)  #NOTE: get the 4-char flag "POS ", from the snappHDF5.py list
# mass_str = snap.read_block(fname,"MASS",parttype=4)

plt.plot(pos_str[:, 0], pos_str[:, 2], ',', alpha=0.2)
plt.title("x-z")
plt.show()

pdb.set_trace()
コード例 #5
0
def readhalo(base,
             snapbase,
             num,
             block_name,
             parttype,
             fof_num,
             sub_num,
             long_ids=False,
             double_output=False,
             verbose=False):
    global FlagRead, cat, GroupOffset, HaloOffset, multiple, filename, Parttype, FileTypeNumbers, FileNum

    if (FlagRead == False) | ((parttype in Parttype) == False):
        if verbose:
            print("READHALO: INITIAL READ")

        #add parttype to list
        Parttype.append(parttype)

        if verbose:
            print(("READHALO: Parttype = ", Parttype))

        #read in catalog
        cat = readsubfHDF5.subfind_catalog(base,
                                           num,
                                           long_ids=long_ids,
                                           double_output=double_output,
                                           keysel=[
                                               "GroupLenType", "GroupNsubs",
                                               "GroupFirstSub",
                                               "SubhaloLenType",
                                               "SubhaloMassType"
                                           ])

        if (cat.ngroups == 0):
            if verbose:
                print("READHALO: no groups in catalog... returning")
            return

        if (FlagRead == False):
            GroupOffset = np.zeros([cat.ngroups, 6], dtype="int64")
            HaloOffset = np.zeros([cat.nsubs, 6], dtype="int64")

            filename = base + "/" + snapbase + "_" + str(num).zfill(3)
            multiple = False
            if (os.path.exists(filename + ".hdf5") == False):
                filename = base + "/snapdir_" + str(num).zfill(
                    3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(
                        0)
                multiple = True
            if (os.path.exists(filename + ".hdf5") == False):
                print(("READHALO: [error] file not found : ", filename))
                sys.exit()

            FlagRead = True

    #construct offset tables
        k = 0
        for i in range(0, cat.ngroups):
            if (i > 0):
                GroupOffset[i, parttype] = GroupOffset[
                    i - 1, parttype] + cat.GroupLenType[i - 1, parttype]
            if (cat.GroupNsubs[i] > 0):
                HaloOffset[k, parttype] = GroupOffset[i, parttype]
                k += 1
                for j in range(1, cat.GroupNsubs[i]):
                    HaloOffset[k, parttype] = HaloOffset[
                        k - 1, parttype] + cat.SubhaloLenType[k - 1, parttype]
                    k += 1
        if (k != cat.nsubs):
            print(("READHALO: problem with offset table", k, cat.nsubs))
            sys.exit()

        #construct file tables
        if (multiple):
            filename = base + "/snapdir_" + str(num).zfill(
                3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(0)
        else:
            filename = base + "/" + snapbase + "_" + str(num).zfill(3)

        head = snapHDF5.snapshot_header(filename)
        FileNum = head.filenum

        FileTypeNumbers = np.zeros([FileNum, 6], dtype="int64")
        cumcount = np.zeros(6, dtype="int64")

        for fnr in range(0, FileNum - 1):
            if (multiple):
                filename = base + "/snapdir_" + str(num).zfill(
                    3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(
                        fnr)
            else:
                filename = base + "/" + snapbase + "_" + str(num).zfill(3)

            if verbose:
                print(("READHALO: initial reading file :", filename))

            head = snapHDF5.snapshot_header(filename)

            cumcount[:] += head.npart[:]
            FileTypeNumbers[fnr + 1, :] = cumcount[:]

    if (sub_num >= 0) & (fof_num < 0):
        off = HaloOffset[sub_num, parttype]
        left = cat.SubhaloLenType[sub_num, parttype]
        if verbose:
            print(("READHALO: nr / particle # / mass :", sub_num,
                   cat.SubhaloLenType[sub_num, parttype],
                   cat.SubhaloMassType[sub_num, parttype].astype("float64")))
    if (fof_num >= 0) & (sub_num < 0):
        off = GroupOffset[fof_num, parttype]
        left = cat.GroupLenType[fof_num, parttype]
        if verbose:
            print(("READHALO: nr / particle # / mass :", fof_num,
                   cat.GroupLenType[fof_num, parttype],
                   cat.GroupMassType[fof_num, parttype].astype("float64")))
    if (sub_num >= 0) & (fof_num >= 0):
        real_sub_num = sub_num + cat.GroupFirstSub[fof_num]
        off = HaloOffset[real_sub_num, parttype]
        left = cat.SubhaloLenType[real_sub_num, parttype]
        if verbose:
            print(("READHALO: nr / particle # / mass :", real_sub_num,
                   cat.SubhaloLenType[real_sub_num, parttype],
                   cat.SubhaloMassType[real_sub_num,
                                       parttype].astype("float64")))

    if (left == 0):
        if verbose:
            print("READHALO: no particles of type... returning")
        return

    #get first file that contains particles of required halo/fof/etc
    findex = np.argmax(FileTypeNumbers[:, parttype] > off) - 1
    #in case we reached the end argmax returns 0
    if (findex == -1):
        findex = FileNum - 1

    if verbose:
        print(("READHALO: first file that contains particles =", findex))

    for fnr in range(0, findex):
        off -= FileTypeNumbers[fnr + 1, parttype] - FileTypeNumbers[fnr,
                                                                    parttype]

    #read data from file
    first = True
    for fnr in range(findex, FileNum):
        if (multiple):
            filename = base + "/snapdir_" + str(num).zfill(
                3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(fnr)
        else:
            filename = base + "/" + snapbase + "_" + str(num).zfill(3)

        if verbose:
            print(("READHALO: reading file :", filename))

        head = snapHDF5.snapshot_header(filename)
        nloc = head.npart[parttype]

        if (nloc > off):
            if verbose:
                print("READHALO: data")
            start = off
            if (nloc - off > left):
                count = left
            else:
                count = nloc - off

            if (first == True):
                data = snapHDF5.read_block(filename,
                                           block_name,
                                           parttype,
                                           slab_start=start,
                                           slab_len=count)
                first = False
            else:
                data = np.append(data,
                                 snapHDF5.read_block(filename,
                                                     block_name,
                                                     parttype,
                                                     slab_start=start,
                                                     slab_len=count),
                                 axis=0)

            left -= count
            off += count
        if (left == 0):
            break
        off -= nloc

    return data
コード例 #6
0
Example to read snapshot from FIRE simulation
Author: Pouria A. Mistani
email: [email protected]
"""
import numpy as np
import matplotlib.pyplot as plt
import snapHDF5 as snap
import pdb
import PySmoother as PyS
import matplotlib.pyplot as plt

filename = '../../../snapshot_440'
filename = '../m12qq_hr_Dec16_2013/snapshot_440'
## -- read stellar components ---
pos = snap.read_block(
    filename, "POS ", parttype=4
)  #Notice that to read POS and VEL of gas and DM, only change parttype=0 or 1, respectively
print 'pase pos'
vel = snap.read_block(filename, "VEL ", parttype=4)
print 'pase vel'
mass = snap.read_block(filename, "MASS", parttype=4)
print 'pase mass'
'''
age = snap.read_block(filename, "AGE ", parttype=4)
print 'pase age'
metal = snap.read_block(filename, "Z   ", parttype=4)
print 'pase metal'
## -- read some gas-exclusive info ----- ##
sfr = snap.read_block(filename, "SFR ", parttype=0)
print 'pase sfr'
'''
コード例 #7
0
#Read halo catalog
cat = readsubfHDF5.subfind_catalog(filename2, snapnum)
r200 = cat.Group_R_Crit200
haloPos = cat.GroupPos

#indices of haloes >300 dm particles and r200 not 0
over300idx, = np.where(
    np.logical_and(np.greater(cat.GroupLenType[:, 1], 300),
                   np.not_equal(r200, 0.)))

#Initialize gas/DM indices for all haloes
allgasindex = np.empty(np.size(r200), dtype=list)
alldmindex = np.empty(np.size(r200), dtype=list)

#Read in particles
posgas = snapHDF5.read_block(filename3, "POS ", parttype=0)
posdm = snapHDF5.read_block(filename3, "POS ", parttype=1)

for j in over300idx:
    #Calculate indices of particles within r200
    indgas, = np.where(
        dist2(posgas[:, 0] - haloPos[j][0], posgas[:, 1] -
              haloPos[j][1], posgas[:, 2] -
              haloPos[j][2], boxSize) < r200[j]**2)
    allgasindex[j] = indgas
    inddm, = np.where(
        dist2(posdm[:, 0] - haloPos[j][0], posdm[:, 1] -
              haloPos[j][1], posdm[:, 2] -
              haloPos[j][2], boxSize) < r200[j]**2)
    alldmindex[j] = inddm
コード例 #8
0
ファイル: readhaloHDF5.py プロジェクト: ezbc/planckpy
def readhalo(
    base, snapbase, num, block_name, parttype, fof_num, sub_num, long_ids=False, double_output=False, verbose=False
):
    global FlagRead, cat, GroupOffset, HaloOffset, multiple, filename, Parttype, FileTypeNumbers, FileNum

    if (FlagRead == False) | ((parttype in Parttype) == False):
        if verbose:
            print "READHALO: INITIAL READ"

            # add parttype to list
        Parttype.append(parttype)

        if verbose:
            print "READHALO: Parttype = ", Parttype

            # read in catalog
        cat = readsubfHDF5.subfind_catalog(
            base,
            num,
            long_ids=long_ids,
            double_output=double_output,
            keysel=["GroupLenType", "GroupNsubs", "GroupFirstSub", "SubhaloLenType", "SubhaloMassType"],
        )

        if cat.ngroups == 0:
            if verbose:
                print "READHALO: no groups in catalog... returning"
            return

        if FlagRead == False:
            GroupOffset = np.zeros([cat.ngroups, 6], dtype="int64")
            HaloOffset = np.zeros([cat.nsubs, 6], dtype="int64")

            filename = base + "/" + snapbase + "_" + str(num).zfill(3)
            multiple = False
            if os.path.exists(filename + ".hdf5") == False:
                filename = (
                    base + "/snapdir_" + str(num).zfill(3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(0)
                )
                multiple = True
            if os.path.exists(filename + ".hdf5") == False:
                print "READHALO: [error] file not found : ", filename
                sys.exit()

            FlagRead = True

            # construct offset tables
        k = 0
        for i in range(0, cat.ngroups):
            if i > 0:
                GroupOffset[i, parttype] = GroupOffset[i - 1, parttype] + cat.GroupLenType[i - 1, parttype]
            if cat.GroupNsubs[i] > 0:
                HaloOffset[k, parttype] = GroupOffset[i, parttype]
                k += 1
                for j in range(1, cat.GroupNsubs[i]):
                    HaloOffset[k, parttype] = HaloOffset[k - 1, parttype] + cat.SubhaloLenType[k - 1, parttype]
                    k += 1
        if k != cat.nsubs:
            print "READHALO: problem with offset table", k, cat.nsubs
            sys.exit()

            # construct file tables
        if multiple:
            filename = base + "/snapdir_" + str(num).zfill(3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(0)
        else:
            filename = base + "/" + snapbase + "_" + str(num).zfill(3)

        head = snapHDF5.snapshot_header(filename)
        FileNum = head.filenum

        FileTypeNumbers = np.zeros([FileNum, 6], dtype="int64")
        cumcount = np.zeros(6, dtype="int64")

        for fnr in range(0, FileNum - 1):
            if multiple:
                filename = (
                    base + "/snapdir_" + str(num).zfill(3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(fnr)
                )
            else:
                filename = base + "/" + snapbase + "_" + str(num).zfill(3)

            if verbose:
                print "READHALO: initial reading file :", filename

            head = snapHDF5.snapshot_header(filename)

            cumcount[:] += head.npart[:]
            FileTypeNumbers[fnr + 1, :] = cumcount[:]

    if (sub_num >= 0) & (fof_num < 0):
        off = HaloOffset[sub_num, parttype]
        left = cat.SubhaloLenType[sub_num, parttype]
        if verbose:
            print "READHALO: nr / particle # / mass :", sub_num, cat.SubhaloLenType[
                sub_num, parttype
            ], cat.SubhaloMassType[sub_num, parttype].astype("float64")
    if (fof_num >= 0) & (sub_num < 0):
        off = GroupOffset[fof_num, parttype]
        left = cat.GroupLenType[fof_num, parttype]
        if verbose:
            print "READHALO: nr / particle # / mass :", fof_num, cat.GroupLenType[fof_num, parttype], cat.GroupMassType[
                fof_num, parttype
            ].astype("float64")
    if (sub_num >= 0) & (fof_num >= 0):
        real_sub_num = sub_num + cat.GroupFirstSub[fof_num]
        off = HaloOffset[real_sub_num, parttype]
        left = cat.SubhaloLenType[real_sub_num, parttype]
        if verbose:
            print "READHALO: nr / particle # / mass :", real_sub_num, cat.SubhaloLenType[
                real_sub_num, parttype
            ], cat.SubhaloMassType[real_sub_num, parttype].astype("float64")

    if left == 0:
        if verbose:
            print "READHALO: no particles of type... returning"
        return

        # get first file that contains particles of required halo/fof/etc
    findex = np.argmax(FileTypeNumbers[:, parttype] > off) - 1
    # in case we reached the end argmax returns 0
    if findex == -1:
        findex = FileNum - 1

    if verbose:
        print "READHALO: first file that contains particles =", findex

    for fnr in range(0, findex):
        off -= FileTypeNumbers[fnr + 1, parttype] - FileTypeNumbers[fnr, parttype]

        # read data from file
    first = True
    for fnr in range(findex, FileNum):
        if multiple:
            filename = (
                base + "/snapdir_" + str(num).zfill(3) + "/" + snapbase + "_" + str(num).zfill(3) + "." + str(fnr)
            )
        else:
            filename = base + "/" + snapbase + "_" + str(num).zfill(3)

        if verbose:
            print "READHALO: reading file :", filename

        head = snapHDF5.snapshot_header(filename)
        nloc = head.npart[parttype]

        if nloc > off:
            if verbose:
                print "READHALO: data"
            start = off
            if nloc - off > left:
                count = left
            else:
                count = nloc - off

            if first == True:
                data = snapHDF5.read_block(filename, block_name, parttype, slab_start=start, slab_len=count)
                first = False
            else:
                data = np.append(
                    data, snapHDF5.read_block(filename, block_name, parttype, slab_start=start, slab_len=count), axis=0
                )

            left -= count
            off += count
        if left == 0:
            break
        off -= nloc

    return data
コード例 #9
0
#Read header information
header = snapHDF5.snapshot_header(filename3)
red = header.redshift
atime = header.time
boxSize = header.boxsize
Omega0 = header.omega0
OmegaLambda = header.omegaL
massDMParticle = header.massarr[1] #all DM particles have same mass

#redshift evolution of critical_density
critical_density *= Omega0 + atime**3 * OmegaLambda
critical_density_gas = critical_density * baryonfraction

#load particle indices and catalogs
pGas = snapHDF5.read_block(filename3,"POS ", parttype=0)
mGas = snapHDF5.read_block(filename3,"MASS", parttype=0)
vGas = snapHDF5.read_block(filename3,"VEL ", parttype=0)
rGas = snapHDF5.read_block(filename3,"RHO ",parttype=0) 
uGas = snapHDF5.read_block(filename3,"U   ",parttype=0)
pDM = snapHDF5.read_block(filename3,"POS ",parttype=1)
catGas = readsubfHDF5.subfind_catalog(filename2, snapnum)


halo100_indices= np.where(catGas.GroupLenType[:,0] >100)[0]		
startAllGas = []
endAllGas   = []
for i in halo100_indices:
	startAllGas += [np.sum(catGas.GroupLenType[:i,0])]
	endAllGas   += [startAllGas[-1] + catGas.GroupLenType[i,0]]
コード例 #10
0
ファイル: SIGO_partposi.py プロジェクト: yurina0207/git_test2
    header = snapHDF5.snapshot_header(filename3)
    red = header.redshift
    atime = header.time
    boxSize = header.boxsize
    Omega0 = header.omega0
    OmegaLambda = header.omegaL
    massDMParticle = header.massarr[1]  #all DM particles have same mass

    print('reading header information done')
    #redshift evolution of critical density
    critical_density *= Omega0 + atime**3 * OmegaLambda
    critical_density_gas = critical_density * baryonfraction

    print(filename3)
    #load particle indices and catalogs
    pGas = snapHDF5.read_block(filename3, "POS ", parttype=0)
    iGas = snapHDF5.read_block(filename3, "ID  ", parttype=0)
    mGas = snapHDF5.read_block(filename3, "MASS", parttype=0)
    eGas = snapHDF5.read_block(filename3, "U   ", parttype=0)
    dGas = snapHDF5.read_block(filename3, "RHO ", parttype=0)
    xHI = snapHDF5.read_block(filename3, "HI  ", parttype=0)
    if str(species) == 'H2':
        xH2I = snapHDF5.read_block(filename3, "H2I ", parttype=0)
    pDM = snapHDF5.read_block(filename3, "POS ", parttype=1)
    cat = readsubfHDF5.subfind_catalog(filename2, snapnum)
    #Read in particles
    '''
    posgas = snapHDF5.read_block(filename4, "POS ", parttype=0)
    posdm = snapHDF5.read_block(filename4, "POS ", parttype=1)
    idxdm = snapHDF5.read_block(filename4, "ID  ", parttype=1)
    idxgas = snapHDF5.read_block(filename4, "ID  ", parttype=0)
コード例 #11
0
'''
mass = snap.read_block(filename, "MASS", parttype=4)
print 'pase mass'
age = snap.read_block(filename, "AGE ", parttype=4)
print 'pase age'
metal = snap.read_block(filename, "Z   ", parttype=4)
print 'pase metal'
## -- read some gas-exclusive info ----- ##
sfr = snap.read_block(filename, "SFR ", parttype=0)
print 'pase sfr'
'''

print '1e10'

pos = snap.read_block(
    filename1, "POS ", parttype=1
)  #Notice that to read POS and VEL of gas and DM, only change parttype=0 or 1, respectively
print 'pase pos'
vel = snap.read_block(filename1, "VEL ", parttype=1)
print 'pase vel'
print 'done reading', pos.shape
X = pos[:, 0]
Y = pos[:, 1]
Z = pos[:, 2]
Vx = vel[:, 0]
Vy = vel[:, 1]
Vz = vel[:, 2]
mass = np.ones(len(X))

image = PyS.PyS(X, Y, Z, mass, Vx, Vy, Vz, r=2, w=1000, h=1000, offset=50)
M_Vir = image.Virial_Cut(45)
コード例 #12
0
    #SIGO_data = np.loadtxt('/home/nakazatoyr/arepo-c/Analysis/output/'+res+vel+species+'/SIGOs_info_'+res+'_'+vel+'_'+species+'.dat')
    ID_posi = np.loadtxt('/home/nakazatoyr/arepo-c/Analysis/output/' + res +
                         vel + species + '/SIGO_IDinfo_' + res + '_' + vel +
                         '_' + species + '.dat')
    filename = "/xc-work/nakazatoyr/arepo-c/" + res + vel + species + "_refine/"
if str(ref) == 'OFF':
    #SIGO_data = np.loadtxt('/home/nakazatoyr/arepo-c/Analysis/output/'+res+vel+species+'_nonref/SIGOs_info_'+res+'_'+vel+'_'+species+'.dat')
    ID_posi = np.loadtxt('/home/nakazatoyr/arepo-c/Analysis/output/' + res +
                         vel + species + '_nonref/SIGO_IDinfo_' + res + '_' +
                         vel + '_' + species + '.dat')
    filename = "/xc-work/nakazatoyr/arepo-c/" + res + vel + species + "_nonref/"
filename2 = filename + "GasOnly_FOF"  #Used for readsubfHDF5
filename3_032 = filename2 + "/snap-groupordered_" + str(32).zfill(
    3)  #Used for snapHDF5
print(filename3_032)
iGas = snapHDF5.read_block(filename3_032, "ID  ",
                           parttype=0)  #load particle indices and catalogs
print('loading data done!')

#get unique ID
#for i in range(ID_posi.shape[0]):
for i in [0]:
    if i > 10:
        break
    else:
        idx = int(ID_posi[i, 0])  #SIGO_idx
        startidx = int(ID_posi[i, 1])  #startAllGas
        endidx = int(ID_posi[i, 2])  #endAllGas
        ID = iGas[startidx:endidx]
        print("halo" + str(idx) + ", startidx is" + str(startidx) +
              ", endidx is" + str(endidx))
        print("the number of IDs is" + str(len(ID)))
コード例 #13
0
	def __init__(self, res, vel, snapnum):
		self.vel = vel
		self.res = res
		self.snapnum = int(snapnum)
		self.s_vel = vel.replace(".","")
		self.s_res = res.replace(".","")

		#File paths
		filename = "/n/hernquistfs3/mvogelsberger/GlobularClusters/InterfaceWArepo_All_" + self.res + '_' + self.vel  + "/output/"
		filename2 = filename +  "DM_FOF" #Used for readsubfHDF5
		filename3 = filename + "snap_" + str(self.snapnum).zfill(3) #Used for hdf5lib, snapHDF5
		#Read header information	
		header = snapHDF5.snapshot_header(filename3)
		with hdf5lib.OpenFile(filename3 + ".hdf5") as fs:
			red = hdf5lib.GetAttr(fs, "Header", "Redshift")
			atime = hdf5lib.GetAttr(fs, "Header", "Time")
			boxSize = hdf5lib.GetAttr(fs, "Header", "BoxSize")
			boxSize *= atime / hubbleparam #convert from ckpc/h to kpc
			Omega0 = hdf5lib.GetAttr(fs, "Header", "Omega0")
			OmegaLambda = hdf5lib.GetAttr(fs, "Header", "OmegaLambda")
		
		#Read halo catalog
		cat = readsubfHDF5.subfind_catalog(filename2, self.snapnum)	
		#critical_density *= 1. / (Omega0 + OmegaLambda * atime * atime * atime) #redshift correction
		r200 = cat.Group_R_Crit200
		r200 *= atime / hubbleparam #convert from ckpc/h to kpc
		m200 = cat.Group_M_Crit200
		m200 *= 1. / hubbleparam #convert to 10^10 M_sun
		haloCMvel = cat.GroupVel
		haloCMvel *= 1. / atime #convert from km/s/a to km/s
		haloPos = cat.GroupPos
		haloPos *= atime / hubbleparam #convert from ckpc/h to kpc

		#Initialize arrays
		spinparamTotal = np.zeros(np.size(r200))
		spinparamGas = np.zeros(np.size(r200))
		spinparamDM = np.zeros(np.size(r200))
		gasfrac = np.zeros(np.size(r200))
		costheta = np.zeros(np.size(r200)) #misalignment angle
		v200 = np.zeros(np.size(r200))	
		numGas = np.zeros(np.size(r200))
		numDM = np.zeros(np.size(r200)) 

		#Read in particles
		massgas = snapHDF5.read_block(filename3, "MASS", parttype=0)
		massdm = snapHDF5.read_block(filename3, "MASS", parttype=1)
		posgas = snapHDF5.read_block(filename3, "POS ", parttype=0)
		posdm = snapHDF5.read_block(filename3, "POS ", parttype=1)
		velgas = snapHDF5.read_block(filename3, "VEL ", parttype=0)
		veldm = snapHDF5.read_block(filename3, "VEL ", parttype=1)
		#redefine position units from ckpc/h to kpc
		posgas *= atime / hubbleparam
		posdm *= atime / hubbleparam
		#redefine velocity units from kmsqrt(a)/s to km/s
		velgas *= np.sqrt(atime)
		veldm *= np.sqrt(atime)

		#boxSize hubble flow correction for halo CM velocity subtraction
		boxSizeVel = boxSize * hubbleparam * .1 * np.sqrt(Omega0/atime/atime/atime + OmegaLambda)
		


		#load particle indices
		over300idx, indgas, inddm = np.load('particleindex_' + self.res + '_' + self.vel + '_' + str(self.snapnum) + '.npy')
		over300idx = over300idx.astype(int)
		over1 = []

		for i,j in enumerate(over300idx):
			#remove halo CM velocity
			tempvelgas = dx_wrap(velgas[indgas[i]] - haloCMvel[j],boxSizeVel)
			tempveldm = dx_wrap(veldm[inddm[i]] - haloCMvel[j],boxSizeVel)
			#redefine positions wrt COM
			tempposgas = dx_wrap(posgas[indgas[i]] - haloPos[j],boxSize)
			tempposdm = dx_wrap(posdm[inddm[i]] - haloPos[j],boxSize)
			numDM[j] = np.size(tempposdm)
			numGas[j] = np.size(tempposgas)
			#Calculating j200
			#j200 of all particles
			j200vecgas = np.sum(np.cross(tempposgas,tempvelgas)*massgas[indgas[i]][:, np.newaxis],axis=0)
			j200vecdm = np.sum(np.cross(tempposdm,tempveldm)*massdm[inddm[i]][:, np.newaxis],axis=0)
			if np.size(tempvelgas)!=0: #can be no gas particles!
				costheta[j] = np.dot(j200vecgas,j200vecdm)/np.linalg.norm(j200vecgas)/np.linalg.norm(j200vecdm)
			j200vec = j200vecgas + j200vecdm
			j200 = np.linalg.norm(j200vec)
			j200gas = np.linalg.norm(j200vecgas)
			j200dm = np.linalg.norm(j200vecdm)
			v200[j] = np.sqrt(GCONST*m200[j]/r200[j])
			
			#Bullock spin parameter
			totalmass = massgas[indgas[i]].sum(dtype='float64') + massdm[inddm[i]].sum(dtype='float64')
			spinparamTotal[j] = j200/np.sqrt(2)/v200[j]/r200[j]/totalmass
			if np.size(tempveldm)!=0: #tempveldm can be empty no dm particles!
				spinparamDM[j] = j200dm/np.sqrt(2)/v200[j]/r200[j]/massdm[inddm[i]].sum(dtype='float64')
			if np.size(tempvelgas)!=0: #tempvelgas can be empty no gas particles!
				spinparamGas[j] = j200gas/np.sqrt(2)/v200[j]/r200[j]/massgas[indgas[i]].sum(dtype='float64')
			gasfrac[j] = massgas[indgas[i]].sum(dtype='float64') / (massgas[indgas[i]].sum(dtype='float64') + massdm[inddm[i]].sum(dtype='float64'))

		#Reindex over300idx to account for SO halos with DM particles >300
		over300idx2 = over300idx[numDM[over300idx] > 300]

		#Plotting
		#Redfine in terms of over300idx2
		self.spinparamTotal = spinparamTotal[over300idx2]
		self.spinparamGas = spinparamGas[over300idx2]
		self.spinparamDM = spinparamDM[over300idx2]
		self.gasfrac = gasfrac[over300idx2]	
		self.m200 = m200[over300idx2]
		self.m200 *= 10**10  #Convert to solar mass.
		self.costheta = costheta[over300idx2]
		self.gasfracCosTheta = self.gasfrac[self.costheta!=0.]
		self.m2002 = self.m200[self.costheta!=0.]
		self.costheta = self.costheta[self.costheta!=0.] #take out the 0 gas components
		self.thetadeg = np.arccos(self.costheta)*180./np.pi