def set_nHI_grid(self, gas=False): """Set up the grid around each halo where the HI is calculated. """ self.once=True #Now grid the HI for each halo files = hdfsim.get_all_files(self.snapnum, self.snap_dir) #Larger numbers seem to be towards the beginning files.reverse() for ff in files: f = h5py.File(ff,"r") print "Starting file ",ff bar=f["PartType0"] ipos=np.array(bar["Coordinates"]) smooth = hsml.get_smooth_length(bar) [self.sub_gridize_single_file(ii,ipos,smooth,bar,self.sub_nHI_grid) for ii in xrange(0,self.nhalo)] f.close() #Explicitly delete some things. del ipos del smooth #Deal with zeros: 0.1 will not even register for things at 1e17. #Also fix the units: #we calculated things in internal gadget /cell and we want atoms/cm^2 #So the conversion is mass/(cm/cell)^2 for ii in xrange(0,self.nhalo): massg=self.UnitMass_in_g/self.hubble*self.hy_mass/self.protonmass epsilon=2.*self.sub_radii[ii]/(self.ngrid[ii])*self.UnitLength_in_cm/self.hubble/(1+self.redshift) self.sub_nHI_grid[ii]*=(massg/epsilon**2) self.sub_nHI_grid[ii]+=0.1 [np.log10(grid,grid) for grid in self.sub_nHI_grid] return
def omega_gas(self): """Compute Omega_gas, the sum of the hydrogen mass, divided by the critical density. """ mass = 0. kpchincm = 1. / (1 + self.redshift) self.protonmass = 1.67262178e-24 for files in np.arange(0, 500): try: ff = hdfsim.get_file(self.num, self.base, files) except IOError: break bar = ff["PartType0"] nH = self.gas.get_code_rhoH(bar) pvol = 4. / 3. * math.pi * (hsml.get_smooth_length(bar) * kpchincm)**3 #Total mass of H in g ind = np.where(nH < 0.1) mass += np.sum(nH[ind] * pvol[ind]) * protonmass #mass += np.sum(nH*pvol)*protonmass #Total volume of the box in comoving cm^3 volume = (self.box)**3 #Total mass of HI * m_p / r_c omega_g = mass / volume / self.rho_crit() return omega_g
def set_zdir_grid(self, dlaind, gas=False, key="zpos", ion=-1): """Set up the grid around each halo where the HI is calculated. """ star=cold_gas.RahmatiRT(self.redshift, self.hubble, molec=self.molec) self.once=True #Now grid the HI for each halo files = hdfsim.get_all_files(self.snapnum, self.snap_dir) #Larger numbers seem to be towards the beginning files.reverse() self.xslab = np.zeros_like(dlaind[0], dtype=np.float64) try: start = self.load_fast_tmp(self.start, key) except IOError: start = self.start end = np.min([np.size(files),self.end]) for xx in xrange(start,end): ff = files[xx] f = h5py.File(ff,"r") print "Starting file ",ff bar=f["PartType0"] ipos=np.array(bar["Coordinates"]) #Get HI mass in internal units mass=np.array(bar["Masses"]) if not gas: #Hydrogen mass fraction try: mass *= np.array(bar["GFM_Metals"][:,0]) except KeyError: mass *= self.hy_mass nhi = star.get_reproc_HI(bar) ind = np.where(nhi > 1.e-3) ipos = ipos[ind,:][0] mass = mass[ind] #Get x * m for the weighted z direction if not gas: mass *= nhi[ind] if key == "zpos": mass*=ipos[:,0] elif key != "": mass *= self._get_secondary_array(ind,bar,key, ion) smooth = hsml.get_smooth_length(bar)[ind] for slab in xrange(self.nhalo): ind = np.where(dlaind[0] == slab) self.xslab[ind] += self.sub_list_grid_file(slab,ipos,smooth,mass,dlaind[1][ind], dlaind[2][ind]) f.close() #Explicitly delete some things. del ipos del mass del smooth self.save_fast_tmp(start,key) #Fix the units: #we calculated things in internal gadget /cell and we want atoms/cm^2 #So the conversion is mass/(cm/cell)^2 massg=self.UnitMass_in_g/self.hubble/self.protonmass epsilon=2.*self.sub_radii[0]/(self.ngrid[0])*self.UnitLength_in_cm/self.hubble/(1+self.redshift) self.xslab*=(massg/epsilon**2) return self.xslab
def _age_single_file(self,fn, elem, ion): """Get the density weighted interpolated temperature field for a single file""" (pos, vel, age, temp, hh, amumass) = self._read_particle_data(fn, elem, ion,True) if amumass == False: return np.zeros([np.shape(self.cofm)[0],self.nbins],dtype=np.float32) else: ff = h5py.File(fn, "r") data = ff["PartType0"] hh2 = hsml.get_smooth_length(data) pos2 = np.array(data["Coordinates"],dtype=np.float32) ind = self.particles_near_lines(pos2, hh2,self.axis,self.cofm) ind2 = self._filter_particles(age, None, None, None) part_ids = np.array(data["ParticleIDs"])[ind][ind2] assert np.size(part_ids) == np.size(age) tracer = ff["PartType3"] #Last star time is {a, -a, a+1, a+2} depending on from a star/wind to a gas, or from a gas to a star/wind. laststar = np.array(tracer["FluidQuantities"][:,8]) #Only interested in moving from star or wind to gas. #Assume that something from a wind came out of a star reasonably soon before #Zero value means was never in a star t_ind = np.where(np.logical_and(laststar < 1, laststar != 0)) laststar = laststar[t_ind] #Associate each tracer particle with a gas particle tracerparents = np.array(tracer["ParentID"])[t_ind] ff.close() #Now we have tracers we look through our gas cells to find #attached tracers. If we don't find a tracer, set this gas particle to zero time. #It was never in a star or wind. #Find all particles that don't have attached tracers withtracers = np.in1d(part_ids, tracerparents) withouttracers = np.where(np.logical_not(withtracers)) withtracers = np.where(withtracers) #Metal density of stuff that has been in a star should be much higher than stuff which hasn't been in a star #Otherwise we have problems. if np.any(withouttracers): print("Metal density in a star ",np.mean(age[withouttracers])) #It is not allowed to set individual elements of arrays selected in this complex way, so we must create a new array of #the multiplication factor. if np.size(withtracers) > 0: print("metal density not in star ",np.mean(age[withtracers])) lastfactor = np.array([np.max(np.abs(laststar[np.where(tracerparents == pid)])) for pid in part_ids[withtracers]]) age = age[withtracers] * lastfactor pos = pos[withtracers] vel = vel[withtracers] temp = temp[withtracers] hh = hh[withtracers] line = self.lines[("H",1)][1215] stuff = self._do_interpolation_work(pos, vel, age, temp, hh, amumass, line, False) return stuff else: return np.zeros([np.shape(self.cofm)[0],self.nbins],dtype=np.float32)
def set_nHI_grid(self, gas=False, start=0): """Set up the grid around each halo where the HI is calculated. """ star=cold_gas.RahmatiRT(self.redshift, self.hubble, molec=self.molec) self.cloudy_table = convert_cloudy.CloudyTable(self.redshift) self.once=True #Now grid the HI for each halo files = hdfsim.get_all_files(self.snapnum, self.snap_dir) #Larger numbers seem to be towards the beginning files.reverse() end = np.min([np.size(files),self.end]) for xx in xrange(start, end): ff = files[xx] f = h5py.File(ff,"r") print "Starting file ",ff bar=f["PartType0"] ipos=np.array(bar["Coordinates"]) #Get HI mass in internal units mass=np.array(bar["Masses"]) #Carbon mass fraction den = star.get_code_rhoH(bar) temp = star.get_temp(bar) mass_frac = np.array(bar["GFM_Metals"][:,2]) #Floor on the mass fraction of the metal ind = np.where(mass_frac > 1e-10) mass = mass[ind]*mass_frac[ind] #High densities will have no CIV anyway. den[np.where(den > 1e4)] = 9999. den[np.where(den < 1e-7)] = 1.01e-7 temp[np.where(temp > 3e8)] = 3e8 temp[np.where(temp < 1e3)] = 1e3 mass *= self.cloudy_table.ion("C", self.ion, den[ind], temp[ind]) smooth = hsml.get_smooth_length(bar)[ind] ipos = ipos[ind,:][0] [self.sub_gridize_single_file(ii,ipos,smooth,mass,self.sub_nHI_grid) for ii in xrange(0,self.nhalo)] f.close() #Explicitly delete some things. del ipos del mass del smooth #Deal with zeros: 0.1 will not even register for things at 1e17. #Also fix the units: #we calculated things in internal gadget /cell and we want atoms/cm^2 #So the conversion is mass/(cm/cell)^2 for ii in xrange(0,self.nhalo): massg=self.UnitMass_in_g/self.hubble/(self.protonmass*12.011) epsilon=2.*self.sub_radii[ii]/(self.ngrid[ii])*self.UnitLength_in_cm/self.hubble/(1+self.redshift) self.sub_nHI_grid[ii]*=(massg/epsilon**2) self.sub_nHI_grid[ii]+=0.1 np.log10(self.sub_nHI_grid[ii],self.sub_nHI_grid[ii]) return
def set_nHI_grid(self): """Set up the grid around each halo where the velocity HI is calculated. """ star=cold_gas.RahmatiRT(self.redshift, self.hubble) self.once=True #This is the real HI grid nHI_grid=np.array([np.zeros([self.ngrid[i],self.ngrid[i]]) for i in xrange(0,self.nhalo)]) #Now grid the HI for each halo for fnum in xrange(0,500): try: f=hdfsim.get_file(self.snapnum,self.snap_dir,fnum) except IOError: break print "Starting file ",fnum bar=f["PartType0"] ipos=np.array(bar["Coordinates"],dtype=np.float64) smooth = hsml.get_smooth_length(bar) # Velocity in cm/s vel = np.array(bar["Velocities"],dtype=np.float64)*self.UnitVelocity_in_cm_per_s #We will weight by neutral mass per cell irhoH0 = star.get_reproc_rhoHI(bar) irho=np.array(bar["Density"],dtype=np.float64)*(self.UnitMass_in_g/self.UnitLength_in_cm**3)*self.hubble**2 #HI * Cell Mass, internal units mass = np.array(bar["Masses"],dtype=np.float64)*irhoH0/irho f.close() #Perform the grid interpolation #sub_gas_grid is x velocity #sub_nHI_grid is y velocity [self.sub_gridize_single_file(ii,ipos,smooth,vel[:,1]*mass,self.sub_gas_grid,vel[:,2]*mass,self.sub_nHI_grid,mass) for ii in xrange(0,self.nhalo)] #Find the HI density also, so that we can discard #velocities in cells that are not DLAs. [self.sub_gridize_single_file(ii,ipos,smooth,irhoH0,nHI_grid,np.zeros(np.size(irhoH0)),nHI_grid) for ii in xrange(0,self.nhalo)] #Explicitly delete some things. del ipos del irhoH0 del irho del smooth del mass del vel #No /= in list comprehensions... :| #Average over z for i in xrange(0,self.nhalo): self.sub_gas_grid[i]/=self.sub_radii[i] self.sub_nHI_grid[i]/=self.sub_radii[i] ind = np.where(nHI_grid[i] < 10**20.3) self.sub_nHI_grid[i][ind] = 0 self.sub_gas_grid[i][ind] = 0 return
def set_nHI_grid(self, gas=False, start=0): """Set up the grid around each halo where the HI is calculated. """ star=cold_gas.RahmatiRT(self.redshift, self.hubble, molec=self.molec) self.once=True #Now grid the HI for each halo files = hdfsim.get_all_files(self.snapnum, self.snap_dir) #Larger numbers seem to be towards the beginning files.reverse() restart = 10 end = np.min([np.size(files),self.end]) for xx in xrange(start, end): ff = files[xx] f = h5py.File(ff,"r") print "Starting file ",ff bar=f["PartType0"] ipos=np.array(bar["Coordinates"]) #Get HI mass in internal units mass=np.array(bar["Masses"]) if not gas: #Hydrogen mass fraction try: mass *= np.array(bar["GFM_Metals"][:,0]) except KeyError: mass *= self.hy_mass mass *= star.get_reproc_HI(bar) smooth = hsml.get_smooth_length(bar) [self.sub_gridize_single_file(ii,ipos,smooth,mass,self.sub_nHI_grid) for ii in xrange(0,self.nhalo)] f.close() #Explicitly delete some things. del ipos del mass del smooth if xx % restart == 0 or xx == end-1: self.save_tmp(xx) #Deal with zeros: 0.1 will not even register for things at 1e17. #Also fix the units: #we calculated things in internal gadget /cell and we want atoms/cm^2 #So the conversion is mass/(cm/cell)^2 for ii in xrange(0,self.nhalo): massg=self.UnitMass_in_g/self.hubble/self.protonmass epsilon=2.*self.sub_radii[ii]/(self.ngrid[ii])*self.UnitLength_in_cm/self.hubble/(1+self.redshift) self.sub_nHI_grid[ii]*=(massg/epsilon**2) np.log10(self.sub_nHI_grid[ii],self.sub_nHI_grid[ii]) return
def set_ZZ_grid(self, start=0): """Set up the mass * metallicity grid for the box Same as set_nHI_grid except mass is multiplied by GFM_Metallicity. """ self.once=True #Now grid the HI for each halo files = hdfsim.get_all_files(self.snapnum, self.snap_dir) #Larger numbers seem to be towards the beginning files.reverse() restart = 10 end = np.min([np.size(files),self.end]) for xx in xrange(start, end): ff = files[xx] f = h5py.File(ff,"r") print "Starting file ",ff bar=f["PartType0"] ipos=np.array(bar["Coordinates"]) #Get HI mass in internal units mass=np.array(bar["Masses"]) #Sometimes the metallicity is less than zero: fix that met = np.array(bar["GFM_Metallicity"]) met[np.where(met <=0)] = 1e-50 mass *= met smooth = hsml.get_smooth_length(bar) [self.sub_gridize_single_file(ii,ipos,smooth,mass,self.sub_ZZ_grid) for ii in xrange(0,self.nhalo)] f.close() #Explicitly delete some things. del ipos del mass del smooth if xx % restart == 0 or xx == end-1: self.save_met_tmp(xx) #Deal with zeros: 0.1 will not even register for things at 1e17. #Also fix the units: #we calculated things in internal gadget /cell and we want atoms/cm^2 #So the conversion is mass/(cm/cell)^2 for ii in xrange(0,self.nhalo): massg=self.UnitMass_in_g/self.hubble/self.protonmass epsilon=2.*self.sub_radii[ii]/(self.ngrid[ii])*self.UnitLength_in_cm/self.hubble/(1+self.redshift) self.sub_ZZ_grid[ii]*=(massg/epsilon**2) self.sub_ZZ_grid[ii]+=0.1 np.log10(self.sub_ZZ_grid[ii],self.sub_ZZ_grid[ii]) return
def omega_gas(self): """Compute Omega_gas, the sum of the hydrogen mass, divided by the critical density. """ mass = 0. kpchincm = 1./(1+self.redshift) self.protonmass=1.67262178e-24 for files in np.arange(0,500): try: ff = hdfsim.get_file(self.num, self.base,files) except IOError: break bar = ff["PartType0"] nH = self.gas.get_code_rhoH(bar) pvol = 4./3.*math.pi*(hsml.get_smooth_length(bar)*kpchincm)**3 #Total mass of H in g ind = np.where(nH < 0.1) mass += np.sum(nH[ind]*pvol[ind])*protonmass #mass += np.sum(nH*pvol)*protonmass #Total volume of the box in comoving cm^3 volume = (self.box)**3 #Total mass of HI * m_p / r_c omega_g=mass/volume/self.rho_crit() return omega_g
def set_zdir_grid(self, dlaind, gas=False, key="zpos", ion=-1): """Set up the grid around each halo where the HI is calculated. """ star = cold_gas.RahmatiRT(self.redshift, self.hubble, molec=self.molec) self.once = True #Now grid the HI for each halo files = hdfsim.get_all_files(self.snapnum, self.snap_dir) #Larger numbers seem to be towards the beginning files.reverse() self.xslab = np.zeros_like(dlaind[0], dtype=np.float64) try: start = self.load_fast_tmp(self.start, key) except IOError: start = self.start end = np.min([np.size(files), self.end]) for xx in xrange(start, end): ff = files[xx] f = h5py.File(ff, "r") print "Starting file ", ff bar = f["PartType0"] ipos = np.array(bar["Coordinates"]) #Get HI mass in internal units mass = np.array(bar["Masses"]) if not gas: #Hydrogen mass fraction try: mass *= np.array(bar["GFM_Metals"][:, 0]) except KeyError: mass *= self.hy_mass nhi = star.get_reproc_HI(bar) ind = np.where(nhi > 1.e-3) ipos = ipos[ind, :][0] mass = mass[ind] #Get x * m for the weighted z direction if not gas: mass *= nhi[ind] if key == "zpos": mass *= ipos[:, 0] elif key != "": mass *= self._get_secondary_array(ind, bar, key, ion) smooth = hsml.get_smooth_length(bar)[ind] for slab in xrange(self.nhalo): ind = np.where(dlaind[0] == slab) self.xslab[ind] += self.sub_list_grid_file( slab, ipos, smooth, mass, dlaind[1][ind], dlaind[2][ind]) f.close() #Explicitly delete some things. del ipos del mass del smooth self.save_fast_tmp(start, key) #Fix the units: #we calculated things in internal gadget /cell and we want atoms/cm^2 #So the conversion is mass/(cm/cell)^2 massg = self.UnitMass_in_g / self.hubble / self.protonmass epsilon = 2. * self.sub_radii[0] / ( self.ngrid[0]) * self.UnitLength_in_cm / self.hubble / ( 1 + self.redshift) self.xslab *= (massg / epsilon**2) return self.xslab