Пример #1
0
    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)
Пример #2
0
    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()
Пример #3
0
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))
Пример #4
0
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))
Пример #5
0
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