Esempio n. 1
0
 def test_different_aim_weights_h2(self):
     """Test different aim_weights for molgrid."""
     coordinates = np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]], float)
     atg1 = AtomicGrid(
         self.rgrid,
         0.5,
         scales=np.array([]),
         degs=np.array([17]),
         center=coordinates[0],
     )
     atg2 = AtomicGrid(
         self.rgrid,
         0.5,
         scales=np.array([]),
         degs=np.array([17]),
         center=coordinates[1],
     )
     aim_weights = np.ones(22000)
     mg = MolGrid([atg1, atg2],
                  np.array([0.5, 0.5]),
                  aim_weights=aim_weights)
     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, 4.0, decimal=4)
Esempio n. 2
0
    def test_integrate_hydrogen_trimer_1s(self):
        """Test molecular integral in H3."""
        coordinates = np.array(
            [[0.0, 0.0, -0.5], [0.0, 0.0, 0.5], [0.0, 0.5, 0.0]], float)
        atg1 = AtomicGrid(
            self.rgrid,
            0.5,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[0],
        )
        atg2 = AtomicGrid(
            self.rgrid,
            0.5,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[1],
        )
        atg3 = AtomicGrid(
            self.rgrid,
            0.5,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[2],
        )

        mg = MolGrid([atg1, atg2, atg3], np.array([0.5, 0.5, 0.5]))
        dist0 = np.sqrt(((coordinates[0] - mg.points)**2).sum(axis=1))
        dist1 = np.sqrt(((coordinates[1] - mg.points)**2).sum(axis=1))
        dist2 = np.sqrt(((coordinates[2] - mg.points)**2).sum(axis=1))
        fn = (np.exp(-2 * dist0) / np.pi + np.exp(-2 * dist1) / np.pi +
              np.exp(-2 * dist2) / np.pi)
        occupation = mg.integrate(fn)
        assert_almost_equal(occupation, 3.0, decimal=4)
