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