LCOV - code coverage report
Current view: top level - src - mp2_ri_libint.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 97.7 % 436 426
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 7 7

            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 Routines to calculate the 3 and 2 center ERI's needed in the RI
      10              : !>        approximation using libint
      11              : !> \par History
      12              : !>      08.2013 created [Mauro Del Ben]
      13              : !> \author Mauro Del Ben
      14              : ! **************************************************************************************************
      15              : MODULE mp2_ri_libint
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      17              :                                               get_atomic_kind_set
      18              :    USE basis_set_types,                 ONLY: get_gto_basis_set,&
      19              :                                               gto_basis_set_type,&
      20              :                                               init_aux_basis_set
      21              :    USE cell_types,                      ONLY: cell_type
      22              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      23              :                                               cp_blacs_env_release,&
      24              :                                               cp_blacs_env_type
      25              :    USE cp_control_types,                ONLY: dft_control_type
      26              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_triangular_invert
      27              :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose
      28              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      29              :                                               cp_fm_struct_release,&
      30              :                                               cp_fm_struct_type
      31              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      32              :                                               cp_fm_get_submatrix,&
      33              :                                               cp_fm_release,&
      34              :                                               cp_fm_set_submatrix,&
      35              :                                               cp_fm_type
      36              :    USE four_center_integral_methods,    ONLY: coulomb4
      37              :    USE four_center_integral_preparation,ONLY: prepare_eri4_contraction
      38              :    USE hfx_pair_list_methods,           ONLY: build_pair_list_mp2
      39              :    USE hfx_screening_methods,           ONLY: calc_pair_dist_radii,&
      40              :                                               calc_screening_functions
      41              :    USE hfx_types,                       ONLY: &
      42              :         hfx_basis_info_type, hfx_basis_type, hfx_create_neighbor_cells, hfx_pgf_list, &
      43              :         hfx_potential_type, hfx_release_basis_types, hfx_screen_coeff_type, hfx_type, log_zero, &
      44              :         pair_set_list_type
      45              :    USE input_constants,                 ONLY: do_potential_long
      46              :    USE kinds,                           ONLY: dp,&
      47              :                                               int_8
      48              :    USE message_passing,                 ONLY: mp_para_env_type
      49              :    USE mp2_types,                       ONLY: mp2_biel_type,&
      50              :                                               mp2_type,&
      51              :                                               pair_list_type_mp2
      52              :    USE orbital_pointers,                ONLY: ncoset
      53              :    USE particle_types,                  ONLY: particle_type
      54              :    USE qs_environment_types,            ONLY: get_qs_env,&
      55              :                                               qs_environment_type
      56              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      57              :                                               get_qs_kind_set,&
      58              :                                               qs_kind_type
      59              :    USE r12_fundamentals,                ONLY: r12_fundamentals_init
      60              : #include "./base/base_uses.f90"
      61              : 
      62              :    IMPLICIT NONE
      63              :    PRIVATE
      64              : 
      65              :    PUBLIC ::  libint_ri_mp2, read_RI_basis_set, release_RI_basis_set, prepare_integral_calc
      66              : 
      67              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mp2_ri_libint'
      68              : 
      69              : !***
      70              : 
      71              : CONTAINS
      72              : 
      73              : ! **************************************************************************************************
      74              : !> \brief ...
      75              : !> \param dimen ...
      76              : !> \param RI_dimen ...
      77              : !> \param occupied ...
      78              : !> \param natom ...
      79              : !> \param mp2_biel ...
      80              : !> \param mp2_env ...
      81              : !> \param C ...
      82              : !> \param kind_of ...
      83              : !> \param RI_basis_parameter ...
      84              : !> \param RI_basis_info ...
      85              : !> \param basis_S0 ...
      86              : !> \param RI_index_table ...
      87              : !> \param qs_env ...
      88              : !> \param para_env ...
      89              : !> \param Lai ...
      90              : ! **************************************************************************************************
      91         5816 :    SUBROUTINE libint_ri_mp2(dimen, RI_dimen, occupied, natom, mp2_biel, mp2_env, C, &
      92         2908 :                             kind_of, &
      93              :                             RI_basis_parameter, RI_basis_info, basis_S0, RI_index_table, &
      94              :                             qs_env, para_env, &
      95              :                             Lai)
      96              :       INTEGER                                            :: dimen, RI_dimen, occupied, natom
      97              :       TYPE(mp2_biel_type)                                :: mp2_biel
      98              :       TYPE(mp2_type)                                     :: mp2_env
      99              :       REAL(KIND=dp), DIMENSION(dimen, dimen)             :: C
     100              :       INTEGER, DIMENSION(:)                              :: kind_of
     101              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: RI_basis_parameter
     102              :       TYPE(hfx_basis_info_type)                          :: RI_basis_info
     103              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_S0
     104              :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: RI_index_table
     105              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     106              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     107              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: Lai
     108              : 
     109              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'libint_ri_mp2'
     110              : 
     111              :       INTEGER                                            :: handle, nkind
     112              : 
     113         2908 :       CALL timeset(routineN, handle)
     114              : 
     115              :       ! Get the RI basis set and store in to a nice form
     116         2908 :       IF (.NOT. (ASSOCIATED(RI_basis_parameter))) THEN
     117            0 :          IF (ALLOCATED(RI_index_table)) DEALLOCATE (RI_index_table)
     118            0 :          IF (ASSOCIATED(basis_S0)) CALL release_RI_basis_set(RI_basis_parameter, basis_S0)
     119              :          CALL read_RI_basis_set(qs_env, RI_basis_parameter, RI_basis_info, &
     120              :                                 natom, nkind, kind_of, RI_index_table, RI_dimen, &
     121            0 :                                 basis_S0)
     122              :       END IF
     123              : 
     124              :       CALL calc_lai_libint(mp2_env, qs_env, para_env, &
     125              :                            mp2_biel, dimen, C, occupied, &
     126              :                            RI_basis_parameter, RI_basis_info, RI_index_table, RI_dimen, basis_S0, &
     127         2908 :                            Lai)
     128              : 
     129         2908 :       CALL timestop(handle)
     130              : 
     131         2908 :    END SUBROUTINE libint_ri_mp2
     132              : 
     133              : ! **************************************************************************************************
     134              : !> \brief Read the auxiliary basis set for RI approxiamtion
     135              : !> \param qs_env ...
     136              : !> \param RI_basis_parameter ...
     137              : !> \param RI_basis_info ...
     138              : !> \param natom ...
     139              : !> \param nkind ...
     140              : !> \param kind_of ...
     141              : !> \param RI_index_table ...
     142              : !> \param RI_dimen ...
     143              : !> \param basis_S0 ...
     144              : !> \par History
     145              : !>      08.2013 created [Mauro Del Ben]
     146              : !> \author Mauro Del Ben
     147              : ! **************************************************************************************************
     148            6 :    SUBROUTINE read_RI_basis_set(qs_env, RI_basis_parameter, RI_basis_info, &
     149            6 :                                 natom, nkind, kind_of, RI_index_table, RI_dimen, &
     150              :                                 basis_S0)
     151              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     152              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: RI_basis_parameter
     153              :       TYPE(hfx_basis_info_type)                          :: RI_basis_info
     154              :       INTEGER                                            :: natom, nkind
     155              :       INTEGER, DIMENSION(:)                              :: kind_of
     156              :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: RI_index_table
     157              :       INTEGER                                            :: RI_dimen
     158              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_S0
     159              : 
     160              :       INTEGER                                            :: counter, iatom, ikind, iset, max_set, &
     161              :                                                             nset
     162              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_a
     163            6 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     164              :       TYPE(qs_kind_type), POINTER                        :: atom_kind
     165              : 
     166            6 :       NULLIFY (RI_basis_parameter)
     167              : 
     168            6 :       NULLIFY (qs_kind_set)
     169            6 :       CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set)
     170              : 
     171            6 :       nkind = SIZE(qs_kind_set, 1)
     172           24 :       ALLOCATE (RI_basis_parameter(nkind))
     173           24 :       ALLOCATE (basis_S0(nkind))
     174            6 :       max_set = 0
     175           12 :       DO ikind = 1, nkind
     176            6 :          NULLIFY (atom_kind)
     177            6 :          atom_kind => qs_kind_set(ikind)
     178              :          ! here we reset the initial RI basis such that we can
     179              :          ! work with non-normalized auxiliary basis functions
     180              :          CALL get_qs_kind(qs_kind=atom_kind, &
     181            6 :                           basis_set=orb_basis_a, basis_type="RI_AUX")
     182            6 :          IF (.NOT. (ASSOCIATED(orb_basis_a))) THEN
     183            0 :             CPABORT("Initial RI auxiliary basis not specified.")
     184              :          END IF
     185          264 :          orb_basis_a%gcc = 1.0_dp
     186            6 :          orb_basis_a%norm_type = 1
     187            6 :          CALL init_aux_basis_set(orb_basis_a)
     188              : 
     189              :          CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
     190              :                               maxsgf=RI_basis_info%max_sgf, &
     191              :                               maxnset=RI_basis_info%max_set, &
     192              :                               maxlgto=RI_basis_info%max_am, &
     193            6 :                               basis_type="RI_AUX")
     194              :          CALL get_gto_basis_set(gto_basis_set=orb_basis_a, &
     195              :                                 lmax=RI_basis_parameter(ikind)%lmax, &
     196              :                                 lmin=RI_basis_parameter(ikind)%lmin, &
     197              :                                 npgf=RI_basis_parameter(ikind)%npgf, &
     198              :                                 nset=RI_basis_parameter(ikind)%nset, &
     199              :                                 zet=RI_basis_parameter(ikind)%zet, &
     200              :                                 nsgf_set=RI_basis_parameter(ikind)%nsgf, &
     201              :                                 first_sgf=RI_basis_parameter(ikind)%first_sgf, &
     202              :                                 sphi=RI_basis_parameter(ikind)%sphi, &
     203              :                                 nsgf=RI_basis_parameter(ikind)%nsgf_total, &
     204              :                                 l=RI_basis_parameter(ikind)%nl, &
     205              :                                 nshell=RI_basis_parameter(ikind)%nshell, &
     206              :                                 set_radius=RI_basis_parameter(ikind)%set_radius, &
     207              :                                 pgf_radius=RI_basis_parameter(ikind)%pgf_radius, &
     208            6 :                                 kind_radius=RI_basis_parameter(ikind)%kind_radius)
     209            6 :          CALL prepare_eri4_contraction(orb_basis_a, RI_basis_parameter(ikind)%contraction)
     210              : 
     211            6 :          max_set = MAX(max_set, RI_basis_parameter(ikind)%nset)
     212              : 
     213            6 :          basis_S0(ikind)%kind_radius = RI_basis_parameter(ikind)%kind_radius
     214            6 :          basis_S0(ikind)%nset = 1
     215            6 :          basis_S0(ikind)%nsgf_total = 1
     216            6 :          ALLOCATE (basis_S0(ikind)%set_radius(1))
     217            6 :          basis_S0(ikind)%set_radius(1) = RI_basis_parameter(ikind)%kind_radius
     218            6 :          ALLOCATE (basis_S0(ikind)%lmax(1))
     219            6 :          basis_S0(ikind)%lmax(1) = 0
     220            6 :          ALLOCATE (basis_S0(ikind)%lmin(1))
     221            6 :          basis_S0(ikind)%lmin(1) = 0
     222            6 :          ALLOCATE (basis_S0(ikind)%npgf(1))
     223            6 :          basis_S0(ikind)%npgf(1) = 1
     224            6 :          ALLOCATE (basis_S0(ikind)%nsgf(1))
     225            6 :          basis_S0(ikind)%nsgf(1) = 1
     226            6 :          ALLOCATE (basis_S0(ikind)%nshell(1))
     227            6 :          basis_S0(ikind)%nshell(1) = 1
     228            6 :          ALLOCATE (basis_S0(ikind)%pgf_radius(1, 1))
     229            6 :          basis_S0(ikind)%pgf_radius(1, 1) = RI_basis_parameter(ikind)%kind_radius
     230            6 :          ALLOCATE (basis_S0(ikind)%sphi(1, 1))
     231            6 :          basis_S0(ikind)%sphi(1, 1) = 1.0_dp
     232            6 :          ALLOCATE (basis_S0(ikind)%zet(1, 1))
     233            6 :          basis_S0(ikind)%zet(1, 1) = 0.0_dp
     234            6 :          ALLOCATE (basis_S0(ikind)%first_sgf(1, 1))
     235            6 :          basis_S0(ikind)%first_sgf(1, 1) = 1
     236            6 :          ALLOCATE (basis_S0(ikind)%nl(1, 1))
     237            6 :          basis_S0(ikind)%nl(1, 1) = 0
     238              : 
     239           18 :          ALLOCATE (basis_S0(ikind)%contraction(0:0, 1))
     240           18 :          ALLOCATE (basis_S0(ikind)%contraction(0, 1)%angular(1, 1), source=1.0_dp)
     241           24 :          ALLOCATE (basis_S0(ikind)%contraction(0, 1)%weights(1, 1), source=1.0_dp)
     242              : 
     243              :       END DO
     244            6 :       RI_basis_info%max_set = max_set
     245              : 
     246           24 :       ALLOCATE (RI_index_table(natom, max_set))
     247          178 :       RI_index_table = -HUGE(0)
     248            6 :       counter = 0
     249            6 :       RI_dimen = 0
     250           12 :       DO iatom = 1, natom
     251            6 :          ikind = kind_of(iatom)
     252            6 :          nset = RI_basis_parameter(ikind)%nset
     253           98 :          DO iset = 1, nset
     254           86 :             RI_index_table(iatom, iset) = counter + 1
     255           86 :             counter = counter + RI_basis_parameter(ikind)%nsgf(iset)
     256           92 :             RI_dimen = RI_dimen + RI_basis_parameter(ikind)%nsgf(iset)
     257              :          END DO
     258              :       END DO
     259              : 
     260            6 :    END SUBROUTINE read_RI_basis_set
     261              : 
     262              : ! **************************************************************************************************
     263              : !> \brief Release the auxiliary basis set for RI approxiamtion (to be used
     264              : !>        only in the case of basis optimization)
     265              : !> \param RI_basis_parameter ...
     266              : !> \param basis_S0 ...
     267              : !> \par History
     268              : !>      08.2013 created [Mauro Del Ben]
     269              : !> \author Mauro Del Ben
     270              : ! **************************************************************************************************
     271            6 :    SUBROUTINE release_RI_basis_set(RI_basis_parameter, basis_S0)
     272              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: RI_basis_parameter, basis_S0
     273              : 
     274              :       INTEGER                                            :: i
     275              : 
     276            6 :       IF (ASSOCIATED(RI_basis_parameter)) CALL hfx_release_basis_types(RI_basis_parameter)
     277              : 
     278              :       ! S0 basis
     279           12 :       DO i = 1, SIZE(basis_S0)
     280            6 :          DEALLOCATE (basis_S0(i)%set_radius)
     281            6 :          DEALLOCATE (basis_S0(i)%lmax)
     282            6 :          DEALLOCATE (basis_S0(i)%lmin)
     283            6 :          DEALLOCATE (basis_S0(i)%npgf)
     284            6 :          DEALLOCATE (basis_S0(i)%nsgf)
     285            6 :          DEALLOCATE (basis_S0(i)%nshell)
     286            6 :          DEALLOCATE (basis_S0(i)%pgf_radius)
     287            6 :          DEALLOCATE (basis_S0(i)%sphi)
     288            6 :          DEALLOCATE (basis_S0(i)%zet)
     289            6 :          DEALLOCATE (basis_S0(i)%first_sgf)
     290           12 :          DEALLOCATE (basis_S0(i)%nl)
     291              :       END DO
     292            6 :       CALL hfx_release_basis_types(basis_S0)
     293              : 
     294            6 :    END SUBROUTINE release_RI_basis_set
     295              : 
     296              : ! **************************************************************************************************
     297              : !> \brief ...
     298              : !> \param mp2_env ...
     299              : !> \param qs_env   ...
     300              : !> \param para_env ...
     301              : !> \param mp2_biel ...
     302              : !> \param dimen ...
     303              : !> \param C ...
     304              : !> \param occupied ...
     305              : !> \param RI_basis_parameter ...
     306              : !> \param RI_basis_info ...
     307              : !> \param RI_index_table ...
     308              : !> \param RI_dimen ...
     309              : !> \param basis_S0 ...
     310              : !> \param Lai ...
     311              : !> \par History
     312              : !>      08.2013 created [Mauro Del Ben]
     313              : !> \author Mauro Del Ben
     314              : ! **************************************************************************************************
     315         2908 :    SUBROUTINE calc_lai_libint(mp2_env, qs_env, para_env, &
     316         2908 :                               mp2_biel, dimen, C, occupied, &
     317              :                               RI_basis_parameter, RI_basis_info, RI_index_table, RI_dimen, basis_S0, &
     318              :                               Lai)
     319              : 
     320              :       TYPE(mp2_type)                                     :: mp2_env
     321              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     322              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     323              :       TYPE(mp2_biel_type)                                :: mp2_biel
     324              :       INTEGER                                            :: dimen
     325              :       REAL(KIND=dp), DIMENSION(dimen, dimen)             :: C
     326              :       INTEGER                                            :: occupied
     327              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: RI_basis_parameter
     328              :       TYPE(hfx_basis_info_type)                          :: RI_basis_info
     329              :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: RI_index_table
     330              :       INTEGER                                            :: RI_dimen
     331              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_S0
     332              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: Lai
     333              : 
     334              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'calc_lai_libint'
     335              : 
     336              :       INTEGER :: counter_L_blocks, handle, i, i_list_kl, i_set_list_kl, i_set_list_kl_start, &
     337              :          i_set_list_kl_stop, iatom, iatom_end, iatom_start, iiB, ikind, info_chol, iset, jatom, &
     338              :          jatom_end, jatom_start, jjB, jkind, jset, katom, katom_end, katom_start, kkB, kkind, &
     339              :          kset, kset_start, L_B_i_end, L_B_i_start, L_B_k_end, L_B_k_start, latom, latom_end, &
     340              :          latom_start, lkind, llB, lset, max_pgf, max_set, natom, ncob, nints, nseta, nsetb, nsetc, &
     341              :          nsetd, nsgf_max, nspins, orb_k_end, orb_k_start, orb_l_end, orb_l_start, &
     342              :          primitive_counter, virtual
     343              :       INTEGER(int_8)                                     :: estimate_to_store_int, neris_tmp, &
     344              :                                                             neris_total, nprim_ints
     345         2908 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of, nimages
     346         2908 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, lc_max, &
     347         2908 :                                                             lc_min, ld_max, ld_min, npgfa, npgfb, &
     348         2908 :                                                             npgfc, npgfd, nsgfa, nsgfb, nsgfc, &
     349         2908 :                                                             nsgfd
     350              :       LOGICAL                                            :: do_periodic
     351         2908 :       LOGICAL, DIMENSION(:, :), POINTER                  :: shm_atomic_pair_list
     352              :       REAL(KIND=dp) :: cartesian_estimate, coeffs_kind_max0, eps_schwarz, ln_10, &
     353              :          log10_eps_schwarz, log10_pmax, max_contraction_val, pmax_atom, pmax_entry, ra(3), rb(3), &
     354              :          rc(3), rd(3)
     355         2908 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: ee_buffer1, ee_buffer2, ee_work2, &
     356         2908 :                                                             primitive_integrals
     357         2908 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: L_block, L_full_matrix, max_contraction
     358         2908 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: BI1, MNRS
     359         2908 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: shm_pmax_block, zeta, zetb, zetc, zetd
     360              :       TYPE(cell_type), POINTER                           :: cell
     361              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     362              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_L
     363              :       TYPE(cp_fm_type)                                   :: fm_matrix_L
     364         2908 :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
     365         2908 :       TYPE(hfx_pgf_list), ALLOCATABLE, DIMENSION(:)      :: pgf_list_ij, pgf_list_kl
     366              :       TYPE(hfx_potential_type)                           :: mp2_potential_parameter
     367              :       TYPE(hfx_screen_coeff_type), ALLOCATABLE, &
     368         2908 :          DIMENSION(:, :), TARGET                         :: radii_pgf_large, screen_coeffs_pgf_large
     369              :       TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
     370         2908 :          POINTER                                         :: screen_coeffs_kind, tmp_R_1, tmp_R_2, &
     371         2908 :                                                             tmp_screen_pgf1, tmp_screen_pgf2
     372              :       TYPE(hfx_screen_coeff_type), &
     373         2908 :          DIMENSION(:, :, :, :), POINTER                  :: screen_coeffs_set
     374              :       TYPE(hfx_screen_coeff_type), &
     375         2908 :          DIMENSION(:, :, :, :, :, :), POINTER            :: radii_pgf, screen_coeffs_pgf
     376              :       TYPE(hfx_type), POINTER                            :: actual_x_data
     377         2908 :       TYPE(pair_list_type_mp2)                           :: list_kl
     378              :       TYPE(pair_set_list_type), ALLOCATABLE, &
     379         2908 :          DIMENSION(:)                                    :: set_list_kl
     380         2908 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     381              : 
     382         2908 :       CALL timeset(routineN, handle)
     383              : 
     384         2908 :       ln_10 = LOG(10.0_dp)
     385              : 
     386              :       !! initialize some counters
     387         2908 :       neris_tmp = 0_int_8
     388         2908 :       neris_total = 0_int_8
     389         2908 :       nprim_ints = 0_int_8
     390              : 
     391              :       CALL prepare_integral_calc(cell, qs_env, mp2_env, para_env, mp2_potential_parameter, actual_x_data, &
     392              :                                  do_periodic, basis_parameter, max_set, particle_set, natom, kind_of, &
     393              :                                  nsgf_max, primitive_integrals, ee_work2, ee_buffer1, ee_buffer2, &
     394              :                                  nspins, max_contraction, max_pgf, pgf_list_ij, &
     395              :                                  pgf_list_kl, nimages, eps_schwarz, log10_eps_schwarz, &
     396              :                                  screen_coeffs_set, screen_coeffs_kind, screen_coeffs_pgf, &
     397         2908 :                                  radii_pgf, RI_basis_parameter, RI_basis_info)
     398              : 
     399        52344 :       ALLOCATE (radii_pgf_large(SIZE(radii_pgf, 1), SIZE(radii_pgf, 2)))
     400        52344 :       ALLOCATE (screen_coeffs_pgf_large(SIZE(screen_coeffs_pgf, 1), SIZE(screen_coeffs_pgf, 2)))
     401        11632 :       DO iiB = 1, SIZE(radii_pgf, 1)
     402        37804 :          DO jjB = 1, SIZE(radii_pgf, 2)
     403        87240 :             radii_pgf_large(iiB, jjB)%x = 100_dp
     404              :          END DO
     405              :       END DO
     406        11632 :       DO iiB = 1, SIZE(screen_coeffs_pgf, 1)
     407        37804 :          DO jjB = 1, SIZE(screen_coeffs_pgf, 2)
     408        87240 :             screen_coeffs_pgf_large(iiB, jjB)%x = 5000_dp
     409              :          END DO
     410              :       END DO
     411         2908 :       tmp_R_1 => radii_pgf_large(:, :)
     412         2908 :       tmp_R_2 => radii_pgf_large(:, :)
     413         2908 :       tmp_screen_pgf1 => screen_coeffs_pgf_large(:, :)
     414         2908 :       tmp_screen_pgf2 => screen_coeffs_pgf_large(:, :)
     415              : 
     416              :       ! start computing the L matrix
     417        11632 :       ALLOCATE (L_full_matrix(RI_dimen, RI_dimen))
     418         2908 :       L_full_matrix = 0.0_dp
     419              : 
     420         2908 :       counter_L_blocks = 0
     421         5816 :       DO iatom = 1, natom
     422         2908 :          jatom = iatom
     423              : 
     424         2908 :          ikind = kind_of(iatom)
     425         2908 :          jkind = kind_of(jatom)
     426        11632 :          ra = particle_set(iatom)%r(:)
     427        11632 :          rb = particle_set(jatom)%r(:)
     428              : 
     429         2908 :          la_max => RI_basis_parameter(ikind)%lmax
     430         2908 :          la_min => RI_basis_parameter(ikind)%lmin
     431         2908 :          npgfa => RI_basis_parameter(ikind)%npgf
     432         2908 :          nseta = RI_basis_parameter(ikind)%nset
     433         2908 :          zeta => RI_basis_parameter(ikind)%zet
     434         2908 :          nsgfa => RI_basis_parameter(ikind)%nsgf
     435              : 
     436         2908 :          lb_max => basis_S0(jkind)%lmax
     437         2908 :          lb_min => basis_S0(jkind)%lmin
     438         2908 :          npgfb => basis_S0(jkind)%npgf
     439         2908 :          nsetb = basis_S0(jkind)%nset
     440         2908 :          zetb => basis_S0(jkind)%zet
     441         2908 :          nsgfb => basis_S0(jkind)%nsgf
     442              : 
     443         8724 :          DO katom = iatom, natom
     444         2908 :             latom = katom
     445              : 
     446         2908 :             kkind = kind_of(katom)
     447         2908 :             lkind = kind_of(latom)
     448        11632 :             rc = particle_set(katom)%r(:)
     449        11632 :             rd = particle_set(latom)%r(:)
     450              : 
     451         2908 :             lc_max => RI_basis_parameter(kkind)%lmax
     452         2908 :             lc_min => RI_basis_parameter(kkind)%lmin
     453         2908 :             npgfc => RI_basis_parameter(kkind)%npgf
     454         2908 :             nsetc = RI_basis_parameter(kkind)%nset
     455         2908 :             zetc => RI_basis_parameter(kkind)%zet
     456         2908 :             nsgfc => RI_basis_parameter(kkind)%nsgf
     457              : 
     458         2908 :             ld_max => basis_S0(lkind)%lmax
     459         2908 :             ld_min => basis_S0(lkind)%lmin
     460         2908 :             npgfd => basis_S0(lkind)%npgf
     461         2908 :             nsetd = RI_basis_parameter(lkind)%nset
     462         2908 :             zetd => basis_S0(lkind)%zet
     463         2908 :             nsgfd => basis_S0(lkind)%nsgf
     464              : 
     465         2908 :             jset = 1
     466         2908 :             lset = 1
     467        45948 :             DO iset = 1, nseta
     468              : 
     469        40132 :                ncob = npgfb(jset)*ncoset(lb_max(jset))
     470              : 
     471        40132 :                L_B_i_start = RI_index_table(iatom, iset)
     472        40132 :                L_B_i_end = RI_index_table(iatom, iset) + nsgfa(iset) - 1
     473              : 
     474        40132 :                kset_start = 1
     475        40132 :                IF (iatom == katom) kset_start = iset
     476       341424 :                DO kset = kset_start, nsetc
     477              : 
     478       298384 :                   counter_L_blocks = counter_L_blocks + 1
     479       298384 :                   IF (MOD(counter_L_blocks, para_env%num_pe) /= para_env%mepos) CYCLE
     480              : 
     481       279006 :                   L_B_k_start = RI_index_table(katom, kset)
     482       279006 :                   L_B_k_end = RI_index_table(katom, kset) + nsgfc(kset) - 1
     483              : 
     484       279006 :                   pmax_entry = 0.0_dp
     485       279006 :                   log10_pmax = pmax_entry
     486       279006 :                   log10_eps_schwarz = log_zero
     487              : 
     488       279006 :                   max_contraction_val = 1.0000_dp
     489              : 
     490      1116024 :                   ALLOCATE (L_block(nsgfa(iset), nsgfc(kset)))
     491              : 
     492              :                   CALL coulomb4(actual_x_data%eri4_context, ra, rb, rc, rd, npgfa(iset), npgfb(jset), npgfc(kset), npgfd(lset), &
     493              :                                 la_min(iset), la_max(iset), lb_min(jset), lb_max(jset), &
     494              :                                 lc_min(kset), lc_max(kset), ld_min(lset), ld_max(lset), &
     495              :                                 nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
     496              :                                 zeta(1:npgfa(iset), iset), zetb(1:npgfb(jset), jset), &
     497              :                                 zetc(1:npgfc(kset), kset), zetd(1:npgfd(lset), lset), &
     498              :                                 primitive_integrals, &
     499              :                                 mp2_potential_parameter, &
     500              :                                 actual_x_data%neighbor_cells, screen_coeffs_set(1, 1, 1, 1)%x, &
     501              :                                 screen_coeffs_set(1, 1, 1, 1)%x, eps_schwarz, &
     502              :                                 max_contraction_val, cartesian_estimate, cell, neris_tmp, &
     503              :                                 log10_pmax, log10_eps_schwarz, &
     504              :                                 tmp_R_1, tmp_R_2, tmp_screen_pgf1, tmp_screen_pgf2, &
     505              :                                 pgf_list_ij, pgf_list_kl, &
     506              :                                 RI_basis_parameter(ikind)%contraction(:, iset), basis_S0(jkind)%contraction(:, jset), &
     507              :                                 RI_basis_parameter(kkind)%contraction(:, kset), basis_S0(lkind)%contraction(:, lset), &
     508              :                                 ee_work2, ee_buffer1, ee_buffer2, &
     509       279006 :                                 nimages, do_periodic)
     510              : 
     511       279006 :                   primitive_counter = 0
     512       558012 :                   DO llB = 1, nsgfd(lset)
     513      1689326 :                      DO kkB = 1, nsgfc(kset)
     514      2541634 :                         DO jjB = 1, nsgfb(jset)
     515      4952738 :                            DO iiB = 1, nsgfa(iset)
     516      2690110 :                               primitive_counter = primitive_counter + 1
     517      3821424 :                               L_block(iiB, kkB) = primitive_integrals(primitive_counter)
     518              :                            END DO
     519              :                         END DO
     520              :                      END DO
     521              :                   END DO
     522              : 
     523      4100430 :                   L_full_matrix(L_B_i_start:L_B_i_end, L_B_k_start:L_B_k_end) = L_block
     524      3546782 :                   L_full_matrix(L_B_k_start:L_B_k_end, L_B_i_start:L_B_i_end) = TRANSPOSE(L_block)
     525              : 
     526       338516 :                   DEALLOCATE (L_block)
     527              : 
     528              :                END DO ! kset
     529              :             END DO ! iset
     530              : 
     531              :          END DO !katom
     532              :       END DO !iatom
     533              : 
     534              :       ! now create a fm_matrix for the L matrix
     535         2908 :       CALL para_env%sum(L_full_matrix)
     536              : 
     537              :       ! create a sub blacs_env
     538         2908 :       NULLIFY (blacs_env)
     539         2908 :       CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=para_env)
     540              : 
     541         2908 :       NULLIFY (fm_struct_L)
     542              :       CALL cp_fm_struct_create(fm_struct_L, context=blacs_env, nrow_global=RI_dimen, &
     543         2908 :                                ncol_global=RI_dimen, para_env=para_env)
     544         2908 :       CALL cp_fm_create(fm_matrix_L, fm_struct_L, name="fm_matrix_L")
     545         2908 :       CALL cp_fm_struct_release(fm_struct_L)
     546         2908 :       CALL cp_blacs_env_release(blacs_env)
     547              : 
     548              :       CALL cp_fm_set_submatrix(fm=fm_matrix_L, new_values=L_full_matrix, start_row=1, start_col=1, &
     549         2908 :                                n_rows=RI_dimen, n_cols=RI_dimen)
     550              : 
     551              :       info_chol = 0
     552         2908 :       CALL cp_fm_cholesky_decompose(matrix=fm_matrix_L, n=RI_dimen, info_out=info_chol)
     553         2908 :       CPASSERT(info_chol == 0)
     554              : 
     555              :       ! triangual invert
     556         2908 :       CALL cp_fm_triangular_invert(matrix_a=fm_matrix_L, uplo_tr='U')
     557              : 
     558              :       ! replicate L matrix to each proc
     559         2908 :       L_full_matrix = 0.0_dp
     560         2908 :       CALL cp_fm_get_submatrix(fm_matrix_L, L_full_matrix, 1, 1, RI_dimen, RI_dimen, .FALSE.)
     561         2908 :       CALL cp_fm_release(fm_matrix_L)
     562              : 
     563              :       ! clean lower part
     564       125632 :       DO iiB = 1, RI_dimen
     565      2676220 :          L_full_matrix(iiB + 1:RI_dimen, iiB) = 0.0_dp
     566              :       END DO
     567              : 
     568        46528 :       ALLOCATE (list_kl%elements(natom**2))
     569              : 
     570         8724 :       coeffs_kind_max0 = MAXVAL(screen_coeffs_kind(:, :)%x(2))
     571       571176 :       ALLOCATE (set_list_kl((max_set*natom)**2))
     572              : 
     573              :       !! precalculate maximum density matrix elements in blocks
     574         8724 :       actual_x_data%pmax_block = 0.0_dp
     575         2908 :       shm_pmax_block => actual_x_data%pmax_block
     576              : 
     577         2908 :       shm_atomic_pair_list => actual_x_data%atomic_pair_list
     578              : 
     579         2908 :       iatom_start = 1
     580         2908 :       iatom_end = natom
     581         2908 :       jatom_start = 1
     582         2908 :       jatom_end = natom
     583         2908 :       katom_start = 1
     584         2908 :       katom_end = natom
     585         2908 :       latom_start = 1
     586         2908 :       latom_end = natom
     587              : 
     588              :       CALL build_pair_list_mp2(natom, list_kl, set_list_kl, katom_start, katom_end, &
     589              :                                latom_start, latom_end, &
     590              :                                kind_of, basis_parameter, particle_set, &
     591              :                                do_periodic, screen_coeffs_set, screen_coeffs_kind, &
     592              :                                coeffs_kind_max0, log10_eps_schwarz, cell, 0.D+00, &
     593         2908 :                                shm_atomic_pair_list)
     594              : 
     595         2908 :       virtual = dimen - occupied
     596              : 
     597        14540 :       ALLOCATE (Lai(RI_dimen, virtual, occupied))
     598         2908 :       Lai = 0.0_dp
     599              : 
     600         5816 :       DO iatom = 1, natom
     601         2908 :          jatom = iatom
     602              : 
     603         2908 :          ikind = kind_of(iatom)
     604         2908 :          jkind = kind_of(jatom)
     605        11632 :          ra = particle_set(iatom)%r(:)
     606        11632 :          rb = particle_set(jatom)%r(:)
     607              : 
     608         2908 :          la_max => RI_basis_parameter(ikind)%lmax
     609         2908 :          la_min => RI_basis_parameter(ikind)%lmin
     610         2908 :          npgfa => RI_basis_parameter(ikind)%npgf
     611         2908 :          nseta = RI_basis_parameter(ikind)%nset
     612         2908 :          zeta => RI_basis_parameter(ikind)%zet
     613         2908 :          nsgfa => RI_basis_parameter(ikind)%nsgf
     614              : 
     615         2908 :          lb_max => basis_S0(jkind)%lmax
     616         2908 :          lb_min => basis_S0(jkind)%lmin
     617         2908 :          npgfb => basis_S0(jkind)%npgf
     618         2908 :          nsetb = basis_S0(jkind)%nset
     619         2908 :          zetb => basis_S0(jkind)%zet
     620         2908 :          nsgfb => basis_S0(jkind)%nsgf
     621              : 
     622         2908 :          jset = 1
     623        45948 :          DO iset = 1, nseta
     624              : 
     625        40132 :             counter_L_blocks = counter_L_blocks + 1
     626        40132 :             IF (MOD(counter_L_blocks, para_env%num_pe) /= para_env%mepos) CYCLE
     627              : 
     628        37518 :             ncob = npgfb(jset)*ncoset(lb_max(jset))
     629              : 
     630        37518 :             L_B_i_start = RI_index_table(iatom, iset)
     631        37518 :             L_B_i_end = RI_index_table(iatom, iset) + nsgfa(iset) - 1
     632              : 
     633       187590 :             ALLOCATE (BI1(dimen, dimen, nsgfa(iset)))
     634        37518 :             BI1 = 0.0_dp
     635              : 
     636        75036 :             DO i_list_kl = 1, list_kl%n_element
     637              : 
     638        37518 :                katom = list_kl%elements(i_list_kl)%pair(1)
     639        37518 :                latom = list_kl%elements(i_list_kl)%pair(2)
     640              : 
     641        37518 :                i_set_list_kl_start = list_kl%elements(i_list_kl)%set_bounds(1)
     642        37518 :                i_set_list_kl_stop = list_kl%elements(i_list_kl)%set_bounds(2)
     643        37518 :                kkind = list_kl%elements(i_list_kl)%kind_pair(1)
     644        37518 :                lkind = list_kl%elements(i_list_kl)%kind_pair(2)
     645       150072 :                rc = list_kl%elements(i_list_kl)%r1
     646       150072 :                rd = list_kl%elements(i_list_kl)%r2
     647              : 
     648        37518 :                pmax_atom = 0.0_dp
     649              : 
     650        37518 :                lc_max => basis_parameter(kkind)%lmax
     651        37518 :                lc_min => basis_parameter(kkind)%lmin
     652        37518 :                npgfc => basis_parameter(kkind)%npgf
     653        37518 :                zetc => basis_parameter(kkind)%zet
     654        37518 :                nsgfc => basis_parameter(kkind)%nsgf
     655              : 
     656        37518 :                ld_max => basis_parameter(lkind)%lmax
     657        37518 :                ld_min => basis_parameter(lkind)%lmin
     658        37518 :                npgfd => basis_parameter(lkind)%npgf
     659        37518 :                zetd => basis_parameter(lkind)%zet
     660        37518 :                nsgfd => basis_parameter(lkind)%nsgf
     661              : 
     662       412698 :                DO i_set_list_kl = i_set_list_kl_start, i_set_list_kl_stop
     663       337662 :                   kset = set_list_kl(i_set_list_kl)%pair(1)
     664       337662 :                   lset = set_list_kl(i_set_list_kl)%pair(2)
     665              : 
     666       337662 :                   IF (katom == latom .AND. lset < kset) CYCLE
     667              : 
     668       225108 :                   orb_k_start = mp2_biel%index_table(katom, kset)
     669       225108 :                   orb_k_end = orb_k_start + nsgfc(kset) - 1
     670       225108 :                   orb_l_start = mp2_biel%index_table(latom, lset)
     671       225108 :                   orb_l_end = orb_l_start + nsgfd(lset) - 1
     672              : 
     673              :                   !! get max_vals if we screen on initial density
     674       225108 :                   pmax_entry = 0.0_dp
     675              : 
     676       225108 :                   log10_pmax = pmax_entry
     677       225108 :                   log10_eps_schwarz = log_zero
     678              : 
     679       225108 :                   IF (ALLOCATED(MNRS)) DEALLOCATE (MNRS)
     680      1125540 :                   ALLOCATE (MNRS(nsgfd(lset), nsgfc(kset), nsgfa(iset)))
     681              : 
     682       225108 :                   MNRS = 0.0_dp
     683              : 
     684       225108 :                   max_contraction_val = max_contraction(kset, katom)*max_contraction(lset, latom)
     685              : 
     686              :                   CALL coulomb4(actual_x_data%eri4_context, ra, rb, rc, rd, npgfa(iset), npgfb(jset), npgfc(kset), npgfd(lset), &
     687              :                                 la_min(iset), la_max(iset), lb_min(jset), lb_max(jset), &
     688              :                                 lc_min(kset), lc_max(kset), ld_min(lset), ld_max(lset), &
     689              :                                 nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
     690              :                                 zeta(1:npgfa(iset), iset), zetb(1:npgfb(jset), jset), &
     691              :                                 zetc(1:npgfc(kset), kset), zetd(1:npgfd(lset), lset), &
     692              :                                 primitive_integrals, &
     693              :                                 mp2_potential_parameter, &
     694              :                                 actual_x_data%neighbor_cells, screen_coeffs_set(kset, kset, kkind, kkind)%x, &
     695              :                                 screen_coeffs_set(kset, kset, kkind, kkind)%x, eps_schwarz, &
     696              :                                 max_contraction_val, cartesian_estimate, cell, neris_tmp, &
     697              :                                 log10_pmax, log10_eps_schwarz, &
     698              :                                 tmp_R_1, tmp_R_2, tmp_screen_pgf1, tmp_screen_pgf2, &
     699              :                                 pgf_list_ij, pgf_list_kl, &
     700              :                                 RI_basis_parameter(ikind)%contraction(:, iset), basis_S0(jkind)%contraction(:, jset), &
     701              :                                 basis_parameter(kkind)%contraction(:, kset), basis_parameter(lkind)%contraction(:, lset), &
     702              :                                 ee_work2, ee_buffer1, ee_buffer2, &
     703       225108 :                                 nimages, do_periodic)
     704              : 
     705       225108 :                   nints = nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset)
     706       225108 :                   neris_total = neris_total + nints
     707       225108 :                   nprim_ints = nprim_ints + neris_tmp
     708       225108 :                   IF (cartesian_estimate == 0.0_dp) cartesian_estimate = TINY(cartesian_estimate)
     709       225108 :                   estimate_to_store_int = EXPONENT(cartesian_estimate)
     710       225108 :                   estimate_to_store_int = MAX(estimate_to_store_int, -15_int_8)
     711       225108 :                   cartesian_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int + 1)
     712              : 
     713       225108 :                   primitive_counter = 0
     714      1238094 :                   DO llB = 1, nsgfd(lset)
     715      5477628 :                      DO kkB = 1, nsgfc(kset)
     716      9492054 :                         DO jjB = 1, nsgfb(jset)
     717     21444462 :                            DO iiB = 1, nsgfa(iset)
     718     12965394 :                               primitive_counter = primitive_counter + 1
     719     17204928 :                               MNRS(llB, kkB, iiB) = primitive_integrals(primitive_counter)
     720              :                            END DO
     721              :                         END DO
     722              :                      END DO
     723              :                   END DO
     724              : 
     725       951054 :                   DO iiB = 1, nsgfa(iset)
     726     16522272 :                      BI1(orb_l_start:orb_l_end, orb_k_start:orb_k_end, iiB) = MNRS(:, :, iiB)
     727     17089410 :                      BI1(orb_k_start:orb_k_end, orb_l_start:orb_l_end, iiB) = TRANSPOSE(MNRS(:, :, iiB))
     728              :                   END DO
     729              : 
     730              :                END DO ! i_set_list_kl
     731              :             END DO ! i_list_kl
     732              : 
     733       152256 :             DO iiB = 1, nsgfa(iset)
     734       114738 :                BI1(1:virtual, 1:occupied, iiB) = MATMUL(TRANSPOSE(C(1:dimen, occupied + 1:dimen)), &
     735    216281130 :                                                         MATMUL(BI1(1:dimen, 1:dimen, iiB), C(1:dimen, 1:occupied)))
     736      3823872 :                Lai(L_B_i_start + iiB - 1, 1:virtual, 1:occupied) = BI1(1:virtual, 1:occupied, iiB)
     737              :             END DO
     738              : 
     739        43040 :             DEALLOCATE (BI1)
     740              : 
     741              :          END DO
     742              :       END DO
     743              : 
     744         2908 :       CALL para_env%sum(Lai)
     745              : 
     746        11632 :       DO iiB = 1, occupied
     747        11632 :          IF (MOD(iiB, para_env%num_pe) == para_env%mepos) THEN
     748    293843910 :             Lai(1:RI_dimen, 1:virtual, iiB) = MATMUL(TRANSPOSE(L_full_matrix), Lai(1:RI_dimen, 1:virtual, iiB))
     749              :          ELSE
     750       237674 :             Lai(:, :, iiB) = 0.0_dp
     751              :          END IF
     752              :       END DO
     753              : 
     754         2908 :       CALL para_env%sum(Lai)
     755              : 
     756         2908 :       DEALLOCATE (set_list_kl)
     757              : 
     758        29080 :       DO i = 1, max_pgf**2
     759        26172 :          DEALLOCATE (pgf_list_ij(i)%image_list)
     760        29080 :          DEALLOCATE (pgf_list_kl(i)%image_list)
     761              :       END DO
     762              : 
     763         2908 :       DEALLOCATE (pgf_list_ij)
     764         2908 :       DEALLOCATE (pgf_list_kl)
     765              : 
     766         2908 :       DEALLOCATE (max_contraction, kind_of)
     767              : 
     768         2908 :       DEALLOCATE (ee_work2, ee_buffer1, ee_buffer2)
     769              : 
     770         2908 :       DEALLOCATE (nimages)
     771              : 
     772         2908 :       CALL timestop(handle)
     773              : 
     774        11632 :    END SUBROUTINE calc_lai_libint
     775              : 
     776              : ! **************************************************************************************************
     777              : !> \brief ...
     778              : !> \param cell ...
     779              : !> \param qs_env ...
     780              : !> \param mp2_env ...
     781              : !> \param para_env ...
     782              : !> \param mp2_potential_parameter ...
     783              : !> \param actual_x_data ...
     784              : !> \param do_periodic ...
     785              : !> \param basis_parameter ...
     786              : !> \param max_set ...
     787              : !> \param particle_set ...
     788              : !> \param natom ...
     789              : !> \param kind_of ...
     790              : !> \param nsgf_max ...
     791              : !> \param primitive_integrals ...
     792              : !> \param ee_work2 ...
     793              : !> \param ee_buffer1 ...
     794              : !> \param ee_buffer2 ...
     795              : !> \param nspins ...
     796              : !> \param max_contraction ...
     797              : !> \param max_pgf ...
     798              : !> \param pgf_list_ij ...
     799              : !> \param pgf_list_kl ...
     800              : !> \param nimages ...
     801              : !> \param eps_schwarz ...
     802              : !> \param log10_eps_schwarz ...
     803              : !> \param screen_coeffs_set ...
     804              : !> \param screen_coeffs_kind ...
     805              : !> \param screen_coeffs_pgf ...
     806              : !> \param radii_pgf ...
     807              : !> \param RI_basis_parameter ...
     808              : !> \param RI_basis_info ...
     809              : ! **************************************************************************************************
     810         2949 :    SUBROUTINE prepare_integral_calc(cell, qs_env, mp2_env, para_env, mp2_potential_parameter, actual_x_data, &
     811              :                                     do_periodic, basis_parameter, max_set, particle_set, natom, kind_of, &
     812              :                                     nsgf_max, primitive_integrals, ee_work2, ee_buffer1, ee_buffer2, &
     813              :                                     nspins, max_contraction, max_pgf, pgf_list_ij, &
     814              :                                     pgf_list_kl, nimages, eps_schwarz, log10_eps_schwarz, &
     815              :                                     screen_coeffs_set, screen_coeffs_kind, screen_coeffs_pgf, &
     816              :                                     radii_pgf, RI_basis_parameter, RI_basis_info)
     817              : 
     818              :       TYPE(cell_type), POINTER                           :: cell
     819              :       TYPE(qs_environment_type), INTENT(IN), POINTER     :: qs_env
     820              :       TYPE(mp2_type), INTENT(INOUT)                      :: mp2_env
     821              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
     822              :       TYPE(hfx_potential_type), INTENT(OUT)              :: mp2_potential_parameter
     823              :       TYPE(hfx_type), POINTER                            :: actual_x_data
     824              :       LOGICAL, INTENT(OUT)                               :: do_periodic
     825              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
     826              :       INTEGER, INTENT(OUT)                               :: max_set
     827              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     828              :       INTEGER, INTENT(OUT)                               :: natom
     829              :       INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT)    :: kind_of
     830              :       INTEGER, INTENT(OUT)                               :: nsgf_max
     831              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), &
     832              :          INTENT(OUT)                                     :: primitive_integrals, ee_work2, &
     833              :                                                             ee_buffer1, ee_buffer2
     834              :       INTEGER, INTENT(OUT)                               :: nspins
     835              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
     836              :          INTENT(OUT)                                     :: max_contraction
     837              :       INTEGER, INTENT(OUT)                               :: max_pgf
     838              :       TYPE(hfx_pgf_list), ALLOCATABLE, DIMENSION(:), &
     839              :          INTENT(OUT)                                     :: pgf_list_ij, pgf_list_kl
     840              :       INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT)    :: nimages
     841              :       REAL(KIND=dp), INTENT(OUT)                         :: eps_schwarz, log10_eps_schwarz
     842              :       TYPE(hfx_screen_coeff_type), &
     843              :          DIMENSION(:, :, :, :), POINTER                  :: screen_coeffs_set
     844              :       TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
     845              :          POINTER                                         :: screen_coeffs_kind
     846              :       TYPE(hfx_screen_coeff_type), &
     847              :          DIMENSION(:, :, :, :, :, :), POINTER            :: screen_coeffs_pgf, radii_pgf
     848              :       TYPE(hfx_basis_type), DIMENSION(:), OPTIONAL, &
     849              :          POINTER                                         :: RI_basis_parameter
     850              :       TYPE(hfx_basis_info_type), INTENT(IN), OPTIONAL    :: RI_basis_info
     851              : 
     852              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'prepare_integral_calc'
     853              : 
     854              :       INTEGER                                            :: handle, i_thread, ii, ikind, irep, &
     855              :                                                             l_max, n_threads, ncos_max, nkind, &
     856              :                                                             nneighbors
     857         2949 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     858              :       TYPE(dft_control_type), POINTER                    :: dft_control
     859              :       TYPE(hfx_basis_info_type), POINTER                 :: basis_info
     860              :       TYPE(hfx_type), POINTER                            :: shm_master_x_data
     861              : 
     862         2949 :       CALL timeset(routineN, handle)
     863              : 
     864         2949 :       NULLIFY (dft_control)
     865              : 
     866         2949 :       irep = 1
     867              : 
     868              :       CALL get_qs_env(qs_env, &
     869              :                       atomic_kind_set=atomic_kind_set, &
     870              :                       cell=cell, &
     871         2949 :                       dft_control=dft_control)
     872              : 
     873              :       !! Calculate l_max used in fgamma , because init_md_ftable is definitely not thread safe
     874         2949 :       nkind = SIZE(atomic_kind_set, 1)
     875         2949 :       l_max = 0
     876         5916 :       DO ikind = 1, nkind
     877        14973 :          l_max = MAX(l_max, MAXVAL(qs_env%x_data(1, 1)%basis_parameter(ikind)%lmax))
     878              :       END DO
     879         2949 :       IF (PRESENT(RI_basis_parameter)) THEN
     880         5816 :          DO ikind = 1, nkind
     881        45948 :             l_max = MAX(l_max, MAXVAL(RI_basis_parameter(ikind)%lmax))
     882              :          END DO
     883              :       END IF
     884         2949 :       l_max = 4*l_max
     885              : 
     886         2949 :       CALL r12_fundamentals_init(l_max, mp2_env%potential_parameter, para_env)
     887         2949 :       IF (mp2_env%potential_parameter%potential_type == do_potential_long) THEN
     888            2 :          mp2_potential_parameter%omega = mp2_env%potential_parameter%omega
     889              :       END IF
     890         2949 :       mp2_potential_parameter%potential_type = mp2_env%potential_parameter%potential_type
     891              : 
     892         2949 :       n_threads = 1
     893              : 
     894         2949 :       i_thread = 0
     895              : 
     896         2949 :       actual_x_data => qs_env%x_data(irep, i_thread + 1)
     897              : 
     898         2949 :       shm_master_x_data => qs_env%x_data(irep, 1)
     899              : 
     900         2949 :       do_periodic = actual_x_data%periodic_parameter%do_periodic
     901              : 
     902         2949 :       IF (do_periodic) THEN
     903              :          ! ** Rebuild neighbor lists in case the cell has changed (i.e. NPT MD)
     904            0 :          actual_x_data%periodic_parameter%number_of_shells = actual_x_data%periodic_parameter%mode
     905              :          CALL hfx_create_neighbor_cells(actual_x_data, actual_x_data%periodic_parameter%number_of_shells_from_input, &
     906            0 :                                         cell, i_thread)
     907              :       END IF
     908              : 
     909         2949 :       basis_parameter => actual_x_data%basis_parameter
     910         2949 :       basis_info => actual_x_data%basis_info
     911              : 
     912         2949 :       max_set = basis_info%max_set
     913         2949 :       IF (PRESENT(RI_basis_info)) max_set = MAX(max_set, RI_basis_info%max_set)
     914              : 
     915              :       CALL get_qs_env(qs_env=qs_env, &
     916              :                       atomic_kind_set=atomic_kind_set, &
     917         2949 :                       particle_set=particle_set)
     918              : 
     919         2949 :       natom = SIZE(particle_set, 1)
     920              : 
     921         2949 :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
     922              : 
     923              :       !! precompute maximum nco and allocate scratch
     924         2949 :       ncos_max = 0
     925         2949 :       nsgf_max = 0
     926         2949 :       CALL get_ncos_and_ncsgf(natom, kind_of, basis_parameter, ncos_max, nsgf_max)
     927         2949 :       IF (PRESENT(RI_basis_parameter)) THEN
     928         2908 :          CALL get_ncos_and_ncsgf(natom, kind_of, RI_basis_parameter, ncos_max, nsgf_max)
     929              :       END IF
     930              : 
     931              :       !! Allocate the arrays for the integrals.
     932         8847 :       ALLOCATE (primitive_integrals(nsgf_max**4))
     933         2949 :       primitive_integrals = 0.0_dp
     934              : 
     935         8847 :       ALLOCATE (ee_work2(ncos_max**4))
     936         5898 :       ALLOCATE (ee_buffer1(ncos_max**4))
     937         5898 :       ALLOCATE (ee_buffer2(ncos_max**4))
     938              : 
     939         2949 :       nspins = dft_control%nspins
     940              : 
     941         2949 :       CALL get_max_contraction(max_contraction, max_set, natom, max_pgf, kind_of, basis_parameter)
     942              : 
     943              :       ! ** Allocate buffers for pgf_lists
     944         2949 :       nneighbors = SIZE(actual_x_data%neighbor_cells)
     945        36513 :       ALLOCATE (pgf_list_ij(max_pgf**2))
     946        33564 :       ALLOCATE (pgf_list_kl(max_pgf**2))
     947         8847 :       ALLOCATE (nimages(max_pgf**2))
     948              : 
     949        30615 :       DO ii = 1, max_pgf**2
     950       359658 :          ALLOCATE (pgf_list_ij(ii)%image_list(nneighbors))
     951       334941 :          ALLOCATE (pgf_list_kl(ii)%image_list(nneighbors))
     952              :       END DO
     953              : 
     954              :       !!! Skipped part
     955              : 
     956              :       !! Get screening parameter
     957         2949 :       eps_schwarz = actual_x_data%screening_parameter%eps_schwarz
     958         2949 :       IF (eps_schwarz <= 0.0_dp) THEN
     959            0 :          log10_eps_schwarz = log_zero
     960              :       ELSE
     961         2949 :          log10_eps_schwarz = LOG10(eps_schwarz)
     962              :       END IF
     963              : 
     964              :       !! The number of integrals that fit into the given MAX_MEMORY
     965              : 
     966              :       !!!!!!!! Missing part on the density matrix
     967              : 
     968              :       !! Initialize schwarz screening matrices for near field estimates and boxing screening matrices
     969              :       !! for far field estimates. The update is only performed if the geomtry of the system changed.
     970              :       !! If the system is periodic, then the corresponding routines are called and some variables
     971              :       !! are initialized
     972              : 
     973         2949 :       IF (.NOT. shm_master_x_data%screen_funct_is_initialized) THEN
     974              :          CALL calc_pair_dist_radii(qs_env, basis_parameter, &
     975              :                                    shm_master_x_data%pair_dist_radii_pgf, max_set, max_pgf, eps_schwarz, &
     976            0 :                                    n_threads, i_thread)
     977              :          CALL calc_screening_functions(qs_env, basis_parameter, actual_x_data%eri4_context, shm_master_x_data%potential_parameter, &
     978              :                                        shm_master_x_data%screen_funct_coeffs_set, &
     979              :                                        shm_master_x_data%screen_funct_coeffs_kind, &
     980              :                                        shm_master_x_data%screen_funct_coeffs_pgf, &
     981              :                                        shm_master_x_data%pair_dist_radii_pgf, &
     982            0 :                                        max_set, max_pgf, n_threads, i_thread)
     983              : 
     984            0 :          shm_master_x_data%screen_funct_is_initialized = .TRUE.
     985              :       END IF
     986              : 
     987         2949 :       screen_coeffs_set => shm_master_x_data%screen_funct_coeffs_set
     988         2949 :       screen_coeffs_kind => shm_master_x_data%screen_funct_coeffs_kind
     989         2949 :       screen_coeffs_pgf => shm_master_x_data%screen_funct_coeffs_pgf
     990         2949 :       radii_pgf => shm_master_x_data%pair_dist_radii_pgf
     991              : 
     992         2949 :       CALL timestop(handle)
     993              : 
     994         2949 :    END SUBROUTINE prepare_integral_calc
     995              : 
     996              : ! **************************************************************************************************
     997              : !> \brief ...
     998              : !> \param natom ...
     999              : !> \param kind_of ...
    1000              : !> \param basis_parameter ...
    1001              : !> \param ncos_max ...
    1002              : !> \param nsgf_max ...
    1003              : ! **************************************************************************************************
    1004         5857 :    SUBROUTINE get_ncos_and_ncsgf(natom, kind_of, basis_parameter, ncos_max, nsgf_max)
    1005              : 
    1006              :       INTEGER, INTENT(IN)                                :: natom
    1007              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: kind_of
    1008              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
    1009              :       INTEGER, INTENT(INOUT)                             :: ncos_max, nsgf_max
    1010              : 
    1011              :       INTEGER                                            :: iatom, ikind, iset, nseta
    1012         5857 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, npgfa, nsgfa
    1013              : 
    1014        11756 :       DO iatom = 1, natom
    1015         5899 :          ikind = kind_of(iatom)
    1016         5899 :          nseta = basis_parameter(ikind)%nset
    1017         5899 :          npgfa => basis_parameter(ikind)%npgf
    1018         5899 :          la_max => basis_parameter(ikind)%lmax
    1019         5899 :          nsgfa => basis_parameter(ikind)%nsgf
    1020        61065 :          DO iset = 1, nseta
    1021        49309 :             ncos_max = MAX(ncos_max, ncoset(la_max(iset)))
    1022        55208 :             nsgf_max = MAX(nsgf_max, nsgfa(iset))
    1023              :          END DO
    1024              :       END DO
    1025              : 
    1026         5857 :    END SUBROUTINE get_ncos_and_ncsgf
    1027              : 
    1028              : ! **************************************************************************************************
    1029              : !> \brief ...
    1030              : !> \param max_contraction ...
    1031              : !> \param max_set ...
    1032              : !> \param natom ...
    1033              : !> \param max_pgf ...
    1034              : !> \param kind_of ...
    1035              : !> \param basis_parameter ...
    1036              : ! **************************************************************************************************
    1037         2949 :    SUBROUTINE get_max_contraction(max_contraction, max_set, natom, max_pgf, kind_of, basis_parameter)
    1038              : 
    1039              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :), &
    1040              :          INTENT(OUT)                                     :: max_contraction
    1041              :       INTEGER, INTENT(IN)                                :: max_set, natom
    1042              :       INTEGER, INTENT(OUT)                               :: max_pgf
    1043              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: kind_of
    1044              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
    1045              : 
    1046              :       INTEGER                                            :: i, jatom, jkind, jset, ncob, nsetb, sgfb
    1047         2949 :       INTEGER, DIMENSION(:), POINTER                     :: lb_max, npgfb, nsgfb
    1048         2949 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfb
    1049         2949 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: sphi_b
    1050              : 
    1051        11796 :       ALLOCATE (max_contraction(max_set, natom))
    1052              : 
    1053         2949 :       max_contraction = 0.0_dp
    1054         2949 :       max_pgf = 0
    1055         5940 :       DO jatom = 1, natom
    1056         2991 :          jkind = kind_of(jatom)
    1057         2991 :          lb_max => basis_parameter(jkind)%lmax
    1058         2991 :          nsetb = basis_parameter(jkind)%nset
    1059         2991 :          npgfb => basis_parameter(jkind)%npgf
    1060         2991 :          first_sgfb => basis_parameter(jkind)%first_sgf
    1061         2991 :          sphi_b => basis_parameter(jkind)%sphi
    1062         2991 :          nsgfb => basis_parameter(jkind)%nsgf
    1063        15117 :          DO jset = 1, nsetb
    1064              :             ! takes the primitive to contracted transformation into account
    1065         9177 :             ncob = npgfb(jset)*ncoset(lb_max(jset))
    1066         9177 :             sgfb = first_sgfb(1, jset)
    1067              :             ! if the primitives are assumed to be all of max_val2, max_val2*p2s_b becomes
    1068              :             ! the maximum value after multiplication with sphi_b
    1069       436350 :             max_contraction(jset, jatom) = MAXVAL([(SUM(ABS(sphi_b(1:ncob, i))), i=sgfb, sgfb + nsgfb(jset) - 1)])
    1070        12168 :             max_pgf = MAX(max_pgf, npgfb(jset))
    1071              :          END DO
    1072              :       END DO
    1073              : 
    1074         2949 :    END SUBROUTINE get_max_contraction
    1075              : 
    1076       114738 : END MODULE mp2_ri_libint
        

Generated by: LCOV version 2.0-1