Exemplo n.º 1
0
def setup_sph_code(sph_code, N1, N2, L, rho,u):
    #rho -- local group density
    converter = nbody_system.nbody_to_si(1 | units.MSun, 1 | units.kpc)
    sph_code = sph_code(converter, redirection = 'none')#, mode = 'periodic')#, redirection = 'none')#change periodic
    #sph_code.parameters.periodic_box_size = 10.0 | units.kpc
    dm = Particles(N1)
    dm.mass = (rho * L**3) / N1
    np.random.seed(12345)
    dm.x = L * np.random.uniform(0.0, 1.0, N1)
    dm.y = L * np.random.uniform(0.0, 1.0, N1)
    dm.z = L * np.random.uniform(0.0, 1.0, N1)
    dm.vx = np.zeros(N1) | units.km / units.s
    dm.vy = np.zeros(N1) | units.km / units.s
    dm.vz = np.zeros(N1) | units.km / units.s
    gas = Particles(N2)
    gas.mass = (rho * L**3) / N2
    gas.x = L * np.random.uniform(0.0, 1.0, N2)
    gas.y = L * np.random.uniform(0.0, 1.0, N2)
    gas.z = L * np.random.uniform(0.0, 1.0, N2)
    gas.vx = np.zeros(N2) | units.km / units.s
    gas.vy = np.zeros(N2) | units.km / units.s
    gas.vz = np.zeros(N2) | units.km / units.s
    gas.u = u 
    if isinstance(sph_code, Fi):
        sph_code.parameters.self_gravity_flag = True
        sph_code.parameters.timestep = 5 | units.Myr
        gas.h_smooth = L / N2**(1/3.0)
        dm.h_smooth = L / N1**(1/3.0)
        #gas.position -= 0.5 * L
        
    sph_code.gas_particles.add_particles(gas)
    sph_code.dm_particles.add_particles(dm)
    sph_code.commit_particles()
    return sph_code
Exemplo n.º 2
0
def setup_sph_code(N1, N2, L, rho, u):
    #rho -- local group density
    #converter = nbody_system.nbody_to_si(1 | units.MSun, 1 | units.kpc)
    #sph_code = sph_code(converter)#, mode = 'periodic')#, redirection = 'none')#change periodic
    #sph_code.parameters.periodic_box_size = 10.0 | units.kpc
    dm = Particles(N1)
    dm.mass = 0.8 * (rho * L**3) / N1
    #np.random.seed(12345)
    dm.x = L * np.random.uniform(-0.5, 0.5, N1)
    dm.y = L * np.random.uniform(-0.5, 0.5, N1)
    dm.z = L * np.random.uniform(-0.5, 0.5, N1)
    dm.vx = np.zeros(N1) | units.km / units.s
    dm.vy = np.zeros(N1) | units.km / units.s
    dm.vz = np.zeros(N1) | units.km / units.s
    gas = Particles(N2)
    gas.mass = 0.2 * (rho * L**3) / N2
    gas.x = L * np.random.uniform(-0.5, 0.5, N2)
    gas.y = L * np.random.uniform(-0.5, 0.5, N2)
    gas.z = L * np.random.uniform(-0.5, 0.5, N2)
    gas.vx = np.zeros(N2) | units.km / units.s
    gas.vy = np.zeros(N2) | units.km / units.s
    gas.vz = np.zeros(N2) | units.km / units.s
    gas.u = u
    gas.h_smooth = L / N2**(1 / 3.0)
    dm.h_smooth = L / N1**(1 / 3.0)

    #sph_code.gas_particles.add_particles(gas)
    #sph_code.dm_particles.add_particles(dm)
    #sph_code.commit_particles()

    return gas, dm
Exemplo n.º 3
0
def test_particles(galaxy, n_halo, n_bulge, n_disk):
    _dsk = Particles(len(galaxy))
    _dsk.mass = galaxy.mass
    _dsk.position = galaxy.position
    _dsk.velocity = galaxy.velocity
    disk = _dsk[n_bulge:n_halo]
    return disk
