Exemplo n.º 1
0
def fractal_model(N, F=1.6, x_label='x [length]', y_label='y [length]'):
    fig = single_frame(x_label, y_label, xsize=8, ysize=8)
    ax = pyplot.gca()
    model = new_fractal_cluster_model(N=N,
                                      fractal_dimension=1.6,
                                      random_seed=42)
    plot_projected_density(model, col=2)
    pyplot.savefig("fractal_model")
Exemplo n.º 2
0
def fractal_model(N, F=1.6, x_label = 'x [length]', y_label='y [length]'):
    fig = single_frame(x_label, y_label, xsize=8, ysize=8)
    ax = pyplot.gca()
    model = new_fractal_cluster_model(N=N, fractal_dimension=1.6,
                                      random_seed=42)
    plot_projected_density(model, col=2)
    file = "fractal_model.png"
    pyplot.savefig(file)
    print('Saved figure in file', file)
Exemplo n.º 3
0
def fractal_model(N, F=1.6, x_label = 'x [length]', y_label='y [length]'):
    fig = single_frame(x_label, y_label, xsize=8, ysize=8)
    ax = pyplot.gca()
    model = new_fractal_cluster_model(N=N, fractal_dimension=1.6,
                                      random_seed=42)
    plot_projected_density(model, col=2)
    file = "fractal_model.png"
    pyplot.savefig(file)
    print 'Saved figure in file', file
Exemplo n.º 4
0
def make_fractal_cluster(masses, name, converter, Fd=1.6):
    stars = new_fractal_cluster_model(N=len(masses),
                                      convert_nbody=converter,
                                      do_scale=False,
                                      fractal_dimension=Fd)
    stars.type = "star"
    stars.name = name
    stars.mass = masses
    stars.move_to_center()
    return stars
Exemplo n.º 5
0
def main(N, Rvir, Qvir, Fd, t_end, filename):
    masses = new_kroupa_mass_distribution(N, 100 | units.MSun)
    converter = nbody_system.nbody_to_si(masses.sum(), Rvir)
    bodies = new_fractal_cluster_model(N=N,
                                       fractal_dimension=Fd,
                                       convert_nbody=converter)
    bodies.scale_to_standard(converter, virial_ratio=Qvir)
    bodies.stellar_mass = masses
    bodies.disk_mass = 0.01 * bodies.stellar_mass
    bodies.mass = bodies.stellar_mass + bodies.disk_mass
    bodies.accreted_mass = 0 | units.MSun
    bodies.disk_radius = 400 | units.AU
    bodies.radius = 10 * bodies.disk_radius

    gravity = ph4(converter, number_of_workers=2)
    gravity.parameters.epsilon_squared = (100 | units.AU)**2
    gravity.particles.add_particles(bodies)
    channel_from_gravity = gravity.particles.new_channel_to(bodies)
    channel_to_gravity = bodies.new_channel_to(gravity.particles)

    stopping_condition = gravity.stopping_conditions.collision_detection
    stopping_condition.enable()

    write_set_to_file(bodies.savepoint(0 | units.Myr),
                      filename,
                      'hdf5',
                      append_to_file=False)

    Etot_init = gravity.kinetic_energy + gravity.potential_energy
    Etot_prev = Etot_init
    ###BOOKLISTSTOP1###

    ###BOOKLISTSTART0###
    dt = t_end / 10.
    time = 0 | units.yr
    while gravity.model_time < t_end:
        time += dt
        evolve_system_to(time, gravity, bodies, stopping_condition,
                         channel_from_gravity, channel_to_gravity)
        write_set_to_file(bodies.savepoint(gravity.model_time), filename,
                          'hdf5')
        Etot = gravity.kinetic_energy + gravity.potential_energy
        print "T=", gravity.model_time,
        print "E= ", Etot, "Q= ", \
              gravity.kinetic_energy/gravity.potential_energy
        print "dE=", (Etot - Etot_init) / Etot, "ddE=", (Etot -
                                                         Etot_prev) / Etot
        Etot_init -= (Etot_prev - Etot)
        Etot_prev = Etot

    gravity.stop()
Exemplo n.º 6
0
def _main(N=10, W=None): 

    f, ax1, ax2, ax3, ax4 = quad_frame("X [length]", "Y [length]")
    model = new_plummer_model(N)
    plot_projected_density(model, ax1)

    model = new_king_model(N, 9.0)
    plot_projected_density(model, ax2)

    model = new_halogen_model(N, alpha=1., beta=5., gamma=0.5)
    plot_projected_density(model, ax3)

    model = new_fractal_cluster_model(N=N, fractal_dimension=1.6)
    plot_projected_density(model, ax4)

#    pyplot.show()
    pyplot.savefig("density_distributions.eps", bbox_inches="tight")  
