Beispiel #1
0
    def test4(self):
        print "Testing Halogen generate_particles"
        number_of_particles = 100
        instance = Halogen(**default_options)
        instance.initialize_code()
        instance.parameters.alpha = 2.0
        instance.parameters.beta = 5.0
        instance.parameters.gamma = 0.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        instance.commit_parameters()
        instance.generate_particles()
        self.assertEquals(len(instance.particles), number_of_particles)
        self.assertAlmostEquals(instance.particles.total_mass(),
                                1.0 | nbody_system.mass)
        self.assertAlmostEquals(instance.particles.kinetic_energy(),
                                0.17345836639 | nbody_system.energy)
        self.assertAlmostEquals(
            instance.particles.potential_energy(G=nbody_system.G),
            -0.310395778644 | nbody_system.energy)
        self.assertAlmostEquals(instance.particles.virial_radius(),
                                1.61084664935 | nbody_system.length)

        instance.cleanup_code()
        instance.stop()
Beispiel #2
0
 def test7(self):
     print "Testing Halogen error handling"
     number_of_particles = 1000
     instance = Halogen(**default_options)
     instance.initialize_code()
     self.assertRaises(exceptions.AmuseException, instance.commit_parameters, expected_message = 
         "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
         "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'")
     instance.parameters.alpha = 2.0
     instance.parameters.beta  = 5.0
     instance.parameters.gamma = 5.0
     instance.parameters.number_of_particles = number_of_particles
     instance.parameters.random_seed = 1
     self.assertRaises(exceptions.AmuseException, instance.commit_parameters, expected_message = 
         "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
         "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'")
     instance.parameters.gamma = -0.5
     self.assertRaises(exceptions.AmuseException, instance.commit_parameters, expected_message = 
         "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
         "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'")
     instance.parameters.gamma = 0.0
     instance.parameters.beta  = 2.0
     self.assertRaises(exceptions.AmuseException, instance.commit_parameters, expected_message = 
         "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
         "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'")
     instance.parameters.beta  = 5.0
     instance.commit_parameters()
     
     instance.cleanup_code()
     instance.stop()
