chimie4psmn / database / import_data.py @ 48
Historique | Voir | Annoter | Télécharger (8,19 ko)
1 | 18 | tjiang | # $Id$
|
---|---|---|---|
2 | 38 | tjiang | import os, sys, argparse |
3 | 15 | tjiang | from ase import Atoms |
4 | 15 | tjiang | from ase.io import read |
5 | 15 | tjiang | from ase.db import connect |
6 | 15 | tjiang | from ase.calculators.vasp import Vasp |
7 | 31 | tjiang | from ase.constraints import FixAtoms |
8 | 31 | tjiang | from numpy.linalg import norm |
9 | 32 | tjiang | from bz2 import BZ2File |
10 | 33 | tjiang | from gzip import GzipFile |
11 | 15 | tjiang | |
12 | 24 | bschweit | |
13 | 38 | tjiang | def import_vasp_calculations(path,con,include_dir=[],exclude_dir=[],use_poscar=False,creator=os.environ['USER']): |
14 | 15 | tjiang | log = open('success.log','w') |
15 | 15 | tjiang | errlog = open('err.log','w') |
16 | 32 | tjiang | _all = os.walk(os.path.abspath(path)) |
17 | 15 | tjiang | calculator=None
|
18 | 21 | tjiang | xc_dict = {'8': 'PBE', '91': 'PW91', 'CA': 'LDA'} |
19 | 21 | tjiang | read_all_dir = 0
|
20 | 21 | tjiang | if len(include_dir) == 0: |
21 | 21 | tjiang | read_all_dir = 1
|
22 | 15 | tjiang | for files in _all: |
23 | 15 | tjiang | vasp_out = [i for i in range(len(files[2])) if ('OUTCAR' in files[2][i] and files[2][i][:6]=='OUTCAR')] |
24 | 21 | tjiang | condition1 = (read_all_dir or files[0].split('/')[-1] in include_dir) |
25 | 24 | bschweit | #condition2 = not files[0].split('/')[-1] in exclude_dir
|
26 | 24 | bschweit | condition3 = [ i for i, x in enumerate(exclude_dir) if x in files[0] ] |
27 | 24 | bschweit | if len(vasp_out) != 0 and condition1 and not condition3 : |
28 | 15 | tjiang | ncalc = 0
|
29 | 15 | tjiang | for f in vasp_out: |
30 | 15 | tjiang | try:
|
31 | 15 | tjiang | Atoms = read(files[0]+'/'+files[2][f]) |
32 | 38 | tjiang | if use_poscar:
|
33 | 31 | tjiang | constraints = read('POSCAR').constraints
|
34 | 31 | tjiang | Atoms.set_constraint(constraints) |
35 | 32 | tjiang | print 'get constraint from poscar' |
36 | 31 | tjiang | else:
|
37 | 32 | tjiang | Atoms0 = read(files[0]+'/'+files[2][f], index=0) |
38 | 31 | tjiang | pos0 = Atoms0.positions |
39 | 31 | tjiang | pos = Atoms.positions |
40 | 31 | tjiang | diff = pos - pos0 |
41 | 31 | tjiang | disp = norm(diff, axis=1)
|
42 | 31 | tjiang | if disp.sum() > 0: |
43 | 32 | tjiang | print 'find constraint from outcar' |
44 | 31 | tjiang | mask = [_disp == 0 for _disp in disp] |
45 | 31 | tjiang | constraints = FixAtoms(mask=mask) |
46 | 31 | tjiang | Atoms.set_constraint(constraints) |
47 | 31 | tjiang | else:
|
48 | 32 | tjiang | print 'no constraint' |
49 | 31 | tjiang | constraints = None
|
50 | 31 | tjiang | #os.system('cp '+files[0]+'/'+files[2][f]+' OUTCAR')
|
51 | 31 | tjiang | #calc = Vasp()
|
52 | 31 | tjiang | #calc.atoms = Atoms
|
53 | 31 | tjiang | #calc.sort = list(range(len(Atoms)))
|
54 | 31 | tjiang | #calc.resort = list(range(len(Atoms)))
|
55 | 31 | tjiang | #calc.read_outcar()
|
56 | 31 | tjiang | #Atoms = calc.atoms
|
57 | 31 | tjiang | #nbands = calc.get_number_of_bands()
|
58 | 31 | tjiang | ##xc = calc.get_xc_functional()
|
59 | 31 | tjiang | #os.system('rm -f OUTCAR')
|
60 | 15 | tjiang | |
61 | 15 | tjiang | #read version number, XC, ENCUT etc.
|
62 | 32 | tjiang | if files[2][f][-4:] == '.bz2': |
63 | 32 | tjiang | fobj = BZ2File(files[0]+'/'+files[2][f]) |
64 | 33 | tjiang | elif files[2][f][-3:] == '.gz': |
65 | 33 | tjiang | fobj = GzipFile(files[0]+'/'+files[2][f]) |
66 | 32 | tjiang | else:
|
67 | 32 | tjiang | fobj = open(files[0]+'/'+files[2][f]) |
68 | 32 | tjiang | with fobj as outcar: |
69 | 15 | tjiang | version = outcar.readline() |
70 | 15 | tjiang | line = outcar.readline() |
71 | 20 | tjiang | read_para = 0
|
72 | 32 | tjiang | lsol = False
|
73 | 32 | tjiang | ldipol = False
|
74 | 20 | tjiang | while line != '': |
75 | 32 | tjiang | if line.startswith(' INCAR:'): |
76 | 32 | tjiang | line = outcar.readline().split() |
77 | 32 | tjiang | if line[1] == 'PAW_PBE': |
78 | 32 | tjiang | pot = 'PAW'
|
79 | 32 | tjiang | else:
|
80 | 32 | tjiang | pot = line[1]
|
81 | 32 | tjiang | elif line.startswith(' Dimension of arrays'): |
82 | 32 | tjiang | #if line.startswith(' Dimension of arrays'):
|
83 | 20 | tjiang | read_para = 1
|
84 | 20 | tjiang | elif read_para:
|
85 | 21 | tjiang | if 'LEXCH' in line: |
86 | 24 | bschweit | xc_flag = line.split()[2].upper()
|
87 | 20 | tjiang | if xc_flag not in xc_dict.keys(): |
88 | 20 | tjiang | raise ValueError('Unknown xc-functional flag found in POTCAR,' |
89 | 20 | tjiang | ' LEXCH=%s' % xc_flag)
|
90 | 20 | tjiang | xc = xc_dict[xc_flag] |
91 | 20 | tjiang | elif 'NKPTS' in line: |
92 | 31 | tjiang | #if 'NKPTS' in line:
|
93 | 20 | tjiang | nkpts = int(line.split()[3]) |
94 | 21 | tjiang | elif 'ENCUT' in line: |
95 | 20 | tjiang | encut = float(line.split()[2]) |
96 | 21 | tjiang | elif 'ENAUG' in line: |
97 | 20 | tjiang | enaug = float(line.split()[2]) |
98 | 31 | tjiang | elif 'NBANDS' in line: |
99 | 31 | tjiang | nbands = int(line.split()[-1]) |
100 | 31 | tjiang | elif 'EDIFFG' in line: |
101 | 32 | tjiang | f_limit = -float(line.split()[2]) |
102 | 21 | tjiang | elif 'LSOL' in line: |
103 | 20 | tjiang | if line.split()[2] == 'T': |
104 | 20 | tjiang | lsol = True
|
105 | 21 | tjiang | elif 'LDIPOL' in line: |
106 | 20 | tjiang | if line.split()[2] == 'T': |
107 | 20 | tjiang | ldipol = True
|
108 | 21 | tjiang | elif '-'*104 in line: |
109 | 21 | tjiang | break
|
110 | 15 | tjiang | line = outcar.readline() |
111 | 31 | tjiang | if constraints is not None: |
112 | 32 | tjiang | fmax = ((Atoms.get_forces()) ** 2).sum(1).max()**0.5 |
113 | 31 | tjiang | if fmax < f_limit:
|
114 | 32 | tjiang | print 'updating the database with minimized calculation from file: ', fmax, files[2][f] |
115 | 38 | tjiang | if use_poscar:
|
116 | 32 | tjiang | con.write(Atoms,functional = xc,path=files[0],code='VASP',filename=files[2][f], version=version, potential=pot, encut=encut, enaug=enaug, lsol=lsol, ldipol=ldipol, nkpts=nkpts, constraint=True) |
117 | 31 | tjiang | else:
|
118 | 32 | tjiang | con.write(Atoms,functional = xc,creator=creator,path=files[0],code='VASP',filename=files[2][f], version=version, potential=pot, encut=encut, enaug=enaug, lsol=lsol, ldipol=ldipol, nkpts=nkpts, constraint=True) |
119 | 32 | tjiang | else:
|
120 | 32 | tjiang | print 'not updating the database as fmax is too big for file: ', fmax, files[2][f] |
121 | 31 | tjiang | else:
|
122 | 32 | tjiang | print 'updating the database with calculation', files[2][f] |
123 | 32 | tjiang | con.write(Atoms,functional = xc,creator=creator,path=files[0],code='VASP',filename=files[2][f], version=version, potential=pot, encut=encut, enaug=enaug, lsol=lsol, ldipol=ldipol, nkpts=nkpts, constraint=False) |
124 | 32 | tjiang | print >> log, files[0], files[2][f] |
125 | 15 | tjiang | except (IndexError, ValueError): |
126 | 32 | tjiang | print >> errlog, files[0], files[2][f] |
127 | 15 | tjiang | |
128 | 15 | tjiang | log.flush() |
129 | 15 | tjiang | errlog.flush() |
130 | 15 | tjiang | |
131 | 15 | tjiang | #def import_adf_calculations(path,con):
|
132 | 15 | tjiang | # log = open('success.log','w')
|
133 | 15 | tjiang | # errlog = open('err.log','w')
|
134 | 15 | tjiang | # _all = os.walk(path)
|
135 | 15 | tjiang | # calculator=None
|
136 | 15 | tjiang | # for files in _all:
|
137 | 15 | tjiang | # vasp_out = [i for i in range(len(files[2])) if ('OUTCAR' in files[2][i] and files[2][i][:6]=='OUTCAR')]
|
138 | 15 | tjiang | # if len(vasp_out) != 0:
|
139 | 15 | tjiang | # print files[0]
|
140 | 15 | tjiang | # ncalc = 0
|
141 | 15 | tjiang | # for f in vasp_out:
|
142 | 15 | tjiang | # calculator = 'vasp'
|
143 | 15 | tjiang | # try:
|
144 | 15 | tjiang | # Atoms = read(files[0]+'/'+files[2][f])
|
145 | 15 | tjiang | # con.write(Atoms,creator='rkerber',path=files[0],code='vasp',filename=files[2][f])
|
146 | 15 | tjiang | # print >> log, files[0]
|
147 | 15 | tjiang | # except (IndexError, ValueError):
|
148 | 15 | tjiang | # print >> errlog, files[0]
|
149 | 15 | tjiang | #
|
150 | 15 | tjiang | # log.flush()
|
151 | 15 | tjiang | # errlog.flush()
|
152 | 38 | tjiang | |
153 | 38 | tjiang | |
154 | 38 | tjiang | if __name__ == '__main__': |
155 | 38 | tjiang | parser = argparse.ArgumentParser() |
156 | 38 | tjiang | parser.add_argument("-p", help="Use POSCAR in the same directory to determine the constraint", action="store_true") |
157 | 38 | tjiang | args = parser.parse_args() |
158 | 38 | tjiang | if args.p:
|
159 | 38 | tjiang | use_poscar = True
|
160 | 38 | tjiang | else:
|
161 | 38 | tjiang | use_poscar = False
|
162 | 38 | tjiang | |
163 | 32 | tjiang | con = connect('test.db')
|
164 | 32 | tjiang | #import_vasp_calculations('/data/users/tjiang/rkerber', con)
|
165 | 32 | tjiang | #import_vasp_calculations('.', con, include_dir=['<opt_dir>'], exclude_dir=['<to_exclude_dir1>', '<to_exclude_dir2>'])
|
166 | 38 | tjiang | import_vasp_calculations('.', con, use_poscar=use_poscar) |