Esempio n. 1
0
    def test2(self):
        random.seed(1001)

        hop = Hop()
        hop.initialize_code()
        hop.parameters.number_of_neighbors_for_local_density = 7
        hop.commit_parameters()

        particles = new_plummer_model(1000)
        hop.particles.add_particles(particles)

        #distance_to_center = (particles.position - particles.center_of_mass()).lengths()

        #print distance_to_center
        ds = {0: 0.482308834791, 1: 0.4885137677192688, 2: 0.27442726492881775}
        for method in [0, 1, 2]:
            hop.set_density_method(method)
            hop.calculate_densities()

            d = hop.particles[0].density

            self.assertAlmostRelativeEquals(d,
                                            ds[method] | nbody_system.density,
                                            5)
        hop.stop()
Esempio n. 2
0
 def test6(self):
     print("Test with different masses")
     # Particles on a cubic grid with masses according to a gaussian density profile
     grid = numpy.mgrid[-1:1:21j, -1:1:21j, -1:1:21j] | units.m
     particles = Particles(9261, x=grid[0].flatten(), y=grid[1].flatten(), z=grid[2].flatten())
     peak_positions = [[0.2, -0.4, 0.3], [-0.6, 0.2, 0.7]] | units.m
     particles.mass = 2*numpy.exp(-(particles.position-peak_positions[0]).lengths_squared() / (0.1|units.m**2)) | units.kg
     particles.mass += numpy.exp(-(particles.position-peak_positions[1]).lengths_squared() / (0.1|units.m**2)) | units.kg
     self.assertAlmostEqual(particles.position[particles.mass.argmax()], peak_positions[0])
     self.assertAlmostEqual(particles[:4000].position[particles[:4000].mass.argmax()], peak_positions[1])
     
     hop = Hop(unit_converter=nbody_system.nbody_to_si(particles.mass.sum(), 1.0 | units.m))#, redirection="none")
     hop.parameters.density_method = 2
     hop.parameters.number_of_neighbors_for_local_density = 50
     hop.parameters.relative_saddle_density_threshold = True
     hop.commit_parameters()
     hop.particles.add_particles(particles)
     hop.calculate_densities()
     self.assertAlmostEqual(hop.particles.position[hop.particles.density.argmax()], peak_positions[0])
     self.assertAlmostEqual(hop.particles[:4000].position[hop.particles[:4000].density.argmax()], peak_positions[1])
     hop.do_hop()
     groups = list(hop.groups())
     self.assertEqual(len(groups), 2)
     for group, peak_position in zip(groups, peak_positions):
         self.assertAlmostEqual(group.center_of_mass(), peak_position, 1)
     hop.stop()
Esempio n. 3
0
 def test6(self):
     print "Test with different masses"
     # Particles on a cubic grid with masses according to a gaussian density profile
     grid = numpy.mgrid[-1:1:21j, -1:1:21j, -1:1:21j] | units.m
     particles = Particles(9261, x=grid[0], y=grid[1], z=grid[2])
     peak_positions = [[0.2, -0.4, 0.3], [-0.6, 0.2, 0.7]] | units.m
     particles.mass = 2*numpy.exp(-(particles.position-peak_positions[0]).lengths_squared() / (0.1|units.m**2)) | units.kg
     particles.mass += numpy.exp(-(particles.position-peak_positions[1]).lengths_squared() / (0.1|units.m**2)) | units.kg
     self.assertAlmostEquals(particles.position[particles.mass.argmax()], peak_positions[0])
     self.assertAlmostEquals(particles[:4000].position[particles[:4000].mass.argmax()], peak_positions[1])
     
     hop = Hop(unit_converter=nbody_system.nbody_to_si(particles.mass.sum(), 1.0 | units.m))#, redirection="none")
     hop.parameters.density_method = 2
     hop.parameters.number_of_neighbors_for_local_density = 50
     hop.parameters.relative_saddle_density_threshold = True
     hop.commit_parameters()
     hop.particles.add_particles(particles)
     hop.calculate_densities()
     self.assertAlmostEquals(hop.particles.position[hop.particles.density.argmax()], peak_positions[0])
     self.assertAlmostEquals(hop.particles[:4000].position[hop.particles[:4000].density.argmax()], peak_positions[1])
     hop.do_hop()
     groups = list(hop.groups())
     self.assertEquals(len(groups), 2)
     for group, peak_position in zip(groups, peak_positions):
         self.assertAlmostEquals(group.center_of_mass(), peak_position, 1)
     hop.stop()
