Exemplo n.º 1
0
 def result(self):
     self.setup_lookup_tables()
     self.setup_variates()
     
     sph_particles = Particles(self.number_of_sph_particles)
     sph_particles.position = self.new_particle_positions()
     sph_particles.velocity = self.new_particle_velocities()
     sph_particles.u = self.new_particle_specific_internal_energies()
     sph_particles.rho = self.new_particle_densities()
     
     sph_particles.mass = (self.mass.number * 1.0 / self.number_of_sph_particles) | self.mass.unit
     # Crude estimate of the smoothing length; the SPH code will calculate the true value itself.
     sph_particles.h_smooth = (self.grid.get_volume() * 50.0/self.number_of_sph_particles)**(1/3.0)
     
     return sph_particles
Exemplo n.º 2
0
    def setup_sph_code(self, sph_code, number_of_particles, L, rho, u):
        converter = ConvertBetweenGenericAndSiUnits(L, rho, constants.G)
        sph_code = sph_code(converter, mode = 'periodic')#, redirection = 'none')
        sph_code.parameters.periodic_box_size = 10.0 | units.parsec
        gas = Particles(number_of_particles)
        gas.mass = (rho * L**3) / number_of_particles
        numpy.random.seed(12345)
        gas.x = L * numpy.random.uniform(0.0, 1.0, number_of_particles)
        gas.y = L * numpy.random.uniform(0.0, 1.0, number_of_particles)
        gas.z = L * numpy.random.uniform(0.0, 1.0, number_of_particles)
        gas.vx = numpy.zeros(number_of_particles) | units.cm / units.s
        gas.vy = numpy.zeros(number_of_particles) | units.cm / units.s
        gas.vz = numpy.zeros(number_of_particles) | units.cm / units.s
        gas.u = u
        if isinstance(sph_code, Fi):
            sph_code.parameters.self_gravity_flag = False
            sph_code.parameters.timestep = 0.1 | generic_unit_system.time
            gas.h_smooth = L / number_of_particles**(1/3.0)
            gas.position -= 0.5 * L
        
        sph_code.gas_particles.add_particles(gas)
        sph_code.commit_particles()
#~        sph_code.evolve_model(0.01 | units.Myr)
        return sph_code
