File: test_neighbor_kernel.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (216 lines) | stat: -rw-r--r-- 7,826 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import numpy as np
import pytest

import ase
import ase.lattice.hexagonal
from ase.build import bulk, molecule

from ase.neighborlist import (mic, neighbor_list, primitive_neighbor_list,
                              first_neighbors)


@pytest.mark.slow
def test_neighbor_kernel():
    tol = 1e-7

    # two atoms
    a = ase.Atoms('CC', positions=[[0.5, 0.5, 0.5], [1, 1, 1]], cell=[10, 10, 10],
                  pbc=True)
    i, j, d = neighbor_list("ijd", a, 1.1)
    assert (i == np.array([0, 1])).all()
    assert (j == np.array([1, 0])).all()
    assert np.abs(d - np.array([np.sqrt(3/4), np.sqrt(3/4)])).max() < tol

    # test_neighbor_list
    for pbc in [True, False, [True, False, True]]:
        a = ase.Atoms('4001C', cell=[29, 29, 29])
        a.set_scaled_positions(np.transpose([np.random.random(len(a)),
                                             np.random.random(len(a)),
                                             np.random.random(len(a))]))
        j, dr, i, abs_dr, shift = neighbor_list("jDidS", a, 1.85)

        assert (np.bincount(i) == np.bincount(j)).all()

        r = a.get_positions()
        dr_direct = mic(r[j]-r[i], a.cell)
        assert np.abs(r[j]-r[i]+shift.dot(a.cell) - dr_direct).max() < tol

        abs_dr_from_dr = np.sqrt(np.sum(dr*dr, axis=1))
        abs_dr_direct = np.sqrt(np.sum(dr_direct*dr_direct, axis=1))

        assert np.all(np.abs(abs_dr-abs_dr_from_dr) < 1e-12)
        assert np.all(np.abs(abs_dr-abs_dr_direct) < 1e-12)

        assert np.all(np.abs(dr-dr_direct) < 1e-12)

    # test_neighbor_list_atoms_outside_box
    for pbc in [True, False, [True, False, True]]:
        a = ase.Atoms('4001C', cell=[29, 29, 29])
        a.set_scaled_positions(np.transpose([np.random.random(len(a)),
                                             np.random.random(len(a)),
                                             np.random.random(len(a))]))
        a.set_pbc(pbc)
        a.positions[100, :] += a.cell[0, :]
        a.positions[200, :] += a.cell[1, :]
        a.positions[300, :] += a.cell[2, :]
        j, dr, i, abs_dr, shift = neighbor_list("jDidS", a, 1.85)

        assert (np.bincount(i) == np.bincount(j)).all()

        r = a.get_positions()
        dr_direct = mic(r[j]-r[i], a.cell)
        assert np.abs(r[j]-r[i]+shift.dot(a.cell) - dr_direct).max() < tol

        abs_dr_from_dr = np.sqrt(np.sum(dr*dr, axis=1))
        abs_dr_direct = np.sqrt(np.sum(dr_direct*dr_direct, axis=1))

        assert np.all(np.abs(abs_dr-abs_dr_from_dr) < 1e-12)
        assert np.all(np.abs(abs_dr-abs_dr_direct) < 1e-12)

        assert np.all(np.abs(dr-dr_direct) < 1e-12)

    # test_small_cell
    a = ase.Atoms('C', positions=[[0.5, 0.5, 0.5]], cell=[1, 1, 1],
                  pbc=True)
    i, j, dr, shift = neighbor_list("ijDS", a, 1.1)
    assert np.bincount(i)[0] == 6
    assert (dr == shift).all()

    i, j = neighbor_list("ij", a, 1.5)
    assert np.bincount(i)[0] == 18

    a.set_pbc(False)
    i = neighbor_list("i", a, 1.1)
    assert len(i) == 0

    a.set_pbc([True, False, False])
    i = neighbor_list("i", a, 1.1)
    assert np.bincount(i)[0] == 2

    a.set_pbc([True, False, True])
    i = neighbor_list("i", a, 1.1)
    assert np.bincount(i)[0] == 4

    # test_out_of_cell_small_cell
    a = ase.Atoms('CC', positions=[[0.5, 0.5, 0.5],
                                   [1.1, 0.5, 0.5]],
                  cell=[1, 1, 1], pbc=False)
    i1, j1, r1 = neighbor_list("ijd", a, 1.1)
    a.set_cell([2, 1, 1])
    i2, j2, r2 = neighbor_list("ijd", a, 1.1)

    assert (i1 == i2).all()
    assert (j1 == j2).all()
    assert np.abs(r1 - r2).max() < tol

    # test_out_of_cell_large_cell
    a = ase.Atoms('CC', positions=[[9.5, 0.5, 0.5],
                                   [10.1, 0.5, 0.5]],
                  cell=[10, 10, 10], pbc=False)
    i1, j1, r1 = neighbor_list("ijd", a, 1.1)
    a.set_cell([20, 10, 10])
    i2, j2, r2 = neighbor_list("ijd", a, 1.1)

    assert (i1 == i2).all()
    assert (j1 == j2).all()
    assert np.abs(r1 - r2).max() < tol

    # test_hexagonal_cell
    for sx in range(3):
        a = ase.lattice.hexagonal.Graphite('C', latticeconstant=(2.5, 10.0),
                                           size=[sx+1, sx+1, 1])
        i = neighbor_list("i", a, 1.85)
        assert np.all(np.bincount(i) == 3)

    # test_first_neighbors
    i = [1, 1, 1, 1, 3, 3, 3]
    assert (first_neighbors(5, i) == np.array([0, 0, 4, 4, 7, 7])).all()
    i = [0, 1, 2, 3, 4, 5]
    assert (first_neighbors(6, i) == np.array([0, 1, 2, 3, 4, 5, 6])).all()

    # test_multiple_elements
    a = molecule('HCOOH')
    a.center(vacuum=5.0)
    i = neighbor_list("i", a, 1.85)
    assert (np.bincount(i) == np.array([2, 3, 1, 1, 1])).all()

    cutoffs = {(1, 6): 1.2}
    i = neighbor_list("i", a, cutoffs)
    assert (np.bincount(i) == np.array([0, 1, 0, 0, 1])).all()

    cutoffs = {(6, 8): 1.4}
    i = neighbor_list("i", a, cutoffs)
    assert (np.bincount(i) == np.array([1, 2, 1])).all()

    cutoffs = {('H', 'C'): 1.2, (6, 8): 1.4}
    i = neighbor_list("i", a, cutoffs)
    assert (np.bincount(i) == np.array([1, 3, 1, 0, 1])).all()

    cutoffs = [0.0, 0.9, 0.0, 0.5, 0.5]
    i = neighbor_list("i", a, cutoffs)
    assert (np.bincount(i) == np.array([0, 1, 0, 0, 1])).all()

    cutoffs = [0.7, 0.9, 0.7, 0.5, 0.5]
    i = neighbor_list("i", a, cutoffs)
    assert (np.bincount(i) == np.array([2, 3, 1, 1, 1])).all()

    # test_noncubic
    a = bulk("Al", cubic=False)
    i, j, d = neighbor_list("ijd", a, 3.1)
    assert (np.bincount(i) == np.array([12])).all()
    assert np.abs(d - [2.86378246]*12).max() < tol

    # test pbc
    nat = 10
    atoms = ase.Atoms(numbers=range(nat),
                      cell=[(0.2, 1.2, 1.4),
                            (1.4, 0.1, 1.6),
                            (1.3, 2.0, -0.1)])
    atoms.set_scaled_positions(3 * np.random.random((nat, 3)) - 1)

    for p1 in range(2):
        for p2 in range(2):
            for p3 in range(2):
                atoms.set_pbc((p1, p2, p3))
                i, j, d, D, S = neighbor_list("ijdDS", atoms, atoms.numbers * 0.2 + 0.5)
                c = np.bincount(i, minlength=len(atoms))
                atoms2 = atoms.repeat((p1 + 1, p2 + 1, p3 + 1))
                i2, j2, d2, D2, S2 = neighbor_list("ijdDS", atoms2, atoms2.numbers * 0.2 + 0.5)
                c2 = np.bincount(i2, minlength=len(atoms))
                c2.shape = (-1, nat)
                dd = d.sum() * (p1 + 1) * (p2 + 1) * (p3 + 1) - d2.sum()
                dr = np.linalg.solve(atoms.cell.T, (atoms.positions[1]-atoms.positions[0]).T).T+np.array([0, 0, 3])
                assert abs(dd) < 1e-10
                assert not (c2 - c).any()

    c = 0.0058
    i, j, d = primitive_neighbor_list('ijd',
                                      [True, True, True],
                                      np.eye(3) * 7.56,
                                      np.array([[0, 0, 0],
                                                [0, 0, 0.99875]]),
                                      [c, c],
                                      self_interaction=False,
                                      use_scaled_positions=True)
    assert np.all(i == [0, 1])
    assert np.all(j == [1, 0])
    assert np.allclose(d, [0.00945, 0.00945])

    # Empty atoms object
    i, D, d, j, S = neighbor_list("iDdjS", ase.Atoms(), 1.0)
    assert i.dtype == int
    assert j.dtype == int
    assert d.dtype == float
    assert D.dtype == float
    assert S.dtype == int
    assert i.shape == (0,)
    assert j.shape == (0,)
    assert d.shape == (0,)
    assert D.shape == (0, 3)
    assert S.shape == (0, 3)

    # Check that only a scalar (not a tuple) is returned if we request a single
    # argument.
    i = neighbor_list("i", ase.Atoms(), 1.0)
    assert i.dtype == int
    assert i.shape == (0,)