def test_replicas(self): """Test that replication works.""" for num_replicas in range(1, 10): box, points = freud.data.UnitCell.fcc().generate_system( num_replicas=num_replicas) assert box == freud.box.Box.cube(num_replicas) test_points = np.array([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0], [0.0, 0.0, 0.0]]) test_points = test_points[np.newaxis, np.newaxis, np.newaxis, ...] test_points = np.tile( test_points, [num_replicas, num_replicas, num_replicas, 1, 1]) test_points[..., 0] += np.arange(num_replicas)[:, np.newaxis, np.newaxis, np.newaxis] test_points[..., 1] += np.arange(num_replicas)[np.newaxis, :, np.newaxis, np.newaxis] test_points[..., 2] += np.arange(num_replicas)[np.newaxis, np.newaxis, :, np.newaxis] test_points = (test_points - (num_replicas * 0.5)).reshape(-1, 3) npt.assert_allclose( sort_rounded_xyz_array(points), sort_rounded_xyz_array(box.wrap(test_points)), )
def test_voronoi_tess_2d(self): # Test that the voronoi polytope works for a 2D system L = 10 # Box length box = freud.box.Box.square(L) vor = freud.locality.Voronoi() # Make a regular grid points = np.array([ [0, 0, 0], [0, 1, 0], [0, 2, 0], [1, 0, 0], [1, 1, 0], [1, 2, 0], [2, 0, 0], [2, 1, 0], [2, 2, 0], ]).astype(np.float64) vor.compute((box, points)) center_polytope = sort_rounded_xyz_array(vor.polytopes[4]) expected_polytope = sort_rounded_xyz_array([[1.5, 1.5, 0], [0.5, 1.5, 0], [0.5, 0.5, 0], [1.5, 0.5, 0]]) npt.assert_almost_equal(center_polytope, expected_polytope) # Verify the cell areas npt.assert_almost_equal(vor.volumes[4], 1) npt.assert_almost_equal(np.sum(vor.volumes), box.volume) # Verify the neighbor list weights npt.assert_almost_equal( vor.nlist.weights[vor.nlist.query_point_indices == 4], 1) # Verify the neighbor distances wrapped_distances = np.linalg.norm( box.wrap(points[vor.nlist.point_indices] - points[vor.nlist.query_point_indices]), axis=-1, ) npt.assert_allclose(wrapped_distances, vor.nlist.distances) # Double the points (still inside the box) and test again points *= 2 vor.compute((box, points)) center_polytope = sort_rounded_xyz_array(vor.polytopes[4]) expected_polytope = sort_rounded_xyz_array([[3, 3, 0], [1, 3, 0], [1, 1, 0], [3, 1, 0]]) npt.assert_almost_equal(center_polytope, expected_polytope) npt.assert_almost_equal(vor.volumes[4], 4) npt.assert_almost_equal(np.sum(vor.volumes), box.volume) npt.assert_almost_equal( vor.nlist.weights[vor.nlist.query_point_indices == 4], 2) # Verify the neighbor distances wrapped_distances = np.linalg.norm( box.wrap(points[vor.nlist.point_indices] - points[vor.nlist.query_point_indices]), axis=-1, ) npt.assert_allclose(wrapped_distances, vor.nlist.distances)
def test_voronoi_tess_3d(self): # Test that the voronoi polytope works for a 3D system L = 10 # Box length box = freud.box.Box.cube(L) vor = freud.locality.Voronoi() # Make a regular grid points = np.array([[0, 0, 0], [0, 1, 0], [0, 2, 0], [1, 0, 0], [1, 1, 0], [1, 2, 0], [2, 0, 0], [2, 1, 0], [2, 2, 0], [0, 0, 1], [0, 1, 1], [0, 2, 1], [1, 0, 1], [1, 1, 1], [1, 2, 1], [2, 0, 1], [2, 1, 1], [2, 2, 1], [0, 0, 2], [0, 1, 2], [0, 2, 2], [1, 0, 2], [1, 1, 2], [1, 2, 2], [2, 0, 2], [2, 1, 2], [2, 2, 2]]).astype(np.float64) vor.compute((box, points)) center_polytope = sort_rounded_xyz_array(vor.polytopes[13]) expected_polytope = sort_rounded_xyz_array([[1.5, 1.5, 1.5], [1.5, 0.5, 1.5], [1.5, 0.5, 0.5], [1.5, 1.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 1.5], [0.5, 1.5, 0.5], [0.5, 1.5, 1.5]]) npt.assert_almost_equal(center_polytope, expected_polytope) # Verify the cell volumes npt.assert_almost_equal(vor.volumes[13], 1) npt.assert_almost_equal(np.sum(vor.volumes), box.volume) # Verify the neighbor list weights npt.assert_almost_equal( vor.nlist.weights[vor.nlist.query_point_indices == 13], 1) # Double the points (still inside the box) and test again points *= 2 vor.compute((box, points)) center_polytope = sort_rounded_xyz_array(vor.polytopes[13]) expected_polytope = sort_rounded_xyz_array([[3, 3, 3], [3, 1, 3], [3, 1, 1], [3, 3, 1], [1, 1, 1], [1, 1, 3], [1, 3, 1], [1, 3, 3]]) npt.assert_almost_equal(center_polytope, expected_polytope) npt.assert_almost_equal(vor.volumes[13], 8) npt.assert_almost_equal(np.sum(vor.volumes), box.volume) npt.assert_almost_equal( vor.nlist.weights[vor.nlist.query_point_indices == 13], 4)