def test20(self): particles = datamodel.Particles(2) particles.x = [0.0, 10.0] | nbody_system.length particles.y = 0.0 | nbody_system.length particles.z = 0.0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0.0 | nbody_system.speed particles.vy = 0.0 | nbody_system.speed particles.vz = 0.0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = BHTree() instance.initialize_code() instance.parameters.stopping_conditions_timeout = very_short_time_to_evolve self.assertEquals(instance.parameters.stopping_conditions_timeout, very_short_time_to_evolve) instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2 instance.particles.add_particles(particles) codeparticles1 = instance.particles instance.particles.add_particle( datamodel.Particle(position=[0, 1, 2] | nbody_system.length, velocity=[0, 0, 0] | nbody_system.speed, radius=0.005 | nbody_system.length, mass=1 | nbody_system.mass)) codeparticles2 = instance.particles self.assertTrue(codeparticles1 is codeparticles2) instance.cleanup_code() codeparticles3 = instance.particles self.assertFalse(codeparticles1 is codeparticles3) instance.stop()
def test19(self): particles = datamodel.Particles(2) particles.x = [0.0,10.0] | nbody_system.length particles.y = 0.0 | nbody_system.length particles.z = 0.0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0.0 | nbody_system.speed particles.vy = 0.0 | nbody_system.speed particles.vz = 0.0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = BHTree() instance.initialize_code() instance.parameters.stopping_conditions_timeout = very_short_time_to_evolve self.assertEquals(instance.parameters.stopping_conditions_timeout, very_short_time_to_evolve) instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2 instance.particles.add_particles(particles) instance.stopping_conditions.timeout_detection.enable() start = time.time() instance.evolve_model(very_long_time_to_evolve) end = time.time() self.assertTrue(instance.stopping_conditions.timeout_detection.is_set()) self.assertTrue((end-start) < very_short_time_to_evolve.value_in(units.s) + 2)#2 = some overhead compensation instance.stop()
def test19(self): particles = datamodel.Particles(2) particles.x = [0.0, 10.0] | nbody_system.length particles.y = 0.0 | nbody_system.length particles.z = 0.0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0.0 | nbody_system.speed particles.vy = 0.0 | nbody_system.speed particles.vz = 0.0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = BHTree() instance.initialize_code() instance.parameters.stopping_conditions_timeout = very_short_time_to_evolve self.assertEquals(instance.parameters.stopping_conditions_timeout, very_short_time_to_evolve) instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2 instance.particles.add_particles(particles) instance.stopping_conditions.timeout_detection.enable() start = time.time() instance.evolve_model(very_long_time_to_evolve) end = time.time() self.assertTrue( instance.stopping_conditions.timeout_detection.is_set()) self.assertTrue( (end - start) < very_short_time_to_evolve.value_in(units.s) + 2) #2 = some overhead compensation instance.stop()
def test20(self): particles = datamodel.Particles(2) particles.x = [0.0,10.0] | nbody_system.length particles.y = 0.0 | nbody_system.length particles.z = 0.0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0.0 | nbody_system.speed particles.vy = 0.0 | nbody_system.speed particles.vz = 0.0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = BHTree() instance.initialize_code() instance.parameters.stopping_conditions_timeout = very_short_time_to_evolve self.assertEquals(instance.parameters.stopping_conditions_timeout, very_short_time_to_evolve) instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2 instance.particles.add_particles(particles) codeparticles1 = instance.particles instance.particles.add_particle(datamodel.Particle( position = [0,1,2] | nbody_system.length, velocity = [0,0,0] | nbody_system.speed, radius = 0.005 | nbody_system.length, mass = 1 | nbody_system.mass )) codeparticles2 = instance.particles self.assertTrue(codeparticles1 is codeparticles2) instance.cleanup_code() codeparticles3 = instance.particles self.assertFalse(codeparticles1 is codeparticles3) instance.stop()
def test16(self): numpy.random.seed(0) number_of_stars = 2 stars = plummer.new_plummer_model(number_of_stars) stars.radius = 0.00001 | nbody_system.length stars.scale_to_standard() instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = (1.0 / 20.0 / (number_of_stars**0.33333) | nbody_system.length)**2 instance.parameters.timestep = 0.004 | nbody_system.time instance.parameters.timestep = 0.00001 | nbody_system.time instance.commit_parameters() print instance.parameters.timestep instance.particles.add_particles(stars) instance.commit_particles() energy_total_t0 = instance.potential_energy + instance.kinetic_energy request = instance.evolve_model. async (1.0 | nbody_system.time) request.result() energy_total_t1 = instance.potential_energy + instance.kinetic_energy self.assertAlmostRelativeEqual(energy_total_t0, energy_total_t1, 3) instance.stop() numpy.random.seed()
def test17(self): print "Testing BHTree collision_detection" particles = datamodel.Particles(7) particles.mass = 0.001 | nbody_system.mass particles.radius = 0.01 | nbody_system.length particles.x = [-101.0, -100.0, -0.5, 0.5, 100.0, 101.0, 104.0] | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.velocity = [[2, 0, 0], [-2, 0, 0]]*3 + [[-4, 0, 0]] | nbody_system.speed instance = BHTree(redirection='none') instance.initialize_code() instance.parameters.set_defaults() # Uncommenting any of the following two lines will suppress collision detection #~ instance.parameters.use_self_gravity = 0 #~ instance.parameters.epsilon_squared = 0.0 | nbody_system.length**2 instance.parameters.opening_angle = 0.1 instance.particles.add_particles(particles) collisions = instance.stopping_conditions.collision_detection collisions.enable() instance.evolve_model(1.0 | nbody_system.time) self.assertTrue(collisions.is_set()) self.assertTrue(instance.model_time < 0.5 | nbody_system.time) self.assertEquals(len(collisions.particles(0)), 3) self.assertEquals(len(collisions.particles(1)), 3) self.assertEquals(len(particles - collisions.particles(0) - collisions.particles(1)), 1) self.assertEquals(abs(collisions.particles(0).x - collisions.particles(1).x) < (collisions.particles(0).radius + collisions.particles(1).radius), [True, True, True]) sticky_merged = datamodel.Particles(len(collisions.particles(0))) sticky_merged.mass = collisions.particles(0).mass + collisions.particles(1).mass sticky_merged.radius = collisions.particles(0).radius for p1, p2, merged in zip(collisions.particles(0), collisions.particles(1), sticky_merged): merged.position = (p1 + p2).center_of_mass() merged.velocity = (p1 + p2).center_of_mass_velocity() print instance.model_time print instance.particles instance.particles.remove_particles(collisions.particles(0) + collisions.particles(1)) instance.particles.add_particles(sticky_merged) instance.evolve_model(1.0 | nbody_system.time) print print instance.model_time print instance.particles self.assertTrue(collisions.is_set()) self.assertTrue(instance.model_time < 1.0 | nbody_system.time) self.assertEquals(len(collisions.particles(0)), 1) self.assertEquals(len(collisions.particles(1)), 1) self.assertEquals(len(instance.particles - collisions.particles(0) - collisions.particles(1)), 2) self.assertEquals(abs(collisions.particles(0).x - collisions.particles(1).x) < (collisions.particles(0).radius + collisions.particles(1).radius), [True]) instance.stop()
def test10(self): instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = 0.00001 | nbody_system.length**2 instance.commit_parameters() particles = datamodel.Particles(6) particles.mass = 1.0 | nbody_system.mass particles.radius = 0.00001 | nbody_system.length particles.position = [[-1.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, -1.0], [0.0, 0.0, 1.0] ] | nbody_system.length particles.velocity = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [ 0.0, 0.0, 0.0 ], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0] ] | nbody_system.speed instance.particles.add_particles(particles) instance.commit_particles() zero = 0.0 | nbody_system.length fx, fy, fz = instance.get_gravity_at_point(zero, zero, zero, zero) self.assertAlmostEqual(fx, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fy, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz, 0.0 | nbody_system.acceleration, 3) for position in (0.25, 0.5, 0.75): p0 = position | nbody_system.length p1 = -position | nbody_system.length for i in range(3): args0 = [zero] * 4 args1 = [zero] * 4 args0[1 + i] = p0 args1[1 + i] = p1 f0 = instance.get_gravity_at_point(*args0) f1 = instance.get_gravity_at_point(*args1) for j in range(3): if j != i: self.assertAlmostEqual(f0[j], 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(f1[j], 0.0 | nbody_system.acceleration, 3) else: self.assertAlmostEqual(f0[j], -1.0 * f1[j], 5) instance.stop()
def test9(self): instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = 0.00001 | nbody_system.length**2 particles = datamodel.Particles(2) particles.mass = [1.0, 1.0] | nbody_system.mass particles.radius = [0.0001, 0.0001] | nbody_system.length particles.position = [[0.0, 0.0, 0.0], [2.0, 0.0, 0.0] ] | nbody_system.length particles.velocity = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0] ] | nbody_system.speed instance.particles.add_particles(particles) zero = 0.0 | nbody_system.length fx, fy, fz = instance.get_gravity_at_point(zero, 1.0 | nbody_system.length, zero, zero) self.assertAlmostEqual(fx, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fy, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz, 0.0 | nbody_system.acceleration, 3) for x in (0.25, 0.5, 0.75): x0 = x | nbody_system.length x1 = (2.0 - x) | nbody_system.length potential0 = instance.get_potential_at_point(zero, x0, zero, zero) potential1 = instance.get_potential_at_point(zero, x1, zero, zero) fx0, fy0, fz0 = instance.get_gravity_at_point(zero, x0, zero, zero) fx1, fy1, fz1 = instance.get_gravity_at_point(zero, x1, zero, zero) self.assertAlmostEqual(fy0, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz0, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fy1, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz1, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fx0, -1.0 * fx1, 5) fx = (-1.0 / (x0**2) + 1.0 / (x1**2)) * (1.0 | nbody_system.length**3 / nbody_system.time**2) self.assertAlmostEqual(fx, fx0, 2) self.assertAlmostEqual(potential0, potential1, 5) instance.cleanup_code() instance.stop()
def test15(self): print "Test15: Testing effect of BHTree parameter epsilon_squared" convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 1.0 | units.AU) particles = datamodel.Particles(2) sun = particles[0] sun.mass = 1.0 | units.MSun sun.position = [0.0, 0.0, 0.0] | units.AU sun.velocity = [0.0, 0.0, 0.0] | units.AU / units.yr sun.radius = 1.0 | units.RSun earth = particles[1] earth.mass = 5.9736e24 | units.kg earth.radius = 6371.0 | units.km earth.position = [0.0, 1.0, 0.0] | units.AU earth.velocity = [2.0 * numpy.pi, -0.0001, 0.0] | units.AU / units.yr initial_direction = math.atan((earth.velocity[0] / earth.velocity[1])) final_direction = [] for log_eps2 in range(-9, 10, 2): instance = BHTree(convert_nbody) instance.initialize_code() instance.parameters.epsilon_squared = 10.0**log_eps2 | units.AU**2 instance.particles.add_particles(particles) instance.commit_particles() instance.evolve_model(0.25 | units.yr) final_direction.append( math.atan((instance.particles[1].velocity[0] / instance.particles[1].velocity[1]))) instance.stop() # Small values of epsilon_squared should result in normal earth-sun dynamics: rotation of 90 degrees self.assertAlmostEquals(abs(final_direction[0]), abs(initial_direction + math.pi / 2.0), 2) # Large values of epsilon_squared should result in ~ no interaction self.assertAlmostEquals(final_direction[-1], initial_direction, 2) # Outcome is most sensitive to epsilon_squared when epsilon_squared = d(earth, sun)^2 delta = [ abs(final_direction[i + 1] - final_direction[i]) for i in range(len(final_direction) - 1) ] self.assertEquals(delta[len(final_direction) // 2 - 1], max(delta))
def test10(self): instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = 0.00001 | nbody_system.length**2 instance.commit_parameters() particles = datamodel.Particles(6) particles.mass = 1.0 | nbody_system.mass particles.radius = 0.00001 | nbody_system.length particles.position = [[-1.0,0.0,0.0],[1.0,0.0,0.0],[0.0,-1.0,0.0],[0.0,1.0,0.0],[0.0,0.0,-1.0],[0.0,0.0,1.0]] | nbody_system.length particles.velocity = [[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]] | nbody_system.speed instance.particles.add_particles(particles) instance.commit_particles() zero = 0.0 | nbody_system.length fx, fy, fz = instance.get_gravity_at_point(zero, zero, zero, zero) self.assertAlmostEqual(fx, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fy, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz, 0.0 | nbody_system.acceleration, 3) for position in (0.25, 0.5, 0.75): p0 = position | nbody_system.length p1 = -position | nbody_system.length for i in range(3): args0 = [zero] * 4 args1 = [zero] * 4 args0[1 + i] = p0 args1[1 + i] = p1 f0 = instance.get_gravity_at_point(*args0) f1 = instance.get_gravity_at_point(*args1) for j in range(3): if j != i: self.assertAlmostEqual(f0[j], 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(f1[j], 0.0 | nbody_system.acceleration, 3) else: self.assertAlmostEqual(f0[j], -1.0 * f1[j], 5) instance.stop()
def test21(self): particles = datamodel.Particles(2) particles.x = [0.0,10.0] | nbody_system.length particles.y = 0.0 | nbody_system.length particles.z = 0.0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0.0 | nbody_system.speed particles.vy = 0.0 | nbody_system.speed particles.vz = 0.0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = (1e-5 | nbody_system.length)**2 instance.particles.add_particles(particles) instance.commit_particles() self.assertAlmostRelativeEquals(instance.potential_energy, -0.1 | nbody_system.energy, 5) instance.stop()
def test21(self): particles = datamodel.Particles(2) particles.x = [0.0, 10.0] | nbody_system.length particles.y = 0.0 | nbody_system.length particles.z = 0.0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0.0 | nbody_system.speed particles.vy = 0.0 | nbody_system.speed particles.vz = 0.0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = (1e-5 | nbody_system.length)**2 instance.particles.add_particles(particles) instance.commit_particles() self.assertAlmostRelativeEquals(instance.potential_energy, -0.1 | nbody_system.energy, 5) instance.stop()
def test18(self): particles = datamodel.Particles(2) particles.x = [0.0,10.0] | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0 | nbody_system.speed particles.vy = 0 | nbody_system.speed particles.vz = 0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass instance = BHTree() instance.initialize_code() instance.parameters.stopping_conditions_number_of_steps = 2 self.assertEquals(instance.parameters.stopping_conditions_number_of_steps, 2) instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2 instance.particles.add_particles(particles) instance.stopping_conditions.number_of_steps_detection.enable() instance.evolve_model(10 | nbody_system.time) self.assertTrue(instance.stopping_conditions.number_of_steps_detection.is_set()) self.assertTrue(instance.model_time < 10 | nbody_system.time) instance.stop()
def test9(self): instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = 0.00001 | nbody_system.length**2 particles = datamodel.Particles(2) particles.mass = [1.0, 1.0] | nbody_system.mass particles.radius = [0.0001, 0.0001] | nbody_system.length particles.position = [[0.0,0.0,0.0], [2.0,0.0,0.0]] | nbody_system.length particles.velocity = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] | nbody_system.speed instance.particles.add_particles(particles) zero = 0.0 | nbody_system.length fx, fy, fz = instance.get_gravity_at_point(zero, 1.0 | nbody_system.length, zero, zero) self.assertAlmostEqual(fx, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fy, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz, 0.0 | nbody_system.acceleration, 3) for x in (0.25, 0.5, 0.75): x0 = x | nbody_system.length x1 = (2.0 - x) | nbody_system.length potential0 = instance.get_potential_at_point(zero, x0, zero, zero) potential1 = instance.get_potential_at_point(zero, x1, zero, zero) fx0, fy0, fz0 = instance.get_gravity_at_point(zero, x0, zero, zero) fx1, fy1, fz1 = instance.get_gravity_at_point(zero, x1, zero, zero) self.assertAlmostEqual(fy0, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz0, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fy1, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fz1, 0.0 | nbody_system.acceleration, 3) self.assertAlmostEqual(fx0, -1.0 * fx1, 5) fx = (-1.0 / (x0**2) + 1.0 / (x1**2)) * (1.0 | nbody_system.length ** 3 / nbody_system.time ** 2) self.assertAlmostEqual(fx, fx0, 2) self.assertAlmostEqual(potential0, potential1, 5) instance.cleanup_code() instance.stop()
def test16(self): numpy.random.seed(0) number_of_stars = 2 stars = plummer.new_plummer_model(number_of_stars) stars.radius = 0.00001 | nbody_system.length stars.scale_to_standard() instance = BHTree() instance.initialize_code() instance.parameters.epsilon_squared = (1.0 / 20.0 / (number_of_stars**0.33333) | nbody_system.length)**2 instance.parameters.timestep = 0.004 | nbody_system.time instance.parameters.timestep = 0.00001 | nbody_system.time instance.commit_parameters() print instance.parameters.timestep instance.particles.add_particles(stars) instance.commit_particles() energy_total_t0 = instance.potential_energy + instance.kinetic_energy request = instance.evolve_model.async(1.0 | nbody_system.time) request.result() energy_total_t1 = instance.potential_energy + instance.kinetic_energy self.assertAlmostRelativeEqual(energy_total_t0, energy_total_t1, 3) instance.stop() numpy.random.seed()
def test15(self): print "Test15: Testing effect of BHTree parameter epsilon_squared" convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 1.0 | units.AU) particles = datamodel.Particles(2) sun = particles[0] sun.mass = 1.0 | units.MSun sun.position = [0.0, 0.0, 0.0] | units.AU sun.velocity = [0.0, 0.0, 0.0] | units.AU / units.yr sun.radius = 1.0 | units.RSun earth = particles[1] earth.mass = 5.9736e24 | units.kg earth.radius = 6371.0 | units.km earth.position = [0.0, 1.0, 0.0] | units.AU earth.velocity = [2.0*numpy.pi, -0.0001, 0.0] | units.AU / units.yr initial_direction = math.atan((earth.velocity[0]/earth.velocity[1])) final_direction = [] for log_eps2 in range(-9,10,2): instance = BHTree(convert_nbody) instance.initialize_code() instance.parameters.epsilon_squared = 10.0**log_eps2 | units.AU ** 2 instance.particles.add_particles(particles) instance.commit_particles() instance.evolve_model(0.25 | units.yr) final_direction.append(math.atan((instance.particles[1].velocity[0]/ instance.particles[1].velocity[1]))) instance.stop() # Small values of epsilon_squared should result in normal earth-sun dynamics: rotation of 90 degrees self.assertAlmostEquals(abs(final_direction[0]), abs(initial_direction+math.pi/2.0), 2) # Large values of epsilon_squared should result in ~ no interaction self.assertAlmostEquals(final_direction[-1], initial_direction, 2) # Outcome is most sensitive to epsilon_squared when epsilon_squared = d(earth, sun)^2 delta = [abs(final_direction[i+1]-final_direction[i]) for i in range(len(final_direction)-1)] self.assertEquals(delta[len(final_direction)//2 -1], max(delta))
def test18(self): particles = datamodel.Particles(2) particles.x = [0.0, 10.0] | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.radius = 0.005 | nbody_system.length particles.vx = 0 | nbody_system.speed particles.vy = 0 | nbody_system.speed particles.vz = 0 | nbody_system.speed particles.mass = 1.0 | nbody_system.mass instance = BHTree() instance.initialize_code() instance.parameters.stopping_conditions_number_of_steps = 2 self.assertEquals( instance.parameters.stopping_conditions_number_of_steps, 2) instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2 instance.particles.add_particles(particles) instance.stopping_conditions.number_of_steps_detection.enable() instance.evolve_model(10 | nbody_system.time) self.assertTrue( instance.stopping_conditions.number_of_steps_detection.is_set()) self.assertTrue(instance.model_time < 10 | nbody_system.time) instance.stop()
def test2(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) bhtree = BHTree(convert_nbody) bhtree.initialize_code() bhtree.eps2_for_gravity = 0.001 bhtree_particles = self.new_system_sun_and_earth() bhtree.particles.add_particles(bhtree_particles) if bhtree.legacy_interface.channel_type == 'mpi': from mpi4py import MPI if not MPI.Query_thread() == MPI.THREAD_MULTIPLE: bhtree.stop() self.skip( "can only test parallel with multiple thread support in mpi implementation" ) hermite = Hermite(convert_nbody) hermite.dt_dia = 5000 hermite.commit_parameters() hermite_particles = self.new_system_sun_and_earth() hermite.particles.add_particles(hermite_particles) thread1 = threading.Thread(target=self.evolve_model_unit_day, args=(bhtree, bhtree_particles, 10)) thread2 = threading.Thread(target=self.evolve_model_unit_day, args=(hermite, hermite_particles, 10)) thread1.start() thread2.start() thread1.join() thread2.join() if HAS_MATPLOTLIB: figure = pyplot.figure() plot = figure.add_subplot(1, 1, 1) earth = bhtree_particles[1] x_points = earth.get_timeline_of_attribute("x") y_points = earth.get_timeline_of_attribute("y") x_points_in_AU = [t_x[1].value_in(units.AU) for t_x in x_points] y_points_in_AU = [t_x1[1].value_in(units.AU) for t_x1 in y_points] plot.scatter(x_points_in_AU, y_points_in_AU, color="b", marker='o') earth = hermite_particles[1] x_points = earth.get_timeline_of_attribute("x") y_points = earth.get_timeline_of_attribute("y") x_points_in_AU = [t_x2[1].value_in(units.AU) for t_x2 in x_points] y_points_in_AU = [t_x3[1].value_in(units.AU) for t_x3 in y_points] plot.scatter(x_points_in_AU, y_points_in_AU, color="g", marker='o') plot.set_xlim(-1.5, 1.5) plot.set_ylim(-1.5, 1.5) test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "parallel-earth-sun.svg") figure.savefig(output_file) bhtree.stop() hermite.stop() bhtree.stop()
def test17(self): print "Testing BHTree collision_detection" particles = datamodel.Particles(7) particles.mass = 0.001 | nbody_system.mass particles.radius = 0.01 | nbody_system.length particles.x = [-101.0, -100.0, -0.5, 0.5, 100.0, 101.0, 104.0 ] | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.velocity = [[2, 0, 0], [-2, 0, 0] ] * 3 + [[-4, 0, 0]] | nbody_system.speed instance = BHTree(redirection='none') instance.initialize_code() instance.parameters.set_defaults() # Uncommenting any of the following two lines will suppress collision detection #~ instance.parameters.use_self_gravity = 0 #~ instance.parameters.epsilon_squared = 0.0 | nbody_system.length**2 instance.parameters.opening_angle = 0.1 instance.particles.add_particles(particles) collisions = instance.stopping_conditions.collision_detection collisions.enable() instance.evolve_model(1.0 | nbody_system.time) self.assertTrue(collisions.is_set()) self.assertTrue(instance.model_time < 0.5 | nbody_system.time) self.assertEquals(len(collisions.particles(0)), 3) self.assertEquals(len(collisions.particles(1)), 3) self.assertEquals( len(particles - collisions.particles(0) - collisions.particles(1)), 1) self.assertEquals( abs(collisions.particles(0).x - collisions.particles(1).x) < (collisions.particles(0).radius + collisions.particles(1).radius), [True, True, True]) sticky_merged = datamodel.Particles(len(collisions.particles(0))) sticky_merged.mass = collisions.particles( 0).mass + collisions.particles(1).mass sticky_merged.radius = collisions.particles(0).radius for p1, p2, merged in zip(collisions.particles(0), collisions.particles(1), sticky_merged): merged.position = (p1 + p2).center_of_mass() merged.velocity = (p1 + p2).center_of_mass_velocity() print instance.model_time print instance.particles instance.particles.remove_particles( collisions.particles(0) + collisions.particles(1)) instance.particles.add_particles(sticky_merged) instance.evolve_model(1.0 | nbody_system.time) print print instance.model_time print instance.particles self.assertTrue(collisions.is_set()) self.assertTrue(instance.model_time < 1.0 | nbody_system.time) self.assertEquals(len(collisions.particles(0)), 1) self.assertEquals(len(collisions.particles(1)), 1) self.assertEquals( len(instance.particles - collisions.particles(0) - collisions.particles(1)), 2) self.assertEquals( abs(collisions.particles(0).x - collisions.particles(1).x) < (collisions.particles(0).radius + collisions.particles(1).radius), [True]) instance.stop()
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) gravity.initialize_code() # gravity.parameters.set_defaults() # 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() stellar_evolution.initialize_module_with_default_parameters() 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) gravity.commit_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 = gravity.evolve_model.async(time) print "Stellar evolution step starting" stellar_evolution_evolve = stellar_evolution.evolve_model(time) print "Stellar evolution step done." gravity_evolve.result() print "Gravity evolve 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 assignment_2d(): current_cluster_mass = 400 | units.MSun initial_mass_fraction = 0.84 desired_initial_mass = current_cluster_mass / initial_mass_fraction masses = new_salpeter_mass_distribution(100000) mean_salpeter_mass = masses.mean() print "mean salpeter mass", mean_salpeter_mass N = int(desired_initial_mass / mean_salpeter_mass) print "N", N Rvir = 10 | units.lightyear z = 0.17 masses = new_salpeter_mass_distribution(N) converter = nbody_system.nbody_to_si(masses.sum(), Rvir) G_SI = converter.to_si(nbody_system.G) bodies = new_plummer_sphere(N, convert_nbody=converter) bodies.mass = masses bodies.metalicity = z # start the gravity solver gravity = BHTree(converter) gravity.initialize_code() gravity.parameters.timestep = 0.1 | units.Myr # start the stellar evolution solver stellar = SSE() stars = stellar.particles.add_particles(bodies) from_stellar_evolution_to_model \ = stellar.particles.new_channel_to(bodies) from_stellar_evolution_to_model.copy_attributes(["mass"]) bodies.scale_to_standard(converter) gravity.particles.add_particles(bodies) from_model_to_gravity = bodies.new_channel_to(gravity.particles) from_gravity_to_model = gravity.particles.new_channel_to(bodies) gravity.commit_particles() end_time = 1000 | units.Myr current_time = 0 | units.Myr cluster = "Hyades" bound_stars_counts = [] main_sequence_stars_counts = [] giant_stars_counts = [] remnant_stars_counts = [] max_radii = [] | units.parsec virial_radii = [] | units.parsec times = [] | units.Myr while current_time < end_time: name_of_the_figure = "isochrone_with_grav_"+str(int(current_time.value_in(units.Myr)))+".png" gravity.evolve_model(current_time) stellar.evolve_model(current_time) from_gravity_to_model.copy() from_stellar_evolution_to_model.copy_attributes(["mass", "radius"]) from_model_to_gravity.copy_attributes(["mass"]) remnant_count = 0 main_sequence_count = 0 giant_count = 0 for star in stars: if stellar_remnant_state(star): remnant_count += 1 if stellar_giant_state(star): giant_count += 1 if stellar_main_sequence_state(star): main_sequence_count += 1 max_radius = bodies.total_radius() virial_radius = bodies.virial_radius() bound_star_count = len(bodies.bound_subset(unit_converter=converter, G=G_SI)) print "bound stars:", bound_star_count print "main sequence stars:", main_sequence_count print "giant stars:", giant_count print "remnant stars:", remnant_count print "cluster radius(max from centre):", max_radius print "virial radius:", virial_radius print current_time times.append(current_time) remnant_stars_counts.append(remnant_count) giant_stars_counts.append(giant_count) main_sequence_stars_counts.append(main_sequence_count) max_radii.append(max_radius) virial_radii.append(virial_radius) bound_stars_counts.append(bound_star_count) temperatures = stars.temperature luminosities = stars.luminosity plot_HR_diagram(temperatures, luminosities, cluster+"/", name_of_the_figure, current_time) current_time += 10 | units.Myr data = {} data["bound_stars_at_time"] = bound_stars_counts data["remnant_stars_at_time"] = remnant_stars_counts data["giant_stars_at_time"] = giant_stars_counts data["main_sequence_stars_at_time"] = main_sequence_stars_counts data["max_radius_at_time"] = max_radii data["virial_radii"] = virial_radii data["times"] = times pickle.dump(data, open(cluster+"/assignment2d.dat", "wb"))
def test2(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) bhtree = BHTree(convert_nbody) bhtree.initialize_code() bhtree.eps2_for_gravity = 0.001 bhtree_particles = self.new_system_sun_and_earth() bhtree.particles.add_particles(bhtree_particles) if bhtree.legacy_interface.channel_type == 'mpi': from mpi4py import MPI if not MPI.Query_thread() == MPI.THREAD_MULTIPLE: bhtree.stop() self.skip("can only test parallel with multiple thread support in mpi implementation") hermite = Hermite(convert_nbody) hermite.dt_dia = 5000 hermite.commit_parameters() hermite_particles = self.new_system_sun_and_earth() hermite.particles.add_particles(hermite_particles) thread1 = threading.Thread(target = self.evolve_model_unit_day, args = (bhtree, bhtree_particles, 10)) thread2 = threading.Thread(target = self.evolve_model_unit_day, args = (hermite, hermite_particles, 10)) thread1.start() thread2.start() thread1.join() thread2.join() if HAS_MATPLOTLIB: figure = pyplot.figure() plot = figure.add_subplot(1,1,1) earth = bhtree_particles[1] x_points = earth.get_timeline_of_attribute("x") y_points = earth.get_timeline_of_attribute("y") x_points_in_AU = map(lambda (t,x) : x.value_in(units.AU), x_points) y_points_in_AU = map(lambda (t,x) : x.value_in(units.AU), y_points) plot.scatter(x_points_in_AU,y_points_in_AU, color = "b", marker = 'o') earth = hermite_particles[1] x_points = earth.get_timeline_of_attribute("x") y_points = earth.get_timeline_of_attribute("y") x_points_in_AU = map(lambda (t,x) : x.value_in(units.AU), x_points) y_points_in_AU = map(lambda (t,x) : x.value_in(units.AU), y_points) plot.scatter(x_points_in_AU,y_points_in_AU, color = "g", marker = 'o') plot.set_xlim(-1.5, 1.5) plot.set_ylim(-1.5, 1.5) test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "parallel-earth-sun.svg") figure.savefig(output_file) bhtree.stop() hermite.stop() bhtree.stop()