def test_1(self): for prec in self.precs: # 2 sets of 100 points in 10*10*10 box n = 100 boxsize = 10.0 d_max = 2.0 a = (np.random.random(n * 3).reshape(n, 3) * boxsize).astype(prec) b = (np.random.random(n * 3).reshape(n, 3) * boxsize).astype(prec) box = (np.ones(3) * boxsize).astype(prec) idx, dists = capped_distance_array(a, b, d_max, box) # Brute force approach, do all n * n comparisons ref = np.zeros(n * n, dtype=prec) cgmath.inter_distance_array_withpbc(a, b, box, ref) ref = ref.reshape(n, n) # Check all distances under 2.0 were caught ref_idx = np.where(ref < d_max) for x, y in zip(ref_idx[0], ref_idx[1]): # Order can be reversed assert (x, y in idx) or (y, x in idx) # Check all reported distances were accurate # ie, ref[idx[i]] == dists[i] for (i, j), d in zip(idx, dists): assert ref[i, j] == d
def test_1(self): a = np.arange(30).reshape(10, 3).astype(np.float32) b = np.arange(30).reshape(10, 3).astype(np.float32) res = np.zeros(100).astype(np.float32) box = np.ones(3).astype(np.float32) * 30 cgmath.inter_distance_array_withpbc(a, b, box, res) ref = np.ravel(util.slow_inter_distance_withpbc(a, b, box)) assert_array_almost_equal(ref, res)
def test_1(self): for prec in self.precs: a = np.arange(30).reshape(10, 3).astype(prec) b = np.arange(30).reshape(10, 3).astype(prec) res = np.zeros(100).astype(prec) box = np.ones(3).astype(prec) * 30 cgmath.inter_distance_array_withpbc(a, b, box, res) ref = np.ravel(util.slow_inter_distance_withpbc(a, b, box)) assert_array_almost_equal(ref, res)
def cellgrid_self_distance_array(cg1): """Calculate all pairwise distances within a certain CellGrid :Returns: indices, distances indices - (n, 2) distances (n) """ box = cg1.box Nreq = _calculate_self_distance_array_size(cg1) indices = np.empty((Nreq, 2), dtype=np.int) dist = np.empty(Nreq, dtype=np.float32) pos = 0 for cell in cg1: n = len(cell) if n > 1: # Do own cell as a self distance comparison cgmath.intra_distance_array_withpbc( cell.coordinates, box, dist[pos:] ) cgmath.intra_index_array( cell.indices, indices[pos:] ) pos += n * (n - 1) // 2 # Then all half neighbours as a full comparison for addr in cell.half_neighbours: other = cg1[addr] if not other: continue cgmath.inter_distance_array_withpbc( cell.coordinates, other.coordinates, box, dist[pos:] ) cgmath.inter_index_array( cell.indices, other.indices, indices[pos:] ) pos += n * len(other) return indices, dist
def cellgrid_distance_array(cg1, cg2): """Calculate all pairwise distances between pairs in cg1 and cg2 :Returns: indices, distances indices - (n, 2) array of indices The first index refers to coordinates within cg1 The second index from cg2 distances - (n) Array of distances """ if not cg1 == cg2: raise ValueError("CellGrids are not compatible") # calculate required size of array Nreq = _calculate_distance_array_size(cg1, cg2) dist = np.empty(Nreq, dtype=np.float32) indices = np.empty((Nreq, 2), dtype=np.int) box = cg1.box # Calculate distances cell by cell pos = 0 # For each cell in cg1 for cell in cg1: # Iterate over all neighbours in other cellgrid for addr in itertools.chain([cell.address], cell.all_neighbours): other = cg2[addr] cgmath.inter_distance_array_withpbc( cell.coordinates, other.coordinates, box, dist[pos:] ) cgmath.inter_index_array( cell.indices, other.indices, indices[pos:] ) pos += len(cell) * len(other) return indices, dist
def test_1(self): for prec in self.precs: n = 100 boxsize = 10.0 d_max = 2.0 a = (np.random.random(n * 3).reshape(n, 3) * boxsize).astype(prec) box = (np.ones(3) * boxsize).astype(prec) idx, dists = capped_self_distance_array(a, d_max, box) ref = np.zeros(n * n, dtype=prec) cgmath.inter_distance_array_withpbc(a, a, box, ref) ref = ref.reshape(n, n) # Mask out coordinates seeing themselves ref[np.diag_indices_from(ref)] = d_max + 1.0 ref_idx = np.where(ref < d_max) for x, y in zip(ref_idx[0], ref_idx[1]): assert (x, y in idx) or (y, x in idx) for (i, j), d in zip(idx, dists): assert ref[i, j] == d
def cellgrid_self_distance_array(cg1): """Calculate all pairwise distances within a certain CellGrid :Returns: indices, distances indices - (n, 2) distances (n) """ box = cg1.box Nreq = _calculate_self_distance_array_size(cg1) indices = np.empty((Nreq, 2), dtype=np.int) dist = np.empty(Nreq, dtype=cg1.datatype) pos = 0 for cell in cg1: n = len(cell) if n > 1: # Do own cell as a self distance comparison cgmath.intra_distance_array_withpbc(cell.coordinates, box, dist[pos:]) cgmath.intra_index_array(cell.indices, indices[pos:]) pos += n * (n - 1) // 2 # Then all half neighbours as a full comparison for addr in cell.half_neighbours: other = cg1[addr] if not other: continue cgmath.inter_distance_array_withpbc(cell.coordinates, other.coordinates, box, dist[pos:]) cgmath.inter_index_array(cell.indices, other.indices, indices[pos:]) pos += n * len(other) return indices, dist
def cellgrid_distance_array(cg1, cg2): """Calculate all pairwise distances between pairs in cg1 and cg2 :Returns: indices, distances indices - (n, 2) array of indices The first index refers to coordinates within cg1 The second index from cg2 distances - (n) Array of distances """ if not cg1 == cg2: raise ValueError("CellGrids are not compatible") # calculate required size of array Nreq = _calculate_distance_array_size(cg1, cg2) dist = np.empty(Nreq, dtype=cg1.datatype) indices = np.empty((Nreq, 2), dtype=np.int) box = cg1.box # Calculate distances cell by cell pos = 0 # For each cell in cg1 for cell in cg1: # Iterate over all neighbours in other cellgrid for addr in itertools.chain([cell.address], cell.all_neighbours): other = cg2[addr] cgmath.inter_distance_array_withpbc(cell.coordinates, other.coordinates, box, dist[pos:]) cgmath.inter_index_array(cell.indices, other.indices, indices[pos:]) pos += len(cell) * len(other) return indices, dist
def brute_force_method(a, b, box): res = np.zeros((a.shape[0] * b.shape[0]), dtype=PREC) return inter_distance_array_withpbc(a, b, box, res)