Ejemplo n.º 1
0
    def test_cubicspline_and_deriv(self):
        """Test spline for derivation."""
        odg = OneDGrid(np.arange(10) + 1, np.ones(10), (0, np.inf))
        rad = IdentityRTransform().transform_1d_grid(odg)
        atgrid = AtomGrid.from_pruned(rad, 1, r_sectors=[], 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[:, 1], sph_coor[:, 2])
        result = spline_with_sph_harms(r_sph, values, atgrid.weights,
                                       atgrid.indices, rad)
        semi_sph_c = sph_coor[atgrid.indices[5]:atgrid.indices[6]]
        interp = interpolate(result,
                             6,
                             semi_sph_c[:, 1],
                             semi_sph_c[:, 2],
                             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[:, 1],
                                 semi_sph_c[:, 2],
                                 deriv=4)
        with self.assertRaises(ValueError):
            interp = interpolate(result,
                                 6,
                                 semi_sph_c[:, 1],
                                 semi_sph_c[:, 2],
                                 deriv=-1)
Ejemplo n.º 2
0
 def test_cubicspline_and_interp_mol(self):
     """Test cubicspline interpolation values."""
     odg = OneDGrid(np.arange(10) + 1, np.ones(10), (0, np.inf))
     rad = IdentityRTransform().transform_1d_grid(odg)
     atgrid = AtomGrid.from_pruned(rad, 1, sectors_r=[], sectors_degree=[7])
     values = self.helper_func_power(atgrid.points)
     result = spline_with_atomic_grid(atgrid, values)
     sph_coor = atgrid.convert_cart_to_sph()
     semi_sph_c = sph_coor[atgrid.indices[5] : atgrid.indices[6]]
     interp = interpolate(result, rad.points[5], semi_sph_c[:, 1], semi_sph_c[:, 2])
     # same result from points and interpolation
     assert_allclose(interp, values[atgrid.indices[5] : atgrid.indices[6]])
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 def test_cubicspline_and_interp_gauss(self):
     """Test cubicspline interpolation values."""
     oned = GaussLegendre(30)
     btf = BeckeTF(0.0001, 1.5)
     rad = btf.transform_1d_grid(oned)
     atgrid = AtomGrid.from_pruned(rad, 1, sectors_r=[], sectors_degree=[7])
     value_array = self.helper_func_gauss(atgrid.points)
     result = spline_with_atomic_grid(atgrid, value_array)
     # random test points on gauss function
     for _ in range(20):
         r = np.random.rand(1)[0] * 2
         theta = np.random.rand(10)
         phi = np.random.rand(10)
         inters = interpolate(result, r, theta, phi)
         x = r * np.sin(phi) * np.cos(theta)
         y = r * np.sin(phi) * np.sin(theta)
         z = r * np.cos(phi)
         assert_allclose(
             self.helper_func_gauss(np.array([x, y, z]).T), inters, atol=1e-4
         )
Ejemplo n.º 5
0
    def test_poisson_proj(self):
        """Test the project function."""
        oned = GaussChebyshev(30)
        btf = BeckeRTransform(0.0001, 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)
        spl_res = spline_with_atomic_grid(atgrid, value_array)
        # test for random, r, theta, phi
        for _ in range(20):
            r = np.random.rand(1)[0] * 2
            theta = np.random.rand(10) * 3.14
            phi = np.random.rand(10) * 3.14
            result = interpolate(spl_res, r, theta, phi)
            x = r * np.sin(phi) * np.cos(theta)
            y = r * np.sin(phi) * np.sin(theta)
            z = r * np.cos(phi)
            result_ref = self.helper_func_gauss(np.array([x, y, z]).T)
            # assert similar value less than 1e-4 discrepancy
            assert_allclose(result, result_ref, 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 each point is the same
        for _ in range(20):
            r = np.random.rand(1)[0] * 2
            # random spherical point
            int_res = np.zeros((l_max, l_max // 2 + 1), dtype=float)
            for j in range(l_max // 2 + 1):
                for i in range(-j, j + 1):
                    int_res[i, j] += spls_mt[i, j](r)
            assert_allclose(spl_res(r), int_res)