Exemplo n.º 4
0
def displace_galaxy(galaxy, rotation_mat, translation_vector, radial_velocity,
                    transverse_velocity):
    widgets = [
        'Adjusting relative velocities and orientations: ',
        pbwg.AnimatedMarker(),
        pbwg.EndMsg()
    ]

    with pbar.ProgressBar(widgets=widgets, fd=sys.stdout) as progress:
        displaced_galaxy = Particles(len(galaxy))
        displaced_galaxy.mass = galaxy.mass
        displaced_galaxy.position = galaxy.position
        displaced_galaxy.velocity = galaxy.velocity

        for body in displaced_galaxy:
            body.position = ([
                body.x.value_in(units.kpc),
                body.y.value_in(units.kpc),
                body.z.value_in(units.kpc)
            ] @ rotation_mat) | units.kpc
            body.velocity = ([
                body.vx.value_in(units.kms),
                body.vy.value_in(units.kms),
                body.vz.value_in(units.kms)
            ] @ rotation_mat) | units.kms

        displaced_galaxy.position += translation_vector

        displaced_galaxy.velocity += radial_velocity
        displaced_galaxy.velocity += transverse_velocity

    return displaced_galaxy
Exemplo n.º 5
0
def add_comet_objects(pre_tack_giants_system, N_objects, comet_positions,
                      comet_velocities):
    for i in tqdm(range(N_objects)):
        comet = Particles(1)
        comet.name = "OORT_" + str(i)
        comet.mass = 0.0 | units.MSun  #Take massless test particles
        comet.radius = 0.0 | units.RSun
        comet.position = (comet_positions[i, 0], comet_positions[i, 1],
                          comet_positions[i, 2])
        comet.velocity = (comet_velocities[i, 0], comet_velocities[i, 1],
                          comet_velocities[i, 2])
        comet.density = 0.0 | (units.MSun / (units.RSun**3))

        pre_tack_giants_system.add_particle(comet)

    z_comp = np.arctan(
        100 / 8500.
    )  #Determining the z-component of the sun's trajectory around the galactic center

    for i in range(len(pre_tack_giants_system)
                   ):  #adding the sun's trajectory around the galactic center
        pre_tack_giants_system[i].position += (1, 0, 0) * (8.5 | units.kpc)
        pre_tack_giants_system[i].velocity += (0, np.sqrt(1 - z_comp**2),
                                               z_comp) * (220 | units.kms)
    return pre_tack_giants_system
Exemplo n.º 6
0
def merge_two_particles(gravity, particles_in_encounter):
    new_particle = Particles(1)
    new_particle.mass = particles_in_encounter.total_mass()
    new_particle.position = particles_in_encounter.center_of_mass()
    new_particle.velosity = particles_in_encounter.center_of_mass_velocity()
    new_particle.radius = particles_in_encounter.radius.sum()
    gravity.particles.add_particles(new_particle)
    gravity.particles.remove_particles(particles_in_encounter)
Exemplo n.º 7
0
def create_system():
    system = new_solar_system()
    system = system[system.mass > 10**-5 | units.MSun] # Takes gas giants and Sun only
    system.move_to_center()
    
    sun = Particles(1)
    sun.name = "Sun"
    sun.mass = 1.0 | units.MSun
    sun.radius = 1.0 | units.RSun  
    sun.position = (0, 0, 0) | units.AU
    sun.velocity = (0, 0, 0) | units.kms
    sun.density = 3*sun.mass/(4*np.pi*sun.radius**3)
    
    names = ["Jupiter", "Saturn", "Uranus", "Neptune"]
    masses = [317.8, 90, 15, 17] | units.MEarth
    radii = [0.10049, 0.083703, 0.036455, 0.035392] | units.RSun
    semis = [5.4, 7.1, 10.5, 13] | units.AU #[3.5, 4.9, 6.4, 8.4]
    trues = [0, 90, 180, 270]| units.deg#np.random.uniform(0, 360, 4) | units.deg
    longs = np.random.uniform(0, 360, 4) | units.deg
    args = np.random.uniform(0, 360, 4) | units.deg
    
    for i in range(4):
        orbital_elements = get_orbital_elements_from_binary(system[0]+ system[i+1], G=constants.G)
        eccentricity, inclination = 0, orbital_elements[5]
        
        
        sun_and_plan = new_binary_from_orbital_elements(sun[0].mass, masses[i], 
                                          semis[i], 0, trues[i], inclination, longs[i], args[i], G=constants.G)
        
        planet = Particles(1)
        planet.name = system[i+1].name
        planet.mass = system[i+1].mass
        planet.radius = system[i+1].radius # This is purely non-zero for collisional purposes
        planet.position = (sun_and_plan[1].x-sun_and_plan[0].x, sun_and_plan[1].y-sun_and_plan[0].y, sun_and_plan[1].z-sun_and_plan[0].z)
        planet.velocity = (sun_and_plan[1].vx-sun_and_plan[0].vx, sun_and_plan[1].vy-sun_and_plan[0].vy, sun_and_plan[1].vz-sun_and_plan[0].vz)
        planet.density = 3*planet.mass/(4*np.pi*planet.radius**3)
        sun.add_particle(planet)
        
    return sun
