-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpa_im_time_force_methods.F
3999 lines (3465 loc) · 200 KB
/
rpa_im_time_force_methods.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 Routines needed for cubic-scaling RPA and SOS-Laplace-MP2 forces
!> \author Augustin Bussy
! **************************************************************************************************
MODULE rpa_im_time_force_methods
USE admm_methods, ONLY: admm_projection_derivative
USE admm_types, ONLY: admm_type,&
get_admm_env
USE ao_util, ONLY: exp_radius_very_extended
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind_set
USE basis_set_types, ONLY: gto_basis_set_p_type,&
gto_basis_set_type
USE bibliography, ONLY: Bussy2023,&
cite_reference
USE cell_types, ONLY: cell_type,&
pbc
USE core_ae, ONLY: build_core_ae
USE core_ppl, ONLY: build_core_ppl
USE core_ppnl, ONLY: build_core_ppnl
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_add_on_diag, dbcsr_clear, dbcsr_complete_redistribute, dbcsr_copy, &
dbcsr_create, dbcsr_distribution_new, dbcsr_distribution_release, dbcsr_distribution_type, &
dbcsr_frobenius_norm, dbcsr_get_block_p, dbcsr_iterator_blocks_left, &
dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
dbcsr_multiply, dbcsr_p_type, dbcsr_release, dbcsr_scale, dbcsr_set, dbcsr_type, &
dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
USE cp_dbcsr_cholesky, ONLY: cp_dbcsr_cholesky_decompose,&
cp_dbcsr_cholesky_invert
USE cp_dbcsr_diag, ONLY: cp_dbcsr_power
USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
copy_fm_to_dbcsr,&
cp_dbcsr_dist2d_to_dist,&
cp_dbcsr_sm_fm_multiply,&
dbcsr_allocate_matrix_set,&
dbcsr_deallocate_matrix_set
USE cp_eri_mme_interface, ONLY: cp_eri_mme_update_local_counts
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_to_fm,&
cp_fm_type
USE dbt_api, ONLY: &
dbt_batched_contract_finalize, dbt_batched_contract_init, dbt_clear, dbt_contract, &
dbt_copy, dbt_copy_matrix_to_tensor, dbt_copy_tensor_to_matrix, dbt_create, dbt_destroy, &
dbt_filter, dbt_get_info, dbt_mp_environ_pgrid, dbt_pgrid_create, dbt_pgrid_destroy, &
dbt_pgrid_type, dbt_scale, dbt_type
USE distribution_2d_types, ONLY: distribution_2d_type
USE ec_methods, ONLY: create_kernel
USE gaussian_gridlevels, ONLY: gaussian_gridlevel
USE hfx_admm_utils, ONLY: tddft_hfx_matrix
USE hfx_derivatives, ONLY: derivatives_four_center
USE hfx_exx, ONLY: add_exx_to_rhs
USE hfx_ri, ONLY: get_2c_der_force,&
get_force_from_3c_trace,&
get_idx_to_atom,&
hfx_ri_update_forces
USE hfx_types, ONLY: alloc_containers,&
block_ind_type,&
dealloc_containers,&
hfx_compression_type,&
hfx_type
USE input_constants, ONLY: do_admm_aux_exch_func_none,&
do_eri_gpw,&
do_eri_mme,&
do_potential_id,&
ri_rpa_method_gpw
USE input_section_types, ONLY: section_vals_get,&
section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE iterate_matrix, ONLY: matrix_exponential
USE kinds, ONLY: dp,&
int_8
USE libint_2c_3c, ONLY: libint_potential_type
USE machine, ONLY: m_flush,&
m_walltime
USE mathconstants, ONLY: fourpi
USE message_passing, ONLY: mp_cart_type,&
mp_para_env_release,&
mp_para_env_type
USE mp2_eri, ONLY: integrate_set_2c
USE mp2_eri_gpw, ONLY: calc_potential_gpw,&
cleanup_gpw,&
prepare_gpw,&
virial_gpw_potential
USE mp2_types, ONLY: mp2_type
USE orbital_pointers, ONLY: ncoset
USE parallel_gemm_api, ONLY: parallel_gemm
USE particle_methods, ONLY: get_particle_set
USE particle_types, ONLY: particle_type
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_methods, ONLY: pw_axpy,&
pw_copy,&
pw_integral_ab,&
pw_scale,&
pw_transfer,&
pw_zero
USE pw_poisson_methods, ONLY: pw_poisson_solve
USE pw_poisson_types, ONLY: pw_poisson_type
USE pw_pool_types, ONLY: pw_pool_type
USE pw_types, ONLY: pw_c1d_gs_type,&
pw_r3d_rs_type
USE qs_collocate_density, ONLY: calculate_rho_elec,&
collocate_function
USE qs_density_matrices, ONLY: calculate_whz_matrix
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_force_types, ONLY: qs_force_type
USE qs_integral_utils, ONLY: basis_set_list_setup
USE qs_integrate_potential, ONLY: integrate_pgf_product,&
integrate_v_core_rspace,&
integrate_v_rspace
USE qs_interactions, ONLY: init_interaction_radii_orb_basis
USE qs_kind_types, ONLY: qs_kind_type
USE qs_kinetic, ONLY: build_kinetic_matrix
USE qs_ks_methods, ONLY: calc_rho_tot_gspace
USE qs_ks_reference, ONLY: ks_ref_potential
USE qs_ks_types, ONLY: set_ks_env
USE qs_linres_types, ONLY: linres_control_type
USE qs_matrix_w, ONLY: compute_matrix_w
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type,&
release_neighbor_list_sets
USE qs_overlap, ONLY: build_overlap_matrix
USE qs_p_env_methods, ONLY: p_env_create,&
p_env_psi0_changed
USE qs_p_env_types, ONLY: p_env_release,&
qs_p_env_type
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE qs_tensors, ONLY: &
build_2c_derivatives, build_2c_integrals, build_2c_neighbor_lists, build_3c_derivatives, &
build_3c_neighbor_lists, calc_2c_virial, calc_3c_virial, compress_tensor, &
decompress_tensor, get_tensor_occupancy, neighbor_list_3c_destroy
USE qs_tensors_types, ONLY: create_2c_tensor,&
create_3c_tensor,&
create_tensor_batches,&
distribution_3d_create,&
distribution_3d_type,&
neighbor_list_3c_type
USE realspace_grid_types, ONLY: map_gaussian_here,&
realspace_grid_type
USE response_solver, ONLY: response_equation_new
USE rpa_im_time, ONLY: compute_mat_dm_global
USE rpa_im_time_force_types, ONLY: im_time_force_type
USE rs_pw_interface, ONLY: potential_pw2rs
USE task_list_types, ONLY: task_list_type
USE virial_types, ONLY: virial_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rpa_im_time_force_methods'
PUBLIC :: init_im_time_forces, calc_laplace_loop_forces, calc_post_loop_forces, &
keep_initial_quad, calc_rpa_loop_forces
CONTAINS
! **************************************************************************************************
!> \brief Initializes and pre-calculates all needed tensors for the forces
!> \param force_data ...
!> \param fm_matrix_PQ ...
!> \param t_3c_M the 3-center M tensor to be used as a template
!> \param unit_nr ...
!> \param mp2_env ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE init_im_time_forces(force_data, fm_matrix_PQ, t_3c_M, unit_nr, mp2_env, qs_env)
TYPE(im_time_force_type), INTENT(INOUT) :: force_data
TYPE(cp_fm_type), INTENT(IN) :: fm_matrix_PQ
TYPE(dbt_type), INTENT(INOUT) :: t_3c_M
INTEGER, INTENT(IN) :: unit_nr
TYPE(mp2_type) :: mp2_env
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'init_im_time_forces'
INTEGER :: handle, i_mem, i_xyz, ibasis, ispin, &
n_dependent, n_mem, n_rep, natom, &
nkind, nspins
INTEGER(int_8) :: nze, nze_tot
INTEGER, ALLOCATABLE, DIMENSION(:) :: dist1, dist2, dist_AO_1, dist_AO_2, &
dist_RI, dummy_end, dummy_start, &
end_blocks, sizes_AO, sizes_RI, &
start_blocks
INTEGER, DIMENSION(2) :: pdims_t2c
INTEGER, DIMENSION(3) :: nblks_total, pcoord, pdims, pdims_t3c
INTEGER, DIMENSION(:), POINTER :: col_bsize, row_bsize
LOGICAL :: do_periodic, use_virial
REAL(dp) :: compression_factor, eps_pgf_orb, &
eps_pgf_orb_old, memory, occ
TYPE(cell_type), POINTER :: cell
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(dbcsr_distribution_type) :: dbcsr_dist
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, rho_ao
TYPE(dbcsr_type) :: dbcsr_work, dbcsr_work2, dbcsr_work3
TYPE(dbcsr_type), DIMENSION(1) :: t_2c_int_tmp
TYPE(dbcsr_type), DIMENSION(1, 3) :: t_2c_der_tmp
TYPE(dbt_pgrid_type) :: pgrid_t2c, pgrid_t3c
TYPE(dbt_type) :: t_2c_template, t_2c_tmp, t_3c_template
TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :, :) :: t_3c_der_AO_prv, t_3c_der_RI_prv
TYPE(dft_control_type), POINTER :: dft_control
TYPE(distribution_2d_type), POINTER :: dist_2d
TYPE(distribution_3d_type) :: dist_3d, dist_vir
TYPE(gto_basis_set_p_type), ALLOCATABLE, &
DIMENSION(:), TARGET :: basis_set_ao, basis_set_ri_aux
TYPE(gto_basis_set_type), POINTER :: orb_basis, ri_basis
TYPE(libint_potential_type) :: identity_pot
TYPE(mp_cart_type) :: mp_comm_t3c, mp_comm_vir
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_3c_type) :: nl_3c
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: nl_2c
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_rho_type), POINTER :: rho
TYPE(section_vals_type), POINTER :: qs_section
TYPE(virial_type), POINTER :: virial
NULLIFY (dft_control, para_env, particle_set, qs_kind_set, dist_2d, nl_2c, blacs_env, matrix_s, &
rho, rho_ao, cell, qs_section, orb_basis, ri_basis, virial)
CALL cite_reference(Bussy2023)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, natom=natom, nkind=nkind, dft_control=dft_control, para_env=para_env, &
particle_set=particle_set, qs_kind_set=qs_kind_set, cell=cell, virial=virial)
IF (dft_control%qs_control%gapw) THEN
CPABORT("Low-scaling RPA/SOS-MP2 forces only available with GPW")
END IF
use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
do_periodic = .FALSE.
IF (ANY(cell%perd == 1)) do_periodic = .TRUE.
force_data%do_periodic = do_periodic
!Dealing with the 3-center derivatives
pdims_t3c = 0
CALL dbt_pgrid_create(para_env, pdims_t3c, pgrid_t3c)
!Make sure we use the proper QS EPS_PGF_ORB values
qs_section => section_vals_get_subs_vals(qs_env%input, "DFT%QS")
CALL section_vals_val_get(qs_section, "EPS_PGF_ORB", n_rep_val=n_rep)
IF (n_rep /= 0) THEN
CALL section_vals_val_get(qs_section, "EPS_PGF_ORB", r_val=eps_pgf_orb)
ELSE
CALL section_vals_val_get(qs_section, "EPS_DEFAULT", r_val=eps_pgf_orb)
eps_pgf_orb = SQRT(eps_pgf_orb)
END IF
eps_pgf_orb_old = dft_control%qs_control%eps_pgf_orb
ALLOCATE (sizes_RI(natom), sizes_AO(natom))
ALLOCATE (basis_set_ri_aux(nkind), basis_set_ao(nkind))
CALL basis_set_list_setup(basis_set_ri_aux, "RI_AUX", qs_kind_set)
CALL get_particle_set(particle_set, qs_kind_set, nsgf=sizes_RI, basis=basis_set_ri_aux)
CALL basis_set_list_setup(basis_set_ao, "ORB", qs_kind_set)
CALL get_particle_set(particle_set, qs_kind_set, nsgf=sizes_AO, basis=basis_set_ao)
DO ibasis = 1, SIZE(basis_set_ao)
orb_basis => basis_set_ao(ibasis)%gto_basis_set
CALL init_interaction_radii_orb_basis(orb_basis, eps_pgf_orb)
ri_basis => basis_set_ri_aux(ibasis)%gto_basis_set
CALL init_interaction_radii_orb_basis(ri_basis, eps_pgf_orb)
END DO
CALL create_3c_tensor(t_3c_template, dist_RI, dist_AO_1, dist_AO_2, pgrid_t3c, &
sizes_RI, sizes_AO, sizes_AO, map1=[1], map2=[2, 3], name="der (RI AO | AO)")
ALLOCATE (t_3c_der_RI_prv(1, 1, 3), t_3c_der_AO_prv(1, 1, 3))
DO i_xyz = 1, 3
CALL dbt_create(t_3c_template, t_3c_der_RI_prv(1, 1, i_xyz))
CALL dbt_create(t_3c_template, t_3c_der_AO_prv(1, 1, i_xyz))
END DO
IF (use_virial) THEN
ALLOCATE (force_data%t_3c_virial, force_data%t_3c_virial_split)
CALL dbt_create(t_3c_template, force_data%t_3c_virial)
CALL dbt_create(t_3c_M, force_data%t_3c_virial_split)
END IF
CALL dbt_destroy(t_3c_template)
CALL dbt_mp_environ_pgrid(pgrid_t3c, pdims, pcoord)
CALL mp_comm_t3c%create(pgrid_t3c%mp_comm_2d, 3, pdims)
CALL distribution_3d_create(dist_3d, dist_RI, dist_AO_1, dist_AO_2, &
nkind, particle_set, mp_comm_t3c, own_comm=.TRUE.)
!In case of virial, we need to store the 3c_nl
IF (use_virial) THEN
ALLOCATE (force_data%nl_3c)
CALL mp_comm_vir%create(pgrid_t3c%mp_comm_2d, 3, pdims)
CALL distribution_3d_create(dist_vir, dist_RI, dist_AO_1, dist_AO_2, &
nkind, particle_set, mp_comm_vir, own_comm=.TRUE.)
CALL build_3c_neighbor_lists(force_data%nl_3c, basis_set_ri_aux, basis_set_ao, basis_set_ao, &
dist_vir, mp2_env%ri_metric, "RPA_3c_nl", qs_env, op_pos=1, &
sym_jk=.FALSE., own_dist=.TRUE.)
END IF
CALL build_3c_neighbor_lists(nl_3c, basis_set_ri_aux, basis_set_ao, basis_set_ao, dist_3d, &
mp2_env%ri_metric, "RPA_3c_nl", qs_env, op_pos=1, sym_jk=.TRUE., &
own_dist=.TRUE.)
DEALLOCATE (dist_RI, dist_AO_1, dist_AO_2)
!Prepare the resulting 3c tensors in the format of t_3c_M for compatible traces: (RI|AO AO), split blocks
CALL dbt_get_info(t_3c_M, nblks_total=nblks_total)
ALLOCATE (force_data%bsizes_RI_split(nblks_total(1)), force_data%bsizes_AO_split(nblks_total(2)))
CALL dbt_get_info(t_3c_M, blk_size_1=force_data%bsizes_RI_split, blk_size_2=force_data%bsizes_AO_split)
DO i_xyz = 1, 3
CALL dbt_create(t_3c_M, force_data%t_3c_der_RI(i_xyz))
CALL dbt_create(t_3c_M, force_data%t_3c_der_AO(i_xyz))
END DO
!Keep track of atom index corresponding to split blocks
ALLOCATE (force_data%idx_to_at_RI(nblks_total(1)))
CALL get_idx_to_atom(force_data%idx_to_at_RI, force_data%bsizes_RI_split, sizes_RI)
ALLOCATE (force_data%idx_to_at_AO(nblks_total(2)))
CALL get_idx_to_atom(force_data%idx_to_at_AO, force_data%bsizes_AO_split, sizes_AO)
n_mem = mp2_env%ri_rpa_im_time%cut_memory
CALL create_tensor_batches(sizes_RI, n_mem, dummy_start, dummy_end, start_blocks, end_blocks)
DEALLOCATE (dummy_start, dummy_end)
ALLOCATE (force_data%t_3c_der_AO_comp(n_mem, 3), force_data%t_3c_der_RI_comp(n_mem, 3))
ALLOCATE (force_data%t_3c_der_AO_ind(n_mem, 3), force_data%t_3c_der_RI_ind(n_mem, 3))
memory = 0.0_dp
nze_tot = 0
DO i_mem = 1, n_mem
CALL build_3c_derivatives(t_3c_der_RI_prv, t_3c_der_AO_prv, mp2_env%ri_rpa_im_time%eps_filter, &
qs_env, nl_3c, basis_set_ri_aux, basis_set_ao, basis_set_ao, &
mp2_env%ri_metric, der_eps=mp2_env%ri_rpa_im_time%eps_filter, op_pos=1, &
bounds_i=[start_blocks(i_mem), end_blocks(i_mem)])
DO i_xyz = 1, 3
CALL dbt_copy(t_3c_der_RI_prv(1, 1, i_xyz), force_data%t_3c_der_RI(i_xyz), move_data=.TRUE.)
CALL dbt_filter(force_data%t_3c_der_RI(i_xyz), mp2_env%ri_rpa_im_time%eps_filter)
CALL get_tensor_occupancy(force_data%t_3c_der_RI(i_xyz), nze, occ)
nze_tot = nze_tot + nze
CALL alloc_containers(force_data%t_3c_der_RI_comp(i_mem, i_xyz), 1)
CALL compress_tensor(force_data%t_3c_der_RI(i_xyz), force_data%t_3c_der_RI_ind(i_mem, i_xyz)%ind, &
force_data%t_3c_der_RI_comp(i_mem, i_xyz), mp2_env%ri_rpa_im_time%eps_compress, memory)
CALL dbt_clear(force_data%t_3c_der_RI(i_xyz))
CALL dbt_copy(t_3c_der_AO_prv(1, 1, i_xyz), force_data%t_3c_der_AO(i_xyz), move_data=.TRUE.)
CALL dbt_filter(force_data%t_3c_der_AO(i_xyz), mp2_env%ri_rpa_im_time%eps_filter)
CALL get_tensor_occupancy(force_data%t_3c_der_AO(i_xyz), nze, occ)
nze_tot = nze_tot + nze
CALL alloc_containers(force_data%t_3c_der_AO_comp(i_mem, i_xyz), 1)
CALL compress_tensor(force_data%t_3c_der_AO(i_xyz), force_data%t_3c_der_AO_ind(i_mem, i_xyz)%ind, &
force_data%t_3c_der_AO_comp(i_mem, i_xyz), mp2_env%ri_rpa_im_time%eps_compress, memory)
CALL dbt_clear(force_data%t_3c_der_AO(i_xyz))
END DO
END DO
CALL neighbor_list_3c_destroy(nl_3c)
DO i_xyz = 1, 3
CALL dbt_destroy(t_3c_der_RI_prv(1, 1, i_xyz))
CALL dbt_destroy(t_3c_der_AO_prv(1, 1, i_xyz))
END DO
CALL para_env%sum(memory)
compression_factor = REAL(nze_tot, dp)*1.0E-06*8.0_dp/memory
IF (unit_nr > 0) THEN
WRITE (UNIT=unit_nr, FMT="((T3,A,T66,F11.2,A4))") &
"MEMORY_INFO| Memory for 3-center derivatives (compressed):", memory, ' MiB'
WRITE (UNIT=unit_nr, FMT="((T3,A,T60,F21.2))") &
"MEMORY_INFO| Compression factor: ", compression_factor
END IF
!Dealing with the 2-center derivatives
CALL get_qs_env(qs_env, distribution_2d=dist_2d, blacs_env=blacs_env, matrix_s=matrix_s)
CALL cp_dbcsr_dist2d_to_dist(dist_2d, dbcsr_dist)
ALLOCATE (row_bsize(SIZE(sizes_RI)))
ALLOCATE (col_bsize(SIZE(sizes_RI)))
row_bsize(:) = sizes_RI(:)
col_bsize(:) = sizes_RI(:)
pdims_t2c = 0
CALL dbt_pgrid_create(para_env, pdims_t2c, pgrid_t2c)
CALL create_2c_tensor(t_2c_template, dist1, dist2, pgrid_t2c, force_data%bsizes_RI_split, &
force_data%bsizes_RI_split, name='(RI| RI)')
DEALLOCATE (dist1, dist2)
CALL dbcsr_create(t_2c_int_tmp(1), "(P|Q) RPA", dbcsr_dist, dbcsr_type_symmetric, row_bsize, col_bsize)
DO i_xyz = 1, 3
CALL dbcsr_create(t_2c_der_tmp(1, i_xyz), "(P|Q) RPA der", dbcsr_dist, &
dbcsr_type_antisymmetric, row_bsize, col_bsize)
END DO
IF (use_virial) THEN
ALLOCATE (force_data%RI_virial_pot, force_data%RI_virial_met)
CALL dbcsr_create(force_data%RI_virial_pot, "RI_virial", dbcsr_dist, &
dbcsr_type_no_symmetry, row_bsize, col_bsize)
CALL dbcsr_create(force_data%RI_virial_met, "RI_virial", dbcsr_dist, &
dbcsr_type_no_symmetry, row_bsize, col_bsize)
END IF
! Main (P|Q) integrals and derivatives
! Integrals are passed as a full matrix => convert to DBCSR
CALL dbcsr_create(dbcsr_work, template=t_2c_int_tmp(1))
CALL copy_fm_to_dbcsr(fm_matrix_PQ, dbcsr_work)
! We need the +/- square root of (P|Q)
CALL dbcsr_create(dbcsr_work2, template=t_2c_int_tmp(1))
CALL dbcsr_create(dbcsr_work3, template=t_2c_int_tmp(1))
CALL dbcsr_copy(dbcsr_work2, dbcsr_work)
CALL cp_dbcsr_power(dbcsr_work, -0.5_dp, 1.0E-7_dp, n_dependent, para_env, blacs_env) !1.0E-7 ev qunenching thresh
! Transfer to tensor format with split blocks
CALL dbt_create(dbcsr_work, t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(dbcsr_work, t_2c_tmp)
CALL dbt_create(t_2c_template, force_data%t_2c_pot_msqrt)
CALL dbt_copy(t_2c_tmp, force_data%t_2c_pot_msqrt, move_data=.TRUE.)
CALL dbt_filter(force_data%t_2c_pot_msqrt, mp2_env%ri_rpa_im_time%eps_filter)
CALL dbcsr_multiply('N', 'N', 1.0_dp, dbcsr_work2, dbcsr_work, 0.0_dp, dbcsr_work3)
CALL dbt_copy_matrix_to_tensor(dbcsr_work3, t_2c_tmp)
CALL dbt_create(t_2c_template, force_data%t_2c_pot_psqrt)
CALL dbt_copy(t_2c_tmp, force_data%t_2c_pot_psqrt, move_data=.TRUE.)
CALL dbt_filter(force_data%t_2c_pot_psqrt, mp2_env%ri_rpa_im_time%eps_filter)
CALL dbt_destroy(t_2c_tmp)
CALL dbcsr_release(dbcsr_work2)
CALL dbcsr_release(dbcsr_work3)
CALL dbcsr_clear(dbcsr_work)
! Deal with the 2c potential derivatives. Only precompute if not in PBCs
IF (.NOT. do_periodic) THEN
CALL build_2c_neighbor_lists(nl_2c, basis_set_ri_aux, basis_set_ri_aux, mp2_env%potential_parameter, &
"RPA_2c_nl_pot", qs_env, sym_ij=.TRUE., dist_2d=dist_2d)
CALL build_2c_derivatives(t_2c_der_tmp, mp2_env%ri_rpa_im_time%eps_filter, qs_env, nl_2c, &
basis_set_ri_aux, basis_set_ri_aux, mp2_env%potential_parameter)
CALL release_neighbor_list_sets(nl_2c)
DO i_xyz = 1, 3
CALL dbt_create(t_2c_der_tmp(1, i_xyz), t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(t_2c_der_tmp(1, i_xyz), t_2c_tmp)
CALL dbt_create(t_2c_template, force_data%t_2c_der_pot(i_xyz))
CALL dbt_copy(t_2c_tmp, force_data%t_2c_der_pot(i_xyz), move_data=.TRUE.)
CALL dbt_filter(force_data%t_2c_der_pot(i_xyz), mp2_env%ri_rpa_im_time%eps_filter)
CALL dbt_destroy(t_2c_tmp)
CALL dbcsr_clear(t_2c_der_tmp(1, i_xyz))
END DO
IF (use_virial) THEN
CALL build_2c_neighbor_lists(force_data%nl_2c_pot, basis_set_ri_aux, basis_set_ri_aux, &
mp2_env%potential_parameter, "RPA_2c_nl_pot", qs_env, &
sym_ij=.FALSE., dist_2d=dist_2d)
END IF
END IF
! Create a G_PQ matrix to collect the terms for the force trace in the periodic case
CALL dbcsr_create(force_data%G_PQ, "G_PQ", dbcsr_dist, dbcsr_type_no_symmetry, row_bsize, col_bsize)
! we need the RI metric derivatives and the inverse of the integrals
CALL build_2c_neighbor_lists(nl_2c, basis_set_ri_aux, basis_set_ri_aux, mp2_env%ri_metric, &
"RPA_2c_nl_metric", qs_env, sym_ij=.TRUE., dist_2d=dist_2d)
CALL build_2c_integrals(t_2c_int_tmp, mp2_env%ri_rpa_im_time%eps_filter, qs_env, nl_2c, &
basis_set_ri_aux, basis_set_ri_aux, mp2_env%ri_metric)
CALL build_2c_derivatives(t_2c_der_tmp, mp2_env%ri_rpa_im_time%eps_filter, qs_env, nl_2c, &
basis_set_ri_aux, basis_set_ri_aux, mp2_env%ri_metric)
CALL release_neighbor_list_sets(nl_2c)
IF (use_virial) THEN
CALL build_2c_neighbor_lists(force_data%nl_2c_met, basis_set_ri_aux, basis_set_ri_aux, &
mp2_env%ri_metric, "RPA_2c_nl_metric", qs_env, sym_ij=.FALSE., &
dist_2d=dist_2d)
END IF
CALL dbcsr_copy(dbcsr_work, t_2c_int_tmp(1))
CALL cp_dbcsr_cholesky_decompose(dbcsr_work, para_env=para_env, blacs_env=blacs_env)
CALL cp_dbcsr_cholesky_invert(dbcsr_work, para_env=para_env, blacs_env=blacs_env, upper_to_full=.TRUE.)
CALL dbt_create(dbcsr_work, t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(dbcsr_work, t_2c_tmp)
CALL dbt_create(t_2c_template, force_data%t_2c_inv_metric)
CALL dbt_copy(t_2c_tmp, force_data%t_2c_inv_metric, move_data=.TRUE.)
CALL dbt_filter(force_data%t_2c_inv_metric, mp2_env%ri_rpa_im_time%eps_filter)
CALL dbt_destroy(t_2c_tmp)
CALL dbcsr_clear(dbcsr_work)
CALL dbcsr_clear(t_2c_int_tmp(1))
DO i_xyz = 1, 3
CALL dbt_create(t_2c_der_tmp(1, i_xyz), t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(t_2c_der_tmp(1, i_xyz), t_2c_tmp)
CALL dbt_create(t_2c_template, force_data%t_2c_der_metric(i_xyz))
CALL dbt_copy(t_2c_tmp, force_data%t_2c_der_metric(i_xyz), move_data=.TRUE.)
CALL dbt_filter(force_data%t_2c_der_metric(i_xyz), mp2_env%ri_rpa_im_time%eps_filter)
CALL dbt_destroy(t_2c_tmp)
CALL dbcsr_clear(t_2c_der_tmp(1, i_xyz))
END DO
!Pre-calculate matrix K = metric^-1 * V^0.5
CALL dbt_create(t_2c_template, force_data%t_2c_K)
CALL dbt_contract(1.0_dp, force_data%t_2c_inv_metric, force_data%t_2c_pot_psqrt, &
0.0_dp, force_data%t_2c_K, &
contract_1=[2], notcontract_1=[1], &
contract_2=[1], notcontract_2=[2], &
map_1=[1], map_2=[2], filter_eps=mp2_env%ri_rpa_im_time%eps_filter)
! Finally, we need the overlap matrix derivative and the inverse of the integrals
CALL dbt_destroy(t_2c_template)
CALL dbcsr_release(dbcsr_work)
CALL dbcsr_release(t_2c_int_tmp(1))
DO i_xyz = 1, 3
CALL dbcsr_release(t_2c_der_tmp(1, i_xyz))
END DO
DEALLOCATE (row_bsize, col_bsize)
ALLOCATE (row_bsize(SIZE(sizes_AO)))
ALLOCATE (col_bsize(SIZE(sizes_AO)))
row_bsize(:) = sizes_AO(:)
col_bsize(:) = sizes_AO(:)
CALL create_2c_tensor(t_2c_template, dist1, dist2, pgrid_t2c, force_data%bsizes_AO_split, &
force_data%bsizes_AO_split, name='(AO| AO)')
DEALLOCATE (dist1, dist2)
DO i_xyz = 1, 3
CALL dbcsr_create(t_2c_der_tmp(1, i_xyz), "(P|Q) RPA der", dbcsr_dist, &
dbcsr_type_antisymmetric, row_bsize, col_bsize)
END DO
identity_pot%potential_type = do_potential_id
CALL build_2c_neighbor_lists(nl_2c, basis_set_ao, basis_set_ao, identity_pot, &
"RPA_2c_nl_metric", qs_env, sym_ij=.TRUE., dist_2d=dist_2d)
CALL build_2c_derivatives(t_2c_der_tmp, mp2_env%ri_rpa_im_time%eps_filter, qs_env, nl_2c, &
basis_set_ao, basis_set_ao, identity_pot)
CALL release_neighbor_list_sets(nl_2c)
IF (use_virial) THEN
CALL build_2c_neighbor_lists(force_data%nl_2c_ovlp, basis_set_ao, basis_set_ao, identity_pot, &
"RPA_2c_nl_metric", qs_env, sym_ij=.FALSE., dist_2d=dist_2d)
END IF
CALL dbcsr_create(force_data%inv_ovlp, template=matrix_s(1)%matrix)
CALL dbcsr_copy(force_data%inv_ovlp, matrix_s(1)%matrix)
CALL cp_dbcsr_cholesky_decompose(force_data%inv_ovlp, para_env=para_env, blacs_env=blacs_env)
CALL cp_dbcsr_cholesky_invert(force_data%inv_ovlp, para_env=para_env, blacs_env=blacs_env, upper_to_full=.TRUE.)
DO i_xyz = 1, 3
CALL dbt_create(t_2c_der_tmp(1, i_xyz), t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(t_2c_der_tmp(1, i_xyz), t_2c_tmp)
CALL dbt_create(t_2c_template, force_data%t_2c_der_ovlp(i_xyz))
CALL dbt_copy(t_2c_tmp, force_data%t_2c_der_ovlp(i_xyz), move_data=.TRUE.)
CALL dbt_filter(force_data%t_2c_der_ovlp(i_xyz), mp2_env%ri_rpa_im_time%eps_filter)
CALL dbt_destroy(t_2c_tmp)
CALL dbcsr_clear(t_2c_der_tmp(1, i_xyz))
END DO
!Create the rest of the 2-center AO tensors
nspins = dft_control%nspins
ALLOCATE (force_data%P_virt(nspins), force_data%P_occ(nspins))
ALLOCATE (force_data%sum_YP_tau(nspins), force_data%sum_O_tau(nspins))
DO ispin = 1, nspins
ALLOCATE (force_data%P_virt(ispin)%matrix, force_data%P_occ(ispin)%matrix)
ALLOCATE (force_data%sum_YP_tau(ispin)%matrix, force_data%sum_O_tau(ispin)%matrix)
CALL dbcsr_create(force_data%P_virt(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_create(force_data%P_occ(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_create(force_data%sum_O_tau(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_create(force_data%sum_YP_tau(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(force_data%sum_O_tau(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_copy(force_data%sum_YP_tau(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(force_data%sum_O_tau(ispin)%matrix, 0.0_dp)
CALL dbcsr_set(force_data%sum_YP_tau(ispin)%matrix, 0.0_dp)
END DO
!Populate the density matrices: 1 = P_virt*S +P_occ*S ==> P_virt = S^-1 - P_occ
CALL get_qs_env(qs_env, rho=rho)
CALL qs_rho_get(rho, rho_ao=rho_ao)
CALL dbcsr_copy(force_data%P_occ(1)%matrix, rho_ao(1)%matrix)
IF (nspins == 1) THEN
CALL dbcsr_scale(force_data%P_occ(1)%matrix, 0.5_dp) !because double occupency
ELSE
CALL dbcsr_copy(force_data%P_occ(2)%matrix, rho_ao(2)%matrix)
END IF
DO ispin = 1, nspins
CALL dbcsr_copy(force_data%P_virt(ispin)%matrix, force_data%inv_ovlp)
CALL dbcsr_add(force_data%P_virt(ispin)%matrix, force_data%P_occ(ispin)%matrix, 1.0_dp, -1.0_dp)
END DO
DO ibasis = 1, SIZE(basis_set_ao)
orb_basis => basis_set_ao(ibasis)%gto_basis_set
CALL init_interaction_radii_orb_basis(orb_basis, eps_pgf_orb_old)
ri_basis => basis_set_ri_aux(ibasis)%gto_basis_set
CALL init_interaction_radii_orb_basis(ri_basis, eps_pgf_orb_old)
END DO
CALL dbt_destroy(t_2c_template)
CALL dbcsr_release(dbcsr_work)
DO i_xyz = 1, 3
CALL dbcsr_release(t_2c_der_tmp(1, i_xyz))
END DO
DEALLOCATE (row_bsize, col_bsize)
CALL dbt_pgrid_destroy(pgrid_t3c)
CALL dbt_pgrid_destroy(pgrid_t2c)
CALL dbcsr_distribution_release(dbcsr_dist)
CALL timestop(handle)
END SUBROUTINE init_im_time_forces
! **************************************************************************************************
!> \brief Updates the cubic-scaling SOS-Laplace-MP2 contribution to the forces at each quadrature point
!> \param force_data ...
!> \param mat_P_omega ...
!> \param t_3c_M ...
!> \param t_3c_O ...
!> \param t_3c_O_compressed ...
!> \param t_3c_O_ind ...
!> \param fm_scaled_dm_occ_tau ...
!> \param fm_scaled_dm_virt_tau ...
!> \param fm_mo_coeff_occ ...
!> \param fm_mo_coeff_virt ...
!> \param fm_mo_coeff_occ_scaled ...
!> \param fm_mo_coeff_virt_scaled ...
!> \param starts_array_mc ...
!> \param ends_array_mc ...
!> \param starts_array_mc_block ...
!> \param ends_array_mc_block ...
!> \param num_integ_points ...
!> \param nmo ...
!> \param Eigenval ...
!> \param tau_tj ...
!> \param tau_wj ...
!> \param cut_memory ...
!> \param Pspin ...
!> \param Qspin ...
!> \param open_shell ...
!> \param unit_nr ...
!> \param dbcsr_time ...
!> \param dbcsr_nflop ...
!> \param mp2_env ...
!> \param qs_env ...
!> \note In open-shell, we need to take Q from one spin, and everything from the other
! **************************************************************************************************
SUBROUTINE calc_laplace_loop_forces(force_data, mat_P_omega, t_3c_M, t_3c_O, t_3c_O_compressed, &
t_3c_O_ind, fm_scaled_dm_occ_tau, fm_scaled_dm_virt_tau, &
fm_mo_coeff_occ, fm_mo_coeff_virt, fm_mo_coeff_occ_scaled, &
fm_mo_coeff_virt_scaled, starts_array_mc, ends_array_mc, &
starts_array_mc_block, ends_array_mc_block, num_integ_points, &
nmo, Eigenval, tau_tj, tau_wj, cut_memory, Pspin, Qspin, &
open_shell, unit_nr, dbcsr_time, dbcsr_nflop, mp2_env, qs_env)
TYPE(im_time_force_type), INTENT(INOUT) :: force_data
TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(INOUT) :: mat_P_omega
TYPE(dbt_type), INTENT(INOUT) :: t_3c_M, t_3c_O
TYPE(hfx_compression_type), DIMENSION(:) :: t_3c_O_compressed
TYPE(block_ind_type), DIMENSION(:), INTENT(INOUT) :: t_3c_O_ind
TYPE(cp_fm_type), INTENT(IN) :: fm_scaled_dm_occ_tau, &
fm_scaled_dm_virt_tau
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: fm_mo_coeff_occ, fm_mo_coeff_virt
TYPE(cp_fm_type), INTENT(IN) :: fm_mo_coeff_occ_scaled, &
fm_mo_coeff_virt_scaled
INTEGER, DIMENSION(:), INTENT(IN) :: starts_array_mc, ends_array_mc, &
starts_array_mc_block, &
ends_array_mc_block
INTEGER, INTENT(IN) :: num_integ_points, nmo
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: Eigenval
REAL(KIND=dp), DIMENSION(0:num_integ_points), &
INTENT(IN) :: tau_tj
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
INTENT(IN) :: tau_wj
INTEGER, INTENT(IN) :: cut_memory, Pspin, Qspin
LOGICAL, INTENT(IN) :: open_shell
INTEGER, INTENT(IN) :: unit_nr
REAL(dp), INTENT(INOUT) :: dbcsr_time
INTEGER(int_8), INTENT(INOUT) :: dbcsr_nflop
TYPE(mp2_type) :: mp2_env
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'calc_laplace_loop_forces'
INTEGER :: dummy_int, handle, handle2, i_mem, i_xyz, ibasis, ispin, j_xyz, jquad, k_xyz, &
n_mem_RI, n_rep, natom, nkind, nspins, unit_nr_dbcsr
INTEGER(int_8) :: flop, nze, nze_ddint, nze_der_AO, &
nze_der_RI, nze_KQK
INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_of_kind, batch_blk_end, &
batch_blk_start, batch_end_RI, &
batch_start_RI, kind_of, mc_ranges, &
mc_ranges_RI
INTEGER, DIMENSION(:, :), POINTER :: dummy_ptr
LOGICAL :: memory_info, use_virial
REAL(dp) :: eps_filter, eps_pgf_orb, &
eps_pgf_orb_old, fac, occ, occ_ddint, &
occ_der_AO, occ_der_RI, occ_KQK, &
omega, pref, t1, t2, tau
REAL(dp), DIMENSION(3, 3) :: work_virial, work_virial_ovlp
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: mat_dm_occ, mat_dm_virt
TYPE(dbcsr_type) :: dbcsr_work1, dbcsr_work2, dbcsr_work3, &
exp_occ, exp_virt, R_occ, R_virt, &
virial_ovlp, Y_1, Y_2
TYPE(dbt_type) :: t_2c_AO, t_2c_RI, t_2c_RI_2, t_2c_tmp, t_3c_0, t_3c_1, t_3c_3, t_3c_4, &
t_3c_5, t_3c_6, t_3c_7, t_3c_8, t_3c_help_1, t_3c_help_2, t_3c_ints, t_3c_sparse, &
t_3c_work, t_dm_occ, t_dm_virt, t_KQKT, t_M_occ, t_M_virt, t_Q, t_R_occ, t_R_virt
TYPE(dbt_type), ALLOCATABLE, DIMENSION(:) :: t_P
TYPE(dft_control_type), POINTER :: dft_control
TYPE(gto_basis_set_p_type), ALLOCATABLE, &
DIMENSION(:), TARGET :: basis_set_ao, basis_set_ri_aux
TYPE(gto_basis_set_type), POINTER :: orb_basis, ri_basis
TYPE(libint_potential_type) :: identity_pot
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_force_type), DIMENSION(:), POINTER :: force
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(section_vals_type), POINTER :: qs_section
TYPE(virial_type), POINTER :: virial
NULLIFY (matrix_s, dummy_ptr, atomic_kind_set, force, matrix_s, matrix_ks, mat_dm_occ, mat_dm_virt)
NULLIFY (dft_control, virial, particle_set, cell, para_env, orb_basis, ri_basis, qs_section)
NULLIFY (qs_kind_set)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, matrix_s=matrix_s, natom=natom, atomic_kind_set=atomic_kind_set, &
force=force, matrix_ks=matrix_ks, dft_control=dft_control, virial=virial, &
particle_set=particle_set, cell=cell, para_env=para_env, nkind=nkind, &
qs_kind_set=qs_kind_set)
eps_filter = mp2_env%ri_rpa_im_time%eps_filter
nspins = dft_control%nspins
memory_info = mp2_env%ri_rpa_im_time%memory_info
IF (memory_info) THEN
unit_nr_dbcsr = unit_nr
ELSE
unit_nr_dbcsr = 0
END IF
use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
IF (use_virial) virial%pv_calculate = .TRUE.
IF (use_virial) THEN
qs_section => section_vals_get_subs_vals(qs_env%input, "DFT%QS")
CALL section_vals_val_get(qs_section, "EPS_PGF_ORB", n_rep_val=n_rep)
IF (n_rep /= 0) THEN
CALL section_vals_val_get(qs_section, "EPS_PGF_ORB", r_val=eps_pgf_orb)
ELSE
CALL section_vals_val_get(qs_section, "EPS_DEFAULT", r_val=eps_pgf_orb)
eps_pgf_orb = SQRT(eps_pgf_orb)
END IF
eps_pgf_orb_old = dft_control%qs_control%eps_pgf_orb
ALLOCATE (basis_set_ri_aux(nkind), basis_set_ao(nkind))
CALL basis_set_list_setup(basis_set_ri_aux, "RI_AUX", qs_kind_set)
CALL basis_set_list_setup(basis_set_ao, "ORB", qs_kind_set)
DO ibasis = 1, SIZE(basis_set_ao)
orb_basis => basis_set_ao(ibasis)%gto_basis_set
CALL init_interaction_radii_orb_basis(orb_basis, eps_pgf_orb)
ri_basis => basis_set_ri_aux(ibasis)%gto_basis_set
CALL init_interaction_radii_orb_basis(ri_basis, eps_pgf_orb)
END DO
END IF
!We follow the general logic of the compute_mat_P_omega routine
ALLOCATE (t_P(nspins))
CALL dbt_create(force_data%t_2c_K, t_2c_RI)
CALL dbt_create(force_data%t_2c_K, t_2c_RI_2)
CALL dbt_create(force_data%t_2c_der_ovlp(1), t_2c_AO)
CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of, atom_of_kind=atom_of_kind)
! Always do the batching of the MO on mu and sigma, such that it is consistent between
! the occupied and the virtual quantities
ALLOCATE (mc_ranges(cut_memory + 1))
mc_ranges(:cut_memory) = starts_array_mc_block(:)
mc_ranges(cut_memory + 1) = ends_array_mc_block(cut_memory) + 1
! Also need some batching on the RI, because it loses sparsity at some point
n_mem_RI = cut_memory
CALL create_tensor_batches(force_data%bsizes_RI_split, n_mem_RI, batch_start_RI, batch_end_RI, &
batch_blk_start, batch_blk_end)
ALLOCATE (mc_ranges_RI(n_mem_RI + 1))
mc_ranges_RI(1:n_mem_RI) = batch_blk_start(1:n_mem_RI)
mc_ranges_RI(n_mem_RI + 1) = batch_blk_end(n_mem_RI) + 1
DEALLOCATE (batch_blk_start, batch_blk_end)
!Pre-allocate all required tensors and matrices
DO ispin = 1, nspins
CALL dbt_create(t_2c_RI, t_P(ispin))
END DO
CALL dbt_create(t_2c_RI, t_Q)
CALL dbt_create(t_2c_RI, t_KQKT)
CALL dbt_create(t_2c_AO, t_dm_occ)
CALL dbt_create(t_2c_AO, t_dm_virt)
!note: t_3c_O and t_3c_M have different mappings (map_1d, map_2d)
CALL dbt_create(t_3c_O, t_M_occ)
CALL dbt_create(t_3c_O, t_M_virt)
CALL dbt_create(t_3c_O, t_3c_0)
CALL dbt_create(t_3c_O, t_3c_1)
CALL dbt_create(t_3c_O, t_3c_3)
CALL dbt_create(t_3c_O, t_3c_4)
CALL dbt_create(t_3c_O, t_3c_5)
CALL dbt_create(t_3c_M, t_3c_6)
CALL dbt_create(t_3c_M, t_3c_7)
CALL dbt_create(t_3c_M, t_3c_8)
CALL dbt_create(t_3c_M, t_3c_sparse)
CALL dbt_create(t_3c_O, t_3c_help_1)
CALL dbt_create(t_3c_O, t_3c_help_2)
CALL dbt_create(t_2c_AO, t_R_occ)
CALL dbt_create(t_2c_AO, t_R_virt)
CALL dbt_create(t_3c_M, t_3c_ints)
CALL dbt_create(t_3c_M, t_3c_work)
!Pre-define the sparsity of t_3c_4 as a function of the derivatives
occ_der_AO = 0; nze_der_AO = 0
occ_der_RI = 0; nze_der_RI = 0
DO i_xyz = 1, 3
DO i_mem = 1, cut_memory
CALL decompress_tensor(force_data%t_3c_der_RI(i_xyz), force_data%t_3c_der_RI_ind(i_mem, i_xyz)%ind, &
force_data%t_3c_der_RI_comp(i_mem, i_xyz), mp2_env%ri_rpa_im_time%eps_compress)
CALL get_tensor_occupancy(force_data%t_3c_der_RI(i_xyz), nze, occ)
occ_der_RI = occ_der_RI + occ
nze_der_RI = nze_der_RI + nze
CALL dbt_copy(force_data%t_3c_der_RI(i_xyz), t_3c_sparse, summation=.TRUE., move_data=.TRUE.)
CALL decompress_tensor(force_data%t_3c_der_AO(i_xyz), force_data%t_3c_der_AO_ind(i_mem, i_xyz)%ind, &
force_data%t_3c_der_AO_comp(i_mem, i_xyz), mp2_env%ri_rpa_im_time%eps_compress)
CALL get_tensor_occupancy(force_data%t_3c_der_AO(i_xyz), nze, occ)
occ_der_AO = occ_der_AO + occ
nze_der_AO = nze_der_AO + nze
CALL dbt_copy(force_data%t_3c_der_AO(i_xyz), t_3c_sparse, order=[1, 3, 2], summation=.TRUE.)
CALL dbt_copy(force_data%t_3c_der_AO(i_xyz), t_3c_sparse, summation=.TRUE., move_data=.TRUE.)
END DO
END DO
occ_der_RI = occ_der_RI/3.0_dp
occ_der_AO = occ_der_AO/3.0_dp
nze_der_RI = nze_der_RI/3
nze_der_AO = nze_der_AO/3
CALL dbcsr_create(R_occ, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(R_virt, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(dbcsr_work1, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(dbcsr_work2, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(dbcsr_work3, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(exp_occ, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(exp_virt, template=matrix_s(1)%matrix, matrix_type=dbcsr_type_no_symmetry)
IF (use_virial) CALL dbcsr_create(virial_ovlp, template=dbcsr_work1)
CALL dbt_batched_contract_init(t_3c_0, batch_range_2=mc_ranges, batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_1, batch_range_2=mc_ranges, batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_3, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges)
CALL dbt_batched_contract_init(t_M_occ, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges)
CALL dbt_batched_contract_init(t_M_virt, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges)
CALL dbt_batched_contract_init(t_3c_ints, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges)
CALL dbt_batched_contract_init(t_3c_work, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges)
CALL dbt_batched_contract_init(t_3c_4, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges, &
batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_5, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges, &
batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_6, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges, &
batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_7, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges, &
batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_8, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges, &
batch_range_3=mc_ranges)
CALL dbt_batched_contract_init(t_3c_sparse, batch_range_1=mc_ranges_RI, batch_range_2=mc_ranges, &
batch_range_3=mc_ranges)
work_virial = 0.0_dp
work_virial_ovlp = 0.0_dp
DO jquad = 1, num_integ_points
tau = tau_tj(jquad)
omega = tau_wj(jquad)
fac = -2.0_dp*omega*mp2_env%scale_S
IF (open_shell) fac = 0.5_dp*fac
occ_ddint = 0; nze_ddint = 0
CALL para_env%sync()
t1 = m_walltime()
!Deal with the force contributions where there is no explicit 3-center quantities, i.e. the
!forces due to the metric and potential derivatives
DO ispin = 1, nspins
CALL dbt_create(mat_P_omega(jquad, ispin)%matrix, t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(mat_P_omega(jquad, ispin)%matrix, t_2c_tmp)
CALL dbt_copy(t_2c_tmp, t_P(ispin), move_data=.TRUE.)
CALL dbt_filter(t_P(ispin), eps_filter)
CALL dbt_destroy(t_2c_tmp)
END DO
!Q = K^T*P*K, open-shell: Q is from one spin, everything else from the other
CALL dbt_contract(1.0_dp, t_P(Qspin), force_data%t_2c_K, 0.0_dp, t_2c_RI, &
contract_1=[2], notcontract_1=[1], &
contract_2=[1], notcontract_2=[2], &
map_1=[1], map_2=[2], filter_eps=eps_filter, &
flop=flop, unit_nr=unit_nr_dbcsr)
dbcsr_nflop = dbcsr_nflop + flop
CALL dbt_contract(1.0_dp, force_data%t_2c_K, t_2c_RI, 0.0_dp, t_Q, &
contract_1=[1], notcontract_1=[2], &
contract_2=[1], notcontract_2=[2], &
map_1=[1], map_2=[2], filter_eps=eps_filter, &
flop=flop, unit_nr=unit_nr_dbcsr)
dbcsr_nflop = dbcsr_nflop + flop
CALL dbt_clear(t_2c_RI)
CALL perform_2c_ops(force, t_KQKT, force_data, fac, t_Q, t_P(Pspin), t_2c_RI, t_2c_RI_2, &
use_virial, atom_of_kind, kind_of, eps_filter, dbcsr_nflop, unit_nr_dbcsr)
CALL get_tensor_occupancy(t_KQKT, nze_KQK, occ_KQK)
!Calculate the pseudo-density matrix in tensor form. There are a few useless arguments for SOS-MP2
CALL compute_mat_dm_global(fm_scaled_dm_occ_tau, fm_scaled_dm_virt_tau, tau_tj, num_integ_points, &
nmo, fm_mo_coeff_occ(Pspin), fm_mo_coeff_virt(Pspin), &
fm_mo_coeff_occ_scaled, fm_mo_coeff_virt_scaled, mat_dm_occ, mat_dm_virt, &
matrix_s, Pspin, Eigenval(:, Pspin), 0.0_dp, eps_filter, &
mp2_env%ri_rpa_im_time%memory_info, unit_nr, &
jquad, .FALSE., .FALSE., qs_env, dummy_int, dummy_ptr, para_env)
CALL dbt_create(mat_dm_occ(jquad, 1)%matrix, t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(mat_dm_occ(jquad, 1)%matrix, t_2c_tmp)
CALL dbt_copy(t_2c_tmp, t_dm_occ, move_data=.TRUE.)
CALL dbt_filter(t_dm_occ, eps_filter)
CALL dbt_destroy(t_2c_tmp)
CALL dbt_create(mat_dm_virt(jquad, 1)%matrix, t_2c_tmp)
CALL dbt_copy_matrix_to_tensor(mat_dm_virt(jquad, 1)%matrix, t_2c_tmp)
CALL dbt_copy(t_2c_tmp, t_dm_virt, move_data=.TRUE.)
CALL dbt_filter(t_dm_virt, eps_filter)
CALL dbt_destroy(t_2c_tmp)
!Deal with the 3-center quantities.
CALL perform_3c_ops(force, t_R_occ, t_R_virt, force_data, fac, cut_memory, n_mem_RI, &
t_KQKT, t_dm_occ, t_dm_virt, t_3c_O, t_3c_M, t_M_occ, t_M_virt, t_3c_0, t_3c_1, &
t_3c_3, t_3c_4, t_3c_5, t_3c_6, t_3c_7, t_3c_8, t_3c_sparse, t_3c_help_1, t_3c_help_2, &
t_3c_ints, t_3c_work, starts_array_mc, ends_array_mc, batch_start_RI, &
batch_end_RI, t_3c_O_compressed, t_3c_O_ind, use_virial, &
atom_of_kind, kind_of, eps_filter, occ_ddint, nze_ddint, dbcsr_nflop, &
unit_nr_dbcsr, mp2_env)
CALL timeset(routineN//"_dbcsr", handle2)
!We go back to DBCSR matrices from now on
!Note: R matrices are in fact symmetric, but use a normal type for convenience
CALL dbt_create(matrix_s(1)%matrix, t_2c_tmp)
CALL dbt_copy(t_R_occ, t_2c_tmp, move_data=.TRUE.)
CALL dbt_copy_tensor_to_matrix(t_2c_tmp, R_occ)
CALL dbt_copy(t_R_virt, t_2c_tmp, move_data=.TRUE.)
CALL dbt_copy_tensor_to_matrix(t_2c_tmp, R_virt)
!Iteratively calculate the Y1 and Y2 matrices
CALL dbcsr_multiply('N', 'N', tau, force_data%P_occ(Pspin)%matrix, &
matrix_ks(Pspin)%matrix, 0.0_dp, dbcsr_work1)
CALL build_Y_matrix(Y_1, dbcsr_work1, force_data%P_occ(Pspin)%matrix, R_virt, eps_filter)
CALL matrix_exponential(exp_occ, dbcsr_work1, 1.0_dp, 1.0_dp, eps_filter)
CALL dbcsr_multiply('N', 'N', -tau, force_data%P_virt(Pspin)%matrix, &
matrix_ks(Pspin)%matrix, 0.0_dp, dbcsr_work1)
CALL build_Y_matrix(Y_2, dbcsr_work1, force_data%P_virt(Pspin)%matrix, R_occ, eps_filter)
CALL matrix_exponential(exp_virt, dbcsr_work1, 1.0_dp, 1.0_dp, eps_filter)
!The force contribution coming from [-S^-1*(e^-tau*P_virt*F)^T*R_occ*S^-1
! +tau*S^-1*Y_2^T*F*S^-1] * der_S
CALL dbcsr_multiply('N', 'N', 1.0_dp, R_occ, force_data%inv_ovlp, 0.0_dp, dbcsr_work1)
CALL dbcsr_multiply('T', 'N', 1.0_dp, exp_virt, dbcsr_work1, 0.0_dp, dbcsr_work3)
CALL dbcsr_multiply('N', 'N', 1.0_dp, force_data%inv_ovlp, dbcsr_work3, 0.0_dp, dbcsr_work2)
CALL dbcsr_multiply('N', 'T', tau, force_data%inv_ovlp, Y_2, 0.0_dp, dbcsr_work3)
CALL dbcsr_multiply('N', 'N', 1.0_dp, dbcsr_work3, matrix_ks(Pspin)%matrix, 0.0_dp, dbcsr_work1)
CALL dbcsr_multiply('N', 'N', 1.0_dp, dbcsr_work1, force_data%inv_ovlp, 0.0_dp, dbcsr_work3)
CALL dbcsr_add(dbcsr_work2, dbcsr_work3, 1.0_dp, -1.0_dp)
CALL dbt_copy_matrix_to_tensor(dbcsr_work2, t_2c_tmp)
CALL dbt_copy(t_2c_tmp, t_2c_AO, move_data=.TRUE.)
pref = -1.0_dp*fac
CALL get_2c_der_force(force, t_2c_AO, force_data%t_2c_der_ovlp, atom_of_kind, &
kind_of, force_data%idx_to_at_AO, pref, do_ovlp=.TRUE.)