File: test_read_vasp_xml.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 (411 lines) | stat: -rw-r--r-- 14,847 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# fmt: off
from collections import OrderedDict
from io import StringIO
from pathlib import Path

import numpy as np
import pytest

from ase.constraints import FixAtoms, FixScaled
from ase.io import read
from ase.units import GPa

parent = Path(__file__).parents[2]


def test_parse_dfpt_dielectric(testdir):
    outfile = parent / "testdata/vasp/vasprun_dfpt.xml"
    atoms = read(outfile, format="vasp-xml")

    diel = atoms.calc.results['dielectric_tensor']

    diel_0 = np.diag(3 * [2.6958435, ])
    assert np.allclose(diel, diel_0)

    bec = atoms.calc.results['born_effective_charges']

    _bec = np.diag(3 * [1.14672091, ])

    bec_0 = np.array([_bec, -_bec])
    assert np.allclose(bec, bec_0)


@pytest.fixture()
def vasprun():
    # "Hand-written" (reduced) vasprun.xml
    sample_vasprun = """\
<?xml version="1.0" encoding="ISO-8859-1"?>
<modeling>
 <structure name="primitive_cell" >
  <crystal>
   <varray name="basis" >
    <v>       3.16000000       0.00000000       0.00000000 </v>
    <v>       0.00000000       3.16000000       0.00000000 </v>
    <v>       0.00000000       0.00000000       3.16000000 </v>
   </varray>
  </crystal>
  <varray name="positions" >
   <v>       0.00000000       0.00000000       0.00000000 </v>
   <v>       0.50000000       0.50000000       0.50000000 </v>
  </varray>
 </structure>
 <kpoints>
  <varray name="kpointlist" >
   <v>       0.00000000       0.00000000       0.00000000 </v>
  </varray>
 </kpoints>
 <atominfo>
  <atoms>       2 </atoms>
  <types>       1 </types>
  <array name="atoms" >
   <set>
    <rc><c>W </c><c>   1</c></rc>
    <rc><c>W </c><c>   1</c></rc>
   </set>
  </array>
 </atominfo>
 <structure name="initialpos" >
  <crystal>
   <varray name="basis" >
    <v>       3.16000000       0.00000000       0.00000000 </v>
    <v>       0.00000000       3.16000000       0.00000000 </v>
    <v>       0.00000000       0.00000000       3.16000000 </v>
   </varray>
   <i name="volume">     31.55449600 </i>
  </crystal>
  <varray name="positions" >
   <v>       0.00000000       0.00000000       0.00000000 </v>
   <v>       0.50000000       0.50000000       0.50000000 </v>
  </varray>
 </structure>
"""
    return sample_vasprun


