File: test_mustem.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 (150 lines) | stat: -rw-r--r-- 5,725 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
from distutils.version import LooseVersion
import numpy as np
import pytest

from ase import Atoms
from ase.io import read
from ase.build import bulk
from ase.atoms import symbols2numbers


pytestmark = pytest.mark.skipif(LooseVersion(np.__version__) <
                                LooseVersion("1.14"),
                                reason="This test requires numpy >= 1.14")


def make_STO_atoms():
    atoms = Atoms(['O', 'O', 'O', 'Sr', 'Ti'],
                  scaled_positions=[[0.5, 0.5, 0],
                                    [0.5, 0, 0.5],
                                    [0, 0.5, 0.5],
                                    [0, 0, 0],
                                    [0.5, 0.5, 0.5]],
                  cell=[3.905, 3.905, 3.905],
                  pbc=True)

    return atoms


def test_mustem_several_elements():
    """Check writing and reading a xtl mustem file."""
    # Reproduce the sto xtl file distributed with muSTEM
    atoms = make_STO_atoms()

    filename = 'sto_mustem.xtl'
    STO_DW_dict = {'Sr': 0.62, 'O': 0.73, 'Ti': 0.43}
    STO_DW_dict_Ti_missing = {key: STO_DW_dict[key] for key in ['Sr', 'O']}

    with pytest.raises(TypeError):
        atoms.write(filename)

    with pytest.raises(ValueError):
        atoms.write(filename, keV=300)

    with pytest.raises(TypeError):
        atoms.write(filename,
                    debye_waller_factors=STO_DW_dict)

    atoms.write(filename, keV=300,
                debye_waller_factors=STO_DW_dict)

    atoms2 = read(filename, format='mustem')
    atoms3 = read(filename)

    for _atoms in [atoms2, atoms3]:
        assert atoms.positions == pytest.approx(_atoms.positions)
        np.testing.assert_allclose(atoms.cell, _atoms.cell)

    with pytest.raises(ValueError):
        # Raise an error if there is a missing key.
        atoms.write(filename, keV=300,
                    debye_waller_factors=STO_DW_dict_Ti_missing)

    atoms.write(filename, keV=300,
                debye_waller_factors=STO_DW_dict,
                occupancies={'Sr': 1.0, 'O': 0.5, 'Ti': 0.9})

    with pytest.raises(ValueError):
        # Raise an error if there is a missing key.
        atoms.write(filename, keV=300,
                    debye_waller_factors=STO_DW_dict,
                    occupancies={'O': 0.5, 'Ti': 0.9})

    with pytest.raises(ValueError):
        # Raise an error if the unit cell is not defined.
        atoms4 = Atoms(['Sr', 'Ti', 'O', 'O', 'O'],
                       positions=[[0, 0, 0],
                                  [0.5, 0.5, 0.5],
                                  [0.5, 0.5, 0],
                                  [0.5, 0, 0.5],
                                  [0, 0.5, 0.5]])
        atoms4.write(filename, keV=300,
                     debye_waller_factors=STO_DW_dict)

    atoms5 = make_STO_atoms()
    atoms5.set_array('occupancies', np.ones(5))
    atoms5.arrays['occupancies'][atoms5.numbers == symbols2numbers('Sr')] = 0.9
    # element 0 is Sr and there is onlye one Sr in the cell: this is a valid
    # cell to export to xtl file
    atoms5.write(filename, keV=300, debye_waller_factors=STO_DW_dict)
    atoms6 = read(filename)
    condition = atoms6.numbers == symbols2numbers('Sr')
    np.testing.assert_allclose(atoms6.arrays['occupancies'][condition], 0.9)

    atoms5.arrays['occupancies'][0] = 0.8
    with pytest.raises(ValueError):
        atoms5.write(filename, keV=300, debye_waller_factors=STO_DW_dict)

    atoms7 = make_STO_atoms()
    debye_waller_factors = np.array([0.73, 0.73, 0.73, 0.62, 0.43])
    atoms7.set_array('debye_waller_factors', debye_waller_factors)
    # element 0 is Sr and there is onlye one Sr in the cell: this is a valid
    # cell to export to xtl file
    atoms7.write(filename, keV=300)
    atoms8 = read(filename)
    for element in ['Sr', 'Ti', 'O']:
        number = symbols2numbers(element)
        np.testing.assert_allclose(
            atoms7.arrays['debye_waller_factors'][atoms7.numbers == number],
            atoms8.arrays['debye_waller_factors'][atoms8.numbers == number],
            rtol=1e-2
        )


def test_mustem_single_elements():
    # Setting Debye-Waller factor as float.
    Si_atoms = bulk('Si', cubic=True)

    filename = 'Si100.xtl'
    DW = 0.62
    Si_atoms.write(filename, keV=300, debye_waller_factors=DW)
    Si_atoms2 = read(filename)

    np.testing.assert_allclose(Si_atoms.positions, Si_atoms2.positions)
    np.testing.assert_allclose(Si_atoms.cell, Si_atoms2.cell)
    np.testing.assert_allclose(Si_atoms2.arrays['occupancies'], np.ones(8))
    np.testing.assert_allclose(Si_atoms2.arrays['debye_waller_factors'],
                               np.ones(8) * DW, rtol=1e-2)

    Si_atoms3 = bulk('Si', cubic=True)
    Si_atoms3.set_array('occupancies', np.ones(8) * 0.9)
    Si_atoms3.set_array('debye_waller_factors', np.ones(8) * DW)
    Si_atoms3.write(filename, keV=300)

    Si_atoms4 = read(filename)
    np.testing.assert_allclose(Si_atoms3.positions, Si_atoms4.positions)
    np.testing.assert_allclose(Si_atoms3.cell, Si_atoms4.cell)
    np.testing.assert_allclose(Si_atoms3.arrays['occupancies'],
                               Si_atoms4.arrays['occupancies'])
    np.testing.assert_allclose(Si_atoms3.arrays['debye_waller_factors'],
                               Si_atoms4.arrays['debye_waller_factors'],
                               rtol=1e-2)

    Si_atoms5 = bulk('Si', cubic=True)
    debye_waller_factors = np.ones(8) * DW
    debye_waller_factors[0] = debye_waller_factors[0] / 2
    Si_atoms5.set_array('debye_waller_factors', debye_waller_factors)

    with pytest.raises(ValueError):
        # Raise an error if one Debye-Waller factor is different.
        Si_atoms5.write(filename, keV=300)