Statistiques
| Révision :

root / ase / test / center.py @ 13

Historique | Voir | Annoter | Télécharger (2,21 ko)

1
"Test that atoms.center() works when adding vacuum ()"
2

    
3
import numpy as np
4
from math import pi, sqrt, cos
5
from ase import data
6
from ase.lattice.cubic import FaceCenteredCubic
7

    
8
def checkang(a, b, phi):
9
    "Check the angle between two vectors."
10
    cosphi = np.dot(a,b) / sqrt(np.dot(a,a) * np.dot(b,b))
11
    assert np.abs(cosphi - cos(phi)) < 1e-10
12

    
13
symb = "Cu"
14
Z = data.atomic_numbers[symb]
15
a0 = data.reference_states[Z]['a']
16

    
17
# (100) oriented block
18
atoms = FaceCenteredCubic(size=(5,5,5), symbol="Cu", pbc=(1,1,0))
19
assert len(atoms) == 5*5*5*4
20
c = atoms.get_cell()
21
checkang(c[0], c[1], pi/2)
22
checkang(c[0], c[2], pi/2)
23
checkang(c[1], c[2], pi/2)
24
assert np.abs(5 * a0 - c[2,2]) < 1e-10
25

    
26
# Add vacuum in one direction
27
vac = 10.0
28
atoms.center(axis=2, vacuum=vac)
29
c = atoms.get_cell()
30
checkang(c[0], c[1], pi/2)
31
checkang(c[0], c[2], pi/2)
32
checkang(c[1], c[2], pi/2)
33
assert np.abs(4.5 * a0 + 2* vac - c[2,2]) < 1e-10
34

    
35
# Add vacuum in all directions
36
vac = 4.0
37
atoms.center(vacuum=vac)
38
c = atoms.get_cell()
39
checkang(c[0], c[1], pi/2)
40
checkang(c[0], c[2], pi/2)
41
checkang(c[1], c[2], pi/2)
42
assert np.abs(4.5 * a0 + 2* vac - c[0,0]) < 1e-10
43
assert np.abs(4.5 * a0 + 2* vac - c[1,1]) < 1e-10
44
assert np.abs(4.5 * a0 + 2* vac - c[2,2]) < 1e-10
45

    
46
# Now a general unit cell
47
atoms = FaceCenteredCubic(size=(5,5,5), directions=[[1,0,0], [0,1,0], [1,0,1]],
48
                          symbol="Cu", pbc=(1,1,0))
49
assert len(atoms) == 5*5*5*2
50
c = atoms.get_cell()
51
checkang(c[0], c[1], pi/2)
52
checkang(c[0], c[2], pi/4)
53
checkang(c[1], c[2], pi/2)
54
assert np.abs(2.5 * a0 - c[2,2]) < 1e-10
55

    
56
# Add vacuum in one direction
57
vac = 10.0
58
atoms.center(axis=2, vacuum=vac)
59
c = atoms.get_cell()
60
checkang(c[0], c[1], pi/2)
61
checkang(c[0], c[2], pi/4)
62
checkang(c[1], c[2], pi/2)
63
assert np.abs(2 * a0 + 2* vac - c[2,2]) < 1e-10
64

    
65
# Recenter without specifying vacuum
66
atoms.center()
67
c = atoms.get_cell()
68
checkang(c[0], c[1], pi/2)
69
checkang(c[0], c[2], pi/4)
70
checkang(c[1], c[2], pi/2)
71
assert np.abs(2 * a0 + 2* vac - c[2,2]) < 1e-10
72

    
73
# Add vacuum in all directions
74
vac = 4.0
75
atoms.center(vacuum=vac)
76
c = atoms.get_cell()
77
checkang(c[0], c[1], pi/2)
78
checkang(c[0], c[2], pi/4)
79
checkang(c[1], c[2], pi/2)
80
assert np.abs(4.5 * a0 + 2* vac - c[1,1]) < 1e-10
81
assert np.abs(2 * a0 + 2* vac - c[2,2]) < 1e-10
82