Beispiel #3
0
    def test6(self):
        print "Testing Halogen state"
        number_of_particles = 1000

        print "First do everything manually:"
        instance = Halogen(**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.alpha = 2.0
        instance.parameters.beta = 5.0
        instance.parameters.gamma = 0.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        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 = Halogen(**default_options)
        self.assertEquals(instance.get_name_of_current_state(),
                          'UNINITIALIZED')
        instance.parameters.alpha = 2.0
        instance.parameters.beta = 5.0
        instance.parameters.gamma = 0.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        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.random_seed = 2
        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')
Beispiel #4
0
 def test3(self):
     print "Testing Halogen parameters (nbody units, no converter)"
     instance = Halogen(**default_options)
     instance.initialize_code()
     
     for par, value in [('total_mass', 1.0 | nbody_system.mass), 
             ('scale_radius',    1.0 | nbody_system.length), 
             ('cutoff_radius',  -1.0 | nbody_system.length), 
             ('black_hole_mass', 0.0 | nbody_system.mass)]:
         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()
Beispiel #5
0
    def test3(self):
        print "Testing Halogen parameters (nbody units, no converter)"
        instance = Halogen(**default_options)
        instance.initialize_code()

        for par, value in [('total_mass', 1.0 | nbody_system.mass),
                           ('scale_radius', 1.0 | nbody_system.length),
                           ('cutoff_radius', -1.0 | nbody_system.length),
                           ('black_hole_mass', 0.0 | nbody_system.mass)]:
            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 initialize_galaxy_model(N, converter):
    #default_options = dict(redirection = "none")
    instance = Halogen(converter)
    instance.initialize_code()
    instance.parameters.alpha = 2.0
    instance.parameters.beta  = 5.0
    instance.parameters.gamma = 0.0
    instance.parameters.number_of_particles = N
    instance.parameters.random_seed = 1
    instance.commit_parameters()
    instance.generate_particles()
    galaxy = instance.particles.copy()
        
    instance.cleanup_code()
    instance.stop()
    return galaxy
Beispiel #7
0
def initialize_galaxy_model(N, converter):
    #default_options = dict(redirection = "none")
    instance = Halogen(converter)
    instance.initialize_code()
    instance.parameters.alpha = 2.0
    instance.parameters.beta = 5.0
    instance.parameters.gamma = 0.0
    instance.parameters.number_of_particles = N
    instance.parameters.random_seed = 1
    instance.commit_parameters()
    instance.generate_particles()
    galaxy = instance.particles.copy()

    instance.cleanup_code()
    instance.stop()
    return galaxy
Beispiel #8
0
    def test6(self):
        print "Testing Halogen state"
        number_of_particles = 1000

        print "First do everything manually:"
        instance = Halogen(**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.alpha = 2.0
        instance.parameters.beta  = 5.0
        instance.parameters.gamma = 0.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        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 = Halogen(**default_options)
        self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.parameters.alpha = 2.0
        instance.parameters.beta  = 5.0
        instance.parameters.gamma = 0.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        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.random_seed = 2
        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')
Beispiel #9
0
    def test7(self):
        print "Testing Halogen error handling"
        number_of_particles = 1000
        instance = Halogen(**default_options)
        instance.initialize_code()
        self.assertRaises(
            exceptions.AmuseException,
            instance.commit_parameters,
            expected_message=
            "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
            "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'"
        )
        instance.parameters.alpha = 2.0
        instance.parameters.beta = 5.0
        instance.parameters.gamma = 5.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        self.assertRaises(
            exceptions.AmuseException,
            instance.commit_parameters,
            expected_message=
            "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
            "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'"
        )
        instance.parameters.gamma = -0.5
        self.assertRaises(
            exceptions.AmuseException,
            instance.commit_parameters,
            expected_message=
            "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
            "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'"
        )
        instance.parameters.gamma = 0.0
        instance.parameters.beta = 2.0
        self.assertRaises(
            exceptions.AmuseException,
            instance.commit_parameters,
            expected_message=
            "Error when calling 'commit_parameters' of a 'Halogen', errorcode is -2, error is "
            "'Missing or bad parameter for halo (see amuse/community/halogen/src/doc for details on required parameters).'"
        )
        instance.parameters.beta = 5.0
        instance.commit_parameters()

        instance.cleanup_code()
        instance.stop()
Beispiel #10
0
    def test5(self):
        print "Testing Halogen generate_particles: generate multiple sets"
        number_of_particles = 1000
        instance = Halogen(**default_options)
        instance.initialize_code()
        instance.parameters.alpha = 2.0
        instance.parameters.beta = 5.0
        instance.parameters.gamma = 0.0
        instance.parameters.number_of_particles = number_of_particles
        instance.parameters.random_seed = 1
        instance.commit_parameters()

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

        instance.parameters.random_seed = 1
        instance.generate_particles()
        set2 = instance.particles.copy()
        self.assertEquals(len(set2), number_of_particles)
        # Since a (any would do!) parameter was changed, recommit_parameters was
        # called, re-seeding, 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.generate_particles()
        # No parameter change: draw the next 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.assertIsOfOrder(abs(set1.x).median(), abs(set3.x).median(), 1)
        self.assertAlmostEquals(
            abs(set1.vy).median(),
            abs(set3.vy).median(), 1)

        instance.cleanup_code()
        instance.stop()
Beispiel #11
0
    def test2(self):
        print "Testing Halogen parameters (with unit converter)"
        instance = Halogen(self.default_unit_converter, **default_options)
        instance.initialize_code()

        for par, value in [('do_exact_virial_radius_flag', False),
                           ('outputgridr_flag', False),
                           ('outputgriddf_flag', False),
                           ('write_output_flag', False)]:
            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 [('alpha', -1.0), ('beta', -1.0), ('gamma', -1.0),
                           ('number_of_particles', -1), ('random_seed', 42)]:
            self.assertEquals(value, getattr(instance.parameters, par))
            setattr(instance.parameters, par, 1)
            self.assertEquals(1, getattr(instance.parameters, par))

        for par, value in [('total_mass', 1.0 | nbody_system.mass),
                           ('scale_radius', 1.0 | nbody_system.length),
                           ('cutoff_radius', -1.0 | nbody_system.length),
                           ('black_hole_mass', 0.0 | nbody_system.mass)]:
            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))

        for par, value in [('output_directory',
                            os.path.join(instance.get_output_directory(), "")),
                           ('output_basename', "halogen")]:
            self.assertEquals(value, getattr(instance.parameters, par))
            setattr(instance.parameters, par, 'test/')
            self.assertEquals("test/", getattr(instance.parameters, par))

        instance.cleanup_code()
        instance.stop()
Beispiel #12
0
 def test4(self):
     print "Testing Halogen generate_particles"
     number_of_particles = 100
     instance = Halogen(**default_options)
     instance.initialize_code()
     instance.parameters.alpha = 2.0
     instance.parameters.beta  = 5.0
     instance.parameters.gamma = 0.0
     instance.parameters.number_of_particles = number_of_particles
     instance.parameters.random_seed = 1
     instance.commit_parameters()
     instance.generate_particles()
     self.assertEquals(len(instance.particles), number_of_particles)
     self.assertAlmostEquals(instance.particles.total_mass(), 1.0 | nbody_system.mass)
     self.assertAlmostEquals(instance.particles.kinetic_energy(), 
         0.17345836639 | nbody_system.energy)
     self.assertAlmostEquals(instance.particles.potential_energy(G = nbody_system.G), 
         -0.310395778644 | nbody_system.energy)
     self.assertAlmostEquals(instance.particles.virial_radius(), 
         1.61084664935 | nbody_system.length)
     
     instance.cleanup_code()
     instance.stop()
