LCOV - code coverage report
Current view: top level - src - hfx_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:039bc03) Lines: 95.9 % 1232 1181
Test Date: 2026-03-13 07:10:03 Functions: 41.5 % 53 22

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

Generated by: LCOV version 2.0-1