Exemplo n.º 8
0
def get_ps_from_yt(pltfile, particle_type="all"):
    
    """Take a FLASH plotfile and return an AMUSE
       particle set.
       
       Keyword arguments:
       pltfile      -- The FLASH hdf5 plot or checkpoint file.
                       Note a plotfile means there must also be
                       a particle file with the same ID number.
       particle_type -- the type of particle that you
                         want from FLASH. Can be:
                         star -- active FLASH particle type
                         sink -- sink FLASH particle type
                         any other string or none returns 
                         all particle types.
                         The default is all
       
       Returns:
       stars -- An AMUSE particle set.
    """
    import yt
    ds = yt.load(pltfile)
    dd = ds.all_data()
    
    all_parts_mass = dd['particle_mass'].in_units('Msun')
    all_parts_pos  = dd['particle_position'].in_units('cm')
    all_parts_vel  = dd['particle_velocity'].in_units('cm/s')
    
    if (particle_type == 'star' or particle_type == 'stars'):
        # Index for only the star particles, and not including sink particles.
        mass_ind = np.where(dd['particle_type'].v == 1)
    elif (particle_type == 'sink' or particle_type == 'sinks'):
        mass_ind = np.where(dd['particle_type'].v == 2)
    else:
        mass_ind = np.arange(len(all_parts_mass))
        
    all_parts_mass = all_parts_mass[mass_ind]
    all_parts_pos  = all_parts_pos[mass_ind]
    all_parts_vel  = all_parts_vel[mass_ind]
        
    num_parts = len(all_parts_mass.v)
    
    stars = Particles(num_parts)
    stars.mass     = all_parts_mass.v | u.MSun
    stars.position = all_parts_pos.v  | u.cm
    stars.velocity = all_parts_vel.v  | u.cm/u.s
    stars.index_of_the_particle = range(num_parts)
    
    return stars
Exemplo n.º 9
0
def create_pre_tack_giants_system():
    #Create a pre_tack_giants_system by first recreating the sun.
    pre_tack_giants_system = Particles(1)
    pre_tack_giants_system[0].name = "Sun"
    pre_tack_giants_system[0].mass = 1.0 | units.MSun
    pre_tack_giants_system[0].radius = 1.0 | units.RSun
    pre_tack_giants_system[0].position = (0, 0, 0) | units.AU
    pre_tack_giants_system[0].velocity = (0, 0, 0) | units.kms
    pre_tack_giants_system[0].density = 3 * pre_tack_giants_system[0].mass / (
        4 * np.pi * pre_tack_giants_system[0].radius**3)

    #The pre tack orbital elements for the planets as below
    names = ["Jupiter", "Saturn", "Uranus", "Neptune"]
    masses = np.array([317.8, 30, 5, 5]) | units.MEarth
    radii = np.array([0.10049, 0.083703, 0.036455, 0.035392]) | units.RSun
    a = np.array([3.5, 4.5, 6.013, 8.031]) | units.AU
    inclinations = np.random.uniform(-5, 5, 4) | units.deg
    true_anomalies = np.random.uniform(0, 360, 4) | units.deg
    longs_of_ascending_node = np.random.uniform(0, 360, 4) | units.deg
    args_of_periapsis = np.random.uniform(0, 360, 4) | units.deg

    #Create the four planets as binaries with the sun and add them to the pre_tack_giants_system
    for i in range(4):
        sun_and_planet = new_binary_from_orbital_elements(
            pre_tack_giants_system[0].mass,
            masses[i],
            a[i],
            0,
            true_anomalies[i],
            inclinations[i],
            longs_of_ascending_node[i],
            args_of_periapsis[i],
            G=constants.G)

        planet = Particles(1)
        planet.name = names[i]
        planet.mass = masses[i]
        planet.radius = radii[
            i]  # This is purely non-zero for collisional purposes
        planet.position = (sun_and_planet[1].x - (0 | units.AU),
                           sun_and_planet[1].y - (0 | units.AU),
                           sun_and_planet[1].z - (0 | units.AU))
        planet.velocity = (sun_and_planet[1].vx - (0 | units.kms),
                           sun_and_planet[1].vy - (0 | units.kms),
                           sun_and_planet[1].vz - (0 | units.kms))
        planet.density = 3 * planet.mass / (4 * np.pi * planet.radius**3)
        pre_tack_giants_system.add_particle(planet)

    return pre_tack_giants_system
