LCOV - code coverage report
Current view: top level - src - hfx_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:51fc4cd) Lines: 96.7 % 1219 1179
Test Date: 2026-02-04 06:28:27 Functions: 41.5 % 53 22

            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 Types and set/get functions for HFX
      10              : !> \par History
      11              : !>      04.2008 created [Manuel Guidon]
      12              : !>      05.2019 Moved erfc_cutoff to common/mathlib (A. Bussy)
      13              : !> \author Manuel Guidon
      14              : ! **************************************************************************************************
      15              : MODULE hfx_types
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      17              :                                               get_atomic_kind,&
      18              :                                               get_atomic_kind_set
      19              :    USE basis_set_types,                 ONLY: get_gto_basis_set,&
      20              :                                               gto_basis_set_p_type,&
      21              :                                               gto_basis_set_type
      22              :    USE bibliography,                    ONLY: bussy2023,&
      23              :                                               cite_reference,&
      24              :                                               guidon2008,&
      25              :                                               guidon2009
      26              :    USE cell_types,                      ONLY: cell_type,&
      27              :                                               get_cell,&
      28              :                                               plane_distance,&
      29              :                                               scaled_to_real
      30              :    USE cp_array_utils,                  ONLY: cp_1d_logical_p_type
      31              :    USE cp_control_types,                ONLY: dft_control_type
      32              :    USE cp_dbcsr_api,                    ONLY: dbcsr_release,&
      33              :                                               dbcsr_type
      34              :    USE cp_files,                        ONLY: close_file,&
      35              :                                               file_exists,&
      36              :                                               open_file
      37              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      38              :                                               cp_logger_type
      39              :    USE cp_output_handling,              ONLY: cp_print_key_finished_output,&
      40              :                                               cp_print_key_unit_nr
      41              :    USE cp_units,                        ONLY: cp_unit_from_cp2k
      42              :    USE dbt_api,                         ONLY: &
      43              :         dbt_create, dbt_default_distvec, dbt_destroy, dbt_distribution_destroy, &
      44              :         dbt_distribution_new, dbt_distribution_type, dbt_mp_dims_create, dbt_pgrid_create, &
      45              :         dbt_pgrid_destroy, dbt_pgrid_type, dbt_type
      46              :    USE hfx_helpers,                     ONLY: count_cells_perd,&
      47              :                                               next_image_cell_perd
      48              :    USE input_constants,                 ONLY: &
      49              :         do_hfx_auto_shells, do_potential_coulomb, do_potential_gaussian, do_potential_id, &
      50              :         do_potential_long, do_potential_mix_cl, do_potential_mix_cl_trunc, do_potential_mix_lg, &
      51              :         do_potential_short, do_potential_truncated, hfx_ri_do_2c_diag, hfx_ri_do_2c_iter
      52              :    USE input_cp2k_hfx,                  ONLY: ri_mo,&
      53              :                                               ri_pmat
      54              :    USE input_section_types,             ONLY: section_vals_get,&
      55              :                                               section_vals_get_subs_vals,&
      56              :                                               section_vals_type,&
      57              :                                               section_vals_val_get
      58              :    USE kinds,                           ONLY: default_path_length,&
      59              :                                               default_string_length,&
      60              :                                               dp,&
      61              :                                               int_8
      62              :    USE libint_2c_3c,                    ONLY: compare_potential_types,&
      63              :                                               libint_potential_type
      64              :    USE libint_wrapper,                  ONLY: &
      65              :         cp_libint_cleanup_eri, cp_libint_cleanup_eri1, cp_libint_init_eri, cp_libint_init_eri1, &
      66              :         cp_libint_set_contrdepth, cp_libint_static_cleanup, cp_libint_static_init, cp_libint_t, &
      67              :         prim_data_f_size
      68              :    USE machine,                         ONLY: m_chdir,&
      69              :                                               m_getcwd
      70              :    USE mathlib,                         ONLY: erfc_cutoff
      71              :    USE message_passing,                 ONLY: mp_cart_type,&
      72              :                                               mp_para_env_type
      73              :    USE orbital_pointers,                ONLY: nco,&
      74              :                                               ncoset,&
      75              :                                               nso
      76              :    USE particle_methods,                ONLY: get_particle_set
      77              :    USE particle_types,                  ONLY: particle_type
      78              :    USE physcon,                         ONLY: a_bohr
      79              :    USE qs_integral_utils,               ONLY: basis_set_list_setup
      80              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      81              :                                               get_qs_kind_set,&
      82              :                                               qs_kind_type
      83              :    USE qs_tensors_types,                ONLY: &
      84              :         create_2c_tensor, create_3c_tensor, create_tensor_batches, default_block_size, &
      85              :         distribution_3d_create, distribution_3d_destroy, distribution_3d_type, pgf_block_sizes, &
      86              :         split_block_sizes
      87              :    USE string_utilities,                ONLY: compress
      88              :    USE t_c_g0,                          ONLY: free_C0
      89              : 
      90              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
      91              : 
      92              : #include "./base/base_uses.f90"
      93              : 
      94              :    IMPLICIT NONE
      95              :    PRIVATE
      96              :    PUBLIC :: hfx_type, hfx_create, hfx_release, &
      97              :              hfx_set_distr_energy, &
      98              :              hfx_set_distr_forces, &
      99              :              hfx_cell_type, hfx_distribution, &
     100              :              hfx_potential_type, hfx_screening_type, &
     101              :              hfx_memory_type, hfx_load_balance_type, hfx_general_type, &
     102              :              hfx_container_type, hfx_cache_type, &
     103              :              hfx_basis_type, parse_memory_section, &
     104              :              hfx_init_container, &
     105              :              hfx_basis_info_type, hfx_screen_coeff_type, &
     106              :              hfx_reset_memory_usage_counter, pair_list_type, pair_list_element_type, &
     107              :              pair_set_list_type, hfx_p_kind, hfx_2D_map, hfx_pgf_list, &
     108              :              hfx_pgf_product_list, hfx_block_range_type, &
     109              :              alloc_containers, dealloc_containers, hfx_task_list_type, init_t_c_g0_lmax, &
     110              :              hfx_create_neighbor_cells, hfx_create_basis_types, hfx_release_basis_types, &
     111              :              hfx_ri_type, hfx_compression_type, block_ind_type, hfx_ri_init, hfx_ri_release, &
     112              :              compare_hfx_sections
     113              : 
     114              : #define CACHE_SIZE 1024
     115              : #define BITS_MAX_VAL 6
     116              : 
     117              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_types'
     118              :    INTEGER, PARAMETER, PUBLIC                 :: max_atom_block = 32
     119              :    INTEGER, PARAMETER, PUBLIC                 :: max_images = 27
     120              :    REAL(dp), PARAMETER, PUBLIC                :: log_zero = -1000.0_dp
     121              :    REAL(dp), PARAMETER, PUBLIC                :: powell_min_log = -20.0_dp
     122              :    REAL(KIND=dp), DIMENSION(0:10), &
     123              :       PARAMETER, PUBLIC                       :: mul_fact = [1.0_dp, &
     124              :                                                              1.1781_dp, &
     125              :                                                              1.3333_dp, &
     126              :                                                              1.4726_dp, &
     127              :                                                              1.6000_dp, &
     128              :                                                              1.7181_dp, &
     129              :                                                              1.8286_dp, &
     130              :                                                              1.9328_dp, &
     131              :                                                              2.0317_dp, &
     132              :                                                              2.1261_dp, &
     133              :                                                              2.2165_dp]
     134              : 
     135              :    INTEGER, SAVE                                         :: init_t_c_g0_lmax = -1
     136              : 
     137              : !***
     138              : 
     139              : ! **************************************************************************************************
     140              :    TYPE hfx_potential_type
     141              :       INTEGER                                  :: potential_type = do_potential_coulomb !! 1/r/ erfc(wr)/r ...
     142              :       REAL(dp)                                 :: omega = 0.0_dp !! w
     143              :       REAL(dp)                                 :: scale_coulomb = 0.0_dp !! scaling factor for mixed potential
     144              :       REAL(dp)                                 :: scale_longrange = 0.0_dp !! scaling factor for mixed potential
     145              :       REAL(dp)                                 :: scale_gaussian = 0.0_dp!! scaling factor for mixed potential
     146              :       REAL(dp)                                 :: cutoff_radius = 0.0_dp!! cutoff radius if cutoff potential in use
     147              :       CHARACTER(default_path_length)           :: filename = ""
     148              :    END TYPE hfx_potential_type
     149              : 
     150              : ! **************************************************************************************************
     151              :    TYPE hfx_screening_type
     152              :       REAL(dp)                                 :: eps_schwarz = 0.0_dp !! threshold
     153              :       REAL(dp)                                 :: eps_schwarz_forces = 0.0_dp !! threshold
     154              :       LOGICAL                                  :: do_p_screening_forces = .FALSE. !! screen on P^2 ?
     155              :       LOGICAL                                  :: do_initial_p_screening = .FALSE. !! screen on initial guess?
     156              :    END TYPE hfx_screening_type
     157              : 
     158              : ! **************************************************************************************************
     159              :    TYPE hfx_memory_type
     160              :       INTEGER                                  :: max_memory = 0 !! user def max memory MiB
     161              :       INTEGER(int_8)                           :: max_compression_counter = 0_int_8 !! corresponding number of reals
     162              :       INTEGER(int_8)                           :: final_comp_counter_energy = 0_int_8
     163              :       LOGICAL                                  :: do_all_on_the_fly = .FALSE. !! max mem == 0 ?
     164              :       REAL(dp)                                 :: eps_storage_scaling = 0.0_dp
     165              :       INTEGER                                  :: cache_size = 0
     166              :       INTEGER                                  :: bits_max_val = 0
     167              :       INTEGER                                  :: actual_memory_usage = 0
     168              :       INTEGER                                  :: actual_memory_usage_disk = 0
     169              :       INTEGER(int_8)                           :: max_compression_counter_disk = 0_int_8
     170              :       LOGICAL                                  :: do_disk_storage = .FALSE.
     171              :       CHARACTER(len=default_path_length)       :: storage_location = ""
     172              :       INTEGER(int_8)                           :: ram_counter = 0_int_8
     173              :       INTEGER(int_8)                           :: ram_counter_forces = 0_int_8
     174              :       INTEGER(int_8)                           :: size_p_screen = 0_int_8
     175              :       LOGICAL                                  :: treat_forces_in_core = .FALSE.
     176              :       LOGICAL                                  :: recalc_forces = .FALSE.
     177              :    END TYPE hfx_memory_type
     178              : 
     179              : ! **************************************************************************************************
     180              :    TYPE hfx_periodic_type
     181              :       INTEGER                                  :: number_of_shells = -1 !! number of periodic image cells
     182              :       LOGICAL                                  :: do_periodic = .FALSE. !! periodic ?
     183              :       INTEGER                                  :: perd(3) = -1 !! x,xy,xyz,...
     184              :       INTEGER                                  :: mode = -1
     185              :       REAL(dp)                                 :: R_max_stress = 0.0_dp
     186              :       INTEGER                                  :: number_of_shells_from_input = 0
     187              :    END TYPE hfx_periodic_type
     188              : 
     189              : ! **************************************************************************************************
     190              :    TYPE hfx_load_balance_type
     191              :       INTEGER                                  :: nbins = 0
     192              :       INTEGER                                  :: block_size = 0
     193              :       INTEGER                                  :: nblocks = 0
     194              :       LOGICAL                                  :: rtp_redistribute = .FALSE.
     195              :       LOGICAL                                  :: blocks_initialized = .FALSE.
     196              :       LOGICAL                                  :: do_randomize = .FALSE.
     197              :    END TYPE hfx_load_balance_type
     198              : 
     199              : ! **************************************************************************************************
     200              :    TYPE hfx_general_type
     201              :       REAL(dp)                                 :: fraction = 0.0_dp !! for hybrids
     202              :       LOGICAL                                  :: treat_lsd_in_core = .FALSE.
     203              :    END TYPE hfx_general_type
     204              : 
     205              : ! **************************************************************************************************
     206              :    TYPE hfx_cell_type
     207              :       REAL(dp)                                 :: cell(3) = 0.0_dp
     208              :       REAL(dp)                                 :: cell_r(3) = 0.0_dp
     209              :    END TYPE hfx_cell_type
     210              : 
     211              : ! **************************************************************************************************
     212              :    TYPE hfx_distribution
     213              :       INTEGER(int_8)                           :: istart = 0_int_8
     214              :       INTEGER(int_8)                           :: number_of_atom_quartets = 0_int_8
     215              :       INTEGER(int_8)                           :: cost = 0_int_8
     216              :       REAL(KIND=dp)                            :: time_first_scf = 0.0_dp
     217              :       REAL(KIND=dp)                            :: time_other_scf = 0.0_dp
     218              :       REAL(KIND=dp)                            :: time_forces = 0.0_dp
     219              :       INTEGER(int_8)                           :: ram_counter = 0_int_8
     220              :    END TYPE hfx_distribution
     221              : 
     222              : ! **************************************************************************************************
     223              :    TYPE pair_list_element_type
     224              :       INTEGER, DIMENSION(2) :: pair = 0
     225              :       INTEGER, DIMENSION(2) :: set_bounds = 0
     226              :       INTEGER, DIMENSION(2) :: kind_pair = 0
     227              :       REAL(KIND=dp)         :: r1(3) = 0.0_dp, r2(3) = 0.0_dp
     228              :       REAL(KIND=dp)         :: dist2 = 0.0_dp
     229              :    END TYPE pair_list_element_type
     230              : 
     231              :    ! **************************************************************************************************
     232              :    TYPE pair_set_list_type
     233              :       INTEGER, DIMENSION(2) :: pair = 0
     234              :    END TYPE pair_set_list_type
     235              : 
     236              : ! **************************************************************************************************
     237              :    TYPE pair_list_type
     238              :       TYPE(pair_list_element_type), DIMENSION(max_atom_block**2) :: elements = pair_list_element_type()
     239              :       INTEGER :: n_element = 0
     240              :    END TYPE pair_list_type
     241              : 
     242              : ! **************************************************************************************************
     243              :    TYPE hfx_cache_type
     244              :       INTEGER(int_8), DIMENSION(CACHE_SIZE)    :: DATA = 0_int_8
     245              :       INTEGER                                  :: element_counter = 0
     246              :    END TYPE hfx_cache_type
     247              : 
     248              : ! **************************************************************************************************
     249              :    TYPE hfx_container_node
     250              :       TYPE(hfx_container_node), POINTER        :: next => NULL(), prev => NULL()
     251              :       INTEGER(int_8), DIMENSION(CACHE_SIZE)    :: DATA = 0_int_8
     252              :    END TYPE hfx_container_node
     253              : 
     254              : ! **************************************************************************************************
     255              :    TYPE hfx_container_type
     256              :       TYPE(hfx_container_node), POINTER        :: first => NULL(), current => NULL()
     257              :       INTEGER                                  :: element_counter = 0
     258              :       INTEGER(int_8)                           :: file_counter = 0
     259              :       CHARACTER(LEN=5)                         :: desc = ""
     260              :       INTEGER                                  :: unit = -1
     261              :       CHARACTER(default_path_length)           :: filename = ""
     262              :    END TYPE hfx_container_type
     263              : 
     264              : ! **************************************************************************************************
     265              :    TYPE hfx_basis_type
     266              :       INTEGER, DIMENSION(:), POINTER           :: lmax => NULL()
     267              :       INTEGER, DIMENSION(:), POINTER           :: lmin => NULL()
     268              :       INTEGER, DIMENSION(:), POINTER           :: npgf => NULL()
     269              :       INTEGER                                  :: nset = 0
     270              :       REAL(dp), DIMENSION(:, :), POINTER        :: zet => NULL()
     271              :       INTEGER, DIMENSION(:), POINTER           :: nsgf => NULL()
     272              :       INTEGER, DIMENSION(:, :), POINTER         :: first_sgf => NULL()
     273              :       REAL(dp), DIMENSION(:, :), POINTER        :: sphi => NULL()
     274              :       INTEGER                                  :: nsgf_total = 0
     275              :       INTEGER, DIMENSION(:, :), POINTER         :: nl => NULL()
     276              :       INTEGER, DIMENSION(:, :), POINTER         :: nsgfl => NULL()
     277              :       INTEGER, DIMENSION(:), POINTER           :: nshell => NULL()
     278              :       REAL(dp), DIMENSION(:, :, :, :), POINTER &
     279              :          :: sphi_ext => NULL()
     280              :       REAL(dp), DIMENSION(:), POINTER          :: set_radius => NULL()
     281              :       REAL(dp), DIMENSION(:, :), POINTER        :: pgf_radius => NULL()
     282              :       REAL(dp)                                 :: kind_radius = 0.0_dp
     283              :    END TYPE hfx_basis_type
     284              : 
     285              : ! **************************************************************************************************
     286              :    TYPE hfx_basis_info_type
     287              :       INTEGER                                  :: max_set = 0
     288              :       INTEGER                                  :: max_sgf = 0
     289              :       INTEGER                                  :: max_am = 0
     290              :    END TYPE hfx_basis_info_type
     291              : 
     292              : ! **************************************************************************************************
     293              :    TYPE hfx_screen_coeff_type
     294              :       REAL(dp)                                 :: x(2) = 0.0_dp
     295              :    END TYPE hfx_screen_coeff_type
     296              : 
     297              : ! **************************************************************************************************
     298              :    TYPE hfx_p_kind
     299              :       REAL(dp), DIMENSION(:, :, :, :), POINTER    :: p_kind => NULL()
     300              :    END TYPE hfx_p_kind
     301              : 
     302              : ! **************************************************************************************************
     303              :    TYPE hfx_2D_map
     304              :       INTEGER, DIMENSION(:), POINTER           :: iatom_list => NULL()
     305              :       INTEGER, DIMENSION(:), POINTER           :: jatom_list => NULL()
     306              :    END TYPE hfx_2D_map
     307              : 
     308              : ! **************************************************************************************************
     309              :    TYPE hfx_pgf_image
     310              :       REAL(dp)                                 :: ra(3) = 0.0_dp, rb(3) = 0.0_dp
     311              :       REAL(dp)                                 :: rab2 = 0.0_dp
     312              :       REAL(dp)                                 :: S1234 = 0.0_dp
     313              :       REAL(dp)                                 :: P(3) = 0.0_dp
     314              :       REAL(dp)                                 :: R = 0.0_dp
     315              :       REAL(dp)                                 :: pgf_max = 0.0_dp
     316              :       REAL(dp), DIMENSION(3)                   :: bcell = 0.0_dp
     317              :    END TYPE hfx_pgf_image
     318              : 
     319              : ! **************************************************************************************************
     320              :    TYPE hfx_pgf_list
     321              :       TYPE(hfx_pgf_image), DIMENSION(:), POINTER &
     322              :          :: image_list => NULL()
     323              :       INTEGER                                  :: nimages = 0
     324              :       REAL(dp)                                 :: zetapzetb = 0.0_dp
     325              :       REAL(dp)                                 :: ZetaInv = 0.0_dp
     326              :       REAL(dp)                                 :: zeta = 0.0_dp, zetb = 0.0_dp
     327              :       INTEGER                                  :: ipgf = 0, jpgf = 0
     328              :    END TYPE hfx_pgf_list
     329              : 
     330              : ! **************************************************************************************************
     331              :    TYPE hfx_pgf_product_list
     332              :       REAL(dp)                                 :: ra(3) = 0.0_dp, rb(3) = 0.0_dp, rc(3) = 0.0_dp, rd(3) = 0.0_dp
     333              :       REAL(dp)                                 :: ZetapEtaInv = 0.0_dp
     334              :       REAL(dp)                                 :: Rho = 0.0_dp, RhoInv = 0.0_dp
     335              :       REAL(dp)                                 :: P(3) = 0.0_dp, Q(3) = 0.0_dp, W(3) = 0.0_dp
     336              :       REAL(dp)                                 :: AB(3) = 0.0_dp, CD(3) = 0.0_dp
     337              :       REAL(dp)                                 :: Fm(prim_data_f_size) = 0.0_dp
     338              :    END TYPE hfx_pgf_product_list
     339              : 
     340              : ! **************************************************************************************************
     341              :    TYPE hfx_block_range_type
     342              :       INTEGER        :: istart = 0, iend = 0
     343              :       INTEGER(int_8) :: cost = 0_int_8
     344              :    END TYPE hfx_block_range_type
     345              : 
     346              : ! **************************************************************************************************
     347              :    TYPE hfx_task_list_type
     348              :       INTEGER                                  :: thread_id = 0
     349              :       INTEGER                                  :: bin_id = 0
     350              :       INTEGER(int_8)                           :: cost = 0_int_8
     351              :    END TYPE hfx_task_list_type
     352              : 
     353              :    TYPE :: hfx_compression_type
     354              :       TYPE(hfx_container_type), DIMENSION(:), &
     355              :          POINTER        :: maxval_container => NULL()
     356              :       TYPE(hfx_cache_type), DIMENSION(:), &
     357              :          POINTER            :: maxval_cache => NULL()
     358              :       TYPE(hfx_container_type), DIMENSION(:, :), &
     359              :          POINTER        :: integral_containers => NULL()
     360              :       TYPE(hfx_cache_type), DIMENSION(:, :), &
     361              :          POINTER            :: integral_caches => NULL()
     362              :       TYPE(hfx_container_type), POINTER :: maxval_container_disk => NULL()
     363              :       TYPE(hfx_cache_type)     :: maxval_cache_disk = hfx_cache_type()
     364              :       TYPE(hfx_cache_type)      :: integral_caches_disk(64) = hfx_cache_type()
     365              :       TYPE(hfx_container_type), POINTER, &
     366              :          DIMENSION(:)  :: integral_containers_disk => NULL()
     367              :    END TYPE hfx_compression_type
     368              : 
     369              :    TYPE :: block_ind_type
     370              :       INTEGER, DIMENSION(:, :), ALLOCATABLE :: ind
     371              :    END TYPE block_ind_type
     372              : 
     373              :    TYPE hfx_ri_type
     374              :       ! input parameters (see input_cp2k_hfx)
     375              :       REAL(KIND=dp) :: filter_eps = 0.0_dp, filter_eps_2c = 0.0_dp, filter_eps_storage = 0.0_dp, filter_eps_mo = 0.0_dp, &
     376              :                        eps_lanczos = 0.0_dp, eps_pgf_orb = 0.0_dp, eps_eigval = 0.0_dp, kp_RI_range = 0.0_dp, &
     377              :                        kp_image_range = 0.0_dp, kp_bump_rad = 0.0_dp
     378              :       INTEGER :: t2c_sqrt_order = 0, max_iter_lanczos = 0, flavor = 0, unit_nr_dbcsr = -1, unit_nr = -1, &
     379              :                  min_bsize = 0, max_bsize_MO = 0, t2c_method = 0, nelectron_total = 0, input_flavor = 0, &
     380              :                  ncell_RI = 0, nimg = 0, kp_stack_size = 0, nimg_nze = 0, kp_ngroups = 1
     381              :       LOGICAL :: check_2c_inv = .FALSE., calc_condnum = .FALSE.
     382              : 
     383              :       TYPE(libint_potential_type) :: ri_metric = libint_potential_type()
     384              : 
     385              :       ! input parameters from hfx
     386              :       TYPE(libint_potential_type) :: hfx_pot = libint_potential_type() ! interaction potential
     387              :       REAL(KIND=dp) :: eps_schwarz = 0.0_dp ! integral screening threshold
     388              :       REAL(KIND=dp) :: eps_schwarz_forces = 0.0_dp ! integral derivatives screening threshold
     389              : 
     390              :       LOGICAL :: same_op = .FALSE. ! whether RI operator is same as HF potential
     391              : 
     392              :       ! default process grid used for 3c tensors
     393              :       TYPE(dbt_pgrid_type), POINTER :: pgrid => NULL()
     394              :       TYPE(dbt_pgrid_type), POINTER :: pgrid_2d => NULL()
     395              : 
     396              :       ! distributions for (RI | AO AO) 3c integral tensor (non split)
     397              :       TYPE(distribution_3d_type) :: dist_3d = distribution_3d_type()
     398              :       TYPE(dbt_distribution_type) :: dist
     399              : 
     400              :       ! block sizes for RI and AO tensor dimensions (split)
     401              :       INTEGER, DIMENSION(:), ALLOCATABLE :: bsizes_RI, bsizes_AO, bsizes_RI_split, bsizes_AO_split, &
     402              :                                             bsizes_RI_fit, bsizes_AO_fit
     403              : 
     404              :       ! KP RI-HFX basis info
     405              :       INTEGER, DIMENSION(:), ALLOCATABLE ::  img_to_RI_cell, present_images, idx_to_img, img_to_idx, &
     406              :                                             RI_cell_to_img
     407              : 
     408              :       ! KP RI-HFX cost information for a given atom pair i,j at a given cell b
     409              :       REAL(dp), DIMENSION(:, :, :), ALLOCATABLE :: kp_cost
     410              : 
     411              :       ! KP distribution of iatom (of i,j atom pairs) to subgroups
     412              :       TYPE(cp_1d_logical_p_type), DIMENSION(:), ALLOCATABLE :: iatom_to_subgroup
     413              : 
     414              :       ! KP 3c tensors replicated on the subgroups
     415              :       TYPE(dbt_type), DIMENSION(:), ALLOCATABLE :: kp_t_3c_int
     416              : 
     417              :       ! Note: changed static DIMENSION(1,1) of dbt_type to allocatables as workaround for gfortran 8.3.0,
     418              :       ! with static dimension gfortran gets stuck during compilation
     419              : 
     420              :       ! 2c tensors in (AO | AO) format
     421              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE :: rho_ao_t, ks_t
     422              : 
     423              :       ! 2c tensors in (RI | RI) format for forces
     424              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE    :: t_2c_inv
     425              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE    :: t_2c_pot
     426              : 
     427              :       ! 2c tensor in matrix format for K-points RI-HFX
     428              :       TYPE(dbcsr_type), DIMENSION(:, :), ALLOCATABLE  :: kp_mat_2c_pot
     429              : 
     430              :       ! 2c tensor in (RI | RI) format for contraction
     431              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE    :: t_2c_int
     432              : 
     433              :       ! 3c integral tensor in (AO RI | AO) format for contraction
     434              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE :: t_3c_int_ctr_1
     435              :       TYPE(block_ind_type), DIMENSION(:, :), ALLOCATABLE :: blk_indices
     436              :       TYPE(dbt_pgrid_type), POINTER                :: pgrid_1 => NULL()
     437              : 
     438              :       ! 3c integral tensor in ( AO | RI AO) (MO) or (AO RI | AO) (RHO) format for contraction
     439              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE :: t_3c_int_ctr_2
     440              :       TYPE(dbt_pgrid_type), POINTER                :: pgrid_2 => NULL()
     441              : 
     442              :       ! 3c integral tensor in ( RI | AO AO ) format for contraction
     443              :       TYPE(dbt_type), DIMENSION(:, :), ALLOCATABLE :: t_3c_int_ctr_3
     444              : 
     445              :       ! 3c integral tensor in (RI | MO AO ) format for contraction
     446              :       TYPE(dbt_type), DIMENSION(:, :, :), ALLOCATABLE :: t_3c_int_mo
     447              :       TYPE(dbt_type), DIMENSION(:, :, :), ALLOCATABLE :: t_3c_ctr_RI
     448              :       TYPE(dbt_type), DIMENSION(:, :, :), ALLOCATABLE :: t_3c_ctr_KS
     449              :       TYPE(dbt_type), DIMENSION(:, :, :), ALLOCATABLE :: t_3c_ctr_KS_copy
     450              : 
     451              :       ! optional: sections for output handling
     452              :       ! alternatively set unit_nr_dbcsr (for logging tensor operations) and unit_nr (for general
     453              :       ! output) directly
     454              :       TYPE(section_vals_type), POINTER :: ri_section => NULL(), hfx_section => NULL()
     455              : 
     456              :       ! types of primary and auxiliary basis
     457              :       CHARACTER(len=default_string_length) :: orb_basis_type = "", ri_basis_type = ""
     458              : 
     459              :       ! memory reduction factor
     460              :       INTEGER :: n_mem_input = 0, n_mem = 0, n_mem_RI = 0, n_mem_flavor_switch = 0
     461              : 
     462              :       ! offsets for memory batches
     463              :       INTEGER, DIMENSION(:), ALLOCATABLE :: starts_array_mem_block, ends_array_mem_block
     464              :       INTEGER, DIMENSION(:), ALLOCATABLE :: starts_array_mem, ends_array_mem
     465              : 
     466              :       INTEGER, DIMENSION(:), ALLOCATABLE :: starts_array_RI_mem_block, ends_array_RI_mem_block
     467              :       INTEGER, DIMENSION(:), ALLOCATABLE :: starts_array_RI_mem, ends_array_RI_mem
     468              : 
     469              :       INTEGER(int_8) :: dbcsr_nflop = 0_int_8
     470              :       REAL(dp)       :: dbcsr_time = 0.0_dp
     471              :       INTEGER        :: num_pe = 0
     472              :       TYPE(hfx_compression_type), DIMENSION(:, :), ALLOCATABLE :: store_3c
     473              : 
     474              :    END TYPE hfx_ri_type
     475              : 
     476              : ! **************************************************************************************************
     477              : !> \brief stores some data used in construction of Kohn-Sham matrix
     478              : !> \param potential_parameter stores information on the potential (1/r, erfc(wr)/r
     479              : !> \param screening_parameter stores screening infos such as epsilon
     480              : !> \param memory_parameter stores infos on memory used for in-core calculations
     481              : !> \param periodic_parameter stores information on how to apply pbc
     482              : !> \param load_balance_parameter contains infos for Monte Carlo simulated annealing
     483              : !> \param general_paramter at the moment stores the fraction of HF amount to be included
     484              : !> \param maxval_container stores the maxvals in compressed form
     485              : !> \param maxval_cache cache for maxvals in decompressed form
     486              : !> \param integral_containers 64 containers for compressed integrals
     487              : !> \param integral_caches 64 caches for decompressed integrals
     488              : !> \param neighbor_cells manages handling of periodic cells
     489              : !> \param distribution_energy stores information on parallelization of energy
     490              : !> \param distribution_forces stores information on parallelization of forces
     491              : !> \param initial_p stores the initial guess if requested
     492              : !> \param is_assoc_atomic_block reflects KS sparsity
     493              : !> \param number_of_p_entries Size of P matrix
     494              : !> \param n_rep_hf Number of HFX replicas
     495              : !> \param b_first_load_balance_x flag to indicate if it is enough just to update
     496              : !>        the distribution of the integrals
     497              : !> \param full_ks_x full ks matrices
     498              : !> \param lib libint type for eris
     499              : !> \param basis_info contains information for basis sets
     500              : !> \param screen_funct_coeffs_pgf pgf based near field screening coefficients
     501              : !> \param pair_dist_radii_pgf pgf based radii coefficients of pair distributions
     502              : !> \param screen_funct_coeffs_set set based near field screening coefficients
     503              : !> \param screen_funct_coeffs_kind kind based near field screening coefficients
     504              : !> \param screen_funct_is_initialized flag that indicates if the coefficients
     505              : !>        have already been fitted
     506              : !> \par History
     507              : !>      11.2006 created [Manuel Guidon]
     508              : !>      02.2009 completely rewritten due to new screening
     509              : !> \author Manuel Guidon
     510              : ! **************************************************************************************************
     511              :    TYPE hfx_type
     512              :       TYPE(hfx_potential_type)                 :: potential_parameter = hfx_potential_type()
     513              :       TYPE(hfx_screening_type)                 :: screening_parameter = hfx_screening_type()
     514              :       TYPE(hfx_memory_type)                    :: memory_parameter = hfx_memory_type()
     515              :       TYPE(hfx_periodic_type)                  :: periodic_parameter = hfx_periodic_type()
     516              :       TYPE(hfx_load_balance_type)              :: load_balance_parameter = hfx_load_balance_type()
     517              :       TYPE(hfx_general_type)                   :: general_parameter = hfx_general_type()
     518              : 
     519              :       TYPE(hfx_compression_type) :: store_ints = hfx_compression_type()
     520              :       TYPE(hfx_compression_type) :: store_forces = hfx_compression_type()
     521              : 
     522              :       TYPE(hfx_cell_type), DIMENSION(:), &
     523              :          POINTER                       :: neighbor_cells => NULL()
     524              :       TYPE(hfx_distribution), DIMENSION(:), &
     525              :          POINTER         :: distribution_energy => NULL()
     526              :       TYPE(hfx_distribution), DIMENSION(:), &
     527              :          POINTER         :: distribution_forces => NULL()
     528              :       INTEGER, DIMENSION(:, :), POINTER         :: is_assoc_atomic_block => NULL()
     529              :       INTEGER                                  :: number_of_p_entries = 0
     530              :       TYPE(hfx_basis_type), DIMENSION(:), &
     531              :          POINTER           :: basis_parameter => NULL()
     532              :       INTEGER                                  :: n_rep_hf = 0
     533              :       LOGICAL                                  :: b_first_load_balance_energy = .FALSE., &
     534              :                                                   b_first_load_balance_forces = .FALSE.
     535              :       REAL(dp), DIMENSION(:, :), POINTER        :: full_ks_alpha => NULL()
     536              :       REAL(dp), DIMENSION(:, :), POINTER        :: full_ks_beta => NULL()
     537              :       TYPE(cp_libint_t)                        :: lib
     538              :       TYPE(hfx_basis_info_type)                :: basis_info = hfx_basis_info_type()
     539              :       TYPE(hfx_screen_coeff_type), &
     540              :          DIMENSION(:, :, :, :, :, :), POINTER     :: screen_funct_coeffs_pgf => NULL(), &
     541              :                                                      pair_dist_radii_pgf => NULL()
     542              :       TYPE(hfx_screen_coeff_type), &
     543              :          DIMENSION(:, :, :, :), POINTER         :: screen_funct_coeffs_set => NULL()
     544              :       TYPE(hfx_screen_coeff_type), &
     545              :          DIMENSION(:, :), POINTER             :: screen_funct_coeffs_kind => NULL()
     546              :       LOGICAL                                  :: screen_funct_is_initialized = .FALSE.
     547              :       TYPE(hfx_p_kind), DIMENSION(:), POINTER  :: initial_p => NULL()
     548              :       TYPE(hfx_p_kind), DIMENSION(:), POINTER  :: initial_p_forces => NULL()
     549              :       INTEGER, DIMENSION(:), POINTER           :: map_atom_to_kind_atom => NULL()
     550              :       TYPE(hfx_2D_map), DIMENSION(:), POINTER  :: map_atoms_to_cpus => NULL()
     551              :       INTEGER, DIMENSION(:, :), POINTER         :: atomic_block_offset => NULL()
     552              :       INTEGER, DIMENSION(:, :, :, :), POINTER     :: set_offset => NULL()
     553              :       INTEGER, DIMENSION(:), POINTER           :: block_offset => NULL()
     554              :       TYPE(hfx_block_range_type), DIMENSION(:), &
     555              :          POINTER      :: blocks => NULL()
     556              :       TYPE(hfx_task_list_type), DIMENSION(:), &
     557              :          POINTER        :: task_list => NULL()
     558              :       REAL(dp), DIMENSION(:, :), POINTER        :: pmax_atom => NULL(), pmax_atom_forces => NULL()
     559              :       TYPE(cp_libint_t)                         :: lib_deriv
     560              :       REAL(dp), DIMENSION(:, :), POINTER        :: pmax_block => NULL()
     561              :       LOGICAL, DIMENSION(:, :), POINTER         :: atomic_pair_list => NULL()
     562              :       LOGICAL, DIMENSION(:, :), POINTER         :: atomic_pair_list_forces => NULL()
     563              :       LOGICAL                                   :: do_hfx_ri = .FALSE.
     564              :       TYPE(hfx_ri_type), POINTER                :: ri_data => NULL()
     565              :    END TYPE hfx_type
     566              : 
     567              : CONTAINS
     568              : 
     569              : ! **************************************************************************************************
     570              : !> \brief - This routine allocates and initializes all types in hfx_data
     571              : !> \param x_data contains all relevant data structures for hfx runs
     572              : !> \param para_env ...
     573              : !> \param hfx_section input section
     574              : !> \param atomic_kind_set ...
     575              : !> \param qs_kind_set ...
     576              : !> \param particle_set ...
     577              : !> \param dft_control ...
     578              : !> \param cell ...
     579              : !> \param orb_basis ...
     580              : !> \param ri_basis ...
     581              : !> \param nelectron_total ...
     582              : !> \param nkp_grid ...
     583              : !> \par History
     584              : !>      09.2007 created [Manuel Guidon]
     585              : !>      01.2024 pushed basis set decision outside of routine, keeps default as
     586              : !>              orb_basis = "ORB" and ri_basis = "AUX_FIT"
     587              : !>              No more ADMM references!
     588              : !> \author Manuel Guidon
     589              : !> \note
     590              : !>      - All POINTERS and ALLOCATABLES are allocated, even if their size is
     591              : !>        unknown at invocation time
     592              : ! **************************************************************************************************
     593         1342 :    SUBROUTINE hfx_create(x_data, para_env, hfx_section, atomic_kind_set, qs_kind_set, &
     594              :                          particle_set, dft_control, cell, orb_basis, ri_basis, &
     595              :                          nelectron_total, nkp_grid)
     596              :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
     597              :       TYPE(mp_para_env_type)                             :: para_env
     598              :       TYPE(section_vals_type), POINTER                   :: hfx_section
     599              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     600              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     601              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     602              :       TYPE(dft_control_type), POINTER                    :: dft_control
     603              :       TYPE(cell_type), POINTER                           :: cell
     604              :       CHARACTER(LEN=*), OPTIONAL                         :: orb_basis, ri_basis
     605              :       INTEGER, OPTIONAL                                  :: nelectron_total
     606              :       INTEGER, DIMENSION(3), OPTIONAL                    :: nkp_grid
     607              : 
     608              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'hfx_create'
     609              : 
     610              :       CHARACTER(LEN=512)                                 :: error_msg
     611              :       CHARACTER(LEN=default_path_length)                 :: char_val
     612              :       CHARACTER(LEN=default_string_length)               :: orb_basis_type, ri_basis_type
     613              :       INTEGER :: handle, i, i_thread, iatom, ikind, int_val, irep, jkind, max_set, n_rep_hf, &
     614              :          n_threads, natom, natom_a, natom_b, nkind, nseta, nsetb, pbc_shells, storage_id
     615         1342 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom2kind, kind_of
     616              :       LOGICAL                                            :: do_ri, explicit, logic_val
     617              :       REAL(dp)                                           :: real_val
     618              :       TYPE(hfx_type), POINTER                            :: actual_x_data
     619              :       TYPE(section_vals_type), POINTER                   :: hf_pbc_section, hf_sub_section, &
     620              :                                                             hfx_ri_section
     621              : 
     622         1342 :       CALL timeset(routineN, handle)
     623              : 
     624         1342 :       CALL cite_reference(Guidon2008)
     625         1342 :       CALL cite_reference(Guidon2009)
     626              : 
     627         1342 :       natom = SIZE(particle_set)
     628              : 
     629              :       !! There might be 2 hf sections
     630         1342 :       CALL section_vals_get(hfx_section, n_repetition=n_rep_hf)
     631         1342 :       n_threads = 1
     632         1342 : !$    n_threads = omp_get_max_threads()
     633              : 
     634         1342 :       CALL section_vals_val_get(hfx_section, "RI%_SECTION_PARAMETERS_", l_val=do_ri)
     635         1342 :       IF (do_ri) n_threads = 1 ! RI implementation does not use threads
     636              : 
     637         1342 :       IF (PRESENT(orb_basis)) THEN
     638         1342 :          orb_basis_type = orb_basis
     639              :       ELSE
     640            0 :          orb_basis_type = "ORB"
     641              :       END IF
     642         1342 :       IF (PRESENT(ri_basis)) THEN
     643            0 :          ri_basis_type = ri_basis
     644              :       ELSE
     645         1342 :          ri_basis_type = "RI_HFX"
     646              :       END IF
     647              : 
     648      5682038 :       ALLOCATE (x_data(n_rep_hf, n_threads))
     649         2684 :       DO i_thread = 1, n_threads
     650         4036 :          DO irep = 1, n_rep_hf
     651         1352 :             actual_x_data => x_data(irep, i_thread)
     652              :             !! Get data from input file
     653              :             !!
     654              :             !! GENERAL params
     655         1352 :             CALL section_vals_val_get(hfx_section, "FRACTION", r_val=real_val, i_rep_section=irep)
     656         1352 :             actual_x_data%general_parameter%fraction = real_val
     657         1352 :             actual_x_data%n_rep_hf = n_rep_hf
     658              : 
     659         1352 :             NULLIFY (actual_x_data%map_atoms_to_cpus)
     660              : 
     661         1352 :             CALL section_vals_val_get(hfx_section, "TREAT_LSD_IN_CORE", l_val=logic_val, i_rep_section=irep)
     662         1352 :             actual_x_data%general_parameter%treat_lsd_in_core = logic_val
     663              : 
     664         1352 :             hfx_ri_section => section_vals_get_subs_vals(hfx_section, "RI")
     665         1352 :             CALL section_vals_val_get(hfx_ri_section, "_SECTION_PARAMETERS_", l_val=actual_x_data%do_hfx_ri)
     666              : 
     667              :             !! MEMORY section
     668         1352 :             hf_sub_section => section_vals_get_subs_vals(hfx_section, "MEMORY", i_rep_section=irep)
     669              :             CALL parse_memory_section(actual_x_data%memory_parameter, hf_sub_section, storage_id, i_thread, &
     670         1352 :                                       n_threads, para_env, irep, skip_disk=.FALSE., skip_in_core_forces=.FALSE.)
     671              : 
     672              :             !! PERIODIC section
     673         1352 :             hf_sub_section => section_vals_get_subs_vals(hfx_section, "PERIODIC", i_rep_section=irep)
     674         1352 :             CALL section_vals_val_get(hf_sub_section, "NUMBER_OF_SHELLS", i_val=int_val)
     675         1352 :             actual_x_data%periodic_parameter%number_of_shells = int_val
     676         1352 :             actual_x_data%periodic_parameter%mode = int_val
     677         1352 :             CALL get_cell(cell=cell, periodic=actual_x_data%periodic_parameter%perd)
     678         5408 :             IF (SUM(actual_x_data%periodic_parameter%perd) == 0) THEN
     679          926 :                actual_x_data%periodic_parameter%do_periodic = .FALSE.
     680              :             ELSE
     681          426 :                actual_x_data%periodic_parameter%do_periodic = .TRUE.
     682              :             END IF
     683              : 
     684              :             !! SCREENING section
     685         1352 :             hf_sub_section => section_vals_get_subs_vals(hfx_section, "SCREENING", i_rep_section=irep)
     686         1352 :             CALL section_vals_val_get(hf_sub_section, "EPS_SCHWARZ", r_val=real_val)
     687         1352 :             actual_x_data%screening_parameter%eps_schwarz = real_val
     688         1352 :             CALL section_vals_val_get(hf_sub_section, "EPS_SCHWARZ_FORCES", r_val=real_val, explicit=explicit)
     689         1352 :             IF (explicit) THEN
     690          194 :                actual_x_data%screening_parameter%eps_schwarz_forces = real_val
     691              :             ELSE
     692              :                actual_x_data%screening_parameter%eps_schwarz_forces = &
     693         1158 :                   100._dp*actual_x_data%screening_parameter%eps_schwarz
     694              :             END IF
     695         1352 :             CALL section_vals_val_get(hf_sub_section, "SCREEN_P_FORCES", l_val=logic_val)
     696         1352 :             actual_x_data%screening_parameter%do_p_screening_forces = logic_val
     697         1352 :             CALL section_vals_val_get(hf_sub_section, "SCREEN_ON_INITIAL_P", l_val=logic_val)
     698         1352 :             actual_x_data%screening_parameter%do_initial_p_screening = logic_val
     699         1352 :             actual_x_data%screen_funct_is_initialized = .FALSE.
     700              : 
     701              :             !! INTERACTION_POTENTIAL section
     702         1352 :             hf_sub_section => section_vals_get_subs_vals(hfx_section, "INTERACTION_POTENTIAL", i_rep_section=irep)
     703         1352 :             CALL section_vals_val_get(hf_sub_section, "POTENTIAL_TYPE", i_val=int_val)
     704         1352 :             actual_x_data%potential_parameter%potential_type = int_val
     705         1352 :             CALL section_vals_val_get(hf_sub_section, "OMEGA", r_val=real_val)
     706         1352 :             actual_x_data%potential_parameter%omega = real_val
     707         1352 :             CALL section_vals_val_get(hf_sub_section, "SCALE_COULOMB", r_val=real_val)
     708         1352 :             actual_x_data%potential_parameter%scale_coulomb = real_val
     709         1352 :             CALL section_vals_val_get(hf_sub_section, "SCALE_LONGRANGE", r_val=real_val)
     710         1352 :             actual_x_data%potential_parameter%scale_longrange = real_val
     711         1352 :             CALL section_vals_val_get(hf_sub_section, "SCALE_GAUSSIAN", r_val=real_val)
     712         1352 :             actual_x_data%potential_parameter%scale_gaussian = real_val
     713         1352 :             IF (actual_x_data%potential_parameter%potential_type == do_potential_truncated .OR. &
     714              :                 actual_x_data%potential_parameter%potential_type == do_potential_mix_cl_trunc) THEN
     715          368 :                CALL section_vals_val_get(hf_sub_section, "CUTOFF_RADIUS", r_val=real_val)
     716          368 :                actual_x_data%potential_parameter%cutoff_radius = real_val
     717          368 :                CALL section_vals_val_get(hf_sub_section, "T_C_G_DATA", c_val=char_val)
     718          368 :                CALL compress(char_val, .TRUE.)
     719              :                ! ** Check if file is there
     720          368 :                IF (.NOT. file_exists(char_val)) THEN
     721              :                   WRITE (error_msg, '(A,A,A)') "Truncated hfx calculation requested. The file containing "// &
     722            0 :                      "the data could not be found at ", TRIM(char_val), " Please check T_C_G_DATA "// &
     723            0 :                      "in the INTERACTION_POTENTIAL section"
     724            0 :                   CPABORT(error_msg)
     725              :                ELSE
     726          368 :                   actual_x_data%potential_parameter%filename = char_val
     727              :                END IF
     728              :             END IF
     729         1352 :             IF (actual_x_data%potential_parameter%potential_type == do_potential_short) THEN
     730              :                CALL erfc_cutoff(actual_x_data%screening_parameter%eps_schwarz, &
     731              :                                 actual_x_data%potential_parameter%omega, &
     732           48 :                                 actual_x_data%potential_parameter%cutoff_radius)
     733              :             END IF
     734         1352 :             IF (actual_x_data%potential_parameter%potential_type == do_potential_id) THEN
     735           22 :                actual_x_data%potential_parameter%cutoff_radius = 0.0_dp
     736              :             END IF
     737              : 
     738              :             !! LOAD_BALANCE section
     739         1352 :             hf_sub_section => section_vals_get_subs_vals(hfx_section, "LOAD_BALANCE", i_rep_section=irep)
     740         1352 :             CALL section_vals_val_get(hf_sub_section, "NBINS", i_val=int_val)
     741         1352 :             actual_x_data%load_balance_parameter%nbins = MAX(1, int_val)
     742         1352 :             actual_x_data%load_balance_parameter%blocks_initialized = .FALSE.
     743              : 
     744         1352 :             CALL section_vals_val_get(hf_sub_section, "RANDOMIZE", l_val=logic_val)
     745         1352 :             actual_x_data%load_balance_parameter%do_randomize = logic_val
     746              : 
     747         1352 :             actual_x_data%load_balance_parameter%rtp_redistribute = .FALSE.
     748         1352 :             IF (ASSOCIATED(dft_control%rtp_control)) &
     749           34 :                actual_x_data%load_balance_parameter%rtp_redistribute = dft_control%rtp_control%hfx_redistribute
     750              : 
     751         1352 :             CALL section_vals_val_get(hf_sub_section, "BLOCK_SIZE", i_val=int_val)
     752              :             ! negative values ask for a computed default
     753         1352 :             IF (int_val <= 0) THEN
     754              :                ! this gives a reasonable number of blocks for binning, yet typically results in blocking.
     755              :                int_val = CEILING(0.1_dp*natom/ &
     756         1352 :                                  REAL(actual_x_data%load_balance_parameter%nbins*n_threads*para_env%num_pe, KIND=dp)**(0.25_dp))
     757              :             END IF
     758              :             ! at least 1 atom per block, and avoid overly large blocks
     759         1352 :             actual_x_data%load_balance_parameter%block_size = MIN(max_atom_block, MAX(1, int_val))
     760              : 
     761              :             CALL hfx_create_basis_types(actual_x_data%basis_parameter, actual_x_data%basis_info, qs_kind_set, &
     762         1352 :                                         orb_basis_type)
     763              : 
     764              : !!**************************************************************************************************
     765              : !! **        !! ** This code writes the contraction routines
     766              : !! **        !! ** Very UGLY: BASIS_SET has to be 1 primitive and lmin=lmax=l. For g-functions
     767              : !! **        !! **
     768              : !! **        !! ** 1  4  4  1  1
     769              : !! **        !! **    1.0  1.0
     770              : !! **        !! **
     771              : !! **        k = max_am - 1
     772              : !! **        write(filename,'(A,I0,A)') "sphi",k+1,"a"
     773              : !! **        OPEN(UNIT=31415,FILE=filename)
     774              : !! **        DO i=ncoset(k)+1,SIZE(sphi_a,1)
     775              : !! **          DO j=1,SIZE(sphi_a,2)
     776              : !! **            IF( sphi_a(i,j) /= 0.0_dp) THEN
     777              : !! **              write(31415,'(A,I0,A,I0,A,I0,A,I0,A,I0,A)') "buffer1(i+imax*(",&
     778              : !! **                          j,&
     779              : !! **                          "-1)) = buffer1(i+imax*(",&
     780              : !! **                          j,&
     781              : !! **                          "-1)) + work(",&
     782              : !! **                          i-ncoset(k),&
     783              : !! **                          "+(i-1)*kmax) * sphi_a(",&
     784              : !! **                          i-ncoset(k),&
     785              : !! **                          ",",&
     786              : !! **                          j,&
     787              : !! **                          "+s_offset_a1)"
     788              : !! **            END IF
     789              : !! **          END DO
     790              : !! **        END DO
     791              : !! **        CLOSE(UNIT=31415)
     792              : !! **        write(filename,'(A,I0,A)') "sphi",k+1,"b"
     793              : !! **        OPEN(UNIT=31415,FILE=filename)
     794              : !! **        DO i=ncoset(k)+1,SIZE(sphi_a,1)
     795              : !! **          DO j=1,SIZE(sphi_a,2)
     796              : !! **            IF( sphi_a(i,j) /= 0.0_dp) THEN
     797              : !! **               write(31415,'(A,I0,A,I0,A,I0,A,I0,A,I0,A)') "buffer2(i+imax*(",&
     798              : !! **                          j,&
     799              : !! **                          "-1)) = buffer2(i+imax*(",&
     800              : !! **                          j,&
     801              : !! **                          "-1)) + buffer1(",&
     802              : !! **                          i-ncoset(k),&
     803              : !! **                          "+(i-1)*kmax) * sphi_b(",&
     804              : !! **                          i-ncoset(k),&
     805              : !! **                          ",",&
     806              : !! **                          j,&
     807              : !! **                          "+s_offset_b1)"
     808              : !! **
     809              : !! **            END IF
     810              : !! **          END DO
     811              : !! **        END DO
     812              : !! **        CLOSE(UNIT=31415)
     813              : !! **        write(filename,'(A,I0,A)') "sphi",k+1,"c"
     814              : !! **        OPEN(UNIT=31415,FILE=filename)
     815              : !! **        DO i=ncoset(k)+1,SIZE(sphi_a,1)
     816              : !! **          DO j=1,SIZE(sphi_a,2)
     817              : !! **            IF( sphi_a(i,j) /= 0.0_dp) THEN
     818              : !! **               write(31415,'(A,I0,A,I0,A,I0,A,I0,A,I0,A)') "buffer1(i+imax*(",&
     819              : !! **                          j,&
     820              : !! **                          "-1)) = buffer1(i+imax*(",&
     821              : !! **                          j,&
     822              : !! **                          "-1)) + buffer2(",&
     823              : !! **                          i-ncoset(k),&
     824              : !! **                          "+(i-1)*kmax) * sphi_c(",&
     825              : !! **                          i-ncoset(k),&
     826              : !! **                          ",",&
     827              : !! **                          j,&
     828              : !! **                          "+s_offset_c1)"
     829              : !! **
     830              : !! **            END IF
     831              : !! **          END DO
     832              : !! **        END DO
     833              : !! **        CLOSE(UNIT=31415)
     834              : !! **        write(filename,'(A,I0,A)') "sphi",k+1,"d"
     835              : !! **        OPEN(UNIT=31415,FILE=filename)
     836              : !! **        DO i=ncoset(k)+1,SIZE(sphi_a,1)
     837              : !! **          DO j=1,SIZE(sphi_a,2)
     838              : !! **            IF( sphi_a(i,j) /= 0.0_dp) THEN
     839              : !! **
     840              : !! **
     841              : !! **               write(31415,'(A,I0,A)') "primitives(s_offset_a1+i3, s_offset_b1+i2, s_offset_c1+i1, s_offset_d1+",&
     842              : !! **                           j,")= &"
     843              : !! **               write(31415,'(A,I0,A)') "primitives(s_offset_a1+i3, s_offset_b1+i2, s_offset_c1+i1, s_offset_d1+",&
     844              : !! **                           j,")+ &"
     845              : !! **               write(31415,'(A,I0,A,I0,A,I0,A)') "buffer1(",&
     846              : !! **                          i-ncoset(k),&
     847              : !! **                          "+(i-1)*kmax) * sphi_d(",&
     848              : !! **                          i-ncoset(k),&
     849              : !! **                          ",",&
     850              : !! **                          j,&
     851              : !! **                          "+s_offset_d1)"
     852              : !! **
     853              : !! **
     854              : !! **            END IF
     855              : !! **          END DO
     856              : !! **        END DO
     857              : !! **        CLOSE(UNIT=31415)
     858              : !! **        stop
     859              : !! *************************************************************************************************************************
     860              : 
     861         1352 :             IF (actual_x_data%periodic_parameter%do_periodic) THEN
     862          426 :                hf_pbc_section => section_vals_get_subs_vals(hfx_section, "PERIODIC", i_rep_section=irep)
     863          426 :                CALL section_vals_val_get(hf_pbc_section, "NUMBER_OF_SHELLS", i_val=pbc_shells)
     864          426 :                actual_x_data%periodic_parameter%number_of_shells_from_input = pbc_shells
     865         3408 :                ALLOCATE (actual_x_data%neighbor_cells(1))
     866          852 :                CALL hfx_create_neighbor_cells(actual_x_data, pbc_shells, cell, i_thread, nkp_grid=nkp_grid)
     867              :             ELSE
     868         7408 :                ALLOCATE (actual_x_data%neighbor_cells(1))
     869              :                ! ** Initialize this guy to enable non periodic stress regtests
     870          926 :                actual_x_data%periodic_parameter%R_max_stress = 1.0_dp
     871              :             END IF
     872              : 
     873         1352 :             nkind = SIZE(qs_kind_set, 1)
     874         1352 :             max_set = actual_x_data%basis_info%max_set
     875              : 
     876              :             !! ** This guy is allocated on the master thread only
     877         1352 :             IF (i_thread == 1) THEN
     878         5408 :                ALLOCATE (actual_x_data%is_assoc_atomic_block(natom, natom))
     879         4056 :                ALLOCATE (actual_x_data%atomic_block_offset(natom, natom))
     880         8112 :                ALLOCATE (actual_x_data%set_offset(max_set, max_set, nkind, nkind))
     881         4056 :                ALLOCATE (actual_x_data%block_offset(para_env%num_pe + 1))
     882              :             END IF
     883              : 
     884         2704 :             ALLOCATE (actual_x_data%distribution_forces(1))
     885         2704 :             ALLOCATE (actual_x_data%distribution_energy(1))
     886              : 
     887         1352 :             actual_x_data%memory_parameter%size_p_screen = 0_int_8
     888         1352 :             IF (i_thread == 1) THEN
     889         5408 :                ALLOCATE (actual_x_data%atomic_pair_list(natom, natom))
     890         4056 :                ALLOCATE (actual_x_data%atomic_pair_list_forces(natom, natom))
     891              :             END IF
     892              : 
     893         1352 :             IF (actual_x_data%screening_parameter%do_initial_p_screening .OR. &
     894              :                 actual_x_data%screening_parameter%do_p_screening_forces) THEN
     895              :                !! ** This guy is allocated on the master thread only
     896         1330 :                IF (i_thread == 1) THEN
     897         5320 :                   ALLOCATE (actual_x_data%pmax_atom(natom, natom))
     898         8052 :                   ALLOCATE (actual_x_data%initial_p(nkind*(nkind + 1)/2))
     899         1330 :                   i = 1
     900         3764 :                   DO ikind = 1, nkind
     901         2434 :                      CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom_a)
     902         2434 :                      nseta = actual_x_data%basis_parameter(ikind)%nset
     903         7826 :                      DO jkind = ikind, nkind
     904         4062 :                         CALL get_atomic_kind(atomic_kind_set(jkind), natom=natom_b)
     905         4062 :                         nsetb = actual_x_data%basis_parameter(jkind)%nset
     906        24372 :                         ALLOCATE (actual_x_data%initial_p(i)%p_kind(nseta, nsetb, natom_a, natom_b))
     907              :                         actual_x_data%memory_parameter%size_p_screen = &
     908         4062 :                            actual_x_data%memory_parameter%size_p_screen + nseta*nsetb*natom_a*natom_b
     909        10558 :                         i = i + 1
     910              :                      END DO
     911              :                   END DO
     912              : 
     913         3990 :                   ALLOCATE (actual_x_data%pmax_atom_forces(natom, natom))
     914         6722 :                   ALLOCATE (actual_x_data%initial_p_forces(nkind*(nkind + 1)/2))
     915         1330 :                   i = 1
     916         3764 :                   DO ikind = 1, nkind
     917         2434 :                      CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom_a)
     918         2434 :                      nseta = actual_x_data%basis_parameter(ikind)%nset
     919         7826 :                      DO jkind = ikind, nkind
     920         4062 :                         CALL get_atomic_kind(atomic_kind_set(jkind), natom=natom_b)
     921         4062 :                         nsetb = actual_x_data%basis_parameter(jkind)%nset
     922        24372 :                         ALLOCATE (actual_x_data%initial_p_forces(i)%p_kind(nseta, nsetb, natom_a, natom_b))
     923              :                         actual_x_data%memory_parameter%size_p_screen = &
     924         4062 :                            actual_x_data%memory_parameter%size_p_screen + nseta*nsetb*natom_a*natom_b
     925        10558 :                         i = i + 1
     926              :                      END DO
     927              :                   END DO
     928              :                END IF
     929         3990 :                ALLOCATE (actual_x_data%map_atom_to_kind_atom(natom))
     930         1330 :                CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
     931              : 
     932         3990 :                ALLOCATE (atom2kind(nkind))
     933         3764 :                atom2kind = 0
     934         5530 :                DO iatom = 1, natom
     935         4200 :                   ikind = kind_of(iatom)
     936         4200 :                   atom2kind(ikind) = atom2kind(ikind) + 1
     937         5530 :                   actual_x_data%map_atom_to_kind_atom(iatom) = atom2kind(ikind)
     938              :                END DO
     939         1330 :                DEALLOCATE (kind_of, atom2kind)
     940              :             END IF
     941              : 
     942              :             ! ** Initialize libint type
     943         1352 :             CALL cp_libint_static_init()
     944         1352 :             CALL cp_libint_init_eri(actual_x_data%lib, actual_x_data%basis_info%max_am)
     945         1352 :             CALL cp_libint_init_eri1(actual_x_data%lib_deriv, actual_x_data%basis_info%max_am)
     946         1352 :             CALL cp_libint_set_contrdepth(actual_x_data%lib, 1)
     947         1352 :             CALL cp_libint_set_contrdepth(actual_x_data%lib_deriv, 1)
     948              : 
     949         1352 :             CALL alloc_containers(actual_x_data%store_ints, 1)
     950         1352 :             CALL alloc_containers(actual_x_data%store_forces, 1)
     951              : 
     952         1352 :             actual_x_data%store_ints%maxval_cache_disk%element_counter = 1
     953         1352 :             ALLOCATE (actual_x_data%store_ints%maxval_container_disk)
     954      1385800 :             ALLOCATE (actual_x_data%store_ints%maxval_container_disk%first)
     955         1352 :             actual_x_data%store_ints%maxval_container_disk%first%prev => NULL()
     956         1352 :             actual_x_data%store_ints%maxval_container_disk%first%next => NULL()
     957         1352 :             actual_x_data%store_ints%maxval_container_disk%current => actual_x_data%store_ints%maxval_container_disk%first
     958      1385800 :             actual_x_data%store_ints%maxval_container_disk%current%data = 0
     959         1352 :             actual_x_data%store_ints%maxval_container_disk%element_counter = 1
     960         1352 :             actual_x_data%store_ints%maxval_container_disk%file_counter = 1
     961         1352 :             actual_x_data%store_ints%maxval_container_disk%desc = 'Max_'
     962         1352 :             actual_x_data%store_ints%maxval_container_disk%unit = -1
     963              :             WRITE (actual_x_data%store_ints%maxval_container_disk%filename, '(A,I0,A,A,A)') &
     964         1352 :                TRIM(actual_x_data%memory_parameter%storage_location), &
     965         2704 :                storage_id, "_", actual_x_data%store_ints%maxval_container_disk%desc, "6"
     966         1352 :             CALL compress(actual_x_data%store_ints%maxval_container_disk%filename, .TRUE.)
     967        87880 :             ALLOCATE (actual_x_data%store_ints%integral_containers_disk(64))
     968        87880 :             DO i = 1, 64
     969        86528 :                actual_x_data%store_ints%integral_caches_disk(i)%element_counter = 1
     970     88691200 :                actual_x_data%store_ints%integral_caches_disk(i)%data = 0
     971     88691200 :                ALLOCATE (actual_x_data%store_ints%integral_containers_disk(i)%first)
     972        86528 :                actual_x_data%store_ints%integral_containers_disk(i)%first%prev => NULL()
     973        86528 :                actual_x_data%store_ints%integral_containers_disk(i)%first%next => NULL()
     974              :                actual_x_data%store_ints%integral_containers_disk(i)%current => &
     975        86528 :                   actual_x_data%store_ints%integral_containers_disk(i)%first
     976     88691200 :                actual_x_data%store_ints%integral_containers_disk(i)%current%data = 0
     977        86528 :                actual_x_data%store_ints%integral_containers_disk(i)%element_counter = 1
     978        86528 :                actual_x_data%store_ints%integral_containers_disk(i)%file_counter = 1
     979        86528 :                actual_x_data%store_ints%integral_containers_disk(i)%desc = 'Int_'
     980        86528 :                actual_x_data%store_ints%integral_containers_disk(i)%unit = -1
     981              :                WRITE (actual_x_data%store_ints%integral_containers_disk(i)%filename, '(A,I0,A,A,I0)') &
     982        86528 :                   TRIM(actual_x_data%memory_parameter%storage_location), &
     983       173056 :                   storage_id, "_", actual_x_data%store_ints%integral_containers_disk(i)%desc, i
     984        87880 :                CALL compress(actual_x_data%store_ints%integral_containers_disk(i)%filename, .TRUE.)
     985              :             END DO
     986              : 
     987         1352 :             actual_x_data%b_first_load_balance_energy = .TRUE.
     988         1352 :             actual_x_data%b_first_load_balance_forces = .TRUE.
     989              : 
     990         1352 :             hf_sub_section => section_vals_get_subs_vals(hfx_section, "RI", i_rep_section=irep)
     991        12158 :             IF (actual_x_data%do_hfx_ri) THEN
     992          108 :                CPASSERT(PRESENT(nelectron_total))
     993          756 :                ALLOCATE (actual_x_data%ri_data)
     994              :                CALL hfx_ri_init_read_input_from_hfx(actual_x_data%ri_data, actual_x_data, hfx_section, &
     995              :                                                     hf_sub_section, qs_kind_set, &
     996              :                                                     particle_set, atomic_kind_set, dft_control, para_env, irep, &
     997          108 :                                                     nelectron_total, orb_basis_type, ri_basis_type)
     998              :             END IF
     999              :          END DO
    1000              :       END DO
    1001              : 
    1002         2694 :       DO irep = 1, n_rep_hf
    1003         1352 :          actual_x_data => x_data(irep, 1)
    1004         2694 :          CALL hfx_print_info(actual_x_data, hfx_section, irep)
    1005              :       END DO
    1006              : 
    1007         1342 :       CALL timestop(handle)
    1008              : 
    1009         5368 :    END SUBROUTINE hfx_create
    1010              : 
    1011              : ! **************************************************************************************************
    1012              : !> \brief Read RI input and initialize RI data for use within Hartree-Fock
    1013              : !> \param ri_data ...
    1014              : !> \param x_data ...
    1015              : !> \param hfx_section ...
    1016              : !> \param ri_section ...
    1017              : !> \param qs_kind_set ...
    1018              : !> \param particle_set ...
    1019              : !> \param atomic_kind_set ...
    1020              : !> \param dft_control ...
    1021              : !> \param para_env ...
    1022              : !> \param irep ...
    1023              : !> \param nelectron_total ...
    1024              : !> \param orb_basis_type ...
    1025              : !> \param ri_basis_type ...
    1026              : ! **************************************************************************************************
    1027          108 :    SUBROUTINE hfx_ri_init_read_input_from_hfx(ri_data, x_data, hfx_section, ri_section, qs_kind_set, &
    1028              :                                               particle_set, atomic_kind_set, dft_control, para_env, irep, &
    1029              :                                               nelectron_total, orb_basis_type, ri_basis_type)
    1030              :       TYPE(hfx_ri_type), INTENT(INOUT)                   :: ri_data
    1031              :       TYPE(hfx_type), INTENT(INOUT)                      :: x_data
    1032              :       TYPE(section_vals_type), POINTER                   :: hfx_section, ri_section
    1033              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1034              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1035              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1036              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1037              :       TYPE(mp_para_env_type)                             :: para_env
    1038              :       INTEGER, INTENT(IN)                                :: irep, nelectron_total
    1039              :       CHARACTER(LEN=*)                                   :: orb_basis_type, ri_basis_type
    1040              : 
    1041              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_ri_init_read_input_from_hfx'
    1042              : 
    1043              :       CHARACTER(LEN=512)                                 :: error_msg
    1044              :       CHARACTER(LEN=default_path_length)                 :: char_val, t_c_filename
    1045              :       INTEGER                                            :: handle, unit_nr, unit_nr_dbcsr
    1046              :       TYPE(cp_logger_type), POINTER                      :: logger
    1047              :       TYPE(section_vals_type), POINTER                   :: hf_sub_section
    1048              : 
    1049          108 :       CALL timeset(routineN, handle)
    1050              : 
    1051          108 :       NULLIFY (hf_sub_section)
    1052              : 
    1053              :       ASSOCIATE (hfx_pot => ri_data%hfx_pot)
    1054          108 :          hfx_pot%potential_type = x_data%potential_parameter%potential_type
    1055          108 :          hfx_pot%omega = x_data%potential_parameter%omega
    1056          108 :          hfx_pot%cutoff_radius = x_data%potential_parameter%cutoff_radius
    1057          108 :          hfx_pot%scale_coulomb = x_data%potential_parameter%scale_coulomb
    1058          108 :          hfx_pot%scale_longrange = x_data%potential_parameter%scale_longrange
    1059              :       END ASSOCIATE
    1060          108 :       ri_data%ri_section => ri_section
    1061          108 :       ri_data%hfx_section => hfx_section
    1062          108 :       ri_data%eps_schwarz = x_data%screening_parameter%eps_schwarz
    1063          108 :       ri_data%eps_schwarz_forces = x_data%screening_parameter%eps_schwarz_forces
    1064              : 
    1065          108 :       logger => cp_get_default_logger()
    1066              :       unit_nr_dbcsr = cp_print_key_unit_nr(logger, ri_data%ri_section, "PRINT%RI_INFO", &
    1067          108 :                                            extension=".dbcsrLog")
    1068              : 
    1069              :       unit_nr = cp_print_key_unit_nr(logger, ri_data%hfx_section, "HF_INFO", &
    1070          108 :                                      extension=".scfLog")
    1071              : 
    1072          108 :       hf_sub_section => section_vals_get_subs_vals(hfx_section, "INTERACTION_POTENTIAL", i_rep_section=irep)
    1073          108 :       CALL section_vals_val_get(hf_sub_section, "T_C_G_DATA", c_val=char_val)
    1074          108 :       CALL compress(char_val, .TRUE.)
    1075              : 
    1076          108 :       IF (.NOT. file_exists(char_val)) THEN
    1077              :          WRITE (error_msg, '(A,A,A)') "File not found. Please check T_C_G_DATA "// &
    1078            0 :             "in the INTERACTION_POTENTIAL section"
    1079            0 :          CPABORT(error_msg)
    1080              :       ELSE
    1081          108 :          t_c_filename = char_val
    1082              :       END IF
    1083              : 
    1084              :       CALL hfx_ri_init_read_input(ri_data, ri_section, qs_kind_set, particle_set, atomic_kind_set, &
    1085              :                                   orb_basis_type, ri_basis_type, para_env, unit_nr, unit_nr_dbcsr, &
    1086          108 :                                   nelectron_total, t_c_filename=t_c_filename)
    1087              : 
    1088          108 :       IF (dft_control%smear .AND. ri_data%flavor == ri_mo) THEN
    1089            0 :          CPABORT("RI_FLAVOR MO is not consistent with smearing. Please use RI_FLAVOR RHO.")
    1090              :       END IF
    1091              : 
    1092          108 :       CALL timestop(handle)
    1093              : 
    1094          108 :    END SUBROUTINE hfx_ri_init_read_input_from_hfx
    1095              : 
    1096              : ! **************************************************************************************************
    1097              : !> \brief General routine for reading input of RI section and initializing RI data
    1098              : !> \param ri_data ...
    1099              : !> \param ri_section ...
    1100              : !> \param qs_kind_set ...
    1101              : !> \param particle_set ...
    1102              : !> \param atomic_kind_set ...
    1103              : !> \param orb_basis_type ...
    1104              : !> \param ri_basis_type ...
    1105              : !> \param para_env ...
    1106              : !> \param unit_nr unit number of general output
    1107              : !> \param unit_nr_dbcsr unit number for logging DBCSR tensor operations
    1108              : !> \param nelectron_total ...
    1109              : !> \param t_c_filename ...
    1110              : ! **************************************************************************************************
    1111          108 :    SUBROUTINE hfx_ri_init_read_input(ri_data, ri_section, qs_kind_set, &
    1112              :                                      particle_set, atomic_kind_set, orb_basis_type, ri_basis_type, para_env, &
    1113              :                                      unit_nr, unit_nr_dbcsr, nelectron_total, t_c_filename)
    1114              :       TYPE(hfx_ri_type), INTENT(INOUT)                   :: ri_data
    1115              :       TYPE(section_vals_type), POINTER                   :: ri_section
    1116              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1117              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1118              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1119              :       CHARACTER(LEN=*), INTENT(IN)                       :: orb_basis_type, ri_basis_type
    1120              :       TYPE(mp_para_env_type)                             :: para_env
    1121              :       INTEGER, INTENT(IN)                                :: unit_nr, unit_nr_dbcsr, nelectron_total
    1122              :       CHARACTER(len=*), INTENT(IN), OPTIONAL             :: t_c_filename
    1123              : 
    1124              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_ri_init_read_input'
    1125              : 
    1126              :       INTEGER                                            :: handle
    1127              :       LOGICAL                                            :: explicit
    1128              :       REAL(dp)                                           :: eps_storage_scaling
    1129              : 
    1130          108 :       CALL timeset(routineN, handle)
    1131              : 
    1132          108 :       CALL section_vals_val_get(ri_section, "EPS_FILTER", r_val=ri_data%filter_eps)
    1133          108 :       CALL section_vals_val_get(ri_section, "EPS_FILTER_2C", r_val=ri_data%filter_eps_2c)
    1134          108 :       CALL section_vals_val_get(ri_section, "EPS_STORAGE_SCALING", r_val=eps_storage_scaling)
    1135          108 :       ri_data%filter_eps_storage = ri_data%filter_eps*eps_storage_scaling
    1136          108 :       CALL section_vals_val_get(ri_section, "EPS_FILTER_MO", r_val=ri_data%filter_eps_mo)
    1137              : 
    1138              :       ASSOCIATE (ri_metric => ri_data%ri_metric, hfx_pot => ri_data%hfx_pot)
    1139          108 :          CALL section_vals_val_get(ri_section, "RI_METRIC", i_val=ri_metric%potential_type, explicit=explicit)
    1140          108 :          IF (.NOT. explicit .OR. ri_metric%potential_type == 0) THEN
    1141           44 :             ri_metric%potential_type = hfx_pot%potential_type
    1142              :          END IF
    1143              : 
    1144          108 :          CALL section_vals_val_get(ri_section, "OMEGA", r_val=ri_metric%omega, explicit=explicit)
    1145          108 :          IF (.NOT. explicit) THEN
    1146          108 :             ri_metric%omega = hfx_pot%omega
    1147              :          END IF
    1148              : 
    1149          108 :          CALL section_vals_val_get(ri_section, "CUTOFF_RADIUS", r_val=ri_metric%cutoff_radius, explicit=explicit)
    1150          108 :          IF (.NOT. explicit) THEN
    1151          100 :             ri_metric%cutoff_radius = hfx_pot%cutoff_radius
    1152              :          END IF
    1153              : 
    1154          108 :          CALL section_vals_val_get(ri_section, "SCALE_COULOMB", r_val=ri_metric%scale_coulomb, explicit=explicit)
    1155          108 :          IF (.NOT. explicit) THEN
    1156          108 :             ri_metric%scale_coulomb = hfx_pot%scale_coulomb
    1157              :          END IF
    1158              : 
    1159          108 :          CALL section_vals_val_get(ri_section, "SCALE_LONGRANGE", r_val=ri_metric%scale_longrange, explicit=explicit)
    1160          108 :          IF (.NOT. explicit) THEN
    1161          108 :             ri_metric%scale_longrange = hfx_pot%scale_longrange
    1162              :          END IF
    1163              : 
    1164          108 :          IF (ri_metric%potential_type == do_potential_short) &
    1165            2 :             CALL erfc_cutoff(ri_data%eps_schwarz, ri_metric%omega, ri_metric%cutoff_radius)
    1166          108 :          IF (ri_metric%potential_type == do_potential_id) ri_metric%cutoff_radius = 0.0_dp
    1167              :       END ASSOCIATE
    1168              : 
    1169          108 :       CALL section_vals_val_get(ri_section, "2C_MATRIX_FUNCTIONS", i_val=ri_data%t2c_method)
    1170          108 :       CALL section_vals_val_get(ri_section, "EPS_EIGVAL", r_val=ri_data%eps_eigval)
    1171          108 :       CALL section_vals_val_get(ri_section, "CHECK_2C_MATRIX", l_val=ri_data%check_2c_inv)
    1172          108 :       CALL section_vals_val_get(ri_section, "CALC_COND_NUM", l_val=ri_data%calc_condnum)
    1173          108 :       CALL section_vals_val_get(ri_section, "SQRT_ORDER", i_val=ri_data%t2c_sqrt_order)
    1174          108 :       CALL section_vals_val_get(ri_section, "EPS_LANCZOS", r_val=ri_data%eps_lanczos)
    1175          108 :       CALL section_vals_val_get(ri_section, "MAX_ITER_LANCZOS", i_val=ri_data%max_iter_lanczos)
    1176          108 :       CALL section_vals_val_get(ri_section, "RI_FLAVOR", i_val=ri_data%flavor)
    1177          108 :       CALL section_vals_val_get(ri_section, "EPS_PGF_ORB", r_val=ri_data%eps_pgf_orb)
    1178          108 :       CALL section_vals_val_get(ri_section, "MIN_BLOCK_SIZE", i_val=ri_data%min_bsize)
    1179          108 :       CALL section_vals_val_get(ri_section, "MAX_BLOCK_SIZE_MO", i_val=ri_data%max_bsize_MO)
    1180          108 :       CALL section_vals_val_get(ri_section, "MEMORY_CUT", i_val=ri_data%n_mem_input)
    1181          108 :       CALL section_vals_val_get(ri_section, "FLAVOR_SWITCH_MEMORY_CUT", i_val=ri_data%n_mem_flavor_switch)
    1182              : 
    1183          108 :       ri_data%orb_basis_type = orb_basis_type
    1184          108 :       ri_data%ri_basis_type = ri_basis_type
    1185          108 :       ri_data%nelectron_total = nelectron_total
    1186          108 :       ri_data%input_flavor = ri_data%flavor
    1187              : 
    1188          108 :       IF (PRESENT(t_c_filename)) THEN
    1189          108 :          ri_data%ri_metric%filename = t_c_filename
    1190          108 :          ri_data%hfx_pot%filename = t_c_filename
    1191              :       END IF
    1192              : 
    1193          108 :       ri_data%unit_nr_dbcsr = unit_nr_dbcsr
    1194          108 :       ri_data%unit_nr = unit_nr
    1195          108 :       ri_data%dbcsr_nflop = 0
    1196          108 :       ri_data%dbcsr_time = 0.0_dp
    1197              : 
    1198          108 :       CALL hfx_ri_init(ri_data, qs_kind_set, particle_set, atomic_kind_set, para_env)
    1199              : 
    1200          108 :       CALL timestop(handle)
    1201              : 
    1202          756 :    END SUBROUTINE hfx_ri_init_read_input
    1203              : 
    1204              : ! **************************************************************************************************
    1205              : !> \brief ...
    1206              : !> \param ri_data ...
    1207              : !> \param qs_kind_set ...
    1208              : !> \param particle_set ...
    1209              : !> \param atomic_kind_set ...
    1210              : !> \param para_env ...
    1211              : ! **************************************************************************************************
    1212          130 :    SUBROUTINE hfx_ri_init(ri_data, qs_kind_set, particle_set, atomic_kind_set, para_env)
    1213              :       TYPE(hfx_ri_type), INTENT(INOUT)                   :: ri_data
    1214              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1215              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1216              :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1217              :       TYPE(mp_para_env_type)                             :: para_env
    1218              : 
    1219              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'hfx_ri_init'
    1220              : 
    1221              :       INTEGER                                            :: handle, i_mem, j_mem, MO_dim, natom, &
    1222              :                                                             nkind, nproc
    1223          130 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: bsizes_AO_store, bsizes_RI_store, dist1, &
    1224          130 :                                                             dist2, dist3, dist_AO_1, dist_AO_2, &
    1225              :                                                             dist_RI
    1226              :       INTEGER, DIMENSION(2)                              :: pdims_2d
    1227              :       INTEGER, DIMENSION(3)                              :: pdims
    1228              :       LOGICAL                                            :: same_op
    1229              :       TYPE(distribution_3d_type)                         :: dist_3d
    1230              :       TYPE(gto_basis_set_p_type), ALLOCATABLE, &
    1231          130 :          DIMENSION(:)                                    :: basis_set_AO, basis_set_RI
    1232          130 :       TYPE(mp_cart_type)                                 :: mp_comm_3d
    1233              : 
    1234          130 :       CALL cite_reference(Bussy2023)
    1235              : 
    1236          130 :       CALL timeset(routineN, handle)
    1237              : 
    1238              :       ! initialize libint
    1239          130 :       CALL cp_libint_static_init()
    1240              : 
    1241          130 :       natom = SIZE(particle_set)
    1242          130 :       nkind = SIZE(qs_kind_set, 1)
    1243          130 :       nproc = para_env%num_pe
    1244              : 
    1245              :       ASSOCIATE (ri_metric => ri_data%ri_metric, hfx_pot => ri_data%hfx_pot)
    1246          130 :          IF (ri_metric%potential_type == do_potential_short) THEN
    1247            2 :             CALL erfc_cutoff(ri_data%eps_schwarz, ri_metric%omega, ri_metric%cutoff_radius)
    1248              :          END IF
    1249              : 
    1250          130 :          IF (hfx_pot%potential_type == do_potential_short) THEN
    1251              :             ! need a more accurate threshold for determining 2-center integral operator range
    1252              :             ! because stability of matrix inversion/sqrt is sensitive to this
    1253            4 :             CALL erfc_cutoff(ri_data%filter_eps_2c, hfx_pot%omega, hfx_pot%cutoff_radius)
    1254              :          END IF
    1255              :          ! determine whether RI metric is same operator as used in HFX
    1256          130 :          same_op = compare_potential_types(ri_metric, hfx_pot)
    1257              :       END ASSOCIATE
    1258              : 
    1259          130 :       ri_data%same_op = same_op
    1260              : 
    1261          130 :       pdims = 0
    1262          130 :       CALL mp_comm_3d%create(para_env, 3, pdims)
    1263              : 
    1264          390 :       ALLOCATE (ri_data%bsizes_RI(natom))
    1265          260 :       ALLOCATE (ri_data%bsizes_AO(natom))
    1266          972 :       ALLOCATE (basis_set_RI(nkind), basis_set_AO(nkind))
    1267          130 :       CALL basis_set_list_setup(basis_set_RI, ri_data%ri_basis_type, qs_kind_set)
    1268          130 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=ri_data%bsizes_RI, basis=basis_set_RI)
    1269          130 :       CALL basis_set_list_setup(basis_set_AO, ri_data%orb_basis_type, qs_kind_set)
    1270          130 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=ri_data%bsizes_AO, basis=basis_set_AO)
    1271              : 
    1272          260 :       ALLOCATE (dist_RI(natom))
    1273          260 :       ALLOCATE (dist_AO_1(natom))
    1274          260 :       ALLOCATE (dist_AO_2(natom))
    1275          130 :       CALL dbt_default_distvec(natom, pdims(1), ri_data%bsizes_RI, dist_RI)
    1276          130 :       CALL dbt_default_distvec(natom, pdims(2), ri_data%bsizes_AO, dist_AO_1)
    1277          130 :       CALL dbt_default_distvec(natom, pdims(3), ri_data%bsizes_AO, dist_AO_2)
    1278              :       CALL distribution_3d_create(dist_3d, dist_RI, dist_ao_1, dist_ao_2, nkind, particle_set, &
    1279          130 :                                   mp_comm_3d, own_comm=.TRUE.)
    1280              : 
    1281          390 :       ALLOCATE (ri_data%pgrid)
    1282          130 :       CALL dbt_pgrid_create(para_env, pdims, ri_data%pgrid)
    1283              : 
    1284          390 :       ALLOCATE (ri_data%pgrid_2d)
    1285          130 :       pdims_2d = 0
    1286          130 :       CALL dbt_pgrid_create(para_env, pdims_2d, ri_data%pgrid_2d)
    1287              : 
    1288          130 :       ri_data%dist_3d = dist_3d
    1289              : 
    1290              :       CALL dbt_distribution_new(ri_data%dist, ri_data%pgrid, &
    1291          130 :                                 dist_RI, dist_AO_1, dist_AO_2)
    1292              : 
    1293          130 :       DEALLOCATE (dist_AO_1, dist_AO_2, dist_RI)
    1294              : 
    1295          130 :       ri_data%num_pe = para_env%num_pe
    1296              : 
    1297              :       ! initialize tensors expressed in basis representation
    1298          130 :       CALL pgf_block_sizes(atomic_kind_set, basis_set_AO, ri_data%min_bsize, ri_data%bsizes_AO_split)
    1299          130 :       CALL pgf_block_sizes(atomic_kind_set, basis_set_RI, ri_data%min_bsize, ri_data%bsizes_RI_split)
    1300              : 
    1301          130 :       CALL pgf_block_sizes(atomic_kind_set, basis_set_AO, 1, bsizes_AO_store)
    1302          130 :       CALL pgf_block_sizes(atomic_kind_set, basis_set_RI, 1, bsizes_RI_store)
    1303              : 
    1304          642 :       CALL split_block_sizes([SUM(ri_data%bsizes_AO)], ri_data%bsizes_AO_fit, default_block_size)
    1305          642 :       CALL split_block_sizes([SUM(ri_data%bsizes_RI)], ri_data%bsizes_RI_fit, default_block_size)
    1306              : 
    1307          130 :       IF (ri_data%flavor == ri_pmat) THEN
    1308              : 
    1309              :          !2 batching loops in RHO flavor SCF calculations => need to take the square root of MEMORY_CUT
    1310          112 :          ri_data%n_mem = ri_data%n_mem_input
    1311          112 :          ri_data%n_mem_RI = ri_data%n_mem_input
    1312              : 
    1313              :          CALL create_tensor_batches(ri_data%bsizes_AO_split, ri_data%n_mem, ri_data%starts_array_mem, &
    1314              :                                     ri_data%ends_array_mem, ri_data%starts_array_mem_block, &
    1315          112 :                                     ri_data%ends_array_mem_block)
    1316              : 
    1317              :          CALL create_tensor_batches(ri_data%bsizes_RI_split, ri_data%n_mem_RI, &
    1318              :                                     ri_data%starts_array_RI_mem, ri_data%ends_array_RI_mem, &
    1319          112 :                                     ri_data%starts_array_RI_mem_block, ri_data%ends_array_RI_mem_block)
    1320              : 
    1321          336 :          ALLOCATE (ri_data%pgrid_1)
    1322          336 :          ALLOCATE (ri_data%pgrid_2)
    1323          112 :          pdims = 0
    1324              : 
    1325              :          CALL dbt_mp_dims_create(nproc, pdims, [SIZE(ri_data%bsizes_AO_split), SIZE(ri_data%bsizes_RI_split), &
    1326          448 :                                                 SIZE(ri_data%bsizes_AO_split)])
    1327              : 
    1328          112 :          CALL dbt_pgrid_create(para_env, pdims, ri_data%pgrid_1)
    1329              : 
    1330          784 :          pdims = pdims([2, 1, 3])
    1331          112 :          CALL dbt_pgrid_create(para_env, pdims, ri_data%pgrid_2)
    1332              : 
    1333         1008 :          ALLOCATE (ri_data%t_3c_int_ctr_1(1, 1))
    1334              :          CALL create_3c_tensor(ri_data%t_3c_int_ctr_1(1, 1), dist1, dist2, dist3, &
    1335              :                                ri_data%pgrid_1, ri_data%bsizes_AO_split, ri_data%bsizes_RI_split, &
    1336          112 :                                ri_data%bsizes_AO_split, [1, 2], [3], name="(AO RI | AO)")
    1337          112 :          DEALLOCATE (dist1, dist2, dist3)
    1338              : 
    1339         1456 :          ALLOCATE (ri_data%blk_indices(ri_data%n_mem, ri_data%n_mem_RI))
    1340       238000 :          ALLOCATE (ri_data%store_3c(ri_data%n_mem, ri_data%n_mem_RI))
    1341          392 :          DO i_mem = 1, ri_data%n_mem
    1342         1120 :          DO j_mem = 1, ri_data%n_mem_RI
    1343         1008 :             CALL alloc_containers(ri_data%store_3c(i_mem, j_mem), 1)
    1344              :          END DO
    1345              :          END DO
    1346              : 
    1347         1008 :          ALLOCATE (ri_data%t_3c_int_ctr_2(1, 1))
    1348              :          CALL create_3c_tensor(ri_data%t_3c_int_ctr_2(1, 1), dist1, dist2, dist3, &
    1349              :                                ri_data%pgrid_1, ri_data%bsizes_AO_split, ri_data%bsizes_RI_split, &
    1350          112 :                                ri_data%bsizes_AO_split, [1, 2], [3], name="(AO RI | AO)")
    1351          112 :          DEALLOCATE (dist1, dist2, dist3)
    1352              : 
    1353         1008 :          ALLOCATE (ri_data%t_3c_int_ctr_3(1, 1))
    1354              :          CALL create_3c_tensor(ri_data%t_3c_int_ctr_3(1, 1), dist1, dist2, dist3, &
    1355              :                                ri_data%pgrid_2, ri_data%bsizes_RI_split, ri_data%bsizes_AO_split, &
    1356          112 :                                ri_data%bsizes_AO_split, [1], [2, 3], name="(RI | AO AO)")
    1357          112 :          DEALLOCATE (dist1, dist2, dist3)
    1358              : 
    1359         1008 :          ALLOCATE (ri_data%t_2c_int(1, 1))
    1360              :          CALL create_2c_tensor(ri_data%t_2c_int(1, 1), dist1, dist2, ri_data%pgrid_2d, &
    1361              :                                ri_data%bsizes_RI_split, ri_data%bsizes_RI_split, &
    1362          112 :                                name="(RI | RI)")
    1363          112 :          DEALLOCATE (dist1, dist2)
    1364              : 
    1365              :          !We store previous Pmat and KS mat, so that we can work with Delta P and gain sprasity as we go
    1366         1120 :          ALLOCATE (ri_data%rho_ao_t(2, 1))
    1367              :          CALL create_2c_tensor(ri_data%rho_ao_t(1, 1), dist1, dist2, ri_data%pgrid_2d, &
    1368              :                                ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
    1369          112 :                                name="(AO | AO)")
    1370          112 :          DEALLOCATE (dist1, dist2)
    1371          112 :          CALL dbt_create(ri_data%rho_ao_t(1, 1), ri_data%rho_ao_t(2, 1))
    1372              : 
    1373         1120 :          ALLOCATE (ri_data%ks_t(2, 1))
    1374              :          CALL create_2c_tensor(ri_data%ks_t(1, 1), dist1, dist2, ri_data%pgrid_2d, &
    1375              :                                ri_data%bsizes_AO_split, ri_data%bsizes_AO_split, &
    1376          112 :                                name="(AO | AO)")
    1377          112 :          DEALLOCATE (dist1, dist2)
    1378          672 :          CALL dbt_create(ri_data%ks_t(1, 1), ri_data%ks_t(2, 1))
    1379              : 
    1380           18 :       ELSEIF (ri_data%flavor == ri_mo) THEN
    1381          180 :          ALLOCATE (ri_data%t_2c_int(2, 1))
    1382              : 
    1383              :          CALL create_2c_tensor(ri_data%t_2c_int(1, 1), dist1, dist2, ri_data%pgrid_2d, &
    1384              :                                ri_data%bsizes_RI_fit, ri_data%bsizes_RI_fit, &
    1385           18 :                                name="(RI | RI)")
    1386           18 :          CALL dbt_create(ri_data%t_2c_int(1, 1), ri_data%t_2c_int(2, 1))
    1387              : 
    1388           18 :          DEALLOCATE (dist1, dist2)
    1389              : 
    1390          162 :          ALLOCATE (ri_data%t_3c_int_ctr_1(1, 1))
    1391              : 
    1392           54 :          ALLOCATE (ri_data%pgrid_1)
    1393           54 :          ALLOCATE (ri_data%pgrid_2)
    1394              :          pdims = 0
    1395              : 
    1396           18 :          ri_data%n_mem = ri_data%n_mem_input**2
    1397           18 :          IF (ri_data%n_mem > ri_data%nelectron_total/2) ri_data%n_mem = MAX(ri_data%nelectron_total/2, 1)
    1398              :          ! Size of dimension corresponding to MOs is nelectron/2 and divided by the memory factor
    1399              :          ! we are using ceiling of that division to make sure that no MO dimension (after memory cut)
    1400              :          ! is larger than this (it is however not a problem for load balancing if actual MO dimension
    1401              :          ! is slightly smaller)
    1402           18 :          MO_dim = MAX((ri_data%nelectron_total/2 - 1)/ri_data%n_mem + 1, 1)
    1403           18 :          MO_dim = (MO_dim - 1)/ri_data%max_bsize_MO + 1
    1404              : 
    1405           18 :          pdims = 0
    1406           72 :          CALL dbt_mp_dims_create(nproc, pdims, [SIZE(ri_data%bsizes_AO_split), SIZE(ri_data%bsizes_RI_split), MO_dim])
    1407              : 
    1408           18 :          CALL dbt_pgrid_create(para_env, pdims, ri_data%pgrid_1)
    1409              : 
    1410          126 :          pdims = pdims([3, 2, 1])
    1411           18 :          CALL dbt_pgrid_create(para_env, pdims, ri_data%pgrid_2)
    1412              : 
    1413              :          CALL create_3c_tensor(ri_data%t_3c_int_ctr_1(1, 1), dist1, dist2, dist3, &
    1414              :                                ri_data%pgrid_1, ri_data%bsizes_AO_split, ri_data%bsizes_RI_split, ri_data%bsizes_AO_split, &
    1415           18 :                                [1, 2], [3], name="(AO RI | AO)")
    1416           18 :          DEALLOCATE (dist1, dist2, dist3)
    1417              : 
    1418          162 :          ALLOCATE (ri_data%t_3c_int_ctr_2(1, 1))
    1419              :          CALL create_3c_tensor(ri_data%t_3c_int_ctr_2(1, 1), dist1, dist2, dist3, &
    1420              :                                ri_data%pgrid_2, ri_data%bsizes_AO_split, ri_data%bsizes_RI_split, ri_data%bsizes_AO_split, &
    1421           18 :                                [1], [2, 3], name="(AO | RI AO)")
    1422           36 :          DEALLOCATE (dist1, dist2, dist3)
    1423              : 
    1424              :       END IF
    1425              : 
    1426              :       !For forces
    1427         1170 :       ALLOCATE (ri_data%t_2c_inv(1, 1))
    1428              :       CALL create_2c_tensor(ri_data%t_2c_inv(1, 1), dist1, dist2, ri_data%pgrid_2d, &
    1429              :                             ri_data%bsizes_RI_split, ri_data%bsizes_RI_split, &
    1430          130 :                             name="(RI | RI)")
    1431          130 :       DEALLOCATE (dist1, dist2)
    1432              : 
    1433         1170 :       ALLOCATE (ri_data%t_2c_pot(1, 1))
    1434              :       CALL create_2c_tensor(ri_data%t_2c_pot(1, 1), dist1, dist2, ri_data%pgrid_2d, &
    1435              :                             ri_data%bsizes_RI_split, ri_data%bsizes_RI_split, &
    1436          130 :                             name="(RI | RI)")
    1437          130 :       DEALLOCATE (dist1, dist2)
    1438              : 
    1439          130 :       CALL timestop(handle)
    1440              : 
    1441          780 :    END SUBROUTINE hfx_ri_init
    1442              : 
    1443              : ! **************************************************************************************************
    1444              : !> \brief ...
    1445              : !> \param ri_data ...
    1446              : ! **************************************************************************************************
    1447          108 :    SUBROUTINE hfx_ri_write_stats(ri_data)
    1448              :       TYPE(hfx_ri_type), INTENT(IN)                      :: ri_data
    1449              : 
    1450              :       REAL(dp)                                           :: my_flop_rate
    1451              : 
    1452              :       ASSOCIATE (unit_nr => ri_data%unit_nr, dbcsr_nflop => ri_data%dbcsr_nflop, &
    1453              :                  dbcsr_time => ri_data%dbcsr_time, num_pe => ri_data%num_pe)
    1454          108 :          my_flop_rate = REAL(dbcsr_nflop, dp)/(1.0E09_dp*ri_data%dbcsr_time)
    1455          108 :          IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(/T2,A,T73,ES8.2)") &
    1456           48 :             "RI-HFX PERFORMANCE| DBT total number of flops:", REAL(dbcsr_nflop*num_pe, dp)
    1457          108 :          IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T2,A,T66,F15.2)") &
    1458           48 :             "RI-HFX PERFORMANCE| DBT total execution time:", dbcsr_time
    1459          108 :          IF (unit_nr > 0) WRITE (UNIT=unit_nr, FMT="(T2,A,T66,F15.2)") &
    1460          156 :             "RI-HFX PERFORMANCE| DBT flop rate (Gflops / MPI rank):", my_flop_rate
    1461              :       END ASSOCIATE
    1462          108 :    END SUBROUTINE hfx_ri_write_stats
    1463              : 
    1464              : ! **************************************************************************************************
    1465              : !> \brief ...
    1466              : !> \param ri_data ...
    1467              : !> \param write_stats ...
    1468              : ! **************************************************************************************************
    1469          130 :    SUBROUTINE hfx_ri_release(ri_data, write_stats)
    1470              :       TYPE(hfx_ri_type), INTENT(INOUT)                   :: ri_data
    1471              :       LOGICAL, OPTIONAL                                  :: write_stats
    1472              : 
    1473              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'hfx_ri_release'
    1474              : 
    1475              :       INTEGER                                            :: handle, i, i_mem, ispin, j, j_mem, unused
    1476              :       LOGICAL                                            :: my_write_stats
    1477              : 
    1478          130 :       CALL timeset(routineN, handle)
    1479              : 
    1480              :       ! cleanup libint
    1481          130 :       CALL cp_libint_static_cleanup()
    1482              : 
    1483          130 :       my_write_stats = .TRUE.
    1484          130 :       IF (PRESENT(write_stats)) my_write_stats = write_stats
    1485          130 :       IF (my_write_stats) CALL hfx_ri_write_stats(ri_data)
    1486              : 
    1487          130 :       IF (ASSOCIATED(ri_data%pgrid)) THEN
    1488          130 :          CALL dbt_pgrid_destroy(ri_data%pgrid)
    1489          130 :          DEALLOCATE (ri_data%pgrid)
    1490              :       END IF
    1491          130 :       IF (ASSOCIATED(ri_data%pgrid_1)) THEN
    1492          130 :          CALL dbt_pgrid_destroy(ri_data%pgrid_1)
    1493          130 :          DEALLOCATE (ri_data%pgrid_1)
    1494              :       END IF
    1495          130 :       IF (ASSOCIATED(ri_data%pgrid_2)) THEN
    1496          130 :          CALL dbt_pgrid_destroy(ri_data%pgrid_2)
    1497          130 :          DEALLOCATE (ri_data%pgrid_2)
    1498              :       END IF
    1499          130 :       IF (ASSOCIATED(ri_data%pgrid_2d)) THEN
    1500          130 :          CALL dbt_pgrid_destroy(ri_data%pgrid_2d)
    1501          130 :          DEALLOCATE (ri_data%pgrid_2d)
    1502              :       END IF
    1503              : 
    1504          130 :       CALL distribution_3d_destroy(ri_data%dist_3d)
    1505          130 :       CALL dbt_distribution_destroy(ri_data%dist)
    1506              : 
    1507          130 :       DEALLOCATE (ri_data%bsizes_RI)
    1508          130 :       DEALLOCATE (ri_data%bsizes_AO)
    1509          130 :       DEALLOCATE (ri_data%bsizes_AO_split)
    1510          130 :       DEALLOCATE (ri_data%bsizes_RI_split)
    1511          130 :       DEALLOCATE (ri_data%bsizes_AO_fit)
    1512          130 :       DEALLOCATE (ri_data%bsizes_RI_fit)
    1513              : 
    1514          130 :       IF (ri_data%flavor == ri_pmat) THEN
    1515          392 :          DO i_mem = 1, ri_data%n_mem
    1516         1120 :          DO j_mem = 1, ri_data%n_mem_RI
    1517         1008 :             CALL dealloc_containers(ri_data%store_3c(i_mem, j_mem), unused)
    1518              :          END DO
    1519              :          END DO
    1520              : 
    1521         1478 :          DO j = 1, SIZE(ri_data%t_3c_int_ctr_1, 2)
    1522         2844 :             DO i = 1, SIZE(ri_data%t_3c_int_ctr_1, 1)
    1523         2732 :                CALL dbt_destroy(ri_data%t_3c_int_ctr_1(i, j))
    1524              :             END DO
    1525              :          END DO
    1526         1478 :          DEALLOCATE (ri_data%t_3c_int_ctr_1)
    1527              : 
    1528          224 :          DO j = 1, SIZE(ri_data%t_3c_int_ctr_2, 2)
    1529          336 :             DO i = 1, SIZE(ri_data%t_3c_int_ctr_2, 1)
    1530          224 :                CALL dbt_destroy(ri_data%t_3c_int_ctr_2(i, j))
    1531              :             END DO
    1532              :          END DO
    1533          224 :          DEALLOCATE (ri_data%t_3c_int_ctr_2)
    1534              : 
    1535          224 :          DO j = 1, SIZE(ri_data%t_3c_int_ctr_3, 2)
    1536          336 :             DO i = 1, SIZE(ri_data%t_3c_int_ctr_3, 1)
    1537          224 :                CALL dbt_destroy(ri_data%t_3c_int_ctr_3(i, j))
    1538              :             END DO
    1539              :          END DO
    1540          224 :          DEALLOCATE (ri_data%t_3c_int_ctr_3)
    1541              : 
    1542          278 :          DO j = 1, SIZE(ri_data%t_2c_int, 2)
    1543          444 :             DO i = 1, SIZE(ri_data%t_2c_int, 1)
    1544          332 :                CALL dbt_destroy(ri_data%t_2c_int(i, j))
    1545              :             END DO
    1546              :          END DO
    1547          278 :          DEALLOCATE (ri_data%t_2c_int)
    1548              : 
    1549         1478 :          DO j = 1, SIZE(ri_data%rho_ao_t, 2)
    1550         3106 :             DO i = 1, SIZE(ri_data%rho_ao_t, 1)
    1551         2994 :                CALL dbt_destroy(ri_data%rho_ao_t(i, j))
    1552              :             END DO
    1553              :          END DO
    1554         1740 :          DEALLOCATE (ri_data%rho_ao_t)
    1555              : 
    1556         1478 :          DO j = 1, SIZE(ri_data%ks_t, 2)
    1557         3106 :             DO i = 1, SIZE(ri_data%ks_t, 1)
    1558         2994 :                CALL dbt_destroy(ri_data%ks_t(i, j))
    1559              :             END DO
    1560              :          END DO
    1561         1740 :          DEALLOCATE (ri_data%ks_t)
    1562              : 
    1563            0 :          DEALLOCATE (ri_data%starts_array_mem_block, ri_data%ends_array_mem_block, &
    1564          112 :                      ri_data%starts_array_mem, ri_data%ends_array_mem)
    1565            0 :          DEALLOCATE (ri_data%starts_array_RI_mem_block, ri_data%ends_array_RI_mem_block, &
    1566          112 :                      ri_data%starts_array_RI_mem, ri_data%ends_array_RI_mem)
    1567              : 
    1568          840 :          DEALLOCATE (ri_data%blk_indices)
    1569          112 :          DEALLOCATE (ri_data%store_3c)
    1570           18 :       ELSEIF (ri_data%flavor == ri_mo) THEN
    1571           18 :          CALL dbt_destroy(ri_data%t_3c_int_ctr_1(1, 1))
    1572           18 :          CALL dbt_destroy(ri_data%t_3c_int_ctr_2(1, 1))
    1573           36 :          DEALLOCATE (ri_data%t_3c_int_ctr_1)
    1574           36 :          DEALLOCATE (ri_data%t_3c_int_ctr_2)
    1575              : 
    1576           40 :          DO ispin = 1, SIZE(ri_data%t_3c_int_mo, 1)
    1577           22 :             CALL dbt_destroy(ri_data%t_3c_int_mo(ispin, 1, 1))
    1578           22 :             CALL dbt_destroy(ri_data%t_3c_ctr_RI(ispin, 1, 1))
    1579           22 :             CALL dbt_destroy(ri_data%t_3c_ctr_KS(ispin, 1, 1))
    1580           40 :             CALL dbt_destroy(ri_data%t_3c_ctr_KS_copy(ispin, 1, 1))
    1581              :          END DO
    1582           54 :          DO ispin = 1, 2
    1583           54 :             CALL dbt_destroy(ri_data%t_2c_int(ispin, 1))
    1584              :          END DO
    1585           54 :          DEALLOCATE (ri_data%t_2c_int)
    1586           40 :          DEALLOCATE (ri_data%t_3c_int_mo)
    1587           40 :          DEALLOCATE (ri_data%t_3c_ctr_RI)
    1588           40 :          DEALLOCATE (ri_data%t_3c_ctr_KS)
    1589           40 :          DEALLOCATE (ri_data%t_3c_ctr_KS_copy)
    1590              :       END IF
    1591              : 
    1592          314 :       DO j = 1, SIZE(ri_data%t_2c_inv, 2)
    1593          498 :          DO i = 1, SIZE(ri_data%t_2c_inv, 1)
    1594          368 :             CALL dbt_destroy(ri_data%t_2c_inv(i, j))
    1595              :          END DO
    1596              :       END DO
    1597          314 :       DEALLOCATE (ri_data%t_2c_inv)
    1598              : 
    1599          314 :       DO j = 1, SIZE(ri_data%t_2c_pot, 2)
    1600          498 :          DO i = 1, SIZE(ri_data%t_2c_pot, 1)
    1601          368 :             CALL dbt_destroy(ri_data%t_2c_pot(i, j))
    1602              :          END DO
    1603              :       END DO
    1604          314 :       DEALLOCATE (ri_data%t_2c_pot)
    1605              : 
    1606          130 :       IF (ALLOCATED(ri_data%kp_mat_2c_pot)) THEN
    1607         1362 :          DO j = 1, SIZE(ri_data%kp_mat_2c_pot, 2)
    1608         2670 :             DO i = 1, SIZE(ri_data%kp_mat_2c_pot, 1)
    1609         2616 :                CALL dbcsr_release(ri_data%kp_mat_2c_pot(i, j))
    1610              :             END DO
    1611              :          END DO
    1612           54 :          DEALLOCATE (ri_data%kp_mat_2c_pot)
    1613              :       END IF
    1614              : 
    1615          130 :       IF (ALLOCATED(ri_data%kp_t_3c_int)) THEN
    1616         1362 :          DO i = 1, SIZE(ri_data%kp_t_3c_int)
    1617         1362 :             CALL dbt_destroy(ri_data%kp_t_3c_int(i))
    1618              :          END DO
    1619         1362 :          DEALLOCATE (ri_data%kp_t_3c_int)
    1620              :       END IF
    1621              : 
    1622          130 :       IF (ALLOCATED(ri_data%rho_ao_t)) THEN
    1623            0 :          DO j = 1, SIZE(ri_data%rho_ao_t, 2)
    1624            0 :             DO i = 1, SIZE(ri_data%rho_ao_t, 1)
    1625            0 :                CALL dbt_destroy(ri_data%rho_ao_t(i, j))
    1626              :             END DO
    1627              :          END DO
    1628            0 :          DEALLOCATE (ri_data%rho_ao_t)
    1629              :       END IF
    1630              : 
    1631          130 :       IF (ALLOCATED(ri_data%ks_t)) THEN
    1632            0 :          DO j = 1, SIZE(ri_data%ks_t, 2)
    1633            0 :             DO i = 1, SIZE(ri_data%ks_t, 1)
    1634            0 :                CALL dbt_destroy(ri_data%ks_t(i, j))
    1635              :             END DO
    1636              :          END DO
    1637            0 :          DEALLOCATE (ri_data%ks_t)
    1638              :       END IF
    1639              : 
    1640          130 :       IF (ALLOCATED(ri_data%iatom_to_subgroup)) THEN
    1641          162 :          DO i = 1, SIZE(ri_data%iatom_to_subgroup)
    1642          162 :             DEALLOCATE (ri_data%iatom_to_subgroup(i)%array)
    1643              :          END DO
    1644           54 :          DEALLOCATE (ri_data%iatom_to_subgroup)
    1645              :       END IF
    1646              : 
    1647          130 :       CALL timestop(handle)
    1648          130 :    END SUBROUTINE hfx_ri_release
    1649              : 
    1650              : ! **************************************************************************************************
    1651              : !> \brief - This routine allocates and initializes the basis_info and basis_parameter types
    1652              : !> \param basis_parameter ...
    1653              : !> \param basis_info ...
    1654              : !> \param qs_kind_set ...
    1655              : !> \param basis_type ...
    1656              : !> \par History
    1657              : !>      07.2011 refactored
    1658              : ! **************************************************************************************************
    1659         2044 :    SUBROUTINE hfx_create_basis_types(basis_parameter, basis_info, qs_kind_set, &
    1660              :                                      basis_type)
    1661              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
    1662              :       TYPE(hfx_basis_info_type)                          :: basis_info
    1663              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1664              :       CHARACTER(LEN=*)                                   :: basis_type
    1665              : 
    1666              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_create_basis_types'
    1667              : 
    1668              :       INTEGER :: co_counter, handle, i, ikind, ipgf, iset, j, k, la, max_am_kind, max_coeff, &
    1669              :          max_nsgfl, max_pgf, max_pgf_kind, max_set, nkind, nl_count, nset, nseta, offset_a, &
    1670              :          offset_a1, s_offset_nl_a, sgfa, so_counter
    1671         2044 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, npgfa, nshell
    1672         2044 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, nl_a
    1673         2044 :       REAL(dp), DIMENSION(:, :), POINTER                 :: sphi_a
    1674              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_a
    1675              : 
    1676         2044 :       CALL timeset(routineN, handle)
    1677              : 
    1678              :       ! BASIS parameter
    1679         2044 :       nkind = SIZE(qs_kind_set, 1)
    1680              :       !
    1681         9868 :       ALLOCATE (basis_parameter(nkind))
    1682         2044 :       max_set = 0
    1683         5780 :       DO ikind = 1, nkind
    1684         3736 :          CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_a, basis_type=basis_type)
    1685              :          CALL get_qs_kind_set(qs_kind_set, &
    1686              :                               maxsgf=basis_info%max_sgf, &
    1687              :                               maxnset=basis_info%max_set, &
    1688              :                               maxlgto=basis_info%max_am, &
    1689         3736 :                               basis_type=basis_type)
    1690         3736 :          IF (basis_info%max_set < max_set) CPABORT("UNEXPECTED MAX_SET")
    1691         3736 :          max_set = MAX(max_set, basis_info%max_set)
    1692              :          CALL get_gto_basis_set(gto_basis_set=orb_basis_a, &
    1693              :                                 lmax=basis_parameter(ikind)%lmax, &
    1694              :                                 lmin=basis_parameter(ikind)%lmin, &
    1695              :                                 npgf=basis_parameter(ikind)%npgf, &
    1696              :                                 nset=basis_parameter(ikind)%nset, &
    1697              :                                 zet=basis_parameter(ikind)%zet, &
    1698              :                                 nsgf_set=basis_parameter(ikind)%nsgf, &
    1699              :                                 first_sgf=basis_parameter(ikind)%first_sgf, &
    1700              :                                 sphi=basis_parameter(ikind)%sphi, &
    1701              :                                 nsgf=basis_parameter(ikind)%nsgf_total, &
    1702              :                                 l=basis_parameter(ikind)%nl, &
    1703              :                                 nshell=basis_parameter(ikind)%nshell, &
    1704              :                                 set_radius=basis_parameter(ikind)%set_radius, &
    1705              :                                 pgf_radius=basis_parameter(ikind)%pgf_radius, &
    1706         5780 :                                 kind_radius=basis_parameter(ikind)%kind_radius)
    1707              :       END DO
    1708         5780 :       DO ikind = 1, nkind
    1709        14944 :          ALLOCATE (basis_parameter(ikind)%nsgfl(0:basis_info%max_am, max_set))
    1710        46864 :          basis_parameter(ikind)%nsgfl = 0
    1711         3736 :          nset = basis_parameter(ikind)%nset
    1712         3736 :          nshell => basis_parameter(ikind)%nshell
    1713        16508 :          DO iset = 1, nset
    1714        43168 :             DO i = 0, basis_info%max_am
    1715        28704 :                nl_count = 0
    1716        66880 :                DO j = 1, nshell(iset)
    1717        66880 :                   IF (basis_parameter(ikind)%nl(j, iset) == i) nl_count = nl_count + 1
    1718              :                END DO
    1719        39432 :                basis_parameter(ikind)%nsgfl(i, iset) = nl_count
    1720              :             END DO
    1721              :          END DO
    1722              :       END DO
    1723              : 
    1724              :       max_nsgfl = 0
    1725              :       max_pgf = 0
    1726         5780 :       DO ikind = 1, nkind
    1727         3736 :          max_coeff = 0
    1728         3736 :          max_am_kind = 0
    1729         3736 :          max_pgf_kind = 0
    1730         3736 :          npgfa => basis_parameter(ikind)%npgf
    1731         3736 :          nseta = basis_parameter(ikind)%nset
    1732         3736 :          nl_a => basis_parameter(ikind)%nsgfl
    1733         3736 :          la_max => basis_parameter(ikind)%lmax
    1734         3736 :          la_min => basis_parameter(ikind)%lmin
    1735        14464 :          DO iset = 1, nseta
    1736        10728 :             max_pgf_kind = MAX(max_pgf_kind, npgfa(iset))
    1737              :             max_pgf = MAX(max_pgf, npgfa(iset))
    1738        27626 :             DO la = la_min(iset), la_max(iset)
    1739        13162 :                max_nsgfl = MAX(max_nsgfl, nl_a(la, iset))
    1740        13162 :                max_coeff = MAX(max_coeff, nso(la)*nl_a(la, iset)*nco(la))
    1741        23890 :                max_am_kind = MAX(max_am_kind, la)
    1742              :             END DO
    1743              :          END DO
    1744        22416 :          ALLOCATE (basis_parameter(ikind)%sphi_ext(max_coeff, 0:max_am_kind, max_pgf_kind, nseta))
    1745      2138054 :          basis_parameter(ikind)%sphi_ext = 0.0_dp
    1746              :       END DO
    1747              : 
    1748         5780 :       DO ikind = 1, nkind
    1749         3736 :          sphi_a => basis_parameter(ikind)%sphi
    1750         3736 :          nseta = basis_parameter(ikind)%nset
    1751         3736 :          la_max => basis_parameter(ikind)%lmax
    1752         3736 :          la_min => basis_parameter(ikind)%lmin
    1753         3736 :          npgfa => basis_parameter(ikind)%npgf
    1754         3736 :          first_sgfa => basis_parameter(ikind)%first_sgf
    1755         3736 :          nl_a => basis_parameter(ikind)%nsgfl
    1756        16508 :          DO iset = 1, nseta
    1757        10728 :             sgfa = first_sgfa(1, iset)
    1758        34686 :             DO ipgf = 1, npgfa(iset)
    1759        20222 :                offset_a1 = (ipgf - 1)*ncoset(la_max(iset))
    1760        20222 :                s_offset_nl_a = 0
    1761        57344 :                DO la = la_min(iset), la_max(iset)
    1762        26394 :                   offset_a = offset_a1 + ncoset(la - 1)
    1763              :                   co_counter = 0
    1764        26394 :                   co_counter = co_counter + 1
    1765        26394 :                   so_counter = 0
    1766        81652 :                   DO k = sgfa + s_offset_nl_a, sgfa + s_offset_nl_a + nso(la)*nl_a(la, iset) - 1
    1767       233480 :                      DO i = offset_a + 1, offset_a + nco(la)
    1768       151828 :                         so_counter = so_counter + 1
    1769       207086 :                         basis_parameter(ikind)%sphi_ext(so_counter, la, ipgf, iset) = sphi_a(i, k)
    1770              :                      END DO
    1771              :                   END DO
    1772        46616 :                   s_offset_nl_a = s_offset_nl_a + nso(la)*(nl_a(la, iset))
    1773              :                END DO
    1774              :             END DO
    1775              :          END DO
    1776              :       END DO
    1777              : 
    1778         2044 :       CALL timestop(handle)
    1779              : 
    1780         2044 :    END SUBROUTINE hfx_create_basis_types
    1781              : 
    1782              : ! **************************************************************************************************
    1783              : !> \brief ...
    1784              : !> \param basis_parameter ...
    1785              : ! **************************************************************************************************
    1786         2044 :    SUBROUTINE hfx_release_basis_types(basis_parameter)
    1787              :       TYPE(hfx_basis_type), DIMENSION(:), POINTER        :: basis_parameter
    1788              : 
    1789              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'hfx_release_basis_types'
    1790              : 
    1791              :       INTEGER                                            :: handle, i
    1792              : 
    1793         2044 :       CALL timeset(routineN, handle)
    1794              : 
    1795              :       !! BASIS parameter
    1796         5780 :       DO i = 1, SIZE(basis_parameter)
    1797         3736 :          DEALLOCATE (basis_parameter(i)%nsgfl)
    1798         5780 :          DEALLOCATE (basis_parameter(i)%sphi_ext)
    1799              :       END DO
    1800         2044 :       DEALLOCATE (basis_parameter)
    1801         2044 :       CALL timestop(handle)
    1802              : 
    1803         2044 :    END SUBROUTINE hfx_release_basis_types
    1804              : 
    1805              : ! **************************************************************************************************
    1806              : !> \brief - Parses the memory section
    1807              : !> \param memory_parameter ...
    1808              : !> \param hf_sub_section ...
    1809              : !> \param storage_id ...
    1810              : !> \param i_thread ...
    1811              : !> \param n_threads ...
    1812              : !> \param para_env ...
    1813              : !> \param irep ...
    1814              : !> \param skip_disk ...
    1815              : !> \param skip_in_core_forces ...
    1816              : ! **************************************************************************************************
    1817         2350 :    SUBROUTINE parse_memory_section(memory_parameter, hf_sub_section, storage_id, &
    1818              :                                    i_thread, n_threads, para_env, irep, skip_disk, skip_in_core_forces)
    1819              :       TYPE(hfx_memory_type)                              :: memory_parameter
    1820              :       TYPE(section_vals_type), POINTER                   :: hf_sub_section
    1821              :       INTEGER, INTENT(OUT), OPTIONAL                     :: storage_id
    1822              :       INTEGER, INTENT(IN), OPTIONAL                      :: i_thread, n_threads
    1823              :       TYPE(mp_para_env_type), OPTIONAL                   :: para_env
    1824              :       INTEGER, INTENT(IN), OPTIONAL                      :: irep
    1825              :       LOGICAL, INTENT(IN)                                :: skip_disk, skip_in_core_forces
    1826              : 
    1827              :       CHARACTER(LEN=512)                                 :: error_msg
    1828              :       CHARACTER(LEN=default_path_length)                 :: char_val, filename, orig_wd
    1829              :       INTEGER                                            :: int_val, stat
    1830              :       LOGICAL                                            :: check, logic_val
    1831              :       REAL(dp)                                           :: real_val
    1832              : 
    1833              :       check = (PRESENT(storage_id) .EQV. PRESENT(i_thread)) .AND. &
    1834              :               (PRESENT(storage_id) .EQV. PRESENT(n_threads)) .AND. &
    1835              :               (PRESENT(storage_id) .EQV. PRESENT(para_env)) .AND. &
    1836         2350 :               (PRESENT(storage_id) .EQV. PRESENT(irep))
    1837            0 :       CPASSERT(check)
    1838              : 
    1839              :       ! Memory Storage
    1840         2350 :       CALL section_vals_val_get(hf_sub_section, "MAX_MEMORY", i_val=int_val)
    1841         2350 :       memory_parameter%max_memory = int_val
    1842         2350 :       memory_parameter%max_compression_counter = int_val*1024_int_8*128_int_8
    1843         2350 :       CALL section_vals_val_get(hf_sub_section, "EPS_STORAGE", r_val=real_val)
    1844         2350 :       memory_parameter%eps_storage_scaling = real_val
    1845         2350 :       IF (int_val == 0) THEN
    1846           20 :          memory_parameter%do_all_on_the_fly = .TRUE.
    1847              :       ELSE
    1848         2330 :          memory_parameter%do_all_on_the_fly = .FALSE.
    1849              :       END IF
    1850         2350 :       memory_parameter%cache_size = CACHE_SIZE
    1851         2350 :       memory_parameter%bits_max_val = BITS_MAX_VAL
    1852         2350 :       memory_parameter%actual_memory_usage = 1
    1853         2350 :       IF (.NOT. skip_in_core_forces) THEN
    1854         1352 :          CALL section_vals_val_get(hf_sub_section, "TREAT_FORCES_IN_CORE", l_val=logic_val)
    1855         1352 :          memory_parameter%treat_forces_in_core = logic_val
    1856              :       END IF
    1857              : 
    1858              :       ! ** IF MAX_MEM == 0 overwrite this flag to false
    1859         2350 :       IF (memory_parameter%do_all_on_the_fly) memory_parameter%treat_forces_in_core = .FALSE.
    1860              : 
    1861              :       ! Disk Storage
    1862         2350 :       IF (.NOT. skip_disk) THEN
    1863         1352 :          memory_parameter%actual_memory_usage_disk = 1
    1864         1352 :          CALL section_vals_val_get(hf_sub_section, "MAX_DISK_SPACE", i_val=int_val)
    1865         1352 :          memory_parameter%max_compression_counter_disk = int_val*1024_int_8*128_int_8
    1866         1352 :          IF (int_val == 0) THEN
    1867         1346 :             memory_parameter%do_disk_storage = .FALSE.
    1868              :          ELSE
    1869            6 :             memory_parameter%do_disk_storage = .TRUE.
    1870              :          END IF
    1871         1352 :          CALL section_vals_val_get(hf_sub_section, "STORAGE_LOCATION", c_val=char_val)
    1872         1352 :          CALL compress(char_val, .TRUE.)
    1873              :          !! Add ending / if necessary
    1874              : 
    1875         1352 :          IF (SCAN(char_val, "/", .TRUE.) /= LEN_TRIM(char_val)) THEN
    1876         1352 :             WRITE (filename, '(A,A)') TRIM(char_val), "/"
    1877         1352 :             CALL compress(filename)
    1878              :          ELSE
    1879            0 :             filename = TRIM(char_val)
    1880              :          END IF
    1881         1352 :          CALL compress(filename, .TRUE.)
    1882              : 
    1883              :          !! quickly check if we can write on storage_location
    1884         1352 :          CALL m_getcwd(orig_wd)
    1885         1352 :          CALL m_chdir(TRIM(filename), stat)
    1886         1352 :          IF (stat /= 0) THEN
    1887            0 :             WRITE (error_msg, '(A,A,A)') "Request for disk storage failed due to unknown error while writing to ", &
    1888            0 :                TRIM(filename), ". Please check STORAGE_LOCATION"
    1889            0 :             CPABORT(error_msg)
    1890              :          END IF
    1891         1352 :          CALL m_chdir(orig_wd, stat)
    1892              : 
    1893         1352 :          memory_parameter%storage_location = filename
    1894         1352 :          CALL compress(memory_parameter%storage_location, .TRUE.)
    1895              :       ELSE
    1896          998 :          memory_parameter%do_disk_storage = .FALSE.
    1897              :       END IF
    1898         2350 :       IF (PRESENT(storage_id)) THEN
    1899         1352 :          storage_id = (irep - 1)*para_env%num_pe*n_threads + para_env%mepos*n_threads + i_thread - 1
    1900              :       END IF
    1901         2350 :    END SUBROUTINE parse_memory_section
    1902              : 
    1903              : ! **************************************************************************************************
    1904              : !> \brief - This routine deallocates all data structures
    1905              : !> \param x_data contains all relevant data structures for hfx runs
    1906              : !> \par History
    1907              : !>      09.2007 created [Manuel Guidon]
    1908              : !> \author Manuel Guidon
    1909              : ! **************************************************************************************************
    1910         1342 :    SUBROUTINE hfx_release(x_data)
    1911              :       TYPE(hfx_type), DIMENSION(:, :), POINTER           :: x_data
    1912              : 
    1913              :       INTEGER                                            :: i, i_thread, irep, n_rep_hf, n_threads
    1914              :       TYPE(cp_logger_type), POINTER                      :: logger
    1915              :       TYPE(hfx_type), POINTER                            :: actual_x_data
    1916              : 
    1917              : !! There might be 2 hf sections
    1918              : 
    1919         1342 :       n_rep_hf = x_data(1, 1)%n_rep_hf
    1920         1342 :       n_threads = SIZE(x_data, 2)
    1921              : 
    1922         1342 :       IF (x_data(1, 1)%potential_parameter%potential_type == do_potential_truncated .OR. &
    1923              :           x_data(1, 1)%potential_parameter%potential_type == do_potential_mix_cl_trunc) THEN
    1924          368 :          init_t_c_g0_lmax = -1
    1925          368 :          CALL free_C0()
    1926              :       END IF
    1927         2684 :       DO i_thread = 1, n_threads
    1928         4036 :          DO irep = 1, n_rep_hf
    1929         1352 :             actual_x_data => x_data(irep, i_thread)
    1930         1352 :             DEALLOCATE (actual_x_data%neighbor_cells)
    1931         1352 :             DEALLOCATE (actual_x_data%distribution_energy)
    1932         1352 :             DEALLOCATE (actual_x_data%distribution_forces)
    1933              : 
    1934         1352 :             IF (actual_x_data%load_balance_parameter%blocks_initialized) THEN
    1935         1236 :                DEALLOCATE (actual_x_data%blocks)
    1936         1236 :                IF (i_thread == 1) THEN
    1937         1236 :                   DEALLOCATE (actual_x_data%pmax_block)
    1938              :                END IF
    1939              :             END IF
    1940              : 
    1941         1352 :             IF (i_thread == 1) THEN
    1942         1352 :                DEALLOCATE (actual_x_data%atomic_pair_list)
    1943         1352 :                DEALLOCATE (actual_x_data%atomic_pair_list_forces)
    1944              :             END IF
    1945              : 
    1946         1352 :             IF (actual_x_data%screening_parameter%do_initial_p_screening .OR. &
    1947              :                 actual_x_data%screening_parameter%do_p_screening_forces) THEN
    1948         1330 :                IF (i_thread == 1) THEN
    1949         1330 :                   DEALLOCATE (actual_x_data%pmax_atom)
    1950         5392 :                   DO i = 1, SIZE(actual_x_data%initial_p)
    1951         5392 :                      DEALLOCATE (actual_x_data%initial_p(i)%p_kind)
    1952              :                   END DO
    1953         1330 :                   DEALLOCATE (actual_x_data%initial_p)
    1954              : 
    1955         1330 :                   DEALLOCATE (actual_x_data%pmax_atom_forces)
    1956         5392 :                   DO i = 1, SIZE(actual_x_data%initial_p_forces)
    1957         5392 :                      DEALLOCATE (actual_x_data%initial_p_forces(i)%p_kind)
    1958              :                   END DO
    1959         1330 :                   DEALLOCATE (actual_x_data%initial_p_forces)
    1960              :                END IF
    1961         1330 :                DEALLOCATE (actual_x_data%map_atom_to_kind_atom)
    1962              :             END IF
    1963         1352 :             IF (i_thread == 1) THEN
    1964         1352 :                DEALLOCATE (actual_x_data%is_assoc_atomic_block)
    1965         1352 :                DEALLOCATE (actual_x_data%atomic_block_offset)
    1966         1352 :                DEALLOCATE (actual_x_data%set_offset)
    1967         1352 :                DEALLOCATE (actual_x_data%block_offset)
    1968              :             END IF
    1969              : 
    1970              :             !! BASIS parameter
    1971         1352 :             CALL hfx_release_basis_types(actual_x_data%basis_parameter)
    1972              : 
    1973              :             !MK Release libint and libderiv data structure
    1974         1352 :             CALL cp_libint_cleanup_eri(actual_x_data%lib)
    1975         1352 :             CALL cp_libint_cleanup_eri1(actual_x_data%lib_deriv)
    1976         1352 :             CALL cp_libint_static_cleanup()
    1977              : 
    1978              :             !! Deallocate containers
    1979         1352 :             CALL dealloc_containers(actual_x_data%store_ints, actual_x_data%memory_parameter%actual_memory_usage)
    1980         1352 :             CALL dealloc_containers(actual_x_data%store_forces, actual_x_data%memory_parameter%actual_memory_usage)
    1981              : 
    1982              :             !! Deallocate containers
    1983              :             CALL hfx_init_container(actual_x_data%store_ints%maxval_container_disk, &
    1984              :                                     actual_x_data%memory_parameter%actual_memory_usage_disk, &
    1985         1352 :                                     .FALSE.)
    1986         1352 :             IF (actual_x_data%memory_parameter%do_disk_storage) THEN
    1987            6 :                CALL close_file(unit_number=actual_x_data%store_ints%maxval_container_disk%unit, file_status="DELETE")
    1988              :             END IF
    1989         1352 :             DEALLOCATE (actual_x_data%store_ints%maxval_container_disk%first)
    1990         1352 :             DEALLOCATE (actual_x_data%store_ints%maxval_container_disk)
    1991              : 
    1992        87880 :             DO i = 1, 64
    1993              :                CALL hfx_init_container(actual_x_data%store_ints%integral_containers_disk(i), &
    1994              :                                        actual_x_data%memory_parameter%actual_memory_usage_disk, &
    1995        86528 :                                        .FALSE.)
    1996        86528 :                IF (actual_x_data%memory_parameter%do_disk_storage) THEN
    1997          384 :                   CALL close_file(unit_number=actual_x_data%store_ints%integral_containers_disk(i)%unit, file_status="DELETE")
    1998              :                END IF
    1999        87880 :                DEALLOCATE (actual_x_data%store_ints%integral_containers_disk(i)%first)
    2000              :             END DO
    2001         1352 :             DEALLOCATE (actual_x_data%store_ints%integral_containers_disk)
    2002              : 
    2003              :             ! ** screening functions
    2004         1352 :             IF (actual_x_data%screen_funct_is_initialized) THEN
    2005         1236 :                DEALLOCATE (actual_x_data%screen_funct_coeffs_set)
    2006         1236 :                DEALLOCATE (actual_x_data%screen_funct_coeffs_kind)
    2007         1236 :                DEALLOCATE (actual_x_data%pair_dist_radii_pgf)
    2008         1236 :                DEALLOCATE (actual_x_data%screen_funct_coeffs_pgf)
    2009         1236 :                actual_x_data%screen_funct_is_initialized = .FALSE.
    2010              :             END IF
    2011              : 
    2012              :             ! ** maps
    2013         1352 :             IF (ASSOCIATED(actual_x_data%map_atoms_to_cpus)) THEN
    2014         3706 :                DO i = 1, SIZE(actual_x_data%map_atoms_to_cpus)
    2015         2470 :                   DEALLOCATE (actual_x_data%map_atoms_to_cpus(i)%iatom_list)
    2016         3706 :                   DEALLOCATE (actual_x_data%map_atoms_to_cpus(i)%jatom_list)
    2017              :                END DO
    2018         1236 :                DEALLOCATE (actual_x_data%map_atoms_to_cpus)
    2019              :             END IF
    2020              : 
    2021         2694 :             IF (actual_x_data%do_hfx_ri) THEN
    2022          108 :                CALL hfx_ri_release(actual_x_data%ri_data)
    2023          108 :                IF (ASSOCIATED(actual_x_data%ri_data%ri_section)) THEN
    2024          108 :                   logger => cp_get_default_logger()
    2025              :                   CALL cp_print_key_finished_output(actual_x_data%ri_data%unit_nr_dbcsr, logger, actual_x_data%ri_data%ri_section, &
    2026          108 :                                                     "PRINT%RI_INFO")
    2027              :                END IF
    2028          108 :                IF (ASSOCIATED(actual_x_data%ri_data%hfx_section)) THEN
    2029          108 :                   logger => cp_get_default_logger()
    2030              :                   CALL cp_print_key_finished_output(actual_x_data%ri_data%unit_nr, logger, actual_x_data%ri_data%hfx_section, &
    2031          108 :                                                     "HF_INFO")
    2032              :                END IF
    2033          108 :                DEALLOCATE (actual_x_data%ri_data)
    2034              :             END IF
    2035              :          END DO
    2036              : 
    2037              :       END DO
    2038              : 
    2039         1342 :       DEALLOCATE (x_data)
    2040         1342 :    END SUBROUTINE hfx_release
    2041              : 
    2042              : ! **************************************************************************************************
    2043              : !> \brief - This routine computes the neighbor cells that are taken into account
    2044              : !>        in periodic runs
    2045              : !> \param x_data contains all relevant data structures for hfx runs
    2046              : !> \param pbc_shells number of shells taken into account
    2047              : !> \param cell cell
    2048              : !> \param i_thread current thread ID
    2049              : !> \param nkp_grid ...
    2050              : !> \par History
    2051              : !>      09.2007 created [Manuel Guidon]
    2052              : !> \author Manuel Guidon
    2053              : ! **************************************************************************************************
    2054         9677 :    SUBROUTINE hfx_create_neighbor_cells(x_data, pbc_shells, cell, i_thread, nkp_grid)
    2055              :       TYPE(hfx_type), POINTER                            :: x_data
    2056              :       INTEGER, INTENT(INOUT)                             :: pbc_shells
    2057              :       TYPE(cell_type), POINTER                           :: cell
    2058              :       INTEGER, INTENT(IN)                                :: i_thread
    2059              :       INTEGER, DIMENSION(3), OPTIONAL                    :: nkp_grid
    2060              : 
    2061              :       CHARACTER(LEN=512)                                 :: error_msg
    2062              :       CHARACTER(LEN=64)                                  :: char_nshells
    2063              :       INTEGER :: i, idx, ikind, ipgf, iset, ishell, j, jkind, jpgf, jset, jshell, k, kshell, l, &
    2064              :          m(3), max_shell, nkp(3), nseta, nsetb, perd(3), total_number_of_cells, ub, ub_max
    2065         9677 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, lb_max, npgfa, npgfb
    2066              :       LOGICAL                                            :: do_kpoints, image_cell_found, &
    2067              :                                                             nothing_more_to_add
    2068              :       REAL(dp) :: cross_product(3), dist_min, distance(14), l_min, normal(3, 6), P(3, 14), &
    2069              :          plane_vector(3, 2), point_in_plane(3), r(3), R1, R_max, R_max_stress, s(3), x, y, z, Zeta1
    2070         9677 :       REAL(dp), DIMENSION(:, :), POINTER                 :: zeta, zetb
    2071         9677 :       TYPE(hfx_cell_type), ALLOCATABLE, DIMENSION(:)     :: tmp_neighbor_cells
    2072              : 
    2073         9677 :       total_number_of_cells = 0
    2074              : 
    2075        38708 :       nkp = 1
    2076         9677 :       IF (PRESENT(nkp_grid)) nkp = nkp_grid
    2077        38546 :       do_kpoints = ANY(nkp > 1)
    2078              : 
    2079              :       ! ** Check some settings
    2080         9677 :       IF (i_thread == 1) THEN
    2081              :          IF (x_data%potential_parameter%potential_type /= do_potential_truncated .AND. &
    2082              :              x_data%potential_parameter%potential_type /= do_potential_short .AND. &
    2083          426 :              x_data%potential_parameter%potential_type /= do_potential_mix_cl_trunc .AND. &
    2084              :              x_data%potential_parameter%potential_type /= do_potential_id) THEN
    2085              :             CALL cp_warn(__LOCATION__, &
    2086              :                          "Periodic Hartree Fock calculation requested without use "// &
    2087              :                          "of a truncated or shortrange potential. This may lead to unphysical total energies. "// &
    2088           96 :                          "Use a truncated  potential to avoid possible problems.")
    2089          330 :          ELSE IF (x_data%potential_parameter%potential_type /= do_potential_id) THEN
    2090              :             !If k-points, use the Born-von Karman super cell as reference
    2091              :             l_min = MIN(REAL(nkp(1), dp)*plane_distance(1, 0, 0, cell), &
    2092              :                         REAL(nkp(2), dp)*plane_distance(0, 1, 0, cell), &
    2093          308 :                         REAL(nkp(3), dp)*plane_distance(0, 0, 1, cell))
    2094          308 :             l_min = 0.5_dp*l_min
    2095          308 :             IF (x_data%potential_parameter%cutoff_radius >= l_min) THEN
    2096           38 :                IF (.NOT. do_kpoints) THEN
    2097              :                   WRITE (error_msg, "(A,F6.3,A,F6.3,A)") &
    2098              :                      "Periodic Hartree Fock calculation requested with the use "// &
    2099              :                      "of a truncated or shortrange potential. "// &
    2100           38 :                      "The cutoff radius (", x_data%potential_parameter%cutoff_radius/a_bohr*1e-10_dp, &
    2101           38 :                      " A) is larger than half the minimal cell dimension (", &
    2102           38 :                      l_min/a_bohr*1e-10_dp, " A). This may lead to unphysical "// &
    2103              :                      "total energies. Reduce the cutoff radius in order to avoid "// &
    2104           76 :                      "possible problems."
    2105              :                ELSE
    2106              :                   WRITE (error_msg, "(A,F6.3,A,F6.3,A)") &
    2107              :                      "K-point Hartree-Fock calculation requested with the use of a "// &
    2108            0 :                      "truncated or shortrange potential. The cutoff radius (", &
    2109            0 :                      x_data%potential_parameter%cutoff_radius/a_bohr*1e-10_dp, &
    2110            0 :                      " A) is larger than half the minimal Born-von Karman supercell dimension (", &
    2111            0 :                      l_min/a_bohr*1e-10_dp, " A). This may lead "// &
    2112              :                      "to unphysical total energies. Reduce the cutoff radius or increase "// &
    2113            0 :                      "the number of K-points in order to avoid possible problems."
    2114              :                END IF
    2115           38 :                CALL cp_warn(__LOCATION__, error_msg)
    2116              :             END IF
    2117              :          END IF
    2118              :       END IF
    2119              : 
    2120        16829 :       SELECT CASE (x_data%potential_parameter%potential_type)
    2121              :       CASE (do_potential_truncated, do_potential_mix_cl_trunc, do_potential_short)
    2122         7152 :          R_max = 0.0_dp
    2123        19754 :          DO ikind = 1, SIZE(x_data%basis_parameter)
    2124        12602 :             la_max => x_data%basis_parameter(ikind)%lmax
    2125        12602 :             zeta => x_data%basis_parameter(ikind)%zet
    2126        12602 :             nseta = x_data%basis_parameter(ikind)%nset
    2127        12602 :             npgfa => x_data%basis_parameter(ikind)%npgf
    2128        43376 :             DO jkind = 1, SIZE(x_data%basis_parameter)
    2129        23622 :                lb_max => x_data%basis_parameter(jkind)%lmax
    2130        23622 :                zetb => x_data%basis_parameter(jkind)%zet
    2131        23622 :                nsetb = x_data%basis_parameter(jkind)%nset
    2132        23622 :                npgfb => x_data%basis_parameter(jkind)%npgf
    2133        98196 :                DO iset = 1, nseta
    2134       267136 :                   DO jset = 1, nsetb
    2135       557076 :                      DO ipgf = 1, npgfa(iset)
    2136      1080230 :                         DO jpgf = 1, npgfb(jset)
    2137       585126 :                            Zeta1 = zeta(ipgf, iset) + zetb(jpgf, jset)
    2138              :                            R1 = 1.0_dp/SQRT(Zeta1)*mul_fact(la_max(iset) + lb_max(jset))* &
    2139       585126 :                                 SQRT(-LOG(x_data%screening_parameter%eps_schwarz))
    2140       898688 :                            R_max = MAX(R1, R_max)
    2141              :                         END DO
    2142              :                      END DO
    2143              :                   END DO
    2144              :                END DO
    2145              :             END DO
    2146              :          END DO
    2147              : 
    2148         7152 :          R_max = 2.0_dp*R_max + x_data%potential_parameter%cutoff_radius
    2149         7152 :          nothing_more_to_add = .FALSE.
    2150         7152 :          max_shell = 0
    2151         7152 :          total_number_of_cells = 0
    2152         7152 :          ub = 1
    2153         7152 :          DEALLOCATE (x_data%neighbor_cells)
    2154        57216 :          ALLOCATE (x_data%neighbor_cells(1))
    2155        28608 :          x_data%neighbor_cells(1)%cell = 0.0_dp
    2156        28608 :          x_data%neighbor_cells(1)%cell_r = 0.0_dp
    2157              : 
    2158              :          ! ** What follows is kind of a ray tracing algorithm
    2159              :          ! ** Given a image cell (ishell, jshell, kshell) we try to figure out the
    2160              :          ! ** shortest distance of this image cell to the basic unit cell (0,0,0), i.e. the point
    2161              :          ! ** (0.0, 0.0, 0.0)
    2162              :          ! ** This is achieved by checking the 8 Corners of the cell, and, in addition, the shortest distance
    2163              :          ! ** to all 6 faces. The faces are only taken into account if the penetration point of the normal
    2164              :          ! ** to the plane defined by a face lies within this face.
    2165              :          ! ** This is very fast, because no trigonometric functions are being used
    2166              :          ! ** The points are defined as follows
    2167              :          ! **
    2168              :          ! **
    2169              :          ! **               _________________________
    2170              :          ! **              /P4____________________P8/|
    2171              :          ! **             / / ___________________/ / |
    2172              :          ! **            / / /| |               / /  |       z
    2173              :          ! **           / / / | |              / / . |      /|\  _ y
    2174              :          ! **          / / /| | |             / / /| |       |   /|
    2175              :          ! **         / / / | | |            / / / | |       |  /
    2176              :          ! **        / / /  | | |           / / /| | |       | /
    2177              :          ! **       / /_/___| | |__________/ / / | | |       |/
    2178              :          ! **      /P2______| | |_________P6/ /  | | |       ----------> x
    2179              :          ! **      | _______| | |_________| | |  | | |
    2180              :          ! **      | | |    | | |________________| | |
    2181              :          ! **      | | |    |P3___________________P7 |
    2182              :          ! **      | | |   / / _________________  / /
    2183              :          ! **      | | |  / / /           | | |/ / /
    2184              :          ! **      | | | / / /            | | | / /
    2185              :          ! **      | | |/ / /             | | |/ /
    2186              :          ! **      | | | / /              | | ' /
    2187              :          ! **      | | |/_/_______________| |  /
    2188              :          ! **      | |____________________| | /
    2189              :          ! **      |P1_____________________P5/
    2190              :          ! **
    2191              :          ! **
    2192              : 
    2193        36212 :          DO WHILE (.NOT. nothing_more_to_add)
    2194              :             ! Calculate distances to the eight points P1 to P8
    2195        29060 :             image_cell_found = .FALSE.
    2196      1225012 :             ALLOCATE (tmp_neighbor_cells(1:ub))
    2197       963472 :             DO i = 1, ub - 1
    2198       963472 :                tmp_neighbor_cells(i) = x_data%neighbor_cells(i)
    2199              :             END DO
    2200        29060 :             ub_max = (2*max_shell + 1)**3
    2201        29060 :             DEALLOCATE (x_data%neighbor_cells)
    2202      4415880 :             ALLOCATE (x_data%neighbor_cells(1:ub_max))
    2203       963472 :             DO i = 1, ub - 1
    2204       963472 :                x_data%neighbor_cells(i) = tmp_neighbor_cells(i)
    2205              :             END DO
    2206      3248988 :             DO i = ub, ub_max
    2207     12879712 :                x_data%neighbor_cells(i)%cell = 0.0_dp
    2208     12908772 :                x_data%neighbor_cells(i)%cell_r = 0.0_dp
    2209              :             END DO
    2210              : 
    2211        29060 :             DEALLOCATE (tmp_neighbor_cells)
    2212              : 
    2213       116240 :             perd(1:3) = x_data%periodic_parameter%perd(1:3)
    2214              : 
    2215       148136 :             DO ishell = -max_shell*perd(1), max_shell*perd(1)
    2216       800728 :             DO jshell = -max_shell*perd(2), max_shell*perd(2)
    2217      4809680 :             DO kshell = -max_shell*perd(3), max_shell*perd(3)
    2218      4038012 :                IF (MAX(ABS(ishell), ABS(jshell), ABS(kshell)) /= max_shell) CYCLE
    2219              :                idx = 0
    2220      8039532 :                DO j = 0, 1
    2221      5359688 :                   x = -1.0_dp/2.0_dp + j*1.0_dp
    2222     18758908 :                   DO k = 0, 1
    2223     10719376 :                      y = -1.0_dp/2.0_dp + k*1.0_dp
    2224     37517816 :                      DO l = 0, 1
    2225     21438752 :                         z = -1.0_dp/2.0_dp + l*1.0_dp
    2226     21438752 :                         idx = idx + 1
    2227     21438752 :                         P(1, idx) = x + ishell
    2228     21438752 :                         P(2, idx) = y + jshell
    2229     21438752 :                         P(3, idx) = z + kshell
    2230     21438752 :                         CALL scaled_to_real(r, P(:, idx), cell)
    2231     85755008 :                         distance(idx) = SQRT(SUM(r**2))
    2232     96474384 :                         P(1:3, idx) = r
    2233              :                      END DO
    2234              :                   END DO
    2235              :                END DO
    2236              :                ! Now check distance to Faces and only take them into account if the base point lies within quadrilateral
    2237              : 
    2238              :                ! Face A (1342) 1 is the reference
    2239      2679844 :                idx = idx + 1
    2240     10719376 :                plane_vector(:, 1) = P(:, 3) - P(:, 1)
    2241     10719376 :                plane_vector(:, 2) = P(:, 2) - P(:, 1)
    2242      2679844 :                cross_product(1) = plane_vector(2, 1)*plane_vector(3, 2) - plane_vector(3, 1)*plane_vector(2, 2)
    2243      2679844 :                cross_product(2) = plane_vector(3, 1)*plane_vector(1, 2) - plane_vector(1, 1)*plane_vector(3, 2)
    2244      2679844 :                cross_product(3) = plane_vector(1, 1)*plane_vector(2, 2) - plane_vector(2, 1)*plane_vector(1, 2)
    2245     18758908 :                normal(:, 1) = cross_product/SQRT(SUM(cross_product**2))
    2246     10719376 :                point_in_plane = -normal(:, 1)*(normal(1, 1)*P(1, 1) + normal(2, 1)*P(2, 1) + normal(3, 1)*P(3, 1))
    2247              : 
    2248      2679844 :                IF (point_is_in_quadrilateral(P(:, 1), P(:, 3), P(:, 4), P(:, 2), point_in_plane)) THEN
    2249        51796 :                   distance(idx) = ABS(normal(1, 1)*P(1, 1) + normal(2, 1)*P(2, 1) + normal(3, 1)*P(3, 1))
    2250              :                ELSE
    2251      2628048 :                   distance(idx) = HUGE(distance(idx))
    2252              :                END IF
    2253              : 
    2254              :                ! Face B (1562) 1 is the reference
    2255      2679844 :                idx = idx + 1
    2256     10719376 :                plane_vector(:, 1) = P(:, 2) - P(:, 1)
    2257     10719376 :                plane_vector(:, 2) = P(:, 5) - P(:, 1)
    2258      2679844 :                cross_product(1) = plane_vector(2, 1)*plane_vector(3, 2) - plane_vector(3, 1)*plane_vector(2, 2)
    2259      2679844 :                cross_product(2) = plane_vector(3, 1)*plane_vector(1, 2) - plane_vector(1, 1)*plane_vector(3, 2)
    2260      2679844 :                cross_product(3) = plane_vector(1, 1)*plane_vector(2, 2) - plane_vector(2, 1)*plane_vector(1, 2)
    2261     18758908 :                normal(:, 1) = cross_product/SQRT(SUM(cross_product**2))
    2262     10719376 :                point_in_plane = -normal(:, 1)*(normal(1, 1)*P(1, 1) + normal(2, 1)*P(2, 1) + normal(3, 1)*P(3, 1))
    2263              : 
    2264      2679844 :                IF (point_is_in_quadrilateral(P(:, 1), P(:, 5), P(:, 6), P(:, 2), point_in_plane)) THEN
    2265        51972 :                   distance(idx) = ABS(normal(1, 1)*P(1, 1) + normal(2, 1)*P(2, 1) + normal(3, 1)*P(3, 1))
    2266              :                ELSE
    2267      2627872 :                   distance(idx) = HUGE(distance(idx))
    2268              :                END IF
    2269              : 
    2270              :                ! Face C (5786) 5 is the reference
    2271      2679844 :                idx = idx + 1
    2272     10719376 :                plane_vector(:, 1) = P(:, 7) - P(:, 5)
    2273     10719376 :                plane_vector(:, 2) = P(:, 6) - P(:, 5)
    2274      2679844 :                cross_product(1) = plane_vector(2, 1)*plane_vector(3, 2) - plane_vector(3, 1)*plane_vector(2, 2)
    2275      2679844 :                cross_product(2) = plane_vector(3, 1)*plane_vector(1, 2) - plane_vector(1, 1)*plane_vector(3, 2)
    2276      2679844 :                cross_product(3) = plane_vector(1, 1)*plane_vector(2, 2) - plane_vector(2, 1)*plane_vector(1, 2)
    2277     18758908 :                normal(:, 1) = cross_product/SQRT(SUM(cross_product**2))
    2278     10719376 :                point_in_plane = -normal(:, 1)*(normal(1, 1)*P(1, 5) + normal(2, 1)*P(2, 5) + normal(3, 1)*P(3, 5))
    2279              : 
    2280      2679844 :                IF (point_is_in_quadrilateral(P(:, 5), P(:, 7), P(:, 8), P(:, 6), point_in_plane)) THEN
    2281        51796 :                   distance(idx) = ABS(normal(1, 1)*P(1, 5) + normal(2, 1)*P(2, 5) + normal(3, 1)*P(3, 5))
    2282              :                ELSE
    2283      2628048 :                   distance(idx) = HUGE(distance(idx))
    2284              :                END IF
    2285              : 
    2286              :                ! Face D (3784) 3 is the reference
    2287      2679844 :                idx = idx + 1
    2288     10719376 :                plane_vector(:, 1) = P(:, 7) - P(:, 3)
    2289     10719376 :                plane_vector(:, 2) = P(:, 4) - P(:, 3)
    2290      2679844 :                cross_product(1) = plane_vector(2, 1)*plane_vector(3, 2) - plane_vector(3, 1)*plane_vector(2, 2)
    2291      2679844 :                cross_product(2) = plane_vector(3, 1)*plane_vector(1, 2) - plane_vector(1, 1)*plane_vector(3, 2)
    2292      2679844 :                cross_product(3) = plane_vector(1, 1)*plane_vector(2, 2) - plane_vector(2, 1)*plane_vector(1, 2)
    2293     18758908 :                normal(:, 1) = cross_product/SQRT(SUM(cross_product**2))
    2294     10719376 :                point_in_plane = -normal(:, 1)*(normal(1, 1)*P(1, 3) + normal(2, 1)*P(2, 3) + normal(3, 1)*P(3, 3))
    2295              : 
    2296      2679844 :                IF (point_is_in_quadrilateral(P(:, 3), P(:, 7), P(:, 8), P(:, 4), point_in_plane)) THEN
    2297        51972 :                   distance(idx) = ABS(normal(1, 1)*P(1, 3) + normal(2, 1)*P(2, 3) + normal(3, 1)*P(3, 3))
    2298              :                ELSE
    2299      2627872 :                   distance(idx) = HUGE(distance(idx))
    2300              :                END IF
    2301              : 
    2302              :                ! Face E (2684) 2 is the reference
    2303      2679844 :                idx = idx + 1
    2304     10719376 :                plane_vector(:, 1) = P(:, 6) - P(:, 2)
    2305     10719376 :                plane_vector(:, 2) = P(:, 4) - P(:, 2)
    2306      2679844 :                cross_product(1) = plane_vector(2, 1)*plane_vector(3, 2) - plane_vector(3, 1)*plane_vector(2, 2)
    2307      2679844 :                cross_product(2) = plane_vector(3, 1)*plane_vector(1, 2) - plane_vector(1, 1)*plane_vector(3, 2)
    2308      2679844 :                cross_product(3) = plane_vector(1, 1)*plane_vector(2, 2) - plane_vector(2, 1)*plane_vector(1, 2)
    2309     18758908 :                normal(:, 1) = cross_product/SQRT(SUM(cross_product**2))
    2310     10719376 :                point_in_plane = -normal(:, 1)*(normal(1, 1)*P(1, 2) + normal(2, 1)*P(2, 2) + normal(3, 1)*P(3, 2))
    2311              : 
    2312      2679844 :                IF (point_is_in_quadrilateral(P(:, 2), P(:, 6), P(:, 8), P(:, 4), point_in_plane)) THEN
    2313        51776 :                   distance(idx) = ABS(normal(1, 1)*P(1, 2) + normal(2, 1)*P(2, 2) + normal(3, 1)*P(3, 2))
    2314              :                ELSE
    2315      2628068 :                   distance(idx) = HUGE(distance(idx))
    2316              :                END IF
    2317              : 
    2318              :                ! Face F (1573) 1 is the reference
    2319      2679844 :                idx = idx + 1
    2320     10719376 :                plane_vector(:, 1) = P(:, 5) - P(:, 1)
    2321     10719376 :                plane_vector(:, 2) = P(:, 3) - P(:, 1)
    2322      2679844 :                cross_product(1) = plane_vector(2, 1)*plane_vector(3, 2) - plane_vector(3, 1)*plane_vector(2, 2)
    2323      2679844 :                cross_product(2) = plane_vector(3, 1)*plane_vector(1, 2) - plane_vector(1, 1)*plane_vector(3, 2)
    2324      2679844 :                cross_product(3) = plane_vector(1, 1)*plane_vector(2, 2) - plane_vector(2, 1)*plane_vector(1, 2)
    2325     18758908 :                normal(:, 1) = cross_product/SQRT(SUM(cross_product**2))
    2326     10719376 :                point_in_plane = -normal(:, 1)*(normal(1, 1)*P(1, 1) + normal(2, 1)*P(2, 1) + normal(3, 1)*P(3, 1))
    2327              : 
    2328      2679844 :                IF (point_is_in_quadrilateral(P(:, 1), P(:, 5), P(:, 7), P(:, 3), point_in_plane)) THEN
    2329        51776 :                   distance(idx) = ABS(normal(1, 1)*P(1, 1) + normal(2, 1)*P(2, 1) + normal(3, 1)*P(3, 1))
    2330              :                ELSE
    2331      2628068 :                   distance(idx) = HUGE(distance(idx))
    2332              :                END IF
    2333              : 
    2334     42877504 :                dist_min = MINVAL(distance)
    2335      2679844 :                IF (max_shell == 0) THEN
    2336         7152 :                   image_cell_found = .TRUE.
    2337              :                END IF
    2338      3332436 :                IF (dist_min < R_max) THEN
    2339       631076 :                   total_number_of_cells = total_number_of_cells + 1
    2340      2524304 :                   x_data%neighbor_cells(ub)%cell = REAL([ishell, jshell, kshell], dp)
    2341       631076 :                   ub = ub + 1
    2342       631076 :                   image_cell_found = .TRUE.
    2343              :                END IF
    2344              : 
    2345              :             END DO
    2346              :             END DO
    2347              :             END DO
    2348        36212 :             IF (image_cell_found) THEN
    2349        21908 :                max_shell = max_shell + 1
    2350              :             ELSE
    2351              :                nothing_more_to_add = .TRUE.
    2352              :             END IF
    2353              :          END DO
    2354              :          ! now remove what is not needed
    2355       695444 :          ALLOCATE (tmp_neighbor_cells(total_number_of_cells))
    2356       638228 :          DO i = 1, ub - 1
    2357       638228 :             tmp_neighbor_cells(i) = x_data%neighbor_cells(i)
    2358              :          END DO
    2359         7152 :          DEALLOCATE (x_data%neighbor_cells)
    2360              :          ! If we only need the supercell, total_number_of_cells is still 0, repair
    2361         7152 :          IF (total_number_of_cells == 0) THEN
    2362            0 :             total_number_of_cells = 1
    2363            0 :             ALLOCATE (x_data%neighbor_cells(total_number_of_cells))
    2364            0 :             DO i = 1, total_number_of_cells
    2365            0 :                x_data%neighbor_cells(i)%cell = 0.0_dp
    2366            0 :                x_data%neighbor_cells(i)%cell_r = 0.0_dp
    2367              :             END DO
    2368              :          ELSE
    2369       688292 :             ALLOCATE (x_data%neighbor_cells(total_number_of_cells))
    2370       638228 :             DO i = 1, total_number_of_cells
    2371       638228 :                x_data%neighbor_cells(i) = tmp_neighbor_cells(i)
    2372              :             END DO
    2373              :          END IF
    2374         7152 :          DEALLOCATE (tmp_neighbor_cells)
    2375              : 
    2376         7152 :          IF (x_data%periodic_parameter%number_of_shells == do_hfx_auto_shells) THEN
    2377              :             ! Do nothing
    2378              :          ELSE
    2379           60 :             total_number_of_cells = 0
    2380          206 :             DO i = 0, x_data%periodic_parameter%number_of_shells
    2381          206 :                total_number_of_cells = total_number_of_cells + count_cells_perd(i, x_data%periodic_parameter%perd)
    2382              :             END DO
    2383           60 :             IF (total_number_of_cells < SIZE(x_data%neighbor_cells)) THEN
    2384           60 :                IF (i_thread == 1) THEN
    2385            4 :                   WRITE (char_nshells, '(I3)') SIZE(x_data%neighbor_cells)
    2386              :                   WRITE (error_msg, '(A,A,A)') "Periodic Hartree Fock calculation requested with use "// &
    2387              :                      "of a truncated potential. The number of shells to be considered "// &
    2388              :                      "might be too small. CP2K conservatively estimates to need "//TRIM(char_nshells)//" periodic images "// &
    2389            4 :                      "Please carefully check if you get converged results."
    2390            4 :                   CPWARN(error_msg)
    2391              :                END IF
    2392              :             END IF
    2393           60 :             total_number_of_cells = 0
    2394          206 :             DO i = 0, x_data%periodic_parameter%number_of_shells
    2395          206 :                total_number_of_cells = total_number_of_cells + count_cells_perd(i, x_data%periodic_parameter%perd)
    2396              :             END DO
    2397           60 :             DEALLOCATE (x_data%neighbor_cells)
    2398              : 
    2399         1272 :             ALLOCATE (x_data%neighbor_cells(total_number_of_cells))
    2400           60 :             m = 0
    2401           60 :             i = 1
    2402         3168 :             DO WHILE (SUM(m**2) <= x_data%periodic_parameter%number_of_shells)
    2403         2928 :                x_data%neighbor_cells(i)%cell = REAL(m, dp)
    2404          732 :                CALL next_image_cell_perd(m, x_data%periodic_parameter%perd)
    2405          732 :                i = i + 1
    2406              :             END DO
    2407              :          END IF
    2408              :       CASE DEFAULT
    2409         2525 :          total_number_of_cells = 0
    2410         2525 :          IF (pbc_shells == -1) pbc_shells = 0
    2411         5050 :          DO i = 0, pbc_shells
    2412         5050 :             total_number_of_cells = total_number_of_cells + count_cells_perd(i, x_data%periodic_parameter%perd)
    2413              :          END DO
    2414         2525 :          DEALLOCATE (x_data%neighbor_cells)
    2415              : 
    2416        25250 :          ALLOCATE (x_data%neighbor_cells(total_number_of_cells))
    2417              : 
    2418         2525 :          m = 0
    2419         2525 :          i = 1
    2420        29877 :          DO WHILE (SUM(m**2) <= pbc_shells)
    2421        10100 :             x_data%neighbor_cells(i)%cell = REAL(m, dp)
    2422         2525 :             CALL next_image_cell_perd(m, x_data%periodic_parameter%perd)
    2423         5050 :             i = i + 1
    2424              :          END DO
    2425              :       END SELECT
    2426              : 
    2427              :       ! ** Transform into real coord
    2428       639150 :       DO i = 1, SIZE(x_data%neighbor_cells)
    2429              :          r = 0.0_dp
    2430      2517892 :          x_data%neighbor_cells(i)%cell_r(:) = 0.0_dp
    2431      2517892 :          s = x_data%neighbor_cells(i)%cell(:)
    2432       639150 :          CALL scaled_to_real(x_data%neighbor_cells(i)%cell_r, s, cell)
    2433              :       END DO
    2434         9677 :       x_data%periodic_parameter%number_of_shells = pbc_shells
    2435              : 
    2436         9677 :       R_max_stress = 0.0_dp
    2437       639150 :       DO i = 1, SIZE(x_data%neighbor_cells)
    2438      3157042 :          R_max_stress = MAX(R_max_stress, MAXVAL(ABS(x_data%neighbor_cells(i)%cell_r(:))))
    2439              :       END DO
    2440       125801 :       R_max_stress = R_max_stress + ABS(MAXVAL(cell%hmat(:, :)))
    2441         9677 :       x_data%periodic_parameter%R_max_stress = R_max_stress
    2442              : 
    2443         9677 :    END SUBROUTINE hfx_create_neighbor_cells
    2444              : 
    2445              :    ! performs a fuzzy check of being in a quadrilateral
    2446              : ! **************************************************************************************************
    2447              : !> \brief ...
    2448              : !> \param A ...
    2449              : !> \param B ...
    2450              : !> \param C ...
    2451              : !> \param D ...
    2452              : !> \param P ...
    2453              : !> \return ...
    2454              : ! **************************************************************************************************
    2455     16079064 :    FUNCTION point_is_in_quadrilateral(A, B, C, D, P)
    2456              :       REAL(dp)                                           :: A(3), B(3), C(3), D(3), P(3)
    2457              :       LOGICAL                                            :: point_is_in_quadrilateral
    2458              : 
    2459              :       REAL(dp), PARAMETER :: fuzzy = 1000.0_dp*EPSILON(1.0_dp)
    2460              : 
    2461              :       REAL(dp)                                           :: dot00, dot01, dot02, dot11, dot12, &
    2462              :                                                             invDenom, u, v, v0(3), v1(3), v2(3)
    2463              : 
    2464     16079064 :       point_is_in_quadrilateral = .FALSE.
    2465              : 
    2466              :       ! ** Check for both triangles ABC and ACD
    2467              :       ! **
    2468              :       ! **     D -------------- C
    2469              :       ! **    /                /
    2470              :       ! **   /                /
    2471              :       ! **  A----------------B
    2472              :       ! **
    2473              :       ! **
    2474              :       ! **
    2475              : 
    2476              :       ! ** ABC
    2477              : 
    2478     64316256 :       v0 = D - A
    2479     64316256 :       v1 = C - A
    2480     64316256 :       v2 = P - A
    2481              : 
    2482              :       ! ** Compute dot products
    2483     64316256 :       dot00 = DOT_PRODUCT(v0, v0)
    2484     64316256 :       dot01 = DOT_PRODUCT(v0, v1)
    2485     64316256 :       dot02 = DOT_PRODUCT(v0, v2)
    2486     64316256 :       dot11 = DOT_PRODUCT(v1, v1)
    2487     64316256 :       dot12 = DOT_PRODUCT(v1, v2)
    2488              : 
    2489              :       ! ** Compute barycentric coordinates
    2490     16079064 :       invDenom = 1/(dot00*dot11 - dot01*dot01)
    2491     16079064 :       u = (dot11*dot02 - dot01*dot12)*invDenom
    2492     16079064 :       v = (dot00*dot12 - dot01*dot02)*invDenom
    2493              :       ! ** Check if point is in triangle
    2494     16079064 :       IF ((u >= 0 - fuzzy) .AND. (v >= 0 - fuzzy) .AND. (u + v <= 1 + fuzzy)) THEN
    2495     16079064 :          point_is_in_quadrilateral = .TRUE.
    2496              :          RETURN
    2497              :       END IF
    2498     63092592 :       v0 = C - A
    2499     63092592 :       v1 = B - A
    2500     63092592 :       v2 = P - A
    2501              : 
    2502              :       ! ** Compute dot products
    2503     63092592 :       dot00 = DOT_PRODUCT(v0, v0)
    2504     63092592 :       dot01 = DOT_PRODUCT(v0, v1)
    2505     63092592 :       dot02 = DOT_PRODUCT(v0, v2)
    2506     63092592 :       dot11 = DOT_PRODUCT(v1, v1)
    2507     63092592 :       dot12 = DOT_PRODUCT(v1, v2)
    2508              : 
    2509              :       ! ** Compute barycentric coordinates
    2510     15773148 :       invDenom = 1/(dot00*dot11 - dot01*dot01)
    2511     15773148 :       u = (dot11*dot02 - dot01*dot12)*invDenom
    2512     15773148 :       v = (dot00*dot12 - dot01*dot02)*invDenom
    2513              : 
    2514              :       ! ** Check if point is in triangle
    2515     15773148 :       IF ((u >= 0 - fuzzy) .AND. (v >= 0 - fuzzy) .AND. (u + v <= 1 + fuzzy)) THEN
    2516         5172 :          point_is_in_quadrilateral = .TRUE.
    2517         5172 :          RETURN
    2518              :       END IF
    2519              : 
    2520              :    END FUNCTION point_is_in_quadrilateral
    2521              : 
    2522              : ! **************************************************************************************************
    2523              : !> \brief - This routine deletes all list entries in a container in order to
    2524              : !>        deallocate the memory.
    2525              : !> \param container container that contains the compressed elements
    2526              : !> \param memory_usage ...
    2527              : !> \param do_disk_storage ...
    2528              : !> \par History
    2529              : !>      10.2007 created [Manuel Guidon]
    2530              : !> \author Manuel Guidon
    2531              : ! **************************************************************************************************
    2532      3780650 :    SUBROUTINE hfx_init_container(container, memory_usage, do_disk_storage)
    2533              :       TYPE(hfx_container_type)                           :: container
    2534              :       INTEGER                                            :: memory_usage
    2535              :       LOGICAL                                            :: do_disk_storage
    2536              : 
    2537              :       TYPE(hfx_container_node), POINTER                  :: current, next
    2538              : 
    2539              : !! DEALLOCATE memory
    2540              : 
    2541      3780650 :       current => container%first
    2542      7737448 :       DO WHILE (ASSOCIATED(current))
    2543      3956798 :          next => current%next
    2544      3956798 :          DEALLOCATE (current)
    2545      3956798 :          current => next
    2546              :       END DO
    2547              : 
    2548              :       !! Allocate first list entry, init members
    2549   3878946900 :       ALLOCATE (container%first)
    2550              :       container%first%prev => NULL()
    2551              :       container%first%next => NULL()
    2552      3780650 :       container%current => container%first
    2553   3875166250 :       container%current%data = 0
    2554      3780650 :       container%element_counter = 1
    2555      3780650 :       memory_usage = 1
    2556              : 
    2557      3780650 :       IF (do_disk_storage) THEN
    2558              :          !! close the file, if this is no the first time
    2559          390 :          IF (container%unit /= -1) THEN
    2560            0 :             CALL close_file(unit_number=container%unit)
    2561              :          END IF
    2562              :          CALL open_file(file_name=TRIM(container%filename), file_status="UNKNOWN", file_form="UNFORMATTED", file_action="WRITE", &
    2563          390 :                         unit_number=container%unit)
    2564              :       END IF
    2565              : 
    2566      3780650 :    END SUBROUTINE hfx_init_container
    2567              : 
    2568              : ! **************************************************************************************************
    2569              : !> \brief - This routine stores the data obtained from the load balance routine
    2570              : !>        for the energy
    2571              : !> \param ptr_to_distr contains data to store
    2572              : !> \param x_data contains all relevant data structures for hfx runs
    2573              : !> \par History
    2574              : !>      09.2007 created [Manuel Guidon]
    2575              : !> \author Manuel Guidon
    2576              : ! **************************************************************************************************
    2577         2188 :    SUBROUTINE hfx_set_distr_energy(ptr_to_distr, x_data)
    2578              :       TYPE(hfx_distribution), DIMENSION(:), POINTER      :: ptr_to_distr
    2579              :       TYPE(hfx_type), POINTER                            :: x_data
    2580              : 
    2581         2188 :       DEALLOCATE (x_data%distribution_energy)
    2582              : 
    2583       146470 :       ALLOCATE (x_data%distribution_energy(SIZE(ptr_to_distr)))
    2584       284188 :       x_data%distribution_energy = ptr_to_distr
    2585              : 
    2586         2188 :    END SUBROUTINE hfx_set_distr_energy
    2587              : 
    2588              : ! **************************************************************************************************
    2589              : !> \brief - This routine stores the data obtained from the load balance routine
    2590              : !>        for the forces
    2591              : !> \param ptr_to_distr contains data to store
    2592              : !> \param x_data contains all relevant data structures for hfx runs
    2593              : !> \par History
    2594              : !>      09.2007 created [Manuel Guidon]
    2595              : !> \author Manuel Guidon
    2596              : ! **************************************************************************************************
    2597         1376 :    SUBROUTINE hfx_set_distr_forces(ptr_to_distr, x_data)
    2598              :       TYPE(hfx_distribution), DIMENSION(:), POINTER      :: ptr_to_distr
    2599              :       TYPE(hfx_type), POINTER                            :: x_data
    2600              : 
    2601         1376 :       DEALLOCATE (x_data%distribution_forces)
    2602              : 
    2603        92186 :       ALLOCATE (x_data%distribution_forces(SIZE(ptr_to_distr)))
    2604       178880 :       x_data%distribution_forces = ptr_to_distr
    2605              : 
    2606         1376 :    END SUBROUTINE hfx_set_distr_forces
    2607              : 
    2608              : ! **************************************************************************************************
    2609              : !> \brief - resets the maximum memory usage for a HFX calculation subtracting
    2610              : !>          all relevant buffers from the input MAX_MEM value and add 10% of
    2611              : !>          safety margin
    2612              : !> \param memory_parameter Memory information
    2613              : !> \param subtr_size_mb size of buffers in MiB
    2614              : !> \par History
    2615              : !>      02.2009 created [Manuel Guidon]
    2616              : !> \author Manuel Guidon
    2617              : ! **************************************************************************************************
    2618        36293 :    SUBROUTINE hfx_reset_memory_usage_counter(memory_parameter, subtr_size_mb)
    2619              : 
    2620              :       TYPE(hfx_memory_type)                              :: memory_parameter
    2621              :       INTEGER(int_8), INTENT(IN)                         :: subtr_size_mb
    2622              : 
    2623              :       INTEGER(int_8)                                     :: max_memory
    2624              : 
    2625        36293 :       max_memory = memory_parameter%max_memory
    2626        36293 :       max_memory = max_memory - subtr_size_mb
    2627        36293 :       IF (max_memory <= 0) THEN
    2628           38 :          memory_parameter%do_all_on_the_fly = .TRUE.
    2629           38 :          memory_parameter%max_compression_counter = 0
    2630              :       ELSE
    2631        36255 :          memory_parameter%do_all_on_the_fly = .FALSE.
    2632        36255 :          memory_parameter%max_compression_counter = max_memory*1024_int_8*128_int_8
    2633              :       END IF
    2634        36293 :    END SUBROUTINE hfx_reset_memory_usage_counter
    2635              : 
    2636              : ! **************************************************************************************************
    2637              : !> \brief - This routine prints some information on HFX
    2638              : !> \param x_data contains all relevant data structures for hfx runs
    2639              : !> \param hfx_section HFX input section
    2640              : !> \par History
    2641              : !>      03.2008 created [Manuel Guidon]
    2642              : !> \author Manuel Guidon
    2643              : ! **************************************************************************************************
    2644         1244 :    SUBROUTINE hfx_print_std_info(x_data, hfx_section)
    2645              :       TYPE(hfx_type), POINTER                            :: x_data
    2646              :       TYPE(section_vals_type), POINTER                   :: hfx_section
    2647              : 
    2648              :       INTEGER                                            :: iw
    2649              :       TYPE(cp_logger_type), POINTER                      :: logger
    2650              : 
    2651         1244 :       NULLIFY (logger)
    2652         1244 :       logger => cp_get_default_logger()
    2653              : 
    2654              :       iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
    2655         1244 :                                 extension=".scfLog")
    2656              : 
    2657         1244 :       IF (iw > 0) THEN
    2658              :          WRITE (UNIT=iw, FMT="((T3,A,T73,ES8.1))") &
    2659          309 :             "HFX_INFO| EPS_SCHWARZ:     ", x_data%screening_parameter%eps_schwarz
    2660              :          WRITE (UNIT=iw, FMT="((T3,A,T73,ES8.1))") &
    2661          309 :             "HFX_INFO| EPS_SCHWARZ_FORCES     ", x_data%screening_parameter%eps_schwarz_forces
    2662              :          WRITE (UNIT=iw, FMT="((T3,A,T73,ES8.1))") &
    2663          309 :             "HFX_INFO| EPS_STORAGE_SCALING:     ", x_data%memory_parameter%eps_storage_scaling
    2664              :          WRITE (UNIT=iw, FMT="((T3,A,T61,I20))") &
    2665          309 :             "HFX_INFO| NBINS:     ", x_data%load_balance_parameter%nbins
    2666              :          WRITE (UNIT=iw, FMT="((T3,A,T61,I20))") &
    2667          309 :             "HFX_INFO| BLOCK_SIZE:     ", x_data%load_balance_parameter%block_size
    2668          309 :          IF (x_data%periodic_parameter%do_periodic) THEN
    2669           94 :             IF (x_data%periodic_parameter%mode == -1) THEN
    2670              :                WRITE (UNIT=iw, FMT="((T3,A,T77,A))") &
    2671           92 :                   "HFX_INFO| NUMBER_OF_SHELLS:     ", "AUTO"
    2672              :             ELSE
    2673              :                WRITE (UNIT=iw, FMT="((T3,A,T61,I20))") &
    2674            2 :                   "HFX_INFO| NUMBER_OF_SHELLS:     ", x_data%periodic_parameter%mode
    2675              :             END IF
    2676              :             WRITE (UNIT=iw, FMT="((T3,A,T61,I20))") &
    2677           94 :                "HFX_INFO| Number of periodic shells considered:     ", x_data%periodic_parameter%number_of_shells
    2678              :             WRITE (UNIT=iw, FMT="((T3,A,T61,I20),/)") &
    2679           94 :                "HFX_INFO| Number of periodic cells considered:     ", SIZE(x_data%neighbor_cells)
    2680              :          ELSE
    2681              :             WRITE (UNIT=iw, FMT="((T3,A,T77,A))") &
    2682          215 :                "HFX_INFO| Number of periodic shells considered:     ", "NONE"
    2683              :             WRITE (UNIT=iw, FMT="((T3,A,T77,A),/)") &
    2684          215 :                "HFX_INFO| Number of periodic cells considered:     ", "NONE"
    2685              :          END IF
    2686              :       END IF
    2687         1244 :    END SUBROUTINE hfx_print_std_info
    2688              : 
    2689              : ! **************************************************************************************************
    2690              : !> \brief ...
    2691              : !> \param ri_data ...
    2692              : !> \param hfx_section ...
    2693              : ! **************************************************************************************************
    2694          108 :    SUBROUTINE hfx_print_ri_info(ri_data, hfx_section)
    2695              :       TYPE(hfx_ri_type), POINTER                         :: ri_data
    2696              :       TYPE(section_vals_type), POINTER                   :: hfx_section
    2697              : 
    2698              :       INTEGER                                            :: iw
    2699              :       REAL(dp)                                           :: rc_ang
    2700              :       TYPE(cp_logger_type), POINTER                      :: logger
    2701              :       TYPE(section_vals_type), POINTER                   :: ri_section
    2702              : 
    2703          108 :       NULLIFY (logger, ri_section)
    2704          108 :       logger => cp_get_default_logger()
    2705              : 
    2706          108 :       ri_section => ri_data%ri_section
    2707              : 
    2708              :       iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
    2709          108 :                                 extension=".scfLog")
    2710              : 
    2711          108 :       IF (iw > 0) THEN
    2712              : 
    2713              :          ASSOCIATE (ri_metric => ri_data%ri_metric, hfx_pot => ri_data%hfx_pot)
    2714           59 :             SELECT CASE (ri_metric%potential_type)
    2715              :             CASE (do_potential_coulomb)
    2716              :                WRITE (UNIT=iw, FMT="(/T3,A,T74,A)") &
    2717           11 :                   "HFX_RI_INFO| RI metric: ", "COULOMB"
    2718              :             CASE (do_potential_short)
    2719              :                WRITE (UNIT=iw, FMT="(T3,A,T71,A)") &
    2720            1 :                   "HFX_RI_INFO| RI metric: ", "SHORTRANGE"
    2721              :                WRITE (iw, '(T3,A,T61,F20.10)') &
    2722            1 :                   "HFX_RI_INFO| Omega:     ", ri_metric%omega
    2723            1 :                rc_ang = cp_unit_from_cp2k(ri_metric%cutoff_radius, "angstrom")
    2724              :                WRITE (iw, '(T3,A,T61,F20.10)') &
    2725            1 :                   "HFX_RI_INFO| Cutoff Radius [angstrom]:     ", rc_ang
    2726              :             CASE (do_potential_long)
    2727              :                WRITE (UNIT=iw, FMT="(T3,A,T72,A)") &
    2728            0 :                   "HFX_RI_INFO| RI metric: ", "LONGRANGE"
    2729              :                WRITE (iw, '(T3,A,T61,F20.10)') &
    2730            0 :                   "HFX_RI_INFO| Omega:     ", ri_metric%omega
    2731              :             CASE (do_potential_id)
    2732              :                WRITE (UNIT=iw, FMT="(T3,A,T74,A)") &
    2733           30 :                   "HFX_RI_INFO| RI metric: ", "OVERLAP"
    2734              :             CASE (do_potential_truncated)
    2735              :                WRITE (UNIT=iw, FMT="(T3,A,T64,A)") &
    2736            5 :                   "HFX_RI_INFO| RI metric: ", "TRUNCATED COULOMB"
    2737            5 :                rc_ang = cp_unit_from_cp2k(ri_metric%cutoff_radius, "angstrom")
    2738              :                WRITE (iw, '(T3,A,T61,F20.10)') &
    2739           53 :                   "HFX_RI_INFO| Cutoff Radius [angstrom]:     ", rc_ang
    2740              :             END SELECT
    2741              : 
    2742              :          END ASSOCIATE
    2743           51 :          SELECT CASE (ri_data%flavor)
    2744              :          CASE (ri_mo)
    2745              :             WRITE (UNIT=iw, FMT="(T3, A, T79, A)") &
    2746            3 :                "HFX_RI_INFO| RI flavor: ", "MO"
    2747              :          CASE (ri_pmat)
    2748              :             WRITE (UNIT=iw, FMT="(T3, A, T78, A)") &
    2749           48 :                "HFX_RI_INFO| RI flavor: ", "RHO"
    2750              :          END SELECT
    2751           48 :          SELECT CASE (ri_data%t2c_method)
    2752              :          CASE (hfx_ri_do_2c_iter)
    2753              :             WRITE (UNIT=iw, FMT="(T3, A, T69, A)") &
    2754            0 :                "HFX_RI_INFO| Matrix SQRT/INV", "DBCSR / iter"
    2755              :          CASE (hfx_ri_do_2c_diag)
    2756              :             WRITE (UNIT=iw, FMT="(T3, A, T65, A)") &
    2757           48 :                "HFX_RI_INFO| Matrix SQRT/INV", "Dense / diag"
    2758              :          END SELECT
    2759              :          WRITE (UNIT=iw, FMT="(T3, A, T73, ES8.1)") &
    2760           48 :             "HFX_RI_INFO| EPS_FILTER", ri_data%filter_eps
    2761              :          WRITE (UNIT=iw, FMT="(T3, A, T73, ES8.1)") &
    2762           48 :             "HFX_RI_INFO| EPS_FILTER 2-center", ri_data%filter_eps_2c
    2763              :          WRITE (UNIT=iw, FMT="(T3, A, T73, ES8.1)") &
    2764           48 :             "HFX_RI_INFO| EPS_FILTER storage", ri_data%filter_eps_storage
    2765              :          WRITE (UNIT=iw, FMT="(T3, A, T73, ES8.1)") &
    2766           48 :             "HFX_RI_INFO| EPS_FILTER MO", ri_data%filter_eps_mo
    2767              :          WRITE (UNIT=iw, FMT="(T3, A, T73, ES8.1)") &
    2768           48 :             "HFX_RI_INFO| EPS_PGF_ORB", ri_data%eps_pgf_orb
    2769              :          WRITE (UNIT=iw, FMT="((T3, A, T73, ES8.1))") &
    2770           48 :             "HFX_RI_INFO| EPS_SCHWARZ:     ", ri_data%eps_schwarz
    2771              :          WRITE (UNIT=iw, FMT="((T3, A, T73, ES8.1))") &
    2772           48 :             "HFX_RI_INFO| EPS_SCHWARZ_FORCES:     ", ri_data%eps_schwarz_forces
    2773              :          WRITE (UNIT=iw, FMT="(T3, A, T78, I3)") &
    2774           48 :             "HFX_RI_INFO| Minimum block size", ri_data%min_bsize
    2775              :          WRITE (UNIT=iw, FMT="(T3, A, T78, I3)") &
    2776           48 :             "HFX_RI_INFO| MO block size", ri_data%max_bsize_MO
    2777              :          WRITE (UNIT=iw, FMT="(T3, A, T79, I2)") &
    2778           48 :             "HFX_RI_INFO| Memory reduction factor", ri_data%n_mem_input
    2779              :       END IF
    2780              : 
    2781          108 :    END SUBROUTINE hfx_print_ri_info
    2782              : 
    2783              : ! **************************************************************************************************
    2784              : !> \brief ...
    2785              : !> \param x_data ...
    2786              : !> \param hfx_section ...
    2787              : !> \param i_rep ...
    2788              : ! **************************************************************************************************
    2789         1352 :    SUBROUTINE hfx_print_info(x_data, hfx_section, i_rep)
    2790              :       TYPE(hfx_type), POINTER                            :: x_data
    2791              :       TYPE(section_vals_type), POINTER                   :: hfx_section
    2792              :       INTEGER, INTENT(IN)                                :: i_rep
    2793              : 
    2794              :       INTEGER                                            :: iw
    2795              :       REAL(dp)                                           :: rc_ang
    2796              :       TYPE(cp_logger_type), POINTER                      :: logger
    2797              : 
    2798         1352 :       NULLIFY (logger)
    2799         1352 :       logger => cp_get_default_logger()
    2800              : 
    2801              :       iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
    2802         1352 :                                 extension=".scfLog")
    2803              : 
    2804         1352 :       IF (iw > 0) THEN
    2805              :          WRITE (UNIT=iw, FMT="(/,(T3,A,T61,I20))") &
    2806          357 :             "HFX_INFO| Replica ID:     ", i_rep
    2807              : 
    2808              :          WRITE (iw, '(T3,A,T61,F20.10)') &
    2809          357 :             "HFX_INFO| FRACTION:     ", x_data%general_parameter%fraction
    2810          575 :          SELECT CASE (x_data%potential_parameter%potential_type)
    2811              :          CASE (do_potential_coulomb)
    2812              :             WRITE (UNIT=iw, FMT="((T3,A,T74,A))") &
    2813          218 :                "HFX_INFO| Interaction Potential:     ", "COULOMB"
    2814              :          CASE (do_potential_short)
    2815              :             WRITE (UNIT=iw, FMT="((T3,A,T71,A))") &
    2816           12 :                "HFX_INFO| Interaction Potential:    ", "SHORTRANGE"
    2817              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2818           12 :                "HFX_INFO| Omega:     ", x_data%potential_parameter%omega
    2819           12 :             rc_ang = cp_unit_from_cp2k(x_data%potential_parameter%cutoff_radius, "angstrom")
    2820              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2821           12 :                "HFX_INFO| Cutoff Radius [angstrom]:     ", rc_ang
    2822              :          CASE (do_potential_long)
    2823              :             WRITE (UNIT=iw, FMT="((T3,A,T72,A))") &
    2824            4 :                "HFX_INFO| Interaction Potential:     ", "LONGRANGE"
    2825              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2826            4 :                "HFX_INFO| Omega:     ", x_data%potential_parameter%omega
    2827              :          CASE (do_potential_mix_cl)
    2828              :             WRITE (UNIT=iw, FMT="((T3,A,T75,A))") &
    2829            7 :                "HFX_INFO| Interaction Potential:     ", "MIX_CL"
    2830              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2831            7 :                "HFX_INFO| Omega:     ", x_data%potential_parameter%omega
    2832              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2833            7 :                "HFX_INFO| SCALE_COULOMB:     ", x_data%potential_parameter%scale_coulomb
    2834              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2835            7 :                "HFX_INFO| SCALE_LONGRANGE:     ", x_data%potential_parameter%scale_longrange
    2836              :          CASE (do_potential_gaussian)
    2837              :             WRITE (UNIT=iw, FMT="((T3,A,T73,A))") &
    2838            0 :                "HFX_INFO| Interaction Potential:     ", "GAUSSIAN"
    2839              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2840            0 :                "HFX_INFO| Omega:     ", x_data%potential_parameter%omega
    2841              :          CASE (do_potential_mix_lg)
    2842              :             WRITE (UNIT=iw, FMT="((T3,A,T75,A))") &
    2843            2 :                "HFX_INFO| Interaction Potential:    ", "MIX_LG"
    2844              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2845            2 :                "HFX_INFO| Omega:     ", x_data%potential_parameter%omega
    2846              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2847            2 :                "HFX_INFO| SCALE_LONGRANGE:     ", x_data%potential_parameter%scale_longrange
    2848              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2849            2 :                "HFX_INFO| SCALE_GAUSSIAN:    ", x_data%potential_parameter%scale_gaussian
    2850              :          CASE (do_potential_id)
    2851              :             WRITE (UNIT=iw, FMT="((T3,A,T73,A))") &
    2852           11 :                "HFX_INFO| Interaction Potential:    ", "IDENTITY"
    2853              :          CASE (do_potential_truncated)
    2854              :             WRITE (UNIT=iw, FMT="((T3,A,T72,A))") &
    2855           94 :                "HFX_INFO| Interaction Potential:    ", "TRUNCATED"
    2856           94 :             rc_ang = cp_unit_from_cp2k(x_data%potential_parameter%cutoff_radius, "angstrom")
    2857              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2858           94 :                "HFX_INFO| Cutoff Radius [angstrom]:     ", rc_ang
    2859              :          CASE (do_potential_mix_cl_trunc)
    2860              :             WRITE (UNIT=iw, FMT="((T3,A,T65,A))") &
    2861            9 :                "HFX_INFO| Interaction Potential:    ", "TRUNCATED MIX_CL"
    2862            9 :             rc_ang = cp_unit_from_cp2k(x_data%potential_parameter%cutoff_radius, "angstrom")
    2863              :             WRITE (iw, '(T3,A,T61,F20.10)') &
    2864          366 :                "HFX_INFO| Cutoff Radius [angstrom]:     ", rc_ang
    2865              :          END SELECT
    2866              : 
    2867              :       END IF
    2868         1352 :       IF (x_data%do_hfx_ri) THEN
    2869          108 :          CALL hfx_print_ri_info(x_data%ri_data, hfx_section)
    2870              :       ELSE
    2871         1244 :          CALL hfx_print_std_info(x_data, hfx_section)
    2872              :       END IF
    2873              : 
    2874              :       CALL cp_print_key_finished_output(iw, logger, hfx_section, &
    2875         1352 :                                         "HF_INFO")
    2876         1352 :    END SUBROUTINE hfx_print_info
    2877              : 
    2878              : ! **************************************************************************************************
    2879              : !> \brief ...
    2880              : !> \param DATA ...
    2881              : !> \param memory_usage ...
    2882              : ! **************************************************************************************************
    2883        32154 :    SUBROUTINE dealloc_containers(DATA, memory_usage)
    2884              :       TYPE(hfx_compression_type)                         :: data
    2885              :       INTEGER                                            :: memory_usage
    2886              : 
    2887              :       INTEGER                                            :: bin, i
    2888              : 
    2889        64308 :       DO bin = 1, SIZE(data%maxval_container)
    2890              :          CALL hfx_init_container(data%maxval_container(bin), memory_usage, &
    2891        32154 :                                  .FALSE.)
    2892        64308 :          DEALLOCATE (data%maxval_container(bin)%first)
    2893              :       END DO
    2894        32154 :       DEALLOCATE (data%maxval_container)
    2895        32154 :       DEALLOCATE (data%maxval_cache)
    2896              : 
    2897        64308 :       DO bin = 1, SIZE(data%integral_containers, 2)
    2898      2122164 :          DO i = 1, 64
    2899              :             CALL hfx_init_container(data%integral_containers(i, bin), memory_usage, &
    2900      2057856 :                                     .FALSE.)
    2901      2090010 :             DEALLOCATE (data%integral_containers(i, bin)%first)
    2902              :          END DO
    2903              :       END DO
    2904        32154 :       DEALLOCATE (data%integral_containers)
    2905              : 
    2906        32154 :       DEALLOCATE (data%integral_caches)
    2907              : 
    2908        32154 :    END SUBROUTINE dealloc_containers
    2909              : 
    2910              : ! **************************************************************************************************
    2911              : !> \brief ...
    2912              : !> \param DATA ...
    2913              : !> \param bin_size ...
    2914              : ! **************************************************************************************************
    2915        32154 :    SUBROUTINE alloc_containers(DATA, bin_size)
    2916              :       TYPE(hfx_compression_type)                         :: data
    2917              :       INTEGER, INTENT(IN)                                :: bin_size
    2918              : 
    2919              :       INTEGER                                            :: bin, i
    2920              : 
    2921     33054312 :       ALLOCATE (data%maxval_cache(bin_size))
    2922        64308 :       DO bin = 1, bin_size
    2923        64308 :          data%maxval_cache(bin)%element_counter = 1
    2924              :       END DO
    2925       128616 :       ALLOCATE (data%maxval_container(bin_size))
    2926        64308 :       DO bin = 1, bin_size
    2927     32990004 :          ALLOCATE (data%maxval_container(bin)%first)
    2928              :          data%maxval_container(bin)%first%prev => NULL()
    2929              :          data%maxval_container(bin)%first%next => NULL()
    2930        32154 :          data%maxval_container(bin)%current => data%maxval_container(bin)%first
    2931     32957850 :          data%maxval_container(bin)%current%data = 0
    2932        64308 :          data%maxval_container(bin)%element_counter = 1
    2933              :       END DO
    2934              : 
    2935      2186472 :       ALLOCATE (data%integral_containers(64, bin_size))
    2936     35112168 :       ALLOCATE (data%integral_caches(64, bin_size))
    2937              : 
    2938        64308 :       DO bin = 1, bin_size
    2939      2122164 :          DO i = 1, 64
    2940      2057856 :             data%integral_caches(i, bin)%element_counter = 1
    2941   2109302400 :             data%integral_caches(i, bin)%data = 0
    2942   2111360256 :             ALLOCATE (data%integral_containers(i, bin)%first)
    2943              :             data%integral_containers(i, bin)%first%prev => NULL()
    2944              :             data%integral_containers(i, bin)%first%next => NULL()
    2945      2057856 :             data%integral_containers(i, bin)%current => data%integral_containers(i, bin)%first
    2946   2109302400 :             data%integral_containers(i, bin)%current%data = 0
    2947      2090010 :             data%integral_containers(i, bin)%element_counter = 1
    2948              :          END DO
    2949              :       END DO
    2950              : 
    2951        32154 :    END SUBROUTINE alloc_containers
    2952              : 
    2953              : ! **************************************************************************************************
    2954              : !> \brief Compares the non-technical parts of two HFX input section and check whether they are the same
    2955              : !>        Ignore things that would not change results (MEMORY, LOAD_BALANCE)
    2956              : !> \param hfx_section1 ...
    2957              : !> \param hfx_section2 ...
    2958              : !> \param is_identical ...
    2959              : !> \param same_except_frac ...
    2960              : !> \return ...
    2961              : ! **************************************************************************************************
    2962          534 :    SUBROUTINE compare_hfx_sections(hfx_section1, hfx_section2, is_identical, same_except_frac)
    2963              : 
    2964              :       TYPE(section_vals_type), POINTER                   :: hfx_section1, hfx_section2
    2965              :       LOGICAL, INTENT(OUT)                               :: is_identical
    2966              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: same_except_frac
    2967              : 
    2968              :       CHARACTER(LEN=default_path_length)                 :: cval1, cval2
    2969              :       INTEGER                                            :: irep, ival1, ival2, n_rep_hf1, n_rep_hf2
    2970              :       LOGICAL                                            :: lval1, lval2
    2971              :       REAL(dp)                                           :: rval1, rval2
    2972              :       TYPE(section_vals_type), POINTER                   :: hfx_sub_section1, hfx_sub_section2
    2973              : 
    2974          178 :       is_identical = .TRUE.
    2975          178 :       IF (PRESENT(same_except_frac)) same_except_frac = .FALSE.
    2976              : 
    2977          178 :       CALL section_vals_get(hfx_section1, n_repetition=n_rep_hf1)
    2978          178 :       CALL section_vals_get(hfx_section2, n_repetition=n_rep_hf2)
    2979          178 :       is_identical = n_rep_hf1 == n_rep_hf2
    2980          184 :       IF (.NOT. is_identical) RETURN
    2981              : 
    2982          106 :       DO irep = 1, n_rep_hf1
    2983           56 :          CALL section_vals_val_get(hfx_section1, "PW_HFX", l_val=lval1, i_rep_section=irep)
    2984           56 :          CALL section_vals_val_get(hfx_section2, "PW_HFX", l_val=lval2, i_rep_section=irep)
    2985           56 :          IF (lval1 .NEQV. lval2) is_identical = .FALSE.
    2986              : 
    2987           56 :          CALL section_vals_val_get(hfx_section1, "PW_HFX_BLOCKSIZE", i_val=ival1, i_rep_section=irep)
    2988           56 :          CALL section_vals_val_get(hfx_section2, "PW_HFX_BLOCKSIZE", i_val=ival2, i_rep_section=irep)
    2989           56 :          IF (ival1 /= ival2) is_identical = .FALSE.
    2990              : 
    2991           56 :          CALL section_vals_val_get(hfx_section1, "TREAT_LSD_IN_CORE", l_val=lval1, i_rep_section=irep)
    2992           56 :          CALL section_vals_val_get(hfx_section2, "TREAT_LSD_IN_CORE", l_val=lval2, i_rep_section=irep)
    2993           56 :          IF (lval1 .NEQV. lval2) is_identical = .FALSE.
    2994              : 
    2995           56 :          hfx_sub_section1 => section_vals_get_subs_vals(hfx_section1, "INTERACTION_POTENTIAL", i_rep_section=irep)
    2996           56 :          hfx_sub_section2 => section_vals_get_subs_vals(hfx_section2, "INTERACTION_POTENTIAL", i_rep_section=irep)
    2997              : 
    2998           56 :          CALL section_vals_val_get(hfx_sub_section1, "OMEGA", r_val=rval1, i_rep_section=irep)
    2999           56 :          CALL section_vals_val_get(hfx_sub_section2, "OMEGA", r_val=rval2, i_rep_section=irep)
    3000           56 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3001              : 
    3002           56 :          CALL section_vals_val_get(hfx_sub_section1, "POTENTIAL_TYPE", i_val=ival1, i_rep_section=irep)
    3003           56 :          CALL section_vals_val_get(hfx_sub_section2, "POTENTIAL_TYPE", i_val=ival2, i_rep_section=irep)
    3004           56 :          IF (ival1 /= ival2) is_identical = .FALSE.
    3005           56 :          IF (.NOT. is_identical) RETURN
    3006              : 
    3007           50 :          IF (ival1 == do_potential_truncated .OR. ival1 == do_potential_mix_cl_trunc) THEN
    3008            6 :             CALL section_vals_val_get(hfx_sub_section1, "CUTOFF_RADIUS", r_val=rval1, i_rep_section=irep)
    3009            6 :             CALL section_vals_val_get(hfx_sub_section2, "CUTOFF_RADIUS", r_val=rval2, i_rep_section=irep)
    3010            6 :             IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3011              : 
    3012            6 :             CALL section_vals_val_get(hfx_sub_section1, "T_C_G_DATA", c_val=cval1, i_rep_section=irep)
    3013            6 :             CALL section_vals_val_get(hfx_sub_section2, "T_C_G_DATA", c_val=cval2, i_rep_section=irep)
    3014            6 :             IF (cval1 /= cval2) is_identical = .FALSE.
    3015              :          END IF
    3016              : 
    3017           50 :          CALL section_vals_val_get(hfx_sub_section1, "SCALE_COULOMB", r_val=rval1, i_rep_section=irep)
    3018           50 :          CALL section_vals_val_get(hfx_sub_section2, "SCALE_COULOMB", r_val=rval2, i_rep_section=irep)
    3019           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3020              : 
    3021           50 :          CALL section_vals_val_get(hfx_sub_section1, "SCALE_GAUSSIAN", r_val=rval1, i_rep_section=irep)
    3022           50 :          CALL section_vals_val_get(hfx_sub_section2, "SCALE_GAUSSIAN", r_val=rval2, i_rep_section=irep)
    3023           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3024              : 
    3025           50 :          CALL section_vals_val_get(hfx_sub_section1, "SCALE_LONGRANGE", r_val=rval1, i_rep_section=irep)
    3026           50 :          CALL section_vals_val_get(hfx_sub_section2, "SCALE_LONGRANGE", r_val=rval2, i_rep_section=irep)
    3027           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3028              : 
    3029           50 :          hfx_sub_section1 => section_vals_get_subs_vals(hfx_section1, "PERIODIC", i_rep_section=irep)
    3030           50 :          hfx_sub_section2 => section_vals_get_subs_vals(hfx_section2, "PERIODIC", i_rep_section=irep)
    3031              : 
    3032           50 :          CALL section_vals_val_get(hfx_sub_section1, "NUMBER_OF_SHELLS", i_val=ival1, i_rep_section=irep)
    3033           50 :          CALL section_vals_val_get(hfx_sub_section2, "NUMBER_OF_SHELLS", i_val=ival2, i_rep_section=irep)
    3034           50 :          IF (ival1 /= ival2) is_identical = .FALSE.
    3035              : 
    3036           50 :          hfx_sub_section1 => section_vals_get_subs_vals(hfx_section1, "RI", i_rep_section=irep)
    3037           50 :          hfx_sub_section2 => section_vals_get_subs_vals(hfx_section2, "RI", i_rep_section=irep)
    3038              : 
    3039           50 :          CALL section_vals_val_get(hfx_sub_section1, "_SECTION_PARAMETERS_", l_val=lval1, i_rep_section=irep)
    3040           50 :          CALL section_vals_val_get(hfx_sub_section2, "_SECTION_PARAMETERS_", l_val=lval2, i_rep_section=irep)
    3041           50 :          IF (lval1 .NEQV. lval2) is_identical = .FALSE.
    3042              : 
    3043           50 :          CALL section_vals_val_get(hfx_sub_section1, "CUTOFF_RADIUS", r_val=rval1, i_rep_section=irep)
    3044           50 :          CALL section_vals_val_get(hfx_sub_section2, "CUTOFF_RADIUS", r_val=rval2, i_rep_section=irep)
    3045           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3046              : 
    3047           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_EIGVAL", r_val=rval1, i_rep_section=irep)
    3048           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_EIGVAL", r_val=rval2, i_rep_section=irep)
    3049           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3050              : 
    3051           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_FILTER", r_val=rval1, i_rep_section=irep)
    3052           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_FILTER", r_val=rval2, i_rep_section=irep)
    3053           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3054              : 
    3055           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_FILTER_2C", r_val=rval1, i_rep_section=irep)
    3056           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_FILTER_2C", r_val=rval2, i_rep_section=irep)
    3057           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3058              : 
    3059           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_FILTER_MO", r_val=rval1, i_rep_section=irep)
    3060           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_FILTER_MO", r_val=rval2, i_rep_section=irep)
    3061           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3062              : 
    3063           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_PGF_ORB", r_val=rval1, i_rep_section=irep)
    3064           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_PGF_ORB", r_val=rval2, i_rep_section=irep)
    3065           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3066              : 
    3067           50 :          CALL section_vals_val_get(hfx_sub_section1, "MAX_BLOCK_SIZE_MO", i_val=ival1, i_rep_section=irep)
    3068           50 :          CALL section_vals_val_get(hfx_sub_section2, "MAX_BLOCK_SIZE_MO", i_val=ival2, i_rep_section=irep)
    3069           50 :          IF (ival1 /= ival2) is_identical = .FALSE.
    3070              : 
    3071           50 :          CALL section_vals_val_get(hfx_sub_section1, "MIN_BLOCK_SIZE", i_val=ival1, i_rep_section=irep)
    3072           50 :          CALL section_vals_val_get(hfx_sub_section2, "MIN_BLOCK_SIZE", i_val=ival2, i_rep_section=irep)
    3073           50 :          IF (ival1 /= ival2) is_identical = .FALSE.
    3074              : 
    3075           50 :          CALL section_vals_val_get(hfx_sub_section1, "OMEGA", r_val=rval1, i_rep_section=irep)
    3076           50 :          CALL section_vals_val_get(hfx_sub_section2, "OMEGA", r_val=rval2, i_rep_section=irep)
    3077           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3078              : 
    3079           50 :          CALL section_vals_val_get(hfx_sub_section1, "RI_FLAVOR", i_val=ival1, i_rep_section=irep)
    3080           50 :          CALL section_vals_val_get(hfx_sub_section2, "RI_FLAVOR", i_val=ival2, i_rep_section=irep)
    3081           50 :          IF (ival1 /= ival2) is_identical = .FALSE.
    3082              : 
    3083           50 :          CALL section_vals_val_get(hfx_sub_section1, "RI_METRIC", i_val=ival1, i_rep_section=irep)
    3084           50 :          CALL section_vals_val_get(hfx_sub_section2, "RI_METRIC", i_val=ival2, i_rep_section=irep)
    3085           50 :          IF (ival1 /= ival2) is_identical = .FALSE.
    3086              : 
    3087           50 :          hfx_sub_section1 => section_vals_get_subs_vals(hfx_section1, "SCREENING", i_rep_section=irep)
    3088           50 :          hfx_sub_section2 => section_vals_get_subs_vals(hfx_section2, "SCREENING", i_rep_section=irep)
    3089              : 
    3090           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_SCHWARZ", r_val=rval1, i_rep_section=irep)
    3091           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_SCHWARZ", r_val=rval2, i_rep_section=irep)
    3092           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3093              : 
    3094           50 :          CALL section_vals_val_get(hfx_sub_section1, "EPS_SCHWARZ_FORCES", r_val=rval1, i_rep_section=irep)
    3095           50 :          CALL section_vals_val_get(hfx_sub_section2, "EPS_SCHWARZ_FORCES", r_val=rval2, i_rep_section=irep)
    3096           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3097              : 
    3098           50 :          CALL section_vals_val_get(hfx_sub_section1, "P_SCREEN_CORRECTION_FACTOR", r_val=rval1, i_rep_section=irep)
    3099           50 :          CALL section_vals_val_get(hfx_sub_section2, "P_SCREEN_CORRECTION_FACTOR", r_val=rval2, i_rep_section=irep)
    3100           50 :          IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3101              : 
    3102           50 :          CALL section_vals_val_get(hfx_sub_section1, "SCREEN_ON_INITIAL_P", l_val=lval1, i_rep_section=irep)
    3103           50 :          CALL section_vals_val_get(hfx_sub_section2, "SCREEN_ON_INITIAL_P", l_val=lval2, i_rep_section=irep)
    3104           50 :          IF (lval1 .NEQV. lval2) is_identical = .FALSE.
    3105              : 
    3106           50 :          CALL section_vals_val_get(hfx_sub_section1, "SCREEN_P_FORCES", l_val=lval1, i_rep_section=irep)
    3107           50 :          CALL section_vals_val_get(hfx_sub_section2, "SCREEN_P_FORCES", l_val=lval2, i_rep_section=irep)
    3108         1380 :          IF (lval1 .NEQV. lval2) is_identical = .FALSE.
    3109              : 
    3110              :       END DO
    3111              : 
    3112              :       !Test of the fraction
    3113           50 :       IF (is_identical) THEN
    3114           92 :          DO irep = 1, n_rep_hf1
    3115           46 :             CALL section_vals_val_get(hfx_section1, "FRACTION", r_val=rval1, i_rep_section=irep)
    3116           46 :             CALL section_vals_val_get(hfx_section2, "FRACTION", r_val=rval2, i_rep_section=irep)
    3117           92 :             IF (ABS(rval1 - rval2) > EPSILON(1.0_dp)) is_identical = .FALSE.
    3118              :          END DO
    3119              : 
    3120           46 :          IF (PRESENT(same_except_frac)) THEN
    3121           34 :             IF (.NOT. is_identical) same_except_frac = .TRUE.
    3122              :          END IF
    3123              :       END IF
    3124              : 
    3125              :    END SUBROUTINE compare_hfx_sections
    3126              : 
    3127            0 : END MODULE hfx_types
    3128              : 
        

Generated by: LCOV version 2.0-1