-
Notifications
You must be signed in to change notification settings - Fork 1
/
libgrpp_integrals.F
928 lines (807 loc) · 39.1 KB
/
libgrpp_integrals.F
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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Local and semi-local ECP integrals using the libgrpp library
! **************************************************************************************************
MODULE libgrpp_integrals
USE kinds, ONLY: dp
USE mathconstants, ONLY: pi
USE ai_derivatives, ONLY: dabdr_noscreen, adbdr, dabdr
USE orbital_pointers, ONLY: nco, &
ncoset
#if defined(__LIBGRPP)
USE libgrpp, ONLY: libgrpp_init, libgrpp_type1_integrals, libgrpp_type2_integrals, &
libgrpp_type1_integrals_gradient, libgrpp_type2_integrals_gradient
#endif
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'libgrpp_integrals'
PUBLIC :: libgrpp_semilocal_integrals, libgrpp_local_integrals, &
libgrpp_local_forces_ref, libgrpp_semilocal_forces_ref
CONTAINS
! **************************************************************************************************
!> \brief Local ECP integrals using libgrpp
!> \param la_max_set ...
!> \param la_min_set ...
!> \param npgfa ...
!> \param rpgfa ...
!> \param zeta ...
!> \param lb_max_set ...
!> \param lb_min_set ...
!> \param npgfb ...
!> \param rpgfb ...
!> \param zetb ...
!> \param npot_ecp ...
!> \param alpha_ecp ...
!> \param coeffs_ecp ...
!> \param nrpot_ecp ...
!> \param rpgfc ...
!> \param rab ...
!> \param dab ...
!> \param rac ...
!> \param dac ...
!> \param dbc ...
!> \param vab ...
!> \param pab ...
!> \param force_a ...
!> \param force_b ...
! **************************************************************************************************
SUBROUTINE libgrpp_local_integrals(la_max_set, la_min_set, npgfa, rpgfa, zeta, &
lb_max_set, lb_min_set, npgfb, rpgfb, zetb, &
npot_ecp, alpha_ecp, coeffs_ecp, nrpot_ecp, &
rpgfc, rab, dab, rac, dac, dbc, vab, pab, force_a, force_b)
INTEGER, INTENT(IN) :: la_max_set, la_min_set, npgfa
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfa, zeta
INTEGER, INTENT(IN) :: lb_max_set, lb_min_set, npgfb
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfb, zetb
INTEGER, INTENT(IN) :: npot_ecp
REAL(KIND=dp), DIMENSION(1:npot_ecp), INTENT(IN) :: alpha_ecp, coeffs_ecp
INTEGER, DIMENSION(1:npot_ecp), INTENT(IN) :: nrpot_ecp
REAL(KIND=dp), INTENT(IN) :: rpgfc
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rab
REAL(KIND=dp), INTENT(IN) :: dab
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rac
REAL(KIND=dp), INTENT(IN) :: dac
REAL(KIND=dp), INTENT(IN) :: dbc
REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: vab
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
OPTIONAL :: pab
REAL(KIND=dp), DIMENSION(3), INTENT(INOUT), &
OPTIONAL :: force_a, force_b
#if defined(__LIBGRPP)
INTEGER :: a_offset, a_start, b_offset, b_start, i, &
ipgf, j, jpgf, li, lj, ncoa, ncob
LOGICAL :: calc_forces
REAL(dp) :: expi, expj, normi, normj, prefi, prefj, &
zeti, zetj, mindist, fac_a, fac_b
REAL(dp), ALLOCATABLE, DIMENSION(:) :: tmp, tmpx, tmpy, tmpz
REAL(dp), DIMENSION(3) :: ra, rb, rc
CALL libgrpp_init()
calc_forces = .FALSE.
IF (PRESENT(pab) .AND. PRESENT(force_a) .AND. PRESENT(force_b)) calc_forces = .TRUE.
IF (calc_forces) THEN
!Note: warning against numerical stability of libgrpp gradients. The day the library becomes
! stable, this routine can be used immediatly as is, and the warning removed.
CALL cp_warn(__LOCATION__, &
"ECP gradients calculated with the libgrpp library are, to this date, not numerically stable. "// &
"Please use the reference routine 'libgrpp_local_forces_ref' instead.")
!there is a weird feature of libgrpp gradients, which is such that the gradient is calculated
!for a point in space, and not with respect to an atomic center. For example, if atoms A and
!B are the same (and C is different), then d<A | U_C | B>/dPx = d<A | U_C | B>/dAx + d<A | U_C | B>/dBx
!Because we want the forces on centers A and B seprately, we need a case study on atomic positions
!We always calculate the gradient wrt to atomic position of A and B, and we scale accordingly
mindist = 1.0E-6_dp
!If ra != rb != rc
IF (dab >= mindist .AND. dbc >= mindist .AND. dac >= mindist) THEN
fac_a = 1.0_dp
fac_b = 1.0_dp
!If ra = rb, but ra != rc
ELSE IF (dab < mindist .AND. dac >= mindist) THEN
fac_a = 0.5_dp
fac_b = 0.5_dp
!IF ra != rb but ra = rc
ELSE IF (dab >= mindist .AND. dac < mindist) THEN
fac_a = 0.5_dp
fac_b = 1.0_dp
!IF ra != rb but rb = rc
ELSE IF (dab >= mindist .AND. dbc < mindist) THEN
fac_a = 1.0_dp
fac_b = 0.5_dp
!If all atoms the same --> no force
ELSE
calc_forces = .FALSE.
END IF
END IF
!libgrpp requires absolute positions, not relative ones
ra(:) = 0.0_dp
rb(:) = rab(:)
rc(:) = rac(:)
ALLOCATE (tmp(nco(la_max_set)*nco(lb_max_set)))
IF (calc_forces) THEN
ALLOCATE (tmpx(nco(la_max_set)*nco(lb_max_set)))
ALLOCATE (tmpy(nco(la_max_set)*nco(lb_max_set)))
ALLOCATE (tmpz(nco(la_max_set)*nco(lb_max_set)))
END IF
DO ipgf = 1, npgfa
IF (rpgfa(ipgf) + rpgfc < dac) CYCLE
zeti = zeta(ipgf)
a_start = (ipgf - 1)*ncoset(la_max_set)
DO jpgf = 1, npgfb
IF (rpgfb(jpgf) + rpgfc < dbc) CYCLE
IF (rpgfa(ipgf) + rpgfb(jpgf) < dab) CYCLE
zetj = zetb(jpgf)
b_start = (jpgf - 1)*ncoset(lb_max_set)
DO li = la_min_set, la_max_set
a_offset = a_start + ncoset(li - 1)
ncoa = nco(li)
prefi = 2.0_dp**li*(2.0_dp/pi)**0.75_dp
expi = 0.25_dp*REAL(2*li + 3, dp)
normi = 1.0_dp/(prefi*zeti**expi)
DO lj = lb_min_set, lb_max_set
b_offset = b_start + ncoset(lj - 1)
ncob = nco(lj)
prefj = 2.0_dp**lj*(2.0_dp/pi)**0.75_dp
expj = 0.25_dp*REAL(2*lj + 3, dp)
normj = 1.0_dp/(prefj*zetj**expj)
tmp(1:ncoa*ncob) = 0.0_dp
!libgrpp implicitely normalizes cartesian Gaussian. In CP2K, we do not, hence
!the 1/norm coefficients for PGFi and PGFj
CALL libgrpp_type1_integrals(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, [npot_ecp], nrpot_ecp, &
coeffs_ecp, alpha_ecp, tmp)
!note: tmp array is in C row-major ordering
DO j = 1, ncob
DO i = 1, ncoa
vab(a_offset + i, b_offset + j) = vab(a_offset + i, b_offset + j) + tmp((i - 1)*ncob + j)
END DO
END DO
IF (calc_forces) THEN
tmpx(1:ncoa*ncob) = 0.0_dp
tmpy(1:ncoa*ncob) = 0.0_dp
tmpz(1:ncoa*ncob) = 0.0_dp
!force wrt to atomic position A
CALL libgrpp_type1_integrals_gradient(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, [npot_ecp], nrpot_ecp, &
coeffs_ecp, alpha_ecp, ra, &
tmpx, tmpy, tmpz)
!note: tmp array is in C row-major ordering
!note: zero-gradients sometime comes out as NaN, hence tampval==tmpval check
DO j = 1, ncob
DO i = 1, ncoa
force_a(1) = force_a(1) + fac_a*pab(a_offset + i, b_offset + j)*tmpx((i - 1)*ncob + j)
force_a(2) = force_a(2) + fac_a*pab(a_offset + i, b_offset + j)*tmpy((i - 1)*ncob + j)
force_a(3) = force_a(3) + fac_a*pab(a_offset + i, b_offset + j)*tmpz((i - 1)*ncob + j)
END DO
END DO
tmpx(1:ncoa*ncob) = 0.0_dp
tmpy(1:ncoa*ncob) = 0.0_dp
tmpz(1:ncoa*ncob) = 0.0_dp
!force wrt to atomic position B
CALL libgrpp_type1_integrals_gradient(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, [npot_ecp], nrpot_ecp, &
coeffs_ecp, alpha_ecp, rb, &
tmpx, tmpy, tmpz)
!note: tmp array is in C row-major ordering
!note: zero-gradients sometime comes out as NaN, hence tampval==tmpval check
DO j = 1, ncob
DO i = 1, ncoa
force_b(1) = force_b(1) + fac_b*pab(a_offset + i, b_offset + j)*tmpx((i - 1)*ncob + j)
force_b(2) = force_b(2) + fac_b*pab(a_offset + i, b_offset + j)*tmpy((i - 1)*ncob + j)
force_b(3) = force_b(3) + fac_b*pab(a_offset + i, b_offset + j)*tmpz((i - 1)*ncob + j)
END DO
END DO
END IF
END DO !lj
END DO !li
END DO !jpgf
END DO !ipgf
#else
MARK_USED(la_max_set)
MARK_USED(la_min_set)
MARK_USED(npgfa)
MARK_USED(rpgfa)
MARK_USED(zeta)
MARK_USED(lb_max_set)
MARK_USED(lb_min_set)
MARK_USED(npgfb)
MARK_USED(rpgfb)
MARK_USED(zetb)
MARK_USED(npot_ecp)
MARK_USED(alpha_ecp)
MARK_USED(coeffs_ecp)
MARK_USED(nrpot_ecp)
MARK_USED(rpgfc)
MARK_USED(rab)
MARK_USED(dab)
MARK_USED(rac)
MARK_USED(dac)
MARK_USED(dbc)
MARK_USED(vab)
MARK_USED(pab)
MARK_USED(force_a)
MARK_USED(force_b)
CPABORT("Please compile CP2K with libgrpp support for calculations with ECPs")
#endif
END SUBROUTINE libgrpp_local_integrals
! **************************************************************************************************
!> \brief Semi-local ECP integrals using libgrpp.
!> \param la_max_set ...
!> \param la_min_set ...
!> \param npgfa ...
!> \param rpgfa ...
!> \param zeta ...
!> \param lb_max_set ...
!> \param lb_min_set ...
!> \param npgfb ...
!> \param rpgfb ...
!> \param zetb ...
!> \param lmax_ecp ...
!> \param npot_ecp ...
!> \param alpha_ecp ...
!> \param coeffs_ecp ...
!> \param nrpot_ecp ...
!> \param rpgfc ...
!> \param rab ...
!> \param dab ...
!> \param rac ...
!> \param dac ...
!> \param dbc ...
!> \param vab ...
!> \param pab ...
!> \param force_a ...
!> \param force_b ...
! **************************************************************************************************
SUBROUTINE libgrpp_semilocal_integrals(la_max_set, la_min_set, npgfa, rpgfa, zeta, &
lb_max_set, lb_min_set, npgfb, rpgfb, zetb, &
lmax_ecp, npot_ecp, alpha_ecp, coeffs_ecp, nrpot_ecp, &
rpgfc, rab, dab, rac, dac, dbc, vab, pab, force_a, force_b)
INTEGER, INTENT(IN) :: la_max_set, la_min_set, npgfa
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfa, zeta
INTEGER, INTENT(IN) :: lb_max_set, lb_min_set, npgfb
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfb, zetb
INTEGER, INTENT(IN) :: lmax_ecp
INTEGER, DIMENSION(0:10), INTENT(IN) :: npot_ecp
REAL(KIND=dp), DIMENSION(1:15, 0:10), INTENT(IN) :: alpha_ecp, coeffs_ecp
INTEGER, DIMENSION(1:15, 0:10), INTENT(IN) :: nrpot_ecp
REAL(KIND=dp), INTENT(IN) :: rpgfc
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rab
REAL(KIND=dp), INTENT(IN) :: dab
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rac
REAL(KIND=dp), INTENT(IN) :: dac
REAL(KIND=dp), INTENT(IN) :: dbc
REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: vab
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
OPTIONAL :: pab
REAL(KIND=dp), DIMENSION(3), INTENT(INOUT), &
OPTIONAL :: force_a, force_b
#if defined(__LIBGRPP)
INTEGER :: a_offset, a_start, b_offset, b_start, i, &
ipgf, j, jpgf, li, lj, lk, ncoa, ncob
LOGICAL :: calc_forces
REAL(dp) :: expi, expj, normi, normj, prefi, prefj, &
zeti, zetj, mindist, fac_a, fac_b
REAL(dp), ALLOCATABLE, DIMENSION(:) :: tmp, tmpx, tmpz, tmpy
REAL(dp), DIMENSION(3) :: ra, rb, rc
CALL libgrpp_init()
calc_forces = .FALSE.
IF (PRESENT(pab) .AND. PRESENT(force_a) .AND. PRESENT(force_b)) calc_forces = .TRUE.
IF (calc_forces) THEN
!Note: warning against numerical stability of libgrpp gradients. The day the library becomes
! stable, this routine can be used immediatly as is, and the warning removed.
CALL cp_warn(__LOCATION__, &
"ECP gradients calculated with the libgrpp library are, to this date, not numerically stable. "// &
"Please use the reference routine 'libgrpp_semilocal_forces_ref' instead.")
!there is a weird feature of libgrpp gradients, which is such that the gradient is calculated
!for a point in space, and not with respect to an atomic center. For example, if atoms A and
!B are the same (and C is different), then d<A | U_C | B>/dPx = d<A | U_C | B>/dAx + d<A | U_C | B>/dBx
!Because we want the forces on centers A and B seprately, we need a case study on atomic positions
!We always calculate the gradient wrt to atomic position of A and B, and we scale accordingly
mindist = 1.0E-6_dp
!If ra != rb != rc
IF (dab >= mindist .AND. dbc >= mindist .AND. dac >= mindist) THEN
fac_a = 1.0_dp
fac_b = 1.0_dp
!If ra = rb, but ra != rc
ELSE IF (dab < mindist .AND. dac >= mindist) THEN
fac_a = 0.5_dp
fac_b = 0.5_dp
!IF ra != rb but ra = rc
ELSE IF (dab >= mindist .AND. dac < mindist) THEN
fac_a = 0.5_dp
fac_b = 1.0_dp
!IF ra != rb but rb = rc
ELSE IF (dab >= mindist .AND. dbc < mindist) THEN
fac_a = 1.0_dp
fac_b = 0.5_dp
!If all atoms the same --> no force
ELSE
calc_forces = .FALSE.
END IF
END IF
!libgrpp requires absolute positions, not relative ones
ra(:) = 0.0_dp
rb(:) = rab(:)
rc(:) = rac(:)
ALLOCATE (tmp(nco(la_max_set)*nco(lb_max_set)))
IF (calc_forces) THEN
ALLOCATE (tmpx(nco(la_max_set)*nco(lb_max_set)))
ALLOCATE (tmpy(nco(la_max_set)*nco(lb_max_set)))
ALLOCATE (tmpz(nco(la_max_set)*nco(lb_max_set)))
END IF
DO ipgf = 1, npgfa
IF (rpgfa(ipgf) + rpgfc < dac) CYCLE
zeti = zeta(ipgf)
a_start = (ipgf - 1)*ncoset(la_max_set)
DO jpgf = 1, npgfb
IF (rpgfb(jpgf) + rpgfc < dbc) CYCLE
IF (rpgfa(ipgf) + rpgfb(jpgf) < dab) CYCLE
zetj = zetb(jpgf)
b_start = (jpgf - 1)*ncoset(lb_max_set)
DO li = la_min_set, la_max_set
a_offset = a_start + ncoset(li - 1)
ncoa = nco(li)
prefi = 2.0_dp**li*(2.0_dp/pi)**0.75_dp
expi = 0.25_dp*REAL(2*li + 3, dp)
normi = 1.0_dp/(prefi*zeti**expi)
DO lj = lb_min_set, lb_max_set
b_offset = b_start + ncoset(lj - 1)
ncob = nco(lj)
prefj = 2.0_dp**lj*(2.0_dp/pi)**0.75_dp
expj = 0.25_dp*REAL(2*lj + 3, dp)
normj = 1.0_dp/(prefj*zetj**expj)
!Loop over ECP angular momentum
DO lk = 0, lmax_ecp
tmp(1:ncoa*ncob) = 0.0_dp
!libgrpp implicitely normalizes cartesian Gaussian. In CP2K, we do not, hence
!the 1/norm coefficients for PGFi and PGFj
CALL libgrpp_type2_integrals(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, lk, [npot_ecp(lk)], nrpot_ecp(:, lk), &
coeffs_ecp(:, lk), alpha_ecp(:, lk), tmp)
!note: tmp array is in C row-major ordering
DO j = 1, ncob
DO i = 1, ncoa
vab(a_offset + i, b_offset + j) = vab(a_offset + i, b_offset + j) + tmp((i - 1)*ncob + j)
END DO
END DO
IF (calc_forces) THEN
tmpx(1:ncoa*ncob) = 0.0_dp
tmpy(1:ncoa*ncob) = 0.0_dp
tmpz(1:ncoa*ncob) = 0.0_dp
!force wrt to atomic position A
CALL libgrpp_type2_integrals_gradient(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, lk, [npot_ecp(lk)], nrpot_ecp(:, lk), &
coeffs_ecp(:, lk), alpha_ecp(:, lk), ra, &
tmpx, tmpy, tmpz)
!note: tmp array is in C row-major ordering
!note: zero-gradients sometime comes out as NaN, hence tampval==tmpval check
DO j = 1, ncob
DO i = 1, ncoa
force_a(1) = force_a(1) + fac_a*pab(a_offset + i, b_offset + j)*tmpx((i - 1)*ncob + j)
force_a(2) = force_a(2) + fac_a*pab(a_offset + i, b_offset + j)*tmpy((i - 1)*ncob + j)
force_a(3) = force_a(3) + fac_a*pab(a_offset + i, b_offset + j)*tmpz((i - 1)*ncob + j)
END DO
END DO
tmpx(1:ncoa*ncob) = 0.0_dp
tmpy(1:ncoa*ncob) = 0.0_dp
tmpz(1:ncoa*ncob) = 0.0_dp
!force wrt to atomic position B
CALL libgrpp_type2_integrals_gradient(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, lk, [npot_ecp(lk)], nrpot_ecp(:, lk), &
coeffs_ecp(:, lk), alpha_ecp(:, lk), rb, &
tmpx, tmpy, tmpz)
!note: tmp array is in C row-major ordering
!note: zero-gradients sometime comes out as NaN, hence tampval==tmpval check
DO j = 1, ncob
DO i = 1, ncoa
force_b(1) = force_b(1) + fac_b*pab(a_offset + i, b_offset + j)*tmpx((i - 1)*ncob + j)
force_b(2) = force_b(2) + fac_b*pab(a_offset + i, b_offset + j)*tmpy((i - 1)*ncob + j)
force_b(3) = force_b(3) + fac_b*pab(a_offset + i, b_offset + j)*tmpz((i - 1)*ncob + j)
END DO
END DO
END IF !calc_forces
END DO !lk
END DO !lj
END DO !li
END DO !jpgf
END DO !ipgf
#else
MARK_USED(la_max_set)
MARK_USED(la_min_set)
MARK_USED(npgfa)
MARK_USED(rpgfa)
MARK_USED(zeta)
MARK_USED(lb_max_set)
MARK_USED(lb_min_set)
MARK_USED(npgfb)
MARK_USED(rpgfb)
MARK_USED(zetb)
MARK_USED(lmax_ecp)
MARK_USED(npot_ecp)
MARK_USED(alpha_ecp)
MARK_USED(coeffs_ecp)
MARK_USED(nrpot_ecp)
MARK_USED(rpgfc)
MARK_USED(rab)
MARK_USED(dab)
MARK_USED(rac)
MARK_USED(dac)
MARK_USED(dbc)
MARK_USED(vab)
MARK_USED(pab)
MARK_USED(force_a)
MARK_USED(force_b)
CPABORT("Please compile CP2K with libgrpp support for calculations with ECPs")
#endif
END SUBROUTINE libgrpp_semilocal_integrals
! **************************************************************************************************
!> \brief Reference local ECP force routine using l+-1 integrals. No call is made to the numerically
!> unstable gradient routine of libgrpp. Calculates both the integrals and the forces.
!> \param la_max_set ...
!> \param la_min_set ...
!> \param npgfa ...
!> \param rpgfa ...
!> \param zeta ...
!> \param lb_max_set ...
!> \param lb_min_set ...
!> \param npgfb ...
!> \param rpgfb ...
!> \param zetb ...
!> \param npot_ecp ...
!> \param alpha_ecp ...
!> \param coeffs_ecp ...
!> \param nrpot_ecp ...
!> \param rpgfc ...
!> \param rab ...
!> \param dab ...
!> \param rac ...
!> \param dac ...
!> \param dbc ...
!> \param vab ...
!> \param pab ...
!> \param force_a ...
!> \param force_b ...
!> \note: this is a reference routine, which has no reason to be used once the libgrpp gradients
!> become numerically stable
! **************************************************************************************************
SUBROUTINE libgrpp_local_forces_ref(la_max_set, la_min_set, npgfa, rpgfa, zeta, &
lb_max_set, lb_min_set, npgfb, rpgfb, zetb, &
npot_ecp, alpha_ecp, coeffs_ecp, nrpot_ecp, &
rpgfc, rab, dab, rac, dac, dbc, vab, pab, force_a, force_b)
INTEGER, INTENT(IN) :: la_max_set, la_min_set, npgfa
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfa, zeta
INTEGER, INTENT(IN) :: lb_max_set, lb_min_set, npgfb
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfb, zetb
INTEGER, INTENT(IN) :: npot_ecp
REAL(KIND=dp), DIMENSION(1:npot_ecp), INTENT(IN) :: alpha_ecp, coeffs_ecp
INTEGER, DIMENSION(1:npot_ecp), INTENT(IN) :: nrpot_ecp
REAL(KIND=dp), INTENT(IN) :: rpgfc
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rab
REAL(KIND=dp), INTENT(IN) :: dab
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rac
REAL(KIND=dp), INTENT(IN) :: dac
REAL(KIND=dp), INTENT(IN) :: dbc
REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: vab
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: pab
REAL(KIND=dp), DIMENSION(3), INTENT(INOUT) :: force_a, force_b
#if defined(__LIBGRPP)
INTEGER :: a_offset, a_start, b_offset, b_start, i, &
ipgf, j, jpgf, li, lj, ncoa, ncob, a_offset_f, &
b_offset_f, a_start_f, b_start_f
REAL(dp) :: expi, expj, normi, normj, prefi, prefj, &
zeti, zetj
REAL(dp), ALLOCATABLE, DIMENSION(:) :: tmp
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: vab_f, tmpx, tmpy, tmpz
REAL(dp), DIMENSION(3) :: ra, rb, rc
CALL libgrpp_init()
!Contains the integrals necessary for the forces, with angular momenta from lmin-1 to lmax+1
ALLOCATE (vab_f(npgfa*ncoset(la_max_set + 1), npgfb*ncoset(lb_max_set + 1)))
vab_f(:, :) = 0.0_dp
!libgrpp requires absolute positions, not relative ones
ra(:) = 0.0_dp
rb(:) = rab(:)
rc(:) = rac(:)
ALLOCATE (tmp(nco(la_max_set + 1)*nco(lb_max_set + 1)))
DO ipgf = 1, npgfa
IF (rpgfa(ipgf) + rpgfc < dac) CYCLE
zeti = zeta(ipgf)
a_start = (ipgf - 1)*ncoset(la_max_set)
a_start_f = (ipgf - 1)*ncoset(la_max_set + 1)
DO jpgf = 1, npgfb
IF (rpgfb(jpgf) + rpgfc < dbc) CYCLE
IF (rpgfa(ipgf) + rpgfb(jpgf) < dab) CYCLE
zetj = zetb(jpgf)
b_start = (jpgf - 1)*ncoset(lb_max_set)
b_start_f = (jpgf - 1)*ncoset(lb_max_set + 1)
DO li = MAX(0, la_min_set - 1), la_max_set + 1
a_offset = a_start + ncoset(li - 1)
a_offset_f = a_start_f + ncoset(li - 1)
ncoa = nco(li)
prefi = 2.0_dp**li*(2.0_dp/pi)**0.75_dp
expi = 0.25_dp*REAL(2*li + 3, dp)
normi = 1.0_dp/(prefi*zeti**expi)
DO lj = MAX(0, lb_min_set - 1), lb_max_set + 1
b_offset = b_start + ncoset(lj - 1)
b_offset_f = b_start_f + ncoset(lj - 1)
ncob = nco(lj)
prefj = 2.0_dp**lj*(2.0_dp/pi)**0.75_dp
expj = 0.25_dp*REAL(2*lj + 3, dp)
normj = 1.0_dp/(prefj*zetj**expj)
tmp(1:ncoa*ncob) = 0.0_dp
!libgrpp implicitely normalizes cartesian Gaussian. In CP2K, we do not, hence
!the 1/norm coefficients for PGFi and PGFj
CALL libgrpp_type1_integrals(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, [npot_ecp], nrpot_ecp, &
coeffs_ecp, alpha_ecp, tmp)
!the l+-1 integrals for gradient calculation
DO j = 1, ncob
DO i = 1, ncoa
vab_f(a_offset_f + i, b_offset_f + j) = &
vab_f(a_offset_f + i, b_offset_f + j) + tmp((i - 1)*ncob + j)
END DO
END DO
!the actual integrals
IF (li >= la_min_set .AND. li <= la_max_set .AND. lj >= lb_min_set .AND. lj <= lb_max_set) THEN
DO j = 1, ncob
DO i = 1, ncoa
vab(a_offset + i, b_offset + j) = vab(a_offset + i, b_offset + j) + tmp((i - 1)*ncob + j)
END DO
END DO
END IF
END DO !lj
END DO !li
END DO !jpgf
END DO !ipgf
ALLOCATE (tmpx(npgfa*ncoset(la_max_set), npgfb*ncoset(lb_max_set)))
ALLOCATE (tmpy(npgfa*ncoset(la_max_set), npgfb*ncoset(lb_max_set)))
ALLOCATE (tmpz(npgfa*ncoset(la_max_set), npgfb*ncoset(lb_max_set)))
!Derivative wrt to center A
tmpx(:, :) = 0.0_dp
tmpy(:, :) = 0.0_dp
tmpz(:, :) = 0.0_dp
CALL dabdr(la_max_set, npgfa, zeta, rpgfa, la_min_set, lb_max_set, npgfb, rpgfb, lb_min_set, &
dab, vab_f, tmpx, tmpy, tmpz)
DO j = 1, npgfb*ncoset(lb_max_set)
DO i = 1, npgfa*ncoset(la_max_set)
force_a(1) = force_a(1) + tmpx(i, j)*pab(i, j)
force_a(2) = force_a(2) + tmpy(i, j)*pab(i, j)
force_a(3) = force_a(3) + tmpz(i, j)*pab(i, j)
END DO
END DO
!Derivative wrt to center B
tmpx(:, :) = 0.0_dp
tmpy(:, :) = 0.0_dp
tmpz(:, :) = 0.0_dp
CALL adbdr(la_max_set, npgfa, rpgfa, la_min_set, lb_max_set, npgfb, zetb, rpgfb, lb_min_set, &
dab, vab_f, tmpx, tmpy, tmpz)
DO j = 1, npgfb*ncoset(lb_max_set)
DO i = 1, npgfa*ncoset(la_max_set)
force_b(1) = force_b(1) + tmpx(i, j)*pab(i, j)
force_b(2) = force_b(2) + tmpy(i, j)*pab(i, j)
force_b(3) = force_b(3) + tmpz(i, j)*pab(i, j)
END DO
END DO
DEALLOCATE (tmpx, tmpy, tmpz)
#else
MARK_USED(la_max_set)
MARK_USED(la_min_set)
MARK_USED(npgfa)
MARK_USED(rpgfa)
MARK_USED(zeta)
MARK_USED(lb_max_set)
MARK_USED(lb_min_set)
MARK_USED(npgfb)
MARK_USED(rpgfb)
MARK_USED(zetb)
MARK_USED(npot_ecp)
MARK_USED(alpha_ecp)
MARK_USED(coeffs_ecp)
MARK_USED(nrpot_ecp)
MARK_USED(rpgfc)
MARK_USED(rab)
MARK_USED(dab)
MARK_USED(rac)
MARK_USED(dac)
MARK_USED(dbc)
MARK_USED(pab)
MARK_USED(vab)
MARK_USED(force_a)
MARK_USED(force_b)
CPABORT("Please compile CP2K with libgrpp support for calculations with ECPs")
#endif
END SUBROUTINE libgrpp_local_forces_ref
! **************************************************************************************************
!> \brief Reference semi-local ECP forces using l+-1 integrals. No call is made to the numerically
!> unstable gradient routine of libgrpp. Calculates both the integrals and the forces.
!> \param la_max_set ...
!> \param la_min_set ...
!> \param npgfa ...
!> \param rpgfa ...
!> \param zeta ...
!> \param lb_max_set ...
!> \param lb_min_set ...
!> \param npgfb ...
!> \param rpgfb ...
!> \param zetb ...
!> \param lmax_ecp ...
!> \param npot_ecp ...
!> \param alpha_ecp ...
!> \param coeffs_ecp ...
!> \param nrpot_ecp ...
!> \param rpgfc ...
!> \param rab ...
!> \param dab ...
!> \param rac ...
!> \param dac ...
!> \param dbc ...
!> \param vab ...
!> \param pab ...
!> \param force_a ...
!> \param force_b ...
!> \note: this is a reference routine, which has no reason to be used once the libgrpp gradients
!> become numerically stable
! **************************************************************************************************
SUBROUTINE libgrpp_semilocal_forces_ref(la_max_set, la_min_set, npgfa, rpgfa, zeta, &
lb_max_set, lb_min_set, npgfb, rpgfb, zetb, &
lmax_ecp, npot_ecp, alpha_ecp, coeffs_ecp, nrpot_ecp, &
rpgfc, rab, dab, rac, dac, dbc, vab, pab, force_a, force_b)
INTEGER, INTENT(IN) :: la_max_set, la_min_set, npgfa
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfa, zeta
INTEGER, INTENT(IN) :: lb_max_set, lb_min_set, npgfb
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: rpgfb, zetb
INTEGER, INTENT(IN) :: lmax_ecp
INTEGER, DIMENSION(0:10), INTENT(IN) :: npot_ecp
REAL(KIND=dp), DIMENSION(1:15, 0:10), INTENT(IN) :: alpha_ecp, coeffs_ecp
INTEGER, DIMENSION(1:15, 0:10), INTENT(IN) :: nrpot_ecp
REAL(KIND=dp), INTENT(IN) :: rpgfc
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rab
REAL(KIND=dp), INTENT(IN) :: dab
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rac
REAL(KIND=dp), INTENT(IN) :: dac
REAL(KIND=dp), INTENT(IN) :: dbc
REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: vab
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: pab
REAL(KIND=dp), DIMENSION(3), INTENT(INOUT) :: force_a, force_b
#if defined(__LIBGRPP)
INTEGER :: a_offset, a_start, b_offset, b_start, i, &
ipgf, j, jpgf, li, lj, lk, ncoa, ncob, &
a_start_f, b_start_f, a_offset_f, b_offset_f
REAL(dp) :: expi, expj, normi, normj, prefi, prefj, &
zeti, zetj
REAL(dp), ALLOCATABLE, DIMENSION(:) :: tmp
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: vab_f, tmpx, tmpy, tmpz
REAL(dp), DIMENSION(3) :: ra, rb, rc
CALL libgrpp_init()
!Contains the integrals necessary for the forces, with angular momenta from lmin-1 to lmax+1
ALLOCATE (vab_f(npgfa*ncoset(la_max_set + 1), npgfb*ncoset(lb_max_set + 1)))
vab_f(:, :) = 0.0_dp
!libgrpp requires absolute positions, not relative ones
ra(:) = 0.0_dp
rb(:) = rab(:)
rc(:) = rac(:)
ALLOCATE (tmp(nco(la_max_set + 1)*nco(lb_max_set + 1)))
DO ipgf = 1, npgfa
IF (rpgfa(ipgf) + rpgfc < dac) CYCLE
zeti = zeta(ipgf)
a_start = (ipgf - 1)*ncoset(la_max_set)
a_start_f = (ipgf - 1)*ncoset(la_max_set + 1)
DO jpgf = 1, npgfb
IF (rpgfb(jpgf) + rpgfc < dbc) CYCLE
IF (rpgfa(ipgf) + rpgfb(jpgf) < dab) CYCLE
zetj = zetb(jpgf)
b_start = (jpgf - 1)*ncoset(lb_max_set)
b_start_f = (jpgf - 1)*ncoset(lb_max_set + 1)
DO li = MAX(0, la_min_set - 1), la_max_set + 1
a_offset = a_start + ncoset(li - 1)
a_offset_f = a_start_f + ncoset(li - 1)
ncoa = nco(li)
prefi = 2.0_dp**li*(2.0_dp/pi)**0.75_dp
expi = 0.25_dp*REAL(2*li + 3, dp)
normi = 1.0_dp/(prefi*zeti**expi)
DO lj = MAX(0, lb_min_set - 1), lb_max_set + 1
b_offset = b_start + ncoset(lj - 1)
b_offset_f = b_start_f + ncoset(lj - 1)
ncob = nco(lj)
prefj = 2.0_dp**lj*(2.0_dp/pi)**0.75_dp
expj = 0.25_dp*REAL(2*lj + 3, dp)
normj = 1.0_dp/(prefj*zetj**expj)
!Loop over ECP angular momentum
DO lk = 0, lmax_ecp
tmp(1:ncoa*ncob) = 0.0_dp
!libgrpp implicitely normalizes cartesian Gaussian. In CP2K, we do not, hence
!the 1/norm coefficients for PGFi and PGFj
CALL libgrpp_type2_integrals(ra, li, 1, [normi], [zeti], &
rb, lj, 1, [normj], [zetj], &
rc, lk, [npot_ecp(lk)], nrpot_ecp(:, lk), &
coeffs_ecp(:, lk), alpha_ecp(:, lk), tmp)
!the l+-1 integrals for gradient calculation
DO j = 1, ncob
DO i = 1, ncoa
vab_f(a_offset_f + i, b_offset_f + j) = &
vab_f(a_offset_f + i, b_offset_f + j) + tmp((i - 1)*ncob + j)
END DO
END DO
!the actual integrals
IF (li >= la_min_set .AND. li <= la_max_set .AND. lj >= lb_min_set .AND. lj <= lb_max_set) THEN
DO j = 1, ncob
DO i = 1, ncoa
vab(a_offset + i, b_offset + j) = vab(a_offset + i, b_offset + j) + tmp((i - 1)*ncob + j)
END DO
END DO
END IF
END DO !lk
END DO !lj
END DO !li
END DO !jpgf
END DO !ipgf
ALLOCATE (tmpx(npgfa*ncoset(la_max_set), npgfb*ncoset(lb_max_set)))
ALLOCATE (tmpy(npgfa*ncoset(la_max_set), npgfb*ncoset(lb_max_set)))
ALLOCATE (tmpz(npgfa*ncoset(la_max_set), npgfb*ncoset(lb_max_set)))
!Derivative wrt to center A
tmpx(:, :) = 0.0_dp
tmpy(:, :) = 0.0_dp
tmpz(:, :) = 0.0_dp
CALL dabdr(la_max_set, npgfa, zeta, rpgfa, la_min_set, lb_max_set, npgfb, rpgfb, lb_min_set, &
0.0_dp, vab_f, tmpx, tmpy, tmpz)
DO j = 1, npgfb*ncoset(lb_max_set)
DO i = 1, npgfa*ncoset(la_max_set)
force_a(1) = force_a(1) + tmpx(i, j)*pab(i, j)
force_a(2) = force_a(2) + tmpy(i, j)*pab(i, j)
force_a(3) = force_a(3) + tmpz(i, j)*pab(i, j)
END DO
END DO
!Derivative wrt to center B
tmpx(:, :) = 0.0_dp
tmpy(:, :) = 0.0_dp
tmpz(:, :) = 0.0_dp
CALL adbdr(la_max_set, npgfa, rpgfa, la_min_set, lb_max_set, npgfb, zetb, rpgfb, lb_min_set, &
0.0_dp, vab_f, tmpx, tmpy, tmpz)
DO j = 1, npgfb*ncoset(lb_max_set)
DO i = 1, npgfa*ncoset(la_max_set)
force_b(1) = force_b(1) + tmpx(i, j)*pab(i, j)
force_b(2) = force_b(2) + tmpy(i, j)*pab(i, j)
force_b(3) = force_b(3) + tmpz(i, j)*pab(i, j)
END DO
END DO
DEALLOCATE (tmpx, tmpy, tmpz)
#else
MARK_USED(la_max_set)
MARK_USED(la_min_set)
MARK_USED(npgfa)
MARK_USED(rpgfa)
MARK_USED(zeta)
MARK_USED(lb_max_set)
MARK_USED(lb_min_set)
MARK_USED(npgfb)
MARK_USED(rpgfb)
MARK_USED(zetb)
MARK_USED(lmax_ecp)
MARK_USED(npot_ecp)
MARK_USED(alpha_ecp)
MARK_USED(coeffs_ecp)
MARK_USED(nrpot_ecp)
MARK_USED(rpgfc)
MARK_USED(rab)
MARK_USED(dab)
MARK_USED(rac)
MARK_USED(dac)
MARK_USED(dbc)
MARK_USED(pab)
MARK_USED(vab)
MARK_USED(force_a)
MARK_USED(force_b)
CPABORT("Please compile CP2K with libgrpp support for calculations with ECPs")
#endif
END SUBROUTINE libgrpp_semilocal_forces_ref
END MODULE libgrpp_integrals