Example #1
0
    def test_generate_spherical(self):
        """Test generated real spherical harmonics values."""
        atgrid = AngularGrid(degree=7)
        pts = atgrid.points
        wts = atgrid.weights
        r = np.linalg.norm(pts, axis=1)
        # polar
        phi = np.arccos(pts[:, 2] / r)
        # azimuthal
        theta = np.arctan2(pts[:, 1], pts[:, 0])
        # generate spherical harmonics
        sph_h = AtomGrid._generate_real_sph_harm(3, theta, phi)  # l_max = 3
        assert sph_h.shape == (16, 26)
        for _ in range(100):
            n1, n2 = np.random.randint(0, 16, 2)
            re = sum(sph_h[n1] * sph_h[n2] * wts)
            if n1 != n2:
                print(n1, n2, re)
                assert_almost_equal(re, 0)
            else:
                print(n1, n2, re)
                assert_almost_equal(re, 1)

        for i in range(10):
            sph_h = AtomGrid._generate_real_sph_harm(i, theta, phi)
            assert sph_h.shape == ((i + 1)**2, 26)
Example #2
0
    def test_error_raises(self):
        """Tests for error raises."""
        with self.assertRaises(TypeError):
            AtomGrid.from_pruned(np.arange(3),
                                 1.0,
                                 sectors_r=np.arange(2),
                                 sectors_degree=np.arange(3))
        with self.assertRaises(ValueError):
            AtomGrid.from_pruned(
                OneDGrid(np.arange(3), np.arange(3)),
                radius=1.0,
                sectors_r=np.arange(2),
                sectors_degree=np.arange(0),
            )
        with self.assertRaises(ValueError):
            AtomGrid.from_pruned(
                OneDGrid(np.arange(3), np.arange(3)),
                radius=1.0,
                sectors_r=np.arange(2),
                sectors_degree=np.arange(4),
            )
        with self.assertRaises(ValueError):
            AtomGrid._generate_atomic_grid(
                OneDGrid(np.arange(3), np.arange(3)), np.arange(2))
        with self.assertRaises(ValueError):
            AtomGrid.from_pruned(
                OneDGrid(np.arange(3), np.arange(3)),
                radius=1.0,
                sectors_r=np.array([0.3, 0.5, 0.7]),
                sectors_degree=np.array([3, 5, 7, 5]),
                center=np.array([0, 0, 0, 0]),
            )

        # test preset
        with self.assertRaises(ValueError):
            AtomGrid.from_preset(atnum=1, preset="fine")

        with self.assertRaises(TypeError):
            AtomGrid(OneDGrid(np.arange(3), np.arange(3)), sizes=110)
        with self.assertRaises(TypeError):
            AtomGrid(OneDGrid(np.arange(3), np.arange(3)), degrees=17)
        with self.assertRaises(ValueError):
            AtomGrid(OneDGrid(np.arange(3), np.arange(3)),
                     degrees=[17],
                     rotate=-1)
        with self.assertRaises(TypeError):
            AtomGrid(OneDGrid(np.arange(3), np.arange(3)),
                     degrees=[17],
                     rotate="asdfaf")
        # error of radial grid
        with self.assertRaises(TypeError):
            AtomGrid(Grid(np.arange(1, 5, 1), np.ones(4)),
                     degrees=[2, 3, 4, 5])
        with self.assertRaises(TypeError):
            AtomGrid(OneDGrid(np.arange(-2, 2, 1), np.ones(4)),
                     degrees=[2, 3, 4, 5])
        with self.assertRaises(TypeError):
            rgrid = OneDGrid(np.arange(1, 3, 1), np.ones(2), domain=(-1, 5))
            AtomGrid(rgrid, degrees=[2])
        with self.assertRaises(TypeError):
            rgrid = OneDGrid(np.arange(-1, 1, 1), np.ones(2))
            AtomGrid(rgrid, degrees=[2])

        with self.assertRaises(ValueError):
            AtomGrid._generate_real_sph_harm(-1, np.random.rand(10),
                                             np.random.rand(10))
        with self.assertRaises(ValueError):
            oned = GaussLegendre(30)
            btf = BeckeTF(0.0001, 1.5)
            rad = btf.transform_1d_grid(oned)
            atgrid = AtomGrid.from_preset(rad, atnum=1, preset="fine")
            atgrid.fit_values(np.random.rand(100))