Пример #1
0
    def slowtest4(self):
        print "Testing GalactICs generate_particles"
        n_particles_halo = 100
        n_particles_bulge = 100
        n_particles_disk = 100
        number_of_particles = n_particles_disk + n_particles_bulge + n_particles_halo

        instance = GalactICs(**default_options)
        instance.initialize_code()
        instance.parameters.disk_number_of_particles = n_particles_disk
        instance.parameters.bulge_number_of_particles = n_particles_bulge
        instance.parameters.halo_number_of_particles = n_particles_halo
        instance.commit_parameters()
        instance.generate_particles()
        self.assertEquals(len(instance.particles), number_of_particles)
        self.assertAlmostRelativeEquals(instance.particles.total_mass(),
                                        1225.4466176 | nbody_system.mass, 3)
        self.assertAlmostRelativeEquals(instance.particles.kinetic_energy(),
                                        2564.69894361 | nbody_system.energy, 3)
        self.assertAlmostRelativeEquals(
            instance.particles.potential_energy(G=nbody_system.G),
            -4531.58416742 | nbody_system.energy, 3)
        self.assertAlmostRelativeEquals(instance.particles.virial_radius(),
                                        165.694750127 | nbody_system.length, 3)

        instance.cleanup_code()
        instance.stop()
Пример #2
0
    def test5(self):
        print "Testing GalactICs generate_particles"
        instance = GalactICs(**default_options)
        instance.initialize_code()
        instance.parameters.halo_number_of_particles = 1000
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        instance.commit_parameters()
        instance.generate_particles()
        self.assertEquals(len(instance.particles), 1000)
        accuracy = 3
        mass_halo = 1178.89297009 | nbody_system.mass
        expected_kinetic_energy = 2418.49730735 | nbody_system.energy
        self.assertAlmostRelativeEquals(instance.particles.total_mass(),
                                        mass_halo, accuracy)
        self.assertAlmostRelativeEquals(instance.particles.kinetic_energy(),
                                        expected_kinetic_energy, accuracy)

        self.assertEqual(len(instance.halo_particles), 1000)
        self.assertEqual(len(instance.disk_particles), 0)
        self.assertEqual(len(instance.bulge_particles), 0)

        instance.cleanup_code()
        instance.stop()
Пример #3
0
    def test7(self):
        print "Testing GalactICs state"
        number_of_particles = 1000

        print "First do everything manually:"
        instance = GalactICs(**default_options)
        self.assertEquals(instance.get_name_of_current_state(),
                          'UNINITIALIZED')
        instance.initialize_code()
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.parameters.halo_number_of_particles = number_of_particles
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        instance.commit_parameters()
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.overridden().generate_particles()
        self.assertEquals(instance.get_name_of_current_state(), 'UPDATE')
        instance.invoke_state_change_updated()
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        self.assertEquals(len(instance.particles), number_of_particles)
        instance.cleanup_code()
        self.assertEquals(instance.get_name_of_current_state(), 'END')
        instance.stop()

        print "initialize_code(), (re)commit_parameters(), update_particle_set(), " \
            "and cleanup_code() should be called automatically:"
        instance = GalactICs(**default_options)
        self.assertEquals(instance.get_name_of_current_state(),
                          'UNINITIALIZED')
        instance.parameters.halo_number_of_particles = number_of_particles
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        self.assertEquals(instance.get_number_of_particles_updated(), 0)
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.parameters.halo_random_seed = -42.0
        self.assertEquals(instance.get_name_of_current_state(),
                          'CHANGE_PARAMETERS_EDIT')
        self.assertEquals(instance.get_number_of_particles_updated(), 0)
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.generate_particles()
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        self.assertEquals(len(instance.particles), number_of_particles)
        self.assertEquals(instance.get_number_of_particles_updated(), 0)
        instance.stop()
        self.assertEquals(instance.get_name_of_current_state(), 'STOPPED')
Пример #4
0
    def test7(self):
        print "Testing GalactICs state"
        number_of_particles = 1000

        print "First do everything manually:"
        instance = GalactICs(**default_options)
        self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.initialize_code()
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.parameters.halo_number_of_particles = number_of_particles
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        instance.commit_parameters()
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.overridden().generate_particles()
        self.assertEquals(instance.get_name_of_current_state(), 'UPDATE')
        instance.invoke_state_change_updated()
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        self.assertEquals(len(instance.particles), number_of_particles)
        instance.cleanup_code()
        self.assertEquals(instance.get_name_of_current_state(), 'END')
        instance.stop()

        print "initialize_code(), (re)commit_parameters(), update_particle_set(), " \
            "and cleanup_code() should be called automatically:"
        instance = GalactICs(**default_options)
        self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.parameters.halo_number_of_particles = number_of_particles
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        self.assertEquals(instance.get_number_of_particles_updated(), 0)
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.parameters.halo_random_seed = -42.0
        self.assertEquals(instance.get_name_of_current_state(), 'CHANGE_PARAMETERS_EDIT')
        self.assertEquals(instance.get_number_of_particles_updated(), 0)
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.generate_particles()
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        self.assertEquals(len(instance.particles), number_of_particles)
        self.assertEquals(instance.get_number_of_particles_updated(), 0)
        instance.stop()
        self.assertEquals(instance.get_name_of_current_state(), 'STOPPED')
Пример #5
0
def _new_galactics_model(halo_number_of_particles, unit_system_converter=None, do_scale=False, **keyword_arguments):
    instance = GalactICs(unit_converter=unit_system_converter)
    instance.parameters.halo_number_of_particles = halo_number_of_particles
    for (key, value) in keyword_arguments.iteritems():
        setattr(instance.parameters, key, value)
    
    instance.generate_particles()
    result = instance.particles.copy()
    instance.stop()
    
    result.move_to_center()
    if do_scale:
        result.scale_to_standard(convert_nbody=unit_system_converter)
    
    if not unit_system_converter is None:
        result = ParticlesWithUnitsConverted(result, unit_system_converter.as_converter_from_si_to_generic())
        result = result.copy()
    return result
