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.assertEqual(len(collisions.particles(0)), 3) self.assertEqual(len(collisions.particles(1)), 3) self.assertEqual( len(particles - collisions.particles(0) - collisions.particles(1)), 1) self.assertEqual( abs(collisions.particles(0).x - collisions.particles(1).x) <= (collisions.particles(0).radius + collisions.particles(1).radius), [True, True, True]) 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 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 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 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 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 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 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 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 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 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 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))
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))