Exemplo n.º 10
0
def add_comet_objects(pre_tack_giants_system, N_objects, comet_positions,
                      comet_velocities):
    for i in tqdm(range(N_objects)):
        comet = Particles(1)
        comet.name = "OORT_" + str(i)
        comet.mass = 0.0 | units.MSun  #Take massless test particles
        comet.radius = 0.0 | units.RSun
        comet.position = (comet_positions[i, 0], comet_positions[i, 1],
                          comet_positions[i, 2])
        comet.velocity = (comet_velocities[i, 0], comet_velocities[i, 1],
                          comet_velocities[i, 2])
        comet.density = 0.0 | (units.MSun / (units.RSun**3))

        pre_tack_giants_system.add_particle(comet)
    return pre_tack_giants_system
Exemplo n.º 11
0
def add_comet_objects(system, N_objects, rand_pos, rand_vel):
    for i in tqdm(range(N_objects)):
        oort = Particles(1)
        oort.name = "OORT_" + str(i)
        oort.mass = 0.0 | units.MSun
        oort.radius = (2.3 | units.km).in_(units.RSun) # This is purely non-zero for collisional purposes
        oort.position = (rand_pos[i, 0], rand_pos[i, 1], rand_pos[i, 2])
        oort.velocity = (rand_vel[i, 0], rand_vel[i, 1], rand_vel[i, 2])

        system.add_particle(oort)
        
    for particle in system: #So that Mercury will work
        particle.density = 3*particle.mass/(4*np.pi*particle.radius**3)
    
    return system
Exemplo n.º 12
0
def merge_two_bodies(bodies, particles_in_encounter):
    com_pos = particles_in_encounter.center_of_mass()
    com_vel = particles_in_encounter.center_of_mass_velocity()
    d = (particles_in_encounter[0].position - particles_in_encounter[1].position)
    v = (particles_in_encounter[0].velocity - particles_in_encounter[1].velocity)
    print("Actually merger occurred:")
    print("Two objects (M=",particles_in_encounter.mass.in_(units.MSun),
          ") collided with d=", d.length().in_(units.au))
    #time.sleep(10)
    new_particle=Particles(1)
    new_particle.mass = particles_in_encounter.total_mass()
    new_particle.position = com_pos
    new_particle.velocity = com_vel
    new_particle.radius = ((particles_in_encounter.radius**3).sum())**(1./3.) # New radius cannot simply be summed
    bodies.add_particles(new_particle)
    bodies.remove_particles(particles_in_encounter)
Exemplo n.º 13
0
def new_field_stars(
    N,
    width=10 | units.parsec,
    height=10 | units.parsec,
    depth=100 | units.parsec,
    massdistribution="salpeter",
    agespread=3 | units.Gyr,
    seed=1701,
):
    np.random.seed(seed)
    stars = Particles(N)
    stars.x = (np.random.random(N) - 0.5) * width
    stars.y = (np.random.random(N) - 0.5) * height
    stars.z = (np.random.random(N) - 0.02) * depth
    if massdistribution == "salpeter":
        stars.mass = new_salpeter_mass_distribution(N)

    return stars
Exemplo n.º 14
0
def create_post_tack_giants_system():
    #Create the present day solar system and keep only the sun and the giants
    present_day_solar_system = new_solar_system()
    present_day_solar_system = present_day_solar_system[present_day_solar_system.mass > 10**-5 | units.MSun] # Takes gas giants and Sun only
    present_day_solar_system.move_to_center()
    
    #Create a post_tack_giants_system by first recreating the sun.
    post_tack_giants_system = Particles(1) 
    post_tack_giants_system[0].name = "Sun"
    post_tack_giants_system[0].mass = 1.0 | units.MSun
    post_tack_giants_system[0].radius = 1.0 | units.RSun  
    post_tack_giants_system[0].position = (0, 0, 0) | units.AU
    post_tack_giants_system[0].velocity = (0, 0, 0) | units.kms
    
    #The post tack orbital elements for the planets as below
    a =  np.array([5.4, 7.1, 10.5, 13]) | units.AU 
    true_anomalies = np.random.uniform(0, 360, 4) | units.deg
    long_of_ascending_node = np.random.uniform(0, 360, 4) | units.deg
    args_of_periapsis = np.random.uniform(0, 360, 4) | units.deg
    
    #Create the four planets as binaries with the sun and add them to the post_tack_giants_system
    for i in range(4):
        orbital_elements = get_orbital_elements_from_binary(present_day_solar_system[0]+ present_day_solar_system[i+1], G=constants.G)
        inclination = orbital_elements[5] #Make sure we have a sensable inclination for the giants
        
        
        sun_and_planet = new_binary_from_orbital_elements(post_tack_giants_system.mass[0], present_day_solar_system[i+1].mass, 
                                          a[i], 0, true_anomalies[i], inclination, long_of_ascending_node[i], args_of_periapsis[i], G=constants.G)
        
        planet = Particles(1)
        planet.name = present_day_solar_system[i+1].name
        planet.mass = present_day_solar_system[i+1].mass
        planet.radius = present_day_solar_system[i+1].radius
        planet.position = (sun_and_planet[1].x-sun_and_planet[0].x, sun_and_planet[1].y-sun_and_planet[0].y, sun_and_planet[1].z-sun_and_planet[0].z)
        planet.velocity = (sun_and_planet[1].vx-sun_and_planet[0].vx, sun_and_planet[1].vy-sun_and_planet[0].vy, sun_and_planet[1].vz-sun_and_planet[0].vz)
        post_tack_giants_system.add_particle(planet) 
        
    return post_tack_giants_system