Esempio n. 4
0
    def test7(self):
        print "Testing Hop states"
        unit_converter = nbody_system.nbody_to_si(1.0 | units.MSun,
                                                  1.0 | units.AU)
        particles = new_plummer_model(200, convert_nbody=unit_converter)

        print "First do everything manually:",
        instance = Hop(unit_converter=unit_converter)
        self.assertEquals(instance.get_name_of_current_state(),
                          'UNINITIALIZED')
        instance.initialize_code()
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.commit_parameters()
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.particles.add_particles(particles)
        instance.commit_particles()
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        instance.cleanup_code()
        self.assertEquals(instance.get_name_of_current_state(), 'END')
        instance.stop()
        print "ok"

        print "initialize_code(), commit_parameters(), (re)commit_particles(), " \
            "and cleanup_code() should be called automatically:",
        instance = Hop(unit_converter=unit_converter)
        self.assertEquals(instance.get_name_of_current_state(),
                          'UNINITIALIZED')
        instance.parameters.number_of_neighbors_for_local_density = 50
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.particles.add_particles(particles[:100])
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        mass = instance.particles[0].mass
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        instance.particles.add_particles(particles[100:])
        self.assertEquals(instance.get_name_of_current_state(), 'UPDATE')
        mass = instance.particles[100].mass
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        instance.stop()
        self.assertEquals(instance.get_name_of_current_state(), 'STOPPED')
        print "ok"
Esempio n. 5
0
    def test7(self):
        print "Testing Hop states"
        unit_converter = nbody_system.nbody_to_si(1.0 | units.MSun, 1.0 | units.AU)
        particles = new_plummer_model(200, convert_nbody=unit_converter)
        
        print "First do everything manually:",
        instance = Hop(unit_converter=unit_converter)
        self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.initialize_code()
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.commit_parameters()
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        instance.particles.add_particles(particles)
        instance.commit_particles()
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        instance.cleanup_code()
        self.assertEquals(instance.get_name_of_current_state(), 'END')
        instance.stop()
        print "ok"

        print "initialize_code(), commit_parameters(), (re)commit_particles(), " \
            "and cleanup_code() should be called automatically:",
        instance = Hop(unit_converter=unit_converter)
        self.assertEquals(instance.get_name_of_current_state(), 'UNINITIALIZED')
        instance.parameters.number_of_neighbors_for_local_density = 50
        self.assertEquals(instance.get_name_of_current_state(), 'INITIALIZED')
        instance.particles.add_particles(particles[:100])
        self.assertEquals(instance.get_name_of_current_state(), 'EDIT')
        mass = instance.particles[0].mass
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        instance.particles.add_particles(particles[100:])
        self.assertEquals(instance.get_name_of_current_state(), 'UPDATE')
        mass = instance.particles[100].mass
        self.assertEquals(instance.get_name_of_current_state(), 'RUN')
        instance.stop()
        self.assertEquals(instance.get_name_of_current_state(), 'STOPPED')
        print "ok"
Esempio n. 6
0
 def test2(self):
     random.seed(1001)
     
     hop = Hop()
     hop.initialize_code()
     hop.parameters.number_of_neighbors_for_local_density = 7
     hop.commit_parameters()
     
     particles = new_plummer_model(1000)
     hop.particles.add_particles(particles)
     
     
     #distance_to_center = (particles.position - particles.center_of_mass()).lengths()
     
     #print distance_to_center
     ds = {0: 0.482308834791, 1:0.4885137677192688, 2:0.27442726492881775}
     for method in [0,1,2]:
         hop.set_density_method(method)
         hop.calculate_densities()
         
         d = hop.particles[0].density
         
         self.assertAlmostRelativeEquals(d, ds[method] | nbody_system.density, 5)
     hop.stop()
