LCOV - code coverage report
Current view: top level - src - qs_scf_post_gpw.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:07c9450) Lines: 87.9 % 1543 1356
Test Date: 2025-12-13 06:52:47 Functions: 100.0 % 23 23

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Does all kind of post scf calculations for GPW/GAPW
      10              : !> \par History
      11              : !>      Started as a copy from the relevant part of qs_scf
      12              : !>      Start to adapt for k-points [07.2015, JGH]
      13              : !> \author Joost VandeVondele (10.2003)
      14              : ! **************************************************************************************************
      15              : MODULE qs_scf_post_gpw
      16              :    USE admm_types,                      ONLY: admm_type
      17              :    USE admm_utils,                      ONLY: admm_correct_for_eigenvalues,&
      18              :                                               admm_uncorrect_for_eigenvalues
      19              :    USE ai_onecenter,                    ONLY: sg_overlap
      20              :    USE atom_kind_orbitals,              ONLY: calculate_atomic_density
      21              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      22              :                                               get_atomic_kind
      23              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      24              :                                               gto_basis_set_type
      25              :    USE cell_types,                      ONLY: cell_type
      26              :    USE cp_array_utils,                  ONLY: cp_1d_r_p_type
      27              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      28              :    USE cp_control_types,                ONLY: dft_control_type
      29              :    USE cp_dbcsr_api,                    ONLY: dbcsr_add,&
      30              :                                               dbcsr_p_type,&
      31              :                                               dbcsr_type
      32              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      33              :                                               dbcsr_deallocate_matrix_set
      34              :    USE cp_dbcsr_output,                 ONLY: cp_dbcsr_write_sparse_matrix
      35              :    USE cp_ddapc_util,                   ONLY: get_ddapc
      36              :    USE cp_fm_diag,                      ONLY: choose_eigv_solver
      37              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      38              :                                               cp_fm_struct_release,&
      39              :                                               cp_fm_struct_type
      40              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      41              :                                               cp_fm_get_info,&
      42              :                                               cp_fm_init_random,&
      43              :                                               cp_fm_release,&
      44              :                                               cp_fm_to_fm,&
      45              :                                               cp_fm_type
      46              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      47              :                                               cp_logger_get_default_io_unit,&
      48              :                                               cp_logger_type,&
      49              :                                               cp_to_string
      50              :    USE cp_output_handling,              ONLY: cp_p_file,&
      51              :                                               cp_print_key_finished_output,&
      52              :                                               cp_print_key_should_output,&
      53              :                                               cp_print_key_unit_nr
      54              :    USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
      55              :    USE dct,                             ONLY: pw_shrink
      56              :    USE ed_analysis,                     ONLY: edmf_analysis
      57              :    USE eeq_method,                      ONLY: eeq_print
      58              :    USE et_coupling_types,               ONLY: set_et_coupling_type
      59              :    USE hfx_ri,                          ONLY: print_ri_hfx
      60              :    USE hirshfeld_methods,               ONLY: comp_hirshfeld_charges,&
      61              :                                               comp_hirshfeld_i_charges,&
      62              :                                               create_shape_function,&
      63              :                                               save_hirshfeld_charges,&
      64              :                                               write_hirshfeld_charges
      65              :    USE hirshfeld_types,                 ONLY: create_hirshfeld_type,&
      66              :                                               hirshfeld_type,&
      67              :                                               release_hirshfeld_type,&
      68              :                                               set_hirshfeld_info
      69              :    USE iao_analysis,                    ONLY: iao_wfn_analysis
      70              :    USE iao_types,                       ONLY: iao_env_type,&
      71              :                                               iao_read_input
      72              :    USE input_constants,                 ONLY: &
      73              :         do_loc_both, do_loc_homo, do_loc_jacobi, do_loc_lumo, do_loc_mixed, do_loc_none, &
      74              :         ot_precond_full_all, radius_covalent, radius_user, ref_charge_atomic, ref_charge_mulliken
      75              :    USE input_section_types,             ONLY: section_get_ival,&
      76              :                                               section_get_ivals,&
      77              :                                               section_get_lval,&
      78              :                                               section_get_rval,&
      79              :                                               section_vals_get,&
      80              :                                               section_vals_get_subs_vals,&
      81              :                                               section_vals_type,&
      82              :                                               section_vals_val_get
      83              :    USE kinds,                           ONLY: default_path_length,&
      84              :                                               default_string_length,&
      85              :                                               dp
      86              :    USE kpoint_types,                    ONLY: kpoint_type
      87              :    USE mao_wfn_analysis,                ONLY: mao_analysis
      88              :    USE mathconstants,                   ONLY: pi
      89              :    USE memory_utilities,                ONLY: reallocate
      90              :    USE message_passing,                 ONLY: mp_para_env_type
      91              :    USE minbas_wfn_analysis,             ONLY: minbas_analysis
      92              :    USE molden_utils,                    ONLY: write_mos_molden
      93              :    USE molecule_types,                  ONLY: molecule_type
      94              :    USE mulliken,                        ONLY: mulliken_charges
      95              :    USE orbital_pointers,                ONLY: indso
      96              :    USE particle_list_types,             ONLY: particle_list_type
      97              :    USE particle_types,                  ONLY: particle_type
      98              :    USE physcon,                         ONLY: angstrom,&
      99              :                                               evolt
     100              :    USE population_analyses,             ONLY: lowdin_population_analysis,&
     101              :                                               mulliken_population_analysis
     102              :    USE preconditioner_types,            ONLY: preconditioner_type
     103              :    USE ps_implicit_types,               ONLY: MIXED_BC,&
     104              :                                               MIXED_PERIODIC_BC,&
     105              :                                               NEUMANN_BC,&
     106              :                                               PERIODIC_BC
     107              :    USE pw_env_types,                    ONLY: pw_env_get,&
     108              :                                               pw_env_type
     109              :    USE pw_grids,                        ONLY: get_pw_grid_info
     110              :    USE pw_methods,                      ONLY: pw_axpy,&
     111              :                                               pw_copy,&
     112              :                                               pw_derive,&
     113              :                                               pw_integrate_function,&
     114              :                                               pw_scale,&
     115              :                                               pw_transfer,&
     116              :                                               pw_zero
     117              :    USE pw_poisson_methods,              ONLY: pw_poisson_solve
     118              :    USE pw_poisson_types,                ONLY: pw_poisson_implicit,&
     119              :                                               pw_poisson_type
     120              :    USE pw_pool_types,                   ONLY: pw_pool_p_type,&
     121              :                                               pw_pool_type
     122              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
     123              :                                               pw_r3d_rs_type
     124              :    USE qs_chargemol,                    ONLY: write_wfx
     125              :    USE qs_collocate_density,            ONLY: calculate_rho_resp_all,&
     126              :                                               calculate_wavefunction
     127              :    USE qs_commutators,                  ONLY: build_com_hr_matrix
     128              :    USE qs_core_energies,                ONLY: calculate_ptrace
     129              :    USE qs_dos,                          ONLY: calculate_dos,&
     130              :                                               calculate_dos_kp
     131              :    USE qs_electric_field_gradient,      ONLY: qs_efg_calc
     132              :    USE qs_elf_methods,                  ONLY: qs_elf_calc
     133              :    USE qs_energy_types,                 ONLY: qs_energy_type
     134              :    USE qs_energy_window,                ONLY: energy_windows
     135              :    USE qs_environment_types,            ONLY: get_qs_env,&
     136              :                                               qs_environment_type,&
     137              :                                               set_qs_env
     138              :    USE qs_epr_hyp,                      ONLY: qs_epr_hyp_calc
     139              :    USE qs_grid_atom,                    ONLY: grid_atom_type
     140              :    USE qs_integral_utils,               ONLY: basis_set_list_setup
     141              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
     142              :                                               qs_kind_type
     143              :    USE qs_ks_methods,                   ONLY: calc_rho_tot_gspace,&
     144              :                                               qs_ks_update_qs_env
     145              :    USE qs_ks_types,                     ONLY: qs_ks_did_change
     146              :    USE qs_loc_dipole,                   ONLY: loc_dipole
     147              :    USE qs_loc_states,                   ONLY: get_localization_info
     148              :    USE qs_loc_types,                    ONLY: qs_loc_env_create,&
     149              :                                               qs_loc_env_release,&
     150              :                                               qs_loc_env_type
     151              :    USE qs_loc_utils,                    ONLY: loc_write_restart,&
     152              :                                               qs_loc_control_init,&
     153              :                                               qs_loc_env_init,&
     154              :                                               qs_loc_init,&
     155              :                                               retain_history
     156              :    USE qs_local_properties,             ONLY: qs_local_energy,&
     157              :                                               qs_local_stress
     158              :    USE qs_mo_io,                        ONLY: write_dm_binary_restart
     159              :    USE qs_mo_methods,                   ONLY: calculate_subspace_eigenvalues,&
     160              :                                               make_mo_eig
     161              :    USE qs_mo_occupation,                ONLY: set_mo_occupation
     162              :    USE qs_mo_types,                     ONLY: get_mo_set,&
     163              :                                               mo_set_type
     164              :    USE qs_moments,                      ONLY: qs_moment_berry_phase,&
     165              :                                               qs_moment_locop
     166              :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
     167              :                                               get_neighbor_list_set_p,&
     168              :                                               neighbor_list_iterate,&
     169              :                                               neighbor_list_iterator_create,&
     170              :                                               neighbor_list_iterator_p_type,&
     171              :                                               neighbor_list_iterator_release,&
     172              :                                               neighbor_list_set_p_type
     173              :    USE qs_ot_eigensolver,               ONLY: ot_eigensolver
     174              :    USE qs_pdos,                         ONLY: calculate_projected_dos
     175              :    USE qs_resp,                         ONLY: resp_fit
     176              :    USE qs_rho0_types,                   ONLY: get_rho0_mpole,&
     177              :                                               mpole_rho_atom,&
     178              :                                               rho0_mpole_type
     179              :    USE qs_rho_atom_types,               ONLY: rho_atom_type
     180              :    USE qs_rho_methods,                  ONLY: qs_rho_update_rho
     181              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
     182              :                                               qs_rho_type
     183              :    USE qs_scf_csr_write,                ONLY: write_hcore_matrix_csr,&
     184              :                                               write_ks_matrix_csr,&
     185              :                                               write_p_matrix_csr,&
     186              :                                               write_s_matrix_csr
     187              :    USE qs_scf_output,                   ONLY: qs_scf_write_mos
     188              :    USE qs_scf_types,                    ONLY: ot_method_nr,&
     189              :                                               qs_scf_env_type
     190              :    USE qs_scf_wfn_mix,                  ONLY: wfn_mix
     191              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
     192              :                                               qs_subsys_type
     193              :    USE qs_wannier90,                    ONLY: wannier90_interface
     194              :    USE s_square_methods,                ONLY: compute_s_square
     195              :    USE scf_control_types,               ONLY: scf_control_type
     196              :    USE stm_images,                      ONLY: th_stm_image
     197              :    USE transport,                       ONLY: qs_scf_post_transport
     198              :    USE trexio_utils,                    ONLY: write_trexio
     199              :    USE virial_types,                    ONLY: virial_type
     200              :    USE voronoi_interface,               ONLY: entry_voronoi_or_bqb
     201              :    USE xray_diffraction,                ONLY: calculate_rhotot_elec_gspace,&
     202              :                                               xray_diffraction_spectrum
     203              : #include "./base/base_uses.f90"
     204              : 
     205              :    IMPLICIT NONE
     206              :    PRIVATE
     207              : 
     208              :    ! Global parameters
     209              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_post_gpw'
     210              :    PUBLIC :: scf_post_calculation_gpw, &
     211              :              qs_scf_post_moments, &
     212              :              write_mo_dependent_results, &
     213              :              write_mo_free_results
     214              : 
     215              :    PUBLIC :: make_lumo_gpw
     216              : 
     217              : CONTAINS
     218              : 
     219              : ! **************************************************************************************************
     220              : !> \brief collects possible post - scf calculations and prints info / computes properties.
     221              : !> \param qs_env the qs_env in which the qs_env lives
     222              : !> \param wf_type ...
     223              : !> \param do_mp2 ...
     224              : !> \par History
     225              : !>      02.2003 created [fawzi]
     226              : !>      10.2004 moved here from qs_scf [Joost VandeVondele]
     227              : !>              started splitting out different subroutines
     228              : !>      10.2015 added header for wave-function correlated methods [Vladimir Rybkin]
     229              : !> \author fawzi
     230              : !> \note
     231              : !>      this function changes mo_eigenvectors and mo_eigenvalues, depending on the print keys.
     232              : !>      In particular, MO_CUBES causes the MOs to be rotated to make them eigenstates of the KS
     233              : !>      matrix, and mo_eigenvalues is updated accordingly. This can, for unconverged wavefunctions,
     234              : !>      change afterwards slightly the forces (hence small numerical differences between MD
     235              : !>      with and without the debug print level). Ideally this should not happen...
     236              : ! **************************************************************************************************
     237        10053 :    SUBROUTINE scf_post_calculation_gpw(qs_env, wf_type, do_mp2)
     238              : 
     239              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     240              :       CHARACTER(6), OPTIONAL                             :: wf_type
     241              :       LOGICAL, OPTIONAL                                  :: do_mp2
     242              : 
     243              :       CHARACTER(len=*), PARAMETER :: routineN = 'scf_post_calculation_gpw'
     244              : 
     245              :       INTEGER                                            :: handle, homo, ispin, min_lumos, n_rep, &
     246              :                                                             nchk_nmoloc, nhomo, nlumo, nlumo_stm, &
     247              :                                                             nlumos, nmo, nspins, output_unit, &
     248              :                                                             unit_nr
     249        10053 :       INTEGER, DIMENSION(:, :, :), POINTER               :: marked_states
     250              :       LOGICAL :: check_write, compute_lumos, do_homo, do_kpoints, do_mixed, do_mo_cubes, do_stm, &
     251              :          do_wannier_cubes, has_homo, has_lumo, loc_explicit, loc_print_explicit, my_do_mp2, &
     252              :          my_localized_wfn, p_loc, p_loc_homo, p_loc_lumo, p_loc_mixed
     253              :       REAL(dp)                                           :: e_kin
     254              :       REAL(KIND=dp)                                      :: gap, homo_lumo(2, 2), total_zeff_corr
     255        10053 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: mo_eigenvalues
     256              :       TYPE(admm_type), POINTER                           :: admm_env
     257        10053 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     258        10053 :       TYPE(cp_1d_r_p_type), DIMENSION(:), POINTER        :: mixed_evals, occupied_evals, &
     259        10053 :                                                             unoccupied_evals, unoccupied_evals_stm
     260        10053 :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: mixed_orbs, occupied_orbs
     261              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:), &
     262        10053 :          TARGET                                          :: homo_localized, lumo_localized, &
     263        10053 :                                                             mixed_localized
     264        10053 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: lumo_ptr, mo_loc_history, &
     265        10053 :                                                             unoccupied_orbs, unoccupied_orbs_stm
     266              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     267              :       TYPE(cp_logger_type), POINTER                      :: logger
     268        10053 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_rmpv, matrix_p_mp2, matrix_s, &
     269        10053 :                                                             mo_derivs
     270        10053 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: kinetic_m, rho_ao
     271              :       TYPE(dft_control_type), POINTER                    :: dft_control
     272        10053 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     273        10053 :       TYPE(molecule_type), POINTER                       :: molecule_set(:)
     274              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     275              :       TYPE(particle_list_type), POINTER                  :: particles
     276        10053 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     277              :       TYPE(pw_c1d_gs_type)                               :: wf_g
     278              :       TYPE(pw_env_type), POINTER                         :: pw_env
     279        10053 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
     280              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     281              :       TYPE(pw_r3d_rs_type)                               :: wf_r
     282        10053 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     283              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env_homo, qs_loc_env_lumo, &
     284              :                                                             qs_loc_env_mixed
     285              :       TYPE(qs_rho_type), POINTER                         :: rho
     286              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     287              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     288              :       TYPE(scf_control_type), POINTER                    :: scf_control
     289              :       TYPE(section_vals_type), POINTER                   :: dft_section, input, loc_print_section, &
     290              :                                                             localize_section, print_key, &
     291              :                                                             stm_section
     292              : 
     293        10053 :       CALL timeset(routineN, handle)
     294              : 
     295        10053 :       logger => cp_get_default_logger()
     296        10053 :       output_unit = cp_logger_get_default_io_unit(logger)
     297              : 
     298              :       ! Print out the type of wavefunction to distinguish between SCF and post-SCF
     299        10053 :       my_do_mp2 = .FALSE.
     300        10053 :       IF (PRESENT(do_mp2)) my_do_mp2 = do_mp2
     301        10053 :       IF (PRESENT(wf_type)) THEN
     302          322 :          IF (output_unit > 0) THEN
     303          161 :             WRITE (UNIT=output_unit, FMT='(/,(T1,A))') REPEAT("-", 40)
     304          161 :             WRITE (UNIT=output_unit, FMT='(/,(T3,A,T19,A,T25,A))') "Properties from ", wf_type, " density"
     305          161 :             WRITE (UNIT=output_unit, FMT='(/,(T1,A))') REPEAT("-", 40)
     306              :          END IF
     307              :       END IF
     308              : 
     309              :       ! Writes the data that is already available in qs_env
     310        10053 :       CALL get_qs_env(qs_env, scf_env=scf_env)
     311              : 
     312        10053 :       my_localized_wfn = .FALSE.
     313        10053 :       NULLIFY (admm_env, dft_control, pw_env, auxbas_pw_pool, pw_pools, mos, rho, &
     314        10053 :                mo_coeff, ks_rmpv, matrix_s, qs_loc_env_homo, qs_loc_env_lumo, scf_control, &
     315        10053 :                unoccupied_orbs, mo_eigenvalues, unoccupied_evals, &
     316        10053 :                unoccupied_evals_stm, molecule_set, mo_derivs, &
     317        10053 :                subsys, particles, input, print_key, kinetic_m, marked_states, &
     318        10053 :                mixed_evals, qs_loc_env_mixed)
     319        10053 :       NULLIFY (lumo_ptr, rho_ao)
     320              : 
     321        10053 :       has_homo = .FALSE.
     322        10053 :       has_lumo = .FALSE.
     323        10053 :       p_loc = .FALSE.
     324        10053 :       p_loc_homo = .FALSE.
     325        10053 :       p_loc_lumo = .FALSE.
     326        10053 :       p_loc_mixed = .FALSE.
     327              : 
     328        10053 :       CPASSERT(ASSOCIATED(scf_env))
     329        10053 :       CPASSERT(ASSOCIATED(qs_env))
     330              :       ! Here we start with data that needs a postprocessing...
     331              :       CALL get_qs_env(qs_env, &
     332              :                       dft_control=dft_control, &
     333              :                       molecule_set=molecule_set, &
     334              :                       scf_control=scf_control, &
     335              :                       do_kpoints=do_kpoints, &
     336              :                       input=input, &
     337              :                       subsys=subsys, &
     338              :                       rho=rho, &
     339              :                       pw_env=pw_env, &
     340              :                       particle_set=particle_set, &
     341              :                       atomic_kind_set=atomic_kind_set, &
     342        10053 :                       qs_kind_set=qs_kind_set)
     343        10053 :       CALL qs_subsys_get(subsys, particles=particles)
     344              : 
     345        10053 :       CALL qs_rho_get(rho, rho_ao_kp=rho_ao)
     346              : 
     347        10053 :       IF (my_do_mp2) THEN
     348              :          ! Get the HF+MP2 density
     349          322 :          CALL get_qs_env(qs_env, matrix_p_mp2=matrix_p_mp2)
     350          742 :          DO ispin = 1, dft_control%nspins
     351          742 :             CALL dbcsr_add(rho_ao(ispin, 1)%matrix, matrix_p_mp2(ispin)%matrix, 1.0_dp, 1.0_dp)
     352              :          END DO
     353          322 :          CALL qs_rho_update_rho(rho, qs_env=qs_env)
     354          322 :          CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
     355              :          ! In MP2 case update the Hartree potential
     356          322 :          CALL update_hartree_with_mp2(rho, qs_env)
     357              :       END IF
     358              : 
     359        10053 :       CALL write_available_results(qs_env, scf_env)
     360              : 
     361              :       !    **** the kinetic energy
     362        10053 :       IF (cp_print_key_should_output(logger%iter_info, input, &
     363              :                                      "DFT%PRINT%KINETIC_ENERGY") /= 0) THEN
     364           80 :          CALL get_qs_env(qs_env, kinetic_kp=kinetic_m)
     365           80 :          CPASSERT(ASSOCIATED(kinetic_m))
     366           80 :          CPASSERT(ASSOCIATED(kinetic_m(1, 1)%matrix))
     367           80 :          CALL calculate_ptrace(kinetic_m, rho_ao, e_kin, dft_control%nspins)
     368              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%KINETIC_ENERGY", &
     369           80 :                                         extension=".Log")
     370           80 :          IF (unit_nr > 0) THEN
     371           40 :             WRITE (unit_nr, '(T3,A,T55,F25.14)') "Electronic kinetic energy:", e_kin
     372              :          END IF
     373              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
     374           80 :                                            "DFT%PRINT%KINETIC_ENERGY")
     375              :       END IF
     376              : 
     377              :       ! Atomic Charges that require further computation
     378        10053 :       CALL qs_scf_post_charges(input, logger, qs_env)
     379              : 
     380              :       ! Moments of charge distribution
     381        10053 :       CALL qs_scf_post_moments(input, logger, qs_env, output_unit)
     382              : 
     383              :       ! Determine if we need to computer properties using the localized centers
     384        10053 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     385        10053 :       localize_section => section_vals_get_subs_vals(dft_section, "LOCALIZE")
     386        10053 :       loc_print_section => section_vals_get_subs_vals(localize_section, "PRINT")
     387        10053 :       CALL section_vals_get(localize_section, explicit=loc_explicit)
     388        10053 :       CALL section_vals_get(loc_print_section, explicit=loc_print_explicit)
     389              : 
     390              :       ! Print_keys controlled by localization
     391        10053 :       IF (loc_print_explicit) THEN
     392           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "MOLECULAR_DIPOLES")
     393           96 :          p_loc = BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     394           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "TOTAL_DIPOLE")
     395           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     396           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "WANNIER_CENTERS")
     397           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     398           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "WANNIER_SPREADS")
     399           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     400           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "WANNIER_CUBES")
     401           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     402           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "MOLECULAR_STATES")
     403           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     404           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "MOLECULAR_MOMENTS")
     405           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     406           96 :          print_key => section_vals_get_subs_vals(loc_print_section, "WANNIER_STATES")
     407           96 :          p_loc = p_loc .OR. BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)
     408              :       ELSE
     409              :          p_loc = .FALSE.
     410              :       END IF
     411        10053 :       IF (loc_explicit) THEN
     412              :          p_loc_homo = (section_get_ival(localize_section, "STATES") == do_loc_homo .OR. &
     413           96 :                        section_get_ival(localize_section, "STATES") == do_loc_both) .AND. p_loc
     414              :          p_loc_lumo = (section_get_ival(localize_section, "STATES") == do_loc_lumo .OR. &
     415           96 :                        section_get_ival(localize_section, "STATES") == do_loc_both) .AND. p_loc
     416           96 :          p_loc_mixed = (section_get_ival(localize_section, "STATES") == do_loc_mixed) .AND. p_loc
     417           96 :          CALL section_vals_val_get(localize_section, "LIST_UNOCCUPIED", n_rep_val=n_rep)
     418              :       ELSE
     419         9957 :          p_loc_homo = .FALSE.
     420         9957 :          p_loc_lumo = .FALSE.
     421         9957 :          p_loc_mixed = .FALSE.
     422         9957 :          n_rep = 0
     423              :       END IF
     424              : 
     425        10053 :       IF (n_rep == 0 .AND. p_loc_lumo) THEN
     426              :          CALL cp_abort(__LOCATION__, "No LIST_UNOCCUPIED was specified, "// &
     427            0 :                        "therefore localization of unoccupied states will be skipped!")
     428            0 :          p_loc_lumo = .FALSE.
     429              :       END IF
     430              : 
     431              :       ! Control for STM
     432        10053 :       stm_section => section_vals_get_subs_vals(input, "DFT%PRINT%STM")
     433        10053 :       CALL section_vals_get(stm_section, explicit=do_stm)
     434        10053 :       nlumo_stm = 0
     435        10053 :       IF (do_stm) nlumo_stm = section_get_ival(stm_section, "NLUMO")
     436              : 
     437              :       ! check for CUBES (MOs and WANNIERS)
     438              :       do_mo_cubes = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, "PRINT%MO_CUBES") &
     439        10053 :                           , cp_p_file)
     440        10053 :       IF (loc_print_explicit) THEN
     441              :          do_wannier_cubes = BTEST(cp_print_key_should_output(logger%iter_info, loc_print_section, &
     442           96 :                                                              "WANNIER_CUBES"), cp_p_file)
     443              :       ELSE
     444              :          do_wannier_cubes = .FALSE.
     445              :       END IF
     446        10053 :       nlumo = section_get_ival(dft_section, "PRINT%MO_CUBES%NLUMO")
     447        10053 :       nhomo = section_get_ival(dft_section, "PRINT%MO_CUBES%NHOMO")
     448              : 
     449              :       ! Setup the grids needed to compute a wavefunction given a vector..
     450        10053 :       IF (((do_mo_cubes .OR. do_wannier_cubes) .AND. (nlumo /= 0 .OR. nhomo /= 0)) .OR. p_loc) THEN
     451              :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
     452          210 :                          pw_pools=pw_pools)
     453          210 :          CALL auxbas_pw_pool%create_pw(wf_r)
     454          210 :          CALL auxbas_pw_pool%create_pw(wf_g)
     455              :       END IF
     456              : 
     457        10053 :       IF (dft_control%restricted) THEN
     458              :          !For ROKS usefull only first term
     459           74 :          nspins = 1
     460              :       ELSE
     461         9979 :          nspins = dft_control%nspins
     462              :       END IF
     463              :       !Some info about ROKS
     464        10053 :       IF (dft_control%restricted .AND. (do_mo_cubes .OR. p_loc_homo)) THEN
     465            0 :          CALL cp_abort(__LOCATION__, "Unclear how we define MOs / localization in the restricted case ... ")
     466              :          ! It is possible to obtain Wannier centers for ROKS without rotations for SINGLE OCCUPIED ORBITALS
     467              :       END IF
     468              :       ! Makes the MOs eigenstates, computes eigenvalues, write cubes
     469        10053 :       IF (do_kpoints) THEN
     470          220 :          CPWARN_IF(do_mo_cubes, "Print MO cubes not implemented for k-point calculations")
     471              :       ELSE
     472              :          CALL get_qs_env(qs_env, &
     473              :                          mos=mos, &
     474         9833 :                          matrix_ks=ks_rmpv)
     475         9833 :          IF ((do_mo_cubes .AND. nhomo /= 0) .OR. do_stm) THEN
     476          134 :             CALL get_qs_env(qs_env, mo_derivs=mo_derivs)
     477          134 :             IF (dft_control%do_admm) THEN
     478            0 :                CALL get_qs_env(qs_env, admm_env=admm_env)
     479            0 :                CALL make_mo_eig(mos, nspins, ks_rmpv, scf_control, mo_derivs, admm_env=admm_env)
     480              :             ELSE
     481          134 :                IF (dft_control%hairy_probes) THEN
     482            0 :                   scf_control%smear%do_smear = .FALSE.
     483              :                   CALL make_mo_eig(mos, dft_control%nspins, ks_rmpv, scf_control, mo_derivs, &
     484              :                                    hairy_probes=dft_control%hairy_probes, &
     485            0 :                                    probe=dft_control%probe)
     486              :                ELSE
     487          134 :                   CALL make_mo_eig(mos, dft_control%nspins, ks_rmpv, scf_control, mo_derivs)
     488              :                END IF
     489              :             END IF
     490          288 :             DO ispin = 1, dft_control%nspins
     491          154 :                CALL get_mo_set(mo_set=mos(ispin), eigenvalues=mo_eigenvalues, homo=homo)
     492          288 :                homo_lumo(ispin, 1) = mo_eigenvalues(homo)
     493              :             END DO
     494              :             has_homo = .TRUE.
     495              :          END IF
     496         9833 :          IF (do_mo_cubes .AND. nhomo /= 0) THEN
     497          274 :             DO ispin = 1, nspins
     498              :                ! Prints the cube files of OCCUPIED ORBITALS
     499              :                CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
     500          146 :                                eigenvalues=mo_eigenvalues, homo=homo, nmo=nmo)
     501              :                CALL qs_scf_post_occ_cubes(input, dft_section, dft_control, logger, qs_env, &
     502          274 :                                           mo_coeff, wf_g, wf_r, particles, homo, ispin)
     503              :             END DO
     504              :          END IF
     505              :       END IF
     506              : 
     507              :       ! Initialize the localization environment, needed e.g. for wannier functions and molecular states
     508              :       ! Gets localization info for the occupied orbs
     509              :       !  - Possibly gets wannier functions
     510              :       !  - Possibly gets molecular states
     511        10053 :       IF (p_loc_homo) THEN
     512           90 :          IF (do_kpoints) THEN
     513            0 :             CPWARN("Localization not implemented for k-point calculations!")
     514              :          ELSEIF (dft_control%restricted &
     515              :                  .AND. (section_get_ival(localize_section, "METHOD") /= do_loc_none) &
     516           90 :                  .AND. (section_get_ival(localize_section, "METHOD") /= do_loc_jacobi)) THEN
     517            0 :             CPABORT("ROKS works only with LOCALIZE METHOD NONE or JACOBI")
     518              :          ELSE
     519          376 :             ALLOCATE (occupied_orbs(dft_control%nspins))
     520          376 :             ALLOCATE (occupied_evals(dft_control%nspins))
     521          376 :             ALLOCATE (homo_localized(dft_control%nspins))
     522          196 :             DO ispin = 1, dft_control%nspins
     523              :                CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
     524          106 :                                eigenvalues=mo_eigenvalues)
     525          106 :                occupied_orbs(ispin) = mo_coeff
     526          106 :                occupied_evals(ispin)%array => mo_eigenvalues
     527          106 :                CALL cp_fm_create(homo_localized(ispin), occupied_orbs(ispin)%matrix_struct)
     528          196 :                CALL cp_fm_to_fm(occupied_orbs(ispin), homo_localized(ispin))
     529              :             END DO
     530              : 
     531           90 :             CALL get_qs_env(qs_env, mo_loc_history=mo_loc_history)
     532           90 :             do_homo = .TRUE.
     533              : 
     534          720 :             ALLOCATE (qs_loc_env_homo)
     535           90 :             CALL qs_loc_env_create(qs_loc_env_homo)
     536           90 :             CALL qs_loc_control_init(qs_loc_env_homo, localize_section, do_homo=do_homo)
     537              :             CALL qs_loc_init(qs_env, qs_loc_env_homo, localize_section, homo_localized, do_homo, &
     538           90 :                              do_mo_cubes, mo_loc_history=mo_loc_history)
     539              :             CALL get_localization_info(qs_env, qs_loc_env_homo, localize_section, homo_localized, &
     540           90 :                                        wf_r, wf_g, particles, occupied_orbs, occupied_evals, marked_states)
     541              : 
     542              :             !retain the homo_localized for future use
     543           90 :             IF (qs_loc_env_homo%localized_wfn_control%use_history) THEN
     544           10 :                CALL retain_history(mo_loc_history, homo_localized)
     545           10 :                CALL set_qs_env(qs_env, mo_loc_history=mo_loc_history)
     546              :             END IF
     547              : 
     548              :             !write restart for localization of occupied orbitals
     549              :             CALL loc_write_restart(qs_loc_env_homo, loc_print_section, mos, &
     550           90 :                                    homo_localized, do_homo)
     551           90 :             CALL cp_fm_release(homo_localized)
     552           90 :             DEALLOCATE (occupied_orbs)
     553           90 :             DEALLOCATE (occupied_evals)
     554              :             ! Print Total Dipole if the localization has been performed
     555          180 :             IF (qs_loc_env_homo%do_localize) THEN
     556           74 :                CALL loc_dipole(input, dft_control, qs_loc_env_homo, logger, qs_env)
     557              :             END IF
     558              :          END IF
     559              :       END IF
     560              : 
     561              :       ! Gets the lumos, and eigenvalues for the lumos, and localize them if requested
     562        10053 :       IF (do_kpoints) THEN
     563          220 :          IF (do_mo_cubes .OR. p_loc_lumo) THEN
     564              :             ! nothing at the moment, not implemented
     565            2 :             CPWARN("Localization and MO related output not implemented for k-point calculations!")
     566              :          END IF
     567              :       ELSE
     568         9833 :          compute_lumos = do_mo_cubes .AND. nlumo /= 0
     569         9833 :          compute_lumos = compute_lumos .OR. p_loc_lumo
     570              : 
     571        21556 :          DO ispin = 1, dft_control%nspins
     572        11723 :             CALL get_mo_set(mo_set=mos(ispin), homo=homo, nmo=nmo)
     573        33231 :             compute_lumos = compute_lumos .AND. homo == nmo
     574              :          END DO
     575              : 
     576         9833 :          IF (do_mo_cubes .AND. .NOT. compute_lumos) THEN
     577              : 
     578           96 :             nlumo = section_get_ival(dft_section, "PRINT%MO_CUBES%NLUMO")
     579          194 :             DO ispin = 1, dft_control%nspins
     580              : 
     581           98 :                CALL get_mo_set(mo_set=mos(ispin), homo=homo, nmo=nmo, eigenvalues=mo_eigenvalues)
     582          194 :                IF (nlumo > nmo - homo) THEN
     583              :                   ! this case not yet implemented
     584              :                ELSE
     585           98 :                   IF (nlumo == -1) THEN
     586            0 :                      nlumo = nmo - homo
     587              :                   END IF
     588           98 :                   IF (output_unit > 0) WRITE (output_unit, *) " "
     589           98 :                   IF (output_unit > 0) WRITE (output_unit, *) " Lowest eigenvalues of the unoccupied subspace spin ", ispin
     590           98 :                   IF (output_unit > 0) WRITE (output_unit, *) "---------------------------------------------"
     591           98 :                   IF (output_unit > 0) WRITE (output_unit, '(4(1X,1F16.8))') mo_eigenvalues(homo + 1:homo + nlumo)
     592              : 
     593              :                   ! Prints the cube files of UNOCCUPIED ORBITALS
     594           98 :                   CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff)
     595              :                   CALL qs_scf_post_unocc_cubes(input, dft_section, dft_control, logger, qs_env, &
     596           98 :                                                mo_coeff, wf_g, wf_r, particles, nlumo, homo, ispin, lumo=homo + 1)
     597              :                END IF
     598              :             END DO
     599              : 
     600              :          END IF
     601              : 
     602         9801 :          IF (compute_lumos) THEN
     603           32 :             check_write = .TRUE.
     604           32 :             min_lumos = nlumo
     605           32 :             IF (nlumo == 0) check_write = .FALSE.
     606           32 :             IF (p_loc_lumo) THEN
     607            6 :                do_homo = .FALSE.
     608           48 :                ALLOCATE (qs_loc_env_lumo)
     609            6 :                CALL qs_loc_env_create(qs_loc_env_lumo)
     610            6 :                CALL qs_loc_control_init(qs_loc_env_lumo, localize_section, do_homo=do_homo)
     611           98 :                min_lumos = MAX(MAXVAL(qs_loc_env_lumo%localized_wfn_control%loc_states(:, :)), nlumo)
     612              :             END IF
     613              : 
     614          144 :             ALLOCATE (unoccupied_orbs(dft_control%nspins))
     615          144 :             ALLOCATE (unoccupied_evals(dft_control%nspins))
     616           32 :             CALL make_lumo_gpw(qs_env, scf_env, unoccupied_orbs, unoccupied_evals, min_lumos, nlumos)
     617           32 :             lumo_ptr => unoccupied_orbs
     618           80 :             DO ispin = 1, dft_control%nspins
     619           48 :                has_lumo = .TRUE.
     620           48 :                homo_lumo(ispin, 2) = unoccupied_evals(ispin)%array(1)
     621           48 :                CALL get_mo_set(mo_set=mos(ispin), homo=homo)
     622           80 :                IF (check_write) THEN
     623           48 :                   IF (p_loc_lumo .AND. nlumo /= -1) nlumos = MIN(nlumo, nlumos)
     624              :                   ! Prints the cube files of UNOCCUPIED ORBITALS
     625              :                   CALL qs_scf_post_unocc_cubes(input, dft_section, dft_control, logger, qs_env, &
     626           48 :                                                unoccupied_orbs(ispin), wf_g, wf_r, particles, nlumos, homo, ispin)
     627              :                END IF
     628              :             END DO
     629              : 
     630           64 :             IF (p_loc_lumo) THEN
     631           30 :                ALLOCATE (lumo_localized(dft_control%nspins))
     632           18 :                DO ispin = 1, dft_control%nspins
     633           12 :                   CALL cp_fm_create(lumo_localized(ispin), unoccupied_orbs(ispin)%matrix_struct)
     634           18 :                   CALL cp_fm_to_fm(unoccupied_orbs(ispin), lumo_localized(ispin))
     635              :                END DO
     636              :                CALL qs_loc_init(qs_env, qs_loc_env_lumo, localize_section, lumo_localized, do_homo, do_mo_cubes, &
     637            6 :                                 evals=unoccupied_evals)
     638              :                CALL qs_loc_env_init(qs_loc_env_lumo, qs_loc_env_lumo%localized_wfn_control, qs_env, &
     639            6 :                                     loc_coeff=unoccupied_orbs)
     640              :                CALL get_localization_info(qs_env, qs_loc_env_lumo, localize_section, &
     641              :                                           lumo_localized, wf_r, wf_g, particles, &
     642            6 :                                           unoccupied_orbs, unoccupied_evals, marked_states)
     643              :                CALL loc_write_restart(qs_loc_env_lumo, loc_print_section, mos, homo_localized, do_homo, &
     644            6 :                                       evals=unoccupied_evals)
     645            6 :                lumo_ptr => lumo_localized
     646              :             END IF
     647              :          END IF
     648              : 
     649         9833 :          IF (has_homo .AND. has_lumo) THEN
     650           32 :             IF (output_unit > 0) WRITE (output_unit, *) " "
     651           80 :             DO ispin = 1, dft_control%nspins
     652           80 :                IF (.NOT. scf_control%smear%do_smear) THEN
     653           48 :                   gap = homo_lumo(ispin, 2) - homo_lumo(ispin, 1)
     654           48 :                   IF (output_unit > 0) WRITE (output_unit, '(T2,A,F12.6)') &
     655           24 :                      "HOMO - LUMO gap [eV] :", gap*evolt
     656              :                END IF
     657              :             END DO
     658              :          END IF
     659              :       END IF
     660              : 
     661        10053 :       IF (p_loc_mixed) THEN
     662            2 :          IF (do_kpoints) THEN
     663            0 :             CPWARN("Localization not implemented for k-point calculations!")
     664            2 :          ELSEIF (dft_control%restricted) THEN
     665            0 :             IF (output_unit > 0) WRITE (output_unit, *) &
     666            0 :                " Unclear how we define MOs / localization in the restricted case... skipping"
     667              :          ELSE
     668              : 
     669            8 :             ALLOCATE (mixed_orbs(dft_control%nspins))
     670            8 :             ALLOCATE (mixed_evals(dft_control%nspins))
     671            8 :             ALLOCATE (mixed_localized(dft_control%nspins))
     672            4 :             DO ispin = 1, dft_control%nspins
     673              :                CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
     674            2 :                                eigenvalues=mo_eigenvalues)
     675            2 :                mixed_orbs(ispin) = mo_coeff
     676            2 :                mixed_evals(ispin)%array => mo_eigenvalues
     677            2 :                CALL cp_fm_create(mixed_localized(ispin), mixed_orbs(ispin)%matrix_struct)
     678            4 :                CALL cp_fm_to_fm(mixed_orbs(ispin), mixed_localized(ispin))
     679              :             END DO
     680              : 
     681            2 :             CALL get_qs_env(qs_env, mo_loc_history=mo_loc_history)
     682            2 :             do_homo = .FALSE.
     683            2 :             do_mixed = .TRUE.
     684            2 :             total_zeff_corr = scf_env%sum_zeff_corr
     685           16 :             ALLOCATE (qs_loc_env_mixed)
     686            2 :             CALL qs_loc_env_create(qs_loc_env_mixed)
     687            2 :             CALL qs_loc_control_init(qs_loc_env_mixed, localize_section, do_homo=do_homo, do_mixed=do_mixed)
     688              :             CALL qs_loc_init(qs_env, qs_loc_env_mixed, localize_section, mixed_localized, do_homo, &
     689              :                              do_mo_cubes, mo_loc_history=mo_loc_history, tot_zeff_corr=total_zeff_corr, &
     690            2 :                              do_mixed=do_mixed)
     691              : 
     692            4 :             DO ispin = 1, dft_control%nspins
     693            4 :                CALL cp_fm_get_info(mixed_localized(ispin), ncol_global=nchk_nmoloc)
     694              :             END DO
     695              : 
     696              :             CALL get_localization_info(qs_env, qs_loc_env_mixed, localize_section, mixed_localized, &
     697            2 :                                        wf_r, wf_g, particles, mixed_orbs, mixed_evals, marked_states)
     698              : 
     699              :             !retain the homo_localized for future use
     700            2 :             IF (qs_loc_env_mixed%localized_wfn_control%use_history) THEN
     701            0 :                CALL retain_history(mo_loc_history, mixed_localized)
     702            0 :                CALL set_qs_env(qs_env, mo_loc_history=mo_loc_history)
     703              :             END IF
     704              : 
     705              :             !write restart for localization of occupied orbitals
     706              :             CALL loc_write_restart(qs_loc_env_mixed, loc_print_section, mos, &
     707            2 :                                    mixed_localized, do_homo, do_mixed=do_mixed)
     708            2 :             CALL cp_fm_release(mixed_localized)
     709            2 :             DEALLOCATE (mixed_orbs)
     710            4 :             DEALLOCATE (mixed_evals)
     711              :             ! Print Total Dipole if the localization has been performed
     712              : ! Revisit the formalism later
     713              :             !IF (qs_loc_env_mixed%do_localize) THEN
     714              :             !   CALL loc_dipole(input, dft_control, qs_loc_env_mixed, logger, qs_env)
     715              :             !END IF
     716              :          END IF
     717              :       END IF
     718              : 
     719              :       ! Deallocate grids needed to compute wavefunctions
     720        10053 :       IF (((do_mo_cubes .OR. do_wannier_cubes) .AND. (nlumo /= 0 .OR. nhomo /= 0)) .OR. p_loc) THEN
     721          210 :          CALL auxbas_pw_pool%give_back_pw(wf_r)
     722          210 :          CALL auxbas_pw_pool%give_back_pw(wf_g)
     723              :       END IF
     724              : 
     725              :       ! Destroy the localization environment
     726        10053 :       IF (.NOT. do_kpoints) THEN
     727         9833 :          IF (p_loc_homo) THEN
     728           90 :             CALL qs_loc_env_release(qs_loc_env_homo)
     729           90 :             DEALLOCATE (qs_loc_env_homo)
     730              :          END IF
     731         9833 :          IF (p_loc_lumo) THEN
     732            6 :             CALL qs_loc_env_release(qs_loc_env_lumo)
     733            6 :             DEALLOCATE (qs_loc_env_lumo)
     734              :          END IF
     735         9833 :          IF (p_loc_mixed) THEN
     736            2 :             CALL qs_loc_env_release(qs_loc_env_mixed)
     737            2 :             DEALLOCATE (qs_loc_env_mixed)
     738              :          END IF
     739              :       END IF
     740              : 
     741              :       ! generate a mix of wfns, and write to a restart
     742        10053 :       IF (do_kpoints) THEN
     743              :          ! nothing at the moment, not implemented
     744              :       ELSE
     745         9833 :          CALL get_qs_env(qs_env, matrix_s=matrix_s, para_env=para_env)
     746              :          CALL wfn_mix(mos, particle_set, dft_section, qs_kind_set, para_env, &
     747              :                       output_unit, unoccupied_orbs=lumo_ptr, scf_env=scf_env, &
     748         9833 :                       matrix_s=matrix_s, marked_states=marked_states)
     749              : 
     750         9833 :          IF (p_loc_lumo) CALL cp_fm_release(lumo_localized)
     751              :       END IF
     752        10053 :       IF (ASSOCIATED(marked_states)) THEN
     753           16 :          DEALLOCATE (marked_states)
     754              :       END IF
     755              : 
     756              :       ! This is just a deallocation for printing MO_CUBES or TDDFPT
     757        10053 :       IF (.NOT. do_kpoints) THEN
     758         9833 :          IF (compute_lumos) THEN
     759           80 :             DO ispin = 1, dft_control%nspins
     760           48 :                DEALLOCATE (unoccupied_evals(ispin)%array)
     761           80 :                CALL cp_fm_release(unoccupied_orbs(ispin))
     762              :             END DO
     763           32 :             DEALLOCATE (unoccupied_evals)
     764           32 :             DEALLOCATE (unoccupied_orbs)
     765              :          END IF
     766              :       END IF
     767              : 
     768              :       !stm images
     769        10053 :       IF (do_stm) THEN
     770            6 :          IF (do_kpoints) THEN
     771            0 :             CPWARN("STM not implemented for k-point calculations!")
     772              :          ELSE
     773            6 :             NULLIFY (unoccupied_orbs_stm, unoccupied_evals_stm)
     774            6 :             IF (nlumo_stm > 0) THEN
     775            8 :                ALLOCATE (unoccupied_orbs_stm(dft_control%nspins))
     776            8 :                ALLOCATE (unoccupied_evals_stm(dft_control%nspins))
     777              :                CALL make_lumo_gpw(qs_env, scf_env, unoccupied_orbs_stm, unoccupied_evals_stm, &
     778            2 :                                   nlumo_stm, nlumos)
     779              :             END IF
     780              : 
     781              :             CALL th_stm_image(qs_env, stm_section, particles, unoccupied_orbs_stm, &
     782            6 :                               unoccupied_evals_stm)
     783              : 
     784            6 :             IF (nlumo_stm > 0) THEN
     785            4 :                DO ispin = 1, dft_control%nspins
     786            4 :                   DEALLOCATE (unoccupied_evals_stm(ispin)%array)
     787              :                END DO
     788            2 :                DEALLOCATE (unoccupied_evals_stm)
     789            2 :                CALL cp_fm_release(unoccupied_orbs_stm)
     790              :             END IF
     791              :          END IF
     792              :       END IF
     793              : 
     794              :       ! Print coherent X-ray diffraction spectrum
     795        10053 :       CALL qs_scf_post_xray(input, dft_section, logger, qs_env, output_unit)
     796              : 
     797              :       ! Calculation of Electric Field Gradients
     798        10053 :       CALL qs_scf_post_efg(input, logger, qs_env)
     799              : 
     800              :       ! Calculation of ET
     801        10053 :       CALL qs_scf_post_et(input, qs_env, dft_control)
     802              : 
     803              :       ! Calculation of EPR Hyperfine Coupling Tensors
     804        10053 :       CALL qs_scf_post_epr(input, logger, qs_env)
     805              : 
     806              :       ! Calculation of properties needed for BASIS_MOLOPT optimizations
     807        10053 :       CALL qs_scf_post_molopt(input, logger, qs_env)
     808              : 
     809              :       ! Calculate ELF
     810        10053 :       CALL qs_scf_post_elf(input, logger, qs_env)
     811              : 
     812              :       ! Use Wannier90 interface
     813        10053 :       CALL wannier90_interface(input, logger, qs_env)
     814              : 
     815        10053 :       IF (my_do_mp2) THEN
     816              :          ! Get everything back
     817          742 :          DO ispin = 1, dft_control%nspins
     818          742 :             CALL dbcsr_add(rho_ao(ispin, 1)%matrix, matrix_p_mp2(ispin)%matrix, 1.0_dp, -1.0_dp)
     819              :          END DO
     820          322 :          CALL qs_rho_update_rho(rho, qs_env=qs_env)
     821          322 :          CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
     822              :       END IF
     823              : 
     824        10053 :       CALL timestop(handle)
     825              : 
     826        20106 :    END SUBROUTINE scf_post_calculation_gpw
     827              : 
     828              : ! **************************************************************************************************
     829              : !> \brief Gets the lumos, and eigenvalues for the lumos
     830              : !> \param qs_env ...
     831              : !> \param scf_env ...
     832              : !> \param unoccupied_orbs ...
     833              : !> \param unoccupied_evals ...
     834              : !> \param nlumo ...
     835              : !> \param nlumos ...
     836              : ! **************************************************************************************************
     837           34 :    SUBROUTINE make_lumo_gpw(qs_env, scf_env, unoccupied_orbs, unoccupied_evals, nlumo, nlumos)
     838              : 
     839              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     840              :       TYPE(qs_scf_env_type), POINTER                     :: scf_env
     841              :       TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT)      :: unoccupied_orbs
     842              :       TYPE(cp_1d_r_p_type), DIMENSION(:), POINTER        :: unoccupied_evals
     843              :       INTEGER, INTENT(IN)                                :: nlumo
     844              :       INTEGER, INTENT(OUT)                               :: nlumos
     845              : 
     846              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'make_lumo_gpw'
     847              : 
     848              :       INTEGER                                            :: handle, homo, ispin, n, nao, nmo, &
     849              :                                                             output_unit
     850              :       TYPE(admm_type), POINTER                           :: admm_env
     851              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     852              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct_tmp
     853              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     854              :       TYPE(cp_logger_type), POINTER                      :: logger
     855           34 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_rmpv, matrix_s
     856              :       TYPE(dft_control_type), POINTER                    :: dft_control
     857           34 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     858              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     859              :       TYPE(preconditioner_type), POINTER                 :: local_preconditioner
     860              :       TYPE(scf_control_type), POINTER                    :: scf_control
     861              : 
     862           34 :       CALL timeset(routineN, handle)
     863              : 
     864           34 :       NULLIFY (mos, ks_rmpv, scf_control, dft_control, admm_env, para_env, blacs_env)
     865              :       CALL get_qs_env(qs_env, &
     866              :                       mos=mos, &
     867              :                       matrix_ks=ks_rmpv, &
     868              :                       scf_control=scf_control, &
     869              :                       dft_control=dft_control, &
     870              :                       matrix_s=matrix_s, &
     871              :                       admm_env=admm_env, &
     872              :                       para_env=para_env, &
     873           34 :                       blacs_env=blacs_env)
     874              : 
     875           34 :       logger => cp_get_default_logger()
     876           34 :       output_unit = cp_logger_get_default_io_unit(logger)
     877              : 
     878           84 :       DO ispin = 1, dft_control%nspins
     879           50 :          NULLIFY (unoccupied_evals(ispin)%array)
     880              :          ! Always write eigenvalues
     881           50 :          IF (output_unit > 0) WRITE (output_unit, *) " "
     882           50 :          IF (output_unit > 0) WRITE (output_unit, *) " Lowest Eigenvalues of the unoccupied subspace spin ", ispin
     883           50 :          IF (output_unit > 0) WRITE (output_unit, FMT='(1X,A)') "-----------------------------------------------------"
     884           50 :          CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, homo=homo, nao=nao, nmo=nmo)
     885           50 :          CALL cp_fm_get_info(mo_coeff, nrow_global=n)
     886           50 :          nlumos = MAX(1, MIN(nlumo, nao - nmo))
     887           50 :          IF (nlumo == -1) nlumos = nao - nmo
     888          150 :          ALLOCATE (unoccupied_evals(ispin)%array(nlumos))
     889              :          CALL cp_fm_struct_create(fm_struct_tmp, para_env=para_env, context=blacs_env, &
     890           50 :                                   nrow_global=n, ncol_global=nlumos)
     891           50 :          CALL cp_fm_create(unoccupied_orbs(ispin), fm_struct_tmp, name="lumos")
     892           50 :          CALL cp_fm_struct_release(fm_struct_tmp)
     893           50 :          CALL cp_fm_init_random(unoccupied_orbs(ispin), nlumos)
     894              : 
     895              :          ! the full_all preconditioner makes not much sense for lumos search
     896           50 :          NULLIFY (local_preconditioner)
     897           50 :          IF (ASSOCIATED(scf_env%ot_preconditioner)) THEN
     898           26 :             local_preconditioner => scf_env%ot_preconditioner(1)%preconditioner
     899              :             ! this one can for sure not be right (as it has to match a given C0)
     900           26 :             IF (local_preconditioner%in_use == ot_precond_full_all) THEN
     901            4 :                NULLIFY (local_preconditioner)
     902              :             END IF
     903              :          END IF
     904              : 
     905              :          ! If we do ADMM, we add have to modify the Kohn-Sham matrix
     906           50 :          IF (dft_control%do_admm) THEN
     907            0 :             CALL admm_correct_for_eigenvalues(ispin, admm_env, ks_rmpv(ispin)%matrix)
     908              :          END IF
     909              : 
     910              :          CALL ot_eigensolver(matrix_h=ks_rmpv(ispin)%matrix, matrix_s=matrix_s(1)%matrix, &
     911              :                              matrix_c_fm=unoccupied_orbs(ispin), &
     912              :                              matrix_orthogonal_space_fm=mo_coeff, &
     913              :                              eps_gradient=scf_control%eps_lumos, &
     914              :                              preconditioner=local_preconditioner, &
     915              :                              iter_max=scf_control%max_iter_lumos, &
     916           50 :                              size_ortho_space=nmo)
     917              : 
     918              :          CALL calculate_subspace_eigenvalues(unoccupied_orbs(ispin), ks_rmpv(ispin)%matrix, &
     919              :                                              unoccupied_evals(ispin)%array, scr=output_unit, &
     920           50 :                                              ionode=output_unit > 0)
     921              : 
     922              :          ! If we do ADMM, we restore the original Kohn-Sham matrix
     923          134 :          IF (dft_control%do_admm) THEN
     924            0 :             CALL admm_uncorrect_for_eigenvalues(ispin, admm_env, ks_rmpv(ispin)%matrix)
     925              :          END IF
     926              : 
     927              :       END DO
     928              : 
     929           34 :       CALL timestop(handle)
     930              : 
     931           34 :    END SUBROUTINE make_lumo_gpw
     932              : ! **************************************************************************************************
     933              : !> \brief Computes and Prints Atomic Charges with several methods
     934              : !> \param input ...
     935              : !> \param logger ...
     936              : !> \param qs_env the qs_env in which the qs_env lives
     937              : ! **************************************************************************************************
     938        10053 :    SUBROUTINE qs_scf_post_charges(input, logger, qs_env)
     939              :       TYPE(section_vals_type), POINTER                   :: input
     940              :       TYPE(cp_logger_type), POINTER                      :: logger
     941              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     942              : 
     943              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_charges'
     944              : 
     945              :       INTEGER                                            :: handle, print_level, unit_nr
     946              :       LOGICAL                                            :: do_kpoints, print_it
     947              :       TYPE(section_vals_type), POINTER                   :: density_fit_section, print_key
     948              : 
     949        10053 :       CALL timeset(routineN, handle)
     950              : 
     951        10053 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     952              : 
     953              :       ! Mulliken charges require no further computation and are printed from write_mo_free_results
     954              : 
     955              :       ! Compute the Lowdin charges
     956        10053 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%LOWDIN")
     957        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     958              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%LOWDIN", extension=".lowdin", &
     959           82 :                                         log_filename=.FALSE.)
     960           82 :          print_level = 1
     961           82 :          CALL section_vals_val_get(print_key, "PRINT_GOP", l_val=print_it)
     962           82 :          IF (print_it) print_level = 2
     963           82 :          CALL section_vals_val_get(print_key, "PRINT_ALL", l_val=print_it)
     964           82 :          IF (print_it) print_level = 3
     965           82 :          IF (do_kpoints) THEN
     966            2 :             CPWARN("Lowdin charges not implemented for k-point calculations!")
     967              :          ELSE
     968           80 :             CALL lowdin_population_analysis(qs_env, unit_nr, print_level)
     969              :          END IF
     970           82 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%LOWDIN")
     971              :       END IF
     972              : 
     973              :       ! Compute the RESP charges
     974        10053 :       CALL resp_fit(qs_env)
     975              : 
     976              :       ! Compute the Density Derived Atomic Point charges with the Bloechl scheme
     977        10053 :       print_key => section_vals_get_subs_vals(input, "PROPERTIES%FIT_CHARGE")
     978        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
     979              :          unit_nr = cp_print_key_unit_nr(logger, input, "PROPERTIES%FIT_CHARGE", extension=".Fitcharge", &
     980          102 :                                         log_filename=.FALSE.)
     981          102 :          density_fit_section => section_vals_get_subs_vals(input, "DFT%DENSITY_FITTING")
     982          102 :          CALL get_ddapc(qs_env, .FALSE., density_fit_section, iwc=unit_nr)
     983          102 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "PROPERTIES%FIT_CHARGE")
     984              :       END IF
     985              : 
     986        10053 :       CALL timestop(handle)
     987              : 
     988        10053 :    END SUBROUTINE qs_scf_post_charges
     989              : 
     990              : ! **************************************************************************************************
     991              : !> \brief Computes and prints the Cube Files for MO
     992              : !> \param input ...
     993              : !> \param dft_section ...
     994              : !> \param dft_control ...
     995              : !> \param logger ...
     996              : !> \param qs_env the qs_env in which the qs_env lives
     997              : !> \param mo_coeff ...
     998              : !> \param wf_g ...
     999              : !> \param wf_r ...
    1000              : !> \param particles ...
    1001              : !> \param homo ...
    1002              : !> \param ispin ...
    1003              : ! **************************************************************************************************
    1004          146 :    SUBROUTINE qs_scf_post_occ_cubes(input, dft_section, dft_control, logger, qs_env, &
    1005              :                                     mo_coeff, wf_g, wf_r, particles, homo, ispin)
    1006              :       TYPE(section_vals_type), POINTER                   :: input, dft_section
    1007              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1008              :       TYPE(cp_logger_type), POINTER                      :: logger
    1009              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1010              :       TYPE(cp_fm_type), INTENT(IN)                       :: mo_coeff
    1011              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                :: wf_g
    1012              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                :: wf_r
    1013              :       TYPE(particle_list_type), POINTER                  :: particles
    1014              :       INTEGER, INTENT(IN)                                :: homo, ispin
    1015              : 
    1016              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_occ_cubes'
    1017              : 
    1018              :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube, title
    1019              :       INTEGER                                            :: handle, i, ir, ivector, n_rep, nhomo, &
    1020              :                                                             nlist, unit_nr
    1021          146 :       INTEGER, DIMENSION(:), POINTER                     :: list, list_index
    1022              :       LOGICAL                                            :: append_cube, mpi_io
    1023          146 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1024              :       TYPE(cell_type), POINTER                           :: cell
    1025          146 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1026              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1027          146 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1028              : 
    1029          146 :       CALL timeset(routineN, handle)
    1030              : 
    1031          146 :       NULLIFY (list_index)
    1032              : 
    1033              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, dft_section, "PRINT%MO_CUBES") &
    1034          146 :                 , cp_p_file) .AND. section_get_lval(dft_section, "PRINT%MO_CUBES%WRITE_CUBE")) THEN
    1035          108 :          nhomo = section_get_ival(dft_section, "PRINT%MO_CUBES%NHOMO")
    1036          108 :          append_cube = section_get_lval(dft_section, "PRINT%MO_CUBES%APPEND")
    1037          108 :          my_pos_cube = "REWIND"
    1038          108 :          IF (append_cube) THEN
    1039            0 :             my_pos_cube = "APPEND"
    1040              :          END IF
    1041          108 :          CALL section_vals_val_get(dft_section, "PRINT%MO_CUBES%HOMO_LIST", n_rep_val=n_rep)
    1042          108 :          IF (n_rep > 0) THEN ! write the cubes of the list
    1043            0 :             nlist = 0
    1044            0 :             DO ir = 1, n_rep
    1045            0 :                NULLIFY (list)
    1046              :                CALL section_vals_val_get(dft_section, "PRINT%MO_CUBES%HOMO_LIST", i_rep_val=ir, &
    1047            0 :                                          i_vals=list)
    1048            0 :                IF (ASSOCIATED(list)) THEN
    1049            0 :                   CALL reallocate(list_index, 1, nlist + SIZE(list))
    1050            0 :                   DO i = 1, SIZE(list)
    1051            0 :                      list_index(i + nlist) = list(i)
    1052              :                   END DO
    1053            0 :                   nlist = nlist + SIZE(list)
    1054              :                END IF
    1055              :             END DO
    1056              :          ELSE
    1057              : 
    1058          108 :             IF (nhomo == -1) nhomo = homo
    1059          108 :             nlist = homo - MAX(1, homo - nhomo + 1) + 1
    1060          324 :             ALLOCATE (list_index(nlist))
    1061          220 :             DO i = 1, nlist
    1062          220 :                list_index(i) = MAX(1, homo - nhomo + 1) + i - 1
    1063              :             END DO
    1064              :          END IF
    1065          220 :          DO i = 1, nlist
    1066          112 :             ivector = list_index(i)
    1067              :             CALL get_qs_env(qs_env=qs_env, &
    1068              :                             atomic_kind_set=atomic_kind_set, &
    1069              :                             qs_kind_set=qs_kind_set, &
    1070              :                             cell=cell, &
    1071              :                             particle_set=particle_set, &
    1072          112 :                             pw_env=pw_env)
    1073              :             CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
    1074          112 :                                         cell, dft_control, particle_set, pw_env)
    1075          112 :             WRITE (filename, '(a4,I5.5,a1,I1.1)') "WFN_", ivector, "_", ispin
    1076          112 :             mpi_io = .TRUE.
    1077              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MO_CUBES", extension=".cube", &
    1078              :                                            middle_name=TRIM(filename), file_position=my_pos_cube, log_filename=.FALSE., &
    1079          112 :                                            mpi_io=mpi_io)
    1080          112 :             WRITE (title, *) "WAVEFUNCTION ", ivector, " spin ", ispin, " i.e. HOMO - ", ivector - homo
    1081              :             CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, &
    1082              :                                stride=section_get_ivals(dft_section, "PRINT%MO_CUBES%STRIDE"), &
    1083              :                                max_file_size_mb=section_get_rval(dft_section, "PRINT%MO_CUBES%MAX_FILE_SIZE_MB"), &
    1084          112 :                                mpi_io=mpi_io)
    1085          220 :             CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MO_CUBES", mpi_io=mpi_io)
    1086              :          END DO
    1087          108 :          IF (ASSOCIATED(list_index)) DEALLOCATE (list_index)
    1088              :       END IF
    1089              : 
    1090          146 :       CALL timestop(handle)
    1091              : 
    1092          146 :    END SUBROUTINE qs_scf_post_occ_cubes
    1093              : 
    1094              : ! **************************************************************************************************
    1095              : !> \brief Computes and prints the Cube Files for MO
    1096              : !> \param input ...
    1097              : !> \param dft_section ...
    1098              : !> \param dft_control ...
    1099              : !> \param logger ...
    1100              : !> \param qs_env the qs_env in which the qs_env lives
    1101              : !> \param unoccupied_orbs ...
    1102              : !> \param wf_g ...
    1103              : !> \param wf_r ...
    1104              : !> \param particles ...
    1105              : !> \param nlumos ...
    1106              : !> \param homo ...
    1107              : !> \param ispin ...
    1108              : !> \param lumo ...
    1109              : ! **************************************************************************************************
    1110          146 :    SUBROUTINE qs_scf_post_unocc_cubes(input, dft_section, dft_control, logger, qs_env, &
    1111              :                                       unoccupied_orbs, wf_g, wf_r, particles, nlumos, homo, ispin, lumo)
    1112              : 
    1113              :       TYPE(section_vals_type), POINTER                   :: input, dft_section
    1114              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1115              :       TYPE(cp_logger_type), POINTER                      :: logger
    1116              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1117              :       TYPE(cp_fm_type), INTENT(IN)                       :: unoccupied_orbs
    1118              :       TYPE(pw_c1d_gs_type), INTENT(INOUT)                :: wf_g
    1119              :       TYPE(pw_r3d_rs_type), INTENT(INOUT)                :: wf_r
    1120              :       TYPE(particle_list_type), POINTER                  :: particles
    1121              :       INTEGER, INTENT(IN)                                :: nlumos, homo, ispin
    1122              :       INTEGER, INTENT(IN), OPTIONAL                      :: lumo
    1123              : 
    1124              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_unocc_cubes'
    1125              : 
    1126              :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube, title
    1127              :       INTEGER                                            :: handle, ifirst, index_mo, ivector, &
    1128              :                                                             unit_nr
    1129              :       LOGICAL                                            :: append_cube, mpi_io
    1130          146 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1131              :       TYPE(cell_type), POINTER                           :: cell
    1132          146 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1133              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1134          146 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1135              : 
    1136          146 :       CALL timeset(routineN, handle)
    1137              : 
    1138              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, dft_section, "PRINT%MO_CUBES"), cp_p_file) &
    1139          146 :           .AND. section_get_lval(dft_section, "PRINT%MO_CUBES%WRITE_CUBE")) THEN
    1140          108 :          NULLIFY (qs_kind_set, particle_set, pw_env, cell)
    1141          108 :          append_cube = section_get_lval(dft_section, "PRINT%MO_CUBES%APPEND")
    1142          108 :          my_pos_cube = "REWIND"
    1143          108 :          IF (append_cube) THEN
    1144            0 :             my_pos_cube = "APPEND"
    1145              :          END IF
    1146          108 :          ifirst = 1
    1147          108 :          IF (PRESENT(lumo)) ifirst = lumo
    1148          250 :          DO ivector = ifirst, ifirst + nlumos - 1
    1149              :             CALL get_qs_env(qs_env=qs_env, &
    1150              :                             atomic_kind_set=atomic_kind_set, &
    1151              :                             qs_kind_set=qs_kind_set, &
    1152              :                             cell=cell, &
    1153              :                             particle_set=particle_set, &
    1154          142 :                             pw_env=pw_env)
    1155              :             CALL calculate_wavefunction(unoccupied_orbs, ivector, wf_r, wf_g, atomic_kind_set, &
    1156          142 :                                         qs_kind_set, cell, dft_control, particle_set, pw_env)
    1157              : 
    1158          142 :             IF (ifirst == 1) THEN
    1159          130 :                index_mo = homo + ivector
    1160              :             ELSE
    1161           12 :                index_mo = ivector
    1162              :             END IF
    1163          142 :             WRITE (filename, '(a4,I5.5,a1,I1.1)') "WFN_", index_mo, "_", ispin
    1164          142 :             mpi_io = .TRUE.
    1165              : 
    1166              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MO_CUBES", extension=".cube", &
    1167              :                                            middle_name=TRIM(filename), file_position=my_pos_cube, log_filename=.FALSE., &
    1168          142 :                                            mpi_io=mpi_io)
    1169          142 :             WRITE (title, *) "WAVEFUNCTION ", index_mo, " spin ", ispin, " i.e. LUMO + ", ifirst + ivector - 2
    1170              :             CALL cp_pw_to_cube(wf_r, unit_nr, title, particles=particles, &
    1171              :                                stride=section_get_ivals(dft_section, "PRINT%MO_CUBES%STRIDE"), &
    1172              :                                max_file_size_mb=section_get_rval(dft_section, "PRINT%MO_CUBES%MAX_FILE_SIZE_MB"), &
    1173          142 :                                mpi_io=mpi_io)
    1174          250 :             CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MO_CUBES", mpi_io=mpi_io)
    1175              :          END DO
    1176              :       END IF
    1177              : 
    1178          146 :       CALL timestop(handle)
    1179              : 
    1180          146 :    END SUBROUTINE qs_scf_post_unocc_cubes
    1181              : 
    1182              : ! **************************************************************************************************
    1183              : !> \brief Computes and prints electric moments
    1184              : !> \param input ...
    1185              : !> \param logger ...
    1186              : !> \param qs_env the qs_env in which the qs_env lives
    1187              : !> \param output_unit ...
    1188              : ! **************************************************************************************************
    1189        11239 :    SUBROUTINE qs_scf_post_moments(input, logger, qs_env, output_unit)
    1190              :       TYPE(section_vals_type), POINTER                   :: input
    1191              :       TYPE(cp_logger_type), POINTER                      :: logger
    1192              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1193              :       INTEGER, INTENT(IN)                                :: output_unit
    1194              : 
    1195              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_moments'
    1196              : 
    1197              :       CHARACTER(LEN=default_path_length)                 :: filename
    1198              :       INTEGER                                            :: handle, maxmom, reference, unit_nr
    1199              :       LOGICAL                                            :: com_nl, do_kpoints, magnetic, periodic, &
    1200              :                                                             second_ref_point, vel_reprs
    1201        11239 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: ref_point
    1202              :       TYPE(section_vals_type), POINTER                   :: print_key
    1203              : 
    1204        11239 :       CALL timeset(routineN, handle)
    1205              : 
    1206              :       print_key => section_vals_get_subs_vals(section_vals=input, &
    1207        11239 :                                               subsection_name="DFT%PRINT%MOMENTS")
    1208              : 
    1209        11239 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    1210              : 
    1211              :          maxmom = section_get_ival(section_vals=input, &
    1212         1270 :                                    keyword_name="DFT%PRINT%MOMENTS%MAX_MOMENT")
    1213              :          periodic = section_get_lval(section_vals=input, &
    1214         1270 :                                      keyword_name="DFT%PRINT%MOMENTS%PERIODIC")
    1215              :          reference = section_get_ival(section_vals=input, &
    1216         1270 :                                       keyword_name="DFT%PRINT%MOMENTS%REFERENCE")
    1217              :          magnetic = section_get_lval(section_vals=input, &
    1218         1270 :                                      keyword_name="DFT%PRINT%MOMENTS%MAGNETIC")
    1219              :          vel_reprs = section_get_lval(section_vals=input, &
    1220         1270 :                                       keyword_name="DFT%PRINT%MOMENTS%VEL_REPRS")
    1221              :          com_nl = section_get_lval(section_vals=input, &
    1222         1270 :                                    keyword_name="DFT%PRINT%MOMENTS%COM_NL")
    1223              :          second_ref_point = section_get_lval(section_vals=input, &
    1224         1270 :                                              keyword_name="DFT%PRINT%MOMENTS%SECOND_REFERENCE_POINT")
    1225              : 
    1226         1270 :          NULLIFY (ref_point)
    1227         1270 :          CALL section_vals_val_get(input, "DFT%PRINT%MOMENTS%REF_POINT", r_vals=ref_point)
    1228              :          unit_nr = cp_print_key_unit_nr(logger=logger, basis_section=input, &
    1229              :                                         print_key_path="DFT%PRINT%MOMENTS", extension=".dat", &
    1230         1270 :                                         middle_name="moments", log_filename=.FALSE.)
    1231              : 
    1232         1270 :          IF (output_unit > 0) THEN
    1233          645 :             IF (unit_nr /= output_unit) THEN
    1234           33 :                INQUIRE (UNIT=unit_nr, NAME=filename)
    1235              :                WRITE (UNIT=output_unit, FMT="(/,T2,A,2(/,T3,A),/)") &
    1236           33 :                   "MOMENTS", "The electric/magnetic moments are written to file:", &
    1237           66 :                   TRIM(filename)
    1238              :             ELSE
    1239          612 :                WRITE (UNIT=output_unit, FMT="(/,T2,A)") "ELECTRIC/MAGNETIC MOMENTS"
    1240              :             END IF
    1241              :          END IF
    1242              : 
    1243         1270 :          CALL get_qs_env(qs_env, do_kpoints=do_kpoints)
    1244              : 
    1245         1270 :          IF (do_kpoints) THEN
    1246            2 :             CPWARN("Moments not implemented for k-point calculations!")
    1247              :          ELSE
    1248         1268 :             IF (periodic) THEN
    1249          472 :                CALL qs_moment_berry_phase(qs_env, magnetic, maxmom, reference, ref_point, unit_nr)
    1250              :             ELSE
    1251          796 :                CALL qs_moment_locop(qs_env, magnetic, maxmom, reference, ref_point, unit_nr, vel_reprs, com_nl)
    1252              :             END IF
    1253              :          END IF
    1254              : 
    1255              :          CALL cp_print_key_finished_output(unit_nr=unit_nr, logger=logger, &
    1256         1270 :                                            basis_section=input, print_key_path="DFT%PRINT%MOMENTS")
    1257              : 
    1258         1270 :          IF (second_ref_point) THEN
    1259              :             reference = section_get_ival(section_vals=input, &
    1260            0 :                                          keyword_name="DFT%PRINT%MOMENTS%REFERENCE_2")
    1261              : 
    1262            0 :             NULLIFY (ref_point)
    1263            0 :             CALL section_vals_val_get(input, "DFT%PRINT%MOMENTS%REF_POINT_2", r_vals=ref_point)
    1264              :             unit_nr = cp_print_key_unit_nr(logger=logger, basis_section=input, &
    1265              :                                            print_key_path="DFT%PRINT%MOMENTS", extension=".dat", &
    1266            0 :                                            middle_name="moments_refpoint_2", log_filename=.FALSE.)
    1267              : 
    1268            0 :             IF (output_unit > 0) THEN
    1269            0 :                IF (unit_nr /= output_unit) THEN
    1270            0 :                   INQUIRE (UNIT=unit_nr, NAME=filename)
    1271              :                   WRITE (UNIT=output_unit, FMT="(/,T2,A,2(/,T3,A),/)") &
    1272            0 :                      "MOMENTS", "The electric/magnetic moments for the second reference point are written to file:", &
    1273            0 :                      TRIM(filename)
    1274              :                ELSE
    1275            0 :                   WRITE (UNIT=output_unit, FMT="(/,T2,A)") "ELECTRIC/MAGNETIC MOMENTS"
    1276              :                END IF
    1277              :             END IF
    1278            0 :             IF (do_kpoints) THEN
    1279            0 :                CPWARN("Moments not implemented for k-point calculations!")
    1280              :             ELSE
    1281            0 :                IF (periodic) THEN
    1282            0 :                   CALL qs_moment_berry_phase(qs_env, magnetic, maxmom, reference, ref_point, unit_nr)
    1283              :                ELSE
    1284            0 :                   CALL qs_moment_locop(qs_env, magnetic, maxmom, reference, ref_point, unit_nr, vel_reprs, com_nl)
    1285              :                END IF
    1286              :             END IF
    1287              :             CALL cp_print_key_finished_output(unit_nr=unit_nr, logger=logger, &
    1288            0 :                                               basis_section=input, print_key_path="DFT%PRINT%MOMENTS")
    1289              :          END IF
    1290              : 
    1291              :       END IF
    1292              : 
    1293        11239 :       CALL timestop(handle)
    1294              : 
    1295        11239 :    END SUBROUTINE qs_scf_post_moments
    1296              : 
    1297              : ! **************************************************************************************************
    1298              : !> \brief Computes and prints the X-ray diffraction spectrum.
    1299              : !> \param input ...
    1300              : !> \param dft_section ...
    1301              : !> \param logger ...
    1302              : !> \param qs_env the qs_env in which the qs_env lives
    1303              : !> \param output_unit ...
    1304              : ! **************************************************************************************************
    1305        10053 :    SUBROUTINE qs_scf_post_xray(input, dft_section, logger, qs_env, output_unit)
    1306              : 
    1307              :       TYPE(section_vals_type), POINTER                   :: input, dft_section
    1308              :       TYPE(cp_logger_type), POINTER                      :: logger
    1309              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1310              :       INTEGER, INTENT(IN)                                :: output_unit
    1311              : 
    1312              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'qs_scf_post_xray'
    1313              : 
    1314              :       CHARACTER(LEN=default_path_length)                 :: filename
    1315              :       INTEGER                                            :: handle, unit_nr
    1316              :       REAL(KIND=dp)                                      :: q_max
    1317              :       TYPE(section_vals_type), POINTER                   :: print_key
    1318              : 
    1319        10053 :       CALL timeset(routineN, handle)
    1320              : 
    1321              :       print_key => section_vals_get_subs_vals(section_vals=input, &
    1322        10053 :                                               subsection_name="DFT%PRINT%XRAY_DIFFRACTION_SPECTRUM")
    1323              : 
    1324        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    1325              :          q_max = section_get_rval(section_vals=dft_section, &
    1326           30 :                                   keyword_name="PRINT%XRAY_DIFFRACTION_SPECTRUM%Q_MAX")
    1327              :          unit_nr = cp_print_key_unit_nr(logger=logger, &
    1328              :                                         basis_section=input, &
    1329              :                                         print_key_path="DFT%PRINT%XRAY_DIFFRACTION_SPECTRUM", &
    1330              :                                         extension=".dat", &
    1331              :                                         middle_name="xrd", &
    1332           30 :                                         log_filename=.FALSE.)
    1333           30 :          IF (output_unit > 0) THEN
    1334           15 :             INQUIRE (UNIT=unit_nr, NAME=filename)
    1335              :             WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
    1336           15 :                "X-RAY DIFFRACTION SPECTRUM"
    1337           15 :             IF (unit_nr /= output_unit) THEN
    1338              :                WRITE (UNIT=output_unit, FMT="(/,T3,A,/,/,T3,A,/)") &
    1339           14 :                   "The coherent X-ray diffraction spectrum is written to the file:", &
    1340           28 :                   TRIM(filename)
    1341              :             END IF
    1342              :          END IF
    1343              :          CALL xray_diffraction_spectrum(qs_env=qs_env, &
    1344              :                                         unit_number=unit_nr, &
    1345           30 :                                         q_max=q_max)
    1346              :          CALL cp_print_key_finished_output(unit_nr=unit_nr, &
    1347              :                                            logger=logger, &
    1348              :                                            basis_section=input, &
    1349           30 :                                            print_key_path="DFT%PRINT%XRAY_DIFFRACTION_SPECTRUM")
    1350              :       END IF
    1351              : 
    1352        10053 :       CALL timestop(handle)
    1353              : 
    1354        10053 :    END SUBROUTINE qs_scf_post_xray
    1355              : 
    1356              : ! **************************************************************************************************
    1357              : !> \brief Computes and prints Electric Field Gradient
    1358              : !> \param input ...
    1359              : !> \param logger ...
    1360              : !> \param qs_env the qs_env in which the qs_env lives
    1361              : ! **************************************************************************************************
    1362        10053 :    SUBROUTINE qs_scf_post_efg(input, logger, qs_env)
    1363              :       TYPE(section_vals_type), POINTER                   :: input
    1364              :       TYPE(cp_logger_type), POINTER                      :: logger
    1365              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1366              : 
    1367              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_scf_post_efg'
    1368              : 
    1369              :       INTEGER                                            :: handle
    1370              :       TYPE(section_vals_type), POINTER                   :: print_key
    1371              : 
    1372        10053 :       CALL timeset(routineN, handle)
    1373              : 
    1374              :       print_key => section_vals_get_subs_vals(section_vals=input, &
    1375        10053 :                                               subsection_name="DFT%PRINT%ELECTRIC_FIELD_GRADIENT")
    1376        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), &
    1377              :                 cp_p_file)) THEN
    1378           30 :          CALL qs_efg_calc(qs_env=qs_env)
    1379              :       END IF
    1380              : 
    1381        10053 :       CALL timestop(handle)
    1382              : 
    1383        10053 :    END SUBROUTINE qs_scf_post_efg
    1384              : 
    1385              : ! **************************************************************************************************
    1386              : !> \brief Computes the Electron Transfer Coupling matrix element
    1387              : !> \param input ...
    1388              : !> \param qs_env the qs_env in which the qs_env lives
    1389              : !> \param dft_control ...
    1390              : ! **************************************************************************************************
    1391        20106 :    SUBROUTINE qs_scf_post_et(input, qs_env, dft_control)
    1392              :       TYPE(section_vals_type), POINTER                   :: input
    1393              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1394              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1395              : 
    1396              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_scf_post_et'
    1397              : 
    1398              :       INTEGER                                            :: handle, ispin
    1399              :       LOGICAL                                            :: do_et
    1400        10053 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: my_mos
    1401              :       TYPE(section_vals_type), POINTER                   :: et_section
    1402              : 
    1403        10053 :       CALL timeset(routineN, handle)
    1404              : 
    1405              :       do_et = .FALSE.
    1406        10053 :       et_section => section_vals_get_subs_vals(input, "PROPERTIES%ET_COUPLING")
    1407        10053 :       CALL section_vals_get(et_section, explicit=do_et)
    1408        10053 :       IF (do_et) THEN
    1409           10 :          IF (qs_env%et_coupling%first_run) THEN
    1410           10 :             NULLIFY (my_mos)
    1411           50 :             ALLOCATE (my_mos(dft_control%nspins))
    1412           50 :             ALLOCATE (qs_env%et_coupling%et_mo_coeff(dft_control%nspins))
    1413           30 :             DO ispin = 1, dft_control%nspins
    1414              :                CALL cp_fm_create(matrix=my_mos(ispin), &
    1415              :                                  matrix_struct=qs_env%mos(ispin)%mo_coeff%matrix_struct, &
    1416           20 :                                  name="FIRST_RUN_COEFF"//TRIM(ADJUSTL(cp_to_string(ispin)))//"MATRIX")
    1417              :                CALL cp_fm_to_fm(qs_env%mos(ispin)%mo_coeff, &
    1418           30 :                                 my_mos(ispin))
    1419              :             END DO
    1420           10 :             CALL set_et_coupling_type(qs_env%et_coupling, et_mo_coeff=my_mos)
    1421           10 :             DEALLOCATE (my_mos)
    1422              :          END IF
    1423              :       END IF
    1424              : 
    1425        10053 :       CALL timestop(handle)
    1426              : 
    1427        10053 :    END SUBROUTINE qs_scf_post_et
    1428              : 
    1429              : ! **************************************************************************************************
    1430              : !> \brief compute the electron localization function
    1431              : !>
    1432              : !> \param input ...
    1433              : !> \param logger ...
    1434              : !> \param qs_env ...
    1435              : !> \par History
    1436              : !>      2012-07 Created [MI]
    1437              : ! **************************************************************************************************
    1438        10053 :    SUBROUTINE qs_scf_post_elf(input, logger, qs_env)
    1439              :       TYPE(section_vals_type), POINTER                   :: input
    1440              :       TYPE(cp_logger_type), POINTER                      :: logger
    1441              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1442              : 
    1443              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_scf_post_elf'
    1444              : 
    1445              :       CHARACTER(LEN=default_path_length)                 :: filename, mpi_filename, my_pos_cube, &
    1446              :                                                             title
    1447              :       INTEGER                                            :: handle, ispin, output_unit, unit_nr
    1448              :       LOGICAL                                            :: append_cube, gapw, mpi_io
    1449              :       REAL(dp)                                           :: rho_cutoff
    1450              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1451              :       TYPE(particle_list_type), POINTER                  :: particles
    1452              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1453        10053 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1454              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1455        10053 :       TYPE(pw_r3d_rs_type), ALLOCATABLE, DIMENSION(:)    :: elf_r
    1456              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1457              :       TYPE(section_vals_type), POINTER                   :: elf_section
    1458              : 
    1459        10053 :       CALL timeset(routineN, handle)
    1460        10053 :       output_unit = cp_logger_get_default_io_unit(logger)
    1461              : 
    1462        10053 :       elf_section => section_vals_get_subs_vals(input, "DFT%PRINT%ELF_CUBE")
    1463        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    1464              :                                            "DFT%PRINT%ELF_CUBE"), cp_p_file)) THEN
    1465              : 
    1466           80 :          NULLIFY (dft_control, pw_env, auxbas_pw_pool, pw_pools, particles, subsys)
    1467           80 :          CALL get_qs_env(qs_env, dft_control=dft_control, pw_env=pw_env, subsys=subsys)
    1468           80 :          CALL qs_subsys_get(subsys, particles=particles)
    1469              : 
    1470           80 :          gapw = dft_control%qs_control%gapw
    1471           80 :          IF (.NOT. gapw) THEN
    1472              :             ! allocate
    1473          322 :             ALLOCATE (elf_r(dft_control%nspins))
    1474              :             CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
    1475           80 :                             pw_pools=pw_pools)
    1476          162 :             DO ispin = 1, dft_control%nspins
    1477           82 :                CALL auxbas_pw_pool%create_pw(elf_r(ispin))
    1478          162 :                CALL pw_zero(elf_r(ispin))
    1479              :             END DO
    1480              : 
    1481           80 :             IF (output_unit > 0) THEN
    1482              :                WRITE (UNIT=output_unit, FMT="(/,T15,A,/)") &
    1483           40 :                   " ----- ELF is computed on the real space grid -----"
    1484              :             END IF
    1485           80 :             rho_cutoff = section_get_rval(elf_section, "density_cutoff")
    1486           80 :             CALL qs_elf_calc(qs_env, elf_r, rho_cutoff)
    1487              : 
    1488              :             ! write ELF into cube file
    1489           80 :             append_cube = section_get_lval(elf_section, "APPEND")
    1490           80 :             my_pos_cube = "REWIND"
    1491           80 :             IF (append_cube) THEN
    1492            0 :                my_pos_cube = "APPEND"
    1493              :             END IF
    1494              : 
    1495          162 :             DO ispin = 1, dft_control%nspins
    1496           82 :                WRITE (filename, '(a5,I1.1)') "ELF_S", ispin
    1497           82 :                WRITE (title, *) "ELF spin ", ispin
    1498           82 :                mpi_io = .TRUE.
    1499              :                unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%ELF_CUBE", extension=".cube", &
    1500              :                                               middle_name=TRIM(filename), file_position=my_pos_cube, log_filename=.FALSE., &
    1501           82 :                                               mpi_io=mpi_io, fout=mpi_filename)
    1502           82 :                IF (output_unit > 0) THEN
    1503           41 :                   IF (.NOT. mpi_io) THEN
    1504            0 :                      INQUIRE (UNIT=unit_nr, NAME=filename)
    1505              :                   ELSE
    1506           41 :                      filename = mpi_filename
    1507              :                   END IF
    1508              :                   WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    1509           41 :                      "ELF is written in cube file format to the file:", &
    1510           82 :                      TRIM(filename)
    1511              :                END IF
    1512              : 
    1513              :                CALL cp_pw_to_cube(elf_r(ispin), unit_nr, title, particles=particles, &
    1514           82 :                                   stride=section_get_ivals(elf_section, "STRIDE"), mpi_io=mpi_io)
    1515           82 :                CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%ELF_CUBE", mpi_io=mpi_io)
    1516              : 
    1517          162 :                CALL auxbas_pw_pool%give_back_pw(elf_r(ispin))
    1518              :             END DO
    1519              : 
    1520              :             ! deallocate
    1521           80 :             DEALLOCATE (elf_r)
    1522              : 
    1523              :          ELSE
    1524              :             ! not implemented
    1525            0 :             CPWARN("ELF not implemented for GAPW calculations!")
    1526              :          END IF
    1527              : 
    1528              :       END IF ! print key
    1529              : 
    1530        10053 :       CALL timestop(handle)
    1531              : 
    1532        20106 :    END SUBROUTINE qs_scf_post_elf
    1533              : 
    1534              : ! **************************************************************************************************
    1535              : !> \brief computes the condition number of the overlap matrix and
    1536              : !>      prints the value of the total energy. This is needed
    1537              : !>      for BASIS_MOLOPT optimizations
    1538              : !> \param input ...
    1539              : !> \param logger ...
    1540              : !> \param qs_env the qs_env in which the qs_env lives
    1541              : !> \par History
    1542              : !>      2007-07 Created [Joost VandeVondele]
    1543              : ! **************************************************************************************************
    1544        10053 :    SUBROUTINE qs_scf_post_molopt(input, logger, qs_env)
    1545              :       TYPE(section_vals_type), POINTER                   :: input
    1546              :       TYPE(cp_logger_type), POINTER                      :: logger
    1547              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1548              : 
    1549              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_molopt'
    1550              : 
    1551              :       INTEGER                                            :: handle, nao, unit_nr
    1552              :       REAL(KIND=dp)                                      :: S_cond_number
    1553        10053 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: eigenvalues
    1554              :       TYPE(cp_fm_struct_type), POINTER                   :: ao_ao_fmstruct
    1555              :       TYPE(cp_fm_type)                                   :: fm_s, fm_work
    1556              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1557        10053 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
    1558        10053 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1559              :       TYPE(qs_energy_type), POINTER                      :: energy
    1560              :       TYPE(section_vals_type), POINTER                   :: print_key
    1561              : 
    1562        10053 :       CALL timeset(routineN, handle)
    1563              : 
    1564              :       print_key => section_vals_get_subs_vals(section_vals=input, &
    1565        10053 :                                               subsection_name="DFT%PRINT%BASIS_MOLOPT_QUANTITIES")
    1566        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), &
    1567              :                 cp_p_file)) THEN
    1568              : 
    1569           28 :          CALL get_qs_env(qs_env, energy=energy, matrix_s=matrix_s, mos=mos)
    1570              : 
    1571              :          ! set up the two needed full matrices, using mo_coeff as a template
    1572           28 :          CALL get_mo_set(mo_set=mos(1), mo_coeff=mo_coeff, nao=nao)
    1573              :          CALL cp_fm_struct_create(fmstruct=ao_ao_fmstruct, &
    1574              :                                   nrow_global=nao, ncol_global=nao, &
    1575           28 :                                   template_fmstruct=mo_coeff%matrix_struct)
    1576              :          CALL cp_fm_create(fm_s, matrix_struct=ao_ao_fmstruct, &
    1577           28 :                            name="fm_s")
    1578              :          CALL cp_fm_create(fm_work, matrix_struct=ao_ao_fmstruct, &
    1579           28 :                            name="fm_work")
    1580           28 :          CALL cp_fm_struct_release(ao_ao_fmstruct)
    1581           84 :          ALLOCATE (eigenvalues(nao))
    1582              : 
    1583           28 :          CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, fm_s)
    1584           28 :          CALL choose_eigv_solver(fm_s, fm_work, eigenvalues)
    1585              : 
    1586           28 :          CALL cp_fm_release(fm_s)
    1587           28 :          CALL cp_fm_release(fm_work)
    1588              : 
    1589         1048 :          S_cond_number = MAXVAL(ABS(eigenvalues))/MAX(MINVAL(ABS(eigenvalues)), EPSILON(0.0_dp))
    1590              : 
    1591              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%BASIS_MOLOPT_QUANTITIES", &
    1592           28 :                                         extension=".molopt")
    1593              : 
    1594           28 :          IF (unit_nr > 0) THEN
    1595              :             ! please keep this format fixed, needs to be grepable for molopt
    1596              :             ! optimizations
    1597           14 :             WRITE (unit_nr, '(T2,A28,2A25)') "", "Tot. Ener.", "S Cond. Numb."
    1598           14 :             WRITE (unit_nr, '(T2,A28,2E25.17)') "BASIS_MOLOPT_QUANTITIES", energy%total, S_cond_number
    1599              :          END IF
    1600              : 
    1601              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    1602           84 :                                            "DFT%PRINT%BASIS_MOLOPT_QUANTITIES")
    1603              : 
    1604              :       END IF
    1605              : 
    1606        10053 :       CALL timestop(handle)
    1607              : 
    1608        20106 :    END SUBROUTINE qs_scf_post_molopt
    1609              : 
    1610              : ! **************************************************************************************************
    1611              : !> \brief Dumps EPR
    1612              : !> \param input ...
    1613              : !> \param logger ...
    1614              : !> \param qs_env the qs_env in which the qs_env lives
    1615              : ! **************************************************************************************************
    1616        10053 :    SUBROUTINE qs_scf_post_epr(input, logger, qs_env)
    1617              :       TYPE(section_vals_type), POINTER                   :: input
    1618              :       TYPE(cp_logger_type), POINTER                      :: logger
    1619              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1620              : 
    1621              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_scf_post_epr'
    1622              : 
    1623              :       INTEGER                                            :: handle
    1624              :       TYPE(section_vals_type), POINTER                   :: print_key
    1625              : 
    1626        10053 :       CALL timeset(routineN, handle)
    1627              : 
    1628              :       print_key => section_vals_get_subs_vals(section_vals=input, &
    1629        10053 :                                               subsection_name="DFT%PRINT%HYPERFINE_COUPLING_TENSOR")
    1630        10053 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), &
    1631              :                 cp_p_file)) THEN
    1632           30 :          CALL qs_epr_hyp_calc(qs_env=qs_env)
    1633              :       END IF
    1634              : 
    1635        10053 :       CALL timestop(handle)
    1636              : 
    1637        10053 :    END SUBROUTINE qs_scf_post_epr
    1638              : 
    1639              : ! **************************************************************************************************
    1640              : !> \brief Interface routine to trigger writing of results available from normal
    1641              : !>        SCF. Can write MO-dependent and MO free results (needed for call from
    1642              : !>        the linear scaling code)
    1643              : !> \param qs_env the qs_env in which the qs_env lives
    1644              : !> \param scf_env ...
    1645              : ! **************************************************************************************************
    1646        10053 :    SUBROUTINE write_available_results(qs_env, scf_env)
    1647              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1648              :       TYPE(qs_scf_env_type), OPTIONAL, POINTER           :: scf_env
    1649              : 
    1650              :       CHARACTER(len=*), PARAMETER :: routineN = 'write_available_results'
    1651              : 
    1652              :       INTEGER                                            :: handle
    1653              : 
    1654        10053 :       CALL timeset(routineN, handle)
    1655              : 
    1656              :       ! those properties that require MOs (not suitable density matrix based methods)
    1657        10053 :       CALL write_mo_dependent_results(qs_env, scf_env)
    1658              : 
    1659              :       ! those that depend only on the density matrix, they should be linear scaling in their implementation
    1660        10053 :       CALL write_mo_free_results(qs_env)
    1661              : 
    1662        10053 :       CALL timestop(handle)
    1663              : 
    1664        10053 :    END SUBROUTINE write_available_results
    1665              : 
    1666              : ! **************************************************************************************************
    1667              : !> \brief Write QS results available if MO's are present (if switched on through the print_keys)
    1668              : !>        Writes only MO dependent results. Split is necessary as ls_scf does not
    1669              : !>        provide MO's
    1670              : !> \param qs_env the qs_env in which the qs_env lives
    1671              : !> \param scf_env ...
    1672              : ! **************************************************************************************************
    1673        10365 :    SUBROUTINE write_mo_dependent_results(qs_env, scf_env)
    1674              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1675              :       TYPE(qs_scf_env_type), OPTIONAL, POINTER           :: scf_env
    1676              : 
    1677              :       CHARACTER(len=*), PARAMETER :: routineN = 'write_mo_dependent_results'
    1678              : 
    1679              :       INTEGER                                            :: handle, homo, ispin, nmo, output_unit
    1680              :       LOGICAL                                            :: all_equal, do_kpoints, explicit
    1681              :       REAL(KIND=dp)                                      :: maxocc, s_square, s_square_ideal, &
    1682              :                                                             total_abs_spin_dens, total_spin_dens
    1683        10365 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: mo_eigenvalues, occupation_numbers
    1684              :       TYPE(admm_type), POINTER                           :: admm_env
    1685        10365 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1686              :       TYPE(cell_type), POINTER                           :: cell
    1687              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
    1688              :       TYPE(cp_logger_type), POINTER                      :: logger
    1689        10365 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: ks_rmpv, matrix_s
    1690              :       TYPE(dbcsr_type), POINTER                          :: mo_coeff_deriv
    1691              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1692        10365 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1693        10365 :       TYPE(molecule_type), POINTER                       :: molecule_set(:)
    1694              :       TYPE(particle_list_type), POINTER                  :: particles
    1695        10365 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1696              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1697        10365 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1698              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1699              :       TYPE(pw_r3d_rs_type)                               :: wf_r
    1700        10365 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r
    1701              :       TYPE(qs_energy_type), POINTER                      :: energy
    1702        10365 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1703              :       TYPE(qs_rho_type), POINTER                         :: rho
    1704              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1705              :       TYPE(scf_control_type), POINTER                    :: scf_control
    1706              :       TYPE(section_vals_type), POINTER                   :: dft_section, input, sprint_section, &
    1707              :                                                             trexio_section
    1708              : 
    1709              : ! TYPE(kpoint_type), POINTER                         :: kpoints
    1710              : 
    1711        10365 :       CALL timeset(routineN, handle)
    1712              : 
    1713        10365 :       NULLIFY (cell, dft_control, pw_env, auxbas_pw_pool, pw_pools, mo_coeff, &
    1714        10365 :                mo_coeff_deriv, mo_eigenvalues, mos, atomic_kind_set, qs_kind_set, &
    1715        10365 :                particle_set, rho, ks_rmpv, matrix_s, scf_control, dft_section, &
    1716        10365 :                molecule_set, input, particles, subsys, rho_r)
    1717              : 
    1718        10365 :       logger => cp_get_default_logger()
    1719        10365 :       output_unit = cp_logger_get_default_io_unit(logger)
    1720              : 
    1721        10365 :       CPASSERT(ASSOCIATED(qs_env))
    1722              :       CALL get_qs_env(qs_env, &
    1723              :                       dft_control=dft_control, &
    1724              :                       molecule_set=molecule_set, &
    1725              :                       atomic_kind_set=atomic_kind_set, &
    1726              :                       particle_set=particle_set, &
    1727              :                       qs_kind_set=qs_kind_set, &
    1728              :                       admm_env=admm_env, &
    1729              :                       scf_control=scf_control, &
    1730              :                       input=input, &
    1731              :                       cell=cell, &
    1732        10365 :                       subsys=subsys)
    1733        10365 :       CALL qs_subsys_get(subsys, particles=particles)
    1734        10365 :       CALL get_qs_env(qs_env, rho=rho)
    1735        10365 :       CALL qs_rho_get(rho, rho_r=rho_r)
    1736              : 
    1737              :       ! k points
    1738        10365 :       CALL get_qs_env(qs_env, do_kpoints=do_kpoints)
    1739              : 
    1740              :       ! Write last MO information to output file if requested
    1741        10365 :       dft_section => section_vals_get_subs_vals(input, "DFT")
    1742        10365 :       IF (.NOT. qs_env%run_rtp) THEN
    1743        10053 :          CALL qs_scf_write_mos(qs_env, scf_env, final_mos=.TRUE.)
    1744        10053 :          trexio_section => section_vals_get_subs_vals(dft_section, "PRINT%TREXIO")
    1745        10053 :          CALL section_vals_get(trexio_section, explicit=explicit)
    1746        10053 :          IF (explicit) THEN
    1747            8 :             CALL write_trexio(qs_env, trexio_section)
    1748              :          END IF
    1749        10053 :          IF (.NOT. do_kpoints) THEN
    1750         9833 :             CALL get_qs_env(qs_env, mos=mos, matrix_ks=ks_rmpv)
    1751         9833 :             CALL write_dm_binary_restart(mos, dft_section, ks_rmpv)
    1752         9833 :             sprint_section => section_vals_get_subs_vals(dft_section, "PRINT%MO_MOLDEN")
    1753         9833 :             CALL write_mos_molden(mos, qs_kind_set, particle_set, sprint_section)
    1754              :             ! Write Chargemol .wfx
    1755         9833 :             IF (BTEST(cp_print_key_should_output(logger%iter_info, &
    1756              :                                                  dft_section, "PRINT%CHARGEMOL"), &
    1757              :                       cp_p_file)) THEN
    1758            2 :                CALL write_wfx(qs_env, dft_section)
    1759              :             END IF
    1760              :          END IF
    1761              : 
    1762              :          ! DOS printout after the SCF cycle is completed
    1763        10053 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, dft_section, "PRINT%DOS") &
    1764              :                    , cp_p_file)) THEN
    1765           42 :             IF (do_kpoints) THEN
    1766            2 :                CALL calculate_dos_kp(qs_env, dft_section)
    1767              :             ELSE
    1768           40 :                CALL get_qs_env(qs_env, mos=mos)
    1769           40 :                CALL calculate_dos(mos, dft_section)
    1770              :             END IF
    1771              :          END IF
    1772              : 
    1773              :          ! Print the projected density of states (pDOS) for each atomic kind
    1774        10053 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, dft_section, "PRINT%PDOS"), &
    1775              :                    cp_p_file)) THEN
    1776           48 :             IF (do_kpoints) THEN
    1777            0 :                CPWARN("Projected density of states (pDOS) is not implemented for k points")
    1778              :             ELSE
    1779              :                CALL get_qs_env(qs_env, &
    1780              :                                mos=mos, &
    1781           48 :                                matrix_ks=ks_rmpv)
    1782           96 :                DO ispin = 1, dft_control%nspins
    1783              :                   ! With ADMM, we have to modify the Kohn-Sham matrix
    1784           48 :                   IF (dft_control%do_admm) THEN
    1785            0 :                      CALL admm_correct_for_eigenvalues(ispin, admm_env, ks_rmpv(ispin)%matrix)
    1786              :                   END IF
    1787           48 :                   IF (PRESENT(scf_env)) THEN
    1788           48 :                      IF (scf_env%method == ot_method_nr) THEN
    1789              :                         CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, &
    1790            8 :                                         eigenvalues=mo_eigenvalues)
    1791            8 :                         IF (ASSOCIATED(qs_env%mo_derivs)) THEN
    1792            8 :                            mo_coeff_deriv => qs_env%mo_derivs(ispin)%matrix
    1793              :                         ELSE
    1794            0 :                            mo_coeff_deriv => NULL()
    1795              :                         END IF
    1796              :                         CALL calculate_subspace_eigenvalues(mo_coeff, ks_rmpv(ispin)%matrix, mo_eigenvalues, &
    1797              :                                                             do_rotation=.TRUE., &
    1798            8 :                                                             co_rotate_dbcsr=mo_coeff_deriv)
    1799            8 :                         CALL set_mo_occupation(mo_set=mos(ispin))
    1800              :                      END IF
    1801              :                   END IF
    1802           48 :                   IF (dft_control%nspins == 2) THEN
    1803              :                      CALL calculate_projected_dos(mos(ispin), atomic_kind_set, &
    1804            0 :                                                   qs_kind_set, particle_set, qs_env, dft_section, ispin=ispin)
    1805              :                   ELSE
    1806              :                      CALL calculate_projected_dos(mos(ispin), atomic_kind_set, &
    1807           48 :                                                   qs_kind_set, particle_set, qs_env, dft_section)
    1808              :                   END IF
    1809              :                   ! With ADMM, we have to undo the modification of the Kohn-Sham matrix
    1810           96 :                   IF (dft_control%do_admm) THEN
    1811            0 :                      CALL admm_uncorrect_for_eigenvalues(ispin, admm_env, ks_rmpv(ispin)%matrix)
    1812              :                   END IF
    1813              :                END DO
    1814              :             END IF
    1815              :          END IF
    1816              :       END IF
    1817              : 
    1818              :       ! Integrated absolute spin density and spin contamination ***
    1819        10365 :       IF (dft_control%nspins == 2) THEN
    1820         1982 :          CALL get_qs_env(qs_env, mos=mos)
    1821         1982 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    1822              :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
    1823         1982 :                          pw_pools=pw_pools)
    1824         1982 :          CALL auxbas_pw_pool%create_pw(wf_r)
    1825         1982 :          CALL pw_copy(rho_r(1), wf_r)
    1826         1982 :          CALL pw_axpy(rho_r(2), wf_r, alpha=-1._dp)
    1827         1982 :          total_spin_dens = pw_integrate_function(wf_r)
    1828         1982 :          IF (output_unit > 0) WRITE (UNIT=output_unit, FMT='(/,(T3,A,T61,F20.10))') &
    1829         1014 :             "Integrated spin density: ", total_spin_dens
    1830         1982 :          total_abs_spin_dens = pw_integrate_function(wf_r, oprt="ABS")
    1831         1982 :          IF (output_unit > 0) WRITE (UNIT=output_unit, FMT='((T3,A,T61,F20.10))') &
    1832         1014 :             "Integrated absolute spin density: ", total_abs_spin_dens
    1833         1982 :          CALL auxbas_pw_pool%give_back_pw(wf_r)
    1834              :          !
    1835              :          ! XXX Fix Me XXX
    1836              :          ! should be extended to the case where added MOs are present
    1837              :          ! should be extended to the k-point case
    1838              :          !
    1839         1982 :          IF (do_kpoints) THEN
    1840           30 :             CPWARN("Spin contamination estimate not implemented for k-points.")
    1841              :          ELSE
    1842         1952 :             all_equal = .TRUE.
    1843         5856 :             DO ispin = 1, dft_control%nspins
    1844              :                CALL get_mo_set(mo_set=mos(ispin), &
    1845              :                                occupation_numbers=occupation_numbers, &
    1846              :                                homo=homo, &
    1847              :                                nmo=nmo, &
    1848         3904 :                                maxocc=maxocc)
    1849         5856 :                IF (nmo > 0) THEN
    1850              :                   all_equal = all_equal .AND. &
    1851              :                               (ALL(occupation_numbers(1:homo) == maxocc) .AND. &
    1852        22302 :                                ALL(occupation_numbers(homo + 1:nmo) == 0.0_dp))
    1853              :                END IF
    1854              :             END DO
    1855         1952 :             IF (.NOT. all_equal) THEN
    1856          106 :                IF (output_unit > 0) WRITE (UNIT=output_unit, FMT="(T3,A)") &
    1857           53 :                   "WARNING: S**2 computation does not yet treat fractional occupied orbitals"
    1858              :             ELSE
    1859              :                CALL get_qs_env(qs_env=qs_env, &
    1860              :                                matrix_s=matrix_s, &
    1861         1846 :                                energy=energy)
    1862              :                CALL compute_s_square(mos=mos, matrix_s=matrix_s, s_square=s_square, &
    1863         1846 :                                      s_square_ideal=s_square_ideal)
    1864         1846 :                IF (output_unit > 0) WRITE (UNIT=output_unit, FMT='(T3,A,T51,2F15.6)') &
    1865          946 :                   "Ideal and single determinant S**2 : ", s_square_ideal, s_square
    1866         1846 :                energy%s_square = s_square
    1867              :             END IF
    1868              :          END IF
    1869              :       END IF
    1870              : 
    1871        10365 :       CALL timestop(handle)
    1872              : 
    1873        10365 :    END SUBROUTINE write_mo_dependent_results
    1874              : 
    1875              : ! **************************************************************************************************
    1876              : !> \brief Write QS results always available (if switched on through the print_keys)
    1877              : !>        Can be called from ls_scf
    1878              : !> \param qs_env the qs_env in which the qs_env lives
    1879              : ! **************************************************************************************************
    1880        11299 :    SUBROUTINE write_mo_free_results(qs_env)
    1881              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1882              : 
    1883              :       CHARACTER(len=*), PARAMETER :: routineN = 'write_mo_free_results'
    1884              :       CHARACTER(len=1), DIMENSION(3), PARAMETER          :: cdir = ["x", "y", "z"]
    1885              : 
    1886              :       CHARACTER(LEN=2)                                   :: element_symbol
    1887              :       CHARACTER(LEN=default_path_length)                 :: filename, mpi_filename, my_pos_cube, &
    1888              :                                                             my_pos_voro
    1889              :       CHARACTER(LEN=default_string_length)               :: name, print_density
    1890              :       INTEGER :: after, handle, i, iat, iatom, id, ikind, img, iso, ispin, iw, l, n_rep_hf, nat, &
    1891              :          natom, nd(3), ngto, niso, nkind, np, nr, output_unit, print_level, should_print_bqb, &
    1892              :          should_print_voro, unit_nr, unit_nr_voro
    1893              :       LOGICAL :: append_cube, append_voro, do_hfx, do_kpoints, mpi_io, omit_headers, print_it, &
    1894              :          rho_r_valid, voro_print_txt, write_ks, write_xc, xrd_interface
    1895              :       REAL(KIND=dp)                                      :: norm_factor, q_max, rho_hard, rho_soft, &
    1896              :                                                             rho_total, rho_total_rspace, udvol, &
    1897              :                                                             volume, zeff
    1898        11299 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: zcharge
    1899        11299 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: bfun
    1900        11299 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)     :: aedens, ccdens, ppdens
    1901              :       REAL(KIND=dp), DIMENSION(3)                        :: dr
    1902        11299 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: my_Q0
    1903        11299 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1904              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
    1905              :       TYPE(cell_type), POINTER                           :: cell
    1906              :       TYPE(cp_logger_type), POINTER                      :: logger
    1907        11299 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_hr
    1908        11299 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: ks_rmpv, matrix_vxc, rho_ao
    1909              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1910              :       TYPE(grid_atom_type), POINTER                      :: grid_atom
    1911              :       TYPE(iao_env_type)                                 :: iao_env
    1912              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1913              :       TYPE(particle_list_type), POINTER                  :: particles
    1914        11299 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1915              :       TYPE(pw_c1d_gs_type)                               :: aux_g, rho_elec_gspace
    1916              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho0_s_gs, rho_core, rhoz_cneo_s_gs
    1917              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1918        11299 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
    1919              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    1920              :       TYPE(pw_r3d_rs_type)                               :: aux_r, rho_elec_rspace, wf_r
    1921        11299 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r
    1922              :       TYPE(pw_r3d_rs_type), POINTER                      :: mb_rho, v_hartree_rspace, vee
    1923        11299 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    1924              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
    1925              :       TYPE(qs_rho_type), POINTER                         :: rho
    1926              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1927              :       TYPE(rho0_mpole_type), POINTER                     :: rho0_mpole
    1928        11299 :       TYPE(rho_atom_type), DIMENSION(:), POINTER         :: rho_atom_set
    1929              :       TYPE(rho_atom_type), POINTER                       :: rho_atom
    1930              :       TYPE(section_vals_type), POINTER                   :: dft_section, hfx_section, input, &
    1931              :                                                             print_key, print_key_bqb, &
    1932              :                                                             print_key_voro, xc_section
    1933              : 
    1934        11299 :       CALL timeset(routineN, handle)
    1935        11299 :       NULLIFY (cell, dft_control, pw_env, auxbas_pw_pool, pw_pools, hfx_section, &
    1936        11299 :                atomic_kind_set, qs_kind_set, particle_set, rho, ks_rmpv, rho_ao, rho_r, &
    1937        11299 :                dft_section, xc_section, input, particles, subsys, matrix_vxc, v_hartree_rspace, &
    1938        11299 :                vee)
    1939              : 
    1940        11299 :       logger => cp_get_default_logger()
    1941        11299 :       output_unit = cp_logger_get_default_io_unit(logger)
    1942              : 
    1943        11299 :       CPASSERT(ASSOCIATED(qs_env))
    1944              :       CALL get_qs_env(qs_env, &
    1945              :                       atomic_kind_set=atomic_kind_set, &
    1946              :                       qs_kind_set=qs_kind_set, &
    1947              :                       particle_set=particle_set, &
    1948              :                       cell=cell, &
    1949              :                       para_env=para_env, &
    1950              :                       dft_control=dft_control, &
    1951              :                       input=input, &
    1952              :                       do_kpoints=do_kpoints, &
    1953        11299 :                       subsys=subsys)
    1954        11299 :       dft_section => section_vals_get_subs_vals(input, "DFT")
    1955        11299 :       CALL qs_subsys_get(subsys, particles=particles)
    1956              : 
    1957        11299 :       CALL get_qs_env(qs_env, rho=rho)
    1958        11299 :       CALL qs_rho_get(rho, rho_r=rho_r)
    1959              : 
    1960        11299 :       CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
    1961        33897 :       ALLOCATE (zcharge(natom))
    1962        31651 :       DO ikind = 1, nkind
    1963        20352 :          CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff)
    1964        20352 :          CALL get_atomic_kind(atomic_kind_set(ikind), natom=nat)
    1965        74842 :          DO iatom = 1, nat
    1966        43191 :             iat = atomic_kind_set(ikind)%atom_list(iatom)
    1967        63543 :             zcharge(iat) = zeff
    1968              :          END DO
    1969              :       END DO
    1970              : 
    1971              :       ! Print the total density (electronic + core charge)
    1972        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    1973              :                                            "DFT%PRINT%TOT_DENSITY_CUBE"), cp_p_file)) THEN
    1974           82 :          NULLIFY (rho_core, rho0_s_gs, rhoz_cneo_s_gs)
    1975           82 :          append_cube = section_get_lval(input, "DFT%PRINT%TOT_DENSITY_CUBE%APPEND")
    1976           82 :          my_pos_cube = "REWIND"
    1977           82 :          IF (append_cube) THEN
    1978            0 :             my_pos_cube = "APPEND"
    1979              :          END IF
    1980              : 
    1981              :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, rho_core=rho_core, &
    1982           82 :                          rho0_s_gs=rho0_s_gs, rhoz_cneo_s_gs=rhoz_cneo_s_gs)
    1983              :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
    1984           82 :                          pw_pools=pw_pools)
    1985           82 :          CALL auxbas_pw_pool%create_pw(wf_r)
    1986           82 :          IF (dft_control%qs_control%gapw) THEN
    1987            0 :             IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
    1988            0 :                CALL pw_axpy(rho_core, rho0_s_gs)
    1989            0 :                IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
    1990            0 :                   CALL pw_axpy(rhoz_cneo_s_gs, rho0_s_gs)
    1991              :                END IF
    1992            0 :                CALL pw_transfer(rho0_s_gs, wf_r)
    1993            0 :                CALL pw_axpy(rho_core, rho0_s_gs, -1.0_dp)
    1994            0 :                IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
    1995            0 :                   CALL pw_axpy(rhoz_cneo_s_gs, rho0_s_gs, -1.0_dp)
    1996              :                END IF
    1997              :             ELSE
    1998            0 :                IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
    1999            0 :                   CALL pw_axpy(rhoz_cneo_s_gs, rho0_s_gs)
    2000              :                END IF
    2001            0 :                CALL pw_transfer(rho0_s_gs, wf_r)
    2002            0 :                IF (ASSOCIATED(rhoz_cneo_s_gs)) THEN
    2003            0 :                   CALL pw_axpy(rhoz_cneo_s_gs, rho0_s_gs, -1.0_dp)
    2004              :                END IF
    2005              :             END IF
    2006              :          ELSE
    2007           82 :             CALL pw_transfer(rho_core, wf_r)
    2008              :          END IF
    2009          164 :          DO ispin = 1, dft_control%nspins
    2010          164 :             CALL pw_axpy(rho_r(ispin), wf_r)
    2011              :          END DO
    2012           82 :          filename = "TOTAL_DENSITY"
    2013           82 :          mpi_io = .TRUE.
    2014              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%TOT_DENSITY_CUBE", &
    2015              :                                         extension=".cube", middle_name=TRIM(filename), file_position=my_pos_cube, &
    2016           82 :                                         log_filename=.FALSE., mpi_io=mpi_io)
    2017              :          CALL cp_pw_to_cube(wf_r, unit_nr, "TOTAL DENSITY", &
    2018              :                             particles=particles, zeff=zcharge, &
    2019           82 :                             stride=section_get_ivals(dft_section, "PRINT%TOT_DENSITY_CUBE%STRIDE"), mpi_io=mpi_io)
    2020              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2021           82 :                                            "DFT%PRINT%TOT_DENSITY_CUBE", mpi_io=mpi_io)
    2022           82 :          CALL auxbas_pw_pool%give_back_pw(wf_r)
    2023              :       END IF
    2024              : 
    2025              :       ! Write cube file with electron density
    2026        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2027              :                                            "DFT%PRINT%E_DENSITY_CUBE"), cp_p_file)) THEN
    2028              :          CALL section_vals_val_get(dft_section, &
    2029              :                                    keyword_name="PRINT%E_DENSITY_CUBE%DENSITY_INCLUDE", &
    2030          150 :                                    c_val=print_density)
    2031              :          print_density = TRIM(print_density)
    2032          150 :          append_cube = section_get_lval(input, "DFT%PRINT%E_DENSITY_CUBE%APPEND")
    2033          150 :          my_pos_cube = "REWIND"
    2034          150 :          IF (append_cube) THEN
    2035            0 :             my_pos_cube = "APPEND"
    2036              :          END IF
    2037              :          ! Write the info on core densities for the interface between cp2k and the XRD code
    2038              :          ! together with the valence density they are used to compute the form factor (Fourier transform)
    2039          150 :          xrd_interface = section_get_lval(input, "DFT%PRINT%E_DENSITY_CUBE%XRD_INTERFACE")
    2040          150 :          IF (xrd_interface) THEN
    2041              :             !cube file only contains soft density (GAPW)
    2042            2 :             IF (dft_control%qs_control%gapw) print_density = "SOFT_DENSITY"
    2043              : 
    2044            2 :             filename = "ELECTRON_DENSITY"
    2045              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2046              :                                            extension=".xrd", middle_name=TRIM(filename), &
    2047            2 :                                            file_position=my_pos_cube, log_filename=.FALSE.)
    2048            2 :             ngto = section_get_ival(input, "DFT%PRINT%E_DENSITY_CUBE%NGAUSS")
    2049            2 :             IF (output_unit > 0) THEN
    2050            1 :                INQUIRE (UNIT=unit_nr, NAME=filename)
    2051              :                WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2052            1 :                   "The electron density (atomic part) is written to the file:", &
    2053            2 :                   TRIM(filename)
    2054              :             END IF
    2055              : 
    2056            2 :             xc_section => section_vals_get_subs_vals(input, "DFT%XC")
    2057            2 :             nkind = SIZE(atomic_kind_set)
    2058            2 :             IF (unit_nr > 0) THEN
    2059            1 :                WRITE (unit_nr, *) "Atomic (core) densities"
    2060            1 :                WRITE (unit_nr, *) "Unit cell"
    2061            1 :                WRITE (unit_nr, FMT="(3F20.12)") cell%hmat(1, 1), cell%hmat(1, 2), cell%hmat(1, 3)
    2062            1 :                WRITE (unit_nr, FMT="(3F20.12)") cell%hmat(2, 1), cell%hmat(2, 2), cell%hmat(2, 3)
    2063            1 :                WRITE (unit_nr, FMT="(3F20.12)") cell%hmat(3, 1), cell%hmat(3, 2), cell%hmat(3, 3)
    2064            1 :                WRITE (unit_nr, *) "Atomic types"
    2065            1 :                WRITE (unit_nr, *) nkind
    2066              :             END IF
    2067              :             ! calculate atomic density and core density
    2068           16 :             ALLOCATE (ppdens(ngto, 2, nkind), aedens(ngto, 2, nkind), ccdens(ngto, 2, nkind))
    2069            6 :             DO ikind = 1, nkind
    2070            4 :                atomic_kind => atomic_kind_set(ikind)
    2071            4 :                qs_kind => qs_kind_set(ikind)
    2072            4 :                CALL get_atomic_kind(atomic_kind, name=name, element_symbol=element_symbol)
    2073              :                CALL calculate_atomic_density(ppdens(:, :, ikind), atomic_kind, qs_kind, ngto, &
    2074            4 :                                              iunit=output_unit, confine=.TRUE.)
    2075              :                CALL calculate_atomic_density(aedens(:, :, ikind), atomic_kind, qs_kind, ngto, &
    2076            4 :                                              iunit=output_unit, allelectron=.TRUE., confine=.TRUE.)
    2077           52 :                ccdens(:, 1, ikind) = aedens(:, 1, ikind)
    2078           52 :                ccdens(:, 2, ikind) = 0._dp
    2079              :                CALL project_function_a(ccdens(1:ngto, 2, ikind), ccdens(1:ngto, 1, ikind), &
    2080            4 :                                        ppdens(1:ngto, 2, ikind), ppdens(1:ngto, 1, ikind), 0)
    2081           52 :                ccdens(:, 2, ikind) = aedens(:, 2, ikind) - ccdens(:, 2, ikind)
    2082            4 :                IF (unit_nr > 0) THEN
    2083            2 :                   WRITE (unit_nr, FMT="(I6,A10,A20)") ikind, TRIM(element_symbol), TRIM(name)
    2084            2 :                   WRITE (unit_nr, FMT="(I6)") ngto
    2085            2 :                   WRITE (unit_nr, *) "   Total density"
    2086           26 :                   WRITE (unit_nr, FMT="(2G24.12)") (aedens(i, 1, ikind), aedens(i, 2, ikind), i=1, ngto)
    2087            2 :                   WRITE (unit_nr, *) "    Core density"
    2088           26 :                   WRITE (unit_nr, FMT="(2G24.12)") (ccdens(i, 1, ikind), ccdens(i, 2, ikind), i=1, ngto)
    2089              :                END IF
    2090            6 :                NULLIFY (atomic_kind)
    2091              :             END DO
    2092              : 
    2093            2 :             IF (dft_control%qs_control%gapw) THEN
    2094            2 :                CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom_set)
    2095              : 
    2096            2 :                IF (unit_nr > 0) THEN
    2097            1 :                   WRITE (unit_nr, *) "Coordinates and GAPW density"
    2098              :                END IF
    2099            2 :                np = particles%n_els
    2100            6 :                DO iat = 1, np
    2101            4 :                   CALL get_atomic_kind(particles%els(iat)%atomic_kind, kind_number=ikind)
    2102            4 :                   CALL get_qs_kind(qs_kind_set(ikind), grid_atom=grid_atom)
    2103            4 :                   rho_atom => rho_atom_set(iat)
    2104            4 :                   IF (ASSOCIATED(rho_atom%rho_rad_h(1)%r_coef)) THEN
    2105            2 :                      nr = SIZE(rho_atom%rho_rad_h(1)%r_coef, 1)
    2106            2 :                      niso = SIZE(rho_atom%rho_rad_h(1)%r_coef, 2)
    2107              :                   ELSE
    2108            2 :                      nr = 0
    2109            2 :                      niso = 0
    2110              :                   END IF
    2111            4 :                   CALL para_env%sum(nr)
    2112            4 :                   CALL para_env%sum(niso)
    2113              : 
    2114           16 :                   ALLOCATE (bfun(nr, niso))
    2115         1840 :                   bfun = 0._dp
    2116            8 :                   DO ispin = 1, dft_control%nspins
    2117            8 :                      IF (ASSOCIATED(rho_atom%rho_rad_h(1)%r_coef)) THEN
    2118          920 :                         bfun(:, :) = bfun + rho_atom%rho_rad_h(ispin)%r_coef - rho_atom%rho_rad_s(ispin)%r_coef
    2119              :                      END IF
    2120              :                   END DO
    2121            4 :                   CALL para_env%sum(bfun)
    2122           52 :                   ccdens(:, 1, ikind) = ppdens(:, 1, ikind)
    2123           52 :                   ccdens(:, 2, ikind) = 0._dp
    2124            4 :                   IF (unit_nr > 0) THEN
    2125            8 :                      WRITE (unit_nr, '(I10,I5,3f12.6)') iat, ikind, particles%els(iat)%r
    2126              :                   END IF
    2127           40 :                   DO iso = 1, niso
    2128           36 :                      l = indso(1, iso)
    2129           36 :                      CALL project_function_b(ccdens(:, 2, ikind), ccdens(:, 1, ikind), bfun(:, iso), grid_atom, l)
    2130           40 :                      IF (unit_nr > 0) THEN
    2131           18 :                         WRITE (unit_nr, FMT="(3I6)") iso, l, ngto
    2132          234 :                         WRITE (unit_nr, FMT="(2G24.12)") (ccdens(i, 1, ikind), ccdens(i, 2, ikind), i=1, ngto)
    2133              :                      END IF
    2134              :                   END DO
    2135           10 :                   DEALLOCATE (bfun)
    2136              :                END DO
    2137              :             ELSE
    2138            0 :                IF (unit_nr > 0) THEN
    2139            0 :                   WRITE (unit_nr, *) "Coordinates"
    2140            0 :                   np = particles%n_els
    2141            0 :                   DO iat = 1, np
    2142            0 :                      CALL get_atomic_kind(particles%els(iat)%atomic_kind, kind_number=ikind)
    2143            0 :                      WRITE (unit_nr, '(I10,I5,3f12.6)') iat, ikind, particles%els(iat)%r
    2144              :                   END DO
    2145              :                END IF
    2146              :             END IF
    2147              : 
    2148            2 :             DEALLOCATE (ppdens, aedens, ccdens)
    2149              : 
    2150              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2151            2 :                                               "DFT%PRINT%E_DENSITY_CUBE")
    2152              : 
    2153              :          END IF
    2154          150 :          IF (dft_control%qs_control%gapw .AND. print_density == "TOTAL_DENSITY") THEN
    2155              :             ! total density in g-space not implemented for k-points
    2156            4 :             CPASSERT(.NOT. do_kpoints)
    2157              :             ! Print total electronic density
    2158              :             CALL get_qs_env(qs_env=qs_env, &
    2159            4 :                             pw_env=pw_env)
    2160              :             CALL pw_env_get(pw_env=pw_env, &
    2161              :                             auxbas_pw_pool=auxbas_pw_pool, &
    2162            4 :                             pw_pools=pw_pools)
    2163            4 :             CALL auxbas_pw_pool%create_pw(pw=rho_elec_rspace)
    2164            4 :             CALL pw_zero(rho_elec_rspace)
    2165            4 :             CALL auxbas_pw_pool%create_pw(pw=rho_elec_gspace)
    2166            4 :             CALL pw_zero(rho_elec_gspace)
    2167              :             CALL get_pw_grid_info(pw_grid=rho_elec_gspace%pw_grid, &
    2168              :                                   dr=dr, &
    2169            4 :                                   vol=volume)
    2170           16 :             q_max = SQRT(SUM((pi/dr(:))**2))
    2171              :             CALL calculate_rhotot_elec_gspace(qs_env=qs_env, &
    2172              :                                               auxbas_pw_pool=auxbas_pw_pool, &
    2173              :                                               rhotot_elec_gspace=rho_elec_gspace, &
    2174              :                                               q_max=q_max, &
    2175              :                                               rho_hard=rho_hard, &
    2176            4 :                                               rho_soft=rho_soft)
    2177            4 :             rho_total = rho_hard + rho_soft
    2178              :             CALL get_pw_grid_info(pw_grid=rho_elec_gspace%pw_grid, &
    2179            4 :                                   vol=volume)
    2180              :             ! rhotot pw coefficients are by default scaled by grid volume
    2181              :             ! need to undo this to get proper charge from printed cube
    2182            4 :             CALL pw_scale(rho_elec_gspace, 1.0_dp/volume)
    2183              : 
    2184            4 :             CALL pw_transfer(rho_elec_gspace, rho_elec_rspace)
    2185            4 :             rho_total_rspace = pw_integrate_function(rho_elec_rspace, isign=-1)
    2186            4 :             filename = "TOTAL_ELECTRON_DENSITY"
    2187            4 :             mpi_io = .TRUE.
    2188              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2189              :                                            extension=".cube", middle_name=TRIM(filename), &
    2190              :                                            file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2191            4 :                                            fout=mpi_filename)
    2192            4 :             IF (output_unit > 0) THEN
    2193            2 :                IF (.NOT. mpi_io) THEN
    2194            0 :                   INQUIRE (UNIT=unit_nr, NAME=filename)
    2195              :                ELSE
    2196            2 :                   filename = mpi_filename
    2197              :                END IF
    2198              :                WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2199            2 :                   "The total electron density is written in cube file format to the file:", &
    2200            4 :                   TRIM(filename)
    2201              :                WRITE (UNIT=output_unit, FMT="(/,(T2,A,F20.10))") &
    2202            2 :                   "q(max) [1/Angstrom]              :", q_max/angstrom, &
    2203            2 :                   "Soft electronic charge (G-space) :", rho_soft, &
    2204            2 :                   "Hard electronic charge (G-space) :", rho_hard, &
    2205            2 :                   "Total electronic charge (G-space):", rho_total, &
    2206            4 :                   "Total electronic charge (R-space):", rho_total_rspace
    2207              :             END IF
    2208              :             CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "TOTAL ELECTRON DENSITY", &
    2209              :                                particles=particles, zeff=zcharge, &
    2210            4 :                                stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), mpi_io=mpi_io)
    2211              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2212            4 :                                               "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2213              :             ! Print total spin density for spin-polarized systems
    2214            4 :             IF (dft_control%nspins > 1) THEN
    2215            2 :                CALL pw_zero(rho_elec_gspace)
    2216            2 :                CALL pw_zero(rho_elec_rspace)
    2217              :                CALL calculate_rhotot_elec_gspace(qs_env=qs_env, &
    2218              :                                                  auxbas_pw_pool=auxbas_pw_pool, &
    2219              :                                                  rhotot_elec_gspace=rho_elec_gspace, &
    2220              :                                                  q_max=q_max, &
    2221              :                                                  rho_hard=rho_hard, &
    2222              :                                                  rho_soft=rho_soft, &
    2223            2 :                                                  fsign=-1.0_dp)
    2224            2 :                rho_total = rho_hard + rho_soft
    2225              : 
    2226              :                ! rhotot pw coefficients are by default scaled by grid volume
    2227              :                ! need to undo this to get proper charge from printed cube
    2228            2 :                CALL pw_scale(rho_elec_gspace, 1.0_dp/volume)
    2229              : 
    2230            2 :                CALL pw_transfer(rho_elec_gspace, rho_elec_rspace)
    2231            2 :                rho_total_rspace = pw_integrate_function(rho_elec_rspace, isign=-1)
    2232            2 :                filename = "TOTAL_SPIN_DENSITY"
    2233            2 :                mpi_io = .TRUE.
    2234              :                unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2235              :                                               extension=".cube", middle_name=TRIM(filename), &
    2236              :                                               file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2237            2 :                                               fout=mpi_filename)
    2238            2 :                IF (output_unit > 0) THEN
    2239            1 :                   IF (.NOT. mpi_io) THEN
    2240            0 :                      INQUIRE (UNIT=unit_nr, NAME=filename)
    2241              :                   ELSE
    2242            1 :                      filename = mpi_filename
    2243              :                   END IF
    2244              :                   WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2245            1 :                      "The total spin density is written in cube file format to the file:", &
    2246            2 :                      TRIM(filename)
    2247              :                   WRITE (UNIT=output_unit, FMT="(/,(T2,A,F20.10))") &
    2248            1 :                      "q(max) [1/Angstrom]                    :", q_max/angstrom, &
    2249            1 :                      "Soft part of the spin density (G-space):", rho_soft, &
    2250            1 :                      "Hard part of the spin density (G-space):", rho_hard, &
    2251            1 :                      "Total spin density (G-space)           :", rho_total, &
    2252            2 :                      "Total spin density (R-space)           :", rho_total_rspace
    2253              :                END IF
    2254              :                CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "TOTAL SPIN DENSITY", &
    2255              :                                   particles=particles, zeff=zcharge, &
    2256            2 :                                   stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), mpi_io=mpi_io)
    2257              :                CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2258            2 :                                                  "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2259              :             END IF
    2260            4 :             CALL auxbas_pw_pool%give_back_pw(rho_elec_gspace)
    2261            4 :             CALL auxbas_pw_pool%give_back_pw(rho_elec_rspace)
    2262              : 
    2263          146 :          ELSE IF (print_density == "SOFT_DENSITY" .OR. .NOT. dft_control%qs_control%gapw) THEN
    2264          142 :             IF (dft_control%nspins > 1) THEN
    2265              :                CALL get_qs_env(qs_env=qs_env, &
    2266           48 :                                pw_env=pw_env)
    2267              :                CALL pw_env_get(pw_env=pw_env, &
    2268              :                                auxbas_pw_pool=auxbas_pw_pool, &
    2269           48 :                                pw_pools=pw_pools)
    2270           48 :                CALL auxbas_pw_pool%create_pw(pw=rho_elec_rspace)
    2271           48 :                CALL pw_copy(rho_r(1), rho_elec_rspace)
    2272           48 :                CALL pw_axpy(rho_r(2), rho_elec_rspace)
    2273           48 :                filename = "ELECTRON_DENSITY"
    2274           48 :                mpi_io = .TRUE.
    2275              :                unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2276              :                                               extension=".cube", middle_name=TRIM(filename), &
    2277              :                                               file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2278           48 :                                               fout=mpi_filename)
    2279           48 :                IF (output_unit > 0) THEN
    2280           24 :                   IF (.NOT. mpi_io) THEN
    2281            0 :                      INQUIRE (UNIT=unit_nr, NAME=filename)
    2282              :                   ELSE
    2283           24 :                      filename = mpi_filename
    2284              :                   END IF
    2285              :                   WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2286           24 :                      "The sum of alpha and beta density is written in cube file format to the file:", &
    2287           48 :                      TRIM(filename)
    2288              :                END IF
    2289              :                CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "SUM OF ALPHA AND BETA DENSITY", &
    2290              :                                   particles=particles, zeff=zcharge, &
    2291              :                                   stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), &
    2292           48 :                                   mpi_io=mpi_io)
    2293              :                CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2294           48 :                                                  "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2295           48 :                CALL pw_copy(rho_r(1), rho_elec_rspace)
    2296           48 :                CALL pw_axpy(rho_r(2), rho_elec_rspace, alpha=-1.0_dp)
    2297           48 :                filename = "SPIN_DENSITY"
    2298           48 :                mpi_io = .TRUE.
    2299              :                unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2300              :                                               extension=".cube", middle_name=TRIM(filename), &
    2301              :                                               file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2302           48 :                                               fout=mpi_filename)
    2303           48 :                IF (output_unit > 0) THEN
    2304           24 :                   IF (.NOT. mpi_io) THEN
    2305            0 :                      INQUIRE (UNIT=unit_nr, NAME=filename)
    2306              :                   ELSE
    2307           24 :                      filename = mpi_filename
    2308              :                   END IF
    2309              :                   WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2310           24 :                      "The spin density is written in cube file format to the file:", &
    2311           48 :                      TRIM(filename)
    2312              :                END IF
    2313              :                CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "SPIN DENSITY", &
    2314              :                                   particles=particles, zeff=zcharge, &
    2315           48 :                                   stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), mpi_io=mpi_io)
    2316              :                CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2317           48 :                                                  "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2318           48 :                CALL auxbas_pw_pool%give_back_pw(rho_elec_rspace)
    2319              :             ELSE
    2320           94 :                filename = "ELECTRON_DENSITY"
    2321           94 :                mpi_io = .TRUE.
    2322              :                unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2323              :                                               extension=".cube", middle_name=TRIM(filename), &
    2324              :                                               file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2325           94 :                                               fout=mpi_filename)
    2326           94 :                IF (output_unit > 0) THEN
    2327           47 :                   IF (.NOT. mpi_io) THEN
    2328            0 :                      INQUIRE (UNIT=unit_nr, NAME=filename)
    2329              :                   ELSE
    2330           47 :                      filename = mpi_filename
    2331              :                   END IF
    2332              :                   WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2333           47 :                      "The electron density is written in cube file format to the file:", &
    2334           94 :                      TRIM(filename)
    2335              :                END IF
    2336              :                CALL cp_pw_to_cube(rho_r(1), unit_nr, "ELECTRON DENSITY", &
    2337              :                                   particles=particles, zeff=zcharge, &
    2338           94 :                                   stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), mpi_io=mpi_io)
    2339              :                CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2340           94 :                                                  "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2341              :             END IF ! nspins
    2342              : 
    2343            4 :          ELSE IF (dft_control%qs_control%gapw .AND. print_density == "TOTAL_HARD_APPROX") THEN
    2344            4 :             CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, rho0_mpole=rho0_mpole, natom=natom)
    2345            4 :             CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, pw_pools=pw_pools)
    2346            4 :             CALL auxbas_pw_pool%create_pw(rho_elec_rspace)
    2347              : 
    2348            4 :             NULLIFY (my_Q0)
    2349           12 :             ALLOCATE (my_Q0(natom))
    2350           16 :             my_Q0 = 0.0_dp
    2351              : 
    2352              :             ! (eta/pi)**3: normalization for 3d gaussian of form exp(-eta*r**2)
    2353            4 :             norm_factor = SQRT((rho0_mpole%zet0_h/pi)**3)
    2354              : 
    2355              :             ! store hard part of electronic density in array
    2356           16 :             DO iat = 1, natom
    2357           34 :                my_Q0(iat) = SUM(rho0_mpole%mp_rho(iat)%Q0(1:dft_control%nspins))*norm_factor
    2358              :             END DO
    2359              :             ! multiply coeff with gaussian and put on realspace grid
    2360              :             ! coeff is the gaussian prefactor, eta the gaussian exponent
    2361            4 :             CALL calculate_rho_resp_all(rho_elec_rspace, coeff=my_Q0, natom=natom, eta=rho0_mpole%zet0_h, qs_env=qs_env)
    2362            4 :             rho_hard = pw_integrate_function(rho_elec_rspace, isign=-1)
    2363              : 
    2364            4 :             rho_soft = 0.0_dp
    2365           10 :             DO ispin = 1, dft_control%nspins
    2366            6 :                CALL pw_axpy(rho_r(ispin), rho_elec_rspace)
    2367           10 :                rho_soft = rho_soft + pw_integrate_function(rho_r(ispin), isign=-1)
    2368              :             END DO
    2369              : 
    2370            4 :             rho_total_rspace = rho_soft + rho_hard
    2371              : 
    2372            4 :             filename = "ELECTRON_DENSITY"
    2373            4 :             mpi_io = .TRUE.
    2374              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2375              :                                            extension=".cube", middle_name=TRIM(filename), &
    2376              :                                            file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2377            4 :                                            fout=mpi_filename)
    2378            4 :             IF (output_unit > 0) THEN
    2379            2 :                IF (.NOT. mpi_io) THEN
    2380            0 :                   INQUIRE (UNIT=unit_nr, NAME=filename)
    2381              :                ELSE
    2382            2 :                   filename = mpi_filename
    2383              :                END IF
    2384              :                WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2385            2 :                   "The electron density is written in cube file format to the file:", &
    2386            4 :                   TRIM(filename)
    2387              :                WRITE (UNIT=output_unit, FMT="(/,(T2,A,F20.10))") &
    2388            2 :                   "Soft electronic charge (R-space) :", rho_soft, &
    2389            2 :                   "Hard electronic charge (R-space) :", rho_hard, &
    2390            4 :                   "Total electronic charge (R-space):", rho_total_rspace
    2391              :             END IF
    2392              :             CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "ELECTRON DENSITY", &
    2393              :                                particles=particles, zeff=zcharge, &
    2394              :                                stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), &
    2395            4 :                                mpi_io=mpi_io)
    2396              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2397            4 :                                               "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2398              : 
    2399              :             !------------
    2400            4 :             IF (dft_control%nspins > 1) THEN
    2401            8 :             DO iat = 1, natom
    2402            8 :                my_Q0(iat) = (rho0_mpole%mp_rho(iat)%Q0(1) - rho0_mpole%mp_rho(iat)%Q0(2))*norm_factor
    2403              :             END DO
    2404            2 :             CALL pw_zero(rho_elec_rspace)
    2405            2 :             CALL calculate_rho_resp_all(rho_elec_rspace, coeff=my_Q0, natom=natom, eta=rho0_mpole%zet0_h, qs_env=qs_env)
    2406            2 :             rho_hard = pw_integrate_function(rho_elec_rspace, isign=-1)
    2407              : 
    2408            2 :             CALL pw_axpy(rho_r(1), rho_elec_rspace)
    2409            2 :             CALL pw_axpy(rho_r(2), rho_elec_rspace, alpha=-1.0_dp)
    2410              :             rho_soft = pw_integrate_function(rho_r(1), isign=-1) &
    2411            2 :                        - pw_integrate_function(rho_r(2), isign=-1)
    2412              : 
    2413            2 :             rho_total_rspace = rho_soft + rho_hard
    2414              : 
    2415            2 :             filename = "SPIN_DENSITY"
    2416            2 :             mpi_io = .TRUE.
    2417              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%E_DENSITY_CUBE", &
    2418              :                                            extension=".cube", middle_name=TRIM(filename), &
    2419              :                                            file_position=my_pos_cube, log_filename=.FALSE., mpi_io=mpi_io, &
    2420            2 :                                            fout=mpi_filename)
    2421            2 :             IF (output_unit > 0) THEN
    2422            1 :                IF (.NOT. mpi_io) THEN
    2423            0 :                   INQUIRE (UNIT=unit_nr, NAME=filename)
    2424              :                ELSE
    2425            1 :                   filename = mpi_filename
    2426              :                END IF
    2427              :                WRITE (UNIT=output_unit, FMT="(/,T2,A,/,/,T2,A)") &
    2428            1 :                   "The spin density is written in cube file format to the file:", &
    2429            2 :                   TRIM(filename)
    2430              :                WRITE (UNIT=output_unit, FMT="(/,(T2,A,F20.10))") &
    2431            1 :                   "Soft part of the spin density          :", rho_soft, &
    2432            1 :                   "Hard part of the spin density          :", rho_hard, &
    2433            2 :                   "Total spin density (R-space)           :", rho_total_rspace
    2434              :             END IF
    2435              :             CALL cp_pw_to_cube(rho_elec_rspace, unit_nr, "SPIN DENSITY", &
    2436              :                                particles=particles, zeff=zcharge, &
    2437            2 :                                stride=section_get_ivals(dft_section, "PRINT%E_DENSITY_CUBE%STRIDE"), mpi_io=mpi_io)
    2438              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2439            2 :                                               "DFT%PRINT%E_DENSITY_CUBE", mpi_io=mpi_io)
    2440              :             END IF ! nspins
    2441            4 :             CALL auxbas_pw_pool%give_back_pw(rho_elec_rspace)
    2442            4 :             DEALLOCATE (my_Q0)
    2443              :          END IF ! print_density
    2444              :       END IF ! print key
    2445              : 
    2446              :       IF (BTEST(cp_print_key_should_output(logger%iter_info, &
    2447        11299 :                                            dft_section, "PRINT%ENERGY_WINDOWS"), cp_p_file) .AND. .NOT. do_kpoints) THEN
    2448           90 :          CALL energy_windows(qs_env)
    2449              :       END IF
    2450              : 
    2451              :       ! Print the hartree potential
    2452        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2453              :                                            "DFT%PRINT%V_HARTREE_CUBE"), cp_p_file)) THEN
    2454              : 
    2455              :          CALL get_qs_env(qs_env=qs_env, &
    2456              :                          pw_env=pw_env, &
    2457          114 :                          v_hartree_rspace=v_hartree_rspace)
    2458          114 :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    2459          114 :          CALL auxbas_pw_pool%create_pw(aux_r)
    2460              : 
    2461          114 :          append_cube = section_get_lval(input, "DFT%PRINT%V_HARTREE_CUBE%APPEND")
    2462          114 :          my_pos_cube = "REWIND"
    2463          114 :          IF (append_cube) THEN
    2464            0 :             my_pos_cube = "APPEND"
    2465              :          END IF
    2466          114 :          mpi_io = .TRUE.
    2467          114 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    2468          114 :          CALL pw_env_get(pw_env)
    2469              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%V_HARTREE_CUBE", &
    2470          114 :                                         extension=".cube", middle_name="v_hartree", file_position=my_pos_cube, mpi_io=mpi_io)
    2471          114 :          udvol = 1.0_dp/v_hartree_rspace%pw_grid%dvol
    2472              : 
    2473          114 :          CALL pw_copy(v_hartree_rspace, aux_r)
    2474          114 :          CALL pw_scale(aux_r, udvol)
    2475              : 
    2476              :          CALL cp_pw_to_cube(aux_r, unit_nr, "HARTREE POTENTIAL", particles=particles, zeff=zcharge, &
    2477              :                             stride=section_get_ivals(dft_section, &
    2478          114 :                                                      "PRINT%V_HARTREE_CUBE%STRIDE"), mpi_io=mpi_io)
    2479              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2480          114 :                                            "DFT%PRINT%V_HARTREE_CUBE", mpi_io=mpi_io)
    2481              : 
    2482          114 :          CALL auxbas_pw_pool%give_back_pw(aux_r)
    2483              :       END IF
    2484              : 
    2485              :       ! Print the external potential
    2486        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2487              :                                            "DFT%PRINT%EXTERNAL_POTENTIAL_CUBE"), cp_p_file)) THEN
    2488           86 :          IF (dft_control%apply_external_potential) THEN
    2489            4 :             CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, vee=vee)
    2490            4 :             CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    2491            4 :             CALL auxbas_pw_pool%create_pw(aux_r)
    2492              : 
    2493            4 :             append_cube = section_get_lval(input, "DFT%PRINT%EXTERNAL_POTENTIAL_CUBE%APPEND")
    2494            4 :             my_pos_cube = "REWIND"
    2495            4 :             IF (append_cube) THEN
    2496            0 :                my_pos_cube = "APPEND"
    2497              :             END IF
    2498            4 :             mpi_io = .TRUE.
    2499            4 :             CALL pw_env_get(pw_env)
    2500              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%EXTERNAL_POTENTIAL_CUBE", &
    2501            4 :                                            extension=".cube", middle_name="ext_pot", file_position=my_pos_cube, mpi_io=mpi_io)
    2502              : 
    2503            4 :             CALL pw_copy(vee, aux_r)
    2504              : 
    2505              :             CALL cp_pw_to_cube(aux_r, unit_nr, "EXTERNAL POTENTIAL", particles=particles, zeff=zcharge, &
    2506              :                                stride=section_get_ivals(dft_section, &
    2507            4 :                                                         "PRINT%EXTERNAL_POTENTIAL_CUBE%STRIDE"), mpi_io=mpi_io)
    2508              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2509            4 :                                               "DFT%PRINT%EXTERNAL_POTENTIAL_CUBE", mpi_io=mpi_io)
    2510              : 
    2511            4 :             CALL auxbas_pw_pool%give_back_pw(aux_r)
    2512              :          END IF
    2513              :       END IF
    2514              : 
    2515              :       ! Print the Electrical Field Components
    2516        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2517              :                                            "DFT%PRINT%EFIELD_CUBE"), cp_p_file)) THEN
    2518              : 
    2519           82 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    2520           82 :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    2521           82 :          CALL auxbas_pw_pool%create_pw(aux_r)
    2522           82 :          CALL auxbas_pw_pool%create_pw(aux_g)
    2523              : 
    2524           82 :          append_cube = section_get_lval(input, "DFT%PRINT%EFIELD_CUBE%APPEND")
    2525           82 :          my_pos_cube = "REWIND"
    2526           82 :          IF (append_cube) THEN
    2527            0 :             my_pos_cube = "APPEND"
    2528              :          END IF
    2529              :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, &
    2530           82 :                          v_hartree_rspace=v_hartree_rspace)
    2531           82 :          CALL pw_env_get(pw_env)
    2532           82 :          udvol = 1.0_dp/v_hartree_rspace%pw_grid%dvol
    2533          328 :          DO id = 1, 3
    2534          246 :             mpi_io = .TRUE.
    2535              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%EFIELD_CUBE", &
    2536              :                                            extension=".cube", middle_name="efield_"//cdir(id), file_position=my_pos_cube, &
    2537          246 :                                            mpi_io=mpi_io)
    2538              : 
    2539          246 :             CALL pw_transfer(v_hartree_rspace, aux_g)
    2540          246 :             nd = 0
    2541          246 :             nd(id) = 1
    2542          246 :             CALL pw_derive(aux_g, nd)
    2543          246 :             CALL pw_transfer(aux_g, aux_r)
    2544          246 :             CALL pw_scale(aux_r, udvol)
    2545              : 
    2546              :             CALL cp_pw_to_cube(aux_r, &
    2547              :                                unit_nr, "ELECTRIC FIELD", particles=particles, zeff=zcharge, &
    2548              :                                stride=section_get_ivals(dft_section, &
    2549          246 :                                                         "PRINT%EFIELD_CUBE%STRIDE"), mpi_io=mpi_io)
    2550              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    2551          328 :                                               "DFT%PRINT%EFIELD_CUBE", mpi_io=mpi_io)
    2552              :          END DO
    2553              : 
    2554           82 :          CALL auxbas_pw_pool%give_back_pw(aux_r)
    2555           82 :          CALL auxbas_pw_pool%give_back_pw(aux_g)
    2556              :       END IF
    2557              : 
    2558              :       ! Write cube files from the local energy
    2559        11299 :       CALL qs_scf_post_local_energy(input, logger, qs_env)
    2560              : 
    2561              :       ! Write cube files from the local stress tensor
    2562        11299 :       CALL qs_scf_post_local_stress(input, logger, qs_env)
    2563              : 
    2564              :       ! Write cube files from the implicit Poisson solver
    2565        11299 :       CALL qs_scf_post_ps_implicit(input, logger, qs_env)
    2566              : 
    2567              :       ! post SCF Transport
    2568        11299 :       CALL qs_scf_post_transport(qs_env)
    2569              : 
    2570        11299 :       CALL section_vals_val_get(input, "DFT%PRINT%AO_MATRICES%OMIT_HEADERS", l_val=omit_headers)
    2571              :       ! Write the density matrices
    2572        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2573              :                                            "DFT%PRINT%AO_MATRICES/DENSITY"), cp_p_file)) THEN
    2574              :          iw = cp_print_key_unit_nr(logger, input, "DFT%PRINT%AO_MATRICES/DENSITY", &
    2575            4 :                                    extension=".Log")
    2576            4 :          CALL section_vals_val_get(input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
    2577            4 :          CALL qs_rho_get(rho, rho_ao_kp=rho_ao)
    2578            4 :          after = MIN(MAX(after, 1), 16)
    2579            8 :          DO ispin = 1, dft_control%nspins
    2580           12 :             DO img = 1, dft_control%nimages
    2581              :                CALL cp_dbcsr_write_sparse_matrix(rho_ao(ispin, img)%matrix, 4, after, qs_env, &
    2582            8 :                                                  para_env, output_unit=iw, omit_headers=omit_headers)
    2583              :             END DO
    2584              :          END DO
    2585              :          CALL cp_print_key_finished_output(iw, logger, input, &
    2586            4 :                                            "DFT%PRINT%AO_MATRICES/DENSITY")
    2587              :       END IF
    2588              : 
    2589              :       ! Write the Kohn-Sham matrices
    2590              :       write_ks = BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2591        11299 :                                                   "DFT%PRINT%AO_MATRICES/KOHN_SHAM_MATRIX"), cp_p_file)
    2592              :       write_xc = BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2593        11299 :                                                   "DFT%PRINT%AO_MATRICES/MATRIX_VXC"), cp_p_file)
    2594              :       ! we need to update stuff before writing, potentially computing the matrix_vxc
    2595        11299 :       IF (write_ks .OR. write_xc) THEN
    2596            4 :          IF (write_xc) qs_env%requires_matrix_vxc = .TRUE.
    2597            4 :          CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
    2598              :          CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., &
    2599            4 :                                   just_energy=.FALSE.)
    2600            4 :          IF (write_xc) qs_env%requires_matrix_vxc = .FALSE.
    2601              :       END IF
    2602              : 
    2603              :       ! Write the Kohn-Sham matrices
    2604        11299 :       IF (write_ks) THEN
    2605              :          iw = cp_print_key_unit_nr(logger, input, "DFT%PRINT%AO_MATRICES/KOHN_SHAM_MATRIX", &
    2606            4 :                                    extension=".Log")
    2607            4 :          CALL get_qs_env(qs_env=qs_env, matrix_ks_kp=ks_rmpv)
    2608            4 :          CALL section_vals_val_get(input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
    2609            4 :          after = MIN(MAX(after, 1), 16)
    2610            8 :          DO ispin = 1, dft_control%nspins
    2611           12 :             DO img = 1, dft_control%nimages
    2612              :                CALL cp_dbcsr_write_sparse_matrix(ks_rmpv(ispin, img)%matrix, 4, after, qs_env, &
    2613            8 :                                                  para_env, output_unit=iw, omit_headers=omit_headers)
    2614              :             END DO
    2615              :          END DO
    2616              :          CALL cp_print_key_finished_output(iw, logger, input, &
    2617            4 :                                            "DFT%PRINT%AO_MATRICES/KOHN_SHAM_MATRIX")
    2618              :       END IF
    2619              : 
    2620              :       ! write csr matrices
    2621              :       ! matrices in terms of the PAO basis will be taken care of in pao_post_scf.
    2622        11299 :       IF (.NOT. dft_control%qs_control%pao) THEN
    2623        10787 :          CALL write_ks_matrix_csr(qs_env, input)
    2624        10787 :          CALL write_s_matrix_csr(qs_env, input)
    2625        10787 :          CALL write_hcore_matrix_csr(qs_env, input)
    2626        10787 :          CALL write_p_matrix_csr(qs_env, input)
    2627              :       END IF
    2628              : 
    2629              :       ! write adjacency matrix
    2630        11299 :       CALL write_adjacency_matrix(qs_env, input)
    2631              : 
    2632              :       ! Write the xc matrix
    2633        11299 :       IF (write_xc) THEN
    2634            0 :          CALL get_qs_env(qs_env=qs_env, matrix_vxc_kp=matrix_vxc)
    2635            0 :          CPASSERT(ASSOCIATED(matrix_vxc))
    2636              :          iw = cp_print_key_unit_nr(logger, input, "DFT%PRINT%AO_MATRICES/MATRIX_VXC", &
    2637            0 :                                    extension=".Log")
    2638            0 :          CALL section_vals_val_get(input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
    2639            0 :          after = MIN(MAX(after, 1), 16)
    2640            0 :          DO ispin = 1, dft_control%nspins
    2641            0 :             DO img = 1, dft_control%nimages
    2642              :                CALL cp_dbcsr_write_sparse_matrix(matrix_vxc(ispin, img)%matrix, 4, after, qs_env, &
    2643            0 :                                                  para_env, output_unit=iw, omit_headers=omit_headers)
    2644              :             END DO
    2645              :          END DO
    2646              :          CALL cp_print_key_finished_output(iw, logger, input, &
    2647            0 :                                            "DFT%PRINT%AO_MATRICES/MATRIX_VXC")
    2648              :       END IF
    2649              : 
    2650              :       ! Write the [H,r] commutator matrices
    2651        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    2652              :                                            "DFT%PRINT%AO_MATRICES/COMMUTATOR_HR"), cp_p_file)) THEN
    2653              :          iw = cp_print_key_unit_nr(logger, input, "DFT%PRINT%AO_MATRICES/COMMUTATOR_HR", &
    2654            0 :                                    extension=".Log")
    2655            0 :          CALL section_vals_val_get(input, "DFT%PRINT%AO_MATRICES%NDIGITS", i_val=after)
    2656            0 :          NULLIFY (matrix_hr)
    2657            0 :          CALL build_com_hr_matrix(qs_env, matrix_hr)
    2658            0 :          after = MIN(MAX(after, 1), 16)
    2659            0 :          DO img = 1, 3
    2660              :             CALL cp_dbcsr_write_sparse_matrix(matrix_hr(img)%matrix, 4, after, qs_env, &
    2661            0 :                                               para_env, output_unit=iw, omit_headers=omit_headers)
    2662              :          END DO
    2663            0 :          CALL dbcsr_deallocate_matrix_set(matrix_hr)
    2664              :          CALL cp_print_key_finished_output(iw, logger, input, &
    2665            0 :                                            "DFT%PRINT%AO_MATRICES/COMMUTATOR_HR")
    2666              :       END IF
    2667              : 
    2668              :       ! Compute the Mulliken charges
    2669        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%MULLIKEN")
    2670        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2671         4792 :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MULLIKEN", extension=".mulliken", log_filename=.FALSE.)
    2672         4792 :          print_level = 1
    2673         4792 :          CALL section_vals_val_get(print_key, "PRINT_GOP", l_val=print_it)
    2674         4792 :          IF (print_it) print_level = 2
    2675         4792 :          CALL section_vals_val_get(print_key, "PRINT_ALL", l_val=print_it)
    2676         4792 :          IF (print_it) print_level = 3
    2677         4792 :          CALL mulliken_population_analysis(qs_env, unit_nr, print_level)
    2678         4792 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MULLIKEN")
    2679              :       END IF
    2680              : 
    2681              :       ! Compute the Hirshfeld charges
    2682        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%HIRSHFELD")
    2683        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2684              :          ! we check if real space density is available
    2685         4864 :          NULLIFY (rho)
    2686         4864 :          CALL get_qs_env(qs_env=qs_env, rho=rho)
    2687         4864 :          CALL qs_rho_get(rho, rho_r_valid=rho_r_valid)
    2688         4864 :          IF (rho_r_valid) THEN
    2689         4790 :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%HIRSHFELD", extension=".hirshfeld", log_filename=.FALSE.)
    2690         4790 :             CALL hirshfeld_charges(qs_env, print_key, unit_nr)
    2691         4790 :             CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%HIRSHFELD")
    2692              :          END IF
    2693              :       END IF
    2694              : 
    2695              :       ! Compute EEQ charges
    2696        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%EEQ_CHARGES")
    2697        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2698           30 :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%EEQ_CHARGES", extension=".eeq", log_filename=.FALSE.)
    2699           30 :          print_level = 1
    2700           30 :          CALL eeq_print(qs_env, unit_nr, print_level, ext=.FALSE.)
    2701           30 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MULLIKEN")
    2702              :       END IF
    2703              : 
    2704              :       ! Do a Voronoi Integration or write a compressed BQB File
    2705        11299 :       print_key_voro => section_vals_get_subs_vals(input, "DFT%PRINT%VORONOI")
    2706        11299 :       print_key_bqb => section_vals_get_subs_vals(input, "DFT%PRINT%E_DENSITY_BQB")
    2707        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key_voro), cp_p_file)) THEN
    2708           24 :          should_print_voro = 1
    2709              :       ELSE
    2710        11275 :          should_print_voro = 0
    2711              :       END IF
    2712        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key_bqb), cp_p_file)) THEN
    2713            2 :          should_print_bqb = 1
    2714              :       ELSE
    2715        11297 :          should_print_bqb = 0
    2716              :       END IF
    2717        11299 :       IF ((should_print_voro /= 0) .OR. (should_print_bqb /= 0)) THEN
    2718              : 
    2719              :          ! we check if real space density is available
    2720           26 :          NULLIFY (rho)
    2721           26 :          CALL get_qs_env(qs_env=qs_env, rho=rho)
    2722           26 :          CALL qs_rho_get(rho, rho_r_valid=rho_r_valid)
    2723           26 :          IF (rho_r_valid) THEN
    2724              : 
    2725           26 :             IF (dft_control%nspins > 1) THEN
    2726              :                CALL get_qs_env(qs_env=qs_env, &
    2727            0 :                                pw_env=pw_env)
    2728              :                CALL pw_env_get(pw_env=pw_env, &
    2729              :                                auxbas_pw_pool=auxbas_pw_pool, &
    2730            0 :                                pw_pools=pw_pools)
    2731            0 :                NULLIFY (mb_rho)
    2732            0 :                ALLOCATE (mb_rho)
    2733            0 :                CALL auxbas_pw_pool%create_pw(pw=mb_rho)
    2734            0 :                CALL pw_copy(rho_r(1), mb_rho)
    2735            0 :                CALL pw_axpy(rho_r(2), mb_rho)
    2736              :                !CALL voronoi_analysis(qs_env, rho_elec_rspace, print_key, unit_nr)
    2737              :             ELSE
    2738           26 :                mb_rho => rho_r(1)
    2739              :                !CALL voronoi_analysis( qs_env, rho_r(1), print_key, unit_nr )
    2740              :             END IF ! nspins
    2741              : 
    2742           26 :             IF (should_print_voro /= 0) THEN
    2743           24 :                CALL section_vals_val_get(print_key_voro, "OUTPUT_TEXT", l_val=voro_print_txt)
    2744           24 :                IF (voro_print_txt) THEN
    2745           24 :                   append_voro = section_get_lval(input, "DFT%PRINT%VORONOI%APPEND")
    2746           24 :                   my_pos_voro = "REWIND"
    2747           24 :                   IF (append_voro) THEN
    2748            0 :                      my_pos_voro = "APPEND"
    2749              :                   END IF
    2750              :                   unit_nr_voro = cp_print_key_unit_nr(logger, input, "DFT%PRINT%VORONOI", extension=".voronoi", &
    2751           24 :                                                       file_position=my_pos_voro, log_filename=.FALSE.)
    2752              :                ELSE
    2753            0 :                   unit_nr_voro = 0
    2754              :                END IF
    2755              :             ELSE
    2756            2 :                unit_nr_voro = 0
    2757              :             END IF
    2758              : 
    2759              :             CALL entry_voronoi_or_bqb(should_print_voro, should_print_bqb, print_key_voro, print_key_bqb, &
    2760           26 :                                       unit_nr_voro, qs_env, mb_rho)
    2761              : 
    2762           26 :             IF (dft_control%nspins > 1) THEN
    2763            0 :                CALL auxbas_pw_pool%give_back_pw(mb_rho)
    2764            0 :                DEALLOCATE (mb_rho)
    2765              :             END IF
    2766              : 
    2767           26 :             IF (unit_nr_voro > 0) THEN
    2768           12 :                CALL cp_print_key_finished_output(unit_nr_voro, logger, input, "DFT%PRINT%VORONOI")
    2769              :             END IF
    2770              : 
    2771              :          END IF
    2772              :       END IF
    2773              : 
    2774              :       ! MAO analysis
    2775        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%MAO_ANALYSIS")
    2776        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2777           38 :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MAO_ANALYSIS", extension=".mao", log_filename=.FALSE.)
    2778           38 :          CALL mao_analysis(qs_env, print_key, unit_nr)
    2779           38 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MAO_ANALYSIS")
    2780              :       END IF
    2781              : 
    2782              :       ! MINBAS analysis
    2783        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%MINBAS_ANALYSIS")
    2784        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2785           28 :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MINBAS_ANALYSIS", extension=".mao", log_filename=.FALSE.)
    2786           28 :          CALL minbas_analysis(qs_env, print_key, unit_nr)
    2787           28 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MINBAS_ANALYSIS")
    2788              :       END IF
    2789              : 
    2790              :       ! IAO analysis
    2791        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%IAO_ANALYSIS")
    2792        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2793           32 :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%IAO_ANALYSIS", extension=".iao", log_filename=.FALSE.)
    2794           32 :          CALL iao_read_input(iao_env, print_key, cell)
    2795           32 :          IF (iao_env%do_iao) THEN
    2796            4 :             CALL iao_wfn_analysis(qs_env, iao_env, unit_nr)
    2797              :          END IF
    2798           32 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%IAO_ANALYSIS")
    2799              :       END IF
    2800              : 
    2801              :       ! Energy Decomposition Analysis
    2802        11299 :       print_key => section_vals_get_subs_vals(input, "DFT%PRINT%ENERGY_DECOMPOSITION_ANALYSIS")
    2803        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
    2804              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%ENERGY_DECOMPOSITION_ANALYSIS", &
    2805           58 :                                         extension=".mao", log_filename=.FALSE.)
    2806           58 :          CALL edmf_analysis(qs_env, print_key, unit_nr)
    2807           58 :          CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%ENERGY_DECOMPOSITION_ANALYSIS")
    2808              :       END IF
    2809              : 
    2810              :       ! Print the density in the RI-HFX basis
    2811        11299 :       hfx_section => section_vals_get_subs_vals(input, "DFT%XC%HF")
    2812        11299 :       CALL section_vals_get(hfx_section, explicit=do_hfx)
    2813        11299 :       CALL section_vals_get(hfx_section, n_repetition=n_rep_hf)
    2814        11299 :       IF (do_hfx) THEN
    2815         4526 :          DO i = 1, n_rep_hf
    2816         4526 :             IF (qs_env%x_data(i, 1)%do_hfx_ri) CALL print_ri_hfx(qs_env%x_data(i, 1)%ri_data, qs_env)
    2817              :          END DO
    2818              :       END IF
    2819              : 
    2820        11299 :       DEALLOCATE (zcharge)
    2821              : 
    2822        11299 :       CALL timestop(handle)
    2823              : 
    2824        45196 :    END SUBROUTINE write_mo_free_results
    2825              : 
    2826              : ! **************************************************************************************************
    2827              : !> \brief Calculates Hirshfeld charges
    2828              : !> \param qs_env the qs_env where to calculate the charges
    2829              : !> \param input_section the input section for Hirshfeld charges
    2830              : !> \param unit_nr the output unit number
    2831              : ! **************************************************************************************************
    2832         4790 :    SUBROUTINE hirshfeld_charges(qs_env, input_section, unit_nr)
    2833              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    2834              :       TYPE(section_vals_type), POINTER                   :: input_section
    2835              :       INTEGER, INTENT(IN)                                :: unit_nr
    2836              : 
    2837              :       INTEGER                                            :: i, iat, ikind, natom, nkind, nspin, &
    2838              :                                                             radius_type, refc, shapef
    2839         4790 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list
    2840              :       LOGICAL                                            :: do_radius, do_sc, paw_atom
    2841              :       REAL(KIND=dp)                                      :: zeff
    2842         4790 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: radii
    2843         4790 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: charges
    2844         4790 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    2845              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
    2846         4790 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_p, matrix_s
    2847              :       TYPE(dft_control_type), POINTER                    :: dft_control
    2848              :       TYPE(hirshfeld_type), POINTER                      :: hirshfeld_env
    2849              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    2850         4790 :       TYPE(mpole_rho_atom), DIMENSION(:), POINTER        :: mp_rho
    2851         4790 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    2852         4790 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    2853              :       TYPE(qs_rho_type), POINTER                         :: rho
    2854              :       TYPE(rho0_mpole_type), POINTER                     :: rho0_mpole
    2855              : 
    2856         4790 :       NULLIFY (hirshfeld_env)
    2857         4790 :       NULLIFY (radii)
    2858         4790 :       CALL create_hirshfeld_type(hirshfeld_env)
    2859              :       !
    2860         4790 :       CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
    2861        14370 :       ALLOCATE (hirshfeld_env%charges(natom))
    2862              :       ! input options
    2863         4790 :       CALL section_vals_val_get(input_section, "SELF_CONSISTENT", l_val=do_sc)
    2864         4790 :       CALL section_vals_val_get(input_section, "USER_RADIUS", l_val=do_radius)
    2865         4790 :       CALL section_vals_val_get(input_section, "SHAPE_FUNCTION", i_val=shapef)
    2866         4790 :       CALL section_vals_val_get(input_section, "REFERENCE_CHARGE", i_val=refc)
    2867         4790 :       IF (do_radius) THEN
    2868            0 :          radius_type = radius_user
    2869            0 :          CALL section_vals_val_get(input_section, "ATOMIC_RADII", r_vals=radii)
    2870            0 :          IF (.NOT. SIZE(radii) == nkind) &
    2871              :             CALL cp_abort(__LOCATION__, &
    2872              :                           "Length of keyword HIRSHFELD\ATOMIC_RADII does not "// &
    2873            0 :                           "match number of atomic kinds in the input coordinate file.")
    2874              :       ELSE
    2875         4790 :          radius_type = radius_covalent
    2876              :       END IF
    2877              :       CALL set_hirshfeld_info(hirshfeld_env, shape_function_type=shapef, &
    2878              :                               iterative=do_sc, ref_charge=refc, &
    2879         4790 :                               radius_type=radius_type)
    2880              :       ! shape function
    2881         4790 :       CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, atomic_kind_set=atomic_kind_set)
    2882              :       CALL create_shape_function(hirshfeld_env, qs_kind_set, atomic_kind_set, &
    2883         4790 :                                  radii_list=radii)
    2884              :       ! reference charges
    2885         4790 :       CALL get_qs_env(qs_env, rho=rho)
    2886         4790 :       CALL qs_rho_get(rho, rho_ao_kp=matrix_p)
    2887         4790 :       nspin = SIZE(matrix_p, 1)
    2888        19160 :       ALLOCATE (charges(natom, nspin))
    2889         4778 :       SELECT CASE (refc)
    2890              :       CASE (ref_charge_atomic)
    2891        13086 :          DO ikind = 1, nkind
    2892         8308 :             CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff)
    2893         8308 :             atomic_kind => atomic_kind_set(ikind)
    2894         8308 :             CALL get_atomic_kind(atomic_kind, atom_list=atom_list)
    2895        41380 :             DO iat = 1, SIZE(atom_list)
    2896        19986 :                i = atom_list(iat)
    2897        28294 :                hirshfeld_env%charges(i) = zeff
    2898              :             END DO
    2899              :          END DO
    2900              :       CASE (ref_charge_mulliken)
    2901           12 :          CALL get_qs_env(qs_env, matrix_s_kp=matrix_s, para_env=para_env)
    2902           12 :          CALL mulliken_charges(matrix_p, matrix_s, para_env, charges)
    2903           48 :          DO iat = 1, natom
    2904          108 :             hirshfeld_env%charges(iat) = SUM(charges(iat, :))
    2905              :          END DO
    2906              :       CASE DEFAULT
    2907         4790 :          CPABORT("Unknown type of reference charge for Hirshfeld partitioning.")
    2908              :       END SELECT
    2909              :       !
    2910        33314 :       charges = 0.0_dp
    2911         4790 :       IF (hirshfeld_env%iterative) THEN
    2912              :          ! Hirshfeld-I charges
    2913           22 :          CALL comp_hirshfeld_i_charges(qs_env, hirshfeld_env, charges, unit_nr)
    2914              :       ELSE
    2915              :          ! Hirshfeld charges
    2916         4768 :          CALL comp_hirshfeld_charges(qs_env, hirshfeld_env, charges)
    2917              :       END IF
    2918         4790 :       CALL get_qs_env(qs_env, particle_set=particle_set, dft_control=dft_control)
    2919         4790 :       IF (dft_control%qs_control%gapw) THEN
    2920              :          ! GAPW: add core charges (rho_hard - rho_soft)
    2921          698 :          CALL get_qs_env(qs_env, rho0_mpole=rho0_mpole)
    2922          698 :          CALL get_rho0_mpole(rho0_mpole, mp_rho=mp_rho)
    2923         3088 :          DO iat = 1, natom
    2924         2390 :             atomic_kind => particle_set(iat)%atomic_kind
    2925         2390 :             CALL get_atomic_kind(atomic_kind, kind_number=ikind)
    2926         2390 :             CALL get_qs_kind(qs_kind_set(ikind), paw_atom=paw_atom)
    2927         3088 :             IF (paw_atom) THEN
    2928         4574 :                charges(iat, 1:nspin) = charges(iat, 1:nspin) + mp_rho(iat)%q0(1:nspin)
    2929              :             END IF
    2930              :          END DO
    2931              :       END IF
    2932              :       !
    2933         4790 :       IF (unit_nr > 0) THEN
    2934              :          CALL write_hirshfeld_charges(charges, hirshfeld_env, particle_set, &
    2935         2409 :                                       qs_kind_set, unit_nr)
    2936              :       END IF
    2937              :       ! Save the charges to the results under the tag [HIRSHFELD-CHARGES]
    2938         4790 :       CALL save_hirshfeld_charges(charges, particle_set, qs_kind_set, qs_env)
    2939              :       !
    2940         4790 :       CALL release_hirshfeld_type(hirshfeld_env)
    2941         4790 :       DEALLOCATE (charges)
    2942              : 
    2943         9580 :    END SUBROUTINE hirshfeld_charges
    2944              : 
    2945              : ! **************************************************************************************************
    2946              : !> \brief ...
    2947              : !> \param ca ...
    2948              : !> \param a ...
    2949              : !> \param cb ...
    2950              : !> \param b ...
    2951              : !> \param l ...
    2952              : ! **************************************************************************************************
    2953            4 :    SUBROUTINE project_function_a(ca, a, cb, b, l)
    2954              :       ! project function cb on ca
    2955              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: ca
    2956              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: a, cb, b
    2957              :       INTEGER, INTENT(IN)                                :: l
    2958              : 
    2959              :       INTEGER                                            :: info, n
    2960            4 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipiv
    2961            4 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: smat, tmat, v
    2962              : 
    2963            4 :       n = SIZE(ca)
    2964           40 :       ALLOCATE (smat(n, n), tmat(n, n), v(n, 1), ipiv(n))
    2965              : 
    2966            4 :       CALL sg_overlap(smat, l, a, a)
    2967            4 :       CALL sg_overlap(tmat, l, a, b)
    2968         1252 :       v(:, 1) = MATMUL(tmat, cb)
    2969            4 :       CALL dgesv(n, 1, smat, n, ipiv, v, n, info)
    2970            4 :       CPASSERT(info == 0)
    2971           52 :       ca(:) = v(:, 1)
    2972              : 
    2973            4 :       DEALLOCATE (smat, tmat, v, ipiv)
    2974              : 
    2975            4 :    END SUBROUTINE project_function_a
    2976              : 
    2977              : ! **************************************************************************************************
    2978              : !> \brief ...
    2979              : !> \param ca ...
    2980              : !> \param a ...
    2981              : !> \param bfun ...
    2982              : !> \param grid_atom ...
    2983              : !> \param l ...
    2984              : ! **************************************************************************************************
    2985           36 :    SUBROUTINE project_function_b(ca, a, bfun, grid_atom, l)
    2986              :       ! project function f on ca
    2987              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: ca
    2988              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: a, bfun
    2989              :       TYPE(grid_atom_type), POINTER                      :: grid_atom
    2990              :       INTEGER, INTENT(IN)                                :: l
    2991              : 
    2992              :       INTEGER                                            :: i, info, n, nr
    2993           36 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipiv
    2994           36 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: afun
    2995           36 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: smat, v
    2996              : 
    2997           36 :       n = SIZE(ca)
    2998           36 :       nr = grid_atom%nr
    2999          360 :       ALLOCATE (smat(n, n), v(n, 1), ipiv(n), afun(nr))
    3000              : 
    3001           36 :       CALL sg_overlap(smat, l, a, a)
    3002          468 :       DO i = 1, n
    3003        22032 :          afun(:) = grid_atom%rad(:)**l*EXP(-a(i)*grid_atom%rad2(:))
    3004        22068 :          v(i, 1) = SUM(afun(:)*bfun(:)*grid_atom%wr(:))
    3005              :       END DO
    3006           36 :       CALL dgesv(n, 1, smat, n, ipiv, v, n, info)
    3007           36 :       CPASSERT(info == 0)
    3008          468 :       ca(:) = v(:, 1)
    3009              : 
    3010           36 :       DEALLOCATE (smat, v, ipiv, afun)
    3011              : 
    3012           36 :    END SUBROUTINE project_function_b
    3013              : 
    3014              : ! **************************************************************************************************
    3015              : !> \brief Performs printing of cube files from local energy
    3016              : !> \param input input
    3017              : !> \param logger the logger
    3018              : !> \param qs_env the qs_env in which the qs_env lives
    3019              : !> \par History
    3020              : !>      07.2019 created
    3021              : !> \author JGH
    3022              : ! **************************************************************************************************
    3023        11299 :    SUBROUTINE qs_scf_post_local_energy(input, logger, qs_env)
    3024              :       TYPE(section_vals_type), POINTER                   :: input
    3025              :       TYPE(cp_logger_type), POINTER                      :: logger
    3026              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3027              : 
    3028              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_local_energy'
    3029              : 
    3030              :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube
    3031              :       INTEGER                                            :: handle, io_unit, natom, unit_nr
    3032              :       LOGICAL                                            :: append_cube, gapw, gapw_xc, mpi_io
    3033              :       TYPE(dft_control_type), POINTER                    :: dft_control
    3034              :       TYPE(particle_list_type), POINTER                  :: particles
    3035              :       TYPE(pw_env_type), POINTER                         :: pw_env
    3036              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    3037              :       TYPE(pw_r3d_rs_type)                               :: eden
    3038              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    3039              :       TYPE(section_vals_type), POINTER                   :: dft_section
    3040              : 
    3041        11299 :       CALL timeset(routineN, handle)
    3042        11299 :       io_unit = cp_logger_get_default_io_unit(logger)
    3043        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    3044              :                                            "DFT%PRINT%LOCAL_ENERGY_CUBE"), cp_p_file)) THEN
    3045           32 :          dft_section => section_vals_get_subs_vals(input, "DFT")
    3046           32 :          CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, natom=natom)
    3047           32 :          gapw = dft_control%qs_control%gapw
    3048           32 :          gapw_xc = dft_control%qs_control%gapw_xc
    3049           32 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, subsys=subsys)
    3050           32 :          CALL qs_subsys_get(subsys, particles=particles)
    3051           32 :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    3052           32 :          CALL auxbas_pw_pool%create_pw(eden)
    3053              :          !
    3054           32 :          CALL qs_local_energy(qs_env, eden)
    3055              :          !
    3056           32 :          append_cube = section_get_lval(input, "DFT%PRINT%LOCAL_ENERGY_CUBE%APPEND")
    3057           32 :          IF (append_cube) THEN
    3058            0 :             my_pos_cube = "APPEND"
    3059              :          ELSE
    3060           32 :             my_pos_cube = "REWIND"
    3061              :          END IF
    3062           32 :          mpi_io = .TRUE.
    3063              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%LOCAL_ENERGY_CUBE", &
    3064              :                                         extension=".cube", middle_name="local_energy", &
    3065           32 :                                         file_position=my_pos_cube, mpi_io=mpi_io)
    3066              :          CALL cp_pw_to_cube(eden, &
    3067              :                             unit_nr, "LOCAL ENERGY", particles=particles, &
    3068              :                             stride=section_get_ivals(dft_section, &
    3069           32 :                                                      "PRINT%LOCAL_ENERGY_CUBE%STRIDE"), mpi_io=mpi_io)
    3070           32 :          IF (io_unit > 0) THEN
    3071           16 :             INQUIRE (UNIT=unit_nr, NAME=filename)
    3072           16 :             IF (gapw .OR. gapw_xc) THEN
    3073              :                WRITE (UNIT=io_unit, FMT="(/,T3,A,A)") &
    3074            0 :                   "The soft part of the local energy is written to the file: ", TRIM(ADJUSTL(filename))
    3075              :             ELSE
    3076              :                WRITE (UNIT=io_unit, FMT="(/,T3,A,A)") &
    3077           16 :                   "The local energy is written to the file: ", TRIM(ADJUSTL(filename))
    3078              :             END IF
    3079              :          END IF
    3080              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    3081           32 :                                            "DFT%PRINT%LOCAL_ENERGY_CUBE", mpi_io=mpi_io)
    3082              :          !
    3083           32 :          CALL auxbas_pw_pool%give_back_pw(eden)
    3084              :       END IF
    3085        11299 :       CALL timestop(handle)
    3086              : 
    3087        11299 :    END SUBROUTINE qs_scf_post_local_energy
    3088              : 
    3089              : ! **************************************************************************************************
    3090              : !> \brief Performs printing of cube files from local energy
    3091              : !> \param input input
    3092              : !> \param logger the logger
    3093              : !> \param qs_env the qs_env in which the qs_env lives
    3094              : !> \par History
    3095              : !>      07.2019 created
    3096              : !> \author JGH
    3097              : ! **************************************************************************************************
    3098        11299 :    SUBROUTINE qs_scf_post_local_stress(input, logger, qs_env)
    3099              :       TYPE(section_vals_type), POINTER                   :: input
    3100              :       TYPE(cp_logger_type), POINTER                      :: logger
    3101              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3102              : 
    3103              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_local_stress'
    3104              : 
    3105              :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube
    3106              :       INTEGER                                            :: handle, io_unit, natom, unit_nr
    3107              :       LOGICAL                                            :: append_cube, gapw, gapw_xc, mpi_io
    3108              :       REAL(KIND=dp)                                      :: beta
    3109              :       TYPE(dft_control_type), POINTER                    :: dft_control
    3110              :       TYPE(particle_list_type), POINTER                  :: particles
    3111              :       TYPE(pw_env_type), POINTER                         :: pw_env
    3112              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    3113              :       TYPE(pw_r3d_rs_type)                               :: stress
    3114              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    3115              :       TYPE(section_vals_type), POINTER                   :: dft_section
    3116              : 
    3117        11299 :       CALL timeset(routineN, handle)
    3118        11299 :       io_unit = cp_logger_get_default_io_unit(logger)
    3119        11299 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, input, &
    3120              :                                            "DFT%PRINT%LOCAL_STRESS_CUBE"), cp_p_file)) THEN
    3121           30 :          dft_section => section_vals_get_subs_vals(input, "DFT")
    3122           30 :          CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, natom=natom)
    3123           30 :          gapw = dft_control%qs_control%gapw
    3124           30 :          gapw_xc = dft_control%qs_control%gapw_xc
    3125           30 :          CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, subsys=subsys)
    3126           30 :          CALL qs_subsys_get(subsys, particles=particles)
    3127           30 :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    3128           30 :          CALL auxbas_pw_pool%create_pw(stress)
    3129              :          !
    3130              :          ! use beta=0: kinetic energy density in symmetric form
    3131           30 :          beta = 0.0_dp
    3132           30 :          CALL qs_local_stress(qs_env, beta=beta)
    3133              :          !
    3134           30 :          append_cube = section_get_lval(input, "DFT%PRINT%LOCAL_STRESS_CUBE%APPEND")
    3135           30 :          IF (append_cube) THEN
    3136            0 :             my_pos_cube = "APPEND"
    3137              :          ELSE
    3138           30 :             my_pos_cube = "REWIND"
    3139              :          END IF
    3140           30 :          mpi_io = .TRUE.
    3141              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%LOCAL_STRESS_CUBE", &
    3142              :                                         extension=".cube", middle_name="local_stress", &
    3143           30 :                                         file_position=my_pos_cube, mpi_io=mpi_io)
    3144              :          CALL cp_pw_to_cube(stress, &
    3145              :                             unit_nr, "LOCAL STRESS", particles=particles, &
    3146              :                             stride=section_get_ivals(dft_section, &
    3147           30 :                                                      "PRINT%LOCAL_STRESS_CUBE%STRIDE"), mpi_io=mpi_io)
    3148           30 :          IF (io_unit > 0) THEN
    3149           15 :             INQUIRE (UNIT=unit_nr, NAME=filename)
    3150           15 :             WRITE (UNIT=io_unit, FMT="(/,T3,A)") "Write 1/3*Tr(sigma) to cube file"
    3151           15 :             IF (gapw .OR. gapw_xc) THEN
    3152              :                WRITE (UNIT=io_unit, FMT="(T3,A,A)") &
    3153            0 :                   "The soft part of the local stress is written to the file: ", TRIM(ADJUSTL(filename))
    3154              :             ELSE
    3155              :                WRITE (UNIT=io_unit, FMT="(T3,A,A)") &
    3156           15 :                   "The local stress is written to the file: ", TRIM(ADJUSTL(filename))
    3157              :             END IF
    3158              :          END IF
    3159              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    3160           30 :                                            "DFT%PRINT%LOCAL_STRESS_CUBE", mpi_io=mpi_io)
    3161              :          !
    3162           30 :          CALL auxbas_pw_pool%give_back_pw(stress)
    3163              :       END IF
    3164              : 
    3165        11299 :       CALL timestop(handle)
    3166              : 
    3167        11299 :    END SUBROUTINE qs_scf_post_local_stress
    3168              : 
    3169              : ! **************************************************************************************************
    3170              : !> \brief Performs printing of cube files related to the implicit Poisson solver
    3171              : !> \param input input
    3172              : !> \param logger the logger
    3173              : !> \param qs_env the qs_env in which the qs_env lives
    3174              : !> \par History
    3175              : !>      03.2016 refactored from write_mo_free_results [Hossein Bani-Hashemian]
    3176              : !> \author Mohammad Hossein Bani-Hashemian
    3177              : ! **************************************************************************************************
    3178        11299 :    SUBROUTINE qs_scf_post_ps_implicit(input, logger, qs_env)
    3179              :       TYPE(section_vals_type), POINTER                   :: input
    3180              :       TYPE(cp_logger_type), POINTER                      :: logger
    3181              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3182              : 
    3183              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_ps_implicit'
    3184              : 
    3185              :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube
    3186              :       INTEGER                                            :: boundary_condition, handle, i, j, &
    3187              :                                                             n_cstr, n_tiles, unit_nr
    3188              :       LOGICAL :: append_cube, do_cstr_charge_cube, do_dielectric_cube, do_dirichlet_bc_cube, &
    3189              :          has_dirichlet_bc, has_implicit_ps, mpi_io, tile_cubes
    3190              :       TYPE(particle_list_type), POINTER                  :: particles
    3191              :       TYPE(pw_env_type), POINTER                         :: pw_env
    3192              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
    3193              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    3194              :       TYPE(pw_r3d_rs_type)                               :: aux_r
    3195              :       TYPE(pw_r3d_rs_type), POINTER                      :: dirichlet_tile
    3196              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    3197              :       TYPE(section_vals_type), POINTER                   :: dft_section
    3198              : 
    3199        11299 :       CALL timeset(routineN, handle)
    3200              : 
    3201        11299 :       NULLIFY (pw_env, auxbas_pw_pool, dft_section, particles)
    3202              : 
    3203        11299 :       dft_section => section_vals_get_subs_vals(input, "DFT")
    3204        11299 :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, subsys=subsys)
    3205        11299 :       CALL qs_subsys_get(subsys, particles=particles)
    3206        11299 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
    3207              : 
    3208        11299 :       has_implicit_ps = .FALSE.
    3209        11299 :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env)
    3210        11299 :       IF (pw_env%poisson_env%parameters%solver == pw_poisson_implicit) has_implicit_ps = .TRUE.
    3211              : 
    3212              :       ! Write the dielectric constant into a cube file
    3213              :       do_dielectric_cube = BTEST(cp_print_key_should_output(logger%iter_info, input, &
    3214        11299 :                                                             "DFT%PRINT%IMPLICIT_PSOLVER%DIELECTRIC_CUBE"), cp_p_file)
    3215        11299 :       IF (has_implicit_ps .AND. do_dielectric_cube) THEN
    3216            0 :          append_cube = section_get_lval(input, "DFT%PRINT%IMPLICIT_PSOLVER%DIELECTRIC_CUBE%APPEND")
    3217            0 :          my_pos_cube = "REWIND"
    3218            0 :          IF (append_cube) THEN
    3219            0 :             my_pos_cube = "APPEND"
    3220              :          END IF
    3221            0 :          mpi_io = .TRUE.
    3222              :          unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%IMPLICIT_PSOLVER%DIELECTRIC_CUBE", &
    3223              :                                         extension=".cube", middle_name="DIELECTRIC_CONSTANT", file_position=my_pos_cube, &
    3224            0 :                                         mpi_io=mpi_io)
    3225            0 :          CALL pw_env_get(pw_env, poisson_env=poisson_env, auxbas_pw_pool=auxbas_pw_pool)
    3226            0 :          CALL auxbas_pw_pool%create_pw(aux_r)
    3227              : 
    3228            0 :          boundary_condition = pw_env%poisson_env%parameters%ps_implicit_params%boundary_condition
    3229            0 :          SELECT CASE (boundary_condition)
    3230              :          CASE (PERIODIC_BC, MIXED_PERIODIC_BC)
    3231            0 :             CALL pw_copy(poisson_env%implicit_env%dielectric%eps, aux_r)
    3232              :          CASE (MIXED_BC, NEUMANN_BC)
    3233              :             CALL pw_shrink(pw_env%poisson_env%parameters%ps_implicit_params%neumann_directions, &
    3234              :                            pw_env%poisson_env%implicit_env%dct_env%dests_shrink, &
    3235              :                            pw_env%poisson_env%implicit_env%dct_env%srcs_shrink, &
    3236              :                            pw_env%poisson_env%implicit_env%dct_env%bounds_local_shftd, &
    3237            0 :                            poisson_env%implicit_env%dielectric%eps, aux_r)
    3238              :          END SELECT
    3239              : 
    3240              :          CALL cp_pw_to_cube(aux_r, unit_nr, "DIELECTRIC CONSTANT", particles=particles, &
    3241              :                             stride=section_get_ivals(dft_section, "PRINT%IMPLICIT_PSOLVER%DIELECTRIC_CUBE%STRIDE"), &
    3242            0 :                             mpi_io=mpi_io)
    3243              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    3244            0 :                                            "DFT%PRINT%IMPLICIT_PSOLVER%DIELECTRIC_CUBE", mpi_io=mpi_io)
    3245              : 
    3246            0 :          CALL auxbas_pw_pool%give_back_pw(aux_r)
    3247              :       END IF
    3248              : 
    3249              :       ! Write Dirichlet constraint charges into a cube file
    3250              :       do_cstr_charge_cube = BTEST(cp_print_key_should_output(logger%iter_info, input, &
    3251        11299 :                                                              "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_CSTR_CHARGE_CUBE"), cp_p_file)
    3252              : 
    3253        11299 :       has_dirichlet_bc = .FALSE.
    3254        11299 :       IF (has_implicit_ps) THEN
    3255           86 :          boundary_condition = pw_env%poisson_env%parameters%ps_implicit_params%boundary_condition
    3256           86 :          IF (boundary_condition == MIXED_PERIODIC_BC .OR. boundary_condition == MIXED_BC) THEN
    3257           60 :             has_dirichlet_bc = .TRUE.
    3258              :          END IF
    3259              :       END IF
    3260              : 
    3261        11299 :       IF (has_implicit_ps .AND. do_cstr_charge_cube .AND. has_dirichlet_bc) THEN
    3262              :          append_cube = section_get_lval(input, &
    3263            0 :                                         "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_CSTR_CHARGE_CUBE%APPEND")
    3264            0 :          my_pos_cube = "REWIND"
    3265            0 :          IF (append_cube) THEN
    3266            0 :             my_pos_cube = "APPEND"
    3267              :          END IF
    3268            0 :          mpi_io = .TRUE.
    3269              :          unit_nr = cp_print_key_unit_nr(logger, input, &
    3270              :                                         "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_CSTR_CHARGE_CUBE", &
    3271              :                                         extension=".cube", middle_name="dirichlet_cstr_charge", file_position=my_pos_cube, &
    3272            0 :                                         mpi_io=mpi_io)
    3273            0 :          CALL pw_env_get(pw_env, poisson_env=poisson_env, auxbas_pw_pool=auxbas_pw_pool)
    3274            0 :          CALL auxbas_pw_pool%create_pw(aux_r)
    3275              : 
    3276            0 :          boundary_condition = pw_env%poisson_env%parameters%ps_implicit_params%boundary_condition
    3277            0 :          SELECT CASE (boundary_condition)
    3278              :          CASE (MIXED_PERIODIC_BC)
    3279            0 :             CALL pw_copy(poisson_env%implicit_env%cstr_charge, aux_r)
    3280              :          CASE (MIXED_BC)
    3281              :             CALL pw_shrink(pw_env%poisson_env%parameters%ps_implicit_params%neumann_directions, &
    3282              :                            pw_env%poisson_env%implicit_env%dct_env%dests_shrink, &
    3283              :                            pw_env%poisson_env%implicit_env%dct_env%srcs_shrink, &
    3284              :                            pw_env%poisson_env%implicit_env%dct_env%bounds_local_shftd, &
    3285            0 :                            poisson_env%implicit_env%cstr_charge, aux_r)
    3286              :          END SELECT
    3287              : 
    3288              :          CALL cp_pw_to_cube(aux_r, unit_nr, "DIRICHLET CONSTRAINT CHARGE", particles=particles, &
    3289              :                             stride=section_get_ivals(dft_section, "PRINT%IMPLICIT_PSOLVER%DIRICHLET_CSTR_CHARGE_CUBE%STRIDE"), &
    3290            0 :                             mpi_io=mpi_io)
    3291              :          CALL cp_print_key_finished_output(unit_nr, logger, input, &
    3292            0 :                                            "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_CSTR_CHARGE_CUBE", mpi_io=mpi_io)
    3293              : 
    3294            0 :          CALL auxbas_pw_pool%give_back_pw(aux_r)
    3295              :       END IF
    3296              : 
    3297              :       ! Write Dirichlet type constranits into cube files
    3298              :       do_dirichlet_bc_cube = BTEST(cp_print_key_should_output(logger%iter_info, input, &
    3299        11299 :                                                               "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE"), cp_p_file)
    3300        11299 :       has_dirichlet_bc = .FALSE.
    3301        11299 :       IF (has_implicit_ps) THEN
    3302           86 :          boundary_condition = pw_env%poisson_env%parameters%ps_implicit_params%boundary_condition
    3303           86 :          IF (boundary_condition == MIXED_PERIODIC_BC .OR. boundary_condition == MIXED_BC) THEN
    3304           60 :             has_dirichlet_bc = .TRUE.
    3305              :          END IF
    3306              :       END IF
    3307              : 
    3308        11299 :       IF (has_implicit_ps .AND. has_dirichlet_bc .AND. do_dirichlet_bc_cube) THEN
    3309            0 :          append_cube = section_get_lval(input, "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE%APPEND")
    3310            0 :          my_pos_cube = "REWIND"
    3311            0 :          IF (append_cube) THEN
    3312            0 :             my_pos_cube = "APPEND"
    3313              :          END IF
    3314            0 :          tile_cubes = section_get_lval(input, "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE%TILE_CUBES")
    3315              : 
    3316            0 :          CALL pw_env_get(pw_env, poisson_env=poisson_env, auxbas_pw_pool=auxbas_pw_pool)
    3317            0 :          CALL auxbas_pw_pool%create_pw(aux_r)
    3318            0 :          CALL pw_zero(aux_r)
    3319              : 
    3320            0 :          IF (tile_cubes) THEN
    3321              :             ! one cube file per tile
    3322            0 :             n_cstr = SIZE(poisson_env%implicit_env%contacts)
    3323            0 :             DO j = 1, n_cstr
    3324            0 :                n_tiles = poisson_env%implicit_env%contacts(j)%dirichlet_bc%n_tiles
    3325            0 :                DO i = 1, n_tiles
    3326              :                   filename = "dirichlet_cstr_"//TRIM(ADJUSTL(cp_to_string(j)))// &
    3327            0 :                              "_tile_"//TRIM(ADJUSTL(cp_to_string(i)))
    3328            0 :                   mpi_io = .TRUE.
    3329              :                   unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE", &
    3330              :                                                  extension=".cube", middle_name=filename, file_position=my_pos_cube, &
    3331            0 :                                                  mpi_io=mpi_io)
    3332              : 
    3333            0 :                   CALL pw_copy(poisson_env%implicit_env%contacts(j)%dirichlet_bc%tiles(i)%tile%tile_pw, aux_r)
    3334              : 
    3335              :                   CALL cp_pw_to_cube(aux_r, unit_nr, "DIRICHLET TYPE CONSTRAINT", particles=particles, &
    3336              :                                      stride=section_get_ivals(dft_section, "PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE%STRIDE"), &
    3337            0 :                                      mpi_io=mpi_io)
    3338              :                   CALL cp_print_key_finished_output(unit_nr, logger, input, &
    3339            0 :                                                     "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE", mpi_io=mpi_io)
    3340              :                END DO
    3341              :             END DO
    3342              :          ELSE
    3343              :             ! a single cube file
    3344            0 :             NULLIFY (dirichlet_tile)
    3345            0 :             ALLOCATE (dirichlet_tile)
    3346            0 :             CALL auxbas_pw_pool%create_pw(dirichlet_tile)
    3347            0 :             CALL pw_zero(dirichlet_tile)
    3348            0 :             mpi_io = .TRUE.
    3349              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE", &
    3350              :                                            extension=".cube", middle_name="DIRICHLET_CSTR", file_position=my_pos_cube, &
    3351            0 :                                            mpi_io=mpi_io)
    3352              : 
    3353            0 :             n_cstr = SIZE(poisson_env%implicit_env%contacts)
    3354            0 :             DO j = 1, n_cstr
    3355            0 :                n_tiles = poisson_env%implicit_env%contacts(j)%dirichlet_bc%n_tiles
    3356            0 :                DO i = 1, n_tiles
    3357            0 :                   CALL pw_copy(poisson_env%implicit_env%contacts(j)%dirichlet_bc%tiles(i)%tile%tile_pw, dirichlet_tile)
    3358            0 :                   CALL pw_axpy(dirichlet_tile, aux_r)
    3359              :                END DO
    3360              :             END DO
    3361              : 
    3362              :             CALL cp_pw_to_cube(aux_r, unit_nr, "DIRICHLET TYPE CONSTRAINT", particles=particles, &
    3363              :                                stride=section_get_ivals(dft_section, "PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE%STRIDE"), &
    3364            0 :                                mpi_io=mpi_io)
    3365              :             CALL cp_print_key_finished_output(unit_nr, logger, input, &
    3366            0 :                                               "DFT%PRINT%IMPLICIT_PSOLVER%DIRICHLET_BC_CUBE", mpi_io=mpi_io)
    3367            0 :             CALL auxbas_pw_pool%give_back_pw(dirichlet_tile)
    3368            0 :             DEALLOCATE (dirichlet_tile)
    3369              :          END IF
    3370              : 
    3371            0 :          CALL auxbas_pw_pool%give_back_pw(aux_r)
    3372              :       END IF
    3373              : 
    3374        11299 :       CALL timestop(handle)
    3375              : 
    3376        11299 :    END SUBROUTINE qs_scf_post_ps_implicit
    3377              : 
    3378              : !**************************************************************************************************
    3379              : !> \brief write an adjacency (interaction) matrix
    3380              : !> \param qs_env qs environment
    3381              : !> \param input the input
    3382              : !> \author Mohammad Hossein Bani-Hashemian
    3383              : ! **************************************************************************************************
    3384        11299 :    SUBROUTINE write_adjacency_matrix(qs_env, input)
    3385              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3386              :       TYPE(section_vals_type), POINTER                   :: input
    3387              : 
    3388              :       CHARACTER(len=*), PARAMETER :: routineN = 'write_adjacency_matrix'
    3389              : 
    3390              :       INTEGER                                            :: adjm_size, colind, handle, iatom, ikind, &
    3391              :                                                             ind, jatom, jkind, k, natom, nkind, &
    3392              :                                                             output_unit, rowind, unit_nr
    3393        11299 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: interact_adjm
    3394              :       LOGICAL                                            :: do_adjm_write, do_symmetric
    3395              :       TYPE(cp_logger_type), POINTER                      :: logger
    3396        11299 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list_a, basis_set_list_b
    3397              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a, basis_set_b
    3398              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    3399              :       TYPE(neighbor_list_iterator_p_type), &
    3400        11299 :          DIMENSION(:), POINTER                           :: nl_iterator
    3401              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
    3402        11299 :          POINTER                                         :: nl
    3403        11299 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
    3404              :       TYPE(section_vals_type), POINTER                   :: dft_section
    3405              : 
    3406        11299 :       CALL timeset(routineN, handle)
    3407              : 
    3408        11299 :       NULLIFY (dft_section)
    3409              : 
    3410        11299 :       logger => cp_get_default_logger()
    3411        11299 :       output_unit = cp_logger_get_default_io_unit(logger)
    3412              : 
    3413        11299 :       dft_section => section_vals_get_subs_vals(input, "DFT")
    3414              :       do_adjm_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
    3415        11299 :                                                        "PRINT%ADJMAT_WRITE"), cp_p_file)
    3416              : 
    3417        11299 :       IF (do_adjm_write) THEN
    3418           28 :          NULLIFY (qs_kind_set, nl_iterator)
    3419           28 :          NULLIFY (basis_set_list_a, basis_set_list_b, basis_set_a, basis_set_b)
    3420              : 
    3421           28 :          CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, sab_orb=nl, natom=natom, para_env=para_env)
    3422              : 
    3423           28 :          nkind = SIZE(qs_kind_set)
    3424           28 :          CPASSERT(SIZE(nl) > 0)
    3425           28 :          CALL get_neighbor_list_set_p(neighbor_list_sets=nl, symmetric=do_symmetric)
    3426           28 :          CPASSERT(do_symmetric)
    3427          216 :          ALLOCATE (basis_set_list_a(nkind), basis_set_list_b(nkind))
    3428           28 :          CALL basis_set_list_setup(basis_set_list_a, "ORB", qs_kind_set)
    3429           28 :          CALL basis_set_list_setup(basis_set_list_b, "ORB", qs_kind_set)
    3430              : 
    3431           28 :          adjm_size = ((natom + 1)*natom)/2
    3432           84 :          ALLOCATE (interact_adjm(4*adjm_size))
    3433          620 :          interact_adjm = 0
    3434              : 
    3435           28 :          NULLIFY (nl_iterator)
    3436           28 :          CALL neighbor_list_iterator_create(nl_iterator, nl)
    3437         2021 :          DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
    3438              :             CALL get_iterator_info(nl_iterator, &
    3439              :                                    ikind=ikind, jkind=jkind, &
    3440         1993 :                                    iatom=iatom, jatom=jatom)
    3441              : 
    3442         1993 :             basis_set_a => basis_set_list_a(ikind)%gto_basis_set
    3443         1993 :             IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
    3444         1993 :             basis_set_b => basis_set_list_b(jkind)%gto_basis_set
    3445         1993 :             IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
    3446              : 
    3447              :             ! move everything to the upper triangular part
    3448         1993 :             IF (iatom <= jatom) THEN
    3449              :                rowind = iatom
    3450              :                colind = jatom
    3451              :             ELSE
    3452          670 :                rowind = jatom
    3453          670 :                colind = iatom
    3454              :                ! swap the kinds too
    3455              :                ikind = ikind + jkind
    3456          670 :                jkind = ikind - jkind
    3457          670 :                ikind = ikind - jkind
    3458              :             END IF
    3459              : 
    3460              :             ! indexing upper triangular matrix
    3461         1993 :             ind = adjm_size - (natom - rowind + 1)*((natom - rowind + 1) + 1)/2 + colind - rowind + 1
    3462              :             ! convert the upper triangular matrix into a adjm_size x 4 matrix
    3463              :             ! columns are: iatom, jatom, ikind, jkind
    3464         1993 :             interact_adjm((ind - 1)*4 + 1) = rowind
    3465         1993 :             interact_adjm((ind - 1)*4 + 2) = colind
    3466         1993 :             interact_adjm((ind - 1)*4 + 3) = ikind
    3467         1993 :             interact_adjm((ind - 1)*4 + 4) = jkind
    3468              :          END DO
    3469              : 
    3470           28 :          CALL para_env%sum(interact_adjm)
    3471              : 
    3472              :          unit_nr = cp_print_key_unit_nr(logger, dft_section, "PRINT%ADJMAT_WRITE", &
    3473              :                                         extension=".adjmat", file_form="FORMATTED", &
    3474           28 :                                         file_status="REPLACE")
    3475           28 :          IF (unit_nr > 0) THEN
    3476           14 :             WRITE (unit_nr, "(1A,2X,1A,5X,1A,4X,A5,3X,A5)") "#", "iatom", "jatom", "ikind", "jkind"
    3477           88 :             DO k = 1, 4*adjm_size, 4
    3478              :                ! print only the interacting atoms
    3479           88 :                IF (interact_adjm(k) > 0 .AND. interact_adjm(k + 1) > 0) THEN
    3480           74 :                   WRITE (unit_nr, "(I8,2X,I8,3X,I6,2X,I6)") interact_adjm(k:k + 3)
    3481              :                END IF
    3482              :             END DO
    3483              :          END IF
    3484              : 
    3485           28 :          CALL cp_print_key_finished_output(unit_nr, logger, dft_section, "PRINT%ADJMAT_WRITE")
    3486              : 
    3487           28 :          CALL neighbor_list_iterator_release(nl_iterator)
    3488           56 :          DEALLOCATE (basis_set_list_a, basis_set_list_b)
    3489              :       END IF
    3490              : 
    3491        11299 :       CALL timestop(handle)
    3492              : 
    3493        22598 :    END SUBROUTINE write_adjacency_matrix
    3494              : 
    3495              : ! **************************************************************************************************
    3496              : !> \brief Updates Hartree potential with MP2 density. Important for REPEAT charges
    3497              : !> \param rho ...
    3498              : !> \param qs_env ...
    3499              : !> \author Vladimir Rybkin
    3500              : ! **************************************************************************************************
    3501          322 :    SUBROUTINE update_hartree_with_mp2(rho, qs_env)
    3502              :       TYPE(qs_rho_type), POINTER                         :: rho
    3503              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    3504              : 
    3505              :       LOGICAL                                            :: use_virial
    3506              :       TYPE(pw_c1d_gs_type)                               :: rho_tot_gspace, v_hartree_gspace
    3507              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho_core
    3508              :       TYPE(pw_env_type), POINTER                         :: pw_env
    3509              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
    3510              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
    3511              :       TYPE(pw_r3d_rs_type), POINTER                      :: v_hartree_rspace
    3512              :       TYPE(qs_energy_type), POINTER                      :: energy
    3513              :       TYPE(virial_type), POINTER                         :: virial
    3514              : 
    3515          322 :       NULLIFY (auxbas_pw_pool, pw_env, poisson_env, energy, rho_core, v_hartree_rspace, virial)
    3516              :       CALL get_qs_env(qs_env, pw_env=pw_env, energy=energy, &
    3517              :                       rho_core=rho_core, virial=virial, &
    3518          322 :                       v_hartree_rspace=v_hartree_rspace)
    3519              : 
    3520          322 :       use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
    3521              : 
    3522              :       IF (.NOT. use_virial) THEN
    3523              : 
    3524              :          CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
    3525          268 :                          poisson_env=poisson_env)
    3526          268 :          CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
    3527          268 :          CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
    3528              : 
    3529          268 :          CALL calc_rho_tot_gspace(rho_tot_gspace, qs_env, rho)
    3530              :          CALL pw_poisson_solve(poisson_env, rho_tot_gspace, energy%hartree, &
    3531          268 :                                v_hartree_gspace, rho_core=rho_core)
    3532              : 
    3533          268 :          CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
    3534          268 :          CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
    3535              : 
    3536          268 :          CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
    3537          268 :          CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
    3538              :       END IF
    3539              : 
    3540          322 :    END SUBROUTINE update_hartree_with_mp2
    3541              : 
    3542              : END MODULE qs_scf_post_gpw
        

Generated by: LCOV version 2.0-1