def make_plots(all_particles, disk_only, i=0): for j, particles in enumerate([all_particles, disk_only]): if HAS_PYNBODY: temp_particles = particles.copy() temp_particles.u = 1 | units.ms**2 temp = Gadget2() temp.gas_particles.add_particles(temp_particles) temp.commit_particles() pynbody_column_density_plot( temp.gas_particles, width=300 | units.kpc, units='m_p cm^-2', vmin=17, vmax=23) temp.stop() else: native_plot.gca().set_aspect("equal") native_plot.xlim(-150, 150) native_plot.ylim(-150, 150) plot(particles.x.as_quantity_in(units.kpc), particles.y.as_quantity_in(units.kpc), 'r.') native_plot.savefig( os.path.join( "plots", "plot_galaxy_merger_{0}_{1:=04}.png".format( "disk" if j else "total", i) ) ) native_plot.close()
def make_plots(all_particles, disk_only, i=0): for j, particles in enumerate([all_particles, disk_only]): if HAS_PYNBODY: temp_particles = particles.copy() temp_particles.u = 1 | units.ms**2 temp = Gadget2() temp.gas_particles.add_particles(temp_particles) temp.commit_particles() pynbody_column_density_plot(temp.gas_particles, width=300 | units.kpc, units='m_p cm^-2', vmin=17, vmax=23) temp.stop() else: native_plot.gca().set_aspect("equal") native_plot.xlim(-150, 150) native_plot.ylim(-150, 150) plot(particles.x.as_quantity_in(units.kpc), particles.y.as_quantity_in(units.kpc), 'r.') native_plot.savefig( os.path.join( "plots", "plot_galaxy_merger_{0}_{1:=04}.png".format( "disk" if j else "total", i))) native_plot.close()
def density_plot(coupled_system, i_step): if not HAS_PYNBODY: return figname = os.path.join("plots", "hydro_giant{0:=04}.png".format(i_step)) print " - Hydroplot saved to: ", figname pynbody_column_density_plot(coupled_system.gas_particles, width=3 | units.AU, vmin=26, vmax=33) scatter(coupled_system.dm_particles.x, coupled_system.dm_particles.y, c="w") pyplot.savefig(figname) pyplot.close()
def slowtest1(self): stellar_evolution = self.new_instance(MESA) stellar_evolution.particles.add_particles(Particles(2, mass=[1.0, 5.0]|units.MSun)) stellar_evolution.evolve_model(10.0 | units.Myr) initial_separation = stellar_evolution.particles.radius.sum() sph_particles_1 = convert_stellar_model_to_SPH( stellar_evolution.particles[0], 200, seed=12345 ).gas_particles sph_particles_2 = convert_stellar_model_to_SPH( stellar_evolution.particles[1], 1000, seed=12345 ).gas_particles stellar_evolution.stop() initial_speed = 10.0 | units.km / units.s sph_particles_2.x += initial_separation sph_particles_1.vx += initial_speed all_sph_particles = ParticlesSuperset([sph_particles_1, sph_particles_2]) all_sph_particles.move_to_center() t_end = 4.0e3 | units.s print "Evolving to:", t_end n_steps = 4 unit_system_converter = ConvertBetweenGenericAndSiUnits(1.0 | units.RSun, 1.0 | units.MSun, t_end) hydro_code = Gadget2(unit_system_converter) hydro_code.gas_particles.add_particles(all_sph_particles) pyplot.ion() for time in [i*t_end/n_steps for i in range(1, n_steps+1)]: hydro_code.evolve_model(time) pyplot.close('all') pyplot.figure() pynbody_column_density_plot(hydro_code.gas_particles, width=10|units.RSun) pyplot.draw() pyplot.figure() loglog(hydro_code.gas_particles.position.lengths_squared(), hydro_code.gas_particles.pressure, 'bo') pyplot.draw() hydro_code.stop() sleep(3) pyplot.ioff()
def slowtest1(self): stellar_evolution = self.new_instance(MESA) stellar_evolution.particles.add_particles( Particles(2, mass=[1.0, 5.0] | units.MSun)) stellar_evolution.evolve_model(10.0 | units.Myr) initial_separation = stellar_evolution.particles.radius.sum() sph_particles_1 = convert_stellar_model_to_SPH( stellar_evolution.particles[0], 200, seed=12345).gas_particles sph_particles_2 = convert_stellar_model_to_SPH( stellar_evolution.particles[1], 1000, seed=12345).gas_particles stellar_evolution.stop() initial_speed = 10.0 | units.km / units.s sph_particles_2.x += initial_separation sph_particles_1.vx += initial_speed all_sph_particles = ParticlesSuperset( [sph_particles_1, sph_particles_2]) all_sph_particles.move_to_center() t_end = 4.0e3 | units.s print "Evolving to:", t_end n_steps = 4 unit_system_converter = ConvertBetweenGenericAndSiUnits( 1.0 | units.RSun, 1.0 | units.MSun, t_end) hydro_code = Gadget2(unit_system_converter) hydro_code.gas_particles.add_particles(all_sph_particles) pyplot.ion() for time in [i * t_end / n_steps for i in range(1, n_steps + 1)]: hydro_code.evolve_model(time) pyplot.close('all') pyplot.figure() pynbody_column_density_plot(hydro_code.gas_particles, width=10 | units.RSun) pyplot.draw() pyplot.figure() loglog(hydro_code.gas_particles.position.lengths_squared(), hydro_code.gas_particles.pressure, 'bo') pyplot.draw() hydro_code.stop() sleep(3) pyplot.ioff()
def evolve_system(system, t_end, n_steps): times = (t_end * range(1, n_steps+1) / n_steps).as_quantity_in(units.day) for i_step, time in enumerate(times): system.evolve_model(time) print " Evolved to:", time, figname = os.path.join("plots", "hydro_giant_{0:=04}.png".format(i_step)) print " - Hydroplot saved to: ", figname pynbody_column_density_plot(system.gas_particles, width=30|units.AU, vmin=26, vmax=33) scatter(system.dm_particles.x, system.dm_particles.y, c="w") pyplot.savefig(figname) pyplot.close() file_base_name = os.path.join("snapshots", "hydro_giant_{0:=04}_".format(i_step)) write_set_to_file(system.gas_particles, file_base_name+"gas.amuse", format='amuse') write_set_to_file(system.dm_particles, file_base_name+"dm.amuse", format='amuse') system.stop()
def evolve_coupled_system(binary_system, giant_system, t_end, n_steps, do_energy_evolution_plot, previous_data=None): directsum = CalculateFieldForParticles(particles=giant_system.particles, gravity_constant=constants.G) directsum.smoothing_length_squared = giant_system.parameters.gas_epsilon**2 coupled_system = Bridge(timestep=(t_end / (2 * n_steps)), verbose=False, use_threading=True) coupled_system.add_system(binary_system, (directsum,), False) coupled_system.add_system(giant_system, (binary_system,), False) times = (t_end * list(range(1, n_steps+1)) / n_steps).as_quantity_in(units.day) if previous_data: with open(previous_data, 'rb') as file: (all_times, potential_energies, kinetic_energies, thermal_energies, giant_center_of_mass, ms1_position, ms2_position, giant_center_of_mass_velocity, ms1_velocity, ms2_velocity) = pickle.load(file) all_times.extend(times + all_times[-1]) else: all_times = times if do_energy_evolution_plot: potential_energies = coupled_system.particles.potential_energy().as_vector_with_length(1).as_quantity_in(units.erg) kinetic_energies = coupled_system.particles.kinetic_energy().as_vector_with_length(1).as_quantity_in(units.erg) thermal_energies = coupled_system.gas_particles.thermal_energy().as_vector_with_length(1).as_quantity_in(units.erg) else: potential_energies = kinetic_energies = thermal_energies = None giant_center_of_mass = [] | units.RSun ms1_position = [] | units.RSun ms2_position = [] | units.RSun giant_center_of_mass_velocity = [] | units.km / units.s ms1_velocity = [] | units.km / units.s ms2_velocity = [] | units.km / units.s i_offset = len(giant_center_of_mass) giant_total_mass = giant_system.particles.total_mass() ms1_mass = binary_system.particles[0].mass ms2_mass = binary_system.particles[1].mass print(" Evolving for", t_end) for i_step, time in enumerate(times): coupled_system.evolve_model(time) print(" Evolved to:", time, end=' ') if do_energy_evolution_plot: potential_energies.append(coupled_system.particles.potential_energy()) kinetic_energies.append(coupled_system.particles.kinetic_energy()) thermal_energies.append(coupled_system.gas_particles.thermal_energy()) giant_center_of_mass.append(giant_system.particles.center_of_mass()) ms1_position.append(binary_system.particles[0].position) ms2_position.append(binary_system.particles[1].position) giant_center_of_mass_velocity.append(giant_system.particles.center_of_mass_velocity()) ms1_velocity.append(binary_system.particles[0].velocity) ms2_velocity.append(binary_system.particles[1].velocity) a_giant, e_giant = calculate_orbital_elements(ms1_mass, ms2_mass, ms1_position, ms2_position, ms1_velocity, ms2_velocity, giant_total_mass, giant_center_of_mass, giant_center_of_mass_velocity) print("Outer Orbit:", time.in_(units.day), a_giant[-1].in_(units.AU), e_giant[-1], ms1_mass.in_(units.MSun), ms2_mass.in_(units.MSun), giant_total_mass.in_(units.MSun)) if i_step % 10 == 9: snapshotfile = os.path.join("snapshots", "hydro_triple_{0:=04}_gas.amuse".format(i_step + i_offset)) write_set_to_file(giant_system.gas_particles, snapshotfile, format='amuse') snapshotfile = os.path.join("snapshots", "hydro_triple_{0:=04}_core.amuse".format(i_step + i_offset)) write_set_to_file(giant_system.dm_particles, snapshotfile, format='amuse') snapshotfile = os.path.join("snapshots", "hydro_triple_{0:=04}_binary.amuse".format(i_step + i_offset)) write_set_to_file(binary_system.particles, snapshotfile, format='amuse') datafile = os.path.join("snapshots", "hydro_triple_{0:=04}_info.amuse".format(i_step + i_offset)) with open(datafile, 'wb') as outfile: pickle.dump((all_times[:len(giant_center_of_mass)], potential_energies, kinetic_energies, thermal_energies, giant_center_of_mass, ms1_position, ms2_position, giant_center_of_mass_velocity, ms1_velocity, ms2_velocity), outfile) figname1 = os.path.join("plots", "hydro_triple_small{0:=04}.png".format(i_step + i_offset)) figname2 = os.path.join("plots", "hydro_triple_large{0:=04}.png".format(i_step + i_offset)) print(" - Hydroplots are saved to: ", figname1, "and", figname2) for plot_range, plot_name in [(8|units.AU, figname1), (40|units.AU, figname2)]: if HAS_PYNBODY: pynbody_column_density_plot(coupled_system.gas_particles, width=plot_range, vmin=26, vmax=32) scatter(coupled_system.dm_particles.x, coupled_system.dm_particles.y, c="w") else: pyplot.figure(figsize = [16, 16]) sph_particles_plot(coupled_system.gas_particles, gd_particles=coupled_system.dm_particles, view=plot_range*[-0.5, 0.5, -0.5, 0.5]) pyplot.savefig(plot_name) pyplot.close() coupled_system.stop() make_movie() if do_energy_evolution_plot: energy_evolution_plot(all_times[:len(kinetic_energies)-1], kinetic_energies, potential_energies, thermal_energies) print(" Calculating semimajor axis and eccentricity evolution for inner binary") # Some temporary variables to calculate semimajor_axis and eccentricity evolution total_mass = ms1_mass + ms2_mass rel_position = ms1_position - ms2_position rel_velocity = ms1_velocity - ms2_velocity separation_in = rel_position.lengths() speed_squared_in = rel_velocity.lengths_squared() # Now calculate the important quantities: semimajor_axis_binary = (constants.G * total_mass * separation_in / (2 * constants.G * total_mass - separation_in * speed_squared_in)).as_quantity_in(units.AU) eccentricity_binary = numpy.sqrt(1.0 - (rel_position.cross(rel_velocity)**2).sum(axis=1) / (constants.G * total_mass * semimajor_axis_binary)) print(" Calculating semimajor axis and eccentricity evolution of the giant's orbit") # Some temporary variables to calculate semimajor_axis and eccentricity evolution rel_position = ((ms1_mass * ms1_position + ms2_mass * ms2_position)/total_mass - giant_center_of_mass) rel_velocity = ((ms1_mass * ms1_velocity + ms2_mass * ms2_velocity)/total_mass - giant_center_of_mass_velocity) total_mass += giant_total_mass separation = rel_position.lengths() speed_squared = rel_velocity.lengths_squared() # Now calculate the important quantities: semimajor_axis_giant = (constants.G * total_mass * separation / (2 * constants.G * total_mass - separation * speed_squared)).as_quantity_in(units.AU) eccentricity_giant = numpy.sqrt(1.0 - (rel_position.cross(rel_velocity)**2).sum(axis=1) / (constants.G * total_mass * semimajor_axis_giant)) orbit_parameters_plot(semimajor_axis_binary, semimajor_axis_giant, all_times[:len(semimajor_axis_binary)]) orbit_ecc_plot(eccentricity_binary, eccentricity_giant, all_times[:len(eccentricity_binary)]) orbit_parameters_plot(separation_in.as_quantity_in(units.AU), separation.as_quantity_in(units.AU), all_times[:len(eccentricity_binary)], par_symbol="r", par_name="separation") orbit_parameters_plot(speed_squared_in.as_quantity_in(units.km**2 / units.s**2), speed_squared.as_quantity_in(units.km**2 / units.s**2), all_times[:len(eccentricity_binary)], par_symbol="v^2", par_name="speed_squared")
def evolve_coupled_system(binary_system, giant_system, t_end, n_steps, do_energy_evolution_plot, previous_data=None): directsum = CalculateFieldForParticles(particles=giant_system.particles, gravity_constant=constants.G) directsum.smoothing_length_squared = giant_system.parameters.gas_epsilon**2 coupled_system = Bridge(timestep=(t_end / (2 * n_steps)), verbose=False, use_threading=True) coupled_system.add_system(binary_system, (directsum,), False) coupled_system.add_system(giant_system, (binary_system,), False) times = (t_end * range(1, n_steps+1) / n_steps).as_quantity_in(units.day) if previous_data: with open(previous_data, 'rb') as file: (all_times, potential_energies, kinetic_energies, thermal_energies, giant_center_of_mass, ms1_position, ms2_position, giant_center_of_mass_velocity, ms1_velocity, ms2_velocity) = pickle.load(file) all_times.extend(times + all_times[-1]) else: all_times = times if do_energy_evolution_plot: potential_energies = coupled_system.particles.potential_energy().as_vector_with_length(1).as_quantity_in(units.erg) kinetic_energies = coupled_system.particles.kinetic_energy().as_vector_with_length(1).as_quantity_in(units.erg) thermal_energies = coupled_system.gas_particles.thermal_energy().as_vector_with_length(1).as_quantity_in(units.erg) else: potential_energies = kinetic_energies = thermal_energies = None giant_center_of_mass = [] | units.RSun ms1_position = [] | units.RSun ms2_position = [] | units.RSun giant_center_of_mass_velocity = [] | units.km / units.s ms1_velocity = [] | units.km / units.s ms2_velocity = [] | units.km / units.s i_offset = len(giant_center_of_mass) giant_total_mass = giant_system.particles.total_mass() ms1_mass = binary_system.particles[0].mass ms2_mass = binary_system.particles[1].mass print " Evolving for", t_end for i_step, time in enumerate(times): coupled_system.evolve_model(time) print " Evolved to:", time, if do_energy_evolution_plot: potential_energies.append(coupled_system.particles.potential_energy()) kinetic_energies.append(coupled_system.particles.kinetic_energy()) thermal_energies.append(coupled_system.gas_particles.thermal_energy()) giant_center_of_mass.append(giant_system.particles.center_of_mass()) ms1_position.append(binary_system.particles[0].position) ms2_position.append(binary_system.particles[1].position) giant_center_of_mass_velocity.append(giant_system.particles.center_of_mass_velocity()) ms1_velocity.append(binary_system.particles[0].velocity) ms2_velocity.append(binary_system.particles[1].velocity) a_giant, e_giant = calculate_orbital_elements(ms1_mass, ms2_mass, ms1_position, ms2_position, ms1_velocity, ms2_velocity, giant_total_mass, giant_center_of_mass, giant_center_of_mass_velocity) print "Outer Orbit:", time.in_(units.day), a_giant[-1].in_(units.AU), e_giant[-1], ms1_mass.in_(units.MSun), ms2_mass.in_(units.MSun), giant_total_mass.in_(units.MSun) if i_step % 10 == 9: snapshotfile = os.path.join("snapshots", "hydro_triple_{0:=04}_gas.amuse".format(i_step + i_offset)) write_set_to_file(giant_system.gas_particles, snapshotfile, format='amuse') snapshotfile = os.path.join("snapshots", "hydro_triple_{0:=04}_core.amuse".format(i_step + i_offset)) write_set_to_file(giant_system.dm_particles, snapshotfile, format='amuse') snapshotfile = os.path.join("snapshots", "hydro_triple_{0:=04}_binary.amuse".format(i_step + i_offset)) write_set_to_file(binary_system.particles, snapshotfile, format='amuse') datafile = os.path.join("snapshots", "hydro_triple_{0:=04}_info.amuse".format(i_step + i_offset)) with open(datafile, 'wb') as outfile: pickle.dump((all_times[:len(giant_center_of_mass)], potential_energies, kinetic_energies, thermal_energies, giant_center_of_mass, ms1_position, ms2_position, giant_center_of_mass_velocity, ms1_velocity, ms2_velocity), outfile) figname1 = os.path.join("plots", "hydro_triple_small{0:=04}.png".format(i_step + i_offset)) figname2 = os.path.join("plots", "hydro_triple_large{0:=04}.png".format(i_step + i_offset)) print " - Hydroplots are saved to: ", figname1, "and", figname2 for plot_range, plot_name in [(8|units.AU, figname1), (40|units.AU, figname2)]: if HAS_PYNBODY: pynbody_column_density_plot(coupled_system.gas_particles, width=plot_range, vmin=26, vmax=32) scatter(coupled_system.dm_particles.x, coupled_system.dm_particles.y, c="w") else: pyplot.figure(figsize = [16, 16]) sph_particles_plot(coupled_system.gas_particles, gd_particles=coupled_system.dm_particles, view=plot_range*[-0.5, 0.5, -0.5, 0.5]) pyplot.savefig(plot_name) pyplot.close() coupled_system.stop() make_movie() if do_energy_evolution_plot: energy_evolution_plot(all_times[:len(kinetic_energies)-1], kinetic_energies, potential_energies, thermal_energies) print " Calculating semimajor axis and eccentricity evolution for inner binary" # Some temporary variables to calculate semimajor_axis and eccentricity evolution total_mass = ms1_mass + ms2_mass rel_position = ms1_position - ms2_position rel_velocity = ms1_velocity - ms2_velocity separation_in = rel_position.lengths() speed_squared_in = rel_velocity.lengths_squared() # Now calculate the important quantities: semimajor_axis_binary = (constants.G * total_mass * separation_in / (2 * constants.G * total_mass - separation_in * speed_squared_in)).as_quantity_in(units.AU) eccentricity_binary = numpy.sqrt(1.0 - (rel_position.cross(rel_velocity)**2).sum(axis=1) / (constants.G * total_mass * semimajor_axis_binary)) print " Calculating semimajor axis and eccentricity evolution of the giant's orbit" # Some temporary variables to calculate semimajor_axis and eccentricity evolution rel_position = ((ms1_mass * ms1_position + ms2_mass * ms2_position)/total_mass - giant_center_of_mass) rel_velocity = ((ms1_mass * ms1_velocity + ms2_mass * ms2_velocity)/total_mass - giant_center_of_mass_velocity) total_mass += giant_total_mass separation = rel_position.lengths() speed_squared = rel_velocity.lengths_squared() # Now calculate the important quantities: semimajor_axis_giant = (constants.G * total_mass * separation / (2 * constants.G * total_mass - separation * speed_squared)).as_quantity_in(units.AU) eccentricity_giant = numpy.sqrt(1.0 - (rel_position.cross(rel_velocity)**2).sum(axis=1) / (constants.G * total_mass * semimajor_axis_giant)) orbit_parameters_plot(semimajor_axis_binary, semimajor_axis_giant, all_times[:len(semimajor_axis_binary)]) orbit_ecc_plot(eccentricity_binary, eccentricity_giant, all_times[:len(eccentricity_binary)]) orbit_parameters_plot(separation_in.as_quantity_in(units.AU), separation.as_quantity_in(units.AU), all_times[:len(eccentricity_binary)], par_symbol="r", par_name="separation") orbit_parameters_plot(speed_squared_in.as_quantity_in(units.km**2 / units.s**2), speed_squared.as_quantity_in(units.km**2 / units.s**2), all_times[:len(eccentricity_binary)], par_symbol="v^2", par_name="speed_squared")