From 50cec1aa0b3893469a3ca93ad1cd9feea4127748 Mon Sep 17 00:00:00 2001 From: littlef0x <32670075+littlef0x@users.noreply.github.com> Date: Thu, 21 Feb 2019 18:03:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BFnebinfo=E5=85=BC=E5=AE=B9=E6=97=A7?= =?UTF-8?q?=E6=A0=BC=E5=BC=8FPOSCAR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit vasp5.1以下版本使用的POSCAR原子数在第6行,脚本读取第7行会导致ValueError,加入判断并将旧格式转换为新格式再进行处理,nebinfo测试通过,其他脚本未测 --- VASP.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/VASP.py b/VASP.py index 4140d54..0e70c7c 100644 --- a/VASP.py +++ b/VASP.py @@ -2,6 +2,32 @@ import subprocess # 注意:所有函数读取的坐标统一为笛卡尔坐标 +def old_POSCAR(file_name): + """ + :param file_name: str + check whether POSCAR is in old format(vasp 5.1-) + """ + with open(file_name) as input_file: + content = input_file.readlines() + try: + float(content[6].strip().split()[0]) # 如果第7行是数字,则是新格式,即第6行为元素符号 + return False + except ValueError: + pass + return True + +def convert_POSCAR(file_name): + """ + :param file_name: str + convert old POSCAR to new one + """ + with open(file_name) as input_file: + content = input_file.readlines() + content.insert(5,content[0]) + poscar = ''.join(content) + input_file = open(file_name,'w') + input_file.write(poscar) + input_file.close() class CmdRrror(Exception): def __init__(self, errorinfo): @@ -27,7 +53,7 @@ def execCmd(command): # this function can only read file in VASP 5.0 or later def readVasp(file_name): """ - :param file_name: + :param file_name: str :return: [lattice, basis, elements, num_atoms, selectiveflag, coordinate_type, coordinates, selective] lattice: scale, float basis: [x1, y1, z1], [x2, y2, z2], [x3, y3, z3]], float @@ -38,6 +64,8 @@ def readVasp(file_name): coordinates: [[x1, y1, z1], [x2, y2, z2], ...], float selective: [[T/F, T/F, T/F], [T/F, T/F, T/F], ...], str """ + if old_POSCAR(file_name): + convert_POSCAR(file_name) space = re.compile(r'\s+') with open(file_name) as input_file: content = input_file.readlines() @@ -101,6 +129,8 @@ def writeVasp(file_name, lattice, basis, elements, num_atoms, selectiveflag, coo :param selective: [[T/F, T/F, T/F], [T/F, T/F, T/F], ...] or [], str :return: """ + if old_POSCAR(file_name): + convert_POSCAR(file_name) with open(file_name, 'w') as output_file: description = ' '.join(elements) output_file.write('%s\n' % description)