def main(filename, imode, omode): ipos = open(filename, "r") ifr = 0 # extracts the dimension, its units and the cell_units from the first frame ret = read_file_raw(imode, ipos) ipos.close() comment = ret["comment"] cell_units = get_cell_units(comment, imode) key, dim, dim_units = get_key_dim_units(comment, imode) ipos = open(filename, "r") while True: try: ret = read_file(imode, ipos) pos = ret["atoms"] cell = ret["cell"] except EOFError: # finished reading files sys.exit(0) print_file( omode, pos, cell, filedesc=sys.stdout, title="", key=key, dimension=dim, units=dim_units, cell_units=cell_units, ) ifr += 1
def __init__(self, fixcom=False, fixatoms=None, intraj=None): """Initialises Replay. Args: dt: The simulation timestep. temp: The system temperature. fixcom: An optional boolean which decides whether the centre of mass motion will be constrained or not. Defaults to False. intraj: The input trajectory file. """ super(Replay, self).__init__(fixcom=fixcom, fixatoms=fixatoms) if intraj is None: raise ValueError( "Must provide an initialized InitFile object to read trajectory from" ) self.intraj = intraj if intraj.mode == "manual": raise ValueError( "Replay can only read from PDB or XYZ files -- or a single frame from a CHK file" ) # Posibility to read beads from separate XYZ files by a wildcard if any(char in self.intraj.value for char in "*?[]"): infilelist = [] for file in sorted(os.listdir(".")): if fnmatch(file, self.intraj.value): infilelist.append(file) # determine bead numbers in input files bead_map_list = [] for file in infilelist: fdin = open(file, "r") rr = read_file_raw(self.intraj.mode, fdin) metainfo = rr["comment"].split() for i, word in enumerate(metainfo): if word == "Bead:": bead_map_list.append(int(metainfo[i + 1])) fdin.close() # check that beads are continuous (no files missing) if sorted(bead_map_list) != list(range(len(bead_map_list))): info( "ATTENTION: Provided trajectory files have non-sequential " "range of bead indices.\n" "\tIndices found: %s\n" "\tMake sure that the wildcard does what it's supposed to do." % str(bead_map_list), verbosity.low, ) # sort the list of files according to their bead indices infilelist_sorted, _ = zip( *sorted(zip(infilelist, bead_map_list), key=lambda t: t[1])) self.rfile = [open(f, "r") for f in infilelist_sorted] else: # no wildcard self.rfile = open(self.intraj.value, "r") self.rstep = 0
def main(filename, imode, omode): ipos = open(filename, "r") natoms = 0 ifr = 0 # extracts the dimension, its units and the cell_units from the first frame ret = read_file_raw(imode, ipos) ipos.close() comment = ret['comment'] cell_units = get_cell_units(comment, imode) key, dim, dim_units = get_key_dim_units(comment, imode) ipos = open(filename, "r") while True: try: ret = read_file(imode, ipos) pos = ret["atoms"] cell = ret["cell"] except EOFError: # finished reading files sys.exit(0) print_file(omode, pos, cell, filedesc=sys.stdout, title="", key=key, dimension=dim, units=dim_units, cell_units=cell_units) ifr += 1
def compute_acf(input_file, output_prefix, maximum_lag, block_length, length_zeropadding, spectral_windowing, labels, timestep, skip, der): # stores the arguments ifile = str(input_file) ofile = str(output_prefix) mlag = int(maximum_lag) bsize = int(block_length) npad = int(length_zeropadding) ftbox = str(spectral_windowing) labels = str(labels) timestep = str(timestep).split() fskip = int(skip) # checks for errors if (mlag <= 0): raise ValueError("MAXIMUM_LAG should be a non-negative integer.") if (npad < 0): raise ValueError( "LENGTH_ZEROPADDING should be a non-negative integer.") if (bsize < 2 * mlag): if (bsize == -1): bsize = 2 * mlag else: raise ValueError( "LENGTH_BLOCK should be greater than or equal to 2 * MAXIMUM_LAG." ) # reads one frame. ff = open(ifile) rr = read_file_raw("xyz", ff) ff.close() # appends "der" to output file in case the acf of the derivative is desired if (der == True): ofile = ofile + "_der" # stores the indices of the "chosen" atoms. ndof = len(rr['data']) if ("*" in labels): labelbool = np.ones(ndof / 3, bool) else: labelbool = np.zeros(ndof / 3, bool) for l in labels: labelbool = np.logical_or(labelbool, rr['names'] == l) nspecies = labelbool.sum() # initializes variables. nblocks = 0 dt = unit_to_internal("time", timestep[1], float(timestep[0])) data = np.zeros((bsize, nspecies, 3), float) time = np.asarray(list(range(mlag + 1))) * dt omega = np.asarray(list(range(2 * (mlag + npad)))) / float( 2 * (mlag + npad)) * (2 * np.pi / dt) fvvacf = omega.copy() * 0.0 fvvacf2 = fvvacf.copy() * 0.0 vvacf = time.copy() * 0.0 vvacf2 = time.copy() * 0.0 # selects window function for fft. if (ftbox == "none"): win = np.ones(2 * mlag + 1, float) elif (ftbox == "cosine-hanning"): win = np.hanning(2 * mlag + 1) elif (ftbox == "cosine-hamming"): win = np.hamming(2 * mlag + 1) elif (ftbox == "cosine-blackman"): win = np.blackman(2 * mlag + 1) elif (ftbox == "triangle-bartlett"): win = np.bartlett(2 * mlag + 1) ff = open(ifile) # Skips the first fskip frames for x in range(fskip): rr = read_file_raw("xyz", ff) while True: try: # Reads the data in blocks. for i in range(bsize): rr = read_file_raw("xyz", ff) data[i] = rr['data'].reshape((ndof / 3, 3))[labelbool] if (der == True): data = np.gradient(data, axis=0) / dt # Computes the Fourier transform of the data. fdata = np.fft.rfft(data, axis=0) # Computes the Fourier transform of the vvac applying the convolution theorem. tfvvacf = fdata * np.conjugate(fdata) # Averages over all species and sums over the x,y,z directions. Also multiplies with the time step and a prefactor of (2pi)^-1. mfvvacf = 3.0 * np.real(np.mean( tfvvacf, axis=(1, 2))) * dt / (2 * np.pi) / bsize # Computes the inverse Fourier transform to get the vvac. mvvacf = np.fft.irfft(mfvvacf)[:mlag + 1] # Applies window in one direction and pads the vvac with zeroes. mpvvacf = np.append(mvvacf * win[mlag:], np.zeros(npad)) # Recomputes the Fourier transform assuming the data is an even function of time. mfpvvacf = np.fft.hfft(mpvvacf) # Accumulates the (f)acfs and their squares. fvvacf += mfpvvacf fvvacf2 += mfpvvacf**2 vvacf += mvvacf vvacf2 += mvvacf**2 nblocks += 1 except EOFError: break ff.close() # Performs the block average of the Fourier transform. fvvacf = fvvacf / nblocks fvvacf_err = np.sqrt(fvvacf2 / nblocks - fvvacf**2) np.savetxt(ofile + "_facf.data", np.c_[omega, fvvacf, fvvacf_err][:mlag + npad]) # Computes the inverse Fourier transform to get the vvac. vvacf = vvacf / nblocks vvacf_err = np.sqrt(vvacf2 / nblocks - vvacf**2) np.savetxt(ofile + "_acf.data", np.c_[time, vvacf, vvacf_err][:mlag + npad])
def compute_acf(input_file, output_prefix, maximum_lag, block_length, length_zeropadding, spectral_windowing, labels, timestep, skip, der): # stores the arguments ifile = str(input_file) ofile = str(output_prefix) mlag = int(maximum_lag) bsize = int(block_length) npad = int(length_zeropadding) ftbox = str(spectral_windowing) labels = str(labels) timestep = str(timestep).split() fskip = int(skip) # checks for errors if(mlag <= 0): raise ValueError("MAXIMUM_LAG should be a non-negative integer.") if(npad < 0): raise ValueError("LENGTH_ZEROPADDING should be a non-negative integer.") if(bsize < 2 * mlag): if(bsize == -1): bsize = 2 * mlag else: raise ValueError("LENGTH_BLOCK should be greater than or equal to 2 * MAXIMUM_LAG.") # reads one frame. ff = open(ifile) rr = read_file_raw("xyz", ff) ff.close() # appends "der" to output file in case the acf of the derivative is desired if(der == True): ofile = ofile + "_der" # stores the indices of the "chosen" atoms. ndof = len(rr['data']) if("*" in labels): labelbool = np.ones(ndof / 3, bool) else: labelbool = np.zeros(ndof / 3, bool) for l in labels: labelbool = np.logical_or(labelbool, rr['names'] == l) nspecies = labelbool.sum() # initializes variables. nblocks = 0 dt = unit_to_internal("time", timestep[1], float(timestep[0])) data = np.zeros((bsize, nspecies, 3), float) fvvacf = np.zeros(bsize / 2 + 1, float) time = np.asarray(range(mlag + 1)) * dt omega = np.asarray(range(2 * (mlag + npad))) / float(2 * (mlag + npad)) * (2 * np.pi / dt) # selects window function for fft. if(ftbox == "none"): win = np.ones(2 * mlag + 1, float) elif(ftbox == "cosine-hanning"): win = np.hanning(2 * mlag + 1) elif(ftbox == "cosine-hamming"): win = np.hamming(2 * mlag + 1) elif(ftbox == "cosine-blackman"): win = np.blackman(2 * mlag + 1) elif(ftbox == "triangle-bartlett"): win = np.bartlett(2 * mlag + 1) ff = open(ifile) # Skips the first fskip frames for x in xrange(fskip): rr = read_file_raw("xyz", ff) while True: try: # Reads the data in blocks. for i in range(bsize): rr = read_file_raw("xyz", ff) data[i] = rr['data'].reshape((ndof / 3, 3))[labelbool] if(der == True): data = np.gradient(data, axis=0) / dt # Computes the Fourier transform of the data. fdata = np.fft.rfft(data, axis=0) # Computes the Fourier transform of the vvac applying the convolution theorem. tfvvacf = fdata * np.conjugate(fdata) # Averages over all species and sums over the x,y,z directions. Also multiplies with the time step and a prefactor of (2pi)^-1. macf = 3.0 * np.real(np.mean(tfvvacf, axis=(1, 2))) * dt / (2 * np.pi) / bsize fvvacf += macf nblocks += 1 except EOFError: break ff.close() # Performs the block average of the Fourier transform. fvvacf = fvvacf / nblocks # Computes the inverse Fourier transform to get the vvac. vvacf = np.fft.irfft(fvvacf)[:mlag + 1] np.savetxt(ofile + "_acf.data", np.vstack((time, vvacf)).T[:mlag + npad]) # Applies window in one direction and pads the vvac with zeroes. pvvacf = np.append(vvacf * win[mlag:], np.zeros(npad)) # Recomputes the Fourier transform assuming the data is an even function of time. fpvvacf = np.fft.hfft(pvvacf) np.savetxt(ofile + "_facf.data", np.vstack((omega, fpvvacf)).T[:mlag + npad])