Exemplo n.º 3
0
def res_increase(
    gas=None,
    recalculate_h_density=False,
    seed=123,
    make_cutout=False,
    make_circular_cutout=False,
    circular_rmax=3000 | units.pc,
    x_center=None,
    y_center=None,
    width=None,
    res_increase_factor=85,
):
    numpy.random.seed(seed)
    if gas is None:
        if len(sys.argv) > 2:
            from amuse.io import read_set_from_file
            filename = sys.argv[1]
            res_increase_factor = int(sys.argv[2])
            gas = read_set_from_file(filename, 'amuse')
            if hasattr(gas, "itype"):
                gas = gas[gas.itype == 1]
                del gas.itype
        else:
            from amuse.ic.gasplummer import new_plummer_gas_model
            converter = nbody_system.nbody_to_si(10000 | units.MSun,
                                                 10 | units.pc)
            filename = "test"
            gas = new_plummer_gas_model(10000, converter)
            res_increase_factor = 85
            sph = Fi(converter, mode="openmp")
            gas_in_code = sph.gas_particles.add_particles(gas)
            gas.h_smooth = gas_in_code.h_smooth
            gas.density = gas_in_code.density
            sph.stop()
            write_set_to_file(gas, "old-%s" % filename, "amuse")
            print("old gas created")

    if make_circular_cutout:
        r2 = gas.x**2 + gas.y**2
        cutout = gas[r2 <= circular_rmax**2]
        gas = cutout
        converter = nbody_system.nbody_to_si(gas.total_mass(), width)
        sph = Fi(converter, mode="openmp")
        gas_in_code = sph.gas_particles.add_particles(gas)
        gas.h_smooth = gas_in_code.h_smooth
        gas.density = gas_in_code.density
        sph.stop()

    if make_cutout:
        if (x_center is None or y_center is None or width is None):
            raise Exception("Need to set x_center, y_center and width!")
        cutout = gas.sorted_by_attribute("x")
        cutout = cutout[cutout.x - x_center < width / 2]
        cutout = cutout[cutout.x - x_center > -width / 2]
        cutout = cutout.sorted_by_attribute("y")
        cutout = cutout[cutout.y - y_center < width / 2]
        cutout = cutout[cutout.y - y_center > -width / 2]
        gas = cutout
        converter = nbody_system.nbody_to_si(gas.total_mass(), width)
        sph = Fi(converter, mode="openmp")
        gas_in_code = sph.gas_particles.add_particles(gas)
        gas.h_smooth = gas_in_code.h_smooth
        gas.density = gas_in_code.density
        sph.stop()
        # boundary = test_cutout.h_smooth.max()

    if res_increase_factor == 1:
        return gas

    original_number_of_particles = len(gas)
    new_number_of_particles = (res_increase_factor *
                               original_number_of_particles)

    converter = nbody_system.nbody_to_si(
        gas.total_mass(),
        1 | units.kpc,
    )

    new_gas = Particles(new_number_of_particles)
    # new_gas.h_smooth = gas.h_smooth

    shells, particles_per_shell, shell_radii = find_shell_struct(
        res_increase_factor)

    relative_positions = pos_shift(
        shell_radii,
        particles_per_shell,
        res_increase_factor=res_increase_factor,
    )
    relative_velocities = numpy.zeros(
        res_increase_factor * 3, dtype=float).reshape(res_increase_factor,
                                                      3) | gas.velocity.unit

    random_samples = 50
    number_of_particles = len(gas)
    starting_index = 0
    for r in range(random_samples):
        print("%i / %i random sample done" % (r, random_samples))
        number_of_particles_remaining = len(gas)
        number_of_particles_in_sample = min(
            number_of_particles_remaining,
            int(1 + number_of_particles / random_samples))

        gas_sample = gas.random_sample(number_of_particles_in_sample).copy()
        gas.remove_particles(gas_sample)
        end_index = (starting_index +
                     number_of_particles_in_sample * res_increase_factor)
        new_gas_sample = new_gas[starting_index:end_index]
        psi = 2 * numpy.pi * numpy.random.random()
        theta = 2 * numpy.pi * numpy.random.random()
        phi = 2 * numpy.pi * numpy.random.random()
        relative_positions = rotated(relative_positions, phi, theta, psi)
        # print(len(gas_sample), len(new_gas_sample))
        for i in range(res_increase_factor):
            new_gas_sample[i::res_increase_factor].mass = (gas_sample.mass /
                                                           res_increase_factor)
            new_gas_sample[i::res_increase_factor].x = (
                gas_sample.x + relative_positions[i, 0] * gas_sample.h_smooth)
            new_gas_sample[i::res_increase_factor].y = (
                gas_sample.y + relative_positions[i, 1] * gas_sample.h_smooth)
            new_gas_sample[i::res_increase_factor].z = (
                gas_sample.z + relative_positions[i, 2] * gas_sample.h_smooth)
            new_gas_sample[i::res_increase_factor].vx = (
                gas_sample.vx + relative_velocities[i, 0])
            new_gas_sample[i::res_increase_factor].vy = (
                gas_sample.vy + relative_velocities[i, 1])
            new_gas_sample[i::res_increase_factor].vz = (
                gas_sample.vz + relative_velocities[i, 2])
            new_gas_sample[i::res_increase_factor].density = gas_sample.density
            new_gas_sample[i::res_increase_factor].u = gas_sample.u
        starting_index += number_of_particles_in_sample * res_increase_factor
    new_gas.h_smooth = ((3 * new_gas.mass / (4 * pi * new_gas.density))**(1 /
                                                                          3))

    # sph = Fi(converter, mode="openmp", redirection="none")
    # new_gas_in_code = sph.gas_particles.add_particles(new_gas)
    # new_gas.h_smooth = new_gas_in_code.h_smooth
    # new_gas.density = new_gas_in_code.density
    # sph.stop()

    print("particles now have a mass of %s" %
          (new_gas[0].mass.in_(units.MSun)))
    return new_gas
