1from io import StringIO
2from pathlib import Path
3import numpy as np
4import pytest
5from ase.io import read
6from ase.io.siesta import read_struct_out, read_fdf
7from ase.units import Bohr
8
9
10sample_struct_out = """\
11  3.0   0.0   0.0
12 -1.5   4.0   0.0
13  0.0   0.0   5.0
14        2
15   1   45   0.0   0.0   0.0
16   1   46   0.3   0.4   0.5
17"""
18
19
20def test_read_struct_out():
21    atoms = read_struct_out(StringIO(sample_struct_out))
22    assert all(atoms.numbers == [45, 46])
23    assert atoms.get_scaled_positions() == pytest.approx(
24        np.array([[0., 0., 0.], [.3, .4, .5]]))
25    assert atoms.cell[:] == pytest.approx(np.array([[3.0, 0.0, 0.0],
26                                                    [-1.5, 4.0, 0.0],
27                                                    [0.0, 0.0, 5.0]]))
28    assert all(atoms.pbc)
29
30
31sample_fdf = """\
32potatoes 5
33COFFEE 6.5
34%block spam
35   1 2.5 hello
36%endblock spam
37"""
38
39
40def test_read_fdf():
41    dct = read_fdf(StringIO(sample_fdf))
42    # This is a "raw" parser, no type conversion is done.
43    ref = dict(potatoes=['5'],
44               coffee=['6.5'],
45               spam=[['1', '2.5', 'hello']])
46    assert dct == ref
47
48
49"""
50"Hand-written" dummy file based on HCP Ti
51"""
52xv_file = """\
53     5.6  0.0  0.0     0.0  0.0  0.0
54    -2.8  4.8  0.0     0.0  0.0  0.0
55     0.0  0.0  8.9     0.0  0.0  0.0
56      2
57  1    22  0.0  0.0  0.0     0.0  0.0  0.0
58  1    22  0.0  3.2  4.4     0.0  0.0  0.0
59"""
60
61
62def test_read_xv():
63    path = Path('tmp.XV')
64    path.write_text(xv_file)
65    atoms = read(path)
66
67    assert str(atoms.symbols) == 'Ti2'
68    pos = atoms.positions
69    assert pos[0] == pytest.approx(0)
70    assert pos[1] / Bohr == pytest.approx([0, 3.2, 4.4])
71    assert all(atoms.pbc)
72    assert atoms.cell / Bohr == pytest.approx(np.array(
73        [[5.6, 0, 0], [-2.8, 4.8, 0], [0, 0, 8.9]]
74    ))
75