def displace_galaxy(galaxy, rotation_mat, translation_vector, radial_velocity, transverse_velocity): widgets = [ 'Adjusting relative velocities and orientations: ', pbwg.AnimatedMarker(), pbwg.EndMsg() ] with pbar.ProgressBar(widgets=widgets, fd=sys.stdout) as progress: displaced_galaxy = Particles(len(galaxy)) displaced_galaxy.mass = galaxy.mass displaced_galaxy.position = galaxy.position displaced_galaxy.velocity = galaxy.velocity for body in displaced_galaxy: body.position = ([ body.x.value_in(units.kpc), body.y.value_in(units.kpc), body.z.value_in(units.kpc) ] @ rotation_mat) | units.kpc body.velocity = ([ body.vx.value_in(units.kms), body.vy.value_in(units.kms), body.vz.value_in(units.kms) ] @ rotation_mat) | units.kms displaced_galaxy.position += translation_vector displaced_galaxy.velocity += radial_velocity displaced_galaxy.velocity += transverse_velocity return displaced_galaxy
def test_particles(galaxy, n_halo, n_bulge, n_disk): _dsk = Particles(len(galaxy)) _dsk.mass = galaxy.mass _dsk.position = galaxy.position _dsk.velocity = galaxy.velocity disk = _dsk[n_bulge:n_halo] return disk
def new_system(): ''' Define the bodies here: ''' particles = Particles(3) planet_1 = particles[0] planet_1.mass = 1.0 | units.MSun planet_1.radius = 1.0 | units.RSun planet_1.position = (855251, -804836, -3186) | units.km planet_1.velocity = (7.893, 11.894, 0.20642) | (units.m / units.s) planet_2 = particles[1] planet_2.mass = 0.0025642 | units.MJupiter planet_2.radius = 3026.0 | units.km planet_2.position = (-0.3767, 0.60159, 0.03930) | units.AU planet_2.velocity = (-29.7725, -18.849, 0.795) | units.kms planet_3 = particles[2] planet_3.mass = 1.0 | units.MEarth planet_3.radius = 1.0 | units.REarth planet_3.position = (-0.98561, 0.0762, -7.847e-5) | units.AU planet_3.velocity = (-2.927, -29.803, -0.0005327) | units.kms particles.move_to_center() return particles
def bbh_hvgc(bbh_mass, mass_ratio, bbh_separation, bbh_phase, gc_closest, gc_vinf): """Creates particle list, defines circular-planar BBH orbit, places GC. """ bh1_mass = bbh_mass / (1 + mass_ratio) bh2_mass = mass_ratio * bh1_mass r1, r2, v1, v2 = bbh_orbit(bbh_mass, mass_ratio, bbh_separation) particles = Particles(3) bh1 = particles[0] bh1.mass = bh1_mass bh1.position = (r1 * math.cos(bbh_phase), r1 * math.sin(bbh_phase), 0 | units.km) bh1.velocity = (-v1 * math.sin(bbh_phase), v1 * math.cos(bbh_phase), 0 | units.kms) #bh1.position = (0,r1,0) #bh1.velocity = (-v1,0,0) bh2 = particles[1] bh2.mass = bh2_mass bh2.position = (-r2 * math.cos(bbh_phase), -r2 * math.sin(bbh_phase), 0 | units.km) bh2.velocity = (v2 * math.sin(bbh_phase), -v2 * math.cos(bbh_phase), 0 | units.kms) # bh2.position = (0,-r2,0) # bh2.velocity = (v1,0,0) b = impact_parameter(bbh_mass, gc_closest, gc_vinf) hvgc = particles[2] hvgc.mass = 1.0e7 | units.MSun hvgc.position = (b, -100, 0) | units.parsec hvgc.velocity = (0 | units.kms, gc_vinf, 0 | units.kms) particles.move_to_center() return particles
def resolve_collision(collision_detection, gravity, bodies): if collision_detection.is_set(): for ci in range(len(collision_detection.particles(0))): encountering_particles = Particles(particles=[collision_detection.particles(0)[ci], collision_detection.particles(1)[ci]]) colliding_particles = encountering_particles.get_intersecting_subset_in(bodies) merge_two_particles(bodies, colliding_particles) bodies.synchronize_to(gravity.particles)
def resolve_collision(collision_detection, gravity_code, bodies, time): print("Well, we have an actual collision between two or more objects.") print("This happened at time=", time.in_(units.yr)) for ci in range(len(collision_detection.particles(0))): # Multiple pairs of collisions could take place in one timestep encountering_particles = Particles(particles=[collision_detection.particles(0)[ci], collision_detection.particles(1)[ci]]) colliding_objects = encountering_particles.get_intersecting_subset_in(bodies) merge_two_bodies(bodies, colliding_objects) bodies.synchronize_to(gravity_code.particles) # Update bodies to contain 1 particle instead of 2
def add_comet_objects(pre_tack_giants_system, N_objects, comet_positions, comet_velocities): for i in tqdm(range(N_objects)): comet = Particles(1) comet.name = "OORT_" + str(i) comet.mass = 0.0 | units.MSun #Take massless test particles comet.radius = 0.0 | units.RSun comet.position = (comet_positions[i, 0], comet_positions[i, 1], comet_positions[i, 2]) comet.velocity = (comet_velocities[i, 0], comet_velocities[i, 1], comet_velocities[i, 2]) comet.density = 0.0 | (units.MSun / (units.RSun**3)) pre_tack_giants_system.add_particle(comet) z_comp = np.arctan( 100 / 8500. ) #Determining the z-component of the sun's trajectory around the galactic center for i in range(len(pre_tack_giants_system) ): #adding the sun's trajectory around the galactic center pre_tack_giants_system[i].position += (1, 0, 0) * (8.5 | units.kpc) pre_tack_giants_system[i].velocity += (0, np.sqrt(1 - z_comp**2), z_comp) * (220 | units.kms) return pre_tack_giants_system
def Nstars(): particles = Particles(Ntracker) i = 0 while i < Ntracker: #here we generate our N bodies, we can change code in here to reflect different things #We can distribut them randomly, have them near eachother, all at the same radius etc. #All particles are in the x-y plane, this should still be adjusted accordingly to give some z randomization #For now I will keep the mass the same and distribute uniformly over a circle smallangle = random.random( ) * 2 * math.pi #smallangle is angle around solarpos, angle is angle around Sag A radius = neighbourradius * math.sqrt( random.random()) #sqrt so it's uniform and not clumped at r=0 particles[i].mass = 5 | units.MSun particles[i].radius = 1.5 | units.RSun #I have no z displacement, I can still add this though particles[i].position = (radius * math.cos(smallangle) + Solarposition[0], radius * math.sin(smallangle) + Solarposition[1], Solarposition[2]) #arctan only -pi/2 to pi/2, this codes gives us from -pi/2 to 3pi/2, we need global angle to fix vel vector if particles[i].x < 0 | units.kpc: angle = math.atan(particles[i].y / particles[i].x) + math.pi else: angle = math.atan(particles[i].y / particles[i].x) #print(360*angle/(2*math.pi)) you can check that the angles are correct this way particles[i].velocity = [ math.sin(angle) * vel + VelocityMilky[0], -math.cos(angle) * vel + VelocityMilky[1], VelocityMilky[2] ] i += 1 return particles
def MW_and_M31(): MW_M31 = Particles(2 + 2 * N) MW = MW_M31[0] MW.mass = 1 * 10**11 | units.MSun MW.position = (0, 0, 0) | units.kpc MW.velocity = (10, 10, 0) | units.kms #MW.u = 0.5*(math.sqrt(2)*10)**2*1000*1000 | units.m**2 * units.s**-2 for i in range(N): MW_M31[1 + i].mass = 10 | units.MSun angle = random.random() * 2 * math.pi MW_M31[1 + i].position = (r * math.cos(angle), r * math.sin(angle), 0) | units.kpc MW_M31[1 + i].velocity = (10 + v * math.sin(angle), 10 - math.cos(angle) * v, 0) | units.kms #Tried to add energy thing since Fi wanted it but didn't work #MW_M31[1+i].u = 0.5*(math.sqrt((10 + v*math.sin(angle))**2+10 - math.cos(angle)*v))**2*1000*1000 | units.m**2 * units.s**-2 M31 = MW_M31[N + 1] M31.mass = 1.6 * 10**11 | units.MSun M31.position = (780, 0, 0) | units.kpc M31.velocity = (0, 0, 0) | units.kms for i in range(N): MW_M31[N + 2 + i].mass = 10 | units.MSun angle = random.random() * 2 * math.pi MW_M31[N + 2 + i].position = (780 + r * math.cos(angle), r * math.sin(angle), 0) | units.kpc MW_M31[N + 2 + i].velocity = (v * math.sin(angle), -math.cos(angle) * v, 0) | units.kms #MW_M31[N+2+i].u = 0.5*230**2*1000*1000 | units.m**2 * units.s**-2 return MW_M31
def body_init_sun_jupiter(): particles = Particles(2) sun = particles[0] sun.mass = 1.0 | units.MSun sun.radius = 1.0 | units.RSun sun.position = (855251, -804836, -3186) | units.km sun.velocity = (7.893, 11.894, 0.20642) | (units.m / units.s) jupiter = particles[1] jupiter.mass = 1.0 | units.MJupiter jupiter.radius = 1.0 | units.RJupiter jupiter.position = (-4.9829, 2.062, -0.10990) | units.AU jupiter.velocity = (-5.158, -11.454, 0.13558) | units.kms particles.move_to_center() return particles
def evolve_a_single_star(m, z, model_time, algorithm=EVtwin): """ Code for Assignment 2A. @param m: mass of the star. @param z: metallicity of the star. @param stellar: specify the stellar evolution code. valid options are: MESA, EVtwin (both Henyey) SSE, SeBa (both parameterized) @return: Dict with input parameters. mass/luminosity and runtime """ t_start = time() stellar = algorithm(redirection="none") # stellar = algorithm() algorithm_name = str(algorithm).split('.')[-1][:-2] stellar.parameters.metallicity = z # stellar.commit_parameters() # print "\nstellar.parameters:\n{0}".format(stellar.parameters) star = Particles(mass=m) # print "\nStar:\n{0}".format(star) # print "\nstellar.particles:\n{0}".format(stellar.particles) stellar.particles.add_particles(star) channel_from_stellar = stellar.particles.new_channel_to(star) stellar.evolve_model(model_time) channel_from_stellar.copy() # print "L(t=", star.age, ")=", star.luminosity, "R=", star.radius, "\n" stellar.stop() runtime = (time() - t_start) set_printing_strategy("custom", preferred_units=[units.MSun, units.RSun, units.Gyr, units.LSun], precision=4, prefix="", separator=" [", suffix="]") # LaTeX table/tabular output <3 print "{0} & {1} & {2} & {3} & {4} & {5} & {6} \\\\ "\ .format(algorithm_name, z, model_time, star.luminosity, m, star.radius, runtime) data = {} data["algorithm_name"] = algorithm_name data["mass"] = m data["metallicity"] = z data["luminosity"] = star.luminosity data["radius"] = star.radius data["runtime"] = runtime #print data.items() return data
def body_init(v1, v2, m3): # Initial condition (mass, coordinates, velocities) particles = Particles(3) body0 = particles[0] body0.mass = 1.0 | nbody_system.mass body0.position = (-1, 0., 0.) | nbody_system.length body0.velocity = (v1, v2, 0) | nbody_system.speed body1 = particles[1] body1.mass = 1.0 | nbody_system.mass body1.position = (1., 0., 0.) | nbody_system.length body1.velocity = (v1, v2, 0.) | nbody_system.speed body2 = particles[2] body2.mass = m3 | nbody_system.mass body2.position = (0., 0., 0.) | nbody_system.length body2.velocity = (-2 * v1 / m3, -2 * v2 / m3, 0.) | nbody_system.speed particles.move_to_center() return particles
def body_init(): particles = Particles(3) body0 = particles[0] body0.mass = 1.0 | nbody_system.mass body0.position = (-1., 0., 0.) | nbody_system.length body0.velocity = (v1, v2, 0.) | nbody_system.speed body1 = particles[1] body1.mass = 1.0 | nbody_system.mass body1.position = (1., 0., 0.) | nbody_system.length body1.velocity = (v1, v2, 0.) | nbody_system.speed body2 = particles[2] body2.mass = m3 | nbody_system.mass body2.position = (0., 0., 0.) | nbody_system.length body2.velocity = (-2 / m3 * v1, -2 / m3 * v2, 0.) | nbody_system.speed particles.move_to_center() return particles
def MW_and_M31(): MW_M31 = Particles(2) MW = MW_M31[0] MW.mass = 1*10**6 | units.MSun MW.position = (0,0,0) | units.kpc MW.velocity = (0.1,0.1,0) | units.kms M31 = MW_M31[1] M31.mass = 1.6*10**6 | units.MSun M31.position = (7.8,0,0) | units.kpc M31.velocity = (0,0,0) | units.kms return MW_M31
def sma_determinator(primary, secondary): binary = Particles(0) binary.add_particle(primary) binary.add_particle(secondary) orbital_params = get_orbital_elements_from_binary(binary, G=constants.G) return orbital_params[2]
def sun_venus_and_earth(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | nbody_system.mass #sun.radius = 1.0 | units.RSun sun.position = (-1, 0., 0.) | nbody_system.length sun.velocity = (v1, v2, 0) | nbody_system.speed venus = particles[1] venus.mass = 1.0 | nbody_system.mass #venus.radius = 1.0 | units.RSun venus.position = (1., 0., 0.) | nbody_system.length venus.velocity = (v1, v2, 0.) | nbody_system.speed earth = particles[2] earth.mass = 0.5 | nbody_system.mass #earth.radius = 1.0 | units.RSun earth.position = (0., 0., 0.) | nbody_system.length earth.velocity = (-4 * v1, -4 * v2, 0.) | nbody_system.speed particles.move_to_center() return particles
def sun_venus_and_earth(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | units.MSun sun.position = (0.0, 0.0, 0.0) | units.m sun.velocity = (0.0, 0.0, 0.0) | (units.m / units.s) sun.radius = 1.0 | units.RSun venus = particles[1] venus.mass = 0.0025642 | units.MJupiter venus.radius = 3026.0 | units.km venus.position = (0.6335, 0.3499, -0.03179) | units.AU venus.velocity = (-17.0420, 30.5055, 1.4004) | units.kms earth = particles[2] earth.mass = 5.9736e24 | units.kg earth.radius = 6371.0 | units.km earth.position = (0.2421, -0.9875, -0.00004) | units.AU earth.velocity = (28.4468, 6.98125, 0.0002) | units.kms particles.move_to_center() return particles
def sun_venus_and_earth(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | units.MSun sun.radius = 1.0 | units.RSun sun.position = (855251, -804836, -3186) | units.km sun.velocity = (7.893, 11.894, 0.20642) | (units.m / units.s) venus = particles[1] venus.mass = 0.0025642 | units.MJupiter venus.radius = 3026.0 | units.km venus.position = (-0.3767, 0.60159, 0.03930) | units.AU venus.velocity = (-29.7725, -18.849, 0.795) | units.kms earth = particles[2] earth.mass = 1.0 | units.MEarth earth.radius = 1.0 | units.REarth earth.position = (-0.98561, 0.0762, -7.847e-5) | units.AU earth.velocity = (-2.927, -29.803, -0.0005327) | units.kms particles.move_to_center() return particles
def sun_venus_and_earth(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | units.kg #sun.radius = 1e-2 | units.m sun.position = (-1, 0, 0) | units.m sun.velocity = (v1, v2, 0) | (units.m / units.s) venus = particles[1] venus.mass = 1.0 | units.kg #venus.radius = 1e-2 | units.m venus.position = (1, 0, 0) | units.m venus.velocity = (v1, v2, 0) | (units.m / units.s) earth = particles[2] earth.mass = 0.5 | units.kg #earth.radius = 1e-2 | units.m earth.position = (0, 0, 0) | units.m earth.velocity = (-4 * v1, -4 * v2, 0) | (units.m / units.s) particles.move_to_center() return particles
def new_system(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | units.MSun sun.radius = 1.0 | units.REarth sun.position = (1, 0, 0) | units.AU sun.velocity = (0, 0, 20) | units.kms venus = particles[1] venus.mass = 1.0 | units.MSun venus.radius = 1.0 | units.REarth venus.position = (0, -1, 0) | units.AU venus.velocity = (20, 0, 0) | units.kms earth = particles[2] earth.mass = 1.0 | units.MSun earth.radius = 1.0 | units.REarth earth.position = (0, 0, -1) | units.AU earth.velocity = (0, 20, 0) | units.kms particles.move_to_center() return particles
def sun_venus_and_earth(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | units.MSun sun.position = (0.0,0.0,0.0) | units.m sun.velocity = (0.0,0.0,0.0) | (units.m/units.s) sun.radius = 1.0 | units.RSun venus = particles[1] venus.mass = 0.0025642 | units.MJupiter venus.radius = 3026.0 | units.km venus.position = (0.6335, 0.3499, -0.03179) | units.AU venus.velocity = (-17.0420, 30.5055, 1.4004) | units.kms earth = particles[2] earth.mass = 5.9736e24 | units.kg earth.radius = 6371.0 | units.km earth.position = (0.2421, -0.9875, -0.00004) | units.AU earth.velocity = (28.4468, 6.98125, 0.0002) | units.kms particles.move_to_center() return particles
def sun_venus_and_earth(): particles = Particles(3) sun = particles[0] sun.mass = 1.0 | units.MSun sun.radius = 1.0 | units.RSun sun.position = (855251, -804836, -3186) |units.km sun.velocity = (7.893, 11.894, 0.20642) | (units.m/units.s) venus = particles[1] venus.mass = 0.0025642 | units.MJupiter venus.radius = 3026.0 | units.km venus.position = (-0.3767, 0.60159, 0.03930) | units.AU venus.velocity = (-29.7725, -18.849, 0.795) | units.kms earth = particles[2] earth.mass = 1.0 | units.MEarth earth.radius = 1.0 | units.REarth earth.position = (-0.98561, 0.0762, -7.847e-5) | units.AU earth.velocity = (-2.927, -29.803, -0.0005327) | units.kms particles.move_to_center() return particles
def evolve_sun_jupiter(particles, tend, dt): SunJupiter = Particles() SunJupiter.add_particle(particles[0]) SunJupiter.add_particle(particles[1]) converter = nbody_system.nbody_to_si(particles.mass.sum(), particles[1].position.length()) gravity = ph4(converter) gravity.particles.add_particles(particles) channel_from_to_SunJupiter = gravity.particles.new_channel_to(SunJupiter) semi_major_axis = [] eccentricity = [] times = quantities.arange(0 | units.yr, tend, dt) for i, t in enumerate(times): print "Time=", t.in_(units.yr) channel_from_to_SunJupiter.copy() orbital_elements = orbital_elements_from_binary(SunJupiter, G=constants.G) a = orbital_elements[2] e = orbital_elements[3] semi_major_axis.append(a.value_in(units.AU)) eccentricity.append(e) gravity.evolve_model(t, timestep=dt) # Plot fig = plt.figure(figsize=(8, 6)) ax1 = fig.add_subplot(211, xlabel='time(yr)', ylabel='semi major axis (AU)') ax1.plot(times.value_in(units.yr), semi_major_axis) ax2 = fig.add_subplot(212, xlabel='time(yr)', ylabel='eccentriity') ax2.plot(times.value_in(units.yr), eccentricity) plt.show() gravity.stop()
def evolve_sun_jupiter(particles, tend, dt): SunJupiter = Particles() SunJupiter.add_particle(particles[0]) SunJupiter.add_particle(particles[1]) converter=nbody_system.nbody_to_si(particles.mass.sum(), particles[1].position.length()) gravity = ph4(converter) gravity.particles.add_particles(particles) channel_from_to_SunJupiter = gravity.particles.new_channel_to(SunJupiter) semi_major_axis = list() eccentricity = list() times = quantities.arange(0|units.yr, tend+dt, dt) for i,t in enumerate(times): #print "Time =", t.in_(units.yr) channel_from_to_SunJupiter.copy() orbital_elements = orbital_elements_from_binary(SunJupiter, G=constants.G) a = orbital_elements[2] e = orbital_elements[3] semi_major_axis.append(a.value_in(units.AU)) eccentricity.append(e) gravity.evolve_model(t, timestep=dt) gravity.stop() # save the data: times, semi_major_axis and eccentricity of Jupiter's orbit t_a_e = np.column_stack((times.value_in(units.yr), semi_major_axis, eccentricity)) np.savetxt('t_a_e.txt', t_a_e, delimiter=',') # make plots fig = plt.figure(figsize = (8, 6)) ax1 = fig.add_subplot(211, xlabel = 'time(yr)', ylabel = 'semi major axis (AU)') ax1.plot(times.value_in(units.yr), semi_major_axis) ax2 = fig.add_subplot(212, xlabel = 'time(yr)', ylabel = 'eccentriity') ax2.plot(times.value_in(units.yr), eccentricity) plt.show()
def add_comet_objects(pre_tack_giants_system, N_objects, comet_positions, comet_velocities): for i in tqdm(range(N_objects)): comet = Particles(1) comet.name = "OORT_" + str(i) comet.mass = 0.0 | units.MSun #Take massless test particles comet.radius = 0.0 | units.RSun comet.position = (comet_positions[i, 0], comet_positions[i, 1], comet_positions[i, 2]) comet.velocity = (comet_velocities[i, 0], comet_velocities[i, 1], comet_velocities[i, 2]) comet.density = 0.0 | (units.MSun / (units.RSun**3)) pre_tack_giants_system.add_particle(comet) return pre_tack_giants_system
def merge_two_particles(gravity, particles_in_encounter): new_particle = Particles(1) new_particle.mass = particles_in_encounter.total_mass() new_particle.position = particles_in_encounter.center_of_mass() new_particle.velosity = particles_in_encounter.center_of_mass_velocity() new_particle.radius = particles_in_encounter.radius.sum() gravity.particles.add_particles(new_particle) gravity.particles.remove_particles(particles_in_encounter)
def MW(): MW = Particles(1 + N1) MW1 = MW[0] MW1.mass = 1 * 10**11 | units.MSun MW1.position = (0, 0, 0) | units.kpc MW1.velocity = (10, 10, 0) | units.kms #MW.u = 0.5*(math.sqrt(2)*10)**2*1000*1000 | units.m**2 * units.s**-2 for i in range(N1): MW[1 + i].mass = 10 | units.MSun angle = random.random() * 2 * math.pi MW[1 + i].position = (r * math.cos(angle), r * math.sin(angle), 0) | units.kpc MW[1 + i].velocity = (10 + v * math.sin(angle), 10 - math.cos(angle) * v, 0) | units.kms return MW
def add_comet_objects(system, N_objects, rand_pos, rand_vel): for i in tqdm(range(N_objects)): oort = Particles(1) oort.name = "OORT_" + str(i) oort.mass = 0.0 | units.MSun oort.radius = (2.3 | units.km).in_(units.RSun) # This is purely non-zero for collisional purposes oort.position = (rand_pos[i, 0], rand_pos[i, 1], rand_pos[i, 2]) oort.velocity = (rand_vel[i, 0], rand_vel[i, 1], rand_vel[i, 2]) system.add_particle(oort) for particle in system: #So that Mercury will work particle.density = 3*particle.mass/(4*np.pi*particle.radius**3) return system
def new_system(): stars = Particles(3) sun = stars[0] sun.mass = units.MSun(1.0) sun.position = units.m(np.array((0.0,0.0,0.0))) sun.velocity = units.ms(np.array((0.0,0.0,0.0))) sun.radius = units.RSun(1.0) earth = stars[1] earth.mass = units.kg(5.9736e24) earth.radius = units.km(6371) earth.position = units.km(np.array((149.5e6,0.0,0.0))) earth.velocity = units.ms(np.array((0.0,29800,0.0))) moon = stars[2] moon.mass = units.kg(7.3477e22 ) moon.radius = units.km(1737.10) moon.position = units.km(np.array((149.5e6 + 384399.0 ,0.0,0.0))) moon.velocity = ([0.0,1.022,0] | units.km/units.s) + earth.velocity return stars
def get_ps_from_yt(pltfile, particle_type="all"): """Take a FLASH plotfile and return an AMUSE particle set. Keyword arguments: pltfile -- The FLASH hdf5 plot or checkpoint file. Note a plotfile means there must also be a particle file with the same ID number. 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 yt ds = yt.load(pltfile) dd = ds.all_data() all_parts_mass = dd['particle_mass'].in_units('Msun') all_parts_pos = dd['particle_position'].in_units('cm') all_parts_vel = dd['particle_velocity'].in_units('cm/s') if (particle_type == 'star' or particle_type == 'stars'): # Index for only the star particles, and not including sink particles. mass_ind = np.where(dd['particle_type'].v == 1) elif (particle_type == 'sink' or particle_type == 'sinks'): mass_ind = np.where(dd['particle_type'].v == 2) else: mass_ind = np.arange(len(all_parts_mass)) all_parts_mass = all_parts_mass[mass_ind] all_parts_pos = all_parts_pos[mass_ind] all_parts_vel = all_parts_vel[mass_ind] num_parts = len(all_parts_mass.v) stars = Particles(num_parts) stars.mass = all_parts_mass.v | u.MSun stars.position = all_parts_pos.v | u.cm stars.velocity = all_parts_vel.v | u.cm/u.s stars.index_of_the_particle = range(num_parts) return stars
def merge_two_bodies(bodies, particles_in_encounter): com_pos = particles_in_encounter.center_of_mass() com_vel = particles_in_encounter.center_of_mass_velocity() d = (particles_in_encounter[0].position - particles_in_encounter[1].position) v = (particles_in_encounter[0].velocity - particles_in_encounter[1].velocity) print("Actually merger occurred:") print("Two objects (M=",particles_in_encounter.mass.in_(units.MSun), ") collided with d=", d.length().in_(units.au)) #time.sleep(10) new_particle=Particles(1) new_particle.mass = particles_in_encounter.total_mass() new_particle.position = com_pos new_particle.velocity = com_vel new_particle.radius = ((particles_in_encounter.radius**3).sum())**(1./3.) # New radius cannot simply be summed bodies.add_particles(new_particle) bodies.remove_particles(particles_in_encounter)
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