def get_previous_run_params(params): # Set simulation directories params["prev_sim_dir"] = os.path.join( params["simulation_run_directory"], "%s-L%d" % (params["simulation_name"], params["level"] - 1)) params["sim_dir"] = os.path.join( params["simulation_run_directory"], "%s-L%d" % (params["simulation_name"], params["level"])) # # Obtain the maxlevel of the original run if params["original_config"] == None: original_config_file = "%s-L0.conf" % (params["simulation_name"]) else: original_config_file = params["original_config"] music_cf0 = cp.ConfigParser() music_cf0.read(original_config_file) params["initial_min_level"] = music_cf0.getint("setup", "levelmin") params["initial_max_level"] = music_cf0.getint("setup", "levelmax") # Obtain the shift of the Lagrangian region from the previous zoom-in # (or unigrid) simulation params["region_shift"] = [0, 0, 0] if params["original_config"] != None and params["level"] == 1: prev_config_logfile = "%s_log.txt" % (params["original_config"]) else: prev_config_logfile = "%s-L%d.conf_log.txt" % \ (params["simulation_name"], params["level"]-1) with open(prev_config_logfile) as fp: for l in fp.readlines(): if l.find("setup/shift_x") >= 0: params["region_shift"][0] = int(l.split("=")[1]) if l.find("setup/shift_y") >= 0: params["region_shift"][1] = int(l.split("=")[1]) if l.find("setup/shift_z") >= 0: params["region_shift"][2] = int(l.split("=")[1]) if l.find("setup/levelmin") >= 0: params["region_point_levelmin"] = int(l.split("=")[1]) # Rounding factor for the Lagrangian region if using a rectangular # prism. params["round_factor"] = 2**params["initial_max_level"] # # Get the inital dataset of the simulation and either # the final dataset or the dataset at the specified redshift. # sim_par_file = os.path.join( params["prev_sim_dir"], "%s-L%d.enzo" % (params["simulation_name"], params["level"] - 1)) es = yt.simulation(sim_par_file, "Enzo", find_outputs=True) params["enzo_initial_fn"] = es.all_outputs[0]["filename"] if "redshift" in params["halo_info"]: es.get_time_series(redshifts=[params["halo_info"]["redshift"]]) ds = es[0] params["enzo_final_fn"] = os.path.join(ds.fullpath, ds.basename) else: params["enzo_final_fn"] = es.all_outputs[-1]["filename"] return params
def find_halos(dsname, simfile=None, wdir='./', restart=True, *args, **kwargs): """ Find the halos in a dataset. For simplicity this ONLY does the halo finding. We do other computations elsewhere. """ if (os.path.isfile(wdir + 'rockstar_halos/ROCKSTARDONE')): print("ROCKSTARDONE file exists. Exiting") return if not (simfile is None): es = yt.simulation(simfile, "Enzo") es.get_time_series(initial_redshift=20.0) for ds in es: setup_ds(ds) else: es = yt.load(dsname + '/' + dsname) setup_ds(es) rhf = RockstarHaloFinder( es, #num_readers=1, num_writers=1, particle_type='max_res_dark_matter') rhf.run(restart=restart) f = open("ROCKSTARDONE", 'w') f.write("Completed running rockstar from find_halos.py") f.close() #halos = yt.load('rockstar_halos/halos_0.0.bin') #hc = HaloCatalog(halos_ds=halos, output_dir = widr + 'halo_catalogs/catalog_'+str(ds))) #hc.load() #del(hc) #del(ds) #del(data) return
import yt from yt import memory_checker import numpy as np import time # sim = yt.simulation("test_dir", "ExodusII") with memory_checker(1): time.sleep(2) sim = yt.simulation("test_dir", "ExodusII") sim.get_time_series() time.sleep(2)
import sys import yt def save_simulation(es, filename=None): def to_arr(my_list): if hasattr(my_list[0], "units"): f = es.arr else: f = np.array return f(my_list) fields = ["filename", "time"] ex_keys = ["box_size", "initial_time", "final_time"] if es.cosmological_simulation: fields.append("redshift") ex_keys.extend(["initial_redshift", "final_redshift"]) data = dict((field, to_arr([d[field] for d in es.all_outputs])) for field in fields) for i in range(data["filename"].size): if data["filename"][i].startswith("./"): data["filename"][i] = data["filename"][i][2:] if filename is None: filename = str(es) filename = filename[:filename.rfind(".")] + ".h5" extra_attrs = dict((field, getattr(es, field)) for field in ex_keys) yt.save_as_dataset(es, filename, data, extra_attrs=extra_attrs) if __name__ == "__main__": es = yt.simulation(sys.argv[1], "Enzo", find_outputs=True) save_simulation(es, filename=sys.argv[1])
import yt # Create a time-series object. sim = yt.simulation("enzo_tiny_cosmology/32Mpc_32.enzo", "Enzo") sim.get_time_series(redshifts=[5, 4, 3, 2, 1, 0]) # Lists to hold profiles, labels, and plot specifications. profiles = [] labels = [] plot_specs = [] # Loop over each dataset in the time-series. for ds in sim: # Create a data container to hold the whole dataset. ad = ds.all_data() # Create a 1d profile of density vs. temperature. profiles.append(yt.create_profile(ad, ["density"], fields=["temperature"])) # Add labels and linestyles. labels.append("z = %.2f" % ds.current_redshift) plot_specs.append(dict(linewidth=2, alpha=0.7)) # Create the profile plot from the list of profiles. plot = yt.ProfilePlot.from_profiles(profiles, labels=labels, plot_specs=plot_specs) # Save the image. plot.save()
import yt es = yt.simulation('TestStarParticleSingle.enzo', 'Enzo') es.get_time_series() for ds in es: yt.ProjectionPlot(ds, 'x', 'density').save() yt.ProjectionPlot(ds, 'x', 'temperature', weight_field='density').save()
from mpi4py import MPI import yt from yt.analysis_modules.halo_finding.rockstar.api import \ RockstarHaloFinder from yt.data_objects.particle_filters import \ particle_filter yt.enable_parallelism() comm = MPI.Comm.Get_parent() @particle_filter("dark_matter", requires=["creation_time"]) def _dm_filter(pfilter, data): return data["creation_time"] <= 0.0 def setup_ds(ds): ds.add_particle_filter("dark_matter") es = yt.simulation("Enzo_64/64.param", "Enzo") es.get_time_series(setup_function=setup_ds, redshifts=[1., 0.]) rh = RockstarHaloFinder(es, num_readers=1, num_writers=1, particle_type="dark_matter") rh.run() comm.Disconnect()
from mpi4py import MPI import yt from yt_astro_analysis.halo_finding.rockstar.api import \ RockstarHaloFinder from yt.data_objects.particle_filters import \ particle_filter yt.enable_parallelism() comm = MPI.Comm.Get_parent() @particle_filter("dark_matter", requires=["creation_time"]) def _dm_filter(pfilter, data): return data["creation_time"] <= 0.0 def setup_ds(ds): ds.add_particle_filter("dark_matter") es = yt.simulation("Enzo_64/64.param", "Enzo") es.get_time_series(setup_function=setup_ds, redshifts=[1., 0.]) rh = RockstarHaloFinder(es, num_readers=1, num_writers=1, particle_type="dark_matter") rh.run() comm.Disconnect()
import yt import numpy as np import matplotlib.pyplot as plt # load up exodus files # NOTE: as of 3/4/2017 yt does not close files so you can hit a file limit sim = yt.simulation('exodus', 'ExodusII') sim.get_time_series() # initialize some things step_counter = 0 step_map = {} simulation_min=1e99 simulation_max=-1e99 # loop over all files and look for min/max values, then map step number to file and step within file for ds in sim: reg = ds.r['u'] plot_max = float(reg.max()) plot_min = float(reg.min()) print 'filename:',ds.index_filename,'step:',ds.step,'time:',ds.current_time.value[()],'steps:',ds.num_steps,'max value:',plot_max,'min value:',plot_min step_map.update({step_counter : (ds.index_filename, ds.step)}) step_counter = step_counter + 1 if plot_max > simulation_max: simulation_max = plot_max if plot_min < simulation_min: simulation_min = plot_min # total number of steps num_steps = step_counter - 1