Esempio n. 3
0
 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 = RadialGrid(radial_pts, radial_wts)
     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.special_init(radial_grid,
                                     radius=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.special_init(radial_grid,
                                     radius=rad,
                                     scales=np.array([]),
                                     degs=np.array([6]))
     assert isinstance(ag_ob, AtomicGrid)
     assert len(ag_ob.indices) == 11
     ag_ob = AtomicGrid(radial_grid, nums=[110])
     assert ag_ob.l_max == 17
     assert_array_equal(ag_ob._rad_degs, np.ones(10) * 17)
     assert ag_ob.size == 110 * 10
     # new init AtomicGrid
     ag_ob2 = AtomicGrid(radial_grid, degs=[17])
     assert ag_ob2.l_max == 17
     assert_array_equal(ag_ob2._rad_degs, np.ones(10) * 17)
     assert ag_ob2.size == 110 * 10
Esempio n. 4
0
 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 = RadialGrid(rad_pts, rad_wts)
     degs = [3, 5, 7]
     atgrid = AtomicGrid(rad_grid, degs=degs)
     # make sure True and 1 is not the same result
     atgrid1 = AtomicGrid(rad_grid, degs=degs, rotate=True)
     atgrid2 = AtomicGrid(rad_grid, degs=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)
Esempio n. 5
0
 def test_preload_unit_sphere_grid(self):
     """Test for private method to preload spherical grids."""
     degs = [3, 3, 5, 5, 7, 7]
     unit_sphere = AtomicGrid._preload_unit_sphere_grid(degs)
     assert len(unit_sphere) == 3
     degs = [3, 4, 5, 6, 7]
     unit_sphere2 = AtomicGrid._preload_unit_sphere_grid(degs)
     assert len(unit_sphere2) == 5
     assert_allclose(unit_sphere2[4].points, unit_sphere2[5].points)
     assert_allclose(unit_sphere2[6].points, unit_sphere2[7].points)
     assert not np.allclose(unit_sphere2[4].points.shape,
                            unit_sphere2[6].points.shape)
Esempio n. 6
0
 def test_horton_molgrid(self):
     """Test horton style grid."""
     coors = np.array([[0, 0, -0.5], [0, 0, 0.5]])
     nums = [1, 1]
     mol_grid = MolGrid.horton_molgrid(coors, nums, self.rgrid, 110)
     atg1 = AtomicGrid.special_init(
         self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coors[0]
     )
     atg2 = AtomicGrid.special_init(
         self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=coors[1]
     )
     ref_grid = MolGrid([atg1, atg2], nums, store=True)
     assert_allclose(ref_grid.points, mol_grid.points)
     assert_allclose(ref_grid.weights, mol_grid.weights)
Esempio n. 7
0
 def test_atomic_grid(self):
     """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 = RadialGrid(rad_pts, rad_wts)
     degs = np.array([3, 5, 7])
     # origin center
     # randome center
     pts, wts, ind = AtomicGrid._generate_atomic_grid(rad_grid, degs)
     ref_pts, ref_wts, ref_ind = AtomicGrid._generate_atomic_grid(
         rad_grid, degs)
     # diff grid points diff by center and same weights
     assert_allclose(pts, ref_pts)
     assert_allclose(wts, ref_wts)
Esempio n. 8
0
    def test_raise_errors(self):
        """Test molgrid errors raise."""
        atg = AtomicGrid.special_init(
            self.rgrid,
            0.5,
            scales=np.array([]),
            degs=np.array([17]),
            center=np.array([0.0, 0.0, 0.0]),
        )
        # initilize errors
        with self.assertRaises(NotImplementedError):
            MolGrid([atg], np.array([1]), aim_weights="test")
        with self.assertRaises(ValueError):
            MolGrid([atg], np.array([1]), aim_weights=np.array(3))
        with self.assertRaises(TypeError):
            MolGrid([atg], np.array([1]), aim_weights=[3, 5])
        # integrate errors
        molg = MolGrid([atg], 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(NotImplementedError):
            molg.get_atomic_grid(0)
        with self.assertRaises(ValueError):
            molg.get_aim_weights(-3)

        molg = MolGrid([atg], np.array([1]), store=True)
        with self.assertRaises(ValueError):
            molg.get_atomic_grid(-5)
Esempio n. 9
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)
Esempio n. 10
0
 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)
Esempio n. 11
0
 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
Esempio n. 12
0
 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])
Esempio n. 13
0
    def test_molgrid_attrs_subgrid(self):
        """Test sub atomic grid attributes."""
        # numbers = np.array([6, 8], int)
        coordinates = np.array([[0.0, 0.2, -0.5], [0.1, 0.0, 0.5]], float)
        atg1 = AtomicGrid.special_init(
            self.rgrid,
            1.228,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[0],
        )
        atg2 = AtomicGrid.special_init(
            self.rgrid,
            0.945,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[1],
        )
        mg = MolGrid([atg1, atg2], np.array([6, 8]), store=True)
        # mg = BeckeMolGrid(coordinates, numbers, None, (rgrid, 110), mode='keep')

        assert mg.size == 2 * 110 * 100
        assert mg.points.shape == (mg.size, 3)
        assert mg.weights.shape == (mg.size,)
        assert mg.aim_weights.shape == (mg.size,)
        assert len(mg._indices) == 2 + 1
        # assert mg.k == 3
        # assert mg.random_rotate

        for i in range(2):
            atgrid = mg[i]
            assert isinstance(atgrid, AtomicGrid)
            assert atgrid.size == 100 * 110
            assert atgrid.points.shape == (100 * 110, 3)
            assert atgrid.weights.shape == (100 * 110,)
            assert (atgrid.center == coordinates[i]).all()
        mg = MolGrid([atg1, atg2], np.array([6, 8]))
        for i in range(2):
            atgrid = mg[i]
            assert isinstance(atgrid, SimpleAtomicGrid)
            assert_allclose(atgrid.center, mg._coors[i])
Esempio n. 14
0
 def test_integrate_hydrogen_pair_1s(self):
     """Test molecular integral in H2."""
     coordinates = np.array([[0.0, 0.0, -0.5], [0.0, 0.0, 0.5]], float)
     atg1 = AtomicGrid.special_init(
         self.rgrid,
         0.5,
         scales=np.array([]),
         degs=np.array([17]),
         center=coordinates[0],
     )
     atg2 = AtomicGrid.special_init(
         self.rgrid,
         0.5,
         scales=np.array([]),
         degs=np.array([17]),
         center=coordinates[1],
     )
     mg = MolGrid([atg1, atg2], np.array([1, 1]))
     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=6)
Esempio n. 15
0
 def test_get_shell_grid(self):
     """Test angular grid get from get_shell_grid function."""
     rad_pts = np.array([0.1, 0.5, 1])
     rad_wts = np.array([0.3, 0.4, 0.3])
     rad_grid = RadialGrid(rad_pts, rad_wts)
     degs = [3, 5, 7]
     atgrid = AtomicGrid(rad_grid, degs=degs)
     assert atgrid.n_shells == 3
     # grep shell with r^2
     for i in range(atgrid.n_shells):
         sh_grid = atgrid.get_shell_grid(i)
         assert isinstance(sh_grid, AngularGrid)
         ref_grid = generate_lebedev_grid(degree=degs[i])
         assert np.allclose(sh_grid.points, ref_grid.points * rad_pts[i])
         assert np.allclose(sh_grid.weights,
                            ref_grid.weights * rad_wts[i] * rad_pts[i]**2)
     # grep shell without r^2
     for i in range(atgrid.n_shells):
         sh_grid = atgrid.get_shell_grid(i, r_sq=False)
         assert isinstance(sh_grid, AngularGrid)
         ref_grid = generate_lebedev_grid(degree=degs[i])
         assert np.allclose(sh_grid.points, ref_grid.points * rad_pts[i])
         assert np.allclose(sh_grid.weights, ref_grid.weights * rad_wts[i])
Esempio n. 16
0
    def test_molgrid_attrs(self):
        """Test MolGrid attributes."""
        # numbers = np.array([6, 8], int)
        coordinates = np.array([[0.0, 0.2, -0.5], [0.1, 0.0, 0.5]], float)
        atg1 = AtomicGrid(
            self.rgrid,
            1.228,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[0],
        )
        atg2 = AtomicGrid(
            self.rgrid,
            0.945,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[1],
        )
        mg = MolGrid([atg1, atg2], np.array([1.228, 0.945]))

        assert mg.size == 2 * 110 * 100
        assert mg.points.shape == (mg.size, 3)
        assert mg.weights.shape == (mg.size, )
        assert mg.aim_weights.shape == (mg.size, )
Esempio n. 17
0
    def test_molgrid_attrs(self):
        """Test MolGrid attributes."""
        # numbers = np.array([6, 8], int)
        coordinates = np.array([[0.0, 0.2, -0.5], [0.1, 0.0, 0.5]], float)
        atg1 = AtomicGrid.special_init(
            self.rgrid,
            1.228,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[0],
        )
        atg2 = AtomicGrid.special_init(
            self.rgrid,
            0.945,
            scales=np.array([]),
            degs=np.array([17]),
            center=coordinates[1],
        )
        mg = MolGrid([atg1, atg2], np.array([6, 8]), store=True)

        assert mg.size == 2 * 110 * 100
        assert mg.points.shape == (mg.size, 3)
        assert mg.weights.shape == (mg.size,)
        assert mg.aim_weights.shape == (mg.size,)
        assert mg.get_atomic_grid(0) is atg1
        assert mg.get_atomic_grid(1) is atg2

        simple_ag1 = mg.get_simple_atomic_grid(0)
        simple_ag2 = mg.get_simple_atomic_grid(1)
        assert_allclose(simple_ag1.points, atg1.points)
        assert_allclose(simple_ag2.points, atg2.points)

        aim_at1 = mg.get_aim_weights(0)
        aim_at2 = mg.get_aim_weights(1)
        assert_allclose(simple_ag1.weights, atg1.weights * aim_at1)
        assert_allclose(simple_ag2.weights, atg2.weights * aim_at2)
Esempio n. 18
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]])
Esempio n. 19
0
 def test_integrate_hydrogen_8_1s(self):
     """Test molecular integral in H2."""
     x, y, z = np.meshgrid(*(3 * [[-0.5, 0.5]]))
     centers = np.stack([x.ravel(), y.ravel(), z.ravel()], axis=1)
     atgs = [
         AtomicGrid.special_init(
             self.rgrid, 0.5, scales=np.array([]), degs=np.array([17]), center=center
         )
         for center in centers
     ]
     mg = MolGrid(atgs, np.array([1] * len(centers)))
     fn = 0
     for center in centers:
         dist = np.linalg.norm(center - mg.points, axis=1)
         fn += np.exp(-2 * dist) / np.pi
     occupation = mg.integrate(fn)
     assert_almost_equal(occupation, len(centers), decimal=2)
Esempio n. 20
0
    def horton_molgrid(cls,
                       coors,
                       numbers,
                       radial,
                       points_of_angular,
                       store=False,
                       rotate=False):
        """Initialize a MolGrid instance with Horton Style input.

        Example
        -------
        >>> onedg = HortonLinear(100) # number of points, oned grid before TF.
        >>> rgrid = ExpRTransform(1e-5, 2e1).transform_grid(onedg) # radial grid
        >>> molgrid = MolGrid.horton_molgrid(coors, numbers, rgrid, 110)

        Parameters
        ----------
        coors : np.ndarray(N, 3)
            Cartesian coordinates for each atoms
        numbers : np.ndarray(N,)
            Atomic number for each atoms
        radial : RadialGrid
            RadialGrid instance for constructing atomic grid for each atom
        points_of_angular : int
            Num of points on each shell of angular grid
        store : bool, optional
            Flag to store each original atomic grid information
        rotate : bool or int , optional
            Flag to set auto rotation for atomic grid, if given int, the number
            will be used as a seed to generate rantom matrix.

        Returns
        -------
        MolGrid
            MolGrid instance with specified grid property
        """
        at_grids = []
        for i, _ in enumerate(numbers):
            at_grids.append(
                AtomicGrid(radial,
                           nums=[points_of_angular],
                           center=coors[i],
                           rotate=rotate))
        return cls(at_grids, numbers, store=store)
Esempio n. 21
0
 def test_integrate_hydrogen_single_1s(self):
     """Test molecular integral in H atom."""
     # numbers = np.array([1], int)
     coordinates = np.array([0.0, 0.0, -0.5], float)
     # rgrid = BeckeTF.transform_grid(oned, 0.001, 0.5)[0]
     # rtf = ExpRTransform(1e-3, 1e1, 100)
     # rgrid = RadialGrid(rtf)
     atg1 = AtomicGrid.special_init(
         self.rgrid,
         0.5,
         scales=np.array([]),
         degs=np.array([17]),
         center=coordinates,
     )
     mg = MolGrid([atg1], np.array([1]))
     # mg = BeckeMolGrid(coordinates, numbers, None, (rgrid, 110), random_rotate=False)
     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)
Esempio n. 22
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
            )
Esempio n. 23
0
 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 = RadialGrid(rad_pts, rad_wts)
     degs = np.array([3, 5, 7])
     pts, wts, ind = AtomicGrid._generate_atomic_grid(rad_grid, degs)
     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,
         )
Esempio n. 24
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)
Esempio n. 25
0
 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]),
         )
Esempio n. 26
0
    def test_error_raises(self):
        """Tests for error raises."""
        with self.assertRaises(TypeError):
            AtomicGrid.special_init(np.arange(3),
                                    1.0,
                                    scales=np.arange(2),
                                    degs=np.arange(3))
        with self.assertRaises(ValueError):
            AtomicGrid.special_init(
                RadialGrid(np.arange(3), np.arange(3)),
                radius=1.0,
                scales=np.arange(2),
                degs=np.arange(0),
            )
        with self.assertRaises(ValueError):
            AtomicGrid.special_init(
                RadialGrid(np.arange(3), np.arange(3)),
                radius=1.0,
                scales=np.arange(2),
                degs=np.arange(4),
            )
        with self.assertRaises(ValueError):
            AtomicGrid._generate_atomic_grid(
                RadialGrid(np.arange(3), np.arange(3)), np.arange(2))
        with self.assertRaises(TypeError):
            AtomicGrid.special_init(
                RadialGrid(np.arange(3), np.arange(3)),
                radius=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.special_init(
                RadialGrid(np.arange(3), np.arange(3)),
                radius=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]),
            )

        with self.assertRaises(TypeError):
            AtomicGrid(RadialGrid(np.arange(3), np.arange(3)), nums=110)
        with self.assertRaises(TypeError):
            AtomicGrid(RadialGrid(np.arange(3), np.arange(3)), degs=17)
        with self.assertRaises(ValueError):
            AtomicGrid(RadialGrid(np.arange(3), np.arange(3)),
                       degs=[17],
                       rotate=-1)
        with self.assertRaises(ValueError):
            AtomicGrid(RadialGrid(np.arange(3), np.arange(3)),
                       degs=[17],
                       rotate="asdfaf")
Esempio n. 27
0
    point_charge_positions=R,
    point_charges_values=q,
    coord_type="cartesian",
    k="HF",
)
r0 = 1e-10
radii = get_cov_radii(molecule.atnums)
n_rad_points = 100
deg = 5
onedgrid = GaussChebyshev(n_rad_points)
molecule_at_grid = []
for i in range(len(molecule.atnums)):
    Rad = BeckeTF.find_parameter(onedgrid.points, r0, radii[i])
    rad_grid = BeckeTF(r0, Rad).transform_grid(onedgrid)
    molecule_at_grid = molecule_at_grid + [
        AtomicGrid.special_init(rad_grid, radii[i], degs=[deg], scales=[])
    ]
molgrid = MolGrid(molecule_at_grid, molecule.atnums)
alc_lda = Alchemical_tools(
    basis,
    molecule,
    point_charge_positions=R,
    point_charges_values=q,
    coord_type="cartesian",
    k="lda_x",
    molgrid=molgrid,
)
two_electron_int = electron_repulsion_integral(basis,
                                               transform=molecule.mo.coeffs.T,
                                               coord_type="cartesian",
                                               notation="chemist")