forked from peteboyd/lammps_interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForceFields.py
4449 lines (3848 loc) · 182 KB
/
ForceFields.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
from uff import UFF_DATA
from uff4mof import UFF4MOF_DATA
from dreiding import DREIDING_DATA
from uff_qeq import UFF_QEQ
from uff_nonbonded import UFF_DATA_nonbonded
from BTW import BTW_angles, BTW_dihedrals, BTW_opbends, BTW_atoms, BTW_bonds, BTW_charges
from Dubbeldam import Dub_atoms, Dub_bonds, Dub_angles, Dub_dihedrals, Dub_impropers
from FMOFCu import FMOFCu_angles, FMOFCu_dihedrals, FMOFCu_opbends, FMOFCu_atoms, FMOFCu_bonds
from MOFFF import MOFFF_angles, MOFFF_dihedrals, MOFFF_opbends, MOFFF_atoms, MOFFF_bonds
from water_models import SPC_E_atoms, TIP3P_atoms, TIP4P_atoms, TIP5P_atoms
from gas_models import EPM2_atoms, EPM2_angles
from lammps_potentials import BondPotential, AnglePotential, DihedralPotential, ImproperPotential, PairPotential
from atomic import METALS
import math
import numpy as np
from operator import mul
import itertools
import abc
import re
import sys
from Molecules import *
DEG2RAD = math.pi/180.
kBtokcal = 0.00198588
class ForceField(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def bond_term(self):
"""Computes the bond parameters"""
@abc.abstractmethod
def angle_term(self):
"""Computes the angle parameters"""
@abc.abstractmethod
def dihedral_term(self):
"""Computes the dihedral parameters"""
@abc.abstractmethod
def improper_term(self):
"""Computes the improper dihedral parameters"""
def compute_force_field_terms(self):
self.compute_atomic_pair_terms()
self.compute_bond_terms()
self.compute_angle_terms()
self.compute_dihedral_terms()
self.compute_improper_terms()
def compute_atomic_pair_terms(self):
for n, data in self.graph.nodes_iter(data=True):
self.pair_terms(n, data, self.cutoff)
def compute_bond_terms(self):
del_edges = []
for n1, n2, data in self.graph.edges_iter2(data=True):
if self.bond_term((n1, n2, data)) is None:
del_edges.append((n1, n2))
for (n1, n2) in del_edges:
self.graph.remove_edge(n1, n2)
def compute_angle_terms(self):
for b, data in self.graph.nodes_iter(data=True):
# compute and store angle terms
try:
rem_ang = []
ang_data = data['angles']
for (a, c), val in ang_data.items():
if self.angle_term((a, b, c, val)) is None:
rem_ang.append((a,c))
for i in rem_ang:
del(data['angles'][i])
except KeyError:
pass
def compute_dihedral_terms(self):
for b, c, data in self.graph.edges_iter2(data=True):
try:
rem_dihed = []
dihed_data = data['dihedrals']
for (a, d), val in dihed_data.items():
if self.dihedral_term((a,b,c,d, val)) is None:
rem_dihed.append((a,d))
for i in rem_dihed:
del(data['dihedrals'][i])
except KeyError:
pass
def compute_improper_terms(self):
for b, data in self.graph.nodes_iter(data=True):
try:
rem_imp = []
imp_data = data['impropers']
for (a, c, d), val in imp_data.items():
if self.improper_term((a,b,c,d, val)) is None:
rem_imp.append((a,c,d))
for i in rem_imp:
del(data['impropers'][i])
except KeyError:
pass
class UserFF(ForceField):
def __init__(self, graph):
self.graph = graph
self.unique_atom_types = {}
self.unique_bond_types = {}
self.unique_angle_types = {}
self.unique_dihedral_types = {}
self.unique_improper_types = {}
self.unique_pair_types = {}
def bond_term(self, bond):
return 1
def angle_term(self, angle):
return 1
def dihedral_term(self, dihedral):
return 1
def improper_term(self, improper):
return 1
def unique_atoms(self):
# ff_type keeps track of the unique integer index
print("Here are the unique atoms")
ff_type = {}
count = 0
for atom in self.structure.atoms:
if atom.force_field_type is None:
label = atom.element
else:
label = atom.force_field_type
try:
type = ff_type[label]
except KeyError:
count += 1
type = count
ff_type[label] = type
self.unique_atom_types[type] = atom
atom.ff_type_index = type
print(atom.ff_type_index)
for key, atom in list(self.unique_atom_types.items()):
print(str(key) + " : " + str(atom.index))
def unique_bonds(self):
print("Here are the unique bonds (Total = " +
str(len(self.structure.bonds)) + ")")
count = 0
bb_type = {}
for bond in self.structure.bonds:
idx1, idx2 = bond.indices
atm1, atm2 = self.structure.atoms[idx1], self.structure.atoms[idx2]
self.bond_term(bond)
try:
type = bb_type[(atm1.ff_type_index,
atm2.ff_type_index,
bond.order)]
except KeyError:
try:
type = bb_type[(atm2.ff_type_index,
atm1.ff_type_index,
bond.order)]
except KeyError:
count += 1
type = count
bb_type[(atm1.ff_type_index,
atm2.ff_type_index,
bond.order)] = type
self.unique_bond_types[type] = bond
bond.ff_type_index = type
print(bond.ff_type_index)
for key, bond in list(self.unique_bond_types.items()):
print(str(key) + " : " + str(bond.atoms[0].index)
+ " - " + str(bond.atoms[1].index))
def unique_angles(self):
print("Here are the unique angles (Total = " +
str(len(self.structure.angles)) + ")")
ang_type = {}
count = 0
for angle in self.structure.angles:
atom_a, atom_b, atom_c = angle.atoms
type_a, type_b, type_c = (atom_a.ff_type_index,
atom_b.ff_type_index,
atom_c.ff_type_index)
# compute and store angle terms
self.angle_term(angle)
try:
type = ang_type[(type_a, type_b, type_c)]
except KeyError:
try:
type = ang_type[(type_c, type_b, type_a)]
except KeyError:
count += 1
type = count
ang_type[(type_a, type_b, type_c)] = type
self.unique_angle_types[type] = angle
angle.ff_type_index = type
print(angle.ff_type_index)
for key, angle in list(self.unique_angle_types.items()):
print(str(key) + " : " + str(angle.atoms[0].index) + "-" +
str(angle.atoms[1].index) + "-" +
str(angle.atoms[2].index))
print(str(key) + " : " + str(angle.atoms[0].force_field_type)
+ "-" + str(angle.atoms[1].force_field_type) +
"-" + str(angle.atoms[2].force_field_type))
def unique_dihedrals(self):
print("Here are the unique dihedrals (Total = "
+ str(len(self.structure.dihedrals)) + ")")
count = 0
dihedral_type = {}
for dihedral in self.structure.dihedrals:
atom_a, atom_b, atom_c, atom_d = dihedral.atoms
type_a, type_b, type_c, type_d = (atom_a.ff_type_index,
atom_b.ff_type_index,
atom_c.ff_type_index,
atom_d.ff_type_index)
M = len(atom_c.neighbours)*len(atom_b.neighbours)
try:
type = dihedral_type[(type_a, type_b, type_c, type_d, M)]
except KeyError:
try:
type = dihedral_type[(type_d, type_c, type_b, type_a, M)]
except KeyError:
count += 1
type = count
dihedral_type[(type_a, type_b, type_c, type_d, M)] = type
#self.dihedral_term(dihedral)
self.unique_dihedral_types[type] = dihedral
dihedral.ff_type_index = type
print(dihedral.ff_type_index)
for key, dihedral in list(self.unique_dihedral_types.items()):
print(str(key) + " : " + str(dihedral.atoms[0].index) + "-" +
str(dihedral.atoms[1].index) + "-" + str(dihedral.atoms[2].index)
+ "-" + str(dihedral.atoms[3].index))
print(str(key) + " : " + str(dihedral.atoms[0].force_field_type)
+ "-" + str(dihedral.atoms[1].force_field_type) + "-"
+ str(dihedral.atoms[2].force_field_type) + "-" +
str(dihedral.atoms[3].force_field_type))
def unique_impropers(self):
"""How many times to list the same set of atoms ???"""
print("Here are the unique impropers (Total = " +
str(len(self.structure.impropers)) + ")")
count = 0
improper_type = {}
#i = 0
#for improper in self.structure.impropers:
# i += 1
# print(str(i) + " : " + str(improper.atoms[0].force_field_type) + "-" + str(improper.atoms[1].force_field_type) + "-" + str(improper.atoms[2].force_field_type) + "-" + str(improper.atoms[3].force_field_type)
for improper in self.structure.impropers:
print("Now keys are + " + str(improper_type.keys()))
atom_a, atom_b, atom_c, atom_d = improper.atoms
type_a, type_b, type_c, type_d = (atom_a.ff_type_index, atom_b.ff_type_index,
atom_c.ff_type_index, atom_d.ff_type_index)
d1 = (type_b, type_a, type_c, type_d)
d2 = (type_b, type_a, type_d, type_c)
d3 = (type_b, type_c, type_d, type_a)
d4 = (type_b, type_c, type_a, type_d)
d5 = (type_b, type_d, type_a, type_c)
d6 = (type_b, type_d, type_c, type_a)
if d1 in improper_type.keys():
print("found d1" + str(d1))
type = improper_type[d1]
elif d2 in improper_type.keys():
print("found d2")
type = improper_type[d2]
elif d3 in improper_type.keys():
print("found d3")
type = improper_type[d3]
elif d4 in improper_type.keys():
print("found d4")
type = improper_type[d4]
elif d5 in improper_type.keys():
print("found d5")
type = improper_type[d5]
elif d6 in improper_type.keys():
print("found d6")
type = improper_type[d6]
else:
print("found else" + str(d1))
count += 1
type = count
improper_type[d1] = type
self.unique_improper_types[type] = improper
improper.ff_type_index = type
print(improper.ff_type_index)
for key, improper in list(self.unique_improper_types.items()):
print(str(key) + " : " + str(improper.atoms[0].force_field_type) +
"-" + str(improper.atoms[1].force_field_type) + "-" +
str(improper.atoms[2].force_field_type) + "-" +
str(improper.atoms[3].force_field_type))
def van_der_waals_pairs(self):
atom_types = self.unique_atom_types.keys()
for type1, type2 in itertools.combinations_with_replacement(atom_types, 2):
atm1 = self.unique_atom_types[type1]
atm2 = self.unique_atom_types[type2]
print(str(re.findall(r'^[a-zA-Z]*',atm1.force_field_type)[0]))
print(str(re.findall(r'^[a-zA-Z]*',atm2.force_field_type)[0]))
# if we are using non-UFF atom types, need to splice off the end descriptors (first non alphabetic char)
eps1 = UFF_DATA_nonbonded[re.findall(r'^[a-zA-Z]*',atm1.force_field_type)[0]][3]
eps2 = UFF_DATA_nonbonded[re.findall(r'^[a-zA-Z]*',atm2.force_field_type)[0]][3]
# radius --> sigma = radius*2**(-1/6)
sig1 = UFF_DATA_nonbonded[re.findall(r'^[a-zA-Z]*',atm1.force_field_type)[0]][2]*(2**(-1./6.))
sig2 = UFF_DATA_nonbonded[re.findall(r'^[a-zA-Z]*',atm2.force_field_type)[0]][2]*(2**(-1./6.))
# l-b mixing
eps = math.sqrt(eps1*eps2)
sig = (sig1 + sig2) / 2.
self.unique_pair_types[(type1, type2)] = (eps, sig)
def parse_user_input(self, filename):
infile = open("user_input.txt","r")
lines = infile.readlines()
# type of interaction found: 1= bonds, 2 = angles, 3 = dihedrals, 4 = torsions
parse_type = 0
for line in lines:
match = line.lower().strip()
if match == "bonds":
print("parsing bond")
parse_type = 1
continue
elif match == "angles":
print("parsing angle")
parse_type = 2
continue
elif match == "dihedrals":
print("parsing dihedral")
parse_type = 3
continue
elif match == "impropers":
print("parsing impropers")
parse_type = 4
continue
data = line.split()
print(data)
if parse_type == 1:
atms = [data[0], data[1]]
bond_pair = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1])]
bond_id = self.map_pair_unique_bond(bond_pair, atms)
self.unique_bond_types[bond_id].function = data[2]
self.unique_bond_types[bond_id].parameters = data[3:]
elif parse_type == 2:
atms = [data[0], data[1], data[2]]
angle_triplet = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1]),
self.map_user_to_unique_atom(atms[2])]
angle_id = self.map_triplet_unique_angle(angle_triplet, atms)
self.unique_angle_types[angle_id].function = data[3]
self.unique_angle_types[angle_id].parameters = data[4:]
elif parse_type == 3:
atms = [data[0], data[1], data[2], data[3]]
dihedral_quadruplet = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1]),
self.map_user_to_unique_atom(atms[2]),
self.map_user_to_unique_atom(atms[3])]
dihedral_id = self.map_quadruplet_unique_dihedral(dihedral_quadruplet, atms)
self.unique_dihedral_types[dihedral_id].function = data[4]
self.unique_dihedral_types[dihedral_id].parameters = data[5:]
elif parse_type == 4:
atms = [data[0], data[1], data[2], data[3]]
improper_quadruplet = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1]),
self.map_user_to_unique_atom(atms[2]),
self.map_user_to_unique_atom(atms[3])]
improper_id = self.map_quadruplet_unique_improper(improper_quadruplet, atms)
self.unique_improper_types[improper_id].function = data[4]
self.unique_improper_types[improper_id].parameters = data[5:]
def write_missing_uniques(self, description):
# Warn user about any unique bond, angle, etc. found that have not
# been specified in user_input.txt
pass
def map_user_to_unique_atom(self, descriptor):
for key, atom in list(self.unique_atom_types.items()):
if descriptor == atom.force_field_type:
return atom.ff_type_index
raise ValueError('Error! An atom identifier ' + str(description) +
' in user_input.txt did not match any atom_site_description in your cif')
def map_pair_unique_bond(self, pair, descriptor):
for key, bond in list(self.unique_bond_types.items()):
if (pair == [bond.atoms[0].ff_type_index, bond.atoms[1].ff_type_index]
or pair == [bond.atoms[1].ff_type_index, bond.atoms[0].ff_type_index]):
return key
raise ValueError('Error! An bond identifier ' + str(descriptor) +
' in user_input.txt did not match any bonds in your cif')
def map_triplet_unique_angle(self, triplet, descriptor):
#print(triplet)
#print(descriptor)
for key, angle in list(self.unique_angle_types.items()):
#print(str(key) + " : " + str([angle.atoms[2].ff_type_index, angle.atoms[1].ff_type_index, angle.atoms[0].ff_type_index]))
if (triplet == [angle.atoms[0].ff_type_index,
angle.atoms[1].ff_type_index,
angle.atoms[2].ff_type_index] or
triplet == [angle.atoms[2].ff_type_index,
angle.atoms[1].ff_type_index,
angle.atoms[0].ff_type_index]):
return key
raise ValueError('Error! An angle identifier ' + str(descriptor) +
' in user_input.txt did not match any angles in your cif')
def map_quadruplet_unique_dihedral(self, quadruplet, descriptor):
for key, dihedral in list(self.unique_dihedral_types.items()):
if (quadruplet == [dihedral.atoms[0].ff_type_index,
dihedral.atoms[1].ff_type_index,
dihedral.atoms[2].ff_type_index,
dihedral.atoms[3].ff_type_index] or
quadruplet == [dihedral.atoms[3].ff_type_index,
dihedral.atoms[2].ff_type_index,
dihedral.atoms[1].ff_type_index,
dihedral.atoms[0].ff_type_index]):
return key
raise ValueError('Error! A dihdral identifier ' + str(descriptor) +
' in user_input.txt did not match any dihedrals in your cif')
def map_quadruplet_unique_improper(self, quadruplet, descriptor):
for key, improper in list(self.unique_improper_types.items()):
if (quadruplet == [improper.atoms[0].ff_type_index,
improper.atoms[1].ff_type_index,
improper.atoms[2].ff_type_index,
improper.atoms[3].ff_type_index] or
quadruplet == [improper.atoms[3].ff_type_index,
improper.atoms[2].ff_type_index,
improper.atoms[1].ff_type_index,
improper.atoms[0].ff_type_index]):
return key
raise ValueError('Error! An improper identifier ' + str(descriptor) +
' in user_input.txt did not match any improper in your cif')
def overwrite_force_field_terms(self):
self.parse_user_input("blah")
def compute_force_field_terms(self):
self.unique_atoms()
self.unique_bonds()
self.unique_angles()
self.unique_dihedrals()
self.unique_impropers()
self.parse_user_input("blah")
self.van_der_waals_pairs()
class OverwriteFF(ForceField):
"""
Prepare a nanoprous material FF from a given structure for a known
FF type.
Then overwrite any parameters that are supplied by user_input.txt
Methods are duplicated from UserFF, can reduce redundancy of code
later if desired
"""
def __init__(self, struct, base_FF):
# Assign the base ForceField
if(baseFF == "UFF"):
self = UFF(struct)
elif(baseFF == "DREIDING"):
self = Dreiding(struct)
elif(baseFF == "CVFF"):
print("CVFF not implemented yet...")
sys.exit()
pass
elif(baseFF == "CHARMM"):
print("CHARMM not implemented yet...")
sys.exit()
pass
else:
# etc. TODO worth adding in these additional FF types
print("Invalid base FF requested\nExiting...")
sys.exit()
# Overwrite any parameters specified by user_input.txt
parse_user_input("user_input.txt")
def parse_user_input(self, filename):
infile = open("user_input.txt","r")
lines = infile.readlines()
# type of interaction found: 1= bonds, 2 = angles, 3 = dihedrals, 4 = torsions
parse_type = 0
for line in lines:
match = line.lower().strip()
if match == "bonds":
print("parsing bond")
parse_type = 1
continue
elif match == "angles":
print("parsing angle")
parse_type = 2
continue
elif match == "dihedrals":
print("parsing dihedral")
parse_type = 3
continue
elif match == "impropers":
print("parsing impropers")
parse_type = 4
continue
data = line.split()
print(data)
if parse_type == 1:
atms = [data[0], data[1]]
bond_pair = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1])]
bond_id = self.map_pair_unique_bond(bond_pair, atms)
self.unique_bond_types[bond_id].function = data[2]
self.unique_bond_types[bond_id].parameters = data[3:]
elif parse_type == 2:
atms = [data[0], data[1], data[2]]
angle_triplet = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1]),
self.map_user_to_unique_atom(atms[2])]
angle_id = self.map_triplet_unique_angle(angle_triplet, atms)
self.unique_angle_types[angle_id].function = data[3]
self.unique_angle_types[angle_id].parameters = data[4:]
elif parse_type == 3:
atms = [data[0], data[1], data[2], data[3]]
dihedral_quadruplet = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1]),
self.map_user_to_unique_atom(atms[2]),
self.map_user_to_unique_atom(atms[3])]
dihedral_id = self.map_quadruplet_unique_dihedral(dihedral_quadruplet, atms)
self.unique_dihedral_types[dihedral_id].function = data[4]
self.unique_dihedral_types[dihedral_id].parameters = data[5:]
elif parse_type == 4:
atms = [data[0], data[1], data[2], data[3]]
improper_quadruplet = [self.map_user_to_unique_atom(atms[0]),
self.map_user_to_unique_atom(atms[1]),
self.map_user_to_unique_atom(atms[2]),
self.map_user_to_unique_atom(atms[3])]
improper_id = self.map_quadruplet_unique_improper(improper_quadruplet, atms)
self.unique_improper_types[improper_id].function = data[4]
self.unique_improper_types[improper_id].parameters = data[5:]
def write_missing_uniques(self, description):
# Warn user about any unique bond, angle, etc. found that have not
# been specified in user_input.txt
pass
def map_user_to_unique_atom(self, descriptor):
for key, atom in list(self.unique_atom_types.items()):
if descriptor == atom.force_field_type:
return atom.ff_type_index
raise ValueError('Error! An atom identifier ' + str(description) +
' in user_input.txt did not match any atom_site_description in your cif')
def map_pair_unique_bond(self, pair, descriptor):
for key, bond in list(self.unique_bond_types.items()):
if (pair == [bond.atoms[0].ff_type_index, bond.atoms[1].ff_type_index]
or pair == [bond.atoms[1].ff_type_index, bond.atoms[0].ff_type_index]):
return key
raise ValueError('Error! A bond identifier ' + str(descriptor) +
' in user_input.txt did not match any bonds in your cif')
def map_triplet_unique_angle(self, triplet, descriptor):
#print(triplet)
#print(descriptor)
for key, angle in list(self.unique_angle_types.items()):
#print(str(key) + " : " + str([angle.atoms[2].ff_type_index, angle.atoms[1].ff_type_index, angle.atoms[0].ff_type_index]))
if (triplet == [angle.atoms[0].ff_type_index,
angle.atoms[1].ff_type_index,
angle.atoms[2].ff_type_index] or
triplet == [angle.atoms[2].ff_type_index,
angle.atoms[1].ff_type_index,
angle.atoms[0].ff_type_index]):
return key
raise ValueError('Error! An angle identifier ' + str(descriptor) +
' in user_input.txt did not match any angles in your cif')
def map_quadruplet_unique_dihedral(self, quadruplet, descriptor):
for key, dihedral in list(self.unique_dihedral_types.items()):
if (quadruplet == [dihedral.atoms[0].ff_type_index,
dihedral.atoms[1].ff_type_index,
dihedral.atoms[2].ff_type_index,
dihedral.atoms[3].ff_type_index] or
quadruplet == [dihedral.atoms[3].ff_type_index,
dihedral.atoms[2].ff_type_index,
dihedral.atoms[1].ff_type_index,
dihedral.atoms[0].ff_type_index]):
return key
raise ValueError('Error! A dihdral identifier ' + str(descriptor) +
' in user_input.txt did not match any dihedrals in your cif')
def map_quadruplet_unique_improper(self, quadruplet, descriptor):
for key, improper in list(self.unique_improper_types.items()):
if (quadruplet == [improper.atoms[0].ff_type_index,
improper.atoms[1].ff_type_index,
improper.atoms[2].ff_type_index,
improper.atoms[3].ff_type_index] or
quadruplet == [improper.atoms[3].ff_type_index,
improper.atoms[2].ff_type_index,
improper.atoms[1].ff_type_index,
improper.atoms[0].ff_type_index]):
return key
raise ValueError('Error! An improper identifier ' + str(descriptor) +
' in user_input.txt did not match any improper in your cif')
class BTW_FF(ForceField):
def __init__(self, **kwargs):
self.pair_in_data = False
self.keep_metal_geometry = False
self.graph = None
self.qeq = False
# override existing arguments with kwargs
for key, value in kwargs.items():
setattr(self, key, value)
if (self.graph is not None):
self.detect_ff_terms()
self.compute_force_field_terms()
def detect_ff_terms(self):
"""
Assigning force field type of atoms
"""
# for each atom determine the ff type if it is None
BTW_organics = ["O", "C", "H"]
mof_sbus = set(self.graph.inorganic_sbus.keys())
BTW_sbus = set(["Cu Paddlewheel", "Zn4O", "Zr_UiO"])
if not (mof_sbus <= BTW_sbus):
print("The system cannot be simulated with BTW-FF!")
sys.exit()
elif ( len(mof_sbus)> 1):
print("No exact charge for the IRMOF is available from BTW-FF. Average charges in BTW-FF is used.")
chrg_flag="TFF_" # Transferable FF charges (average values)
elif("Zn4O" in mof_sbus):
#resp= input("What is the IRMOF number?[1/10]")
# temp fix so I don't have to respond to screen prompt
resp = "0"
if self.graph.number_of_nodes() == 424:
resp = "1"
elif self.graph.number_of_nodes() == 664:
resp = "10"
if resp=="1":
chrg_flag="Zn4O_"
elif resp=="10":
chrg_flag="IRMOF10_"
else:
#print("No exact charge for the IRMOF is available from BTW-FF. Average charges in BTW-FF is used.")
#chrg_flag="TFF_"
print("Cannot parameterize this MOF with BTW-FF.")
sys.exit()
else:
sbu_type = next(iter(mof_sbus))
chrg_flag=sbu_type+"_"
#Assigning force field type of atoms
for node, atom in self.graph.nodes_iter(data=True):
# check if element not in one of the SBUS
if atom['element'] == "Cu":
try:
if atom['special_flag'] == 'Cu_pdw':
atom['force_field_type']="185"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("ERROR: Cu %i is not assigned to a Cu Paddlewheel! exiting"%(node))
sys.exit()
except KeyError:
print("ERROR: Cu %i is not assigned to a Cu Paddlewheel! exiting"%(node))
sys.exit()
elif atom['element'] == "Zn":
try:
if atom['special_flag'] == 'Zn4O':
atom['force_field_type']="172"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("ERROR: Zn %i is not assigned to a Zn4O! exiting"%(node))
sys.exit()
except KeyError:
print("ERROR: Zn %i is not assigned to a Zn4O! exiting"%(node))
sys.exit()
elif atom['element'] == "Zr":
try:
if atom['special_flag'] == 'Zr_UiO':
atom['force_field_type']="192"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("ERROR: Zr %i is not assigned to a Zr_UiO! exiting"%(node))
sys.exit()
except KeyError:
print("ERROR: Zr %i is not assigned to a Zr_UiO! exiting"%(node))
sys.exit()
if atom['force_field_type'] is None:
type_assigned = False
neighbours = [self.graph.node[i] for i in self.graph.neighbors(node)]
neighbour_elements = [a['element'] for a in neighbours]
special = False
if 'special_flag' in atom:
special = True
if (atom['element'] == "O") and special:
# Zn4O cases
if atom['special_flag'] == "O_z_Zn4O":
atom['force_field_type'] = "171"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
elif atom['special_flag'] == "O_c_Zn4O":
atom['force_field_type'] = "170"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
# Zr_UiO cases
elif atom['special_flag'] == "O_z_Zr_UiO":
atom['force_field_type'] = "171"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
elif atom['special_flag'] == "O_h_Zr_UiO":
atom['force_field_type'] = "75"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
elif atom['special_flag'] == "O_c_Zr_UiO":
atom['force_field_type'] = "170"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
# Cu Paddlewheel case
elif (atom['special_flag'] == "O1_Cu_pdw") or (atom['special_flag'] == "O2_Cu_pdw"):
atom['force_field_type'] = "170"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("Oxygen number %i type cannot be detected!"%node)
sys.exit()
elif (atom['element'] == "C") and special:
# Zn4O case
if atom['special_flag'] == "C_Zn4O":
atom['force_field_type'] = "913" # C-acid
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
# Zr_UiO case
elif atom['special_flag'] == "C_Zr_UiO":
atom['force_field_type'] = "913" # C-acid
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
# Cu Paddlewheel case
elif atom['special_flag'] == "C_Cu_pdw":
atom['force_field_type'] = "913" # C-acid
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("Carbon number %i type cannot be detected!"%node)
sys.exit()
elif (atom['element'] == "H") and special:
# only UiO case
if atom['special_flag'] == "H_o_Zr_UiO":
atom['force_field_type'] = "21"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("Hydrogen number %i type cannot be detected!"%node)
sys.exit()
# currently no oxygens assigned types outside of metal SBUs
elif (atom['element'] == "O") and not special:
print("Oxygen number %i type cannot be detected!"%node)
sys.exit()
elif (atom['element'] == "C") and not special:
# all organic SBUs have the same types..
if set(neighbour_elements) == set(["C","H"]):
atom['force_field_type'] = "912" # C- benzene we should be careful that in this case C in ligand has also bond with H, but not in the FF
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
elif set(neighbour_elements) == set(["C"]):
# check if carbon adjacent to metal SBU (signified by the key 'special_flag')
if any(['special_flag' in at for at in neighbours]):
atom['force_field_type'] = "902"
else:
atom['force_field_type'] = "903"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("Carbon number %i type cannot be detected!"%node)
sys.exit()
elif (atom['element'] == "H") and not special:
if set(neighbour_elements)<=set(["C"]):
atom['force_field_type'] = "915"
chrg_key = chrg_flag+atom['force_field_type']
atom['charge']=BTW_charges[chrg_key]
else:
print("Hydrogen number %i type cannot be detected!"%node)
sys.exit()
# Assigning force field type of bonds
for a, b, bond in self.graph.edges_iter2(data=True):
a_atom = self.graph.node[a]
b_atom = self.graph.node[b]
atom_a_fflabel, atom_b_fflabel = a_atom['force_field_type'], b_atom['force_field_type']
bond_fflabel1 = atom_a_fflabel+"_"+atom_b_fflabel
bond_fflabel2 = atom_b_fflabel+"_"+atom_a_fflabel
if bond_fflabel1 in BTW_bonds:
bond['force_field_type']=bond_fflabel1
elif bond_fflabel2 in BTW_bonds:
bond['force_field_type']=bond_fflabel2
else:
print ("BTW-FF cannot be used for the system!\nNo parameter found for bond %s"%(bond_fflabel1))
sys.exit()
#Assigning force field type of angles
missing_labels=[]
for b , data in self.graph.nodes_iter(data=True):
try:
missing_angles=[]
ang_data = data['angles']
for (a, c), val in ang_data.items():
a_atom = self.graph.node[a]
b_atom = data
c_atom = self.graph.node[c]
atom_a_fflabel = a_atom['force_field_type']
atom_b_fflabel = b_atom['force_field_type']
atom_c_fflabel = c_atom['force_field_type']
angle_fflabel1=atom_a_fflabel+"_"+atom_b_fflabel+"_"+atom_c_fflabel
angle_fflabel2=atom_c_fflabel+"_"+atom_b_fflabel+"_"+atom_a_fflabel
if (angle_fflabel1=="170_185_170"):
val['force_field_type']=angle_fflabel1
elif angle_fflabel1 in BTW_angles:
val['force_field_type']=angle_fflabel1
elif angle_fflabel2 in BTW_angles:
val['force_field_type']=angle_fflabel2
else:
missing_angles.append((a,c))
missing_labels.append(angle_fflabel1)
for key in missing_angles:
del ang_data[key]
except KeyError:
pass
for ff_label in set(missing_labels):
print ("%s angle is deleted since the angle was not parametrized in BTW-FF!"%(ff_label))
#Assigning force field type of dihedrals
missing_labels=[]
for b, c, data in self.graph.edges_iter2(data=True):
try:
missing_dihedral=[]
dihed_data = data['dihedrals']
for (a, d), val in dihed_data.items():
a_atom = self.graph.node[a]
b_atom = self.graph.node[b]
c_atom = self.graph.node[c]
d_atom = self.graph.node[d]
atom_a_fflabel = a_atom['force_field_type']
atom_b_fflabel = b_atom['force_field_type']
atom_c_fflabel = c_atom['force_field_type']
atom_d_fflabel = d_atom['force_field_type']
dihedral_fflabel1=atom_a_fflabel+"_"+atom_b_fflabel+"_"+atom_c_fflabel+"_"+atom_d_fflabel
dihedral_fflabel2=atom_d_fflabel+"_"+atom_c_fflabel+"_"+atom_b_fflabel+"_"+atom_a_fflabel
if dihedral_fflabel1 in BTW_dihedrals:
val['force_field_type']=dihedral_fflabel1
elif dihedral_fflabel2 in BTW_dihedrals:
val['force_field_type']=dihedral_fflabel2
else:
missing_dihedral.append((a,d))
missing_labels.append(dihedral_fflabel1)
for key in missing_dihedral:
del dihed_data[key]
except KeyError:
pass
for ff_label in set(missing_labels):
print ("%s dihedral is deleted since the dihedral was not parametrized in BTW-FF!"%(ff_label))
#Assigning force field type of impropers
missing_labels=[]
for b, data in self.graph.nodes_iter(data=True):
try:
missing_improper=[]
imp_data = data['impropers']
for (a, c, d), val in imp_data.items():
a_atom = self.graph.node[a]
b_atom = self.graph.node[b]
c_atom = self.graph.node[c]
d_atom = self.graph.node[d]
atom_a_fflabel = a_atom['force_field_type']
atom_b_fflabel = b_atom['force_field_type']
atom_c_fflabel = c_atom['force_field_type']
atom_d_fflabel = d_atom['force_field_type']
improper_fflabel=atom_a_fflabel+"_"+atom_b_fflabel+"_"+atom_c_fflabel+"_"+atom_d_fflabel
if improper_fflabel in BTW_opbends:
val['force_field_type']=improper_fflabel
else:
missing_improper.append((a,c,d))
missing_labels.append(improper_fflabel)
for key in missing_improper:
del imp_data[key]
except KeyError:
pass
for ff_label in set(missing_labels):
print ("%s improper is deleted since the improper was not parametrized in BTW-FF!"%(ff_label))
def bond_term(self, edge):
"""class2 bond: 4-order polynomial """
n1, n2, data = edge
Ks = BTW_bonds[data['force_field_type']][0]
l0 = BTW_bonds[data['force_field_type']][1]
### All the factors are conversion to kcal/mol from the units in the paper ###
K2= 71.94*Ks
K3= -2.55*K2
K4= 3.793125*K2
data['potential'] = BondPotential.Class2()
data['potential'].K2 = K2
data['potential'].K3 = K3
data['potential'].K4 = K4
data['potential'].R0 = l0
return 1
def angle_term(self, angle):
"""class2 angle
NOTE: We ignored the 5and6 order terms of polynomial since the functional is not implemented in LAMMPS!!
"""
a, b, c, data = angle
if (data['force_field_type']=="170_185_170"): ### in the case of square planar coordination of Cu-paddle-wheel, fourier angle must be used
data['potential'] = AnglePotential.CosinePeriodic()
data['potential'].C = 126.64 # conversion from the K value in MOF-FF to the C value for LAMMPS in cosine/periodic
data['potential'].B = 1
data['potential'].n = 4
return 1
a_data = self.graph.node[a]
b_data = self.graph.node[b]