(git:1f9fd2c)
Loading...
Searching...
No Matches
qs_scf_loop_utils.F
Go to the documentation of this file.
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!> \brief Utility routines for qs_scf
9! **************************************************************************************************
13 USE cp_dbcsr_api, ONLY: dbcsr_copy,&
20 USE kinds, ONLY: default_string_length,&
21 dp
22 USE kpoint_types, ONLY: kpoint_type
38 USE qs_ks_types, ONLY: qs_ks_did_change,&
42 USE qs_mo_types, ONLY: mo_set_type
44 USE qs_ot_scf, ONLY: ot_scf_destroy,&
48 USE qs_rho_types, ONLY: qs_rho_get,&
60 USE qs_scf_types, ONLY: &
68#include "./base/base_uses.f90"
69
70 IMPLICIT NONE
71
72 PRIVATE
73
74 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_loop_utils'
75
76 PUBLIC :: qs_scf_set_loop_flags, &
80
81CONTAINS
82
83! **************************************************************************************************
84!> \brief computes properties for a given hamiltonian using the current wfn
85!> \param scf_env ...
86!> \param diis_step ...
87!> \param energy_only ...
88!> \param just_energy ...
89!> \param exit_inner_loop ...
90! **************************************************************************************************
91 SUBROUTINE qs_scf_set_loop_flags(scf_env, diis_step, &
92 energy_only, just_energy, exit_inner_loop)
93
94 TYPE(qs_scf_env_type), POINTER :: scf_env
95 LOGICAL :: diis_step, energy_only, just_energy, &
96 exit_inner_loop
97
98! Some flags needed to be set at the beginning of the loop
99
100 diis_step = .false.
101 energy_only = .false.
102 just_energy = .false.
103
104 ! SCF loop, optimisation of the wfn coefficients
105 ! qs_env%rho%rho_r and qs_env%rho%rho_g should be up to date here
106
107 scf_env%iter_count = 0
108 exit_inner_loop = .false.
109
110 END SUBROUTINE qs_scf_set_loop_flags
111
112! **************************************************************************************************
113!> \brief takes known energy and derivatives and produces new wfns
114!> and or density matrix
115!> \param qs_env ...
116!> \param scf_env ...
117!> \param scf_control ...
118!> \param scf_section ...
119!> \param diis_step ...
120!> \param energy_only ...
121!> \param probe ...
122! **************************************************************************************************
123 SUBROUTINE qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, &
124 energy_only, probe)
125 TYPE(qs_environment_type), POINTER :: qs_env
126 TYPE(qs_scf_env_type), POINTER :: scf_env
127 TYPE(scf_control_type), POINTER :: scf_control
128 TYPE(section_vals_type), POINTER :: scf_section
129 LOGICAL :: diis_step, energy_only
130 TYPE(hairy_probes_type), DIMENSION(:), OPTIONAL, &
131 POINTER :: probe
132
133 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_scf_new_mos'
134
135 INTEGER :: handle, ispin
136 LOGICAL :: disable_diis, has_unit_metric, &
137 skip_diag_sub
138 REAL(kind=dp) :: saved_eps_diis
139 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
140 TYPE(dft_control_type), POINTER :: dft_control
141 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
142 TYPE(qs_energy_type), POINTER :: energy
143 TYPE(qs_ks_env_type), POINTER :: ks_env
144 TYPE(qs_rho_type), POINTER :: rho
145
146 CALL timeset(routinen, handle)
147
148 NULLIFY (energy, ks_env, matrix_ks, matrix_s, rho, mos, dft_control)
149
150 CALL get_qs_env(qs_env=qs_env, &
151 matrix_s=matrix_s, energy=energy, &
152 ks_env=ks_env, &
153 matrix_ks=matrix_ks, rho=rho, mos=mos, &
154 dft_control=dft_control, &
155 has_unit_metric=has_unit_metric)
156 scf_env%iter_param = 0.0_dp
157 disable_diis = dft_control%qs_control%xtb_control%do_tblite .AND. &
158 tb_native_scc_mixer_active(dft_control)
159 IF (disable_diis) THEN
160 saved_eps_diis = scf_control%eps_diis
161 scf_control%eps_diis = 0.0_dp
162 END IF
163
164 ! transfer total_zeff_corr from qs_env to scf_env only if
165 ! correct_el_density_dip is switched on [SGh]
166 IF (dft_control%correct_el_density_dip) THEN
167 scf_env%sum_zeff_corr = qs_env%total_zeff_corr
168 IF (abs(qs_env%total_zeff_corr) > 0.0_dp) THEN
169 IF (scf_env%method /= general_diag_method_nr) THEN
170 CALL cp_abort(__location__, &
171 "Please use ALGORITHM STANDARD in "// &
172 "SCF%DIAGONALIZATION if "// &
173 "CORE_CORRECTION /= 0.0 and "// &
174 "SURFACE_DIPOLE_CORRECTION TRUE ")
175 ELSEIF (dft_control%roks) THEN
176 CALL cp_abort(__location__, &
177 "Combination of "// &
178 "CORE_CORRECTION /= 0.0 and "// &
179 "SURFACE_DIPOLE_CORRECTION TRUE "// &
180 "is not implemented with ROKS")
181 ELSEIF (scf_control%diagonalization%mom) THEN
182 CALL cp_abort(__location__, &
183 "Combination of "// &
184 "CORE_CORRECTION /= 0.0 and "// &
185 "SURFACE_DIPOLE_CORRECTION TRUE "// &
186 "is not implemented with SCF%MOM")
187 END IF
188 END IF
189 END IF
190
191 SELECT CASE (scf_env%method)
192 CASE DEFAULT
193 CALL cp_abort(__location__, &
194 "unknown scf method: "// &
195 cp_to_string(scf_env%method))
196
197 ! *************************************************************************
198 ! Filter matrix diagonalisation: ugly implementation at this point of time
199 ! *************************************************************************
201
202 IF (abs(qs_env%total_zeff_corr) > 0.0_dp) THEN
203 CALL cp_abort(__location__, &
204 "CORE_CORRECTION /= 0.0 plus SURFACE_DIPOLE_CORRECTION TRUE "// &
205 "requires SCF%DIAGONALIZATION: ALGORITHM STANDARD")
206 END IF
207 CALL fb_env_do_diag(scf_env%filter_matrix_env, qs_env, &
208 matrix_ks, matrix_s, scf_section, diis_step)
209
210 ! Diagonlization in non orthonormal case
212 IF (dft_control%roks) THEN
213 CALL do_roks_diag(scf_env, mos, matrix_ks, matrix_s, &
214 scf_control, scf_section, diis_step, &
215 has_unit_metric)
216 ELSE
217 IF (scf_control%diagonalization%mom) THEN
218 CALL do_mom_diag(scf_env, mos, matrix_ks, &
219 matrix_s, scf_control, scf_section, &
220 diis_step)
221 ELSE
222 IF (dft_control%hairy_probes .EQV. .true.) THEN
223 CALL do_general_diag(scf_env, mos, matrix_ks, &
224 matrix_s, scf_control, scf_section, &
225 diis_step, &
226 probe)
227 ELSE
228 CALL do_general_diag(scf_env, mos, matrix_ks, &
229 matrix_s, scf_control, scf_section, &
230 diis_step)
231 END IF
232 END IF
233 IF (scf_control%do_diag_sub) THEN
234 skip_diag_sub = (scf_env%subspace_env%eps_diag_sub > 0.0_dp) .AND. &
235 (scf_env%iter_count == 1 .OR. scf_env%iter_delta > scf_env%subspace_env%eps_diag_sub)
236 IF (.NOT. skip_diag_sub) THEN
237 CALL do_scf_diag_subspace(qs_env, scf_env, scf_env%subspace_env, mos, rho, &
238 ks_env, scf_section, scf_control)
239 END IF
240 END IF
241 END IF
242 ! Diagonlization in orthonormal case
244 IF (dft_control%roks) THEN
245 CALL do_roks_diag(scf_env, mos, matrix_ks, matrix_s, &
246 scf_control, scf_section, diis_step, &
247 has_unit_metric)
248 ELSE
249 CALL do_special_diag(scf_env, mos, matrix_ks, &
250 scf_control, scf_section, &
251 diis_step)
252 END IF
253 ! OT diagonalization
254 CASE (ot_diag_method_nr)
255 CALL do_ot_diag(scf_env, mos, matrix_ks, matrix_s, &
256 scf_control, scf_section, diis_step)
257 ! Block Krylov diagonlization
259 IF ((scf_env%krylov_space%eps_std_diag > 0.0_dp) .AND. &
260 (scf_env%iter_count == 1 .OR. scf_env%iter_delta > scf_env%krylov_space%eps_std_diag)) THEN
261 IF (scf_env%krylov_space%always_check_conv) THEN
262 CALL do_block_krylov_diag(scf_env, mos, matrix_ks, &
263 scf_control, scf_section, check_moconv_only=.true.)
264 END IF
265 CALL do_general_diag(scf_env, mos, matrix_ks, &
266 matrix_s, scf_control, scf_section, diis_step)
267 ELSE
268 CALL do_block_krylov_diag(scf_env, mos, matrix_ks, &
269 scf_control, scf_section)
270 END IF
271 IF (scf_control%do_diag_sub) THEN
272 skip_diag_sub = (scf_env%subspace_env%eps_diag_sub > 0.0_dp) .AND. &
273 (scf_env%iter_count == 1 .OR. scf_env%iter_delta > scf_env%subspace_env%eps_diag_sub)
274 IF (.NOT. skip_diag_sub) THEN
275 CALL do_scf_diag_subspace(qs_env, scf_env, scf_env%subspace_env, mos, rho, &
276 ks_env, scf_section, scf_control)
277 END IF
278 END IF
279 ! Block Davidson diagonlization
281 CALL do_block_davidson_diag(qs_env, scf_env, mos, matrix_ks, matrix_s, scf_control, &
282 scf_section, .false.)
283 ! OT without diagonlization. Needs special treatment for SCP runs
284 CASE (ot_method_nr)
285 CALL qs_scf_loop_do_ot(qs_env, scf_env, scf_control%smear, mos, rho, &
286 qs_env%mo_derivs, energy%total, &
287 matrix_s, energy_only=energy_only, has_unit_metric=has_unit_metric)
288 END SELECT
289 IF (disable_diis) scf_control%eps_diis = saved_eps_diis
290
291 energy%kTS = 0.0_dp
292 energy%efermi = 0.0_dp
293 CALL get_qs_env(qs_env, mos=mos)
294 DO ispin = 1, SIZE(mos)
295 energy%kTS = energy%kTS + mos(ispin)%kTS
296 energy%efermi = energy%efermi + mos(ispin)%mu
297 END DO
298 energy%efermi = energy%efermi/real(SIZE(mos), kind=dp)
299
300 CALL timestop(handle)
301
302 END SUBROUTINE qs_scf_new_mos
303
304! **************************************************************************************************
305!> \brief Updates MOs and density matrix using diagonalization
306!> Kpoint code
307!> \param qs_env ...
308!> \param scf_env ...
309!> \param scf_control ...
310!> \param diis_step ...
311!> \param probe ...
312! **************************************************************************************************
313 SUBROUTINE qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, probe)
314 TYPE(qs_environment_type), POINTER :: qs_env
315 TYPE(qs_scf_env_type), POINTER :: scf_env
316 TYPE(scf_control_type), POINTER :: scf_control
317 LOGICAL :: diis_step
318 TYPE(hairy_probes_type), DIMENSION(:), OPTIONAL, &
319 POINTER :: probe
320
321 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_scf_new_mos_kp'
322
323 INTEGER :: handle, ispin
324 LOGICAL :: disable_diis, has_unit_metric
325 REAL(dp) :: diis_error, saved_eps_diis
326 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s
327 TYPE(dft_control_type), POINTER :: dft_control
328 TYPE(kpoint_type), POINTER :: kpoints
329 TYPE(mo_set_type), DIMENSION(:, :), POINTER :: mos
330 TYPE(qs_energy_type), POINTER :: energy
331
332 CALL timeset(routinen, handle)
333
334 NULLIFY (dft_control, kpoints, matrix_ks, matrix_s)
335
336 CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, kpoints=kpoints)
337 scf_env%iter_param = 0.0_dp
338 disable_diis = dft_control%qs_control%xtb_control%do_tblite .AND. &
339 tb_native_scc_mixer_active(dft_control)
340 IF (disable_diis) THEN
341 saved_eps_diis = scf_control%eps_diis
342 scf_control%eps_diis = 0.0_dp
343 END IF
344
345 IF (dft_control%roks) &
346 cpabort("KP code: ROKS method not available: ")
347
348 SELECT CASE (scf_env%method)
349 CASE DEFAULT
350 CALL cp_abort(__location__, &
351 "KP code: Unknown scf method: "// &
352 cp_to_string(scf_env%method))
354 ! Diagonlization in non orthonormal case
355 CALL get_qs_env(qs_env, matrix_ks_kp=matrix_ks, matrix_s_kp=matrix_s)
356 IF (dft_control%hairy_probes .EQV. .true.) THEN
357 scf_control%smear%do_smear = .false.
358 CALL do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, .true., &
359 diis_step, diis_error, qs_env, probe)
360 ELSE
361 CALL do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, .true., &
362 diis_step, diis_error, qs_env)
363 END IF
364 IF (diis_step) THEN
365 scf_env%iter_param = diis_error
366 scf_env%iter_method = "DIIS/Diag."
367 ELSE
368 IF (scf_env%mixing_method == 0) THEN
369 scf_env%iter_method = "NoMix/Diag."
370 ELSE IF (scf_env%mixing_method == 1) THEN
371 scf_env%iter_param = scf_env%p_mix_alpha
372 scf_env%iter_method = "P_Mix/Diag."
373 ELSEIF (scf_env%mixing_method > 1) THEN
374 scf_env%iter_param = scf_env%mixing_store%alpha
375 scf_env%iter_method = trim(scf_env%mixing_store%iter_method)//"/Diag."
376 END IF
377 END IF
379 CALL get_qs_env(qs_env=qs_env, has_unit_metric=has_unit_metric)
380 cpassert(has_unit_metric)
381 ! Diagonlization in orthonormal case
382 CALL cp_abort(__location__, &
383 "KP code: Scf method not available: "// &
384 cp_to_string(scf_env%method))
385 CASE (ot_diag_method_nr, &
389 CALL cp_abort(__location__, &
390 "KP code: Scf method not available: "// &
391 cp_to_string(scf_env%method))
392 CASE (smeagol_method_nr)
393 ! SMEAGOL interface
394 diis_step = .false.
395 IF (scf_env%mixing_method == 0) THEN
396 scf_env%iter_method = "NoMix/SMGL"
397 ELSE IF (scf_env%mixing_method == 1) THEN
398 scf_env%iter_param = scf_env%p_mix_alpha
399 scf_env%iter_method = "P_Mix/SMGL"
400 ELSE IF (scf_env%mixing_method > 1) THEN
401 scf_env%iter_param = scf_env%mixing_store%alpha
402 scf_env%iter_method = trim(scf_env%mixing_store%iter_method)//"/SMGL"
403 END IF
404 CALL run_smeagol_emtrans(qs_env, last=.false., iter=scf_env%iter_count, rho_ao_kp=scf_env%p_mix_new)
405 END SELECT
406 IF (disable_diis) scf_control%eps_diis = saved_eps_diis
407
408 CALL get_qs_env(qs_env=qs_env, energy=energy)
409 energy%kTS = 0.0_dp
410 energy%efermi = 0.0_dp
411 mos => kpoints%kp_env(1)%kpoint_env%mos
412 DO ispin = 1, SIZE(mos, 2)
413 energy%kTS = energy%kTS + mos(1, ispin)%kTS
414 energy%efermi = energy%efermi + mos(1, ispin)%mu
415 END DO
416 energy%efermi = energy%efermi/real(SIZE(mos, 2), kind=dp)
417
418 CALL timestop(handle)
419
420 END SUBROUTINE qs_scf_new_mos_kp
421
422! **************************************************************************************************
423!> \brief the inner loop of scf, specific to using to the orbital transformation method
424!> basically, in goes the ks matrix out goes a new p matrix
425!> \param qs_env ...
426!> \param scf_env ...
427!> \param smear ...
428!> \param mos ...
429!> \param rho ...
430!> \param mo_derivs ...
431!> \param total_energy ...
432!> \param matrix_s ...
433!> \param energy_only ...
434!> \param has_unit_metric ...
435!> \par History
436!> 03.2006 created [Joost VandeVondele]
437!> 2013 moved from qs_scf [Florian Schiffmann]
438! **************************************************************************************************
439 SUBROUTINE qs_scf_loop_do_ot(qs_env, scf_env, smear, mos, rho, mo_derivs, total_energy, &
440 matrix_s, energy_only, has_unit_metric)
441
442 TYPE(qs_environment_type), POINTER :: qs_env
443 TYPE(qs_scf_env_type), POINTER :: scf_env
444 TYPE(smear_type), POINTER :: smear
445 TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
446 TYPE(qs_rho_type), POINTER :: rho
447 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mo_derivs
448 REAL(kind=dp), INTENT(IN) :: total_energy
449 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
450 LOGICAL, INTENT(INOUT) :: energy_only
451 LOGICAL, INTENT(IN) :: has_unit_metric
452
453 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_scf_loop_do_ot'
454
455 INTEGER :: handle, ispin
456 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
457 TYPE(dbcsr_type), POINTER :: orthogonality_metric
458
459 CALL timeset(routinen, handle)
460 NULLIFY (rho_ao)
461
462 CALL qs_rho_get(rho, rho_ao=rho_ao)
463
464 IF (has_unit_metric) THEN
465 NULLIFY (orthogonality_metric)
466 ELSE
467 orthogonality_metric => matrix_s(1)%matrix
468 END IF
469
470 ! in case of LSD the first spin qs_ot_env will drive the minimization
471 ! in the case of a restricted calculation, it will make sure the spin orbitals are equal
472
473 CALL ot_scf_mini(mos, mo_derivs, smear, orthogonality_metric, &
474 total_energy, energy_only, scf_env%iter_delta, &
475 scf_env%qs_ot_env)
476
477 DO ispin = 1, SIZE(mos)
478 CALL set_mo_occupation(mo_set=mos(ispin), smear=smear)
479 END DO
480
481 DO ispin = 1, SIZE(mos)
482 CALL calculate_density_matrix(mos(ispin), &
483 rho_ao(ispin)%matrix, &
484 use_dbcsr=.true.)
485 END DO
486
487 scf_env%iter_method = scf_env%qs_ot_env(1)%OT_METHOD_FULL
488 scf_env%iter_param = scf_env%qs_ot_env(1)%ds_min
489 qs_env%broyden_adaptive_sigma = scf_env%qs_ot_env(1)%broyden_adaptive_sigma
490
491 CALL timestop(handle)
492
493 END SUBROUTINE qs_scf_loop_do_ot
494
495! **************************************************************************************************
496!> \brief Performs the requested density mixing if any needed
497!> \param scf_env Holds SCF environment information
498!> \param rho All data for the electron density
499!> \param para_env Parallel environment
500!> \param diis_step Did we do a DIIS step?
501! **************************************************************************************************
502 SUBROUTINE qs_scf_density_mixing(scf_env, rho, para_env, diis_step)
503 TYPE(qs_scf_env_type), POINTER :: scf_env
504 TYPE(qs_rho_type), POINTER :: rho
505 TYPE(mp_para_env_type), POINTER :: para_env
506 LOGICAL :: diis_step
507
508 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
509
510 NULLIFY (rho_ao_kp)
511
512 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
513
514 SELECT CASE (scf_env%mixing_method)
515 CASE (direct_mixing_nr)
516 CALL scf_env_density_mixing(scf_env%p_mix_new, &
517 scf_env%mixing_store, rho_ao_kp, para_env, scf_env%iter_delta, scf_env%iter_count, &
518 diis=diis_step)
521 ! Compute the difference p_out-p_in
522 CALL self_consistency_check(rho_ao_kp, scf_env%p_delta, para_env, scf_env%p_mix_new, &
523 delta=scf_env%iter_delta)
524 CASE (no_mixing_nr)
525 CASE DEFAULT
526 CALL cp_abort(__location__, &
527 "unknown scf mixing method: "// &
528 cp_to_string(scf_env%mixing_method))
529 END SELECT
530
531 END SUBROUTINE qs_scf_density_mixing
532
533! **************************************************************************************************
534!> \brief checks whether exit conditions for outer loop are satisfied
535!> \param qs_env ...
536!> \param scf_env ...
537!> \param scf_control ...
538!> \param should_stop ...
539!> \param outer_loop_converged ...
540!> \param exit_outer_loop ...
541! **************************************************************************************************
542 SUBROUTINE qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, &
543 outer_loop_converged, exit_outer_loop)
544 TYPE(qs_environment_type), POINTER :: qs_env
545 TYPE(qs_scf_env_type), POINTER :: scf_env
546 TYPE(scf_control_type), POINTER :: scf_control
547 LOGICAL :: should_stop, outer_loop_converged, &
548 exit_outer_loop
549
550 REAL(kind=dp) :: outer_loop_eps
551
552 outer_loop_converged = .true.
553 IF (scf_control%outer_scf%have_scf) THEN
554 ! We have an outer SCF loop...
555 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
556 outer_loop_converged = .false.
557
558 CALL outer_loop_gradient(qs_env, scf_env)
559 ! Multiple constraints: get largest deviation
560 outer_loop_eps = sqrt(maxval(scf_env%outer_scf%gradient(:, scf_env%outer_scf%iter_count)**2))
561
562 IF (outer_loop_eps < scf_control%outer_scf%eps_scf) outer_loop_converged = .true.
563 END IF
564
565 exit_outer_loop = should_stop .OR. outer_loop_converged .OR. &
566 scf_env%outer_scf%iter_count > scf_control%outer_scf%max_scf
567
568 END SUBROUTINE qs_scf_check_outer_exit
569
570! **************************************************************************************************
571!> \brief checks whether exit conditions for inner loop are satisfied
572!> \param qs_env ...
573!> \param scf_env ...
574!> \param scf_control ...
575!> \param should_stop ...
576!> \param just_energy ...
577!> \param exit_inner_loop ...
578!> \param inner_loop_converged ...
579!> \param output_unit ...
580! **************************************************************************************************
581 SUBROUTINE qs_scf_check_inner_exit(qs_env, scf_env, scf_control, should_stop, just_energy, &
582 exit_inner_loop, inner_loop_converged, output_unit)
583 TYPE(qs_environment_type), POINTER :: qs_env
584 TYPE(qs_scf_env_type), POINTER :: scf_env
585 TYPE(scf_control_type), POINTER :: scf_control
586 LOGICAL :: should_stop, just_energy, &
587 exit_inner_loop, inner_loop_converged
588 INTEGER :: output_unit
589
590 inner_loop_converged = .false.
591 exit_inner_loop = .false.
592
593 CALL external_control(should_stop, "SCF", target_time=qs_env%target_time, &
594 start_time=qs_env%start_time)
595 IF (scf_env%iter_delta < scf_control%eps_scf) THEN
596 IF (output_unit > 0) THEN
597 WRITE (unit=output_unit, fmt="(/,T3,A,I5,A/)") &
598 "*** SCF run converged in ", scf_env%iter_count, " steps ***"
599 END IF
600 inner_loop_converged = .true.
601 exit_inner_loop = .true.
602 ELSE IF (should_stop .OR. scf_env%iter_count >= scf_control%max_scf) THEN
603 inner_loop_converged = .false.
604 IF (just_energy) THEN
605 exit_inner_loop = .false.
606 ELSE
607 exit_inner_loop = .true.
608 IF (output_unit > 0) THEN
609 WRITE (unit=output_unit, fmt="(/,T3,A,I5,A/)") &
610 "Leaving inner SCF loop after reaching ", scf_env%iter_count, " steps."
611 END IF
612 END IF
613 END IF
614
615 END SUBROUTINE qs_scf_check_inner_exit
616
617! **************************************************************************************************
618!> \brief undoing density mixing. Important upon convergence
619!> \param scf_env ...
620!> \param rho ...
621!> \param dft_control ...
622!> \param para_env ...
623!> \param diis_step ...
624! **************************************************************************************************
625 SUBROUTINE qs_scf_undo_mixing(scf_env, rho, dft_control, para_env, diis_step)
626 TYPE(qs_scf_env_type), POINTER :: scf_env
627 TYPE(qs_rho_type), POINTER :: rho
628 TYPE(dft_control_type), POINTER :: dft_control
629 TYPE(mp_para_env_type), POINTER :: para_env
630 LOGICAL :: diis_step
631
632 CHARACTER(len=default_string_length) :: name
633 INTEGER :: ic, ispin, nc
634 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
635
636 NULLIFY (rho_ao_kp)
637
638 IF (scf_env%mixing_method > 0) THEN
639 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
640 nc = SIZE(scf_env%p_mix_new, 2)
641 SELECT CASE (scf_env%mixing_method)
642 CASE (direct_mixing_nr)
643 CALL scf_env_density_mixing(scf_env%p_mix_new, scf_env%mixing_store, &
644 rho_ao_kp, para_env, scf_env%iter_delta, &
645 scf_env%iter_count, diis=diis_step, &
646 invert=.true.)
647 DO ic = 1, nc
648 DO ispin = 1, dft_control%nspins
649 CALL dbcsr_get_info(rho_ao_kp(ispin, ic)%matrix, name=name) ! keep the name
650 CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
651 END DO
652 END DO
655 DO ic = 1, nc
656 DO ispin = 1, dft_control%nspins
657 CALL dbcsr_get_info(rho_ao_kp(ispin, ic)%matrix, name=name) ! keep the name
658 CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
659 END DO
660 END DO
661 END SELECT
662 END IF
663 END SUBROUTINE qs_scf_undo_mixing
664
665! **************************************************************************************************
666!> \brief Performs the updates rho (takes care of mixing as well)
667!> \param rho ...
668!> \param qs_env ...
669!> \param scf_env ...
670!> \param ks_env ...
671!> \param mix_rho ...
672! **************************************************************************************************
673 SUBROUTINE qs_scf_rho_update(rho, qs_env, scf_env, ks_env, mix_rho)
674 TYPE(qs_rho_type), POINTER :: rho
675 TYPE(qs_environment_type), POINTER :: qs_env
676 TYPE(qs_scf_env_type), POINTER :: scf_env
677 TYPE(qs_ks_env_type), POINTER :: ks_env
678 LOGICAL, INTENT(IN) :: mix_rho
679
680 TYPE(mp_para_env_type), POINTER :: para_env
681
682 NULLIFY (para_env)
683 CALL get_qs_env(qs_env, para_env=para_env)
684 ! ** update qs_env%rho
685 CALL qs_rho_update_rho(rho, qs_env=qs_env)
686 ! ** Density mixing through density matrix or on the reciprocal space grid (exclusive)
687 IF (mix_rho) THEN
688 CALL gspace_mixing(qs_env, scf_env%mixing_method, scf_env%mixing_store, rho, &
689 para_env, scf_env%iter_count)
690
691 END IF
692 CALL qs_ks_did_change(ks_env, rho_changed=.true.)
693
694 END SUBROUTINE qs_scf_rho_update
695
696! **************************************************************************************************
697!> \brief Performs the necessary steps before leaving innner scf loop
698!> \param scf_env ...
699!> \param qs_env ...
700!> \param diis_step ...
701!> \param output_unit ...
702! **************************************************************************************************
703 SUBROUTINE qs_scf_inner_finalize(scf_env, qs_env, diis_step, output_unit)
704 TYPE(qs_scf_env_type), POINTER :: scf_env
705 TYPE(qs_environment_type), POINTER :: qs_env
706 LOGICAL :: diis_step
707 INTEGER, INTENT(IN) :: output_unit
708
709 LOGICAL :: do_kpoints
710 TYPE(dft_control_type), POINTER :: dft_control
711 TYPE(mp_para_env_type), POINTER :: para_env
712 TYPE(qs_energy_type), POINTER :: energy
713 TYPE(qs_ks_env_type), POINTER :: ks_env
714 TYPE(qs_rho_type), POINTER :: rho
715
716 NULLIFY (energy, rho, dft_control, ks_env)
717
718 CALL get_qs_env(qs_env=qs_env, energy=energy, ks_env=ks_env, &
719 rho=rho, dft_control=dft_control, para_env=para_env, &
720 do_kpoints=do_kpoints)
721
722 CALL cleanup_scf_loop(scf_env)
723
724 ! now, print out energies and charges corresponding to the obtained wfn
725 ! (this actually is not 100% consistent at this point)!
726 CALL qs_scf_print_summary(output_unit, qs_env)
727
728 CALL qs_scf_undo_mixing(scf_env, rho, dft_control, para_env, diis_step)
729
730 ! *** update rspace rho since the mo changed
731 ! *** this might not always be needed (i.e. no post calculation / no forces )
732 ! *** but guarantees that rho and wfn are consistent at this point
733 CALL qs_scf_rho_update(rho, qs_env, scf_env, ks_env, mix_rho=.false.)
734
735 END SUBROUTINE qs_scf_inner_finalize
736
737! **************************************************************************************************
738!> \brief perform cleanup operations at the end of an scf loop
739!> \param scf_env ...
740!> \par History
741!> 03.2006 created [Joost VandeVondele]
742! **************************************************************************************************
743 SUBROUTINE cleanup_scf_loop(scf_env)
744 TYPE(qs_scf_env_type), INTENT(INOUT) :: scf_env
745
746 CHARACTER(len=*), PARAMETER :: routinen = 'cleanup_scf_loop'
747
748 INTEGER :: handle, ispin
749
750 CALL timeset(routinen, handle)
751
752 SELECT CASE (scf_env%method)
753 CASE (ot_method_nr)
754 DO ispin = 1, SIZE(scf_env%qs_ot_env)
755 CALL ot_scf_destroy(scf_env%qs_ot_env(ispin))
756 END DO
757 DEALLOCATE (scf_env%qs_ot_env)
758 CASE (ot_diag_method_nr)
759 !
761 !
763 !
765 !
767 !
768 CASE (smeagol_method_nr)
769 !
770 CASE DEFAULT
771 CALL cp_abort(__location__, &
772 "unknown scf method method:"// &
773 cp_to_string(scf_env%method))
774 END SELECT
775
776 CALL timestop(handle)
777
778 END SUBROUTINE cleanup_scf_loop
779
780END MODULE qs_scf_loop_utils
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
Routines to handle the external control of CP2K.
subroutine, public external_control(should_stop, flag, globenv, target_time, start_time, force_check)
External manipulations during a run : when the <PROJECT_NAME>.EXIT_$runtype command is sent the progr...
various routines to log and control the output. The idea is that decisions about where to log should ...
objects that represent the structure of input sections and the data contained in an input section
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Types and basic routines needed for a kpoint calculation.
Interface to the message passing library MPI.
collects routines that calculate density matrices
module that contains the definitions of the scf types
integer, parameter, public new_pulay_mixing_nr
integer, parameter, public broyden_mixing_nr
integer, parameter, public modified_broyden_mixing_nr
integer, parameter, public no_mixing_nr
integer, parameter, public direct_mixing_nr
integer, parameter, public multisecant_mixing_nr
integer, parameter, public pulay_mixing_nr
integer, parameter, public gspace_mixing_nr
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
subroutine, public fb_env_do_diag(fb_env, qs_env, matrix_ks, matrix_s, scf_section, diis_step)
Do filtered matrix method diagonalisation.
subroutine, public gspace_mixing(qs_env, mixing_method, mixing_store, rho, para_env, iter_count)
Driver for the g-space mixing, calls the proper routine given the requested method.
subroutine, public qs_ks_did_change(ks_env, s_mstruct_changed, rho_changed, potential_changed, full_reset)
tells that some of the things relevant to the ks calculation did change. has to be called when change...
subroutine, public self_consistency_check(rho_ao, p_delta, para_env, p_out, delta)
...
Set occupation of molecular orbitals.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
methods for deltaSCF calculations
subroutine, public do_mom_diag(scf_env, mos, matrix_ks, matrix_s, scf_control, scf_section, diis_step)
do an SCF iteration, then compute occupation numbers of the new molecular orbitals according to their...
basic functionality for using ot in the scf routines.
Definition qs_ot_scf.F:14
subroutine, public ot_scf_mini(mo_array, matrix_dedc, smear, matrix_s, energy, energy_only, delta, qs_ot_env)
performs the actual minimisation, needs only limited info updated for restricted calculations matrix_...
Definition qs_ot_scf.F:125
subroutine, public ot_scf_destroy(qs_ot_env)
...
Definition qs_ot_scf.F:444
Routines for performing an outer scf loop.
subroutine, public outer_loop_gradient(qs_env, scf_env)
computes the gradient wrt to the outer loop variables
methods of the rho structure (defined in qs_rho_types)
subroutine, public qs_rho_update_rho(rho_struct, qs_env, rho_xc_external, local_rho_set, task_list_external, task_list_external_soft, pw_env_external, para_env_external)
updates rho_r and rho_g to the rhorho_ao. if use_kinetic_energy_density also computes tau_r and tau_g...
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
Different diagonalization schemes that can be used for the iterative solution of the eigenvalue probl...
subroutine, public do_ot_diag(scf_env, mos, matrix_ks, matrix_s, scf_control, scf_section, diis_step)
the inner loop of scf, specific to iterative diagonalization using OT with S matrix; basically,...
subroutine, public do_block_davidson_diag(qs_env, scf_env, mos, matrix_ks, matrix_s, scf_control, scf_section, check_moconv_only)
iterative diagonalization using the block davidson space approach
subroutine, public do_roks_diag(scf_env, mos, matrix_ks, matrix_s, scf_control, scf_section, diis_step, orthogonal_basis)
Solve a set restricted open Kohn-Sham (ROKS) equations based on the alpha and beta Kohn-Sham matrices...
subroutine, public do_scf_diag_subspace(qs_env, scf_env, subspace_env, mos, rho, ks_env, scf_section, scf_control)
inner loop within MOS subspace, to refine occupation and density, before next diagonalization of the ...
subroutine, public do_block_krylov_diag(scf_env, mos, matrix_ks, scf_control, scf_section, check_moconv_only)
iterative diagonalization using the block Krylov-space approach
subroutine, public do_special_diag(scf_env, mos, matrix_ks, scf_control, scf_section, diis_step)
the inner loop of scf, specific to diagonalization without S matrix basically, in goes the ks matrix ...
subroutine, public do_general_diag(scf_env, mos, matrix_ks, matrix_s, scf_control, scf_section, diis_step, probe)
...
subroutine, public do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, update_p, diis_step, diis_error, qs_env, probe)
Kpoint diagonalization routine Transforms matrices to kpoint, distributes kpoint groups,...
Utility routines for qs_scf.
subroutine, public qs_scf_check_inner_exit(qs_env, scf_env, scf_control, should_stop, just_energy, exit_inner_loop, inner_loop_converged, output_unit)
checks whether exit conditions for inner loop are satisfied
subroutine, public qs_scf_inner_finalize(scf_env, qs_env, diis_step, output_unit)
Performs the necessary steps before leaving innner scf loop.
subroutine, public qs_scf_set_loop_flags(scf_env, diis_step, energy_only, just_energy, exit_inner_loop)
computes properties for a given hamiltonian using the current wfn
subroutine, public qs_scf_rho_update(rho, qs_env, scf_env, ks_env, mix_rho)
Performs the updates rho (takes care of mixing as well)
subroutine, public qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, probe)
Updates MOs and density matrix using diagonalization Kpoint code.
subroutine, public qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, outer_loop_converged, exit_outer_loop)
checks whether exit conditions for outer loop are satisfied
subroutine, public qs_scf_density_mixing(scf_env, rho, para_env, diis_step)
Performs the requested density mixing if any needed.
subroutine, public qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only, probe)
takes known energy and derivatives and produces new wfns and or density matrix
groups fairly general SCF methods, so that modules other than qs_scf can use them too split off from ...
subroutine, public scf_env_density_mixing(p_mix_new, mixing_store, rho_ao, para_env, iter_delta, iter_count, diis, invert)
perform (if requested) a density mixing
subroutine, public qs_scf_print_summary(output_unit, qs_env)
writes a summary of information after scf
module that contains the definitions of the scf types
integer, parameter, public ot_diag_method_nr
integer, parameter, public filter_matrix_diag_method_nr
integer, parameter, public block_davidson_diag_method_nr
integer, parameter, public smeagol_method_nr
integer, parameter, public ot_method_nr
integer, parameter, public special_diag_method_nr
integer, parameter, public block_krylov_diag_method_nr
integer, parameter, public general_diag_method_nr
parameters that control an scf iteration
CP2K+SMEAGOL interface.
subroutine, public run_smeagol_emtrans(qs_env, last, iter, rho_ao_kp)
Run NEGF/SMEAGOL transport calculation.
interface to tblite
logical function, public tb_native_scc_mixer_active(dft_control)
Return whether the tblite native SCC mixer is active for this run.
Contains information about kpoints.
stores all the informations relevant to an mpi environment
calculation environment to calculate the ks matrix, holds all the needed vars. assumes that the core ...
keeps the density in various representations, keeping track of which ones are valid.
contains the parameters needed by a scf run