def test_distarray(self): from MDAnalysis.core.distances import distance_array from MDAnalysis.core.distances import transform_StoR, transform_RtoS R_mol1 = transform_StoR(self.S_mol1, self.box) R_mol2 = transform_StoR(self.S_mol2, self.box) # Try with box dists = distance_array(R_mol1, R_mol2, box=self.box) # Manually calculate distance_array manual = np.zeros((len(R_mol1), len(R_mol2))) for i, Ri in enumerate(R_mol1): for j, Rj in enumerate(R_mol2): Rij = Rj - Ri Rij -= round(Rij[2] / self.box[2][2]) * self.box[2] Rij -= round(Rij[1] / self.box[1][1]) * self.box[1] Rij -= round(Rij[0] / self.box[0][0]) * self.box[0] Rij = np.linalg.norm(Rij) # find norm of Rij vector manual[i][j] = Rij assert_almost_equal(dists, manual, self.prec, err_msg="distance_array failed with box") # Now check using boxV dists = distance_array(R_mol1, R_mol2, box=self.boxV) assert_almost_equal(dists, manual, self.prec, err_msg="distance_array failed with boxV")
def contact_matrix(coord, cutoff=15.0, returntype="numpy", box=None, progress_meter_freq=100, suppress_progmet=False): '''Calculates a matrix of contacts between a list of coordinates. There is a fast, high-memory-usage version for small systems (*returntype* = 'numpy'), and a slower, low-memory-usage version for larger systems (*returntype* = 'sparse'). If *box* dimensions are passed (``box = [Lx, Ly, Lz]``), then periodic boundary conditions are applied. Only orthorhombic boxes are currently supported. Change *progress_meter_freq* to alter frequency of progress meter updates. Or switch *suppress_progmet* to ``True`` to suppress it completely. ''' if returntype == "numpy": adj = (distance_array(coord, coord, box=box) < cutoff) return adj elif returntype == "sparse": # Initialize square List of Lists matrix of dimensions equal to number of coordinates passed sparse_contacts = sparse.lil_matrix((len(coord), len(coord)), dtype='bool') # if PBC # TODO Jan: this distance matrix will be symmetric, hence some of the iterations could be skipped. if box is not None: contact_matrix_pbc(coord, sparse_contacts, box, cutoff, progress_meter_freq, suppress_progmet) # if no PBC else: contact_matrix_no_pbc(coord, sparse_contacts, cutoff, progress_meter_freq, suppress_progmet) return sparse_contacts
def process(self): self.vol += self.processor.currsnap.volume if self.usecom: refcom = np.asarray([self.ref.centerOfGeometry()]) for i,sel in enumerate(self.sels): selcom = np.asarray([r.centerOfGeometry() for r in sel.residues]) dist = mddist.distance_array(refcom, sel.positions, self.processor.currbox) h, e = np.histogram(dist,bins=self.edges) self.densities[i] += h else: for i,(sel,dist) in enumerate(zip(self.sels,self.dists)): mddist.distance_array(self.ref.positions,sel.positions, self.processor.currbox,dist) h, e = np.histogram(dist,bins=self.edges) self.densities[i] += h
def test_pbc_dist(self): from MDAnalysis.core.distances import distance_array results = np.array([[37.629944]]) dists = distance_array(self.S_mol1, self.S_mol2, box=self.boxV) assert_almost_equal(dists, results, self.prec, err_msg="distance_array failed to retrieve PBC distance")
def process(self): self.vol += self.processor.currsnap.volume if self.usecom: refcom = np.asarray([self.ref.centerOfGeometry()]) for i, sel in enumerate(self.sels): selcom = np.asarray( [r.centerOfGeometry() for r in sel.residues]) dist = mddist.distance_array(refcom, sel.positions, self.processor.currbox) h, e = np.histogram(dist, bins=self.edges) self.densities[i] += h else: for i, (sel, dist) in enumerate(zip(self.sels, self.dists)): mddist.distance_array(self.ref.positions, sel.positions, self.processor.currbox, dist) h, e = np.histogram(dist, bins=self.edges) self.densities[i] += h
def _single_run(self, start, stop): """Perform a single pass of the trajectory""" self.u.trajectory[start] # Calculate partners at t=0 box = self.u.dimensions if self.pbc else None # 2d array of all distances d = distance_array(self.h.positions, self.a.positions, box=box) if self.exclusions: # set to above dist crit to exclude d[self.exclusions] = self.d_crit + 1.0 # find which partners satisfy distance criteria hidx, aidx = numpy.where(d < self.d_crit) a = calc_angles(self.d.positions[hidx], self.h.positions[hidx], self.a.positions[aidx], box=box) # from amongst those, who also satisfiess angle crit idx2 = numpy.where(a > self.a_crit) hidx = hidx[idx2] aidx = aidx[idx2] nbonds = len(hidx) # number of hbonds at t=0 results = numpy.zeros_like(numpy.arange(start, stop, self._skip), dtype=numpy.float32) if self.time_cut: # counter for time criteria count = numpy.zeros(nbonds, dtype=numpy.float64) for i, ts in enumerate(self.u.trajectory[start:stop:self._skip]): box = self.u.dimensions if self.pbc else None d = calc_bonds(self.h.positions[hidx], self.a.positions[aidx], box=box) a = calc_angles(self.d.positions[hidx], self.h.positions[hidx], self.a.positions[aidx], box=box) winners = (d < self.d_crit) & (a > self.a_crit) results[i] = winners.sum() if self.bond_type is 'continuous': # Remove losers for continuous definition hidx = hidx[numpy.where(winners)] aidx = aidx[numpy.where(winners)] elif self.bond_type is 'intermittent': if self.time_cut: # Add to counter of where losers are count[~winners] += self._skip * self.u.trajectory.dt count[winners] = 0 # Reset timer for winners # Remove if you've lost too many times # New arrays contain everything but removals hidx = hidx[count < self.time_cut] aidx = aidx[count < self.time_cut] count = count[count < self.time_cut] else: pass if len(hidx) == 0: # Once everyone has lost, the fun stops break results /= nbonds return results
S4list = [] listoftaus = [] for ts in u.trajectory: if ts.frame > start or x == 1: neighborlistlist = [] normvec = np.array([0, 0, 1]) normvecmatrix = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) pm.echo(ts.frame) coords = a.coordinates() boxvectors = MD.coordinates.core.triclinic_vectors(ts.dimensions) boxvector = boxvectors[boxvectors > 0] boxvectorcheck = np.diag(boxvectors) d = distance_array(coords, coords, boxvectors, distancearray) if np.shape(boxvector) != np.shape(boxvectorcheck): print "The box is not orthorhombic. I am leaving." sys.exit() neighborlist = nextneighborlist(d) S6, S4 = latticeorderparameter(coords, neighborlist, boxvector, normvecmatrix) S6list.append(S6) S4list.append(S4) # print normvec projcoordinatestemp = np.dot(coords, normvec) projcoordinates = projcoordinatestemp - np.average(projcoordinatestemp) # print projcoordinates tau, layerdistance = transorderparameter(projcoordinates, 1, 0.5 * np.dot(boxvector, normvec), 200) listoftaus.append(tau)
def _single_run(self, start, stop): """Perform a single pass of the trajectory""" self.u.trajectory[start] # Calculate partners at t=0 box = self.u.dimensions if self.pbc else None # 2d array of all distances d = distance_array(self.h.positions, self.a.positions, box=box) if self.exclusions: # set to above dist crit to exclude d[self.exclusions] = self.d_crit + 1.0 # find which partners satisfy distance criteria hidx, aidx = numpy.where(d < self.d_crit) a = calc_angles(self.d.positions[hidx], self.h.positions[hidx], self.a.positions[aidx], box=box) # from amongst those, who also satisfiess angle crit idx2 = numpy.where(a > self.a_crit) hidx = hidx[idx2] aidx = aidx[idx2] nbonds = len(hidx) # number of hbonds at t=0 results = numpy.zeros_like(numpy.arange(start, stop, self._skip), dtype=numpy.float32) if self.time_cut: # counter for time criteria count = numpy.zeros(nbonds, dtype=numpy.float64) for i, ts in enumerate(self.u.trajectory[start : stop : self._skip]): box = self.u.dimensions if self.pbc else None d = calc_bonds(self.h.positions[hidx], self.a.positions[aidx], box=box) a = calc_angles(self.d.positions[hidx], self.h.positions[hidx], self.a.positions[aidx], box=box) winners = (d < self.d_crit) & (a > self.a_crit) results[i] = winners.sum() if self.bond_type is "continuous": # Remove losers for continuous definition hidx = hidx[numpy.where(winners)] aidx = aidx[numpy.where(winners)] elif self.bond_type is "intermittent": if self.time_cut: # Add to counter of where losers are count[~winners] += self._skip * self.u.trajectory.dt count[winners] = 0 # Reset timer for winners # Remove if you've lost too many times # New arrays contain everything but removals hidx = hidx[count < self.time_cut] aidx = aidx[count < self.time_cut] count = count[count < self.time_cut] else: pass if len(hidx) == 0: # Once everyone has lost, the fun stops break results /= nbonds return results
S4list = [] listoftaus = [] for ts in u.trajectory: if ts.frame > start or x == 1: neighborlistlist = [] normvec = np.array([0, 0, 1]) normvecmatrix = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) pm.echo(ts.frame) coords = a.coordinates() boxvectors = MD.coordinates.core.triclinic_vectors(ts.dimensions) boxvector = boxvectors[boxvectors > 0] boxvectorcheck = np.diag(boxvectors) d = distance_array(coords, coords, boxvectors, distancearray) if np.shape(boxvector) != np.shape(boxvectorcheck): print "The box is not orthorhombic. I am leaving." sys.exit() neighborlist = nextneighborlist(d) S6, S4 = latticeorderparameter(coords, neighborlist, boxvector, normvecmatrix) S6list.append(S6) S4list.append(S4) #print normvec projcoordinatestemp = np.dot(coords, normvec) projcoordinates = projcoordinatestemp - np.average(projcoordinatestemp) #print projcoordinates tau, layerdistance = transorderparameter( projcoordinates, 1, 0.5 * np.dot(boxvector, normvec), 200)