Exemplo n.º 4
0
    def make_xyz(self):
        from amuse.community.fi.interface import Fi

        N = self.targetN
        target_rms = self.target_rms

        L = 1 | nbody_system.length
        dt = 0.01 | nbody_system.time
        x, y, z = uniform_random_unit_cube(N).make_xyz()
        vx, vy, vz = uniform_unit_sphere(N).make_xyz()

        p = Particles(N)
        p.x = L * x
        p.y = L * y
        p.z = L * z
        p.h_smooth = 0. * L
        p.vx = 0.1 * vx | (nbody_system.speed)
        p.vy = 0.1 * vy | (nbody_system.speed)
        p.vz = 0.1 * vz | (nbody_system.speed)
        p.u = (0.1 * 0.1) | nbody_system.speed**2
        p.mass = (8. / N) | nbody_system.mass

        sph = Fi(use_gl=False, mode='periodic', redirection='none')
        sph.initialize_code()

        sph.parameters.use_hydro_flag = True
        sph.parameters.radiation_flag = False
        sph.parameters.self_gravity_flag = False
        sph.parameters.gamma = 1.
        sph.parameters.isothermal_flag = True
        sph.parameters.integrate_entropy_flag = False
        sph.parameters.timestep = dt
        sph.parameters.verbosity = 0
        sph.parameters.periodic_box_size = 2 * L
        sph.parameters.artificial_viscosity_alpha = 1.
        sph.parameters.beta = 2.
        sph.commit_parameters()
        sph.gas_particles.add_particles(p)
        sph.commit_particles()

        #        sph.start_viewer()

        t = 0. | nbody_system.time
        rms = 1.
        minrms = 1.
        i = 0
        while rms > target_rms:
            i += 1
            t = t + (0.25 | nbody_system.time)
            sph.evolve_model(t)
            rho = sph.particles.rho.value_in(nbody_system.density)
            rms = rho.std() / rho.mean()
            minrms = min(minrms, rms)
            if rms > 2. * minrms or i > 300:
                print " RMS(rho) convergence warning:", i, rms, minrms
            if i > 100000:
                print "i> 100k steps - not sure about this..."
                print " rms:", rms
                break

        x = sph.particles.x.value_in(nbody_system.length)
        y = sph.particles.y.value_in(nbody_system.length)
        z = sph.particles.z.value_in(nbody_system.length)

        del sph
        return x, y, z
Exemplo n.º 5
0
 def glass(self):
     from amuse.community.fi.interface import Fi
     
     if self.target_rms < 0.0001:
         print "warning: target_rms may not succeed"
     if self.number_of_particles < 1000:
         print "warning: not enough particles"
     
     N = 2 * self.number_of_particles
     L = 1 | nbody_system.length
     dt = 0.01 | nbody_system.time
     
     x, y, z = self._random_cube(N)
     vx,vy,vz= self.random(N)
     
     p = Particles(N)
     p.x = L * x
     p.y = L * y
     p.z = L * z
     p.h_smooth = 0.0 | nbody_system.length
     p.vx = (0.1 | nbody_system.speed) * vx
     p.vy = (0.1 | nbody_system.speed) * vy
     p.vz = (0.1 | nbody_system.speed) * vz
     p.u = (0.1*0.1) | nbody_system.speed**2
     p.mass = (8.0/N) | nbody_system.mass
     
     sph = Fi(mode = 'periodic', redirection = 'none')
     sph.initialize_code()
     
     sph.parameters.use_hydro_flag = True
     sph.parameters.radiation_flag = False
     sph.parameters.self_gravity_flag = False
     sph.parameters.gamma = 1.0
     sph.parameters.isothermal_flag = True
     sph.parameters.integrate_entropy_flag = False
     sph.parameters.timestep = dt
     sph.parameters.verbosity = 0
     sph.parameters.periodic_box_size = 2 * L
     sph.parameters.artificial_viscosity_alpha = 1.0
     sph.parameters.beta = 2.0
     sph.commit_parameters()
     sph.gas_particles.add_particles(p)
     sph.commit_particles()
     
     t = 0.0 | nbody_system.time
     rms = 1.0
     minrms = 1.0
     i = 0
     while rms > self.target_rms:
         i += 1
         t += (0.25 | nbody_system.time)
         sph.evolve_model(t)
         rho = sph.particles.rho.value_in(nbody_system.density)
         rms = rho.std()/rho.mean()
         minrms = min(minrms, rms)
         if (rms > 2.0*minrms) or (i > 300):
             print " RMS(rho) convergence warning:", i, rms, minrms
         if i > 100000:
             print "i> 100k steps - not sure about this..."
             print " rms:", rms
             break
     
     x = sph.particles.x.value_in(nbody_system.length)
     y = sph.particles.y.value_in(nbody_system.length)
     z = sph.particles.z.value_in(nbody_system.length)
     sph.stop()
     del sph
     return self._cutout_sphere(x, y, z)