Exemplo n.º 15
0
def get_ps_from_hdf5(partfile, particle_type='all'):
    
    """ Returns an AMUSE particle set from a FLASH
        hdf5 particle file (NOT a plot file) or checkpoint file.
        
        Keyword arguments:
        partfile      -- the FLASH particle file
        particle_type -- the type of particle that you
                         want from FLASH. Can be:
                         star -- active FLASH particle type
                         sink -- sink FLASH particle type
                         any other string or none returns all particle types.
                         The default is all.
                         
        Returns:
        stars -- An AMUSE particle set.
    """
    import h5py

    ds = h5py.File(partfile, 'r')
    
    part_data    = ds.get('tracer particles')
    part_names   = ds.get('particle names')
    part_data    = np.array(part_data)
    part_names   = np.array(part_names)
    real_scalars = ds.get('real scalars')
    part_time    = real_scalars[0][1]
    
    if (part_data.size > 1):
        
        mass_ind  = np.where(np.char.strip(part_names)=='mass')[0]
        tag_ind   = np.where(np.char.strip(part_names)=='tag')[0]
        type_ind  = np.where(np.char.strip(part_names)=='type')[0]
        ct_ind    = np.where(np.char.strip(part_names)=='creation_time')[0]
        posx_ind  = np.where(np.char.strip(part_names)=='posx')[0]
        posy_ind  = np.where(np.char.strip(part_names)=='posy')[0]
        posz_ind  = np.where(np.char.strip(part_names)=='posz')[0]
        velx_ind  = np.where(np.char.strip(part_names)=='velx')[0]
        vely_ind  = np.where(np.char.strip(part_names)=='vely')[0]
        velz_ind  = np.where(np.char.strip(part_names)=='velz')[0]
        
        # Get the particle types
        type_part = part_data[:,type_ind].flatten()
        
        if (particle_type == 'star' or particle_type == 'stars'):
            # Find only star particles (particle_type=1)
            star_ind = np.where(type_part==1)[0]
        elif (particle_type == 'sink' or particle_type == 'sinks'):
            # Find only sink particles (particle_type=2)
            star_ind = np.where(type_part==2)[0]
        else:
            star_ind = np.arange(len(type_part))
        
        num_parts = len(star_ind)
        # allocate the array
        stars = Particles(num_parts)
        
        # Masses of stars
        stars.mass = part_data[star_ind,mass_ind] | u.g
        # Tags of stars
        stars.tag = part_data[star_ind,tag_ind]
        # Creation times of stars
        stars.ct   = part_data[star_ind,ct_ind]   | u.s
        # Positions
        stars.x = part_data[star_ind,posx_ind]    | u.cm
        stars.y = part_data[star_ind,posy_ind]    | u.cm
        stars.z = part_data[star_ind,posz_ind]    | u.cm
        # Velocities
        stars.vx = part_data[star_ind,velx_ind]   | u.cm/u.s
        stars.vy = part_data[star_ind,vely_ind]   | u.cm/u.s
        stars.vz = part_data[star_ind,velz_ind]   | u.cm/u.s
        # Set an index attribute. Useful for some functions/codes in AMUSE.
        stars.index_of_the_particle = np.arange(num_parts)

    
    else:
        print "Error: No particles found in this file!"
        stars = None
    
    return stars