LCOV - code coverage report
Current view: top level - src - post_scf_bandstructure_utils.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 84.9 % 1562 1326
Test Date: 2026-11-24 01:22:08 Functions: 94.0 % 50 47

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief
      10              : !> \author Jan Wilhelm
      11              : !> \date 07.2023
      12              : ! **************************************************************************************************
      13              : MODULE post_scf_bandstructure_utils
      14              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      15              :                                               get_atomic_kind,&
      16              :                                               get_atomic_kind_set
      17              :    USE bse_input,                       ONLY: read_bse_section
      18              :    USE bse_main,                        ONLY: bse_fulldiag_memory_check
      19              :    USE bse_types,                       ONLY: bse_env_type
      20              :    USE bse_util,                        ONLY: bse_window_from_dft
      21              :    USE cell_types,                      ONLY: cell_type,&
      22              :                                               get_cell
      23              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      24              :                                               cp_blacs_env_release,&
      25              :                                               cp_blacs_env_type
      26              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_scale
      27              :    USE cp_cfm_cholesky,                 ONLY: cp_cfm_cholesky_decompose
      28              :    USE cp_cfm_diag,                     ONLY: cp_cfm_diag_workspace_type,&
      29              :                                               cp_cfm_geeig,&
      30              :                                               cp_cfm_geeig_canon_local,&
      31              :                                               cp_cfm_heevd
      32              :    USE cp_cfm_types,                    ONLY: &
      33              :         cp_cfm_copy_general, cp_cfm_create, cp_cfm_get_info, cp_cfm_release, cp_cfm_set_all, &
      34              :         cp_cfm_to_cfm, cp_cfm_to_fm, cp_cfm_type, cp_fm_to_cfm
      35              :    USE cp_control_types,                ONLY: dft_control_type
      36              :    USE cp_dbcsr_api,                    ONLY: dbcsr_create,&
      37              :                                               dbcsr_deallocate_matrix,&
      38              :                                               dbcsr_p_type,&
      39              :                                               dbcsr_set,&
      40              :                                               dbcsr_type,&
      41              :                                               dbcsr_type_antisymmetric,&
      42              :                                               dbcsr_type_no_symmetry,&
      43              :                                               dbcsr_type_symmetric
      44              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      45              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      46              :                                               copy_fm_to_dbcsr,&
      47              :                                               dbcsr_allocate_matrix_set,&
      48              :                                               dbcsr_deallocate_matrix_set
      49              :    USE cp_files,                        ONLY: close_file,&
      50              :                                               open_file
      51              :    USE cp_fm_diag,                      ONLY: cp_fm_geeig_canon,&
      52              :                                               set_removed_eigval_to
      53              :    USE cp_fm_pool_types,                ONLY: cp_fm_pool_type,&
      54              :                                               fm_pool_get_el_struct
      55              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      56              :                                               cp_fm_struct_release,&
      57              :                                               cp_fm_struct_type
      58              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      59              :                                               cp_fm_get_diag,&
      60              :                                               cp_fm_get_info,&
      61              :                                               cp_fm_release,&
      62              :                                               cp_fm_set_all,&
      63              :                                               cp_fm_to_fm,&
      64              :                                               cp_fm_to_fm_submat,&
      65              :                                               cp_fm_type
      66              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit,&
      67              :                                               cp_to_string
      68              :    USE cp_parser_methods,               ONLY: read_float_object
      69              :    USE dft_plus_u,                      ONLY: lowdin_kp_apply,&
      70              :                                               lowdin_kp_prepare
      71              :    USE input_constants,                 ONLY: &
      72              :         G0W0, bse_fulldiag, cohsex_off, evGW0, int_ldos_z, ri_rs_large_cell_Gamma, &
      73              :         ri_rs_non_periodic, ri_rs_small_cell_full_kp, smear_fermi_dirac, smear_gaussian, smear_mp, &
      74              :         smear_mv, tensor_large_cell_Gamma, tensor_small_cell_full_kp, tensor_static_cohsex
      75              :    USE input_section_types,             ONLY: section_vals_get,&
      76              :                                               section_vals_get_subs_vals,&
      77              :                                               section_vals_type,&
      78              :                                               section_vals_val_get
      79              :    USE kinds,                           ONLY: default_string_length,&
      80              :                                               dp,&
      81              :                                               max_line_length
      82              :    USE kpoint_k_r_trafo_simple,         ONLY: fm_gamma_to_kp
      83              :    USE kpoint_methods,                  ONLY: kpoint_env_initialize,&
      84              :                                               kpoint_init_cell_index,&
      85              :                                               kpoint_initialize_mos,&
      86              :                                               rskp_transform
      87              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      88              :                                               kpoint_create,&
      89              :                                               kpoint_env_type,&
      90              :                                               kpoint_release,&
      91              :                                               kpoint_type
      92              :    USE machine,                         ONLY: m_walltime
      93              :    USE mathconstants,                   ONLY: gaussi,&
      94              :                                               twopi,&
      95              :                                               z_one,&
      96              :                                               z_zero
      97              :    USE message_passing,                 ONLY: mp_para_env_release,&
      98              :                                               mp_para_env_type
      99              :    USE mo_window,                       ONLY: mo_window_type
     100              :    USE parallel_gemm_api,               ONLY: parallel_gemm
     101              :    USE particle_types,                  ONLY: particle_type
     102              :    USE physcon,                         ONLY: angstrom,&
     103              :                                               evolt
     104              :    USE post_scf_bandstructure_types,    ONLY: band_edges_type,&
     105              :                                               eps_qp_gap,&
     106              :                                               max_qp_gap,&
     107              :                                               post_scf_bandstructure_type
     108              :    USE pw_env_types,                    ONLY: pw_env_get,&
     109              :                                               pw_env_type
     110              :    USE pw_pool_types,                   ONLY: pw_pool_type
     111              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
     112              :                                               pw_r3d_rs_type
     113              :    USE qs_band_structure,               ONLY: calculate_kpoints_for_bs
     114              :    USE qs_collocate_density,            ONLY: calculate_rho_elec
     115              :    USE qs_environment_types,            ONLY: get_qs_env,&
     116              :                                               qs_environment_type
     117              :    USE qs_kpoint_operators,             ONLY: kpoint_operator_context_create,&
     118              :                                               kpoint_operator_context_release,&
     119              :                                               kpoint_operator_context_type,&
     120              :                                               kpoint_operator_get,&
     121              :                                               kpoint_operator_prepare,&
     122              :                                               kpoint_slot_ks,&
     123              :                                               kpoint_slot_s
     124              :    USE qs_ks_types,                     ONLY: qs_ks_env_type
     125              :    USE qs_matrix_pools,                 ONLY: mpools_get
     126              :    USE qs_mo_types,                     ONLY: get_mo_set,&
     127              :                                               mo_set_type
     128              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
     129              :    USE rpa_gw_im_time_util,             ONLY: compute_weight_re_im,&
     130              :                                               get_atom_index_from_basis_function_index
     131              :    USE scf_control_types,               ONLY: scf_control_type
     132              :    USE smearing_utils,                  ONLY: Smearkp,&
     133              :                                               Smearkp2
     134              :    USE soc_pseudopotential_methods,     ONLY: V_SOC_xyz_from_pseudopotential,&
     135              :                                               remove_soc_outside_energy_window_mo
     136              :    USE soc_pseudopotential_utils,       ONLY: add_cfm_submat,&
     137              :                                               add_dbcsr_submat,&
     138              :                                               cfm_add_on_diag,&
     139              :                                               create_cfm_double,&
     140              :                                               get_cfm_submat
     141              :    USE string_utilities,                ONLY: uppercase
     142              : #include "base/base_uses.f90"
     143              : 
     144              :    IMPLICIT NONE
     145              : 
     146              :    PRIVATE
     147              : 
     148              :    PUBLIC :: create_and_init_bs_env, &
     149              :              eval_bandstructure_properties, cfm_ikp_from_fm_Gamma, &
     150              :              MIC_contribution_from_ikp, compute_xkp, kpoint_init_cell_index_simple, &
     151              :              rsmat_to_kp, create_operator_kpoints, soc, get_VBM_CBM_bandgaps, &
     152              :              get_all_VBM_CBM_bandgaps, diagonalize_ks_matrix, gw_flavour_label
     153              : 
     154              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'post_scf_bandstructure_utils'
     155              : 
     156              : CONTAINS
     157              : 
     158              : ! **************************************************************************************************
     159              : !> \brief ...
     160              : !> \param qs_env ...
     161              : !> \param bs_env ...
     162              : !> \param post_scf_bandstructure_section ...
     163              : ! **************************************************************************************************
     164          158 :    SUBROUTINE create_and_init_bs_env(qs_env, bs_env, post_scf_bandstructure_section)
     165              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     166              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     167              :       TYPE(section_vals_type), POINTER                   :: post_scf_bandstructure_section
     168              : 
     169              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_and_init_bs_env'
     170              : 
     171              :       INTEGER                                            :: handle
     172              : 
     173          158 :       CALL timeset(routineN, handle)
     174              : 
     175        15484 :       ALLOCATE (bs_env)
     176              : 
     177          158 :       CALL print_header(bs_env)
     178              : 
     179          158 :       CALL read_bandstructure_input_parameters(bs_env, post_scf_bandstructure_section, qs_env)
     180              : 
     181          158 :       CALL get_parameters_from_qs_env(qs_env, bs_env)
     182              : 
     183          158 :       CALL set_heuristic_parameters(bs_env)
     184              : 
     185          284 :       SELECT CASE (bs_env%gw_implementation)
     186              :       CASE (tensor_large_cell_Gamma, ri_rs_large_cell_Gamma, ri_rs_non_periodic, tensor_static_cohsex)
     187              : 
     188          126 :          CALL setup_kpoints_DOS_large_cell_Gamma(qs_env, bs_env, bs_env%kpoints_DOS)
     189              : 
     190          126 :          CALL allocate_and_fill_fm_ks_fm_s(qs_env, bs_env)
     191              : 
     192          126 :          CALL diagonalize_ks_matrix(bs_env)
     193              : 
     194          126 :          CALL check_positive_definite_overlap_mat(bs_env, qs_env)
     195              : 
     196              :       CASE (tensor_small_cell_full_kp, ri_rs_small_cell_full_kp)
     197              : 
     198           32 :          CALL setup_kpoints_scf_desymm(qs_env, bs_env, bs_env%kpoints_scf_desymm, .TRUE.)
     199              : 
     200           32 :          CALL setup_kpoints_DOS_small_cell_full_kp(bs_env, bs_env%kpoints_DOS)
     201              : 
     202           32 :          CALL allocate_and_fill_fm_ks_fm_s(qs_env, bs_env)
     203              : 
     204          190 :          CALL compute_cfm_mo_coeff_kp_and_eigenval_scf_kp(qs_env, bs_env)
     205              : 
     206              :       END SELECT
     207              : 
     208          158 :       CALL read_bse_input(qs_env, bs_env)
     209              : 
     210          158 :       CALL timestop(handle)
     211              : 
     212          158 :    END SUBROUTINE create_and_init_bs_env
     213              : 
     214              : ! **************************************************************************************************
     215              : !> \brief What PROPERTIES%BSE needs the GW to keep and, with FULLDIAG, the early memory check of the
     216              : !>        full diagonalization; check_cp2k_input refused the input combinations the BSE cannot run
     217              : !> \param qs_env source of the input
     218              : !> \param bs_env SCF eigenvalues and occupations of the memory check; keep_W_freq_zero and keep_3c_nl are set
     219              : ! **************************************************************************************************
     220          158 :    SUBROUTINE read_bse_input(qs_env, bs_env)
     221              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     222              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     223              : 
     224              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'read_bse_input'
     225              : 
     226              :       INTEGER                                            :: handle, n_spin
     227              :       INTEGER, DIMENSION(2)                              :: n_mo
     228              :       LOGICAL                                            :: do_bse_props
     229          632 :       TYPE(bse_env_type)                                 :: bse_env
     230              :       TYPE(mo_window_type)                               :: window
     231              :       TYPE(section_vals_type), POINTER                   :: bse_sec, input
     232              : 
     233          158 :       CALL timeset(routineN, handle)
     234              : 
     235          158 :       NULLIFY (bse_sec, input)
     236          158 :       CALL get_qs_env(qs_env, input=input)
     237          158 :       bse_sec => section_vals_get_subs_vals(input, "PROPERTIES%BSE")
     238          158 :       CALL section_vals_get(bse_sec, explicit=do_bse_props)
     239              : 
     240          158 :       IF (do_bse_props) THEN
     241              :          ! the full diagonalization's footprint is known here, before any GW work
     242            6 :          CALL read_bse_section(bse_sec, bse_env)
     243            6 :          IF (bse_env%bse_diag_method == bse_fulldiag) THEN
     244            4 :             n_spin = bs_env%n_spin
     245              :             ! the canonical orthogonalisation parks the linearly dependent directions above n_mo_retained
     246           12 :             n_mo(:) = MIN(bs_env%n_mo_retained, bs_env%n_ao)
     247              :             ! the BSE warns about a combined window when it starts; the estimate stays silent
     248              :             CALL bse_window_from_dft(bs_env%eigenval_scf_Gamma(1:n_mo(1), 1:n_spin), n_mo(1:n_spin), &
     249              :                                      bs_env%n_occ(1:n_spin), bse_env%bse_cutoff_occ, &
     250            4 :                                      bse_env%bse_cutoff_empty, window, warn=.FALSE.)
     251              :             CALL bse_fulldiag_memory_check(bse_env, window, bs_env%n_occ(1:n_spin), bs_env%para_env, &
     252            4 :                                            bs_env%unit_nr)
     253              :          END IF
     254              :       END IF
     255          158 :       bs_env%keep_W_freq_zero = do_bse_props
     256          158 :       bs_env%keep_3c_nl = do_bse_props
     257              : 
     258          158 :       CALL timestop(handle)
     259              : 
     260          158 :    END SUBROUTINE read_bse_input
     261              : 
     262              : ! **************************************************************************************************
     263              : !> \brief ...
     264              : !> \param bs_env ...
     265              : !> \param bs_sec ...
     266              : !> \param qs_env ...
     267              : ! **************************************************************************************************
     268          158 :    SUBROUTINE read_bandstructure_input_parameters(bs_env, bs_sec, qs_env)
     269              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     270              :       TYPE(section_vals_type), POINTER                   :: bs_sec
     271              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     272              : 
     273              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'read_bandstructure_input_parameters'
     274              : 
     275              :       CHARACTER(LEN=default_string_length)               :: ustr
     276              :       CHARACTER(LEN=default_string_length), &
     277          158 :          DIMENSION(:), POINTER                           :: string_ptr
     278              :       CHARACTER(LEN=max_line_length)                     :: error_msg
     279              :       INTEGER                                            :: handle, i, ikp
     280              :       LOGICAL                                            :: do_evgw0
     281              :       REAL(KIND=dp), DIMENSION(3)                        :: kpptr
     282              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: cart_hmat
     283              :       TYPE(cell_type), POINTER                           :: cell
     284              :       TYPE(section_vals_type), POINTER                   :: dos_pdos_sec, floquet_sec, gw_sec, &
     285              :                                                             kp_bs_sec, ldos_sec, soc_sec
     286              : 
     287          158 :       CALL timeset(routineN, handle)
     288          158 :       NULLIFY (cell)
     289          158 :       CALL get_qs_env(qs_env=qs_env, cell=cell)
     290         2054 :       cart_hmat(:, :) = cell%hmat(:, :)
     291          158 :       IF (cell%input_cell_canonicalized) cart_hmat(:, :) = cell%input_hmat(:, :)
     292              : 
     293          158 :       NULLIFY (gw_sec)
     294          158 :       gw_sec => section_vals_get_subs_vals(bs_sec, "GW")
     295          158 :       CALL section_vals_get(gw_sec, explicit=bs_env%do_gw)
     296          158 :       CALL section_vals_val_get(gw_sec, "COHSEX%_SECTION_PARAMETERS_", i_val=bs_env%cohsex_flavor)
     297          158 :       bs_env%do_static_cohsex = (bs_env%cohsex_flavor /= cohsex_off)
     298          158 :       CALL section_vals_val_get(gw_sec, "RI_RS%_SECTION_PARAMETERS_", l_val=bs_env%do_gw_ri_rs)
     299          158 :       CALL section_vals_val_get(gw_sec, "EVGW0%_SECTION_PARAMETERS_", l_val=do_evgw0)
     300          158 :       IF (do_evgw0) bs_env%gw_flavour = evGW0
     301              : 
     302          158 :       NULLIFY (soc_sec)
     303          158 :       soc_sec => section_vals_get_subs_vals(bs_sec, "SOC")
     304          158 :       CALL section_vals_get(soc_sec, explicit=bs_env%do_soc)
     305              : 
     306          158 :       CALL section_vals_val_get(soc_sec, "SOC_WINDOW_OCC", r_val=bs_env%soc_window_occ)
     307          158 :       CALL section_vals_val_get(soc_sec, "SOC_WINDOW_VIRT", r_val=bs_env%soc_window_virt)
     308          158 :       CALL section_vals_val_get(soc_sec, "SOC_WINDOW_SMEARING", r_val=bs_env%soc_window_smearing)
     309              : 
     310          158 :       CALL section_vals_val_get(soc_sec, "SOC_NBANDS_OCC", i_val=bs_env%soc_nbands_occ)
     311          158 :       CALL section_vals_val_get(soc_sec, "SOC_NBANDS_VIRT", i_val=bs_env%soc_nbands_virt)
     312              : 
     313          158 :       NULLIFY (dos_pdos_sec)
     314          158 :       dos_pdos_sec => section_vals_get_subs_vals(bs_sec, "DOS")
     315          158 :       CALL section_vals_get(dos_pdos_sec, explicit=bs_env%do_dos_pdos)
     316              : 
     317          158 :       CALL section_vals_val_get(bs_sec, "DOS%KPOINTS", i_vals=bs_env%nkp_grid_DOS_input)
     318          158 :       CALL section_vals_val_get(bs_sec, "DOS%ENERGY_WINDOW", r_val=bs_env%energy_window_DOS)
     319          158 :       CALL section_vals_val_get(bs_sec, "DOS%ENERGY_STEP", r_val=bs_env%energy_step_DOS)
     320          158 :       CALL section_vals_val_get(bs_sec, "DOS%BROADENING", r_val=bs_env%broadening_DOS)
     321              : 
     322          158 :       NULLIFY (ldos_sec)
     323          158 :       ldos_sec => section_vals_get_subs_vals(bs_sec, "DOS%LDOS")
     324          158 :       CALL section_vals_get(ldos_sec, explicit=bs_env%do_ldos)
     325              : 
     326          158 :       CALL section_vals_val_get(ldos_sec, "INTEGRATION", i_val=bs_env%int_ldos_xyz)
     327          158 :       CALL section_vals_val_get(ldos_sec, "BIN_MESH", i_vals=bs_env%bin_mesh)
     328              : 
     329          158 :       NULLIFY (kp_bs_sec)
     330          158 :       kp_bs_sec => section_vals_get_subs_vals(bs_sec, "BANDSTRUCTURE_PATH")
     331          158 :       CALL section_vals_val_get(kp_bs_sec, "NPOINTS", i_val=bs_env%input_kp_bs_npoints)
     332          158 :       CALL section_vals_val_get(kp_bs_sec, "UNITS", c_val=ustr)
     333          158 :       CALL uppercase(ustr)
     334          158 :       CALL section_vals_val_get(kp_bs_sec, "SPECIAL_POINT", n_rep_val=bs_env%input_kp_bs_n_sp_pts)
     335              : 
     336          158 :       NULLIFY (floquet_sec)
     337          158 :       floquet_sec => section_vals_get_subs_vals(bs_sec, "FLOQUET")
     338          158 :       CALL section_vals_get(floquet_sec, explicit=bs_env%do_floquet)
     339          158 :       CALL section_vals_val_get(floquet_sec, "AMPLITUDE", r_val=bs_env%floquet_amplitude)
     340          158 :       CALL section_vals_val_get(floquet_sec, "FREQUENCY", r_val=bs_env%floquet_omega)
     341          158 :       CALL section_vals_val_get(floquet_sec, "POLARISATION", r_vals=bs_env%floquet_polarisation)
     342          158 :       CALL section_vals_val_get(floquet_sec, "PHASE_OFFSETS", r_vals=bs_env%floquet_phi)
     343          158 :       CALL section_vals_val_get(floquet_sec, "MAX_FLOQUET_INDEX", i_val=bs_env%max_floquet_index)
     344          158 :       CALL section_vals_val_get(floquet_sec, "MAX_NMO", i_val=bs_env%max_nmo_floquet)
     345          158 :       CALL section_vals_val_get(floquet_sec, "EPS_FLOQUET", r_val=bs_env%eps_floquet)
     346          158 :       CALL section_vals_val_get(floquet_sec, "ENERGY_WINDOW", r_val=bs_env%energy_window_floquet)
     347          158 :       CALL section_vals_val_get(floquet_sec, "ENERGY_STEP", r_val=bs_env%energy_step_floquet)
     348          158 :       CALL section_vals_val_get(floquet_sec, "BROADENING", r_val=bs_env%broadening_floquet)
     349          158 :       CALL section_vals_val_get(floquet_sec, "MEM_FILL_FRACTION", r_val=bs_env%floquet_mem_fill_fraction)
     350          158 :       CALL section_vals_val_get(floquet_sec, "TEMPERATURE", r_val=bs_env%floquet_temperature)
     351          158 :       CALL section_vals_val_get(floquet_sec, "FLOQUET_DOS_FILE_NAME", c_val=bs_env%floquet_dos_file)
     352          158 :       CALL section_vals_val_get(floquet_sec, "QUASI_ENERGIES_FILE_NAME", c_val=bs_env%floquet_qe_file)
     353          158 :       CALL section_vals_val_get(floquet_sec, "FLOQUET_BS_FILE_NAME", c_val=bs_env%floquet_bs_file)
     354              : 
     355              :       ! read special points for band structure
     356          334 :       ALLOCATE (bs_env%xkp_special(3, bs_env%input_kp_bs_n_sp_pts))
     357          196 :       DO ikp = 1, bs_env%input_kp_bs_n_sp_pts
     358           38 :          CALL section_vals_val_get(kp_bs_sec, "SPECIAL_POINT", i_rep_val=ikp, c_vals=string_ptr)
     359           38 :          CPASSERT(SIZE(string_ptr(:), 1) == 4)
     360          152 :          DO i = 1, 3
     361          114 :             CALL read_float_object(string_ptr(i + 1), kpptr(i), error_msg)
     362          152 :             IF (LEN_TRIM(error_msg) > 0) CPABORT(TRIM(error_msg))
     363              :          END DO
     364          158 :          SELECT CASE (ustr)
     365              :          CASE ("B_VECTOR")
     366          152 :             bs_env%xkp_special(1:3, ikp) = kpptr(1:3)
     367              :          CASE ("CART_ANGSTROM")
     368              :             bs_env%xkp_special(1:3, ikp) = (kpptr(1)*cart_hmat(1, 1:3) + &
     369              :                                             kpptr(2)*cart_hmat(2, 1:3) + &
     370            0 :                                             kpptr(3)*cart_hmat(3, 1:3))/twopi*angstrom
     371              :          CASE ("CART_BOHR")
     372              :             bs_env%xkp_special(1:3, ikp) = (kpptr(1)*cart_hmat(1, 1:3) + &
     373              :                                             kpptr(2)*cart_hmat(2, 1:3) + &
     374            0 :                                             kpptr(3)*cart_hmat(3, 1:3))/twopi
     375              :          CASE DEFAULT
     376           38 :             CPABORT("Unknown unit <"//TRIM(ustr)//"> specified for k-point definition")
     377              :          END SELECT
     378              :       END DO
     379              : 
     380          158 :       CALL timestop(handle)
     381              : 
     382          316 :    END SUBROUTINE read_bandstructure_input_parameters
     383              : 
     384              : ! **************************************************************************************************
     385              : !> \brief ...
     386              : !> \param bs_env ...
     387              : ! **************************************************************************************************
     388          158 :    SUBROUTINE print_header(bs_env)
     389              : 
     390              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     391              : 
     392              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'print_header'
     393              : 
     394              :       INTEGER                                            :: handle, u
     395              : 
     396          158 :       CALL timeset(routineN, handle)
     397              : 
     398          158 :       bs_env%unit_nr = cp_logger_get_default_io_unit()
     399              : 
     400          158 :       u = bs_env%unit_nr
     401              : 
     402          158 :       IF (u > 0) THEN
     403           79 :          WRITE (u, '(T2,A)') ' '
     404           79 :          WRITE (u, '(T2,A)') REPEAT('-', 79)
     405           79 :          WRITE (u, '(T2,A,A78)') '-', '-'
     406           79 :          WRITE (u, '(T2,A,A51,A27)') '-', 'BANDSTRUCTURE CALCULATION', '-'
     407           79 :          WRITE (u, '(T2,A,A78)') '-', '-'
     408           79 :          WRITE (u, '(T2,A)') REPEAT('-', 79)
     409           79 :          WRITE (u, '(T2,A)') ' '
     410              :       END IF
     411              : 
     412          158 :       CALL timestop(handle)
     413              : 
     414          158 :    END SUBROUTINE print_header
     415              : 
     416              : ! **************************************************************************************************
     417              : !> \brief ...
     418              : !> \param qs_env ...
     419              : !> \param bs_env ...
     420              : !> \param kpoints ...
     421              : ! **************************************************************************************************
     422          126 :    SUBROUTINE setup_kpoints_DOS_large_cell_Gamma(qs_env, bs_env, kpoints)
     423              : 
     424              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     425              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     426              :       TYPE(kpoint_type), POINTER                         :: kpoints
     427              : 
     428              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_DOS_large_cell_Gamma'
     429              : 
     430              :       INTEGER                                            :: handle, i_dim, i_kp_in_line, &
     431              :                                                             i_special_kp, ikk, n_kp_in_line, &
     432              :                                                             n_special_kp, nkp, nkp_only_bs, &
     433              :                                                             nkp_only_DOS, u
     434              :       INTEGER, DIMENSION(3)                              :: nkp_grid, periodic
     435              : 
     436          126 :       CALL timeset(routineN, handle)
     437              : 
     438              :       ! routine adapted from mp2_integrals.F
     439          126 :       NULLIFY (kpoints)
     440          126 :       CALL kpoint_create(kpoints)
     441              : 
     442          126 :       kpoints%kp_scheme = "GENERAL"
     443              : 
     444          126 :       n_special_kp = bs_env%input_kp_bs_n_sp_pts
     445          126 :       n_kp_in_line = bs_env%input_kp_bs_npoints
     446              : 
     447          504 :       periodic(1:3) = bs_env%periodic(1:3)
     448              : 
     449          504 :       DO i_dim = 1, 3
     450              : 
     451          378 :          CPASSERT(periodic(i_dim) == 0 .OR. periodic(i_dim) == 1)
     452              : 
     453          504 :          IF (bs_env%nkp_grid_DOS_input(i_dim) < 0) THEN
     454          336 :             IF (periodic(i_dim) == 1) nkp_grid(i_dim) = 2
     455          336 :             IF (periodic(i_dim) == 0) nkp_grid(i_dim) = 1
     456              :          ELSE
     457           42 :             nkp_grid(i_dim) = bs_env%nkp_grid_DOS_input(i_dim)
     458              :          END IF
     459              : 
     460              :       END DO
     461              : 
     462              :       ! use the k <-> -k symmetry to reduce the number of kpoints
     463          126 :       IF (nkp_grid(1) > 1) THEN
     464            8 :          nkp_only_DOS = (nkp_grid(1) + 1)/2*nkp_grid(2)*nkp_grid(3)
     465          118 :       ELSE IF (nkp_grid(2) > 1) THEN
     466            8 :          nkp_only_DOS = nkp_grid(1)*(nkp_grid(2) + 1)/2*nkp_grid(3)
     467          110 :       ELSE IF (nkp_grid(3) > 1) THEN
     468            2 :          nkp_only_DOS = nkp_grid(1)*nkp_grid(2)*(nkp_grid(3) + 1)/2
     469              :       ELSE
     470          108 :          nkp_only_DOS = 1
     471              :       END IF
     472              : 
     473              :       ! we will compute the GW QP levels for all k's in the bandstructure path but also
     474              :       ! for all k-points from the SCF (e.g. for DOS or for self-consistent GW)
     475          126 :       IF (n_special_kp > 0) THEN
     476            0 :          nkp_only_bs = n_kp_in_line*(n_special_kp - 1) + 1
     477              :       ELSE
     478              :          nkp_only_bs = 0
     479              :       END IF
     480              : 
     481          126 :       nkp = nkp_only_DOS + nkp_only_bs
     482              : 
     483          504 :       kpoints%nkp_grid(1:3) = nkp_grid(1:3)
     484          126 :       kpoints%nkp = nkp
     485              : 
     486          126 :       bs_env%nkp_bs_and_DOS = nkp
     487          126 :       bs_env%nkp_only_bs = nkp_only_bs
     488          126 :       bs_env%nkp_only_DOS = nkp_only_DOS
     489              : 
     490          630 :       ALLOCATE (kpoints%xkp(3, nkp), kpoints%wkp(nkp))
     491          272 :       kpoints%wkp(:) = 0.0_dp
     492          272 :       kpoints%wkp(1:nkp_only_DOS) = 1.0_dp/REAL(nkp_only_DOS, KIND=dp)
     493              : 
     494          126 :       CALL compute_xkp(kpoints%xkp, 1, nkp_only_DOS, nkp_grid)
     495              : 
     496          126 :       IF (n_special_kp > 0) THEN
     497            0 :          kpoints%xkp(1:3, nkp_only_DOS + 1) = bs_env%xkp_special(1:3, 1)
     498            0 :          ikk = nkp_only_DOS + 1
     499            0 :          DO i_special_kp = 2, n_special_kp
     500            0 :             DO i_kp_in_line = 1, n_kp_in_line
     501            0 :                ikk = ikk + 1
     502              :                kpoints%xkp(1:3, ikk) = bs_env%xkp_special(1:3, i_special_kp - 1) + &
     503              :                                        REAL(i_kp_in_line, KIND=dp)/REAL(n_kp_in_line, KIND=dp)* &
     504              :                                        (bs_env%xkp_special(1:3, i_special_kp) - &
     505            0 :                                         bs_env%xkp_special(1:3, i_special_kp - 1))
     506            0 :                kpoints%wkp(ikk) = 0.0_dp
     507              :             END DO
     508              :          END DO
     509              :       END IF
     510              : 
     511          126 :       CALL kpoint_init_cell_index_simple(kpoints, qs_env)
     512              : 
     513          126 :       u = bs_env%unit_nr
     514              : 
     515          126 :       IF (u > 0 .AND. bs_env%do_periodic) THEN
     516            9 :          IF (nkp_only_bs > 0) THEN
     517              :             WRITE (u, FMT="(T2,1A,T77,I4)") &
     518            0 :                "Number of special k-points for the bandstructure", n_special_kp
     519            0 :             WRITE (u, FMT="(T2,1A,T77,I4)") "Number of k-points for the bandstructure", nkp
     520              :             WRITE (u, FMT="(T2,1A,T69,3I4)") &
     521            0 :                "K-point mesh for the density of states (DOS)", nkp_grid(1:3)
     522              :          ELSE
     523              :             WRITE (u, FMT="(T2,1A,T69,3I4)") &
     524            9 :                "K-point mesh for the density of states (DOS) and the self-energy", nkp_grid(1:3)
     525              :          END IF
     526              :       END IF
     527              : 
     528          126 :       CALL timestop(handle)
     529              : 
     530          126 :    END SUBROUTINE setup_kpoints_DOS_large_cell_Gamma
     531              : 
     532              : ! **************************************************************************************************
     533              : !> \brief ...
     534              : !> \param qs_env ...
     535              : !> \param bs_env ...
     536              : !> \param kpoints ...
     537              : !> \param do_print ...
     538              : ! **************************************************************************************************
     539           32 :    SUBROUTINE setup_kpoints_scf_desymm(qs_env, bs_env, kpoints, do_print)
     540              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     541              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     542              :       TYPE(kpoint_type), POINTER                         :: kpoints
     543              : 
     544              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_scf_desymm'
     545              : 
     546              :       INTEGER                                            :: handle, i_cell_x, i_dim, img, j_cell_y, &
     547              :                                                             k_cell_z, nimages, nkp, u
     548              :       INTEGER, DIMENSION(3)                              :: cell_grid, cixd, nkp_grid
     549              :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
     550              : 
     551              :       LOGICAL:: do_print
     552              : 
     553           32 :       CALL timeset(routineN, handle)
     554              : 
     555           32 :       NULLIFY (kpoints)
     556           32 :       CALL kpoint_create(kpoints)
     557              : 
     558           32 :       CALL get_qs_env(qs_env=qs_env, kpoints=kpoints_scf)
     559           32 :       kpoints%rskp_transform = kpoints_scf%rskp_transform
     560              : 
     561          128 :       nkp_grid(1:3) = kpoints_scf%nkp_grid(1:3)
     562           32 :       nkp = nkp_grid(1)*nkp_grid(2)*nkp_grid(3)
     563              : 
     564          128 :       DO i_dim = 1, 3
     565          128 :          IF (bs_env%periodic(i_dim) == 1) THEN
     566           72 :             IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
     567           36 :                CPASSERT(nkp_grid(i_dim) >= 1)
     568              :             ELSE
     569           36 :                CPASSERT(nkp_grid(i_dim) >= 4)
     570              :             END IF
     571              :          END IF
     572              :       END DO
     573              : 
     574           32 :       kpoints%kp_scheme = "GENERAL"
     575          128 :       kpoints%nkp_grid(1:3) = nkp_grid(1:3)
     576           32 :       kpoints%nkp = nkp
     577           32 :       bs_env%nkp_scf_desymm = nkp
     578              : 
     579           96 :       ALLOCATE (kpoints%xkp(1:3, nkp))
     580           32 :       CALL compute_xkp(kpoints%xkp, 1, nkp, nkp_grid)
     581           32 :       IF (bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
     582              :          ! Preserve the actual SCF mesh, including MacDonald shifts on odd grids.
     583          112 :          kpoints%kp_shift = kpoints_scf%kp_shift
     584           56 :          DO i_dim = 1, 3
     585           42 :             IF (kpoints_scf%gamma_centered .AND. MODULO(nkp_grid(i_dim), 2) == 0) THEN
     586            0 :                kpoints%kp_shift(i_dim) = kpoints%kp_shift(i_dim) + 0.5_dp/REAL(nkp_grid(i_dim), dp)
     587              :             END IF
     588          800 :             kpoints%xkp(i_dim, :) = kpoints%xkp(i_dim, :) + kpoints%kp_shift(i_dim)
     589              :          END DO
     590              :       END IF
     591              : 
     592           96 :       ALLOCATE (kpoints%wkp(nkp))
     593          568 :       kpoints%wkp(:) = 1.0_dp/REAL(nkp, KIND=dp)
     594              : 
     595              :       ! for example 4x3x6 kpoint grid -> 3x3x5 cell grid because we need the same number of
     596              :       ! neighbor cells on both sides of the unit cell
     597          128 :       cell_grid(1:3) = nkp_grid(1:3) - MODULO(nkp_grid(1:3) + 1, 2)
     598              : 
     599              :       ! cell index: for example for x: from -n_x/2 to +n_x/2, n_x: number of cells in x direction
     600          128 :       cixd(1:3) = cell_grid(1:3)/2
     601              : 
     602           32 :       nimages = cell_grid(1)*cell_grid(2)*cell_grid(3)
     603              : 
     604           32 :       bs_env%nimages_scf_desymm = nimages
     605          128 :       bs_env%cell_grid_scf_desymm(1:3) = cell_grid(1:3)
     606              : 
     607           32 :       IF (ASSOCIATED(kpoints%index_to_cell)) DEALLOCATE (kpoints%index_to_cell)
     608           32 :       IF (ASSOCIATED(kpoints%cell_to_index)) DEALLOCATE (kpoints%cell_to_index)
     609              : 
     610          160 :       ALLOCATE (kpoints%cell_to_index(-cixd(1):cixd(1), -cixd(2):cixd(2), -cixd(3):cixd(3)))
     611           96 :       ALLOCATE (kpoints%index_to_cell(3, nimages))
     612              : 
     613           32 :       img = 0
     614           84 :       DO i_cell_x = -cixd(1), cixd(1)
     615          224 :          DO j_cell_y = -cixd(2), cixd(2)
     616          488 :             DO k_cell_z = -cixd(3), cixd(3)
     617          296 :                img = img + 1
     618          296 :                kpoints%cell_to_index(i_cell_x, j_cell_y, k_cell_z) = img
     619         1324 :                kpoints%index_to_cell(1:3, img) = [i_cell_x, j_cell_y, k_cell_z]
     620              :             END DO
     621              :          END DO
     622              :       END DO
     623              : 
     624           32 :       u = bs_env%unit_nr
     625           32 :       IF (u > 0 .AND. do_print) THEN
     626           16 :          WRITE (u, FMT="(T2,A,I49)") "Number of cells for G, χ, W, Σ", nimages
     627              :       END IF
     628              : 
     629           32 :       CALL timestop(handle)
     630              : 
     631           32 :    END SUBROUTINE setup_kpoints_scf_desymm
     632              : 
     633              : ! **************************************************************************************************
     634              : !> \brief ...
     635              : !> \param bs_env ...
     636              : !> \param kpoints ...
     637              : ! **************************************************************************************************
     638           32 :    SUBROUTINE setup_kpoints_DOS_small_cell_full_kp(bs_env, kpoints)
     639              : 
     640              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     641              :       TYPE(kpoint_type), POINTER                         :: kpoints
     642              : 
     643              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_DOS_small_cell_full_kp'
     644              : 
     645              :       INTEGER                                            :: handle, i_kp_in_line, i_special_kp, ikk, &
     646              :                                                             n_kp_in_line, n_special_kp, nkp, &
     647              :                                                             nkp_only_bs, nkp_scf_desymm, u
     648              : 
     649           32 :       CALL timeset(routineN, handle)
     650              : 
     651              :       ! routine adapted from mp2_integrals.F
     652           32 :       NULLIFY (kpoints)
     653           32 :       CALL kpoint_create(kpoints)
     654              : 
     655           32 :       n_special_kp = bs_env%input_kp_bs_n_sp_pts
     656           32 :       n_kp_in_line = bs_env%input_kp_bs_npoints
     657           32 :       nkp_scf_desymm = bs_env%nkp_scf_desymm
     658              : 
     659           32 :       IF (n_special_kp > 0) THEN
     660           18 :          nkp_only_bs = n_kp_in_line*(n_special_kp - 1) + 1
     661              :       ELSE
     662           14 :          nkp_only_bs = 0
     663              :       END IF
     664           32 :       bs_env%nkp_only_DOS = nkp_scf_desymm
     665           32 :       nkp = nkp_only_bs + bs_env%nkp_only_DOS
     666              : 
     667           96 :       ALLOCATE (kpoints%xkp(3, nkp))
     668           96 :       ALLOCATE (kpoints%wkp(nkp))
     669              : 
     670           32 :       kpoints%nkp = nkp
     671              : 
     672           32 :       bs_env%nkp_bs_and_DOS = nkp
     673           32 :       bs_env%nkp_only_bs = nkp_only_bs
     674              : 
     675         4352 :       kpoints%xkp(1:3, 1:nkp_scf_desymm) = bs_env%kpoints_scf_desymm%xkp(1:3, 1:nkp_scf_desymm)
     676          568 :       kpoints%wkp(1:nkp_scf_desymm) = 1.0_dp/REAL(nkp_scf_desymm, KIND=dp)
     677              : 
     678           32 :       IF (n_special_kp > 0) THEN
     679          144 :          kpoints%xkp(1:3, bs_env%nkp_only_DOS + 1) = bs_env%xkp_special(1:3, 1)
     680           18 :          kpoints%wkp(bs_env%nkp_only_DOS + 1) = 0.0_dp
     681           18 :          ikk = bs_env%nkp_only_DOS + 1
     682           38 :          DO i_special_kp = 2, n_special_kp
     683          126 :             DO i_kp_in_line = 1, n_kp_in_line
     684           88 :                ikk = ikk + 1
     685              :                kpoints%xkp(1:3, ikk) = bs_env%xkp_special(1:3, i_special_kp - 1) + &
     686              :                                        REAL(i_kp_in_line, KIND=dp)/REAL(n_kp_in_line, KIND=dp)* &
     687              :                                        (bs_env%xkp_special(1:3, i_special_kp) - &
     688          704 :                                         bs_env%xkp_special(1:3, i_special_kp - 1))
     689          108 :                kpoints%wkp(ikk) = 0.0_dp
     690              :             END DO
     691              :          END DO
     692              :       END IF
     693              : 
     694           32 :       IF (ASSOCIATED(kpoints%index_to_cell)) DEALLOCATE (kpoints%index_to_cell)
     695              : 
     696           96 :       ALLOCATE (kpoints%index_to_cell(3, bs_env%nimages_scf_desymm))
     697         2432 :       kpoints%index_to_cell(:, :) = bs_env%kpoints_scf_desymm%index_to_cell(:, :)
     698              : 
     699           32 :       u = bs_env%unit_nr
     700              : 
     701           32 :       IF (u > 0) THEN
     702           16 :          WRITE (u, FMT="(T2,1A,T72,I9)") "Number of special k-points for the bandstructure", &
     703           32 :             n_special_kp
     704           16 :          WRITE (u, FMT="(T2,1A,T72,I9)") "Number of k-points on the bandstructure path", nkp_only_bs
     705              :          WRITE (u, FMT="(T2,1A,T72,I9)") &
     706           16 :             "SCF-mesh k-points included as additional targets", bs_env%nkp_only_DOS
     707              :       END IF
     708              : 
     709           32 :       CALL timestop(handle)
     710              : 
     711           32 :    END SUBROUTINE setup_kpoints_DOS_small_cell_full_kp
     712              : 
     713              : ! **************************************************************************************************
     714              : !> \brief ...
     715              : !> \param qs_env ...
     716              : !> \param bs_env ...
     717              : ! **************************************************************************************************
     718           32 :    SUBROUTINE compute_cfm_mo_coeff_kp_and_eigenval_scf_kp(qs_env, bs_env)
     719              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     720              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     721              : 
     722              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_cfm_mo_coeff_kp_and_eigenval_scf_kp'
     723              : 
     724              :       INTEGER                                            :: handle, ikp, ikp_first, ikp_last, ispin, &
     725              :                                                             my_ikp, nkp_bs_and_DOS, nmo_retained
     726              :       REAL(KIND=dp)                                      :: CBM, VBM
     727           32 :       REAL(KIND=dp), ALLOCATABLE                         :: eigenval_local(:, :)
     728              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_local
     729           32 :       TYPE(cp_cfm_diag_workspace_type)                   :: solver
     730              :       TYPE(cp_cfm_type)                                  :: cfm_kp_group, cfm_ks, cfm_none, cfm_s, &
     731              :                                                             cfm_s_local, cfm_s_work, cfm_u_work
     732           32 :       TYPE(cp_cfm_type), ALLOCATABLE                     :: cfm_ks_local(:), cfm_mos_local(:)
     733              :       TYPE(cp_fm_struct_type), POINTER                   :: struct_local
     734           32 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks, matrix_s
     735              :       TYPE(kpoint_env_type), POINTER                     :: lowdin_kp
     736           32 :       TYPE(kpoint_operator_context_type)                 :: operators
     737              :       TYPE(kpoint_type), POINTER                         :: kpoints_bs, kpoints_scf
     738              :       TYPE(mp_para_env_type), POINTER                    :: para_env, para_env_local
     739              : 
     740           32 :       CALL timeset(routineN, handle)
     741              : 
     742              :       CALL get_qs_env(qs_env, &
     743              :                       para_env=para_env, &
     744              :                       matrix_ks_kp=matrix_ks, &
     745              :                       matrix_s_kp=matrix_s, &
     746           32 :                       kpoints=kpoints_scf)
     747              : 
     748           32 :       NULLIFY (lowdin_kp)
     749           32 :       IF (ALLOCATED(kpoints_scf%lowdin_v)) THEN
     750            0 :          IF (ANY(kpoints_scf%lowdin_v /= 0.0_dp)) ALLOCATE (lowdin_kp)
     751              :       END IF
     752              : 
     753           32 :       CALL create_operator_kpoints(qs_env, bs_env, kpoints_bs, cfm_kp_group)
     754           32 :       CALL kpoint_operator_context_create(operators, kpoints_bs, bs_env%fm_work_mo(1), bs_env%n_spin)
     755           32 :       CALL kpoint_operator_prepare(operators, matrix_s, kpoint_slot_s, matrix_row=1)
     756           32 :       CALL kpoint_operator_prepare(operators, matrix_ks, kpoint_slot_ks)
     757              : 
     758           32 :       CALL cp_cfm_create(cfm_ks, bs_env%cfm_work_mo%matrix_struct)
     759           32 :       CALL cp_cfm_create(cfm_s, bs_env%cfm_work_mo%matrix_struct)
     760              : 
     761              :       ! nkp_bs_and_DOS contains desymmetrized k-point mesh from SCF and k-points from GW bandstructure
     762           32 :       nkp_bs_and_DOS = bs_env%nkp_bs_and_DOS
     763              : 
     764          802 :       ALLOCATE (bs_env%cfm_mo_coeff_kp(nkp_bs_and_DOS, bs_env%n_spin))
     765          802 :       ALLOCATE (bs_env%cfm_ks_kp(nkp_bs_and_DOS, bs_env%n_spin))
     766          738 :       ALLOCATE (bs_env%cfm_s_kp(nkp_bs_and_DOS))
     767          674 :       DO ikp = 1, nkp_bs_and_DOS
     768         1284 :       DO ispin = 1, bs_env%n_spin
     769          642 :          CALL cp_cfm_create(bs_env%cfm_mo_coeff_kp(ikp, ispin), bs_env%cfm_work_mo%matrix_struct)
     770         1284 :          CALL cp_cfm_create(bs_env%cfm_ks_kp(ikp, ispin), bs_env%cfm_work_mo%matrix_struct)
     771              :       END DO
     772          674 :       CALL cp_cfm_create(bs_env%cfm_s_kp(ikp), bs_env%cfm_work_mo%matrix_struct)
     773              :       END DO
     774              : 
     775              :       ! H(k) and S(k) have only n_ao rows, far too few for ScaLAPACK across all ranks. In each
     776              :       ! batch of num_pe k points, rank MODULO(ikp - 1, num_pe) solves k with LAPACK.
     777           32 :       ALLOCATE (para_env_local)
     778           32 :       CALL para_env_local%from_split(para_env, para_env%mepos)
     779           32 :       CALL cp_blacs_env_create(blacs_local, para_env_local)
     780              :       CALL cp_fm_struct_create(struct_local, context=blacs_local, nrow_global=bs_env%n_ao, &
     781           32 :                                ncol_global=bs_env%n_ao)
     782           32 :       CALL cp_cfm_create(cfm_s_local, struct_local)
     783           32 :       CALL cp_cfm_create(cfm_s_work, struct_local)
     784           32 :       CALL cp_cfm_create(cfm_u_work, struct_local)
     785          224 :       ALLOCATE (cfm_ks_local(bs_env%n_spin), cfm_mos_local(bs_env%n_spin))
     786          128 :       ALLOCATE (eigenval_local(bs_env%n_ao, bs_env%n_spin))
     787           64 :       DO ispin = 1, bs_env%n_spin
     788           32 :          CALL cp_cfm_create(cfm_ks_local(ispin), struct_local)
     789           64 :          CALL cp_cfm_create(cfm_mos_local(ispin), struct_local)
     790              :       END DO
     791         6492 :       bs_env%eigenval_scf(:, :, :) = 0.0_dp
     792           32 :       nmo_retained = bs_env%n_ao
     793              : 
     794          360 :       DO ikp_first = 1, nkp_bs_and_DOS, para_env%num_pe
     795          328 :          ikp_last = MIN(nkp_bs_and_DOS, ikp_first + para_env%num_pe - 1)
     796          328 :          my_ikp = ikp_first + para_env%mepos
     797          970 :          DO ikp = ikp_first, ikp_last
     798              : 
     799              :             ! S(k) and its Lowdin projectors are shared by both spin channels.
     800          642 :             CALL kpoint_operator_get(operators, ikp, 1, matrix_s, cmat=cfm_kp_group, matrix_row=1, slot=kpoint_slot_s)
     801          642 :             CALL cp_cfm_copy_general(cfm_kp_group, cfm_s, para_env)
     802          642 :             CALL cp_cfm_to_cfm(cfm_s, bs_env%cfm_s_kp(ikp))
     803          642 :             CALL copy_to_k_owner(cfm_s, cfm_s_local, ikp == my_ikp, para_env)
     804          642 :             IF (ASSOCIATED(lowdin_kp)) THEN
     805            0 :                CALL lowdin_kp_prepare(lowdin_kp, kpoints_scf%lowdin_orbitals, .FALSE., cfm_s)
     806              :             END IF
     807         1612 :             DO ispin = 1, bs_env%n_spin
     808              :                ! h^KS^R -> h^KS(k)
     809              :                CALL kpoint_operator_get(operators, ikp, ispin, matrix_ks, cmat=cfm_kp_group, &
     810          642 :                                         matrix_row=ispin, slot=kpoint_slot_ks)
     811          642 :                CALL cp_cfm_copy_general(cfm_kp_group, cfm_ks, para_env)
     812          642 :                IF (ASSOCIATED(lowdin_kp)) THEN
     813            0 :                   CALL lowdin_kp_apply(lowdin_kp, kpoints_scf%lowdin_v(:, ispin), cmat=cfm_ks)
     814              :                END IF
     815          642 :                CALL cp_cfm_to_cfm(cfm_ks, bs_env%cfm_ks_kp(ikp, ispin))
     816         1284 :                CALL copy_to_k_owner(cfm_ks, cfm_ks_local(ispin), ikp == my_ikp, para_env)
     817              :             END DO
     818              :          END DO
     819              : 
     820          328 :          IF (my_ikp <= ikp_last) THEN
     821          642 :             DO ispin = 1, bs_env%n_spin
     822              :                ! Diagonalize KS-matrix via Rothaan-Hall equation:
     823              :                ! H^KS(k) C(k) = S(k) C(k) ε(k); the solver overwrites S(k).
     824          321 :                CALL cp_cfm_to_cfm(cfm_s_local, cfm_s_work)
     825              :                CALL cp_cfm_geeig_canon_local(cfm_ks_local(ispin), cfm_s_work, cfm_mos_local(ispin), &
     826              :                                              eigenval_local(:, ispin), cfm_u_work, &
     827          321 :                                              bs_env%eps_eigval_mat_s, solver)
     828         3214 :                bs_env%eigenval_scf(:, my_ikp, ispin) = eigenval_local(:, ispin)
     829              :                ! Canonical orthogonalisation parks the removed overlap modes at a placeholder energy.
     830         3535 :                nmo_retained = MIN(nmo_retained, COUNT(eigenval_local(:, ispin) < set_removed_eigval_to))
     831              :             END DO
     832              :          END IF
     833              : 
     834         1002 :          DO ikp = ikp_first, ikp_last
     835         1612 :             DO ispin = 1, bs_env%n_spin
     836         1284 :                IF (ikp == my_ikp) THEN
     837          321 :                   CALL cp_cfm_copy_general(cfm_mos_local(ispin), bs_env%cfm_mo_coeff_kp(ikp, ispin), para_env)
     838              :                ELSE
     839          321 :                   CALL cp_cfm_copy_general(cfm_none, bs_env%cfm_mo_coeff_kp(ikp, ispin), para_env)
     840              :                END IF
     841              :             END DO
     842              :          END DO
     843              :       END DO
     844           32 :       CALL para_env%sum(bs_env%eigenval_scf)
     845           32 :       CALL para_env%min(nmo_retained)
     846           32 :       bs_env%n_mo_retained = MIN(bs_env%n_mo_retained, nmo_retained)
     847              : 
     848           64 :       DO ispin = 1, bs_env%n_spin
     849           32 :          CALL cp_cfm_release(cfm_ks_local(ispin))
     850           64 :          CALL cp_cfm_release(cfm_mos_local(ispin))
     851              :       END DO
     852           32 :       CALL cp_cfm_release(cfm_s_local)
     853           32 :       CALL cp_cfm_release(cfm_s_work)
     854           32 :       CALL cp_cfm_release(cfm_u_work)
     855           32 :       CALL cp_fm_struct_release(struct_local)
     856           32 :       CALL cp_blacs_env_release(blacs_local)
     857           32 :       CALL mp_para_env_release(para_env_local)
     858              : 
     859           64 :       DO ispin = 1, bs_env%n_spin
     860          674 :          VBM = MAXVAL(bs_env%eigenval_scf(bs_env%n_occ(ispin), :, ispin))
     861          674 :          CBM = MINVAL(bs_env%eigenval_scf(bs_env%n_occ(ispin) + 1, :, ispin))
     862              : 
     863           64 :          bs_env%e_fermi(ispin) = 0.5_dp*(VBM + CBM)
     864              : 
     865              :       END DO
     866              : 
     867           32 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_scf, bs_env%eigenval_scf, bs_env)
     868              : 
     869           32 :       CALL kpoint_operator_context_release(operators)
     870           32 :       CALL cp_cfm_release(cfm_kp_group)
     871           32 :       CALL kpoint_release(kpoints_bs)
     872           32 :       CALL cp_cfm_release(cfm_ks)
     873           32 :       CALL cp_cfm_release(cfm_s)
     874           32 :       IF (ASSOCIATED(lowdin_kp)) THEN
     875            0 :          CALL cp_cfm_release(lowdin_kp%lowdin_cb)
     876            0 :          CALL cp_cfm_release(lowdin_kp%lowdin_cwork)
     877            0 :          CALL lowdin_kp%lowdin_gemm%destroy()
     878            0 :          DEALLOCATE (lowdin_kp)
     879              :       END IF
     880              : 
     881           32 :       CALL timestop(handle)
     882              : 
     883          160 :    END SUBROUTINE compute_cfm_mo_coeff_kp_and_eigenval_scf_kp
     884              : 
     885              : ! **************************************************************************************************
     886              : !> \brief Copy a distributed matrix of one k point into the one-rank matrix of the rank solving it.
     887              : !> \param cfm_global matrix distributed over all ranks
     888              : !> \param cfm_local one-rank matrix of this rank
     889              : !> \param is_owner whether this rank solves the k point
     890              : !> \param para_env ...
     891              : ! **************************************************************************************************
     892         1284 :    SUBROUTINE copy_to_k_owner(cfm_global, cfm_local, is_owner, para_env)
     893              :       TYPE(cp_cfm_type), INTENT(IN)                      :: cfm_global, cfm_local
     894              :       LOGICAL, INTENT(IN)                                :: is_owner
     895              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     896              : 
     897              :       TYPE(cp_cfm_type)                                  :: cfm_none
     898              : 
     899         1284 :       IF (is_owner) THEN
     900          642 :          CALL cp_cfm_copy_general(cfm_global, cfm_local, para_env)
     901              :       ELSE
     902          642 :          CALL cp_cfm_copy_general(cfm_global, cfm_none, para_env)
     903              :       END IF
     904         1284 :    END SUBROUTINE copy_to_k_owner
     905              : 
     906              : ! **************************************************************************************************
     907              : !> \brief Set up the bandstructure and DOS k-points as one k-point group spanning all ranks, as
     908              : !>        calculate_kp_orbitals does, so that the k-point operators evaluate H(k) and S(k) there
     909              : !> \param qs_env ...
     910              : !> \param bs_env ...
     911              : !> \param kpoints k-points of bs_env%kpoints_DOS, to be released by the caller
     912              : !> \param cfm_kp_group work matrix on the layout of the k-point group
     913              : ! **************************************************************************************************
     914           62 :    SUBROUTINE create_operator_kpoints(qs_env, bs_env, kpoints, cfm_kp_group)
     915              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     916              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     917              :       TYPE(kpoint_type), POINTER                         :: kpoints
     918              :       TYPE(cp_cfm_type), INTENT(OUT)                     :: cfm_kp_group
     919              : 
     920              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     921              :       TYPE(cp_fm_pool_type), POINTER                     :: ao_ao_fm_pool
     922              :       TYPE(dft_control_type), POINTER                    :: dft_control
     923           62 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     924              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     925              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     926           62 :          POINTER                                         :: sab_kp
     927              : 
     928              :       CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env, mos=mos, &
     929           62 :                       sab_kp=sab_kp, dft_control=dft_control)
     930              : 
     931           62 :       NULLIFY (kpoints)
     932           62 :       CALL calculate_kpoints_for_bs(kpoints, "GENERAL", group_size_ext=0, kpgeneral=bs_env%kpoints_DOS%xkp)
     933           62 :       CALL kpoint_env_initialize(kpoints, para_env, blacs_env)
     934           62 :       CALL kpoint_initialize_mos(kpoints, mos)
     935           62 :       CALL kpoint_init_cell_index(kpoints, sab_kp, para_env, dft_control%nimages)
     936              : 
     937           62 :       CALL mpools_get(kpoints%mpools, ao_ao_fm_pool=ao_ao_fm_pool)
     938           62 :       CALL cp_cfm_create(cfm_kp_group, fm_pool_get_el_struct(ao_ao_fm_pool))
     939              : 
     940           62 :    END SUBROUTINE create_operator_kpoints
     941              : 
     942              : ! **************************************************************************************************
     943              : !> \brief ...
     944              : !> \param mat_rs ...
     945              : !> \param ispin ...
     946              : !> \param xkp ...
     947              : !> \param cell_to_index_scf ...
     948              : !> \param sab_nl ...
     949              : !> \param bs_env ...
     950              : !> \param cfm_kp ...
     951              : !> \param imag_rs_mat ...
     952              : ! **************************************************************************************************
     953          954 :    SUBROUTINE rsmat_to_kp(mat_rs, ispin, xkp, cell_to_index_scf, sab_nl, bs_env, cfm_kp, imag_rs_mat)
     954              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_rs
     955              :       INTEGER                                            :: ispin
     956              :       REAL(KIND=dp), DIMENSION(3)                        :: xkp
     957              :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index_scf
     958              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     959              :          POINTER                                         :: sab_nl
     960              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     961              :       TYPE(cp_cfm_type)                                  :: cfm_kp
     962              :       LOGICAL, OPTIONAL                                  :: imag_rs_mat
     963              : 
     964              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'rsmat_to_kp'
     965              : 
     966              :       INTEGER                                            :: handle
     967              :       LOGICAL                                            :: imag_rs_mat_private
     968              :       TYPE(dbcsr_type), POINTER                          :: cmat, rmat
     969              : 
     970          954 :       CALL timeset(routineN, handle)
     971              : 
     972          954 :       ALLOCATE (rmat, cmat)
     973              : 
     974          954 :       imag_rs_mat_private = .FALSE.
     975          954 :       IF (PRESENT(imag_rs_mat)) imag_rs_mat_private = imag_rs_mat
     976              : 
     977          954 :       IF (imag_rs_mat_private) THEN
     978          954 :          CALL dbcsr_create(rmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_antisymmetric)
     979          954 :          CALL dbcsr_create(cmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
     980              :       ELSE
     981            0 :          CALL dbcsr_create(rmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
     982            0 :          CALL dbcsr_create(cmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_antisymmetric)
     983              :       END IF
     984          954 :       CALL cp_dbcsr_alloc_block_from_nbl(rmat, sab_nl)
     985          954 :       CALL cp_dbcsr_alloc_block_from_nbl(cmat, sab_nl)
     986              : 
     987          954 :       CALL dbcsr_set(rmat, 0.0_dp)
     988          954 :       CALL dbcsr_set(cmat, 0.0_dp)
     989              :       CALL rskp_transform(rmatrix=rmat, cmatrix=cmat, rsmat=mat_rs, ispin=ispin, &
     990          954 :                           xkp=xkp, cell_to_index=cell_to_index_scf, sab_nl=sab_nl)
     991              : 
     992          954 :       CALL copy_dbcsr_to_fm(rmat, bs_env%fm_work_mo(1))
     993          954 :       CALL copy_dbcsr_to_fm(cmat, bs_env%fm_work_mo(2))
     994          954 :       CALL cp_fm_to_cfm(bs_env%fm_work_mo(1), bs_env%fm_work_mo(2), cfm_kp)
     995              : 
     996          954 :       CALL dbcsr_deallocate_matrix(rmat)
     997          954 :       CALL dbcsr_deallocate_matrix(cmat)
     998              : 
     999          954 :       CALL timestop(handle)
    1000              : 
    1001          954 :    END SUBROUTINE rsmat_to_kp
    1002              : 
    1003              : ! **************************************************************************************************
    1004              : !> \brief ...
    1005              : !> \param bs_env ...
    1006              : ! **************************************************************************************************
    1007          126 :    SUBROUTINE diagonalize_ks_matrix(bs_env)
    1008              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1009              : 
    1010              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'diagonalize_ks_matrix'
    1011              : 
    1012              :       INTEGER                                            :: handle, ikp, ispin, nmo_retained
    1013              :       REAL(KIND=dp)                                      :: CBM, VBM
    1014              : 
    1015          126 :       CALL timeset(routineN, handle)
    1016              : 
    1017          504 :       ALLOCATE (bs_env%eigenval_scf_Gamma(bs_env%n_ao, bs_env%n_spin))
    1018              : 
    1019          270 :       DO ispin = 1, bs_env%n_spin
    1020              : 
    1021              :          ! use work matrices because the matrices are overwritten in cp_fm_geeig_canon
    1022          144 :          CALL cp_fm_to_fm(bs_env%fm_ks_Gamma(ispin), bs_env%fm_work_mo(1))
    1023          144 :          CALL cp_fm_to_fm(bs_env%fm_s_Gamma, bs_env%fm_work_mo(2))
    1024              : 
    1025              :          ! diagonalize the Kohn-Sham matrix to obtain MO coefficients and SCF eigenvalues
    1026              :          ! (at the Gamma-point)
    1027              :          CALL cp_fm_geeig_canon(bs_env%fm_work_mo(1), &
    1028              :                                 bs_env%fm_work_mo(2), &
    1029              :                                 bs_env%fm_mo_coeff_Gamma(ispin), &
    1030              :                                 bs_env%eigenval_scf_Gamma(:, ispin), &
    1031              :                                 bs_env%fm_work_mo(3), &
    1032              :                                 bs_env%eps_eigval_mat_s, &
    1033          144 :                                 nmo_retained=nmo_retained)
    1034          144 :          bs_env%n_mo_retained = MIN(bs_env%n_mo_retained, nmo_retained)
    1035              : 
    1036          144 :          VBM = bs_env%eigenval_scf_Gamma(bs_env%n_occ(ispin), ispin)
    1037          144 :          CBM = bs_env%eigenval_scf_Gamma(bs_env%n_occ(ispin) + 1, ispin)
    1038              : 
    1039          270 :          bs_env%e_fermi(ispin) = 0.5_dp*(VBM + CBM)
    1040              : 
    1041              :       END DO
    1042              : 
    1043          126 :       CALL timestop(handle)
    1044              : 
    1045              :       ! Gamma-only path for molecules: eigenval_scf is filled here from the Gamma eigenvalues
    1046          270 :       DO ispin = 1, bs_env%n_spin
    1047          438 :          DO ikp = 1, bs_env%nkp_bs_and_DOS
    1048         2396 :             bs_env%eigenval_scf(:, ikp, ispin) = bs_env%eigenval_scf_Gamma(:, ispin)
    1049              :          END DO
    1050              :       END DO
    1051              : 
    1052          126 :    END SUBROUTINE diagonalize_ks_matrix
    1053              : 
    1054              : ! **************************************************************************************************
    1055              : !> \brief ...
    1056              : !> \param bs_env ...
    1057              : !> \param qs_env ...
    1058              : ! **************************************************************************************************
    1059          126 :    SUBROUTINE check_positive_definite_overlap_mat(bs_env, qs_env)
    1060              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1061              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1062              : 
    1063              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'check_positive_definite_overlap_mat'
    1064              : 
    1065              :       INTEGER                                            :: handle, ikp, info, u
    1066              :       TYPE(cp_cfm_type)                                  :: cfm_s_ikp
    1067              : 
    1068          126 :       CALL timeset(routineN, handle)
    1069              : 
    1070          272 :       DO ikp = 1, bs_env%kpoints_DOS%nkp
    1071              : 
    1072              :          ! get S_µν(k_i) from S_µν(k=0)
    1073              :          CALL cfm_ikp_from_fm_Gamma(cfm_s_ikp, bs_env%fm_s_Gamma, &
    1074          146 :                                     ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1075              : 
    1076              :          ! check whether S_µν(k_i) is positive definite
    1077          146 :          CALL cp_cfm_cholesky_decompose(matrix=cfm_s_ikp, n=bs_env%n_ao, info_out=info)
    1078              : 
    1079              :          ! check if Cholesky decomposition failed (Cholesky decomposition only works for
    1080              :          ! positive definite matrices
    1081          272 :          IF (info /= 0) THEN
    1082            0 :             u = bs_env%unit_nr
    1083              : 
    1084            0 :             IF (u > 0) THEN
    1085            0 :                WRITE (u, FMT="(T2,A)") ""
    1086              :                WRITE (u, FMT="(T2,A)") "ERROR: The Cholesky decomposition "// &
    1087            0 :                   "of the k-point overlap matrix failed. This is"
    1088              :                WRITE (u, FMT="(T2,A)") "because the algorithm is "// &
    1089            0 :                   "only correct in the limit of large cells. The cell of "
    1090              :                WRITE (u, FMT="(T2,A)") "the calculation is too small. "// &
    1091            0 :                   "Use MULTIPLE_UNIT_CELL to create a larger cell "
    1092            0 :                WRITE (u, FMT="(T2,A)") "and to prevent this error."
    1093              :             END IF
    1094              : 
    1095            0 :             CALL bs_env%para_env%sync()
    1096            0 :             CPABORT("Please see information on the error above.")
    1097              : 
    1098              :          END IF ! Cholesky decomposition failed
    1099              : 
    1100              :       END DO ! ikp
    1101              : 
    1102          126 :       CALL cp_cfm_release(cfm_s_ikp)
    1103              : 
    1104          126 :       CALL timestop(handle)
    1105              : 
    1106          126 :    END SUBROUTINE check_positive_definite_overlap_mat
    1107              : 
    1108              : ! **************************************************************************************************
    1109              : !> \brief ...
    1110              : !> \param qs_env ...
    1111              : !> \param bs_env ...
    1112              : ! **************************************************************************************************
    1113          316 :    SUBROUTINE get_parameters_from_qs_env(qs_env, bs_env)
    1114              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1115              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1116              : 
    1117              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_parameters_from_qs_env'
    1118              : 
    1119              :       INTEGER                                            :: color_sub, handle, homo, n_ao, n_atom, &
    1120              :                                                             nelectron, u
    1121              :       INTEGER, DIMENSION(3)                              :: periodic
    1122              :       REAL(KIND=dp)                                      :: maxocc
    1123              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat
    1124              :       TYPE(cell_type), POINTER                           :: cell
    1125              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1126          158 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1127              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1128          158 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1129              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1130              :       TYPE(section_vals_type), POINTER                   :: input
    1131              : 
    1132          158 :       CALL timeset(routineN, handle)
    1133              : 
    1134              :       CALL get_qs_env(qs_env, &
    1135              :                       dft_control=dft_control, &
    1136              :                       scf_control=scf_control, &
    1137          158 :                       mos=mos)
    1138              : 
    1139          158 :       bs_env%n_spin = dft_control%nspins
    1140          158 :       IF (bs_env%n_spin == 1) bs_env%spin_degeneracy = 2.0_dp
    1141          158 :       IF (bs_env%n_spin == 2) bs_env%spin_degeneracy = 1.0_dp
    1142              : 
    1143          158 :       CALL get_mo_set(mo_set=mos(1), nao=n_ao, homo=homo, nelectron=nelectron, maxocc=maxocc)
    1144          158 :       bs_env%n_ao = n_ao
    1145          474 :       bs_env%n_occ_nosmear(1:2) = NINT(nelectron/maxocc) !fix for moire large-cell Gamma
    1146          474 :       bs_env%n_electron(1:2) = nelectron
    1147              :       ! lowered by the diagonalization below if the canonical orthogonalization removes modes
    1148          158 :       bs_env%n_mo_retained = n_ao
    1149          474 :       bs_env%n_occ(1:2) = homo
    1150          474 :       bs_env%n_vir(1:2) = n_ao - homo
    1151          158 :       IF (bs_env%n_spin == 2) THEN
    1152           18 :          CALL get_mo_set(mo_set=mos(2), homo=homo, nelectron=nelectron, maxocc=maxocc)
    1153           18 :          bs_env%n_occ_nosmear(2) = NINT(nelectron/maxocc)
    1154           18 :          bs_env%n_occ(2) = homo
    1155           18 :          bs_env%n_vir(2) = n_ao - homo
    1156           18 :          bs_env%n_electron(2) = nelectron
    1157              :       END IF
    1158          158 :       bs_env%eps_eigval_mat_s = scf_control%eps_eigval
    1159              :       ! reuse the ground-state SCF's &SMEAR settings (if any) for the metal-capable Fermi
    1160              :       ! level search (get_fermi_level_1c/2c); SMEAR_ENERGY_WINDOW and SMEAR_LIST are not
    1161              :       ! supported by Smearkp/Smearkp2, so fall back to the T=0 bisection for those
    1162          158 :       bs_env%do_smear = .FALSE.
    1163          158 :       bs_env%smear_method = smear_fermi_dirac
    1164          158 :       bs_env%smear_width = 0.0_dp
    1165          158 :       IF (scf_control%smear%do_smear) THEN
    1166            4 :          SELECT CASE (scf_control%smear%method)
    1167              :          CASE (smear_fermi_dirac)
    1168            2 :             bs_env%do_smear = .TRUE.
    1169            2 :             bs_env%smear_method = smear_fermi_dirac
    1170            2 :             bs_env%smear_width = scf_control%smear%electronic_temperature
    1171              :          CASE (smear_gaussian, smear_mp, smear_mv)
    1172            0 :             bs_env%do_smear = .TRUE.
    1173            0 :             bs_env%smear_method = scf_control%smear%method
    1174            0 :             bs_env%smear_width = scf_control%smear%smearing_width
    1175              :          CASE DEFAULT
    1176            2 :             IF (bs_env%unit_nr > 0) THEN
    1177              :                WRITE (bs_env%unit_nr, '(T2,A)') &
    1178              :                   'SMEAR method not supported for the post-SCF Fermi level search, '// &
    1179            0 :                   'falling back to a T=0 bisection.'
    1180              :             END IF
    1181              :          END SELECT
    1182              :       END IF
    1183          158 :       CPASSERT(scf_control%smear%fixed_mag_mom <= 0.0_dp)
    1184              :       ! get para_env from qs_env (bs_env%para_env is identical to para_env in qs_env)
    1185          158 :       CALL get_qs_env(qs_env, para_env=para_env)
    1186          158 :       color_sub = 0
    1187          158 :       ALLOCATE (bs_env%para_env)
    1188          158 :       CALL bs_env%para_env%from_split(para_env, color_sub)
    1189              : 
    1190          158 :       CALL get_qs_env(qs_env, particle_set=particle_set)
    1191              : 
    1192          158 :       n_atom = SIZE(particle_set)
    1193          158 :       bs_env%n_atom = n_atom
    1194              : 
    1195          158 :       CALL get_qs_env(qs_env=qs_env, cell=cell)
    1196          158 :       CALL get_cell(cell=cell, periodic=periodic, h=hmat)
    1197          632 :       bs_env%periodic(1:3) = periodic(1:3)
    1198          506 :       bs_env%do_periodic = ANY(bs_env%periodic /= 0)
    1199         2054 :       bs_env%hmat(1:3, 1:3) = hmat
    1200          158 :       bs_env%nimages_scf = dft_control%nimages
    1201          158 :       IF (dft_control%nimages == 1) THEN
    1202          126 :          IF (bs_env%do_gw_ri_rs) THEN
    1203           42 :             IF (bs_env%do_periodic) THEN
    1204            0 :                bs_env%gw_implementation = ri_rs_large_cell_Gamma
    1205              :             ELSE
    1206           42 :                bs_env%gw_implementation = ri_rs_non_periodic
    1207              :             END IF
    1208           84 :          ELSE IF (bs_env%do_static_cohsex) THEN
    1209            8 :             bs_env%gw_implementation = tensor_static_cohsex
    1210              :          ELSE
    1211           76 :             bs_env%gw_implementation = tensor_large_cell_Gamma
    1212              :          END IF
    1213           32 :       ELSE IF (dft_control%nimages > 1) THEN
    1214           32 :          IF (bs_env%do_gw_ri_rs) THEN
    1215           14 :             bs_env%gw_implementation = ri_rs_small_cell_full_kp
    1216           18 :          ELSE IF (bs_env%do_static_cohsex) THEN
    1217            0 :             CPABORT("COHSEX Not Implemented for K-point Calculations")
    1218              :          ELSE
    1219           18 :             bs_env%gw_implementation = tensor_small_cell_full_kp
    1220              :          END IF
    1221              :       ELSE
    1222            0 :          CPABORT("Wrong number of cells from DFT calculation.")
    1223              :       END IF
    1224              : 
    1225          158 :       u = bs_env%unit_nr
    1226              : 
    1227              :       ! Marek : Get and save the rtp method
    1228          158 :       CALL get_qs_env(qs_env=qs_env, input=input)
    1229          158 :       CALL section_vals_val_get(input, "DFT%REAL_TIME_PROPAGATION%RTBSE%_SECTION_PARAMETERS_", i_val=bs_env%rtp_method)
    1230              : 
    1231          158 :       IF (u > 0) THEN
    1232           79 :          WRITE (u, FMT="(T2,2A,T73,I8)") "Number of occupied molecular orbitals (MOs) ", &
    1233          158 :             "= Number of occupied bands", homo
    1234           79 :          WRITE (u, FMT="(T2,2A,T73,I8)") "Number of unoccupied (= virtual) MOs ", &
    1235          158 :             "= Number of unoccupied bands", n_ao - homo
    1236           79 :          WRITE (u, FMT="(T2,A,T73,I8)") "Number of Gaussian basis functions for MOs", n_ao
    1237           79 :          IF (bs_env%gw_implementation == tensor_small_cell_full_kp .OR. &
    1238              :              bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
    1239           16 :             WRITE (u, FMT="(T2,2A,T73,I8)") "Number of cells considered in the DFT ", &
    1240           32 :                "calculation", bs_env%nimages_scf
    1241              :          END IF
    1242              :       END IF
    1243              : 
    1244          158 :       CALL timestop(handle)
    1245              : 
    1246          158 :    END SUBROUTINE get_parameters_from_qs_env
    1247              : 
    1248              : ! **************************************************************************************************
    1249              : !> \brief ...
    1250              : !> \param bs_env ...
    1251              : ! **************************************************************************************************
    1252          158 :    SUBROUTINE set_heuristic_parameters(bs_env)
    1253              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1254              : 
    1255              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_heuristic_parameters'
    1256              : 
    1257              :       INTEGER                                            :: handle
    1258              : 
    1259          158 :       CALL timeset(routineN, handle)
    1260              : 
    1261          158 :       bs_env%n_bins_max_for_printing = 5000
    1262              : 
    1263          158 :       CALL timestop(handle)
    1264              : 
    1265          158 :    END SUBROUTINE set_heuristic_parameters
    1266              : 
    1267              : ! **************************************************************************************************
    1268              : !> \brief ...
    1269              : !> \param qs_env ...
    1270              : !> \param bs_env ...
    1271              : ! **************************************************************************************************
    1272          158 :    SUBROUTINE allocate_and_fill_fm_ks_fm_s(qs_env, bs_env)
    1273              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1274              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1275              : 
    1276              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_and_fill_fm_ks_fm_s'
    1277              : 
    1278              :       INTEGER                                            :: handle, i_work, ispin
    1279              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
    1280              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1281          158 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks, matrix_s
    1282              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1283              : 
    1284          158 :       CALL timeset(routineN, handle)
    1285              : 
    1286              :       CALL get_qs_env(qs_env, &
    1287              :                       para_env=para_env, &
    1288              :                       blacs_env=blacs_env, &
    1289              :                       matrix_ks_kp=matrix_ks, &
    1290          158 :                       matrix_s_kp=matrix_s)
    1291              : 
    1292          158 :       NULLIFY (fm_struct)
    1293              :       CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=bs_env%n_ao, &
    1294          158 :                                ncol_global=bs_env%n_ao, para_env=para_env)
    1295              : 
    1296          790 :       DO i_work = 1, SIZE(bs_env%fm_work_mo)
    1297          790 :          CALL cp_fm_create(bs_env%fm_work_mo(i_work), fm_struct)
    1298              :       END DO
    1299              : 
    1300          158 :       CALL cp_cfm_create(bs_env%cfm_work_mo, fm_struct)
    1301          158 :       CALL cp_cfm_create(bs_env%cfm_work_mo_2, fm_struct)
    1302              : 
    1303          158 :       CALL cp_fm_create(bs_env%fm_s_Gamma, fm_struct)
    1304          158 :       CALL copy_dbcsr_to_fm(matrix_s(1, 1)%matrix, bs_env%fm_s_Gamma)
    1305              : 
    1306          334 :       DO ispin = 1, bs_env%n_spin
    1307          176 :          CALL cp_fm_create(bs_env%fm_ks_Gamma(ispin), fm_struct)
    1308          176 :          CALL copy_dbcsr_to_fm(matrix_ks(ispin, 1)%matrix, bs_env%fm_ks_Gamma(ispin))
    1309          334 :          CALL cp_fm_create(bs_env%fm_mo_coeff_Gamma(ispin), fm_struct)
    1310              :       END DO
    1311              : 
    1312          158 :       CALL cp_fm_struct_release(fm_struct)
    1313              : 
    1314          158 :       NULLIFY (bs_env%mat_ao_ao%matrix)
    1315          158 :       ALLOCATE (bs_env%mat_ao_ao%matrix)
    1316              :       CALL dbcsr_create(bs_env%mat_ao_ao%matrix, template=matrix_s(1, 1)%matrix, &
    1317          158 :                         matrix_type=dbcsr_type_no_symmetry)
    1318              : 
    1319          790 :       ALLOCATE (bs_env%eigenval_scf(bs_env%n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
    1320              : 
    1321          158 :       CALL timestop(handle)
    1322              : 
    1323          158 :    END SUBROUTINE allocate_and_fill_fm_ks_fm_s
    1324              : 
    1325              : ! **************************************************************************************************
    1326              : !> \brief ...
    1327              : !> \param qs_env ...
    1328              : !> \param bs_env ...
    1329              : ! **************************************************************************************************
    1330          158 :    SUBROUTINE eval_bandstructure_properties(qs_env, bs_env)
    1331              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1332              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1333              : 
    1334              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'eval_bandstructure_properties'
    1335              : 
    1336              :       CHARACTER(LEN=default_string_length)               :: gw_label, gw_soc_label
    1337              :       INTEGER                                            :: handle, homo, homo_1, homo_2, &
    1338              :                                                             homo_spinor, ikp, ikp_for_file, ispin, &
    1339              :                                                             n_ao, n_E, nkind, nkp
    1340              :       LOGICAL                                            :: is_bandstruc_kpoint, print_DOS_kpoints, &
    1341              :                                                             print_ikp
    1342              :       REAL(KIND=dp)                                      :: broadening, E_max, E_max_GW, E_min, &
    1343              :                                                             E_min_GW, E_total_window, &
    1344              :                                                             energy_step_DOS, energy_window_DOS, &
    1345              :                                                             n_el_spinor, t1
    1346          158 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: DOS_G0W0_SOC, DOS_GW, DOS_GW_SOC, DOS_scf, &
    1347          158 :          DOS_scf_SOC, eigenval, eigenval_spinor, eigenval_spinor_G0W0, eigenval_spinor_GW, &
    1348          158 :          eigenval_spinor_no_SOC
    1349          158 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: eigenval_spinor_all_G0W0, &
    1350          158 :          eigenval_spinor_all_gw, eigenval_spinor_all_scf, PDOS_G0W0_SOC, PDOS_GW, PDOS_GW_SOC, &
    1351          158 :          PDOS_scf, PDOS_scf_SOC, proj_mo_on_kind
    1352          158 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_GW_2d, LDOS_scf_2d, LDOS_scf_2d_SOC
    1353              :       TYPE(band_edges_type) :: band_edges_G0W0, band_edges_G0W0_SOC, band_edges_GW, &
    1354              :          band_edges_GW_SOC, band_edges_scf, band_edges_scf_guess, band_edges_scf_SOC
    1355              :       TYPE(cp_cfm_type) :: cfm_ks_ikp, cfm_ks_ikp_spinor, cfm_mos_ikp_spinor, cfm_s_ikp, &
    1356              :          cfm_s_ikp_copy, cfm_s_ikp_spinor, cfm_s_ikp_spinor_copy, cfm_SOC_ikp_spinor, &
    1357              :          cfm_spinor_wf_ikp, cfm_work_ikp, cfm_work_ikp_spinor
    1358          474 :       TYPE(cp_cfm_type), DIMENSION(2)                    :: cfm_mos_ikp
    1359              : 
    1360          158 :       CALL timeset(routineN, handle)
    1361              : 
    1362          158 :       gw_label = gw_flavour_label(bs_env)
    1363              : 
    1364              :       ! GW denotes the final result of the requested flavour.
    1365              :       ! G0W0 denotes the first cycle, also retained separately during evGW0.
    1366              : 
    1367          158 :       n_ao = bs_env%n_ao
    1368              : 
    1369          158 :       energy_window_DOS = bs_env%energy_window_DOS
    1370          158 :       energy_step_DOS = bs_env%energy_step_DOS
    1371          158 :       broadening = bs_env%broadening_DOS
    1372              : 
    1373              :       ! if we have done GW or a full kpoint SCF, we already have the band edges
    1374              :       IF (bs_env%do_gw .OR. &
    1375          158 :           bs_env%gw_implementation == tensor_small_cell_full_kp .OR. &
    1376              :           bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
    1377          158 :          band_edges_scf = bs_env%band_edges_scf
    1378          158 :          band_edges_scf_guess = band_edges_scf
    1379              :       ELSE
    1380              : 
    1381            0 :          IF (bs_env%n_spin == 1) THEN
    1382            0 :             homo = bs_env%n_occ(1)
    1383            0 :             band_edges_scf_guess%VBM = bs_env%eigenval_scf_Gamma(homo, 1)
    1384            0 :             band_edges_scf_guess%CBM = bs_env%eigenval_scf_Gamma(homo + 1, 1)
    1385              :          ELSE
    1386            0 :             homo_1 = bs_env%n_occ(1)
    1387            0 :             homo_2 = bs_env%n_occ(2)
    1388              :             band_edges_scf_guess%VBM = MAX(bs_env%eigenval_scf_Gamma(homo_1, 1), &
    1389            0 :                                            bs_env%eigenval_scf_Gamma(homo_2, 2))
    1390              :             band_edges_scf_guess%CBM = MIN(bs_env%eigenval_scf_Gamma(homo_1 + 1, 1), &
    1391            0 :                                            bs_env%eigenval_scf_Gamma(homo_2 + 1, 2))
    1392              :          END IF
    1393              : 
    1394              :          ! initialization
    1395            0 :          band_edges_scf%VBM = -1000.0_dp
    1396            0 :          band_edges_scf%CBM = 1000.0_dp
    1397            0 :          band_edges_scf%DBG = 1000.0_dp
    1398              :       END IF
    1399              : 
    1400          158 :       E_min = band_edges_scf_guess%VBM - 0.5_dp*energy_window_DOS
    1401          158 :       E_max = band_edges_scf_guess%CBM + 0.5_dp*energy_window_DOS
    1402              : 
    1403          158 :       IF (bs_env%do_gw) THEN
    1404          156 :          band_edges_GW = bs_env%band_edges_GW
    1405          156 :          E_min_GW = band_edges_GW%VBM - 0.5_dp*energy_window_DOS
    1406          156 :          E_max_GW = band_edges_GW%CBM + 0.5_dp*energy_window_DOS
    1407          156 :          E_min = MIN(E_min, E_min_GW)
    1408          156 :          E_max = MAX(E_max, E_max_GW)
    1409          156 :          IF (bs_env%gw_flavour == evGW0 .AND. bs_env%do_soc) THEN
    1410            0 :             CALL get_VBM_CBM_bandgaps(band_edges_G0W0, bs_env%eigenval_G0W0, bs_env)
    1411            0 :             E_min = MIN(E_min, band_edges_G0W0%VBM - 0.5_dp*energy_window_DOS)
    1412            0 :             E_max = MAX(E_max, band_edges_G0W0%CBM + 0.5_dp*energy_window_DOS)
    1413              :          END IF
    1414              :       END IF
    1415              : 
    1416          158 :       E_total_window = E_max - E_min
    1417              : 
    1418          158 :       n_E = INT(E_total_window/energy_step_DOS)
    1419              : 
    1420          158 :       CALL get_qs_env(qs_env, nkind=nkind)
    1421              : 
    1422          632 :       ALLOCATE (proj_mo_on_kind(n_ao, nkind))
    1423          158 :       proj_mo_on_kind(:, :) = 0.0_dp
    1424              : 
    1425          474 :       ALLOCATE (eigenval(n_ao))
    1426          474 :       ALLOCATE (eigenval_spinor(2*n_ao))
    1427          316 :       ALLOCATE (eigenval_spinor_no_SOC(2*n_ao))
    1428          316 :       ALLOCATE (eigenval_spinor_GW(2*n_ao))
    1429          158 :       IF (bs_env%do_soc .AND. bs_env%do_gw .AND. bs_env%gw_flavour == evGW0) THEN
    1430            0 :          ALLOCATE (eigenval_spinor_G0W0(2*n_ao))
    1431              :       END IF
    1432              : 
    1433              :       ! collect the SOC spinor eigenvalues of every DOS k-point (band-structure-path
    1434              :       ! k-points are included but carry wkp=0, see get_fermi_level_1c) to later obtain
    1435              :       ! the SCF+SOC (and, if requested, GW+SOC) Fermi level from get_fermi_level_1c
    1436          158 :       IF (bs_env%do_soc) THEN
    1437           88 :          ALLOCATE (eigenval_spinor_all_scf(2*n_ao, bs_env%nkp_bs_and_DOS))
    1438           22 :          IF (bs_env%do_gw) THEN
    1439           66 :             ALLOCATE (eigenval_spinor_all_gw(2*n_ao, bs_env%nkp_bs_and_DOS))
    1440           22 :             IF (bs_env%gw_flavour == evGW0) THEN
    1441            0 :                ALLOCATE (eigenval_spinor_all_G0W0(2*n_ao, bs_env%nkp_bs_and_DOS))
    1442              :             END IF
    1443              :          END IF
    1444              :       END IF
    1445              : 
    1446          158 :       IF (bs_env%do_dos_pdos) THEN
    1447              : 
    1448           84 :          ALLOCATE (DOS_scf(n_E))
    1449           28 :          DOS_scf(:) = 0.0_dp
    1450          112 :          ALLOCATE (PDOS_scf(n_E, nkind))
    1451           28 :          PDOS_scf(:, :) = 0.0_dp
    1452              : 
    1453           28 :          IF (bs_env%do_soc) THEN
    1454              : 
    1455           32 :             ALLOCATE (DOS_scf_SOC(n_E))
    1456           16 :             DOS_scf_SOC(:) = 0.0_dp
    1457           48 :             ALLOCATE (PDOS_scf_SOC(n_E, nkind))
    1458           16 :             PDOS_scf_SOC(:, :) = 0.0_dp
    1459              : 
    1460              :          END IF
    1461              : 
    1462           28 :          IF (bs_env%do_gw) THEN
    1463              : 
    1464           56 :             ALLOCATE (DOS_GW(n_E))
    1465           28 :             DOS_GW(:) = 0.0_dp
    1466           84 :             ALLOCATE (PDOS_GW(n_E, nkind))
    1467           28 :             PDOS_GW(:, :) = 0.0_dp
    1468              : 
    1469           28 :             IF (bs_env%do_soc) THEN
    1470              : 
    1471           32 :                ALLOCATE (DOS_GW_SOC(n_E))
    1472           16 :                DOS_GW_SOC(:) = 0.0_dp
    1473           48 :                ALLOCATE (PDOS_GW_SOC(n_E, nkind))
    1474           16 :                PDOS_GW_SOC(:, :) = 0.0_dp
    1475           16 :                IF (bs_env%gw_flavour == evGW0) THEN
    1476            0 :                   ALLOCATE (DOS_G0W0_SOC(n_E), PDOS_G0W0_SOC(n_E, nkind))
    1477            0 :                   DOS_G0W0_SOC = 0.0_dp
    1478            0 :                   PDOS_G0W0_SOC = 0.0_dp
    1479              :                END IF
    1480              : 
    1481              :             END IF
    1482              :          END IF
    1483              :       END IF
    1484              : 
    1485          158 :       CALL cp_cfm_create(cfm_mos_ikp(1), bs_env%fm_ks_Gamma(1)%matrix_struct)
    1486          158 :       CALL cp_cfm_create(cfm_mos_ikp(2), bs_env%fm_ks_Gamma(1)%matrix_struct)
    1487          158 :       CALL cp_cfm_create(cfm_work_ikp, bs_env%fm_ks_Gamma(1)%matrix_struct)
    1488          158 :       CALL cp_cfm_create(cfm_s_ikp_copy, bs_env%fm_ks_Gamma(1)%matrix_struct)
    1489              : 
    1490          158 :       IF (bs_env%do_soc) THEN
    1491              : 
    1492           22 :          CALL cp_cfm_create(cfm_mos_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1493           22 :          CALL cp_cfm_create(cfm_work_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1494           22 :          CALL cp_cfm_create(cfm_s_ikp_spinor_copy, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1495           22 :          CALL cp_cfm_create(cfm_ks_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1496           22 :          CALL cp_cfm_create(cfm_SOC_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1497           22 :          CALL cp_cfm_create(cfm_s_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1498           22 :          CALL cp_cfm_create(cfm_spinor_wf_ikp, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1499              : 
    1500           22 :          homo_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    1501              : 
    1502           18 :          SELECT CASE (bs_env%n_spin)
    1503              :          CASE (1)
    1504           18 :             n_el_spinor = REAL(bs_env%n_electron(1), KIND=dp)
    1505              :          CASE (2)
    1506           22 :             n_el_spinor = REAL(bs_env%n_electron(1) + bs_env%n_electron(2), KIND=dp)
    1507              :          END SELECT
    1508              : 
    1509           22 :          band_edges_scf_SOC%VBM = -1000.0_dp
    1510           22 :          band_edges_scf_SOC%CBM = 1000.0_dp
    1511           22 :          band_edges_scf_SOC%DBG = 1000.0_dp
    1512              : 
    1513           22 :          IF (bs_env%do_gw) THEN
    1514           22 :             band_edges_GW_SOC%VBM = -1000.0_dp
    1515           22 :             band_edges_GW_SOC%CBM = 1000.0_dp
    1516           22 :             band_edges_GW_SOC%DBG = 1000.0_dp
    1517           22 :             IF (bs_env%gw_flavour == evGW0) THEN
    1518            0 :                band_edges_G0W0_SOC%VBM = -1000.0_dp
    1519            0 :                band_edges_G0W0_SOC%CBM = 1000.0_dp
    1520            0 :                band_edges_G0W0_SOC%DBG = 1000.0_dp
    1521              :             END IF
    1522              :          END IF
    1523              : 
    1524           22 :          IF (bs_env%unit_nr > 0) THEN
    1525           11 :             WRITE (bs_env%unit_nr, '(A)') ''
    1526           11 :             IF (bs_env%soc_window_occ > 0.0_dp) THEN
    1527            3 :                WRITE (bs_env%unit_nr, '(T2,A,T71,F10.2)') 'SOC requested, SOC energy window occ (eV):', &
    1528            6 :                   bs_env%soc_window_occ*evolt
    1529              :             ELSE
    1530            8 :                WRITE (bs_env%unit_nr, '(T2,A,T71,A10)') 'SOC requested, SOC energy window occ (eV):', &
    1531           16 :                   ' no window'
    1532              :             END IF
    1533           11 :             IF (bs_env%soc_window_virt > 0.0_dp) THEN
    1534            3 :                WRITE (bs_env%unit_nr, '(T2,A,T71,F10.2)') 'SOC requested, SOC energy window virt (eV):', &
    1535            6 :                   bs_env%soc_window_virt*evolt
    1536              :             ELSE
    1537            8 :                WRITE (bs_env%unit_nr, '(T2,A,T71,A10)') 'SOC requested, SOC energy window virt (eV):', &
    1538           16 :                   ' no window'
    1539              :             END IF
    1540           11 :             IF (bs_env%soc_window_occ > 0.0_dp .OR. bs_env%soc_window_virt > 0.0_dp) THEN
    1541            4 :                WRITE (bs_env%unit_nr, '(T2,A,T71,F10.2)') 'SOC requested, SOC window smearing (eV):', &
    1542            8 :                   bs_env%soc_window_smearing*evolt
    1543              :             END IF
    1544              :          END IF
    1545              :       END IF
    1546              : 
    1547          158 :       IF (bs_env%do_ldos) THEN
    1548            2 :          CPASSERT(bs_env%int_ldos_xyz == int_ldos_z)
    1549              :       END IF
    1550              : 
    1551          158 :       IF (bs_env%gw_implementation == tensor_small_cell_full_kp .OR. &
    1552              :           bs_env%gw_implementation == ri_rs_small_cell_full_kp) THEN
    1553           32 :          CALL cp_cfm_create(cfm_ks_ikp, bs_env%cfm_ks_kp(1, 1)%matrix_struct)
    1554           32 :          CALL cp_cfm_create(cfm_s_ikp, bs_env%cfm_ks_kp(1, 1)%matrix_struct)
    1555              :       END IF
    1556              : 
    1557          946 :       DO ikp = 1, bs_env%nkp_bs_and_DOS
    1558              : 
    1559          788 :          t1 = m_walltime()
    1560              : 
    1561         1598 :          DO ispin = 1, bs_env%n_spin
    1562              : 
    1563          978 :             SELECT CASE (bs_env%gw_implementation)
    1564              :             CASE (tensor_large_cell_Gamma, ri_rs_large_cell_Gamma, ri_rs_non_periodic, tensor_static_cohsex)
    1565              : 
    1566              :                ! 1. get H^KS_µν(k_i) from H^KS_µν(k=0)
    1567              :                CALL cfm_ikp_from_fm_Gamma(cfm_ks_ikp, bs_env%fm_ks_Gamma(ispin), &
    1568          168 :                                           ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1569              : 
    1570              :                ! 2. get S_µν(k_i) from S_µν(k=0)
    1571              :                CALL cfm_ikp_from_fm_Gamma(cfm_s_ikp, bs_env%fm_s_Gamma, &
    1572          168 :                                           ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1573          168 :                CALL cp_cfm_to_cfm(cfm_s_ikp, cfm_s_ikp_copy)
    1574              : 
    1575              :                ! 3. Diagonalize (Roothaan-Hall): H_KS(k_i)*C(k_i) = S(k_i)*C(k_i)*ϵ(k_i)
    1576              :                CALL cp_cfm_geeig(cfm_ks_ikp, cfm_s_ikp_copy, cfm_mos_ikp(ispin), &
    1577          168 :                                  eigenval, cfm_work_ikp)
    1578              : 
    1579         2252 :                bs_env%eigenval_scf(:, ikp, ispin) = eigenval(:)
    1580              : 
    1581              :             CASE (tensor_small_cell_full_kp, ri_rs_small_cell_full_kp)
    1582              : 
    1583              :                ! 1. get H^KS_µν(k_i)
    1584          642 :                CALL cp_cfm_to_cfm(bs_env%cfm_ks_kp(ikp, ispin), cfm_ks_ikp)
    1585              : 
    1586              :                ! 2. get S_µν(k_i)
    1587          642 :                CALL cp_cfm_to_cfm(bs_env%cfm_s_kp(ikp), cfm_s_ikp)
    1588              : 
    1589              :                ! 3. get C_µn(k_i) and ϵ_n(k_i)
    1590          642 :                CALL cp_cfm_to_cfm(bs_env%cfm_mo_coeff_kp(ikp, ispin), cfm_mos_ikp(ispin))
    1591         7238 :                eigenval(:) = bs_env%eigenval_scf(:, ikp, ispin)
    1592              : 
    1593              :             END SELECT
    1594              : 
    1595          810 :             IF (bs_env%do_gw) CALL write_DFT_eigenvalues(eigenval, ikp, ispin, bs_env)
    1596              : 
    1597              :             ! 4. Projection p_nk^A of MO ψ_nk(r) on atom type A (inspired by Mulliken charge)
    1598              :             !    p_nk^A = sum_µ^A,ν C*_µ^A,n(k) S_µ^A,ν(k) C_ν,n(k)
    1599          810 :             CALL compute_proj_mo_on_kind(proj_mo_on_kind, qs_env, cfm_mos_ikp(ispin), cfm_s_ikp)
    1600              : 
    1601              :             ! 5. DOS and PDOS
    1602          810 :             IF (bs_env%do_dos_pdos) THEN
    1603              :                CALL add_to_DOS_PDOS(DOS_scf, PDOS_scf, eigenval, ikp, bs_env, n_E, E_min, &
    1604          254 :                                     proj_mo_on_kind)
    1605              : 
    1606          254 :                IF (bs_env%do_gw) THEN
    1607              :                   CALL add_to_DOS_PDOS(DOS_GW, PDOS_GW, bs_env%eigenval_GW(:, ikp, ispin), &
    1608          254 :                                        ikp, bs_env, n_E, E_min, proj_mo_on_kind)
    1609              :                END IF
    1610              :             END IF
    1611              : 
    1612          810 :             IF (bs_env%do_ldos) THEN
    1613              :                CALL add_to_LDOS_2d(LDOS_scf_2d, qs_env, ikp, bs_env, cfm_mos_ikp(ispin), &
    1614            2 :                                    eigenval(:), band_edges_scf_guess)
    1615              : 
    1616            2 :                IF (bs_env%do_gw) THEN
    1617              :                   CALL add_to_LDOS_2d(LDOS_GW_2d, qs_env, ikp, bs_env, cfm_mos_ikp(ispin), &
    1618            2 :                                       bs_env%eigenval_GW(:, ikp, 1), band_edges_GW)
    1619              :                END IF
    1620              : 
    1621              :             END IF
    1622              : 
    1623          810 :             homo = bs_env%n_occ(ispin)
    1624              : 
    1625          810 :             band_edges_scf%VBM = MAX(band_edges_scf%VBM, eigenval(homo))
    1626          810 :             band_edges_scf%CBM = MIN(band_edges_scf%CBM, eigenval(homo + 1))
    1627         1598 :             band_edges_scf%DBG = MIN(band_edges_scf%DBG, eigenval(homo + 1) - eigenval(homo))
    1628              : 
    1629              :          END DO ! spin
    1630              : 
    1631              :          ! now the same with spin-orbit coupling
    1632          788 :          IF (bs_env%do_soc) THEN
    1633              : 
    1634              :             ! only print eigenvalues of DOS k-points in case no bandstructure path has been given
    1635          328 :             print_DOS_kpoints = (bs_env%nkp_only_bs <= 0)
    1636              :             ! in kpoints_DOS, the last nkp_only_bs are bandstructure k-points
    1637          328 :             is_bandstruc_kpoint = (ikp > bs_env%nkp_only_DOS)
    1638          328 :             print_ikp = print_DOS_kpoints .OR. is_bandstruc_kpoint
    1639              : 
    1640          328 :             IF (print_DOS_kpoints) THEN
    1641          234 :                nkp = bs_env%nkp_only_DOS
    1642          234 :                ikp_for_file = ikp
    1643              :             ELSE
    1644           94 :                nkp = bs_env%nkp_only_bs
    1645           94 :                ikp_for_file = ikp - bs_env%nkp_only_DOS
    1646              :             END IF
    1647              : 
    1648              :             ! compute DFT+SOC eigenvalues; based on these, compute band edges, DOS and LDOS
    1649              :             CALL SOC_ev(bs_env, qs_env, ikp, bs_env%eigenval_scf, &
    1650              :                         E_min, cfm_mos_ikp, DOS_scf_SOC, PDOS_scf_SOC, &
    1651          328 :                         band_edges_scf_SOC, eigenval_spinor, cfm_spinor_wf_ikp)
    1652         8472 :             eigenval_spinor_all_scf(:, ikp) = eigenval_spinor
    1653          328 :             IF (.NOT. bs_env%do_gw .AND. print_ikp) THEN
    1654            0 :                CALL write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env)
    1655          328 :             ELSE IF (bs_env%do_gw .AND. print_ikp) THEN
    1656              :                CALL write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env, &
    1657          296 :                                           file_name='bandstructure_DFT_plus_SOC')
    1658              :             END IF
    1659              : 
    1660          328 :             IF (bs_env%do_ldos) THEN
    1661              :                CALL add_to_LDOS_2d(LDOS_scf_2d_SOC, qs_env, ikp, bs_env, cfm_spinor_wf_ikp, &
    1662            2 :                                    eigenval_spinor, band_edges_scf_guess, .TRUE., cfm_work_ikp)
    1663              :             END IF
    1664              : 
    1665          328 :             IF (bs_env%do_gw) THEN
    1666              : 
    1667          328 :                IF (bs_env%gw_flavour == evGW0) THEN
    1668              :                   CALL SOC_ev(bs_env, qs_env, ikp, bs_env%eigenval_G0W0, &
    1669              :                               E_min, cfm_mos_ikp, DOS_G0W0_SOC, PDOS_G0W0_SOC, &
    1670            0 :                               band_edges_G0W0_SOC, eigenval_spinor_G0W0, cfm_spinor_wf_ikp)
    1671            0 :                   eigenval_spinor_all_G0W0(:, ikp) = eigenval_spinor_G0W0
    1672            0 :                   IF (print_ikp) THEN
    1673              :                      CALL write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env, &
    1674              :                                                 eigenval_spinor_G0W0, &
    1675            0 :                                                 file_name='bandstructure_G0W0_first_cycle_plus_SOC')
    1676              :                   END IF
    1677              :                END IF
    1678              : 
    1679              :                ! Compute final GW+SOC eigenvalues, band edges, DOS and LDOS.
    1680              :                CALL SOC_ev(bs_env, qs_env, ikp, bs_env%eigenval_GW, &
    1681              :                            E_min, cfm_mos_ikp, DOS_GW_SOC, PDOS_GW_SOC, &
    1682          328 :                            band_edges_GW_SOC, eigenval_spinor_GW, cfm_spinor_wf_ikp)
    1683         8472 :                eigenval_spinor_all_gw(:, ikp) = eigenval_spinor_GW
    1684          328 :                IF (print_ikp) THEN
    1685              :                   ! Write SCF+SOC and final GW+SOC eigenvalues.
    1686              :                   CALL write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env, &
    1687          296 :                                              eigenval_spinor_GW)
    1688              :                END IF
    1689              : 
    1690              :             END IF ! do_gw
    1691              : 
    1692              :          END IF ! do_soc
    1693              : 
    1694          946 :          IF (bs_env%unit_nr > 0) THEN
    1695          394 :             IF (m_walltime() - t1 > 20.0_dp) THEN
    1696              :                WRITE (bs_env%unit_nr, '(T2,A,T43,I5,A,I3,A,F7.1,A)') &
    1697            0 :                   'Compute DOS, LDOS for k-point ', ikp, ' /', bs_env%nkp_bs_and_DOS, &
    1698            0 :                   ',    Execution time', m_walltime() - t1, ' s'
    1699              :             END IF
    1700              :          END IF
    1701              : 
    1702              :       END DO ! ikp_DOS
    1703              : 
    1704          158 :       band_edges_scf%IDBG = band_edges_scf%CBM - band_edges_scf%VBM
    1705              : 
    1706          298 :       SELECT CASE (bs_env%n_spin)
    1707              :       CASE (1)
    1708              :          CALL get_fermi_level_1c(bs_env, bs_env%eigenval_scf(:, :, 1), &
    1709              :                                  REAL(bs_env%n_electron(1), KIND=dp), &
    1710              :                                  bs_env%spin_degeneracy, band_edges_scf%VBM, &
    1711          140 :                                  band_edges_scf%CBM, band_edges_scf%E_fermi)
    1712              :       CASE (2)
    1713              :          CALL get_fermi_level_2c(bs_env, bs_env%eigenval_scf(:, :, 1:2), &
    1714              :                                  REAL(bs_env%n_electron(1) + bs_env%n_electron(2), KIND=dp), &
    1715          158 :                                  band_edges_scf%VBM, band_edges_scf%CBM, band_edges_scf%E_fermi)
    1716              :       END SELECT
    1717              : 
    1718          158 :       IF (bs_env%do_soc) THEN
    1719           22 :          band_edges_scf_SOC%IDBG = band_edges_scf_SOC%CBM - band_edges_scf_SOC%VBM
    1720              :          ! SOC merges both spins into 2*n_ao spinor states, each holding at most one
    1721              :          ! electron (maxocc=1); homo_spinor = n_occ(1)+n_occ(n_spin) is the total spinor
    1722              :          ! electron count, same quantity SOC_ev uses to locate the occupied/unoccupied edge
    1723              :          CALL get_fermi_level_1c(bs_env, eigenval_spinor_all_scf, &
    1724              :                                  n_el_spinor, 1.0_dp, &
    1725           22 :                                  band_edges_scf_SOC%VBM, band_edges_scf_SOC%CBM, band_edges_scf_SOC%E_fermi)
    1726              : 
    1727           22 :          IF (bs_env%do_gw) THEN
    1728           22 :             band_edges_GW_SOC%IDBG = band_edges_GW_SOC%CBM - band_edges_GW_SOC%VBM
    1729              :             CALL get_fermi_level_1c(bs_env, eigenval_spinor_all_gw, &
    1730              :                                     n_el_spinor, 1.0_dp, &
    1731           22 :                                     band_edges_GW_SOC%VBM, band_edges_GW_SOC%CBM, band_edges_GW_SOC%E_fermi)
    1732           22 :             IF (bs_env%gw_flavour == evGW0) THEN
    1733            0 :                band_edges_G0W0_SOC%IDBG = band_edges_G0W0_SOC%CBM - band_edges_G0W0_SOC%VBM
    1734              :                CALL get_fermi_level_1c(bs_env, eigenval_spinor_all_G0W0, &
    1735              :                                        n_el_spinor, 1.0_dp, band_edges_G0W0_SOC%VBM, &
    1736            0 :                                        band_edges_G0W0_SOC%CBM, band_edges_G0W0_SOC%E_fermi)
    1737              :             END IF
    1738              : 
    1739              :          END IF
    1740              :       END IF
    1741              : 
    1742          158 :       CALL write_band_edges(band_edges_scf, "SCF", bs_env)
    1743          158 :       IF (bs_env%do_dos_pdos) THEN
    1744           28 :          CALL write_dos_pdos(DOS_scf, PDOS_scf, bs_env, qs_env, "SCF", E_min, band_edges_scf%VBM)
    1745              :       END IF
    1746          158 :       IF (bs_env%do_ldos) THEN
    1747            2 :          CALL print_LDOS_main(LDOS_scf_2d, bs_env, band_edges_scf, "SCF")
    1748              :       END IF
    1749              : 
    1750          158 :       IF (bs_env%do_soc) THEN
    1751           22 :          CALL write_band_edges(band_edges_scf_SOC, "SCF+SOC", bs_env)
    1752           22 :          IF (bs_env%do_dos_pdos) THEN
    1753              :             CALL write_dos_pdos(DOS_scf_SOC, PDOS_scf_SOC, bs_env, qs_env, "SCF_SOC", &
    1754           16 :                                 E_min, band_edges_scf_SOC%VBM)
    1755              :          END IF
    1756           22 :          IF (bs_env%do_ldos) THEN
    1757              :             ! argument band_edges_scf is actually correct because the non-SOC band edges
    1758              :             ! have been used as reference in add_to_LDOS_2d
    1759              :             CALL print_LDOS_main(LDOS_scf_2d_SOC, bs_env, band_edges_scf, &
    1760            2 :                                  "SCF_SOC")
    1761              :          END IF
    1762              :       END IF
    1763              : 
    1764              :       ! the printed band edges carry the GW flavour that was actually run; the labels handed to
    1765              :       ! write_dos_pdos / print_LDOS_main below become file names and stay as they are
    1766          158 :       gw_soc_label = TRIM(gw_label)//"+SOC"
    1767              : 
    1768          158 :       IF (bs_env%do_gw) THEN
    1769          156 :          CALL write_band_edges(band_edges_GW, TRIM(gw_label), bs_env)
    1770          156 :          CALL write_band_edges(bs_env%band_edges_HF, "Hartree-Fock with SCF orbitals", bs_env)
    1771          156 :          IF (bs_env%do_dos_pdos) THEN
    1772              :             CALL write_dos_pdos(DOS_GW, PDOS_GW, bs_env, qs_env, "G0W0", E_min, &
    1773           28 :                                 band_edges_GW%VBM)
    1774              :          END IF
    1775          156 :          IF (bs_env%do_ldos) THEN
    1776            2 :             CALL print_LDOS_main(LDOS_GW_2d, bs_env, band_edges_GW, "G0W0")
    1777              :          END IF
    1778              :       END IF
    1779              : 
    1780          158 :       IF (bs_env%do_soc .AND. bs_env%do_gw) THEN
    1781           22 :          IF (bs_env%gw_flavour == evGW0) THEN
    1782            0 :             CALL write_band_edges(band_edges_G0W0_SOC, 'G0W0+SOC', bs_env)
    1783            0 :             IF (bs_env%do_dos_pdos) THEN
    1784              :                CALL write_dos_pdos(DOS_G0W0_SOC, PDOS_G0W0_SOC, bs_env, qs_env, &
    1785            0 :                                    'G0W0_SOC', E_min, band_edges_G0W0_SOC%VBM)
    1786              :             END IF
    1787              :          END IF
    1788           22 :          CALL write_band_edges(band_edges_GW_SOC, TRIM(gw_soc_label), bs_env)
    1789           22 :          IF (bs_env%do_dos_pdos) THEN
    1790              :             CALL write_dos_pdos(DOS_GW_SOC, PDOS_GW_SOC, bs_env, qs_env, &
    1791              :                                 TRIM(gw_label)//"_SOC", E_min, &
    1792           16 :                                 band_edges_GW_SOC%VBM)
    1793              :          END IF
    1794              :       END IF
    1795              : 
    1796          158 :       CALL cp_cfm_release(cfm_s_ikp)
    1797          158 :       CALL cp_cfm_release(cfm_ks_ikp)
    1798          158 :       CALL cp_cfm_release(cfm_mos_ikp(1))
    1799          158 :       CALL cp_cfm_release(cfm_mos_ikp(2))
    1800          158 :       CALL cp_cfm_release(cfm_work_ikp)
    1801          158 :       CALL cp_cfm_release(cfm_s_ikp_copy)
    1802              : 
    1803          158 :       CALL cp_cfm_release(cfm_s_ikp_spinor)
    1804          158 :       CALL cp_cfm_release(cfm_ks_ikp_spinor)
    1805          158 :       CALL cp_cfm_release(cfm_SOC_ikp_spinor)
    1806          158 :       CALL cp_cfm_release(cfm_mos_ikp_spinor)
    1807          158 :       CALL cp_cfm_release(cfm_work_ikp_spinor)
    1808          158 :       CALL cp_cfm_release(cfm_s_ikp_spinor_copy)
    1809          158 :       CALL cp_cfm_release(cfm_spinor_wf_ikp)
    1810              : 
    1811          158 :       CALL timestop(handle)
    1812              : 
    1813          632 :    END SUBROUTINE eval_bandstructure_properties
    1814              : 
    1815              : ! **************************************************************************************************
    1816              : !> \brief ...
    1817              : !> \param LDOS_2d ...
    1818              : !> \param bs_env ...
    1819              : !> \param band_edges ...
    1820              : !> \param scf_gw_soc ...
    1821              : ! **************************************************************************************************
    1822            6 :    SUBROUTINE print_LDOS_main(LDOS_2d, bs_env, band_edges, scf_gw_soc)
    1823              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d
    1824              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1825              :       TYPE(band_edges_type)                              :: band_edges
    1826              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    1827              : 
    1828              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'print_LDOS_main'
    1829              : 
    1830              :       INTEGER :: handle, i_x, i_x_bin, i_x_end, i_x_end_bin, i_x_end_glob, i_x_start, &
    1831              :          i_x_start_bin, i_x_start_glob, i_y, i_y_bin, i_y_end, i_y_end_bin, i_y_end_glob, &
    1832              :          i_y_start, i_y_start_bin, i_y_start_glob, n_E
    1833            6 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: n_sum_for_bins
    1834              :       INTEGER, DIMENSION(2)                              :: bin_mesh
    1835              :       LOGICAL                                            :: do_xy_bins
    1836              :       REAL(KIND=dp)                                      :: E_min, energy_step, energy_window
    1837              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d_bins
    1838              : 
    1839            6 :       CALL timeset(routineN, handle)
    1840              : 
    1841            6 :       n_E = SIZE(LDOS_2d, 3)
    1842              : 
    1843            6 :       energy_window = bs_env%energy_window_DOS
    1844            6 :       energy_step = bs_env%energy_step_DOS
    1845            6 :       E_min = band_edges%VBM - 0.5_dp*energy_window
    1846              : 
    1847           18 :       bin_mesh(1:2) = bs_env%bin_mesh(1:2)
    1848            6 :       do_xy_bins = (bin_mesh(1) > 0 .AND. bin_mesh(2) > 0)
    1849              : 
    1850            6 :       i_x_start = LBOUND(LDOS_2d, 1)
    1851            6 :       i_x_end = UBOUND(LDOS_2d, 1)
    1852            6 :       i_y_start = LBOUND(LDOS_2d, 2)
    1853            6 :       i_y_end = UBOUND(LDOS_2d, 2)
    1854              : 
    1855            6 :       IF (do_xy_bins) THEN
    1856            6 :          i_x_start_bin = 1
    1857            6 :          i_x_end_bin = bin_mesh(1)
    1858            6 :          i_y_start_bin = 1
    1859            6 :          i_y_end_bin = bin_mesh(2)
    1860              :       ELSE
    1861              :          i_x_start_bin = i_x_start
    1862              :          i_x_end_bin = i_x_end
    1863              :          i_y_start_bin = i_y_start
    1864              :          i_y_end_bin = i_y_end
    1865              :       END IF
    1866              : 
    1867           30 :       ALLOCATE (LDOS_2d_bins(i_x_start_bin:i_x_end_bin, i_y_start_bin:i_y_end_bin, n_E))
    1868            6 :       LDOS_2d_bins(:, :, :) = 0.0_dp
    1869              : 
    1870            6 :       IF (do_xy_bins) THEN
    1871              : 
    1872            6 :          i_x_start_glob = i_x_start
    1873            6 :          i_x_end_glob = i_x_end
    1874            6 :          i_y_start_glob = i_y_start
    1875            6 :          i_y_end_glob = i_y_end
    1876              : 
    1877            6 :          CALL bs_env%para_env%min(i_x_start_glob)
    1878            6 :          CALL bs_env%para_env%max(i_x_end_glob)
    1879            6 :          CALL bs_env%para_env%min(i_y_start_glob)
    1880            6 :          CALL bs_env%para_env%max(i_y_end_glob)
    1881              : 
    1882           24 :          ALLOCATE (n_sum_for_bins(bin_mesh(1), bin_mesh(2)), SOURCE=0)
    1883              : 
    1884              :          ! transform interval [i_x_start, i_x_end] to [1, bin_mesh(1)] (and same for y)
    1885          390 :          DO i_y = i_y_start, i_y_end
    1886         4230 :             DO i_x = i_x_start, i_x_end
    1887         3840 :                i_x_bin = bin_mesh(1)*(i_x - i_x_start_glob)/(i_x_end_glob - i_x_start_glob + 1) + 1
    1888         3840 :                i_y_bin = bin_mesh(2)*(i_y - i_y_start_glob)/(i_y_end_glob - i_y_start_glob + 1) + 1
    1889              :                LDOS_2d_bins(i_x_bin, i_y_bin, :) = LDOS_2d_bins(i_x_bin, i_y_bin, :) + &
    1890      1073920 :                                                    LDOS_2d(i_x, i_y, :)
    1891         4224 :                n_sum_for_bins(i_x_bin, i_y_bin) = n_sum_for_bins(i_x_bin, i_y_bin) + 1
    1892              :             END DO
    1893              :          END DO
    1894              : 
    1895            6 :          CALL bs_env%para_env%sum(LDOS_2d_bins)
    1896            6 :          CALL bs_env%para_env%sum(n_sum_for_bins)
    1897              : 
    1898              :          ! divide by number of terms in the sum so we have the average LDOS(x,y,E)
    1899           30 :          DO i_y_bin = 1, bin_mesh(2)
    1900          126 :             DO i_x_bin = 1, bin_mesh(1)
    1901              :                LDOS_2d_bins(i_x_bin, i_y_bin, :) = LDOS_2d_bins(i_x_bin, i_y_bin, :)/ &
    1902        26872 :                                                    REAL(n_sum_for_bins(i_x_bin, i_y_bin), KIND=dp)
    1903              :             END DO
    1904              :          END DO
    1905              : 
    1906              :       ELSE
    1907              : 
    1908            0 :          LDOS_2d_bins(:, :, :) = LDOS_2d(:, :, :)
    1909              : 
    1910              :       END IF
    1911              : 
    1912            6 :       IF (bin_mesh(1)*bin_mesh(2) < bs_env%n_bins_max_for_printing) THEN
    1913            6 :          CALL print_LDOS_2d_bins(LDOS_2d_bins, bs_env, E_min, scf_gw_soc)
    1914              :       ELSE
    1915            0 :          CPWARN("The number of bins for the LDOS is too large. Decrease BIN_MESH.")
    1916              :       END IF
    1917              : 
    1918            6 :       CALL timestop(handle)
    1919              : 
    1920           12 :    END SUBROUTINE print_LDOS_main
    1921              : 
    1922              : ! **************************************************************************************************
    1923              : !> \brief ...
    1924              : !> \param LDOS_2d_bins ...
    1925              : !> \param bs_env ...
    1926              : !> \param E_min ...
    1927              : !> \param scf_gw_soc ...
    1928              : ! **************************************************************************************************
    1929            6 :    SUBROUTINE print_LDOS_2d_bins(LDOS_2d_bins, bs_env, E_min, scf_gw_soc)
    1930              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d_bins
    1931              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1932              :       REAL(KIND=dp)                                      :: E_min
    1933              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    1934              : 
    1935              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'print_LDOS_2d_bins'
    1936              : 
    1937              :       CHARACTER(LEN=18)                                  :: print_format
    1938              :       CHARACTER(LEN=4)                                   :: print_format_1, print_format_2
    1939              :       CHARACTER(len=default_string_length)               :: fname
    1940              :       INTEGER                                            :: handle, i_E, i_x, i_x_end, i_x_start, &
    1941              :                                                             i_y, i_y_end, i_y_start, iunit, n_E, &
    1942              :                                                             n_x, n_y
    1943              :       REAL(KIND=dp)                                      :: energy
    1944              :       REAL(KIND=dp), DIMENSION(3)                        :: coord, idx
    1945              : 
    1946            6 :       CALL timeset(routineN, handle)
    1947              : 
    1948            6 :       i_x_start = LBOUND(LDOS_2d_bins, 1)
    1949            6 :       i_x_end = UBOUND(LDOS_2d_bins, 1)
    1950            6 :       i_y_start = LBOUND(LDOS_2d_bins, 2)
    1951            6 :       i_y_end = UBOUND(LDOS_2d_bins, 2)
    1952            6 :       n_E = SIZE(LDOS_2d_bins, 3)
    1953              : 
    1954            6 :       n_x = i_x_end - i_x_start + 1
    1955            6 :       n_y = i_y_end - i_y_start + 1
    1956              : 
    1957            6 :       IF (bs_env%para_env%is_source()) THEN
    1958              : 
    1959           15 :          DO i_y = i_y_start, i_y_end
    1960           63 :             DO i_x = i_x_start, i_x_end
    1961              : 
    1962           48 :                idx(1) = (REAL(i_x, KIND=dp) - 0.5_dp)/REAL(n_x, KIND=dp)
    1963           48 :                idx(2) = (REAL(i_y, KIND=dp) - 0.5_dp)/REAL(n_y, KIND=dp)
    1964           48 :                idx(3) = 0.0_dp
    1965          624 :                coord(1:3) = MATMUL(bs_env%hmat, idx)
    1966              : 
    1967           48 :                CALL get_print_format(coord(1), print_format_1)
    1968           48 :                CALL get_print_format(coord(2), print_format_2)
    1969              : 
    1970           48 :                print_format = "(3A,"//print_format_1//",A,"//print_format_2//",A)"
    1971              : 
    1972           48 :                WRITE (fname, print_format) "LDOS_", scf_gw_soc, &
    1973           96 :                   "_at_x_", coord(1)*angstrom, '_A_and_y_', coord(2)*angstrom, '_A'
    1974              : 
    1975              :                CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", &
    1976           48 :                               file_action="WRITE")
    1977              : 
    1978           48 :                WRITE (iunit, "(2A)") "        Energy E (eV)    average LDOS(x,y,E) (1/(eV*Å^2), ", &
    1979           96 :                   "integrated over z, averaged inside bin)"
    1980              : 
    1981        13424 :                DO i_E = 1, n_E
    1982        13376 :                   energy = E_min + i_E*bs_env%energy_step_DOS
    1983        13376 :                   WRITE (iunit, "(2F17.3)") energy*evolt, &
    1984              :                      LDOS_2d_bins(i_x, i_y, i_E)* &
    1985        26800 :                      bs_env%unit_ldos_int_z_inv_Ang2_eV
    1986              :                END DO
    1987              : 
    1988           60 :                CALL close_file(iunit)
    1989              : 
    1990              :             END DO
    1991              :          END DO
    1992              : 
    1993              :       END IF
    1994              : 
    1995            6 :       CALL timestop(handle)
    1996              : 
    1997            6 :    END SUBROUTINE print_LDOS_2d_bins
    1998              : 
    1999              : ! **************************************************************************************************
    2000              : !> \brief ...
    2001              : !> \param coord ...
    2002              : !> \param print_format ...
    2003              : ! **************************************************************************************************
    2004           96 :    SUBROUTINE get_print_format(coord, print_format)
    2005              :       REAL(KIND=dp)                                      :: coord
    2006              :       CHARACTER(LEN=4)                                   :: print_format
    2007              : 
    2008              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_print_format'
    2009              : 
    2010              :       INTEGER                                            :: handle
    2011              : 
    2012           96 :       CALL timeset(routineN, handle)
    2013              : 
    2014           96 :       IF (coord < -10000/angstrom) THEN
    2015            0 :          print_format = "F9.2"
    2016           96 :       ELSE IF (coord < -1000/angstrom) THEN
    2017            0 :          print_format = "F8.2"
    2018           96 :       ELSE IF (coord < -100/angstrom) THEN
    2019            0 :          print_format = "F7.2"
    2020           96 :       ELSE IF (coord < -10/angstrom) THEN
    2021            0 :          print_format = "F6.2"
    2022           96 :       ELSE IF (coord < -1/angstrom) THEN
    2023            0 :          print_format = "F5.2"
    2024           96 :       ELSE IF (coord < 10/angstrom) THEN
    2025           96 :          print_format = "F4.2"
    2026            0 :       ELSE IF (coord < 100/angstrom) THEN
    2027            0 :          print_format = "F5.2"
    2028            0 :       ELSE IF (coord < 1000/angstrom) THEN
    2029            0 :          print_format = "F6.2"
    2030            0 :       ELSE IF (coord < 10000/angstrom) THEN
    2031            0 :          print_format = "F7.2"
    2032              :       ELSE
    2033            0 :          print_format = "F8.2"
    2034              :       END IF
    2035              : 
    2036           96 :       CALL timestop(handle)
    2037              : 
    2038           96 :    END SUBROUTINE get_print_format
    2039              : 
    2040              : ! **************************************************************************************************
    2041              : !> \brief ...
    2042              : !> \param bs_env ...
    2043              : !> \param qs_env ...
    2044              : !> \param ikp ...
    2045              : !> \param eigenval_no_SOC non-SOC band energies (n_ao, n_kpoints, n_spin)
    2046              : !> \param E_min ...
    2047              : !> \param cfm_mos_ikp ...
    2048              : !> \param DOS ...
    2049              : !> \param PDOS ...
    2050              : !> \param band_edges ...
    2051              : !> \param eigenval_spinor ...
    2052              : !> \param cfm_spinor_wf_ikp ...
    2053              : ! **************************************************************************************************
    2054          656 :    SUBROUTINE SOC_ev(bs_env, qs_env, ikp, eigenval_no_SOC, E_min, cfm_mos_ikp, &
    2055              :                      DOS, PDOS, band_edges, eigenval_spinor, cfm_spinor_wf_ikp)
    2056              : 
    2057              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2058              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2059              :       INTEGER                                            :: ikp
    2060              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: eigenval_no_SOC
    2061              :       REAL(KIND=dp)                                      :: E_min
    2062              :       TYPE(cp_cfm_type), DIMENSION(2)                    :: cfm_mos_ikp
    2063              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: DOS
    2064              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    2065              :       TYPE(band_edges_type)                              :: band_edges
    2066              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor
    2067              :       TYPE(cp_cfm_type)                                  :: cfm_spinor_wf_ikp
    2068              : 
    2069              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'SOC_ev'
    2070              : 
    2071              :       INTEGER                                            :: handle
    2072              :       LOGICAL                                            :: do_truncate
    2073              : 
    2074          656 :       CALL timeset(routineN, handle)
    2075              : 
    2076              :       !for the window truncation part
    2077          656 :       do_truncate = (bs_env%soc_nbands_occ > 0 .AND. bs_env%soc_nbands_virt > 0)
    2078              : 
    2079              :       IF (do_truncate) THEN
    2080              :          CALL SOC_ev_truncate_mos(bs_env, qs_env, ikp, eigenval_no_SOC, cfm_mos_ikp, &
    2081            0 :                                   band_edges, eigenval_spinor, cfm_spinor_wf_ikp)
    2082              :       ELSE
    2083              :          CALL SOC_ev_all_mos(bs_env, qs_env, ikp, eigenval_no_SOC, E_min, cfm_mos_ikp, &
    2084          656 :                              DOS, PDOS, band_edges, eigenval_spinor, cfm_spinor_wf_ikp)
    2085              :       END IF
    2086              : 
    2087          656 :       CALL timestop(handle)
    2088              : 
    2089          656 :    END SUBROUTINE SOC_ev
    2090              : 
    2091              : ! **************************************************************************************************
    2092              : !> \brief SOC second-variational diagonalization over the full 2*n_ao spinor space (no truncation
    2093              : !>        window) -- the SOC_NBANDS_OCC/VIRT-truncated path lives in SOC_ev_truncate_mos.
    2094              : !> \param bs_env ...
    2095              : !> \param qs_env ...
    2096              : !> \param ikp ...
    2097              : !> \param eigenval_no_SOC non-SOC band energies (n_ao, n_kpoints, n_spin)
    2098              : !> \param E_min ...
    2099              : !> \param cfm_mos_ikp ...
    2100              : !> \param DOS ...
    2101              : !> \param PDOS ...
    2102              : !> \param band_edges ...
    2103              : !> \param eigenval_spinor ...
    2104              : !> \param cfm_spinor_wf_ikp ...
    2105              : ! **************************************************************************************************
    2106          656 :    SUBROUTINE SOC_ev_all_mos(bs_env, qs_env, ikp, eigenval_no_SOC, E_min, cfm_mos_ikp, &
    2107              :                              DOS, PDOS, band_edges, eigenval_spinor, cfm_spinor_wf_ikp)
    2108              : 
    2109              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2110              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2111              :       INTEGER                                            :: ikp
    2112              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: eigenval_no_SOC
    2113              :       REAL(KIND=dp)                                      :: E_min
    2114              :       TYPE(cp_cfm_type), DIMENSION(2)                    :: cfm_mos_ikp
    2115              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: DOS
    2116              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    2117              :       TYPE(band_edges_type)                              :: band_edges
    2118              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor
    2119              :       TYPE(cp_cfm_type)                                  :: cfm_spinor_wf_ikp
    2120              : 
    2121              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'SOC_ev_all_mos'
    2122              : 
    2123              :       INTEGER                                            :: handle, homo_spinor, n_ao, n_E, nkind
    2124              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor_no_SOC
    2125              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind_spinor
    2126              :       TYPE(cp_cfm_type)                                  :: cfm_eigenvec_ikp_spinor, &
    2127              :                                                             cfm_ks_ikp_spinor, cfm_mos_ikp_spinor, &
    2128              :                                                             cfm_SOC_ikp_spinor, cfm_work_ikp_spinor
    2129              : 
    2130          656 :       CALL timeset(routineN, handle)
    2131              : 
    2132          656 :       n_ao = bs_env%n_ao
    2133          656 :       homo_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    2134          656 :       CALL get_qs_env(qs_env, nkind=nkind)
    2135              : 
    2136          656 :       CALL cp_cfm_create(cfm_ks_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2137          656 :       CALL cp_cfm_create(cfm_SOC_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2138          656 :       CALL cp_cfm_create(cfm_mos_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2139          656 :       CALL cp_cfm_create(cfm_work_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2140          656 :       CALL cp_cfm_create(cfm_eigenvec_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2141              : 
    2142         1968 :       ALLOCATE (eigenval_spinor_no_SOC(2*n_ao))
    2143         2624 :       ALLOCATE (proj_mo_on_kind_spinor(2*n_ao, nkind))
    2144              :       ! PDOS not yet implemented -> projection is just zero -> PDOS is zero
    2145          656 :       proj_mo_on_kind_spinor(:, :) = 0.0_dp
    2146              : 
    2147              :       ! 1. get V^SOC_µν,σσ'(k_i)
    2148          676 :       SELECT CASE (bs_env%gw_implementation)
    2149              :       CASE (tensor_large_cell_Gamma, ri_rs_large_cell_Gamma, ri_rs_non_periodic, tensor_static_cohsex)
    2150              : 
    2151              :          ! 1. get V^SOC_µν,σσ'(k_i) from V^SOC_µν,σσ'(k=0)
    2152              :          CALL cfm_ikp_from_cfm_spinor_Gamma(cfm_SOC_ikp_spinor, &
    2153              :                                             bs_env%cfm_SOC_spinor_ao(1), &
    2154              :                                             bs_env%fm_s_Gamma%matrix_struct, &
    2155           20 :                                             ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    2156              : 
    2157              :       CASE (tensor_small_cell_full_kp, ri_rs_small_cell_full_kp)
    2158              : 
    2159              :          ! 1. V^SOC_µν,σσ'(k_i) already there
    2160          656 :          CALL cp_cfm_to_cfm(bs_env%cfm_SOC_spinor_ao(ikp), cfm_SOC_ikp_spinor)
    2161              : 
    2162              :       END SELECT
    2163              : 
    2164              :       ! 2. V^SOC_nn',σσ'(k_i) = sum_µν C^*_µn,σ(k_i) V^SOC_µν,σσ'(k_i) C_νn'(k_i),
    2165              :       !    C_µn,σ(k_i): MO coefficiencts from diagonalizing KS-matrix h^KS_nn',σσ'(k_i)
    2166              : 
    2167              :       ! 2.1 build matrix C_µn,σ(k_i)
    2168          656 :       CALL cp_cfm_set_all(cfm_mos_ikp_spinor, z_zero)
    2169          656 :       CALL add_cfm_submat(cfm_mos_ikp_spinor, cfm_mos_ikp(1), 1, 1)
    2170          656 :       CALL add_cfm_submat(cfm_mos_ikp_spinor, cfm_mos_ikp(bs_env%n_spin), n_ao + 1, n_ao + 1)
    2171              : 
    2172              :       ! 2.2 work_nν,σσ' = sum_µ C^*_µn,σ(k_i) V^SOC_µν,σσ'(k_i)
    2173              :       CALL parallel_gemm('C', 'N', 2*n_ao, 2*n_ao, 2*n_ao, z_one, &
    2174              :                          cfm_mos_ikp_spinor, cfm_SOC_ikp_spinor, &
    2175          656 :                          z_zero, cfm_work_ikp_spinor)
    2176              : 
    2177              :       ! 2.3 V^SOC_nn',σσ'(k_i) = sum_ν work_nν,σσ' C_νn'(k_i)
    2178              :       CALL parallel_gemm('N', 'N', 2*n_ao, 2*n_ao, 2*n_ao, z_one, &
    2179              :                          cfm_work_ikp_spinor, cfm_mos_ikp_spinor, &
    2180          656 :                          z_zero, cfm_ks_ikp_spinor)
    2181              : 
    2182              :       ! 3. remove SOC outside of energy window (otherwise, numerical problems arise
    2183              :       !    because energetically low semicore states and energetically very high
    2184              :       !    unbound states couple to the states around the Fermi level)
    2185         8800 :       eigenval_spinor_no_SOC(1:n_ao) = eigenval_no_SOC(1:n_ao, ikp, 1)
    2186         8800 :       eigenval_spinor_no_SOC(n_ao + 1:) = eigenval_no_SOC(1:n_ao, ikp, bs_env%n_spin)
    2187          656 :       IF (bs_env%soc_window_occ > 0.0_dp .OR. bs_env%soc_window_virt > 0.0_dp) THEN
    2188              :          CALL remove_soc_outside_energy_window_mo(cfm_ks_ikp_spinor, &
    2189              :                                                   bs_env%soc_window_virt, &
    2190              :                                                   bs_env%soc_window_smearing, &
    2191              :                                                   eigenval_spinor_no_SOC, &
    2192          256 :                                                   bs_env%e_fermi(1))
    2193              : 
    2194              :       END IF
    2195              : 
    2196              :       ! 4. h^G0W0+SOC_nn',σσ'(k_i) = ε_nσ^G0W0(k_i) δ_nn' δ_σσ' + V^SOC_nn',σσ'(k_i)
    2197          656 :       CALL cfm_add_on_diag(cfm_ks_ikp_spinor, eigenval_spinor_no_SOC)
    2198              : 
    2199              :       ! 5. diagonalize h^G0W0+SOC_nn',σσ'(k_i) to get eigenvalues
    2200          656 :       CALL cp_cfm_heevd(cfm_ks_ikp_spinor, cfm_eigenvec_ikp_spinor, eigenval_spinor)
    2201              : 
    2202              :       ! 6. DOS from spinors, no PDOS
    2203          656 :       IF (bs_env%do_dos_pdos) THEN
    2204          452 :          n_E = SIZE(DOS)
    2205              :          CALL add_to_DOS_PDOS(DOS, PDOS, eigenval_spinor, &
    2206          452 :                               ikp, bs_env, n_E, E_min, proj_mo_on_kind_spinor)
    2207              :       END IF
    2208              : 
    2209              :       ! 7. valence band max. (VBM), conduction band min. (CBM) and direct bandgap (DBG)
    2210          656 :       band_edges%VBM = MAX(band_edges%VBM, eigenval_spinor(homo_spinor))
    2211          656 :       band_edges%CBM = MIN(band_edges%CBM, eigenval_spinor(homo_spinor + 1))
    2212              :       band_edges%DBG = MIN(band_edges%DBG, eigenval_spinor(homo_spinor + 1) &
    2213          656 :                            - eigenval_spinor(homo_spinor))
    2214              : 
    2215              :       ! Only LDOS consumes the AO spinor wavefunctions; band energies and DOS do not.
    2216          656 :       CALL cp_cfm_set_all(cfm_spinor_wf_ikp, z_zero)
    2217          656 :       IF (bs_env%do_ldos) THEN
    2218              :          CALL parallel_gemm('N', 'N', 2*n_ao, 2*n_ao, 2*n_ao, z_one, &
    2219              :                             cfm_mos_ikp_spinor, cfm_eigenvec_ikp_spinor, &
    2220            4 :                             z_zero, cfm_spinor_wf_ikp)
    2221              :       END IF
    2222              : 
    2223          656 :       CALL cp_cfm_release(cfm_ks_ikp_spinor)
    2224          656 :       CALL cp_cfm_release(cfm_SOC_ikp_spinor)
    2225          656 :       CALL cp_cfm_release(cfm_work_ikp_spinor)
    2226          656 :       CALL cp_cfm_release(cfm_eigenvec_ikp_spinor)
    2227          656 :       CALL cp_cfm_release(cfm_mos_ikp_spinor)
    2228              : 
    2229          656 :       CALL timestop(handle)
    2230              : 
    2231         1968 :    END SUBROUTINE SOC_ev_all_mos
    2232              : 
    2233              : ! **************************************************************************************************
    2234              : !> \brief SOC second-variational diagonalization truncated to a window of SOC_NBANDS_OCC occupied
    2235              : !>        and SOC_NBANDS_VIRT virtual states around the Fermi level -- the untruncated, full
    2236              : !>        2*n_ao spinor-space path lives in SOC_ev_all_mos.
    2237              : !> \param bs_env ...
    2238              : !> \param qs_env ...
    2239              : !> \param ikp ...
    2240              : !> \param eigenval_no_SOC non-SOC band energies (n_ao, n_kpoints, n_spin)
    2241              : !> \param cfm_mos_ikp ...
    2242              : !> \param band_edges ...
    2243              : !> \param eigenval_spinor ...
    2244              : !> \param cfm_spinor_wf_ikp ...
    2245              : ! **************************************************************************************************
    2246            0 :    SUBROUTINE SOC_ev_truncate_mos(bs_env, qs_env, ikp, eigenval_no_SOC, cfm_mos_ikp, &
    2247              :                                   band_edges, eigenval_spinor, cfm_spinor_wf_ikp)
    2248              : 
    2249              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2250              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2251              :       INTEGER                                            :: ikp
    2252              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: eigenval_no_SOC
    2253              :       TYPE(cp_cfm_type), DIMENSION(2)                    :: cfm_mos_ikp
    2254              :       TYPE(band_edges_type)                              :: band_edges
    2255              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor
    2256              :       TYPE(cp_cfm_type)                                  :: cfm_spinor_wf_ikp
    2257              : 
    2258              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'SOC_ev_truncate_mos'
    2259              : 
    2260              :       INTEGER                                            :: handle, homo_spinor, homo_used, &
    2261              :                                                             mo_index_start_1, mo_index_start_2, &
    2262              :                                                             n_ao, n_window, n_window2tot, nkind, &
    2263              :                                                             nocc, nvirt
    2264            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eig_win_sorted, eigenval_spinor_no_SOC
    2265            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind_spinor
    2266              :       TYPE(cp_cfm_type)                                  :: cfm_eigenvec_ikp_spinor, &
    2267              :                                                             cfm_ks_ikp_spinor, cfm_mos_ikp_spinor, &
    2268              :                                                             cfm_SOC_ikp_spinor, cfm_work_ikp_spinor
    2269              :       TYPE(cp_fm_struct_type), POINTER                   :: fst_mos, fst_wa, fst_ww
    2270              :       TYPE(cp_fm_type)                                   :: fm_src_im, fm_src_re, fm_tgt_im, &
    2271              :                                                             fm_tgt_re
    2272              : 
    2273            0 :       CALL timeset(routineN, handle)
    2274              : 
    2275            0 :       n_ao = bs_env%n_ao
    2276            0 :       homo_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    2277            0 :       CALL get_qs_env(qs_env, nkind=nkind)
    2278              : 
    2279            0 :       nocc = bs_env%soc_nbands_occ
    2280            0 :       nvirt = bs_env%soc_nbands_virt
    2281            0 :       CPASSERT(nocc <= bs_env%n_occ_nosmear(1))
    2282            0 :       CPASSERT(nocc <= bs_env%n_occ_nosmear(bs_env%n_spin))
    2283            0 :       CPASSERT(nvirt <= n_ao - bs_env%n_occ_nosmear(1))
    2284            0 :       CPASSERT(nvirt <= n_ao - bs_env%n_occ_nosmear(bs_env%n_spin))
    2285            0 :       CPASSERT(.NOT. bs_env%do_dos_pdos)
    2286            0 :       mo_index_start_1 = bs_env%n_occ_nosmear(1) - nocc + 1
    2287            0 :       mo_index_start_2 = bs_env%n_occ_nosmear(bs_env%n_spin) - nocc + 1
    2288            0 :       n_window = nocc + nvirt
    2289            0 :       n_window2tot = 2*n_window
    2290            0 :       homo_used = 2*nocc
    2291              : 
    2292            0 :       NULLIFY (fst_ww, fst_wa, fst_mos)
    2293              :       CALL cp_fm_struct_create(fst_ww, nrow_global=n_window2tot, ncol_global=n_window2tot, &
    2294            0 :                                template_fmstruct=bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2295              :       CALL cp_fm_struct_create(fst_wa, nrow_global=n_window2tot, ncol_global=2*n_ao, &
    2296            0 :                                template_fmstruct=bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2297              :       CALL cp_fm_struct_create(fst_mos, nrow_global=2*n_ao, ncol_global=n_window2tot, &
    2298            0 :                                template_fmstruct=bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2299            0 :       CALL cp_cfm_create(cfm_ks_ikp_spinor, fst_ww)
    2300            0 :       CALL cp_cfm_create(cfm_eigenvec_ikp_spinor, fst_ww)
    2301            0 :       CALL cp_cfm_create(cfm_work_ikp_spinor, fst_wa)
    2302            0 :       CALL cp_cfm_create(cfm_SOC_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    2303            0 :       CALL cp_cfm_create(cfm_mos_ikp_spinor, fst_mos)
    2304              : 
    2305            0 :       ALLOCATE (eigenval_spinor_no_SOC(2*n_ao))
    2306            0 :       ALLOCATE (proj_mo_on_kind_spinor(2*n_ao, nkind))
    2307              :       ! PDOS not yet implemented -> projection is just zero -> PDOS is zero
    2308              :       proj_mo_on_kind_spinor(:, :) = 0.0_dp
    2309              : 
    2310              :       ! 1. get V^SOC_µν,σσ'(k_i)
    2311            0 :       SELECT CASE (bs_env%gw_implementation)
    2312              :       CASE (tensor_large_cell_Gamma, ri_rs_large_cell_Gamma, ri_rs_non_periodic, tensor_static_cohsex)
    2313              : 
    2314              :          ! 1. get V^SOC_µν,σσ'(k_i) from V^SOC_µν,σσ'(k=0)
    2315              :          CALL cfm_ikp_from_cfm_spinor_Gamma(cfm_SOC_ikp_spinor, &
    2316              :                                             bs_env%cfm_SOC_spinor_ao(1), &
    2317              :                                             bs_env%fm_s_Gamma%matrix_struct, &
    2318            0 :                                             ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    2319              : 
    2320              :       CASE (tensor_small_cell_full_kp, ri_rs_small_cell_full_kp)
    2321              : 
    2322              :          ! 1. V^SOC_µν,σσ'(k_i) already there
    2323            0 :          CALL cp_cfm_to_cfm(bs_env%cfm_SOC_spinor_ao(ikp), cfm_SOC_ikp_spinor)
    2324              : 
    2325              :       END SELECT
    2326              : 
    2327              :       ! 2. V^SOC_nn',σσ'(k_i) = sum_µν C^*_µn,σ(k_i) V^SOC_µν,σσ'(k_i) C_νn'(k_i),
    2328              :       !    C_µn,σ(k_i): MO coefficiencts from diagonalizing KS-matrix h^KS_nn',σσ'(k_i)
    2329              : 
    2330              :       ! 2.1 build matrix C_µn,σ(k_i)
    2331            0 :       CALL cp_cfm_set_all(cfm_mos_ikp_spinor, z_zero)
    2332              :       ! get_cfm_submat/add_cfm_submat only copy SQUARE blocks, so the rectangular
    2333              :       ! (n_ao x n_window) column windows are copied with cp_fm_to_fm_submat (separate
    2334              :       ! nrow/ncol) on the real and imaginary parts separately, then recombined.
    2335            0 :       CALL cp_fm_create(fm_src_re, cfm_mos_ikp(1)%matrix_struct)
    2336            0 :       CALL cp_fm_create(fm_src_im, cfm_mos_ikp(1)%matrix_struct)
    2337            0 :       CALL cp_fm_create(fm_tgt_re, fst_mos)
    2338            0 :       CALL cp_fm_create(fm_tgt_im, fst_mos)
    2339            0 :       CALL cp_fm_set_all(fm_tgt_re, 0.0_dp)
    2340            0 :       CALL cp_fm_set_all(fm_tgt_im, 0.0_dp)
    2341              :       ! spin 1: MO columns mo_index_start_1..mo_index_start_1+n_window-1 -> rows 1..n_ao, cols 1..n_window
    2342            0 :       CALL cp_cfm_to_fm(cfm_mos_ikp(1), fm_src_re, fm_src_im)
    2343              :       CALL cp_fm_to_fm_submat(msource=fm_src_re, mtarget=fm_tgt_re, nrow=n_ao, ncol=n_window, &
    2344            0 :                               s_firstrow=1, s_firstcol=mo_index_start_1, t_firstrow=1, t_firstcol=1)
    2345              :       CALL cp_fm_to_fm_submat(msource=fm_src_im, mtarget=fm_tgt_im, nrow=n_ao, ncol=n_window, &
    2346            0 :                               s_firstrow=1, s_firstcol=mo_index_start_1, t_firstrow=1, t_firstcol=1)
    2347              : 
    2348              :       ! spin 2: MO columns mo_index_start_2..mo_index_start_2+n_window-1 -> rows n_ao+1..2*n_ao, cols n_window+1..2*n_window
    2349            0 :       CALL cp_cfm_to_fm(cfm_mos_ikp(bs_env%n_spin), fm_src_re, fm_src_im)
    2350              :       CALL cp_fm_to_fm_submat(msource=fm_src_re, mtarget=fm_tgt_re, nrow=n_ao, ncol=n_window, &
    2351            0 :                               s_firstrow=1, s_firstcol=mo_index_start_2, t_firstrow=n_ao + 1, t_firstcol=n_window + 1)
    2352              :       CALL cp_fm_to_fm_submat(msource=fm_src_im, mtarget=fm_tgt_im, nrow=n_ao, ncol=n_window, &
    2353            0 :                               s_firstrow=1, s_firstcol=mo_index_start_2, t_firstrow=n_ao + 1, t_firstcol=n_window + 1)
    2354              : 
    2355            0 :       CALL cp_fm_to_cfm(fm_tgt_re, fm_tgt_im, cfm_mos_ikp_spinor)
    2356              : 
    2357            0 :       CALL cp_fm_release(fm_src_re)
    2358            0 :       CALL cp_fm_release(fm_src_im)
    2359            0 :       CALL cp_fm_release(fm_tgt_re)
    2360            0 :       CALL cp_fm_release(fm_tgt_im)
    2361              : 
    2362              :       ! 2.2 work_nν,σσ' = sum_µ C^*_µn,σ(k_i) V^SOC_µν,σσ'(k_i)
    2363              :       CALL parallel_gemm('C', 'N', n_window2tot, 2*n_ao, 2*n_ao, z_one, &
    2364              :                          cfm_mos_ikp_spinor, cfm_SOC_ikp_spinor, &
    2365            0 :                          z_zero, cfm_work_ikp_spinor)
    2366              :       ! 2.3 V^SOC_nn',σσ'(k_i) = sum_ν work_nν,σσ' C_νn'(k_i)
    2367              :       CALL parallel_gemm('N', 'N', n_window2tot, n_window2tot, 2*n_ao, z_one, &
    2368              :                          cfm_work_ikp_spinor, cfm_mos_ikp_spinor, &
    2369            0 :                          z_zero, cfm_ks_ikp_spinor)
    2370              : 
    2371              :       ! 3. remove SOC outside of energy window: not applicable here -- the truncation window
    2372              :       !    itself already restricts SOC to the states around the Fermi level
    2373            0 :       eigenval_spinor_no_SOC(1:n_ao) = eigenval_no_SOC(1:n_ao, ikp, 1)
    2374            0 :       eigenval_spinor_no_SOC(n_ao + 1:) = eigenval_no_SOC(1:n_ao, ikp, bs_env%n_spin)
    2375              : 
    2376              :       ! 4. h^G0W0+SOC_nn',σσ'(k_i) = ε_nσ^G0W0(k_i) δ_nn' δ_σσ' + V^SOC_nn',σσ'(k_i)
    2377              :       CALL cfm_add_on_diag(cfm_ks_ikp_spinor, &
    2378              :                            [eigenval_no_SOC(mo_index_start_1:mo_index_start_1 + n_window - 1, ikp, 1), &
    2379            0 :                             eigenval_no_SOC(mo_index_start_2:mo_index_start_2 + n_window - 1, ikp, bs_env%n_spin)])
    2380              : 
    2381              :       ! 5. diagonalize h^G0W0+SOC_nn',σσ'(k_i) to get eigenvalues
    2382            0 :       ALLOCATE (eig_win_sorted(n_window2tot))
    2383            0 :       CALL cp_cfm_heevd(cfm_ks_ikp_spinor, cfm_eigenvec_ikp_spinor, eig_win_sorted)
    2384              : 
    2385              :       ! 6. DOS from spinors: not supported for the truncated window (CPASSERT'd above)
    2386              : 
    2387              :       ! 7. valence band max. (VBM), conduction band min. (CBM) and direct bandgap (DBG)
    2388            0 :       band_edges%VBM = MAX(band_edges%VBM, eig_win_sorted(homo_used))
    2389            0 :       band_edges%CBM = MIN(band_edges%CBM, eig_win_sorted(homo_used + 1))
    2390            0 :       band_edges%DBG = MIN(band_edges%DBG, eig_win_sorted(homo_used + 1) - eig_win_sorted(homo_used))
    2391              : 
    2392              :       CALL fill_truncated_eigenval_spinor(eigenval_no_SOC, eig_win_sorted, ikp, bs_env%n_spin, &
    2393              :                                           n_ao, mo_index_start_1, mo_index_start_2, n_window, &
    2394            0 :                                           n_window2tot, eigenval_spinor)
    2395              : 
    2396              :       ! LDOS is unsupported in the truncated path, so AO spinors are unused.
    2397            0 :       CALL cp_cfm_set_all(cfm_spinor_wf_ikp, z_zero)
    2398            0 :       CPASSERT(.NOT. bs_env%do_ldos)
    2399              : 
    2400            0 :       CALL cp_cfm_release(cfm_ks_ikp_spinor)
    2401            0 :       CALL cp_cfm_release(cfm_SOC_ikp_spinor)
    2402            0 :       CALL cp_cfm_release(cfm_work_ikp_spinor)
    2403            0 :       CALL cp_cfm_release(cfm_eigenvec_ikp_spinor)
    2404            0 :       CALL cp_cfm_release(cfm_mos_ikp_spinor)
    2405              : 
    2406            0 :       CALL cp_fm_struct_release(fst_ww)
    2407            0 :       CALL cp_fm_struct_release(fst_wa)
    2408            0 :       CALL cp_fm_struct_release(fst_mos)
    2409              : 
    2410            0 :       CALL timestop(handle)
    2411              : 
    2412            0 :    END SUBROUTINE SOC_ev_truncate_mos
    2413              : ! **************************************************************************************************
    2414              : !> \brief ...
    2415              : !> \param eigenval_no_SOC non-SOC band energies (n_ao, n_kpoints, n_spin)
    2416              : !> \param eig_win_sorted SOC-corrected eigenvalues of the window, ascending
    2417              : !> \param ikp k-point index into eigenval_no_SOC
    2418              : !> \param n_spin number of spin channels
    2419              : !> \param n_ao number of AO basis functions (per spin)
    2420              : !> \param mo_index_start_1 first retained state index for spin 1
    2421              : !> \param mo_index_start_2 first retained state index for spin n_spin
    2422              : !> \param n_window per-spin window size
    2423              : !> \param n_window2tot total window size
    2424              : !> \param eigenval_spinor output: full repacked spinor spectrum (2*n_ao)
    2425              : ! **************************************************************************************************
    2426            0 :    SUBROUTINE fill_truncated_eigenval_spinor(eigenval_no_SOC, eig_win_sorted, ikp, n_spin, n_ao, &
    2427              :                                              mo_index_start_1, mo_index_start_2, n_window, &
    2428            0 :                                              n_window2tot, eigenval_spinor)
    2429              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: eigenval_no_SOC
    2430              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eig_win_sorted
    2431              :       INTEGER                                            :: ikp, n_spin, n_ao, mo_index_start_1, &
    2432              :                                                             mo_index_start_2, n_window, &
    2433              :                                                             n_window2tot
    2434              :       REAL(KIND=dp), DIMENSION(:)                        :: eigenval_spinor
    2435              : 
    2436              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'fill_truncated_eigenval_spinor'
    2437              : 
    2438              :       INTEGER                                            :: handle, i, ia, ib, idx_end, idx_start, &
    2439              :                                                             mo_index_start_1_m1, &
    2440              :                                                             mo_index_start_2_m1
    2441              : 
    2442            0 :       CALL timeset(routineN, handle)
    2443              : 
    2444              :       ! verify the window was wide enough that SOC did not push a retained boundary state
    2445              :       ! past an excluded neighbor on either side -- this would invalidate the merge below
    2446            0 :       IF (mo_index_start_1 > 1) THEN
    2447            0 :          IF (eig_win_sorted(1) < eigenval_no_SOC(mo_index_start_1 - 1, ikp, 1)) THEN
    2448            0 :             CPABORT("SOC_NBANDS_OCC too small.")
    2449              :          END IF
    2450              :       END IF
    2451            0 :       IF (mo_index_start_2 > 1) THEN
    2452            0 :          IF (eig_win_sorted(1) < eigenval_no_SOC(mo_index_start_2 - 1, ikp, n_spin)) THEN
    2453            0 :             CPABORT("SOC_NBANDS_OCC too small.")
    2454              :          END IF
    2455              :       END IF
    2456            0 :       IF (mo_index_start_1 + n_window <= n_ao) THEN
    2457            0 :          IF (eig_win_sorted(n_window2tot) > eigenval_no_SOC(mo_index_start_1 + n_window, ikp, 1)) THEN
    2458            0 :             CPABORT("SOC_NBANDS_VIRT too small.")
    2459              :          END IF
    2460              :       END IF
    2461            0 :       IF (mo_index_start_2 + n_window <= n_ao) THEN
    2462            0 :          IF (eig_win_sorted(n_window2tot) > eigenval_no_SOC(mo_index_start_2 + n_window, ikp, n_spin)) THEN
    2463            0 :             CPABORT("SOC_NBANDS_VIRT too small.")
    2464              :          END IF
    2465              :       END IF
    2466              : 
    2467            0 :       mo_index_start_1_m1 = mo_index_start_1 - 1
    2468            0 :       mo_index_start_2_m1 = mo_index_start_2 - 1
    2469              : 
    2470            0 :       ia = 1
    2471            0 :       ib = 1
    2472            0 :       DO i = 1, mo_index_start_1_m1 + mo_index_start_2_m1
    2473            0 :          IF (ia <= mo_index_start_1_m1 .AND. ib <= mo_index_start_2_m1) THEN
    2474            0 :             IF (eigenval_no_SOC(ia, ikp, 1) <= eigenval_no_SOC(ib, ikp, n_spin)) THEN
    2475            0 :                eigenval_spinor(i) = eigenval_no_SOC(ia, ikp, 1)
    2476            0 :                ia = ia + 1
    2477              :             ELSE
    2478            0 :                eigenval_spinor(i) = eigenval_no_SOC(ib, ikp, n_spin)
    2479            0 :                ib = ib + 1
    2480              :             END IF
    2481            0 :          ELSE IF (ia <= mo_index_start_1_m1) THEN
    2482            0 :             eigenval_spinor(i) = eigenval_no_SOC(ia, ikp, 1)
    2483            0 :             ia = ia + 1
    2484              :          ELSE
    2485            0 :             eigenval_spinor(i) = eigenval_no_SOC(ib, ikp, n_spin)
    2486            0 :             ib = ib + 1
    2487              :          END IF
    2488              :       END DO
    2489              : 
    2490            0 :       idx_start = mo_index_start_1_m1 + mo_index_start_2_m1 + 1
    2491            0 :       idx_end = mo_index_start_1_m1 + mo_index_start_2_m1 + n_window2tot
    2492              : 
    2493            0 :       eigenval_spinor(idx_start:idx_end) = eig_win_sorted(:)
    2494              : 
    2495            0 :       ia = mo_index_start_1 + n_window
    2496            0 :       ib = mo_index_start_2 + n_window
    2497            0 :       DO i = mo_index_start_1_m1 + mo_index_start_2_m1 + n_window2tot + 1, 2*n_ao
    2498            0 :          IF (ia <= n_ao .AND. ib <= n_ao) THEN
    2499            0 :             IF (eigenval_no_SOC(ia, ikp, 1) <= eigenval_no_SOC(ib, ikp, n_spin)) THEN
    2500            0 :                eigenval_spinor(i) = eigenval_no_SOC(ia, ikp, 1)
    2501            0 :                ia = ia + 1
    2502              :             ELSE
    2503            0 :                eigenval_spinor(i) = eigenval_no_SOC(ib, ikp, n_spin)
    2504            0 :                ib = ib + 1
    2505              :             END IF
    2506            0 :          ELSE IF (ia <= n_ao) THEN
    2507            0 :             eigenval_spinor(i) = eigenval_no_SOC(ia, ikp, 1)
    2508            0 :             ia = ia + 1
    2509              :          ELSE
    2510            0 :             eigenval_spinor(i) = eigenval_no_SOC(ib, ikp, n_spin)
    2511            0 :             ib = ib + 1
    2512              :          END IF
    2513              :       END DO
    2514              : 
    2515            0 :       DEALLOCATE (eig_win_sorted)
    2516              : 
    2517            0 :       CALL timestop(handle)
    2518              : 
    2519            0 :    END SUBROUTINE fill_truncated_eigenval_spinor
    2520              : ! **************************************************************************************************
    2521              : !> \brief ...
    2522              : !> \param DOS ...
    2523              : !> \param PDOS ...
    2524              : !> \param eigenval ...
    2525              : !> \param ikp ...
    2526              : !> \param bs_env ...
    2527              : !> \param n_E ...
    2528              : !> \param E_min ...
    2529              : !> \param proj_mo_on_kind ...
    2530              : ! **************************************************************************************************
    2531          960 :    SUBROUTINE add_to_DOS_PDOS(DOS, PDOS, eigenval, ikp, bs_env, n_E, E_min, proj_mo_on_kind)
    2532              : 
    2533              :       REAL(KIND=dp), DIMENSION(:)                        :: DOS
    2534              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    2535              :       REAL(KIND=dp), DIMENSION(:)                        :: eigenval
    2536              :       INTEGER                                            :: ikp
    2537              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2538              :       INTEGER                                            :: n_E
    2539              :       REAL(KIND=dp)                                      :: E_min
    2540              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind
    2541              : 
    2542              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'add_to_DOS_PDOS'
    2543              : 
    2544              :       INTEGER                                            :: handle, i_E, i_kind, i_mo, n_mo, nkind
    2545              :       REAL(KIND=dp)                                      :: broadening, energy, energy_step_DOS, wkp
    2546              : 
    2547          960 :       CALL timeset(routineN, handle)
    2548              : 
    2549          960 :       energy_step_DOS = bs_env%energy_step_DOS
    2550          960 :       broadening = bs_env%broadening_DOS
    2551              : 
    2552          960 :       n_mo = SIZE(eigenval)
    2553          960 :       nkind = SIZE(proj_mo_on_kind, 2)
    2554              : 
    2555              :       ! normalize to closed-shell / open-shell
    2556          960 :       wkp = bs_env%kpoints_DOS%wkp(ikp)*bs_env%spin_degeneracy
    2557      2163304 :       DO i_E = 1, n_E
    2558      2162344 :          energy = E_min + i_E*energy_step_DOS
    2559     47852468 :          DO i_mo = 1, n_mo
    2560              :             ! DOS
    2561     45689164 :             DOS(i_E) = DOS(i_E) + wkp*Gaussian(energy - eigenval(i_mo), broadening)
    2562              : 
    2563              :             ! PDOS
    2564    139229836 :             DO i_kind = 1, nkind
    2565    137067492 :                IF (proj_mo_on_kind(i_mo, i_kind) > 0.0_dp) THEN
    2566              :                   PDOS(i_E, i_kind) = PDOS(i_E, i_kind) + &
    2567              :                                       proj_mo_on_kind(i_mo, i_kind)*wkp* &
    2568     31107580 :                                       Gaussian(energy - eigenval(i_mo), broadening)
    2569              :                END IF
    2570              :             END DO
    2571              :          END DO
    2572              :       END DO
    2573              : 
    2574          960 :       CALL timestop(handle)
    2575              : 
    2576          960 :    END SUBROUTINE add_to_DOS_PDOS
    2577              : 
    2578              : ! **************************************************************************************************
    2579              : !> \brief ...
    2580              : !> \param LDOS_2d ...
    2581              : !> \param qs_env ...
    2582              : !> \param ikp ...
    2583              : !> \param bs_env ...
    2584              : !> \param cfm_mos_ikp ...
    2585              : !> \param eigenval ...
    2586              : !> \param band_edges ...
    2587              : !> \param do_spinor ...
    2588              : !> \param cfm_non_spinor ...
    2589              : ! **************************************************************************************************
    2590            6 :    SUBROUTINE add_to_LDOS_2d(LDOS_2d, qs_env, ikp, bs_env, cfm_mos_ikp, eigenval, &
    2591              :                              band_edges, do_spinor, cfm_non_spinor)
    2592              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d
    2593              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2594              :       INTEGER                                            :: ikp
    2595              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2596              :       TYPE(cp_cfm_type)                                  :: cfm_mos_ikp
    2597              :       REAL(KIND=dp), DIMENSION(:)                        :: eigenval
    2598              :       TYPE(band_edges_type)                              :: band_edges
    2599              :       LOGICAL, OPTIONAL                                  :: do_spinor
    2600              :       TYPE(cp_cfm_type), OPTIONAL                        :: cfm_non_spinor
    2601              : 
    2602              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'add_to_LDOS_2d'
    2603              : 
    2604              :       INTEGER :: handle, i_E, i_x_end, i_x_start, i_y_end, i_y_start, i_z, i_z_end, i_z_start, &
    2605              :          j_col, j_mo, n_E, n_mo, n_z, ncol_local, nimages, z_end_global, z_start_global
    2606            6 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices
    2607              :       LOGICAL                                            :: is_any_weight_non_zero, my_do_spinor
    2608              :       REAL(KIND=dp)                                      :: broadening, E_max, E_min, &
    2609              :                                                             E_total_window, energy, energy_step, &
    2610              :                                                             energy_window, spin_degeneracy, weight
    2611              :       TYPE(cp_cfm_type)                                  :: cfm_weighted_dm_ikp, cfm_work
    2612              :       TYPE(cp_fm_type)                                   :: fm_non_spinor, fm_weighted_dm_MIC
    2613            6 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: weighted_dm_MIC
    2614              :       TYPE(dft_control_type), POINTER                    :: dft_control
    2615              :       TYPE(pw_c1d_gs_type)                               :: rho_g
    2616              :       TYPE(pw_env_type), POINTER                         :: pw_env
    2617              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    2618              :       TYPE(pw_r3d_rs_type)                               :: LDOS_3d
    2619              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    2620              : 
    2621            6 :       CALL timeset(routineN, handle)
    2622              : 
    2623            6 :       my_do_spinor = .FALSE.
    2624            6 :       IF (PRESENT(do_spinor)) my_do_spinor = do_spinor
    2625              : 
    2626            6 :       CALL get_qs_env(qs_env, ks_env=ks_env, pw_env=pw_env, dft_control=dft_control)
    2627              : 
    2628              :       ! previously, dft_control%nimages set to # neighbor cells, revert for Γ-only KS matrix
    2629            6 :       nimages = dft_control%nimages
    2630            6 :       dft_control%nimages = bs_env%nimages_scf
    2631              : 
    2632            6 :       energy_window = bs_env%energy_window_DOS
    2633            6 :       energy_step = bs_env%energy_step_DOS
    2634            6 :       broadening = bs_env%broadening_DOS
    2635              : 
    2636            6 :       E_min = band_edges%VBM - 0.5_dp*energy_window
    2637            6 :       E_max = band_edges%CBM + 0.5_dp*energy_window
    2638            6 :       E_total_window = E_max - E_min
    2639              : 
    2640            6 :       n_E = INT(E_total_window/energy_step)
    2641              : 
    2642            6 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    2643              : 
    2644            6 :       CALL auxbas_pw_pool%create_pw(LDOS_3d)
    2645            6 :       CALL auxbas_pw_pool%create_pw(rho_g)
    2646              : 
    2647            6 :       i_x_start = LBOUND(LDOS_3d%array, 1)
    2648            6 :       i_x_end = UBOUND(LDOS_3d%array, 1)
    2649            6 :       i_y_start = LBOUND(LDOS_3d%array, 2)
    2650            6 :       i_y_end = UBOUND(LDOS_3d%array, 2)
    2651            6 :       i_z_start = LBOUND(LDOS_3d%array, 3)
    2652            6 :       i_z_end = UBOUND(LDOS_3d%array, 3)
    2653              : 
    2654            6 :       z_start_global = i_z_start
    2655            6 :       z_end_global = i_z_end
    2656              : 
    2657            6 :       CALL bs_env%para_env%min(z_start_global)
    2658            6 :       CALL bs_env%para_env%max(z_end_global)
    2659            6 :       n_z = z_end_global - z_start_global + 1
    2660              : 
    2661           36 :       IF (ANY(ABS(bs_env%hmat(1:2, 3)) > 1.0E-6_dp) .OR. ANY(ABS(bs_env%hmat(3, 1:2)) > 1.0E-6_dp)) THEN
    2662            0 :          CPABORT("Please choose a cell that has 90° angles to the z-direction.")
    2663              :       END IF
    2664              :       ! for integration, we need the dz and the conversion from H -> eV and a_Bohr -> Å
    2665            6 :       bs_env%unit_ldos_int_z_inv_Ang2_eV = bs_env%hmat(3, 3)/REAL(n_z, KIND=dp)/evolt/angstrom**2
    2666              : 
    2667            6 :       IF (ikp == 1) THEN
    2668           30 :          ALLOCATE (LDOS_2d(i_x_start:i_x_end, i_y_start:i_y_end, n_E))
    2669            6 :          LDOS_2d(:, :, :) = 0.0_dp
    2670              :       END IF
    2671              : 
    2672            6 :       CALL cp_cfm_create(cfm_work, cfm_mos_ikp%matrix_struct)
    2673            6 :       CALL cp_cfm_create(cfm_weighted_dm_ikp, cfm_mos_ikp%matrix_struct)
    2674            6 :       CALL cp_fm_create(fm_weighted_dm_MIC, cfm_mos_ikp%matrix_struct)
    2675            6 :       IF (my_do_spinor) THEN
    2676            2 :          CALL cp_fm_create(fm_non_spinor, cfm_non_spinor%matrix_struct)
    2677              :       END IF
    2678              : 
    2679              :       CALL cp_cfm_get_info(matrix=cfm_mos_ikp, &
    2680              :                            ncol_global=n_mo, &
    2681              :                            ncol_local=ncol_local, &
    2682            6 :                            col_indices=col_indices)
    2683              : 
    2684            6 :       NULLIFY (weighted_dm_MIC)
    2685            6 :       CALL dbcsr_allocate_matrix_set(weighted_dm_MIC, 1)
    2686            6 :       ALLOCATE (weighted_dm_MIC(1)%matrix)
    2687              :       CALL dbcsr_create(weighted_dm_MIC(1)%matrix, template=bs_env%mat_ao_ao%matrix, &
    2688            6 :                         matrix_type=dbcsr_type_symmetric)
    2689              : 
    2690         1678 :       DO i_E = 1, n_E
    2691              : 
    2692         1672 :          energy = E_min + i_E*energy_step
    2693              : 
    2694         1672 :          is_any_weight_non_zero = .FALSE.
    2695              : 
    2696        20950 :          DO j_col = 1, ncol_local
    2697              : 
    2698        19278 :             j_mo = col_indices(j_col)
    2699              : 
    2700        19278 :             IF (my_do_spinor) THEN
    2701              :                spin_degeneracy = 1.0_dp
    2702              :             ELSE
    2703        10818 :                spin_degeneracy = bs_env%spin_degeneracy
    2704              :             END IF
    2705              : 
    2706        19278 :             weight = Gaussian(energy - eigenval(j_mo), broadening)*spin_degeneracy
    2707              : 
    2708       144099 :             cfm_work%local_data(:, j_col) = cfm_mos_ikp%local_data(:, j_col)*weight
    2709              : 
    2710        20950 :             IF (weight > 1.0E-5_dp) is_any_weight_non_zero = .TRUE.
    2711              : 
    2712              :          END DO
    2713              : 
    2714         1672 :          CALL bs_env%para_env%sync()
    2715         1672 :          CALL bs_env%para_env%sum(is_any_weight_non_zero)
    2716         1672 :          CALL bs_env%para_env%sync()
    2717              : 
    2718              :          ! cycle if there are no states at the energy i_E
    2719         1678 :          IF (is_any_weight_non_zero) THEN
    2720              : 
    2721              :             CALL parallel_gemm('N', 'C', n_mo, n_mo, n_mo, z_one, &
    2722           24 :                                cfm_mos_ikp, cfm_work, z_zero, cfm_weighted_dm_ikp)
    2723              : 
    2724           24 :             IF (my_do_spinor) THEN
    2725              : 
    2726              :                ! contribution from up,up to fm_non_spinor
    2727            8 :                CALL get_cfm_submat(cfm_non_spinor, cfm_weighted_dm_ikp, 1, 1)
    2728            8 :                CALL cp_fm_set_all(fm_non_spinor, 0.0_dp)
    2729              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_non_spinor, &
    2730              :                                               cfm_non_spinor, ikp, bs_env%kpoints_DOS, &
    2731            8 :                                               "ORB", bs_env%kpoints_DOS%wkp(ikp))
    2732              : 
    2733              :                ! add contribution from down,down to fm_non_spinor
    2734            8 :                CALL get_cfm_submat(cfm_non_spinor, cfm_weighted_dm_ikp, n_mo/2, n_mo/2)
    2735              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_non_spinor, &
    2736              :                                               cfm_non_spinor, ikp, bs_env%kpoints_DOS, &
    2737            8 :                                               "ORB", bs_env%kpoints_DOS%wkp(ikp))
    2738              :                CALL copy_fm_to_dbcsr(fm_non_spinor, weighted_dm_MIC(1)%matrix, &
    2739            8 :                                      keep_sparsity=.FALSE.)
    2740              :             ELSE
    2741           16 :                CALL cp_fm_set_all(fm_weighted_dm_MIC, 0.0_dp)
    2742              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_weighted_dm_MIC, &
    2743              :                                               cfm_weighted_dm_ikp, ikp, bs_env%kpoints_DOS, &
    2744           16 :                                               "ORB", bs_env%kpoints_DOS%wkp(ikp))
    2745              :                CALL copy_fm_to_dbcsr(fm_weighted_dm_MIC, weighted_dm_MIC(1)%matrix, &
    2746           16 :                                      keep_sparsity=.FALSE.)
    2747              :             END IF
    2748              : 
    2749       338424 :             LDOS_3d%array(:, :, :) = 0.0_dp
    2750              : 
    2751              :             CALL calculate_rho_elec(matrix_p_kp=weighted_dm_MIC, &
    2752              :                                     rho=LDOS_3d, &
    2753              :                                     rho_gspace=rho_g, &
    2754           24 :                                     ks_env=ks_env)
    2755              : 
    2756          504 :             DO i_z = i_z_start, i_z_end
    2757       338424 :                LDOS_2d(:, :, i_E) = LDOS_2d(:, :, i_E) + LDOS_3d%array(:, :, i_z)
    2758              :             END DO
    2759              : 
    2760              :          END IF
    2761              : 
    2762              :       END DO
    2763              : 
    2764              :       ! set back nimages
    2765            6 :       dft_control%nimages = nimages
    2766              : 
    2767            6 :       CALL auxbas_pw_pool%give_back_pw(LDOS_3d)
    2768            6 :       CALL auxbas_pw_pool%give_back_pw(rho_g)
    2769              : 
    2770            6 :       CALL cp_cfm_release(cfm_work)
    2771            6 :       CALL cp_cfm_release(cfm_weighted_dm_ikp)
    2772              : 
    2773            6 :       CALL cp_fm_release(fm_weighted_dm_MIC)
    2774              : 
    2775            6 :       CALL dbcsr_deallocate_matrix_set(weighted_dm_MIC)
    2776              : 
    2777            6 :       IF (my_do_spinor) THEN
    2778            2 :          CALL cp_fm_release(fm_non_spinor)
    2779              :       END IF
    2780              : 
    2781            6 :       CALL timestop(handle)
    2782              : 
    2783            6 :    END SUBROUTINE add_to_LDOS_2d
    2784              : 
    2785              : ! **************************************************************************************************
    2786              : !> \brief Write the scalar DFT spectrum for a GW calculation.
    2787              : !> \param eigenval ...
    2788              : !> \param ikp ...
    2789              : !> \param ispin ...
    2790              : !> \param bs_env ...
    2791              : ! **************************************************************************************************
    2792          776 :    SUBROUTINE write_DFT_eigenvalues(eigenval, ikp, ispin, bs_env)
    2793              :       REAL(KIND=dp), INTENT(IN)                          :: eigenval(:)
    2794              :       INTEGER, INTENT(IN)                                :: ikp, ispin
    2795              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2796              : 
    2797              :       CHARACTER(len=3)                                   :: occ_vir
    2798              :       INTEGER                                            :: i_mo, ikp_for_file, iunit
    2799              : 
    2800          776 :       IF (.NOT. bs_env%para_env%is_source()) RETURN
    2801          388 :       IF (bs_env%nkp_only_bs > 0 .AND. ikp <= bs_env%nkp_only_DOS) RETURN
    2802              : 
    2803          248 :       ikp_for_file = ikp
    2804          248 :       IF (bs_env%nkp_only_bs > 0) ikp_for_file = ikp - bs_env%nkp_only_DOS
    2805          248 :       IF (ikp_for_file == 1 .AND. ispin == 1) THEN
    2806              :          CALL open_file('bandstructure_DFT', unit_number=iunit, file_status='REPLACE', &
    2807           78 :                         file_action='WRITE')
    2808              :       ELSE
    2809              :          CALL open_file('bandstructure_DFT', unit_number=iunit, file_status='OLD', &
    2810          170 :                         file_action='WRITE', file_position='APPEND')
    2811              :       END IF
    2812              : 
    2813          248 :       WRITE (iunit, '(A)') ' '
    2814          248 :       WRITE (iunit, '(A10,I7,A25,3F10.4)') 'kpoint: ', ikp_for_file, 'coordinate: ', &
    2815         1240 :          bs_env%kpoints_DOS%xkp(:, ikp)
    2816          248 :       WRITE (iunit, '(A5,I2)') 'spin:', ispin
    2817          248 :       WRITE (iunit, '(A)') ' '
    2818          248 :       WRITE (iunit, '(A5,A12,A22)') 'n', 'k', 'ϵ_nk^DFT (eV)'
    2819          248 :       WRITE (iunit, '(A)') ' '
    2820         3227 :       DO i_mo = 1, bs_env%n_mo_retained
    2821         2979 :          IF (i_mo <= bs_env%n_occ(ispin)) occ_vir = 'occ'
    2822         2979 :          IF (i_mo > bs_env%n_occ(ispin)) occ_vir = 'vir'
    2823         2979 :          WRITE (iunit, '(I5,3A,I5,F17.3)') i_mo, ' (', occ_vir, ') ', ikp_for_file, &
    2824         6206 :             eigenval(i_mo)*evolt
    2825              :       END DO
    2826          248 :       CALL close_file(iunit)
    2827              :    END SUBROUTINE write_DFT_eigenvalues
    2828              : 
    2829              : ! **************************************************************************************************
    2830              : !> \brief Write the DFT+SOC and optionally GW+SOC spectra.
    2831              : !> \param eigenval_spinor ...
    2832              : !> \param ikp_for_file ...
    2833              : !> \param ikp ...
    2834              : !> \param bs_env ...
    2835              : !> \param eigenval_spinor_GW ...
    2836              : !> \param file_name Optional destination for a separate spectrum.
    2837              : ! **************************************************************************************************
    2838          592 :    SUBROUTINE write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env, &
    2839              :                                     eigenval_spinor_GW, file_name)
    2840              : 
    2841              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor
    2842              :       INTEGER                                            :: ikp_for_file, ikp
    2843              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2844              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), OPTIONAL :: eigenval_spinor_GW
    2845              :       CHARACTER(LEN=*), INTENT(IN), OPTIONAL             :: file_name
    2846              : 
    2847              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'write_SOC_eigenvalues'
    2848              : 
    2849              :       CHARACTER(len=3)                                   :: occ_vir
    2850              :       CHARACTER(LEN=40)                                  :: gw_soc_column
    2851              :       CHARACTER(LEN=default_string_length)               :: fname, gw_label
    2852              :       INTEGER                                            :: handle, i_mo, iunit, n_occ_spinor
    2853              : 
    2854          592 :       CALL timeset(routineN, handle)
    2855              : 
    2856          592 :       gw_label = gw_flavour_label(bs_env)
    2857              : 
    2858          592 :       fname = "bandstructure_SCF_and_G0W0_plus_SOC"
    2859          592 :       IF (PRESENT(file_name)) fname = file_name
    2860              : 
    2861          592 :       IF (bs_env%para_env%is_source()) THEN
    2862              : 
    2863          296 :          IF (ikp_for_file == 1) THEN
    2864              :             CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", &
    2865           22 :                            file_action="WRITE")
    2866              :          ELSE
    2867              :             CALL open_file(TRIM(fname), unit_number=iunit, file_status="OLD", &
    2868          274 :                            file_action="WRITE", file_position="APPEND")
    2869              :          END IF
    2870              : 
    2871          296 :          WRITE (iunit, "(A)") " "
    2872          296 :          WRITE (iunit, "(A10,I7,A25,3F10.4)") "kpoint: ", ikp_for_file, "coordinate: ", &
    2873         1480 :             bs_env%kpoints_DOS%xkp(:, ikp)
    2874          296 :          WRITE (iunit, "(A)") " "
    2875              : 
    2876          296 :          IF (PRESENT(eigenval_spinor_GW)) THEN
    2877          148 :             gw_soc_column = "ϵ_nk^"//TRIM(gw_label)//"+SOC (eV)"
    2878          148 :             IF (PRESENT(file_name)) gw_soc_column = "ϵ_nk^G0W0+SOC (eV)"
    2879          148 :             WRITE (iunit, "(A5,A12,2A22)") "n", "k", "ϵ_nk^DFT+SOC (eV)", TRIM(gw_soc_column)
    2880              :          ELSE
    2881              :             ! SCF+SOC eigenvalues only
    2882          148 :             WRITE (iunit, "(A5,A12,A22)") "n", "k", "ϵ_nk^DFT+SOC (eV)"
    2883              :          END IF
    2884              : 
    2885          296 :          n_occ_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    2886              : 
    2887         7864 :          DO i_mo = 1, SIZE(eigenval_spinor)
    2888         7568 :             IF (i_mo <= n_occ_spinor) occ_vir = 'occ'
    2889         7568 :             IF (i_mo > n_occ_spinor) occ_vir = 'vir'
    2890         7864 :             IF (PRESENT(eigenval_spinor_GW)) THEN
    2891              :                ! SCF+SOC and G0W0+SOC eigenvalues
    2892         3784 :                WRITE (iunit, "(I5,3A,I5,4F16.3,2F17.3)") i_mo, ' (', occ_vir, ') ', &
    2893         7568 :                   ikp_for_file, eigenval_spinor(i_mo)*evolt, eigenval_spinor_GW(i_mo)*evolt
    2894              :             ELSE
    2895              :                ! SCF+SOC eigenvalues only
    2896         3784 :                WRITE (iunit, "(I5,3A,I5,4F16.3,F17.3)") i_mo, ' (', occ_vir, ') ', &
    2897         7568 :                   ikp_for_file, eigenval_spinor(i_mo)*evolt
    2898              :             END IF
    2899              :          END DO
    2900              : 
    2901          296 :          CALL close_file(iunit)
    2902              : 
    2903              :       END IF
    2904              : 
    2905          592 :       CALL timestop(handle)
    2906              : 
    2907          592 :    END SUBROUTINE write_SOC_eigenvalues
    2908              : 
    2909              : ! **************************************************************************************************
    2910              : !> \brief ...
    2911              : !> \param int_number ...
    2912              : !> \return ...
    2913              : ! **************************************************************************************************
    2914            0 :    PURE FUNCTION count_digits(int_number)
    2915              : 
    2916              :       INTEGER, INTENT(IN)                                :: int_number
    2917              :       INTEGER                                            :: count_digits
    2918              : 
    2919              :       INTEGER                                            :: digitCount, tempInt
    2920              : 
    2921            0 :       digitCount = 0
    2922              : 
    2923            0 :       tempInt = int_number
    2924              : 
    2925            0 :       DO WHILE (tempInt /= 0)
    2926            0 :          tempInt = tempInt/10
    2927            0 :          digitCount = digitCount + 1
    2928              :       END DO
    2929              : 
    2930            0 :       count_digits = digitCount
    2931              : 
    2932            0 :    END FUNCTION count_digits
    2933              : 
    2934              : ! **************************************************************************************************
    2935              : !> \brief ...
    2936              : !> \param band_edges ...
    2937              : !> \param scf_gw_soc ...
    2938              : !> \param bs_env ...
    2939              : ! **************************************************************************************************
    2940          514 :    SUBROUTINE write_band_edges(band_edges, scf_gw_soc, bs_env)
    2941              : 
    2942              :       TYPE(band_edges_type)                              :: band_edges
    2943              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    2944              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2945              : 
    2946              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'write_band_edges'
    2947              : 
    2948              :       CHARACTER(LEN=17)                                  :: print_format
    2949              :       INTEGER                                            :: handle, u
    2950              : 
    2951          514 :       CALL timeset(routineN, handle)
    2952              : 
    2953              :       ! print format
    2954          514 :       print_format = "(T2,2A,T61,F20.3)"
    2955              : 
    2956          514 :       u = bs_env%unit_nr
    2957          514 :       IF (u > 0) THEN
    2958          257 :          WRITE (u, '(T2,A)') ''
    2959          257 :          WRITE (u, print_format) scf_gw_soc, ' valence band maximum (eV):', band_edges%VBM*evolt
    2960          257 :          WRITE (u, print_format) scf_gw_soc, ' conduction band minimum (eV):', band_edges%CBM*evolt
    2961          257 :          WRITE (u, print_format) scf_gw_soc, ' indirect band gap (eV):', band_edges%IDBG*evolt
    2962          257 :          WRITE (u, print_format) scf_gw_soc, ' direct band gap (eV):', band_edges%DBG*evolt
    2963              :       END IF
    2964              : 
    2965          514 :       CALL timestop(handle)
    2966              : 
    2967          514 :    END SUBROUTINE write_band_edges
    2968              : 
    2969              : ! **************************************************************************************************
    2970              : !> \brief ...
    2971              : !> \param DOS ...
    2972              : !> \param PDOS ...
    2973              : !> \param bs_env ...
    2974              : !> \param qs_env ...
    2975              : !> \param scf_gw_soc ...
    2976              : !> \param E_min ...
    2977              : !> \param E_VBM ...
    2978              : ! **************************************************************************************************
    2979           88 :    SUBROUTINE write_dos_pdos(DOS, PDOS, bs_env, qs_env, scf_gw_soc, E_min, E_VBM)
    2980              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: DOS
    2981              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    2982              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2983              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2984              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    2985              :       REAL(KIND=dp)                                      :: E_min, E_VBM
    2986              : 
    2987              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'write_dos_pdos'
    2988              : 
    2989              :       CHARACTER(LEN=3), DIMENSION(100)                   :: elements
    2990              :       CHARACTER(LEN=default_string_length)               :: atom_name, fname, output_string
    2991              :       INTEGER                                            :: handle, i_E, i_kind, iatom, iunit, n_A, &
    2992              :                                                             n_E, nkind
    2993              :       REAL(KIND=dp)                                      :: energy
    2994           88 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2995              : 
    2996           88 :       CALL timeset(routineN, handle)
    2997              : 
    2998           88 :       WRITE (fname, "(3A)") "DOS_PDOS_", scf_gw_soc, ".out"
    2999              : 
    3000           88 :       n_E = SIZE(PDOS, 1)
    3001           88 :       nkind = SIZE(PDOS, 2)
    3002           88 :       CALL get_qs_env(qs_env, particle_set=particle_set)
    3003              : 
    3004           88 :       IF (bs_env%para_env%is_source()) THEN
    3005              : 
    3006           44 :          CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", file_action="WRITE")
    3007              : 
    3008           44 :          n_A = 2 + nkind
    3009              : 
    3010          170 :          DO iatom = 1, bs_env%n_atom
    3011              :             CALL get_atomic_kind(atomic_kind=particle_set(iatom)%atomic_kind, &
    3012          126 :                                  kind_number=i_kind, name=atom_name)
    3013          170 :             elements(i_kind) = atom_name(1:3)
    3014              :          END DO
    3015              : 
    3016           44 :          WRITE (output_string, "(A,I1,A)") "(", n_A, "A)"
    3017              : 
    3018           44 :          WRITE (iunit, TRIM(output_string)) "Energy-E_F (eV)    DOS (1/eV)    PDOS (1/eV) ", &
    3019           88 :             " of atom type ", elements(1:nkind)
    3020              : 
    3021           44 :          WRITE (output_string, "(A,I1,A)") "(", n_A, "F13.5)"
    3022              : 
    3023        91682 :          DO i_E = 1, n_E
    3024              :             ! energy is relative to valence band maximum => - E_VBM
    3025        91638 :             energy = E_min + i_E*bs_env%energy_step_DOS - E_VBM
    3026       274958 :             WRITE (iunit, TRIM(output_string)) energy*evolt, DOS(i_E)/evolt, PDOS(i_E, :)/evolt
    3027              :          END DO
    3028              : 
    3029           44 :          CALL close_file(iunit)
    3030              : 
    3031              :       END IF
    3032              : 
    3033           88 :       CALL timestop(handle)
    3034              : 
    3035           88 :    END SUBROUTINE write_dos_pdos
    3036              : 
    3037              : ! **************************************************************************************************
    3038              : !> \brief ...
    3039              : !> \param energy ...
    3040              : !> \param broadening ...
    3041              : !> \return ...
    3042              : ! **************************************************************************************************
    3043     76816022 :    PURE FUNCTION Gaussian(energy, broadening)
    3044              : 
    3045              :       REAL(KIND=dp), INTENT(IN)                          :: energy, broadening
    3046              :       REAL(KIND=dp)                                      :: Gaussian
    3047              : 
    3048     76816022 :       IF (ABS(energy) < 5*broadening) THEN
    3049       117160 :          Gaussian = 1.0_dp/broadening/SQRT(twopi)*EXP(-0.5_dp*energy**2/broadening**2)
    3050              :       ELSE
    3051              :          Gaussian = 0.0_dp
    3052              :       END IF
    3053              : 
    3054     76816022 :    END FUNCTION Gaussian
    3055              : 
    3056              : ! **************************************************************************************************
    3057              : !> \brief ...
    3058              : !> \param proj_mo_on_kind ...
    3059              : !> \param qs_env ...
    3060              : !> \param cfm_mos ...
    3061              : !> \param cfm_s ...
    3062              : ! **************************************************************************************************
    3063          810 :    SUBROUTINE compute_proj_mo_on_kind(proj_mo_on_kind, qs_env, cfm_mos, cfm_s)
    3064              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind
    3065              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3066              :       TYPE(cp_cfm_type)                                  :: cfm_mos, cfm_s
    3067              : 
    3068              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_proj_mo_on_kind'
    3069              : 
    3070              :       INTEGER                                            :: handle, i_atom, i_global, i_kind, i_row, &
    3071              :                                                             j_col, n_ao, n_mo, ncol_local, nkind, &
    3072              :                                                             nrow_local
    3073          810 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_from_bf, kind_of
    3074          810 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    3075          810 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    3076              :       TYPE(cp_cfm_type)                                  :: cfm_proj, cfm_s_i_kind, cfm_work
    3077              :       TYPE(cp_fm_type)                                   :: fm_proj_im, fm_proj_re
    3078              : 
    3079          810 :       CALL timeset(routineN, handle)
    3080              : 
    3081          810 :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, nkind=nkind)
    3082          810 :       CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
    3083              : 
    3084              :       CALL cp_cfm_get_info(matrix=cfm_mos, &
    3085              :                            nrow_global=n_mo, &
    3086              :                            nrow_local=nrow_local, &
    3087              :                            ncol_local=ncol_local, &
    3088              :                            row_indices=row_indices, &
    3089          810 :                            col_indices=col_indices)
    3090              : 
    3091          810 :       n_ao = qs_env%bs_env%n_ao
    3092              : 
    3093         2430 :       ALLOCATE (atom_from_bf(n_ao))
    3094          810 :       CALL get_atom_index_from_basis_function_index(qs_env, atom_from_bf, n_ao, "ORB")
    3095              : 
    3096          810 :       proj_mo_on_kind(:, :) = 0.0_dp
    3097              : 
    3098          810 :       CALL cp_cfm_create(cfm_s_i_kind, cfm_s%matrix_struct)
    3099          810 :       CALL cp_cfm_create(cfm_work, cfm_s%matrix_struct)
    3100          810 :       CALL cp_cfm_create(cfm_proj, cfm_s%matrix_struct)
    3101          810 :       CALL cp_fm_create(fm_proj_re, cfm_s%matrix_struct)
    3102          810 :       CALL cp_fm_create(fm_proj_im, cfm_s%matrix_struct)
    3103              : 
    3104         2018 :       DO i_kind = 1, nkind
    3105              : 
    3106         1208 :          CALL cp_cfm_to_cfm(cfm_s, cfm_s_i_kind)
    3107              : 
    3108              :          ! set entries in overlap matrix to zero which do not belong to atoms of i_kind
    3109        14244 :          DO j_col = 1, ncol_local
    3110        98782 :             DO i_row = 1, nrow_local
    3111              : 
    3112        84538 :                i_global = row_indices(i_row)
    3113              : 
    3114        84538 :                IF (i_global <= n_ao) THEN
    3115        84538 :                   i_atom = atom_from_bf(i_global)
    3116            0 :                ELSE IF (i_global <= 2*n_ao) THEN
    3117            0 :                   i_atom = atom_from_bf(i_global - n_ao)
    3118              :                ELSE
    3119            0 :                   CPABORT("Wrong indices.")
    3120              :                END IF
    3121              : 
    3122        97574 :                IF (i_kind /= kind_of(i_atom)) THEN
    3123        36941 :                   cfm_s_i_kind%local_data(i_row, j_col) = z_zero
    3124              :                END IF
    3125              : 
    3126              :             END DO
    3127              :          END DO
    3128              : 
    3129              :          CALL parallel_gemm('N', 'N', n_mo, n_mo, n_mo, z_one, &
    3130         1208 :                             cfm_s_i_kind, cfm_mos, z_zero, cfm_work)
    3131              :          CALL parallel_gemm('C', 'N', n_mo, n_mo, n_mo, z_one, &
    3132         1208 :                             cfm_mos, cfm_work, z_zero, cfm_proj)
    3133              : 
    3134         1208 :          CALL cp_cfm_to_fm(cfm_proj, fm_proj_re, fm_proj_im)
    3135              : 
    3136         1208 :          CALL cp_fm_get_diag(fm_proj_im, proj_mo_on_kind(:, i_kind))
    3137         2018 :          CALL cp_fm_get_diag(fm_proj_re, proj_mo_on_kind(:, i_kind))
    3138              : 
    3139              :       END DO ! i_kind
    3140              : 
    3141          810 :       CALL cp_cfm_release(cfm_s_i_kind)
    3142          810 :       CALL cp_cfm_release(cfm_work)
    3143          810 :       CALL cp_cfm_release(cfm_proj)
    3144          810 :       CALL cp_fm_release(fm_proj_re)
    3145          810 :       CALL cp_fm_release(fm_proj_im)
    3146              : 
    3147          810 :       CALL timestop(handle)
    3148              : 
    3149         3240 :    END SUBROUTINE compute_proj_mo_on_kind
    3150              : 
    3151              : ! **************************************************************************************************
    3152              : !> \brief ...
    3153              : !> \param cfm_spinor_ikp ...
    3154              : !> \param cfm_spinor_Gamma ...
    3155              : !> \param fm_struct_non_spinor ...
    3156              : !> \param ikp ...
    3157              : !> \param qs_env ...
    3158              : !> \param kpoints ...
    3159              : !> \param basis_type ...
    3160              : ! **************************************************************************************************
    3161          120 :    SUBROUTINE cfm_ikp_from_cfm_spinor_Gamma(cfm_spinor_ikp, cfm_spinor_Gamma, fm_struct_non_spinor, &
    3162              :                                             ikp, qs_env, kpoints, basis_type)
    3163              :       TYPE(cp_cfm_type)                                  :: cfm_spinor_ikp, cfm_spinor_Gamma
    3164              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_non_spinor
    3165              :       INTEGER                                            :: ikp
    3166              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3167              :       TYPE(kpoint_type), POINTER                         :: kpoints
    3168              :       CHARACTER(LEN=*)                                   :: basis_type
    3169              : 
    3170              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cfm_ikp_from_cfm_spinor_Gamma'
    3171              : 
    3172              :       INTEGER                                            :: handle, i_block, i_offset, j_block, &
    3173              :                                                             j_offset, n_ao
    3174              :       TYPE(cp_cfm_type)                                  :: cfm_non_spinor_Gamma, cfm_non_spinor_ikp
    3175              :       TYPE(cp_fm_type)                                   :: fm_non_spinor_Gamma_im, &
    3176              :                                                             fm_non_spinor_Gamma_re
    3177              : 
    3178           20 :       CALL timeset(routineN, handle)
    3179              : 
    3180           20 :       CALL cp_cfm_create(cfm_non_spinor_Gamma, fm_struct_non_spinor)
    3181           20 :       CALL cp_cfm_create(cfm_non_spinor_ikp, fm_struct_non_spinor)
    3182           20 :       CALL cp_fm_create(fm_non_spinor_Gamma_re, fm_struct_non_spinor)
    3183           20 :       CALL cp_fm_create(fm_non_spinor_Gamma_im, fm_struct_non_spinor)
    3184              : 
    3185           20 :       CALL cp_cfm_get_info(cfm_non_spinor_Gamma, nrow_global=n_ao)
    3186              : 
    3187           20 :       CALL cp_cfm_set_all(cfm_spinor_ikp, z_zero)
    3188              : 
    3189           60 :       DO i_block = 0, 1
    3190          140 :          DO j_block = 0, 1
    3191           80 :             i_offset = i_block*n_ao + 1
    3192           80 :             j_offset = j_block*n_ao + 1
    3193           80 :             CALL get_cfm_submat(cfm_non_spinor_Gamma, cfm_spinor_Gamma, i_offset, j_offset)
    3194           80 :             CALL cp_cfm_to_fm(cfm_non_spinor_Gamma, fm_non_spinor_Gamma_re, fm_non_spinor_Gamma_im)
    3195              : 
    3196              :             ! transform real part of Gamma-point matrix to ikp
    3197              :             CALL cfm_ikp_from_fm_Gamma(cfm_non_spinor_ikp, fm_non_spinor_Gamma_re, &
    3198           80 :                                        ikp, qs_env, kpoints, basis_type)
    3199           80 :             CALL add_cfm_submat(cfm_spinor_ikp, cfm_non_spinor_ikp, i_offset, j_offset)
    3200              : 
    3201              :             ! transform imag part of Gamma-point matrix to ikp
    3202              :             CALL cfm_ikp_from_fm_Gamma(cfm_non_spinor_ikp, fm_non_spinor_Gamma_im, &
    3203           80 :                                        ikp, qs_env, kpoints, basis_type)
    3204          120 :             CALL add_cfm_submat(cfm_spinor_ikp, cfm_non_spinor_ikp, i_offset, j_offset, gaussi)
    3205              : 
    3206              :          END DO
    3207              :       END DO
    3208              : 
    3209           20 :       CALL cp_cfm_release(cfm_non_spinor_Gamma)
    3210           20 :       CALL cp_cfm_release(cfm_non_spinor_ikp)
    3211           20 :       CALL cp_fm_release(fm_non_spinor_Gamma_re)
    3212           20 :       CALL cp_fm_release(fm_non_spinor_Gamma_im)
    3213              : 
    3214           20 :       CALL timestop(handle)
    3215              : 
    3216           20 :    END SUBROUTINE cfm_ikp_from_cfm_spinor_Gamma
    3217              : 
    3218              : ! **************************************************************************************************
    3219              : !> \brief Reconstruct a k-point operator from a real Gamma matrix using one MIC phase.
    3220              : !> \param cfm_ikp ...
    3221              : !> \param fm_Gamma ...
    3222              : !> \param ikp ...
    3223              : !> \param qs_env ...
    3224              : !> \param kpoints ...
    3225              : !> \param basis_type ...
    3226              : ! **************************************************************************************************
    3227         8422 :    SUBROUTINE cfm_ikp_from_fm_Gamma(cfm_ikp, fm_Gamma, ikp, qs_env, kpoints, basis_type)
    3228              :       TYPE(cp_cfm_type)                                  :: cfm_ikp
    3229              :       TYPE(cp_fm_type)                                   :: fm_Gamma
    3230              :       INTEGER                                            :: ikp
    3231              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3232              :       TYPE(kpoint_type), POINTER                         :: kpoints
    3233              :       CHARACTER(LEN=*)                                   :: basis_type
    3234              : 
    3235              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cfm_ikp_from_fm_Gamma'
    3236              : 
    3237              :       INTEGER                                            :: handle
    3238         8422 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_from_bf
    3239              :       TYPE(cell_type), POINTER                           :: cell
    3240         8422 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    3241              : 
    3242         8422 :       CALL timeset(routineN, handle)
    3243              : 
    3244         8422 :       CPASSERT(basis_type == "ORB" .OR. basis_type == "RI_AUX")
    3245        25266 :       ALLOCATE (atom_from_bf(fm_Gamma%matrix_struct%nrow_global))
    3246         8422 :       CALL get_atom_index_from_basis_function_index(qs_env, atom_from_bf, SIZE(atom_from_bf), basis_type)
    3247         8422 :       NULLIFY (cell, particle_set)
    3248         8422 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
    3249              :       CALL fm_gamma_to_kp(cfm_ikp, fm_Gamma, cell, particle_set, atom_from_bf, &
    3250         8422 :                           kpoints%index_to_cell, kpoints%xkp(:, ikp))
    3251              : 
    3252         8422 :       CALL timestop(handle)
    3253              : 
    3254        16844 :    END SUBROUTINE cfm_ikp_from_fm_Gamma
    3255              : 
    3256              : ! **************************************************************************************************
    3257              : !> \brief ...
    3258              : !> \param bs_env ...
    3259              : !> \param qs_env ...
    3260              : !> \param fm_W_MIC_freq_j ...
    3261              : !> \param cfm_W_ikp_freq_j ...
    3262              : !> \param ikp ...
    3263              : !> \param kpoints ...
    3264              : !> \param basis_type ...
    3265              : !> \param wkp_ext ...
    3266              : ! **************************************************************************************************
    3267         3930 :    SUBROUTINE MIC_contribution_from_ikp(bs_env, qs_env, fm_W_MIC_freq_j, &
    3268              :                                         cfm_W_ikp_freq_j, ikp, kpoints, basis_type, wkp_ext)
    3269              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3270              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3271              :       TYPE(cp_fm_type)                                   :: fm_W_MIC_freq_j
    3272              :       TYPE(cp_cfm_type)                                  :: cfm_W_ikp_freq_j
    3273              :       INTEGER, INTENT(IN)                                :: ikp
    3274              :       TYPE(kpoint_type), POINTER                         :: kpoints
    3275              :       CHARACTER(LEN=*)                                   :: basis_type
    3276              :       REAL(KIND=dp), OPTIONAL                            :: wkp_ext
    3277              : 
    3278              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'MIC_contribution_from_ikp'
    3279              : 
    3280              :       INTEGER                                            :: handle, i_bf, iatom, iatom_old, irow, &
    3281              :                                                             j_bf, jatom, jatom_old, jcol, n_bf, &
    3282              :                                                             ncol_local, nrow_local, num_cells
    3283         3930 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_from_bf_index
    3284         3930 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    3285         3930 :       INTEGER, DIMENSION(:, :), POINTER                  :: index_to_cell
    3286              :       REAL(KIND=dp)                                      :: contribution, weight_im, weight_re, &
    3287              :                                                             wkp_of_ikp
    3288              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat
    3289         3930 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: wkp
    3290         3930 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: xkp
    3291              :       TYPE(cell_type), POINTER                           :: cell
    3292         3930 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    3293              : 
    3294         3930 :       CALL timeset(routineN, handle)
    3295              : 
    3296              :       ! get number of basis functions (bf) for different basis sets
    3297         3930 :       IF (basis_type == "ORB") THEN
    3298           32 :          n_bf = qs_env%bs_env%n_ao
    3299         3898 :       ELSE IF (basis_type == "RI_AUX") THEN
    3300         3898 :          n_bf = qs_env%bs_env%n_RI
    3301              :       ELSE
    3302            0 :          CPABORT("Only ORB and RI_AUX basis implemented.")
    3303              :       END IF
    3304              : 
    3305        11790 :       ALLOCATE (atom_from_bf_index(n_bf))
    3306         3930 :       CALL get_atom_index_from_basis_function_index(qs_env, atom_from_bf_index, n_bf, basis_type)
    3307              : 
    3308         3930 :       NULLIFY (cell, particle_set)
    3309         3930 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
    3310         3930 :       CALL get_cell(cell=cell, h=hmat)
    3311              : 
    3312              :       CALL cp_cfm_get_info(matrix=cfm_W_ikp_freq_j, &
    3313              :                            nrow_local=nrow_local, &
    3314              :                            ncol_local=ncol_local, &
    3315              :                            row_indices=row_indices, &
    3316         3930 :                            col_indices=col_indices)
    3317              : 
    3318         3930 :       CALL get_kpoint_info(kpoints, xkp=xkp, wkp=wkp)
    3319         3930 :       index_to_cell => kpoints%index_to_cell
    3320         3930 :       num_cells = SIZE(index_to_cell, 2)
    3321              : 
    3322         3930 :       iatom_old = 0
    3323         3930 :       jatom_old = 0
    3324              : 
    3325        77984 :       DO jcol = 1, ncol_local
    3326      1550847 :          DO irow = 1, nrow_local
    3327              : 
    3328      1472863 :             i_bf = row_indices(irow)
    3329      1472863 :             j_bf = col_indices(jcol)
    3330              : 
    3331      1472863 :             iatom = atom_from_bf_index(i_bf)
    3332      1472863 :             jatom = atom_from_bf_index(j_bf)
    3333              : 
    3334      1472863 :             IF (PRESENT(wkp_ext)) THEN
    3335         3496 :                wkp_of_ikp = wkp_ext
    3336              :             ELSE
    3337      1516506 :                SELECT CASE (bs_env%l_RI(i_bf) + bs_env%l_RI(j_bf))
    3338              :                CASE (0)
    3339              :                   ! both RI functions are s-functions, k-extrapolation for 2D and 3D
    3340        47139 :                   wkp_of_ikp = wkp(ikp)
    3341              :                CASE (1)
    3342              :                   ! one function is an s-function, the other a p-function, k-extrapolation for 3D
    3343       193398 :                   wkp_of_ikp = bs_env%wkp_s_p(ikp)
    3344              :                CASE DEFAULT
    3345              :                   ! for any other matrix element of W, there is no need for extrapolation
    3346      1469367 :                   wkp_of_ikp = bs_env%wkp_no_extra(ikp)
    3347              :                END SELECT
    3348              :             END IF
    3349              : 
    3350      1472863 :             IF (iatom /= iatom_old .OR. jatom /= jatom_old) THEN
    3351              : 
    3352              :                CALL compute_weight_re_im(weight_re, weight_im, &
    3353              :                                          num_cells, iatom, jatom, xkp(1:3, ikp), wkp_of_ikp, &
    3354       146460 :                                          cell, index_to_cell, hmat, particle_set)
    3355              : 
    3356       146460 :                iatom_old = iatom
    3357       146460 :                jatom_old = jatom
    3358              : 
    3359              :             END IF
    3360              : 
    3361              :             contribution = weight_re*REAL(cfm_W_ikp_freq_j%local_data(irow, jcol)) + &
    3362      1472863 :                            weight_im*AIMAG(cfm_W_ikp_freq_j%local_data(irow, jcol))
    3363              : 
    3364              :             fm_W_MIC_freq_j%local_data(irow, jcol) = fm_W_MIC_freq_j%local_data(irow, jcol) &
    3365      1546917 :                                                      + contribution
    3366              : 
    3367              :          END DO
    3368              :       END DO
    3369              : 
    3370         3930 :       CALL timestop(handle)
    3371              : 
    3372        11790 :    END SUBROUTINE MIC_contribution_from_ikp
    3373              : 
    3374              : ! **************************************************************************************************
    3375              : !> \brief ...
    3376              : !> \param xkp ...
    3377              : !> \param ikp_start ...
    3378              : !> \param ikp_end ...
    3379              : !> \param grid ...
    3380              : ! **************************************************************************************************
    3381          158 :    SUBROUTINE compute_xkp(xkp, ikp_start, ikp_end, grid)
    3382              : 
    3383              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: xkp
    3384              :       INTEGER                                            :: ikp_start, ikp_end
    3385              :       INTEGER, DIMENSION(3)                              :: grid
    3386              : 
    3387              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_xkp'
    3388              : 
    3389              :       INTEGER                                            :: handle, i, ix, iy, iz
    3390              : 
    3391          158 :       CALL timeset(routineN, handle)
    3392              : 
    3393          158 :       i = ikp_start
    3394          358 :       DO ix = 1, grid(1)
    3395          736 :          DO iy = 1, grid(2)
    3396         1298 :             DO iz = 1, grid(3)
    3397              : 
    3398          720 :                IF (i > ikp_end) CYCLE
    3399              : 
    3400          682 :                xkp(1, i) = REAL(2*ix - grid(1) - 1, KIND=dp)/(2._dp*REAL(grid(1), KIND=dp))
    3401          682 :                xkp(2, i) = REAL(2*iy - grid(2) - 1, KIND=dp)/(2._dp*REAL(grid(2), KIND=dp))
    3402          682 :                xkp(3, i) = REAL(2*iz - grid(3) - 1, KIND=dp)/(2._dp*REAL(grid(3), KIND=dp))
    3403         1098 :                i = i + 1
    3404              : 
    3405              :             END DO
    3406              :          END DO
    3407              :       END DO
    3408              : 
    3409          158 :       CALL timestop(handle)
    3410              : 
    3411          158 :    END SUBROUTINE compute_xkp
    3412              : 
    3413              : ! **************************************************************************************************
    3414              : !> \brief ...
    3415              : !> \param kpoints ...
    3416              : !> \param qs_env ...
    3417              : ! **************************************************************************************************
    3418          252 :    SUBROUTINE kpoint_init_cell_index_simple(kpoints, qs_env)
    3419              : 
    3420              :       TYPE(kpoint_type), POINTER                         :: kpoints
    3421              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3422              : 
    3423              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'kpoint_init_cell_index_simple'
    3424              : 
    3425              :       INTEGER                                            :: handle, nimages
    3426              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    3427              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    3428          126 :          POINTER                                         :: sab_orb
    3429              : 
    3430          126 :       CALL timeset(routineN, handle)
    3431              : 
    3432          126 :       NULLIFY (para_env, sab_orb)
    3433          126 :       CALL get_qs_env(qs_env=qs_env, para_env=para_env, sab_orb=sab_orb)
    3434          126 :       CALL kpoint_init_cell_index(kpoints, sab_orb, para_env, nimages)
    3435              : 
    3436          126 :       CALL timestop(handle)
    3437              : 
    3438          126 :    END SUBROUTINE kpoint_init_cell_index_simple
    3439              : 
    3440              : ! **************************************************************************************************
    3441              : !> \brief ...
    3442              : !> \param qs_env ...
    3443              : !> \param bs_env ...
    3444              : ! **************************************************************************************************
    3445           22 :    SUBROUTINE soc(qs_env, bs_env)
    3446              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3447              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3448              : 
    3449              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'soc'
    3450              : 
    3451              :       INTEGER                                            :: handle
    3452              : 
    3453           22 :       CALL timeset(routineN, handle)
    3454              : 
    3455              :       ! V^SOC_µν^(α),R = ħ/2 < ϕ_µ cell O | sum_ℓ ΔV_ℓ^SO(r,r') L^(α) | ϕ_ν cell R>, α = x,y,z
    3456              :       ! see Hartwigsen, Goedecker, Hutter, Eq.(18), (19) (doi.org/10.1103/PhysRevB.58.3641)
    3457           22 :       CALL V_SOC_xyz_from_pseudopotential(qs_env, bs_env%mat_V_SOC_xyz)
    3458              : 
    3459              :       ! Calculate H^SOC_µν,σσ'(k) = sum_α V^SOC_µν^(α)(k)*Pauli-matrix^(α)_σσ'
    3460              :       ! see Hartwigsen, Goedecker, Hutter, Eq.(18) (doi.org/10.1103/PhysRevB.58.3641)
    3461           28 :       SELECT CASE (bs_env%gw_implementation)
    3462              :       CASE (tensor_large_cell_Gamma, ri_rs_large_cell_Gamma, ri_rs_non_periodic, tensor_static_cohsex)
    3463              : 
    3464              :          ! H^SOC_µν,σσ' = sum_α V^SOC_µν^(α)*Pauli-matrix^(α)_σσ'
    3465            6 :          CALL H_KS_spinor_Gamma(bs_env)
    3466              : 
    3467              :       CASE (tensor_small_cell_full_kp, ri_rs_small_cell_full_kp)
    3468              : 
    3469              :          ! V^SOC_µν^(α),R -> V^SOC_µν^(α)(k); then calculate spinor H^SOC_µν,σσ'(k) (see above)
    3470           22 :          CALL H_KS_spinor_kp(qs_env, bs_env)
    3471              : 
    3472              :       END SELECT
    3473              : 
    3474           22 :       CALL timestop(handle)
    3475              : 
    3476           22 :    END SUBROUTINE soc
    3477              : 
    3478              : ! **************************************************************************************************
    3479              : !> \brief ...
    3480              : !> \param bs_env ...
    3481              : ! **************************************************************************************************
    3482            6 :    SUBROUTINE H_KS_spinor_Gamma(bs_env)
    3483              : 
    3484              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3485              : 
    3486              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'H_KS_spinor_Gamma'
    3487              : 
    3488              :       INTEGER                                            :: handle, nao, s
    3489              :       TYPE(cp_fm_struct_type), POINTER                   :: str
    3490              : 
    3491            6 :       CALL timeset(routineN, handle)
    3492              : 
    3493            6 :       CALL cp_fm_get_info(bs_env%fm_ks_Gamma(1), nrow_global=nao)
    3494              : 
    3495           12 :       ALLOCATE (bs_env%cfm_SOC_spinor_ao(1))
    3496            6 :       CALL create_cfm_double(bs_env%cfm_SOC_spinor_ao(1), fm_orig=bs_env%fm_ks_Gamma(1))
    3497            6 :       CALL cp_cfm_set_all(bs_env%cfm_SOC_spinor_ao(1), z_zero)
    3498              : 
    3499            6 :       str => bs_env%fm_ks_Gamma(1)%matrix_struct
    3500              : 
    3501            6 :       s = nao + 1
    3502              : 
    3503              :       ! careful: inside add_dbcsr_submat, mat_V_SOC_xyz is multiplied by i because the real matrix
    3504              :       !          mat_V_SOC_xyz is antisymmetric as V_SOC matrix is purely imaginary and Hermitian
    3505              :       ! V_x * sigma_x: sigma_x = ((0,1),(1,0))
    3506              :       ! ud block (1,s): +i*V_x
    3507              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(1, 1)%matrix, &
    3508            6 :                             str, 1, s, z_one, .FALSE.)
    3509              :       ! du block (s,1): +i*V_x
    3510              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(1, 1)%matrix, &
    3511            6 :                             str, s, 1, z_one, .FALSE.)
    3512              : 
    3513              :       ! V_y * sigma_y: sigma_y = ((0,-i),(i,0))
    3514              :       ! ud block (1,s): i*(i*V_y) = -V_y  (extra gaussi factor)
    3515              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(2, 1)%matrix, &
    3516            6 :                             str, 1, s, gaussi, .FALSE.)
    3517              :       ! du block (s,1): -i*(i*V_y) = +V_y  (extra -gaussi factor)
    3518              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(2, 1)%matrix, &
    3519            6 :                             str, s, 1, -gaussi, .FALSE.)
    3520              : 
    3521              :       ! V_z * sigma_z: sigma_z = ((1,0),(0,-1))
    3522              :       ! uu block (1,1): +i*V_z
    3523              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(3, 1)%matrix, &
    3524            6 :                             str, 1, 1, z_one, .FALSE.)
    3525              :       ! dd block (s,s): -i*V_z
    3526              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(3, 1)%matrix, &
    3527            6 :                             str, s, s, -z_one, .FALSE.)
    3528              : 
    3529            6 :       CALL timestop(handle)
    3530              : 
    3531            6 :    END SUBROUTINE H_KS_spinor_Gamma
    3532              : 
    3533              : ! **************************************************************************************************
    3534              : !> \brief ...
    3535              : !> \param qs_env ...
    3536              : !> \param bs_env ...
    3537              : ! **************************************************************************************************
    3538           32 :    SUBROUTINE H_KS_spinor_kp(qs_env, bs_env)
    3539              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3540              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3541              : 
    3542              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'H_KS_spinor_kp'
    3543              : 
    3544              :       INTEGER                                            :: handle, i_dim, ikp, n_spin, &
    3545              :                                                             nkp_bs_and_DOS, s
    3546           16 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index_scf
    3547              :       REAL(KIND=dp), DIMENSION(3)                        :: xkp
    3548              :       TYPE(cp_cfm_type)                                  :: cfm_V_SOC_xyz_ikp
    3549              :       TYPE(cp_fm_struct_type), POINTER                   :: str
    3550              :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
    3551              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    3552           16 :          POINTER                                         :: sab_nl
    3553              : 
    3554           16 :       CALL timeset(routineN, handle)
    3555              : 
    3556           16 :       nkp_bs_and_DOS = bs_env%nkp_bs_and_DOS
    3557           16 :       n_spin = bs_env%n_spin
    3558           16 :       s = bs_env%n_ao + 1
    3559           16 :       str => bs_env%cfm_ks_kp(1, 1)%matrix_struct
    3560              : 
    3561           16 :       CALL cp_cfm_create(cfm_V_SOC_xyz_ikp, bs_env%cfm_work_mo%matrix_struct)
    3562              : 
    3563           16 :       CALL alloc_cfm_double_array_1d(bs_env%cfm_SOC_spinor_ao, bs_env%cfm_ks_kp(1, 1), nkp_bs_and_DOS)
    3564              : 
    3565           16 :       CALL get_qs_env(qs_env, kpoints=kpoints_scf)
    3566              : 
    3567           16 :       NULLIFY (sab_nl)
    3568           16 :       CALL get_kpoint_info(kpoints_scf, sab_nl=sab_nl, cell_to_index=cell_to_index_scf)
    3569              : 
    3570           64 :       DO i_dim = 1, 3
    3571              : 
    3572         1018 :          DO ikp = 1, nkp_bs_and_DOS
    3573              : 
    3574         3816 :             xkp(1:3) = bs_env%kpoints_DOS%xkp(1:3, ikp)
    3575              : 
    3576          954 :             CALL cp_cfm_set_all(cfm_V_SOC_xyz_ikp, z_zero)
    3577              : 
    3578              :             CALL rsmat_to_kp(bs_env%mat_V_SOC_xyz, i_dim, xkp, cell_to_index_scf, &
    3579          954 :                              sab_nl, bs_env, cfm_V_SOC_xyz_ikp, imag_rs_mat=.TRUE.)
    3580              : 
    3581              :             ! multiply V_SOC with i because bs_env%mat_V_SOC_xyz stores imag. part (real part = 0)
    3582          954 :             CALL cp_cfm_scale(gaussi, cfm_V_SOC_xyz_ikp)
    3583              : 
    3584           48 :             SELECT CASE (i_dim)
    3585              :             CASE (1)
    3586              :                ! add V^SOC_x * σ_x for σ_x = ( (0,1) (1,0) )
    3587          318 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, 1, s)
    3588          318 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, s, 1)
    3589              :             CASE (2)
    3590              :                ! add V^SOC_y * σ_y for σ_y = ( (0,-i) (i,0) )
    3591          318 :                CALL cp_cfm_scale(gaussi, cfm_V_SOC_xyz_ikp)
    3592          318 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, 1, s)
    3593          318 :                CALL cp_cfm_scale(-z_one, cfm_V_SOC_xyz_ikp)
    3594          318 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, s, 1)
    3595              :             CASE (3)
    3596              :                ! add V^SOC_z * σ_z for σ_z = ( (1,0) (0,1) )
    3597          318 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, 1, 1)
    3598          318 :                CALL cp_cfm_scale(-z_one, cfm_V_SOC_xyz_ikp)
    3599         1272 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, s, s)
    3600              :             END SELECT
    3601              : 
    3602              :          END DO
    3603              : 
    3604              :       END DO ! ikp
    3605              : 
    3606           16 :       CALL cp_cfm_release(cfm_V_SOC_xyz_ikp)
    3607              : 
    3608           16 :       CALL timestop(handle)
    3609              : 
    3610           16 :    END SUBROUTINE H_KS_spinor_kp
    3611              : 
    3612              : ! **************************************************************************************************
    3613              : !> \brief ...
    3614              : !> \param cfm_array ...
    3615              : !> \param cfm_template ...
    3616              : !> \param n ...
    3617              : ! **************************************************************************************************
    3618           16 :    SUBROUTINE alloc_cfm_double_array_1d(cfm_array, cfm_template, n)
    3619              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:)       :: cfm_array
    3620              :       TYPE(cp_cfm_type)                                  :: cfm_template
    3621              :       INTEGER                                            :: n
    3622              : 
    3623              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'alloc_cfm_double_array_1d'
    3624              : 
    3625              :       INTEGER                                            :: handle, i
    3626              : 
    3627           16 :       CALL timeset(routineN, handle)
    3628              : 
    3629          366 :       ALLOCATE (cfm_array(n))
    3630          334 :       DO i = 1, n
    3631          318 :          CALL create_cfm_double(cfm_array(i), cfm_orig=cfm_template)
    3632          334 :          CALL cp_cfm_set_all(cfm_array(i), z_zero)
    3633              :       END DO
    3634              : 
    3635           16 :       CALL timestop(handle)
    3636              : 
    3637           16 :    END SUBROUTINE alloc_cfm_double_array_1d
    3638              : 
    3639              : ! **************************************************************************************************
    3640              : !> \brief ...
    3641              : !> \param bs_env ...
    3642              : ! **************************************************************************************************
    3643          182 :    SUBROUTINE get_all_VBM_CBM_bandgaps(bs_env)
    3644              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3645              : 
    3646              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_all_VBM_CBM_bandgaps'
    3647              : 
    3648              :       INTEGER                                            :: handle
    3649              : 
    3650          182 :       CALL timeset(routineN, handle)
    3651              : 
    3652          182 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_scf, bs_env%eigenval_scf, bs_env)
    3653          182 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_GW, bs_env%eigenval_GW, bs_env)
    3654          182 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_HF, bs_env%eigenval_HF, bs_env)
    3655              : 
    3656          182 :       CALL check_qp_gap_sanity(bs_env)
    3657              : 
    3658          182 :       CALL check_scf_gw_level_ordering(bs_env)
    3659              : 
    3660          182 :       CALL timestop(handle)
    3661              : 
    3662          182 :    END SUBROUTINE get_all_VBM_CBM_bandgaps
    3663              : 
    3664              : ! **************************************************************************************************
    3665              : !> \brief Warn if the GW fundamental band gap is inverted or implausibly large, i.e. if the
    3666              : !>        quasiparticle solve has produced a spectrum that cannot be physical.
    3667              : !> \param bs_env ...
    3668              : ! **************************************************************************************************
    3669          182 :    SUBROUTINE check_qp_gap_sanity(bs_env)
    3670              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3671              : 
    3672              :       CHARACTER(LEN=default_string_length)               :: gw_label
    3673              :       REAL(KIND=dp)                                      :: gap, gap_scf
    3674              : 
    3675          182 :       gw_label = gw_flavour_label(bs_env)
    3676              : 
    3677          182 :       gap = bs_env%band_edges_GW%IDBG
    3678          182 :       gap_scf = bs_env%band_edges_scf%IDBG
    3679              : 
    3680              :       ! requiring a healthy SCF gap keeps the inversion test from firing on a genuine metal
    3681          182 :       IF (gap < -eps_qp_gap .AND. gap_scf > eps_qp_gap) THEN
    3682              :          CALL cp_warn(__LOCATION__, &
    3683              :                       TRIM(gw_label)//" band gap is negative ("// &
    3684              :                       TRIM(ADJUSTL(cp_to_string(gap*evolt, '(F12.3)')))//" eV): the quasiparticle "// &
    3685            0 :                       "spectrum is inverted. Check numerical parameters.")
    3686          182 :       ELSE IF (ABS(gap) > max_qp_gap) THEN
    3687              :          CALL cp_warn(__LOCATION__, &
    3688              :                       TRIM(gw_label)//" band gap is implausibly large ("// &
    3689              :                       TRIM(ADJUSTL(cp_to_string(gap*evolt, '(F12.3)')))//" eV): the quasiparticle "// &
    3690            0 :                       "solve has likely diverged. Check numerical parameters.")
    3691              :       END IF
    3692              : 
    3693          182 :    END SUBROUTINE check_qp_gap_sanity
    3694              : 
    3695              : ! **************************************************************************************************
    3696              : !> \brief Check whether the GW correction reorders the frontier levels with respect to the SCF
    3697              : !>        eigenvalues, i.e. whether the SCF HOMO (LUMO) is a different level than the GW HOMO
    3698              : !>        (LUMO). If so, the SCF and GW band edges printed afterwards belong to different
    3699              : !>        orbitals, which is easily overlooked => warn.
    3700              : !>        Levels closer than eps_degeneracy in the SCF spectrum count as degenerate, i.e. their
    3701              : !>        reordering by GW is not reported.
    3702              : !> \param bs_env ...
    3703              : ! **************************************************************************************************
    3704          182 :    SUBROUTINE check_scf_gw_level_ordering(bs_env)
    3705              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3706              : 
    3707              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'check_scf_gw_level_ordering'
    3708              :       REAL(KIND=dp), PARAMETER                           :: eps_degeneracy = 1.0E-05_dp
    3709              : 
    3710              :       CHARACTER(LEN=default_string_length)               :: gw_label, level_gw, level_scf, &
    3711              :                                                             spin_string
    3712              :       CHARACTER(LEN=max_line_length)                     :: msg
    3713              :       INTEGER                                            :: handle, homo, ispin, n_mo
    3714              :       INTEGER, DIMENSION(2)                              :: loc_gw, loc_scf
    3715              : 
    3716          182 :       CALL timeset(routineN, handle)
    3717              : 
    3718          182 :       gw_label = gw_flavour_label(bs_env)
    3719              : 
    3720          182 :       n_mo = bs_env%n_ao
    3721              : 
    3722          400 :       DO ispin = 1, bs_env%n_spin
    3723              : 
    3724          218 :          homo = bs_env%n_occ(ispin)
    3725              : 
    3726          218 :          IF (bs_env%n_spin == 2) THEN
    3727           72 :             WRITE (spin_string, '(A,I0,A)') " (spin ", ispin, ")"
    3728              :          ELSE
    3729          146 :             spin_string = ""
    3730              :          END IF
    3731              : 
    3732              :          ! occupied levels: level carrying the SCF VBM versus level carrying the GW VBM
    3733         3828 :          loc_scf(:) = MAXLOC(bs_env%eigenval_scf(1:homo, :, ispin))
    3734         3828 :          loc_gw(:) = MAXLOC(bs_env%eigenval_GW(1:homo, :, ispin))
    3735              : 
    3736              :          ! only report a genuine reordering, not a permutation within a degenerate SCF shell
    3737          218 :          IF (bs_env%eigenval_scf(loc_scf(1), loc_scf(2), ispin) - &
    3738              :              bs_env%eigenval_scf(loc_gw(1), loc_gw(2), ispin) > eps_degeneracy) THEN
    3739              : 
    3740           14 :             CALL level_string(level_scf, loc_scf, bs_env)
    3741           14 :             CALL level_string(level_gw, loc_gw, bs_env)
    3742              : 
    3743              :             msg = TRIM(gw_label)//" reorders the occupied levels"//TRIM(spin_string)// &
    3744              :                   ": the SCF valence band maximum (SCF HOMO) is "//TRIM(level_scf)// &
    3745              :                   ", whereas the "//TRIM(gw_label)//" valence band maximum ("// &
    3746              :                   TRIM(gw_label)//" HOMO) is "//TRIM(level_gw)//". The SCF and "// &
    3747              :                   TRIM(gw_label)//" band edges printed below therefore belong to different "// &
    3748           14 :                   "orbitals; see the eigenvalues in the file bandstructure_SCF_and_G0W0."
    3749           14 :             CPWARN(TRIM(msg))
    3750              : 
    3751              :          END IF
    3752              : 
    3753              :          ! empty levels: level carrying the SCF CBM versus level carrying the GW CBM
    3754         7134 :          loc_scf(:) = MINLOC(bs_env%eigenval_scf(homo + 1:n_mo, :, ispin))
    3755         7134 :          loc_gw(:) = MINLOC(bs_env%eigenval_GW(homo + 1:n_mo, :, ispin))
    3756          218 :          loc_scf(1) = loc_scf(1) + homo
    3757          218 :          loc_gw(1) = loc_gw(1) + homo
    3758              : 
    3759          218 :          IF (bs_env%eigenval_scf(loc_gw(1), loc_gw(2), ispin) - &
    3760          182 :              bs_env%eigenval_scf(loc_scf(1), loc_scf(2), ispin) > eps_degeneracy) THEN
    3761              : 
    3762            0 :             CALL level_string(level_scf, loc_scf, bs_env)
    3763            0 :             CALL level_string(level_gw, loc_gw, bs_env)
    3764              : 
    3765              :             msg = TRIM(gw_label)//" reorders the empty levels"//TRIM(spin_string)// &
    3766              :                   ": the SCF conduction band minimum (SCF LUMO) is "//TRIM(level_scf)// &
    3767              :                   ", whereas the "//TRIM(gw_label)//" conduction band minimum ("// &
    3768              :                   TRIM(gw_label)//" LUMO) is "//TRIM(level_gw)//". The SCF and "// &
    3769              :                   TRIM(gw_label)//" band edges printed below therefore belong to different "// &
    3770            0 :                   "orbitals; see the eigenvalues in the file bandstructure_SCF_and_G0W0."
    3771            0 :             CPWARN(TRIM(msg))
    3772              : 
    3773              :          END IF
    3774              : 
    3775              :       END DO
    3776              : 
    3777          182 :       CALL timestop(handle)
    3778              : 
    3779          182 :    END SUBROUTINE check_scf_gw_level_ordering
    3780              : 
    3781              : ! **************************************************************************************************
    3782              : !> \brief Human readable name of the level (i_mo, ikp) given as loc = [i_mo, ikp]
    3783              : !> \param string ...
    3784              : !> \param loc ...
    3785              : !> \param bs_env ...
    3786              : ! **************************************************************************************************
    3787           28 :    SUBROUTINE level_string(string, loc, bs_env)
    3788              : 
    3789              :       CHARACTER(LEN=*), INTENT(OUT)                      :: string
    3790              :       INTEGER, DIMENSION(2), INTENT(IN)                  :: loc
    3791              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3792              : 
    3793           28 :       IF (bs_env%nkp_bs_and_DOS > 1) THEN
    3794           28 :          WRITE (string, '(A,I0,A,I0)') "level ", loc(1), " at k-point ", loc(2)
    3795              :       ELSE
    3796            0 :          WRITE (string, '(A,I0)') "level ", loc(1)
    3797              :       END IF
    3798              : 
    3799           28 :    END SUBROUTINE level_string
    3800              : 
    3801              : ! **************************************************************************************************
    3802              : !> \brief ...
    3803              : !> \param band_edges ...
    3804              : !> \param ev ...
    3805              : !> \param bs_env ...
    3806              : ! **************************************************************************************************
    3807          578 :    SUBROUTINE get_VBM_CBM_bandgaps(band_edges, ev, bs_env)
    3808              :       TYPE(band_edges_type)                              :: band_edges
    3809              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: ev
    3810              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3811              : 
    3812              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_VBM_CBM_bandgaps'
    3813              : 
    3814              :       INTEGER                                            :: handle, homo, homo_1, homo_2, ikp, &
    3815              :                                                             ispin, lumo, lumo_1, lumo_2, n_mo
    3816              :       REAL(KIND=dp)                                      :: E_DBG_at_ikp
    3817              : 
    3818          578 :       CALL timeset(routineN, handle)
    3819              : 
    3820          578 :       n_mo = bs_env%n_ao
    3821              : 
    3822          578 :       band_edges%DBG = 1000.0_dp
    3823              : 
    3824         1048 :       SELECT CASE (bs_env%n_spin)
    3825              :       CASE (1)
    3826          470 :          homo = bs_env%n_occ(1)
    3827          470 :          lumo = homo + 1
    3828        10772 :          band_edges%VBM = MAXVAL(ev(1:homo, :, 1))
    3829        23116 :          band_edges%CBM = MINVAL(ev(homo + 1:n_mo, :, 1))
    3830              :       CASE (2)
    3831          108 :          homo_1 = bs_env%n_occ(1)
    3832          108 :          lumo_1 = homo_1 + 1
    3833          108 :          homo_2 = bs_env%n_occ(2)
    3834          108 :          lumo_2 = homo_2 + 1
    3835         1668 :          band_edges%VBM = MAX(MAXVAL(ev(1:homo_1, :, 1)), MAXVAL(ev(1:homo_2, :, 2)))
    3836         1632 :          band_edges%CBM = MIN(MINVAL(ev(homo_1 + 1:n_mo, :, 1)), MINVAL(ev(homo_2 + 1:n_mo, :, 2)))
    3837              :       CASE DEFAULT
    3838          578 :          CPABORT("Error with number of spins.")
    3839              :       END SELECT
    3840              : 
    3841          578 :       band_edges%IDBG = band_edges%CBM - band_edges%VBM
    3842              : 
    3843         1264 :       DO ispin = 1, bs_env%n_spin
    3844              : 
    3845          686 :          homo = bs_env%n_occ(ispin)
    3846              : 
    3847         4366 :          DO ikp = 1, bs_env%nkp_bs_and_DOS
    3848              : 
    3849        32930 :             E_DBG_at_ikp = -MAXVAL(ev(1:homo, ikp, ispin)) + MINVAL(ev(homo + 1:n_mo, ikp, ispin))
    3850              : 
    3851         3788 :             IF (E_DBG_at_ikp < band_edges%DBG) band_edges%DBG = E_DBG_at_ikp
    3852              : 
    3853              :          END DO
    3854              : 
    3855              :       END DO
    3856          470 :       SELECT CASE (bs_env%n_spin)
    3857              :       CASE (1)
    3858              :          CALL get_fermi_level_1c(bs_env, ev(:, :, 1), &
    3859              :                                  REAL(bs_env%n_electron(1), KIND=dp), &
    3860          470 :                                  bs_env%spin_degeneracy, band_edges%VBM, band_edges%CBM, band_edges%E_fermi)
    3861              :       CASE (2)
    3862              :          CALL get_fermi_level_2c(bs_env, ev(:, :, 1:2), &
    3863              :                                  REAL(bs_env%n_electron(1) + bs_env%n_electron(2), KIND=dp), &
    3864          578 :                                  band_edges%VBM, band_edges%CBM, band_edges%E_fermi)
    3865              :       END SELECT
    3866              : 
    3867          578 :       CALL timestop(handle)
    3868              : 
    3869          578 :    END SUBROUTINE get_VBM_CBM_bandgaps
    3870              : 
    3871              : ! **************************************************************************************************
    3872              : !> \brief Fermi level (chemical potential) for one set of band energies on the (weighted)
    3873              : !>        DOS k-point mesh bs_env%kpoints_DOS (band-structure-path k-points carry wkp=0 and
    3874              : !>        therefore do not contribute). Used both for a single spin-degenerate channel
    3875              : !>        (n_spin=1, maxocc=bs_env%spin_degeneracy=2) and for the spin-orbit-coupled spinor
    3876              : !>        space, where the two spins are already merged into one set of 2*n_ao states, each
    3877              : !>        holding at most one electron (maxocc=1).
    3878              : !>        IMPORTANT: Smearkp's T=0 bisection does *not* converge to the gap midpoint for a
    3879              : !>        real (flat) gap: N(mu) is exactly n_el for every mu in [VBM,CBM), so the bisection
    3880              : !>        exits on its very first probe, wherever that lands inside the gap, not at the
    3881              : !>        midpoint. Therefore Smearkp is only invoked when bs_env%do_smear is active (a
    3882              : !>        genuine metal, smeared with the ground-state SCF's &SMEAR settings); otherwise
    3883              : !>        E_fermi = VBM (highest occupied eigenvalue), matching the convention ABINIT uses
    3884              : !>        for its fixed-occupation-number case (occopt<3), rather than the gap midpoint.
    3885              : !> \param bs_env ...
    3886              : !> \param eigenval band energies (n_states, n_kpoints)
    3887              : !> \param n_el target (weighted) number of occupied states
    3888              : !> \param maxocc maximum occupation per state
    3889              : !> \param VBM valence band maximum, used as E_fermi when bs_env%do_smear = .FALSE.
    3890              : !> \param CBM conduction band minimum, used only to detect a wide gap
    3891              : !> \param e_fermi ...
    3892              : ! **************************************************************************************************
    3893          654 :    SUBROUTINE get_fermi_level_1c(bs_env, eigenval, n_el, maxocc, VBM, CBM, e_fermi)
    3894              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3895              :       REAL(KIND=dp), DIMENSION(:, :)                     :: eigenval
    3896              :       REAL(KIND=dp)                                      :: n_el, maxocc, VBM, CBM, e_fermi
    3897              : 
    3898              :       REAL(KIND=dp), PARAMETER                           :: gap_smear_ratio = 20.0_dp
    3899              : 
    3900              :       REAL(KIND=dp)                                      :: kTS
    3901          654 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: f_occ
    3902              : 
    3903          654 :       IF (bs_env%do_smear .AND. .NOT. (CBM > VBM .AND. &
    3904              :                                        CBM - VBM > gap_smear_ratio*bs_env%smear_width)) THEN
    3905           16 :          ALLOCATE (f_occ(SIZE(eigenval, 1), SIZE(eigenval, 2)))
    3906              :          CALL Smearkp(f_occ, e_fermi, kTS, eigenval, n_el, &
    3907              :                       bs_env%kpoints_DOS%wkp(1:SIZE(eigenval, 2)), &
    3908            4 :                       bs_env%smear_width, maxocc, bs_env%smear_method)
    3909            4 :          DEALLOCATE (f_occ)
    3910              :       ELSE
    3911          650 :          e_fermi = VBM
    3912              :       END IF
    3913              : 
    3914          654 :    END SUBROUTINE get_fermi_level_1c
    3915              : 
    3916              : ! **************************************************************************************************
    3917              : !> \brief Fermi level (chemical potential) shared across two spin channels with independent
    3918              : !>        band energies but the same chemical potential (standard spin-polarized DFT); see
    3919              : !>        get_fermi_level_1c for the k-point weighting and for why Smearkp2 is only used
    3920              : !>        when bs_env%do_smear is active (E_fermi = VBM otherwise).
    3921              : !> \param bs_env ...
    3922              : !> \param eigenval band energies (n_states, n_kpoints, 2)
    3923              : !> \param n_el target (weighted) total number of occupied states, summed over both spins
    3924              : !> \param VBM valence band maximum, used as E_fermi when bs_env%do_smear = .FALSE.
    3925              : !> \param CBM conduction band minimum, used only to detect a wide gap
    3926              : !> \param e_fermi ...
    3927              : ! **************************************************************************************************
    3928          126 :    SUBROUTINE get_fermi_level_2c(bs_env, eigenval, n_el, VBM, CBM, e_fermi)
    3929              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3930              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: eigenval
    3931              :       REAL(KIND=dp)                                      :: n_el, VBM, CBM, e_fermi
    3932              : 
    3933              :       REAL(KIND=dp), PARAMETER                           :: gap_smear_ratio = 20.0_dp
    3934              : 
    3935              :       REAL(KIND=dp)                                      :: kTS
    3936          126 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: f_occ
    3937              : 
    3938          126 :       IF (bs_env%do_smear .AND. .NOT. (CBM > VBM .AND. &
    3939              :                                        CBM - VBM > gap_smear_ratio*bs_env%smear_width)) THEN
    3940            0 :          ALLOCATE (f_occ(SIZE(eigenval, 1), SIZE(eigenval, 2), 2))
    3941              :          CALL Smearkp2(f_occ, e_fermi, kTS, eigenval, n_el, &
    3942              :                        bs_env%kpoints_DOS%wkp(1:SIZE(eigenval, 2)), &
    3943            0 :                        bs_env%smear_width, bs_env%smear_method)
    3944            0 :          DEALLOCATE (f_occ)
    3945              :       ELSE
    3946          126 :          e_fermi = VBM
    3947              :       END IF
    3948              : 
    3949          126 :    END SUBROUTINE get_fermi_level_2c
    3950              : 
    3951              : ! **************************************************************************************************
    3952              : !> \brief Name of the GW flavour that was requested, for printing
    3953              : !> \param bs_env ...
    3954              : !> \return ...
    3955              : ! **************************************************************************************************
    3956         1403 :    FUNCTION gw_flavour_label(bs_env) RESULT(label)
    3957              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    3958              :       CHARACTER(LEN=default_string_length)               :: label
    3959              : 
    3960         1494 :       SELECT CASE (bs_env%gw_flavour)
    3961              :       CASE (evGW0)
    3962           91 :          label = "evGW0"
    3963              :       CASE DEFAULT
    3964         1403 :          label = "G0W0"
    3965              :       END SELECT
    3966         1403 :       IF (bs_env%do_static_cohsex) label = "static COHSEX"
    3967              : 
    3968         1403 :    END FUNCTION gw_flavour_label
    3969              : 
    3970              : END MODULE post_scf_bandstructure_utils
        

Generated by: LCOV version 2.0-1