def run_tracker(DD, simdir, haloname, simname, anchor_fits): DDname = 'DD%.4i' % DD ds = yt.load('%s/%s/%s/%s/%s' % (simdir, haloname, simname, DDname, DDname)) ad = ds.all_data() yt.add_particle_filter("stars", function=_stars, filtered_type='all', requires=["particle_type"]) ds.add_particle_filter('stars') make_savefile(anchor_fits=anchor_fits, simname=simname, haloname=haloname, simdir=simdir, DD=DD, ds=ds, ad=ad)
def get_particles(haloname, simname, snapname): snaps = np.sort( np.asarray( glob.glob("/nobackupp2/mpeeples/%s/%s/%s/%s" % (haloname, simname, snapname, snapname)))) #abssnap = os.path.abspath(snaps[0]) assert os.path.lexists(snaps[0]) out_dir = '/u/rcsimons/' assert os.path.lexists(out_dir) new_snapfiles = np.asarray(snaps) ts = yt.DatasetSeries(new_snapfiles) def _stars(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 2 def _darkmatter(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 4 yt.add_particle_filter("stars", function=_stars, filtered_type='all', requires=["particle_type"]) yt.add_particle_filter("darkmatter", function=_darkmatter, filtered_type='all', requires=["particle_type"]) for ds, snapfile in zip(reversed(ts), np.flipud(new_snapfiles)): ad = ds.all_data() ds.add_particle_filter('stars') ds.add_particle_filter('darkmatter') dark_pos_x = ad['darkmatter', 'particle_position_x'].in_units('kpc') stars_pos_x = ad['stars', 'particle_position_x'].in_units('kpc') print shape(dark_pos_x), shape(stars_pos_x)
def run_writer(DD, simdir, haloname, simname): DDname = 'DD%.4i' % DD ds = yt.load('%s/%s/%s/%s/%s' % (simdir, haloname, enzo_simname, DDname, DDname)) ad = ds.all_data() yt.add_particle_filter("stars", function=_stars, filtered_type='all', requires=["particle_type"]) yt.add_particle_filter("darkmatter", function=_darkmatter, filtered_type='all', requires=["particle_type"]) ds.add_particle_filter('stars') ds.add_particle_filter('darkmatter') #if not os.path.exists('/nobackupp2/rcsimons/foggie_momentum/particles/%s/%s_DD%.4i_particles.fits'%(simname, simname, DD)): make_savefile(simname=simname, haloname=haloname, simdir=simdir, DD=DD, ds=ds, ad=ad)
def make_figure(figdir, DD, cen_name, simdir, haloname, simname, wd=30, wdd=30, wd2=300, wdd2=300): DDname = 'DD%.4i' % DD ds = yt.load('%s/%s/%s/%s/%s' % (simdir, haloname, simname, DDname, DDname)) def _stars(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 2 yt.add_particle_filter("stars", function=_stars, filtered_type='all', requires=["particle_type"]) ds.add_particle_filter('stars') cen_fits = fits.open( '/nobackupp2/rcsimons/foggie_momentum/anchor_files/%s/%s_DD%.4i_anchorprops.fits' % (cen_name, simname, DD)) central_xyz_fit = np.load( '/nobackupp2/rcsimons/foggie_momentum/catalogs/center_natural.npy')[()] xf = central_xyz_fit['x'] yf = central_xyz_fit['y'] zf = central_xyz_fit['z'] central_x = xf[0] * DD**4. + xf[1] * DD**3. + xf[2] * DD**2. + xf[ 3] * DD + xf[4] central_y = yf[0] * DD**4. + yf[1] * DD**3. + yf[2] * DD**2. + yf[ 3] * DD + yf[4] central_z = zf[0] * DD**4. + zf[1] * DD**3. + zf[2] * DD**2. + zf[ 3] * DD + zf[4] cen_central = yt.YTArray([central_x, central_y, central_z], 'kpc') W = yt.YTArray([wd, wd, wd], 'kpc') W2 = yt.YTArray([wd2, wd2, wd2], 'kpc') for sat_n in arange(12): if len(cen_fits['SAT_%.2i' % sat_n].data['box_avg']) > 0: cenx = cen_fits['SAT_%.2i' % sat_n].data['box_avg'][0] ceny = cen_fits['SAT_%.2i' % sat_n].data['box_avg'][1] cenz = cen_fits['SAT_%.2i' % sat_n].data['box_avg'][2] cen_g = yt.YTArray([cenx, ceny, cenz], 'kpc') for axis in ['x', 'y', 'z']: figname_zoomin = '%s_%.4i_%.2i_%s_zoomin.png' % (cen_name, DD, sat_n, axis) figname_zoomout = '%s_%.4i_%.2i_%s_zoomout.png' % ( cen_name, DD, sat_n, axis) if not os.path.exists('%s/%s' % (figdir, figname_zoomin)): if axis == 'x': box = ds.r[cen_g[0] - 0.5 * yt.YTArray(wdd, 'kpc'): cen_g[0] + 0.5 * yt.YTArray(wdd, 'kpc'), \ cen_g[1] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[1] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[2] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[2] + 0.5 * yt.YTArray(3*wd, 'kpc')] box2 = ds.r[cen_central[0] - 0.5 * yt.YTArray(wdd2, 'kpc'): cen_central[0] + 0.5 * yt.YTArray(wdd2, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[1] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(3*wd2, 'kpc')] p_1 = cen_g[1] - cen_central[1] p_2 = cen_g[2] - cen_central[2] elif axis == 'y': box = ds.r[cen_g[0] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[0] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[1] - 0.5 * yt.YTArray(wdd, 'kpc'): cen_g[1] + 0.5 * yt.YTArray(wdd, 'kpc'), \ cen_g[2] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[2] + 0.5 * yt.YTArray(3*wd, 'kpc')] box2 = ds.r[cen_central[0] - 0.5 * yt.YTArray(3*wd2, 'kpc') : cen_central[0] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(wdd2, 'kpc') : cen_central[1] + 0.5 * yt.YTArray(wdd2, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(3*wd2, 'kpc')] p_1 = cen_g[0] - cen_central[0] p_2 = cen_g[2] - cen_central[2] elif axis == 'z': box = ds.r[cen_g[0] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[0] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[1] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[1] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[2] - 0.5 * yt.YTArray(wdd, 'kpc'): cen_g[2] + 0.5 * yt.YTArray(wdd, 'kpc')] box2 = ds.r[cen_central[0] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[0] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[1] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(wdd2, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(wdd2, 'kpc')] p_1 = cen_g[0] - cen_central[0] p_2 = cen_g[1] - cen_central[1] fig = plt.figure(sat_n) grid = AxesGrid(fig, (0.0, 0.0, 1.0, 1.0), nrows_ncols=(1, 2), axes_pad=0.0, label_mode="1", share_all=False, cbar_mode=None, aspect=False) p = yt.ProjectionPlot(ds, axis, ("gas", "density"), center=cen_g, data_source=box, width=W) p.set_unit(('gas', 'density'), 'Msun/pc**2') p.set_zlim(('gas', 'density'), zmin=density_proj_min * 0.1, zmax=density_proj_max) p.set_cmap(('gas', 'density'), density_color_map) p.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p.hide_axes() p.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p.annotate_scale(size_bar_args={'color': 'white'}) plot = p.plots[("gas", "density")] plot.figure = fig plot.axes = grid[0].axes p._setup_plots() p = yt.ParticleProjectionPlot(ds, axis, ('stars', 'particle_mass'), center=cen_g, data_source=box, width=W) cmp = plt.cm.Greys_r cmp.set_bad('k') p.set_cmap(field=('stars', 'particle_mass'), cmap=cmp) p.hide_axes() p.annotate_scale(size_bar_args={'color': 'white'}) p.set_zlim(field=('stars', 'particle_mass'), zmin=2.e35 * 0.3, zmax=1.e42 * 0.9) plot = p.plots[('stars', 'particle_mass')] plot.figure = fig plot.axes = grid[1].axes p._setup_plots() fig.set_size_inches(12, 6) fig.savefig('%s/%s' % (figdir, figname_zoomin)) plt.close(fig) fig = plt.figure(sat_n) grid = AxesGrid(fig, (0.0, 0.0, 1.0, 1.0), nrows_ncols=(1, 2), axes_pad=0.0, label_mode="1", share_all=False, cbar_mode=None, aspect=False) p = yt.ProjectionPlot(ds, axis, ("gas", "density"), center=cen_central, data_source=box2, width=W2) p.set_unit(('gas', 'density'), 'Msun/pc**2') p.set_zlim(('gas', 'density'), zmin=density_proj_min * 0.1, zmax=density_proj_max) p.set_cmap(('gas', 'density'), density_color_map) p.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p.hide_axes() p.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p.annotate_scale(size_bar_args={'color': 'white'}) plot = p.plots[("gas", "density")] plot.figure = fig plot.axes = grid[0].axes p._setup_plots() if (abs(p_1) < W2 / 2.) & (abs(p_2) < W2 / 2.): plot.axes.scatter(p_1, p_2, marker='o', facecolor="none", edgecolor='red', lw=2, s=800) p = yt.ParticleProjectionPlot(ds, axis, ('stars', 'particle_mass'), center=cen_central, data_source=box2, width=W2) cmp = plt.cm.Greys_r cmp.set_bad('k') p.set_cmap(field=('stars', 'particle_mass'), cmap=cmp) p.hide_axes() p.annotate_scale(size_bar_args={'color': 'white'}) p.set_zlim(field=('stars', 'particle_mass'), zmin=2.e35 * 0.3, zmax=1.e42 * 0.9) plot = p.plots[('stars', 'particle_mass')] plot.figure = fig plot.axes = grid[1].axes p._setup_plots() if (abs(p_1) < W2 / 2.) & (abs(p_2) < W2 / 2.): plot.axes.scatter(p_1, p_2, marker='o', facecolor="none", edgecolor='red', lw=2, s=800) fig.set_size_inches(12, 6) fig.savefig('%s/%s' % (figdir, figname_zoomout)) plt.close(fig) ds.index.clear_all_data()
#ds_1 = yt.load('~/Dropbox/rcs_foggie/data/halo_008508/nref11n_selfshield_z15/RD0018/RD0018') ds_1 = yt.load('/Users/rsimons/Desktop/git/foggie_local/natural/RD0020/RD0020') print 'Loading nref11n_nref10f_selfshield_z6...' #ds_2 = yt.load('~/Dropbox/rcs_foggie/data/halo_008508/nref11n_nref10f_selfshield_z6/RD0018/RD0018') ds_2 = yt.load('/Users/rsimons/Desktop/git/foggie_local/forced/RD0020/RD0020') ad_1 = ds_1.all_data() ad_2 = ds_2.all_data() def _stars(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 2 yt.add_particle_filter("stars",function=_stars, filtered_type='all',requires=["particle_type"]) ds_1.add_particle_filter('stars') ds_2.add_particle_filter('stars') def find_center(ad): x = histogram(ad['particle_position_x'].value, bins = linspace(0.45, 0.55, 10000)) good_argmax_x = argmax(x[0]) x_cen = (x[1][good_argmax_x] + x[1][good_argmax_x+1])/2. y = histogram(ad['particle_position_y'].value, bins = linspace(0.45, 0.55, 20000)) good_argmax_y = argmax(y[0]) y_cen = (y[1][good_argmax_y] + y[1][good_argmax_y+1])/2. z = histogram(ad['particle_position_z'].value, bins = linspace(0.45, 0.55, 10000))
for field in fields: if field in ['scale', 'stars_total_mass', 'stars_rhalf', 'gas_total_mass' ]: galaxy_props[field] = np.array([]) else: galaxy_props[field] = [] def _stars(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 2 #this gets dark matter particles in zoom region only def _darkmatter(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 4 yt.add_particle_filter("stars",function=_stars, filtered_type='all',requires=["particle_type"]) yt.add_particle_filter("darkmatter",function=_darkmatter, filtered_type='all',requires=["particle_type"]) ts = yt.DatasetSeries(new_snapfiles) for ds,snap_dir in zip(reversed(ts),np.flipud(new_snapfiles)): print( "Getting galaxy props: ", snap_dir) ds.add_particle_filter('stars') ds.add_particle_filter('darkmatter') dd = ds.all_data() ds.domain_right_edge = ds.arr(ds.domain_right_edge,'code_length') ds.domain_left_edge = ds.arr(ds.domain_left_edge,'code_length')
import sys from collections import defaultdict import numpy as np import matplotlib.pyplot as plt import os def mass_add(field, data): return data['mvir'].to('Msun/h') def stars(pfilter, data): filter = data[(pfilter.filtered_type, 'creation_time')] > 0.0 return filter yt.add_particle_filter('stars', function=stars, filtered_type = 'all', \ requires=['creation_time']) final = int(sys.argv[2]) simname = sys.argv[1] #data = "/media/azton/bigbook/projects/nextGenIGM/%s"%simname data = '/home/azton/simulations/%s' % simname results = "/home/azton/simulations/analysis/%s" % simname tree = ytree.load('%s/rockstar_halos/trees/tree_0_0_0.dat' % data) tree.add_derived_field('mass', mass_add, units='Msun/h') mostMassive = tree[np.where(tree['mvir'] == max(tree['mvir']))[0][0]] zlist = mostMassive['prog', 'redshift'] mmpInfo = defaultdict(list) fstar = []
ts = yt.load([ prefix + '/Data_%06d' % idx for idx in range(idx_start, idx_end + 1, didx) ]) #ts = yt.load( 'Data_??????' ) # define the particle filter for the newly formed stars def new_star(pfilter, data): filter = data["all", "ParCreTime"] > 0 return filter yt.add_particle_filter("new_star", function=new_star, filtered_type="all", requires=["ParCreTime"]) AllPar = ('all', 'particle_mass') NewPar = ('new_star', 'particle_mass') for ds in ts.piter(): # add the particle filter ds.add_particle_filter("new_star") # face-on (all particles) pz = yt.ParticleProjectionPlot(ds, 'z', AllPar, center=center_mode,
def _intermediate_stars(pfilter, data): """Filter star particles with creation time 100 Myr - 1 Gyr ago To use: yt.add_particle_filter("intermediate_stars", function=_old_stars, filtered_type='stars', requires=["particle_creation_time"])""" age = data.ds.current_time - data[pfilter.filtered_type, "particle_creation_time"] filter = np.logical_and(age.in_units('Gyr') <= 1, age.in_units('Myr') > 100) return filter def _old_stars(pfilter, data): """Filter star particles with creation time > 1 Gyr ago To use: yt.add_particle_filter("old_stars", function=_old_stars, filtered_type='stars', requires=["particle_creation_time"])""" age = data.ds.current_time - data[pfilter.filtered_type, "particle_creation_time"] filter = np.logical_or(age.in_units('Gyr') > 1, age.in_units('Myr') < 0) return filter yt.add_particle_filter("new_stars", function=_new_stars, filtered_type='stars', requires=["particle_creation_time"]) yt.add_particle_filter("young_stars", function=_young_stars, filtered_type='stars', requires=["particle_creation_time"]) yt.add_particle_filter("intermediate_stars", function=_intermediate_stars, filtered_type='stars', requires=["particle_creation_time"]) yt.add_particle_filter("old_stars", function=_old_stars, filtered_type='stars', requires=["particle_creation_time"]) ds.add_particle_filter("new_stars") ds.add_particle_filter("young_stars") ds.add_particle_filter("intermediate_stars") ds.add_particle_filter("old_stars") ad = ds.all_data() redshift = round(float(ds.current_redshift), 2) sim_dict.update([('redshift', redshift)]) # Set centers
ds = yt.load('%s/%s/%s/%s/%s' % (simdir, haloname, simname, DDname, DDname)) def _stars(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 2 def _youngstars(pfilter, data): return data[(pfilter.filtered_type, "age")] < 2.e7 # these are only the must refine dark matter particles def _darkmatter(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 4 yt.add_particle_filter("stars", function=_stars, filtered_type='all', requires=["particle_type"]) yt.add_particle_filter("youngstars", function=_youngstars, filtered_type='all', requires=["age"]) yt.add_particle_filter("darkmatter", function=_darkmatter, filtered_type='all', requires=["particle_type"]) ds.add_particle_filter('stars') ds.add_particle_filter('darkmatter') ds.add_particle_filter('youngstars') print('adding trident fields...') trident.add_ion_fields(ds,
ds = yt.load(HOME + '/models/simulation_output/' + fn + '/' + fn[:6]) dd = ds.all_data() if args.plotmap: # ---------to plot the original simulation projected density map------------------ p = yt.visualization.plot_window.ProjectionPlot(ds, 'z', ('gas', 'density'), width=(20, 'kpc'), fontsize=40) p.set_unit('density', 'Msun/pc**2') p.set_zlim('density', 10**0., 3 * 10**2) p.save(HOME + '/Dropbox/papers/enzo_paper/Figs/' + fn + '_gas_density_map.eps') yt.add_particle_filter('young_stars', function=young_stars, filtered_type='all', requires=['age', \ 'creation_time']) ds.add_particle_filter('young_stars') print 'Filtered stars, now extracting parameters...' ### xg = dd['young_stars', 'particle_position_x'] x = xg.in_units('kpc') yg = dd['young_stars', 'particle_position_y'] y = yg.in_units('kpc') zg = dd['young_stars', 'particle_position_z'] z = zg.in_units('kpc') vz = dd['young_stars', 'particle_velocity_z'] vz = vz.in_units('km/s') a = dd['young_stars', 'age'] a = a.in_units('Myr') m = dd['young_stars', 'particle_mass']
def generate_data(cluster, tfrecord_dir, base_data_dir, cluster_dirs, snap_dir, centers, number_of_projections=26, exp_time=1000., redshift=0.20, number_of_virtual_nodes=1000, number_of_neighbours=26, plotting=0): cluster_idx = get_index(cluster) good_cluster = True print(f'\nStarting new cluster : {cluster_idx}') yr = 3.15576e7 # in seconds pc = 3.085678e18 # in cm Mpc = 1e6 * pc M_sun = 1.989e33 # in gram # Parameters for making the xray images exp_t = (exp_time, "ks") # exposure time area = (1000.0, "cm**2") # collecting area emin = 0.05 # Minimum energy of photons in keV emax = 11.0 # Maximum energy of photons in keV metallicty = 0.3 # Metallicity in units of solar metallicity kt_min = 0.05 # Minimum temperature to solve emission for n_chan = 1000 # Number of channels in the spectrum hydrogen_dens = 0.04 # The foreground column density in units of 10^22 cm^{-2}. Only used if absorption is applied. radius = (4.0, "Mpc") # Radius of the sphere which captures photons sky_center = [ 0., 0. ] # Ra and dec coordinates of the cluster (which are currently dummy values) hot_gas_temp = 10**5.4 units = np.array([ Mpc, Mpc, Mpc, 1e-4 * pc / yr, 1e-4 * pc / yr, 1e-4 * pc / yr, 1e-7 * M_sun / pc**3, 1e-7 * (pc / yr)**2, 1e8 * M_sun, 1e5 * pc ]) # Load in particle data and prepare for making an xray image. if get_simulation_name(cluster) == 'Bahamas': properties, c, ds = load_data_bahamas(cluster=cluster, centers=centers, base_data_dir=base_data_dir) else: properties, c, ds = load_data_magneticum(cluster=cluster, snap_dir=snap_dir) sp = ds.sphere(c, radius) _box_size, split = check_split(ds, properties[:, :3]) print(f'\nBox size : {_box_size}') print(f'Cluster center : {sp.center}') print(f'Split cluster : {split}') if split: print( f'\nThe positions of the particles in cluster {cluster_idx} are ' f'split by a periodic boundary and the easiest solution for this ' f'is to leave the cluster out of the dataset.') good_cluster = False if properties.shape[0] < number_of_virtual_nodes: print( f'\nThe cluster contains {properties.shape[0]} particles ' f'which is not enough to make {number_of_virtual_nodes} virtual nodes.' ) good_cluster = False # Set a minimum temperature to leave out that shouldn't be X-ray emitting, # set metallicity to 0.3 Zsolar (should maybe fix later) # The source model determines the distribution of photons that are emitted source_model = pyxsim.ThermalSourceModel(spectral_model="apec", emin=emin, emax=emax, nchan=n_chan, Zmet=metallicty, kT_min=kt_min) # Create the photonlist photons = pyxsim.PhotonList.from_data_source(data_source=sp, redshift=redshift, area=area, exp_time=exp_t, source_model=source_model) # Calculate the physical diameter of the image with : distance * fov = diameter chandra_acis_fov = 0.0049160 # in radians cutout_box_size = photons.parameters["fid_d_a"].d * chandra_acis_fov * Mpc number_of_photons = int(np.sum(photons["num_photons"])) if number_of_photons > 5e8: print( f'\nThe number of photons {number_of_photons} is too large and will take too long to process ' f'so cluster {cluster_idx} is skipped.') good_cluster = False if plotting > 0: # This is a filter which creates a new particle type (in memory), which # makes a cut on gas temperature to only consider gas that will really be # X-ray emitting def hot_gas(pfilter, data): temp = data[pfilter.filtered_type, "temperature"] return temp > hot_gas_temp yt.add_particle_filter("hot_gas", function=hot_gas, filtered_type='gas', requires=["temperature"]) ds.add_particle_filter("hot_gas") def data_generator(): for projection_idx in tqdm(np.arange(number_of_projections)): print(f'\n\nCluster file: {cluster}') print(f'Cluster index: {cluster_idx}') print( f'Clusters done (or in the making) : {len(glob.glob(os.path.join(tfrecord_dir, "*")))}' ) print( f'Projection : {projection_idx + 1} / {number_of_projections}\n' ) _properties = properties.copy() # Rotate variables rot_mat = _random_special_ortho_matrix(3) # rot_mat = np.eye(3) _properties[:, :3] = (rot_mat @ _properties[:, :3].T).T _properties[:, 3:6] = (rot_mat @ _properties[:, 3:6].T).T center = (rot_mat @ np.array(sp.center.in_cgs()).T).T # Cut out box in 3D space lower_lim = center - 0.5 * cutout_box_size * np.array([1, 1, 1]) upper_lim = center + 0.5 * cutout_box_size * np.array([1, 1, 1]) indices = np.where((_properties[:, 0:3] < lower_lim) | (_properties[:, 0:3] > upper_lim))[0] _properties = np.delete(_properties, indices, axis=0) # Scale the variables _properties[:, 0:3] = (_properties[:, 0:3] - center) / units[0:3] _properties[:, 3:6] = _properties[:, 3:6] / units[3:6] _properties[:, 6:] = np.log10(_properties[:, 6:] / units[6:]) center /= units[0:3] print(f'Properties :', _properties[0]) print('Properties shape: ', _properties.shape) if plotting > 0: hot_gas_pos = ds.all_data()['hot_gas', 'position'].in_cgs().d hot_gas_pos = (rot_mat @ hot_gas_pos.T).T hot_gas_pos = (hot_gas_pos / units[0:3]) - center else: hot_gas_pos = None v = np.eye(3) vprime = rot_mat.T @ v north_vector = vprime[:, 1] viewing_vec = vprime[:, 2] # Finds the events along a certain line of sight cluster_projection_identity = number_of_projections * cluster_idx + projection_idx events_z = photons.project_photons(viewing_vec, sky_center, absorb_model="tbabs", nH=hydrogen_dens, north_vector=north_vector) events_z.write_simput_file(f'snap_{cluster_projection_identity}', overwrite=True) # Determine which events get detected by the AcisI instrument of Chandra soxs.instrument_simulator( f'snap_{cluster_projection_identity}_simput.fits', f'snap_{cluster_projection_identity}_evt.fits', exp_t, "chandra_acisi_cy0", sky_center, overwrite=True, ptsrc_bkgnd=False, foreground=False, instr_bkgnd=False) soxs.write_image(f'snap_{cluster_projection_identity}_evt.fits', f'snap_{cluster_projection_identity}_img.fits', emin=emin, emax=emax, overwrite=True) with fits.open( f'snap_{cluster_projection_identity}_img.fits') as hdu: xray_image = np.array( hdu[0].data, dtype='float32')[1358:3406, 1329:3377] # [2048,2048] temp_fits_files = glob.glob( os.path.join(os.getcwd(), f'snap_{cluster_projection_identity}_*.fits')) for file in temp_fits_files: print(f'Removing : {os.path.basename(file)}') os.remove(file) xray_image = downsample(xray_image).numpy()[:, :, None] xray_image = np.log10(np.where(xray_image < 1e-5, 1e-5, xray_image)) # For imshow the image is flipped plt_xray_image = xray_image[:, :, 0][::-1, :] # Create a graph with the positions and properties graph = generate_example_random_choice( _properties, plt_xray_image, hot_gas_positions=hot_gas_pos, number_of_virtual_nodes=number_of_virtual_nodes, k=number_of_neighbours, plot=plotting, base_data_dir=base_data_dir, center=center, vprime=vprime, dataset=ds, sphere=sp) # This function is a generator, which has the advantage of not keeping used and upcoming data in memory. yield (graph, xray_image, cluster_idx, projection_idx, vprime) if good_cluster: # Save the data as tfrecords and return the filenames of the tfrecords save_examples(data_generator(), save_dir=tfrecord_dir, examples_per_file=number_of_projections, num_examples=number_of_projections * len(cluster_dirs), exp_time=exp_t[0], prefix='train')
#Defining particle filters for RAMSES def Stars(pfilter, data): age = data[(pfilter.filtered_type, "particle_age")] filter = np.logical_or(age < 0, age > 0) return filter def DM(pfilter, data): filter = data[(pfilter.filtered_type, "particle_age")] == 0 return filter yt.add_particle_filter("stars", function=Stars, filtered_type='all', requires=['particle_age']) yt.add_particle_filter("dm", function=DM, filtered_type='all', requires=['particle_age']) #datapath = 'output_00080/info_00080.txt' datapath = 'output_00016/info_00016.txt' ds = yt.load(datapath) ds.add_particle_filter("stars") ds.add_particle_filter("dm") ds.derived_field_list
def arepo_field_add(fname, bounding_box=None, ds=None): def _starmetals(field, data): return data[('newstars', 'GFM_Metallicity')] def _starcoordinates(field, data): return data[('newstars', 'Coordinates')] def _starformationtime(field, data): return data[('newstars', 'GFM_StellarFormationTime')] def _starmasses(field, data): return data[("newstars", "Masses")] # def _diskstarcoordinates(field, data): # return data[('PartType2', 'Coordinates')] # def _diskstarmasses(field, data): # return data[("PartType2", "Masses")] # def _bulgestarcoordinates(field, data): # return data[('PartType3', 'Coordinates')] # def _bulgestarmasses(field, data): # return data[("PartType3", "Masses")] def _gasdensity(field, data): return data[('PartType0', 'density')] def _gasmetals(field, data): return data[('PartType0', 'GFM_Metallicity')] def _gascoordinates(field, data): return data[('PartType0', 'Coordinates')] def _gasmasses(field, data): return data[('PartType0', 'Masses')] def _gasfh2(field, data): try: return data[('PartType0', 'FractionH2')] except: return data[('PartType0', 'GFM_Metallicity' )] * 0. #just some dimensionless array def _gassfr(field, data): return data[('PartType0', 'StarFormationRate')] def _metaldens(field, data): return (data["PartType0", "density"] * data["PartType0", "GFM_Metallicity"]) def _metalmass(field, data): return (data["PartType0", "Masses"] * (data["PartType0", "GFM_Metallicity"].value)) def _dustmass(field, data): return (data.ds.arr(data[("PartType0", "Dust_Masses")].value, 'code_mass')) def _li_ml_dustmass(field, data): li_ml_dgr = dgr_ert(data["gasmetals"], data["PartType0", "StarFormationRate"], data["PartType0", "Masses"]) li_ml_dustmass = ((10.**li_ml_dgr) * data["PartType0", "Masses"]).in_units('code_mass') #ds.parameters['li_ml_dustmass'] = li_ml_dustmass ##return (data.ds.arr(data.ds.parameters['li_ml_dustmass'].value,'code_mass')) return li_ml_dustmass def _stellarages(field, data): ad = data.ds.all_data() if data.ds.cosmological_simulation == False: simtime = data.ds.current_time.in_units('Gyr') simtime = simtime.value age = simtime - data[( "PartType4", "GFM_StellarFormationTime")].in_units('Gyr').value # make the minimum age 1 million years age[np.where(age < 1.e-3)[0]] = 1.e-3 print('\n--------------') print( '[arepo2pd: ] Idealized Galaxy Simulation Assumed: Simulation time is (Gyr): ', simtime) print('--------------\n') else: yt_cosmo = yt.utilities.cosmology.Cosmology( hubble_constant=data.ds.hubble_constant, omega_matter=data.ds.omega_matter, omega_lambda=data.ds.omega_lambda) simtime = yt_cosmo.t_from_z(ds.current_redshift).in_units( 'Gyr').value # Current age of the universe scalefactor = data[("PartType4", "GFM_StellarFormationTime")].value formation_z = (1. / scalefactor) - 1. formation_time = yt_cosmo.t_from_z(formation_z).in_units( 'Gyr').value age = simtime - formation_time # Minimum age is set to 1 Myr (FSPS doesn't work properly for ages below 1 Myr) age[np.where(age < 1.e-3)[0]] = 1.e-3 print('\n--------------') print( '[arepo2pd: ] Cosmological Galaxy Simulation Assumed: Current age of Universe is (Gyr): ', simtime) print('--------------\n') age = data.ds.arr(age, 'Gyr') return age ''' def _bhluminosity(field, data): ad = data.ds.all_data() mdot = ad[("PartType5", "BH_Mdot")] # give it a unit since usually these are dimensionless in yt mdot = data.ds.arr(mdot, "code_mass/code_time") c = yt.utilities.physical_constants.speed_of_light_cgs bhluminosity = (cfg.par.BH_eta * mdot * c**2.).in_units("erg/s") if cfg.par.BH_var: return bhluminosity * cfg.par.bhlfrac else: return bhluminosity def _bhcoordinates(field, data): return data["PartType5", "Coordinates"] def _bhsed_nu(field, data): bhluminosity = data["bhluminosity"] log_lum_lsun = np.log10(bhluminosity[0].in_units("Lsun")) nu, bhlum = agn_spectrum(log_lum_lsun) # the last 4 numbers aren't part of the SED nu = nu[0:-4] nu = 10.**nu nu = yt.YTArray(nu, "Hz") return nu def _bhsed_sed(field, data): bhluminosity = data["bhluminosity"] nholes = len(bhluminosity) # get len of nu just for the 0th hole so we know how long the vector is log_lum_lsun = np.log10(bhluminosity[0].in_units("Lsun")) nu, l_band_vec = agn_spectrum(log_lum_lsun) nu = nu[0:-4] n_nu = len(nu) bh_sed = np.zeros([nholes, n_nu]) for i in range(nholes): log_lum_lsun = np.log10(bhluminosity[i].in_units("Lsun")) nu, l_band_vec = agn_spectrum(log_lum_lsun) l_band_vec = 10.**l_band_vec l_band_vec = l_band_vec[0:-4] for l in range(len(l_band_vec)): l_band_vec[l] = data.ds.quan(l_band_vec[l], "erg/s") bh_sed[i, :] = l_band_vec bh_sed = yt.YTArray(bh_sed, "erg/s") return bh_sed ''' # load the ds (but only if this is our first passthrough and we pass in fname) if fname != None: try: yt.__version__ == '4.0.dev0' ds = yt.load(fname) ds.index ad = ds.all_data() except: raise ValueError( "It appears as though you are running in yt3.x The vornoi mesh cannot be read in yt3.x. Please update to yt4.x following the instructions here: https://powderday.readthedocs.io/en/latest/installation.html#yt-4-x-configuration-wip" ) #set up particle_filters to figure out which particles are stars. #we'll call particles that have ages > 0 newstars. def _newstars(pfilter, data): filter = data[(pfilter.filtered_type, "GFM_StellarFormationTime")] > 0 return filter yt.add_particle_filter("newstars", function=_newstars, filtered_type='PartType4') ds.add_particle_filter("newstars") ds.add_field(('starmetals'), function=_starmetals, units="code_metallicity", particle_type=True) ds.add_field(('gasmetals'), function=_gasmetals, units="code_metallicity", particle_type=True) ds.add_field(('metaldens'), function=_metaldens, units="g/cm**3", particle_type=True) ds.add_field(('PartType0', 'metalmass'), function=_metalmass, units="g", particle_type=True) # get the dust mass if ('PartType0', 'Dust_Masses') in ds.derived_field_list: ds.add_field(('dustmass'), function=_dustmass, units='code_mass', particle_type=True) ds.add_deposited_particle_field(("PartType0", "Dust_Masses"), "sum") #if we have the Li, Narayanan & Dave 2019 Extreme Randomized Trees #dust model in place, create a field for these so that #dust_grid_gen can use these dust masses if cfg.par.dust_grid_type == 'li_ml': #get the dust to gas ratio #ad = ds.all_data() #li_ml_dgr = dgr_ert(ad["gasmetals"],ad["PartType0","StarFormationRate"],ad["PartType0","Masses"]) #li_ml_dustmass = ((10.**li_ml_dgr)*ad["PartType0","Masses"]).in_units('code_mass') #this is an icky way to pass this to the function for ds.add_field in the next line. but such is life. #ds.parameters['li_ml_dustmass'] = li_ml_dustmass ds.add_field(('li_ml_dustmass'), function=_li_ml_dustmass, units='code_mass', particle_type=True) ds.add_field(('starmasses'), function=_starmasses, units='g', particle_type=True) ds.add_field(('starcoordinates'), function=_starcoordinates, units='cm', particle_type=True) ds.add_field(('starformationtime'), function=_starformationtime, units='dimensionless', particle_type=True) ds.add_field(('stellarages'), function=_stellarages, units='Gyr', particle_type=True) # if ('PartType2', 'Masses') in ds.derived_field_list: # ds.add_field(('diskstarmasses'), function=_diskstarmasses, units='g', particle_type=True) # ds.add_field(('diskstarcoordinates'), function=_diskstarcoordinates, units='cm', particle_type=True) # if ('PartType3', 'Masses') in ds.derived_field_list: # ds.add_field(('bulgestarmasses'), function=_bulgestarmasses, units='g', particle_type=True) # ds.add_field(('bulgestarcoordinates'), function=_bulgestarcoordinates, units='cm', particle_type=True) ds.add_field(('gasdensity'), function=_gasdensity, units='g/cm**3', particle_type=True) # Gas Coordinates need to be in Comoving/h as they'll get converted later. ds.add_field(('gascoordinates'), function=_gascoordinates, units='cm', particle_type=True) ds.add_field(('gasmasses'), function=_gasmasses, units='g', particle_type=True) ds.add_field(('gasfh2'), function=_gasfh2, units='dimensionless', particle_type=True) ds.add_field(('gassfr'), function=_gassfr, units='g/s', particle_type=True) if cfg.par.BH_SED == True: try: nholes = len(ds.all_data()[('PartType5', 'BH_Mass')]) if nholes > 0: if cfg.par.BH_model == 'Nenkova': from powderday.agn_models.nenkova import Nenkova2008 agn_spectrum = Nenkova2008( *cfg.par.nenkova_params).agn_spectrum else: from powderday.agn_models.hopkins import agn_spectrum if cfg.par.BH_var: from powderday.agn_models.hickox import vary_bhluminosity cfg.par.bhlfrac = vary_bhluminosity(nholes) ds.add_field(("bhluminosity"), function=_bhluminosity, units='erg/s', particle_type=True) ds.add_field(("bhcoordinates"), function=_bhcoordinates, units="cm", particle_type=True) ds.add_field(("bhnu"), function=_bhsed_nu, units='Hz', particle_type=True) ds.add_field(("bhsed"), function=_bhsed_sed, units="erg/s", particle_type=True) else: print('No black holes found (length of BH_Mass field is 0)') except: print('Unable to find field "BH_Mass" in snapshot. Skipping.') return ds
"HeII_Density", "HeI_Density" ] eng_units = 'g/(cm*s**2)' ef('xtra_fields_always.py') if 'add_field' not in dir(): from yt import add_particle_filter, add_field, ValidateGridType def formed_star(pfilter, data): filter = data["all", "creation_time"] > 0 return filter add_particle_filter("formed_star", function=formed_star, filtered_type='all', requires=["creation_time"]) ef('xtra_fields_particles.py') ef('xtra_energy_fields.py') if 0: #these take arguments (CenterOfMass, AngularMomentumVector, Positions, Velocities) import yt.utilities.math_utils as mu compute_vphi = mu.compute_rotational_velocity compute_vz = mu.compute_parallel_velocity compute_vr = mu.compute_radial_velocity #'compute_parallel_velocity', 'compute_radial_velocity', 'compute_rotational_velocity'
print('Could not find corresponding VELA 10Mpc File for snapshot:', VELA_a) else: ds = yt.load(VELA_snaps[position[0]]) domain_width = float(ds.domain_width.in_units('Mpc/h')[0]) ad = ds.all_data() masses = yt.np.unique(ad[('darkmatter', 'particle_mass')]) #filter out the darkmatter0 particles def mass_filter(pfilter, data): filter = data[(pfilter.filtered_type, 'particle_mass')] == masses[0] return filter yt.add_particle_filter('darkmatter0', function=mass_filter, filtered_type='darkmatter', requires=['particle_mass']) ds.add_particle_filter('darkmatter0') scale = float(ds.scale_factor) fig = plt.figure() grid = AxesGrid(fig, (0.075, 0.075, 10, 5), nrows_ncols=(3, 1), axes_pad=1.0, label_mode="L", share_all=False, cbar_location="right", cbar_mode="each", cbar_size="3%",
def make_figure(figdir, DD, cen_name, simdir, haloname, simname, wd=100., wdd=100., wd2=150., wdd2=150., wd3=1000., wdd3=1000.): #figname_zoomoutfar = '%s_%.4i_%.2i_%s_zoomoutfar.png'%(cen_name, DD, 6, 'x') #figname_check = '%s/%s/%s/%s'%(figdir,'x', 'zoomoutfar', figname_zoomoutfar) #if os.path.isfile(figname_check): return DDname = 'DD%.4i' % DD ds = yt.load('%s/%s/%s/%s/%s' % (simdir, haloname, simname, DDname, DDname)) def _stars(pfilter, data): return data[(pfilter.filtered_type, "particle_type")] == 2 yt.add_particle_filter("stars", function=_stars, filtered_type='all', requires=["particle_type"]) ds.add_particle_filter('stars') cen_fits = np.load( '/nobackupp2/rcsimons/foggie_momentum/catalogs/sat_interpolations/%s_interpolations_DD0150_new.npy' % cen_name, allow_pickle=True)[()] ''' #encoding='latin1' is needed for loading python 2 pickles in python 3 if 'natural' in cen_name: central_xyz_fit = np.load('/nobackupp2/rcsimons/foggie_momentum/catalogs/center_natural.npy', allow_pickle=True, encoding='latin1')[()] elif ('nref11n_nref10f' in cen_name) | ('nref11c_nref9f' in cen_name): central_xyz_fit = np.load('/nobackupp2/rcsimons/foggie_momentum/catalogs/center_nref11n_nref10f.npy', allow_pickle=True, encoding='latin1')[()] ''' #central_x = cen_fits['CENTRAL']['fxe'](DD) #central_y = cen_fits['CENTRAL']['fye'](DD) #central_z = cen_fits['CENTRAL']['fze'](DD) central_x = cen_fits['CENTRAL']['fxe'](DD) central_y = cen_fits['CENTRAL']['fye'](DD) central_z = cen_fits['CENTRAL']['fze'](DD) print(central_x, central_y, central_z) ''' xf = central_xyz_fit['x'] yf = central_xyz_fit['y'] zf = central_xyz_fit['z'] ''' #central_x = xf[0] * DD**4. + xf[1] * DD**3. + xf[2] * DD**2. + xf[3] * DD + xf[4] #central_y = yf[0] * DD**4. + yf[1] * DD**3. + yf[2] * DD**2. + yf[3] * DD + yf[4] #central_z = zf[0] * DD**4. + zf[1] * DD**3. + zf[2] * DD**2. + zf[3] * DD + zf[4] cen_central = yt.YTArray([central_x, central_y, central_z], 'kpc') W = yt.YTArray([wd, wd, wd], 'kpc') W2 = yt.YTArray([wd2, wd2, wd2], 'kpc') W3 = yt.YTArray([wd3, wd3, wd3], 'kpc') for axis in ['x']: #@, 'y', 'z']: if axis == 'x': box2 = ds.r[cen_central[0] - 0.5 * yt.YTArray(wdd2, 'kpc'): cen_central[0] + 0.5 * yt.YTArray(wdd2, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[1] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(3*wd2, 'kpc')] box3 = ds.r[cen_central[0] - 0.5 * yt.YTArray(wdd3, 'kpc'): cen_central[0] + 0.5 * yt.YTArray(wdd3, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(3*wd3, 'kpc'): cen_central[1] + 0.5 * yt.YTArray(3*wd3, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(3*wd3, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(3*wd3, 'kpc')] if axis == 'y': box2 = ds.r[cen_central[0] - 0.5 * yt.YTArray(3*wd2, 'kpc') : cen_central[0] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(wdd2, 'kpc') : cen_central[1] + 0.5 * yt.YTArray(wdd2, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(3*wd2, 'kpc')] box3 = ds.r[cen_central[0] - 0.5 * yt.YTArray(3*wd3, 'kpc') : cen_central[0] + 0.5 * yt.YTArray(3*wd3, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(wdd3, 'kpc') : cen_central[1] + 0.5 * yt.YTArray(wdd3, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(3*wd3, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(3*wd3, 'kpc')] if axis == 'z': box2 = ds.r[cen_central[0] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[0] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(3*wd2, 'kpc'): cen_central[1] + 0.5 * yt.YTArray(3*wd2, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(wdd2, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(wdd2, 'kpc')] box3 = ds.r[cen_central[0] - 0.5 * yt.YTArray(3*wd3, 'kpc'): cen_central[0] + 0.5 * yt.YTArray(3*wd3, 'kpc'), \ cen_central[1] - 0.5 * yt.YTArray(3*wd3, 'kpc'): cen_central[1] + 0.5 * yt.YTArray(3*wd3, 'kpc'), \ cen_central[2] - 0.5 * yt.YTArray(wdd3, 'kpc'): cen_central[2] + 0.5 * yt.YTArray(wdd3, 'kpc')] ''' a = time.time() p_wd2_g= yt.ProjectionPlot(ds, axis, ("gas","density"), center = cen_central, data_source=box2, width=W2) b = time.time() print ('p_wd2_g', b-a) p_wd2_g.set_unit(('gas','density'), 'Msun/pc**2') p_wd2_g.set_zlim(('gas', 'density'), zmin = density_proj_min, zmax = density_proj_max) p_wd2_g.set_cmap(('gas', 'density'), density_color_map) p_wd2_g.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p_wd2_g.hide_axes() p_wd2_g.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p_wd2_g.annotate_scale(size_bar_args={'color':'white'}) a = time.time() p_wd2_s = yt.ParticleProjectionPlot(ds, axis, ('stars', 'particle_mass'), center = cen_central, data_source=box2, width = W2) b = time.time() print ('p_wd2_s', b-a) cmp = plt.cm.Greys_r cmp.set_bad('k') p_wd2_s.set_cmap(field = ('stars','particle_mass'), cmap = cmp) p_wd2_s.hide_axes() p_wd2_s.annotate_scale(size_bar_args={'color':'white'}) p_wd2_s.set_zlim(field = ('stars','particle_mass'), zmin = 2.e35 * 0.3, zmax = 1.e42*0.9) a = time.time() p_wd3_g= yt.ProjectionPlot(ds, axis, ("gas","density"), center = cen_central, data_source=box3, width=W3) b = time.time() print ('p_wd3_g', b-a) p_wd3_g.set_unit(('gas','density'), 'Msun/pc**2') p_wd3_g.set_zlim(('gas', 'density'), zmin = density_proj_min, zmax = density_proj_max) p_wd3_g.set_cmap(('gas', 'density'), density_color_map) p_wd3_g.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p_wd3_g.hide_axes() p_wd3_g.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p_wd3_g.annotate_scale(size_bar_args={'color':'white'}) a = time.time() p_wd3_s = yt.ParticleProjectionPlot(ds, axis, ('stars', 'particle_mass'), center = cen_central, data_source=box3, width = W3) b = time.time() print ('p_wd3_s', b-a) cmp = plt.cm.Greys_r cmp.set_bad('k') p_wd3_s.set_cmap(field = ('stars','particle_mass'), cmap = cmp) p_wd3_s.hide_axes() p_wd3_s.annotate_scale(size_bar_args={'color':'white'}) p_wd3_s.set_zlim(field = ('stars','particle_mass'), zmin = 2.e35 * 0.3, zmax = 1.e42*0.9) ''' for sat_n in arange(6, 7): if sat_n < 6: cenx = cen_fits['SAT_%.2i' % sat_n]['fxe'](DD) ceny = cen_fits['SAT_%.2i' % sat_n]['fye'](DD) cenz = cen_fits['SAT_%.2i' % sat_n]['fze'](DD) if sat_n == 6: #cenx = cen_fits['CENTRAL']['fxe'](DD) #ceny = cen_fits['CENTRAL']['fye'](DD) #cenz = cen_fits['CENTRAL']['fze'](DD) cenx = cen_fits['CENTRAL']['fxe'](DD) ceny = cen_fits['CENTRAL']['fye'](DD) cenz = cen_fits['CENTRAL']['fze'](DD) cen_g = yt.YTArray([cenx, ceny, cenz], 'kpc') print('satellite center: ', cen_g) print('central center: ', cen_central) figname_zoomin = '%s_%.4i_%.2i_%s_zoomin_100kpc.png' % ( cen_name, DD, sat_n, axis) figname_zoomout = '%s_%.4i_%.2i_%s_zoomout.png' % (cen_name, DD, sat_n, axis) figname_zoomoutfar = '%s_%.4i_%.2i_%s_zoomoutfar.png' % ( cen_name, DD, sat_n, axis) if axis == 'x': box = ds.r[cen_g[0] - 0.5 * yt.YTArray(wdd, 'kpc'): cen_g[0] + 0.5 * yt.YTArray(wdd, 'kpc'), \ cen_g[1] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[1] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[2] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[2] + 0.5 * yt.YTArray(3*wd, 'kpc')] p_1 = cen_g[1] - cen_central[1] p_2 = cen_g[2] - cen_central[2] p_3 = cen_g[0] - cen_central[0] elif axis == 'y': box = ds.r[cen_g[0] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[0] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[1] - 0.5 * yt.YTArray(wdd, 'kpc'): cen_g[1] + 0.5 * yt.YTArray(wdd, 'kpc'), \ cen_g[2] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[2] + 0.5 * yt.YTArray(3*wd, 'kpc')] p_1 = cen_g[2] - cen_central[2] p_2 = cen_g[0] - cen_central[0] p_3 = cen_g[1] - cen_central[1] elif axis == 'z': box = ds.r[cen_g[0] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[0] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[1] - 0.5 * yt.YTArray(3*wd, 'kpc'): cen_g[1] + 0.5 * yt.YTArray(3*wd, 'kpc'), \ cen_g[2] - 0.5 * yt.YTArray(wdd, 'kpc'): cen_g[2] + 0.5 * yt.YTArray(wdd, 'kpc')] p_1 = cen_g[0] - cen_central[0] p_2 = cen_g[1] - cen_central[1] p_3 = cen_g[2] - cen_central[2] fig = plt.figure(sat_n) grid = AxesGrid(fig, (0.0, 0.0, 1.0, 1.0), nrows_ncols=(1, 2), axes_pad=0.0, label_mode="1", share_all=False, cbar_mode=None, aspect=False) p = yt.ProjectionPlot(ds, axis, ("gas", "density"), center=cen_g, data_source=box, width=W) p.set_unit(('gas', 'density'), 'Msun/pc**2') p.set_zlim(('gas', 'density'), zmin=density_proj_min, zmax=density_proj_max) p.set_cmap(('gas', 'density'), density_color_map) p.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p.hide_axes() p.annotate_timestamp(corner='upper_left', redshift=True, draw_inset_box=True) p.annotate_scale(size_bar_args={'color': 'white'}) plot = p.plots[("gas", "density")] plot.figure = fig plot.axes = grid[0].axes p._setup_plots() p = yt.ParticleProjectionPlot(ds, axis, ('stars', 'particle_mass'), center=cen_g, data_source=box, width=W) cmp = plt.cm.Greys_r cmp.set_bad('k') p.set_cmap(field=('stars', 'particle_mass'), cmap=cmp) p.hide_axes() p.annotate_scale(size_bar_args={'color': 'white'}) p.set_zlim(field=('stars', 'particle_mass'), zmin=2.e35 * 0.3, zmax=1.e42 * 0.9) plot = p.plots[('stars', 'particle_mass')] plot.figure = fig plot.axes = grid[1].axes p._setup_plots() fig.set_size_inches(12, 6) fig.savefig('%s/%s/%s/%s' % (figdir, axis, 'zoomin', figname_zoomin)) plt.close(fig) ''' fig = plt.figure(sat_n) grid = AxesGrid(fig, (0.0,0.0,1.0,1.0), nrows_ncols = (1, 2), axes_pad = 0.0, label_mode = "1", share_all = False, cbar_mode=None, aspect = False) p = copy.copy(p_wd2_g) plot = p.plots[("gas","density")] plot.figure = fig plot.axes = grid[0].axes p._setup_plots() print (abs(p_1), abs(p_2), W2) if (abs(p_1) < wd2/2.) & (abs(p_2) < wd2/2.) & (abs(p_3) < wd2/2.): plot.axes.scatter(p_1, p_2, marker = 'o', facecolor = "none", edgecolor='red', lw = 2, s = 800) p = copy.copy(p_wd2_s) plot = p.plots[('stars','particle_mass')] plot.figure = fig plot.axes = grid[1].axes p._setup_plots() #if (abs(p_1) < wd2/2.) & (abs(p_2) < wd2/2.) & (abs(p_3) < wd2/2.): plot.axes.scatter(p_1, p_2, marker = 'o', facecolor = "none", edgecolor='red', lw = 2, s = 800) fig.set_size_inches(12, 6) fig.savefig('%s/%s/%s/%s'%(figdir,axis, 'zoomout', figname_zoomout)) plt.close(fig) fig = plt.figure(sat_n) grid = AxesGrid(fig, (0.0,0.0,1.0,1.0), nrows_ncols = (1, 2), axes_pad = 0.0, label_mode = "1", share_all = False, cbar_mode=None, aspect = False) p = copy.copy(p_wd3_g) plot = p.plots[("gas","density")] plot.figure = fig plot.axes = grid[0].axes p._setup_plots() print (abs(p_1), abs(p_2), W2) #if (abs(p_1) < wd3/2.) & (abs(p_2) < wd3/2.) & (abs(p_3) < wdd3/2.): plot.axes.scatter(p_1, p_2, marker = 'o', facecolor = "none", edgecolor='red', lw = 2, s = 800) p = copy.copy(p_wd3_s) plot = p.plots[('stars','particle_mass')] plot.figure = fig plot.axes = grid[1].axes p._setup_plots() #if (abs(p_1) < wd3/2.) & (abs(p_2) < wd3/2.) & (abs(p_3) < wdd3/2.): plot.axes.scatter(p_1, p_2, marker = 'o', facecolor = "none", edgecolor='red', lw = 2, s = 800) fig.set_size_inches(12, 6) fig.savefig('%s/%s/%s/%s'%(figdir,axis, 'zoomoutfar', figname_zoomoutfar)) plt.close(fig) ''' ds.index.clear_all_data()
def plotting_xyz_projection(ds, zoom, center, rvir, tomer_center, tomer_rvir, VELA_a, input_dir): #setup the figure grid to put the images onto fig = plt.figure() grid = AxesGrid(fig, (0.075, 0.075, 15, 5), nrows_ncols=(6, 3), axes_pad=0.05, label_mode="L", share_all=True, cbar_location="right", cbar_mode="single", cbar_size="3%", cbar_pad="0%") ad = ds.all_data() par_type = 'darkmatter' masses = yt.np.unique(ad[(par_type, 'particle_mass')]) for index in range(len(masses)): global filter_name, index1 index1 = index filter_name = par_type + str(index) def mass_filter(pfilter, data): filter = data[(pfilter.filtered_type, 'particle_mass')] == masses[index] return filter yt.add_particle_filter(filter_name, function=mass_filter, filtered_type=par_type, requires=['particle_mass']) ds.add_particle_filter(filter_name) global a, b, c a = yt.ParticlePlot(ds, (filter_name, 'particle_position_x'), (filter_name, 'particle_position_y'),\ (filter_name,'particle_mass')) a.set_unit((filter_name, 'particle_mass'), 'Msun') a.set_figure_size(5) a.zoom(zoom) b = yt.ParticlePlot(ds, (filter_name, 'particle_position_y'), (filter_name, 'particle_position_z'),\ (filter_name,'particle_mass')) b.set_unit((filter_name, 'particle_mass'), 'Msun') b.set_figure_size(5) b.zoom(zoom) c = yt.ParticlePlot(ds, (filter_name, 'particle_position_z'), (filter_name, 'particle_position_x'),\ (filter_name,'particle_mass')) c.set_unit((filter_name, 'particle_mass'), 'Msun') c.set_figure_size(5) c.zoom(zoom) a.annotate_sphere(tomer_center, radius=(tomer_rvir, 'kpc'), circle_args={'color': 'red'}) a.annotate_sphere(center, radius=(rvir, 'kpc'), circle_args={'color': 'black'}) b.annotate_sphere(tomer_center, radius=(tomer_rvir, 'kpc'), circle_args={'color': 'red'}) b.annotate_sphere(center, radius=(rvir, 'kpc'), circle_args={'color': 'black'}) c.annotate_sphere(tomer_center, radius=(tomer_rvir, 'kpc'), circle_args={'color': 'red'}) c.annotate_sphere(center, radius=(rvir, 'kpc'), circle_args={'color': 'black'}) plot = a.plots[(filter_name, 'particle_mass')] plot.figure = fig plot.axes = grid[index * 3].axes plot.cax = grid.cbar_axes[0] a._setup_plots() plot = b.plots[(filter_name, 'particle_mass')] plot.figure = fig plot.axes = grid[index * 3 + 1].axes plot.cax = grid.cbar_axes[0] b._setup_plots() plot = c.plots[(filter_name, 'particle_mass')] plot.figure = fig plot.axes = grid[index * 3 + 2].axes plot.cax = grid.cbar_axes[0] c._setup_plots() plt.savefig('%s/%sdarkmattercomparison.png' % (input_dir, VELA_a), bbox_inches='tight') plt.close()
fields = { 'gas': ('gas', 'density'), 'stars': ('deposit', 'stars_density'), 'dark_matter': ('deposit', 'dark_matter_density') } def stars(pfilter, data): filter = data[(pfilter.filtered_type, 'particle_type')] == 2 return filter def dark_matter(pfilter, data): filter = data[(pfilter.filtered_type, 'particle_type')] == 1 return filter yt.add_particle_filter('stars', function=stars, filtered_type='all', requires=['particle_type']) yt.add_particle_filter('dark_matter', function=dark_matter, filtered_type='all', requires=['particle_type']) ds = yt.load(directory+'/'+directory) ds.add_particle_filter('stars') ds.add_particle_filter('dark_matter') halos_ds = yt.load('halo_catalogs/catalog/catalog.0.h5') hc = HaloCatalog(data_ds=ds, halos_ds=halos_ds) hc.add_callback("sphere", factor=2.0) hc.add_callback("profile", ["radius"], [("gas", "overdensity")], weight_field="cell_volume", accumulation=True, storage="virial_quantities_profiles") hc.add_callback("virial_quantities", ["radius"], profile_storage = "virial_quantities_profiles") hc.add_callback('sphere', radius_field='radius_200', factor=5, field_parameters=dict(virial_radius=('quantity', 'radius_200'))) hc.add_callback('profile', 'virial_radius_fraction', [('gas','temperature')], storage='virial_profiles', weight_field='cell_mass', accumulation=False, output_dir='profiles') hc.load()
import sys import deepdish as dd from joblib import Parallel, delayed import multiprocessing BUFF = 1024 # # move to yt field defines # def main_sequence(pfilter,data): filter = data[(pfilter.filtered_type, "particle_type")] == 11 return filter yt.add_particle_filter("main_sequence", function=main_sequence, filtered_type="all", requires=["particle_type"]) def phase_plots(ds, to_plot = 'all', region = None): # construct a list of phase diagrams here. List contains list of tuples, where each # list is the following set of 4 items: [ x-axis field, y-axis field, cbar field, weight_field ] # pp_def = { 'T_n' : [ ('gas','number_density'), ('enzo','Temperature'), ('gas','cell_mass'), None], 'T_n_Fe' : [ ('gas','number_density'), ('enzo','Temperature'), ('gas','Fe_Mass'), None], 'T_n_O' : [ ('gas','number_density'), ('enzo','Temperature'), ('gas','O_Mass'), None], 'T_n_C' : [ ('gas','number_density'), ('enzo','Temperature'), ('gas','C_Mass'), None], 'P_T' : [ ('enzo','Temperature'), ('gas','pressure'), ('gas', 'cell_mass'), None], 'Go_n' : [ ('gas','number_density'), ('gas','G_o'), ('gas','cell_mass'), None], 'Go_r' : [ ('index','magnitude_cylindrical_radius'), ('gas','G_o'), ('gas','cell_mass'), None] }
redshifts = np.load('stored_arrays/ytree_redshifts.npy') # Load cosmological times for each dataset. cosmological_times = np.load('stored_arrays/cosmological_times.npy') # Add analysis fields to the arbor. a.add_analysis_field('is_popiii_halo', units='dimensionless') a.add_analysis_field('is_popiii_progenitor', units='dimensionless') # Create particle filter for Pop III stars. def popiii(pfilter, data): particle_type = data[(pfilter.filtered_type, "particle_type")] filter = particle_type == 5 return filter yt.add_particle_filter("popiii", function=popiii, filtered_type='all', \ requires=["particle_type"]) # Set the minimum mass threshold for virialized halos. # ~250 dark matter particles. min_mass = 5e5 # Iterate through each dataset, find all Pop III particles, and assign halos to # each live Pop III particle. for i, ds in enumerate(ts): # Add the Pop III particle filter. ds.add_particle_filter('popiii') # Load whole dataset to find all Pop III particles in the box. ad = ds.all_data()
def prep_yt_TT(self, conserved_smooth=False, force_redo=False): if 'PGas' in self.yt_ds.particle_types: Ptype = 'PGas' else: Ptype = 'Gas' if (self.yt_sp is None) or force_redo: # only need to calculate once import yt if force_redo: print("data fields are forced to recalculated.") # def Ele_num_den(field, data): # # if ("Gas", "ElectronAbundance") in data.ds.field_info: # return data[field.name[0], "Density"] * data[field.name[0], "ElectronAbundance"] * \ # (1 - data[field.name[0], "Z"] - 0.24) / mp # # else: # Assume full ionized # # return data[field.name[0], "Density"] * 1.351 * (1 - data[field.name[0], # # "Z"] - 0.24) / mp def Temp_SZ(field, data): const = kb * cross_section_thompson_cgs / mass_electron_cgs / speed_of_light_cgs**2 / mp end = data[field.name[0], "Mass"] * data[field.name[0], "ElectronAbundance"] * \ (1 - data[field.name[0], "Z"] - 0.24) return end * data[field.name[0], 'Temperature'] * const def MTsz(field, data): return data[field.name[0], 'Tsz'] * data[field.name[0], 'Mass'] def SMWTsz(field, data): ret = data[field.name[0], 'Gas_smoothed_MTsz'] ids = data[field.name[0], 'Gas_smoothed_Mass'] > 0 ret[ids] /= data[field.name[0], 'Gas_smoothed_Mass'][ids] return ret # self.yt_ds.add_field(("Gas", "END"), function=Ele_num_den, # sampling_type="particle", units="cm**(-3)") self.yt_ds.add_field(("Gas", "Tsz"), function=Temp_SZ, sampling_type="particle", units="cm**2", force_override=True) if conserved_smooth: print("conserved smoothing...") self.yt_ds.add_field(("Gas", "MTsz"), function=MTsz, sampling_type="particle", units="g/cm", force_override=True) self.yt_ds.add_smoothed_particle_field(("Gas", "Mass")) self.yt_ds.add_smoothed_particle_field(("Gas", "MTsz")) self.yt_ds.add_field(('deposit', "Gas" + "_smmothed_Tsz"), function=SMWTsz, sampling_type="cell", units="1/cm", force_override=True) else: print("Not conserved smoothing...") self.yt_ds.add_smoothed_particle_field(("Gas", "Tsz")) def _proper_gas(pfilter, data): filter = data[pfilter.filtered_type, "StarFomationRate"] < 0.1 return filter if (self.center is not None) and (self.radius is not None): self.yt_sp = self.yt_ds.sphere(center=self.center, radius=(self.radius, "kpc/h")) else: self.yt_sp = self.yt_ds.all_data() if ('Gas', 'StarFomationRate') in self.yt_ds.field_info.keys(): if len(self.yt_sp['Gas', 'StarFomationRate'][self.yt_sp['Gas', 'StarFomationRate'] >= 0.1]) > 0: yt.add_particle_filter("PGas", function=_proper_gas, filtered_type='Gas', requires=["StarFomationRate"]) self.yt_ds.add_particle_filter('PGas') Ptype = 'PGas' return Ptype