def sigma(halo, sim, grps=[1],nbins=50, nangles=10,j_disk_min = 0.8, j_disk_max=1.1, E_cut = None, j_circ_from_r=False, vcen=None, log_interp=False, angmom_size="3 kpc"): sigma = np.zeros(np.size(grps)) cen = np.zeros(np.size(grps)) cnt = 0 for grp in grps: print "calculating sigma for halo number ",grp cen[cnt] = halo.shrink_sphere_center(h[grp])
def BH_Disp_v_BHMass(halos,nhalos=10): for i in range(10): cen = halo.shrink_sphere_center(halos[i+1]) halos[i+1]['pos'] -= cen bhind = np.where(halos[i+1].stars['tform'] < 0) if not np.size(bhind): continue bhdist = halos[i+1].stars[bhind]['r'].in_units('kpc') massbh = halos[i+1].stars[bhind]['mass'].in_units('Msol') plt.plot(massbh,bhdist,'k.') plt.ylabel("Displacement from Halo Center (kpc)") plt.xlabel("BH Mass (Msun)")
def plotOccFrac(sim, centrals=True, rlim=1, cum=True, bins=10): ''' plot the black hole occupation fraction of halos as a function of mass ----inputs----- sim = name of the snapshot you wish to analyze centrals = whether or not you only want to count black holes within rlim from center of halo rlim = the maximum radius from the halo center that you would define a black hole to be "central" cum = whether you want a cumulative distribution bins = number of log bins in halo mass you want ----outputs---- array[occupation fraction] array[mass bins] also a nice plot! ''' stat = readcol.readcol(sim + '.amiga.stat', skipline=1) Mvir = stat[:, 5].astype('float') s = pynbody.load(sim) h = s.halos() bhgrps = s.stars['amiga.grp'][(s.stars['tform'] < 0)] print "calculating Min and Max halo masses..." MvirMin = h[int(bhgrps.max())]['mass'].in_units('Msol').sum() MvirMax = h[1]['mass'].in_units('Msol').sum() print "Min: ", MvirMin, " Max: ", MvirMax dLogM = (np.log10(MvirMax) - np.log10(MvirMin)) / bins BHFlag = np.zeros(bhgrps.max()) ugrps = np.unique(bhgrps) print "there are ", len(ugrps), "halos with BHs" print "determining existence of central BHs..." for i in ugrps: if i == 0: continue print "halo ", i cen = halo.shrink_sphere_center(h[i]) h[i].stars['pos'] -= cen if len(h[i].stars[((h[i].stars['tform'] < 0) & (h[i].stars['r'].in_units('kpc') < rlim))]) > 0: BHFlag[i - 1] = 1 h[i].stars['pos'] += cen occNum, Mbins = np.histogram(np.log10(Mvir[np.arange(bhgrps.max())]), bins=bins, weights=BHFlag) HNum, Mbins2 = np.histogram(np.log10(Mvir[np.arange(bhgrps.max())]), bins=bins) if cum == True: occFrac = np.cumsum(occNum) / np.cumsum(HNum).astype('float') else: occFrac = occNum / HNum.astype('float') return Mbins, occFrac
def plotOccFrac(sim,centrals=True,rlim=1,cum=True,bins=10): ''' plot the black hole occupation fraction of halos as a function of mass ----inputs----- sim = name of the snapshot you wish to analyze centrals = whether or not you only want to count black holes within rlim from center of halo rlim = the maximum radius from the halo center that you would define a black hole to be "central" cum = whether you want a cumulative distribution bins = number of log bins in halo mass you want ----outputs---- array[occupation fraction] array[mass bins] also a nice plot! ''' stat = readcol.readcol(sim+'.amiga.stat',skipline=1) Mvir = stat[:,5].astype('float') s = pynbody.load(sim) h = s.halos() bhgrps = s.stars['amiga.grp'][(s.stars['tform']<0)] print "calculating Min and Max halo masses..." MvirMin = h[bhgrps.max()]['mass'].in_units('Msol').sum() MvirMax = h[1]['mass'].in_units('Msol').sum() print "Min: ", MvirMin, " Max: ", MvirMax dLogM = (np.log10(MvirMax) - np.log10(MvirMin))/bins BHFlag = np.zeros(bhgrps.max()) ugrps = np.unique(bhgrps) print "there are ", len(ugrps), "halos with BHs" print "determining existence of central BHs..." for i in ugrps: if i == 0: continue print "halo ", i cen = halo.shrink_sphere_center(h[i]) h[i].stars['pos'] -= cen if len(h[i].stars[((h[i].stars['tform']<0)&(h[i].stars['r'].in_units('kpc')<rlim))])>0: BHFlag[i-1] = 1 h[i].stars['pos'] += cen occNum, Mbins = np.histogram(np.log10(Mvir[np.arange(bhgrps.max())]),bins=bins,weights = BHFlag) HNum, Mbins2 = np.histogram(np.log10(Mvir[np.arange(bhgrps.max())]),bins=bins) if cum==True: occFrac = np.cumsum(occNum)/np.cumsum(HNum).astype('float') else: occFrac = occNum/HNum.astype('float') return Mbins,occFrac
def BHHM_relation(halos,nhalos=10,center=True,massive=False): counter = 1 while counter<=nhalos: print counter bhind = np.where(halos[counter].stars['tform'] < 0) if not np.size(bhind): continue Mhalo = halos[counter]['mass'].in_units('Msol').sum() Mbh = halos[counter].stars[bhind]['mass'].in_units('Msol') if massive: MassBH = Mbh[np.where(Mbh == Mbh.max())] plt.plot([Mhalo],MassBH,'k.') if center: cen = halo.shrink_sphere_center(halos[counter]) halos[counter]['pos'] -= cen bhdist = halos[counter].stars[bhind]['r'].in_units('kpc') MassBH = Mbh[np.where(bhdist == bhdist.min())] plt.plot([Mhalo],MassBH,'b.') counter += 1 plt.ylabel("BH Mass (Msun)") plt.xlabel("Halo Mass (Msun)")
def BH_Disp(halos,nhalos=10,massive=False,title=False): for i in range(10): cen = halo.shrink_sphere_center(halos[i+1]) halos[i+1]['pos'] -= cen bhind = np.where(halos[i+1].stars['tform'] < 0) print np.size(bhind) if not np.size(bhind): continue bhdist = halos[i+1].stars[bhind]['r'].in_units('kpc') Mhalo = halos[i+1]['mass'].in_units('Msol').sum() if (not massive): for ii in range(np.size(bhind)): plt.plot([Mhalo],[bhdist[ii]],'k.') else: if massive: mass = halos[i+1].stars[bhind]['mass'] MassDist = bhdist[np.where(mass == mass.max())] print MassDist plt.plot([Mhalo],[MassDist],'k.') if title: plt.title(title) else: plt.title("Most Massive BHs") plt.ylabel("Distance from Halo Center (kpc)") plt.xlabel("Halo Mass (Msun)")
def getBHhalo(simname,findcenter='mass',filename=None, initFile=None): if not os.path.exists("grpfiles.list"): simname_split = simname.split('.') num = len(simname_split) os.system('ls '+simname+'.00*.grp | cut -d "." -f1-'+str(num+1)+ '> grpfiles.list' ) if filename: if os.path.exists(filename): print "file", filename, "already exists! reading it in and appending it with new data" f = open(filename,'rb') BHhaloOLD = pickle.load(f) f.close() startStep = len(BHhaloOLD['haloID'][0]) os.system('rm '+filename) print "old file has", startStep, "halos already completed" else: startStep = 0 if initFile: if os.path.exists(initFile): print "found file ", initFile, "reading it in now" f = open(initFile,'rb') BHhaloOLD = pickle.load(f) f.close() startStep = len(BHhaloOLD['haloID'][0]) print "given file has", startStep, "halos already completed" if initFile==filename: print "given file has same name as target file... deleting old file to replace with new file" os.system('rm '+filename) if initFile==None: startStep = 0 f= open("grpfiles.list") munits = 'Msol' vunits = 'km s**-1' posunits = 'kpc' if findcenter != 'mass' and findcenter != 'pot': print 'Warning: findcenter = mass or pot. Cannot understand given input, reverting to shrink_sphere_center for halo center calculation.' findcenter = 'mass' print "finding BH iords..." bhorbitdata = readcol.readcol(simname+'.orbit') bhiords = np.unique(bhorbitdata[:,0]) files = f.readlines() nsteps = len(files) - startStep nbh = len(bhiords) print "cleaning up... and initializing arrays" del(bhorbitdata) bhmass = array.SimArray(np.zeros((nbh,nsteps)),munits) haloid = np.zeros((nbh,nsteps)) mhalo = array.SimArray(np.zeros((nbh,nsteps)),munits) mdark = array.SimArray(np.zeros((nbh,nsteps)),munits) mstar = array.SimArray(np.zeros((nbh,nsteps)),munits) mgas = array.SimArray(np.zeros((nbh,nsteps)),munits) vhalo = array.SimArray(np.zeros((nbh,nsteps,3)),vunits) dist = array.SimArray(np.zeros((nbh,nsteps)),posunits) bhpos = array.SimArray(np.zeros((nbh,nsteps,3)),posunits) bhvel = array.SimArray(np.zeros((nbh,nsteps,3)),vunits) halocen = array.SimArray(np.zeros((nbh,nsteps,3)),posunits) halorad = array.SimArray(np.zeros((nbh,nsteps)),posunits) scaleFac = np.zeros((nbh,nsteps)) rho = array.SimArray(np.zeros((nbh,nsteps)),'g cm**-3') cs = array.SimArray(np.zeros((nbh,nsteps)),'cm s**-1') for stepcnt in range(nsteps): line = files[stepcnt+startStep].strip() print "getting halo information for ", line s = pynbody.load(line) h = s.halos() if not np.size(s.star[(s.star['tform']<0)]): print "no BHs in this step! moving on..." continue else: print "there are ", np.size(s.star[(s.star['tform']<0)]), "BHs in the step" print "finding halo centers..." allHaloID = np.unique(s.star['amiga.grp'][(s.star['tform']<0)]) centers = np.zeros((len(allHaloID),3)) velocities = np.zeros((len(allHaloID),3)) radius = np.zeros(len(allHaloID)) for cnt in range(len(allHaloID)): if findcenter=='mass': cen = halo.shrink_sphere_center(h[allHaloID[cnt]]) if findcenter=='phi': cen = h[allHaloID[cnt]]['pos'][(h[allHaloID[cnt]]['phi']==float(h[allHaloID[cnt]]['phi'].min()))] vel = halo.center_of_mass_velocity(h[allHaloID[cnt]]) h[allHaloID[cnt]]['pos'] -= cen rad = h[allHaloID[cnt]]['r'].max() centers[cnt,:] = cen.in_units(posunits,a=s.properties['a']) velocities[cnt,:] = vel.in_units(vunits,a=s.properties['a']) radius[cnt] = rad.in_units(posunits,a=s.properties['a']) h[allHaloID[cnt]]['pos'] += cen print "getting BH data for this step..." for cnt in range(nbh): curbh, = np.where(s.star['iord']==bhiords[cnt]) if len(curbh)==0: continue bhmass[cnt,stepcnt] = s.star['mass'].in_units('Msol')[curbh[0]] haloid[cnt,stepcnt] = s.star['amiga.grp'][curbh] mhalo[cnt,stepcnt] = h[haloid[cnt,stepcnt]]['mass'].sum().in_units(munits) mstar[cnt,stepcnt] = h[haloid[cnt,stepcnt]].star['mass'].sum().in_units(munits) mgas[cnt,stepcnt] = h[haloid[cnt,stepcnt]].gas['mass'].sum().in_units(munits) vhalo[cnt,stepcnt,:] = velocities[(allHaloID==haloid[cnt,stepcnt]),:] bhvel[cnt,stepcnt,:] = s.star['vel'].in_units(vunits)[curbh[0]] - vhalo[cnt,stepcnt,:] halocen[cnt,stepcnt,:] = centers[(allHaloID==haloid[cnt,stepcnt]),:] halorad[cnt,stepcnt] = radius[(allHaloID==haloid[cnt,stepcnt])] bhpos[cnt,stepcnt,:] = s.star['pos'].in_units(posunits)[curbh[0]]-halocen[cnt,stepcnt,:] dist[cnt,stepcnt] = np.sqrt((bhpos[cnt,stepcnt,:]**2).sum()) if len(h[haloid[cnt,stepcnt]].gas) > 0: h[haloid[cnt,stepcnt]].gas['pos'] -= s.star['pos'][curbh[0]] o = np.argsort(h[haloid[cnt,stepcnt]].gas['r']) rho[cnt,stepcnt] = h[haloid[cnt,stepcnt]].gas['rho'][o[0:32]].in_units('g cm**-3').mean() cs[cnt,stepcnt] = h[haloid[cnt,stepcnt]].gas['cs'][o[0:32]].in_units('cm s**-1').mean() h[haloid[cnt,stepcnt]].gas['pos'] += s.star['pos'][curbh[0]] else: rho[cnt,stepcnt] = 0 cs[cnt,stepcnt] = 0 scaleFac[cnt,stepcnt] = s.properties['a'] print "deleting stuff" del(s) del(h) if startStep == 0: bhhalo = {'iord':bhiords,'mass':bhmass,'pos':bhpos,'vel':bhvel,'haloID':haloid,'halomass': mhalo,'haloradius':halorad,'halostarmass':mstar,'halodarkmass':mdark,'halogasmass':mgas,'halocen':halocen,'halovel':vhalo,'dist':dist,'scaleFac':scaleFac,'rho':rho,'cs':cs} else: bhhalo = {'iord':bhiords,'mass':bhmass,'pos':bhpos,'vel':bhvel,'haloID':haloid,'halomass': mhalo,'haloradius':halorad,'halostarmass':mstar,'halodarkmass':mdark,'halogasmass':mgas,'halocen':halocen,'halovel':vhalo,'dist':dist,'scaleFac':scaleFac,'rho':rho,'cs':cs} bhhalo['mass'] = np.append(BHhaloOLD['mass'], bhhalo['mass'],axis=1) bhhalo['pos'] = np.append(BHhaloOLD['pos'], bhhalo['pos'],axis=1) bhhalo['vel'] = np.append(BHhaloOLD['vel'], bhhalo['vel'],axis=1) bhhalo['haloID'] = np.append(BHhaloOLD['haloID'], bhhalo['haloID'],axis=1) bhhalo['halomass'] = np.append(BHhaloOLD['halomass'], bhhalo['halomass'],axis=1) bhhalo['haloradius'] = np.append(BHhaloOLD['haloradius'], bhhalo['haloradius'],axis=1) bhhalo['halostarmass'] = np.append(BHhaloOLD['halostarmass'], bhhalo['halostarmass'],axis=1) bhhalo['halodarkmass'] = np.append(BHhaloOLD['halodarkmass'], bhhalo['halodarkmass'],axis=1) bhhalo['halogasmass'] = np.append(BHhaloOLD['halogasmass'], bhhalo['halogasmass'],axis=1) bhhalo['halocen'] = np.append(BHhaloOLD['halocen'], bhhalo['halocen'],axis=1) bhhalo['halovel'] = np.append(BHhaloOLD['halovel'], bhhalo['halovel'],axis=1) bhhalo['dist'] = np.append(BHhaloOLD['dist'], bhhalo['dist'],axis=1) bhhalo['scaleFac'] = np.append(BHhaloOLD['scaleFac'], bhhalo['scaleFac'],axis=1) bhhalo['rho'] = np.append(BHhaloOLD['rho'], bhhalo['rho'],axis=1) bhhalo['cs'] = np.append(BHhaloOLD['cs'], bhhalo['cs'],axis=1) if filename: f = open(str(filename),'wb') pickle.dump(bhhalo,f) f.close() return bhhalo