-
Notifications
You must be signed in to change notification settings - Fork 0
/
pme.py
1602 lines (1439 loc) · 65.4 KB
/
pme.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
#!/usr/bin/env python
import torch
import functorch
from functorch import vmap
from dmff_torch.pairwise import distribute_scalar, distribute_multipoles, distribute_v3
from dmff_torch.utils import jit_condition
from dmff_torch.spatial import generate_construct_local_frames
from dmff_torch.multipole import C1_c2h, rot_local2global
from torch import erf, erfc
from dmff_torch.constants import DIELECTRIC
from dmff_torch.nblist import build_covalent_map
from functools import partial
import numpy as np
from dmff_torch.recip import pme_recip
from typing import Tuple, Optional
from dmff_torch.settings import POL_CONV, MAX_N_POL
# we use torch.autograd.grad to calculate the grad
DEFAULT_THOLE_WIDTH = 5.0
class ADMPPmeForce:
'''
This is a convenient wrapper for multipolar PME calculations
It wrapps all the environment parameters of multipolar PME calculation
The so called "environment paramters" means parameters that do not need to be differentiable
'''
def __init__(self, box, axis_type, axis_indices, rc, ethresh, lmax, lpol=False, lpme=True, steps_pol=None):
'''
Initialize the ADMPPmeForce calculator.
Input:
box:
(3, 3) float, box size in row
axis_type:
(na,) int, types of local axis (bisector, z-then-x etc.)
rc:
float: cutoff distance
ethresh:
float: pme energy threshold
lmax:
int: max L for multipoles
lpol:
bool: polarize or not?
lpme:
bool: do pme or simple cutoff?
if False, the kappa will be set to zero and the reciprocal part will not be computed
steps:
None or int: Whether do fixed number of dipole iteration steps?
if None: converge dipoles until convergence threshold is met
if int: optimize for this many steps and stop, this is useful if you want to jit the entire function
Output:
'''
self.device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
if axis_indices == None:
self.axis_type = None
self.axis_indices = None
else:
self.axis_type = axis_type
self.axis_indices = axis_indices
self.rc = rc
self.ethresh = ethresh
self.lmax = lmax # jichen: type checking
# turn off pme if lpme is False, this is useful when doing cluster calculations
self.lpme = lpme
if self.lpme is False:
self.kappa = torch.tensor(0., dtype=torch.float32, device=self.device)
self.K1 = torch.tensor(0., dtype=torch.float32, device=self.device)
self.K2 = torch.tensor(0., dtype=torch.float32, device=self.device)
self.K3 = torch.tensor(0., dtype=torch.float32, device=self.device)
self.n_mesh = None
self.shifts = None
else:
kappa, K1, K2, K3 = setup_ewald_parameters(rc, ethresh, box)
self.kappa = kappa
self.K1 = K1
self.K2 = K2
self.K3 = K3
#################################################################
# modify here, for the torch.jit purpose
pme_order = 6
bspline_range = torch.arange(-pme_order//2, pme_order//2, dtype=torch.float32, device=self.device)
n_mesh = pme_order**3
shift_y,shift_x,shift_z = torch.meshgrid(bspline_range, bspline_range, bspline_range,indexing='ij')
shifts = torch.stack((shift_x,shift_y,shift_z)).transpose(0,3).reshape((1,n_mesh,3))
self.n_mesh = torch.tensor(n_mesh, dtype=torch.int32, device=self.device)
self.shifts = shifts.to(torch.float32)
################################################################
self.pme_order = 6
self.lpol = lpol
self.steps_pol = steps_pol
self.n_atoms = len(axis_type)
# setup calculators
self.refresh_calculators()
return
def generate_energy(self, positions, box, pairs,
Q_local, pol, tholes, mScales, pScales, dScales, B, ldmp):
# if the force field is not polarizable
if not self.lpol:
return energy_pme(positions, box, pairs,
Q_local, None, None, None,
mScales, None, None,
self.construct_local_frames,
self.kappa, self.K1, self.K2, self.K3, self.lmax, False, self.n_mesh, self.shifts, B, ldmp, lpme=self.lpme)
else:
U_init = torch.zeros((self.n_atoms, 3), dtype=torch.float32, device=positions.device, requires_grad=True)
# this is the wrapper that include a Uind optimize
self.U_ind, lconverg, n_cycle = self.optimize_Uind(
positions, box, pairs, Q_local, pol, tholes,
mScales, pScales, dScales,
U_init=U_init, steps_pol=self.steps_pol)
# here we rely on Feynman-Hellman theorem, drop the term dV/dU*dU/dr !
# self.U_ind = jax.lax.stop_gradient(U_ind)
return energy_pme(positions, box, pairs,
Q_local, self.U_ind, pol, tholes,
mScales, pScales, dScales,
self.construct_local_frames,
self.kappa, self.K1, self.K2, self.K3, self.lmax, True, self.n_mesh, self.shifts, B, ldmp, lpme=self.lpme)
def update_env(self, attr, val):
'''
Update the environment of the calculator
'''
setattr(self, attr, val)
self.refresh_calculators()
def refresh_calculators(self):
'''
refresh the energy and force calculators according to the current environment
'''
if self.lmax > 0:
if self.axis_type == None:
self.construct_local_frames = None
else:
self.construct_local_frames = generate_construct_local_frames(self.axis_type, self.axis_indices)
else:
self.construct_local_frames = None
lmax = self.lmax
# for polarizable monopole force field, need to increase lmax to 1, accomodating induced dipoles
if self.lmax == 0 and self.lpol is True:
lmax = 1
if self.lpol:
self.grad_U_fn = functorch.grad(energy_pme, argnums=(4))
self.get_energy = self.generate_energy
return
def optimize_Uind(self,
positions, box, pairs,
Q_local, pol, tholes, mScales, pScales, dScales,
U_init=None, steps_pol=None):
'''
This function converges the induced dipole
Note that we cut all the gradient chain passing through this function as we assume Feynman-Hellman theorem
Gradients related to Uind should be dropped
'''
# Do not track gradient in Uind optimization
maxiter = 30
thresh = 1.0
positions = positions.detach()
box = box.detach()
Q_local = Q_local.detach()
pol = pol.detach()
tholes = tholes.detach()
mScales = mScales.detach()
pScales = pScales.detach()
dScales = dScales.detach()
if U_init is None:
U = torch.zeros_like(positions, dtype=torch.float32, device=positions.device, requires_grad=True)
else:
U = U_init
if steps_pol is None:
site_filter = (pol>0.001) # focus on the actual polarizable sites
if steps_pol is None:
for i in range(maxiter):
field = self.grad_U_fn(positions, box, pairs, Q_local, U, pol, tholes, mScales, pScales, dScales, self.construct_local_frames, self.kappa, self.K1, self.K2, self.K3, self.lmax, True, self.n_mesh, self.shifts, None, False, lpme=self.lpme)
if torch.max(torch.abs(field[site_filter])) < thresh:
break
U = U - field * pol[:, None] / DIELECTRIC
if i == maxiter-1:
flag = False
else: # converged
flag = True
else:
def update_U(i, U):
field = self.grad_U_fn(positions, box, pairs, Q_local, U, pol, tholes, mScales, pScales, dScales, self.construct_local_frames, self.kappa, self.K1, self.K2, self.K3, self.lmax, True, self.n_mesh, self.shifts, None, False, lpme=self.lpme)
U = U - field * pol[:, None] / DIELECTRIC
return U
# here check the U_ind
#e = self.energy_fn(positions, box, pairs, Q_local, Uind_global, pol, tholes, mScales, pScales, dScales)
#field = self.grad_U_fn(positions, box, pairs, Q_local, Uind_global, pol, tholes, mScales, pScales, dScales)
for ii in range(0, steps_pol):
U = update_U(ii, U)
#U = jax.lax.fori_loop(0, steps_pol, update_U, U)
flag = True
return U, flag, steps_pol
def pme_real(positions, box, pairs,
Q_global, Uind_global, pol, tholes,
mScales, pScales, dScales,
kappa, lmax, lpol, B, ldmp):
'''
This is the real space PME calculate function
NOTE: only deals with permanent-permanent multipole interactions
for jax, it is pointless to jit it
Input:
positions:
Na * 3: positions
box:
3 * 3: box, axes arranged in row
pairs:
Np * 3: interacting pair indices and topology distance
Q_global:
Na * (l+1)**2: harmonics multipoles of each atom, in global frame
Uind_global:
Na * 3: harmonic induced dipoles, in global frame
pol:
(Na,): polarizabilities
tholes:
(Na,): thole damping parameters
mScales:
(Nexcl,): permanent multipole-multipole interaction exclusion scalings: 1-2, 1-3 ...
covalent_map:
Na * Na: topological distances between atoms, if i, j are topologically distant, then covalent_map[i, j] == 0
kappa:
float: kappa in A^-1
lmax:
int: maximum L
lpol:
Bool: whether do a polarizable calculation?
Output:
ene: pme realspace energy
'''
@vmap
@jit_condition()
def regularize_pairs(p):
# using vmap; we view 2-d array with only its element (1-d array, exampe p = p[m]), but dp is same as p[:,0] - p[:,1]
dp = p[1] - p[0]
dp = torch.where(dp > torch.tensor(0, dtype=torch.int32, device=dp.device), torch.tensor(0, dtype=torch.int32, device=dp.device), torch.tensor(1, dtype=torch.int32, device=dp.device))
# vmap don't support .item on a Tensor, for nopbc system, no buffer atoms
#dp_vec = torch.tensor([dp, 2 * dp])
p[0] = p[0] - dp
p[1] = p[1] - dp * 2
return p
@vmap
@jit_condition()
def pair_buffer_scales(p):
dp = p[0] - p[1]
return torch.where(dp < torch.tensor(0, dtype=torch.int32, device=dp.device), torch.tensor(1, dtype=torch.int32, device=dp.device), torch.tensor(0, dtype=torch.int32, device=dp.device))
pairs[:,:2] = regularize_pairs(pairs[:,:2])
buffer_scales = pair_buffer_scales(pairs[:, :2])
box_inv = torch.linalg.inv(box)
r1 = distribute_v3(positions.T, pairs[:,0]).T
r2 = distribute_v3(positions.T, pairs[:,1]).T
Q_extendi = distribute_multipoles(Q_global.T, pairs[:, 0]).T
Q_extendj = distribute_multipoles(Q_global.T, pairs[:, 1]).T
nbonds = pairs[:,2]
indices = (nbonds + (mScales.shape[0] - 1)) % mScales.shape[0]
mscales = distribute_scalar(mScales, indices)
mscales = mscales * buffer_scales
@partial(vmap, in_dims=(0, 0), out_dims=(0))
@jit_condition()
def get_pair_dmp(pol1, pol2):
return (pol1*pol2) ** (1/6)
if ldmp:
B_i = distribute_scalar(B, pairs[:,0])
B_j = distribute_scalar(B, pairs[:,1])
if lpol:
pol1 = distribute_scalar(pol, pairs[:, 0])
pol2 = distribute_scalar(pol, pairs[:, 1])
thole1 = distribute_scalar(tholes, pairs[:, 0])
thole2 = distribute_scalar(tholes, pairs[:, 1])
Uind_extendi = distribute_v3(Uind_global.T, pairs[:, 0]).T
Uind_extendj = distribute_v3(Uind_global.T, pairs[:, 1]).T
pscales = distribute_scalar(pScales, indices)
pscales = pscales * buffer_scales
dscales = distribute_scalar(dScales, indices)
dscales = dscales * buffer_scales
dmp = get_pair_dmp(pol1, pol2)
else:
Uind_extendi = None
Uind_extendj = None
pscales = None
dscales = None
thole1 = None
thole2 = None
dmp = None
@partial(vmap, in_dims=(0, None, None), out_dims=0)
@jit_condition()
def v_pbc_shift(drvecs, box, box_inv):
unshifted_dsvecs = torch.matmul(drvecs, box_inv)
dsvecs = unshifted_dsvecs - torch.floor(unshifted_dsvecs + 0.5)
return torch.matmul(dsvecs, box)
@partial(vmap, in_dims=(0, 0, 0, 0), out_dims=0)
@jit_condition()
def build_quasi_internal(r1, r2, dr, norm_dr, bias_0 = torch.tensor([1., 0., 0.],dtype=torch.float32, device='cuda:0' if torch.cuda.is_available() else 'cpu'), bias_1 = torch.tensor([0., 1., 0.], dtype=torch.float32, device='cuda:0' if torch.cuda.is_available() else 'cpu')):
'''
Build the quasi-internal frame between a pair of sites
In this frame, the z-axis is pointing from r2 to r1
Input:
r1:
N * 3, positions of the first vector
r2:
N * 3, positions of the second vector
dr:
N * 3, vector pointing from r1 to r2
norm_dr:
(N,), distances between r1 and r2
Output:
local_frames:
N * 3 * 3: local frames, three axes arranged in rows
'''
vectorZ = dr/norm_dr
vectorX = torch.where(torch.logical_or(r1[1]!=r2[1],r1[2]!=r2[2]),vectorZ + bias_0, vectorZ + bias_1)
dot_xz = torch.matmul(vectorZ, vectorX)
vectorX = vectorX - vectorZ * dot_xz
vectorX = vectorX / torch.norm(vectorX)
vectorY = torch.cross(vectorZ,vectorX)
return torch.stack([vectorX, vectorY, vectorZ])
@partial(vmap, in_dims=(0, 0), out_dims=0)
@jit_condition()
def rot_ind_global2local(U_g, localframes, zxy = torch.tensor([2,0,1], dtype=torch.long, device='cuda:0' if torch.cuda.is_available() else 'cpu')):
'''
A special rotation function for just dipoles, aim for applying on induced dipoles
'''
R1 = localframes[zxy][:,zxy]
U_l = torch.matmul(R1, U_g)
return U_l
@partial(vmap, in_dims=(0, 0, 0, 0, 0, 0, 0, None), out_dims=0)
@jit_condition()
def rot_global2local(Qi_0, Qi_1, Qi_2, Qj_0, Qj_1, Qj_2, localframes, lmax, rt3=torch.tensor(1.73205080757,dtype=torch.float32, device='cuda:0' if torch.cuda.is_available() else 'cpu'), zxy=torch.tensor([2,0,1], dtype=torch.long, device='cuda:0' if torch.cuda.is_available() else 'cpu')):
'''
This function rotates harmonic moments Q from global frame to local frame
Input:
Q_gh:
n * (l+1)^2, stores the global harmonic multipole moments of each site
localframes:
n * 3 * 3, stores the Rotation matrix for each site, the R is defined as:
[r1, r2, r3]^T, with r1, r2, r3 being the local frame axes
lmax:
integer, the maximum multipole order
C2_gl: the local rotate matrix when lmax=2
zxy: the harmonic transform matrix when lmax=1
Output:
Q_lh:
n * (l+1)^2, stores the local harmonic multipole moments
'''
#rt3 = 1.73205080757
inv_rt3 = 1.0/rt3
# monopole
if lmax < 1:
Qi_lh = Qi_0
Qj_lh = Qj_0
# dipole
elif lmax < 2:
# the rotation matrix
#zxy = [2,0,1]
R1 = localframes[zxy][:,zxy]
# rotate
Qi_lh_1 = torch.matmul(R1, Qi_1)
Qj_lh_1 = torch.matmul(R1, Qj_1)
Qi_lh = torch.hstack([Qi_0, Qi_lh_1])
Qj_lh = torch.hstack([Qj_0, Qj_lh_1])
else:
# the rotation matrix
#zxy = [2,0,1]
R1 = localframes[zxy][:,zxy]
# rotate
Qi_lh_1 = torch.matmul(R1, Qi_1)
Qj_lh_1 = torch.matmul(R1, Qj_1)
xx = localframes[0, 0]
xy = localframes[0, 1]
xz = localframes[0, 2]
yx = localframes[1, 0]
yy = localframes[1, 1]
yz = localframes[1, 2]
zx = localframes[2, 0]
zy = localframes[2, 1]
zz = localframes[2, 2]
# construct the local->global transformation matrix
# this is copied directly from the convert_mom_to_xml.py code
C2_gl_00 = (3*zz**2-1)/2
C2_gl_01 = rt3*zx*zz
C2_gl_02 = rt3*zy*zz
C2_gl_03 = (rt3*(-2*zy**2-zz**2+1))/2
C2_gl_04 = rt3*zx*zy
C2_gl_10 = rt3*xz*zz
C2_gl_11 = 2*xx*zz-yy
C2_gl_12 = yx+2*xy*zz
C2_gl_13 = -2*xy*zy-xz*zz
C2_gl_14 = xx*zy+zx*xy
C2_gl_20 = rt3*yz*zz
C2_gl_21 = 2*yx*zz+xy
C2_gl_22 = -xx+2*yy*zz
C2_gl_23 = -2*yy*zy-yz*zz
C2_gl_24 = yx*zy+zx*yy
C2_gl_30 = rt3*(-2*yz**2-zz**2+1)/2
C2_gl_31 = -2*yx*yz-zx*zz
C2_gl_32 = -2*yy*yz-zy*zz
C2_gl_33 = (4*yy**2+2*zy**2+2*yz**2+zz**2-3)/2
C2_gl_34 = -2*yx*yy-zx*zy
C2_gl_40 = rt3*xz*yz
C2_gl_41 = xx*yz+yx*xz
C2_gl_42 = xy*yz+yy*xz
C2_gl_43 = -2*xy*yy-xz*yz
C2_gl_44 = xx*yy+yx*xy
C2_gl = torch.stack(
(
torch.stack((C2_gl_00, C2_gl_10, C2_gl_20, C2_gl_30, C2_gl_40)),
torch.stack((C2_gl_01, C2_gl_11, C2_gl_21, C2_gl_31, C2_gl_41)),
torch.stack((C2_gl_02, C2_gl_12, C2_gl_22, C2_gl_32, C2_gl_42)),
torch.stack((C2_gl_03, C2_gl_13, C2_gl_23, C2_gl_33, C2_gl_43)),
torch.stack((C2_gl_04, C2_gl_14, C2_gl_24, C2_gl_34, C2_gl_44))
)
)
C2_gl = torch.transpose(C2_gl,0,1)
Qi_lh_2 = torch.einsum('jk,k->j', C2_gl, Qi_2)
Qj_lh_2 = torch.einsum('jk,k->j', C2_gl, Qj_2)
Qi_lh = torch.hstack([Qi_0, Qi_lh_1, Qi_lh_2])
Qj_lh = torch.hstack([Qj_0, Qj_lh_1, Qj_lh_2])
return Qi_lh, Qj_lh
# deals with geometries
dr = r1 - r2
dr = v_pbc_shift(dr, box, box_inv)
norm_dr = torch.linalg.norm(dr, dim=-1)
Ri = build_quasi_internal(r1, r2, dr, norm_dr.unsqueeze(-1))
########################################################################
Q_0i = Q_extendi[:,0:1]; Q_1i = Q_extendi[:,1:4]; Q_2i = Q_extendi[:,4:9]
Q_0j = Q_extendj[:,0:1]; Q_1j = Q_extendj[:,1:4]; Q_2j = Q_extendj[:,4:9]
qiQI, qiQJ = rot_global2local(Q_0i, Q_1i, Q_2i, Q_0j, Q_1j, Q_2j, Ri, lmax)
#qiQJ = rot_global2local(Q_0j, Q_1j, Q_2j, Ri, lmax)
########################################################################
if lpol:
qiUindI = rot_ind_global2local(Uind_extendi, Ri)
qiUindJ = rot_ind_global2local(Uind_extendj, Ri)
else:
qiUindI = None
qiUindJ = None
# everything should be pair-specific now
if lpol:
if ldmp :
ene = torch.sum(
pme_dmp_kernel(
norm_dr,
Q_0i,
Q_0j,
B_i,
B_j,
mscales) * buffer_scales) + torch.sum(
pme_real_kernel_pol(
norm_dr,
qiQI,
qiQJ,
qiUindI,
qiUindJ,
thole1,
thole2,
dmp,
mscales,
pscales,
dscales,
kappa,
lmax,
lpol
) * buffer_scales)
else:
ene = torch.sum(
pme_real_kernel_pol(
norm_dr,
qiQI,
qiQJ,
qiUindI,
qiUindJ,
thole1,
thole2,
dmp,
mscales,
pscales,
dscales,
kappa,
lmax,
lpol
) * buffer_scales
)
else:
ene = torch.sum(
pme_real_kernel_nopol(
norm_dr,
qiQI,
qiQJ,
qiUindI,
qiUindJ,
thole1,
thole2,
dmp,
mscales,
pscales,
dscales,
kappa,
lmax,
lpol
) * buffer_scales
)
return ene
@vmap
#@jit_condition()
def pme_dmp_kernel(dr, Qi, Qj, Bi, Bj, m):
DIELECTRIC = torch.tensor(1389.35455846, dtype=torch.float32, device=dr.device)
b = torch.sqrt(Bi * Bj); q = Qi[0] * Qj[0]
br = b * dr
exp_br = torch.exp(-br)
#expdmp = torch.where(dr < torch.tensor(2.5, dtype=torch.float32, device=dr.device), torch.tensor(0., dtype=torch.float32, device=dr.device), torch.exp(-(dr-torch.tensor(2.5, dtype=torch.float32, device=dr.device))**3))
e_tot = - exp_br * (1 + br) * q / dr * DIELECTRIC #* expdmp
return e_tot * m
@partial(vmap, in_dims=(0, 0, 0, None, None, None, None, None, 0, None, None, None, None, None), out_dims=0)
def pme_real_kernel_nopol(dr, qiQI, qiQJ, qiUindI, qiUindJ, thole1, thole2, dmp, mscales, pscales, dscales, kappa, lmax, lpol=False):
'''
This is the heavy-lifting kernel function to compute the realspace multipolar PME
Vectorized over interacting pairs
Input:
dr:
float, the interatomic distances, (np) array if vectorized
qiQI:
[(lmax+1)^2] float array, the harmonic multipoles of site i in quasi-internal frame
qiQJ:
[(lmax+1)^2] float array, the harmonic multipoles of site j in quasi-internal frame
qiUindI
(3,) float array, the harmonic dipoles of site i in QI frame
qiUindJ
(3,) float array, the harmonic dipoles of site j in QI frame
thole1
float: thole damping coeff of site i
thole2
float: thole damping coeff of site j
dmp:
float: (pol1 * pol2)**1/6, distance rescaling params used in thole damping
mscale:
float, scaling factor between interacting sites (permanent-permanent)
pscale:
float, scaling factor between perm-ind interaction
dscale:
float, scaling factor between ind-ind interaction
kappa:
float, kappa in unit A^1
lmax:
int, maximum angular momentum
lpol:
bool, doing polarization?
Output:
energy:
'''
# as we using vmap, thus, the tensor is from 2D to 1D
@jit_condition()
def calc_e_perm(dr, mscales, kappa, lmax):
r'''
This function calculates the ePermCoefs at once
ePermCoefs is basically the interaction tensor between permanent multipole components
Everything should be done in the so called quasi-internal (qi) frame
Energy = \sum_ij qiQI * ePermCoeff_ij * qiQJ
Inputs:
dr:
float: distance between one pair of particles
mscales:
float: scaling factor between permanent - permanent multipole interactions, for each pair
kappa:
float: \kappa in PME, unit in A^-1
lmax:
int: max L
Output:
cc, cd, dd_m0, dd_m1, cq, dq_m0, dq_m1, qq_m0, qq_m1, qq_m2:
n * 1 array: ePermCoefs
'''
# torch.Tensor can not combine with vmap
DIELECTRIC = torch.tensor(1389.35455846, dtype=torch.float32, device=dr.device)
rInv = 1 / dr
rInvVec = [DIELECTRIC*(rInv**i) for i in range(0, 9)]
alphaRVec = [(kappa*dr)**i for i in range(0, 10)]
X = 2 * torch.exp(-alphaRVec[2]) / torch.sqrt(torch.tensor(torch.pi, dtype=torch.float32, device=dr.device))
tmp = alphaRVec[1]
doubleFactorial = 1
facCount = 1
erfAlphaR = erf(alphaRVec[1])
# the calc_kernel using the pmap, dr is a value
bVec = [torch.zeros_like(erfAlphaR), -erfAlphaR]
#bVec = torch.empty(6)
#bVec[1] = -erfAlphaR
for i in range(2, 6):
bVec.append(bVec[i-1] + (tmp*X/doubleFactorial))
facCount = facCount + 2
doubleFactorial = doubleFactorial * facCount
tmp = tmp * 2 * alphaRVec[2]
# in pme we need add erfc function in 1/rij
cc = rInvVec[1] * (mscales + bVec[2] - alphaRVec[1]*X)
if lmax >= 1:
# C-D
cd = rInvVec[2] * (mscales + bVec[2])
# D-D: 2
dd_m0 = -2/3 * rInvVec[3] * (3*(mscales + bVec[3]) + alphaRVec[3]*X)
dd_m1 = rInvVec[3] * (mscales + bVec[3] - (2/3)*alphaRVec[3]*X)
else:
cd = torch.tensor(0., dtype=torch.float32, device=dr.device)
dd_m0 = torch.tensor(0., dtype=torch.float32, device=dr.device)
dd_m1 = torch.tensor(0., dtype=torch.float32, device=dr.device)
if lmax >= 2:
## C-Q: 1
cq = (mscales + bVec[3]) * rInvVec[3]
## D-Q: 2
dq_m0 = rInvVec[4] * (3* (mscales + bVec[3]) + (4/3) * alphaRVec[5]*X)
dq_m1 = -torch.sqrt(torch.tensor(3., dtype=torch.float32, device=dr.device)) * rInvVec[4] * (mscales + bVec[3])
## Q-Q
qq_m0 = rInvVec[5] * (6* (mscales + bVec[4]) + (4/45)* (-3 + 10*alphaRVec[2]) * alphaRVec[5]*X)
qq_m1 = - (4/15) * rInvVec[5] * (15*(mscales+bVec[4]) + alphaRVec[5]*X)
qq_m2 = rInvVec[5] * (mscales + bVec[4] - (4/15)*alphaRVec[5]*X)
else:
cq = torch.tensor(0., dtype=torch.float32, device=dr.device)
dq_m0 = torch.tensor(0., dtype=torch.float32, device=dr.device)
dq_m1 = torch.tensor(0., dtype=torch.float32, device=dr.device)
qq_m0 = torch.tensor(0., dtype=torch.float32, device=dr.device)
qq_m1 = torch.tensor(0., dtype=torch.float32, device=dr.device)
qq_m1 = torch.tensor(0., dtype=torch.float32, device=dr.device)
qq_m2 = torch.tensor(0., dtype=torch.float32, device=dr.device)
######################################################################
# add the damping function here !!!!
######################################################################
#expdmp = torch.where(dr < torch.tensor(2.5, dtype=torch.float32, device=dr.device), torch.tensor(0., dtype=torch.float32, device=dr.device), torch.exp(-(dr-torch.tensor(2.5, dtype=torch.float32, device=dr.device))**3))
expdmp = torch.tensor(1., dtype=torch.float32, device=dr.device)
return cc*expdmp, cd*expdmp, dd_m0*expdmp, dd_m1*expdmp, cq*expdmp, dq_m0*expdmp, dq_m1*expdmp, qq_m0*expdmp, qq_m1*expdmp, qq_m2*expdmp
cc, cd, dd_m0, dd_m1, cq, dq_m0, dq_m1, qq_m0, qq_m1, qq_m2 = calc_e_perm(dr, mscales, kappa, lmax)
@jit_condition()
def trim_val_0(x):
return torch.where(x < torch.tensor(1e-8, dtype=torch.float32, device=x.device), torch.tensor(1e-8, dtype=torch.float32, device=x.device), x)
@jit_condition()
def trim_val_infty(x):
return torch.where(x > torch.tensor(1e8, dtype=torch.float32, device=x.device), torch.tensor(1e8, dtype=torch.float32, device=x.device), x)
@jit_condition()
def calc_e_ind(dr, thole1, thole2, dmp, pscales, dscales, kappa, lmax):
'''
This function calculates the eUindCoefs at once
## compute the Thole damping factors for energies
eUindCoefs is basically the interaction tensor between permanent multipole components and induced dipoles
Everything should be done in the so called quasi-internal (qi) frame
Inputs:
dr:
float: distance between one pair of particles
dmp
float: damping factors between one pair of particles
mscales:
float: scaling factor between permanent - permanent multipole interactions, for each pair
pscales:
float: scaling factor between permanent - induced multipole interactions, for each pair
au:
float: for damping factors
kappa:
float: \kappa in PME, unit in A^-1
lmax:
int: max L
Output:
Interaction tensors components
'''
DEFAULT_THOLE_WIDTH = 5.0
## pscale == 0 ? thole1 + thole2 : DEFAULT_THOLE_WIDTH`
w = torch.heaviside(pscales, torch.tensor(0., dtype=torch.float32, device=dr.device))
a = w * DEFAULT_THOLE_WIDTH + (1-w) * (thole1+thole2)
dmp = trim_val_0(dmp)
u = trim_val_infty(dr/dmp)
## au <= 50 aupi = au ;au> 50 aupi = 50
au = a * u
expau = torch.where(au < torch.tensor(50.0, dtype=torch.float32, device=dr.device), torch.exp(-au), torch.tensor(0.,dtype=torch.float32, device=dr.device))
## compute the Thole damping factors for energies
au2 = trim_val_infty(au*au)
au3 = trim_val_infty(au2*au)
au4 = trim_val_infty(au3*au)
au5 = trim_val_infty(au4*au)
au6 = trim_val_infty(au5*au)
## Thole damping factors for energies
thole_c = 1.0 - expau*(1.0 + au + 0.5*au2)
thole_d0 = 1.0 - expau*(1.0 + au + 0.5*au2 + au3/4.0)
thole_d1 = 1.0 - expau*(1.0 + au + 0.5*au2)
thole_q0 = 1.0 - expau*(1.0 + au + 0.5*au2 + au3/6.0 + au4/18.0)
thole_q1 = 1.0 - expau*(1.0 + au + 0.5*au2 + au3/6.0)
rInv = 1. / dr
rInvVec = [DIELECTRIC*(rInv**i) for i in range(0, 9)]
alphaRVec = [(kappa*dr)**i for i in range(0, 10)]
X = 2. * torch.exp(-alphaRVec[2]) / torch.sqrt(torch.tensor(torch.pi))
tmp = alphaRVec[1]
doubleFactorial = 1
facCount = 1
erfAlphaR = erf(alphaRVec[1])
# the calc_kernel using the pmap, dr is a value
bVec = [torch.zeros_like(erfAlphaR), -erfAlphaR]
#bVec = torch.empty(6)
#bVec[1] = -erfAlphaR
for i in range(2, 6):
bVec.append(bVec[i-1] + (tmp*X/doubleFactorial))
facCount = facCount + 2.0
doubleFactorial = doubleFactorial * facCount
tmp = tmp * 2.0 * alphaRVec[2]
## C-Uind
cud = 2.0*rInvVec[2]*(pscales*thole_c + bVec[2])
if lmax >= 1:
## D-Uind terms
dud_m0 = -2.0*2.0/3.0*rInvVec[3]*(3.0*(pscales*thole_d0 + bVec[3]) + alphaRVec[3]*X)
dud_m1 = 2.0*rInvVec[3]*(pscales*thole_d1 + bVec[3] - 2.0/3.0*alphaRVec[3]*X)
else:
dud_m0 = torch.tensor(0.0, dtype=torch.float32, device=dr.device)
dud_m1 = torch.tensor(0.0, dtype=torch.float32, device=dr.device)
if lmax >= 2:
udq_m0 = 2.0*rInvVec[4]*(3.0*(pscales*thole_q0 + bVec[3]) + 4/3*alphaRVec[5]*X)
udq_m1 = -2.0*torch.sqrt(torch.tensor(3))*rInvVec[4]*(pscales*thole_q1 + bVec[3])
else:
udq_m0 = torch.tensor(0.0, dtype=torch.float32, device=dr.device)
udq_m1 = torch.tensor(0.0, dtype=torch.float32, device=dr.device)
## Uind-Uind
udud_m0 = -2.0/3.0*rInvVec[3]*(3.0*(dscales*thole_d0 + bVec[3]) + alphaRVec[3]*X)
udud_m1 = rInvVec[3]*(dscales*thole_d1 + bVec[3] - 2.0/3.0*alphaRVec[3]*X)
#expdmp = torch.where(dr < torch.tensor(2.5, dtype=torch.float32, device=dr.device), torch.tensor(0., dtype=torch.float32, device=dr.device), torch.exp(-(dr-torch.tensor(2.5, dtype=torch.float32, device=dr.device))**3))
return cud, dud_m0, dud_m1, udq_m0, udq_m1, udud_m0, udud_m1
if lpol:
cud, dud_m0, dud_m1, udq_m0, udq_m1, udud_m0, udud_m1 = calc_e_ind(dr, thole1, thole2, dmp, pscales, dscales, kappa, lmax)
@jit_condition()
def calc_e_tot(qiQI, qiQJ, qiUindI, qiUindJ, lmax, lpol, cc, cd, dd_m0, dd_m1, cq, dq_m0, dq_m1, qq_m0, qq_m1, qq_m2, cud, dud_m0, dud_m1, udq_m0, udq_m1, udud_m0, udud_m1):
Vij0 = cc*qiQI[0]
Vji0 = cc*qiQJ[0]
# C-Uind
if lpol > 0:
Vij0 -= cud * qiUindI[0]
Vji0 += cud * qiUindJ[0]
if lmax >= 1:
# C-D
Vij0 = Vij0 - cd*qiQI[1]
Vji1 = -cd*qiQJ[0]
Vij1 = cd*qiQI[0]
Vji0 = Vji0 + cd*qiQJ[1]
# D-D m0
Vij1 += dd_m0 * qiQI[1]
Vji1 += dd_m0 * qiQJ[1]
# D-D m1
Vij2 = dd_m1*qiQI[2]
Vji2 = dd_m1*qiQJ[2]
Vij3 = dd_m1*qiQI[3]
Vji3 = dd_m1*qiQJ[3]
# D-Uind
if lpol > 0:
Vij1 += dud_m0 * qiUindI[0]
Vji1 += dud_m0 * qiUindJ[0]
Vij2 += dud_m1 * qiUindI[1]
Vji2 += dud_m1 * qiUindJ[1]
Vij3 += dud_m1 * qiUindI[2]
Vji3 += dud_m1 * qiUindJ[2]
else:
Vij0 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji1 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij1 = torch.tensor(0., dtype=torch.float32, device=cc.device)
# D-D m1
Vij2 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji2 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij3 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji3 = torch.tensor(0., dtype=torch.float32, device=cc.device)
if lmax >= 2:
# C-Q
Vij0 = Vij0 + cq*qiQI[4]
Vji4 = cq*qiQJ[0]
Vij4 = cq*qiQI[0]
Vji0 = Vji0 + cq*qiQJ[4]
# D-Q m0
Vij1 += dq_m0*qiQI[4]
Vji4 += dq_m0*qiQJ[1]
# Q-D m0
Vij4 -= dq_m0*qiQI[1]
Vji1 -= dq_m0*qiQJ[4]
# D-Q m1
Vij2 = Vij2 + dq_m1*qiQI[5]
Vji5 = dq_m1*qiQJ[2]
Vij3 += dq_m1*qiQI[6]
Vji6 = dq_m1*qiQJ[3]
Vij5 = -(dq_m1*qiQI[2])
Vji2 += -(dq_m1*qiQJ[5])
Vij6 = -(dq_m1*qiQI[3])
Vji3 += -(dq_m1*qiQJ[6])
# Q-Q m0
Vij4 += qq_m0*qiQI[4]
Vji4 += qq_m0*qiQJ[4]
# Q-Q m1
Vij5 += qq_m1*qiQI[5]
Vji5 += qq_m1*qiQJ[5]
Vij6 += qq_m1*qiQI[6]
Vji6 += qq_m1*qiQJ[6]
# Q-Q m2
Vij7 = qq_m2*qiQI[7]
Vji7 = qq_m2*qiQJ[7]
Vij8 = qq_m2*qiQI[8]
Vji8 = qq_m2*qiQJ[8]
# Q-Uind
if lpol > 0:
Vji4 += udq_m0*qiUindJ[0]
Vij4 -= udq_m0*qiUindI[0]
Vji5 += udq_m1*qiUindJ[1]
Vji6 += udq_m1*qiUindJ[2]
Vij5 -= udq_m1*qiUindI[1]
Vij6 -= udq_m1*qiUindI[2]
else:
# C-Q
Vji4 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij4 = torch.tensor(0., dtype=torch.float32, device=cc.device)
# D-Q m1
Vji5 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji6 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij5 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij6 = torch.tensor(0., dtype=torch.float32, device=cc.device)
# Q-Q m2
Vij7 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji7 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij8 = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji8 = torch.tensor(0., dtype=torch.float32, device=cc.device)
# Uind - Uind
if lpol > 0:
Vij1dd = udud_m0 * qiUindI[0]
Vji1dd = udud_m0 * qiUindJ[0]
Vij2dd = udud_m1 * qiUindI[1]
Vji2dd = udud_m1 * qiUindJ[1]
Vij3dd = udud_m1 * qiUindI[2]
Vji3dd = udud_m1 * qiUindJ[2]
Vijdd = torch.stack(( Vij1dd, Vij2dd, Vij3dd))
Vjidd = torch.stack(( Vji1dd, Vji2dd, Vji3dd))
else:
Vij1dd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji1dd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij2dd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji2dd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vij3dd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vji3dd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vijdd = torch.tensor(0., dtype=torch.float32, device=cc.device)
Vjidd = torch.tensor(0., dtype=torch.float32, device=cc.device)
if lmax == 0:
Vij = Vij0
Vji = Vji0
elif lmax == 1:
Vij = torch.stack((Vij0, Vij1, Vij2, Vij3))
Vji = torch.stack((Vji0, Vji1, Vji2, Vji3))
elif lmax == 2:
Vij = torch.stack((Vij0, Vij1, Vij2, Vij3, Vij4, Vij5, Vij6, Vij7, Vij8))
Vji = torch.stack((Vji0, Vji1, Vji2, Vji3, Vji4, Vji5, Vji6, Vji7, Vji8))
else:
raise ValueError(f"Invalid lmax {lmax}. Valid values are 0, 1, 2")
if lpol > 0:
return 0.5 * (torch.sum(qiQJ*Vij) + torch.sum(qiQI*Vji)) + 0.5 * (torch.sum(qiUindJ*Vijdd) + torch.sum(qiUindI*Vjidd))
else:
return 0.5 * (torch.sum(qiQJ*Vij) + torch.sum(qiQI*Vji))
if lpol == True:
e_tot = calc_e_tot(qiQI, qiQJ, qiUindI, qiUindJ, lmax, torch.tensor(1., dtype=torch.float32, device=cc.device), cc, cd, dd_m0, dd_m1, cq, dq_m0, dq_m1, qq_m0, qq_m1, qq_m2, cud, dud_m0, dud_m1, udq_m0, udq_m1, udud_m0, udud_m1)
else:
e_tot = calc_e_tot(qiQI, qiQJ, qiUindI, qiUindJ, lmax, torch.tensor(0., dtype=torch.float32, device=cc.device), cc, cd, dd_m0, dd_m1, cq, dq_m0, dq_m1, qq_m0, qq_m1, qq_m2, None, None, None, None, None, None, None)
return e_tot
@partial(vmap, in_dims=(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, None, None, None), out_dims=0)
def pme_real_kernel_pol(dr, qiQI, qiQJ, qiUindI, qiUindJ, thole1, thole2, dmp, mscales, pscales, dscales, kappa, lmax, lpol=False):
'''
This is the heavy-lifting kernel function to compute the realspace multipolar PME
Vectorized over interacting pairs
Input:
dr:
float, the interatomic distances, (np) array if vectorized
qiQI:
[(lmax+1)^2] float array, the harmonic multipoles of site i in quasi-internal frame
qiQJ:
[(lmax+1)^2] float array, the harmonic multipoles of site j in quasi-internal frame
qiUindI
(3,) float array, the harmonic dipoles of site i in QI frame
qiUindJ
(3,) float array, the harmonic dipoles of site j in QI frame
thole1
float: thole damping coeff of site i
thole2
float: thole damping coeff of site j
dmp:
float: (pol1 * pol2)**1/6, distance rescaling params used in thole damping
mscale:
float, scaling factor between interacting sites (permanent-permanent)
pscale:
float, scaling factor between perm-ind interaction
dscale:
float, scaling factor between ind-ind interaction
kappa:
float, kappa in unit A^1
lmax:
int, maximum angular momentum
lpol:
bool, doing polarization?
Output:
energy:
'''
# as we using vmap, thus, the tensor is from 2D to 1D
@jit_condition()
def calc_e_perm(dr, mscales, kappa, lmax):
r'''
This function calculates the ePermCoefs at once
ePermCoefs is basically the interaction tensor between permanent multipole components
Everything should be done in the so called quasi-internal (qi) frame
Energy = \sum_ij qiQI * ePermCoeff_ij * qiQJ
Inputs:
dr:
float: distance between one pair of particles
mscales:
float: scaling factor between permanent - permanent multipole interactions, for each pair
kappa:
float: \kappa in PME, unit in A^-1
lmax:
int: max L
Output:
cc, cd, dd_m0, dd_m1, cq, dq_m0, dq_m1, qq_m0, qq_m1, qq_m2:
n * 1 array: ePermCoefs
'''
# torch.Tensor can not combine with vmap
DIELECTRIC = torch.tensor(1389.35455846, dtype=torch.float32, device=dr.device)