Exemplo n.º 6
0
 def glass(self):
     from amuse.community.fi.interface import Fi
     
     if self.target_rms < 0.0001:
         print("warning: target_rms may not succeed")
     if self.number_of_particles < 1000:
         print("warning: not enough particles")
     
     N = 2 * self.number_of_particles
     L = 1 | nbody_system.length
     dt = 0.01 | nbody_system.time
     
     x, y, z = self._random_cube(N)
     vx,vy,vz= self.random(N)
     
     p = Particles(N)
     p.x = L * x
     p.y = L * y
     p.z = L * z
     p.h_smooth = 0.0 | nbody_system.length
     p.vx = (0.1 | nbody_system.speed) * vx[:N]
     p.vy = (0.1 | nbody_system.speed) * vy[:N]
     p.vz = (0.1 | nbody_system.speed) * vz[:N]
     p.u = (0.1*0.1) | nbody_system.speed**2
     p.mass = (8.0/N) | nbody_system.mass
     
     sph = Fi(mode = 'periodic', redirection = 'none')
     sph.initialize_code()
     
     sph.parameters.use_hydro_flag = True
     sph.parameters.radiation_flag = False
     sph.parameters.self_gravity_flag = False
     sph.parameters.gamma = 1.0
     sph.parameters.isothermal_flag = True
     sph.parameters.integrate_entropy_flag = False
     sph.parameters.timestep = dt
     sph.parameters.verbosity = 0
     sph.parameters.periodic_box_size = 2 * L
     sph.parameters.artificial_viscosity_alpha = 1.0
     sph.parameters.beta = 2.0
     sph.commit_parameters()
     sph.gas_particles.add_particles(p)
     sph.commit_particles()
     
     t = 0.0 | nbody_system.time
     rms = 1.0
     minrms = 1.0
     i = 0
     while rms > self.target_rms:
         i += 1
         t += (0.25 | nbody_system.time)
         sph.evolve_model(t)
         rho = sph.particles.rho.value_in(nbody_system.density)
         rms = rho.std()/rho.mean()
         minrms = min(minrms, rms)
         if (rms > 2.0*minrms) or (i > 300):
             print(" RMS(rho) convergence warning:", i, rms, minrms)
         if i > 100000:
             print("i> 100k steps - not sure about this...")
             print(" rms:", rms)
             break
     
     x = sph.particles.x.value_in(nbody_system.length)
     y = sph.particles.y.value_in(nbody_system.length)
     z = sph.particles.z.value_in(nbody_system.length)
     sph.stop()
     del sph
     return self._cutout_sphere(x, y, z)
Exemplo n.º 7
0
def iliev_test_7_ic(N=10000, Ns=10, L=6.6 | units.kpc):
    print "Initializing iliev_test_7"

    mp = rhoinit * (2 * L)**3 / N
    Nc = ((rhoclump * 4 * constants.pi / 3 * (0.8 | units.kpc)**3) / mp)
    Nc = int(Nc)

    print Nc

    try:
        f = open("glass%9.9i.pkl" % N, "rb")
        x, y, z = cPickle.load(f)
        f.close()
    except:
        x, y, z = glass_unit_cube(N, target_rms=0.05).make_xyz()
        f = open("glass%9.9i.pkl" % N, "wb")
        cPickle.dump((x, y, z), f)
        f.close()

    sel = numpy.where(((x -
                        (5. / 6.6))**2 + y**2 + z**2)**0.5 > (0.8 / 6.6))[0]

    x = x[sel]
    y = y[sel]
    z = z[sel]

    p = Particles(len(x))
    print len(x)
    # set particles homogeneously in space
    p.x = L * x
    p.y = L * y
    p.z = L * z

    # set other properties
    p.h_smooth = 0. | units.parsec
    p.vx = 0. | (units.km / units.s)
    p.vy = 0. | (units.km / units.s)
    p.vz = 0. | (units.km / units.s)
    p.u = uinit
    p.rho = rhoinit
    p.mass = mp
    p.flux = 0. | (units.s**-1)
    p.xion = 0. | units.none

    sources = Particles(Ns)
    x, y, z = uniform_unit_sphere(Ns).make_xyz()
    if Ns == 1:
        x, y, z = 0., 0., 0.

    sources.x = -(5. | units.kpc) + L * x * (1. / N)**(1. / 3) / 10
    sources.y = L * y * (1. / N)**(1. / 3) / 10
    sources.z = L * z * (1. / N)**(1. / 3) / 10
    sources.luminosity = (1.2e52 / Ns) | (units.s**-1)
    sources.SpcType = 1.

    clump = Particles(Nc)
    x, y, z = uniform_unit_sphere(Nc).make_xyz()

    clump.x = (5. | units.kpc) + (0.8 | units.kpc) * x
    clump.y = (0.8 | units.kpc) * y
    clump.z = (0.8 | units.kpc) * z

    clump.h_smooth = 0. | units.parsec
    clump.vx = 0. | (units.km / units.s)
    clump.vy = 0. | (units.km / units.s)
    clump.vz = 0. | (units.km / units.s)
    clump.u = uclump
    clump.rho = rhoclump
    clump.mass = mp
    clump.flux = 0. | (units.s**-1)
    clump.xion = 0. | units.none

    p.add_particles(clump)

    return p, sources
