LCOV - code coverage report
Current view: top level - src/motion - rt_propagation.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 97.8 % 278 272
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 8 8

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

Generated by: LCOV version 2.0-1