Пример #1
0
    def test_from_predefined(self):
        """Test grid construction with predefined grid."""
        # test coarse grid
        pts = HortonLinear(20)
        tf = PowerRTransform(7.0879993828935345e-06, 16.05937640019924)
        rad_grid = tf.transform_1d_grid(pts)
        atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="coarse")
        # 604 points for coarse H atom
        assert_equal(atgrid.size, 604)
        assert_almost_equal(
            np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights),
            5.56840953,
        )

        # test medium grid
        pts = HortonLinear(24)
        tf = PowerRTransform(3.69705074304963e-06, 19.279558946793685)
        rad_grid = tf.transform_1d_grid(pts)
        atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="medium")
        # 928 points for coarse H atom
        assert_equal(atgrid.size, 928)
        assert_almost_equal(
            np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights),
            5.56834559,
        )
        # test fine grid
        pts = HortonLinear(34)
        tf = PowerRTransform(2.577533167224667e-07, 16.276983371222354)
        rad_grid = tf.transform_1d_grid(pts)
        atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="fine")
        # 1984 points for coarse H atom
        assert_equal(atgrid.size, 1984)
        assert_almost_equal(
            np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights),
            5.56832800,
        )
        # test veryfine grid
        pts = HortonLinear(41)
        tf = PowerRTransform(1.1774580743206259e-07, 20.140888089596444)
        rad_grid = tf.transform_1d_grid(pts)
        atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="veryfine")
        # 3154 points for coarse H atom
        assert_equal(atgrid.size, 3154)
        assert_almost_equal(
            np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights),
            5.56832800,
        )
        # test ultrafine grid
        pts = HortonLinear(49)
        tf = PowerRTransform(4.883104847991021e-08, 21.05456999309752)
        rad_grid = tf.transform_1d_grid(pts)
        atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="ultrafine")
        # 4546 points for coarse H atom
        assert_equal(atgrid.size, 4546)
        assert_almost_equal(
            np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights),
            5.56832800,
        )
        # test insane grid
        pts = HortonLinear(59)
        tf = PowerRTransform(1.9221827244049134e-08, 21.413278983919113)
        rad_grid = tf.transform_1d_grid(pts)
        atgrid = AtomGrid.from_preset(rad_grid, atnum=1, preset="insane")
        # 6622 points for coarse H atom
        assert_equal(atgrid.size, 6622)
        assert_almost_equal(
            np.sum(np.exp(-np.sum(atgrid.points**2, axis=1)) * atgrid.weights),
            5.56832800,
        )
Пример #2
0
    def from_preset(
        cls,
        atnums,
        atcoords,
        rgrid,
        preset,
        aim_weights,
        *_,
        rotate=37,
        store=False,
    ):
        """Construct molecular grid wih preset parameters.

        Parameters
        ----------
        atnums : np.ndarray(N,)
            array of atomic number
        atcoords : np.ndarray(N, 3)
            atomic coordinates of atoms
        rgrid : OneDGrid
            one dimension grid  to construct spherical grid
        preset : str
            preset grid accuracy scheme, support "coarse", "medium", "fine",
            "veryfine", "ultrafine", "insane"
        aim_weights : Callable or np.ndarray(K,)
            Atoms in molecule weights.
        rotate : bool, optional
            Random rotate for each shell of atomic grid
        store : bool, optional
            Store atomic grid separately
        """
        # construct for a atom molecule
        if atcoords.ndim != 2:
            raise ValueError("The dimension of coordinates need to be 2\n"
                             f"got shape: {atcoords.ndim}")
        if len(atnums) != atcoords.shape[0]:
            raise ValueError(
                "shape of atomic nums does not match with coordinates\n"
                f"atomic numbers: {atnums.shape}, coordinates: {atcoords.shape}"
            )
        total_atm = len(atnums)
        atomic_grids = []
        for i in range(total_atm):
            # get proper radial grid
            if isinstance(rgrid, OneDGrid):
                rad = rgrid
            elif isinstance(rgrid, list):
                rad = rgrid[i]
            elif isinstance(rgrid, dict):
                rad = rgrid[atnums[i]]
            else:
                raise TypeError(
                    f"not supported radial grid input; got input type: {type(rgrid)}"
                )
            # get proper grid type
            if isinstance(preset, str):
                gd_type = preset
            elif isinstance(preset, list):
                gd_type = preset[i]
            elif isinstance(preset, dict):
                gd_type = preset[atnums[i]]
            else:
                raise TypeError(
                    f"Not supported preset type; got preset {preset} with type {type(preset)}"
                )
            at_grid = AtomGrid.from_preset(rad,
                                           atnum=atnums[i],
                                           preset=gd_type,
                                           center=atcoords[i],
                                           rotate=rotate)
            atomic_grids.append(at_grid)
        return cls(atnums, atomic_grids, aim_weights, store=store)
Пример #3
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))
Пример #4
0
    def test_make_grid_different_grid_type(self):
        """Test different kind molgrid initizalize setting."""
        # three different radial grid
        rad2 = GaussLaguerre(50)
        rad3 = GaussLaguerre(70)
        # construct grid
        numbers = np.array([1, 8, 1])
        coordinates = np.array(
            [[0.0, 0.0, -0.5], [0.0, 0.0, 0.5], [0.0, 0.5, 0.0]], float
        )
        becke = BeckeWeights(order=3)

        # grid_type test with list
        mg = MolGrid.from_preset(
            numbers,
            coordinates,
            rad2,
            ["fine", "veryfine", "medium"],
            becke,
            store=True,
            rotate=False,
        )
        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.exp(-2 * dist1) + np.exp(-2 * dist2)) / np.pi
        occupation = mg.integrate(fn)
        assert_almost_equal(occupation, 3, decimal=3)

        atgrid1 = AtomGrid.from_preset(
            rad2, atnum=numbers[0], preset="fine", center=coordinates[0]
        )
        atgrid2 = AtomGrid.from_preset(
            rad2, atnum=numbers[1], preset="veryfine", center=coordinates[1]
        )
        atgrid3 = AtomGrid.from_preset(
            rad2, atnum=numbers[2], preset="medium", center=coordinates[2]
        )
        assert_allclose(mg._atgrids[0].points, atgrid1.points)
        assert_allclose(mg._atgrids[1].points, atgrid2.points)
        assert_allclose(mg._atgrids[2].points, atgrid3.points)

        # grid type test with dict
        mg = MolGrid.from_preset(
            numbers,
            coordinates,
            rad3,
            {1: "fine", 8: "veryfine"},
            becke,
            store=True,
            rotate=False,
        )
        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.exp(-2 * dist1) + np.exp(-2 * dist2)) / np.pi
        occupation = mg.integrate(fn)
        assert_almost_equal(occupation, 3, decimal=3)

        atgrid1 = AtomGrid.from_preset(
            rad3, atnum=numbers[0], preset="fine", center=coordinates[0]
        )
        atgrid2 = AtomGrid.from_preset(
            rad3, atnum=numbers[1], preset="veryfine", center=coordinates[1]
        )
        atgrid3 = AtomGrid.from_preset(
            rad3, atnum=numbers[2], preset="fine", center=coordinates[2]
        )
        assert_allclose(mg._atgrids[0].points, atgrid1.points)
        assert_allclose(mg._atgrids[1].points, atgrid2.points)
        assert_allclose(mg._atgrids[2].points, atgrid3.points)