def rdump_m(filename): steps = [] with open(filename, 'r') as file: while True: line = file.readline() if not line: break line = file.readline() count_step = int(line.split()[0]) for i in range(2): line = file.readline() natoms = int(line.split()[0]) file.readline() box = [] for i in range(3): line = file.readline() box.append([float(word) for word in line.split()]) line = file.readline() kws = line.split()[2:] atoms = {} for i in range(natoms): line = file.readline() words = line.split() properties = words atom = Atom_m(properties) atoms[atom.get_property('id',kws)] = atom step = mt.Step(atoms, count_step, box) steps.append(step) return steps,kws
def convert_to_dump(pkas): import numpy as np atoms = {} for pka in pkas: atom = mt.Atom({ 'id': pka[0], 'x': pka[2] * 1e10, 'y': pka[3] * 1e10, 'z': pka[4] * 1e10, 'E': pka[5], 'time': pka[1] * 1e6 }) atoms[atom.id] = atom step = mt.Step(atoms, 0, np.array(box) * 1e10) steps = [step] mt.wdump(steps, 'pkas.dump', ['id', 'x', 'y', 'z', 'E', 'time'])
def sia_va(refdump, cascadedump, siavadump): steps = mt.rdump(refdump) refatoms = steps[0].atoms with open(cascadedump, 'r') as file: cascade_lines = file.readlines() with open(siavadump, 'w') as file: nline = 0 nstep = 0 while nline < len(cascade_lines): for i in range(4): line = cascade_lines[nline] file.write(line) nline += 1 ndefects = int(line.split()[0]) for i in range(4): line = cascade_lines[nline] file.write(line) nline += 1 for i in range(1): line = "ITEM: ATOMS id type x y z c_2[1]\n" file.write(line) nline += 1 for i in range(ndefects): line = cascade_lines[nline] words = line.split() id = int(words[0]) r = refatoms[id].r words = words[:2] + [str(xs) for xs in r] + words[2:] words[2:5] = [str(xs) for xs in r] line = "{0} {1} {2} {3} {4} {5}\n".format( words[0], words[1], words[2], words[3], words[4], words[5]) file.write(line) nline += 1 nstep += 1 if (nstep % 100 == 0): print(nstep)
def rdump(filename): steps = [] with open(filename, 'r') as file: while True: line = file.readline() if not line: break line = file.readline() count_step = int(line.split()[0]) for i in range(2): line = file.readline() natoms = int(line.split()[0]) file.readline() box = [] for i in range(3): line = file.readline() box.append([float(word) for word in line.split()]) line = file.readline() kws = line.split()[3:] dir = {} for kw in kws: dir[kw] = [] index = [] for i in range(natoms): line = file.readline() words = [float(word) for word in line.split()] for word, kw in zip(words[1:], kws): dir[kw].append(word) index.append(int(words[0])) if i % 100000 == 0: print('Loading atoms:', i) atoms = pd.DataFrame(dir, index) dir = {} step = mt.Step(atoms, count_step, box) steps.append(step) return steps
import MDtools as mt import math import numpy as np print('processing... don\'t touch me\n') atoms_cell = mt.rdump('cell.dump')[-1].atoms atoms_sia = mt.rdump('sia_lammps.dump')[-1].atoms print('reading completed.') print('start computing...') def c_distance(r1, r2): return math.sqrt(np.dot(r1 - r2, r1 - r2)) def find_neibour(self, atoms): atoms_sorted = sorted( atoms.items(), key=lambda item: c_distance(item[1].r, self.r)) self.neibours = [atoms_sorted[0][1], atoms_sorted[1][1]] def sia_dirction(self): dr = self.neibours[1].r - self.neibours[0].r maxr = max(abs(dr)) for i in range(3): if abs(dr[i]) / maxr < 0.3: dr[i] = 0 self.dirction = [int(i / abs(i)) if i != 0 else 0 for i in dr] self.properties['c_2[1]'] = int(self.properties['c_2[1]']) if self.properties['c_2[1]'] == 0:
ave_msd[key] += value for key, value in ave_msd.items(): ave_msd[key] /= len(ids) return ave_msd def compute_diff_coe(msddata): ''' unit: unit: Angstrom^2/ps ''' from scipy import stats times = [] x2s = [] for time, x2 in msddata.items(): times.append(time) x2s.append(x2) linregress = stats.linregress(times, x2s) diff_coe = linregress[0] r2 = linregress[2] return diff_coe, r2 if __name__ == '__main__': import MDtools as mt steps = mt.rdump('H1.dump') msd = compute_msd(steps, [2001], istep=1000, ave_level=100, nplot=5) coe = compute_diff_coe(msd) print(coe) for k, v in msd.items(): print(k, v)
addids.append(id) break elif (np.array([atom.properties['d1'], atom.properties['d2'], atom.properties['d3']]) == -1 * self.direction).all(): addids.append(id) break for id in addids: self.add_member(atoms[id]) if len(addids) == 0: self.fulled = 1 def divide_atoms(atoms, cut): groups = [] for id, atom in atoms.items(): atom.fresh() for id, atom in atoms.items(): if not hasattr(atom, 'swallowed'): group = Group({atom.id: atom}) group.swallow(atoms, cut) groups.append(group) return groups if __name__ == '__main__': steps = mt.rdump('loop.dump') atoms = steps[0].atoms groups = divide_atoms(atoms, 10) print(len(groups)) print(len(groups[0].members)) print(groups[1].size)
import MDtools as mt import numpy as np import math import matplotlib.pyplot as plt import os def distance(r1, r2): return math.sqrt((r2[0] - r1[0])**2 + (r2[1] - r1[1])**2 + (r2[2] - r1[2])**2) steps = mt.rdump('cell_direction.dump') atoms = steps[0].atoms s_center = np.array([0., 0., 0.]) s_n = 0 v_center = np.array([0., 0., 0.]) v_n = 0 for id_, atom in atoms.items(): if atom.properties['c_2[1]'] >= 2: s_center += atom.r s_n += 1 else: v_center += atom.r v_n += 1 s_center /= s_n v_center /= v_n
import numpy as np from numpy import linalg as LA def compute_pressure(steps, c_stress='c_2'): # unit: bar*A^3 for step in steps: for id_, atom in step.atoms.items(): sts = [ atom.properties[c_stress + '[{0}]'.format(i)] for i in range(1, 7) ] stress_mat = np.mat([[sts[0], sts[3], sts[5]], [sts[3], sts[1], sts[4]], [sts[5], sts[4], sts[2]]]) eig_mat = LA.eig(stress_mat)[1] diag_mat = eig_mat.I * stress_mat * eig_mat pressure_v = np.array(diag_mat)[0][0] + np.array( diag_mat)[1][1] + np.array(diag_mat)[2][2] atom.properties['ps_v'] = pressure_v if __name__ == '__main__': import MDtools as mt filename = input('File name: ') steps = mt.rdump(filename) compute_pressure(steps) mt.wdump(steps, 'ps_v.' + filename)
import MDtools as mt steps = mt.rdump('cell.dump') displacements = [] for step in steps: displacement = 0 for k,atom in step.atoms.items(): if atom.properties['c_2[1]'] == 0: displacement += 1 displacements.append(displacement) steps = [] times = [] with open('log.lammps','r') as log: flag = 0 while True: # 这有点难 ( ╯□╰ ) 谔谔 line = log.readline() if line == "Step Time Dt Temp \n": flag += 1 elif line[:8] == "Fix halt": break elif flag == 2: words = [float(word) for word in line.split()] time = words[1] - 1.0 times.append(time) with open('results/process.data','w') as file: for time,displacement in zip(times,displacements): file.write('{0} {1}\n'.format(time, displacement))