def create_binary(stars, binaries, primary_mass, mass_ratio, separation,
                  eccentricity):
    """
    creates a single binary, the constituent stars will be accumulated
    in the stars partice set, the binary will be added to the binaries
    particle set.
    """
    primary_star = datamodel.Particle()
    primary_star.mass = primary_mass

    # we add the particle to the stars set
    # and we get a reference to the particle in the stars set
    # back
    # we want to use that star in constructing the
    # binary, so that the binaries all refer to
    # the stars in the "stars set"
    primary_star = stars.add_particle(primary_star)

    secondary_star = datamodel.Particle()
    secondary_star.mass = primary_mass * mass_ratio
    secondary_star = stars.add_particle(secondary_star)

    binary = datamodel.Particle()
    binary.eccentricity = eccentricity
    binary.semi_major_axis = separation
    binary.child1 = primary_star
    binary.child2 = secondary_star

    binaries.add_particle(binary)
Example #2
0
 def test5(self):
     instance = self.code_factory()()
     p1 = datamodel.Particle(mass=1. | units.MSun)
     p2 = datamodel.Particle(mass=1. | units.MSun)
     p1 = instance.particles.add_particle(p1)
     instance.evolve_model(1. | units.Gyr)
     p2 = instance.particles.add_particle(p2)
     instance.evolve_model(2. | units.Gyr)
     self.assertGreaterEqual(p1.age, (1. | units.Gyr))
     self.assertGreaterEqual(p2.age, (1. | units.Gyr))
     self.assertLess(p2.age, (2. | units.Gyr))
Example #3
0
 def test8(self):
     instance1 = self.code_factory()()
     instance2 = self.code_factory()()
     p1 = datamodel.Particle(mass=1. | units.MSun)
     p1 = instance1.particles.add_particle(p1)
     p1.evolve_for(10. | units.Myr)
     p2 = datamodel.Particle(mass=1. | units.MSun)
     p2 = instance2.particles.add_particle(p2)
     instance2.evolve_model(10. | units.Myr)
     self.assertAlmostEqual(p1.mass, p2.mass)
     self.assertAlmostEqual(p1.radius, p2.radius)
     self.assertAlmostEqual(p1.luminosity, p2.luminosity)
Example #4
0
def planet(ID, host_star, planet_mass, init_a, init_e, random_orientation=False):
    ''' Creates a planet as an AMUSE Particle with provided characteristics.
        ID: Identifying number unique to this planet.
        host_star: The AMUSE Particle that is the host star for the planet.
        planet_mass: The mass of the planet (in the nbody units).
        init_a: Initial semi-major axis (in nbody units).
        init_e: Initial eccentricity (in nbody units).
        random_orientation: Boolean to incline the planet in a random fashion.
    '''
# Sets Planet Values to Provided Conditions
    p = datamodel.Particle()
    p.id = ID
    p.type = "planet"
    p.host_star = host_star.id
    p.mass = planet_mass
# Calculates a Random Position in the Orbit & Gets the Orbital Speed
    cosTheta = math.cos(np.random.random()*math.pi*2)
    init_x = init_a*(1-init_e**2)/(1+init_e*cosTheta) # Need to Double Check ... Thornton 302
    init_vy = np.sqrt(units.constants.G*host_star.mass*(2/init_x-1/init_a))
# Sets the Dynamical Radius to the Hill Sphere Approx.
    p.radius = util.calc_HillRadius(init_a, init_e, p.mass, host_star.mass)
# Sets the Particle in "Orbit" Around the Origin
# May Need to Correct This Later to Accurately Depict Position in Orbit
    p.position = [init_x.number, 0.0 , 0.0] | init_x.unit
    p.velocity = [0.0 , init_vy.number, 0.0] | init_vy.unit
# Preforms a Euler Rotation on the Planet if Requested
# Not Working Properly due to Data Structure, Need to Fix
    if random_orientation:
        util.preform_EulerRotation(p)
# Moves the Planet to Orbit the Host Star
    p.position = p.position + host_star.position
    p.velocity = p.velocity + host_star.velocity
# Returns the Created AMUSE Particle
    return p
