/
test_neighbors.py
300 lines (274 loc) · 13.8 KB
/
test_neighbors.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
import unittest
import numpy as np
from pyiron.atomistics.structure.atoms import Atoms, CrystalStructure
import warnings
class TestAtoms(unittest.TestCase):
@classmethod
def tearDownClass(cls):
pass
@classmethod
def setUpClass(cls):
pass
def test_allow_ragged(self):
struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(10)
del struct[0]
neigh = struct.get_neighbors_by_distance(cutoff_radius=3)
self.assertTrue(neigh.allow_ragged)
self.assertTrue(isinstance(neigh.indices, list))
indices = neigh.indices.copy()
with self.assertRaises(ValueError):
neigh.allow_ragged = 'yes'
neigh.allow_ragged = False
self.assertTrue(isinstance(neigh.indices, np.ndarray))
self.assertGreater(len(neigh.indices[0]), len(indices[0]))
with self.assertRaises(IndexError):
struct.positions[neigh.indices] = struct.positions[neigh.indices]
neigh.allow_ragged = True
self.assertTrue(np.array_equal(neigh.indices[0], indices[0]))
neigh = struct.get_neighbors(cutoff_radius=3, num_neighbors=None)
self.assertFalse(neigh.allow_ragged)
self.assertTrue(isinstance(neigh.indices, np.ndarray))
def test_getters(self):
struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc')
neigh = struct.get_neighbors()
self.assertTrue(np.array_equal(neigh.distances, neigh.get_distances()))
self.assertTrue(np.array_equal(neigh.vecs, neigh.get_vectors()))
self.assertTrue(np.array_equal(neigh.indices, neigh.get_indices()))
def test_getter_and_ragged(self):
struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc').repeat(2)
del struct[0]
neigh = struct.get_neighbors_by_distance(cutoff_radius=3)
vecs = neigh.get_vectors(allow_ragged=False)
distances = neigh.get_distances(allow_ragged=False)
indices = neigh.get_indices(allow_ragged=False)
neigh.allow_ragged = False
self.assertTrue(np.array_equal(neigh.distances, distances))
self.assertTrue(np.array_equal(neigh.indices, indices))
self.assertTrue(np.array_equal(neigh.vecs, vecs))
def test_get_neighborhood_single(self):
struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc')
neigh = struct.get_neighborhood(np.random.random(3), cutoff_radius=3)
distances = neigh.distances.copy()
neigh.allow_ragged = False
self.assertTrue(np.array_equal(neigh.distances, distances))
neigh.allow_ragged = True
self.assertTrue(np.array_equal(neigh.distances, distances))
neigh.allow_ragged = False
self.assertTrue(np.array_equal(neigh.distances, distances))
def test_get_neighborhood(self):
struct = CrystalStructure(elements='Al', lattice_constants=4, bravais_basis='fcc')
struct.positions += 0.01*(2*np.random.random(struct.positions.shape)-1)
struct = struct.center_coordinates_in_unit_cell()
positions = np.random.random((2, 3)).dot(struct.cell)
neigh = struct.get_neighborhood(positions)
positions = np.random.random((2, 3)).dot(struct.cell)
new_neigh = neigh.get_neighborhood(positions)
self.assertTrue(np.array_equal(new_neigh.distances, neigh.get_distances(positions)))
self.assertTrue(np.array_equal(new_neigh.vecs, neigh.get_vectors(positions)))
self.assertTrue(np.array_equal(new_neigh.indices, neigh.get_indices(positions)))
def test_get_neighbors(self):
struct = CrystalStructure(
elements='Fe', lattice_constants=2.85, bravais_basis='bcc'
).repeat(10)
cell = struct.cell.copy()
cell += np.random.random((3,3))-0.5
struct.positions += np.random.random((len(struct), 3))-0.5
struct.set_cell(cell, scale_atoms=True)
neigh = struct.get_neighbors()
self.assertAlmostEqual(
np.absolute(neigh.distances-np.linalg.norm(neigh.vecs, axis=-1)).max(), 0
)
myself = np.ones_like(neigh.indices)
myself = myself*np.arange(len(myself))[:,np.newaxis]
dist = struct.get_distances(myself.flatten(), neigh.indices.flatten(), mic=True)
self.assertAlmostEqual(np.absolute(dist-neigh.distances.flatten()).max(), 0)
vecs = struct.get_distances(
myself.flatten(), neigh.indices.flatten(), mic=True, vector=True
)
self.assertAlmostEqual(np.absolute(vecs-neigh.vecs.reshape(-1, 3)).max(), 0)
dist = struct.get_scaled_positions()
dist = dist[:,np.newaxis,:]-dist[np.newaxis,:,:]
dist -= np.rint(dist)
dist = np.einsum('nmi,ij->nmj', dist, struct.cell)
dist = np.linalg.norm(dist, axis=-1).flatten()
dist = dist[dist>0]
self.assertAlmostEqual(neigh.distances.min(), dist.min())
def test_pbc_false(self):
struct = CrystalStructure(
elements='Fe', lattice_constants=2.85, bravais_basis='bcc'
).repeat(10)
struct.pbc = False
cell = struct.cell.copy()
cell += np.random.random((3,3))-0.5
struct.set_cell(cell, scale_atoms=True)
neigh = struct.get_neighbors()
self.assertAlmostEqual(
np.absolute(neigh.distances-np.linalg.norm(neigh.vecs, axis=-1)).max(), 0
)
myself = np.ones_like(neigh.indices)
myself = myself*np.arange(len(myself))[:,np.newaxis]
dist = np.linalg.norm(
struct.positions[myself]-struct.positions[neigh.indices], axis=-1
)
self.assertAlmostEqual(np.absolute(dist-neigh.distances).max(), 0)
def test_fe_large(self):
struct = CrystalStructure(
elements='Fe', lattice_constants=2.85, bravais_basis='bcc'
).repeat(10)
neigh = struct.get_neighbors()
self.assertAlmostEqual(
np.absolute(neigh.distances-np.linalg.norm(neigh.vecs, axis=-1)).max(), 0
)
self.assertAlmostEqual(neigh.vecs[neigh.shells==1].sum(), 0)
self.assertAlmostEqual(neigh.vecs[0, neigh.shells[0]==1].sum(), 0)
def test_fe_small(self):
struct = CrystalStructure(elements='Fe', lattice_constants=2.85, bravais_basis='bcc')
neigh = struct.get_neighbors()
self.assertAlmostEqual(neigh.vecs[neigh.shells==1].sum(), 0)
with self.assertRaises(ValueError):
_ = struct.get_neighbors(num_neighbors=None)
def test_al_large(self):
struct = CrystalStructure(
elements='Al', lattice_constants=4.04, bravais_basis='fcc'
).repeat(10)
neigh = struct.get_neighbors()
self.assertAlmostEqual(
np.absolute(neigh.distances-np.linalg.norm(neigh.vecs, axis=-1)).max(), 0
)
self.assertAlmostEqual(neigh.vecs[neigh.shells==1].sum(), 0)
self.assertAlmostEqual(neigh.vecs[0, neigh.shells[0]==1].sum(), 0)
def test_al_small(self):
struct = CrystalStructure(elements='Al', lattice_constants=4.04, bravais_basis='fcc')
neigh = struct.get_neighbors()
self.assertAlmostEqual(
np.absolute(neigh.distances-np.linalg.norm(neigh.vecs, axis=-1)).max(), 0
)
self.assertAlmostEqual(neigh.vecs[neigh.shells==1].sum(), 0)
with self.assertRaises(ValueError):
struct.get_neighbors(num_neighbors=0)
def test_wrapped_positions(self):
structure = CrystalStructure(
elements='Al', lattice_constants=4, bravais_basis='fcc'
).repeat(2)
neigh = structure.get_neighbors()
distances = neigh.distances
new_positions = structure.positions+structure.cell.diagonal()
self.assertFalse(
np.all(np.isclose(distances, neigh.get_distances(new_positions, num_neighbors=13)[:,1:]))
)
neigh.wrap_positions = True
self.assertTrue(
np.all(np.isclose(distances, neigh.get_distances(new_positions, num_neighbors=13)[:,1:]))
)
def test_get_global_shells(self):
structure = CrystalStructure(
elements='Al', lattice_constants=4, bravais_basis='fcc'
).repeat(2)
neigh = structure.get_neighbors()
self.assertTrue(np.array_equal(neigh.shells, neigh.get_global_shells()))
structure += Atoms(elements='C', positions=[[0, 0, 0.5*4]])
neigh = structure.get_neighbors()
self.assertFalse(np.array_equal(neigh.shells, neigh.get_global_shells()))
structure = CrystalStructure(
elements='Al', lattice_constants=4, bravais_basis='fcc'
).repeat(2)
neigh = structure.get_neighbors()
shells = neigh.get_global_shells()
structure.positions += 0.01*(np.random.random((len(structure), 3))-0.5)
structure.center_coordinates_in_unit_cell()
neigh = structure.get_neighbors()
self.assertTrue(
np.array_equal(
shells, neigh.get_global_shells(cluster_by_vecs=True, cluster_by_distances=True)
)
)
neigh.reset_clusters()
self.assertTrue(np.array_equal(shells, neigh.get_global_shells(cluster_by_vecs=True)))
self.assertFalse(np.array_equal(shells, neigh.get_global_shells()))
def test_get_local_shells(self):
structure = CrystalStructure(
elements='Al', lattice_constants=4, bravais_basis='fcc'
).repeat(2)
neigh = structure.get_neighbors()
shells = neigh.get_local_shells()
structure.positions += 0.01*(np.random.random((len(structure), 3))-0.5)
neigh = structure.get_neighbors()
self.assertTrue(
np.array_equal(
shells, neigh.get_local_shells(cluster_by_vecs=True, cluster_by_distances=True)
)
)
neigh.reset_clusters()
self.assertTrue(np.array_equal(shells, neigh.get_local_shells(cluster_by_vecs=True)))
self.assertFalse(np.array_equal(shells, neigh.get_local_shells()))
def test_get_shell_matrix(self):
structure = CrystalStructure(
elements='Fe', lattice_constants=2.83, bravais_basis='bcc'
).repeat(2)
structure[0] = 'Ni'
neigh = structure.get_neighbors(num_neighbors=8)
mat = neigh.get_shell_matrix()
self.assertEqual(mat[0].sum(), 8*len(structure))
mat = neigh.get_shell_matrix(chemical_pair=['Fe', 'Ni'])
self.assertEqual(mat[0].sum(), 16)
mat = neigh.get_shell_matrix(chemical_pair=['Ni', 'Ni'])
self.assertEqual(mat[0].sum(), 0)
def test_cluster_analysis(self):
basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(10)
neigh = basis.get_neighbors(num_neighbors=100)
key, counts = neigh.cluster_analysis(id_list=[0,1], return_cluster_sizes=True)
self.assertTrue(np.array_equal(key[1], [0,1]))
self.assertEqual(counts[0], 2)
key, counts = neigh.cluster_analysis(
id_list=[0,int(len(basis)/2)], return_cluster_sizes=True
)
self.assertTrue(np.array_equal(key[1], [0]))
self.assertEqual(counts[0], 1)
def test_get_bonds(self):
basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(5)
neigh = basis.get_neighbors(num_neighbors=20)
bonds = neigh.get_bonds()
self.assertTrue(np.array_equal(np.sort(bonds[0]['Al'][0]),
np.sort(neigh.indices[0, neigh.shells[0]==1])))
def test_find_neighbors_by_vector(self):
basis = Atoms(symbols=2*["Fe"],
scaled_positions=[(0, 0, 0), (0.5, 0.5, 0.5)],
cell=np.identity(3),
pbc=True)
neigh = basis.get_neighbors(num_neighbors=14)
id_lst, dist = neigh.find_neighbors_by_vector([0, 0, 1],
deviation=True)
self.assertEqual(len(np.unique(np.unique(id_lst, return_counts=True)[1])), 1)
self.assertLess(np.linalg.norm(dist), 1.0e-4)
id_lst = neigh.find_neighbors_by_vector([0, 0, 0])
self.assertTrue(np.array_equal(id_lst, np.arange(len(basis))))
def test_get_distances_arbitrary_array(self):
basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(3)
neigh = basis.get_neighbors(cutoff_radius=3.5, num_neighbors=None)
self.assertEqual(len(neigh.get_indices(np.random.random(3), num_neighbors=12)), 12)
self.assertEqual(
len(neigh.get_distances(np.random.random(3), num_neighbors=12, allow_ragged=True)), 12
)
self.assertLessEqual(
len(neigh.get_vectors(np.random.random(3), num_neighbors=12, allow_ragged=True, cutoff_radius=3.5)), 12
)
self.assertTrue(neigh.get_vectors(np.random.random((2,3)), num_neighbors=12).shape==(2,12,3))
neigh = basis.get_neighbors(num_neighbors=50)
self.assertTrue(neigh.get_distances(np.random.random(3)).shape==(50,))
self.assertTrue(neigh.get_indices(np.random.random((2,3))).shape==(2,50))
self.assertTrue(neigh.get_vectors(np.random.random((2,2,3))).shape==(2,2,50,3))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
_ = neigh.get_neighborhood(np.random.random(3), num_neighbors=51)
self.assertEqual(len(w), 1)
_ = neigh.get_distances(np.random.random(3), num_neighbors=51)
self.assertEqual(len(w), 2)
def test_repr(self):
basis = CrystalStructure("Al", bravais_basis="fcc", lattice_constants=4.2).repeat(3)
neigh = basis.get_neighbors(cutoff_radius=3.5, num_neighbors=None)
self.assertTrue('each atom' in neigh.__repr__())
if __name__ == "__main__":
unittest.main()