Пример #6
0
 def slowtest4(self):
     print "Testing GalactICs generate_particles"
     n_particles_halo = 100
     n_particles_bulge = 100
     n_particles_disk = 100
     number_of_particles = n_particles_disk + n_particles_bulge + n_particles_halo
     
     instance = GalactICs(**default_options)
     instance.initialize_code()
     instance.parameters.disk_number_of_particles = n_particles_disk
     instance.parameters.bulge_number_of_particles = n_particles_bulge
     instance.parameters.halo_number_of_particles = n_particles_halo
     instance.commit_parameters()
     instance.generate_particles()
     self.assertEquals(len(instance.particles), number_of_particles)
     self.assertAlmostRelativeEquals(instance.particles.total_mass(), 1225.4466176 | nbody_system.mass, 3)
     self.assertAlmostRelativeEquals(instance.particles.kinetic_energy(), 2564.69894361 | nbody_system.energy, 3)
     self.assertAlmostRelativeEquals(instance.particles.potential_energy(G = nbody_system.G), -4531.58416742 | nbody_system.energy, 3)
     self.assertAlmostRelativeEquals(instance.particles.virial_radius(), 165.694750127 | nbody_system.length, 3)
     
     instance.cleanup_code()
     instance.stop()
Пример #7
0
    def test5(self):
        print "Testing GalactICs generate_particles"
        instance = GalactICs(**default_options)
        instance.initialize_code()
        instance.parameters.halo_number_of_particles = 1000
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        instance.commit_parameters()
        instance.generate_particles()
        self.assertEquals(len(instance.particles), 1000)
        accuracy = 3
        mass_halo = 1178.89297009 | nbody_system.mass
        expected_kinetic_energy = 2418.49730735 | nbody_system.energy
        self.assertAlmostRelativeEquals(instance.particles.total_mass(), mass_halo, accuracy)
        self.assertAlmostRelativeEquals(instance.particles.kinetic_energy(), expected_kinetic_energy, accuracy)

        self.assertEqual(len(instance.halo_particles), 1000)
        self.assertEqual(len(instance.disk_particles), 0)
        self.assertEqual(len(instance.bulge_particles), 0)

        instance.cleanup_code()
        instance.stop()
Пример #8
0
    def test6(self):
        print "Testing GalactICs generate_particles: generate multiple sets"
        number_of_particles = 1000
        instance = GalactICs(**default_options)
        instance.initialize_code()
        instance.parameters.halo_number_of_particles = number_of_particles
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        instance.parameters.halo_random_seed = -1.0
        instance.commit_parameters()

        instance.generate_particles()
        set1 = instance.particles.copy()
        self.assertEquals(len(set1), number_of_particles)

        instance.generate_particles()
        set2 = instance.particles.copy()
        self.assertEquals(len(set2), number_of_particles)
        # GalactICs' random-number generator is re-seeded with 'halo_random_seed'
        # each time, and the result should be the same:
        for attribute in ["mass", "x", "y", "z", "vx", "vy", "vz"]:
            self.assertEquals(getattr(set1, attribute),
                              getattr(set2, attribute))

        instance.parameters.halo_random_seed = -42.0
        instance.generate_particles()
        # halo_random_seed changed: draw a different random set of particles
        set3 = instance.particles.copy()
        self.assertEquals(len(set3), number_of_particles)
        self.assertEquals(set1.mass, set3.mass)
        self.assertRaises(self.failureException, self.assertEquals, set1.x,
                          set3.x)
        self.assertAlmostRelativeEquals(
            abs(set1.x).median(),
            abs(set3.x).median(), 1)
        self.assertAlmostRelativeEquals(
            abs(set1.vy).median(),
            abs(set3.vy).median(), 1)

        instance.cleanup_code()
        instance.stop()
Пример #9
0
    def test6(self):
        print "Testing GalactICs generate_particles: generate multiple sets"
        number_of_particles = 1000
        instance = GalactICs(**default_options)
        instance.initialize_code()
        instance.parameters.halo_number_of_particles = number_of_particles
        instance.parameters.generate_bulge_flag = False
        instance.parameters.generate_disk_flag = False
        instance.parameters.order_of_multipole_expansion = 0
        instance.parameters.halo_random_seed = -1.0
        instance.commit_parameters()

        instance.generate_particles()
        set1 = instance.particles.copy()
        self.assertEquals(len(set1), number_of_particles)

        instance.generate_particles()
        set2 = instance.particles.copy()
        self.assertEquals(len(set2), number_of_particles)
        # GalactICs' random-number generator is re-seeded with 'halo_random_seed'
        # each time, and the result should be the same:
        for attribute in ["mass", "x", "y", "z", "vx", "vy", "vz"]:
            self.assertEquals(getattr(set1, attribute), getattr(set2, attribute))

        instance.parameters.halo_random_seed = -42.0
        instance.generate_particles()
        # halo_random_seed changed: draw a different random set of particles
        set3 = instance.particles.copy()
        self.assertEquals(len(set3), number_of_particles)
        self.assertEquals(set1.mass, set3.mass)
        self.assertRaises(self.failureException, self.assertEquals, set1.x, set3.x)
        self.assertAlmostRelativeEquals(abs(set1.x).median(), abs(set3.x).median(), 1)
        self.assertAlmostRelativeEquals(abs(set1.vy).median(), abs(set3.vy).median(), 1)

        instance.cleanup_code()
        instance.stop()