-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_serialization.py
More file actions
380 lines (325 loc) · 13.1 KB
/
Copy pathtest_serialization.py
File metadata and controls
380 lines (325 loc) · 13.1 KB
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
import numpy as np
import pytest
from timemachine.ff.handlers import bonded, nonbonded
from timemachine.ff.handlers.deserialize import deserialize_handlers
from timemachine.ff.handlers.serialize import bin_to_str
pytestmark = [pytest.mark.nocuda]
def test_harmonic_bond():
patterns = [
["[#6X4:1]-[#6X4:2]", 0.1, 0.2],
["[#6X4:1]-[#6X3:2]", 99.0, 99.0],
["[#6X4:1]-[#6X3:2]=[#8X1+0]", 99.0, 99.0],
["[#6X3:1]-[#6X3:2]", 99.0, 99.0],
["[#6X3:1]:[#6X3:2]", 99.0, 99.0],
["[#6X3:1]=[#6X3:2]", 99.0, 99.0],
["[#6:1]-[#7:2]", 0.1, 0.2],
["[#6X3:1]-[#7X3:2]", 99.0, 99.0],
["[#6X4:1]-[#7X3:2]-[#6X3]=[#8X1+0]", 99.0, 99.0],
["[#6X3:1](=[#8X1+0])-[#7X3:2]", 99.0, 99.0],
["[#6X3:1]-[#7X2:2]", 99.0, 99.0],
["[#6X3:1]:[#7X2,#7X3+1:2]", 99.0, 99.0],
["[#6X3:1]=[#7X2,#7X3+1:2]", 99.0, 99.0],
["[#6:1]-[#8:2]", 99.0, 99.0],
["[#6X3:1]-[#8X1-1:2]", 99.0, 99.0],
["[#6X4:1]-[#8X2H0:2]", 0.3, 0.4],
["[#6X3:1]-[#8X2:2]", 99.0, 99.0],
["[#6X3:1]-[#8X2H1:2]", 99.0, 99.0],
["[#6X3a:1]-[#8X2H0:2]", 99.0, 99.0],
["[#6X3:1](=[#8X1])-[#8X2H0:2]", 99.0, 99.0],
["[#6:1]=[#8X1+0,#8X2+1:2]", 99.0, 99.0],
["[#6X3:1](~[#8X1])~[#8X1:2]", 99.0, 99.0],
["[#6X3:1]~[#8X2+1:2]~[#6X3]", 99.0, 99.0],
["[#6X2:1]-[#6:2]", 99.0, 99.0],
["[#6X2:1]-[#6X4:2]", 99.0, 99.0],
["[#6X2:1]=[#6X3:2]", 99.0, 99.0],
["[#6:1]#[#7:2]", 99.0, 99.0],
["[#6X2:1]#[#6X2:2]", 99.0, 99.0],
["[#6X2:1]-[#8X2:2]", 99.0, 99.0],
["[#6X2:1]-[#7:2]", 99.0, 99.0],
["[#6X2:1]=[#7:2]", 99.0, 99.0],
["[#16:1]=[#6:2]", 99.0, 99.0],
["[#6X2:1]=[#16:2]", 99.0, 99.0],
["[#7:1]-[#7:2]", 99.0, 99.0],
["[#7X3:1]-[#7X2:2]", 99.0, 99.0],
["[#7X2:1]-[#7X2:2]", 99.0, 99.0],
["[#7:1]:[#7:2]", 99.0, 99.0],
["[#7:1]=[#7:2]", 99.0, 99.0],
["[#7+1:1]=[#7-1:2]", 99.0, 99.0],
["[#7:1]#[#7:2]", 99.0, 99.0],
["[#7:1]-[#8X2:2]", 99.0, 99.0],
["[#7:1]~[#8X1:2]", 99.0, 99.0],
["[#8X2:1]-[#8X2:2]", 99.0, 99.0],
["[#16:1]-[#6:2]", 99.0, 99.0],
["[#16:1]-[#1:2]", 99.0, 99.0],
["[#16:1]-[#16:2]", 99.0, 99.0],
["[#16:1]-[#9:2]", 99.0, 99.0],
["[#16:1]-[#17:2]", 99.0, 99.0],
["[#16:1]-[#35:2]", 99.0, 99.0],
["[#16:1]-[#53:2]", 99.0, 99.0],
["[#16X2,#16X1-1,#16X3+1:1]-[#6X4:2]", 99.0, 99.0],
["[#16X2,#16X1-1,#16X3+1:1]-[#6X3:2]", 99.0, 99.0],
["[#16X2:1]-[#7:2]", 99.0, 99.0],
["[#16X2:1]-[#8X2:2]", 99.0, 99.0],
["[#16X2:1]=[#8X1,#7X2:2]", 99.0, 99.0],
["[#16X4,#16X3!+1:1]-[#6:2]", 99.0, 99.0],
["[#16X4,#16X3:1]~[#7:2]", 99.0, 99.0],
["[#16X4,#16X3:1]-[#8X2:2]", 99.0, 99.0],
["[#16X4,#16X3:1]~[#8X1:2]", 99.0, 99.0],
["[#15:1]-[#1:2]", 99.0, 99.0],
["[#15:1]~[#6:2]", 99.0, 99.0],
["[#15:1]-[#7:2]", 99.0, 99.0],
["[#15:1]=[#7:2]", 99.0, 99.0],
["[#15:1]~[#8X2:2]", 99.0, 99.0],
["[#15:1]~[#8X1:2]", 99.0, 99.0],
["[#16:1]-[#15:2]", 99.0, 99.0],
["[#15:1]=[#16X1:2]", 99.0, 99.0],
["[#6:1]-[#9:2]", 99.0, 99.0],
["[#6X4:1]-[#9:2]", 0.6, 0.7],
["[#6:1]-[#17:2]", 99.0, 99.0],
["[#6X4:1]-[#17:2]", 99.0, 99.0],
["[#6:1]-[#35:2]", 99.0, 99.0],
["[#6X4:1]-[#35:2]", 99.0, 99.0],
["[#6:1]-[#53:2]", 99.0, 99.0],
["[#6X4:1]-[#53:2]", 99.0, 99.0],
["[#7:1]-[#9:2]", 99.0, 99.0],
["[#7:1]-[#17:2]", 99.0, 99.0],
["[#7:1]-[#35:2]", 99.0, 99.0],
["[#7:1]-[#53:2]", 99.0, 99.0],
["[#15:1]-[#9:2]", 99.0, 99.0],
["[#15:1]-[#17:2]", 99.0, 99.0],
["[#15:1]-[#35:2]", 99.0, 99.0],
["[#15:1]-[#53:2]", 99.0, 99.0],
["[#6X4:1]-[#1:2]", 99.0, 99.0],
["[#6X3:1]-[#1:2]", 99.0, 99.0],
["[#6X2:1]-[#1:2]", 99.0, 99.0],
["[#7:1]-[#1:2]", 99.0, 99.0],
["[#8:1]-[#1:2]", 99.0, 99.1],
]
smirks = [x[0] for x in patterns]
params = np.array([[x[1], x[2]] for x in patterns])
hbh = bonded.HarmonicBondHandler(smirks, params, None)
obj = hbh.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
assert len(all_handlers) == 1
new_hbh = all_handlers[0]
np.testing.assert_equal(new_hbh.smirks, hbh.smirks)
np.testing.assert_equal(new_hbh.params, hbh.params)
assert new_hbh.props == hbh.props
def test_proper_torsion():
# proper torsions have a variadic number of terms
patterns = [
["[*:1]-[#6X3:2]=[#6X3:3]-[*:4]", [[99.0, 99.0, 99.0]]],
["[*:1]-[#6X3:2]=[#6X3:3]-[#35:4]", [[99.0, 99.0, 99.0]]],
["[#9:1]-[#6X3:2]=[#6X3:3]-[#35:4]", [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]],
["[#35:1]-[#6X3:2]=[#6X3:3]-[#35:4]", [[7.0, 8.0, 9.0], [1.0, 3.0, 5.0], [4.0, 4.0, 4.0]]],
["[#9:1]-[#6X3:2]=[#6X3:3]-[#9:4]", [[7.0, 8.0, 9.0]]],
]
smirks = [x[0] for x in patterns]
params = [x[1] for x in patterns]
ph = bonded.ProperTorsionHandler(smirks, params, None)
obj = ph.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
assert len(all_handlers) == 1
new_ph = all_handlers[0]
np.testing.assert_equal(new_ph.smirks, ph.smirks)
np.testing.assert_equal(new_ph.params, ph.params)
assert new_ph.props == ph.props
def test_improper_torsion():
patterns = [
["[*:1]~[#6X3:2](~[*:3])~[*:4]", 1.5341333333333333, 3.141592653589793, 2.0],
["[*:1]~[#6X3:2](~[#8X1:3])~[#8:4]", 99.0, 99.0, 99.0],
["[*:1]~[#7X3$(*~[#15,#16](!-[*])):2](~[*:3])~[*:4]", 99.0, 99.0, 99.0],
["[*:1]~[#7X3$(*~[#6X3]):2](~[*:3])~[*:4]", 1.3946666666666667, 3.141592653589793, 2.0],
["[*:1]~[#7X3$(*~[#7X2]):2](~[*:3])~[*:4]", 99.0, 99.0, 99.0],
["[*:1]~[#7X3$(*@1-[*]=,:[*][*]=,:[*]@1):2](~[*:3])~[*:4]", 99.0, 99.0, 99.0],
["[*:1]~[#6X3:2](=[#7X2,#7X3+1:3])~[#7:4]", 99.0, 99.0, 99.0],
]
smirks = [x[0] for x in patterns]
params = np.array([[x[1], x[2], x[3]] for x in patterns])
imph = bonded.ImproperTorsionHandler(smirks, params, None)
obj = imph.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
assert len(all_handlers) == 1
new_imph = all_handlers[0]
np.testing.assert_equal(new_imph.smirks, imph.smirks)
np.testing.assert_equal(new_imph.params, imph.params)
assert new_imph.props == imph.props
def test_simple_charge_handler():
patterns = [
["[#1:1]", 99.0],
["[#1:1]-[#6X4]", 99.0],
["[#1:1]-[#6X4]-[#7,#8,#9,#16,#17,#35]", 99.0],
["[#1:1]-[#6X4](-[#7,#8,#9,#16,#17,#35])-[#7,#8,#9,#16,#17,#35]", 99.0],
["[#1:1]-[#6X4](-[#7,#8,#9,#16,#17,#35])(-[#7,#8,#9,#16,#17,#35])-[#7,#8,#9,#16,#17,#35]", 99.0],
["[#1:1]-[#6X4]~[*+1,*+2]", 99.0],
["[#1:1]-[#6X3]", 99.0],
["[#1:1]-[#6X3]~[#7,#8,#9,#16,#17,#35]", 99.0],
["[#1:1]-[#6X3](~[#7,#8,#9,#16,#17,#35])~[#7,#8,#9,#16,#17,#35]", 99.0],
["[#1:1]-[#6X2]", 99.0],
["[#1:1]-[#7]", 99.0],
["[#1:1]-[#8]", 99.0],
["[#1:1]-[#16]", 99.0],
["[#6:1]", 0.7],
["[#6X2:1]", 99.0],
["[#6X4:1]", 0.1],
["[#8:1]", 99.0],
["[#8X2H0+0:1]", 0.5],
["[#8X2H1+0:1]", 99.0],
["[#7:1]", 0.3],
["[#16:1]", 99.0],
["[#15:1]", 99.0],
["[#9:1]", 1.0],
["[#17:1]", 99.0],
["[#35:1]", 99.0],
["[#53:1]", 99.0],
["[#3+1:1]", 99.0],
["[#11+1:1]", 99.0],
["[#19+1:1]", 99.0],
["[#37+1:1]", 99.0],
["[#55+1:1]", 99.0],
["[#9X0-1:1]", 99.0],
["[#17X0-1:1]", 99.0],
["[#35X0-1:1]", 99.0],
["[#53X0-1:1]", 99.0],
]
smirks = [x[0] for x in patterns]
params = np.array([x[1] for x in patterns])
props = None
sch = nonbonded.SimpleChargeHandler(smirks, params, props)
obj = sch.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
assert len(all_handlers) == 1
new_sch = all_handlers[0]
np.testing.assert_equal(new_sch.smirks, sch.smirks)
np.testing.assert_equal(new_sch.params, sch.params)
assert new_sch.props == sch.props
def test_gbsa_handler():
patterns = [
["[*:1]", 99.0, 99.0],
["[#1:1]", 99.0, 99.0],
["[#1:1]~[#7]", 99.0, 99.0],
["[#6:1]", 0.1, 0.2],
["[#7:1]", 0.3, 0.4],
["[#8:1]", 0.5, 0.6],
["[#9:1]", 0.7, 0.8],
["[#14:1]", 99.0, 99.0],
["[#15:1]", 99.0, 99.0],
["[#16:1]", 99.0, 99.0],
["[#17:1]", 99.0, 99.0],
]
props = {
"solvent_dielectric": 78.3, # matches OBC2,
"solute_dielectric": 1.0,
"probe_radius": 0.14,
"surface_tension": 28.3919551,
"dielectric_offset": 0.009,
# GBOBC1
"alpha": 0.8,
"beta": 0.0,
"gamma": 2.909125,
}
smirks = [x[0] for x in patterns]
params = np.array([[x[1], x[2]] for x in patterns])
gbh = nonbonded.GBSAHandler(smirks, params, props)
obj = gbh.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
assert len(all_handlers) == 1
new_gbh = all_handlers[0]
np.testing.assert_equal(new_gbh.smirks, gbh.smirks)
np.testing.assert_equal(new_gbh.params, gbh.params)
assert new_gbh.props == gbh.props
def test_am1bcc():
smirks = []
params = []
props = None
am1 = nonbonded.AM1BCCHandler(smirks, params, props)
obj = am1.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
am1 = all_handlers[0]
np.testing.assert_equal(am1.smirks, am1.smirks)
np.testing.assert_equal(am1.params, am1.params)
assert am1.props == am1.props
def test_resp():
smirks = []
params = []
props = None
resp = nonbonded.RESPHandler(smirks, params, props)
obj = resp.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
resp = all_handlers[0]
np.testing.assert_equal(resp.smirks, resp.smirks)
np.testing.assert_equal(resp.params, resp.params)
assert resp.props == resp.props
def test_am1ccc():
patterns = [
["[#6X4:1]-[#1:2]", 0.46323257920556493],
["[#6X3$(*=[#8,#16]):1]-[#6a:2]", 0.24281402370571598],
["[#6X3$(*=[#8,#16]):1]-[#8X1,#8X2:2]", 1.0620166764992722],
["[#6X3$(*=[#8,#16]):1]=[#8X1$(*=[#6X3]-[#8X2]):2]", 2.227759732057297],
["[#6X3$(*=[#8,#16]):1]=[#8X1,#8X2:2]", 2.8182928673804217],
["[#6a:1]-[#8X1,#8X2:2]", 0.5315976926761063],
["[#6a:1]-[#1:2]", 0.0],
["[#6a:1]:[#6a:2]", 0.0],
["[#6a:1]:[#6a:2]", 0.0],
["[#8X1,#8X2:1]-[#1:2]", -2.3692047944101415],
["[#16:1]-[#8:2]", 99.0],
]
smirks = [x[0] for x in patterns]
params = np.array([x[1] * np.sqrt(138.935456) for x in patterns])
props = None
am1h = nonbonded.AM1CCCHandler(smirks, params, props)
obj = am1h.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
assert len(all_handlers) == 1
new_am1h = all_handlers[0]
np.testing.assert_equal(new_am1h.smirks, am1h.smirks)
np.testing.assert_equal(new_am1h.params, am1h.params)
assert new_am1h.props == am1h.props
def test_lennard_jones_handler():
patterns = [
["[#1:1]", 99.0, 999.0],
["[#1:1]-[#6X4]", 99.0, 999.0],
["[#1:1]-[#6X4]-[#7,#8,#9,#16,#17,#35]", 99.0, 999.0],
["[#1:1]-[#6X4](-[#7,#8,#9,#16,#17,#35])-[#7,#8,#9,#16,#17,#35]", 99.0, 999.0],
["[#1:1]-[#6X4](-[#7,#8,#9,#16,#17,#35])(-[#7,#8,#9,#16,#17,#35])-[#7,#8,#9,#16,#17,#35]", 99.0, 999.0],
["[#1:1]-[#6X4]~[*+1,*+2]", 99.0, 999.0],
["[#1:1]-[#6X3]", 99.0, 999.0],
["[#1:1]-[#6X3]~[#7,#8,#9,#16,#17,#35]", 99.0, 999.0],
["[#1:1]-[#6X3](~[#7,#8,#9,#16,#17,#35])~[#7,#8,#9,#16,#17,#35]", 99.0, 999.0],
["[#1:1]-[#6X2]", 99.0, 999.0],
["[#1:1]-[#7]", 99.0, 999.0],
["[#1:1]-[#8]", 99.0, 999.0],
["[#1:1]-[#16]", 99.0, 999.0],
["[#6:1]", 0.7, 0.8],
["[#6X2:1]", 99.0, 999.0],
["[#6X4:1]", 0.1, 0.2],
["[#8:1]", 99.0, 999.0],
["[#8X2H0+0:1]", 0.5, 0.6],
["[#8X2H1+0:1]", 99.0, 999.0],
["[#7:1]", 0.3, 0.4],
["[#16:1]", 99.0, 999.0],
["[#15:1]", 99.0, 999.0],
["[#9:1]", 1.0, 1.1],
["[#17:1]", 99.0, 999.0],
["[#35:1]", 99.0, 999.0],
["[#53:1]", 99.0, 999.0],
["[#3+1:1]", 99.0, 999.0],
["[#11+1:1]", 99.0, 999.0],
["[#19+1:1]", 99.0, 999.0],
["[#37+1:1]", 99.0, 999.0],
["[#55+1:1]", 99.0, 999.0],
["[#9X0-1:1]", 99.0, 999.0],
["[#17X0-1:1]", 99.0, 999.0],
["[#35X0-1:1]", 99.0, 999.0],
["[#53X0-1:1]", 99.0, 999.0],
]
smirks = [x[0] for x in patterns]
params = np.array([[x[1], x[2]] for x in patterns])
props = None
ljh = nonbonded.LennardJonesHandler(smirks, params, props)
obj = ljh.serialize()
all_handlers, _, _ = deserialize_handlers(bin_to_str(obj))
ljh = all_handlers[0]
np.testing.assert_equal(ljh.smirks, ljh.smirks)
np.testing.assert_equal(ljh.params, ljh.params)
assert ljh.props == ljh.props