def simulated_halo_mass_function(ds, finder): hc = HaloCatalog(data_ds=ds, finder_method=finder) hc.create() hmf = HaloMassFcn(halos_ds=hc.halos_ds) result = np.empty((2, hmf.masses_sim.size)) result[0] = hmf.masses_sim.d result[1] = hmf.n_cumulative_sim.d return result
def load_halos(ds,location): #Merely loads an already saved halocatalog #Calls a fn to gather positions and radii of halos over a mass threshold halo_ds = yt.load(location) hc = HaloCatalog(data_ds=ds,halos_ds=halo_ds) hc.add_filter('quantity_value','particle_mass','>',1E12,'Msun') halo_ds = hc.halos_ds.all_data() radii, positions = get_halo_pos(ds,halo_ds) return hc, radii, positions
def run(self): from yt.analysis_modules.halo_analysis.api import HaloCatalog from yt.analysis_modules.halo_mass_function.api import HaloMassFcn hc = HaloCatalog(data_ds=self.ds, finder_method=self.finder) hc.create() hmf = HaloMassFcn(halos_ds=hc.halos_ds) result = np.empty((2, hmf.masses_sim.size)) result[0] = hmf.masses_sim.d result[1] = hmf.n_cumulative_sim.d return result
def generate_halos(ds,dsname): #function to find the halos for the given dataset, using the HOP finder #Calls a fn to gather positions and radii of halos over a mass threshold #Autosaves the HC hc = HaloCatalog(data_ds=ds,finder_method='hop',finder_kwargs={'threshold':300,'padding':0.04},output_dir="".join(['~/',dsname])) hc.add_filter('quantity_value','particle_mass','>',1E12,'Msun') hc.create() hc = hc.halos_ds.all_data() radii, positions = get_halo_pos(ds,hc) return hc, radii, positions
def run(self): curdir = os.getcwd() tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) dds = data_dir_load(self.data_ds_fn) hds = data_dir_load(self.halos_ds_fn) hc = HaloCatalog(data_ds=dds, halos_ds=hds, output_dir=os.path.join(tmpdir, str(dds))) hc.add_callback("sphere") hc.add_quantity("nstars") hc.create() fn = os.path.join(tmpdir, str(dds), "%s.0.h5" % str(dds)) ds = load(fn) ad = ds.all_data() mi, ma = ad.quantities.extrema("nstars") mean = ad.quantities.weighted_average_quantity("nstars", "particle_ones") os.chdir(curdir) shutil.rmtree(tmpdir) return np.array([mean, mi, ma])
def make_catalog(code, z, finder = 'hop', output_dir="halo_catalogs/catalog"): """Creates HaloCatalog with no filters or callbacks contains; particle_identifier, particle_mass, particle_position_x, particle_position_y, particle_position_z, virial_radius for all halos in selected code. The virial mass is calculated, using the built in `Halo.virial_info` functionality. The mass is then returned. Parameters ---------- code : string Input code in ' ' or " ", No Default. z: integer The number related to the redshift you wish to observe. No Default. finder : string The only method tested to run in yt at the time of this development was 'hop', but if changed so the 'Rockstar works, input finder in ' ' or " ". Default = 'hop' output_dir : string Input where you want the catalog file to export too. Default = "halo_catalogs/catalog" Returns ------- catalog file = catalog.0.h5 Found in the output file inputted or at default location if not specified. Examples -------- >>> make_catalog('enzo', 1) """ datapath = snapshot.get_snapshot_name(code,z) data_pf = load(datapath) # With version of yt that was used when this script was written # finder_kwargs was required to be set as follows hc = HaloCatalog(data_pf=data_pf, finder_method=finder, finder_kwargs={}, output_dir=output_dir) hc.create() print "Your catalog has been created at" + str(output_dir)
def test_hmf(): es = sim_dir_load(_pf_name, path=_dir_name) es.get_time_series() ds = es[-1] hc = HaloCatalog(data_ds=ds, finder_method='fof', output_dir=os.path.join(_dir_name, "halo_catalogs/catalog")) hc.create() masses = hc.data_source['particle_mass'].in_units('Msun') h = ds.hubble_constant mtot = np.log10(masses * 1.2) - np.log10(h) masses_sim = np.sort(mtot) sim_volume = ds.domain_width.in_units('Mpccm').prod() n_cumulative_sim = np.arange(len(mtot), 0, -1) masses_sim, unique_indices = np.unique(masses_sim, return_index=True) n_cumulative_sim = n_cumulative_sim[unique_indices] / sim_volume filename = 'hmf.h5' save_filename = os.path.join(_dir_name, filename) data = {'masses': masses_sim, 'n_sim': n_cumulative_sim} yt.save_as_dataset(ds, save_filename, data) # make a plot fig = plt.figure(figsize=(8, 8)) plt.semilogy(masses_sim, n_cumulative_sim, '-') plt.ylabel('Cumulative Halo Number Density $\mathrm{Mpc}^{-3}$', fontsize=16) plt.xlabel('log Mass/$\mathrm{M}_{\odot}$', fontsize=16) plt.tick_params(labelsize=16) plt.savefig(os.path.join(_dir_name, 'hmf.png'), format='png') compare_filename = os.path.join(test_data_dir, filename) if generate_answers: os.rename(save_filename, compare_filename) return # do the comparison ds_comp = yt.load(compare_filename) # assert quality to 8 decimals assert_rel_equal(data['masses'], ds_comp.data['masses'], 8) assert_rel_equal(data['n_sim'], ds_comp.data['n_sim'], 8)
def get_halo_catalog(ds): hc = HaloCatalog(data_ds=ds, finder_method='hop') hc.create() t = Table() t['id'] = [1., 4] t['x'] = [1., 4] t['y'] = [1., 4] t['z'] = [1., 4] t['mass'] = [1., 4] t['rvir'] = [1., 4] for i in hc.catalog: print(i['particle_identifier'],i['particle_position_x'],i['particle_position_y'], \ i['particle_position_z'],i['particle_mass'], i['virial_radius']) t.add_row([i['particle_identifier'],\ i['particle_position_x']/units.kpc.to('cm'), i['particle_position_y']/units.kpc.to('cm'), i['particle_position_z']/units.kpc.to('cm'), i['particle_mass']/units.M_sun.to('g'), i['virial_radius']/units.kpc.to('cm')]) return t[2:], hc
def find_halos(self): final_output = os.popen("tail -1 {}/OutputLog".format( self.test_dir)).read() fname = final_output.split(" ")[2] ds = yt.load(os.path.join(self.test_dir, fname)) hc = HaloCatalog(data_ds=ds, finder_method='hop') hc.create() hc.load("halo_catalogs/catalog/catalog.0.h5") f, ax = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True) halo_positions = [] halo_mass = [] dw = ds.domain_width def get_relative_position(halo_dict, dw): k = "particle_position_{}" relpos = [] for orient in ["x", "y", "z"]: key = k.format(orient) relpos.append(halo_dict[key] / dw[0]) return relpos for h in hc.catalog: halo_positions.append(get_relative_position(h, dw)) halo_mass.append(h["particle_mass"].in_units("Msun").v) halo_positions = np.array(halo_positions).transpose() if (len(halo_positions) < 1): return False ax[0].scatter(halo_positions[0], halo_positions[1], s=np.log10(halo_mass) * 10) ax[1].scatter(halo_positions[0], halo_positions[2], s=np.log10(halo_mass) * 10) ax[2].scatter(halo_positions[1], halo_positions[2], s=np.log10(halo_mass) * 10) ax[0].set_xlim(0, 1) ax[0].set_ylim(0, 1) f.savefig("halo_position.png") np.save(self.test_dir + "_halo_positions.npy", halo_positions) np.save(self.test_dir + "_halo_mass.npy", halo_mass) self.halo_positions = halo_positions self.halo_mass = halo_mass return True
def test_datacontainer_data(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) ds = data_dir_load(enzotiny) ds.add_particle_filter("dm") for method in ["fof", "hop"]: hc = HaloCatalog(data_ds=ds, finder_method=method, output_dir="hc1", finder_kwargs={"dm_only": True}) hc.create() hc = HaloCatalog(data_ds=ds, finder_method=method, output_dir="hc2", finder_kwargs={"dm_only": False, "ptype": "dm"}) hc.create() ds1 = load("hc1/hc1.0.h5") ds2 = load("hc2/hc2.0.h5") assert_array_equal(ds1.r["particle_mass"], ds2.r["particle_mass"]) os.chdir(curdir) shutil.rmtree(tmpdir)
method = sys.argv[1] comm = MPI.Comm.Get_parent() methods = { "fof": {}, "hop": {}, "rockstar": { "num_readers": 1, "num_writers": 1, "particle_type": "dark_matter" } } @particle_filter("dark_matter", requires=["creation_time"]) def _dm_filter(pfilter, data): return data["creation_time"] <= 0.0 ds = yt.load("Enzo_64/DD0043/data0043") ds.add_particle_filter("dark_matter") output_dir = os.path.join(os.path.dirname(__file__), "halo_catalogs", method) hc = HaloCatalog(data_ds=ds, output_dir=output_dir, finder_method=method, finder_kwargs=methods[method]) hc.create() comm.Disconnect()
import argparse parser = argparse.ArgumentParser() parser.add_argument("directory", type=str, help="List one or more directories to analyze.") args = parser.parse_args() directory = args.directory import yt from yt.analysis_modules.halo_analysis.api import HaloCatalog from yt.analysis_modules.halo_analysis.halo_callbacks import add_callback ds = yt.load(directory + '/' + directory) halos_ds = yt.load('halo_catalogs/catalog/catalog.0.h5') hc = HaloCatalog(data_ds=ds, halos_ds=halos_ds) hc.load() def count_particles(halo): """ Creates a 30kpc sphere around a center of mass and counts the number of contained particles. """ x = halo.quantities['particle_position_x'] y = halo.quantities['particle_position_y'] z = halo.quantities['particle_position_z'] sphere = halo.halo_catalog.data_ds.sphere([x, y, z], (30, 'kpc')) n_particles = len(sphere['x']) halo.particle_count = n_particles
'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 yt from yt.analysis_modules.halo_analysis.api import HaloCatalog # Load the data set with the full simulation information # and rockstar halos data_ds = yt.load('Enzo_64/RD0006/RedshiftOutput0006') halos_ds = yt.load('rockstar_halos/halos_0.0.bin') # Instantiate a catalog using those two paramter files hc = HaloCatalog(data_ds=data_ds, halos_ds=halos_ds) # Filter out less massive halos hc.add_filter("quantity_value", "particle_mass", ">", 1e14, "Msun") # This recipe creates a spherical data container, computes # radial profiles, and calculates r_200 and M_200. hc.add_recipe("calculate_virial_quantities", ["radius", "matter_mass"]) # Create a sphere container with radius 5x r_200. field_params = dict(virial_radius=('quantity', 'radius_200')) hc.add_callback('sphere', radius_field='radius_200', factor=5, field_parameters=field_params) # Compute profiles of T vs. r/r_200 hc.add_callback('profile', ['virial_radius_fraction'], [('gas', 'temperature')], storage='virial_profiles', weight_field='cell_mass', accumulation=False, output_dir='profiles') # Save the profiles
import yt from yt.analysis_modules.halo_analysis.api import HaloCatalog # Load the data set with the full simulation information # and rockstar halos data_ds = yt.load('Enzo_64/RD0006/RedshiftOutput0006') halos_ds = yt.load('rockstar_halos/halos_0.0.bin') # Instantiate a catalog using those two paramter files hc = HaloCatalog(data_ds=data_ds, halos_ds=halos_ds) # Filter out less massive halos hc.add_filter("quantity_value", "particle_mass", ">", 1e14, "Msun") # attach a sphere object to each halo whose radius extends # to twice the radius of the halo hc.add_callback("sphere", factor=2.0) # use the sphere to calculate radial profiles of gas density # weighted by cell volume in terms of the virial radius hc.add_callback("profile", x_field="radius", y_fields=[("gas", "overdensity")], weight_field="cell_volume", accumulation=False, storage="virial_quantities_profiles") hc.add_callback("virial_quantities", ["radius"], profile_storage="virial_quantities_profiles") hc.add_callback('delete_attribute', 'virial_quantities_profiles')
# Define custom quantity functions here. # counthaloparticles.py RD0006 import argparse parser = argparse.ArgumentParser() parser.add_argument("directory", type=str, help="List one or more directories to analyze.") args = parser.parse_args() directory = args.directory import yt from yt.analysis_modules.halo_analysis.api import HaloCatalog from yt.analysis_modules.halo_analysis.halo_callbacks import add_callback ds = yt.load(directory+'/'+directory) halos_ds = yt.load('halo_catalogs/catalog/catalog.0.h5') hc = HaloCatalog(data_ds=ds, halos_ds=halos_ds) hc.load() def count_particles(halo): """ Creates a 30kpc sphere around a center of mass and counts the number of contained particles. """ x = halo.quantities['particle_position_x'] y = halo.quantities['particle_position_y'] z = halo.quantities['particle_position_z'] sphere = halo.halo_catalog.data_ds.sphere([x, y, z], (30, 'kpc')) n_particles = len(sphere['x']) halo.particle_count = n_particles print halo.particle_count
import sys import yt from yt.analysis_modules.halo_analysis.api import \ HaloCatalog from yt.data_objects.particle_filters import \ particle_filter yt.enable_parallelism() method = sys.argv[1] comm = MPI.Comm.Get_parent() methods = {"fof": {}, "hop": {}, "rockstar": {"num_readers":1, "num_writers":1, "particle_type":"dark_matter"}} @particle_filter("dark_matter", requires=["creation_time"]) def _dm_filter(pfilter, data): return data["creation_time"] <= 0.0 ds = yt.load("Enzo_64/DD0043/data0043") ds.add_particle_filter("dark_matter") output_dir = os.path.join(os.path.dirname(__file__), "halo_catalogs", method) hc = HaloCatalog(data_ds=ds, output_dir=output_dir, finder_method=method, finder_kwargs=methods[method]) hc.create() comm.Disconnect()
path_man = path_manager(args.root_data_dir, args.root_output_dir, sim_num=args.sim_number, snap_name=args.time_slice, data_dir_prefix=args.data_prefix) if not dataset_finished(path_man.get_exp_path()): print("Sim number %s is not yet finished. Skipping." % args.sim_number) sys.exit(0) path_man.ensure_directories() catalog_ds = yt.load(path_man.get_rockstar_catalog_first_file()) ds = yt.load(path_man.get_dataset_path()) hc = HaloCatalog(halos_ds=catalog_ds, output_dir=path_man.get_rockstar_catalogue_dirname()) for axis in "xyz": p = yt.ProjectionPlot(ds, axis, "density", center=([0, 0, 0], "Mpc"), fontsize=24) #p = yt.ProjectionPlot(ds, axis, "density") p.set_buff_size(2400) p.set_zlim('density', 1e-6, 5e-2) #p.annotate_halos(hc, annotate_field = 'particle_identifier', radius_field = 'radius_200', center_field_prefix= "halo_position") p.annotate_halos(hc, annotate_field='particle_identifier') p.save(path_man.get_rockstar_catalogue_dirname() + "/projection_%s_orig.png" % axis)
path_man = path_manager(args.root_data_dir, args.root_output_dir, sim_num=args.sim_number, snap_name=args.time_slice, data_dir_prefix=args.data_prefix) if not dataset_finished(path_man.get_exp_path()): print("Sim number %s is not yet finished. Skipping." % args.sim_number) sys.exit(0) if yt.is_root(): path_man.ensure_directories() ds = yt.load(path_man.get_dataset_path()) halos_ds = yt.load(path_man.get_rockstar_halo_dirname() + "/halos_0.0.bin") hc = HaloCatalog(data_ds=ds, halos_ds=halos_ds, output_dir=path_man.get_rockstar_catalogue_dirname()) hc.add_callback('save_quantities', prefix='orig', fields=[ 'virial_radius', 'particle_position_x', 'particle_position_y', 'particle_position_z', 'particle_mass' ]) hc.add_callback('get_additional_halo_properties') # hc.add_callback('recenter_halo', radius_field='virial_radius', units='pc') hc.add_recipe('calculate_virial_quantities', ['radius', 'matter_mass']) hc.create()
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']) directory = 'RD0009' 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.load() halos = hc.halo_list fieldname = 'gas' fieldvalue = fields[fieldname] index = 0 halo = halos[index] # PROJECTION PLOT com = [halo.quantities['particle_position_x'], halo.quantities['particle_position_y'], halo.quantities['particle_position_z']] sp = ds.sphere(com, (30, 'kpc')) amv = sp.quantities.angular_momentum_vector() amv = amv / np.sqrt((amv**2).sum()) center = sp.quantities.center_of_mass() res = 1024
import numpy as np import yt from yt.analysis_modules.halo_analysis.api import HaloCatalog import pynbody data_ds = yt.load('/mnt/is2/dpaz/ITV/S1373/out/snapshot_050') hc = HaloCatalog(data_ds=data_ds, finder_method='fof', finder_kwargs={ "ptype": "stars", "padding": 0.02 }) hc.create()
import yt from yt.analysis_modules.halo_analysis.api import HaloCatalog # Load the data set with the full simulation information # and rockstar halos data_ds = yt.load('Enzo_64/RD0006/RedshiftOutput0006') halos_ds = yt.load('rockstar_halos/halos_0.0.bin') # Instantiate a catalog using those two paramter files hc = HaloCatalog(data_ds=data_ds, halos_ds=halos_ds) # Filter out less massive halos hc.add_filter("quantity_value", "particle_mass", ">", 1e14, "Msun") # attach a sphere object to each halo whose radius extends # to twice the radius of the halo hc.add_callback("sphere", factor=2.0) # use the sphere to calculate radial profiles of gas density # weighted by cell volume in terms of the virial radius 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('delete_attribute', 'virial_quantities_profiles')
function=stars, filtered_type='all', requires=['particle_type']) yt.add_particle_filter('dark_matter', function=dark_matter, filtered_type='all', requires=['particle_type']) directory = 'RD0009' 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.load() halos = hc.halo_list fieldname = 'gas' fieldvalue = fields[fieldname] index = 0 halo = halos[index] # PROJECTION PLOT com = [ halo.quantities['particle_position_x'], halo.quantities['particle_position_y'], halo.quantities['particle_position_z'] ] sp = ds.sphere(com, (30, 'kpc'))
# findhalos.py # Find halos for a given enzo directory. # findhalos.py RD0006 import argparse parser = argparse.ArgumentParser() parser.add_argument("directory", type=str, help="List one or more directories to analyze.") args = parser.parse_args() directory = args.directory import yt from yt.analysis_modules.halo_analysis.api import HaloCatalog from yt.analysis_modules.halo_analysis.halo_filters import add_filter ds = yt.load(directory+'/'+directory) hc = HaloCatalog(data_ds=ds, finder_method='hop') # Define custom filters. def filter_position(halo): """ Checks to see if the particle position is within the defined boundaries. """ lower_bound = 0.375 upper_bound = 0.625 x = halo.quantities['particle_position_x'] y = halo.quantities['particle_position_y'] z = halo.quantities['particle_position_z'] return (lower_bound < x < upper_bound) and (lower_bound < y < upper_bound) and (lower_bound < z < upper_bound) def filter_particles(halo):
def createHaloCatalogueFromSnap(snap): "creates Halo Catalogue from a given snap" data_ds = yt.load(snap) hc = HaloCatalog(data_ds=data_ds, finder_method='hop') hc.create()