def main(N, Rvir, Qvir, Fd):
    numpy.random.seed(12345)
    masses = new_kroupa_mass_distribution(N, 100 | units.MSun)
    converter = nbody_system.nbody_to_si(masses.sum(), Rvir)
    bodies = new_fractal_cluster_model(N=N,
                                       fractal_dimension=Fd,
                                       random_seed=1234,
                                       convert_nbody=converter)

    print "N all=", len(bodies)
    clumps = find_clumps_with_hop(bodies, converter)
    print "N blob=", len(clumps)

    bodies.scale_to_standard(converter, virial_ratio=Qvir)
    for clump in clumps:
        clump.scale_to_standard(converter, virial_ratio=0.5)

    lim = 10
    plot_single_image(clumps, lim)
def main(N, Rvir, Qvir, Fd, seed):
    numpy.random.seed(seed)
    
    masses = new_kroupa_mass_distribution(N)

    converter = nbody_system.nbody_to_si(masses.sum(), Rvir)
    bodies = new_fractal_cluster_model(N=N, fractal_dimension=Fd,
                                       random_seed=seed,
                                       convert_nbody=converter)
    bodies.mass = masses
    bodies.move_to_center()
    bodies.scale_to_standard(converter, virial_ratio=Qvir)

    clumps = find_clumps_with_hop(bodies, converter)
    
    for clump in clumps:
        clump.scale_to_standard(converter, virial_ratio=0.5)

    plot_single_image(clumps, lim=50)
Exemplo n.º 9
0
def main(N, Rvir, Qvir, Fd, seed):
    numpy.random.seed(seed)
    
    masses = new_kroupa_mass_distribution(N)

    converter = nbody_system.nbody_to_si(masses.sum(), Rvir)
    bodies = new_fractal_cluster_model(N=N, fractal_dimension=Fd,
                                       random_seed=seed,
                                       convert_nbody=converter)
    bodies.mass = masses
    bodies.move_to_center()
    bodies.scale_to_standard(converter, virial_ratio=Qvir)

    clumps = find_clumps_with_hop(bodies, converter)
    
    for clump in clumps:
        clump.scale_to_standard(converter, virial_ratio=0.5)

    plot_single_image(clumps, lim=50)
Exemplo n.º 10
0
def main(N, Rvir, Qvir, Fd):

    filename = 'Cl_N%g_R%gpc_Q%g_F%g.h5' % (N, Rvir.value_in(
        units.parsec), Qvir, Fd)
    t_end = 1.0 | units.Myr
    dt = 0.1 | units.Myr

    Mmax = 100 | units.MSun
    masses = new_kroupa_mass_distribution(N, Mmax)
    Mtot_init = masses.sum()
    converter = nbody_system.nbody_to_si(Mtot_init, Rvir)
    bodies = new_fractal_cluster_model(N=N,
                                       fractal_dimension=Fd,
                                       convert_nbody=converter)
    bodies.scale_to_standard(converter, virial_ratio=Qvir)
    bodies.stellar_mass = masses
    bodies.disk_mass = 0.1 * bodies.stellar_mass
    bodies.mass = bodies.stellar_mass + bodies.disk_mass
    bodies.accreted_mass = 0 | units.MSun
    bodies.disk_radius = 400 | units.AU
    bodies.radius = 10 * bodies.disk_radius

    gravity = ph4(converter)
    gravity.parameters.epsilon_squared = (100 | units.AU)**2
    gravity.particles.add_particles(bodies)

    channel_from_gd_to_framework = gravity.particles.new_channel_to(bodies)
    channel_from_framework_to_gd = bodies.new_channel_to(gravity.particles)

    stopping_condition = gravity.stopping_conditions.collision_detection
    stopping_condition.enable()

    Johannes = Kepler(converter)
    Johannes.initialize_code()

    write_set_to_file(bodies.savepoint(0 | units.Myr),
                      filename,
                      'hdf5',
                      append_to_file=False)

    Etot_init = gravity.kinetic_energy + gravity.potential_energy
    Etot_prev = Etot_init

    Nenc = 0
    dEk_enc = zero
    dEp_enc = zero
    time = 0.0 | t_end.unit
    while time < t_end:
        time += dt

        gravity.evolve_model(time)
        Etot_prev_se = gravity.kinetic_energy + gravity.potential_energy
        while stopping_condition.is_set():
            channel_from_gd_to_framework.copy()
            Ek_enc = gravity.kinetic_energy
            Ep_enc = gravity.potential_energy
            for ci in range(len(stopping_condition.particles(0))):
                particles_in_encounter = Particles(particles=[
                    stopping_condition.particles(0)[ci],
                    stopping_condition.particles(1)[ci]
                ])
                local_particles_in_encounter = particles_in_encounter.get_intersecting_subset_in(
                    bodies)

                resolve_close_encounter(gravity.model_time,
                                        local_particles_in_encounter, Johannes)
                Nenc += 1
                print("At time=", gravity.model_time.value_in(units.Myr),
                      "Nenc=", Nenc, "Rdisk=",
                      local_particles_in_encounter.disk_radius.in_(units.AU))
                channel_from_framework_to_gd.copy_attributes(["radius"])
            dEk_enc += Ek_enc - gravity.kinetic_energy
            dEp_enc += Ep_enc - gravity.potential_energy

            gravity.evolve_model(time)

        channel_from_framework_to_gd.copy_attributes(["mass"])

        write_set_to_file(bodies.savepoint(time), filename, 'hdf5')

        Ekin = gravity.kinetic_energy
        Epot = gravity.potential_energy
        Etot = Ekin + Epot
        dE = Etot_prev - Etot
        dE_se = Etot_prev_se - Etot
        Mtot = bodies.mass.sum()
        print("T=", time, end=' ')
        print("M=", Mtot, "(dM[SE]=", Mtot / Mtot_init, ")", end=' ')
        print("E= ", Etot, "Q= ", Ekin / Epot, end=' ')
        print("dE=", (Etot_init - Etot) / Etot,
              "ddE=", (Etot_prev - Etot) / Etot,
              end=' ')
        print("(dE[SE]=", dE_se / Etot, ")")
        print("dE(enc)=", dEk_enc, dEp_enc)
        Etot_init -= dE
        Etot_prev = Etot

    gravity.stop()
    Johannes.stop()
