def general_find_mic(v, cell, pbc=True): """Finds the minimum-image representation of vector(s) v. Using the Minkowski reduction the algorithm is relatively slow but safe for any cell. """ cell = complete_cell(cell) rcell, _ = minkowski_reduce(cell, pbc=pbc) positions = wrap_positions(v, rcell, pbc=pbc, eps=0) # In a Minkowski-reduced cell we only need to test nearest neighbors, # or "Voronoi-relevant" vectors. These are a subset of combinations of # [-1, 0, 1] of the reduced cell vectors. # Define ranges [-1, 0, 1] for periodic directions and [0] for aperiodic # directions. ranges = [np.arange(-1 * p, p + 1) for p in pbc] # Get Voronoi-relevant vectors. # Pre-pend (0, 0, 0) to resolve issue #772 hkls = np.array([(0, 0, 0)] + list(itertools.product(*ranges))) vrvecs = hkls @ rcell # Map positions into neighbouring cells. x = positions + vrvecs[:, None] # Find minimum images lengths = np.linalg.norm(x, axis=2) indices = np.argmin(lengths, axis=0) vmin = x[indices, np.arange(len(positions)), :] vlen = lengths[indices, np.arange(len(positions))] return vmin, vlen
def find_mic(v, cell, pbc=True): """Finds the minimum-image representation of vector(s) v""" pbc = cell.any(1) & pbc2pbc(pbc) v = np.array(v) single = len(v.shape) == 1 v = np.atleast_2d(v) if np.sum(pbc) > 0: cell = complete_cell(cell) rcell, _ = minkowski_reduce(cell, pbc=pbc) # in a Minkowski-reduced cell we only need to test nearest neighbors cs = [np.arange(-1 * p, p + 1) for p in pbc] neighbor_cells = list(itertools.product(*cs)) positions = wrap_positions(v, rcell, pbc=pbc, eps=0) vmin = positions.copy() vlen = np.linalg.norm(positions, axis=1) for nbr in neighbor_cells: trial = positions + np.dot(rcell.T, nbr) trial_len = np.linalg.norm(trial, axis=1) indices = np.where(trial_len < vlen) vmin[indices] = trial[indices] vlen[indices] = trial_len[indices] else: vmin = v.copy() vlen = np.linalg.norm(vmin, axis=1) if single: return vmin[0], vlen[0] else: return vmin, vlen
def wrap_positions(positions, cell, pbc=True, center=(0.5, 0.5, 0.5), eps=1e-7): """Wrap positions to unit cell. Returns positions changed by a multiple of the unit cell vectors to fit inside the space spanned by these vectors. See also the :meth:`ase.Atoms.wrap` method. Parameters: positions: float ndarray of shape (n, 3) Positions of the atoms cell: float ndarray of shape (3, 3) Unit cell vectors. pbc: one or 3 bool For each axis in the unit cell decides whether the positions will be moved along this axis. center: three float The positons in fractional coordinates that the new positions will be nearest possible to. eps: float Small number to prevent slightly negative coordinates from being wrapped. Example: >>> from ase.geometry import wrap_positions >>> wrap_positions([[-0.1, 1.01, -0.5]], ... [[1, 0, 0], [0, 1, 0], [0, 0, 4]], ... pbc=[1, 1, 0]) array([[ 0.9 , 0.01, -0.5 ]]) """ if not hasattr(pbc, '__len__'): pbc = (pbc, ) * 3 if not hasattr(center, '__len__'): center = (center, ) * 3 shift = np.asarray(center) - 0.5 - eps # Don't change coordinates when pbc is False shift[np.logical_not(pbc)] = 0.0 assert np.asarray(cell)[np.asarray(pbc)].any(axis=1).all(), (cell, pbc) cell = complete_cell(cell) fractional = np.linalg.solve(cell.T, np.asarray(positions).T).T - shift for i, periodic in enumerate(pbc): if periodic: fractional[:, i] %= 1.0 fractional[:, i] += shift[i] return np.dot(fractional, cell)
def wrap_positions(positions, cell, pbc=True, center=(0.5, 0.5, 0.5), eps=1e-7): """Wrap positions to unit cell. Returns positions changed by a multiple of the unit cell vectors to fit inside the space spanned by these vectors. See also the :meth:`ase.atoms.Atoms.wrap` method. Parameters: positions: float ndarray of shape (n, 3) Positions of the atoms cell: float ndarray of shape (3, 3) Unit cell vectors. pbc: one or 3 bool For each axis in the unit cell decides whether the positions will be moved along this axis. center: three float The positons in fractional coordinates that the new positions will be nearest possible to. eps: float Small number to prevent slightly negative coordinates from being wrapped. Example: >>> from ase.geometry import wrap_positions >>> wrap_positions([[-0.1, 1.01, -0.5]], ... [[1, 0, 0], [0, 1, 0], [0, 0, 4]], ... pbc=[1, 1, 0]) array([[ 0.9 , 0.01, -0.5 ]]) """ if not hasattr(pbc, '__len__'): pbc = (pbc,) * 3 if not hasattr(center, '__len__'): center = (center,) * 3 shift = np.asarray(center) - 0.5 - eps # Don't change coordinates when pbc is False shift[np.logical_not(pbc)] = 0.0 assert np.asarray(cell)[np.asarray(pbc)].any(axis=1).all(), (cell, pbc) cell = complete_cell(cell) fractional = np.linalg.solve(cell.T, np.asarray(positions).T).T - shift for i, periodic in enumerate(pbc): if periodic: fractional[:, i] %= 1.0 fractional[:, i] += shift[i] return np.dot(fractional, cell)
def _get_neighbors(self, dx: np.ndarray) -> Iterator[np.ndarray]: pbc = self.atoms.pbc if self.cell is None or not np.all(self.cell == self.atoms.cell): self.cell = self.atoms.cell.array.copy() rcell, self.op = minkowski_reduce(complete_cell(self.cell), pbc=pbc) self.rcell = Cell(rcell) dx_sc = dx @ self.rcell.reciprocal().T offset = np.zeros(3, dtype=np.int32) for _ in range(2): offset += pbc * ((dx_sc - offset) // 1.).astype(np.int32) for ts in product(*[np.arange(-1 * p, p + 1) for p in pbc]): yield (np.array(ts) - offset) @ self.op
def set_view(self, key): if key == 'Z': self.axes = rotate('0.0x,0.0y,0.0z') elif key == 'X': self.axes = rotate('-90.0x,-90.0y,0.0z') elif key == 'Y': self.axes = rotate('90.0x,0.0y,90.0z') elif key == 'Alt+Z': self.axes = rotate('180.0x,0.0y,90.0z') elif key == 'Alt+X': self.axes = rotate('0.0x,90.0y,0.0z') elif key == 'Alt+Y': self.axes = rotate('-90.0x,0.0y,0.0z') else: if key == '3': i, j = 0, 1 elif key == '1': i, j = 1, 2 elif key == '2': i, j = 2, 0 elif key == 'Alt+3': i, j = 1, 0 elif key == 'Alt+1': i, j = 2, 1 elif key == 'Alt+2': i, j = 0, 2 A = complete_cell(self.atoms.cell) x1 = A[i] x2 = A[j] norm = np.linalg.norm x1 = x1 / norm(x1) x2 = x2 - x1 * np.dot(x1, x2) x2 /= norm(x2) x3 = np.cross(x1, x2) self.axes = np.array([x1, x2, x3]).T self.set_frame()
def find_mic(D, cell, pbc=True): """Finds the minimum-image representation of vector(s) D""" cell = complete_cell(cell) # Calculate the 4 unique unit cell diagonal lengths diags = np.sqrt((np.dot([ [1, 1, 1], [-1, 1, 1], [1, -1, 1], [-1, -1, 1], ], cell)**2).sum(1)) # calculate 'mic' vectors (D) and lengths (D_len) using simple method Dr = np.dot(D, np.linalg.inv(cell)) D = np.dot(Dr - np.round(Dr) * pbc, cell) D_len = np.sqrt((D**2).sum(1)) # return mic vectors and lengths for only orthorhombic cells, # as the results may be wrong for non-orthorhombic cells if (max(diags) - min(diags)) / max(diags) < 1e-9: return D, D_len # The cutoff radius is the longest direct distance between atoms # or half the longest lattice diagonal, whichever is smaller cutoff = min(max(D_len), max(diags) / 2.) # The number of neighboring images to search in each direction is # equal to the ceiling of the cutoff distance (defined above) divided # by the length of the projection of the lattice vector onto its # corresponding surface normal. a's surface normal vector is e.g. # b x c / (|b| |c|), so this projection is (a . (b x c)) / (|b| |c|). # The numerator is just the lattice volume, so this can be simplified # to V / (|b| |c|). This is rewritten as V |a| / (|a| |b| |c|) # for vectorization purposes. latt_len = np.sqrt((cell**2).sum(1)) V = abs(np.linalg.det(cell)) n = pbc * np.array(np.ceil(cutoff * np.prod(latt_len) / (V * latt_len)), dtype=int) # Construct a list of translation vectors. For example, if we are # searching only the nearest images (27 total), tvecs will be a # 27x3 array of translation vectors. This is the only nested loop # in the routine, and it takes a very small fraction of the total # execution time, so it is not worth optimizing further. tvecs = [] for i in range(-n[0], n[0] + 1): latt_a = i * cell[0] for j in range(-n[1], n[1] + 1): latt_ab = latt_a + j * cell[1] for k in range(-n[2], n[2] + 1): tvecs.append(latt_ab + k * cell[2]) tvecs = np.array(tvecs) # Check periodic neighbors iff the displacement vector in # scaled coordinates is greater than 0.5. good = np.sqrt((np.linalg.solve(cell.T, D.T)**2).sum(0)) <= 0.5 D_min = D.copy() D_min_len = D_len.copy() for i, (Di, gdi) in enumerate(zip(D, good)): if gdi: # No need to check periodic neighbors. continue # Translate the direct displacement vector by each translation # vector, and calculate the corresponding length. Di_trans = Di[np.newaxis] + tvecs Di_trans_len = np.sqrt((Di_trans**2).sum(1)) # Find mic distance and corresponding vector. Di_min_ind = Di_trans_len.argmin() D_min[i] = Di_trans[Di_min_ind] D_min_len[i] = Di_trans_len[Di_min_ind] return D_min, D_min_len
def primitive_neighbor_list(quantities, pbc, cell, positions, cutoff, numbers=None, self_interaction=False, use_scaled_positions=False, max_nbins=1e6): """Compute a neighbor list for an atomic configuration. Atoms outside periodic boundaries are mapped into the box. Atoms outside nonperiodic boundaries are included in the neighbor list but complexity of neighbor list search for those can become n^2. The neighbor list is sorted by first atom index 'i', but not by second atom index 'j'. Parameters: quantities: str Quantities to compute by the neighbor list algorithm. Each character in this string defines a quantity. They are returned in a tuple of the same order. Possible quantities are * 'i' : first atom index * 'j' : second atom index * 'd' : absolute distance * 'D' : distance vector * 'S' : shift vector (number of cell boundaries crossed by the bond between atom i and j). With the shift vector S, the distances D between atoms can be computed from: D = positions[j]-positions[i]+S.dot(cell) pbc: array_like 3-tuple indicating giving periodic boundaries in the three Cartesian directions. cell: 3x3 matrix Unit cell vectors. positions: list of xyz-positions Atomic positions. Anything that can be converted to an ndarray of shape (n, 3) will do: [(x1,y1,z1), (x2,y2,z2), ...]. If use_scaled_positions is set to true, this must be scaled positions. cutoff: float or dict Cutoff for neighbor search. It can be: * A single float: This is a global cutoff for all elements. * A dictionary: This specifies cutoff values for element pairs. Specification accepts element numbers of symbols. Example: {(1, 6): 1.1, (1, 1): 1.0, ('C', 'C'): 1.85} * A list/array with a per atom value: This specifies the radius of an atomic sphere for each atoms. If spheres overlap, atoms are within each others neighborhood. See :func:`~ase.neighborlist.natural_cutoffs` for an example on how to get such a list. self_interaction: bool Return the atom itself as its own neighbor if set to true. Default: False use_scaled_positions: bool If set to true, positions are expected to be scaled positions. max_nbins: int Maximum number of bins used in neighbor search. This is used to limit the maximum amount of memory required by the neighbor list. Returns: i, j, ... : array Tuple with arrays for each quantity specified above. Indices in `i` are returned in ascending order 0..len(a)-1, but the order of (i,j) pairs is not guaranteed. """ # Naming conventions: Suffixes indicate the dimension of an array. The # following convention is used here: # c: Cartesian index, can have values 0, 1, 2 # i: Global atom index, can have values 0..len(a)-1 # xyz: Bin index, three values identifying x-, y- and z-component of a # spatial bin that is used to make neighbor search O(n) # b: Linearized version of the 'xyz' bin index # a: Bin-local atom index, i.e. index identifying an atom *within* a # bin # p: Pair index, can have value 0 or 1 # n: (Linear) neighbor index # Return empty neighbor list if no atoms are passed here if len(positions) == 0: empty_types = dict(i=(np.int, (0, )), j=(np.int, (0, )), D=(np.float, (0, 3)), d=(np.float, (0, )), S=(np.int, (0, 3))) retvals = [] for i in quantities: dtype, shape = empty_types[i] retvals += [np.array([], dtype=dtype).reshape(shape)] if len(retvals) == 1: return retvals[0] else: return tuple(retvals) # Compute reciprocal lattice vectors. b1_c, b2_c, b3_c = np.linalg.pinv(cell).T # Compute distances of cell faces. l1 = np.linalg.norm(b1_c) l2 = np.linalg.norm(b2_c) l3 = np.linalg.norm(b3_c) face_dist_c = np.array([ 1 / l1 if l1 > 0 else 1, 1 / l2 if l2 > 0 else 1, 1 / l3 if l3 > 0 else 1 ]) if isinstance(cutoff, dict): max_cutoff = max(cutoff.values()) else: if np.isscalar(cutoff): max_cutoff = cutoff else: cutoff = np.asarray(cutoff) max_cutoff = 2 * np.max(cutoff) # We use a minimum bin size of 3 A bin_size = max(max_cutoff, 3) # Compute number of bins such that a sphere of radius cutoff fits into # eight neighboring bins. nbins_c = np.maximum((face_dist_c / bin_size).astype(int), [1, 1, 1]) nbins = np.prod(nbins_c) # Make sure we limit the amount of memory used by the explicit bins. while nbins > max_nbins: nbins_c = np.maximum(nbins_c // 2, [1, 1, 1]) nbins = np.prod(nbins_c) # Compute over how many bins we need to loop in the neighbor list search. neigh_search_x, neigh_search_y, neigh_search_z = \ np.ceil(bin_size * nbins_c / face_dist_c).astype(int) # If we only have a single bin and the system is not periodic, then we # do not need to search neighboring bins neigh_search_x = 0 if nbins_c[0] == 1 and not pbc[0] else neigh_search_x neigh_search_y = 0 if nbins_c[1] == 1 and not pbc[1] else neigh_search_y neigh_search_z = 0 if nbins_c[2] == 1 and not pbc[2] else neigh_search_z # Sort atoms into bins. if use_scaled_positions: scaled_positions_ic = positions positions = np.dot(scaled_positions_ic, cell) else: scaled_positions_ic = np.linalg.solve( complete_cell(cell).T, positions.T).T bin_index_ic = np.floor(scaled_positions_ic * nbins_c).astype(int) cell_shift_ic = np.zeros_like(bin_index_ic) for c in range(3): if pbc[c]: # (Note: np.divmod does not exist in older numpies) cell_shift_ic[:, c], bin_index_ic[:, c] = \ divmod(bin_index_ic[:, c], nbins_c[c]) else: bin_index_ic[:, c] = np.clip(bin_index_ic[:, c], 0, nbins_c[c] - 1) # Convert Cartesian bin index to unique scalar bin index. bin_index_i = (bin_index_ic[:, 0] + nbins_c[0] * (bin_index_ic[:, 1] + nbins_c[1] * bin_index_ic[:, 2])) # atom_i contains atom index in new sort order. atom_i = np.argsort(bin_index_i) bin_index_i = bin_index_i[atom_i] # Find max number of atoms per bin max_natoms_per_bin = np.bincount(bin_index_i).max() # Sort atoms into bins: atoms_in_bin_ba contains for each bin (identified # by its scalar bin index) a list of atoms inside that bin. This list is # homogeneous, i.e. has the same size *max_natoms_per_bin* for all bins. # The list is padded with -1 values. atoms_in_bin_ba = -np.ones([nbins, max_natoms_per_bin], dtype=int) for i in range(max_natoms_per_bin): # Create a mask array that identifies the first atom of each bin. mask = np.append([True], bin_index_i[:-1] != bin_index_i[1:]) # Assign all first atoms. atoms_in_bin_ba[bin_index_i[mask], i] = atom_i[mask] # Remove atoms that we just sorted into atoms_in_bin_ba. The next # "first" atom will be the second and so on. mask = np.logical_not(mask) atom_i = atom_i[mask] bin_index_i = bin_index_i[mask] # Make sure that all atoms have been sorted into bins. assert len(atom_i) == 0 assert len(bin_index_i) == 0 # Now we construct neighbor pairs by pairing up all atoms within a bin or # between bin and neighboring bin. atom_pairs_pn is a helper buffer that # contains all potential pairs of atoms between two bins, i.e. it is a list # of length max_natoms_per_bin**2. atom_pairs_pn = np.indices((max_natoms_per_bin, max_natoms_per_bin), dtype=int) atom_pairs_pn = atom_pairs_pn.reshape(2, -1) # Initialized empty neighbor list buffers. first_at_neightuple_nn = [] secnd_at_neightuple_nn = [] cell_shift_vector_x_n = [] cell_shift_vector_y_n = [] cell_shift_vector_z_n = [] # This is the main neighbor list search. We loop over neighboring bins and # then construct all possible pairs of atoms between two bins, assuming # that each bin contains exactly max_natoms_per_bin atoms. We then throw # out pairs involving pad atoms with atom index -1 below. binz_xyz, biny_xyz, binx_xyz = np.meshgrid(np.arange(nbins_c[2]), np.arange(nbins_c[1]), np.arange(nbins_c[0]), indexing='ij') # The memory layout of binx_xyz, biny_xyz, binz_xyz is such that computing # the respective bin index leads to a linearly increasing consecutive list. # The following assert statement succeeds: # b_b = (binx_xyz + nbins_c[0] * (biny_xyz + nbins_c[1] * # binz_xyz)).ravel() # assert (b_b == np.arange(np.prod(nbins_c))).all() # First atoms in pair. _first_at_neightuple_n = atoms_in_bin_ba[:, atom_pairs_pn[0]] for dz in range(-neigh_search_z, neigh_search_z + 1): for dy in range(-neigh_search_y, neigh_search_y + 1): for dx in range(-neigh_search_x, neigh_search_x + 1): # Bin index of neighboring bin and shift vector. shiftx_xyz, neighbinx_xyz = divmod(binx_xyz + dx, nbins_c[0]) shifty_xyz, neighbiny_xyz = divmod(biny_xyz + dy, nbins_c[1]) shiftz_xyz, neighbinz_xyz = divmod(binz_xyz + dz, nbins_c[2]) neighbin_b = ( neighbinx_xyz + nbins_c[0] * (neighbiny_xyz + nbins_c[1] * neighbinz_xyz)).ravel() # Second atom in pair. _secnd_at_neightuple_n = \ atoms_in_bin_ba[neighbin_b][:, atom_pairs_pn[1]] # Shift vectors. _cell_shift_vector_x_n = \ np.resize(shiftx_xyz.reshape(-1, 1), (max_natoms_per_bin**2, shiftx_xyz.size)).T _cell_shift_vector_y_n = \ np.resize(shifty_xyz.reshape(-1, 1), (max_natoms_per_bin**2, shifty_xyz.size)).T _cell_shift_vector_z_n = \ np.resize(shiftz_xyz.reshape(-1, 1), (max_natoms_per_bin**2, shiftz_xyz.size)).T # We have created too many pairs because we assumed each bin # has exactly max_natoms_per_bin atoms. Remove all surperfluous # pairs. Those are pairs that involve an atom with index -1. mask = np.logical_and(_first_at_neightuple_n != -1, _secnd_at_neightuple_n != -1) if mask.sum() > 0: first_at_neightuple_nn += [_first_at_neightuple_n[mask]] secnd_at_neightuple_nn += [_secnd_at_neightuple_n[mask]] cell_shift_vector_x_n += [_cell_shift_vector_x_n[mask]] cell_shift_vector_y_n += [_cell_shift_vector_y_n[mask]] cell_shift_vector_z_n += [_cell_shift_vector_z_n[mask]] # Flatten overall neighbor list. first_at_neightuple_n = np.concatenate(first_at_neightuple_nn) secnd_at_neightuple_n = np.concatenate(secnd_at_neightuple_nn) cell_shift_vector_n = np.transpose([ np.concatenate(cell_shift_vector_x_n), np.concatenate(cell_shift_vector_y_n), np.concatenate(cell_shift_vector_z_n) ]) # Add global cell shift to shift vectors cell_shift_vector_n += cell_shift_ic[first_at_neightuple_n] - \ cell_shift_ic[secnd_at_neightuple_n] # Remove all self-pairs that do not cross the cell boundary. if not self_interaction: m = np.logical_not( np.logical_and(first_at_neightuple_n == secnd_at_neightuple_n, (cell_shift_vector_n == 0).all(axis=1))) first_at_neightuple_n = first_at_neightuple_n[m] secnd_at_neightuple_n = secnd_at_neightuple_n[m] cell_shift_vector_n = cell_shift_vector_n[m] # For nonperiodic directions, remove any bonds that cross the domain # boundary. for c in range(3): if not pbc[c]: m = cell_shift_vector_n[:, c] == 0 first_at_neightuple_n = first_at_neightuple_n[m] secnd_at_neightuple_n = secnd_at_neightuple_n[m] cell_shift_vector_n = cell_shift_vector_n[m] # Sort neighbor list. i = np.argsort(first_at_neightuple_n) first_at_neightuple_n = first_at_neightuple_n[i] secnd_at_neightuple_n = secnd_at_neightuple_n[i] cell_shift_vector_n = cell_shift_vector_n[i] # Compute distance vectors. distance_vector_nc = positions[secnd_at_neightuple_n] - \ positions[first_at_neightuple_n] + \ cell_shift_vector_n.dot(cell) abs_distance_vector_n = \ np.sqrt(np.sum(distance_vector_nc*distance_vector_nc, axis=1)) # We have still created too many pairs. Only keep those with distance # smaller than max_cutoff. mask = abs_distance_vector_n < max_cutoff first_at_neightuple_n = first_at_neightuple_n[mask] secnd_at_neightuple_n = secnd_at_neightuple_n[mask] cell_shift_vector_n = cell_shift_vector_n[mask] distance_vector_nc = distance_vector_nc[mask] abs_distance_vector_n = abs_distance_vector_n[mask] if isinstance(cutoff, dict) and numbers is not None: # If cutoff is a dictionary, then the cutoff radii are specified per # element pair. We now have a list up to maximum cutoff. per_pair_cutoff_n = np.zeros_like(abs_distance_vector_n) for (atomic_number1, atomic_number2), c in cutoff.items(): try: atomic_number1 = atomic_numbers[atomic_number1] except KeyError: pass try: atomic_number2 = atomic_numbers[atomic_number2] except KeyError: pass if atomic_number1 == atomic_number2: mask = np.logical_and( numbers[first_at_neightuple_n] == atomic_number1, numbers[secnd_at_neightuple_n] == atomic_number2) else: mask = np.logical_or( np.logical_and( numbers[first_at_neightuple_n] == atomic_number1, numbers[secnd_at_neightuple_n] == atomic_number2), np.logical_and( numbers[first_at_neightuple_n] == atomic_number2, numbers[secnd_at_neightuple_n] == atomic_number1)) per_pair_cutoff_n[mask] = c mask = abs_distance_vector_n < per_pair_cutoff_n first_at_neightuple_n = first_at_neightuple_n[mask] secnd_at_neightuple_n = secnd_at_neightuple_n[mask] cell_shift_vector_n = cell_shift_vector_n[mask] distance_vector_nc = distance_vector_nc[mask] abs_distance_vector_n = abs_distance_vector_n[mask] elif not np.isscalar(cutoff): # If cutoff is neither a dictionary nor a scalar, then we assume it is # a list or numpy array that contains atomic radii. Atoms are neighbors # if their radii overlap. mask = abs_distance_vector_n < \ cutoff[first_at_neightuple_n] + cutoff[secnd_at_neightuple_n] first_at_neightuple_n = first_at_neightuple_n[mask] secnd_at_neightuple_n = secnd_at_neightuple_n[mask] cell_shift_vector_n = cell_shift_vector_n[mask] distance_vector_nc = distance_vector_nc[mask] abs_distance_vector_n = abs_distance_vector_n[mask] # Assemble return tuple. retvals = [] for q in quantities: if q == 'i': retvals += [first_at_neightuple_n] elif q == 'j': retvals += [secnd_at_neightuple_n] elif q == 'D': retvals += [distance_vector_nc] elif q == 'd': retvals += [abs_distance_vector_n] elif q == 'S': retvals += [cell_shift_vector_n] else: raise ValueError('Unsupported quantity specified.') if len(retvals) == 1: return retvals[0] else: return tuple(retvals)
def find_mic(D, cell, pbc=True): """Finds the minimum-image representation of vector(s) D""" cell = complete_cell(cell) # Calculate the 4 unique unit cell diagonal lengths diags = np.sqrt((np.dot([[1, 1, 1], [-1, 1, 1], [1, -1, 1], [-1, -1, 1], ], cell)**2).sum(1)) # calculate 'mic' vectors (D) and lengths (D_len) using simple method Dr = np.dot(D, np.linalg.inv(cell)) D = np.dot(Dr - np.round(Dr) * pbc, cell) D_len = np.sqrt((D**2).sum(1)) # return mic vectors and lengths for only orthorhombic cells, # as the results may be wrong for non-orthorhombic cells if (max(diags) - min(diags)) / max(diags) < 1e-9: return D, D_len # The cutoff radius is the longest direct distance between atoms # or half the longest lattice diagonal, whichever is smaller cutoff = min(max(D_len), max(diags) / 2.) # The number of neighboring images to search in each direction is # equal to the ceiling of the cutoff distance (defined above) divided # by the length of the projection of the lattice vector onto its # corresponding surface normal. a's surface normal vector is e.g. # b x c / (|b| |c|), so this projection is (a . (b x c)) / (|b| |c|). # The numerator is just the lattice volume, so this can be simplified # to V / (|b| |c|). This is rewritten as V |a| / (|a| |b| |c|) # for vectorization purposes. latt_len = np.sqrt((cell**2).sum(1)) V = abs(np.linalg.det(cell)) n = pbc * np.array(np.ceil(cutoff * np.prod(latt_len) / (V * latt_len)), dtype=int) # Construct a list of translation vectors. For example, if we are # searching only the nearest images (27 total), tvecs will be a # 27x3 array of translation vectors. This is the only nested loop # in the routine, and it takes a very small fraction of the total # execution time, so it is not worth optimizing further. tvecs = [] for i in range(-n[0], n[0] + 1): latt_a = i * cell[0] for j in range(-n[1], n[1] + 1): latt_ab = latt_a + j * cell[1] for k in range(-n[2], n[2] + 1): tvecs.append(latt_ab + k * cell[2]) tvecs = np.array(tvecs) # Translate the direct displacement vectors by each translation # vector, and calculate the corresponding lengths. D_trans = tvecs[np.newaxis] + D[:, np.newaxis] D_trans_len = np.sqrt((D_trans**2).sum(2)) # Find mic distances and corresponding vector(s) for each given pair # of atoms. For symmetrical systems, there may be more than one # translation vector corresponding to the MIC distance; this finds the # first one in D_trans_len. D_min_len = np.min(D_trans_len, axis=1) D_min_ind = D_trans_len.argmin(axis=1) D_min = D_trans[list(range(len(D_min_ind))), D_min_ind] return D_min, D_min_len