def test1(self): print "Testing GalactICs initialization" instance = GalactICs(**default_options) instance.initialize_code() instance.parameters.generate_bulge_flag = False instance.parameters.generate_disk_flag = False instance.parameters.order_of_multipole_expansion = 0 instance.commit_parameters() instance.cleanup_code() instance.stop()
def test2(self): print "Testing GalactICs parameters (with unit converter)" instance = GalactICs(self.default_unit_converter, **default_options) instance.initialize_code() for par, value in [('generate_halo_flag', True), ('generate_disk_flag', True), ('generate_bulge_flag', True), ('halo_do_center_flag', True), ('bulge_do_center_flag', True), ('disk_do_center_flag', True)]: self.assertTrue(value is getattr(instance.parameters, par)) setattr(instance.parameters, par, not value) self.assertFalse(value is getattr(instance.parameters, par)) for par, value in [('number_of_grid_intervals', 90000), ('order_of_multipole_expansion', 10), ('number_of_radial_steps_correction_fns_disk_df', 10), ('number_of_iterations_disk_df', 50), ('halo_number_of_particles', 200000), ('bulge_number_of_particles', 50000), ('disk_number_of_particles', 100000), ('halo_random_seed', -1), ('bulge_random_seed', -1), ('disk_random_seed', -1)]: self.assertEquals(value, getattr(instance.parameters, par)) setattr(instance.parameters, par, 1) self.assertEquals(1, getattr(instance.parameters, par)) for par, value in [ ('halo_outer_radius', 300.0 | nbody_system.length), ('halo_scale_velocity', 3.26331115 | nbody_system.speed), ('halo_scale_radius', 6.06699419 | nbody_system.length), ('halo_truncation_width', 100.0 | nbody_system.length) ]: self.assertEquals(instance.unit_converter.to_si(value), getattr(instance.parameters, par)) setattr(instance.parameters, par, 3.0 | value.unit) self.assertEquals(instance.unit_converter.to_si(3.0 | value.unit), getattr(instance.parameters, par)) self.assertEquals(os.path.join(instance.get_output_directory()), instance.parameters.output_directory) instance.parameters.output_directory = 'test' self.assertEquals("test", instance.parameters.output_directory) instance.cleanup_code() instance.stop()
def test3(self): print "Testing GalactICs parameters (nbody units, no converter)" instance = GalactICs(**default_options) instance.initialize_code() for par, value in [('halo_outer_radius', 300.0 | nbody_system.length), ('halo_scale_velocity', 3.26331115 | nbody_system.speed), ('halo_scale_radius', 6.06699419 | nbody_system.length), ('halo_truncation_width', 100.0 | nbody_system.length)]: self.assertEquals(value, getattr(instance.parameters, par)) setattr(instance.parameters, par, 3.0 | value.unit) self.assertEquals(3.0 | value.unit, getattr(instance.parameters, par)) instance.cleanup_code() instance.stop()
def test2(self): print "Testing GalactICs parameters (with unit converter)" instance = GalactICs(self.default_unit_converter, **default_options) instance.initialize_code() for par, value in [ ("generate_halo_flag", True), ("generate_disk_flag", True), ("generate_bulge_flag", True), ("halo_do_center_flag", True), ("bulge_do_center_flag", True), ("disk_do_center_flag", True), ]: self.assertTrue(value is getattr(instance.parameters, par)) setattr(instance.parameters, par, not value) self.assertFalse(value is getattr(instance.parameters, par)) for par, value in [ ("number_of_grid_intervals", 90000), ("order_of_multipole_expansion", 10), ("number_of_radial_steps_correction_fns_disk_df", 10), ("number_of_iterations_disk_df", 50), ("halo_number_of_particles", 200000), ("bulge_number_of_particles", 50000), ("disk_number_of_particles", 100000), ("halo_random_seed", -1), ("bulge_random_seed", -1), ("disk_random_seed", -1), ]: self.assertEquals(value, getattr(instance.parameters, par)) setattr(instance.parameters, par, 1) self.assertEquals(1, getattr(instance.parameters, par)) for par, value in [ ("halo_outer_radius", 300.0 | nbody_system.length), ("halo_scale_velocity", 3.26331115 | nbody_system.speed), ("halo_scale_radius", 6.06699419 | nbody_system.length), ("halo_truncation_width", 100.0 | nbody_system.length), ]: self.assertEquals(instance.unit_converter.to_si(value), getattr(instance.parameters, par)) setattr(instance.parameters, par, 3.0 | value.unit) self.assertEquals(instance.unit_converter.to_si(3.0 | value.unit), getattr(instance.parameters, par)) self.assertEquals(os.path.join(instance.get_output_directory()), instance.parameters.output_directory) instance.parameters.output_directory = "test" self.assertEquals("test", instance.parameters.output_directory) instance.cleanup_code() instance.stop()
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
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')
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()
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()
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()
def _create_docstring(): _tmp_instance = GalactICs() docstring = "\nGalactICs documentation:\n" + GalactICsInterface.__doc__ + \ "\n" + _tmp_instance.parameters.__doc__ + "\n" _tmp_instance.stop() return docstring
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")
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()
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()