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 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_slice_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) for i, d in enumerate("xyz"): for coord in np.arange(0.0, 1.0, 0.1): data = ds.slice(i, coord) data.get_data() v = data[d].to_ndarray() assert_equal(data.shape[0], 64**2) assert_equal(data["ones"].shape[0], 64**2) assert_array_less(np.abs(v - coord), 1.0 / 128.0 + 1e-6)
def test_obtain_position_vector(): ds = fake_random_ds(64, nprocs=8, fields=_fields, negative=[False, True, True, True]) dd = ds.sphere((0.5, 0.5, 0.5), 0.2) coords = obtain_position_vector(dd) r = np.sqrt(np.sum(coords * coords, axis=0)) assert_array_less(r.max(), 0.2) assert_array_less(0.0, r.min())
def test_ellipsoid(): # We decompose in different ways cs = [ np.array([0.5, 0.5, 0.5]), np.array([0.1, 0.2, 0.3]), np.array([0.8, 0.8, 0.8]), ] np.random.seed(int(0x4D3D3D3)) for nprocs in [1, 2, 4, 8]: ds = fake_random_ds(64, nprocs=nprocs) DW = ds.domain_right_edge - ds.domain_left_edge min_dx = 2.0 / ds.domain_dimensions ABC = np.random.random((3, 12)) * 0.1 e0s = np.random.random((3, 12)) tilts = np.random.random(12) ABC[:, 0] = 0.1 for i in range(12): for c in cs: A, B, C = reversed(sorted(ABC[:, i])) A = max(A, min_dx[0]) B = max(B, min_dx[1]) C = max(C, min_dx[2]) e0 = e0s[:, i] tilt = tilts[i] ell = ds.ellipsoid(c, A, B, C, e0, tilt) assert_array_less(ell[("index", "radius")], A) p = np.array([ell["index", ax] for ax in "xyz"]) dot_evec = [np.zeros_like(ell[("index", "radius")]) for i in range(3)] vecs = [ell._e0, ell._e1, ell._e2] mags = [ell._A, ell._B, ell._C] my_c = np.array([c] * p.shape[1]).transpose() dot_evec = [de.to_ndarray() for de in dot_evec] mags = [m.to_ndarray() for m in mags] for ax_i in range(3): dist = _difference(p[ax_i, :], my_c[ax_i, :], DW[ax_i]) for ax_j in range(3): dot_evec[ax_j] += dist * vecs[ax_j][ax_i] dist = 0 for ax_i in range(3): dist += dot_evec[ax_i] ** 2.0 / mags[ax_i] ** 2.0 assert_array_less(dist, 1.0)