def half_light_r(sim, band='v'): '''Calculate half light radius Calculates entire luminosity of simulation, finds half that, sorts stars by distance from halo center, and finds out inside which radius the half luminosity is reached. ''' import pynbody import pynbody.filt as f half_l = halo_lum(sim, band=band) * 0.5 max_high_r = np.max(sim.star['r']) test_r = 0.5 * max_high_r testrf = f.LowPass('r', test_r) min_low_r = 0.0 test_l = halo_lum(sim[testrf], band=band) it = 0 while ((np.abs(test_l - half_l) / half_l) > 0.01): it = it + 1 if (it > 20): break if (test_l > half_l): test_r = 0.5 * (min_low_r + test_r) else: test_r = (test_r + max_high_r) * 0.5 testrf = f.LowPass('r', test_r) test_l = halo_lum(sim[testrf], band=band) if (test_l > half_l): max_high_r = test_r else: min_low_r = test_r return test_r * sim.star['r'].units
def getOxygenAbundance(halo, smallify=False): ''' According to Tremonti et al 2004. Set smallify to false if the halo is already cut. ''' temp = 1.5e4 coolgasf = filt.And(filt.LowPass('temp', temp), filt.HighPass('rho', '0.03 m_p cm^-3')) # Tremonti if smallify: try: halo = half_stellar_radius(halo) except Exception as e: print(e) try: coolgash = np.sum(halo.g[coolgasf]['hydrogen']) if coolgash == 0: oxh = 0 print("No cool gas for halo with stellar radius " + str(np.max(halo.star['r'].in_units('kpc')))) else: oxh = np.log10( np.sum(halo.g[coolgasf]['OxMassFrac']) / (16 * coolgash)) + 12 return oxh except Exception as e: print(e) print("Unable to obtain oxh") return None
def getColdGas(halo, temp): """ Based on Peeples 2014 Set smallify to False if the halo being passed is already cut according to radius. Temperature sets the lowpass limit. """ coolgasf = filt.And(filt.LowPass('temp', temp), filt.HighPass('rho', '0.03 m_p cm^-3')) mgascool = np.sum(halo.gas[coolgasf]['mass'].in_units('Msol')) return mgascool
def half_light_r(sim, band='v', cylindrical=False): '''Calculate half light radius Calculates entire luminosity of simulation, finds half that, sorts stars by distance from halo center, and finds out inside which radius the half luminosity is reached. If cylindrical is True compute the half light radius as seen from the z-axis. ''' import pynbody import pynbody.filt as f half_l = halo_lum(sim, band=band) * 0.5 if cylindrical: coord = 'rxy' else: coord = 'r' max_high_r = np.max(sim.star[coord]) test_r = 0.5 * max_high_r testrf = f.LowPass(coord, test_r) min_low_r = 0.0 test_l = halo_lum(sim[testrf], band=band) it = 0 while ((np.abs(test_l - half_l) / half_l) > 0.01): it = it + 1 if (it > 20): break if (test_l > half_l): test_r = 0.5 * (min_low_r + test_r) else: test_r = (test_r + max_high_r) * 0.5 testrf = f.LowPass(coord, test_r) test_l = halo_lum(sim[testrf], band=band) if (test_l > half_l): max_high_r = test_r else: min_low_r = test_r return test_r
def half_stellar_radius(halo): half_sm = np.sum(halo.star['mass'].in_units('Msol'))* 0.5 max_high_r = np.max(halo.star['r'].in_units('kpc')) #print(max_high_r) test_r = 0.5 * max_high_r testrf = filt.LowPass('r', test_r) min_low_r = 0.0 test_sm = np.sum(halo[testrf].star['mass'].in_units('Msol')) it = 0 while ((np.abs(test_sm - half_sm) / half_sm) > 0.01): it = it + 1 if (it > 20): break if (test_sm > half_sm): test_r = 0.5 * (min_low_r + test_r) else: test_r = (test_r + max_high_r) * 0.5 testrf = filt.LowPass('r', test_r) test_sm = np.sum(halo[testrf].star['mass'].in_units('Msol')) if (test_sm > half_sm): max_high_r = test_r else: min_low_r = test_r #print("Half stellar radius found as: {}".format(test_r)) diskf = filt.Sphere(str(test_r) +' kpc') return halo[diskf] # Clean sample: put a cut on 100 stellar particles and hallo mass max based on Moster # Add more observations # Stick with the clean sample 10^9 and make plots for isolated galaxies and whole population # Select all galaxies that do not have any halo similar or larger within 3 virial - flag galaxies as isolated or non-isolated. There should be no other galaxy # Salam isolated - still plot them as single plots
def bulk_processing(tfile, halo_nums): ZSOLAR = 0.0130215 XSOLO = 0.84E-2 #What pynbody uses XSOLH = 0.706 s = pynbody.load(tfile) s.physical_units() h = s.halos() (s.star[filt.LowPass('OxMassFrac', 1e-7)])['OxMassFrac'] = 1e-7 (s.star[filt.LowPass('FeMassFrac', 1e-8)])['FeMassFrac'] = 1e-8 (s.star[filt.LowPass('metals', 1e-7 * ZSOLAR)])['metals'] = 1e-7 * ZSOLAR maxHI = np.amax(s.gas['HI']) f = open(tfile + '.data', 'wb') for halo_num in halo_nums: print(halo_num) halo_ahf = h[int(halo_num)] #h.load_copy(int(halo_num)) halo_ahf = halo_ahf[pynbody.filt.Sphere( 5 * np.std( np.sqrt(halo_ahf['x'].in_units('kpc')**2 + halo_ahf['y'].in_units('kpc')**2 + halo_ahf['z'].in_units('kpc')**2)), (np.mean(halo_ahf['x'].in_units('kpc')), np.mean(halo_ahf['y'].in_units('kpc')), np.mean(halo_ahf['z'].in_units('kpc'))) )] #Filter out outlying particles. This was happening with Sonia. A massive dark matter particle was being included in the halos pynbody.analysis.halo.center(halo_ahf, vel=False) rvir = pynbody.array.SimArray( np.max( np.sqrt(halo_ahf['x'].in_units('kpc')**2 + halo_ahf['y'].in_units('kpc')**2 + halo_ahf['z'].in_units('kpc')**2)), 'kpc') halo = halo_ahf #halo = s[pynbody.filt.Sphere(rvir, (0,0,0))] #This code can be used to select all material within a virial radius, whether AHF considers it part of the halo or not. It presents problems for satellites as then the host halo material is included. stars = halo.star stars.physical_units() #For calculating HI and halo mass currenttime = halo.properties['time'].in_units('Gyr') #For stellar half-mass raidus profile_stellar = pynbody.analysis.profile.Profile( stars, ndin=2, min=0, max=np.ceil(rvir), nbins=int(np.ceil(rvir) / 0.01)) index = np.argmin( np.abs(profile_stellar['mass_enc'] / max(profile_stellar['mass_enc']) - 0.5)) r_half = profile_stellar['rbins'].in_units('kpc')[index] pickle.dump( { 'haloid': halo_num, 'z': s.properties['z'], 'time': currenttime, 'mvir': np.sum(halo['mass'].in_units('Msol')), 'rvir': rvir, 'mgas': np.sum(halo.gas['mass'].in_units('Msol')), 'mstar': np.sum(halo.star['mass'].in_units('Msol')), }, f, pickle.HIGHEST_PROTOCOL) f.close()
bins = 0.5*(binedges[:-1]+binedges[1:]) return hist,bins def write(s,filename='dumbfile.out'): f = pynbody.util.open_(filename,'wb') f.write(struct.pack(">i", len(s))) s.tofile(f) f.close() simname = sys.argv[1] #pp.plt.ion() s = pynbody.load(simname) h = s.halos() diskf = filt.Disc('40 kpc','3 kpc') fifmyrf = filt.LowPass('age','15 Myr') fhmyrf = filt.LowPass('age','500 Myr') twokpcf = filt.Sphere('2 kpc') i=1 if (len(sys.argv) > 2): photiords = np.genfromtxt(sys.argv[2],dtype='i8') frac = np.float(len(np.where(np.in1d(photiords,h[i]['iord']))[0]))/len(photiords) print 'i: %d frac: %.2f'%(i,frac) while(((frac) < 0.5) & (i<100)): i=i+1 frac = np.float(len(np.where(np.in1d(photiords,h[i]['iord']))[0]))/len(photiords) print 'i: %d frac: %.2f'%(i,frac) else: while len(h[i].star) <2: i=i+1 if (i==100): sys.exit()
def getSFR(halo, Myr): fifmyrf = filt.LowPass('age', str(Myr) + ' Myr') return np.sum(halo.star[fifmyrf]['mass'].in_units('Msol')) / (Myr * 10**6)
def write(s, filename='dumbfile.out'): f = pynbody.util.open_(filename, 'wb') f.write(struct.pack(">i", len(s))) s.tofile(f) f.close() simname = sys.argv[1] #pp.plt.ion() s = pynbody.load(simname) h = s.halos() diskf = filt.Disc('40 kpc', '3 kpc') fifmyrf = filt.LowPass('age', '15 Myr') twokpcf = filt.Sphere('2 kpc') i = 1 if (len(sys.argv) > 2): photiords = np.genfromtxt(sys.argv[2], dtype='i8') frac = np.float(len(np.where(np.in1d(photiords, h[i]['iord']))[0])) / len(photiords) print 'i: %d frac: %.2f' % (i, frac) while (((frac) < 0.5) & (i < 100)): i = i + 1 frac = np.float(len(np.where(np.in1d( photiords, h[i]['iord']))[0])) / len(photiords) print 'i: %d frac: %.2f' % (i, frac) else: while len(h[i].star) < 2: i = i + 1