def nbody_integrator(Ncl, mcl, rcl, t_end, n_steps, algorithm=BHTree, timestep=None): converter = nbody_system.nbody_to_si(mcl, rcl) bodies = new_plummer_model(Ncl, convert_nbody=converter) verbose = False # For AMUSE functions this is called redirection. algorithm_name = str(algorithm).split('.')[-1][:-2] # Allow selecting Stellar Dynamics code trough a function argument. try: if algorithm_name == 'Hermite': gravity = algorithm(converter, number_of_workers=4) else: gravity = algorithm(converter) except Exception, e: # Too generic, but I don't know what Error to expect. # raise Exception print "Failure for {0}".format(algorithm_name) print Exception, e print traceback.format_exc() return None
def cluster_init(N, mMin, mMax, alpha, mCluster, rCluster): converter = nbody_system.nbody_to_si(mCluster, rCluster) cluster = new_plummer_model(N, convert_nbody=converter) mZAMS = new_powerlaw_mass_distribution(N, mMin, mMax, alpha) cluster.mass = mZAMS cluster.scale_to_standard(converter) return cluster
def main(N=10): figure(figsize=(5,5)) bodies = new_plummer_model(N) scatter(bodies.x, bodies.y) xlim(-1, 1) ylim(-1, 1) xlabel("X") ylabel("Y") show()
def main(N=10): figure(figsize=(5, 5)) bodies = new_plummer_model(N) scatter(bodies.x, bodies.y) xlim(-1, 1) ylim(-1, 1) xlabel("X") ylabel("Y") show()
def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): R_galaxy = 0.1 | units.kpc M_galaxy = 1.6e10 | units.MSun converter = nbody_system.nbody_to_si(M_galaxy, R_galaxy) galaxy = new_plummer_model(10000, convert_nbody=converter) print "com:", galaxy.center_of_mass().in_(units.kpc) print "comv:", galaxy.center_of_mass_velocity().in_(units.kms) print len(galaxy) galaxy_code = BHTree(converter, number_of_workers=2) galaxy_code.parameters.epsilon_squared = (0.01 | units.kpc)**2 channe_to_galaxy = galaxy_code.particles.new_channel_to(galaxy) channe_to_galaxy.copy() galaxy_code.particles.add_particles(galaxy) inner_stars = galaxy.select(lambda r: r.length() < Rinit, ["position"]) Minner = inner_stars.mass.sum() print "Minner=", Minner.in_(units.MSun) print "Ninner=", len(inner_stars) vc_inner = (constants.G * Minner / Rinit).sqrt() converter = nbody_system.nbody_to_si(Mcluster, Rcluster) stars = new_king_model(N, W0, convert_nbody=converter) masses = new_powerlaw_mass_distribution(N, 0.1 | units.MSun, 100 | units.MSun, -2.35) stars.mass = masses stars.scale_to_standard(converter) stars.x += Rinit stars.vy += 0.8 * vc_inner cluster_code = ph4(converter, number_of_workers=2) cluster_code.particles.add_particles(stars) channel_to_stars = cluster_code.particles.new_channel_to(stars) system = bridge(verbose=False) system.add_system(cluster_code, (galaxy_code, )) system.add_system(galaxy_code, (cluster_code, )) system.timestep = 0.1 * timestep times = quantities.arange(0 | units.Myr, tend, timestep) for i, t in enumerate(times): print "Time=", t.in_(units.Myr) channe_to_galaxy.copy() channel_to_stars.copy() inner_stars = galaxy.select(lambda r: r.length() < Rinit, ["position"]) print "Minner=", inner_stars.mass.sum().in_(units.MSun) system.evolve_model(t, timestep=timestep) plot_galaxy_and_stars(galaxy, stars) galaxy_code.stop() cluster_code.stop()
def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): R_galaxy=0.1 | units.kpc M_galaxy=1.6e10 | units.MSun converter=nbody_system.nbody_to_si(M_galaxy, R_galaxy) galaxy=new_plummer_model(10000,convert_nbody=converter) print "com:", galaxy.center_of_mass().in_(units.kpc) print "comv:", galaxy.center_of_mass_velocity().in_(units.kms) print len(galaxy) galaxy_code = BHTree(converter, number_of_workers=2) galaxy_code.parameters.epsilon_squared = (0.01 | units.kpc)**2 channe_to_galaxy = galaxy_code.particles.new_channel_to(galaxy) channe_to_galaxy.copy() galaxy_code.particles.add_particles(galaxy) inner_stars = galaxy.select(lambda r: r.length()<Rinit,["position"]) Minner = inner_stars.mass.sum() print "Minner=", Minner.in_(units.MSun) print "Ninner=", len(inner_stars) vc_inner = (constants.G*Minner/Rinit).sqrt() converter=nbody_system.nbody_to_si(Mcluster,Rcluster) stars=new_king_model(N,W0,convert_nbody=converter) masses = new_powerlaw_mass_distribution(N, 0.1|units.MSun, 100|units.MSun, -2.35) stars.mass = masses stars.scale_to_standard(converter) stars.x += Rinit stars.vy += 0.8*vc_inner cluster_code=ph4(converter, number_of_workers=2) cluster_code.particles.add_particles(stars) channel_to_stars=cluster_code.particles.new_channel_to(stars) system=bridge(verbose=False) system.add_system(cluster_code, (galaxy_code,)) system.add_system(galaxy_code, (cluster_code,)) system.timestep = 0.1*timestep times = quantities.arange(0|units.Myr, tend, timestep) for i,t in enumerate(times): print "Time=", t.in_(units.Myr) channe_to_galaxy.copy() channel_to_stars.copy() inner_stars = galaxy.select(lambda r: r.length()<Rinit,["position"]) print "Minner=", inner_stars.mass.sum().in_(units.MSun) system.evolve_model(t,timestep=timestep) plot_galaxy_and_stars(galaxy, stars) galaxy_code.stop() cluster_code.stop()
def plummer(mass, radius, N): conv = nbody_system.nbody_to_si(radius, mass) pm = new_plummer_model(N, convert_nbody=conv, do_scale=True) gravity = ph4(conv) pm.positon = pm.position + ((100, 100, 100) | units.parsec) pm.velocity = pm.velocity + ((-1000, -1000, -1000) | (units.km / units.s)) lr, mf = pm.LagrangianRadii(unit_converter=conv) print lr, mf #print 'position of pm[0] before gravity evolution: ',pm[0].position #print 'velocity of pm[0] before gravity evolution: ',pm[0].velocity #while gravity.model_time < 1000000 | units.yr: #print gravity.particles[0].position #gravity.evolve_model(gravity.model_time + (100000 | units.yr)) #print 'position of pm[0] after gravity evolution: ',pm[0].position #print 'velocity of pm[0] after gravity evolution: ',pm[0].velocity gravity.stop()
def nbody_integrator(Ncl, mcl, rcl, t_end, n_steps, escape_velocity_fraction, R): converter = nbody_system.nbody_to_si(mcl, rcl) bodies = new_plummer_model(Ncl, convert_nbody=converter) #estimate of milky way mass by "Mass models of the Milky Way", McMillan blackhole_mass = 1.26e12 | units.MSun blackhole = Particle(mass=blackhole_mass) blackhole.position = [0, 0, 0] | units.m cluster_velocity = [0, 0, 0] | units.m / units.s cluster_position = [0, 0, 0] | units.parsec cluster_position[0] = R G_si = converter.to_si(nbody_system.G) escape_v = (2 * G_si * blackhole_mass / R).sqrt().as_quantity_in(units.m / units.s) V = escape_v * escape_velocity_fraction cluster_velocity[1] = V bodies.move_to_center() bodies.velocity += cluster_velocity bodies.position += cluster_position bodies.add_particle(blackhole) gravity = BHTree(converter) gravity.particles.add_particles(bodies) channel_from_gravity_to_framework = gravity.particles.\ new_channel_to(bodies) time = zero dt = t_end / float(n_steps) x = 0 base_path = "encounter_plots/"+str(R.value_in(units.parsec))+"_"+\ str(escape_velocity_fraction)+"_" while time < t_end: plot_cluster(bodies, base_path + str(x), time, rcl, V) time += dt gravity.evolve_model(time) channel_from_gravity_to_framework.copy() x += 1 plot_cluster(bodies, base_path + str(x), time, rcl, V) gravity.stop() return V, bodies
def nbody_integrator(Ncl, mcl, rcl, t_end, n_steps, escape_velocity_fraction, R): converter = nbody_system.nbody_to_si(mcl, rcl) bodies = new_plummer_model(Ncl, convert_nbody=converter) #estimate of milky way mass by "Mass models of the Milky Way", McMillan blackhole_mass = 1.26e12 | units.MSun blackhole = Particle(mass=blackhole_mass) blackhole.position = [0,0,0] | units.m cluster_velocity = [0,0,0] | units.m / units.s cluster_position = [0,0,0] | units.parsec cluster_position[0] = R G_si = converter.to_si(nbody_system.G) escape_v = (2*G_si*blackhole_mass/R).sqrt().as_quantity_in(units.m/units.s) V = escape_v * escape_velocity_fraction cluster_velocity[1] = V bodies.move_to_center() bodies.velocity += cluster_velocity bodies.position += cluster_position bodies.add_particle(blackhole) gravity = BHTree(converter) gravity.particles.add_particles(bodies) channel_from_gravity_to_framework = gravity.particles.\ new_channel_to(bodies) time = zero dt = t_end / float(n_steps) x = 0 base_path = "encounter_plots/"+str(R.value_in(units.parsec))+"_"+\ str(escape_velocity_fraction)+"_" while time < t_end: plot_cluster(bodies, base_path+str(x),time, rcl, V) time += dt gravity.evolve_model(time) channel_from_gravity_to_framework.copy() x+=1 plot_cluster(bodies, base_path+str(x),time, rcl, V) gravity.stop() return V, bodies
return vc N = 100 # Stars in the cluster Rv = 1. | al.units.parsec # virial radius of cluster RSun = 50 | al.units.parsec # Position of cluster in the galaxy M = al.new_kroupa_mass_distribution( N, 100. | al.units.MSun) # N samples from Kroupa IMF, with each max. 100 MSun # Contains the plummer radius and total mass of the plummer distribution converter = al.nbody_system.nbody_to_si(R_virial_to_plummer(Rv), M.sum()) bodies = al.new_plummer_model(N, converter) bodies.mass = M t_end = 2.5 | al.units.Myr # End time dt = 0.01 | al.units.Myr # Bridge timestep # Galaxy model with 1.6x10^10 MSun within 1 kpc, and power law scaling with index 1.2 galaxy_gravity = GalacticPotential(1. | al.units.kpc, 1.6E10 | al.units.MSun, 1.2) # Place cluster in galaxy bodies.x += RSun bodies.vy += galaxy_gravity.circular_velocity(RSun) cluster_gravity = al.ph4(converter) cluster_gravity.particles.add_particles(bodies)