Skip to content

Commit 2f83dc4

Browse files
committed
Merge dev to master.
2 parents 9524952 + cfa8dc0 commit 2f83dc4

File tree

10 files changed

+157
-66
lines changed

10 files changed

+157
-66
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Enforces LF line endings
2-
eol=crlf
2+
* text eol=crlf
33

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ MANIFEST
6565
vaspy/*CAR
6666
vaspy/*.xsd
6767
OUTCAR
68-
vaspy/DOS/*
68+
vaspy/DOS/*
69+
70+
# Vim temp files
71+
*.swp
72+
*.swo

scripts/change_incar_parameters.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
Modify recursively parameters in all INCAR file.
3+
'''
4+
5+
import argparse
6+
import commands
7+
import logging
8+
9+
10+
from vaspy.incar import InCar
11+
12+
SHELL_COMMAND = "find ./ -name 'INCAR'"
13+
14+
if "__main__" == __name__:
15+
16+
# Check command validity.
17+
status, output = commands.getstatusoutput(SHELL_COMMAND)
18+
if status:
19+
raise SystemExit("Invalid shell commands - '{}'".format(SHELL_COMMAND))
20+
21+
# Get InCar objects.
22+
incar_paths = (incar_path.strip() for incar_path in output.split('\n'))
23+
incars = [InCar(incar_path) for incar_path in incar_paths]
24+
25+
# Get all possible arguments.
26+
set_list = [set(incar.pnames()) for incar in incars]
27+
possible_args = set.intersection(*set_list)
28+
29+
# Set arguments for this script.
30+
parser = argparse.ArgumentParser()
31+
for arg in possible_args:
32+
arg_str = "--{}".format(arg)
33+
parser.add_argument(arg_str, help="set {} INCAR parameter".format(arg))
34+
args_space = parser.parse_args()
35+
36+
# Change parameters for all incars.
37+
for pname, value in args_space.__dict__.iteritems():
38+
if value is None:
39+
continue
40+
41+
for incar in incars:
42+
logging.info("{} --> {} in {}.".format(pname, value, incar.filename()))
43+
incar.set(pname, value)
44+
incar.tofile()
45+
46+
logging.info("{} INCAR files ... ok.".format(len(incars)))
47+

scripts/create_xsd.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,60 @@
44
import sys
55
import re
66
import commands
7+
import argparse
78

8-
import vaspy
9+
from vaspy import atomco, matstudio
910

10-
if len(sys.argv) > 1:
11-
# extract certain step data to .xyz file
12-
step = sys.argv[1]
13-
step_regex = re.compile(r'^STEP\s+=\s+' + step + r'$')
14-
with open('OUT.ANI', 'r') as f:
15-
natom = int(f.readline().strip())
16-
# match step
17-
content = ''
18-
count = 0
19-
line = f.readline()
20-
while line:
21-
if step_regex.match(line):
22-
# get data content
23-
for i in xrange(natom):
24-
content += f.readline()
25-
break
11+
if "__main__" == __name__:
12+
13+
# Set argument parser.
14+
parser = argparse.ArgumentParser()
15+
parser.add_argument("-s", "--step",
16+
help="step number on which the data is extracted.")
17+
args = parser.parse_args()
18+
19+
if args.step:
20+
# extract certain step data to .xyz file
21+
step = args.step
22+
step_regex = re.compile(r'^STEP\s+=\s+' + step + r'$')
23+
with open('OUT.ANI', 'r') as f:
24+
natom = int(f.readline().strip())
25+
# match step
26+
content = ''
27+
count = 0
2628
line = f.readline()
27-
if not content:
28-
print 'Step: %s is out of range.' % step
29-
sys.exit(1)
30-
# write to .xyz file
31-
with open('ts.xyz', 'w') as f:
32-
head = '%12d\nSTEP = %8s\n' % (natom, step)
33-
content = head + content
34-
f.write(content)
29+
while line:
30+
if step_regex.match(line):
31+
# get data content
32+
for i in xrange(natom):
33+
content += f.readline()
34+
break
35+
line = f.readline()
36+
if not content:
37+
print 'Step: %s is out of range.' % step
38+
sys.exit(1)
39+
# write to .xyz file
40+
with open('ts.xyz', 'w') as f:
41+
head = '%12d\nSTEP = %8s\n' % (natom, step)
42+
content = head + content
43+
f.write(content)
3544

36-
# coordinate transformation
37-
xyz = vaspy.atomco.XyzFile('ts.xyz')
38-
poscar = vaspy.atomco.PosCar()
39-
direct_coordinates = xyz.coordinate_transform(poscar.bases)
40-
suffix = '-' + step + '.xsd'
41-
else: # the last step data
42-
contcar = vaspy.atomco.ContCar()
43-
direct_coordinates = contcar.data
44-
suffix = '-y.xsd'
45+
# coordinate transformation
46+
xyz = atomco.XyzFile('ts.xyz')
47+
poscar = atomco.PosCar()
48+
direct_coordinates = xyz.coordinate_transform(poscar.bases)
49+
suffix = '-' + step + '.xsd'
50+
else: # the last step data
51+
contcar = atomco.ContCar()
52+
direct_coordinates = contcar.data
53+
suffix = '-y.xsd'
4554

4655
# create .xsd file
4756
status, output = commands.getstatusoutput('ls *.xsd | head -1')
4857
if not output.endswith('.xsd'):
4958
print "No .xsd file in current directory."
5059
sys.exit(1)
51-
xsd = vaspy.matstudio.XsdFile(filename=output)
60+
xsd = matstudio.XsdFile(filename=output)
5261
xsd.data = direct_coordinates
5362

5463
jobname = output.split('.')[0]

vaspy/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
__version__ = '0.4.3'
2-
__all__ = ['atomco', 'electro', 'iter', 'matstudio', 'plotter']
1+
__version__ = '0.5.3'
2+
__all__ = ['atomco', 'electro', 'iter', 'matstudio', 'plotter', 'incar']
33

44

55
class VasPy(object):
66
def __init__(self, filename):
77
"Base class to be inherited by all classes in VASPy."
8-
self.filename = filename
8+
self.__filename = filename
9+
10+
def filename(self):
11+
" Query function for bounded filename."
12+
return self.__filename
913

1014

1115
class CarfileValueError(Exception):

vaspy/atomco.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Provide coordinate file class which do operations on these files.
55
===================================================================
66
Written by PytLab <shaozhengjiang@gmail.com>, November 2014
7-
Updated by PytLab <shaozhengjiang@gmail.com>, November 2015
7+
Updated by PytLab <shaozhengjiang@gmail.com>, May 2016
88
99
==============================================================
1010
@@ -24,7 +24,7 @@ def __repr__(self):
2424
if hasattr(self, 'get_content'):
2525
return self.get_content()
2626
else:
27-
return self.filename
27+
return self.filename()
2828

2929
def __str__(self):
3030
return self.__repr__()
@@ -38,7 +38,7 @@ def __getattribute__(self, attr):
3838
elif attr == 'tf_dict':
3939
return self.get_tf_dict(self.tf)
4040
else:
41-
return object.__getattribute__(self, attr)
41+
return VasPy.__getattribute__(self, attr)
4242

4343
def verify(self):
4444
if len(self.data) != self.ntot:
@@ -177,13 +177,13 @@ class XyzFile(AtomCo):
177177
============ =======================================================
178178
"""
179179
def __init__(self, filename):
180-
AtomCo.__init__(self, filename)
180+
super(self.__class__, self).__init__(filename)
181181
self.load()
182182
self.verify()
183183

184184
def load(self):
185185
"加载文件内容"
186-
with open(self.filename, 'r') as f:
186+
with open(self.filename(), 'r') as f:
187187
content_list = f.readlines()
188188
ntot = int(content_list[0].strip()) # total atom number
189189
step = int(str2list(content_list[1])[-1]) # iter step number
@@ -269,7 +269,7 @@ def __init__(self, filename='POSCAR'):
269269
def load(self):
270270
"获取文件数据信息"
271271
"Load all information in POSCAR."
272-
with open(self.filename, 'r') as f:
272+
with open(self.filename(), 'r') as f:
273273
content_list = f.readlines()
274274
#get scale factor
275275
bases_const = float(content_list[1])
@@ -415,7 +415,7 @@ def __init__(self, filename='XDATCAR'):
415415
self.load()
416416

417417
def load(self):
418-
with open(self.filename, 'r') as f:
418+
with open(self.filename(), 'r') as f:
419419
# read lattice info
420420
self.system = f.readline().strip()
421421
self.bases_const = float(f.readline().strip())
@@ -432,7 +432,7 @@ def load(self):
432432

433433
def __iter__(self):
434434
"generator which yield step number and iterative data."
435-
with open(self.filename, 'r') as f:
435+
with open(self.filename(), 'r') as f:
436436
# pass info lines
437437
for i in xrange(self.info_nline):
438438
f.readline()

vaspy/electro.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Provide electro-related file class which do operations on these files.
55
========================================================================
66
Written by PytLab <shaozhengjiang@gmail.com>, September 2015
7-
Updated by PytLab <shaozhengjiang@gmail.com>, October 2015
7+
Updated by PytLab <shaozhengjiang@gmail.com>, May 2016
88
========================================================================
99
1010
"""
@@ -131,7 +131,7 @@ def tofile(self):
131131
content = ''
132132
for datalist in data:
133133
content += ('%12.8f'*ndata + '\n') % tuple(datalist)
134-
with open(self.filename, 'w') as f:
134+
with open(self.filename(), 'w') as f:
135135
f.write(content)
136136

137137
return
@@ -189,12 +189,12 @@ def __init__(self, filename='ELFCAR'):
189189
plot_field method, plot scalar field for elf data
190190
============== =============================================
191191
"""
192-
PosCar.__init__(self, filename=filename)
192+
super(self.__class__, self).__init__(filename)
193193

194194
def load(self):
195195
"Rewrite load method"
196196
PosCar.load(self)
197-
with open(self.filename, 'r') as f:
197+
with open(self.filename(), 'r') as f:
198198
for i in xrange(self.totline):
199199
f.readline()
200200
#get dimension of 3d array

vaspy/incar.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ def __init__(self, filename='INCAR'):
2727
filename string, name of INCAR file
2828
============ =======================================
2929
"""
30-
VasPy.__init__(self, filename)
30+
super(self.__class__, self).__init__(filename)
31+
self.__filename = filename
3132
self.load()
3233

3334
def load(self):
3435
"Load all data in INCAR."
3536
tot_pnames, tot_datas = [], []
36-
with open(self.filename, 'r') as f:
37+
with open(self.__filename, 'r') as f:
3738
for line in f:
3839
matched = self.rdata(line)
3940
if matched:
@@ -43,10 +44,22 @@ def load(self):
4344
# set attrs
4445
for pname, data in zip(tot_pnames, tot_datas):
4546
setattr(self, pname, data)
46-
self.pnames = tot_pnames
47+
self.__pnames = tot_pnames
4748

4849
return
4950

51+
def pnames(self):
52+
"""
53+
Query function for all parameter names.
54+
"""
55+
return self.__pnames
56+
57+
def file_name(self):
58+
"""
59+
Query function for all INCAR path names.
60+
"""
61+
return self.__filename
62+
5063
@staticmethod
5164
def rdata(line):
5265
"Get INCAR data(s) in a line."
@@ -74,32 +87,46 @@ def rdata(line):
7487
return pnames, datas
7588

7689
def set(self, pname, data):
90+
"""
91+
Set a named parameter of InCar object.
92+
93+
Example:
94+
--------
95+
>>> incar_obj.set("ISIF", 2)
96+
"""
7797
if not hasattr(self, pname):
7898
raise ValueError('%s is not in INCAR, ' +
7999
'Use add() instead.' % pname)
80100
setattr(self, pname, str(data))
81101
return
82102

83103
def add(self, pname, data):
104+
"""
105+
Add a new parameter name to InCar object.
106+
107+
Example:
108+
--------
109+
>>> incar_obj.add("ISIF", 2)
110+
"""
84111
data = str(data)
85112
if hasattr(self, pname):
86113
print ("Waring: %s is already in INCAR, " +
87114
"set to %s" % (pname, data))
88115
else:
89-
self.pnames.append(pname)
116+
self.__pnames.append(pname)
90117
setattr(self, pname, data)
91118

92119
return
93120

94121
def tofile(self):
95122
"Create INCAR file."
96123
content = '# Created by VASPy\n'
97-
for pname in self.pnames:
124+
for pname in self.__pnames:
98125
if not hasattr(self, pname):
99126
raise ValueError('Unknown parameter: %s' % pname)
100127
data = str(getattr(self, pname))
101128
content += '%s = %s\n' % (pname, data)
102-
with open('INCAR', 'w') as f:
129+
with open(self.__filename, 'w') as f:
103130
f.write(content)
104131

105132
return

0 commit comments

Comments
 (0)