示例#1
0
    def __init__(self,
                 basedir,
                 skipfac,
                 snapnum,
                 filenum=0,
                 tree_start=-1,
                 tree_num=-1,
                 keysel=None):

        self.filebase = basedir + "trees_sf" + str(skipfac) + "_" + str(
            snapnum).zfill(3)
        self.basedir = basedir
        self.filenum = filenum
        filename = self.filebase + "." + str(filenum) + ".hdf5"
        f = hdf5lib.OpenFile(filename)
        self.NtreesPerFile = hdf5lib.GetAttr(f, "Header", "NtreesPerFile")
        self.NumberOfOutputFiles = hdf5lib.GetAttr(f, "Header",
                                                   "NumberOfOutputFiles")
        self.ParticleMass = hdf5lib.GetAttr(f, "Header", "ParticleMass")
        if (self.ParticleMass == 0):
            print(
                "WARNING: ParticleMass = 0, needed for merger rate calculation"
            )
        self.TreeNHalos = hdf5lib.GetData(f, "Header/TreeNHalos")[:]
        self.TotNsubhalos = hdf5lib.GetData(f, "Header/TotNsubhalos")[:]
        self.Redshifts = hdf5lib.GetData(f, "Header/Redshifts")[:]
        if (tree_start == -1) | (tree_num == -1):
            tree_start = 0
            tree_num = self.NtreesPerFile
        self.trees = np.empty(tree_num - tree_start, dtype='object')
        self.tree_start = tree_start
        self.tree_num = tree_num
        for ntree in range(tree_start, tree_start + tree_num):
            list = []
            if (keysel == None):
                for datablock in list(mergertree_datablocks.keys()):
                    data = hdf5lib.GetData(
                        f, "Tree" + str(ntree) + "/" + datablock)[:]
                    list.append((datablock, data))
            else:
                for datablock in keysel:
                    if hdf5lib.Contains(f, "Tree" + str(ntree), datablock):
                        data = hdf5lib.GetData(
                            f, "Tree" + str(ntree) + "/" + datablock)[:]
                        list.append((datablock, data))
            self.trees[ntree - tree_start] = dict(list)
        f.close()
