def yield_matches(snapbase1, gpos1, r200c1, groupbase2, snapbase2, ids_block1, ids_block2, min_value, min_ids_len_factor, max_distance, max_r200c_factor_ids, min_block_value='MCRI', use_cache=False): """ loop over haloes and find matches with the halo on gpos1 and radius r200c1 in simulation snapbase1. In simulation1 read block ids_block1, in simulation2 read block ids_block2 - bc DMO runs have blocks shifted wrt the BAO counterpart. """ ids1 = None #cache ids list of this halo icluster2 = -1 for cluster2 in yield_haloes(groupbase2, 0, min_value=min_value, min_block_value=min_block_value, use_cache=use_cache): glen2 = cluster2['GLEN'] gpos2 = cluster2['GPOS'] r200c2 = cluster2['RCRI'] m200c2 = cluster2['MCRI'] boxsize1 = cluster2['boxsize'] distance = g3.periodic_distance(gpos1, gpos2, periodic=boxsize1) cluster2['distance'] = distance cluster2['int_frac'] = np.nan if (distance <= max_distance): if (ids_block1 is not None) and (ids_block2 is not None): if ids1 is None: #check if halo ids are in cache or if we must obtain them dm_data1 = g3.read_particles_in_box( snapbase1, gpos1, r200c1, ['POS ', 'ID '], ids_block1) mask1 = g3.to_spherical( dm_data1['POS '], gpos1).T[0] < (r200c1 * max_r200c_factor_ids) ids1 = np.sort(dm_data1['ID '][mask1]) dm_data2 = g3.read_particles_in_box(snapbase2, gpos2, r200c2, ['POS ', 'ID '], ids_block2) mask2 = g3.to_spherical( dm_data2['POS '], gpos2).T[0] < (r200c2 * max_r200c_factor_ids) ids2 = np.sort(dm_data2['ID '][mask2]) ids_int = np.intersect1d(ids1, ids2) int_len = len(ids_int) int_frac = float(int_len) / float(len(ids1)) cluster2['int_frac'] = int_frac if int_frac > min_ids_len_factor: yield cluster2 return else: yield cluster2 return
groups_in_file_data = g3.read_new(group_filename, ['RCRI', 'MCRI', 'GPOS'], 0, is_snap=False) groups_in_file = len(groups_in_file_data['MCRI']) for i in range(groups_in_file): ihalo += 1 center = groups_in_file_data['GPOS'][i] r200c = groups_in_file_data['RCRI'][i] M200c = groups_in_file_data['MCRI'][i] print(' - ihalo: ', ihalo) print(' center: ', center, '[code units]') print(' R200c: ', r200c, '[code units]') print(' M200c: ', M200c, '[code units]') group_gas_data = g3.read_particles_in_box(snapbase, center, r200c, ['MASS', 'TEMP', 'POS '], 0) #note: in the spirit of Klaus read_particles_in_box, the above routine returns a superset of particles within `r200c`. #we now filter data outside r200c, we use g3.to_spherical that returns an array of [rho, theta, phi] around `center`. group_gas_distance_from_center = g3.to_spherical( group_gas_data['POS '], center).T[0] group_gas_mask = group_gas_distance_from_center < r200c group_gas_masswtemp = group_gas_data['TEMP'][ group_gas_mask] * group_gas_data['MASS'][group_gas_mask] group_gas_avgtemp = np.mean(group_gas_masswtemp) / np.sum( group_gas_data['MASS'][group_gas_mask]) print(' Avg Temp(R<R200c): ', group_gas_avgtemp, '[K]')
def spherical(self, ptype): #print (ptype, self.read_new()[ptype]['POS ']) #print(self.read_new()[ptype]) return g.to_spherical(self.read_new()[ptype]['POS '], self.gpos())
def gravitational_potential( masses, positions, gpos, cut=None, spherical=None, cut_type=None, superkeys=True, G=43007.1, set_to_value_after_cut=None, remove_constant_rho=0, #remove_constant_rho=7.563375e-09 spher_nbs=40, spher_nfi=4, spher_nteta=4, has_keys=True): all_data = {} all_data[-1] = {} all_data[-1]["MASS"] = masses all_data[-1]["POS "] = positions if spherical is None: all_sferical = g.to_spherical(all_data[-1]["POS "], gpos) else: all_sferical = spherical all_data[-1]["SPOS"] = all_sferical Nall = len(all_data[-1]['MASS']) import math twopi = 2. * math.pi pi = math.pi """ POTENTIAL """ #print all_data[-1]['SPOS'] #print(np.min(all_data[-1]['SPOS'][:,0]),np.max(all_data[-1]['SPOS'][:,0])) spher_bs = [ np.logspace(np.log10(np.min(all_data[-1]['SPOS'][:, 0]) + 0.01), np.log10(np.max(all_data[-1]['SPOS'][:, 0])), spher_nbs), np.linspace(0., pi, spher_nteta), np.linspace(-pi, pi, spher_nfi) ] mass_weights = all_data[-1]['MASS'] if cut is not None and cut_type is not None: if cut_type == "sphere": mass_weights[all_data[-1]['SPOS'][:, 0] > cut] = 0. elif cut_type == "cube": printf("cube cut", e=True) mass_weights[np.abs(all_data[-1]['POS '][:, 0] - gpos[0]) > cut] = 0. mass_weights[np.abs(all_data[-1]['POS '][:, 1] - gpos[1]) > cut] = 0. mass_weights[np.abs(all_data[-1]['POS '][:, 2] - gpos[2]) > cut] = 0. spher_all_ms, spher_b = np.histogramdd(all_data[-1]['SPOS'], weights=mass_weights, bins=spher_bs) spher_all_ds, spher_b = np.histogramdd(all_data[-1]['SPOS'], weights=all_data[-1]['SPOS'].T[0], bins=spher_bs) spher_all_ts, spher_b = np.histogramdd(all_data[-1]['SPOS'], weights=all_data[-1]['SPOS'].T[1], bins=spher_bs) spher_all_fs, spher_b = np.histogramdd(all_data[-1]['SPOS'], weights=all_data[-1]['SPOS'].T[2], bins=spher_bs) spher_all_ns, spher_b = np.histogramdd(all_data[-1]['SPOS'], bins=spher_bs) spher_all_ns[spher_all_ns == 0] = np.nan spher_all_cds = spher_all_ds / spher_all_ns spher_all_cts = spher_all_ts / spher_all_ns spher_all_cfs = spher_all_fs / spher_all_ns spher_all_x, spher_all_y, spher_all_z = g.to_cartesian( np.array([spher_all_cds, spher_all_cts, spher_all_cfs]).T) shape = spher_all_ds.shape spher_b_delta_r = (spher_b[0][1:] - spher_b[0][:-1]) spher_b_delta_t = (spher_b[1][1:] - spher_b[1][:-1]) spher_b_delta_f = (spher_b[2][1:] - spher_b[2][:-1]) shper_delta_rs = np.transpose( (np.transpose(np.ones(shape), axes=(2, 1, 0)) * (spher_b_delta_r)), axes=(2, 1, 0)) shper_delta_ts = np.transpose( (np.transpose(np.ones(shape), axes=(0, 2, 1)) * (spher_b_delta_t)), axes=(0, 2, 1)) shper_delta_fs = np.transpose( (np.transpose(np.ones(shape), axes=(0, 1, 2)) * (spher_b_delta_f)), axes=(0, 1, 2)) spher_all_vols = spher_all_cds**2. * np.sin( spher_all_cts) * shper_delta_rs * shper_delta_ts * shper_delta_fs spher_all_rhos = spher_all_ms / spher_all_vols spher_all_ms = np.nan_to_num(spher_all_ms) if remove_constant_rho > 0: print("removing constant rho", remove_constant_rho) spher_all_ms[ spher_all_rhos >= remove_constant_rho] -= remove_constant_rho * spher_all_vols[ spher_all_rhos >= remove_constant_rho] spher_all_ms[spher_all_rhos < remove_constant_rho] -= 0. def generate_fi(spher_b, spher_all_cds, spher_all_cts, spher_all_cfs, spher_all_x, spher_all_y, spher_all_z, spher_all_ms): fi = np.ones(spher_all_ds.shape) for bin_r in range(len(spher_b[0]) - 1): for bin_t in range(len(spher_b[1]) - 1): for bin_phi in range(len(spher_b[2]) - 1): position_xyz = g.to_cartesian( np.array( np.array([ spher_all_cds[bin_r, bin_t, bin_phi], spher_all_cts[bin_r, bin_t, bin_phi], spher_all_cfs[bin_r, bin_t, bin_phi] ])).T) distances = np.sqrt((spher_all_x - position_xyz[0])**2. + (spher_all_y - position_xyz[1])**2. + (spher_all_z - position_xyz[2])**2.) distances = np.nan_to_num(distances) non_zero_distances = distances > 0. fi[bin_r, bin_t, bin_phi] = -G * np.sum(spher_all_ms[non_zero_distances] / distances[non_zero_distances]) return np.nan_to_num(fi) fi = generate_fi(spher_b, spher_all_cds, spher_all_cts, spher_all_cfs, spher_all_x, spher_all_y, spher_all_z, spher_all_ms) bin_all_h_i = np.digitize(all_data[-1]['SPOS'][:, 0], spher_bs[0]) - 1 bin_all_h_j = np.digitize(all_data[-1]['SPOS'][:, 1], spher_bs[1]) - 1 bin_all_h_k = np.digitize(all_data[-1]['SPOS'][:, 2], spher_bs[2]) - 1 bin_all_h_i[bin_all_h_i >= len(spher_bs[0]) - 1] = len( spher_bs[0] ) - 2 #bug of np, if a value is exactly a boundary, the bin is larger than it should bin_all_h_j[bin_all_h_j >= len(spher_bs[1]) - 1] = len(spher_bs[1]) - 2 bin_all_h_k[bin_all_h_k >= len(spher_bs[2]) - 1] = len(spher_bs[2]) - 2 bin_all_h = np.array([bin_all_h_i, bin_all_h_j, bin_all_h_k]).T bin_all_h_tuple = tuple(bin_all_h) somma_all_inte = fi[tuple(bin_all_h.T)] """ set to zero things outside rcri""" if set_to_value_after_cut is not None: if cut_type == "sphere": somma_all_inte[ all_data[-1]['SPOS'][:, 0] > cut] = set_to_value_after_cut elif cut_type == "cube": print("cube zeroing") somma_all_inte[np.abs(all_data[-1]['POS '][:, 0] - gpos[0]) > cut] = 0. somma_all_inte[np.abs(all_data[-1]['POS '][:, 1] - gpos[1]) > cut] = 0. somma_all_inte[np.abs(all_data[-1]['POS '][:, 2] - gpos[2]) > cut] = 0. all_data[-1]["SPHERICAL_POTE"] = somma_all_inte return O(potential=all_data[-1]["SPHERICAL_POTE"])
if with_ids: halo_ids = halo['ids'] else: halo_ids = None # # If required by read_particles, we read all halo particles within RCRI with read_particles_in_box # if read_particles: halo_particles = g3.read_particles_in_box(snapbase, halo['GPOS'], halo['RCRI'], read_particles_blocks, -1) printf(' Readed n. of halo particles: %d\n' % len(halo_particles['MASS'])) if compute_concentration: halo_particles['DIST'] = g3.to_spherical( halo_particles['POS '], halo['GPOS']).T[0] #get distnaces from center nfw_res = matcha.nfw_fit_fast_cu(halo_particles['MASS'], halo_particles['DIST'], halo['RCRI']) printf(' c200c tot. matter : %.1f\n' % nfw_res['c']) printf('\n') printf(' subhaloes: \n') # # Here we loop over subhaloes of the current halo # Note we must pass halo['GOFF'] to the subhalo-reader in order to efficiently find subhaloes. # for subhalo in matcha.yield_subhaloes(groupbase, ihalo, with_ids=with_ids,
f = g.GadgetFile(filename) # #the function returns a data structure for all selected blocks (POS, VEL, MASS, TEMP) #and stack the data for the various particle types (0,1,2,3,4,5), where 0=gas, 1=dark matter, 4=stars, 5=black holes. #For instance, you can access all positions readin data["POS "] #If you need data separated per particle type, run #data = f.read_new(blocks=[...], ptypes=[...], only_joined_ptypes=False) #and you can access the properties for each data type, for instance gas particles, using data["POS "][0] # data = f.read_new(blocks=["POS ","VEL ","TEMP","MASS"], ptypes=[0,1,2,3,4,5]) center = np.average(data["POS "],weights=data["MASS"],axis=0) #the function 'g.to_spherical()' returns data with columns 0,1,2 being rho,theta,phi spherical_cut = g.to_spherical(data["POS "],center)[:,0]<cut_radius vel = data["VEL "][spherical_cut] T_inside_radius = data["TEMP"][spherical_cut] #in the previous lines we loaded the block temperature for all particles, #but only gas particles have a temperature, so the library fills #the particles T_inside_radius = T_inside_radius[~np.isnan(T_inside_radius)] radial_vel = g.to_spherical(data["VEL "],[0.,0.,0.])[:,0] avg_vel = np.mean(radial_vel) avg_vel2 = np.mean(radial_vel**2) sigma2_vel = avg_vel2 - avg_vel*avg_vel meanT = np.mean(T_inside_radius)