Exemple #1
0
    def test_get_localgrid_small_radius(self):
        """Basic checks for the get_localgrid method.

        In this unit test, the cutoff sphere fits inside a primitive cell, such
        that each grid point from the parent periodic grid will at most appear
        once in the local grid.
        """
        center = self.grid.points[3]
        radius = 0.19475
        # Check that the sphere fits inside the primitive cell:
        assert (2 * radius < self.grid.spacings).all()
        # Build local grids.
        localgrids = [
            self.grid.get_localgrid(center, radius),
            self.wrapped_grid.get_localgrid(center, radius),
        ]
        # When there are no lattice vectors, the local grid from the base class
        # should also be the same.
        if self._ref_realvecs is None:
            aperiodic_grid = Grid(self._ref_points, self._ref_weights)
            localgrids.append(aperiodic_grid.get_localgrid(center, radius))
        # One should get the same local grid with or without wrapping, possibly
        # with a different ordering of the points. We can perform a relatively
        # simple check here because each point appears at most once.
        order0 = localgrids[0].indices.argsort()
        for localgrid in localgrids[1:]:
            assert_allclose(localgrids[0].center, localgrid.center)
            order = localgrid.indices.argsort()
            assert_allclose(localgrids[0].points[order0],
                            localgrid.points[order])
            assert_allclose(localgrids[0].weights[order0],
                            localgrid.weights[order])
        # Other sanity checks on the grids.
        for localgrid in localgrids:
            # Just make sure we are testing with an actual local grid with at least
            # some points.
            assert localgrid.size > 0
            assert localgrid.size <= self.grid.size
            assert len(localgrid.indices) == len(set(localgrid.indices))
            # Test that the localgrid contains sensible results.
            assert_allclose(localgrid.center, center)
            assert localgrid.points.ndim == self.grid.points.ndim
            assert localgrid.weights.ndim == self.grid.weights.ndim
            if self._ref_points.ndim == 2:
                assert (np.linalg.norm(localgrid.points - center, axis=1) <=
                        radius).all()
            else:
                assert (abs(localgrid.points - center) <= radius).all()
Exemple #2
0
    def test_get_localgrid_large_radius(self):
        """Basic checks for the get_localgrid method.

        In this unit test, the cutoff sphere fits inside a primitive cell, such
        that each grid point from the parent periodic grid will at most appear
        once in the local grid.
        """
        center = self.grid.points[3]
        radius = 2.51235
        # Check that the sphere flows over the primitive cell, when there are
        # some lattice vectors.
        if self._ref_realvecs is not None:
            assert (2 * radius > self.grid.spacings).any()
        # Build local grids.
        localgrids = [
            self.grid.get_localgrid(center, radius),
            self.wrapped_grid.get_localgrid(center, radius),
        ]
        # When there are no lattice vectors, the local grid from the base class
        # should also be the same.
        if self._ref_realvecs is None:
            aperiodic_grid = Grid(self._ref_points, self._ref_weights)
            localgrids.append(aperiodic_grid.get_localgrid(center, radius))
        # One should get the same local grid with or without wrapping, possibly
        # with a different ordering of the points.
        for localgrid in localgrids[1:]:
            assert_equal_localgrids(localgrids[0], localgrid)
        # Other sanity checks.
        for localgrid in localgrids:
            # With a large radius there will never be less points in the local grid.
            if self._ref_realvecs is None:
                assert localgrid.size == self.grid.size
            else:
                assert localgrid.size > self.grid.size
            # Test that the local grid contains sensible results.
            assert_allclose(localgrid.center, center)
            assert localgrid.points.ndim == self.grid.points.ndim
            assert localgrid.weights.ndim == self.grid.weights.ndim
            if self._ref_points.ndim == 2:
                assert (
                    np.linalg.norm(localgrid.points - center, axis=1) <= radius
                ).all()
            else:
                assert (abs(localgrid.points - center) <= radius).all()
