示例#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()
示例#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()
示例#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')
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
示例#5
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
示例#6
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')
示例#7
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()
示例#8
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()
示例#9
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()
示例#10
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
示例#11
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()