LCOV - code coverage report
Current view: top level - src - gw_utils.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:4c33f95) Lines: 1145 1242 92.2 %
Date: 2025-01-30 06:53:08 Functions: 43 46 93.5 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief
      10             : !> \author Jan Wilhelm
      11             : !> \date 07.2023
      12             : ! **************************************************************************************************
      13             : MODULE gw_utils
      14             :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      15             :                                               get_atomic_kind_set
      16             :    USE basis_set_types,                 ONLY: get_gto_basis_set,&
      17             :                                               gto_basis_set_type
      18             :    USE bibliography,                    ONLY: Graml2024,&
      19             :                                               cite_reference
      20             :    USE cell_types,                      ONLY: cell_type,&
      21             :                                               pbc
      22             :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      23             :                                               cp_blacs_env_release,&
      24             :                                               cp_blacs_env_type
      25             :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      26             :                                               cp_cfm_release,&
      27             :                                               cp_cfm_to_cfm,&
      28             :                                               cp_cfm_to_fm,&
      29             :                                               cp_cfm_type
      30             :    USE cp_control_types,                ONLY: dft_control_type
      31             :    USE cp_dbcsr_api,                    ONLY: &
      32             :         dbcsr_create, dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_p_type, &
      33             :         dbcsr_release, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry, dbcsr_type_symmetric
      34             :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      35             :                                               copy_fm_to_dbcsr,&
      36             :                                               cp_dbcsr_dist2d_to_dist,&
      37             :                                               dbcsr_allocate_matrix_set,&
      38             :                                               dbcsr_deallocate_matrix_set
      39             :    USE cp_files,                        ONLY: close_file,&
      40             :                                               open_file
      41             :    USE cp_fm_basic_linalg,              ONLY: cp_fm_scale_and_add
      42             :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      43             :                                               cp_fm_struct_release,&
      44             :                                               cp_fm_struct_type
      45             :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      46             :                                               cp_fm_get_diag,&
      47             :                                               cp_fm_release,&
      48             :                                               cp_fm_set_all,&
      49             :                                               cp_fm_type
      50             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      51             :                                               cp_logger_type
      52             :    USE cp_output_handling,              ONLY: cp_print_key_generate_filename
      53             :    USE dbt_api,                         ONLY: &
      54             :         dbt_clear, dbt_create, dbt_destroy, dbt_filter, dbt_iterator_blocks_left, &
      55             :         dbt_iterator_next_block, dbt_iterator_start, dbt_iterator_stop, dbt_iterator_type, &
      56             :         dbt_mp_environ_pgrid, dbt_pgrid_create, dbt_pgrid_destroy, dbt_pgrid_type, dbt_type
      57             :    USE distribution_2d_types,           ONLY: distribution_2d_type
      58             :    USE gw_communication,                ONLY: fm_to_local_array
      59             :    USE gw_integrals,                    ONLY: build_3c_integral_block
      60             :    USE gw_kp_to_real_space_and_back,    ONLY: trafo_rs_to_ikp
      61             :    USE input_constants,                 ONLY: do_potential_truncated,&
      62             :                                               large_cell_Gamma,&
      63             :                                               ri_rpa_g0w0_crossing_newton,&
      64             :                                               rtp_method_bse,&
      65             :                                               small_cell_full_kp,&
      66             :                                               xc_none
      67             :    USE input_section_types,             ONLY: section_vals_get,&
      68             :                                               section_vals_get_subs_vals,&
      69             :                                               section_vals_type,&
      70             :                                               section_vals_val_get,&
      71             :                                               section_vals_val_set
      72             :    USE kinds,                           ONLY: default_string_length,&
      73             :                                               dp,&
      74             :                                               int_8
      75             :    USE kpoint_methods,                  ONLY: kpoint_init_cell_index
      76             :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      77             :                                               kpoint_create,&
      78             :                                               kpoint_type
      79             :    USE libint_2c_3c,                    ONLY: libint_potential_type
      80             :    USE libint_wrapper,                  ONLY: cp_libint_static_cleanup,&
      81             :                                               cp_libint_static_init
      82             :    USE machine,                         ONLY: m_memory,&
      83             :                                               m_walltime
      84             :    USE mathconstants,                   ONLY: gaussi,&
      85             :                                               z_one,&
      86             :                                               z_zero
      87             :    USE mathlib,                         ONLY: diag_complex,&
      88             :                                               gcd
      89             :    USE message_passing,                 ONLY: mp_cart_type,&
      90             :                                               mp_para_env_type
      91             :    USE minimax_exp,                     ONLY: get_exp_minimax_coeff
      92             :    USE minimax_exp_gw,                  ONLY: get_exp_minimax_coeff_gw
      93             :    USE minimax_rpa,                     ONLY: get_rpa_minimax_coeff,&
      94             :                                               get_rpa_minimax_coeff_larger_grid
      95             :    USE mp2_gpw,                         ONLY: create_mat_munu
      96             :    USE mp2_grids,                       ONLY: get_l_sq_wghts_cos_tf_t_to_w,&
      97             :                                               get_l_sq_wghts_cos_tf_w_to_t,&
      98             :                                               get_l_sq_wghts_sin_tf_t_to_w
      99             :    USE mp2_ri_2c,                       ONLY: trunc_coulomb_for_exchange
     100             :    USE parallel_gemm_api,               ONLY: parallel_gemm
     101             :    USE particle_methods,                ONLY: get_particle_set
     102             :    USE particle_types,                  ONLY: particle_type
     103             :    USE physcon,                         ONLY: angstrom,&
     104             :                                               evolt
     105             :    USE post_scf_bandstructure_types,    ONLY: post_scf_bandstructure_type
     106             :    USE post_scf_bandstructure_utils,    ONLY: rsmat_to_kp
     107             :    USE qs_energy_types,                 ONLY: qs_energy_type
     108             :    USE qs_environment_types,            ONLY: get_qs_env,&
     109             :                                               qs_env_part_release,&
     110             :                                               qs_environment_type
     111             :    USE qs_integral_utils,               ONLY: basis_set_list_setup
     112             :    USE qs_interactions,                 ONLY: init_interaction_radii_orb_basis
     113             :    USE qs_kind_types,                   ONLY: get_qs_kind,&
     114             :                                               qs_kind_type
     115             :    USE qs_ks_methods,                   ONLY: qs_ks_build_kohn_sham_matrix
     116             :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type,&
     117             :                                               release_neighbor_list_sets
     118             :    USE qs_tensors,                      ONLY: build_2c_integrals,&
     119             :                                               build_2c_neighbor_lists,&
     120             :                                               build_3c_integrals,&
     121             :                                               build_3c_neighbor_lists,&
     122             :                                               get_tensor_occupancy,&
     123             :                                               neighbor_list_3c_destroy
     124             :    USE qs_tensors_types,                ONLY: create_2c_tensor,&
     125             :                                               create_3c_tensor,&
     126             :                                               distribution_3d_create,&
     127             :                                               distribution_3d_type,&
     128             :                                               neighbor_list_3c_type
     129             :    USE rpa_gw,                          ONLY: continuation_pade
     130             : #include "base/base_uses.f90"
     131             : 
     132             :    IMPLICIT NONE
     133             : 
     134             :    PRIVATE
     135             : 
     136             :    PUBLIC :: create_and_init_bs_env_for_gw, de_init_bs_env, get_i_j_atoms, &
     137             :              kpoint_init_cell_index_simple, compute_xkp, time_to_freq, analyt_conti_and_print, &
     138             :              add_R, is_cell_in_index_to_cell, get_V_tr_R, power
     139             : 
     140             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'gw_utils'
     141             : 
     142             : CONTAINS
     143             : 
     144             : ! **************************************************************************************************
     145             : !> \brief ...
     146             : !> \param qs_env ...
     147             : !> \param bs_env ...
     148             : !> \param bs_sec ...
     149             : ! **************************************************************************************************
     150          34 :    SUBROUTINE create_and_init_bs_env_for_gw(qs_env, bs_env, bs_sec)
     151             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     152             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     153             :       TYPE(section_vals_type), POINTER                   :: bs_sec
     154             : 
     155             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_and_init_bs_env_for_gw'
     156             : 
     157             :       INTEGER                                            :: handle
     158             : 
     159          34 :       CALL timeset(routineN, handle)
     160             : 
     161          34 :       CALL cite_reference(Graml2024)
     162             : 
     163          34 :       CALL read_gw_input_parameters(bs_env, bs_sec)
     164             : 
     165          34 :       CALL print_header_and_input_parameters(bs_env)
     166             : 
     167          34 :       CALL setup_AO_and_RI_basis_set(qs_env, bs_env)
     168             : 
     169          34 :       CALL get_RI_basis_and_basis_function_indices(qs_env, bs_env)
     170             : 
     171          34 :       CALL set_heuristic_parameters(bs_env, qs_env)
     172             : 
     173          34 :       CALL cp_libint_static_init()
     174             : 
     175          34 :       CALL setup_kpoints_chi_eps_W(bs_env, bs_env%kpoints_chi_eps_W)
     176             : 
     177          34 :       IF (bs_env%small_cell_full_kp_or_large_cell_Gamma == small_cell_full_kp) THEN
     178           6 :          CALL setup_cells_3c(qs_env, bs_env)
     179             :       END IF
     180             : 
     181          34 :       CALL set_parallelization_parameters(qs_env, bs_env)
     182             : 
     183          34 :       CALL allocate_matrices(qs_env, bs_env)
     184             : 
     185          34 :       CALL compute_V_xc(qs_env, bs_env)
     186             : 
     187          34 :       CALL create_tensors(qs_env, bs_env)
     188             : 
     189          62 :       SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
     190             :       CASE (large_cell_Gamma)
     191             : 
     192          28 :          CALL allocate_GW_eigenvalues(bs_env)
     193             : 
     194          28 :          CALL check_sparsity_3c(qs_env, bs_env)
     195             : 
     196          28 :          CALL set_sparsity_parallelization_parameters(bs_env)
     197             : 
     198          28 :          CALL check_for_restart_files(qs_env, bs_env)
     199             : 
     200             :       CASE (small_cell_full_kp)
     201             : 
     202           6 :          CALL compute_3c_integrals(qs_env, bs_env)
     203             : 
     204           6 :          CALL setup_cells_Delta_R(bs_env)
     205             : 
     206           6 :          CALL setup_parallelization_Delta_R(bs_env)
     207             : 
     208           6 :          CALL allocate_matrices_small_cell_full_kp(qs_env, bs_env)
     209             : 
     210           6 :          CALL trafo_V_xc_R_to_kp(qs_env, bs_env)
     211             : 
     212          40 :          CALL heuristic_RI_regularization(qs_env, bs_env)
     213             : 
     214             :       END SELECT
     215             : 
     216          34 :       CALL setup_time_and_frequency_minimax_grid(bs_env)
     217             : 
     218             :       ! free memory in qs_env; only if one is not calculating the LDOS because
     219             :       ! we need real-space grid operations in pw_env, task_list for the LDOS
     220             :       ! Recommendation in case of memory issues: first perform GW calculation without calculating
     221             :       !                                          LDOS (to safe memor). Then, use GW restart files
     222             :       !                                          in a subsequent calculation to calculate the LDOS
     223             :       ! Marek : TODO - boolean that does not interfere with RTP init but sets this to correct value
     224             :       IF (.NOT. bs_env%do_ldos .AND. .FALSE.) THEN
     225             :          CALL qs_env_part_release(qs_env)
     226             :       END IF
     227             : 
     228          34 :       CALL timestop(handle)
     229             : 
     230          34 :    END SUBROUTINE create_and_init_bs_env_for_gw
     231             : 
     232             : ! **************************************************************************************************
     233             : !> \brief ...
     234             : !> \param bs_env ...
     235             : ! **************************************************************************************************
     236          34 :    SUBROUTINE de_init_bs_env(bs_env)
     237             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     238             : 
     239             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'de_init_bs_env'
     240             : 
     241             :       INTEGER                                            :: handle
     242             : 
     243          34 :       CALL timeset(routineN, handle)
     244             :       ! deallocate quantities here which:
     245             :       ! 1. cannot be deallocated in bs_env_release due to circular dependencies
     246             :       ! 2. consume a lot of memory and should not be kept until the quantity is
     247             :       !    deallocated in bs_env_release
     248             : 
     249          34 :       IF (ASSOCIATED(bs_env%nl_3c%ij_list) .AND. (bs_env%rtp_method == rtp_method_bse)) THEN
     250          12 :          IF (bs_env%unit_nr > 0) WRITE (bs_env%unit_nr, *) "Retaining nl_3c for RTBSE"
     251             :       ELSE
     252          22 :          CALL neighbor_list_3c_destroy(bs_env%nl_3c)
     253             :       END IF
     254             : 
     255          34 :       CALL cp_libint_static_cleanup()
     256             : 
     257          34 :       CALL timestop(handle)
     258             : 
     259          34 :    END SUBROUTINE de_init_bs_env
     260             : 
     261             : ! **************************************************************************************************
     262             : !> \brief ...
     263             : !> \param bs_env ...
     264             : !> \param bs_sec ...
     265             : ! **************************************************************************************************
     266          34 :    SUBROUTINE read_gw_input_parameters(bs_env, bs_sec)
     267             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     268             :       TYPE(section_vals_type), POINTER                   :: bs_sec
     269             : 
     270             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'read_gw_input_parameters'
     271             : 
     272             :       INTEGER                                            :: handle
     273             :       TYPE(section_vals_type), POINTER                   :: gw_sec
     274             : 
     275          34 :       CALL timeset(routineN, handle)
     276             : 
     277          34 :       NULLIFY (gw_sec)
     278          34 :       gw_sec => section_vals_get_subs_vals(bs_sec, "GW")
     279             : 
     280          34 :       CALL section_vals_val_get(gw_sec, "NUM_TIME_FREQ_POINTS", i_val=bs_env%num_time_freq_points)
     281          34 :       CALL section_vals_val_get(gw_sec, "EPS_FILTER", r_val=bs_env%eps_filter)
     282          34 :       CALL section_vals_val_get(gw_sec, "REGULARIZATION_RI", r_val=bs_env%input_regularization_RI)
     283          34 :       CALL section_vals_val_get(gw_sec, "CUTOFF_RADIUS_RI", r_val=bs_env%ri_metric%cutoff_radius)
     284          34 :       CALL section_vals_val_get(gw_sec, "MEMORY_PER_PROC", r_val=bs_env%input_memory_per_proc_GB)
     285          34 :       CALL section_vals_val_get(gw_sec, "APPROX_KP_EXTRAPOL", l_val=bs_env%approx_kp_extrapol)
     286          34 :       CALL section_vals_val_get(gw_sec, "SIZE_LATTICE_SUM", i_val=bs_env%size_lattice_sum_V)
     287          34 :       CALL section_vals_val_get(gw_sec, "KPOINTS_W", i_vals=bs_env%nkp_grid_chi_eps_W_input)
     288          34 :       CALL section_vals_val_get(gw_sec, "HEDIN_SHIFT", l_val=bs_env%do_hedin_shift)
     289             : 
     290          34 :       CALL timestop(handle)
     291             : 
     292          34 :    END SUBROUTINE read_gw_input_parameters
     293             : 
     294             : ! **************************************************************************************************
     295             : !> \brief ...
     296             : !> \param qs_env ...
     297             : !> \param bs_env ...
     298             : ! **************************************************************************************************
     299          34 :    SUBROUTINE setup_AO_and_RI_basis_set(qs_env, bs_env)
     300             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     301             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     302             : 
     303             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_AO_and_RI_basis_set'
     304             : 
     305             :       INTEGER                                            :: handle, natom, nkind
     306          34 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     307          34 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     308             : 
     309          34 :       CALL timeset(routineN, handle)
     310             : 
     311             :       CALL get_qs_env(qs_env, &
     312             :                       qs_kind_set=qs_kind_set, &
     313             :                       particle_set=particle_set, &
     314          34 :                       natom=natom, nkind=nkind)
     315             : 
     316             :       ! set up basis
     317         170 :       ALLOCATE (bs_env%sizes_RI(natom), bs_env%sizes_AO(natom))
     318         282 :       ALLOCATE (bs_env%basis_set_RI(nkind), bs_env%basis_set_AO(nkind))
     319             : 
     320          34 :       CALL basis_set_list_setup(bs_env%basis_set_RI, "RI_AUX", qs_kind_set)
     321          34 :       CALL basis_set_list_setup(bs_env%basis_set_AO, "ORB", qs_kind_set)
     322             : 
     323             :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=bs_env%sizes_RI, &
     324          34 :                             basis=bs_env%basis_set_RI)
     325             :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=bs_env%sizes_AO, &
     326          34 :                             basis=bs_env%basis_set_AO)
     327             : 
     328          34 :       CALL timestop(handle)
     329             : 
     330          34 :    END SUBROUTINE setup_AO_and_RI_basis_set
     331             : 
     332             : ! **************************************************************************************************
     333             : !> \brief ...
     334             : !> \param qs_env ...
     335             : !> \param bs_env ...
     336             : ! **************************************************************************************************
     337          34 :    SUBROUTINE get_RI_basis_and_basis_function_indices(qs_env, bs_env)
     338             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     339             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     340             : 
     341             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_RI_basis_and_basis_function_indices'
     342             : 
     343             :       INTEGER                                            :: handle, i_RI, iatom, ikind, iset, &
     344             :                                                             max_AO_bf_per_atom, n_ao_test, n_atom, &
     345             :                                                             n_kind, n_RI, nset, nsgf, u
     346          34 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: kind_of
     347          34 :       INTEGER, DIMENSION(:), POINTER                     :: l_max, l_min, nsgf_set
     348          34 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     349             :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a
     350          34 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     351             : 
     352          34 :       CALL timeset(routineN, handle)
     353             : 
     354             :       ! determine RI basis set size
     355          34 :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
     356             : 
     357          34 :       n_kind = SIZE(qs_kind_set)
     358          34 :       n_atom = bs_env%n_atom
     359             : 
     360          34 :       CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
     361             : 
     362          90 :       DO ikind = 1, n_kind
     363             :          CALL get_qs_kind(qs_kind=qs_kind_set(ikind), basis_set=basis_set_a, &
     364          56 :                           basis_type="RI_AUX")
     365          90 :          CPASSERT(ASSOCIATED(basis_set_a))
     366             :       END DO
     367             : 
     368         102 :       ALLOCATE (bs_env%i_RI_start_from_atom(n_atom))
     369          68 :       ALLOCATE (bs_env%i_RI_end_from_atom(n_atom))
     370          68 :       ALLOCATE (bs_env%i_ao_start_from_atom(n_atom))
     371          68 :       ALLOCATE (bs_env%i_ao_end_from_atom(n_atom))
     372             : 
     373          34 :       n_RI = 0
     374         108 :       DO iatom = 1, n_atom
     375          74 :          bs_env%i_RI_start_from_atom(iatom) = n_RI + 1
     376          74 :          ikind = kind_of(iatom)
     377          74 :          CALL get_qs_kind(qs_kind=qs_kind_set(ikind), nsgf=nsgf, basis_type="RI_AUX")
     378          74 :          n_RI = n_RI + nsgf
     379         108 :          bs_env%i_RI_end_from_atom(iatom) = n_RI
     380             :       END DO
     381          34 :       bs_env%n_RI = n_RI
     382             : 
     383          34 :       max_AO_bf_per_atom = 0
     384          34 :       n_ao_test = 0
     385         108 :       DO iatom = 1, n_atom
     386          74 :          bs_env%i_ao_start_from_atom(iatom) = n_ao_test + 1
     387          74 :          ikind = kind_of(iatom)
     388          74 :          CALL get_qs_kind(qs_kind=qs_kind_set(ikind), nsgf=nsgf, basis_type="ORB")
     389          74 :          n_ao_test = n_ao_test + nsgf
     390          74 :          bs_env%i_ao_end_from_atom(iatom) = n_ao_test
     391         108 :          max_AO_bf_per_atom = MAX(max_AO_bf_per_atom, nsgf)
     392             :       END DO
     393          34 :       CPASSERT(n_ao_test == bs_env%n_ao)
     394          34 :       bs_env%max_AO_bf_per_atom = max_AO_bf_per_atom
     395             : 
     396         102 :       ALLOCATE (bs_env%l_RI(n_RI))
     397          34 :       i_RI = 0
     398         108 :       DO iatom = 1, n_atom
     399          74 :          ikind = kind_of(iatom)
     400             : 
     401          74 :          nset = bs_env%basis_set_RI(ikind)%gto_basis_set%nset
     402          74 :          l_max => bs_env%basis_set_RI(ikind)%gto_basis_set%lmax
     403          74 :          l_min => bs_env%basis_set_RI(ikind)%gto_basis_set%lmin
     404          74 :          nsgf_set => bs_env%basis_set_RI(ikind)%gto_basis_set%nsgf_set
     405             : 
     406         300 :          DO iset = 1, nset
     407         192 :             CPASSERT(l_max(iset) == l_min(iset))
     408         580 :             bs_env%l_RI(i_RI + 1:i_RI + nsgf_set(iset)) = l_max(iset)
     409         266 :             i_RI = i_RI + nsgf_set(iset)
     410             :          END DO
     411             : 
     412             :       END DO
     413          34 :       CPASSERT(i_RI == n_RI)
     414             : 
     415          34 :       u = bs_env%unit_nr
     416             : 
     417          34 :       IF (u > 0) THEN
     418          17 :          WRITE (u, FMT="(T2,A)") " "
     419          17 :          WRITE (u, FMT="(T2,2A,T75,I8)") "Number of auxiliary Gaussian basis functions ", &
     420          34 :             "for χ, ε, W", n_RI
     421             :       END IF
     422             : 
     423          34 :       CALL timestop(handle)
     424             : 
     425          68 :    END SUBROUTINE get_RI_basis_and_basis_function_indices
     426             : 
     427             : ! **************************************************************************************************
     428             : !> \brief ...
     429             : !> \param bs_env ...
     430             : !> \param kpoints ...
     431             : ! **************************************************************************************************
     432          34 :    SUBROUTINE setup_kpoints_chi_eps_W(bs_env, kpoints)
     433             : 
     434             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     435             :       TYPE(kpoint_type), POINTER                         :: kpoints
     436             : 
     437             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_chi_eps_W'
     438             : 
     439             :       INTEGER                                            :: handle, i_dim, n_dim, nkp, nkp_extra, &
     440             :                                                             nkp_orig, u
     441             :       INTEGER, DIMENSION(3)                              :: nkp_grid, nkp_grid_extra, periodic
     442             :       REAL(KIND=dp)                                      :: exp_s_p, n_dim_inv
     443             : 
     444          34 :       CALL timeset(routineN, handle)
     445             : 
     446             :       ! routine adapted from mp2_integrals.F
     447          34 :       NULLIFY (kpoints)
     448          34 :       CALL kpoint_create(kpoints)
     449             : 
     450          34 :       kpoints%kp_scheme = "GENERAL"
     451             : 
     452         136 :       periodic(1:3) = bs_env%periodic(1:3)
     453             : 
     454          34 :       CPASSERT(SIZE(bs_env%nkp_grid_chi_eps_W_input) == 3)
     455             : 
     456             :       IF (bs_env%nkp_grid_chi_eps_W_input(1) > 0 .AND. &
     457          34 :           bs_env%nkp_grid_chi_eps_W_input(2) > 0 .AND. &
     458             :           bs_env%nkp_grid_chi_eps_W_input(3) > 0) THEN
     459             :          ! 1. k-point mesh for χ, ε, W from input
     460           0 :          DO i_dim = 1, 3
     461           0 :             SELECT CASE (periodic(i_dim))
     462             :             CASE (0)
     463           0 :                nkp_grid(i_dim) = 1
     464           0 :                nkp_grid_extra(i_dim) = 1
     465             :             CASE (1)
     466           0 :                nkp_grid(i_dim) = bs_env%nkp_grid_chi_eps_W_input(i_dim)
     467           0 :                nkp_grid_extra(i_dim) = nkp_grid(i_dim)*2
     468             :             CASE DEFAULT
     469           0 :                CPABORT("Error in periodicity.")
     470             :             END SELECT
     471             :          END DO
     472             : 
     473             :       ELSE IF (bs_env%nkp_grid_chi_eps_W_input(1) == -1 .AND. &
     474          34 :                bs_env%nkp_grid_chi_eps_W_input(2) == -1 .AND. &
     475             :                bs_env%nkp_grid_chi_eps_W_input(3) == -1) THEN
     476             :          ! 2. automatic k-point mesh for χ, ε, W
     477             : 
     478         136 :          DO i_dim = 1, 3
     479             : 
     480         102 :             CPASSERT(periodic(i_dim) == 0 .OR. periodic(i_dim) == 1)
     481             : 
     482          34 :             SELECT CASE (periodic(i_dim))
     483             :             CASE (0)
     484          58 :                nkp_grid(i_dim) = 1
     485          58 :                nkp_grid_extra(i_dim) = 1
     486             :             CASE (1)
     487          44 :                SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
     488             :                CASE (large_cell_Gamma)
     489          32 :                   nkp_grid(i_dim) = 4
     490          32 :                   nkp_grid_extra(i_dim) = 6
     491             :                CASE (small_cell_full_kp)
     492          12 :                   nkp_grid(i_dim) = bs_env%kpoints_scf_desymm%nkp_grid(i_dim)*4
     493          12 :                   nkp_grid_extra(i_dim) = bs_env%kpoints_scf_desymm%nkp_grid(i_dim)*8
     494             :                END SELECT
     495             :             CASE DEFAULT
     496         102 :                CPABORT("Error in periodicity.")
     497             :             END SELECT
     498             : 
     499             :          END DO
     500             : 
     501             :       ELSE
     502             : 
     503           0 :          CPABORT("An error occured when setting up the k-mesh for W.")
     504             : 
     505             :       END IF
     506             : 
     507          34 :       nkp_orig = MAX(nkp_grid(1)*nkp_grid(2)*nkp_grid(3)/2, 1)
     508             : 
     509          34 :       nkp_extra = nkp_grid_extra(1)*nkp_grid_extra(2)*nkp_grid_extra(3)/2
     510             : 
     511          34 :       nkp = nkp_orig + nkp_extra
     512             : 
     513         136 :       kpoints%nkp_grid(1:3) = nkp_grid(1:3)
     514          34 :       kpoints%nkp = nkp
     515             : 
     516         136 :       bs_env%nkp_grid_chi_eps_W_orig(1:3) = nkp_grid(1:3)
     517         136 :       bs_env%nkp_grid_chi_eps_W_extra(1:3) = nkp_grid_extra(1:3)
     518          34 :       bs_env%nkp_chi_eps_W_orig = nkp_orig
     519          34 :       bs_env%nkp_chi_eps_W_extra = nkp_extra
     520          34 :       bs_env%nkp_chi_eps_W_orig_plus_extra = nkp
     521             : 
     522         170 :       ALLOCATE (kpoints%xkp(3, nkp), kpoints%wkp(nkp))
     523         170 :       ALLOCATE (bs_env%wkp_no_extra(nkp), bs_env%wkp_s_p(nkp))
     524             : 
     525          34 :       CALL compute_xkp(kpoints%xkp, 1, nkp_orig, nkp_grid)
     526          34 :       CALL compute_xkp(kpoints%xkp, nkp_orig + 1, nkp, nkp_grid_extra)
     527             : 
     528         136 :       n_dim = SUM(periodic)
     529          34 :       IF (n_dim == 0) THEN
     530             :          ! molecules
     531          12 :          kpoints%wkp(1) = 1.0_dp
     532          12 :          bs_env%wkp_s_p(1) = 1.0_dp
     533          12 :          bs_env%wkp_no_extra(1) = 1.0_dp
     534             :       ELSE
     535             : 
     536          22 :          n_dim_inv = 1.0_dp/REAL(n_dim, KIND=dp)
     537             : 
     538             :          ! k-point weights are chosen to automatically extrapolate the k-point mesh
     539          22 :          CALL compute_wkp(kpoints%wkp(1:nkp_orig), nkp_orig, nkp_extra, n_dim_inv)
     540          22 :          CALL compute_wkp(kpoints%wkp(nkp_orig + 1:nkp), nkp_extra, nkp_orig, n_dim_inv)
     541             : 
     542         918 :          bs_env%wkp_no_extra(1:nkp_orig) = 0.0_dp
     543        3382 :          bs_env%wkp_no_extra(nkp_orig + 1:nkp) = 1.0_dp/REAL(nkp_extra, KIND=dp)
     544             : 
     545          22 :          IF (n_dim == 3) THEN
     546             :             ! W_PQ(k) for an s-function P and a p-function Q diverges as 1/k at k=0
     547             :             ! (instead of 1/k^2 for P and Q both being s-functions).
     548           0 :             exp_s_p = 2.0_dp*n_dim_inv
     549           0 :             CALL compute_wkp(bs_env%wkp_s_p(1:nkp_orig), nkp_orig, nkp_extra, exp_s_p)
     550           0 :             CALL compute_wkp(bs_env%wkp_s_p(nkp_orig + 1:nkp), nkp_extra, nkp_orig, exp_s_p)
     551             :          ELSE
     552        4278 :             bs_env%wkp_s_p(1:nkp) = bs_env%wkp_no_extra(1:nkp)
     553             :          END IF
     554             : 
     555             :       END IF
     556             : 
     557          34 :       IF (bs_env%approx_kp_extrapol) THEN
     558           2 :          bs_env%wkp_orig = 1.0_dp/REAL(nkp_orig, KIND=dp)
     559             :       END IF
     560             : 
     561             :       ! heuristic parameter: how many k-points for χ, ε, and W are used simultaneously
     562             :       ! (less simultaneous k-points: less memory, but more computational effort because of
     563             :       !  recomputation of V(k))
     564          34 :       bs_env%nkp_chi_eps_W_batch = 4
     565             : 
     566             :       bs_env%num_chi_eps_W_batches = (bs_env%nkp_chi_eps_W_orig_plus_extra - 1)/ &
     567          34 :                                      bs_env%nkp_chi_eps_W_batch + 1
     568             : 
     569          34 :       u = bs_env%unit_nr
     570             : 
     571          34 :       IF (u > 0) THEN
     572          17 :          WRITE (u, FMT="(T2,A)") " "
     573          17 :          WRITE (u, FMT="(T2,1A,T71,3I4)") "K-point mesh 1 for χ, ε, W", nkp_grid(1:3)
     574          17 :          WRITE (u, FMT="(T2,2A,T71,3I4)") "K-point mesh 2 for χ, ε, W ", &
     575          34 :             "(for k-point extrapolation of W)", nkp_grid_extra(1:3)
     576          17 :          WRITE (u, FMT="(T2,A,T80,L)") "Approximate the k-point extrapolation", &
     577          34 :             bs_env%approx_kp_extrapol
     578             :       END IF
     579             : 
     580          34 :       CALL timestop(handle)
     581             : 
     582          34 :    END SUBROUTINE setup_kpoints_chi_eps_W
     583             : 
     584             : ! **************************************************************************************************
     585             : !> \brief ...
     586             : !> \param kpoints ...
     587             : !> \param qs_env ...
     588             : ! **************************************************************************************************
     589           0 :    SUBROUTINE kpoint_init_cell_index_simple(kpoints, qs_env)
     590             : 
     591             :       TYPE(kpoint_type), POINTER                         :: kpoints
     592             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     593             : 
     594             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'kpoint_init_cell_index_simple'
     595             : 
     596             :       INTEGER                                            :: handle
     597             :       TYPE(dft_control_type), POINTER                    :: dft_control
     598             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     599             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     600           0 :          POINTER                                         :: sab_orb
     601             : 
     602           0 :       CALL timeset(routineN, handle)
     603             : 
     604           0 :       NULLIFY (dft_control, para_env, sab_orb)
     605           0 :       CALL get_qs_env(qs_env=qs_env, para_env=para_env, dft_control=dft_control, sab_orb=sab_orb)
     606           0 :       CALL kpoint_init_cell_index(kpoints, sab_orb, para_env, dft_control)
     607             : 
     608           0 :       CALL timestop(handle)
     609             : 
     610           0 :    END SUBROUTINE kpoint_init_cell_index_simple
     611             : 
     612             : ! **************************************************************************************************
     613             : !> \brief ...
     614             : !> \param xkp ...
     615             : !> \param ikp_start ...
     616             : !> \param ikp_end ...
     617             : !> \param grid ...
     618             : ! **************************************************************************************************
     619          68 :    SUBROUTINE compute_xkp(xkp, ikp_start, ikp_end, grid)
     620             : 
     621             :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: xkp
     622             :       INTEGER                                            :: ikp_start, ikp_end
     623             :       INTEGER, DIMENSION(3)                              :: grid
     624             : 
     625             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_xkp'
     626             : 
     627             :       INTEGER                                            :: handle, i, ix, iy, iz
     628             : 
     629          68 :       CALL timeset(routineN, handle)
     630             : 
     631          68 :       i = ikp_start
     632         384 :       DO ix = 1, grid(1)
     633        6120 :          DO iy = 1, grid(2)
     634       14588 :             DO iz = 1, grid(3)
     635             : 
     636        8536 :                IF (i > ikp_end) CYCLE
     637             : 
     638        4268 :                xkp(1, i) = REAL(2*ix - grid(1) - 1, KIND=dp)/(2._dp*REAL(grid(1), KIND=dp))
     639        4268 :                xkp(2, i) = REAL(2*iy - grid(2) - 1, KIND=dp)/(2._dp*REAL(grid(2), KIND=dp))
     640        4268 :                xkp(3, i) = REAL(2*iz - grid(3) - 1, KIND=dp)/(2._dp*REAL(grid(3), KIND=dp))
     641       14272 :                i = i + 1
     642             : 
     643             :             END DO
     644             :          END DO
     645             :       END DO
     646             : 
     647          68 :       CALL timestop(handle)
     648             : 
     649          68 :    END SUBROUTINE compute_xkp
     650             : 
     651             : ! **************************************************************************************************
     652             : !> \brief ...
     653             : !> \param wkp ...
     654             : !> \param nkp_1 ...
     655             : !> \param nkp_2 ...
     656             : !> \param exponent ...
     657             : ! **************************************************************************************************
     658          44 :    SUBROUTINE compute_wkp(wkp, nkp_1, nkp_2, exponent)
     659             :       REAL(KIND=dp), DIMENSION(:)                        :: wkp
     660             :       INTEGER                                            :: nkp_1, nkp_2
     661             :       REAL(KIND=dp)                                      :: exponent
     662             : 
     663             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_wkp'
     664             : 
     665             :       INTEGER                                            :: handle
     666             :       REAL(KIND=dp)                                      :: nkp_ratio
     667             : 
     668          44 :       CALL timeset(routineN, handle)
     669             : 
     670          44 :       nkp_ratio = REAL(nkp_2, KIND=dp)/REAL(nkp_1, KIND=dp)
     671             : 
     672        4300 :       wkp(:) = 1.0_dp/REAL(nkp_1, KIND=dp)/(1.0_dp - nkp_ratio**exponent)
     673             : 
     674          44 :       CALL timestop(handle)
     675             : 
     676          44 :    END SUBROUTINE compute_wkp
     677             : 
     678             : ! **************************************************************************************************
     679             : !> \brief ...
     680             : !> \param qs_env ...
     681             : !> \param bs_env ...
     682             : ! **************************************************************************************************
     683          34 :    SUBROUTINE allocate_matrices(qs_env, bs_env)
     684             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     685             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     686             : 
     687             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'allocate_matrices'
     688             : 
     689             :       INTEGER                                            :: handle, i_t
     690             :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env, blacs_env_tensor
     691             :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct, fm_struct_RI_global
     692             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     693             : 
     694          34 :       CALL timeset(routineN, handle)
     695             : 
     696          34 :       CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
     697             : 
     698          34 :       fm_struct => bs_env%fm_ks_Gamma(1)%matrix_struct
     699             : 
     700          34 :       CALL cp_fm_create(bs_env%fm_Gocc, fm_struct)
     701          34 :       CALL cp_fm_create(bs_env%fm_Gvir, fm_struct)
     702             : 
     703          34 :       NULLIFY (fm_struct_RI_global)
     704             :       CALL cp_fm_struct_create(fm_struct_RI_global, context=blacs_env, nrow_global=bs_env%n_RI, &
     705          34 :                                ncol_global=bs_env%n_RI, para_env=para_env)
     706          34 :       CALL cp_fm_create(bs_env%fm_RI_RI, fm_struct_RI_global)
     707          34 :       CALL cp_fm_create(bs_env%fm_chi_Gamma_freq, fm_struct_RI_global)
     708          34 :       CALL cp_fm_create(bs_env%fm_W_MIC_freq, fm_struct_RI_global)
     709          34 :       IF (bs_env%approx_kp_extrapol) THEN
     710           2 :          CALL cp_fm_create(bs_env%fm_W_MIC_freq_1_extra, fm_struct_RI_global)
     711           2 :          CALL cp_fm_create(bs_env%fm_W_MIC_freq_1_no_extra, fm_struct_RI_global)
     712           2 :          CALL cp_fm_set_all(bs_env%fm_W_MIC_freq_1_extra, 0.0_dp)
     713           2 :          CALL cp_fm_set_all(bs_env%fm_W_MIC_freq_1_no_extra, 0.0_dp)
     714             :       END IF
     715          34 :       CALL cp_fm_struct_release(fm_struct_RI_global)
     716             : 
     717             :       ! create blacs_env for subgroups of tensor operations
     718          34 :       NULLIFY (blacs_env_tensor)
     719          34 :       CALL cp_blacs_env_create(blacs_env=blacs_env_tensor, para_env=bs_env%para_env_tensor)
     720             : 
     721             :       ! allocate dbcsr matrices in the tensor subgroup; actually, one only needs a small
     722             :       ! subset of blocks in the tensor subgroup, however, all atomic blocks are allocated.
     723             :       ! One might think of creating a dbcsr matrix with only the blocks that are needed
     724             :       ! in the tensor subgroup
     725             :       CALL create_mat_munu(bs_env%mat_ao_ao_tensor, qs_env, bs_env%eps_atom_grid_2d_mat, &
     726          34 :                            blacs_env_tensor, do_ri_aux_basis=.FALSE.)
     727             : 
     728             :       CALL create_mat_munu(bs_env%mat_RI_RI_tensor, qs_env, bs_env%eps_atom_grid_2d_mat, &
     729          34 :                            blacs_env_tensor, do_ri_aux_basis=.TRUE.)
     730             : 
     731             :       CALL create_mat_munu(bs_env%mat_RI_RI, qs_env, bs_env%eps_atom_grid_2d_mat, &
     732          34 :                            blacs_env, do_ri_aux_basis=.TRUE.)
     733             : 
     734          34 :       CALL cp_blacs_env_release(blacs_env_tensor)
     735             : 
     736          34 :       NULLIFY (bs_env%mat_chi_Gamma_tau)
     737          34 :       CALL dbcsr_allocate_matrix_set(bs_env%mat_chi_Gamma_tau, bs_env%num_time_freq_points)
     738             : 
     739         470 :       DO i_t = 1, bs_env%num_time_freq_points
     740         436 :          ALLOCATE (bs_env%mat_chi_Gamma_tau(i_t)%matrix)
     741         470 :          CALL dbcsr_create(bs_env%mat_chi_Gamma_tau(i_t)%matrix, template=bs_env%mat_RI_RI%matrix)
     742             :       END DO
     743             : 
     744          34 :       CALL timestop(handle)
     745             : 
     746          34 :    END SUBROUTINE allocate_matrices
     747             : 
     748             : ! **************************************************************************************************
     749             : !> \brief ...
     750             : !> \param bs_env ...
     751             : ! **************************************************************************************************
     752          28 :    SUBROUTINE allocate_GW_eigenvalues(bs_env)
     753             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     754             : 
     755             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_GW_eigenvalues'
     756             : 
     757             :       INTEGER                                            :: handle
     758             : 
     759          28 :       CALL timeset(routineN, handle)
     760             : 
     761         140 :       ALLOCATE (bs_env%eigenval_G0W0(bs_env%n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
     762         140 :       ALLOCATE (bs_env%eigenval_HF(bs_env%n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
     763             : 
     764          28 :       CALL timestop(handle)
     765             : 
     766          28 :    END SUBROUTINE allocate_GW_eigenvalues
     767             : 
     768             : ! **************************************************************************************************
     769             : !> \brief ...
     770             : !> \param qs_env ...
     771             : !> \param bs_env ...
     772             : ! **************************************************************************************************
     773          34 :    SUBROUTINE create_tensors(qs_env, bs_env)
     774             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     775             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     776             : 
     777             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'create_tensors'
     778             : 
     779             :       INTEGER                                            :: handle
     780             : 
     781          34 :       CALL timeset(routineN, handle)
     782             : 
     783          34 :       CALL init_interaction_radii(bs_env)
     784             : 
     785             :       ! split blocks does not improve load balancing/efficienfy for tensor contraction, so we go
     786             :       ! with the standard atomic blocks
     787             :       CALL create_3c_t(bs_env%t_RI_AO__AO, bs_env%para_env_tensor, "(RI AO | AO)", [1, 2], [3], &
     788             :                        bs_env%sizes_RI, bs_env%sizes_AO, &
     789          34 :                        create_nl_3c=.TRUE., nl_3c=bs_env%nl_3c, qs_env=qs_env)
     790             :       CALL create_3c_t(bs_env%t_RI__AO_AO, bs_env%para_env_tensor, "(RI | AO AO)", [1], [2, 3], &
     791          34 :                        bs_env%sizes_RI, bs_env%sizes_AO)
     792             : 
     793          34 :       CALL create_2c_t(bs_env, bs_env%sizes_RI, bs_env%sizes_AO)
     794             : 
     795          34 :       CALL timestop(handle)
     796             : 
     797          34 :    END SUBROUTINE create_tensors
     798             : 
     799             : ! **************************************************************************************************
     800             : !> \brief ...
     801             : !> \param qs_env ...
     802             : !> \param bs_env ...
     803             : ! **************************************************************************************************
     804          28 :    SUBROUTINE check_sparsity_3c(qs_env, bs_env)
     805             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     806             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     807             : 
     808             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'check_sparsity_3c'
     809             : 
     810             :       INTEGER                                            :: handle, n_atom_step, RI_atom
     811             :       INTEGER(int_8)                                     :: mem, non_zero_elements_sum, nze
     812             :       REAL(dp)                                           :: max_dist_AO_atoms, occ, occupation_sum
     813             :       REAL(KIND=dp)                                      :: t1, t2
     814         196 :       TYPE(dbt_type)                                     :: t_3c_global
     815          28 :       TYPE(dbt_type), ALLOCATABLE, DIMENSION(:, :)       :: t_3c_global_array
     816             :       TYPE(neighbor_list_3c_type)                        :: nl_3c_global
     817             : 
     818          28 :       CALL timeset(routineN, handle)
     819             : 
     820             :       ! check the sparsity of 3c integral tensor (µν|P); calculate maximum distance between
     821             :       ! AO atoms µ, ν where at least a single integral (µν|P) is larger than the filter threshold
     822             :       CALL create_3c_t(t_3c_global, bs_env%para_env, "(RI AO | AO)", [1, 2], [3], &
     823             :                        bs_env%sizes_RI, bs_env%sizes_AO, &
     824          28 :                        create_nl_3c=.TRUE., nl_3c=nl_3c_global, qs_env=qs_env)
     825             : 
     826          28 :       CALL m_memory(mem)
     827          28 :       CALL bs_env%para_env%max(mem)
     828             : 
     829         252 :       ALLOCATE (t_3c_global_array(1, 1))
     830          28 :       CALL dbt_create(t_3c_global, t_3c_global_array(1, 1))
     831             : 
     832          28 :       CALL bs_env%para_env%sync()
     833          28 :       t1 = m_walltime()
     834             : 
     835          28 :       occupation_sum = 0.0_dp
     836          28 :       non_zero_elements_sum = 0
     837          28 :       max_dist_AO_atoms = 0.0_dp
     838          28 :       n_atom_step = INT(SQRT(REAL(bs_env%n_atom, KIND=dp)))
     839             :       ! do not compute full 3c integrals at once because it may cause out of memory
     840          88 :       DO RI_atom = 1, bs_env%n_atom, n_atom_step
     841             : 
     842             :          CALL build_3c_integrals(t_3c_global_array, &
     843             :                                  bs_env%eps_filter, &
     844             :                                  qs_env, &
     845             :                                  nl_3c_global, &
     846             :                                  int_eps=bs_env%eps_filter, &
     847             :                                  basis_i=bs_env%basis_set_RI, &
     848             :                                  basis_j=bs_env%basis_set_AO, &
     849             :                                  basis_k=bs_env%basis_set_AO, &
     850             :                                  bounds_i=[RI_atom, MIN(RI_atom + n_atom_step - 1, bs_env%n_atom)], &
     851             :                                  potential_parameter=bs_env%ri_metric, &
     852         180 :                                  desymmetrize=.FALSE.)
     853             : 
     854          60 :          CALL dbt_filter(t_3c_global_array(1, 1), bs_env%eps_filter)
     855             : 
     856          60 :          CALL bs_env%para_env%sync()
     857             : 
     858          60 :          CALL get_tensor_occupancy(t_3c_global_array(1, 1), nze, occ)
     859          60 :          non_zero_elements_sum = non_zero_elements_sum + nze
     860          60 :          occupation_sum = occupation_sum + occ
     861             : 
     862          60 :          CALL get_max_dist_AO_atoms(t_3c_global_array(1, 1), max_dist_AO_atoms, qs_env)
     863             : 
     864         148 :          CALL dbt_clear(t_3c_global_array(1, 1))
     865             : 
     866             :       END DO
     867             : 
     868          28 :       t2 = m_walltime()
     869             : 
     870          28 :       bs_env%occupation_3c_int = occupation_sum
     871          28 :       bs_env%max_dist_AO_atoms = max_dist_AO_atoms
     872             : 
     873          28 :       CALL dbt_destroy(t_3c_global)
     874          28 :       CALL dbt_destroy(t_3c_global_array(1, 1))
     875          56 :       DEALLOCATE (t_3c_global_array)
     876             : 
     877          28 :       CALL neighbor_list_3c_destroy(nl_3c_global)
     878             : 
     879          28 :       IF (bs_env%unit_nr > 0) THEN
     880          14 :          WRITE (bs_env%unit_nr, '(T2,A)') ''
     881             :          WRITE (bs_env%unit_nr, '(T2,A,F27.1,A)') &
     882          14 :             'Computed 3-center integrals (µν|P), execution time', t2 - t1, ' s'
     883          14 :          WRITE (bs_env%unit_nr, '(T2,A,F48.3,A)') 'Percentage of non-zero (µν|P)', &
     884          28 :             occupation_sum*100, ' %'
     885          14 :          WRITE (bs_env%unit_nr, '(T2,A,F33.1,A)') 'Max. distance between µ,ν in non-zero (µν|P)', &
     886          28 :             max_dist_AO_atoms*angstrom, ' A'
     887          14 :          WRITE (bs_env%unit_nr, '(T2,2A,I20,A)') 'Required memory if storing all 3-center ', &
     888          28 :             'integrals (µν|P)', INT(REAL(non_zero_elements_sum, KIND=dp)*8.0E-9_dp), ' GB'
     889             :       END IF
     890             : 
     891          28 :       CALL timestop(handle)
     892             : 
     893         112 :    END SUBROUTINE check_sparsity_3c
     894             : 
     895             : ! **************************************************************************************************
     896             : !> \brief ...
     897             : !> \param bs_env ...
     898             : !> \param sizes_RI ...
     899             : !> \param sizes_AO ...
     900             : ! **************************************************************************************************
     901          34 :    SUBROUTINE create_2c_t(bs_env, sizes_RI, sizes_AO)
     902             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     903             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: sizes_RI, sizes_AO
     904             : 
     905             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'create_2c_t'
     906             : 
     907             :       INTEGER                                            :: handle
     908          34 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: dist_1, dist_2
     909             :       INTEGER, DIMENSION(2)                              :: pdims_2d
     910         102 :       TYPE(dbt_pgrid_type)                               :: pgrid_2d
     911             : 
     912          34 :       CALL timeset(routineN, handle)
     913             : 
     914             :       ! inspired from rpa_im_time.F / hfx_types.F
     915             : 
     916          34 :       pdims_2d = 0
     917          34 :       CALL dbt_pgrid_create(bs_env%para_env_tensor, pdims_2d, pgrid_2d)
     918             : 
     919             :       CALL create_2c_tensor(bs_env%t_G, dist_1, dist_2, pgrid_2d, sizes_AO, sizes_AO, &
     920          34 :                             name="(AO | AO)")
     921          34 :       DEALLOCATE (dist_1, dist_2)
     922             :       CALL create_2c_tensor(bs_env%t_chi, dist_1, dist_2, pgrid_2d, sizes_RI, sizes_RI, &
     923          34 :                             name="(RI | RI)")
     924          34 :       DEALLOCATE (dist_1, dist_2)
     925             :       CALL create_2c_tensor(bs_env%t_W, dist_1, dist_2, pgrid_2d, sizes_RI, sizes_RI, &
     926          34 :                             name="(RI | RI)")
     927          34 :       DEALLOCATE (dist_1, dist_2)
     928          34 :       CALL dbt_pgrid_destroy(pgrid_2d)
     929             : 
     930          34 :       CALL timestop(handle)
     931             : 
     932          34 :    END SUBROUTINE create_2c_t
     933             : 
     934             : ! **************************************************************************************************
     935             : !> \brief ...
     936             : !> \param tensor ...
     937             : !> \param para_env ...
     938             : !> \param tensor_name ...
     939             : !> \param map1 ...
     940             : !> \param map2 ...
     941             : !> \param sizes_RI ...
     942             : !> \param sizes_AO ...
     943             : !> \param create_nl_3c ...
     944             : !> \param nl_3c ...
     945             : !> \param qs_env ...
     946             : ! **************************************************************************************************
     947          96 :    SUBROUTINE create_3c_t(tensor, para_env, tensor_name, map1, map2, sizes_RI, sizes_AO, &
     948             :                           create_nl_3c, nl_3c, qs_env)
     949             :       TYPE(dbt_type)                                     :: tensor
     950             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     951             :       CHARACTER(LEN=12)                                  :: tensor_name
     952             :       INTEGER, DIMENSION(:)                              :: map1, map2
     953             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: sizes_RI, sizes_AO
     954             :       LOGICAL, OPTIONAL                                  :: create_nl_3c
     955             :       TYPE(neighbor_list_3c_type), OPTIONAL              :: nl_3c
     956             :       TYPE(qs_environment_type), OPTIONAL, POINTER       :: qs_env
     957             : 
     958             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'create_3c_t'
     959             : 
     960             :       INTEGER                                            :: handle, nkind
     961          96 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: dist_AO_1, dist_AO_2, dist_RI
     962             :       INTEGER, DIMENSION(3)                              :: pcoord, pdims, pdims_3d
     963             :       LOGICAL                                            :: my_create_nl_3c
     964         288 :       TYPE(dbt_pgrid_type)                               :: pgrid_3d
     965             :       TYPE(distribution_3d_type)                         :: dist_3d
     966          96 :       TYPE(mp_cart_type)                                 :: mp_comm_t3c_2
     967          96 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     968             : 
     969          96 :       CALL timeset(routineN, handle)
     970             : 
     971          96 :       pdims_3d = 0
     972          96 :       CALL dbt_pgrid_create(para_env, pdims_3d, pgrid_3d)
     973             :       CALL create_3c_tensor(tensor, dist_RI, dist_AO_1, dist_AO_2, &
     974             :                             pgrid_3d, sizes_RI, sizes_AO, sizes_AO, &
     975          96 :                             map1=map1, map2=map2, name=tensor_name)
     976             : 
     977          96 :       IF (PRESENT(create_nl_3c)) THEN
     978          62 :          my_create_nl_3c = create_nl_3c
     979             :       ELSE
     980             :          my_create_nl_3c = .FALSE.
     981             :       END IF
     982             : 
     983          62 :       IF (my_create_nl_3c) THEN
     984          62 :          CALL get_qs_env(qs_env, nkind=nkind, particle_set=particle_set)
     985          62 :          CALL dbt_mp_environ_pgrid(pgrid_3d, pdims, pcoord)
     986          62 :          CALL mp_comm_t3c_2%create(pgrid_3d%mp_comm_2d, 3, pdims)
     987             :          CALL distribution_3d_create(dist_3d, dist_RI, dist_AO_1, dist_AO_2, &
     988          62 :                                      nkind, particle_set, mp_comm_t3c_2, own_comm=.TRUE.)
     989             : 
     990             :          CALL build_3c_neighbor_lists(nl_3c, &
     991             :                                       qs_env%bs_env%basis_set_RI, &
     992             :                                       qs_env%bs_env%basis_set_AO, &
     993             :                                       qs_env%bs_env%basis_set_AO, &
     994             :                                       dist_3d, qs_env%bs_env%ri_metric, &
     995          62 :                                       "GW_3c_nl", qs_env, own_dist=.TRUE.)
     996             :       END IF
     997             : 
     998          96 :       DEALLOCATE (dist_RI, dist_AO_1, dist_AO_2)
     999          96 :       CALL dbt_pgrid_destroy(pgrid_3d)
    1000             : 
    1001          96 :       CALL timestop(handle)
    1002             : 
    1003         192 :    END SUBROUTINE create_3c_t
    1004             : 
    1005             : ! **************************************************************************************************
    1006             : !> \brief ...
    1007             : !> \param bs_env ...
    1008             : ! **************************************************************************************************
    1009          34 :    SUBROUTINE init_interaction_radii(bs_env)
    1010             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1011             : 
    1012             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'init_interaction_radii'
    1013             : 
    1014             :       INTEGER                                            :: handle, ibasis
    1015             :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis, ri_basis
    1016             : 
    1017          34 :       CALL timeset(routineN, handle)
    1018             : 
    1019          90 :       DO ibasis = 1, SIZE(bs_env%basis_set_AO)
    1020             : 
    1021          56 :          orb_basis => bs_env%basis_set_AO(ibasis)%gto_basis_set
    1022          56 :          CALL init_interaction_radii_orb_basis(orb_basis, bs_env%eps_filter)
    1023             : 
    1024          56 :          ri_basis => bs_env%basis_set_RI(ibasis)%gto_basis_set
    1025          90 :          CALL init_interaction_radii_orb_basis(ri_basis, bs_env%eps_filter)
    1026             : 
    1027             :       END DO
    1028             : 
    1029          34 :       CALL timestop(handle)
    1030             : 
    1031          34 :    END SUBROUTINE init_interaction_radii
    1032             : 
    1033             : ! **************************************************************************************************
    1034             : !> \brief ...
    1035             : !> \param t_3c_int ...
    1036             : !> \param max_dist_AO_atoms ...
    1037             : !> \param qs_env ...
    1038             : ! **************************************************************************************************
    1039          60 :    SUBROUTINE get_max_dist_AO_atoms(t_3c_int, max_dist_AO_atoms, qs_env)
    1040             :       TYPE(dbt_type)                                     :: t_3c_int
    1041             :       REAL(KIND=dp)                                      :: max_dist_AO_atoms
    1042             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1043             : 
    1044             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_max_dist_AO_atoms'
    1045             : 
    1046             :       INTEGER                                            :: atom_1, atom_2, handle, num_cells
    1047             :       INTEGER, DIMENSION(3)                              :: atom_ind
    1048          60 :       INTEGER, DIMENSION(:, :), POINTER                  :: index_to_cell
    1049             :       REAL(KIND=dp)                                      :: abs_rab
    1050             :       REAL(KIND=dp), DIMENSION(3)                        :: rab
    1051             :       TYPE(cell_type), POINTER                           :: cell
    1052             :       TYPE(dbt_iterator_type)                            :: iter
    1053             :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1054          60 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1055             : 
    1056          60 :       CALL timeset(routineN, handle)
    1057             : 
    1058          60 :       NULLIFY (cell, particle_set, para_env)
    1059          60 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set, para_env=para_env)
    1060             : 
    1061             : !$OMP PARALLEL DEFAULT(NONE) &
    1062             : !$OMP SHARED(t_3c_int, max_dist_AO_atoms, num_cells, index_to_cell, particle_set, cell) &
    1063          60 : !$OMP PRIVATE(iter,atom_ind,rab, abs_rab, atom_1, atom_2)
    1064             :       CALL dbt_iterator_start(iter, t_3c_int)
    1065             :       DO WHILE (dbt_iterator_blocks_left(iter))
    1066             :          CALL dbt_iterator_next_block(iter, atom_ind)
    1067             : 
    1068             :          atom_1 = atom_ind(2)
    1069             :          atom_2 = atom_ind(3)
    1070             : 
    1071             :          rab = pbc(particle_set(atom_1)%r(1:3), particle_set(atom_2)%r(1:3), cell)
    1072             : 
    1073             :          abs_rab = SQRT(rab(1)**2 + rab(2)**2 + rab(3)**2)
    1074             : 
    1075             :          max_dist_AO_atoms = MAX(max_dist_AO_atoms, abs_rab)
    1076             : 
    1077             :       END DO
    1078             :       CALL dbt_iterator_stop(iter)
    1079             : !$OMP END PARALLEL
    1080             : 
    1081          60 :       CALL para_env%max(max_dist_AO_atoms)
    1082             : 
    1083          60 :       CALL timestop(handle)
    1084             : 
    1085          60 :    END SUBROUTINE get_max_dist_AO_atoms
    1086             : 
    1087             : ! **************************************************************************************************
    1088             : !> \brief ...
    1089             : !> \param bs_env ...
    1090             : ! **************************************************************************************************
    1091          28 :    SUBROUTINE set_sparsity_parallelization_parameters(bs_env)
    1092             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1093             : 
    1094             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_sparsity_parallelization_parameters'
    1095             : 
    1096             :       INTEGER :: handle, i_ivl, IL_ivl, j_ivl, n_atom_per_IL_ivl, n_atom_per_ivl, n_intervals_i, &
    1097             :          n_intervals_inner_loop_atoms, n_intervals_j, u
    1098             :       INTEGER(KIND=int_8)                                :: input_memory_per_proc
    1099             : 
    1100          28 :       CALL timeset(routineN, handle)
    1101             : 
    1102             :       ! heuristic parameter to prevent out of memory
    1103          28 :       bs_env%safety_factor_memory = 0.10_dp
    1104             : 
    1105          28 :       input_memory_per_proc = INT(bs_env%input_memory_per_proc_GB*1.0E9_dp, KIND=int_8)
    1106             : 
    1107             :       ! choose atomic range for λ ("i_atom"), ν ("j_atom") in
    1108             :       ! M_λνP(iτ) = sum_µ (µν|P) G^occ_µλ(i|τ|,k=0)
    1109             :       ! N_νλQ(iτ) = sum_σ (σλ|Q) G^vir_σν(i|τ|,k=0)
    1110             :       ! such that M and N fit into the memory
    1111             :       n_atom_per_ivl = INT(SQRT(bs_env%safety_factor_memory*input_memory_per_proc &
    1112             :                                 *bs_env%group_size_tensor/24/bs_env%n_RI &
    1113          28 :                                 /SQRT(bs_env%occupation_3c_int)))/bs_env%max_AO_bf_per_atom
    1114             : 
    1115          28 :       n_intervals_i = (bs_env%n_atom_i - 1)/n_atom_per_ivl + 1
    1116          28 :       n_intervals_j = (bs_env%n_atom_j - 1)/n_atom_per_ivl + 1
    1117             : 
    1118          28 :       bs_env%n_atom_per_interval_ij = n_atom_per_ivl
    1119          28 :       bs_env%n_intervals_i = n_intervals_i
    1120          28 :       bs_env%n_intervals_j = n_intervals_j
    1121             : 
    1122          84 :       ALLOCATE (bs_env%i_atom_intervals(2, n_intervals_i))
    1123          84 :       ALLOCATE (bs_env%j_atom_intervals(2, n_intervals_j))
    1124             : 
    1125          56 :       DO i_ivl = 1, n_intervals_i
    1126          28 :          bs_env%i_atom_intervals(1, i_ivl) = (i_ivl - 1)*n_atom_per_ivl + bs_env%atoms_i(1)
    1127             :          bs_env%i_atom_intervals(2, i_ivl) = MIN(i_ivl*n_atom_per_ivl + bs_env%atoms_i(1) - 1, &
    1128          56 :                                                  bs_env%atoms_i(2))
    1129             :       END DO
    1130             : 
    1131          56 :       DO j_ivl = 1, n_intervals_j
    1132          28 :          bs_env%j_atom_intervals(1, j_ivl) = (j_ivl - 1)*n_atom_per_ivl + bs_env%atoms_j(1)
    1133             :          bs_env%j_atom_intervals(2, j_ivl) = MIN(j_ivl*n_atom_per_ivl + bs_env%atoms_j(1) - 1, &
    1134          56 :                                                  bs_env%atoms_j(2))
    1135             :       END DO
    1136             : 
    1137         112 :       ALLOCATE (bs_env%skip_Sigma_occ(n_intervals_i, n_intervals_j))
    1138          84 :       ALLOCATE (bs_env%skip_Sigma_vir(n_intervals_i, n_intervals_j))
    1139          84 :       bs_env%skip_Sigma_occ(:, :) = .FALSE.
    1140          84 :       bs_env%skip_Sigma_vir(:, :) = .FALSE.
    1141             : 
    1142             :       ! choose atomic range for µ and σ ("inner loop (IL) atom") in
    1143             :       ! M_λνP(iτ) = sum_µ (µν|P) G^occ_µλ(i|τ|,k=0)
    1144             :       ! N_νλQ(iτ) = sum_σ (σλ|Q) G^vir_σν(i|τ|,k=0)
    1145             :       n_atom_per_IL_ivl = MIN(INT(bs_env%safety_factor_memory*input_memory_per_proc &
    1146             :                                   *bs_env%group_size_tensor/n_atom_per_ivl &
    1147             :                                   /bs_env%max_AO_bf_per_atom &
    1148             :                                   /bs_env%n_RI/8/SQRT(bs_env%occupation_3c_int) &
    1149          28 :                                   /bs_env%max_AO_bf_per_atom), bs_env%n_atom)
    1150             : 
    1151          28 :       n_intervals_inner_loop_atoms = (bs_env%n_atom - 1)/n_atom_per_IL_ivl + 1
    1152             : 
    1153          28 :       bs_env%n_atom_per_IL_interval = n_atom_per_IL_ivl
    1154          28 :       bs_env%n_intervals_inner_loop_atoms = n_intervals_inner_loop_atoms
    1155             : 
    1156          84 :       ALLOCATE (bs_env%inner_loop_atom_intervals(2, n_intervals_inner_loop_atoms))
    1157          56 :       DO IL_ivl = 1, n_intervals_inner_loop_atoms
    1158          28 :          bs_env%inner_loop_atom_intervals(1, IL_ivl) = (IL_ivl - 1)*n_atom_per_IL_ivl + 1
    1159          56 :          bs_env%inner_loop_atom_intervals(2, IL_ivl) = MIN(IL_ivl*n_atom_per_IL_ivl, bs_env%n_atom)
    1160             :       END DO
    1161             : 
    1162          28 :       u = bs_env%unit_nr
    1163          28 :       IF (u > 0) THEN
    1164          14 :          WRITE (u, '(T2,A)') ''
    1165          14 :          WRITE (u, '(T2,A,I33)') 'Number of i and j atoms in M_λνP(τ), N_νλQ(τ):', n_atom_per_ivl
    1166          14 :          WRITE (u, '(T2,A,I18)') 'Number of inner loop atoms for µ in M_λνP = sum_µ (µν|P) G_µλ', &
    1167          28 :             n_atom_per_IL_ivl
    1168             :       END IF
    1169             : 
    1170          28 :       CALL timestop(handle)
    1171             : 
    1172          28 :    END SUBROUTINE set_sparsity_parallelization_parameters
    1173             : 
    1174             : ! **************************************************************************************************
    1175             : !> \brief ...
    1176             : !> \param qs_env ...
    1177             : !> \param bs_env ...
    1178             : ! **************************************************************************************************
    1179          28 :    SUBROUTINE check_for_restart_files(qs_env, bs_env)
    1180             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1181             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1182             : 
    1183             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'check_for_restart_files'
    1184             : 
    1185             :       CHARACTER(LEN=9)                                   :: frmt
    1186             :       CHARACTER(LEN=default_string_length)               :: f_chi, f_S_n, f_S_p, f_S_x, f_W_t, &
    1187             :                                                             prefix, project_name
    1188             :       INTEGER                                            :: handle, i_spin, i_t_or_w, ind, n_spin, &
    1189             :                                                             num_time_freq_points
    1190             :       LOGICAL                                            :: chi_exists, Sigma_neg_time_exists, &
    1191             :                                                             Sigma_pos_time_exists, &
    1192             :                                                             Sigma_x_spin_exists, W_time_exists
    1193             :       TYPE(cp_logger_type), POINTER                      :: logger
    1194             :       TYPE(section_vals_type), POINTER                   :: input, print_key
    1195             : 
    1196          28 :       CALL timeset(routineN, handle)
    1197             : 
    1198          28 :       num_time_freq_points = bs_env%num_time_freq_points
    1199          28 :       n_spin = bs_env%n_spin
    1200             : 
    1201          84 :       ALLOCATE (bs_env%read_chi(num_time_freq_points))
    1202          56 :       ALLOCATE (bs_env%calc_chi(num_time_freq_points))
    1203         112 :       ALLOCATE (bs_env%Sigma_c_exists(num_time_freq_points, n_spin))
    1204             : 
    1205          28 :       CALL get_qs_env(qs_env, input=input)
    1206             : 
    1207          28 :       logger => cp_get_default_logger()
    1208          28 :       print_key => section_vals_get_subs_vals(input, 'PROPERTIES%BANDSTRUCTURE%GW%PRINT%RESTART')
    1209             :       project_name = cp_print_key_generate_filename(logger, print_key, extension="", &
    1210          28 :                                                     my_local=.FALSE.)
    1211          28 :       WRITE (prefix, '(2A)') TRIM(project_name), "-RESTART_"
    1212          28 :       bs_env%prefix = prefix
    1213             : 
    1214          28 :       bs_env%all_W_exist = .TRUE.
    1215             : 
    1216         412 :       DO i_t_or_w = 1, num_time_freq_points
    1217             : 
    1218         384 :          IF (i_t_or_w < 10) THEN
    1219         240 :             WRITE (frmt, '(A)') '(3A,I1,A)'
    1220         240 :             WRITE (f_chi, frmt) TRIM(prefix), bs_env%chi_name, "_0", i_t_or_w, ".matrix"
    1221         240 :             WRITE (f_W_t, frmt) TRIM(prefix), bs_env%W_time_name, "_0", i_t_or_w, ".matrix"
    1222         144 :          ELSE IF (i_t_or_w < 100) THEN
    1223         144 :             WRITE (frmt, '(A)') '(3A,I2,A)'
    1224         144 :             WRITE (f_chi, frmt) TRIM(prefix), bs_env%chi_name, "_", i_t_or_w, ".matrix"
    1225         144 :             WRITE (f_W_t, frmt) TRIM(prefix), bs_env%W_time_name, "_", i_t_or_w, ".matrix"
    1226             :          ELSE
    1227           0 :             CPABORT('Please implement more than 99 time/frequency points.')
    1228             :          END IF
    1229             : 
    1230         384 :          INQUIRE (file=TRIM(f_chi), exist=chi_exists)
    1231         384 :          INQUIRE (file=TRIM(f_W_t), exist=W_time_exists)
    1232             : 
    1233         384 :          bs_env%read_chi(i_t_or_w) = chi_exists
    1234         384 :          bs_env%calc_chi(i_t_or_w) = .NOT. chi_exists
    1235             : 
    1236         384 :          bs_env%all_W_exist = bs_env%all_W_exist .AND. W_time_exists
    1237             : 
    1238             :          ! the self-energy is spin-dependent
    1239         876 :          DO i_spin = 1, n_spin
    1240             : 
    1241         464 :             ind = i_t_or_w + (i_spin - 1)*num_time_freq_points
    1242             : 
    1243         464 :             IF (ind < 10) THEN
    1244         240 :                WRITE (frmt, '(A)') '(3A,I1,A)'
    1245         240 :                WRITE (f_S_p, frmt) TRIM(prefix), bs_env%Sigma_p_name, "_0", ind, ".matrix"
    1246         240 :                WRITE (f_S_n, frmt) TRIM(prefix), bs_env%Sigma_n_name, "_0", ind, ".matrix"
    1247         224 :             ELSE IF (i_t_or_w < 100) THEN
    1248         224 :                WRITE (frmt, '(A)') '(3A,I2,A)'
    1249         224 :                WRITE (f_S_p, frmt) TRIM(prefix), bs_env%Sigma_p_name, "_", ind, ".matrix"
    1250         224 :                WRITE (f_S_n, frmt) TRIM(prefix), bs_env%Sigma_n_name, "_", ind, ".matrix"
    1251             :             END IF
    1252             : 
    1253         464 :             INQUIRE (file=TRIM(f_S_p), exist=Sigma_pos_time_exists)
    1254         464 :             INQUIRE (file=TRIM(f_S_n), exist=Sigma_neg_time_exists)
    1255             : 
    1256             :             bs_env%Sigma_c_exists(i_t_or_w, i_spin) = Sigma_pos_time_exists .AND. &
    1257        1072 :                                                       Sigma_neg_time_exists
    1258             : 
    1259             :          END DO
    1260             : 
    1261             :       END DO
    1262             : 
    1263          28 :       IF (bs_env%all_W_exist) THEN
    1264         192 :          bs_env%read_chi(:) = .FALSE.
    1265         192 :          bs_env%calc_chi(:) = .FALSE.
    1266             :       END IF
    1267             : 
    1268          28 :       bs_env%Sigma_x_exists = .TRUE.
    1269          64 :       DO i_spin = 1, n_spin
    1270          36 :          WRITE (f_S_x, '(3A,I1,A)') TRIM(prefix), bs_env%Sigma_x_name, "_0", i_spin, ".matrix"
    1271          36 :          INQUIRE (file=TRIM(f_S_x), exist=Sigma_x_spin_exists)
    1272          82 :          bs_env%Sigma_x_exists = bs_env%Sigma_x_exists .AND. Sigma_x_spin_exists
    1273             :       END DO
    1274             : 
    1275          28 :       CALL timestop(handle)
    1276             : 
    1277          28 :    END SUBROUTINE check_for_restart_files
    1278             : 
    1279             : ! **************************************************************************************************
    1280             : !> \brief ...
    1281             : !> \param qs_env ...
    1282             : !> \param bs_env ...
    1283             : ! **************************************************************************************************
    1284          34 :    SUBROUTINE set_parallelization_parameters(qs_env, bs_env)
    1285             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1286             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1287             : 
    1288             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_parallelization_parameters'
    1289             : 
    1290             :       INTEGER                                            :: color_sub, dummy_1, dummy_2, handle, &
    1291             :                                                             num_pe, num_t_groups, u
    1292             :       INTEGER(KIND=int_8)                                :: mem
    1293             :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1294             : 
    1295          34 :       CALL timeset(routineN, handle)
    1296             : 
    1297          34 :       CALL get_qs_env(qs_env, para_env=para_env)
    1298             : 
    1299          34 :       num_pe = para_env%num_pe
    1300             :       ! if not already set, use all processors for the group (for large-cell GW, performance
    1301             :       ! seems to be best for a single group with all MPI processes per group)
    1302          34 :       IF (bs_env%group_size_tensor < 0 .OR. bs_env%group_size_tensor > num_pe) &
    1303          28 :          bs_env%group_size_tensor = num_pe
    1304             : 
    1305             :       ! group_size_tensor must divide num_pe without rest; otherwise everything will be complicated
    1306          34 :       IF (MODULO(num_pe, bs_env%group_size_tensor) .NE. 0) THEN
    1307           0 :          CALL find_good_group_size(num_pe, bs_env%group_size_tensor)
    1308             :       END IF
    1309             : 
    1310             :       ! para_env_tensor for tensor subgroups
    1311          34 :       color_sub = para_env%mepos/bs_env%group_size_tensor
    1312          34 :       bs_env%tensor_group_color = color_sub
    1313             : 
    1314          34 :       ALLOCATE (bs_env%para_env_tensor)
    1315          34 :       CALL bs_env%para_env_tensor%from_split(para_env, color_sub)
    1316             : 
    1317          34 :       num_t_groups = para_env%num_pe/bs_env%group_size_tensor
    1318          34 :       bs_env%num_tensor_groups = num_t_groups
    1319             : 
    1320             :       CALL get_i_j_atoms(bs_env%atoms_i, bs_env%atoms_j, bs_env%n_atom_i, bs_env%n_atom_j, &
    1321          34 :                          color_sub, bs_env)
    1322             : 
    1323         102 :       ALLOCATE (bs_env%atoms_i_t_group(2, num_t_groups))
    1324         102 :       ALLOCATE (bs_env%atoms_j_t_group(2, num_t_groups))
    1325          74 :       DO color_sub = 0, num_t_groups - 1
    1326             :          CALL get_i_j_atoms(bs_env%atoms_i_t_group(1:2, color_sub + 1), &
    1327             :                             bs_env%atoms_j_t_group(1:2, color_sub + 1), &
    1328          74 :                             dummy_1, dummy_2, color_sub, bs_env)
    1329             :       END DO
    1330             : 
    1331          34 :       CALL m_memory(mem)
    1332          34 :       CALL bs_env%para_env%max(mem)
    1333             : 
    1334          34 :       u = bs_env%unit_nr
    1335          34 :       IF (u > 0) THEN
    1336          17 :          WRITE (u, '(T2,A,I47)') 'Group size for tensor operations', bs_env%group_size_tensor
    1337          17 :          IF (bs_env%group_size_tensor > 1 .AND. bs_env%n_atom < 5) THEN
    1338          14 :             WRITE (u, '(T2,A)') 'The requested group size is > 1 which can lead to bad performance.'
    1339          14 :             WRITE (u, '(T2,A)') 'Using more memory per MPI process might improve performance.'
    1340          14 :             WRITE (u, '(T2,A)') '(Also increase MEMORY_PER_PROC when using more memory per process.)'
    1341             :          END IF
    1342             :       END IF
    1343             : 
    1344          34 :       CALL timestop(handle)
    1345             : 
    1346          68 :    END SUBROUTINE set_parallelization_parameters
    1347             : 
    1348             : ! **************************************************************************************************
    1349             : !> \brief ...
    1350             : !> \param num_pe ...
    1351             : !> \param group_size ...
    1352             : ! **************************************************************************************************
    1353           0 :    SUBROUTINE find_good_group_size(num_pe, group_size)
    1354             : 
    1355             :       INTEGER                                            :: num_pe, group_size
    1356             : 
    1357             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'find_good_group_size'
    1358             : 
    1359             :       INTEGER                                            :: group_size_minus, group_size_orig, &
    1360             :                                                             group_size_plus, handle, i_diff
    1361             : 
    1362           0 :       CALL timeset(routineN, handle)
    1363             : 
    1364           0 :       group_size_orig = group_size
    1365             : 
    1366           0 :       DO i_diff = 1, num_pe
    1367             : 
    1368           0 :          group_size_minus = group_size - i_diff
    1369             : 
    1370           0 :          IF (MODULO(num_pe, group_size_minus) == 0 .AND. group_size_minus > 0) THEN
    1371           0 :             group_size = group_size_minus
    1372           0 :             EXIT
    1373             :          END IF
    1374             : 
    1375           0 :          group_size_plus = group_size + i_diff
    1376             : 
    1377           0 :          IF (MODULO(num_pe, group_size_plus) == 0 .AND. group_size_plus <= num_pe) THEN
    1378           0 :             group_size = group_size_plus
    1379           0 :             EXIT
    1380             :          END IF
    1381             : 
    1382             :       END DO
    1383             : 
    1384           0 :       IF (group_size_orig == group_size) CPABORT("Group size error")
    1385             : 
    1386           0 :       CALL timestop(handle)
    1387             : 
    1388           0 :    END SUBROUTINE find_good_group_size
    1389             : 
    1390             : ! **************************************************************************************************
    1391             : !> \brief ...
    1392             : !> \param atoms_i ...
    1393             : !> \param atoms_j ...
    1394             : !> \param n_atom_i ...
    1395             : !> \param n_atom_j ...
    1396             : !> \param color_sub ...
    1397             : !> \param bs_env ...
    1398             : ! **************************************************************************************************
    1399          74 :    SUBROUTINE get_i_j_atoms(atoms_i, atoms_j, n_atom_i, n_atom_j, color_sub, bs_env)
    1400             : 
    1401             :       INTEGER, DIMENSION(2)                              :: atoms_i, atoms_j
    1402             :       INTEGER                                            :: n_atom_i, n_atom_j, color_sub
    1403             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1404             : 
    1405             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_i_j_atoms'
    1406             : 
    1407             :       INTEGER                                            :: handle, i_atoms_per_group, i_group, &
    1408             :                                                             ipcol, ipcol_loop, iprow, iprow_loop, &
    1409             :                                                             j_atoms_per_group, npcol, nprow
    1410             : 
    1411          74 :       CALL timeset(routineN, handle)
    1412             : 
    1413             :       ! create a square mesh of tensor groups for iatom and jatom; code from blacs_env_create
    1414          74 :       CALL square_mesh(nprow, npcol, bs_env%num_tensor_groups)
    1415             : 
    1416          74 :       i_group = 0
    1417         148 :       DO ipcol_loop = 0, npcol - 1
    1418         240 :          DO iprow_loop = 0, nprow - 1
    1419          92 :             IF (i_group == color_sub) THEN
    1420          74 :                iprow = iprow_loop
    1421          74 :                ipcol = ipcol_loop
    1422             :             END IF
    1423         166 :             i_group = i_group + 1
    1424             :          END DO
    1425             :       END DO
    1426             : 
    1427          74 :       IF (MODULO(bs_env%n_atom, nprow) == 0) THEN
    1428          68 :          i_atoms_per_group = bs_env%n_atom/nprow
    1429             :       ELSE
    1430           6 :          i_atoms_per_group = bs_env%n_atom/nprow + 1
    1431             :       END IF
    1432             : 
    1433          74 :       IF (MODULO(bs_env%n_atom, npcol) == 0) THEN
    1434          74 :          j_atoms_per_group = bs_env%n_atom/npcol
    1435             :       ELSE
    1436           0 :          j_atoms_per_group = bs_env%n_atom/npcol + 1
    1437             :       END IF
    1438             : 
    1439          74 :       atoms_i(1) = iprow*i_atoms_per_group + 1
    1440          74 :       atoms_i(2) = MIN((iprow + 1)*i_atoms_per_group, bs_env%n_atom)
    1441          74 :       n_atom_i = atoms_i(2) - atoms_i(1) + 1
    1442             : 
    1443          74 :       atoms_j(1) = ipcol*j_atoms_per_group + 1
    1444          74 :       atoms_j(2) = MIN((ipcol + 1)*j_atoms_per_group, bs_env%n_atom)
    1445          74 :       n_atom_j = atoms_j(2) - atoms_j(1) + 1
    1446             : 
    1447          74 :       CALL timestop(handle)
    1448             : 
    1449          74 :    END SUBROUTINE get_i_j_atoms
    1450             : 
    1451             : ! **************************************************************************************************
    1452             : !> \brief ...
    1453             : !> \param nprow ...
    1454             : !> \param npcol ...
    1455             : !> \param nproc ...
    1456             : ! **************************************************************************************************
    1457          74 :    SUBROUTINE square_mesh(nprow, npcol, nproc)
    1458             :       INTEGER                                            :: nprow, npcol, nproc
    1459             : 
    1460             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'square_mesh'
    1461             : 
    1462             :       INTEGER                                            :: gcd_max, handle, ipe, jpe
    1463             : 
    1464          74 :       CALL timeset(routineN, handle)
    1465             : 
    1466          74 :       gcd_max = -1
    1467         166 :       DO ipe = 1, CEILING(SQRT(REAL(nproc, dp)))
    1468          92 :          jpe = nproc/ipe
    1469          92 :          IF (ipe*jpe .NE. nproc) CYCLE
    1470         166 :          IF (gcd(ipe, jpe) >= gcd_max) THEN
    1471          92 :             nprow = ipe
    1472          92 :             npcol = jpe
    1473          92 :             gcd_max = gcd(ipe, jpe)
    1474             :          END IF
    1475             :       END DO
    1476             : 
    1477          74 :       CALL timestop(handle)
    1478             : 
    1479          74 :    END SUBROUTINE square_mesh
    1480             : 
    1481             : ! **************************************************************************************************
    1482             : !> \brief ...
    1483             : !> \param bs_env ...
    1484             : !> \param qs_env ...
    1485             : ! **************************************************************************************************
    1486          34 :    SUBROUTINE set_heuristic_parameters(bs_env, qs_env)
    1487             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1488             :       TYPE(qs_environment_type), OPTIONAL, POINTER       :: qs_env
    1489             : 
    1490             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_heuristic_parameters'
    1491             : 
    1492             :       INTEGER                                            :: handle, u
    1493             :       LOGICAL                                            :: do_BvK_cell
    1494             : 
    1495          34 :       CALL timeset(routineN, handle)
    1496             : 
    1497             :       ! for generating numerically stable minimax Fourier integration weights
    1498          34 :       bs_env%num_points_per_magnitude = 200
    1499             : 
    1500             :       ! for periodic systems and for 20 minimax points, we use a regularized minimax mesh
    1501             :       ! (from experience: regularized minimax meshes converges faster for periodic systems
    1502             :       !  and for 20 pts)
    1503         136 :       IF (SUM(bs_env%periodic) .NE. 0 .OR. bs_env%num_time_freq_points == 20) THEN
    1504          34 :          bs_env%regularization_minimax = 1.0E-6_dp
    1505             :       ELSE
    1506           0 :          bs_env%regularization_minimax = 0.0_dp
    1507             :       END IF
    1508             : 
    1509          34 :       bs_env%stabilize_exp = 70.0_dp
    1510          34 :       bs_env%eps_atom_grid_2d_mat = 1.0E-50_dp
    1511             : 
    1512             :       ! only use interval ω in [0, 1 Ha] (1 Hartree = 27.211 eV) for virt, and ω in [-1 Ha, 0]
    1513             :       ! for occ for use in analytic continuation of self-energy Σ^c_n(iω,k) -> Σ^c_n(ϵ,k)
    1514          34 :       bs_env%freq_max_fit = 1.0_dp
    1515             : 
    1516             :       ! use a 16-parameter Padé fit
    1517          34 :       bs_env%nparam_pade = 16
    1518             : 
    1519             :       ! resolution of the identity with the truncated Coulomb metric, cutoff radius 3 Angström
    1520          34 :       bs_env%ri_metric%potential_type = do_potential_truncated
    1521          34 :       bs_env%ri_metric%omega = 0.0_dp
    1522             :       ! cutoff radius is specified in the input
    1523          34 :       bs_env%ri_metric%filename = "t_c_g.dat"
    1524             : 
    1525          34 :       bs_env%eps_eigval_mat_RI = 0.0_dp
    1526             : 
    1527          34 :       IF (bs_env%input_regularization_RI > -1.0E-12_dp) THEN
    1528           0 :          bs_env%regularization_RI = bs_env%input_regularization_RI
    1529             :       ELSE
    1530             :          ! default case:
    1531             : 
    1532             :          ! 1. for periodic systems, we use the regularized resolution of the identity per default
    1533          34 :          bs_env%regularization_RI = 1.0E-2_dp
    1534             : 
    1535             :          ! 2. for molecules, no regularization is necessary
    1536         136 :          IF (SUM(bs_env%periodic) == 0) bs_env%regularization_RI = 0.0_dp
    1537             : 
    1538             :       END IF
    1539             : 
    1540             :       ! truncated Coulomb operator for exchange self-energy
    1541             :       ! (see details in Guidon, VandeVondele, Hutter, JCTC 5, 3010 (2009) and references therein)
    1542          34 :       do_BvK_cell = bs_env%small_cell_full_kp_or_large_cell_Gamma == small_cell_full_kp
    1543             :       CALL trunc_coulomb_for_exchange(qs_env, bs_env%trunc_coulomb, &
    1544             :                                       rel_cutoff_trunc_coulomb_ri_x=0.5_dp, &
    1545             :                                       cell_grid=bs_env%cell_grid_scf_desymm, &
    1546          34 :                                       do_BvK_cell=do_BvK_cell)
    1547             : 
    1548             :       ! for small-cell GW, we need more cells than normally used by the filter bs_env%eps_filter
    1549             :       ! (in particular for computing the self-energy because of higher number of cells needed)
    1550          34 :       bs_env%heuristic_filter_factor = 1.0E-4
    1551             : 
    1552          34 :       u = bs_env%unit_nr
    1553          34 :       IF (u > 0) THEN
    1554          17 :          WRITE (u, FMT="(T2,2A,F21.1,A)") "Cutoff radius for the truncated Coulomb ", &
    1555          34 :             "operator in Σ^x:", bs_env%trunc_coulomb%cutoff_radius*angstrom, " Å"
    1556          17 :          WRITE (u, FMT="(T2,2A,F15.1,A)") "Cutoff radius for the truncated Coulomb ", &
    1557          34 :             "operator in RI metric:", bs_env%ri_metric%cutoff_radius*angstrom, " Å"
    1558          17 :          WRITE (u, FMT="(T2,A,ES48.1)") "Regularization parameter of RI ", bs_env%regularization_RI
    1559          17 :          WRITE (u, FMT="(T2,A,I53)") "Lattice sum size for V(k):", bs_env%size_lattice_sum_V
    1560             :       END IF
    1561             : 
    1562          34 :       CALL timestop(handle)
    1563             : 
    1564          34 :    END SUBROUTINE set_heuristic_parameters
    1565             : 
    1566             : ! **************************************************************************************************
    1567             : !> \brief ...
    1568             : !> \param bs_env ...
    1569             : ! **************************************************************************************************
    1570          34 :    SUBROUTINE print_header_and_input_parameters(bs_env)
    1571             : 
    1572             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1573             : 
    1574             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'print_header_and_input_parameters'
    1575             : 
    1576             :       INTEGER                                            :: handle, u
    1577             : 
    1578          34 :       CALL timeset(routineN, handle)
    1579             : 
    1580          34 :       u = bs_env%unit_nr
    1581             : 
    1582          34 :       IF (u > 0) THEN
    1583          17 :          WRITE (u, *) ' '
    1584          17 :          WRITE (u, '(T2,2A)') '------------------------------------------------', &
    1585          34 :             '-------------------------------'
    1586          17 :          WRITE (u, '(T2,2A)') '-                                               ', &
    1587          34 :             '                              -'
    1588          17 :          WRITE (u, '(T2,2A)') '-                              GW CALCULATION   ', &
    1589          34 :             '                              -'
    1590          17 :          WRITE (u, '(T2,2A)') '-                                               ', &
    1591          34 :             '                              -'
    1592          17 :          WRITE (u, '(T2,2A)') '------------------------------------------------', &
    1593          34 :             '-------------------------------'
    1594          17 :          WRITE (u, '(T2,A)') ' '
    1595          17 :          WRITE (u, '(T2,A,I45)') 'Input: Number of time/freq. points', bs_env%num_time_freq_points
    1596          17 :          WRITE (u, '(T2,A,ES27.1)') 'Input: Filter threshold for sparse tensor operations', &
    1597          34 :             bs_env%eps_filter
    1598          17 :          WRITE (bs_env%unit_nr, FMT="(T2,A,L62)") "Apply Hedin shift", bs_env%do_hedin_shift
    1599             :       END IF
    1600             : 
    1601          34 :       CALL timestop(handle)
    1602             : 
    1603          34 :    END SUBROUTINE print_header_and_input_parameters
    1604             : 
    1605             : ! **************************************************************************************************
    1606             : !> \brief ...
    1607             : !> \param qs_env ...
    1608             : !> \param bs_env ...
    1609             : ! **************************************************************************************************
    1610          68 :    SUBROUTINE compute_V_xc(qs_env, bs_env)
    1611             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1612             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1613             : 
    1614             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_V_xc'
    1615             : 
    1616             :       INTEGER                                            :: handle, img, ispin, myfun, nimages
    1617             :       LOGICAL                                            :: hf_present
    1618             :       REAL(KIND=dp)                                      :: energy_ex, energy_exc, energy_total, &
    1619             :                                                             myfraction
    1620          34 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: mat_ks_without_v_xc
    1621          34 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_kp
    1622             :       TYPE(dft_control_type), POINTER                    :: dft_control
    1623             :       TYPE(qs_energy_type), POINTER                      :: energy
    1624             :       TYPE(section_vals_type), POINTER                   :: hf_section, input, xc_section
    1625             : 
    1626          34 :       CALL timeset(routineN, handle)
    1627             : 
    1628          34 :       CALL get_qs_env(qs_env, input=input, energy=energy, dft_control=dft_control)
    1629             : 
    1630             :       ! previously, dft_control%nimages set to # neighbor cells, revert for Γ-only KS matrix
    1631          34 :       nimages = dft_control%nimages
    1632          34 :       dft_control%nimages = bs_env%nimages_scf
    1633             : 
    1634             :       ! we need to reset XC functional, therefore, get XC input
    1635          34 :       xc_section => section_vals_get_subs_vals(input, "DFT%XC")
    1636          34 :       CALL section_vals_val_get(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", i_val=myfun)
    1637          34 :       CALL section_vals_val_set(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", i_val=xc_none)
    1638             :       ! IF (ASSOCIATED(section_vals_get_subs_vals(xc_section, "HF", can_return_null=.TRUE.))) THEN
    1639          34 :       hf_section => section_vals_get_subs_vals(input, "DFT%XC%HF", can_return_null=.TRUE.)
    1640          34 :       hf_present = .FALSE.
    1641          34 :       IF (ASSOCIATED(hf_section)) THEN
    1642          34 :          CALL section_vals_get(hf_section, explicit=hf_present)
    1643             :       END IF
    1644          34 :       IF (hf_present) THEN
    1645             :          ! Special case for handling hfx
    1646           0 :          CALL section_vals_val_get(xc_section, "HF%FRACTION", r_val=myfraction)
    1647           0 :          CALL section_vals_val_set(xc_section, "HF%FRACTION", r_val=0.0_dp)
    1648             :       END IF
    1649             : 
    1650             :       ! save the energy before the energy gets updated
    1651          34 :       energy_total = energy%total
    1652          34 :       energy_exc = energy%exc
    1653          34 :       energy_ex = energy%ex
    1654             : 
    1655          62 :       SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
    1656             :       CASE (large_cell_Gamma)
    1657             : 
    1658          28 :          NULLIFY (mat_ks_without_v_xc)
    1659          28 :          CALL dbcsr_allocate_matrix_set(mat_ks_without_v_xc, bs_env%n_spin)
    1660             : 
    1661          64 :          DO ispin = 1, bs_env%n_spin
    1662          36 :             ALLOCATE (mat_ks_without_v_xc(ispin)%matrix)
    1663          64 :             IF (hf_present) THEN
    1664             :                CALL dbcsr_create(mat_ks_without_v_xc(ispin)%matrix, template=bs_env%mat_ao_ao%matrix, &
    1665           0 :                                  matrix_type=dbcsr_type_symmetric)
    1666             :             ELSE
    1667          36 :                CALL dbcsr_create(mat_ks_without_v_xc(ispin)%matrix, template=bs_env%mat_ao_ao%matrix)
    1668             :             END IF
    1669             :          END DO
    1670             : 
    1671             :          ! calculate KS-matrix without XC
    1672             :          CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., just_energy=.FALSE., &
    1673          28 :                                            ext_ks_matrix=mat_ks_without_v_xc)
    1674             : 
    1675          64 :          DO ispin = 1, bs_env%n_spin
    1676             :             ! transfer dbcsr matrix to fm
    1677          36 :             CALL cp_fm_create(bs_env%fm_V_xc_Gamma(ispin), bs_env%fm_s_Gamma%matrix_struct)
    1678          36 :             CALL copy_dbcsr_to_fm(mat_ks_without_v_xc(ispin)%matrix, bs_env%fm_V_xc_Gamma(ispin))
    1679             : 
    1680             :             ! v_xc = h_ks - h_ks(v_xc = 0)
    1681             :             CALL cp_fm_scale_and_add(alpha=-1.0_dp, matrix_a=bs_env%fm_V_xc_Gamma(ispin), &
    1682          64 :                                      beta=1.0_dp, matrix_b=bs_env%fm_ks_Gamma(ispin))
    1683             :          END DO
    1684             : 
    1685          28 :          CALL dbcsr_deallocate_matrix_set(mat_ks_without_v_xc)
    1686             : 
    1687             :       CASE (small_cell_full_kp)
    1688             : 
    1689             :          ! calculate KS-matrix without XC
    1690           6 :          CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
    1691           6 :          CALL get_qs_env(qs_env=qs_env, matrix_ks_kp=matrix_ks_kp)
    1692             : 
    1693         176 :          ALLOCATE (bs_env%fm_V_xc_R(dft_control%nimages, bs_env%n_spin))
    1694          46 :          DO ispin = 1, bs_env%n_spin
    1695         158 :             DO img = 1, dft_control%nimages
    1696             :                ! safe fm_V_xc_R in fm_matrix because saving in dbcsr matrix caused trouble...
    1697         146 :                CALL copy_dbcsr_to_fm(matrix_ks_kp(ispin, img)%matrix, bs_env%fm_work_mo(1))
    1698         146 :                CALL cp_fm_create(bs_env%fm_V_xc_R(img, ispin), bs_env%fm_work_mo(1)%matrix_struct)
    1699             :                ! store h_ks(v_xc = 0) in fm_V_xc_R
    1700             :                CALL cp_fm_scale_and_add(alpha=1.0_dp, matrix_a=bs_env%fm_V_xc_R(img, ispin), &
    1701         152 :                                         beta=1.0_dp, matrix_b=bs_env%fm_work_mo(1))
    1702             :             END DO
    1703             :          END DO
    1704             : 
    1705             :       END SELECT
    1706             : 
    1707             :       ! set back the energy
    1708          34 :       energy%total = energy_total
    1709          34 :       energy%exc = energy_exc
    1710          34 :       energy%ex = energy_ex
    1711             : 
    1712             :       ! set back nimages
    1713          34 :       dft_control%nimages = nimages
    1714             : 
    1715             :       ! set the DFT functional and HF fraction back
    1716             :       CALL section_vals_val_set(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", &
    1717          34 :                                 i_val=myfun)
    1718          34 :       IF (hf_present) THEN
    1719             :          CALL section_vals_val_set(xc_section, "HF%FRACTION", &
    1720           0 :                                    r_val=myfraction)
    1721             :       END IF
    1722             : 
    1723          34 :       IF (bs_env%small_cell_full_kp_or_large_cell_Gamma == small_cell_full_kp) THEN
    1724             :          ! calculate KS-matrix again with XC
    1725           6 :          CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
    1726          12 :          DO ispin = 1, bs_env%n_spin
    1727         158 :             DO img = 1, dft_control%nimages
    1728             :                ! store h_ks in fm_work_mo
    1729         146 :                CALL copy_dbcsr_to_fm(matrix_ks_kp(ispin, img)%matrix, bs_env%fm_work_mo(1))
    1730             :                ! v_xc = h_ks - h_ks(v_xc = 0)
    1731             :                CALL cp_fm_scale_and_add(alpha=-1.0_dp, matrix_a=bs_env%fm_V_xc_R(img, ispin), &
    1732         152 :                                         beta=1.0_dp, matrix_b=bs_env%fm_work_mo(1))
    1733             :             END DO
    1734             :          END DO
    1735             :       END IF
    1736             : 
    1737          34 :       CALL timestop(handle)
    1738             : 
    1739          34 :    END SUBROUTINE compute_V_xc
    1740             : 
    1741             : ! **************************************************************************************************
    1742             : !> \brief ...
    1743             : !> \param bs_env ...
    1744             : ! **************************************************************************************************
    1745          34 :    SUBROUTINE setup_time_and_frequency_minimax_grid(bs_env)
    1746             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1747             : 
    1748             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_time_and_frequency_minimax_grid'
    1749             : 
    1750             :       INTEGER                                            :: handle, homo, i_w, ierr, ispin, j_w, &
    1751             :                                                             n_mo, num_time_freq_points, u
    1752             :       REAL(KIND=dp)                                      :: E_max, E_max_ispin, E_min, E_min_ispin, &
    1753             :                                                             E_range, max_error_min
    1754          34 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: points_and_weights
    1755             : 
    1756          34 :       CALL timeset(routineN, handle)
    1757             : 
    1758          34 :       n_mo = bs_env%n_ao
    1759          34 :       num_time_freq_points = bs_env%num_time_freq_points
    1760             : 
    1761         102 :       ALLOCATE (bs_env%imag_freq_points(num_time_freq_points))
    1762         102 :       ALLOCATE (bs_env%imag_time_points(num_time_freq_points))
    1763         102 :       ALLOCATE (bs_env%imag_time_weights_freq_zero(num_time_freq_points))
    1764         136 :       ALLOCATE (bs_env%weights_cos_t_to_w(num_time_freq_points, num_time_freq_points))
    1765         136 :       ALLOCATE (bs_env%weights_cos_w_to_t(num_time_freq_points, num_time_freq_points))
    1766         136 :       ALLOCATE (bs_env%weights_sin_t_to_w(num_time_freq_points, num_time_freq_points))
    1767             : 
    1768             :       ! minimum and maximum difference between eigenvalues of unoccupied and an occupied MOs
    1769          34 :       E_min = 1000.0_dp
    1770          34 :       E_max = -1000.0_dp
    1771          76 :       DO ispin = 1, bs_env%n_spin
    1772          42 :          homo = bs_env%n_occ(ispin)
    1773          78 :          SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
    1774             :          CASE (large_cell_Gamma)
    1775             :             E_min_ispin = bs_env%eigenval_scf_Gamma(homo + 1, ispin) - &
    1776          36 :                           bs_env%eigenval_scf_Gamma(homo, ispin)
    1777             :             E_max_ispin = bs_env%eigenval_scf_Gamma(n_mo, ispin) - &
    1778          36 :                           bs_env%eigenval_scf_Gamma(1, ispin)
    1779             :          CASE (small_cell_full_kp)
    1780             :             E_min_ispin = MINVAL(bs_env%eigenval_scf(homo + 1, :, ispin)) - &
    1781         458 :                           MAXVAL(bs_env%eigenval_scf(homo, :, ispin))
    1782             :             E_max_ispin = MAXVAL(bs_env%eigenval_scf(n_mo, :, ispin)) - &
    1783         500 :                           MINVAL(bs_env%eigenval_scf(1, :, ispin))
    1784             :          END SELECT
    1785          42 :          E_min = MIN(E_min, E_min_ispin)
    1786          76 :          E_max = MAX(E_max, E_max_ispin)
    1787             :       END DO
    1788             : 
    1789          34 :       E_range = E_max/E_min
    1790             : 
    1791         102 :       ALLOCATE (points_and_weights(2*num_time_freq_points))
    1792             : 
    1793             :       ! frequency points
    1794          34 :       IF (num_time_freq_points .LE. 20) THEN
    1795          34 :          CALL get_rpa_minimax_coeff(num_time_freq_points, E_range, points_and_weights, ierr, .FALSE.)
    1796             :       ELSE
    1797           0 :          CALL get_rpa_minimax_coeff_larger_grid(num_time_freq_points, E_range, points_and_weights)
    1798             :       END IF
    1799             : 
    1800             :       ! one needs to scale the minimax grids, see Azizi, Wilhelm, Golze, Panades-Barrueta,
    1801             :       ! Giantomassi, Rinke, Draxl, Gonze et al., 2 publications
    1802         470 :       bs_env%imag_freq_points(:) = points_and_weights(1:num_time_freq_points)*E_min
    1803             : 
    1804             :       ! determine number of fit points in the interval [0,ω_max] for virt, or [-ω_max,0] for occ
    1805          34 :       bs_env%num_freq_points_fit = 0
    1806         470 :       DO i_w = 1, num_time_freq_points
    1807         470 :          IF (bs_env%imag_freq_points(i_w) < bs_env%freq_max_fit) THEN
    1808         180 :             bs_env%num_freq_points_fit = bs_env%num_freq_points_fit + 1
    1809             :          END IF
    1810             :       END DO
    1811             : 
    1812             :       ! iω values for the analytic continuation Σ^c_n(iω,k) -> Σ^c_n(ϵ,k)
    1813         102 :       ALLOCATE (bs_env%imag_freq_points_fit(bs_env%num_freq_points_fit))
    1814          34 :       j_w = 0
    1815         470 :       DO i_w = 1, num_time_freq_points
    1816         470 :          IF (bs_env%imag_freq_points(i_w) < bs_env%freq_max_fit) THEN
    1817         180 :             j_w = j_w + 1
    1818         180 :             bs_env%imag_freq_points_fit(j_w) = bs_env%imag_freq_points(i_w)
    1819             :          END IF
    1820             :       END DO
    1821             : 
    1822             :       ! reset the number of Padé parameters if smaller than the number of
    1823             :       ! imaginary-frequency points for the fit
    1824          34 :       IF (bs_env%num_freq_points_fit < bs_env%nparam_pade) THEN
    1825          34 :          bs_env%nparam_pade = bs_env%num_freq_points_fit
    1826             :       END IF
    1827             : 
    1828             :       ! time points
    1829          34 :       IF (num_time_freq_points .LE. 20) THEN
    1830          34 :          CALL get_exp_minimax_coeff(num_time_freq_points, E_range, points_and_weights)
    1831             :       ELSE
    1832           0 :          CALL get_exp_minimax_coeff_gw(num_time_freq_points, E_range, points_and_weights)
    1833             :       END IF
    1834             : 
    1835         470 :       bs_env%imag_time_points(:) = points_and_weights(1:num_time_freq_points)/(2.0_dp*E_min)
    1836         470 :       bs_env%imag_time_weights_freq_zero(:) = points_and_weights(num_time_freq_points + 1:)/(E_min)
    1837             : 
    1838          34 :       DEALLOCATE (points_and_weights)
    1839             : 
    1840          34 :       u = bs_env%unit_nr
    1841          34 :       IF (u > 0) THEN
    1842          17 :          WRITE (u, '(T2,A)') ''
    1843          17 :          WRITE (u, '(T2,A,F55.2)') 'SCF direct band gap (eV)', E_min*evolt
    1844          17 :          WRITE (u, '(T2,A,F53.2)') 'Max. SCF eigval diff. (eV)', E_max*evolt
    1845          17 :          WRITE (u, '(T2,A,F55.2)') 'E-Range for minimax grid', E_range
    1846          17 :          WRITE (u, '(T2,A,I27)') 'Number of Padé parameters for analytic continuation:', &
    1847          34 :             bs_env%nparam_pade
    1848          17 :          WRITE (u, '(T2,A)') ''
    1849             :       END IF
    1850             : 
    1851             :       ! in minimax grids, Fourier transforms t -> w and w -> t are split using
    1852             :       ! e^(iwt) = cos(wt) + i sin(wt); we thus calculate weights for trafos with a cos and
    1853             :       ! sine prefactor; details in Azizi, Wilhelm, Golze, Giantomassi, Panades-Barrueta,
    1854             :       ! Rinke, Draxl, Gonze et al., 2 publications
    1855             : 
    1856             :       ! cosine transform weights imaginary time to imaginary frequency
    1857             :       CALL get_l_sq_wghts_cos_tf_t_to_w(num_time_freq_points, &
    1858             :                                         bs_env%imag_time_points, &
    1859             :                                         bs_env%weights_cos_t_to_w, &
    1860             :                                         bs_env%imag_freq_points, &
    1861             :                                         E_min, E_max, max_error_min, &
    1862             :                                         bs_env%num_points_per_magnitude, &
    1863          34 :                                         bs_env%regularization_minimax)
    1864             : 
    1865             :       ! cosine transform weights imaginary frequency to imaginary time
    1866             :       CALL get_l_sq_wghts_cos_tf_w_to_t(num_time_freq_points, &
    1867             :                                         bs_env%imag_time_points, &
    1868             :                                         bs_env%weights_cos_w_to_t, &
    1869             :                                         bs_env%imag_freq_points, &
    1870             :                                         E_min, E_max, max_error_min, &
    1871             :                                         bs_env%num_points_per_magnitude, &
    1872          34 :                                         bs_env%regularization_minimax)
    1873             : 
    1874             :       ! sine transform weights imaginary time to imaginary frequency
    1875             :       CALL get_l_sq_wghts_sin_tf_t_to_w(num_time_freq_points, &
    1876             :                                         bs_env%imag_time_points, &
    1877             :                                         bs_env%weights_sin_t_to_w, &
    1878             :                                         bs_env%imag_freq_points, &
    1879             :                                         E_min, E_max, max_error_min, &
    1880             :                                         bs_env%num_points_per_magnitude, &
    1881          34 :                                         bs_env%regularization_minimax)
    1882             : 
    1883          34 :       CALL timestop(handle)
    1884             : 
    1885          68 :    END SUBROUTINE setup_time_and_frequency_minimax_grid
    1886             : 
    1887             : ! **************************************************************************************************
    1888             : !> \brief ...
    1889             : !> \param qs_env ...
    1890             : !> \param bs_env ...
    1891             : ! **************************************************************************************************
    1892           6 :    SUBROUTINE setup_cells_3c(qs_env, bs_env)
    1893             : 
    1894             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1895             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1896             : 
    1897             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'setup_cells_3c'
    1898             : 
    1899             :       INTEGER :: atom_i, atom_j, atom_k, cell_pair_count, handle, i, i_cell_x, i_cell_x_max, &
    1900             :          i_cell_x_min, i_size, ikind, img, j, j_cell, j_cell_max, j_cell_y, j_cell_y_max, &
    1901             :          j_cell_y_min, j_size, k_cell, k_cell_max, k_cell_z, k_cell_z_max, k_cell_z_min, k_size, &
    1902             :          nimage_pairs_3c, nimages_3c, nimages_3c_max, nkind, u
    1903             :       INTEGER(KIND=int_8)                                :: mem_occ_per_proc
    1904           6 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: n_other_3c_images_max
    1905           6 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: index_to_cell_3c_max, nblocks_3c_max
    1906             :       INTEGER, DIMENSION(3)                              :: cell_index, n_max
    1907             :       REAL(KIND=dp) :: avail_mem_per_proc_GB, cell_dist, cell_radius_3c, eps, exp_min_ao, &
    1908             :          exp_min_RI, frobenius_norm, mem_3c_GB, mem_occ_per_proc_GB, radius_ao, radius_ao_product, &
    1909             :          radius_RI
    1910           6 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: int_3c
    1911           6 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: exp_ao, exp_RI
    1912             : 
    1913           6 :       CALL timeset(routineN, handle)
    1914             : 
    1915           6 :       CALL get_qs_env(qs_env, nkind=nkind)
    1916             : 
    1917           6 :       exp_min_RI = 10.0_dp
    1918           6 :       exp_min_ao = 10.0_dp
    1919             : 
    1920          18 :       DO ikind = 1, nkind
    1921             : 
    1922          12 :          CALL get_gto_basis_set(bs_env%basis_set_RI(ikind)%gto_basis_set, zet=exp_RI)
    1923          12 :          CALL get_gto_basis_set(bs_env%basis_set_ao(ikind)%gto_basis_set, zet=exp_ao)
    1924             : 
    1925             :          ! we need to remove all exponents lower than a lower bound, e.g. 1E-3, because
    1926             :          ! for contracted basis sets, there might be exponents = 0 in zet
    1927          24 :          DO i = 1, SIZE(exp_RI, 1)
    1928          42 :             DO j = 1, SIZE(exp_RI, 2)
    1929          30 :                IF (exp_RI(i, j) < exp_min_RI .AND. exp_RI(i, j) > 1E-3_dp) exp_min_RI = exp_RI(i, j)
    1930             :             END DO
    1931             :          END DO
    1932          66 :          DO i = 1, SIZE(exp_ao, 1)
    1933         144 :             DO j = 1, SIZE(exp_ao, 2)
    1934         132 :                IF (exp_ao(i, j) < exp_min_ao .AND. exp_ao(i, j) > 1E-3_dp) exp_min_ao = exp_ao(i, j)
    1935             :             END DO
    1936             :          END DO
    1937             : 
    1938             :       END DO
    1939             : 
    1940           6 :       eps = bs_env%eps_filter*bs_env%heuristic_filter_factor
    1941             : 
    1942           6 :       radius_ao = SQRT(-LOG(eps)/exp_min_ao)
    1943           6 :       radius_ao_product = SQRT(-LOG(eps)/(2.0_dp*exp_min_ao))
    1944           6 :       radius_RI = SQRT(-LOG(eps)/exp_min_RI)
    1945             : 
    1946             :       ! For a 3c integral (μR υS | P0) we have that cell R and cell S need to be within radius_3c
    1947           6 :       cell_radius_3c = radius_ao_product + radius_RI + bs_env%ri_metric%cutoff_radius
    1948             : 
    1949          24 :       n_max(1:3) = bs_env%periodic(1:3)*30
    1950             : 
    1951           6 :       nimages_3c_max = 0
    1952             : 
    1953           6 :       i_cell_x_min = 0
    1954           6 :       i_cell_x_max = 0
    1955           6 :       j_cell_y_min = 0
    1956           6 :       j_cell_y_max = 0
    1957           6 :       k_cell_z_min = 0
    1958           6 :       k_cell_z_max = 0
    1959             : 
    1960         252 :       DO i_cell_x = -n_max(1), n_max(1)
    1961       15258 :          DO j_cell_y = -n_max(2), n_max(2)
    1962       37578 :             DO k_cell_z = -n_max(3), n_max(3)
    1963             : 
    1964       89304 :                cell_index(1:3) = (/i_cell_x, j_cell_y, k_cell_z/)
    1965             : 
    1966       22326 :                CALL get_cell_dist(cell_index, bs_env%hmat, cell_dist)
    1967             : 
    1968       37332 :                IF (cell_dist < cell_radius_3c) THEN
    1969         134 :                   nimages_3c_max = nimages_3c_max + 1
    1970         134 :                   i_cell_x_min = MIN(i_cell_x_min, i_cell_x)
    1971         134 :                   i_cell_x_max = MAX(i_cell_x_max, i_cell_x)
    1972         134 :                   j_cell_y_min = MIN(j_cell_y_min, j_cell_y)
    1973         134 :                   j_cell_y_max = MAX(j_cell_y_max, j_cell_y)
    1974         134 :                   k_cell_z_min = MIN(k_cell_z_min, k_cell_z)
    1975         134 :                   k_cell_z_max = MAX(k_cell_z_max, k_cell_z)
    1976             :                END IF
    1977             : 
    1978             :             END DO
    1979             :          END DO
    1980             :       END DO
    1981             : 
    1982             :       ! get index_to_cell_3c_max for the maximum possible cell range;
    1983             :       ! compute 3c integrals later in this routine and check really which cell is needed
    1984          18 :       ALLOCATE (index_to_cell_3c_max(nimages_3c_max, 3))
    1985             : 
    1986           6 :       img = 0
    1987         252 :       DO i_cell_x = -n_max(1), n_max(1)
    1988       15258 :          DO j_cell_y = -n_max(2), n_max(2)
    1989       37578 :             DO k_cell_z = -n_max(3), n_max(3)
    1990             : 
    1991       89304 :                cell_index(1:3) = (/i_cell_x, j_cell_y, k_cell_z/)
    1992             : 
    1993       22326 :                CALL get_cell_dist(cell_index, bs_env%hmat, cell_dist)
    1994             : 
    1995       37332 :                IF (cell_dist < cell_radius_3c) THEN
    1996         134 :                   img = img + 1
    1997         536 :                   index_to_cell_3c_max(img, 1:3) = cell_index(1:3)
    1998             :                END IF
    1999             : 
    2000             :             END DO
    2001             :          END DO
    2002             :       END DO
    2003             : 
    2004             :       ! get pairs of R and S which have non-zero 3c integral (μR υS | P0)
    2005          24 :       ALLOCATE (nblocks_3c_max(nimages_3c_max, nimages_3c_max))
    2006        3154 :       nblocks_3c_max(:, :) = 0
    2007             : 
    2008             :       cell_pair_count = 0
    2009         140 :       DO j_cell = 1, nimages_3c_max
    2010        3154 :          DO k_cell = 1, nimages_3c_max
    2011             : 
    2012        3014 :             cell_pair_count = cell_pair_count + 1
    2013             : 
    2014             :             ! trivial parallelization over cell pairs
    2015        3014 :             IF (MODULO(cell_pair_count, bs_env%para_env%num_pe) .NE. bs_env%para_env%mepos) CYCLE
    2016             : 
    2017        5280 :             DO atom_j = 1, bs_env%n_atom
    2018       15806 :             DO atom_k = 1, bs_env%n_atom
    2019       36723 :             DO atom_i = 1, bs_env%n_atom
    2020             : 
    2021       23931 :                j_size = bs_env%i_ao_end_from_atom(atom_j) - bs_env%i_ao_start_from_atom(atom_j) + 1
    2022       23931 :                k_size = bs_env%i_ao_end_from_atom(atom_k) - bs_env%i_ao_start_from_atom(atom_k) + 1
    2023       23931 :                i_size = bs_env%i_RI_end_from_atom(atom_i) - bs_env%i_RI_start_from_atom(atom_i) + 1
    2024             : 
    2025      119655 :                ALLOCATE (int_3c(j_size, k_size, i_size))
    2026             : 
    2027             :                ! compute 3-c int. ( μ(atom j) R , ν (atom k) S | P (atom i) 0 )
    2028             :                ! ("|": truncated Coulomb operator), inside build_3c_integrals: (j k | i)
    2029             :                CALL build_3c_integral_block(int_3c, qs_env, bs_env%ri_metric, &
    2030             :                                             basis_j=bs_env%basis_set_AO, &
    2031             :                                             basis_k=bs_env%basis_set_AO, &
    2032             :                                             basis_i=bs_env%basis_set_RI, &
    2033             :                                             cell_j=index_to_cell_3c_max(j_cell, 1:3), &
    2034             :                                             cell_k=index_to_cell_3c_max(k_cell, 1:3), &
    2035      167517 :                                             atom_k=atom_k, atom_j=atom_j, atom_i=atom_i)
    2036             : 
    2037     1404411 :                frobenius_norm = SQRT(SUM(int_3c(:, :, :)**2))
    2038             : 
    2039       23931 :                DEALLOCATE (int_3c)
    2040             : 
    2041             :                ! we use a higher threshold here to safe memory when storing the 3c integrals
    2042             :                ! in every tensor group
    2043       33084 :                IF (frobenius_norm > eps) THEN
    2044         513 :                   nblocks_3c_max(j_cell, k_cell) = nblocks_3c_max(j_cell, k_cell) + 1
    2045             :                END IF
    2046             : 
    2047             :             END DO
    2048             :             END DO
    2049             :             END DO
    2050             : 
    2051             :          END DO
    2052             :       END DO
    2053             : 
    2054           6 :       CALL bs_env%para_env%sum(nblocks_3c_max)
    2055             : 
    2056          18 :       ALLOCATE (n_other_3c_images_max(nimages_3c_max))
    2057         140 :       n_other_3c_images_max(:) = 0
    2058             : 
    2059           6 :       nimages_3c = 0
    2060           6 :       nimage_pairs_3c = 0
    2061             : 
    2062         140 :       DO j_cell = 1, nimages_3c_max
    2063        3148 :          DO k_cell = 1, nimages_3c_max
    2064        3148 :             IF (nblocks_3c_max(j_cell, k_cell) > 0) THEN
    2065         178 :                n_other_3c_images_max(j_cell) = n_other_3c_images_max(j_cell) + 1
    2066         178 :                nimage_pairs_3c = nimage_pairs_3c + 1
    2067             :             END IF
    2068             :          END DO
    2069             : 
    2070         140 :          IF (n_other_3c_images_max(j_cell) > 0) nimages_3c = nimages_3c + 1
    2071             : 
    2072             :       END DO
    2073             : 
    2074           6 :       bs_env%nimages_3c = nimages_3c
    2075          18 :       ALLOCATE (bs_env%index_to_cell_3c(nimages_3c, 3))
    2076             :       ALLOCATE (bs_env%cell_to_index_3c(i_cell_x_min:i_cell_x_max, &
    2077             :                                         j_cell_y_min:j_cell_y_max, &
    2078          30 :                                         k_cell_z_min:k_cell_z_max))
    2079         240 :       bs_env%cell_to_index_3c(:, :, :) = -1
    2080             : 
    2081          24 :       ALLOCATE (bs_env%nblocks_3c(nimages_3c, nimages_3c))
    2082           6 :       bs_env%nblocks_3c(nimages_3c, nimages_3c) = 0
    2083             : 
    2084           6 :       j_cell = 0
    2085         140 :       DO j_cell_max = 1, nimages_3c_max
    2086         134 :          IF (n_other_3c_images_max(j_cell_max) == 0) CYCLE
    2087          44 :          j_cell = j_cell + 1
    2088         176 :          cell_index(1:3) = index_to_cell_3c_max(j_cell_max, 1:3)
    2089         176 :          bs_env%index_to_cell_3c(j_cell, 1:3) = cell_index(1:3)
    2090          44 :          bs_env%cell_to_index_3c(cell_index(1), cell_index(2), cell_index(3)) = j_cell
    2091             : 
    2092          44 :          k_cell = 0
    2093        1070 :          DO k_cell_max = 1, nimages_3c_max
    2094        1020 :             IF (n_other_3c_images_max(k_cell_max) == 0) CYCLE
    2095         388 :             k_cell = k_cell + 1
    2096             : 
    2097        1154 :             bs_env%nblocks_3c(j_cell, k_cell) = nblocks_3c_max(j_cell_max, k_cell_max)
    2098             :          END DO
    2099             : 
    2100             :       END DO
    2101             : 
    2102             :       ! we use: 8*10^-9 GB / double precision number
    2103             :       mem_3c_GB = REAL(bs_env%n_RI, KIND=dp)*REAL(bs_env%n_ao, KIND=dp)**2 &
    2104           6 :                   *REAL(nimage_pairs_3c, KIND=dp)*8E-9_dp
    2105             : 
    2106           6 :       CALL m_memory(mem_occ_per_proc)
    2107           6 :       CALL bs_env%para_env%max(mem_occ_per_proc)
    2108             : 
    2109           6 :       mem_occ_per_proc_GB = REAL(mem_occ_per_proc, KIND=dp)/1.0E9_dp
    2110             : 
    2111             :       ! number of processors per group that entirely stores the 3c integrals and does tensor ops
    2112           6 :       avail_mem_per_proc_GB = bs_env%input_memory_per_proc_GB - mem_occ_per_proc_GB
    2113             : 
    2114             :       ! careful: downconvering real to integer, 1.9 -> 1; thus add 1.0 for upconversion, 1.9 -> 2
    2115           6 :       bs_env%group_size_tensor = MAX(INT(mem_3c_GB/avail_mem_per_proc_GB + 1.0_dp), 1)
    2116             : 
    2117           6 :       u = bs_env%unit_nr
    2118             : 
    2119           6 :       IF (u > 0) THEN
    2120           3 :          WRITE (u, FMT="(T2,A,F52.1,A)") "Radius of atomic orbitals", radius_ao*angstrom, " Å"
    2121           3 :          WRITE (u, FMT="(T2,A,F55.1,A)") "Radius of RI functions", radius_RI*angstrom, " Å"
    2122           3 :          WRITE (u, FMT="(T2,A,I47)") "Number of cells for 3c integrals", nimages_3c
    2123           3 :          WRITE (u, FMT="(T2,A,I42)") "Number of cell pairs for 3c integrals", nimage_pairs_3c
    2124           3 :          WRITE (u, '(T2,A)') ''
    2125           3 :          WRITE (u, '(T2,A,F37.1,A)') 'Input: Available memory per MPI process', &
    2126           6 :             bs_env%input_memory_per_proc_GB, ' GB'
    2127           3 :          WRITE (u, '(T2,A,F35.1,A)') 'Used memory per MPI process before GW run', &
    2128           6 :             mem_occ_per_proc_GB, ' GB'
    2129           3 :          WRITE (u, '(T2,A,F44.1,A)') 'Memory of three-center integrals', mem_3c_GB, ' GB'
    2130             :       END IF
    2131             : 
    2132           6 :       CALL timestop(handle)
    2133             : 
    2134          18 :    END SUBROUTINE setup_cells_3c
    2135             : 
    2136             : ! **************************************************************************************************
    2137             : !> \brief ...
    2138             : !> \param index_to_cell_1 ...
    2139             : !> \param index_to_cell_2 ...
    2140             : !> \param nimages_1 ...
    2141             : !> \param nimages_2 ...
    2142             : !> \param index_to_cell ...
    2143             : !> \param cell_to_index ...
    2144             : !> \param nimages ...
    2145             : ! **************************************************************************************************
    2146           6 :    SUBROUTINE sum_two_R_grids(index_to_cell_1, index_to_cell_2, nimages_1, nimages_2, &
    2147             :                               index_to_cell, cell_to_index, nimages)
    2148             : 
    2149             :       INTEGER, DIMENSION(:, :)                           :: index_to_cell_1, index_to_cell_2
    2150             :       INTEGER                                            :: nimages_1, nimages_2
    2151             :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: index_to_cell
    2152             :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index
    2153             :       INTEGER                                            :: nimages
    2154             : 
    2155             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'sum_two_R_grids'
    2156             : 
    2157             :       INTEGER                                            :: handle, i_dim, img_1, img_2, nimages_max
    2158           6 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: index_to_cell_tmp
    2159             :       INTEGER, DIMENSION(3)                              :: cell_1, cell_2, R, R_max, R_min
    2160             : 
    2161           6 :       CALL timeset(routineN, handle)
    2162             : 
    2163          24 :       DO i_dim = 1, 3
    2164         282 :          R_min(i_dim) = MINVAL(index_to_cell_1(:, i_dim)) + MINVAL(index_to_cell_2(:, i_dim))
    2165         306 :          R_max(i_dim) = MAXVAL(index_to_cell_1(:, i_dim)) + MAXVAL(index_to_cell_2(:, i_dim))
    2166             :       END DO
    2167             : 
    2168           6 :       nimages_max = (R_max(1) - R_min(1) + 1)*(R_max(2) - R_min(2) + 1)*(R_max(3) - R_min(3) + 1)
    2169             : 
    2170          18 :       ALLOCATE (index_to_cell_tmp(nimages_max, 3))
    2171         534 :       index_to_cell_tmp(:, :) = -1
    2172             : 
    2173          30 :       ALLOCATE (cell_to_index(R_min(1):R_max(1), R_min(2):R_max(2), R_min(3):R_max(3)))
    2174         284 :       cell_to_index(:, :, :) = -1
    2175             : 
    2176           6 :       nimages = 0
    2177             : 
    2178          50 :       DO img_1 = 1, nimages_1
    2179             : 
    2180         438 :          DO img_2 = 1, nimages_2
    2181             : 
    2182        1552 :             cell_1(1:3) = index_to_cell_1(img_1, 1:3)
    2183        1552 :             cell_2(1:3) = index_to_cell_2(img_2, 1:3)
    2184             : 
    2185        1552 :             R(1:3) = cell_1(1:3) + cell_2(1:3)
    2186             : 
    2187             :             ! check whether we have found a new cell
    2188         432 :             IF (cell_to_index(R(1), R(2), R(3)) == -1) THEN
    2189             : 
    2190         122 :                nimages = nimages + 1
    2191         122 :                cell_to_index(R(1), R(2), R(3)) = nimages
    2192         488 :                index_to_cell_tmp(nimages, 1:3) = R(1:3)
    2193             : 
    2194             :             END IF
    2195             : 
    2196             :          END DO
    2197             : 
    2198             :       END DO
    2199             : 
    2200          18 :       ALLOCATE (index_to_cell(nimages, 3))
    2201         390 :       index_to_cell(:, :) = index_to_cell_tmp(1:nimages, 1:3)
    2202             : 
    2203           6 :       CALL timestop(handle)
    2204             : 
    2205          12 :    END SUBROUTINE sum_two_R_grids
    2206             : 
    2207             : ! **************************************************************************************************
    2208             : !> \brief ...
    2209             : !> \param qs_env ...
    2210             : !> \param bs_env ...
    2211             : ! **************************************************************************************************
    2212           6 :    SUBROUTINE compute_3c_integrals(qs_env, bs_env)
    2213             : 
    2214             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2215             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2216             : 
    2217             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_3c_integrals'
    2218             : 
    2219             :       INTEGER                                            :: handle, j_cell, k_cell, nimages_3c
    2220             : 
    2221           6 :       CALL timeset(routineN, handle)
    2222             : 
    2223           6 :       nimages_3c = bs_env%nimages_3c
    2224         504 :       ALLOCATE (bs_env%t_3c_int(nimages_3c, nimages_3c))
    2225          50 :       DO j_cell = 1, nimages_3c
    2226         438 :          DO k_cell = 1, nimages_3c
    2227         432 :             CALL dbt_create(bs_env%t_RI_AO__AO, bs_env%t_3c_int(j_cell, k_cell))
    2228             :          END DO
    2229             :       END DO
    2230             : 
    2231             :       CALL build_3c_integrals(bs_env%t_3c_int, &
    2232             :                               bs_env%eps_filter, &
    2233             :                               qs_env, &
    2234             :                               bs_env%nl_3c, &
    2235             :                               int_eps=bs_env%eps_filter*0.05_dp, &
    2236             :                               basis_i=bs_env%basis_set_RI, &
    2237             :                               basis_j=bs_env%basis_set_AO, &
    2238             :                               basis_k=bs_env%basis_set_AO, &
    2239             :                               potential_parameter=bs_env%ri_metric, &
    2240             :                               desymmetrize=.FALSE., do_kpoints=.TRUE., cell_sym=.TRUE., &
    2241           6 :                               cell_to_index_ext=bs_env%cell_to_index_3c)
    2242             : 
    2243           6 :       CALL bs_env%para_env%sync()
    2244             : 
    2245           6 :       CALL timestop(handle)
    2246             : 
    2247           6 :    END SUBROUTINE compute_3c_integrals
    2248             : 
    2249             : ! **************************************************************************************************
    2250             : !> \brief ...
    2251             : !> \param cell_index ...
    2252             : !> \param hmat ...
    2253             : !> \param cell_dist ...
    2254             : ! **************************************************************************************************
    2255       44652 :    SUBROUTINE get_cell_dist(cell_index, hmat, cell_dist)
    2256             : 
    2257             :       INTEGER, DIMENSION(3)                              :: cell_index
    2258             :       REAL(KIND=dp)                                      :: hmat(3, 3), cell_dist
    2259             : 
    2260             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_cell_dist'
    2261             : 
    2262             :       INTEGER                                            :: handle, i_dim
    2263             :       INTEGER, DIMENSION(3)                              :: cell_index_adj
    2264             :       REAL(KIND=dp)                                      :: cell_dist_3(3)
    2265             : 
    2266       44652 :       CALL timeset(routineN, handle)
    2267             : 
    2268             :       ! the distance of cells needs to be taken to adjacent neighbors, not
    2269             :       ! between the center of the cells. We thus need to rescale the cell index
    2270      178608 :       DO i_dim = 1, 3
    2271      133956 :          IF (cell_index(i_dim) > 0) cell_index_adj(i_dim) = cell_index(i_dim) - 1
    2272      133956 :          IF (cell_index(i_dim) < 0) cell_index_adj(i_dim) = cell_index(i_dim) + 1
    2273      178608 :          IF (cell_index(i_dim) == 0) cell_index_adj(i_dim) = cell_index(i_dim)
    2274             :       END DO
    2275             : 
    2276      714432 :       cell_dist_3(1:3) = MATMUL(hmat, REAL(cell_index_adj, KIND=dp))
    2277             : 
    2278      178608 :       cell_dist = SQRT(ABS(SUM(cell_dist_3(1:3)**2)))
    2279             : 
    2280       44652 :       CALL timestop(handle)
    2281             : 
    2282       44652 :    END SUBROUTINE get_cell_dist
    2283             : 
    2284             : ! **************************************************************************************************
    2285             : !> \brief ...
    2286             : !> \param qs_env ...
    2287             : !> \param bs_env ...
    2288             : !> \param kpoints ...
    2289             : !> \param do_print ...
    2290             : ! **************************************************************************************************
    2291           0 :    SUBROUTINE setup_kpoints_scf_desymm(qs_env, bs_env, kpoints, do_print)
    2292             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2293             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2294             :       TYPE(kpoint_type), POINTER                         :: kpoints
    2295             : 
    2296             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_scf_desymm'
    2297             : 
    2298             :       INTEGER                                            :: handle, i_cell_x, i_dim, img, j_cell_y, &
    2299             :                                                             k_cell_z, nimages, nkp, u
    2300             :       INTEGER, DIMENSION(3)                              :: cell_grid, cixd, nkp_grid
    2301             :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
    2302             : 
    2303             :       LOGICAL:: do_print
    2304             : 
    2305           0 :       CALL timeset(routineN, handle)
    2306             : 
    2307           0 :       NULLIFY (kpoints)
    2308           0 :       CALL kpoint_create(kpoints)
    2309             : 
    2310           0 :       CALL get_qs_env(qs_env=qs_env, kpoints=kpoints_scf)
    2311             : 
    2312           0 :       nkp_grid(1:3) = kpoints_scf%nkp_grid(1:3)
    2313           0 :       nkp = nkp_grid(1)*nkp_grid(2)*nkp_grid(3)
    2314             : 
    2315             :       ! we need in periodic directions at least 2 k-points in the SCF
    2316           0 :       DO i_dim = 1, 3
    2317           0 :          IF (bs_env%periodic(i_dim) == 1) THEN
    2318           0 :             CPASSERT(nkp_grid(i_dim) > 1)
    2319             :          END IF
    2320             :       END DO
    2321             : 
    2322           0 :       kpoints%kp_scheme = "GENERAL"
    2323           0 :       kpoints%nkp_grid(1:3) = nkp_grid(1:3)
    2324           0 :       kpoints%nkp = nkp
    2325           0 :       bs_env%nkp_scf_desymm = nkp
    2326             : 
    2327           0 :       ALLOCATE (kpoints%xkp(1:3, nkp))
    2328           0 :       CALL compute_xkp(kpoints%xkp, 1, nkp, nkp_grid)
    2329             : 
    2330           0 :       ALLOCATE (kpoints%wkp(nkp))
    2331           0 :       kpoints%wkp(:) = 1.0_dp/REAL(nkp, KIND=dp)
    2332             : 
    2333             :       ! for example 4x3x6 kpoint grid -> 3x3x5 cell grid because we need the same number of
    2334             :       ! neighbor cells on both sides of the unit cell
    2335           0 :       cell_grid(1:3) = nkp_grid(1:3) - MODULO(nkp_grid(1:3) + 1, 2)
    2336             :       ! cell index: for example for x: from -n_x/2 to +n_x/2, n_x: number of cells in x direction
    2337           0 :       cixd(1:3) = cell_grid(1:3)/2
    2338             : 
    2339           0 :       nimages = cell_grid(1)*cell_grid(2)*cell_grid(3)
    2340             : 
    2341           0 :       bs_env%nimages_scf_desymm = nimages
    2342             : 
    2343           0 :       ALLOCATE (kpoints%cell_to_index(-cixd(1):cixd(1), -cixd(2):cixd(2), -cixd(3):cixd(3)))
    2344           0 :       ALLOCATE (kpoints%index_to_cell(nimages, 3))
    2345             : 
    2346           0 :       img = 0
    2347           0 :       DO i_cell_x = -cixd(1), cixd(1)
    2348           0 :          DO j_cell_y = -cixd(2), cixd(2)
    2349           0 :             DO k_cell_z = -cixd(3), cixd(3)
    2350           0 :                img = img + 1
    2351           0 :                kpoints%cell_to_index(i_cell_x, j_cell_y, k_cell_z) = img
    2352           0 :                kpoints%index_to_cell(img, 1:3) = (/i_cell_x, j_cell_y, k_cell_z/)
    2353             :             END DO
    2354             :          END DO
    2355             :       END DO
    2356             : 
    2357           0 :       u = bs_env%unit_nr
    2358           0 :       IF (u > 0 .AND. do_print) THEN
    2359           0 :          WRITE (u, FMT="(T2,A,I49)") "Number of cells for G, χ, W, Σ", nimages
    2360             :       END IF
    2361             : 
    2362           0 :       CALL timestop(handle)
    2363             : 
    2364           0 :    END SUBROUTINE setup_kpoints_scf_desymm
    2365             : 
    2366             : ! **************************************************************************************************
    2367             : !> \brief ...
    2368             : !> \param bs_env ...
    2369             : ! **************************************************************************************************
    2370           6 :    SUBROUTINE setup_cells_Delta_R(bs_env)
    2371             : 
    2372             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2373             : 
    2374             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_cells_Delta_R'
    2375             : 
    2376             :       INTEGER                                            :: handle
    2377             : 
    2378           6 :       CALL timeset(routineN, handle)
    2379             : 
    2380             :       ! cell sums batch wise for fixed ΔR = S_1 - R_1; for example:
    2381             :       ! Σ_λσ^R = sum_PR1νS1 M^G_λ0,νS1,PR1 M^W_σR,νS1,PR1
    2382             : 
    2383             :       CALL sum_two_R_grids(bs_env%index_to_cell_3c, &
    2384             :                            bs_env%index_to_cell_3c, &
    2385             :                            bs_env%nimages_3c, bs_env%nimages_3c, &
    2386             :                            bs_env%index_to_cell_Delta_R, &
    2387             :                            bs_env%cell_to_index_Delta_R, &
    2388           6 :                            bs_env%nimages_Delta_R)
    2389             : 
    2390           6 :       IF (bs_env%unit_nr > 0) THEN
    2391           3 :          WRITE (bs_env%unit_nr, FMT="(T2,A,I61)") "Number of cells ΔR", bs_env%nimages_Delta_R
    2392             :       END IF
    2393             : 
    2394           6 :       CALL timestop(handle)
    2395             : 
    2396           6 :    END SUBROUTINE setup_cells_Delta_R
    2397             : 
    2398             : ! **************************************************************************************************
    2399             : !> \brief ...
    2400             : !> \param bs_env ...
    2401             : ! **************************************************************************************************
    2402           6 :    SUBROUTINE setup_parallelization_Delta_R(bs_env)
    2403             : 
    2404             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2405             : 
    2406             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_parallelization_Delta_R'
    2407             : 
    2408             :       INTEGER                                            :: handle, i_cell_Delta_R, i_task_local, &
    2409             :                                                             n_tasks_local
    2410           6 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: i_cell_Delta_R_group, &
    2411           6 :                                                             n_tensor_ops_Delta_R
    2412             : 
    2413           6 :       CALL timeset(routineN, handle)
    2414             : 
    2415           6 :       CALL compute_n_tensor_ops_Delta_R(bs_env, n_tensor_ops_Delta_R)
    2416             : 
    2417           6 :       CALL compute_Delta_R_dist(bs_env, n_tensor_ops_Delta_R, i_cell_Delta_R_group, n_tasks_local)
    2418             : 
    2419           6 :       bs_env%n_tasks_Delta_R_local = n_tasks_local
    2420             : 
    2421          18 :       ALLOCATE (bs_env%task_Delta_R(n_tasks_local))
    2422             : 
    2423           6 :       i_task_local = 0
    2424         128 :       DO i_cell_Delta_R = 1, bs_env%nimages_Delta_R
    2425             : 
    2426         122 :          IF (i_cell_Delta_R_group(i_cell_Delta_R) /= bs_env%tensor_group_color) CYCLE
    2427             : 
    2428          56 :          i_task_local = i_task_local + 1
    2429             : 
    2430         128 :          bs_env%task_Delta_R(i_task_local) = i_cell_Delta_R
    2431             : 
    2432             :       END DO
    2433             : 
    2434           6 :       CALL timestop(handle)
    2435             : 
    2436          12 :    END SUBROUTINE setup_parallelization_Delta_R
    2437             : 
    2438             : ! **************************************************************************************************
    2439             : !> \brief ...
    2440             : !> \param bs_env ...
    2441             : !> \param n_tensor_ops_Delta_R ...
    2442             : !> \param i_cell_Delta_R_group ...
    2443             : !> \param n_tasks_local ...
    2444             : ! **************************************************************************************************
    2445           6 :    SUBROUTINE compute_Delta_R_dist(bs_env, n_tensor_ops_Delta_R, i_cell_Delta_R_group, n_tasks_local)
    2446             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2447             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: n_tensor_ops_Delta_R, &
    2448             :                                                             i_cell_Delta_R_group
    2449             :       INTEGER                                            :: n_tasks_local
    2450             : 
    2451             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_Delta_R_dist'
    2452             : 
    2453             :       INTEGER                                            :: handle, i_Delta_R_max_op, i_group_min, &
    2454             :                                                             nimages_Delta_R, u
    2455           6 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: n_tensor_ops_Delta_R_in_group
    2456             : 
    2457           6 :       CALL timeset(routineN, handle)
    2458             : 
    2459           6 :       nimages_Delta_R = bs_env%nimages_Delta_R
    2460             : 
    2461           6 :       u = bs_env%unit_nr
    2462             : 
    2463           6 :       IF (u > 0 .AND. nimages_Delta_R < bs_env%num_tensor_groups) THEN
    2464           0 :          WRITE (u, FMT="(T2,A,I5,A,I5,A)") "There are only ", nimages_Delta_R, &
    2465           0 :             " tasks to work on but there are ", bs_env%num_tensor_groups, " groups."
    2466           0 :          WRITE (u, FMT="(T2,A)") "Please reduce the number of MPI processes."
    2467           0 :          WRITE (u, '(T2,A)') ''
    2468             :       END IF
    2469             : 
    2470          18 :       ALLOCATE (n_tensor_ops_Delta_R_in_group(bs_env%num_tensor_groups))
    2471          18 :       n_tensor_ops_Delta_R_in_group(:) = 0
    2472          18 :       ALLOCATE (i_cell_Delta_R_group(nimages_Delta_R))
    2473         128 :       i_cell_Delta_R_group(:) = -1
    2474             : 
    2475           6 :       n_tasks_local = 0
    2476             : 
    2477         474 :       DO WHILE (ANY(n_tensor_ops_Delta_R(:) .NE. 0))
    2478             : 
    2479             :          ! get largest element of n_tensor_ops_Delta_R
    2480        2888 :          i_Delta_R_max_op = MAXLOC(n_tensor_ops_Delta_R, 1)
    2481             : 
    2482             :          ! distribute i_Delta_R_max_op to tensor group which has currently the smallest load
    2483         448 :          i_group_min = MINLOC(n_tensor_ops_Delta_R_in_group, 1)
    2484             : 
    2485             :          ! the tensor groups are 0-index based; but i_group_min is 1-index based
    2486         112 :          i_cell_Delta_R_group(i_Delta_R_max_op) = i_group_min - 1
    2487             :          n_tensor_ops_Delta_R_in_group(i_group_min) = n_tensor_ops_Delta_R_in_group(i_group_min) + &
    2488         112 :                                                       n_tensor_ops_Delta_R(i_Delta_R_max_op)
    2489             : 
    2490             :          ! remove i_Delta_R_max_op from n_tensor_ops_Delta_R
    2491         112 :          n_tensor_ops_Delta_R(i_Delta_R_max_op) = 0
    2492             : 
    2493         118 :          IF (bs_env%tensor_group_color == i_group_min - 1) n_tasks_local = n_tasks_local + 1
    2494             : 
    2495             :       END DO
    2496             : 
    2497           6 :       CALL timestop(handle)
    2498             : 
    2499          12 :    END SUBROUTINE compute_Delta_R_dist
    2500             : 
    2501             : ! **************************************************************************************************
    2502             : !> \brief ...
    2503             : !> \param bs_env ...
    2504             : !> \param n_tensor_ops_Delta_R ...
    2505             : ! **************************************************************************************************
    2506           6 :    SUBROUTINE compute_n_tensor_ops_Delta_R(bs_env, n_tensor_ops_Delta_R)
    2507             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2508             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: n_tensor_ops_Delta_R
    2509             : 
    2510             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_n_tensor_ops_Delta_R'
    2511             : 
    2512             :       INTEGER :: handle, i_cell_Delta_R, i_cell_R, i_cell_R1, i_cell_R1_minus_R, i_cell_R2, &
    2513             :          i_cell_R2_m_R1, i_cell_S1, i_cell_S1_m_R1_p_R2, i_cell_S1_minus_R, i_cell_S2, &
    2514             :          nimages_Delta_R
    2515             :       INTEGER, DIMENSION(3) :: cell_DR, cell_m_R1, cell_R, cell_R1, cell_R1_minus_R, cell_R2, &
    2516             :          cell_R2_m_R1, cell_S1, cell_S1_m_R2_p_R1, cell_S1_minus_R, cell_S1_p_S2_m_R1, cell_S2
    2517             :       LOGICAL                                            :: cell_found
    2518             : 
    2519           6 :       CALL timeset(routineN, handle)
    2520             : 
    2521           6 :       nimages_Delta_R = bs_env%nimages_Delta_R
    2522             : 
    2523          18 :       ALLOCATE (n_tensor_ops_Delta_R(nimages_Delta_R))
    2524         128 :       n_tensor_ops_Delta_R(:) = 0
    2525             : 
    2526             :       ! compute number of tensor operations for specific Delta_R
    2527         128 :       DO i_cell_Delta_R = 1, nimages_Delta_R
    2528             : 
    2529         122 :          IF (MODULO(i_cell_Delta_R, bs_env%num_tensor_groups) /= bs_env%tensor_group_color) CYCLE
    2530             : 
    2531         617 :          DO i_cell_R1 = 1, bs_env%nimages_3c
    2532             : 
    2533        2200 :             cell_R1(1:3) = bs_env%index_to_cell_3c(i_cell_R1, 1:3)
    2534        2200 :             cell_DR(1:3) = bs_env%index_to_cell_Delta_R(i_cell_Delta_R, 1:3)
    2535             : 
    2536             :             ! S_1 = R_1 + ΔR (from ΔR = S_1 - R_1)
    2537             :             CALL add_R(cell_R1, cell_DR, bs_env%index_to_cell_3c, cell_S1, &
    2538         550 :                        cell_found, bs_env%cell_to_index_3c, i_cell_S1)
    2539         550 :             IF (.NOT. cell_found) CYCLE
    2540             : 
    2541        1850 :             DO i_cell_R2 = 1, bs_env%nimages_scf_desymm
    2542             : 
    2543        6660 :                cell_R2(1:3) = bs_env%kpoints_scf_desymm%index_to_cell(i_cell_R2, 1:3)
    2544             : 
    2545             :                ! R_2 - R_1
    2546             :                CALL add_R(cell_R2, -cell_R1, bs_env%index_to_cell_3c, cell_R2_m_R1, &
    2547        6660 :                           cell_found, bs_env%cell_to_index_3c, i_cell_R2_m_R1)
    2548        1665 :                IF (.NOT. cell_found) CYCLE
    2549             : 
    2550             :                ! S_1 - R_1 + R_2
    2551             :                CALL add_R(cell_S1, cell_R2_m_R1, bs_env%index_to_cell_3c, cell_S1_m_R2_p_R1, &
    2552         945 :                           cell_found, bs_env%cell_to_index_3c, i_cell_S1_m_R1_p_R2)
    2553         945 :                IF (.NOT. cell_found) CYCLE
    2554             : 
    2555        2457 :                n_tensor_ops_Delta_R(i_cell_Delta_R) = n_tensor_ops_Delta_R(i_cell_Delta_R) + 1
    2556             : 
    2557             :             END DO ! i_cell_R2
    2558             : 
    2559        1850 :             DO i_cell_S2 = 1, bs_env%nimages_scf_desymm
    2560             : 
    2561        6660 :                cell_S2(1:3) = bs_env%kpoints_scf_desymm%index_to_cell(i_cell_S2, 1:3)
    2562        6660 :                cell_m_R1(1:3) = -cell_R1(1:3)
    2563        6660 :                cell_S1_p_S2_m_R1(1:3) = cell_S1(1:3) + cell_S2(1:3) - cell_R1(1:3)
    2564             : 
    2565        1665 :                CALL is_cell_in_index_to_cell(cell_m_R1, bs_env%index_to_cell_3c, cell_found)
    2566        1665 :                IF (.NOT. cell_found) CYCLE
    2567             : 
    2568        1422 :                CALL is_cell_in_index_to_cell(cell_S1_p_S2_m_R1, bs_env%index_to_cell_3c, cell_found)
    2569         185 :                IF (.NOT. cell_found) CYCLE
    2570             : 
    2571             :             END DO ! i_cell_S2
    2572             : 
    2573        2522 :             DO i_cell_R = 1, bs_env%nimages_scf_desymm
    2574             : 
    2575        6660 :                cell_R = bs_env%kpoints_scf_desymm%index_to_cell(i_cell_R, 1:3)
    2576             : 
    2577             :                ! R_1 - R
    2578             :                CALL add_R(cell_R1, -cell_R, bs_env%index_to_cell_3c, cell_R1_minus_R, &
    2579        6660 :                           cell_found, bs_env%cell_to_index_3c, i_cell_R1_minus_R)
    2580        1665 :                IF (.NOT. cell_found) CYCLE
    2581             : 
    2582             :                ! S_1 - R
    2583             :                CALL add_R(cell_S1, -cell_R, bs_env%index_to_cell_3c, cell_S1_minus_R, &
    2584        4032 :                           cell_found, bs_env%cell_to_index_3c, i_cell_S1_minus_R)
    2585         550 :                IF (.NOT. cell_found) CYCLE
    2586             : 
    2587             :             END DO ! i_cell_R
    2588             : 
    2589             :          END DO ! i_cell_R1
    2590             : 
    2591             :       END DO ! i_cell_Delta_R
    2592             : 
    2593           6 :       CALL bs_env%para_env%sum(n_tensor_ops_Delta_R)
    2594             : 
    2595           6 :       CALL timestop(handle)
    2596             : 
    2597           6 :    END SUBROUTINE compute_n_tensor_ops_Delta_R
    2598             : 
    2599             : ! **************************************************************************************************
    2600             : !> \brief ...
    2601             : !> \param cell_1 ...
    2602             : !> \param cell_2 ...
    2603             : !> \param index_to_cell ...
    2604             : !> \param cell_1_plus_2 ...
    2605             : !> \param cell_found ...
    2606             : !> \param cell_to_index ...
    2607             : !> \param i_cell_1_plus_2 ...
    2608             : ! **************************************************************************************************
    2609      131410 :    SUBROUTINE add_R(cell_1, cell_2, index_to_cell, cell_1_plus_2, cell_found, &
    2610             :                     cell_to_index, i_cell_1_plus_2)
    2611             : 
    2612             :       INTEGER, DIMENSION(3)                              :: cell_1, cell_2
    2613             :       INTEGER, DIMENSION(:, :)                           :: index_to_cell
    2614             :       INTEGER, DIMENSION(3)                              :: cell_1_plus_2
    2615             :       LOGICAL                                            :: cell_found
    2616             :       INTEGER, DIMENSION(:, :, :), INTENT(IN), &
    2617             :          OPTIONAL, POINTER                               :: cell_to_index
    2618             :       INTEGER, INTENT(OUT), OPTIONAL                     :: i_cell_1_plus_2
    2619             : 
    2620             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'add_R'
    2621             : 
    2622             :       INTEGER                                            :: handle
    2623             : 
    2624      131410 :       CALL timeset(routineN, handle)
    2625             : 
    2626      525640 :       cell_1_plus_2(1:3) = cell_1(1:3) + cell_2(1:3)
    2627             : 
    2628      131410 :       CALL is_cell_in_index_to_cell(cell_1_plus_2, index_to_cell, cell_found)
    2629             : 
    2630      131410 :       IF (PRESENT(i_cell_1_plus_2)) THEN
    2631      131410 :          IF (cell_found) THEN
    2632       68845 :             CPASSERT(PRESENT(cell_to_index))
    2633       68845 :             i_cell_1_plus_2 = cell_to_index(cell_1_plus_2(1), cell_1_plus_2(2), cell_1_plus_2(3))
    2634             :          ELSE
    2635       62565 :             i_cell_1_plus_2 = -1000
    2636             :          END IF
    2637             :       END IF
    2638             : 
    2639      131410 :       CALL timestop(handle)
    2640             : 
    2641      131410 :    END SUBROUTINE add_R
    2642             : 
    2643             : ! **************************************************************************************************
    2644             : !> \brief ...
    2645             : !> \param cell ...
    2646             : !> \param index_to_cell ...
    2647             : !> \param cell_found ...
    2648             : ! **************************************************************************************************
    2649      205408 :    SUBROUTINE is_cell_in_index_to_cell(cell, index_to_cell, cell_found)
    2650             :       INTEGER, DIMENSION(3)                              :: cell
    2651             :       INTEGER, DIMENSION(:, :)                           :: index_to_cell
    2652             :       LOGICAL                                            :: cell_found
    2653             : 
    2654             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'is_cell_in_index_to_cell'
    2655             : 
    2656             :       INTEGER                                            :: handle, i_cell, nimg
    2657             :       INTEGER, DIMENSION(3)                              :: cell_i
    2658             : 
    2659      205408 :       CALL timeset(routineN, handle)
    2660             : 
    2661      205408 :       nimg = SIZE(index_to_cell, 1)
    2662             : 
    2663      205408 :       cell_found = .FALSE.
    2664             : 
    2665     2144394 :       DO i_cell = 1, nimg
    2666             : 
    2667     7755944 :          cell_i(1:3) = index_to_cell(i_cell, 1:3)
    2668             : 
    2669     2144394 :          IF (cell_i(1) == cell(1) .AND. cell_i(2) == cell(2) .AND. cell_i(3) == cell(3)) THEN
    2670      117317 :             cell_found = .TRUE.
    2671             :          END IF
    2672             : 
    2673             :       END DO
    2674             : 
    2675      205408 :       CALL timestop(handle)
    2676             : 
    2677      205408 :    END SUBROUTINE is_cell_in_index_to_cell
    2678             : 
    2679             : ! **************************************************************************************************
    2680             : !> \brief ...
    2681             : !> \param qs_env ...
    2682             : !> \param bs_env ...
    2683             : ! **************************************************************************************************
    2684           6 :    SUBROUTINE allocate_matrices_small_cell_full_kp(qs_env, bs_env)
    2685             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2686             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2687             : 
    2688             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_matrices_small_cell_full_kp'
    2689             : 
    2690             :       INTEGER                                            :: handle, i_spin, i_t, img, n_spin, &
    2691             :                                                             nimages_scf, num_time_freq_points
    2692             :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
    2693             :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2694             : 
    2695           6 :       CALL timeset(routineN, handle)
    2696             : 
    2697           6 :       nimages_scf = bs_env%nimages_scf_desymm
    2698           6 :       num_time_freq_points = bs_env%num_time_freq_points
    2699           6 :       n_spin = bs_env%n_spin
    2700             : 
    2701           6 :       CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env)
    2702             : 
    2703          72 :       ALLOCATE (bs_env%fm_G_S(nimages_scf))
    2704          72 :       ALLOCATE (bs_env%fm_Sigma_x_R(nimages_scf))
    2705         544 :       ALLOCATE (bs_env%fm_chi_R_t(nimages_scf, num_time_freq_points))
    2706         544 :       ALLOCATE (bs_env%fm_MWM_R_t(nimages_scf, num_time_freq_points))
    2707         556 :       ALLOCATE (bs_env%fm_Sigma_c_R_neg_tau(nimages_scf, num_time_freq_points, n_spin))
    2708         556 :       ALLOCATE (bs_env%fm_Sigma_c_R_pos_tau(nimages_scf, num_time_freq_points, n_spin))
    2709          60 :       DO img = 1, nimages_scf
    2710          54 :          CALL cp_fm_create(bs_env%fm_G_S(img), bs_env%fm_work_mo(1)%matrix_struct)
    2711          54 :          CALL cp_fm_create(bs_env%fm_Sigma_x_R(img), bs_env%fm_work_mo(1)%matrix_struct)
    2712         528 :          DO i_t = 1, num_time_freq_points
    2713         468 :             CALL cp_fm_create(bs_env%fm_chi_R_t(img, i_t), bs_env%fm_RI_RI%matrix_struct)
    2714         468 :             CALL cp_fm_create(bs_env%fm_MWM_R_t(img, i_t), bs_env%fm_RI_RI%matrix_struct)
    2715         468 :             CALL cp_fm_set_all(bs_env%fm_MWM_R_t(img, i_t), 0.0_dp)
    2716         990 :             DO i_spin = 1, n_spin
    2717             :                CALL cp_fm_create(bs_env%fm_Sigma_c_R_neg_tau(img, i_t, i_spin), &
    2718         468 :                                  bs_env%fm_work_mo(1)%matrix_struct)
    2719             :                CALL cp_fm_create(bs_env%fm_Sigma_c_R_pos_tau(img, i_t, i_spin), &
    2720         468 :                                  bs_env%fm_work_mo(1)%matrix_struct)
    2721         468 :                CALL cp_fm_set_all(bs_env%fm_Sigma_c_R_neg_tau(img, i_t, i_spin), 0.0_dp)
    2722         936 :                CALL cp_fm_set_all(bs_env%fm_Sigma_c_R_pos_tau(img, i_t, i_spin), 0.0_dp)
    2723             :             END DO
    2724             :          END DO
    2725             :       END DO
    2726             : 
    2727           6 :       CALL timestop(handle)
    2728             : 
    2729           6 :    END SUBROUTINE allocate_matrices_small_cell_full_kp
    2730             : 
    2731             : ! **************************************************************************************************
    2732             : !> \brief ...
    2733             : !> \param qs_env ...
    2734             : !> \param bs_env ...
    2735             : ! **************************************************************************************************
    2736           6 :    SUBROUTINE trafo_V_xc_R_to_kp(qs_env, bs_env)
    2737             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2738             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2739             : 
    2740             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'trafo_V_xc_R_to_kp'
    2741             : 
    2742             :       INTEGER                                            :: handle, ikp, img, ispin, n_ao
    2743           6 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index_scf
    2744             :       TYPE(cp_cfm_type)                                  :: cfm_mo_coeff, cfm_tmp, cfm_V_xc
    2745             :       TYPE(cp_fm_type)                                   :: fm_V_xc_re
    2746           6 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks
    2747             :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
    2748             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    2749           6 :          POINTER                                         :: sab_nl
    2750             : 
    2751           6 :       CALL timeset(routineN, handle)
    2752             : 
    2753           6 :       n_ao = bs_env%n_ao
    2754             : 
    2755           6 :       CALL get_qs_env(qs_env, matrix_ks_kp=matrix_ks, kpoints=kpoints_scf)
    2756             : 
    2757           6 :       NULLIFY (sab_nl)
    2758           6 :       CALL get_kpoint_info(kpoints_scf, sab_nl=sab_nl, cell_to_index=cell_to_index_scf)
    2759             : 
    2760           6 :       CALL cp_cfm_create(cfm_V_xc, bs_env%cfm_work_mo%matrix_struct)
    2761           6 :       CALL cp_cfm_create(cfm_mo_coeff, bs_env%cfm_work_mo%matrix_struct)
    2762           6 :       CALL cp_cfm_create(cfm_tmp, bs_env%cfm_work_mo%matrix_struct)
    2763           6 :       CALL cp_fm_create(fm_V_xc_re, bs_env%cfm_work_mo%matrix_struct)
    2764             : 
    2765         152 :       DO img = 1, bs_env%nimages_scf
    2766         298 :          DO ispin = 1, bs_env%n_spin
    2767             :             ! JW kind of hack because the format of matrix_ks remains dubious...
    2768         146 :             CALL dbcsr_set(matrix_ks(ispin, img)%matrix, 0.0_dp)
    2769         292 :             CALL copy_fm_to_dbcsr(bs_env%fm_V_xc_R(img, ispin), matrix_ks(ispin, img)%matrix)
    2770             :          END DO
    2771             :       END DO
    2772             : 
    2773          30 :       ALLOCATE (bs_env%v_xc_n(n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
    2774             : 
    2775          12 :       DO ispin = 1, bs_env%n_spin
    2776         232 :          DO ikp = 1, bs_env%nkp_bs_and_DOS
    2777             : 
    2778             :             ! v^xc^R -> v^xc(k)  (matrix_ks stores v^xc^R, see SUBROUTINE compute_V_xc)
    2779             :             CALL rsmat_to_kp(matrix_ks, ispin, bs_env%kpoints_DOS%xkp(1:3, ikp), &
    2780         220 :                              cell_to_index_scf, sab_nl, bs_env, cfm_V_xc)
    2781             : 
    2782             :             ! get C_µn(k)
    2783         220 :             CALL cp_cfm_to_cfm(bs_env%cfm_mo_coeff_kp(ikp, ispin), cfm_mo_coeff)
    2784             : 
    2785             :             ! v^xc_nm(k_i) = sum_µν C^*_µn(k_i) v^xc_µν(k_i) C_νn(k_i)
    2786             :             CALL parallel_gemm('N', 'N', n_ao, n_ao, n_ao, z_one, cfm_V_xc, cfm_mo_coeff, &
    2787         220 :                                z_zero, cfm_tmp)
    2788             :             CALL parallel_gemm('C', 'N', n_ao, n_ao, n_ao, z_one, cfm_mo_coeff, cfm_tmp, &
    2789         220 :                                z_zero, cfm_V_xc)
    2790             : 
    2791             :             ! get v^xc_nn(k_i) which is a real quantity as v^xc is Hermitian
    2792         220 :             CALL cp_cfm_to_fm(cfm_V_xc, fm_V_xc_re)
    2793         226 :             CALL cp_fm_get_diag(fm_V_xc_re, bs_env%v_xc_n(:, ikp, ispin))
    2794             : 
    2795             :          END DO
    2796             : 
    2797             :       END DO
    2798             : 
    2799             :       ! just rebuild the overwritten KS matrix again
    2800           6 :       CALL qs_ks_build_kohn_sham_matrix(qs_env, calculate_forces=.FALSE., just_energy=.FALSE.)
    2801             : 
    2802           6 :       CALL cp_cfm_release(cfm_V_xc)
    2803           6 :       CALL cp_cfm_release(cfm_mo_coeff)
    2804           6 :       CALL cp_cfm_release(cfm_tmp)
    2805           6 :       CALL cp_fm_release(fm_V_xc_re)
    2806             : 
    2807           6 :       CALL timestop(handle)
    2808             : 
    2809          12 :    END SUBROUTINE trafo_V_xc_R_to_kp
    2810             : 
    2811             : ! **************************************************************************************************
    2812             : !> \brief ...
    2813             : !> \param qs_env ...
    2814             : !> \param bs_env ...
    2815             : ! **************************************************************************************************
    2816           6 :    SUBROUTINE heuristic_RI_regularization(qs_env, bs_env)
    2817             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2818             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2819             : 
    2820             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'heuristic_RI_regularization'
    2821             : 
    2822           6 :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)  :: M
    2823             :       INTEGER                                            :: handle, ikp, ikp_local, n_RI, nkp, &
    2824             :                                                             nkp_local, u
    2825             :       REAL(KIND=dp)                                      :: cond_nr, cond_nr_max, max_ev, &
    2826             :                                                             max_ev_ikp, min_ev, min_ev_ikp
    2827           6 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: M_R
    2828             : 
    2829           6 :       CALL timeset(routineN, handle)
    2830             : 
    2831             :       ! compute M^R_PQ = <phi_P,0|V^tr(rc)|phi_Q,R> for RI metric
    2832           6 :       CALL get_V_tr_R(M_R, bs_env%ri_metric, 0.0_dp, bs_env, qs_env)
    2833             : 
    2834           6 :       nkp = bs_env%nkp_chi_eps_W_orig_plus_extra
    2835           6 :       n_RI = bs_env%n_RI
    2836             : 
    2837           6 :       nkp_local = 0
    2838        3846 :       DO ikp = 1, nkp
    2839             :          ! trivial parallelization over k-points
    2840        3840 :          IF (MODULO(ikp, bs_env%para_env%num_pe) .NE. bs_env%para_env%mepos) CYCLE
    2841        3846 :          nkp_local = nkp_local + 1
    2842             :       END DO
    2843             : 
    2844          30 :       ALLOCATE (M(n_RI, n_RI, nkp_local))
    2845             : 
    2846           6 :       ikp_local = 0
    2847           6 :       cond_nr_max = 0.0_dp
    2848           6 :       min_ev = 1000.0_dp
    2849           6 :       max_ev = -1000.0_dp
    2850             : 
    2851        3846 :       DO ikp = 1, nkp
    2852             : 
    2853             :          ! trivial parallelization
    2854        3840 :          IF (MODULO(ikp, bs_env%para_env%num_pe) .NE. bs_env%para_env%mepos) CYCLE
    2855             : 
    2856        1920 :          ikp_local = ikp_local + 1
    2857             : 
    2858             :          ! M(k) = sum_R e^ikR M^R
    2859             :          CALL trafo_rs_to_ikp(M_R, M(:, :, ikp_local), &
    2860             :                               bs_env%kpoints_scf_desymm%index_to_cell, &
    2861        1920 :                               bs_env%kpoints_chi_eps_W%xkp(1:3, ikp))
    2862             : 
    2863             :          ! compute condition number of M_PQ(k)
    2864        1920 :          CALL power(M(:, :, ikp_local), 1.0_dp, 0.0_dp, cond_nr, min_ev_ikp, max_ev_ikp)
    2865             : 
    2866        1920 :          IF (cond_nr > cond_nr_max) cond_nr_max = cond_nr
    2867        1920 :          IF (max_ev_ikp > max_ev) max_ev = max_ev_ikp
    2868        1926 :          IF (min_ev_ikp < min_ev) min_ev = min_ev_ikp
    2869             : 
    2870             :       END DO ! ikp
    2871             : 
    2872           6 :       CALL bs_env%para_env%max(cond_nr_max)
    2873             : 
    2874           6 :       u = bs_env%unit_nr
    2875           6 :       IF (u > 0) THEN
    2876           3 :          WRITE (u, FMT="(T2,A,ES34.1)") "Min. abs. eigenvalue of RI metric matrix M(k)", min_ev
    2877           3 :          WRITE (u, FMT="(T2,A,ES34.1)") "Max. abs. eigenvalue of RI metric matrix M(k)", max_ev
    2878           3 :          WRITE (u, FMT="(T2,A,ES50.1)") "Max. condition number of M(k)", cond_nr_max
    2879             :       END IF
    2880             : 
    2881           6 :       CALL timestop(handle)
    2882             : 
    2883          12 :    END SUBROUTINE heuristic_RI_regularization
    2884             : 
    2885             : ! **************************************************************************************************
    2886             : !> \brief ...
    2887             : !> \param V_tr_R ...
    2888             : !> \param pot_type ...
    2889             : !> \param regularization_RI ...
    2890             : !> \param bs_env ...
    2891             : !> \param qs_env ...
    2892             : ! **************************************************************************************************
    2893          76 :    SUBROUTINE get_V_tr_R(V_tr_R, pot_type, regularization_RI, bs_env, qs_env)
    2894             :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: V_tr_R
    2895             :       TYPE(libint_potential_type)                        :: pot_type
    2896             :       REAL(KIND=dp)                                      :: regularization_RI
    2897             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2898             :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2899             : 
    2900             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_V_tr_R'
    2901             : 
    2902             :       INTEGER                                            :: handle, img, nimages_scf_desymm
    2903             :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: sizes_RI
    2904          76 :       INTEGER, DIMENSION(:), POINTER                     :: col_bsize, row_bsize
    2905             :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
    2906          76 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: fm_V_tr_R
    2907             :       TYPE(dbcsr_distribution_type)                      :: dbcsr_dist
    2908          76 :       TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:)        :: mat_V_tr_R
    2909             :       TYPE(distribution_2d_type), POINTER                :: dist_2d
    2910             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    2911          76 :          POINTER                                         :: sab_RI
    2912          76 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2913          76 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2914             : 
    2915          76 :       CALL timeset(routineN, handle)
    2916             : 
    2917          76 :       NULLIFY (sab_RI, dist_2d)
    2918             : 
    2919             :       CALL get_qs_env(qs_env=qs_env, &
    2920             :                       blacs_env=blacs_env, &
    2921             :                       distribution_2d=dist_2d, &
    2922             :                       qs_kind_set=qs_kind_set, &
    2923          76 :                       particle_set=particle_set)
    2924             : 
    2925         228 :       ALLOCATE (sizes_RI(bs_env%n_atom))
    2926          76 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=sizes_RI, basis=bs_env%basis_set_RI)
    2927             :       CALL build_2c_neighbor_lists(sab_RI, bs_env%basis_set_RI, bs_env%basis_set_RI, &
    2928             :                                    pot_type, "2c_nl_RI", qs_env, sym_ij=.FALSE., &
    2929          76 :                                    dist_2d=dist_2d)
    2930          76 :       CALL cp_dbcsr_dist2d_to_dist(dist_2d, dbcsr_dist)
    2931         228 :       ALLOCATE (row_bsize(SIZE(sizes_RI)))
    2932         228 :       ALLOCATE (col_bsize(SIZE(sizes_RI)))
    2933         248 :       row_bsize(:) = sizes_RI
    2934         248 :       col_bsize(:) = sizes_RI
    2935             : 
    2936          76 :       nimages_scf_desymm = bs_env%nimages_scf_desymm
    2937         912 :       ALLOCATE (mat_V_tr_R(nimages_scf_desymm))
    2938             :       CALL dbcsr_create(mat_V_tr_R(1), "(RI|RI)", dbcsr_dist, dbcsr_type_no_symmetry, &
    2939          76 :                         row_bsize, col_bsize)
    2940          76 :       DEALLOCATE (row_bsize, col_bsize)
    2941             : 
    2942         684 :       DO img = 2, nimages_scf_desymm
    2943         684 :          CALL dbcsr_create(mat_V_tr_R(img), template=mat_V_tr_R(1))
    2944             :       END DO
    2945             : 
    2946             :       CALL build_2c_integrals(mat_V_tr_R, 0.0_dp, qs_env, sab_RI, bs_env%basis_set_RI, &
    2947             :                               bs_env%basis_set_RI, pot_type, do_kpoints=.TRUE., &
    2948             :                               ext_kpoints=bs_env%kpoints_scf_desymm, &
    2949          76 :                               regularization_RI=regularization_RI)
    2950             : 
    2951         912 :       ALLOCATE (fm_V_tr_R(nimages_scf_desymm))
    2952         760 :       DO img = 1, nimages_scf_desymm
    2953         684 :          CALL cp_fm_create(fm_V_tr_R(img), bs_env%fm_RI_RI%matrix_struct)
    2954         684 :          CALL copy_dbcsr_to_fm(mat_V_tr_R(img), fm_V_tr_R(img))
    2955         760 :          CALL dbcsr_release(mat_V_tr_R(img))
    2956             :       END DO
    2957             : 
    2958          76 :       IF (.NOT. ALLOCATED(V_tr_R)) THEN
    2959         380 :          ALLOCATE (V_tr_R(bs_env%n_RI, bs_env%n_RI, nimages_scf_desymm))
    2960             :       END IF
    2961             : 
    2962          76 :       CALL fm_to_local_array(fm_V_tr_R, V_tr_R)
    2963             : 
    2964          76 :       CALL cp_fm_release(fm_V_tr_R)
    2965          76 :       CALL dbcsr_distribution_release(dbcsr_dist)
    2966          76 :       CALL release_neighbor_list_sets(sab_RI)
    2967             : 
    2968          76 :       CALL timestop(handle)
    2969             : 
    2970         228 :    END SUBROUTINE get_V_tr_R
    2971             : 
    2972             : ! **************************************************************************************************
    2973             : !> \brief ...
    2974             : !> \param matrix ...
    2975             : !> \param exponent ...
    2976             : !> \param eps ...
    2977             : !> \param cond_nr ...
    2978             : !> \param min_ev ...
    2979             : !> \param max_ev ...
    2980             : ! **************************************************************************************************
    2981       39504 :    SUBROUTINE power(matrix, exponent, eps, cond_nr, min_ev, max_ev)
    2982             :       COMPLEX(KIND=dp), DIMENSION(:, :)                  :: matrix
    2983             :       REAL(KIND=dp)                                      :: exponent, eps
    2984             :       REAL(KIND=dp), OPTIONAL                            :: cond_nr, min_ev, max_ev
    2985             : 
    2986             :       CHARACTER(len=*), PARAMETER                        :: routineN = 'power'
    2987             : 
    2988       39504 :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :)     :: eigenvectors
    2989             :       INTEGER                                            :: handle, i, n
    2990             :       REAL(KIND=dp)                                      :: pos_eval
    2991       39504 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenvalues
    2992             : 
    2993       39504 :       CALL timeset(routineN, handle)
    2994             : 
    2995             :       ! make matrix perfectly Hermitian
    2996     2642832 :       matrix(:, :) = 0.5_dp*(matrix(:, :) + CONJG(TRANSPOSE(matrix(:, :))))
    2997             : 
    2998       39504 :       n = SIZE(matrix, 1)
    2999      237024 :       ALLOCATE (eigenvalues(n), eigenvectors(n, n))
    3000       39504 :       CALL diag_complex(matrix, eigenvectors, eigenvalues)
    3001             : 
    3002       63824 :       IF (PRESENT(cond_nr)) cond_nr = MAXVAL(ABS(eigenvalues))/MINVAL(ABS(eigenvalues))
    3003       51664 :       IF (PRESENT(min_ev)) min_ev = MINVAL(ABS(eigenvalues))
    3004       51664 :       IF (PRESENT(max_ev)) max_ev = MAXVAL(ABS(eigenvalues))
    3005             : 
    3006      246736 :       DO i = 1, n
    3007      207232 :          IF (eps < eigenvalues(i)) THEN
    3008      207232 :             pos_eval = (eigenvalues(i))**(0.5_dp*exponent)
    3009             :          ELSE
    3010             :             pos_eval = 0.0_dp
    3011             :          END IF
    3012     1341168 :          eigenvectors(:, i) = eigenvectors(:, i)*pos_eval
    3013             :       END DO
    3014             : 
    3015       39504 :       CALL ZGEMM("N", "C", n, n, n, z_one, eigenvectors, n, eigenvectors, n, z_zero, matrix, n)
    3016             : 
    3017       39504 :       DEALLOCATE (eigenvalues, eigenvectors)
    3018             : 
    3019       39504 :       CALL timestop(handle)
    3020             : 
    3021       39504 :    END SUBROUTINE power
    3022             : 
    3023             : ! **************************************************************************************************
    3024             : !> \brief ...
    3025             : !> \param bs_env ...
    3026             : !> \param Sigma_c_n_time ...
    3027             : !> \param Sigma_c_n_freq ...
    3028             : !> \param ispin ...
    3029             : ! **************************************************************************************************
    3030         276 :    SUBROUTINE time_to_freq(bs_env, Sigma_c_n_time, Sigma_c_n_freq, ispin)
    3031             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3032             :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: Sigma_c_n_time, Sigma_c_n_freq
    3033             :       INTEGER                                            :: ispin
    3034             : 
    3035             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'time_to_freq'
    3036             : 
    3037             :       INTEGER                                            :: handle, i_t, j_w, n_occ
    3038             :       REAL(KIND=dp)                                      :: freq_j, time_i, w_cos_ij, w_sin_ij
    3039         276 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: Sigma_c_n_cos_time, Sigma_c_n_sin_time
    3040             : 
    3041         276 :       CALL timeset(routineN, handle)
    3042             : 
    3043        1104 :       ALLOCATE (Sigma_c_n_cos_time(bs_env%n_ao, bs_env%num_time_freq_points))
    3044         828 :       ALLOCATE (Sigma_c_n_sin_time(bs_env%n_ao, bs_env%num_time_freq_points))
    3045             : 
    3046       27428 :       Sigma_c_n_cos_time(:, :) = 0.5_dp*(Sigma_c_n_time(:, :, 1) + Sigma_c_n_time(:, :, 2))
    3047       27428 :       Sigma_c_n_sin_time(:, :) = 0.5_dp*(Sigma_c_n_time(:, :, 1) - Sigma_c_n_time(:, :, 2))
    3048             : 
    3049       55132 :       Sigma_c_n_freq(:, :, :) = 0.0_dp
    3050             : 
    3051        2996 :       DO i_t = 1, bs_env%num_time_freq_points
    3052             : 
    3053       31636 :          DO j_w = 1, bs_env%num_time_freq_points
    3054             : 
    3055       28640 :             freq_j = bs_env%imag_freq_points(j_w)
    3056       28640 :             time_i = bs_env%imag_time_points(i_t)
    3057             :             ! integration weights for cosine and sine transform
    3058       28640 :             w_cos_ij = bs_env%weights_cos_t_to_w(j_w, i_t)*COS(freq_j*time_i)
    3059       28640 :             w_sin_ij = bs_env%weights_sin_t_to_w(j_w, i_t)*SIN(freq_j*time_i)
    3060             : 
    3061             :             ! 1. Re(Σ^c_nn(k_i,iω)) from cosine transform
    3062             :             Sigma_c_n_freq(:, j_w, 1) = Sigma_c_n_freq(:, j_w, 1) + &
    3063      262592 :                                         w_cos_ij*Sigma_c_n_cos_time(:, i_t)
    3064             : 
    3065             :             ! 2. Im(Σ^c_nn(k_i,iω)) from sine transform
    3066             :             Sigma_c_n_freq(:, j_w, 2) = Sigma_c_n_freq(:, j_w, 2) + &
    3067      265312 :                                         w_sin_ij*Sigma_c_n_sin_time(:, i_t)
    3068             : 
    3069             :          END DO
    3070             : 
    3071             :       END DO
    3072             : 
    3073             :       ! for occupied levels, we need the correlation self-energy for negative omega.
    3074             :       ! Therefore, weight_sin should be computed with -omega, which results in an
    3075             :       ! additional minus for the imaginary part:
    3076         276 :       n_occ = bs_env%n_occ(ispin)
    3077       12996 :       Sigma_c_n_freq(1:n_occ, :, 2) = -Sigma_c_n_freq(1:n_occ, :, 2)
    3078             : 
    3079         276 :       CALL timestop(handle)
    3080             : 
    3081         552 :    END SUBROUTINE time_to_freq
    3082             : 
    3083             : ! **************************************************************************************************
    3084             : !> \brief ...
    3085             : !> \param bs_env ...
    3086             : !> \param Sigma_c_ikp_n_freq ...
    3087             : !> \param Sigma_x_ikp_n ...
    3088             : !> \param V_xc_ikp_n ...
    3089             : !> \param eigenval_scf ...
    3090             : !> \param ikp ...
    3091             : !> \param ispin ...
    3092             : ! **************************************************************************************************
    3093         276 :    SUBROUTINE analyt_conti_and_print(bs_env, Sigma_c_ikp_n_freq, Sigma_x_ikp_n, V_xc_ikp_n, &
    3094         276 :                                      eigenval_scf, ikp, ispin)
    3095             : 
    3096             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3097             :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: Sigma_c_ikp_n_freq
    3098             :       REAL(KIND=dp), DIMENSION(:)                        :: Sigma_x_ikp_n, V_xc_ikp_n, eigenval_scf
    3099             :       INTEGER                                            :: ikp, ispin
    3100             : 
    3101             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'analyt_conti_and_print'
    3102             : 
    3103             :       CHARACTER(len=3)                                   :: occ_vir
    3104             :       CHARACTER(len=default_string_length)               :: fname
    3105             :       INTEGER                                            :: handle, i_mo, ikp_for_print, iunit, &
    3106             :                                                             n_mo, nkp
    3107             :       LOGICAL                                            :: is_bandstruc_kpoint, print_DOS_kpoints, &
    3108             :                                                             print_ikp
    3109             :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: dummy, Sigma_c_ikp_n_qp
    3110             : 
    3111         276 :       CALL timeset(routineN, handle)
    3112             : 
    3113         276 :       n_mo = bs_env%n_ao
    3114        1104 :       ALLOCATE (dummy(n_mo), Sigma_c_ikp_n_qp(n_mo))
    3115        2948 :       Sigma_c_ikp_n_qp(:) = 0.0_dp
    3116             : 
    3117        2948 :       DO i_mo = 1, n_mo
    3118             : 
    3119             :          ! parallelization
    3120        2672 :          IF (MODULO(i_mo, bs_env%para_env%num_pe) /= bs_env%para_env%mepos) CYCLE
    3121             : 
    3122             :          CALL continuation_pade(Sigma_c_ikp_n_qp, &
    3123             :                                 bs_env%imag_freq_points_fit, dummy, dummy, &
    3124             :                                 Sigma_c_ikp_n_freq(:, 1:bs_env%num_freq_points_fit, 1)*z_one + &
    3125             :                                 Sigma_c_ikp_n_freq(:, 1:bs_env%num_freq_points_fit, 2)*gaussi, &
    3126             :                                 Sigma_x_ikp_n(:) - V_xc_ikp_n(:), &
    3127             :                                 eigenval_scf(:), eigenval_scf(:), &
    3128             :                                 bs_env%do_hedin_shift, &
    3129             :                                 i_mo, bs_env%n_occ(ispin), bs_env%n_vir(ispin), &
    3130             :                                 bs_env%nparam_pade, bs_env%num_freq_points_fit, &
    3131             :                                 ri_rpa_g0w0_crossing_newton, bs_env%n_occ(ispin), &
    3132       87412 :                                 0.0_dp, .TRUE., .FALSE., 1, e_fermi_ext=bs_env%e_fermi(ispin))
    3133             :       END DO
    3134             : 
    3135         276 :       CALL bs_env%para_env%sum(Sigma_c_ikp_n_qp)
    3136             : 
    3137         276 :       CALL correct_obvious_fitting_fails(Sigma_c_ikp_n_qp, ispin, bs_env)
    3138             : 
    3139             :       bs_env%eigenval_G0W0(:, ikp, ispin) = eigenval_scf(:) + &
    3140             :                                             Sigma_c_ikp_n_qp(:) + &
    3141             :                                             Sigma_x_ikp_n(:) - &
    3142        2948 :                                             V_xc_ikp_n(:)
    3143             : 
    3144        2948 :       bs_env%eigenval_HF(:, ikp, ispin) = eigenval_scf(:) + Sigma_x_ikp_n(:) - V_xc_ikp_n(:)
    3145             : 
    3146             :       ! only print eigenvalues of DOS k-points in case no bandstructure path has been given
    3147         276 :       print_DOS_kpoints = (bs_env%nkp_only_bs .LE. 0)
    3148             :       ! in kpoints_DOS, the last nkp_only_bs are bandstructure k-points
    3149         276 :       is_bandstruc_kpoint = (ikp > bs_env%nkp_only_DOS)
    3150         276 :       print_ikp = print_DOS_kpoints .OR. is_bandstruc_kpoint
    3151             : 
    3152         276 :       IF (bs_env%para_env%is_source() .AND. print_ikp) THEN
    3153             : 
    3154         106 :          IF (print_DOS_kpoints) THEN
    3155          44 :             nkp = bs_env%nkp_only_DOS
    3156          44 :             ikp_for_print = ikp
    3157             :          ELSE
    3158          62 :             nkp = bs_env%nkp_only_bs
    3159          62 :             ikp_for_print = ikp - bs_env%nkp_only_DOS
    3160             :          END IF
    3161             : 
    3162         106 :          fname = "bandstructure_SCF_and_G0W0"
    3163             : 
    3164         106 :          IF (ikp_for_print == 1) THEN
    3165             :             CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", &
    3166          21 :                            file_action="WRITE")
    3167             :          ELSE
    3168             :             CALL open_file(TRIM(fname), unit_number=iunit, file_status="OLD", &
    3169          85 :                            file_action="WRITE", file_position="APPEND")
    3170             :          END IF
    3171             : 
    3172         106 :          WRITE (iunit, "(A)") " "
    3173         106 :          WRITE (iunit, "(A10,I7,A25,3F10.4)") "kpoint: ", ikp_for_print, "coordinate: ", &
    3174         212 :             bs_env%kpoints_DOS%xkp(:, ikp)
    3175         106 :          WRITE (iunit, "(A)") " "
    3176         106 :          WRITE (iunit, "(A5,A12,3A17,A16,A18)") "n", "k", "ϵ_nk^DFT (eV)", "Σ^c_nk (eV)", &
    3177         212 :             "Σ^x_nk (eV)", "v_nk^xc (eV)", "ϵ_nk^G0W0 (eV)"
    3178         106 :          WRITE (iunit, "(A)") " "
    3179             : 
    3180        1154 :          DO i_mo = 1, n_mo
    3181        1048 :             IF (i_mo .LE. bs_env%n_occ(ispin)) occ_vir = 'occ'
    3182        1048 :             IF (i_mo > bs_env%n_occ(ispin)) occ_vir = 'vir'
    3183        1048 :             WRITE (iunit, "(I5,3A,I5,4F16.3,F17.3)") i_mo, ' (', occ_vir, ') ', ikp_for_print, &
    3184        1048 :                eigenval_scf(i_mo)*evolt, &
    3185        1048 :                Sigma_c_ikp_n_qp(i_mo)*evolt, &
    3186        1048 :                Sigma_x_ikp_n(i_mo)*evolt, &
    3187        1048 :                V_xc_ikp_n(i_mo)*evolt, &
    3188        2202 :                bs_env%eigenval_G0W0(i_mo, ikp, ispin)*evolt
    3189             :          END DO
    3190             : 
    3191         106 :          WRITE (iunit, "(A)") " "
    3192             : 
    3193         106 :          CALL close_file(iunit)
    3194             : 
    3195             :       END IF
    3196             : 
    3197         276 :       CALL timestop(handle)
    3198             : 
    3199         552 :    END SUBROUTINE analyt_conti_and_print
    3200             : 
    3201             : ! **************************************************************************************************
    3202             : !> \brief ...
    3203             : !> \param Sigma_c_ikp_n_qp ...
    3204             : !> \param ispin ...
    3205             : !> \param bs_env ...
    3206             : ! **************************************************************************************************
    3207         276 :    SUBROUTINE correct_obvious_fitting_fails(Sigma_c_ikp_n_qp, ispin, bs_env)
    3208             :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: Sigma_c_ikp_n_qp
    3209             :       INTEGER                                            :: ispin
    3210             :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3211             : 
    3212             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'correct_obvious_fitting_fails'
    3213             : 
    3214             :       INTEGER                                            :: handle, homo, i_mo, j_mo, &
    3215             :                                                             n_levels_scissor, n_mo
    3216             :       LOGICAL                                            :: is_occ, is_vir
    3217             :       REAL(KIND=dp)                                      :: sum_Sigma_c
    3218             : 
    3219         276 :       CALL timeset(routineN, handle)
    3220             : 
    3221         276 :       n_mo = bs_env%n_ao
    3222         276 :       homo = bs_env%n_occ(ispin)
    3223             : 
    3224        2948 :       DO i_mo = 1, n_mo
    3225             : 
    3226             :          ! if |𝚺^c| > 13 eV, we use a scissors shift
    3227        2948 :          IF (ABS(Sigma_c_ikp_n_qp(i_mo)) > 13.0_dp/evolt) THEN
    3228             : 
    3229           0 :             is_occ = (i_mo .LE. homo)
    3230           0 :             is_vir = (i_mo > homo)
    3231             : 
    3232           0 :             n_levels_scissor = 0
    3233           0 :             sum_Sigma_c = 0.0_dp
    3234             : 
    3235             :             ! compute scissor
    3236           0 :             DO j_mo = 1, n_mo
    3237             : 
    3238             :                ! only compute scissor from other GW levels close in energy
    3239           0 :                IF (is_occ .AND. j_mo > homo) CYCLE
    3240           0 :                IF (is_vir .AND. j_mo .LE. homo) CYCLE
    3241           0 :                IF (ABS(i_mo - j_mo) > 10) CYCLE
    3242           0 :                IF (i_mo == j_mo) CYCLE
    3243             : 
    3244           0 :                n_levels_scissor = n_levels_scissor + 1
    3245           0 :                sum_Sigma_c = sum_Sigma_c + Sigma_c_ikp_n_qp(j_mo)
    3246             : 
    3247             :             END DO
    3248             : 
    3249             :             ! overwrite the self-energy with scissor shift
    3250           0 :             Sigma_c_ikp_n_qp(i_mo) = sum_Sigma_c/REAL(n_levels_scissor, KIND=dp)
    3251             : 
    3252             :          END IF
    3253             : 
    3254             :       END DO ! i_mo
    3255             : 
    3256         276 :       CALL timestop(handle)
    3257             : 
    3258         276 :    END SUBROUTINE correct_obvious_fitting_fails
    3259             : 
    3260             : END MODULE gw_utils

Generated by: LCOV version 1.15