1
1
#!/usr/bin/python3
2
2
3
3
import numpy as np
4
- import dpdata
4
+ import dpdata , warnings
5
5
6
6
ry2ev = 13.605693009
7
7
hartree2ev = 27.211386018
@@ -36,8 +36,10 @@ def convert_celldm(ibrav, celldm) :
36
36
return celldm [0 ] * 0.5 * np .array ([[1 ,1 ,1 ], [- 1 ,1 ,1 ], [- 1 ,- 1 ,1 ]])
37
37
elif ibrav == - 3 :
38
38
return celldm [0 ] * 0.5 * np .array ([[- 1 ,1 ,1 ], [1 ,- 1 ,1 ], [1 ,1 ,- 1 ]])
39
- else :
40
- raise RuntimeError ('unsupported ibrav ' + str (ibrav ))
39
+ else :
40
+ warnings .warn ('unsupported ibrav ' + str (ibrav ) + ' if no .cel file, the cell convertion may be wrong. ' )
41
+ return np .eye (3 )
42
+ #raise RuntimeError('unsupported ibrav ' + str(ibrav))
41
43
42
44
def load_cell_parameters (lines ) :
43
45
blk = load_block (lines , 'CELL_PARAMETERS' , 3 )
@@ -98,15 +100,16 @@ def _load_pos_block(fp, natoms) :
98
100
head = fp .readline ()
99
101
if not head :
100
102
# print('get None')
101
- return None
103
+ return None , None
102
104
else :
105
+ ss = head .split ()[0 ]
103
106
blk = []
104
107
for ii in range (natoms ) :
105
108
newline = fp .readline ()
106
109
if not newline :
107
- return None
110
+ return None , None
108
111
blk .append ([float (jj ) for jj in newline .split ()])
109
- return blk
112
+ return blk , ss
110
113
111
114
112
115
def load_data (fname ,
@@ -115,18 +118,20 @@ def load_data(fname,
115
118
step = 1 ,
116
119
convert = 1. ) :
117
120
coords = []
121
+ steps = []
118
122
cc = 0
119
123
with open (fname ) as fp :
120
124
while True :
121
- blk = _load_pos_block (fp , natoms )
125
+ blk , ss = _load_pos_block (fp , natoms )
122
126
if blk == None :
123
127
break
124
128
else :
125
129
if cc >= begin and (cc - begin ) % step == 0 :
126
130
coords .append (blk )
131
+ steps .append (ss )
127
132
cc += 1
128
133
coords = convert * np .array (coords )
129
- return coords
134
+ return coords , steps
130
135
131
136
132
137
# def load_pos(fname, natoms) :
@@ -145,14 +150,19 @@ def load_data(fname,
145
150
146
151
def load_energy (fname , begin = 0 , step = 1 ) :
147
152
data = np .loadtxt (fname )
153
+ steps = []
154
+ for ii in data [begin ::step ,0 ]:
155
+ steps .append ('%d' % ii )
148
156
with open (fname ) as fp :
149
- line = fp .readline ()
150
- if line :
151
- nw = len (line .split ())
152
- else :
153
- return None
157
+ while True :
158
+ line = fp .readline ()
159
+ if not line :
160
+ return None
161
+ if line .split ()[0 ][0 ] != '#' :
162
+ nw = len (line .split ())
163
+ break
154
164
data = np .reshape (data , [- 1 , nw ])
155
- return energy_convert * data [begin ::step ,5 ]
165
+ return energy_convert * data [begin ::step ,5 ], steps
156
166
157
167
158
168
# def load_force(fname, natoms) :
@@ -176,35 +186,48 @@ def to_system_data(input_name, prefix, begin = 0, step = 1) :
176
186
data ['atom_types' ], \
177
187
cell \
178
188
= load_param_file (input_name )
179
- data ['coords' ] \
189
+ data ['coords' ], csteps \
180
190
= load_data (prefix + '.pos' ,
181
191
np .sum (data ['atom_numbs' ]),
182
192
begin = begin ,
183
193
step = step ,
184
194
convert = length_convert )
185
195
data ['orig' ] = np .zeros (3 )
186
- data ['cells' ] = np .tile (cell , (data ['coords' ].shape [0 ], 1 , 1 ))
187
- return data
196
+ try :
197
+ data ['cells' ], tmp_steps \
198
+ = load_data (prefix + '.cel' ,
199
+ 3 ,
200
+ begin = begin ,
201
+ step = step ,
202
+ convert = length_convert )
203
+ assert (csteps == tmp_steps ), "the step key between files are not consistent"
204
+ except FileNotFoundError :
205
+ data ['cells' ] = np .tile (cell , (data ['coords' ].shape [0 ], 1 , 1 ))
206
+ return data , csteps
188
207
189
208
190
209
def to_system_label (input_name , prefix , begin = 0 , step = 1 ) :
191
210
atom_names , atom_numbs , atom_types , cell = load_param_file (input_name )
192
- energy = load_energy (prefix + '.evp' ,
193
- begin = begin ,
194
- step = step )
195
- force = load_data (prefix + '.for' ,
196
- np .sum (atom_numbs ),
197
- begin = begin ,
198
- step = step ,
199
- convert = force_convert )
200
- return energy , force
211
+ energy , esteps = load_energy (prefix + '.evp' ,
212
+ begin = begin ,
213
+ step = step )
214
+ force , fsteps = load_data (prefix + '.for' ,
215
+ np .sum (atom_numbs ),
216
+ begin = begin ,
217
+ step = step ,
218
+ convert = force_convert )
219
+ assert (esteps == fsteps ), "the step key between files are not consistent "
220
+ return energy , force , esteps
201
221
202
222
203
223
if __name__ == '__main__' :
204
- atom_names , atom_numbs , atom_types , cell = load_param_file ('oh-md.in' )
205
- coords = load_pos ('oh-md.pos' , np .sum (atom_numbs ))
224
+ prefix = 'nacl'
225
+ atom_names , atom_numbs , atom_types , cell = load_param_file (prefix + '.in' )
226
+ coords = load_data (prefix + '.pos' , np .sum (atom_numbs ))
227
+ cells = load_data (prefix + '.cel' , 3 )
206
228
print (atom_names )
207
229
print (atom_numbs )
208
230
print (atom_types )
209
- print (cell )
231
+ print (cells )
210
232
print (coords .shape )
233
+ print (cells .shape )
0 commit comments