-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmultiplayer1.py
387 lines (329 loc) · 20.3 KB
/
multiplayer1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/python
# -*- coding:utf-8 -*-
"""
@author: XueFeiLiu([email protected]),Nxu
@file: multiplayer.py
@time: 2019/1/29 21:44
A script to produce multi-layers of POSCAR
"""
import os
from numpy import *
import numpy as np
import shutil
import pprint
class VASP(object):
def __init__(self,layers,layerdistance,mode,lata=2.178,latb=2.178,disp_n=200000000000,disp_flag=True,agl=30):
self.lattice=np.zeros((3,3))
self.Selective_infomation=False
self.Direct_mode=True
#self.mode=mode0
self.mode0=mode
self.layers=layers # layers
self.layerdistance=layerdistance
self.displace=disp_n
self.flag_displace=disp_flag
self.rotate0=mat([[cos((agl/360)*2*np.pi),sin((agl/360)*2*np.pi),0],[-sin((agl/360)*2*np.pi),cos((agl/360)*2*np.pi),0],[0,0,1]])
self.M=self.rotate0
self.rotate1=mat([[cos((-agl/360)*2*np.pi),sin((-agl/360)*2*np.pi),0],[-sin((-agl/360)*2*np.pi),cos((-agl/360)*2*np.pi),0],[0,0,1]])
self.M1=self.rotate1
def xyz_read(self):
print('Now reading vasp structures.')
if os.path.exists("POSCAR"):
poscar=open("POSCAR",'r')
elif os.path.exists("CONTCAR"):
poscar=open("CONTCAR",'r')
else:
raise IOError('CONTCAR OR POSCAR does not exist!')
self.title=poscar.readline().rstrip('\r\n').rstrip('\n')
self.scaling_factor=float(poscar.readline())
for i in range(3):
self.lattice[i]=np.array([float(j) for j in poscar.readline().split()])
#self.lattice*=self.scaling_factor
self.element_list=[j for j in poscar.readline().split()]
try:
self.element_amount=[int(j) for j in poscar.readline().split()]
except ValueError:
raise ValueError('VASP 5.x POSCAR is needed!')
line_tmp=poscar.readline()
if line_tmp.strip().upper().startswith("S"):
self.Selective_infomation=True
line_tmp=poscar.readline()
else:# no atoms fixed
self.Selective_infomation=False
if line_tmp.strip().upper().startswith("D"):
self.Direct_mode=True
elif line_tmp.strip().upper().startswith("C"):
self.Direct_mode=False
else:
raise ValueError("POSCAR format is not correct!")
self.total_atom=sum(self.element_amount)
self.atomic_position=np.zeros((self.total_atom,3))
self.Selective_TF=[]
if self.Selective_infomation == True:
for i in range(self.total_atom):
line_tmp=poscar.readline()
self.atomic_position[i]=np.array([float(j) for j in line_tmp.split()[0:3]])
self.Selective_TF.append([j for j in line_tmp.split()[3:]])
else:
for i in range(self.total_atom):
line_tmp=poscar.readline()
self.atomic_position[i]=np.array([float(j) for j in line_tmp.split()[0:3]])
self.atomic_position*=self.scaling_factor
if self.Direct_mode == True:
self.cartesian_position=np.dot(self.atomic_position,self.lattice)
self.direct_position=self.atomic_position
else:
self.cartesian_position=self.atomic_position
self.direct_position=np.dot(self.atomic_position,np.linalg.inv(self.lattice))
poscar.close()
return (self.cartesian_position,self.direct_position)
def xyz_write(self):
print('Now writing new vasp structures.')
if os.path.exists("POSCAR1"):
shutil.copyfile("POSCAR1","POSCAR2")
os.remove("POSCAR1")
writen_lines=[]
writen_lines.append(self.title)
writen_lines.append(str(self.scaling_factor))
for i in range(3):
writen_lines.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(self.lattice[i][0],self.lattice[i][1],self.lattice[i][2]))
writen_lines.append(' '+' '.join(self.element_list))
writen_lines.append(' '+' '.join([str(j) for j in self.element_amount]))
if self.Selective_infomation == True:
writen_lines.append("Selective")
if self.mode0.upper().startswith('C'):
writen_lines.append("Cartesian")
position_to_be_writen=self.cartesian_position
else:
writen_lines.append("Direct")
position_to_be_writen=self.direct_position
position_to_be_writen/=self.scaling_factor
if self.Selective_infomation == True:
for i in range(np.size(position_to_be_writen,0)):
writen_lines.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
else:
for i in range(np.size(position_to_be_writen,0)):
writen_lines.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],
position_to_be_writen[i][2]))
writen_lines=[j+'\n' for j in writen_lines]
poscar=open("POSCAR1",'w')
poscar.writelines(writen_lines)
poscar.close()
def energy_extract(self):
print('Now reading vasp energies.')
if os.path.exists("OUTCAR"):
outcar=open("OUTCAR",'r')
for index,line in enumerate(outcar):
if "energy(sigma->0)" in line:
E0=float(line.split()[-1])
outcar.close()
self.energy=E0
return E0
elif os.path.exists("OSZICAR"):
oszicar=open("OSZICAR",'r')
for index,line in enumerate(oszicar):
if "E0=" in line:
E0=float(line.split()[4])
oszicar.close()
self.energy=E0
return E0
else:
raise IOError('OSZICAR OR OUTCAR does not exist!')
def multilayer(self):
print('Now produce new multi layers vasp structures.')
if os.path.exists("POSCAR_mullayer"+str(self.layers)):
shutil.copyfile("POSCAR_mullayer"+str(self.layers),"POSCAR_mullayer"+str(self.layers)+".bak")
os.remove("POSCAR_mullayer"+str(self.layers))
writen_lines=[]
writen_all_layers=[]
writen_each_layers=[]
writen_lines.append(self.title)
writen_lines.append(str(self.scaling_factor))
self.lattice[2][2]+=self.layerdistance*(self.layers-1)/self.scaling_factor
self.lattice_a=sqrt(self.lattice[0][0]**2+self.lattice[0][1]**2+self.lattice[0][2]**2)
self.lattice_b=sqrt(self.lattice[1][0]**2+self.lattice[1][1]**2+self.lattice[1][2]**2)
for i in range(3):
writen_lines.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(self.lattice[i][0],self.lattice[i][1],self.lattice[i][2]))
writen_lines.append(' '+' '.join(self.element_list))
self.element_amount0=self.element_amount[:]
self.element_amount=[self.element_amount0[i]*self.layers for i in range(len(self.element_amount0))]
writen_lines.append(' '+' '.join([str(j) for j in self.element_amount]))
if self.Selective_infomation == True:
writen_lines.append("Selective")
if self.mode0.upper().startswith('C'):
writen_lines.append("Cartesian")
position_to_be_writen=self.cartesian_position
else:
writen_lines.append("Direct")
position_to_be_writen=self.direct_position
position_to_be_writen/=self.scaling_factor
if self.Selective_infomation == True:
if self.mode0.upper().startswith('D'):
for i in range(np.size(position_to_be_writen,0)):
position_to_be_writen[i][2]-=(self.layers-1)*self.layerdistance/self.scaling_factor/2/self.lattice[2][2]
else:
pass
for num_layer in range(self.layers):
if num_layer < 1:
for i in range(np.size(position_to_be_writen,0)):
if self.mode0.upper().startswith('D'):
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
else:
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
else:
if self.flag_displace == True:
for i in range(np.size(position_to_be_writen,0)):
if self.mode0.upper().startswith('D'):
position_xyz=mat(position_to_be_writen[i])*self.M
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]+=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]+=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
else:
position_xyz=mat(position_to_be_writen[i])*self.M
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]+=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]+=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor
self.flag_displace = False
else:
for i in range(np.size(position_to_be_writen,0)):
if self.mode0.upper().startswith('D'):
position_xyz=mat(position_to_be_writen[i])*self.M1
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]-=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]-=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
else:
position_xyz=mat(position_to_be_writen[i])*self.M1
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]-=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]-=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f} {3:4}{4:4}{5:4}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1], \
position_to_be_writen[i][2],self.Selective_TF[i][0],\
self.Selective_TF[i][1],self.Selective_TF[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor
self.flag_displace = True
writen_all_layers=[j+'\n' for j in writen_all_layers]
writen_each_layers=[writen_all_layers[k:k+self.total_atom] for k in range(0,len(writen_all_layers),self.total_atom)]
else:
if self.mode0.upper().startswith('D'):
for i in range(np.size(position_to_be_writen,0)):
position_to_be_writen[i][2]-=(self.layers-1)*self.layerdistance/self.scaling_factor/2/self.lattice[2][2]
else:
pass
for num_layer in range(self.layers):
if num_layer < 1:
for i in range(np.size(position_to_be_writen,0)):
if self.mode0.upper().startswith('D'):
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],position_to_be_writen[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
else:
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],position_to_be_writen[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor
else:
if self.flag_displace == True:
for i in range(np.size(position_to_be_writen,0)):
if self.mode0.upper().startswith('D'):
position_xyz=mat(position_to_be_writen[i])*self.M
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]+=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]+=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],position_to_be_writen[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
else:
position_xyz=mat(position_to_be_writen[i])*self.M
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]+=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]+=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],position_to_be_writen[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor
self.flag_displace = False
else:
for i in range(np.size(position_to_be_writen,0)):
if self.mode0.upper().startswith('D'):
position_xyz=mat(position_to_be_writen[i])*self.M1
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]-=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]-=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],position_to_be_writen[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor/self.lattice[2][2]
else:
position_xyz=mat(position_to_be_writen[i])*self.M1
position_to_be_writen[i]=position_xyz
position_to_be_writen[i][0]-=self.lattice_a/self.displace/self.scaling_factor
position_to_be_writen[i][1]-=self.lattice_b/self.displace/self.scaling_factor
position_to_be_writen[i][2]+=num_layer*self.layerdistance/self.scaling_factor
writen_all_layers.append("{0:>15.8f}{1:>15.8f}{2:>15.8f}" \
.format(position_to_be_writen[i][0],position_to_be_writen[i][1],position_to_be_writen[i][2]))
position_to_be_writen[i][2]-=num_layer*self.layerdistance/self.scaling_factor
self.flag_displace = True
writen_all_layers=[j+'\n' for j in writen_all_layers]
#pprint.pprint(writen_all_layers)
writen_each_layers=[writen_all_layers[k:k+self.total_atom] for k in range(0,len(writen_all_layers),self.total_atom)]
#pprint.pprint(writen_each_layers)
writen_lines=[j+'\n' for j in writen_lines]
poscar=open("POSCAR_mullayer"+str(self.layers),'w')
poscar.writelines(writen_lines)
j=0
#print(self.element_amount0)
for i in self.element_amount0:
for num_layer in range(self.layers):
poscar.writelines(writen_each_layers[num_layer][j:j+i])
j+=i
poscar.close()
if __name__ == "__main__":
a=str("Given the number of layers ,distance beween two layers and type of atoms coordination you want to build:\n")
words=len(a)
print("*"*words)
print(a)
print("*"*words)
#n_layer,ldistance=input("Input the number of layers and distance:\n")
n_layer=int(input("Input the number of layers:\n"))
ldistance=float(input("Input the distance of two layers:\n"))
coordtype=str(input("Input the type of atoms coordination(C or D):\n"))
angle0=float(input("Input the angle you want to rotate:\n"))
distance_n=float(input("Input the displace number according to supercell:\n"))
poscar=VASP(layers=n_layer,layerdistance=ldistance,mode=coordtype,agl=angle0,disp_n=distance_n)
cartesian_position,direct_position=poscar.xyz_read()
#print(cartesian_position)
poscar.multilayer()