LCOV - code coverage report
Current view: top level - src - post_scf_bandstructure_utils.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:c24029e) Lines: 94.0 % 1173 1103
Test Date: 2026-07-04 06:36:57 Functions: 97.3 % 37 36

            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 cell_types,                      ONLY: cell_type,&
      18              :                                               get_cell,&
      19              :                                               pbc
      20              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      21              :    USE cp_cfm_basic_linalg,             ONLY: cp_cfm_scale
      22              :    USE cp_cfm_cholesky,                 ONLY: cp_cfm_cholesky_decompose
      23              :    USE cp_cfm_diag,                     ONLY: cp_cfm_geeig,&
      24              :                                               cp_cfm_geeig_canon,&
      25              :                                               cp_cfm_heevd
      26              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      27              :                                               cp_cfm_get_info,&
      28              :                                               cp_cfm_release,&
      29              :                                               cp_cfm_set_all,&
      30              :                                               cp_cfm_to_cfm,&
      31              :                                               cp_cfm_to_fm,&
      32              :                                               cp_cfm_type,&
      33              :                                               cp_fm_to_cfm
      34              :    USE cp_control_types,                ONLY: dft_control_type
      35              :    USE cp_dbcsr_api,                    ONLY: &
      36              :         dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, dbcsr_p_type, dbcsr_set, &
      37              :         dbcsr_type, dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
      38              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_alloc_block_from_nbl
      39              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      40              :                                               copy_fm_to_dbcsr,&
      41              :                                               dbcsr_allocate_matrix_set,&
      42              :                                               dbcsr_deallocate_matrix_set
      43              :    USE cp_files,                        ONLY: close_file,&
      44              :                                               open_file
      45              :    USE cp_fm_diag,                      ONLY: cp_fm_geeig_canon
      46              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      47              :                                               cp_fm_struct_release,&
      48              :                                               cp_fm_struct_type
      49              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      50              :                                               cp_fm_get_diag,&
      51              :                                               cp_fm_get_info,&
      52              :                                               cp_fm_release,&
      53              :                                               cp_fm_set_all,&
      54              :                                               cp_fm_to_fm,&
      55              :                                               cp_fm_type
      56              :    USE cp_log_handling,                 ONLY: cp_logger_get_default_io_unit
      57              :    USE cp_parser_methods,               ONLY: read_float_object
      58              :    USE input_constants,                 ONLY: int_ldos_z,&
      59              :                                               large_cell_Gamma,&
      60              :                                               large_cell_Gamma_ri_rs,&
      61              :                                               non_periodic_ri_rs,&
      62              :                                               small_cell_full_kp
      63              :    USE input_section_types,             ONLY: section_vals_get,&
      64              :                                               section_vals_get_subs_vals,&
      65              :                                               section_vals_type,&
      66              :                                               section_vals_val_get
      67              :    USE kinds,                           ONLY: default_string_length,&
      68              :                                               dp,&
      69              :                                               max_line_length
      70              :    USE kpoint_methods,                  ONLY: kpoint_init_cell_index,&
      71              :                                               rskp_transform
      72              :    USE kpoint_types,                    ONLY: get_kpoint_info,&
      73              :                                               kpoint_create,&
      74              :                                               kpoint_type
      75              :    USE machine,                         ONLY: m_walltime
      76              :    USE mathconstants,                   ONLY: gaussi,&
      77              :                                               twopi,&
      78              :                                               z_one,&
      79              :                                               z_zero
      80              :    USE message_passing,                 ONLY: mp_para_env_type
      81              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      82              :    USE particle_types,                  ONLY: particle_type
      83              :    USE physcon,                         ONLY: angstrom,&
      84              :                                               evolt
      85              :    USE post_scf_bandstructure_types,    ONLY: band_edges_type,&
      86              :                                               post_scf_bandstructure_type
      87              :    USE pw_env_types,                    ONLY: pw_env_get,&
      88              :                                               pw_env_type
      89              :    USE pw_pool_types,                   ONLY: pw_pool_type
      90              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      91              :                                               pw_r3d_rs_type
      92              :    USE qs_collocate_density,            ONLY: calculate_rho_elec
      93              :    USE qs_environment_types,            ONLY: get_qs_env,&
      94              :                                               qs_environment_type
      95              :    USE qs_ks_types,                     ONLY: qs_ks_env_type
      96              :    USE qs_mo_types,                     ONLY: get_mo_set,&
      97              :                                               mo_set_type
      98              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
      99              :    USE rpa_gw_im_time_util,             ONLY: compute_weight_re_im,&
     100              :                                               get_atom_index_from_basis_function_index
     101              :    USE scf_control_types,               ONLY: scf_control_type
     102              :    USE soc_pseudopotential_methods,     ONLY: V_SOC_xyz_from_pseudopotential,&
     103              :                                               remove_soc_outside_energy_window_mo
     104              :    USE soc_pseudopotential_utils,       ONLY: add_cfm_submat,&
     105              :                                               add_dbcsr_submat,&
     106              :                                               cfm_add_on_diag,&
     107              :                                               create_cfm_double,&
     108              :                                               get_cfm_submat
     109              :    USE string_utilities,                ONLY: uppercase
     110              : #include "base/base_uses.f90"
     111              : 
     112              :    IMPLICIT NONE
     113              : 
     114              :    PRIVATE
     115              : 
     116              :    PUBLIC :: create_and_init_bs_env, &
     117              :              eval_bandstructure_properties, cfm_ikp_from_fm_Gamma, &
     118              :              MIC_contribution_from_ikp, compute_xkp, kpoint_init_cell_index_simple, &
     119              :              rsmat_to_kp, soc, get_VBM_CBM_bandgaps, get_all_VBM_CBM_bandgaps
     120              : 
     121              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'post_scf_bandstructure_utils'
     122              : 
     123              : CONTAINS
     124              : 
     125              : ! **************************************************************************************************
     126              : !> \brief ...
     127              : !> \param qs_env ...
     128              : !> \param bs_env ...
     129              : !> \param post_scf_bandstructure_section ...
     130              : ! **************************************************************************************************
     131           44 :    SUBROUTINE create_and_init_bs_env(qs_env, bs_env, post_scf_bandstructure_section)
     132              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     133              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     134              :       TYPE(section_vals_type), POINTER                   :: post_scf_bandstructure_section
     135              : 
     136              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'create_and_init_bs_env'
     137              : 
     138              :       INTEGER                                            :: handle
     139              : 
     140           44 :       CALL timeset(routineN, handle)
     141              : 
     142         4312 :       ALLOCATE (bs_env)
     143              : 
     144           44 :       CALL print_header(bs_env)
     145              : 
     146           44 :       CALL read_bandstructure_input_parameters(bs_env, post_scf_bandstructure_section, qs_env)
     147              : 
     148           44 :       CALL get_parameters_from_qs_env(qs_env, bs_env)
     149              : 
     150           44 :       CALL set_heuristic_parameters(bs_env)
     151              : 
     152           78 :       SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
     153              :       CASE (large_cell_Gamma, large_cell_Gamma_ri_rs, non_periodic_ri_rs)
     154              : 
     155           34 :          CALL setup_kpoints_DOS_large_cell_Gamma(qs_env, bs_env, bs_env%kpoints_DOS)
     156              : 
     157           34 :          CALL allocate_and_fill_fm_ks_fm_s(qs_env, bs_env)
     158              : 
     159           34 :          CALL diagonalize_ks_matrix(bs_env)
     160              : 
     161           34 :          CALL check_positive_definite_overlap_mat(bs_env, qs_env)
     162              : 
     163              :       CASE (small_cell_full_kp)
     164              : 
     165           10 :          CALL setup_kpoints_scf_desymm(qs_env, bs_env, bs_env%kpoints_scf_desymm, .TRUE.)
     166           10 :          CALL setup_kpoints_scf_desymm(qs_env, bs_env, bs_env%kpoints_scf_desymm_2, .FALSE.)
     167              : 
     168           10 :          CALL setup_kpoints_DOS_small_cell_full_kp(bs_env, bs_env%kpoints_DOS)
     169              : 
     170           10 :          CALL allocate_and_fill_fm_ks_fm_s(qs_env, bs_env)
     171              : 
     172           54 :          CALL compute_cfm_mo_coeff_kp_and_eigenval_scf_kp(qs_env, bs_env)
     173              : 
     174              :       END SELECT
     175              : 
     176           44 :       CALL timestop(handle)
     177              : 
     178           44 :    END SUBROUTINE create_and_init_bs_env
     179              : 
     180              : ! **************************************************************************************************
     181              : !> \brief ...
     182              : !> \param bs_env ...
     183              : !> \param bs_sec ...
     184              : !> \param qs_env ...
     185              : ! **************************************************************************************************
     186           44 :    SUBROUTINE read_bandstructure_input_parameters(bs_env, bs_sec, qs_env)
     187              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     188              :       TYPE(section_vals_type), POINTER                   :: bs_sec
     189              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     190              : 
     191              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'read_bandstructure_input_parameters'
     192              : 
     193              :       CHARACTER(LEN=default_string_length)               :: ustr
     194              :       CHARACTER(LEN=default_string_length), &
     195           44 :          DIMENSION(:), POINTER                           :: string_ptr
     196              :       CHARACTER(LEN=max_line_length)                     :: error_msg
     197              :       INTEGER                                            :: handle, i, ikp
     198              :       REAL(KIND=dp), DIMENSION(3)                        :: kpptr
     199              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: cart_hmat
     200              :       TYPE(cell_type), POINTER                           :: cell
     201              :       TYPE(section_vals_type), POINTER                   :: dos_pdos_sec, floquet_sec, gw_sec, &
     202              :                                                             kp_bs_sec, ldos_sec, soc_sec
     203              : 
     204           44 :       CALL timeset(routineN, handle)
     205           44 :       NULLIFY (cell)
     206           44 :       CALL get_qs_env(qs_env=qs_env, cell=cell)
     207          572 :       cart_hmat(:, :) = cell%hmat(:, :)
     208           44 :       IF (cell%input_cell_canonicalized) cart_hmat(:, :) = cell%input_hmat(:, :)
     209              : 
     210           44 :       NULLIFY (gw_sec)
     211           44 :       gw_sec => section_vals_get_subs_vals(bs_sec, "GW")
     212           44 :       CALL section_vals_get(gw_sec, explicit=bs_env%do_gw)
     213           44 :       CALL section_vals_val_get(gw_sec, "RI_RS", l_val=bs_env%do_gw_ri_rs)
     214              : 
     215           44 :       NULLIFY (soc_sec)
     216           44 :       soc_sec => section_vals_get_subs_vals(bs_sec, "SOC")
     217           44 :       CALL section_vals_get(soc_sec, explicit=bs_env%do_soc)
     218              : 
     219           44 :       CALL section_vals_val_get(soc_sec, "ENERGY_WINDOW", r_val=bs_env%energy_window_soc)
     220              : 
     221           44 :       NULLIFY (dos_pdos_sec)
     222           44 :       dos_pdos_sec => section_vals_get_subs_vals(bs_sec, "DOS")
     223           44 :       CALL section_vals_get(dos_pdos_sec, explicit=bs_env%do_dos_pdos)
     224              : 
     225           44 :       CALL section_vals_val_get(bs_sec, "DOS%KPOINTS", i_vals=bs_env%nkp_grid_DOS_input)
     226           44 :       CALL section_vals_val_get(bs_sec, "DOS%ENERGY_WINDOW", r_val=bs_env%energy_window_DOS)
     227           44 :       CALL section_vals_val_get(bs_sec, "DOS%ENERGY_STEP", r_val=bs_env%energy_step_DOS)
     228           44 :       CALL section_vals_val_get(bs_sec, "DOS%BROADENING", r_val=bs_env%broadening_DOS)
     229              : 
     230           44 :       NULLIFY (ldos_sec)
     231           44 :       ldos_sec => section_vals_get_subs_vals(bs_sec, "DOS%LDOS")
     232           44 :       CALL section_vals_get(ldos_sec, explicit=bs_env%do_ldos)
     233              : 
     234           44 :       CALL section_vals_val_get(ldos_sec, "INTEGRATION", i_val=bs_env%int_ldos_xyz)
     235           44 :       CALL section_vals_val_get(ldos_sec, "BIN_MESH", i_vals=bs_env%bin_mesh)
     236              : 
     237           44 :       NULLIFY (kp_bs_sec)
     238           44 :       kp_bs_sec => section_vals_get_subs_vals(bs_sec, "BANDSTRUCTURE_PATH")
     239           44 :       CALL section_vals_val_get(kp_bs_sec, "NPOINTS", i_val=bs_env%input_kp_bs_npoints)
     240           44 :       CALL section_vals_val_get(kp_bs_sec, "UNITS", c_val=ustr)
     241           44 :       CALL uppercase(ustr)
     242           44 :       CALL section_vals_val_get(kp_bs_sec, "SPECIAL_POINT", n_rep_val=bs_env%input_kp_bs_n_sp_pts)
     243              : 
     244           44 :       NULLIFY (floquet_sec)
     245           44 :       floquet_sec => section_vals_get_subs_vals(bs_sec, "FLOQUET")
     246           44 :       CALL section_vals_get(floquet_sec, explicit=bs_env%do_floquet)
     247           44 :       CALL section_vals_val_get(floquet_sec, "AMPLITUDE", r_val=bs_env%floquet_amplitude)
     248           44 :       CALL section_vals_val_get(floquet_sec, "FREQUENCY", r_val=bs_env%floquet_omega)
     249           44 :       CALL section_vals_val_get(floquet_sec, "POLARISATION", r_vals=bs_env%floquet_polarisation)
     250           44 :       CALL section_vals_val_get(floquet_sec, "PHASE_OFFSETS", r_vals=bs_env%floquet_phi)
     251           44 :       CALL section_vals_val_get(floquet_sec, "MAX_FLOQUET_INDEX", i_val=bs_env%max_floquet_index)
     252           44 :       CALL section_vals_val_get(floquet_sec, "EPS_FLOQUET", r_val=bs_env%eps_floquet)
     253           44 :       CALL section_vals_val_get(floquet_sec, "ENERGY_WINDOW", r_val=bs_env%energy_window_floquet)
     254           44 :       CALL section_vals_val_get(floquet_sec, "ENERGY_STEP", r_val=bs_env%energy_step_floquet)
     255           44 :       CALL section_vals_val_get(floquet_sec, "BROADENING", r_val=bs_env%broadening_floquet)
     256           44 :       CALL section_vals_val_get(floquet_sec, "FLOQUET_DOS_FILE_NAME", c_val=bs_env%floquet_dos_file)
     257           44 :       CALL section_vals_val_get(floquet_sec, "QUASI_ENERGIES_FILE_NAME", c_val=bs_env%floquet_qe_file)
     258              : 
     259              :       ! read special points for band structure
     260           92 :       ALLOCATE (bs_env%xkp_special(3, bs_env%input_kp_bs_n_sp_pts))
     261           54 :       DO ikp = 1, bs_env%input_kp_bs_n_sp_pts
     262           10 :          CALL section_vals_val_get(kp_bs_sec, "SPECIAL_POINT", i_rep_val=ikp, c_vals=string_ptr)
     263           10 :          CPASSERT(SIZE(string_ptr(:), 1) == 4)
     264           40 :          DO i = 1, 3
     265           30 :             CALL read_float_object(string_ptr(i + 1), kpptr(i), error_msg)
     266           40 :             IF (LEN_TRIM(error_msg) > 0) CPABORT(TRIM(error_msg))
     267              :          END DO
     268           44 :          SELECT CASE (ustr)
     269              :          CASE ("B_VECTOR")
     270           40 :             bs_env%xkp_special(1:3, ikp) = kpptr(1:3)
     271              :          CASE ("CART_ANGSTROM")
     272              :             bs_env%xkp_special(1:3, ikp) = (kpptr(1)*cart_hmat(1, 1:3) + &
     273              :                                             kpptr(2)*cart_hmat(2, 1:3) + &
     274            0 :                                             kpptr(3)*cart_hmat(3, 1:3))/twopi*angstrom
     275              :          CASE ("CART_BOHR")
     276              :             bs_env%xkp_special(1:3, ikp) = (kpptr(1)*cart_hmat(1, 1:3) + &
     277              :                                             kpptr(2)*cart_hmat(2, 1:3) + &
     278            0 :                                             kpptr(3)*cart_hmat(3, 1:3))/twopi
     279              :          CASE DEFAULT
     280           10 :             CPABORT("Unknown unit <"//TRIM(ustr)//"> specified for k-point definition")
     281              :          END SELECT
     282              :       END DO
     283              : 
     284           44 :       CALL timestop(handle)
     285              : 
     286           44 :    END SUBROUTINE read_bandstructure_input_parameters
     287              : 
     288              : ! **************************************************************************************************
     289              : !> \brief ...
     290              : !> \param bs_env ...
     291              : ! **************************************************************************************************
     292           44 :    SUBROUTINE print_header(bs_env)
     293              : 
     294              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     295              : 
     296              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'print_header'
     297              : 
     298              :       INTEGER                                            :: handle, u
     299              : 
     300           44 :       CALL timeset(routineN, handle)
     301              : 
     302           44 :       bs_env%unit_nr = cp_logger_get_default_io_unit()
     303              : 
     304           44 :       u = bs_env%unit_nr
     305              : 
     306           44 :       IF (u > 0) THEN
     307           22 :          WRITE (u, '(T2,A)') ' '
     308           22 :          WRITE (u, '(T2,A)') REPEAT('-', 79)
     309           22 :          WRITE (u, '(T2,A,A78)') '-', '-'
     310           22 :          WRITE (u, '(T2,A,A51,A27)') '-', 'BANDSTRUCTURE CALCULATION', '-'
     311           22 :          WRITE (u, '(T2,A,A78)') '-', '-'
     312           22 :          WRITE (u, '(T2,A)') REPEAT('-', 79)
     313           22 :          WRITE (u, '(T2,A)') ' '
     314              :       END IF
     315              : 
     316           44 :       CALL timestop(handle)
     317              : 
     318           44 :    END SUBROUTINE print_header
     319              : 
     320              : ! **************************************************************************************************
     321              : !> \brief ...
     322              : !> \param qs_env ...
     323              : !> \param bs_env ...
     324              : !> \param kpoints ...
     325              : ! **************************************************************************************************
     326           34 :    SUBROUTINE setup_kpoints_DOS_large_cell_Gamma(qs_env, bs_env, kpoints)
     327              : 
     328              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     329              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     330              :       TYPE(kpoint_type), POINTER                         :: kpoints
     331              : 
     332              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_DOS_large_cell_Gamma'
     333              : 
     334              :       INTEGER                                            :: handle, i_dim, i_kp_in_line, &
     335              :                                                             i_special_kp, ikk, n_kp_in_line, &
     336              :                                                             n_special_kp, nkp, nkp_only_bs, &
     337              :                                                             nkp_only_DOS, u
     338              :       INTEGER, DIMENSION(3)                              :: nkp_grid, periodic
     339              : 
     340           34 :       CALL timeset(routineN, handle)
     341              : 
     342              :       ! routine adapted from mp2_integrals.F
     343           34 :       NULLIFY (kpoints)
     344           34 :       CALL kpoint_create(kpoints)
     345              : 
     346           34 :       kpoints%kp_scheme = "GENERAL"
     347              : 
     348           34 :       n_special_kp = bs_env%input_kp_bs_n_sp_pts
     349           34 :       n_kp_in_line = bs_env%input_kp_bs_npoints
     350              : 
     351          136 :       periodic(1:3) = bs_env%periodic(1:3)
     352              : 
     353          136 :       DO i_dim = 1, 3
     354              : 
     355          102 :          CPASSERT(periodic(i_dim) == 0 .OR. periodic(i_dim) == 1)
     356              : 
     357          136 :          IF (bs_env%nkp_grid_DOS_input(i_dim) < 0) THEN
     358           84 :             IF (periodic(i_dim) == 1) nkp_grid(i_dim) = 2
     359           84 :             IF (periodic(i_dim) == 0) nkp_grid(i_dim) = 1
     360              :          ELSE
     361           18 :             nkp_grid(i_dim) = bs_env%nkp_grid_DOS_input(i_dim)
     362              :          END IF
     363              : 
     364              :       END DO
     365              : 
     366              :       ! use the k <-> -k symmetry to reduce the number of kpoints
     367           34 :       IF (nkp_grid(1) > 1) THEN
     368            4 :          nkp_only_DOS = (nkp_grid(1) + 1)/2*nkp_grid(2)*nkp_grid(3)
     369           30 :       ELSE IF (nkp_grid(2) > 1) THEN
     370            4 :          nkp_only_DOS = nkp_grid(1)*(nkp_grid(2) + 1)/2*nkp_grid(3)
     371           26 :       ELSE IF (nkp_grid(3) > 1) THEN
     372            2 :          nkp_only_DOS = nkp_grid(1)*nkp_grid(2)*(nkp_grid(3) + 1)/2
     373              :       ELSE
     374           24 :          nkp_only_DOS = 1
     375              :       END IF
     376              : 
     377              :       ! we will compute the GW QP levels for all k's in the bandstructure path but also
     378              :       ! for all k-points from the SCF (e.g. for DOS or for self-consistent GW)
     379           34 :       IF (n_special_kp > 0) THEN
     380            0 :          nkp_only_bs = n_kp_in_line*(n_special_kp - 1) + 1
     381              :       ELSE
     382              :          nkp_only_bs = 0
     383              :       END IF
     384              : 
     385           34 :       nkp = nkp_only_DOS + nkp_only_bs
     386              : 
     387          136 :       kpoints%nkp_grid(1:3) = nkp_grid(1:3)
     388           34 :       kpoints%nkp = nkp
     389              : 
     390           34 :       bs_env%nkp_bs_and_DOS = nkp
     391           34 :       bs_env%nkp_only_bs = nkp_only_bs
     392           34 :       bs_env%nkp_only_DOS = nkp_only_DOS
     393              : 
     394          170 :       ALLOCATE (kpoints%xkp(3, nkp), kpoints%wkp(nkp))
     395           76 :       kpoints%wkp(1:nkp_only_DOS) = 1.0_dp/REAL(nkp_only_DOS, KIND=dp)
     396              : 
     397           34 :       CALL compute_xkp(kpoints%xkp, 1, nkp_only_DOS, nkp_grid)
     398              : 
     399           34 :       IF (n_special_kp > 0) THEN
     400            0 :          kpoints%xkp(1:3, nkp_only_DOS + 1) = bs_env%xkp_special(1:3, 1)
     401            0 :          ikk = nkp_only_DOS + 1
     402            0 :          DO i_special_kp = 2, n_special_kp
     403            0 :             DO i_kp_in_line = 1, n_kp_in_line
     404            0 :                ikk = ikk + 1
     405              :                kpoints%xkp(1:3, ikk) = bs_env%xkp_special(1:3, i_special_kp - 1) + &
     406              :                                        REAL(i_kp_in_line, KIND=dp)/REAL(n_kp_in_line, KIND=dp)* &
     407              :                                        (bs_env%xkp_special(1:3, i_special_kp) - &
     408            0 :                                         bs_env%xkp_special(1:3, i_special_kp - 1))
     409            0 :                kpoints%wkp(ikk) = 0.0_dp
     410              :             END DO
     411              :          END DO
     412              :       END IF
     413              : 
     414           34 :       CALL kpoint_init_cell_index_simple(kpoints, qs_env)
     415              : 
     416           34 :       u = bs_env%unit_nr
     417              : 
     418           34 :       IF (u > 0) THEN
     419           17 :          IF (nkp_only_bs > 0) THEN
     420              :             WRITE (u, FMT="(T2,1A,T77,I4)") &
     421            0 :                "Number of special k-points for the bandstructure", n_special_kp
     422            0 :             WRITE (u, FMT="(T2,1A,T77,I4)") "Number of k-points for the bandstructure", nkp
     423              :             WRITE (u, FMT="(T2,1A,T69,3I4)") &
     424            0 :                "K-point mesh for the density of states (DOS)", nkp_grid(1:3)
     425              :          ELSE
     426              :             WRITE (u, FMT="(T2,1A,T69,3I4)") &
     427           17 :                "K-point mesh for the density of states (DOS) and the self-energy", nkp_grid(1:3)
     428              :          END IF
     429              :       END IF
     430              : 
     431           34 :       CALL timestop(handle)
     432              : 
     433           34 :    END SUBROUTINE setup_kpoints_DOS_large_cell_Gamma
     434              : 
     435              : ! **************************************************************************************************
     436              : !> \brief ...
     437              : !> \param qs_env ...
     438              : !> \param bs_env ...
     439              : !> \param kpoints ...
     440              : !> \param do_print ...
     441              : ! **************************************************************************************************
     442           20 :    SUBROUTINE setup_kpoints_scf_desymm(qs_env, bs_env, kpoints, do_print)
     443              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     444              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     445              :       TYPE(kpoint_type), POINTER                         :: kpoints
     446              : 
     447              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_scf_desymm'
     448              : 
     449              :       INTEGER                                            :: handle, i_cell_x, i_dim, img, j_cell_y, &
     450              :                                                             k_cell_z, nimages, nkp, u
     451              :       INTEGER, DIMENSION(3)                              :: cell_grid, cixd, nkp_grid
     452              :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
     453              : 
     454              :       LOGICAL:: do_print
     455              : 
     456           20 :       CALL timeset(routineN, handle)
     457              : 
     458           20 :       NULLIFY (kpoints)
     459           20 :       CALL kpoint_create(kpoints)
     460              : 
     461           20 :       CALL get_qs_env(qs_env=qs_env, kpoints=kpoints_scf)
     462              : 
     463           80 :       nkp_grid(1:3) = kpoints_scf%nkp_grid(1:3)
     464           20 :       nkp = nkp_grid(1)*nkp_grid(2)*nkp_grid(3)
     465              : 
     466              :       ! we need in periodic directions at least 4 k-points in the SCF
     467           80 :       DO i_dim = 1, 3
     468           80 :          IF (bs_env%periodic(i_dim) == 1) THEN
     469           40 :             CPASSERT(nkp_grid(i_dim) >= 4)
     470              :          END IF
     471              :       END DO
     472              : 
     473           20 :       kpoints%kp_scheme = "GENERAL"
     474           80 :       kpoints%nkp_grid(1:3) = nkp_grid(1:3)
     475           20 :       kpoints%nkp = nkp
     476           20 :       bs_env%nkp_scf_desymm = nkp
     477              : 
     478           60 :       ALLOCATE (kpoints%xkp(1:3, nkp))
     479           20 :       CALL compute_xkp(kpoints%xkp, 1, nkp, nkp_grid)
     480              : 
     481           60 :       ALLOCATE (kpoints%wkp(nkp))
     482          340 :       kpoints%wkp(:) = 1.0_dp/REAL(nkp, KIND=dp)
     483              : 
     484              :       ! for example 4x3x6 kpoint grid -> 3x3x5 cell grid because we need the same number of
     485              :       ! neighbor cells on both sides of the unit cell
     486           80 :       cell_grid(1:3) = nkp_grid(1:3) - MODULO(nkp_grid(1:3) + 1, 2)
     487              : 
     488              :       ! cell index: for example for x: from -n_x/2 to +n_x/2, n_x: number of cells in x direction
     489           80 :       cixd(1:3) = cell_grid(1:3)/2
     490              : 
     491           20 :       nimages = cell_grid(1)*cell_grid(2)*cell_grid(3)
     492              : 
     493           20 :       bs_env%nimages_scf_desymm = nimages
     494           80 :       bs_env%cell_grid_scf_desymm(1:3) = cell_grid(1:3)
     495              : 
     496           20 :       IF (ASSOCIATED(kpoints%index_to_cell)) DEALLOCATE (kpoints%index_to_cell)
     497           20 :       IF (ASSOCIATED(kpoints%cell_to_index)) DEALLOCATE (kpoints%cell_to_index)
     498              : 
     499          100 :       ALLOCATE (kpoints%cell_to_index(-cixd(1):cixd(1), -cixd(2):cixd(2), -cixd(3):cixd(3)))
     500           60 :       ALLOCATE (kpoints%index_to_cell(3, nimages))
     501              : 
     502           20 :       img = 0
     503           56 :       DO i_cell_x = -cixd(1), cixd(1)
     504          164 :          DO j_cell_y = -cixd(2), cixd(2)
     505          324 :             DO k_cell_z = -cixd(3), cixd(3)
     506          180 :                img = img + 1
     507          180 :                kpoints%cell_to_index(i_cell_x, j_cell_y, k_cell_z) = img
     508          828 :                kpoints%index_to_cell(1:3, img) = [i_cell_x, j_cell_y, k_cell_z]
     509              :             END DO
     510              :          END DO
     511              :       END DO
     512              : 
     513           20 :       u = bs_env%unit_nr
     514           20 :       IF (u > 0 .AND. do_print) THEN
     515            5 :          WRITE (u, FMT="(T2,A,I49)") "Number of cells for G, χ, W, Σ", nimages
     516              :       END IF
     517              : 
     518           20 :       CALL timestop(handle)
     519              : 
     520           20 :    END SUBROUTINE setup_kpoints_scf_desymm
     521              : 
     522              : ! **************************************************************************************************
     523              : !> \brief ...
     524              : !> \param bs_env ...
     525              : !> \param kpoints ...
     526              : ! **************************************************************************************************
     527           10 :    SUBROUTINE setup_kpoints_DOS_small_cell_full_kp(bs_env, kpoints)
     528              : 
     529              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     530              :       TYPE(kpoint_type), POINTER                         :: kpoints
     531              : 
     532              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'setup_kpoints_DOS_small_cell_full_kp'
     533              : 
     534              :       INTEGER                                            :: handle, i_kp_in_line, i_special_kp, ikk, &
     535              :                                                             n_kp_in_line, n_special_kp, nkp, &
     536              :                                                             nkp_only_bs, nkp_scf_desymm, u
     537              : 
     538           10 :       CALL timeset(routineN, handle)
     539              : 
     540              :       ! routine adapted from mp2_integrals.F
     541           10 :       NULLIFY (kpoints)
     542           10 :       CALL kpoint_create(kpoints)
     543              : 
     544           10 :       n_special_kp = bs_env%input_kp_bs_n_sp_pts
     545           10 :       n_kp_in_line = bs_env%input_kp_bs_npoints
     546           10 :       nkp_scf_desymm = bs_env%nkp_scf_desymm
     547              : 
     548              :       ! we will compute the GW QP levels for all k's in the bandstructure path but also
     549              :       ! for all k-points from the SCF (e.g. for DOS or for self-consistent GW)
     550           10 :       IF (n_special_kp > 0) THEN
     551            4 :          nkp_only_bs = n_kp_in_line*(n_special_kp - 1) + 1
     552              :       ELSE
     553              :          nkp_only_bs = 0
     554              :       END IF
     555           10 :       nkp = nkp_only_bs + nkp_scf_desymm
     556              : 
     557           30 :       ALLOCATE (kpoints%xkp(3, nkp))
     558           30 :       ALLOCATE (kpoints%wkp(nkp))
     559              : 
     560           10 :       kpoints%nkp = nkp
     561              : 
     562           10 :       bs_env%nkp_bs_and_DOS = nkp
     563           10 :       bs_env%nkp_only_bs = nkp_only_bs
     564           10 :       bs_env%nkp_only_DOS = nkp_scf_desymm
     565              : 
     566         1300 :       kpoints%xkp(1:3, 1:nkp_scf_desymm) = bs_env%kpoints_scf_desymm%xkp(1:3, 1:nkp_scf_desymm)
     567          170 :       kpoints%wkp(1:nkp_scf_desymm) = 1.0_dp/REAL(nkp_scf_desymm, KIND=dp)
     568              : 
     569           10 :       IF (n_special_kp > 0) THEN
     570           32 :          kpoints%xkp(1:3, nkp_scf_desymm + 1) = bs_env%xkp_special(1:3, 1)
     571            4 :          ikk = nkp_scf_desymm + 1
     572           10 :          DO i_special_kp = 2, n_special_kp
     573           70 :             DO i_kp_in_line = 1, n_kp_in_line
     574           60 :                ikk = ikk + 1
     575              :                kpoints%xkp(1:3, ikk) = bs_env%xkp_special(1:3, i_special_kp - 1) + &
     576              :                                        REAL(i_kp_in_line, KIND=dp)/REAL(n_kp_in_line, KIND=dp)* &
     577              :                                        (bs_env%xkp_special(1:3, i_special_kp) - &
     578          480 :                                         bs_env%xkp_special(1:3, i_special_kp - 1))
     579           66 :                kpoints%wkp(ikk) = 0.0_dp
     580              :             END DO
     581              :          END DO
     582              :       END IF
     583              : 
     584           10 :       IF (ASSOCIATED(kpoints%index_to_cell)) DEALLOCATE (kpoints%index_to_cell)
     585              : 
     586           30 :       ALLOCATE (kpoints%index_to_cell(3, bs_env%nimages_scf_desymm))
     587          740 :       kpoints%index_to_cell(:, :) = bs_env%kpoints_scf_desymm%index_to_cell(:, :)
     588              : 
     589           10 :       u = bs_env%unit_nr
     590              : 
     591           10 :       IF (u > 0) THEN
     592            5 :          WRITE (u, FMT="(T2,1A,T77,I4)") "Number of special k-points for the bandstructure", &
     593           10 :             n_special_kp
     594            5 :          WRITE (u, FMT="(T2,1A,T77,I4)") "Number of k-points for the bandstructure", nkp
     595              :       END IF
     596              : 
     597           10 :       CALL timestop(handle)
     598              : 
     599           10 :    END SUBROUTINE setup_kpoints_DOS_small_cell_full_kp
     600              : 
     601              : ! **************************************************************************************************
     602              : !> \brief ...
     603              : !> \param qs_env ...
     604              : !> \param bs_env ...
     605              : ! **************************************************************************************************
     606           10 :    SUBROUTINE compute_cfm_mo_coeff_kp_and_eigenval_scf_kp(qs_env, bs_env)
     607              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     608              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     609              : 
     610              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_cfm_mo_coeff_kp_and_eigenval_scf_kp'
     611              : 
     612              :       INTEGER                                            :: handle, ikp, ispin, nkp_bs_and_DOS
     613           10 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index_scf
     614              :       REAL(KIND=dp)                                      :: CBM, VBM
     615              :       REAL(KIND=dp), DIMENSION(3)                        :: xkp
     616              :       TYPE(cp_cfm_type)                                  :: cfm_ks, cfm_mos, cfm_s
     617           10 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks, matrix_s
     618              :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
     619              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     620           10 :          POINTER                                         :: sab_nl
     621              : 
     622           10 :       CALL timeset(routineN, handle)
     623              : 
     624              :       CALL get_qs_env(qs_env, &
     625              :                       matrix_ks_kp=matrix_ks, &
     626              :                       matrix_s_kp=matrix_s, &
     627           10 :                       kpoints=kpoints_scf)
     628              : 
     629           10 :       NULLIFY (sab_nl)
     630           10 :       CALL get_kpoint_info(kpoints_scf, sab_nl=sab_nl, cell_to_index=cell_to_index_scf)
     631              : 
     632           10 :       CALL cp_cfm_create(cfm_ks, bs_env%cfm_work_mo%matrix_struct)
     633           10 :       CALL cp_cfm_create(cfm_s, bs_env%cfm_work_mo%matrix_struct)
     634           10 :       CALL cp_cfm_create(cfm_mos, bs_env%cfm_work_mo%matrix_struct)
     635              : 
     636              :       ! nkp_bs_and_DOS contains desymmetrized k-point mesh from SCF and k-points from GW bandstructure
     637           10 :       nkp_bs_and_DOS = bs_env%nkp_bs_and_DOS
     638              : 
     639           50 :       ALLOCATE (bs_env%eigenval_G0W0(bs_env%n_ao, nkp_bs_and_DOS, bs_env%n_spin))
     640           50 :       ALLOCATE (bs_env%eigenval_HF(bs_env%n_ao, nkp_bs_and_DOS, bs_env%n_spin))
     641          274 :       ALLOCATE (bs_env%cfm_mo_coeff_kp(nkp_bs_and_DOS, bs_env%n_spin))
     642          274 :       ALLOCATE (bs_env%cfm_ks_kp(nkp_bs_and_DOS, bs_env%n_spin))
     643          254 :       ALLOCATE (bs_env%cfm_s_kp(nkp_bs_and_DOS))
     644          234 :       DO ikp = 1, nkp_bs_and_DOS
     645          448 :       DO ispin = 1, bs_env%n_spin
     646          224 :          CALL cp_cfm_create(bs_env%cfm_mo_coeff_kp(ikp, ispin), bs_env%cfm_work_mo%matrix_struct)
     647          448 :          CALL cp_cfm_create(bs_env%cfm_ks_kp(ikp, ispin), bs_env%cfm_work_mo%matrix_struct)
     648              :       END DO
     649          234 :       CALL cp_cfm_create(bs_env%cfm_s_kp(ikp), bs_env%cfm_work_mo%matrix_struct)
     650              :       END DO
     651              : 
     652           20 :       DO ispin = 1, bs_env%n_spin
     653          234 :          DO ikp = 1, nkp_bs_and_DOS
     654              : 
     655          896 :             xkp(1:3) = bs_env%kpoints_DOS%xkp(1:3, ikp)
     656              : 
     657              :             ! h^KS^R -> h^KS(k)
     658          224 :             CALL rsmat_to_kp(matrix_ks, ispin, xkp, cell_to_index_scf, sab_nl, bs_env, cfm_ks)
     659              : 
     660              :             ! S^R -> S(k)
     661          224 :             CALL rsmat_to_kp(matrix_s, 1, xkp, cell_to_index_scf, sab_nl, bs_env, cfm_s)
     662              : 
     663              :             ! we store the complex KS matrix as fm matrix because the infrastructure for fm is
     664              :             ! much nicer compared to cfm
     665          224 :             CALL cp_cfm_to_cfm(cfm_ks, bs_env%cfm_ks_kp(ikp, ispin))
     666          224 :             CALL cp_cfm_to_cfm(cfm_s, bs_env%cfm_s_kp(ikp))
     667              : 
     668              :             ! Diagonalize KS-matrix via Rothaan-Hall equation:
     669              :             ! H^KS(k) C(k) = S(k) C(k) ε(k)
     670              :             CALL cp_cfm_geeig_canon(cfm_ks, cfm_s, cfm_mos, &
     671              :                                     bs_env%eigenval_scf(:, ikp, ispin), &
     672          224 :                                     bs_env%cfm_work_mo, bs_env%eps_eigval_mat_s)
     673              : 
     674              :             ! we store the complex MO coeff as fm matrix because the infrastructure for fm is
     675              :             ! much nicer compared to cfm
     676          234 :             CALL cp_cfm_to_cfm(cfm_mos, bs_env%cfm_mo_coeff_kp(ikp, ispin))
     677              : 
     678              :          END DO
     679              : 
     680          234 :          VBM = MAXVAL(bs_env%eigenval_scf(bs_env%n_occ(ispin), :, ispin))
     681          234 :          CBM = MINVAL(bs_env%eigenval_scf(bs_env%n_occ(ispin) + 1, :, ispin))
     682              : 
     683           20 :          bs_env%e_fermi(ispin) = 0.5_dp*(VBM + CBM)
     684              : 
     685              :       END DO
     686              : 
     687           10 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_scf, bs_env%eigenval_scf, bs_env)
     688              : 
     689           10 :       CALL cp_cfm_release(cfm_ks)
     690           10 :       CALL cp_cfm_release(cfm_s)
     691           10 :       CALL cp_cfm_release(cfm_mos)
     692              : 
     693           10 :       CALL timestop(handle)
     694              : 
     695           20 :    END SUBROUTINE compute_cfm_mo_coeff_kp_and_eigenval_scf_kp
     696              : 
     697              : ! **************************************************************************************************
     698              : !> \brief ...
     699              : !> \param mat_rs ...
     700              : !> \param ispin ...
     701              : !> \param xkp ...
     702              : !> \param cell_to_index_scf ...
     703              : !> \param sab_nl ...
     704              : !> \param bs_env ...
     705              : !> \param cfm_kp ...
     706              : !> \param imag_rs_mat ...
     707              : ! **************************************************************************************************
     708         1208 :    SUBROUTINE rsmat_to_kp(mat_rs, ispin, xkp, cell_to_index_scf, sab_nl, bs_env, cfm_kp, imag_rs_mat)
     709              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: mat_rs
     710              :       INTEGER                                            :: ispin
     711              :       REAL(KIND=dp), DIMENSION(3)                        :: xkp
     712              :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index_scf
     713              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     714              :          POINTER                                         :: sab_nl
     715              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     716              :       TYPE(cp_cfm_type)                                  :: cfm_kp
     717              :       LOGICAL, OPTIONAL                                  :: imag_rs_mat
     718              : 
     719              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'rsmat_to_kp'
     720              : 
     721              :       INTEGER                                            :: handle
     722              :       LOGICAL                                            :: imag_rs_mat_private
     723              :       TYPE(dbcsr_type), POINTER                          :: cmat, nsmat, rmat
     724              : 
     725         1208 :       CALL timeset(routineN, handle)
     726              : 
     727         1208 :       ALLOCATE (rmat, cmat, nsmat)
     728              : 
     729         1208 :       imag_rs_mat_private = .FALSE.
     730         1208 :       IF (PRESENT(imag_rs_mat)) imag_rs_mat_private = imag_rs_mat
     731              : 
     732          570 :       IF (imag_rs_mat_private) THEN
     733          570 :          CALL dbcsr_create(rmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_antisymmetric)
     734          570 :          CALL dbcsr_create(cmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
     735              :       ELSE
     736          638 :          CALL dbcsr_create(rmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
     737          638 :          CALL dbcsr_create(cmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_antisymmetric)
     738              :       END IF
     739         1208 :       CALL dbcsr_create(nsmat, template=mat_rs(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
     740         1208 :       CALL cp_dbcsr_alloc_block_from_nbl(rmat, sab_nl)
     741         1208 :       CALL cp_dbcsr_alloc_block_from_nbl(cmat, sab_nl)
     742              : 
     743         1208 :       CALL dbcsr_set(rmat, 0.0_dp)
     744         1208 :       CALL dbcsr_set(cmat, 0.0_dp)
     745              :       CALL rskp_transform(rmatrix=rmat, cmatrix=cmat, rsmat=mat_rs, ispin=ispin, &
     746         1208 :                           xkp=xkp, cell_to_index=cell_to_index_scf, sab_nl=sab_nl)
     747              : 
     748         1208 :       CALL dbcsr_desymmetrize(rmat, nsmat)
     749         1208 :       CALL copy_dbcsr_to_fm(nsmat, bs_env%fm_work_mo(1))
     750         1208 :       CALL dbcsr_desymmetrize(cmat, nsmat)
     751         1208 :       CALL copy_dbcsr_to_fm(nsmat, bs_env%fm_work_mo(2))
     752         1208 :       CALL cp_fm_to_cfm(bs_env%fm_work_mo(1), bs_env%fm_work_mo(2), cfm_kp)
     753              : 
     754         1208 :       CALL dbcsr_deallocate_matrix(rmat)
     755         1208 :       CALL dbcsr_deallocate_matrix(cmat)
     756         1208 :       CALL dbcsr_deallocate_matrix(nsmat)
     757              : 
     758         1208 :       CALL timestop(handle)
     759              : 
     760         1208 :    END SUBROUTINE rsmat_to_kp
     761              : 
     762              : ! **************************************************************************************************
     763              : !> \brief ...
     764              : !> \param bs_env ...
     765              : ! **************************************************************************************************
     766           34 :    SUBROUTINE diagonalize_ks_matrix(bs_env)
     767              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     768              : 
     769              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'diagonalize_ks_matrix'
     770              : 
     771              :       INTEGER                                            :: handle, ispin
     772              :       REAL(KIND=dp)                                      :: CBM, VBM
     773              : 
     774           34 :       CALL timeset(routineN, handle)
     775              : 
     776          136 :       ALLOCATE (bs_env%eigenval_scf_Gamma(bs_env%n_ao, bs_env%n_spin))
     777              : 
     778           74 :       DO ispin = 1, bs_env%n_spin
     779              : 
     780              :          ! use work matrices because the matrices are overwritten in cp_fm_geeig_canon
     781           40 :          CALL cp_fm_to_fm(bs_env%fm_ks_Gamma(ispin), bs_env%fm_work_mo(1))
     782           40 :          CALL cp_fm_to_fm(bs_env%fm_s_Gamma, bs_env%fm_work_mo(2))
     783              : 
     784              :          ! diagonalize the Kohn-Sham matrix to obtain MO coefficients and SCF eigenvalues
     785              :          ! (at the Gamma-point)
     786              :          CALL cp_fm_geeig_canon(bs_env%fm_work_mo(1), &
     787              :                                 bs_env%fm_work_mo(2), &
     788              :                                 bs_env%fm_mo_coeff_Gamma(ispin), &
     789              :                                 bs_env%eigenval_scf_Gamma(:, ispin), &
     790              :                                 bs_env%fm_work_mo(3), &
     791           40 :                                 bs_env%eps_eigval_mat_s)
     792              : 
     793           40 :          VBM = bs_env%eigenval_scf_Gamma(bs_env%n_occ(ispin), ispin)
     794           40 :          CBM = bs_env%eigenval_scf_Gamma(bs_env%n_occ(ispin) + 1, ispin)
     795              : 
     796           40 :          bs_env%band_edges_scf_Gamma(ispin)%VBM = VBM
     797           40 :          bs_env%band_edges_scf_Gamma(ispin)%CBM = CBM
     798           74 :          bs_env%e_fermi(ispin) = 0.5_dp*(VBM + CBM)
     799              : 
     800              :       END DO
     801              : 
     802           34 :       CALL timestop(handle)
     803              : 
     804           34 :    END SUBROUTINE diagonalize_ks_matrix
     805              : 
     806              : ! **************************************************************************************************
     807              : !> \brief ...
     808              : !> \param bs_env ...
     809              : !> \param qs_env ...
     810              : ! **************************************************************************************************
     811           34 :    SUBROUTINE check_positive_definite_overlap_mat(bs_env, qs_env)
     812              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     813              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     814              : 
     815              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'check_positive_definite_overlap_mat'
     816              : 
     817              :       INTEGER                                            :: handle, ikp, info, u
     818              :       TYPE(cp_cfm_type)                                  :: cfm_s_ikp
     819              : 
     820           34 :       CALL timeset(routineN, handle)
     821              : 
     822           76 :       DO ikp = 1, bs_env%kpoints_DOS%nkp
     823              : 
     824              :          ! get S_µν(k_i) from S_µν(k=0)
     825              :          CALL cfm_ikp_from_fm_Gamma(cfm_s_ikp, bs_env%fm_s_Gamma, &
     826           42 :                                     ikp, qs_env, bs_env%kpoints_DOS, "ORB")
     827              : 
     828              :          ! check whether S_µν(k_i) is positive definite
     829           42 :          CALL cp_cfm_cholesky_decompose(matrix=cfm_s_ikp, n=bs_env%n_ao, info_out=info)
     830              : 
     831              :          ! check if Cholesky decomposition failed (Cholesky decomposition only works for
     832              :          ! positive definite matrices
     833           76 :          IF (info /= 0) THEN
     834            0 :             u = bs_env%unit_nr
     835              : 
     836            0 :             IF (u > 0) THEN
     837            0 :                WRITE (u, FMT="(T2,A)") ""
     838              :                WRITE (u, FMT="(T2,A)") "ERROR: The Cholesky decomposition "// &
     839            0 :                   "of the k-point overlap matrix failed. This is"
     840              :                WRITE (u, FMT="(T2,A)") "because the algorithm is "// &
     841            0 :                   "only correct in the limit of large cells. The cell of "
     842              :                WRITE (u, FMT="(T2,A)") "the calculation is too small. "// &
     843            0 :                   "Use MULTIPLE_UNIT_CELL to create a larger cell "
     844            0 :                WRITE (u, FMT="(T2,A)") "and to prevent this error."
     845              :             END IF
     846              : 
     847            0 :             CALL bs_env%para_env%sync()
     848            0 :             CPABORT("Please see information on the error above.")
     849              : 
     850              :          END IF ! Cholesky decomposition failed
     851              : 
     852              :       END DO ! ikp
     853              : 
     854           34 :       CALL cp_cfm_release(cfm_s_ikp)
     855              : 
     856           34 :       CALL timestop(handle)
     857              : 
     858           34 :    END SUBROUTINE check_positive_definite_overlap_mat
     859              : 
     860              : ! **************************************************************************************************
     861              : !> \brief ...
     862              : !> \param qs_env ...
     863              : !> \param bs_env ...
     864              : ! **************************************************************************************************
     865           88 :    SUBROUTINE get_parameters_from_qs_env(qs_env, bs_env)
     866              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     867              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     868              : 
     869              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_parameters_from_qs_env'
     870              : 
     871              :       INTEGER                                            :: color_sub, handle, homo, n_ao, n_atom, u
     872              :       INTEGER, DIMENSION(3)                              :: periodic
     873              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat
     874              :       TYPE(cell_type), POINTER                           :: cell
     875              :       TYPE(dft_control_type), POINTER                    :: dft_control
     876           44 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     877              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     878           44 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     879              :       TYPE(scf_control_type), POINTER                    :: scf_control
     880              :       TYPE(section_vals_type), POINTER                   :: input
     881              : 
     882           44 :       CALL timeset(routineN, handle)
     883              : 
     884              :       CALL get_qs_env(qs_env, &
     885              :                       dft_control=dft_control, &
     886              :                       scf_control=scf_control, &
     887           44 :                       mos=mos)
     888              : 
     889           44 :       bs_env%n_spin = dft_control%nspins
     890           44 :       IF (bs_env%n_spin == 1) bs_env%spin_degeneracy = 2.0_dp
     891           44 :       IF (bs_env%n_spin == 2) bs_env%spin_degeneracy = 1.0_dp
     892              : 
     893           44 :       CALL get_mo_set(mo_set=mos(1), nao=n_ao, homo=homo)
     894           44 :       bs_env%n_ao = n_ao
     895          132 :       bs_env%n_occ(1:2) = homo
     896          132 :       bs_env%n_vir(1:2) = n_ao - homo
     897              : 
     898           44 :       IF (bs_env%n_spin == 2) THEN
     899            6 :          CALL get_mo_set(mo_set=mos(2), homo=homo)
     900            6 :          bs_env%n_occ(2) = homo
     901            6 :          bs_env%n_vir(2) = n_ao - homo
     902              :       END IF
     903              : 
     904           44 :       bs_env%eps_eigval_mat_s = scf_control%eps_eigval
     905              : 
     906              :       ! get para_env from qs_env (bs_env%para_env is identical to para_env in qs_env)
     907           44 :       CALL get_qs_env(qs_env, para_env=para_env)
     908           44 :       color_sub = 0
     909           44 :       ALLOCATE (bs_env%para_env)
     910           44 :       CALL bs_env%para_env%from_split(para_env, color_sub)
     911              : 
     912           44 :       CALL get_qs_env(qs_env, particle_set=particle_set)
     913              : 
     914           44 :       n_atom = SIZE(particle_set)
     915           44 :       bs_env%n_atom = n_atom
     916              : 
     917           44 :       CALL get_qs_env(qs_env=qs_env, cell=cell)
     918           44 :       CALL get_cell(cell=cell, periodic=periodic, h=hmat)
     919          176 :       bs_env%periodic(1:3) = periodic(1:3)
     920          572 :       bs_env%hmat(1:3, 1:3) = hmat
     921           44 :       bs_env%nimages_scf = dft_control%nimages
     922           44 :       IF (dft_control%nimages == 1) THEN
     923           34 :          IF (bs_env%do_gw_ri_rs) THEN
     924           40 :             IF (ANY(periodic /= 0)) THEN
     925            0 :                bs_env%small_cell_full_kp_or_large_cell_Gamma = large_cell_Gamma_ri_rs
     926              :             ELSE
     927           10 :                bs_env%small_cell_full_kp_or_large_cell_Gamma = non_periodic_ri_rs
     928              :             END IF
     929              :          ELSE
     930           24 :             bs_env%small_cell_full_kp_or_large_cell_Gamma = large_cell_Gamma
     931              :          END IF
     932           10 :       ELSE IF (dft_control%nimages > 1) THEN
     933           10 :          IF (bs_env%do_gw_ri_rs) THEN
     934            0 :             CPABORT("RI-RS Not Implemented for K-point Calculations")
     935              :          ELSE
     936           10 :             bs_env%small_cell_full_kp_or_large_cell_Gamma = small_cell_full_kp
     937              :          END IF
     938              :       ELSE
     939            0 :          CPABORT("Wrong number of cells from DFT calculation.")
     940              :       END IF
     941              : 
     942           44 :       u = bs_env%unit_nr
     943              : 
     944              :       ! Marek : Get and save the rtp method
     945           44 :       CALL get_qs_env(qs_env=qs_env, input=input)
     946           44 :       CALL section_vals_val_get(input, "DFT%REAL_TIME_PROPAGATION%RTBSE%_SECTION_PARAMETERS_", i_val=bs_env%rtp_method)
     947              : 
     948           44 :       IF (u > 0) THEN
     949           22 :          WRITE (u, FMT="(T2,2A,T73,I8)") "Number of occupied molecular orbitals (MOs) ", &
     950           44 :             "= Number of occupied bands", homo
     951           22 :          WRITE (u, FMT="(T2,2A,T73,I8)") "Number of unoccupied (= virtual) MOs ", &
     952           44 :             "= Number of unoccupied bands", n_ao - homo
     953           22 :          WRITE (u, FMT="(T2,A,T73,I8)") "Number of Gaussian basis functions for MOs", n_ao
     954           22 :          IF (bs_env%small_cell_full_kp_or_large_cell_Gamma == small_cell_full_kp) THEN
     955            5 :             WRITE (u, FMT="(T2,2A,T73,I8)") "Number of cells considered in the DFT ", &
     956           10 :                "calculation", bs_env%nimages_scf
     957              :          END IF
     958              :       END IF
     959              : 
     960           44 :       CALL timestop(handle)
     961              : 
     962           44 :    END SUBROUTINE get_parameters_from_qs_env
     963              : 
     964              : ! **************************************************************************************************
     965              : !> \brief ...
     966              : !> \param bs_env ...
     967              : ! **************************************************************************************************
     968           44 :    SUBROUTINE set_heuristic_parameters(bs_env)
     969              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     970              : 
     971              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'set_heuristic_parameters'
     972              : 
     973              :       INTEGER                                            :: handle
     974              : 
     975           44 :       CALL timeset(routineN, handle)
     976              : 
     977           44 :       bs_env%n_bins_max_for_printing = 5000
     978              : 
     979           44 :       CALL timestop(handle)
     980              : 
     981           44 :    END SUBROUTINE set_heuristic_parameters
     982              : 
     983              : ! **************************************************************************************************
     984              : !> \brief ...
     985              : !> \param qs_env ...
     986              : !> \param bs_env ...
     987              : ! **************************************************************************************************
     988           44 :    SUBROUTINE allocate_and_fill_fm_ks_fm_s(qs_env, bs_env)
     989              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     990              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
     991              : 
     992              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'allocate_and_fill_fm_ks_fm_s'
     993              : 
     994              :       INTEGER                                            :: handle, i_work, ispin
     995              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     996              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     997           44 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks, matrix_s
     998              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     999              : 
    1000           44 :       CALL timeset(routineN, handle)
    1001              : 
    1002              :       CALL get_qs_env(qs_env, &
    1003              :                       para_env=para_env, &
    1004              :                       blacs_env=blacs_env, &
    1005              :                       matrix_ks_kp=matrix_ks, &
    1006           44 :                       matrix_s_kp=matrix_s)
    1007              : 
    1008           44 :       NULLIFY (fm_struct)
    1009              :       CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=bs_env%n_ao, &
    1010           44 :                                ncol_global=bs_env%n_ao, para_env=para_env)
    1011              : 
    1012          220 :       DO i_work = 1, SIZE(bs_env%fm_work_mo)
    1013          220 :          CALL cp_fm_create(bs_env%fm_work_mo(i_work), fm_struct)
    1014              :       END DO
    1015              : 
    1016           44 :       CALL cp_cfm_create(bs_env%cfm_work_mo, fm_struct)
    1017           44 :       CALL cp_cfm_create(bs_env%cfm_work_mo_2, fm_struct)
    1018              : 
    1019           44 :       CALL cp_fm_create(bs_env%fm_s_Gamma, fm_struct)
    1020           44 :       CALL copy_dbcsr_to_fm(matrix_s(1, 1)%matrix, bs_env%fm_s_Gamma)
    1021              : 
    1022           94 :       DO ispin = 1, bs_env%n_spin
    1023           50 :          CALL cp_fm_create(bs_env%fm_ks_Gamma(ispin), fm_struct)
    1024           50 :          CALL copy_dbcsr_to_fm(matrix_ks(ispin, 1)%matrix, bs_env%fm_ks_Gamma(ispin))
    1025           94 :          CALL cp_fm_create(bs_env%fm_mo_coeff_Gamma(ispin), fm_struct)
    1026              :       END DO
    1027              : 
    1028           44 :       CALL cp_fm_struct_release(fm_struct)
    1029              : 
    1030           44 :       NULLIFY (bs_env%mat_ao_ao%matrix)
    1031           44 :       ALLOCATE (bs_env%mat_ao_ao%matrix)
    1032              :       CALL dbcsr_create(bs_env%mat_ao_ao%matrix, template=matrix_s(1, 1)%matrix, &
    1033           44 :                         matrix_type=dbcsr_type_no_symmetry)
    1034              : 
    1035          220 :       ALLOCATE (bs_env%eigenval_scf(bs_env%n_ao, bs_env%nkp_bs_and_DOS, bs_env%n_spin))
    1036              : 
    1037           44 :       CALL timestop(handle)
    1038              : 
    1039           44 :    END SUBROUTINE allocate_and_fill_fm_ks_fm_s
    1040              : 
    1041              : ! **************************************************************************************************
    1042              : !> \brief ...
    1043              : !> \param qs_env ...
    1044              : !> \param bs_env ...
    1045              : ! **************************************************************************************************
    1046           44 :    SUBROUTINE eval_bandstructure_properties(qs_env, bs_env)
    1047              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1048              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1049              : 
    1050              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'eval_bandstructure_properties'
    1051              : 
    1052              :       INTEGER                                            :: handle, homo, homo_1, homo_2, &
    1053              :                                                             homo_spinor, ikp, ikp_for_file, ispin, &
    1054              :                                                             n_ao, n_E, nkind, nkp
    1055              :       LOGICAL                                            :: is_bandstruc_kpoint, print_DOS_kpoints, &
    1056              :                                                             print_ikp
    1057              :       REAL(KIND=dp)                                      :: broadening, E_max, E_max_G0W0, E_min, &
    1058              :                                                             E_min_G0W0, E_total_window, &
    1059              :                                                             energy_step_DOS, energy_window_DOS, t1
    1060           44 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: DOS_G0W0, DOS_G0W0_SOC, DOS_scf, DOS_scf_SOC, &
    1061           44 :          eigenval, eigenval_spinor, eigenval_spinor_G0W0, eigenval_spinor_no_SOC
    1062           44 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS_G0W0, PDOS_G0W0_SOC, PDOS_scf, &
    1063           44 :                                                             PDOS_scf_SOC, proj_mo_on_kind
    1064           44 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_G0W0_2d, LDOS_scf_2d, &
    1065           44 :                                                             LDOS_scf_2d_SOC
    1066              :       TYPE(band_edges_type)                              :: band_edges_G0W0, band_edges_G0W0_SOC, &
    1067              :                                                             band_edges_scf, band_edges_scf_guess, &
    1068              :                                                             band_edges_scf_SOC
    1069              :       TYPE(cp_cfm_type) :: cfm_ks_ikp, cfm_ks_ikp_spinor, cfm_mos_ikp_spinor, cfm_s_ikp, &
    1070              :          cfm_s_ikp_copy, cfm_s_ikp_spinor, cfm_s_ikp_spinor_copy, cfm_SOC_ikp_spinor, &
    1071              :          cfm_spinor_wf_ikp, cfm_work_ikp, cfm_work_ikp_spinor
    1072          132 :       TYPE(cp_cfm_type), DIMENSION(2)                    :: cfm_mos_ikp
    1073              : 
    1074           44 :       CALL timeset(routineN, handle)
    1075              : 
    1076           44 :       n_ao = bs_env%n_ao
    1077              : 
    1078           44 :       energy_window_DOS = bs_env%energy_window_DOS
    1079           44 :       energy_step_DOS = bs_env%energy_step_DOS
    1080           44 :       broadening = bs_env%broadening_DOS
    1081              : 
    1082              :       ! if we have done GW or a full kpoint SCF, we already have the band edges
    1083           44 :       IF (bs_env%do_gw .OR. &
    1084              :           bs_env%small_cell_full_kp_or_large_cell_Gamma == small_cell_full_kp) THEN
    1085           44 :          band_edges_scf = bs_env%band_edges_scf
    1086           44 :          band_edges_scf_guess = band_edges_scf
    1087              :       ELSE
    1088              : 
    1089            0 :          IF (bs_env%n_spin == 1) THEN
    1090            0 :             homo = bs_env%n_occ(1)
    1091            0 :             band_edges_scf_guess%VBM = bs_env%eigenval_scf_Gamma(homo, 1)
    1092            0 :             band_edges_scf_guess%CBM = bs_env%eigenval_scf_Gamma(homo + 1, 1)
    1093              :          ELSE
    1094            0 :             homo_1 = bs_env%n_occ(1)
    1095            0 :             homo_2 = bs_env%n_occ(2)
    1096              :             band_edges_scf_guess%VBM = MAX(bs_env%eigenval_scf_Gamma(homo_1, 1), &
    1097            0 :                                            bs_env%eigenval_scf_Gamma(homo_2, 2))
    1098              :             band_edges_scf_guess%CBM = MIN(bs_env%eigenval_scf_Gamma(homo_1 + 1, 1), &
    1099            0 :                                            bs_env%eigenval_scf_Gamma(homo_2 + 1, 2))
    1100              :          END IF
    1101              : 
    1102              :          ! initialization
    1103            0 :          band_edges_scf%VBM = -1000.0_dp
    1104            0 :          band_edges_scf%CBM = 1000.0_dp
    1105            0 :          band_edges_scf%DBG = 1000.0_dp
    1106              :       END IF
    1107              : 
    1108           44 :       E_min = band_edges_scf_guess%VBM - 0.5_dp*energy_window_DOS
    1109           44 :       E_max = band_edges_scf_guess%CBM + 0.5_dp*energy_window_DOS
    1110              : 
    1111           44 :       IF (bs_env%do_gw) THEN
    1112           42 :          band_edges_G0W0 = bs_env%band_edges_G0W0
    1113           42 :          E_min_G0W0 = band_edges_G0W0%VBM - 0.5_dp*energy_window_DOS
    1114           42 :          E_max_G0W0 = band_edges_G0W0%CBM + 0.5_dp*energy_window_DOS
    1115           42 :          E_min = MIN(E_min, E_min_G0W0)
    1116           42 :          E_max = MAX(E_max, E_max_G0W0)
    1117              :       END IF
    1118              : 
    1119           44 :       E_total_window = E_max - E_min
    1120              : 
    1121           44 :       n_E = INT(E_total_window/energy_step_DOS)
    1122              : 
    1123           44 :       CALL get_qs_env(qs_env, nkind=nkind)
    1124              : 
    1125          176 :       ALLOCATE (proj_mo_on_kind(n_ao, nkind))
    1126           44 :       proj_mo_on_kind(:, :) = 0.0_dp
    1127              : 
    1128          132 :       ALLOCATE (eigenval(n_ao))
    1129          132 :       ALLOCATE (eigenval_spinor(2*n_ao))
    1130           88 :       ALLOCATE (eigenval_spinor_no_SOC(2*n_ao))
    1131           88 :       ALLOCATE (eigenval_spinor_G0W0(2*n_ao))
    1132              : 
    1133           44 :       IF (bs_env%do_dos_pdos) THEN
    1134              : 
    1135           36 :          ALLOCATE (DOS_scf(n_E))
    1136           12 :          DOS_scf(:) = 0.0_dp
    1137           48 :          ALLOCATE (PDOS_scf(n_E, nkind))
    1138           12 :          PDOS_scf(:, :) = 0.0_dp
    1139              : 
    1140           12 :          IF (bs_env%do_soc) THEN
    1141              : 
    1142           16 :             ALLOCATE (DOS_scf_SOC(n_E))
    1143            8 :             DOS_scf_SOC(:) = 0.0_dp
    1144           24 :             ALLOCATE (PDOS_scf_SOC(n_E, nkind))
    1145            8 :             PDOS_scf_SOC(:, :) = 0.0_dp
    1146              : 
    1147              :          END IF
    1148              : 
    1149           12 :          IF (bs_env%do_gw) THEN
    1150              : 
    1151           24 :             ALLOCATE (DOS_G0W0(n_E))
    1152           12 :             DOS_G0W0(:) = 0.0_dp
    1153           36 :             ALLOCATE (PDOS_G0W0(n_E, nkind))
    1154           12 :             PDOS_G0W0(:, :) = 0.0_dp
    1155              : 
    1156           12 :             IF (bs_env%do_soc) THEN
    1157              : 
    1158           16 :                ALLOCATE (DOS_G0W0_SOC(n_E))
    1159            8 :                DOS_G0W0_SOC(:) = 0.0_dp
    1160           24 :                ALLOCATE (PDOS_G0W0_SOC(n_E, nkind))
    1161            8 :                PDOS_G0W0_SOC(:, :) = 0.0_dp
    1162              : 
    1163              :             END IF
    1164              :          END IF
    1165              :       END IF
    1166              : 
    1167           44 :       CALL cp_cfm_create(cfm_mos_ikp(1), bs_env%fm_ks_Gamma(1)%matrix_struct)
    1168           44 :       CALL cp_cfm_create(cfm_mos_ikp(2), bs_env%fm_ks_Gamma(1)%matrix_struct)
    1169           44 :       CALL cp_cfm_create(cfm_work_ikp, bs_env%fm_ks_Gamma(1)%matrix_struct)
    1170           44 :       CALL cp_cfm_create(cfm_s_ikp_copy, bs_env%fm_ks_Gamma(1)%matrix_struct)
    1171              : 
    1172           44 :       IF (bs_env%do_soc) THEN
    1173              : 
    1174           14 :          CALL cp_cfm_create(cfm_mos_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1175           14 :          CALL cp_cfm_create(cfm_work_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1176           14 :          CALL cp_cfm_create(cfm_s_ikp_spinor_copy, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1177           14 :          CALL cp_cfm_create(cfm_ks_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1178           14 :          CALL cp_cfm_create(cfm_SOC_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1179           14 :          CALL cp_cfm_create(cfm_s_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1180           14 :          CALL cp_cfm_create(cfm_spinor_wf_ikp, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1181              : 
    1182           14 :          homo_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    1183              : 
    1184           14 :          band_edges_scf_SOC%VBM = -1000.0_dp
    1185           14 :          band_edges_scf_SOC%CBM = 1000.0_dp
    1186           14 :          band_edges_scf_SOC%DBG = 1000.0_dp
    1187              : 
    1188           14 :          IF (bs_env%do_gw) THEN
    1189           14 :             band_edges_G0W0_SOC%VBM = -1000.0_dp
    1190           14 :             band_edges_G0W0_SOC%CBM = 1000.0_dp
    1191           14 :             band_edges_G0W0_SOC%DBG = 1000.0_dp
    1192              :          END IF
    1193              : 
    1194           14 :          IF (bs_env%unit_nr > 0) THEN
    1195            7 :             WRITE (bs_env%unit_nr, '(A)') ''
    1196            7 :             WRITE (bs_env%unit_nr, '(T2,A,F43.1,A)') 'SOC requested, SOC energy window:', &
    1197           14 :                bs_env%energy_window_soc*evolt, ' eV'
    1198              :          END IF
    1199              : 
    1200              :       END IF
    1201              : 
    1202           44 :       IF (bs_env%do_ldos) THEN
    1203            2 :          CPASSERT(bs_env%int_ldos_xyz == int_ldos_z)
    1204              :       END IF
    1205              : 
    1206           44 :       IF (bs_env%unit_nr > 0) THEN
    1207           22 :          WRITE (bs_env%unit_nr, '(A)') ''
    1208              :       END IF
    1209              : 
    1210           44 :       IF (bs_env%small_cell_full_kp_or_large_cell_Gamma == small_cell_full_kp) THEN
    1211           10 :          CALL cp_cfm_create(cfm_ks_ikp, bs_env%cfm_ks_kp(1, 1)%matrix_struct)
    1212           10 :          CALL cp_cfm_create(cfm_s_ikp, bs_env%cfm_ks_kp(1, 1)%matrix_struct)
    1213              :       END IF
    1214              : 
    1215          310 :       DO ikp = 1, bs_env%nkp_bs_and_DOS
    1216              : 
    1217          266 :          t1 = m_walltime()
    1218              : 
    1219          542 :          DO ispin = 1, bs_env%n_spin
    1220              : 
    1221          328 :             SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
    1222              :             CASE (large_cell_Gamma, large_cell_Gamma_ri_rs, non_periodic_ri_rs)
    1223              : 
    1224              :                ! 1. get H^KS_µν(k_i) from H^KS_µν(k=0)
    1225              :                CALL cfm_ikp_from_fm_Gamma(cfm_ks_ikp, bs_env%fm_ks_Gamma(ispin), &
    1226           52 :                                           ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1227              : 
    1228              :                ! 2. get S_µν(k_i) from S_µν(k=0)
    1229              :                CALL cfm_ikp_from_fm_Gamma(cfm_s_ikp, bs_env%fm_s_Gamma, &
    1230           52 :                                           ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1231           52 :                CALL cp_cfm_to_cfm(cfm_s_ikp, cfm_s_ikp_copy)
    1232              : 
    1233              :                ! 3. Diagonalize (Roothaan-Hall): H_KS(k_i)*C(k_i) = S(k_i)*C(k_i)*ϵ(k_i)
    1234              :                CALL cp_cfm_geeig(cfm_ks_ikp, cfm_s_ikp_copy, cfm_mos_ikp(ispin), &
    1235           52 :                                  eigenval, cfm_work_ikp)
    1236              : 
    1237              :             CASE (small_cell_full_kp)
    1238              : 
    1239              :                ! 1. get H^KS_µν(k_i)
    1240          224 :                CALL cp_cfm_to_cfm(bs_env%cfm_ks_kp(ikp, ispin), cfm_ks_ikp)
    1241              : 
    1242              :                ! 2. get S_µν(k_i)
    1243          224 :                CALL cp_cfm_to_cfm(bs_env%cfm_s_kp(ikp), cfm_s_ikp)
    1244              : 
    1245              :                ! 3. get C_µn(k_i) and ϵ_n(k_i)
    1246          224 :                CALL cp_cfm_to_cfm(bs_env%cfm_mo_coeff_kp(ikp, ispin), cfm_mos_ikp(ispin))
    1247         2962 :                eigenval(:) = bs_env%eigenval_scf(:, ikp, ispin)
    1248              : 
    1249              :             END SELECT
    1250              : 
    1251              :             ! 4. Projection p_nk^A of MO ψ_nk(r) on atom type A (inspired by Mulliken charge)
    1252              :             !    p_nk^A = sum_µ^A,ν C*_µ^A,n(k) S_µ^A,ν(k) C_ν,n(k)
    1253          276 :             CALL compute_proj_mo_on_kind(proj_mo_on_kind, qs_env, cfm_mos_ikp(ispin), cfm_s_ikp)
    1254              : 
    1255              :             ! 5. DOS and PDOS
    1256          276 :             IF (bs_env%do_dos_pdos) THEN
    1257              :                CALL add_to_DOS_PDOS(DOS_scf, PDOS_scf, eigenval, ikp, bs_env, n_E, E_min, &
    1258          106 :                                     proj_mo_on_kind)
    1259              : 
    1260          106 :                IF (bs_env%do_gw) THEN
    1261              :                   CALL add_to_DOS_PDOS(DOS_G0W0, PDOS_G0W0, bs_env%eigenval_G0W0(:, ikp, ispin), &
    1262          106 :                                        ikp, bs_env, n_E, E_min, proj_mo_on_kind)
    1263              :                END IF
    1264              :             END IF
    1265              : 
    1266          276 :             IF (bs_env%do_ldos) THEN
    1267              :                CALL add_to_LDOS_2d(LDOS_scf_2d, qs_env, ikp, bs_env, cfm_mos_ikp(ispin), &
    1268            2 :                                    eigenval(:), band_edges_scf_guess)
    1269              : 
    1270            2 :                IF (bs_env%do_gw) THEN
    1271              :                   CALL add_to_LDOS_2d(LDOS_G0W0_2d, qs_env, ikp, bs_env, cfm_mos_ikp(ispin), &
    1272            2 :                                       bs_env%eigenval_G0W0(:, ikp, 1), band_edges_G0W0)
    1273              :                END IF
    1274              : 
    1275              :             END IF
    1276              : 
    1277          276 :             homo = bs_env%n_occ(ispin)
    1278              : 
    1279          276 :             band_edges_scf%VBM = MAX(band_edges_scf%VBM, eigenval(homo))
    1280          276 :             band_edges_scf%CBM = MIN(band_edges_scf%CBM, eigenval(homo + 1))
    1281          542 :             band_edges_scf%DBG = MIN(band_edges_scf%DBG, eigenval(homo + 1) - eigenval(homo))
    1282              : 
    1283              :          END DO ! spin
    1284              : 
    1285              :          ! now the same with spin-orbit coupling
    1286          266 :          IF (bs_env%do_soc) THEN
    1287              : 
    1288              :             ! only print eigenvalues of DOS k-points in case no bandstructure path has been given
    1289          200 :             print_DOS_kpoints = (bs_env%nkp_only_bs <= 0)
    1290              :             ! in kpoints_DOS, the last nkp_only_bs are bandstructure k-points
    1291          200 :             is_bandstruc_kpoint = (ikp > bs_env%nkp_only_DOS)
    1292          200 :             print_ikp = print_DOS_kpoints .OR. is_bandstruc_kpoint
    1293              : 
    1294          200 :             IF (print_DOS_kpoints) THEN
    1295          106 :                nkp = bs_env%nkp_only_DOS
    1296          106 :                ikp_for_file = ikp
    1297              :             ELSE
    1298           94 :                nkp = bs_env%nkp_only_bs
    1299           94 :                ikp_for_file = ikp - bs_env%nkp_only_DOS
    1300              :             END IF
    1301              : 
    1302              :             ! compute DFT+SOC eigenvalues; based on these, compute band edges, DOS and LDOS
    1303              :             CALL SOC_ev(bs_env, qs_env, ikp, bs_env%eigenval_scf, band_edges_scf, &
    1304              :                         E_min, cfm_mos_ikp, DOS_scf_SOC, PDOS_scf_SOC, &
    1305          200 :                         band_edges_scf_SOC, eigenval_spinor, cfm_spinor_wf_ikp)
    1306              : 
    1307          200 :             IF (.NOT. bs_env%do_gw .AND. print_ikp) THEN
    1308            0 :                CALL write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env)
    1309              :             END IF
    1310              : 
    1311          200 :             IF (bs_env%do_ldos) THEN
    1312              :                CALL add_to_LDOS_2d(LDOS_scf_2d_SOC, qs_env, ikp, bs_env, cfm_spinor_wf_ikp, &
    1313            2 :                                    eigenval_spinor, band_edges_scf_guess, .TRUE., cfm_work_ikp)
    1314              :             END IF
    1315              : 
    1316          200 :             IF (bs_env%do_gw) THEN
    1317              : 
    1318              :                ! compute G0W0+SOC eigenvalues; based on these, compute band edges, DOS and LDOS
    1319              :                CALL SOC_ev(bs_env, qs_env, ikp, bs_env%eigenval_G0W0, band_edges_G0W0, &
    1320              :                            E_min, cfm_mos_ikp, DOS_G0W0_SOC, PDOS_G0W0_SOC, &
    1321          200 :                            band_edges_G0W0_SOC, eigenval_spinor_G0W0, cfm_spinor_wf_ikp)
    1322              : 
    1323          200 :                IF (print_ikp) THEN
    1324              :                   ! write SCF+SOC and G0W0+SOC eigenvalues to file
    1325              :                   ! SCF_and_G0W0_band_structure_for_kpoint_<ikp>_+_SOC
    1326              :                   CALL write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env, &
    1327          168 :                                              eigenval_spinor_G0W0)
    1328              :                END IF
    1329              : 
    1330              :             END IF ! do_gw
    1331              : 
    1332              :          END IF ! do_soc
    1333              : 
    1334          310 :          IF (bs_env%unit_nr > 0 .AND. m_walltime() - t1 > 20.0_dp) THEN
    1335              :             WRITE (bs_env%unit_nr, '(T2,A,T43,I5,A,I3,A,F7.1,A)') &
    1336            0 :                'Compute DOS, LDOS for k-point ', ikp, ' /', bs_env%nkp_bs_and_DOS, &
    1337            0 :                ',    Execution time', m_walltime() - t1, ' s'
    1338              :          END IF
    1339              : 
    1340              :       END DO ! ikp_DOS
    1341              : 
    1342           44 :       band_edges_scf%IDBG = band_edges_scf%CBM - band_edges_scf%VBM
    1343           44 :       IF (bs_env%do_soc) THEN
    1344           14 :          band_edges_scf_SOC%IDBG = band_edges_scf_SOC%CBM - band_edges_scf_SOC%VBM
    1345           14 :          IF (bs_env%do_gw) THEN
    1346           14 :             band_edges_G0W0_SOC%IDBG = band_edges_G0W0_SOC%CBM - band_edges_G0W0_SOC%VBM
    1347              :          END IF
    1348              :       END IF
    1349              : 
    1350           44 :       CALL write_band_edges(band_edges_scf, "SCF", bs_env)
    1351           44 :       IF (bs_env%do_dos_pdos) THEN
    1352           12 :          CALL write_dos_pdos(DOS_scf, PDOS_scf, bs_env, qs_env, "SCF", E_min, band_edges_scf%VBM)
    1353              :       END IF
    1354           44 :       IF (bs_env%do_ldos) THEN
    1355            2 :          CALL print_LDOS_main(LDOS_scf_2d, bs_env, band_edges_scf, "SCF")
    1356              :       END IF
    1357              : 
    1358           44 :       IF (bs_env%do_soc) THEN
    1359           14 :          CALL write_band_edges(band_edges_scf_SOC, "SCF+SOC", bs_env)
    1360           14 :          IF (bs_env%do_dos_pdos) THEN
    1361              :             CALL write_dos_pdos(DOS_scf_SOC, PDOS_scf_SOC, bs_env, qs_env, "SCF_SOC", &
    1362            8 :                                 E_min, band_edges_scf_SOC%VBM)
    1363              :          END IF
    1364           14 :          IF (bs_env%do_ldos) THEN
    1365              :             ! argument band_edges_scf is actually correct because the non-SOC band edges
    1366              :             ! have been used as reference in add_to_LDOS_2d
    1367              :             CALL print_LDOS_main(LDOS_scf_2d_SOC, bs_env, band_edges_scf, &
    1368            2 :                                  "SCF_SOC")
    1369              :          END IF
    1370              :       END IF
    1371              : 
    1372           44 :       IF (bs_env%do_gw) THEN
    1373           42 :          CALL write_band_edges(band_edges_G0W0, "G0W0", bs_env)
    1374           42 :          CALL write_band_edges(bs_env%band_edges_HF, "Hartree-Fock with SCF orbitals", bs_env)
    1375           42 :          IF (bs_env%do_dos_pdos) THEN
    1376              :             CALL write_dos_pdos(DOS_G0W0, PDOS_G0W0, bs_env, qs_env, "G0W0", E_min, &
    1377           12 :                                 band_edges_G0W0%VBM)
    1378              :          END IF
    1379           42 :          IF (bs_env%do_ldos) THEN
    1380            2 :             CALL print_LDOS_main(LDOS_G0W0_2d, bs_env, band_edges_G0W0, "G0W0")
    1381              :          END IF
    1382              :       END IF
    1383              : 
    1384           44 :       IF (bs_env%do_soc .AND. bs_env%do_gw) THEN
    1385           14 :          CALL write_band_edges(band_edges_G0W0_SOC, "G0W0+SOC", bs_env)
    1386           14 :          IF (bs_env%do_dos_pdos) THEN
    1387              :             CALL write_dos_pdos(DOS_G0W0_SOC, PDOS_G0W0_SOC, bs_env, qs_env, "G0W0_SOC", E_min, &
    1388            8 :                                 band_edges_G0W0_SOC%VBM)
    1389              :          END IF
    1390              :       END IF
    1391              : 
    1392           44 :       CALL cp_cfm_release(cfm_s_ikp)
    1393           44 :       CALL cp_cfm_release(cfm_ks_ikp)
    1394           44 :       CALL cp_cfm_release(cfm_mos_ikp(1))
    1395           44 :       CALL cp_cfm_release(cfm_mos_ikp(2))
    1396           44 :       CALL cp_cfm_release(cfm_work_ikp)
    1397           44 :       CALL cp_cfm_release(cfm_s_ikp_copy)
    1398              : 
    1399           44 :       CALL cp_cfm_release(cfm_s_ikp_spinor)
    1400           44 :       CALL cp_cfm_release(cfm_ks_ikp_spinor)
    1401           44 :       CALL cp_cfm_release(cfm_SOC_ikp_spinor)
    1402           44 :       CALL cp_cfm_release(cfm_mos_ikp_spinor)
    1403           44 :       CALL cp_cfm_release(cfm_work_ikp_spinor)
    1404           44 :       CALL cp_cfm_release(cfm_s_ikp_spinor_copy)
    1405           44 :       CALL cp_cfm_release(cfm_spinor_wf_ikp)
    1406              : 
    1407           44 :       CALL timestop(handle)
    1408              : 
    1409          176 :    END SUBROUTINE eval_bandstructure_properties
    1410              : 
    1411              : ! **************************************************************************************************
    1412              : !> \brief ...
    1413              : !> \param LDOS_2d ...
    1414              : !> \param bs_env ...
    1415              : !> \param band_edges ...
    1416              : !> \param scf_gw_soc ...
    1417              : ! **************************************************************************************************
    1418            6 :    SUBROUTINE print_LDOS_main(LDOS_2d, bs_env, band_edges, scf_gw_soc)
    1419              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d
    1420              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1421              :       TYPE(band_edges_type)                              :: band_edges
    1422              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    1423              : 
    1424              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'print_LDOS_main'
    1425              : 
    1426              :       INTEGER :: handle, i_x, i_x_bin, i_x_end, i_x_end_bin, i_x_end_glob, i_x_start, &
    1427              :          i_x_start_bin, i_x_start_glob, i_y, i_y_bin, i_y_end, i_y_end_bin, i_y_end_glob, &
    1428              :          i_y_start, i_y_start_bin, i_y_start_glob, n_E
    1429            6 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: n_sum_for_bins
    1430              :       INTEGER, DIMENSION(2)                              :: bin_mesh
    1431              :       LOGICAL                                            :: do_xy_bins
    1432              :       REAL(KIND=dp)                                      :: E_min, energy_step, energy_window
    1433              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d_bins
    1434              : 
    1435            6 :       CALL timeset(routineN, handle)
    1436              : 
    1437            6 :       n_E = SIZE(LDOS_2d, 3)
    1438              : 
    1439            6 :       energy_window = bs_env%energy_window_DOS
    1440            6 :       energy_step = bs_env%energy_step_DOS
    1441            6 :       E_min = band_edges%VBM - 0.5_dp*energy_window
    1442              : 
    1443           18 :       bin_mesh(1:2) = bs_env%bin_mesh(1:2)
    1444            6 :       do_xy_bins = (bin_mesh(1) > 0 .AND. bin_mesh(2) > 0)
    1445              : 
    1446            6 :       i_x_start = LBOUND(LDOS_2d, 1)
    1447            6 :       i_x_end = UBOUND(LDOS_2d, 1)
    1448            6 :       i_y_start = LBOUND(LDOS_2d, 2)
    1449            6 :       i_y_end = UBOUND(LDOS_2d, 2)
    1450              : 
    1451            6 :       IF (do_xy_bins) THEN
    1452            6 :          i_x_start_bin = 1
    1453            6 :          i_x_end_bin = bin_mesh(1)
    1454            6 :          i_y_start_bin = 1
    1455            6 :          i_y_end_bin = bin_mesh(2)
    1456              :       ELSE
    1457              :          i_x_start_bin = i_x_start
    1458              :          i_x_end_bin = i_x_end
    1459              :          i_y_start_bin = i_y_start
    1460              :          i_y_end_bin = i_y_end
    1461              :       END IF
    1462              : 
    1463           30 :       ALLOCATE (LDOS_2d_bins(i_x_start_bin:i_x_end_bin, i_y_start_bin:i_y_end_bin, n_E))
    1464            6 :       LDOS_2d_bins(:, :, :) = 0.0_dp
    1465              : 
    1466            6 :       IF (do_xy_bins) THEN
    1467              : 
    1468            6 :          i_x_start_glob = i_x_start
    1469            6 :          i_x_end_glob = i_x_end
    1470            6 :          i_y_start_glob = i_y_start
    1471            6 :          i_y_end_glob = i_y_end
    1472              : 
    1473            6 :          CALL bs_env%para_env%min(i_x_start_glob)
    1474            6 :          CALL bs_env%para_env%max(i_x_end_glob)
    1475            6 :          CALL bs_env%para_env%min(i_y_start_glob)
    1476            6 :          CALL bs_env%para_env%max(i_y_end_glob)
    1477              : 
    1478           24 :          ALLOCATE (n_sum_for_bins(bin_mesh(1), bin_mesh(2)), SOURCE=0)
    1479              : 
    1480              :          ! transform interval [i_x_start, i_x_end] to [1, bin_mesh(1)] (and same for y)
    1481          390 :          DO i_y = i_y_start, i_y_end
    1482         4230 :             DO i_x = i_x_start, i_x_end
    1483         3840 :                i_x_bin = bin_mesh(1)*(i_x - i_x_start_glob)/(i_x_end_glob - i_x_start_glob + 1) + 1
    1484         3840 :                i_y_bin = bin_mesh(2)*(i_y - i_y_start_glob)/(i_y_end_glob - i_y_start_glob + 1) + 1
    1485              :                LDOS_2d_bins(i_x_bin, i_y_bin, :) = LDOS_2d_bins(i_x_bin, i_y_bin, :) + &
    1486      1073920 :                                                    LDOS_2d(i_x, i_y, :)
    1487         4224 :                n_sum_for_bins(i_x_bin, i_y_bin) = n_sum_for_bins(i_x_bin, i_y_bin) + 1
    1488              :             END DO
    1489              :          END DO
    1490              : 
    1491            6 :          CALL bs_env%para_env%sum(LDOS_2d_bins)
    1492            6 :          CALL bs_env%para_env%sum(n_sum_for_bins)
    1493              : 
    1494              :          ! divide by number of terms in the sum so we have the average LDOS(x,y,E)
    1495           30 :          DO i_y_bin = 1, bin_mesh(2)
    1496          126 :             DO i_x_bin = 1, bin_mesh(1)
    1497              :                LDOS_2d_bins(i_x_bin, i_y_bin, :) = LDOS_2d_bins(i_x_bin, i_y_bin, :)/ &
    1498        26872 :                                                    REAL(n_sum_for_bins(i_x_bin, i_y_bin), KIND=dp)
    1499              :             END DO
    1500              :          END DO
    1501              : 
    1502              :       ELSE
    1503              : 
    1504            0 :          LDOS_2d_bins(:, :, :) = LDOS_2d(:, :, :)
    1505              : 
    1506              :       END IF
    1507              : 
    1508            6 :       IF (bin_mesh(1)*bin_mesh(2) < bs_env%n_bins_max_for_printing) THEN
    1509            6 :          CALL print_LDOS_2d_bins(LDOS_2d_bins, bs_env, E_min, scf_gw_soc)
    1510              :       ELSE
    1511            0 :          CPWARN("The number of bins for the LDOS is too large. Decrease BIN_MESH.")
    1512              :       END IF
    1513              : 
    1514            6 :       CALL timestop(handle)
    1515              : 
    1516           12 :    END SUBROUTINE print_LDOS_main
    1517              : 
    1518              : ! **************************************************************************************************
    1519              : !> \brief ...
    1520              : !> \param LDOS_2d_bins ...
    1521              : !> \param bs_env ...
    1522              : !> \param E_min ...
    1523              : !> \param scf_gw_soc ...
    1524              : ! **************************************************************************************************
    1525            6 :    SUBROUTINE print_LDOS_2d_bins(LDOS_2d_bins, bs_env, E_min, scf_gw_soc)
    1526              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d_bins
    1527              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1528              :       REAL(KIND=dp)                                      :: E_min
    1529              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    1530              : 
    1531              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'print_LDOS_2d_bins'
    1532              : 
    1533              :       CHARACTER(LEN=18)                                  :: print_format
    1534              :       CHARACTER(LEN=4)                                   :: print_format_1, print_format_2
    1535              :       CHARACTER(len=default_string_length)               :: fname
    1536              :       INTEGER                                            :: handle, i_E, i_x, i_x_end, i_x_start, &
    1537              :                                                             i_y, i_y_end, i_y_start, iunit, n_E, &
    1538              :                                                             n_x, n_y
    1539              :       REAL(KIND=dp)                                      :: energy
    1540              :       REAL(KIND=dp), DIMENSION(3)                        :: coord, idx
    1541              : 
    1542            6 :       CALL timeset(routineN, handle)
    1543              : 
    1544            6 :       i_x_start = LBOUND(LDOS_2d_bins, 1)
    1545            6 :       i_x_end = UBOUND(LDOS_2d_bins, 1)
    1546            6 :       i_y_start = LBOUND(LDOS_2d_bins, 2)
    1547            6 :       i_y_end = UBOUND(LDOS_2d_bins, 2)
    1548            6 :       n_E = SIZE(LDOS_2d_bins, 3)
    1549              : 
    1550            6 :       n_x = i_x_end - i_x_start + 1
    1551            6 :       n_y = i_y_end - i_y_start + 1
    1552              : 
    1553            6 :       IF (bs_env%para_env%is_source()) THEN
    1554              : 
    1555           15 :          DO i_y = i_y_start, i_y_end
    1556           63 :             DO i_x = i_x_start, i_x_end
    1557              : 
    1558           48 :                idx(1) = (REAL(i_x, KIND=dp) - 0.5_dp)/REAL(n_x, KIND=dp)
    1559           48 :                idx(2) = (REAL(i_y, KIND=dp) - 0.5_dp)/REAL(n_y, KIND=dp)
    1560           48 :                idx(3) = 0.0_dp
    1561          624 :                coord(1:3) = MATMUL(bs_env%hmat, idx)
    1562              : 
    1563           48 :                CALL get_print_format(coord(1), print_format_1)
    1564           48 :                CALL get_print_format(coord(2), print_format_2)
    1565              : 
    1566           48 :                print_format = "(3A,"//print_format_1//",A,"//print_format_2//",A)"
    1567              : 
    1568           48 :                WRITE (fname, print_format) "LDOS_", scf_gw_soc, &
    1569           96 :                   "_at_x_", coord(1)*angstrom, '_A_and_y_', coord(2)*angstrom, '_A'
    1570              : 
    1571              :                CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", &
    1572           48 :                               file_action="WRITE")
    1573              : 
    1574           48 :                WRITE (iunit, "(2A)") "        Energy E (eV)    average LDOS(x,y,E) (1/(eV*Å^2), ", &
    1575           96 :                   "integrated over z, averaged inside bin)"
    1576              : 
    1577        13424 :                DO i_E = 1, n_E
    1578        13376 :                   energy = E_min + i_E*bs_env%energy_step_DOS
    1579        13376 :                   WRITE (iunit, "(2F17.3)") energy*evolt, &
    1580              :                      LDOS_2d_bins(i_x, i_y, i_E)* &
    1581        26800 :                      bs_env%unit_ldos_int_z_inv_Ang2_eV
    1582              :                END DO
    1583              : 
    1584           60 :                CALL close_file(iunit)
    1585              : 
    1586              :             END DO
    1587              :          END DO
    1588              : 
    1589              :       END IF
    1590              : 
    1591            6 :       CALL timestop(handle)
    1592              : 
    1593            6 :    END SUBROUTINE print_LDOS_2d_bins
    1594              : 
    1595              : ! **************************************************************************************************
    1596              : !> \brief ...
    1597              : !> \param coord ...
    1598              : !> \param print_format ...
    1599              : ! **************************************************************************************************
    1600           96 :    SUBROUTINE get_print_format(coord, print_format)
    1601              :       REAL(KIND=dp)                                      :: coord
    1602              :       CHARACTER(LEN=4)                                   :: print_format
    1603              : 
    1604              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'get_print_format'
    1605              : 
    1606              :       INTEGER                                            :: handle
    1607              : 
    1608           96 :       CALL timeset(routineN, handle)
    1609              : 
    1610           96 :       IF (coord < -10000/angstrom) THEN
    1611            0 :          print_format = "F9.2"
    1612           96 :       ELSE IF (coord < -1000/angstrom) THEN
    1613            0 :          print_format = "F8.2"
    1614           96 :       ELSE IF (coord < -100/angstrom) THEN
    1615            0 :          print_format = "F7.2"
    1616           96 :       ELSE IF (coord < -10/angstrom) THEN
    1617            0 :          print_format = "F6.2"
    1618           96 :       ELSE IF (coord < -1/angstrom) THEN
    1619            0 :          print_format = "F5.2"
    1620           96 :       ELSE IF (coord < 10/angstrom) THEN
    1621           96 :          print_format = "F4.2"
    1622            0 :       ELSE IF (coord < 100/angstrom) THEN
    1623            0 :          print_format = "F5.2"
    1624            0 :       ELSE IF (coord < 1000/angstrom) THEN
    1625            0 :          print_format = "F6.2"
    1626            0 :       ELSE IF (coord < 10000/angstrom) THEN
    1627            0 :          print_format = "F7.2"
    1628              :       ELSE
    1629            0 :          print_format = "F8.2"
    1630              :       END IF
    1631              : 
    1632           96 :       CALL timestop(handle)
    1633              : 
    1634           96 :    END SUBROUTINE get_print_format
    1635              : 
    1636              : ! **************************************************************************************************
    1637              : !> \brief ...
    1638              : !> \param bs_env ...
    1639              : !> \param qs_env ...
    1640              : !> \param ikp ...
    1641              : !> \param eigenval_no_SOC ...
    1642              : !> \param band_edges_no_SOC ...
    1643              : !> \param E_min ...
    1644              : !> \param cfm_mos_ikp ...
    1645              : !> \param DOS ...
    1646              : !> \param PDOS ...
    1647              : !> \param band_edges ...
    1648              : !> \param eigenval_spinor ...
    1649              : !> \param cfm_spinor_wf_ikp ...
    1650              : ! **************************************************************************************************
    1651          400 :    SUBROUTINE SOC_ev(bs_env, qs_env, ikp, eigenval_no_SOC, band_edges_no_SOC, E_min, cfm_mos_ikp, &
    1652              :                      DOS, PDOS, band_edges, eigenval_spinor, cfm_spinor_wf_ikp)
    1653              : 
    1654              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1655              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1656              :       INTEGER                                            :: ikp
    1657              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: eigenval_no_SOC
    1658              :       TYPE(band_edges_type)                              :: band_edges_no_SOC
    1659              :       REAL(KIND=dp)                                      :: E_min
    1660              :       TYPE(cp_cfm_type), DIMENSION(2)                    :: cfm_mos_ikp
    1661              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: DOS
    1662              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    1663              :       TYPE(band_edges_type)                              :: band_edges
    1664              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor
    1665              :       TYPE(cp_cfm_type)                                  :: cfm_spinor_wf_ikp
    1666              : 
    1667              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'SOC_ev'
    1668              : 
    1669              :       INTEGER                                            :: handle, homo_spinor, n_ao, n_E, nkind
    1670              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor_no_SOC
    1671              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind_spinor
    1672              :       TYPE(cp_cfm_type)                                  :: cfm_eigenvec_ikp_spinor, &
    1673              :                                                             cfm_ks_ikp_spinor, cfm_mos_ikp_spinor, &
    1674              :                                                             cfm_SOC_ikp_spinor, cfm_work_ikp_spinor
    1675              : 
    1676          400 :       CALL timeset(routineN, handle)
    1677              : 
    1678          400 :       n_ao = bs_env%n_ao
    1679          400 :       homo_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    1680          400 :       CALL get_qs_env(qs_env, nkind=nkind)
    1681              : 
    1682          400 :       CALL cp_cfm_create(cfm_ks_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1683          400 :       CALL cp_cfm_create(cfm_SOC_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1684          400 :       CALL cp_cfm_create(cfm_mos_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1685          400 :       CALL cp_cfm_create(cfm_work_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1686          400 :       CALL cp_cfm_create(cfm_eigenvec_ikp_spinor, bs_env%cfm_SOC_spinor_ao(1)%matrix_struct)
    1687              : 
    1688         1200 :       ALLOCATE (eigenval_spinor_no_SOC(2*n_ao))
    1689         1600 :       ALLOCATE (proj_mo_on_kind_spinor(2*n_ao, nkind))
    1690              :       ! PDOS not yet implemented -> projection is just zero -> PDOS is zero
    1691          400 :       proj_mo_on_kind_spinor(:, :) = 0.0_dp
    1692              : 
    1693              :       ! 1. get V^SOC_µν,σσ'(k_i)
    1694          420 :       SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
    1695              :       CASE (large_cell_Gamma, large_cell_Gamma_ri_rs, non_periodic_ri_rs)
    1696              : 
    1697              :          ! 1. get V^SOC_µν,σσ'(k_i) from V^SOC_µν,σσ'(k=0)
    1698              :          CALL cfm_ikp_from_cfm_spinor_Gamma(cfm_SOC_ikp_spinor, &
    1699              :                                             bs_env%cfm_SOC_spinor_ao(1), &
    1700              :                                             bs_env%fm_s_Gamma%matrix_struct, &
    1701           20 :                                             ikp, qs_env, bs_env%kpoints_DOS, "ORB")
    1702              : 
    1703              :       CASE (small_cell_full_kp)
    1704              : 
    1705              :          ! 1. V^SOC_µν,σσ'(k_i) already there
    1706          400 :          CALL cp_cfm_to_cfm(bs_env%cfm_SOC_spinor_ao(ikp), cfm_SOC_ikp_spinor)
    1707              : 
    1708              :       END SELECT
    1709              : 
    1710              :       ! 2. V^SOC_nn',σσ'(k_i) = sum_µν C^*_µn,σ(k_i) V^SOC_µν,σσ'(k_i) C_νn'(k_i),
    1711              :       !    C_µn,σ(k_i): MO coefficiencts from diagonalizing KS-matrix h^KS_nn',σσ'(k_i)
    1712              : 
    1713              :       ! 2.1 build matrix C_µn,σ(k_i)
    1714          400 :       CALL cp_cfm_set_all(cfm_mos_ikp_spinor, z_zero)
    1715          400 :       CALL add_cfm_submat(cfm_mos_ikp_spinor, cfm_mos_ikp(1), 1, 1)
    1716          400 :       CALL add_cfm_submat(cfm_mos_ikp_spinor, cfm_mos_ikp(bs_env%n_spin), n_ao + 1, n_ao + 1)
    1717              : 
    1718              :       ! 2.2 work_nν,σσ' = sum_µ C^*_µn,σ(k_i) V^SOC_µν,σσ'(k_i)
    1719              :       CALL parallel_gemm('C', 'N', 2*n_ao, 2*n_ao, 2*n_ao, z_one, &
    1720              :                          cfm_mos_ikp_spinor, cfm_SOC_ikp_spinor, &
    1721          400 :                          z_zero, cfm_work_ikp_spinor)
    1722              : 
    1723              :       ! 2.3 V^SOC_nn',σσ'(k_i) = sum_ν work_nν,σσ' C_νn'(k_i)
    1724              :       CALL parallel_gemm('N', 'N', 2*n_ao, 2*n_ao, 2*n_ao, z_one, &
    1725              :                          cfm_work_ikp_spinor, cfm_mos_ikp_spinor, &
    1726          400 :                          z_zero, cfm_ks_ikp_spinor)
    1727              : 
    1728              :       ! 3. remove SOC outside of energy window (otherwise, numerical problems arise
    1729              :       !    because energetically low semicore states and energetically very high
    1730              :       !    unbound states couple to the states around the Fermi level)
    1731         4960 :       eigenval_spinor_no_SOC(1:n_ao) = eigenval_no_SOC(1:n_ao, ikp, 1)
    1732         4960 :       eigenval_spinor_no_SOC(n_ao + 1:) = eigenval_no_SOC(1:n_ao, ikp, bs_env%n_spin)
    1733          400 :       IF (bs_env%energy_window_soc > 0.0_dp) THEN
    1734              :          CALL remove_soc_outside_energy_window_mo(cfm_ks_ikp_spinor, &
    1735              :                                                   bs_env%energy_window_soc, &
    1736              :                                                   eigenval_spinor_no_SOC, &
    1737              :                                                   band_edges_no_SOC%VBM, &
    1738          400 :                                                   band_edges_no_SOC%CBM)
    1739              :       END IF
    1740              : 
    1741              :       ! 4. h^G0W0+SOC_nn',σσ'(k_i) = ε_nσ^G0W0(k_i) δ_nn' δ_σσ' + V^SOC_nn',σσ'(k_i)
    1742          400 :       CALL cfm_add_on_diag(cfm_ks_ikp_spinor, eigenval_spinor_no_SOC)
    1743              : 
    1744              :       ! 5. diagonalize h^G0W0+SOC_nn',σσ'(k_i) to get eigenvalues
    1745          400 :       CALL cp_cfm_heevd(cfm_ks_ikp_spinor, cfm_eigenvec_ikp_spinor, eigenval_spinor)
    1746              : 
    1747              :       ! 6. DOS from spinors, no PDOS
    1748          400 :       IF (bs_env%do_dos_pdos) THEN
    1749          196 :          n_E = SIZE(DOS)
    1750              :          CALL add_to_DOS_PDOS(DOS, PDOS, eigenval_spinor, &
    1751          196 :                               ikp, bs_env, n_E, E_min, proj_mo_on_kind_spinor)
    1752              :       END IF
    1753              : 
    1754              :       ! 7. valence band max. (VBM), conduction band min. (CBM) and direct bandgap (DBG)
    1755          400 :       band_edges%VBM = MAX(band_edges%VBM, eigenval_spinor(homo_spinor))
    1756          400 :       band_edges%CBM = MIN(band_edges%CBM, eigenval_spinor(homo_spinor + 1))
    1757              :       band_edges%DBG = MIN(band_edges%DBG, eigenval_spinor(homo_spinor + 1) &
    1758          400 :                            - eigenval_spinor(homo_spinor))
    1759              : 
    1760              :       ! 8. spinor wavefunctions:
    1761              :       CALL parallel_gemm('N', 'N', 2*n_ao, 2*n_ao, 2*n_ao, z_one, &
    1762              :                          cfm_mos_ikp_spinor, cfm_eigenvec_ikp_spinor, &
    1763          400 :                          z_zero, cfm_spinor_wf_ikp)
    1764              : 
    1765          400 :       CALL cp_cfm_release(cfm_ks_ikp_spinor)
    1766          400 :       CALL cp_cfm_release(cfm_SOC_ikp_spinor)
    1767          400 :       CALL cp_cfm_release(cfm_work_ikp_spinor)
    1768          400 :       CALL cp_cfm_release(cfm_eigenvec_ikp_spinor)
    1769          400 :       CALL cp_cfm_release(cfm_mos_ikp_spinor)
    1770              : 
    1771          400 :       CALL timestop(handle)
    1772              : 
    1773         1200 :    END SUBROUTINE SOC_ev
    1774              : 
    1775              : ! **************************************************************************************************
    1776              : !> \brief ...
    1777              : !> \param DOS ...
    1778              : !> \param PDOS ...
    1779              : !> \param eigenval ...
    1780              : !> \param ikp ...
    1781              : !> \param bs_env ...
    1782              : !> \param n_E ...
    1783              : !> \param E_min ...
    1784              : !> \param proj_mo_on_kind ...
    1785              : ! **************************************************************************************************
    1786          408 :    SUBROUTINE add_to_DOS_PDOS(DOS, PDOS, eigenval, ikp, bs_env, n_E, E_min, proj_mo_on_kind)
    1787              : 
    1788              :       REAL(KIND=dp), DIMENSION(:)                        :: DOS
    1789              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    1790              :       REAL(KIND=dp), DIMENSION(:)                        :: eigenval
    1791              :       INTEGER                                            :: ikp
    1792              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1793              :       INTEGER                                            :: n_E
    1794              :       REAL(KIND=dp)                                      :: E_min
    1795              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind
    1796              : 
    1797              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'add_to_DOS_PDOS'
    1798              : 
    1799              :       INTEGER                                            :: handle, i_E, i_kind, i_mo, n_mo, nkind
    1800              :       REAL(KIND=dp)                                      :: broadening, energy, energy_step_DOS, wkp
    1801              : 
    1802          408 :       CALL timeset(routineN, handle)
    1803              : 
    1804          408 :       energy_step_DOS = bs_env%energy_step_DOS
    1805          408 :       broadening = bs_env%broadening_DOS
    1806              : 
    1807          408 :       n_mo = SIZE(eigenval)
    1808          408 :       nkind = SIZE(proj_mo_on_kind, 2)
    1809              : 
    1810              :       ! normalize to closed-shell / open-shell
    1811          408 :       wkp = bs_env%kpoints_DOS%wkp(ikp)*bs_env%spin_degeneracy
    1812       912984 :       DO i_E = 1, n_E
    1813       912576 :          energy = E_min + i_E*energy_step_DOS
    1814     20122036 :          DO i_mo = 1, n_mo
    1815              :             ! DOS
    1816     19209052 :             DOS(i_E) = DOS(i_E) + wkp*Gaussian(energy - eigenval(i_mo), broadening)
    1817              : 
    1818              :             ! PDOS
    1819     58539732 :             DO i_kind = 1, nkind
    1820     57627156 :                IF (proj_mo_on_kind(i_mo, i_kind) > 0.0_dp) THEN
    1821              :                   PDOS(i_E, i_kind) = PDOS(i_E, i_kind) + &
    1822              :                                       proj_mo_on_kind(i_mo, i_kind)*wkp* &
    1823     12536764 :                                       Gaussian(energy - eigenval(i_mo), broadening)
    1824              :                END IF
    1825              :             END DO
    1826              :          END DO
    1827              :       END DO
    1828              : 
    1829          408 :       CALL timestop(handle)
    1830              : 
    1831          408 :    END SUBROUTINE add_to_DOS_PDOS
    1832              : 
    1833              : ! **************************************************************************************************
    1834              : !> \brief ...
    1835              : !> \param LDOS_2d ...
    1836              : !> \param qs_env ...
    1837              : !> \param ikp ...
    1838              : !> \param bs_env ...
    1839              : !> \param cfm_mos_ikp ...
    1840              : !> \param eigenval ...
    1841              : !> \param band_edges ...
    1842              : !> \param do_spinor ...
    1843              : !> \param cfm_non_spinor ...
    1844              : ! **************************************************************************************************
    1845            6 :    SUBROUTINE add_to_LDOS_2d(LDOS_2d, qs_env, ikp, bs_env, cfm_mos_ikp, eigenval, &
    1846              :                              band_edges, do_spinor, cfm_non_spinor)
    1847              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: LDOS_2d
    1848              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1849              :       INTEGER                                            :: ikp
    1850              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    1851              :       TYPE(cp_cfm_type)                                  :: cfm_mos_ikp
    1852              :       REAL(KIND=dp), DIMENSION(:)                        :: eigenval
    1853              :       TYPE(band_edges_type)                              :: band_edges
    1854              :       LOGICAL, OPTIONAL                                  :: do_spinor
    1855              :       TYPE(cp_cfm_type), OPTIONAL                        :: cfm_non_spinor
    1856              : 
    1857              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'add_to_LDOS_2d'
    1858              : 
    1859              :       INTEGER :: handle, i_E, i_x_end, i_x_start, i_y_end, i_y_start, i_z, i_z_end, i_z_start, &
    1860              :          j_col, j_mo, n_E, n_mo, n_z, ncol_local, nimages, z_end_global, z_start_global
    1861            6 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices
    1862              :       LOGICAL                                            :: is_any_weight_non_zero, my_do_spinor
    1863              :       REAL(KIND=dp)                                      :: broadening, E_max, E_min, &
    1864              :                                                             E_total_window, energy, energy_step, &
    1865              :                                                             energy_window, spin_degeneracy, weight
    1866              :       TYPE(cp_cfm_type)                                  :: cfm_weighted_dm_ikp, cfm_work
    1867              :       TYPE(cp_fm_type)                                   :: fm_non_spinor, fm_weighted_dm_MIC
    1868            6 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: weighted_dm_MIC
    1869              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1870              :       TYPE(pw_c1d_gs_type)                               :: rho_g
    1871              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1872              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1873              :       TYPE(pw_r3d_rs_type)                               :: LDOS_3d
    1874              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1875              : 
    1876            6 :       CALL timeset(routineN, handle)
    1877              : 
    1878            6 :       my_do_spinor = .FALSE.
    1879            6 :       IF (PRESENT(do_spinor)) my_do_spinor = do_spinor
    1880              : 
    1881            6 :       CALL get_qs_env(qs_env, ks_env=ks_env, pw_env=pw_env, dft_control=dft_control)
    1882              : 
    1883              :       ! previously, dft_control%nimages set to # neighbor cells, revert for Γ-only KS matrix
    1884            6 :       nimages = dft_control%nimages
    1885            6 :       dft_control%nimages = bs_env%nimages_scf
    1886              : 
    1887            6 :       energy_window = bs_env%energy_window_DOS
    1888            6 :       energy_step = bs_env%energy_step_DOS
    1889            6 :       broadening = bs_env%broadening_DOS
    1890              : 
    1891            6 :       E_min = band_edges%VBM - 0.5_dp*energy_window
    1892            6 :       E_max = band_edges%CBM + 0.5_dp*energy_window
    1893            6 :       E_total_window = E_max - E_min
    1894              : 
    1895            6 :       n_E = INT(E_total_window/energy_step)
    1896              : 
    1897            6 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    1898              : 
    1899            6 :       CALL auxbas_pw_pool%create_pw(LDOS_3d)
    1900            6 :       CALL auxbas_pw_pool%create_pw(rho_g)
    1901              : 
    1902            6 :       i_x_start = LBOUND(LDOS_3d%array, 1)
    1903            6 :       i_x_end = UBOUND(LDOS_3d%array, 1)
    1904            6 :       i_y_start = LBOUND(LDOS_3d%array, 2)
    1905            6 :       i_y_end = UBOUND(LDOS_3d%array, 2)
    1906            6 :       i_z_start = LBOUND(LDOS_3d%array, 3)
    1907            6 :       i_z_end = UBOUND(LDOS_3d%array, 3)
    1908              : 
    1909            6 :       z_start_global = i_z_start
    1910            6 :       z_end_global = i_z_end
    1911              : 
    1912            6 :       CALL bs_env%para_env%min(z_start_global)
    1913            6 :       CALL bs_env%para_env%max(z_end_global)
    1914            6 :       n_z = z_end_global - z_start_global + 1
    1915              : 
    1916           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)) &
    1917            0 :          CPABORT("Please choose a cell that has 90° angles to the z-direction.")
    1918              :       ! for integration, we need the dz and the conversion from H -> eV and a_Bohr -> Å
    1919            6 :       bs_env%unit_ldos_int_z_inv_Ang2_eV = bs_env%hmat(3, 3)/REAL(n_z, KIND=dp)/evolt/angstrom**2
    1920              : 
    1921            6 :       IF (ikp == 1) THEN
    1922           30 :          ALLOCATE (LDOS_2d(i_x_start:i_x_end, i_y_start:i_y_end, n_E))
    1923            6 :          LDOS_2d(:, :, :) = 0.0_dp
    1924              :       END IF
    1925              : 
    1926            6 :       CALL cp_cfm_create(cfm_work, cfm_mos_ikp%matrix_struct)
    1927            6 :       CALL cp_cfm_create(cfm_weighted_dm_ikp, cfm_mos_ikp%matrix_struct)
    1928            6 :       CALL cp_fm_create(fm_weighted_dm_MIC, cfm_mos_ikp%matrix_struct)
    1929            6 :       IF (my_do_spinor) THEN
    1930            2 :          CALL cp_fm_create(fm_non_spinor, cfm_non_spinor%matrix_struct)
    1931              :       END IF
    1932              : 
    1933              :       CALL cp_cfm_get_info(matrix=cfm_mos_ikp, &
    1934              :                            ncol_global=n_mo, &
    1935              :                            ncol_local=ncol_local, &
    1936            6 :                            col_indices=col_indices)
    1937              : 
    1938            6 :       NULLIFY (weighted_dm_MIC)
    1939            6 :       CALL dbcsr_allocate_matrix_set(weighted_dm_MIC, 1)
    1940            6 :       ALLOCATE (weighted_dm_MIC(1)%matrix)
    1941              :       CALL dbcsr_create(weighted_dm_MIC(1)%matrix, template=bs_env%mat_ao_ao%matrix, &
    1942            6 :                         matrix_type=dbcsr_type_symmetric)
    1943              : 
    1944         1678 :       DO i_E = 1, n_E
    1945              : 
    1946         1672 :          energy = E_min + i_E*energy_step
    1947              : 
    1948         1672 :          is_any_weight_non_zero = .FALSE.
    1949              : 
    1950        20950 :          DO j_col = 1, ncol_local
    1951              : 
    1952        19278 :             j_mo = col_indices(j_col)
    1953              : 
    1954        19278 :             IF (my_do_spinor) THEN
    1955              :                spin_degeneracy = 1.0_dp
    1956              :             ELSE
    1957        10818 :                spin_degeneracy = bs_env%spin_degeneracy
    1958              :             END IF
    1959              : 
    1960        19278 :             weight = Gaussian(energy - eigenval(j_mo), broadening)*spin_degeneracy
    1961              : 
    1962       144099 :             cfm_work%local_data(:, j_col) = cfm_mos_ikp%local_data(:, j_col)*weight
    1963              : 
    1964        20950 :             IF (weight > 1.0E-5_dp) is_any_weight_non_zero = .TRUE.
    1965              : 
    1966              :          END DO
    1967              : 
    1968         1672 :          CALL bs_env%para_env%sync()
    1969         1672 :          CALL bs_env%para_env%sum(is_any_weight_non_zero)
    1970         1672 :          CALL bs_env%para_env%sync()
    1971              : 
    1972              :          ! cycle if there are no states at the energy i_E
    1973         1678 :          IF (is_any_weight_non_zero) THEN
    1974              : 
    1975              :             CALL parallel_gemm('N', 'C', n_mo, n_mo, n_mo, z_one, &
    1976           24 :                                cfm_mos_ikp, cfm_work, z_zero, cfm_weighted_dm_ikp)
    1977              : 
    1978           24 :             IF (my_do_spinor) THEN
    1979              : 
    1980              :                ! contribution from up,up to fm_non_spinor
    1981            8 :                CALL get_cfm_submat(cfm_non_spinor, cfm_weighted_dm_ikp, 1, 1)
    1982            8 :                CALL cp_fm_set_all(fm_non_spinor, 0.0_dp)
    1983              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_non_spinor, &
    1984              :                                               cfm_non_spinor, ikp, bs_env%kpoints_DOS, &
    1985            8 :                                               "ORB", bs_env%kpoints_DOS%wkp(ikp))
    1986              : 
    1987              :                ! add contribution from down,down to fm_non_spinor
    1988            8 :                CALL get_cfm_submat(cfm_non_spinor, cfm_weighted_dm_ikp, n_mo/2, n_mo/2)
    1989              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_non_spinor, &
    1990              :                                               cfm_non_spinor, ikp, bs_env%kpoints_DOS, &
    1991            8 :                                               "ORB", bs_env%kpoints_DOS%wkp(ikp))
    1992              :                CALL copy_fm_to_dbcsr(fm_non_spinor, weighted_dm_MIC(1)%matrix, &
    1993            8 :                                      keep_sparsity=.FALSE.)
    1994              :             ELSE
    1995           16 :                CALL cp_fm_set_all(fm_weighted_dm_MIC, 0.0_dp)
    1996              :                CALL MIC_contribution_from_ikp(bs_env, qs_env, fm_weighted_dm_MIC, &
    1997              :                                               cfm_weighted_dm_ikp, ikp, bs_env%kpoints_DOS, &
    1998           16 :                                               "ORB", bs_env%kpoints_DOS%wkp(ikp))
    1999              :                CALL copy_fm_to_dbcsr(fm_weighted_dm_MIC, weighted_dm_MIC(1)%matrix, &
    2000           16 :                                      keep_sparsity=.FALSE.)
    2001              :             END IF
    2002              : 
    2003       338424 :             LDOS_3d%array(:, :, :) = 0.0_dp
    2004              : 
    2005              :             CALL calculate_rho_elec(matrix_p_kp=weighted_dm_MIC, &
    2006              :                                     rho=LDOS_3d, &
    2007              :                                     rho_gspace=rho_g, &
    2008           24 :                                     ks_env=ks_env)
    2009              : 
    2010          504 :             DO i_z = i_z_start, i_z_end
    2011       338424 :                LDOS_2d(:, :, i_E) = LDOS_2d(:, :, i_E) + LDOS_3d%array(:, :, i_z)
    2012              :             END DO
    2013              : 
    2014              :          END IF
    2015              : 
    2016              :       END DO
    2017              : 
    2018              :       ! set back nimages
    2019            6 :       dft_control%nimages = nimages
    2020              : 
    2021            6 :       CALL auxbas_pw_pool%give_back_pw(LDOS_3d)
    2022            6 :       CALL auxbas_pw_pool%give_back_pw(rho_g)
    2023              : 
    2024            6 :       CALL cp_cfm_release(cfm_work)
    2025            6 :       CALL cp_cfm_release(cfm_weighted_dm_ikp)
    2026              : 
    2027            6 :       CALL cp_fm_release(fm_weighted_dm_MIC)
    2028              : 
    2029            6 :       CALL dbcsr_deallocate_matrix_set(weighted_dm_MIC)
    2030              : 
    2031            6 :       IF (my_do_spinor) THEN
    2032            2 :          CALL cp_fm_release(fm_non_spinor)
    2033              :       END IF
    2034              : 
    2035            6 :       CALL timestop(handle)
    2036              : 
    2037            6 :    END SUBROUTINE add_to_LDOS_2d
    2038              : 
    2039              : ! **************************************************************************************************
    2040              : !> \brief ...
    2041              : !> \param eigenval_spinor ...
    2042              : !> \param ikp_for_file ...
    2043              : !> \param ikp ...
    2044              : !> \param bs_env ...
    2045              : !> \param eigenval_spinor_G0W0 ...
    2046              : ! **************************************************************************************************
    2047          168 :    SUBROUTINE write_SOC_eigenvalues(eigenval_spinor, ikp_for_file, ikp, bs_env, eigenval_spinor_G0W0)
    2048              : 
    2049              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenval_spinor
    2050              :       INTEGER                                            :: ikp_for_file, ikp
    2051              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2052              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:), OPTIONAL :: eigenval_spinor_G0W0
    2053              : 
    2054              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'write_SOC_eigenvalues'
    2055              : 
    2056              :       CHARACTER(len=3)                                   :: occ_vir
    2057              :       CHARACTER(LEN=default_string_length)               :: fname
    2058              :       INTEGER                                            :: handle, i_mo, iunit, n_occ_spinor
    2059              : 
    2060          168 :       CALL timeset(routineN, handle)
    2061              : 
    2062          168 :       fname = "bandstructure_SCF_and_G0W0_plus_SOC"
    2063              : 
    2064          168 :       IF (bs_env%para_env%is_source()) THEN
    2065              : 
    2066           84 :          IF (ikp_for_file == 1) THEN
    2067              :             CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", &
    2068            7 :                            file_action="WRITE")
    2069              :          ELSE
    2070              :             CALL open_file(TRIM(fname), unit_number=iunit, file_status="OLD", &
    2071           77 :                            file_action="WRITE", file_position="APPEND")
    2072              :          END IF
    2073              : 
    2074           84 :          WRITE (iunit, "(A)") " "
    2075           84 :          WRITE (iunit, "(A10,I7,A25,3F10.4)") "kpoint: ", ikp_for_file, "coordinate: ", &
    2076          420 :             bs_env%kpoints_DOS%xkp(:, ikp)
    2077           84 :          WRITE (iunit, "(A)") " "
    2078              : 
    2079           84 :          IF (PRESENT(eigenval_spinor_G0W0)) THEN
    2080              :             ! SCF+SOC and G0W0+SOC eigenvalues
    2081           84 :             WRITE (iunit, "(A5,A12,2A22)") "n", "k", "ϵ_nk^DFT+SOC (eV)", "ϵ_nk^G0W0+SOC (eV)"
    2082              :          ELSE
    2083              :             ! SCF+SOC eigenvalues only
    2084            0 :             WRITE (iunit, "(A5,A12,A22)") "n", "k", "ϵ_nk^DFT+SOC (eV)"
    2085              :          END IF
    2086              : 
    2087           84 :          n_occ_spinor = bs_env%n_occ(1) + bs_env%n_occ(bs_env%n_spin)
    2088              : 
    2089         2076 :          DO i_mo = 1, SIZE(eigenval_spinor)
    2090         1992 :             IF (i_mo <= n_occ_spinor) occ_vir = 'occ'
    2091         1992 :             IF (i_mo > n_occ_spinor) occ_vir = 'vir'
    2092         2076 :             IF (PRESENT(eigenval_spinor_G0W0)) THEN
    2093              :                ! SCF+SOC and G0W0+SOC eigenvalues
    2094         1992 :                WRITE (iunit, "(I5,3A,I5,4F16.3,2F17.3)") i_mo, ' (', occ_vir, ') ', &
    2095         3984 :                   ikp_for_file, eigenval_spinor(i_mo)*evolt, eigenval_spinor_G0W0(i_mo)*evolt
    2096              :             ELSE
    2097              :                ! SCF+SOC eigenvalues only
    2098            0 :                WRITE (iunit, "(I5,3A,I5,4F16.3,F17.3)") i_mo, ' (', occ_vir, ') ', &
    2099            0 :                   ikp_for_file, eigenval_spinor(i_mo)*evolt
    2100              :             END IF
    2101              :          END DO
    2102              : 
    2103           84 :          CALL close_file(iunit)
    2104              : 
    2105              :       END IF
    2106              : 
    2107          168 :       CALL timestop(handle)
    2108              : 
    2109          168 :    END SUBROUTINE write_SOC_eigenvalues
    2110              : 
    2111              : ! **************************************************************************************************
    2112              : !> \brief ...
    2113              : !> \param int_number ...
    2114              : !> \return ...
    2115              : ! **************************************************************************************************
    2116            0 :    PURE FUNCTION count_digits(int_number)
    2117              : 
    2118              :       INTEGER, INTENT(IN)                                :: int_number
    2119              :       INTEGER                                            :: count_digits
    2120              : 
    2121              :       INTEGER                                            :: digitCount, tempInt
    2122              : 
    2123            0 :       digitCount = 0
    2124              : 
    2125            0 :       tempInt = int_number
    2126              : 
    2127            0 :       DO WHILE (tempInt /= 0)
    2128            0 :          tempInt = tempInt/10
    2129            0 :          digitCount = digitCount + 1
    2130              :       END DO
    2131              : 
    2132            0 :       count_digits = digitCount
    2133              : 
    2134            0 :    END FUNCTION count_digits
    2135              : 
    2136              : ! **************************************************************************************************
    2137              : !> \brief ...
    2138              : !> \param band_edges ...
    2139              : !> \param scf_gw_soc ...
    2140              : !> \param bs_env ...
    2141              : ! **************************************************************************************************
    2142          156 :    SUBROUTINE write_band_edges(band_edges, scf_gw_soc, bs_env)
    2143              : 
    2144              :       TYPE(band_edges_type)                              :: band_edges
    2145              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    2146              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2147              : 
    2148              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'write_band_edges'
    2149              : 
    2150              :       CHARACTER(LEN=17)                                  :: print_format
    2151              :       INTEGER                                            :: handle, u
    2152              : 
    2153          156 :       CALL timeset(routineN, handle)
    2154              : 
    2155              :       ! print format
    2156          156 :       print_format = "(T2,2A,T61,F20.3)"
    2157              : 
    2158          156 :       u = bs_env%unit_nr
    2159          156 :       IF (u > 0) THEN
    2160           78 :          WRITE (u, '(T2,A)') ''
    2161           78 :          WRITE (u, print_format) scf_gw_soc, ' valence band maximum (eV):', band_edges%VBM*evolt
    2162           78 :          WRITE (u, print_format) scf_gw_soc, ' conduction band minimum (eV):', band_edges%CBM*evolt
    2163           78 :          WRITE (u, print_format) scf_gw_soc, ' indirect band gap (eV):', band_edges%IDBG*evolt
    2164           78 :          WRITE (u, print_format) scf_gw_soc, ' direct band gap (eV):', band_edges%DBG*evolt
    2165              :       END IF
    2166              : 
    2167          156 :       CALL timestop(handle)
    2168              : 
    2169          156 :    END SUBROUTINE write_band_edges
    2170              : 
    2171              : ! **************************************************************************************************
    2172              : !> \brief ...
    2173              : !> \param DOS ...
    2174              : !> \param PDOS ...
    2175              : !> \param bs_env ...
    2176              : !> \param qs_env ...
    2177              : !> \param scf_gw_soc ...
    2178              : !> \param E_min ...
    2179              : !> \param E_VBM ...
    2180              : ! **************************************************************************************************
    2181           40 :    SUBROUTINE write_dos_pdos(DOS, PDOS, bs_env, qs_env, scf_gw_soc, E_min, E_VBM)
    2182              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: DOS
    2183              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: PDOS
    2184              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2185              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2186              :       CHARACTER(LEN=*)                                   :: scf_gw_soc
    2187              :       REAL(KIND=dp)                                      :: E_min, E_VBM
    2188              : 
    2189              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'write_dos_pdos'
    2190              : 
    2191              :       CHARACTER(LEN=3), DIMENSION(100)                   :: elements
    2192              :       CHARACTER(LEN=default_string_length)               :: atom_name, fname, output_string
    2193              :       INTEGER                                            :: handle, i_E, i_kind, iatom, iunit, n_A, &
    2194              :                                                             n_E, nkind
    2195              :       REAL(KIND=dp)                                      :: energy
    2196           40 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2197              : 
    2198           40 :       CALL timeset(routineN, handle)
    2199              : 
    2200           40 :       WRITE (fname, "(3A)") "DOS_PDOS_", scf_gw_soc, ".out"
    2201              : 
    2202           40 :       n_E = SIZE(PDOS, 1)
    2203           40 :       nkind = SIZE(PDOS, 2)
    2204           40 :       CALL get_qs_env(qs_env, particle_set=particle_set)
    2205              : 
    2206           40 :       IF (bs_env%para_env%is_source()) THEN
    2207              : 
    2208           20 :          CALL open_file(TRIM(fname), unit_number=iunit, file_status="REPLACE", file_action="WRITE")
    2209              : 
    2210           20 :          n_A = 2 + nkind
    2211              : 
    2212           76 :          DO iatom = 1, bs_env%n_atom
    2213              :             CALL get_atomic_kind(atomic_kind=particle_set(iatom)%atomic_kind, &
    2214           56 :                                  kind_number=i_kind, name=atom_name)
    2215           76 :             elements(i_kind) = atom_name(1:3)
    2216              :          END DO
    2217              : 
    2218           20 :          WRITE (output_string, "(A,I1,A)") "(", n_A, "A)"
    2219              : 
    2220           20 :          WRITE (iunit, TRIM(output_string)) "Energy-E_F (eV)    DOS (1/eV)    PDOS (1/eV) ", &
    2221           40 :             " of atom type ", elements(1:nkind)
    2222              : 
    2223           20 :          WRITE (output_string, "(A,I1,A)") "(", n_A, "F13.5)"
    2224              : 
    2225        37446 :          DO i_E = 1, n_E
    2226              :             ! energy is relative to valence band maximum => - E_VBM
    2227        37426 :             energy = E_min + i_E*bs_env%energy_step_DOS - E_VBM
    2228       112298 :             WRITE (iunit, TRIM(output_string)) energy*evolt, DOS(i_E)/evolt, PDOS(i_E, :)/evolt
    2229              :          END DO
    2230              : 
    2231           20 :          CALL close_file(iunit)
    2232              : 
    2233              :       END IF
    2234              : 
    2235           40 :       CALL timestop(handle)
    2236              : 
    2237           40 :    END SUBROUTINE write_dos_pdos
    2238              : 
    2239              : ! **************************************************************************************************
    2240              : !> \brief ...
    2241              : !> \param energy ...
    2242              : !> \param broadening ...
    2243              : !> \return ...
    2244              : ! **************************************************************************************************
    2245     31765094 :    PURE FUNCTION Gaussian(energy, broadening)
    2246              : 
    2247              :       REAL(KIND=dp), INTENT(IN)                          :: energy, broadening
    2248              :       REAL(KIND=dp)                                      :: Gaussian
    2249              : 
    2250     31765094 :       IF (ABS(energy) < 5*broadening) THEN
    2251        49072 :          Gaussian = 1.0_dp/broadening/SQRT(twopi)*EXP(-0.5_dp*energy**2/broadening**2)
    2252              :       ELSE
    2253              :          Gaussian = 0.0_dp
    2254              :       END IF
    2255              : 
    2256     31765094 :    END FUNCTION Gaussian
    2257              : 
    2258              : ! **************************************************************************************************
    2259              : !> \brief ...
    2260              : !> \param proj_mo_on_kind ...
    2261              : !> \param qs_env ...
    2262              : !> \param cfm_mos ...
    2263              : !> \param cfm_s ...
    2264              : ! **************************************************************************************************
    2265          276 :    SUBROUTINE compute_proj_mo_on_kind(proj_mo_on_kind, qs_env, cfm_mos, cfm_s)
    2266              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: proj_mo_on_kind
    2267              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2268              :       TYPE(cp_cfm_type)                                  :: cfm_mos, cfm_s
    2269              : 
    2270              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'compute_proj_mo_on_kind'
    2271              : 
    2272              :       INTEGER                                            :: handle, i_atom, i_global, i_kind, i_row, &
    2273              :                                                             j_col, n_ao, n_mo, ncol_local, nkind, &
    2274              :                                                             nrow_local
    2275          276 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_from_bf, kind_of
    2276          276 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    2277          276 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2278              :       TYPE(cp_cfm_type)                                  :: cfm_proj, cfm_s_i_kind, cfm_work
    2279              :       TYPE(cp_fm_type)                                   :: fm_proj_im, fm_proj_re
    2280              : 
    2281          276 :       CALL timeset(routineN, handle)
    2282              : 
    2283          276 :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, nkind=nkind)
    2284          276 :       CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
    2285              : 
    2286              :       CALL cp_cfm_get_info(matrix=cfm_mos, &
    2287              :                            nrow_global=n_mo, &
    2288              :                            nrow_local=nrow_local, &
    2289              :                            ncol_local=ncol_local, &
    2290              :                            row_indices=row_indices, &
    2291          276 :                            col_indices=col_indices)
    2292              : 
    2293          276 :       n_ao = qs_env%bs_env%n_ao
    2294              : 
    2295          828 :       ALLOCATE (atom_from_bf(n_ao))
    2296          276 :       CALL get_atom_index_from_basis_function_index(qs_env, atom_from_bf, n_ao, "ORB")
    2297              : 
    2298          276 :       proj_mo_on_kind(:, :) = 0.0_dp
    2299              : 
    2300          276 :       CALL cp_cfm_create(cfm_s_i_kind, cfm_s%matrix_struct)
    2301          276 :       CALL cp_cfm_create(cfm_work, cfm_s%matrix_struct)
    2302          276 :       CALL cp_cfm_create(cfm_proj, cfm_s%matrix_struct)
    2303          276 :       CALL cp_fm_create(fm_proj_re, cfm_s%matrix_struct)
    2304          276 :       CALL cp_fm_create(fm_proj_im, cfm_s%matrix_struct)
    2305              : 
    2306          776 :       DO i_kind = 1, nkind
    2307              : 
    2308          500 :          CALL cp_cfm_to_cfm(cfm_s, cfm_s_i_kind)
    2309              : 
    2310              :          ! set entries in overlap matrix to zero which do not belong to atoms of i_kind
    2311         6024 :          DO j_col = 1, ncol_local
    2312        39852 :             DO i_row = 1, nrow_local
    2313              : 
    2314        33828 :                i_global = row_indices(i_row)
    2315              : 
    2316        33828 :                IF (i_global <= n_ao) THEN
    2317        33828 :                   i_atom = atom_from_bf(i_global)
    2318            0 :                ELSE IF (i_global <= 2*n_ao) THEN
    2319            0 :                   i_atom = atom_from_bf(i_global - n_ao)
    2320              :                ELSE
    2321            0 :                   CPABORT("Wrong indices.")
    2322              :                END IF
    2323              : 
    2324        39352 :                IF (i_kind /= kind_of(i_atom)) THEN
    2325        16256 :                   cfm_s_i_kind%local_data(i_row, j_col) = z_zero
    2326              :                END IF
    2327              : 
    2328              :             END DO
    2329              :          END DO
    2330              : 
    2331              :          CALL parallel_gemm('N', 'N', n_mo, n_mo, n_mo, z_one, &
    2332          500 :                             cfm_s_i_kind, cfm_mos, z_zero, cfm_work)
    2333              :          CALL parallel_gemm('C', 'N', n_mo, n_mo, n_mo, z_one, &
    2334          500 :                             cfm_mos, cfm_work, z_zero, cfm_proj)
    2335              : 
    2336          500 :          CALL cp_cfm_to_fm(cfm_proj, fm_proj_re, fm_proj_im)
    2337              : 
    2338          500 :          CALL cp_fm_get_diag(fm_proj_im, proj_mo_on_kind(:, i_kind))
    2339          776 :          CALL cp_fm_get_diag(fm_proj_re, proj_mo_on_kind(:, i_kind))
    2340              : 
    2341              :       END DO ! i_kind
    2342              : 
    2343          276 :       CALL cp_cfm_release(cfm_s_i_kind)
    2344          276 :       CALL cp_cfm_release(cfm_work)
    2345          276 :       CALL cp_cfm_release(cfm_proj)
    2346          276 :       CALL cp_fm_release(fm_proj_re)
    2347          276 :       CALL cp_fm_release(fm_proj_im)
    2348              : 
    2349          276 :       CALL timestop(handle)
    2350              : 
    2351         1104 :    END SUBROUTINE compute_proj_mo_on_kind
    2352              : 
    2353              : ! **************************************************************************************************
    2354              : !> \brief ...
    2355              : !> \param cfm_spinor_ikp ...
    2356              : !> \param cfm_spinor_Gamma ...
    2357              : !> \param fm_struct_non_spinor ...
    2358              : !> \param ikp ...
    2359              : !> \param qs_env ...
    2360              : !> \param kpoints ...
    2361              : !> \param basis_type ...
    2362              : ! **************************************************************************************************
    2363          120 :    SUBROUTINE cfm_ikp_from_cfm_spinor_Gamma(cfm_spinor_ikp, cfm_spinor_Gamma, fm_struct_non_spinor, &
    2364              :                                             ikp, qs_env, kpoints, basis_type)
    2365              :       TYPE(cp_cfm_type)                                  :: cfm_spinor_ikp, cfm_spinor_Gamma
    2366              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_non_spinor
    2367              :       INTEGER                                            :: ikp
    2368              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2369              :       TYPE(kpoint_type), POINTER                         :: kpoints
    2370              :       CHARACTER(LEN=*)                                   :: basis_type
    2371              : 
    2372              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cfm_ikp_from_cfm_spinor_Gamma'
    2373              : 
    2374              :       INTEGER                                            :: handle, i_block, i_offset, j_block, &
    2375              :                                                             j_offset, n_ao
    2376              :       TYPE(cp_cfm_type)                                  :: cfm_non_spinor_Gamma, cfm_non_spinor_ikp
    2377              :       TYPE(cp_fm_type)                                   :: fm_non_spinor_Gamma_im, &
    2378              :                                                             fm_non_spinor_Gamma_re
    2379              : 
    2380           20 :       CALL timeset(routineN, handle)
    2381              : 
    2382           20 :       CALL cp_cfm_create(cfm_non_spinor_Gamma, fm_struct_non_spinor)
    2383           20 :       CALL cp_cfm_create(cfm_non_spinor_ikp, fm_struct_non_spinor)
    2384           20 :       CALL cp_fm_create(fm_non_spinor_Gamma_re, fm_struct_non_spinor)
    2385           20 :       CALL cp_fm_create(fm_non_spinor_Gamma_im, fm_struct_non_spinor)
    2386              : 
    2387           20 :       CALL cp_cfm_get_info(cfm_non_spinor_Gamma, nrow_global=n_ao)
    2388              : 
    2389           20 :       CALL cp_cfm_set_all(cfm_spinor_ikp, z_zero)
    2390              : 
    2391           60 :       DO i_block = 0, 1
    2392          140 :          DO j_block = 0, 1
    2393           80 :             i_offset = i_block*n_ao + 1
    2394           80 :             j_offset = j_block*n_ao + 1
    2395           80 :             CALL get_cfm_submat(cfm_non_spinor_Gamma, cfm_spinor_Gamma, i_offset, j_offset)
    2396           80 :             CALL cp_cfm_to_fm(cfm_non_spinor_Gamma, fm_non_spinor_Gamma_re, fm_non_spinor_Gamma_im)
    2397              : 
    2398              :             ! transform real part of Gamma-point matrix to ikp
    2399              :             CALL cfm_ikp_from_fm_Gamma(cfm_non_spinor_ikp, fm_non_spinor_Gamma_re, &
    2400           80 :                                        ikp, qs_env, kpoints, basis_type)
    2401           80 :             CALL add_cfm_submat(cfm_spinor_ikp, cfm_non_spinor_ikp, i_offset, j_offset)
    2402              : 
    2403              :             ! transform imag part of Gamma-point matrix to ikp
    2404              :             CALL cfm_ikp_from_fm_Gamma(cfm_non_spinor_ikp, fm_non_spinor_Gamma_im, &
    2405           80 :                                        ikp, qs_env, kpoints, basis_type)
    2406          120 :             CALL add_cfm_submat(cfm_spinor_ikp, cfm_non_spinor_ikp, i_offset, j_offset, gaussi)
    2407              : 
    2408              :          END DO
    2409              :       END DO
    2410              : 
    2411           20 :       CALL cp_cfm_release(cfm_non_spinor_Gamma)
    2412           20 :       CALL cp_cfm_release(cfm_non_spinor_ikp)
    2413           20 :       CALL cp_fm_release(fm_non_spinor_Gamma_re)
    2414           20 :       CALL cp_fm_release(fm_non_spinor_Gamma_im)
    2415              : 
    2416           20 :       CALL timestop(handle)
    2417              : 
    2418           20 :    END SUBROUTINE cfm_ikp_from_cfm_spinor_Gamma
    2419              : 
    2420              : ! **************************************************************************************************
    2421              : !> \brief ...
    2422              : !> \param cfm_ikp ...
    2423              : !> \param fm_Gamma ...
    2424              : !> \param ikp ...
    2425              : !> \param qs_env ...
    2426              : !> \param kpoints ...
    2427              : !> \param basis_type ...
    2428              : ! **************************************************************************************************
    2429         3330 :    SUBROUTINE cfm_ikp_from_fm_Gamma(cfm_ikp, fm_Gamma, ikp, qs_env, kpoints, basis_type)
    2430              :       TYPE(cp_cfm_type)                                  :: cfm_ikp
    2431              :       TYPE(cp_fm_type)                                   :: fm_Gamma
    2432              :       INTEGER                                            :: ikp
    2433              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2434              :       TYPE(kpoint_type), POINTER                         :: kpoints
    2435              :       CHARACTER(LEN=*)                                   :: basis_type
    2436              : 
    2437              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'cfm_ikp_from_fm_Gamma'
    2438              : 
    2439              :       INTEGER :: col_global, handle, i_atom, i_atom_old, i_cell, i_mic_cell, i_row, j_atom, &
    2440              :          j_atom_old, j_cell, j_col, n_bf, ncol_local, nrow_local, num_cells, row_global
    2441         3330 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_from_bf
    2442         3330 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    2443         3330 :       INTEGER, DIMENSION(:, :), POINTER                  :: index_to_cell
    2444              :       LOGICAL :: i_cell_is_the_minimum_image_cell
    2445              :       REAL(KIND=dp)                                      :: abs_rab_cell_i, abs_rab_cell_j, arg
    2446              :       REAL(KIND=dp), DIMENSION(3)                        :: cell_vector, cell_vector_j, rab_cell_i, &
    2447              :                                                             rab_cell_j
    2448              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat
    2449              :       TYPE(cell_type), POINTER                           :: cell
    2450         3330 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2451              : 
    2452         3330 :       CALL timeset(routineN, handle)
    2453              : 
    2454         3330 :       IF (.NOT. ASSOCIATED(cfm_ikp%local_data)) THEN
    2455         1710 :          CALL cp_cfm_create(cfm_ikp, fm_Gamma%matrix_struct)
    2456              :       END IF
    2457         3330 :       CALL cp_cfm_set_all(cfm_ikp, z_zero)
    2458              : 
    2459              :       CALL cp_fm_get_info(matrix=fm_Gamma, &
    2460              :                           nrow_local=nrow_local, &
    2461              :                           ncol_local=ncol_local, &
    2462              :                           row_indices=row_indices, &
    2463         3330 :                           col_indices=col_indices)
    2464              : 
    2465              :       ! get number of basis functions (bf) for different basis sets
    2466         3330 :       IF (basis_type == "ORB") THEN
    2467         1790 :          n_bf = qs_env%bs_env%n_ao
    2468         1540 :       ELSE IF (basis_type == "RI_AUX") THEN
    2469         1540 :          n_bf = qs_env%bs_env%n_RI
    2470              :       ELSE
    2471            0 :          CPABORT("Only ORB and RI_AUX basis implemented.")
    2472              :       END IF
    2473              : 
    2474         9990 :       ALLOCATE (atom_from_bf(n_bf))
    2475         3330 :       CALL get_atom_index_from_basis_function_index(qs_env, atom_from_bf, n_bf, basis_type)
    2476              : 
    2477         3330 :       NULLIFY (cell, particle_set)
    2478         3330 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
    2479         3330 :       CALL get_cell(cell=cell, h=hmat)
    2480              : 
    2481         3330 :       index_to_cell => kpoints%index_to_cell
    2482              : 
    2483         3330 :       num_cells = SIZE(index_to_cell, 2)
    2484         3330 :       i_atom_old = 0
    2485         3330 :       j_atom_old = 0
    2486              : 
    2487        29568 :       DO j_col = 1, ncol_local
    2488       185327 :          DO i_row = 1, nrow_local
    2489              : 
    2490       155759 :             row_global = row_indices(i_row)
    2491       155759 :             col_global = col_indices(j_col)
    2492              : 
    2493       155759 :             i_atom = atom_from_bf(row_global)
    2494       155759 :             j_atom = atom_from_bf(col_global)
    2495              : 
    2496              :             ! we only need to check for new MIC cell for new i_atom-j_atom pair
    2497       155759 :             IF (i_atom /= i_atom_old .OR. j_atom /= j_atom_old) THEN
    2498       458792 :                DO i_cell = 1, num_cells
    2499              : 
    2500              :                   ! only check nearest neigbors
    2501      1275504 :                   IF (ANY(ABS(index_to_cell(1:3, i_cell)) > 1)) CYCLE
    2502              : 
    2503      3647744 :                   cell_vector(1:3) = MATMUL(hmat, REAL(index_to_cell(1:3, i_cell), dp))
    2504              : 
    2505              :                   rab_cell_i(1:3) = pbc(particle_set(i_atom)%r(1:3), cell) - &
    2506       911936 :                                     (pbc(particle_set(j_atom)%r(1:3), cell) + cell_vector(1:3))
    2507       227984 :                   abs_rab_cell_i = SQRT(rab_cell_i(1)**2 + rab_cell_i(2)**2 + rab_cell_i(3)**2)
    2508              : 
    2509              :                   ! minimum image convention
    2510       227984 :                   i_cell_is_the_minimum_image_cell = .TRUE.
    2511      3497896 :                   DO j_cell = 1, num_cells
    2512     52318592 :                      cell_vector_j(1:3) = MATMUL(hmat, REAL(index_to_cell(1:3, j_cell), dp))
    2513              :                      rab_cell_j(1:3) = pbc(particle_set(i_atom)%r(1:3), cell) - &
    2514     13079648 :                                        (pbc(particle_set(j_atom)%r(1:3), cell) + cell_vector_j(1:3))
    2515      3269912 :                      abs_rab_cell_j = SQRT(rab_cell_j(1)**2 + rab_cell_j(2)**2 + rab_cell_j(3)**2)
    2516              : 
    2517      3497896 :                      IF (abs_rab_cell_i > abs_rab_cell_j + 1.0E-6_dp) THEN
    2518       676826 :                         i_cell_is_the_minimum_image_cell = .FALSE.
    2519              :                      END IF
    2520              :                   END DO
    2521              : 
    2522       275120 :                   IF (i_cell_is_the_minimum_image_cell) THEN
    2523        47136 :                      i_mic_cell = i_cell
    2524              :                   END IF
    2525              : 
    2526              :                END DO ! i_cell
    2527              :             END IF
    2528              : 
    2529              :             arg = REAL(index_to_cell(1, i_mic_cell), dp)*kpoints%xkp(1, ikp) + &
    2530              :                   REAL(index_to_cell(2, i_mic_cell), dp)*kpoints%xkp(2, ikp) + &
    2531       155759 :                   REAL(index_to_cell(3, i_mic_cell), dp)*kpoints%xkp(3, ikp)
    2532              : 
    2533              :             cfm_ikp%local_data(i_row, j_col) = COS(twopi*arg)*fm_Gamma%local_data(i_row, j_col)*z_one + &
    2534       155759 :                                                SIN(twopi*arg)*fm_Gamma%local_data(i_row, j_col)*gaussi
    2535              : 
    2536       155759 :             j_atom_old = j_atom
    2537       181997 :             i_atom_old = i_atom
    2538              : 
    2539              :          END DO ! j_col
    2540              :       END DO ! i_row
    2541              : 
    2542         3330 :       CALL timestop(handle)
    2543              : 
    2544         9990 :    END SUBROUTINE cfm_ikp_from_fm_Gamma
    2545              : 
    2546              : ! **************************************************************************************************
    2547              : !> \brief ...
    2548              : !> \param bs_env ...
    2549              : !> \param qs_env ...
    2550              : !> \param fm_W_MIC_freq_j ...
    2551              : !> \param cfm_W_ikp_freq_j ...
    2552              : !> \param ikp ...
    2553              : !> \param kpoints ...
    2554              : !> \param basis_type ...
    2555              : !> \param wkp_ext ...
    2556              : ! **************************************************************************************************
    2557         1604 :    SUBROUTINE MIC_contribution_from_ikp(bs_env, qs_env, fm_W_MIC_freq_j, &
    2558              :                                         cfm_W_ikp_freq_j, ikp, kpoints, basis_type, wkp_ext)
    2559              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2560              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2561              :       TYPE(cp_fm_type)                                   :: fm_W_MIC_freq_j
    2562              :       TYPE(cp_cfm_type)                                  :: cfm_W_ikp_freq_j
    2563              :       INTEGER, INTENT(IN)                                :: ikp
    2564              :       TYPE(kpoint_type), POINTER                         :: kpoints
    2565              :       CHARACTER(LEN=*)                                   :: basis_type
    2566              :       REAL(KIND=dp), OPTIONAL                            :: wkp_ext
    2567              : 
    2568              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'MIC_contribution_from_ikp'
    2569              : 
    2570              :       INTEGER                                            :: handle, i_bf, iatom, iatom_old, irow, &
    2571              :                                                             j_bf, jatom, jatom_old, jcol, n_bf, &
    2572              :                                                             ncol_local, nrow_local, num_cells
    2573         1604 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_from_bf_index
    2574         1604 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
    2575         1604 :       INTEGER, DIMENSION(:, :), POINTER                  :: index_to_cell
    2576              :       REAL(KIND=dp)                                      :: contribution, weight_im, weight_re, &
    2577              :                                                             wkp_of_ikp
    2578              :       REAL(KIND=dp), DIMENSION(3, 3)                     :: hmat
    2579         1604 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: wkp
    2580         1604 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: xkp
    2581              :       TYPE(cell_type), POINTER                           :: cell
    2582         1604 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2583              : 
    2584         1604 :       CALL timeset(routineN, handle)
    2585              : 
    2586              :       ! get number of basis functions (bf) for different basis sets
    2587         1604 :       IF (basis_type == "ORB") THEN
    2588           32 :          n_bf = qs_env%bs_env%n_ao
    2589         1572 :       ELSE IF (basis_type == "RI_AUX") THEN
    2590         1572 :          n_bf = qs_env%bs_env%n_RI
    2591              :       ELSE
    2592            0 :          CPABORT("Only ORB and RI_AUX basis implemented.")
    2593              :       END IF
    2594              : 
    2595         4812 :       ALLOCATE (atom_from_bf_index(n_bf))
    2596         1604 :       CALL get_atom_index_from_basis_function_index(qs_env, atom_from_bf_index, n_bf, basis_type)
    2597              : 
    2598         1604 :       NULLIFY (cell, particle_set)
    2599         1604 :       CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
    2600         1604 :       CALL get_cell(cell=cell, h=hmat)
    2601              : 
    2602              :       CALL cp_cfm_get_info(matrix=cfm_W_ikp_freq_j, &
    2603              :                            nrow_local=nrow_local, &
    2604              :                            ncol_local=ncol_local, &
    2605              :                            row_indices=row_indices, &
    2606         1604 :                            col_indices=col_indices)
    2607              : 
    2608         1604 :       CALL get_kpoint_info(kpoints, xkp=xkp, wkp=wkp)
    2609         1604 :       index_to_cell => kpoints%index_to_cell
    2610         1604 :       num_cells = SIZE(index_to_cell, 2)
    2611              : 
    2612         1604 :       iatom_old = 0
    2613         1604 :       jatom_old = 0
    2614              : 
    2615        15024 :       DO jcol = 1, ncol_local
    2616        94038 :          DO irow = 1, nrow_local
    2617              : 
    2618        79014 :             i_bf = row_indices(irow)
    2619        79014 :             j_bf = col_indices(jcol)
    2620              : 
    2621        79014 :             iatom = atom_from_bf_index(i_bf)
    2622        79014 :             jatom = atom_from_bf_index(j_bf)
    2623              : 
    2624        79014 :             IF (PRESENT(wkp_ext)) THEN
    2625         3496 :                wkp_of_ikp = wkp_ext
    2626              :             ELSE
    2627        81070 :                SELECT CASE (bs_env%l_RI(i_bf) + bs_env%l_RI(j_bf))
    2628              :                CASE (0)
    2629              :                   ! both RI functions are s-functions, k-extrapolation for 2D and 3D
    2630         5552 :                   wkp_of_ikp = wkp(ikp)
    2631              :                CASE (1)
    2632              :                   ! one function is an s-function, the other a p-function, k-extrapolation for 3D
    2633        17832 :                   wkp_of_ikp = bs_env%wkp_s_p(ikp)
    2634              :                CASE DEFAULT
    2635              :                   ! for any other matrix element of W, there is no need for extrapolation
    2636        75518 :                   wkp_of_ikp = bs_env%wkp_no_extra(ikp)
    2637              :                END SELECT
    2638              :             END IF
    2639              : 
    2640        79014 :             IF (iatom /= iatom_old .OR. jatom /= jatom_old) THEN
    2641              : 
    2642              :                CALL compute_weight_re_im(weight_re, weight_im, &
    2643              :                                          num_cells, iatom, jatom, xkp(1:3, ikp), wkp_of_ikp, &
    2644        23592 :                                          cell, index_to_cell, hmat, particle_set)
    2645              : 
    2646        23592 :                iatom_old = iatom
    2647        23592 :                jatom_old = jatom
    2648              : 
    2649              :             END IF
    2650              : 
    2651              :             contribution = weight_re*REAL(cfm_W_ikp_freq_j%local_data(irow, jcol)) + &
    2652        79014 :                            weight_im*AIMAG(cfm_W_ikp_freq_j%local_data(irow, jcol))
    2653              : 
    2654              :             fm_W_MIC_freq_j%local_data(irow, jcol) = fm_W_MIC_freq_j%local_data(irow, jcol) &
    2655        92434 :                                                      + contribution
    2656              : 
    2657              :          END DO
    2658              :       END DO
    2659              : 
    2660         1604 :       CALL timestop(handle)
    2661              : 
    2662         4812 :    END SUBROUTINE MIC_contribution_from_ikp
    2663              : 
    2664              : ! **************************************************************************************************
    2665              : !> \brief ...
    2666              : !> \param xkp ...
    2667              : !> \param ikp_start ...
    2668              : !> \param ikp_end ...
    2669              : !> \param grid ...
    2670              : ! **************************************************************************************************
    2671           54 :    SUBROUTINE compute_xkp(xkp, ikp_start, ikp_end, grid)
    2672              : 
    2673              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: xkp
    2674              :       INTEGER                                            :: ikp_start, ikp_end
    2675              :       INTEGER, DIMENSION(3)                              :: grid
    2676              : 
    2677              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'compute_xkp'
    2678              : 
    2679              :       INTEGER                                            :: handle, i, ix, iy, iz
    2680              : 
    2681           54 :       CALL timeset(routineN, handle)
    2682              : 
    2683           54 :       i = ikp_start
    2684          136 :       DO ix = 1, grid(1)
    2685          362 :          DO iy = 1, grid(2)
    2686          688 :             DO iz = 1, grid(3)
    2687              : 
    2688          380 :                IF (i > ikp_end) CYCLE
    2689              : 
    2690          362 :                xkp(1, i) = REAL(2*ix - grid(1) - 1, KIND=dp)/(2._dp*REAL(grid(1), KIND=dp))
    2691          362 :                xkp(2, i) = REAL(2*iy - grid(2) - 1, KIND=dp)/(2._dp*REAL(grid(2), KIND=dp))
    2692          362 :                xkp(3, i) = REAL(2*iz - grid(3) - 1, KIND=dp)/(2._dp*REAL(grid(3), KIND=dp))
    2693          606 :                i = i + 1
    2694              : 
    2695              :             END DO
    2696              :          END DO
    2697              :       END DO
    2698              : 
    2699           54 :       CALL timestop(handle)
    2700              : 
    2701           54 :    END SUBROUTINE compute_xkp
    2702              : 
    2703              : ! **************************************************************************************************
    2704              : !> \brief ...
    2705              : !> \param kpoints ...
    2706              : !> \param qs_env ...
    2707              : ! **************************************************************************************************
    2708           68 :    SUBROUTINE kpoint_init_cell_index_simple(kpoints, qs_env)
    2709              : 
    2710              :       TYPE(kpoint_type), POINTER                         :: kpoints
    2711              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2712              : 
    2713              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'kpoint_init_cell_index_simple'
    2714              : 
    2715              :       INTEGER                                            :: handle, nimages
    2716              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2717              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    2718           34 :          POINTER                                         :: sab_orb
    2719              : 
    2720           34 :       CALL timeset(routineN, handle)
    2721              : 
    2722           34 :       NULLIFY (para_env, sab_orb)
    2723           34 :       CALL get_qs_env(qs_env=qs_env, para_env=para_env, sab_orb=sab_orb)
    2724           34 :       CALL kpoint_init_cell_index(kpoints, sab_orb, para_env, nimages)
    2725              : 
    2726           34 :       CALL timestop(handle)
    2727              : 
    2728           34 :    END SUBROUTINE kpoint_init_cell_index_simple
    2729              : 
    2730              : ! **************************************************************************************************
    2731              : !> \brief ...
    2732              : !> \param qs_env ...
    2733              : !> \param bs_env ...
    2734              : ! **************************************************************************************************
    2735           14 :    SUBROUTINE soc(qs_env, bs_env)
    2736              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2737              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2738              : 
    2739              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'soc'
    2740              : 
    2741              :       INTEGER                                            :: handle
    2742              : 
    2743           14 :       CALL timeset(routineN, handle)
    2744              : 
    2745              :       ! V^SOC_µν^(α),R = ħ/2 < ϕ_µ cell O | sum_ℓ ΔV_ℓ^SO(r,r') L^(α) | ϕ_ν cell R>, α = x,y,z
    2746              :       ! see Hartwigsen, Goedecker, Hutter, Eq.(18), (19) (doi.org/10.1103/PhysRevB.58.3641)
    2747           14 :       CALL V_SOC_xyz_from_pseudopotential(qs_env, bs_env%mat_V_SOC_xyz)
    2748              : 
    2749              :       ! Calculate H^SOC_µν,σσ'(k) = sum_α V^SOC_µν^(α)(k)*Pauli-matrix^(α)_σσ'
    2750              :       ! see Hartwigsen, Goedecker, Hutter, Eq.(18) (doi.org/10.1103/PhysRevB.58.3641)
    2751           20 :       SELECT CASE (bs_env%small_cell_full_kp_or_large_cell_Gamma)
    2752              :       CASE (large_cell_Gamma, large_cell_Gamma_ri_rs, non_periodic_ri_rs)
    2753              : 
    2754              :          ! H^SOC_µν,σσ' = sum_α V^SOC_µν^(α)*Pauli-matrix^(α)_σσ'
    2755            6 :          CALL H_KS_spinor_Gamma(bs_env)
    2756              : 
    2757              :       CASE (small_cell_full_kp)
    2758              : 
    2759              :          ! V^SOC_µν^(α),R -> V^SOC_µν^(α)(k); then calculate spinor H^SOC_µν,σσ'(k) (see above)
    2760           14 :          CALL H_KS_spinor_kp(qs_env, bs_env)
    2761              : 
    2762              :       END SELECT
    2763              : 
    2764           14 :       CALL timestop(handle)
    2765              : 
    2766           14 :    END SUBROUTINE soc
    2767              : 
    2768              : ! **************************************************************************************************
    2769              : !> \brief ...
    2770              : !> \param bs_env ...
    2771              : ! **************************************************************************************************
    2772            6 :    SUBROUTINE H_KS_spinor_Gamma(bs_env)
    2773              : 
    2774              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2775              : 
    2776              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'H_KS_spinor_Gamma'
    2777              : 
    2778              :       INTEGER                                            :: handle, nao, s
    2779              :       TYPE(cp_fm_struct_type), POINTER                   :: str
    2780              : 
    2781            6 :       CALL timeset(routineN, handle)
    2782              : 
    2783            6 :       CALL cp_fm_get_info(bs_env%fm_ks_Gamma(1), nrow_global=nao)
    2784              : 
    2785           12 :       ALLOCATE (bs_env%cfm_SOC_spinor_ao(1))
    2786            6 :       CALL create_cfm_double(bs_env%cfm_SOC_spinor_ao(1), fm_orig=bs_env%fm_ks_Gamma(1))
    2787            6 :       CALL cp_cfm_set_all(bs_env%cfm_SOC_spinor_ao(1), z_zero)
    2788              : 
    2789            6 :       str => bs_env%fm_ks_Gamma(1)%matrix_struct
    2790              : 
    2791            6 :       s = nao + 1
    2792              : 
    2793              :       ! careful: inside add_dbcsr_submat, mat_V_SOC_xyz is multiplied by i because the real matrix
    2794              :       !          mat_V_SOC_xyz is antisymmetric as V_SOC matrix is purely imaginary and Hermitian
    2795              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(1, 1)%matrix, &
    2796            6 :                             str, s, 1, z_one, .TRUE.)
    2797              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(2, 1)%matrix, &
    2798            6 :                             str, s, 1, gaussi, .TRUE.)
    2799              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(3, 1)%matrix, &
    2800            6 :                             str, 1, 1, z_one, .FALSE.)
    2801              :       CALL add_dbcsr_submat(bs_env%cfm_SOC_spinor_ao(1), bs_env%mat_V_SOC_xyz(3, 1)%matrix, &
    2802            6 :                             str, s, s, -z_one, .FALSE.)
    2803              : 
    2804            6 :       CALL timestop(handle)
    2805              : 
    2806            6 :    END SUBROUTINE H_KS_spinor_Gamma
    2807              : 
    2808              : ! **************************************************************************************************
    2809              : !> \brief ...
    2810              : !> \param qs_env ...
    2811              : !> \param bs_env ...
    2812              : ! **************************************************************************************************
    2813           16 :    SUBROUTINE H_KS_spinor_kp(qs_env, bs_env)
    2814              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2815              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2816              : 
    2817              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'H_KS_spinor_kp'
    2818              : 
    2819              :       INTEGER                                            :: handle, i_dim, ikp, n_spin, &
    2820              :                                                             nkp_bs_and_DOS, s
    2821            8 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index_scf
    2822              :       REAL(KIND=dp), DIMENSION(3)                        :: xkp
    2823              :       TYPE(cp_cfm_type)                                  :: cfm_V_SOC_xyz_ikp
    2824              :       TYPE(cp_fm_struct_type), POINTER                   :: str
    2825              :       TYPE(kpoint_type), POINTER                         :: kpoints_scf
    2826              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    2827            8 :          POINTER                                         :: sab_nl
    2828              : 
    2829            8 :       CALL timeset(routineN, handle)
    2830              : 
    2831            8 :       nkp_bs_and_DOS = bs_env%nkp_bs_and_DOS
    2832            8 :       n_spin = bs_env%n_spin
    2833            8 :       s = bs_env%n_ao + 1
    2834            8 :       str => bs_env%cfm_ks_kp(1, 1)%matrix_struct
    2835              : 
    2836            8 :       CALL cp_cfm_create(cfm_V_SOC_xyz_ikp, bs_env%cfm_work_mo%matrix_struct)
    2837              : 
    2838            8 :       CALL alloc_cfm_double_array_1d(bs_env%cfm_SOC_spinor_ao, bs_env%cfm_ks_kp(1, 1), nkp_bs_and_DOS)
    2839              : 
    2840            8 :       CALL get_qs_env(qs_env, kpoints=kpoints_scf)
    2841              : 
    2842            8 :       NULLIFY (sab_nl)
    2843            8 :       CALL get_kpoint_info(kpoints_scf, sab_nl=sab_nl, cell_to_index=cell_to_index_scf)
    2844              : 
    2845           32 :       DO i_dim = 1, 3
    2846              : 
    2847          602 :          DO ikp = 1, nkp_bs_and_DOS
    2848              : 
    2849         2280 :             xkp(1:3) = bs_env%kpoints_DOS%xkp(1:3, ikp)
    2850              : 
    2851          570 :             CALL cp_cfm_set_all(cfm_V_SOC_xyz_ikp, z_zero)
    2852              : 
    2853              :             CALL rsmat_to_kp(bs_env%mat_V_SOC_xyz, i_dim, xkp, cell_to_index_scf, &
    2854          570 :                              sab_nl, bs_env, cfm_V_SOC_xyz_ikp, imag_rs_mat=.TRUE.)
    2855              : 
    2856              :             ! multiply V_SOC with i because bs_env%mat_V_SOC_xyz stores imag. part (real part = 0)
    2857          570 :             CALL cp_cfm_scale(gaussi, cfm_V_SOC_xyz_ikp)
    2858              : 
    2859           24 :             SELECT CASE (i_dim)
    2860              :             CASE (1)
    2861              :                ! add V^SOC_x * σ_x for σ_x = ( (0,1) (1,0) )
    2862          190 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, 1, s)
    2863          190 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, s, 1)
    2864              :             CASE (2)
    2865              :                ! add V^SOC_y * σ_y for σ_y = ( (0,-i) (i,0) )
    2866          190 :                CALL cp_cfm_scale(gaussi, cfm_V_SOC_xyz_ikp)
    2867          190 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, 1, s)
    2868          190 :                CALL cp_cfm_scale(-z_one, cfm_V_SOC_xyz_ikp)
    2869          190 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, s, 1)
    2870              :             CASE (3)
    2871              :                ! add V^SOC_z * σ_z for σ_z = ( (1,0) (0,1) )
    2872          190 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, 1, 1)
    2873          190 :                CALL cp_cfm_scale(-z_one, cfm_V_SOC_xyz_ikp)
    2874          760 :                CALL add_cfm_submat(bs_env%cfm_SOC_spinor_ao(ikp), cfm_V_SOC_xyz_ikp, s, s)
    2875              :             END SELECT
    2876              : 
    2877              :          END DO
    2878              : 
    2879              :       END DO ! ikp
    2880              : 
    2881            8 :       CALL cp_cfm_release(cfm_V_SOC_xyz_ikp)
    2882              : 
    2883            8 :       CALL timestop(handle)
    2884              : 
    2885            8 :    END SUBROUTINE H_KS_spinor_kp
    2886              : 
    2887              : ! **************************************************************************************************
    2888              : !> \brief ...
    2889              : !> \param cfm_array ...
    2890              : !> \param cfm_template ...
    2891              : !> \param n ...
    2892              : ! **************************************************************************************************
    2893            8 :    SUBROUTINE alloc_cfm_double_array_1d(cfm_array, cfm_template, n)
    2894              :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:)       :: cfm_array
    2895              :       TYPE(cp_cfm_type)                                  :: cfm_template
    2896              :       INTEGER                                            :: n
    2897              : 
    2898              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'alloc_cfm_double_array_1d'
    2899              : 
    2900              :       INTEGER                                            :: handle, i
    2901              : 
    2902            8 :       CALL timeset(routineN, handle)
    2903              : 
    2904          214 :       ALLOCATE (cfm_array(n))
    2905          198 :       DO i = 1, n
    2906          190 :          CALL create_cfm_double(cfm_array(i), cfm_orig=cfm_template)
    2907          198 :          CALL cp_cfm_set_all(cfm_array(i), z_zero)
    2908              :       END DO
    2909              : 
    2910            8 :       CALL timestop(handle)
    2911              : 
    2912            8 :    END SUBROUTINE alloc_cfm_double_array_1d
    2913              : 
    2914              : ! **************************************************************************************************
    2915              : !> \brief ...
    2916              : !> \param bs_env ...
    2917              : ! **************************************************************************************************
    2918           42 :    SUBROUTINE get_all_VBM_CBM_bandgaps(bs_env)
    2919              : 
    2920              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2921              : 
    2922              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_all_VBM_CBM_bandgaps'
    2923              : 
    2924              :       INTEGER                                            :: handle
    2925              : 
    2926           42 :       CALL timeset(routineN, handle)
    2927              : 
    2928           42 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_scf, bs_env%eigenval_scf, bs_env)
    2929           42 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_G0W0, bs_env%eigenval_G0W0, bs_env)
    2930           42 :       CALL get_VBM_CBM_bandgaps(bs_env%band_edges_HF, bs_env%eigenval_HF, bs_env)
    2931              : 
    2932           42 :       CALL timestop(handle)
    2933              : 
    2934           42 :    END SUBROUTINE get_all_VBM_CBM_bandgaps
    2935              : 
    2936              : ! **************************************************************************************************
    2937              : !> \brief ...
    2938              : !> \param band_edges ...
    2939              : !> \param ev ...
    2940              : !> \param bs_env ...
    2941              : ! **************************************************************************************************
    2942          136 :    SUBROUTINE get_VBM_CBM_bandgaps(band_edges, ev, bs_env)
    2943              :       TYPE(band_edges_type)                              :: band_edges
    2944              :       REAL(KIND=dp), DIMENSION(:, :, :)                  :: ev
    2945              :       TYPE(post_scf_bandstructure_type), POINTER         :: bs_env
    2946              : 
    2947              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'get_VBM_CBM_bandgaps'
    2948              : 
    2949              :       INTEGER                                            :: handle, homo, homo_1, homo_2, ikp, &
    2950              :                                                             ispin, lumo, lumo_1, lumo_2, n_mo
    2951              :       REAL(KIND=dp)                                      :: E_DBG_at_ikp
    2952              : 
    2953          136 :       CALL timeset(routineN, handle)
    2954              : 
    2955          136 :       n_mo = bs_env%n_ao
    2956              : 
    2957          136 :       band_edges%DBG = 1000.0_dp
    2958              : 
    2959          254 :       SELECT CASE (bs_env%n_spin)
    2960              :       CASE (1)
    2961          118 :          homo = bs_env%n_occ(1)
    2962          118 :          lumo = homo + 1
    2963         4460 :          band_edges%VBM = MAXVAL(ev(1:homo, :, 1))
    2964         7446 :          band_edges%CBM = MINVAL(ev(homo + 1:n_mo, :, 1))
    2965              :       CASE (2)
    2966           18 :          homo_1 = bs_env%n_occ(1)
    2967           18 :          lumo_1 = homo_1 + 1
    2968           18 :          homo_2 = bs_env%n_occ(2)
    2969           18 :          lumo_2 = homo_2 + 1
    2970          342 :          band_edges%VBM = MAX(MAXVAL(ev(1:homo_1, :, 1)), MAXVAL(ev(1:homo_2, :, 2)))
    2971          366 :          band_edges%CBM = MIN(MINVAL(ev(homo_1 + 1:n_mo, :, 1)), MINVAL(ev(homo_2 + 1:n_mo, :, 2)))
    2972              :       CASE DEFAULT
    2973          136 :          CPABORT("Error with number of spins.")
    2974              :       END SELECT
    2975              : 
    2976          136 :       band_edges%IDBG = band_edges%CBM - band_edges%VBM
    2977              : 
    2978          290 :       DO ispin = 1, bs_env%n_spin
    2979              : 
    2980          154 :          homo = bs_env%n_occ(ispin)
    2981              : 
    2982         1240 :          DO ikp = 1, bs_env%nkp_bs_and_DOS
    2983              : 
    2984        11392 :             E_DBG_at_ikp = -MAXVAL(ev(1:homo, ikp, ispin)) + MINVAL(ev(homo + 1:n_mo, ikp, ispin))
    2985              : 
    2986         1104 :             IF (E_DBG_at_ikp < band_edges%DBG) band_edges%DBG = E_DBG_at_ikp
    2987              : 
    2988              :          END DO
    2989              : 
    2990              :       END DO
    2991              : 
    2992          136 :       CALL timestop(handle)
    2993              : 
    2994          136 :    END SUBROUTINE get_VBM_CBM_bandgaps
    2995              : 
    2996              : END MODULE post_scf_bandstructure_utils
        

Generated by: LCOV version 2.0-1