示例#2
0
    def __init__(self,
                 basedir,
                 snapnum,
                 long_ids=False,
                 double_output=False,
                 grpcat=True,
                 subcat=True,
                 name="fof_subhalo_tab",
                 keysel=None):
        self.filebase = basedir + "/groups_" + str(snapnum).zfill(
            3) + "/" + name + "_" + str(snapnum).zfill(3) + "."

        if long_ids: self.id_type = np.uint64
        else: self.id_type = np.uint32
        if double_output: self.double_type = np.float32
        else: self.double_type = np.float64

        filenum = 0
        doneflag = False
        skip_gr = 0
        skip_sub = 0
        vardict = {}

        while not doneflag:
            curfile = self.filebase + str(filenum) + ".hdf5"

            if (not os.path.exists(curfile)):
                self.filebase = basedir + "/" + name + "_" + str(
                    snapnum).zfill(3)
                curfile = self.filebase + ".hdf5"
            if (not os.path.exists(curfile)):
                print "file not found:", curfile
                sys.exit()

            f = hdf5lib.OpenFile(curfile)
            ngroups = hdf5lib.GetAttr(f, "Header", "Ngroups_ThisFile")
            nsubs = hdf5lib.GetAttr(f, "Header", "Nsubgroups_ThisFile")
            nfiles = hdf5lib.GetAttr(f, "Header", "NumFiles")
            if filenum == 0:
                self.ngroups = hdf5lib.GetAttr(f, "Header", "Ngroups_Total")
                self.nids = hdf5lib.GetAttr(f, "Header", "Nids_Total")
                self.nsubs = hdf5lib.GetAttr(f, "Header", "Nsubgroups_Total")
                #GROUPS
                if (grpcat == True):
                    if (keysel == None):
                        for key, val in grp_datablocks.items():
                            if hdf5lib.Contains(f, "Group", key):
                                type = val[0]
                                dim = val[1]
                                if (type == 'FLOAT'):
                                    vars(self)[key] = np.empty(
                                        self.ngroups,
                                        dtype=np.dtype(
                                            (self.double_type, dim)))
                                if (type == 'INT'):
                                    vars(self)[key] = np.empty(self.ngroups,
                                                               dtype=np.dtype(
                                                                   (np.int32,
                                                                    dim)))
                                if (type == 'INT64'):
                                    vars(self)[key] = np.empty(self.ngroups,
                                                               dtype=np.dtype(
                                                                   (np.int64,
                                                                    dim)))
                                if (type == 'ID'):
                                    vars(self)[key] = np.empty(
                                        self.ngroups,
                                        dtype=np.dtype((self.id_type, dim)))
                                vardict[key] = vars(self)[key]
                    else:
                        for key in keysel:
                            if hdf5lib.Contains(f, "Group", key):
                                val = grp_datablocks[key]
                                type = val[0]
                                dim = val[1]
                                if (type == 'FLOAT'):
                                    vars(self)[key] = np.empty(
                                        self.ngroups,
                                        dtype=np.dtype(
                                            (self.double_type, dim)))
                                if (type == 'INT'):
                                    vars(self)[key] = np.empty(self.ngroups,
                                                               dtype=np.dtype(
                                                                   (np.int32,
                                                                    dim)))
                                if (type == 'INT64'):
                                    vars(self)[key] = np.empty(self.ngroups,
                                                               dtype=np.dtype(
                                                                   (np.int64,
                                                                    dim)))
                                if (type == 'ID'):
                                    vars(self)[key] = np.empty(
                                        self.ngroups,
                                        dtype=np.dtype((self.id_type, dim)))
                                vardict[key] = vars(self)[key]

                #SUBHALOS
                if (subcat == True):
                    if (keysel == None):
                        for key, val in sub_datablocks.items():
                            if hdf5lib.Contains(f, "Subhalo", key):
                                type = val[0]
                                dim = val[1]
                            if (type == 'FLOAT'):
                                vars(self)[key] = np.empty(
                                    self.nsubs,
                                    dtype=np.dtype((self.double_type, dim)))
                            if (type == 'INT'):
                                vars(self)[key] = np.empty(self.nsubs,
                                                           dtype=np.dtype(
                                                               (np.int32,
                                                                dim)))
                            if (type == 'INT64'):
                                vars(self)[key] = np.empty(self.nsubs,
                                                           dtype=np.dtype(
                                                               (np.int32,
                                                                dim)))
                            if (type == 'ID'):
                                vars(self)[key] = np.empty(self.nsubs,
                                                           dtype=np.dtype(
                                                               (self.id_type,
                                                                dim)))
                            vardict[key] = vars(self)[key]
                    else:
                        for key in keysel:
                            if hdf5lib.Contains(f, "Subhalo", key):
                                val = sub_datablocks[key]
                                type = val[0]
                                dim = val[1]
                                if (type == 'FLOAT'):
                                    vars(self)[key] = np.empty(
                                        self.nsubs,
                                        dtype=np.dtype(
                                            (self.double_type, dim)))
                                if (type == 'INT'):
                                    vars(self)[key] = np.empty(self.nsubs,
                                                               dtype=np.dtype(
                                                                   (np.int32,
                                                                    dim)))
                                if (type == 'INT64'):
                                    vars(self)[key] = np.empty(self.nsubs,
                                                               dtype=np.dtype(
                                                                   (np.int64,
                                                                    dim)))
                                if (type == 'ID'):
                                    vars(self)[key] = np.empty(
                                        self.nsubs,
                                        dtype=np.dtype((self.id_type, dim)))
                                vardict[key] = vars(self)[key]

        #GROUPS
            if (grpcat == True):
                if ngroups > 0:
                    if (keysel == None):
                        for key, val in grp_datablocks.items():
                            if hdf5lib.Contains(f, "Group", key):
                                type = val[0]
                                dim = val[1]
                                a = hdf5lib.GetData(f, "Group/" + key)
                                if dim == 1:
                                    vardict[key][skip_gr:skip_gr +
                                                 ngroups] = a[:]
                                else:
                                    for d in range(0, dim):
                                        vardict[key][skip_gr:skip_gr + ngroups,
                                                     d] = a[:, d]
                    else:
                        for key in keysel:
                            if hdf5lib.Contains(f, "Group", key):
                                val = grp_datablocks[key]
                                type = val[0]
                                dim = val[1]
                                a = hdf5lib.GetData(f, "Group/" + key)
                                if dim == 1:
                                    vardict[key][skip_gr:skip_gr +
                                                 ngroups] = a[:]
                                else:
                                    for d in range(0, dim):
                                        vardict[key][skip_gr:skip_gr + ngroups,
                                                     d] = a[:, d]

                    skip_gr += ngroups
            #SUBHALOS
            if (subcat == True):
                if nsubs > 0:
                    if (keysel == None):
                        for key, val in sub_datablocks.items():
                            if hdf5lib.Contains(f, "Subhalo", key):
                                type = val[0]
                                dim = val[1]
                                a = hdf5lib.GetData(f, "Subhalo/" + key)
                                if dim == 1:
                                    vardict[key][skip_sub:skip_sub +
                                                 nsubs] = a[:]
                                else:
                                    for d in range(0, dim):
                                        vardict[key][skip_sub:skip_sub + nsubs,
                                                     d] = a[:, d]
                    else:
                        for key in keysel:
                            if hdf5lib.Contains(f, "Subhalo", key):
                                val = sub_datablocks[key]
                                type = val[0]
                                dim = val[1]
                                a = hdf5lib.GetData(f, "Subhalo/" + key)
                                if dim == 1:
                                    vardict[key][skip_sub:skip_sub +
                                                 nsubs] = a[:]
                                else:
                                    for d in range(0, dim):
                                        vardict[key][skip_sub:skip_sub + nsubs,
                                                     d] = a[:, d]

                    skip_sub += nsubs

            f.close()

            filenum += 1
            if filenum == nfiles: doneflag = True
