Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
    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()
Exemplo n.º 3
0
    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")
Exemplo n.º 4
0
    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")
Exemplo n.º 5
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))
Exemplo n.º 6
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))
Exemplo n.º 7
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