File: test_exciting.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (254 lines) | stat: -rw-r--r-- 10,071 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# fmt: off
"""Test file for exciting file input and output methods."""

import xml.etree.ElementTree as ET
from pathlib import Path

import numpy as np
import pytest

import ase
import ase.io.exciting

# Import a realistic looking exciting text output file as a string.
from ase.test.calculator.exciting.test_exciting import LDA_VWN_AR_INFO_OUT


@pytest.fixture()
def excitingtools():
    """If we cannot import excitingtools we skip tests with this fixture."""
    return pytest.importorskip('excitingtools')


@pytest.fixture()
def nitrogen_trioxide_atoms():
    """Helper fixture to create a NO3 ase atoms object for tests."""
    return ase.Atoms('NO3',
                     cell=[[2, 2, 0], [0, 4, 0], [0, 0, 6]],
                     positions=[(0, 0, 0), (1, 3, 0),
                                (0, 0, 1), (0.5, 0.5, 0.5)],
                     pbc=True)


def test_write_input_xml_file(
        tmp_path, nitrogen_trioxide_atoms, excitingtools):
    """Test writing input.xml file using write_input_xml_file()."""
    file_path = tmp_path / 'input.xml'
    ground_state_input_dict = {
        "rgkmax": 8.0,
        "do": "fromscratch",
        "ngridk": [6, 6, 6],
        "xctype": "GGA_PBE_SOL",
        "vkloff": [0, 0, 0],
        "tforce": True,
        "nosource": False
    }
    ase.io.exciting.write_input_xml_file(
        file_name=file_path,
        atoms=nitrogen_trioxide_atoms,
        ground_state_input=ground_state_input_dict,
        species_path="/dummy/arbitrary/path",
        title=None)
    assert file_path.exists()
    # Now read the XML file and ensure that it has what we expect:
    atoms_obj = ase.io.exciting.ase_atoms_from_exciting_input_xml(file_path)

    assert all(atoms_obj.symbols == "NOOO")
    input_xml_tree = ET.parse(file_path).getroot()
    parsed_calc_params = list(input_xml_tree)[2]
    assert parsed_calc_params.get("xctype") == "GGA_PBE_SOL"
    assert parsed_calc_params.get("rgkmax") == '8.0'
    assert parsed_calc_params.get("tforce") == 'true'


def test_write_bs_xml(
        tmp_path, nitrogen_trioxide_atoms, excitingtools):
    """Test writing input for bandstructure and skip ground state.

    The input.xml should contain a `do`=skip key in the groun state to avoid
    repeating the ground state. `do`=fromscratch can also be called if the
    ground state should be recalculated or was never done.

    The bandstructure is passed into the exciting xml as an additional property.
    We use excitingtools and pass the ase atoms object and the number of steps
    we want to run to get the bandstructure. Excitingtools in turn calls
    bandpath = cell.bandpath() on the ase atoms object cell (lattice vectors).
    This is done so that excitingtools is independent of ASE.

    """
    from excitingtools.input.bandstructure import (
        band_structure_input_from_ase_atoms_obj,
    )
    file_path = tmp_path / 'input.xml'
    ground_state_input_dict = {
        "rgkmax": 8.0,
        "do": "skip",
        "ngridk": [6, 6, 6],
        "xctype": "GGA_PBE_SOL",
        "vkloff": [0, 0, 0],
        "tforce": True,
        "nosource": False
    }
    bandstructure_steps = 100
    bandstructure = band_structure_input_from_ase_atoms_obj(
        nitrogen_trioxide_atoms, steps=bandstructure_steps)
    properties_input_dict = {'bandstructure': bandstructure}

    ase.io.exciting.write_input_xml_file(
        file_name=file_path,
        atoms=nitrogen_trioxide_atoms,
        ground_state_input=ground_state_input_dict,
        species_path="/dummy/arbitrary/path",
        title=None,
        properties_input=properties_input_dict)
    assert file_path.exists()
    # Now read the XML file and ensure that it has what we expect:
    atoms_obj = ase.io.exciting.ase_atoms_from_exciting_input_xml(file_path)

    assert all(atoms_obj.symbols == "NOOO")
    input_xml_tree = ET.parse(file_path).getroot()
    # Check ground state parameters.
    parsed_ground_state_calc_params = list(input_xml_tree)[2]
    assert parsed_ground_state_calc_params.get("xctype") == "GGA_PBE_SOL"
    assert parsed_ground_state_calc_params.get("rgkmax") == "8.0"
    assert parsed_ground_state_calc_params.get("tforce") == "true"
    assert parsed_ground_state_calc_params.get("do") == "skip"

    # Check additional properties which all relate to the bandstructure.
    parsed_properties = list(input_xml_tree)[3]
    assert parsed_properties.tag == "properties"
    assert len(list(parsed_properties)) == 1
    assert parsed_properties[0].tag == "bandstructure"
    assert parsed_properties[0][0].tag == "plot1d"
    parsed_bandstructure_path = parsed_properties[0][0][0]
    assert parsed_bandstructure_path.tag == "path"
    parsed_bandstructure_path.get("steps") == 100
    parsed_bandstructure_gamma_point = parsed_bandstructure_path[0]
    assert parsed_bandstructure_gamma_point.tag == "point"
    assert parsed_bandstructure_gamma_point.get("coord") == "0.0 0.0 0.0"


