예제 #1
0
    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()
예제 #2
0
    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)