def test_atomic_rotate(self): """Test random rotation for atomic grid.""" rad_pts = np.array([0.1, 0.5, 1]) rad_wts = np.array([0.3, 0.4, 0.3]) rad_grid = OneDGrid(rad_pts, rad_wts) degs = [3, 5, 7] atgrid = AtomGrid(rad_grid, degrees=degs) # make sure True and 1 is not the same result atgrid1 = AtomGrid(rad_grid, degrees=degs, rotate=True) atgrid2 = AtomGrid(rad_grid, degrees=degs, rotate=1) # test diff points, same weights assert not np.allclose(atgrid.points, atgrid1.points) assert not np.allclose(atgrid.points, atgrid2.points) assert not np.allclose(atgrid1.points, atgrid2.points) assert_allclose(atgrid.weights, atgrid1.weights) assert_allclose(atgrid.weights, atgrid2.weights) assert_allclose(atgrid1.weights, atgrid2.weights) # test same integral value = np.prod(atgrid.points**2, axis=-1) value1 = np.prod(atgrid.points**2, axis=-1) value2 = np.prod(atgrid.points**2, axis=-1) res = atgrid.integrate(value) res1 = atgrid1.integrate(value1) res2 = atgrid2.integrate(value2) assert_almost_equal(res, res1) assert_almost_equal(res1, res2) # test rotated shells for i in range(len(degs)): non_rot_shell = atgrid.get_shell_grid(i).points rot_shell = atgrid2.get_shell_grid(i).points rot_mt = R.random(random_state=1 + i).as_matrix() assert_allclose(rot_shell, non_rot_shell @ rot_mt)
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_poisson_solve_mtr_cmpl(self): """Test solve poisson equation and interpolate the result.""" oned = GaussChebyshev(50) btf = BeckeRTransform(1e-7, 1.5) rad = btf.transform_1d_grid(oned) l_max = 7 atgrid = AtomGrid(rad, degrees=[l_max]) value_array = self.helper_func_gauss(atgrid.points) p_0 = atgrid.integrate(value_array) # test density sum up to np.pi**(3 / 2) assert_allclose(p_0, np.pi**1.5, atol=1e-4) sph_coor = atgrid.convert_cart_to_sph()[:, 1:3] spls_mt = Poisson._proj_sph_value( atgrid.rgrid, sph_coor, l_max // 2, value_array, atgrid.weights, atgrid.indices, ) ibtf = InverseRTransform(btf) linsp = np.linspace(-1, 0.99, 50) bound = p_0 * np.sqrt(4 * np.pi) pois_mtr = Poisson.solve_poisson(spls_mt, linsp, bound, tfm=ibtf) assert pois_mtr.shape == (7, 4) near_rg_pts = np.array([1e-2, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0, 1.2]) near_tf_pts = ibtf.transform(near_rg_pts) ref_short_res = [ 6.28286, # 0.01 6.26219, # 0.1 6.20029, # 0.2 6.09956, # 0.3 5.79652, # 0.5 5.3916, # 0.7 4.69236, # 1.0 4.22403, # 1.2 ] for i, j in enumerate(near_tf_pts): assert_almost_equal( Poisson.interpolate_radial(pois_mtr, j, 0, True) / near_rg_pts[i], ref_short_res[i] * np.sqrt(4 * np.pi), decimal=3, ) matrix_result = Poisson.interpolate_radial(pois_mtr, j) assert_almost_equal( matrix_result[0, 0] / near_rg_pts[i], ref_short_res[i] * np.sqrt(4 * np.pi), decimal=3, ) # test interpolate with sph result = Poisson.interpolate(pois_mtr, j, np.random.rand(5), np.random.rand(5)) assert_allclose(result / near_rg_pts[i] - ref_short_res[i], np.zeros(5), atol=1e-3)
def test_raises_errors(self): """Test proper error raises.""" oned = GaussChebyshev(50) btf = BeckeRTransform(1e-7, 1.5) rad = btf.transform_1d_grid(oned) l_max = 7 atgrid = AtomGrid(rad, degrees=[l_max]) value_array = self.helper_func_gauss(atgrid.points) p_0 = atgrid.integrate(value_array) # test density sum up to np.pi**(3 / 2) assert_allclose(p_0, np.pi**1.5, atol=1e-4) sph_coor = atgrid.convert_cart_to_sph() with self.assertRaises(ValueError): Poisson._proj_sph_value( atgrid.rgrid, sph_coor, l_max // 2, value_array, atgrid.weights, atgrid.indices, )
def test_poisson_solve(self): """Test the poisson solve function.""" oned = GaussChebyshev(30) oned = GaussChebyshev(50) btf = BeckeRTransform(1e-7, 1.5) rad = btf.transform_1d_grid(oned) l_max = 7 atgrid = AtomGrid(rad, degrees=[l_max]) value_array = self.helper_func_gauss(atgrid.points) p_0 = atgrid.integrate(value_array) # test density sum up to np.pi**(3 / 2) assert_allclose(p_0, np.pi**1.5, atol=1e-4) sph_coor = atgrid.convert_cart_to_sph()[:, 1:3] spls_mt = Poisson._proj_sph_value( atgrid.rgrid, sph_coor, l_max // 2, value_array, atgrid.weights, atgrid.indices, ) # test splines project fit gauss function well def gauss(r): return np.exp(-(r**2)) for _ in range(20): coors = np.random.rand(10, 3) r = np.linalg.norm(coors, axis=-1) spl_0_0 = spls_mt[0, 0] interp_v = spl_0_0(r) ref_v = gauss(r) * np.sqrt(4 * np.pi) # 0.28209479 is the value in spherical harmonic Z_0_0 assert_allclose(interp_v, ref_v, atol=1e-3) ibtf = InverseRTransform(btf) linsp = np.linspace(-1, 0.99, 50) bound = p_0 * np.sqrt(4 * np.pi) res_bv = Poisson.solve_poisson_bv(spls_mt[0, 0], linsp, bound, tfm=ibtf) near_rg_pts = np.array([1e-2, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0, 1.2]) near_tf_pts = ibtf.transform(near_rg_pts) long_rg_pts = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10]) long_tf_pts = ibtf.transform(long_rg_pts) short_res = res_bv(near_tf_pts)[0] / near_rg_pts / (2 * np.sqrt(np.pi)) long_res = res_bv(long_tf_pts)[0] / long_rg_pts / (2 * np.sqrt(np.pi)) # ref are calculated with mathemetical # integrate[exp[-x^2 - y^2 - z^2] / sqrt[(x - a)^2 + y^2 +z^2], range] ref_short_res = [ 6.28286, # 0.01 6.26219, # 0.1 6.20029, # 0.2 6.09956, # 0.3 5.79652, # 0.5 5.3916, # 0.7 4.69236, # 1.0 4.22403, # 1.2 ] ref_long_res = [ 2.77108, # 2 1.85601, # 3 1.39203, # 4 1.11362, # 5 0.92802, # 6 0.79544, # 7 0.69601, # 8 0.61867, # 9 0.55680, # 10 ] assert_allclose(short_res, ref_short_res, atol=5e-4) assert_allclose(long_res, ref_long_res, atol=5e-4) # solve same poisson equation with gauss directly gauss_pts = btf.transform(linsp) res_gs = Poisson.solve_poisson_bv(gauss, gauss_pts, p_0) gs_int_short = res_gs(near_rg_pts)[0] / near_rg_pts gs_int_long = res_gs(long_rg_pts)[0] / long_rg_pts assert_allclose(gs_int_short, ref_short_res, 5e-4) assert_allclose(gs_int_long, ref_long_res, 5e-4)