@pytest.fixture()
def calculation():
    def factory(test_case_index=0):
        # list of dictionaries with the expected values for
        # test calculation xml strings
        expected_values = [{"e_0_energy": -29.67691672,
                            "e_fr_energy": -29.67243317,
                            "forces": np.array([[7.58587457,
                                                 -5.22590317,
                                                 6.88227285],
                                                [-7.58587457,
                                                 5.22590317,
                                                 -6.88227285]]),
                            "stress": np.array([[4300.36902090,
                                                 -284.50040544,
                                                 -1468.20603140],
                                                [-284.50040595,
                                                 4824.17435683,
                                                 -1625.37541639],
                                                [-1468.20603158,
                                                 -1625.37541697,
                                                 5726.84189498]])},
                           {"e_0_energy": -2.0,
                            "e_fr_energy": -3.0,
                            "forces": np.full((2, 3), np.pi),
                            "stress": np.full((3, 3), 2.0 * np.pi)}
                           ]

        e_fr_energy = expected_values[test_case_index]["e_fr_energy"]
        e_0_energy = expected_values[test_case_index]["e_0_energy"]
        forces = expected_values[test_case_index]["forces"]
        stress = expected_values[test_case_index]["stress"]

        # "Hand-written" calculation record
        sample_calculation = f"""\
 <calculation>
  <scstep>
   <energy>
    <i name="e_fr_energy">     32.61376955 </i>
    <i name="e_wo_entrp">     32.60797165 </i>
    <i name="e_0_energy">     32.61183692 </i>
   </energy>
  </scstep>
  <scstep>
   <energy>
    <i name="e_fr_energy">   {e_fr_energy:.8f} </i>
    <i name="e_wo_entrp">    -29.68588381 </i>
    <i name="e_0_energy">    {e_0_energy:.8f}   </i>
   </energy>
  </scstep>
  <structure>
   <crystal>
    <varray name="basis" >
     <v>       3.16000000       0.00000000       0.00000000 </v>
     <v>       0.00000000       3.16000000       0.00000000 </v>
     <v>       0.00000000       0.00000000       3.16000000 </v>
    </varray>
    <i name="volume">     31.55449600 </i>
    <varray name="rec_basis" >
     <v>       0.31645570       0.00000000       0.00000000 </v>
     <v>       0.00000000       0.31645570       0.00000000 </v>
     <v>       0.00000000       0.00000000       0.31645570 </v>
    </varray>
   </crystal>
   <varray name="positions" >
    <v>       0.00000000       0.00000000       0.00000000 </v>
    <v>       0.50000000       0.50000000       0.50000000 </v>
   </varray>
  </structure>
  <varray name="forces" >
   <v>      {forces[0, 0]:.8f} {forces[0, 1]:.8f} {forces[0, 2]:.8f} </v>
   <v>      {forces[1, 0]:.8f} {forces[1, 1]:.8f} {forces[1, 2]:.8f} </v>
  </varray>
  <varray name="stress" >
   <v>    {stress[0, 0]:.8f} {stress[0, 1]:.8f} {stress[0, 2]:.8f}  </v>
   <v>    {stress[1, 0]:.8f} {stress[1, 1]:.8f} {stress[1, 2]:.8f} </v>
   <v>    {stress[2, 0]:.8f} {stress[2, 1]:.8f} {stress[2, 2]:.8f} </v>
  </varray>
  <energy>
   <i name="e_fr_energy">   {e_fr_energy:.8f} </i>
   <i name="e_wo_entrp">    -29.68588381 </i>
   <i name="e_0_energy">    {e_0_energy:.8f} </i>
  </energy>
"""
        return sample_calculation, expected_values[test_case_index]

    return factory


def test_atoms(vasprun):

    atoms = read(StringIO(vasprun), index=-1, format='vasp-xml')

    # check number of atoms
    assert len(atoms) == 2

    # make sure it is still tungsten
    assert all(np.array(atoms.get_chemical_symbols()) == "W")

    # check scaled_positions
    expected_scaled_positions = np.array([[0.0, 0.0, 0.0],
                                          [0.5, 0.5, 0.5]])

    np.testing.assert_allclose(atoms.get_scaled_positions(),
                               expected_scaled_positions)

    expected_cell = np.array([[3.16, 0.0, 0.0],
                              [0.0, 3.16, 0.0],
                              [0.0, 0.0, 3.16]])

    # check cell
    np.testing.assert_allclose(atoms.cell, expected_cell)

    # check real positions
    np.testing.assert_allclose(atoms.positions,
                               expected_scaled_positions @
                               atoms.cell.complete())


def check_calculation(vasprun_record, expected_values, index=-1):

    atoms = read(StringIO(vasprun_record), index=index,
                 format='vasp-xml')

    assert atoms.get_potential_energy() == \
        pytest.approx(expected_values["e_0_energy"])

    assert (atoms.get_potential_energy(force_consistent=True) ==
            pytest.approx(expected_values["e_fr_energy"]))

    np.testing.assert_allclose(atoms.get_forces(),
                               expected_values["forces"])

    assertion_stress = -0.1 * GPa * expected_values["stress"]
    assertion_stress = assertion_stress.reshape(9)[[0, 4, 8, 5, 2, 1]]

    np.testing.assert_allclose(atoms.get_stress(), assertion_stress)


