def test15(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.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 instance = SmallN() instance.particles.add_particles(particles) instance.commit_particles() self.assertEquals(instance.particles[0].radius, 0.0 | nbody_system.length) p = datamodel.Particle( x = 1.0 | nbody_system.length, y = 2.0 | nbody_system.length, z = 3.0 | nbody_system.length, vx = 1.0 | nbody_system.speed, vy = 2.0 | nbody_system.speed, vz = 3.0 | nbody_system.speed, mass = 1.0 | nbody_system.mass, radius = 4.0 | nbody_system.length, ) instance.particles.add_particle(p) self.assertEquals(instance.particles[0].radius, 0.0 | nbody_system.length) self.assertEquals(instance.particles[1].radius, 0.0 | nbody_system.length) self.assertEquals(instance.particles[2].radius, 4.0 | nbody_system.length) instance.stop()
def test17(self): particles = datamodel.Particles(keys=[1, 2, 3, 4, 5, 6, 7]) particles.mass = 0.001 | nbody_system.mass particles.radius = 0.1 | nbody_system.length particles.x = [-100.5, -99.5, -0.5, 0.5, 99.5, 100.5, 120.0 ] | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.velocity = [[2, 0, 0], [-2, 0, 0], [2, 0, 0], [-2, 0, 0], [2, 0, 0], [-2, 0, 0], [-4, 0, 0] ] | nbody_system.speed instance = SmallN() 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])
def initialize_isOverCode(**kwargs): converter = kwargs.get("converter", None) if converter == None: converter = nbody_system.nbody_to_si(1 | units.MSun, 100 | units.AU) isOverCode = SmallN(redirection="none", convert_nbody=converter) isOverCode.initialize_code() isOverCode.parameters.set_defaults() isOverCode.parameters.allow_full_unperturbed = 0 return isOverCode
def test3(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = SmallN(convert_nbody) instance.initialize_code() instance.dt_dia = 5000 stars = datamodel.Stars(2) star1 = stars[0] star2 = stars[1] star1.mass = units.MSun(1.0) star1.position = units.AU(numpy.array((-1.0, 0.0, 0.0))) star1.velocity = units.AUd(numpy.array((0.0, 0.0, 0.0))) star1.radius = units.RSun(1.0) star2.mass = units.MSun(1.0) star2.position = units.AU(numpy.array((1.0, 0.0, 0.0))) star2.velocity = units.AUd(numpy.array((0.0, 0.0, 0.0))) star2.radius = units.RSun(100.0) instance.particles.add_particles(stars) for x in range(1, 2000, 10): instance.evolve_model(x | units.day) instance.particles.copy_values_of_all_attributes_to(stars) stars.savepoint() instance.stop()
def test6(self): print("Test6: Testing SmallN parameters") convert_nbody = nbody_system.nbody_to_si(1.0 | units.yr, 1.0 | units.AU) instance = SmallN(convert_nbody) instance.initialize_code() value = instance.get_eta() self.assertEqual(0.14, value) self.assertAlmostEqual(0.14, instance.parameters.timestep_parameter) for x in [0.001, 0.01, 0.1]: instance.parameters.timestep_parameter = x self.assertAlmostEqual(x, instance.parameters.timestep_parameter) value = instance.get_time() self.assertEqual(0 | units.yr, value) value = instance.get_gamma() self.assertEqual(1e-6, value) self.assertAlmostEqual(1e-6, instance.parameters.unperturbed_threshold) for x in [0.001, 0.01, 0.1]: instance.parameters.unperturbed_threshold = x self.assertAlmostEqual(x, instance.parameters.unperturbed_threshold) instance.stop()
def test5(self): convert_nbody = nbody_system.nbody_to_si(5.0 | units.kg, 10.0 | units.m) instance = SmallN(convert_nbody) instance.initialize_code() particles = datamodel.Particles(2) self.assertEqual(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.assertEqual(len(instance.particles), 2) instance.set_state(1, 16 | units.kg, 20.0 | units.m, 40.0 | units.m, 60.0 | units.m, 1.0 | units.ms, 1.0 | units.ms, 1.0 | units.ms, 20.0 | units.m) curr_state = instance.get_state(1) for expected, actual in zip([ 16 | units.kg, 20.0 | units.m, 40.0 | units.m, 60.0 | units.m, 1.0 | units.ms, 1.0 | units.ms, 1.0 | units.ms, 20.0 | units.m ], curr_state): self.assertAlmostRelativeEquals(expected, actual) instance.stop() self.assertEqual(curr_state[0], 16 | units.kg, 8)
def test20(self): p = datamodel.Particles(3) p[0].mass = 6.667e-01 | nbody_system.mass p[0].radius = 4.000e-03 | nbody_system.length p[0].x = -1.309e+01 | nbody_system.length p[0].y = 1.940e+01 | nbody_system.length p[0].z = -1.163e+01 | nbody_system.length p[0].vx = 2.366e-01 | nbody_system.speed p[0].vy = -3.813e-01 | nbody_system.speed p[0].vz = 2.486e-01 | nbody_system.speed p[1].mass = 3.333e-01 | nbody_system.mass p[1].radius = 1.000e-03 | nbody_system.length p[1].x = -1.506e+01 | nbody_system.length p[1].y = 1.937e+01 | nbody_system.length p[1].z = -1.163e+01 | nbody_system.length p[1].vx = 3.483e-01 | nbody_system.speed p[1].vy = -4.513e-01 | nbody_system.speed p[1].vz = 2.486e-01 | nbody_system.speed p[2].mass = 5.000e-01 | nbody_system.mass p[2].radius = 2.000e-03 | nbody_system.length p[2].x = 2.749e+01 | nbody_system.length p[2].y = -3.877e+01 | nbody_system.length p[2].z = 2.325e+01 | nbody_system.length p[2].vx = -5.476e-01 | nbody_system.speed p[2].vy = 8.092e-01 | nbody_system.speed p[2].vz = -4.972e-01 | nbody_system.speed instance = SmallN() instance.initialize_code() instance.parameters.set_defaults N = 3 t_begin = 0.0 | nbody_system.time t_end = 100.0 | nbody_system.time particles = p instance.particles.add_particles(particles) instance.commit_particles() sc = instance.stopping_conditions.collision_detection sc.enable() isCollision = False instance.evolve_model(t_end) isCollision = sc.is_set() instance.stop() self.assertTrue(isCollision, "no collision detected")
def test3(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = SmallN(convert_nbody) instance.initialize_code() instance.dt_dia = 5000 stars = datamodel.Stars(2) star1 = stars[0] star2 = stars[1] star1.mass = units.MSun(1.0) star1.position = units.AU(numpy.array((-1.0,0.0,0.0))) star1.velocity = units.AUd(numpy.array((0.0,0.0,0.0))) star1.radius = units.RSun(1.0) star2.mass = units.MSun(1.0) star2.position = units.AU(numpy.array((1.0,0.0,0.0))) star2.velocity = units.AUd(numpy.array((0.0,0.0,0.0))) star2.radius = units.RSun(100.0) instance.particles.add_particles(stars) for x in range(1,2000,10): instance.evolve_model(x | units.day) instance.particles.copy_values_of_all_attributes_to(stars) stars.savepoint() instance.stop()
def test5(self): convert_nbody = nbody_system.nbody_to_si(5.0 | units.kg, 10.0 | units.m) instance = SmallN(convert_nbody) instance.initialize_code() 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.set_state(1, 16|units.kg, 20.0|units.m, 40.0|units.m, 60.0|units.m, 1.0|units.ms, 1.0|units.ms, 1.0|units.ms , 20.0|units.m) curr_state = instance.get_state(1) for expected, actual in zip([16|units.kg, 20.0|units.m, 40.0|units.m, 60.0|units.m, 1.0|units.ms, 1.0|units.ms, 1.0|units.ms , 20.0|units.m], curr_state): self.assertAlmostRelativeEquals(expected, actual) instance.stop() self.assertEquals(curr_state[0], 16|units.kg, 8)
def test6(self): print "Test6: Testing SmallN parameters" convert_nbody = nbody_system.nbody_to_si(1.0 | units.yr, 1.0 | units.AU) instance = SmallN(convert_nbody) instance.initialize_code() value = instance.get_eta() self.assertEquals(0.14, value) self.assertAlmostEquals(0.14, instance.parameters.timestep_parameter) for x in [0.001, 0.01, 0.1]: instance.parameters.timestep_parameter = x self.assertAlmostEquals(x, instance.parameters.timestep_parameter) value = instance.get_time() self.assertEquals(0| units.yr, value) value = instance.get_gamma() self.assertEquals(1e-6, value) self.assertAlmostEquals(1e-6, instance.parameters.unperturbed_threshold) for x in [0.001, 0.01, 0.1]: instance.parameters.unperturbed_threshold = x self.assertAlmostEquals(x, instance.parameters.unperturbed_threshold) instance.stop()
def init_smalln(): global SMALLN sys.stdout.flush() SMALLN = SmallN() sys.stdout.flush() SMALLN.parameters.timestep_parameter = 0.1 #SMALLN.parameters.cm_index = 2001 # don't set this here!! sys.stdout.flush()
def new_smalln_si(self): if not self.previous is None: self.previous.stop() converter = nbody_system.nbody_to_si(units.MSun, units.parsec) result = SmallN(converter) result.parameters.timestep_parameter = 0.1 result.parameters.cm_index = 2001 return result
def new_smalln(self): if not self.previous is None: self.previous.stop() result = SmallN() result.parameters.timestep_parameter = 0.1 result.parameters.cm_index = 2001 self.previous = result return result
def test16(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) smalln = SmallN(convert_nbody) smalln.initialize_code() smalln.dt_dia = 5000 stars = self.new_system_of_sun_and_earth() moon = datamodel.Particle() moon.mass = units.kg(7.3477e22) moon.radius = units.km(1737.10) moon.position = units.km(numpy.array((149.5e6 + 384.399 ,0.0,0.0))) moon.velocity = units.ms(numpy.array((0.0,29800 + 1022,0.0))) stars.add_particle(moon) earth = stars[1] smalln.particles.add_particles(stars) smalln.evolve_model(365.0 | units.day) smalln.update_particle_tree() smalln.update_particle_set() self.assertEquals(len(smalln.particles), 5) self.assertEarthAndMoonWasDetectedAsBinary(smalln.particles, stars) inmemory = smalln.particles.copy() self.assertEarthAndMoonWasDetectedAsBinary(inmemory, stars) test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "newsmalln-test16.hdf5") if os.path.exists(output_file): os.remove(output_file) io.write_set_to_file(smalln.particles, output_file, "hdf5") fromfile = io.read_set_from_file(output_file, "hdf5") self.assertEarthAndMoonWasDetectedAsBinary(fromfile, stars)
def test18(self): particles = datamodel.Particles(keys=[1, 2]) particles.mass = 1 | nbody_system.mass particles.radius = 0.1 | nbody_system.length particles.x = [1, -1] | nbody_system.length particles.y = [1, -1] | nbody_system.length particles.z = 0 | nbody_system.length particles.velocity = [[-1, 0, 0], [1, 0, 0]] | nbody_system.speed instance = SmallN() instance.particles.add_particles(particles) stopping_condition = instance.stopping_conditions.interaction_over_detection stopping_condition.enable() instance.evolve_model(10.0 | nbody_system.time) self.assertTrue(stopping_condition.is_set()) self.assertTrue(instance.model_time < 11.0 | nbody_system.time)
def test18(self): particles = datamodel.Particles(keys=[1,2]) particles.mass = 1 | nbody_system.mass particles.radius = 0.1 | nbody_system.length particles.x = [1, -1] | nbody_system.length particles.y = [1, -1] | nbody_system.length particles.z = 0 | nbody_system.length particles.velocity = [[-1, 0, 0], [1, 0, 0]] | nbody_system.speed instance = SmallN() instance.particles.add_particles(particles) stopping_condition = instance.stopping_conditions.interaction_over_detection stopping_condition.enable() instance.evolve_model(10.0 | nbody_system.time) self.assertTrue(stopping_condition.is_set()) self.assertTrue(instance.model_time < 11.0 | nbody_system.time)
def test15(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.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 instance = SmallN() instance.particles.add_particles(particles) instance.commit_particles() self.assertEqual(instance.particles[0].radius, 0.0 | nbody_system.length) p = datamodel.Particle( x=1.0 | nbody_system.length, y=2.0 | nbody_system.length, z=3.0 | nbody_system.length, vx=1.0 | nbody_system.speed, vy=2.0 | nbody_system.speed, vz=3.0 | nbody_system.speed, mass=1.0 | nbody_system.mass, radius=4.0 | nbody_system.length, ) instance.particles.add_particle(p) self.assertEqual(instance.particles[0].radius, 0.0 | nbody_system.length) self.assertEqual(instance.particles[1].radius, 0.0 | nbody_system.length) self.assertEqual(instance.particles[2].radius, 4.0 | nbody_system.length) instance.stop()
def test2(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = SmallN(convert_nbody) instance.initialize_code() instance.dt_dia = 5000 stars = self.new_system_of_sun_and_earth() earth = stars[1] instance.particles.add_particles(stars) for x in range(1,2000,10): 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, "smalln-earth-sun2.svg") figure.savefig(output_file) instance.cleanup_code() instance.stop()
def test17(self): particles = datamodel.Particles(keys=[1,2,3,4,5,6,7]) particles.mass = 0.001 | nbody_system.mass particles.radius = 0.1 | nbody_system.length particles.x = [ -100.5, -99.5, -0.5, 0.5, 99.5, 100.5, 120.0 ] | nbody_system.length particles.y = 0 | nbody_system.length particles.z = 0 | nbody_system.length particles.velocity = [ [2, 0, 0], [-2, 0, 0], [2, 0, 0], [-2, 0, 0], [2, 0, 0], [-2, 0, 0], [-4, 0, 0] ] | nbody_system.speed instance = SmallN() 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]) instance.stop()
def test4(self): convert_nbody = nbody_system.nbody_to_si(5.0 | units.kg, 10.0 | units.m) instance = SmallN(convert_nbody) instance.initialize_code() particles = datamodel.Particles(2) self.assertEqual(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.assertEqual(len(instance.particles), 2) instance.particles.mass = [17.0, 33.0] | units.kg self.assertEqual(instance.get_mass(1), 17.0 | units.kg) self.assertEqual(instance.get_mass(2), 33.0 | units.kg) instance.stop()
def test4(self): convert_nbody = nbody_system.nbody_to_si(5.0 | units.kg, 10.0 | units.m) instance = SmallN(convert_nbody) instance.initialize_code() 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 run_smallN(particles, end_time=1000 | nbody_system.time, delta_t=10 | nbody_system.time, accuracy_parameter=0.1): gravity = SmallN(redirection="none") # , debugger="gdb") gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.timestep_parameter = accuracy_parameter gravity.parameters.cm_index = 2001 gravity.commit_parameters() time = 0 | nbody_system.time print("\nadding particles to smallN") sys.stdout.flush() gravity.set_time(time) gravity.particles.add_particles(particles) print("committing particles to smallN") gravity.commit_particles() print("smallN: number_of_stars =", len(particles)) print("smallN: evolving to time =", end_time.number, end=' ') print("in steps of", delta_t.number) sys.stdout.flush() E0 = print_log('smallN', gravity) # Channel to copy values from the code to the set in memory. channel = gravity.particles.new_channel_to(particles) while time < end_time: time += delta_t print('evolving smallN to time', time.number) sys.stdout.flush() gravity.evolve_model(time) print_log('smallN', gravity, E0) over = gravity.is_over() if over.number: print('interaction is over\n') sys.stdout.flush() # Create a tree in the module representing the binary structure. gravity.update_particle_tree() # Return the tree structure to AMUSE. Children are # identified by get_children_of_particle in interface.??, # and the information is returned in the copy operation. gravity.update_particle_set() gravity.particles.synchronize_to(particles) channel.copy() channel.copy_attribute("index_in_code", "id") gravity.stop() # Basic diagnostics: BinaryTreesOnAParticleSet creates # binary tree structure for all particles in the set; then # we loop over roots (top-level nodes) and print data on # all binaries below each. print("smallN binaries:") sys.stdout.flush() x = trees.BinaryTreesOnAParticleSet(particles, "child1", "child2") roots = list(x.iter_roots()) for r in roots: for level, particle in r.iter_levels(): print(' ' * level, int(particle.id.number), end=' ') if not particle.child1 is None: M, a, e, r, E = get_cm_binary_elements(particle) print(" mass = %.5e" % (M.number)) m1 = particle.child1.mass m2 = particle.child2.mass print_elements(' ', a, e, r, E * m1 * m2 / M) else: print('') sys.stdout.flush() return E0 sys.stdout.flush() gravity.stop() raise Exception("Did not finish the small-N simulation " + "before end time {0}".format(end_time))
def new_smalln(): result = SmallN() result.parameters.timestep_parameter = 0.1 #result.parameters.cm_index = 2001 return result
def init_smalln(): global SMALLN SMALLN = SmallN()
def test16(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) smalln = SmallN(convert_nbody) smalln.initialize_code() smalln.dt_dia = 5000 stars = self.new_system_of_sun_and_earth() moon = datamodel.Particle() moon.mass = units.kg(7.3477e22) moon.radius = units.km(1737.10) moon.position = units.km(numpy.array((149.5e6 + 384.399, 0.0, 0.0))) moon.velocity = units.ms(numpy.array((0.0, 29800 + 1022, 0.0))) stars.add_particle(moon) earth = stars[1] smalln.particles.add_particles(stars) smalln.evolve_model(365.0 | units.day) smalln.update_particle_tree() smalln.update_particle_set() self.assertEqual(len(smalln.particles), 5) self.assertEarthAndMoonWasDetectedAsBinary(smalln.particles, stars) inmemory = smalln.particles.copy() self.assertEarthAndMoonWasDetectedAsBinary(inmemory, stars) test_results_path = self.get_path_to_results() output_file = os.path.join(test_results_path, "newsmalln-test16.hdf5") if os.path.exists(output_file): os.remove(output_file) io.write_set_to_file(smalln.particles, output_file, "hdf5") fromfile = io.read_set_from_file(output_file, "hdf5") self.assertEarthAndMoonWasDetectedAsBinary(fromfile, stars) smalln.stop()
def test1(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) smalln = SmallN(convert_nbody) smalln.initialize_code() smalln.dt_dia = 5000 stars = self.new_system_of_sun_and_earth() earth = stars[1] smalln.particles.add_particles(stars) smalln.evolve_model(365.0 | units.day) smalln.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) smalln.evolve_model(365.0 + (365.0 / 2) | units.day) smalln.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) smalln.evolve_model(365.0 + (365.0 / 2) + (365.0 / 4) | units.day) smalln.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) smalln.cleanup_code() smalln.stop()
assert is_mpd_running() # Instantiate worker modules once only and pass them to scatter32 # as arguments. # Kepler manages two-body dynamics. kep = Kepler(redirection="none") #, debugger="gdb") kep.initialize_code() kep.set_random(random_seed) # ** Note potential conflict between C++ # ** and Python random generators. # Gravity manages the N-body integration. gravity = SmallN(redirection="none") gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.timestep_parameter = accuracy_parameter gravity.parameters.unperturbed_threshold = gamma # Treecheck determines the structure of the 3-body system. # Treecheck = gravity is OK. treecheck = SmallN(redirection="none") treecheck.initialize_code() treecheck.parameters.set_defaults() treecheck.parameters.timestep_parameter = accuracy_parameter treecheck.parameters.unperturbed_threshold = gamma # Timing:
def new_smalln(self): result = SmallN() result.parameters.timestep_parameter = 0.1 result.parameters.cm_index = 50000 return result
def new_smalln(): code = SmallN() code.initialize_code() code.parameters.set_defaults() return code
def run_collision(GravitatingBodies, end_time, delta_time, save_file, **kwargs): # Define Additional User Options and Set Defaults Properly converter = kwargs.get("converter", None) doEncPatching = kwargs.get("doEncPatching", False) doVerboseSaves = kwargs.get("doVerboseSaves", False) if converter == None: converter = nbody_system.nbody_to_si(GravitatingBodies.mass.sum(), 2 * np.max(GravitatingBodies.radius.number) | GravitatingBodies.radius.unit) # Storing Initial Center of Mass Information for the Encounter rCM_i = GravitatingBodies.center_of_mass() vCM_i = GravitatingBodies.center_of_mass_velocity() # Fixing Stored Encounter Particle Set to Feed into SmallN GravitatingBodies = Particles(particles=GravitatingBodies) if 'child1' in GravitatingBodies.get_attribute_names_defined_in_store(): del GravitatingBodies.child1, GravitatingBodies.child2 # Moving the Encounter's Center of Mass to the Origin and Setting it at Rest GravitatingBodies.position -= rCM_i GravitatingBodies.velocity -= vCM_i # Setting Up Gravity Code gravity = SmallN(redirection = 'none', convert_nbody = converter) gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.allow_full_unperturbed = 0 gravity.particles.add_particles(GravitatingBodies) # adds bodies to gravity calculations gravity.commit_particles() channel_from_grav_to_python = gravity.particles.new_channel_to(GravitatingBodies) channel_from_grav_to_python.copy() # Setting Coarse Timesteps list_of_times = np.arange(0. | units.yr, end_time, delta_time) stepNumber = 0 # Integrate the Encounter Until Over ... for current_time in list_of_times: # Evolve the Model to the Desired Current Time gravity.evolve_model(current_time) # Update Python Set in In-Code Set channel_from_grav_to_python.copy() # original channel_from_grav_to_python.copy_attribute("index_in_code", "id") # Handle Writing Output of Integration if doVerboseSaves: # Write a Save Every Coarse Timestep write_set_to_file(GravitatingBodies.savepoint(current_time), save_file, 'hdf5') else: # Write a Save at the Begninning, Middle & End Times if stepNumber==0 or stepNumber==len(list_of_times) or stepNumber==len(list_of_times)/2: # Write Set to File write_set_to_file(GravitatingBodies.savepoint(current_time), save_file, 'hdf5') # Check to See if the Encounter is Declared "Over" Every 50 Timesteps if stepNumber%50: over = gravity.is_over() if over: gravity.update_particle_tree() gravity.update_particle_set() gravity.particles.synchronize_to(GravitatingBodies) channel_from_grav_to_python.copy() print "Encounter has finished at Step #", stepNumber break else: print "Encounter has NOT finished at Step #", stepNumber stepNumber +=1 # Stop the Gravity Code Once the Encounter Finishes gravity.stop() # Seperate out the Systems to Prepare for Encounter Patching if doEncPatching: ResultingPSystems = stellar_systems.get_planetary_systems_from_set(GravitatingBodies, converter=converter, RelativePosition=True) else: ResultingPSystems = stellar_systems.get_planetary_systems_from_set(GravitatingBodies, converter=converter, RelativePosition=False) return ResultingPSystems
def init_smalln(converter): global SMALLN SMALLN = SmallN(convert_nbody=converter)
def test2(self): convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km) instance = SmallN(convert_nbody) instance.initialize_code() instance.dt_dia = 5000 stars = self.new_system_of_sun_and_earth() earth = stars[1] instance.particles.add_particles(stars) for x in range(1, 2000, 10): 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 = [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') 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, "smalln-earth-sun2.svg") figure.savefig(output_file) instance.cleanup_code() instance.stop()
def run_smallN( particles, end_time = 1000 | nbody_system.time, delta_t = 10 | nbody_system.time, accuracy_parameter = 0.1 ): gravity = SmallN(redirection = "none") # , debugger="gdb") gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.timestep_parameter = accuracy_parameter gravity.parameters.cm_index = 2001 gravity.commit_parameters() time = 0 | nbody_system.time print "\nadding particles to smallN" sys.stdout.flush() gravity.set_time(time); gravity.particles.add_particles(particles) print "committing particles to smallN" gravity.commit_particles() print "smallN: number_of_stars =", len(particles) print "smallN: evolving to time =", end_time.number, print "in steps of", delta_t.number sys.stdout.flush() E0 = print_log('smallN', gravity) # Channel to copy values from the code to the set in memory. channel = gravity.particles.new_channel_to(particles) while time < end_time: time += delta_t print 'evolving smallN to time', time.number sys.stdout.flush() gravity.evolve_model(time) print_log('smallN', gravity, E0) over = gravity.is_over() if over.number: print 'interaction is over\n'; sys.stdout.flush() # Create a tree in the module representing the binary structure. gravity.update_particle_tree() # Return the tree structure to AMUSE. Children are # identified by get_children_of_particle in interface.??, # and the information is returned in the copy operation. gravity.update_particle_set() gravity.particles.synchronize_to(particles) channel.copy() channel.copy_attribute("index_in_code", "id") gravity.stop() # Basic diagnostics: BinaryTreesOnAParticleSet creates # binary tree structure for all particles in the set; then # we loop over roots (top-level nodes) and print data on # all binaries below each. print "smallN binaries:"; sys.stdout.flush() x = trees.BinaryTreesOnAParticleSet(particles, "child1", "child2") roots = list(x.iter_roots()) for r in roots: for level, particle in r.iter_levels(): print ' '*level, int(particle.id.number), if not particle.child1 is None: M,a,e,r,E = get_cm_binary_elements(particle) print " mass = %.5e" % (M.number) m1 = particle.child1.mass m2 = particle.child2.mass print_elements(' ', a, e, r, E*m1*m2/M) else: print '' sys.stdout.flush() return E0 sys.stdout.flush() gravity.stop() raise Exception("Did not finish the small-N simulation " +"before end time {0}".format(end_time))
def init_smalln(): global SMALLN SMALLN = SmallN(redirection="none") SMALLN.parameters.timestep_parameter = 0.05
def evolve_triple_with_wind(M1, M2, M3, Pora, Pin_0, ain_0, aout_0, ein_0, eout_0, t_end, nsteps, scheme, integrator, t_stellar, dt_se, dtse_fac, interp): import random from amuse.ext.solarsystem import get_position numpy.random.seed(42) print("Initial masses:", M1, M2, M3) triple = Particles(3) triple[0].mass = M1 triple[1].mass = M2 triple[2].mass = M3 stellar = SeBa() stellar.particles.add_particles(triple) channel_from_stellar = stellar.particles.new_channel_to(triple) # Evolve to t_stellar. stellar.evolve_model(t_stellar) channel_from_stellar.copy_attributes(["mass"]) M1 = triple[0].mass M2 = triple[1].mass M3 = triple[2].mass print("t=", stellar.model_time.in_(units.Myr)) print("M=", stellar.particles.mass.in_(units.MSun)) print("R=", stellar.particles.radius.in_(units.RSun)) print("L=", stellar.particles.luminosity.in_(units.LSun)) print("T=", stellar.particles.temperature.in_(units.K)) print("Mdot=", \ -stellar.particles.wind_mass_loss_rate.in_(units.MSun/units.yr)) # Start the dynamics. # Inner binary: tmp_stars = Particles(2) tmp_stars[0].mass = M1 tmp_stars[1].mass = M2 if Pora == 1: ain_0 = semimajor_axis(Pin_0, M1+M2) else: Pin_0 = orbital_period(ain_0, M1+M2) print('Pin =', Pin_0) print('ain_0 =', ain_0) print('M1+M2 =', M1+M2) print('Pin_0 =', Pin_0.value_in(units.day), '[day]') #print 'semi:', semimajor_axis(Pin_0, M1+M2).value_in(units.AU), 'AU' #print 'period:', orbital_period(ain_0, M1+M2).value_in(units.day), '[day]' dt_init = 0.01*Pin_0 ma = 180 inc = 60 aop = 180 lon = 0 r,v = get_position(M1, M2, ein_0, ain_0, ma, inc, aop, lon, dt_init) tmp_stars[1].position = r tmp_stars[1].velocity = v tmp_stars.move_to_center() # Outer binary: r,v = get_position(M1+M2, M3, eout_0, aout_0, 0, 0, 0, 0, dt_init) tertiary = Particle() tertiary.mass = M3 tertiary.position = r tertiary.velocity = v tmp_stars.add_particle(tertiary) tmp_stars.move_to_center() triple.position = tmp_stars.position triple.velocity = tmp_stars.velocity Mtriple = triple.mass.sum() Pout = orbital_period(aout_0, Mtriple) print("T=", stellar.model_time.in_(units.Myr)) print("M=", stellar.particles.mass.in_(units.MSun)) print("Pout=", Pout.in_(units.Myr)) print('tK =', ((M1+M2)/M3)*Pout**2*(1-eout_0**2)**1.5/Pin_0) converter = nbody_system.nbody_to_si(triple.mass.sum(), aout_0) if integrator == 0: gravity = Hermite(converter) gravity.parameters.timestep_parameter = 0.01 elif integrator == 1: gravity = SmallN(converter) gravity.parameters.timestep_parameter = 0.01 gravity.parameters.full_unperturbed = 0 elif integrator == 2: gravity = Huayno(converter) gravity.parameters.inttype_parameter = 20 gravity.parameters.timestep = (1./256)*Pin_0 else: gravity = symple(converter) gravity.parameters.integrator = 10 #gravity.parameters.timestep_parameter = 0. gravity.parameters.timestep = (1./128)*Pin_0 print(gravity.parameters) gravity.particles.add_particles(triple) channel_from_framework_to_gd = triple.new_channel_to(gravity.particles) channel_from_gd_to_framework = gravity.particles.new_channel_to(triple) Etot_init = gravity.kinetic_energy + gravity.potential_energy Etot_prev = Etot_init gravity.particles.move_to_center() # Note: time = t_diag = 0 at the start of the dynamical integration. dt_diag = t_end/float(nsteps) t_diag = dt_diag time = 0.0 | t_end.unit t_se = t_stellar + time print('t_end =', t_end) print('dt_diag =', dt_diag) ain, ein, aout, eout = get_orbital_elements_of_triple(triple) print("Triple elements t=", time, \ "inner:", triple[0].mass, triple[1].mass, ain, ein, \ "outer:", triple[2].mass, aout, eout) t = [time.value_in(units.Myr)] Mtot = triple.mass.sum() mtot = [Mtot.value_in(units.MSun)] smai = [ain/ain_0] ecci = [ein/ein_0] smao = [aout/aout_0] ecco = [eout/eout_0] if interp: # Create arrays of stellar times and masses for interpolation. times = [time] masses = [triple.mass.copy()] while time < t_end: time += dt_se stellar.evolve_model(t_stellar+time) channel_from_stellar.copy_attributes(["mass"]) times.append(time) masses.append(triple.mass.copy()) time = 0.0 | t_end.unit print('\ntimes:', times, '\n') # Evolve the system. def advance_stellar(t_se, dt): E0 = gravity.kinetic_energy + gravity.potential_energy t_se += dt if interp: t = t_se-t_stellar i = int(t/dt_se) mass = masses[i] + (t-times[i])*(masses[i+1]-masses[i])/dt_se triple.mass = mass #print 't_se =', t_se, 'masses =', mass else: stellar.evolve_model(t_se) channel_from_stellar.copy_attributes(["mass"]) channel_from_framework_to_gd.copy_attributes(["mass"]) return t_se, gravity.kinetic_energy + gravity.potential_energy - E0 def advance_gravity(tg, dt): tg += dt gravity.evolve_model(tg) channel_from_gd_to_framework.copy() return tg while time < t_end: if scheme == 1: # Advance to the next diagnostic time. dE_se = zero dt = t_diag - time if dt > 0|dt.unit: time = advance_gravity(time, dt) elif scheme == 2: # Derive dt from Pin using dtse_fac. dt = dtse_fac*Pin_0 if time + dt > t_diag: dt = t_diag - time if dt > 0|dt.unit: t_se, dE_se = advance_stellar(t_se, dt) time = advance_gravity(time, dt) elif scheme == 3: # Derive dt from Pin using dtse_fac. dt = dtse_fac*Pin_0 if time + dt > t_diag: dt = t_diag - time if dt > 0|dt.unit: time = advance_gravity(time, dt) t_se, dE_se = advance_stellar(t_se, dt) elif scheme == 4: # Derive dt from Pin using dtse_fac. dt = dtse_fac*Pin_0 if time + dt > t_diag: dt = t_diag - time if dt > 0|dt.unit: t_se, dE_se = advance_stellar(t_se, 0.5*dt) time = advance_gravity(time, dt) t_se, dE_se2 = advance_stellar(t_se, 0.5*dt) dE_se += dE_se2 elif scheme == 5: # Use the specified dt_se. dE_se = zero dt = dt_se if time + dt > t_diag: dt = t_diag - time if dt > 0|dt.unit: # For use with symple only: set up average mass loss. channel_from_stellar.copy_attributes(["mass"]) m0 = triple.mass.copy() stellar.evolve_model(t_se+dt) channel_from_stellar.copy_attributes(["mass"]) t_se = stellar.model_time m1 = triple.mass dmdt = (m1-m0)/dt for i in range(len(dmdt)): gravity.set_dmdt(i, dmdt[i]) time = advance_gravity(time, dt) else: print('unknown option') sys.exit(0) if time >= t_diag: t_diag = time + dt_diag Ekin = gravity.kinetic_energy Epot = gravity.potential_energy Etot = Ekin + Epot dE = Etot_prev - Etot Mtot = triple.mass.sum() print("T=", time, end=' ') print("M=", Mtot, "(dM[SE]=", Mtot/Mtriple, ")", end=' ') print("E= ", Etot, "Q= ", Ekin/Epot, end=' ') print("dE=", (Etot_init-Etot)/Etot, "ddE=", (Etot_prev-Etot)/Etot, end=' ') print("(dE[SE]=", dE_se/Etot, ")") Etot_init -= dE Etot_prev = Etot ain, ein, aout, eout = get_orbital_elements_of_triple(triple) print("Triple elements t=", t_stellar + time, \ "inner:", triple[0].mass, triple[1].mass, ain, ein, \ "outer:", triple[2].mass, aout, eout) t.append(time.value_in(units.yr)) mtot.append(Mtot.value_in(units.MSun)) smai.append(ain/ain_0) ecci.append(ein/ein_0) smao.append(aout/aout_0) ecco.append(eout/eout_0) if eout > 1 or aout <= zero: print("Binary ionized or merged") break gravity.stop() stellar.stop() return t, mtot, smai, ecci, smao, ecco
def get_smalln(converter): instance = SmallN(converter) instance.initialize_code() instance.parameters.set_defaults() return instance
else: print "unexpected argument", o if random_seed <= 0: numpy.random.seed() random_seed = numpy.random.randint(1, pow(2, 31) - 1) numpy.random.seed(random_seed) print "random seed =", random_seed, numpy.random.random() #----------------------------------------------------------------- assert is_mpd_running() # Instantiate workers once only and pass to scatter3 as arguments. gravity = SmallN(redirection="none") #gravity = SmallN(redirection = "none", debugger="valgrind") # search for # memory leaks gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.timestep_parameter = accuracy_parameter gravity.parameters.unperturbed_threshold = gamma kep = Kepler(redirection="none") #, debugger="gdb") kep.initialize_code() kep.set_random(random_seed) # ** Note potential conflict between C++ # ** and Python random generators. # Timing: cpu = numpy.zeros(4)
def init_smalln(): global SMALLN #SMALLN = SmallN(redirection="none", debugger="xterm") SMALLN = SmallN(redirection="none") SMALLN.parameters.timestep_parameter = 0.1
assert is_mpd_running() # Instantiate worker modules once only and pass them to scatter32 # as arguments. # Kepler manages two-body dynamics. kep = Kepler(redirection = "none") #, debugger="gdb") kep.initialize_code() kep.set_random(random_seed) # ** Note potential conflict between C++ # ** and Python random generators. # Gravity manages the N-body integration. gravity = SmallN(redirection = "none") gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.timestep_parameter = accuracy_parameter gravity.parameters.unperturbed_threshold = gamma # Treecheck determines the structure of the 3-body system. # Treecheck = gravity is OK. treecheck = SmallN(redirection = "none") treecheck.initialize_code() treecheck.parameters.set_defaults() treecheck.parameters.timestep_parameter = accuracy_parameter treecheck.parameters.unperturbed_threshold = gamma # Timing:
else: print "unexpected argument", o if random_seed <= 0: numpy.random.seed() random_seed = numpy.random.randint(1, pow(2,31)-1) numpy.random.seed(random_seed) print "random seed =", random_seed, numpy.random.random() #----------------------------------------------------------------- assert is_mpd_running() # Instantiate workers once only and pass to scatter3 as arguments. gravity = SmallN(redirection = "none") #gravity = SmallN(redirection = "none", debugger="valgrind") # search for # memory leaks gravity.initialize_code() gravity.parameters.set_defaults() gravity.parameters.timestep_parameter = accuracy_parameter gravity.parameters.unperturbed_threshold = gamma kep = Kepler(redirection = "none") #, debugger="gdb") kep.initialize_code() kep.set_random(random_seed) # ** Note potential conflict between C++ # ** and Python random generators. # Timing: cpu = numpy.zeros(4)