LCOV - code coverage report
Current view: top level - src - mp2_direct_method.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 84.5 % 734 620
Test Date: 2026-11-24 01:22:08 Functions: 85.7 % 7 6

            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 MP2 energy
      10              : !> \par History
      11              : !>      06.2011 created [Mauro Del Ben]
      12              : !> \author Mauro Del Ben
      13              : ! **************************************************************************************************
      14              : MODULE mp2_direct_method
      15              :    USE cell_types,                      ONLY: cell_type
      16              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      17              :                                               cp_logger_type
      18              :    USE four_center_integral_methods,    ONLY: coulomb4
      19              :    USE hfx_load_balance_methods,        ONLY: cost_model,&
      20              :                                               p1_energy,&
      21              :                                               p2_energy,&
      22              :                                               p3_energy
      23              :    USE hfx_pair_list_methods,           ONLY: build_pair_list_mp2
      24              :    USE hfx_types,                       ONLY: hfx_basis_type,&
      25              :                                               hfx_pgf_list,&
      26              :                                               hfx_potential_type,&
      27              :                                               hfx_screen_coeff_type,&
      28              :                                               hfx_type,&
      29              :                                               pair_set_list_type
      30              :    USE kinds,                           ONLY: dp,&
      31              :                                               int_8
      32              :    USE machine,                         ONLY: m_flush
      33              :    USE mathconstants,                   ONLY: zero
      34              :    USE message_passing,                 ONLY: mp_para_env_release,&
      35              :                                               mp_para_env_type
      36              :    USE mp2_ri_libint,                   ONLY: prepare_integral_calc
      37              :    USE mp2_types,                       ONLY: mp2_biel_type,&
      38              :                                               mp2_type,&
      39              :                                               pair_list_type_mp2
      40              :    USE orbital_pointers,                ONLY: ncoset
      41              :    USE particle_types,                  ONLY: particle_type
      42              :    USE qs_environment_types,            ONLY: qs_environment_type
      43              : #include "./base/base_uses.f90"
      44              : 
      45              :    IMPLICIT NONE
      46              :    PRIVATE
      47              : 
      48              :    PUBLIC ::  mp2_direct_energy, mp2_canonical_direct_single_batch
      49              : 
      50              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'mp2_direct_method'
      51              : 
      52              : !***
      53              : 
      54              : CONTAINS
      55              : 
      56              : ! **************************************************************************************************
      57              : !> \brief ...
      58              : !> \param dimen ...
      59              : !> \param occ_i ...
      60              : !> \param occ_j ...
      61              : !> \param mp2_biel ...
      62              : !> \param mp2_env ...
      63              : !> \param C_i ...
      64              : !> \param Auto_i ...
      65              : !> \param Emp2 ...
      66              : !> \param Emp2_Cou ...
      67              : !> \param Emp2_ex ...
      68              : !> \param qs_env ...
      69              : !> \param para_env ...
      70              : !> \param unit_nr ...
      71              : !> \param C_j ...
      72              : !> \param Auto_j ...
      73              : ! **************************************************************************************************
      74           24 :    SUBROUTINE mp2_direct_energy(dimen, occ_i, occ_j, mp2_biel, mp2_env, C_i, Auto_i, Emp2, Emp2_Cou, Emp2_ex, &
      75           24 :                                 qs_env, para_env, unit_nr, C_j, Auto_j)
      76              :       INTEGER                                            :: dimen, occ_i, occ_j
      77              :       TYPE(mp2_biel_type)                                :: mp2_biel
      78              :       TYPE(mp2_type)                                     :: mp2_env
      79              :       REAL(KIND=dp), DIMENSION(dimen, dimen)             :: C_i
      80              :       REAL(KIND=dp), DIMENSION(dimen)                    :: Auto_i
      81              :       REAL(KIND=dp)                                      :: Emp2, Emp2_Cou, Emp2_ex
      82              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      83              :       TYPE(mp_para_env_type), POINTER                    :: para_env
      84              :       INTEGER                                            :: unit_nr
      85              :       REAL(KIND=dp), DIMENSION(dimen, dimen), OPTIONAL   :: C_j
      86              :       REAL(KIND=dp), DIMENSION(dimen), OPTIONAL          :: Auto_j
      87              : 
      88              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'mp2_direct_energy'
      89              :       REAL(KIND=dp), PARAMETER                           :: zero = 0.D+00
      90              : 
      91              :       INTEGER :: batch_number, color_sub, counter, elements_ij_proc, group_counter, handle, i, &
      92              :          i_batch, i_batch_start, i_group_counter, j, j_batch_start, j_group_counter, last_batch, &
      93              :          max_batch_number, max_batch_size, max_set, minimum_memory_needed, my_batch_size, &
      94              :          my_I_batch_size, my_I_occupied_end, my_I_occupied_start, my_J_batch_size, &
      95              :          my_J_occupied_end, my_J_occupied_start, natom, Ni_occupied, Nj_occupied, number_groups, &
      96              :          number_i_subset, number_j_subset, one, sqrt_number_groups, total_I_size_batch_group, &
      97              :          total_J_size_batch_group, virt_i, virt_j
      98           24 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: batch_sizes, batch_sizes_tmp, &
      99           24 :                                                             vector_batch_I_size_group, &
     100           24 :                                                             vector_batch_J_size_group
     101           24 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: ij_list_proc, ij_list_proc_temp, &
     102           24 :                                                             ij_matrix
     103              :       LOGICAL                                            :: alpha_beta_case
     104              :       TYPE(mp_para_env_type), POINTER                    :: para_env_sub
     105              : 
     106           24 :       CALL timeset(routineN, handle)
     107              : 
     108           24 :       alpha_beta_case = .FALSE.
     109           24 :       IF (PRESENT(C_j) .AND. PRESENT(Auto_j)) alpha_beta_case = .TRUE.
     110              : 
     111              :       ! create the local para env
     112              :       ! each para_env_sub corresponds to a group that is going to compute
     113              :       ! all the integrals. To each group a batch I is assigned and the
     114              :       ! communication takes place only inside the group
     115           24 :       number_groups = para_env%num_pe/mp2_env%mp2_num_proc
     116           24 :       IF (number_groups*mp2_env%mp2_num_proc /= para_env%num_pe) THEN
     117            0 :          CPABORT(" The number of processors needs to be a multiple of the processors per group. ")
     118              :       END IF
     119           24 :       IF (number_groups > occ_i*occ_j) THEN
     120            2 :          IF (unit_nr > 0) WRITE (unit_nr, '(T3,A)') 'Number of groups greater then the number of IJ pairs!'
     121            2 :          IF (unit_nr > 0) WRITE (unit_nr, '(T3,A)') 'Consider using more processors per group for improved efficiency'
     122              :       END IF
     123              : 
     124           24 :       color_sub = para_env%mepos/mp2_env%mp2_num_proc
     125           24 :       ALLOCATE (para_env_sub)
     126           24 :       CALL para_env_sub%from_split(para_env, color_sub)
     127              : 
     128              :       ! calculate the maximal size of the batch, according to the maximum RS size
     129           24 :       max_set = SIZE(mp2_biel%index_table, 2)
     130           24 :       minimum_memory_needed = (8*(max_set**4))/1024**2
     131           24 :       IF (minimum_memory_needed > mp2_env%mp2_memory) THEN
     132            0 :          IF (unit_nr > 0) WRITE (unit_nr, '(T3,A,T66,F12.6,A3)') 'Memory required below the minimum, new memory:', &
     133            0 :             minimum_memory_needed, 'MiB'
     134            0 :          mp2_env%mp2_memory = minimum_memory_needed
     135              :       END IF
     136              : 
     137              :       ! Distribute the batches over the groups in
     138              :       ! a rectangular fashion, bigger size for J index
     139              :       ! the sizes of the I batches should be as small as possible
     140           24 :       sqrt_number_groups = INT(SQRT(REAL(number_groups, KIND=dp)))
     141           24 :       DO i = 1, number_groups
     142           24 :          IF (MOD(number_groups, i) == 0) THEN
     143           24 :             IF (sqrt_number_groups/i <= 1) THEN
     144              :                number_j_subset = i
     145              :                EXIT
     146              :             END IF
     147              :          END IF
     148              :       END DO
     149           24 :       number_i_subset = number_groups/number_j_subset
     150              : 
     151           24 :       IF (number_i_subset < number_j_subset) THEN
     152            0 :          number_i_subset = number_j_subset
     153            0 :          number_j_subset = number_groups/number_i_subset
     154              :       END IF
     155              : 
     156              :       ! Distribute the I index and the J index over groups
     157           24 :       total_I_size_batch_group = occ_i/number_i_subset
     158           24 :       IF (total_I_size_batch_group < 1) total_I_size_batch_group = 1
     159           72 :       ALLOCATE (vector_batch_I_size_group(0:number_i_subset - 1))
     160              : 
     161           24 :       vector_batch_I_size_group = 0
     162           72 :       DO i = 0, number_i_subset - 1
     163           72 :          vector_batch_I_size_group(i) = total_I_size_batch_group
     164              :       END DO
     165           72 :       IF (SUM(vector_batch_I_size_group) /= occ_i) THEN
     166           54 :          one = 1
     167           54 :          IF (SUM(vector_batch_I_size_group) > occ_i) one = -1
     168           18 :          i = -1
     169              :          DO
     170           18 :             i = i + 1
     171           18 :             vector_batch_I_size_group(i) = vector_batch_I_size_group(i) + one
     172           54 :             IF (SUM(vector_batch_I_size_group) == occ_i) EXIT
     173           18 :             IF (i == number_i_subset - 1) i = -1
     174              :          END DO
     175              :       END IF
     176              : 
     177           24 :       total_J_size_batch_group = occ_j/number_j_subset
     178           24 :       IF (total_J_size_batch_group < 1) total_J_size_batch_group = 1
     179           72 :       ALLOCATE (vector_batch_J_size_group(0:number_j_subset - 1))
     180              : 
     181           24 :       vector_batch_J_size_group = 0
     182           48 :       DO i = 0, number_J_subset - 1
     183           48 :          vector_batch_J_size_group(i) = total_J_size_batch_group
     184              :       END DO
     185           48 :       IF (SUM(vector_batch_J_size_group) /= occ_j) THEN
     186            0 :          one = 1
     187            0 :          IF (SUM(vector_batch_J_size_group) > occ_j) one = -1
     188            0 :          i = -1
     189              :          DO
     190            0 :             i = i + 1
     191            0 :             vector_batch_J_size_group(i) = vector_batch_J_size_group(i) + one
     192            0 :             IF (SUM(vector_batch_J_size_group) == occ_j) EXIT
     193            0 :             IF (i == number_J_subset - 1) i = -1
     194              :          END DO
     195              :       END IF
     196              : 
     197              :       ! now the starting and ending I and J occupied orbitals are assigned to each group
     198           24 :       group_counter = 0
     199           24 :       i_group_counter = 0
     200           24 :       my_I_occupied_start = 1
     201           36 :       DO i = 0, number_i_subset - 1
     202              :          my_J_occupied_start = 1
     203              :          j_group_counter = 0
     204           48 :          DO j = 0, number_j_subset - 1
     205           36 :             group_counter = group_counter + 1
     206           36 :             IF (color_sub == group_counter - 1) EXIT
     207           12 :             my_J_occupied_start = my_J_occupied_start + vector_batch_J_size_group(j)
     208           48 :             j_group_counter = j_group_counter + 1
     209              :          END DO
     210           36 :          IF (color_sub == group_counter - 1) EXIT
     211           12 :          my_I_occupied_start = my_I_occupied_start + vector_batch_I_size_group(i)
     212           36 :          i_group_counter = i_group_counter + 1
     213              :       END DO
     214           24 :       my_I_occupied_end = my_I_occupied_start + vector_batch_I_size_group(i_group_counter) - 1
     215           24 :       my_I_batch_size = vector_batch_I_size_group(i_group_counter)
     216           24 :       my_J_occupied_end = my_J_occupied_start + vector_batch_J_size_group(j_group_counter) - 1
     217           24 :       my_J_batch_size = vector_batch_J_size_group(j_group_counter)
     218              : 
     219           24 :       DEALLOCATE (vector_batch_I_size_group)
     220           24 :       DEALLOCATE (vector_batch_J_size_group)
     221              : 
     222              :       max_batch_size = MIN( &
     223              :                        MAX(1, &
     224              :                            INT(mp2_env%mp2_memory*INT(1024, KIND=int_8)**2/ &
     225              :                                (8*(2*dimen - occ_i)*INT(dimen, KIND=int_8)*my_J_batch_size/para_env_sub%num_pe))) &
     226           24 :                        , my_I_batch_size)
     227           24 :       IF (max_batch_size < 1) THEN
     228            1 :          max_batch_size = INT((8*(occ_i + 1)*INT(dimen, KIND=int_8)**2/para_env%num_pe)/1024**2)
     229            1 :          IF (unit_nr > 0) WRITE (unit_nr, '(T3,A,T72,I6,A3)') 'More memory required, at least:', max_batch_size, 'MiB'
     230            1 :          max_batch_size = 1
     231              :       END IF
     232              : 
     233              :       ! create the size of the batches inside the group
     234           24 :       my_batch_size = my_I_batch_size
     235           71 :       ALLOCATE (batch_sizes(my_batch_size))
     236              : 
     237           73 :       batch_sizes = -HUGE(0)
     238              :       batch_number = 0
     239           47 :       DO i = 1, my_batch_size
     240           41 :          IF (i*max_batch_size > my_batch_size) EXIT
     241           23 :          batch_number = batch_number + 1
     242           47 :          batch_sizes(i) = max_batch_size
     243              :       END DO
     244           24 :       last_batch = my_batch_size - max_batch_size*batch_number
     245           24 :       IF (last_batch > 0) THEN
     246            0 :          batch_number = batch_number + 1
     247            0 :          batch_sizes(batch_number) = last_batch
     248              :       END IF
     249              : 
     250           71 :       ALLOCATE (batch_sizes_tmp(batch_number))
     251           47 :       batch_sizes_tmp(1:batch_number) = batch_sizes(1:batch_number)
     252           24 :       DEALLOCATE (batch_sizes)
     253           47 :       ALLOCATE (batch_sizes(batch_number))
     254           47 :       batch_sizes(:) = batch_sizes_tmp
     255           24 :       DEALLOCATE (batch_sizes_tmp)
     256              : 
     257           47 :       max_batch_size = MAXVAL(batch_sizes)
     258           24 :       CALL para_env%max(max_batch_size)
     259           24 :       max_batch_number = batch_number
     260           24 :       CALL para_env%max(max_batch_number)
     261           24 :       IF (unit_nr > 0) THEN
     262           12 :          WRITE (unit_nr, '(T3,A,T76,I5)') 'Maximum used batch size: ', max_batch_size
     263           12 :          WRITE (unit_nr, '(T3,A,T76,I5)') 'Number of integral recomputations: ', max_batch_number
     264           12 :          CALL m_flush(unit_nr)
     265              :       END IF
     266              : 
     267              :       ! Batches sizes exceed the occupied orbitals allocated for group
     268           47 :       CPASSERT(SUM(batch_sizes) <= my_batch_size)
     269              : 
     270           24 :       virt_i = dimen - occ_i
     271           24 :       virt_j = dimen - occ_j
     272           24 :       natom = SIZE(mp2_biel%index_table, 1)
     273              : 
     274           24 :       CALL para_env%sync()
     275           24 :       Emp2 = zero
     276           24 :       Emp2_Cou = zero
     277           24 :       Emp2_ex = zero
     278           24 :       i_batch_start = my_I_occupied_start - 1
     279           24 :       j_batch_start = my_J_occupied_start - 1
     280           24 :       Nj_occupied = my_J_batch_size
     281           47 :       DO i_batch = 1, batch_number
     282              : 
     283           23 :          Ni_occupied = batch_sizes(i_batch)
     284              : 
     285           23 :          counter = -1
     286           92 :          ALLOCATE (ij_matrix(Ni_occupied, Nj_occupied))
     287              : 
     288           23 :          ij_matrix = 0
     289           72 :          DO i = 1, Ni_occupied
     290          290 :             DO j = 1, Nj_occupied
     291          218 :                counter = counter + 1
     292          267 :                IF (MOD(counter, para_env_sub%num_pe) == para_env_sub%mepos) THEN
     293          218 :                   ij_matrix(i, j) = ij_matrix(i, j) + 1
     294              :                END IF
     295              :             END DO
     296              :          END DO
     297              : 
     298           69 :          ALLOCATE (ij_list_proc_temp(Ni_occupied*occ_j, 2))
     299              : 
     300           23 :          elements_ij_proc = 0
     301           72 :          DO i = 1, Ni_occupied
     302          290 :             DO j = 1, Nj_occupied
     303          218 :                IF (ij_matrix(i, j) == 0) CYCLE
     304          218 :                elements_ij_proc = elements_ij_proc + 1
     305          218 :                ij_list_proc_temp(elements_ij_proc, 1) = i
     306          267 :                ij_list_proc_temp(elements_ij_proc, 2) = j
     307              :             END DO
     308              :          END DO
     309           23 :          DEALLOCATE (ij_matrix)
     310              : 
     311           69 :          ALLOCATE (ij_list_proc(elements_ij_proc, 2))
     312          241 :          DO i = 1, elements_ij_proc
     313          218 :             ij_list_proc(i, 1) = ij_list_proc_temp(i, 1)
     314          241 :             ij_list_proc(i, 2) = ij_list_proc_temp(i, 2)
     315              :          END DO
     316           23 :          DEALLOCATE (ij_list_proc_temp)
     317              : 
     318           23 :          IF (.NOT. alpha_beta_case) THEN
     319              :             CALL mp2_canonical_direct_single_batch(Emp2, Emp2_Cou, Emp2_ex, mp2_env, qs_env, para_env_sub, &
     320              :                                                    mp2_biel, dimen, C_i, Auto_i, i_batch_start, Ni_occupied, occ_i, &
     321           19 :                                                    elements_ij_proc, ij_list_proc, Nj_occupied, j_batch_start)
     322              :          ELSE
     323              :             CALL mp2_canonical_direct_single_batch(Emp2, Emp2_Cou, Emp2_ex, mp2_env, qs_env, para_env_sub, &
     324              :                                                    mp2_biel, dimen, C_i, Auto_i, i_batch_start, Ni_occupied, occ_i, &
     325              :                                                    elements_ij_proc, ij_list_proc, Nj_occupied, j_batch_start, &
     326            4 :                                                    occ_j, C_j, Auto_j)
     327              :          END IF
     328              : 
     329           23 :          i_batch_start = i_batch_start + Ni_occupied
     330              : 
     331           47 :          DEALLOCATE (ij_list_proc)
     332              : 
     333              :       END DO
     334              : 
     335           24 :       CALL para_env%sum(Emp2_Cou)
     336           24 :       CALL para_env%sum(Emp2_Ex)
     337           24 :       CALL para_env%sum(Emp2)
     338              : 
     339           24 :       CALL mp_para_env_release(para_env_sub)
     340              : 
     341           24 :       CALL timestop(handle)
     342              : 
     343           48 :    END SUBROUTINE mp2_direct_energy
     344              : 
     345              : ! **************************************************************************************************
     346              : !> \brief ...
     347              : !> \param Emp2 ...
     348              : !> \param Emp2_Cou ...
     349              : !> \param Emp2_ex ...
     350              : !> \param mp2_env ...
     351              : !> \param qs_env ...
     352              : !> \param para_env ...
     353              : !> \param mp2_biel ...
     354              : !> \param dimen ...
     355              : !> \param C ...
     356              : !> \param Auto ...
     357              : !> \param i_batch_start ...
     358              : !> \param Ni_occupied ...
     359              : !> \param occupied ...
     360              : !> \param elements_ij_proc ...
     361              : !> \param ij_list_proc ...
     362              : !> \param Nj_occupied ...
     363              : !> \param j_batch_start ...
     364              : !> \param occupied_beta ...
     365              : !> \param C_beta ...
     366              : !> \param Auto_beta ...
     367              : !> \param Integ_MP2 ...
     368              : !> \par History
     369              : !>      06.2011 created [Mauro Del Ben]
     370              : !> \author Mauro Del Ben
     371              : ! **************************************************************************************************
     372           41 :    SUBROUTINE mp2_canonical_direct_single_batch(Emp2, Emp2_Cou, Emp2_ex, mp2_env, qs_env, para_env, &
     373           82 :                                                 mp2_biel, dimen, C, Auto, i_batch_start, Ni_occupied, &
     374           41 :                                                 occupied, elements_ij_proc, ij_list_proc, Nj_occupied, j_batch_start, &
     375           10 :                                                 occupied_beta, C_beta, Auto_beta, Integ_MP2)
     376              : 
     377              :       REAL(KIND=dp), INTENT(INOUT)                       :: Emp2, Emp2_Cou, Emp2_ex
     378              :       TYPE(mp2_type)                                     :: mp2_env
     379              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     380              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
     381              :       TYPE(mp2_biel_type), INTENT(IN)                    :: mp2_biel
     382              :       INTEGER, INTENT(IN)                                :: dimen
     383              :       REAL(KIND=dp), DIMENSION(dimen, dimen), INTENT(IN) :: C
     384              :       REAL(KIND=dp), DIMENSION(dimen), INTENT(IN)        :: Auto
     385              :       INTEGER, INTENT(IN)                                :: i_batch_start, Ni_occupied, occupied, &
     386              :                                                             elements_ij_proc
     387              :       INTEGER, DIMENSION(elements_ij_proc, 2), &
     388              :          INTENT(IN)                                      :: ij_list_proc
     389              :       INTEGER, INTENT(IN)                                :: Nj_occupied, j_batch_start
     390              :       INTEGER, INTENT(IN), OPTIONAL                      :: occupied_beta
     391              :       REAL(KIND=dp), DIMENSION(dimen, dimen), &
     392              :          INTENT(IN), OPTIONAL                            :: C_beta
     393              :       REAL(KIND=dp), DIMENSION(dimen), INTENT(IN), &
     394              :          OPTIONAL                                        :: Auto_beta
     395              :       REAL(KIND=dp), ALLOCATABLE, &
     396              :          DIMENSION(:, :, :, :), INTENT(OUT), OPTIONAL    :: Integ_MP2
     397              : 
     398              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'mp2_canonical_direct_single_batch'
     399              : 
     400              :       INTEGER :: case_index, counter_proc, elements_ij_proc_rec, elements_kl_proc, global_counter, &
     401              :          handle, i, i_list_ij, i_list_kl, i_set_list_ij, i_set_list_ij_start, i_set_list_ij_stop, &
     402              :          i_set_list_kl, i_set_list_kl_start, i_set_list_kl_stop, i_start, iatom, iatom_end, &
     403              :          iatom_start, iiB, ij_elem_max, ikind, index_ij_rec, index_ij_send, index_kl, &
     404              :          index_proc_ij, index_proc_shift, iset, jatom, jatom_end, jatom_start, jjB, jkind, jset, &
     405              :          katom, katom_end, katom_start, kkB, kkind, kset, latom, latom_end, latom_start, lkind, &
     406              :          llB, lset, max_num_call_sec_transf, max_pgf, max_set, multiple
     407              :       INTEGER :: my_num_call_sec_transf, natom, ncob, nints, nseta, nsetb, nsgf_max, nspins, &
     408              :          primitive_counter, proc_num, proc_receive, proc_send, R_offset_rec, Rsize_rec, &
     409              :          S_offset_rec, same_size_kl_index, Ssize_rec, step_size, total_num_RS_task
     410              :       INTEGER(int_8)                                     :: estimate_to_store_int, neris_tmp, &
     411              :                                                             neris_total, nprim_ints
     412           41 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of, nimages, proc_num_task, &
     413           41 :                                                             same_size_kl_elements_counter
     414           41 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: kl_list_proc, task_counter_RS, &
     415           41 :                                                             task_counter_RS_temp
     416              :       INTEGER, DIMENSION(4)                              :: RS_counter_temp
     417              :       INTEGER, DIMENSION(5)                              :: size_parameter_rec, size_parameter_send
     418           41 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, lc_max, &
     419           41 :                                                             lc_min, ld_max, ld_min, npgfa, npgfb, &
     420           41 :                                                             npgfc, npgfd, nsgfa, nsgfb, nsgfc, &
     421           41 :                                                             nsgfd
     422              :       LOGICAL                                            :: alpha_beta_case, case_send_receive, &
     423              :                                                             copy_integrals, do_periodic
     424              :       LOGICAL, DIMENSION(:, :), POINTER                  :: shm_atomic_pair_list
     425              :       REAL(KIND=dp) :: cartesian_estimate, coeffs_kind_max0, cost_tmp, eps_schwarz, ln_10, &
     426              :          log10_eps_schwarz, log10_pmax, max_contraction_val, max_val1, max_val2, max_val2_set, &
     427              :          pmax_atom, pmax_entry, ra(3), rab2, rb(3), rc(3), rcd2, rd(3), screen_kind_ij, &
     428              :          screen_kind_kl
     429           41 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: cost_RS, cost_RS_temp, ee_buffer1, &
     430           41 :                                                             ee_buffer2, ee_work2, &
     431           41 :                                                             primitive_integrals
     432           41 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: BIb_RS_mat_rec, C_beta_T, max_contraction
     433           41 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: BIb, BIb_RS_mat_rec_big, zero_mat_big
     434           41 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :)  :: BI1, MNRS
     435           41 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: shm_pmax_block, zeta, zetb, zetc, zetd
     436           82 :       REAL(KIND=dp), DIMENSION(dimen, 2)                 :: zero_mat
     437           82 :       REAL(KIND=dp), DIMENSION(dimen, dimen)             :: C_T
     438              :       TYPE(cell_type), POINTER                           :: cell
     439              :       TYPE(cp_logger_type), POINTER                      :: logger
     440           41 :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
     441           41 :       TYPE(hfx_pgf_list), ALLOCATABLE, DIMENSION(:)      :: pgf_list_ij, pgf_list_kl
     442              :       TYPE(hfx_potential_type)                           :: mp2_potential_parameter
     443              :       TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
     444           41 :          POINTER                                         :: screen_coeffs_kind, tmp_R_1, tmp_R_2, &
     445           41 :                                                             tmp_screen_pgf1, tmp_screen_pgf2
     446              :       TYPE(hfx_screen_coeff_type), &
     447           41 :          DIMENSION(:, :, :, :), POINTER                  :: screen_coeffs_set
     448              :       TYPE(hfx_screen_coeff_type), &
     449           41 :          DIMENSION(:, :, :, :, :, :), POINTER            :: radii_pgf, screen_coeffs_pgf
     450              :       TYPE(hfx_type), POINTER                            :: actual_x_data
     451           41 :       TYPE(pair_list_type_mp2)                           :: list_ij, list_kl
     452              :       TYPE(pair_set_list_type), ALLOCATABLE, &
     453           41 :          DIMENSION(:)                                    :: set_list_ij, set_list_kl
     454           41 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     455              : 
     456           41 :       CALL timeset(routineN, handle)
     457              : 
     458              :       ! The Integ_MP2 will contain the (ia|jb) integrals, necessary for example
     459              :       ! for the RI-MP2 basis optimization. In this case the number of ij batches
     460              :       ! has to be equal to 1 (all integrals over molecular orbitals are computed
     461              :       ! in a single step).
     462           41 :       copy_integrals = .FALSE.
     463           41 :       IF (PRESENT(Integ_MP2)) copy_integrals = .TRUE.
     464              : 
     465           41 :       alpha_beta_case = .FALSE.
     466              : 
     467              :       CALL prepare_integral_calc(cell, qs_env, mp2_env, para_env, mp2_potential_parameter, actual_x_data, &
     468              :                                  do_periodic, basis_parameter, max_set, particle_set, natom, kind_of, &
     469              :                                  nsgf_max, primitive_integrals, ee_work2, ee_buffer1, ee_buffer2, &
     470              :                                  nspins, max_contraction, max_pgf, pgf_list_ij, &
     471              :                                  pgf_list_kl, nimages, eps_schwarz, log10_eps_schwarz, &
     472              :                                  screen_coeffs_set, screen_coeffs_kind, screen_coeffs_pgf, &
     473           41 :                                  radii_pgf)
     474              : 
     475           41 :       ln_10 = LOG(10.0_dp)
     476              : 
     477           41 :       neris_tmp = 0_int_8
     478           41 :       neris_total = 0_int_8
     479           41 :       nprim_ints = 0_int_8
     480              : 
     481              : !!!!!!!!!
     482          842 :       ALLOCATE (list_ij%elements(natom**2))
     483          801 :       ALLOCATE (list_kl%elements(natom**2))
     484              : !!!!!!!!!
     485              : 
     486          195 :       coeffs_kind_max0 = MAXVAL(screen_coeffs_kind(:, :)%x(2))
     487        13828 :       ALLOCATE (set_list_ij((max_set*natom)**2))
     488        13787 :       ALLOCATE (set_list_kl((max_set*natom)**2))
     489              : 
     490              :       !! precalculate maximum density matrix elements in blocks
     491          351 :       actual_x_data%pmax_block = 0.0_dp
     492           41 :       shm_pmax_block => actual_x_data%pmax_block
     493              : 
     494           41 :       shm_atomic_pair_list => actual_x_data%atomic_pair_list
     495              : 
     496           41 :       iatom_start = 1
     497           41 :       iatom_end = natom
     498           41 :       jatom_start = 1
     499           41 :       jatom_end = natom
     500           41 :       katom_start = 1
     501           41 :       katom_end = natom
     502           41 :       latom_start = 1
     503           41 :       latom_end = natom
     504              : 
     505              :       CALL build_pair_list_mp2(natom, list_ij, set_list_ij, iatom_start, iatom_end, &
     506              :                                jatom_start, jatom_end, &
     507              :                                kind_of, basis_parameter, particle_set, &
     508              :                                do_periodic, screen_coeffs_set, screen_coeffs_kind, &
     509              :                                coeffs_kind_max0, log10_eps_schwarz, cell, 0.D+00, &
     510           41 :                                shm_atomic_pair_list)
     511              : 
     512              :       CALL build_pair_list_mp2(natom, list_kl, set_list_kl, katom_start, katom_end, &
     513              :                                latom_start, latom_end, &
     514              :                                kind_of, basis_parameter, particle_set, &
     515              :                                do_periodic, screen_coeffs_set, screen_coeffs_kind, &
     516              :                                coeffs_kind_max0, log10_eps_schwarz, cell, 0.D+00, &
     517           41 :                                shm_atomic_pair_list)
     518              : 
     519          205 :       ALLOCATE (BIb(dimen, dimen, elements_ij_proc))
     520           41 :       BIb = 0.0D+00
     521        47081 :       C_T = TRANSPOSE(C)
     522              : 
     523           41 :       IF (PRESENT(occupied_beta) .AND. PRESENT(C_beta) .AND. PRESENT(Auto_beta)) THEN
     524           40 :          ALLOCATE (C_beta_T(dimen, dimen))
     525         9322 :          C_beta_T(:, :) = TRANSPOSE(C_beta)
     526              :          alpha_beta_case = .TRUE.
     527              :       END IF
     528              : 
     529           41 :       ij_elem_max = elements_ij_proc
     530           41 :       CALL para_env%max(ij_elem_max)
     531              : 
     532              :       ! calculate the minimum multiple of num_pe >= to Ni_occupied*occupied, in such a way
     533              :       ! that the i, j loop is performed exactly the same number of time for each procewssor
     534           41 :       multiple = 0
     535              :       DO
     536          302 :          multiple = multiple + para_env%num_pe
     537          302 :          IF (multiple >= Ni_occupied*Nj_occupied) EXIT
     538              :       END DO
     539              : 
     540              :       ! proc_num_task contains the number of times the second occupied
     541              :       ! orbital transformation is called for each processor, needs for balancing
     542              :       ! the point to point send
     543          123 :       ALLOCATE (proc_num_task(0:para_env%num_pe - 1))
     544              : 
     545           41 :       proc_num_task = 0
     546              : 
     547           41 :       counter_proc = 0
     548          196 :       DO i_list_ij = 1, list_ij%n_element
     549          155 :          iatom = list_ij%elements(i_list_ij)%pair(1)
     550          155 :          jatom = list_ij%elements(i_list_ij)%pair(2)
     551          155 :          i_set_list_ij_start = list_ij%elements(i_list_ij)%set_bounds(1)
     552          155 :          i_set_list_ij_stop = list_ij%elements(i_list_ij)%set_bounds(2)
     553          155 :          ikind = list_ij%elements(i_list_ij)%kind_pair(1)
     554          155 :          jkind = list_ij%elements(i_list_ij)%kind_pair(2)
     555              : 
     556          155 :          nsgfb => basis_parameter(jkind)%nsgf
     557          155 :          nsgfa => basis_parameter(ikind)%nsgf
     558              : 
     559         5395 :          DO i_set_list_ij = i_set_list_ij_start, i_set_list_ij_stop
     560         5199 :             iset = set_list_ij(i_set_list_ij)%pair(1)
     561         5199 :             jset = set_list_ij(i_set_list_ij)%pair(2)
     562         5199 :             IF (iatom == jatom .AND. jset < iset) CYCLE
     563              : 
     564         4041 :             counter_proc = counter_proc + 1
     565         4041 :             proc_num = MOD(counter_proc, para_env%num_pe)
     566              : 
     567         5354 :             proc_num_task(proc_num) = proc_num_task(proc_num) + 1
     568              : 
     569              :          END DO
     570              :       END DO
     571              :       ! calculate the exact maximum number of calls to the second occupied
     572              :       ! orbital transformation
     573              :       ! max_num_call_sec_transf=MAXVAL(proc_num_task)
     574              : 
     575              :       ! distribute the RS pair over all processor
     576          123 :       ALLOCATE (kl_list_proc(proc_num_task(para_env%mepos), 3))
     577              : 
     578           41 :       kl_list_proc = 0
     579              : 
     580           41 :       counter_proc = 0
     581           41 :       elements_kl_proc = 0
     582          196 :       DO i_list_ij = 1, list_ij%n_element
     583          155 :          iatom = list_ij%elements(i_list_ij)%pair(1)
     584          155 :          jatom = list_ij%elements(i_list_ij)%pair(2)
     585          155 :          i_set_list_ij_start = list_ij%elements(i_list_ij)%set_bounds(1)
     586          155 :          i_set_list_ij_stop = list_ij%elements(i_list_ij)%set_bounds(2)
     587          155 :          ikind = list_ij%elements(i_list_ij)%kind_pair(1)
     588          155 :          jkind = list_ij%elements(i_list_ij)%kind_pair(2)
     589              : 
     590          155 :          nsgfb => basis_parameter(jkind)%nsgf
     591          155 :          nsgfa => basis_parameter(ikind)%nsgf
     592              : 
     593         5395 :          DO i_set_list_ij = i_set_list_ij_start, i_set_list_ij_stop
     594         5199 :             iset = set_list_ij(i_set_list_ij)%pair(1)
     595         5199 :             jset = set_list_ij(i_set_list_ij)%pair(2)
     596         5199 :             IF (iatom == jatom .AND. jset < iset) CYCLE
     597              : 
     598         4041 :             counter_proc = counter_proc + 1
     599         4041 :             proc_num = MOD(counter_proc, para_env%num_pe)
     600              : 
     601         4196 :             IF (proc_num == para_env%mepos) THEN
     602         3987 :                elements_kl_proc = elements_kl_proc + 1
     603         3987 :                kl_list_proc(elements_kl_proc, 1) = i_list_ij
     604         3987 :                kl_list_proc(elements_kl_proc, 2) = i_set_list_ij
     605         3987 :                kl_list_proc(elements_kl_proc, 3) = counter_proc
     606              :             END IF
     607              : 
     608              :          END DO
     609              :       END DO
     610              : 
     611          100 :       total_num_RS_task = SUM(proc_num_task)
     612          123 :       ALLOCATE (task_counter_RS(total_num_RS_task, 4))
     613              : 
     614          123 :       ALLOCATE (cost_RS(total_num_RS_task))
     615              : 
     616           41 :       task_counter_RS = 0
     617           41 :       cost_RS = 0.0_dp
     618              : 
     619          123 :       DO case_index = 1, 2
     620              : 
     621              :          my_num_call_sec_transf = 0
     622         8056 :          DO index_kl = 1, elements_kl_proc
     623              : 
     624         7974 :             i_list_ij = kl_list_proc(index_kl, 1)
     625         7974 :             i_set_list_ij = kl_list_proc(index_kl, 2)
     626              : 
     627         7974 :             iatom = list_ij%elements(i_list_ij)%pair(1)
     628         7974 :             jatom = list_ij%elements(i_list_ij)%pair(2)
     629         7974 :             i_set_list_ij_start = list_ij%elements(i_list_ij)%set_bounds(1)
     630         7974 :             i_set_list_ij_stop = list_ij%elements(i_list_ij)%set_bounds(2)
     631         7974 :             ikind = list_ij%elements(i_list_ij)%kind_pair(1)
     632         7974 :             jkind = list_ij%elements(i_list_ij)%kind_pair(2)
     633        31896 :             ra = list_ij%elements(i_list_ij)%r1
     634        31896 :             rb = list_ij%elements(i_list_ij)%r2
     635         7974 :             rab2 = list_ij%elements(i_list_ij)%dist2
     636              : 
     637         7974 :             la_max => basis_parameter(ikind)%lmax
     638         7974 :             la_min => basis_parameter(ikind)%lmin
     639         7974 :             npgfa => basis_parameter(ikind)%npgf
     640         7974 :             nseta = basis_parameter(ikind)%nset
     641         7974 :             zeta => basis_parameter(ikind)%zet
     642         7974 :             nsgfa => basis_parameter(ikind)%nsgf
     643              : 
     644         7974 :             lb_max => basis_parameter(jkind)%lmax
     645         7974 :             lb_min => basis_parameter(jkind)%lmin
     646         7974 :             npgfb => basis_parameter(jkind)%npgf
     647         7974 :             nsetb = basis_parameter(jkind)%nset
     648         7974 :             zetb => basis_parameter(jkind)%zet
     649         7974 :             nsgfb => basis_parameter(jkind)%nsgf
     650              : 
     651         7974 :             iset = set_list_ij(i_set_list_ij)%pair(1)
     652         7974 :             jset = set_list_ij(i_set_list_ij)%pair(2)
     653              : 
     654         7974 :             ncob = npgfb(jset)*ncoset(lb_max(jset))
     655              :             max_val1 = screen_coeffs_set(jset, iset, jkind, ikind)%x(1)*rab2 + &
     656         7974 :                        screen_coeffs_set(jset, iset, jkind, ikind)%x(2)
     657              : 
     658         7974 :             IF (case_index == 1) THEN
     659         3987 :                global_counter = kl_list_proc(index_kl, 3)
     660         3987 :                task_counter_RS(global_counter, 1) = i_list_ij
     661         3987 :                task_counter_RS(global_counter, 2) = i_set_list_ij
     662         3987 :                task_counter_RS(global_counter, 3) = nsgfb(jset)*nsgfa(iset)
     663              :             END IF
     664              : 
     665         7974 :             IF (ALLOCATED(BI1)) DEALLOCATE (BI1)
     666        47844 :             ALLOCATE (BI1(dimen, Ni_occupied, nsgfb(jset), nsgfa(iset)))
     667              : 
     668         7974 :             BI1 = 0.D+00
     669              : 
     670        66276 :             DO i_list_kl = 1, list_kl%n_element
     671              : 
     672        58302 :                katom = list_kl%elements(i_list_kl)%pair(1)
     673        58302 :                latom = list_kl%elements(i_list_kl)%pair(2)
     674              : 
     675        58302 :                i_set_list_kl_start = list_kl%elements(i_list_kl)%set_bounds(1)
     676        58302 :                i_set_list_kl_stop = list_kl%elements(i_list_kl)%set_bounds(2)
     677        58302 :                kkind = list_kl%elements(i_list_kl)%kind_pair(1)
     678        58302 :                lkind = list_kl%elements(i_list_kl)%kind_pair(2)
     679       233208 :                rc = list_kl%elements(i_list_kl)%r1
     680       233208 :                rd = list_kl%elements(i_list_kl)%r2
     681        58302 :                rcd2 = list_kl%elements(i_list_kl)%dist2
     682              : 
     683        58302 :                pmax_atom = 0.0_dp
     684              : 
     685              :                screen_kind_ij = screen_coeffs_kind(jkind, ikind)%x(1)*rab2 + &
     686        58302 :                                 screen_coeffs_kind(jkind, ikind)%x(2)
     687              :                screen_kind_kl = screen_coeffs_kind(lkind, kkind)%x(1)*rcd2 + &
     688        58302 :                                 screen_coeffs_kind(lkind, kkind)%x(2)
     689              : 
     690              :                !!!!! Change the loop order
     691        58302 :                IF (max_val1 + screen_kind_kl + pmax_atom < log10_eps_schwarz) CYCLE
     692              :                !!!!!
     693        58302 :                IF (screen_kind_ij + screen_kind_kl + pmax_atom < log10_eps_schwarz) CYCLE
     694              : 
     695        58302 :                lc_max => basis_parameter(kkind)%lmax
     696        58302 :                lc_min => basis_parameter(kkind)%lmin
     697        58302 :                npgfc => basis_parameter(kkind)%npgf
     698        58302 :                zetc => basis_parameter(kkind)%zet
     699        58302 :                nsgfc => basis_parameter(kkind)%nsgf
     700              : 
     701        58302 :                ld_max => basis_parameter(lkind)%lmax
     702        58302 :                ld_min => basis_parameter(lkind)%lmin
     703        58302 :                npgfd => basis_parameter(lkind)%npgf
     704        58302 :                zetd => basis_parameter(lkind)%zet
     705        58302 :                nsgfd => basis_parameter(lkind)%nsgf
     706              : 
     707      2108682 :                DO i_set_list_kl = i_set_list_kl_start, i_set_list_kl_stop
     708      2042406 :                   kset = set_list_kl(i_set_list_kl)%pair(1)
     709      2042406 :                   lset = set_list_kl(i_set_list_kl)%pair(2)
     710              : 
     711      2042406 :                   IF (katom == latom .AND. lset < kset) CYCLE
     712              : 
     713              :                   max_val2_set = (screen_coeffs_set(lset, kset, lkind, kkind)%x(1)*rcd2 + &
     714      1636794 :                                   screen_coeffs_set(lset, kset, lkind, kkind)%x(2))
     715      1636794 :                   max_val2 = max_val1 + max_val2_set
     716              : 
     717              :                   !! Near field screening
     718      1636794 :                   IF (max_val2 + pmax_atom < log10_eps_schwarz) CYCLE
     719              :                   !! get max_vals if we screen on initial density
     720      1636794 :                   pmax_entry = 0.0_dp
     721              : 
     722      1636794 :                   log10_pmax = pmax_entry
     723      1636794 :                   max_val2 = max_val2 + log10_pmax
     724              :                   IF (max_val2 < log10_eps_schwarz) CYCLE
     725      1636794 :                   pmax_entry = EXP(log10_pmax*ln_10)
     726              : 
     727      1695096 :                   IF (case_index == 2) THEN
     728       818397 :                      IF (ALLOCATED(MNRS)) DEALLOCATE (MNRS)
     729      4910382 :                      ALLOCATE (MNRS(nsgfd(lset), nsgfc(kset), nsgfb(jset), nsgfa(iset)))
     730              : 
     731       818397 :                      MNRS = 0.D+00
     732              : 
     733              :                      max_contraction_val = max_contraction(iset, iatom)* &
     734              :                                            max_contraction(jset, jatom)* &
     735              :                                            max_contraction(kset, katom)* &
     736       818397 :                                            max_contraction(lset, latom)*pmax_entry
     737       818397 :                      tmp_R_1 => radii_pgf(:, :, jset, iset, jkind, ikind)
     738       818397 :                      tmp_R_2 => radii_pgf(:, :, lset, kset, lkind, kkind)
     739       818397 :                      tmp_screen_pgf1 => screen_coeffs_pgf(:, :, jset, iset, jkind, ikind)
     740       818397 :                      tmp_screen_pgf2 => screen_coeffs_pgf(:, :, lset, kset, lkind, kkind)
     741              : 
     742              :                      CALL coulomb4(actual_x_data%eri4_context, ra, rb, rc, rd, npgfa(iset), npgfb(jset), npgfc(kset), npgfd(lset), &
     743              :                                    la_min(iset), la_max(iset), lb_min(jset), lb_max(jset), &
     744              :                                    lc_min(kset), lc_max(kset), ld_min(lset), ld_max(lset), &
     745              :                                    nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
     746              :                                    zeta(1:npgfa(iset), iset), zetb(1:npgfb(jset), jset), &
     747              :                                    zetc(1:npgfc(kset), kset), zetd(1:npgfd(lset), lset), &
     748              :                                    primitive_integrals, &
     749              :                                    mp2_potential_parameter, &
     750              :                                    actual_x_data%neighbor_cells, screen_coeffs_set(jset, iset, jkind, ikind)%x, &
     751              :                                    screen_coeffs_set(lset, kset, lkind, kkind)%x, eps_schwarz, &
     752              :                                    max_contraction_val, cartesian_estimate, cell, neris_tmp, &
     753              :                                    log10_pmax, log10_eps_schwarz, &
     754              :                                    tmp_R_1, tmp_R_2, tmp_screen_pgf1, tmp_screen_pgf2, &
     755              :                                    pgf_list_ij, pgf_list_kl, &
     756              :                                    basis_parameter(ikind)%contraction(:, iset), basis_parameter(jkind)%contraction(:, jset), &
     757              :                                    basis_parameter(kkind)%contraction(:, kset), basis_parameter(lkind)%contraction(:, lset), &
     758              :                                    ee_work2, ee_buffer1, ee_buffer2, &
     759       818397 :                                    nimages, do_periodic)
     760              : 
     761       818397 :                      nints = nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset)
     762       818397 :                      neris_total = neris_total + nints
     763       818397 :                      nprim_ints = nprim_ints + neris_tmp
     764       818397 :                      IF (cartesian_estimate == 0.0_dp) cartesian_estimate = TINY(cartesian_estimate)
     765       818397 :                      estimate_to_store_int = EXPONENT(cartesian_estimate)
     766       818397 :                      estimate_to_store_int = MAX(estimate_to_store_int, -15_int_8)
     767       818397 :                      cartesian_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int + 1)
     768              : 
     769       818397 :                      IF (cartesian_estimate < eps_schwarz) CYCLE
     770              : 
     771              :                      primitive_counter = 0
     772      2546532 :                      DO llB = 1, nsgfd(lset)
     773      6776721 :                         DO kkB = 1, nsgfc(kset)
     774     15326382 :                            DO jjB = 1, nsgfb(jset)
     775     37558011 :                               DO iiB = 1, nsgfa(iset)
     776     23961456 :                                  primitive_counter = primitive_counter + 1
     777     33327822 :                                  MNRS(llB, kkB, jjB, iiB) = primitive_integrals(primitive_counter)
     778              :                               END DO
     779              :                            END DO
     780              :                         END DO
     781              :                      END DO
     782              : 
     783              :                      CALL transform_occupied_orbitals_first(dimen, iatom, jatom, katom, latom, &
     784              :                                                             iset, jset, kset, lset, &
     785              :                                                             nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
     786              :                                                             i_batch_start, Ni_occupied, &
     787       816705 :                                                             MNRS, C_T, mp2_biel, BI1)
     788              :                   ELSE
     789       818397 :                      task_counter_RS(global_counter, 4) = task_counter_RS(global_counter, 4) + 1
     790              : 
     791       818397 :                      cost_tmp = 0.0_dp
     792              :                      cost_tmp = cost_model(nsgfd(lset), nsgfc(kset), nsgfb(jset), nsgfa(iset), &
     793              :                                            npgfd(lset), npgfc(kset), npgfb(jset), npgfa(iset), &
     794              :                                            max_val2/log10_eps_schwarz, &
     795       818397 :                                            p1_energy, p2_energy, p3_energy)
     796       818397 :                      cost_RS(global_counter) = cost_RS(global_counter) + cost_tmp
     797              :                   END IF
     798              : 
     799              :                END DO ! i_set_list_kl
     800              :             END DO ! i_list_kl
     801              : 
     802         8056 :             IF (case_index == 2) THEN
     803         3987 :                my_num_call_sec_transf = my_num_call_sec_transf + 1
     804         3987 :                IF (.NOT. alpha_beta_case) THEN
     805         3327 :                   IF (.NOT. mp2_env%direct_canonical%big_send) THEN
     806              :                      CALL transform_occupied_orbitals_second(dimen, iatom, jatom, iset, jset, &
     807              :                                                              nsgfa(iset), nsgfb(jset), Ni_occupied, Nj_occupied, j_batch_start, &
     808              :                                                              BI1, C_T, mp2_biel, para_env, elements_ij_proc, &
     809            0 :                                                              multiple, BIb)
     810              :                   ELSE
     811              :                      CALL transform_occupied_orbitals_second_big( &
     812              :                         dimen, iatom, jatom, iset, jset, &
     813              :                         nsgfa(iset), nsgfb(jset), Ni_occupied, Nj_occupied, j_batch_start, &
     814         3327 :                         ij_elem_max, BI1, C_T, mp2_biel, para_env, elements_ij_proc, BIb)
     815              :                   END IF
     816              :                ELSE
     817          660 :                   IF (.NOT. mp2_env%direct_canonical%big_send) THEN
     818              :                      CALL transform_occupied_orbitals_second(dimen, iatom, jatom, iset, jset, &
     819              :                                                              nsgfa(iset), nsgfb(jset), Ni_occupied, Nj_occupied, j_batch_start, &
     820              :                                                              BI1, C_beta_T, mp2_biel, para_env, elements_ij_proc, &
     821            0 :                                                              multiple, BIb)
     822              :                   ELSE
     823              :                      CALL transform_occupied_orbitals_second_big( &
     824              :                         dimen, iatom, jatom, iset, jset, &
     825              :                         nsgfa(iset), nsgfb(jset), Ni_occupied, Nj_occupied, j_batch_start, &
     826          660 :                         ij_elem_max, BI1, C_beta_T, mp2_biel, para_env, elements_ij_proc, BIb)
     827              :                   END IF
     828              :                END IF
     829              :             END IF
     830              : 
     831              :          END DO !i_list_ij
     832              : 
     833          123 :          IF (case_index == 1) THEN
     834           41 :             CALL para_env%sum(task_counter_RS)
     835           41 :             CALL para_env%sum(cost_RS)
     836           82 :             ALLOCATE (task_counter_RS_temp(total_num_RS_task, 4))
     837              : 
     838           82 :             ALLOCATE (cost_RS_temp(total_num_RS_task))
     839              : 
     840           41 :             step_size = 1
     841          123 :             ALLOCATE (same_size_kl_elements_counter((nsgf_max**2 + 1)/step_size + 1))
     842              : 
     843           41 :             same_size_kl_elements_counter = 0
     844              : 
     845           41 :             same_size_kl_index = 0
     846           41 :             global_counter = 0
     847           41 :             DO iiB = nsgf_max**2 + 1, 0, -step_size
     848       115734 :                DO jjB = 1, total_num_RS_task
     849       115734 :                   IF (task_counter_RS(jjB, 3) > iiB - step_size .AND. task_counter_RS(jjB, 3) <= iiB) THEN
     850         4041 :                      global_counter = global_counter + 1
     851        20205 :                      task_counter_RS_temp(global_counter, 1:4) = task_counter_RS(jjB, 1:4)
     852         4041 :                      cost_RS_temp(global_counter) = cost_RS(jjB)
     853              :                   END IF
     854              :                END DO
     855         1227 :                same_size_kl_index = same_size_kl_index + 1
     856         1227 :                same_size_kl_elements_counter(same_size_kl_index) = global_counter
     857              :             END DO
     858              : 
     859           41 :             DEALLOCATE (task_counter_RS)
     860           41 :             DEALLOCATE (cost_RS)
     861              : 
     862           41 :             i_start = 1
     863         1268 :             DO same_size_kl_index = 1, SIZE(same_size_kl_elements_counter)
     864         5268 :                DO iiB = i_start, same_size_kl_elements_counter(same_size_kl_index)
     865        90594 :                   DO jjB = iiB + 1, same_size_kl_elements_counter(same_size_kl_index)
     866              : 
     867        89367 :                      IF (cost_RS_temp(jjB) >= cost_RS_temp(iiB)) THEN
     868       189080 :                         RS_counter_temp = task_counter_RS_temp(iiB, 1:4)
     869       189080 :                         task_counter_RS_temp(iiB, 1:4) = task_counter_RS_temp(jjB, 1:4)
     870       189080 :                         task_counter_RS_temp(jjB, 1:4) = RS_counter_temp
     871              : 
     872        37816 :                         cost_tmp = cost_RS_temp(iiB)
     873        37816 :                         cost_RS_temp(iiB) = cost_RS_temp(jjB)
     874        37816 :                         cost_RS_temp(jjB) = cost_tmp
     875              :                      END IF
     876              :                   END DO
     877              :                END DO
     878         1268 :                i_start = same_size_kl_elements_counter(same_size_kl_index) + 1
     879              :             END DO
     880              : 
     881           41 :             proc_num_task = 0
     882         4082 :             DO counter_proc = 1, total_num_RS_task
     883         4041 :                proc_num = MOD(counter_proc, para_env%num_pe)
     884         4082 :                proc_num_task(proc_num) = proc_num_task(proc_num) + 1
     885              :             END DO
     886              : 
     887          100 :             max_num_call_sec_transf = MAXVAL(proc_num_task)
     888              : 
     889           41 :             DEALLOCATE (kl_list_proc)
     890          123 :             ALLOCATE (kl_list_proc(proc_num_task(para_env%mepos), 2))
     891              : 
     892           41 :             kl_list_proc = 0
     893              : 
     894           41 :             elements_kl_proc = 0
     895         4082 :             DO counter_proc = 1, total_num_RS_task
     896         4041 :                proc_num = MOD(counter_proc, para_env%num_pe)
     897         4082 :                IF (proc_num == para_env%mepos) THEN
     898         3987 :                   elements_kl_proc = elements_kl_proc + 1
     899         3987 :                   kl_list_proc(elements_kl_proc, 1) = task_counter_RS_temp(counter_proc, 1)
     900         3987 :                   kl_list_proc(elements_kl_proc, 2) = task_counter_RS_temp(counter_proc, 2)
     901              :                END IF
     902              :             END DO
     903              : 
     904           41 :             DEALLOCATE (task_counter_RS_temp)
     905           41 :             DEALLOCATE (cost_RS_temp)
     906              :          END IF
     907              :       END DO ! case_index
     908              : 
     909           41 :       size_parameter_send(1) = 1
     910           41 :       size_parameter_send(2) = 1
     911           41 :       size_parameter_send(3) = 0
     912           41 :       size_parameter_send(4) = 0
     913           41 :       size_parameter_send(5) = elements_ij_proc
     914              : 
     915           41 :       IF (mp2_env%direct_canonical%big_send) THEN
     916          164 :          ALLOCATE (zero_mat_big(dimen, 2, ij_elem_max))
     917              : 
     918              :       END IF
     919              : 
     920           41 :       DO iiB = my_num_call_sec_transf + 1, max_num_call_sec_transf
     921           41 :          DO index_proc_shift = 0, para_env%num_pe - 1
     922              : 
     923            0 :             proc_send = MODULO(para_env%mepos + index_proc_shift, para_env%num_pe)
     924            0 :             proc_receive = MODULO(para_env%mepos - index_proc_shift, para_env%num_pe)
     925              : 
     926            0 :             case_send_receive = (proc_send /= para_env%mepos)
     927              : 
     928            0 :             IF (case_send_receive) THEN
     929              :                ! the processor starts to send (and receive?)
     930              : 
     931            0 :                CALL para_env%sendrecv(size_parameter_send, proc_send, size_parameter_rec, proc_receive)
     932              : 
     933            0 :                Rsize_rec = size_parameter_rec(1)
     934            0 :                Ssize_rec = size_parameter_rec(2)
     935            0 :                R_offset_rec = size_parameter_rec(3)
     936            0 :                S_offset_rec = size_parameter_rec(4)
     937            0 :                elements_ij_proc_rec = size_parameter_rec(5)
     938            0 :                IF (.NOT. mp2_env%direct_canonical%big_send) THEN
     939            0 :                   ALLOCATE (BIb_RS_mat_rec(dimen, Rsize_rec + Ssize_rec))
     940              :                ELSE
     941            0 :                   ALLOCATE (BIb_RS_mat_rec_big(dimen, Rsize_rec + Ssize_rec, ij_elem_max))
     942              :                END IF
     943              :             ELSE
     944            0 :                elements_ij_proc_rec = elements_ij_proc
     945              :             END IF
     946              : 
     947            0 :             IF (.NOT. mp2_env%direct_canonical%big_send) THEN
     948            0 :                index_ij_send = 0
     949            0 :                index_ij_rec = 0
     950            0 :                DO index_proc_ij = proc_send + 1, multiple, para_env%num_pe
     951              : 
     952            0 :                   zero_mat = 0.D+00
     953              : 
     954            0 :                   IF (case_send_receive) THEN
     955              : 
     956            0 :                      CALL para_env%sendrecv(zero_mat, proc_send, BIb_RS_mat_rec, proc_receive)
     957              : 
     958            0 :                      index_ij_rec = index_ij_rec + 1
     959            0 :                      IF (index_ij_rec <= elements_ij_proc .AND. elements_ij_proc > 0) THEN
     960              : 
     961              :                         BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, index_ij_rec) = &
     962              :                            BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, index_ij_rec) + &
     963            0 :                            BIb_RS_mat_rec(1:dimen, 1:Rsize_rec)
     964              : 
     965              :                         BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, index_ij_rec) = &
     966              :                            BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, index_ij_rec) + &
     967            0 :                            BIb_RS_mat_rec(1:dimen, Rsize_rec + 1:Rsize_rec + Ssize_rec)
     968              : 
     969              :                      END IF
     970              :                   END IF
     971              : 
     972              :                END DO
     973              :             ELSE
     974            0 :                zero_mat_big = 0.D+00
     975              : 
     976            0 :                IF (case_send_receive) THEN
     977              : 
     978            0 :                   CALL para_env%sendrecv(zero_mat_big, proc_send, BIb_RS_mat_rec_big, proc_receive)
     979              : 
     980              :                   BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, 1:elements_ij_proc) = &
     981              :                      BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, 1:elements_ij_proc) + &
     982            0 :                      BIb_RS_mat_rec_big(1:dimen, 1:Rsize_rec, 1:elements_ij_proc)
     983              : 
     984              :                   BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, 1:elements_ij_proc) = &
     985              :                      BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, 1:elements_ij_proc) + &
     986            0 :                      BIb_RS_mat_rec_big(1:dimen, Rsize_rec + 1:Rsize_rec + Ssize_rec, 1:elements_ij_proc)
     987              : 
     988              :                END IF
     989              :             END IF
     990              : 
     991            0 :             IF (case_send_receive) THEN
     992            0 :                IF (.NOT. mp2_env%direct_canonical%big_send) THEN
     993            0 :                   DEALLOCATE (BIb_RS_mat_rec)
     994              :                ELSE
     995            0 :                   DEALLOCATE (BIb_RS_mat_rec_big)
     996              :                END IF
     997              :             END IF
     998              : 
     999              :          END DO
    1000              :       END DO
    1001              : 
    1002           41 :       IF (mp2_env%direct_canonical%big_send) THEN
    1003           41 :          DEALLOCATE (zero_mat_big)
    1004              :       END IF
    1005              : 
    1006           41 :       logger => cp_get_default_logger()
    1007              : 
    1008           41 :       DEALLOCATE (primitive_integrals)
    1009              : 
    1010           41 :       IF (.NOT. alpha_beta_case) THEN
    1011              :          CALL transform_virtual_orbitals_and_accumulate(dimen, occupied, dimen - occupied, i_batch_start, &
    1012              :                                                         j_batch_start, BIb, C, Auto, elements_ij_proc, ij_list_proc, &
    1013           31 :                                                         nspins, Emp2, Emp2_Cou, Emp2_ex)
    1014              :       ELSE
    1015              :          CALL transform_virtual_orbitals_and_accumulate_ABcase( &
    1016              :             dimen, occupied, occupied_beta, dimen - occupied, dimen - occupied_beta, &
    1017              :             i_batch_start, j_batch_start, &
    1018              :             BIb, C, C_beta, Auto, Auto_beta, &
    1019           10 :             elements_ij_proc, ij_list_proc, Emp2, Emp2_Cou)
    1020           10 :          DEALLOCATE (C_beta_T)
    1021              :       END IF
    1022              : 
    1023           41 :       IF (copy_integrals) THEN
    1024           18 :          IF (.NOT. alpha_beta_case) THEN
    1025           72 :             ALLOCATE (Integ_MP2(dimen - occupied, dimen - occupied, occupied, occupied))
    1026           12 :             Integ_MP2 = 0.0_dp
    1027           72 :             DO i = 1, elements_ij_proc
    1028           60 :                iiB = ij_list_proc(i, 1)
    1029           60 :                jjB = ij_list_proc(i, 2)
    1030         5976 :                Integ_MP2(:, :, iiB + i_batch_start, jjB + j_batch_start) = BIb(1:dimen - occupied, 1:dimen - occupied, i)
    1031              :             END DO
    1032              :          ELSE
    1033           36 :             ALLOCATE (Integ_MP2(dimen - occupied, dimen - occupied_beta, occupied, occupied_beta))
    1034            6 :             Integ_MP2 = 0.0_dp
    1035           30 :             DO i = 1, elements_ij_proc
    1036           24 :                iiB = ij_list_proc(i, 1)
    1037           24 :                jjB = ij_list_proc(i, 2)
    1038         2670 :                Integ_MP2(:, :, iiB + i_batch_start, jjB + j_batch_start) = BIb(1:dimen - occupied, 1:dimen - occupied_beta, i)
    1039              :             END DO
    1040              :          END IF
    1041              :       END IF
    1042           41 :       DEALLOCATE (BIb)
    1043              : 
    1044           41 :       DEALLOCATE (set_list_ij, set_list_kl)
    1045              : 
    1046         1535 :       DO i = 1, max_pgf**2
    1047         1494 :          DEALLOCATE (pgf_list_ij(i)%image_list)
    1048         1535 :          DEALLOCATE (pgf_list_kl(i)%image_list)
    1049              :       END DO
    1050              : 
    1051           41 :       DEALLOCATE (pgf_list_ij)
    1052           41 :       DEALLOCATE (pgf_list_kl)
    1053              : 
    1054           41 :       DEALLOCATE (max_contraction, kind_of)
    1055              : 
    1056           41 :       DEALLOCATE (ee_work2, ee_buffer1, ee_buffer2)
    1057              : 
    1058           41 :       DEALLOCATE (nimages)
    1059              : 
    1060           41 :       CALL timestop(handle)
    1061              : 
    1062           92 :    END SUBROUTINE mp2_canonical_direct_single_batch
    1063              : 
    1064              : ! **************************************************************************************************
    1065              : !> \brief ...
    1066              : !> \param dimen ...
    1067              : !> \param latom ...
    1068              : !> \param katom ...
    1069              : !> \param jatom ...
    1070              : !> \param iatom ...
    1071              : !> \param lset ...
    1072              : !> \param kset ...
    1073              : !> \param jset ...
    1074              : !> \param iset ...
    1075              : !> \param Ssize ...
    1076              : !> \param Rsize ...
    1077              : !> \param Nsize ...
    1078              : !> \param Msize ...
    1079              : !> \param i_batch_start ...
    1080              : !> \param Ni_occupied ...
    1081              : !> \param MNRS ...
    1082              : !> \param C_T ...
    1083              : !> \param mp2_biel ...
    1084              : !> \param BI1 ...
    1085              : ! **************************************************************************************************
    1086       816705 :    SUBROUTINE transform_occupied_orbitals_first(dimen, latom, katom, jatom, iatom, &
    1087              :                                                 lset, kset, jset, iset, &
    1088              :                                                 Ssize, Rsize, Nsize, Msize, &
    1089              :                                                 i_batch_start, Ni_occupied, &
    1090       816705 :                                                 MNRS, C_T, mp2_biel, BI1)
    1091              : 
    1092              :       INTEGER, INTENT(IN)                                :: dimen, latom, katom, jatom, iatom, lset, &
    1093              :                                                             kset, jset, iset, Ssize, Rsize, Nsize, &
    1094              :                                                             Msize, i_batch_start, Ni_occupied
    1095              :       REAL(KIND=dp), &
    1096              :          DIMENSION(Msize, Nsize, Rsize, Ssize), &
    1097              :          INTENT(IN)                                      :: MNRS
    1098              :       REAL(KIND=dp), DIMENSION(dimen, dimen), INTENT(IN) :: C_T
    1099              :       TYPE(mp2_biel_type), INTENT(IN)                    :: mp2_biel
    1100              :       REAL(KIND=dp), &
    1101              :          DIMENSION(dimen, Ni_occupied, Rsize, Ssize), &
    1102              :          INTENT(INOUT)                                   :: BI1
    1103              : 
    1104              :       INTEGER                                            :: i, i_global, m, M_global, M_offset, &
    1105              :                                                             M_start, n, N_global, N_offset, r, &
    1106              :                                                             R_offset, R_start, s, S_offset
    1107              :       REAL(KIND=dp)                                      :: MNRS_element
    1108              : 
    1109       816705 :       N_offset = mp2_biel%index_table(jatom, jset) - 1
    1110       816705 :       M_offset = mp2_biel%index_table(iatom, iset) - 1
    1111       816705 :       S_offset = mp2_biel%index_table(latom, lset) - 1
    1112       816705 :       R_offset = mp2_biel%index_table(katom, kset) - 1
    1113              : 
    1114      2754306 :       DO S = 1, Ssize
    1115      1937601 :          R_start = 1
    1116      1937601 :          IF (katom == latom .AND. kset == lset) R_start = S
    1117      6801804 :          DO R = R_start, Rsize
    1118              : 
    1119              :             ! fast i don't know why
    1120     15780924 :             DO N = 1, Nsize
    1121      9795825 :                N_global = N + N_offset
    1122      9795825 :                M_start = 1
    1123      9795825 :                IF (iatom == jatom .AND. iset == jset) THEN
    1124       962415 :                   M = N
    1125       962415 :                   M_global = M + M_offset
    1126       962415 :                   MNRS_element = MNRS(M, N, R, S)
    1127      3085425 :                   DO i = 1, Ni_occupied
    1128      2123010 :                      i_global = i + i_batch_start
    1129      3085425 :                      BI1(N_global, i, R, S) = BI1(N_global, i, R, S) + C_T(i_global, M_global)*MNRS_element
    1130              :                   END DO
    1131       962415 :                   M_start = N + 1
    1132              :                END IF
    1133              : 
    1134     34567551 :                DO M = M_start, Msize
    1135     20724228 :                   M_global = M + M_offset
    1136     20724228 :                   MNRS_element = MNRS(M, N, R, S)
    1137     76487133 :                   DO i = 1, Ni_occupied
    1138     45967080 :                      i_global = i + i_batch_start
    1139     45967080 :                      BI1(N_global, i, R, S) = BI1(N_global, i, R, S) + C_T(i_global, M_global)*MNRS_element
    1140     66691308 :                      BI1(M_global, i, R, S) = BI1(M_global, i, R, S) + C_T(i_global, N_global)*MNRS_element
    1141              :                   END DO
    1142              :                END DO
    1143              :             END DO
    1144              : 
    1145              :          END DO
    1146              :       END DO
    1147              : 
    1148       816705 :    END SUBROUTINE transform_occupied_orbitals_first
    1149              : 
    1150              : ! **************************************************************************************************
    1151              : !> \brief ...
    1152              : !> \param dimen ...
    1153              : !> \param latom ...
    1154              : !> \param katom ...
    1155              : !> \param lset ...
    1156              : !> \param kset ...
    1157              : !> \param Ssize ...
    1158              : !> \param Rsize ...
    1159              : !> \param Ni_occupied ...
    1160              : !> \param Nj_occupied ...
    1161              : !> \param j_batch_start ...
    1162              : !> \param BI1 ...
    1163              : !> \param C_T ...
    1164              : !> \param mp2_biel ...
    1165              : !> \param para_env ...
    1166              : !> \param elements_ij_proc ...
    1167              : !> \param multiple ...
    1168              : !> \param BIb ...
    1169              : ! **************************************************************************************************
    1170            0 :    SUBROUTINE transform_occupied_orbitals_second(dimen, latom, katom, lset, kset, &
    1171              :                                                  Ssize, Rsize, Ni_occupied, Nj_occupied, j_batch_start, &
    1172            0 :                                                  BI1, C_T, mp2_biel, para_env, &
    1173              :                                                  elements_ij_proc, &
    1174            0 :                                                  multiple, BIb)
    1175              : 
    1176              :       INTEGER, INTENT(IN)                                :: dimen, latom, katom, lset, kset, Ssize, &
    1177              :                                                             Rsize, Ni_occupied, Nj_occupied, &
    1178              :                                                             j_batch_start
    1179              :       REAL(KIND=dp), &
    1180              :          DIMENSION(dimen, Ni_occupied, Rsize, Ssize), &
    1181              :          INTENT(IN)                                      :: BI1
    1182              :       REAL(KIND=dp), DIMENSION(dimen, dimen), INTENT(IN) :: C_T
    1183              :       TYPE(mp2_biel_type), INTENT(IN)                    :: mp2_biel
    1184              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
    1185              :       INTEGER, INTENT(IN)                                :: elements_ij_proc, multiple
    1186              :       REAL(KIND=dp), &
    1187              :          DIMENSION(dimen, dimen, elements_ij_proc), &
    1188              :          INTENT(INOUT)                                   :: BIb
    1189              : 
    1190              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'transform_occupied_orbitals_second'
    1191              : 
    1192              :       INTEGER :: elements_ij_proc_rec, handle, i, index_ij_rec, index_ij_send, index_proc_ij, &
    1193              :          index_proc_shift, j, n, proc_receive, proc_send, r, R_global, R_offset, R_offset_rec, &
    1194              :          R_start, Rsize_rec, s, S_global, S_offset, S_offset_rec, Ssize_rec
    1195              :       INTEGER, DIMENSION(5)                              :: size_parameter_rec, size_parameter_send
    1196              :       LOGICAL                                            :: case_send_receive
    1197              :       REAL(KIND=dp)                                      :: C_T_R, C_T_S
    1198            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: BIb_RS_mat_rec
    1199            0 :       REAL(KIND=dp), DIMENSION(dimen, Rsize+Ssize)       :: BIb_RS_mat
    1200              : 
    1201            0 :       CALL timeset(routineN, handle)
    1202              : 
    1203            0 :       S_offset = mp2_biel%index_table(latom, lset) - 1
    1204            0 :       R_offset = mp2_biel%index_table(katom, kset) - 1
    1205              : 
    1206            0 :       size_parameter_send(1) = Rsize
    1207            0 :       size_parameter_send(2) = Ssize
    1208            0 :       size_parameter_send(3) = R_offset
    1209            0 :       size_parameter_send(4) = S_offset
    1210            0 :       size_parameter_send(5) = elements_ij_proc
    1211              : 
    1212            0 :       DO index_proc_shift = 0, para_env%num_pe - 1
    1213              : 
    1214            0 :          proc_send = MODULO(para_env%mepos + index_proc_shift, para_env%num_pe)
    1215            0 :          proc_receive = MODULO(para_env%mepos - index_proc_shift, para_env%num_pe)
    1216              : 
    1217            0 :          case_send_receive = (proc_send /= para_env%mepos)
    1218              : 
    1219            0 :          IF (case_send_receive) THEN
    1220              :             ! the processor starts to send (and receive?)
    1221              : 
    1222            0 :             CALL para_env%sendrecv(size_parameter_send, proc_send, size_parameter_rec, proc_receive)
    1223              : 
    1224            0 :             Rsize_rec = size_parameter_rec(1)
    1225            0 :             Ssize_rec = size_parameter_rec(2)
    1226            0 :             R_offset_rec = size_parameter_rec(3)
    1227            0 :             S_offset_rec = size_parameter_rec(4)
    1228            0 :             elements_ij_proc_rec = size_parameter_rec(5)
    1229            0 :             ALLOCATE (BIb_RS_mat_rec(dimen, Rsize_rec + Ssize_rec))
    1230              : 
    1231              :          ELSE
    1232            0 :             elements_ij_proc_rec = elements_ij_proc
    1233              :          END IF
    1234              : 
    1235            0 :          index_ij_send = 0
    1236            0 :          index_ij_rec = 0
    1237            0 :          DO index_proc_ij = proc_send + 1, multiple, para_env%num_pe
    1238              : 
    1239            0 :             BIb_RS_mat = zero
    1240            0 :             IF (index_proc_ij <= Ni_occupied*Nj_occupied) THEN
    1241              : 
    1242            0 :                index_ij_send = index_ij_send + 1
    1243              : 
    1244            0 :                i = (index_proc_ij - 1)/Nj_occupied + 1
    1245            0 :                j = index_proc_ij - (i - 1)*Nj_occupied + j_batch_start
    1246              : 
    1247            0 :                DO S = 1, Ssize
    1248            0 :                   S_global = S + S_offset
    1249            0 :                   R_start = 1
    1250            0 :                   IF (katom == latom .AND. kset == lset) R_start = S
    1251            0 :                   DO R = R_start, Rsize
    1252            0 :                      R_global = R + R_offset
    1253              : 
    1254            0 :                      IF (R_global /= S_global) THEN
    1255            0 :                         C_T_R = C_T(j, R_global)
    1256            0 :                         C_T_S = C_T(j, S_global)
    1257            0 :                         DO N = 1, dimen
    1258            0 :                            BIb_RS_mat(N, R) = BIb_RS_mat(N, R) + C_T_S*BI1(N, i, R, S)
    1259              :                         END DO
    1260            0 :                         DO N = 1, dimen
    1261            0 :                            BIb_RS_mat(N, Rsize + S) = BIb_RS_mat(N, Rsize + S) + C_T_R*BI1(N, i, R, S)
    1262              :                         END DO
    1263              :                      ELSE
    1264            0 :                         C_T_S = C_T(j, S_global)
    1265            0 :                         DO N = 1, dimen
    1266            0 :                            BIb_RS_mat(N, R) = BIb_RS_mat(N, R) + C_T_S*BI1(N, i, R, S)
    1267              :                         END DO
    1268              :                      END IF
    1269              : 
    1270              :                   END DO
    1271              :                END DO
    1272              : 
    1273              :             END IF
    1274              : 
    1275            0 :             IF (case_send_receive) THEN
    1276              : 
    1277            0 :                CALL para_env%sendrecv(BIb_RS_mat, proc_send, BIb_RS_mat_rec, proc_receive)
    1278              : 
    1279            0 :                index_ij_rec = index_ij_rec + 1
    1280            0 :                IF (index_ij_rec <= elements_ij_proc .AND. elements_ij_proc > 0) THEN
    1281              : 
    1282              :                   BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, index_ij_rec) = &
    1283              :                      BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, index_ij_rec) + &
    1284            0 :                      BIb_RS_mat_rec(1:dimen, 1:Rsize_rec)
    1285              : 
    1286              :                   BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, index_ij_rec) = &
    1287              :                      BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, index_ij_rec) + &
    1288            0 :                      BIb_RS_mat_rec(1:dimen, Rsize_rec + 1:Rsize_rec + Ssize_rec)
    1289              : 
    1290              :                END IF
    1291              :             ELSE
    1292              :                ! the processor is the sender and receiver itself
    1293            0 :                IF (index_ij_send <= elements_ij_proc .AND. elements_ij_proc > 0) THEN
    1294              : 
    1295              :                   BIb(1:dimen, R_offset + 1:R_offset + Rsize, index_ij_send) = &
    1296            0 :                      BIb(1:dimen, R_offset + 1:R_offset + Rsize, index_ij_send) + BIb_RS_mat(1:dimen, 1:Rsize)
    1297              : 
    1298              :                   BIb(1:dimen, S_offset + 1:S_offset + Ssize, index_ij_send) = &
    1299            0 :                      BIb(1:dimen, S_offset + 1:S_offset + Ssize, index_ij_send) + BIb_RS_mat(1:dimen, Rsize + 1:Rsize + Ssize)
    1300              : 
    1301              :                END IF
    1302              :             END IF
    1303              : 
    1304              :          END DO ! loop over the ij of the processor
    1305              : 
    1306            0 :          IF (case_send_receive) THEN
    1307            0 :             DEALLOCATE (BIb_RS_mat_rec)
    1308              :          END IF
    1309              : 
    1310              :       END DO ! loop over the processor starting from itself
    1311              : 
    1312            0 :       CALL timestop(handle)
    1313              : 
    1314            0 :    END SUBROUTINE transform_occupied_orbitals_second
    1315              : 
    1316              : ! **************************************************************************************************
    1317              : !> \brief ...
    1318              : !> \param dimen ...
    1319              : !> \param latom ...
    1320              : !> \param katom ...
    1321              : !> \param lset ...
    1322              : !> \param kset ...
    1323              : !> \param Ssize ...
    1324              : !> \param Rsize ...
    1325              : !> \param Ni_occupied ...
    1326              : !> \param Nj_occupied ...
    1327              : !> \param j_batch_start ...
    1328              : !> \param ij_elem_max ...
    1329              : !> \param BI1 ...
    1330              : !> \param C_T ...
    1331              : !> \param mp2_biel ...
    1332              : !> \param para_env ...
    1333              : !> \param elements_ij_proc ...
    1334              : !> \param BIb ...
    1335              : ! **************************************************************************************************
    1336         3987 :    SUBROUTINE transform_occupied_orbitals_second_big(dimen, latom, katom, lset, kset, &
    1337              :                                                      Ssize, Rsize, Ni_occupied, Nj_occupied, j_batch_start, &
    1338         3987 :                                                      ij_elem_max, BI1, C_T, mp2_biel, para_env, &
    1339         3987 :                                                      elements_ij_proc, BIb)
    1340              : 
    1341              :       INTEGER, INTENT(IN)                                :: dimen, latom, katom, lset, kset, Ssize, &
    1342              :                                                             Rsize, Ni_occupied, Nj_occupied, &
    1343              :                                                             j_batch_start, ij_elem_max
    1344              :       REAL(KIND=dp), &
    1345              :          DIMENSION(dimen, Ni_occupied, Rsize, Ssize), &
    1346              :          INTENT(IN)                                      :: BI1
    1347              :       REAL(KIND=dp), DIMENSION(dimen, dimen), INTENT(IN) :: C_T
    1348              :       TYPE(mp2_biel_type), INTENT(IN)                    :: mp2_biel
    1349              :       TYPE(mp_para_env_type), INTENT(IN)                 :: para_env
    1350              :       INTEGER, INTENT(IN)                                :: elements_ij_proc
    1351              :       REAL(KIND=dp), &
    1352              :          DIMENSION(dimen, dimen, elements_ij_proc), &
    1353              :          INTENT(INOUT)                                   :: BIb
    1354              : 
    1355              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'transform_occupied_orbitals_second_big'
    1356              : 
    1357              :       INTEGER :: elements_ij_proc_rec, handle, i, index_ij_rec, index_ij_send, index_proc_ij, &
    1358              :          index_proc_shift, j, n, proc_receive, proc_send, r, R_global, R_offset, R_offset_rec, &
    1359              :          R_start, Rsize_rec, s, S_global, S_offset, S_offset_rec, Ssize_rec
    1360              :       INTEGER, DIMENSION(5)                              :: size_parameter_rec, size_parameter_send
    1361              :       LOGICAL                                            :: case_send_receive
    1362              :       REAL(KIND=dp)                                      :: C_T_R, C_T_S
    1363         3987 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: BIb_RS_mat_rec
    1364              :       REAL(KIND=dp), &
    1365         7974 :          DIMENSION(dimen, Rsize+Ssize, ij_elem_max)      :: BIb_RS_mat
    1366              : 
    1367         3987 :       CALL timeset(routineN, handle)
    1368              : 
    1369         3987 :       S_offset = mp2_biel%index_table(latom, lset) - 1
    1370         3987 :       R_offset = mp2_biel%index_table(katom, kset) - 1
    1371              : 
    1372         3987 :       size_parameter_send(1) = Rsize
    1373         3987 :       size_parameter_send(2) = Ssize
    1374         3987 :       size_parameter_send(3) = R_offset
    1375         3987 :       size_parameter_send(4) = S_offset
    1376         3987 :       size_parameter_send(5) = elements_ij_proc
    1377              : 
    1378         8028 :       DO index_proc_shift = 0, para_env%num_pe - 1
    1379              : 
    1380         4041 :          proc_send = MODULO(para_env%mepos + index_proc_shift, para_env%num_pe)
    1381         4041 :          proc_receive = MODULO(para_env%mepos - index_proc_shift, para_env%num_pe)
    1382              : 
    1383         4041 :          case_send_receive = (proc_send /= para_env%mepos)
    1384              : 
    1385         4041 :          IF (case_send_receive) THEN
    1386              :             ! the processor starts to send (and receive?)
    1387              : 
    1388           54 :             CALL para_env%sendrecv(size_parameter_send, proc_send, size_parameter_rec, proc_receive)
    1389              : 
    1390           54 :             Rsize_rec = size_parameter_rec(1)
    1391           54 :             Ssize_rec = size_parameter_rec(2)
    1392           54 :             R_offset_rec = size_parameter_rec(3)
    1393           54 :             S_offset_rec = size_parameter_rec(4)
    1394           54 :             elements_ij_proc_rec = size_parameter_rec(5)
    1395          270 :             ALLOCATE (BIb_RS_mat_rec(dimen, Rsize_rec + Ssize_rec, ij_elem_max))
    1396              :          ELSE
    1397         4041 :             elements_ij_proc_rec = elements_ij_proc
    1398              :          END IF
    1399              : 
    1400         4041 :          index_ij_send = 0
    1401         4041 :          index_ij_rec = 0
    1402      8858598 :          BIb_RS_mat = zero
    1403              : 
    1404        47346 :          DO index_proc_ij = proc_send + 1, Ni_occupied*Nj_occupied, para_env%num_pe
    1405              : 
    1406        43305 :             index_ij_send = index_ij_send + 1
    1407              : 
    1408        43305 :             i = (index_proc_ij - 1)/Nj_occupied + 1
    1409        43305 :             j = index_proc_ij - (i - 1)*Nj_occupied + j_batch_start
    1410              : 
    1411       151272 :             DO S = 1, Ssize
    1412       103926 :                S_global = S + S_offset
    1413       103926 :                R_start = 1
    1414       103926 :                IF (katom == latom .AND. kset == lset) R_start = S
    1415       367260 :                DO R = R_start, Rsize
    1416       220029 :                   R_global = R + R_offset
    1417              : 
    1418       323955 :                   IF (R_global /= S_global) THEN
    1419       209464 :                      C_T_R = C_T(j, R_global)
    1420       209464 :                      C_T_S = C_T(j, S_global)
    1421      9276183 :                      DO N = 1, dimen
    1422      9276183 :                         BIb_RS_mat(N, R, index_ij_send) = BIb_RS_mat(N, R, index_ij_send) + C_T_S*BI1(N, i, R, S)
    1423              :                      END DO
    1424      9276183 :                      DO N = 1, dimen
    1425      9276183 :                         BIb_RS_mat(N, Rsize + S, index_ij_send) = BIb_RS_mat(N, Rsize + S, index_ij_send) + C_T_R*BI1(N, i, R, S)
    1426              :                      END DO
    1427              :                   ELSE
    1428        10565 :                      C_T_S = C_T(j, S_global)
    1429       440058 :                      DO N = 1, dimen
    1430       440058 :                         BIb_RS_mat(N, R, index_ij_send) = BIb_RS_mat(N, R, index_ij_send) + C_T_S*BI1(N, i, R, S)
    1431              :                      END DO
    1432              :                   END IF
    1433              : 
    1434              :                END DO
    1435              :             END DO
    1436              : 
    1437              :          END DO
    1438              : 
    1439         8028 :          IF (case_send_receive) THEN
    1440              : 
    1441           54 :             CALL para_env%sendrecv(BIb_RS_mat, proc_send, BIb_RS_mat_rec, proc_receive)
    1442              : 
    1443              :             BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, 1:elements_ij_proc) = &
    1444              :                BIb(1:dimen, R_offset_rec + 1:R_offset_rec + Rsize_rec, 1:elements_ij_proc) + &
    1445        16182 :                BIb_RS_mat_rec(1:dimen, 1:Rsize_rec, 1:elements_ij_proc)
    1446              : 
    1447              :             BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, 1:elements_ij_proc) = &
    1448              :                BIb(1:dimen, S_offset_rec + 1:S_offset_rec + Ssize_rec, 1:elements_ij_proc) + &
    1449        15006 :                BIb_RS_mat_rec(1:dimen, Rsize_rec + 1:Rsize_rec + Ssize_rec, 1:elements_ij_proc)
    1450              : 
    1451           54 :             DEALLOCATE (BIb_RS_mat_rec)
    1452              :          ELSE
    1453              :             ! the processor is the sender and receiver itself
    1454              :             BIb(1:dimen, R_offset + 1:R_offset + Rsize, 1:elements_ij_proc) = &
    1455              :                BIb(1:dimen, R_offset + 1:R_offset + Rsize, 1:elements_ij_proc) + &
    1456      4187907 :                BIb_RS_mat(1:dimen, 1:Rsize, 1:elements_ij_proc)
    1457              : 
    1458              :             BIb(1:dimen, S_offset + 1:S_offset + Ssize, 1:elements_ij_proc) = &
    1459              :                BIb(1:dimen, S_offset + 1:S_offset + Ssize, 1:elements_ij_proc) + &
    1460      4686849 :                BIb_RS_mat(1:dimen, Rsize + 1:Rsize + Ssize, 1:elements_ij_proc)
    1461              : 
    1462              :          END IF
    1463              : 
    1464              :       END DO ! loop over the processor starting from itself
    1465              : 
    1466         3987 :       CALL timestop(handle)
    1467              : 
    1468         3987 :    END SUBROUTINE transform_occupied_orbitals_second_big
    1469              : 
    1470              : ! **************************************************************************************************
    1471              : !> \brief ...
    1472              : !> \param dimen ...
    1473              : !> \param occupied ...
    1474              : !> \param virtual ...
    1475              : !> \param i_batch_start ...
    1476              : !> \param j_batch_start ...
    1477              : !> \param BIb ...
    1478              : !> \param C ...
    1479              : !> \param Auto ...
    1480              : !> \param elements_ij_proc ...
    1481              : !> \param ij_list_proc ...
    1482              : !> \param nspins ...
    1483              : !> \param Emp2 ...
    1484              : !> \param Emp2_Cou ...
    1485              : !> \param Emp2_ex ...
    1486              : ! **************************************************************************************************
    1487           31 :    SUBROUTINE transform_virtual_orbitals_and_accumulate(dimen, occupied, virtual, i_batch_start, &
    1488           31 :                                                         j_batch_start, BIb, C, Auto, elements_ij_proc, &
    1489           31 :                                                         ij_list_proc, nspins, Emp2, Emp2_Cou, Emp2_ex)
    1490              : 
    1491              :       INTEGER, INTENT(IN)                                :: dimen, occupied, virtual, i_batch_start, &
    1492              :                                                             j_batch_start
    1493              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
    1494              :          INTENT(INOUT)                                   :: BIb
    1495              :       REAL(KIND=dp), DIMENSION(dimen, dimen), INTENT(IN) :: C
    1496              :       REAL(KIND=dp), DIMENSION(dimen), INTENT(IN)        :: Auto
    1497              :       INTEGER, INTENT(IN)                                :: elements_ij_proc
    1498              :       INTEGER, DIMENSION(elements_ij_proc, 2), &
    1499              :          INTENT(IN)                                      :: ij_list_proc
    1500              :       INTEGER, INTENT(IN)                                :: nspins
    1501              :       REAL(KIND=dp), INTENT(INOUT)                       :: Emp2, Emp2_Cou, Emp2_ex
    1502              : 
    1503              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'transform_virtual_orbitals_and_accumulate'
    1504              :       REAL(KIND=dp), PARAMETER                           :: zero = 0.0_dp
    1505              : 
    1506              :       INTEGER                                            :: a, a_global, b, b_global, handle, i, &
    1507              :                                                             i_global, index_ij, j, j_global
    1508              :       REAL(KIND=dp)                                      :: iajb, ibja, parz, two
    1509           31 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: BIa
    1510              : 
    1511           31 :       CALL timeset(routineN, handle)
    1512              : 
    1513          124 :       ALLOCATE (BIa(dimen, virtual))
    1514              : 
    1515           31 :       BIa = zero
    1516          287 :       DO index_ij = 1, elements_ij_proc
    1517              : 
    1518              :          CALL DGEMM('T', 'N', dimen, virtual, dimen, 1.0_dp, Bib(1, 1, index_ij), &
    1519          256 :                     dimen, C(1, occupied + 1), dimen, 0.0_dp, Bia(1, 1), dimen)
    1520       337334 :          Bib(1:dimen, 1:virtual, index_ij) = Bia(1:dimen, 1:virtual)
    1521              : 
    1522              :       END DO
    1523              : 
    1524           31 :       DEALLOCATE (BIa)
    1525          124 :       ALLOCATE (BIa(virtual, virtual))
    1526              : 
    1527           31 :       BIa = zero
    1528          287 :       DO index_ij = 1, elements_ij_proc
    1529              : 
    1530              :          CALL DGEMM('T', 'N', virtual, virtual, dimen, 1.0_dp, Bib(1, 1, index_ij), dimen, C(1, occupied + 1), dimen, 0.0_dp, &
    1531          256 :                     BIa(1, 1), virtual)
    1532       299439 :          BIb(1:virtual, 1:virtual, index_ij) = BIa(1:virtual, 1:virtual)
    1533              : 
    1534              :       END DO
    1535              : 
    1536           31 :       two = 2.0_dp/nspins
    1537          287 :       DO index_ij = 1, elements_ij_proc
    1538          256 :          i = ij_list_proc(index_ij, 1)
    1539          256 :          j = ij_list_proc(index_ij, 2)
    1540          256 :          i_global = i + i_batch_start
    1541          256 :          j_global = j + j_batch_start
    1542         8298 :          DO a = 1, virtual
    1543         8011 :             a_global = a + occupied
    1544       299408 :             DO b = 1, virtual
    1545       291141 :                b_global = b + occupied
    1546       291141 :                iajb = BIb(a, b, index_ij)
    1547       291141 :                ibja = BIb(b, a, index_ij)
    1548       291141 :                parz = iajb/(Auto(i_global) + Auto(j_global) - Auto(a_global) - Auto(b_global))
    1549              :                ! parz=parz*(two*iajb-ibja)   !Full
    1550              :                ! parz=parz*(iajb)            !Coulomb
    1551              :                ! parz=parz*(ibja)            !Coulomb
    1552              :                ! Emp2=Emp2+parz/nspins
    1553       291141 :                Emp2_Cou = Emp2_Cou + parz*two*(iajb)/nspins
    1554       291141 :                Emp2_ex = Emp2_ex - parz*(ibja)/nspins
    1555       299152 :                Emp2 = Emp2 + parz*(two*iajb - ibja)/nspins
    1556              :             END DO
    1557              :          END DO
    1558              :       END DO
    1559              : 
    1560           31 :       DEALLOCATE (BIa)
    1561              : 
    1562           31 :       CALL timestop(handle)
    1563              : 
    1564           31 :    END SUBROUTINE transform_virtual_orbitals_and_accumulate
    1565              : 
    1566              : ! **************************************************************************************************
    1567              : !> \brief ...
    1568              : !> \param dimen ...
    1569              : !> \param occ_i ...
    1570              : !> \param occ_j ...
    1571              : !> \param virt_i ...
    1572              : !> \param virt_j ...
    1573              : !> \param i_batch_start ...
    1574              : !> \param j_batch_start ...
    1575              : !> \param BIb ...
    1576              : !> \param C_i ...
    1577              : !> \param C_j ...
    1578              : !> \param Auto_i ...
    1579              : !> \param Auto_j ...
    1580              : !> \param elements_ij_proc ...
    1581              : !> \param ij_list_proc ...
    1582              : !> \param Emp2 ...
    1583              : !> \param Emp2_Cou ...
    1584              : ! **************************************************************************************************
    1585           10 :    SUBROUTINE transform_virtual_orbitals_and_accumulate_ABcase(dimen, occ_i, occ_j, virt_i, virt_j, i_batch_start, &
    1586           10 :                                                                j_batch_start, BIb, C_i, C_j, Auto_i, Auto_j, elements_ij_proc, &
    1587           10 :                                                                ij_list_proc, Emp2, Emp2_Cou)
    1588              : 
    1589              :       INTEGER, INTENT(IN)                                :: dimen, occ_i, occ_j, virt_i, virt_j, &
    1590              :                                                             i_batch_start, j_batch_start
    1591              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :), &
    1592              :          INTENT(INOUT)                                   :: BIb
    1593              :       REAL(KIND=dp), DIMENSION(dimen, dimen), INTENT(IN) :: C_i, C_j
    1594              :       REAL(KIND=dp), DIMENSION(dimen), INTENT(IN)        :: Auto_i, Auto_j
    1595              :       INTEGER, INTENT(IN)                                :: elements_ij_proc
    1596              :       INTEGER, DIMENSION(elements_ij_proc, 2), &
    1597              :          INTENT(IN)                                      :: ij_list_proc
    1598              :       REAL(KIND=dp), INTENT(INOUT)                       :: Emp2, Emp2_Cou
    1599              : 
    1600              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'transform_virtual_orbitals_and_accumulate_ABcase'
    1601              :       REAL(KIND=dp), PARAMETER                           :: two = 2.D+00, zero = 0.D+00
    1602              : 
    1603              :       INTEGER                                            :: a, a_global, b, b_global, handle, i, &
    1604              :                                                             i_global, index_ij, j, j_global, n, s
    1605              :       REAL(KIND=dp)                                      :: iajb, parz
    1606           10 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: BIa
    1607              : 
    1608           10 :       CALL timeset(routineN, handle)
    1609              : 
    1610           40 :       ALLOCATE (BIa(dimen, virt_i))
    1611              : 
    1612           56 :       DO index_ij = 1, elements_ij_proc
    1613              : 
    1614         1236 :          DO a = 1, virt_i
    1615         1190 :             a_global = a + occ_i
    1616        51930 :             DO S = 1, dimen
    1617              :                parz = zero
    1618      2449752 :                DO N = 1, dimen
    1619      2449752 :                   parz = parz + C_i(N, a_global)*BIb(N, S, index_ij)
    1620              :                END DO
    1621        51884 :                BIa(S, a) = parz
    1622              :             END DO
    1623              :          END DO
    1624              : 
    1625        51940 :          BIb(1:dimen, 1:virt_i, index_ij) = BIa
    1626              : 
    1627              :       END DO
    1628              : 
    1629           10 :       DEALLOCATE (BIa)
    1630           40 :       ALLOCATE (BIa(virt_i, virt_j))
    1631              : 
    1632           56 :       DO index_ij = 1, elements_ij_proc
    1633              : 
    1634         1236 :          DO a = 1, virt_i
    1635        47824 :             DO b = 1, virt_j
    1636        46588 :                b_global = b + occ_j
    1637        46588 :                parz = zero
    1638      2257144 :                DO S = 1, dimen
    1639      2257144 :                   parz = parz + C_j(S, b_global)*BIb(S, a, index_ij)
    1640              :                END DO
    1641        47778 :                BIa(a, b) = parz
    1642              :             END DO
    1643              :          END DO
    1644              : 
    1645        47904 :          BIb(1:virt_i, 1:virt_j, index_ij) = BIa
    1646              : 
    1647              :       END DO
    1648              : 
    1649           56 :       DO index_ij = 1, elements_ij_proc
    1650           46 :          i = ij_list_proc(index_ij, 1)
    1651           46 :          j = ij_list_proc(index_ij, 2)
    1652           46 :          i_global = i + i_batch_start
    1653           46 :          j_global = j + j_batch_start
    1654         1246 :          DO a = 1, virt_i
    1655         1190 :             a_global = a + occ_i
    1656        47824 :             DO b = 1, virt_j
    1657        46588 :                b_global = b + occ_j
    1658        46588 :                iajb = BIb(a, b, index_ij)
    1659        46588 :                parz = iajb*iajb/(Auto_i(i_global) + Auto_j(j_global) - Auto_i(a_global) - Auto_j(b_global))
    1660        46588 :                Emp2_Cou = Emp2_Cou + parz/two
    1661        47778 :                Emp2 = Emp2 + parz/two
    1662              :             END DO
    1663              :          END DO
    1664              :       END DO
    1665              : 
    1666           10 :       DEALLOCATE (BIa)
    1667              : 
    1668           10 :       CALL timestop(handle)
    1669              : 
    1670           10 :    END SUBROUTINE transform_virtual_orbitals_and_accumulate_ABcase
    1671              : 
    1672              : END MODULE mp2_direct_method
        

Generated by: LCOV version 2.0-1