def run_only_gravity(bodies, t_end): Mtot_init = bodies.mass.sum() time = [] | units.Myr Lr25 = [] |units.parsec Lr50 = [] |units.parsec Lr75 = [] |units.parsec converter = nbody_system.nbody_to_si(bodies.mass.sum(), 1|units.parsec) gravity = ph4(converter) gravity.particles.add_particles(bodies) channel_from_gravity = gravity.particles.new_channel_to(bodies) dt = 0.1|units.Myr while True: time.append(gravity.model_time) Lr25.append(LagrangianRadii(gravity.particles)[5]) Lr50.append(LagrangianRadii(gravity.particles)[6]) Lr75.append(LagrangianRadii(gravity.particles)[7]) gravity.evolve_model(time[-1]+dt) channel_from_gravity.copy_attributes(["x", "y", "z", "vx", "vy", "vz"]) print "G: T=", time[-1], "M=", bodies.mass.sum(), \ "(dM[SE]=", bodies.mass.sum()/Mtot_init, ")" if time[-1] >= t_end: break gravity.stop() return time, Lr25, Lr50, Lr75
def run_event_driven_gravity_and_stellar(bodies, t_end): Mtot_init = bodies.mass.sum() time = [] | units.Myr Lr25 = [] |units.parsec Lr50 = [] |units.parsec Lr75 = [] |units.parsec converter = nbody_system.nbody_to_si(bodies.mass.sum(), 1|units.parsec) gravity = ph4(converter, number_of_workers=2) gravity.particles.add_particles(bodies) stellar = SSE() stellar.parameters.metallicity = 0.02 stellar.particles.add_particle(bodies) channel_from_gravity = gravity.particles.new_channel_to( bodies, attributes=["mass", "x", "y", "z", "vx", "vy", "vz"]) channel_from_stellar = stellar.particles.new_channel_to( bodies, attributes=["mass"]) channel_from_stellar_to_gravity = stellar.particles.new_channel_to( gravity.particles, attributes=["mass"]) while True: dt = 0.5*stellar.particles.time_step.min() stellar.evolve_model(gravity.model_time + dt/2) channel_from_stellar_to_gravity.copy() dt = 0.5*stellar.particles.time_step.min() gravity.evolve_model(stellar.model_time + dt) channel_from_gravity.copy() stellar.evolve_model(gravity.model_time) channel_from_stellar.copy() time.append(gravity.model_time) Lr25.append(LagrangianRadii(gravity.particles)[5]) Lr50.append(LagrangianRadii(gravity.particles)[6]) Lr75.append(LagrangianRadii(gravity.particles)[7]) stellar.evolve_model() print "GSE: T=", time[-1], "M=", bodies.mass.sum(), \ "(dM[SE]=", bodies.mass.sum()/Mtot_init, ")" if time[-1] >= t_end: break gravity.stop() stellar.stop() return time, Lr25, Lr50, Lr75
def run_sequential_gravity_and_stellar(bodies, t_end): Mtot_init = bodies.mass.sum() time = [] | units.Myr Lr25 = [] | units.parsec Lr50 = [] | units.parsec Lr75 = [] | units.parsec converter = nbody_system.nbody_to_si(bodies.mass.sum(), 1 | units.parsec) gravity = ph4(converter) gravity.particles.add_particles(bodies) channel_from_gravity = gravity.particles.new_channel_to(bodies) channel_to_gravity = bodies.new_channel_to(gravity.particles) stellar = SSE() stellar.parameters.metallicity = 0.02 stellar.particles.add_particle(bodies) channel_from_stellar = stellar.particles.new_channel_to(bodies) dt = 0.1 | units.Myr while True: stellar.evolve_model(gravity.model_time + dt / 2) channel_from_stellar.copy_attributes(["mass"]) channel_to_gravity.copy_attributes(["mass"]) gravity.evolve_model(gravity.model_time + dt) channel_from_gravity.copy_attributes(["x", "y", "z", "vx", "vy", "vz"]) stellar.evolve_model(gravity.model_time + dt) channel_from_stellar.copy_attributes(["mass"]) channel_to_gravity.copy_attributes(["mass"]) time.append(gravity.model_time) Lr25.append(LagrangianRadii(gravity.particles)[5]) Lr50.append(LagrangianRadii(gravity.particles)[6]) Lr75.append(LagrangianRadii(gravity.particles)[7]) print("GS: T=", time[-1], "M=", bodies.mass.sum(), \ "(dM[SE]=", bodies.mass.sum()/Mtot_init, ")") if time[-1] >= t_end: break gravity.stop() stellar.stop() return time, Lr25, Lr50, Lr75
def get_zones(stars): stars.move_to_center() from amuse.ext.LagrangianRadii import LagrangianRadii massf = [0.25, 0.50, 0.75, 1.0] lagrad = LagrangianRadii(stars, massf = massf) for zi in lagrad: print("R=", zi.in_(units.RSun)) center_of_mass = [0,0,0] | units.RSun #initial_stars.center_of_mass() zoneA = stars.select(lambda r: (center_of_mass-r).length()<lagrad[0], ["position"]) zoneB = stars.select(lambda r: (center_of_mass-r).length()<lagrad[1], ["position"]) zoneC = stars.select(lambda r: (center_of_mass-r).length()<lagrad[2], ["position"]) zoneC -= zoneB zoneB -= zoneA zoneD = stars.select(lambda r: (center_of_mass-r).length()>lagrad[2], ["position"]) print("N=", len(zoneA), len(zoneB), len(zoneC), len(zoneD)) return (zoneA, zoneB, zoneC, zoneD)
def main(N=10, W0=7.0, t_end=10, nsteps=10, filename="gravity_stellar.hdf5", Mmax=100, Qvir=0.5): t_end = t_end | nbody_system.time dt = t_end / float(nsteps) bodies = new_king_model(N, W0) masses = new_powerlaw_mass_distribution(N, mass_min=0.1 | units.MSun, mass_max=10 | units.MSun, alpha=-2.35) masses /= masses.sum() bodies.mass = masses | nbody_system.mass bodies.radius = 0.001 | nbody_system.length Mtot_init = 1 | nbody_system.mass gravity = ph4(number_of_workers=4) gravity.particles.add_particles(bodies) bodies.scale_to_standard(virial_ratio=Qvir) channel_from_gd_to_framework = gravity.particles.new_channel_to(bodies) stopping_condition = gravity.stopping_conditions.collision_detection stopping_condition.enable() Etot_init = gravity.kinetic_energy + gravity.potential_energy Etot_prev = Etot_init time = 0.0 | t_end.unit Nenc = 0 dEk_enc = zero dEp_enc = zero t = [] rcore = [] MassFraction = [0.005, 0.01, 0.02, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0] r50 = [] r90 = [] tcoll = [] rcoll = [] mcoll = [] while time < t_end: RL = LagrangianRadii(gravity.particles, massf=MassFraction) pos,coreradius,coredens = \ gravity.particles.densitycentre_coreradius_coredens() print("Cluster at time=", time, "core_radius=", coreradius, \ "L_radii =", end=' ') for rl in RL: print(rl.number, " ", end=' ') print("length") t.append(time.number) rcore.append(coreradius.number) r50.append(RL[6].number) r90.append(RL[8].number) gravity.evolve_model(time + dt) time = gravity.model_time if stopping_condition.is_set(): Ek_enc = gravity.kinetic_energy Ep_enc = gravity.potential_energy print("At time=", time, "number of encounters=", \ len(stopping_condition.particles(0))) for ci in range(len(stopping_condition.particles(0))): particles_in_encounter = Particles(particles=[ stopping_condition.particles(0)[ci], stopping_condition.particles(1)[ci] ]) particles_in_encounter = \ particles_in_encounter.get_intersecting_subset_in(bodies) new_particle = merge_two_stars(bodies, particles_in_encounter, gravity.model_time) bodies.synchronize_to(gravity.particles) Nenc += 1 RL = LagrangianRadii(gravity.particles, massf=MassFraction) print("Resolve encounter number", Nenc) pos,coreradius,coredens = \ gravity.particles.densitycentre_coreradius_coredens() print("Collision at time=", time, new_particle.mass.sum(), \ new_particle.position.length(), "Nstars= ", len(bodies), \ "Ncoll=", Nenc, "core_radius=", coreradius, "L_radii=", end=' ') for rl in RL: print(rl.number, " ", end=' ') print("length") pos = new_particle[0].position.number rc = math.sqrt(numpy.sum(pos**2)) mc = new_particle[0].mass.number tcoll.append(time.number) rcoll.append(rc) mcoll.append(mc) dEk_enc += Ek_enc - gravity.kinetic_energy dEp_enc += Ep_enc - gravity.potential_energy bodies.move_to_center() channel_from_gd_to_framework.copy() Ekin = gravity.kinetic_energy Epot = gravity.potential_energy Etot = Ekin + Epot dE = Etot_prev - Etot Mtot = bodies.mass.sum() print("T=", time, end=' ') print("M=", Mtot, "(dM[SE]=", Mtot / Mtot_init, ")", end=' ') print("E=", Etot, "Q=", Ekin / Epot, end=' ') print("dE=", (Etot_init - Etot) / Etot, "ddE=", (Etot_prev - Etot) / Etot, end=' ') print("dE(enc)=", dEk_enc, dEp_enc) Etot_init -= dE Etot_prev = Etot gravity.stop() pyplot.figure(figsize=(8, 6)) pyplot.plot(t, rcore, 'b') pyplot.plot(t, r50, 'r') pyplot.plot(t, r90, 'y') size = numpy.array(mcoll) mmin = numpy.amin(size) size = 2 + 15 * numpy.log10(size / mmin) pyplot.scatter(tcoll, rcoll, c='g', s=size) pyplot.xlabel('t [N-body]') pyplot.ylabel('R [N-body]') pyplot.yscale('log') save_file = 'gravity_collision.png' pyplot.savefig(save_file) print('\nSaved figure in file', save_file, '\n') pyplot.show()
def main(N=10, W0=7.0, t_end=10, nsteps=10, filename="gravity_stellar.hdf5", Mmax=100, Qvir=0.5): t_end = t_end | nbody_system.time dt = t_end / float(nsteps) bodies = new_king_model(N, W0) masses = new_powerlaw_mass_distribution(N, mass_min=0.1 | units.MSun, mass_max=10 | units.MSun, alpha=-2.35) masses /= masses.sum() bodies.mass = masses | nbody_system.mass """ alpha1 = 2.35 + 1 factor = (pow(Mmax, alpha1) - 1.0 ) masses = (pow(1.0 + (factor * numpy.random.random(N)), 1.0 / alpha1)) masses = masses / masses.sum() Mtot_init = masses.sum() """ bodies.radius = 0.001 | nbody_system.length Mtot_init = 1 | nbody_system.mass gravity = ph4(number_of_workers=4) # gravity.parameters.timestep_parameter = 0.01 gravity.particles.add_particles(bodies) bodies.scale_to_standard(virial_ratio=Qvir) channel_from_gd_to_framework = gravity.particles.new_channel_to(bodies) stopping_condition = gravity.stopping_conditions.collision_detection stopping_condition.enable() Etot_init = gravity.kinetic_energy + gravity.potential_energy Etot_prev = Etot_init time = 0.0 | t_end.unit Nenc = 0 dEk_enc = zero dEp_enc = zero while time < t_end: RL = LagrangianRadii(gravity.particles) pos, coreradius, coredens = gravity.particles.densitycentre_coreradius_coredens( ) print "Cluster at time=", time, "core_radius=", coreradius, "L_radius=", for rl in RL: print rl.number, " ", print "length" gravity.evolve_model(time + dt) time = gravity.model_time if stopping_condition.is_set(): Ek_enc = gravity.kinetic_energy Ep_enc = gravity.potential_energy print "At time=", time, "number of encounters=", len( stopping_condition.particles(0)) for ci in range(len(stopping_condition.particles(0))): particles_in_encounter = Particles(particles=[ stopping_condition.particles(0)[ci], stopping_condition.particles(1)[ci] ]) particles_in_encounter = particles_in_encounter.get_intersecting_subset_in( bodies) new_particle = merge_two_stars(bodies, particles_in_encounter, gravity.model_time) bodies.synchronize_to(gravity.particles) Nenc += 1 RL = LagrangianRadii(gravity.particles) print "Resolve encounter Number:", Nenc pos, coreradius, coredens = gravity.particles.densitycentre_coreradius_coredens( ) print "Collision at time=", time, new_particle.mass.sum( ), new_particle.position.length(), "Nstars= ", len( bodies ), "Ncoll=", Nenc, "core_radius=", coreradius, "L_radius=", for rl in RL: print rl.number, " ", print "length" dEk_enc += Ek_enc - gravity.kinetic_energy dEp_enc += Ep_enc - gravity.potential_energy channel_from_gd_to_framework.copy() # write_set_to_file(bodies.savepoint(time), filename, 'hdf5') Ekin = gravity.kinetic_energy Epot = gravity.potential_energy Etot = Ekin + Epot dE = Etot_prev - Etot Mtot = bodies.mass.sum() print "T=", time, print "M=", Mtot, "(dM[SE]=", Mtot / Mtot_init, ")", print "E= ", Etot, "Q= ", Ekin / Epot, print "dE=", (Etot_init - Etot) / Etot, "ddE=", (Etot_prev - Etot) / Etot, print "dE(enc)=", dEk_enc, dEp_enc Etot_init -= dE Etot_prev = Etot gravity.stop()
dEk_enc = zero dEp_enc = zero t = [] MassFraction = [0.005, 0.01, 0.02, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0] r10 = [] r25 = [] r50 = [] r75 = [] tcoll = [] rcoll = [] mcoll = [] while time < t_end: RL = LagrangianRadii(gravity.particles, massf=MassFraction) pos,coreradius,coredens = \ gravity.particles.densitycentre_coreradius_coredens() print "Cluster at time=", time, "core_radius=", coreradius, \ "L_radii =", for rl in RL: print rl.number, " ", print "length" t.append(time.number) r10.append(RL[4].number) r25.append(RL[5].number) r50.append(RL[6].number) r75.append(RL[7].number) gravity.evolve_model(time + dt)