def test_calculation(vasprun, calculation):

    calculation_record, expected_values = calculation(test_case_index=0)
    check_calculation(vasprun + calculation_record, expected_values)


def test_two_calculations(vasprun, calculation):

    (first_calculation_record,
     first_expected_values) = calculation(test_case_index=0)

    (second_calculation_record,
     second_expected_values) = calculation(test_case_index=1)

    extended_vasprun = (vasprun +
                        first_calculation_record + second_calculation_record)
    # make sure we have two atoms objects in the list if we read all records
    images = read(StringIO(extended_vasprun), index=':', format="vasp-xml")
    assert len(images) == 2
    check_calculation(extended_vasprun, second_expected_values)

    # make sure we can read the first (second from the end)
    # calculation by passing index=-2
    check_calculation(extended_vasprun, first_expected_values,
                      index=-2)


def test_corrupted_calculation(vasprun, calculation):

    (first_calculation_record,
     first_expected_values) = calculation(test_case_index=0)

    second_calculation_record, _ = calculation(test_case_index=1)

    # corrupted calculation that does not have energy record.
    # Thus the parser is expected to read the previous one.
    corrupted_record = '\n'.join(second_calculation_record.split('\n')[:-6])
    # assert that we actually do have two calculations in the set up
    xml_string = vasprun + first_calculation_record + corrupted_record
    images = read(StringIO(xml_string), index=':', format="vasp-xml")
    assert len(images) == 1
    check_calculation(vasprun + first_calculation_record + corrupted_record,
                      expected_values=first_expected_values,
                      index=-2)
    # check that the parser skips the corrupted last one
    # Should there be a warning in this case?
    check_calculation(vasprun + first_calculation_record + corrupted_record,
                      expected_values=first_expected_values,
                      index=-1)


def test_vasp_parameters(vasprun, calculation):

    vasp_parameters = """\
 <kpoints>
  <generation param="Monkhorst-Pack">
   <v type="int" name="divisions">       1        1        1 </v>
   <v name="usershift">      0.00000000       0.00000000       0.00000000 </v>
   <v name="genvec1">      1.00000000       0.00000000       0.00000000 </v>
   <v name="genvec2">      0.00000000       1.00000000       0.00000000 </v>
   <v name="genvec3">      0.00000000       0.00000000       1.00000000 </v>
   <v name="shift">      0.00000000       0.00000000       0.00000000 </v>
  </generation>
  <varray name="kpointlist" >
   <v>       0.00000000       0.00000000       0.00000000 </v>
  </varray>
  <varray name="weights" >
   <v>       1.00000000 </v>
  </varray>
 </kpoints>
<parameters>
  <separator name="electronic" >
   <i type="string" name="PREC">medium</i>
   <i name="ENMAX">    500.00000000</i>
   <i name="ENAUG">    373.43800000</i>
   <i name="EDIFF">      1.00000000</i>
   <separator name="electronic smearing" >
    <i type="int" name="ISMEAR">     1</i>
    <i name="SIGMA">      0.10000000</i>
    <i name="KSPACING">      0.50000000</i>
   </separator>
   <separator name="electronic startup" >
    <i type="int" name="ISTART">     0</i>
    <i type="int" name="ICHARG">     2</i>
    <i type="int" name="INIWAV">     1</i>
   </separator>
   <separator name="electronic exchange-correlation" >
    <i type="logical" name="LASPH"> F  </i>
    <i type="logical" name="LMETAGGA"> F  </i>
   </separator>
  </separator>
  <separator name="ionic" >
   <i type="int" name="NSW"> 10000</i>
   <i type="int" name="IBRION">    11</i>
   <i name="EDIFFG">     10.00000000</i>
  </separator>
  <separator name="symmetry" >
   <i type="int" name="ISYM">     0</i>
   <i name="SYMPREC">      0.00001000</i>
  </separator>
 </parameters>
    """
    calculation_record, _ = calculation()
    atoms = read(StringIO(vasprun + calculation_record + vasp_parameters),
                 index=-1, format="vasp-xml")

    expected_parameters = \
        OrderedDict([('kpoints_generation',
                      OrderedDict([('divisions', [1, 1, 1]),
                                   ('usershift', [0.0, 0.0, 0.0]),
                                   ('genvec1', [1.0, 0.0, 0.0]),
                                   ('genvec2', [0.0, 1.0, 0.0]),
                                   ('genvec3', [0.0, 0.0, 1.0]),
                                   ('shift', [0.0, 0.0, 0.0])])),
                     ('prec', 'medium'), ('enmax', 500.0),
                     ('enaug', 373.438), ('ediff', 1.0),
                     ('ismear', 1), ('sigma', 0.1),
                     ('kspacing', 0.5), ('istart', 0),
                     ('icharg', 2), ('iniwav', 1),
                     ('lasph', False), ('lmetagga', False),
                     ('nsw', 10000), ('ibrion', 11), ('ediffg', 10.0),
                     ('isym', 0), ('symprec', 1e-05)])

    assert atoms.calc.parameters == expected_parameters