Exemplo n.º 8
0
def set_gas_and_src(Ngas, Lbox, Lsrc, rho_init, T_init):
    """ Given the number of particles and box parameters, sets up gas 
  particles and a source particle. """

    gamma = 5. / 3.
    mu = 1.0 | units.amu

    # set particles homogeneously in space from -Lbox/2 to Lbox/2
    # NOTE reset Ngas

    if args.grid == 1:

        x, y, z = regular_grid_unit_cube(Ngas).make_xyz()
        #x,y,z = body_centered_grid_unit_cube(Ngas).make_xyz()

        Ngas = len(x)
        gas = Particles(Ngas)

        gas.x = x * Lbox / 2
        gas.y = y * Lbox / 2
        gas.z = z * Lbox / 2

    elif args.grid == 0:

        gas = Particles(Ngas)

        gas.x = Lbox / 2 * numpy.random.uniform(-1., 1., Ngas)
        gas.y = Lbox / 2 * numpy.random.uniform(-1., 1., Ngas)
        gas.z = Lbox / 2 * numpy.random.uniform(-1., 1., Ngas)

    else:

        print 'args.grid not recognized'
        print 'args.grid = ', args.grid
        sys.exit(1)


# set zero velocities

    gas.vx = 0. | (units.km / units.s)
    gas.vy = 0. | (units.km / units.s)
    gas.vz = 0. | (units.km / units.s)

    # set other properties

    gas.h_smooth = 0.0 * Lbox  # will be set in call to hydro code

    gas.u = 1 / (gamma - 1) * constants.kB * T_init / mu

    gas.rho = rho_init

    gas.mass = rho_init * Lbox**3 / Ngas

    gas.xion = 1.2e-3

    #gas.dudt=gas.u/(1.| units.Myr)

    # set source in the center of the box

    sources = Particles(1)
    sources.x = 0.0 * Lbox
    sources.y = 0.0 * Lbox
    sources.z = 0.0 * Lbox
    sources.luminosity = Lsrc

    return gas, sources
Exemplo n.º 9
0
    def make_xyz(self):
        from amuse.community.fi.interface import Fi

        N=self.targetN
        target_rms=self.target_rms

        L=1| nbody_system.length
        dt=0.01 | nbody_system.time
        x,y,z=uniform_random_unit_cube(N).make_xyz()
        vx,vy,vz=uniform_unit_sphere(N).make_xyz()

        p=Particles(N)
        p.x=L*x
        p.y=L*y
        p.z=L*z
        p.h_smooth=0. * L
        p.vx= 0.1*vx | (nbody_system.speed)
        p.vy= 0.1*vy | (nbody_system.speed)
        p.vz= 0.1*vz | (nbody_system.speed)
        p.u= (0.1*0.1) | nbody_system.speed**2 
        p.mass=(8./N) | nbody_system.mass

        sph=Fi(use_gl=False,mode='periodic',redirection='none')   
        sph.initialize_code()

        sph.parameters.use_hydro_flag=True
        sph.parameters.radiation_flag=False
        sph.parameters.self_gravity_flag=False
        sph.parameters.gamma=1.
        sph.parameters.isothermal_flag=True
        sph.parameters.integrate_entropy_flag=False
        sph.parameters.timestep=dt  
        sph.parameters.verbosity=0
        sph.parameters.periodic_box_size=2*L
        sph.parameters.artificial_viscosity_alpha = 1.
        sph.parameters.beta = 2.
        sph.commit_parameters()
        sph.gas_particles.add_particles(p)
        sph.commit_particles()

#        sph.start_viewer()

        t=0. | nbody_system.time
        rms=1.
        minrms=1.
        i=0
        while rms > target_rms:
            i+=1
            t=t+(0.25 | nbody_system.time)
            sph.evolve_model(t)
            rho=sph.particles.rho.value_in(nbody_system.density)
            rms=rho.std()/rho.mean()
            minrms=min(minrms,rms)
            if rms>2.*minrms or i>300:
                print " RMS(rho) convergence warning:", i, rms,minrms
            if i>100000:
                print "i> 100k steps - not sure about this..."
                print " rms:", rms
                break


        x=sph.particles.x.value_in(nbody_system.length)
        y=sph.particles.y.value_in(nbody_system.length)
        z=sph.particles.z.value_in(nbody_system.length)

        del sph  
        return x,y,z