LCOV - code coverage report
Current view: top level - src - hfx_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:c24029e) Lines: 95.8 % 1250 1197
Test Date: 2026-07-04 06:36:57 Functions: 41.5 % 53 22

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

Generated by: LCOV version 2.0-1