def xtest6(self): print( "Test6: Should get error when setting attributes with non-quantities (HDF5 storage)." ) particles = Particles(4) particle = Particle() subset = particles[:2] superset = ParticlesSuperset([particles, particle.as_set()]) superset.mass = 1.0 | units.MSun test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "attr_test.hdf5") instances = [particles, particle, subset] classes = [Particles, Particle, ParticlesSubset] lengths = [4, 1, 2] for i, x in enumerate(instances): self.assertTrue(isinstance(x, classes[i])) self.assertEqual(len(x.as_set()), lengths[i]) if os.path.exists(output_file): os.remove(output_file) HDFstorage = store.StoreHDF(output_file) if isinstance(x, Particle): x = x.as_set() x.model_time = 2.0 | units.s HDFstorage.store(x) loaded_particles = HDFstorage.load() self.assertRaises( AttributeError, self.bogus_func, loaded_particles, expected_message= "Can only assign quantities or other particles to an attribute." ) HDFstorage.close() del HDFstorage
def test3(self): print( "Test3: Should get error when accessing non-existent attributes (HDF5 storage)." ) particles = Particles(4) particle = Particle() subset = particles[:2] superset = ParticlesSuperset([particles, particle.as_set()]) superset.mass = 1.0 | units.MSun test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "attr_test.hdf5") instances = [particles, particle, subset] classes = [Particles, Particle, ParticlesSubset] lengths = [4, 1, 2] for i, x in enumerate(instances): self.assertTrue(isinstance(x, classes[i])) self.assertEqual(len(x.as_set()), lengths[i]) if os.path.exists(output_file): os.remove(output_file) HDFstorage = store.StoreHDF(output_file) if isinstance(x, Particle): x = x.as_set() x.model_time = 2.0 | units.s HDFstorage.store(x) loaded_particles = HDFstorage.load() self.assertRaises( AttributeError, lambda: loaded_particles.bogus, expected_message= "You tried to access attribute 'bogus' but this attribute is not defined for this set." ) HDFstorage.close() del HDFstorage
def main(filename="nbody.hdf5", lim=3): pyplot.ion() storage = store.StoreHDF(filename, "r") stars = storage.load() # lim = 4*stars.center_of_mass().length().value_in(stars.x.unit) lim = 2 * max( max(stars.x).value_in(stars.x.unit), stars.center_of_mass().length().value_in(stars.x.unit)) m = 1 + 3.0 * stars.mass / min(stars.mass) for si in stars.history: time = si.get_timestamp() pyplot.title("Cluster at t=" + str(time)) print "time = ", time scatter(si.x, si.y, s=m) xlabel("X") ylabel("Y") pyplot.xlim(-lim, lim) pyplot.ylim(-lim, lim) pyplot.draw() pyplot.cla() pyplot.show()
def simulate_small_cluster(number_of_stars, end_time=40 | units.Myr, name_of_the_figure="test-2.svg"): random.seed() salpeter_masses = new_salpeter_mass_distribution(number_of_stars) total_mass = salpeter_masses.sum() convert_nbody = nbody_system.nbody_to_si(total_mass, 1.0 | units.parsec) convert_nbody.set_as_default() particles = new_plummer_model(number_of_stars, convert_nbody) gravity = PhiGRAPE(convert_nbody, mode="gpu") gravity.initialize_code() gravity.parameters.timestep_parameter = 0.01 gravity.parameters.initial_timestep_parameter = 0.01 gravity.parameters.epsilon_squared = 0.000001 | units.parsec**2 stellar_evolution = SSE() stellar_evolution.initialize_module_with_default_parameters() #print "setting masses of the stars" particles.radius = 0.0 | units.RSun #comment out to get plummer masses particles.mass = salpeter_masses gravity.particles.add_particles(particles) gravity.initialize_particles(0.0) from_model_to_gravity = particles.new_channel_to(gravity.particles) from_gravity_to_model = gravity.particles.new_channel_to(particles) time = 0.0 | units.Myr particles.savepoint(time) move_particles_to_center_of_mass(particles) kineticEnergy = gravity.kinetic_energy.value_in(units.J) potentialEnergy = gravity.potential_energy.value_in(units.J) ToverV = kineticEnergy / potentialEnergy file.write(str(time.value_in(units.Myr))) file.write(' ') file.write(str(ToverV)) file.write('\n') while time < end_time: time += 0.25 | units.Myr gravity.evolve_model(time) from_gravity_to_model.copy() print "Evolved model to t =", str(time) print "Evolved model to t =", str( convert_nbody.to_nbody(time.value_in(units.Myr) | units.Myr)) kineticEnergy = gravity.kinetic_energy.value_in(units.J) potentialEnergy = gravity.potential_energy.value_in(units.J) ToverV = kineticEnergy / potentialEnergy print "Kin / Pot =", ToverV #print "Particle Mass =", particles[1].mass file.write(str(time.value_in(units.Myr))) file.write(' ') file.write(str(ToverV)) file.write('\n') file.close() if os.path.exists('small.hdf5'): os.remove('small.hdf5') storage = store.StoreHDF("small.hdf5") storage.store(particles) del gravity del stellar_evolution
def simulate_small_cluster(number_of_stars, end_time=40 | units.Myr, name_of_the_figure="test-2.svg"): # numpy.random.seed(1) salpeter_masses = new_salpeter_mass_distribution(number_of_stars) total_mass = salpeter_masses.sum() convert_nbody = nbody_system.nbody_to_si(total_mass, 1.0 | units.parsec) particles = new_plummer_model(number_of_stars, convert_nbody) gravity = BHTree(convert_nbody) # print gravity.parameters.timestep.as_quantity_in(units.Myr) gravity.parameters.timestep = 0.0001 | units.Myr # tiny! gravity.parameters.epsilon_squared \ = (float(number_of_stars)**(-0.333333) | units.parsec) ** 2 stellar_evolution = SSE() print "setting masses of the stars" particles.radius = 0.0 | units.RSun particles.mass = salpeter_masses print "initializing the particles" stellar_evolution.particles.add_particles(particles) from_stellar_evolution_to_model \ = stellar_evolution.particles.new_channel_to(particles) from_stellar_evolution_to_model.copy_attributes(["mass"]) print "centering the particles" particles.move_to_center() print "scaling particles to viridial equilibrium" particles.scale_to_standard(convert_nbody) gravity.particles.add_particles(particles) from_model_to_gravity = particles.new_channel_to(gravity.particles) from_gravity_to_model = gravity.particles.new_channel_to(particles) time = 0.0 | units.Myr particles.savepoint(time) total_energy_at_t0 = gravity.kinetic_energy + gravity.potential_energy print "evolving the model until t = " + str(end_time) while time < end_time: time += 0.25 | units.Myr print "gravity evolve step starting" gravity.evolve_model(time) print "gravity evolve step done" print "stellar evolution step starting" stellar_evolution.evolve_model(time) print "stellar evolution step done" from_gravity_to_model.copy() from_stellar_evolution_to_model.copy_attributes(["mass", "radius"]) particles.savepoint(time) from_model_to_gravity.copy_attributes(["mass"]) total_energy_at_this_time \ = gravity.kinetic_energy + gravity.potential_energy print_log(time, gravity, particles, total_energy_at_t0, total_energy_at_this_time) test_results_path = get_path_to_results() output_file = os.path.join(test_results_path, "small.hdf5") if os.path.exists(output_file): os.remove(output_file) storage = store.StoreHDF(output_file) storage.store(particles) gravity.stop() stellar_evolution.stop() plot_particles(particles, name_of_the_figure)
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())
def simulate_small_cluster(number_of_stars, end_time=40 | units.Myr, name_of_the_figure="test-2.svg"): random.seed() salpeter_masses = new_salpeter_mass_distribution(number_of_stars) total_mass = salpeter_masses.sum() convert_nbody = nbody_system.nbody_to_si(total_mass, 1.0 | units.parsec) convert_nbody.set_as_default() print 'end_time:', convert_nbody.to_nbody(end_time) print convert_nbody.to_nbody(total_mass) particles = new_plummer_model(number_of_stars, convert_nbody) gravity = PhiGRAPE(convert_nbody, mode="gpu", use_gl="true") gravity.parameters.timestep_parameter = 0.01 gravity.parameters.initial_timestep_parameter = 0.01 gravity.parameters.epsilon_squared = 0.0015 | units.parsec**2 particles.radius = 0.0 | units.RSun # particles.mass = salpeter_masses move_particles_to_center_of_mass(particles) gravity.particles.add_particles(particles) gravity.initialize_particles(0.0) gravity.start_viewer() from_model_to_gravity = particles.new_channel_to(gravity.particles) from_gravity_to_model = gravity.particles.new_channel_to(particles) time = 0.0 | units.Myr # particles.savepoint(time) kineticEnergy = gravity.kinetic_energy.value_in(units.J) potentialEnergy = gravity.potential_energy.value_in(units.J) ToverV = kineticEnergy / potentialEnergy E0 = convert_nbody.to_nbody(gravity.kinetic_energy + gravity.potential_energy) file.write(str(time.value_in(units.Myr))) file.write(' ') file.write(str(ToverV)) file.write('\n') gravity.parameters.timestep_parameter = 0.01 gravity.parameters.initial_timestep_parameter = 0.01 while time < end_time: time += 0.5 * convert_nbody.to_si(1 | nbody_system.time) gravity.evolve_model(time) gravity.synchronize_model() from_gravity_to_model.copy() print "Evolved model to t =", str(time) kineticEnergy = gravity.kinetic_energy.value_in(units.J) potentialEnergy = gravity.potential_energy.value_in(units.J) ToverV = kineticEnergy / potentialEnergy Et = convert_nbody.to_nbody(gravity.kinetic_energy + gravity.potential_energy) print "Kin / Pot =", ToverV, (Et - E0) / E0 file.write(str(time.value_in(units.Myr))) file.write(' ') file.write(str(ToverV)) file.write('\n') file.close() if os.path.exists('small.hdf5'): os.remove('small.hdf5') storage = store.StoreHDF("small.hdf5") storage.store(particles) del gravity del stellar_evolution