def get_displacement_tensor(self):
"""A matrix where the entry A[i, j, :] is the vector
self.cartesian_pos[i] - self.cartesian_pos[j].
For periodic systems the distance of an atom from itself is the
smallest displacement of an atom from one of it's periodic copies, and
the distance of two different atoms is the distance of two closest
copies.
Returns:
np.array: 3D matrix containing the pairwise distance vectors.
"""
if self._displacement_tensor is None:
if self.pbc.any():
pos = self.get_scaled_positions()
disp_tensor = pos[:, None, :] - pos[None, :, :]
# Take periodicity into account by wrapping coordinate elements
# that are bigger than 0.5 or smaller than -0.5
indices = np.where(disp_tensor > 0.5)
disp_tensor[indices] = 1 - disp_tensor[indices]
indices = np.where(disp_tensor < -0.5)
disp_tensor[indices] = disp_tensor[indices] + 1
# Transform to cartesian
disp_tensor = self.to_cartesian(disp_tensor)
# Figure out the smallest basis vector and set it as
# displacement for diagonal
cell = self.get_cell()
basis_lengths = np.linalg.norm(cell, axis=1)
min_index = np.argmin(basis_lengths)
min_basis = cell[min_index]
diag_indices = np.diag_indices(len(disp_tensor))
disp_tensor[diag_indices] = min_basis
else:
pos = self.get_positions()
disp_tensor = pos[:, None, :] - pos[None, :, :]
self._displacement_tensor = disp_tensor
return self._displacement_tensor
评论列表
文章目录