File: test_main.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 (113 lines) | stat: -rw-r--r-- 4,077 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
import numpy as np
import pytest
from ase.cell import Cell
from ase.lattice import bravais_lattices, UnsupportedLattice
from ase.build import bulk, fcc111


def test_bravais_check():
    bravais = {}
    for name in bravais_lattices:
        bravais[name.lower()] = bravais_lattices[name]


    def check_single(name, cell, pbc=None):
        c = Cell(cell)

        try:
            print('TEST', c, pbc)
            if pbc[:2].all() or sum(pbc) == 1:
                lattice = c.get_bravais_lattice(pbc=pbc)
            else:
                with pytest.raises(UnsupportedLattice):
                    lattice = c.get_bravais_lattice(pbc=pbc)
                return
        except RuntimeError:
            print('error checking {}'.format(name))
            raise
        name1 = lattice.name.lower()
        latname = name.split('@')[0]
        ok = latname == name1
        print(name, '-->', name1, 'OK' if ok else 'ERR', c.cellpar())
        assert ok, 'Expected {} but found {}'.format(latname, name1)


    def check(name, cell, pbc=None):
        if pbc is None:
            pbc = cell.any(1)
        pbc = np.asarray(pbc)
        cell = Cell(cell)

        # Check all three positive permutations:
        check_single(name + '@012', cell[[0, 1, 2]], pbc=pbc[[0, 1, 2]])
        # 2D lattice determination only supports pbc=(1,1,0) and hence we
        # check the permutations only for 3D lattices:
        if cell.rank == 3 and pbc.sum() != 1:
            check_single(name + '@201', cell[[2, 0, 1]], pbc=pbc[[2, 0, 1]])
            check_single(name + '@120', cell[[1, 2, 0]], pbc=pbc[[1, 2, 0]])


    check('cub', bravais['cub'](3.3).tocell())
    check('fcc', bravais['fcc'](3.4).tocell())
    check('fcc', bulk('Au').cell)
    check('bcc', bravais['bcc'](3.5).tocell())
    check('bcc', bulk('Fe').cell)
    check('tet', bravais['tet'](4., 5.).tocell())
    check('tet', np.diag([4., 5., 5.]))
    check('tet', np.diag([5., 4., 5.]))
    check('tet', np.diag([5., 5., 4.]))
    check('bct', bravais['bct'](3., 4.).tocell())
    check('orc', bravais['orc'](3., 4., 5.).tocell())
    check('orcf', bravais['orcf'](4., 5., 7.).tocell())
    check('orci', bravais['orci'](2., 5., 6.).tocell())
    check('orcc', bravais['orcc'](3., 4., 5.).tocell())
    check('hex', fcc111('Au', size=(1, 1, 3), periodic=True).cell)
    check('hex', bravais['hex'](5., 6.).tocell())
    check('rhl', bravais['rhl'](4., 54.).tocell())
    check('mcl', bravais['mcl'](2., 3., 4., 62.).tocell())
    check('mclc', bravais['mclc'](3., 4., 5., 75.).tocell())
    check('tri', bravais['tri'](7., 6., 5., 65., 70., 80.).tocell())

    # For 2D materials we have to check both the tocell() method
    # but also for realistic cell nonzero nonperiodic axis.
    check('sqr', bravais['sqr'](3.).tocell())
    check('sqr', Cell(np.diag([3., 3., 10.])),
          pbc=np.array([True, True, False]))

    check('crect', bravais['crect'](3., 40).tocell())

    alpha = 40 / 360 * 2 * np.pi
    a = 3
    x = np.cos(alpha)
    y = np.sin(alpha)

    crectcell = np.array([[a, 0, 0],
                          [a * x, a * y, 0],
                          [0, 0, 10]])
    check('crect', Cell(crectcell), pbc=[1, 1, 0])

    check('rect', bravais['rect'](3., 4.).tocell())
    check('rect', Cell.new([3, 4, 10]), pbc=[1, 1, 0])

    check('hex2d', bravais['hex2d'](3.).tocell())
    x = 0.5 * np.sqrt(3)
    hexcell = np.array([[a, 0, 0],
                        [-0.5 * a, x * a, 0],
                        [0., 0., 0.]])
    check('hex2d', Cell(hexcell))

    check('obl', bravais['obl'](3., 4., 40).tocell())

    b = 4
    x = np.cos(alpha)
    y = np.sin(alpha)
    oblcell = np.array([[a, 0, 0],
                        [b * x, b * y, 0],
                        [0, 0, 10]])
    check('obl', Cell(oblcell), pbc=np.array([True, True, False]))

    # 1-d:
    check('line', Cell(np.diag([a, 0, 0.0])))
    check('line', Cell(np.diag([a, 1, 1.0])), pbc=np.array([1, 0, 0]))
    check('line', Cell(np.diag([0.0, 0, a])))
    check('line', Cell(np.diag([1.0, 1, a])), pbc=np.array([0, 0, 1]))