File: test_vasp_charge.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (115 lines) | stat: -rw-r--r-- 3,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import pytest
from ase.build import bulk

calc = pytest.mark.calculator


@pytest.fixture
def system():
    return bulk('Al', 'fcc', a=4.5, cubic=True)


@pytest.fixture
def expected_nelect_from_vasp():
    # Expected number of electrons from the specified system
    # with no charge
    return 12


@calc('vasp')
def test_vasp_charge(factory, system, expected_nelect_from_vasp):
    """
    Run VASP tests to ensure that determining number of electrons from
    user-supplied charge works correctly.

    Test that the number of charge found matches the expected.
    """

    # Dummy calculation to let VASP determine default number of electrons
    calc = factory.calc(xc='LDA',
                        nsw=-1,
                        ibrion=-1,
                        nelm=1,
                        lwave=False,
                        lcharg=False)
    system.calc = calc
    system.get_potential_energy()

    default_nelect_from_vasp = calc.get_number_of_electrons()
    assert default_nelect_from_vasp == expected_nelect_from_vasp


@calc('vasp')
def test_vasp_no_inputs(system, factory):
    # Make sure that no nelect was written into INCAR yet (as it wasn't necessary)
    calc = factory.calc()
    system.calc = calc
    system.get_potential_energy()
    calc.read_incar()
    assert calc.float_params['nelect'] is None


@calc('vasp')
def test_vasp_minus_charge(factory, system, expected_nelect_from_vasp):
    # Compare VASP's output nelect from before minus charge to default nelect
    # determined by us minus charge
    charge = -2
    calc = factory.calc(xc='LDA',
                        nsw=-1,
                        ibrion=-1,
                        nelm=1,
                        lwave=False,
                        lcharg=False,
                        charge=charge)
    calc.initialize(system)
    calc.write_input(system)
    calc.read_incar()
    assert calc.float_params['nelect'] == expected_nelect_from_vasp - charge


@calc('vasp')
def test_vasp_nelect_charge_conflict(factory, system,
                                     expected_nelect_from_vasp):
    # Test that conflicts between explicitly given nelect and charge are detected
    charge = -2
    calc = factory.calc(xc='LDA',
                        nsw=-1,
                        ibrion=-1,
                        nelm=1,
                        lwave=False,
                        lcharg=False,
                        nelect=expected_nelect_from_vasp - charge + 1,
                        charge=charge)
    system.calc = calc
    with pytest.raises(ValueError):
        system.get_potential_energy()


@calc('vasp')
def test_vasp_nelect_no_write(factory, system):
    # Test that nothing is written if charge is 0 and nelect not given
    calc = factory.calc(xc='LDA',
                        nsw=-1,
                        ibrion=-1,
                        nelm=1,
                        lwave=False,
                        lcharg=False,
                        charge=0)
    calc.initialize(system)
    calc.write_input(system)
    calc.read_incar()
    assert calc.float_params['nelect'] is None


@calc('vasp')
def test_vasp_nelect(factory, system):
    # Test that explicitly given nelect still works as expected
    calc = factory.calc(xc='LDA',
                        nsw=-1,
                        ibrion=-1,
                        nelm=1,
                        lwave=False,
                        lcharg=False,
                        nelect=15)
    calc.calculate(system)
    assert calc.get_number_of_electrons() == 15