def test_constraints(vasprun):
    """Test if constraints are parsed correctly."""
    selective = [
        '  <varray name="selective"  type="logical" >\n',
        '   <v type="logical" >  T  T  F </v>\n',
        '   <v type="logical" >  F  F  F </v>\n',
        '  </varray>\n',
    ]

    # insert the selective block before the last </structure>
    tmp = vasprun.splitlines(keepends=True)
    tmp[-1:-1] = selective
    vasprun = ''.join(tmp)

    atoms = read(StringIO(vasprun), index=-1, format='vasp-xml')

    assert isinstance(atoms.constraints[0], FixScaled)
    assert np.all(atoms.constraints[0].index == [0])
    assert np.all(atoms.constraints[0].mask == [False, False, True])

    assert isinstance(atoms.constraints[1], FixAtoms)
    assert np.all(atoms.constraints[1].index == [1])


def test_vasprun_line_mode(vasprun):
    line_mode = """\
 <kpoints>
  <generation param="listgenerated">
   <i name="divisions" type="int">       2 </i>
   <v>       0.00000000       0.00000000       0.00000000 </v>
   <v>       0.50000000       0.50000000       0.00000000 </v>
   <v>       0.50000000       0.75000000       0.25000000 </v>
   <v>       0.00000000       0.00000000       0.00000000 </v>
  </generation>
  <varray name="kpointlist" >
   <v>       0.00000000       0.00000000       0.00000000 </v>
   <v>       0.50000000       0.50000000       0.00000000 </v>
   <v>       0.50000000       0.50000000       0.00000000 </v>
   <v>       0.50000000       0.75000000       0.25000000 </v>
   <v>       0.50000000       0.75000000       0.25000000 </v>
   <v>       0.00000000       0.00000000       0.00000000 </v>
  </varray>
  <varray name="weights" >
   <v>       0.16666667 </v>
   <v>       0.16666667 </v>
   <v>       0.16666667 </v>
   <v>       0.16666667 </v>
   <v>       0.16666667 </v>
   <v>       0.16666667 </v>
  </varray>
  <kpoints_labels>
   <i name="Γ" type="int">       1 </i>
   <i name="X" type="int">       2 </i>
   <i name="X" type="int">       3 </i>
   <i name="W" type="int">       4 </i>
   <i name="W" type="int">       5 </i>
   <i name="Γ" type="int">       6 </i>
  </kpoints_labels>
 </kpoints>
"""
    assert read(StringIO(vasprun + line_mode),
                format="vasp-xml")