-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_moments.F
3295 lines (2881 loc) · 138 KB
/
qs_moments.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
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
!--------------------------------------------------------------------------------------------------!
! 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 Calculates the moment integrals <a|r^m|b> and <a|r x d/dr|b>
!> \par History
!> added angular moments (JGH 11.2012)
!> \author JGH (20.07.2006)
! **************************************************************************************************
MODULE qs_moments
USE ai_angmom, ONLY: angmom
USE ai_moments, ONLY: contract_cossin,&
cossin,&
diff_momop,&
diff_momop2,&
diff_momop_velocity,&
moment
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind
USE basis_set_types, ONLY: gto_basis_set_p_type,&
gto_basis_set_type
USE bibliography, ONLY: Mattiat2019,&
cite_reference
USE block_p_types, ONLY: block_p_type
USE cell_types, ONLY: cell_type,&
pbc
USE commutator_rpnl, ONLY: build_com_mom_nl
USE cp_cfm_basic_linalg, ONLY: cp_cfm_det
USE cp_cfm_types, ONLY: cp_cfm_create,&
cp_cfm_get_info,&
cp_cfm_release,&
cp_cfm_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_copy, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_distribution_type, dbcsr_dot, &
dbcsr_get_block_p, dbcsr_init_p, dbcsr_multiply, dbcsr_p_type, dbcsr_set, dbcsr_trace, &
dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
USE cp_dbcsr_operations, ONLY: cp_dbcsr_sm_fm_multiply,&
dbcsr_allocate_matrix_set,&
dbcsr_deallocate_matrix_set
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_double,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_info,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_type
USE cp_result_methods, ONLY: cp_results_erase,&
put_results
USE cp_result_types, ONLY: cp_result_type
USE distribution_1d_types, ONLY: distribution_1d_type
USE kinds, ONLY: default_string_length,&
dp
USE kpoint_types, ONLY: get_kpoint_info,&
kpoint_type
USE mathconstants, ONLY: pi,&
twopi
USE message_passing, ONLY: mp_para_env_type
USE moments_utils, ONLY: get_reference_point
USE orbital_pointers, ONLY: current_maxl,&
indco,&
ncoset
USE parallel_gemm_api, ONLY: parallel_gemm
USE particle_methods, ONLY: get_particle_set
USE particle_types, ONLY: particle_type
USE physcon, ONLY: bohr,&
debye
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_kind_types, ONLY: get_qs_kind,&
get_qs_kind_set,&
qs_kind_type
USE qs_ks_types, ONLY: get_ks_env,&
qs_ks_env_type
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE qs_neighbor_list_types, ONLY: get_iterator_info,&
neighbor_list_iterate,&
neighbor_list_iterator_create,&
neighbor_list_iterator_p_type,&
neighbor_list_iterator_release,&
neighbor_list_set_p_type
USE qs_operators_ao, ONLY: build_lin_mom_matrix
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE rt_propagation_types, ONLY: get_rtp,&
rt_prop_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_moments'
! Public subroutines
PUBLIC :: build_berry_moment_matrix, build_local_moment_matrix
PUBLIC :: build_berry_kpoint_matrix, build_local_magmom_matrix
PUBLIC :: qs_moment_berry_phase, qs_moment_locop
PUBLIC :: dipole_deriv_ao
PUBLIC :: build_local_moments_der_matrix
PUBLIC :: build_dsdv_moments
PUBLIC :: dipole_velocity_deriv
CONTAINS
! *****************************************************************************
!> \brief This returns the derivative of the moment integrals [a|\mu|b], with respect
!> to the basis function on the right
!> difdip(beta, alpha) = < mu | r_beta | ∂_alpha nu > * (mu - nu)
!> \param qs_env ...
!> \param difdip ...
!> \param order The order of the derivative (1 for dipole moment)
!> \param lambda The atom on which we take the derivative
!> \param rc ...
!> \author Edward Ditler
! **************************************************************************************************
SUBROUTINE dipole_velocity_deriv(qs_env, difdip, order, lambda, rc)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(INOUT) :: difdip
INTEGER, INTENT(IN) :: order, lambda
REAL(KIND=dp), DIMENSION(3) :: rc
CHARACTER(LEN=*), PARAMETER :: routineN = 'dipole_velocity_deriv'
INTEGER :: handle, i, iatom, icol, idir, ikind, inode, irow, iset, j, jatom, jkind, jset, &
last_jatom, lda, ldab, ldb, M_dim, maxsgf, natom, ncoa, ncob, nkind, nseta, nsetb, sgfa, &
sgfb
LOGICAL :: found
REAL(dp) :: dab
REAL(dp), DIMENSION(3) :: ra, rab, rac, rb, rbc
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: work
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: mab
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: difmab, difmab2
TYPE(block_p_type), ALLOCATABLE, DIMENSION(:, :) :: mint, mint2
TYPE(cell_type), POINTER :: cell
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_all
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
CALL timeset(routineN, handle)
NULLIFY (cell, particle_set, qs_kind_set, sab_all)
CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set, &
qs_kind_set=qs_kind_set, sab_all=sab_all)
CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
maxco=ldab, maxsgf=maxsgf)
nkind = SIZE(qs_kind_set)
natom = SIZE(particle_set)
M_dim = ncoset(order) - 1
ALLOCATE (basis_set_list(nkind))
ALLOCATE (mab(ldab, ldab, M_dim))
ALLOCATE (difmab2(ldab, ldab, M_dim, 3))
ALLOCATE (work(ldab, maxsgf))
ALLOCATE (mint(3, 3))
ALLOCATE (mint2(3, 3))
mab(1:ldab, 1:ldab, 1:M_dim) = 0.0_dp
difmab2(1:ldab, 1:ldab, 1:M_dim, 1:3) = 0.0_dp
work(1:ldab, 1:maxsgf) = 0.0_dp
DO i = 1, 3
DO j = 1, 3
NULLIFY (mint(i, j)%block)
NULLIFY (mint2(i, j)%block)
END DO
END DO
! Set the basis_set_list(nkind) to point to the corresponding basis sets
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
CALL neighbor_list_iterator_create(nl_iterator, sab_all)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, inode=inode, &
iatom=iatom, jatom=jatom, r=rab)
basis_set_a => basis_set_list(ikind)%gto_basis_set
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
ASSOCIATE ( &
! basis ikind
first_sgfa => basis_set_a%first_sgf, &
la_max => basis_set_a%lmax, &
la_min => basis_set_a%lmin, &
npgfa => basis_set_a%npgf, &
nsgfa => basis_set_a%nsgf_set, &
rpgfa => basis_set_a%pgf_radius, &
set_radius_a => basis_set_a%set_radius, &
sphi_a => basis_set_a%sphi, &
zeta => basis_set_a%zet, &
! basis jkind, &
first_sgfb => basis_set_b%first_sgf, &
lb_max => basis_set_b%lmax, &
lb_min => basis_set_b%lmin, &
npgfb => basis_set_b%npgf, &
nsgfb => basis_set_b%nsgf_set, &
rpgfb => basis_set_b%pgf_radius, &
set_radius_b => basis_set_b%set_radius, &
sphi_b => basis_set_b%sphi, &
zetb => basis_set_b%zet)
nseta = basis_set_a%nset
nsetb = basis_set_b%nset
IF (inode == 1) last_jatom = 0
! this guarantees minimum image convention
! anything else would not make sense
IF (jatom == last_jatom) THEN
CYCLE
END IF
last_jatom = jatom
irow = iatom
icol = jatom
DO i = 1, 3
DO j = 1, 3
NULLIFY (mint(i, j)%block)
CALL dbcsr_get_block_p(matrix=difdip(i, j)%matrix, &
row=irow, col=icol, BLOCK=mint(i, j)%block, &
found=found)
CPASSERT(found)
mint(i, j)%block = 0._dp
END DO
END DO
! With and without MIC, rab(:) is the vector giving us the coordinates of rb.
ra = pbc(particle_set(iatom)%r(:), cell)
rb(:) = ra(:) + rab(:)
rac = pbc(rc, ra, cell)
rbc = rac + rab
dab = norm2(rab)
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
DO jset = 1, nsetb
IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
ldab = MAX(ncoa, ncob)
lda = ncoset(la_max(iset))*npgfa(iset)
ldb = ncoset(lb_max(jset))*npgfb(jset)
ALLOCATE (difmab(lda, ldb, M_dim, 3))
! Calculate integral difmab(beta, alpha) = (a|r_beta|db_alpha)
! difmab(beta, alpha) = < a | r_beta | ∂_alpha b >
! difmab(j, idir) = < a | r_j | ∂_idir b >
CALL diff_momop_velocity(la_max(iset), npgfa(iset), zeta(:, iset), &
rpgfa(:, iset), la_min(iset), lb_max(jset), npgfb(jset), &
zetb(:, jset), rpgfb(:, jset), lb_min(jset), order, rac, rbc, &
difmab, lambda=lambda, iatom=iatom, jatom=jatom)
! *** Contraction step ***
DO idir = 1, 3 ! derivative of AO function
DO j = 1, 3 ! position operator r_j
CALL dgemm("N", "N", ncoa, nsgfb(jset), ncob, &
1.0_dp, difmab(1, 1, j, idir), SIZE(difmab, 1), &
sphi_b(1, sgfb), SIZE(sphi_b, 1), &
0.0_dp, work(1, 1), SIZE(work, 1))
CALL dgemm("T", "N", nsgfa(iset), nsgfb(jset), ncoa, &
1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
work(1, 1), SIZE(work, 1), &
1.0_dp, mint(j, idir)%block(sgfa, sgfb), &
SIZE(mint(j, idir)%block, 1))
END DO !j
END DO !idir
DEALLOCATE (difmab)
END DO !jset
END DO !iset
END ASSOCIATE
END DO!iterator
CALL neighbor_list_iterator_release(nl_iterator)
DO i = 1, 3
DO j = 1, 3
NULLIFY (mint(i, j)%block)
END DO
END DO
DEALLOCATE (mab, difmab2, basis_set_list, work, mint, mint2)
CALL timestop(handle)
END SUBROUTINE dipole_velocity_deriv
! **************************************************************************************************
!> \brief Builds the moments for the derivative of the overlap with respect to nuclear velocities
!> \param qs_env ...
!> \param moments ...
!> \param nmoments ...
!> \param ref_point ...
!> \param ref_points ...
!> \param basis_type ...
!> \author Edward Ditler
! **************************************************************************************************
SUBROUTINE build_dsdv_moments(qs_env, moments, nmoments, ref_point, ref_points, basis_type)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:) :: moments
INTEGER, INTENT(IN) :: nmoments
REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL :: ref_point
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
OPTIONAL :: ref_points
CHARACTER(len=*), OPTIONAL :: basis_type
CHARACTER(LEN=*), PARAMETER :: routineN = 'build_dsdv_moments'
INTEGER :: handle, i, iatom, icol, ikind, inode, irow, iset, jatom, jkind, jset, last_jatom, &
maxco, maxsgf, natom, ncoa, ncob, nkind, nm, nseta, nsetb, sgfa, sgfb
INTEGER, DIMENSION(3) :: image_cell
LOGICAL :: found
REAL(KIND=dp) :: dab
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: work
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: mab
REAL(KIND=dp), DIMENSION(3) :: ra, rab, rac, rb, rbc, rc
TYPE(block_p_type), ALLOCATABLE, DIMENSION(:) :: mint
TYPE(cell_type), POINTER :: cell
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
IF (nmoments < 1) RETURN
CALL timeset(routineN, handle)
NULLIFY (qs_kind_set, cell, particle_set, sab_orb)
nm = (6 + 11*nmoments + 6*nmoments**2 + nmoments**3)/6 - 1
CPASSERT(SIZE(moments) >= nm)
NULLIFY (qs_kind_set, particle_set, sab_orb, cell)
CALL get_qs_env(qs_env=qs_env, &
qs_kind_set=qs_kind_set, &
particle_set=particle_set, cell=cell, &
sab_orb=sab_orb)
nkind = SIZE(qs_kind_set)
natom = SIZE(particle_set)
! Allocate work storage
CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
maxco=maxco, maxsgf=maxsgf, &
basis_type=basis_type)
ALLOCATE (mab(maxco, maxco, nm))
mab(:, :, :) = 0.0_dp
ALLOCATE (work(maxco, maxsgf))
work(:, :) = 0.0_dp
ALLOCATE (mint(nm))
DO i = 1, nm
NULLIFY (mint(i)%block)
END DO
ALLOCATE (basis_set_list(nkind))
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a, basis_type=basis_type)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, inode=inode, &
iatom=iatom, jatom=jatom, r=rab, cell=image_cell)
basis_set_a => basis_set_list(ikind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
! basis ikind
ASSOCIATE ( &
first_sgfa => basis_set_a%first_sgf, &
la_max => basis_set_a%lmax, &
la_min => basis_set_a%lmin, &
npgfa => basis_set_a%npgf, &
nsgfa => basis_set_a%nsgf_set, &
rpgfa => basis_set_a%pgf_radius, &
set_radius_a => basis_set_a%set_radius, &
sphi_a => basis_set_a%sphi, &
zeta => basis_set_a%zet, &
! basis jkind, &
first_sgfb => basis_set_b%first_sgf, &
lb_max => basis_set_b%lmax, &
lb_min => basis_set_b%lmin, &
npgfb => basis_set_b%npgf, &
nsgfb => basis_set_b%nsgf_set, &
rpgfb => basis_set_b%pgf_radius, &
set_radius_b => basis_set_b%set_radius, &
sphi_b => basis_set_b%sphi, &
zetb => basis_set_b%zet)
nseta = basis_set_a%nset
nsetb = basis_set_b%nset
IF (inode == 1) last_jatom = 0
! this guarantees minimum image convention
! anything else would not make sense
IF (jatom == last_jatom) THEN
CYCLE
END IF
last_jatom = jatom
IF (iatom <= jatom) THEN
irow = iatom
icol = jatom
ELSE
irow = jatom
icol = iatom
END IF
DO i = 1, nm
NULLIFY (mint(i)%block)
CALL dbcsr_get_block_p(matrix=moments(i)%matrix, &
row=irow, col=icol, BLOCK=mint(i)%block, found=found)
mint(i)%block = 0._dp
END DO
! fold atomic position back into unit cell
IF (PRESENT(ref_points)) THEN
rc(:) = 0.5_dp*(ref_points(:, iatom) + ref_points(:, jatom))
ELSE IF (PRESENT(ref_point)) THEN
rc(:) = ref_point(:)
ELSE
rc(:) = 0._dp
END IF
! using PBC here might screw a molecule that fits the box (but e.g. hasn't been shifted by center_molecule)
! by folding around the center, such screwing can be avoided for a proper choice of center.
! we dont use PBC at this point
! With and without MIC, rab(:) is the vector giving us the coordinates of rb.
ra(:) = particle_set(iatom)%r(:)
rb(:) = ra(:) + rab(:)
rac = pbc(rc, ra, cell)
rbc = rac + rab
dab = NORM2(rab)
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
DO jset = 1, nsetb
IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
! Calculate the primitive integrals
CALL moment(la_max(iset), npgfa(iset), zeta(:, iset), &
rpgfa(:, iset), la_min(iset), &
lb_max(jset), npgfb(jset), zetb(:, jset), &
rpgfb(:, jset), nmoments, rac, rbc, mab)
! Contraction step
DO i = 1, nm
CALL dgemm("N", "N", ncoa, nsgfb(jset), ncob, &
1.0_dp, mab(1, 1, i), SIZE(mab, 1), &
sphi_b(1, sgfb), SIZE(sphi_b, 1), &
0.0_dp, work(1, 1), SIZE(work, 1))
IF (iatom <= jatom) THEN
CALL dgemm("T", "N", nsgfa(iset), nsgfb(jset), ncoa, &
1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
work(1, 1), SIZE(work, 1), &
1.0_dp, mint(i)%block(sgfa, sgfb), &
SIZE(mint(i)%block, 1))
ELSE
CALL dgemm("T", "N", nsgfb(jset), nsgfa(iset), ncoa, &
1.0_dp, work(1, 1), SIZE(work, 1), &
sphi_a(1, sgfa), SIZE(sphi_a, 1), &
1.0_dp, mint(i)%block(sgfb, sgfa), &
SIZE(mint(i)%block, 1))
END IF
END DO
END DO
END DO
END ASSOCIATE
END DO ! iterator
CALL neighbor_list_iterator_release(nl_iterator)
! Release work storage
DEALLOCATE (mab, basis_set_list)
DEALLOCATE (work)
DO i = 1, nm
NULLIFY (mint(i)%block)
END DO
DEALLOCATE (mint)
CALL timestop(handle)
END SUBROUTINE build_dsdv_moments
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param moments ...
!> \param nmoments ...
!> \param ref_point ...
!> \param ref_points ...
!> \param basis_type ...
! **************************************************************************************************
SUBROUTINE build_local_moment_matrix(qs_env, moments, nmoments, ref_point, ref_points, basis_type)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: moments
INTEGER, INTENT(IN) :: nmoments
REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL :: ref_point
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN), &
OPTIONAL :: ref_points
CHARACTER(len=*), OPTIONAL :: basis_type
CHARACTER(LEN=*), PARAMETER :: routineN = 'build_local_moment_matrix'
INTEGER :: handle, i, iatom, icol, ikind, inode, irow, iset, jatom, jkind, jset, last_jatom, &
maxco, maxsgf, natom, ncoa, ncob, nkind, nm, nseta, nsetb, sgfa, sgfb
LOGICAL :: found
REAL(KIND=dp) :: dab
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: work
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: mab
REAL(KIND=dp), DIMENSION(3) :: ra, rab, rac, rb, rbc, rc
TYPE(block_p_type), ALLOCATABLE, DIMENSION(:) :: mint
TYPE(cell_type), POINTER :: cell
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
IF (nmoments < 1) RETURN
CALL timeset(routineN, handle)
NULLIFY (qs_kind_set, cell, particle_set, sab_orb)
nm = (6 + 11*nmoments + 6*nmoments**2 + nmoments**3)/6 - 1
CPASSERT(SIZE(moments) >= nm)
NULLIFY (qs_kind_set, particle_set, sab_orb, cell)
CALL get_qs_env(qs_env=qs_env, &
qs_kind_set=qs_kind_set, &
particle_set=particle_set, cell=cell, &
sab_orb=sab_orb)
nkind = SIZE(qs_kind_set)
natom = SIZE(particle_set)
! Allocate work storage
CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
maxco=maxco, maxsgf=maxsgf, &
basis_type=basis_type)
ALLOCATE (mab(maxco, maxco, nm))
mab(:, :, :) = 0.0_dp
ALLOCATE (work(maxco, maxsgf))
work(:, :) = 0.0_dp
ALLOCATE (mint(nm))
DO i = 1, nm
NULLIFY (mint(i)%block)
END DO
ALLOCATE (basis_set_list(nkind))
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a, basis_type=basis_type)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, inode=inode, &
iatom=iatom, jatom=jatom, r=rab)
basis_set_a => basis_set_list(ikind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
ASSOCIATE ( &
! basis ikind
first_sgfa => basis_set_a%first_sgf, &
la_max => basis_set_a%lmax, &
la_min => basis_set_a%lmin, &
npgfa => basis_set_a%npgf, &
nsgfa => basis_set_a%nsgf_set, &
rpgfa => basis_set_a%pgf_radius, &
set_radius_a => basis_set_a%set_radius, &
sphi_a => basis_set_a%sphi, &
zeta => basis_set_a%zet, &
! basis jkind, &
first_sgfb => basis_set_b%first_sgf, &
lb_max => basis_set_b%lmax, &
lb_min => basis_set_b%lmin, &
npgfb => basis_set_b%npgf, &
nsgfb => basis_set_b%nsgf_set, &
rpgfb => basis_set_b%pgf_radius, &
set_radius_b => basis_set_b%set_radius, &
sphi_b => basis_set_b%sphi, &
zetb => basis_set_b%zet)
nseta = basis_set_a%nset
nsetb = basis_set_b%nset
IF (inode == 1) last_jatom = 0
! this guarantees minimum image convention
! anything else would not make sense
IF (jatom == last_jatom) THEN
CYCLE
END IF
last_jatom = jatom
IF (iatom <= jatom) THEN
irow = iatom
icol = jatom
ELSE
irow = jatom
icol = iatom
END IF
DO i = 1, nm
NULLIFY (mint(i)%block)
CALL dbcsr_get_block_p(matrix=moments(i)%matrix, &
row=irow, col=icol, BLOCK=mint(i)%block, found=found)
mint(i)%block = 0._dp
END DO
! fold atomic position back into unit cell
IF (PRESENT(ref_points)) THEN
rc(:) = 0.5_dp*(ref_points(:, iatom) + ref_points(:, jatom))
ELSE IF (PRESENT(ref_point)) THEN
rc(:) = ref_point(:)
ELSE
rc(:) = 0._dp
END IF
! using PBC here might screw a molecule that fits the box (but e.g. hasn't been shifted by center_molecule)
! by folding around the center, such screwing can be avoided for a proper choice of center.
ra(:) = pbc(particle_set(iatom)%r(:) - rc, cell) + rc
rb(:) = pbc(particle_set(jatom)%r(:) - rc, cell) + rc
! we dont use PBC at this point
rab(:) = ra(:) - rb(:)
rac(:) = ra(:) - rc(:)
rbc(:) = rb(:) - rc(:)
dab = NORM2(rab)
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
DO jset = 1, nsetb
IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
! Calculate the primitive integrals
CALL moment(la_max(iset), npgfa(iset), zeta(:, iset), &
rpgfa(:, iset), la_min(iset), &
lb_max(jset), npgfb(jset), zetb(:, jset), &
rpgfb(:, jset), nmoments, rac, rbc, mab)
! Contraction step
DO i = 1, nm
CALL dgemm("N", "N", ncoa, nsgfb(jset), ncob, &
1.0_dp, mab(1, 1, i), SIZE(mab, 1), &
sphi_b(1, sgfb), SIZE(sphi_b, 1), &
0.0_dp, work(1, 1), SIZE(work, 1))
IF (iatom <= jatom) THEN
CALL dgemm("T", "N", nsgfa(iset), nsgfb(jset), ncoa, &
1.0_dp, sphi_a(1, sgfa), SIZE(sphi_a, 1), &
work(1, 1), SIZE(work, 1), &
1.0_dp, mint(i)%block(sgfa, sgfb), &
SIZE(mint(i)%block, 1))
ELSE
CALL dgemm("T", "N", nsgfb(jset), nsgfa(iset), ncoa, &
1.0_dp, work(1, 1), SIZE(work, 1), &
sphi_a(1, sgfa), SIZE(sphi_a, 1), &
1.0_dp, mint(i)%block(sgfb, sgfa), &
SIZE(mint(i)%block, 1))
END IF
END DO
END DO
END DO
END ASSOCIATE
END DO
CALL neighbor_list_iterator_release(nl_iterator)
! Release work storage
DEALLOCATE (mab, basis_set_list)
DEALLOCATE (work)
DO i = 1, nm
NULLIFY (mint(i)%block)
END DO
DEALLOCATE (mint)
CALL timestop(handle)
END SUBROUTINE build_local_moment_matrix
! **************************************************************************************************
!> \brief Calculate right-hand sided derivatives of multipole moments, e. g. < a | xy d/dz | b >
!> Optionally stores the multipole moments themselves for free.
!> Note that the multipole moments are symmetric while their derivatives are anti-symmetric
!> Only first derivatives are performed, e. g. x d/dy
!> \param qs_env ...
!> \param moments_der will contain the derivatives of the multipole moments
!> \param nmoments_der order of the moments with derivatives
!> \param nmoments order of the multipole moments (no derivatives, same output as
!> build_local_moment_matrix, needs moments as arguments to store results)
!> \param ref_point ...
!> \param moments contains the multipole moments, optionally for free, up to order nmoments
!> \note
!> Adapted from rRc_xyz_der_ao in qs_operators_ao
! **************************************************************************************************
SUBROUTINE build_local_moments_der_matrix(qs_env, moments_der, nmoments_der, nmoments, &
ref_point, moments)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:, :), &
INTENT(INOUT), POINTER :: moments_der
INTEGER, INTENT(IN) :: nmoments_der, nmoments
REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL :: ref_point
TYPE(dbcsr_p_type), DIMENSION(:), INTENT(INOUT), &
OPTIONAL, POINTER :: moments
CHARACTER(LEN=*), PARAMETER :: routineN = 'build_local_moments_der_matrix'
INTEGER :: dimders, handle, i, iatom, icol, ider, ii, ikind, inode, ipgf, irow, iset, j, &
jatom, jkind, jpgf, jset, last_jatom, M_dim, maxco, maxsgf, na, nb, ncoa, ncob, nda, ndb, &
nders, nkind, nm, nmom_build, nseta, nsetb, sgfa, sgfb
LOGICAL :: found
REAL(KIND=dp) :: dab
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: work
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: mab
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: difmab
REAL(KIND=dp), DIMENSION(3) :: ra, rab, rac, rb, rbc, rc
REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: mab_tmp
TYPE(block_p_type), ALLOCATABLE, DIMENSION(:) :: mom_block
TYPE(block_p_type), ALLOCATABLE, DIMENSION(:, :) :: mom_block_der
TYPE(cell_type), POINTER :: cell
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
nmom_build = MAX(nmoments, nmoments_der) ! build moments up to order nmom_buiod
IF (nmom_build < 1) RETURN
CALL timeset(routineN, handle)
nders = 1 ! only first order derivatives
dimders = ncoset(nders) - 1
NULLIFY (qs_kind_set, particle_set, sab_orb, cell)
CALL get_qs_env(qs_env=qs_env, &
qs_kind_set=qs_kind_set, &
particle_set=particle_set, &
cell=cell, &
sab_orb=sab_orb)
nkind = SIZE(qs_kind_set)
! Work storage
CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
maxco=maxco, maxsgf=maxsgf)
IF (nmoments > 0) THEN
CPASSERT(PRESENT(moments))
nm = (6 + 11*nmoments + 6*nmoments**2 + nmoments**3)/6 - 1
CPASSERT(SIZE(moments) == nm)
! storage for integrals
ALLOCATE (mab(maxco, maxco, nm))
! blocks
mab(:, :, :) = 0.0_dp
ALLOCATE (mom_block(nm))
DO i = 1, nm
NULLIFY (mom_block(i)%block)
END DO
END IF
IF (nmoments_der > 0) THEN
M_dim = ncoset(nmoments_der) - 1
CPASSERT(SIZE(moments_der, dim=1) == M_dim)
CPASSERT(SIZE(moments_der, dim=2) == dimders)
! storage for integrals
ALLOCATE (difmab(maxco, maxco, M_dim, dimders))
difmab(:, :, :, :) = 0.0_dp
! blocks
ALLOCATE (mom_block_der(M_dim, dimders))
DO i = 1, M_dim
DO ider = 1, dimders
NULLIFY (mom_block_der(i, ider)%block)
END DO
END DO
END IF
ALLOCATE (work(maxco, maxsgf))
work(:, :) = 0.0_dp
NULLIFY (basis_set_a, basis_set_b, basis_set_list)
NULLIFY (qs_kind)
ALLOCATE (basis_set_list(nkind))
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
! Calculate derivatives looping over neighbour list
NULLIFY (nl_iterator)
CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, inode=inode, &
iatom=iatom, jatom=jatom, r=rab)
basis_set_a => basis_set_list(ikind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
ASSOCIATE ( &
! basis ikind
first_sgfa => basis_set_a%first_sgf, &
la_max => basis_set_a%lmax, &
la_min => basis_set_a%lmin, &
npgfa => basis_set_a%npgf, &
nsgfa => basis_set_a%nsgf_set, &
rpgfa => basis_set_a%pgf_radius, &
set_radius_a => basis_set_a%set_radius, &
sphi_a => basis_set_a%sphi, &
zeta => basis_set_a%zet, &
! basis jkind, &
first_sgfb => basis_set_b%first_sgf, &
lb_max => basis_set_b%lmax, &
lb_min => basis_set_b%lmin, &
npgfb => basis_set_b%npgf, &
nsgfb => basis_set_b%nsgf_set, &
rpgfb => basis_set_b%pgf_radius, &
set_radius_b => basis_set_b%set_radius, &
sphi_b => basis_set_b%sphi, &
zetb => basis_set_b%zet)
nseta = basis_set_a%nset
nsetb = basis_set_b%nset
! reference point
IF (PRESENT(ref_point)) THEN
rc(:) = ref_point(:)
ELSE
rc(:) = 0._dp
END IF
! using PBC here might screw a molecule that fits the box (but e.g. hasn't been shifted by center_molecule)
! by folding around the center, such screwing can be avoided for a proper choice of center.
ra(:) = pbc(particle_set(iatom)%r(:) - rc, cell) + rc
rb(:) = pbc(particle_set(jatom)%r(:) - rc, cell) + rc
! we dont use PBC at this point
rab(:) = ra(:) - rb(:)
rac(:) = ra(:) - rc(:)
rbc(:) = rb(:) - rc(:)
dab = NORM2(rab)
! get blocks
IF (inode == 1) last_jatom = 0
IF (jatom == last_jatom) THEN
CYCLE
END IF
last_jatom = jatom
IF (iatom <= jatom) THEN
irow = iatom
icol = jatom
ELSE
irow = jatom
icol = iatom
END IF
IF (nmoments > 0) THEN
DO i = 1, nm
NULLIFY (mom_block(i)%block)
! get block from pre calculated overlap matrix
CALL dbcsr_get_block_p(matrix=moments(i)%matrix, &
row=irow, col=icol, BLOCK=mom_block(i)%block, found=found)
CPASSERT(found .AND. ASSOCIATED(mom_block(i)%block))
mom_block(i)%block = 0._dp
END DO
END IF
IF (nmoments_der > 0) THEN
DO i = 1, M_dim
DO ider = 1, dimders
NULLIFY (mom_block_der(i, ider)%block)
CALL dbcsr_get_block_p(matrix=moments_der(i, ider)%matrix, &
row=irow, col=icol, &
block=mom_block_der(i, ider)%block, &
found=found)
CPASSERT(found .AND. ASSOCIATED(mom_block_der(i, ider)%block))
mom_block_der(i, ider)%block = 0._dp
END DO
END DO
END IF
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
DO jset = 1, nsetb
IF (set_radius_a(iset) + set_radius_b(jset) < dab) CYCLE
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
NULLIFY (mab_tmp)
ALLOCATE (mab_tmp(npgfa(iset)*ncoset(la_max(iset) + 1), &
npgfb(jset)*ncoset(lb_max(jset) + 1), ncoset(nmom_build) - 1))
! Calculate the primitive integrals (need l+1 for derivatives)
CALL moment(la_max(iset) + 1, npgfa(iset), zeta(:, iset), &
rpgfa(:, iset), la_min(iset), &