def setup(): testing.init_blank_db_for_testing() creator = tangos.testing.simulation_generator.TestSimulationGenerator() halo_offset = 0 for ts in range(1, 4): creator.add_timestep() creator.add_objects_to_timestep(4) creator.link_last_halos() tangos.get_default_session().commit() tangos.get_item("sim/ts1/halo_1")['test_image'] = np.zeros((500, 500, 3)) for ts in tangos.get_simulation(1).timesteps: for h in ts.halos: h['test_value'] = 1.0 tangos.get_default_session().commit() creator = tangos.testing.simulation_generator.TestSimulationGenerator( "simname/has/slashes") creator.add_timestep() creator.add_objects_to_timestep(1) creator.add_timestep() creator.add_objects_to_timestep(1) creator.link_last_halos() tangos.get_simulation(2).timesteps[0].halos[0]['test_value'] = 2.0 tangos.get_default_session().commit() global app app = TestApp(tangos.web.main({}))
def test_readd_simulation(): manager = add_simulation.SimulationAdderUpdater(output_testing.TestInputHandler("dummy_sim_1")) with log.LogCapturer(): manager.scan_simulation_and_add_all_descendants() assert db.core.get_default_session().query(db.core.Simulation).count()==1 assert len(db.get_simulation("dummy_sim_1").timesteps)==2 assert db.get_simulation("dummy_sim_1").properties.count()==3
def test_custom_delta_t(): try: db.get_simulation("sim")["histogram_delta_t_Gyr"] = 0.01 reconstructed_lc = db.get_halo("sim/ts2/1").calculate("reassemble(dummy_histogram, 'place')") assert len(reconstructed_lc) == int(db.get_timestep("sim/ts2").time_gyr/0.01) finally: # restore to default db.get_simulation("sim")["histogram_delta_t_Gyr"] = DummyHistogramProperty.pixel_delta_t_Gyr
def Tangos_load(sim, redshift): global path print('Loading %s at z=%.2f' % (sim, redshift)) tangos.core.init_db('{}/{}.db'.format(path, sim.split('_')[0])) session = tangos.core.get_default_session() # Find the output closest to this redshift: times = tangos.get_simulation('%s' % sim, session).timesteps final_time = times[-1].__dict__['extension'] h = tangos.get_halo('{}/{}/halo_{:d}'.format(sim, final_time, 1), session) halo, z = h.calculate_for_progenitors('halo_number()', 'z()') z_ = np.array([times[i].__dict__['redshift'] for i in range(len(times))])[-len(halo):] output = np.array([times[i].__dict__['extension'] for i in range(len(times))])[-len(halo):] output = output[abs(z_ - redshift).argmin()] redshift = z_[abs(z_ - redshift).argmin()] # Find the progenitor halo number closest to this redshift: halo = halo[abs(z - redshift).argmin()] h = tangos.get_halo('{}/{}/halo_{:d}'.format(sim, output, halo)) return h, session, output, halo, redshift
def find_halos_with_stars(timestep, output): halos = tangos.get_simulation("h1.cosmo50").timesteps[timestep].halos #halos = tangos.get_simulation("cosmo25").timesteps[110].halos halo_id = [] Rvir = [] dist_to_cluster = [] Mvir = [] Mstar = [] Mgas = [] MColdGas = [] MHIGas = [] Rvir = [] R200 = [] center = [] Contam = [] cluster_center = halos[0]['shrink_center'] for i in range(len(halos[:1000])): halo = halos[i] mstar = halo['Mstar'] mgas = halo['Mgas'] if i % 50 == 0: print(i) if mstar > 1e8: print(i, mstar / 1e12) halo_id.append(i) Contam.append(halo['contamination_fraction']) Rvir.append(halo['max_radius']) r200 = halo.calculate('radius(200)') R200.append(r200) center.append(halo['shrink_center']) dist_to_cluster.append( np.linalg.norm( np.subtract(halo['shrink_center'], cluster_center))) Mvir.append(halo['Mvir']) Mstar.append(mstar) Mgas.append(mgas) MColdGas.append(halo['MColdGas']) MHIGas.append(halo['MHIGas']) h5file = h5.File('/nobackup/ibutsky/data/romulusC_halo_data_%i' % (output), 'w') h5file.create_dataset('contamination', data=np.array(Contam)) h5file.create_dataset('r200', data=np.array(R200)) h5file.create_dataset('halo_id', data=np.array(halo_id)) h5file.create_dataset('rvir', data=np.array(Rvir)) h5file.create_dataset('mvir', data=np.array(Mvir)) h5file.create_dataset('mstar', data=np.array(Mstar)) h5file.create_dataset('mgas', data=np.array(Mgas)) h5file.create_dataset('mcoldgas', data=np.array(MColdGas)) h5file.create_dataset('mhigas', data=np.array(MHIGas)) h5file.create_dataset('center', data=np.array(center)) h5file.create_dataset('dist_to_cluster', data=np.array(dist_to_cluster)) h5file.flush()
def setup(): testing.init_blank_db_for_testing() creator = tangos.testing.simulation_generator.TestSimulationGenerator() halo_offset = 0 for ts in range(1,4): creator.add_timestep() creator.add_objects_to_timestep(4) creator.link_last_halos() tangos.get_default_session().commit() tangos.get_item("sim/ts1/halo_1")['test_image'] = np.zeros((500,500,3)) for ts in tangos.get_simulation(1).timesteps: for h in ts.halos: h['test_value'] = 1.0 tangos.get_default_session().commit() creator.add_timestep() # add ts 4 creator.add_objects_to_timestep(3) tangos.get_default_session().commit() # now make the object have halo numbers which have a different ordering to the database IDs tangos.get_item("sim/ts4/1")['test_value'] = 1.0 tangos.get_item("sim/ts4/2")['test_value'] = 2.0 tangos.get_item("sim/ts4/3")['test_value'] = 3.0 tangos.get_item("sim/ts4/1").halo_number = 10 tangos.get_default_session().commit() creator = tangos.testing.simulation_generator.TestSimulationGenerator("simname/has/slashes") creator.add_timestep() creator.add_objects_to_timestep(1) creator.add_timestep() creator.add_objects_to_timestep(1) creator.link_last_halos() tangos.get_simulation(2).timesteps[0].halos[0]['test_value'] = 2.0 tangos.get_default_session().commit() global app app = TestApp(tangos.web.main({}))
def add_test_simulation_to_db(): global _added_to_db if not _added_to_db: with log.LogCapturer(): add.SimulationAdderUpdater( output_manager).scan_simulation_and_add_all_descendants() tx = db.core.tracking.TrackData(db.get_simulation("test_tipsy")) tx.particles = tracked_particles tx.use_iord = False tx = db.core.get_default_session().merge(tx) tx.create_objects() tx = db.core.tracking.TrackData(db.get_simulation("test_tipsy")) tx.particles = tracked_iord tx.use_iord = True tx = db.core.get_default_session().merge(tx) tx.create_objects() _added_to_db = True
def __init__(self, sim_name, center_halo, latest_timestep, earliest_timestep, region_size, box_size=25e3, periodic=True): import numpy as np import tangos from scipy.spatial import KDTree self.simulation = tangos.get_simulation(str(sim_name)) self.region_size = 5*float(region_size) self.box_size = float(box_size) self.timestep, self.num_timesteps = self.find_timesteps(int(latest_timestep), int(earliest_timestep)) self.center_halo = self.timestep[center_halo] self.relevant_halos = self.gather_relevant_halos()
def _setup_dummy_histogram_data(ts1, ts2): global test_histogram test_histogram = np.arange(0.0,1000.0,1.0) ts1_h1 = db.get_halo("sim/ts1/1") ts1_h2 = db.get_halo("sim/ts1/2") ts2_h1 = db.get_halo("sim/ts2/1") property = DummyHistogramProperty(db.get_simulation("sim")) ts1_h1['dummy_histogram'] = test_histogram[property.store_slice(ts1.time_gyr)] ts1_h2['dummy_histogram'] = test_histogram[property.store_slice(ts1.time_gyr)] * 0.5 ts2_h1['dummy_histogram'] = test_histogram[property.store_slice(ts2.time_gyr)] db.core.get_default_session().commit()
def update_tracker_halos(sim=None): from tangos import get_simulation from tangos.core import get_default_session from tangos.util.terminalcontroller import heading if sim is None: x = get_default_session().query(TrackData).all() else: x = get_simulation(sim).trackers.all() for y in x: heading(repr(y)) y.create_objects()
def test_update_simulation(): with testing.autorevert(): assert db.get_simulation("dummy_sim_1")['dummy_sim_property_2'] == 'banana' _perform_simulation_update() assert db.get_simulation("dummy_sim_1").properties.count() == 4 assert db.get_simulation("dummy_sim_1")['dummy_sim_property_2']=='orange' assert db.get_simulation("dummy_sim_1")['dummy_sim_property_new'] == 'fruits' assert len(db.get_simulation("dummy_sim_1").timesteps) == 3 assert db.get_timestep("dummy_sim_1/step.3").halos.count()==7 assert db.get_simulation("dummy_sim_1")['dummy_sim_property_2'] == 'banana'
def test_histogram_written_as_expected(): dumhistprop = DummyHistogramProperty(db.get_simulation("sim")) ts1 = db.get_timestep("sim/ts1") ts2 = db.get_timestep("sim/ts2") ts1_h1 = db.get_halo("sim/ts1/1") ts1_h2 = db.get_halo("sim/ts1/2") ts2_h1 = db.get_halo("sim/ts2/1") npt.assert_almost_equal( ts1_h1.calculate('raw(dummy_histogram)'), test_histogram[dumhistprop.store_slice(ts1.time_gyr)]) npt.assert_almost_equal( ts1_h2.calculate('raw(dummy_histogram)'), test_histogram[dumhistprop.store_slice(ts1.time_gyr)] * 0.5) npt.assert_almost_equal( ts2_h1.calculate('raw(dummy_histogram)'), test_histogram[dumhistprop.store_slice(ts2.time_gyr)])
def test_handler(): assert isinstance( db.get_simulation("test_tipsy_yt").get_output_handler(), yt_outputs.YtChangaAHFInputHandler)
def wrap(relpos,scale=1,boxsize=25e3): #author: Michael Tremmel #scale = 1/1+z bphys = boxsize*scale bad = np.where(np.abs(relpos) > bphys/2.) if type(bphys) == np.ndarray: relpos[bad] = -1.0 * (relpos[bad] / np.abs(relpos[bad])) * np.abs(bphys[bad] - np.abs(relpos[bad])) else: relpos[bad] = -1.0 * (relpos[bad]/np.abs(relpos[bad])) * np.abs(bphys - np.abs(relpos[bad])) return #Load in Romulus25 and the desired halo properties print('Loading Database...') os.environ['TANGOS_DB_CONNECTION'] = '/myhome2/users/munshi/Romulus/data_romulus25.working.db' import tangos rom = tangos.get_simulation('cosmo25') hnum, cen, mvir, rvir, vmag, rmag, kmag, bmag, mstar, csfh, sfr = rom[-1].calculate_all( 'halo_number()','shrink_center','Mvir','max_radius', 'AB_V','AB_R','AB_K','AB_B','Mstar','CumSFH','SFR_encl_250Myr') myprint('Database Loaded',clear=True) #Initialize Data output Dictionaries MilkyWays = {} Satellites = {} LargeHalos = {} #Find Large Halos (Mvir > 5e11) print('Searching for Large Halos...') for i in np.arange(len(hnum)): if mvir[i] > 5e11: LargeHalos[str(hnum[i])] = {}
import numpy as np import pynbody, tangos, glob, gc sim = tangos.get_simulation('h1.cosmo50') steps = sim.timesteps stepnum = -1 halonum = 0 #generalize these later halo = steps[stepnum].halos[halonum] steps = [0, 8, 9, 10, 11, 12, 16, 17, 18, 25, 26, 27, 37, 38, 39] ts = halo.calculate_for_progenitors('t()')[0] times = np.array([ts[i] for i in steps]) def vdisp(profile): return np.sqrt(profile['vx_disp']**2 + profile['vy_disp']**2 + profile['vz_disp']**2) def get_profile(snap, weight='mass', rmin=5, rmax=2e3, tmax=None, tmin=None): if tmin: if tmax: ptcls = snap.g[(pynbody.filt.HighPass('temp', tmin)) & (pynbody.filt.LowPass('temp', tmax))] elif tmax: ptcls = snap.g[pynbody.filt.LowPass('temp', tmax)] else: ptcls = snap.g return pynbody.analysis.profile.Profile(ptcls, min=rmin, max=rmax,
import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import tangos as db import pynbody from matplotlib import rc import gc #Halo numbers #numbers = [18, 23, 24, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 186, 187, 188, 189, 190, 191, 192, 193, 194, 196, 197, 198, 199, 200, 201, 202, 204, 206, 207, 208, 209, 210, 213, 214, 215, 216] numbers = [38] #Simulation data sim = db.get_simulation('cosmo25') step1 = db.get_timestep('cosmo25%/%8192') s = pynbody.load('/oasis/scratch/comet/mjt29/temp_project/Romulus25/cosmo25p.768sg1bwK1BHe75.008192') s.physical_units() gal = s.halos(dosort=True) #Skirt data & comparison data data = np.genfromtxt('skirt_faceon.dat', names = ['halo', 'IRX', 'beta', 'neg_ebeta', 'pos_ebeta', 'intercept', 'neg_eint', 'pos_eint']) #data = data_angles[data_angles['angle'] == 90.] compare_data = np.genfromtxt('BF_IRXBdata_comparison.dat', names = ['IRX', 'e_IRX', 'beta', 'e_beta'], skip_header =11) #Fit from Forrest et al. B = np.linspace(-2.5, 2.5, 100) y = np.log10(10**(0.4*(5.05 + 2.39*B)) - 1) + np.log10(1.68) #Grabbing my variables halomvir = []
def test_simulation_properties(fresh_database): assert db.get_simulation("dummy_sim_1").properties.count() == 3 assert db.get_simulation("dummy_sim_1")['dummy_sim_property'] == '42'
import numpy as np from scipy.stats import binned_statistic import tangos as db import glob, gc, pynbody #load particles basename = '/nobackupp2/mtremmel/Romulus/' datadir = basename + 'h1.cosmo50/h1.cosmo50PLK.1536gst1bwK1BH.004096' # basename= '/gpfs/project/fas/nagai/etl28/Romulus/' # datadir = basename+'RomulusC_Snapshots/h1.cosmo50PLK.1536gst1bwK1BH.004096' sim = db.get_simulation('h1.cosmo50') steps = sim.timesteps halonum = 0 halo = steps[-1].halos[halonum] steptime = halo.reverse_property_cascade('t()')[0] keytimes = [9.2, 9.7, 11.3, 11.65, 11.97, 12.20, 12.50, 12.80, 13.25, 13.55] #Gyr snapnums = [ np.argmin(abs(np.array(steptime) - keytime)) for keytime in keytimes ] snapinds = [1, 3, 5, 7, 9] # rcore = 30 radius = str(rcore) + ' kpc' nbins = 25 allgas = True nosubs = True def filter(snap, halo_center=[0, 0, 0], radius='1 kpc'):
import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import tangos as db import pynbody from matplotlib import rc import gc #Halo numbers #numbers = [18, 23, 24, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 186, 187, 188, 189, 190, 191, 192, 193, 194, 196, 197, 198, 199, 200, 201, 202, 204, 206, 207, 208, 209, 210, 213, 214, 215, 216] numbers = [38] #Simulation data sim = db.get_simulation('cosmo25') step1 = db.get_timestep('cosmo25%/%8192') s = pynbody.load( '/oasis/scratch/comet/mjt29/temp_project/Romulus25/cosmo25p.768sg1bwK1BHe75.008192' ) s.physical_units() gal = s.halos(dosort=True) #Skirt data & comparison data data = np.genfromtxt('skirt_faceon.dat', names=[ 'halo', 'IRX', 'beta', 'neg_ebeta', 'pos_ebeta', 'intercept', 'neg_eint', 'pos_eint' ]) #data = data_angles[data_angles['angle'] == 90.] compare_data = np.genfromtxt('BF_IRXBdata_comparison.dat', names=['IRX', 'e_IRX', 'beta', 'e_beta'],
#this analysis is officially easier to run on Grace #because both the snapshots and database files now live here #and pynbody and tangos work import tangos as db import numpy as np import matplotlib.pylab as plt from matplotlib import cm import os from scipy.stats import binned_statistic from astropy import units, constants from tangos.examples import mergers # from astropy.stats import LombScargle omega_m = 0.3 simname='h1.cosmo50' sim = db.get_simulation(simname) steps = sim.timesteps #black holes count as halos #get local copies of h1.cosmo50 and the database to the office desktop haloind = 0 startstep = -1 h = steps[startstep].halos[0] zs = np.array(h.calculate_for_progenitors("z()")[0]) ts = np.array(h.calculate_for_progenitors("t()")[0]) #total mass is in Msol ind00 = np.argmin(abs(ts - 13.80)) #sim end ind01 = np.argmin(abs(ts - 12.50)) #disruption end ind02 = np.argmin(abs(ts - 12)) # disruption start ind03 = np.argmin(abs(ts - 11.1)) #1st pericenter passage done ind04 = np.argmin(abs(ts - 10.8)) #merger start ind05 = np.argmin(abs(ts - 9.7))#post quenching end
tracedvy = np.interp(tracedTime, np.flipud(time), np.flipud(vel[:, 1]), left=0) tracedvz = np.interp(tracedTime, np.flipud(time), np.flipud(vel[:, 2]), left=0) tracedVelocities = np.vstack((tracedvx, tracedvy, tracedvz)) #Combine everything into a dictionary if hasBH: historyBook = {"time": tracedTime, "haloNumber": np.array(haloNumber), "Mstar": tracedMstar, "SFR": combinedSFR, "Mvir": tracedMvir, \ "R200": tracedR200, "Mgas": tracedMgas, "Mcold": tracedMcold, "SSC": tracedCoordinates, "Vcom": tracedVelocities, "t_slice": time, \ "Mbh": tracedMbh, "BHAR": combinedBHAR, "Dbh": tracedDbh} else: historyBook = {"time": tracedTime, "haloNumber": np.array(haloNumber), "Mstar": tracedMstar, "SFR": combinedSFR, "Mvir": tracedMvir, \ "R200": tracedR200, "Mgas": tracedMgas, "Mcold": tracedMcold, "SSC": tracedCoordinates, "Vcom": tracedVelocities, "t_slice": time} return historyBook if __name__ == '__main__': simulationName = 'h1.cosmo50' sim = db.get_simulation(simulationName) #This is z=0.14 startingStep = 59 step = sim.timesteps[startingStep] book = makeHistory(step.halos[2])