def new_star_code_phigrape(self): result = PhiGRAPE(self.converter, mode="gpu") result.parameters.initialize_gpu_once = 1 result.parameters.epsilon_squared = self.star_epsilon ** 2 result.particles.add_particles(self.new_particles_cluster()) result.commit_particles() return result
def test10(self): instance = PhiGRAPE(**default_test_options) instance.initialize_code() instance.parameters.set_defaults() stars = new_plummer_model(100) stars.radius = 0 | nbody_system.length instance.particles.add_particles(stars) channel = stars.new_channel_to(instance.particles) instance.evolve_model(0.001 | nbody_system.time) e0 = instance.kinetic_energy + instance.potential_energy stars.mass *= 0.9 channel.copy() instance.synchronize_model() e1 = instance.kinetic_energy + instance.potential_energy delta_e = e1 - e0 self.assertTrue(e1 != e0) instance.stop()
def test7(self): instance = PhiGRAPE(**default_test_options) instance.initialize_code() instance.parameters.set_defaults() 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) instance.commit_particles() zero = [0.0, 0.0, 0.0] | nbody_system.length fx, fy, fz = instance.get_gravity_at_point(zero, [0.5, 1.0, 1.5] | nbody_system.length, zero, zero) self.assertAlmostRelativeEqual(fx[0], -3.55555555556 | nbody_system.acceleration, 5) self.assertAlmostRelativeEqual(fy[0], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fz[0], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fx[1], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fy[1], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fz[1], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fx[2], 3.55555555556 | nbody_system.acceleration, 5) self.assertAlmostRelativeEqual(fy[2], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fz[2], 0.0 | nbody_system.acceleration, 3) n = 512 x = nbody_system.length.new_quantity(numpy.linspace(0.1, 1.9, n)) zero = nbody_system.length.new_quantity(numpy.zeros(n)) fx, fy, fz = instance.get_gravity_at_point(zero, x, zero, zero) for i in range(n//2): self.assertAlmostRelativeEqual(fx[i], - fx[n - 1 - i], 5) instance.stop()
def test19(self): converter = nbody_system.nbody_to_si(1 | units.MSun, 1 | units.parsec) particles = datamodel.Particles(2) particles.mass = 100 | units.MSun particles.radius = 200 | units.RSun particles[0].position = [0,0,0] | units.parsec particles[1].position = [1,0,0] | units.parsec particles.velocity = [0,0,0] | units.km / units.s code = PhiGRAPE( converter, PhiGRAPEInterface.MODE_G6LIB, number_of_workers=2 ) code.initialize_code() stop_cond = code.stopping_conditions.collision_detection stop_cond.enable() code.particles.add_particles(particles) code.evolve_model( 0.08 | nbody_system.time) self.assertTrue(stop_cond.is_set())
def new_star_code_phigrape(self): result = PhiGRAPE(mode="gpu") result.parameters.initialize_gpu_once = 1 result.parameters.epsilon_squared = self.star_epsilon**2 result.particles.add_particles(self.new_particles_cluster()) result.commit_particles() return result
def test7(self): instance = PhiGRAPE(**default_test_options) instance.initialize_code() instance.parameters.set_defaults() 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) instance.commit_particles() zero = [0.0, 0.0, 0.0] | nbody_system.length fx, fy, fz = instance.get_gravity_at_point(zero, [0.5, 1.0, 1.5] | nbody_system.length, zero, zero) self.assertAlmostRelativeEqual(fx[0], -3.55555555556 | nbody_system.acceleration, 5) self.assertAlmostRelativeEqual(fy[0], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fz[0], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fx[1], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fy[1], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fz[1], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fx[2], 3.55555555556 | nbody_system.acceleration, 5) self.assertAlmostRelativeEqual(fy[2], 0.0 | nbody_system.acceleration, 3) self.assertAlmostRelativeEqual(fz[2], 0.0 | nbody_system.acceleration, 3) n = 512 x = nbody_system.length.new_quantity(numpy.linspace(0.1, 1.9, n)) zero = nbody_system.length.new_quantity(numpy.zeros(n)) fx, fy, fz = instance.get_gravity_at_point(zero, x, zero, zero) for i in range(n/2): self.assertAlmostRelativeEqual(fx[i], - fx[n - 1 - i], 5) instance.stop()
def new_instance_of_capreole_and_nbody_code(self): gridcode=Capreole(number_of_workers=self.number_of_workers) gridcode.initialize_code() nbodycode = PhiGRAPE(mode="gpu") #nbodycode = Octgrav(mode="gpu") nbodycode.initialize_code() nbodycode.parameters.epsilon_squared = (self.size / (10000.0 * self.number_of_grid_points)) ** 2 nbodycode.commit_parameters() result = HydroGridAndNbodyWithAccelerationTransfer(gridcode, nbodycode) return result
def test11(self): print "Testing PhiGRAPE 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 = PhiGRAPE(redirection='none', **default_test_options) instance.initialize_code() instance.parameters.set_defaults() 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 test14(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 very_short_time_to_evolve = 1 | units.s very_long_time_to_evolve = 1e9 | nbody_system.time instance = PhiGRAPE(**default_test_options) 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 new_instance_of_athena_and_nbody_code(self): gridcode=Athena(redirection="none", number_of_workers=1) gridcode.initialize_code() gridcode.parameters.gamma = self.gamma gridcode.parameters.courant_number=0.3 gridcode.set_has_external_gravitational_potential(1) nbodycode = PhiGRAPE(mode="gpu") #nbodycode = Octgrav(mode="gpu") nbodycode.initialize_code() nbodycode.parameters.epsilon_squared = (self.size / (10000.0 * self.number_of_grid_points)) ** 2 nbodycode.commit_parameters() result = HydroGridAndNbody(gridcode, nbodycode) return result
def test4(self): convert_nbody = nbody_system.nbody_to_si(5.0 | units.kg, 10.0 | units.m) instance = PhiGRAPE(convert_nbody, **default_test_options) instance.initialize_code() instance.parameters.set_defaults() particles = datamodel.Particles(2) self.assertEquals(len(instance.particles), 0) particles.mass = [15.0, 30.0] | units.kg particles.radius = [10.0, 20.0] | units.m particles.position = [[10.0, 20.0, 30.0], [20.0, 40.0, 60.0]] | units.m particles.velocity = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] | units.m / units.s instance.particles.add_particles(particles) self.assertEquals(len(instance.particles), 2) instance.particles.mass = [17.0, 33.0] | units.kg self.assertEquals(instance.get_mass(1), 17.0| units.kg) self.assertEquals(instance.get_mass(2), 33.0| units.kg) instance.stop()
def test18(self): print "Testing PhiGRAPE delete_particle" number_of_particles = 15 delete_order = [6, 9, 3, 1, 0, 2, 5] number_of_initial_deletes = 3 particles = datamodel.Particles(number_of_particles) particles.mass = range(1, number_of_particles + 1) | nbody_system.mass particles.radius = 0.01 | nbody_system.length particles.x = range(number_of_particles) | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.vx = range(number_of_particles) | nbody_system.speed particles.vy = 0 | nbody_system.speed particles.vz = 0 | nbody_system.speed instance = PhiGRAPE(**default_test_options) instance.initialize_code() instance.parameters.set_defaults() initial_number_of_particles = number_of_particles - len(delete_order) + number_of_initial_deletes instance.particles.add_particles(particles[:initial_number_of_particles]) index_new_particle = initial_number_of_particles self.assertEquals(len(instance.particles), initial_number_of_particles) instance.evolve_model(0.01 | nbody_system.time) instance.particles.remove_particles(particles[delete_order[:number_of_initial_deletes]]) number_of_deletes = number_of_initial_deletes self.assertEquals(len(instance.particles), initial_number_of_particles - number_of_deletes) for i, particle_index in enumerate(delete_order[number_of_initial_deletes:]): instance.evolve_model((i+1) / 10.0 | nbody_system.time) instance.particles.remove_particle(particles[particle_index]) number_of_deletes += 1 instance.particles.add_particle(particles[index_new_particle]) index_new_particle += 1 self.assertEquals(len(instance.particles), initial_number_of_particles - number_of_deletes + i + 1) self.assertAlmostEquals(particles[:index_new_particle].total_mass() - instance.particles.total_mass(), (delete_order[:number_of_deletes] | nbody_system.mass).sum() + (number_of_deletes | nbody_system.mass)) instance.stop()
def test5(self): instance = PhiGRAPE(**default_test_options) instance.initialize_code() instance.parameters.set_defaults() 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) instance.commit_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.stop()
def test6(self): instance = PhiGRAPE(**default_test_options) instance.initialize_code() instance.parameters.set_defaults() particles = datamodel.Particles(6) particles.mass = nbody_system.mass.new_quantity(range(1,7)) 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() copyof = instance.particles.copy() self.assertEquals(2 | nbody_system.mass, copyof[1].mass) instance.stop()
def test_phigrape(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.kg, 1.0 | units.km) phigrape = PhiGRAPE(convert_nbody) phigrape.parameters.epsilon_squared = 10 | units.km**2 docstring = phigrape.parameters.__doc__ print docstring self.assertTrue("smoothing parameter for gravity calculations (default value:0.0 m**2)" in docstring) self.assertTrue("timestep parameter (default value:0.02" in docstring) self.assertTrue("parameter to determine the initial timestep (default value:0.01" in docstring) parameter_str_method_output = str(phigrape.parameters) self.assertTrue("epsilon_squared: 10000000.0 m**2" in parameter_str_method_output) self.assertTrue("timestep_parameter: 0.0" in parameter_str_method_output) self.assertTrue("initial_timestep_parameter: 0.0" in parameter_str_method_output)
def new_instance_of_capreole_and_nbody_code(self): gridcode = Capreole(number_of_workers=self.number_of_workers) gridcode.initialize_code() nbodycode = PhiGRAPE(mode="gpu") #nbodycode = Octgrav(mode="gpu") nbodycode.initialize_code() nbodycode.parameters.epsilon_squared = ( self.size / (10000.0 * self.number_of_grid_points))**2 nbodycode.commit_parameters() result = HydroGridAndNbodyWithAccelerationTransfer(gridcode, nbodycode) return result
def test13(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 = PhiGRAPE(**default_test_options) 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 new_instance_of_athena_and_nbody_code(self): gridcode = Athena(redirection="none", number_of_workers=1) gridcode.initialize_code() gridcode.parameters.gamma = self.gamma gridcode.parameters.courant_number = 0.3 gridcode.set_has_external_gravitational_potential(1) nbodycode = PhiGRAPE(mode="gpu") #nbodycode = Octgrav(mode="gpu") nbodycode.initialize_code() nbodycode.parameters.epsilon_squared = ( self.size / (10000.0 * self.number_of_grid_points))**2 nbodycode.commit_parameters() result = HydroGridAndNbody(gridcode, nbodycode) return result
def init_integrator(method): if method == 'octgrav': gravity = Octgrav() elif method == 'ph4': gravity = ph4() elif method == 'fi': gravity = Fi(convert_nbody) gravity.initialize_code() gravity.parameters.epsilon_squared = 0.00000001 | units.AU**2 gravity.parameters.timestep = .10 | units.day elif method == 'phigrape': gravity = PhiGRAPE(convert_nbody, mode=PhiGRAPEInterface.MODE_GPU) elif method == 'bhtree': gravity = BHTree(convert_nbody, number_of_workes=workers) elif method == 'hermite': gravity = Hermite(convert_nbody, number_of_workers=workers #debugger = "xterm", #redirection = "none" ) gravity.parameters.epsilon_squared = 0.00001 | units.AU**2 return gravity
def test17(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = PhiGRAPE(convert_nbody, **default_test_options) instance.initialize_code() instance.parameters.set_defaults() instance.parameters.initial_timestep_parameter = 0.001 instance.parameters.timestep_parameter = 0.001 instance.parameters.epsilon_squared = 0.0 | units.AU**2 stars = self.new_system_of_sun_and_earth() earth = stars[1] instance.particles.add_particles(stars) instance.commit_particles() position_at_start = earth.position.value_in(units.AU)[0] print instance.particles[0].position.as_quantity_in(units.AU) print instance.particles[1].position.as_quantity_in(units.AU) instance.evolve_model(365 | units.day) print instance.particles[0].position.as_quantity_in(units.AU) print instance.particles[1].position.as_quantity_in(units.AU) instance.particles.copy_values_of_all_attributes_to(stars) position_after_full_rotation = earth.position.value_in(units.AU)[0] self.assertAlmostEqual(position_at_start, position_after_full_rotation, 3) print instance.particles[0].position.as_quantity_in(units.AU) instance.evolve_model(365.0 + (365.0 / 2) | units.day) print instance.particles[0].position.as_quantity_in(units.AU) instance.particles.copy_values_of_all_attributes_to(stars) position_after_half_a_rotation = earth.position.value_in(units.AU)[0] self.assertAlmostEqual(-position_at_start, position_after_half_a_rotation, 2) instance.evolve_model(365.0 + (365.0 / 2) + (365.0 / 4) | units.day) instance.particles.copy_values_of_all_attributes_to(stars) position_after_half_a_rotation = earth.position.value_in(units.AU)[1] #self.assertAlmostEqual(-position_at_start, position_after_half_a_rotation, 3) instance.cleanup_code() instance.stop()
def test16(self): instance = PhiGRAPE(number_of_workers = 2, **default_test_options) instance.initialize_code() instance.parameters.set_defaults() print 1 particles = datamodel.Particles(2) particles.x = [-1.0,1.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, 0.5] | nbody_system.mass instance.particles.add_particles(particles) instance.commit_particles() instance.evolve_model(0.01 | nbody_system.time) instance.particles.copy_values_of_all_attributes_to(particles) print instance.particles.position.x self.assertAlmostRelativeEquals(instance.particles[0].position.x, -0.999984741095 | nbody_system.length, 8) self.assertAlmostRelativeEquals(instance.particles[1].position.x, 0.999969482189 | nbody_system.length, 8) instance.evolve_model(0.10 | nbody_system.time) instance.particles.copy_values_of_all_attributes_to(particles) print instance.particles.position.x self.assertAlmostRelativeEquals(instance.particles[0].position.x, -0.999022960148 | nbody_system.length, 8) self.assertAlmostRelativeEquals(instance.particles[1].position.x, 0.998045920305 | nbody_system.length, 8) instance.evolve_model(0.50 | nbody_system.time) instance.particles.copy_values_of_all_attributes_to(particles) print instance.particles.position.x self.assertAlmostRelativeEquals(instance.particles[0].position.x, -0.984250788742 | nbody_system.length, 8) self.assertAlmostRelativeEquals(instance.particles[1].position.x, 0.968501583383 | nbody_system.length, 8) instance.cleanup_code() instance.stop()
def test15(self): instance = PhiGRAPE(number_of_workers = 2, **default_test_options)#, redirection = "none") instance.initialize_code() instance.parameters.set_defaults() print 1 particles = datamodel.Particles(2) particles.x = [-1.0,1.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.particles.add_particles(particles) instance.commit_particles() instance.evolve_model(0.01 | nbody_system.time) instance.particles.copy_values_of_all_attributes_to(particles) print instance.particles.position.x self.assertEquals(instance.particles[0].position.x, -instance.particles[1].position.x) self.assertAlmostRelativeEquals(instance.particles[1].position.x, 0.999969482111 | nbody_system.length, 6) instance.evolve_model(0.10 | nbody_system.time) instance.particles.copy_values_of_all_attributes_to(particles) print instance.particles.position.x self.assertEquals(instance.particles[0].position.x, -instance.particles[1].position.x) self.assertAlmostRelativeEquals(instance.particles[1].position.x, 0.99804560161 | nbody_system.length, 6) instance.evolve_model(0.50 | nbody_system.time) instance.particles.copy_values_of_all_attributes_to(particles) print instance.particles.position.x self.assertEquals(instance.particles[0].position.x, -instance.particles[1].position.x) self.assertAlmostRelativeEquals(instance.particles[1].position.x, 0.968416814302 | nbody_system.length, 6) instance.cleanup_code() instance.stop()
def test2(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = PhiGRAPE(convert_nbody, **default_test_options) instance.initialize_code() instance.parameters.set_defaults() stars = self.new_system_of_sun_and_earth() earth = stars[1] instance.particles.add_particles(stars) instance.commit_particles() for x in range(1,365,30): instance.evolve_model(x | units.day) instance.particles.copy_values_of_all_attributes_to(stars) stars.savepoint() if HAS_MATPLOTLIB: figure = pyplot.figure() plot = figure.add_subplot(1,1,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') 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, "phiGRAPE-earth-sun2.svg") figure.savefig(output_file) instance.cleanup_code() instance.stop()
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 test2(self): instance = PhiGRAPE() self._run_addition_removal_test(instance)
def test9(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = PhiGRAPE(convert_nbody, **default_test_options)#, debugger="xterm") instance.initialize_code() instance.parameters.set_defaults() instance.parameters.initialize_gpu_once = 1 stars = self.new_system_of_sun_and_earth() earth = stars[1] instance.particles.add_particles(stars) instance.commit_particles() instance.evolve_model(365 | units.day) instance.particles.copy_values_of_all_attributes_to(stars) position_at_start = earth.position.value_in(units.AU)[0] position_after_full_rotation = earth.position.value_in(units.AU)[0] self.assertAlmostEqual(position_at_start, position_after_full_rotation, 6) instance.evolve_model(365.0 + (365.0 / 2) | units.day) instance.particles.copy_values_of_all_attributes_to(stars) position_after_half_a_rotation = earth.position.value_in(units.AU)[0] self.assertAlmostEqual(-position_at_start, position_after_half_a_rotation, 2) instance.evolve_model(365.0 + (365.0 / 2) + (365.0 / 4) | units.day) instance.particles.copy_values_of_all_attributes_to(stars) position_after_half_a_rotation = earth.position.value_in(units.AU)[1] #self.assertAlmostEqual(-position_at_start, position_after_half_a_rotation, 3) instance.cleanup_code() instance.stop()
def test8(self): particles = datamodel.Particles(6) particles.mass = nbody_system.mass.new_quantity(range(1,7)) 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 for current_mode in ['g6lib','gpu','grape']: try: instance = PhiGRAPE(mode = current_mode) except: print "Running PhiGRAPE with mode=" + current_mode, "was unsuccessful." else: print "Running PhiGRAPE with mode=" + current_mode + "...", instance.initialize_code() instance.particles.add_particles(particles) instance.commit_particles() instance.evolve_model(0.1 | nbody_system.time) instance.cleanup_code() instance.stop() print "ok"
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
orbit_frac = 0.5 print "orbit_frac=%s" % (orbit_frac, ) # case 1: central body with an orbiting planet T = two_body_orbital_period(m1=1 / 20000, m2=10e-10, a=4.8481e-6, e=0.5) print "two_body_central period=%s" % (T, ) r = nbe.NBodyComputation(initialConditions=two_body_initial_conditions( m1=1 / 20000, m2=10e-10, a=4.8481e-6, e=0.5), dt=T / 100 | nbody_system.time, tFinal=orbit_frac * T | nbody_system.time, analyticSolution=None, ndim=2, outfName="two_body_central") # run the problem on two integrators r.runProblemOnIntegrator(PhiGRAPE(), label="PhiGRAPE", printProgress=False) r.runProblemOnIntegrator(Hermite(), label="Hermite", printProgress=False) hc = huayno_cc.Huayno(redirection='none') hc.initialize_code() hc.set_inttype_parameter(huayno_cc.Huayno.EVOLVE_HOLD_DKD_CC) hc.set_timestep_parameter(0.01) r.runProblemOnIntegrator(hc, label="Huayno_CC", printProgress=False) # plot a comparison in energy r.plotTotalEnergyRelativeError(labels=["PhiGRAPE", "Hermite", "Huayno_CC"]) # plot trajectories r.plotTrajectories(label="PhiGRAPE") r.plotTrajectories(label="Hermite") r.plotTrajectories(label="Huayno_CC")
def new_gravity_code(converter, timestep=0.0025 | units.Myr): result = PhiGRAPE(converter) result.parameters.epsilon_squared = (0.01 | units.parsec)**2 return result
sat_two = stars[2] sat_two.mass = units.kg(1000) sat_two.radius = units.m(10) sat_two.position = units.km(np.array((6371 + 242, 100.0, 0.0))) sat_two.velocity = units.ms(np.array((0.0, 27359 / 3.6, 0.0))) convert_nbody = nbody_system.nbody_to_si(5.9736e24 | units.kg, 6371 | units.km) s = Viewer(stars, (640, 480), convert_nbody) #stars.scale_to_standard() if method == 'octgrav': gravity = Octgrav() elif method == 'phigrape': gravity = PhiGRAPE(convert_nbody) elif method == 'bhtree': gravity = BHTree(convert_nbody, number_of_workes=workers) elif method == 'hermite': gravity = Hermite(convert_nbody, number_of_workers=workers #debugger = "xterm", #redirection = "none" ) gravity.initialize_code() gravity.parameters.epsilon_squared = 0.001 | nbody_system.length**2 #stars.radius = 0.000001 | nbody_system.length gravity.particles.add_particles(stars) #gravity.stopping_conditions.pair_detection.enable() from_model_to_gravity = stars.new_channel_to(gravity.particles)
ZC = ZRES / 2.0 if __name__ == "__main__": nstars = int(sys.argv[1]) workers = int(sys.argv[2]) method = sys.argv[3] print nstars seed = None stars = new_plummer_model( nstars) #, convert_nbody, random_state = seed).result if method == 'octgrav': gravity = Octgrav() elif method == 'phigrape': gravity = PhiGRAPE(PhiGRAPE.NBODY) elif method == 'bhtree': gravity = BHTree(number_of_workes=workers) elif method == 'hermite': gravity = Hermite(number_of_workers=workers #debugger = "xterm", #redirection = "none" ) gravity.initialize_code() gravity.parameters.epsilon_squared = 0.001 | nbody_system.length**2 stars.radius = 0.000001 | nbody_system.length gravity.particles.add_particles(stars) gravity.stopping_conditions.pair_detection.enable() from_model_to_gravity = stars.new_channel_to(gravity.particles) from_gravity_to_model = gravity.particles.new_channel_to(stars)
if __name__ in ('__main__', '__plot__'): # set up parameters: N = 100 W0 = 3 Rinit = 50. | units.parsec timestep = 0.01 | units.Myr Mcluster = 4.e4 | units.MSun Rcluster = 0.7 | units.parsec converter = nbody_system.nbody_to_si(Mcluster,Rcluster) # create a globular cluster model particles = new_king_model(N, W0, convert_nbody=converter) particles.radius = 0.0| units.parsec cluster = PhiGRAPE(converter, parameters=[("epsilon_squared", (0.01 | units.parsec)**2)]) # create the external potential of the Galaxy galaxy = Agama(converter, type="Dehnen", gamma=1.8, \ rscale=1000.| units.parsec, mass=1.6e10 | units.MSun, channel_type='sockets') # shift the cluster to an orbit around Galactic center acc,_,_ = galaxy.get_gravity_at_point(0|units.kpc, Rinit, 0|units.kpc, 0|units.kpc) vcirc = (-acc * Rinit)**0.5 print "Vcirc=", vcirc.value_in(units.kms), "km/s" particles.x += Rinit particles.vy += vcirc cluster.particles.add_particles(particles) # set up bridge; cluster is evolved under influence of the galaxy sys = bridge(verbose=False)