示例#3
0
	def __init__(self, *args, **kwargs):
		if (len(args) == 1):
			filename = args[0]

			if os.path.exists(filename):
				curfilename=filename
			elif os.path.exists(filename+".hdf5"):
				curfilename = filename+".hdf5"
			elif os.path.exists(filename+".0.hdf5"): 
				curfilename = filename+".0.hdf5"
			else:	
				print("[error] file not found : ", filename)
				sys.stdout.flush()
				sys.exit()

			f=hdf5lib.OpenFile(curfilename)
			self.npart = hdf5lib.GetAttr(f, "Header", "NumPart_ThisFile") 
			self.nall = hdf5lib.GetAttr(f, "Header", "NumPart_Total")
			self.nall_highword = hdf5lib.GetAttr(f, "Header", "NumPart_Total_HighWord") 
			self.massarr = hdf5lib.GetAttr(f, "Header", "MassTable")
			self.time = hdf5lib.GetAttr(f, "Header", "Time") 
			self.redshift = hdf5lib.GetAttr(f, "Header", "Redshift") 
			self.boxsize = hdf5lib.GetAttr(f, "Header", "BoxSize") 
			self.filenum = hdf5lib.GetAttr(f, "Header", "NumFilesPerSnapshot") 
			self.omega0 = hdf5lib.GetAttr(f, "Header", "Omega0")
			self.omegaL = hdf5lib.GetAttr(f, "Header", "OmegaLambda") 
			self.hubble = hdf5lib.GetAttr(f, "Header", "HubbleParam") 
			self.sfr = hdf5lib.GetAttr(f, "Header", "Flag_Sfr") 
			self.cooling = hdf5lib.GetAttr(f, "Header", "Flag_Cooling") 
			self.stellar_age = hdf5lib.GetAttr(f, "Header", "Flag_StellarAge") 
			self.metals = hdf5lib.GetAttr(f, "Header", "Flag_Metals") 
			self.feedback = hdf5lib.GetAttr(f, "Header", "Flag_Feedback") 
			self.double = hdf5lib.GetAttr(f, "Header", "Flag_DoublePrecision") #GADGET-2 change
			f.close()
		else:
			#read arguments
			self.npart = kwargs.get("npart")
			self.nall = kwargs.get("nall")
			self.nall_highword = kwargs.get("nall_highword")
			self.massarr = kwargs.get("massarr")
			self.time = kwargs.get("time")
			self.redshift = kwargs.get("redshift")
			self.boxsize = kwargs.get("boxsize")
			self.filenum = kwargs.get("filenum")
			self.omega0 = kwargs.get("omega0")
			self.omegaL = kwargs.get("omegaL")
			self.hubble = kwargs.get("hubble")
			self.sfr = kwargs.get("sfr")
			self.cooling = kwargs.get("cooling")
			self.stellar_age = kwargs.get("stellar_age")
			self.metals = kwargs.get("metals")
			self.feedback = kwargs.get("feedback")
			self.double = kwargs.get("double")

			#set default values
			if (self.npart == None):
				self.npart = np.array([0,0,0,0,0,0], dtype="int32")
			if (self.nall == None):				
				self.nall  = np.array([0,0,0,0,0,0], dtype="uint32")
			if (self.nall_highword == None):				
				self.nall_highword = np.array([0,0,0,0,0,0], dtype="uint32")
			if (self.massarr == None):
				self.massarr = np.array([0,0,0,0,0,0], dtype="float64")
			if (self.time == None):				
				self.time = np.array([0], dtype="float64")
			if (self.redshift == None):				
				self.redshift = np.array([0], dtype="float64")
			if (self.boxsize == None):				
				self.boxsize = np.array([0], dtype="float64")
			if (self.filenum == None):
				self.filenum = np.array([1], dtype="int32")
			if (self.omega0 == None):
				self.omega0 = np.array([0], dtype="float64")
			if (self.omegaL == None):
				self.omegaL = np.array([0], dtype="float64")
			if (self.hubble == None):
				self.hubble = np.array([0], dtype="float64")
			if (self.sfr == None):	
				self.sfr = np.array([0], dtype="int32")            
			if (self.cooling == None):	
				self.cooling = np.array([0], dtype="int32")
			if (self.stellar_age == None):	
				self.stellar_age = np.array([0], dtype="int32")
			if (self.metals == None):	
				self.metals = np.array([0], dtype="int32")
			if (self.feedback == None):	
				self.feedback = np.array([0], dtype="int32")
			if (self.double == None):
				self.double = np.array([0], dtype="int32")
    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))