Example #5
0
    def __init__(self):
        self.load_integrator_and_units()
        self.planets = []
        self.planets_data = Horizons()

        #Notable events
        self.start_date = date(1971, 10, 26)
        self.voyagerI_launch_date = date(1977, 9, 7)
        self.stop_date = date(1989, 10, 26)

        self.model_t0 = self.start_date

        self.planets = datamodel.Stars(10)
        self.voyagerI = datamodel.Particle()

        #set I.C. using Horizons database
        self.set_IC_at_date([self.planets_data.Sun, self.planets_data.Mercury, self.planets_data.Venus, 
                             self.planets_data.Earth, self.planets_data.Moon, self.planets_data.Mars, 
                             self.planets_data.Jupiter, self.planets_data.Saturn, self.planets_data.Uranus,
                             self.planets_data.Neptune], 
                            self.planets,
                            self.start_date)

        self.set_IC_at_date([self.planets_data.VoyagerI],self.voyagerI.as_set(), self.voyagerI_launch_date)

        self.planets.synchronize_to(self.code.particles)

        self.planets_from_code_to_model = self.code.particles.new_channel_to(self.planets)

        #later we will add particles
        self.all_particles = self.planets.copy()

        self.P = planetarium.SolarSystemView((1600,1000))
Example #6
0
    def test3(self):
        code = Hermite()
        particles_in_binary = self.new_binary(0.1 | nbody_system.mass,
                                              0.1 | nbody_system.mass,
                                              0.01 | nbody_system.length,
                                              keyoffset=1)
        particles_in_binary.radius = 0.001 | nbody_system.length
        binary = datamodel.Particle(key=3)
        binary.child1 = particles_in_binary[0]
        binary.child2 = particles_in_binary[1]
        binary.radius = 0.5 | nbody_system.length
        binary.mass = 0.2 | nbody_system.mass
        encounter_code = encounters.HandleEncounter(
            kepler_code=self.new_kepler(),
            resolve_collision_code=self.new_smalln(),
            interaction_over_code=None)
        multiples_code = encounters.Multiples(
            gravity_code=code, handle_encounter_code=encounter_code)
        multiples_code.singles_in_binaries.add_particles(particles_in_binary)
        multiples_code.binaries.add_particle(binary)

        self.assertEquals(len(multiples_code.singles_in_binaries), 2)
        self.assertEquals(id(multiples_code.binaries[0].child1.particles_set),
                          id(multiples_code.singles_in_binaries))

        multiples_code.commit_particles()

        self.assertEquals(len(multiples_code.multiples), 1)
        self.assertEquals(len(multiples_code.components_of_multiples), 2)
Example #7
0
def plottillagb():
    sun = datamodel.Particle(
        mass = 1 | units.MSun,
        radius = 1 | units.RSun
    )
    
    sse = SSE()
    sse.particles.add_particle(sun)
    
    channel_from_se_to_memory = sse.particles.new_channel_to(sun.as_set())
    channel_from_se_to_memory.copy()
    
    masses = []|units.MSun

    timerange = numpy.arange(11500, 13500,10) | units.Myr
    for time in timerange:
        sse.evolve_model(time)
        channel_from_se_to_memory.copy()
        masses.append(sun.mass)
        print(time.as_quantity_in(units.Myr), sun.mass.as_quantity_in(units.MSun))
        
    sse.stop()
    
    figure = pyplot.figure(figsize= (6,6))
    
    subplot = figure.add_subplot(1, 1, 1)
    subplot.plot(timerange.value_in(units.Gyr), masses.value_in(units.MSun),'.')
    subplot.set_xlabel('t (Gyr)')
    subplot.set_ylabel('mass (MSun)')
    
    pyplot.show()
