def test_spherical_complete(self): """Test atomitc grid consistence for spherical integral.""" num_pts = len(LEBEDEV_DEGREES) pts = HortonLinear(num_pts) for _ in range(10): start = np.random.rand() * 1e-5 end = np.random.rand() * 10 + 10 tf = PowerRTransform(start, end) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid(rad_grid, degrees=list(LEBEDEV_DEGREES.keys())) values = np.random.rand(len(LEBEDEV_DEGREES)) pt_val = np.zeros(atgrid.size) for index, value in enumerate(values): pt_val[atgrid._indices[index]:atgrid._indices[index + 1]] = value rad_int_val = (value * rad_grid.weights[index] * 4 * np.pi * rad_grid.points[index]**2) atgrid_int_val = np.sum( pt_val[atgrid._indices[index]:atgrid._indices[index + 1]] * atgrid.weights[atgrid._indices[index]:atgrid. _indices[index + 1]]) assert_almost_equal(rad_int_val, atgrid_int_val) ref_int_at = atgrid.integrate(pt_val) ref_int_rad = rad_grid.integrate(4 * np.pi * rad_grid.points**2 * values) assert_almost_equal(ref_int_at, ref_int_rad)
def test_spline_with_sph_harms(self): """Test spline projection the same as spherical harmonics.""" rad = IdentityRTransform().transform_grid(HortonLinear(10)) rad._points += 1 atgrid = AtomicGrid.special_init(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms( r_sph, values, atgrid.weights, atgrid.indices, rad.points ) # generate ref # for shell in range(1, 11): for shell in range(1, 11): sh_grid = atgrid.get_shell_grid(shell - 1, r_sq=False) r = np.linalg.norm(sh_grid._points, axis=1) theta = np.arctan2(sh_grid._points[:, 1], sh_grid._points[:, 0]) phi = np.arccos(sh_grid._points[:, 2] / r) r_sph = generate_real_sph_harms(l_max, theta, phi) r_sph_proj = np.sum( r_sph * self.helper_func_power(sh_grid.points) * sh_grid.weights, axis=-1, ) assert_allclose(r_sph_proj, result(shell), atol=1e-10)
def setUp(self): """Set up radial grid for integral tests.""" pts = HortonLinear(100) tf = ExpRTransform(1e-3, 1e1) rad_pts = tf.transform(pts.points) rad_wts = tf.deriv(pts.points) * pts.weights self.rgrid = OneDGrid(rad_pts, rad_wts)
def test_integrate_gauss(): """Test radial grid integral.""" oned = HortonLinear(100) rtf = PowerRTransform(0.0005, 1e1) grid = rtf.transform_1d_grid(oned) assert isinstance(grid, OneDGrid) y = np.exp(-0.5 * grid.points**2) # time 4 \pi and r^2 to accommodate old horton test grid._weights = grid.weights * 4 * np.pi * grid.points**2 assert_almost_equal(grid.integrate(y), (2 * np.pi)**1.5)
def test_basics2(): """Test basic radial grid transform properties for bigger grid.""" oned = HortonLinear(100) rtf = ExpRTransform(1e-3, 1e1) grid = rtf.transform_1d_grid(oned) assert isinstance(grid, OneDGrid) assert grid.size == 100 # assert grid.shape == (100,) # assert grid.rtransform == rtf assert (grid.weights > 0).all() assert (grid.points == rtf.transform(oned.points)).all()
def test_basics1(): """Test basic radial grid transform properties.""" oned = HortonLinear(4) rtf = ExpRTransform(0.1, 1e1) grid = rtf.transform_1d_grid(oned) assert isinstance(grid, OneDGrid) assert grid.size == 4 # assert grid.shape == (4,) # assert grid.rtransform == rtf assert (grid.weights > 0).all() assert (grid.points == rtf.transform(oned.points)).all()
def test_integrate_hirshfeld_weights_single_1s(self): """Test molecular integral in H atom with Hirshfeld weights.""" pts = HortonLinear(100) tf = ExpRTransform(1e-5, 2e1) rgrid = tf.transform_1d_grid(pts) coordinates = np.array([0.0, 0.0, -0.5]) atg1 = AtomGrid.from_pruned( rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=coordinates, ) mg = MolGrid([atg1], HirshfeldWeights(), np.array([7])) dist0 = np.sqrt(((coordinates - mg.points)**2).sum(axis=1)) fn = np.exp(-2 * dist0) / np.pi occupation = mg.integrate(fn) assert_almost_equal(occupation, 1.0, decimal=6)
def test_domain(self): """Test domain errors.""" rad = HortonLinear(10) with self.assertRaises(ValueError): tf = BeckeTF(0.1, 1.2) tf.transform_1d_grid(rad) with self.assertRaises(ValueError): tf = HandyModTF(0.1, 10.0, 2) tf.transform_1d_grid(rad) with self.assertRaises(ValueError): tf = KnowlesTF(0.1, 1.2, 2) tf.transform_1d_grid(rad) with self.assertRaises(ValueError): tf = LinearTF(0.1, 10) tf.transform_1d_grid(rad) with self.assertRaises(ValueError): tf = MultiExpTF(0.1, 1.2) tf.transform_1d_grid(rad)
def test_cubicspline_and_deriv(self): """Test spline for derivation.""" rad = IdentityRTransform().transform_grid(HortonLinear(10)) rad._points += 1 atgrid = AtomicGrid.special_init(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms( r_sph, values, atgrid.weights, atgrid.indices, rad.points ) semi_sph_c = sph_coor[atgrid.indices[5] : atgrid.indices[6]] interp = interpolate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1], deriv=1) # same result from points and interpolation ref_deriv = self.helper_func_power_deriv( atgrid.points[atgrid.indices[5] : atgrid.indices[6]] ) assert_allclose(interp, ref_deriv) # test random x, y, z with fd xyz = np.random.rand(1, 3) xyz /= np.linalg.norm(xyz, axis=-1)[:, None] rad = np.random.normal() * np.random.randint(1, 11) xyz *= rad ref_value = self.helper_func_power_deriv(xyz) r = np.linalg.norm(xyz, axis=-1) theta = np.arctan2(xyz[:, 1], xyz[:, 0]) phi = np.arccos(xyz[:, 2] / r) interp = interpolate(result, np.abs(rad), theta, phi, deriv=1) assert_allclose(interp, ref_value) with self.assertRaises(ValueError): interp = interpolate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1], deriv=4) with self.assertRaises(ValueError): interp = interpolate( result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1], deriv=-1 )
def test_cubicspline_and_interp(self): """Test cubicspline interpolation values.""" rad = IdentityRTransform().transform_grid(HortonLinear(10)) rad._points += 1 atgrid = AtomicGrid.special_init(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms( r_sph, values, atgrid.weights, atgrid.indices, rad.points ) semi_sph_c = sph_coor[atgrid.indices[5] : atgrid.indices[6]] interp = interpolate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1]) # same result from points and interpolation assert_allclose(interp, values[atgrid.indices[5] : atgrid.indices[6]]) # random multiple interpolation test for _ in range(100): indices = np.random.randint(1, 11, np.random.randint(1, 10)) interp = interpolate(result, indices, semi_sph_c[:, 0], semi_sph_c[:, 1]) for i, j in enumerate(indices): assert_allclose( interp[i], values[atgrid.indices[j - 1] : atgrid.indices[j]] ) # test random x, y, z xyz = np.random.rand(10, 3) xyz /= np.linalg.norm(xyz, axis=-1)[:, None] rad = np.random.normal() * np.random.randint(1, 11) xyz *= rad ref_value = self.helper_func_power(xyz) r = np.linalg.norm(xyz, axis=-1) theta = np.arctan2(xyz[:, 1], xyz[:, 0]) phi = np.arccos(xyz[:, 2] / r) interp = interpolate(result, np.abs(rad), theta, phi) assert_allclose(interp, ref_value)
def test_errors_raise(self): """Test errors raise.""" with self.assertRaises(ValueError): GaussLaguerre(10, -1) with self.assertRaises(ValueError): GaussLaguerre(0, 1) with self.assertRaises(ValueError): GaussLegendre(-10) with self.assertRaises(ValueError): GaussChebyshev(-10) with self.assertRaises(ValueError): HortonLinear(-10) with self.assertRaises(ValueError): TanhSinh(10, 1) with self.assertRaises(ValueError): Simpson(4) with self.assertRaises(ValueError): GaussChebyshevType2(-10) with self.assertRaises(ValueError): GaussChebyshevLobatto(-10) with self.assertRaises(ValueError): Trapezoidal(-10) with self.assertRaises(ValueError): RectangleRuleSineEndPoints(-10) with self.assertRaises(ValueError): RectangleRuleSine(-10) with self.assertRaises(ValueError): TanhSinh(-11, 1) with self.assertRaises(ValueError): Simpson(-11) with self.assertRaises(ValueError): MidPoint(-10) with self.assertRaises(ValueError): ClenshawCurtis(-10) with self.assertRaises(ValueError): FejerFirst(-10) with self.assertRaises(ValueError): FejerSecond(-10)
def test_make_grid_integral(self): """Test molecular make_grid works as designed.""" pts = HortonLinear(70) tf = ExpRTransform(1e-5, 2e1) rgrid = tf.transform_1d_grid(pts) numbers = np.array([1, 1]) coordinates = np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]], float) becke = BeckeWeights(order=3) # construct molgrid for grid_type, deci in ( ("coarse", 3), ("medium", 4), ("fine", 5), ("veryfine", 6), ("ultrafine", 6), ("insane", 6), ): mg = MolGrid.from_preset(numbers, coordinates, rgrid, grid_type, becke) dist0 = np.sqrt(((coordinates[0] - mg.points) ** 2).sum(axis=1)) dist1 = np.sqrt(((coordinates[1] - mg.points) ** 2).sum(axis=1)) fn = np.exp(-2 * dist0) / np.pi + np.exp(-2 * dist1) / np.pi occupation = mg.integrate(fn) assert_almost_equal(occupation, 2.0, decimal=deci)
def test_cubicpline_and_interp(self): """Test cubicspline interpolation values.""" rad = HortonLinear(10) rad._points += 1 atgrid = AtomicGrid(rad, 1, scales=[], degs=[7]) sph_coor = atgrid.convert_cart_to_sph() values = self.helper_func_power(atgrid.points) l_max = atgrid.l_max // 2 r_sph = generate_real_sph_harms(l_max, sph_coor[:, 0], sph_coor[:, 1]) result = spline_with_sph_harms(r_sph, values, atgrid.weights, atgrid.indices, rad.points) semi_sph_c = sph_coor[atgrid.indices[5]:atgrid.indices[6]] interp = interpelate(result, 6, semi_sph_c[:, 0], semi_sph_c[:, 1]) # same result from points and interpolation assert_allclose(interp, values[atgrid.indices[5]:atgrid.indices[6]]) # random multiple interpolation test for _ in range(100): indices = np.random.randint(1, 11, np.random.randint(1, 10)) interp = interpelate(result, indices, semi_sph_c[:, 0], semi_sph_c[:, 1]) for i, j in enumerate(indices): assert_allclose( interp[i], values[atgrid.indices[j - 1]:atgrid.indices[j]])
def test_from_predefined(self): """Test grid construction with predefined grid.""" # test coarse grid pts = HortonLinear(20) tf = PowerRTransform(7.0879993828935345e-06, 16.05937640019924) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="coarse") # 604 points for coarse H atom assert_equal(atgrid.size, 604) assert_almost_equal( np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights), 5.56840953, ) # test medium grid pts = HortonLinear(24) tf = PowerRTransform(3.69705074304963e-06, 19.279558946793685) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="medium") # 928 points for coarse H atom assert_equal(atgrid.size, 928) assert_almost_equal( np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights), 5.56834559, ) # test fine grid pts = HortonLinear(34) tf = PowerRTransform(2.577533167224667e-07, 16.276983371222354) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="fine") # 1984 points for coarse H atom assert_equal(atgrid.size, 1984) assert_almost_equal( np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights), 5.56832800, ) # test veryfine grid pts = HortonLinear(41) tf = PowerRTransform(1.1774580743206259e-07, 20.140888089596444) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="veryfine") # 3154 points for coarse H atom assert_equal(atgrid.size, 3154) assert_almost_equal( np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights), 5.56832800, ) # test ultrafine grid pts = HortonLinear(49) tf = PowerRTransform(4.883104847991021e-08, 21.05456999309752) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="ultrafine") # 4546 points for coarse H atom assert_equal(atgrid.size, 4546) assert_almost_equal( np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights), 5.56832800, ) # test insane grid pts = HortonLinear(59) tf = PowerRTransform(1.9221827244049134e-08, 21.413278983919113) rad_grid = tf.transform_1d_grid(pts) atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="insane") # 6622 points for coarse H atom assert_equal(atgrid.size, 6622) assert_almost_equal( np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights), 5.56832800, )
def test_horton_linear(self): """Test horton linear grids.""" grid = HortonLinear(10) assert_allclose(grid.points, np.arange(10)) assert_allclose(grid.weights, np.ones(10))
def test_raise_errors(self): """Test molgrid errors raise.""" atg = AtomGrid.from_pruned( self.rgrid, 0.5, r_sectors=np.array([]), degs=np.array([17]), center=np.array([0.0, 0.0, 0.0]), ) # errors of aim_weight with self.assertRaises(TypeError): MolGrid([atg], aim_weights="test", atom_nums=np.array([1])) with self.assertRaises(ValueError): MolGrid([atg], aim_weights=np.array(3), atom_nums=np.array([1])) with self.assertRaises(TypeError): MolGrid([atg], aim_weights=[3, 5], atom_nums=np.array([1])) # integrate errors becke = BeckeWeights({1: 0.472_431_53}, order=3) molg = MolGrid([atg], becke, np.array([1])) with self.assertRaises(ValueError): molg.integrate() with self.assertRaises(TypeError): molg.integrate(1) with self.assertRaises(ValueError): molg.integrate(np.array([3, 5])) with self.assertRaises(ValueError): molg.get_atomic_grid(-3) molg = MolGrid([atg], becke, np.array([1]), store=True) with self.assertRaises(ValueError): molg.get_atomic_grid(-5) # test make_grid error pts = HortonLinear(70) tf = ExpRTransform(1e-5, 2e1) rgrid = tf.transform_1d_grid(pts) numbers = np.array([1, 1]) becke = BeckeWeights(order=3) # construct molgrid with self.assertRaises(ValueError): MolGrid.make_grid(numbers, np.array([0.0, 0.0, 0.0]), rgrid, "fine", becke) with self.assertRaises(ValueError): MolGrid.make_grid(np.array([1, 1]), np.array([[0.0, 0.0, 0.0]]), rgrid, "fine", becke) with self.assertRaises(ValueError): MolGrid.make_grid(np.array([1, 1]), np.array([[0.0, 0.0, 0.0]]), rgrid, "fine", becke) with self.assertRaises(TypeError): MolGrid.make_grid( np.array([1, 1]), np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]]), {3, 5}, "fine", becke, ) with self.assertRaises(TypeError): MolGrid.make_grid( np.array([1, 1]), np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]]), rgrid, np.array([3, 5]), becke, )
def setUp(self): """Set up radial grid for integral tests.""" pts = HortonLinear(100) tf = ExpRTransform(1e-5, 2e1) self.rgrid = tf.transform_1d_grid(pts)