Пример #1
0
def test_kpoints_write_line(kpoints_parser_line, tmpdir):
    """Test read, write and read KPOINTS in line mode.

    """

    kpoints = kpoints_parser_line.get_dict()
    temp_file = str(tmpdir.join('KPOINTSLINE'))
    kpoints_parser_line.write(file_path=temp_file)
    kpoints_parser_line_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_line_temp.get_dict()
    assert kpoints_temp['mode'] == 'line'
    assert kpoints_temp['comment'] == 'k-points along high symmetry lines'
    assert kpoints_temp['divisions'] == None
    assert kpoints_temp['shifts'] == None
    assert kpoints_temp['centering'] == None
    assert kpoints_temp['num_kpoints'] == 40
    points = kpoints_temp['points']
    np.testing.assert_allclose(points[0][0], np.array([0.0, 0.0, 0.0]))
    np.testing.assert_allclose(points[1][0], np.array([0.5, 0.5, 0.0]))
    np.testing.assert_allclose(points[2][0], np.array([0.5, 0.5, 0.0]))
    np.testing.assert_allclose(points[3][0], np.array([0.5, 0.75, 0.25]))
    np.testing.assert_allclose(points[4][0], np.array([0.5, 0.75, 0.25]))
    np.testing.assert_allclose(points[5][0], np.array([0.0, 0.0, 0.0]))
    assert math.isclose(points[0][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[1][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[2][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[3][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[4][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[5][1], 1.0, rel_tol=1e-07)
    assert points[0][2]
    assert points[1][2]
    assert points[2][2]
    assert points[3][2]
    assert points[4][2]
    assert points[5][2]
Пример #2
0
def test_kpoints_modify_line(kpoints_parser_line, tmpdir):
    """Test read, modify, write and read KPOINTS in line mode.

    """

    kpoints = kpoints_parser_line.get_dict()
    assert kpoints['comment'] == 'k-points along high symmetry lines'
    kpoints_parser_line.modify('comment', 'No comment')
    point = Kpoint(np.array([0.5, 0.5, 0.25]), 1.0)
    kpoints_parser_line.modify('points', point, point_number=3)
    kpoints_parser_line.modify('points', point, point_number=4)
    temp_file = str(tmpdir.join('KPOINTSLINE'))
    kpoints_parser_line.write(file_path=temp_file)
    kpoints_parser_line_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_line_temp.get_dict()
    assert kpoints_temp['comment'] == 'No comment'
    points = kpoints_temp['points']
    np.testing.assert_allclose(points[0][0], np.array([0.0, 0.0, 0.0]))
    np.testing.assert_allclose(points[1][0], np.array([0.5, 0.5, 0.0]))
    np.testing.assert_allclose(points[2][0], np.array([0.5, 0.5, 0.0]))
    np.testing.assert_allclose(points[3][0], np.array([0.5, 0.5, 0.25]))
    np.testing.assert_allclose(points[4][0], np.array([0.5, 0.5, 0.25]))
    np.testing.assert_allclose(points[5][0], np.array([0.0, 0.0, 0.0]))
    assert math.isclose(points[0][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[1][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[2][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[3][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[4][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[5][1], 1.0, rel_tol=1e-07)
    assert points[0][2]
    assert points[1][2]
    assert points[2][2]
    assert points[3][2]
    assert points[4][2]
    assert points[5][2]
Пример #3
0
def test_kpoints_write_explicit(kpoints_parser_explicit, tmpdir):
    """Test read, write and read KPOINTS in explicit mode.

    """

    kpoints = kpoints_parser_explicit.get_dict()
    temp_file = str(tmpdir.join('KPOINTSEXP'))
    kpoints_parser_explicit.write(file_path=temp_file)
    kpoints_parser_explicit_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_explicit_temp.get_dict()
    assert kpoints_temp['mode'] == 'explicit'
    assert kpoints_temp['comment'] == 'Example file'
    assert kpoints_temp['divisions'] == None
    assert kpoints_temp['shifts'] == None
    assert kpoints_temp['centering'] == None
    points = kpoints_temp['points']
    assert len(points) == 4
    np.testing.assert_allclose(points[0][0], np.array([0.0, 0.0, 0.0]))
    np.testing.assert_allclose(points[1][0], np.array([0.0, 0.0, 0.5]))
    np.testing.assert_allclose(points[2][0], np.array([0.0, 0.5, 0.5]))
    np.testing.assert_allclose(points[3][0], np.array([0.5, 0.5, 0.5]))
    assert math.isclose(points[0][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[1][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[2][1], 2.0, rel_tol=1e-07)
    assert math.isclose(points[3][1], 4.0, rel_tol=1e-07)
    assert points[0][2]
    assert points[1][2]
    assert points[2][2]
    assert points[3][2]
    assert kpoints_temp['tetra'] == [[6, 1, 2, 3, 4]]
    assert math.isclose(kpoints_temp['tetra_volume'],
                        0.183333333333333,
                        rel_tol=1e-07)
Пример #4
0
def test_kpoints_dict(tmpdir):
    """Test to initialize KPOINTS in auto mode using dictionary.

    """

    kpoints_dict = {
        'comment': 'Example file',
        'divisions': [5, 5, 5],
        'mode': 'automatic',
        'shifts': None,
        'points': None,
        'centering': 'Gamma',
        'tetra': None,
        'tetra_volume': None,
        'num_kpoints': 0
    }
    temp_file = str(tmpdir.join('KPOINTS'))
    kpoints_parser_auto_temp = Kpoints(kpoints_dict=kpoints_dict)
    kpoints_temp = kpoints_parser_auto_temp.get_dict()
    assert kpoints_temp['mode'] == 'automatic'
    assert kpoints_temp['comment'] == 'Example file'
    assert kpoints_temp['divisions'] == [5, 5, 5]
    assert kpoints_temp['shifts'] == None
    assert kpoints_temp['points'] == None
    assert kpoints_temp['centering'] == 'Gamma'
    assert kpoints_temp['tetra'] == None
    assert kpoints_temp['tetra_volume'] == None
    assert kpoints_temp['num_kpoints'] == 0
Пример #5
0
def test_kpoints_modify_auto(kpoints_parser_auto, tmpdir):
    """Test read, modify, write and read KPOINTS in auto mode.

    """

    kpoints = kpoints_parser_auto.get_dict()
    assert kpoints['comment'] == 'Example file'
    assert kpoints['divisions'] == [4, 4, 4]
    kpoints_parser_auto.modify('comment', 'No comment')
    kpoints_parser_auto.modify('divisions', [5, 5, 5])
    temp_file = str(tmpdir.join('KPOINTS'))
    kpoints_parser_auto.write(file_path=temp_file)
    kpoints_parser_auto_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_auto_temp.get_dict()
    assert kpoints_temp['comment'] == 'No comment'
    assert kpoints_temp['divisions'] == [5, 5, 5]
Пример #6
0
    def _parse_file(self, inputs):
        """Create a DB Node from a KPOINTS file."""

        result = inputs
        result = {}

        if isinstance(self._data_obj, get_data_class('array.kpoints')):
            return {'kpoints-kpoints': self._data_obj}

        try:
            parsed_kpoints = Kpoints(file_path=self._data_obj.path,
                                     logger=self._logger)
        except SystemExit:
            self._logger.warning(
                'Parsevasp exitited abnormally. Returning None.')
            return {'kpoints-kpoints': None}

        if parsed_kpoints.entries.get('mode') == 'line':
            self._logger.warning(
                'The read KPOINTS contained line mode which is'
                'not supported. Returning None.')
            return {'kpoints-kpoints': None}
        result['kpoints-kpoints'] = parsed_kpoints.entries

        return result
Пример #7
0
    def _parse_file(self, inputs):
        """Create a DB Node from a KPOINTS file"""

        result = inputs
        result = {}

        if isinstance(self._data_obj, get_data_class('array.kpoints')):
            return {'kpoints-kpoints': self._data_obj}

        try:
            parsed_kpoints = Kpoints(file_path=self._data_obj.path)
        except SystemExit:
            self._logger.warning("Parsevasp exitited abnormally. "
                                 "Returning None.")
            return {'kpoints-kpoints': None}

        mode = parsed_kpoints.entries.get('mode')
        if mode == 'line':
            self._logger.warning(
                "The read KPOINTS contained line mode which is"
                "not supported. Returning None.")
            return {'kpoints-kpoints': None}
        result['kpoints-kpoints'] = getattr(self, '_get_kpointsdata_' + mode)(
            parsed_kpoints.entries)

        return result
Пример #8
0
    def _parsed_object(self):
        """
        Return an instance of parsevasp.Kpoints.

        Corresponds to the stored KpointsData.

        """

        # The KpointsData has not been successfully parsed yet. So let's parse it.
        if self._data_obj.get_attrs().get('mesh'):
            mode = 'automatic'
        elif self._data_obj.get_attrs().get('array|kpoints'):
            mode = 'explicit'

        kpoints_dict = {}
        for keyword in [
                'comment', 'divisions', 'shifts', 'points', 'tetra',
                'tetra_volume', 'mode', 'centering', 'num_kpoints'
        ]:
            kpoints_dict[keyword] = None

        kpoints_dict.update(
            getattr(self, '_get_kpointsdict_' + mode)(self._data_obj))

        try:
            return Kpoints(kpoints_dict=kpoints_dict)
        except SystemExit:
            return None
Пример #9
0
def test_kpoints_string(tmpdir):
    """Test to initialize KPOINTS in auto mode using string.

    """

    kpoints_str = '# Example file\n0\nG\n4 4 4\n'
    temp_file = str(tmpdir.join('KPOINTS'))
    kpoints_parser_auto_temp = Kpoints(kpoints_string=kpoints_str)
    kpoints_temp = kpoints_parser_auto_temp.get_dict()
    assert kpoints_temp['mode'] == 'automatic'
    assert kpoints_temp['comment'] == 'Example file'
    assert kpoints_temp['divisions'] == [4, 4, 4]
    assert kpoints_temp['shifts'] == None
    assert kpoints_temp['points'] == None
    assert kpoints_temp['centering'] == 'Gamma'
    assert kpoints_temp['tetra'] == None
    assert kpoints_temp['tetra_volume'] == None
    assert kpoints_temp['num_kpoints'] == 0
Пример #10
0
def kpoints_parser_line():
    """Load KPOINTS file.

    """

    testdir = os.path.dirname(__file__)
    kpointsfile = testdir + '/KPOINTSLINE'
    kpoints = Kpoints(file_path=kpointsfile)

    return kpoints
Пример #11
0
def kpoints_parser_explicit():
    """Load KPOINTS file.

    """

    testdir = os.path.dirname(__file__)
    kpointsfile = testdir + '/KPOINTSEXP'
    kpoints = Kpoints(file_path=kpointsfile)

    return kpoints
Пример #12
0
def kpoints_parser_auto():
    """Load KPOINTS file.

    """

    testdir = os.path.dirname(__file__)
    kpointsfile = testdir + "/KPOINTS"
    kpoints = Kpoints(file_path=kpointsfile)

    return kpoints
Пример #13
0
def test_kpoints_write_auto(kpoints_parser_auto, tmpdir):
    """Test read, write and read KPOINTS in auto mode.

    """

    kpoints = kpoints_parser_auto.get_dict()
    temp_file = str(tmpdir.join('KPOINTS'))
    kpoints_parser_auto.write(file_path=temp_file)
    kpoints_parser_auto_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_auto_temp.get_dict()
    assert kpoints_temp['mode'] == 'automatic'
    assert kpoints_temp['comment'] == 'Example file'
    assert kpoints_temp['divisions'] == [4, 4, 4]
    assert kpoints_temp['shifts'] == [0.0, 0.0, 0.0]
    assert kpoints_temp['points'] == None
    assert kpoints_temp['centering'] == 'Gamma'
    assert kpoints_temp['tetra'] == None
    assert kpoints_temp['tetra_volume'] == None
    assert kpoints_temp['num_kpoints'] == 0
Пример #14
0
def test_kpoints_modify_explicit(kpoints_parser_explicit, tmpdir):
    """Test read, modify, write and read KPOINTS in explicit mode.

    """

    kpoints = kpoints_parser_explicit.get_dict()
    assert kpoints['comment'] == 'Example file'
    points = kpoints['points']
    assert len(points) == 4
    np.testing.assert_allclose(points[0][0], np.array([0.0, 0.0, 0.0]))
    np.testing.assert_allclose(points[1][0], np.array([0.0, 0.0, 0.5]))
    np.testing.assert_allclose(points[2][0], np.array([0.0, 0.5, 0.5]))
    np.testing.assert_allclose(points[3][0], np.array([0.5, 0.5, 0.5]))
    assert math.isclose(points[0][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[1][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[2][1], 2.0, rel_tol=1e-07)
    assert math.isclose(points[3][1], 4.0, rel_tol=1e-07)
    assert points[0][2]
    assert points[1][2]
    assert points[2][2]
    assert points[3][2]
    kpoints_parser_explicit.modify('comment', 'Nada comment')
    point = Kpoint(np.array([0.0, 0.0, 0.0]), 1.0)
    kpoints_parser_explicit.modify('points', point, point_number=3)
    temp_file = str(tmpdir.join('KPOINTSEXP'))
    kpoints_parser_explicit.write(file_path=temp_file)
    kpoints_parser_explicit_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_explicit_temp.get_dict()
    assert kpoints_temp['comment'] == 'Nada comment'
    points = kpoints_temp['points']
    assert len(points) == 4
    np.testing.assert_allclose(points[0][0], np.array([0.0, 0.0, 0.0]))
    np.testing.assert_allclose(points[1][0], np.array([0.0, 0.0, 0.5]))
    np.testing.assert_allclose(points[2][0], np.array([0.0, 0.5, 0.5]))
    np.testing.assert_allclose(points[3][0], np.array([0.0, 0.0, 0.0]))
    assert math.isclose(points[0][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[1][1], 1.0, rel_tol=1e-07)
    assert math.isclose(points[2][1], 2.0, rel_tol=1e-07)
    assert math.isclose(points[3][1], 1.0, rel_tol=1e-07)
    assert points[0][2]
    assert points[1][2]
    assert points[2][2]
    assert points[3][2]
Пример #15
0
def kpoints_parser_auto_file_object():
    """Load KPOINTS file.

    """

    testdir = os.path.dirname(__file__)
    kpointsfile = testdir + '/KPOINTS'
    kpoints = None
    with open(kpointsfile) as file_handler:
        kpoints = Kpoints(file_handler=file_handler)

    return kpoints
Пример #16
0
    def _parsed_object(self):
        """
        Return an instance of parsevasp.Kpoints.

        Corresponds to the stored KpointsData.

        """

        if isinstance(self._data_obj, get_data_class('array.kpoints')):
            # The KpointsData has not been successfully parsed yet. So let's parse it.
            try:
                _ = self._data_obj.get_attribute('mesh')
                mode = 'automatic'
            except AttributeError:
                pass

            try:
                _ = self._data_obj.get_attribute('array|kpoints')
                mode = 'explicit'
            except AttributeError:
                pass

            kpoints_dict = {}
            for keyword in [
                    'comment', 'divisions', 'shifts', 'points', 'tetra',
                    'tetra_volume', 'mode', 'centering', 'num_kpoints'
            ]:
                kpoints_dict[keyword] = None

            kpoints_dict.update(
                getattr(self, '_get_kpointsdict_' + mode)(self._data_obj))

            try:
                return Kpoints(kpoints_dict=kpoints_dict, logger=self._logger)
            except SystemExit:
                return None

        # _data_obj is SingleFile:
        return self._data_obj
Пример #17
0
def test_kpoints_write_auto(kpoints_parser_auto, tmpdir):
    """Test read, write and read KPOINTS in auto mode.

    Here we also test that the write using file handler works properly.

    """

    kpoints = kpoints_parser_auto.get_dict()
    temp_file = str(tmpdir.join('KPOINTS'))
    kpoints_parser_auto.write(file_path=temp_file)
    kpoints_parser_auto_temp = Kpoints(file_path=temp_file)
    kpoints_temp = kpoints_parser_auto_temp.get_dict()
    verify_kpoints_content(kpoints_temp)
    with open(temp_file, 'w') as handler:
        kpoints_parser_auto.write(file_handler=handler)
    with open(temp_file, 'r') as handler:
        kpoints_parser_auto_temp = Kpoints(file_handler=handler)
    kpoints_temp = kpoints_parser_auto_temp.get_dict()
    verify_kpoints_content(kpoints_temp)