def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): Rgal=1. | units.kpc Mgal=1.6e10 | units.MSun alpha=1.2 galaxy_code=GalacticCenterGravityCode(Rgal, Mgal, alpha) cluster_code=make_king_model_cluster(BHTree,N,W0, M,R, parameters=[("epsilon_squared", (0.01 | units.parsec)**2)]) stars=cluster_code.particles.copy() stars.x += Rinit stars.vy = 0.8*galaxy_code.circular_velocity(Rinit) channel=stars.new_channel_to(cluster_code.particles) channel.copy_attributes(["x","y","z","vx","vy","vz"]) system=bridge(verbose=False) system.add_system(cluster_code, (galaxy_code,)) times=numpy.arange(0|units.Myr, tend, timestep) for i,t in enumerate(times): system.evolve_model(t,timestep=timestep) x=system.particles.x.value_in(units.parsec) y=system.particles.y.value_in(units.parsec) cluster_code.stop() from prepare_figure import single_frame, get_distinct colors = get_distinct(1) f = single_frame('X [pc]', 'Y [pc]') pyplot.xlim(-60, 60) pyplot.ylim(-60, 60) pyplot.scatter(x,y, c=colors[0], s=50, lw=0) pyplot.savefig("Arches")
def test1(self): convert = nbody_system.nbody_to_si(1.e5 | units.MSun, 1.0 | units.parsec) eps=2.e-4 | nbody_system.length test_class=PhiGRAPE number_of_particles = 50 stars = new_king_model(number_of_particles, W0=7, convert_nbody=convert) stars.radius = 0.0 | units.RSun cluster=test_class(convert) cluster.parameters.epsilon_squared = eps**2 cluster.particles.add_particles(stars) cluster.synchronize_model() Ep1=convert.to_nbody(cluster.potential_energy).number Ek1=convert.to_nbody(cluster.kinetic_energy).number parts=cluster.particles.copy() parts1=parts.select_array(lambda x: (x > 0 | units.m), ['x'] ) parts2=parts.select_array(lambda x: (x < 0 | units.m), ['x'] ) cluster1=sys_from_parts(test_class, parts1, convert, eps) cluster2=sys_from_parts(test_class, parts2, convert, eps) cluster1.synchronize_model() cluster2.synchronize_model() bridgesys=bridge() bridgesys.add_system(cluster1, (cluster2,) ) bridgesys.add_system(cluster2, (cluster1,) ) Ep2=convert.to_nbody(bridgesys.potential_energy).number Ek2=convert.to_nbody(bridgesys.kinetic_energy).number self.assertAlmostEqual(Ek1,Ek2,12) self.assertAlmostEqual(Ep1,Ep2,12)
def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): galaxy_code = MilkyWay_galaxy() converter = nbody_system.nbody_to_si(M, R) cluster_code = drift_without_gravity(convert_nbody=converter) bodies = new_king_model(N, W0, convert_nbody=converter) cluster_code.particles.add_particles(bodies) stars = cluster_code.particles.copy() stars.x += Rinit stars.vy = 0.8 * galaxy_code.circular_velocity(Rinit) channel = stars.new_channel_to(cluster_code.particles) channel.copy_attributes(["x", "y", "z", "vx", "vy", "vz"]) system = bridge(verbose=False) system.add_system(cluster_code, (galaxy_code, )) times = quantities.arange(0 | units.Myr, tend, 100 * timestep) for i, t in enumerate(times): print("Time=", t.in_(units.Myr)) system.evolve_model(t, timestep=timestep) x = system.particles.x.value_in(units.kpc) y = system.particles.y.value_in(units.kpc) cluster_code.stop() return x, y
def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): ###BOOKLISTSTART2### Rgal = 1. | units.kpc Mgal = 1.6e10 | units.MSun alpha = 1.2 galaxy_code = GalacticCenterGravityCode(Rgal, Mgal, alpha) cluster_code = make_king_model_cluster(BHTree, N, W0, M, R, parameters=[("epsilon_squared", (0.01 | units.parsec)**2)]) stars = cluster_code.particles.copy() stars.x += Rinit stars.vy = 0.8*galaxy_code.circular_velocity(Rinit) channel = stars.new_channel_to(cluster_code.particles) channel.copy_attributes(["x","y","z","vx","vy","vz"]) system = bridge(verbose=False) system.add_system(cluster_code, (galaxy_code,)) times = quantities.arange(0|units.Myr, tend, timestep) for i,t in enumerate(times): system.evolve_model(t,timestep=timestep) x = system.particles.x.value_in(units.parsec) y = system.particles.y.value_in(units.parsec) cluster_code.stop() ###BOOKLISTSTOP2### return x, y
def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): galaxy_code = MilkyWay_galaxy() converter = nbody_system.nbody_to_si(M, R) cluster_code = drift_without_gravity(convert_nbody=converter) bodies = new_king_model(N, W0, convert_nbody=converter) cluster_code.particles.add_particles(bodies) stars = cluster_code.particles.copy() stars.x += Rinit stars.vy = 0.8*galaxy_code.circular_velocity(Rinit) channel = stars.new_channel_to(cluster_code.particles) channel.copy_attributes(["x","y","z","vx","vy","vz"]) system = bridge(verbose=False) system.add_system(cluster_code, (galaxy_code,)) times = quantities.arange(0|units.Myr, tend, 100*timestep) for i,t in enumerate(times): print "Time=", t.in_(units.Myr) system.evolve_model(t, timestep=timestep) x = system.particles.x.value_in(units.kpc) y = system.particles.y.value_in(units.kpc) cluster_code.stop() return x, y
def evolve_cluster(cluster,mCut,dt,tend,mCluster,rCluster,dump=False,dump_dir='data_dump'): if dump: print('Will be saving data in:'+dump_dir) converter=nbody_system.nbody_to_si(mCluster,rCluster) # Splitting particle according to mass_cut low_mass_stars = cluster.select(lambda m: m < mCut,["mass"]) high_mass_stars = cluster.select(lambda m: m >= mCut,["mass"]) # Sanity checks print('Number of low-mass stars:',low_mass_stars.__len__()) print('Number of high-mass stars:',high_mass_stars.__len__()) # Making models and assigning particles code_tree = BHTree(converter) code_direct = ph4(converter) code_tree.particles.add_particles(low_mass_stars) code_direct.particles.add_particles(high_mass_stars) channel_from_code_tree = code_tree.particles.new_channel_to(low_mass_stars) channel_from_code_direct = code_direct.particles.new_channel_to(high_mass_stars) # Making bridge combined_gravity = bridge() combined_gravity.add_system(code_tree,(code_direct,)) combined_gravity.add_system(code_direct,(code_tree,)) combined_gravity.timestep = dt # Making first snapshot #plot_cluster(low_mass_stars,high_mass_stars,'t=0',save=True) # Evolving the model times = quantities.arange(0|units.Myr, tend, dt) mCut_str = str(mCut.value_in(units.MSun)) for i,t in enumerate(times): print "Time=", t.in_(units.Myr) channel_from_code_tree.copy() channel_from_code_direct.copy() combined_gravity.evolve_model(t, timestep=dt) snapshot_name = 't_'+str(t.in_(units.Myr)) plot_cluster(low_mass_stars,high_mass_stars,snapshot_name,save=True) time = str(t.value_in(units.Myr)) if dump: pkl.dump(low_mass_stars, open(dump_dir+'/t'+time+'_m'+mCut_str+'_low_mass.p', 'wb')) pkl.dump(high_mass_stars, open(dump_dir+'/t'+time+'_m'+mCut_str+'_high_mass.p', 'wb')) code_tree.stop() code_direct.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 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(cluster,mCut,dt,tend,mCluster,rCluster): converter=nbody_system.nbody_to_si(mCluster,rCluster) # Splitting particle according to mass_cut low_mass_stars = cluster.select(lambda m: m < mCut,["mass"]) high_mass_stars = cluster.select(lambda m: m >= mCut,["mass"]) # Sanity checks print('dynamical timescale:', cluster.dynamical_timescale().value_in(units.Myr)) print('Number of low-mass stars:',low_mass_stars.__len__()) print('Number of high-mass stars:',high_mass_stars.__len__()) #plot_cluster(low_mass_stars, high_mass_stars) # Making models and assigning particles code_tree = BHTree(converter) code_direct = ph4(converter) code_tree.particles.add_particles(low_mass_stars) code_direct.particles.add_particles(high_mass_stars) channel_from_code_tree = code_tree.particles.new_channel_to(low_mass_stars) channel_from_code_direct = code_direct.particles.new_channel_to(high_mass_stars) # Making bridge combined_gravity = bridge() combined_gravity.add_system(code_tree,(code_direct,)) combined_gravity.add_system(code_direct,(code_tree,)) combined_gravity.timestep = dt # Evolving the model time_series = [] half_mass_radii = [] core_radii = [] times = quantities.arange(0.|units.Myr, tend, dt) for i,t in enumerate(times): print "Time =", t.in_(units.Myr) channel_from_code_tree.copy() channel_from_code_direct.copy() time_series.append(t.value_in(units.Myr)) pos,coreradius,coredens=cluster.densitycentre_coreradius_coredens(converter) lr,mf=cluster.LagrangianRadii(converter) # outputs are radii, mass fractions half_mass_radii.append(lr[5].value_in(units.parsec)) # 5th argument attributes to half-mass radius core_radii.append(coreradius.value_in(units.parsec)) combined_gravity.evolve_model(t, timestep=dt) code_tree.stop() code_direct.stop() # Plotting results #plot_cluster(low_mass_stars, high_mass_stars) # Plotting radii plot_radii(time_series, half_mass_radii, x_label='time(Myr)', y_label='half-mass radius (pc)') plot_radii(time_series, core_radii, x_label='time(Myr)', y_label='core radius (pc)')
def evolve_cloud_in_galaxy(N, RinitA, RinitB, tend, timestep, MA, RA, MB, RB): Rgal = 1 | units.kpc Mgal = 7.22e9 | units.MSun alpha = 1.2 galaxy_code = GalacticCenterGravityCode(Rgal, Mgal, alpha) cloud_codeA = plummer_model_A(BHTree, N, MA, RA, parameters=[("epsilon_squared", (0.01 | units.parsec)**2)]) cloud_codeB = plummer_model_B(BHTree, N, MB, RB, parameters=[("epsilon_squared", (0.01 | units.parsec)**2)]) cloudA = cloud_codeA.particles.copy() cloudB = cloud_codeA.particles.copy() cloudA.x += RinitA cloudB.x += RinitB cloudA.vy = 0.01 * galaxy_code.circular_velocity(RinitA) cloudB.vy = 0.05 * galaxy_code.circular_velocity(RinitB) channelA = cloudA.new_channel_to(cloud_codeA.particles) channelA.copy_attributes(["x", "y", "z", "vx", "vy", "vz"]) channelB = cloudB.new_channel_to(cloud_codeB.particles) channelB.copy_attributes(["x", "y", "z", "vx", "vy", "vz"]) system = bridge(verbose=False) system.add_system(cloud_codeA, (galaxy_code, )) system.add_system(cloud_codeB, (galaxy_code, )) times = quantities.arange(0 | units.Myr, tend, timestep) for i, t in enumerate(times): system.evolve_model(t, timestep=timestep) x = system.particles.x.value_in(units.parsec) y = system.particles.y.value_in(units.parsec) cloud_codeA.stop() cloud_codeB.stop() return x, y
def evolve_cluster_in_galaxy(N, W0, Rinit, tend, timestep, M, R): Rgal = 1. | units.kpc Mgal = 1.6e10 | units.MSun alpha = 1.2 galaxy_code = GalacticCenterGravityCode(Rgal, Mgal, alpha) cluster_code = make_king_model_cluster(BHTree, N, W0, M, R, parameters=[ ("epsilon_squared", (0.01 | units.parsec)**2) ]) stars = cluster_code.particles.copy() stars.x += Rinit stars.vy = 0.8 * galaxy_code.circular_velocity(Rinit) channel = stars.new_channel_to(cluster_code.particles) channel.copy_attributes(["x", "y", "z", "vx", "vy", "vz"]) system = bridge(verbose=False) system.add_system(cluster_code, (galaxy_code, )) times = numpy.arange(0 | units.Myr, tend, timestep) for i, t in enumerate(times): system.evolve_model(t, timestep=timestep) x = system.particles.x.value_in(units.parsec) y = system.particles.y.value_in(units.parsec) cluster_code.stop() from prepare_figure import single_frame, get_distinct colors = get_distinct(1) f = single_frame('X [pc]', 'Y [pc]') pyplot.xlim(-60, 60) pyplot.ylim(-60, 60) pyplot.scatter(x, y, c=colors[0], s=50, lw=0) pyplot.savefig("Arches")
def evolve_cluster_in_galaxy(N, pot, Rinit, Vinit, tstart, tend, timestep, M, R): galaxy_code = galpy_profile(pot, tgalpy=tstart) cluster_code = setup_cluster(BHTree, N, Mcluster, Rcluster, Rinit, Vinit, parameters=[("epsilon_squared", (0.1 | units.parsec)**2), ("timestep", 0.1 | units.Myr), ("opening_angle", 0.6)]) stars = cluster_code.particles.copy() particles = [] channel_from_stars_to_cluster_code = stars.new_channel_to( cluster_code.particles, attributes=["x", "y", "z", "vx", "vy", "vz"]) channel_from_cluster_code_to_stars = cluster_code.particles.new_channel_to( stars, attributes=["x", "y", "z", "vx", "vy", "vz"]) system = bridge(verbose=False) system.add_system(cluster_code, (galaxy_code, )) system.add_system(galaxy_code) times = quantities.arange(0 | units.Myr, tend - tstart, timestep) for i, t in enumerate(times): if (t.value_in(units.Myr) % 10 < timestep.value_in(units.Myr)): print(t) particles.append([ cluster_code.particles.position, cluster_code.particles.velocity ]) system.evolve_model(t, timestep=timestep) particles.append( [cluster_code.particles.position, cluster_code.particles.velocity]) cluster_code.stop() return particles
def test1(self): convert = nbody_system.nbody_to_si(1.e5 | units.MSun, 1.0 | units.parsec) eps = 2.e-4 | nbody_system.length test_class = PhiGRAPE number_of_particles = 50 stars = new_king_model(number_of_particles, W0=7, convert_nbody=convert) stars.radius = 0.0 | units.RSun cluster = test_class(convert) cluster.parameters.epsilon_squared = eps**2 cluster.particles.add_particles(stars) cluster.synchronize_model() Ep1 = convert.to_nbody(cluster.potential_energy).number Ek1 = convert.to_nbody(cluster.kinetic_energy).number parts = cluster.particles.copy() parts1 = parts.select_array(lambda x: (x > 0 | units.m), ['x']) parts2 = parts.select_array(lambda x: (x < 0 | units.m), ['x']) cluster1 = sys_from_parts(test_class, parts1, convert, eps) cluster2 = sys_from_parts(test_class, parts2, convert, eps) cluster1.synchronize_model() cluster2.synchronize_model() bridgesys = bridge() bridgesys.add_system(cluster1, (cluster2, )) bridgesys.add_system(cluster2, (cluster1, )) Ep2 = convert.to_nbody(bridgesys.potential_energy).number Ek2 = convert.to_nbody(bridgesys.kinetic_energy).number self.assertAlmostEqual(Ek1, Ek2, 12) self.assertAlmostEqual(Ep1, Ep2, 12)
cluster = PhiGRAPE(converter, parameters=[("epsilon_squared", (0.01 | units.parsec)**2)]) # create the external potential of the Galaxy galaxy = Agama(converter, type="Dehnen", gamma=1.8, \ rscale=1000.| units.parsec, mass=1.6e10 | units.MSun, channel_type='sockets') # shift the cluster to an orbit around Galactic center acc,_,_ = galaxy.get_gravity_at_point(0|units.kpc, Rinit, 0|units.kpc, 0|units.kpc) vcirc = (-acc * Rinit)**0.5 print "Vcirc=", vcirc.value_in(units.kms), "km/s" particles.x += Rinit particles.vy += vcirc cluster.particles.add_particles(particles) # set up bridge; cluster is evolved under influence of the galaxy sys = bridge(verbose=False) sys.add_system(cluster, (galaxy,), False) # evolve and make plots times = units.Myr([0.,0.2,0.4,0.6,0.8,1.0,1.2,1.4]) f = pyplot.figure(figsize=(8,16)) for i,t in enumerate(times): sys.evolve_model(t, timestep=timestep) print "Evolved the system to time",t x=sys.particles.x.value_in(units.parsec) y=sys.particles.y.value_in(units.parsec) subplot=f.add_subplot(4,2,i+1) subplot.plot(x,y,'r .')
def validate_bridge(): convert_clu = nbody_system.nbody_to_si( 1.e5 | units.MSun, 1.0 | units.parsec) convert_gal = nbody_system.nbody_to_si( 1.e7 | units.MSun, 10. | units.parsec) def convert_gal_to_clu(x): return convert_clu.to_nbody( convert_gal.to_si(x)) def convert_clu_to_gal(x): return convert_gal.to_nbody( convert_clu.to_si(x)) total_sim_time = convert_clu_to_gal(200 | nbody_system.time) total_sim_time = 200 | nbody_system.time sim_step = 1./128 | nbody_system.time dt_diag = 1./64 | nbody_system.time eps_clu = convert_gal_to_clu(2.e-4 | nbody_system.length) eps_gal = 6.25e-3 | nbody_system.length cluster = sys_from_king(PhiGRAPE, 1000, 7, convert_clu, eps_clu, usegl=False, mode="gpu") galaxy = sys_from_king(Fi, 10000, 9, convert_gal, eps_gal, sim_step, usegl=False) shift_sys( cluster, convert_clu.to_si( convert_gal_to_clu( 2.5 | nbody_system.length ) ), 0 | units.m, 0 | units.m, 0 | units.m/units.s, convert_clu.to_si( convert_gal_to_clu(0.65 | nbody_system.speed) ), 0 | units.m/units.s) if hasattr(cluster, "start_viewer"): cluster.start_viewer() if hasattr(galaxy, "viewer"): galaxy.viewer() clustercopy = copycat(BHTree, cluster, convert_clu) bridgesys = bridge(verbose=False) bridgesys.add_system(galaxy, (clustercopy,), False) bridgesys.add_system(cluster, (galaxy,), True) t = 0. | nbody_system.time if os.path.exists("cluster.hdf"): os.remove("cluster.hdf") storage = store.StoreHDF("cluster.hdf") bridgesys.synchronize_model() Ep0 = bridgesys.potential_energy Ek0 = bridgesys.kinetic_energy Esp0 = cluster.kinetic_energy Esk0 = cluster.potential_energy tc = cluster.model_time tg = galaxy.model_time print(convert_gal.to_nbody(tg)) print(0.) # print Ep0,Ek0 # print Esp0,Esk0 for x in cluster.get_center_of_mass_position(): print(convert_gal.to_nbody(x), end=' ') print() part = bridgesys.particles.copy() part.savepoint(tg) storage.store(part.previous_state()) while(t < total_sim_time): t = t+dt_diag bridgesys.evolve_model(convert_gal.to_si(t), timestep=convert_gal.to_si(sim_step)) bridgesys.synchronize_model() Ep = bridgesys.potential_energy Ek = bridgesys.kinetic_energy Esp = cluster.kinetic_energy Esk = cluster.potential_energy tc = cluster.model_time tg = galaxy.model_time print(convert_gal.to_nbody(tg)) print((Ep+Ek-Ep0-Ek0)/(Ep0+Ek0)) # print (Esp+Esk-Esp0-Esk0)/(Esp0+Esk0) for x in cluster.get_center_of_mass_position(): print(convert_gal.to_nbody(x), end=' ') print() part = bridgesys.particles.copy() part.savepoint(tg) storage.store(part.previous_state())
# 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) # Bridge couples a number of gravitational codes to another code. In this case, # the cluster evolves under its own gravity and under that of the galaxy. # The galaxy is represented by just a smooth potential, and its forces are updated # every timestep dt. bridge_gravity = bridge.bridge() bridge_gravity.add_system( cluster_gravity, (galaxy_gravity, )) # The codes in the bracket (1 in this case) influence the first code # Plot initial conditions plot_cluster(bridge_gravity.particles.x.value_in(al.units.kpc), bridge_gravity.particles.y.value_in(al.units.kpc), num=1) bridge_gravity.evolve_model(t_end, timestep=dt) # Plot outcome plot_cluster(bridge_gravity.particles.x.value_in(al.units.kpc), bridge_gravity.particles.y.value_in(al.units.kpc),
def validate_bridge(): convert_clu = nbody_system.nbody_to_si( 1.e5 | units.MSun, 1.0 | units.parsec) convert_gal = nbody_system.nbody_to_si( 1.e7 | units.MSun, 10. | units.parsec) def convert_gal_to_clu(x): return convert_clu.to_nbody( convert_gal.to_si(x)) def convert_clu_to_gal(x): return convert_gal.to_nbody( convert_clu.to_si(x)) total_sim_time = convert_clu_to_gal(200 | nbody_system.time) total_sim_time = 200 | nbody_system.time sim_step = 1./128 | nbody_system.time dt_diag = 1./64 | nbody_system.time eps_clu = convert_gal_to_clu(2.e-4 | nbody_system.length) eps_gal = 6.25e-3 | nbody_system.length cluster = sys_from_king(PhiGRAPE, 1000, 7, convert_clu, eps_clu, usegl=False, mode="gpu") galaxy = sys_from_king(Fi, 10000, 9, convert_gal, eps_gal, sim_step, usegl=False) shift_sys( cluster, convert_clu.to_si( convert_gal_to_clu( 2.5 | nbody_system.length ) ), 0 | units.m, 0 | units.m, 0 | units.m/units.s, convert_clu.to_si( convert_gal_to_clu(0.65 | nbody_system.speed) ), 0 | units.m/units.s) if hasattr(cluster, "start_viewer"): cluster.start_viewer() if hasattr(galaxy, "viewer"): galaxy.viewer() clustercopy = copycat(BHTree, cluster, convert_clu) bridgesys = bridge(verbose=False) bridgesys.add_system(galaxy, (clustercopy,), False) bridgesys.add_system(cluster, (galaxy,), True) t = 0. | nbody_system.time if os.path.exists("cluster.hdf"): os.remove("cluster.hdf") storage = store.StoreHDF("cluster.hdf") bridgesys.synchronize_model() Ep0 = bridgesys.potential_energy Ek0 = bridgesys.kinetic_energy Esp0 = cluster.kinetic_energy Esk0 = cluster.potential_energy tc = cluster.model_time tg = galaxy.model_time print convert_gal.to_nbody(tg) print 0. # print Ep0,Ek0 # print Esp0,Esk0 for x in cluster.get_center_of_mass_position(): print convert_gal.to_nbody(x), print part = bridgesys.particles.copy() part.savepoint(tg) storage.store(part.previous_state()) while(t < total_sim_time): t = t+dt_diag bridgesys.evolve_model(convert_gal.to_si(t), timestep=convert_gal.to_si(sim_step)) bridgesys.synchronize_model() Ep = bridgesys.potential_energy Ek = bridgesys.kinetic_energy Esp = cluster.kinetic_energy Esk = cluster.potential_energy tc = cluster.model_time tg = galaxy.model_time print convert_gal.to_nbody(tg) print (Ep+Ek-Ep0-Ek0)/(Ep0+Ek0) # print (Esp+Esk-Esp0-Esk0)/(Esp0+Esk0) for x in cluster.get_center_of_mass_position(): print convert_gal.to_nbody(x), print part = bridgesys.particles.copy() part.savepoint(tg) storage.store(part.previous_state())
# create the external potential of the Galaxy galaxy = Agama(converter, type="Dehnen", gamma=1.8, \ rscale=1000.| units.parsec, mass=1.6e10 | units.MSun, channel_type='sockets') # shift the cluster to an orbit around Galactic center acc, _, _ = galaxy.get_gravity_at_point(0 | units.kpc, Rinit, 0 | units.kpc, 0 | units.kpc) vcirc = (-acc * Rinit)**0.5 print("Vcirc=%f km/s" % vcirc.value_in(units.kms)) particles.x += Rinit particles.vy += vcirc cluster.particles.add_particles(particles) # set up bridge; cluster is evolved under influence of the galaxy sys = bridge(verbose=False) sys.add_system(cluster, (galaxy, ), False) # evolve and make plots times = units.Myr([0., 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4]) f = pyplot.figure(figsize=(16, 8)) for i, t in enumerate(times): sys.evolve_model(t, timestep=timestep) print("Evolved the system to time %f" % t) x = sys.particles.x.value_in(units.parsec) y = sys.particles.y.value_in(units.parsec) subplot = f.add_subplot(2, 4, i + 1) subplot.plot(x, y, 'r .')
def evolve_cluster(cluster,mCut,dt,tend,mCluster,rCluster): print 'Start evolving the cluster with mCut =', mCut # timing t1 = time() converter=nbody_system.nbody_to_si(mCluster,rCluster) # split particle according to mass_cut low_mass_stars = cluster.select(lambda m: m < mCut,["mass"]) high_mass_stars = cluster.select(lambda m: m >= mCut,["mass"]) print 'Number of low-mass stars:', low_mass_stars.__len__() print 'Number of high-mass stars:', high_mass_stars.__len__() # make models and assigning particles code_tree = BHTree(converter) code_direct = ph4(converter) code_tree.particles.add_particles(low_mass_stars) code_direct.particles.add_particles(high_mass_stars) channel_from_code_tree = code_tree.particles.new_channel_to(low_mass_stars) channel_from_code_direct = code_direct.particles.new_channel_to(high_mass_stars) # make bridge combined_gravity = bridge() combined_gravity.add_system(code_tree,(code_direct,)) combined_gravity.add_system(code_direct,(code_tree,)) combined_gravity.timestep = dt # the initial total energy E0 = combined_gravity.potential_energy + combined_gravity.kinetic_energy # evolve the model times = quantities.arange(0.|units.Myr, tend+dt, dt) n_step = len(times) dE = np.empty(n_step) Qs = np.empty(n_step) for i,t in enumerate(times): if t.value_in(units.Myr)%1 == 0: print "Time =", t.in_(units.Myr) channel_from_code_tree.copy() channel_from_code_direct.copy() combined_gravity.evolve_model(t, timestep=dt) U = cluster.potential_energy() T = cluster.kinetic_energy() Et = U + T dE[i] = np.abs((E0-Et)/E0) Qs[i] = -T / U code_tree.stop() code_direct.stop() t2 = time() print 'Finished. Time cost: %.2f s'%(t2-t1) output_filename = 'ee_q_%.1f.txt'%mCut.value_in(units.MSun) with open(output_filename, 'w'): np.savetxt(output_filename, np.append(dE,Qs).reshape(2,n_step).transpose()) print 'Saved the data of energy error and virial ratios in', output_filename # plot energy plot_energy_error(times.value_in(units.Myr), dE, Qs, mCut) return dE[-1], Qs[-1], t2-t1