LCOV - code coverage report
Current view: top level - src/motion - rt_propagation.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:4dc10b3) Lines: 207 213 97.2 %
Date: 2024-11-21 06:45:46 Functions: 6 6 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 for the real time propagation.
      10             : !> \author Florian Schiffmann (02.09)
      11             : ! **************************************************************************************************
      12             : 
      13             : MODULE rt_propagation
      14             :    USE bibliography,                    ONLY: Andermatt2016,&
      15             :                                               cite_reference
      16             :    USE cp_control_types,                ONLY: dft_control_type,&
      17             :                                               rtp_control_type
      18             :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      19             :                                               dbcsr_p_type
      20             :    USE cp_external_control,             ONLY: external_control
      21             :    USE cp_fm_types,                     ONLY: cp_fm_set_all,&
      22             :                                               cp_fm_to_fm,&
      23             :                                               cp_fm_type
      24             :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      25             :                                               cp_logger_type,&
      26             :                                               cp_to_string
      27             :    USE cp_output_handling,              ONLY: cp_add_iter_level,&
      28             :                                               cp_iterate,&
      29             :                                               cp_p_file,&
      30             :                                               cp_print_key_generate_filename,&
      31             :                                               cp_print_key_should_output,&
      32             :                                               cp_print_key_unit_nr,&
      33             :                                               cp_rm_iter_level
      34             :    USE efield_utils,                    ONLY: calculate_ecore_efield
      35             :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      36             :    USE force_env_types,                 ONLY: force_env_get,&
      37             :                                               force_env_type
      38             :    USE global_types,                    ONLY: global_environment_type
      39             :    USE hfx_admm_utils,                  ONLY: hfx_admm_init
      40             :    USE input_constants,                 ONLY: real_time_propagation,&
      41             :                                               use_restart_wfn,&
      42             :                                               use_rt_restart,&
      43             :                                               use_scf_wfn
      44             :    USE input_cp2k_restarts,             ONLY: write_restart
      45             :    USE input_section_types,             ONLY: section_vals_get,&
      46             :                                               section_vals_get_subs_vals,&
      47             :                                               section_vals_type,&
      48             :                                               section_vals_val_get,&
      49             :                                               section_vals_val_set
      50             :    USE kinds,                           ONLY: default_path_length,&
      51             :                                               dp
      52             :    USE machine,                         ONLY: m_walltime
      53             :    USE md_environment_types,            ONLY: md_environment_type
      54             :    USE pw_env_types,                    ONLY: pw_env_type
      55             :    USE qs_core_hamiltonian,             ONLY: qs_matrix_h_allocate_imag_from_real
      56             :    USE qs_energy_init,                  ONLY: qs_energies_init
      57             :    USE qs_energy_types,                 ONLY: qs_energy_type
      58             :    USE qs_environment_types,            ONLY: get_qs_env,&
      59             :                                               qs_environment_type
      60             :    USE qs_external_potential,           ONLY: external_c_potential,&
      61             :                                               external_e_potential
      62             :    USE qs_ks_methods,                   ONLY: qs_ks_allocate_basics,&
      63             :                                               qs_ks_update_qs_env
      64             :    USE qs_ks_types,                     ONLY: qs_ks_did_change,&
      65             :                                               qs_ks_env_type,&
      66             :                                               set_ks_env
      67             :    USE qs_mo_io,                        ONLY: wfn_restart_file_name
      68             :    USE qs_mo_types,                     ONLY: get_mo_set,&
      69             :                                               init_mo_set,&
      70             :                                               mo_set_type
      71             :    USE qs_rho_methods,                  ONLY: allocate_rho_ao_imag_from_real
      72             :    USE qs_rho_types,                    ONLY: qs_rho_set,&
      73             :                                               qs_rho_type
      74             :    USE rt_delta_pulse,                  ONLY: apply_delta_pulse
      75             :    USE rt_hfx_utils,                    ONLY: rtp_hfx_rebuild
      76             :    USE rt_projection_mo_utils,          ONLY: init_mo_projection
      77             :    USE rt_propagation_methods,          ONLY: propagation_step
      78             :    USE rt_propagation_output,           ONLY: rt_prop_output
      79             :    USE rt_propagation_types,            ONLY: get_rtp,&
      80             :                                               rt_prop_create,&
      81             :                                               rt_prop_type,&
      82             :                                               rtp_create_SinvH_imag,&
      83             :                                               rtp_history_create
      84             :    USE rt_propagation_utils,            ONLY: calc_S_derivs,&
      85             :                                               calc_update_rho,&
      86             :                                               calc_update_rho_sparse,&
      87             :                                               get_restart_wfn
      88             :    USE rt_propagation_velocity_gauge,   ONLY: velocity_gauge_ks_matrix
      89             :    USE rt_propagator_init,              ONLY: init_propagators,&
      90             :                                               rt_initialize_rho_from_mos
      91             : #include "../base/base_uses.f90"
      92             : 
      93             :    IMPLICIT NONE
      94             : 
      95             :    PRIVATE
      96             : 
      97             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation'
      98             : 
      99             :    PUBLIC :: rt_prop_setup
     100             : 
     101             : CONTAINS
     102             : 
     103             : ! **************************************************************************************************
     104             : !> \brief creates rtp_type, gets the initial state, either by reading MO's
     105             : !>        from file or calling SCF run
     106             : !> \param force_env ...
     107             : !> \author Florian Schiffmann (02.09)
     108             : ! **************************************************************************************************
     109             : 
     110         594 :    SUBROUTINE rt_prop_setup(force_env)
     111             :       TYPE(force_env_type), POINTER                      :: force_env
     112             : 
     113             :       INTEGER                                            :: aspc_order
     114             :       LOGICAL                                            :: magnetic, vel_reprs
     115             :       TYPE(dft_control_type), POINTER                    :: dft_control
     116             :       TYPE(global_environment_type), POINTER             :: globenv
     117             :       TYPE(qs_energy_type), POINTER                      :: energy
     118             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     119             :       TYPE(rt_prop_type), POINTER                        :: rtp
     120             :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     121             :       TYPE(section_vals_type), POINTER                   :: hfx_sections, input, ls_scf_section, &
     122             :                                                             md_section, motion_section, &
     123             :                                                             print_moments_section
     124             : 
     125         198 :       NULLIFY (qs_env, rtp_control, dft_control)
     126             : 
     127         198 :       CALL cite_reference(Andermatt2016)
     128             : 
     129         198 :       CALL force_env_get(force_env=force_env, qs_env=qs_env, globenv=globenv)
     130         198 :       CALL get_qs_env(qs_env, dft_control=dft_control, energy=energy)
     131         198 :       rtp_control => dft_control%rtp_control
     132             : 
     133             :       ! Takes care that an initial wavefunction/density is available
     134             :       ! Can either be by performing an scf loop or reading a restart
     135         198 :       CALL rt_initial_guess(qs_env, force_env, rtp_control)
     136             : 
     137             :       ! Initializes the extrapolation
     138         198 :       NULLIFY (rtp)
     139         198 :       CALL get_qs_env(qs_env=qs_env, rtp=rtp, input=input)
     140         198 :       aspc_order = rtp_control%aspc_order
     141         198 :       CALL rtp_history_create(rtp, aspc_order)
     142             : 
     143             :       ! Reads the simulation parameters from the input
     144         198 :       motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
     145         198 :       md_section => section_vals_get_subs_vals(motion_section, "MD")
     146         198 :       hfx_sections => section_vals_get_subs_vals(force_env%root_section, "FORCE_EVAL%DFT%XC%HF")
     147         198 :       print_moments_section => section_vals_get_subs_vals(force_env%root_section, "FORCE_EVAL%DFT%PRINT%MOMENTS")
     148         198 :       CALL section_vals_val_get(md_section, "TIMESTEP", r_val=qs_env%rtp%dt)
     149         198 :       CALL section_vals_val_get(md_section, "STEP_START_VAL", i_val=qs_env%rtp%i_start)
     150         198 :       CALL section_vals_val_get(md_section, "STEPS", i_val=rtp%nsteps)
     151         198 :       CALL section_vals_val_get(md_section, "MAX_STEPS", i_val=rtp%max_steps)
     152             : 
     153         198 :       ls_scf_section => section_vals_get_subs_vals(input, "DFT%LS_SCF")
     154         198 :       CALL section_vals_val_get(ls_scf_section, "EPS_FILTER", r_val=rtp%filter_eps)
     155         198 :       IF (.NOT. qs_env%rtp%linear_scaling) rtp%filter_eps = 0.0_dp
     156         198 :       IF (rtp_control%acc_ref < 1) rtp_control%acc_ref = 1
     157         198 :       rtp%filter_eps_small = rtp%filter_eps/rtp_control%acc_ref
     158         198 :       CALL section_vals_val_get(ls_scf_section, "EPS_LANCZOS", r_val=rtp%lanzcos_threshold)
     159         198 :       CALL section_vals_val_get(ls_scf_section, "MAX_ITER_LANCZOS", i_val=rtp%lanzcos_max_iter)
     160         198 :       CALL section_vals_val_get(ls_scf_section, "SIGN_SQRT_ORDER", i_val=rtp%newton_schulz_order)
     161         198 :       CALL section_vals_get(hfx_sections, explicit=rtp%do_hfx)
     162         198 :       CALL section_vals_val_get(print_moments_section, "MAGNETIC", l_val=magnetic)
     163         198 :       CALL section_vals_val_get(print_moments_section, "VEL_REPRS", l_val=vel_reprs)
     164             : 
     165             :       rtp%track_imag_density = (magnetic) .OR. (vel_reprs) .OR. (rtp_control%velocity_gauge) &
     166         198 :                                .OR. (rtp%do_hfx) .OR. (.NOT. rtp_control%fixed_ions)
     167         198 :       rtp%propagate_complex_ks = rtp%do_hfx .OR. rtp_control%velocity_gauge
     168             : 
     169             :       CALL rt_init_complex_quantities(qs_env, imag_p=rtp%track_imag_density, &
     170         198 :                                       imag_ks=rtp%propagate_complex_ks, imag_h=rtp_control%velocity_gauge)
     171             : 
     172             :       ! Hmm, not really like to initialize with the structure of S but I reckon it is
     173             :       ! done everywhere like this
     174         198 :       IF (rtp%do_hfx) CALL rtp_hfx_rebuild(qs_env)
     175             : 
     176             :       ! Setup the MO projection environment if required
     177         198 :       IF (rtp_control%is_proj_mo) CALL init_mo_projection(qs_env, rtp_control)
     178             : 
     179         198 :       CALL init_propagation_run(qs_env)
     180         198 :       IF (.NOT. rtp_control%fixed_ions) THEN
     181             :          !derivativs of the overlap needed for EMD
     182          72 :          CALL calc_S_derivs(qs_env)
     183             :          ! a bit hidden, but computes SinvH and SinvB (calc_SinvH for CN,EM and ARNOLDI)
     184             :          ! make_etrs_exp in case of ETRS in combination with TAYLOR and PADE
     185             :       END IF
     186         198 :       CALL init_propagators(qs_env)
     187         198 :       IF (rtp_control%fixed_ions) THEN
     188         126 :          CALL run_propagation(qs_env, force_env, globenv)
     189             :       ELSE
     190          72 :          rtp_control%initial_step = .TRUE.
     191          72 :          CALL force_env_calc_energy_force(force_env, calc_force=.TRUE.)
     192          72 :          rtp_control%initial_step = .FALSE.
     193          72 :          rtp%energy_old = energy%total
     194             :       END IF
     195             : 
     196         198 :    END SUBROUTINE rt_prop_setup
     197             : 
     198             : ! **************************************************************************************************
     199             : !> \brief calculates the matrices needed in the first step of EMD/RTP
     200             : !> \param qs_env ...
     201             : !> \author Florian Schiffmann (02.09)
     202             : ! **************************************************************************************************
     203             : 
     204         198 :    SUBROUTINE init_propagation_run(qs_env)
     205             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     206             : 
     207             :       REAL(KIND=dp), PARAMETER                           :: zero = 0.0_dp
     208             : 
     209             :       INTEGER                                            :: i, ispin, re
     210             :       INTEGER, DIMENSION(2)                              :: nelectron_spin
     211         198 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new, mos_old
     212         198 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_ks, rho_new, rho_old
     213             :       TYPE(dft_control_type), POINTER                    :: dft_control
     214         198 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     215             :       TYPE(rt_prop_type), POINTER                        :: rtp
     216             :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     217             : 
     218         198 :       NULLIFY (dft_control, rtp, rtp_control)
     219             : 
     220         198 :       CALL cite_reference(Andermatt2016)
     221             : 
     222             :       CALL get_qs_env(qs_env, &
     223             :                       rtp=rtp, &
     224         198 :                       dft_control=dft_control)
     225         198 :       rtp_control => dft_control%rtp_control
     226             : 
     227         198 :       IF (rtp_control%initial_wfn == use_scf_wfn) THEN
     228         162 :          IF (rtp_control%apply_delta_pulse .OR. rtp_control%apply_delta_pulse_mag) THEN
     229          54 :             CALL apply_delta_pulse(qs_env, rtp, rtp_control)
     230             :          ELSE
     231         108 :             IF (.NOT. rtp%linear_scaling) THEN
     232          74 :                CALL get_rtp(rtp=rtp, mos_old=mos_old)
     233          74 :                CALL get_qs_env(qs_env, mos=mos)
     234         162 :                DO i = 1, SIZE(mos)
     235          88 :                   CALL cp_fm_to_fm(mos(i)%mo_coeff, mos_old(2*i - 1))
     236         162 :                   CALL cp_fm_set_all(mos_old(2*i), zero, zero)
     237             :                END DO
     238             :             END IF
     239             :          END IF
     240             :       END IF
     241             : 
     242         198 :       IF (.NOT. rtp%linear_scaling) THEN
     243         108 :          CALL get_rtp(rtp=rtp, mos_old=mos_old, mos_new=mos_new)
     244         384 :          DO i = 1, SIZE(mos_old)
     245         384 :             CALL cp_fm_to_fm(mos_old(i), mos_new(i))
     246             :          END DO
     247         108 :          CALL calc_update_rho(qs_env)
     248             :       ELSE
     249          90 :          IF (rtp_control%initial_wfn == use_scf_wfn) THEN
     250             :             CALL get_qs_env(qs_env, &
     251             :                             matrix_ks=matrix_ks, &
     252             :                             mos=mos, &
     253          74 :                             nelectron_spin=nelectron_spin)
     254          74 :             IF (ASSOCIATED(mos)) THEN
     255             :                !The wavefunction was minimized by an mo based algorith. P is therefore calculated from the mos
     256          64 :                IF (ASSOCIATED(rtp%mos)) THEN
     257          40 :                   IF (ASSOCIATED(rtp%mos%old)) THEN
     258             :                      ! Delta kick was applied and the results is in rtp%mos%old
     259          40 :                      CALL rt_initialize_rho_from_mos(rtp, mos, mos_old=rtp%mos%old)
     260             :                   ELSE
     261           0 :                      CALL rt_initialize_rho_from_mos(rtp, mos)
     262             :                   END IF
     263             :                ELSE
     264          24 :                   CALL rt_initialize_rho_from_mos(rtp, mos)
     265             :                END IF
     266             :             ELSE
     267             :                !The wavefunction was minimized using a linear scaling method. The density matrix is therefore taken from the ls_scf_env.
     268          10 :                CALL get_rtp(rtp=rtp, rho_old=rho_old, rho_new=rho_new)
     269          24 :                DO ispin = 1, SIZE(rho_old)/2
     270          14 :                   re = 2*ispin - 1
     271          14 :                   CALL dbcsr_copy(rho_old(re)%matrix, qs_env%ls_scf_env%matrix_p(ispin))
     272          24 :                   CALL dbcsr_copy(rho_new(re)%matrix, qs_env%ls_scf_env%matrix_p(ispin))
     273             :                END DO
     274             :             END IF
     275          74 :             CALL calc_update_rho_sparse(qs_env)
     276             :          END IF
     277             :       END IF
     278             :       ! Modify KS matrix to include the additional terms in the velocity gauge
     279         198 :       IF (rtp_control%velocity_gauge) THEN
     280             :          ! As matrix_h and matrix_h_im are not updated by qs_ks_update_qs_env()
     281             :          ! the non-gauge transformed non-local part has to be subtracted here
     282           8 :          CALL velocity_gauge_ks_matrix(qs_env, subtract_nl_term=.TRUE.)
     283             :       END IF
     284         198 :       CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE.)
     285             : 
     286         198 :    END SUBROUTINE init_propagation_run
     287             : 
     288             : ! **************************************************************************************************
     289             : !> \brief performs the real RTP run, gets information from MD section
     290             : !>        uses MD as iteration level
     291             : !> \param qs_env ...
     292             : !> \param force_env ...
     293             : !> \param globenv ...
     294             : !> \author Florian Schiffmann (02.09)
     295             : ! **************************************************************************************************
     296             : 
     297         126 :    SUBROUTINE run_propagation(qs_env, force_env, globenv)
     298             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     299             :       TYPE(force_env_type), POINTER                      :: force_env
     300             :       TYPE(global_environment_type), POINTER             :: globenv
     301             : 
     302             :       CHARACTER(len=*), PARAMETER                        :: routineN = 'run_propagation'
     303             : 
     304             :       INTEGER                                            :: aspc_order, handle, i_iter, i_step, &
     305             :                                                             max_iter, max_steps, output_unit
     306             :       LOGICAL                                            :: should_stop
     307             :       REAL(Kind=dp)                                      :: eps_ener, time_iter_start, &
     308             :                                                             time_iter_stop, used_time
     309             :       TYPE(cp_logger_type), POINTER                      :: logger
     310             :       TYPE(dft_control_type), POINTER                    :: dft_control
     311             :       TYPE(pw_env_type), POINTER                         :: pw_env
     312             :       TYPE(qs_energy_type), POINTER                      :: energy
     313             :       TYPE(rt_prop_type), POINTER                        :: rtp
     314             :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     315             :       TYPE(section_vals_type), POINTER                   :: input, rtp_section
     316             : 
     317         126 :       should_stop = .FALSE.
     318         126 :       CALL timeset(routineN, handle)
     319             : 
     320         126 :       CALL cite_reference(Andermatt2016)
     321             : 
     322         126 :       NULLIFY (logger, dft_control, energy, rtp, rtp_control, input, rtp_section)
     323         126 :       logger => cp_get_default_logger()
     324             : 
     325         126 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, rtp=rtp, energy=energy, input=input)
     326             : 
     327         126 :       rtp_control => dft_control%rtp_control
     328         126 :       max_steps = MIN(rtp%nsteps, rtp%max_steps)
     329         126 :       max_iter = rtp_control%max_iter
     330         126 :       eps_ener = rtp_control%eps_ener
     331             : 
     332         126 :       aspc_order = rtp_control%aspc_order
     333             : 
     334         126 :       rtp%energy_old = energy%total
     335         126 :       time_iter_start = m_walltime()
     336         126 :       CALL cp_add_iter_level(logger%iter_info, "MD")
     337         126 :       CALL cp_iterate(logger%iter_info, iter_nr=0)
     338         126 :       IF (rtp%i_start >= max_steps) CALL cp_abort(__LOCATION__, &
     339           0 :                                                   "maximum step number smaller than initial step value")
     340             : 
     341         126 :       rtp_section => section_vals_get_subs_vals(input, "DFT%REAL_TIME_PROPAGATION")
     342             :       output_unit = cp_print_key_unit_nr(logger, rtp_section, "PRINT%PROGRAM_RUN_INFO", &
     343         126 :                                          extension=".scfLog")
     344             : 
     345         470 :       DO i_step = rtp%i_start + 1, max_steps
     346         344 :          IF (output_unit > 0) THEN
     347             :             WRITE (output_unit, FMT="(/,(T2,A,T40,I6))") &
     348         172 :                "Real time propagation step:", i_step
     349             :          END IF
     350         344 :          energy%efield_core = 0.0_dp
     351         344 :          qs_env%sim_time = REAL(i_step, dp)*rtp%dt
     352         344 :          CALL get_qs_env(qs_env, pw_env=pw_env)
     353         344 :          pw_env%poisson_env%parameters%dbc_params%time = qs_env%sim_time
     354         344 :          qs_env%sim_step = i_step
     355         344 :          rtp%istep = i_step - rtp%i_start
     356         344 :          CALL calculate_ecore_efield(qs_env, .FALSE.)
     357         344 :          IF (dft_control%apply_external_potential) THEN
     358           0 :             IF (.NOT. dft_control%expot_control%static) THEN
     359           0 :                dft_control%eval_external_potential = .TRUE.
     360             :             END IF
     361             :          END IF
     362         344 :          CALL external_c_potential(qs_env, calculate_forces=.FALSE.)
     363         344 :          CALL external_e_potential(qs_env)
     364         344 :          CALL cp_iterate(logger%iter_info, last=(i_step == max_steps), iter_nr=i_step)
     365         344 :          rtp%converged = .FALSE.
     366        1144 :          DO i_iter = 1, max_iter
     367        1144 :             IF (i_step == rtp%i_start + 1 .AND. i_iter == 2 .AND. rtp_control%hfx_redistribute) &
     368           0 :                CALL qs_ks_did_change(qs_env%ks_env, s_mstruct_changed=.TRUE.)
     369        1144 :             rtp%iter = i_iter
     370        1144 :             CALL propagation_step(qs_env, rtp, rtp_control)
     371        1144 :             CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE.)
     372        1144 :             rtp%energy_new = energy%total
     373        1144 :             IF (rtp%converged) EXIT
     374        1144 :             CALL rt_prop_output(qs_env, real_time_propagation, rtp%delta_iter)
     375             :          END DO
     376         470 :          IF (rtp%converged) THEN
     377         344 :             CALL external_control(should_stop, "MD", globenv=globenv)
     378         344 :             IF (should_stop) CALL cp_iterate(logger%iter_info, last=.TRUE., iter_nr=i_step)
     379         344 :             time_iter_stop = m_walltime()
     380         344 :             used_time = time_iter_stop - time_iter_start
     381         344 :             time_iter_start = time_iter_stop
     382         344 :             CALL rt_prop_output(qs_env, real_time_propagation, delta_iter=rtp%delta_iter, used_time=used_time)
     383         344 :             CALL rt_write_input_restart(force_env=force_env, qs_env=qs_env)
     384         344 :             IF (should_stop) EXIT
     385             :          ELSE
     386             :             EXIT
     387             :          END IF
     388             :       END DO
     389         126 :       CALL cp_rm_iter_level(logger%iter_info, "MD")
     390             : 
     391         126 :       IF (.NOT. rtp%converged) &
     392             :          CALL cp_abort(__LOCATION__, "propagation did not converge, "// &
     393           0 :                        "either increase MAX_ITER or use a smaller TIMESTEP")
     394             : 
     395         126 :       CALL timestop(handle)
     396             : 
     397         126 :    END SUBROUTINE run_propagation
     398             : 
     399             : ! **************************************************************************************************
     400             : !> \brief overwrites some values in the input file such that the .restart
     401             : !>        file will contain the appropriate information
     402             : !> \param md_env ...
     403             : !> \param qs_env ...
     404             : !> \param force_env ...
     405             : !> \author Florian Schiffmann (02.09)
     406             : ! **************************************************************************************************
     407             : 
     408         344 :    SUBROUTINE rt_write_input_restart(md_env, qs_env, force_env)
     409             :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
     410             :       TYPE(qs_environment_type), OPTIONAL, POINTER       :: qs_env
     411             :       TYPE(force_env_type), POINTER                      :: force_env
     412             : 
     413             :       CHARACTER(len=default_path_length)                 :: file_name
     414         344 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tmp_vals
     415             :       TYPE(cp_logger_type), POINTER                      :: logger
     416             :       TYPE(dft_control_type), POINTER                    :: dft_control
     417             :       TYPE(section_vals_type), POINTER                   :: dft_section, efield_section, &
     418             :                                                             motion_section, print_key, &
     419             :                                                             root_section, rt_section
     420             : 
     421         344 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
     422         344 :       root_section => force_env%root_section
     423         344 :       motion_section => section_vals_get_subs_vals(root_section, "MOTION")
     424         344 :       dft_section => section_vals_get_subs_vals(root_section, "FORCE_EVAL%DFT")
     425         344 :       rt_section => section_vals_get_subs_vals(root_section, "FORCE_EVAL%DFT%REAL_TIME_PROPAGATION")
     426             : 
     427         344 :       CALL section_vals_val_set(rt_section, "INITIAL_WFN", i_val=use_rt_restart)
     428         344 :       CALL section_vals_val_set(rt_section, "APPLY_DELTA_PULSE", l_val=.FALSE.)
     429         344 :       CALL section_vals_val_set(rt_section, "APPLY_DELTA_PULSE_MAG", l_val=.FALSE.)
     430         344 :       CALL section_vals_val_set(rt_section, "APPLY_WFN_MIX_INIT_RESTART", l_val=.FALSE.)
     431             : 
     432         344 :       logger => cp_get_default_logger()
     433             : 
     434             :       ! to continue propagating the TD wavefunction we need to read from the new .rtpwfn
     435         344 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     436             :                                            rt_section, "PRINT%RESTART"), cp_p_file)) THEN
     437         126 :          print_key => section_vals_get_subs_vals(rt_section, "PRINT%RESTART")
     438             :          file_name = cp_print_key_generate_filename(logger, print_key, &
     439         126 :                                                     extension=".rtpwfn", my_local=.FALSE.)
     440         126 :          CALL section_vals_val_set(dft_section, "WFN_RESTART_FILE_NAME", c_val=TRIM(file_name))
     441             :       END IF
     442             : 
     443             :       ! coming from RTP
     444         344 :       IF (.NOT. PRESENT(md_env)) THEN
     445         344 :          CALL section_vals_val_set(motion_section, "MD%STEP_START_VAL", i_val=force_env%qs_env%sim_step)
     446             :       END IF
     447             : 
     448         344 :       IF (dft_control%apply_vector_potential) THEN
     449          22 :          efield_section => section_vals_get_subs_vals(root_section, "FORCE_EVAL%DFT%EFIELD")
     450             :          NULLIFY (tmp_vals)
     451          22 :          ALLOCATE (tmp_vals(3))
     452          88 :          tmp_vals = dft_control%efield_fields(1)%efield%vec_pot_initial
     453             :          CALL section_vals_val_set(efield_section, "VEC_POT_INITIAL", &
     454             :                                    r_vals_ptr=tmp_vals, &
     455          22 :                                    i_rep_section=1)
     456             :       END IF
     457             : 
     458         344 :       CALL write_restart(md_env=md_env, root_section=root_section)
     459             : 
     460         344 :    END SUBROUTINE rt_write_input_restart
     461             : 
     462             : ! **************************************************************************************************
     463             : !> \brief Creates the initial electronic states and allocates the necessary
     464             : !>        matrices
     465             : !> \param qs_env ...
     466             : !> \param force_env ...
     467             : !> \param rtp_control ...
     468             : !> \author Florian Schiffmann (02.09)
     469             : ! **************************************************************************************************
     470             : 
     471         198 :    SUBROUTINE rt_initial_guess(qs_env, force_env, rtp_control)
     472             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     473             :       TYPE(force_env_type), POINTER                      :: force_env
     474             :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     475             : 
     476             :       INTEGER                                            :: homo, ispin
     477             :       LOGICAL                                            :: energy_consistency
     478             :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     479         198 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     480             :       TYPE(dft_control_type), POINTER                    :: dft_control
     481             : 
     482         198 :       NULLIFY (matrix_s, dft_control)
     483         198 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     484             : 
     485         360 :       SELECT CASE (rtp_control%initial_wfn)
     486             :       CASE (use_scf_wfn)
     487         162 :          qs_env%sim_time = 0.0_dp
     488         162 :          qs_env%sim_step = 0
     489         162 :          energy_consistency = .TRUE.
     490             :          !in the linear scaling case we need a correct kohn-sham matrix, which we cannot get with consistent energies
     491         162 :          IF (rtp_control%linear_scaling) energy_consistency = .FALSE.
     492             :          CALL force_env_calc_energy_force(force_env, calc_force=.FALSE., &
     493         162 :                                           consistent_energies=energy_consistency)
     494         162 :          qs_env%run_rtp = .TRUE.
     495         162 :          ALLOCATE (qs_env%rtp)
     496         162 :          CALL get_qs_env(qs_env, matrix_s=matrix_s)
     497         162 :          IF (dft_control%do_admm) THEN
     498           6 :             CALL hfx_admm_init(qs_env)
     499             :             CALL rt_prop_create(qs_env%rtp, qs_env%mos, qs_env%mpools, dft_control, matrix_s(1)%matrix, &
     500           6 :                                 rtp_control%linear_scaling, qs_env%admm_env%mos_aux_fit)
     501             :          ELSE
     502             :             CALL rt_prop_create(qs_env%rtp, qs_env%mos, qs_env%mpools, dft_control, matrix_s(1)%matrix, &
     503         156 :                                 rtp_control%linear_scaling)
     504             :          END IF
     505             : 
     506             :       CASE (use_restart_wfn, use_rt_restart)
     507          36 :          CALL qs_energies_init(qs_env, .FALSE.)
     508          36 :          IF (.NOT. rtp_control%linear_scaling .OR. rtp_control%initial_wfn == use_restart_wfn) THEN
     509          86 :             DO ispin = 1, SIZE(qs_env%mos)
     510          52 :                CALL get_mo_set(qs_env%mos(ispin), mo_coeff=mo_coeff, homo=homo)
     511          86 :                IF (.NOT. ASSOCIATED(mo_coeff)) THEN
     512             :                   CALL init_mo_set(qs_env%mos(ispin), &
     513             :                                    qs_env%mpools%ao_mo_fm_pools(ispin)%pool, &
     514          52 :                                    name="qs_env%mo"//TRIM(ADJUSTL(cp_to_string(ispin))))
     515             :                END IF
     516             :             END DO
     517          34 :             IF (dft_control%do_admm) CALL hfx_admm_init(qs_env)
     518             :          END IF
     519          36 :          ALLOCATE (qs_env%rtp)
     520          36 :          CALL get_qs_env(qs_env, matrix_s=matrix_s)
     521             :          CALL rt_prop_create(qs_env%rtp, qs_env%mos, qs_env%mpools, dft_control, matrix_s(1)%matrix, &
     522          36 :                              rtp_control%linear_scaling, qs_env%admm_env%mos_aux_fit)
     523          36 :          CALL get_restart_wfn(qs_env)
     524             : 
     525         234 :          qs_env%run_rtp = .TRUE.
     526             :       END SELECT
     527             : 
     528         198 :    END SUBROUTINE rt_initial_guess
     529             : 
     530             : ! **************************************************************************************************
     531             : !> \brief ...
     532             : !> \param qs_env ...
     533             : !> \param imag_p ...
     534             : !> \param imag_ks ...
     535             : !> \param imag_h ...
     536             : ! **************************************************************************************************
     537         198 :    SUBROUTINE rt_init_complex_quantities(qs_env, imag_p, imag_ks, imag_h)
     538             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     539             :       LOGICAL, INTENT(in)                                :: imag_p, imag_ks, imag_h
     540             : 
     541             :       TYPE(dft_control_type), POINTER                    :: dft_control
     542             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     543             :       TYPE(qs_rho_type), POINTER                         :: rho
     544             :       TYPE(rt_prop_type), POINTER                        :: rtp
     545             : 
     546         198 :       NULLIFY (ks_env, rho, dft_control)
     547             : 
     548             :       CALL get_qs_env(qs_env, &
     549             :                       dft_control=dft_control, &
     550             :                       ks_env=ks_env, &
     551             :                       rho=rho, &
     552         198 :                       rtp=rtp)
     553             : 
     554             :       ! rho
     555         198 :       CALL qs_rho_set(rho, complex_rho_ao=imag_p)
     556         198 :       IF (imag_p) CALL allocate_rho_ao_imag_from_real(rho, qs_env)
     557             : 
     558             :       ! ks
     559         198 :       CALL set_ks_env(ks_env, complex_ks=imag_ks)
     560         198 :       IF (imag_ks) THEN
     561          40 :          CALL qs_ks_allocate_basics(qs_env, is_complex=imag_ks)
     562          40 :          IF (.NOT. dft_control%rtp_control%fixed_ions) &
     563          22 :             CALL rtp_create_SinvH_imag(rtp, dft_control%nspins)
     564             :       END IF
     565             : 
     566             :       ! h
     567         198 :       IF (imag_h) CALL qs_matrix_h_allocate_imag_from_real(qs_env)
     568             : 
     569         198 :    END SUBROUTINE rt_init_complex_quantities
     570             : 
     571             : END MODULE rt_propagation

Generated by: LCOV version 1.15