forked from masoudk/AsiteDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradient.pyx
755 lines (603 loc) · 33.1 KB
/
Gradient.pyx
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
from pyrosetta import Pose
from pyrosetta import get_fa_scorefxn
from pyrosetta.rosetta.core.scoring import ScoreType
cimport cython
cimport openmp
import numpy as np
import random
from libc.math cimport cos, sin, acos, sqrt, M_PI, acos, atan2, fabs, pow, exp
from libc.float cimport DBL_MAX
from cython.parallel cimport prange
from cython.parallel cimport parallel
from pyrosetta.rosetta.utility import vector1_numeric_xyzVector_double_t as vec1
##from Energy cimport switch
from Energy import ligand_ligand_reduced, ligand_environment_reduced, bond, angle
""" Contains following functions
-> ligand_steep_decent
-> ligand_nonbonded_gradient
- ligand_enviroment_gradient
-> bond
-> angle
-> switch
"""
""" Vector definition conventions
The displacement vector between two particles i, and j is defined as:
uij = rj - ri
where the ri and rj are position vectors. Thus, force vectors for pair
potentials are defined as
fi = -g * uji
fj = -g * uij
where g is the gradient and uij = -uij.
"""
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.profile(True)
def ligand_steep_decent(double [:, :] ligand_xyz,
double[:, :] environment_xyz,
double [:] ligand_qij,
double [:] ligand_dljr,
double [:] ligand_dljep,
double [:, :] ligand_sol_prefactor,
double [:] ligand_lklam,
int [:, :] ligand_nonbonded,
double [:] ligand_nonbondedWeights,
double ligand_coupling,
double [:] ligand_weights,
int[:, :] bond_atomIndex,
double[:] equilibrium_distance,
double[:] bond_force_constance,
int [:, :] angle_atomIndex,
double [:] equilibrium_angle,
double [:] angle_force_constance,
#double[:, :] ligand_xyz,
double[:] ligand_mask,
double[:, :] ligand_environment_qij,
double[:, :] ligand_environment_dljr,
double[:, :] ligand_environment_dljep,
double[:, :, :] ligand_environment_sol_prefactor,
#double [:] ligand_lklam,
double [:] environmen_lklam,
int [:] environment_sideChainFalgs,
int [:] environment_mainChainFalgs,
double sideChainCoupling,
double mainChainCoupling,
double [:] ligand_environment_weights,
double convergence_cutoff,
int max_cycle,
int max_line_search):
cdef int N = ligand_xyz.shape[0]
cdef int M = ligand_xyz.shape[1]
cdef double [:, :] gradient_vector, xyz_tm
cdef double E_new, E_old, g_Max, g_norm2, step
cdef int i, j, k
gradient_vector = np.zeros((N, 3), dtype=np.float64)
xyz_tm = np.zeros((N, M), dtype=np.float64)
# Compute initial energy E_old
E_old = 0.0
E_old += ligand_ligand_reduced(xyz_tm,
ligand_qij,
ligand_dljr,
ligand_dljep,
ligand_sol_prefactor,
ligand_lklam,
ligand_nonbonded,
ligand_nonbondedWeights,
coupling,
weights)
# Compute ligand_environment Energy
E_old += ligand_environment_reduced(ligand_xyz,
environment_xyz,
ligand_mask,
ligand_environment_qij,
ligand_environment_dljr,
ligand_environment_dljep,
ligand_environment_sol_prefactor,
ligand_lklam,
environmen_lklam,
environment_sideChainFalgs,
environment_mainChainFalgs,
sideChainCoupling,
mainChainCoupling,
weights)
# Compute bond Energy
E_old += bond(ligand_xyz,
bond_atomIndex,
equilibrium_distance,
bond_force_constance)
# Compute Angles Energy
E_old += angle(ligand_xyz,
angle_atomIndex,
equilibrium_angle,
angle_force_constance)
# Minimize
for i in range(max_cycle):
# rest stuff
g_Max = -DBL_MAX
g_norm2 = 0.0
E_new = 0.0
# Reset the gradient_vector
for j in range(N):
gradient_vector[j, 0] = 0.0
gradient_vector[j, 1] = 0.0
gradient_vector[j, 2] = 0.0
# Compute gradient by xyz
# Compute ligand ligand non-bonded gradient
ligand_ligand_gradient(ligand_xyz,
ligand_qij,
ligand_dljr,
ligand_dljep,
ligand_sol_prefactor,
ligand_lklam,
ligand_nonbonded,
ligand_nonbondedWeights,
ligand_coupling,
ligand_weights,
gradient_vector)
# Compute ligand environment non-bonded gradient
ligand_environment_gradient(ligand_xyz,
environment_xyz,
ligand_mask,
ligand_environment_qij,
ligand_environment_dljr,
ligand_environment_dljep,
ligand_environment_sol_prefactor,
ligand_lklam,
environmen_lklam,
environment_sideChainFalgs,
environment_mainChainFalgs,
sideChainCoupling,
mainChainCoupling,
ligand_environment_weights,
gradient_vector)
# Compute ligand bond gradient
bond_gradient(ligand_xyz,
bond_bond_atomIndex,
equilibrium_distance,
force_constance,
gradient_vector)
# Compute ligand angle gradient
angle_gradient(ligand_xyz,
angle_atomIndex,
equilibrium_angle,
angle_force_constance,
gradient_vector)
# Go over the grad vector
for j in range(N):
# Compute the dot product of grad vector. Treat it as a flat matrix
g_norm2 += gradient_vector[j, 0] * gradient_vector[j, 0]
g_norm2 += gradient_vector[j, 1] * gradient_vector[j, 1]
g_norm2 += gradient_vector[j, 2] * gradient_vector[j, 2]
# get g_Max
if gradient_vector[j, 0] > g_Max:
g_Max = gradient_vector[j, 0]
if gradient_vector[j, 1] > g_Max:
g_Max = gradient_vector[j, 1]
if gradient_vector[j, 2] > g_Max:
g_Max = gradient_vector[j, 2]
# Choose a Step size such that g_Max*Step = 0.5
step = 0.5/g_Max
for j in range(max_line_search):
# Get the new coordinate
for k in range(N):
xyz_tm[k, 0] = ligand_xyz[k, 0] * (-1 * gradient_vector[k, 0] * step)
xyz_tm[k, 1] = ligand_xyz[k, 1] * (-1 * gradient_vector[k, 1] * step)
xyz_tm[k, 2] = ligand_xyz[k, 2] * (-1 * gradient_vector[k, 2] * step)
# compute E_new
# Compute ligand_ligand Energy
E_new += ligand_ligand_reduced(xyz_tm,
ligand_qij,
ligand_dljr,
ligand_dljep,
ligand_sol_prefactor,
ligand_lklam,
ligand_nonbonded,
ligand_nonbondedWeights,
coupling,
weights)
# Compute ligand_environment Energy
E_new += ligand_environment_reduced(xyz_tm,
environment_xyz,
ligand_mask,
ligand_environment_qij,
ligand_environment_dljr,
ligand_environment_dljep,
ligand_environment_sol_prefactor,
ligand_lklam,
environmen_lklam,
environment_sideChainFalgs,
environment_mainChainFalgs,
sideChainCoupling,
mainChainCoupling,
weights)
# Compute bond Energy
E_new += bond(xyz_tm,
bond_atomIndex,
equilibrium_distance,
bond_force_constance)
# Compute Angles Energy
E_new += angle(xyz_tm,
angle_atomIndex,
equilibrium_angle,
angle_force_constance)
if E_new > E_old:
step *= 0.5
continue
else: # accept the move
#update xyz
for k in range(N):
ligand_xyz[k, 0] = xyz_tm[k, 0]
ligand_xyz[k, 1] = xyz_tm[k, 1]
ligand_xyz[k, 2] = xyz_tm[k, 2]
break
# Quite if energy converges
if fabs(E_new - E_old) < 0.1:
return 0
else:
E_old = E_new
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.profile(True)
cdef inline void ligand_ligand_gradient(double [:, :] ligand_xyz,
double [:] ligand_qij,
double [:] ligand_dljr,
double [:] ligand_dljep,
double [:, :] ligand_sol_prefactor,
double [:] ligand_lklam,
int [:, :] ligand_nonbonded,
double [:] ligand_nonbondedWeights,
double ligand_coupling,
double [:] ligand_weights,
double [:, :] gradient_vector):
cdef int N = ligand_nonbonded.shape[0]
cdef int i, j
cdef double dxij, dyij, dzij, dij, dlj, dlj_dij, dlj_dij3, dlj_dij6, dlj_dij12, epsilon, s, s_p
cdef double sol_expfactor_i, sol_expfactor_j
cdef double rep, atr, col, sol, grad_magnititude
for pair in range(N):
# Assuming fist element in pair is i and the other is j
i = ligand_nonbonded[pair, 0]
j = ligand_nonbonded[pair, 1]
dxij = ligand_xyz[j, 0] - ligand_xyz[i, 0]
dyij = ligand_xyz[j, 1] - ligand_xyz[i, 1]
dzij = ligand_xyz[j, 2] - ligand_xyz[i, 2]
dij = dxij * dxij + dyij * dyij + dzij * dzij
dij = sqrt(dij)
#dlj = ligand_dljr[pair]
#eij = ligand_dljep[pair]
# Compute rep gradient magnitude
if dij <= 0.6 * ligand_dljr[pair]:
# constant, replacing dij = 0.6*dljr
dlj_dij = 1/0.6
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
rep = (-12 * ligand_dljep[pair]/(0.6 * ligand_dljr[pair])) * (dlj_dij12 - dlj_dij6) #- (dlj_dij*dij)
elif 0.6 * ligand_dljr[pair] < dij and dij <= ligand_dljr[pair]:
dlj_dij = ligand_dljr[pair]/dij
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
rep = (-12 * ligand_dljep[pair]/dij) * (dlj_dij12 - dlj_dij6)
elif ligand_dljr[pair] < dij:
rep = 0
# Compute atr
if dij <= ligand_dljr[pair]:
atr = ligand_dljep[pair]
elif ligand_dljr[pair] < dij and dij <= 4.5:
dlj_dij = ligand_dljr[pair]/dij
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
atr = (-12 * ligand_dljep[pair]/dij) * (dlj_dij12 - dlj_dij6)
elif 4.5 < dij and dij <= 6.0:
dlj_dij = ligand_dljr[pair]/dij
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
atr = ( ((-12 * ligand_dljep[pair]/dij)) * (dlj_dij12 - dlj_dij6) * switch(dij, 4.5, 6.0)) + \
( ligand_dljep[pair] * (dlj_dij12 - (2 * dlj_dij6)) * switch_gradient(dij, 4.5, 6.0))
elif 6.0 < dij:
atr = 0
# compute electrostatic
epsilon = 6
if dij < 1.45:
col = -ligand_qij[pair] / (1.45 * 1.45 * epsilon)
elif 1.45 <= dij and dij < 1.85:
s = switch(dij, 1.45, 1.85)
s_p = switch_gradient(dij, 1.45, 1.85)
col = ((-ligand_qij[pair]/ (1.45 * 1.45 * epsilon)) * s) + \
((ligand_qij[pair]/ (1.45 * epsilon)) * s_p) + \
((-2 * ligand_qij[pair]/epsilon) * (1 / (dij*dij*dij)) * (1 - s)) + \
((ligand_qij[pair]/epsilon) * ( (1 / (dij*dij)) - (1 / (5.5 * 5.5)) ) * (-s_p))
elif 1.85 <= dij and dij < 4.5:
col = ( (-2 * ligand_qij[pair]/epsilon) * (1 / (dij*dij*dij)) )
elif 4.5 <= dij and dij < 5.5:
col = ( (-2 * ligand_qij[pair]/epsilon) * (1 / (dij*dij*dij)) * s ) + \
( (ligand_qij[pair]/epsilon) * ( (1 / (dij*dij)) - (1 / (5.5*5.5)) ) * switch_gradient(dij, 4.5, 5.5) )
else:
col = 0
# compute solvation
sol = 0
if ligand_weights[3] != 0:
if dij <= ligand_dljr[pair] - 0.3:
sol += -ligand_sol_prefactor[pair, 0]
sol += -ligand_sol_prefactor[pair, 1]
elif ligand_dljr[pair] - 0.3 < dij and dij <= ligand_dljr[pair] + 0.2:
s = switch(dij, ligand_dljr[pair] - 0.3, ligand_dljr[pair] + 0.2)
s_p = switch_gradient(dij, ligand_dljr[pair] - 0.3, ligand_dljr[pair] + 0.2)
sol_expfactor_i = (dij - ligand_dljr[pair]) / ligand_lklam[i]
sol_expfactor_j = (dij - ligand_dljr[pair]) / ligand_lklam[j]
sol += (-ligand_sol_prefactor[pair, 0] * s) + \
(ligand_sol_prefactor[pair, 0] * s_p) + \
((ligand_sol_prefactor[pair, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-2 * (dij - ligand_dljr[pair]) / (ligand_lklam[i] * ligand_lklam[i]))) * (1 - s)) + \
(ligand_sol_prefactor[pair, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-s_p))
sol += (-ligand_sol_prefactor[pair, 1] * s) + \
(ligand_sol_prefactor[pair, 1] * s_p) + \
((ligand_sol_prefactor[pair, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-2 * (dij - ligand_dljr[pair]) / (ligand_lklam[j] * ligand_lklam[j])) )* (1 -s)) + \
(ligand_sol_prefactor[pair, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-s_p))
elif ligand_dljr[pair] + 0.2 < dij and dij <= 4.5:
sol_expfactor_i = (dij - ligand_dljr[pair]) / ligand_lklam[i]
sol_expfactor_j = (dij - ligand_dljr[pair]) / ligand_lklam[j]
sol += ( ligand_sol_prefactor[pair, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-2 * (dij - ligand_dljr[pair]) / (ligand_lklam[i] * ligand_lklam[i])) )
sol += ( ligand_sol_prefactor[pair, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-2 * (dij - ligand_dljr[pair]) / (ligand_lklam[j] * ligand_lklam[j])) )
elif 4.5 < dij and dij <= 6.0:
s = switch(dij, 4.5, 6.0)
s_p = switch_gradient(dij, 4.5, 6.0)
sol_expfactor_i = (dij - ligand_dljr[pair]) / ligand_lklam[i]
sol_expfactor_j = (dij - ligand_dljr[pair]) / ligand_lklam[j]
sol += ( ligand_sol_prefactor[pair, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-2 * (dij - ligand_dljr[pair]) / (ligand_lklam[i] * ligand_lklam[i])) * s) + \
( ligand_sol_prefactor[pair, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * s_p )
sol += ( ligand_sol_prefactor[pair, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-2 * (dij - ligand_dljr[pair]) / (ligand_lklam[j] * ligand_lklam[j])) * s) + \
(ligand_sol_prefactor[pair, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * s_p)
else:
sol = 0
grad_magnititude = ligand_coupling * ligand_nonbondedWeights[pair] * (ligand_weights[0] * rep + ligand_weights[1] * atr + ligand_weights[2] * col + ligand_weights[3] * sol)
# Set forces for atom j
gradient_vector[j, 0] = gradient_vector[j, 0] + ((dxij * grad_magnititude)/dij)
gradient_vector[j, 1] = gradient_vector[j, 1] + ((dyij * grad_magnititude)/dij)
gradient_vector[j, 2] = gradient_vector[j, 2] + ((dzij * grad_magnititude)/dij)
# For atom i we use -dxij
gradient_vector[i, 0] = gradient_vector[i, 0] + ((-dxij * grad_magnititude)/dij)
gradient_vector[i, 1] = gradient_vector[i, 1] + ((-dyij * grad_magnititude)/dij)
gradient_vector[i, 2] = gradient_vector[i, 2] + ((-dzij * grad_magnititude)/dij)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.profile(True)
def ligand_environment_gradient(double[:, :] ligand_xyz,
double[:, :] environment_xyz,
double[:] ligand_mask,
double[:, :] ligand_environment_qij,
double[:, :] ligand_environment_dljr,
double[:, :] ligand_environment_dljep,
double[:, :, :] ligand_environment_sol_prefactor,
double [:] ligand_lklam,
double [:] environmen_lklam,
int [:] environment_sideChainFalgs,
int [:] environment_mainChainFalgs,
double sideChainCoupling,
double mainChainCoupling,
double [:] ligand_environment_weights,
double [:, :] gradient_vector):
cdef int N = ligand_xyz.shape[0]
cdef int M = environment_xyz.shape[0]
cdef int i, j
cdef double dxji, dyji, dzji, dij, dlj, dlj_dij, dlj_dij3, dlj_dij6, dlj_dij12, epsilon, s
cdef double sol_expfactor_i, sol_expfactor_j, coupling
cdef double rep, atr, col, sol, grad_magnititude
for i in range(N):
if ligand_mask[i] == 0:
continue
for j in range(M):
coupling = (environment_sideChainFalgs[j] * sideChainCoupling) + (environment_mainChainFalgs[j] * mainChainCoupling)
if coupling == 0:
continue
# The vector element are computed wrt ligand
dxji = ligand_xyz[i, 0] - environment_xyz[j, 0]
dyji = ligand_xyz[i, 1] - environment_xyz[j, 1]
dzji = ligand_xyz[i, 2] - environment_xyz[j, 2]
dij = dxji * dxji + dxji * dxji + dxji * dxji
dij = sqrt(dij)
# Compute rep gradient magnitude
if dij <= 0.6 * ligand_environment_dljr[i, j]:
# constant, replacing dij = 0.6*dljr
dlj_dij = 1 / 0.6
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
rep = (-12 * ligand_environment_dljep[i, j] / (0.6 * ligand_environment_dljr[i, j])) * (dlj_dij12 - dlj_dij6) # - (dlj_dij*dij)
elif 0.6 * ligand_environment_dljr[i, j] < dij and dij <= ligand_environment_dljr[i, j]:
dlj_dij = ligand_environment_dljr[i, j] / dij
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
rep = (-12 * ligand_environment_dljep[i, j] / dij) * (dlj_dij12 - dlj_dij6)
elif ligand_environment_dljr[i, j] < dij:
rep = 0
# Compute atr
if dij <= ligand_environment_dljr[i, j]:
atr = ligand_environment_dljr[i, j]
elif ligand_environment_dljr[i, j] < dij and dij <= 4.5:
dlj_dij = ligand_environment_dljr[i, j] / dij
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
atr = (-12 * ligand_environment_dljep[i, j]/ dij) * (dlj_dij12 - dlj_dij6)
elif 4.5 < dij and dij <= 6.0:
dlj_dij = ligand_environment_dljr[i, j] / dij
dlj_dij3 = dlj_dij * dlj_dij * dlj_dij
dlj_dij6 = dlj_dij3 * dlj_dij3
dlj_dij12 = dlj_dij6 * dlj_dij6
atr = (((-12 * ligand_environment_dljep[i, j] / dij)) * (dlj_dij12 - dlj_dij6) * switch(dij, 4.5, 6.0)) + \
(ligand_environment_dljep[i, j] * (dlj_dij12 - (2 * dlj_dij6)) * switch_gradient(dij, 4.5, 6.0))
elif 6.0 < dij:
atr = 0
# compute electrostatic
epsilon = 6
if dij < 1.45:
col = -ligand_environment_qij[i, j] / (1.45 * 1.45 * epsilon)
elif 1.45 <= dij and dij < 1.85:
s = switch(dij, 1.45, 1.85)
s_p = switch_gradient(dij, 1.45, 1.85)
col = ((-ligand_environment_qij[i, j] / (1.45 * 1.45 * epsilon)) * s) + \
((ligand_environment_qij[i, j] / (1.45 * epsilon)) * s_p) + \
((-2 * ligand_environment_qij[i, j] / epsilon) * (1 / (dij * dij * dij)) * (1 - s)) + \
((ligand_environment_qij[i, j] / epsilon) * ((1 / (dij * dij)) - (1 / (5.5 * 5.5))) * (-s_p))
elif 1.85 <= dij and dij < 4.5:
col = ((-2 * ligand_environment_qij[i, j] / epsilon) * (1 / (dij * dij * dij)))
elif 4.5 <= dij and dij < 5.5:
col = ((-2 * ligand_environment_qij[i, j] / epsilon) * (1 / (dij * dij * dij)) * s) + \
((ligand_environment_qij[i, j] / epsilon) * ((1 / (dij * dij)) - (1 / (5.5 * 5.5))) * switch_gradient(dij, 4.5, 5.5))
else:
col = 0
# compute solvation
sol = 0
if ligand_environment_weights[3] != 0:
if dij <= ligand_environment_dljr[i, j] - 0.3:
sol += -ligand_environment_sol_prefactor[i, j, 0]
sol += -ligand_environment_sol_prefactor[i, j, 1]
elif ligand_environment_dljr[i, j] - 0.3 < dij and dij <= ligand_environment_dljr[i, j] + 0.2:
s = switch(dij, ligand_environment_dljr[i, j] - 0.3, ligand_environment_dljr[i, j] + 0.2)
s_p = switch_gradient(dij, ligand_environment_dljr[i, j] - 0.3, ligand_environment_dljr[i, j] + 0.2)
sol_expfactor_i = (dij - ligand_environment_dljr[i, j]) / ligand_lklam[i]
sol_expfactor_j = (dij - ligand_environment_dljr[i, j]) / environmen_lklam[j]
sol += (-ligand_environment_sol_prefactor[i, j, 0] * s) + \
(ligand_environment_sol_prefactor[i, j, 0] * s_p) + \
((ligand_environment_sol_prefactor[i, j, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-2 * (dij - ligand_environment_dljr[i, j]) / (ligand_lklam[i] * ligand_lklam[i]))) * (1 - s)) + \
(ligand_environment_sol_prefactor[i, j, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-s_p))
sol += (-ligand_environment_sol_prefactor[i, j, 1] * s) + \
(ligand_environment_sol_prefactor[i, j, 1] * s_p) + \
((ligand_environment_sol_prefactor[i, j, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-2 * (dij - ligand_environment_dljr[i, j]) / (environmen_lklam[j] * environmen_lklam[j]))) * (1 - s)) + \
(ligand_environment_sol_prefactor[i, j, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-s_p))
elif ligand_environment_dljr[i, j] + 0.2 < dij and dij <= 4.5:
sol_expfactor_i = (dij - ligand_environment_dljr[i, j]) / ligand_lklam[i]
sol_expfactor_j = (dij - ligand_environment_dljr[i, j]) / environmen_lklam[j]
sol += (ligand_environment_sol_prefactor[i, j, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-2 * (dij - ligand_environment_dljr[i, j]) / (ligand_lklam[i] * ligand_lklam[i])))
sol += (ligand_environment_sol_prefactor[i, j, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-2 * (dij - ligand_environment_dljr[i, j]) / (environmen_lklam[j] * environmen_lklam[j])))
elif 4.5 < dij and dij <= 6.0:
s = switch(dij, 4.5, 6.0)
s_p = switch_gradient(dij, 4.5, 6.0)
sol_expfactor_i = (dij - ligand_environment_dljr[i, j]) / ligand_lklam[i]
sol_expfactor_j = (dij - ligand_environment_dljr[i, j]) / environmen_lklam[j]
sol += (ligand_environment_sol_prefactor[i, j, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * (-2 * (dij - ligand_environment_dljr[i, j]) / (ligand_lklam[i] * ligand_lklam[i])) * s) + \
(ligand_environment_sol_prefactor[i, j, 0] * exp(-1 * (sol_expfactor_i * sol_expfactor_i)) * s_p)
sol += (ligand_environment_sol_prefactor[i, j, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * (-2 * (dij - ligand_environment_dljr[i, j]) / environmen_lklam[j] * environmen_lklam[j]) * s) + \
(ligand_environment_sol_prefactor[i, j, 1] * exp(-1 * (sol_expfactor_j * sol_expfactor_j)) * s_p)
else:
sol = 0
grad_magnititude = coupling * (ligand_environment_weights[0] * rep + ligand_environment_weights[1] * atr + ligand_environment_weights[2] * col + ligand_environment_weights[3] * sol)
# Set forces for atom i (ligand)
gradient_vector[i, 0] = gradient_vector[i, 0] + ((dxji * grad_magnititude) / dij)
gradient_vector[i, 1] = gradient_vector[i, 1] + ((dyji * grad_magnititude) / dij)
gradient_vector[i, 2] = gradient_vector[i, 2] + ((dzji * grad_magnititude) / dij)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.profile(True)
def bond_gradient(double [:, :] ligand_xyz,
int [:, :] bond_atomIndex,
double [:] equilibrium_distance,
double [:] force_constance,
double [:, :] gradient_vector):
"""
Computes the gradient of bond potential
"""
cdef int i, j, ibond, N = bond_atomIndex.shape[0]
cdef double dxji, dyji, dzji, dji, g
for ibond in range(N):
i = bond_atomIndex[ibond, 0]
j = bond_atomIndex[ibond, 1]
dxji = ligand_xyz[i, 0] - ligand_xyz[j, 0]
dyji = ligand_xyz[i, 1] - ligand_xyz[j, 1]
dzji = ligand_xyz[i, 2] - ligand_xyz[j, 2]
dji = dxji * dxji + dyji * dyji + dzji * dzji
dji = sqrt(dji)
d = dji - equilibrium_distance[ibond]
g = 2 * force_constance[ibond] * d
gradient_vector[i, 0] = gradient_vector[i, 0] + ((dxji * g) / dji)
gradient_vector[i, 1] = gradient_vector[i, 1] + ((dxji * g) / dji)
gradient_vector[i, 2] = gradient_vector[i, 2] + ((dxji * g) / dji)
# For atom j we use -dxji
gradient_vector[j, 0] = gradient_vector[j, 0] + ((-dxji * g) / dji)
gradient_vector[j, 1] = gradient_vector[j, 1] + ((-dxji * g) / dji)
gradient_vector[j, 2] = gradient_vector[j, 2] + ((-dxji * g) / dji)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.profile(True)
def angle_gradient(double [:, :] ligand_xyz,
int [:, :] angle_atomIndex,
double [:] equilibrium_angle,
double [:] force_constance,
double [:, :] gradient_vector):
"""
Computes bond potential
"""
cdef int i, j, iangle, N = angle_atomIndex.shape[0]
cdef double dx, dy, dz, dji, djk, djidjk, djidjk_n, teta, dteta, sn, g
cdef double dji2inv, djk2inv, gxi, gyi, gzi, gxk, gyk, gzk
for iangle in range(N):
i = angle_atomIndex[iangle, 0]
j = angle_atomIndex[iangle, 1]
k = angle_atomIndex[iangle, 2]
dxji = ligand_xyz[i, 0] - ligand_xyz[j, 0]
dyji = ligand_xyz[i, 1] - ligand_xyz[j, 1]
dzji = ligand_xyz[i, 2] - ligand_xyz[j, 2]
dxjk = ligand_xyz[k, 0] - ligand_xyz[j, 0]
dyjk = ligand_xyz[k, 1] - ligand_xyz[j, 1]
dzjk = ligand_xyz[k, 2] - ligand_xyz[j, 2]
dji = dxji * dxji + dyji * dyji + dzji * dzji
dji = sqrt(dji)
dji2inv = 1/(dji*dji)
djk = dxjk * dxjk + dyjk * dyjk + dzjk * dzjk
djk = sqrt(djk)
djk2inv = 1/(djk*djk)
djidjk = dxji * dxjk + dyji * dyjk + dzji * dzjk
djidjk_n = djidjk / (dji * djk)
if djidjk_n > 1.0: djidjk_n = 1
if djidjk_n < -1.0: djdjidjk_nidjk = -1
teta = acos(djidjk_n)
dteta = teta - equilibrium_angle[iangle]
sn = sin(teta)
# To avoid zero division
if sn == 0: sn = 0.00000001
g = (-2 * force_constance[iangle] * dteta) / (sn * dji * djk)
gxi = dxjk - (djidjk * dji2inv * dxji)
gyi = dyjk - (djidjk * dji2inv * dyji)
gzi = dzjk - (djidjk * dji2inv * dzji)
gxk = dxji - (djidjk * dji2inv * dxjk)
gyk = dyji - (djidjk * dji2inv * dyjk)
gzk = dzji - (djidjk * dji2inv * dzjk)
gradient_vector[i, 0] = gradient_vector[i, 0] + (g * gxi)
gradient_vector[i, 1] = gradient_vector[i, 1] + (g * gyi)
gradient_vector[i, 2] = gradient_vector[i, 2] + (g * gzi)
gradient_vector[k, 0] = gradient_vector[k, 0] + (g * gxk)
gradient_vector[k, 1] = gradient_vector[k, 1] + (g * gyk)
gradient_vector[k, 2] = gradient_vector[k, 2] + (g * gzk)
gradient_vector[j, 0] = gradient_vector[j, 0] - (g * (gxi + gxk))
gradient_vector[j, 1] = gradient_vector[j, 1] - (g * (gxi + gxk))
gradient_vector[j, 2] = gradient_vector[j, 2] - (g * (gxi + gxk))
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
@cython.profile(True)
cdef inline double switch_gradient(double x, double min, double max):
cdef double r2, a2, b2_a2
if x < min:
return 1
elif x > max:
return 0
else:
r2 = x * x
a2 = min * min
b2 = max * max
b2_a2 = b2 - a2
return (12*x*(a2 - r2)*(b2 - r2))/(b2_a2*b2_a2*b2_a2)