def to_xyz(self, fn=None): ''' Method to write the given trajectory to an XYZ file. This method assumes that the coords attribute has been assigned. **Optional Arguments** fn a string defining the name of the output file ''' if 'active' in list(self.__dict__.keys()) and not self.active: return if fn is None: fn = 'trajectory-%s-%i.xyz' %(self.term.basename.replace('/', '-'),self.term.index) f = open(fn, 'w') xyz = XYZWriter(f, [pt[Z].symbol for Z in self.numbers]) for frame, coord in enumerate(self.coords): xyz.dump('frame %i' %frame, coord) f.close()
def write_principal_mode(f, f_pca, index, n_frames=100, select=None, mw=True, scaling=1.): """ Writes out one xyz file per principal mode given in index **Arguments:** f Path to an h5.File instance containing the original data. f_pca Path to an h5.File instance containing the PCA, with reference structure, eigenvalues and principal modes. index An array containing the principal modes which need to be written out. **Optional arguments:** n_frames The number of frames in each xyz file. select A list of atom indexes that are considered for the computation of the spectrum. If not given, all atoms are used. mw If mass_weighted is True, the covariance matrix is assumed to be mass-weighted. scaling Scaling factor applied to the maximum deviation of the principal mode (i.e. the maximum principal component for that mode) """ # Load in the relevant data # Atom numbers, masses and initial frame numbers = f['system/numbers'] masses = f['system/masses'] pos = f['trajectory/pos'] if select is not None: numbers = numbers[select] masses = masses[select] pos = pos[:, select, :] masses = np.repeat(masses, 3) # Data from the PC analysis grp = f_pca['pca'] # The selected principal modes pm = grp['pm'][:, index] # The corresponding eigenvalues eigval = grp['eigvals'][index] # And the principal components pc = grp['pc'][:, index] with log.section('PCA'): for i in xrange(len(index)): log('Writing out principal mode %s' % index[i]) if eigval[i] < 0: Warning('Negative eigenvalue encountered, skipping this entry') break # Determine maximum fluctuation (in units of meter*sqrt(kilogram)) max_fluct = np.max(np.abs(pc[:, i])) # Initialize XYZWriter from molmod package xw = XYZWriter('pm_%s.xyz' % index[i], [pd[number].symbol for number in numbers]) # Determine index in trajectory closest to rest state, and the corresponding positions ind_min = np.argmin(np.abs(pc[:, i])) r_ref = pos[ind_min, :, :] for j in xrange(n_frames): q_var = scaling * pm[:, i] * max_fluct * (2. * j - n_frames) / n_frames if mw: q_var /= np.sqrt(masses) r = r_ref + q_var.reshape(-1, 3) xw.dump('Frame%s' % j, r) del xw