示例#5
0
    def __init__(self,
                 basedir,
                 snapnum,
                 long_ids=False,
                 double_output=False,
                 grpcat=True,
                 subcat=True,
                 name="fof_subhalo_tab",
                 keysel=[]):

        if long_ids: self.id_type = np.uint64
        else: self.id_type = np.uint32
        if double_output: self.double_type = np.float32
        else: self.double_type = np.float64

        filenum = 0
        doneflag = False
        skip_gr = 0
        skip_sub = 0
        vardict = {}
        if keysel is None:
            keysel = grp_datablocks.items()

        while not doneflag:
            self.filebase, curfile = naming.return_subfind_filebase(
                basedir, snapnum, name, filenum)
            self.firstfile = curfile

            f = hdf5lib.OpenFile(curfile)
            ngroups = hdf5lib.GetAttr(f, "Header", "Ngroups_ThisFile")
            nsubs = hdf5lib.GetAttr(f, "Header", "Nsubgroups_ThisFile")
            nfiles = hdf5lib.GetAttr(f, "Header", "NumFiles")
            if filenum == 0:
                self.ngroups = hdf5lib.GetAttr(f, "Header", "Ngroups_Total")
                self.nids = hdf5lib.GetAttr(f, "Header", "Nids_Total")
                self.nsubs = hdf5lib.GetAttr(f, "Header", "Nsubgroups_Total")
                #GROUPS
                if grpcat:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Group", key):
                            val = grp_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            if (type == 'FLOAT'):
                                vars(self)[key] = np.empty(
                                    self.ngroups,
                                    dtype=np.dtype((self.double_type, dim)))
                            if (type == 'INT'):
                                vars(self)[key] = np.empty(self.ngroups,
                                                           dtype=np.dtype(
                                                               (np.int32,
                                                                dim)))
                            if (type == 'INT64'):
                                vars(self)[key] = np.empty(self.ngroups,
                                                           dtype=np.dtype(
                                                               (np.int64,
                                                                dim)))
                            if (type == 'ID'):
                                vars(self)[key] = np.empty(self.ngroups,
                                                           dtype=np.dtype(
                                                               (self.id_type,
                                                                dim)))
                            vardict[key] = vars(self)[key]

                #SUBHALOS
                if subcat:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Subhalo", key):
                            val = sub_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            if (type == 'FLOAT'):
                                vars(self)[key] = np.empty(
                                    self.nsubs,
                                    dtype=np.dtype((self.double_type, dim)))
                            if (type == 'INT'):
                                vars(self)[key] = np.empty(self.nsubs,
                                                           dtype=np.dtype(
                                                               (np.int32,
                                                                dim)))
                            if (type == 'INT64'):
                                vars(self)[key] = np.empty(self.nsubs,
                                                           dtype=np.dtype(
                                                               (np.int64,
                                                                dim)))
                            if (type == 'ID'):
                                vars(self)[key] = np.empty(self.nsubs,
                                                           dtype=np.dtype(
                                                               (self.id_type,
                                                                dim)))
                            vardict[key] = vars(self)[key]

            #GROUPS
            if grpcat:
                if ngroups > 0:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Group", key):
                            val = grp_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            a = hdf5lib.GetData(f, "Group/" + key)
                            if dim == 1:
                                vardict[key][skip_gr:skip_gr + ngroups] = a[:]
                            else:
                                for d in range(0, dim):
                                    vardict[key][skip_gr:skip_gr + ngroups,
                                                 d] = a[:, d]

                    skip_gr += ngroups
            #SUBHALOS
            if subcat:
                if nsubs > 0:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Subhalo", key):
                            val = sub_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            a = hdf5lib.GetData(f, "Subhalo/" + key)
                            if dim == 1:
                                vardict[key][skip_sub:skip_sub + nsubs] = a[:]
                            else:
                                for d in range(0, dim):
                                    vardict[key][skip_sub:skip_sub + nsubs,
                                                 d] = a[:, d]

                    skip_sub += nsubs

            f.close()

            filenum += 1
            if filenum == nfiles: doneflag = True
示例#6
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