Exemplo n.º 11
0
def main(N, Rvir, Qvir, Fd):

    filename= 'Cl_N%g_R%gpc_Q%g_F%g.h5'%(N, Rvir.value_in(units.parsec), Qvir, Fd)
    t_end = 1.0 | units.Myr
    dt = 0.1 | units.Myr

    Mmax = 100 | units.MSun
    masses = new_kroupa_mass_distribution(N, Mmax)
    Mtot_init = masses.sum()
    converter=nbody_system.nbody_to_si(Mtot_init,Rvir)
    bodies = new_fractal_cluster_model(N=N, fractal_dimension=Fd, 
                                           convert_nbody=converter)
    bodies.scale_to_standard(converter, virial_ratio=Qvir)
    bodies.stellar_mass = masses
    bodies.disk_mass = 0.1*bodies.stellar_mass
    bodies.mass = bodies.stellar_mass + bodies.disk_mass
    bodies.accreted_mass = 0 | units.MSun
    bodies.disk_radius = 400 | units.AU
    bodies.radius = 10 * bodies.disk_radius

    gravity = ph4(converter)
    gravity.parameters.epsilon_squared = (100|units.AU)**2
    gravity.particles.add_particles(bodies)

    channel_from_gd_to_framework = gravity.particles.new_channel_to(bodies)
    channel_from_framework_to_gd = bodies.new_channel_to(gravity.particles)

    stopping_condition = gravity.stopping_conditions.collision_detection
    stopping_condition.enable()

    Johannes = Kepler(converter)
    Johannes.initialize_code()

    write_set_to_file(bodies.savepoint(0|units.Myr), filename, 'hdf5', append_to_file=False)
    
    Etot_init = gravity.kinetic_energy + gravity.potential_energy
    Etot_prev = Etot_init

    Nenc = 0
    dEk_enc = zero    
    dEp_enc = zero
    time = 0.0 | t_end.unit
    while time < t_end:
        time += dt

        gravity.evolve_model(time)
        Etot_prev_se = gravity.kinetic_energy + gravity.potential_energy
        while stopping_condition.is_set():
            channel_from_gd_to_framework.copy()
            Ek_enc = gravity.kinetic_energy 
            Ep_enc = gravity.potential_energy
            for ci in range(len(stopping_condition.particles(0))): 
                particles_in_encounter = Particles(particles=[stopping_condition.particles(0)[ci], stopping_condition.particles(1)[ci]])
                local_particles_in_encounter = particles_in_encounter.get_intersecting_subset_in(bodies)

                resolve_close_encounter(gravity.model_time, local_particles_in_encounter, Johannes)
                Nenc+=1
                print "At time=", gravity.model_time.value_in(units.Myr), "Nenc=", Nenc, "Rdisk=", local_particles_in_encounter.disk_radius.in_(units.AU)
                channel_from_framework_to_gd.copy_attributes(["radius"])
            dEk_enc += Ek_enc - gravity.kinetic_energy 
            dEp_enc += Ep_enc - gravity.potential_energy

            gravity.evolve_model(time)

        channel_from_framework_to_gd.copy_attributes(["mass"])

        write_set_to_file(bodies.savepoint(time), filename, 'hdf5')

        Ekin = gravity.kinetic_energy 
        Epot = gravity.potential_energy
        Etot = Ekin + Epot
        dE = Etot_prev-Etot
        dE_se = Etot_prev_se-Etot
        Mtot = bodies.mass.sum()
        print "T=", time, 
        print "M=", Mtot, "(dM[SE]=", Mtot/Mtot_init, ")",
        print "E= ", Etot, "Q= ", Ekin/Epot,
        print "dE=", (Etot_init-Etot)/Etot, "ddE=", (Etot_prev-Etot)/Etot, 
        print "(dE[SE]=", dE_se/Etot, ")"
        print "dE(enc)=", dEk_enc, dEp_enc
        Etot_init -= dE
        Etot_prev = Etot

    gravity.stop()
    Johannes.stop()
