def getClosest(a, B): D = [] for b in B: d = getDistance(a._getCoords(), b._getCoords()) D.append(d) i = argmin(D) return B[i]
def getClosest(a, B): D = [] for b in B: d = getDistance(a._getCoords(), b._getCoords()) D.append(d) i = argmin(D) return B[i]
def calcDistance(atoms1, atoms2, unitcell=None): """Returns the Euclidean distance between *atoms1* and *atoms2*. Arguments may be :class:`~.Atomic` instances or NumPy arrays. Shape of numpy arrays must be ``([M,]N,3)``, where *M* is number of coordinate sets and *N* is the number of atoms. If *unitcell* array is provided, periodic boundary conditions will be taken into account. :arg atoms1: atom or coordinate data :type atoms1: :class:`.Atomic`, :class:`numpy.ndarray` :arg atoms2: atom or coordinate data :type atoms2: :class:`.Atomic`, :class:`numpy.ndarray` :arg unitcell: orthorhombic unitcell dimension array with shape ``(3,)`` :type unitcell: :class:`numpy.ndarray`""" if not isinstance(atoms1, ndarray): try: atoms1 = atoms1._getCoords() except AttributeError: raise TypeError('atoms1 must be Atomic instance or an array') if not isinstance(atoms2, ndarray): try: atoms2 = atoms2._getCoords() except AttributeError: raise TypeError('atoms2 must be Atomic instance or an array') if atoms1.shape[-1] != 3 or atoms2.shape[-1] != 3: raise ValueError('atoms1 and atoms2 must have shape ([M,]N,3)') if unitcell is not None: if not isinstance(unitcell, ndarray): raise TypeError('unitcell must be an array') elif unitcell.shape != (3, ): raise ValueError('unitcell.shape must be (3,)') return getDistance(atoms1, atoms2, unitcell)
def calcDistance(atoms1, atoms2, unitcell=None): """Returns the Euclidean distance between *atoms1* and *atoms2*. Arguments may be :class:`~.Atomic` instances or NumPy arrays. Shape of numpy arrays must be ``([M,]N,3)``, where *M* is number of coordinate sets and *N* is the number of atoms. If *unitcell* array is provided, periodic boundary conditions will be taken into account. :arg atoms1: atom or coordinate data :type atoms1: :class:`.Atomic`, :class:`numpy.ndarray` :arg atoms2: atom or coordinate data :type atoms2: :class:`.Atomic`, :class:`numpy.ndarray` :arg unitcell: orthorhombic unitcell dimension array with shape ``(3,)`` :type unitcell: :class:`numpy.ndarray`""" if not isinstance(atoms1, ndarray): try: atoms1 = atoms1._getCoords() except AttributeError: raise TypeError('atoms1 must be Atomic instance or an array') if not isinstance(atoms2, ndarray): try: atoms2 = atoms2._getCoords() except AttributeError: raise TypeError('atoms2 must be Atomic instance or an array') if atoms1.shape[-1] != 3 or atoms2.shape[-1] != 3: raise ValueError('atoms1 and atoms2 must have shape ([M,]N,3)') if unitcell is not None: if not isinstance(unitcell, ndarray): raise TypeError('unitcell must be an array') elif unitcell.shape != (3,): raise ValueError('unitcell.shape must be (3,)') return getDistance(atoms1, atoms2, unitcell)
def buildDistMatrix(atoms1, atoms2=None, unitcell=None, format='mat'): """Returns distance matrix. When *atoms2* is given, a distance matrix with shape ``(len(atoms1), len(atoms2))`` is built. When *atoms2* is **None**, a symmetric matrix with shape ``(len(atoms1), len(atoms1))`` is built. If *unitcell* array is provided, periodic boundary conditions will be taken into account. :arg atoms1: atom or coordinate data :type atoms1: :class:`.Atomic`, :class:`numpy.ndarray` :arg atoms2: atom or coordinate data :type atoms2: :class:`.Atomic`, :class:`numpy.ndarray` :arg unitcell: orthorhombic unitcell dimension array with shape ``(3,)`` :type unitcell: :class:`numpy.ndarray` :arg format: format of the resulting array, one of ``'mat'`` (matrix, default), ``'rcd'`` (arrays of row indices, column indices, and distances), or ``'arr'`` (only array of distances) :type format: bool""" if not isinstance(atoms1, ndarray): try: atoms1 = atoms1._getCoords() except AttributeError: raise TypeError('atoms1 must be Atomic instance or an array') if atoms2 is None: symmetric = True atoms2 = atoms1 else: symmetric = False if not isinstance(atoms2, ndarray): try: atoms2 = atoms2._getCoords() except AttributeError: raise TypeError('atoms2 must be Atomic instance or an array') if atoms2.ndim == 1: atoms2 = atoms2.reshape((1, 3)) if atoms1.shape[-1] != 3 or atoms2.shape[-1] != 3: raise ValueError('one and two must have shape ([M,]N,3)') if unitcell is not None: if not isinstance(unitcell, ndarray): raise TypeError('unitcell must be an array') elif unitcell.shape != (3, ): raise ValueError('unitcell.shape must be (3,)') dist = zeros((len(atoms1), len(atoms2))) if symmetric: if format not in DISTMAT_FORMATS: raise ValueError('format must be one of mat, rcd, or arr') if format == 'mat': for i, xyz in enumerate(atoms1[:-1]): dist[i, i + 1:] = dist[i + 1:, i] = getDistance(xyz, atoms2[i + 1:], unitcell) else: dist = concatenate([ getDistance(xyz, atoms2[i + 1:], unitcell) for i, xyz in enumerate(atoms1) ]) if format == 'rcd': n_atoms = len(atoms1) rc = array([(i, j) for i in range(n_atoms) for j in range(i + 1, n_atoms)]) row, col = rc.T dist = (row, col, dist) else: for i, xyz in enumerate(atoms1): dist[i] = getDistance(xyz, atoms2, unitcell) return dist
def buildDistMatrix(atoms1, atoms2=None, unitcell=None, format='mat'): """Returns distance matrix. When *atoms2* is given, a distance matrix with shape ``(len(atoms1), len(atoms2))`` is built. When *atoms2* is **None**, a symmetric matrix with shape ``(len(atoms1), len(atoms1))`` is built. If *unitcell* array is provided, periodic boundary conditions will be taken into account. :arg atoms1: atom or coordinate data :type atoms1: :class:`.Atomic`, :class:`numpy.ndarray` :arg atoms2: atom or coordinate data :type atoms2: :class:`.Atomic`, :class:`numpy.ndarray` :arg unitcell: orthorhombic unitcell dimension array with shape ``(3,)`` :type unitcell: :class:`numpy.ndarray` :arg format: format of the resulting array, one of ``'mat'`` (matrix, default), ``'rcd'`` (arrays of row indices, column indices, and distances), or ``'arr'`` (only array of distances) :type format: bool""" if not isinstance(atoms1, ndarray): try: atoms1 = atoms1._getCoords() except AttributeError: raise TypeError('atoms1 must be Atomic instance or an array') if atoms2 is None: symmetric = True atoms2 = atoms1 else: symmetric = False if not isinstance(atoms2, ndarray): try: atoms2 = atoms2._getCoords() except AttributeError: raise TypeError('atoms2 must be Atomic instance or an array') if atoms2.ndim == 1: atoms2 = atoms2.reshape((1,3)) if atoms1.shape[-1] != 3 or atoms2.shape[-1] != 3: raise ValueError('one and two must have shape ([M,]N,3)') if unitcell is not None: if not isinstance(unitcell, ndarray): raise TypeError('unitcell must be an array') elif unitcell.shape != (3,): raise ValueError('unitcell.shape must be (3,)') dist = zeros((len(atoms1), len(atoms2))) if symmetric: if format not in DISTMAT_FORMATS: raise ValueError('format must be one of mat, rcd, or arr') if format == 'mat': for i, xyz in enumerate(atoms1[:-1]): dist[i, i+1:] = dist[i+1:, i] = getDistance(xyz, atoms2[i+1:], unitcell) else: dist = concatenate([getDistance(xyz, atoms2[i+1:], unitcell) for i, xyz in enumerate(atoms1)]) if format == 'rcd': n_atoms = len(atoms1) rc = array([(i, j) for i in range(n_atoms) for j in range(i + 1, n_atoms)]) row, col = rc.T dist = (row, col, dist) else: for i, xyz in enumerate(atoms1): dist[i] = getDistance(xyz, atoms2, unitcell) return dist