def parser(name, data): """ Parse xyz files (angstrom) """ # create list of mol, trajectory support tmol = Molecule(name, steps=0) i = 0 while i < len(data): # handle empty lines at eof or between molecules if not data[i].strip().isdigit(): i += 1 continue # fixed format nat and comment tmol.newStep() nat = int(data[i]) tmol.comment = data[i + 1].strip() # read coordinates and types tmol.newAtoms(nat) for j in range(nat): line = data[j + i + 2].split() tmol.setAtom(j, line[0], line[1:4]) tmol.setFmt('angstrom', scale=True) i += nat + 2 return tmol, None
def parser(name, data): """ Parse Lammps custom dump files Preliminary implementation! Needs to be 'custom' format with either: 'id element xs ys yz' or 'id element x y z' Only orthogonal cells for now Assumes angstrom """ tmol = Molecule(name, steps=0) i = 0 while i < len(data): line = data[i] if 'ITEM' in line: if 'TIMESTEP' in line: i += 2 tmol.newStep() elif 'NUMBER OF ATOMS' in line: nat = int(data[i + 1]) i += 2 elif 'BOX BOUNDS' in line: tvec = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] tvec[0][0] = float(data[i + 1].split()[1]) -\ float(data[i + 1].split()[0]) tvec[1][1] = float(data[i + 2].split()[1]) -\ float(data[i + 2].split()[0]) tvec[2][2] = float(data[i + 3].split()[1]) -\ float(data[i + 3].split()[0]) tmol.setVec(tvec) tmol.setCellDim(1, fmt='angstrom') i += 4 elif 'ATOMS' in line: line = line.split() if 'id' not in line or 'element' not in line or\ ('xs' not in line and 'x' not in line): raise NotImplementedError("Lammps dump in not (yet) " "recognized format") # ididx = line.index('id') - 2 elidx = line.index('element') - 2 if 'xs' in line: xidx = line.index('xs') - 2 yidx = line.index('ys') - 2 zidx = line.index('zs') - 2 fmt = 'crystal' else: xidx = line.index('x') - 2 yidx = line.index('y') - 2 zidx = line.index('z') - 2 fmt = 'angstrom' if 'q' in line: qidx = line.index('q') - 2 else: qidx = False tmol.newAtoms(nat) for j in range(nat): at = data[j + 1 + i].split() if qidx: tmol.setAtom(j, at[elidx], [at[xidx], at[yidx], at[zidx]], charge=float(at[qidx])) else: tmol.setAtom(j, at[elidx], [at[xidx], at[yidx], at[zidx]]) tmol.setFmt(fmt, scale=True) i += nat + 1 else: i += 1 return tmol, None
def parser(name, data): """ Parse PWScf output to trajectory """ tmol = Molecule(name, steps=0) i = 0 vec = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] gamma = False while i < len(data): line = data[i].split() # ignore empty lines if not line: pass # read number of atoms elif line[0:3] == ['number', 'of', 'atoms/cell']: nat = int(line[4]) # read cell dimension elif line[0] == 'celldm(1)=': celldm = float(line[1]) # read initial cell vectors elif line[0:2] == ['crystal', 'axes:']: for j in [0, 1, 2]: temp = data[i + 1 + j].split() vec[j] = [float(x) for x in temp[3:6]] # read initial positions: elif line[0] == 'site': tmol.newStep() tmol.setCellDim(celldm) tmol.setVec(vec) tmol.newAtoms(nat) for j in range(nat): atom = data[j + i + 1].split() tmol.setAtom(j, atom[1], atom[6:9]) tmol.setFmt('alat', scale=True) i += nat # read k-points: elif line[0] == 'gamma-point': gamma = True elif line[0:3] == ['number', 'of', 'k'] and not gamma: nk = int(line[4]) if nk < 100: kpoints = [] for j in range(i + 2, i + nk + 2): kp = data[j].split() kpoints.append([kp[4], kp[5], kp[6].strip('), '), kp[9]]) tmol.setKpoints('discrete', kpoints) tmol.setKpoints('active', 'discrete') i += nk # read step-vectors if cell is variable elif line[0] == 'CELL_PARAMETERS': for j in [0, 1, 2]: temp = data[i + 1 + j].split() vec[j] = [float(x) for x in temp[0:3]] # read step-coordinates elif line[0] == 'ATOMIC_POSITIONS': tmol.newStep() tmol.setCellDim(celldm) tmol.setVec(vec) tmol.newAtoms(nat) for j in range(nat): atom = data[j + i + 1].split() tmol.setAtom(j, atom[0], atom[1:4], fix=[not int(x) for x in atom[4:]]) tmol.setFmt(line[1].strip('()'), scale=True) i += nat # break on reaching final coordinates (duplicate) elif line[0] == 'Begin': break # ignore everything else else: pass i += 1 return tmol, None