def test_ellipsoid_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) ellipsoids = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [1.0, 1.0, 1.0], [0.25, 0.75, 0.25]] # spherical ellipsoid tests ratios = 3 * [0.25] for center in ellipsoids: data = ds.ellipsoid(center, ratios[0], ratios[1], ratios[2], np.array([1.0, 0.0, 0.0]), 0.0) data.get_data() dd = ds.all_data() dd.set_field_parameter("center", ds.arr(center, "code_length")) n_outside = (dd[("index", "radius")] >= ratios[0]).sum() assert_equal(data[("index", "radius")].size + n_outside, dd[("index", "radius")].size) positions = np.array([data[("index", ax)] for ax in "xyz"]) centers = (np.tile(data.center, data.shape[0]).reshape(data.shape[0], 3).transpose()) dist = periodic_dist( positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity, ) # WARNING: this value has not been externally verified assert_array_less(dist, ratios[0]) # aligned ellipsoid tests ratios = [0.25, 0.1, 0.1] for center in ellipsoids: data = ds.ellipsoid(center, ratios[0], ratios[1], ratios[2], np.array([1.0, 0.0, 0.0]), 0.0) # hack to compute elliptic distance dist2 = np.zeros(data[("index", "ones")].shape[0]) for i, ax in enumerate(("index", k) for k in "xyz"): positions = np.zeros((3, data[("index", "ones")].shape[0])) positions[i, :] = data[ax] centers = np.zeros((3, data[("index", "ones")].shape[0])) centers[i, :] = center[i] dist2 += (periodic_dist( positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity, ) / ratios[i])**2 # WARNING: this value has not been externally verified assert_array_less(dist2, 1.0)
def test_periodicity(): # First test the simple case were we find the distance between two points a = [0.1, 0.1, 0.1] b = [0.9, 0.9, 0.9] period = 1. dist = periodic_dist(a, b, period) yield assert_almost_equal, dist, 0.34641016151377535 dist = periodic_dist(a, b, period, (True, False, False)) yield assert_almost_equal, dist, 1.1489125293076059 dist = periodic_dist(a, b, period, (False, True, False)) yield assert_almost_equal, dist, 1.1489125293076059 dist = periodic_dist(a, b, period, (False, False, True)) yield assert_almost_equal, dist, 1.1489125293076059 dist = periodic_dist(a, b, period, (True, True, False)) yield assert_almost_equal, dist, 0.84852813742385713 dist = periodic_dist(a, b, period, (True, False, True)) yield assert_almost_equal, dist, 0.84852813742385713 dist = periodic_dist(a, b, period, (False, True, True)) yield assert_almost_equal, dist, 0.84852813742385713 dist = euclidean_dist(a, b) yield assert_almost_equal, dist, 1.3856406460551021 # Now test the more complicated cases where we're calculaing radii based # on data objects ds = fake_random_ds(64) # First we test flattened data data = ds.all_data() positions = np.array([data[ax] for ax in 'xyz']) c = [0.1, 0.1, 0.1] n_tup = tuple([1 for i in range(positions.ndim - 1)]) center = np.tile(np.reshape(np.array(c), (positions.shape[0], ) + n_tup), (1, ) + positions.shape[1:]) dist = periodic_dist(positions, center, period, ds.periodicity) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 0.863319074398 dist = euclidean_dist(positions, center) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 1.54531407988 # Then grid-like data data = ds.index.grids[0] positions = np.array([data[ax] for ax in 'xyz']) c = [0.1, 0.1, 0.1] n_tup = tuple([1 for i in range(positions.ndim - 1)]) center = np.tile(np.reshape(np.array(c), (positions.shape[0], ) + n_tup), (1, ) + positions.shape[1:]) dist = periodic_dist(positions, center, period, ds.periodicity) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 0.863319074398 dist = euclidean_dist(positions, center) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 1.54531407988
def test_periodicity(): # First test the simple case were we find the distance between two points a = [0.1,0.1,0.1] b = [0.9,0.9,0.9] period = 1. dist = periodic_dist(a,b,period) yield assert_almost_equal, dist, 0.34641016151377535 dist = periodic_dist(a, b, period, (True, False, False)) yield assert_almost_equal, dist, 1.1489125293076059 dist = periodic_dist(a, b, period, (False, True, False)) yield assert_almost_equal, dist, 1.1489125293076059 dist = periodic_dist(a, b, period, (False, False, True)) yield assert_almost_equal, dist, 1.1489125293076059 dist = periodic_dist(a, b, period, (True, True, False)) yield assert_almost_equal, dist, 0.84852813742385713 dist = periodic_dist(a, b, period, (True, False, True)) yield assert_almost_equal, dist, 0.84852813742385713 dist = periodic_dist(a, b, period, (False, True, True)) yield assert_almost_equal, dist, 0.84852813742385713 dist = euclidean_dist(a,b) yield assert_almost_equal, dist, 1.3856406460551021 # Now test the more complicated cases where we're calculaing radii based # on data objects ds = fake_random_ds(64) # First we test flattened data data = ds.all_data() positions = np.array([data[ax] for ax in 'xyz']) c = [0.1, 0.1, 0.1] n_tup = tuple([1 for i in range(positions.ndim-1)]) center = np.tile(np.reshape(np.array(c), (positions.shape[0],)+n_tup),(1,)+positions.shape[1:]) dist = periodic_dist(positions, center, period, ds.periodicity) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 0.863319074398 dist = euclidean_dist(positions, center) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 1.54531407988 # Then grid-like data data = ds.index.grids[0] positions = np.array([data[ax] for ax in 'xyz']) c = [0.1, 0.1, 0.1] n_tup = tuple([1 for i in range(positions.ndim-1)]) center = np.tile(np.reshape(np.array(c), (positions.shape[0],)+n_tup),(1,)+positions.shape[1:]) dist = periodic_dist(positions, center, period, ds.periodicity) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 0.863319074398 dist = euclidean_dist(positions, center) yield assert_almost_equal, dist.min(), 0.00270632938683 yield assert_almost_equal, dist.max(), 1.54531407988
def test_sphere_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) # aligned tests spheres = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [1.0, 1.0, 1.0], [0.25, 0.75, 0.25]] for center in spheres: data = ds.sphere(center, 0.25) # WARNING: this value has not be externally verified dd = ds.all_data() dd.set_field_parameter("center", ds.arr(center, "code_length")) n_outside = (dd["radius"] >= 0.25).sum() assert_equal(data["radius"].size + n_outside, dd["radius"].size) positions = np.array([data[ax] for ax in "xyz"]) centers = (np.tile(data.center, data["x"].shape[0]).reshape(data["x"].shape[0], 3).transpose()) dist = periodic_dist( positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity, ) # WARNING: this value has not been externally verified assert_array_less(dist, 0.25)
def virial_info(self, bins=300): r"""Calculates the virial information for the halo. Generally, it is better to call virial_radius or virial_mass instead, which calls this function automatically. """ # Skip if we've already calculated for this number of bins. if self.bin_count == bins and self.overdensity is not None: return None self.bin_count = bins # Cosmology h = self.ds.hubble_constant Om_matter = self.ds.omega_matter z = self.ds.current_redshift period = self.ds.domain_right_edge - self.ds.domain_left_edge thissize = self.get_size() rho_crit = rho_crit_g_cm3_h2 * h**2.0 * Om_matter # g cm^-3 Msun2g = mass_sun_cgs rho_crit = rho_crit * ((1.0 + z) ** 3.0) # Get some pertinent information about the halo. self.mass_bins = self.ds.arr( np.zeros(self.bin_count + 1, dtype="float64"), "Msun" ) dist = np.empty(thissize, dtype="float64") cen = self.center_of_mass() mark = 0 # Find the distances to the particles. I don't like this much, but I # can't see a way to eliminate a loop like this, either here or in yt. for pos in zip( self["particle_position_x"], self["particle_position_y"], self["particle_position_z"], ): dist[mark] = periodic_dist(cen, pos, period) mark += 1 # Set up the radial bins. # Multiply min and max to prevent issues with digitize below. self.radial_bins = np.logspace( np.log10(min(dist) * 0.99 + TINY), np.log10(max(dist) * 1.01 + 2 * TINY), num=self.bin_count + 1, ) self.radial_bins = self.ds.arr(self.radial_bins, "code_length") # Find out which bin each particle goes into, and add the particle # mass to that bin. inds = np.digitize(dist, self.radial_bins) - 1 if self["particle_position_x"].size > 1: for index in np.unique(inds): self.mass_bins[index] += np.sum( self["particle_mass"][inds == index] ).in_units("Msun") # Now forward sum the masses in the bins. for i in range(self.bin_count): self.mass_bins[i + 1] += self.mass_bins[i] # Calculate the over densities in the bins. self.overdensity = ( self.mass_bins * Msun2g / (4.0 / 3.0 * np.pi * rho_crit * (self.radial_bins) ** 3.0) )
def test_ellipsoid_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) ellipsoids = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [1.0, 1.0, 1.0], [0.25, 0.75, 0.25]] # spherical ellipsoid tests ratios = 3 * [0.25] for center in ellipsoids: data = ds.ellipsoid(center, ratios[0], ratios[1], ratios[2], np.array([1.0, 0.0, 0.0]), 0.0) data.get_data() dd = ds.all_data() dd.set_field_parameter("center", ds.arr(center, "code_length")) n_outside = (dd["radius"] >= ratios[0]).sum() assert_equal(data["radius"].size + n_outside, dd["radius"].size) positions = np.array([data[ax] for ax in "xyz"]) centers = np.tile(data.center, data.shape[0]).reshape(data.shape[0], 3).transpose() dist = periodic_dist(positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity) # WARNING: this value has not been externally verified yield assert_array_less, dist, ratios[0] # aligned ellipsoid tests ratios = [0.25, 0.1, 0.1] for center in ellipsoids: data = ds.ellipsoid(center, ratios[0], ratios[1], ratios[2], np.array([1.0, 0.0, 0.0]), 0.0) # hack to compute elliptic distance dist2 = np.zeros(data["ones"].shape[0]) for i, ax in enumerate("xyz"): positions = np.zeros((3, data["ones"].shape[0])) positions[i, :] = data[ax] centers = np.zeros((3, data["ones"].shape[0])) centers[i, :] = center[i] dist2 += ( periodic_dist(positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity) / ratios[i] ) ** 2 # WARNING: this value has not been externally verified yield assert_array_less, dist2, 1.0
def test_sphere_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) # aligned tests spheres = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [1.0, 1.0, 1.0], [0.25, 0.75, 0.25]] for center in spheres: data = ds.sphere(center, 0.25) # WARNING: this value has not be externally verified dd = ds.all_data() dd.set_field_parameter("center", ds.arr(center, "code_length")) n_outside = (dd["radius"] >= 0.25).sum() assert_equal(data["radius"].size + n_outside, dd["radius"].size) positions = np.array([data[ax] for ax in "xyz"]) centers = np.tile(data.center, data["x"].shape[0]).reshape(data["x"].shape[0], 3).transpose() dist = periodic_dist(positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity) # WARNING: this value has not been externally verified yield assert_array_less, dist, 0.25