def readVFE(filename): with open(filename, 'r') as f: cleanStr = f.read() # Define Grammars header = pp.OneOrMore(pp.pythonStyleComment).setParseAction( pp.replaceWith('')) divider = (pp.Literal('---') + pp.restOfLine).setParseAction( pp.replaceWith('')) flt = pp.Word(pp.nums + '.+-') colHeader = pp.Suppress(pp.Literal('Temp (K) vfe (J/mol-c)')) tempTable = pp.Group(pp.OneOrMore(pp.Group(flt * 2))) datum = pp.Group(colHeader + tempTable) data = pp.OneOrMore(datum) # Parse String try: for grammar in [header, divider]: cleanStr = grammar.transformString(cleanStr) ppresults = data.parseString(cleanStr).asList() except pp.ParseException as pe: print(('Parsing Error using pyparsing: invalid input:', pe)) sys.exit() # Format Results results = {} for ppr in ppresults: temps = list(map(float, list(zip(*ppr[0]))[0])) vfes = list(map(float, list(zip(*ppr[0]))[1])) key = 'Temp (K)' if key not in results: results[key] = temps key = 'VFE (J/mol-c)' if key not in results: results[key] = vfes else: results[key] = [sum(x) for x in zip(results[key], vfes)] return results
def read_s2(filename, tempgrid): with open(filename, 'r') as f: cleanStr = f.read() # Define Grammars header = pp.OneOrMore(pp.pythonStyleComment).setParseAction( pp.replaceWith('')) divider = (pp.Literal('---') + pp.restOfLine).setParseAction( pp.replaceWith('')) index = pp.Word(pp.nums) path = pp.Literal('Path Indices:').suppress() + pp.Group( pp.OneOrMore(index)) colon = pp.Literal(':') flt = pp.Word(pp.nums + '.+-') pathlength = pp.Suppress( pp.Literal('Path Len') + pp.SkipTo(colon) + colon) + flt # Debug: FDV # print tempgrid # sys.exit() # ntemp = int(tempgrid.split()[0]) ntemp = int(tempgrid[0]) if ntemp is 1: datum = pp.Group(path + pathlength + flt) else: colHeader = pp.Suppress(pp.Literal('Temp (K) s^2 (1e-3 Ang^2)')) tempTable = pp.Group(pp.OneOrMore(pp.Group(flt * 2))) datum = pp.Group(path + pathlength + colHeader + tempTable) data = pp.OneOrMore(datum) # Parse String try: for grammar in [header, divider]: cleanStr = grammar.transformString(cleanStr) ppresults = data.parseString(cleanStr).asList() except pp.ParseException as pe: print(('Parsing Error using pyparsing: invalid input:', pe)) sys.exit() # Format Results results = {} for ppr in ppresults: key = ','.join(ppr[0]) if ntemp is 1: temps = float(tempgrid.split()[1]) dwfs = float(ppr[2]) else: temps = list(map(float, list(zip(*ppr[2]))[0])) dwfs = list(map(float, list(zip(*ppr[2]))[1])) results[key] = { 'Pathlength (Ang)': float(ppr[1]), 'Temp (K)': temps, 's^2': dwfs } return results
def getU(file, addDS=''): token = pp.Literal('etotal' + addDS) value = pp.Word(pp.nums + ".+-E") end = pp.Literal('END DATASET(S)') etotal = (pp.SkipTo(end) + pp.SkipTo(token) + token).suppress() + value f = open(file, 'r') try: u = etotal.parseString(f.read()).asList()[0] except pp.ParseException as pe: print(('Parsing Error using pyparsing: invalid input:', pe)) sys.exit() f.close() return float(u)
def getacell(file): token = pp.Keyword('acell') vector = pp.Group(pp.Word(pp.nums + ".+-E")*3) end = pp.Literal('END DATASET(S)') acell = (pp.SkipTo(end) + pp.SkipTo(token) + token).suppress() + vector f = open(file, 'r') try: a = acell.parseString(f.read()).asList()[0] except pp.ParseException as pe: print(('Parsing Error using pyparsing: invalid input:', pe)) sys.exit() f.close() return ' '.join(a)
def expandedList(string, length=3): assert isinstance(string, str) value = pp.Word(pp.nums + ".+-E") prefix = pp.Combine(pp.Optional(pp.Word(pp.nums)) + pp.Literal('*')) numList = pp.OneOrMore(pp.Optional(prefix) + value) expanded = [] num = 1 for x in numList.parseString(string).asList(): if num != 1: expanded.extend([x]*num) num = 1 elif '*' in x: if x[:-1] == '': num = length else: num = int(x[:-1]) else: expanded.append(x) return expanded
def read_u2(filename, tempgrid): with open(filename, 'r') as f: cleanStr = f.read() # Define Grammars header = pp.OneOrMore(pp.pythonStyleComment).setParseAction( pp.replaceWith('')) divider = (pp.Literal('===') + pp.restOfLine).setParseAction( pp.replaceWith('')) index = pp.Word(pp.nums) at_index = pp.Literal('Atom Index:').suppress() + pp.Group( pp.OneOrMore(index)) dir = pp.Word(pp.alphas, max=1) coord_dir = pp.Group(pp.OneOrMore('-')).suppress() + pp.Literal( 'Direction').suppress() + dir + pp.Group(pp.OneOrMore('-')).suppress() colon = pp.Literal(':') flt = pp.Word(pp.nums + '.+-') # Modified by FDV # ntemp = int(tempgrid.split()[0]) ntemp = int(tempgrid[0]) if ntemp is 1: colHeader = pp.Suppress(pp.Literal('u^2 (1e-3 Ang^2):')) datum = pp.Group(at_index + coord_dir + colHeader + flt) else: colHeader = pp.Suppress(pp.Literal('Temp (K) u^2 (1e-3 Ang^2)')) tempTable = pp.Group(pp.OneOrMore(pp.Group(flt * 2))) datum = pp.Group(at_index + coord_dir + colHeader + tempTable) data = pp.OneOrMore(datum) # Parse String try: for grammar in [header, divider]: cleanStr = grammar.transformString(cleanStr) ppresults = data.parseString(cleanStr).asList() except pp.ParseException as pe: print(('Parsing Error using pyparsing: invalid input:', pe)) sys.exit() # Format Results results = {} for ppr in ppresults: key = ','.join([ppr[0][0], ppr[1]]) if ntemp is 1: temps = float(tempgrid.split()[1]) dwfs = float(ppr[2]) else: temps = list(map(float, list(zip(*ppr[2]))[0])) dwfs = list(map(float, list(zip(*ppr[2]))[1])) results[key] = {'Temp (K)': temps, 'u^2': dwfs} return results