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

Generated by: LCOV version 2.0-1