def run_feff8l(modules): """ run selected modules of Feff85L """ try: logfile = open('feff8l.log', 'w+') except: logfile = tempfile.NamedTemporaryFile(prefix='feff8l') def write(msg): msg = bytes2str(msg) sys.stdout.write(msg) logfile.write(msg) write("#= Feff85l %s\n" % isotime()) for mod in modules: write("#= Feff85l %s module\n" % mod) exe = find_exe('feff8l_%s' % mod) proc = Popen(exe, stdout=PIPE, stderr=PIPE) while True: msg = bytes2str(proc.stdout.read()) if msg == '': break write(msg) while True: msg = bytes2str(proc.stderr.read()) if msg == '': break write("#ERROR %s" % msg) logfile.flush() for fname in glob.glob('log*.dat'): try: os.unlink(fname) except IOError: pass write("#= Feff85l done %s\n" % isotime())
def write(msg): msg = bytes2str(msg) msg = " : {:s}\n".format(msg.strip().rstrip()) if self._larch is not None: self._larch.writer.write(msg) else: sys.stdout.write(msg)
def run(self, feffinp=None, folder=None, exe='feff8l'): """ Make system call to run one or more of the stand-alone executables, writing a log file to the folder containing the input file. """ if folder is not None: self.folder = folder if feffinp is not None: self.feffinp = feffinp if self.feffinp is None: raise Exception("no feff.inp file was specified") savefile = '.save_.inp' here = abspath(os.getcwd()) os.chdir(abspath(self.folder)) feffinp_dir, feffinp_file = os.path.split(self.feffinp) feffinp_dir = dirname(self.feffinp) if len(feffinp_dir) > 0: os.chdir(feffinp_dir) if not isfile(feffinp_file): raise Exception("feff.inp file '%s' could not be found" % feffinp_file) if exe is None: for module in self.Feff8l_modules: os.chdir(here) self.run(exe=module) return # # exe is set, find the corresponding executable file ## find program to run: program = None if exe in self.Feff8l_modules: exe = "feff8l_%s" % exe resolved_exe = find_exe(exe) if resolved_exe is not None: program = resolved_exe else: try: program = self._larch.symtable.get_symbol('_xafs._feff_executable') except (NameError, AttributeError) as exc: program = None if program is not None: if not os.access(program, os.X_OK): program = None if program is None: # Give up! os.chdir(here) raise Exception("'%s' executable cannot be found" % exe) ## preserve an existing feff.inp file if this is not called feff.inp if feffinp_file != 'feff.inp': if isfile('feff.inp'): copy('feff.inp', savefile) copy(feffinp_file, 'feff.inp') _, logname = os.path.split(program) if logname.endswith('.exe'): logname = logname[:4] log = 'feffrun_%s.log' % logname if isfile(log): os.unlink(log) f = open(log, 'a') header = "\n======== running Feff module %s ========\n" % exe def write(msg): msg = bytes2str(msg) msg = " : {:s}\n".format(msg.strip().rstrip()) if self._larch is not None: self._larch.writer.write(msg) else: sys.stdout.write(msg) if self.verbose: write(header) f.write(header) process=subprocess.Popen(program, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) flag = False thislist = [] while True: if process.returncode is None: process.poll() time.sleep(0.01) line = bytes2str(process.stdout.readline()) if not line: break if self.verbose: write(line) ## snarf threshold energy pattern = re.compile('mu_(new|old)=\s+(-?\d\.\d+)') match = pattern.search(line) if match is not None: self.threshold.append(match.group(2)) ## snarf charge transfer if line.strip().startswith('Charge transfer'): thislist = [] flag = True elif line.strip().startswith('SCF ITERATION'): self.chargetransfer.append(list(thislist)) flag = False elif line.strip().startswith('Done with module 1'): self.chargetransfer.append(list(thislist)) flag = False elif flag: this = line.split() thislist.append(this[1]) f.write(line) f.close if isfile(savefile): move(savefile, 'feff.inp') os.chdir(here) return None
def write(msg): msg = bytes2str(msg) sys.stdout.write(msg) logfile.write(msg)
def run(self, feffinp=None, folder=None, exe='feff8l'): """ Make system call to run one or more of the stand-alone executables, writing a log file to the folder containing the input file. """ if folder is not None: self.folder = folder if feffinp is not None: self.feffinp = feffinp if self.feffinp is None: raise Exception("no feff.inp file was specified") savefile = '.save_.inp' here = abspath(os.getcwd()) os.chdir(abspath(self.folder)) feffinp_dir, feffinp_file = os.path.split(self.feffinp) feffinp_dir = dirname(self.feffinp) if len(feffinp_dir) > 0: os.chdir(feffinp_dir) if not isfile(feffinp_file): raise Exception("feff.inp file '%s' could not be found" % feffinp_file) if exe is None: for module in self.Feff8l_modules: os.chdir(here) self.run(exe=module) return # # exe is set, find the corresponding executable file ## find program to run: program = None if exe in self.Feff8l_modules: exe = "feff8l_%s" % exe resolved_exe = find_exe(exe) if resolved_exe is not None: program = resolved_exe else: try: program = self._larch.symtable.get_symbol( '_xafs._feff_executable') except (NameError, AttributeError) as exc: program = None if program is not None: if not os.access(program, os.X_OK): program = None if program is None: # Give up! os.chdir(here) raise Exception("'%s' executable cannot be found" % exe) ## preserve an existing feff.inp file if this is not called feff.inp if feffinp_file != 'feff.inp': if isfile('feff.inp'): copy('feff.inp', savefile) copy(feffinp_file, 'feff.inp') _, logname = os.path.split(program) if logname.endswith('.exe'): logname = logname[:4] log = 'feffrun_%s.log' % logname if isfile(log): os.unlink(log) f = open(log, 'a') header = "\n======== running Feff module %s ========\n" % exe def write(msg): msg = bytes2str(msg) msg = " : {:s}\n".format(msg.strip().rstrip()) if self._larch is not None: self._larch.writer.write(msg) else: sys.stdout.write(msg) if self.verbose: write(header) f.write(header) process = subprocess.Popen(program, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) flag = False thislist = [] while True: if process.returncode is None: process.poll() time.sleep(0.01) line = bytes2str(process.stdout.readline()) if not line: break if self.verbose: write(line) ## snarf threshold energy pattern = re.compile('mu_(new|old)=\s+(-?\d\.\d+)') match = pattern.search(line) if match is not None: self.threshold.append(match.group(2)) ## snarf charge transfer if line.strip().startswith('Charge transfer'): thislist = [] flag = True elif line.strip().startswith('SCF ITERATION'): self.chargetransfer.append(list(thislist)) flag = False elif line.strip().startswith('Done with module 1'): self.chargetransfer.append(list(thislist)) flag = False elif flag: this = line.split() thislist.append(this[1]) f.write(line) f.close if isfile(savefile): move(savefile, 'feff.inp') os.chdir(here) return None