File: test_vasp_net_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 (93 lines) | stat: -rw-r--r-- 3,477 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
import pytest
from ase.build import bulk

calc = pytest.mark.calculator


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


@calc('vasp')
def test_vasp_net_charge(factory, system):
    """
    Run VASP tests to ensure that determining number of electrons from
    user-supplied net charge (via the deprecated net_charge parameter) works
    correctly. This is conditional on the existence of the VASP_COMMAND or
    VASP_SCRIPT environment variables.

    This is mainly a slightly reduced duplicate of the vasp_charge test, but with
    flipped signs and with checks that ensure FutureWarning is emitted.

    Should be removed along with the net_charge parameter itself at some point.
    """

    # 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)
    calc.calculate(system)
    default_nelect_from_vasp = calc.get_number_of_electrons()
    assert default_nelect_from_vasp == 12

    # Compare VASP's output nelect from before + net charge to default nelect
    # determined by us + net charge
    with pytest.warns(FutureWarning):
        net_charge = -2
        calc = factory.calc(xc='LDA',
                            nsw=-1,
                            ibrion=-1,
                            nelm=1,
                            lwave=False,
                            lcharg=False,
                            net_charge=net_charge)
        calc.initialize(system)
        calc.write_input(system)
        calc.read_incar()
    assert calc.float_params['nelect'] == default_nelect_from_vasp + net_charge

    # Test that conflicts between explicitly given nelect and net charge are
    # detected
    with pytest.raises(ValueError):
        with pytest.warns(FutureWarning):
            calc = factory.calc(xc='LDA',
                                nsw=-1,
                                ibrion=-1,
                                nelm=1,
                                lwave=False,
                                lcharg=False,
                                nelect=default_nelect_from_vasp + net_charge +
                                1,
                                net_charge=net_charge)
            calc.calculate(system)

    # Test that conflicts between charge and net_charge are detected
    with pytest.raises(ValueError):
        with pytest.warns(FutureWarning):
            calc = factory.calc(xc='LDA',
                                nsw=-1,
                                ibrion=-1,
                                nelm=1,
                                lwave=False,
                                lcharg=False,
                                charge=-net_charge - 1,
                                net_charge=net_charge)
            calc.calculate(system)

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