forked from ChenXinAtPKU/PHMLBE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PH.py
2235 lines (2120 loc) · 95.7 KB
/
PH.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import subprocess
import numpy
import math
import matplotlib.pyplot as plt
import random
import os
import shutil
import re
import time
import keras
from sklearn.externals import joblib
from ripser import ripser
from sklearn.utils import shuffle
from sklearn import metrics
from sklearn.model_selection import train_test_split,GridSearchCV,learning_curve,validation_curve
from keras.layers.normalization import BatchNormalization
from sklearn.preprocessing import StandardScaler,MinMaxScaler
from sklearn.ensemble import GradientBoostingRegressor as GBR
from scipy.stats import pearsonr
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation,Flatten
from keras.layers import Convolution1D,MaxPooling1D,AveragePooling1D
from keras.optimizers import SGD,Adam
from keras.models import load_model
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from matplotlib.backends.backend_pdf import PdfPages
def readposcar(target_pos):
pos = []
f = open(target_pos)
try:
for line in f:
pos.append(line)
except:
f.close()
lattice = []
pos_all=[]
for item in pos[2:5]:
try:
lattice.append(list(map(float, item.split())))
except:
return False
for item in lattice:
if len(item) != 3: return False
#for item in pos[6]:
# try:
# orig_atom_num=list(map(float, item.split()))
# except:
# return False
for item in pos[8:]:
try:
pos_all.append(list(map(float, item.split())))
except:
return False
return([lattice,pos_all])
def getsuperpoint(lattice,pos,N):
ID=0
superpoint=[]
superID=[]
for xi in range(N):
superID.append([])
for yi in range(N):
superID[xi].append([])
for zi in range(N):
superID[xi][yi].append([])
for posi in range(len(pos)):
tempx=pos[posi][0]*lattice[0][0]+pos[posi][1]*lattice[1][0]+pos[posi][2]*lattice[2][0]+xi*lattice[0][0]+yi*lattice[1][0]+zi*lattice[2][0]
tempy=pos[posi][0]*lattice[0][1]+pos[posi][1]*lattice[1][1]+pos[posi][2]*lattice[2][1]+xi*lattice[0][1]+yi*lattice[1][1]+zi*lattice[2][1]
tempz=pos[posi][0]*lattice[0][2]+pos[posi][1]*lattice[1][2]+pos[posi][2]*lattice[2][2]+xi*lattice[0][2]+yi*lattice[1][2]+zi*lattice[2][2]
superpoint.append([tempx,tempy,tempz])
superID[xi][yi][zi].append(ID)
ID=ID+1
return superpoint,superID
def makeposcar(target_pos,lattice,pos):
f = open(target_pos,'w')
f.write('by code\n1.0\n')
for item in lattice:
f.write('%15.8f %15.8f %15.8f\n' % tuple(item))
f.write('Li \n%d\ndirect\n'%len(pos))
for item in pos:
f.write('%15.8f %15.8f %15.8f\n' % tuple(item))
f.close()
def point2poscar(target_pos,point):
LATIICE_n=round(len(point)/2)+15
lattice_tmp=[[LATIICE_n,0,0],[0,LATIICE_n,0],[0,0,LATIICE_n]]
tem_point=[]
for item in point:
tem_point.append(list(map(lambda x:x/LATIICE_n+0.5,item)))
makeposcar(target_pos,lattice_tmp,tem_point)
def pos_vibra(pos,model):
# beste=0
# return pos,beste
buchang=(1/far_x)*1.5
atom_num=len(pos)
betti_all=[]
newpos=list(range(len(pos)))
for ji in range(len(pos)):
newpos[ji]=list(pos[ji])
timer=0
goon=1
bestpos=list(pos)
whole_time=0
oldbetti=pos2betti([pos])
betti_all.append(oldbetti[0])
# oldbetti[0].append(round(far_old[0]*far_k)/far_k)
olde=model.predict(numpy.array(oldbetti))
beste=olde[0]
while goon==1:
whole_time=whole_time+1
for i_p in range(len(pos)):
newpos[i_p]=list([pos[i_p][0]+random.random()*random.randint(-1,1)*buchang , pos[i_p][1]+random.random()*random.randint(-1,1)*buchang , pos[i_p][2]+random.random()*random.randint(-1,1)*buchang ])
newbetti=pos2betti([newpos])
if newbetti[0] in betti_all:
print('dup')
continue
betti_all.append(newbetti[0])
oldbetti=pos2betti([pos])
# far_new=pos_farest_dis([newpos])
# far_old=pos_farest_dis([pos])
# newbetti[0].append(round(far_new[0]*far_k)/far_k)
# oldbetti[0].append(round(far_old[0]*far_k)/far_k)
if newbetti[0]== oldbetti[0]:
newpos=list(pos)
continue
newe=model.predict(numpy.array(newbetti))
olde=model.predict(numpy.array(oldbetti))
# print('old:%f new:%f'%(olde[0],newe[0]))
if (newe[0]-olde[0])<=0:
timer=0
pos=list(newpos)
if beste>newe[0]:
beste=newe[0]
bestpos=list(newpos)
continue
else:
if math.exp(-(newe[0]-olde[0])*2625*atom_num)>random.random() and timer<50: #1 A.U.=1 hatree = 2625.500 KJ/mol
print(math.exp(-(newe[0]-olde[0])*2625))
pos=list(newpos)
continue
else:
# print(math.exp(-(newe[0]-olde[0])))
timer=timer+1
newpos=list(pos)
if timer>atom_num*10:
print('done---------------timer>50-----------------------------------------------------------')
print(whole_time)
break
# buchang=buchang*2
# timer=0
# if buchang>xbuchang:
# break
if whole_time>atom_num*10000:
print('done---------------whole_time>10000---------------------------------------------------')
print(whole_time)
break
return bestpos,beste
#def pos_vibra(pos,model):
# buchang=(1/far_x)*1.5
# atom_num=len(pos)
# newpos=list(range(len(pos)))
# for ji in range(len(pos)):
# newpos[ji]=list(pos[ji])
# timer=0
# goon=1
# bestpos=list(pos)
# whole_time=0
# oldbetti=pos2betti([pos])
# far_old=pos_farest_dis([pos])
## oldbetti[0].append(round(far_old[0]*far_k)/far_k)
# olde=model.predict(oldbetti)
# beste=olde[0]
# while goon==1:
# whole_time=whole_time+1
# for i_p in range(len(pos)):
# newpos[i_p]=list([pos[i_p][0]+random.random()*random.randint(-1,1)*buchang , pos[i_p][1]+random.random()*random.randint(-1,1)*buchang , pos[i_p][2]+random.random()*random.randint(-1,1)*buchang ])
# newbetti=pos2betti([newpos])
# oldbetti=pos2betti([pos])
# # far_new=pos_farest_dis([newpos])
# # far_old=pos_farest_dis([pos])
# # newbetti[0].append(round(far_new[0]*far_k)/far_k)
# # oldbetti[0].append(round(far_old[0]*far_k)/far_k)
# if newbetti[0]== oldbetti[0]:
# newpos=list(pos)
# continue
# newe=model.predict(newbetti)
# olde=model.predict(oldbetti)
# # print('old:%f new:%f'%(olde[0],newe[0]))
# if (newe[0]-olde[0])<=0:
# timer=0
# pos=list(newpos)
# if beste>newe[0]:
# beste=newe[0]
# bestpos=list(newpos)
# continue
# else:
#
# if math.exp(-(newe[0]-olde[0])*2625500)>random.random() and timer<50: #1 A.U.=1 hatree = 2625.500 KJ/mol
# # print(math.exp(-(newe[0]-olde[0])))
# pos=list(newpos)
# continue
# else:
# # print(math.exp(-(newe[0]-olde[0])))
# timer=timer+1
# newpos=list(pos)
# if timer>atom_num*20:
# print('done---------------timer>50-----------------------------------------------------------')
# print(whole_time)
# break
## buchang=buchang*2
## timer=0
#
## if buchang>xbuchang:
## break
#
# if whole_time>atom_num*1000:
# print('done---------------whole_time>10000---------------------------------------------------')
# print(whole_time)
# break
# return bestpos,beste
def pos_pred_ene(pos,model):
oldbetti=pos2betti([pos])
# far_old=pos_farest_dis([pos])
# oldbetti[0].append(round(far_old[0]*far_k)/far_k)
olde=model.predict(oldbetti)
print(olde)
return olde
def dist_matric(pos):
dis_mat=[]
if len(pos)<100:
for def_i in range(1,len(pos)):
for def_j in range(def_i):
temp=math.sqrt(math.pow(pos[def_j][0]-pos[def_i][0],2)+math.pow(pos[def_j][1]-pos[def_i][1],2)+math.pow(pos[def_j][2]-pos[def_i][2],2))
dis_mat.append(temp)
dis_mat.sort()
return(dis_mat)
def dist_matric2(pos):
dis_mat=[]
for def_i in range(0,len(pos)):
dis_mat.append([])
for def_j in range(0,len(pos)):
temp=math.sqrt(math.pow(pos[def_j][0]-pos[def_i][0],2)+math.pow(pos[def_j][1]-pos[def_i][1],2)+math.pow(pos[def_j][2]-pos[def_i][2],2))
dis_mat[def_i].append(temp)
return(dis_mat)
def pos_center_dist_mat(pos_tmp,c_mat):
c_mat1=[]
for i_2 in range(len(pos_tmp)):
tmp_dis= math.pow(pos_tmp[i_2][0]-c_mat[0],2)+math.pow(pos_tmp[i_2][1]-c_mat[1],2)+math.pow(pos_tmp[i_2][2]-c_mat[2],2)
c_mat.append(tmp_dis)
c_mat1.sort()
return c_mat
def check_dup(pos1,pos2,limit=0.1):
# pos1_tmp,c1_pmat=pos2standard_coord([pos1])
# pos2_tmp,c2_pmat=pos2standard_coord([pos2])
# c1_mat=pos_center_dist_mat(pos1_tmp[0],[0,0,0])
# c2_mat=pos_center_dist_mat(pos2_tmp[0],[0,0,0])
# c1_mat.sort()
# c2_mat.sort()
pos1_mat=dist_matric(pos1)
pos2_mat=dist_matric(pos2)
for def_i2 in range(len(pos1_mat)):
if abs(abs(pos1_mat[def_i2]) - abs(pos2_mat[def_i2]))>limit:
return False
return True
def check_dup2(pos1_list,pos2,limit=0.1):
for item1 in range(len(pos1_list)):
if check_dup(pos1_list[item1],pos2,limit):
return True
return False
def too_quick(v):
if abs(v)>0.5*atom_distances:
return v/abs(v)*0.5*atom_distances
else:
return v
def too_close(pos,dis=False):
min_dis=math.pow(atom_distances-1.5,2)#-0.8
if dis:
min_dis=math.pow(dis,2)
for i in range(1,len(pos)):
for j in range(i):
if math.pow(pos[i][0]-pos[j][0],2)+math.pow(pos[i][1]-pos[j][1],2)+math.pow(pos[i][2]-pos[j][2],2) <min_dis:
return True
return False
def too_far(pos,dis=False):
max_dis=math.pow(atom_distances+1,2)
if dis:
max_dis=math.pow(dis,2)
tmp_list=[pos[0]]
for k in range(len(pos)):
for i in tmp_list:
for j in pos:
if math.pow(i[0]-j[0],2)+math.pow(i[1]-j[1],2)+math.pow(i[2]-j[2],2) < max_dis and j not in tmp_list:#+0.3
tmp_list.append(j)
if len(tmp_list)!=len(pos):
return True
else:
return False
def search_close_point(ID,exit_ID,N_super,label='FCC'): #!only for FCC, and 0 is z_low atom, 1 is z_high atom! #only for FCC #only for FCC #only for FCC #only for FCC #only for FCC
#print('search_close_point begin')
if label == 'FCC':
close_ID1=[ [ID[0]+1,ID[1],ID[2],ID[3]] , [ID[0],ID[1]+1,ID[2],ID[3]] , [ID[0]+1,ID[1]+1,ID[2],ID[3]] , [ID[0]-1,ID[1],ID[2],ID[3]] , [ID[0],ID[1]-1,ID[2],ID[3]], [ID[0]-1,ID[1]-1,ID[2],ID[3]] ]
if ID[3]==0:
close_ID2=[ [ID[0],ID[1],ID[2],ID[3]+1] , [ID[0],ID[1]+1,ID[2],ID[3]+1] , [ID[0]-1,ID[1],ID[2],ID[3]+1] ]
close_ID3=[ [ID[0],ID[1],ID[2]-1,ID[3]+1] , [ID[0],ID[1]+1,ID[2]-1,ID[3]+1] , [ID[0]-1,ID[1],ID[2]-1,ID[3]+1] ]
if ID[3]==1:
close_ID2=[ [ID[0],ID[1],ID[2],ID[3]-1] , [ID[0],ID[1]-1,ID[2],ID[3]-1] , [ID[0]+1,ID[1],ID[2],ID[3]-1] ]
close_ID3=[ [ID[0],ID[1],ID[2]+1,ID[3]-1] , [ID[0],ID[1]-1,ID[2]+1,ID[3]-1] , [ID[0]+1,ID[1],ID[2]+1,ID[3]-1] ]
close_ID=close_ID1 + close_ID2 + close_ID3
for i in range(len(close_ID)):
close_ID[i]=[close_ID[i][0]%N_super,close_ID[i][1]%N_super,close_ID[i][2]%N_super,close_ID[i][3]]
if label == 'BCC':
if ID[3]==0:
close_ID1=[ [ID[0],ID[1],ID[2],1] , [ID[0]-1,ID[1],ID[2],1] , [ID[0],ID[1]-1,ID[2],1] , [ID[0]-1,ID[1]-1,ID[2],1] ]
close_ID2=[ [ID[0],ID[1],ID[2]-1,1] , [ID[0]-1,ID[1],ID[2]-1,1] , [ID[0],ID[1]-1,ID[2]-1,1] , [ID[0]-1,ID[1]-1,ID[2]-1,1] ]
if ID[3]==1:
close_ID1=[ [ID[0],ID[1],ID[2],0] , [ID[0]+1,ID[1],ID[2],0] , [ID[0],ID[1]+1,ID[2],0] , [ID[0]+1,ID[1]+1,ID[2],0] ]
close_ID2=[ [ID[0],ID[1],ID[2]+1,0] , [ID[0]+1,ID[1],ID[2]+1,0] , [ID[0],ID[1]+1,ID[2]+1,0] , [ID[0]+1,ID[1]+1,ID[2]+1,0] ]
close_ID=close_ID1 + close_ID2
for i in range(len(close_ID)):
close_ID[i]=[close_ID[i][0]%N_super,close_ID[i][1]%N_super,close_ID[i][2]%N_super,close_ID[i][3]]
x_try=0
while x_try<7:
x_try=x_try+1
if label == 'FCC':
No_ID_temp=random.randint(0,11) #! first_close atom number
if label == 'BCC':
No_ID_temp=random.randint(0,7) #! first_close atom number
if close_ID[No_ID_temp] not in exit_ID:
#print('search_close_point done')
return close_ID[No_ID_temp]
return []
def make_vasp(target,point,if_run,if_opt=True,if_pred=False):
file_Kpt=target+'/KPOINT'
file_INC=target+'/INCAR'
file_POS=target+'/POSCAR'
with open( file_Kpt,'w' ) as fk:
fk.write('AUTO GRID\n0\nG\n1 1 1\n0 0 0')
with open( file_INC,'w' ) as fi:
fi.write('SYSTEM=PSO\nLWAVE=.FALSE.\nLCHARG=.FALSE.\nISTART=0\nALGO=Fast\nENCUT=500\nLORBIT=11\nISMEAR=0\nPREC=Normal\n')
fi.write('NELMIN=3\nPOTIM=0.2\nISIF=2\nEDIFF=0.1E-3\nEDIFFG=-1.0E-2\nLREAL = .False.\nNCORE= 4\nSIGMA=0.1\n')
fi.write('NELM=30\n')
if if_opt and not if_pred:
fi.write('NSW=20\nIBRION=2\nISPIN=1\n')
if if_opt and if_pred:
fi.write('NSW=100\nIBRION=3\nISPIN=2\n')
else:
fi.write('NSW=0\n')
point2poscar(file_POS,point)
subprocess.call('cp pbs-vasp %s'%target,shell=True)
subprocess.call('cp POTCAR %s'%target,shell=True)
if if_run:
orig_os=os.getcwd()
os.chdir(target)
subprocess.call('qsub pbs-vasp',shell=True)
os.chdir(orig_os)
def make_gaus(target,point,if_run,pred=False,nomaxcycle=True,opt=False):
len_point=len(point)
if pred==True:
mag_range=[1]
elif len_point<12:
if len_point%2==0 and pred==False:
mag_range=list(range(1,len_point+1,2))
elif len_point%2==1 and pred==False:
mag_range=list(range(2,len_point+1,2))
elif len_point>=12:
if len_point%2==0 and pred==False:
mag_range=list(range(1,round(len_point/3)+2,2))
elif len_point%2==1 and pred==False:
mag_range=list(range(2,round(len_point/3)+2,2))
# if len_point%2==0 :
# mag_range=[1]
# else:
# mag_range=[2]
for i in mag_range:
file_calc_2=target+'/'+'%d'%i
os.mkdir(file_calc_2)
file_gau=file_calc_2+'/dmac.gjf'
with open( file_gau,'w' ) as fg:
if not opt:
fg.write('%chk=C.chk\n%mem=10GB\n\n# bpw91/6-31G pop=full nosym scf=(maxcycle=80,xqc)\n\npso\n\n')
if opt:
fg.write('%chk=C.chk\n%mem=10GB\n\n# bpw91/6-31G pop=full opt=(maxstep=50,maxcycle=50,loose) nosym scf=(maxcycle=80,xqc)\n\npso\n\n')
fg.write('0 %d\n'%i)
for item in point:
fg.write('Li %15.8f %15.8f %15.8f\n' % tuple(item))
fg.write('\n')
subprocess.call('cp gaupbs %s'%file_calc_2,shell=True)
if if_run:
orig_os=os.getcwd()
os.chdir(file_calc_2)
subprocess.call('qsub gaupbs',shell=True)
os.chdir(orig_os)
def if_cal_done_vasp(target,pop_size):
try:
if_done=True
for i in range(1,pop_size+1):
vasp_out=target+'/'+'%d'%i+'/OUTCAR'
vasp_out2=target+'/'+'%d'%i+'/vasp.out'
vpo=open(vasp_out,'r')
vpread=vpo.read()
vpo2=open(vasp_out2,'r')
vpread2=vpo2.read()
if_1=re.search('CPU time used',vpread)
if_2=re.search('reached required',vpread)
if_3=re.search('please rerun with',vpread2)
if if_1==None and if_2==None and if_3==None:
print('not done %s'%vasp_out)
if_done=False
return False
print('done %s'%vasp_out)
if if_done==True:
return True
except:
return False
def if_cal_done_gaus(target,pop_size):
if_done=True
for i in range(1,pop_size+1):
try:
#print(1)
target1=target+'/'+'%d'%i
mag_list=os.listdir(target1)
# print(2)
for j in mag_list:
target2=target1+'/'+j
gaus_log=target2+'/'+'DMAC.log'
with open(gaus_log,'r') as gl:
glread=gl.read()
print(i)
print(j)
if_1=re.search('Job cpu time',glread)
if_2=re.search('termination',glread)
if if_1==None and if_2==None:
if_done=False
return False
except:
# print('i')
return False
if if_done==True:
return True
def random_point_gen(atom_num,pos_all,posID_all,add_how_much,label):
#print('random_point_gen begin')
if label == 'BCC':
lattice,pos=readposcar('POSCAR0')
if label == 'FCC':
lattice,pos=readposcar('POSCAR1')
if atom_num<10:
N_super=round(atom_num*3)
else:
N_super=round(atom_num)
supercell,superID=getsuperpoint(lattice,pos,N_super)
center_atom=round((N_super-1)/2)
origi_posall_len=len(pos_all)
while len(pos_all)-origi_posall_len<add_how_much:
pos=[]
pos_real=[]
while len(pos)<atom_num:
if len(pos)==0:
pos.append([center_atom,center_atom,center_atom,0])
pos_real.append(supercell[superID[center_atom][center_atom][center_atom][0]])
else:
len_pos=len(pos)
mom_atom=random.randint(0,(len_pos-1))
temp=search_close_point(pos[mom_atom],pos,N_super,label)
if temp==[]:
continue
temp_real=supercell[superID[temp[0]][temp[1]][temp[2]][temp[3]]]
#print(len(pos_all))
#print(len_pos)
pos.append(temp)
pos_real.append(temp_real)
add_pos=True
for i in range(len(pos_all)):
if check_dup(pos_all[i],pos_real):
add_pos=False
if add_pos:
posID_all.append(pos)
# pos_real=make_pos_fit(pos_real)
pos_all.append(pos_real)
pos_all,cc_tmp=pos2standard_coord(pos_all)
#print('random_point_gen done')
return pos_all,posID_all
def analysis_vasp(target,atom_num):
pos=[]
energy=[]
mag=[]
pop_list=os.listdir(target)
for i1 in pop_list:
orig_os=os.getcwd()
pos_4=[]
energy_4=[]
mag_4=[]
target1=target+'/'+i1
if_there_is_normal=False
gaus_log=target1+'/'+'vasp.out'
xdat_log=target1+'/'+'XDATCAR'
del_list=[]
os.chdir(target1)
with open('vasp.out','r') as df:
dfread=df.read()
match_tmp=re.findall(r"RMM.*\n.*F=.*", dfread)
for rs in range(len(match_tmp)):
match_tmp1=match_tmp[rs].split()
if int(match_tmp1[1])>29:
del_list.append(rs)
energy_4.append(float(match_tmp1[11]))
mag_4.append(float(0))
with open('XDATCAR','r') as xf:
xfread=xf.read()
match_tmp2=xfread.split('\n')
x1_tmp=match_tmp2[2].split()
y1_tmp=match_tmp2[3].split()
z1_tmp=match_tmp2[4].split()
x1=float(x1_tmp[0])
y1=float(y1_tmp[1])
z1=float(z1_tmp[2])
match_dir=re.findall(r"Direct configuration", xfread)
for pop_i in range(len(match_dir)):
pos_tmp2=[]
for item1 in range(atom_num):
read_line=8+pop_i*(atom_num+1)+item1
pos_tmp3=match_tmp2[read_line].split()
for j3 in range(3):
pos_tmp3[j3]=float(pos_tmp3[j3])-0.5
pos_tmp2.append([ round(float(pos_tmp3[-3]),4)*x1,round(float(pos_tmp3[-2]),4)*y1,round(float(pos_tmp3[-1]),4)*z1 ])
pos_4.append(pos_tmp2)
while len(pos_4)<len(energy_4):
del energy_4[-1]
del mag_4[-1]
for del_k in range(len(del_list)-1,-1,-1):
try:
del pos_4[del_list[del_k]]
del energy_4[del_list[del_k]]
del mag_4[del_list[del_k]]
except:
pass
os.chdir(orig_os)
pos=pos+pos_4
energy=energy+energy_4
mag=mag+mag_4
#print(energy.index(min(energy)))
return pos,energy,mag
def analysis_gaus(target,atom_num):
pos=[]
energy=[]
mag=[]
pop_list=os.listdir(target)
for i1 in pop_list:
pos_4=[]
energy_4=[]
mag_4=[]
target1=target+'/'+i1
mag_list=os.listdir(target1)
if_there_is_normal=False
for j1 in mag_list:
target2=target1+'/'+j1
gaus_log=target2+'/'+'DMAC.log'
with open(gaus_log,'r') as gl:
glread=gl.read()
if_1=re.search('Normal termination of Gaussian',glread)
if if_1!=None:
if_there_is_normal=True
break
#print(i1)
# print(if_there_is_normal)
planB_ene=[]
for j1 in mag_list:
target2=target1+'/'+j1
gaus_log=target2+'/'+'DMAC.log'
with open(gaus_log,'r') as gl:
glread=gl.read()
if_1=re.search('Normal termination of Gaussian',glread)
if if_1!=None:
orig_os=os.getcwd()
os.chdir(target2)
pos_tmp2=[]
with open('DMAC.log','r') as df:
dfread=df.read()
match_tmp=re.findall(r"SCF Done.*", dfread)
match_tmp2=match_tmp[-1].split()
if int(match_tmp2[-2]) !=81:
e_tmp2=float(match_tmp2[4])
match_tmp3=re.findall(r"Input ori.*Distance", dfread,re.S)
match_tmp4=match_tmp3[-1].split('\n')
match_tmp5=match_tmp4[-atom_num-2:-2]
for item1 in range(atom_num):
pos_tmp3=match_tmp5[item1].split()
pos_tmp2.append([ round(float(pos_tmp3[-3]),2),round(float(pos_tmp3[-2]),2),round(float(pos_tmp3[-1]),2) ])
pos_4.append(pos_tmp2)
energy_4.append(e_tmp2)
mag_4.append(j1)
os.chdir(orig_os)
elif if_1==None and if_there_is_normal==False:
try:
orig_os=os.getcwd()
os.chdir(target2)
pos_tmp2=[]
with open('DMAC.log','r') as df:
dfread=df.read()
match_tmp=re.findall(r"SCF Done.*", dfread)
match_tmp2=match_tmp[-1].split()
if int(match_tmp2[-2]) !=81:
e_tmp2=float(match_tmp2[4])
match_tmp3=re.findall(r"Input ori.*Distance", dfread,re.S)
match_tmp4=match_tmp3[-1].split('\n')
match_tmp5=match_tmp4[-atom_num-2:-2]
for item1 in range(atom_num):
pos_tmp3=match_tmp5[item1].split()
pos_tmp2.append([ round(float(pos_tmp3[-3]),2),round(float(pos_tmp3[-2]),2),round(float(pos_tmp3[-1]),2) ])
pos_4.append(pos_tmp2)
energy_4.append(e_tmp2)
mag_4.append(j1)
os.chdir(orig_os)
except:
os.chdir(orig_os)
if len(energy_4)==0:
energy_4=planB_ene
try:
pos_5,energy_5,mag_5=pos_ener_sort(pos_4,energy_4,mag_4)
pos.append(pos_5[0])
energy.append(energy_5[0])
mag.append(mag_5[0])
except:
pass
#print(energy.index(min(energy)))
return pos,energy,mag
def analysisdeep_gaus(target,atom_num):
pos1f=[]
energy1f=[]
mag1f=[]
cycle_rec=[]
qdel_list=[]
pop_list=os.listdir(target)
for i1 in pop_list:
target1=target+'/'+'%s'%i1
mag_list=os.listdir(target1)
# if atom_num%2==0:
# mag_list=['1']
# else:
# mag_list=['2']
#
for j1 in mag_list:
target2=target1+'/'+j1
gaus_log=target2+'/'+'DMAC.log'
with open(gaus_log,'r') as gl:
glread=gl.read()
if_1=re.search('Normal termination of Gaussian',glread)
pos=[]
energy=[]
mag=[]
if if_1!=None:
orig_os=os.getcwd()
os.chdir(target2)
e_tmp=subprocess.check_output("grep 'SCF Do' DMAC.log|awk '{print $5,$8}'",shell=True,universal_newlines=True)
e_tmp=e_tmp.split('\n')
# print(len(e_tmp)-1)
for item3 in range(len(e_tmp)-1):
e_tmp2_1=e_tmp[item3].split()
if int(e_tmp2_1[1]) != 81:
energy.append(float(e_tmp2_1[0]))
cycle_rec.append(int(e_tmp2_1[1]))
mag.append(int(j1))
pos_tmp=subprocess.check_output(" sed '/Input orientation:/,/Distance matrix/!d' DMAC.log| grep -v -E 'I|C|N|D|R|\-\-\-'",shell=True,universal_newlines=True)
pos_tmp=pos_tmp.split('\n')
cycle_num=round((len(pos_tmp)-1)/(atom_num))
# print(cycle_num-1)
# if cycle_num-1!=len(e_tmp)-1:
# print('attention!!!: %s %s'%(target1,target2))
for item2 in range(0,cycle_num-1):
pos_tmp2=[]
for item1 in range(atom_num):
pos_tmp3=pos_tmp[item1+item2*(atom_num)].split()
pos_tmp2.append([ round(float(pos_tmp3[-3]),2),round(float(pos_tmp3[-2]),2),round(float(pos_tmp3[-1]),2) ])
pos.append(pos_tmp2)
os.chdir(orig_os)
else:
try:
orig_os=os.getcwd()
os.chdir(target2)
e_tmp=subprocess.check_output("grep 'SCF Do' DMAC.log|awk '{print $5,$8}'",shell=True,universal_newlines=True)
e_tmp=e_tmp.split('\n')
# print(len(e_tmp)-1)
for item3 in range(len(e_tmp)-1):
e_tmp2_1=e_tmp[item3].split()
if int(e_tmp2_1[1]) != 81:
energy.append(float(e_tmp2_1[0]))
cycle_rec.append(int(e_tmp2_1[1]))
mag.append(int(j1))
pos_tmp=subprocess.check_output(" sed '/Input orientation:/,/Distance matrix/!d' DMAC.log| grep -v -E 'I|C|N|D|R|\-\-\-'",shell=True,universal_newlines=True)
pos_tmp=pos_tmp.split('\n')
cycle_num=round((len(pos_tmp)-1)/(atom_num))
# print(cycle_num-1)
# if cycle_num-1!=len(e_tmp)-1:
# print('attention!!!: %s %s'%(target1,target2))
for item2 in range(0,cycle_num-1):
pos_tmp2=[]
for item1 in range(atom_num):
pos_tmp3=pos_tmp[item1+item2*(atom_num)].split()
pos_tmp2.append([ round(float(pos_tmp3[-3]),2),round(float(pos_tmp3[-2]),2),round(float(pos_tmp3[-1]),2) ])
pos.append(pos_tmp2)
os.chdir(orig_os)
except:
pass
if len(pos) == len(energy):
pos1f=pos1f+pos
energy1f=energy1f+energy
mag1f=mag1f+mag
# elif len(pos) == len(energy)-1:
# pos1f=pos1f+pos[0:-1]
# energy1f=energy1f+energy
# mag1f=mag1f+mag
# print(target2)
# print(len(pos))
# print(len(energy))
return pos1f,energy1f,mag1f
def pos_ener_sort(pos,energy,mag):
if len(pos)==len(energy):
item_num=len(energy)
for i1 in range(item_num):
for i2 in range(1,item_num):
if energy[i2-1]>energy[i2]:
energy_tmp=energy[i2-1]
energy[i2-1]=energy[i2]
energy[i2]=energy_tmp
pos_tmp=pos[i2-1]
pos[i2-1]=pos[i2]
pos[i2]=pos_tmp
mag_tmp=mag[i2-1]
mag[i2-1]=mag[i2]
mag[i2]=mag_tmp
else:
print('no equal')
return pos,energy,mag
def calc_bonding_energy(energy,atom_num,if_even):
re_energy=[]
for i in range(len(energy)):
ene_tmp=(energy[i]-atom_num*sole_energy)
if if_even==1:
ene_tmp=ene_tmp/atom_num
re_energy.append(ene_tmp)
else:
re_energy.append(ene_tmp)
return re_energy
def ini_pos(atom_num,pop_size,cal_method,if_run):
os.chdir((work_file+'/'+'%d'%atom_num))
posID_all=[]
pos_all=[]
add_how_much=pop_size
add_how1=round(add_how_much/2)
add_how2=add_how_much-add_how1
#print(add_how1)
#print(add_how2)
pos_all1,posID_all1=random_point_gen(atom_num,[],[],add_how1,'BCC')
pos_all2,posID_all2=random_point_gen(atom_num,[],[],add_how2,'FCC')
#print(pos_all1)
#print(pos_all2)
pos_all=pos_all1+pos_all2
posID_all=posID_all1+posID_all2
# for i in range(1,len(pos_all)+1):
# file_poscar='POSCAR_%d'%i
# point2poscar(file_poscar,pos_all[i-1])
file_calc=('0')
if os.path.exists( file_calc):
subprocess.call('rm -r 0',shell=True)
os.mkdir(file_calc)
if cal_method=='vasp':
for i in range(1,len(pos_all)+1):
file_calc_1=(file_calc+'/%d'%i)
os.mkdir(file_calc_1)
make_vasp(file_calc_1,pos_all[i-1],if_run)
if cal_method=='gaus':
for i in range(1,len(pos_all)+1):
if random.randint(0,1):
opt1=True
else:
opt1=False
file_calc_1=(file_calc+'/%d'%i)
os.mkdir(file_calc_1)
make_gaus(file_calc_1,pos_all[i-1],if_run,opt=opt1)
check_if_sleep()
os.chdir(work_file)
return pos_all
def plot_barcode(pos,name): #pos1=[[3,3,0],[3,0,0],[0,3,0],[0,0,0],[1.5,1.5,2.121315],[1.5,1.5,-2.121315]]
dis_m=dist_matric2(pos)
ripser_tmp=ripser(numpy.array(dis_m),maxdim=2,thresh=10,distance_matrix=True)
ripser_tmp2=ripser_tmp.get('dgms')
betti0_tmp=ripser_tmp2[0]
betti1_tmp=ripser_tmp2[1]
betti2_tmp=ripser_tmp2[2]
fig=plt.figure(dpi=300, figsize=(5, 3))
ax=[1,2,3,4]
ax[0] = fig.add_subplot(411)
ax[1] = fig.add_subplot(412)
ax[2] = fig.add_subplot(413)
ax[3] = fig.add_subplot(414)
# pdf = PdfPages((work_file+'/fig.pdf'))
line_n=1
for i2 in range(len(betti0_tmp)):
if betti0_tmp[i2,1]>10:
betti0_tmp[i2,1]=10
betti0_tmp[i2,1]=betti0_tmp[i2,1]-betti0_tmp[i2,0]
ax[0].broken_barh([(betti0_tmp[i2,0],betti0_tmp[i2,1])],(line_n,0.4),facecolors='tab:purple')
line_n=line_n+1
ax[0].set_xlim(0,6)
ax[0].set_xticks([])
ax[0].set_yticks([])
ax[0].set_ylim([0,line_n+0.2])
ax[0].invert_yaxis() # labels read top-to-bottom[
line_n=1
for i2 in range(len(betti1_tmp)):
if betti1_tmp[i2,1]>10:
betti1_tmp[i2,1]=10
betti1_tmp[i2,1]=betti1_tmp[i2,1]-betti1_tmp[i2,0]
ax[1].broken_barh([(betti1_tmp[i2,0],betti1_tmp[i2,1])],(line_n,0.4),facecolors='tab:blue')
line_n=line_n+1
ax[1].set_xlim(0,6)
ax[1].set_xticks([])
ax[1].set_yticks([])
if line_n<5:
line_n=5
ax[1].set_ylim([-2,line_n-2+0.2])
else:
ax[1].set_ylim([0,line_n+0.2])
ax[1].invert_yaxis() # labels read top-to-bottom[
line_n=1
for i2 in range(len(betti2_tmp)):
if betti2_tmp[i2,1]>10:
betti2_tmp[i2,1]=10
betti2_tmp[i2,1]=betti2_tmp[i2,1]-betti2_tmp[i2,0]
ax[2].broken_barh([(betti2_tmp[i2,0],betti2_tmp[i2,1])],(line_n,0.4),facecolors='tab:orange')
line_n=line_n+1
ax[2].set_xlim(0,6)
ax[2].set_xticks([])
if line_n<5:
line_n=5
ax[2].set_ylim([-2,line_n-2+0.2])
else:
ax[2].set_ylim([0,line_n+0.2])
ax[2].set_ylim([0,line_n+0.2])
ax[2].set_yticks([])
ax[2].invert_yaxis() # labels read top-to-bottom[
line_n=1
dis_bar=[]
for i5 in range(len(dis_m)-1):
for i6 in range(i5+1,len(dis_m)):
dis_bar.append([0,dis_m[i5][i6]])
dis_bar.sort()
for i3 in range(len(dis_bar)):
ax[3].broken_barh([(dis_bar[i3][0],dis_bar[i3][1])],(line_n,0.4),facecolors='tab:red')
line_n=line_n+1
ax[3].set_xlim(0,6)
ax[3].set_yticks([])
if line_n<5:
line_n=5
ax[3].set_ylim([-2,line_n-2+0.2])
else:
ax[3].set_ylim([0,line_n+0.2])
ax[3].set_ylim([0,line_n+0.2])
ax[3].invert_yaxis() # labels read top-to-bottom[
plt.savefig(name)
plt.show()
# pdf.close()
def pos2betti(pos):
betti=[]
for i1 in range(len(pos)):
dis_m=dist_matric2(pos[i1])
ripser_tmp=ripser(numpy.array(dis_m),maxdim=2,distance_matrix=True)
ripser_tmp2=ripser_tmp.get('dgms')
betti0_tmp=ripser_tmp2[0]
betti1_tmp=ripser_tmp2[1]
betti2_tmp=ripser_tmp2[2]
betti0=[]
betti1=[]
betti2=[]
betti3=[]
betti0_ini=[]
betti1_ini=[]
betti2_ini=[]
betti0_fin=[]
betti1_fin=[]
betti2_fin=[]
i_thresh_mat=list(map(lambda x:x/far_x,range(0,far_x*10+1,1)))
for i_thresh in range(len(i_thresh_mat)-1):
betti0.append(0)
betti1.append(0)
betti2.append(0)
betti3.append(0)
betti0_ini.append(0)
betti1_ini.append(0)
betti2_ini.append(0)
betti0_fin.append(0)
betti1_fin.append(0)
betti2_fin.append(0)
for i2 in range(len(betti0_tmp)):
if i_thresh_mat[i_thresh] >= betti0_tmp[i2][0] and betti0_tmp[i2][1]>=i_thresh_mat[i_thresh+1]:
betti0[i_thresh]=betti0[i_thresh]+1
elif i_thresh_mat[i_thresh] < betti0_tmp[i2][1] and betti0_tmp[i2][1]<i_thresh_mat[i_thresh+1]:
betti0[i_thresh]=betti0[i_thresh]+1
for i3 in range(len(betti1_tmp)):
if (i_thresh_mat[i_thresh] >= betti1_tmp[i3][0] and betti1_tmp[i3][1]>=i_thresh_mat[i_thresh+1]):
betti1[i_thresh]=betti1[i_thresh]+1
elif (i_thresh_mat[i_thresh] < betti1_tmp[i3][0] and betti1_tmp[i3][1]<i_thresh_mat[i_thresh+1]):
betti1[i_thresh]=betti1[i_thresh]+1
elif (i_thresh_mat[i_thresh] < betti1_tmp[i3][0] and betti1_tmp[i3][0]<i_thresh_mat[i_thresh+1]):
betti1[i_thresh]=betti1[i_thresh]+1
elif (i_thresh_mat[i_thresh] < betti1_tmp[i3][1] and betti1_tmp[i3][1]<i_thresh_mat[i_thresh+1]):
betti1[i_thresh]=betti1[i_thresh]+1
for i4 in range(len(betti2_tmp)):
if i_thresh_mat[i_thresh] >= betti2_tmp[i4][0] and betti2_tmp[i4][1]>=i_thresh_mat[i_thresh+1]:
betti2[i_thresh]=betti2[i_thresh]+1
elif i_thresh_mat[i_thresh] < betti2_tmp[i4][0] and betti2_tmp[i4][1]<i_thresh_mat[i_thresh+1]:
betti2[i_thresh]=betti2[i_thresh]+1
elif i_thresh_mat[i_thresh] < betti2_tmp[i4][0] and betti2_tmp[i4][0]<i_thresh_mat[i_thresh+1]:
betti2[i_thresh]=betti2[i_thresh]+1
elif i_thresh_mat[i_thresh] < betti2_tmp[i4][1] and betti2_tmp[i4][1]<i_thresh_mat[i_thresh+1]:
betti2[i_thresh]=betti2[i_thresh]+1
for i5 in range(len(dis_m)-1):
for i6 in range(i5+1,len(dis_m)):
if i_thresh_mat[i_thresh] < dis_m[i5][i6] <=i_thresh_mat[i_thresh+1] :
betti3[i_thresh]=betti3[i_thresh]+1
# for i5 in range(len(dis_m)-1):
# for i6 in range(i5+1,len(dis_m)):
# if dis_m[i5][i6] >i_thresh_mat[i_thresh+1] :
# betti3[i_thresh]=betti3[i_thresh]+1
# for i7 in range(len(betti0_tmp)):
# if i_thresh_mat[i_thresh] <= betti0_tmp[i7][0] and betti0_tmp[i7][0] < i_thresh_mat[i_thresh+1]:
# betti0_ini[i_thresh]=betti0_ini[i_thresh]+1
# for i8 in range(len(betti0_tmp)):
# if i_thresh_mat[i_thresh] <= betti0_tmp[i8][1] and betti0_tmp[i8][1] < i_thresh_mat[i_thresh+1]:
# betti0_fin[i_thresh]=betti0_fin[i_thresh]+1
# for i9 in range(len(betti1_tmp)):
# if i_thresh_mat[i_thresh] <= betti1_tmp[i9][0] and betti1_tmp[i9][0] < i_thresh_mat[i_thresh+1]:
# betti1_ini[i_thresh]=betti1_ini[i_thresh]+1
# for i10 in range(len(betti1_tmp)):
# if i_thresh_mat[i_thresh] <= betti1_tmp[i10][1] and betti1_tmp[i10][1] < i_thresh_mat[i_thresh+1]:
# betti1_fin[i_thresh]=betti1_fin[i_thresh]+1
# for i11 in range(len(betti2_tmp)):
# if i_thresh_mat[i_thresh] <= betti2_tmp[i11][0] and betti2_tmp[i11][0] < i_thresh_mat[i_thresh+1]:
# betti2_ini[i_thresh]=betti2_ini[i_thresh]+1
# for i12 in range(len(betti2_tmp)):
# if i_thresh_mat[i_thresh] <= betti2_tmp[i12][1] and betti2_tmp[i12][1] < i_thresh_mat[i_thresh+1]:
# betti2_fin[i_thresh]=betti2_fin[i_thresh]+1
# betti.append(betti0_ini+betti0+betti0_fin+betti1_ini+betti1+betti1_fin+betti2_ini+betti2+betti2_fin+betti3)
## betti.append(betti0_ini+betti0_fin+betti1_ini+betti1_fin+betti2_ini+betti2_fin+betti3)
betti.append(betti0+betti1+betti2+betti3)
return betti
def pos_farest_dis(pos):
pos_dis=[]
for i in range(len(pos)):
dis_mat=dist_matric(pos[i])