Example #8
0
    def test22(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 = BHTree()
        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()
Example #9
0
    def test2(self):
        convert_nbody = nbody_system.nbody_to_si(5.9742e24 | units.kg,
                                                 1e6 | units.m)
        instance = interface.TwoBody(convert_nbody)

        p = datamodel.Particle()
        p.mass = 5.9742e24 | units.kg
        p.radius = 6.371e6 | units.m
        p.position = [0., 7.e6, -1.2124e7] | units.m
        p.velocity = [0., 2.6679e3, 4.6210e3] | units.m / units.s

        instance.particles.add_particle(p)

        instance.evolve_model(3600.0 | units.s)

        position = instance.particles[0].position
        velocity = instance.particles[0].velocity

        self.assertAlmostEqual(position.x.value_in(units.m), 0., 7)
        self.assertAlmostEqual(
            position.y.value_in(units.m) / (-3.30647600568e6), 1., 7)
        self.assertAlmostEqual(
            position.z.value_in(units.m) / 7.40831575351e6, 1., 7)
        self.assertAlmostEqual(velocity.x.value_in(units.m / units.s), 0., 7)
        self.assertAlmostEqual(
            velocity.y.value_in(units.m / units.s) / (-8.29821376206e3), 1., 7)
        self.assertAlmostEqual(
            velocity.z.value_in(units.m / units.s) / (-0.972888312209e3), 1.,
            7)
        instance.stop()
Example #10
0
    def test3(self):
        instance = self.new_instance_of_an_optional_code(
            Mocassin)  #, debugger = "xterm")
        instance.initialize_code()
        instance.set_random_seed(1)
        instance.set_input_directory(instance.get_default_input_directory())
        instance.set_mocassin_output_directory(instance.output_directory +
                                               os.sep)

        instance.set_initial_nebular_temperature(200.0 | units.K)

        instance.parameters.nx = 7
        instance.parameters.ny = 7
        instance.parameters.nz = 7

        print((0.95E+19 | units.cm).value_in(units.parsec))
        instance.parameters.length_x = 0.95E+19 | units.cm
        instance.parameters.length_y = 0.95E+19 | units.cm
        instance.parameters.length_z = 0.95E+19 | units.cm

        instance.set_high_limit_of_the_frequency_mesh(15
                                                      | mocassin_rydberg_unit)
        instance.set_low_limit_of_the_frequency_mesh(1.001e-5
                                                     | mocassin_rydberg_unit)
        instance.set_maximum_number_of_monte_carlo_iterations(1)
        instance.set_total_number_of_photons(100)
        #~ instance.set_constant_hydrogen_density(100 | units.cm**-3)

        instance.commit_parameters()
        instance.grid.hydrogen_density = 100 | units.cm**-3
        instance.commit_grid()

        p = datamodel.Particle()
        p.x = 0 | units.cm
        p.y = 0 | units.cm
        p.z = 0 | units.cm
        p.temperature = 20000 | units.K
        p.luminosity = 1. | units.LSun

        instance.particles.add_particle(p)

        instance.commit_particles()

        self.assertAlmostRelativeEquals(
            1e-5, instance.ion_density_grid.density[3][1][2][0][0], 7)
        self.assertAlmostRelativeEquals(
            1e-5, instance.ion_density_grid.density[3][1][3][0][0], 7)

        instance.step()

        print(instance.grid.electron_density.mean())

        self.assertAlmostRelativeEquals(0.0,
                                        instance.get_percentage_converged())
        self.assertGreater(instance.grid.electron_density.mean(),
                           65. | units.cm**-3)
        self.assertLess(instance.grid.electron_density.mean(),
                        95. | units.cm**-3)

        instance.stop()
Example #11
0
    def test22(self):
        print(
            "Testing zero-mass test particles in Huayno, can be used for removing particles when inside recursive evolve loop"
        )
        sun_and_earth = self.new_system_of_sun_and_earth()
        period = (4.0 * math.pi**2 * (1.0 | units.AU)**3 /
                  (constants.G * sun_and_earth.total_mass())).sqrt()
        convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun,
                                                 1.0 | units.AU)
        huayno = Huayno(convert_nbody)
        huayno.parameters.epsilon_squared = 0.0 | units.AU**2
        huayno.parameters.inttype_parameter = huayno.inttypes.SHARED8

        test_particle = datamodel.Particle(mass=0 | units.MSun,
                                           position=[4, 0, 0] | units.AU,
                                           velocity=[0, 0, 0] | units.kms)
        test_particle.vy = (constants.G * sun_and_earth.total_mass() /
                            (4.0 | units.AU)).sqrt()
        sun_and_earth.add_particle(test_particle)
        huayno.particles.add_particles(sun_and_earth)
        huayno.evolve_model(period)
        self.assertAlmostRelativeEqual(huayno.particles[:2].x,
                                       sun_and_earth[:2].x, 13)
        huayno.evolve_model(1.25 * period)
        self.assertAlmostRelativeEqual(huayno.particles[1].y,
                                       sun_and_earth[1].x, 13)
        huayno.evolve_model(8.0 * period)
        self.assertAlmostRelativeEqual(huayno.particles.x, sun_and_earth.x, 8)
        huayno.stop()
