コード例 #1
0
 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)
コード例 #2
0
ファイル: test_interpolate.py プロジェクト: FarnazH/grid
 def test_spline_with_sph_harms(self):
     """Test spline projection the same as spherical harmonics."""
     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])
     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)
     # generate ref
     for shell in range(1, 11):
         sh_grid = atgrid.get_shell_grid(shell - 1, r_sq=False)
         # Convert to spherical harmonics
         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)
         # General all spherical harmonics up to l_max
         r_sph = generate_real_sph_harms(l_max, theta, phi)
         # Project the function 2x^2 + 3y^2 + 4z^2
         r_sph_proj = np.sum(
             r_sph * self.helper_func_power(sh_grid.points) *
             sh_grid.weights,
             axis=-1,
         )
         # Check the actual projection against the cubic spline interpolation.
         assert_allclose(r_sph_proj, result(shell), atol=1e-10)
コード例 #3
0
ファイル: test_interpolate.py プロジェクト: FarnazH/grid
    def test_derivative_of_interpolate_given_splines(self):
        """Test the spline interpolating derivative of function in radial coordinate."""
        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])
        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_given_splines(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_given_splines(result,
                                           np.abs(rad),
                                           theta,
                                           phi,
                                           deriv=1)
        assert_allclose(interp, ref_value)

        with self.assertRaises(ValueError):
            interpolate_given_splines(result,
                                      6,
                                      semi_sph_c[:, 1],
                                      semi_sph_c[:, 2],
                                      deriv=4)
        with self.assertRaises(ValueError):
            interpolate_given_splines(result,
                                      6,
                                      semi_sph_c[:, 1],
                                      semi_sph_c[:, 2],
                                      deriv=-1)
コード例 #4
0
    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
            )
コード例 #5
0
ファイル: test_interpolate.py プロジェクト: FarnazH/grid
    def test_interpolate_given_splines_at_random_points(self):
        """Test interpolation given splines at random points."""
        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])
        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_given_splines(result, 6, semi_sph_c[:, 1],
                                           semi_sph_c[:, 2])
        # 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_given_splines(result, indices,
                                               semi_sph_c[:, 1], semi_sph_c[:,
                                                                            2])
            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_given_splines(result, np.abs(rad), theta, phi)
        assert_allclose(interp, ref_value)
コード例 #6
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)
コード例 #7
0
    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]])