Ejemplo n.º 1
0
    def test03(self):
        positions = [[1.0, 2.0, 3.0]] | units.m
        rotated = rotation.rotated(positions, 0.0, 0.0, 0.0)

        print(rotated)

        self.assertAlmostRelativeEquals(rotated, [[1.0, 2.0, 3.0]] | units.m)
        rotated = rotation.rotated(positions, numpy.pi, 0.0, 0.0)

        print(rotated)

        self.assertAlmostRelativeEquals(rotated, [[1.0, -2.0, -3.0]] | units.m)
Ejemplo n.º 2
0
 def test07(self):
     particles = new_plummer_model(100)
     kinetic_energy0 = particles.kinetic_energy()
     potential_energy0 = particles.potential_energy(G=nbody_system.G)
     particles.position = rotation.rotated(particles.position,  numpy.pi/3, numpy.pi/2, 0.0)
     particles.velocity = rotation.rotated(particles.velocity,  numpy.pi/3, numpy.pi/2, 0.0)
 
 
     kinetic_energy1 = particles.kinetic_energy()
     potential_energy1 = particles.potential_energy(G=nbody_system.G)
     self.assertAlmostRelativeEquals(kinetic_energy1, kinetic_energy0)
     self.assertAlmostRelativeEquals(potential_energy1, potential_energy0)
Ejemplo n.º 3
0
 def test04(self):
     positions = [ [1.0, 2.0, 3.0 ] , [4.0, 5.0, 6.0] ] | units.m
     rotated = rotation.rotated(positions, 0.0, 0.0, 0.0)
 
     print rotated
 
     self.assertAlmostRelativeEquals(rotated, [ [1.0, 2.0, 3.0 ], [4.0, 5.0, 6.0] ] | units.m)
 
     rotated = rotation.rotated(positions, numpy.pi, 0.0, 0.0)
     print rotated
 
     self.assertAlmostRelativeEquals(rotated, [ [1.0, -2.0, -3.0 ], [4.0, -5.0, -6.0] ] | units.m)
Ejemplo n.º 4
0
 def test07(self):
     particles = new_plummer_model(100)
     kinetic_energy0 = particles.kinetic_energy()
     potential_energy0 = particles.potential_energy(G=nbody_system.G)
     particles.position = rotation.rotated(particles.position,  numpy.pi/3, numpy.pi/2, 0.0)
     particles.velocity = rotation.rotated(particles.velocity,  numpy.pi/3, numpy.pi/2, 0.0)
 
 
     kinetic_energy1 = particles.kinetic_energy()
     potential_energy1 = particles.potential_energy(G=nbody_system.G)
     self.assertAlmostRelativeEquals(kinetic_energy1, kinetic_energy0)
     self.assertAlmostRelativeEquals(potential_energy1, potential_energy0)
Ejemplo n.º 5
0
 def test05(self):
     positions = [ [1.0, 2.0, 3.0 ] , [4.0, 5.0, 6.0] ] | units.m
     rotated = rotation.rotated(positions,  numpy.pi/2, 0.0, 0.0)
 
     print rotated
 
     self.assertAlmostRelativeEquals(rotated, [ [1.0, -3.0, 2.0 ], [4.0, -6.0, 5.0] ] | units.m)
Ejemplo n.º 6
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