LCOV - code coverage report
Current view: top level - src/emd - rt_propagation_utils.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:262480d) Lines: 327 341 95.9 %
Date: 2024-11-22 07:00:40 Functions: 8 8 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 CP2K developers group <https://cp2k.org>                                   !
       4             : !                                                                                                  !
       5             : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6             : !--------------------------------------------------------------------------------------------------!
       7             : 
       8             : ! **************************************************************************************************
       9             : !> \brief Routines needed for EMD
      10             : !> \author Florian Schiffmann (02.09)
      11             : ! **************************************************************************************************
      12             : 
      13             : MODULE rt_propagation_utils
      14             :    USE atomic_kind_types,               ONLY: atomic_kind_type
      15             :    USE cell_types,                      ONLY: cell_type
      16             :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      17             :    USE cp_control_types,                ONLY: dft_control_type,&
      18             :                                               rtp_control_type
      19             :    USE cp_dbcsr_api,                    ONLY: &
      20             :         dbcsr_add, dbcsr_binary_read, dbcsr_checksum, dbcsr_copy, dbcsr_create, &
      21             :         dbcsr_deallocate_matrix, dbcsr_desymmetrize, dbcsr_distribution_type, dbcsr_filter, &
      22             :         dbcsr_get_info, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
      23             :         dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_p_type, dbcsr_scale, &
      24             :         dbcsr_set, dbcsr_type
      25             :    USE cp_dbcsr_operations,             ONLY: cp_dbcsr_plus_fm_fm_t,&
      26             :                                               dbcsr_deallocate_matrix_set
      27             :    USE cp_fm_basic_linalg,              ONLY: cp_fm_column_scale
      28             :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      29             :                                               cp_fm_get_info,&
      30             :                                               cp_fm_release,&
      31             :                                               cp_fm_set_all,&
      32             :                                               cp_fm_to_fm,&
      33             :                                               cp_fm_type
      34             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      35             :                                               cp_logger_get_default_io_unit,&
      36             :                                               cp_logger_get_default_unit_nr,&
      37             :                                               cp_logger_type
      38             :    USE cp_output_handling,              ONLY: cp_p_file,&
      39             :                                               cp_print_key_finished_output,&
      40             :                                               cp_print_key_should_output,&
      41             :                                               cp_print_key_unit_nr
      42             :    USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
      43             :    USE input_constants,                 ONLY: use_restart_wfn,&
      44             :                                               use_rt_restart
      45             :    USE input_section_types,             ONLY: section_get_ival,&
      46             :                                               section_get_ivals,&
      47             :                                               section_get_lval,&
      48             :                                               section_vals_get_subs_vals,&
      49             :                                               section_vals_type,&
      50             :                                               section_vals_val_get
      51             :    USE kinds,                           ONLY: default_path_length,&
      52             :                                               default_string_length,&
      53             :                                               dp
      54             :    USE mathconstants,                   ONLY: zero
      55             :    USE memory_utilities,                ONLY: reallocate
      56             :    USE message_passing,                 ONLY: mp_para_env_type
      57             :    USE orbital_pointers,                ONLY: ncoset
      58             :    USE particle_list_types,             ONLY: particle_list_type
      59             :    USE particle_types,                  ONLY: particle_type
      60             :    USE pw_env_types,                    ONLY: pw_env_get,&
      61             :                                               pw_env_type
      62             :    USE pw_methods,                      ONLY: pw_multiply,&
      63             :                                               pw_zero
      64             :    USE pw_pool_types,                   ONLY: pw_pool_p_type,&
      65             :                                               pw_pool_type
      66             :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      67             :                                               pw_r3d_rs_type
      68             :    USE qs_collocate_density,            ONLY: calculate_wavefunction
      69             :    USE qs_density_matrices,             ONLY: calculate_density_matrix
      70             :    USE qs_dftb_matrices,                ONLY: build_dftb_overlap
      71             :    USE qs_environment_types,            ONLY: get_qs_env,&
      72             :                                               qs_environment_type
      73             :    USE qs_kind_types,                   ONLY: qs_kind_type
      74             :    USE qs_ks_types,                     ONLY: qs_ks_did_change,&
      75             :                                               qs_ks_env_type
      76             :    USE qs_mo_io,                        ONLY: read_mo_set_from_restart,&
      77             :                                               read_rt_mos_from_restart,&
      78             :                                               write_mo_set_to_output_unit
      79             :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
      80             :                                               deallocate_mo_set,&
      81             :                                               get_mo_set,&
      82             :                                               init_mo_set,&
      83             :                                               mo_set_type
      84             :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
      85             :    USE qs_overlap,                      ONLY: build_overlap_matrix
      86             :    USE qs_rho_methods,                  ONLY: qs_rho_update_rho
      87             :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      88             :                                               qs_rho_set,&
      89             :                                               qs_rho_type
      90             :    USE qs_scf_wfn_mix,                  ONLY: wfn_mix
      91             :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
      92             :                                               qs_subsys_type
      93             :    USE rt_propagation_types,            ONLY: get_rtp,&
      94             :                                               rt_prop_type
      95             : #include "../base/base_uses.f90"
      96             : 
      97             :    IMPLICIT NONE
      98             :    PRIVATE
      99             : 
     100             :    PUBLIC :: get_restart_wfn, &
     101             :              calc_S_derivs, &
     102             :              calc_update_rho, &
     103             :              calc_update_rho_sparse, &
     104             :              calculate_P_imaginary, &
     105             :              write_rtp_mos_to_output_unit, &
     106             :              write_rtp_mo_cubes
     107             : 
     108             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation_utils'
     109             : 
     110             : CONTAINS
     111             : 
     112             : ! **************************************************************************************************
     113             : !> \brief Calculates dS/dR respectily the velocity weighted derivatves
     114             : !>        only needed for ehrenfest MD.
     115             : !>
     116             : !> \param qs_env the qs environment
     117             : !> \par History
     118             : !>      02.2009 created [Manuel Guidon]
     119             : !>      02.2014 switched to dbcsr matrices [Samuel Andermatt]
     120             : !> \author Florian Schiffmann
     121             : ! **************************************************************************************************
     122        1222 :    SUBROUTINE calc_S_derivs(qs_env)
     123             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     124             : 
     125             :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'calc_S_derivs'
     126             :       REAL(KIND=dp), PARAMETER                           :: one = 1.0_dp, zero = 0.0_dp
     127             : 
     128             :       INTEGER                                            :: col_atom, handle, i, j, m, maxder, n, &
     129             :                                                             nder, row_atom
     130             :       INTEGER, DIMENSION(6, 2)                           :: c_map_mat
     131             :       LOGICAL                                            :: return_s_derivatives
     132        1222 :       REAL(dp), DIMENSION(:), POINTER                    :: block_values
     133             :       TYPE(dbcsr_iterator_type)                          :: iter
     134        1222 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: C_mat, S_der, s_derivs
     135             :       TYPE(dbcsr_type), POINTER                          :: B_mat, tmp_mat, tmp_mat2
     136             :       TYPE(dft_control_type), POINTER                    :: dft_control
     137             :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     138        1222 :          POINTER                                         :: sab_orb
     139        1222 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     140             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     141             :       TYPE(rt_prop_type), POINTER                        :: rtp
     142             : 
     143        1222 :       CALL timeset(routineN, handle)
     144             : 
     145        1222 :       return_s_derivatives = .TRUE.
     146             : 
     147        1222 :       NULLIFY (particle_set)
     148        1222 :       NULLIFY (rtp)
     149        1222 :       NULLIFY (s_derivs)
     150        1222 :       NULLIFY (dft_control)
     151        1222 :       NULLIFY (ks_env)
     152             : 
     153             :       CALL get_qs_env(qs_env=qs_env, &
     154             :                       rtp=rtp, &
     155             :                       particle_set=particle_set, &
     156             :                       sab_orb=sab_orb, &
     157             :                       dft_control=dft_control, &
     158        1222 :                       ks_env=ks_env)
     159             : 
     160        1222 :       CALL get_rtp(rtp=rtp, B_mat=B_mat, C_mat=C_mat, S_der=S_der)
     161             : 
     162        1222 :       nder = 2
     163        1222 :       maxder = ncoset(nder)
     164             : 
     165             :       NULLIFY (tmp_mat)
     166        1222 :       ALLOCATE (tmp_mat)
     167        1222 :       CALL dbcsr_create(tmp_mat, template=S_der(1)%matrix, matrix_type="N")
     168             : 
     169        1222 :       IF (rtp%iter < 2) THEN
     170             :          ! calculate the overlap derivative matrices
     171         342 :          IF (dft_control%qs_control%dftb) THEN
     172          84 :             CALL build_dftb_overlap(qs_env, nder, s_derivs)
     173             :          ELSE
     174             :             CALL build_overlap_matrix(ks_env, nderivative=nder, matrix_s=s_derivs, &
     175         258 :                                       basis_type_a="ORB", basis_type_b="ORB", sab_nl=sab_orb)
     176             :          END IF
     177             : 
     178             :          NULLIFY (tmp_mat2)
     179         342 :          ALLOCATE (tmp_mat2)
     180         342 :          CALL dbcsr_create(tmp_mat2, template=S_der(1)%matrix, matrix_type="S")
     181        3420 :          DO m = 1, 9
     182        3078 :             CALL dbcsr_copy(tmp_mat2, s_derivs(m + 1)%matrix)
     183        3078 :             CALL dbcsr_desymmetrize(tmp_mat2, S_der(m)%matrix)
     184        3078 :             CALL dbcsr_scale(S_der(m)%matrix, -one)
     185        3078 :             CALL dbcsr_filter(S_der(m)%matrix, rtp%filter_eps)
     186             :             !The diagonal should be zero
     187        3078 :             CALL dbcsr_iterator_start(iter, S_der(m)%matrix)
     188       15038 :             DO WHILE (dbcsr_iterator_blocks_left(iter))
     189       11960 :                CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
     190      168110 :                IF (row_atom == col_atom) block_values = 0.0_dp
     191             :             END DO
     192        6498 :             CALL dbcsr_iterator_stop(iter)
     193             :          END DO
     194         342 :          CALL dbcsr_deallocate_matrix_set(s_derivs)
     195         342 :          CALL dbcsr_deallocate_matrix(tmp_mat2)
     196             :       END IF
     197             : 
     198             :       !calculate scalar product v(Rb)*<alpha|d/dRb beta> (B_mat), and store the first derivatives
     199             : 
     200        1222 :       CALL dbcsr_set(B_mat, zero)
     201        4888 :       DO m = 1, 3
     202        3666 :          CALL dbcsr_copy(tmp_mat, S_der(m)%matrix)
     203        3666 :          CALL dbcsr_iterator_start(iter, tmp_mat)
     204       18814 :          DO WHILE (dbcsr_iterator_blocks_left(iter))
     205       15148 :             CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
     206      198595 :             IF (row_atom == col_atom) block_values = 0.0_dp
     207      392627 :             block_values = block_values*particle_set(col_atom)%v(m)
     208             :          END DO
     209        3666 :          CALL dbcsr_iterator_stop(iter)
     210        8554 :          CALL dbcsr_add(B_mat, tmp_mat, one, one)
     211             :       END DO
     212        1222 :       CALL dbcsr_filter(B_mat, rtp%filter_eps)
     213             :       !calculate C matrix: v(Rb)*<d/dRa alpha| d/dRb beta>
     214             : 
     215        1222 :       c_map_mat = 0
     216        1222 :       n = 0
     217        4888 :       DO j = 1, 3
     218       12220 :          DO m = j, 3
     219        7332 :             n = n + 1
     220        7332 :             c_map_mat(n, 1) = j
     221        7332 :             IF (m == j) CYCLE
     222       10998 :             c_map_mat(n, 2) = m
     223             :          END DO
     224             :       END DO
     225             : 
     226        4888 :       DO i = 1, 3
     227        4888 :          CALL dbcsr_set(C_mat(i)%matrix, zero)
     228             :       END DO
     229        8554 :       DO m = 1, 6
     230        7332 :          CALL dbcsr_copy(tmp_mat, S_der(m + 3)%matrix)
     231       23218 :          DO j = 1, 2
     232       14664 :             IF (c_map_mat(m, j) == 0) CYCLE
     233       21996 :             CALL dbcsr_add(C_mat(c_map_mat(m, j))%matrix, tmp_mat, one, one)
     234             :          END DO
     235             :       END DO
     236             : 
     237        4888 :       DO m = 1, 3
     238        3666 :          CALL dbcsr_iterator_start(iter, C_mat(m)%matrix)
     239       17746 :          DO WHILE (dbcsr_iterator_blocks_left(iter))
     240       14080 :             CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
     241      387587 :             block_values = block_values*particle_set(row_atom)%v(m)
     242             :          END DO
     243        3666 :          CALL dbcsr_iterator_stop(iter)
     244        8554 :          CALL dbcsr_filter(C_mat(m)%matrix, rtp%filter_eps)
     245             :       END DO
     246             : 
     247        1222 :       CALL dbcsr_deallocate_matrix(tmp_mat)
     248        1222 :       CALL timestop(handle)
     249        1222 :    END SUBROUTINE
     250             : 
     251             : ! **************************************************************************************************
     252             : !> \brief reads the restart file. At the moment only SCF (means only real)
     253             : !> \param qs_env ...
     254             : !> \author Florian Schiffmann (02.09)
     255             : ! **************************************************************************************************
     256             : 
     257          36 :    SUBROUTINE get_restart_wfn(qs_env)
     258             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     259             : 
     260             :       CHARACTER(LEN=default_path_length)                 :: file_name, project_name
     261             :       INTEGER                                            :: i, id_nr, im, ispin, ncol, nspin, &
     262             :                                                             output_unit, re, unit_nr
     263             :       REAL(KIND=dp)                                      :: alpha, cs_pos
     264          36 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     265             :       TYPE(cp_fm_type)                                   :: mos_occ
     266          36 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_old
     267             :       TYPE(cp_logger_type), POINTER                      :: logger
     268             :       TYPE(dbcsr_distribution_type)                      :: dist
     269          36 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: p_rmpv, rho_new, rho_old
     270             :       TYPE(dft_control_type), POINTER                    :: dft_control
     271          36 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
     272             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     273          36 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     274          36 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     275             :       TYPE(qs_rho_type), POINTER                         :: rho_struct
     276             :       TYPE(rt_prop_type), POINTER                        :: rtp
     277             :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     278             : 
     279          36 :       NULLIFY (atomic_kind_set, qs_kind_set, mo_array, particle_set, rho_struct, para_env)
     280             : 
     281             :       CALL get_qs_env(qs_env, &
     282             :                       qs_kind_set=qs_kind_set, &
     283             :                       atomic_kind_set=atomic_kind_set, &
     284             :                       particle_set=particle_set, &
     285             :                       mos=mo_array, &
     286             :                       input=input, &
     287             :                       rtp=rtp, &
     288             :                       dft_control=dft_control, &
     289             :                       rho=rho_struct, &
     290          36 :                       para_env=para_env)
     291          36 :       logger => cp_get_default_logger()
     292          36 :       output_unit = cp_logger_get_default_io_unit(logger)
     293             : 
     294          36 :       IF (logger%para_env%is_source()) THEN
     295          18 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     296             :       ELSE
     297             :          unit_nr = -1
     298             :       END IF
     299             : 
     300          36 :       id_nr = 0
     301          36 :       nspin = SIZE(mo_array)
     302          36 :       CALL qs_rho_get(rho_struct, rho_ao=p_rmpv)
     303          36 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     304          62 :       SELECT CASE (dft_control%rtp_control%initial_wfn)
     305             :       CASE (use_restart_wfn)
     306             :          CALL read_mo_set_from_restart(mo_array, atomic_kind_set, qs_kind_set, particle_set, para_env, &
     307          26 :                                        id_nr=id_nr, multiplicity=dft_control%multiplicity, dft_section=dft_section)
     308          26 :          CALL set_uniform_occupation_mo_array(mo_array, nspin)
     309             : 
     310          26 :          IF (dft_control%rtp_control%apply_wfn_mix_init_restart) &
     311             :             CALL wfn_mix(mo_array, particle_set, dft_section, qs_kind_set, para_env, output_unit, &
     312           4 :                          for_rtp=.TRUE.)
     313             : 
     314          70 :          DO ispin = 1, nspin
     315          70 :             CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
     316             :          END DO
     317          26 :          IF (rtp%linear_scaling) THEN
     318          14 :             CALL get_rtp(rtp=rtp, rho_old=rho_old, rho_new=rho_new)
     319          34 :             DO ispin = 1, nspin
     320          20 :                re = 2*ispin - 1
     321          20 :                im = 2*ispin
     322          20 :                CALL cp_fm_get_info(mo_array(ispin)%mo_coeff, ncol_global=ncol)
     323             :                CALL cp_fm_create(mos_occ, &
     324             :                                  matrix_struct=mo_array(ispin)%mo_coeff%matrix_struct, &
     325          20 :                                  name="mos_occ")
     326          20 :                CALL cp_fm_to_fm(mo_array(ispin)%mo_coeff, mos_occ)
     327          20 :                IF (mo_array(ispin)%uniform_occupation) THEN
     328          16 :                   alpha = 3.0_dp - REAL(nspin, dp)
     329          78 :                   CALL cp_fm_column_scale(mos_occ, mo_array(ispin)%occupation_numbers/alpha)
     330             :                   CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_old(re)%matrix, &
     331             :                                              matrix_v=mos_occ, &
     332             :                                              ncol=ncol, &
     333          16 :                                              alpha=alpha, keep_sparsity=.FALSE.)
     334             :                ELSE
     335           4 :                   alpha = 1.0_dp
     336          88 :                   CALL cp_fm_column_scale(mos_occ, mo_array(ispin)%occupation_numbers/alpha)
     337             :                   CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_old(re)%matrix, &
     338             :                                              matrix_v=mo_array(ispin)%mo_coeff, &
     339             :                                              matrix_g=mos_occ, &
     340             :                                              ncol=ncol, &
     341           4 :                                              alpha=alpha, keep_sparsity=.FALSE.)
     342             :                END IF
     343          20 :                CALL dbcsr_filter(rho_old(re)%matrix, rtp%filter_eps)
     344          20 :                CALL dbcsr_copy(rho_new(re)%matrix, rho_old(re)%matrix)
     345          54 :                CALL cp_fm_release(mos_occ)
     346             :             END DO
     347          14 :             CALL calc_update_rho_sparse(qs_env)
     348             :          ELSE
     349          12 :             CALL get_rtp(rtp=rtp, mos_old=mos_old)
     350          36 :             DO i = 1, SIZE(qs_env%mos)
     351          24 :                CALL cp_fm_to_fm(mo_array(i)%mo_coeff, mos_old(2*i - 1))
     352          36 :                CALL cp_fm_set_all(mos_old(2*i), zero, zero)
     353             :             END DO
     354             :          END IF
     355             :       CASE (use_rt_restart)
     356          36 :          IF (rtp%linear_scaling) THEN
     357           2 :             CALL get_rtp(rtp=rtp, rho_old=rho_old, rho_new=rho_new)
     358           2 :             project_name = logger%iter_info%project_name
     359           4 :             DO ispin = 1, nspin
     360           2 :                re = 2*ispin - 1
     361           2 :                im = 2*ispin
     362           2 :                WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_LS_DM_SPIN_RE", ispin, "_RESTART.dm"
     363           2 :                CALL dbcsr_get_info(rho_old(re)%matrix, distribution=dist)
     364           2 :                CALL dbcsr_binary_read(file_name, distribution=dist, matrix_new=rho_old(re)%matrix)
     365           2 :                cs_pos = dbcsr_checksum(rho_old(re)%matrix, pos=.TRUE.)
     366           2 :                IF (unit_nr > 0) THEN
     367           1 :                   WRITE (unit_nr, '(T2,A,E20.8)') "Read restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
     368             :                END IF
     369           2 :                WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_LS_DM_SPIN_IM", ispin, "_RESTART.dm"
     370           2 :                CALL dbcsr_get_info(rho_old(im)%matrix, distribution=dist)
     371           2 :                CALL dbcsr_binary_read(file_name, distribution=dist, matrix_new=rho_old(im)%matrix)
     372           2 :                cs_pos = dbcsr_checksum(rho_old(im)%matrix, pos=.TRUE.)
     373           8 :                IF (unit_nr > 0) THEN
     374           1 :                   WRITE (unit_nr, '(T2,A,E20.8)') "Read restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
     375             :                END IF
     376             :             END DO
     377           6 :             DO i = 1, SIZE(rho_new)
     378           6 :                CALL dbcsr_copy(rho_new(i)%matrix, rho_old(i)%matrix)
     379             :             END DO
     380           2 :             CALL calc_update_rho_sparse(qs_env)
     381             :          ELSE
     382           8 :             CALL get_rtp(rtp=rtp, mos_old=mos_old)
     383             :             CALL read_rt_mos_from_restart(mo_array, mos_old, atomic_kind_set, qs_kind_set, particle_set, para_env, &
     384           8 :                                           id_nr, dft_control%multiplicity, dft_section)
     385           8 :             CALL set_uniform_occupation_mo_array(mo_array, nspin)
     386          16 :             DO ispin = 1, nspin
     387             :                CALL calculate_density_matrix(mo_array(ispin), &
     388          16 :                                              p_rmpv(ispin)%matrix)
     389             :             END DO
     390             :          END IF
     391             :       END SELECT
     392             : 
     393          36 :    END SUBROUTINE get_restart_wfn
     394             : 
     395             : ! **************************************************************************************************
     396             : !> \brief Set mo_array(ispin)%uniform_occupation after a restart
     397             : !> \param mo_array ...
     398             : !> \param nspin ...
     399             : !> \author Guillaume Le Breton (03.23)
     400             : ! **************************************************************************************************
     401             : 
     402          34 :    SUBROUTINE set_uniform_occupation_mo_array(mo_array, nspin)
     403             : 
     404             :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
     405             :       INTEGER                                            :: nspin
     406             : 
     407             :       INTEGER                                            :: ispin, mo
     408             :       LOGICAL                                            :: is_uniform
     409             : 
     410          86 :       DO ispin = 1, nspin
     411          52 :          is_uniform = .TRUE.
     412         274 :          DO mo = 1, mo_array(ispin)%nmo
     413             :             IF (mo_array(ispin)%occupation_numbers(mo) /= 0.0 .AND. &
     414         222 :                 mo_array(ispin)%occupation_numbers(mo) /= 1.0 .AND. &
     415             :                 mo_array(ispin)%occupation_numbers(mo) /= 2.0) &
     416          92 :                is_uniform = .FALSE.
     417             :          END DO
     418          86 :          mo_array(ispin)%uniform_occupation = is_uniform
     419             :       END DO
     420             : 
     421          34 :    END SUBROUTINE set_uniform_occupation_mo_array
     422             : 
     423             : ! **************************************************************************************************
     424             : !> \brief calculates the density from the complex MOs and passes the density to
     425             : !>        qs_env.
     426             : !> \param qs_env ...
     427             : !> \author Florian Schiffmann (02.09)
     428             : ! **************************************************************************************************
     429             : 
     430        2018 :    SUBROUTINE calc_update_rho(qs_env)
     431             : 
     432             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     433             : 
     434             :       CHARACTER(len=*), PARAMETER                        :: routineN = 'calc_update_rho'
     435             :       REAL(KIND=dp), PARAMETER                           :: one = 1.0_dp, zero = 0.0_dp
     436             : 
     437             :       INTEGER                                            :: handle, i, im, ncol, re
     438             :       REAL(KIND=dp)                                      :: alpha
     439             :       TYPE(cp_fm_type)                                   :: mos_occ
     440        2018 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new
     441        2018 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao, rho_ao_im
     442        2018 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     443             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     444             :       TYPE(qs_rho_type), POINTER                         :: rho
     445             :       TYPE(rt_prop_type), POINTER                        :: rtp
     446             : 
     447        2018 :       CALL timeset(routineN, handle)
     448             : 
     449        2018 :       NULLIFY (rho, ks_env, mos_new, rtp)
     450             :       CALL get_qs_env(qs_env, &
     451             :                       ks_env=ks_env, &
     452             :                       rho=rho, &
     453             :                       rtp=rtp, &
     454        2018 :                       mos=mos)
     455        2018 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     456        2018 :       CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao)
     457        4580 :       DO i = 1, SIZE(mos_new)/2
     458        2562 :          re = 2*i - 1; im = 2*i
     459        2562 :          CALL dbcsr_set(rho_ao(i)%matrix, zero)
     460        2562 :          CALL cp_fm_get_info(mos_new(re), ncol_global=ncol)
     461             :          CALL cp_fm_create(mos_occ, &
     462             :                            matrix_struct=mos(i)%mo_coeff%matrix_struct, &
     463        2562 :                            name="mos_occ")
     464        2562 :          CALL cp_fm_to_fm(mos_new(re), mos_occ)
     465        2562 :          IF (mos(i)%uniform_occupation) THEN
     466        2462 :             alpha = 3*one - REAL(SIZE(mos_new)/2, dp)
     467       10286 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     468             :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     469             :                                        matrix_v=mos_occ, &
     470             :                                        ncol=ncol, &
     471        2462 :                                        alpha=alpha)
     472             :          ELSE
     473         100 :             alpha = 1.0_dp
     474         660 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     475             :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     476             :                                        matrix_v=mos_new(re), &
     477             :                                        matrix_g=mos_occ, &
     478             :                                        ncol=ncol, &
     479         100 :                                        alpha=alpha)
     480             :          END IF
     481             : 
     482             :          ! It is actually complex conjugate but i*i=-1 therefore it must be added
     483        2562 :          CALL cp_fm_to_fm(mos_new(im), mos_occ)
     484        2562 :          IF (mos(i)%uniform_occupation) THEN
     485        2462 :             alpha = 3*one - REAL(SIZE(mos_new)/2, dp)
     486       10286 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     487             :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     488             :                                        matrix_v=mos_occ, &
     489             :                                        ncol=ncol, &
     490        2462 :                                        alpha=alpha)
     491             :          ELSE
     492         100 :             alpha = 1.0_dp
     493         660 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     494             :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     495             :                                        matrix_v=mos_new(im), &
     496             :                                        matrix_g=mos_occ, &
     497             :                                        ncol=ncol, &
     498         100 :                                        alpha=alpha)
     499             :          END IF
     500        7142 :          CALL cp_fm_release(mos_occ)
     501             :       END DO
     502        2018 :       CALL qs_rho_update_rho(rho, qs_env)
     503             : 
     504        2018 :       IF (rtp%track_imag_density) THEN
     505        1358 :          CALL qs_rho_get(rho_struct=rho, rho_ao_im=rho_ao_im)
     506        1358 :          CALL calculate_P_imaginary(qs_env, rtp, rho_ao_im, keep_sparsity=.TRUE.)
     507        1358 :          CALL qs_rho_set(rho, rho_ao_im=rho_ao_im)
     508             :       END IF
     509             : 
     510        2018 :       CALL qs_ks_did_change(ks_env, rho_changed=.TRUE.)
     511             : 
     512        2018 :       CALL timestop(handle)
     513             : 
     514        2018 :    END SUBROUTINE calc_update_rho
     515             : 
     516             : ! **************************************************************************************************
     517             : !> \brief Copies the density matrix back into the qs_env%rho%rho_ao
     518             : !> \param qs_env ...
     519             : !> \author Samuel Andermatt (3.14)
     520             : ! **************************************************************************************************
     521             : 
     522        1264 :    SUBROUTINE calc_update_rho_sparse(qs_env)
     523             : 
     524             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     525             : 
     526             :       CHARACTER(len=*), PARAMETER :: routineN = 'calc_update_rho_sparse'
     527             :       REAL(KIND=dp), PARAMETER                           :: zero = 0.0_dp
     528             : 
     529             :       INTEGER                                            :: handle, ispin
     530        1264 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao, rho_ao_im, rho_new
     531             :       TYPE(dft_control_type), POINTER                    :: dft_control
     532             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     533             :       TYPE(qs_rho_type), POINTER                         :: rho
     534             :       TYPE(rt_prop_type), POINTER                        :: rtp
     535             :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     536             : 
     537        1264 :       NULLIFY (rho, ks_env, rtp, dft_control)
     538        1264 :       CALL timeset(routineN, handle)
     539             :       CALL get_qs_env(qs_env, &
     540             :                       ks_env=ks_env, &
     541             :                       rho=rho, &
     542             :                       rtp=rtp, &
     543        1264 :                       dft_control=dft_control)
     544        1264 :       rtp_control => dft_control%rtp_control
     545        1264 :       CALL get_rtp(rtp=rtp, rho_new=rho_new)
     546        1264 :       CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao)
     547        1264 :       IF (rtp%track_imag_density) CALL qs_rho_get(rho_struct=rho, rho_ao_im=rho_ao_im)
     548        2946 :       DO ispin = 1, SIZE(rho_ao)
     549        1682 :          CALL dbcsr_set(rho_ao(ispin)%matrix, zero)
     550        1682 :          CALL dbcsr_copy(rho_ao(ispin)%matrix, rho_new(ispin*2 - 1)%matrix, keep_sparsity=.TRUE.)
     551        2946 :          IF (rtp%track_imag_density) THEN
     552         488 :             CALL dbcsr_copy(rho_ao_im(ispin)%matrix, rho_new(ispin*2)%matrix, keep_sparsity=.TRUE.)
     553             :          END IF
     554             :       END DO
     555             : 
     556        1264 :       CALL qs_rho_update_rho(rho, qs_env)
     557        1264 :       CALL qs_ks_did_change(ks_env, rho_changed=.TRUE.)
     558             : 
     559        1264 :       CALL timestop(handle)
     560             : 
     561        1264 :    END SUBROUTINE calc_update_rho_sparse
     562             : 
     563             : ! **************************************************************************************************
     564             : !> \brief ...
     565             : !> \param qs_env ...
     566             : !> \param rtp ...
     567             : !> \param matrix_p_im ...
     568             : !> \param keep_sparsity ...
     569             : ! **************************************************************************************************
     570        1370 :    SUBROUTINE calculate_P_imaginary(qs_env, rtp, matrix_p_im, keep_sparsity)
     571             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     572             :       TYPE(rt_prop_type), POINTER                        :: rtp
     573             :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_p_im
     574             :       LOGICAL, OPTIONAL                                  :: keep_sparsity
     575             : 
     576             :       INTEGER                                            :: i, im, ncol, re
     577             :       LOGICAL                                            :: my_keep_sparsity
     578             :       REAL(KIND=dp)                                      :: alpha
     579        1370 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new, mos_occ
     580        1370 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     581             : 
     582        1370 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     583             : 
     584        1370 :       my_keep_sparsity = .FALSE.
     585        1370 :       IF (PRESENT(keep_sparsity)) my_keep_sparsity = keep_sparsity
     586        1370 :       CALL get_qs_env(qs_env, mos=mos)
     587        7482 :       ALLOCATE (mos_occ(SIZE(mos)*2))
     588             : 
     589        3056 :       DO i = 1, SIZE(mos_new)/2
     590        1686 :          re = 2*i - 1; im = 2*i
     591        1686 :          alpha = 3.0_dp - REAL(SIZE(matrix_p_im), dp)
     592             :          CALL cp_fm_create(mos_occ(re), &
     593             :                            matrix_struct=mos(i)%mo_coeff%matrix_struct, &
     594        1686 :                            name="mos_occ")
     595             :          CALL cp_fm_create(mos_occ(im), &
     596             :                            matrix_struct=mos(i)%mo_coeff%matrix_struct, &
     597        1686 :                            name="mos_occ")
     598        1686 :          CALL dbcsr_set(matrix_p_im(i)%matrix, 0.0_dp)
     599        1686 :          CALL cp_fm_get_info(mos_new(re), ncol_global=ncol)
     600        1686 :          CALL cp_fm_to_fm(mos_new(re), mos_occ(re))
     601        7152 :          CALL cp_fm_column_scale(mos_occ(re), mos(i)%occupation_numbers/alpha)
     602        1686 :          CALL cp_fm_to_fm(mos_new(im), mos_occ(im))
     603        7152 :          CALL cp_fm_column_scale(mos_occ(im), mos(i)%occupation_numbers/alpha)
     604             :          CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=matrix_p_im(i)%matrix, &
     605             :                                     matrix_v=mos_occ(im), &
     606             :                                     matrix_g=mos_occ(re), &
     607             :                                     ncol=ncol, &
     608             :                                     keep_sparsity=my_keep_sparsity, &
     609             :                                     alpha=2.0_dp*alpha, &
     610        4742 :                                     symmetry_mode=-1)
     611             :       END DO
     612        1370 :       CALL cp_fm_release(mos_occ)
     613             : 
     614        1370 :    END SUBROUTINE calculate_P_imaginary
     615             : 
     616             : ! **************************************************************************************************
     617             : !> \brief ...
     618             : !> \param qs_env ...
     619             : !> \param rtp ...
     620             : ! **************************************************************************************************
     621         624 :    SUBROUTINE write_rtp_mos_to_output_unit(qs_env, rtp)
     622             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     623             :       TYPE(rt_prop_type), POINTER                        :: rtp
     624             : 
     625             :       CHARACTER(len=*), PARAMETER :: routineN = 'write_rtp_mos_to_output_unit'
     626             : 
     627             :       CHARACTER(LEN=10)                                  :: spin
     628             :       CHARACTER(LEN=2*default_string_length)             :: name
     629             :       INTEGER                                            :: handle, i, ispin, nao, nelectron, nmo, &
     630             :                                                             nspins
     631             :       LOGICAL                                            :: print_eigvecs, print_mo_info
     632             :       REAL(KIND=dp)                                      :: flexible_electron_count, maxocc, n_el_f
     633         312 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     634         312 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new
     635             :       TYPE(cp_logger_type), POINTER                      :: logger
     636             :       TYPE(mo_set_type)                                  :: mo_set_rtp
     637         312 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     638         312 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     639         312 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     640             :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     641             : 
     642         312 :       NULLIFY (atomic_kind_set, particle_set, qs_kind_set, input, mos, dft_section)
     643             : 
     644         312 :       CALL timeset(routineN, handle)
     645             : 
     646             :       CALL get_qs_env(qs_env, &
     647             :                       atomic_kind_set=atomic_kind_set, &
     648             :                       qs_kind_set=qs_kind_set, &
     649             :                       particle_set=particle_set, &
     650             :                       input=input, &
     651         312 :                       mos=mos)
     652             :       ! Quick return, if no printout of MO information is requested
     653         312 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     654         312 :       CALL section_vals_val_get(dft_section, "PRINT%MO%EIGENVECTORS", l_val=print_eigvecs)
     655             : 
     656         312 :       NULLIFY (logger)
     657         312 :       logger => cp_get_default_logger()
     658             :       print_mo_info = (cp_print_key_should_output(logger%iter_info, &
     659             :                                                   dft_section, "PRINT%MO") /= 0) .OR. &
     660         312 :                       (qs_env%sim_step == 1)
     661             : 
     662         100 :       IF ((.NOT. print_mo_info) .OR. (.NOT. print_eigvecs)) THEN
     663         308 :          CALL timestop(handle)
     664         308 :          RETURN
     665             :       END IF
     666             : 
     667           4 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     668             : 
     669           4 :       nspins = SIZE(mos_new)/2
     670             : 
     671           8 :       DO ispin = 1, nspins
     672             :          ! initiate mo_set
     673             :          CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=nmo, nelectron=nelectron, &
     674           4 :                          n_el_f=n_el_f, maxocc=maxocc, flexible_electron_count=flexible_electron_count)
     675             : 
     676             :          CALL allocate_mo_set(mo_set_rtp, &
     677             :                               nao=nao, &
     678             :                               nmo=nmo, &
     679             :                               nelectron=nelectron, &
     680             :                               n_el_f=n_el_f, &
     681             :                               maxocc=maxocc, &
     682           4 :                               flexible_electron_count=flexible_electron_count)
     683             : 
     684           4 :          WRITE (name, FMT="(A,I6)") "RTP MO SET, SPIN ", ispin
     685           4 :          CALL init_mo_set(mo_set_rtp, fm_ref=mos_new(2*ispin - 1), name=name)
     686             : 
     687           4 :          IF (nspins > 1) THEN
     688           0 :             IF (ispin == 1) THEN
     689           0 :                spin = "ALPHA SPIN"
     690             :             ELSE
     691           0 :                spin = "BETA SPIN"
     692             :             END IF
     693             :          ELSE
     694           4 :             spin = ""
     695             :          END IF
     696             : 
     697           8 :          mo_set_rtp%occupation_numbers = mos(ispin)%occupation_numbers
     698             : 
     699             :          !loop for real (odd) and imaginary (even) parts
     700          12 :          DO i = 1, 0, -1
     701           8 :             CALL cp_fm_to_fm(mos_new(2*ispin - i), mo_set_rtp%mo_coeff)
     702             :             CALL write_mo_set_to_output_unit(mo_set_rtp, atomic_kind_set, qs_kind_set, particle_set, &
     703          12 :                                           dft_section, 4, 0, rtp=.TRUE., spin=TRIM(spin), cpart=MOD(i, 2), sim_step=qs_env%sim_step)
     704             :          END DO
     705             : 
     706          12 :          CALL deallocate_mo_set(mo_set_rtp)
     707             :       END DO
     708             : 
     709           4 :       CALL timestop(handle)
     710             : 
     711         312 :    END SUBROUTINE write_rtp_mos_to_output_unit
     712             : 
     713             : ! **************************************************************************************************
     714             : !> \brief Write the time dependent amplitude of the MOs in real grid.
     715             : !>        Very close to qs_scf_post_gpw/qs_scf_post_occ_cubes subroutine.
     716             : !> \param qs_env ...
     717             : !> \param rtp ...
     718             : !> \author Guillaume Le Breton (11.22)
     719             : ! **************************************************************************************************
     720         312 :    SUBROUTINE write_rtp_mo_cubes(qs_env, rtp)
     721             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     722             :       TYPE(rt_prop_type), POINTER                        :: rtp
     723             : 
     724             :       CHARACTER(LEN=*), PARAMETER :: routineN = 'write_rtp_mo_cubes'
     725             : 
     726             :       CHARACTER(LEN=default_path_length)                 :: filename, my_pos_cube, title
     727             :       INTEGER                                            :: handle, homo, i, ir, ispin, ivector, &
     728             :                                                             n_rep, nhomo, nlist, nspins, &
     729             :                                                             rt_time_step, unit_nr
     730         312 :       INTEGER, DIMENSION(:), POINTER                     :: list, list_index
     731             :       LOGICAL                                            :: append_cube, do_kpoints, mpi_io
     732         312 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     733             :       TYPE(cell_type), POINTER                           :: cell
     734             :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     735         312 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new
     736             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     737             :       TYPE(cp_logger_type), POINTER                      :: logger
     738             :       TYPE(dft_control_type), POINTER                    :: dft_control
     739         312 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     740             :       TYPE(mp_para_env_type), POINTER                    :: para_env
     741             :       TYPE(particle_list_type), POINTER                  :: particles
     742         312 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     743             :       TYPE(pw_c1d_gs_type)                               :: wf_g
     744             :       TYPE(pw_env_type), POINTER                         :: pw_env
     745         312 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
     746             :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     747             :       TYPE(pw_r3d_rs_type)                               :: density_r, wf_r
     748         312 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     749             :       TYPE(qs_subsys_type), POINTER                      :: subsys
     750             :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     751             : 
     752         312 :       CALL timeset(routineN, handle)
     753             : 
     754         312 :       NULLIFY (logger, auxbas_pw_pool, pw_pools, pw_env)
     755             : 
     756             :       ! Get all the info from qs:
     757             :       CALL get_qs_env(qs_env, do_kpoints=do_kpoints, &
     758         312 :                       input=input)
     759             : 
     760             :       ! Kill the run in the case of K points
     761         312 :       IF (do_kpoints) THEN
     762           0 :          CPABORT("K points not handled yet for printing MO_CUBE")
     763             :       END IF
     764             : 
     765         312 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     766         312 :       logger => cp_get_default_logger()
     767             : 
     768             :       ! Quick return if no print required
     769         312 :       IF (.NOT. BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
     770             :                                                  "PRINT%MO_CUBES"), cp_p_file)) THEN
     771         288 :          CALL timestop(handle)
     772         288 :          RETURN
     773             :       END IF
     774             : 
     775             :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, &
     776             :                       mos=mos, &
     777             :                       blacs_env=blacs_env, &
     778             :                       qs_kind_set=qs_kind_set, &
     779             :                       pw_env=pw_env, &
     780             :                       subsys=subsys, &
     781             :                       para_env=para_env, &
     782             :                       particle_set=particle_set, &
     783          24 :                       dft_control=dft_control)
     784          24 :       CALL qs_subsys_get(subsys, particles=particles)
     785             : 
     786          24 :       nspins = dft_control%nspins
     787          24 :       rt_time_step = qs_env%sim_step
     788             : 
     789             :       ! Setup the grids needed to compute a wavefunction given a vector
     790             :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
     791          24 :                       pw_pools=pw_pools)
     792          24 :       CALL auxbas_pw_pool%create_pw(wf_r)
     793          24 :       CALL auxbas_pw_pool%create_pw(wf_g)
     794          24 :       CALL auxbas_pw_pool%create_pw(density_r)
     795          24 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     796             : 
     797          70 :       DO ispin = 1, nspins
     798          46 :          CALL get_mo_set(mo_set=mos(ispin), homo=homo)
     799             : 
     800          46 :          nhomo = section_get_ival(dft_section, "PRINT%MO_CUBES%NHOMO")
     801          46 :          append_cube = section_get_lval(dft_section, "PRINT%MO_CUBES%APPEND")
     802          46 :          my_pos_cube = "REWIND"
     803          46 :          IF (append_cube) THEN
     804           0 :             my_pos_cube = "APPEND"
     805             :          END IF
     806          46 :          CALL section_vals_val_get(dft_section, "PRINT%MO_CUBES%HOMO_LIST", n_rep_val=n_rep)
     807          46 :          IF (n_rep > 0) THEN ! write the cubes of the list
     808           0 :             nlist = 0
     809           0 :             DO ir = 1, n_rep
     810           0 :                NULLIFY (list)
     811             :                CALL section_vals_val_get(dft_section, "PRINT%MO_CUBES%HOMO_LIST", i_rep_val=ir, &
     812           0 :                                          i_vals=list)
     813           0 :                IF (ASSOCIATED(list)) THEN
     814           0 :                   CALL reallocate(list_index, 1, nlist + SIZE(list))
     815           0 :                   DO i = 1, SIZE(list)
     816           0 :                      list_index(i + nlist) = list(i)
     817             :                   END DO
     818           0 :                   nlist = nlist + SIZE(list)
     819             :                END IF
     820             :             END DO
     821             :          ELSE
     822             : 
     823          46 :             IF (nhomo == -1) nhomo = homo
     824          46 :             nlist = homo - MAX(1, homo - nhomo + 1) + 1
     825         138 :             ALLOCATE (list_index(nlist))
     826         224 :             DO i = 1, nlist
     827         224 :                list_index(i) = MAX(1, homo - nhomo + 1) + i - 1
     828             :             END DO
     829             :          END IF
     830         224 :          DO i = 1, nlist
     831         178 :             ivector = list_index(i)
     832             :             CALL get_qs_env(qs_env=qs_env, &
     833             :                             atomic_kind_set=atomic_kind_set, &
     834             :                             qs_kind_set=qs_kind_set, &
     835             :                             cell=cell, &
     836             :                             particle_set=particle_set, &
     837         178 :                             pw_env=pw_env)
     838             : 
     839             :             ! density_r contains the density of the MOs
     840         178 :             CALL pw_zero(density_r)
     841         178 :             mo_coeff => mos_new(2*ispin - 1)!Real coeff
     842             :             CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
     843         178 :                                         cell, dft_control, particle_set, pw_env)
     844             :             ! Adding the real part
     845         178 :             CALL pw_multiply(density_r, wf_r, wf_r, 1.0_dp)
     846             : 
     847         178 :             mo_coeff => mos_new(2*ispin) !Im coeff
     848             :             CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
     849         178 :                                         cell, dft_control, particle_set, pw_env)
     850             :             ! Adding the im part
     851         178 :             CALL pw_multiply(density_r, wf_r, wf_r, 1.0_dp)
     852             : 
     853         178 :             WRITE (filename, '(a4,I5.5,a1,I1.1)') "WFN_", ivector, "_", ispin
     854         178 :             mpi_io = .TRUE.
     855             :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MO_CUBES", extension=".cube", &
     856             :                                            middle_name=TRIM(filename), file_position=my_pos_cube, log_filename=.FALSE., &
     857         178 :                                            mpi_io=mpi_io)
     858         178 :             WRITE (title, *) "DENSITY ", ivector, " spin ", ispin, " i.e. HOMO - ", ivector - homo
     859             :             CALL cp_pw_to_cube(density_r, unit_nr, title, particles=particles, &
     860         178 :                                stride=section_get_ivals(dft_section, "PRINT%MO_CUBES%STRIDE"), mpi_io=mpi_io)
     861         224 :             CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MO_CUBES", mpi_io=mpi_io)
     862             :          END DO
     863         162 :          IF (ASSOCIATED(list_index)) DEALLOCATE (list_index)
     864             :       END DO
     865             : 
     866             :       ! Deallocate grids needed to compute wavefunctions
     867          24 :       CALL auxbas_pw_pool%give_back_pw(wf_r)
     868          24 :       CALL auxbas_pw_pool%give_back_pw(wf_g)
     869          24 :       CALL auxbas_pw_pool%give_back_pw(density_r)
     870             : 
     871          24 :       CALL timestop(handle)
     872             : 
     873         312 :    END SUBROUTINE write_rtp_mo_cubes
     874             : 
     875             : END MODULE rt_propagation_utils

Generated by: LCOV version 1.15