forked from mlcclab/PyRAI2MD-hiam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraj_analyzer.py
3421 lines (2937 loc) · 115 KB
/
traj_analyzer.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
## ----------------------
## Trajectory analysis script - a script to automatically extract trajectory data
## ----------------------
##
## Gen-FSSH.py 2019-2022 Jingbai Li
## New version Oct 3 2022 Jingbai Li
import sys
import os
import json
import multiprocessing
import numpy as np
from numpy import linalg as la
from scipy.optimize import linear_sum_assignment
class Element:
def __init__(self, name):
periodic_table = {
"HYDROGEN": "1", "H": "1", "1": "1",
"HELIUM": "2", "He": "2", "2": "2", "HE": "2",
"LITHIUM": "3", "Li": "3", "3": "3", "LI": "3",
"BERYLLIUM": "4", "Be": "4", "4": "4", "BE": "4",
"BORON": "5", "B": "5", "5": "5",
"CARBON": "6", "C": "6", "6": "6",
"NITROGEN": "7", "N": "7", "7": "7",
"OXYGEN": "8", "O": "8", "8": "8",
"FLUORINE": "9", "F": "9", "9": "9",
"NEON": "10", "Ne": "10", "10": "10", "NE": "10",
"SODIUM": "11", "Na": "11", "11": "11", "NA": "11",
"MAGNESIUM": "12", "Mg": "12", "12": "12", "MG": "12",
"ALUMINUM": "13", "Al": "13", "13": "13", "AL": "12",
"SILICON": "14", "Si": "14", "14": "14", "SI": "14",
"PHOSPHORUS": "15", "P": "15", "15": "15",
"SULFUR": "16", "S": "16", "16": "16",
"CHLORINE": "17", "Cl": "17", "17": "17", "CL": "17",
"ARGON": "18", "Ar": "18", "18": "18", "AR": "18",
"POTASSIUM": "19", "K": "19", "19": "19",
"CALCIUM": "20", "Ca": "20", "20": "20", "CA": "20",
"SCANDIUM": "21", "Sc": "21", "21": "21", "SC": "21",
"TITANIUM": "22", "Ti": "22", "22": "22", "TI": "22",
"VANADIUM": "23", "V": "23", "23": "23",
"CHROMIUM": "24", "Cr": "24", "24": "24", "CR": "24",
"MANGANESE": "25", "Mn": "25", "25": "25", "MN": "25",
"IRON": "26", "Fe": "26", "26": "26", "FE": "26",
"COBALT": "27", "Co": "27", "27": "27", "CO": "27",
"NICKEL": "28", "Ni": "28", "28": "28", "NI": "28",
"COPPER": "29", "Cu": "29", "29": "29", "CU": "29",
"ZINC": "30", "Zn": "30", "30": "30", "ZN": "30",
"GALLIUM": "31", "Ga": "31", "31": "31", "GA": "31",
"GERMANIUM": "32", "Ge": "32", "32": "32", "GE": "32",
"ARSENIC": "33", "As": "33", "33": "33", "AS": "33",
"SELENIUM": "34", "Se": "34", "34": "34", "SE": "34",
"BROMINE": "35", "Br": "35", "35": "35", "BR": "35",
"KRYPTON": "36", "Kr": "36", "36": "36", "KR": "36",
"RUBIDIUM": "37", "Rb": "37", "37": "37", "RB": "37",
"STRONTIUM": "38", "Sr": "38", "38": "38", "SR": "38",
"YTTRIUM": "39", "Y": "39", "39": "39",
"ZIRCONIUM": "40", "Zr": "40", "40": "40", "ZR": "40",
"NIOBIUM": "41", "Nb": "41", "41": "41", "NB": "41",
"MOLYBDENUM": "42", "Mo": "42", "42": "42", "MO": "42",
"TECHNETIUM": "43", "Tc": "43", "43": "43", "TC": "43",
"RUTHENIUM": "44", "Ru": "44", "44": "44", "RU": "44",
"RHODIUM": "45", "Rh": "45", "45": "45", "RH": "45",
"PALLADIUM": "46", "Pd": "46", "46": "46", "PD": "46",
"SILVER": "47", "Ag": "47", "47": "47",
"CADMIUM": "48", "Cd": "48", "48": "48", "CD": "48",
"INDIUM": "49", "In": "49", "49": "49", "IN": "49",
"TIN": "50", "Sn": "50", "50": "50", "SN": "50",
"ANTIMONY": "51", "Sb": "51", "51": "51", "SB": "51",
"TELLURIUM": "52", "Te": "52", "52": "52", "TE": "52",
"IODINE": "53", "I": "53", "53": "53",
"XENON": "54", "Xe": "54", "54": "54", "XE": "54",
"CESIUM": "55", "Cs": "55", "55": "55", "CS": "55",
"BARIUM": "56", "Ba": "56", "56": "56", "BA": "56",
"LANTHANUM": "57", "La": "57", "57": "57", "LA": "57",
"CERIUM": "58", "Ce": "58", "58": "58", "CE": "58",
"PRASEODYMIUM": "59", "Pr": "59", "59": "59", "PR": "59",
"NEODYMIUM": "60", "Nd": "60", "60": "60", "ND": "60",
"PROMETHIUM": "61", "Pm": "61", "61": "61", "PM": "61",
"SAMARIUM": "62", "Sm": "62", "62": "62", "SM": "62",
"EUROPIUM": "63", "Eu": "63", "63": "63", "EU": "63",
"GADOLINIUM": "64", "Gd": "64", "64": "64", "GD": "64",
"TERBIUM": "65", "Tb": "65", "65": "65", "TB": "65",
"DYSPROSIUM": "66", "Dy": "66", "66": "66", "DY": "66",
"HOLMIUM": "67", "Ho": "67", "67": "67", "HO": "67",
"ERBIUM": "68", "Er": "68", "68": "68", "ER": "68",
"THULIUM": "69", "TM": "69", "69": "69",
"YTTERBIUM": "70", "Yb": "70", "70": "70", "YB": "70",
"LUTETIUM": "71", "Lu": "71", "71": "71", "LU": "71",
"HAFNIUM": "72", "Hf": "72", "72": "72", "HF": "72",
"TANTALUM": "73", "Ta": "73", "73": "73", "TA": "73",
"TUNGSTEN": "74", "W": "74", "74": "74",
"RHENIUM": "75", "Re": "75", "75": "75", "RE": "75",
"OSMIUM": "76", "Os": "76", "76": "76", "OS": "76",
"IRIDIUM": "77", "Ir": "77", "77": "77", "IR": "77",
"PLATINUM": "78", "Pt": "78", "78": "78", "PT": "78",
"GOLD": "79", "Au": "79", "79": "79", "AU": "79",
"MERCURY": "80", "Hg": "80", "80": "80", "HG": "80",
"THALLIUM": "81", "Tl": "81", "81": "81", "TL": "81",
"LEAD": "82", "Pb": "82", "82": "82", "PB": "82",
"BISMUTH": "83", "Bi": "83", "83": "83", "BI": "83",
"POLONIUM": "84", "Po": "84", "84": "84", "PO": "84",
"ASTATINE": "85", "At": "85", "85": "85", "AT": "85",
"RADON": "86", "Rn": "86", "86": "86", "RN": "86"}
fullname = ["HYDROGEN", "HELIUM", "LITHIUM", "BERYLLIUM", "BORON", "CARBON", "NITROGEN", "OXYGEN", "FLUORINE",
"NEON",
"SODIUM", "MAGNESIUM", "ALUMINUM", "SILICON", "PHOSPHORUS", "SULFUR", "CHLORINE", "ARGON",
"POTASSIUM", "CALCIUM",
"SCANDIUM", "TITANIUM", "VANADIUM", "CHROMIUM", "MANGANESE", "IRON", "COBALT", "NICKEL", "COPPER",
"ZINC",
"GALLIUM", "GERMANIUM", "ARSENIC", "SELENIUM", "BROMINE", "KRYPTON", "RUBIDIUM", "STRONTIUM",
"YTTRIUM", "ZIRCONIUM",
"NIOBIUM", "MOLYBDENUM", "TECHNETIUM", "RUTHENIUM", "RHODIUM", "PALLADIUM", "SILVER", "CADMIUM",
"INDIUM", "TIN",
"ANTIMONY", "TELLURIUM", "IODINE", "XENON", "CESIUM", "BARIUM", "LANTHANUM", "CERIUM",
"PRASEODYMIUM", "NEODYMIUM",
"PROMETHIUM", "SAMARIUM", "EUROPIUM", "GADOLINIUM", "TERBIUM", "DYSPROSIUM", "HOLMIUM", "ERBIUM",
"THULIUM", "YTTERBIUM",
"LUTETIUM", "HAFNIUM", "TANTALUM", "TUNGSTEN", "RHENIUM", "OSMIUM", "IRIDIUM", "PLATINUM", "GOLD",
"MERCURY",
"THALLIUM", "LEAD", "BISMUTH", "POLONIUM", "ASTATINE", "RADON"]
symbol = ["H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne",
"Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca",
"Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn",
"Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr",
"Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn",
"Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd",
"Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "TM", "Yb",
"Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
"Tl", "Pb", "Bi", "Po", "At", "Rn"]
mass = [1.008, 4.003, 6.941, 9.012, 10.811, 12.011, 14.007, 15.999, 18.998, 20.180,
22.990, 24.305, 26.982, 28.086, 30.974, 32.065, 35.453, 39.948, 39.098, 40.078,
44.956, 47.867, 50.942, 51.996, 54.938, 55.845, 58.933, 58.693, 63.546, 65.390,
69.723, 72.640, 74.922, 78.960, 79.904, 83.800, 85.468, 87.620, 88.906, 91.224,
92.906, 95.940, 98.000, 101.070, 102.906, 106.420, 107.868, 112.411, 114.818, 118.710,
121.760, 127.600, 126.905, 131.293, 132.906, 137.327, 138.906, 140.116, 140.908, 144.240,
145.000, 150.360, 151.964, 157.250, 158.925, 162.500, 164.930, 167.259, 168.934, 173.040,
174.967, 178.490, 180.948, 183.840, 186.207, 190.230, 192.217, 195.078, 196.967, 200.590,
204.383, 207.200, 208.980, 209.000, 210.000, 222.000]
# Van der Waals Radius, missing data replaced by 2.00
radii = [1.20, 1.40, 1.82, 1.53, 1.92, 1.70, 1.55, 1.52, 1.47, 1.54,
2.27, 1.73, 1.84, 2.10, 1.80, 1.80, 1.75, 1.88, 2.75, 2.31,
2.11, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 1.63, 1.40, 1.39,
1.87, 2.11, 1.85, 1.90, 1.85, 2.02, 3.03, 2.49, 2.00, 2.00,
2.00, 2.00, 2.00, 2.00, 2.00, 1.63, 1.72, 1.58, 1.93, 2.17,
2.00, 2.06, 1.98, 2.16, 3.43, 2.68, 2.00, 2.00, 2.00, 2.00,
2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00,
2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 1.75, 1.66, 1.55,
1.96, 2.02, 2.07, 1.97, 2.02, 2.20]
self.__name = int(periodic_table[name])
self.__FullName = fullname[self.__name - 1]
self.__Symbol = symbol[self.__name - 1]
self.__Mass = mass[self.__name - 1]
self.__Radii = radii[self.__name - 1]
def getFullName(self):
return self.__FullName
def getSymbol(self):
return self.__Symbol
def getUpperSymbol(self):
return self.__Symbol.upper()
def getMass(self):
return self.__Mass
def getNuc(self):
return self.__name
def getNelectron(self):
return self.__name
def getRadii(self):
return self.__Radii
def BND(xyz, var):
## This function calculate distance
## a<->b
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b = var[0:2]
v1 = xyz[a - 1]
v2 = xyz[b - 1]
r = la.norm(v1 - v2)
return r
def AGL(xyz, var):
## This function calculate angle
## a<-b->c
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c = var[0:3]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
v1 = r1 - r2
v2 = r3 - r2
v1 = v1 / la.norm(v1)
v2 = v2 / la.norm(v2)
cosa = np.dot(v1, v2)
alpha = np.arccos(cosa) * 57.2958
return alpha
def DHD(xyz, var):
## This function calculate dihedral angle
## n1 n2
## | |
## a<-b-><-c->d
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d = var[0:4]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
v1 = r1 - r2
v2 = r3 - r2
v3 = r2 - r3
v4 = r4 - r3
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
beta = np.arccos(cosb) * 57.2958
return beta
def DHD2(xyz, var):
## This function calculate dihedral angle
## n1 n2
## | |
## a<-b-><-c->d
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d = var[0:4]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
v1 = r1 - r2
v2 = r3 - r2
v3 = r2 - r3
v4 = r4 - r3
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
beta = np.arccos(cosb) * 57.2958
axis = np.cross(n1, n2)
pick = np.argmax(np.abs(axis))
# find the projection with the largest magnitude (non-zero), then just compare it to avoid 0/0
sign = np.sign(axis[pick] / v2[pick])
if sign == -1:
beta = - beta
return beta
def DHD3(xyz, var):
## This function calculate dihedral angle
## n1 n2
## | |
## a<-b-><-c->d
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d = var[0:4]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
v1 = r1 - r2
v2 = r3 - r2
v3 = r2 - r3
v4 = r4 - r3
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
beta = np.arccos(cosb) * 57.2958
axis = np.cross(n1, n2)
pick = np.argmax(np.abs(axis))
# find the projection with the largest magnitude (non-zero), then just compare it to avoid 0/0
sign = np.sign(axis[pick] / v2[pick])
if sign == -1:
beta = 360 - beta
return beta
def DHDD(xyz, var):
## This function calculate dihedral angle involving dummy center
## n1 n2
## | |
## a,b<-c-><-d->e,f
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d, e, f = var[0:6]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
r5 = np.array(xyz[e - 1])
r6 = np.array(xyz[f - 1])
v1 = (r1 + r2) / 2 - r3
v2 = r4 - r3
v3 = r3 - r4
v4 = (r5 + r6) / 2 - r4
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
beta = np.arccos(cosb) * 57.2958
axis = np.cross(n1, n2)
pick = np.argmax(np.abs(axis))
# find the projection with the largest magnitude (non-zero), then just compare it to avoid 0/0
sign = np.sign(axis[pick] / v2[pick])
if sign == -1:
beta = 360 - beta
return beta
def DHDD2(xyz, var):
## This function calculate dihedral angle involving dummy center
## n1 n2
## | |
## a,b<-c-><-d->e,f
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d, e, f = var[0:6]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
r5 = np.array(xyz[e - 1])
r6 = np.array(xyz[f - 1])
v1 = (r1 + r2) / 2 - r3
v2 = r4 - r3
v3 = r3 - r4
v4 = (r5 + r6) / 2 - r4
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
beta = np.arccos(cosb) * 57.2958
axis = np.cross(n1, n2)
pick = np.argmax(np.abs(axis))
# find the projection with the largest magnitude (non-zero), then just compare it to avoid 0/0
sign = np.sign(axis[pick] / v2[pick])
if sign == -1:
beta = - beta
return beta
def DHDD3(xyz, var):
## This function calculate dihedral angle involving dummy center
## n1 n2
## | |
## a,b<-c-><-d->e,f
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d, e, f = var[0:6]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
r5 = np.array(xyz[e - 1])
r6 = np.array(xyz[f - 1])
v1 = (r1 + r2) / 2 - r3
v2 = r4 - r3
v3 = r3 - r4
v4 = (r5 + r6) / 2 - r4
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
beta = np.arccos(cosb) * 57.2958
axis = np.cross(n1, n2)
pick = np.argmax(np.abs(axis))
# find the projection with the largest magnitude (non-zero), then just compare it to avoid 0/0
sign = np.sign(axis[pick] / v2[pick])
if sign == -1:
beta = 360 - beta
return beta
def OOP(xyz, var):
## This function calculate out-of-plane angle
## n d
## | |
## a<-b->c
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d = var[0:4]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
v1 = r1 - r2
v2 = r3 - r2
v3 = r4 - r3
v3 = v3 / la.norm(v3)
n = np.cross(v1, v2)
n = n / la.norm(n)
cosb = np.dot(n, v3)
gamma = np.arccos(cosb) * 57.2958
return gamma
def PPA(xyz, var):
## This function calculate plane-plane angle
## n1 n2
## | |
## a<-b->c d<-e->f
var = [int(x) for x in var]
xyz = np.array([[float(y) for y in x.split()[1: 4]] for x in xyz])
a, b, c, d, e, f = var[0:6]
r1 = np.array(xyz[a - 1])
r2 = np.array(xyz[b - 1])
r3 = np.array(xyz[c - 1])
r4 = np.array(xyz[d - 1])
r5 = np.array(xyz[e - 1])
r6 = np.array(xyz[f - 1])
v1 = r1 - r2
v2 = r3 - r2
v3 = r4 - r5
v4 = r6 - r5
n1 = np.cross(v1, v2)
n2 = np.cross(v3, v4)
n1 = n1 / la.norm(n1)
n2 = n2 / la.norm(n2)
cosb = np.dot(n1, n2)
delta = np.arccos(cosb) * 57.2958
return delta
def G(coord, par_sym):
## This function calculate symmetry function in Behler, J, Int. J. Quantum Chem., 2-15, 115 1032-1050
## This function return a list of values for each atom
## coord is a numpy array of floating numbers
## par_sym has default values in RMSD
cut = par_sym['cut'] # cutoff function version 1 or 2
ver = par_sym['ver'] # symmetry function 1-4
rc = par_sym['rc'] # cutoff radii, 0 is the maximum * 1.1
n = par_sym['n'] # Gaussian exponent
rs = par_sym['rs'] # Gaussian center
z = par_sym['z'] # angular exponent
ll = par_sym['l'] # cosine exponent, only 1 or -1
# print('\nSymmetry function: %d' % (ver))
# print('Cutoff function: %d' % (cut))
# print('Cutoff radii:%6.2f Shift:%6.2f' % (rc,rs))
# print('Eta:%6.2f Zeta:%6.2f Lambda:%6.2f' % (n,z,l))
## prepare distance matrix
dist = np.array([[0.0 for _ in coord] for _ in coord])
for n, i in enumerate(coord):
for m, j in enumerate(coord):
if n != m: # update dist if n != m
r = la.norm(i - j)
dist[n][m] = r
## prepare cutoff function matrix
if rc == 0:
rc = 1.1 * np.amax(dist)
fc = np.array([[1.0 for _ in coord] for _ in coord])
for n, i in enumerate(dist):
for m, j in enumerate(dist):
r = dist[n][m]
r /= rc
if r < 1:
fc[n][m] = r # update fc if r < rc
## prepare angle matrix if needed, i is the center atom!
angl = np.zeros(0)
if ver > 2:
angl = np.array([[[0.0 for _ in coord] for _ in coord] for _ in coord])
for n, i in enumerate(coord):
for m, j in enumerate(coord):
for o, k in enumerate(coord):
if n != m and m != o and o != n:
v1 = j - i
v2 = j - k
v1 = v1 / la.norm(v1)
v2 = v2 / la.norm(v2)
cosa = np.dot(v1, v2)
alpha = np.arccos(cosa) * 57.2958
angl[n][m][o] = alpha
if cut == 1:
fc = 0.5 * np.cos(np.pi * fc) + 1
elif cut == 2:
fc = np.tanh(1 - fc) ** 3
else:
print('\n!!! Cannot recognize cutoff function !!!\n')
exit()
if ver == 1:
g = np.sum(fc, axis=1)
elif ver == 2:
w = np.exp((-1) * n * (dist - rs) ** 2)
g = np.sum(w * fc, axis=1)
elif ver == 3:
g = np.array([0.0 for _ in coord])
for i in range(len(coord)):
for j in range(len(coord)):
for k in range(len(coord)):
a = (1 + ll * np.cos(angl[i][j][k])) ** z
w = np.exp((-1) * n * (dist[i][j] ** 2 + dist[i][k] ** 2 + dist[j][k] ** 2))
f = fc[i][j] * fc[i][k] * fc[j][k]
g[i] += 2 ** (1 - z) * a * w * f
elif ver == 4:
g = np.array([0.0 for _ in coord])
for i in range(len(coord)):
for j in range(len(coord)):
for k in range(len(coord)):
a = (1 + ll * np.cos(angl[i][j][k])) ** z
w = np.exp((-1) * n * (dist[i][j] ** 2 + dist[i][k] ** 2))
f = fc[i][j] * fc[i][k]
g[i] += 2 ** (1 - z) * a * w * f
else:
g = np.zeros(0)
print('\n!!! Cannot recognize symmetry function !!!\n')
exit()
return g
def RMSD(xyz, ref, var):
## This function calculate RMSD between product and reference
## This function call kabsch to reduce RMSD between product and reference
## This function call hungarian to align product and reference
## general variables for all functions
excl = [] # exclude elements
incl = [] # only include elements
pck = [] # pick this atoms
align = 'NO' # do not align product and reference
coord = 'CART' # use cartesian coordinates
rmsd = 'NONE' # rmsd have not been calculated
## symmetry function default variables
par_sym = {
'cut': 1, # cutoff function version 1 or 2
'ver': 1, # symmetry function 1-4
'rc': 6, # cutoff radii, 0 is the maximum * 1.1
'n': 1.2, # Gaussian exponent
'rs': 0, # Gaussian center
'z': 1, # angular exponent
'l': 1 # cosine factor, only 1 or -1
}
for i in var:
i = i.upper()
if 'NO=' in i:
e = i.split('=')[1]
e = Element(e).getSymbol()
excl.append(e)
elif 'ON=' in i:
e = i.split('=')[1]
e = Element(e).getSymbol()
incl.append(e)
elif 'PICK=' in i:
pck = [int(x) for x in i.split('=')[1].split(',')]
elif 'ALIGN=' in i:
i = i.split('=')[1]
if i == 'HUNG' or i == 'NO': # align must be either hung or no
align = i
elif 'COORD=' in i:
i = i.split('=')[1]
if i == 'CART' or i == 'SYM': # coord must be either cart or sym
coord = i
elif 'CUT=' in i:
i = int(i.split('=')[1])
if i in [1, 2]: # cut must be within 1-2
par_sym['cut'] = i
elif 'VER=' in i:
i = int(i.split('=')[1])
if i in [1, 2, 3, 4]: # ver must be within 1-4
par_sym['ver'] = i
elif 'RC=' in i:
par_sym['rc'] = float(i.split('=')[1])
elif 'ETA=' in i:
par_sym['n'] = float(i.split('=')[1])
elif 'RS=' in i:
par_sym['rs'] = float(i.split('=')[1])
elif 'ZETA=' in i:
par_sym['z'] = float(i.split('=')[1])
elif 'LAMBDA=' in i:
ll = int(i.split('=')[1])
if ll >= 0: # l only takes +1 or -1
par_sym['l'] = 1
else:
par_sym['l'] = -1
## prepare atom list and coordinates
el = [] # element list
for i in xyz:
e, x, y, z = i.split()
e = Element(e).getSymbol()
if e not in el:
el.append(e)
if len(excl) > 0:
el = [x for x in el if x not in excl]
if len(incl) > 0:
el = [x for x in el if x in incl]
s = [x + 1 for x in range(len(xyz))] # atom index list
if len(pck) > 0:
s = pck
p = [] # product coordinates
patoms = []
for n, i in enumerate(xyz):
e, x, y, z = i.split()
x, y, z = float(x), float(y), float(z)
e = Element(e).getSymbol()
if e in el and n + 1 in s:
p.append([x, y, z])
patoms.append(e)
p = np.array(p)
q = [] # reference coordinates
qatoms = []
for n, i in enumerate(ref):
e, x, y, z = i.split()
x, y, z = float(x), float(y), float(z)
e = Element(e).getSymbol()
if e in el and n + 1 in s:
q.append([x, y, z])
qatoms.append(e)
q = np.array(q)
p -= p.mean(axis=0) # translate to the centroid
q -= q.mean(axis=0) # translate to the centroid
if align == 'HUNG': # align coordinates
swap = np.array([
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 1, 0],
[2, 0, 1]])
reflection = np.array([
[1, 1, 1],
[-1, 1, 1],
[1, -1, 1],
[1, 1, -1],
[-1, -1, 1],
[-1, 1, -1],
[1, -1, -1],
[-1, -1, -1]])
order = []
rmsd = []
for sw in swap:
for r in reflection:
tatoms = [x for x in qatoms]
t = np.array([x for x in q])
t = t[:, sw]
t = np.dot(t, np.diag(r))
t -= t.mean(axis=0)
ip = inertia(patoms, p)
it = inertia(tatoms, t)
u1 = rotate(ip, it)
u2 = rotate(ip, -it)
t1 = np.dot(t, u1)
t2 = np.dot(t, u2)
order1 = hungarian(patoms, tatoms, p, t1)
order2 = hungarian(patoms, tatoms, p, t2)
rmsd1 = kabsch(p, t[order1])
rmsd2 = kabsch(p, t[order2])
order += [order1, order2]
rmsd += [rmsd1, rmsd2]
pick = np.argmin(rmsd)
order = order[pick]
rmsd = rmsd[pick]
q = q[order]
if coord == 'SYM': # use symmetry function
g_prd = G(p, par_sym)
g_ref = G(q, par_sym)
rmsd = np.sqrt(np.sum((g_prd - g_ref) ** 2) / len(g_prd))
if rmsd == 'NONE':
rmsd = kabsch(p, q)
return rmsd
def kabsch(p, q):
## This function use Kabsch algorithm to reduce RMSD by rotation
c = np.dot(np.transpose(p), q)
v, s, w = np.linalg.svd(c)
d = (np.linalg.det(v) * np.linalg.det(w)) < 0.0
if d: # ensure right-hand system
s[-1] = -s[-1]
v[:, -1] = -v[:, -1]
u = np.dot(v, w)
p = np.dot(p, u)
diff = p - q
n = len(p)
return np.sqrt((diff * diff).sum() / n)
def inertia(atoms, xyz):
## This function calculate principal axis
xyz = np.array([i for i in xyz]) # copy the array to avoid changing it
mass = []
for i in atoms:
m = Element(i).getMass()
mass.append(m)
mass = np.array(mass)
xyz -= np.average(xyz, weights=mass, axis=0)
xx = 0.0
yy = 0.0
zz = 0.0
xy = 0.0
xz = 0.0
yz = 0.0
for n, i in enumerate(xyz):
xx += mass[n] * (i[1] ** 2 + i[2] ** 2)
yy += mass[n] * (i[0] ** 2 + i[2] ** 2)
zz += mass[n] * (i[0] ** 2 + i[1] ** 2)
xy += -mass[n] * i[0] * i[1]
xz += -mass[n] * i[0] * i[2]
yz += -mass[n] * i[1] * i[2]
im = np.array([[xx, xy, xz], [xy, yy, yz], [xz, yz, zz]])
eigval, eigvec = np.linalg.eig(im)
return eigvec[np.argmax(eigval)]
def rotate(p, q):
## This function calculate the matrix rotate q onto p
p: np.ndarray
q: np.ndarray
if (p == q).all():
return np.eye(3)
elif (p == -q).all():
# return a rotation of pi around the y-axis
return np.array([[-1., 0., 0.], [0., 1., 0.], [0., 0., -1.]])
else:
v = np.cross(p, q)
s = np.linalg.norm(v)
c = np.vdot(p, q)
vx = np.array([[0., -v[2], v[1]], [v[2], 0., -v[0]], [-v[1], v[0], 0.]])
return np.eye(3) + vx + np.dot(vx, vx) * ((1. - c) / (s * s))
def hungarian(patoms, qatoms, p, q):
## This function use hungarian algorithm to align P onto Q
## This function call linear_sum_assignment from scipy to solve hungarian problem
## This function call inertia to find principal axis
## This function call rotate to rotate P onto aligned Q
unique_atoms = np.unique(patoms)
reorder = np.zeros(len(qatoms), dtype=int)
for atom in unique_atoms:
pidx = []
qidx = []
for n, p in enumerate(patoms):
if p == atom:
pidx.append(n)
for m, q in enumerate(qatoms):
if q == atom:
qidx.append(m)
pidx = np.array(pidx)
qidx = np.array(qidx)
a = p[pidx]
b = q[qidx]
ab = np.array([[la.norm(aa - bb) for bb in b] for aa in a])
aidx, bidx = linear_sum_assignment(ab)
reorder[pidx] = qidx[bidx]
return reorder
def getindex(index):
## This function read single, range, separate range index and convert them to a list
index_list = []
for i in index:
if '-' in i:
a, b = i.split('-')
a, b = int(a), int(b)
index_list += range(a, b + 1)
else:
index_list.append(int(i))
index_list = sorted(list(set(index_list))) # remove duplicates and sort from low to high
return index_list
def redindex(index):
## This function compress a list of index into range
index = sorted(list(set(index)))
groups = []
subrange = []
for i in index:
subrange.append(int(i))
if len(subrange) > 1:
d = subrange[-1] - subrange[-2] # check continuity
if d > 1:
groups.append(subrange[0:-1])
subrange = [subrange[-1]]
if i == index[-1]:
groups.append(subrange)
index_range = ''
for j in groups:
if len(j) == 1:
index_range += '%s ' % (j[0])
elif len(j) == 2:
index_range += '%s %s ' % (j[0], j[1])
else:
index_range += '%s-%s ' % (j[0], j[-1])
return index_range
def set_prune(prune_type, prune_index, prune_thrhd):
prune_index = ' '.join(prune_index).split(',')
prune_index = [x.split() for x in prune_index]
pindex = []
pthrhd = []
if 'frag' in prune_type:
p1 = np.array(getindex(prune_index[0])) - 1
p2 = np.array(getindex(prune_index[1])) - 1
p3 = prune_thrhd[0]
pindex.append([p1, p2])
pthrhd.append(p3)
else:
diff = len(prune_index) - len(prune_thrhd)
if diff > 0:
add = [prune_thrhd[-1] for _ in range(diff)]
prune_thrhd = prune_thrhd + add
else:
prune_thrhd = prune_thrhd[:len(prune_index)]
for n, p in enumerate(prune_index):
p1, p2 = getindex(p)[0: 2]
p3 = prune_thrhd[n]
pindex.append([[p1 - 1], [p2 - 1]])
pthrhd.append(p3)
return pindex, pthrhd
def check_param(coord, src, dst, thrhd):
coord = np.array([x.split()[1: 4] for x in coord]).astype(float)
a = np.mean(coord[src], axis=0)
b = np.mean(coord[dst], axis=0)
d = np.sum((a - b) ** 2) ** 0.5
if d > thrhd:
return True, d
return False, d
def format1(n, xyz):
## This function convert coordinates list to string
output = '%d\n' % n
for i in xyz:
output += '%s\n' % i
return output
def format2(x):
## This function convert a one-line string to multiple lines:
str_new = ''
for n, i in enumerate(x.split()):
str_new += '%10s ' % i
if (n + 1) % 10 == 0:
str_new += '\n'
else:
if i == x.split()[-1]:
str_new += '\n'
return str_new
def format3(n, xyz):
## This function convert coordinates from a list of string to float
output = []
for line in xyz[1: 1 + n]:
atom, x, y, z = line.split()[0: 4]
output.append([str(atom), float(x), float(y), float(z)])
return output
def format4(xyz):
## This function convert coordinates from a list of float to string
output = []
for line in xyz:
atom, x, y, z = line
output.append('%-5s%16.8f%16.8f%16.8f' % (atom, x, y, z))
return output
def Refread(ref):
## This function read the reference structure from a file: ref
ref_coord = []
with open(ref, 'r') as refxyz:
coord = refxyz.read().splitlines()
natom = int(coord[0])
n = 0
m = 0
for _ in coord:
n += 1
if n % (natom + 2) == 0: # at the last line of each coordinates
m += 1
ref_coord.append(coord[n - natom:n])
print('\nRead reference structures: %5d in %s\n' % (m, ref))
return ref_coord
def Paramread(param):
## This function read the geometrical parameters from string or a file
p_list = ['B', 'A', 'D', 'D2', 'D3', 'DD', 'DD2', 'DD3', 'O', 'P', 'RMSD']
parameters = []
par_group = []
for n, p in enumerate(param):
if p in p_list:
if len(par_group) > 0:
parameters.append(par_group)
par_group = [p]
else:
par_group.append(p)
if n == len(param) - 1:
parameters.append(par_group)
return parameters
def Pararead(parameters):
## This function read geometrical parameters and return the name and comment of parameters
name = []
cmmt = []
for i in parameters:
i = [x.upper() for x in i]
name.append(i[0])
if i[0] != 'RMSD':
cmmt.append(','.join(i[1:]))
else:
v1 = 'D'
v2 = ' CART'
if 'ALIGN=HUNG' in i:
v1 = 'A'
if 'COORD=SYM' in i:
v2 = ' SYM'
cmmt.append(v1 + v2)
return name, cmmt
def compute_para(var):
## This function wrap the evaluate for parallelization
i_traj, i_geom, i_param, geom, param, thrhd, ref_coord = var
geom_param = evaluate(geom, ref_coord, param)
geom_type = int(geom_param > thrhd)