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()
def find_clusters( stars, convert_nbody=None, scale_mass=1 | units.MSun, scale_radius=1 | units.parsec, mean_density=None, ): if convert_nbody == None: convert_nbody = nbody_system.nbody_to_si(scale_mass, scale_radius) groupfinder = Hop(convert_nbody) groupfinder.particles.add_particles(stars) groupfinder.calculate_densities() if mean_density is None: mean_density = groupfinder.particles.density.mean() groupfinder.parameters.peak_density_threshold = 10 * mean_density groupfinder.parameters.saddle_density_threshold = 0.99 * mean_density groupfinder.parameters.outer_density_threshold = 0.01 * mean_density print("Mean density: %s" % mean_density) groupfinder.do_hop() result = [ x.get_intersecting_subset_in(stars) for x in groupfinder.groups() ] groupfinder.stop() return result
def test8(self): random.seed(1001) print "Test 8: SI vs nbody units." converter = nbody_system.nbody_to_si(1.0 | units.MSun, 1.0 | units.RSun) numpy.random.seed(1234) particles = new_plummer_model(1000, convert_nbody=converter) hop = Hop(unit_converter=converter) hop.particles.add_particles(particles) hop.calculate_densities() hop.parameters.outer_density_threshold = 0.1 | nbody_system.mass / nbody_system.length**3 hop.do_hop() groups = list(hop.groups()) self.assertEquals(len(hop.particles), 1000) self.assertEquals(len(groups), 1) self.assertEquals(len(groups[0]), 511) self.assertEquals(len(hop.no_group()), 489) hop.stop() numpy.random.seed(1234) particles = new_plummer_model(1000) hop = Hop() hop.particles.add_particles(particles) hop.calculate_densities() hop.parameters.outer_density_threshold = 0.1 | nbody_system.mass / nbody_system.length**3 hop.do_hop() groups = list(hop.groups()) self.assertEquals(len(hop.particles), 1000) self.assertEquals(len(groups), 1) self.assertEquals(len(groups[0]), 511) self.assertEquals(len(hop.no_group()), 489) hop.stop()
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()
def identify_subgroups( unit_converter, particles, saddle_density_threshold=None, outer_density_threshold=None, peak_density_threshold="auto", logger=None, ): "Identify groups of particles by particle densities" # print(peak_density_threshold) # exit() logger = logger or logging.getLogger(__name__) hop = Hop(unit_converter) hop.particles.add_particles(particles) logger.info("particles added to Hop") hop.calculate_densities() logger.info("densities calculated") try: mean_density = hop.particles.density.mean() except: print("error") # if peak_density_threshold == "auto": # peak_density_threshold = mean_density hop.parameters.peak_density_threshold = peak_density_threshold logger.info( "peak density threshold set to %s", peak_density_threshold, ) print(peak_density_threshold / mean_density) saddle_density_threshold = (0.9 * peak_density_threshold if saddle_density_threshold is None else saddle_density_threshold) hop.parameters.saddle_density_threshold = saddle_density_threshold logger.info( "saddle density threshold set to %s", saddle_density_threshold, ) outer_density_threshold = (0.01 * peak_density_threshold if outer_density_threshold is None else outer_density_threshold) hop.parameters.outer_density_threshold = outer_density_threshold logger.info( "outer density threshold set to %s", saddle_density_threshold, ) hop.do_hop() logger.info("doing hop") result = [x.get_intersecting_subset_in(particles) for x in hop.groups()] hop.stop() print("hop done") logger.info("stopping hop") return result
def find_clumps(particles, unit_converter): hop = Hop(unit_converter) hop.particles.add_particles(particles) hop.calculate_densities() hop.do_hop() result = [x.get_intersecting_subset_in(particles) for x in hop.groups()] hop.stop() return result
def test3(self): random.seed(1001) print "Third test: densest neighbors and groups." hop = Hop() hop.parameters.number_of_neighbors_for_local_density = 5 particles1 = new_plummer_model(10) particles2 = new_plummer_model(10) particles3 = new_plummer_model(10) particles2.position += (10, 0, 0) | nbody_system.length particles3.position += (0, 20, 0) | nbody_system.length hop.particles.add_particles(particles1) hop.particles.add_particles(particles2) hop.particles.add_particles(particles3) hop.calculate_densities() hop.do_hop() print hop.particles.group_id groups = list(hop.groups()) self.assertEquals(len(groups), 3) self.assertEquals(hop.get_number_of_particles_outside_groups(), 0) #densities = (0,0,0) | nbody_system.density for index, group in enumerate(groups): self.assertEquals(len(group), 10) self.assertEquals(group.id_of_group(), index) #self.assertEquals(group.get_density_of_group(), densities[index]) hop.stop()
def test3(self): random.seed(1001) print "Third test: densest neighbors and groups." hop = Hop() hop.parameters.number_of_neighbors_for_local_density = 5 particles1 = new_plummer_model(10) particles2 = new_plummer_model(10) particles3 = new_plummer_model(10) particles2.position += (10,0,0) | nbody_system.length particles3.position += (0,20,0) | nbody_system.length hop.particles.add_particles(particles1) hop.particles.add_particles(particles2) hop.particles.add_particles(particles3) hop.calculate_densities() hop.do_hop() print hop.particles.group_id groups = list(hop.groups()) self.assertEquals(len(groups), 3) self.assertEquals(hop.get_number_of_particles_outside_groups(), 0) #densities = (0,0,0) | nbody_system.density for index, group in enumerate(groups): self.assertEquals(len(group), 10) self.assertEquals(group.id_of_group(), index) #self.assertEquals(group.get_density_of_group(), densities[index]) hop.stop()
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()
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()