LCOV - code coverage report
Current view: top level - src - mp2_ri_2c.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 94.3 % 557 525
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 18 18

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Framework for 2c-integrals for RI
      10              : !> \par History
      11              : !>      06.2012 created [Mauro Del Ben]
      12              : !>      03.2019 separated from mp2_ri_gpw [Frederick Stein]
      13              : ! **************************************************************************************************
      14              : MODULE mp2_ri_2c
      15              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      16              :                                               get_atomic_kind_set
      17              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      18              :                                               gto_basis_set_type
      19              :    USE cell_types,                      ONLY: cell_type,&
      20              :                                               get_cell,&
      21              :                                               plane_distance
      22              :    USE constants_operator,              ONLY: operator_coulomb
      23              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      24              :                                               cp_blacs_env_release,&
      25              :                                               cp_blacs_env_type
      26              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_scale_and_add_fm
      27              :    USE cp_cfm_diag,                     ONLY: cp_cfm_power
      28              :    USE cp_cfm_types,                    ONLY: cp_cfm_copy_general,&
      29              :                                               cp_cfm_create,&
      30              :                                               cp_cfm_release,&
      31              :                                               cp_cfm_to_cfm,&
      32              :                                               cp_cfm_to_fm,&
      33              :                                               cp_cfm_type,&
      34              :                                               cp_fm_to_cfm
      35              :    USE cp_control_types,                ONLY: dft_control_type
      36              :    USE cp_dbcsr_api,                    ONLY: &
      37              :         dbcsr_copy, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, &
      38              :         dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_p_type, dbcsr_release, &
      39              :         dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry, dbcsr_type_symmetric
      40              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_reserve_all_blocks
      41              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      42              :                                               cp_dbcsr_dist2d_to_dist,&
      43              :                                               dbcsr_allocate_matrix_set,&
      44              :                                               dbcsr_deallocate_matrix_set
      45              :    USE cp_eri_mme_interface,            ONLY: cp_eri_mme_param
      46              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_column_scale,&
      47              :                                               cp_fm_triangular_invert
      48              :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose
      49              :    USE cp_fm_diag,                      ONLY: choose_eigv_solver,&
      50              :                                               cp_fm_power,&
      51              :                                               cp_fm_syevx
      52              :    USE cp_fm_redistribution,            ONLY: cp_array_columns_to_fm
      53              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      54              :                                               cp_fm_struct_release,&
      55              :                                               cp_fm_struct_type
      56              :    USE cp_fm_types,                     ONLY: cp_fm_copy_general,&
      57              :                                               cp_fm_create,&
      58              :                                               cp_fm_get_info,&
      59              :                                               cp_fm_release,&
      60              :                                               cp_fm_set_all,&
      61              :                                               cp_fm_to_fm,&
      62              :                                               cp_fm_type
      63              :    USE distribution_2d_types,           ONLY: distribution_2d_type
      64              :    USE group_dist_types,                ONLY: create_group_dist,&
      65              :                                               get_group_dist,&
      66              :                                               group_dist_d1_type,&
      67              :                                               release_group_dist
      68              :    USE input_constants,                 ONLY: do_eri_gpw,&
      69              :                                               do_eri_mme,&
      70              :                                               do_eri_os,&
      71              :                                               do_potential_coulomb,&
      72              :                                               do_potential_id,&
      73              :                                               do_potential_long,&
      74              :                                               do_potential_truncated
      75              :    USE integral_library_types,          ONLY: compare_potential_types,&
      76              :                                               coulomb_operator_type
      77              :    USE kinds,                           ONLY: dp
      78              :    USE kpoint_coulomb_2c,               ONLY: build_2c_coulomb_matrix_kp
      79              :    USE kpoint_methods,                  ONLY: kpoint_init_cell_index
      80              :    USE kpoint_transform,                ONLY: rskp_prepared_transform_type,&
      81              :                                               rskp_transform_evaluate,&
      82              :                                               rskp_transform_prepare,&
      83              :                                               rskp_transform_release
      84              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      85              :                                               kpoint_type
      86              :    USE machine,                         ONLY: m_flush
      87              :    USE mathconstants,                   ONLY: gaussi,&
      88              :                                               z_one,&
      89              :                                               z_zero
      90              :    USE message_passing,                 ONLY: mp_comm_type,&
      91              :                                               mp_para_env_release,&
      92              :                                               mp_para_env_type
      93              :    USE mp2_eri,                         ONLY: mp2_eri_2c_integrate
      94              :    USE mp2_eri_gpw,                     ONLY: mp2_eri_2c_integrate_gpw
      95              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      96              :    USE particle_methods,                ONLY: get_particle_set
      97              :    USE particle_types,                  ONLY: particle_type
      98              :    USE qs_environment_types,            ONLY: get_qs_env,&
      99              :                                               qs_environment_type
     100              :    USE qs_integral_utils,               ONLY: basis_set_list_setup
     101              :    USE qs_interactions,                 ONLY: init_interaction_radii
     102              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
     103              :                                               qs_kind_type
     104              :    USE qs_ks_types,                     ONLY: get_ks_env,&
     105              :                                               set_ks_env
     106              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type,&
     107              :                                               release_neighbor_list_sets
     108              :    USE qs_tensors,                      ONLY: build_2c_integrals,&
     109              :                                               build_2c_neighbor_lists
     110              : 
     111              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num
     112              : #include "./base/base_uses.f90"
     113              : 
     114              :    IMPLICIT NONE
     115              : 
     116              :    PRIVATE
     117              : 
     118              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mp2_ri_2c'
     119              : 
     120              :    PUBLIC :: get_2c_integrals, trunc_coulomb_for_exchange, RI_2c_integral_mat, RI_2c_integral_mat_kp, &
     121              :              inversion_of_M_and_mult_with_chol_dec_of_V
     122              : 
     123              : CONTAINS
     124              : 
     125              : ! **************************************************************************************************
     126              : !> \brief ...
     127              : !> \param qs_env ...
     128              : !> \param eri_method ...
     129              : !> \param eri_param ...
     130              : !> \param para_env ...
     131              : !> \param para_env_sub ...
     132              : !> \param mp2_memory ...
     133              : !> \param my_Lrows ...
     134              : !> \param my_Vrows ...
     135              : !> \param fm_matrix_PQ ...
     136              : !> \param ngroup ...
     137              : !> \param color_sub ...
     138              : !> \param dimen_RI ...
     139              : !> \param dimen_RI_red reduced RI dimension,  needed if we perform SVD instead of Cholesky
     140              : !> \param kpoints ...
     141              : !> \param my_group_L_size ...
     142              : !> \param my_group_L_start ...
     143              : !> \param my_group_L_end ...
     144              : !> \param gd_array ...
     145              : !> \param calc_PQ_cond_num ...
     146              : !> \param do_svd ...
     147              : !> \param eps_svd ...
     148              : !> \param potential ...
     149              : !> \param ri_metric ...
     150              : !> \param cfm_matrix_L_kpoints ...
     151              : !> \param cfm_matrix_Minv_L_kpoints ...
     152              : !> \param fm_matrix_Minv_L_gamma ...
     153              : !> \param fm_matrix_Minv ...
     154              : !> \param fm_matrix_Minv_Vtrunc_Minv ...
     155              : !> \param do_im_time ...
     156              : !> \param do_kpoints ...
     157              : !> \param mp2_eps_pgf_orb_S ...
     158              : !> \param qs_kind_set ...
     159              : !> \param sab_orb_sub ...
     160              : !> \param calc_forces ...
     161              : !> \param unit_nr ...
     162              : ! **************************************************************************************************
     163          706 :    SUBROUTINE get_2c_integrals(qs_env, eri_method, eri_param, para_env, para_env_sub, mp2_memory, &
     164              :                                my_Lrows, my_Vrows, fm_matrix_PQ, ngroup, color_sub, dimen_RI, dimen_RI_red, &
     165              :                                kpoints, my_group_L_size, my_group_L_start, my_group_L_end, &
     166              :                                gd_array, calc_PQ_cond_num, do_svd, eps_svd, potential, ri_metric, &
     167              :                                cfm_matrix_L_kpoints, cfm_matrix_Minv_L_kpoints, fm_matrix_Minv_L_gamma, &
     168              :                                fm_matrix_Minv, fm_matrix_Minv_Vtrunc_Minv, &
     169              :                                do_im_time, do_kpoints, mp2_eps_pgf_orb_S, qs_kind_set, sab_orb_sub, calc_forces, unit_nr)
     170              : 
     171              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     172              :       INTEGER, INTENT(IN)                                :: eri_method
     173              :       TYPE(cp_eri_mme_param), POINTER                    :: eri_param
     174              :       TYPE(mp_para_env_type), POINTER                    :: para_env, para_env_sub
     175              :       REAL(KIND=dp), INTENT(IN)                          :: mp2_memory
     176              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
     177              :          INTENT(OUT)                                     :: my_Lrows, my_Vrows
     178              :       TYPE(cp_fm_type), INTENT(OUT)                      :: fm_matrix_PQ
     179              :       INTEGER, INTENT(IN)                                :: ngroup, color_sub
     180              :       INTEGER, INTENT(OUT)                               :: dimen_RI, dimen_RI_red
     181              :       TYPE(kpoint_type), POINTER                         :: kpoints
     182              :       INTEGER, INTENT(OUT)                               :: my_group_L_size, my_group_L_start, &
     183              :                                                             my_group_L_end
     184              :       TYPE(group_dist_d1_type), INTENT(OUT)              :: gd_array
     185              :       LOGICAL, INTENT(IN)                                :: calc_PQ_cond_num, do_svd
     186              :       REAL(KIND=dp), INTENT(IN)                          :: eps_svd
     187              :       TYPE(coulomb_operator_type)                        :: potential, ri_metric
     188              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:)       :: cfm_matrix_L_kpoints, &
     189              :                                                             cfm_matrix_Minv_L_kpoints
     190              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :), &
     191              :          INTENT(OUT)                                     :: fm_matrix_Minv_L_gamma
     192              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_matrix_Minv, &
     193              :                                                             fm_matrix_Minv_Vtrunc_Minv
     194              :       LOGICAL, INTENT(IN)                                :: do_im_time, do_kpoints
     195              :       REAL(KIND=dp), INTENT(IN)                          :: mp2_eps_pgf_orb_S
     196              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     197              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     198              :          POINTER                                         :: sab_orb_sub
     199              :       LOGICAL, INTENT(IN)                                :: calc_forces
     200              :       INTEGER, INTENT(IN)                                :: unit_nr
     201              : 
     202              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_2c_integrals'
     203              : 
     204              :       INTEGER                                            :: handle, i_size, num_small_eigen
     205              :       REAL(KIND=dp)                                      :: cond_num, eps_pgf_orb_old
     206              :       TYPE(coulomb_operator_type)                        :: trunc_coulomb
     207          706 :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:)       :: cfm_matrix_M_kpoints
     208              :       TYPE(cp_fm_type)                                   :: fm_matrix_L_work, fm_matrix_M_inv_work, &
     209              :                                                             fm_matrix_V
     210              :       TYPE(dft_control_type), POINTER                    :: dft_control
     211              :       TYPE(mp_para_env_type), POINTER                    :: para_env_L
     212              : 
     213          706 :       CALL timeset(routineN, handle)
     214              : 
     215              :       ! calculate V and store it in fm_matrix_L_work
     216              :       CALL compute_2c_integrals(qs_env, eri_method, eri_param, para_env, para_env_sub, para_env_L, mp2_memory, &
     217              :                                 fm_matrix_L_work, ngroup, color_sub, dimen_RI, &
     218              :                                 my_group_L_size, my_group_L_start, my_group_L_end, &
     219              :                                 gd_array, calc_PQ_cond_num, cond_num, &
     220          706 :                                 num_small_eigen, potential, sab_orb_sub, do_im_time=do_im_time)
     221              : 
     222          706 :       IF (do_im_time .AND. calc_forces) THEN
     223              :          !need a copy of the (P|Q) integrals
     224           50 :          CALL cp_fm_create(fm_matrix_PQ, fm_matrix_L_work%matrix_struct)
     225           50 :          CALL cp_fm_to_fm(fm_matrix_L_work, fm_matrix_PQ)
     226              :       END IF
     227              : 
     228          706 :       dimen_RI_red = dimen_RI
     229              : 
     230          706 :       IF (compare_potential_types(ri_metric, potential) .AND. .NOT. do_im_time) THEN
     231              :          CALL decomp_mat_L(fm_matrix_L_work, do_svd, eps_svd, num_small_eigen, cond_num, .TRUE., gd_array, ngroup, &
     232          526 :                            dimen_RI, dimen_RI_red, para_env)
     233              :       ELSE
     234              : 
     235              :          ! RI-metric matrix (depending on RI metric: Coulomb, overlap or truncated Coulomb)
     236          180 :          IF (do_im_time) THEN
     237          144 :             CALL get_qs_env(qs_env, dft_control=dft_control)
     238              : 
     239              :             ! re-init the radii to be able to generate pair lists with appropriate screening for overlap matrix
     240          144 :             eps_pgf_orb_old = dft_control%qs_control%eps_pgf_orb
     241          144 :             dft_control%qs_control%eps_pgf_orb = mp2_eps_pgf_orb_S
     242          144 :             CALL init_interaction_radii(dft_control%qs_control, qs_kind_set)
     243              : 
     244          144 :             IF (do_kpoints) THEN
     245              :                CALL RI_2c_integral_mat_kp(qs_env, cfm_matrix_M_kpoints, fm_matrix_L_work%matrix_struct, dimen_RI, &
     246              :                                           ri_metric, kpoints, put_mat_KS_env=.TRUE., &
     247           22 :                                           regularization_RI=qs_env%mp2_env%ri_rpa_im_time%regularization_RI)
     248              :             ELSE
     249              :                CALL RI_2c_integral_mat(qs_env, fm_matrix_Minv_L_gamma, fm_matrix_L_work%matrix_struct, dimen_RI, &
     250              :                                        ri_metric, put_mat_KS_env=.TRUE., &
     251          122 :                                        regularization_RI=qs_env%mp2_env%ri_rpa_im_time%regularization_RI)
     252              :             END IF
     253              : 
     254              :             ! re-init the radii to previous values
     255          144 :             dft_control%qs_control%eps_pgf_orb = eps_pgf_orb_old
     256          144 :             CALL init_interaction_radii(dft_control%qs_control, qs_kind_set)
     257              : 
     258              :             ! GPW overlap matrix
     259              :          ELSE
     260              : 
     261           36 :             CALL mp_para_env_release(para_env_L)
     262           36 :             CALL release_group_dist(gd_array)
     263              : 
     264          108 :             ALLOCATE (fm_matrix_Minv_L_gamma(1, 1))
     265              : 
     266              :             ! Calculate matrix of RI operator (for overlap metric: S), store it in fm_matrix_Minv_L_gamma
     267              :             CALL compute_2c_integrals(qs_env, eri_method, eri_param, para_env, para_env_sub, para_env_L, mp2_memory, &
     268              :                                       fm_matrix_Minv_L_gamma(1, 1), ngroup, color_sub, dimen_RI, &
     269              :                                       my_group_L_size, my_group_L_start, my_group_L_end, &
     270              :                                       gd_array, calc_PQ_cond_num, cond_num, &
     271              :                                       num_small_eigen, ri_metric, sab_orb_sub, &
     272           36 :                                       fm_matrix_L_struct=fm_matrix_L_work%matrix_struct)
     273              : 
     274              :          END IF
     275              : 
     276          180 :          IF (do_kpoints) THEN
     277              : 
     278              :             ! k-dependent 1/r Coulomb matrix V_PQ(k)
     279           22 :             CALL compute_V_by_lattice_sum(qs_env, cfm_matrix_L_kpoints, fm_matrix_L_work%matrix_struct, kpoints)
     280              : 
     281              :             CALL inversion_of_M_and_mult_with_chol_dec_of_V(cfm_matrix_M_kpoints, cfm_matrix_L_kpoints, &
     282              :                                                             cfm_matrix_Minv_L_kpoints, dimen_RI, kpoints, &
     283           22 :                                                             qs_env%mp2_env%ri_rpa_im_time%eps_eigval_S)
     284          450 :             DO i_size = 1, SIZE(cfm_matrix_M_kpoints)
     285          450 :                CALL cp_cfm_release(cfm_matrix_M_kpoints(i_size))
     286              :             END DO
     287           22 :             DEALLOCATE (cfm_matrix_M_kpoints)
     288              : 
     289           22 :             CALL trunc_coulomb_for_exchange(qs_env, trunc_coulomb)
     290              : 
     291              :             ! Gamma-only truncated Coulomb matrix V^tr with cutoff radius = half the unit cell size; for exchange self-energy
     292              :             CALL RI_2c_integral_mat(qs_env, fm_matrix_Minv_Vtrunc_Minv, fm_matrix_L_work%matrix_struct, dimen_RI, trunc_coulomb, &
     293           22 :                                     put_mat_KS_env=.FALSE., regularization_RI=0.0_dp)
     294              : 
     295              :             ! Gamma-only RI-metric matrix; for computing Gamma-only/MIC self-energy
     296              :             CALL RI_2c_integral_mat(qs_env, fm_matrix_Minv, fm_matrix_L_work%matrix_struct, dimen_RI, ri_metric, &
     297           22 :                                     put_mat_KS_env=.FALSE., regularization_RI=0.0_dp)
     298              : 
     299              :             CALL Gamma_only_inversion_of_M_and_mult_with_chol_dec_of_Vtrunc(fm_matrix_Minv_Vtrunc_Minv, &
     300           22 :                                                                             fm_matrix_Minv, qs_env)
     301              :          ELSE
     302          158 :             IF (calc_forces .AND. (.NOT. do_im_time)) THEN
     303              :                ! For the gradients, we make a copy of the inverse root of L
     304           12 :                CALL cp_fm_create(fm_matrix_V, fm_matrix_L_work%matrix_struct)
     305           12 :                CALL cp_fm_to_fm(fm_matrix_L_work, fm_matrix_V)
     306              : 
     307              :                CALL decomp_mat_L(fm_matrix_V, do_svd, eps_svd, num_small_eigen, cond_num, .TRUE., gd_array, ngroup, &
     308           12 :                                  dimen_RI, dimen_RI_red, para_env)
     309              :             END IF
     310              : 
     311              :             CALL decomp_mat_L(fm_matrix_L_work, do_svd, eps_svd, num_small_eigen, cond_num, .FALSE., gd_array, ngroup, &
     312          158 :                               dimen_RI, dimen_RI_red, para_env)
     313              : 
     314              :             CALL decomp_mat_L(fm_matrix_Minv_L_gamma(1, 1), .FALSE., 0.0_dp, num_small_eigen, cond_num, .TRUE., &
     315          158 :                               gd_array, ngroup, dimen_RI, dimen_RI_red, para_env)
     316              : 
     317          158 :             CALL cp_fm_create(fm_matrix_M_inv_work, fm_matrix_Minv_L_gamma(1, 1)%matrix_struct)
     318          158 :             CALL cp_fm_set_all(fm_matrix_M_inv_work, 0.0_dp)
     319              : 
     320              :             CALL parallel_gemm('N', 'T', dimen_RI, dimen_RI, dimen_RI, 1.0_dp, fm_matrix_Minv_L_gamma(1, 1), &
     321          158 :                                fm_matrix_Minv_L_gamma(1, 1), 0.0_dp, fm_matrix_M_inv_work)
     322              : 
     323          158 :             IF (do_svd) THEN
     324              :                ! We have to reset the size of fm_matrix_Minv_L_gamma
     325           34 :                CALL reset_size_matrix(fm_matrix_Minv_L_gamma(1, 1), dimen_RI_red, fm_matrix_L_work%matrix_struct)
     326              : 
     327              :                ! L (=fm_matrix_Minv_L_gamma) = S^(-1)*chol_dec(V)
     328              :                CALL parallel_gemm('T', 'N', dimen_RI, dimen_RI_red, dimen_RI, 1.0_dp, fm_matrix_M_inv_work, &
     329           34 :                                   fm_matrix_L_work, 0.0_dp, fm_matrix_Minv_L_gamma(1, 1))
     330              :             ELSE
     331              :                ! L (=fm_matrix_Minv_L_gamma) = S^(-1)*chol_dec(V)
     332              :                CALL parallel_gemm('T', 'T', dimen_RI, dimen_RI, dimen_RI, 1.0_dp, fm_matrix_M_inv_work, &
     333          124 :                                   fm_matrix_L_work, 0.0_dp, fm_matrix_Minv_L_gamma(1, 1))
     334              :             END IF
     335              : 
     336              :             ! clean the S_inv matrix
     337          158 :             CALL cp_fm_release(fm_matrix_M_inv_work)
     338              : 
     339              :          END IF
     340              : 
     341          180 :          IF (.NOT. do_im_time) THEN
     342           36 :             IF (do_kpoints) THEN
     343            0 :                CALL cp_cfm_to_fm(cfm_matrix_Minv_L_kpoints(1), mtargetr=fm_matrix_L_work)
     344              :             ELSE
     345           36 :                CALL cp_fm_to_fm(fm_matrix_Minv_L_gamma(1, 1), fm_matrix_L_work)
     346           36 :                CALL cp_fm_release(fm_matrix_Minv_L_gamma)
     347              :             END IF
     348              : 
     349              :          END IF
     350              : 
     351              :       END IF
     352              : 
     353              :       ! If the group distribution changed because of an SVD, we get the new values here
     354          706 :       CALL get_group_dist(gd_array, color_sub, my_group_L_start, my_group_L_end, my_group_L_size)
     355              : 
     356          706 :       IF (.NOT. do_im_time) THEN
     357          562 :          IF (unit_nr > 0) THEN
     358          281 :             WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") "RI_INFO| Cholesky decomposition group size:", para_env_L%num_pe
     359          281 :             WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") "RI_INFO| Number of groups for auxiliary basis functions", ngroup
     360          281 :             IF (calc_PQ_cond_num .OR. do_svd) THEN
     361              :                WRITE (UNIT=unit_nr, FMT="(T3,A,T67,ES14.5)") &
     362           16 :                   "RI_INFO| Condition number of the (P|Q):", cond_num
     363              :                WRITE (UNIT=unit_nr, FMT="(T3,A,T75,i6)") &
     364           16 :                   "RI_INFO| Number of non-positive Eigenvalues of (P|Q):", num_small_eigen
     365              :             END IF
     366          281 :             CALL m_flush(unit_nr)
     367              :          END IF
     368              : 
     369              :          ! replicate the necessary row of the L^{-1} matrix on each proc
     370          562 :          CALL grep_Lcols(fm_matrix_L_work, my_group_L_start, my_group_L_end, my_group_L_size, my_Lrows)
     371          562 :          IF (calc_forces .AND. .NOT. compare_potential_types(qs_env%mp2_env%ri_metric, &
     372              :                                                              qs_env%mp2_env%potential_parameter)) THEN
     373           12 :             CALL grep_Lcols(fm_matrix_V, my_group_L_start, my_group_L_end, my_group_L_size, my_Vrows)
     374              :          END IF
     375              :       END IF
     376          706 :       CALL cp_fm_release(fm_matrix_L_work)
     377          706 :       IF (calc_forces .AND. .NOT. (do_im_time .OR. compare_potential_types(qs_env%mp2_env%ri_metric, &
     378           12 :                                                                qs_env%mp2_env%potential_parameter))) CALL cp_fm_release(fm_matrix_V)
     379          706 :       CALL mp_para_env_release(para_env_L)
     380              : 
     381          706 :       CALL timestop(handle)
     382              : 
     383         1412 :    END SUBROUTINE get_2c_integrals
     384              : 
     385              : ! **************************************************************************************************
     386              : !> \brief ...
     387              : !> \param fm_matrix_L ...
     388              : !> \param do_svd ...
     389              : !> \param eps_svd ...
     390              : !> \param num_small_eigen ...
     391              : !> \param cond_num ...
     392              : !> \param do_inversion ...
     393              : !> \param gd_array ...
     394              : !> \param ngroup ...
     395              : !> \param dimen_RI ...
     396              : !> \param dimen_RI_red ...
     397              : !> \param para_env ...
     398              : ! **************************************************************************************************
     399          854 :    SUBROUTINE decomp_mat_L(fm_matrix_L, do_svd, eps_svd, num_small_eigen, cond_num, do_inversion, gd_array, ngroup, &
     400              :                            dimen_RI, dimen_RI_red, para_env)
     401              : 
     402              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_matrix_L
     403              :       LOGICAL, INTENT(IN)                                :: do_svd
     404              :       REAL(KIND=dp), INTENT(IN)                          :: eps_svd
     405              :       INTEGER, INTENT(INOUT)                             :: num_small_eigen
     406              :       REAL(KIND=dp), INTENT(INOUT)                       :: cond_num
     407              :       LOGICAL, INTENT(IN)                                :: do_inversion
     408              :       TYPE(group_dist_d1_type), INTENT(INOUT)            :: gd_array
     409              :       INTEGER, INTENT(IN)                                :: ngroup, dimen_RI
     410              :       INTEGER, INTENT(INOUT)                             :: dimen_RI_red
     411              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
     412              : 
     413          854 :       IF (do_svd) THEN
     414           46 :          CALL matrix_root_with_svd(fm_matrix_L, num_small_eigen, cond_num, eps_svd, do_inversion, para_env)
     415              : 
     416           46 :          dimen_RI_red = dimen_RI - num_small_eigen
     417              : 
     418              :          ! We changed the size of fm_matrix_L in matrix_root_with_svd.
     419              :          ! So, we have to get new group sizes
     420           46 :          CALL release_group_dist(gd_array)
     421              : 
     422           46 :          CALL create_group_dist(gd_array, ngroup, dimen_RI_red)
     423              :       ELSE
     424              : 
     425              :          ! calculate Cholesky decomposition L (V=LL^T)
     426          808 :          CALL cholesky_decomp(fm_matrix_L, dimen_RI, do_inversion=do_inversion)
     427              : 
     428          808 :          IF (do_inversion) CALL invert_mat(fm_matrix_L)
     429              :       END IF
     430              : 
     431          854 :    END SUBROUTINE decomp_mat_L
     432              : 
     433              : ! **************************************************************************************************
     434              : !> \brief ...
     435              : !> \param qs_env ...
     436              : !> \param cfm_matrix_L_kpoints ...
     437              : !> \param fm_matrix_L_struct ...
     438              : !> \param kpoints ...
     439              : ! **************************************************************************************************
     440           22 :    SUBROUTINE compute_V_by_lattice_sum(qs_env, cfm_matrix_L_kpoints, fm_matrix_L_struct, kpoints)
     441              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     442              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:)       :: cfm_matrix_L_kpoints
     443              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_matrix_L_struct
     444              :       TYPE(kpoint_type), POINTER                         :: kpoints
     445              : 
     446              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_V_by_lattice_sum'
     447              : 
     448              :       INTEGER                                            :: handle, i_dim, ikp, nkp, nkp_extra, &
     449              :                                                             nkp_orig
     450              :       INTEGER, DIMENSION(3)                              :: nkp_grid_orig, periodic
     451           22 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     452              :       TYPE(cell_type), POINTER                           :: cell
     453              :       TYPE(cp_fm_type)                                   :: fm_matrix_L_work
     454           22 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_RI_aux_transl, matrix_v_RI_kp
     455           22 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     456           22 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     457              : 
     458           22 :       CALL timeset(routineN, handle)
     459              : 
     460           22 :       NULLIFY (matrix_s_RI_aux_transl, particle_set, cell, qs_kind_set)
     461              : 
     462              :       CALL get_qs_env(qs_env=qs_env, &
     463              :                       matrix_s_RI_aux_kp=matrix_s_RI_aux_transl, &
     464              :                       particle_set=particle_set, &
     465              :                       cell=cell, &
     466              :                       qs_kind_set=qs_kind_set, &
     467           22 :                       atomic_kind_set=atomic_kind_set)
     468              : 
     469              :       ! check that we have a 2n x 2m x 2k mesh to guarantee good convergence
     470           22 :       CALL get_cell(cell=cell, periodic=periodic)
     471           88 :       DO i_dim = 1, 3
     472           88 :          IF (periodic(i_dim) == 1) THEN
     473           38 :             CPASSERT(MODULO(kpoints%nkp_grid(i_dim), 2) == 0)
     474              :          END IF
     475              :       END DO
     476              : 
     477           22 :       nkp = kpoints%nkp
     478              : 
     479          494 :       ALLOCATE (cfm_matrix_L_kpoints(nkp))
     480          450 :       DO ikp = 1, nkp
     481          450 :          CALL cp_cfm_create(cfm_matrix_L_kpoints(ikp), fm_matrix_L_struct)
     482              :       END DO
     483           22 :       CALL cp_fm_create(fm_matrix_L_work, fm_matrix_L_struct)
     484              : 
     485           22 :       CALL allocate_matrix_v_RI_kp(matrix_v_RI_kp, matrix_s_RI_aux_transl, nkp)
     486              : 
     487           22 :       IF (qs_env%mp2_env%ri_rpa_im_time%do_extrapolate_kpoints) THEN
     488              : 
     489           16 :          nkp_orig = qs_env%mp2_env%ri_rpa_im_time%nkp_orig
     490           16 :          nkp_extra = qs_env%mp2_env%ri_rpa_im_time%nkp_extra
     491              : 
     492              :          CALL build_2c_coulomb_matrix_kp(matrix_v_RI_kp, kpoints, basis_type="RI_AUX", &
     493              :                                          cell=cell, particle_set=particle_set, qs_kind_set=qs_kind_set, &
     494              :                                          atomic_kind_set=atomic_kind_set, &
     495              :                                          size_lattice_sum=qs_env%mp2_env%mp2_gpw%size_lattice_sum, &
     496           16 :                                          operator_type=operator_coulomb, ikp_start=1, ikp_end=nkp_orig)
     497              : 
     498           64 :          nkp_grid_orig = kpoints%nkp_grid
     499          128 :          kpoints%nkp_grid(1:3) = qs_env%mp2_env%ri_rpa_im_time%kp_grid_extra(1:3)
     500              : 
     501              :          CALL build_2c_coulomb_matrix_kp(matrix_v_RI_kp, kpoints, basis_type="RI_AUX", &
     502              :                                          cell=cell, particle_set=particle_set, qs_kind_set=qs_kind_set, &
     503              :                                          atomic_kind_set=atomic_kind_set, &
     504              :                                          size_lattice_sum=qs_env%mp2_env%mp2_gpw%size_lattice_sum, &
     505           16 :                                          operator_type=operator_coulomb, ikp_start=nkp_orig + 1, ikp_end=nkp)
     506              : 
     507           64 :          kpoints%nkp_grid = nkp_grid_orig
     508              : 
     509              :       ELSE
     510              : 
     511              :          CALL build_2c_coulomb_matrix_kp(matrix_v_RI_kp, kpoints, basis_type="RI_AUX", &
     512              :                                          cell=cell, particle_set=particle_set, qs_kind_set=qs_kind_set, &
     513              :                                          atomic_kind_set=atomic_kind_set, &
     514              :                                          size_lattice_sum=qs_env%mp2_env%mp2_gpw%size_lattice_sum, &
     515            6 :                                          operator_type=operator_coulomb, ikp_start=1, ikp_end=nkp)
     516              : 
     517              :       END IF
     518              : 
     519          450 :       DO ikp = 1, nkp
     520              :          CALL copy_dbcsr_pair_to_cfm(matrix_v_RI_kp(ikp, 1)%matrix, matrix_v_RI_kp(ikp, 2)%matrix, &
     521          450 :                                      fm_matrix_L_work, cfm_matrix_L_kpoints(ikp))
     522              :       END DO
     523           22 :       CALL cp_fm_release(fm_matrix_L_work)
     524              : 
     525           22 :       CALL dbcsr_deallocate_matrix_set(matrix_v_RI_kp)
     526              : 
     527           22 :       CALL timestop(handle)
     528              : 
     529           44 :    END SUBROUTINE compute_V_by_lattice_sum
     530              : 
     531              : ! **************************************************************************************************
     532              : !> \brief ...
     533              : !> \param matrix_v_RI_kp ...
     534              : !> \param matrix_s_RI_aux_transl ...
     535              : !> \param nkp ...
     536              : ! **************************************************************************************************
     537           22 :    SUBROUTINE allocate_matrix_v_RI_kp(matrix_v_RI_kp, matrix_s_RI_aux_transl, nkp)
     538              : 
     539              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_v_RI_kp, matrix_s_RI_aux_transl
     540              :       INTEGER                                            :: nkp
     541              : 
     542              :       INTEGER                                            :: ikp
     543              : 
     544           22 :       NULLIFY (matrix_v_RI_kp)
     545           22 :       CALL dbcsr_allocate_matrix_set(matrix_v_RI_kp, nkp, 2)
     546              : 
     547          450 :       DO ikp = 1, nkp
     548              : 
     549          428 :          ALLOCATE (matrix_v_RI_kp(ikp, 1)%matrix)
     550              :          CALL dbcsr_create(matrix_v_RI_kp(ikp, 1)%matrix, template=matrix_s_RI_aux_transl(1, 1)%matrix, &
     551          428 :                            matrix_type=dbcsr_type_no_symmetry)
     552          428 :          CALL dbcsr_reserve_all_blocks(matrix_v_RI_kp(ikp, 1)%matrix)
     553          428 :          CALL dbcsr_set(matrix_v_RI_kp(ikp, 1)%matrix, 0.0_dp)
     554              : 
     555          428 :          ALLOCATE (matrix_v_RI_kp(ikp, 2)%matrix)
     556              :          CALL dbcsr_create(matrix_v_RI_kp(ikp, 2)%matrix, template=matrix_s_RI_aux_transl(1, 1)%matrix, &
     557          428 :                            matrix_type=dbcsr_type_no_symmetry)
     558          428 :          CALL dbcsr_reserve_all_blocks(matrix_v_RI_kp(ikp, 2)%matrix)
     559          450 :          CALL dbcsr_set(matrix_v_RI_kp(ikp, 2)%matrix, 0.0_dp)
     560              : 
     561              :       END DO
     562              : 
     563           22 :    END SUBROUTINE allocate_matrix_v_RI_kp
     564              : 
     565              : ! **************************************************************************************************
     566              : !> \brief ...
     567              : !> \param qs_env ...
     568              : !> \param fm_matrix_Minv_L_gamma real Gamma-point result ...
     569              : !> \param fm_matrix_L_struct matrix distribution used for the result ...
     570              : !> \param dimen_RI ...
     571              : !> \param ri_metric ...
     572              : !> \param put_mat_KS_env ...
     573              : !> \param regularization_RI ...
     574              : ! **************************************************************************************************
     575          460 :    SUBROUTINE RI_2c_integral_mat(qs_env, fm_matrix_Minv_L_gamma, fm_matrix_L_struct, dimen_RI, ri_metric, &
     576              :                                  put_mat_KS_env, regularization_RI)
     577              : 
     578              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     579              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_matrix_Minv_L_gamma
     580              :       TYPE(cp_fm_struct_type), INTENT(IN), POINTER       :: fm_matrix_L_struct
     581              :       INTEGER, INTENT(IN)                                :: dimen_RI
     582              :       TYPE(coulomb_operator_type)                        :: ri_metric
     583              :       LOGICAL, OPTIONAL                                  :: put_mat_KS_env
     584              :       REAL(KIND=dp), OPTIONAL                            :: regularization_RI
     585              : 
     586              :       CALL RI_2c_integral_mat_core(qs_env, fm_matrix_Minv_L_gamma=fm_matrix_Minv_L_gamma, &
     587              :                                    fm_matrix_L_struct=fm_matrix_L_struct, dimen_RI=dimen_RI, ri_metric=ri_metric, &
     588              :                                    do_kpoints=.FALSE., put_mat_KS_env=put_mat_KS_env, &
     589          460 :                                    regularization_RI=regularization_RI)
     590              : 
     591          460 :    END SUBROUTINE RI_2c_integral_mat
     592              : 
     593              : ! **************************************************************************************************
     594              : !> \brief Computes the complex k-point RI metric as native CFM matrices.
     595              : !> \param qs_env ...
     596              : !> \param cfm_matrix_M_kpoints ...
     597              : !> \param fm_matrix_struct ...
     598              : !> \param dimen_RI ...
     599              : !> \param ri_metric ...
     600              : !> \param kpoints ...
     601              : !> \param put_mat_KS_env ...
     602              : !> \param regularization_RI ...
     603              : !> \param ikp_ext ...
     604              : !> \param do_build_cell_index ...
     605              : ! **************************************************************************************************
     606          604 :    SUBROUTINE RI_2c_integral_mat_kp(qs_env, cfm_matrix_M_kpoints, fm_matrix_struct, dimen_RI, ri_metric, &
     607              :                                     kpoints, put_mat_KS_env, regularization_RI, ikp_ext, do_build_cell_index)
     608              : 
     609              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     610              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:)       :: cfm_matrix_M_kpoints
     611              :       TYPE(cp_fm_struct_type), INTENT(IN), POINTER       :: fm_matrix_struct
     612              :       INTEGER, INTENT(IN)                                :: dimen_RI
     613              :       TYPE(coulomb_operator_type)                        :: ri_metric
     614              :       TYPE(kpoint_type), POINTER                         :: kpoints
     615              :       LOGICAL, OPTIONAL                                  :: put_mat_KS_env
     616              :       REAL(KIND=dp), OPTIONAL                            :: regularization_RI
     617              :       INTEGER, OPTIONAL                                  :: ikp_ext
     618              :       LOGICAL, OPTIONAL                                  :: do_build_cell_index
     619              : 
     620              :       CALL RI_2c_integral_mat_core(qs_env, cfm_matrix_M_kpoints=cfm_matrix_M_kpoints, &
     621              :                                    fm_matrix_L_struct=fm_matrix_struct, dimen_RI=dimen_RI, ri_metric=ri_metric, &
     622              :                                    do_kpoints=.TRUE., kpoints=kpoints, put_mat_KS_env=put_mat_KS_env, &
     623              :                                    regularization_RI=regularization_RI, ikp_ext=ikp_ext, &
     624          604 :                                    do_build_cell_index=do_build_cell_index)
     625              : 
     626          604 :    END SUBROUTINE RI_2c_integral_mat_kp
     627              : 
     628              : ! **************************************************************************************************
     629              : !> \brief Shared DBCSR evaluator with representation-specific matrix output adapters.
     630              : !> \param qs_env ...
     631              : !> \param fm_matrix_Minv_L_gamma ...
     632              : !> \param cfm_matrix_M_kpoints ...
     633              : !> \param fm_matrix_L_struct ...
     634              : !> \param dimen_RI ...
     635              : !> \param ri_metric ...
     636              : !> \param do_kpoints ...
     637              : !> \param kpoints ...
     638              : !> \param put_mat_KS_env ...
     639              : !> \param regularization_RI ...
     640              : !> \param ikp_ext ...
     641              : !> \param do_build_cell_index ...
     642              : ! **************************************************************************************************
     643         1064 :    SUBROUTINE RI_2c_integral_mat_core(qs_env, fm_matrix_Minv_L_gamma, cfm_matrix_M_kpoints, &
     644              :                                       fm_matrix_L_struct, dimen_RI, ri_metric, do_kpoints, kpoints, &
     645              :                                       put_mat_KS_env, regularization_RI, ikp_ext, do_build_cell_index)
     646              : 
     647              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     648              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :), &
     649              :          INTENT(OUT), OPTIONAL                           :: fm_matrix_Minv_L_gamma
     650              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:), &
     651              :          INTENT(OUT), OPTIONAL                           :: cfm_matrix_M_kpoints
     652              :       TYPE(cp_fm_struct_type), INTENT(IN), POINTER       :: fm_matrix_L_struct
     653              :       INTEGER, INTENT(IN)                                :: dimen_RI
     654              :       TYPE(coulomb_operator_type)                        :: ri_metric
     655              :       LOGICAL, INTENT(IN)                                :: do_kpoints
     656              :       TYPE(kpoint_type), OPTIONAL, POINTER               :: kpoints
     657              :       LOGICAL, OPTIONAL                                  :: put_mat_KS_env
     658              :       REAL(KIND=dp), OPTIONAL                            :: regularization_RI
     659              :       INTEGER, OPTIONAL                                  :: ikp_ext
     660              :       LOGICAL, OPTIONAL                                  :: do_build_cell_index
     661              : 
     662              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'RI_2c_integral_mat_core'
     663              : 
     664              :       INTEGER                                            :: handle, ikp, ikp_for_xkp, img, natom, &
     665              :                                                             nimg, nkind, nkp
     666         1064 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: sizes_RI
     667         1064 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: col_bsize, row_bsize
     668              :       LOGICAL                                            :: my_do_build_cell_index, my_put_mat_KS_env
     669              :       REAL(KIND=dp)                                      :: my_regularization_RI
     670              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     671              :       TYPE(cp_cfm_type)                                  :: cfm_matrix_S_global
     672              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     673              :       TYPE(cp_fm_type)                                   :: fm_matrix_S_global
     674         1064 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)     :: fm_matrix_M_kpoints
     675              :       TYPE(dbcsr_distribution_type)                      :: dbcsr_dist
     676         1064 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_s_RI_aux_transl
     677         1064 :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:)        :: mat_2c
     678              :       TYPE(dbcsr_type), POINTER                          :: matrix_s_RI_aux_desymm
     679              :       TYPE(dft_control_type), POINTER                    :: dft_control
     680              :       TYPE(distribution_2d_type), POINTER                :: dist_2d
     681         1064 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_RI
     682              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     683              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     684         1064 :          POINTER                                         :: sab_RI
     685         1064 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     686         1064 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     687         4256 :       TYPE(rskp_prepared_transform_type)                 :: transform
     688              : 
     689         1064 :       CALL timeset(routineN, handle)
     690              : 
     691         1064 :       IF (do_kpoints) THEN
     692          604 :          CPASSERT(PRESENT(cfm_matrix_M_kpoints))
     693              :       ELSE
     694          460 :          CPASSERT(PRESENT(fm_matrix_Minv_L_gamma))
     695              :       END IF
     696              : 
     697         1064 :       NULLIFY (sab_RI, matrix_s_RI_aux_transl, dist_2d)
     698              : 
     699         1064 :       IF (PRESENT(regularization_RI)) THEN
     700          892 :          my_regularization_RI = regularization_RI
     701              :       ELSE
     702          172 :          my_regularization_RI = 0.0_dp
     703              :       END IF
     704              : 
     705         1064 :       IF (PRESENT(put_mat_KS_env)) THEN
     706          188 :          my_put_mat_KS_env = put_mat_KS_env
     707              :       ELSE
     708              :          my_put_mat_KS_env = .FALSE.
     709              :       END IF
     710              : 
     711         1064 :       IF (PRESENT(do_build_cell_index)) THEN
     712          582 :          my_do_build_cell_index = do_build_cell_index
     713              :       ELSE
     714              :          my_do_build_cell_index = .FALSE.
     715              :       END IF
     716              : 
     717              :       CALL get_qs_env(qs_env=qs_env, &
     718              :                       para_env=para_env, &
     719              :                       blacs_env=blacs_env, &
     720              :                       nkind=nkind, &
     721              :                       distribution_2d=dist_2d, &
     722              :                       qs_kind_set=qs_kind_set, &
     723              :                       particle_set=particle_set, &
     724              :                       dft_control=dft_control, &
     725         1064 :                       natom=natom)
     726              : 
     727         3192 :       ALLOCATE (sizes_RI(natom))
     728         5062 :       ALLOCATE (basis_set_RI(nkind))
     729              : 
     730         1064 :       CALL basis_set_list_setup(basis_set_RI, 'RI_AUX', qs_kind_set)
     731         1064 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=sizes_RI, basis=basis_set_RI)
     732              : 
     733              :       CALL build_2c_neighbor_lists(sab_RI, basis_set_RI, basis_set_RI, ri_metric, "RPA_2c_nl_RI", qs_env, &
     734         1064 :                                    sym_ij=.TRUE., dist_2d=dist_2d)
     735              : 
     736         1064 :       CALL cp_dbcsr_dist2d_to_dist(dist_2d, dbcsr_dist)
     737         3192 :       ALLOCATE (row_bsize(SIZE(sizes_RI)))
     738         2128 :       ALLOCATE (col_bsize(SIZE(sizes_RI)))
     739         3776 :       row_bsize(:) = sizes_RI
     740         3776 :       col_bsize(:) = sizes_RI
     741              : 
     742         1064 :       IF (do_kpoints) THEN
     743          604 :          CPASSERT(PRESENT(kpoints))
     744          604 :          IF (my_do_build_cell_index) THEN
     745           84 :             CALL kpoint_init_cell_index(kpoints, sab_RI, para_env, dft_control%nimages)
     746              :          END IF
     747          604 :          CALL get_kpoint_info(kpoints, nkp=nkp)
     748          604 :          nimg = dft_control%nimages
     749              :       ELSE
     750          460 :          nkp = 1
     751          460 :          nimg = 1
     752              :       END IF
     753              : 
     754        10222 :       ALLOCATE (mat_2c(nimg))
     755              :       CALL dbcsr_create(mat_2c(1), "(RI|RI)", dbcsr_dist, dbcsr_type_symmetric, &
     756         1064 :                         row_bsize, col_bsize)
     757         1064 :       DEALLOCATE (row_bsize, col_bsize)
     758              : 
     759         7030 :       DO img = 2, nimg
     760         7030 :          CALL dbcsr_create(mat_2c(img), template=mat_2c(1))
     761              :       END DO
     762              : 
     763              :       CALL build_2c_integrals(mat_2c, 0.0_dp, qs_env, sab_RI, basis_set_RI, basis_set_RI, &
     764              :                               ri_metric, do_kpoints=do_kpoints, ext_kpoints=kpoints, &
     765         1064 :                               regularization_RI=my_regularization_RI)
     766              : 
     767         1064 :       CALL dbcsr_distribution_release(dbcsr_dist)
     768         1064 :       DEALLOCATE (basis_set_RI)
     769              : 
     770         1064 :       IF (my_put_mat_KS_env) THEN
     771          144 :          CALL get_ks_env(qs_env%ks_env, matrix_s_RI_aux_kp=matrix_s_RI_aux_transl)
     772          144 :          IF (ASSOCIATED(matrix_s_RI_aux_transl)) CALL dbcsr_deallocate_matrix_set(matrix_s_RI_aux_transl)
     773              :       END IF
     774         1064 :       CALL dbcsr_allocate_matrix_set(matrix_s_RI_aux_transl, 1, nimg)
     775         8094 :       DO img = 1, nimg
     776         7030 :          ALLOCATE (matrix_s_RI_aux_transl(1, img)%matrix)
     777         7030 :          CALL dbcsr_copy(matrix_s_RI_aux_transl(1, img)%matrix, mat_2c(img))
     778         8094 :          CALL dbcsr_release(mat_2c(img))
     779              :       END DO
     780              : 
     781         1064 :       IF (my_put_mat_KS_env) THEN
     782          144 :          CALL set_ks_env(qs_env%ks_env, matrix_s_RI_aux_kp=matrix_s_RI_aux_transl)
     783              :       END IF
     784              : 
     785         1064 :       IF (PRESENT(ikp_ext)) nkp = 1
     786              : 
     787         1064 :       IF (do_kpoints) THEN
     788         2822 :          ALLOCATE (cfm_matrix_M_kpoints(nkp))
     789         1614 :          DO ikp = 1, nkp
     790         1614 :             CALL cp_cfm_create(cfm_matrix_M_kpoints(ikp), fm_matrix_L_struct)
     791              :          END DO
     792              :       ELSE
     793         1380 :          ALLOCATE (fm_matrix_M_kpoints(1, 1))
     794          460 :          CALL cp_fm_create(fm_matrix_M_kpoints(1, 1), fm_matrix_L_struct)
     795          460 :          CALL cp_fm_set_all(fm_matrix_M_kpoints(1, 1), 0.0_dp)
     796              :       END IF
     797              : 
     798         1064 :       NULLIFY (fm_struct)
     799              :       CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=dimen_RI, &
     800         1064 :                                ncol_global=dimen_RI, para_env=para_env)
     801              : 
     802         1064 :       IF (do_kpoints) THEN
     803          604 :          CALL cp_cfm_create(cfm_matrix_S_global, fm_struct, set_zero=.TRUE.)
     804              :       ELSE
     805          460 :          CALL cp_fm_create(fm_matrix_S_global, fm_struct)
     806          460 :          CALL cp_fm_set_all(fm_matrix_S_global, 0.0_dp)
     807              :       END IF
     808              : 
     809         1064 :       IF (do_kpoints) THEN
     810              :          CALL rskp_transform_prepare(transform, matrix_s_RI_aux_transl, [1], sab_RI, &
     811              :                                      kpoints%cell_to_index, fm_struct, world=para_env, &
     812              :                                      backend=kpoints%rskp_transform, xkp=kpoints%xkp, &
     813          604 :                                      nkp_grid=kpoints%nkp_grid)
     814              : 
     815         1614 :          DO ikp = 1, nkp
     816         1010 :             ikp_for_xkp = ikp
     817         1010 :             IF (PRESENT(ikp_ext)) ikp_for_xkp = ikp_ext
     818              :             CALL rskp_transform_evaluate(transform, 1, ikp_for_xkp, kpoints%xkp, &
     819         1010 :                                          cfm=cfm_matrix_S_global)
     820         1614 :             CALL cp_cfm_copy_general(cfm_matrix_S_global, cfm_matrix_M_kpoints(ikp), para_env)
     821              : 
     822              :          END DO
     823              : 
     824          604 :          CALL rskp_transform_release(transform)
     825              : 
     826              :       ELSE
     827              : 
     828              :          NULLIFY (matrix_s_RI_aux_desymm)
     829          460 :          ALLOCATE (matrix_s_RI_aux_desymm)
     830              :          CALL dbcsr_create(matrix_s_RI_aux_desymm, template=matrix_s_RI_aux_transl(1, 1)%matrix, &
     831          460 :                            name='S_RI non_symm', matrix_type=dbcsr_type_no_symmetry)
     832              : 
     833          460 :          CALL dbcsr_desymmetrize(matrix_s_RI_aux_transl(1, 1)%matrix, matrix_s_RI_aux_desymm)
     834              : 
     835          460 :          CALL copy_dbcsr_to_fm(matrix_s_RI_aux_desymm, fm_matrix_S_global)
     836              : 
     837          460 :          CALL cp_fm_copy_general(fm_matrix_S_global, fm_matrix_M_kpoints(1, 1), para_env)
     838              : 
     839          460 :          CALL dbcsr_deallocate_matrix(matrix_s_RI_aux_desymm)
     840              : 
     841              :       END IF
     842              : 
     843         1064 :       CALL release_neighbor_list_sets(sab_RI)
     844              : 
     845         1064 :       CALL cp_fm_struct_release(fm_struct)
     846              : 
     847         1064 :       IF (.NOT. do_kpoints) CALL cp_fm_release(fm_matrix_S_global)
     848              : 
     849         1064 :       IF (do_kpoints) THEN
     850          604 :          CALL cp_cfm_release(cfm_matrix_S_global)
     851              :       ELSE
     852          460 :          CPASSERT(PRESENT(fm_matrix_Minv_L_gamma))
     853          460 :          CALL MOVE_ALLOC(fm_matrix_M_kpoints, fm_matrix_Minv_L_gamma)
     854              :       END IF
     855              : 
     856         1064 :       IF (.NOT. my_put_mat_KS_env) THEN
     857          920 :          CALL dbcsr_deallocate_matrix_set(matrix_s_RI_aux_transl)
     858              :       END IF
     859              : 
     860         1064 :       CALL timestop(handle)
     861              : 
     862         4256 :    END SUBROUTINE RI_2c_integral_mat_core
     863              : 
     864              : ! **************************************************************************************************
     865              : !> \brief Copies a real/imaginary DBCSR pair into its native complex full-matrix representation.
     866              : !> \param matrix_re ...
     867              : !> \param matrix_im ...
     868              : !> \param fm_work ...
     869              : !> \param cfm_matrix ...
     870              : ! **************************************************************************************************
     871          428 :    SUBROUTINE copy_dbcsr_pair_to_cfm(matrix_re, matrix_im, fm_work, cfm_matrix)
     872              :       TYPE(dbcsr_type), POINTER                          :: matrix_re, matrix_im
     873              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: fm_work
     874              :       TYPE(cp_cfm_type), INTENT(INOUT)                   :: cfm_matrix
     875              : 
     876          428 :       CALL cp_fm_set_all(fm_work, 0.0_dp)
     877          428 :       CALL copy_dbcsr_to_fm(matrix_re, fm_work)
     878          428 :       CALL cp_fm_to_cfm(msourcer=fm_work, mtarget=cfm_matrix)
     879              : 
     880          428 :       CALL cp_fm_set_all(fm_work, 0.0_dp)
     881          428 :       CALL copy_dbcsr_to_fm(matrix_im, fm_work)
     882          428 :       CALL cp_cfm_scale_and_add_fm(z_one, cfm_matrix, gaussi, fm_work)
     883          428 :    END SUBROUTINE copy_dbcsr_pair_to_cfm
     884              : 
     885              : ! **************************************************************************************************
     886              : !> \brief ...
     887              : !> \param qs_env ...
     888              : !> \param eri_method ...
     889              : !> \param eri_param ...
     890              : !> \param para_env ...
     891              : !> \param para_env_sub ...
     892              : !> \param para_env_L ...
     893              : !> \param mp2_memory ...
     894              : !> \param fm_matrix_L ...
     895              : !> \param ngroup ...
     896              : !> \param color_sub ...
     897              : !> \param dimen_RI ...
     898              : !> \param my_group_L_size ...
     899              : !> \param my_group_L_start ...
     900              : !> \param my_group_L_end ...
     901              : !> \param gd_array ...
     902              : !> \param calc_PQ_cond_num ...
     903              : !> \param cond_num ...
     904              : !> \param num_small_eigen ...
     905              : !> \param potential ...
     906              : !> \param sab_orb_sub ...
     907              : !> \param do_im_time ...
     908              : !> \param fm_matrix_L_struct ...
     909              : ! **************************************************************************************************
     910          742 :    SUBROUTINE compute_2c_integrals(qs_env, eri_method, eri_param, para_env, para_env_sub, para_env_L, mp2_memory, &
     911              :                                    fm_matrix_L, ngroup, color_sub, dimen_RI, &
     912              :                                    my_group_L_size, my_group_L_start, my_group_L_end, &
     913              :                                    gd_array, calc_PQ_cond_num, cond_num, num_small_eigen, potential, &
     914              :                                    sab_orb_sub, do_im_time, fm_matrix_L_struct)
     915              : 
     916              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     917              :       INTEGER, INTENT(IN)                                :: eri_method
     918              :       TYPE(cp_eri_mme_param), POINTER                    :: eri_param
     919              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
     920              :       TYPE(mp_para_env_type), INTENT(IN), POINTER        :: para_env_sub
     921              :       TYPE(mp_para_env_type), INTENT(OUT), POINTER       :: para_env_L
     922              :       REAL(KIND=dp), INTENT(IN)                          :: mp2_memory
     923              :       TYPE(cp_fm_type), INTENT(OUT)                      :: fm_matrix_L
     924              :       INTEGER, INTENT(IN)                                :: ngroup, color_sub
     925              :       INTEGER, INTENT(OUT)                               :: dimen_RI, my_group_L_size, &
     926              :                                                             my_group_L_start, my_group_L_end
     927              :       TYPE(group_dist_d1_type), INTENT(OUT)              :: gd_array
     928              :       LOGICAL, INTENT(IN)                                :: calc_PQ_cond_num
     929              :       REAL(KIND=dp), INTENT(OUT)                         :: cond_num
     930              :       INTEGER, INTENT(OUT)                               :: num_small_eigen
     931              :       TYPE(coulomb_operator_type), INTENT(IN)            :: potential
     932              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     933              :          POINTER                                         :: sab_orb_sub
     934              :       LOGICAL, INTENT(IN), OPTIONAL                      :: do_im_time
     935              :       TYPE(cp_fm_struct_type), OPTIONAL, POINTER         :: fm_matrix_L_struct
     936              : 
     937              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_2c_integrals'
     938              : 
     939              :       INTEGER :: best_group_size, color_L, group_size, handle, handle2, i_global, iatom, iiB, &
     940              :          ikind, iproc, j_global, jjB, natom, ncol_local, nkind, nrow_local, nsgf, potential_type, &
     941              :          proc_receive, proc_receive_static, proc_send_static, proc_shift, rec_L_end, rec_L_size, &
     942              :          rec_L_start, strat_group_size
     943          742 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
     944          742 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
     945              :       LOGICAL                                            :: my_do_im_time
     946              :       REAL(KIND=dp)                                      :: min_mem_for_QK
     947          742 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: egen_L
     948          742 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: L_external_col, L_local_col
     949          742 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     950              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env_L
     951              :       TYPE(cp_fm_type)                                   :: fm_matrix_L_diag
     952          742 :       TYPE(group_dist_d1_type)                           :: gd_sub_array
     953              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a
     954          742 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     955          742 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     956              : 
     957          742 :       CALL timeset(routineN, handle)
     958              : 
     959          742 :       my_do_im_time = .FALSE.
     960          742 :       IF (PRESENT(do_im_time)) THEN
     961          706 :          my_do_im_time = do_im_time
     962              :       END IF
     963              : 
     964              :       ! get stuff
     965              :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
     966          742 :                       particle_set=particle_set)
     967              : 
     968          742 :       nkind = SIZE(qs_kind_set)
     969          742 :       natom = SIZE(particle_set)
     970              : 
     971          742 :       CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
     972              : 
     973         2094 :       DO ikind = 1, nkind
     974         1352 :          CALL get_qs_kind(qs_kind=qs_kind_set(ikind), basis_set=basis_set_a, basis_type="RI_AUX")
     975         2094 :          CPASSERT(ASSOCIATED(basis_set_a))
     976              :       END DO
     977              : 
     978          742 :       dimen_RI = 0
     979         2904 :       DO iatom = 1, natom
     980         2162 :          ikind = kind_of(iatom)
     981         2162 :          CALL get_qs_kind(qs_kind=qs_kind_set(ikind), nsgf=nsgf, basis_type="RI_AUX")
     982         2904 :          dimen_RI = dimen_RI + nsgf
     983              :       END DO
     984              : 
     985              :       ! check that very small systems are not running on too many processes
     986          742 :       IF (dimen_RI < ngroup) THEN
     987              :          CALL cp_abort(__LOCATION__, "Product of block size and number "// &
     988            0 :                        "of RI functions should not exceed total number of processes")
     989              :       END IF
     990              : 
     991          742 :       CALL create_group_dist(gd_array, ngroup, dimen_RI)
     992              : 
     993          742 :       CALL get_group_dist(gd_array, color_sub, my_group_L_start, my_group_L_end, my_group_L_size)
     994              : 
     995          742 :       CALL timeset(routineN//"_loop_lm", handle2)
     996              : 
     997         2968 :       ALLOCATE (L_local_col(dimen_RI, my_group_L_size))
     998          742 :       L_local_col = 0.0_dp
     999              : 
    1000          742 :       potential_type = potential%potential_type
    1001              : 
    1002              :       IF ((eri_method == do_eri_mme .OR. eri_method == do_eri_os) &
    1003          742 :           .AND. potential_type == do_potential_coulomb .OR. &
    1004              :           (eri_method == do_eri_mme .AND. potential_type == do_potential_long)) THEN
    1005              : 
    1006              :          CALL mp2_eri_2c_integrate(eri_param, potential, para_env_sub, qs_env, &
    1007              :                                    basis_type_a="RI_AUX", basis_type_b="RI_AUX", &
    1008              :                                    hab=L_local_col, first_b=my_group_L_start, last_b=my_group_L_end, &
    1009          344 :                                    eri_method=eri_method)
    1010              : 
    1011              :       ELSE IF (eri_method == do_eri_gpw .OR. &
    1012              :                (potential_type == do_potential_long .AND. qs_env%mp2_env%eri_method == do_eri_os) &
    1013          398 :                .OR. (potential_type == do_potential_id .AND. qs_env%mp2_env%eri_method == do_eri_mme)) THEN
    1014              : 
    1015              :          CALL mp2_eri_2c_integrate_gpw(qs_env, para_env_sub, my_group_L_start, my_group_L_end, &
    1016          398 :                                        natom, potential, sab_orb_sub, L_local_col, kind_of)
    1017              : 
    1018              :       ELSE
    1019            0 :          CPABORT("unknown ERI method")
    1020              :       END IF
    1021              : 
    1022          742 :       CALL timestop(handle2)
    1023              : 
    1024              :       ! split the total number of proc in a subgroup of the size of ~1/10 of the
    1025              :       ! total num of proc
    1026          742 :       best_group_size = para_env%num_pe
    1027              : 
    1028          742 :       strat_group_size = MAX(1, para_env%num_pe/10)
    1029              : 
    1030          742 :       min_mem_for_QK = REAL(dimen_RI, KIND=dp)*dimen_RI*3.0_dp*8.0_dp/1024_dp/1024_dp
    1031              : 
    1032          742 :       group_size = strat_group_size - 1
    1033          776 :       DO iproc = strat_group_size, para_env%num_pe
    1034          776 :          group_size = group_size + 1
    1035              :          ! check that group_size is a multiple of sub_group_size and a divisor of
    1036              :          ! the total num of proc
    1037          776 :          IF (MOD(para_env%num_pe, group_size) /= 0 .OR. MOD(group_size, para_env_sub%num_pe) /= 0) CYCLE
    1038              : 
    1039              :          ! check for memory
    1040          742 :          IF (REAL(group_size, KIND=dp)*mp2_memory < min_mem_for_QK) CYCLE
    1041              : 
    1042              :          best_group_size = group_size
    1043           34 :          EXIT
    1044              :       END DO
    1045              : 
    1046          742 :       IF (my_do_im_time) THEN
    1047              :          ! para_env_L is the para_env for im. time to avoid bug
    1048          144 :          best_group_size = para_env%num_pe
    1049              :       END IF
    1050              : 
    1051              :       ! create the L group
    1052          742 :       color_L = para_env%mepos/best_group_size
    1053          742 :       ALLOCATE (para_env_L)
    1054          742 :       CALL para_env_L%from_split(para_env, color_L)
    1055              : 
    1056              :       ! create the blacs_L
    1057          742 :       NULLIFY (blacs_env_L)
    1058          742 :       CALL cp_blacs_env_create(blacs_env=blacs_env_L, para_env=para_env_L)
    1059              : 
    1060              :       ! create the full matrix L defined in the L group
    1061          742 :       CALL create_matrix_L(fm_matrix_L, blacs_env_L, dimen_RI, para_env_L, "fm_matrix_L", fm_matrix_L_struct)
    1062              : 
    1063          742 :       IF (my_do_im_time .AND. para_env%num_pe > 1) THEN
    1064              : 
    1065              :          CALL cp_array_columns_to_fm(fm_matrix_L, L_local_col, para_env, &
    1066              :                                      my_group_L_start, my_group_L_end, &
    1067          144 :                                      dimen_RI)
    1068              : 
    1069              :       ELSE
    1070          598 :          BLOCK
    1071              :             TYPE(mp_comm_type) :: comm_exchange
    1072              : 
    1073          598 :             CALL comm_exchange%from_split(para_env_L, para_env_sub%mepos)
    1074              : 
    1075              :             CALL create_group_dist(gd_sub_array, my_group_L_start, &
    1076          598 :                                    my_group_L_end, my_group_L_size, comm_exchange)
    1077              : 
    1078              :             CALL cp_fm_get_info(matrix=fm_matrix_L, &
    1079              :                                 nrow_local=nrow_local, &
    1080              :                                 ncol_local=ncol_local, &
    1081              :                                 row_indices=row_indices, &
    1082          598 :                                 col_indices=col_indices)
    1083              : 
    1084        45476 :             DO jjB = 1, ncol_local
    1085        44878 :                j_global = col_indices(jjB)
    1086        45476 :                IF (j_global >= my_group_L_start .AND. j_global <= my_group_L_end) THEN
    1087      1882990 :                   DO iiB = 1, nrow_local
    1088      1859431 :                      i_global = row_indices(iiB)
    1089      1882990 :                      fm_matrix_L%local_data(iiB, jjB) = L_local_col(i_global, j_global - my_group_L_start + 1)
    1090              :                   END DO
    1091              :                END IF
    1092              :             END DO
    1093              : 
    1094          598 :             proc_send_static = MODULO(comm_exchange%mepos + 1, comm_exchange%num_pe)
    1095          598 :             proc_receive_static = MODULO(comm_exchange%mepos - 1, comm_exchange%num_pe)
    1096              : 
    1097          598 :             DO proc_shift = 1, comm_exchange%num_pe - 1
    1098            0 :                proc_receive = MODULO(comm_exchange%mepos - proc_shift, comm_exchange%num_pe)
    1099              : 
    1100            0 :                CALL get_group_dist(gd_sub_array, proc_receive, rec_L_start, rec_L_end, rec_L_size)
    1101              : 
    1102            0 :                ALLOCATE (L_external_col(dimen_RI, rec_L_size))
    1103            0 :                L_external_col = 0.0_dp
    1104              : 
    1105            0 :                CALL comm_exchange%sendrecv(L_local_col, proc_send_static, L_external_col, proc_receive_static)
    1106              : 
    1107            0 :                DO jjB = 1, ncol_local
    1108            0 :                   j_global = col_indices(jjB)
    1109            0 :                   IF (j_global >= rec_L_start .AND. j_global <= rec_L_end) THEN
    1110            0 :                      DO iiB = 1, nrow_local
    1111            0 :                         i_global = row_indices(iiB)
    1112            0 :                         fm_matrix_L%local_data(iiB, jjB) = L_external_col(i_global, j_global - rec_L_start + 1)
    1113              :                      END DO
    1114              :                   END IF
    1115              :                END DO
    1116              : 
    1117          598 :                CALL MOVE_ALLOC(L_external_col, L_local_col)
    1118              :             END DO
    1119              : 
    1120          598 :             CALL release_group_dist(gd_sub_array)
    1121         1196 :             CALL comm_exchange%free()
    1122              :          END BLOCK
    1123              :       END IF
    1124              : 
    1125          742 :       DEALLOCATE (L_local_col)
    1126              : 
    1127              :       ! create the new group for the sum of the local data over all processes
    1128              :       BLOCK
    1129              :          TYPE(mp_comm_type) :: comm_exchange
    1130          742 :          comm_exchange = fm_matrix_L%matrix_struct%context%interconnect(para_env)
    1131          742 :          CALL comm_exchange%sum(fm_matrix_L%local_data)
    1132         1484 :          CALL comm_exchange%free()
    1133              :       END BLOCK
    1134              : 
    1135          742 :       cond_num = 1.0_dp
    1136          742 :       num_small_eigen = 0
    1137          742 :       IF (calc_PQ_cond_num) THEN
    1138              :          ! calculate the condition number of the (P|Q) matrix
    1139              :          ! create a copy of the matrix
    1140              : 
    1141            0 :          CALL create_matrix_L(fm_matrix_L_diag, blacs_env_L, dimen_RI, para_env_L, "fm_matrix_L_diag", fm_matrix_L_struct)
    1142              : 
    1143            0 :          CALL cp_fm_to_fm(source=fm_matrix_L, destination=fm_matrix_L_diag)
    1144              : 
    1145            0 :          ALLOCATE (egen_L(dimen_RI))
    1146              : 
    1147            0 :          egen_L = 0.0_dp
    1148            0 :          CALL cp_fm_syevx(matrix=fm_matrix_L_diag, eigenvalues=egen_L)
    1149              : 
    1150            0 :          num_small_eigen = 0
    1151            0 :          DO iiB = 1, dimen_RI
    1152            0 :             IF (ABS(egen_L(iiB)) < 0.001_dp) num_small_eigen = num_small_eigen + 1
    1153              :          END DO
    1154              : 
    1155            0 :          cond_num = MAXVAL(ABS(egen_L))/MINVAL(ABS(egen_L))
    1156              : 
    1157            0 :          CALL cp_fm_release(fm_matrix_L_diag)
    1158              : 
    1159            0 :          DEALLOCATE (egen_L)
    1160              :       END IF
    1161              : 
    1162              :       ! release blacs_env
    1163          742 :       CALL cp_blacs_env_release(blacs_env_L)
    1164              : 
    1165          742 :       CALL timestop(handle)
    1166              : 
    1167         3710 :    END SUBROUTINE compute_2c_integrals
    1168              : 
    1169              : ! **************************************************************************************************
    1170              : !> \brief ...
    1171              : !> \param matrix ...
    1172              : !> \param num_small_evals ...
    1173              : !> \param cond_num ...
    1174              : !> \param eps_svd ...
    1175              : !> \param do_inversion ...
    1176              : !> \param para_env ...
    1177              : ! **************************************************************************************************
    1178           46 :    SUBROUTINE matrix_root_with_svd(matrix, num_small_evals, cond_num, eps_svd, do_inversion, para_env)
    1179              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: matrix
    1180              :       INTEGER, INTENT(OUT)                               :: num_small_evals
    1181              :       REAL(KIND=dp), INTENT(OUT)                         :: cond_num
    1182              :       REAL(KIND=dp), INTENT(IN)                          :: eps_svd
    1183              :       LOGICAL, INTENT(IN)                                :: do_inversion
    1184              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
    1185              : 
    1186              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'matrix_root_with_svd'
    1187              : 
    1188              :       INTEGER                                            :: group_size_L, handle, ii, needed_evals, &
    1189              :                                                             nrow, pos_max(1)
    1190           46 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: num_eval
    1191              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: evals
    1192              :       TYPE(cp_fm_type)                                   :: evecs
    1193              :       TYPE(mp_comm_type)                                 :: comm_exchange
    1194              : 
    1195           46 :       CALL timeset(routineN, handle)
    1196              : 
    1197              :       ! Create arrays carrying eigenvalues and eigenvectors later
    1198           46 :       CALL cp_fm_get_info(matrix=matrix, nrow_global=nrow)
    1199              : 
    1200          138 :       ALLOCATE (evals(nrow))
    1201           46 :       evals = 0
    1202              : 
    1203           46 :       CALL cp_fm_create(evecs, matrix%matrix_struct)
    1204              : 
    1205              :       ! Perform the EVD (=SVD of a positive semidefinite matrix)
    1206           46 :       CALL choose_eigv_solver(matrix, evecs, evals)
    1207              : 
    1208              :       ! Determine the number of neglectable eigenvalues assuming that the eigenvalues are in ascending order
    1209           46 :       num_small_evals = 0
    1210          204 :       DO ii = 1, nrow
    1211          204 :          IF (evals(ii) > eps_svd) THEN
    1212           46 :             num_small_evals = ii - 1
    1213           46 :             EXIT
    1214              :          END IF
    1215              :       END DO
    1216           46 :       needed_evals = nrow - num_small_evals
    1217              : 
    1218              :       ! Get the condition number w.r.t. considered eigenvalues
    1219           46 :       cond_num = evals(nrow)/evals(num_small_evals + 1)
    1220              : 
    1221              :       ! Determine the eigenvalues of the request matrix root or its inverse
    1222          204 :       evals(1:num_small_evals) = 0.0_dp
    1223           46 :       IF (do_inversion) THEN
    1224         1008 :          evals(num_small_evals + 1:nrow) = 1.0_dp/SQRT(evals(num_small_evals + 1:nrow))
    1225              :       ELSE
    1226         2754 :          evals(num_small_evals + 1:nrow) = SQRT(evals(num_small_evals + 1:nrow))
    1227              :       END IF
    1228              : 
    1229           46 :       CALL cp_fm_column_scale(evecs, evals)
    1230              : 
    1231              :       ! As it turns out, the results in the subgroups differ.
    1232              :       ! Thus, we have to equilibrate the results.
    1233              :       ! Step 1: Get a communicator connecting processes with same id within subgroups
    1234              :       ! use that group_size_L is divisible by the total number of processes (see above)
    1235           46 :       group_size_L = para_env%num_pe/matrix%matrix_struct%para_env%num_pe
    1236           46 :       comm_exchange = matrix%matrix_struct%context%interconnect(para_env)
    1237              : 
    1238          138 :       ALLOCATE (num_eval(0:group_size_L - 1))
    1239           46 :       CALL comm_exchange%allgather(num_small_evals, num_eval)
    1240              : 
    1241          122 :       num_small_evals = MINVAL(num_eval)
    1242              : 
    1243          122 :       IF (num_small_evals /= MAXVAL(num_eval)) THEN
    1244              :          ! Step 2: Get position of maximum value
    1245            0 :          pos_max = MAXLOC(num_eval)
    1246            0 :          num_small_evals = num_eval(pos_max(1))
    1247            0 :          needed_evals = nrow - num_small_evals
    1248              : 
    1249              :          ! Step 3: Broadcast your local data to all other processes
    1250            0 :          CALL comm_exchange%bcast(evecs%local_data, pos_max(1))
    1251            0 :          CALL comm_exchange%bcast(cond_num, pos_max(1))
    1252              :       END IF
    1253              : 
    1254           46 :       DEALLOCATE (num_eval)
    1255              : 
    1256           46 :       CALL comm_exchange%free()
    1257              : 
    1258           46 :       CALL reset_size_matrix(matrix, needed_evals, matrix%matrix_struct)
    1259              : 
    1260              :       ! Copy the needed eigenvectors
    1261           46 :       CALL cp_fm_to_fm(evecs, matrix, needed_evals, num_small_evals + 1)
    1262              : 
    1263           46 :       CALL cp_fm_release(evecs)
    1264              : 
    1265           46 :       CALL timestop(handle)
    1266              : 
    1267          138 :    END SUBROUTINE matrix_root_with_svd
    1268              : 
    1269              : ! **************************************************************************************************
    1270              : !> \brief ...
    1271              : !> \param matrix ...
    1272              : !> \param new_size ...
    1273              : !> \param fm_struct_template ...
    1274              : ! **************************************************************************************************
    1275          160 :    SUBROUTINE reset_size_matrix(matrix, new_size, fm_struct_template)
    1276              :       TYPE(cp_fm_type), INTENT(INOUT)                    :: matrix
    1277              :       INTEGER, INTENT(IN)                                :: new_size
    1278              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_template
    1279              : 
    1280              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'reset_size_matrix'
    1281              : 
    1282              :       INTEGER                                            :: handle
    1283              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1284              : 
    1285           80 :       CALL timeset(routineN, handle)
    1286              : 
    1287              :       ! Choose the block sizes as large as in the template for the later steps
    1288           80 :       NULLIFY (fm_struct)
    1289           80 :       CALL cp_fm_struct_create(fm_struct, ncol_global=new_size, template_fmstruct=fm_struct_template, force_block=.TRUE.)
    1290              : 
    1291           80 :       CALL cp_fm_release(matrix)
    1292              : 
    1293           80 :       CALL cp_fm_create(matrix, fm_struct)
    1294           80 :       CALL cp_fm_set_all(matrix, 0.0_dp)
    1295              : 
    1296           80 :       CALL cp_fm_struct_release(fm_struct)
    1297              : 
    1298           80 :       CALL timestop(handle)
    1299              : 
    1300           80 :    END SUBROUTINE reset_size_matrix
    1301              : 
    1302              : ! **************************************************************************************************
    1303              : !> \brief ...
    1304              : !> \param fm_matrix_L ...
    1305              : !> \param dimen_RI ...
    1306              : !> \param do_inversion ...
    1307              : ! **************************************************************************************************
    1308         1660 :    SUBROUTINE cholesky_decomp(fm_matrix_L, dimen_RI, do_inversion)
    1309              : 
    1310              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_matrix_L
    1311              :       INTEGER, INTENT(IN)                                :: dimen_RI
    1312              :       LOGICAL, INTENT(IN)                                :: do_inversion
    1313              : 
    1314              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'cholesky_decomp'
    1315              : 
    1316              :       INTEGER                                            :: handle, i_global, iiB, info_chol, &
    1317              :                                                             j_global, jjB, ncol_local, nrow_local
    1318          830 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1319              : 
    1320          830 :       CALL timeset(routineN, handle)
    1321              : 
    1322              :       ! do cholesky decomposition
    1323          830 :       CALL cp_fm_cholesky_decompose(matrix=fm_matrix_L, n=dimen_RI, info_out=info_chol)
    1324          830 :       CPASSERT(info_chol == 0)
    1325              : 
    1326          830 :       IF (.NOT. do_inversion) THEN
    1327              :          ! clean the lower part of the L^{-1} matrix (just to not have surprises afterwards)
    1328              :          CALL cp_fm_get_info(matrix=fm_matrix_L, &
    1329              :                              nrow_local=nrow_local, &
    1330              :                              ncol_local=ncol_local, &
    1331              :                              row_indices=row_indices, &
    1332          124 :                              col_indices=col_indices)
    1333        10752 :          DO jjB = 1, ncol_local
    1334        10628 :             j_global = col_indices(jjB)
    1335       567250 :             DO iiB = 1, nrow_local
    1336       556498 :                i_global = row_indices(iiB)
    1337       567126 :                IF (j_global < i_global) fm_matrix_L%local_data(iiB, jjB) = 0.0_dp
    1338              :             END DO
    1339              :          END DO
    1340              : 
    1341              :       END IF
    1342              : 
    1343          830 :       CALL timestop(handle)
    1344              : 
    1345          830 :    END SUBROUTINE cholesky_decomp
    1346              : 
    1347              : ! **************************************************************************************************
    1348              : !> \brief ...
    1349              : !> \param cfm_matrix_M_kpoints ...
    1350              : !> \param cfm_matrix_L_kpoints ...
    1351              : !> \param cfm_matrix_Minv_L_kpoints ...
    1352              : !> \param dimen_RI ...
    1353              : !> \param kpoints ...
    1354              : !> \param eps_eigval_S ...
    1355              : ! **************************************************************************************************
    1356           22 :    SUBROUTINE inversion_of_M_and_mult_with_chol_dec_of_V(cfm_matrix_M_kpoints, cfm_matrix_L_kpoints, &
    1357              :                                                          cfm_matrix_Minv_L_kpoints, dimen_RI, kpoints, eps_eigval_S)
    1358              : 
    1359              :       TYPE(cp_cfm_type), DIMENSION(:), INTENT(IN)        :: cfm_matrix_M_kpoints
    1360              :       TYPE(cp_cfm_type), DIMENSION(:), INTENT(INOUT)     :: cfm_matrix_L_kpoints
    1361              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:), &
    1362              :          INTENT(INOUT)                                   :: cfm_matrix_Minv_L_kpoints
    1363              :       INTEGER, INTENT(IN)                                :: dimen_RI
    1364              :       TYPE(kpoint_type), POINTER                         :: kpoints
    1365              :       REAL(KIND=dp), INTENT(IN)                          :: eps_eigval_S
    1366              : 
    1367              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'inversion_of_M_and_mult_with_chol_dec_of_V'
    1368              : 
    1369              :       INTEGER                                            :: handle, ikp, nkp
    1370              :       TYPE(cp_cfm_type)                                  :: cfm_matrix_M_tmp
    1371              :       TYPE(cp_fm_struct_type), POINTER                   :: matrix_struct
    1372              : 
    1373           22 :       CALL timeset(routineN, handle)
    1374              : 
    1375           22 :       matrix_struct => cfm_matrix_M_kpoints(1)%matrix_struct
    1376              : 
    1377           22 :       CALL cp_cfm_create(cfm_matrix_M_tmp, matrix_struct)
    1378              : 
    1379           22 :       CALL get_kpoint_info(kpoints, nkp=nkp)
    1380          494 :       ALLOCATE (cfm_matrix_Minv_L_kpoints(nkp))
    1381              : 
    1382          450 :       DO ikp = 1, nkp
    1383              : 
    1384          428 :          CALL cp_cfm_to_cfm(cfm_matrix_M_kpoints(ikp), cfm_matrix_M_tmp)
    1385              : 
    1386          428 :          CALL cp_cfm_power(cfm_matrix_M_tmp, threshold=eps_eigval_S, exponent=-1.0_dp)
    1387              : 
    1388              :          ! save L(k) = SQRT(V(k))
    1389          428 :          CALL cp_cfm_power(cfm_matrix_L_kpoints(ikp), threshold=0.0_dp, exponent=0.5_dp)
    1390              : 
    1391              :          ! get K = M^(-1)*L, where L is the Cholesky decomposition of V = L^T*L
    1392          428 :          CALL cp_cfm_create(cfm_matrix_Minv_L_kpoints(ikp), matrix_struct)
    1393              :          CALL parallel_gemm("N", "C", dimen_RI, dimen_RI, dimen_RI, z_one, cfm_matrix_M_tmp, &
    1394              :                             cfm_matrix_L_kpoints(ikp), &
    1395          450 :                             z_zero, cfm_matrix_Minv_L_kpoints(ikp))
    1396              : 
    1397              :       END DO
    1398              : 
    1399           22 :       CALL cp_cfm_release(cfm_matrix_M_tmp)
    1400              : 
    1401           22 :       CALL timestop(handle)
    1402              : 
    1403           22 :    END SUBROUTINE inversion_of_M_and_mult_with_chol_dec_of_V
    1404              : 
    1405              : ! **************************************************************************************************
    1406              : !> \brief ...
    1407              : !> \param fm_matrix_Minv_Vtrunc_Minv ...
    1408              : !> \param fm_matrix_Minv ...
    1409              : !> \param qs_env ...
    1410              : ! **************************************************************************************************
    1411           88 :    SUBROUTINE Gamma_only_inversion_of_M_and_mult_with_chol_dec_of_Vtrunc(fm_matrix_Minv_Vtrunc_Minv, &
    1412           22 :                                                                          fm_matrix_Minv, qs_env)
    1413              : 
    1414              :       TYPE(cp_fm_type), DIMENSION(:, :)                  :: fm_matrix_Minv_Vtrunc_Minv, &
    1415              :                                                             fm_matrix_Minv
    1416              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1417              : 
    1418              :       CHARACTER(LEN=*), PARAMETER :: &
    1419              :          routineN = 'Gamma_only_inversion_of_M_and_mult_with_chol_dec_of_Vtrunc'
    1420              :       REAL(KIND=dp), PARAMETER :: eps_eigval_S_Gamma_thre = 1.0E-18_dp
    1421              : 
    1422              :       INTEGER                                            :: dimen_RI, handle, ndep
    1423              :       REAL(KIND=dp)                                      :: eps_eigval_S_Gamma
    1424              :       TYPE(cp_fm_type)                                   :: fm_matrix_RI_metric_inv_work, fm_work
    1425              : 
    1426           22 :       CALL timeset(routineN, handle)
    1427              : 
    1428           22 :       CALL cp_fm_create(fm_work, fm_matrix_Minv(1, 1)%matrix_struct)
    1429           22 :       CALL cp_fm_set_all(fm_work, 0.0_dp)
    1430              : 
    1431           22 :       CALL cp_fm_create(fm_matrix_RI_metric_inv_work, fm_matrix_Minv(1, 1)%matrix_struct)
    1432           22 :       CALL cp_fm_set_all(fm_matrix_RI_metric_inv_work, 0.0_dp)
    1433              : 
    1434           22 :       CALL cp_fm_get_info(matrix=fm_matrix_Minv(1, 1), nrow_global=dimen_RI)
    1435              : 
    1436           22 :       eps_eigval_S_Gamma = qs_env%mp2_env%ri_rpa_im_time%eps_eigval_S_Gamma
    1437              : 
    1438           22 :       IF (eps_eigval_S_Gamma > eps_eigval_S_Gamma_thre) THEN
    1439              : 
    1440              :          ! remove small eigenvalues
    1441              :          CALL cp_fm_power(fm_matrix_Minv(1, 1), fm_matrix_RI_metric_inv_work, -0.5_dp, &
    1442            0 :                           eps_eigval_S_Gamma, ndep)
    1443              : 
    1444              :       ELSE
    1445              : 
    1446           22 :          CALL cholesky_decomp(fm_matrix_Minv(1, 1), dimen_RI, do_inversion=.TRUE.)
    1447              : 
    1448           22 :          CALL invert_mat(fm_matrix_Minv(1, 1))
    1449              : 
    1450              :       END IF
    1451              : 
    1452              :       CALL parallel_gemm('N', 'T', dimen_RI, dimen_RI, dimen_RI, 1.0_dp, fm_matrix_Minv(1, 1), &
    1453           22 :                          fm_matrix_Minv(1, 1), 0.0_dp, fm_matrix_RI_metric_inv_work)
    1454              : 
    1455           22 :       CALL cp_fm_to_fm(fm_matrix_RI_metric_inv_work, fm_matrix_Minv(1, 1))
    1456              : 
    1457              :       CALL parallel_gemm('N', 'N', dimen_RI, dimen_RI, dimen_RI, 1.0_dp, fm_matrix_RI_metric_inv_work, &
    1458           22 :                          fm_matrix_Minv_Vtrunc_Minv(1, 1), 0.0_dp, fm_work)
    1459              : 
    1460              :       CALL parallel_gemm('N', 'N', dimen_RI, dimen_RI, dimen_RI, 1.0_dp, fm_work, &
    1461           22 :                          fm_matrix_RI_metric_inv_work, 0.0_dp, fm_matrix_Minv_Vtrunc_Minv(1, 1))
    1462              : 
    1463           22 :       CALL cp_fm_release(fm_work)
    1464           22 :       CALL cp_fm_release(fm_matrix_RI_metric_inv_work)
    1465              : 
    1466           22 :       CALL timestop(handle)
    1467              : 
    1468           22 :    END SUBROUTINE Gamma_only_inversion_of_M_and_mult_with_chol_dec_of_Vtrunc
    1469              : 
    1470              : ! **************************************************************************************************
    1471              : !> \brief ...
    1472              : !> \param qs_env ...
    1473              : !> \param trunc_coulomb ...
    1474              : !> \param rel_cutoff_trunc_coulomb_ri_x ...
    1475              : !> \param cell_grid ...
    1476              : !> \param do_BvK_cell ...
    1477              : ! **************************************************************************************************
    1478           70 :    SUBROUTINE trunc_coulomb_for_exchange(qs_env, trunc_coulomb, rel_cutoff_trunc_coulomb_ri_x, &
    1479              :                                          cell_grid, do_BvK_cell)
    1480              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1481              :       TYPE(coulomb_operator_type), OPTIONAL              :: trunc_coulomb
    1482              :       REAL(KIND=dp), OPTIONAL                            :: rel_cutoff_trunc_coulomb_ri_x
    1483              :       INTEGER, DIMENSION(3), OPTIONAL                    :: cell_grid
    1484              :       LOGICAL, OPTIONAL                                  :: do_BvK_cell
    1485              : 
    1486              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'trunc_coulomb_for_exchange'
    1487              : 
    1488              :       INTEGER                                            :: handle, i_dim
    1489              :       INTEGER, DIMENSION(3)                              :: periodic
    1490              :       LOGICAL                                            :: my_do_BvK_cell
    1491              :       REAL(KIND=dp) :: kp_fac, kp_fac_idim, my_rel_cutoff_trunc_coulomb_ri_x, &
    1492              :          shortest_dist_cell_planes
    1493              :       TYPE(cell_type), POINTER                           :: cell
    1494              :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
    1495              : 
    1496           70 :       CALL timeset(routineN, handle)
    1497              : 
    1498           70 :       NULLIFY (cell)
    1499           70 :       CALL get_qs_env(qs_env, cell=cell, kpoints=kpoints_scf)
    1500           70 :       CALL get_cell(cell=cell, periodic=periodic)
    1501              : 
    1502           70 :       my_do_BvK_cell = .FALSE.
    1503           70 :       IF (PRESENT(do_BvK_cell)) my_do_BvK_cell = do_BvK_cell
    1504           48 :       IF (my_do_BvK_cell) THEN
    1505              :          kp_fac = 1.0E10_dp
    1506          120 :          DO i_dim = 1, 3
    1507              :             ! look for smallest k-point mesh in periodic direction
    1508          120 :             IF (periodic(i_dim) == 1) THEN
    1509           68 :                IF (PRESENT(cell_grid)) THEN
    1510           68 :                   kp_fac_idim = REAL(cell_grid(i_dim), KIND=dp)
    1511              :                ELSE
    1512            0 :                   kp_fac_idim = REAL(kpoints_scf%nkp_grid(i_dim), KIND=dp)
    1513              :                END IF
    1514              :                IF (kp_fac > kp_fac_idim) kp_fac = kp_fac_idim
    1515              :             END IF
    1516              :          END DO
    1517              :       ELSE
    1518              :          kp_fac = 1.0_dp
    1519              :       END IF
    1520              : 
    1521           70 :       shortest_dist_cell_planes = 1.0E4_dp
    1522           70 :       IF (periodic(1) == 1) THEN
    1523           28 :          IF (shortest_dist_cell_planes > plane_distance(1, 0, 0, cell)) THEN
    1524           28 :             shortest_dist_cell_planes = plane_distance(1, 0, 0, cell)
    1525              :          END IF
    1526              :       END IF
    1527           70 :       IF (periodic(2) == 1) THEN
    1528           60 :          IF (shortest_dist_cell_planes > plane_distance(0, 1, 0, cell)) THEN
    1529           36 :             shortest_dist_cell_planes = plane_distance(0, 1, 0, cell)
    1530              :          END IF
    1531              :       END IF
    1532           70 :       IF (periodic(3) == 1) THEN
    1533           56 :          IF (shortest_dist_cell_planes > plane_distance(0, 0, 1, cell)) THEN
    1534            8 :             shortest_dist_cell_planes = plane_distance(0, 0, 1, cell)
    1535              :          END IF
    1536              :       END IF
    1537              : 
    1538           70 :       IF (PRESENT(rel_cutoff_trunc_coulomb_ri_x)) THEN
    1539           48 :          my_rel_cutoff_trunc_coulomb_ri_x = rel_cutoff_trunc_coulomb_ri_x
    1540              :       ELSE
    1541           22 :          my_rel_cutoff_trunc_coulomb_ri_x = qs_env%mp2_env%ri_rpa_im_time%rel_cutoff_trunc_coulomb_ri_x
    1542              :       END IF
    1543              : 
    1544           70 :       IF (PRESENT(trunc_coulomb)) THEN
    1545           70 :          trunc_coulomb%potential_type = do_potential_truncated
    1546              :          trunc_coulomb%cutoff_radius = shortest_dist_cell_planes* &
    1547              :                                        my_rel_cutoff_trunc_coulomb_ri_x* &
    1548           70 :                                        kp_fac
    1549           70 :          trunc_coulomb%filename = "t_c_g.dat"
    1550              :          ! dummy
    1551           70 :          trunc_coulomb%omega = 0.0_dp
    1552              :       END IF
    1553              : 
    1554           70 :       CALL timestop(handle)
    1555              : 
    1556           70 :    END SUBROUTINE trunc_coulomb_for_exchange
    1557              : 
    1558              : ! **************************************************************************************************
    1559              : !> \brief ...
    1560              : !> \param fm_matrix_L ...
    1561              : ! **************************************************************************************************
    1562         1412 :    SUBROUTINE invert_mat(fm_matrix_L)
    1563              : 
    1564              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_matrix_L
    1565              : 
    1566              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'invert_mat'
    1567              : 
    1568              :       INTEGER                                            :: handle, i_global, iiB, j_global, jjB, &
    1569              :                                                             ncol_local, nrow_local
    1570          706 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    1571              : 
    1572          706 :       CALL timeset(routineN, handle)
    1573              : 
    1574          706 :       CALL cp_fm_triangular_invert(matrix_a=fm_matrix_L, uplo_tr='U')
    1575              : 
    1576              :       ! clear the lower part of the L^{-1} matrix (just to have no surprises afterwards)
    1577              :       CALL cp_fm_get_info(matrix=fm_matrix_L, &
    1578              :                           nrow_local=nrow_local, &
    1579              :                           ncol_local=ncol_local, &
    1580              :                           row_indices=row_indices, &
    1581          706 :                           col_indices=col_indices)
    1582              :       ! assume upper triangular format
    1583        54672 :       DO jjB = 1, ncol_local
    1584        53966 :          j_global = col_indices(jjB)
    1585      3990805 :          DO iiB = 1, nrow_local
    1586      3936133 :             i_global = row_indices(iiB)
    1587      3990099 :             IF (j_global < i_global) fm_matrix_L%local_data(iiB, jjB) = 0.0_dp
    1588              :          END DO
    1589              :       END DO
    1590              : 
    1591          706 :       CALL timestop(handle)
    1592              : 
    1593          706 :    END SUBROUTINE invert_mat
    1594              : 
    1595              : ! **************************************************************************************************
    1596              : !> \brief ...
    1597              : !> \param fm_matrix_L ...
    1598              : !> \param blacs_env_L ...
    1599              : !> \param dimen_RI ...
    1600              : !> \param para_env_L ...
    1601              : !> \param name ...
    1602              : !> \param fm_matrix_L_struct ...
    1603              : ! **************************************************************************************************
    1604          742 :    SUBROUTINE create_matrix_L(fm_matrix_L, blacs_env_L, dimen_RI, para_env_L, name, fm_matrix_L_struct)
    1605              :       TYPE(cp_fm_type), INTENT(OUT)                      :: fm_matrix_L
    1606              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env_L
    1607              :       INTEGER, INTENT(IN)                                :: dimen_RI
    1608              :       TYPE(mp_para_env_type), POINTER                    :: para_env_L
    1609              :       CHARACTER(LEN=*), INTENT(IN)                       :: name
    1610              :       TYPE(cp_fm_struct_type), OPTIONAL, POINTER         :: fm_matrix_L_struct
    1611              : 
    1612              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'create_matrix_L'
    1613              : 
    1614              :       INTEGER                                            :: handle
    1615              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1616              : 
    1617          742 :       CALL timeset(routineN, handle)
    1618              : 
    1619              :       ! create the full matrix L defined in the L group
    1620          742 :       IF (PRESENT(fm_matrix_L_struct)) THEN
    1621           36 :          CALL cp_fm_create(fm_matrix_L, fm_matrix_L_struct, name=name)
    1622              :       ELSE
    1623          706 :          NULLIFY (fm_struct)
    1624              :          CALL cp_fm_struct_create(fm_struct, context=blacs_env_L, nrow_global=dimen_RI, &
    1625          706 :                                   ncol_global=dimen_RI, para_env=para_env_L)
    1626              : 
    1627          706 :          CALL cp_fm_create(fm_matrix_L, fm_struct, name=name)
    1628              : 
    1629          706 :          CALL cp_fm_struct_release(fm_struct)
    1630              :       END IF
    1631              : 
    1632          742 :       CALL cp_fm_set_all(matrix=fm_matrix_L, alpha=0.0_dp)
    1633              : 
    1634          742 :       CALL timestop(handle)
    1635              : 
    1636          742 :    END SUBROUTINE create_matrix_L
    1637              : 
    1638              : ! **************************************************************************************************
    1639              : !> \brief ...
    1640              : !> \param fm_matrix_L ...
    1641              : !> \param my_group_L_start ...
    1642              : !> \param my_group_L_end ...
    1643              : !> \param my_group_L_size ...
    1644              : !> \param my_Lrows ...
    1645              : ! **************************************************************************************************
    1646          574 :    SUBROUTINE grep_Lcols(fm_matrix_L, &
    1647              :                          my_group_L_start, my_group_L_end, my_group_L_size, my_Lrows)
    1648              :       TYPE(cp_fm_type), INTENT(IN)                       :: fm_matrix_L
    1649              :       INTEGER, INTENT(IN)                                :: my_group_L_start, my_group_L_end, &
    1650              :                                                             my_group_L_size
    1651              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
    1652              :          INTENT(OUT)                                     :: my_Lrows
    1653              : 
    1654              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'grep_Lcols'
    1655              : 
    1656              :       INTEGER :: dimen_RI, handle, handle2, i_global, iiB, j_global, jjB, max_row_col_local, &
    1657              :          ncol_local, ncol_rec, nrow_local, nrow_rec, proc_receive_static, proc_send_static, &
    1658              :          proc_shift
    1659          574 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: local_col_row_info, rec_col_row_info
    1660          574 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, col_indices_rec, &
    1661          574 :                                                             row_indices, row_indices_rec
    1662          574 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: local_L, rec_L
    1663              :       REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :), &
    1664          574 :          POINTER                                         :: local_L_internal
    1665              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1666              : 
    1667          574 :       CALL timeset(routineN, handle)
    1668              : 
    1669              :       CALL cp_fm_get_info(matrix=fm_matrix_L, &
    1670              :                           nrow_local=nrow_local, &
    1671              :                           ncol_local=ncol_local, &
    1672              :                           row_indices=row_indices, &
    1673              :                           col_indices=col_indices, &
    1674              :                           nrow_global=dimen_RI, &
    1675              :                           local_data=local_L_internal, &
    1676          574 :                           para_env=para_env)
    1677              : 
    1678         2296 :       ALLOCATE (my_Lrows(dimen_RI, my_group_L_size))
    1679          574 :       my_Lrows = 0.0_dp
    1680              : 
    1681         2296 :       ALLOCATE (local_L(nrow_local, ncol_local))
    1682      3490162 :       local_L(:, :) = local_L_internal(1:nrow_local, 1:ncol_local)
    1683              : 
    1684          574 :       max_row_col_local = MAX(nrow_local, ncol_local)
    1685          574 :       CALL para_env%max(max_row_col_local)
    1686              : 
    1687         2296 :       ALLOCATE (local_col_row_info(0:max_row_col_local, 2))
    1688          574 :       local_col_row_info = 0
    1689              :       ! 0,1 nrows
    1690          574 :       local_col_row_info(0, 1) = nrow_local
    1691        42395 :       local_col_row_info(1:nrow_local, 1) = row_indices(1:nrow_local)
    1692              :       ! 0,2 ncols
    1693          574 :       local_col_row_info(0, 2) = ncol_local
    1694        43288 :       local_col_row_info(1:ncol_local, 2) = col_indices(1:ncol_local)
    1695              : 
    1696         1148 :       ALLOCATE (rec_col_row_info(0:max_row_col_local, 2))
    1697              : 
    1698              :       ! accumulate data on my_Lrows starting from myself
    1699        43288 :       DO jjB = 1, ncol_local
    1700        42714 :          j_global = col_indices(jjB)
    1701        43288 :          IF (j_global >= my_group_L_start .AND. j_global <= my_group_L_end) THEN
    1702      1790308 :             DO iiB = 1, nrow_local
    1703      1767931 :                i_global = row_indices(iiB)
    1704      1790308 :                my_Lrows(i_global, j_global - my_group_L_start + 1) = local_L(iiB, jjB)
    1705              :             END DO
    1706              :          END IF
    1707              :       END DO
    1708              : 
    1709          574 :       proc_send_static = MODULO(para_env%mepos + 1, para_env%num_pe)
    1710          574 :       proc_receive_static = MODULO(para_env%mepos - 1, para_env%num_pe)
    1711              : 
    1712          574 :       CALL timeset(routineN//"_comm", handle2)
    1713              : 
    1714          598 :       DO proc_shift = 1, para_env%num_pe - 1
    1715              :          ! first exchange information on the local data
    1716           24 :          rec_col_row_info = 0
    1717           24 :          CALL para_env%sendrecv(local_col_row_info, proc_send_static, rec_col_row_info, proc_receive_static)
    1718           24 :          nrow_rec = rec_col_row_info(0, 1)
    1719           24 :          ncol_rec = rec_col_row_info(0, 2)
    1720              : 
    1721           72 :          ALLOCATE (row_indices_rec(nrow_rec))
    1722         1061 :          row_indices_rec = rec_col_row_info(1:nrow_rec, 1)
    1723              : 
    1724           72 :          ALLOCATE (col_indices_rec(ncol_rec))
    1725         2064 :          col_indices_rec = rec_col_row_info(1:ncol_rec, 2)
    1726              : 
    1727           96 :          ALLOCATE (rec_L(nrow_rec, ncol_rec))
    1728           24 :          rec_L = 0.0_dp
    1729              : 
    1730              :          ! then send and receive the real data
    1731           24 :          CALL para_env%sendrecv(local_L, proc_send_static, rec_L, proc_receive_static)
    1732              : 
    1733              :          ! accumulate the received data on my_Lrows
    1734         2064 :          DO jjB = 1, ncol_rec
    1735         2040 :             j_global = col_indices_rec(jjB)
    1736         2064 :             IF (j_global >= my_group_L_start .AND. j_global <= my_group_L_end) THEN
    1737        91028 :                DO iiB = 1, nrow_rec
    1738        88988 :                   i_global = row_indices_rec(iiB)
    1739        91028 :                   my_Lrows(i_global, j_global - my_group_L_start + 1) = rec_L(iiB, jjB)
    1740              :                END DO
    1741              :             END IF
    1742              :          END DO
    1743              : 
    1744         4200 :          local_col_row_info(:, :) = rec_col_row_info
    1745           24 :          CALL MOVE_ALLOC(rec_L, local_L)
    1746              : 
    1747           24 :          DEALLOCATE (col_indices_rec)
    1748          598 :          DEALLOCATE (row_indices_rec)
    1749              :       END DO
    1750          574 :       CALL timestop(handle2)
    1751              : 
    1752          574 :       DEALLOCATE (local_col_row_info)
    1753          574 :       DEALLOCATE (rec_col_row_info)
    1754          574 :       DEALLOCATE (local_L)
    1755              : 
    1756          574 :       CALL timestop(handle)
    1757              : 
    1758         1722 :    END SUBROUTINE grep_Lcols
    1759              : 
    1760              : END MODULE mp2_ri_2c
        

Generated by: LCOV version 2.0-1