def setup(self, initdir, settings): """ mv all files and directories (besides initdir) into initdir """ print "Moving files into initial run directory:", initdir initdir = os.path.abspath(initdir) for p in os.listdir(self.relaxdir): if (p in (io.VASP_INPUT_FILE_LIST + self.settings["extra_input_files"])) and (os.path.join( self.relaxdir, p) != initdir): os.rename(os.path.join(self.relaxdir, p), os.path.join(initdir, p)) print "" sys.stdout.flush() # Keep a backup copy of the base INCAR shutil.copyfile(os.path.join(initdir, "INCAR"), os.path.join(self.relaxdir, "INCAR.base")) # If an initial incar is called for, copy it in and set the appropriate flag if (self.settings["initial"] != None) and (os.path.isfile( os.path.join(self.relaxdir, self.settings["initial"]))): new_values = io.Incar( os.path.join(self.relaxdir, self.settings["initial"])).tags io.set_incar_tag(new_values, initdir) print " Set INCAR tags:", new_values, "\n" sys.stdout.flush()
def fix(self, err_jobdir, new_jobdir, settings): """ Try to fix the error by increasing the number of bands""" continue_job(err_jobdir, new_jobdir, settings) with open(os.path.join(err_jobdir, "OUTCAR")) as f: err_outcar = f.read().splitlines() nbands_line = [] for line in err_outcar: if "NBANDS" in line: nbands_line.append(line) if len(nbands_line) < 1: print "SERIOUS WARNING : " print " Couldn't find any reference to nbands in the OUTCAR. Continuing without fixing" else: for l in nbands_line: if "k-points" in l.strip().split(): print " Set NBANDS = " + str(int(1.1 * float(l.strip().split()[-1]))) sys.stdout.flush() io.set_incar_tag({"NBANDS": int(1.1 * float(l.strip().split()[-1]))}, jobdir=new_jobdir) break
def setup(self, initdir, settings): """ mv all files and directories (besides initdir) into initdir """ print "Moving files into initial run directory:", initdir initdir = os.path.abspath(initdir) for p in os.listdir(self.relaxdir): if (p in (io.VASP_INPUT_FILE_LIST + self.settings["extra_input_files"])) and (os.path.join(self.relaxdir, p) != initdir): os.rename(os.path.join(self.relaxdir,p), os.path.join(initdir,p)) print "" sys.stdout.flush() # Keep a backup copy of the base INCAR shutil.copyfile(os.path.join(initdir,"INCAR"),os.path.join(self.relaxdir,"INCAR.base")) # If an initial incar is called for, copy it in and set the appropriate flag if (self.settings["initial"] != None) and (os.path.isfile(os.path.join(self.relaxdir,self.settings["initial"]))): new_values = io.Incar(os.path.join(self.relaxdir,self.settings["initial"])).tags io.set_incar_tag(new_values, initdir) print " Set INCAR tags:", new_values, "\n" sys.stdout.flush()
def fix(self, err_jobdir, new_jobdir, settings): """ First attempt: Set ALGO = VeryFast Unset IALGO Second attempt: Set IBRION = 1 and POTIM = 0.1 Final attempt: Set LREAD = .FALSE. """ continue_job(err_jobdir, new_jobdir, settings) if io.get_incar_tag("ALGO", jobdir=new_jobdir) != "VeryFast": print " Set Algo = VeryFast, and Unset IALGO" io.set_incar_tag({ "IALGO": None, "ALGO": "VeryFast" }, jobdir=new_jobdir) elif io.get_incar_tag("IBRION", jobdir=new_jobdir) != 1: print " Set IBRION = 1 and POTIM = 0.1" sys.stdout.flush() io.set_incar_tag({"IBRION": 1, "POTIM": 0.1}, jobdir=new_jobdir) elif io.get_incar_tag("LREAL", jobdir=new_jobdir) != False: print " Set LREAL = .FALSE." sys.stdout.flush() io.set_incar_tag({"LREAL": False}, jobdir=new_jobdir)
def fix(self, err_jobdir, new_jobdir, settings): """ Try to fix the error by increasing the number of bands""" continue_job(err_jobdir, new_jobdir, settings) with open(os.path.join(err_jobdir, 'OUTCAR')) as f: err_outcar = f.read().splitlines() nbands_line = [] for line in err_outcar: if "NBANDS" in line: nbands_line.append(line) if len(nbands_line) < 1: print "SERIOUS WARNING : " print " Couldn't find any reference to nbands in the OUTCAR. Continuing without fixing" else: for l in nbands_line: if 'k-points' in l.strip().split(): print " Set NBANDS = " + str( int(1.1 * float(l.strip().split()[-1]))) sys.stdout.flush() io.set_incar_tag( {"NBANDS": int(1.1 * float(l.strip().split()[-1]))}, jobdir=new_jobdir) break
def fix(self, err_jobdir, new_jobdir, settings): """ First attempt: Set ALGO = VeryFast Unset IALGO Second attempt: Set IBRION = 1 and POTIM = 0.1 Final attempt: Set LREAD = .FALSE. """ continue_job(err_jobdir, new_jobdir, settings) if io.get_incar_tag("ALGO", jobdir=new_jobdir) != "VeryFast": print " Set Algo = VeryFast, and Unset IALGO" io.set_incar_tag({"IALGO": None, "ALGO": "VeryFast"}, jobdir=new_jobdir) elif io.get_incar_tag("IBRION", jobdir=new_jobdir) != 1: print " Set IBRION = 1 and POTIM = 0.1" sys.stdout.flush() io.set_incar_tag({"IBRION": 1, "POTIM": 0.1}, jobdir=new_jobdir) elif io.get_incar_tag("LREAL", jobdir=new_jobdir) != False: print " Set LREAL = .FALSE." sys.stdout.flush() io.set_incar_tag({"LREAL": False}, jobdir=new_jobdir)
def run(self): """ Perform a series of vasp jobs to relax a structure. Performs a series of vasp calculations until convergence is reached according to the criteria in 'status()'. Then performs a final constant volume run {"ISIF":2, "ISMEAR":-5, "NSW":0, "IBRION":-1}. """ print "Begin VASP relaxation run" sys.stdout.flush() # get current status of the relaxation: (status, task) = self.status() print "\n++ status:", status, " next task:", task sys.stdout.flush() while status == "incomplete": if task == "setup": self.add_rundir() self.setup(self.rundir[-1], self.settings) elif task == "relax": self.add_rundir() vasp.continue_job(self.rundir[-2], self.rundir[-1], self.settings) shutil.copyfile(os.path.join(self.relaxdir, "INCAR.base"), os.path.join(self.rundir[-1], "INCAR")) elif task == "constant": self.add_rundir() vasp.continue_job(self.rundir[-2], self.rundir[-1], self.settings) # set INCAR to ISIF = 2, ISMEAR = -5, NSW = 0, IBRION = -1 if (self.settings["final"] != None) and (os.path.isfile( os.path.join(self.relaxdir, self.settings["final"]))): new_values = io.Incar( os.path.join(self.relaxdir, self.settings["final"])).tags else: new_values = { "ISIF": 2, "ISMEAR": -5, "NSW": 0, "IBRION": -1 } # set INCAR system tag to denote 'final' if io.get_incar_tag("SYSTEM", self.rundir[-1]) is None: new_values["SYSTEM"] = "final" else: new_values["SYSTEM"] = io.get_incar_tag( "SYSTEM", self.rundir[-1]) + " final" io.set_incar_tag(new_values, self.rundir[-1]) print " Set INCAR tags:", new_values, "\n" sys.stdout.flush() else: # probably hit walltime self.add_rundir() vasp.continue_job(self.rundir[-2], self.rundir[-1], self.settings) while True: # run vasp result = vasp.run(self.rundir[-1], npar=self.settings["npar"], ncore=self.settings["ncore"], command=self.settings["vasp_cmd"], ncpus=self.settings["ncpus"], kpar=self.settings["kpar"]) # if no errors, continue if result == None or self.not_converging(): break # else, attempt to fix first error self.add_errdir() os.mkdir(self.rundir[-1]) # self.add_rundir() err = result.itervalues().next() print "\n++ status:", "error", " next task:", "fix_error" sys.stdout.flush() print "Attempting to fix error:", str(err) err.fix(self.errdir[-1], self.rundir[-1], self.settings) print "" sys.stdout.flush() if (self.settings["backup"] != None) and len(self.rundir) > 1: print "Restoring from backups:" for f in self.settings["backup"]: if os.path.isfile( os.path.join(self.rundir[-2], f + "_BACKUP.gz")): f_in = gzip.open( os.path.join(self.rundir[-2], f + "_BACKUP.gz", 'rb')) f_out = open(os.path.join(self.rundir[-1], f, 'wb')) f_out.write(f_in.read()) f_in.close() f_out.close() print f, " restored!" sys.stdout.flush() (status, task) = self.status() print "\n++ status:", status, " next task:", task sys.stdout.flush() if status == "complete": if not os.path.isdir(self.finaldir): # mv final results to relax.final print "mv", os.path.basename( self.rundir[-1]), os.path.basename(self.finaldir) sys.stdout.flush() os.rename(self.rundir[-1], self.finaldir) self.rundir.pop() vasp.complete_job(self.finaldir, self.settings) return (status, task)
def run(jobdir=None, stdout="std.out", stderr="std.err", npar=None, ncore=None, command=None, ncpus=None, kpar=None, poll_check_time=5.0, err_check_time=60.0): """ Run vasp using subprocess. The 'command' is executed in the directory 'jobdir'. Args: jobdir: directory to run vasp. If jobdir == None, the current directory is used. stdout: filename to write to. If stdout == None, "std.out" is used. stderr: filename to write to. If stderr == None, "std.err" is used. npar: (int or None) VASP INCAR NPAR setting. If npar == None, then NPAR is removed from INCAR kpar: (int or None) VASP INCAR KPAR setting. If kpar == None, then KPAR is removed from INCAR ncore: (int or None) VASP INCAR NCORE setting. If not npar == None or ncore == None, then NCORE is removed from INCAR command: (str or None) vasp execution command If command != None: then 'command' is run in a subprocess Else, if ncpus == 1, then command = "vasp" Else, command = "mpirun -np {NCPUS} vasp" ncpus: (int) if '{NCPUS}' is in 'command' string, then 'ncpus' is substituted in the command. if ncpus==None, $PBS_NP is used if it exists, else 1 poll_check_time: how frequently to check if the vasp job is completed err_check_time: how frequently to parse vasp output to check for errors """ print "Begin vasp run:" sys.stdout.flush() if jobdir == None: jobdir = os.getcwd() currdir = os.getcwd() os.chdir(jobdir) if ncpus == None: if "PBS_NP" in os.environ: ncpus = os.environ["PBS_NP"] else: ncpus = 1 if command == None: if ncpus == 1: command = "vasp" else: command = "mpirun -np {NCPUS} vasp" if re.search("\{NCPUS\}", command): command = command.format(NCPUS=str(ncpus)) if not npar == None: ncore = None io.set_incar_tag({"NPAR": npar, "NCORE": ncore, "KPAR": kpar}, jobdir) print " jobdir:", jobdir print " exec:", command sys.stdout.flush() sout = open(os.path.join(jobdir, stdout), 'w') serr = open(os.path.join(jobdir, stderr), 'w') err = None p = subprocess.Popen(command.split(), stdout=sout, stderr=serr) # wait for process to end, and periodically check for errors poll = p.poll() last_check = time.time() stopcar_time = None while poll == None: time.sleep(poll_check_time) if time.time() - last_check > err_check_time: last_check = time.time() err = error_check(jobdir, os.path.join(jobdir, stdout)) if err != None: # FreezeErrors are fatal and usually not helped with STOPCAR if "FreezeError" in err.keys(): print " VASP is frozen, killing job" sys.stdout.flush() p.kill() # Other errors can be killed with STOPCAR, which is safer elif stopcar_time == None: print " Found errors:", for e in err: print e, print "\n Killing job with STOPCAR" sys.stdout.flush() io.write_stopcar('e', jobdir) stopcar_time = time.time() # If the STOPCAR exists, wait 5 min before manually killing the job elif time.time() - stopcar_time > 300: print " VASP is non-responsive, killing job" sys.stdout.flush() p.kill() poll = p.poll() # close output files sout.close() serr.close() os.chdir(currdir) print "Run complete" sys.stdout.flush() # check finished job for errors if err == None: err = error_check(jobdir, os.path.join(jobdir, stdout)) if err != None: print " Found errors:", for e in err: print e, print "\n" return err
def run(self): """ Perform a series of vasp jobs to relax a structure. Performs a series of vasp calculations until convergence is reached according to the criteria in 'status()'. Then performs a final constant volume run {"ISIF":2, "ISMEAR":-5, "NSW":0, "IBRION":-1}. """ print "Begin VASP relaxation run" sys.stdout.flush() # get current status of the relaxation: (status, task) = self.status() print "\n++ status:", status, " next task:", task sys.stdout.flush() while status == "incomplete": if task == "setup": self.add_rundir() self.setup(self.rundir[-1], self.settings) elif task == "relax": self.add_rundir() vasp.continue_job(self.rundir[-2], self.rundir[-1], self.settings) shutil.copyfile(os.path.join(self.relaxdir,"INCAR.base"),os.path.join(self.rundir[-1],"INCAR")) elif task == "constant": self.add_rundir() vasp.continue_job(self.rundir[-2], self.rundir[-1], self.settings) # set INCAR to ISIF = 2, ISMEAR = -5, NSW = 0, IBRION = -1 if (self.settings["final"] != None) and (os.path.isfile(os.path.join(self.relaxdir,self.settings["final"]))): new_values = io.Incar(os.path.join(self.relaxdir, self.settings["final"])).tags else: new_values = {"ISIF":2, "ISMEAR":-5, "NSW":0, "IBRION":-1} # set INCAR system tag to denote 'final' if io.get_incar_tag("SYSTEM", self.rundir[-1]) is None: new_values["SYSTEM"] = "final" else: new_values["SYSTEM"] = io.get_incar_tag("SYSTEM", self.rundir[-1]) + " final" io.set_incar_tag( new_values, self.rundir[-1]) print " Set INCAR tags:", new_values, "\n" sys.stdout.flush() else: # probably hit walltime self.add_rundir() vasp.continue_job(self.rundir[-2], self.rundir[-1], self.settings) while True: # run vasp result = vasp.run(self.rundir[-1],npar=self.settings["npar"],ncore=self.settings["ncore"],command=self.settings["vasp_cmd"],ncpus=self.settings["ncpus"],kpar=self.settings["kpar"],err_types=self.settings["err_types"]) # if no errors, continue if result is None or self.not_converging(): break # else, attempt to fix first error self.add_errdir() os.mkdir(self.rundir[-1]) # self.add_rundir() err = result.itervalues().next() print "\n++ status:", "error", " next task:", "fix_error" sys.stdout.flush() print "Attempting to fix error:", str(err) err.fix(self.errdir[-1],self.rundir[-1], self.settings) print "" sys.stdout.flush() if (self.settings["backup"] != None) and len(self.rundir) > 1: print "Restoring from backups:" for f in self.settings["backup"]: if os.path.isfile(os.path.join(self.rundir[-2], f + "_BACKUP.gz")): f_in = gzip.open(os.path.join(self.rundir[-2], f + "_BACKUP.gz", 'rb')) f_out = open(os.path.join(self.rundir[-1], f, 'wb')) f_out.write(f_in.read()) f_in.close() f_out.close() print f, " restored!" sys.stdout.flush() (status, task) = self.status() print "\n++ status:", status, " next task:", task sys.stdout.flush() if status == "complete": if not os.path.isdir(self.finaldir): # mv final results to relax.final print "mv", os.path.basename(self.rundir[-1]), os.path.basename(self.finaldir) sys.stdout.flush() os.rename(self.rundir[-1], self.finaldir) self.rundir.pop() vasp.complete_job(self.finaldir, self.settings) return (status, task)
def run( jobdir=None, stdout="std.out", stderr="std.err", npar=None, ncore=None, command=None, ncpus=None, kpar=None, poll_check_time=5.0, err_check_time=60.0, ): """ Run vasp using subprocess. The 'command' is executed in the directory 'jobdir'. Args: jobdir: directory to run vasp. If jobdir == None, the current directory is used. stdout: filename to write to. If stdout == None, "std.out" is used. stderr: filename to write to. If stderr == None, "std.err" is used. npar: (int or None) VASP INCAR NPAR setting. If npar == None, then NPAR is removed from INCAR kpar: (int or None) VASP INCAR KPAR setting. If kpar == None, then KPAR is removed from INCAR ncore: (int or None) VASP INCAR NCORE setting. If not npar == None or ncore == None, then NCORE is removed from INCAR command: (str or None) vasp execution command If command != None: then 'command' is run in a subprocess Else, if ncpus == 1, then command = "vasp" Else, command = "mpirun -np {NCPUS} vasp" ncpus: (int) if '{NCPUS}' is in 'command' string, then 'ncpus' is substituted in the command. if ncpus==None, $PBS_NP is used if it exists, else 1 poll_check_time: how frequently to check if the vasp job is completed err_check_time: how frequently to parse vasp output to check for errors """ print "Begin vasp run:" sys.stdout.flush() if jobdir == None: jobdir = os.getcwd() currdir = os.getcwd() os.chdir(jobdir) if ncpus == None: if "PBS_NP" in os.environ: ncpus = os.environ["PBS_NP"] else: ncpus = 1 if command == None: if ncpus == 1: command = "vasp" else: command = "mpirun -np {NCPUS} vasp" if re.search("\{NCPUS\}", command): command = command.format(NCPUS=str(ncpus)) if not npar == None: ncore = None io.set_incar_tag({"NPAR": npar, "NCORE": ncore, "KPAR": kpar}, jobdir) print " jobdir:", jobdir print " exec:", command sys.stdout.flush() sout = open(os.path.join(jobdir, stdout), "w") serr = open(os.path.join(jobdir, stderr), "w") err = None p = subprocess.Popen(command.split(), stdout=sout, stderr=serr) # wait for process to end, and periodically check for errors poll = p.poll() last_check = time.time() stopcar_time = None while poll == None: time.sleep(poll_check_time) if time.time() - last_check > err_check_time: last_check = time.time() err = error_check(jobdir, os.path.join(jobdir, stdout)) if err != None: # FreezeErrors are fatal and usually not helped with STOPCAR if "FreezeError" in err.keys(): print " VASP is frozen, killing job" sys.stdout.flush() p.kill() # Other errors can be killed with STOPCAR, which is safer elif stopcar_time == None: print " Found errors:", for e in err: print e, print "\n Killing job with STOPCAR" sys.stdout.flush() io.write_stopcar("e", jobdir) stopcar_time = time.time() # If the STOPCAR exists, wait 5 min before manually killing the job elif time.time() - stopcar_time > 300: print " VASP is non-responsive, killing job" sys.stdout.flush() p.kill() poll = p.poll() # close output files sout.close() serr.close() os.chdir(currdir) print "Run complete" sys.stdout.flush() # check finished job for errors if err == None: err = error_check(jobdir, os.path.join(jobdir, stdout)) if err != None: print " Found errors:", for e in err: print e, print "\n" return err