Exemplo n.º 12
0
def generate_initial_conditions(
        number_of_stars = 100,
        number_of_gas_particles = 10**5,
        star_formation_efficiency = 0.1,
        virial_radius = 0.33 | units.parsec,
        virial_ratio = 1.0,
        use_fractal = False):
    
    numpy.random.seed(12345678)
    seed_fractal = 312357271
    
    masses = new_salpeter_mass_distribution(number_of_stars,
                                            mass_min=1|units.MSun)
#    masses = new_kroupa_mass_distribution(number_of_stars
    total_stellar_mass = masses.sum()
    total_mass = total_stellar_mass / star_formation_efficiency
    converter = nbody_system.nbody_to_si(total_mass, virial_radius)
    if use_fractal:
        stars = new_fractal_cluster_model(number_of_stars,
                                          convert_nbody=converter,
                                          do_scale=False,
                                          fractal_dimension=1.6,
                                          random_seed=seed_fractal)
    else:
        stars = new_plummer_model(number_of_stars, convert_nbody=converter,
                                  do_scale=False)
    stars.mass = masses
    stars.move_to_center()
    print("scaling positions to match virial_radius")
    stars.position *= virial_radius / stars.virial_radius()
    print("scaling velocities to match virial_ratio")
    stars.velocity *= numpy.sqrt(virial_ratio * converter.to_si(0.5|nbody_system.energy) * star_formation_efficiency / stars.kinetic_energy())
    
    print("new_gas_plummer_distribution")
    gas = new_gas_plummer_distribution(
        number_of_gas_particles, 
        total_mass = (total_mass - total_stellar_mass), 
        virial_radius = virial_radius, 
        type = "fcc")
    gas.h_smooth = 0.0 | units.parsec
    # eat away gas.
    mgas = gas[0].mass
    print("Ngas=", len(gas))
    for si in stars:
        m = si.mass
        nremoved = 0
        while m>mgas:
            gi = si.as_set().nearest_neighbour(gas)
            nremoved += 1
            m-= gi.mass
            gas -= gi
        print("removed:", nremoved, si.mass.in_(units.MSun))
    print("Ngas=", len(gas))
        
    filename = "YSC_{0}_stars{1}_gas{2}k_" \
                 .format("fractal" if use_fractal else "plummer",
        number_of_stars, number_of_gas_particles/1000)
    print("Writing initial conditions to", filename, "+ stars/gas.amuse")
    write_set_to_file(stars, filename+"stars.amuse", "amuse",
                      append_to_file=False)
    write_set_to_file(gas, filename+"gas.amuse", "amuse", append_to_file=False)
    with open(filename+"info.pkl", "wb") as outfile:
        pickle.dump([converter], outfile)
    return stars, gas, filename             
    pyplot.ylim((-lim, lim))

    save_file = 'FractalCluster.png'
    pyplot.savefig(save_file)
    print '\nSaved figure in file', save_file, '\n'
    pyplot.show()


N = 2000  # Number of particles (stars) for the cluster
Rvir = 0.5 | units.parsec  # Virial radius of the cluster
Qvir = 0.5  # Virial ratio of the cluster (Q=0.5 == equilibrium)
Fd = 1.6  # Fractal dimension
numpy.random.seed(1234)  # Random seed for star positions

masses = new_kroupa_mass_distribution(N)  # Stellar masses
converter = nbody_system.nbody_to_si(
    masses.sum(),
    Rvir)  # Converter, turn n-body system units into relevant physical units

# Create new fractal cluster
bodies = new_fractal_cluster_model(N=N,
                                   fractal_dimension=Fd,
                                   random_seed=1234,
                                   convert_nbody=converter)

bodies.mass = masses  # Assign stellar masses to cluster particles
bodies.move_to_center()
bodies.scale_to_standard(converter, virial_ratio=Qvir)

plot_single_image(bodies, lim=1)