예제 #1
0
    def write_snapshot(self,disk,disk_mesh,filename="disk.dat.hdf5",time=0):
        
       
        
        Ngas = self.pos.shape[0]
        f=ws.openfile(filename)
        npart=np.array([Ngas,0,0,0,0,0], dtype="uint32")
        massarr=np.array([0,0,0,0,0,0], dtype="float64")
        header=ws.snapshot_header(npart=npart, nall=npart, massarr=massarr, time=time,
                              boxsize=disk_mesh.BoxSize, double = np.array([1], dtype="int32"))

        
        ws.writeheader(f, header)
        ws.write_block(f, "POS ", 0, self.pos)
        ws.write_block(f, "VEL ", 0, self.vel)
        ws.write_block(f, "MASS", 0, self.dens)
        ws.write_block(f, "U   ", 0, self.utherm)
        ws.write_block(f, "ID  ", 0, self.ids)
        ws.closefile(f)
예제 #2
0
    def write_snapshot(self,
                       disk,
                       disk_mesh,
                       filename="disk.dat.hdf5",
                       time=0):

        Ngas = self.pos.shape[0]
        f = ws.openfile(filename)
        npart = np.array([Ngas, 0, 0, 0, 0, 0], dtype="uint32")
        massarr = np.array([0, 0, 0, 0, 0, 0], dtype="float64")
        header = ws.snapshot_header(npart=npart,
                                    nall=npart,
                                    massarr=massarr,
                                    time=time,
                                    boxsize=disk_mesh.BoxSize,
                                    double=np.array([1], dtype="int32"))

        ws.writeheader(f, header)
        ws.write_block(f, "POS ", 0, self.pos)
        ws.write_block(f, "VEL ", 0, self.vel)
        ws.write_block(f, "MASS", 0, self.dens)
        ws.write_block(f, "U   ", 0, self.utherm)
        ws.write_block(f, "ID  ", 0, self.ids)
        ws.closefile(f)
if not (ExternalPotential):
  print "Adding central star..."
  if(add_background):
    ws.write_block(f, "POS ", 4, np.array([BOX_WIDTH * 0.5, BOX_WIDTH * 0.5, BOX_HEIGHT * 0.5]).T)
  else:
    ws.write_block(f, "POS ", 4,np.array([0.0,0.0,0.0]).T)
  ws.write_block(f, "VEL ", 4, np.array([0.0,0.0,0.0]).T)
  ws.write_block(f, "MASS", 4, np.array([M_star]))
  ws.write_block(f, "ID  ", 4, np.array([N_gas + 1]))
    
  npart=np.array([N_gas,0,0,0,1,0], dtype="uint32")
else:
  npart=np.array([N_gas,0,0,0,0,0], dtype="uint32")

massarr=np.array([0,0,0,0,0,0], dtype="float64")
header=ws.snapshot_header(npart=npart, nall=npart, massarr=massarr, double = np.array([double_precision], dtype="int32"))
ws.writeheader(f, header)
ws.closefile(f)
print "done."

print x_gas.shape[0],mass.shape,dens_gas.shape,vx_gas.shape[0],utherm.shape

print "\n-FINISHED-\n"

if not (add_background):
  print "N_gas  = ", N_gas
if (add_background):
  print "N_gas(in disk)  = ", N_in_disk
  print "N(background grid)  = ", N_BACKGROUND

print "M_disk=",Real_M_disk,GasMass(R_max), GasMass(R_cut)
        print("halo" + str(idx) + ", startidx is" + str(startidx) +
              ", endidx is" + str(endidx))
        print("the number of IDs is" + str(len(ID)))

    #for snapnum in range(20, 33, 2):
    for snapnum in [30, 28]:
        filename = "/xc-work/chiakign/arepo-c/" + res + vel + species + "/"
        filename2 = filename + "GasOnly_FOF"  #Used for readsubfHDF5
        filename3 = filename2 + "/snap-groupordered_" + str(snapnum).zfill(
            3)  #Used for snapHDF5
        filename4 = filename + "snap_" + str(snapnum).zfill(
            3)  #Used for hdf5lib, snapHDF5
        #filename3 = filename + "snap_" + str(snapnum).zfill(3) #Used for hdf5lib, snapHDF5

        #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

        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(filename4, "POS ", parttype=0)  #correct 4
    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))
  cellsize = 2.0*np.pi*radius/Ntheta
elif (mesh_type == "rings"):
  cellsize = 2.0*np.pi*inner_rad/N_in

timestep = np.minimum(CourantFac*cellsize/csnd,ErrTolIntAccuracy*np.sqrt(cellsize/accel))
timestep_nbin=np.log10(MaxSizeTimestep/MinSizeTimestep)/np.log10(2.0)
timestep_bins = 2.0**np.linspace(np.log10(MinSizeTimestep)/np.log10(2.0),np.log10(MaxSizeTimestep)/np.log10(2.0),timestep_nbin)
hist=np.histogram(timestep,bins=timestep_bins)
print hist

Ngas = pos.shape[0]
print "shape",pos.shape, vel.shape,dens.shape,ids.shape

f=ws.openfile("disk.dat.hdf5")
npart=np.array([Ngas,0,0,0,0,0], dtype="uint32")
massarr=np.array([0,0,0,0,0,0], dtype="float64")
header=ws.snapshot_header(npart=npart, nall=npart, massarr=massarr,
                          boxsize=BoxX, double = np.array([1], dtype="int32"))

        
ws.writeheader(f, header)
ws.write_block(f, "POS ", 0, pos)
ws.write_block(f, "VEL ", 0, vel)
ws.write_block(f, "MASS", 0, dens)
ws.write_block(f, "U   ", 0, u)
ws.write_block(f, "ID  ", 0, ids)
ws.closefile(f)

R_edge=2.5
print "Mass resolution inside the cavity",np.pi*(R_edge*2*np.pi/Nphi_in)**2*vecdiskdens(R_edge,p,xi,r_cav)
예제 #7
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
예제 #8
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
예제 #9
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