Exemple #3
0
class TestGrid(TestCase):
    """Grid testcase class."""
    def setUp(self):
        """Test setup function."""
        self._ref_points = np.linspace(-1, 1, 21)
        self._ref_weights = np.ones(21) * 0.1
        self.grid = Grid(self._ref_points, self._ref_weights)

    def test_init_grid(self):
        """Test Grid init."""
        # tests property
        assert isinstance(self.grid, Grid)
        assert_allclose(self.grid.points, self._ref_points, atol=1e-7)
        assert_allclose(self.grid.weights, self._ref_weights)
        assert self.grid.size == self._ref_weights.size

    def test_integrate(self):
        """Test Grid integral."""
        # integral test1
        result2 = self.grid.integrate(np.ones(21))
        assert_allclose(result2, 2.1)
        # integral test2
        value1 = np.linspace(-1, 1, 21)
        value2 = value1**2
        result3 = self.grid.integrate(value1, value2)
        assert_allclose(result3, 0, atol=1e-7)

    def test_getitem(self):
        """Test Grid index and slicing."""
        # test index
        grid_index = self.grid[10]
        ref_grid = Grid(self._ref_points[10:11], self._ref_weights[10:11])
        assert_allclose(grid_index.points, ref_grid.points)
        assert_allclose(grid_index.weights, ref_grid.weights)
        assert isinstance(grid_index, Grid)
        # test slice
        ref_grid_slice = Grid(self._ref_points[:11], self._ref_weights[:11])
        grid_slice = self.grid[:11]
        assert_allclose(grid_slice.points, ref_grid_slice.points)
        assert_allclose(grid_slice.weights, ref_grid_slice.weights)
        assert isinstance(grid_slice, Grid)
        a = np.array([1, 3, 5])
        ref_smt_index = self.grid[a]
        assert_allclose(ref_smt_index.points, self._ref_points[a])
        assert_allclose(ref_smt_index.weights, self._ref_weights[a])

    def test_get_localgrid(self):
        """Test the creation of the local grid with a normal radius."""
        center = self.grid.points[3]
        radius = 0.2
        localgrid = self.grid.get_localgrid(center, radius)
        # Just make sure we are testing with an actual local grid with less (but
        # not zero) points.
        assert localgrid.size > 0
        assert localgrid.size < self.grid.size
        # Test that the local grid contains the correct results.
        assert localgrid.points.ndim == self.grid.points.ndim
        assert localgrid.weights.ndim == self.grid.weights.ndim
        assert_allclose(localgrid.points, self.grid.points[localgrid.indices])
        assert_allclose(localgrid.weights,
                        self.grid.weights[localgrid.indices])
        if self._ref_points.ndim == 2:
            assert (np.linalg.norm(localgrid.points - center, axis=1) <=
                    radius).all()
        else:
            assert (abs(localgrid.points - center) <= radius).all()

    def test_get_localgrid_radius_inf(self):
        """Test the creation of the local grid with an infinite radius."""
        localgrid = self.grid.get_localgrid(self.grid.points[3], np.inf)
        # Just make sure we are testing with a real local grid
        assert localgrid.size == self.grid.size
        assert_allclose(localgrid.points, self.grid.points)
        assert_allclose(localgrid.weights, self.grid.weights)
        assert_allclose(localgrid.indices, np.arange(self.grid.size))

    def test_errors_raise(self):
        """Test errors raise."""
        # grid init
        with self.assertRaises(ValueError):
            Grid(self._ref_points, np.ones(len(self._ref_weights) + 1))
        with self.assertRaises(ValueError):
            Grid(self._ref_points.reshape(self.grid.size, 1, 1),
                 self._ref_weights)
        with self.assertRaises(ValueError):
            Grid(self._ref_points, self._ref_weights.reshape(-1, 1))
        # integral
        with self.assertRaises(ValueError):
            self.grid.integrate()
        with self.assertRaises(TypeError):
            self.grid.integrate(5)
        if self._ref_points.ndim == 2:
            with self.assertRaises(ValueError):
                self.grid.integrate(self._ref_points)
        # get_localgrid
        with self.assertRaises(ValueError):
            self.grid.get_localgrid(self._ref_points[0], -1)
        with self.assertRaises(ValueError):
            self.grid.get_localgrid(self._ref_points[0], -np.inf)
        with self.assertRaises(ValueError):
            self.grid.get_localgrid(self._ref_points[0], np.nan)
        if self._ref_points.ndim == 2:
            with self.assertRaises(ValueError):
                self.grid.get_localgrid(
                    np.zeros(self._ref_points.shape[1] + 1), 5.0)
        else:
            with self.assertRaises(ValueError):
                self.grid.get_localgrid(np.zeros(2), 5.0)