def __init__(self, frameStr=None): """ If second parameter is not given, constructs empty frame, otherwise constructs frame from the string which satisfies following pattern <number of atoms> comment line atom_symbol11 x-coord11 y-coord11 z-coord11 atom_symbol12 x-coord12 y-coord11 z-coord12 ... """ Frame.__init__(self) if not frameStr is None: lines = frameStr.split('\n') if lines[-1] == '': del (lines[-1]) if len(lines) < 3: raise InputError( frameStr, 'Wrong number of arguments while \ constructing the XYZFrame') length = int(lines[0]) self._comment = lines[1] for line in lines[2:]: self.atoms.append(XYZAtom(line)) if len(self.atoms) != length: raise InputError( frameStr, "Wrong number of lines in file: length=%d and len(self.atoms) %d" % (length, len(self.atoms)))
def __init__(self, xyzFile, R = 1.9859, T = 310): self.xyzFile = xyzFile self.frame = self.xyzFile.nextFrame() self.R, self.T = R, T if self.frame is None: raise InputError(xyzFile, "no frames in file") self.hexLatticeLoader = HexLatticeLoader(self.frame)
def openFiles(options): try: xyzFile = XYZFile(options.xyzFilename) datFile = open(options.datFilename, 'w') except: raise(InputError(options.xyzFilename, "error while opening xyz or output file")) return xyzFile, datFile
def openFiles(options): try: outFile = open(options.outFilename, 'w') datFile = open(options.datFilename) except: raise(InputError(options.outFilename, "error while opening output file")) return datFile, outFile
def _construct(self, atomStr): atomList = atomStr.split() if len(atomList) != 4: raise InputError( atomStr, 'Wrong number of arguments while \ constructing the XYZAtom') self.symbol, self.x = atomList[0].strip(), float(atomList[1]) self.y, self.z = float(atomList[2]), float(atomList[3])
def boxVectors(self): try: vList = [float(size) for size in self.comment.split(",")] for _ in range(len(vList), 9): vList.append(0) v1 = [vList[0], vList[3], vList[4]] v2 = [vList[5], vList[1], vList[6]] v3 = [vList[7], vList[8], vList[2]] return [v1, v2, v3] except: raise (InputError(self.comment, "Cannot read size of box from comment."))
def boxSize(self): """ Returns size of the box from frame. This property is only present in specific kind of xyz files (stored in comment). If boxSize is not present, raises input exception """ try: return [float(size) for size in self.comment.split(",")] except: raise (InputError(self.comment, "Cannot read size of box from comment."))
def __init__(self, filename): """ """ self._filename = filename if self._filename[-3:] == 'gro': self._frameCreator = self._createGroFrame self._countNum = 1 elif self._filename[-3:] == 'xyz': self._frameCreator = self._createXYZFrame self._countNum = 0 else: raise InputError(self._filename, "Wrong filename type") self._frames = None self.currentFrame = None self.file = None
def load(self): """ Loads whole file into memory.""" if self._frames is None: self._frames = [] try: xyzFile = open(self.filename, 'r') except: self._frames = [] return lines = xyzFile.readlines() if len(lines) < 2: raise (InputError(lines, 'Not enough lines in frame file')) atomsCount = int(lines[0]) step = atomsCount + 2 i = 0 while (i + step) < len(lines) + 1: step = int(lines[i]) + 2 self._frames.append(XYZFrame(''.join(lines[i:i + step])[:-1])) i += step self.currentFrame = len(self.frames) - 1
def load(self): """ Loads whole file into memory.""" #FIXME: write loaders for both files if self._frames is None: self._frames = [] try: structFile = open(self.filename, 'r') except: self._frames = [] return lines = structFile.readlines() if len(lines) < 2: raise (InputError(lines, 'Not enough lines in frame file')) atomsCount = int(lines[self._countNum]) step = atomsCount + 2 i = 0 while (i + step) < len(lines) + 1: step = int(lines[i + self._countNum]) + 2 self._frames.append( self._frameCreator(''.join(lines[i:i + step + self._countNum])[:-1])) i += step self.currentFrame = len(self.frames) - 1
def nextFrame(self): """ Returns next frame from the file. If file has ended returns None value """ if self.currentFrame is None: self.currentFrame = 0 self.file = open(self.filename, 'r') else: self.currentFrame += 1 frameLst = [self.file.readline()] if frameLst[0] == '': self.file.close() return None atomsCount = int(frameLst[0]) for _ in range(atomsCount + 1): lineStr = self.file.readline() if lineStr == '': print frameLst self.file.close() raise InputError('lineStr == ' '', "Wrong number of lines in file") frameLst.append(lineStr) return XYZFrame(''.join(frameLst))