def test_get_bbox(): cell_size = 0.1 cell = nnps.Cell(IntPoint(0, 0, 0), cell_size=cell_size, narrays=1) centroid = Point() boxmin = Point() boxmax = Point() cell.get_centroid(centroid) cell.get_bounding_box(boxmin, boxmax) assert (abs(boxmin.x - (centroid.x - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.y - (centroid.y - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.z - (centroid.z - 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.x - (centroid.x + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.y - (centroid.y + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.z - (centroid.z + 1.5 * cell_size)) < 1e-10) cell_size = 0.5 cell = nnps.Cell(IntPoint(1, 2, 0), cell_size=cell_size, narrays=1) cell.get_centroid(centroid) cell.get_bounding_box(boxmin, boxmax) assert (abs(boxmin.x - (centroid.x - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.y - (centroid.y - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.z - (centroid.z - 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.x - (centroid.x + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.y - (centroid.y + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.z - (centroid.z + 1.5 * cell_size)) < 1e-10)
def test_flatten_unflatten(): # first consider the 2D case where we assume a 4 X 5 grid of cells dim = 2 ncells_per_dim = IntArray(3) ncells_per_dim[0] = 4 ncells_per_dim[1] = 5 ncells_per_dim[2] = 0 # valid un-flattened cell indices cids = [[i, j] for i in range(4) for j in range(5)] for _cid in cids: cid = IntPoint(_cid[0], _cid[1], 0) flattened = nnps.py_flatten(cid, ncells_per_dim, dim) unflattened = nnps.py_unflatten(flattened, ncells_per_dim, dim) # the unflattened index should match with cid assert (cid == unflattened) # 3D dim = 3 ncells_per_dim = IntArray(3) ncells_per_dim[0] = 4 ncells_per_dim[1] = 5 ncells_per_dim[2] = 2 # valid un-flattened indices cids = [[i, j, k] for i in range(4) for j in range(5) for k in range(2)] for _cid in cids: cid = IntPoint(_cid[0], _cid[1], _cid[2]) flattened = nnps.py_flatten(cid, ncells_per_dim, dim) unflattened = nnps.py_unflatten(flattened, ncells_per_dim, dim) # the unflattened index should match with cid assert (cid == unflattened)
def test_1D_get_valid_cell_index(): dim = 1 # simulate a dummy distribution such that 10 cells are along the # 'x' direction n_cells = 10 ncells_per_dim = IntArray(3) ncells_per_dim[0] = n_cells ncells_per_dim[1] = 0 ncells_per_dim[2] = 0 # target cell cx = 1 cy = cz = 0 # as long as cy and cz are 0, the function should return the valid # flattened cell index for the cell for i in [-1, 0, 1]: index = nnps.py_get_valid_cell_index(IntPoint(cx + i, cy, cz), ncells_per_dim, dim, n_cells) assert index != -1 # index should be -1 whenever cy and cz are > 1. This is # specifically the case that was failing earlier. for j in [-1, 1]: for k in [-1, 1]: index = nnps.py_get_valid_cell_index(IntPoint(cx, cy + j, cz + k), ncells_per_dim, dim, n_cells) assert index == -1
def test_get_centroid(): cell = nnps.Cell(IntPoint(0, 0, 0), cell_size=0.1, narrays=1) centroid = Point() cell.get_centroid(centroid) assert (abs(centroid.x - 0.05) < 1e-10) assert (abs(centroid.y - 0.05) < 1e-10) assert (abs(centroid.z - 0.05) < 1e-10) cell = nnps.Cell(IntPoint(1, 2, 3), cell_size=0.5, narrays=1) cell.get_centroid(centroid) assert (abs(centroid.x - 0.75) < 1e-10) assert (abs(centroid.y - 1.25) < 1e-10) assert (abs(centroid.z - 1.75) < 1e-10)
def setUp(self): """Default set-up used by all the tests Particles with the following coordinates (x, y, z) are placed in a box 0 : -1.5 , 0.25 , 0.5 1 : 0.33 , -0.25, 0.25 2 : 1.25 , -1.25, 1.25 3 : 0.05 , 1.25 , -0.5 4 : -0.5 , 0.5 , -1.25 5 : -0.75, 0.75 , -1.25 6 : -1.25, 0.5 , 0.5 7 : 0.5 , 1.5 , -0.5 8 : 0.5 , -0.5 , 0.5 9 : 0.5 , 1.75 , -0.75 The cell size is set to 1. Valid cell indices and the particles they contain are given below: (-2, 0, 0) : particle 0, 6 (0, -1, 0) : particle 1, 8 (1, -2, 1) : particle 2 (0, 1, -1) : particle 3, 7, 9 (-1, 0, -2): particle 4, 5 """ x = numpy.array([ -1.5, 0.33, 1.25, 0.05, -0.5, -0.75, -1.25, 0.5, 0.5, 0.5]) y = numpy.array([ 0.25, -0.25, -1.25, 1.25, 0.5, 0.75, 0.5, 1.5, -0.5, 1.75]) z = numpy.array([ 0.5, 0.25, 1.25, -0.5, -1.25, -1.25, 0.5, -0.5, 0.5, -0.75]) # using a degenrate (h=0) array will set cell size to 1 for NNPS h = numpy.zeros_like(x) pa = get_particle_array(x=x, y=y, z=z, h=h) self.dict_box_sort_nnps = nnps.DictBoxSortNNPS( dim=3, particles=[pa], radius_scale=1.0 ) self.box_sort_nnps = nnps.BoxSortNNPS( dim=3, particles=[pa], radius_scale=1.0 ) self.ll_nnps = nnps.LinkedListNNPS( dim=3, particles=[pa], radius_scale=1.0 ) self.sp_hash_nnps = nnps.SpatialHashNNPS( dim=3, particles=[pa], radius_scale=1.0 ) self.ext_sp_hash_nnps = nnps.ExtendedSpatialHashNNPS( dim=3, particles=[pa], radius_scale=1.0 ) self.strat_radius_nnps = nnps.StratifiedHashNNPS( dim=3, particles=[pa], radius_scale=1.0 ) # these are the expected cells self.expected_cells = { IntPoint(-2, 0, 0): [0, 6], IntPoint(0, -1, 0): [1, 8], IntPoint(1, -2, 1): [2], IntPoint(0, 1, -1): [3, 7, 9], IntPoint(-1, 0, -2): [4, 5] }