def test_errors_raise(self): """Test errors raise.""" # grid init with self.assertRaises(ValueError): Grid(self._ref_points, np.ones(len(self._ref_weights) + 1)) with self.assertRaises(ValueError): Grid(self._ref_points.reshape(self.grid.size, 1, 1), self._ref_weights) with self.assertRaises(ValueError): Grid(self._ref_points, self._ref_weights.reshape(-1, 1)) # integral with self.assertRaises(ValueError): self.grid.integrate() with self.assertRaises(TypeError): self.grid.integrate(5) if self._ref_points.ndim == 2: with self.assertRaises(ValueError): self.grid.integrate(self._ref_points) # get_localgrid with self.assertRaises(ValueError): self.grid.get_localgrid(self._ref_points[0], -1) with self.assertRaises(ValueError): self.grid.get_localgrid(self._ref_points[0], -np.inf) with self.assertRaises(ValueError): self.grid.get_localgrid(self._ref_points[0], np.nan) if self._ref_points.ndim == 2: with self.assertRaises(ValueError): self.grid.get_localgrid( np.zeros(self._ref_points.shape[1] + 1), 5.0) else: with self.assertRaises(ValueError): self.grid.get_localgrid(np.zeros(2), 5.0)
def test_get_localgrid_small_radius(self): """Basic checks for the get_localgrid method. In this unit test, the cutoff sphere fits inside a primitive cell, such that each grid point from the parent periodic grid will at most appear once in the local grid. """ center = self.grid.points[3] radius = 0.19475 # Check that the sphere fits inside the primitive cell: assert (2 * radius < self.grid.spacings).all() # Build local grids. localgrids = [ self.grid.get_localgrid(center, radius), self.wrapped_grid.get_localgrid(center, radius), ] # When there are no lattice vectors, the local grid from the base class # should also be the same. if self._ref_realvecs is None: aperiodic_grid = Grid(self._ref_points, self._ref_weights) localgrids.append(aperiodic_grid.get_localgrid(center, radius)) # One should get the same local grid with or without wrapping, possibly # with a different ordering of the points. We can perform a relatively # simple check here because each point appears at most once. order0 = localgrids[0].indices.argsort() for localgrid in localgrids[1:]: assert_allclose(localgrids[0].center, localgrid.center) order = localgrid.indices.argsort() assert_allclose(localgrids[0].points[order0], localgrid.points[order]) assert_allclose(localgrids[0].weights[order0], localgrid.weights[order]) # Other sanity checks on the grids. for localgrid in localgrids: # Just make sure we are testing with an actual local grid with at least # some points. assert localgrid.size > 0 assert localgrid.size <= self.grid.size assert len(localgrid.indices) == len(set(localgrid.indices)) # Test that the localgrid contains sensible results. assert_allclose(localgrid.center, center) assert localgrid.points.ndim == self.grid.points.ndim assert localgrid.weights.ndim == self.grid.weights.ndim if self._ref_points.ndim == 2: assert (np.linalg.norm(localgrid.points - center, axis=1) <= radius).all() else: assert (abs(localgrid.points - center) <= radius).all()
def test_getitem(self): """Test Grid index and slicing.""" # test index grid_index = self.grid[10] ref_grid = Grid(np.array([0]), np.array([0.1])) assert_allclose(grid_index.points, ref_grid.points) assert_allclose(grid_index.weights, ref_grid.weights) assert isinstance(grid_index, Grid) # test slice ref_grid_slice = Grid(np.linspace(-1, 0, 11), np.ones(11) * 0.1) grid_slice = self.grid[:11] assert_allclose(grid_slice.points, ref_grid_slice.points) assert_allclose(grid_slice.weights, ref_grid_slice.weights) assert isinstance(grid_slice, Grid) a = np.array([1, 3, 5]) ref_smt_index = self.grid[a] assert_allclose(ref_smt_index.points, np.array([-0.9, -0.7, -0.5])) assert_allclose(ref_smt_index.weights, np.array([0.1, 0.1, 0.1]))
def test_error_raises(self): """Tests for error raises.""" with self.assertRaises(TypeError): AtomGrid.from_pruned(np.arange(3), 1.0, r_sectors=np.arange(2), degs=np.arange(3)) with self.assertRaises(ValueError): AtomGrid.from_pruned( OneDGrid(np.arange(3), np.arange(3)), radius=1.0, r_sectors=np.arange(2), degs=np.arange(0), ) with self.assertRaises(ValueError): AtomGrid.from_pruned( OneDGrid(np.arange(3), np.arange(3)), radius=1.0, r_sectors=np.arange(2), degs=np.arange(4), ) with self.assertRaises(ValueError): AtomGrid._generate_atomic_grid( OneDGrid(np.arange(3), np.arange(3)), np.arange(2)) with self.assertRaises(ValueError): AtomGrid.from_pruned( OneDGrid(np.arange(3), np.arange(3)), radius=1.0, r_sectors=np.array([0.3, 0.5, 0.7]), degs=np.array([3, 5, 7, 5]), center=np.array([0, 0, 0, 0]), ) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), size=110) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degs=17) with self.assertRaises(ValueError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degs=[17], rotate=-1) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degs=[17], rotate="asdfaf") # error of radial grid with self.assertRaises(TypeError): AtomGrid(Grid(np.arange(1, 5, 1), np.ones(4)), degs=[2, 3, 4, 5]) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(-2, 2, 1), np.ones(4)), degs=[2, 3, 4, 5]) with self.assertRaises(TypeError): rgrid = OneDGrid(np.arange(1, 3, 1), np.ones(2), domain=(-1, 5)) AtomGrid(rgrid, degs=[2]) with self.assertRaises(TypeError): rgrid = OneDGrid(np.arange(-1, 1, 1), np.ones(2)) AtomGrid(rgrid, degs=[2])
def test_getitem(self): """Test Grid index and slicing.""" # test index grid_index = self.grid[10] ref_grid = Grid(self._ref_points[10:11], self._ref_weights[10:11]) assert_allclose(grid_index.points, ref_grid.points) assert_allclose(grid_index.weights, ref_grid.weights) assert isinstance(grid_index, Grid) # test slice ref_grid_slice = Grid(self._ref_points[:11], self._ref_weights[:11]) grid_slice = self.grid[:11] assert_allclose(grid_slice.points, ref_grid_slice.points) assert_allclose(grid_slice.weights, ref_grid_slice.weights) assert isinstance(grid_slice, Grid) a = np.array([1, 3, 5]) ref_smt_index = self.grid[a] assert_allclose(ref_smt_index.points, self._ref_points[a]) assert_allclose(ref_smt_index.weights, self._ref_weights[a])
def test_get_localgrid_large_radius(self): """Basic checks for the get_localgrid method. In this unit test, the cutoff sphere fits inside a primitive cell, such that each grid point from the parent periodic grid will at most appear once in the local grid. """ center = self.grid.points[3] radius = 2.51235 # Check that the sphere flows over the primitive cell, when there are # some lattice vectors. if self._ref_realvecs is not None: assert (2 * radius > self.grid.spacings).any() # Build local grids. localgrids = [ self.grid.get_localgrid(center, radius), self.wrapped_grid.get_localgrid(center, radius), ] # When there are no lattice vectors, the local grid from the base class # should also be the same. if self._ref_realvecs is None: aperiodic_grid = Grid(self._ref_points, self._ref_weights) localgrids.append(aperiodic_grid.get_localgrid(center, radius)) # One should get the same local grid with or without wrapping, possibly # with a different ordering of the points. for localgrid in localgrids[1:]: assert_equal_localgrids(localgrids[0], localgrid) # Other sanity checks. for localgrid in localgrids: # With a large radius there will never be less points in the local grid. if self._ref_realvecs is None: assert localgrid.size == self.grid.size else: assert localgrid.size > self.grid.size # Test that the local grid contains sensible results. assert_allclose(localgrid.center, center) assert localgrid.points.ndim == self.grid.points.ndim assert localgrid.weights.ndim == self.grid.weights.ndim if self._ref_points.ndim == 2: assert ( np.linalg.norm(localgrid.points - center, axis=1) <= radius ).all() else: assert (abs(localgrid.points - center) <= radius).all()
def test_find_l_for_rad_list(self): """Test private method find_l_for_rad_list.""" radial_pts = np.arange(0.1, 1.1, 0.1) radial_wts = np.ones(10) * 0.1 radial_grid = Grid(radial_pts, radial_wts) atomic_rad = 1 scales = np.array([0.2, 0.4, 0.8]) degs = np.array([3, 5, 7, 3]) atomic_grid_degree = AtomicGrid._find_l_for_rad_list( radial_grid.points, atomic_rad, scales, degs) assert_equal(atomic_grid_degree, [3, 3, 5, 5, 7, 7, 7, 7, 3, 3])
def test_error_raises(self): """Tests for error raises.""" with self.assertRaises(TypeError): AtomicGrid(np.arange(3), 1.0, scales=np.arange(2), degs=np.arange(3)) with self.assertRaises(ValueError): AtomicGrid( Grid(np.arange(3), np.arange(3)), 1.0, scales=np.arange(2), degs=np.arange(0), ) with self.assertRaises(ValueError): AtomicGrid( Grid(np.arange(3), np.arange(3)), 1.0, scales=np.arange(2), degs=np.arange(4), ) with self.assertRaises(ValueError): AtomicGrid._generate_atomic_grid(Grid(np.arange(3), np.arange(3)), np.arange(2), np.array([0, 0, 0])) with self.assertRaises(TypeError): AtomicGrid( Grid(np.arange(3), np.arange(3)), 1.0, scales=np.array([0.3, 0.5, 0.7]), degs=np.array([3, 5, 7, 5]), center=(0, 0, 0), ) with self.assertRaises(ValueError): AtomicGrid( Grid(np.arange(3), np.arange(3)), 1.0, scales=np.array([0.3, 0.5, 0.7]), degs=np.array([3, 5, 7, 5]), center=np.array([0, 0, 0, 0]), )
def test_errors_raise(self): """Test errors raise.""" # grid init weights = np.ones(4) points = np.arange(5) with self.assertRaises(ValueError): Grid(points, weights) # integral with self.assertRaises(ValueError): self.grid.integrate() with self.assertRaises(TypeError): self.grid.integrate(5) with self.assertRaises(ValueError): self.grid.integrate(points)
def test_atomic_grid(center): """Test atomic grid center transilation.""" rad_pts = np.array([0.1, 0.5, 1]) rad_wts = np.array([0.3, 0.4, 0.3]) rad_grid = Grid(rad_pts, rad_wts) degs = np.array([3, 5, 7]) # origin center center = np.array([0, 0, 0]) # randome center ref_center = np.random.rand(3) pts, wts, ind = AtomicGrid._generate_atomic_grid( rad_grid, degs, center) ref_pts, ref_wts, ref_ind = AtomicGrid._generate_atomic_grid( rad_grid, degs, ref_center) # diff grid points diff by center and same weights assert_allclose(pts, ref_pts) assert_allclose(wts, ref_wts)
def test_total_atomic_grid(self): """Normal initialization test.""" radial_pts = np.arange(0.1, 1.1, 0.1) radial_wts = np.ones(10) * 0.1 radial_grid = Grid(radial_pts, radial_wts) atomic_rad = 0.5 scales = np.array([0.5, 1, 1.5]) degs = np.array([6, 14, 14, 6]) # generate a proper instance without failing. ag_ob = AtomicGrid(radial_grid, atomic_rad, scales=scales, degs=degs) assert isinstance(ag_ob, AtomicGrid) assert len(ag_ob.indices) == 11 assert ag_ob.l_max == 15 ag_ob = AtomicGrid(radial_grid, atomic_rad, scales=np.array([]), degs=np.array([6])) assert isinstance(ag_ob, AtomicGrid) assert len(ag_ob.indices) == 11
def test_generate_atomic_grid(self): """Test for generating atomic grid.""" # setup testing class rad_pts = np.array([0.1, 0.5, 1]) rad_wts = np.array([0.3, 0.4, 0.3]) rad_grid = Grid(rad_pts, rad_wts) degs = np.array([3, 5, 7]) center = np.array([0, 0, 0]) pts, wts, ind = AtomicGrid._generate_atomic_grid( rad_grid, degs, center) assert len(pts) == 46 assert_equal(ind, [0, 6, 20, 46]) # set tests for slicing grid from atomic grid for i in range(3): # set each layer of points ref_grid = generate_lebedev_grid(degree=degs[i]) # check for each point assert_allclose(pts[ind[i]:ind[i + 1]], ref_grid.points * rad_pts[i]) # check for each weight assert_allclose( wts[ind[i]:ind[i + 1]], ref_grid.weights * rad_wts[i] * rad_pts[i]**2, )
def setUp(self): """Test setup function.""" points = np.linspace(-1, 1, 21) weights = np.ones(21) * 0.1 self.grid = Grid(points, weights)
class TestGrid(TestCase): """Grid testcase class.""" def setUp(self): """Test setup function.""" points = np.linspace(-1, 1, 21) weights = np.ones(21) * 0.1 self.grid = Grid(points, weights) def test_init_grid(self): """Test Grid init.""" # tests property assert isinstance(self.grid, Grid) ref_pts = np.arange(-1, 1.1, 0.1) ref_wts = np.ones(21) * 0.1 assert_allclose(self.grid.points, ref_pts, atol=1e-7) assert_allclose(self.grid.weights, ref_wts) assert self.grid.size == 21 def test_integrate(self): """Test Grid integral.""" # integral test1 result2 = self.grid.integrate(np.ones(21)) assert_allclose(result2, 2.1) # integral test2 value1 = np.linspace(-1, 1, 21) value2 = value1**2 result3 = self.grid.integrate(value1, value2) assert_allclose(result3, 0, atol=1e-7) def test_getitem(self): """Test Grid index and slicing.""" # test index grid_index = self.grid[10] ref_grid = Grid(np.array([0]), np.array([0.1])) assert_allclose(grid_index.points, ref_grid.points) assert_allclose(grid_index.weights, ref_grid.weights) assert isinstance(grid_index, Grid) # test slice ref_grid_slice = Grid(np.linspace(-1, 0, 11), np.ones(11) * 0.1) grid_slice = self.grid[:11] assert_allclose(grid_slice.points, ref_grid_slice.points) assert_allclose(grid_slice.weights, ref_grid_slice.weights) assert isinstance(grid_slice, Grid) a = np.array([1, 3, 5]) ref_smt_index = self.grid[a] assert_allclose(ref_smt_index.points, np.array([-0.9, -0.7, -0.5])) assert_allclose(ref_smt_index.weights, np.array([0.1, 0.1, 0.1])) def test_errors_raise(self): """Test errors raise.""" # grid init weights = np.ones(4) points = np.arange(5) with self.assertRaises(ValueError): Grid(points, weights) # integral with self.assertRaises(ValueError): self.grid.integrate() with self.assertRaises(TypeError): self.grid.integrate(5) with self.assertRaises(ValueError): self.grid.integrate(points)
class TestGrid(TestCase): """Grid testcase class.""" def setUp(self): """Test setup function.""" self._ref_points = np.linspace(-1, 1, 21) self._ref_weights = np.ones(21) * 0.1 self.grid = Grid(self._ref_points, self._ref_weights) def test_init_grid(self): """Test Grid init.""" # tests property assert isinstance(self.grid, Grid) assert_allclose(self.grid.points, self._ref_points, atol=1e-7) assert_allclose(self.grid.weights, self._ref_weights) assert self.grid.size == self._ref_weights.size def test_integrate(self): """Test Grid integral.""" # integral test1 result2 = self.grid.integrate(np.ones(21)) assert_allclose(result2, 2.1) # integral test2 value1 = np.linspace(-1, 1, 21) value2 = value1**2 result3 = self.grid.integrate(value1, value2) assert_allclose(result3, 0, atol=1e-7) def test_getitem(self): """Test Grid index and slicing.""" # test index grid_index = self.grid[10] ref_grid = Grid(self._ref_points[10:11], self._ref_weights[10:11]) assert_allclose(grid_index.points, ref_grid.points) assert_allclose(grid_index.weights, ref_grid.weights) assert isinstance(grid_index, Grid) # test slice ref_grid_slice = Grid(self._ref_points[:11], self._ref_weights[:11]) grid_slice = self.grid[:11] assert_allclose(grid_slice.points, ref_grid_slice.points) assert_allclose(grid_slice.weights, ref_grid_slice.weights) assert isinstance(grid_slice, Grid) a = np.array([1, 3, 5]) ref_smt_index = self.grid[a] assert_allclose(ref_smt_index.points, self._ref_points[a]) assert_allclose(ref_smt_index.weights, self._ref_weights[a]) def test_get_localgrid(self): """Test the creation of the local grid with a normal radius.""" center = self.grid.points[3] radius = 0.2 localgrid = self.grid.get_localgrid(center, radius) # Just make sure we are testing with an actual local grid with less (but # not zero) points. assert localgrid.size > 0 assert localgrid.size < self.grid.size # Test that the local grid contains the correct results. assert localgrid.points.ndim == self.grid.points.ndim assert localgrid.weights.ndim == self.grid.weights.ndim assert_allclose(localgrid.points, self.grid.points[localgrid.indices]) assert_allclose(localgrid.weights, self.grid.weights[localgrid.indices]) if self._ref_points.ndim == 2: assert (np.linalg.norm(localgrid.points - center, axis=1) <= radius).all() else: assert (abs(localgrid.points - center) <= radius).all() def test_get_localgrid_radius_inf(self): """Test the creation of the local grid with an infinite radius.""" localgrid = self.grid.get_localgrid(self.grid.points[3], np.inf) # Just make sure we are testing with a real local grid assert localgrid.size == self.grid.size assert_allclose(localgrid.points, self.grid.points) assert_allclose(localgrid.weights, self.grid.weights) assert_allclose(localgrid.indices, np.arange(self.grid.size)) def test_errors_raise(self): """Test errors raise.""" # grid init with self.assertRaises(ValueError): Grid(self._ref_points, np.ones(len(self._ref_weights) + 1)) with self.assertRaises(ValueError): Grid(self._ref_points.reshape(self.grid.size, 1, 1), self._ref_weights) with self.assertRaises(ValueError): Grid(self._ref_points, self._ref_weights.reshape(-1, 1)) # integral with self.assertRaises(ValueError): self.grid.integrate() with self.assertRaises(TypeError): self.grid.integrate(5) if self._ref_points.ndim == 2: with self.assertRaises(ValueError): self.grid.integrate(self._ref_points) # get_localgrid with self.assertRaises(ValueError): self.grid.get_localgrid(self._ref_points[0], -1) with self.assertRaises(ValueError): self.grid.get_localgrid(self._ref_points[0], -np.inf) with self.assertRaises(ValueError): self.grid.get_localgrid(self._ref_points[0], np.nan) if self._ref_points.ndim == 2: with self.assertRaises(ValueError): self.grid.get_localgrid( np.zeros(self._ref_points.shape[1] + 1), 5.0) else: with self.assertRaises(ValueError): self.grid.get_localgrid(np.zeros(2), 5.0)
def test_error_raises(self): """Tests for error raises.""" with self.assertRaises(TypeError): AtomGrid.from_pruned(np.arange(3), 1.0, sectors_r=np.arange(2), sectors_degree=np.arange(3)) with self.assertRaises(ValueError): AtomGrid.from_pruned( OneDGrid(np.arange(3), np.arange(3)), radius=1.0, sectors_r=np.arange(2), sectors_degree=np.arange(0), ) with self.assertRaises(ValueError): AtomGrid.from_pruned( OneDGrid(np.arange(3), np.arange(3)), radius=1.0, sectors_r=np.arange(2), sectors_degree=np.arange(4), ) with self.assertRaises(ValueError): AtomGrid._generate_atomic_grid( OneDGrid(np.arange(3), np.arange(3)), np.arange(2)) with self.assertRaises(ValueError): AtomGrid.from_pruned( OneDGrid(np.arange(3), np.arange(3)), radius=1.0, sectors_r=np.array([0.3, 0.5, 0.7]), sectors_degree=np.array([3, 5, 7, 5]), center=np.array([0, 0, 0, 0]), ) # test preset with self.assertRaises(ValueError): AtomGrid.from_preset(atnum=1, preset="fine") with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), sizes=110) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degrees=17) with self.assertRaises(ValueError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degrees=[17], rotate=-1) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degrees=[17], rotate="asdfaf") # error of radial grid with self.assertRaises(TypeError): AtomGrid(Grid(np.arange(1, 5, 1), np.ones(4)), degrees=[2, 3, 4, 5]) with self.assertRaises(TypeError): AtomGrid(OneDGrid(np.arange(-2, 2, 1), np.ones(4)), degrees=[2, 3, 4, 5]) with self.assertRaises(TypeError): rgrid = OneDGrid(np.arange(1, 3, 1), np.ones(2), domain=(-1, 5)) AtomGrid(rgrid, degrees=[2]) with self.assertRaises(TypeError): rgrid = OneDGrid(np.arange(-1, 1, 1), np.ones(2)) AtomGrid(rgrid, degrees=[2]) with self.assertRaises(ValueError): AtomGrid._generate_real_sph_harm(-1, np.random.rand(10), np.random.rand(10)) with self.assertRaises(ValueError): oned = GaussLegendre(30) btf = BeckeTF(0.0001, 1.5) rad = btf.transform_1d_grid(oned) atgrid = AtomGrid.from_preset(rad, atnum=1, preset="fine") atgrid.fit_values(np.random.rand(100))
def setUp(self): """Test setup function.""" self._ref_points = np.linspace(-1, 1, 21) self._ref_weights = np.ones(21) * 0.1 self.grid = Grid(self._ref_points, self._ref_weights)