def _get_simba_property(self,ptype,prop): from readgadget import readsnap snapfile = ('%s/%s'%(self.ds.fullpath,self.ds.basename)) # set up units coming out of pygr prop_unit = {'mass':'Msun', 'pos':'kpccm', 'vel':'km/s', 'pot':'Msun * kpccm**2 / s**2', 'rho':'g / cm**3', 'sfr':'Msun / yr', 'u':'K', 'Dust_Masses':'Msun', 'bhmass':'Msun', 'bhmdot':'Msun / yr', 'hsml':'kpccm'} # damn you little h! if prop == 'mass' or prop == 'pos': hfact = 1./self.ds.hubble_constant elif prop == 'rho': hfact = self.ds.hubble_constant**2 else: hfact = 1. # deal with differences in pygr vs. yt/caesar naming if ptype == 'bh': ptype = 'bndry' if prop == 'temperature': prop = 'u' if prop == 'haloid' or prop == 'dustmass' or prop == 'aform' or prop == 'bhmass' or prop == 'bhmdot': prop = self.get_property_name(ptype, prop) # read in the data data = readsnap(snapfile, prop, ptype, units=1, suppress=1) * hfact # set to doubles if prop == 'HaloID' or prop == 'particle_index': # this fixes a bug in our Gizmo, that HaloID is output as a float! data = data.astype(np.int64) else: data = data.astype(np.float32) if prop in prop_unit.keys(): data = self.ds.arr(data, prop_unit[prop]) else: data = self.ds.arr(data, '') return data
def collect_group_IDs(obj, data_type, part_type, snap_dir): """Collates list of particle and associated group IDs for all specified objects. Returns particle and group ID lists, and a hash list of indexes for particle IDs corresponding to the starting index of each group. Parameters ---------- obj : :class:`main.CAESAR` Caesar object for which to collect group IDs data_type : str 'halo', 'galaxy', or 'cloud' part_type : str Particle type snap_dir : str Path where snapshot files are located; if None, uses obj.simulation.fullpath """ # read in particle IDs from readgadget import readsnap if snap_dir is None: #snapfile = obj.simulation.fullpath.decode('utf-8')+'/'+obj.simulation.basename.decode('utf-8') snapfile = obj.simulation.fullpath.decode( 'utf-8') + '/' + obj.simulation.basename.decode('utf-8') else: snapfile = snap_dir + '/' + obj.simulation.basename.decode('utf-8') all_pids = np.array(readsnap(snapfile, 'pid', part_type, suppress=1), dtype=np.uint64) from caesar.fubar_halo import plist_dict if data_type == 'halo': part_list = 'obj.halos[i].%s' % plist_dict[part_type] ngroups = len(obj.halos) elif data_type == 'galaxy': part_list = 'obj.galaxies[i].%s' % plist_dict[part_type] ngroups = len(obj.galaxies) elif data_type == 'cloud': part_list = 'obj.clouds[i].%s' % plist_dict[part_type] ngroups = len(obj.clouds) # count number of total particles in groups npart = 0 for i in range(ngroups): mylist = eval(part_list) npart += len(mylist) # fill particle and group ID lists pids = np.zeros(npart, dtype=np.int64) gids = np.zeros(npart, dtype=np.int32) pid_hash = np.zeros(ngroups, dtype=np.int64) count = 0 for i in range(ngroups): mylist = eval(part_list) pids[count:count + len(mylist)] = all_pids[mylist] gids[count:count + len(mylist)] = np.full(len(mylist), i) pid_hash[i] = count count += len(mylist) pid_hash = np.append(pid_hash, npart + 1) return ngroups, pids, gids, pid_hash
def info(obj, snapFile, top=None, savetxt=False): """ Customized version to print more info on the CAESAR extracted galaxies. Parameters ---------- obj : :class:`main.CAESAR` Main CAESAR object. top : int Number of objects to print. """ from readgadget import readsnap from yt2caesar import group_part_by_galaxy, get_partmasses_from_snapshot group_list = obj.galaxies[:] nobjs = len(group_list) if top is None: top = nobjs elif top > nobjs: top = nobjs if obj.simulation.cosmological_simulation: time = 'z=%0.3f' % obj.simulation.redshift else: time = 't=%0.3f' % obj.simulation.time output = '##\n' output += '## Largest %d Galaxies \n' % (top) if hasattr(obj, 'data_file'): output += '## from: %s\n' % obj.data_file output += '## %d @ %s' % (nobjs, time) output += '##\n' output += '##\n' cnt = 1 # get molecular gas fraction of each particle from snapshot file gfH2_p = readsnap(snapFile,'fH2','gas', units=1) gas_p_m = get_partmasses_from_snapshot(snapFile, obj, ptype='gas') output += '## ID Mstar Mgas MBH fedd SFR [Msun/yr] SFRSD [Msun/yr/kpc^2] SFRSD_r_stellar_half_mass [Msun/yr/kpc^2] gasSD [Msun/pc^2] r_baryon r_gas r_gas_half_mass r_stellar r_stellar_half_mass Z_sfrWeighted [/Zsun] Z_massWeighted [/Zsun] Z_stellar [/Zsun] T_gas_massWeighted T_gas_SFRWeighted fgas f_h2_fromSnap DGR nrho Central\t| Mhalo_parent HID\n' output += '## ----------------------------------------------------------------------------------------\n' h = obj.simulation.hubble_constant bhmdot = readsnap(snapFile, 'BH_Mdot','bndry',suppress=1)*1.e10/h/3.08568e+16*3.155e7 # in Mo/yr # debug mbh: # mmm = [g.masses['bh'] for g in obj.galaxies] # mmm[:10] for ii, o in enumerate(group_list): phm, phid = -1, -1 if o.halo is not None: phm, phid = o.halo.masses['total'], o.halo.GroupID try: bhmdots = [bhmdot[k] for k in o.bhlist] bm = o.masses['bh'] imax = np.argmax(bm) try: bm = bm[imax] bmdot = bhmdots[imax] # only the massive BH particle matters. except: bm = bm bmdot = bhmdots frad = 0.1 mdot_edd = 4*np.pi*6.67e-8*1.673e-24/(frad*3.e10*6.65245e-25) * bm * 3.155e7 # in Mo/yr fedd = bmdot / mdot_edd fedd = fedd[0].value except: bm = 0 fedd = 0 # print bm, fedd # import pdb; pdb.set_trace() # Calculate mass-weighted f_h2 of each gal gas_f_H2 = group_part_by_galaxy(gfH2_p, o, ptype='gas') gas_m = group_part_by_galaxy(gas_p_m, o, ptype='gas') output += ' %04d %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.2e %0.3f %0.3f %0.3f %0.2e %0.2e %0.3f %0.2e %.2e %.2e %s\t| %0.2e %d \n' % \ (o.GroupID, o.masses['stellar'], o.masses['gas'], bm, fedd, o.sfr, o.sfr/np.pi/o.radii['gas'].in_units('kpc')**2, o.sfr/np.pi/o.radii['stellar_half_mass'].in_units('kpc')**2, o.masses['gas']/np.pi/(o.radii['gas'].in_units('kpc')*1.e3)**2, o.radii['baryon'].in_units('kpc'), o.radii['gas'].in_units('kpc'), o.radii['gas_half_mass'].in_units('kpc'), o.radii['stellar'].in_units('kpc'), o.radii['stellar_half_mass'].in_units('kpc'), o.metallicities['sfr_weighted']/0.0134, o.metallicities['mass_weighted']/0.0134, o.metallicities['stellar']/0.0134, o.temperatures['mass_weighted'], o.temperatures['sfr_weighted'], o.gas_fraction, # = Mgas / (Mg + Ms) np.sum(gas_f_H2 * gas_m)/np.sum(gas_m), # mass-weighted f_h2 o.masses['gas']/o.masses['dust'], o.local_number_density, o.central, phm, phid) cnt += 1 if cnt > top: break # print(output) if savetxt: outputFileName = aux_filename(snapFile, top) f = open(outputFileName, 'w') f.write(output) f.close() return output, outputFileName
gal_sm = np.array([i.masses['stellar'].in_units('Msun') for i in sim.galaxies]) gal_sfr = np.array([i.sfr.in_units('Msun/yr') for i in sim.galaxies]) gal_ssfr = gal_sfr / gal_sm gal_sm = np.log10(gal_sm) gal_ssfr = np.log10(gal_ssfr) with h5py.File(halflight_file, 'r') as f: gal_rad = f['abs_' + model + '_' + wind + '_' + snap][:] # these are in pkpc gal_rad = np.sum(gal_rad, axis=0) / 3. gal_ids = np.array([True for i in range(len(sim.galaxies))]) gal_ids = np.arange(len(gal_ids))[gal_ids] # load in star particle data with readsnap star_pos = readsnap(snapfile, 'pos', 'star', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc star_mass = readsnap(snapfile, 'mass', 'star', suppress=1, units=1) / h # in Mo star_vels = readsnap(snapfile, 'vel', 'star', suppress=1, units=0) # in km/s star_tform = readsnap(snapfile, 'age', 'star', suppress=1, units=1) # expansion times at time of formation gas_pos = readsnap(snapfile, 'pos', 'gas', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc gas_mass = readsnap(snapfile, 'mass', 'gas', suppress=1, units=1) / h # in Mo gas_sfr = readsnap(snapfile, 'sfr', 'gas', suppress=1, units=1) # in Mo/yr gas_h1 = readsnap(snapfile, 'NeutralHydrogenAbundance', 'gas', suppress=1,
xmin = -20. # [kpc] xmax = 20. # [kpc] Npixels = 100 #512 DR = 1. # kpc z = obj.simulation.redshift masses = np.array([i.masses['stellar'] for i in obj.galaxies]) sfr = np.array([i.sfr for i in obj.galaxies]) mask_mass = (masses > 4e10) & (masses < 6e10) mask_sfr = (sfr > 0.5) & (sfr < 2.5) #mw_gal_no = np.arange(0, len(masses))[mask_mass*mask_sfr] mw_gal_no = np.arange(0, len(masses))[mask_mass] star_positions = readsnap(snap, 'pos', 'star', suppress=1, units=1) / (1.+z) star_vels = readsnap(snap, 'vel', 'star', suppress=1, units=0) star_mass = readsnap(snap, 'mass', 'star', suppress=1, units=1) / (h*10**7.) names = ['edge_on', 'face_on'] vecs = [np.array([0,1,0]), np.array([0,0,1])] faceon = np.zeros((len(obj.galaxies), 5)) edgeon = np.zeros((len(obj.galaxies), 5)) b_t = np.zeros(len(obj.galaxies)) for i in mw_gal_no: print 'galaxy: ' + str(i) slist = obj.galaxies[i].slist gal_mass = obj.galaxies[i].masses['stellar'].in_units('Msun') gal_pos = obj.galaxies[i].pos.in_units('kpc/h')
def simba_to_pd(galnames, raw_sim_dir, raw_sim_name_prefix, caesar_dir, name_prefix, redshiftFile, d_data, startGAL=None, endGAL=None, debugplotgas=False, R_max=20.0, verbose=False, debug=False): """ Write out useful fields gas, stars, DM particles of the selected galaxies into DataFrames Parameters ---------- galnames: str or list of string if it's a simple str, it's a file that stores the output from return from select_SFgal_from_simba() if it's a list of str, it's output from select_SFgal_from_simba() raw_sim_dir: string where to look for raw snapshot files raw_sim_name_prefix: string prefix to the speciifc volume and resolution of snapshots we are looking caesar_dir: str path to the caesar output files name_prefix: str predix to file of caesar outputs redshiftFile: str where to look for the redshift info for this particular set of snapshots d_data: str directory location to save dataframe files startGAL: None or int snapshot number to begin extraction. Use to pseudo-parallelized things startGAL <= GAL < endGAL endGAL: None or int snapshot number to end extraction. Use to pseudo-parallelized things plotgas: bool whether or not to plot gas distribution of galaxy. Uesful for e.g., looking at the galaxy position and see if it might be the COM of two overlapping galaxies - see Romeel's email on Apr 4th, 2019. R_max: float physical kpc for projection plot Returns ------- galnames_selected: list of string galaxies names indicating the halo ID, snapshot number, and galaxy ID (numbered based on some predefined criterion) zreds_selected: numpy array of float redshifts of galaxies """ try: import cPickle as pickle except: import _pickle as pickle import caesar import yt import numpy as np import pandas as pd import os from readgadget import readsnap kpc2m = 3.085677580666e19 resort = False # Save the names and redshift for the galaxies that we finally decide to save in DataFrames: galnames_selected = [] zreds_selected = np.array([]) if type(galnames) is str: try: with open(galnames, 'r') as f: galnames = pickle.load(f) except: pass try: print(galnames['halo'].values, galnames['snap'].values, galnames['GAL'].values, galnames['SFR'].values) except TypeError: galnames = galnames[0] if (sorted(galnames['snap'].values) == galnames['snap'].values ).all() == False: # then we have to sort galnames by 'snap' galnames = galnames.sort_values( ['snap', 'SFR'], ascending=[False, False]).reset_index( drop=False) # drop: avoid old index being added as a column resort = True snap_hold = [] for num, (halo, snap, GAL, sfr) in enumerate( zip(galnames['halo'].values, galnames['snap'].values, galnames['GAL'].values, galnames['SFR'].values)): if num == 0: snap_hold = snap s, h, obj, ds, zred = define_ds(snap, raw_sim_dir, raw_sim_name_prefix, caesar_dir, name_prefix, redshiftFile) # get H2 fraction from snapfile, instead of from YT sphere - faster way to weed out galaxies that are too small/with too few dense gas particles. snapFile = raw_sim_dir + raw_sim_name_prefix + '{:0>3}'.format( int(snap)) + '.hdf5' gfH2 = readsnap(snapFile, 'fH2', 'gas') gas_p_m = get_partmasses_from_snapshot(snapFile, obj, ptype='gas') else: if snap != snap_hold: s, h, obj, ds, zred = define_ds(snap, raw_sim_dir, raw_sim_name_prefix, caesar_dir, name_prefix, redshiftFile) snap_hold = snap # if we have sorted galnames by ('snap', SFR of each snap), we have to do so for obj too. if resort: gal = obj.galaxies obj.galaxies = [gal[i] for i in galnames.index.values] # if we want to split up exection of this script into multiple nodes. if startGAL is None: startGAL = 0 else: startGAL = int(startGAL) if endGAL is None: endGAL = len(galnames.index) else: endGAL = int(endGAL) assert startGAL < len(galnames.index) assert endGAL <= len(galnames.index) assert startGAL < endGAL if startGAL <= GAL < endGAL: print( '\nNow looking at galaxy # %s with parent halo ID %s in snapshot %s at z = %s' % (int(GAL), int(halo), int(snap), zred)) print('Creating galaxy name:') galname = 'h' + str(int(halo)) + '_s' + \ str(int(snap)) + '_G' + str(int(GAL)) print(galname) galaxy = obj.galaxies[GAL] # Exclude galaxies based on global properties to speed things up. TO BE IMPLEMENTED if galaxy.sfr < 0.1: print("SFR too low.. Skipping") continue galaxy_fH2 = np.array([gfh2[k] for k in obj.galaxies.glist]) assert len(galaxy_fH2) == len(galaxy.masses['gas']) if (galaxy.masses['gas'] * galaxy_fH2).sum() <= 1.e5: print("Dense gas mass less than 10^5 Msun.. Skipping") continue if len(galaxy.slist) < 64 or len(galaxy.glist) < 64: print( "Too few star particles or gas particles, unlikely to be real galaxy or useful for our purpose" ) continue if galaxy.masses['gas'] == 0.0 or galaxy.masses['stellar'] == 0.0: # then don't bother getting any info on this galaxy if galaxy.masses['gas'] == 0.0: print("Don't bother with this galaxy, it has no gas mass.") elif galaxy.masses['stellar'] == 0.0: print( "Don't bother with this galaxy, it has no stellar mass." ) continue simgas_path = (d_data + 'particle_data/sim_data/z' + '{:.2f}').format( float(zred)) + '_' + galname + '_sim.gas' simstar_path = (d_data + 'particle_data/sim_data/z' + '{:.2f}').format( float(zred)) + '_' + galname + '_sim.star' simdm_path = (d_data + 'particle_data/sim_data/z' + '{:.2f}').format( float(zred)) + '_' + galname + '_sim.dm' if not os.path.exists(simgas_path) and not os.path.exists( simstar_path) and not os.path.exists(simdm_path): # Get location and radius for each galaxy belonging to this haloID: loc = galaxy.pos # .in_units('unitary') R_gal = galaxy.radius # kpccm, i.e., co-moving # print(galaxy.radii) print('Cut out a sphere with radius %s, %s' % (R_gal, R_gal.in_units('kpc'))) sphere = ds.sphere(loc, R_gal) gas_pos = sphere['PartType0', 'Coordinates'].in_units('kpc') print('%s SPH particles' % len(gas_pos)) star_pos_all = sphere['PartType4', 'Coordinates'].in_units('kpc') print('%s Star particles' % len(star_pos_all)) if debug: print("List all stuff inside the raw sim .hdf5") rawSim = raw_sim_dir + raw_sim_name_prefix + \ '{:>03}'.format(int(snap)) + '.hdf5' os.system('h5ls -r ' + rawSim) print("") print(ds.field_list) if len(gas_pos) > 0 and len(star_pos_all) > 0: print('Extracting all gas particle properties...') gas_pos = gas_pos - loc # rotation to ensure that the galaxies lie in the xy-plane. That makes it easier to visualize later, and you can always add random viewing angles later as well. gas_pos = caesar.utils.rotator( gas_pos, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) gas_posx, gas_posy, gas_posz = gas_pos[:, 0].d, gas_pos[:, 1].d, gas_pos[:, 2].d gas_vel = sphere['PartType0', 'Velocities'].in_cgs() / 1.e5 gas_vel = caesar.utils.rotator( gas_vel, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) gas_velx, gas_vely, gas_velz = gas_vel[:, 0].d, gas_vel[:, 1].d, gas_vel[:, 2].d print("gas_velx length: ", len(gas_velx)) gas_densities = sphere['PartType0', 'Density'].in_cgs() gas_f_H2 = sphere['PartType0', 'FractionH2'] gas_f_neu = sphere['PartType0', 'NeutralHydrogenAbundance'] gas_m = sphere['PartType0', 'Masses'].in_units('Msun') # electrons per Hydrogen atom (max: 1.15) gas_x_e = sphere['PartType0', 'ElectronAbundance'] # ionized gas mass fraction (because we don't trust # NeutralHydrogenAbundance -- which only includes atomic gas but not moleuclar) # At the end, calculated from gas_f_ion = gas_x_e / max(gas_x_e) gas_f_HI = 1 - gas_f_ion print( '\nChecking molecular gas mass fraction from simulation:' ) print('%.3s %% \n' % (np.sum(gas_m * gas_f_H2) / np.sum(gas_m) * 100.)) # Tk gas_Tk = sphere['PartType0', 'Temperature'] gas_h = sphere['PartType0', 'SmoothingLength'].in_units('kpc') # Tk # Msun/yr gas_SFR = sphere['PartType0', 'StarFormationRate'] # gas_SFR = galaxy.sfr gas_Z = sphere['PartType0', 'Metallicity_00'].d / \ 0.0134 # from RD gas_a_He = sphere['PartType0', 'Metallicity_01'].d gas_a_C = sphere['PartType0', 'Metallicity_02'].d gas_a_N = sphere['PartType0', 'Metallicity_03'].d gas_a_O = sphere['PartType0', 'Metallicity_04'].d gas_a_Ne = sphere['PartType0', 'Metallicity_05'].d gas_a_Mg = sphere['PartType0', 'Metallicity_06'].d gas_a_Si = sphere['PartType0', 'Metallicity_07'].d gas_a_S = sphere['PartType0', 'Metallicity_08'].d gas_a_Ca = sphere['PartType0', 'Metallicity_09'].d gas_a_Fe = sphere['PartType0', 'Metallicity_10'].d print('Extracting all star particle properties...') star_pos = star_pos_all - loc star_pos = caesar.utils.rotator( star_pos, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) star_posx, star_posy, star_posz = star_pos[:, 0].d, star_pos[:, 1].d, star_pos[:, 2].d star_vel = sphere['PartType4', 'Velocities'].in_cgs() / 1e5 star_vel = caesar.utils.rotator( star_vel, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) star_velx, star_vely, star_velz = star_vel[:, 0].d, star_vel[:, 1].d, star_vel[:, 2].d star_m = sphere['PartType4', 'Masses'].in_units('Msun') # star_m = galaxy.masses['stellar'] star_a_C = sphere['PartType4', 'Metallicity_02'].d star_a_O = sphere['PartType4', 'Metallicity_04'].d star_a_Si = sphere['PartType4', 'Metallicity_07'].d star_a_Fe = sphere['PartType4', 'Metallicity_10'].d star_Z = sphere['PartType4', 'Metallicity_00'].d / \ 0.0134 # from RD print('Info for this snapshot:') omega_baryon = obj.simulation.omega_baryon omega_matter = obj.simulation.omega_matter hubble_constant = obj.simulation.hubble_constant print('Omega baryon: %s' % omega_baryon) print('Omega matter: %s' % omega_matter) print('Hubble constant: %s' % hubble_constant) print('XH: %s' % obj.simulation.XH) current_time = ds.current_time.in_units('yr') / 1.e6 # Myr # in scale factors, do as with Illustris star_formation_a = sphere['PartType4', 'StellarFormationTime'].d star_formation_z = 1. / star_formation_a - 1 # Code from yt project (yt.utilities.cosmology) star_formation_t = 2.0 / 3.0 / np.sqrt( 1 - omega_matter) * np.arcsinh( np.sqrt((1 - omega_matter) / omega_matter) / np.power(1 + star_formation_z, 1.5)) / ( hubble_constant) # Mpc*s/(100*km) star_formation_t *= kpc2m / 100. / (1e6 * 365.25 * 86400 ) # Myr star_age = current_time.d - star_formation_t print('Extracting all DM particle properties...') dm_pos_all = sphere['PartType1', 'Coordinates'].in_units('kpc') dm_pos = dm_pos_all - loc dm_pos = caesar.utils.rotator( dm_pos, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) dm_posx, dm_posy, dm_posz = dm_pos[:, 0].d, dm_pos[:, 1].d, dm_pos[:, 2].d dm_vel = sphere['PartType1', 'Velocities'].in_cgs() / 1e5 dm_vel = caesar.utils.rotator( dm_vel, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) dm_velx, dm_vely, dm_velz = dm_vel[:, 0].d, dm_vel[:, 1].d, dm_vel[:, 2].d dm_m = sphere['PartType1', 'Masses'].in_units('Msun') print('Extracting all BH particle properties...') BH_pos = sphere['PartType5', 'Coordinates'].in_units('kpc') - loc print('%s BH particles' % len(BH_pos)) if len(BH_pos) > 0: BH_pos = caesar.utils.rotator( BH_pos, galaxy.rotation_angles['ALPHA'], galaxy.rotation_angles['BETA']) BH_posx, BH_posy, BH_posz = BH_pos[:, 0].d, BH_pos[:, 1].d, BH_pos[:, 2].d # BH mass # which grows through accretion and mergers w/ other BHs BH_m = sphere['PartType5', 'BH_Mass'].in_units('Msun') # dynamical mass, which enters into gravity calculation BH_m2 = sphere['PartType5', 'Masses'].in_units('Msun') BH_mdot = sphere['PartType5', 'BH_Mdot'] # the one that actually matters is the most massive BH particle idx = np.where(BH_m == BH_m.max()) BH_m = BH_m[idx] BH_mdot = BH_mdot[idx] print(BH_m, BH_mdot) simbh = pd.DataFrame({ 'mBH_Msun': BH_m, 'mdot': BH_mdot }) simbh.to_pickle(d_data + 'particle_data/sim_data/z' + '{:.2f}'.format(float(zred)) + '_' + galname + '_sim.bh') # Put into dataframes: simgas = pd.DataFrame({ 'x': gas_posx, 'y': gas_posy, 'z': gas_posz, 'vx': gas_velx, 'vy': gas_vely, 'vz': gas_velz, 'SFR': gas_SFR, 'Z': gas_Z, 'nH': gas_densities, 'Tk': gas_Tk, 'h': gas_h, 'f_HI1': gas_f_HI, # atomic and molecular H 'f_neu': gas_f_neu, # atomic H 'f_H21': gas_f_H2, 'm': gas_m, 'a_He': gas_a_He, 'a_C': gas_a_C, 'a_N': gas_a_N, 'a_O': gas_a_O, 'a_Ne': gas_a_Ne, 'a_Mg': gas_a_Mg, 'a_Si': gas_a_Si, 'a_S': gas_a_S, 'a_Ca': gas_a_Ca, 'a_Fe': gas_a_Fe }) simstar = pd.DataFrame({ 'x': star_posx, 'y': star_posy, 'z': star_posz, 'vx': star_velx, 'vy': star_vely, 'vz': star_velz, 'Z': star_Z, 'm': star_m, 'age': star_age }) simdm = pd.DataFrame({ 'x': dm_posx, 'y': dm_posy, 'z': dm_posz, 'vx': dm_velx, 'vy': dm_vely, 'vz': dm_velz, 'm': dm_m }) # Calculate approximate 100 Myr average of SFR for this galaxy m_star = simstar['m'].values age_star = simstar['age'].values SFR_avg = sum(m_star[age_star < 100]) / 100.e6 print( 'Estimated SFR for this galaxy averaged over past 100 Myr: ' + str(SFR_avg)) print('SFR in simulation: %s' % galaxy.sfr) print('SFR for parent halo: %s' % galaxy.halo.sfr) print('Stellar mass: %s Msun' % np.sum(m_star)) print('Dark matter mass: %s ' % np.sum(dm_m)) print('Saving galaxy data as DataFrames in {}').format( d_data) # replace w/ this line if we decide to merge this func to load_module.py and call via parameters_zx.txt # print('Saving galaxy data as DataFrames in {}, as defined in __init__.py').format(d_data) savepath = d_data + 'particle_data/sim_data/' if not os.path.exists(savepath): os.makedirs(savepath) simgas.to_pickle(simgas_path) simstar.to_pickle(simstar_path) simdm.to_pickle(simdm_path) if plotgas: import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import AxesGrid # projection plot plt.close('all') savepath = 'plots/sims/' if not os.path.exists(savepath): os.makedirs(savepath) ppp = yt.ProjectionPlot( ds, 0, [('gas', 'density')], center=sphere.center.value, width=(R_max, 'kpc'), # center='c', weight_field='density') try: ppp.annotate_timestamp(corner='upper_left', redshift=True, time=False, draw_inset_box=True) #p.annotate_scale(corner='upper_right') ppp.annotate_particles((R_max, 'kpc'), p_size=20, ptype='PartType5', minimum_mass=1.e7) except: pass filename = 'z' + '{:.2f}'.format( float(zred)) + '_' + galname + '_gas.pdf' p.save(os.path.join(savepath, filename)) elif len(gas_pos) == 0 or len(star_pos_all) == 0: if len(star_pos_all) == 0: print( "Not saving this galaxy {:} because it has no stellar mass..." ).format(galname) elif len(gas_pos) == 0: print( "Not saving this galaxy {:} because it has no gas mass..." ).format(galname) if debug: import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import AxesGrid # projection plot plt.close('all') savepath = 'plots/sims/' if not os.path.exists(savepath): os.makedirs(savepath) filename = 'z' + '{:.2f}'.format( float(zred)) + '_' + galname + '_gas.pdf' if not os.path.isfile(filename): print("Plotting gas particles...") ppp = yt.ProjectionPlot( ds, 0, [('gas', 'density')], center=sphere.center.value, width=(R_max, 'kpc'), # center='c', weight_field='density') try: ppp.annotate_timestamp(corner='upper_left', redshift=True, time=False, draw_inset_box=True) #p.annotate_scale(corner='upper_right') ppp.annotate_particles((R_max, 'kpc'), p_size=20, ptype='PartType5', minimum_mass=1.e7) except: pass ppp.save(os.path.join(savepath, filename)) continue else: print("Skipping... Already extracted...") galnames_selected.append(galname) zreds_selected = np.append(zreds_selected, float(zred)) return galnames_selected, zreds_selected
gal_sfr = np.array([i.sfr.in_units('Msun/yr') for i in sim.galaxies]) gal_ssfr = gal_sfr / gal_sm with h5py.File(halflight_file, 'r') as f: gal_rad = f[model + '_' + wind + '_' + snap + '_halflight'][:] # these are in pkpc gal_rad = np.sum(gal_rad, axis=0) / 3. #gal_rad = np.array([i.radii['stellar_half_mass'].in_units('kpc') for i in sim.galaxies]) gal_sm = np.log10(gal_sm[gal_ids * gal_cent]) gal_ssfr = np.log10(gal_ssfr[gal_ids * gal_cent]) gal_rad = gal_rad[gal_ids * gal_cent] gal_ids = np.arange(len(gal_ids))[gal_ids * gal_cent] # load in star particle data with readsnap star_pos = readsnap(snapfile, 'pos', 'star', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc star_mass = readsnap(snapfile, 'mass', 'star', suppress=1, units=1) / h # in Mo gas_pos = readsnap(snapfile, 'pos', 'gas', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc gas_mass = readsnap(snapfile, 'mass', 'gas', suppress=1, units=1) / h # in Mo gas_temp = readsnap(snapfile, 'u', 'gas', suppress=1, units=1) bh_pos = readsnap(snapfile, 'pos', 'bndry', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc no_gals = np.zeros(len(bin_labels)) for j, m in enumerate(masks):
sim = caesar.load(infile,LoadHalo=False) redshift = np.round(sim.simulation.redshift,3) h = sim.simulation.hubble_constant mygals = sim.galaxies print('Doing z=',redshift,'snapshot',snapfile,'with',len(mygals),'galaxies.') # read in galaxy info ids = np.asarray([i.GroupID for i in mygals]) central = np.asarray([i.central for i in mygals]) ms = np.asarray([i.masses['stellar'] for i in mygals]) mbh = np.asarray([i.masses['bh'] for i in mygals]) sfr = np.asarray([i.sfr for i in mygals]) # read in particle info sm = readsnap(snapfile,'mass','star',units=1,suppress=1)/h # Mo sp = readsnap(snapfile,'pos','star',units=1,suppress=1)/(1+redshift)/h # pkpc gm = readsnap(snapfile,'mass','gas',units=1,suppress=1)/h # Mo gp = readsnap(snapfile,'pos','gas',units=1,suppress=1)/(1+redshift)/h # pkpc gsfr = readsnap(snapfile,'sfr','gas',units=1,suppress=1) # Mo/yr # compute rhalf rhalf = np.zeros([3,len(mygals)]) if (MODEL == 'm100n1024') & (SNAP in ['105', '151']): params, mags = get_mags_band(loserfile, mtype, band, only_mtype=True, verbose=True) where = int(np.where(params['bands'] == band)[0]) mags = mags.transpose()[where] else: params, mags = get_mags_band(loserfile, mtype, band, verbose=True) allL = absmag_to_lum(mags)
gal_sm = np.array( [i.masses['stellar'].in_units('Msun') for i in sim.central_galaxies]) gal_sfr = np.array([i.sfr.in_units('Msun/yr') for i in sim.central_galaxies]) gal_ssfr = np.log10(gal_sfr / gal_sm) gal_rad = np.array([ i.radii['stellar_half_mass'].in_units('kpc') for i in sim.central_galaxies ]) richness = np.log10(gal_h1m / gal_sm) mass_mask = np.log10(gal_sm) > 9. n_mask = np.array([(len(i.glist) > n_min) for i in sim.central_galaxies]) mask = mass_mask * n_mask * (gal_h1m > 0.) mask = mass_mask * (gal_h1m > 0.) gal_ids = np.arange(len(sim.central_galaxies))[mask] gas_pos = readsnap(snapfile, 'pos', 'gas', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc gas_mass = readsnap(snapfile, 'mass', 'gas', suppress=1, units=1) / h # in Mo gas_h1 = readsnap(snapfile, 'NeutralHydrogenAbundance', 'gas', suppress=1, units=1) bh_pos = readsnap(snapfile, 'pos', 'bndry', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc h1_radii = np.zeros(len(gal_ids)) for i in range(len(gal_ids)): print('\n')
gal_ssfr = gal_sfr / gal_sm # load in half light radii: with h5py.File(halflight_file, 'r') as f: gal_rad = f[model + '_' + wind + '_' + snap + '_halflight'][:] # these are in pkpc gal_rad = np.sum(gal_rad, axis=0) / 3. # select central galaxies: gal_sm = np.log10(gal_sm[gal_ids * gal_cent]) gal_ssfr = np.log10(gal_ssfr[gal_ids * gal_cent]) gal_rad = gal_rad[gal_ids * gal_cent] gal_ids = np.arange(len(gal_ids))[gal_ids * gal_cent] # load in star particle data with readsnap star_pos = readsnap(snapfile, 'pos', 'star', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc star_tform = readsnap(snapfile, 'age', 'star', suppress=1, units=1) # expansion times at time of formation star_mass = readsnap(snapfile, 'mass', 'star', suppress=1, units=1) / h # in Mo bh_pos = readsnap(snapfile, 'pos', 'bndry', suppress=1, units=1) / ( h * (1. + redshift)) # in kpc no_gals = np.zeros(len(bin_labels)) for j, m in enumerate(masks): # for each mass bin make a mass mask and select those galaxies: print '\n'