-
Notifications
You must be signed in to change notification settings - Fork 2
/
gfnff_engrad.f90
3162 lines (2776 loc) · 94.4 KB
/
gfnff_engrad.f90
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
!================================================================================!
! This file is part of gfnff.
!
! Copyright (C) 2023 Philipp Pracht
!
! gfnff is free software: you can redistribute it and/or modify it under
! the terms of the GNU Lesser General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! gfnff is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License
! along with gfnff. If not, see <https://www.gnu.org/licenses/>.
!--------------------------------------------------------------------------------!
!> The original (unmodified) source code can be found under the GNU LGPL 3.0 license
!> Copyright (C) 2019-2020 Sebastian Ehlert, Sebastian Spicher, Stefan Grimme
!> at https://github.com/grimme-lab/xtb
!================================================================================!
module gfnff_engrad_module
use iso_fortran_env,only:wp => real64,sp => real32,stdout => output_unit
use gfnff_ini2
use gfnff_data_types,only:TGFFData,TGFFNeighbourList,new,TGFFTopology
use gfnff_gbsa,only:TBorn
use gfnff_param,only:sqrtZr4r2
use gfnff_helpers
use gfnff_math_wrapper
implicit none
private
public :: gfnff_eg,gfnff_results
type :: gfnff_results
real(wp) :: e_total = 0.0_wp
real(wp) :: e_rep = 0.0_wp
real(wp) :: e_es = 0.0_wp
real(wp) :: e_disp = 0.0_wp
real(wp) :: e_xb = 0.0_wp
real(wp) :: g_born = 0.0_wp
real(wp) :: g_sasa = 0.0_wp
real(wp) :: g_hb = 0.0_wp
real(wp) :: g_shift = 0.0_wp
real(wp) :: dipole(3) = (/0.0_wp,0.0_wp,0.0_wp/)
real(wp) :: g_solv = 0.0_wp
real(wp) :: gnorm = 0.0_wp
real(wp) :: e_bond = 0.0_wp
real(wp) :: e_angl = 0.0_wp
real(wp) :: e_tors = 0.0_wp
real(wp) :: e_hb = 0.0_wp
real(wp) :: e_batm = 0.0_wp
real(wp) :: e_ext = 0.0_wp
end type gfnff_results
real(wp),private,parameter :: pi = 3.1415926535897932385_wp
real(wp),private,parameter :: sqrtpi = 1.77245385091_wp
!========================================================================================!
!========================================================================================!
contains !> MODULE PROCEDURES START HERE
!========================================================================================!
!========================================================================================!
!---------------------------------------------------
!> GFN-FF
!> energy and analytical gradient for given xyz and
!> charge ichrg
!> requires D3 ini (rcov,r2r4,copyc6) as well as
!> gfnff_ini call
!>
!> the total energy is
!> ees + edisp + erep + ebond + eangl + etors + ehb + exb + ebatm + eext
!>
!> uses EEQ charge and D3 routines
!> basic trigonometry for bending and torsion angles
!> taken slightly modified from QMDFF code
!> repulsion and rabguess from xtb GFN0 part
!>
!> requires setup of
!> integer,allocatable :: blist(:,:)
!> integer,allocatable :: alist(:,:)
!> integer,allocatable :: tlist(:,:)
!> integer,allocatable ::b3list(:,:)
!> real(wp),allocatable:: vbond(:,:)
!> real(wp),allocatable:: vangl(:,:)
!> real(wp),allocatable:: vtors(:,:)
!> chi,gam,alp,cnf
!> repa,repz,alphanb
!>
!---------------------------------------------------
subroutine gfnff_eg(pr,n,ichrg,at,xyz,makeq,g,etot,res_gff, &
& param,topo,nlist,solvation,update,version,accuracy,io)
use gfnff_param,only:efield,gffVersion,gfnff_thresholds
use gfnff_gdisp0
use gfnff_cn
use gfnff_rab
implicit none
character(len=*),parameter :: source = 'gfnff_eg'
type(gfnff_results),intent(out) :: res_gff
type(TGFFData),intent(in) :: param
type(TGFFTopology),intent(in) :: topo
type(TGFFNeighbourList),intent(inout) :: nlist
type(TBorn),allocatable,intent(inout) :: solvation
logical,intent(in) :: update
integer,intent(in) :: version
real(wp),intent(in) :: accuracy
integer,intent(out) :: io
integer,intent(in) :: n
integer,intent(in) :: ichrg
integer,intent(in) :: at(n)
real(wp),intent(in) :: xyz(3,n)
real(wp),intent(inout) :: g(3,n)
real(wp),intent(inout) :: etot
logical,intent(in) :: pr
logical,intent(in) :: makeq
real(wp) :: edisp,ees,ebond,eangl,etors,erep,ehb,exb,ebatm,eext
real(wp) :: gsolv,gborn,ghb,gsasa,gshift
integer :: i,j,k,l,m,ij,nd3
integer :: ati,atj,iat,jat
integer :: hbA,hbB
integer :: lin
logical :: ex,require_update
integer :: nhb1,nhb2,nxb
real(wp) :: r2,rab,qq0,erff,dd,dum1,r3(3),t8,dum,t22,t39
real(wp) :: dx,dy,dz,yy,t4,t5,t6,alpha,t20
real(wp) :: repab,t16,t19,t26,t27,xa,ya,za,cosa,de,t28
real(wp) :: gammij,eesinf,etmp,phi
real(wp) :: rn,dr,g3tmp(3,3),g4tmp(3,4)
real(wp) :: rij,drij(3,n)
real(wp),allocatable :: grab0(:,:,:),rab0(:),eeqtmp(:,:)
real(wp),allocatable :: cn(:),dcn(:,:,:),qtmp(:)
real(wp),allocatable :: hb_cn(:),hb_dcn(:,:,:)
real(wp),allocatable :: sqrab(:),srab(:)
real(wp),allocatable :: g5tmp(:,:)
integer,allocatable :: d3list(:,:)
!type(tb_timer) :: timer
real(wp) :: dispthr,cnthr,repthr,hbthr1,hbthr2
call gfnff_thresholds(accuracy,dispthr,cnthr,repthr,hbthr1,hbthr2)
io = 0 !> return status
g = 0
exb = 0
ehb = 0
erep = 0
ees = 0
edisp = 0
ebond = 0
eangl = 0
etors = 0
ebatm = 0
eext = 0
gsolv = 0.0d0
gsasa = 0.0d0
gborn = 0.0d0
ghb = 0.0d0
gshift = 0.0d0
allocate (sqrab(n*(n+1)/2),srab(n*(n+1)/2),qtmp(n),g5tmp(3,n), &
& eeqtmp(2,n*(n+1)/2),d3list(2,n*(n+1)/2),dcn(3,n,n),cn(n), &
& hb_dcn(3,n,n),hb_cn(n))
! if (pr) call timer%new(10 + count([allocated(solvation)]),.false.)
! if (pr) call timer%measure(1,'distance/D3 list')
nd3 = 0
do i = 1,n
ij = i*(i-1)/2
do j = 1,i-1
k = ij+j
sqrab(k) = (xyz(1,i)-xyz(1,j))**2+&
& (xyz(2,i)-xyz(2,j))**2+&
& (xyz(3,i)-xyz(3,j))**2
if (sqrab(k) .lt. dispthr) then
nd3 = nd3+1
d3list(1,nd3) = i
d3list(2,nd3) = j
end if
srab(k) = sqrt(sqrab(k))
end do
!> The loop above only runs over the off diagonal elements
!> This initializes the unitialized diagonal to zero but does not
!> add it to the dispersion list.
sqrab(ij+i) = 0.0d0
srab(ij+i) = 0.0d0
end do
! if (pr) call timer%measure(1)
!!!!!!!!!!!!
! Setup HB
!!!!!!!!!!!!
! if (pr) call timer%measure(10,'HB/XB (incl list setup)')
if (allocated(nlist%q)) then
nlist%initialized = size(nlist%q) == n
end if
call gfnff_hbset0(n,at,xyz,sqrab,topo,nhb1,nhb2,nxb,hbthr1,hbthr2)
nlist%initialized = nlist%initialized.and.nhb1 <= nlist%nhb1 &
& .and.nhb2 <= nlist%nhb2.and.nxb <= nlist%nxb
require_update = .not.nlist%initialized
nlist%force_hbond_update = nhb1 .ne. nlist%nhb1 &
& .or.nhb2 .ne. nlist%nhb2 &
& .or.nxb .ne. nlist%nxb &
& .or. require_update
if (.not.nlist%initialized) then
call new(nlist,n,5*nhb1,5*nhb2,3*nxb)
nlist%hbrefgeo(:,:) = xyz
end if
if (update.or.require_update) then
call gfnff_hbset(n,at,xyz,sqrab,topo,nlist,hbthr1,hbthr2)
end if
! if (pr) call timer%measure(10)
!!!!!!!!!!!!!
! Setup
! GBSA
!!!!!!!!!!!!!
if (allocated(solvation)) then
! call timer%measure(11, "GBSA")
call solvation%update(at,xyz)
! call timer%measure(11)
end if
!!!!!!!!!!!!!
! REP part
! non-bonded
!!!!!!!!!!!!!
! if (pr) call timer%measure(2,'non bonded repulsion')
!$omp parallel do default(none) reduction(+:erep, g) &
!$omp shared(n, at, xyz, srab, sqrab, repthr, topo, param) &
!$omp private(iat, jat, m, ij, ati, atj, rab, r2, r3, t8, t16, t19, t26, t27)
do iat = 1,n
m = iat*(iat-1)/2
do jat = 1,iat-1
ij = m+jat
r2 = sqrab(ij)
if (r2 .gt. repthr) cycle ! cut-off
if (topo%bpair(ij) .eq. 1) cycle ! list avoided because of memory
ati = at(iat)
atj = at(jat)
rab = srab(ij)
t16 = r2**0.75
t19 = t16*t16
t8 = t16*topo%alphanb(ij)
t26 = exp(-t8)*param%repz(ati)*param%repz(atj)*param%repscaln
erep = erep+t26/rab !energy
t27 = t26*(1.5d0*t8+1.0d0)/t19
r3 = (xyz(:,iat)-xyz(:,jat))*t27
g(:,iat) = g(:,iat)-r3
g(:,jat) = g(:,jat)+r3
end do
end do
!$omp end parallel do
! if (pr) call timer%measure(2)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! just a extremely crude mode for 2D-3D conversion
! i.e. an harmonic potential with estimated Re
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (version == gffVersion%harmonic2020) then
ebond = 0
!$omp parallel do default(none) reduction(+:ebond, g) &
!$omp shared(topo, param, xyz, at) private(i, iat, jat, rab, r2, r3, rn, dum)
do i = 1,topo%nbond
iat = topo%blist(1,i)
jat = topo%blist(2,i)
r3 = xyz(:,iat)-xyz(:,jat)
rab = sqrt(sum(r3*r3))
rn = 0.7*(param%rcov(at(iat))+param%rcov(at(jat)))
r2 = rn-rab
ebond = ebond+0.1d0*r2**2 ! fixfc = 0.1
dum = 0.1d0*2.0d0*r2/rab
g(:,jat) = g(:,jat)+dum*r3
g(:,iat) = g(:,iat)-dum*r3
end do
!$omp end parallel do
etot = ebond+erep
return
end if
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! erf CN and gradient for disp
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! if (pr) call timer%measure(3,'dCN')
call gfnff_dlogcoord(n,at,xyz,srab,cn,dcn,cnthr,param) ! new erf used in GFN0
if (sum(topo%nr_hb) .gt. 0) call dncoord_erf(n,at,xyz,param%rcov,hb_cn,hb_dcn,900.0d0,topo) ! HB erf CN
! if (pr) call timer%measure(3)
!!!!!!
! EEQ
!!!!!!
! if (pr) call timer%measure(4,'EEQ energy and q')
call goed_gfnff(accuracy .gt. 1,n,at,sqrab,srab,& ! modified version
& dfloat(ichrg),eeqtmp,cn,nlist%q,ees,solvation,param,topo,io) ! without dq/dr
! if (pr) call timer%measure(4)
!!!!!!!!
!D3(BJ)
!!!!!!!!
! if (pr) call timer%measure(5,'D3')
if (nd3 .gt. 0) then
call d3_gradient(topo%dispm,n,at,xyz,nd3,d3list,topo%zetac6, &
& param%d3r0,sqrtZr4r2,4.0d0,param%dispscale,cn,dcn,edisp,g)
end if
deallocate (d3list)
! if (pr) call timer%measure(5)
!!!!!!!!
! ES part
!!!!!!!!
! if (pr) call timer%measure(6,'EEQ gradient')
!$omp parallel do default(none) reduction (+:g) &
!$omp shared(topo,nlist,n,sqrab,srab,eeqtmp,xyz,at) &
!$omp private(i,j,k,ij,r3,r2,rab,gammij,erff,dd)
do i = 1,n
k = i*(i-1)/2
do j = 1,i-1
ij = k+j
r2 = sqrab(ij)
rab = srab(ij)
gammij = eeqtmp(1,ij)
erff = eeqtmp(2,ij)
dd = (2.0d0*gammij*exp(-gammij**2*r2) &
& /(sqrtpi*r2)-erff/(rab*r2))*nlist%q(i)*nlist%q(j)
r3 = (xyz(:,i)-xyz(:,j))*dd
g(:,i) = g(:,i)+r3
g(:,j) = g(:,j)-r3
end do
end do
!$omp end parallel do
if (.not.pr) deallocate (eeqtmp)
if (allocated(solvation)) then
! call timer%measure(11, "GBSA")
call solvation%addGradient(at,xyz,nlist%q,nlist%q,g)
call solvation%getEnergyParts(nlist%q,nlist%q,gborn,ghb,gsasa, &
& gshift)
gsolv = gsasa+gborn+ghb+gshift
! call timer%measure(11)
else
gborn = 0.0d0
ghb = 0.0d0
end if
do i = 1,n
qtmp(i) = nlist%q(i)*param%cnf(at(i))/(2.0d0*sqrt(cn(i))+1.d-16)
end do
call gemv(dcn,qtmp,g,alpha=-1.0_wp,beta=1.0_wp)
! if (pr) call timer%measure(6)
!!!!!!!!!!!!!!!!!!
! SRB bonded part
!!!!!!!!!!!!!!!!!!
! if (pr) call timer%measure(7,'bonds')
if (topo%nbond .gt. 0) then
allocate (grab0(3,n,topo%nbond),rab0(topo%nbond))
rab0(:) = topo%vbond(1,:) ! shifts
call gfnffdrab(n,at,xyz,cn,dcn,topo%nbond,topo%blist,rab0,grab0)
deallocate (dcn)
!$omp parallel do default(none) reduction(+:g, ebond) &
!$omp shared(grab0, topo, param, rab0, srab, xyz, at, hb_cn, hb_dcn, n) &
!$omp private(i, k, iat, jat, ij, rab, rij, drij, t8, dr, dum, yy, &
!$omp& dx, dy, dz, t4, t5, t6, ati, atj)
do i = 1,topo%nbond
iat = topo%blist(1,i)
jat = topo%blist(2,i)
ati = at(iat)
atj = at(jat)
ij = iat*(iat-1)/2+jat
rab = srab(ij)
rij = rab0(i)
drij = grab0(:,:,i)
if (topo%nr_hb(i) .ge. 1) then
call egbond_hb(i,iat,jat,rab,rij,drij,hb_cn,hb_dcn,n,at,xyz,ebond,g,param,topo)
else
call egbond(i,iat,jat,rab,rij,drij,n,at,xyz,ebond,g,topo)
end if
end do
!$omp end parallel do
deallocate (hb_dcn)
!!!!!!!!!!!!!!!!!!
! bonded REP
!!!!!!!!!!!!!!!!!!
!$omp parallel do default(none) reduction(+:erep, g) &
!$omp shared(topo, param, at, sqrab, srab, xyz) &
!$omp private(i, iat, jat, ij, xa, ya, za, dx, dy, dz, r2, rab, ati, atj, &
!$omp& alpha, repab, t16, t19, t26, t27)
do i = 1,topo%nbond
iat = topo%blist(1,i)
jat = topo%blist(2,i)
ij = iat*(iat-1)/2+jat
xa = xyz(1,iat)
ya = xyz(2,iat)
za = xyz(3,iat)
dx = xa-xyz(1,jat)
dy = ya-xyz(2,jat)
dz = za-xyz(3,jat)
r2 = sqrab(ij)
rab = srab(ij)
ati = at(iat)
atj = at(jat)
alpha = sqrt(param%repa(ati)*param%repa(atj))
repab = param%repz(ati)*param%repz(atj)*param%repscalb
t16 = r2**0.75d0
t19 = t16*t16
t26 = exp(-alpha*t16)*repab
erep = erep+t26/rab !energy
t27 = t26*(1.5d0*alpha*t16+1.0d0)/t19
g(1,iat) = g(1,iat)-dx*t27
g(2,iat) = g(2,iat)-dy*t27
g(3,iat) = g(3,iat)-dz*t27
g(1,jat) = g(1,jat)+dx*t27
g(2,jat) = g(2,jat)+dy*t27
g(3,jat) = g(3,jat)+dz*t27
end do
!$omp end parallel do
end if
! if (pr) call timer%measure(7)
!!!!!!!!!!!!!!!!!!
! bend
!!!!!!!!!!!!!!!!!!
! if (pr) call timer%measure(8,'bend and torsion')
if (topo%nangl .gt. 0) then
!$omp parallel do default(none) reduction (+:eangl, g) &
!$omp shared(n, at, xyz, topo, param) &
!$omp private(m, j, i, k, etmp, g3tmp)
do m = 1,topo%nangl
j = topo%alist(1,m)
i = topo%alist(2,m)
k = topo%alist(3,m)
call egbend(m,j,i,k,n,at,xyz,etmp,g3tmp,param,topo)
g(1:3,j) = g(1:3,j)+g3tmp(1:3,1)
g(1:3,i) = g(1:3,i)+g3tmp(1:3,2)
g(1:3,k) = g(1:3,k)+g3tmp(1:3,3)
eangl = eangl+etmp
end do
!$omp end parallel do
end if
!!!!!!!!!!!!!!!!!!
! torsion
!!!!!!!!!!!!!!!!!!
if (topo%ntors .gt. 0) then
!$omp parallel do default(none) reduction(+:etors, g) &
!$omp shared(param, topo, n, at, xyz) &
!$omp private(m, i, j, k, l, etmp, g4tmp)
do m = 1,topo%ntors
i = topo%tlist(1,m)
j = topo%tlist(2,m)
k = topo%tlist(3,m)
l = topo%tlist(4,m)
call egtors(m,i,j,k,l,n,at,xyz,etmp,g4tmp,param,topo)
g(1:3,i) = g(1:3,i)+g4tmp(1:3,1)
g(1:3,j) = g(1:3,j)+g4tmp(1:3,2)
g(1:3,k) = g(1:3,k)+g4tmp(1:3,3)
g(1:3,l) = g(1:3,l)+g4tmp(1:3,4)
etors = etors+etmp
end do
!$omp end parallel do
end if
! if (pr) call timer%measure(8)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! triple bonded carbon torsion potential
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (allocated(topo%sTorsl)) then
m = size(topo%sTorsl(1,:))
if (m.ne.0) then
do i=1, m
call sTors_eg(m, n, xyz, topo, etmp, g5tmp)
etors = etors + etmp
g = g + g5tmp
enddo
endif
endif
!!!!!!!!!!!!!!!!!!
! BONDED ATM
!!!!!!!!!!!!!!!!!!
! if (pr) call timer%measure(9,'bonded ATM')
if (topo%nbatm .gt. 0) then
!$omp parallel do default(none) reduction(+:ebatm, g) &
!$omp shared(n, at, xyz, srab, sqrab, topo, param) &
!$omp private(i, j, k, l, etmp, g3tmp)
do i = 1,topo%nbatm
j = topo%b3list(1,i)
k = topo%b3list(2,i)
l = topo%b3list(3,i)
call batmgfnff_eg(n,j,k,l,at,xyz,topo%qa,sqrab,srab,etmp,g3tmp,param)
g(1:3,j) = g(1:3,j)+g3tmp(1:3,1)
g(1:3,k) = g(1:3,k)+g3tmp(1:3,2)
g(1:3,l) = g(1:3,l)+g3tmp(1:3,3)
ebatm = ebatm+etmp
end do
!$omp end parallel do
end if
! if (pr) call timer%measure(9)
!!!!!!!!!!!!!!!!!!
! EHB
!!!!!!!!!!!!!!!!!!
! if (pr) call timer%measure(10,'HB/XB (incl list setup)')
if (nlist%nhb1 .gt. 0) then
!$omp parallel do default(none) reduction(+:ehb, g) &
!$omp shared(topo, nlist, param, n, at, xyz, sqrab, srab) &
!$omp private(i, j, k, l, etmp, g3tmp)
do i = 1,nlist%nhb1
j = nlist%hblist1(1,i)
k = nlist%hblist1(2,i)
l = nlist%hblist1(3,i)
call abhgfnff_eg1(n,j,k,l,at,xyz,topo%qa,sqrab,srab,etmp,g3tmp,param,topo)
g(1:3,j) = g(1:3,j)+g3tmp(1:3,1)
g(1:3,k) = g(1:3,k)+g3tmp(1:3,2)
g(1:3,l) = g(1:3,l)+g3tmp(1:3,3)
ehb = ehb+etmp
end do
!$omp end parallel do
end if
if (nlist%nhb2 .gt. 0) then
!$omp parallel do default(none) reduction(+:ehb, g) &
!$omp shared(topo, nlist, param, n, at, xyz, sqrab, srab) &
!$omp private(i, j, k, l, etmp, g5tmp)
do i = 1,nlist%nhb2
j = nlist%hblist2(1,i)
k = nlist%hblist2(2,i)
l = nlist%hblist2(3,i)
!Carbonyl case R-C=O...H_A
if (at(k) .eq. 8.and.topo%nb(20,k) .eq. 1.and.at(topo%nb(1,k)) .eq. 6) then
call abhgfnff_eg3(n,j,k,l,at,xyz,topo%qa,sqrab,srab, &
& etmp,g5tmp,param,topo)
!Nitro case R-N=O...H_A
else if (at(k) .eq. 8.and.topo%nb(20,k) .eq. 1.and.at(topo%nb(1,k)) .eq. 7) then
call abhgfnff_eg3(n,j,k,l,at,xyz,topo%qa,sqrab,srab, &
& etmp,g5tmp,param,topo)
!N hetero aromat
else if (at(k) .eq. 7.and.topo%nb(20,k) .eq. 2) then
call abhgfnff_eg2_rnr(n,j,k,l,at,xyz,topo%qa,sqrab,srab, &
& etmp,g5tmp,param,topo)
else
!Default
call abhgfnff_eg2new(n,j,k,l,at,xyz,topo%qa,sqrab,srab, &
& etmp,g5tmp,param,topo)
end if
g = g+g5tmp
ehb = ehb+etmp
end do
!$omp end parallel do
end if
!!!!!!!!!!!!!!!!!!
! EXB
!!!!!!!!!!!!!!!!!!
if (nlist%nxb .gt. 0) then
!$omp parallel do default(none) reduction(+:exb, g) &
!$omp shared(topo, nlist, param, n, at, xyz) &
!$omp private(i, j, k, l, etmp, g3tmp)
do i = 1,nlist%nxb
j = nlist%hblist3(1,i)
k = nlist%hblist3(2,i)
l = nlist%hblist3(3,i)
call rbxgfnff_eg(n,j,k,l,at,xyz,topo%qa,etmp,g3tmp,param)
g(1:3,j) = g(1:3,j)+g3tmp(1:3,1)
g(1:3,k) = g(1:3,k)+g3tmp(1:3,2)
g(1:3,l) = g(1:3,l)+g3tmp(1:3,3)
exb = exb+etmp
end do
!$omp end parallel do
end if
! if (pr) call timer%measure(10)
!!!!!!!!!!!!!!!!!!
! external stuff
!!!!!!!!!!!!!!!!!!
if (sum(abs(efield)) .gt. 1d-6) then
do i = 1,n
r3(:) = -nlist%q(i)*efield(:)
g(:,i) = g(:,i)+r3(:)
eext = eext+r3(1)*(xyz(1,i)-topo%xyze0(1,i))+&
& r3(2)*(xyz(2,i)-topo%xyze0(2,i))+&
& r3(3)*(xyz(3,i)-topo%xyze0(3,i))
end do
end if
!!!!!!!!!!!!!!!!!!!!!!!!!!
! total energy summation
!!!!!!!!!!!!!!!!!!!!!!!!!!
etot = ees+edisp+erep+ebond &
& +eangl+etors+ehb+exb+ebatm+eext &
& +gsolv
!!!!!!!!!!!!!!!!!!
! printout
!!!!!!!!!!!!!!!!!!
if (pr) then
! call timer%write(6,'E+G')
if (abs(sum(nlist%q)-ichrg) .gt. 1.d-1) then ! check EEQ only once
write (stdout,*) nlist%q
write (stdout,*) sum(nlist%q),ichrg
write (stdout,'("EEQ charge constrain error ",a)') source
io = 1
return
end if
r3 = 0
do i = 1,n
r3(:) = r3(:)+nlist%q(i)*xyz(:,i)
end do
!> just for fit De calc
sqrab = 1.d+12
srab = 1.d+6
cn = 0
!> asymtotically for R=inf, Etot is the SIE contaminted EES
!> which is computed here to get the atomization energy De,n,at(n)
call goed_gfnff(.true.,n,at,sqrab,srab,dfloat(ichrg),eeqtmp,cn,qtmp,eesinf,solvation,param,topo,io)
de = -(etot-eesinf)
end if
!> write resusts to res type
res_gff%e_total = etot
res_gff%gnorm = sqrt(sum(g**2))
res_gff%e_bond = ebond
res_gff%e_angl = eangl
res_gff%e_tors = etors
res_gff%e_es = ees
res_gff%e_rep = erep
res_gff%e_disp = edisp
res_gff%e_hb = ehb
res_gff%e_xb = exb
res_gff%e_batm = ebatm
res_gff%e_ext = eext
res_gff%g_hb = ghb
res_gff%g_born = gborn
res_gff%g_solv = gsolv
res_gff%g_shift = gshift
res_gff%g_sasa = gsasa
call gemv(xyz,nlist%q,res_gff%dipole)
end subroutine gfnff_eg
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine egbond(i,iat,jat,rab,rij,drij,n,at,xyz,e,g,topo)
implicit none
!Dummy
type(TGFFTopology),intent(in) :: topo
integer,intent(in) :: i
integer,intent(in) :: n
integer,intent(in) :: iat
integer,intent(in) :: jat
integer,intent(in) :: at(n)
real(wp),intent(in) :: rab
real(wp),intent(in) :: rij
real(wp),intent(in) :: drij(3,n)
real(wp),intent(in) :: xyz(3,n)
real(wp),intent(inout) :: e
real(wp),intent(inout) :: g(3,n)
!Stack
integer j,k
real(wp) :: dr,dum
real(wp) :: dx,dy,dz
real(wp) :: yy
real(wp) :: t4,t5,t6,t8
t8 = topo%vbond(2,i)
dr = rab-rij
dum = topo%vbond(3,i)*exp(-t8*dr**2)
e = e+dum ! bond energy
yy = 2.0d0*t8*dr*dum
dx = xyz(1,iat)-xyz(1,jat)
dy = xyz(2,iat)-xyz(2,jat)
dz = xyz(3,iat)-xyz(3,jat)
t4 = -yy*(dx/rab-drij(1,iat))
t5 = -yy*(dy/rab-drij(2,iat))
t6 = -yy*(dz/rab-drij(3,iat))
g(1,iat) = g(1,iat)+t4-drij(1,iat)*yy ! to avoid if in loop below
g(2,iat) = g(2,iat)+t5-drij(2,iat)*yy
g(3,iat) = g(3,iat)+t6-drij(3,iat)*yy
t4 = -yy*(-dx/rab-drij(1,jat))
t5 = -yy*(-dy/rab-drij(2,jat))
t6 = -yy*(-dz/rab-drij(3,jat))
g(1,jat) = g(1,jat)+t4-drij(1,jat)*yy ! to avoid if in loop below
g(2,jat) = g(2,jat)+t5-drij(2,jat)*yy
g(3,jat) = g(3,jat)+t6-drij(3,jat)*yy
do k = 1,n !3B gradient
g(:,k) = g(:,k)+drij(:,k)*yy
end do
end subroutine egbond
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine egbond_hb(i,iat,jat,rab,rij,drij,hb_cn,hb_dcn,n,at,xyz,e,g,param,topo)
implicit none
!Dummy
type(TGFFData),intent(in) :: param
type(TGFFTopology),intent(in) :: topo
integer,intent(in) :: i
integer,intent(in) :: n
integer,intent(in) :: iat
integer,intent(in) :: jat
integer,intent(in) :: at(n)
real(wp),intent(in) :: rab
real(wp),intent(in) :: rij
real(wp),intent(in) :: drij(3,n)
real(wp),intent(in) :: xyz(3,n)
real(wp),intent(in) :: hb_cn(n)
real(wp),intent(in) :: hb_dcn(3,n,n)
real(wp),intent(inout) :: e
real(wp),intent(inout) :: g(3,n)
!Stack
integer j,k
integer jA,jH
integer hbH,hbB,hbA
real(wp) :: dr,dum
real(wp) :: dx,dy,dz
real(wp) :: yy,zz
real(wp) :: t1,t4,t5,t6,t8
if (at(iat) .eq. 1) then
hbH = iat
hbA = jat
else if (at(jat) .eq. 1) then
hbH = jat
hbA = iat
else
! write (stdout,'(10x,"No H-atom found in this bond ",i0,1x,i0)') iat,jat
return
end if
t1 = 1.0-param%vbond_scale
t8 = (-t1*hb_cn(hbH)+1.0)*topo%vbond(2,i)
dr = rab-rij
dum = topo%vbond(3,i)*exp(-t8*dr**2)
e = e+dum ! bond energy
yy = 2.0d0*t8*dr*dum
dx = xyz(1,iat)-xyz(1,jat)
dy = xyz(2,iat)-xyz(2,jat)
dz = xyz(3,iat)-xyz(3,jat)
t4 = -yy*(dx/rab-drij(1,iat))
t5 = -yy*(dy/rab-drij(2,iat))
t6 = -yy*(dz/rab-drij(3,iat))
g(1,iat) = g(1,iat)+t4-drij(1,iat)*yy ! to avoid if in loop below
g(2,iat) = g(2,iat)+t5-drij(2,iat)*yy
g(3,iat) = g(3,iat)+t6-drij(3,iat)*yy
t4 = -yy*(-dx/rab-drij(1,jat))
t5 = -yy*(-dy/rab-drij(2,jat))
t6 = -yy*(-dz/rab-drij(3,jat))
g(1,jat) = g(1,jat)+t4-drij(1,jat)*yy ! to avoid if in loop below
g(2,jat) = g(2,jat)+t5-drij(2,jat)*yy
g(3,jat) = g(3,jat)+t6-drij(3,jat)*yy
do k = 1,n !3B gradient
g(:,k) = g(:,k)+drij(:,k)*yy
end do
zz = dum*topo%vbond(2,i)*dr**2*t1
do j = 1,topo%bond_hb_nr !CN gradient
jH = topo%bond_hb_AH(2,j)
jA = topo%bond_hb_AH(1,j)
if (jH .eq. hbH.and.jA .eq. hbA) then
g(:,hbH) = g(:,hbH)+hb_dcn(:,hbH,hbH)*zz
do k = 1,topo%bond_hb_Bn(j)
hbB = topo%bond_hb_B(k,j)
g(:,hbB) = g(:,hbB)-hb_dcn(:,hbB,hbH)*zz
end do
end if
end do
end subroutine egbond_hb
subroutine dncoord_erf(nat,at,xyz,rcov,cn,dcn,thr,topo)
implicit none
!Dummy
type(TGFFTopology),intent(in) :: topo
integer,intent(in) :: nat
integer,intent(in) :: at(nat)
real(wp),intent(in) :: xyz(3,nat)
real(wp),intent(in) :: rcov(:)
real(wp),intent(out) :: cn(nat)
real(wp),intent(out) :: dcn(3,nat,nat)
real(wp),intent(in),optional :: thr
real(wp) :: cn_thr
!Stack
integer :: i,j
integer :: lin,linAH
integer :: iat,jat
integer :: iA,jA,jH
integer :: ati,atj
real(wp) :: r,r2,rij(3)
real(wp) :: rcovij
real(wp) :: dtmp,tmp
real(wp),parameter :: hlfosqrtpi = 1.0_wp/1.77245385091_wp
real(wp),parameter :: kn = 27.5_wp
real(wp),parameter :: rcov_scal = 1.78
cn = 0._wp
dcn = 0._wp
do i = 1,topo%bond_hb_nr
iat = topo%bond_hb_AH(2,i)
ati = at(iat)
iA = topo%bond_hb_AH(1,i)
do j = 1,topo%bond_hb_Bn(i)
jat = topo%bond_hb_B(j,i)
atj = at(jat)
rij = xyz(:,jat)-xyz(:,iat)
r2 = sum(rij**2)
if (r2 .gt. thr) cycle
r = sqrt(r2)
rcovij = rcov_scal*(rcov(ati)+rcov(atj))
tmp = 0.5_wp*(1.0_wp+erf(-kn*(r-rcovij)/rcovij))
dtmp = -hlfosqrtpi*kn*exp(-kn**2*(r-rcovij)**2/rcovij**2)/rcovij
cn(iat) = cn(iat)+tmp
cn(jat) = cn(jat)+tmp
dcn(:,jat,jat) = dtmp*rij/r+dcn(:,jat,jat)
dcn(:,iat,jat) = dtmp*rij/r
dcn(:,jat,iat) = -dtmp*rij/r
dcn(:,iat,iat) = -dtmp*rij/r+dcn(:,iat,iat)
end do
end do
end subroutine dncoord_erf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine egbend(m,j,i,k,n,at,xyz,e,g,param,topo)
implicit none
type(TGFFData),intent(in) :: param
type(TGFFTopology),intent(in) :: topo
integer m,n,at(n)
integer i,j,k
real(wp) :: xyz(3,n),g(3,3),e
real(wp) :: c0,kijk,va(3),vb(3),vc(3),cosa
real(wp) :: dt,ea,dedb(3),dedc(3),rmul2,rmul1,deddt
real(wp) :: term1(3),term2(3),rab2,vab(3),vcb(3),rp
real(wp) :: rcb2,damp,dampij,damp2ij,dampjk,damp2jk
real(wp) :: theta,deda(3),vp(3),et,dij,c1
real(wp) :: term3(3),x1sin,x1cos,e1,dphi1,vdc(3)
real(wp) :: ddd(3),ddc(3),ddb(3),dda(3),rjl,phi
real(wp) :: rij,rijk,phi0,rkl,rjk,dampkl,damp2kl
real(wp) :: dampjl,damp2jl,rn
c0 = topo%vangl(1,m)
kijk = topo%vangl(2,m)
va(1:3) = xyz(1:3,i)
vb(1:3) = xyz(1:3,j)
vc(1:3) = xyz(1:3,k)
call vsub(va,vb,vab,3)
call vsub(vc,vb,vcb,3)
rab2 = vab(1)*vab(1)+vab(2)*vab(2)+vab(3)*vab(3)
rcb2 = vcb(1)*vcb(1)+vcb(2)*vcb(2)+vcb(3)*vcb(3)
call crprod(vcb,vab,vp)
rp = vlen(vp)+1.d-14
call impsc(vab,vcb,cosa)
cosa = dble(min(1.0d0,max(-1.0d0,cosa)))
theta = dacos(cosa)
call gfnffdampa(at(i),at(j),rab2,dampij,damp2ij,param)
call gfnffdampa(at(k),at(j),rcb2,dampjk,damp2jk,param)
damp = dampij*dampjk
if (pi-c0 .lt. 1.d-6) then ! linear
dt = theta-c0
ea = kijk*dt**2
deddt = 2.d0*kijk*dt
else
ea = kijk*(cosa-cos(c0))**2
deddt = 2.0d0*kijk*sin(theta)*(cos(c0)-cosa)
end if
e = ea*damp
call crprod(vab,vp,deda)
rmul1 = -deddt/(rab2*rp)
deda = deda*rmul1
call crprod(vcb,vp,dedc)
rmul2 = deddt/(rcb2*rp)
dedc = dedc*rmul2
dedb = deda+dedc
term1(1:3) = ea*damp2ij*dampjk*vab(1:3)
term2(1:3) = ea*damp2jk*dampij*vcb(1:3)
g(1:3,1) = -dedb(1:3)*damp-term1(1:3)-term2(1:3)
g(1:3,2) = deda(1:3)*damp+term1(1:3)
g(1:3,3) = dedc(1:3)*damp+term2(1:3)
end subroutine egbend
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine egbend_nci_mul(j,i,k,c0,fc,n,at,xyz,e,g)
implicit none
!Dummy
integer n,at(n)
integer i,j,k
real(wp) :: c0,fc
real(wp) :: xyz(3,n),g(3,3),e
!Stack
real(wp) :: kijk,va(3),vb(3),vc(3),cosa
real(wp) :: dt,ea,dedb(3),dedc(3),rmul2,rmul1,deddt
real(wp) :: term1(3),term2(3),rab2,vab(3),vcb(3),rp
real(wp) :: rcb2,damp,dampij,damp2ij,dampjk,damp2jk
real(wp) :: theta,deda(3),vp(3),et,dij,c1
real(wp) :: term3(3),x1sin,x1cos,e1,dphi1,vdc(3)
real(wp) :: ddd(3),ddc(3),ddb(3),dda(3),rjl,phi
real(wp) :: rij,rijk,phi0,rkl,rjk,dampkl,damp2kl
real(wp) :: dampjl,damp2jl,rn
kijk = fc/(cos(0.0d0)-cos(c0))**2
va(1:3) = xyz(1:3,i)
vb(1:3) = xyz(1:3,j)
vc(1:3) = xyz(1:3,k)
call vsub(va,vb,vab,3)
call vsub(vc,vb,vcb,3)
rab2 = vab(1)*vab(1)+vab(2)*vab(2)+vab(3)*vab(3)
rcb2 = vcb(1)*vcb(1)+vcb(2)*vcb(2)+vcb(3)*vcb(3)
call crprod(vcb,vab,vp)
rp = vlen(vp)+1.d-14
call impsc(vab,vcb,cosa)
cosa = dble(min(1.0d0,max(-1.0d0,cosa)))
theta = dacos(cosa)
if (pi-c0 .lt. 1.d-6) then ! linear
dt = theta-c0
ea = kijk*dt**2
deddt = 2.d0*kijk*dt
else
ea = kijk*(cosa-cos(c0))**2 ! not linear
deddt = 2.0d0*kijk*sin(theta)*(cos(c0)-cosa)
end if
e = (1.0d0-ea)
call crprod(vab,vp,deda)
rmul1 = -deddt/(rab2*rp)
deda = deda*rmul1
call crprod(vcb,vp,dedc)
rmul2 = deddt/(rcb2*rp)
dedc = dedc*rmul2
dedb = deda+dedc
g(1:3,1) = dedb(1:3)
g(1:3,2) = -deda(1:3)
g(1:3,3) = -dedc(1:3)
end subroutine egbend_nci_mul
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine egbend_nci(j,i,k,c0,kijk,n,at,xyz,e,g,param)
implicit none
!Dummy
type(TGFFData),intent(in) :: param
integer n,at(n)
integer i,j,k
real(wp) :: c0,kijk
real(wp) :: xyz(3,n),g(3,3),e
!Stack
real(wp) :: va(3),vb(3),vc(3),cosa
real(wp) :: dt,ea,dedb(3),dedc(3),rmul2,rmul1,deddt
real(wp) :: term1(3),term2(3),rab2,vab(3),vcb(3),rp
real(wp) :: rcb2,damp,dampij,damp2ij,dampjk,damp2jk
real(wp) :: theta,deda(3),vp(3),et,dij,c1
real(wp) :: term3(3),x1sin,x1cos,e1,dphi1,vdc(3)
real(wp) :: ddd(3),ddc(3),ddb(3),dda(3),rjl,phi
real(wp) :: rij,rijk,phi0,rkl,rjk,dampkl,damp2kl
real(wp) :: dampjl,damp2jl,rn
va(1:3) = xyz(1:3,i)
vb(1:3) = xyz(1:3,j)
vc(1:3) = xyz(1:3,k)
call vsub(va,vb,vab,3)
call vsub(vc,vb,vcb,3)
rab2 = vab(1)*vab(1)+vab(2)*vab(2)+vab(3)*vab(3)