Esempio n. 7
0
    def test4(self):
        random.seed(1001)
        print "Test 4: complicated density field."

        # A separate group below peak_density_threshold -> should be dropped
        particles0 = new_plummer_model(90,
                                       convert_nbody=nbody_system.nbody_to_si(
                                           0.9 | units.MSun, 1.0 | units.RSun))

        # A nearby group below peak_density_threshold -> should be attached to proper group
        particles1 = new_plummer_model(80,
                                       convert_nbody=nbody_system.nbody_to_si(
                                           0.8 | units.MSun, 1.0 | units.RSun))
        particles1.x += 10 | units.RSun

        # A proper group very nearby other proper group -> groups should merge
        particles2a = new_plummer_model(200,
                                        convert_nbody=nbody_system.nbody_to_si(
                                            2.0 | units.MSun,
                                            1.0 | units.RSun))
        particles2b = new_plummer_model(300,
                                        convert_nbody=nbody_system.nbody_to_si(
                                            3.0 | units.MSun,
                                            1.0 | units.RSun))
        particles2a.x += 11.0 | units.RSun
        particles2b.x += 11.2 | units.RSun

        # A separate proper group other proper group -> groups should be preserved
        particles3 = new_plummer_model(400,
                                       convert_nbody=nbody_system.nbody_to_si(
                                           4.0 | units.MSun, 1.0 | units.RSun))
        particles3.x += 20 | units.RSun

        hop = Hop(
            unit_converter=nbody_system.nbody_to_si(10.7 | units.MSun, 1.0
                                                    | units.RSun))
        hop.parameters.number_of_neighbors_for_local_density = 100
        hop.parameters.saddle_density_threshold_factor = 0.5
        hop.parameters.relative_saddle_density_threshold = True
        hop.commit_parameters()

        for set in [
                particles0, particles1, particles2a, particles2b, particles3
        ]:
            hop.particles.add_particles(set)

        hop.calculate_densities()
        hop.parameters.outer_density_threshold = 0.1 * hop.particles.density.mean(
        )
        hop.parameters.peak_density_threshold = hop.particles.density.amax(
        ) / 4.0
        hop.recommit_parameters()
        hop.do_hop()
        groups = list(hop.groups())

        self.assertEquals(len(hop.particles), 1070)
        self.assertEquals(len(groups), 2)
        self.assertEquals(
            hop.particles.select(lambda x: x < 5 | units.RSun, "x").group_id,
            -1)
        self.assertEquals(hop.get_number_of_particles_outside_groups(), 299)
        self.assertEquals(1070 - len(groups[0]) - len(groups[1]), 299)

        expected_size = [
            477, 294
        ]  # Less than [580, 400], because particles below outer_density_threshold are excluded
        expected_average_x = [11.0, 20] | units.RSun
        for index, group in enumerate(groups):
            self.assertEquals(group.id_of_group(), index)
            self.assertAlmostEquals(group.center_of_mass()[0],
                                    expected_average_x[index], 1)
            self.assertEquals(len(group), expected_size[index])

        if False:  # Make a plot
            original = hop.particles.copy()
            from amuse.plot import scatter, native_plot
            colors = ["r", "g", "b", "y", "k", "w"] * 100
            for group, color in zip(hop.groups(), colors):
                scatter(group.x, group.y, c=color)
                original -= group
            scatter(original.x, original.y, c="m", marker="s")
            native_plot.show()

        hop.stop()
Esempio n. 8
0
 def test4(self):
     random.seed(1001)
     print "Test 4: complicated density field."
     
     # A separate group below peak_density_threshold -> should be dropped
     particles0 = new_plummer_model(90, convert_nbody=nbody_system.nbody_to_si(0.9 | units.MSun, 1.0 | units.RSun))
     
     # A nearby group below peak_density_threshold -> should be attached to proper group
     particles1 = new_plummer_model(80, convert_nbody=nbody_system.nbody_to_si(0.8 | units.MSun, 1.0 | units.RSun))
     particles1.x += 10 | units.RSun
     
     # A proper group very nearby other proper group -> groups should merge
     particles2a = new_plummer_model(200, convert_nbody=nbody_system.nbody_to_si(2.0 | units.MSun, 1.0 | units.RSun))
     particles2b = new_plummer_model(300, convert_nbody=nbody_system.nbody_to_si(3.0 | units.MSun, 1.0 | units.RSun))
     particles2a.x += 11.0 | units.RSun
     particles2b.x += 11.2 | units.RSun
     
     # A separate proper group other proper group -> groups should be preserved
     particles3 = new_plummer_model(400, convert_nbody=nbody_system.nbody_to_si(4.0 | units.MSun, 1.0 | units.RSun))
     particles3.x += 20 | units.RSun
     
     hop = Hop(unit_converter=nbody_system.nbody_to_si(10.7 | units.MSun, 1.0 | units.RSun))
     hop.parameters.number_of_neighbors_for_local_density = 100
     hop.parameters.saddle_density_threshold_factor = 0.5
     hop.parameters.relative_saddle_density_threshold = True
     hop.commit_parameters()
     
     for set in [particles0, particles1, particles2a, particles2b, particles3]:
         hop.particles.add_particles(set)
     
     hop.calculate_densities()
     hop.parameters.outer_density_threshold = 0.1 * hop.particles.density.mean()
     hop.parameters.peak_density_threshold = hop.particles.density.amax() / 4.0
     hop.recommit_parameters()
     hop.do_hop()
     groups = list(hop.groups())
     
     self.assertEquals(len(hop.particles), 1070)
     self.assertEquals(len(groups), 2)
     self.assertEquals(hop.particles.select(lambda x: x < 5|units.RSun, "x").group_id, -1)
     self.assertEquals(hop.get_number_of_particles_outside_groups(), 299)
     self.assertEquals(1070 - len(groups[0]) - len(groups[1]), 299)
     
     expected_size = [477, 294] # Less than [580, 400], because particles below outer_density_threshold are excluded
     expected_average_x = [11.0, 20] | units.RSun
     for index, group in enumerate(groups):
         self.assertEquals(group.id_of_group(), index)
         self.assertAlmostEquals(group.center_of_mass()[0], expected_average_x[index], 1)
         self.assertEquals(len(group), expected_size[index])
     
     if False: # Make a plot
         original = hop.particles.copy()
         from amuse.plot import scatter, native_plot
         colors = ["r", "g", "b", "y", "k", "w"]*100
         for group, color in zip(hop.groups(), colors):
             scatter(group.x, group.y, c=color)
             original -= group
         scatter(original.x, original.y, c="m", marker="s")
         native_plot.show()
     
     hop.stop()