LCOV - code coverage report
Current view: top level - src - qs_scf_post_gpw.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:8ebf9ad) Lines: 87.7 % 1607 1409
Test Date: 2026-01-22 06:43:13 Functions: 97.0 % 33 32

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

Generated by: LCOV version 2.0-1