|
1 | 1 | ''' |
2 | 2 | Script to convert .xsd file to VASP input files. |
3 | 3 | ''' |
| 4 | + |
| 5 | +import argparse |
4 | 6 | import commands |
5 | 7 | import os |
6 | 8 | import sys |
| 9 | +import logging |
7 | 10 |
|
8 | 11 | import numpy as np |
9 | 12 |
|
10 | 13 | from vaspy.matstudio import XsdFile |
11 | 14 | from vaspy.incar import InCar |
12 | 15 |
|
13 | | -#create POSCAR |
14 | | -status, output = commands.getstatusoutput('ls *.xsd | head -1') |
15 | | -xsd = XsdFile(filename=output) |
16 | | -poscar_content = xsd.get_poscar_content(bases_const=1.0) |
17 | | -with open('POSCAR', 'w') as f: |
18 | | - f.write(poscar_content) |
| 16 | +if "__main__" == __name__: |
| 17 | + # Set argument parser. |
| 18 | + parser = argparse.ArgumentParser() |
19 | 19 |
|
20 | | -#create POTCAR |
21 | | -potdir = r'/data/pot/vasp/potpaw_PBE2010/' |
22 | | -#delete old POTCAR |
23 | | -if os.path.exists('./POTCAR'): |
24 | | - os.remove('./POTCAR') |
25 | | -for elem in xsd.atoms: |
26 | | -# if os.path.exists(potdir + elem + '_new/'): |
27 | | -# potcar = potdir + elem + '_new/POTCAR' |
28 | | - if os.path.exists(potdir + elem): |
29 | | - potcar = potdir + elem + '/POTCAR' |
30 | | - else: |
31 | | - print 'No POTCAR for ' + elem |
32 | | - sys.exit(1) |
33 | | - commands.getstatusoutput('cat ' + potcar + ' >> ./POTCAR') |
| 20 | + # Add kpoint optional argument. |
| 21 | + parser.add_argument("-k", "--kpoints", help="set k-points") |
| 22 | + args = parser.parse_args() |
| 23 | + |
| 24 | + #create POSCAR |
| 25 | + status, output = commands.getstatusoutput('ls *.xsd | head -1') |
| 26 | + xsd = XsdFile(filename=output) |
| 27 | + poscar_content = xsd.get_poscar_content(bases_const=1.0) |
| 28 | + with open('POSCAR', 'w') as f: |
| 29 | + f.write(poscar_content) |
34 | 30 |
|
35 | | -#creat KPOINTS |
36 | | -kpoints = [] |
37 | | -for base in xsd.bases: |
38 | | - l = np.dot(base, base)**0.5 |
39 | | - kpt = int(20/l) + 1 |
40 | | - kpoints.append(str(kpt)) |
41 | | -kpt_str = ' '.join(kpoints) |
42 | | -kpt_content = 'mesh auto\n0\nG\n' + kpt_str + '\n0 0 0\n' |
43 | | -with open('KPOINTS', 'w') as f: |
44 | | - f.write(kpt_content) |
| 31 | + #create POTCAR |
| 32 | + potdir = r'/data/pot/vasp/potpaw_PBE2010/' |
| 33 | + #delete old POTCAR |
| 34 | + if os.path.exists('./POTCAR'): |
| 35 | + os.remove('./POTCAR') |
| 36 | + for elem in xsd.atoms: |
| 37 | + # if os.path.exists(potdir + elem + '_new/'): |
| 38 | + # potcar = potdir + elem + '_new/POTCAR' |
| 39 | + if os.path.exists(potdir + elem): |
| 40 | + potcar = potdir + elem + '/POTCAR' |
| 41 | + else: |
| 42 | + print 'No POTCAR for ' + elem |
| 43 | + sys.exit(1) |
| 44 | + commands.getstatusoutput('cat ' + potcar + ' >> ./POTCAR') |
| 45 | + |
| 46 | + #creat KPOINTS |
| 47 | + if not args.kpoints: |
| 48 | + kpoints = [] |
| 49 | + for base in xsd.bases: |
| 50 | + l = np.dot(base, base)**0.5 |
| 51 | + kpt = int(20/l) + 1 |
| 52 | + kpoints.append(str(kpt)) |
| 53 | + else: |
| 54 | + kpoints = [i.strip() for i in args.kpoints.split(",")] |
| 55 | + logging.info("Set k-point -> {} {} {}".format(*kpoints)) |
| 56 | + kpt_str = ' '.join(kpoints) |
| 57 | + kpt_content = 'mesh auto\n0\nG\n' + kpt_str + '\n0 0 0\n' |
| 58 | + with open('KPOINTS', 'w') as f: |
| 59 | + f.write(kpt_content) |
45 | 60 |
|
46 | | -#copy INCAR vasp.script |
47 | | -commands.getstatusoutput('cp $HOME/example/INCAR $HOME/example/vasp.script ./') |
48 | | -#change jobname |
49 | | -jobname = output.split('.')[0] |
50 | | -with open('vasp.script', 'r') as f: |
51 | | - content_list = f.readlines() |
| 61 | + #copy INCAR vasp.script |
| 62 | + commands.getstatusoutput('cp $HOME/example/INCAR $HOME/example/vasp.script ./') |
| 63 | + #change jobname |
| 64 | + jobname = output.split('.')[0] |
| 65 | + with open('vasp.script', 'r') as f: |
| 66 | + content_list = f.readlines() |
52 | 67 |
|
53 | | -content_list[1] = '#PBS -N ' + jobname + '\n' |
54 | | -with open('vasp.script', 'w') as f: |
55 | | - f.writelines(content_list) |
| 68 | + content_list[1] = '#PBS -N ' + jobname + '\n' |
| 69 | + with open('vasp.script', 'w') as f: |
| 70 | + f.writelines(content_list) |
56 | 71 |
|
57 | | -#create fort.188 |
58 | | -atom_idxs = [] |
59 | | -atom_names = [] |
60 | | -for idx, atom_name in enumerate(xsd.atom_names): |
61 | | - if atom_name.endswith('_c'): |
62 | | - atom_idxs.append(idx) |
63 | | - atom_names.append(atom_name) |
64 | | -# if constrained get distance and create fort.188 |
65 | | -if atom_idxs: |
66 | | - if len(atom_idxs) > 2: |
67 | | - raise ValueError("More than two atoms end with '_c'") |
68 | | - pt1, pt2 = [xsd.data[idx, :] for idx in atom_idxs] |
69 | | - # use Ax=b convert to cartisan coordinate |
70 | | - diff = pt1 - pt2 |
71 | | - A = np.matrix(xsd.bases.T) |
72 | | - x = np.matrix(diff).T |
73 | | - b = A*x |
74 | | - distance = np.linalg.norm(b) |
75 | | - # create fort.188 |
76 | | - content = '1\n3\n6\n4\n0.04\n%-5d%-5d%f\n0\n' % \ |
77 | | - (atom_idxs[0]+1, atom_idxs[1]+1, distance) |
78 | | - with open('fort.188', 'w') as f: |
79 | | - f.write(content) |
80 | | - print "fort.188 has been created." |
81 | | - print '-'*20 |
82 | | - print "atom number: %-5d%-5d" % (atom_idxs[0]+1, atom_idxs[1]+1) |
83 | | - print "atom name: %s %s" % tuple(atom_names) |
84 | | - print "distance: %f" % distance |
85 | | - print '-'*20 |
| 72 | + #create fort.188 |
| 73 | + atom_idxs = [] |
| 74 | + atom_names = [] |
| 75 | + for idx, atom_name in enumerate(xsd.atom_names): |
| 76 | + if atom_name.endswith('_c'): |
| 77 | + atom_idxs.append(idx) |
| 78 | + atom_names.append(atom_name) |
| 79 | + # if constrained get distance and create fort.188 |
| 80 | + if atom_idxs: |
| 81 | + if len(atom_idxs) > 2: |
| 82 | + raise ValueError("More than two atoms end with '_c'") |
| 83 | + pt1, pt2 = [xsd.data[idx, :] for idx in atom_idxs] |
| 84 | + # use Ax=b convert to cartisan coordinate |
| 85 | + diff = pt1 - pt2 |
| 86 | + A = np.matrix(xsd.bases.T) |
| 87 | + x = np.matrix(diff).T |
| 88 | + b = A*x |
| 89 | + distance = np.linalg.norm(b) |
| 90 | + # create fort.188 |
| 91 | + content = '1\n3\n6\n4\n0.04\n%-5d%-5d%f\n0\n' % \ |
| 92 | + (atom_idxs[0]+1, atom_idxs[1]+1, distance) |
| 93 | + with open('fort.188', 'w') as f: |
| 94 | + f.write(content) |
| 95 | + print "fort.188 has been created." |
| 96 | + print '-'*20 |
| 97 | + print "atom number: %-5d%-5d" % (atom_idxs[0]+1, atom_idxs[1]+1) |
| 98 | + print "atom name: %s %s" % tuple(atom_names) |
| 99 | + print "distance: %f" % distance |
| 100 | + print '-'*20 |
86 | 101 |
|
87 | | - # set IBRION = 1 |
88 | | - incar = InCar() |
89 | | - incar.set('IBRION', 1) |
90 | | - incar.tofile() |
91 | | - print "IBRION is set to 1." |
| 102 | + # set IBRION = 1 |
| 103 | + incar = InCar() |
| 104 | + incar.set('IBRION', 1) |
| 105 | + incar.tofile() |
| 106 | + print "IBRION is set to 1." |
0 commit comments