Beispiel #13
0
 def test2(self):
     print "Testing Halogen parameters (with unit converter)"
     instance = Halogen(self.default_unit_converter, **default_options)
     instance.initialize_code()
     
     for par, value in [('do_exact_virial_radius_flag', False), 
             ('outputgridr_flag', False), ('outputgriddf_flag', False), 
             ('write_output_flag', False)]:
         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 [('alpha', -1.0), ('beta', -1.0), ('gamma', -1.0),
             ('number_of_particles', -1), ('random_seed', 42)]:
         self.assertEquals(value, getattr(instance.parameters, par))
         setattr(instance.parameters, par, 1)
         self.assertEquals(1, getattr(instance.parameters, par))
     
     for par, value in [('total_mass', 1.0 | nbody_system.mass), 
             ('scale_radius',    1.0 | nbody_system.length), 
             ('cutoff_radius',  -1.0 | nbody_system.length), 
             ('black_hole_mass', 0.0 | nbody_system.mass)]:
         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))
     
     for par, value in [('output_directory', os.path.join(instance.get_output_directory(), "")), 
             ('output_basename', "halogen")]:
         self.assertEquals(value, getattr(instance.parameters, par))
         setattr(instance.parameters, par, 'test/')
         self.assertEquals("test/", getattr(instance.parameters, par))
     
     instance.cleanup_code()
     instance.stop()
Beispiel #14
0
 def test5(self):
     print "Testing Halogen generate_particles: generate multiple sets"
     number_of_particles = 1000
     instance = Halogen(**default_options)
     instance.initialize_code()
     instance.parameters.alpha = 2.0
     instance.parameters.beta  = 5.0
     instance.parameters.gamma = 0.0
     instance.parameters.number_of_particles = number_of_particles
     instance.parameters.random_seed = 1
     instance.commit_parameters()
     
     instance.generate_particles()
     set1 = instance.particles.copy()
     self.assertEquals(len(set1), number_of_particles)
     
     instance.parameters.random_seed = 1
     instance.generate_particles()
     set2 = instance.particles.copy()
     self.assertEquals(len(set2), number_of_particles)
     # Since a (any would do!) parameter was changed, recommit_parameters was
     # called, re-seeding, 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.generate_particles()
     # No parameter change: draw the next 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.assertIsOfOrder(abs(set1.x).median(), abs(set3.x).median(), 1)
     self.assertAlmostEquals(abs(set1.vy).median(), abs(set3.vy).median(), 1)
     
     instance.cleanup_code()
     instance.stop()
Beispiel #15
0
def initialize_nfw_model(N,
                         mass=1.77e12,
                         scale_radius=24.6,
                         cutoff_mod=15.,
                         outfile=None):
    '''mass in Msun
    radius in Kpc
    Halogen: M Zemp et al. 2008 
    default_options = dict(redirection = "none")
    NFW = (1,3,1)
    need cutoff for beta <=3, gamma must be <3'''

    mass = mass | units.MSun
    scale_radius = scale_radius | units.kpc
    converter = nbody_system.nbody_to_si(mass, scale_radius)

    instance = Halogen(converter)
    instance.initialize_code()
    instance.parameters.alpha = 1.0
    instance.parameters.beta = 3.0
    instance.parameters.gamma = 1.0
    instance.parameters.scale_radius = scale_radius
    instance.parameters.cutoff_radius = cutoff_mod * instance.parameters.scale_radius
    instance.parameters.number_of_particles = N
    instance.parameters.random_seed = 1
    instance.commit_parameters()
    instance.generate_particles()
    stars = instance.particles.copy()

    instance.cleanup_code()
    instance.stop_reusable_channels()
    instance.stop()
    if outfile is not None:
        amuse.io.write_set_to_file(stars, outfile, format='csv')

    return stars, converter
Beispiel #16
0
 def test1(self):
     print "Testing Halogen initialization"
     instance = Halogen(**default_options)
     instance.initialize_code()
     instance.cleanup_code()
     instance.stop()
Beispiel #17
0
 def test1(self):
     print "Testing Halogen initialization"
     instance = Halogen(**default_options)
     instance.initialize_code()
     instance.cleanup_code()
     instance.stop()