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()
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 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 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.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 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 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 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 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 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 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 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 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 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()
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 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 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 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))
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:
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 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 get_smalln(converter): instance = SmallN(converter) instance.initialize_code() instance.parameters.set_defaults() return instance
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) for i in range(nscatter):
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) for i in range(nscatter):