def test_write_dos_xml(
        tmp_path, nitrogen_trioxide_atoms, excitingtools):
    """Test creating required input to run a DOS calculation."""
    file_path = tmp_path / 'input.xml'
    ground_state_input_dict = {
        "rgkmax": 8.0,
        "do": "skip",
        "ngridk": [6, 6, 6],
        "xctype": "GGA_PBE_SOL",
        "vkloff": [0, 0, 0],
        "tforce": True,
        "nosource": False
    }
    nsmdos = 2
    ngrdos = 300
    nwdos = 1000
    winddos = [-0.3, 0.3]
    properties_input_dict = {'dos': {
        'nsmdos': nsmdos, 'ngrdos': ngrdos,
        'nwdos': nwdos, 'winddos': winddos}}
    ase.io.exciting.write_input_xml_file(
        file_name=file_path,
        atoms=nitrogen_trioxide_atoms,
        ground_state_input=ground_state_input_dict,
        species_path="/dummy/arbitrary/path",
        title=None,
        properties_input=properties_input_dict)
    assert file_path.exists()
    # Now read the XML file and ensure that it has what we expect:
    atoms_obj = ase.io.exciting.ase_atoms_from_exciting_input_xml(file_path)
    assert all(atoms_obj.symbols == "NOOO")
    input_xml_tree = ET.parse(file_path).getroot()
    # Check ground state parameters.
    parsed_ground_state_calc_params = list(input_xml_tree)[2]
    assert parsed_ground_state_calc_params.get("do") == "skip"
    # Check additional properties which all relate to the bandstructure.
    parsed_properties = list(input_xml_tree)[3]
    assert parsed_properties.tag == "properties"
    assert len(list(parsed_properties)) == 1
    assert len(list(parsed_properties)) == 1
    assert parsed_properties[0].tag == "dos"
    assert parsed_properties[0].get('nsmdos') == str(nsmdos)
    assert parsed_properties[0].get('ngrdos') == str(ngrdos)


def test_ase_atoms_from_exciting_input_xml(
        tmp_path, nitrogen_trioxide_atoms, excitingtools):
    """Test reading the of the exciting input.xml file into ASE atoms obj."""
    expected_cell = np.array([[2, 2, 0], [0, 4, 0], [0, 0, 6]])
    expected_positions = np.array([(0, 0, 0), (1, 3, 0), (0, 0, 1),
                                   (0.5, 0.5, 0.5)])
    # First we write an input.xml file into a temp dir, so we can
    # read it back with our method we put under test.
    file_path = tmp_path / 'input.xml'
    ground_state_input_dict = {
        "rgkmax": 8.0,
        "do": "fromscratch",
        "ngridk": [6, 6, 6],
        "xctype": "GGA_PBE_SOL",
        "vkloff": [0, 0, 0],
        "tforce": True,
        "nosource": False
    }
    ase.io.exciting.write_input_xml_file(
        file_name=file_path,
        atoms=nitrogen_trioxide_atoms,
        ground_state_input=ground_state_input_dict,
        species_path="/dummy/arbitrary/path",
        title=None)
    atoms_obj = ase.io.exciting.ase_atoms_from_exciting_input_xml(file_path)

    assert all(atoms_obj.symbols == "NOOO")
    assert atoms_obj.cell.array == pytest.approx(expected_cell)
    assert atoms_obj.positions == pytest.approx(expected_positions)


def test_parse_info_out_xml_bad_path(tmp_path, excitingtools):
    """Tests parse method raises error when info.out file doesn't exist."""
    output_file_path = Path(tmp_path).joinpath('info.out')

    with pytest.raises(FileNotFoundError):
        ase.io.exciting.parse_output(
            output_file_path)


def test_parse_info_out_energy(tmp_path, excitingtools):
    """Test parsing the INFO.OUT output from exciting using parse_output()."""
    expected_lattice_cell = [
        [10.3360193975, 10.3426010725, 0.0054547264],
        [-10.3461511392, 10.3527307290, 0.0059928210],
        [10.3354645037, 10.3540072605, 20.6246241525]]

    file = tmp_path / "INFO.OUT"
    file.write_text(LDA_VWN_AR_INFO_OUT)
    assert file.exists(), "INFO.OUT written to tmp_path"

    results = ase.io.exciting.parse_output(file)
    initialization = results['initialization']

    # Finally ensure that we that the final SCL cycle is what we expect and
    # the final SCL results can be accessed correctly:
    final_scl_iteration = list(results["scl"].keys())[-1]
    assert pytest.approx(
        float(results["scl"][final_scl_iteration][
            "Hartree energy"])) == 205.65454603
    assert pytest.approx(
        float(results["scl"][final_scl_iteration][
            "Estimated fundamental gap"])) == 0.36095838
    assert pytest.approx(float(results["scl"][
        final_scl_iteration]["Hartree energy"])) == 205.65454603
    assert pytest.approx(float(
        initialization['Unit cell volume'])) == 4412.7512103067

    # This used to be '1' (str) in version 'nitrogen' but is 1 (int)
    # as of version 'neon':
    assert int(initialization['Total number of k-points']) == 1
    assert int(initialization['Maximum number of plane-waves']) == 251

    # Grab the lattice vectors. excitingtools parses them in a fortran like
    # vector. We reshape accordingly into a 3x3 matrix where rows correspond
    # to lattice vectors.
    lattice_vectors_as_matrix = np.array(np.reshape(
        initialization['Lattice vectors (cartesian)'], (3, 3), 'F'), float)
    assert lattice_vectors_as_matrix.tolist() == expected_lattice_cell