def test_save_load_octree(): np.random.seed(int(0x4d3d3d3)) pos = np.random.normal(0.5, scale=0.05, size=(NPART, 3)) * (DRE - DLE) + DLE octree = ParticleOctreeContainer((1, 1, 1), DLE, DRE) octree.n_ref = 32 for i in range(3): np.clip(pos[:, i], DLE[i], DRE[i], pos[:, i]) # Convert to integers pos = np.floor((pos - DLE) / dx).astype("uint64") morton = get_morton_indices(pos) morton.sort() octree.add(morton) octree.finalize() saved = octree.save_octree() loaded = OctreeContainer.load_octree(saved) always = AlwaysSelector(None) ir1 = octree.ires(always) ir2 = loaded.ires(always) yield assert_equal, ir1, ir2 fc1 = octree.fcoords(always) fc2 = loaded.fcoords(always) yield assert_equal, fc1, fc2 fw1 = octree.fwidth(always) fw2 = loaded.fwidth(always) yield assert_equal, fw1, fw2
def test_add_particles_random(): np.random.seed(int(0x4d3d3d3)) pos = np.random.normal(0.5, scale=0.05, size=(NPART, 3)) * (DRE - DLE) + DLE # Now convert to integers for i in range(3): np.clip(pos[:, i], DLE[i], DRE[i], pos[:, i]) # Convert to integers pos = np.floor((pos - DLE) / dx).astype("uint64") morton = get_morton_indices(pos) morton.sort() for ndom in [1, 2, 4, 8]: octree = ParticleOctreeContainer((1, 1, 1), DLE, DRE) octree.n_ref = 32 for dom, split in enumerate(np.array_split(morton, ndom)): octree.add(split) octree.finalize() # This visits every oct. tc = octree.recursively_count() total_count = np.zeros(len(tc), dtype="int32") for i in sorted(tc): total_count[i] = tc[i] yield assert_equal, octree.nocts, total_count.sum() # This visits every cell -- including those covered by octs. #for dom in range(ndom): # level_count += octree.count_levels(total_count.size-1, dom, mask) yield assert_equal, total_count, [1, 8, 64, 64, 256, 536, 1856, 1672]
def test_save_load_octree(): np.random.seed(int(0x4d3d3d3)) pos = np.random.normal(0.5, scale=0.05, size=(NPART,3)) * (DRE-DLE) + DLE octree = ParticleOctreeContainer((1, 1, 1), DLE, DRE) octree.n_ref = 32 for i in range(3): np.clip(pos[:,i], DLE[i], DRE[i], pos[:,i]) # Convert to integers pos = np.floor((pos - DLE)/dx).astype("uint64") morton = get_morton_indices(pos) morton.sort() octree.add(morton) octree.finalize() saved = octree.save_octree() loaded = OctreeContainer.load_octree(saved) always = AlwaysSelector(None) ir1 = octree.ires(always) ir2 = loaded.ires(always) yield assert_equal, ir1, ir2 fc1 = octree.fcoords(always) fc2 = loaded.fcoords(always) yield assert_equal, fc1, fc2 fw1 = octree.fwidth(always) fw2 = loaded.fwidth(always) yield assert_equal, fw1, fw2
def test_add_particles_random(): np.random.seed(int(0x4d3d3d3)) pos = np.random.normal(0.5, scale=0.05, size=(NPART,3)) * (DRE-DLE) + DLE # Now convert to integers for i in range(3): np.clip(pos[:,i], DLE[i], DRE[i], pos[:,i]) # Convert to integers pos = np.floor((pos - DLE)/dx).astype("uint64") morton = get_morton_indices(pos) morton.sort() for ndom in [1, 2, 4, 8]: octree = ParticleOctreeContainer((1, 1, 1), DLE, DRE) octree.n_ref = 32 for dom, split in enumerate(np.array_split(morton, ndom)): octree.add(split) octree.finalize() # This visits every oct. tc = octree.recursively_count() total_count = np.zeros(len(tc), dtype="int32") for i in sorted(tc): total_count[i] = tc[i] yield assert_equal, octree.nocts, total_count.sum() # This visits every cell -- including those covered by octs. #for dom in range(ndom): # level_count += octree.count_levels(total_count.size-1, dom, mask) yield assert_equal, total_count, [1, 8, 64, 64, 256, 536, 1856, 1672]
def test_get_morton_indices(): from yt.utilities.lib.geometry_utils import get_morton_indices, get_morton_indices_unravel INDEX_MAX_64 = np.uint64(2097151) li = np.arange(6, dtype=np.uint64).reshape((2, 3)) mi_ans = np.array([10, 229], dtype=np.uint64) mi_out = get_morton_indices(li) mi_out2 = get_morton_indices_unravel(li[:, 0], li[:, 1], li[:, 2]) assert_array_equal(mi_out, mi_ans) assert_array_equal(mi_out2, mi_ans) li[0, :] = INDEX_MAX_64 * np.ones(3, dtype=np.uint64) assert_raises(ValueError, get_morton_indices, li) assert_raises(ValueError, get_morton_indices_unravel, li[:, 0], li[:, 1], li[:, 2])
def test_morton_neighbor(): from yt.utilities.lib.geometry_utils import get_morton_indices, morton_neighbor order = 20 imax = np.uint64(1 << order) p = np.array( [ [imax / 2, imax / 2, imax / 2], [imax / 2, imax / 2, 0], [imax / 2, imax / 2, imax], ], dtype=np.uint64, ) p_ans = np.array( [ [imax / 2, imax / 2, imax / 2 + 1], [imax / 2, imax / 2, imax / 2 - 1], [imax / 2, imax / 2, imax - 1], [imax / 2, imax / 2, 1], [imax / 2, imax / 2 + 1, imax / 2 + 1], [imax / 2 - 1, imax / 2 - 1, imax / 2], [imax / 2 - 1, imax / 2, imax / 2 + 1], [imax / 2, imax / 2 - 1, imax - 1], [imax / 2, imax / 2 + 1, 1], ], dtype=np.uint64, ) mi_ans = get_morton_indices(p_ans) assert_equal(morton_neighbor(p[0, :], [2], [+1], imax), mi_ans[0]) assert_equal(morton_neighbor(p[0, :], [2], [-1], imax), mi_ans[1]) assert_equal(morton_neighbor(p[1, :], [2], [-1], imax, periodic=False), -1) assert_equal(morton_neighbor(p[2, :], [2], [+1], imax, periodic=False), -1) assert_equal(morton_neighbor(p[1, :], [2], [-1], imax, periodic=True), mi_ans[2]) assert_equal(morton_neighbor(p[2, :], [2], [+1], imax, periodic=True), mi_ans[3]) assert_equal(morton_neighbor(p[0, :], [1, 2], [+1, +1], imax), mi_ans[4]) assert_equal(morton_neighbor(p[0, :], [0, 1], [-1, -1], imax), mi_ans[5]) assert_equal(morton_neighbor(p[0, :], [0, 2], [-1, +1], imax), mi_ans[6]) assert_equal( morton_neighbor(p[1, :], [1, 2], [-1, -1], imax, periodic=False), -1) assert_equal( morton_neighbor(p[2, :], [1, 2], [+1, +1], imax, periodic=False), -1) assert_equal( morton_neighbor(p[1, :], [1, 2], [-1, -1], imax, periodic=True), mi_ans[7]) assert_equal( morton_neighbor(p[2, :], [1, 2], [+1, +1], imax, periodic=True), mi_ans[8])
def test_get_morton_neighbors(): from yt.utilities.lib.geometry_utils import get_morton_indices, get_morton_neighbors order = 20 imax = 1 << order p = np.array( [ [imax / 2, imax / 2, imax / 2], [imax / 2, imax / 2, 0], [imax / 2, imax / 2, imax], ], dtype=np.uint64, ) pn_non = [ np.array( [ # x +/- 1 [imax / 2 + 1, imax / 2, imax / 2], [imax / 2 + 1, imax / 2 + 1, imax / 2], [imax / 2 + 1, imax / 2 + 1, imax / 2 + 1], [imax / 2 + 1, imax / 2 + 1, imax / 2 - 1], [imax / 2 + 1, imax / 2 - 1, imax / 2], [imax / 2 + 1, imax / 2 - 1, imax / 2 + 1], [imax / 2 + 1, imax / 2 - 1, imax / 2 - 1], [imax / 2 + 1, imax / 2, imax / 2 + 1], [imax / 2 + 1, imax / 2, imax / 2 - 1], [imax / 2 - 1, imax / 2, imax / 2], [imax / 2 - 1, imax / 2 + 1, imax / 2], [imax / 2 - 1, imax / 2 + 1, imax / 2 + 1], [imax / 2 - 1, imax / 2 + 1, imax / 2 - 1], [imax / 2 - 1, imax / 2 - 1, imax / 2], [imax / 2 - 1, imax / 2 - 1, imax / 2 + 1], [imax / 2 - 1, imax / 2 - 1, imax / 2 - 1], [imax / 2 - 1, imax / 2, imax / 2 + 1], [imax / 2 - 1, imax / 2, imax / 2 - 1], # y +/- 1 [imax / 2, imax / 2 + 1, imax / 2], [imax / 2, imax / 2 + 1, imax / 2 + 1], [imax / 2, imax / 2 + 1, imax / 2 - 1], [imax / 2, imax / 2 - 1, imax / 2], [imax / 2, imax / 2 - 1, imax / 2 + 1], [imax / 2, imax / 2 - 1, imax / 2 - 1], # x +/- 1 [imax / 2, imax / 2, imax / 2 + 1], [imax / 2, imax / 2, imax / 2 - 1], ], dtype=np.uint64, ), np.array( [ # x +/- 1 [imax / 2 + 1, imax / 2, 0], [imax / 2 + 1, imax / 2 + 1, 0], [imax / 2 + 1, imax / 2 + 1, 1], [imax / 2 + 1, imax / 2 - 1, 0], [imax / 2 + 1, imax / 2 - 1, 1], [imax / 2 + 1, imax / 2, 1], [imax / 2 - 1, imax / 2, 0], [imax / 2 - 1, imax / 2 + 1, 0], [imax / 2 - 1, imax / 2 + 1, 1], [imax / 2 - 1, imax / 2 - 1, 0], [imax / 2 - 1, imax / 2 - 1, 1], [imax / 2 - 1, imax / 2, 1], # y +/- 1 [imax / 2, imax / 2 + 1, 0], [imax / 2, imax / 2 + 1, 1], [imax / 2, imax / 2 - 1, 0], [imax / 2, imax / 2 - 1, 1], # z +/- 1 [imax / 2, imax / 2, 0 + 1], ], dtype=np.uint64, ), np.array( [ # x +/- 1 [imax / 2 + 1, imax / 2, imax], [imax / 2 + 1, imax / 2 + 1, imax], [imax / 2 + 1, imax / 2 + 1, imax - 1], [imax / 2 + 1, imax / 2 - 1, imax], [imax / 2 + 1, imax / 2 - 1, imax - 1], [imax / 2 + 1, imax / 2, imax - 1], [imax / 2 - 1, imax / 2, imax], [imax / 2 - 1, imax / 2 + 1, imax], [imax / 2 - 1, imax / 2 + 1, imax - 1], [imax / 2 - 1, imax / 2 - 1, imax], [imax / 2 - 1, imax / 2 - 1, imax - 1], [imax / 2 - 1, imax / 2, imax - 1], # y +/- 1 [imax / 2, imax / 2 + 1, imax], [imax / 2, imax / 2 + 1, imax - 1], [imax / 2, imax / 2 - 1, imax], [imax / 2, imax / 2 - 1, imax - 1], # z +/- 1 [imax / 2, imax / 2, imax - 1], ], dtype=np.uint64, ), ] pn_per = [ np.array( [ # x +/- 1 [imax / 2 + 1, imax / 2, imax / 2], [imax / 2 + 1, imax / 2 + 1, imax / 2], [imax / 2 + 1, imax / 2 + 1, imax / 2 + 1], [imax / 2 + 1, imax / 2 + 1, imax / 2 - 1], [imax / 2 + 1, imax / 2 - 1, imax / 2], [imax / 2 + 1, imax / 2 - 1, imax / 2 + 1], [imax / 2 + 1, imax / 2 - 1, imax / 2 - 1], [imax / 2 + 1, imax / 2, imax / 2 + 1], [imax / 2 + 1, imax / 2, imax / 2 - 1], [imax / 2 - 1, imax / 2, imax / 2], [imax / 2 - 1, imax / 2 + 1, imax / 2], [imax / 2 - 1, imax / 2 + 1, imax / 2 + 1], [imax / 2 - 1, imax / 2 + 1, imax / 2 - 1], [imax / 2 - 1, imax / 2 - 1, imax / 2], [imax / 2 - 1, imax / 2 - 1, imax / 2 + 1], [imax / 2 - 1, imax / 2 - 1, imax / 2 - 1], [imax / 2 - 1, imax / 2, imax / 2 + 1], [imax / 2 - 1, imax / 2, imax / 2 - 1], # y +/- 1 [imax / 2, imax / 2 + 1, imax / 2], [imax / 2, imax / 2 + 1, imax / 2 + 1], [imax / 2, imax / 2 + 1, imax / 2 - 1], [imax / 2, imax / 2 - 1, imax / 2], [imax / 2, imax / 2 - 1, imax / 2 + 1], [imax / 2, imax / 2 - 1, imax / 2 - 1], # z +/- 1 [imax / 2, imax / 2, imax / 2 + 1], [imax / 2, imax / 2, imax / 2 - 1], ], dtype=np.uint64, ), np.array( [ # x +/- 1 [imax / 2 + 1, imax / 2, 0], [imax / 2 + 1, imax / 2 + 1, 0], [imax / 2 + 1, imax / 2 + 1, 1], [imax / 2 + 1, imax / 2 + 1, imax - 1], [imax / 2 + 1, imax / 2 - 1, 0], [imax / 2 + 1, imax / 2 - 1, 1], [imax / 2 + 1, imax / 2 - 1, imax - 1], [imax / 2 + 1, imax / 2, 1], [imax / 2 + 1, imax / 2, imax - 1], [imax / 2 - 1, imax / 2, 0], [imax / 2 - 1, imax / 2 + 1, 0], [imax / 2 - 1, imax / 2 + 1, 1], [imax / 2 - 1, imax / 2 + 1, imax - 1], [imax / 2 - 1, imax / 2 - 1, 0], [imax / 2 - 1, imax / 2 - 1, 1], [imax / 2 - 1, imax / 2 - 1, imax - 1], [imax / 2 - 1, imax / 2, 1], [imax / 2 - 1, imax / 2, imax - 1], # y +/- 1 [imax / 2, imax / 2 + 1, 0], [imax / 2, imax / 2 + 1, 1], [imax / 2, imax / 2 + 1, imax - 1], [imax / 2, imax / 2 - 1, 0], [imax / 2, imax / 2 - 1, 1], [imax / 2, imax / 2 - 1, imax - 1], # z +/- 1 [imax / 2, imax / 2, 0 + 1], [imax / 2, imax / 2, imax - 1], ], dtype=np.uint64, ), np.array( [ # x +/- 1 [imax / 2 + 1, imax / 2, imax], [imax / 2 + 1, imax / 2 + 1, imax], [imax / 2 + 1, imax / 2 + 1, 1], [imax / 2 + 1, imax / 2 + 1, imax - 1], [imax / 2 + 1, imax / 2 - 1, imax], [imax / 2 + 1, imax / 2 - 1, 1], [imax / 2 + 1, imax / 2 - 1, imax - 1], [imax / 2 + 1, imax / 2, 1], [imax / 2 + 1, imax / 2, imax - 1], [imax / 2 - 1, imax / 2, imax], [imax / 2 - 1, imax / 2 + 1, imax], [imax / 2 - 1, imax / 2 + 1, 1], [imax / 2 - 1, imax / 2 + 1, imax - 1], [imax / 2 - 1, imax / 2 - 1, imax], [imax / 2 - 1, imax / 2 - 1, 1], [imax / 2 - 1, imax / 2 - 1, imax - 1], [imax / 2 - 1, imax / 2, 1], [imax / 2 - 1, imax / 2, imax - 1], # y +/- 1 [imax / 2, imax / 2 + 1, imax], [imax / 2, imax / 2 + 1, 1], [imax / 2, imax / 2 + 1, imax - 1], [imax / 2, imax / 2 - 1, imax], [imax / 2, imax / 2 - 1, 1], [imax / 2, imax / 2 - 1, imax - 1], # z +/- 1 [imax / 2, imax / 2, 1], [imax / 2, imax / 2, imax - 1], ], dtype=np.uint64, ), ] mi = get_morton_indices(p) N = mi.shape[0] # Non-periodic for i in range(N): out = get_morton_neighbors(np.array([mi[i]], dtype=np.uint64), order=order, periodic=False) ans = get_morton_indices(np.vstack([p[i, :], pn_non[i]])) assert_array_equal(np.unique(out), np.unique(ans), err_msg=f"Non-periodic: {i}") # Periodic for i in range(N): out = get_morton_neighbors(np.array([mi[i]], dtype=np.uint64), order=order, periodic=True) ans = get_morton_indices(np.vstack([p[i, :], pn_per[i]])) assert_array_equal(np.unique(out), np.unique(ans), err_msg=f"Periodic: {i}")