File: test_supercell.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 (76 lines) | stat: -rw-r--r-- 1,930 bytes parent folder | download | duplicates (2)
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
import pytest
import numpy as np
from ase.build import bulk, make_supercell
from ase.lattice import FCC, BCC
from ase.calculators.emt import EMT


a = 4.1


@pytest.fixture
def atoms():
    atoms = bulk('Au', a=a)
    return atoms


def test_supercell(atoms):
    assert atoms.cell.get_bravais_lattice().name == 'FCC'

    # Since FCC and BCC are reciprocal, their product is cubic:
    P = BCC(2.0).tocell()
    assert np.allclose(np.linalg.det(P), 4)

    cubatoms = make_supercell(atoms, P)
    assert np.allclose(cubatoms.cell, a * np.eye(3))

    # Also test some of the Cell object methods now that we are at it:
    assert len(cubatoms) == 4
    assert cubatoms.cell.orthorhombic
    assert np.allclose(cubatoms.cell.lengths(), a)
    assert cubatoms.cell.get_bravais_lattice().name == 'CUB'


def test_bcc_to_cub_transformation():
    bcc = bulk('Fe', a=a)
    P = FCC(2.0).tocell()
    assert np.allclose(np.linalg.det(P), 2)
    cubatoms = make_supercell(bcc, P)
    assert np.allclose(cubatoms.cell, a * np.eye(3))


def getenergy(atoms, eref=None):
    atoms.calc = EMT()
    e = atoms.get_potential_energy() / len(atoms)
    if eref is not None:
        err = abs(e - eref)
        print('natoms', len(atoms), 'err', err)
        assert err < 1e-12, err
    return e


def test_random_transformations(atoms):
    # Finally we want to check some random transformations.
    # This will produce some weird supercells, all of which should have
    # the same energy.

    rng = np.random.RandomState(44)

    e0 = getenergy(atoms)

    imgs = []

    i = 0
    while i < 10:
        P = rng.randint(-2, 3, size=(3, 3))
        detP = np.linalg.det(P)
        if detP == 0:
            continue
        elif detP < 0:
            P[0] *= -1
        bigatoms = make_supercell(atoms, P)
        imgs.append(bigatoms)
        getenergy(bigatoms, eref=e0)
        i += 1
    # from ase.visualize import view
    # view(imgs)