LCOV - code coverage report
Current view: top level - src - qs_scf_post_tb.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:06f838d) Lines: 94.3 % 771 727
Test Date: 2026-06-05 07:04:50 Functions: 100.0 % 9 9

            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 Does all kind of post scf calculations for DFTB
      10              : !> \par History
      11              : !>      Started as a copy from the GPW file
      12              : !>      - Revise MO information printout (10.05.2021, MK)
      13              : !> \author JHU (03.2013)
      14              : ! **************************************************************************************************
      15              : MODULE qs_scf_post_tb
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      17              :                                               get_atomic_kind
      18              :    USE cell_types,                      ONLY: cell_type,&
      19              :                                               pbc
      20              :    USE cp_array_utils,                  ONLY: cp_1d_r_p_type
      21              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      22              :    USE cp_control_types,                ONLY: dft_control_type
      23              :    USE cp_dbcsr_api,                    ONLY: dbcsr_p_type,&
      24              :                                               dbcsr_type
      25              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm
      26              :    USE cp_dbcsr_output,                 ONLY: cp_dbcsr_write_sparse_matrix
      27              :    USE cp_fm_cholesky,                  ONLY: cp_fm_cholesky_decompose,&
      28              :                                               cp_fm_cholesky_reduce,&
      29              :                                               cp_fm_cholesky_restore
      30              :    USE cp_fm_diag,                      ONLY: choose_eigv_solver
      31              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      32              :                                               cp_fm_struct_release,&
      33              :                                               cp_fm_struct_type
      34              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      35              :                                               cp_fm_get_info,&
      36              :                                               cp_fm_init_random,&
      37              :                                               cp_fm_release,&
      38              :                                               cp_fm_to_fm_submat,&
      39              :                                               cp_fm_type
      40              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      41              :                                               cp_logger_get_default_io_unit,&
      42              :                                               cp_logger_type
      43              :    USE cp_output_handling,              ONLY: cp_p_file,&
      44              :                                               cp_print_key_finished_output,&
      45              :                                               cp_print_key_should_output,&
      46              :                                               cp_print_key_unit_nr
      47              :    USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
      48              :    USE cp_result_methods,               ONLY: cp_results_erase,&
      49              :                                               put_results
      50              :    USE cp_result_types,                 ONLY: cp_result_type
      51              :    USE eeq_method,                      ONLY: eeq_print
      52              :    USE input_constants,                 ONLY: ot_precond_full_all
      53              :    USE input_section_types,             ONLY: section_get_ival,&
      54              :                                               section_get_ivals,&
      55              :                                               section_get_lval,&
      56              :                                               section_get_rval,&
      57              :                                               section_vals_get,&
      58              :                                               section_vals_get_subs_vals,&
      59              :                                               section_vals_type,&
      60              :                                               section_vals_val_get
      61              :    USE kinds,                           ONLY: default_path_length,&
      62              :                                               default_string_length,&
      63              :                                               dp
      64              :    USE machine,                         ONLY: m_flush
      65              :    USE mathconstants,                   ONLY: twopi,&
      66              :                                               z_one,&
      67              :                                               z_zero
      68              :    USE memory_utilities,                ONLY: reallocate
      69              :    USE message_passing,                 ONLY: mp_para_env_type
      70              :    USE molden_utils,                    ONLY: write_mos_molden
      71              :    USE moments_utils,                   ONLY: get_reference_point
      72              :    USE mulliken,                        ONLY: mulliken_charges
      73              :    USE particle_list_types,             ONLY: particle_list_type
      74              :    USE particle_types,                  ONLY: particle_type
      75              :    USE physcon,                         ONLY: debye
      76              :    USE population_analyses,             ONLY: lowdin_population_analysis
      77              :    USE preconditioner_types,            ONLY: preconditioner_type
      78              :    USE pw_env_methods,                  ONLY: pw_env_create,&
      79              :                                               pw_env_rebuild
      80              :    USE pw_env_types,                    ONLY: pw_env_get,&
      81              :                                               pw_env_release,&
      82              :                                               pw_env_type
      83              :    USE pw_grid_types,                   ONLY: pw_grid_type
      84              :    USE pw_methods,                      ONLY: pw_axpy,&
      85              :                                               pw_copy,&
      86              :                                               pw_derive,&
      87              :                                               pw_scale,&
      88              :                                               pw_transfer,&
      89              :                                               pw_zero
      90              :    USE pw_poisson_types,                ONLY: do_ewald_none,&
      91              :                                               greens_fn_type,&
      92              :                                               pw_green_create,&
      93              :                                               pw_green_release,&
      94              :                                               pw_poisson_analytic,&
      95              :                                               pw_poisson_parameter_type
      96              :    USE pw_pool_types,                   ONLY: pw_pool_p_type,&
      97              :                                               pw_pool_type
      98              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      99              :                                               pw_r3d_rs_type
     100              :    USE qs_collocate_density,            ONLY: calculate_rho_core,&
     101              :                                               calculate_rho_elec,&
     102              :                                               calculate_wavefunction
     103              :    USE qs_dftb_types,                   ONLY: qs_dftb_atom_type
     104              :    USE qs_dftb_utils,                   ONLY: get_dftb_atom_param
     105              :    USE qs_dos,                          ONLY: calculate_dos,&
     106              :                                               calculate_dos_kp
     107              :    USE qs_dos_utils,                    ONLY: get_dos_pdos_flags
     108              :    USE qs_elf_methods,                  ONLY: qs_elf_calc
     109              :    USE qs_energy_window,                ONLY: energy_windows
     110              :    USE qs_environment_types,            ONLY: get_qs_env,&
     111              :                                               qs_environment_type
     112              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
     113              :                                               qs_kind_type
     114              :    USE qs_ks_types,                     ONLY: get_ks_env,&
     115              :                                               qs_ks_env_type,&
     116              :                                               set_ks_env
     117              :    USE qs_mo_methods,                   ONLY: calculate_subspace_eigenvalues,&
     118              :                                               make_mo_eig
     119              :    USE qs_mo_occupation,                ONLY: set_mo_occupation
     120              :    USE qs_mo_types,                     ONLY: get_mo_set,&
     121              :                                               mo_set_type
     122              :    USE qs_ot_eigensolver,               ONLY: ot_eigensolver
     123              :    USE qs_pdos,                         ONLY: calculate_projected_dos,&
     124              :                                               calculate_projected_dos_kp
     125              :    USE qs_rho_methods,                  ONLY: qs_rho_rebuild
     126              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
     127              :                                               qs_rho_set,&
     128              :                                               qs_rho_type
     129              :    USE qs_scf_csr_write,                ONLY: write_hcore_matrix_csr,&
     130              :                                               write_ks_matrix_csr,&
     131              :                                               write_p_matrix_csr,&
     132              :                                               write_s_matrix_csr
     133              :    USE qs_scf_output,                   ONLY: qs_scf_write_mos
     134              :    USE qs_scf_types,                    ONLY: ot_method_nr,&
     135              :                                               qs_scf_env_type
     136              :    USE qs_scf_wfn_mix,                  ONLY: wfn_mix
     137              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
     138              :                                               qs_subsys_type
     139              :    USE scf_control_types,               ONLY: scf_control_type
     140              :    USE stm_images,                      ONLY: th_stm_image
     141              :    USE task_list_methods,               ONLY: generate_qs_task_list
     142              :    USE task_list_types,                 ONLY: allocate_task_list,&
     143              :                                               task_list_type
     144              :    USE xtb_qresp,                       ONLY: build_xtb_qresp
     145              :    USE xtb_types,                       ONLY: get_xtb_atom_param,&
     146              :                                               xtb_atom_type
     147              : #include "./base/base_uses.f90"
     148              : 
     149              :    IMPLICIT NONE
     150              :    PRIVATE
     151              : 
     152              :    ! Global parameters
     153              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_post_tb'
     154              :    PUBLIC :: scf_post_calculation_tb, make_lumo_tb, rebuild_pw_env
     155              : 
     156              : ! **************************************************************************************************
     157              : 
     158              : CONTAINS
     159              : 
     160              : ! **************************************************************************************************
     161              : !> \brief collects possible post - scf calculations and prints info / computes properties.
     162              : !> \param qs_env ...
     163              : !> \param tb_type ...
     164              : !> \param no_mos ...
     165              : !> \par History
     166              : !>      03.2013 copy of scf_post_gpw
     167              : !> \author JHU
     168              : !> \note
     169              : ! **************************************************************************************************
     170        11006 :    SUBROUTINE scf_post_calculation_tb(qs_env, tb_type, no_mos)
     171              : 
     172              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     173              :       CHARACTER(LEN=*)                                   :: tb_type
     174              :       LOGICAL, INTENT(IN)                                :: no_mos
     175              : 
     176              :       CHARACTER(len=*), PARAMETER :: routineN = 'scf_post_calculation_tb'
     177              : 
     178              :       CHARACTER(LEN=6)                                   :: ana
     179              :       CHARACTER(LEN=default_string_length)               :: aname
     180              :       INTEGER :: after, gfn_type, handle, homo, iat, iatom, ikind, img, ispin, iw, nat, natom, &
     181              :          nkind, nlumo_stm, nlumos, nspins, print_level, unit_nr
     182              :       LOGICAL :: do_cube, do_dos, do_kpoints, do_pdos, do_pdos_raw, do_projected_dos, explicit, &
     183              :          gfn0, has_homo, omit_headers, print_it, rebuild, vdip
     184              :       REAL(KIND=dp)                                      :: zeff
     185        11006 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: mcharge, zcharge
     186              :       REAL(KIND=dp), DIMENSION(2, 2)                     :: homo_lumo
     187        11006 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: echarge, mo_eigenvalues
     188        11006 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: charges
     189        11006 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     190              :       TYPE(cell_type), POINTER                           :: cell
     191        11006 :       TYPE(cp_1d_r_p_type), DIMENSION(:), POINTER        :: unoccupied_evals_stm
     192        11006 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: unoccupied_orbs_stm
     193              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     194              :       TYPE(cp_logger_type), POINTER                      :: logger
     195        11006 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_rmpv, mo_derivs
     196        11006 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks, matrix_p, matrix_s
     197              :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_deriv
     198              :       TYPE(dft_control_type), POINTER                    :: dft_control
     199        11006 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     200              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     201              :       TYPE(particle_list_type), POINTER                  :: particles
     202        11006 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     203              :       TYPE(qs_dftb_atom_type), POINTER                   :: dftb_kind
     204        11006 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     205              :       TYPE(qs_rho_type), POINTER                         :: rho
     206              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     207              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     208              :       TYPE(scf_control_type), POINTER                    :: scf_control
     209              :       TYPE(section_vals_type), POINTER                   :: dft_section, moments_section, print_key, &
     210              :                                                             print_section, sprint_section, &
     211              :                                                             wfn_mix_section
     212              :       TYPE(xtb_atom_type), POINTER                       :: xtb_kind
     213              : 
     214        11006 :       CALL timeset(routineN, handle)
     215              : 
     216        11006 :       logger => cp_get_default_logger()
     217              : 
     218        11006 :       gfn0 = .FALSE.
     219        11006 :       vdip = .FALSE.
     220        11006 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     221        18706 :       SELECT CASE (TRIM(tb_type))
     222              :       CASE ("DFTB")
     223              :       CASE ("xTB")
     224         7700 :          gfn_type = dft_control%qs_control%xtb_control%gfn_type
     225         7700 :          gfn0 = (gfn_type == 0)
     226         7700 :          vdip = dft_control%qs_control%xtb_control%var_dipole
     227              :       CASE DEFAULT
     228        11006 :          CPABORT("unknown TB type")
     229              :       END SELECT
     230              : 
     231        11006 :       CPASSERT(ASSOCIATED(qs_env))
     232        11006 :       NULLIFY (rho, para_env, matrix_s, matrix_p)
     233              :       CALL get_qs_env(qs_env, scf_env=scf_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, &
     234              :                       rho=rho, natom=natom, para_env=para_env, &
     235        11006 :                       particle_set=particle_set, do_kpoints=do_kpoints, matrix_s_kp=matrix_s)
     236        11006 :       nspins = dft_control%nspins
     237        11006 :       CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
     238              :       ! Mulliken charges
     239        66036 :       ALLOCATE (charges(natom, nspins), mcharge(natom))
     240              :       !
     241        11006 :       CALL mulliken_charges(matrix_p, matrix_s, para_env, charges)
     242              :       !
     243        33018 :       ALLOCATE (zcharge(natom))
     244        11006 :       nkind = SIZE(atomic_kind_set)
     245        34676 :       DO ikind = 1, nkind
     246        23670 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=nat)
     247        30604 :          SELECT CASE (TRIM(tb_type))
     248              :          CASE ("DFTB")
     249         6934 :             CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
     250        23670 :             CALL get_dftb_atom_param(dftb_kind, zeff=zeff)
     251              :          CASE ("xTB")
     252        16736 :             CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     253        16736 :             CALL get_xtb_atom_param(xtb_kind, zeff=zeff)
     254              :          CASE DEFAULT
     255        47340 :             CPABORT("unknown TB type")
     256              :          END SELECT
     257       137838 :          DO iatom = 1, nat
     258        79492 :             iat = atomic_kind_set(ikind)%atom_list(iatom)
     259       162680 :             mcharge(iat) = zeff - SUM(charges(iat, 1:nspins))
     260       103162 :             zcharge(iat) = zeff
     261              :          END DO
     262              :       END DO
     263              : 
     264        11006 :       dft_section => section_vals_get_subs_vals(qs_env%input, "DFT")
     265        11006 :       print_section => section_vals_get_subs_vals(dft_section, "PRINT")
     266              : 
     267              :       ! Mulliken
     268        11006 :       print_key => section_vals_get_subs_vals(print_section, "MULLIKEN")
     269        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     270              :          unit_nr = cp_print_key_unit_nr(logger, print_section, "MULLIKEN", &
     271         3632 :                                         extension=".mulliken", log_filename=.FALSE.)
     272         3632 :          IF (unit_nr > 0) THEN
     273         1827 :             WRITE (UNIT=unit_nr, FMT="(/,/,T2,A)") "MULLIKEN POPULATION ANALYSIS"
     274         1827 :             IF (nspins == 1) THEN
     275              :                WRITE (UNIT=unit_nr, FMT="(/,T2,A,T70,A)") &
     276         1614 :                   " # Atom   Element   Kind        Atomic population", " Net charge"
     277         4315 :                DO ikind = 1, nkind
     278         2701 :                   CALL get_atomic_kind(atomic_kind_set(ikind), natom=nat)
     279         2701 :                   aname = ""
     280          567 :                   SELECT CASE (tb_type)
     281              :                   CASE ("DFTB")
     282          567 :                      CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
     283          567 :                      CALL get_dftb_atom_param(dftb_kind, name=aname)
     284              :                   CASE ("xTB")
     285         2134 :                      CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     286         2134 :                      CALL get_xtb_atom_param(xtb_kind, symbol=aname)
     287              :                   CASE DEFAULT
     288         2701 :                      CPABORT("unknown TB type")
     289              :                   END SELECT
     290         2701 :                   ana = ADJUSTR(TRIM(ADJUSTL(aname)))
     291        14913 :                   DO iatom = 1, nat
     292         7897 :                      iat = atomic_kind_set(ikind)%atom_list(iatom)
     293              :                      WRITE (UNIT=unit_nr, &
     294              :                             FMT="(T2,I7,5X,A6,I6,T39,F12.6,T69,F12.6)") &
     295        10598 :                         iat, ADJUSTL(ana), ikind, charges(iat, 1), mcharge(iat)
     296              :                   END DO
     297              :                END DO
     298              :                WRITE (UNIT=unit_nr, &
     299              :                       FMT="(T2,A,T39,F12.6,T69,F12.6,/)") &
     300        17408 :                   "# Total charge", SUM(charges(:, 1)), SUM(mcharge(:))
     301              :             ELSE
     302              :                WRITE (UNIT=unit_nr, FMT="(/,T2,A)") &
     303          213 :                   "# Atom  Element  Kind  Atomic population (alpha,beta)   Net charge  Spin moment"
     304          611 :                DO ikind = 1, nkind
     305          398 :                   CALL get_atomic_kind(atomic_kind_set(ikind), natom=nat)
     306          398 :                   aname = ""
     307            3 :                   SELECT CASE (tb_type)
     308              :                   CASE ("DFTB")
     309            3 :                      CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
     310            3 :                      CALL get_dftb_atom_param(dftb_kind, name=aname)
     311              :                   CASE ("xTB")
     312          395 :                      CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
     313          395 :                      CALL get_xtb_atom_param(xtb_kind, symbol=aname)
     314              :                   CASE DEFAULT
     315          398 :                      CPABORT("unknown TB type")
     316              :                   END SELECT
     317          398 :                   ana = ADJUSTR(TRIM(ADJUSTL(aname)))
     318         1626 :                   DO iatom = 1, nat
     319          617 :                      iat = atomic_kind_set(ikind)%atom_list(iatom)
     320              :                      WRITE (UNIT=unit_nr, &
     321              :                             FMT="(T2,I6,3X,A6,I6,T29,4(1X,F12.6))") &
     322         1851 :                         iat, ADJUSTL(ana), ikind, charges(iat, 1:2), mcharge(iat), &
     323         1632 :                         charges(iat, 1) - charges(iat, 2)
     324              :                   END DO
     325              :                END DO
     326              :                WRITE (UNIT=unit_nr, &
     327              :                       FMT="(T2,A,T29,4(1X,F12.6),/)") &
     328         2064 :                   "# Total charge and spin", SUM(charges(:, 1)), SUM(charges(:, 2)), SUM(mcharge(:))
     329              :             END IF
     330         1827 :             CALL m_flush(unit_nr)
     331              :          END IF
     332         3632 :          CALL cp_print_key_finished_output(unit_nr, logger, print_key)
     333              :       END IF
     334              : 
     335              :       ! Compute the Lowdin charges
     336        11006 :       print_key => section_vals_get_subs_vals(print_section, "LOWDIN")
     337        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     338           48 :          SELECT CASE (tb_type)
     339              :          CASE ("DFTB")
     340           48 :             CPWARN("Lowdin population analysis not implemented for DFTB method.")
     341              :          CASE ("xTB")
     342              :             unit_nr = cp_print_key_unit_nr(logger, print_section, "LOWDIN", extension=".lowdin", &
     343           26 :                                            log_filename=.FALSE.)
     344           26 :             print_level = 1
     345           26 :             CALL section_vals_val_get(print_key, "PRINT_GOP", l_val=print_it)
     346           26 :             IF (print_it) print_level = 2
     347           26 :             CALL section_vals_val_get(print_key, "PRINT_ALL", l_val=print_it)
     348           26 :             IF (print_it) print_level = 3
     349           26 :             IF (do_kpoints) THEN
     350            2 :                CPWARN("Lowdin charges not implemented for k-point calculations!")
     351              :             ELSE
     352           24 :                CALL lowdin_population_analysis(qs_env, unit_nr, print_level)
     353              :             END IF
     354           26 :             CALL cp_print_key_finished_output(unit_nr, logger, print_section, "LOWDIN")
     355              :          CASE DEFAULT
     356          126 :             CPABORT("unknown TB type")
     357              :          END SELECT
     358              :       END IF
     359              : 
     360              :       ! EEQ Charges
     361        11006 :       print_key => section_vals_get_subs_vals(print_section, "EEQ_CHARGES")
     362        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     363              :          unit_nr = cp_print_key_unit_nr(logger, print_section, "EEQ_CHARGES", &
     364            4 :                                         extension=".eeq", log_filename=.FALSE.)
     365            4 :          CALL eeq_print(qs_env, unit_nr, print_level, ext=gfn0)
     366            4 :          CALL cp_print_key_finished_output(unit_nr, logger, print_key)
     367              :       END IF
     368              : 
     369              :       ! Hirshfeld
     370        11006 :       print_key => section_vals_get_subs_vals(print_section, "HIRSHFELD")
     371        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     372        11006 :       IF (explicit) THEN
     373            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     374            0 :             CPWARN("Hirshfeld charges not available for TB methods.")
     375              :          END IF
     376              :       END IF
     377              : 
     378              :       ! MAO
     379        11006 :       print_key => section_vals_get_subs_vals(print_section, "MAO_ANALYSIS")
     380        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     381        11006 :       IF (explicit) THEN
     382            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     383            0 :             CPWARN("MAO analysis not available for TB methods.")
     384              :          END IF
     385              :       END IF
     386              : 
     387              :       ! ED
     388        11006 :       print_key => section_vals_get_subs_vals(print_section, "ENERGY_DECOMPOSITION_ANALYSIS")
     389        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     390        11006 :       IF (explicit) THEN
     391            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     392            0 :             CPWARN("ED analysis not available for TB methods.")
     393              :          END IF
     394              :       END IF
     395              : 
     396              :       ! Dipole Moments
     397        11006 :       print_key => section_vals_get_subs_vals(print_section, "MOMENTS")
     398        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     399              :          unit_nr = cp_print_key_unit_nr(logger, print_section, "MOMENTS", &
     400          996 :                                         extension=".data", middle_name="tb_dipole", log_filename=.FALSE.)
     401          996 :          moments_section => section_vals_get_subs_vals(print_section, "MOMENTS")
     402          996 :          IF (gfn0) THEN
     403          158 :             NULLIFY (echarge)
     404          158 :             CALL get_qs_env(qs_env, eeq=echarge)
     405          158 :             CPASSERT(ASSOCIATED(echarge))
     406          158 :             IF (vdip) THEN
     407           58 :                CALL build_xtb_qresp(qs_env, mcharge)
     408          290 :                mcharge(1:natom) = echarge(1:natom) - mcharge(1:natom)
     409              :             END IF
     410          158 :             CALL tb_dipole(qs_env, moments_section, unit_nr, mcharge)
     411              :          ELSE
     412          838 :             CALL tb_dipole(qs_env, moments_section, unit_nr, mcharge)
     413              :          END IF
     414          996 :          CALL cp_print_key_finished_output(unit_nr, logger, print_key)
     415              :       END IF
     416              : 
     417        11006 :       DEALLOCATE (charges, mcharge)
     418              : 
     419              :       ! MO
     420        11006 :       IF (.NOT. no_mos) THEN
     421        10858 :          print_key => section_vals_get_subs_vals(print_section, "MO")
     422        10858 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     423          152 :             CALL qs_scf_write_mos(qs_env, scf_env, final_mos=.TRUE.)
     424          152 :             IF (.NOT. do_kpoints) THEN
     425          100 :                SELECT CASE (tb_type)
     426              :                CASE ("DFTB")
     427              :                CASE ("xTB")
     428          100 :                   sprint_section => section_vals_get_subs_vals(dft_section, "PRINT%MO_MOLDEN")
     429          100 :                   CALL get_qs_env(qs_env, mos=mos, cell=cell)
     430              :                   CALL write_mos_molden(mos, qs_kind_set, particle_set, sprint_section, cell=cell, &
     431          100 :                                         qs_env=qs_env, calc_energies=.TRUE.)
     432              :                CASE DEFAULT
     433          142 :                   CPABORT("Unknown TB type")
     434              :                END SELECT
     435              :             END IF
     436              :          END IF
     437              :       END IF
     438              : 
     439              :       ! Wavefunction mixing
     440        11006 :       IF (.NOT. no_mos) THEN
     441        10858 :          wfn_mix_section => section_vals_get_subs_vals(dft_section, "PRINT%WFN_MIX")
     442        10858 :          CALL section_vals_get(wfn_mix_section, explicit=explicit)
     443        10858 :          IF (explicit .AND. .NOT. qs_env%run_rtp) CALL wfn_mix_tb(qs_env, dft_section, scf_env)
     444              :       END IF
     445              : 
     446        11006 :       IF (.NOT. no_mos) THEN
     447        10858 :          print_key => section_vals_get_subs_vals(print_section, "DOS")
     448        10858 :          do_dos = BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     449        10858 :          CALL get_dos_pdos_flags(print_key, do_dos, do_projected_dos, do_pdos, do_pdos_raw)
     450        10858 :          IF (do_dos) THEN
     451           22 :             IF (do_kpoints) THEN
     452           16 :                CALL calculate_dos_kp(qs_env, dft_section)
     453              :             ELSE
     454            6 :                CALL get_qs_env(qs_env, mos=mos)
     455            6 :                CALL calculate_dos(mos, dft_section, smearing_enabled=dft_control%smear)
     456              :             END IF
     457              :          END IF
     458              : 
     459              :          ! Projected density-of-states outputs
     460        10858 :          IF (do_projected_dos) THEN
     461           18 :             IF (do_kpoints) THEN
     462              :                CALL calculate_projected_dos_kp(qs_env, dft_section, pdos_print_key="PRINT%DOS", &
     463           14 :                                                write_pdos=do_pdos, write_pdos_raw=do_pdos_raw)
     464              :             ELSE
     465            4 :                CALL get_qs_env(qs_env, mos=mos, matrix_ks=ks_rmpv)
     466            8 :                DO ispin = 1, dft_control%nspins
     467            4 :                   IF (scf_env%method == ot_method_nr) THEN
     468              :                      CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
     469            0 :                                      eigenvalues=mo_eigenvalues)
     470            0 :                      IF (ASSOCIATED(qs_env%mo_derivs)) THEN
     471            0 :                         mo_coeff_deriv => qs_env%mo_derivs(ispin)%matrix
     472              :                      ELSE
     473            0 :                         mo_coeff_deriv => NULL()
     474              :                      END IF
     475              :                      CALL calculate_subspace_eigenvalues(mo_coeff, ks_rmpv(ispin)%matrix, mo_eigenvalues, &
     476              :                                                          do_rotation=.TRUE., &
     477            0 :                                                          co_rotate_dbcsr=mo_coeff_deriv)
     478            0 :                      CALL set_mo_occupation(mo_set=mos(ispin))
     479              :                   END IF
     480            8 :                   IF (dft_control%nspins == 2) THEN
     481              :                      CALL calculate_projected_dos(mos(ispin), atomic_kind_set, &
     482              :                                                   qs_kind_set, particle_set, qs_env, dft_section, ispin=ispin, &
     483            0 :                                                   pdos_print_key="PRINT%DOS", write_pdos=do_pdos, write_pdos_raw=do_pdos_raw)
     484              :                   ELSE
     485              :                      CALL calculate_projected_dos(mos(ispin), atomic_kind_set, &
     486              :                                                   qs_kind_set, particle_set, qs_env, dft_section, &
     487            4 :                                                   pdos_print_key="PRINT%DOS", write_pdos=do_pdos, write_pdos_raw=do_pdos_raw)
     488              :                   END IF
     489              :                END DO
     490              :             END IF
     491              :          END IF
     492              :       END IF
     493              : 
     494              :       ! can we do CUBE files?
     495              :       SELECT CASE (tb_type)
     496              :       CASE ("DFTB")
     497              :          do_cube = .FALSE.
     498         7700 :          rebuild = .FALSE.
     499              :       CASE ("xTB")
     500         7700 :          do_cube = .TRUE.
     501         7700 :          rebuild = .TRUE.
     502              :       CASE DEFAULT
     503        11006 :          CPABORT("unknown TB type")
     504              :       END SELECT
     505              : 
     506              :       ! Energy Windows for LS code
     507        11006 :       print_key => section_vals_get_subs_vals(print_section, "ENERGY_WINDOWS")
     508        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     509           74 :          IF (do_cube) THEN
     510           26 :             IF (do_kpoints) THEN
     511            2 :                CPWARN("Energy Windows not implemented for k-points.")
     512              :             ELSE
     513              :                IF (rebuild) THEN
     514           24 :                   CALL rebuild_pw_env(qs_env)
     515              :                   rebuild = .FALSE.
     516              :                END IF
     517           24 :                CALL energy_windows(qs_env)
     518              :             END IF
     519              :          ELSE
     520           48 :             CPWARN("Energy Windows not implemented for TB methods.")
     521              :          END IF
     522              :       END IF
     523              : 
     524              :       ! DENSITY CUBE FILE
     525        11006 :       print_key => section_vals_get_subs_vals(print_section, "E_DENSITY_CUBE")
     526        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     527           72 :          IF (do_cube) THEN
     528           24 :             IF (rebuild) THEN
     529            2 :                CALL rebuild_pw_env(qs_env)
     530            2 :                rebuild = .FALSE.
     531              :             END IF
     532           24 :             CALL print_e_density(qs_env, zcharge, print_key)
     533              :          ELSE
     534           48 :             CPWARN("Electronic density cube file not implemented for TB methods.")
     535              :          END IF
     536              :       END IF
     537              : 
     538              :       ! TOTAL DENSITY CUBE FILE
     539        11006 :       print_key => section_vals_get_subs_vals(print_section, "TOT_DENSITY_CUBE")
     540        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     541           74 :          IF (do_cube) THEN
     542           26 :             IF (rebuild) THEN
     543            2 :                CALL rebuild_pw_env(qs_env)
     544            2 :                rebuild = .FALSE.
     545              :             END IF
     546           26 :             CALL print_density_cubes(qs_env, zcharge, print_key, total_density=.TRUE.)
     547              :          ELSE
     548           48 :             CPWARN("Total density cube file not implemented for TB methods.")
     549              :          END IF
     550              :       END IF
     551              : 
     552              :       ! V_Hartree CUBE FILE
     553        11006 :       print_key => section_vals_get_subs_vals(print_section, "V_HARTREE_CUBE")
     554        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     555           72 :          IF (do_cube) THEN
     556           24 :             IF (rebuild) THEN
     557            0 :                CALL rebuild_pw_env(qs_env)
     558            0 :                rebuild = .FALSE.
     559              :             END IF
     560           24 :             CALL print_density_cubes(qs_env, zcharge, print_key, v_hartree=.TRUE.)
     561              :          ELSE
     562           48 :             CPWARN("Hartree potential cube file not implemented for TB methods.")
     563              :          END IF
     564              :       END IF
     565              : 
     566              :       ! EFIELD CUBE FILE
     567        11006 :       print_key => section_vals_get_subs_vals(print_section, "EFIELD_CUBE")
     568        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     569           72 :          IF (do_cube) THEN
     570           24 :             IF (rebuild) THEN
     571            0 :                CALL rebuild_pw_env(qs_env)
     572            0 :                rebuild = .FALSE.
     573              :             END IF
     574           24 :             CALL print_density_cubes(qs_env, zcharge, print_key, efield=.TRUE.)
     575              :          ELSE
     576           48 :             CPWARN("Efield cube file not implemented for TB methods.")
     577              :          END IF
     578              :       END IF
     579              : 
     580              :       ! ELF
     581        11006 :       print_key => section_vals_get_subs_vals(print_section, "ELF_CUBE")
     582        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     583           72 :          IF (do_cube) THEN
     584           24 :             IF (rebuild) THEN
     585            0 :                CALL rebuild_pw_env(qs_env)
     586            0 :                rebuild = .FALSE.
     587              :             END IF
     588           24 :             CALL print_elf(qs_env, zcharge, print_key)
     589              :          ELSE
     590           48 :             CPWARN("ELF not implemented for TB methods.")
     591              :          END IF
     592              :       END IF
     593              : 
     594              :       ! MO CUBES
     595        11006 :       IF (.NOT. no_mos) THEN
     596        10858 :          print_key => section_vals_get_subs_vals(print_section, "MO_CUBES")
     597        10858 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     598           72 :             IF (do_cube) THEN
     599           24 :                IF (rebuild) THEN
     600            2 :                   CALL rebuild_pw_env(qs_env)
     601            2 :                   rebuild = .FALSE.
     602              :                END IF
     603           24 :                CALL print_mo_cubes(qs_env, zcharge, print_key)
     604              :             ELSE
     605           48 :                CPWARN("Printing of MO cube files not implemented for TB methods.")
     606              :             END IF
     607              :          END IF
     608              :       END IF
     609              : 
     610              :       ! STM
     611        11006 :       IF (.NOT. no_mos) THEN
     612        10858 :          print_key => section_vals_get_subs_vals(print_section, "STM")
     613        10858 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     614            4 :             IF (do_cube) THEN
     615            4 :                IF (rebuild) THEN
     616            2 :                   CALL rebuild_pw_env(qs_env)
     617            2 :                   rebuild = .FALSE.
     618              :                END IF
     619            4 :                IF (do_kpoints) THEN
     620            0 :                   CPWARN("STM not implemented for k-point calculations!")
     621              :                ELSE
     622            4 :                   nlumo_stm = section_get_ival(print_key, "NLUMO")
     623            4 :                   CPASSERT(.NOT. dft_control%restricted)
     624              :                   CALL get_qs_env(qs_env, mos=mos, mo_derivs=mo_derivs, &
     625            4 :                                   scf_control=scf_control, matrix_ks=ks_rmpv)
     626            4 :                   CALL make_mo_eig(mos, dft_control%nspins, ks_rmpv, scf_control, mo_derivs)
     627            8 :                   DO ispin = 1, dft_control%nspins
     628            4 :                      CALL get_mo_set(mo_set=mos(ispin), eigenvalues=mo_eigenvalues, homo=homo)
     629            8 :                      homo_lumo(ispin, 1) = mo_eigenvalues(homo)
     630              :                   END DO
     631            4 :                   has_homo = .TRUE.
     632            4 :                   NULLIFY (unoccupied_orbs_stm, unoccupied_evals_stm)
     633            4 :                   IF (nlumo_stm > 0) THEN
     634            8 :                      ALLOCATE (unoccupied_orbs_stm(dft_control%nspins))
     635            8 :                      ALLOCATE (unoccupied_evals_stm(dft_control%nspins))
     636              :                      CALL make_lumo_tb(qs_env, scf_env, unoccupied_orbs_stm, unoccupied_evals_stm, &
     637            2 :                                        nlumo_stm, nlumos)
     638              :                   END IF
     639              : 
     640            4 :                   CALL get_qs_env(qs_env, subsys=subsys)
     641            4 :                   CALL qs_subsys_get(subsys, particles=particles)
     642              :                   CALL th_stm_image(qs_env, print_key, particles, unoccupied_orbs_stm, &
     643            4 :                                     unoccupied_evals_stm)
     644              : 
     645            4 :                   IF (nlumo_stm > 0) THEN
     646            4 :                      DO ispin = 1, dft_control%nspins
     647            4 :                         DEALLOCATE (unoccupied_evals_stm(ispin)%array)
     648              :                      END DO
     649            2 :                      DEALLOCATE (unoccupied_evals_stm)
     650            2 :                      CALL cp_fm_release(unoccupied_orbs_stm)
     651              :                   END IF
     652              :                END IF
     653              :             END IF
     654              :          END IF
     655              :       END IF
     656              : 
     657              :       ! Write the density matrix
     658        11006 :       CALL get_qs_env(qs_env, matrix_ks_kp=matrix_ks)
     659        11006 :       CALL section_vals_val_get(print_section, "AO_MATRICES%OMIT_HEADERS", l_val=omit_headers)
     660        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_section, &
     661              :                                            "AO_MATRICES/DENSITY"), cp_p_file)) THEN
     662              :          iw = cp_print_key_unit_nr(logger, print_section, "AO_MATRICES/DENSITY", &
     663           50 :                                    extension=".Log")
     664           50 :          CALL section_vals_val_get(print_section, "AO_MATRICES%NDIGITS", i_val=after)
     665           50 :          after = MIN(MAX(after, 1), 16)
     666          100 :          DO ispin = 1, dft_control%nspins
     667          150 :             DO img = 1, SIZE(matrix_p, 2)
     668              :                CALL cp_dbcsr_write_sparse_matrix(matrix_p(ispin, img)%matrix, 4, after, qs_env, &
     669          100 :                                                  para_env, output_unit=iw, omit_headers=omit_headers)
     670              :             END DO
     671              :          END DO
     672           50 :          CALL cp_print_key_finished_output(iw, logger, print_section, "AO_MATRICES/DENSITY")
     673              :       END IF
     674              : 
     675              :       ! The xTB matrix itself
     676        11006 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_section, &
     677              :                                            "AO_MATRICES/KOHN_SHAM_MATRIX"), cp_p_file)) THEN
     678              :          iw = cp_print_key_unit_nr(logger, print_section, "AO_MATRICES/KOHN_SHAM_MATRIX", &
     679           50 :                                    extension=".Log")
     680           50 :          CALL section_vals_val_get(print_section, "AO_MATRICES%NDIGITS", i_val=after)
     681           50 :          after = MIN(MAX(after, 1), 16)
     682          100 :          DO ispin = 1, dft_control%nspins
     683          150 :             DO img = 1, SIZE(matrix_ks, 2)
     684              :                CALL cp_dbcsr_write_sparse_matrix(matrix_ks(ispin, img)%matrix, 4, after, qs_env, para_env, &
     685          100 :                                                  output_unit=iw, omit_headers=omit_headers)
     686              :             END DO
     687              :          END DO
     688           50 :          CALL cp_print_key_finished_output(iw, logger, print_section, "AO_MATRICES/KOHN_SHAM_MATRIX")
     689              :       END IF
     690              : 
     691              :       ! these print keys are not supported in TB
     692              : 
     693              :       ! V_XC CUBE FILE
     694        11006 :       print_key => section_vals_get_subs_vals(print_section, "V_XC_CUBE")
     695        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     696        11006 :       IF (explicit) THEN
     697            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     698            0 :             CPWARN("XC potential cube file not available for TB methods.")
     699              :          END IF
     700              :       END IF
     701              : 
     702              :       ! Electric field gradients
     703        11006 :       print_key => section_vals_get_subs_vals(print_section, "ELECTRIC_FIELD_GRADIENT")
     704        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     705        11006 :       IF (explicit) THEN
     706            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     707            0 :             CPWARN("Electric field gradient not implemented for TB methods.")
     708              :          END IF
     709              :       END IF
     710              : 
     711              :       ! KINETIC ENERGY
     712        11006 :       print_key => section_vals_get_subs_vals(print_section, "KINETIC_ENERGY")
     713        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     714        11006 :       IF (explicit) THEN
     715            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     716            0 :             CPWARN("Kinetic energy not available for TB methods.")
     717              :          END IF
     718              :       END IF
     719              : 
     720              :       ! Xray diffraction spectrum
     721        11006 :       print_key => section_vals_get_subs_vals(print_section, "XRAY_DIFFRACTION_SPECTRUM")
     722        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     723        11006 :       IF (explicit) THEN
     724            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     725            0 :             CPWARN("Xray diffraction spectrum not implemented for TB methods.")
     726              :          END IF
     727              :       END IF
     728              : 
     729              :       ! EPR Hyperfine Coupling
     730        11006 :       print_key => section_vals_get_subs_vals(print_section, "HYPERFINE_COUPLING_TENSOR")
     731        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     732        11006 :       IF (explicit) THEN
     733            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     734            0 :             CPWARN("Hyperfine Coupling not implemented for TB methods.")
     735              :          END IF
     736              :       END IF
     737              : 
     738              :       ! PLUS_U
     739        11006 :       print_key => section_vals_get_subs_vals(print_section, "PLUS_U")
     740        11006 :       CALL section_vals_get(print_key, explicit=explicit)
     741        11006 :       IF (explicit) THEN
     742            0 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     743            0 :             CPWARN("DFT+U method not implemented for TB methods.")
     744              :          END IF
     745              :       END IF
     746              : 
     747        11006 :       CALL write_ks_matrix_csr(qs_env, qs_env%input)
     748        11006 :       CALL write_s_matrix_csr(qs_env, qs_env%input)
     749        11006 :       CALL write_hcore_matrix_csr(qs_env, qs_env%input)
     750        11006 :       CALL write_p_matrix_csr(qs_env, qs_env%input)
     751              : 
     752        11006 :       DEALLOCATE (zcharge)
     753              : 
     754        11006 :       CALL timestop(handle)
     755              : 
     756       132072 :    END SUBROUTINE scf_post_calculation_tb
     757              : 
     758              : ! **************************************************************************************************
     759              : !> \brief ...
     760              : !> \param qs_env ...
     761              : !> \param input ...
     762              : !> \param unit_nr ...
     763              : !> \param charges ...
     764              : ! **************************************************************************************************
     765          996 :    SUBROUTINE tb_dipole(qs_env, input, unit_nr, charges)
     766              : 
     767              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     768              :       TYPE(section_vals_type), POINTER                   :: input
     769              :       INTEGER, INTENT(in)                                :: unit_nr
     770              :       REAL(KIND=dp), DIMENSION(:), INTENT(in)            :: charges
     771              : 
     772              :       CHARACTER(LEN=default_string_length)               :: description, dipole_type
     773              :       COMPLEX(KIND=dp)                                   :: dzeta, dzphase(3), zeta, zphase(3)
     774              :       COMPLEX(KIND=dp), DIMENSION(3)                     :: dggamma, ggamma
     775              :       INTEGER                                            :: i, iat, ikind, j, nat, reference
     776              :       LOGICAL                                            :: do_berry
     777              :       REAL(KIND=dp) :: charge_tot, ci(3), dci(3), dipole(3), dipole_deriv(3), drcc(3), dria(3), &
     778              :          dtheta, gvec(3), q, rcc(3), ria(3), theta, tmp(3), via(3)
     779          996 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: ref_point
     780          996 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     781              :       TYPE(cell_type), POINTER                           :: cell
     782              :       TYPE(cp_result_type), POINTER                      :: results
     783          996 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     784              : 
     785          996 :       NULLIFY (atomic_kind_set, cell, results)
     786              :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, &
     787          996 :                       particle_set=particle_set, cell=cell, results=results)
     788              : 
     789              :       ! Reference point
     790          996 :       reference = section_get_ival(input, keyword_name="REFERENCE")
     791          996 :       NULLIFY (ref_point)
     792          996 :       description = '[DIPOLE]'
     793          996 :       CALL section_vals_val_get(input, "REF_POINT", r_vals=ref_point)
     794          996 :       CALL section_vals_val_get(input, "PERIODIC", l_val=do_berry)
     795              : 
     796          996 :       CALL get_reference_point(rcc, drcc, qs_env=qs_env, reference=reference, ref_point=ref_point)
     797              : 
     798              :       ! Dipole deriv will be the derivative of the Dipole(dM/dt=\sum e_j v_j)
     799          996 :       dipole_deriv = 0.0_dp
     800          996 :       dipole = 0.0_dp
     801          996 :       IF (do_berry) THEN
     802          626 :          dipole_type = "periodic (Berry phase)"
     803         2504 :          rcc = pbc(rcc, cell)
     804          626 :          charge_tot = 0._dp
     805         4004 :          charge_tot = SUM(charges)
     806        10016 :          ria = twopi*MATMUL(cell%h_inv, rcc)
     807         2504 :          zphase = CMPLX(COS(ria), SIN(ria), dp)**charge_tot
     808              : 
     809        10016 :          dria = twopi*MATMUL(cell%h_inv, drcc)
     810         2504 :          dzphase = charge_tot*CMPLX(-SIN(ria), COS(ria), dp)**(charge_tot - 1.0_dp)*dria
     811              : 
     812         2504 :          ggamma = z_one
     813          626 :          dggamma = z_zero
     814         2096 :          DO ikind = 1, SIZE(atomic_kind_set)
     815         1470 :             CALL get_atomic_kind(atomic_kind_set(ikind), natom=nat)
     816         5474 :             DO i = 1, nat
     817         3378 :                iat = atomic_kind_set(ikind)%atom_list(i)
     818        13512 :                ria = particle_set(iat)%r(:)
     819        13512 :                ria = pbc(ria, cell)
     820        13512 :                via = particle_set(iat)%v(:)
     821         3378 :                q = charges(iat)
     822        14982 :                DO j = 1, 3
     823        40536 :                   gvec = twopi*cell%h_inv(j, :)
     824        40536 :                   theta = SUM(ria(:)*gvec(:))
     825        40536 :                   dtheta = SUM(via(:)*gvec(:))
     826        10134 :                   zeta = CMPLX(COS(theta), SIN(theta), KIND=dp)**(-q)
     827        10134 :                   dzeta = -q*CMPLX(-SIN(theta), COS(theta), KIND=dp)**(-q - 1.0_dp)*dtheta
     828        10134 :                   dggamma(j) = dggamma(j)*zeta + ggamma(j)*dzeta
     829        13512 :                   ggamma(j) = ggamma(j)*zeta
     830              :                END DO
     831              :             END DO
     832              :          END DO
     833         2504 :          dggamma = dggamma*zphase + ggamma*dzphase
     834         2504 :          ggamma = ggamma*zphase
     835         2504 :          IF (ALL(REAL(ggamma, KIND=dp) /= 0.0_dp)) THEN
     836         2504 :             tmp = AIMAG(ggamma)/REAL(ggamma, KIND=dp)
     837         2504 :             ci = -ATAN(tmp)
     838              :             dci = -(1.0_dp/(1.0_dp + tmp**2))* &
     839         2504 :                   (AIMAG(dggamma)*REAL(ggamma, KIND=dp) - AIMAG(ggamma)*REAL(dggamma, KIND=dp))/(REAL(ggamma, KIND=dp))**2
     840        10016 :             dipole = MATMUL(cell%hmat, ci)/twopi
     841        10016 :             dipole_deriv = MATMUL(cell%hmat, dci)/twopi
     842              :          END IF
     843              :       ELSE
     844          370 :          dipole_type = "non-periodic"
     845         1724 :          DO i = 1, SIZE(particle_set)
     846              :             ! no pbc(particle_set(i)%r(:),cell) so that the total dipole is the sum of the molecular dipoles
     847         5416 :             ria = particle_set(i)%r(:)
     848         1354 :             q = charges(i)
     849         5416 :             dipole = dipole + q*(ria - rcc)
     850         5786 :             dipole_deriv(:) = dipole_deriv(:) + q*(particle_set(i)%v(:) - drcc)
     851              :          END DO
     852              :       END IF
     853          996 :       CALL cp_results_erase(results=results, description=description)
     854              :       CALL put_results(results=results, description=description, &
     855          996 :                        values=dipole(1:3))
     856          996 :       IF (unit_nr > 0) THEN
     857              :          WRITE (unit_nr, '(/,T2,A,T31,A50)') &
     858          538 :             'TB_DIPOLE| Dipole type', ADJUSTR(TRIM(dipole_type))
     859          538 :          WRITE (unit_nr, "(T2,A,T30,3(1X,F16.8))") "TB_DIPOLE| Ref. Point [Bohr]", rcc
     860              :          WRITE (unit_nr, '(T2,A,T30,3(1X,F16.8))') &
     861          538 :             'TB_DIPOLE| Moment [a.u.]', dipole(1:3)
     862              :          WRITE (unit_nr, '(T2,A,T30,3(1X,F16.8))') &
     863         2152 :             'TB_DIPOLE| Moment [Debye]', dipole(1:3)*debye
     864              :          WRITE (unit_nr, '(T2,A,T30,3(1X,F16.8))') &
     865          538 :             'TB_DIPOLE| Derivative [a.u.]', dipole_deriv(1:3)
     866              :       END IF
     867              : 
     868          996 :    END SUBROUTINE tb_dipole
     869              : 
     870              : ! **************************************************************************************************
     871              : !> \brief computes the MOs and calls the wavefunction mixing routine.
     872              : !> \param qs_env ...
     873              : !> \param dft_section ...
     874              : !> \param scf_env ...
     875              : !> \author Florian Schiffmann
     876              : !> \note
     877              : ! **************************************************************************************************
     878              : 
     879            2 :    SUBROUTINE wfn_mix_tb(qs_env, dft_section, scf_env)
     880              : 
     881              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     882              :       TYPE(section_vals_type), POINTER                   :: dft_section
     883              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     884              : 
     885              :       INTEGER                                            :: ispin, nao, nmo, output_unit
     886            2 :       REAL(dp), DIMENSION(:), POINTER                    :: mo_eigenvalues
     887            2 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     888              :       TYPE(cp_fm_struct_type), POINTER                   :: ao_ao_fmstruct, ao_lumo_struct
     889              :       TYPE(cp_fm_type)                                   :: KS_tmp, MO_tmp, S_tmp, work
     890            2 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: lumos
     891              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     892              :       TYPE(cp_logger_type), POINTER                      :: logger
     893            2 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_ks, matrix_s
     894            2 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     895              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     896            2 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     897            2 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     898              :       TYPE(section_vals_type), POINTER                   :: wfn_mix_section
     899              : 
     900            4 :       logger => cp_get_default_logger()
     901              :       CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, matrix_ks=matrix_ks, &
     902              :                       particle_set=particle_set, atomic_kind_set=atomic_kind_set, &
     903            2 :                       qs_kind_set=qs_kind_set, mos=mos, para_env=para_env)
     904              : 
     905            2 :       wfn_mix_section => section_vals_get_subs_vals(dft_section, "PRINT%WFN_MIX")
     906              : 
     907            2 :       CALL get_mo_set(mos(1), mo_coeff=mo_coeff, nao=nao)
     908              : 
     909              :       CALL cp_fm_struct_create(fmstruct=ao_ao_fmstruct, nrow_global=nao, ncol_global=nao, &
     910            2 :                                template_fmstruct=mo_coeff%matrix_struct)
     911            2 :       CALL cp_fm_create(S_tmp, matrix_struct=ao_ao_fmstruct)
     912            2 :       CALL cp_fm_create(KS_tmp, matrix_struct=ao_ao_fmstruct)
     913            2 :       CALL cp_fm_create(MO_tmp, matrix_struct=ao_ao_fmstruct)
     914            2 :       CALL cp_fm_create(work, matrix_struct=ao_ao_fmstruct)
     915           10 :       ALLOCATE (lumos(SIZE(mos)))
     916              : 
     917            2 :       CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, S_tmp)
     918            2 :       CALL cp_fm_cholesky_decompose(S_tmp)
     919              : 
     920            6 :       DO ispin = 1, SIZE(mos)
     921            4 :          CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, eigenvalues=mo_eigenvalues, nmo=nmo)
     922              :          CALL cp_fm_struct_create(fmstruct=ao_lumo_struct, nrow_global=nao, ncol_global=nao - nmo, &
     923            4 :                                   template_fmstruct=mo_coeff%matrix_struct)
     924              : 
     925            4 :          CALL cp_fm_create(lumos(ispin), matrix_struct=ao_lumo_struct)
     926            4 :          CALL copy_dbcsr_to_fm(matrix_ks(ispin)%matrix, KS_tmp)
     927            4 :          CALL cp_fm_cholesky_reduce(KS_tmp, S_tmp)
     928            4 :          CALL choose_eigv_solver(KS_tmp, work, mo_eigenvalues)
     929            4 :          CALL cp_fm_cholesky_restore(work, nao, S_tmp, MO_tmp, "SOLVE")
     930            4 :          CALL cp_fm_to_fm_submat(MO_tmp, mo_coeff, nao, nmo, 1, 1, 1, 1)
     931            4 :          CALL cp_fm_to_fm_submat(MO_tmp, lumos(ispin), nao, nao - nmo, 1, nmo + 1, 1, 1)
     932              : 
     933           10 :          CALL cp_fm_struct_release(ao_lumo_struct)
     934              :       END DO
     935              : 
     936            2 :       output_unit = cp_logger_get_default_io_unit(logger)
     937              :       CALL wfn_mix(mos, particle_set, dft_section, qs_kind_set, para_env, output_unit, &
     938            2 :                    unoccupied_orbs=lumos, scf_env=scf_env, matrix_s=matrix_s)
     939              : 
     940            2 :       CALL cp_fm_release(lumos)
     941            2 :       CALL cp_fm_release(S_tmp)
     942            2 :       CALL cp_fm_release(MO_tmp)
     943            2 :       CALL cp_fm_release(KS_tmp)
     944            2 :       CALL cp_fm_release(work)
     945            2 :       CALL cp_fm_struct_release(ao_ao_fmstruct)
     946              : 
     947            6 :    END SUBROUTINE wfn_mix_tb
     948              : 
     949              : ! **************************************************************************************************
     950              : !> \brief Gets the lumos, and eigenvalues for the lumos
     951              : !> \param qs_env ...
     952              : !> \param scf_env ...
     953              : !> \param unoccupied_orbs ...
     954              : !> \param unoccupied_evals ...
     955              : !> \param nlumo ...
     956              : !> \param nlumos ...
     957              : ! **************************************************************************************************
     958            2 :    SUBROUTINE make_lumo_tb(qs_env, scf_env, unoccupied_orbs, unoccupied_evals, nlumo, nlumos)
     959              : 
     960              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     961              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     962              :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: unoccupied_orbs
     963              :       TYPE(cp_1d_r_p_type), DIMENSION(:), INTENT(INOUT)  :: unoccupied_evals
     964              :       INTEGER                                            :: nlumo
     965              :       INTEGER, INTENT(OUT)                               :: nlumos
     966              : 
     967              :       INTEGER                                            :: homo, iounit, ispin, n, nao, nmo
     968              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     969              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_tmp
     970              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     971              :       TYPE(cp_logger_type), POINTER                      :: logger
     972            2 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_rmpv, matrix_s
     973              :       TYPE(dft_control_type), POINTER                    :: dft_control
     974            2 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     975              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     976              :       TYPE(preconditioner_type), POINTER                 :: local_preconditioner
     977              :       TYPE(scf_control_type), POINTER                    :: scf_control
     978              : 
     979            2 :       NULLIFY (mos, ks_rmpv, scf_control, dft_control, para_env, blacs_env)
     980              :       CALL get_qs_env(qs_env, &
     981              :                       mos=mos, &
     982              :                       matrix_ks=ks_rmpv, &
     983              :                       scf_control=scf_control, &
     984              :                       dft_control=dft_control, &
     985              :                       matrix_s=matrix_s, &
     986              :                       para_env=para_env, &
     987            2 :                       blacs_env=blacs_env)
     988              : 
     989            2 :       logger => cp_get_default_logger()
     990            2 :       iounit = cp_logger_get_default_io_unit(logger)
     991              : 
     992            4 :       DO ispin = 1, dft_control%nspins
     993            2 :          NULLIFY (unoccupied_evals(ispin)%array)
     994              :          ! Always write eigenvalues
     995            2 :          IF (iounit > 0) WRITE (iounit, *) " "
     996            2 :          IF (iounit > 0) WRITE (iounit, *) " Lowest Eigenvalues of the unoccupied subspace spin ", ispin
     997            2 :          IF (iounit > 0) WRITE (iounit, FMT='(1X,A)') "-----------------------------------------------------"
     998            2 :          CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, homo=homo, nao=nao, nmo=nmo)
     999            2 :          CALL cp_fm_get_info(mo_coeff, nrow_global=n)
    1000            2 :          nlumos = MAX(1, MIN(nlumo, nao - nmo))
    1001            2 :          IF (nlumo == -1) nlumos = nao - nmo
    1002            6 :          ALLOCATE (unoccupied_evals(ispin)%array(nlumos))
    1003              :          CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=blacs_env, &
    1004            2 :                                   nrow_global=n, ncol_global=nlumos)
    1005            2 :          CALL cp_fm_create(unoccupied_orbs(ispin), fm_struct_tmp, name="lumos")
    1006            2 :          CALL cp_fm_struct_release(fm_struct_tmp)
    1007            2 :          CALL cp_fm_init_random(unoccupied_orbs(ispin), nlumos)
    1008              : 
    1009              :          ! the full_all preconditioner makes not much sense for lumos search
    1010            2 :          NULLIFY (local_preconditioner)
    1011            2 :          IF (ASSOCIATED(scf_env%ot_preconditioner)) THEN
    1012            2 :             local_preconditioner => scf_env%ot_preconditioner(1)%preconditioner
    1013              :             ! this one can for sure not be right (as it has to match a given C0)
    1014            2 :             IF (local_preconditioner%in_use == ot_precond_full_all) THEN
    1015            2 :                NULLIFY (local_preconditioner)
    1016              :             END IF
    1017              :          END IF
    1018              : 
    1019              :          CALL ot_eigensolver(matrix_h=ks_rmpv(ispin)%matrix, matrix_s=matrix_s(1)%matrix, &
    1020              :                              matrix_c_fm=unoccupied_orbs(ispin), &
    1021              :                              matrix_orthogonal_space_fm=mo_coeff, &
    1022              :                              eps_gradient=scf_control%eps_lumos, &
    1023              :                              preconditioner=local_preconditioner, &
    1024              :                              iter_max=scf_control%max_iter_lumos, &
    1025            2 :                              size_ortho_space=nmo)
    1026              : 
    1027              :          CALL calculate_subspace_eigenvalues(unoccupied_orbs(ispin), ks_rmpv(ispin)%matrix, &
    1028              :                                              unoccupied_evals(ispin)%array, scr=iounit, &
    1029            6 :                                              ionode=iounit > 0)
    1030              : 
    1031              :       END DO
    1032              : 
    1033            2 :    END SUBROUTINE make_lumo_tb
    1034              : 
    1035              : ! **************************************************************************************************
    1036              : !> \brief ...
    1037              : !> \param qs_env ...
    1038              : ! **************************************************************************************************
    1039           32 :    SUBROUTINE rebuild_pw_env(qs_env)
    1040              : 
    1041              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1042              : 
    1043              :       LOGICAL                                            :: skip_load_balance_distributed
    1044              :       TYPE(cell_type), POINTER                           :: cell
    1045              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1046              :       TYPE(pw_env_type), POINTER                         :: new_pw_env
    1047              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1048              :       TYPE(qs_rho_type), POINTER                         :: rho
    1049              :       TYPE(task_list_type), POINTER                      :: task_list
    1050              : 
    1051           32 :       CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control, pw_env=new_pw_env)
    1052           32 :       IF (.NOT. ASSOCIATED(new_pw_env)) THEN
    1053            0 :          CALL pw_env_create(new_pw_env)
    1054            0 :          CALL set_ks_env(ks_env, pw_env=new_pw_env)
    1055            0 :          CALL pw_env_release(new_pw_env)
    1056              :       END IF
    1057           32 :       CALL get_qs_env(qs_env, pw_env=new_pw_env, dft_control=dft_control, cell=cell)
    1058              : 
    1059          832 :       new_pw_env%cell_hmat = cell%hmat
    1060           32 :       CALL pw_env_rebuild(new_pw_env, qs_env=qs_env)
    1061              : 
    1062           32 :       NULLIFY (task_list)
    1063           32 :       CALL get_ks_env(ks_env, task_list=task_list)
    1064           32 :       IF (.NOT. ASSOCIATED(task_list)) THEN
    1065           32 :          CALL allocate_task_list(task_list)
    1066           32 :          CALL set_ks_env(ks_env, task_list=task_list)
    1067              :       END IF
    1068           32 :       skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
    1069              :       CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB", &
    1070              :                                  reorder_rs_grid_ranks=.TRUE., &
    1071           32 :                                  skip_load_balance_distributed=skip_load_balance_distributed)
    1072           32 :       CALL get_qs_env(qs_env, rho=rho)
    1073           32 :       CALL qs_rho_rebuild(rho, qs_env=qs_env, rebuild_ao=.FALSE., rebuild_grids=.TRUE.)
    1074              : 
    1075           32 :    END SUBROUTINE rebuild_pw_env
    1076              : 
    1077              : ! **************************************************************************************************
    1078              : !> \brief ...
    1079              : !> \param qs_env ...
    1080              : !> \param zcharge ...
    1081              : !> \param cube_section ...
    1082              : ! **************************************************************************************************
    1083           24 :    SUBROUTINE print_e_density(qs_env, zcharge, cube_section)
    1084              : 
    1085              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1086              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: zcharge
    1087              :       TYPE(section_vals_type), POINTER                   :: cube_section
    1088              : 
    1089              :       CHARACTER(LEN=default_path_length)                 :: filename, mpi_filename, my_pos_cube
    1090              :       INTEGER                                            :: iounit, ispin, unit_nr
    1091              :       LOGICAL                                            :: append_cube, mpi_io
    1092           24 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tot_rho_r
    1093              :       TYPE(cp_logger_type), POINTER                      :: logger
    1094           24 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
    1095           24 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao_kp
    1096              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1097              :       TYPE(particle_list_type), POINTER                  :: particles
    1098           24 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
    1099              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1100           24 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1101              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1102           24 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r
    1103              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1104              :       TYPE(qs_rho_type), POINTER                         :: rho
    1105              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1106              : 
    1107           24 :       CALL get_qs_env(qs_env, dft_control=dft_control)
    1108              : 
    1109           24 :       append_cube = section_get_lval(cube_section, "APPEND")
    1110           24 :       my_pos_cube = "REWIND"
    1111           24 :       IF (append_cube) my_pos_cube = "APPEND"
    1112              : 
    1113           24 :       logger => cp_get_default_logger()
    1114           24 :       iounit = cp_logger_get_default_io_unit(logger)
    1115              : 
    1116              :       ! we need to construct the density on a realspace grid
    1117           24 :       CALL get_qs_env(qs_env, ks_env=ks_env, rho=rho)
    1118           24 :       NULLIFY (rho_r, rho_g, tot_rho_r)
    1119              :       CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp, &
    1120           24 :                       rho_r=rho_r, rho_g=rho_g, tot_rho_r=tot_rho_r)
    1121           50 :       DO ispin = 1, dft_control%nspins
    1122           26 :          rho_ao => rho_ao_kp(ispin, :)
    1123              :          CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
    1124              :                                  rho=rho_r(ispin), &
    1125              :                                  rho_gspace=rho_g(ispin), &
    1126              :                                  total_rho=tot_rho_r(ispin), &
    1127           50 :                                  ks_env=ks_env)
    1128              :       END DO
    1129           24 :       CALL qs_rho_set(rho, rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
    1130              : 
    1131           24 :       CALL get_qs_env(qs_env, subsys=subsys)
    1132           24 :       CALL qs_subsys_get(subsys, particles=particles)
    1133              : 
    1134           24 :       IF (dft_control%nspins > 1) THEN
    1135            2 :          IF (iounit > 0) THEN
    1136              :             WRITE (UNIT=iounit, FMT="(/,T2,A,T51,2F15.6)") &
    1137            3 :                "Integrated alpha and beta electronic density:", tot_rho_r(1:2)
    1138              :          END IF
    1139            2 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    1140            2 :          CALL pw_env_get(pw_env=pw_env, auxbas_pw_pool=auxbas_pw_pool, pw_pools=pw_pools)
    1141              :          BLOCK
    1142              :             TYPE(pw_r3d_rs_type) :: rho_elec_rspace
    1143            2 :             CALL auxbas_pw_pool%create_pw(pw=rho_elec_rspace)
    1144            2 :             CALL pw_copy(rho_r(1), rho_elec_rspace)
    1145            2 :             CALL pw_axpy(rho_r(2), rho_elec_rspace)
    1146            2 :             filename = "ELECTRON_DENSITY"
    1147            2 :             mpi_io = .TRUE.
    1148              :             unit_nr = cp_print_key_unit_nr(logger, cube_section, '', &
    1149              :                                            extension=".cube", middle_name=TRIM(filename), &
    1150              :                                            file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    1151            2 :                                            fout=mpi_filename)
    1152            2 :             IF (iounit > 0) THEN
    1153            1 :                IF (.NOT. mpi_io) THEN
    1154            0 :                   INQUIRE (UNIT=unit_nr, NAME=filename)
    1155              :                ELSE
    1156            1 :                   filename = mpi_filename
    1157              :                END IF
    1158              :                WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1159            1 :                   "The sum of alpha and beta density is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1160              :             END IF
    1161              :             CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "SUM OF ALPHA AND BETA DENSITY", &
    1162              :                                particles=particles, zeff=zcharge, stride=section_get_ivals(cube_section, "STRIDE"), &
    1163            2 :                                mpi_io=mpi_io)
    1164            2 :             CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1165            2 :             CALL pw_copy(rho_r(1), rho_elec_rspace)
    1166            2 :             CALL pw_axpy(rho_r(2), rho_elec_rspace, alpha=-1.0_dp)
    1167            2 :             filename = "SPIN_DENSITY"
    1168            2 :             mpi_io = .TRUE.
    1169              :             unit_nr = cp_print_key_unit_nr(logger, cube_section, '', &
    1170              :                                            extension=".cube", middle_name=TRIM(filename), &
    1171              :                                            file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    1172            2 :                                            fout=mpi_filename)
    1173            2 :             IF (iounit > 0) THEN
    1174            1 :                IF (.NOT. mpi_io) THEN
    1175            0 :                   INQUIRE (UNIT=unit_nr, NAME=filename)
    1176              :                ELSE
    1177            1 :                   filename = mpi_filename
    1178              :                END IF
    1179              :                WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1180            1 :                   "The spin density is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1181              :             END IF
    1182              :             CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "SPIN DENSITY", &
    1183              :                                particles=particles, zeff=zcharge, &
    1184            2 :                                stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1185            2 :             CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1186            2 :             CALL auxbas_pw_pool%give_back_pw(rho_elec_rspace)
    1187              :          END BLOCK
    1188              :       ELSE
    1189           22 :          IF (iounit > 0) THEN
    1190              :             WRITE (UNIT=iounit, FMT="(/,T2,A,T66,F15.6)") &
    1191           11 :                "Integrated electronic density:", tot_rho_r(1)
    1192              :          END IF
    1193           22 :          filename = "ELECTRON_DENSITY"
    1194           22 :          mpi_io = .TRUE.
    1195              :          unit_nr = cp_print_key_unit_nr(logger, cube_section, '', &
    1196              :                                         extension=".cube", middle_name=TRIM(filename), &
    1197              :                                         file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    1198           22 :                                         fout=mpi_filename)
    1199           22 :          IF (iounit > 0) THEN
    1200           11 :             IF (.NOT. mpi_io) THEN
    1201            0 :                INQUIRE (UNIT=unit_nr, NAME=filename)
    1202              :             ELSE
    1203           11 :                filename = mpi_filename
    1204              :             END IF
    1205              :             WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1206           11 :                "The electron density is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1207              :          END IF
    1208              :          CALL cp_pw_to_cube(rho_r(1), unit_nr, "ELECTRON DENSITY", &
    1209              :                             particles=particles, zeff=zcharge, &
    1210           22 :                             stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1211           22 :          CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1212              :       END IF ! nspins
    1213              : 
    1214           24 :    END SUBROUTINE print_e_density
    1215              : ! **************************************************************************************************
    1216              : !> \brief ...
    1217              : !> \param qs_env ...
    1218              : !> \param zcharge ...
    1219              : !> \param cube_section ...
    1220              : !> \param total_density ...
    1221              : !> \param v_hartree ...
    1222              : !> \param efield ...
    1223              : ! **************************************************************************************************
    1224           74 :    SUBROUTINE print_density_cubes(qs_env, zcharge, cube_section, total_density, v_hartree, efield)
    1225              : 
    1226              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1227              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: zcharge
    1228              :       TYPE(section_vals_type), POINTER                   :: cube_section
    1229              :       LOGICAL, INTENT(IN), OPTIONAL                      :: total_density, v_hartree, efield
    1230              : 
    1231              :       CHARACTER(len=1), DIMENSION(3), PARAMETER          :: cdir = ["x", "y", "z"]
    1232              : 
    1233              :       CHARACTER(LEN=default_path_length)                 :: filename, mpi_filename, my_pos_cube
    1234              :       INTEGER                                            :: id, iounit, ispin, nd(3), unit_nr
    1235              :       LOGICAL                                            :: append_cube, mpi_io, my_efield, &
    1236              :                                                             my_total_density, my_v_hartree
    1237              :       REAL(KIND=dp)                                      :: total_rho_core_rspace, udvol
    1238           74 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tot_rho_r
    1239              :       TYPE(cell_type), POINTER                           :: cell
    1240              :       TYPE(cp_logger_type), POINTER                      :: logger
    1241           74 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
    1242           74 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao_kp
    1243              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1244              :       TYPE(particle_list_type), POINTER                  :: particles
    1245              :       TYPE(pw_c1d_gs_type)                               :: rho_core
    1246           74 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
    1247              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1248              :       TYPE(pw_poisson_parameter_type)                    :: poisson_params
    1249           74 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1250              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1251              :       TYPE(pw_r3d_rs_type)                               :: rho_tot_rspace
    1252           74 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r
    1253              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1254              :       TYPE(qs_rho_type), POINTER                         :: rho
    1255              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1256              : 
    1257           74 :       CALL get_qs_env(qs_env, cell=cell, dft_control=dft_control)
    1258              : 
    1259           74 :       append_cube = section_get_lval(cube_section, "APPEND")
    1260           74 :       my_pos_cube = "REWIND"
    1261           74 :       IF (append_cube) my_pos_cube = "APPEND"
    1262              : 
    1263           74 :       IF (PRESENT(total_density)) THEN
    1264           26 :          my_total_density = total_density
    1265              :       ELSE
    1266              :          my_total_density = .FALSE.
    1267              :       END IF
    1268           74 :       IF (PRESENT(v_hartree)) THEN
    1269           24 :          my_v_hartree = v_hartree
    1270              :       ELSE
    1271              :          my_v_hartree = .FALSE.
    1272              :       END IF
    1273           74 :       IF (PRESENT(efield)) THEN
    1274           24 :          my_efield = efield
    1275              :       ELSE
    1276              :          my_efield = .FALSE.
    1277              :       END IF
    1278              : 
    1279           74 :       logger => cp_get_default_logger()
    1280           74 :       iounit = cp_logger_get_default_io_unit(logger)
    1281              : 
    1282              :       ! we need to construct the density on a realspace grid
    1283           74 :       CALL get_qs_env(qs_env, ks_env=ks_env, rho=rho)
    1284           74 :       NULLIFY (rho_r, rho_g, tot_rho_r)
    1285              :       CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp, &
    1286           74 :                       rho_r=rho_r, rho_g=rho_g, tot_rho_r=tot_rho_r)
    1287          150 :       DO ispin = 1, dft_control%nspins
    1288           76 :          rho_ao => rho_ao_kp(ispin, :)
    1289              :          CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
    1290              :                                  rho=rho_r(ispin), &
    1291              :                                  rho_gspace=rho_g(ispin), &
    1292              :                                  total_rho=tot_rho_r(ispin), &
    1293          150 :                                  ks_env=ks_env)
    1294              :       END DO
    1295           74 :       CALL qs_rho_set(rho, rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
    1296              : 
    1297           74 :       CALL get_qs_env(qs_env, subsys=subsys)
    1298           74 :       CALL qs_subsys_get(subsys, particles=particles)
    1299              : 
    1300           74 :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    1301           74 :       CALL pw_env_get(pw_env=pw_env, auxbas_pw_pool=auxbas_pw_pool, pw_pools=pw_pools)
    1302           74 :       CALL auxbas_pw_pool%create_pw(pw=rho_core)
    1303           74 :       CALL calculate_rho_core(rho_core, total_rho_core_rspace, qs_env)
    1304              : 
    1305           74 :       IF (iounit > 0) THEN
    1306              :          WRITE (UNIT=iounit, FMT="(/,T2,A,T66,F15.6)") &
    1307           75 :             "Integrated electronic density:", SUM(tot_rho_r(:))
    1308              :          WRITE (UNIT=iounit, FMT="(T2,A,T66,F15.6)") &
    1309           37 :             "Integrated core density:", total_rho_core_rspace
    1310              :       END IF
    1311              : 
    1312           74 :       CALL auxbas_pw_pool%create_pw(pw=rho_tot_rspace)
    1313           74 :       CALL pw_transfer(rho_core, rho_tot_rspace)
    1314          150 :       DO ispin = 1, dft_control%nspins
    1315          150 :          CALL pw_axpy(rho_r(ispin), rho_tot_rspace)
    1316              :       END DO
    1317              : 
    1318           74 :       IF (my_total_density) THEN
    1319           26 :          filename = "TOTAL_DENSITY"
    1320           26 :          mpi_io = .TRUE.
    1321              :          unit_nr = cp_print_key_unit_nr(logger, cube_section, '', &
    1322              :                                         extension=".cube", middle_name=TRIM(filename), file_position=my_pos_cube, &
    1323           26 :                                         log_filename=.FALSE., mpi_io=mpi_io, fout=mpi_filename)
    1324           26 :          IF (iounit > 0) THEN
    1325           13 :             IF (.NOT. mpi_io) THEN
    1326            0 :                INQUIRE (UNIT=unit_nr, NAME=filename)
    1327              :             ELSE
    1328           13 :                filename = mpi_filename
    1329              :             END IF
    1330              :             WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1331           13 :                "The total density is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1332              :          END IF
    1333              :          CALL cp_pw_to_cube(rho_tot_rspace, unit_nr, "TOTAL DENSITY", &
    1334              :                             particles=particles, zeff=zcharge, &
    1335           26 :                             stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1336           26 :          CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1337              :       END IF
    1338           74 :       IF (my_v_hartree .OR. my_efield) THEN
    1339              :          BLOCK
    1340              :             TYPE(pw_c1d_gs_type) :: rho_tot_gspace
    1341           48 :             CALL auxbas_pw_pool%create_pw(pw=rho_tot_gspace)
    1342           48 :             CALL pw_transfer(rho_tot_rspace, rho_tot_gspace)
    1343           48 :             poisson_params%solver = pw_poisson_analytic
    1344          192 :             poisson_params%periodic = cell%perd
    1345           48 :             poisson_params%ewald_type = do_ewald_none
    1346           96 :             BLOCK
    1347           48 :                TYPE(greens_fn_type)                     :: green_fft
    1348              :                TYPE(pw_grid_type), POINTER                        :: pwdummy
    1349           48 :                NULLIFY (pwdummy)
    1350           48 :                CALL pw_green_create(green_fft, poisson_params, cell%hmat, auxbas_pw_pool, pwdummy, pwdummy)
    1351       825006 :                rho_tot_gspace%array(:) = rho_tot_gspace%array(:)*green_fft%influence_fn%array(:)
    1352           96 :                CALL pw_green_release(green_fft, auxbas_pw_pool)
    1353              :             END BLOCK
    1354           48 :             IF (my_v_hartree) THEN
    1355              :                BLOCK
    1356              :                   TYPE(pw_r3d_rs_type) :: vhartree
    1357           24 :                   CALL auxbas_pw_pool%create_pw(pw=vhartree)
    1358           24 :                   CALL pw_transfer(rho_tot_gspace, vhartree)
    1359           24 :                   filename = "V_HARTREE"
    1360           24 :                   mpi_io = .TRUE.
    1361              :                   unit_nr = cp_print_key_unit_nr(logger, cube_section, '', &
    1362              :                                                  extension=".cube", middle_name=TRIM(filename), file_position=my_pos_cube, &
    1363           24 :                                                  log_filename=.FALSE., mpi_io=mpi_io, fout=mpi_filename)
    1364           24 :                   IF (iounit > 0) THEN
    1365           12 :                      IF (.NOT. mpi_io) THEN
    1366            0 :                         INQUIRE (UNIT=unit_nr, NAME=filename)
    1367              :                      ELSE
    1368           12 :                         filename = mpi_filename
    1369              :                      END IF
    1370              :                      WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1371           12 :                         "The Hartree potential is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1372              :                   END IF
    1373              :                   CALL cp_pw_to_cube(vhartree, unit_nr, "Hartree Potential", &
    1374              :                                      particles=particles, zeff=zcharge, &
    1375           24 :                                      stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1376           24 :                   CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1377           24 :                   CALL auxbas_pw_pool%give_back_pw(vhartree)
    1378              :                END BLOCK
    1379              :             END IF
    1380           48 :             IF (my_efield) THEN
    1381              :                BLOCK
    1382              :                   TYPE(pw_c1d_gs_type) :: vhartree
    1383           24 :                   CALL auxbas_pw_pool%create_pw(pw=vhartree)
    1384           24 :                   udvol = 1.0_dp/rho_tot_rspace%pw_grid%dvol
    1385           96 :                   DO id = 1, 3
    1386           72 :                      CALL pw_transfer(rho_tot_gspace, vhartree)
    1387           72 :                      nd = 0
    1388           72 :                      nd(id) = 1
    1389           72 :                      CALL pw_derive(vhartree, nd)
    1390           72 :                      CALL pw_transfer(vhartree, rho_tot_rspace)
    1391           72 :                      CALL pw_scale(rho_tot_rspace, udvol)
    1392              : 
    1393           72 :                      filename = "EFIELD_"//cdir(id)
    1394           72 :                      mpi_io = .TRUE.
    1395              :                      unit_nr = cp_print_key_unit_nr(logger, cube_section, '', &
    1396              :                                                     extension=".cube", middle_name=TRIM(filename), file_position=my_pos_cube, &
    1397           72 :                                                     log_filename=.FALSE., mpi_io=mpi_io, fout=mpi_filename)
    1398           72 :                      IF (iounit > 0) THEN
    1399           36 :                         IF (.NOT. mpi_io) THEN
    1400            0 :                            INQUIRE (UNIT=unit_nr, NAME=filename)
    1401              :                         ELSE
    1402           36 :                            filename = mpi_filename
    1403              :                         END IF
    1404              :                         WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1405           36 :                            "The Efield is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1406              :                      END IF
    1407              :                      CALL cp_pw_to_cube(rho_tot_rspace, unit_nr, "EFIELD "//cdir(id), &
    1408              :                                         particles=particles, zeff=zcharge, &
    1409           72 :                                         stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1410           96 :                      CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1411              :                   END DO
    1412           24 :                   CALL auxbas_pw_pool%give_back_pw(vhartree)
    1413              :                END BLOCK
    1414              :             END IF
    1415           48 :             CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
    1416              :          END BLOCK
    1417              :       END IF
    1418              : 
    1419           74 :       CALL auxbas_pw_pool%give_back_pw(rho_tot_rspace)
    1420           74 :       CALL auxbas_pw_pool%give_back_pw(rho_core)
    1421              : 
    1422          296 :    END SUBROUTINE print_density_cubes
    1423              : 
    1424              : ! **************************************************************************************************
    1425              : !> \brief ...
    1426              : !> \param qs_env ...
    1427              : !> \param zcharge ...
    1428              : !> \param elf_section ...
    1429              : ! **************************************************************************************************
    1430           24 :    SUBROUTINE print_elf(qs_env, zcharge, elf_section)
    1431              : 
    1432              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1433              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: zcharge
    1434              :       TYPE(section_vals_type), POINTER                   :: elf_section
    1435              : 
    1436              :       CHARACTER(LEN=default_path_length)                 :: filename, mpi_filename, my_pos_cube, &
    1437              :                                                             title
    1438              :       INTEGER                                            :: iounit, ispin, unit_nr
    1439              :       LOGICAL                                            :: append_cube, mpi_io
    1440              :       REAL(KIND=dp)                                      :: rho_cutoff
    1441           24 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tot_rho_r
    1442              :       TYPE(cp_logger_type), POINTER                      :: logger
    1443           24 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
    1444           24 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: rho_ao_kp
    1445              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1446              :       TYPE(particle_list_type), POINTER                  :: particles
    1447           24 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
    1448              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1449           24 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1450              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1451           24 :       TYPE(pw_r3d_rs_type), ALLOCATABLE, DIMENSION(:)    :: elf_r
    1452           24 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r
    1453              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
    1454              :       TYPE(qs_rho_type), POINTER                         :: rho
    1455              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1456              : 
    1457           48 :       logger => cp_get_default_logger()
    1458           24 :       iounit = cp_logger_get_default_io_unit(logger)
    1459              : 
    1460              :       ! we need to construct the density on a realspace grid
    1461           24 :       CALL get_qs_env(qs_env, dft_control=dft_control, ks_env=ks_env, rho=rho)
    1462           24 :       NULLIFY (rho_r, rho_g, tot_rho_r)
    1463              :       CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp, &
    1464           24 :                       rho_r=rho_r, rho_g=rho_g, tot_rho_r=tot_rho_r)
    1465           50 :       DO ispin = 1, dft_control%nspins
    1466           26 :          rho_ao => rho_ao_kp(ispin, :)
    1467              :          CALL calculate_rho_elec(matrix_p_kp=rho_ao, &
    1468              :                                  rho=rho_r(ispin), &
    1469              :                                  rho_gspace=rho_g(ispin), &
    1470              :                                  total_rho=tot_rho_r(ispin), &
    1471           50 :                                  ks_env=ks_env)
    1472              :       END DO
    1473           24 :       CALL qs_rho_set(rho, rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
    1474              : 
    1475           24 :       CALL get_qs_env(qs_env, subsys=subsys)
    1476           24 :       CALL qs_subsys_get(subsys, particles=particles)
    1477              : 
    1478           98 :       ALLOCATE (elf_r(dft_control%nspins))
    1479           24 :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    1480           24 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, pw_pools=pw_pools)
    1481           50 :       DO ispin = 1, dft_control%nspins
    1482           26 :          CALL auxbas_pw_pool%create_pw(elf_r(ispin))
    1483           50 :          CALL pw_zero(elf_r(ispin))
    1484              :       END DO
    1485              : 
    1486           24 :       IF (iounit > 0) THEN
    1487              :          WRITE (UNIT=iounit, FMT="(/,T2,A)") &
    1488           12 :             "ELF is computed on the real space grid -----"
    1489              :       END IF
    1490           24 :       rho_cutoff = section_get_rval(elf_section, "density_cutoff")
    1491           24 :       CALL qs_elf_calc(qs_env, elf_r, rho_cutoff)
    1492              : 
    1493              :       ! write ELF into cube file
    1494           24 :       append_cube = section_get_lval(elf_section, "APPEND")
    1495           24 :       my_pos_cube = "REWIND"
    1496           24 :       IF (append_cube) my_pos_cube = "APPEND"
    1497           50 :       DO ispin = 1, dft_control%nspins
    1498           26 :          WRITE (filename, '(a5,I1.1)') "ELF_S", ispin
    1499           26 :          WRITE (title, *) "ELF spin ", ispin
    1500           26 :          mpi_io = .TRUE.
    1501              :          unit_nr = cp_print_key_unit_nr(logger, elf_section, '', extension=".cube", &
    1502              :                                         middle_name=TRIM(filename), file_position=my_pos_cube, &
    1503           26 :                                         log_filename=.FALSE., mpi_io=mpi_io, fout=mpi_filename)
    1504           26 :          IF (iounit > 0) THEN
    1505           13 :             IF (.NOT. mpi_io) THEN
    1506            0 :                INQUIRE (UNIT=unit_nr, NAME=filename)
    1507              :             ELSE
    1508           13 :                filename = mpi_filename
    1509              :             END IF
    1510              :             WRITE (UNIT=iounit, FMT="(T2,A,/,T2,A79)") &
    1511           13 :                "ELF is written in cube file format to the file:", ADJUSTR(TRIM(filename))
    1512              :          END IF
    1513              : 
    1514              :          CALL cp_pw_to_cube(elf_r(ispin), unit_nr, title, particles=particles, zeff=zcharge, &
    1515           26 :                             stride=section_get_ivals(elf_section, "STRIDE"), mpi_io=mpi_io)
    1516           26 :          CALL cp_print_key_finished_output(unit_nr, logger, elf_section, '', mpi_io=mpi_io)
    1517              : 
    1518           50 :          CALL auxbas_pw_pool%give_back_pw(elf_r(ispin))
    1519              :       END DO
    1520              : 
    1521           24 :       DEALLOCATE (elf_r)
    1522              : 
    1523           24 :    END SUBROUTINE print_elf
    1524              : ! **************************************************************************************************
    1525              : !> \brief ...
    1526              : !> \param qs_env ...
    1527              : !> \param zcharge ...
    1528              : !> \param cube_section ...
    1529              : ! **************************************************************************************************
    1530           24 :    SUBROUTINE print_mo_cubes(qs_env, zcharge, cube_section)
    1531              : 
    1532              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1533              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: zcharge
    1534              :       TYPE(section_vals_type), POINTER                   :: cube_section
    1535              : 
    1536              :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube, title
    1537              :       INTEGER                                            :: homo, i, ifirst, ilast, iounit, ir, &
    1538              :                                                             ispin, ivector, n_rep, nhomo, nlist, &
    1539              :                                                             nlumo, nmo, shomo, unit_nr
    1540           24 :       INTEGER, DIMENSION(:), POINTER                     :: list, list_index
    1541              :       LOGICAL                                            :: append_cube, mpi_io, write_cube
    1542              :       REAL(KIND=dp)                                      :: homo_lumo(2, 2)
    1543           24 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: mo_eigenvalues
    1544           24 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1545              :       TYPE(cell_type), POINTER                           :: cell
    1546              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1547              :       TYPE(cp_logger_type), POINTER                      :: logger
    1548           24 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_rmpv, mo_derivs
    1549              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1550           24 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1551              :       TYPE(particle_list_type), POINTER                  :: particles
    1552           24 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1553              :       TYPE(pw_c1d_gs_type)                               :: wf_g
    1554              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1555           24 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1556              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1557              :       TYPE(pw_r3d_rs_type)                               :: wf_r
    1558           24 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1559              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1560              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1561              : 
    1562           48 :       logger => cp_get_default_logger()
    1563           24 :       iounit = cp_logger_get_default_io_unit(logger)
    1564              : 
    1565           24 :       CALL get_qs_env(qs_env, mos=mos, matrix_ks=ks_rmpv, scf_control=scf_control)
    1566           24 :       CALL get_qs_env(qs_env, dft_control=dft_control, mo_derivs=mo_derivs)
    1567           24 :       CALL make_mo_eig(mos, dft_control%nspins, ks_rmpv, scf_control, mo_derivs)
    1568           24 :       NULLIFY (mo_eigenvalues)
    1569           24 :       homo = 0
    1570           50 :       DO ispin = 1, dft_control%nspins
    1571           26 :          CALL get_mo_set(mo_set=mos(ispin), eigenvalues=mo_eigenvalues, homo=shomo)
    1572           26 :          homo_lumo(ispin, 1) = mo_eigenvalues(shomo)
    1573           50 :          homo = MAX(homo, shomo)
    1574              :       END DO
    1575           24 :       write_cube = section_get_lval(cube_section, "WRITE_CUBE")
    1576           24 :       nlumo = section_get_ival(cube_section, "NLUMO")
    1577           24 :       nhomo = section_get_ival(cube_section, "NHOMO")
    1578           24 :       NULLIFY (list_index)
    1579           24 :       CALL section_vals_val_get(cube_section, "HOMO_LIST", n_rep_val=n_rep)
    1580           24 :       IF (n_rep > 0) THEN
    1581            2 :          nlist = 0
    1582            4 :          DO ir = 1, n_rep
    1583            2 :             NULLIFY (list)
    1584            2 :             CALL section_vals_val_get(cube_section, "HOMO_LIST", i_rep_val=ir, i_vals=list)
    1585            4 :             IF (ASSOCIATED(list)) THEN
    1586            2 :                CALL reallocate(list_index, 1, nlist + SIZE(list))
    1587           14 :                DO i = 1, SIZE(list)
    1588           14 :                   list_index(i + nlist) = list(i)
    1589              :                END DO
    1590            2 :                nlist = nlist + SIZE(list)
    1591              :             END IF
    1592              :          END DO
    1593           14 :          nhomo = MAXVAL(list_index)
    1594              :       ELSE
    1595           22 :          IF (nhomo == -1) nhomo = homo
    1596           22 :          nlist = homo - MAX(1, homo - nhomo + 1) + 1
    1597           66 :          ALLOCATE (list_index(nlist))
    1598           44 :          DO i = 1, nlist
    1599           44 :             list_index(i) = MAX(1, homo - nhomo + 1) + i - 1
    1600              :          END DO
    1601              :       END IF
    1602              : 
    1603           24 :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    1604           24 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, pw_pools=pw_pools)
    1605           24 :       CALL auxbas_pw_pool%create_pw(wf_r)
    1606           24 :       CALL auxbas_pw_pool%create_pw(wf_g)
    1607              : 
    1608           24 :       CALL get_qs_env(qs_env, subsys=subsys)
    1609           24 :       CALL qs_subsys_get(subsys, particles=particles)
    1610              : 
    1611           24 :       append_cube = section_get_lval(cube_section, "APPEND")
    1612           24 :       my_pos_cube = "REWIND"
    1613           24 :       IF (append_cube) THEN
    1614            0 :          my_pos_cube = "APPEND"
    1615              :       END IF
    1616              : 
    1617              :       CALL get_qs_env(qs_env=qs_env, &
    1618              :                       atomic_kind_set=atomic_kind_set, &
    1619              :                       qs_kind_set=qs_kind_set, &
    1620              :                       cell=cell, &
    1621           24 :                       particle_set=particle_set)
    1622              : 
    1623           24 :       IF (nhomo >= 0) THEN
    1624           50 :          DO ispin = 1, dft_control%nspins
    1625              :             ! Prints the cube files of OCCUPIED ORBITALS
    1626              :             CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
    1627           26 :                             eigenvalues=mo_eigenvalues, homo=homo, nmo=nmo)
    1628           50 :             IF (write_cube) THEN
    1629           72 :                DO i = 1, nlist
    1630           46 :                   ivector = list_index(i)
    1631           46 :                   IF (ivector > homo) CYCLE
    1632              :                   CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
    1633           46 :                                               cell, dft_control, particle_set, pw_env)
    1634           46 :                   WRITE (filename, '(a4,I5.5,a1,I1.1)') "WFN_", ivector, "_", ispin
    1635           46 :                   mpi_io = .TRUE.
    1636              :                   unit_nr = cp_print_key_unit_nr(logger, cube_section, '', extension=".cube", &
    1637              :                                                  middle_name=TRIM(filename), file_position=my_pos_cube, &
    1638           46 :                                                  log_filename=.FALSE., mpi_io=mpi_io)
    1639           46 :                   WRITE (title, *) "WAVEFUNCTION ", ivector, " spin ", ispin, " i.e. HOMO - ", ivector - homo
    1640              :                   CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, zeff=zcharge, &
    1641           46 :                                      stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1642           72 :                   CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1643              :                END DO
    1644              :             END IF
    1645              :          END DO
    1646              :       END IF
    1647              : 
    1648           24 :       IF (nlumo /= 0) THEN
    1649            6 :          DO ispin = 1, dft_control%nspins
    1650              :             ! Prints the cube files of UNOCCUPIED ORBITALS
    1651              :             CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
    1652            4 :                             eigenvalues=mo_eigenvalues, homo=homo, nmo=nmo)
    1653            6 :             IF (write_cube) THEN
    1654            4 :                ifirst = homo + 1
    1655            4 :                IF (nlumo == -1) THEN
    1656            0 :                   ilast = nmo
    1657              :                ELSE
    1658            4 :                   ilast = ifirst + nlumo - 1
    1659            4 :                   ilast = MIN(nmo, ilast)
    1660              :                END IF
    1661           12 :                DO ivector = ifirst, ilast
    1662              :                   CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, &
    1663            8 :                                               qs_kind_set, cell, dft_control, particle_set, pw_env)
    1664            8 :                   WRITE (filename, '(a4,I5.5,a1,I1.1)') "WFN_", ivector, "_", ispin
    1665            8 :                   mpi_io = .TRUE.
    1666              :                   unit_nr = cp_print_key_unit_nr(logger, cube_section, '', extension=".cube", &
    1667              :                                                  middle_name=TRIM(filename), file_position=my_pos_cube, &
    1668            8 :                                                  log_filename=.FALSE., mpi_io=mpi_io)
    1669            8 :                   WRITE (title, *) "WAVEFUNCTION ", ivector, " spin ", ispin, " i.e. LUMO + ", ivector - ifirst
    1670              :                   CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, zeff=zcharge, &
    1671            8 :                                      stride=section_get_ivals(cube_section, "STRIDE"), mpi_io=mpi_io)
    1672           12 :                   CALL cp_print_key_finished_output(unit_nr, logger, cube_section, '', mpi_io=mpi_io)
    1673              :                END DO
    1674              :             END IF
    1675              :          END DO
    1676              :       END IF
    1677              : 
    1678           24 :       CALL auxbas_pw_pool%give_back_pw(wf_g)
    1679           24 :       CALL auxbas_pw_pool%give_back_pw(wf_r)
    1680           24 :       IF (ASSOCIATED(list_index)) DEALLOCATE (list_index)
    1681              : 
    1682           24 :    END SUBROUTINE print_mo_cubes
    1683              : 
    1684              : ! **************************************************************************************************
    1685              : 
    1686              : END MODULE qs_scf_post_tb
        

Generated by: LCOV version 2.0-1