LCOV - code coverage report
Current view: top level - src/motion - rt_propagation.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:744416f) Lines: 97.9 % 286 280
Test Date: 2026-09-20 02:09:09 Functions: 100.0 % 8 8

            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 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 cell_types,                      ONLY: cell_type
      17              :    USE cp_control_types,                ONLY: dft_control_type,&
      18              :                                               rtp_control_type
      19              :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      20              :                                               dbcsr_create,&
      21              :                                               dbcsr_p_type,&
      22              :                                               dbcsr_release,&
      23              :                                               dbcsr_set
      24              :    USE cp_external_control,             ONLY: external_control
      25              :    USE cp_fm_types,                     ONLY: cp_fm_set_all,&
      26              :                                               cp_fm_to_fm,&
      27              :                                               cp_fm_type
      28              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      29              :                                               cp_logger_get_default_io_unit,&
      30              :                                               cp_logger_get_default_unit_nr,&
      31              :                                               cp_logger_type,&
      32              :                                               cp_to_string
      33              :    USE cp_output_handling,              ONLY: cp_add_iter_level,&
      34              :                                               cp_iterate,&
      35              :                                               cp_p_file,&
      36              :                                               cp_print_key_generate_filename,&
      37              :                                               cp_print_key_should_output,&
      38              :                                               cp_print_key_unit_nr,&
      39              :                                               cp_rm_iter_level
      40              :    USE efield_utils,                    ONLY: calculate_ecore_efield
      41              :    USE force_env_methods,               ONLY: force_env_calc_energy_force
      42              :    USE force_env_types,                 ONLY: force_env_get,&
      43              :                                               force_env_type
      44              :    USE global_types,                    ONLY: global_environment_type
      45              :    USE hfx_admm_utils,                  ONLY: hfx_admm_init
      46              :    USE input_constants,                 ONLY: real_time_propagation,&
      47              :                                               use_restart_wfn,&
      48              :                                               use_rt_restart,&
      49              :                                               use_scf_wfn
      50              :    USE input_cp2k_restarts,             ONLY: write_restart
      51              :    USE input_section_types,             ONLY: section_vals_get,&
      52              :                                               section_vals_get_subs_vals,&
      53              :                                               section_vals_type,&
      54              :                                               section_vals_val_get,&
      55              :                                               section_vals_val_set
      56              :    USE kinds,                           ONLY: default_path_length,&
      57              :                                               dp
      58              :    USE machine,                         ONLY: m_walltime
      59              :    USE md_environment_types,            ONLY: md_environment_type
      60              :    USE moments_utils,                   ONLY: get_reference_point
      61              :    USE pw_env_types,                    ONLY: pw_env_type
      62              :    USE qs_core_hamiltonian,             ONLY: qs_matrix_h_allocate_imag_from_real
      63              :    USE qs_energy_init,                  ONLY: qs_energies_init
      64              :    USE qs_energy_types,                 ONLY: qs_energy_type
      65              :    USE qs_environment_types,            ONLY: get_qs_env,&
      66              :                                               qs_environment_type
      67              :    USE qs_external_potential,           ONLY: external_c_potential,&
      68              :                                               external_e_potential
      69              :    USE qs_ks_methods,                   ONLY: qs_ks_allocate_basics,&
      70              :                                               qs_ks_update_qs_env
      71              :    USE qs_ks_types,                     ONLY: qs_ks_did_change,&
      72              :                                               qs_ks_env_type,&
      73              :                                               set_ks_env
      74              :    USE qs_mo_io,                        ONLY: wfn_restart_file_name
      75              :    USE qs_mo_types,                     ONLY: get_mo_set,&
      76              :                                               init_mo_set,&
      77              :                                               mo_set_type
      78              :    USE qs_moments,                      ONLY: build_local_moment_matrix
      79              :    USE qs_rho_methods,                  ONLY: allocate_rho_ao_imag_from_real
      80              :    USE qs_rho_types,                    ONLY: qs_rho_set,&
      81              :                                               qs_rho_type
      82              :    USE rt_delta_pulse,                  ONLY: apply_delta_pulse
      83              :    USE rt_hfx_utils,                    ONLY: rtp_hfx_rebuild
      84              :    USE rt_projection_mo_utils,          ONLY: init_mo_projection
      85              :    USE rt_propagation_methods,          ONLY: propagation_step,&
      86              :                                               rtp_localize
      87              :    USE rt_propagation_output,           ONLY: calc_local_moment,&
      88              :                                               print_ft,&
      89              :                                               print_moments,&
      90              :                                               rt_prop_output
      91              :    USE rt_propagation_types,            ONLY: get_rtp,&
      92              :                                               rt_prop_create,&
      93              :                                               rt_prop_type,&
      94              :                                               rtp_create_SinvH_imag,&
      95              :                                               rtp_history_create
      96              :    USE rt_propagation_utils,            ONLY: calc_S_derivs,&
      97              :                                               calc_update_rho,&
      98              :                                               calc_update_rho_sparse,&
      99              :                                               get_restart_wfn,&
     100              :                                               read_moments,&
     101              :                                               recalculate_fields,&
     102              :                                               warn_section_unused
     103              :    USE rt_propagation_velocity_gauge,   ONLY: velocity_gauge_ks_matrix
     104              :    USE rt_propagator_init,              ONLY: init_propagators,&
     105              :                                               rt_initialize_rho_from_mos
     106              : #include "../base/base_uses.f90"
     107              : 
     108              :    IMPLICIT NONE
     109              : 
     110              :    PRIVATE
     111              : 
     112              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'rt_propagation'
     113              : 
     114              :    PUBLIC :: rt_prop_setup
     115              : 
     116              : CONTAINS
     117              : 
     118              : ! **************************************************************************************************
     119              : !> \brief creates rtp_type, gets the initial state, either by reading MO's
     120              : !>        from file or calling SCF run
     121              : !> \param force_env ...
     122              : !> \author Florian Schiffmann (02.09)
     123              : ! **************************************************************************************************
     124              : 
     125         1070 :    SUBROUTINE rt_prop_setup(force_env)
     126              :       TYPE(force_env_type), POINTER                      :: force_env
     127              : 
     128              :       INTEGER                                            :: aspc_order
     129              :       LOGICAL                                            :: magnetic, track_current, &
     130              :                                                             track_current_int, vel_reprs
     131              :       TYPE(dft_control_type), POINTER                    :: dft_control
     132              :       TYPE(global_environment_type), POINTER             :: globenv
     133              :       TYPE(qs_energy_type), POINTER                      :: energy
     134              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     135              :       TYPE(rt_prop_type), POINTER                        :: rtp
     136              :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     137              :       TYPE(section_vals_type), POINTER :: current_int_section, current_section, hfx_sections, &
     138              :          input, ls_scf_section, md_section, motion_section, print_moments_section, &
     139              :          rtp_print_section, rtp_section
     140              : 
     141          214 :       NULLIFY (qs_env, rtp_control, dft_control)
     142              : 
     143          214 :       CALL cite_reference(Andermatt2016)
     144              : 
     145          214 :       CALL force_env_get(force_env=force_env, qs_env=qs_env, globenv=globenv)
     146          214 :       CALL get_qs_env(qs_env, dft_control=dft_control, energy=energy)
     147          214 :       rtp_control => dft_control%rtp_control
     148              : 
     149              :       ! Takes care that an initial wavefunction/density is available
     150              :       ! Can either be by performing an scf loop or reading a restart
     151          214 :       CALL rt_initial_guess(qs_env, force_env, rtp_control)
     152              : 
     153              :       ! Initializes the extrapolation
     154          214 :       NULLIFY (rtp)
     155          214 :       CALL get_qs_env(qs_env=qs_env, rtp=rtp, input=input)
     156          214 :       aspc_order = rtp_control%aspc_order
     157          214 :       CALL rtp_history_create(rtp, aspc_order)
     158              : 
     159              :       ! Reads the simulation parameters from the input
     160          214 :       motion_section => section_vals_get_subs_vals(force_env%root_section, "MOTION")
     161          214 :       md_section => section_vals_get_subs_vals(motion_section, "MD")
     162          214 :       hfx_sections => section_vals_get_subs_vals(force_env%root_section, "FORCE_EVAL%DFT%XC%HF")
     163          214 :       rtp_section => section_vals_get_subs_vals(force_env%root_section, "FORCE_EVAL%DFT%REAL_TIME_PROPAGATION")
     164          214 :       print_moments_section => section_vals_get_subs_vals(force_env%root_section, "FORCE_EVAL%DFT%PRINT%MOMENTS")
     165          214 :       CALL section_vals_val_get(md_section, "TIMESTEP", r_val=qs_env%rtp%dt)
     166          214 :       CALL section_vals_val_get(md_section, "STEP_START_VAL", i_val=qs_env%rtp%i_start)
     167          214 :       CALL section_vals_val_get(md_section, "STEPS", i_val=rtp%nsteps)
     168          214 :       CALL section_vals_val_get(md_section, "MAX_STEPS", i_val=rtp%max_steps)
     169              : 
     170          214 :       ls_scf_section => section_vals_get_subs_vals(input, "DFT%LS_SCF")
     171          214 :       CALL section_vals_val_get(ls_scf_section, "EPS_FILTER", r_val=rtp%filter_eps)
     172          214 :       IF (.NOT. qs_env%rtp%linear_scaling) rtp%filter_eps = 0.0_dp
     173          214 :       IF (rtp_control%acc_ref < 1) rtp_control%acc_ref = 1
     174          214 :       rtp%filter_eps_small = rtp%filter_eps/rtp_control%acc_ref
     175          214 :       CALL section_vals_val_get(ls_scf_section, "EPS_LANCZOS", r_val=rtp%lanzcos_threshold)
     176          214 :       CALL section_vals_val_get(ls_scf_section, "MAX_ITER_LANCZOS", i_val=rtp%lanzcos_max_iter)
     177          214 :       CALL section_vals_val_get(ls_scf_section, "SIGN_SQRT_ORDER", i_val=rtp%newton_schulz_order)
     178          214 :       CALL section_vals_get(hfx_sections, explicit=rtp%do_hfx)
     179          214 :       CALL section_vals_val_get(print_moments_section, "MAGNETIC", l_val=magnetic)
     180          214 :       CALL section_vals_val_get(print_moments_section, "VEL_REPRS", l_val=vel_reprs)
     181              : 
     182              :       ! The imaginary part of the density matrix (needed for the current) is generically
     183              :       ! nonzero whenever the propagated state is not stationary, independently of whether
     184              :       ! an explicit time-dependent field/vector potential is applied: e.g. an excited
     185              :       ! state prepared by occupation swap, from LR-TDDFT, or via a MOM-constrained SCF.
     186              :       ! Rather than trying to detect every possible way of preparing such a state, track
     187              :       ! the imaginary density whenever the user actually requests output that needs it.
     188          214 :       rtp_print_section => section_vals_get_subs_vals(rtp_section, "PRINT")
     189          214 :       current_section => section_vals_get_subs_vals(rtp_print_section, "CURRENT")
     190          214 :       current_int_section => section_vals_get_subs_vals(rtp_print_section, "CURRENT_INT")
     191          214 :       CALL section_vals_get(current_section, explicit=track_current)
     192          214 :       CALL section_vals_get(current_int_section, explicit=track_current_int)
     193              : 
     194              :       rtp%track_imag_density = (magnetic) .OR. (vel_reprs) .OR. (rtp_control%velocity_gauge) &
     195              :                                .OR. (rtp%do_hfx) .OR. (.NOT. rtp_control%fixed_ions) &
     196          214 :                                .OR. (track_current) .OR. (track_current_int)
     197          214 :       rtp%propagate_complex_ks = rtp%do_hfx .OR. rtp_control%velocity_gauge
     198              : 
     199              :       ! Marek : In case some print sections that apply so far only to RTBSE are present,
     200              :       !         warn the user that the quantities will not be in fact printed out
     201              :       CALL warn_section_unused(rtp_print_section, "DENSITY_MATRIX", &
     202          214 :                                "DENSITY_MATRIX printing not implemented for non-RTBSE code.")
     203              : 
     204              :       CALL rt_init_complex_quantities(qs_env, imag_p=rtp%track_imag_density, &
     205          214 :                                       imag_ks=rtp%propagate_complex_ks, imag_h=rtp_control%velocity_gauge)
     206              : 
     207          214 :       IF (rtp_control%save_local_moments) CALL rt_init_local_moments(rtp, qs_env)
     208              : 
     209              :       ! Hmm, not really like to initialize with the structure of S but I reckon it is
     210              :       ! done everywhere like this
     211          214 :       IF (rtp%do_hfx) CALL rtp_hfx_rebuild(qs_env)
     212              : 
     213              :       ! Setup the MO projection environment if required
     214          214 :       IF (rtp_control%is_proj_mo) CALL init_mo_projection(qs_env, rtp_control)
     215              : 
     216          214 :       CALL init_propagation_run(qs_env)
     217          214 :       IF (.NOT. rtp_control%fixed_ions) THEN
     218              :          !derivativs of the overlap needed for EMD
     219           76 :          CALL calc_S_derivs(qs_env)
     220              :          ! a bit hidden, but computes SinvH and SinvB (calc_SinvH for CN,EM and ARNOLDI)
     221              :          ! make_etrs_exp in case of ETRS in combination with TAYLOR and PADE
     222              :       END IF
     223          214 :       CALL init_propagators(qs_env)
     224          214 :       IF (rtp_control%fixed_ions) THEN
     225          138 :          CALL run_propagation(qs_env, force_env, globenv)
     226              :       ELSE
     227           76 :          rtp_control%initial_step = .TRUE.
     228           76 :          CALL force_env_calc_energy_force(force_env, calc_force=.TRUE.)
     229           76 :          rtp_control%initial_step = .FALSE.
     230           76 :          rtp%energy_old = energy%total
     231              :       END IF
     232              : 
     233          214 :       IF (rtp_control%save_local_moments) THEN
     234              :          ! Call routines for outputs and deallocations of FT observables
     235           18 :          CALL final_ft_output(qs_env)
     236              :       END IF
     237              : 
     238          214 :       IF (ASSOCIATED(rtp_control%print_pol_elements)) DEALLOCATE (rtp_control%print_pol_elements)
     239              : 
     240          214 :    END SUBROUTINE rt_prop_setup
     241              : 
     242              : ! **************************************************************************************************
     243              : !> \brief calculates the matrices needed in the first step of EMD/RTP
     244              : !> \param qs_env ...
     245              : !> \author Florian Schiffmann (02.09)
     246              : ! **************************************************************************************************
     247              : 
     248          214 :    SUBROUTINE init_propagation_run(qs_env)
     249              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     250              : 
     251              :       REAL(KIND=dp), PARAMETER                           :: zero = 0.0_dp
     252              : 
     253              :       INTEGER                                            :: i, ispin, re
     254              :       INTEGER, DIMENSION(2)                              :: nelectron_spin
     255          214 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: mos_new, mos_old
     256          214 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_ks, rho_new, rho_old
     257              :       TYPE(dft_control_type), POINTER                    :: dft_control
     258          214 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
     259              :       TYPE(rt_prop_type), POINTER                        :: rtp
     260              :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     261              : 
     262          214 :       NULLIFY (dft_control, rtp, rtp_control)
     263              : 
     264          214 :       CALL cite_reference(Andermatt2016)
     265              : 
     266              :       CALL get_qs_env(qs_env, &
     267              :                       rtp=rtp, &
     268          214 :                       dft_control=dft_control)
     269          214 :       rtp_control => dft_control%rtp_control
     270              : 
     271          214 :       IF (rtp_control%initial_wfn == use_scf_wfn) THEN
     272          178 :          IF (rtp_control%apply_delta_pulse .OR. rtp_control%apply_delta_pulse_mag) THEN
     273           60 :             CALL apply_delta_pulse(qs_env, rtp, rtp_control)
     274              :          ELSE
     275          118 :             IF (.NOT. rtp%linear_scaling) THEN
     276           82 :                CALL get_rtp(rtp=rtp, mos_old=mos_old)
     277           82 :                CALL get_qs_env(qs_env, mos=mos)
     278          182 :                DO i = 1, SIZE(mos)
     279          100 :                   CALL cp_fm_to_fm(mos(i)%mo_coeff, mos_old(2*i - 1))
     280          182 :                   CALL cp_fm_set_all(mos_old(2*i), zero, zero)
     281              :                END DO
     282              :             END IF
     283              :          END IF
     284              :       END IF
     285              : 
     286          214 :       IF (.NOT. rtp%linear_scaling) THEN
     287          122 :          CALL get_rtp(rtp=rtp, mos_old=mos_old, mos_new=mos_new)
     288          434 :          DO i = 1, SIZE(mos_old)
     289          434 :             CALL cp_fm_to_fm(mos_old(i), mos_new(i))
     290              :          END DO
     291          122 :          CALL calc_update_rho(qs_env)
     292              :       ELSE
     293           92 :          IF (rtp_control%initial_wfn == use_scf_wfn) THEN
     294              :             CALL get_qs_env(qs_env, &
     295              :                             matrix_ks=matrix_ks, &
     296              :                             mos=mos, &
     297           76 :                             nelectron_spin=nelectron_spin)
     298           76 :             IF (ASSOCIATED(mos)) THEN
     299              :                !The wavefunction was minimized by an mo based algorith. P is therefore calculated from the mos
     300           66 :                IF (ASSOCIATED(rtp%mos)) THEN
     301           40 :                   IF (ASSOCIATED(rtp%mos%old)) THEN
     302              :                      ! Delta kick was applied and the results is in rtp%mos%old
     303           40 :                      CALL rt_initialize_rho_from_mos(rtp, mos, mos_old=rtp%mos%old)
     304              :                   ELSE
     305            0 :                      CALL rt_initialize_rho_from_mos(rtp, mos)
     306              :                   END IF
     307              :                ELSE
     308           26 :                   CALL rt_initialize_rho_from_mos(rtp, mos)
     309              :                END IF
     310              :             ELSE
     311              :                ! The wavefunction was minimized using a linear scaling method.
     312              :                ! The density matrix is therefore taken from the ls_scf_env.
     313           10 :                CALL get_rtp(rtp=rtp, rho_old=rho_old, rho_new=rho_new)
     314           24 :                DO ispin = 1, SIZE(rho_old)/2
     315           14 :                   re = 2*ispin - 1
     316           14 :                   CALL dbcsr_copy(rho_old(re)%matrix, qs_env%ls_scf_env%matrix_p(ispin))
     317           24 :                   CALL dbcsr_copy(rho_new(re)%matrix, qs_env%ls_scf_env%matrix_p(ispin))
     318              :                END DO
     319              :             END IF
     320           76 :             CALL calc_update_rho_sparse(qs_env)
     321              :          END IF
     322              :       END IF
     323              :       ! Modify KS matrix to include the additional terms in the velocity gauge
     324          214 :       IF (rtp_control%velocity_gauge) THEN
     325              :          ! As matrix_h and matrix_h_im are not updated by qs_ks_update_qs_env()
     326              :          ! the non-gauge transformed non-local part has to be subtracted here
     327           12 :          CALL velocity_gauge_ks_matrix(qs_env, subtract_nl_term=.TRUE.)
     328              :       END IF
     329          214 :       CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE.)
     330              : 
     331          214 :    END SUBROUTINE init_propagation_run
     332              : 
     333              : ! **************************************************************************************************
     334              : !> \brief performs the real RTP run, gets information from MD section
     335              : !>        uses MD as iteration level
     336              : !> \param qs_env ...
     337              : !> \param force_env ...
     338              : !> \param globenv ...
     339              : !> \author Florian Schiffmann (02.09)
     340              : ! **************************************************************************************************
     341              : 
     342          138 :    SUBROUTINE run_propagation(qs_env, force_env, globenv)
     343              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     344              :       TYPE(force_env_type), POINTER                      :: force_env
     345              :       TYPE(global_environment_type), POINTER             :: globenv
     346              : 
     347              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'run_propagation'
     348              : 
     349              :       INTEGER                                            :: aspc_order, handle, i_iter, i_step, &
     350              :                                                             max_iter, max_steps, output_unit, &
     351              :                                                             unit_nr
     352              :       LOGICAL                                            :: moments_read, should_stop
     353              :       REAL(Kind=dp)                                      :: eps_ener, time_iter_start, &
     354              :                                                             time_iter_stop, used_time
     355              :       TYPE(cp_logger_type), POINTER                      :: logger
     356          138 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_new
     357              :       TYPE(dft_control_type), POINTER                    :: dft_control
     358              :       TYPE(pw_env_type), POINTER                         :: pw_env
     359              :       TYPE(qs_energy_type), POINTER                      :: energy
     360              :       TYPE(rt_prop_type), POINTER                        :: rtp
     361              :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     362              :       TYPE(section_vals_type), POINTER                   :: input, moments_section, rtp_section
     363              : 
     364          138 :       should_stop = .FALSE.
     365          138 :       CALL timeset(routineN, handle)
     366              : 
     367          138 :       CALL cite_reference(Andermatt2016)
     368              : 
     369          138 :       NULLIFY (logger, dft_control, energy, rtp, rtp_control, input, rtp_section)
     370          138 :       logger => cp_get_default_logger()
     371          138 :       IF (logger%para_env%is_source()) THEN
     372           69 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     373              :       ELSE
     374              :          unit_nr = -1
     375              :       END IF
     376              : 
     377          138 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, rtp=rtp, energy=energy, input=input)
     378              : 
     379          138 :       rtp_control => dft_control%rtp_control
     380          138 :       max_steps = MIN(rtp%nsteps, rtp%max_steps)
     381          138 :       max_iter = rtp_control%max_iter
     382          138 :       eps_ener = rtp_control%eps_ener
     383              : 
     384          138 :       aspc_order = rtp_control%aspc_order
     385              : 
     386          138 :       rtp%energy_old = energy%total
     387          138 :       time_iter_start = m_walltime()
     388          138 :       CALL cp_add_iter_level(logger%iter_info, "MD")
     389          138 :       CALL cp_iterate(logger%iter_info, iter_nr=0)
     390          138 :       IF (rtp%i_start >= max_steps) CALL cp_abort(__LOCATION__, &
     391            0 :                                                   "maximum step number smaller than initial step value")
     392              : 
     393          138 :       rtp_section => section_vals_get_subs_vals(input, "DFT%REAL_TIME_PROPAGATION")
     394              :       output_unit = cp_print_key_unit_nr(logger, rtp_section, "PRINT%PROGRAM_RUN_INFO", &
     395          138 :                                          extension=".scfLog")
     396              :       ! Add the zero iteration moments to the moment trace
     397          138 :       IF (rtp_control%save_local_moments) THEN
     398           18 :          CALL get_rtp(rtp, rho_new=rho_new)
     399           18 :          moments_section => section_vals_get_subs_vals(rtp_section, "PRINT%MOMENTS")
     400              :          ! TODO : Conditions on when not to read the files
     401           18 :          CALL read_moments(moments_section, 0, rtp%i_start, rtp%moments, rtp%times, moments_read)
     402              :          ! Recalculate the field at times in the trace/Read the field from the files
     403           18 :          CALL recalculate_fields(rtp%fields, rtp%times, 0, rtp%i_start, dft_control)
     404           18 :          IF (.NOT. moments_read) THEN
     405           18 :             CALL calc_local_moment(rtp%local_moments, rho_new, rtp%local_moments_work, rtp%moments(:, :, 1))
     406           18 :             qs_env%sim_time = REAL(rtp%i_start, dp)*rtp%dt
     407           18 :             rtp%times(1) = qs_env%sim_time
     408              :             CALL print_moments(moments_section, output_unit, rtp%moments(:, :, 1), &
     409           18 :                                qs_env%sim_time, rtp%track_imag_density)
     410              :          END IF
     411              :       END IF
     412              : 
     413          514 :       DO i_step = rtp%i_start + 1, max_steps
     414          376 :          IF (output_unit > 0) THEN
     415              :             WRITE (output_unit, FMT="(/,(T2,A,T40,I6))") &
     416          188 :                "Real time propagation step:", i_step
     417              :          END IF
     418          376 :          energy%efield_core = 0.0_dp
     419          376 :          qs_env%sim_time = REAL(i_step, dp)*rtp%dt
     420          376 :          CALL get_qs_env(qs_env, pw_env=pw_env)
     421          376 :          pw_env%poisson_env%parameters%dbc_params%time = qs_env%sim_time
     422          376 :          qs_env%sim_step = i_step
     423          376 :          rtp%istep = i_step - rtp%i_start
     424          376 :          CALL calculate_ecore_efield(qs_env, .FALSE.)
     425          376 :          IF (dft_control%apply_external_potential) THEN
     426            0 :             IF (.NOT. dft_control%expot_control%static) THEN
     427            0 :                dft_control%eval_external_potential = .TRUE.
     428              :             END IF
     429              :          END IF
     430          376 :          CALL external_c_potential(qs_env, calculate_forces=.FALSE.)
     431          376 :          CALL external_e_potential(qs_env)
     432          376 :          CALL cp_iterate(logger%iter_info, last=(i_step == max_steps), iter_nr=i_step)
     433          376 :          rtp%converged = .FALSE.
     434         1256 :          DO i_iter = 1, max_iter
     435         1256 :             IF (i_step == rtp%i_start + 1 .AND. i_iter == 2 .AND. rtp_control%hfx_redistribute) THEN
     436            0 :                CALL qs_ks_did_change(qs_env%ks_env, s_mstruct_changed=.TRUE.)
     437              :             END IF
     438         1256 :             rtp%iter = i_iter
     439         1256 :             CALL propagation_step(qs_env, rtp, rtp_control)
     440         1256 :             CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE.)
     441         1256 :             rtp%energy_new = energy%total
     442         1256 :             IF (rtp%converged) EXIT
     443         1256 :             CALL rt_prop_output(qs_env, real_time_propagation, rtp%delta_iter)
     444              :          END DO
     445          514 :          IF (rtp%converged) THEN
     446          376 :             CALL external_control(should_stop, "MD", globenv=globenv)
     447          376 :             IF (should_stop) CALL cp_iterate(logger%iter_info, last=.TRUE., iter_nr=i_step)
     448          376 :             time_iter_stop = m_walltime()
     449          376 :             used_time = time_iter_stop - time_iter_start
     450          376 :             time_iter_start = time_iter_stop
     451          376 :             CALL rt_prop_output(qs_env, real_time_propagation, delta_iter=rtp%delta_iter, used_time=used_time)
     452          376 :             CALL rt_write_input_restart(force_env=force_env, qs_env=qs_env)
     453          376 :             IF (MODULO(i_step, dft_control%localize_each) == 0) THEN
     454          376 :                CALL rtp_localize(qs_env, rtp)
     455              :             END IF
     456          376 :             IF (should_stop) EXIT
     457              :          ELSE
     458              :             EXIT
     459              :          END IF
     460              :       END DO
     461          138 :       CALL cp_rm_iter_level(logger%iter_info, "MD")
     462              : 
     463          138 :       IF (.NOT. rtp%converged) THEN
     464              :          CALL cp_abort(__LOCATION__, "propagation did not converge, "// &
     465            0 :                        "either increase MAX_ITER or use a smaller TIMESTEP")
     466              :       END IF
     467              : 
     468          138 :       CALL timestop(handle)
     469              : 
     470          138 :    END SUBROUTINE run_propagation
     471              : 
     472              : ! **************************************************************************************************
     473              : !> \brief overwrites some values in the input file such that the .restart
     474              : !>        file will contain the appropriate information
     475              : !> \param md_env ...
     476              : !> \param qs_env ...
     477              : !> \param force_env ...
     478              : !> \author Florian Schiffmann (02.09)
     479              : ! **************************************************************************************************
     480              : 
     481          376 :    SUBROUTINE rt_write_input_restart(md_env, qs_env, force_env)
     482              :       TYPE(md_environment_type), OPTIONAL, POINTER       :: md_env
     483              :       TYPE(qs_environment_type), OPTIONAL, POINTER       :: qs_env
     484              :       TYPE(force_env_type), POINTER                      :: force_env
     485              : 
     486              :       CHARACTER(len=default_path_length)                 :: file_name
     487          376 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: tmp_vals
     488              :       TYPE(cp_logger_type), POINTER                      :: logger
     489              :       TYPE(dft_control_type), POINTER                    :: dft_control
     490              :       TYPE(section_vals_type), POINTER                   :: dft_section, efield_section, &
     491              :                                                             motion_section, print_key, &
     492              :                                                             root_section, rt_section
     493              : 
     494          376 :       CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
     495          376 :       root_section => force_env%root_section
     496          376 :       motion_section => section_vals_get_subs_vals(root_section, "MOTION")
     497          376 :       dft_section => section_vals_get_subs_vals(root_section, "FORCE_EVAL%DFT")
     498          376 :       rt_section => section_vals_get_subs_vals(root_section, "FORCE_EVAL%DFT%REAL_TIME_PROPAGATION")
     499              : 
     500          376 :       CALL section_vals_val_set(rt_section, "INITIAL_WFN", i_val=use_rt_restart)
     501          376 :       CALL section_vals_val_set(rt_section, "APPLY_DELTA_PULSE", l_val=.FALSE.)
     502          376 :       CALL section_vals_val_set(rt_section, "APPLY_DELTA_PULSE_MAG", l_val=.FALSE.)
     503          376 :       CALL section_vals_val_set(rt_section, "APPLY_WFN_MIX_INIT_RESTART", l_val=.FALSE.)
     504              : 
     505          376 :       logger => cp_get_default_logger()
     506              : 
     507              :       ! to continue propagating the TD wavefunction we need to read from the new .rtpwfn
     508          376 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, &
     509              :                                            rt_section, "PRINT%RESTART"), cp_p_file)) THEN
     510          138 :          print_key => section_vals_get_subs_vals(rt_section, "PRINT%RESTART")
     511              :          file_name = cp_print_key_generate_filename(logger, print_key, &
     512          138 :                                                     extension=".rtpwfn", my_local=.FALSE.)
     513          138 :          CALL section_vals_val_set(dft_section, "WFN_RESTART_FILE_NAME", c_val=TRIM(file_name))
     514              :       END IF
     515              : 
     516              :       ! coming from RTP
     517          376 :       IF (.NOT. PRESENT(md_env)) THEN
     518          376 :          CALL section_vals_val_set(motion_section, "MD%STEP_START_VAL", i_val=force_env%qs_env%sim_step)
     519              :       END IF
     520              : 
     521          376 :       IF (dft_control%apply_vector_potential) THEN
     522           26 :          efield_section => section_vals_get_subs_vals(root_section, "FORCE_EVAL%DFT%EFIELD")
     523              :          NULLIFY (tmp_vals)
     524           26 :          ALLOCATE (tmp_vals(3))
     525          104 :          tmp_vals = dft_control%efield_fields(1)%efield%vec_pot_initial
     526              :          CALL section_vals_val_set(efield_section, "VEC_POT_INITIAL", &
     527              :                                    r_vals_ptr=tmp_vals, &
     528           26 :                                    i_rep_section=1)
     529              :       END IF
     530              : 
     531          376 :       CALL write_restart(md_env=md_env, root_section=root_section)
     532              : 
     533          376 :    END SUBROUTINE rt_write_input_restart
     534              : 
     535              : ! **************************************************************************************************
     536              : !> \brief Creates the initial electronic states and allocates the necessary
     537              : !>        matrices
     538              : !> \param qs_env ...
     539              : !> \param force_env ...
     540              : !> \param rtp_control ...
     541              : !> \author Florian Schiffmann (02.09)
     542              : ! **************************************************************************************************
     543              : 
     544          214 :    SUBROUTINE rt_initial_guess(qs_env, force_env, rtp_control)
     545              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     546              :       TYPE(force_env_type), POINTER                      :: force_env
     547              :       TYPE(rtp_control_type), POINTER                    :: rtp_control
     548              : 
     549              :       INTEGER                                            :: homo, ispin
     550              :       LOGICAL                                            :: energy_consistency
     551              :       TYPE(cp_fm_type), POINTER                          :: mo_coeff
     552          214 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     553              :       TYPE(dft_control_type), POINTER                    :: dft_control
     554              : 
     555          214 :       NULLIFY (matrix_s, dft_control)
     556          214 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     557          214 :       CPASSERT(ASSOCIATED(qs_env))
     558              : 
     559          392 :       SELECT CASE (rtp_control%initial_wfn)
     560              :       CASE (use_scf_wfn)
     561          178 :          qs_env%sim_time = 0.0_dp
     562          178 :          qs_env%sim_step = 0
     563          178 :          energy_consistency = .TRUE.
     564              :          !in the linear scaling case we need a correct kohn-sham matrix, which we cannot get with consistent energies
     565          178 :          IF (rtp_control%linear_scaling) energy_consistency = .FALSE.
     566              :          CALL force_env_calc_energy_force(force_env, calc_force=.FALSE., &
     567          178 :                                           consistent_energies=energy_consistency)
     568          178 :          qs_env%run_rtp = .TRUE.
     569          178 :          ALLOCATE (qs_env%rtp)
     570          178 :          CALL get_qs_env(qs_env, matrix_s=matrix_s)
     571          178 :          IF (dft_control%do_admm) THEN
     572           10 :             CALL hfx_admm_init(qs_env)
     573              :             CALL rt_prop_create(qs_env%rtp, qs_env%mos, qs_env%mpools, dft_control, matrix_s(1)%matrix, &
     574           10 :                                 rtp_control%linear_scaling, qs_env%admm_env%mos_aux_fit)
     575              :          ELSE
     576              :             CALL rt_prop_create(qs_env%rtp, qs_env%mos, qs_env%mpools, dft_control, matrix_s(1)%matrix, &
     577          168 :                                 rtp_control%linear_scaling)
     578              :          END IF
     579              : 
     580              :       CASE (use_restart_wfn, use_rt_restart)
     581           36 :          CALL qs_energies_init(qs_env, .FALSE.)
     582           36 :          IF (.NOT. rtp_control%linear_scaling .OR. rtp_control%initial_wfn == use_restart_wfn) THEN
     583           86 :             DO ispin = 1, SIZE(qs_env%mos)
     584           52 :                CALL get_mo_set(qs_env%mos(ispin), mo_coeff=mo_coeff, homo=homo)
     585           86 :                IF (.NOT. ASSOCIATED(mo_coeff)) THEN
     586              :                   CALL init_mo_set(qs_env%mos(ispin), &
     587              :                                    qs_env%mpools%ao_mo_fm_pools(ispin)%pool, &
     588           52 :                                    name="qs_env%mo"//TRIM(ADJUSTL(cp_to_string(ispin))))
     589              :                END IF
     590              :             END DO
     591           34 :             IF (dft_control%do_admm) CALL hfx_admm_init(qs_env)
     592              :          END IF
     593           36 :          ALLOCATE (qs_env%rtp)
     594           36 :          CALL get_qs_env(qs_env, matrix_s=matrix_s)
     595              :          CALL rt_prop_create(qs_env%rtp, qs_env%mos, qs_env%mpools, dft_control, matrix_s(1)%matrix, &
     596           36 :                              rtp_control%linear_scaling, qs_env%admm_env%mos_aux_fit)
     597           36 :          CALL get_restart_wfn(qs_env)
     598           36 :          CPASSERT(ASSOCIATED(qs_env))
     599              : 
     600          250 :          qs_env%run_rtp = .TRUE.
     601              :       END SELECT
     602              : 
     603          214 :    END SUBROUTINE rt_initial_guess
     604              : 
     605              : ! **************************************************************************************************
     606              : !> \brief ...
     607              : !> \param qs_env ...
     608              : !> \param imag_p ...
     609              : !> \param imag_ks ...
     610              : !> \param imag_h ...
     611              : ! **************************************************************************************************
     612          214 :    SUBROUTINE rt_init_complex_quantities(qs_env, imag_p, imag_ks, imag_h)
     613              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     614              :       LOGICAL, INTENT(in)                                :: imag_p, imag_ks, imag_h
     615              : 
     616              :       TYPE(dft_control_type), POINTER                    :: dft_control
     617              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     618              :       TYPE(qs_rho_type), POINTER                         :: rho
     619              :       TYPE(rt_prop_type), POINTER                        :: rtp
     620              : 
     621          214 :       NULLIFY (ks_env, rho, dft_control)
     622              : 
     623              :       CALL get_qs_env(qs_env, &
     624              :                       dft_control=dft_control, &
     625              :                       ks_env=ks_env, &
     626              :                       rho=rho, &
     627          214 :                       rtp=rtp)
     628              : 
     629              :       ! rho
     630          214 :       CALL qs_rho_set(rho, complex_rho_ao=imag_p)
     631          214 :       IF (imag_p) CALL allocate_rho_ao_imag_from_real(rho, qs_env)
     632              : 
     633              :       ! ks
     634          214 :       CALL set_ks_env(ks_env, complex_ks=imag_ks)
     635          214 :       IF (imag_ks) THEN
     636           50 :          CALL qs_ks_allocate_basics(qs_env, is_complex=imag_ks)
     637           50 :          IF (.NOT. dft_control%rtp_control%fixed_ions) THEN
     638           26 :             CALL rtp_create_SinvH_imag(rtp, dft_control%nspins)
     639              :          END IF
     640              :       END IF
     641              : 
     642              :       ! h
     643          214 :       IF (imag_h) CALL qs_matrix_h_allocate_imag_from_real(qs_env)
     644              : 
     645          214 :    END SUBROUTINE rt_init_complex_quantities
     646              : 
     647              : ! **************************************************************************************************
     648              : !> \brief Allocates and fills the local moment matrices (only available for linear scaling)
     649              : !> \param rtp Real time propagtion properties - local moment matrices are stored there
     650              : !> \param qs_env QS environment necessary for moment matrix calculation
     651              : ! **************************************************************************************************
     652           18 :    SUBROUTINE rt_init_local_moments(rtp, qs_env)
     653              :       TYPE(rt_prop_type), POINTER                        :: rtp
     654              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     655              : 
     656              :       INTEGER                                            :: k, nspin, output_unit
     657              :       REAL(kind=dp), DIMENSION(3)                        :: reference_point
     658              :       TYPE(cp_logger_type), POINTER                      :: logger
     659           18 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s, rho_old
     660              :       TYPE(dft_control_type), POINTER                    :: dft_control
     661              :       TYPE(rtp_control_type), POINTER                    :: rtc
     662              :       TYPE(section_vals_type), POINTER                   :: input, moments_section
     663              : 
     664           36 :       logger => cp_get_default_logger()
     665           18 :       output_unit = cp_logger_get_default_io_unit(logger)
     666              : 
     667           18 :       CALL get_qs_env(qs_env, dft_control=dft_control, matrix_s=matrix_s, input=input)
     668           18 :       rtc => dft_control%rtp_control
     669              :       moments_section => section_vals_get_subs_vals(input, &
     670           18 :                                                     "DFT%REAL_TIME_PROPAGATION%PRINT%MOMENTS")
     671              : 
     672              :       ! Construct the local moments matrix - copy from matrix_s structure
     673              :       ! NOTE : construction where blocks are allocated by neighbour lists does not seem to work,
     674              :       ! so doing a copy instead of:
     675              :       !    CALL dbcsr_create(rtp%local_moments(k)%matrix, template=matrix_s(1)%matrix, &
     676              :       !       name="Local moment")
     677              :       !    CALL cp_dbcsr_alloc_block_from_nbl(rtp%local_moments(k)%matrix, sab_all)
     678           18 :       NULLIFY (rtp%local_moments)
     679           72 :       ALLOCATE (rtp%local_moments(3))
     680           72 :       DO k = 1, 3
     681           54 :          NULLIFY (rtp%local_moments(k)%matrix)
     682           54 :          ALLOCATE (rtp%local_moments(k)%matrix)
     683           54 :          CALL dbcsr_create(rtp%local_moments(k)%matrix, template=matrix_s(1)%matrix, name="Local moment")
     684           54 :          CALL dbcsr_copy(rtp%local_moments(k)%matrix, matrix_s(1)%matrix)
     685           72 :          CALL dbcsr_set(rtp%local_moments(k)%matrix, 0.0_dp)
     686              :       END DO
     687              :       ! Workspace allocation
     688           18 :       NULLIFY (rtp%local_moments_work)
     689           18 :       ALLOCATE (rtp%local_moments_work)
     690           18 :       CALL dbcsr_create(rtp%local_moments_work, template=rtp%local_moments(1)%matrix, name="tmp")
     691           18 :       CALL dbcsr_copy(rtp%local_moments_work, rtp%local_moments(1)%matrix)
     692              : 
     693              :       CALL get_reference_point(rpoint=reference_point, qs_env=qs_env, &
     694           18 :                                reference=rtc%moment_trace_ref_type, ref_point=rtc%moment_trace_user_ref_point)
     695              : 
     696           18 :       CALL build_local_moment_matrix(qs_env, rtp%local_moments, 1, reference_point)
     697              : 
     698              :       ! Allocate the moments trace and output start moments
     699           18 :       CALL get_rtp(rtp, rho_old=rho_old)
     700           18 :       nspin = SIZE(rho_old)/2
     701          576 :       ALLOCATE (rtp%moments(SIZE(rho_old)/2, 3, rtp%nsteps + 1), source=CMPLX(0.0, 0.0, kind=dp))
     702           18 :       NULLIFY (rtp%times)
     703           54 :       ALLOCATE (rtp%times(rtp%nsteps + 1))
     704           18 :       NULLIFY (rtp%fields)
     705          270 :       ALLOCATE (rtp%fields(3, rtp%nsteps + 1), source=CMPLX(0.0, 0.0, kind=dp))
     706              : 
     707           18 :    END SUBROUTINE rt_init_local_moments
     708              : 
     709              : ! **************************************************************************************************
     710              : !> \brief Allocates and fills the local moment matrices (only available for linear scaling)
     711              : !> \param qs_env QS environment necessary for moment matrix calculation
     712              : ! **************************************************************************************************
     713           18 :    SUBROUTINE final_ft_output(qs_env)
     714              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     715              : 
     716              :       INTEGER                                            :: k, unit_nr
     717              :       TYPE(cell_type), POINTER                           :: cell
     718              :       TYPE(cp_logger_type), POINTER                      :: logger
     719              :       TYPE(dft_control_type), POINTER                    :: dft_control
     720              :       TYPE(rt_prop_type), POINTER                        :: rtp
     721              :       TYPE(section_vals_type), POINTER                   :: input, rtp_section
     722              : 
     723           18 :       CALL get_qs_env(qs_env, cell=cell, rtp=rtp, input=input, dft_control=dft_control)
     724           18 :       rtp_section => section_vals_get_subs_vals(input, "DFT%REAL_TIME_PROPAGATION")
     725           18 :       logger => cp_get_default_logger()
     726           18 :       unit_nr = cp_logger_get_default_io_unit(logger)
     727              :       CALL print_ft(rtp_section, rtp%moments, rtp%times, rtp%fields, dft_control%rtp_control, &
     728           18 :                     info_opt=unit_nr, cell=cell)
     729              :       ! Deallocating the local moments matrices and array
     730           72 :       DO k = 1, 3
     731           54 :          CALL dbcsr_release(rtp%local_moments(k)%matrix)
     732           72 :          DEALLOCATE (rtp%local_moments(k)%matrix)
     733              :       END DO
     734           18 :       DEALLOCATE (rtp%local_moments)
     735           18 :       CALL dbcsr_release(rtp%local_moments_work)
     736           18 :       DEALLOCATE (rtp%local_moments_work)
     737           18 :       DEALLOCATE (rtp%moments)
     738           18 :       DEALLOCATE (rtp%times)
     739           18 :       DEALLOCATE (rtp%fields)
     740           18 :    END SUBROUTINE final_ft_output
     741              : 
     742              : END MODULE rt_propagation
        

Generated by: LCOV version 2.0-1