def setup_sph_code(sph_code, N1, N2, L, rho,u): #rho -- local group density converter = nbody_system.nbody_to_si(1 | units.MSun, 1 | units.kpc) sph_code = sph_code(converter, redirection = 'none')#, mode = 'periodic')#, redirection = 'none')#change periodic #sph_code.parameters.periodic_box_size = 10.0 | units.kpc dm = Particles(N1) dm.mass = (rho * L**3) / N1 np.random.seed(12345) dm.x = L * np.random.uniform(0.0, 1.0, N1) dm.y = L * np.random.uniform(0.0, 1.0, N1) dm.z = L * np.random.uniform(0.0, 1.0, N1) dm.vx = np.zeros(N1) | units.km / units.s dm.vy = np.zeros(N1) | units.km / units.s dm.vz = np.zeros(N1) | units.km / units.s gas = Particles(N2) gas.mass = (rho * L**3) / N2 gas.x = L * np.random.uniform(0.0, 1.0, N2) gas.y = L * np.random.uniform(0.0, 1.0, N2) gas.z = L * np.random.uniform(0.0, 1.0, N2) gas.vx = np.zeros(N2) | units.km / units.s gas.vy = np.zeros(N2) | units.km / units.s gas.vz = np.zeros(N2) | units.km / units.s gas.u = u if isinstance(sph_code, Fi): sph_code.parameters.self_gravity_flag = True sph_code.parameters.timestep = 5 | units.Myr gas.h_smooth = L / N2**(1/3.0) dm.h_smooth = L / N1**(1/3.0) #gas.position -= 0.5 * L sph_code.gas_particles.add_particles(gas) sph_code.dm_particles.add_particles(dm) sph_code.commit_particles() return sph_code
def setup_sph_code(N1, N2, L, rho, u): #rho -- local group density #converter = nbody_system.nbody_to_si(1 | units.MSun, 1 | units.kpc) #sph_code = sph_code(converter)#, mode = 'periodic')#, redirection = 'none')#change periodic #sph_code.parameters.periodic_box_size = 10.0 | units.kpc dm = Particles(N1) dm.mass = 0.8 * (rho * L**3) / N1 #np.random.seed(12345) dm.x = L * np.random.uniform(-0.5, 0.5, N1) dm.y = L * np.random.uniform(-0.5, 0.5, N1) dm.z = L * np.random.uniform(-0.5, 0.5, N1) dm.vx = np.zeros(N1) | units.km / units.s dm.vy = np.zeros(N1) | units.km / units.s dm.vz = np.zeros(N1) | units.km / units.s gas = Particles(N2) gas.mass = 0.2 * (rho * L**3) / N2 gas.x = L * np.random.uniform(-0.5, 0.5, N2) gas.y = L * np.random.uniform(-0.5, 0.5, N2) gas.z = L * np.random.uniform(-0.5, 0.5, N2) gas.vx = np.zeros(N2) | units.km / units.s gas.vy = np.zeros(N2) | units.km / units.s gas.vz = np.zeros(N2) | units.km / units.s gas.u = u gas.h_smooth = L / N2**(1 / 3.0) dm.h_smooth = L / N1**(1 / 3.0) #sph_code.gas_particles.add_particles(gas) #sph_code.dm_particles.add_particles(dm) #sph_code.commit_particles() return gas, dm
def new_field_stars( N, width=10 | units.parsec, height=10 | units.parsec, depth=100 | units.parsec, massdistribution="salpeter", agespread=3 | units.Gyr, seed=1701, ): np.random.seed(seed) stars = Particles(N) stars.x = (np.random.random(N) - 0.5) * width stars.y = (np.random.random(N) - 0.5) * height stars.z = (np.random.random(N) - 0.02) * depth if massdistribution == "salpeter": stars.mass = new_salpeter_mass_distribution(N) return stars
def get_ps_from_hdf5(partfile, particle_type='all'): """ Returns an AMUSE particle set from a FLASH hdf5 particle file (NOT a plot file) or checkpoint file. Keyword arguments: partfile -- the FLASH particle file particle_type -- the type of particle that you want from FLASH. Can be: star -- active FLASH particle type sink -- sink FLASH particle type any other string or none returns all particle types. The default is all. Returns: stars -- An AMUSE particle set. """ import h5py ds = h5py.File(partfile, 'r') part_data = ds.get('tracer particles') part_names = ds.get('particle names') part_data = np.array(part_data) part_names = np.array(part_names) real_scalars = ds.get('real scalars') part_time = real_scalars[0][1] if (part_data.size > 1): mass_ind = np.where(np.char.strip(part_names)=='mass')[0] tag_ind = np.where(np.char.strip(part_names)=='tag')[0] type_ind = np.where(np.char.strip(part_names)=='type')[0] ct_ind = np.where(np.char.strip(part_names)=='creation_time')[0] posx_ind = np.where(np.char.strip(part_names)=='posx')[0] posy_ind = np.where(np.char.strip(part_names)=='posy')[0] posz_ind = np.where(np.char.strip(part_names)=='posz')[0] velx_ind = np.where(np.char.strip(part_names)=='velx')[0] vely_ind = np.where(np.char.strip(part_names)=='vely')[0] velz_ind = np.where(np.char.strip(part_names)=='velz')[0] # Get the particle types type_part = part_data[:,type_ind].flatten() if (particle_type == 'star' or particle_type == 'stars'): # Find only star particles (particle_type=1) star_ind = np.where(type_part==1)[0] elif (particle_type == 'sink' or particle_type == 'sinks'): # Find only sink particles (particle_type=2) star_ind = np.where(type_part==2)[0] else: star_ind = np.arange(len(type_part)) num_parts = len(star_ind) # allocate the array stars = Particles(num_parts) # Masses of stars stars.mass = part_data[star_ind,mass_ind] | u.g # Tags of stars stars.tag = part_data[star_ind,tag_ind] # Creation times of stars stars.ct = part_data[star_ind,ct_ind] | u.s # Positions stars.x = part_data[star_ind,posx_ind] | u.cm stars.y = part_data[star_ind,posy_ind] | u.cm stars.z = part_data[star_ind,posz_ind] | u.cm # Velocities stars.vx = part_data[star_ind,velx_ind] | u.cm/u.s stars.vy = part_data[star_ind,vely_ind] | u.cm/u.s stars.vz = part_data[star_ind,velz_ind] | u.cm/u.s # Set an index attribute. Useful for some functions/codes in AMUSE. stars.index_of_the_particle = np.arange(num_parts) else: print "Error: No particles found in this file!" stars = None return stars