Example #12
0
def simulate_evolution_tracks():
    stellar_evolution = SSE()

    star = datamodel.Particle()
    star.mass = stellar_mass

    star = stellar_evolution.particles.add_particle(star)

    luminosity_at_time = [] | units.LSun
    temperature_at_time = [] | units.K

    print("Evolving a star with mass:", stellar_mass)
    is_evolving = True
    while is_evolving and star.age < end_time:
        luminosity_at_time.append(star.luminosity)
        temperature_at_time.append(star.temperature)
        previous_age = star.age
        # if we do not specify an end_time in the evolve_model function
        # a stellar evolution code will evolve to the next
        # 'natural' timestep, this will ensure all interesting physics
        # is seen in the hr diagram
        stellar_evolution.evolve_model()
        is_evolving = (star.age != previous_age)

    stellar_evolution.stop()

    return temperature_at_time, luminosity_at_time
Example #13
0
    def test20(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.radius = 0.005 | 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

        very_short_time_to_evolve = 1 | units.s
        very_long_time_to_evolve = 1e9 | nbody_system.time

        instance = BHTree()
        instance.initialize_code()
        instance.parameters.stopping_conditions_timeout = very_short_time_to_evolve
        self.assertEquals(instance.parameters.stopping_conditions_timeout,
                          very_short_time_to_evolve)
        instance.parameters.epsilon_squared = (0.01 | nbody_system.length)**2
        instance.particles.add_particles(particles)
        codeparticles1 = instance.particles
        instance.particles.add_particle(
            datamodel.Particle(position=[0, 1, 2] | nbody_system.length,
                               velocity=[0, 0, 0] | nbody_system.speed,
                               radius=0.005 | nbody_system.length,
                               mass=1 | nbody_system.mass))
        codeparticles2 = instance.particles
        self.assertTrue(codeparticles1 is codeparticles2)
        instance.cleanup_code()
        codeparticles3 = instance.particles
        self.assertFalse(codeparticles1 is codeparticles3)

        instance.stop()
Example #14
0
 def test4(self):
     for m in ([0.2, 1., 5.] | units.MSun):
         instance = self.code_factory()()
         p = datamodel.Particle(mass=m)
         p2 = instance.particles.add_particle(p)
         self.assertAlmostEqual(p2.age, 0. | units.Myr)
         instance.evolve_model(1. | units.Myr)
         self.assertGreaterEqual(p2.age, (1. | units.Myr))
         self.assertGreaterEqual(instance.model_time, (1. | units.Myr))
Example #15
0
    def test16(self):
        print("Testing ph4 states")
        stars = new_plummer_model(100)
        black_hole = datamodel.Particle()
        black_hole.mass = 1.0 | nbody_system.mass
        black_hole.radius =  0.0 | nbody_system.length
        black_hole.position = [0.0, 0.0, 0.0] | nbody_system.length
        black_hole.velocity = [0.0, 0.0, 0.0] | nbody_system.speed
        
        print("First do everything manually:")
        instance = ph4()
        self.assertEqual(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.initialize_code()
        self.assertEqual(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.parameters.epsilon_squared = 0.0 | nbody_system.length**2
        instance.parameters.timestep_parameter = 0.01
        instance.commit_parameters()
        self.assertEqual(instance.get_name_of_current_state(), 'EDIT')
        instance.particles.add_particles(stars)
        instance.commit_particles()
        self.assertEqual(instance.get_name_of_current_state(), 'RUN')
        instance.particles.remove_particle(stars[0])
        instance.particles.add_particle(black_hole)
        self.assertEqual(instance.get_name_of_current_state(), 'UPDATE')
        instance.recommit_particles()
        self.assertEqual(instance.get_name_of_current_state(), 'RUN')
        instance.evolve_model(0.001 | nbody_system.time)
        self.assertEqual(instance.get_name_of_current_state(), 'EVOLVED')
        instance.synchronize_model()
        self.assertEqual(instance.get_name_of_current_state(), 'RUN')
        instance.cleanup_code()
        self.assertEqual(instance.get_name_of_current_state(), 'END')
        instance.stop()

        print("initialize_code(), commit_parameters(), (re)commit_particles(), " \
            "synchronize_model(), and cleanup_code() should be called " \
            "automatically before editing parameters, new_particle(), get_xx(), and stop():")
        instance = ph4()
        self.assertEqual(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.parameters.epsilon_squared = 0.0 | nbody_system.length**2
        instance.parameters.timestep_parameter = 0.01
        self.assertEqual(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.particles.add_particles(stars)
        self.assertEqual(instance.get_name_of_current_state(), 'EDIT')
        mass = instance.particles[0].mass
        self.assertEqual(instance.get_name_of_current_state(), 'RUN')
        instance.particles.remove_particle(stars[0])
        instance.particles.add_particle(black_hole)
        self.assertEqual(instance.get_name_of_current_state(), 'UPDATE')
        mass = instance.particles[0].mass
        self.assertEqual(instance.get_name_of_current_state(), 'RUN')
        instance.evolve_model(0.001 | nbody_system.time)
        self.assertEqual(instance.get_name_of_current_state(), 'EVOLVED')
        mass = instance.particles[0].mass
        self.assertEqual(instance.get_name_of_current_state(), 'RUN')
        instance.stop()
        self.assertEqual(instance.get_name_of_current_state(), 'STOPPED')
Example #16
0
    def add_particle_with_parameters(self, subnode, parent):
        added_particle = self.system.add_particle(datamodel.Particle())

        self._recursive_parse_node_into_particles(subnode,
                                                  added_particle,
                                                  parent=added_particle)

        if not parent is None:
            parent.add_child(added_particle)
Example #17
0
 def test3(self):
     instance = self.code_factory()()
     p = datamodel.Particle(mass=1. | units.MSun)
     p2 = instance.particles.add_particle(p)
     self.assertAlmostEqual(p2.mass, p.mass)
     self.assertTrue(hasattr(p2, "radius"))
     self.assertTrue(hasattr(p2, "luminosity"))
     self.assertTrue(hasattr(p2, "age"))
     self.assertTrue(hasattr(p2, "stellar_type"))
     self.assertEqual(str(p2.stellar_type), "Main Sequence star")
Example #18
0
 def test2(self):
     for m in ([0.2, 1., 5., 25.] | units.MSun):
         instance = self.code_factory()()
         p = datamodel.Particle(mass=m)
         p2 = instance.particles.add_particle(p)
         self.assertAlmostEqual(p2.mass, p.mass)
         self.assertTrue(hasattr(p2, "radius"))
         self.assertTrue(hasattr(p2, "luminosity"))
         self.assertTrue(hasattr(p2, "age"))
         self.assertTrue(hasattr(p2, "stellar_type"))
Example #19
0
    def test24(self):
        particle = datamodel.Particle()
        particle.mass = 10 | units.kg

        grid = datamodel.Grid(5, 4, 3)
        grid.mass = 2.0 | units.kg
        grid.nounit = 10
        self.assertEquals(grid.nounit[0][1][2], 10)
        self.assertEquals(grid[0][1][2].nounit, 10)
        self.assertEquals(len(grid.nounit), 5)
Example #20
0
 def restore_pair(self, state, a_planet):
     self.voyagerI = datamodel.Particle()
     voyagerI_attrib = self.voyagerI.as_set()
     rel_vr = ([state['vx'],state['vy'],state['vz']]|units.m/units.s).as_quantity_in(units.AUd)
     rel_r = ([state['x'],state['y'],state['z']]|units.m).as_quantity_in(units.AU)
     voyagerI_attrib[0].velocity = a_planet.velocity + rel_vr
     voyagerI_attrib[0].position = a_planet.position + rel_r
     
     self.all_particles.add_particle(self.voyagerI)
     self.all_particles.synchronize_to(self.code.particles)
     self.voyagerI_from_code_to_model = self.code.particles.new_channel_to(self.voyagerI.as_set())
    def test3(self):
        """
        In this test we will get a list from a particle
        """

        instance = ExampleParticlesInterface()
        self.assertEquals(len(instance.particles), 0)

        theParticle = datamodel.Particle()
        theParticle.mass = 10 | units.kg
        theParticle.x = 0.1 | units.m
        theParticle.y = 0.2 | units.m
        theParticle.z = 0.5 | units.m

        instance.particles.add_particle(theParticle)

        theParticle = datamodel.Particle()
        theParticle.mass = 11 | units.kg
        theParticle.x = 0.1 | units.m
        theParticle.y = 0.2 | units.m
        theParticle.z = 0.5 | units.m

        instance.particles.add_particle(theParticle)

        self.assertEquals(len(instance.particles[0].element_list()), 10)
        list = instance.particles[0].element_list()
        self.assertEquals(list[0].value1, 0 | units.none)
        self.assertEquals(list[0].value2, 0 | units.none)
        self.assertEquals(list[1].value1, 0 | units.none)
        self.assertEquals(list[1].value2, 1 | units.none)
        for x in range(len(list)):
            self.assertEquals(list[x].value1, 0 | units.none)
            self.assertEquals(list[x].value2, x | units.none)

        list = instance.particles[1].element_list()
        for x in range(len(list)):
            self.assertEquals(list[x].value1, 1 | units.none)
            self.assertEquals(list[x].value2, x | units.none)
 def test4(self):
     """
     In this test we will get a list from a particle
     """
     
     instance = ExampleParticlesInterface()
     self.assertEqual(len(instance.particles), 0)
     
     theParticle = datamodel.Particle()
     theParticle.x = 0.1 | units.m
     theParticle.y = 0.2 | units.m
     theParticle.z = 0.5 | units.m
     
     self.assertRaises(exceptions.AmuseException, instance.particles.add_particle, theParticle)        
    def test2(self):
        """
        In this test we will set and get different properties
        of the particle.
        
        To limit overhead, the system will use the set_* or get_* calls 
        that are the closests match to the attributes queries.
        """
        self.log("accessing attributes of a particle")
        instance = ExampleParticlesInterface()
        self.assertEquals(len(instance.particles), 0)

        theParticle = datamodel.Particle()
        theParticle.mass = 10 | units.kg
        theParticle.x = 0.1 | units.m
        theParticle.y = 0.2 | units.m
        theParticle.z = 0.5 | units.m

        instance.particles.add_particle(theParticle)

        self.log(
            "Getting the mass of particle with key {0}, get_mass should be called",
            theParticle.key)
        self.assertEquals(instance.particles[0].mass, 10 | units.kg)

        self.log(
            "Getting the position of particle with key {0}, get_position should be called",
            theParticle.key)
        self.assertEquals(instance.particles[0].position,
                          [0.1, 0.2, 0.5] | units.m)

        self.log(
            "Getting the only the x attribute of particle with key {0}, get_position should be called (y and z are discarded)",
            theParticle.key)
        self.assertEquals(instance.particles[0].x, 0.1 | units.m)

        self.log(
            "Setting the position of particle with key {0}, set_position should be called",
            theParticle.key)
        instance.particles[0].position = [0.2, 0.3, 0.6] | units.m

        self.log(
            "Setting the x of particle with key {0}, should fail as no function can set x and no others",
            theParticle.key)

        def block():
            instance.particles[0].x = 0.1 | units.m

        self.assertRaises(Exception, block)
Example #24
0
    def test5(self):
        #from: Fundamentals of Celestial Mechanics, J.M.A. Danby 2nd edition
        instance = interface.TwoBody()
        p = datamodel.Particle()

        p.mass = 1.0 | nbody_system.mass
        p.radius = 0.001 | nbody_system.length
        p.position = [1.0, 0.1, -0.1] | nbody_system.length
        p.velocity = [-0.1, 2.0, -0.2] | nbody_system.speed

        instance.particles.add_particle(p)
        instance.evolve_model(1.0|nbody_system.time)
        self.assertAlmostEqual(instance.particles.x, 0.611238439231|nbody_system.length, 7)
        self.assertAlmostEqual(instance.particles.y, 1.92873971354574|nbody_system.length, 7)
        self.assertAlmostEqual(instance.particles.z, -0.2562478900031234|nbody_system.length, 7)
        instance.stop()
 def test10(self):
     factory = self.gravity_code_factory()
     instance = self.new_instance_of_an_optional_code(factory)
     particle = datamodel.Particle()
     particle.position = [0, 0, 0] | self.length_unit
     particle.velocity = [1, -2, 3.0] | self.speed_unit
     particle.mass = 1 | self.mass_unit
     particle.radius = 0.0 | self.length_unit
     
     instance.particles.add_particle(particle)
     instance.evolve_model(1 | self.time_unit)
     self.assertAlmostEqual(instance.model_time, 1 | self.time_unit)
     self.assertAlmostEqual(instance.kinetic_energy, 7.0 | self.mass_unit * self.speed_unit**2)
     self.assertAlmostEqual(instance.potential_energy, 0.0 | self.mass_unit * self.speed_unit**2)
     self.assertAlmostEqual(instance.particles[0].position, [1.0, -2.0, 3.0] | self.length_unit)
     instance.stop()
Example #26
0
    def test24(self):
        print("test massless particles/ kepler integrator")
        N = 20
        tend = 2. | units.yr
        numpy.random.seed(12345)
        conv = nbody_system.nbody_to_si(4. | units.MSun, 5. | units.AU)
        orbiters = plummer.new_plummer_model(N, conv)
        sun = datamodel.Particle(mass=1. | units.MSun)
        sun.position = [12.3, 1., -.2] | units.AU
        sun.velocity = [10, 50., -20.] | units.kms
        orbiters.mass *= 0.

        a0, eps0 = elements(sun.mass, orbiters.x, orbiters.y, orbiters.z,
                            orbiters.vx, orbiters.vy, orbiters.vz)

        orbiters.position += sun.position
        orbiters.velocity += sun.velocity

        pos = dict()
        for inttype in [20, 14]:
            code = Huayno(conv)
            code.parameters.inttype_parameter = inttype
            code.parameters.timestep_parameter = 0.1
            code.particles.add_particle(sun)
            orbiters2 = code.particles.add_particles(orbiters).copy()
            orbiters2.position -= sun.position
            orbiters2.velocity -= sun.velocity
            code.evolve_model(tend)
            a, eps = elements(sun.mass, orbiters2.x, orbiters2.y, orbiters2.z,
                              orbiters2.vx, orbiters2.vy, orbiters2.vz)

            da = abs((a - a0) / a0)
            deps = abs(eps - eps0) / eps0

            dev = numpy.where(da > 1.e-12)[0]
            self.assertEqual(len(dev), 0)
            dev = numpy.where(deps > 1.e-12)[0]
            self.assertEqual(len(dev), 0)
            pos[inttype] = [
                orbiters2.x.value_in(units.AU),
                orbiters2.y.value_in(units.AU),
                orbiters2.z.value_in(units.AU)
            ]
        self.assertAlmostEqual(pos[20][0], pos[14][0], 12)
        self.assertAlmostEqual(pos[20][1], pos[14][1], 12)
        self.assertAlmostEqual(pos[20][2], pos[14][2], 12)
Example #27
0
def planet_v2(ID,
              host_star,
              planet_mass,
              init_a,
              init_e,
              random_orientation=False):
    ''' Creates a planet as an AMUSE Particle with provided characteristics.
        ID: Identifying number unique to this planet.
        host_star: The AMUSE Particle that is the host star for the planet.
        planet_mass: The mass of the planet (in the nbody units).
        init_a: Initial semi-major axis (in nbody units).
        init_e: Initial eccentricity (in nbody units).
        random_orientation: Boolean to incline the planet in a random fashion.
    '''
    # Define the Host Star's Original Location & Position
    rCM = host_star.position
    vCM = host_star.velocity
    # Sets Planet Values to Provided Conditions
    p = datamodel.Particle()
    p.id = ID
    p.type = "planet"
    p.host_star = host_star.id
    p.mass = planet_mass
    # Sets the Dynamical Radius to the Hill Sphere Approx.
    p.radius = util.calc_HillRadius(init_a, init_e, p.mass, host_star.mass)
    # Generate a Random Position on the Orbit (True Anomaly)
    # This ensures that all the planets don't start out along the same joining line.
    init_ta = 360 * np.random.random() | units.deg
    # Get the Host Star & Planets Positions from Kepler Relative to the Origin
    newPSystem = new_binary_from_orbital_elements(host_star.mass,
                                                  p.mass,
                                                  init_a,
                                                  eccentricity=init_e,
                                                  true_anomaly=init_ta,
                                                  G=constants.G)
    # Rotate the Binary System & Move to the CoM's Position
    if random_orientation:
        util.preform_EulerRotation(newPSystem)
    host_star.position = rCM + newPSystem[0].position
    host_star.velocity = vCM + newPSystem[0].velocity
    p.position = rCM + newPSystem[1].position
    p.velocity = vCM + newPSystem[1].velocity
    # Returns the Created AMUSE Particle
    return p
Example #28
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()
Example #29
0
 def test3(self):
     convert_nbody = nbody_system.nbody_to_si(5.9742e24 | units.kg, 1e6| units.m)
     instance = interface.TwoBody(convert_nbody)
     
     p = datamodel.Particle()
     p.mass = 5.9742e24 | units.kg
     p.radius = 7.1e6 | units.m
     p.position = [0.,7.e6,-1.2124e7] | units.m
     p.velocity = [0.,2.6679e3,4.6210e3] | units.m/units.s
     
     instance.particles.add_particle(p)
     
     instance.evolve_model(3600.0 | units.s)
     
     dt = convert_nbody.to_si(instance.model_time)
     self.assertAlmostEqual(dt.value_in(units.s)/2583.44780926,1.,7)
     
     position = instance.particles[0].position
     self.assertAlmostEqual(((position.x**2+position.y**2+position.z**2)/(7.1e6)**2).value_in(units.m**2),1.,7)
     instance.stop()
Example #30
0
    def test25(self):
        print("test massless particles/ kepler integrator, smoothed")
        N = 10
        tend = 20. | units.yr
        numpy.random.seed(12345)
        conv = nbody_system.nbody_to_si(4. | units.MSun, 5. | units.AU)
        orbiters = plummer.new_plummer_model(N, conv)
        sun = datamodel.Particle(mass=1. | units.MSun)
        sun.position = [0, 0, 0] | units.AU
        sun.velocity = [0, 0, 0] | units.kms
        orbiters.mass *= 0.
        eps = (5. | units.AU)

        e0 = energy(sun.mass, eps, orbiters)
        l0 = angular_momentum(orbiters)

        pos = dict()
        for inttype in [20, 14]:
            code = Huayno(conv)
            code.parameters.inttype_parameter = inttype
            code.parameters.timestep_parameter = 0.1
            code.parameters.epsilon_squared = eps**2
            code.particles.add_particle(sun)
            orbiters2 = code.particles.add_particles(orbiters)
            code.evolve_model(tend)

            e1 = energy(sun.mass, eps, orbiters2)
            l1 = angular_momentum(orbiters2)
            de, dl = abs((e1 - e0) / e0).max(), abs((l1 - l0) / l1).max()
            self.assertTrue(numpy.all(de < 1.e-8))
            self.assertTrue(numpy.all(dl < 1.e-8))

            pos[inttype] = [
                orbiters2.x.value_in(units.AU),
                orbiters2.y.value_in(units.AU),
                orbiters2.z.value_in(units.AU)
            ]
        self.assertAlmostRelativeEqual(pos[20][0], pos[14][0],
                                       4)  # still not clear why 4
        self.assertAlmostRelativeEqual(pos[20][1], pos[14][1], 4)
        self.assertAlmostRelativeEqual(pos[20][2], pos[14][2], 4)