LCOV - code coverage report
Current view: top level - src/emd - rt_propagation_utils.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:dbd702d) Lines: 91.3 % 378 345
Test Date: 2026-11-22 01:17:58 Functions: 100.0 % 11 11

            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 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_copy, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, &
      21              :         dbcsr_distribution_type, dbcsr_filter, dbcsr_get_info, dbcsr_iterator_blocks_left, &
      22              :         dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
      23              :         dbcsr_p_type, dbcsr_scale, dbcsr_set, dbcsr_type
      24              :    USE cp_dbcsr_contrib,                ONLY: dbcsr_checksum
      25              :    USE cp_dbcsr_io,                     ONLY: dbcsr_binary_read
      26              :    USE cp_dbcsr_operations,             ONLY: cp_dbcsr_plus_fm_fm_t,&
      27              :                                               dbcsr_deallocate_matrix_set
      28              :    USE cp_files,                        ONLY: close_file,&
      29              :                                               file_exists,&
      30              :                                               open_file
      31              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_column_scale
      32              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      33              :                                               cp_fm_get_info,&
      34              :                                               cp_fm_release,&
      35              :                                               cp_fm_set_all,&
      36              :                                               cp_fm_to_fm,&
      37              :                                               cp_fm_type
      38              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      39              :                                               cp_logger_get_default_io_unit,&
      40              :                                               cp_logger_get_default_unit_nr,&
      41              :                                               cp_logger_type
      42              :    USE cp_output_handling,              ONLY: cp_p_file,&
      43              :                                               cp_print_key_finished_output,&
      44              :                                               cp_print_key_generate_filename,&
      45              :                                               cp_print_key_should_output,&
      46              :                                               cp_print_key_unit_nr
      47              :    USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
      48              :    USE efield_utils,                    ONLY: make_field
      49              :    USE input_constants,                 ONLY: use_restart_wfn,&
      50              :                                               use_rt_restart
      51              :    USE input_section_types,             ONLY: section_get_ival,&
      52              :                                               section_get_ivals,&
      53              :                                               section_get_lval,&
      54              :                                               section_vals_get,&
      55              :                                               section_vals_get_subs_vals,&
      56              :                                               section_vals_type,&
      57              :                                               section_vals_val_get
      58              :    USE kinds,                           ONLY: default_path_length,&
      59              :                                               default_string_length,&
      60              :                                               dp
      61              :    USE mathconstants,                   ONLY: zero
      62              :    USE memory_utilities,                ONLY: reallocate
      63              :    USE message_passing,                 ONLY: mp_para_env_type
      64              :    USE orbital_pointers,                ONLY: ncoset
      65              :    USE particle_list_types,             ONLY: particle_list_type
      66              :    USE particle_types,                  ONLY: particle_type
      67              :    USE physcon,                         ONLY: femtoseconds
      68              :    USE pw_env_types,                    ONLY: pw_env_get,&
      69              :                                               pw_env_type
      70              :    USE pw_methods,                      ONLY: pw_multiply,&
      71              :                                               pw_zero
      72              :    USE pw_pool_types,                   ONLY: pw_pool_p_type,&
      73              :                                               pw_pool_type
      74              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      75              :                                               pw_r3d_rs_type
      76              :    USE qs_collocate_density,            ONLY: calculate_wavefunction
      77              :    USE qs_density_matrices,             ONLY: calculate_density_matrix
      78              :    USE qs_dftb_matrices,                ONLY: build_dftb_overlap
      79              :    USE qs_environment_types,            ONLY: get_qs_env,&
      80              :                                               qs_environment_type
      81              :    USE qs_kind_types,                   ONLY: qs_kind_type
      82              :    USE qs_ks_types,                     ONLY: qs_ks_did_change,&
      83              :                                               qs_ks_env_type
      84              :    USE qs_mo_io,                        ONLY: read_mo_set_from_restart,&
      85              :                                               read_rt_mos_from_restart,&
      86              :                                               write_mo_set_to_output_unit
      87              :    USE qs_mo_types,                     ONLY: allocate_mo_set,&
      88              :                                               deallocate_mo_set,&
      89              :                                               get_mo_set,&
      90              :                                               init_mo_set,&
      91              :                                               mo_set_type
      92              :    USE qs_neighbor_list_types,          ONLY: neighbor_list_set_p_type
      93              :    USE qs_overlap,                      ONLY: build_overlap_matrix
      94              :    USE qs_rho_methods,                  ONLY: qs_rho_update_rho
      95              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      96              :                                               qs_rho_set,&
      97              :                                               qs_rho_type
      98              :    USE qs_scf_post_gpw,                 ONLY: get_effective_core_charges
      99              :    USE qs_scf_wfn_mix,                  ONLY: wfn_mix
     100              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
     101              :                                               qs_subsys_type
     102              :    USE rt_propagation_types,            ONLY: get_rtp,&
     103              :                                               rt_prop_type
     104              : #include "../base/base_uses.f90"
     105              : 
     106              :    IMPLICIT NONE
     107              :    PRIVATE
     108              : 
     109              :    PUBLIC :: get_restart_wfn, &
     110              :              calc_S_derivs, &
     111              :              calc_update_rho, &
     112              :              calc_update_rho_sparse, &
     113              :              calculate_P_imaginary, &
     114              :              write_rtp_mos_to_output_unit, &
     115              :              write_rtp_mo_cubes, &
     116              :              warn_section_unused, &
     117              :              read_moments, &
     118              :              recalculate_fields
     119              : 
     120              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation_utils'
     121              : 
     122              : CONTAINS
     123              : 
     124              : ! **************************************************************************************************
     125              : !> \brief Calculates dS/dR respectily the velocity weighted derivatves
     126              : !>        only needed for ehrenfest MD.
     127              : !>
     128              : !> \param qs_env the qs environment
     129              : !> \par History
     130              : !>      02.2009 created [Manuel Guidon]
     131              : !>      02.2014 switched to dbcsr matrices [Samuel Andermatt]
     132              : !> \author Florian Schiffmann
     133              : ! **************************************************************************************************
     134         1592 :    SUBROUTINE calc_S_derivs(qs_env)
     135              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     136              : 
     137              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'calc_S_derivs'
     138              :       INTEGER, DIMENSION(3, 3), PARAMETER :: ider = RESHAPE([4, 5, 6, 5, 7, 8, 6, 8, 9], [3, 3])
     139              :       REAL(KIND=dp), PARAMETER                           :: one = 1.0_dp, zero = 0.0_dp
     140              : 
     141              :       INTEGER                                            :: col_atom, handle, i, k, m, maxder, nder, &
     142              :                                                             row_atom
     143              :       LOGICAL                                            :: return_s_derivatives
     144         1592 :       REAL(dp), DIMENSION(:, :), POINTER                 :: block_values
     145              :       TYPE(dbcsr_iterator_type)                          :: iter
     146         1592 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: C_mat, S_der, s_derivs
     147              :       TYPE(dbcsr_type), POINTER                          :: B_mat, tmp_mat, tmp_mat2
     148              :       TYPE(dft_control_type), POINTER                    :: dft_control
     149              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     150         1592 :          POINTER                                         :: sab_orb
     151         1592 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     152              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     153              :       TYPE(rt_prop_type), POINTER                        :: rtp
     154              : 
     155         1592 :       CALL timeset(routineN, handle)
     156              : 
     157         1592 :       return_s_derivatives = .TRUE.
     158              : 
     159         1592 :       NULLIFY (particle_set)
     160         1592 :       NULLIFY (rtp)
     161         1592 :       NULLIFY (s_derivs)
     162         1592 :       NULLIFY (dft_control)
     163         1592 :       NULLIFY (ks_env)
     164              : 
     165              :       CALL get_qs_env(qs_env=qs_env, &
     166              :                       rtp=rtp, &
     167              :                       particle_set=particle_set, &
     168              :                       sab_orb=sab_orb, &
     169              :                       dft_control=dft_control, &
     170         1592 :                       ks_env=ks_env)
     171              : 
     172         1592 :       CALL get_rtp(rtp=rtp, B_mat=B_mat, C_mat=C_mat, S_der=S_der)
     173              : 
     174         1592 :       nder = 2
     175         1592 :       maxder = ncoset(nder)
     176              : 
     177              :       NULLIFY (tmp_mat)
     178         1592 :       ALLOCATE (tmp_mat)
     179         1592 :       CALL dbcsr_create(tmp_mat, template=S_der(1)%matrix, matrix_type="N")
     180              : 
     181         1592 :       IF (rtp%iter < 2) THEN
     182              :          ! calculate the overlap derivative matrices
     183          440 :          IF (dft_control%qs_control%dftb) THEN
     184           84 :             CALL build_dftb_overlap(qs_env, nder, s_derivs)
     185              :          ELSE
     186              :             CALL build_overlap_matrix(ks_env, nderivative=nder, matrix_s=s_derivs, &
     187          356 :                                       basis_type_a="ORB", basis_type_b="ORB", sab_nl=sab_orb)
     188              :          END IF
     189              : 
     190              :          NULLIFY (tmp_mat2)
     191          440 :          ALLOCATE (tmp_mat2)
     192          440 :          CALL dbcsr_create(tmp_mat2, template=S_der(1)%matrix, matrix_type="S")
     193         4400 :          DO m = 1, 9
     194         3960 :             CALL dbcsr_copy(tmp_mat2, s_derivs(m + 1)%matrix)
     195         3960 :             CALL dbcsr_desymmetrize(tmp_mat2, S_der(m)%matrix)
     196         3960 :             CALL dbcsr_scale(S_der(m)%matrix, -one)
     197         3960 :             CALL dbcsr_filter(S_der(m)%matrix, rtp%filter_eps)
     198              :             !The diagonal should be zero
     199         3960 :             CALL dbcsr_iterator_start(iter, S_der(m)%matrix)
     200        21542 :             DO WHILE (dbcsr_iterator_blocks_left(iter))
     201        17582 :                CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
     202       312188 :                IF (row_atom == col_atom) block_values = 0.0_dp
     203              :             END DO
     204         8360 :             CALL dbcsr_iterator_stop(iter)
     205              :          END DO
     206          440 :          CALL dbcsr_deallocate_matrix_set(s_derivs)
     207          440 :          CALL dbcsr_deallocate_matrix(tmp_mat2)
     208              :       END IF
     209              : 
     210              :       !calculate scalar product v(Rb)*<alpha|d/dRb beta> (B_mat), and store the first derivatives
     211              : 
     212         1592 :       CALL dbcsr_set(B_mat, zero)
     213         6368 :       DO m = 1, 3
     214         4776 :          CALL dbcsr_copy(tmp_mat, S_der(m)%matrix)
     215         4776 :          CALL dbcsr_iterator_start(iter, tmp_mat)
     216        25888 :          DO WHILE (dbcsr_iterator_blocks_left(iter))
     217        21112 :             CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
     218       375430 :             IF (row_atom == col_atom) block_values = 0.0_dp
     219       868890 :             block_values = block_values*particle_set(col_atom)%v(m)
     220              :          END DO
     221         4776 :          CALL dbcsr_iterator_stop(iter)
     222        11144 :          CALL dbcsr_add(B_mat, tmp_mat, one, one)
     223              :       END DO
     224         1592 :       CALL dbcsr_filter(B_mat, rtp%filter_eps)
     225              :       !calculate C matrix: C(i) = <d/dt alpha| d/dRb_i beta> = sum_k v_k(Ra)*<d/dRa_k alpha| d/dRb_i beta>
     226              :       ! with alpha on atom a (row) and beta on atom b (column)
     227              : 
     228         6368 :       DO i = 1, 3
     229         4776 :          CALL dbcsr_set(C_mat(i)%matrix, zero)
     230        19104 :          DO k = 1, 3
     231        14328 :             CALL dbcsr_copy(tmp_mat, S_der(ider(i, k))%matrix)
     232        14328 :             CALL dbcsr_iterator_start(iter, tmp_mat)
     233        78692 :             DO WHILE (dbcsr_iterator_blocks_left(iter))
     234        64364 :                CALL dbcsr_iterator_next_block(iter, row_atom, col_atom, block_values)
     235      2613690 :                block_values = block_values*particle_set(row_atom)%v(k)
     236              :             END DO
     237        14328 :             CALL dbcsr_iterator_stop(iter)
     238        33432 :             CALL dbcsr_add(C_mat(i)%matrix, tmp_mat, one, one)
     239              :          END DO
     240         6368 :          CALL dbcsr_filter(C_mat(i)%matrix, rtp%filter_eps)
     241              :       END DO
     242              : 
     243         1592 :       CALL dbcsr_deallocate_matrix(tmp_mat)
     244         1592 :       CALL timestop(handle)
     245         1592 :    END SUBROUTINE calc_S_derivs
     246              : 
     247              : ! **************************************************************************************************
     248              : !> \brief reads the restart file. At the moment only SCF (means only real)
     249              : !> \param qs_env ...
     250              : !> \author Florian Schiffmann (02.09)
     251              : ! **************************************************************************************************
     252              : 
     253           36 :    SUBROUTINE get_restart_wfn(qs_env)
     254              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     255              : 
     256              :       CHARACTER(LEN=default_path_length)                 :: file_name, project_name
     257              :       INTEGER                                            :: i, id_nr, im, ispin, ncol, nspin, &
     258              :                                                             output_unit, re, unit_nr
     259              :       REAL(KIND=dp)                                      :: alpha, cs_pos
     260           36 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     261              :       TYPE(cp_fm_type)                                   :: mos_occ
     262           36 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_old
     263              :       TYPE(cp_logger_type), POINTER                      :: logger
     264              :       TYPE(dbcsr_distribution_type)                      :: dist
     265           36 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: p_rmpv, rho_new, rho_old
     266              :       TYPE(dft_control_type), POINTER                    :: dft_control
     267           36 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
     268              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     269           36 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     270           36 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     271              :       TYPE(qs_rho_type), POINTER                         :: rho_struct
     272              :       TYPE(rt_prop_type), POINTER                        :: rtp
     273              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     274              : 
     275           36 :       NULLIFY (atomic_kind_set, qs_kind_set, mo_array, particle_set, rho_struct, para_env)
     276              : 
     277              :       CALL get_qs_env(qs_env, &
     278              :                       qs_kind_set=qs_kind_set, &
     279              :                       atomic_kind_set=atomic_kind_set, &
     280              :                       particle_set=particle_set, &
     281              :                       mos=mo_array, &
     282              :                       input=input, &
     283              :                       rtp=rtp, &
     284              :                       dft_control=dft_control, &
     285              :                       rho=rho_struct, &
     286           36 :                       para_env=para_env)
     287           36 :       logger => cp_get_default_logger()
     288           36 :       output_unit = cp_logger_get_default_io_unit(logger)
     289              : 
     290           36 :       IF (logger%para_env%is_source()) THEN
     291           18 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     292              :       ELSE
     293              :          unit_nr = -1
     294              :       END IF
     295              : 
     296           36 :       id_nr = 0
     297           36 :       nspin = SIZE(mo_array)
     298           36 :       CALL qs_rho_get(rho_struct, rho_ao=p_rmpv)
     299           36 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     300           62 :       SELECT CASE (dft_control%rtp_control%initial_wfn)
     301              :       CASE (use_restart_wfn)
     302              :          CALL read_mo_set_from_restart(mo_array, qs_kind_set, particle_set, para_env, &
     303              :                                        id_nr=id_nr, multiplicity=dft_control%multiplicity, &
     304           26 :                                        dft_section=dft_section)
     305           26 :          CALL set_uniform_occupation_mo_array(mo_array, nspin)
     306              : 
     307           26 :          IF (dft_control%rtp_control%apply_wfn_mix_init_restart) THEN
     308              :             CALL wfn_mix(mo_array, particle_set, dft_section, qs_kind_set, para_env, output_unit, &
     309            4 :                          for_rtp=.TRUE.)
     310              :          END IF
     311              : 
     312           70 :          DO ispin = 1, nspin
     313           70 :             CALL calculate_density_matrix(mo_array(ispin), p_rmpv(ispin)%matrix)
     314              :          END DO
     315           26 :          IF (rtp%linear_scaling) THEN
     316           14 :             CALL get_rtp(rtp=rtp, rho_old=rho_old, rho_new=rho_new)
     317           34 :             DO ispin = 1, nspin
     318           20 :                re = 2*ispin - 1
     319           20 :                im = 2*ispin
     320           20 :                CALL cp_fm_get_info(mo_array(ispin)%mo_coeff, ncol_global=ncol)
     321              :                CALL cp_fm_create(mos_occ, &
     322              :                                  matrix_struct=mo_array(ispin)%mo_coeff%matrix_struct, &
     323           20 :                                  name="mos_occ")
     324           20 :                CALL cp_fm_to_fm(mo_array(ispin)%mo_coeff, mos_occ)
     325           20 :                IF (mo_array(ispin)%uniform_occupation) THEN
     326           16 :                   alpha = 3.0_dp - REAL(nspin, dp)
     327           78 :                   CALL cp_fm_column_scale(mos_occ, mo_array(ispin)%occupation_numbers/alpha)
     328              :                   CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_old(re)%matrix, &
     329              :                                              matrix_v=mos_occ, &
     330              :                                              ncol=ncol, &
     331           16 :                                              alpha=alpha, keep_sparsity=.FALSE.)
     332              :                ELSE
     333            4 :                   alpha = 1.0_dp
     334           88 :                   CALL cp_fm_column_scale(mos_occ, mo_array(ispin)%occupation_numbers/alpha)
     335              :                   CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_old(re)%matrix, &
     336              :                                              matrix_v=mo_array(ispin)%mo_coeff, &
     337              :                                              matrix_g=mos_occ, &
     338              :                                              ncol=ncol, &
     339            4 :                                              alpha=alpha, keep_sparsity=.FALSE.)
     340              :                END IF
     341           20 :                CALL dbcsr_filter(rho_old(re)%matrix, rtp%filter_eps)
     342           20 :                CALL dbcsr_copy(rho_new(re)%matrix, rho_old(re)%matrix)
     343           54 :                CALL cp_fm_release(mos_occ)
     344              :             END DO
     345           14 :             CALL calc_update_rho_sparse(qs_env)
     346              :          ELSE
     347           12 :             CALL get_rtp(rtp=rtp, mos_old=mos_old)
     348           36 :             DO i = 1, SIZE(qs_env%mos)
     349           24 :                CALL cp_fm_to_fm(mo_array(i)%mo_coeff, mos_old(2*i - 1))
     350           36 :                CALL cp_fm_set_all(mos_old(2*i), zero, zero)
     351              :             END DO
     352              :          END IF
     353              :       CASE (use_rt_restart)
     354           36 :          IF (rtp%linear_scaling) THEN
     355            2 :             CALL get_rtp(rtp=rtp, rho_old=rho_old, rho_new=rho_new)
     356            2 :             project_name = logger%iter_info%project_name
     357            4 :             DO ispin = 1, nspin
     358            2 :                re = 2*ispin - 1
     359            2 :                im = 2*ispin
     360            2 :                WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_LS_DM_SPIN_RE", ispin, "_RESTART.dm"
     361            2 :                CALL dbcsr_get_info(rho_old(re)%matrix, distribution=dist)
     362            2 :                CALL dbcsr_binary_read(file_name, distribution=dist, matrix_new=rho_old(re)%matrix)
     363            2 :                cs_pos = dbcsr_checksum(rho_old(re)%matrix, pos=.TRUE.)
     364            2 :                IF (unit_nr > 0) THEN
     365            1 :                   WRITE (unit_nr, '(T2,A,E20.8)') "Read restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
     366              :                END IF
     367            2 :                WRITE (file_name, '(A,I0,A)') TRIM(project_name)//"_LS_DM_SPIN_IM", ispin, "_RESTART.dm"
     368            2 :                CALL dbcsr_get_info(rho_old(im)%matrix, distribution=dist)
     369            2 :                CALL dbcsr_binary_read(file_name, distribution=dist, matrix_new=rho_old(im)%matrix)
     370            2 :                cs_pos = dbcsr_checksum(rho_old(im)%matrix, pos=.TRUE.)
     371            8 :                IF (unit_nr > 0) THEN
     372            1 :                   WRITE (unit_nr, '(T2,A,E20.8)') "Read restart DM "//TRIM(file_name)//" with checksum: ", cs_pos
     373              :                END IF
     374              :             END DO
     375            6 :             DO i = 1, SIZE(rho_new)
     376            6 :                CALL dbcsr_copy(rho_new(i)%matrix, rho_old(i)%matrix)
     377              :             END DO
     378            2 :             CALL calc_update_rho_sparse(qs_env)
     379              :          ELSE
     380            8 :             CALL get_rtp(rtp=rtp, mos_old=mos_old)
     381              :             CALL read_rt_mos_from_restart(mo_array, mos_old, qs_kind_set, particle_set, para_env, &
     382            8 :                                           id_nr, dft_control%multiplicity, dft_section)
     383            8 :             CALL set_uniform_occupation_mo_array(mo_array, nspin)
     384           16 :             DO ispin = 1, nspin
     385              :                CALL calculate_density_matrix(mo_array(ispin), &
     386           16 :                                              p_rmpv(ispin)%matrix)
     387              :             END DO
     388              :          END IF
     389              :       END SELECT
     390              : 
     391           36 :    END SUBROUTINE get_restart_wfn
     392              : 
     393              : ! **************************************************************************************************
     394              : !> \brief Set mo_array(ispin)%uniform_occupation after a restart
     395              : !> \param mo_array ...
     396              : !> \param nspin ...
     397              : !> \author Guillaume Le Breton (03.23)
     398              : ! **************************************************************************************************
     399              : 
     400           34 :    SUBROUTINE set_uniform_occupation_mo_array(mo_array, nspin)
     401              : 
     402              :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mo_array
     403              :       INTEGER                                            :: nspin
     404              : 
     405              :       INTEGER                                            :: ispin, mo
     406              :       LOGICAL                                            :: is_uniform
     407              : 
     408           86 :       DO ispin = 1, nspin
     409           52 :          is_uniform = .TRUE.
     410          274 :          DO mo = 1, mo_array(ispin)%nmo
     411              :             IF (mo_array(ispin)%occupation_numbers(mo) /= 0.0_dp .AND. &
     412          222 :                 mo_array(ispin)%occupation_numbers(mo) /= 1.0_dp .AND. &
     413           52 :                 mo_array(ispin)%occupation_numbers(mo) /= 2.0_dp) THEN
     414           48 :                is_uniform = .FALSE.
     415              :             END IF
     416              :          END DO
     417           86 :          mo_array(ispin)%uniform_occupation = is_uniform
     418              :       END DO
     419              : 
     420           34 :    END SUBROUTINE set_uniform_occupation_mo_array
     421              : 
     422              : ! **************************************************************************************************
     423              : !> \brief calculates the density from the complex MOs and passes the density to
     424              : !>        qs_env.
     425              : !> \param qs_env ...
     426              : !> \author Florian Schiffmann (02.09)
     427              : ! **************************************************************************************************
     428              : 
     429         2618 :    SUBROUTINE calc_update_rho(qs_env)
     430              : 
     431              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     432              : 
     433              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'calc_update_rho'
     434              :       REAL(KIND=dp), PARAMETER                           :: one = 1.0_dp, zero = 0.0_dp
     435              : 
     436              :       INTEGER                                            :: handle, i, im, ncol, re
     437              :       REAL(KIND=dp)                                      :: alpha
     438              :       TYPE(cp_fm_type)                                   :: mos_occ
     439         2618 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new
     440         2618 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao, rho_ao_im
     441         2618 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     442              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     443              :       TYPE(qs_rho_type), POINTER                         :: rho
     444              :       TYPE(rt_prop_type), POINTER                        :: rtp
     445              : 
     446         2618 :       CALL timeset(routineN, handle)
     447              : 
     448         2618 :       NULLIFY (rho, ks_env, mos_new, rtp)
     449              :       CALL get_qs_env(qs_env, &
     450              :                       ks_env=ks_env, &
     451              :                       rho=rho, &
     452              :                       rtp=rtp, &
     453         2618 :                       mos=mos)
     454         2618 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     455         2618 :       CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao)
     456         5814 :       DO i = 1, SIZE(mos_new)/2
     457         3196 :          re = 2*i - 1; im = 2*i
     458         3196 :          CALL dbcsr_set(rho_ao(i)%matrix, zero)
     459         3196 :          CALL cp_fm_get_info(mos_new(re), ncol_global=ncol)
     460              :          CALL cp_fm_create(mos_occ, &
     461              :                            matrix_struct=mos(i)%mo_coeff%matrix_struct, &
     462         3196 :                            name="mos_occ")
     463         3196 :          CALL cp_fm_to_fm(mos_new(re), mos_occ)
     464         3196 :          IF (mos(i)%uniform_occupation) THEN
     465         3004 :             alpha = 3*one - REAL(SIZE(mos_new)/2, dp)
     466        14468 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     467              :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     468              :                                        matrix_v=mos_occ, &
     469              :                                        ncol=ncol, &
     470         3004 :                                        alpha=alpha)
     471              :          ELSE
     472          192 :             alpha = 1.0_dp
     473         1488 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     474              :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     475              :                                        matrix_v=mos_new(re), &
     476              :                                        matrix_g=mos_occ, &
     477              :                                        ncol=ncol, &
     478          192 :                                        alpha=alpha)
     479              :          END IF
     480              : 
     481              :          ! It is actually complex conjugate but i*i=-1 therefore it must be added
     482         3196 :          CALL cp_fm_to_fm(mos_new(im), mos_occ)
     483         3196 :          IF (mos(i)%uniform_occupation) THEN
     484         3004 :             alpha = 3*one - REAL(SIZE(mos_new)/2, dp)
     485        14468 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     486              :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     487              :                                        matrix_v=mos_occ, &
     488              :                                        ncol=ncol, &
     489         3004 :                                        alpha=alpha)
     490              :          ELSE
     491          192 :             alpha = 1.0_dp
     492         1488 :             CALL cp_fm_column_scale(mos_occ, mos(i)%occupation_numbers/alpha)
     493              :             CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=rho_ao(i)%matrix, &
     494              :                                        matrix_v=mos_new(im), &
     495              :                                        matrix_g=mos_occ, &
     496              :                                        ncol=ncol, &
     497          192 :                                        alpha=alpha)
     498              :          END IF
     499         9010 :          CALL cp_fm_release(mos_occ)
     500              :       END DO
     501         2618 :       CALL qs_rho_update_rho(rho, qs_env)
     502              : 
     503         2618 :       IF (rtp%track_imag_density) THEN
     504         1958 :          CALL qs_rho_get(rho_struct=rho, rho_ao_im=rho_ao_im)
     505         1958 :          CALL calculate_P_imaginary(qs_env, rtp, rho_ao_im, keep_sparsity=.TRUE.)
     506         1958 :          CALL qs_rho_set(rho, rho_ao_im=rho_ao_im)
     507              :       END IF
     508              : 
     509         2618 :       CALL qs_ks_did_change(ks_env, rho_changed=.TRUE.)
     510              : 
     511         2618 :       CALL timestop(handle)
     512              : 
     513         2618 :    END SUBROUTINE calc_update_rho
     514              : 
     515              : ! **************************************************************************************************
     516              : !> \brief Copies the density matrix back into the qs_env%rho%rho_ao
     517              : !> \param qs_env ...
     518              : !> \author Samuel Andermatt (3.14)
     519              : ! **************************************************************************************************
     520              : 
     521         1268 :    SUBROUTINE calc_update_rho_sparse(qs_env)
     522              : 
     523              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     524              : 
     525              :       CHARACTER(len=*), PARAMETER :: routineN = 'calc_update_rho_sparse'
     526              :       REAL(KIND=dp), PARAMETER                           :: zero = 0.0_dp
     527              : 
     528              :       INTEGER                                            :: handle, ispin
     529         1268 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao, rho_ao_im, rho_new
     530              :       TYPE(dft_control_type), POINTER                    :: dft_control
     531              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     532              :       TYPE(qs_rho_type), POINTER                         :: rho
     533              :       TYPE(rt_prop_type), POINTER                        :: rtp
     534              :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     535              : 
     536         1268 :       NULLIFY (rho, ks_env, rtp, dft_control)
     537         1268 :       CALL timeset(routineN, handle)
     538              :       CALL get_qs_env(qs_env, &
     539              :                       ks_env=ks_env, &
     540              :                       rho=rho, &
     541              :                       rtp=rtp, &
     542         1268 :                       dft_control=dft_control)
     543         1268 :       rtp_control => dft_control%rtp_control
     544         1268 :       CALL get_rtp(rtp=rtp, rho_new=rho_new)
     545         1268 :       CALL qs_rho_get(rho_struct=rho, rho_ao=rho_ao)
     546         1268 :       IF (rtp%track_imag_density) CALL qs_rho_get(rho_struct=rho, rho_ao_im=rho_ao_im)
     547         2954 :       DO ispin = 1, SIZE(rho_ao)
     548         1686 :          CALL dbcsr_set(rho_ao(ispin)%matrix, zero)
     549         1686 :          CALL dbcsr_copy(rho_ao(ispin)%matrix, rho_new(ispin*2 - 1)%matrix, keep_sparsity=.TRUE.)
     550         2954 :          IF (rtp%track_imag_density) THEN
     551          610 :             CALL dbcsr_copy(rho_ao_im(ispin)%matrix, rho_new(ispin*2)%matrix, keep_sparsity=.TRUE.)
     552              :          END IF
     553              :       END DO
     554              : 
     555         1268 :       CALL qs_rho_update_rho(rho, qs_env)
     556         1268 :       CALL qs_ks_did_change(ks_env, rho_changed=.TRUE.)
     557              : 
     558         1268 :       CALL timestop(handle)
     559              : 
     560         1268 :    END SUBROUTINE calc_update_rho_sparse
     561              : 
     562              : ! **************************************************************************************************
     563              : !> \brief ...
     564              : !> \param qs_env ...
     565              : !> \param rtp ...
     566              : !> \param matrix_p_im ...
     567              : !> \param keep_sparsity ...
     568              : ! **************************************************************************************************
     569         1958 :    SUBROUTINE calculate_P_imaginary(qs_env, rtp, matrix_p_im, keep_sparsity)
     570              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     571              :       TYPE(rt_prop_type), POINTER                        :: rtp
     572              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_p_im
     573              :       LOGICAL, OPTIONAL                                  :: keep_sparsity
     574              : 
     575              :       INTEGER                                            :: i, im, ncol, re
     576              :       LOGICAL                                            :: my_keep_sparsity
     577              :       REAL(KIND=dp)                                      :: alpha
     578         1958 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new, mos_occ
     579         1958 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     580              : 
     581         1958 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     582              : 
     583         1958 :       my_keep_sparsity = .FALSE.
     584         1958 :       IF (PRESENT(keep_sparsity)) my_keep_sparsity = keep_sparsity
     585         1958 :       CALL get_qs_env(qs_env, mos=mos)
     586        10490 :       ALLOCATE (mos_occ(SIZE(mos)*2))
     587              : 
     588         4266 :       DO i = 1, SIZE(mos_new)/2
     589         2308 :          re = 2*i - 1; im = 2*i
     590         2308 :          alpha = 3.0_dp - REAL(SIZE(matrix_p_im), dp)
     591              :          CALL cp_fm_create(mos_occ(re), &
     592              :                            matrix_struct=mos(i)%mo_coeff%matrix_struct, &
     593         2308 :                            name="mos_occ")
     594              :          CALL cp_fm_create(mos_occ(im), &
     595              :                            matrix_struct=mos(i)%mo_coeff%matrix_struct, &
     596         2308 :                            name="mos_occ")
     597         2308 :          CALL dbcsr_set(matrix_p_im(i)%matrix, 0.0_dp)
     598         2308 :          CALL cp_fm_get_info(mos_new(re), ncol_global=ncol)
     599         2308 :          CALL cp_fm_to_fm(mos_new(re), mos_occ(re))
     600        12138 :          CALL cp_fm_column_scale(mos_occ(re), mos(i)%occupation_numbers/alpha)
     601         2308 :          CALL cp_fm_to_fm(mos_new(im), mos_occ(im))
     602        12138 :          CALL cp_fm_column_scale(mos_occ(im), mos(i)%occupation_numbers/alpha)
     603              :          CALL cp_dbcsr_plus_fm_fm_t(sparse_matrix=matrix_p_im(i)%matrix, &
     604              :                                     matrix_v=mos_occ(im), &
     605              :                                     matrix_g=mos_occ(re), &
     606              :                                     ncol=ncol, &
     607              :                                     keep_sparsity=my_keep_sparsity, &
     608              :                                     alpha=2.0_dp*alpha, &
     609         6574 :                                     symmetry_mode=-1)
     610              :       END DO
     611         1958 :       CALL cp_fm_release(mos_occ)
     612              : 
     613         1958 :    END SUBROUTINE calculate_P_imaginary
     614              : 
     615              : ! **************************************************************************************************
     616              : !> \brief ...
     617              : !> \param qs_env ...
     618              : !> \param rtp ...
     619              : ! **************************************************************************************************
     620          848 :    SUBROUTINE write_rtp_mos_to_output_unit(qs_env, rtp)
     621              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     622              :       TYPE(rt_prop_type), POINTER                        :: rtp
     623              : 
     624              :       CHARACTER(len=*), PARAMETER :: routineN = 'write_rtp_mos_to_output_unit'
     625              : 
     626              :       CHARACTER(LEN=10)                                  :: spin
     627              :       CHARACTER(LEN=2*default_string_length)             :: name
     628              :       INTEGER                                            :: handle, i, ispin, nao, nelectron, nmo, &
     629              :                                                             nspins
     630              :       LOGICAL                                            :: print_eigvecs, print_mo_info
     631              :       REAL(KIND=dp)                                      :: flexible_electron_count, maxocc, n_el_f
     632          424 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     633          424 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new
     634              :       TYPE(cp_logger_type), POINTER                      :: logger
     635              :       TYPE(mo_set_type)                                  :: mo_set_rtp
     636          424 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     637          424 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     638          424 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     639              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     640              : 
     641          424 :       NULLIFY (atomic_kind_set, particle_set, qs_kind_set, input, mos, dft_section)
     642              : 
     643          424 :       CALL timeset(routineN, handle)
     644              : 
     645              :       CALL get_qs_env(qs_env, &
     646              :                       atomic_kind_set=atomic_kind_set, &
     647              :                       qs_kind_set=qs_kind_set, &
     648              :                       particle_set=particle_set, &
     649              :                       input=input, &
     650          424 :                       mos=mos)
     651              :       ! Quick return, if no printout of MO information is requested
     652          424 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     653          424 :       CALL section_vals_val_get(dft_section, "PRINT%MO%EIGENVECTORS", l_val=print_eigvecs)
     654              : 
     655          424 :       NULLIFY (logger)
     656          424 :       logger => cp_get_default_logger()
     657              :       print_mo_info = (cp_print_key_should_output(logger%iter_info, &
     658              :                                                   dft_section, "PRINT%MO") /= 0) .OR. &
     659          424 :                       (qs_env%sim_step == 1)
     660              : 
     661          126 :       IF ((.NOT. print_mo_info) .OR. (.NOT. print_eigvecs)) THEN
     662          420 :          CALL timestop(handle)
     663          420 :          RETURN
     664              :       END IF
     665              : 
     666            4 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     667              : 
     668            4 :       nspins = SIZE(mos_new)/2
     669              : 
     670            8 :       DO ispin = 1, nspins
     671              :          ! initiate mo_set
     672              :          CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=nmo, nelectron=nelectron, &
     673            4 :                          n_el_f=n_el_f, maxocc=maxocc, flexible_electron_count=flexible_electron_count)
     674              : 
     675              :          CALL allocate_mo_set(mo_set_rtp, &
     676              :                               nao=nao, &
     677              :                               nmo=nmo, &
     678              :                               nelectron=nelectron, &
     679              :                               n_el_f=n_el_f, &
     680              :                               maxocc=maxocc, &
     681            4 :                               flexible_electron_count=flexible_electron_count)
     682              : 
     683            4 :          WRITE (name, FMT="(A,I6)") "RTP MO SET, SPIN ", ispin
     684            4 :          CALL init_mo_set(mo_set_rtp, fm_ref=mos_new(2*ispin - 1), name=name)
     685              : 
     686            4 :          IF (nspins > 1) THEN
     687            0 :             IF (ispin == 1) THEN
     688            0 :                spin = "ALPHA SPIN"
     689              :             ELSE
     690            0 :                spin = "BETA SPIN"
     691              :             END IF
     692              :          ELSE
     693            4 :             spin = ""
     694              :          END IF
     695              : 
     696            8 :          mo_set_rtp%occupation_numbers = mos(ispin)%occupation_numbers
     697              : 
     698              :          !loop for real (odd) and imaginary (even) parts
     699           12 :          DO i = 1, 0, -1
     700            8 :             CALL cp_fm_to_fm(mos_new(2*ispin - i), mo_set_rtp%mo_coeff)
     701              :             CALL write_mo_set_to_output_unit(mo_set_rtp, qs_kind_set, particle_set, &
     702              :                                              dft_section, 4, 0, rtp=.TRUE., spin=TRIM(spin), &
     703           12 :                                              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          424 :    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          424 :    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          424 :       INTEGER, DIMENSION(:), POINTER                     :: list, list_index
     731              :       LOGICAL                                            :: append_cube, do_kpoints, mpi_io
     732          424 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: zcharge
     733          424 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     734              :       TYPE(cell_type), POINTER                           :: cell
     735              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     736          424 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new
     737              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     738              :       TYPE(cp_logger_type), POINTER                      :: logger
     739              :       TYPE(dft_control_type), POINTER                    :: dft_control
     740          424 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     741              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     742              :       TYPE(particle_list_type), POINTER                  :: particles
     743          424 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     744              :       TYPE(pw_c1d_gs_type)                               :: wf_g
     745              :       TYPE(pw_env_type), POINTER                         :: pw_env
     746          424 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
     747              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     748              :       TYPE(pw_r3d_rs_type)                               :: density_r, wf_r
     749          424 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     750              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     751              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     752              : 
     753          424 :       CALL timeset(routineN, handle)
     754              : 
     755          424 :       NULLIFY (logger, auxbas_pw_pool, pw_pools, pw_env)
     756              : 
     757              :       ! Get all the info from qs:
     758              :       CALL get_qs_env(qs_env, do_kpoints=do_kpoints, &
     759          424 :                       input=input)
     760              : 
     761              :       ! Kill the run in the case of K points
     762          424 :       IF (do_kpoints) THEN
     763            0 :          CPABORT("K points not handled yet for printing MO_CUBE")
     764              :       END IF
     765              : 
     766          424 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     767          424 :       logger => cp_get_default_logger()
     768              : 
     769              :       ! Quick return if no print required
     770          424 :       IF (.NOT. BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
     771              :                                                  "PRINT%MO_CUBES"), cp_p_file)) THEN
     772          400 :          CALL timestop(handle)
     773              :          RETURN
     774              :       END IF
     775              : 
     776              :       CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, &
     777              :                       mos=mos, &
     778              :                       blacs_env=blacs_env, &
     779              :                       qs_kind_set=qs_kind_set, &
     780              :                       pw_env=pw_env, &
     781              :                       subsys=subsys, &
     782              :                       para_env=para_env, &
     783              :                       particle_set=particle_set, &
     784           24 :                       dft_control=dft_control)
     785           24 :       CALL qs_subsys_get(subsys, particles=particles)
     786           24 :       CALL get_effective_core_charges(qs_env, zcharge)
     787              : 
     788           24 :       nspins = dft_control%nspins
     789           24 :       rt_time_step = qs_env%sim_step
     790              : 
     791              :       ! Setup the grids needed to compute a wavefunction given a vector
     792              :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
     793           24 :                       pw_pools=pw_pools)
     794           24 :       CALL auxbas_pw_pool%create_pw(wf_r)
     795           24 :       CALL auxbas_pw_pool%create_pw(wf_g)
     796           24 :       CALL auxbas_pw_pool%create_pw(density_r)
     797           24 :       CALL get_rtp(rtp=rtp, mos_new=mos_new)
     798              : 
     799           70 :       DO ispin = 1, nspins
     800           46 :          CALL get_mo_set(mo_set=mos(ispin), homo=homo)
     801              : 
     802           46 :          nhomo = section_get_ival(dft_section, "PRINT%MO_CUBES%NHOMO")
     803           46 :          append_cube = section_get_lval(dft_section, "PRINT%MO_CUBES%APPEND")
     804           46 :          my_pos_cube = "REWIND"
     805           46 :          IF (append_cube) THEN
     806            0 :             my_pos_cube = "APPEND"
     807              :          END IF
     808           46 :          CALL section_vals_val_get(dft_section, "PRINT%MO_CUBES%HOMO_LIST", n_rep_val=n_rep)
     809           46 :          IF (n_rep > 0) THEN ! write the cubes of the list
     810            0 :             nlist = 0
     811            0 :             DO ir = 1, n_rep
     812            0 :                NULLIFY (list)
     813              :                CALL section_vals_val_get(dft_section, "PRINT%MO_CUBES%HOMO_LIST", i_rep_val=ir, &
     814            0 :                                          i_vals=list)
     815            0 :                IF (ASSOCIATED(list)) THEN
     816            0 :                   CALL reallocate(list_index, 1, nlist + SIZE(list))
     817            0 :                   DO i = 1, SIZE(list)
     818            0 :                      list_index(i + nlist) = list(i)
     819              :                   END DO
     820            0 :                   nlist = nlist + SIZE(list)
     821              :                END IF
     822              :             END DO
     823              :          ELSE
     824              : 
     825           46 :             IF (nhomo == -1) nhomo = homo
     826           46 :             nlist = homo - MAX(1, homo - nhomo + 1) + 1
     827          138 :             ALLOCATE (list_index(nlist))
     828          224 :             DO i = 1, nlist
     829          224 :                list_index(i) = MAX(1, homo - nhomo + 1) + i - 1
     830              :             END DO
     831              :          END IF
     832          224 :          DO i = 1, nlist
     833          178 :             ivector = list_index(i)
     834              :             CALL get_qs_env(qs_env=qs_env, &
     835              :                             atomic_kind_set=atomic_kind_set, &
     836              :                             qs_kind_set=qs_kind_set, &
     837              :                             cell=cell, &
     838              :                             particle_set=particle_set, &
     839          178 :                             pw_env=pw_env)
     840              : 
     841              :             ! density_r contains the density of the MOs
     842          178 :             CALL pw_zero(density_r)
     843          178 :             mo_coeff => mos_new(2*ispin - 1)!Real coeff
     844              :             CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
     845          178 :                                         cell, dft_control, particle_set, pw_env)
     846              :             ! Adding the real part
     847          178 :             CALL pw_multiply(density_r, wf_r, wf_r, 1.0_dp)
     848              : 
     849          178 :             mo_coeff => mos_new(2*ispin) !Im coeff
     850              :             CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, qs_kind_set, &
     851          178 :                                         cell, dft_control, particle_set, pw_env)
     852              :             ! Adding the im part
     853          178 :             CALL pw_multiply(density_r, wf_r, wf_r, 1.0_dp)
     854              : 
     855          178 :             WRITE (filename, '(a4,I5.5,a1,I1.1)') "WFN_", ivector, "_", ispin
     856          178 :             mpi_io = .TRUE.
     857              :             unit_nr = cp_print_key_unit_nr(logger, input, "DFT%PRINT%MO_CUBES", extension=".cube", &
     858              :                                            middle_name=TRIM(filename), file_position=my_pos_cube, log_filename=.FALSE., &
     859          178 :                                            mpi_io=mpi_io)
     860          178 :             WRITE (title, *) "DENSITY ", ivector, " spin ", ispin, " i.e. HOMO - ", ivector - homo
     861              :             CALL cp_pw_to_cube(density_r, unit_nr, title, particles=particles, zeff=zcharge, &
     862          178 :                                stride=section_get_ivals(dft_section, "PRINT%MO_CUBES%STRIDE"), mpi_io=mpi_io)
     863          224 :             CALL cp_print_key_finished_output(unit_nr, logger, input, "DFT%PRINT%MO_CUBES", mpi_io=mpi_io)
     864              :          END DO
     865          162 :          IF (ASSOCIATED(list_index)) DEALLOCATE (list_index)
     866              :       END DO
     867              : 
     868              :       ! Deallocate grids needed to compute wavefunctions
     869           24 :       CALL auxbas_pw_pool%give_back_pw(wf_r)
     870           24 :       CALL auxbas_pw_pool%give_back_pw(wf_g)
     871           24 :       CALL auxbas_pw_pool%give_back_pw(density_r)
     872           24 :       DEALLOCATE (zcharge)
     873              : 
     874           24 :       CALL timestop(handle)
     875              : 
     876          424 :    END SUBROUTINE write_rtp_mo_cubes
     877              : 
     878              : ! **************************************************************************************************
     879              : !> \brief Warn about unused sections of the print section - only implemented for some of the methods
     880              : !> \param section Parent section
     881              : !> \param subsection_name Name of the subsection which is not used even when explicitly included
     882              : !> \param error_message Message to display as warning
     883              : !> \author Stepan Marek (01.25)
     884              : ! **************************************************************************************************
     885         1172 :    SUBROUTINE warn_section_unused(section, subsection_name, error_message)
     886              :       TYPE(section_vals_type), POINTER                   :: section
     887              :       CHARACTER(len=*)                                   :: subsection_name, error_message
     888              : 
     889              :       LOGICAL                                            :: explicit
     890              :       TYPE(section_vals_type), POINTER                   :: target_section
     891              : 
     892          586 :       target_section => section_vals_get_subs_vals(section, subsection_name)
     893          586 :       CALL section_vals_get(target_section, explicit=explicit)
     894          586 :       IF (explicit) CPWARN(error_message)
     895          586 :    END SUBROUTINE warn_section_unused
     896              : ! **************************************************************************************************
     897              : !> \brief Attempt to read the moments from a previously written file
     898              : !> \param moments_section Print key section for the moments trace
     899              : !> \param orig_start Index from which the original calculation started (given by input file)
     900              : !> \param current_start The current start index from which the calculation continues
     901              : !> \param moments Moment trace, where the read entries are overwritten
     902              : !> \param times Optional time trace, where again the read entries are overwritten
     903              : !> \param mom_read Whether moments were actually read
     904              : !> \author Stepan Marek (11.25)
     905              : ! **************************************************************************************************
     906           32 :    SUBROUTINE read_moments(moments_section, orig_start, current_start, moments, times, mom_read)
     907              :       TYPE(section_vals_type), POINTER                   :: moments_section
     908              :       INTEGER                                            :: orig_start, current_start
     909              :       COMPLEX(kind=dp), DIMENSION(:, :, :), POINTER      :: moments
     910              :       REAL(kind=dp), DIMENSION(:), OPTIONAL, POINTER     :: times
     911              :       LOGICAL, OPTIONAL                                  :: mom_read
     912              : 
     913              :       CHARACTER(len=14), DIMENSION(4)                    :: file_extensions
     914              :       CHARACTER(len=default_path_length)                 :: save_name_im, save_name_re
     915              :       INTEGER                                            :: i, k, max_n, n, n_spin, unit_im, unit_re
     916              :       REAL(kind=dp)                                      :: time
     917              :       REAL(kind=dp), DIMENSION(3)                        :: moments_im, moments_re
     918              :       TYPE(cp_logger_type), POINTER                      :: logger
     919              : 
     920              : ! Whether at least some moments where extracted from the files
     921              : 
     922           32 :       file_extensions(1) = "_SPIN_A_RE.dat"
     923           32 :       file_extensions(2) = "_SPIN_A_IM.dat"
     924           32 :       file_extensions(3) = "_SPIN_B_RE.dat"
     925           32 :       file_extensions(4) = "_SPIN_B_IM.dat"
     926              : 
     927           32 :       IF (PRESENT(mom_read)) mom_read = .FALSE.
     928              : 
     929           32 :       n_spin = SIZE(moments, 1)
     930              : 
     931           32 :       logger => cp_get_default_logger()
     932           32 :       max_n = current_start - orig_start + 1
     933              : 
     934           76 :       DO i = 1, n_spin
     935              :          ! Open the relevant files
     936              :          save_name_re = cp_print_key_generate_filename(logger, moments_section, &
     937           44 :                                                        extension=file_extensions(2*i - 1), my_local=.FALSE.)
     938              :          save_name_im = cp_print_key_generate_filename(logger, moments_section, &
     939           44 :                                                        extension=file_extensions(2*i), my_local=.FALSE.)
     940           76 :          IF (file_exists(save_name_re) .AND. file_exists(save_name_im)) THEN
     941              :             CALL open_file(save_name_re, file_status="OLD", file_form="FORMATTED", file_action="READ", &
     942            0 :                            unit_number=unit_re)
     943              :             CALL open_file(save_name_im, file_status="OLD", file_form="FORMATTED", file_action="READ", &
     944            0 :                            unit_number=unit_im)
     945              :             ! Skip the headers
     946            0 :             READ (unit_re, '()')
     947            0 :             READ (unit_im, '()')
     948            0 :             DO n = 1, max_n
     949              :                ! Read the data
     950            0 :                READ (unit_re, '(E20.8E3,E20.8E3,E20.8E3,E20.8E3,E20.8E3)') time, &
     951            0 :                   moments_re(1), moments_re(2), moments_re(3)
     952            0 :                READ (unit_im, '(E20.8E3,E20.8E3,E20.8E3,E20.8E3,E20.8E3)') time, &
     953            0 :                   moments_im(1), moments_im(2), moments_im(3)
     954            0 :                DO k = 1, 3
     955            0 :                   moments(i, k, n) = CMPLX(moments_re(k), moments_im(k), kind=dp)
     956              :                END DO
     957            0 :                IF (PRESENT(times)) THEN
     958            0 :                   times(n) = time/femtoseconds
     959              :                END IF
     960              :             END DO
     961            0 :             IF (PRESENT(mom_read)) mom_read = .TRUE.
     962            0 :             CALL close_file(unit_re)
     963            0 :             CALL close_file(unit_im)
     964              :          ELSE
     965          948 :             moments(i, :, 1:max_n) = CMPLX(0.0, 0.0, kind=dp)
     966           44 :             CPWARN("Could not read at least one moments file - missing trace is set to zero.")
     967              :          END IF
     968              :       END DO
     969           32 :    END SUBROUTINE read_moments
     970              : 
     971              : ! **************************************************************************************************
     972              : !> \brief Recalculates the field for index range given by orig_start and i_start,
     973              : !>        with times taken from the times array
     974              : !> \param fields Array, where relevant indices are recalculated
     975              : !> \param times Array of input times
     976              : !> \param orig_start original start (in case of restarts)
     977              : !> \param i_start current start
     978              : !> \param dft_control DFT parameters
     979              : !> \date 11.2025
     980              : !> \author Stepan Marek
     981              : ! **************************************************************************************************
     982           18 :    SUBROUTINE recalculate_fields(fields, times, orig_start, i_start, dft_control)
     983              :       COMPLEX(kind=dp), DIMENSION(:, :), POINTER         :: fields
     984              :       REAL(kind=dp), DIMENSION(:), POINTER               :: times
     985              :       INTEGER                                            :: orig_start, i_start
     986              :       TYPE(dft_control_type), POINTER                    :: dft_control
     987              : 
     988              :       INTEGER                                            :: i, max_n
     989              :       REAL(kind=dp), DIMENSION(3)                        :: field
     990              : 
     991           18 :       max_n = i_start - orig_start + 1
     992              : 
     993           18 :       IF (dft_control%rtp_control%apply_delta_pulse .OR. &
     994              :           dft_control%rtp_control%apply_delta_pulse_mag) THEN
     995           90 :          fields(:, 1:max_n) = 0.0_dp
     996              :       ELSE
     997            0 :          DO i = 1, max_n
     998            0 :             CALL make_field(dft_control, field, i + orig_start - 1, times(i))
     999            0 :             fields(:, i) = field(:)
    1000              :          END DO
    1001              :       END IF
    1002           18 :    END SUBROUTINE recalculate_fields
    1003              : 
    1004              : END MODULE rt_propagation_utils
        

Generated by: LCOV version 2.0-1