@@ -0,0 +1,3354 @@
+# -*- coding:utf-8 -*-%0A%22%22%22%0A========================================================================%0AProvide INCAR file class which do operations on these files.%0A========================================================================%0AWritten by PytLab %3Cshaozhengjiang@gmail.com%3E, October 2015%0AUpdated by PytLab %3Cshaozhengjiang@gmail.com%3E, October 2015%0A========================================================================%0A%0A%22%22%22%0Afrom vaspy import VasPy%0A%0A%0Aclass InCar(VasPy):%0A    def __init__(self, filename='INCAR'):%0A        %22%22%22%0A        Create a INCAR file class.%0A%0A        Example:%0A%0A        %3E%3E%3E a = InCar()%0A%0A        Class attributes descriptions%0A        =======================================================%0A          Attribute      Description%0A          ============  =======================================%0A          filename       string, name of INCAR file%0A          ============  =======================================%0A        %22%22%22%0A        VasPy.__init__(self, filename)%0A        self.load()%0A%0A    def load(self):%0A        %22Load all data in INCAR.%22%0A        tot_pnames, tot_datas = %5B%5D, %5B%5D%0A        with open(self.filename, 'r') as f:%0A            for line in f:%0A                matched = self.rdata(line)%0A                if matched:%0A                    pnames, datas = matched%0A                    tot_pnames.extend(pnames)%0A                    tot_datas.extend(datas)%0A        # set attrs%0A        for pname, data in zip(tot_pnames, tot_datas):%0A            setattr(self, pname, data)%0A        self.pnames = tot_pnames%0A%0A        return%0A%0A    @staticmethod%0A    def rdata(line):%0A        %22Get INCAR data(s) in a line.%22%0A        line = line.strip()%0A        if not line or line.startswith(('!', '#')):%0A            return None%0A        else:%0A            if '#' in line:%0A                line = line.split('#')%5B0%5D.strip()%0A                if '!' in line:%0A                    line = line.split('!')%5B0%5D.strip()%0A            elif '!' in line:%0A                line = line.split('!')%5B0%5D.strip()%0A            # get parameter name and data%0A            if ';' in line:%0A                params = %5Bparam.strip() for param in line.split(';')%5D%0A            else:%0A                params = %5Bline%5D%0A            pnames, datas = %5B%5D, %5B%5D%0A            for param in params:%0A                pname, data = %5Bi.strip() for i in param.split('=')%5D%0A                pnames.append(pname)%0A                datas.append(data)%0A%0A            return pnames, datas%0A%0A    def set(self, pname, data):%0A        if not hasattr(self, pname):%0A            raise ValueError('%25s is not in INCAR, ' +%0A                             'Use add() instead.' %25 pname)%0A        setattr(self, pname, str(data))%0A        return%0A%0A    def add(self, pname, data):%0A        data = str(data)%0A        if hasattr(self, pname):%0A            print (%22Waring: %25s is already in INCAR, %22 +%0A                   %22set to %25s%22 %25 (pname, data))%0A        else:%0A            self.pnames.append(pname)%0A        setattr(self, pname, data)%0A%0A        return%0A%0A    def tofile(self):%0A        %22Create INCAR file.%22%0A        content = '# Created by VASPy%5Cn'%0A        for pname in self.pnames:%0A            if not hasattr(self, pname):%0A                raise ValueError('Unknown parameter: %25s' %25 pname)%0A            data = str(getattr(self, pname))%0A            content += '%25s = %25s%5Cn' %25 (pname, data)%0A        with open('INCAR', 'w') as f:%0A            f.write(content)%0A%0A        return%0A

add InCar class