def test_paramfile(): #Create an empty gadget snapshot snap = Gadget2Snapshot() #Generate random positions and velocities NumPart = 32**3 x = np.random.normal(loc=7.0,scale=5.0,size=(NumPart,3)) * Mpc v = np.zeros((NumPart,3)) * m / s #Put the particles in the snapshot snap.setPositions(x) snap.setVelocities(v) #Generate minimal header snap.setHeaderInfo() #Split the particles between two files snap.write("gadget_sphere",files=2) #Generate the parameter file that will determine the evolution snap.writeParameterFile("gadget_sphere.param") #Generate a file with the scale factor of the output snapshots z = np.arange(90.0,0.0,-10.0) a = 1.0 / (1 + z) np.savetxt("outputs.txt",a)
def ihalo_ID_position_fcn(snap_fn): '''snap_fn is the gadget snapshot file''' print 'GADGET', snap_fn snaps_gadget = Gadget2Snapshot.open(snap_fn) ID_gadget = snaps_gadget.getID() idx = where(in1d (ID_gadget, ID_amiga, assume_unique=1) == True)[0] ID_HaloParticles = ID_gadget[idx] Positions_HaloParticles = snaps_gadget.getPositions()[idx] return ID_HaloParticles, Positions_HaloParticles
def halo_particles(IDsnap_id): ''' input: ID = e.g. 'Om0.300_Ol0.700|512b240|ic1' snap_id = N, where N runs from 0 to total number of snapshots i = n, where n is one of the 16 split files for each snapshots ''' ID, snap_id = IDsnap_id cosmo_id,geometry_id, ic_id = ID.split("|") new_snap_fn = os.path.join(storage, cosmo_id, geometry_id, ic_id, 'snapshots_amiga/snapshot_%03d'%(snap_id)) print new_snap_fn if len(glob.glob(new_snap_fn+'.*'))<16: print 'file doesnt exist:', new_snap_fn #### file names for gadget snap and AHF particles amiga_dir = os.path.join(storage, cosmo_id, geometry_id, ic_id, 'amiga') os.system('mkdir -p %s'%(os.path.join(storage, cosmo_id, geometry_id, ic_id, 'snapshots_amiga'))) halo_fn_arr = glob.glob(amiga_dir+'/snap%i.*.z*.AHF_particle*'%(snap_id)) snap_fn_arr = glob.glob(os.path.join(storage, cosmo_id, geometry_id, ic_id, 'snapshots_gadget/snapshot_%03d.*'%(snap_id))) ##### test on laptop ##### #halo_fn_arr = glob.glob("*particles") #snap_fn_arr = glob.glob('snapshot_060.*') ##### read all the amiga particle files ######### txt_amiga = concatenate(array(map(genamigatxt, halo_fn_arr)), axis = 0).T ID_amiga = txt_amiga[0][txt_amiga[1]==1] ##### find all the halo particles in gadget ######## def ihalo_ID_position_fcn(snap_fn): '''snap_fn is the gadget snapshot file''' print 'GADGET', snap_fn snaps_gadget = Gadget2Snapshot.open(snap_fn) ID_gadget = snaps_gadget.getID() idx = where(in1d (ID_gadget, ID_amiga, assume_unique=1) == True)[0] ID_HaloParticles = ID_gadget[idx] Positions_HaloParticles = snaps_gadget.getPositions()[idx] return ID_HaloParticles, Positions_HaloParticles halo_ID_position = map(ihalo_ID_position_fcn, snap_fn_arr) halo_ID = concatenate([halo_ID_position[i][0] for i in range(len(halo_ID_position))]) halo_position = concatenate([halo_ID_position[i][1] for i in range(len(halo_ID_position))], axis=0) ###### create new gadget snapshot ############ halo_snap = Gadget2Snapshot() hg = Gadget2Snapshot.open(snap_fn_arr[0]).header #header_gadget halo_snap.setPositions(array(halo_position)*halo_ID_position[0][1].unit) halo_snap.setHeaderInfo(Om0=hg['Om0'], Ode0=hg['Ode0'], w0=hg['w0'], wa=hg['wa'], h=hg['h'], redshift=hg['redshift'], box_size=hg['box_size']) ###### write the new snapshot to file ########## halo_snap.write(new_snap_fn, files = len(snap_fn_arr)) print "WROTE", new_snap_fn
def reorganize_snaps (IDsnap_id): ID, snap_id = IDsnap_id cosmo_id,geometry_id, ic_id = ID.split("|") new_snap_fn = os.path.join(storage, cosmo_id, geometry_id, ic_id, 'snapshots_amiga/snapshot_%03d'%(snap_id)) os.system('mkdir -p %s'%(os.path.join(storage, cosmo_id, geometry_id, ic_id, 'snapshots_amiga'))) amiga8_arr = glob.glob(os.path.join(storage, cosmo_id, geometry_id, ic_id, 'snapshots_amiga8/snapshot_%03d.*'%(snap_id))) halo_position = concatenate(array(map(snapposition, amiga8_arr)),axis=0) halo_snap = Gadget2Snapshot() hg = Gadget2Snapshot.open(amiga8_arr[0]).header halo_snap.setPositions(array(halo_position)*Mpc) halo_snap.setHeaderInfo(Om0=hg['Om0'], Ode0=hg['Ode0'], w0=hg['w0'], wa=hg['wa'], h=hg['h'], redshift=hg['redshift'], box_size=hg['box_size']) halo_snap.write(new_snap_fn, files = 16)
def test_read(): #Open the gadget snapshot snapshot = Gadget2Snapshot.open("Data/gadget/snapshot_001") #Get the particles positions and play with the indices pospart = snapshot.getPositions(first=500,last=1000) pos = snapshot.getPositions() assert np.all(pospart==pos[500:1000]) #Visualize the snapshot snapshot.visualize(s=1) #Save the visualization snapshot.ax.set_title(r"${0}^3$ particles".format(snapshot.header["num_particles_total_side"])) snapshot.savefig("gadget_snapshot.png") #Close the snapshot snapshot.close()
def test_write(): #Create an empty gadget snapshot snap = Gadget2Snapshot() #Generate random positions and velocities NumPart = 32**3 x = np.random.normal(loc=7.0,scale=5.0,size=(NumPart,3)) * Mpc v = np.random.uniform(-1,1,size=(NumPart,3)) * m / s #Put the particles in the snapshot snap.setPositions(x) snap.setVelocities(v) #Generate minimal header snap.setHeaderInfo() #Visualize snap.visualize(s=1) snap.savefig("gadget_initial_condition.png") #Write the snapshot snap.write("gadget_ic")
from scipy.ndimage import filters from mayavi import mlab # Initialize MPIWhirlPool try: pool = MPIWhirlPool() except: pool = None if pool is None: print("Processing ICs in series...") # If we run on one core only the snapshots need to be processed in series snap = Gadget2Snapshot.open("Data/gadget/ic1.0") n1, r = snap.numberDensity(resolution=64, left_corner=np.array([0.0, 0.0, 0.0]) * snap.Mpc_over_h) snap.close() snap = Gadget2Snapshot.open("Data/gadget/ic1.1") n2, r = snap.numberDensity(resolution=64, left_corner=np.array([0.0, 0.0, 0.0]) * snap.Mpc_over_h) snap.close() n = n1 + n2 ns = filters.gaussian_filter(n, 2) scene = mlab.pipeline.volume(mlab.pipeline.scalar_field(ns)) else: if pool.is_master(): print("Processing ICs in parallel on {0} cores".format(pool.size + 1))
from scipy.ndimage import filters from mayavi import mlab #Initialize MPIWhirlPool try: pool = MPIWhirlPool() except: pool = None if pool is None: print("Processing ICs in series...") #If we run on one core only the snapshots need to be processed in series snap = Gadget2Snapshot.open("Data/gadget/ic1.0") n1,r = snap.numberDensity(resolution=64,left_corner=np.array([0.0,0.0,0.0])*snap.Mpc_over_h) snap.close() snap = Gadget2Snapshot.open("Data/gadget/ic1.1") n2,r = snap.numberDensity(resolution=64,left_corner=np.array([0.0,0.0,0.0])*snap.Mpc_over_h) snap.close() n = n1+n2 ns = filters.gaussian_filter(n,2) scene = mlab.pipeline.volume(mlab.pipeline.scalar_field(ns)) else: if pool.is_master(): print("Processing ICs in parallel on {0} cores".format(pool.size+1))
import sys sys.path.append("..") from lenstools.simulations import Gadget2Snapshot import numpy as np from mayavi import mlab snap = Gadget2Snapshot.open("Data/gadget/snapshot_001") plane,res,NumPart = snap.cutLens(normal=2,center=7.0*snap.Mpc_over_h,thickness=0.5*snap.Mpc_over_h,plane_size=snap.lensMaxSize(),plane_resolution=128,thickness_resolution=8,smooth=2,tomography=True) scene = mlab.pipeline.volume(mlab.pipeline.scalar_field(plane)) snap.close()
import sys sys.path.append("..") from lenstools.simulations import Gadget2Snapshot import numpy as np from mayavi import mlab snap = Gadget2Snapshot.open("Data/gadget/snapshot_001") plane, res, NumPart = snap.cutLens(normal=2, center=7.0 * snap.Mpc_over_h, thickness=0.5 * snap.Mpc_over_h, plane_size=snap.lensMaxSize(), plane_resolution=128, thickness_resolution=8, smooth=2, tomography=True) scene = mlab.pipeline.volume(mlab.pipeline.scalar_field(plane)) snap.close()
def snapposition (fn): snap = Gadget2Snapshot.open(fn) return snap.getPositions()
from lenstools.simulations import Gadget2Snapshot import numpy as np import matplotlib.pyplot as plt from astropy.units import Mpc, m, s ########################Write################################# #Create an empty gadget snapshot snap = Gadget2Snapshot() #Generate random positions and velocities NumPart = 32**3 x = np.random.normal(loc=7.0, scale=5.0, size=(NumPart, 3)) * Mpc v = np.random.uniform(-1, 1, size=(NumPart, 3)) * m / s #Put the particles in the snapshot snap.setPositions(x) snap.setVelocities(v) #Generate minimal header snap.setHeaderInfo() #Write the snapshot snap.write("gadget_ic") ######################Read and visualize######################### #Open the snapshot snap = Gadget2Snapshot.open("gadget_ic")