def sub_gridize_single_file(self,ii,ipos,ismooth,mHI,sub_nHI_grid,weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ start_time = time.time() end_time = time.time() (coords, ismooth, mHI) = self._find_particles_in_slab(ii,ipos,ismooth, mHI) if coords == None: return fieldize.sph_str(coords,mHI,sub_nHI_grid[ii],ismooth,weights=weights, periodic=True) npart=mHI.shape[0] del coords del ismooth del mHI gc.collect() end_time = time.time() print "fieldize done! took "+str(end_time-start_time)+" seconds ("+str((end_time-start_time)/npart)+" seconds per particle)" return
def sub_gridize_single_halo(self, pos, hsml, ion_mass, grid, weights=None, kernel_type='SPH'): coords = fieldize.convert_centered(pos, self.ngrid, 2 * self.grid_radius) #Convert smoothing lengths to grid coordinates. hsml_grid = hsml * (self.ngrid / (2 * self.grid_radius)) #interpolate the density ts = time.time() if self.verbose: print "Starting to fieldize" if kernel_type == 'SPH': fieldize.sph_str(coords, ion_mass, grid, hsml_grid, weights=weights) elif kernel_type == 'TOPHAT': fieldize.tophat_str(coords, ion_mass, grid, hsml_grid, weights=weights) if self.verbose: print "Time to fieldize: ", time.time() - ts return
def sub_gridize_single_file(self, ii, ipos, ismooth, mHI, sub_nHI_grid, weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ (coords, ismooth, mHI) = self._find_particles_in_slab(ii, ipos, ismooth, mHI) if np.size(coords) == 1 and coords == None: return fieldize.sph_str(coords, mHI, sub_nHI_grid[ii], ismooth, weights=weights, periodic=True) del coords del mHI del ismooth return
def sub_gridize_single_file(self,ipos,ismooth,mHI,sub_nHI_grid,weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ #Find particles near each halo sub_pos=self.sub_pos grid_radius = self.sub_radius #Need a local for numexpr box = self.box #Gather all nearby cells, paying attention to periodic box conditions for dim in np.arange(3): jpos = sub_pos[dim] jjpos = ipos[:,dim] indj = np.where(ne.evaluate("(abs(jjpos-jpos) < grid_radius+ismooth) | (abs(jjpos-jpos+box) < grid_radius+ismooth) | (abs(jjpos-jpos-box) < grid_radius+ismooth)")) if np.size(indj) == 0: return ipos = ipos[indj] # Update smooth and rho arrays as well: ismooth = ismooth[indj] mHI = mHI[indj] jjpos = ipos[:,dim] # BC 1: ind_bc1 = np.where(ne.evaluate("(abs(jjpos-jpos+box) < grid_radius+ismooth)")) ipos[ind_bc1,dim] = ipos[ind_bc1,dim] + box # BC 2: ind_bc2 = np.where(ne.evaluate("(abs(jjpos-jpos-box) < grid_radius+ismooth)")) ipos[ind_bc2,dim] = ipos[ind_bc2,dim] - box #if np.size(ind_bc1)>0 or np.size(ind_bc2)>0: # print "Fixed some periodic cells!" if np.size(ipos) == 0: return #coords in grid units coords=fieldize.convert_centered(ipos-sub_pos,self.ngrid,2*self.sub_radius) #NH0 cellspkpc=(self.ngrid/(2*self.sub_radius)) #Convert smoothing lengths to grid coordinates. ismooth*=cellspkpc if self.once: avgsmth=np.mean(ismooth) print " Av. smoothing length is ",avgsmth/cellspkpc," kpc/h ",avgsmth, "grid cells min: ",np.min(ismooth) self.once=False #interpolate the density fieldize.sph_str(coords,mHI,sub_nHI_grid,ismooth,weights=weights) return
def sub_gridize_single_halo(self,i,pos,hsml,ion_mass,grid,weights=None): coords=fieldize.convert_centered(pos,self.ngrid[i],2*self.grid_radii[i]) #Convert smoothing lengths to grid coordinates. hsml_grid = hsml*(self.ngrid[i]/(2*self.grid_radii[i])) #interpolate the density ts = time.time() print "starting to fieldize" fieldize.sph_str(coords,ion_mass,grid[i],hsml_grid,weights=weights) print "time to fieldize: ",time.time()-ts return
def sub_gridize_single_file(self,ii,ipos,ismooth,mHI,sub_nHI_grid,weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ (ipos, ismooth, mHI) = self._find_particles_near_halo(ii, ipos, ismooth, mHI) if np.size(ipos) == 0: return (coords,ismooth) = self._convert_interp_units(ii, ipos, ismooth) fieldize.sph_str(coords,mHI,sub_nHI_grid[ii],ismooth,weights=weights) return
def sub_gridize_single_file(self,ii,ipos,ismooth,mHI,sub_nHI_grid,weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ (coords, ismooth, mHI) = self._find_particles_in_slab(ii,ipos,ismooth, mHI) if np.size(coords) == 1 and coords == None: return fieldize.sph_str(coords,mHI,sub_nHI_grid[ii],ismooth,weights=weights, periodic=True) del coords del mHI del ismooth return
def sub_gridize_single_halo(self,pos,hsml,ion_mass,grid,weights=None,kernel_type='SPH'): coords=fieldize.convert_centered(pos,self.ngrid,2*self.grid_radius) #Convert smoothing lengths to grid coordinates. hsml_grid = hsml*(self.ngrid/(2*self.grid_radius)) #interpolate the density ts = time.time() if self.verbose: print "Starting to fieldize" if kernel_type == 'SPH': fieldize.sph_str(coords,ion_mass,grid,hsml_grid,weights=weights) elif kernel_type == 'TOPHAT': fieldize.tophat_str(coords,ion_mass,grid,hsml_grid,weights=weights) if self.verbose: print "Time to fieldize: ",time.time()-ts return
def sub_gridize_single_file(self,ii,ipos,ismooth,mHI,sub_nHI_grid,weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ (ipos, ismooth, mHI) = self._find_particles_near_halo(ii, ipos, ismooth, mHI) print ii if np.size(ipos) == 0: return (coords,ismooth) = self._convert_interp_units(ii, ipos, ismooth) fieldize.sph_str(coords,mHI,sub_nHI_grid[ii],ismooth,weights=weights) return
def sub_gridize_single_file(self,ii,ipos,ismooth,bar,sub_nHI_grid,weights=None): """Helper function for sub_nHI_grid that puts data arrays loaded from a particular file onto the grid. Arguments: pos - Position array rho - Density array to be interpolated smooth - Smoothing lengths sub_grid - Grid to add the interpolated data to """ #Find particles near each halo sub_pos=self.sub_cofm[ii] grid_radius = self.sub_radii[ii] #Need a local for numexpr box = self.box #Get gas mass in internal units mass=np.array(bar["Masses"]) #Density in this species nelem = self.species.index(self.elem) mass_frac=np.array(bar["GFM_Metals"][:,nelem]) #In g/cm^3 den = np.array(bar["Density"])*self.dscale #In (hydrogen) atoms / cm^3 den /= self.protonmass #Mean molecular weight: # \mu = 1 / molecules per unit atomic weight # = 1 / (X + Y /4 + E) # where E = Ne * X, and Y = (1-X). # Can neglect metals as they are heavy. # Leading contribution is from electrons, which is already included # [+ Z / (12->16)] from metal species # [+ Z/16*4 ] for OIV from electrons. mu = 1.0/(0.76*(0.75+np.array(bar["ElectronAbundance"])) + 0.25) temp = np.array(bar["InternalEnergy"])*self.tscale*mu #Gather all nearby cells, paying attention to periodic box conditions for dim in np.arange(3): jpos = sub_pos[dim] jjpos = ipos[:,dim] indj = np.where(ne.evaluate("(abs(jjpos-jpos) < grid_radius+ismooth) | (abs(jjpos-jpos+box) < grid_radius+ismooth) | (abs(jjpos-jpos-box) < grid_radius+ismooth)")) if np.size(indj) == 0: return ipos = ipos[indj] # Update smooth and rho arrays as well: ismooth = ismooth[indj] mass = mass[indj] mass_frac = mass_frac[indj] den = den[indj] jjpos = ipos[:,dim] # BC 1: ind_bc1 = np.where(ne.evaluate("(abs(jjpos-jpos+box) < grid_radius+ismooth)")) ipos[ind_bc1,dim] = ipos[ind_bc1,dim] + box # BC 2: ind_bc2 = np.where(ne.evaluate("(abs(jjpos-jpos-box) < grid_radius+ismooth)")) ipos[ind_bc2,dim] = ipos[ind_bc2,dim] - box #if np.size(ind_bc1)>0 or np.size(ind_bc2)>0: # print "Fixed some periodic cells!" if np.size(ipos) == 0: return mass_frac *= self.cloudy_table.ion(self.elem, self.ion, den, temp) #coords in grid units coords=fieldize.convert_centered(ipos-sub_pos,self.ngrid[ii],2*self.sub_radii[ii]) #NH0 cellspkpc=(self.ngrid[ii]/(2*self.sub_radii[ii])) #Convert smoothing lengths to grid coordinates. ismooth*=cellspkpc if self.once: avgsmth=np.mean(ismooth) print ii," Av. smoothing length is ",avgsmth/cellspkpc," kpc/h ",avgsmth, "grid cells min: ",np.min(ismooth) self.once=False #interpolate the density fieldize.sph_str(coords,mass*mass_frac,sub_nHI_grid[ii],ismooth,weights=weights) return