Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_out_of_bounds(self):
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 = neighbour_list("ijdDS", atoms, atoms.numbers * 0.2 + 0.5)
c = np.bincount(i)
atoms2 = atoms.repeat((p1 + 1, p2 + 1, p3 + 1))
i2, j2, d2, D2, S2 = neighbour_list("ijdDS", atoms2, atoms2.numbers * 0.2 + 0.5)
c2 = np.bincount(i2)
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])
self.assertTrue(abs(dd) < 1e-10)
self.assertTrue(not (c2 - c).any())
def test_small_cell(self):
a = ase.Atoms('C', positions=[[0.5, 0.5, 0.5]], cell=[1, 1, 1],
pbc=True)
i, j, dr, shift = neighbour_list("ijDS", a, 1.1)
assert np.bincount(i)[0] == 6
assert (dr == shift).all()
i, j = neighbour_list("ij", a, 1.5)
assert np.bincount(i)[0] == 18
a.set_pbc(False)
i = neighbour_list("i", a, 1.1)
assert len(i) == 0
a.set_pbc([True, False, False])
i = neighbour_list("i", a, 1.1)
assert np.bincount(i)[0] == 2
a.set_pbc([True, False, True])
i = neighbour_list("i", a, 1.1)
assert np.bincount(i)[0] == 4
def test_small_cell(self):
a = ase.Atoms('C', positions=[[0.5, 0.5, 0.5]], cell=[1, 1, 1],
pbc=True)
i, j, dr, shift = neighbour_list("ijDS", a, 1.1)
assert np.bincount(i)[0] == 6
assert (dr == shift).all()
i, j = neighbour_list("ij", a, 1.5)
assert np.bincount(i)[0] == 18
a.set_pbc(False)
i = neighbour_list("i", a, 1.1)
assert len(i) == 0
a.set_pbc([True, False, False])
i = neighbour_list("i", a, 1.1)
assert np.bincount(i)[0] == 2
a.set_pbc([True, False, True])
i = neighbour_list("i", a, 1.1)
assert np.bincount(i)[0] == 4
def test_hydrogenate(self):
a = Diamond('Si', size=[2,2,1])
b = hydrogenate(a, 2.85, 1.0, mask=[True,True,False], vacuum=5.0)
# Check if every atom is fourfold coordinated
syms = np.array(b.get_chemical_symbols())
c = np.bincount(neighbour_list('i', b, 2.4))
self.assertTrue((c[syms!='H']==4).all())
------
ValueError
Must specify cutoff radii for all atoms
"""
self.positions = atoms.get_positions()
self.pbc = atoms.get_pbc()
self.cell = atoms.get_cell()
shorti, shortj = mspy_nl(str("ij"), atoms, self.cutoffs)
new_neighbours = [[] for idx in range(len(atoms))]
for idx in range(len(shorti)):
new_neighbours[shorti[idx]].append(shortj[idx])
if self.do_hysteretic:
longi, longj = mspy_nl(str("ij"), atoms, self.cutoffs_hysteretic)
for idx in range(len(longi)):
# Split for profiling
previously_connected = longj[idx] in self.old_neighbours[longi[idx]]
not_added = longj[idx] not in new_neighbours[longi[idx]]
if previously_connected and not_added:
new_neighbours[longi[idx]].append(longj[idx])
self.old_neighbours = new_neighbours
for idx in range(len(new_neighbours)):
self.neighbours[idx] = np.asarray(list(new_neighbours[idx]))
self.nupdates += 1
def calculate(self, atoms, properties, system_changes):
Calculator.calculate(self, atoms, properties, system_changes)
nat = len(self.atoms)
atnums = self.atoms.numbers
atnums_in_system = set(atnums)
i_n, j_n, dr_nc, abs_dr_n = neighbour_list(
'ijDd', self.atoms, self.dict)
e_n = np.zeros_like(abs_dr_n)
de_n = np.zeros_like(abs_dr_n)
for params, pair in enumerate(self.dict):
if pair[0] == pair[1]:
mask1 = atnums[i_n] == pair[0]
mask2 = atnums[j_n] == pair[0]
mask = np.logical_and(mask1, mask2)
e_n[mask] = self.f[pair](abs_dr_n[mask])
de_n[mask] = self.df[pair](abs_dr_n[mask])
if pair[0] != pair[1]:
mask1 = np.logical_and(
atnums[i_n] == pair[0], atnums[j_n] == pair[1])
if H_format == "sparse":
try:
from scipy.sparse import bsr_matrix
except ImportError:
raise ImportError(
"Import error: Can not output the hessian matrix since scipy.sparse could not be loaded!")
f = self.f
dict = self.dict
df = self.df
df2 = self.df2
nat = len(atoms)
atnums = atoms.numbers
i_n, j_n, dr_nc, abs_dr_n = neighbour_list('ijDd', atoms, dict)
first_i = first_neighbours(nat, i_n)
e_n = np.zeros_like(abs_dr_n)
de_n = np.zeros_like(abs_dr_n)
dde_n = np.zeros_like(abs_dr_n)
for params, pair in enumerate(dict):
if pair[0] == pair[1]:
mask1 = atnums[i_n] == pair[0]
mask2 = atnums[j_n] == pair[0]
mask = np.logical_and(mask1, mask2)
e_n[mask] = f[pair](abs_dr_n[mask])
de_n[mask] = df[pair](abs_dr_n[mask])
dde_n[mask] = df2[pair](abs_dr_n[mask])
if pair[0] != pair[1]:
atnums_in_system = set(atnums)
for atnum in atnums_in_system:
if atnum not in atnums:
raise RuntimeError('Element with atomic number {} found, but '
'this atomic number has no EAM '
'parametrization'.format(atnum))
# i_n: index of the central atom
# j_n: index of the neighbor atom
# dr_nc: distance vector between the two
# abs_dr_n: norm of distance vector
# Variable name ending with _n indicate arrays that contain
# one element for each pair in the neighbor list. Names ending
# with _i indicate arrays containing one element for each atom.
i_n, j_n, dr_nc, abs_dr_n = neighbour_list('ijDd', atoms,
self._db_cutoff)
# Calculate derivatives of the pair energy
drep_n = np.zeros_like(abs_dr_n) # first derivative
ddrep_n = np.zeros_like(abs_dr_n) # second derivative
for atidx1, atnum1 in enumerate(self._db_atomic_numbers):
rep1 = self.rep[atidx1]
drep1 = self.drep[atidx1]
ddrep1 = self.ddrep[atidx1]
mask1 = atnums[i_n]==atnum1
if mask1.sum() > 0:
for atidx2, atnum2 in enumerate(self._db_atomic_numbers):
rep = rep1[atidx2]
drep = drep1[atidx2]
ddrep = ddrep1[atidx2]
mask = np.logical_and(mask1, atnums[j_n]==atnum2)
Parameters
----------
atoms : ase.Atoms
atoms to initialize the list from
Raises
------
ValueError
Must specify cutoff radii for all atoms
"""
self.positions = atoms.get_positions()
self.pbc = atoms.get_pbc()
self.cell = atoms.get_cell()
shorti, shortj = mspy_nl(str("ij"), atoms, self.cutoffs)
new_neighbours = [[] for idx in range(len(atoms))]
for idx in range(len(shorti)):
new_neighbours[shorti[idx]].append(shortj[idx])
if self.do_hysteretic:
longi, longj = mspy_nl(str("ij"), atoms, self.cutoffs_hysteretic)
for idx in range(len(longi)):
# Split for profiling
previously_connected = longj[idx] in self.old_neighbours[longi[idx]]
not_added = longj[idx] not in new_neighbours[longi[idx]]
if previously_connected and not_added:
new_neighbours[longi[idx]].append(longj[idx])
self.old_neighbours = new_neighbours