Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使nebinfo兼容旧格式POSCAR #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion VASP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down