def evolve_disk_flyby(bodies, gravity, t_end, n_steps, converter, snap_dir, file_out): bodies = ParticlesSuperset([stars, planetesimals]) channel_from_gr_to_framework = gravity.particles.new_channel_to(bodies) rm_file(file_out) Etot_init = gravity.kinetic_energy + gravity.potential_energy Etot = Etot_init dt = t_end / float(n_steps) time = 0.0 | units.yr stdout = (file_out.split('.'))[0] stdout += '.txt' f = open(snap_dir + "/" + stdout, 'a') print " ** evolving: t_end = ", t_end, ", dt = ", dt.in_(units.yr) print " \t\t", "time", "\t\t", "dE" while time <= t_end: gravity.evolve_model(time) channel_from_gr_to_framework.copy() bodies.collection_attributes.timestamp = time Ekin = gravity.kinetic_energy Epot = gravity.potential_energy Etot = Ekin + Epot dE = Etot_init - Etot nb_E = converter.to_nbody(Etot) #nb_J = converter.nbody_length ** 2 * units.nbody_mass * units.nbody_time ** -2 # not supposed to work # A formatted string would work better than tabs. (Tabs never work) line = " \t" + str(time.value_in( units.yr)) + "\t" + str(nb_E) + "\t" + str(dE / Etot_init) print line f.write(line + "\n") # Write coordinates in Center of Mass frame # Move Stars to CoM (initially in CoM) #### The stars are already in CoM coordinates #### # Move planetesimals to CoM (initially in CoM) #### The stars are already in CoM coordinates #### (Does this mean the other method is wrong?) write_set_to_file(bodies, snap_dir + "/" + file_out, "hdf5") time += dt gravity.stop() return
def evolve_disk_flyby(bodies, gravity, t_end, n_steps, converter, snap_dir, file_out): bodies = ParticlesSuperset([stars, planetesimals]) channel_from_gr_to_framework = gravity.particles.new_channel_to(bodies) rm_file(file_out) Etot_init = gravity.kinetic_energy + gravity.potential_energy Etot = Etot_init dt = t_end / float(n_steps) time = 0.0 | units.yr stdout = (file_out.split('.'))[0] stdout += '.txt' f = open(snap_dir + "/" + stdout, 'a') print " ** evolving: t_end = ", t_end, ", dt = ", dt.in_(units.yr) print " \t\t", "time", "\t\t", "dE" while time<=t_end: gravity.evolve_model(time) channel_from_gr_to_framework.copy() bodies.collection_attributes.timestamp = time Ekin = gravity.kinetic_energy Epot = gravity.potential_energy Etot = Ekin + Epot dE = Etot_init-Etot nb_E = converter.to_nbody(Etot) #nb_J = converter.nbody_length ** 2 * units.nbody_mass * units.nbody_time ** -2 # not supposed to work # A formatted string would work better than tabs. (Tabs never work) line = " \t" + str(time.value_in(units.yr)) + "\t" + str(nb_E) + "\t" + str(dE/Etot_init) print line f.write(line + "\n") # Write coordinates in Center of Mass frame # Move Stars to CoM (initially in CoM) #### The stars are already in CoM coordinates #### # Move planetesimals to CoM (initially in CoM) #### The stars are already in CoM coordinates #### (Does this mean the other method is wrong?) write_set_to_file(bodies, snap_dir + "/" + file_out, "hdf5") time += dt gravity.stop() return
def update_plot(self, time, code): time = time.value_in(nbody_system.time), sum_energy = code.kinetic_energy + code.potential_energy - self.code.multiples_energy_correction energy = sum_energy.value_in(nbody_system.energy) coreradius = self.star_code.particles.virial_radius().value_in(self.rscale.to_unit()) if self.line is None: pylab.ion() pylab.subplot(1,2,1) self.line = pylab.plot([time], [energy])[0] pylab.xlim(0, self.endtime.value_in(nbody_system.time)) pylab.ylim(energy * 0.8, energy * 1.2) pylab.subplot(1,2,2) self.line2 = pylab.plot([time], [coreradius])[0] #self.line2 = pylab.plot([time], [kicke])[0] pylab.xlim(0, self.endtime.value_in(nbody_system.time)) pylab.ylim(0,3) #pylab.ylim(-0.1, 0.1) else: xdata = self.line.get_xdata() ydata = self.line.get_ydata() xdata = numpy.concatenate( (xdata, time) ) ydata = numpy.concatenate( (ydata, [energy]) ) self.line.set_xdata(xdata) self.line.set_ydata(ydata) xdata = self.line2.get_xdata() ydata = self.line2.get_ydata() xdata = numpy.concatenate( (xdata, time) ) #ydata = numpy.concatenate( (ydata, [kicke]) ) ydata = numpy.concatenate( (ydata, [coreradius]) ) self.line2.set_xdata(xdata) self.line2.set_ydata(ydata) pylab.draw()
def evolve_double_star(Mprim, Msec, a, e, end_time, n_steps): q = Msec/Mprim double_star, stars = create_double_star(Mprim, Msec, a, e) time = 0|units.Myr time_step = end_time/n_steps code = SeBa() code.particles.add_particles(stars) code.binaries.add_particles(double_star) channel_from_code_to_model_for_binaries = code.binaries.new_channel_to(double_star) channel_from_code_to_model_for_stars = code.particles.new_channel_to(stars) t = [] a = [] e = [] while time < end_time: time += time_step code.evolve_model(time) channel_from_code_to_model_for_stars.copy() channel_from_code_to_model_for_binaries.copy() t.append(time.value_in(units.Myr)) a.append(double_star[0].semi_major_axis.value_in(units.RSun)) e.append(double_star[0].eccentricity) code.stop() fig = pyplot.figure(figsize = (8,8)) fta = fig.add_subplot(2,1,1) # fte = fig.add_subplot(2,2,1) pyplot.title('Binary evolution', fontsize=12) fta.plot(t, a) pyplot.xlabel('time [Myr]') pyplot.ylabel('semi major axis (AU)') # fta.plot(t, e) #pyplot.ylabel('eccentricity') pyplot.show()
def evolve_double_star(Mprim, Msec, a, e, end_time, n_steps): q = Msec / Mprim double_star, stars = create_double_star(Mprim, Msec, a, e) time = 0 | units.Myr time_step = end_time / n_steps code = SeBa() code.particles.add_particles(stars) code.binaries.add_particles(double_star) channel_from_code_to_model_for_binaries = code.binaries.new_channel_to(double_star) channel_from_code_to_model_for_stars = code.particles.new_channel_to(stars) t = [] a = [] e = [] while time < end_time: time += time_step code.evolve_model(time) channel_from_code_to_model_for_stars.copy() channel_from_code_to_model_for_binaries.copy() t.append(time.value_in(units.Myr)) a.append(double_star[0].semi_major_axis.value_in(units.RSun)) e.append(double_star[0].eccentricity) code.stop() fig = pyplot.figure(figsize=(8, 8)) fta = fig.add_subplot(2, 1, 1) # fte = fig.add_subplot(2,2,1) pyplot.title("Binary evolution", fontsize=12) fta.plot(t, a) pyplot.xlabel("time [Myr]") pyplot.ylabel("semi major axis (AU)") # fta.plot(t, e) # pyplot.ylabel('eccentricity') pyplot.show()
def evolve_galaxy( end_time, time_step, gal_fraction=1e-6, binfraction=1.0, Mmin=0.125, Mmax=125., a_min=0.01, a_max=1e6, e_min=0, e_max=1., ce=0.5, Mmax_NS=2.1, plot = True ): #initialize some variables for monitoring date = str(datetime.datetime.now()) stellar_galaxy_mass = 0.0 number_of_binaries = 0 number_of_systems = 0 number_of_stars = 0 time = 0.0 * end_time try: session_id = session.query(Simulation.id).all()[-1][0] + 1 except: session_id = 0 code = SeBa() #link code #code = BSE() #configure parameters code.parameters.common_envelope_efficiency = ce code.parameters.maximum_neutron_star_mass = Mmax_NS | units.MSun pyplot.ion() pyplot.show() while time < end_time: time += time_step mass_in_timestep = star_formation(15,7e9,time.value_in(units.yr)) - star_formation(15,7e9,(time-time_step).value_in(units.yr)) mass_in_timestep = mass_in_timestep*gal_fraction N = stars_for_given_Mtot(mass_in_timestep,Mmin,Mmax) #N = 4 #for de-bugging number_of_stars += N number_of_binaries = np.int(number_of_stars*binfraction/2) number_of_systems = np.int(number_of_stars*(1-binfraction) + number_of_binaries) stellar_galaxy_mass += mass_in_timestep print print print print "Total stellar mass of the Galaxy [in 1e10 Msun]:", stellar_galaxy_mass/gal_fraction/1e10 print "Age of the disk:",time.value_in(units.Gyr), "Gyr" print "Number of stellar systems:", number_of_systems, "of which", number_of_binaries ,"are binaries" print "Injected", N, "stars in the last timestep. Total number of stars:", number_of_stars print print print if (time == time_step): binaries, stars = generate_initial_population_grid(Mmin, Mmax, N, gal_fraction, binfraction, a_min, a_max, e_min , e_max,binfraction) code.particles.add_particles(stars) code.binaries.add_particles(binaries) else: binaries2, stars2 = generate_initial_population_grid(0.125, 125,N, 1e-5, 1, 0.1, 1e6, 0.0 , 1.0, binfraction) stars.add_particles(stars2) binaries.add_particles(binaries2) code.particles.add_particles(stars2) code.binaries.add_particles(binaries2) channel_from_code_to_model_for_binaries = code.binaries.new_channel_to(binaries) channel_from_code_to_model_for_stars = code.particles.new_channel_to(stars) code.evolve_model(time) channel_from_code_to_model_for_stars.copy() channel_from_code_to_model_for_binaries.copy() if plot: if (time > time_step): make_hr_diagram3(stars) pyplot.clf() #add_sim_info_to_database(end_time=end_time,time_step=time_step, # gal_fraction=gal_fraction,binfraction=binfraction,Mmin=Mmin, # Mmax=Mmax,a_min=a_min,a_max=a_max,date=date,code='BSE',ce=ce,MNSmax = Mmax_NS) #add_stars_to_database(binaries,sim_id = session_id) write_set_to_file(code.particles,'stellar_properties.bse.hdf5',format='hdf5') write_set_to_file(binaries,'binary_properties.bse.hdf5',format='hdf5')