LCOV - code coverage report
Current view: top level - src - hfx_derivatives.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 96.3 % 217 209
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 5 5

            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 derivatives with respect to basis function origin
      10              : !> \par History
      11              : !>      04.2008 created [Manuel Guidon]
      12              : !> \author Manuel Guidon
      13              : ! **************************************************************************************************
      14              : MODULE hfx_derivatives
      15              :    USE r12_fundamentals, ONLY: r12_fundamentals_init
      16              :    USE admm_types, ONLY: admm_type
      17              :    USE atomic_kind_types, ONLY: atomic_kind_type, &
      18              :                                 get_atomic_kind_set
      19              :    USE cell_types, ONLY: cell_type, &
      20              :                          pbc
      21              :    USE cp_control_types, ONLY: dft_control_type
      22              :    USE cp_files, ONLY: close_file, &
      23              :                        open_file
      24              :    USE cp_log_handling, ONLY: cp_get_default_logger, &
      25              :                               cp_logger_type
      26              :    USE cp_output_handling, ONLY: cp_p_file, &
      27              :                                  cp_print_key_finished_output, &
      28              :                                  cp_print_key_should_output, &
      29              :                                  cp_print_key_unit_nr
      30              :    USE message_passing, ONLY: mp_para_env_type
      31              :    USE cp_dbcsr_api, ONLY: dbcsr_get_matrix_type, &
      32              :                            dbcsr_p_type, &
      33              :                            dbcsr_type_antisymmetric
      34              :    USE four_center_integral_methods, ONLY: forces4
      35              :    USE hfx_communication, ONLY: get_full_density
      36              :    USE hfx_compression_methods, ONLY: hfx_add_mult_cache_elements, &
      37              :                                       hfx_add_single_cache_element, &
      38              :                                       hfx_decompress_first_cache, &
      39              :                                       hfx_flush_last_cache, &
      40              :                                       hfx_get_mult_cache_elements, &
      41              :                                       hfx_get_single_cache_element, &
      42              :                                       hfx_reset_cache_and_container
      43              :    USE hfx_load_balance_methods, ONLY: collect_load_balance_info, &
      44              :                                        hfx_load_balance, &
      45              :                                        hfx_update_load_balance
      46              :    USE hfx_pair_list_methods, ONLY: build_atomic_pair_list, &
      47              :                                     build_pair_list
      48              :    USE hfx_screening_methods, ONLY: update_pmax_mat
      49              :    USE hfx_types, ONLY: &
      50              :       alloc_containers, dealloc_containers, hfx_basis_info_type, hfx_basis_type, hfx_cache_type, &
      51              :       hfx_cell_type, hfx_container_type, hfx_distribution, hfx_general_type, hfx_init_container, &
      52              :       hfx_load_balance_type, hfx_memory_type, hfx_p_kind, hfx_pgf_list, &
      53              :       hfx_potential_type, hfx_screen_coeff_type, hfx_screening_type, hfx_task_list_type, &
      54              :       hfx_type, log_zero, pair_list_type, pair_set_list_type
      55              :    USE input_constants, ONLY: do_potential_mix_cl_trunc, &
      56              :                               do_potential_truncated, &
      57              :                               hfx_do_eval_forces
      58              :    USE input_section_types, ONLY: section_vals_type
      59              :    USE kinds, ONLY: dp, &
      60              :                     int_8
      61              :    USE machine, ONLY: m_walltime
      62              :    USE mathconstants, ONLY: fac
      63              :    USE orbital_pointers, ONLY: nco, &
      64              :                                ncoset, &
      65              :                                nso
      66              :    USE particle_types, ONLY: particle_type
      67              :    USE qs_environment_types, ONLY: get_qs_env, &
      68              :                                    qs_environment_type
      69              :    USE qs_force_types, ONLY: qs_force_type
      70              :    USE util, ONLY: sort
      71              :    USE virial_types, ONLY: virial_type
      72              : 
      73              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
      74              : 
      75              : #include "./base/base_uses.f90"
      76              : 
      77              :    IMPLICIT NONE
      78              :    PRIVATE
      79              : 
      80              :    PUBLIC :: derivatives_four_center
      81              : 
      82              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_derivatives'
      83              : 
      84              : !***
      85              : 
      86              : CONTAINS
      87              : 
      88              : ! **************************************************************************************************
      89              : !> \brief computes four center derivatives for a full basis set and updates the
      90              : !>      forces%fock_4c arrays. Uses all 8 eri symmetries
      91              : !> \param qs_env ...
      92              : !> \param rho_ao density matrix
      93              : !> \param rho_ao_resp relaxed density matrix from response
      94              : !> \param hfx_section HFX input section
      95              : !> \param para_env para_env
      96              : !> \param irep ID of HFX replica
      97              : !> \param use_virial ...
      98              : !> \param adiabatic_rescale_factor parameter used for MCY3 hybrid
      99              : !> \param resp_only Calculate only forces from response density
     100              : !> \par History
     101              : !>      06.2007 created [Manuel Guidon]
     102              : !>      08.2007 optimized load balance [Manuel Guidon]
     103              : !>      02.2009 completely rewritten screening part [Manuel Guidon]
     104              : !>      12.2017 major bug fix. removed wrong cycle that was causing segfault.
     105              : !>              see https://groups.google.com/forum/#!topic/cp2k/pc6B14XOALY
     106              : !>              [Tobias Binninger + Valery Weber]
     107              : !> \author Manuel Guidon
     108              : ! **************************************************************************************************
     109         2012 :    SUBROUTINE derivatives_four_center(qs_env, rho_ao, rho_ao_resp, hfx_section, para_env, &
     110              :                                       irep, use_virial, adiabatic_rescale_factor, resp_only, &
     111              :                                       external_x_data, nspins)
     112              : 
     113              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     114              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao
     115              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao_resp
     116              :       TYPE(section_vals_type), POINTER                   :: hfx_section
     117              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     118              :       INTEGER, INTENT(IN)                                :: irep
     119              :       LOGICAL, INTENT(IN)                                :: use_virial
     120              :       REAL(dp), INTENT(IN), OPTIONAL                     :: adiabatic_rescale_factor
     121              :       LOGICAL, INTENT(IN), OPTIONAL                      :: resp_only
     122              :       TYPE(hfx_type), DIMENSION(:, :), POINTER, OPTIONAL  :: external_x_data
     123              :       INTEGER, INTENT(IN), OPTIONAL                      :: nspins
     124              : 
     125              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'derivatives_four_center'
     126              : 
     127              :       INTEGER :: atomic_offset_ac, atomic_offset_ad, atomic_offset_bc, atomic_offset_bd, bin, &
     128              :                  bits_max_val, buffer_left, buffer_size, buffer_start, cache_size, coord, current_counter, &
     129              :                  forces_map(4, 2), handle, handle_bin, handle_getP, handle_load, handle_main, i, i_atom, &
     130              :                  i_list_ij, i_list_kl, i_set_list_ij, i_set_list_ij_start, i_set_list_ij_stop, &
     131              :                  i_set_list_kl, i_set_list_kl_start, i_set_list_kl_stop, i_thread, iatom, iatom_block, &
     132              :                  iatom_end, iatom_start, ikind, iset, iw, j, j_atom, jatom, jatom_block, jatom_end, &
     133              :                  jatom_start, jkind, jset, k, k_atom, katom, katom_block, katom_end, katom_start
     134              :       INTEGER :: kind_kind_idx, kkind, kset, l_atom, l_max, latom, latom_block, latom_end, &
     135              :                  latom_start, lkind, lset, max_am, max_pgf, max_set, my_bin_id, my_bin_size, my_thread_id, &
     136              :                  n_threads, natom, nbits, ncob, ncos_max, nints, nkimages, nkind, nneighbors, nseta, nsetb, &
     137              :                  nsgf_max, my_nspins, sgfb, shm_task_counter, shm_total_bins, &
     138              :                  swap_id, tmp_i4
     139              :       INTEGER(int_8) :: atom_block, counter, estimate_to_store_int, max_val_memory, &
     140              :                         mem_compression_counter, mem_eris, mem_max_val, my_current_counter, my_istart, &
     141              :                         n_processes, nblocks, ncpu, neris_incore, neris_onthefly, neris_tmp, neris_total, &
     142              :                         nprim_ints, shm_neris_incore, shm_neris_onthefly, shm_neris_total, shm_nprim_ints, &
     143              :                         shm_stor_count_max_val, shm_storage_counter_integrals, stor_count_max_val, &
     144              :                         storage_counter_integrals, tmp_block, tmp_i8(6)
     145         2012 :       INTEGER(int_8), ALLOCATABLE, DIMENSION(:)          :: tmp_task_list_cost
     146         2012 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_of_kind, kind_of, last_sgf_global, &
     147         2012 :                                                             nimages, tmp_index
     148         2012 :       INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, lc_max, lc_min, ld_max, &
     149         4024 :                                         ld_min, npgfa, npgfb, npgfc, npgfd, nsgfa, nsgfb, nsgfc, nsgfd, shm_block_offset
     150         2012 :       INTEGER, DIMENSION(:, :), POINTER :: first_sgfb, &
     151         2012 :                                            offset_ac_set, offset_ad_set, offset_bc_set, offset_bd_set, shm_atomic_block_offset
     152              :       INTEGER, DIMENSION(:, :), POINTER, SAVE            :: shm_is_assoc_atomic_block
     153         2012 :       INTEGER, DIMENSION(:, :, :, :), POINTER            :: shm_set_offset
     154              :       INTEGER, SAVE                                      :: shm_number_of_p_entries
     155              :       LOGICAL :: bins_left, buffer_overflow, do_dynamic_load_balancing, do_it, do_periodic, &
     156              :                  do_print_load_balance_info, is_anti_symmetric, my_resp_only, screen_pmat_forces, &
     157              :                  treat_forces_in_core, use_disk_storage, with_resp_density
     158         2012 :       LOGICAL, DIMENSION(:, :), POINTER                  :: shm_atomic_pair_list
     159              :       REAL(dp) :: bintime_start, bintime_stop, cartesian_estimate, cartesian_estimate_virial, &
     160              :                   compression_factor, eps_schwarz, eps_storage, fac, hf_fraction, ln_10, log10_eps_schwarz, &
     161              :                   log10_pmax, log_2, max_contraction_val, max_val1, max_val2, max_val2_set, &
     162              :                   my_adiabatic_rescale_factor, pmax_atom, pmax_blocks, pmax_entry, pmax_tmp, ra(3), rab2, &
     163              :                   rb(3), rc(3), rcd2, rd(3), spherical_estimate, spherical_estimate_virial, symm_fac, &
     164              :                   tmp_virial(3, 3)
     165         2012 :       REAL(dp), ALLOCATABLE, DIMENSION(:) :: ede_buffer1, ede_buffer2, &
     166         2012 :                                              ede_work, ede_work2, ede_work2_virial, ede_work_forces, &
     167         4024 :                                              ede_work_virial, pac_buf, pac_buf_beta, pac_buf_resp, pac_buf_resp_beta, pad_buf, &
     168         2012 :                                              pad_buf_beta, pad_buf_resp, pad_buf_resp_beta, pbc_buf, pbc_buf_beta, pbc_buf_resp, &
     169         2012 :                                              pbc_buf_resp_beta, pbd_buf, pbd_buf_beta, pbd_buf_resp, pbd_buf_resp_beta
     170         2012 :       REAL(dp), ALLOCATABLE, DIMENSION(:), TARGET        :: primitive_forces, primitive_forces_virial
     171         2012 :       REAL(dp), DIMENSION(:), POINTER                    :: full_density_resp, &
     172         4024 :                                                             full_density_resp_beta, T2
     173         2012 :       REAL(dp), DIMENSION(:, :), POINTER :: full_density_alpha, full_density_beta, &
     174         2012 :                                             max_contraction, ptr_p_1, ptr_p_2, ptr_p_3, ptr_p_4, shm_pmax_atom, shm_pmax_block, &
     175         2012 :                                             sphi_b, zeta, zetb, zetc, zetd
     176              :       REAL(KIND=dp)                                      :: coeffs_kind_max0
     177              :       TYPE(admm_type), POINTER                           :: admm_env
     178         2012 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     179              :       TYPE(cell_type), POINTER                           :: cell
     180              :       TYPE(cp_logger_type), POINTER                      :: logger
     181              :       TYPE(dft_control_type), POINTER                    :: dft_control
     182              :       TYPE(hfx_basis_info_type), POINTER                 :: basis_info
     183         2012 :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
     184         2012 :       TYPE(hfx_cache_type), DIMENSION(:), POINTER        :: integral_caches
     185              :       TYPE(hfx_cache_type), POINTER                      :: maxval_cache
     186         2012 :       TYPE(hfx_container_type), DIMENSION(:), POINTER    :: integral_containers
     187              :       TYPE(hfx_container_type), POINTER                  :: maxval_container
     188              :       TYPE(hfx_distribution), POINTER                    :: distribution_forces
     189              :       TYPE(hfx_general_type)                             :: general_parameter
     190              :       TYPE(hfx_load_balance_type), POINTER               :: load_balance_parameter
     191              :       TYPE(hfx_memory_type), POINTER                     :: memory_parameter
     192         2012 :       TYPE(hfx_p_kind), DIMENSION(:), POINTER            :: shm_initial_p
     193         2012 :       TYPE(hfx_pgf_list), ALLOCATABLE, DIMENSION(:)      :: pgf_list_ij, pgf_list_kl
     194              :       TYPE(hfx_potential_type)                           :: potential_parameter
     195              :       TYPE(hfx_screen_coeff_type), DIMENSION(:, :), &
     196         2012 :          POINTER                                         :: screen_coeffs_kind, tmp_R_1, tmp_R_2, &
     197         2012 :                                                             tmp_screen_pgf1, tmp_screen_pgf2
     198              :       TYPE(hfx_screen_coeff_type), &
     199         2012 :          DIMENSION(:, :, :, :), POINTER                  :: screen_coeffs_set
     200              :       TYPE(hfx_screen_coeff_type), &
     201         2012 :          DIMENSION(:, :, :, :, :, :), POINTER            :: radii_pgf, screen_coeffs_pgf
     202              :       TYPE(hfx_screening_type)                           :: screening_parameter
     203         2012 :       TYPE(hfx_task_list_type), DIMENSION(:), POINTER    :: shm_task_list, tmp_task_list
     204              :       TYPE(hfx_type), POINTER                            :: actual_x_data
     205         2012 :       TYPE(hfx_type), DIMENSION(:, :), POINTER            :: my_x_data
     206              :       TYPE(pair_list_type)                               :: list_ij, list_kl
     207              :       TYPE(pair_set_list_type), ALLOCATABLE, &
     208         2012 :          DIMENSION(:)                                    :: set_list_ij, set_list_kl
     209         2012 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     210         2012 :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
     211              :       TYPE(virial_type), POINTER                         :: virial
     212              : 
     213         2012 :       NULLIFY (dft_control, admm_env)
     214              : 
     215         2012 :       with_resp_density = .FALSE.
     216          684 :       IF (ASSOCIATED(rho_ao_resp)) with_resp_density = .TRUE.
     217              : 
     218         2012 :       my_resp_only = .FALSE.
     219         2012 :       IF (PRESENT(resp_only)) my_resp_only = resp_only
     220              : 
     221         2012 :       is_anti_symmetric = dbcsr_get_matrix_type(rho_ao(1, 1)%matrix) == dbcsr_type_antisymmetric
     222              : 
     223              :       CALL get_qs_env(qs_env, &
     224              :                       admm_env=admm_env, &
     225              :                       particle_set=particle_set, &
     226              :                       atomic_kind_set=atomic_kind_set, &
     227              :                       cell=cell, &
     228         2012 :                       dft_control=dft_control)
     229              : 
     230         2012 :       IF (PRESENT(nspins)) THEN
     231          252 :          my_nspins = nspins
     232              :       ELSE
     233         1760 :          my_nspins = dft_control%nspins
     234              :       END IF
     235         2012 :       nkimages = dft_control%nimages
     236         2012 :       CPASSERT(nkimages == 1)
     237              : 
     238         2012 :       my_x_data => qs_env%x_data
     239         2012 :       IF (PRESENT(external_x_data)) my_x_data => external_x_data
     240              : 
     241              :       !! One atom systems have no contribution to forces
     242         2012 :       IF (SIZE(particle_set, 1) == 1) THEN
     243            4 :          IF (.NOT. use_virial) RETURN
     244              :       END IF
     245              : 
     246         2008 :       CALL timeset(routineN, handle)
     247              :       !! Calculate l_max used in fgamma , because init_md_ftable is definitely not thread safe
     248         2008 :       nkind = SIZE(atomic_kind_set, 1)
     249         2008 :       l_max = 0
     250         5786 :       DO ikind = 1, nkind
     251        16122 :          l_max = MAX(l_max, MAXVAL(my_x_data(1, 1)%basis_parameter(ikind)%lmax))
     252              :       END DO
     253         2008 :       l_max = 4*l_max + 1
     254         2008 :       CALL r12_fundamentals_init(l_max, my_x_data(1, 1)%potential_parameter, para_env)
     255              : 
     256         2008 :       n_threads = 1
     257         2008 : !$    n_threads = omp_get_max_threads()
     258              : 
     259         2008 :       shm_neris_total = 0
     260         2008 :       shm_nprim_ints = 0
     261         2008 :       shm_neris_onthefly = 0
     262         2008 :       shm_storage_counter_integrals = 0
     263         2008 :       shm_neris_incore = 0
     264         2008 :       shm_stor_count_max_val = 0
     265              : 
     266              :       !! get force array
     267         2008 :       CALL get_qs_env(qs_env=qs_env, force=force, virial=virial)
     268              : 
     269         2008 :       my_adiabatic_rescale_factor = 1.0_dp
     270         2008 :       IF (PRESENT(adiabatic_rescale_factor)) THEN
     271          260 :          my_adiabatic_rescale_factor = adiabatic_rescale_factor
     272              :       END IF
     273              : 
     274              :       !$OMP PARALLEL DEFAULT(NONE) &
     275              : !$OMP SHARED(qs_env,&
     276              : !$OMP                                  rho_ao,&
     277              : !$OMP                                  rho_ao_resp,&
     278              : !$OMP                                  with_resp_density,&
     279              : !$OMP                                  hfx_section,&
     280              : !$OMP                                  para_env,&
     281              : !$OMP                                  irep,&
     282              : !$OMP                                  my_resp_only,&
     283              : !$OMP                                  ncoset,&
     284              : !$OMP                                  nco,&
     285              : !$OMP                                  nso,&
     286              : !$OMP                                  n_threads,&
     287              : !$OMP                                  full_density_alpha,&
     288              : !$OMP                                  full_density_resp,&
     289              : !$OMP                                  full_density_resp_beta,&
     290              : !$OMP                                  full_density_beta,&
     291              : !$OMP                                  shm_initial_p,&
     292              : !$OMP                                  shm_is_assoc_atomic_block,&
     293              : !$OMP                                  shm_number_of_p_entries,&
     294              : !$OMP                                  force,&
     295              : !$OMP                                  virial,&
     296              : !$OMP                                  my_adiabatic_rescale_factor,&
     297              : !$OMP                                  shm_neris_total,&
     298              : !$OMP                                  shm_neris_onthefly,&
     299              : !$OMP                                  shm_storage_counter_integrals,&
     300              : !$OMP                                  shm_neris_incore,&
     301              : !$OMP                                  shm_nprim_ints,&
     302              : !$OMP                                  shm_stor_count_max_val,&
     303              : !$OMP                                  cell,&
     304              : !$OMP                                  screen_coeffs_set,&
     305              : !$OMP                                  screen_coeffs_kind,&
     306              : !$OMP                                  screen_coeffs_pgf,&
     307              : !$OMP                                  nkind,&
     308              : !$OMP                                  is_anti_symmetric,&
     309              : !$OMP                                  radii_pgf,&
     310              : !$OMP                                  shm_atomic_block_offset,&
     311              : !$OMP                                  shm_set_offset,&
     312              : !$OMP                                  shm_block_offset,&
     313              : !$OMP                                  shm_task_counter,&
     314              : !$OMP                                  shm_task_list,&
     315              : !$OMP                                  shm_total_bins,&
     316              : !$OMP                                  shm_pmax_block,&
     317              : !$OMP                                  shm_atomic_pair_list,&
     318              : !$OMP                                  shm_pmax_atom,&
     319              : !$OMP                                  use_virial,&
     320              : !$OMP                                  do_print_load_balance_info,&
     321              : !$OMP                                  nkimages,my_nspins,my_x_data)&
     322              : !$OMP  PRIVATE(actual_x_data,atomic_kind_set,atomic_offset_ac,atomic_offset_ad,atomic_offset_bc,&
     323              : !$OMP  atomic_offset_bd,atom_of_kind,basis_info,&
     324              : !$OMP  basis_parameter,bin,bins_left,bintime_start,bintime_stop,bits_max_val,buffer_left,buffer_overflow,buffer_size,&
     325              : !$OMP  buffer_start,cache_size,cartesian_estimate,cartesian_estimate_virial,&
     326              : !$OMP  coeffs_kind_max0,compression_factor,counter,current_counter,&
     327              : !$OMP  distribution_forces,do_dynamic_load_balancing,do_it,do_periodic,ede_buffer1,ede_buffer2,&
     328              : !$OMP  ede_work,ede_work2,ede_work2_virial,ede_work_forces,ede_work_virial,eps_schwarz,eps_storage,estimate_to_store_int,&
     329              : !$OMP  fac,first_sgfb,forces_map,general_parameter,handle_bin,handle_getp,handle_load,&
     330              : !$OMP  handle_main,hf_fraction,i_atom,iatom_end,iatom_start,ikind,integral_caches,integral_containers,&
     331              : !$OMP  i_set_list_ij_start,i_set_list_ij_stop,i_set_list_kl_start,i_set_list_kl_stop,i_thread,iw,j_atom,jatom_end,&
     332              : !$OMP  jatom_start,jkind,katom,k_atom,katom_block,katom_end,katom_start,kind_kind_idx,&
     333              : !$OMP  kind_of,kkind,kset,la_max,la_min,last_sgf_global,latom,l_atom,&
     334              : !$OMP  latom_block,latom_end,latom_start,lb_max,lb_min,lc_max,lc_min,ld_max,&
     335              : !$OMP  ld_min,list_ij,list_kl,lkind,ln_10,load_balance_parameter,log10_eps_schwarz,log10_pmax,&
     336              : !$OMP  log_2,logger,lset,max_am,max_contraction,max_contraction_val,max_pgf,max_set,&
     337              : !$OMP  max_val1,max_val2,max_val2_set,maxval_cache,maxval_container,max_val_memory,mem_compression_counter,mem_eris,&
     338              : !$OMP  mem_max_val,memory_parameter,my_bin_id,my_bin_size,my_current_counter,my_istart,my_thread_id,natom,&
     339              : !$OMP  nbits,nblocks,ncob,ncos_max,ncpu,neris_incore,neris_onthefly,neris_tmp,&
     340              : !$OMP  neris_total,nimages,nints,nneighbors,npgfa,npgfb,npgfc,npgfd,&
     341              : !$OMP  nprim_ints,n_processes,nseta,nsetb,nsgfa,nsgfb,nsgfc,nsgfd,&
     342              : !$OMP  nsgf_max,offset_ac_set,offset_ad_set,&
     343              : !$OMP  offset_bc_set,offset_bd_set,pac_buf,pac_buf_beta,pac_buf_resp,pac_buf_resp_beta,pad_buf,pad_buf_beta,pad_buf_resp,&
     344              : !$OMP  pad_buf_resp_beta,particle_set,pbc_buf,pbc_buf_beta,pbc_buf_resp,pbc_buf_resp_beta,pbd_buf,pbd_buf_beta, &
     345              : !$OMP  pbd_buf_resp, pbd_buf_resp_beta, pgf_list_ij,&
     346              : !$OMP  pgf_list_kl,pmax_atom,pmax_blocks,pmax_entry,pmax_tmp,potential_parameter,primitive_forces,&
     347              : !$OMP  primitive_forces_virial,ptr_p_1,ptr_p_2,ptr_p_3,ptr_p_4,ra,rab2,&
     348              : !$OMP  rb,rc,rcd2,rd,screening_parameter,screen_pmat_forces,set_list_ij,set_list_kl,&
     349              : !$OMP  sgfb,spherical_estimate,spherical_estimate_virial,&
     350              : !$OMP  sphi_b,&
     351              : !$OMP  storage_counter_integrals,stor_count_max_val,swap_id,symm_fac,t2,tmp_block,tmp_i8, tmp_i4,&
     352              : !$OMP  tmp_index,tmp_r_1,tmp_r_2,tmp_screen_pgf1,tmp_screen_pgf2,tmp_task_list,tmp_task_list_cost,tmp_virial,&
     353         2008 : !$OMP  treat_forces_in_core,use_disk_storage,zeta,zetb,zetc,zetd)
     354              : 
     355              :       i_thread = 0
     356              : !$    i_thread = omp_get_thread_num()
     357              : 
     358              :       ln_10 = LOG(10.0_dp)
     359              :       log_2 = LOG10(2.0_dp)
     360              :       actual_x_data => my_x_data(irep, i_thread + 1)
     361              :       do_periodic = actual_x_data%periodic_parameter%do_periodic
     362              : 
     363              :       screening_parameter = actual_x_data%screening_parameter
     364              :       general_parameter = actual_x_data%general_parameter
     365              :       potential_parameter = actual_x_data%potential_parameter
     366              :       basis_info => actual_x_data%basis_info
     367              : 
     368              :       load_balance_parameter => actual_x_data%load_balance_parameter
     369              :       basis_parameter => actual_x_data%basis_parameter
     370              : 
     371              :       ! IF(with_resp_density) THEN
     372              :       !   ! here we also do a copy of the original load_balance_parameter
     373              :       !   ! since if MP2 forces are required after the calculation of the HFX
     374              :       !   ! derivatives we need to calculate again the SCF energy.
     375              :       !   ALLOCATE(load_balance_parameter_energy)
     376              :       !   load_balance_parameter_energy = actual_x_data%load_balance_parameter
     377              :       ! END IF
     378              : 
     379              :       memory_parameter => actual_x_data%memory_parameter
     380              : 
     381              :       cache_size = memory_parameter%cache_size
     382              :       bits_max_val = memory_parameter%bits_max_val
     383              : 
     384              :       use_disk_storage = .FALSE.
     385              : 
     386              :       CALL get_qs_env(qs_env=qs_env, &
     387              :                       atomic_kind_set=atomic_kind_set, &
     388              :                       particle_set=particle_set)
     389              : 
     390              :       max_set = basis_info%max_set
     391              :       max_am = basis_info%max_am
     392              :       natom = SIZE(particle_set, 1)
     393              : 
     394              :       treat_forces_in_core = memory_parameter%treat_forces_in_core
     395              : 
     396              :       hf_fraction = general_parameter%fraction
     397              :       hf_fraction = hf_fraction*my_adiabatic_rescale_factor
     398              :       eps_schwarz = screening_parameter%eps_schwarz_forces
     399              :       IF (eps_schwarz < 0.0_dp) THEN
     400              :          log10_eps_schwarz = log_zero
     401              :       ELSE
     402              :          !! ** make eps_schwarz tighter in case of a stress calculation
     403              :          IF (use_virial) eps_schwarz = eps_schwarz/actual_x_data%periodic_parameter%R_max_stress
     404              :          log10_eps_schwarz = LOG10(eps_schwarz)
     405              :       END IF
     406              :       screen_pmat_forces = screening_parameter%do_p_screening_forces
     407              :       ! don't do density screening in case of response forces
     408              :       IF (with_resp_density) screen_pmat_forces = .FALSE.
     409              : 
     410              :       eps_storage = eps_schwarz*memory_parameter%eps_storage_scaling
     411              : 
     412              :       counter = 0_int_8
     413              : 
     414              :       !! Get screening parameter
     415              : 
     416              :       CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, &
     417              :                                atom_of_kind=atom_of_kind, &
     418              :                                kind_of=kind_of)
     419              : 
     420              :       !! Create helper arrray for mapping local basis functions to global ones
     421              :       ALLOCATE (last_sgf_global(0:natom))
     422              :       last_sgf_global(0) = 0
     423              :       DO iatom = 1, natom
     424              :          ikind = kind_of(iatom)
     425              :          last_sgf_global(iatom) = last_sgf_global(iatom - 1) + basis_parameter(ikind)%nsgf_total
     426              :       END DO
     427              : 
     428              :       ALLOCATE (max_contraction(max_set, natom))
     429              : 
     430              :       max_contraction = 0.0_dp
     431              :       max_pgf = 0
     432              :       DO jatom = 1, natom
     433              :          jkind = kind_of(jatom)
     434              :          lb_max => basis_parameter(jkind)%lmax
     435              :          npgfb => basis_parameter(jkind)%npgf
     436              :          nsetb = basis_parameter(jkind)%nset
     437              :          first_sgfb => basis_parameter(jkind)%first_sgf
     438              :          sphi_b => basis_parameter(jkind)%sphi
     439              :          nsgfb => basis_parameter(jkind)%nsgf
     440              :          DO jset = 1, nsetb
     441              :             ! takes the primitive to contracted transformation into account
     442              :             ncob = npgfb(jset)*ncoset(lb_max(jset))
     443              :             sgfb = first_sgfb(1, jset)
     444              :             ! if the primitives are assumed to be all of max_val2, max_val2*p2s_b becomes
     445              :             ! the maximum value after multiplication with sphi_b
     446              :             max_contraction(jset, jatom) = MAXVAL([(SUM(ABS(sphi_b(1:ncob, i))), i=sgfb, sgfb + nsgfb(jset) - 1)])
     447              :             max_pgf = MAX(max_pgf, npgfb(jset))
     448              :          END DO
     449              :       END DO
     450              : 
     451              :       ncpu = para_env%num_pe
     452              :       n_processes = ncpu*n_threads
     453              : 
     454              :       !! initialize some counters
     455              :       neris_total = 0_int_8
     456              :       neris_incore = 0_int_8
     457              :       neris_onthefly = 0_int_8
     458              :       mem_eris = 0_int_8
     459              :       mem_max_val = 0_int_8
     460              :       compression_factor = 0.0_dp
     461              :       nprim_ints = 0_int_8
     462              :       neris_tmp = 0_int_8
     463              :       max_val_memory = 1_int_8
     464              : 
     465              : !$OMP MASTER
     466              :       !! Set pointer for is_assoc helper
     467              :       shm_is_assoc_atomic_block => actual_x_data%is_assoc_atomic_block
     468              :       shm_number_of_p_entries = actual_x_data%number_of_p_entries
     469              :       shm_atomic_block_offset => actual_x_data%atomic_block_offset
     470              :       shm_set_offset => actual_x_data%set_offset
     471              :       shm_block_offset => actual_x_data%block_offset
     472              : 
     473              :       NULLIFY (full_density_alpha)
     474              :       NULLIFY (full_density_beta)
     475              :       ALLOCATE (full_density_alpha(shm_block_offset(ncpu + 1), nkimages))
     476              : 
     477              :       !! Get the full density from all the processors
     478              :       CALL timeset(routineN//"_getP", handle_getP)
     479              :       CALL get_full_density(para_env, full_density_alpha(:, 1), rho_ao(1, 1)%matrix, shm_number_of_p_entries, &
     480              :                             shm_block_offset, &
     481              :                             kind_of, basis_parameter, get_max_vals_spin=.FALSE., &
     482              :                             antisymmetric=is_anti_symmetric)
     483              :       ! for now only closed shell case
     484              :       IF (with_resp_density) THEN
     485              :          NULLIFY (full_density_resp)
     486              :          ALLOCATE (full_density_resp(shm_block_offset(ncpu + 1)))
     487              :          CALL get_full_density(para_env, full_density_resp, rho_ao_resp(1)%matrix, shm_number_of_p_entries, &
     488              :                                shm_block_offset, &
     489              :                                kind_of, basis_parameter, get_max_vals_spin=.FALSE., &
     490              :                                antisymmetric=is_anti_symmetric)
     491              :       END IF
     492              : 
     493              :       IF (my_nspins == 2) THEN
     494              :          ALLOCATE (full_density_beta(shm_block_offset(ncpu + 1), 1))
     495              :          CALL get_full_density(para_env, full_density_beta(:, 1), rho_ao(2, 1)%matrix, shm_number_of_p_entries, &
     496              :                                shm_block_offset, &
     497              :                                kind_of, basis_parameter, get_max_vals_spin=.FALSE., &
     498              :                                antisymmetric=is_anti_symmetric)
     499              :          ! With resp density
     500              :          IF (with_resp_density) THEN
     501              :             NULLIFY (full_density_resp_beta)
     502              :             ALLOCATE (full_density_resp_beta(shm_block_offset(ncpu + 1)))
     503              :             CALL get_full_density(para_env, full_density_resp_beta, rho_ao_resp(2)%matrix, shm_number_of_p_entries, &
     504              :                                   shm_block_offset, &
     505              :                                   kind_of, basis_parameter, get_max_vals_spin=.FALSE., &
     506              :                                   antisymmetric=is_anti_symmetric)
     507              :          END IF
     508              : 
     509              :       END IF
     510              :       CALL timestop(handle_getP)
     511              : 
     512              :       !! Calculate max entries for screening on actual density. If screen_p_mat_forces = FALSE, the
     513              :       !! matrix is initialized to 1.0
     514              :       IF (screen_pmat_forces) THEN
     515              :          NULLIFY (shm_initial_p)
     516              :          shm_initial_p => actual_x_data%initial_p_forces
     517              :          shm_pmax_atom => actual_x_data%pmax_atom_forces
     518              :          IF (memory_parameter%recalc_forces) THEN
     519              :             CALL update_pmax_mat(shm_initial_p, &
     520              :                                  actual_x_data%map_atom_to_kind_atom, &
     521              :                                  actual_x_data%set_offset, &
     522              :                                  actual_x_data%atomic_block_offset, &
     523              :                                  shm_pmax_atom, &
     524              :                                  full_density_alpha, full_density_beta, &
     525              :                                  natom, kind_of, basis_parameter, &
     526              :                                  nkind, actual_x_data%is_assoc_atomic_block, my_nspins)
     527              :          END IF
     528              :       END IF
     529              : 
     530              :       ! restore as full density the HF density
     531              :       ! maybe in the future
     532              :       IF (with_resp_density .AND. .NOT. my_resp_only) THEN
     533              :          full_density_alpha(:, 1) = full_density_alpha(:, 1) - full_density_resp
     534              :          IF (my_nspins == 2) THEN
     535              :             full_density_beta(:, 1) = &
     536              :                full_density_beta(:, 1) - full_density_resp_beta
     537              :          END IF
     538              :          ! full_density_resp=full_density+full_density_resp
     539              :       END IF
     540              : 
     541              :       screen_coeffs_set => actual_x_data%screen_funct_coeffs_set
     542              :       screen_coeffs_kind => actual_x_data%screen_funct_coeffs_kind
     543              :       screen_coeffs_pgf => actual_x_data%screen_funct_coeffs_pgf
     544              :       radii_pgf => actual_x_data%pair_dist_radii_pgf
     545              :       shm_pmax_block => actual_x_data%pmax_block
     546              : 
     547              : !$OMP END MASTER
     548              : !$OMP BARRIER
     549              : 
     550              : !$OMP MASTER
     551              :       CALL timeset(routineN//"_load", handle_load)
     552              : !$OMP END MASTER
     553              :       !! Load balance the work
     554              :       IF (actual_x_data%memory_parameter%recalc_forces) THEN
     555              :          IF (actual_x_data%b_first_load_balance_forces) THEN
     556              :             CALL hfx_load_balance(actual_x_data, eps_schwarz, particle_set, max_set, para_env, &
     557              :                                   screen_coeffs_set, screen_coeffs_kind, &
     558              :                                   shm_is_assoc_atomic_block, do_periodic, &
     559              :                                   load_balance_parameter, kind_of, basis_parameter, shm_initial_p, &
     560              :                                   shm_pmax_atom, i_thread, n_threads, &
     561              :                                   cell, screen_pmat_forces, actual_x_data%map_atom_to_kind_atom, &
     562              :                                   nkind, hfx_do_eval_forces, shm_pmax_block, use_virial)
     563              :             actual_x_data%b_first_load_balance_forces = .FALSE.
     564              :          ELSE
     565              :             CALL hfx_update_load_balance(actual_x_data, para_env, &
     566              :                                          load_balance_parameter, &
     567              :                                          i_thread, n_threads, hfx_do_eval_forces)
     568              :          END IF
     569              :       END IF
     570              : !$OMP MASTER
     571              :       CALL timestop(handle_load)
     572              : !$OMP END MASTER
     573              : 
     574              :       !! precompute maximum nco and allocate scratch
     575              :       ncos_max = 0
     576              :       nsgf_max = 0
     577              :       DO iatom = 1, natom
     578              :          ikind = kind_of(iatom)
     579              :          nseta = basis_parameter(ikind)%nset
     580              :          npgfa => basis_parameter(ikind)%npgf
     581              :          la_max => basis_parameter(ikind)%lmax
     582              :          nsgfa => basis_parameter(ikind)%nsgf
     583              :          DO iset = 1, nseta
     584              :             ncos_max = MAX(ncos_max, ncoset(la_max(iset)))
     585              :             nsgf_max = MAX(nsgf_max, nsgfa(iset))
     586              :          END DO
     587              :       END DO
     588              : 
     589              :       !! Allocate work arrays
     590              :       ALLOCATE (primitive_forces(12*nsgf_max**4))
     591              :       primitive_forces = 0.0_dp
     592              : 
     593              :       ! ** Allocate buffers for pgf_lists
     594              :       nneighbors = SIZE(actual_x_data%neighbor_cells)
     595              :       ALLOCATE (pgf_list_ij(max_pgf**2))
     596              :       ALLOCATE (pgf_list_kl(max_pgf**2))
     597              :       ALLOCATE (nimages(max_pgf**2))
     598              : 
     599              :       DO i = 1, max_pgf**2
     600              :          ALLOCATE (pgf_list_ij(i)%image_list(nneighbors))
     601              :          ALLOCATE (pgf_list_kl(i)%image_list(nneighbors))
     602              :       END DO
     603              : 
     604              :       ALLOCATE (pbd_buf(nsgf_max**2))
     605              :       ALLOCATE (pbc_buf(nsgf_max**2))
     606              :       ALLOCATE (pad_buf(nsgf_max**2))
     607              :       ALLOCATE (pac_buf(nsgf_max**2))
     608              :       IF (with_resp_density) THEN
     609              :          ALLOCATE (pbd_buf_resp(nsgf_max**2))
     610              :          ALLOCATE (pbc_buf_resp(nsgf_max**2))
     611              :          ALLOCATE (pad_buf_resp(nsgf_max**2))
     612              :          ALLOCATE (pac_buf_resp(nsgf_max**2))
     613              :       END IF
     614              :       IF (my_nspins == 2) THEN
     615              :          ALLOCATE (pbd_buf_beta(nsgf_max**2))
     616              :          ALLOCATE (pbc_buf_beta(nsgf_max**2))
     617              :          ALLOCATE (pad_buf_beta(nsgf_max**2))
     618              :          ALLOCATE (pac_buf_beta(nsgf_max**2))
     619              :          IF (with_resp_density) THEN
     620              :             ALLOCATE (pbd_buf_resp_beta(nsgf_max**2))
     621              :             ALLOCATE (pbc_buf_resp_beta(nsgf_max**2))
     622              :             ALLOCATE (pad_buf_resp_beta(nsgf_max**2))
     623              :             ALLOCATE (pac_buf_resp_beta(nsgf_max**2))
     624              :          END IF
     625              :       END IF
     626              : 
     627              :       ALLOCATE (ede_work(ncos_max**4*12))
     628              :       ALLOCATE (ede_work2(ncos_max**4*12))
     629              :       ALLOCATE (ede_work_forces(ncos_max**4*12))
     630              :       ALLOCATE (ede_buffer1(ncos_max**4))
     631              :       ALLOCATE (ede_buffer2(ncos_max**4))
     632              : 
     633              :       IF (use_virial) THEN
     634              :          ALLOCATE (primitive_forces_virial(12*nsgf_max**4*3))
     635              :          primitive_forces_virial = 0.0_dp
     636              :          ALLOCATE (ede_work_virial(ncos_max**4*12*3))
     637              :          ALLOCATE (ede_work2_virial(ncos_max**4*12*3))
     638              :          tmp_virial = 0.0_dp
     639              :       ELSE
     640              :          ! dummy allocation
     641              :          ALLOCATE (primitive_forces_virial(1))
     642              :          primitive_forces_virial = 0.0_dp
     643              :          ALLOCATE (ede_work_virial(1))
     644              :          ALLOCATE (ede_work2_virial(1))
     645              :       END IF
     646              : 
     647              :       !! Start caluclating integrals of the form (ab|cd) or (ij|kl)
     648              :       !! In order to do so, there is a main four-loop structure that takes into account the two symmetries
     649              :       !!
     650              :       !!   (ab|cd) = (ba|cd) = (ab|dc) = (ba|dc)
     651              :       !!
     652              :       !! by iterating in the following way
     653              :       !!
     654              :       !! DO iatom=1,natom               and       DO katom=1,natom
     655              :       !!   DO jatom=iatom,natom                     DO latom=katom,natom
     656              :       !!
     657              :       !! The third symmetry
     658              :       !!
     659              :       !!  (ab|cd) = (cd|ab)
     660              :       !!
     661              :       !! is taken into account by the following criterion:
     662              :       !!
     663              :       !! IF(katom+latom<=iatom+jatom)  THEN
     664              :       !!   IF( ((iatom+jatom)==(katom+latom) ) .AND.(katom<iatom)) CYCLE
     665              :       !!
     666              :       !! Furthermore, if iatom==jatom==katom==latom we cycle, because the derivatives are zero anyway.
     667              :       !!
     668              :       !! Depending on the degeneracy of an integral the exchange contribution is multiplied by a corresponding
     669              :       !! factor ( symm_fac ).
     670              :       !!
     671              :       !! If one quartet does not pass the screening we CYCLE on the outer most possible loop. Thats why we use
     672              :       !! different hierarchies of short range screening matrices.
     673              :       !!
     674              :       !! If we do a parallel run, each process owns a unique array of workloads. Here, a workload is
     675              :       !! defined as :
     676              :       !!
     677              :       !! istart, jstart, kstart, lstart, number_of_atom_quartets, initial_cpu_id
     678              :       !!
     679              :       !! This tells the process where to start the main loops and how many bunches of integrals it has to
     680              :       !! calculate. The original parallelization is a simple modulo distribution that is binned and
     681              :       !! optimized in the load_balance routines. Since the Monte Carlo routines can swap processors,
     682              :       !! we need to know which was the initial cpu_id.
     683              :       !! Furthermore, the indices iatom, jatom, katom, latom have to be set to istart, jstart, kstart and
     684              :       !! lstart only the first time the loop is executed. All subsequent loops have to start with one or
     685              :       !! iatom and katom respectively. Therefore, we use flags like first_j_loop etc.
     686              : 
     687              : !$OMP BARRIER
     688              : !$OMP MASTER
     689              :       CALL timeset(routineN//"_main", handle_main)
     690              : !$OMP END MASTER
     691              : !$OMP BARRIER
     692              : 
     693              :       coeffs_kind_max0 = MAXVAL(screen_coeffs_kind(:, :)%x(2))
     694              :       ALLOCATE (set_list_ij((max_set*load_balance_parameter%block_size)**2))
     695              :       ALLOCATE (set_list_kl((max_set*load_balance_parameter%block_size)**2))
     696              : 
     697              : !$OMP BARRIER
     698              : 
     699              :       IF (actual_x_data%memory_parameter%recalc_forces) THEN
     700              :          actual_x_data%distribution_forces%ram_counter = HUGE(distribution_forces%ram_counter)
     701              :          memory_parameter%ram_counter_forces = HUGE(memory_parameter%ram_counter_forces)
     702              :       END IF
     703              : 
     704              :       IF (treat_forces_in_core) THEN
     705              :          buffer_overflow = .FALSE.
     706              :       ELSE
     707              :          buffer_overflow = .TRUE.
     708              :       END IF
     709              : 
     710              :       do_dynamic_load_balancing = .TRUE.
     711              :       IF (n_threads == 1) do_dynamic_load_balancing = .FALSE.
     712              : 
     713              :       IF (do_dynamic_load_balancing) THEN
     714              :          my_bin_size = SIZE(actual_x_data%distribution_forces)
     715              :       ELSE
     716              :          my_bin_size = 1
     717              :       END IF
     718              :       !! We do not need the containers if MAX_MEM = 0
     719              :       IF (treat_forces_in_core) THEN
     720              :          !! IF new md step -> reinitialize containers
     721              :          IF (actual_x_data%memory_parameter%recalc_forces) THEN
     722              :             CALL dealloc_containers(actual_x_data%store_forces, memory_parameter%actual_memory_usage)
     723              :             CALL alloc_containers(actual_x_data%store_forces, my_bin_size)
     724              : 
     725              :             DO bin = 1, my_bin_size
     726              :                maxval_container => actual_x_data%store_forces%maxval_container(bin)
     727              :                integral_containers => actual_x_data%store_forces%integral_containers(:, bin)
     728              :                CALL hfx_init_container(maxval_container, memory_parameter%actual_memory_usage, .FALSE.)
     729              :                DO i = 1, 64
     730              :                   CALL hfx_init_container(integral_containers(i), memory_parameter%actual_memory_usage, .FALSE.)
     731              :                END DO
     732              :             END DO
     733              :          END IF
     734              : 
     735              :          !! Decompress the first cache for maxvals and integrals
     736              :          IF (.NOT. actual_x_data%memory_parameter%recalc_forces) THEN
     737              :             DO bin = 1, my_bin_size
     738              :                maxval_cache => actual_x_data%store_forces%maxval_cache(bin)
     739              :                maxval_container => actual_x_data%store_forces%maxval_container(bin)
     740              :                integral_caches => actual_x_data%store_forces%integral_caches(:, bin)
     741              :                integral_containers => actual_x_data%store_forces%integral_containers(:, bin)
     742              :                CALL hfx_decompress_first_cache(bits_max_val, maxval_cache, maxval_container, &
     743              :                                                memory_parameter%actual_memory_usage, .FALSE.)
     744              :                DO i = 1, 64
     745              :                   CALL hfx_decompress_first_cache(i, integral_caches(i), integral_containers(i), &
     746              :                                                   memory_parameter%actual_memory_usage, .FALSE.)
     747              :                END DO
     748              :             END DO
     749              :          END IF
     750              :       END IF
     751              : 
     752              : !$OMP BARRIER
     753              : !$OMP MASTER
     754              :       IF (do_dynamic_load_balancing) THEN
     755              :          ! ** Lets construct the task list
     756              :          shm_total_bins = 0
     757              :          DO i = 1, n_threads
     758              :             shm_total_bins = shm_total_bins + SIZE(my_x_data(irep, i)%distribution_forces)
     759              :          END DO
     760              :          ALLOCATE (tmp_task_list(shm_total_bins))
     761              :          shm_task_counter = 0
     762              :          DO i = 1, n_threads
     763              :             DO bin = 1, SIZE(my_x_data(irep, i)%distribution_forces)
     764              :                shm_task_counter = shm_task_counter + 1
     765              :                tmp_task_list(shm_task_counter)%thread_id = i
     766              :                tmp_task_list(shm_task_counter)%bin_id = bin
     767              :                tmp_task_list(shm_task_counter)%cost = my_x_data(irep, i)%distribution_forces(bin)%cost
     768              :             END DO
     769              :          END DO
     770              : 
     771              :          ! ** Now sort the task list
     772              :          ALLOCATE (tmp_task_list_cost(shm_total_bins))
     773              :          ALLOCATE (tmp_index(shm_total_bins))
     774              : 
     775              :          DO i = 1, shm_total_bins
     776              :             tmp_task_list_cost(i) = tmp_task_list(i)%cost
     777              :          END DO
     778              : 
     779              :          CALL sort(tmp_task_list_cost, shm_total_bins, tmp_index)
     780              : 
     781              :          ALLOCATE (actual_x_data%task_list(shm_total_bins))
     782              : 
     783              :          DO i = 1, shm_total_bins
     784              :             actual_x_data%task_list(i) = tmp_task_list(tmp_index(shm_total_bins - i + 1))
     785              :          END DO
     786              : 
     787              :          shm_task_list => actual_x_data%task_list
     788              :          shm_task_counter = 0
     789              : 
     790              :          DEALLOCATE (tmp_task_list_cost, tmp_index, tmp_task_list)
     791              :       END IF
     792              : 
     793              :       !! precalculate maximum density matrix elements in blocks
     794              :       shm_pmax_block => actual_x_data%pmax_block
     795              :       actual_x_data%pmax_block = 0.0_dp
     796              :       IF (screen_pmat_forces) THEN
     797              :          DO iatom_block = 1, SIZE(actual_x_data%blocks)
     798              :             iatom_start = actual_x_data%blocks(iatom_block)%istart
     799              :             iatom_end = actual_x_data%blocks(iatom_block)%iend
     800              :             DO jatom_block = 1, SIZE(actual_x_data%blocks)
     801              :                jatom_start = actual_x_data%blocks(jatom_block)%istart
     802              :                jatom_end = actual_x_data%blocks(jatom_block)%iend
     803              :                shm_pmax_block(iatom_block, jatom_block) = MAXVAL(shm_pmax_atom(iatom_start:iatom_end, jatom_start:jatom_end))
     804              :             END DO
     805              :          END DO
     806              :       END IF
     807              :       shm_atomic_pair_list => actual_x_data%atomic_pair_list_forces
     808              :       IF (actual_x_data%memory_parameter%recalc_forces) THEN
     809              :          CALL build_atomic_pair_list(natom, shm_atomic_pair_list, kind_of, basis_parameter, particle_set, &
     810              :                                      do_periodic, screen_coeffs_kind, coeffs_kind_max0, log10_eps_schwarz, cell, &
     811              :                                      actual_x_data%blocks)
     812              :       END IF
     813              : 
     814              :       my_bin_size = SIZE(actual_x_data%distribution_forces)
     815              :       DO bin = 1, my_bin_size
     816              :          actual_x_data%distribution_forces(bin)%time_forces = 0.0_dp
     817              :       END DO
     818              : !$OMP END MASTER
     819              : !$OMP BARRIER
     820              : 
     821              :       IF (treat_forces_in_core) THEN
     822              :          IF (my_bin_size > 0) THEN
     823              :             maxval_container => actual_x_data%store_forces%maxval_container(1)
     824              :             maxval_cache => actual_x_data%store_forces%maxval_cache(1)
     825              : 
     826              :             integral_containers => actual_x_data%store_forces%integral_containers(:, 1)
     827              :             integral_caches => actual_x_data%store_forces%integral_caches(:, 1)
     828              :          END IF
     829              :       END IF
     830              : 
     831              :       nblocks = load_balance_parameter%nblocks
     832              : 
     833              :       bins_left = .TRUE.
     834              :       do_it = .TRUE.
     835              :       bin = 0
     836              :       DO WHILE (bins_left)
     837              :          IF (.NOT. do_dynamic_load_balancing) THEN
     838              :             bin = bin + 1
     839              :             IF (bin > my_bin_size) THEN
     840              :                do_it = .FALSE.
     841              :                bins_left = .FALSE.
     842              :             ELSE
     843              :                do_it = .TRUE.
     844              :                bins_left = .TRUE.
     845              :                distribution_forces => actual_x_data%distribution_forces(bin)
     846              :             END IF
     847              :          ELSE
     848              : !$OMP CRITICAL(hfxderivatives_critical)
     849              :             shm_task_counter = shm_task_counter + 1
     850              :             IF (shm_task_counter <= shm_total_bins) THEN
     851              :                my_thread_id = shm_task_list(shm_task_counter)%thread_id
     852              :                my_bin_id = shm_task_list(shm_task_counter)%bin_id
     853              :                IF (treat_forces_in_core) THEN
     854              :                   maxval_cache => my_x_data(irep, my_thread_id)%store_forces%maxval_cache(my_bin_id)
     855              :                   maxval_container => my_x_data(irep, my_thread_id)%store_forces%maxval_container(my_bin_id)
     856              :                   integral_caches => my_x_data(irep, my_thread_id)%store_forces%integral_caches(:, my_bin_id)
     857              :                   integral_containers => my_x_data(irep, my_thread_id)%store_forces%integral_containers(:, my_bin_id)
     858              :                END IF
     859              :                distribution_forces => my_x_data(irep, my_thread_id)%distribution_forces(my_bin_id)
     860              :                do_it = .TRUE.
     861              :                bins_left = .TRUE.
     862              :                IF (actual_x_data%memory_parameter%recalc_forces) THEN
     863              :                   distribution_forces%ram_counter = HUGE(distribution_forces%ram_counter)
     864              :                END IF
     865              :                counter = 0_Int_8
     866              :             ELSE
     867              :                do_it = .FALSE.
     868              :                bins_left = .FALSE.
     869              :             END IF
     870              : !$OMP END CRITICAL(hfxderivatives_critical)
     871              :          END IF
     872              : 
     873              :          IF (.NOT. do_it) CYCLE
     874              : !$OMP MASTER
     875              :          CALL timeset(routineN//"_bin", handle_bin)
     876              : !$OMP END MASTER
     877              :          bintime_start = m_walltime()
     878              :          my_istart = distribution_forces%istart
     879              :          my_current_counter = 0
     880              :          IF (distribution_forces%number_of_atom_quartets == 0 .OR. &
     881              :              my_istart == -1_int_8) my_istart = nblocks**4
     882              :          atomic_blocks: DO atom_block = my_istart, nblocks**4 - 1, n_processes
     883              :             latom_block = INT(MODULO(atom_block, nblocks)) + 1
     884              :             tmp_block = atom_block/nblocks
     885              :             katom_block = INT(MODULO(tmp_block, nblocks)) + 1
     886              :             IF (latom_block < katom_block) CYCLE atomic_blocks
     887              :             tmp_block = tmp_block/nblocks
     888              :             jatom_block = INT(MODULO(tmp_block, nblocks)) + 1
     889              :             tmp_block = tmp_block/nblocks
     890              :             iatom_block = INT(MODULO(tmp_block, nblocks)) + 1
     891              :             IF (jatom_block < iatom_block) CYCLE atomic_blocks
     892              :             my_current_counter = my_current_counter + 1
     893              :             IF (my_current_counter > distribution_forces%number_of_atom_quartets) EXIT atomic_blocks
     894              : 
     895              :             iatom_start = actual_x_data%blocks(iatom_block)%istart
     896              :             iatom_end = actual_x_data%blocks(iatom_block)%iend
     897              :             jatom_start = actual_x_data%blocks(jatom_block)%istart
     898              :             jatom_end = actual_x_data%blocks(jatom_block)%iend
     899              :             katom_start = actual_x_data%blocks(katom_block)%istart
     900              :             katom_end = actual_x_data%blocks(katom_block)%iend
     901              :             latom_start = actual_x_data%blocks(latom_block)%istart
     902              :             latom_end = actual_x_data%blocks(latom_block)%iend
     903              : 
     904              :             pmax_blocks = MAX(shm_pmax_block(katom_block, iatom_block) + &
     905              :                               shm_pmax_block(latom_block, jatom_block), &
     906              :                               shm_pmax_block(latom_block, iatom_block) + &
     907              :                               shm_pmax_block(katom_block, jatom_block))
     908              : 
     909              :             IF (2.0_dp*coeffs_kind_max0 + pmax_blocks < log10_eps_schwarz) CYCLE atomic_blocks
     910              : 
     911              :             CALL build_pair_list(natom, list_ij, set_list_ij, iatom_start, iatom_end, &
     912              :                                  jatom_start, jatom_end, &
     913              :                                  kind_of, basis_parameter, particle_set, &
     914              :                                  do_periodic, screen_coeffs_set, screen_coeffs_kind, coeffs_kind_max0, &
     915              :                                  log10_eps_schwarz, cell, pmax_blocks, shm_atomic_pair_list)
     916              : 
     917              :             CALL build_pair_list(natom, list_kl, set_list_kl, katom_start, katom_end, &
     918              :                                  latom_start, latom_end, &
     919              :                                  kind_of, basis_parameter, particle_set, &
     920              :                                  do_periodic, screen_coeffs_set, screen_coeffs_kind, coeffs_kind_max0, &
     921              :                                  log10_eps_schwarz, cell, pmax_blocks, shm_atomic_pair_list)
     922              : 
     923              :             DO i_list_ij = 1, list_ij%n_element
     924              :                iatom = list_ij%elements(i_list_ij)%pair(1)
     925              :                jatom = list_ij%elements(i_list_ij)%pair(2)
     926              :                i_set_list_ij_start = list_ij%elements(i_list_ij)%set_bounds(1)
     927              :                i_set_list_ij_stop = list_ij%elements(i_list_ij)%set_bounds(2)
     928              :                ikind = list_ij%elements(i_list_ij)%kind_pair(1)
     929              :                jkind = list_ij%elements(i_list_ij)%kind_pair(2)
     930              :                ra = list_ij%elements(i_list_ij)%r1
     931              :                rb = list_ij%elements(i_list_ij)%r2
     932              :                rab2 = list_ij%elements(i_list_ij)%dist2
     933              : 
     934              :                la_max => basis_parameter(ikind)%lmax
     935              :                la_min => basis_parameter(ikind)%lmin
     936              :                npgfa => basis_parameter(ikind)%npgf
     937              :                nseta = basis_parameter(ikind)%nset
     938              :                zeta => basis_parameter(ikind)%zet
     939              :                nsgfa => basis_parameter(ikind)%nsgf
     940              : 
     941              :                lb_max => basis_parameter(jkind)%lmax
     942              :                lb_min => basis_parameter(jkind)%lmin
     943              :                npgfb => basis_parameter(jkind)%npgf
     944              :                nsetb = basis_parameter(jkind)%nset
     945              :                zetb => basis_parameter(jkind)%zet
     946              :                nsgfb => basis_parameter(jkind)%nsgf
     947              : 
     948              :                i_atom = atom_of_kind(iatom)
     949              :                forces_map(1, 1) = ikind
     950              :                forces_map(1, 2) = i_atom
     951              :                j_atom = atom_of_kind(jatom)
     952              :                forces_map(2, 1) = jkind
     953              :                forces_map(2, 2) = j_atom
     954              : 
     955              :                DO i_list_kl = 1, list_kl%n_element
     956              :                   katom = list_kl%elements(i_list_kl)%pair(1)
     957              :                   latom = list_kl%elements(i_list_kl)%pair(2)
     958              : 
     959              :                   !All four centers equivalent => zero-contribution
     960              : !VIIRAL            IF((iatom==jatom .AND. iatom==katom .AND. iatom==latom)) CYCLE
     961              :                   IF (.NOT. (katom + latom <= iatom + jatom)) CYCLE
     962              :                   IF (((iatom + jatom) == (katom + latom)) .AND. (katom < iatom)) CYCLE
     963              : 
     964              :                   i_set_list_kl_start = list_kl%elements(i_list_kl)%set_bounds(1)
     965              :                   i_set_list_kl_stop = list_kl%elements(i_list_kl)%set_bounds(2)
     966              :                   kkind = list_kl%elements(i_list_kl)%kind_pair(1)
     967              :                   lkind = list_kl%elements(i_list_kl)%kind_pair(2)
     968              :                   rc = list_kl%elements(i_list_kl)%r1
     969              :                   rd = list_kl%elements(i_list_kl)%r2
     970              :                   rcd2 = list_kl%elements(i_list_kl)%dist2
     971              : 
     972              :                   IF (screen_pmat_forces) THEN
     973              :                      pmax_atom = MAX(shm_pmax_atom(katom, iatom) + &
     974              :                                      shm_pmax_atom(latom, jatom), &
     975              :                                      shm_pmax_atom(latom, iatom) + &
     976              :                                      shm_pmax_atom(katom, jatom))
     977              :                   ELSE
     978              :                      pmax_atom = 0.0_dp
     979              :                   END IF
     980              : 
     981              :                   IF ((screen_coeffs_kind(jkind, ikind)%x(1)*rab2 + &
     982              :                        screen_coeffs_kind(jkind, ikind)%x(2)) + &
     983              :                       (screen_coeffs_kind(lkind, kkind)%x(1)*rcd2 + &
     984              :                        screen_coeffs_kind(lkind, kkind)%x(2)) + pmax_atom < log10_eps_schwarz) CYCLE
     985              : 
     986              :                   IF (.NOT. (shm_is_assoc_atomic_block(latom, iatom) >= 1 .AND. &
     987              :                              shm_is_assoc_atomic_block(katom, iatom) >= 1 .AND. &
     988              :                              shm_is_assoc_atomic_block(katom, jatom) >= 1 .AND. &
     989              :                              shm_is_assoc_atomic_block(latom, jatom) >= 1)) CYCLE
     990              :                   k_atom = atom_of_kind(katom)
     991              :                   forces_map(3, 1) = kkind
     992              :                   forces_map(3, 2) = k_atom
     993              : 
     994              :                   l_atom = atom_of_kind(latom)
     995              :                   forces_map(4, 1) = lkind
     996              :                   forces_map(4, 2) = l_atom
     997              : 
     998              :                   IF (my_nspins == 1) THEN
     999              :                      fac = 0.25_dp*hf_fraction
    1000              :                   ELSE
    1001              :                      fac = 0.5_dp*hf_fraction
    1002              :                   END IF
    1003              :                   !calculate symmetry_factor
    1004              :                   symm_fac = 0.25_dp
    1005              :                   IF (iatom == jatom) symm_fac = symm_fac*2.0_dp
    1006              :                   IF (katom == latom) symm_fac = symm_fac*2.0_dp
    1007              :                   IF (iatom == katom .AND. jatom == latom .AND. &
    1008              :                       iatom /= jatom .AND. katom /= latom) symm_fac = symm_fac*2.0_dp
    1009              :                   IF (iatom == katom .AND. iatom == jatom .AND. &
    1010              :                       katom == latom) symm_fac = symm_fac*2.0_dp
    1011              : 
    1012              :                   symm_fac = 1.0_dp/symm_fac
    1013              :                   fac = fac*symm_fac
    1014              : 
    1015              :                   lc_max => basis_parameter(kkind)%lmax
    1016              :                   lc_min => basis_parameter(kkind)%lmin
    1017              :                   npgfc => basis_parameter(kkind)%npgf
    1018              :                   zetc => basis_parameter(kkind)%zet
    1019              :                   nsgfc => basis_parameter(kkind)%nsgf
    1020              : 
    1021              :                   ld_max => basis_parameter(lkind)%lmax
    1022              :                   ld_min => basis_parameter(lkind)%lmin
    1023              :                   npgfd => basis_parameter(lkind)%npgf
    1024              :                   zetd => basis_parameter(lkind)%zet
    1025              :                   nsgfd => basis_parameter(lkind)%nsgf
    1026              : 
    1027              :                   atomic_offset_bd = shm_atomic_block_offset(jatom, latom)
    1028              :                   atomic_offset_bc = shm_atomic_block_offset(jatom, katom)
    1029              :                   atomic_offset_ad = shm_atomic_block_offset(iatom, latom)
    1030              :                   atomic_offset_ac = shm_atomic_block_offset(iatom, katom)
    1031              : 
    1032              :                   IF (jatom < latom) THEN
    1033              :                      offset_bd_set => shm_set_offset(:, :, lkind, jkind)
    1034              :                   ELSE
    1035              :                      offset_bd_set => shm_set_offset(:, :, jkind, lkind)
    1036              :                   END IF
    1037              :                   IF (jatom < katom) THEN
    1038              :                      offset_bc_set => shm_set_offset(:, :, kkind, jkind)
    1039              :                   ELSE
    1040              :                      offset_bc_set => shm_set_offset(:, :, jkind, kkind)
    1041              :                   END IF
    1042              :                   IF (iatom < latom) THEN
    1043              :                      offset_ad_set => shm_set_offset(:, :, lkind, ikind)
    1044              :                   ELSE
    1045              :                      offset_ad_set => shm_set_offset(:, :, ikind, lkind)
    1046              :                   END IF
    1047              :                   IF (iatom < katom) THEN
    1048              :                      offset_ac_set => shm_set_offset(:, :, kkind, ikind)
    1049              :                   ELSE
    1050              :                      offset_ac_set => shm_set_offset(:, :, ikind, kkind)
    1051              :                   END IF
    1052              : 
    1053              :                   IF (screen_pmat_forces) THEN
    1054              :                      swap_id = 16
    1055              :                      kind_kind_idx = INT(get_1D_idx(kkind, ikind, INT(nkind, int_8)))
    1056              :                      IF (ikind >= kkind) THEN
    1057              :                         ptr_p_1 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1058              :                                                                        actual_x_data%map_atom_to_kind_atom(katom), &
    1059              :                                                                        actual_x_data%map_atom_to_kind_atom(iatom))
    1060              :                      ELSE
    1061              :                         ptr_p_1 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1062              :                                                                        actual_x_data%map_atom_to_kind_atom(iatom), &
    1063              :                                                                        actual_x_data%map_atom_to_kind_atom(katom))
    1064              :                         swap_id = swap_id + 1
    1065              :                      END IF
    1066              :                      kind_kind_idx = INT(get_1D_idx(lkind, jkind, INT(nkind, int_8)))
    1067              :                      IF (jkind >= lkind) THEN
    1068              :                         ptr_p_2 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1069              :                                                                        actual_x_data%map_atom_to_kind_atom(latom), &
    1070              :                                                                        actual_x_data%map_atom_to_kind_atom(jatom))
    1071              :                      ELSE
    1072              :                         ptr_p_2 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1073              :                                                                        actual_x_data%map_atom_to_kind_atom(jatom), &
    1074              :                                                                        actual_x_data%map_atom_to_kind_atom(latom))
    1075              :                         swap_id = swap_id + 2
    1076              :                      END IF
    1077              :                      kind_kind_idx = INT(get_1D_idx(lkind, ikind, INT(nkind, int_8)))
    1078              :                      IF (ikind >= lkind) THEN
    1079              :                         ptr_p_3 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1080              :                                                                        actual_x_data%map_atom_to_kind_atom(latom), &
    1081              :                                                                        actual_x_data%map_atom_to_kind_atom(iatom))
    1082              :                      ELSE
    1083              :                         ptr_p_3 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1084              :                                                                        actual_x_data%map_atom_to_kind_atom(iatom), &
    1085              :                                                                        actual_x_data%map_atom_to_kind_atom(latom))
    1086              :                         swap_id = swap_id + 4
    1087              :                      END IF
    1088              :                      kind_kind_idx = INT(get_1D_idx(kkind, jkind, INT(nkind, int_8)))
    1089              :                      IF (jkind >= kkind) THEN
    1090              :                         ptr_p_4 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1091              :                                                                        actual_x_data%map_atom_to_kind_atom(katom), &
    1092              :                                                                        actual_x_data%map_atom_to_kind_atom(jatom))
    1093              :                      ELSE
    1094              :                         ptr_p_4 => shm_initial_p(kind_kind_idx)%p_kind(:, :, &
    1095              :                                                                        actual_x_data%map_atom_to_kind_atom(jatom), &
    1096              :                                                                        actual_x_data%map_atom_to_kind_atom(katom))
    1097              :                         swap_id = swap_id + 8
    1098              :                      END IF
    1099              :                   END IF
    1100              : 
    1101              :                   !! At this stage, check for memory used in compression
    1102              : 
    1103              :                   IF (actual_x_data%memory_parameter%recalc_forces) THEN
    1104              :                      IF (treat_forces_in_core) THEN
    1105              :                         ! ** We know the maximum amount of integrals that we can store per MPI-process
    1106              :                         ! ** Now we need to sum the current memory usage among all openMP threads
    1107              :                         ! ** We can just read what is currently stored on the corresponding x_data type
    1108              :                         ! ** If this is thread i and it tries to read the data from thread j, that is
    1109              :                         ! ** currently writing that data, we just dont care, because the possible error
    1110              :                         ! ** is of the order of CACHE_SIZE
    1111              :                         mem_compression_counter = 0
    1112              :                         DO i = 1, n_threads
    1113              : !$OMP                   ATOMIC READ
    1114              :                            tmp_i4 = my_x_data(irep, i)%memory_parameter%actual_memory_usage
    1115              :                            mem_compression_counter = mem_compression_counter + &
    1116              :                                                      tmp_i4*memory_parameter%cache_size
    1117              :                         END DO
    1118              :                         IF (mem_compression_counter + memory_parameter%final_comp_counter_energy &
    1119              :                             > memory_parameter%max_compression_counter) THEN
    1120              :                            buffer_overflow = .TRUE.
    1121              :                            IF (do_dynamic_load_balancing) THEN
    1122              :                               distribution_forces%ram_counter = counter
    1123              :                            ELSE
    1124              :                               memory_parameter%ram_counter_forces = counter
    1125              :                            END IF
    1126              :                         ELSE
    1127              :                            counter = counter + 1
    1128              :                            buffer_overflow = .FALSE.
    1129              :                         END IF
    1130              :                      END IF
    1131              :                   ELSE
    1132              :                      IF (treat_forces_in_core) THEN
    1133              :                         IF (do_dynamic_load_balancing) THEN
    1134              :                            IF (distribution_forces%ram_counter == counter) THEN
    1135              :                               buffer_overflow = .TRUE.
    1136              :                            ELSE
    1137              :                               counter = counter + 1
    1138              :                               buffer_overflow = .FALSE.
    1139              :                            END IF
    1140              :                         ELSE
    1141              :                            IF (memory_parameter%ram_counter_forces == counter) THEN
    1142              :                               buffer_overflow = .TRUE.
    1143              :                            ELSE
    1144              :                               counter = counter + 1
    1145              :                               buffer_overflow = .FALSE.
    1146              :                            END IF
    1147              :                         END IF
    1148              :                      END IF
    1149              :                   END IF
    1150              : 
    1151              :                   DO i_set_list_ij = i_set_list_ij_start, i_set_list_ij_stop
    1152              :                      iset = set_list_ij(i_set_list_ij)%pair(1)
    1153              :                      jset = set_list_ij(i_set_list_ij)%pair(2)
    1154              : 
    1155              :                      ncob = npgfb(jset)*ncoset(lb_max(jset))
    1156              :                      max_val1 = screen_coeffs_set(jset, iset, jkind, ikind)%x(1)*rab2 + &
    1157              :                                 screen_coeffs_set(jset, iset, jkind, ikind)%x(2)
    1158              :                      !! Near field screening
    1159              :                      IF (max_val1 + (screen_coeffs_kind(lkind, kkind)%x(1)*rcd2 + &
    1160              :                                      screen_coeffs_kind(lkind, kkind)%x(2)) + pmax_atom < log10_eps_schwarz) CYCLE
    1161              : 
    1162              :                      DO i_set_list_kl = i_set_list_kl_start, i_set_list_kl_stop
    1163              :                         kset = set_list_kl(i_set_list_kl)%pair(1)
    1164              :                         lset = set_list_kl(i_set_list_kl)%pair(2)
    1165              : 
    1166              :                         max_val2_set = (screen_coeffs_set(lset, kset, lkind, kkind)%x(1)*rcd2 + &
    1167              :                                         screen_coeffs_set(lset, kset, lkind, kkind)%x(2))
    1168              :                         max_val2 = max_val1 + max_val2_set
    1169              : 
    1170              :                         !! Near field screening
    1171              :                         IF (max_val2 + pmax_atom < log10_eps_schwarz) CYCLE
    1172              : 
    1173              :                         IF (screen_pmat_forces) THEN
    1174              :                            CALL get_pmax_val(ptr_p_1, ptr_p_2, ptr_p_3, ptr_p_4, &
    1175              :                                              iset, jset, kset, lset, &
    1176              :                                              pmax_tmp, swap_id)
    1177              : 
    1178              :                            log10_pmax = log_2 + pmax_tmp
    1179              :                         ELSE
    1180              :                            log10_pmax = 0.0_dp
    1181              :                         END IF
    1182              : 
    1183              :                         max_val2 = max_val2 + log10_pmax
    1184              :                         IF (max_val2 < log10_eps_schwarz) CYCLE
    1185              : 
    1186              :                         pmax_entry = EXP(log10_pmax*ln_10)
    1187              :                         !! store current number of integrals, update total number and number of integrals in buffer
    1188              :                         current_counter = nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset)*12
    1189              :                         IF (buffer_overflow) THEN
    1190              :                            neris_onthefly = neris_onthefly + current_counter
    1191              :                         END IF
    1192              : 
    1193              :                         !! Get integrals from buffer and update Kohn-Sham matrix
    1194              :                         IF (.NOT. buffer_overflow .AND. .NOT. actual_x_data%memory_parameter%recalc_forces) THEN
    1195              :                            nints = current_counter
    1196              :                            CALL hfx_get_single_cache_element(estimate_to_store_int, 6, &
    1197              :                                                              maxval_cache, maxval_container, memory_parameter%actual_memory_usage, &
    1198              :                                                              use_disk_storage)
    1199              :                            spherical_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int + 1)
    1200              : !                           IF (spherical_estimate*pmax_entry < eps_schwarz) CYCLE
    1201              :                            IF (.NOT. use_virial) THEN
    1202              :                               IF (spherical_estimate*pmax_entry < eps_schwarz) CYCLE
    1203              :                            END IF
    1204              :                            nbits = EXPONENT(ANINT(spherical_estimate*pmax_entry/eps_storage)) + 1
    1205              :                            buffer_left = nints
    1206              :                            buffer_start = 1
    1207              :                            neris_incore = neris_incore + INT(nints, int_8)
    1208              :                            DO WHILE (buffer_left > 0)
    1209              :                               buffer_size = MIN(buffer_left, cache_size)
    1210              :                               CALL hfx_get_mult_cache_elements(primitive_forces(buffer_start), &
    1211              :                                                                buffer_size, nbits, &
    1212              :                                                                integral_caches(nbits), &
    1213              :                                                                integral_containers(nbits), &
    1214              :                                                                eps_storage, pmax_entry, &
    1215              :                                                                memory_parameter%actual_memory_usage, &
    1216              :                                                                use_disk_storage)
    1217              :                               buffer_left = buffer_left - buffer_size
    1218              :                               buffer_start = buffer_start + buffer_size
    1219              :                            END DO
    1220              :                         END IF
    1221              :                         !! Calculate integrals if we run out of buffer or the geometry did change
    1222              :                         IF (actual_x_data%memory_parameter%recalc_forces .OR. buffer_overflow) THEN
    1223              :                            max_contraction_val = max_contraction(iset, iatom)* &
    1224              :                                                  max_contraction(jset, jatom)* &
    1225              :                                                  max_contraction(kset, katom)* &
    1226              :                                                  max_contraction(lset, latom)* &
    1227              :                                                  pmax_entry
    1228              :                            tmp_R_1 => radii_pgf(:, :, jset, iset, jkind, ikind)
    1229              :                            tmp_R_2 => radii_pgf(:, :, lset, kset, lkind, kkind)
    1230              :                            tmp_screen_pgf1 => screen_coeffs_pgf(:, :, jset, iset, jkind, ikind)
    1231              :                            tmp_screen_pgf2 => screen_coeffs_pgf(:, :, lset, kset, lkind, kkind)
    1232              : 
    1233              :                            CALL forces4(actual_x_data%eri4_context, ra, rb, rc, rd, npgfa(iset), npgfb(jset), &
    1234              :                                         npgfc(kset), npgfd(lset), &
    1235              :                                         la_min(iset), la_max(iset), lb_min(jset), lb_max(jset), &
    1236              :                                         lc_min(kset), lc_max(kset), ld_min(lset), ld_max(lset), &
    1237              :                                         nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1238              :                                         zeta(1:npgfa(iset), iset), zetb(1:npgfb(jset), jset), &
    1239              :                                         zetc(1:npgfc(kset), kset), zetd(1:npgfd(lset), lset), &
    1240              :                                         primitive_forces, &
    1241              :                                         potential_parameter, &
    1242              :                                         actual_x_data%neighbor_cells, &
    1243              :                                         screen_coeffs_set(jset, iset, jkind, ikind)%x, &
    1244              :                                         screen_coeffs_set(lset, kset, lkind, kkind)%x, eps_schwarz, &
    1245              :                                         max_contraction_val, cartesian_estimate, cell, neris_tmp, &
    1246              :                                         log10_pmax, log10_eps_schwarz, &
    1247              :                                         tmp_R_1, tmp_R_2, tmp_screen_pgf1, tmp_screen_pgf2, &
    1248              :                                         pgf_list_ij, pgf_list_kl, &
    1249              :                                         basis_parameter(ikind)%contraction(:, iset), basis_parameter(jkind)%contraction(:, jset), &
    1250              :                                         basis_parameter(kkind)%contraction(:, kset), basis_parameter(lkind)%contraction(:, lset), &
    1251              :                                         ede_work, ede_work2, ede_work_forces, &
    1252              :                                         ede_buffer1, ede_buffer2, &
    1253              :                                         nimages, do_periodic, use_virial, ede_work_virial, ede_work2_virial, &
    1254              :                                         primitive_forces_virial, &
    1255              :                                         cartesian_estimate_virial)
    1256              : 
    1257              :                            nints = nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset)*12
    1258              :                            neris_total = neris_total + nints
    1259              :                            nprim_ints = nprim_ints + neris_tmp
    1260              : !                           IF (cartesian_estimate == 0.0_dp) cartesian_estimate = TINY(cartesian_estimate)
    1261              : !                           estimate_to_store_int = EXPONENT(cartesian_estimate)
    1262              : !                           estimate_to_store_int = MAX(estimate_to_store_int, -15_int_8)
    1263              : !                           cartesian_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int+1)
    1264              : !                           IF (.NOT. buffer_overflow .AND. actual_x_data%memory_parameter%recalc_forces) THEN
    1265              : !                              IF (cartesian_estimate < eps_schwarz) THEN
    1266              : !                                 CALL hfx_add_single_cache_element( &
    1267              : !                                    estimate_to_store_int, 6, &
    1268              : !                                    maxval_cache, maxval_container, memory_parameter%actual_memory_usage, &
    1269              : !                                    use_disk_storage, max_val_memory)
    1270              : !                              END IF
    1271              : !                           END IF
    1272              : !
    1273              : !                           IF (.NOT. use_virial) THEN
    1274              : !                              IF (cartesian_estimate < eps_schwarz) CYCLE
    1275              : !                           END IF
    1276              : 
    1277              :                            !! Compress the array for storage
    1278              :                            spherical_estimate = 0.0_dp
    1279              :                            IF (cartesian_estimate /= 0.0_dp) THEN
    1280              :                               DO i = 1, nints
    1281              :                                  spherical_estimate = MAX(spherical_estimate, ABS(primitive_forces(i)))
    1282              :                               END DO
    1283              :                            END IF
    1284              : 
    1285              :                            spherical_estimate_virial = 0.0_dp
    1286              :                            IF (use_virial .AND. cartesian_estimate_virial /= 0.0_dp) THEN
    1287              :                               DO i = 1, 3*nints
    1288              :                                  spherical_estimate_virial = MAX(spherical_estimate_virial, ABS(primitive_forces_virial(i)))
    1289              :                               END DO
    1290              :                            END IF
    1291              : 
    1292              :                            IF (spherical_estimate == 0.0_dp) spherical_estimate = TINY(spherical_estimate)
    1293              :                            estimate_to_store_int = EXPONENT(spherical_estimate)
    1294              :                            estimate_to_store_int = MAX(estimate_to_store_int, -15_int_8)
    1295              :                            IF (.NOT. buffer_overflow .AND. actual_x_data%memory_parameter%recalc_forces) THEN
    1296              :                               CALL hfx_add_single_cache_element(estimate_to_store_int, 6, &
    1297              :                                                                 maxval_cache, maxval_container, &
    1298              :                                                                 memory_parameter%actual_memory_usage, &
    1299              :                                                                 use_disk_storage, max_val_memory)
    1300              :                            END IF
    1301              :                            spherical_estimate = SET_EXPONENT(1.0_dp, estimate_to_store_int + 1)
    1302              :                            IF (.NOT. use_virial) THEN
    1303              :                               IF (spherical_estimate*pmax_entry < eps_schwarz) CYCLE
    1304              :                            END IF
    1305              :                            IF (.NOT. buffer_overflow) THEN
    1306              :                               nbits = EXPONENT(ANINT(spherical_estimate*pmax_entry/eps_storage)) + 1
    1307              :                               buffer_left = nints
    1308              :                               buffer_start = 1
    1309              : !                              neris_incore = neris_incore+nints
    1310              :                               neris_incore = neris_incore + INT(nints, int_8)
    1311              : 
    1312              :                               DO WHILE (buffer_left > 0)
    1313              :                                  buffer_size = MIN(buffer_left, CACHE_SIZE)
    1314              :                                  CALL hfx_add_mult_cache_elements(primitive_forces(buffer_start), &
    1315              :                                                                   buffer_size, nbits, &
    1316              :                                                                   integral_caches(nbits), &
    1317              :                                                                   integral_containers(nbits), &
    1318              :                                                                   eps_storage, pmax_entry, &
    1319              :                                                                   memory_parameter%actual_memory_usage, &
    1320              :                                                                   use_disk_storage)
    1321              :                                  buffer_left = buffer_left - buffer_size
    1322              :                                  buffer_start = buffer_start + buffer_size
    1323              :                               END DO
    1324              :                            ELSE
    1325              :                               !! In order to be consistent with in-core part, round all the eris wrt. eps_schwarz
    1326              :                               !! but only if we treat forces in-core
    1327              :                               IF (treat_forces_in_core .AND. cartesian_estimate /= 0.0_dp) THEN
    1328              :                                  DO i = 1, nints
    1329              :                                     primitive_forces(i) = primitive_forces(i)*pmax_entry
    1330              :                                     IF (ABS(primitive_forces(i)) > eps_storage) THEN
    1331              :                                        primitive_forces(i) = ANINT(primitive_forces(i)/eps_storage, dp)*eps_storage/pmax_entry
    1332              :                                     ELSE
    1333              :                                        primitive_forces(i) = 0.0_dp
    1334              :                                     END IF
    1335              :                                  END DO
    1336              :                               END IF
    1337              :                            END IF
    1338              :                            IF (cartesian_estimate == 0.0_dp .AND. &
    1339              :                                (.NOT. use_virial .OR. cartesian_estimate_virial == 0.0_dp)) CYCLE
    1340              :                         END IF
    1341              :                         IF (with_resp_density) THEN
    1342              :                            CALL prefetch_density_matrix(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1343              :                                                         full_density_alpha(:, 1), pbd_buf, pbc_buf, pad_buf, pac_buf, &
    1344              :                                                         iatom, jatom, katom, latom, &
    1345              :                                                         iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1346              :                                                         offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1347              :                                                         atomic_offset_ad, atomic_offset_ac, is_anti_symmetric)
    1348              :                            CALL prefetch_density_matrix(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1349              :                                                         full_density_resp, pbd_buf_resp, pbc_buf_resp, pad_buf_resp, pac_buf_resp, &
    1350              :                                                         iatom, jatom, katom, latom, &
    1351              :                                                         iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1352              :                                                         offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1353              :                                                         atomic_offset_ad, atomic_offset_ac, is_anti_symmetric)
    1354              :                         ELSE
    1355              :                            CALL prefetch_density_matrix(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1356              :                                                         full_density_alpha(:, 1), pbd_buf, pbc_buf, pad_buf, pac_buf, &
    1357              :                                                         iatom, jatom, katom, latom, &
    1358              :                                                         iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1359              :                                                         offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1360              :                                                         atomic_offset_ad, atomic_offset_ac, is_anti_symmetric)
    1361              :                         END IF
    1362              :                         IF (my_nspins == 2) THEN
    1363              :                            IF (with_resp_density) THEN
    1364              :                               CALL prefetch_density_matrix(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1365              :                                                            full_density_beta(:, 1), pbd_buf_beta, pbc_buf_beta, &
    1366              :                                                            pad_buf_beta, pac_buf_beta, iatom, jatom, katom, latom, &
    1367              :                                                            iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1368              :                                                            offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1369              :                                                            atomic_offset_ad, atomic_offset_ac, is_anti_symmetric)
    1370              :                               CALL prefetch_density_matrix(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1371              :                                                            full_density_resp_beta, pbd_buf_resp_beta, pbc_buf_resp_beta, &
    1372              :                                                            pad_buf_resp_beta, pac_buf_resp_beta, iatom, jatom, katom, latom, &
    1373              :                                                            iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1374              :                                                            offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1375              :                                                            atomic_offset_ad, atomic_offset_ac, is_anti_symmetric)
    1376              :                            ELSE
    1377              :                               CALL prefetch_density_matrix(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1378              :                                                            full_density_beta(:, 1), pbd_buf_beta, pbc_buf_beta, pad_buf_beta, &
    1379              :                                                            pac_buf_beta, iatom, jatom, katom, latom, &
    1380              :                                                            iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1381              :                                                            offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1382              :                                                            atomic_offset_ad, atomic_offset_ac, is_anti_symmetric)
    1383              :                            END IF
    1384              :                         END IF
    1385              :                         IF (spherical_estimate*pmax_entry >= eps_schwarz) THEN
    1386              :                            DO coord = 1, 12
    1387              :                               T2 => primitive_forces((coord - 1)*nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset) + 1: &
    1388              :                                                      coord*nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset))
    1389              : 
    1390              :                               IF (with_resp_density) THEN
    1391              :                                  CALL update_forces(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1392              :                                                     pbd_buf, pbc_buf, pad_buf, pac_buf, fac, &
    1393              :                                                     T2, force, forces_map, coord, &
    1394              :                                                     pbd_buf_resp, pbc_buf_resp, pad_buf_resp, pac_buf_resp, &
    1395              :                                                     my_resp_only)
    1396              :                               ELSE
    1397              :                                  CALL update_forces(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1398              :                                                     pbd_buf, pbc_buf, pad_buf, pac_buf, fac, &
    1399              :                                                     T2, force, forces_map, coord)
    1400              :                               END IF
    1401              :                               IF (my_nspins == 2) THEN
    1402              :                                  IF (with_resp_density) THEN
    1403              :                                     CALL update_forces(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1404              :                                                        pbd_buf_beta, pbc_buf_beta, pad_buf_beta, pac_buf_beta, &
    1405              :                                                        fac, T2, force, forces_map, coord, &
    1406              :                                                        pbd_buf_resp_beta, pbc_buf_resp_beta, &
    1407              :                                                        pad_buf_resp_beta, pac_buf_resp_beta, &
    1408              :                                                        my_resp_only)
    1409              :                                  ELSE
    1410              :                                     CALL update_forces(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1411              :                                                        pbd_buf_beta, pbc_buf_beta, pad_buf_beta, pac_buf_beta, fac, &
    1412              :                                                        T2, force, forces_map, coord)
    1413              :                                  END IF
    1414              :                               END IF
    1415              :                            END DO !coord
    1416              :                         END IF
    1417              :                         IF (use_virial .AND. spherical_estimate_virial*pmax_entry >= eps_schwarz) THEN
    1418              :                            DO coord = 1, 12
    1419              :                               DO i = 1, 3
    1420              :                                  T2 => primitive_forces_virial( &
    1421              :                                        ((i - 1)*12 + coord - 1)*nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset) + 1: &
    1422              :                                        ((i - 1)*12 + coord)*nsgfa(iset)*nsgfb(jset)*nsgfc(kset)*nsgfd(lset))
    1423              :                                  IF (with_resp_density) THEN
    1424              :                                     CALL update_virial(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1425              :                                                        pbd_buf, pbc_buf, pad_buf, pac_buf, fac, &
    1426              :                                                        T2, tmp_virial, coord, i, &
    1427              :                                                        pbd_buf_resp, pbc_buf_resp, pad_buf_resp, &
    1428              :                                                        pac_buf_resp, my_resp_only)
    1429              :                                  ELSE
    1430              :                                     CALL update_virial(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1431              :                                                        pbd_buf, pbc_buf, pad_buf, pac_buf, fac, &
    1432              :                                                        T2, tmp_virial, coord, i)
    1433              :                                  END IF
    1434              :                                  IF (my_nspins == 2) THEN
    1435              :                                     IF (with_resp_density) THEN
    1436              :                                        CALL update_virial(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1437              :                                                           pbd_buf_beta, pbc_buf_beta, pad_buf_beta, pac_buf_beta, &
    1438              :                                                           fac, T2, tmp_virial, coord, i, &
    1439              :                                                           pbd_buf_resp_beta, pbc_buf_resp_beta, &
    1440              :                                                           pad_buf_resp_beta, pac_buf_resp_beta, &
    1441              :                                                           my_resp_only)
    1442              :                                     ELSE
    1443              :                                        CALL update_virial(nsgfa(iset), nsgfb(jset), nsgfc(kset), nsgfd(lset), &
    1444              :                                                           pbd_buf_beta, pbc_buf_beta, pad_buf_beta, pac_buf_beta, &
    1445              :                                                           fac, T2, tmp_virial, coord, i)
    1446              :                                     END IF
    1447              :                                  END IF
    1448              :                               END DO
    1449              :                            END DO !coord
    1450              :                         END IF
    1451              : 
    1452              :                      END DO !i_set_list_kl
    1453              :                   END DO !i_set_list_ij
    1454              :                END DO !i_list_kl
    1455              :             END DO !i_list_ij
    1456              :          END DO atomic_blocks
    1457              :          bintime_stop = m_walltime()
    1458              :          distribution_forces%time_forces = bintime_stop - bintime_start
    1459              : !$OMP MASTER
    1460              :          CALL timestop(handle_bin)
    1461              : !$OMP END MASTER
    1462              :       END DO !bin
    1463              : 
    1464              : !$OMP MASTER
    1465              :       logger => cp_get_default_logger()
    1466              :       do_print_load_balance_info = .FALSE.
    1467              :       do_print_load_balance_info = BTEST(cp_print_key_should_output(logger%iter_info, hfx_section, &
    1468              :                                                                     "LOAD_BALANCE%PRINT/LOAD_BALANCE_INFO"), cp_p_file)
    1469              : !$OMP END MASTER
    1470              : !$OMP BARRIER
    1471              :       IF (do_print_load_balance_info) THEN
    1472              :          iw = -1
    1473              : !$OMP MASTER
    1474              :          iw = cp_print_key_unit_nr(logger, hfx_section, "LOAD_BALANCE%PRINT/LOAD_BALANCE_INFO", &
    1475              :                                    extension=".scfLog")
    1476              : !$OMP END MASTER
    1477              : 
    1478              :          CALL collect_load_balance_info(para_env, actual_x_data, iw, n_threads, i_thread, &
    1479              :                                         hfx_do_eval_forces)
    1480              : 
    1481              : !$OMP MASTER
    1482              :          CALL cp_print_key_finished_output(iw, logger, hfx_section, &
    1483              :                                            "LOAD_BALANCE%PRINT/LOAD_BALANCE_INFO")
    1484              : !$OMP END MASTER
    1485              :       END IF
    1486              : 
    1487              :       IF (use_virial) THEN
    1488              :          DO i = 1, 3
    1489              :             DO j = 1, 3
    1490              :                DO k = 1, 3
    1491              : !$OMP                 ATOMIC
    1492              :                   virial%pv_fock_4c(i, j) = virial%pv_fock_4c(i, j) + tmp_virial(i, k)*cell%hmat(j, k)
    1493              :                END DO
    1494              :             END DO
    1495              :          END DO
    1496              :       END IF
    1497              : 
    1498              : !$OMP BARRIER
    1499              :       !! Get some number about ERIS
    1500              : !$OMP ATOMIC
    1501              :       shm_neris_total = shm_neris_total + neris_total
    1502              : !$OMP ATOMIC
    1503              :       shm_neris_onthefly = shm_neris_onthefly + neris_onthefly
    1504              : !$OMP ATOMIC
    1505              :       shm_nprim_ints = shm_nprim_ints + nprim_ints
    1506              : 
    1507              :       storage_counter_integrals = memory_parameter%actual_memory_usage* &
    1508              :                                   memory_parameter%cache_size
    1509              :       stor_count_max_val = max_val_memory*memory_parameter%cache_size
    1510              : !$OMP ATOMIC
    1511              :       shm_storage_counter_integrals = shm_storage_counter_integrals + storage_counter_integrals
    1512              : !$OMP ATOMIC
    1513              :       shm_neris_incore = shm_neris_incore + neris_incore
    1514              : !$OMP ATOMIC
    1515              :       shm_stor_count_max_val = shm_stor_count_max_val + stor_count_max_val
    1516              : !$OMP BARRIER
    1517              : 
    1518              :       ! IF(with_resp_density) THEN
    1519              :       !   ! restore original load_balance_parameter
    1520              :       !   actual_x_data%load_balance_parameter = load_balance_parameter_energy
    1521              :       !   DEALLOCATE(load_balance_parameter_energy)
    1522              :       ! END IF
    1523              : 
    1524              : !$OMP MASTER
    1525              :       !! Print some memeory information if this is the first step
    1526              :       IF (actual_x_data%memory_parameter%recalc_forces) THEN
    1527              :          tmp_i8(1:6) = [shm_storage_counter_integrals, shm_neris_onthefly, shm_neris_incore, &
    1528              :                         shm_neris_total, shm_nprim_ints, shm_stor_count_max_val]
    1529              :          CALL para_env%sum(tmp_i8)
    1530              :          shm_storage_counter_integrals = tmp_i8(1)
    1531              :          shm_neris_onthefly = tmp_i8(2)
    1532              :          shm_neris_incore = tmp_i8(3)
    1533              :          shm_neris_total = tmp_i8(4)
    1534              :          shm_nprim_ints = tmp_i8(5)
    1535              :          shm_stor_count_max_val = tmp_i8(6)
    1536              :          mem_eris = (shm_storage_counter_integrals + 128*1024 - 1)/1024/128
    1537              :          compression_factor = REAL(shm_neris_incore, dp)/REAL(shm_storage_counter_integrals, dp)
    1538              :          mem_max_val = (shm_stor_count_max_val + 128*1024 - 1)/1024/128
    1539              : 
    1540              :          IF (shm_neris_incore == 0) THEN
    1541              :             mem_eris = 0
    1542              :             compression_factor = 0.0_dp
    1543              :          END IF
    1544              : 
    1545              :          logger => cp_get_default_logger()
    1546              : 
    1547              :          iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
    1548              :                                    extension=".scfLog")
    1549              :          IF (iw > 0) THEN
    1550              :             WRITE (UNIT=iw, FMT="(/,(T3,A,T65,I16))") &
    1551              :                "HFX_MEM_INFO| Number of cart. primitive DERIV's calculated:      ", shm_nprim_ints
    1552              : 
    1553              :             WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
    1554              :                "HFX_MEM_INFO| Number of sph. DERIV's calculated:           ", shm_neris_total
    1555              : 
    1556              :             WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
    1557              :                "HFX_MEM_INFO| Number of sph. DERIV's stored in-core:        ", shm_neris_incore
    1558              : 
    1559              :             WRITE (UNIT=iw, FMT="((T3,A,T62,I19))") &
    1560              :                "HFX_MEM_INFO| Number of sph. DERIV's calculated on the fly: ", shm_neris_onthefly
    1561              : 
    1562              :             WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
    1563              :                "HFX_MEM_INFO| Total memory consumption DERIV's RAM [MiB]:            ", mem_eris
    1564              : 
    1565              :             WRITE (UNIT=iw, FMT="((T3,A,T60,I21))") &
    1566              :                "HFX_MEM_INFO| Whereof max-vals [MiB]:            ", mem_max_val
    1567              : 
    1568              :             WRITE (UNIT=iw, FMT="((T3,A,T60,F21.2),/)") &
    1569              :                "HFX_MEM_INFO| Total compression factor DERIV's RAM:                  ", compression_factor
    1570              :          END IF
    1571              : 
    1572              :          CALL cp_print_key_finished_output(iw, logger, hfx_section, &
    1573              :                                            "HF_INFO")
    1574              :       END IF
    1575              : !$OMP END MASTER
    1576              : 
    1577              :       !! flush caches if the geometry changed
    1578              :       IF (do_dynamic_load_balancing) THEN
    1579              :          my_bin_size = SIZE(actual_x_data%distribution_forces)
    1580              :       ELSE
    1581              :          my_bin_size = 1
    1582              :       END IF
    1583              : 
    1584              :       IF (actual_x_data%memory_parameter%recalc_forces) THEN
    1585              :          IF (treat_forces_in_core) THEN
    1586              :             DO bin = 1, my_bin_size
    1587              :                maxval_cache => actual_x_data%store_forces%maxval_cache(bin)
    1588              :                maxval_container => actual_x_data%store_forces%maxval_container(bin)
    1589              :                integral_caches => actual_x_data%store_forces%integral_caches(:, bin)
    1590              :                integral_containers => actual_x_data%store_forces%integral_containers(:, bin)
    1591              :                CALL hfx_flush_last_cache(bits_max_val, maxval_cache, maxval_container, memory_parameter%actual_memory_usage, &
    1592              :                                          .FALSE.)
    1593              :                DO i = 1, 64
    1594              :                   CALL hfx_flush_last_cache(i, integral_caches(i), integral_containers(i), &
    1595              :                                             memory_parameter%actual_memory_usage, .FALSE.)
    1596              :                END DO
    1597              :             END DO
    1598              :          END IF
    1599              :       END IF
    1600              :       !! reset all caches except we calculate all on the fly
    1601              :       IF (treat_forces_in_core) THEN
    1602              :          DO bin = 1, my_bin_size
    1603              :             maxval_cache => actual_x_data%store_forces%maxval_cache(bin)
    1604              :             maxval_container => actual_x_data%store_forces%maxval_container(bin)
    1605              :             integral_caches => actual_x_data%store_forces%integral_caches(:, bin)
    1606              :             integral_containers => actual_x_data%store_forces%integral_containers(:, bin)
    1607              : 
    1608              :             CALL hfx_reset_cache_and_container(maxval_cache, maxval_container, memory_parameter%actual_memory_usage, .FALSE.)
    1609              :             DO i = 1, 64
    1610              :                CALL hfx_reset_cache_and_container(integral_caches(i), integral_containers(i), &
    1611              :                                                   memory_parameter%actual_memory_usage, &
    1612              :                                                   .FALSE.)
    1613              :             END DO
    1614              :          END DO
    1615              :       END IF
    1616              : 
    1617              :       IF (actual_x_data%memory_parameter%recalc_forces) THEN
    1618              :          actual_x_data%memory_parameter%recalc_forces = .FALSE.
    1619              :       END IF
    1620              : 
    1621              : !$OMP BARRIER
    1622              : !$OMP MASTER
    1623              :       CALL timestop(handle_main)
    1624              : !$OMP END MASTER
    1625              : !$OMP BARRIER
    1626              :       DEALLOCATE (last_sgf_global)
    1627              : !$OMP MASTER
    1628              :       DEALLOCATE (full_density_alpha)
    1629              :       IF (with_resp_density) THEN
    1630              :          DEALLOCATE (full_density_resp)
    1631              :       END IF
    1632              :       IF (my_nspins == 2) THEN
    1633              :          DEALLOCATE (full_density_beta)
    1634              :          IF (with_resp_density) THEN
    1635              :             DEALLOCATE (full_density_resp_beta)
    1636              :          END IF
    1637              :       END IF
    1638              :       IF (do_dynamic_load_balancing) THEN
    1639              :          DEALLOCATE (actual_x_data%task_list)
    1640              :       END IF
    1641              : !$OMP END MASTER
    1642              :       DEALLOCATE (primitive_forces)
    1643              :       DEALLOCATE (atom_of_kind, kind_of)
    1644              :       DEALLOCATE (max_contraction)
    1645              :       DEALLOCATE (pbd_buf)
    1646              :       DEALLOCATE (pbc_buf)
    1647              :       DEALLOCATE (pad_buf)
    1648              :       DEALLOCATE (pac_buf)
    1649              : 
    1650              :       IF (with_resp_density) THEN
    1651              :          DEALLOCATE (pbd_buf_resp)
    1652              :          DEALLOCATE (pbc_buf_resp)
    1653              :          DEALLOCATE (pad_buf_resp)
    1654              :          DEALLOCATE (pac_buf_resp)
    1655              :       END IF
    1656              : 
    1657              :       DO i = 1, max_pgf**2
    1658              :          DEALLOCATE (pgf_list_ij(i)%image_list)
    1659              :          DEALLOCATE (pgf_list_kl(i)%image_list)
    1660              :       END DO
    1661              : 
    1662              :       DEALLOCATE (pgf_list_ij)
    1663              :       DEALLOCATE (pgf_list_kl)
    1664              : 
    1665              :       DEALLOCATE (set_list_ij, set_list_kl)
    1666              : 
    1667              :       DEALLOCATE (primitive_forces_virial)
    1668              : 
    1669              :       DEALLOCATE (ede_work, ede_work2, ede_work_forces, ede_buffer1, ede_buffer2)
    1670              : 
    1671              :       DEALLOCATE (ede_work_virial, ede_work2_virial)
    1672              : 
    1673              :       DEALLOCATE (nimages)
    1674              : 
    1675              :       IF (my_nspins == 2) THEN
    1676              :          DEALLOCATE (pbd_buf_beta, pbc_buf_beta, pad_buf_beta, pac_buf_beta)
    1677              :          IF (with_resp_density) THEN
    1678              :             DEALLOCATE (pbd_buf_resp_beta)
    1679              :             DEALLOCATE (pbc_buf_resp_beta)
    1680              :             DEALLOCATE (pad_buf_resp_beta)
    1681              :             DEALLOCATE (pac_buf_resp_beta)
    1682              :          END IF
    1683              :       END IF
    1684              : 
    1685              : !$OMP END PARALLEL
    1686              : 
    1687         2008 :       CALL timestop(handle)
    1688              : 
    1689      4172888 :    END SUBROUTINE derivatives_four_center
    1690              : 
    1691              : ! **************************************************************************************************
    1692              : !> \brief Given a 2d index pair, this function returns a 1d index pair for
    1693              : !>        a symmetric upper triangle NxN matrix
    1694              : !>        The compiler should inline this function, therefore it appears in
    1695              : !>        several modules
    1696              : !> \param i 2d index
    1697              : !> \param j 2d index
    1698              : !> \param N matrix size
    1699              : !> \return ...
    1700              : !> \par History
    1701              : !>      03.2009 created [Manuel Guidon]
    1702              : !> \author Manuel Guidon
    1703              : ! **************************************************************************************************
    1704        63136 :    PURE FUNCTION get_1D_idx(i, j, N)
    1705              :       INTEGER, INTENT(IN)                                :: i, j
    1706              :       INTEGER(int_8), INTENT(IN)                         :: N
    1707              :       INTEGER(int_8)                                     :: get_1D_idx
    1708              : 
    1709              :       INTEGER(int_8)                                     :: min_ij
    1710              : 
    1711        63136 :       min_ij = MIN(i, j)
    1712        63136 :       get_1D_idx = min_ij*N + MAX(i, j) - (min_ij - 1)*min_ij/2 - N
    1713              : 
    1714        63136 :    END FUNCTION get_1D_idx
    1715              : 
    1716              : ! **************************************************************************************************
    1717              : !> \brief This routine prefetches density matrix elements, i.e. reshuffles the
    1718              : !>        data in a way that they can be accessed later on in a cache friendly
    1719              : !>        way
    1720              : !> \param ma_max Size of matrix blocks
    1721              : !> \param mb_max Size of matrix blocks
    1722              : !> \param mc_max Size of matrix blocks
    1723              : !> \param md_max Size of matrix blocks
    1724              : !> \param density ...
    1725              : !> \param pbd buffer that will contain P(b,d)
    1726              : !> \param pbc buffer that will contain P(b,c)
    1727              : !> \param pad buffer that will contain P(a,d)
    1728              : !> \param pac buffer that will contain P(a,c)
    1729              : !> \param iatom ...
    1730              : !> \param jatom ...
    1731              : !> \param katom ...
    1732              : !> \param latom ...
    1733              : !> \param iset ...
    1734              : !> \param jset ...
    1735              : !> \param kset ...
    1736              : !> \param lset ...
    1737              : !> \param offset_bd_set ...
    1738              : !> \param offset_bc_set ...
    1739              : !> \param offset_ad_set ...
    1740              : !> \param offset_ac_set ...
    1741              : !> \param atomic_offset_bd ...
    1742              : !> \param atomic_offset_bc ...
    1743              : !> \param atomic_offset_ad ...
    1744              : !> \param atomic_offset_ac ...
    1745              : !> \param anti_symmetric ...
    1746              : !> \par History
    1747              : !>      03.2009 created [Manuel Guidon]
    1748              : !> \author Manuel Guidon
    1749              : ! **************************************************************************************************
    1750      3194149 :    SUBROUTINE prefetch_density_matrix(ma_max, mb_max, mc_max, md_max, &
    1751      3194149 :                                       density, pbd, pbc, pad, pac, &
    1752              :                                       iatom, jatom, katom, latom, &
    1753              :                                       iset, jset, kset, lset, offset_bd_set, offset_bc_set, &
    1754              :                                       offset_ad_set, offset_ac_set, atomic_offset_bd, atomic_offset_bc, &
    1755              :                                       atomic_offset_ad, atomic_offset_ac, anti_symmetric)
    1756              : 
    1757              :       INTEGER, INTENT(IN)                                :: ma_max, mb_max, mc_max, md_max
    1758              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: density
    1759              :       REAL(dp), DIMENSION(*), INTENT(INOUT)              :: pbd, pbc, pad, pac
    1760              :       INTEGER, INTENT(IN)                                :: iatom, jatom, katom, latom, iset, jset, &
    1761              :                                                             kset, lset
    1762              :       INTEGER, DIMENSION(:, :), POINTER                  :: offset_bd_set, offset_bc_set, &
    1763              :                                                             offset_ad_set, offset_ac_set
    1764              :       INTEGER, INTENT(IN)                                :: atomic_offset_bd, atomic_offset_bc, &
    1765              :                                                             atomic_offset_ad, atomic_offset_ac
    1766              :       LOGICAL                                            :: anti_symmetric
    1767              : 
    1768              :       INTEGER                                            :: i, j, ma, mb, mc, md, offset_ac, &
    1769              :                                                             offset_ad, offset_bc, offset_bd
    1770              :       REAL(dp)                                           :: fac
    1771              : 
    1772      3194149 :       fac = 1.0_dp
    1773      3194149 :       IF (anti_symmetric) fac = -1.0_dp
    1774      3194149 :       IF (jatom >= latom) THEN
    1775      3187943 :          i = 1
    1776      3187943 :          offset_bd = offset_bd_set(jset, lset) + atomic_offset_bd - 1
    1777      3187943 :          j = offset_bd
    1778      9391380 :          DO md = 1, md_max
    1779     20312423 :             DO mb = 1, mb_max
    1780     10921043 :                pbd(i) = density(j)*fac
    1781     10921043 :                i = i + 1
    1782     17124480 :                j = j + 1
    1783              :             END DO
    1784              :          END DO
    1785              :       ELSE
    1786         6206 :          i = 1
    1787         6206 :          offset_bd = offset_bd_set(lset, jset) + atomic_offset_bd - 1
    1788        14840 :          DO md = 1, md_max
    1789         8634 :             j = offset_bd + md - 1
    1790        27686 :             DO mb = 1, mb_max
    1791        12846 :                pbd(i) = density(j)
    1792        12846 :                i = i + 1
    1793        21480 :                j = j + md_max
    1794              :             END DO
    1795              :          END DO
    1796              :       END IF
    1797      3194149 :       IF (jatom >= katom) THEN
    1798      3194149 :          i = 1
    1799      3194149 :          offset_bc = offset_bc_set(jset, kset) + atomic_offset_bc - 1
    1800      3194149 :          j = offset_bc
    1801     10479202 :          DO mc = 1, mc_max
    1802     22920695 :             DO mb = 1, mb_max
    1803     12441493 :                pbc(i) = density(j)*fac
    1804     12441493 :                i = i + 1
    1805     19726546 :                j = j + 1
    1806              :             END DO
    1807              :          END DO
    1808              :       ELSE
    1809            0 :          i = 1
    1810            0 :          offset_bc = offset_bc_set(kset, jset) + atomic_offset_bc - 1
    1811            0 :          DO mc = 1, mc_max
    1812            0 :             j = offset_bc + mc - 1
    1813            0 :             DO mb = 1, mb_max
    1814            0 :                pbc(i) = density(j)
    1815            0 :                i = i + 1
    1816            0 :                j = j + mc_max
    1817              :             END DO
    1818              :          END DO
    1819              :       END IF
    1820      3194149 :       IF (iatom >= latom) THEN
    1821      2457499 :          i = 1
    1822      2457499 :          offset_ad = offset_ad_set(iset, lset) + atomic_offset_ad - 1
    1823      2457499 :          j = offset_ad
    1824      7605137 :          DO md = 1, md_max
    1825     18451461 :             DO ma = 1, ma_max
    1826     10846324 :                pad(i) = density(j)*fac
    1827     10846324 :                i = i + 1
    1828     15993962 :                j = j + 1
    1829              :             END DO
    1830              :          END DO
    1831              :       ELSE
    1832       736650 :          i = 1
    1833       736650 :          offset_ad = offset_ad_set(lset, iset) + atomic_offset_ad - 1
    1834      1801083 :          DO md = 1, md_max
    1835      1064433 :             j = offset_ad + md - 1
    1836      4408180 :             DO ma = 1, ma_max
    1837      2607097 :                pad(i) = density(j)
    1838      2607097 :                i = i + 1
    1839      3671530 :                j = j + md_max
    1840              :             END DO
    1841              :          END DO
    1842              :       END IF
    1843      3194149 :       IF (iatom >= katom) THEN
    1844      3106720 :          i = 1
    1845      3106720 :          offset_ac = offset_ac_set(iset, kset) + atomic_offset_ac - 1
    1846      3106720 :          j = offset_ac
    1847     10265166 :          DO mc = 1, mc_max
    1848     25768492 :             DO ma = 1, ma_max
    1849     15503326 :                pac(i) = density(j)*fac
    1850     15503326 :                i = i + 1
    1851     22661772 :                j = j + 1
    1852              :             END DO
    1853              :          END DO
    1854              :       ELSE
    1855        87429 :          i = 1
    1856        87429 :          offset_ac = offset_ac_set(kset, iset) + atomic_offset_ac - 1
    1857       214036 :          DO mc = 1, mc_max
    1858       126607 :             j = offset_ac + mc - 1
    1859       596361 :             DO ma = 1, ma_max
    1860       382325 :                pac(i) = density(j)
    1861       382325 :                i = i + 1
    1862       508932 :                j = j + mc_max
    1863              :             END DO
    1864              :          END DO
    1865              :       END IF
    1866      3194149 :    END SUBROUTINE prefetch_density_matrix
    1867              : 
    1868              : ! **************************************************************************************************
    1869              : !> \brief This routine updates the forces using buffered density matrices
    1870              : !> \param ma_max Size of matrix blocks
    1871              : !> \param mb_max Size of matrix blocks
    1872              : !> \param mc_max Size of matrix blocks
    1873              : !> \param md_max Size of matrix blocks
    1874              : !> \param pbd buffer that will contain P(b,d)
    1875              : !> \param pbc buffer that will contain P(b,c)
    1876              : !> \param pad buffer that will contain P(a,d)
    1877              : !> \param pac buffer that will contain P(a,c)
    1878              : !> \param fac mulitplication factor (spin, symmetry)
    1879              : !> \param prim primitive forces
    1880              : !> \param force storage loacation for forces
    1881              : !> \param forces_map index table
    1882              : !> \param coord which of the 12 coords to be updated
    1883              : !> \param pbd_resp ...
    1884              : !> \param pbc_resp ...
    1885              : !> \param pad_resp ...
    1886              : !> \param pac_resp ...
    1887              : !> \param resp_only ...
    1888              : !> \par History
    1889              : !>      03.2009 created [Manuel Guidon]
    1890              : !> \author Manuel Guidon
    1891              : ! **************************************************************************************************
    1892     25767444 :    SUBROUTINE update_forces(ma_max, mb_max, mc_max, md_max, &
    1893              :                             pbd, pbc, pad, pac, fac, &
    1894     25767444 :                             prim, force, forces_map, coord, &
    1895              :                             pbd_resp, pbc_resp, pad_resp, pac_resp, resp_only)
    1896              : 
    1897              :       INTEGER, INTENT(IN)                                :: ma_max, mb_max, mc_max, md_max
    1898              :       REAL(dp), DIMENSION(*), INTENT(IN)                 :: pbd, pbc, pad, pac
    1899              :       REAL(dp), INTENT(IN)                               :: fac
    1900              :       REAL(dp), DIMENSION(ma_max*mb_max*mc_max*md_max), &
    1901              :          INTENT(IN)                                      :: prim
    1902              :       TYPE(qs_force_type), DIMENSION(:), POINTER         :: force
    1903              :       INTEGER, INTENT(IN)                                :: forces_map(4, 2), coord
    1904              :       REAL(dp), DIMENSION(*), INTENT(IN), OPTIONAL       :: pbd_resp, pbc_resp, pad_resp, pac_resp
    1905              :       LOGICAL, INTENT(IN), OPTIONAL                      :: resp_only
    1906              : 
    1907              :       INTEGER                                            :: ma, mb, mc, md, p_index
    1908              :       LOGICAL                                            :: full_force, with_resp_density
    1909              :       REAL(dp)                                           :: temp1, temp1_resp, temp3, temp3_resp, &
    1910              :                                                             temp4, teresp
    1911              : 
    1912     25767444 :       with_resp_density = .FALSE.
    1913              :       IF (PRESENT(pbd_resp) .AND. &
    1914              :           PRESENT(pbc_resp) .AND. &
    1915     25767444 :           PRESENT(pad_resp) .AND. &
    1916              :           PRESENT(pac_resp)) with_resp_density = .TRUE.
    1917              : 
    1918              :       IF (with_resp_density) THEN
    1919     12535656 :          full_force = .TRUE.
    1920     12535656 :          IF (PRESENT(resp_only)) full_force = .NOT. resp_only
    1921     12535656 :          p_index = 0
    1922     12535656 :          temp4 = 0.0_dp
    1923     36653064 :          DO md = 1, md_max
    1924     95537892 :             DO mc = 1, mc_max
    1925    196593564 :                DO mb = 1, mb_max
    1926    113591328 :                   temp1 = pbc((mc - 1)*mb_max + mb)*fac
    1927    113591328 :                   temp3 = pbd((md - 1)*mb_max + mb)*fac
    1928    113591328 :                   temp1_resp = pbc_resp((mc - 1)*mb_max + mb)*fac
    1929    113591328 :                   temp3_resp = pbd_resp((md - 1)*mb_max + mb)*fac
    1930    491080212 :                   DO ma = 1, ma_max
    1931    318604056 :                      p_index = p_index + 1
    1932              :                      ! HF-SCF
    1933    318604056 :                      IF (full_force) THEN
    1934              :                         teresp = temp1*pad((md - 1)*ma_max + ma) + &
    1935    102217536 :                                  temp3*pac((mc - 1)*ma_max + ma)
    1936              :                      ELSE
    1937              :                         teresp = 0.0_dp
    1938              :                      END IF
    1939              :                      ! RESP+HF
    1940              :                      teresp = teresp + &
    1941              :                               pac((mc - 1)*ma_max + ma)*temp3_resp + &
    1942              :                               pac_resp((mc - 1)*ma_max + ma)*temp3 + &
    1943              :                               pad((md - 1)*ma_max + ma)*temp1_resp + &
    1944    318604056 :                               pad_resp((md - 1)*ma_max + ma)*temp1
    1945    432195384 :                      temp4 = temp4 + teresp*prim(p_index)
    1946              :                   END DO !ma
    1947              :                END DO !mb
    1948              :             END DO !mc
    1949              :          END DO !md
    1950              :       ELSE
    1951              :          p_index = 0
    1952              :          temp4 = 0.0_dp
    1953     39511296 :          DO md = 1, md_max
    1954    110098332 :             DO mc = 1, mc_max
    1955    237240852 :                DO mb = 1, mb_max
    1956    140374308 :                   temp1 = pbc((mc - 1)*mb_max + mb)*fac
    1957    140374308 :                   temp3 = pbd((md - 1)*mb_max + mb)*fac
    1958    679801176 :                   DO ma = 1, ma_max
    1959    468839832 :                      p_index = p_index + 1
    1960              :                      teresp = temp1*pad((md - 1)*ma_max + ma) + &
    1961    468839832 :                               temp3*pac((mc - 1)*ma_max + ma)
    1962    609214140 :                      temp4 = temp4 + teresp*prim(p_index)
    1963              :                   END DO !ma
    1964              :                END DO !mb
    1965              :             END DO !mc
    1966              :          END DO !md
    1967              :       END IF
    1968              : 
    1969     25767444 : !$OMP ATOMIC
    1970              :       force(forces_map((coord - 1)/3 + 1, 1))%fock_4c(MOD(coord - 1, 3) + 1, &
    1971              :                                                       forces_map((coord - 1)/3 + 1, 2)) = &
    1972              :          force(forces_map((coord - 1)/3 + 1, 1))%fock_4c(MOD(coord - 1, 3) + 1, &
    1973              :                                                          forces_map((coord - 1)/3 + 1, 2)) - &
    1974              :          temp4
    1975              : 
    1976     25767444 :    END SUBROUTINE update_forces
    1977              : 
    1978              : ! **************************************************************************************************
    1979              : !> \brief This routine updates the virial using buffered density matrices
    1980              : !> \param ma_max Size of matrix blocks
    1981              : !> \param mb_max Size of matrix blocks
    1982              : !> \param mc_max Size of matrix blocks
    1983              : !> \param md_max Size of matrix blocks
    1984              : !> \param pbd buffer that will contain P(b,d)
    1985              : !> \param pbc buffer that will contain P(b,c)
    1986              : !> \param pad buffer that will contain P(a,d)
    1987              : !> \param pac buffer that will contain P(a,c)
    1988              : !> \param fac mulitplication factor (spin, symmetry)
    1989              : !> \param prim primitive forces
    1990              : !> \param tmp_virial ...
    1991              : !> \param coord which of the 12 coords to be updated
    1992              : !> \param l ...
    1993              : !> \param pbd_resp ...
    1994              : !> \param pbc_resp ...
    1995              : !> \param pad_resp ...
    1996              : !> \param pac_resp ...
    1997              : !> \par History
    1998              : !>      03.2009 created [Manuel Guidon]
    1999              : !> \author Manuel Guidon
    2000              : ! **************************************************************************************************
    2001      6570468 :    SUBROUTINE update_virial(ma_max, mb_max, mc_max, md_max, &
    2002              :                             pbd, pbc, pad, pac, fac, &
    2003      6570468 :                             prim, tmp_virial, coord, l, &
    2004              :                             pbd_resp, pbc_resp, pad_resp, pac_resp, resp_only)
    2005              : 
    2006              :       INTEGER, INTENT(IN)                                :: ma_max, mb_max, mc_max, md_max
    2007              :       REAL(dp), DIMENSION(*), INTENT(IN)                 :: pbd, pbc, pad, pac
    2008              :       REAL(dp), INTENT(IN)                               :: fac
    2009              :       REAL(dp), DIMENSION(ma_max*mb_max*mc_max*md_max), &
    2010              :          INTENT(IN)                                      :: prim
    2011              :       REAL(dp)                                           :: tmp_virial(3, 3)
    2012              :       INTEGER, INTENT(IN)                                :: coord, l
    2013              :       REAL(dp), DIMENSION(*), INTENT(IN), OPTIONAL       :: pbd_resp, pbc_resp, pad_resp, pac_resp
    2014              :       LOGICAL, INTENT(IN), OPTIONAL                      :: resp_only
    2015              : 
    2016              :       INTEGER                                            :: i, j, ma, mb, mc, md, p_index
    2017              :       LOGICAL                                            :: with_resp_density, full_force
    2018              :       REAL(dp)                                           :: temp1, temp1_resp, temp3, temp3_resp, &
    2019              :                                                             temp4, teresp
    2020              : 
    2021      6570468 :       with_resp_density = .FALSE.
    2022              :       IF (PRESENT(pbd_resp) .AND. &
    2023              :           PRESENT(pbc_resp) .AND. &
    2024      6570468 :           PRESENT(pad_resp) .AND. &
    2025              :           PRESENT(pac_resp)) with_resp_density = .TRUE.
    2026              : 
    2027              :       IF (with_resp_density) THEN
    2028      5587056 :          full_force = .TRUE.
    2029      5587056 :          IF (PRESENT(resp_only)) full_force = .NOT. resp_only
    2030      5587056 :          p_index = 0
    2031      5587056 :          temp4 = 0.0_dp
    2032     16806024 :          DO md = 1, md_max
    2033     41978016 :             DO mc = 1, mc_max
    2034     91229400 :                DO mb = 1, mb_max
    2035     54838440 :                   temp1 = pbc((mc - 1)*mb_max + mb)*fac
    2036     54838440 :                   temp3 = pbd((md - 1)*mb_max + mb)*fac
    2037     54838440 :                   temp1_resp = pbc_resp((mc - 1)*mb_max + mb)*fac
    2038     54838440 :                   temp3_resp = pbd_resp((md - 1)*mb_max + mb)*fac
    2039    236614860 :                   DO ma = 1, ma_max
    2040    156604428 :                      p_index = p_index + 1
    2041              :                      ! HF-SCF
    2042    156604428 :                      IF (full_force) THEN
    2043              :                         teresp = temp1*pad((md - 1)*ma_max + ma) + &
    2044     25168824 :                                  temp3*pac((mc - 1)*ma_max + ma)
    2045              :                      ELSE
    2046              :                         teresp = 0.0_dp
    2047              :                      END IF
    2048              :                      ! RESP+HF
    2049              :                      teresp = teresp + &
    2050              :                               pac((mc - 1)*ma_max + ma)*temp3_resp + &
    2051              :                               pac_resp((mc - 1)*ma_max + ma)*temp3 + &
    2052              :                               pad((md - 1)*ma_max + ma)*temp1_resp + &
    2053    156604428 :                               pad_resp((md - 1)*ma_max + ma)*temp1
    2054    211442868 :                      temp4 = temp4 + teresp*prim(p_index)
    2055              :                   END DO !ma
    2056              :                END DO !mb
    2057              :             END DO !mc
    2058              :          END DO !md
    2059              :       ELSE
    2060              :          p_index = 0
    2061              :          temp4 = 0.0_dp
    2062      2505240 :          DO md = 1, md_max
    2063      5427684 :             DO mc = 1, mc_max
    2064      8390340 :                DO mb = 1, mb_max
    2065      3946068 :                   temp1 = pbc((mc - 1)*mb_max + mb)*fac
    2066      3946068 :                   temp3 = pbd((md - 1)*mb_max + mb)*fac
    2067     13873176 :                   DO ma = 1, ma_max
    2068      7004664 :                      p_index = p_index + 1
    2069              :                      teresp = temp1*pad((md - 1)*ma_max + ma) + &
    2070      7004664 :                               temp3*pac((mc - 1)*ma_max + ma)
    2071     10950732 :                      temp4 = temp4 + teresp*prim(p_index)
    2072              :                   END DO !ma
    2073              :                END DO !mb
    2074              :             END DO !mc
    2075              :          END DO !md
    2076              :       END IF
    2077              : 
    2078      6570468 :       j = l
    2079      6570468 :       i = MOD(coord - 1, 3) + 1
    2080      6570468 :       tmp_virial(i, j) = tmp_virial(i, j) - temp4
    2081      6570468 :    END SUBROUTINE update_virial
    2082              : 
    2083              :    #:include 'hfx_get_pmax_val.fypp'
    2084              : 
    2085              : END MODULE hfx_derivatives
        

Generated by: LCOV version 2.0-1