(git:3f0d10b)
Loading...
Searching...
No Matches
qs_scf.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! **************************************************************************************************
9!> \brief Routines for the Quickstep SCF run.
10!> \par History
11!> - Joost VandeVondele (02.2002)
12!> added code for: incremental (pab and gvg) update
13!> initialisation (init_cube, l_info)
14!> - Joost VandeVondele (02.2002)
15!> called the poisson code of the classical part
16!> this takes into account the spherical cutoff and allows for
17!> isolated systems
18!> - Joost VandeVondele (02.2002)
19!> added multiple grid feature
20!> changed to spherical cutoff consistently (?)
21!> therefore removed the gradient correct functionals
22!> - updated with the new QS data structures (10.04.02,MK)
23!> - copy_matrix replaced by transfer_matrix (11.04.02,MK)
24!> - nrebuild_rho and nrebuild_gvg unified (12.04.02,MK)
25!> - set_mo_occupation for smearing of the MO occupation numbers
26!> (17.04.02,MK)
27!> - MO level shifting added (22.04.02,MK)
28!> - Usage of TYPE mo_set_p_type
29!> - Joost VandeVondele (05.2002)
30!> added cholesky based diagonalisation
31!> - 05.2002 added pao method [fawzi]
32!> - parallel FFT (JGH 22.05.2002)
33!> - 06.2002 moved KS matrix construction to qs_build_KS_matrix.F [fawzi]
34!> - started to include more LSD (01.2003,Joost VandeVondele)
35!> - 02.2003 scf_env [fawzi]
36!> - got rid of nrebuild (01.2004, Joost VandeVondele)
37!> - 10.2004 removed pao [fawzi]
38!> - 03.2006 large cleaning action [Joost VandeVondele]
39!> - High-spin ROKS added (05.04.06,MK)
40!> - Mandes (10.2013)
41!> intermediate energy communication with external communicator added
42!> - kpoints (08.2014, JGH)
43!> - unified k-point and gamma-point code (2014.11) [Ole Schuett]
44!> - added extra SCF loop for CDFT constraints (12.2015) [Nico Holmberg]
45!> \author Matthias Krack (30.04.2001)
46! **************************************************************************************************
47MODULE qs_scf
49 USE cc4s_export, ONLY: write_cc4s
51 USE cp_cfm_types, ONLY: cp_cfm_create,&
58 USE cp_dbcsr_api, ONLY: &
66 USE cp_files, ONLY: close_file
71 USE cp_fm_types, ONLY: cp_fm_create,&
85 cp_p_file,&
94 USE input_constants, ONLY: &
106 USE kinds, ONLY: default_path_length,&
108 dp
112 USE kpoint_types, ONLY: get_kpoint_info,&
115 USE machine, ONLY: m_flush,&
117 USE mathlib, ONLY: invert_matrix
118 USE message_passing, ONLY: mp_comm_type,&
121 USE physcon, ONLY: evolt
122 USE preconditioner, ONLY: &
131 USE pw_env_types, ONLY: pw_env_get,&
133 USE pw_pool_types, ONLY: pw_pool_type
146 USE qs_diis, ONLY: qs_diis_b_clear,&
154 USE qs_fod, ONLY: qs_fod_validate
155 USE qs_integrate_potential, ONLY: integrate_v_rspace
156 USE qs_kind_types, ONLY: get_qs_kind,&
167 USE qs_ks_atom, ONLY: update_ks_atom
170 USE qs_ks_types, ONLY: get_ks_env,&
176 USE qs_matrix_pools, ONLY: mpools_get,&
184 USE qs_mo_types, ONLY: allocate_mo_set,&
187 get_mo_set,&
192 USE qs_ot, ONLY: qs_ot_complex_multiply,&
197 USE qs_ot_scf, ONLY: ot_scf_destroy,&
200 USE qs_ot_types, ONLY: &
213 USE qs_rho_types, ONLY: qs_rho_get,&
217 USE qs_scf_loop_utils, ONLY: &
237 USE qs_scf_types, ONLY: &
251#include "./base/base_uses.f90"
252
253 IMPLICIT NONE
254
255 PRIVATE
256
257 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf'
258 INTEGER, PARAMETER, PRIVATE :: kp_ot_entry_initial = 1, &
259 kp_ot_entry_reuse = 2, &
260 kp_ot_entry_resized = 3, &
261 kp_ot_entry_refresh = 4
262 LOGICAL, PRIVATE :: reuse_precond = .false.
263 LOGICAL, PRIVATE :: used_history = .false.
264
266
267CONTAINS
268
269! **************************************************************************************************
270!> \brief perform an scf procedure in the given qs_env
271!> \param qs_env the qs_environment where to perform the scf procedure
272!> \param has_converged ...
273!> \param total_scf_steps ...
274!> \par History
275!> 02.2003 introduced scf_env, moved real work to scf_env_do_scf [fawzi]
276!> \author fawzi
277!> \note
278! **************************************************************************************************
279 SUBROUTINE scf(qs_env, has_converged, total_scf_steps)
280 TYPE(qs_environment_type), POINTER :: qs_env
281 LOGICAL, INTENT(OUT), OPTIONAL :: has_converged
282 INTEGER, INTENT(OUT), OPTIONAL :: total_scf_steps
283
284 INTEGER :: ihistory, max_scf_tmp, tsteps
285 LOGICAL :: converged, outer_scf_loop, should_stop
286 LOGICAL, SAVE :: first_step_flag = .true.
287 REAL(kind=dp), DIMENSION(:, :), POINTER :: gradient_history, variable_history
288 TYPE(cp_logger_type), POINTER :: logger
289 TYPE(dft_control_type), POINTER :: dft_control
290 TYPE(qs_scf_env_type), POINTER :: scf_env
291 TYPE(scf_control_type), POINTER :: scf_control
292 TYPE(section_vals_type), POINTER :: dft_section, input, scf_section
293
294 NULLIFY (scf_env)
295 logger => cp_get_default_logger()
296 cpassert(ASSOCIATED(qs_env))
297 IF (PRESENT(has_converged)) THEN
298 has_converged = .false.
299 END IF
300 IF (PRESENT(total_scf_steps)) THEN
301 total_scf_steps = 0
302 END IF
303 CALL get_qs_env(qs_env, scf_env=scf_env, input=input, &
304 dft_control=dft_control, scf_control=scf_control)
305 CALL qs_fod_validate(input, logger, qs_env)
306 qs_env%scf_convergence_available = .false.
307 qs_env%scf_converged = .false.
308 IF (scf_control%max_scf > 0) THEN
309
310 dft_section => section_vals_get_subs_vals(input, "DFT")
311 scf_section => section_vals_get_subs_vals(dft_section, "SCF")
312
313 IF (.NOT. ASSOCIATED(scf_env)) THEN
314 CALL qs_scf_env_initialize(qs_env, scf_env)
315 ! Moved here from qs_scf_env_initialize to be able to have more scf_env
316 CALL set_qs_env(qs_env, scf_env=scf_env)
317 ELSE
318 CALL qs_scf_env_initialize(qs_env, scf_env)
319 END IF
320
321 IF ((scf_control%density_guess == history_guess) .AND. (first_step_flag)) THEN
322 max_scf_tmp = scf_control%max_scf
323 scf_control%max_scf = 1
324 outer_scf_loop = scf_control%outer_scf%have_scf
325 scf_control%outer_scf%have_scf = .false.
326 END IF
327
328 IF (.NOT. dft_control%qs_control%cdft) THEN
329 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
330 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
331 ELSE
332 ! Third SCF loop needed for CDFT with OT to properly restart OT inner loop
333 CALL cdft_scf(qs_env=qs_env, should_stop=should_stop, &
334 has_converged=converged, total_scf_steps=tsteps)
335 END IF
336
337 qs_env%scf_convergence_available = .true.
338 qs_env%scf_converged = converged
339
340 ! If SCF has not converged, then we should not start MP2
341 IF (ASSOCIATED(qs_env%mp2_env)) qs_env%mp2_env%hf_fail = .NOT. converged
342
343 ! CC4S export is performed only when enabled in the input section.
344 CALL write_cc4s(qs_env, converged)
345
346 ! Add the converged outer_scf SCF gradient(s)/variable(s) to history
347 IF (scf_control%outer_scf%have_scf) THEN
348 ihistory = scf_env%outer_scf%iter_count
349 CALL get_qs_env(qs_env, gradient_history=gradient_history, &
350 variable_history=variable_history)
351 ! We only store the latest two values
352 gradient_history(:, 1) = gradient_history(:, 2)
353 gradient_history(:, 2) = scf_env%outer_scf%gradient(:, ihistory)
354 variable_history(:, 1) = variable_history(:, 2)
355 variable_history(:, 2) = scf_env%outer_scf%variables(:, ihistory)
356 ! Reset flag
357 IF (used_history) used_history = .false.
358 ! Update a counter and check if the Jacobian should be deallocated
359 IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian)) THEN
360 scf_control%outer_scf%cdft_opt_control%ijacobian(2) = scf_control%outer_scf%cdft_opt_control%ijacobian(2) + 1
361 IF (scf_control%outer_scf%cdft_opt_control%ijacobian(2) >= &
362 scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) .AND. &
363 scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) > 0) THEN
364 scf_env%outer_scf%deallocate_jacobian = .true.
365 END IF
366 END IF
367 END IF
368 ! *** add the converged wavefunction to the wavefunction history
369 IF ((ASSOCIATED(qs_env%wf_history)) .AND. &
370 ((scf_control%density_guess /= history_guess) .OR. &
371 (.NOT. first_step_flag))) THEN
372 IF (.NOT. dft_control%qs_control%cdft) THEN
373 ! No next geometry step in a standalone single-point calculation.
374 IF (.NOT. qs_env%skip_wf_history) THEN
375 CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
376 END IF
377 ELSE
378 IF (dft_control%qs_control%cdft_control%should_purge) THEN
379 CALL wfi_purge_history(qs_env)
380 CALL outer_loop_purge_history(qs_env)
381 dft_control%qs_control%cdft_control%should_purge = .false.
382 ELSE
383 CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
384 END IF
385 END IF
386 ELSE IF ((scf_control%density_guess == history_guess) .AND. &
387 (first_step_flag)) THEN
388 scf_control%max_scf = max_scf_tmp
389 scf_control%outer_scf%have_scf = outer_scf_loop
390 first_step_flag = .false.
391 END IF
392
393 ! *** compute properties that depend on the converged wavefunction
394 IF (.NOT. (should_stop)) CALL qs_scf_compute_properties(qs_env)
395
396 ! *** SMEAGOL interface ***
397 IF (.NOT. (should_stop)) THEN
398 ! compute properties that depend on the converged wavefunction ..
399 CALL run_smeagol_emtrans(qs_env, last=.true., iter=0)
400 ! .. or save matrices related to bulk leads
401 CALL run_smeagol_bulktrans(qs_env)
402 END IF
403
404 ! *** cleanup
405 CALL scf_env_cleanup(scf_env)
406 IF (dft_control%qs_control%cdft) THEN
407 CALL cdft_control_cleanup(dft_control%qs_control%cdft_control)
408 END IF
409
410 IF (PRESENT(has_converged)) THEN
411 has_converged = converged
412 END IF
413 IF (PRESENT(total_scf_steps)) THEN
414 total_scf_steps = tsteps
415 END IF
416
417 END IF
418
419 END SUBROUTINE scf
420
421! **************************************************************************************************
422!> \brief perform an scf loop
423!> \param scf_env the scf_env where to perform the scf procedure
424!> \param scf_control ...
425!> \param qs_env the qs_env, the scf_env lives in
426!> \param converged will be true / false if converged is reached
427!> \param should_stop ...
428!> \param total_scf_steps ...
429!> \par History
430!> long history, see cvs and qs_scf module history
431!> 02.2003 introduced scf_env [fawzi]
432!> 09.2005 Frozen density approximation [TdK]
433!> 06.2007 Check for SCF iteration count early [jgh]
434!> 10.2019 switch_surf_dip [SGh]
435!> \author Matthias Krack
436!> \note
437! **************************************************************************************************
438 SUBROUTINE scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
439
440 TYPE(qs_scf_env_type), POINTER :: scf_env
441 TYPE(scf_control_type), POINTER :: scf_control
442 TYPE(qs_environment_type), POINTER :: qs_env
443 LOGICAL, INTENT(OUT) :: converged, should_stop
444 INTEGER, INTENT(OUT) :: total_scf_steps
445
446 CHARACTER(LEN=*), PARAMETER :: routinen = 'scf_env_do_scf'
447 INTEGER, PARAMETER :: max_ot_kp_subspace_refreshes = 4
448 REAL(kind=dp), PARAMETER :: adiis_stagnation_ratio = 1.0e-2_dp
449
450 CHARACTER(LEN=default_string_length) :: description, name
451 INTEGER :: accepted_ot_kp_searches, ext_master_id, handle, handle2, i_tmp, ic, ispin, &
452 iter_count, kp_ot_entry_reason, ot_kp_subspace_refresh_count, &
453 ot_kp_subspace_refresh_iter_count, output_unit, scf_energy_message_tag, total_steps
454 LOGICAL :: added_mos_auto_grow, adiis_pushed, adiis_restarted, adiis_stagnated, &
455 adiis_validation, density_full_step, diis_step, do_kpoints, energy_only, exit_inner_loop, &
456 exit_outer_loop, inner_loop_converged, internal_tblite_density_full_step, &
457 internal_tblite_mixer, just_energy, ot_kp_subspace_refresh, &
458 ot_kp_subspace_refresh_pending, outer_loop_converged, tblite_native_mixer, u_changed
459 REAL(dp), POINTER :: subspace_density(:, :), &
460 subspace_fock(:, :)
461 REAL(kind=dp) :: adiis_raw_delta, adiis_step_delta, t1, t2
462 REAL(kind=dp), DIMENSION(3) :: res_val_3
463 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
464 TYPE(cp_logger_type), POINTER :: logger
465 TYPE(cp_result_type), POINTER :: results
466 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
467 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, rho_ao_kp
468 TYPE(dft_control_type), POINTER :: dft_control
469 TYPE(energy_correction_type), POINTER :: ec_env
470 TYPE(kpoint_type), POINTER :: kpoints
471 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos, mos_last_converged
472 TYPE(mp_comm_type) :: external_comm
473 TYPE(mp_para_env_type), POINTER :: para_env
474 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
475 TYPE(pw_env_type), POINTER :: pw_env
476 TYPE(qs_charges_type), POINTER :: qs_charges
477 TYPE(qs_energy_type), POINTER :: energy
478 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
479 TYPE(qs_ks_env_type), POINTER :: ks_env
480 TYPE(qs_rho_type), POINTER :: rho
481 TYPE(rho_atom_type), POINTER :: rho_atom(:)
482 TYPE(section_vals_type), POINTER :: dft_section, input, scf_section
483
484! Weak orbital metrics can need several bounded REF rebuilds to close the canonical density tail.
485
486 CALL timeset(routinen, handle)
487
488 NULLIFY (dft_control, rho, energy, &
489 logger, qs_charges, ks_env, mos, atomic_kind_set, qs_kind_set, &
490 particle_set, dft_section, input, &
491 scf_section, para_env, results, kpoints, pw_env, matrix_ks, &
492 matrix_ks_kp, rho_ao_kp, mos_last_converged)
493
494 cpassert(ASSOCIATED(scf_env))
495 cpassert(ASSOCIATED(qs_env))
496
497 logger => cp_get_default_logger()
498 t1 = m_walltime()
499
500 CALL get_qs_env(qs_env=qs_env, &
501 energy=energy, &
502 particle_set=particle_set, &
503 qs_charges=qs_charges, &
504 ks_env=ks_env, &
505 atomic_kind_set=atomic_kind_set, &
506 qs_kind_set=qs_kind_set, &
507 rho=rho, &
508 mos=mos, &
509 matrix_ks_kp=matrix_ks_kp, &
510 input=input, &
511 dft_control=dft_control, &
512 do_kpoints=do_kpoints, &
513 kpoints=kpoints, &
514 results=results, &
515 pw_env=pw_env, &
516 para_env=para_env)
517 tblite_native_mixer = dft_control%qs_control%xtb_control%do_tblite .AND. &
518 scf_env%method /= ot_method_nr .AND. &
519 tb_native_scc_mixer_active(dft_control)
520 internal_tblite_mixer = (dft_control%qs_control%dftb .AND. &
521 dft_control%qs_control%dftb_control%tblite_scc_mixer == tblite_scc_mixer_tblite) .OR. &
522 (dft_control%qs_control%xtb .AND. &
523 .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
524 dft_control%qs_control%xtb_control%tblite_scc_mixer == tblite_scc_mixer_tblite)
525 internal_tblite_density_full_step = dft_control%qs_control%xtb .AND. &
526 .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
527 dft_control%qs_control%xtb_control%tblite_scc_mixer == tblite_scc_mixer_tblite
528
529 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
530
531 dft_section => section_vals_get_subs_vals(input, "DFT")
532 scf_section => section_vals_get_subs_vals(dft_section, "SCF")
533
534 output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
535 extension=".scfLog")
536
537 IF (scf_control%gce%do_gce .AND. output_unit > 0) THEN
538 WRITE (unit=output_unit, fmt="(/,T2,78('-'))")
539 WRITE (unit=output_unit, fmt="(T31,A)") &
540 "GRAND-CANONICAL SCF"
541 WRITE (unit=output_unit, fmt="(T20,A,F12.6,A)") &
542 "Target work function (TWF):", &
543 evolt*scf_control%gce%target_workfunction, " eV"
544 WRITE (unit=output_unit, fmt="(T2,78('-'))")
545 END IF
546
547 IF (output_unit > 0) WRITE (unit=output_unit, fmt="(/,/,T2,A)") &
548 "SCF WAVEFUNCTION OPTIMIZATION"
549
550 ! when switch_surf_dip is switched on, indicate storing mos from the last converged step
551 IF (dft_control%switch_surf_dip) THEN
552 CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
553 DO ispin = 1, dft_control%nspins
554 CALL reassign_allocated_mos(mos(ispin), mos_last_converged(ispin))
555 END DO
556 IF (output_unit > 0) WRITE (unit=output_unit, fmt="(/,/,T2,A)") &
557 "COPIED mos_last_converged ---> mos"
558 END IF
559
560 IF ((output_unit > 0) .AND. (.NOT. scf_control%use_ot)) THEN
561 WRITE (unit=output_unit, &
562 fmt="(/,T3,A,T12,A,T31,A,T39,A,T59,A,T75,A,/,T3,A)") &
563 "Step", "Update method", "Time", "Convergence", "Total energy", "Change", &
564 repeat("-", 78)
565 END IF
566 CALL cp_add_iter_level(logger, "QS_SCF")
567
568 ! check for external communicator and if the intermediate energy should be sent
569 res_val_3(:) = -1.0_dp
570 description = "[EXT_SCF_ENER_COMM]"
571 IF (test_for_result(results, description=description)) THEN
572 CALL get_results(results, description=description, &
573 values=res_val_3, n_entries=i_tmp)
574 cpassert(i_tmp == 3)
575 IF (all(res_val_3(:) <= 0.0_dp)) THEN
576 CALL cp_abort(__location__, &
577 " Trying to access result ("//trim(description)// &
578 ") which is not correctly stored.")
579 END IF
580 CALL external_comm%set_handle(nint(res_val_3(1)))
581 END IF
582 ext_master_id = nint(res_val_3(2))
583 scf_energy_message_tag = nint(res_val_3(3))
584
585 ! *** outer loop of the scf, can treat other variables,
586 ! *** such as lagrangian multipliers
587 scf_env%outer_scf%iter_count = 0
588 accepted_ot_kp_searches = 0
589 iter_count = 0
590 kp_ot_entry_reason = kp_ot_entry_initial
591 ot_kp_subspace_refresh_count = 0
592 ot_kp_subspace_refresh_iter_count = 0
593 ot_kp_subspace_refresh_pending = .false.
594 total_steps = 0
595 energy%tot_old = 0.0_dp
596
597 scf_outer_loop: DO
598
599 CALL init_scf_loop(scf_env=scf_env, qs_env=qs_env, &
600 scf_section=scf_section, &
601 kp_ot_entry_reason=kp_ot_entry_reason)
602 kp_ot_entry_reason = kp_ot_entry_reuse
603 scf_env%adiis_shift = scf_control%diagonalization%adiis_shift
604
605 CALL qs_scf_set_loop_flags(scf_env, diis_step, &
606 energy_only, just_energy, exit_inner_loop)
607 IF (ot_kp_subspace_refresh_pending) THEN
608 scf_env%iter_count = ot_kp_subspace_refresh_iter_count
609 ot_kp_subspace_refresh_pending = .false.
610 END IF
611
612 ! decide whether to switch off dipole correction for convergence purposes
613 dft_control%surf_dip_correct_switch = dft_control%correct_surf_dip
614 IF ((dft_control%correct_surf_dip) .AND. (scf_control%outer_scf%have_scf) .AND. &
615 (scf_env%outer_scf%iter_count > floor(scf_control%outer_scf%max_scf/2.0_dp))) THEN
616 IF (dft_control%switch_surf_dip) THEN
617 dft_control%surf_dip_correct_switch = .false.
618 IF (output_unit > 0) WRITE (unit=output_unit, fmt="(/,/,T2,A)") &
619 "SURFACE DIPOLE CORRECTION switched off"
620 END IF
621 END IF
622
623 scf_loop: DO
624
625 CALL timeset(routinen//"_inner_loop", handle2)
626
627 IF (.NOT. just_energy) scf_env%iter_count = scf_env%iter_count + 1
628 iter_count = iter_count + 1
629 CALL cp_iterate(logger%iter_info, last=.false., iter_nr=iter_count)
630
631 IF (output_unit > 0) CALL m_flush(output_unit)
632
633 total_steps = total_steps + 1
634 just_energy = energy_only
635
636 CALL qs_ks_update_qs_env(qs_env, just_energy=just_energy, &
637 calculate_forces=.false.)
638
639 scf_env%raw_map_delta = 0.0_dp
640 scf_env%raw_map_delta_valid = .false.
641 ! print 'heavy weight' or relatively expensive quantities
642 CALL qs_scf_loop_print(qs_env, scf_env, para_env)
643
644 added_mos_auto_grow = .false.
645 ot_kp_subspace_refresh = .false.
646 adiis_validation = scf_control%diagonalization%update_method == diag_update_method_adiis .AND. &
647 scf_env%adiis_check_next
648 adiis_step_delta = scf_env%step_norm
649 scf_env%adiis_validated = .false.
650
651 IF (scf_control%diagonalization%update_method == diag_update_method_adiis) THEN
652 NULLIFY (subspace_density, subspace_fock)
653 IF (do_kpoints) THEN
654 IF (ALLOCATED(kpoints%lowdin_q)) THEN
655 subspace_density => kpoints%lowdin_q
656 subspace_fock => kpoints%lowdin_v
657 END IF
658 END IF
659 ! ADIIS is a Fock-space SCF method and never uses density mixing. A small
660 ! accelerated step is checked against the unmodified F[P] map before
661 ! convergence is accepted. The check detects an artificial subspace zero;
662 ! it is not a second, equally strict convergence criterion.
663 cpassert(ASSOCIATED(scf_env%scf_subspace_buffer))
664 scf_env%scf_subspace_buffer%last_restart = .false.
665 scf_env%scf_subspace_buffer%use_combined_fock = .false.
666 scf_env%scf_subspace_buffer%last_old_fock_weight = 0.0_dp
667 IF (.NOT. adiis_validation .AND. scf_env%iter_count > 1) THEN
668 ! Only accepted densities whose raw F[P] has just been evaluated enter history.
669 ! The initial guess and raw-validation trial endpoint are deliberately excluded.
670 ! A shifted candidate needs a current ADIIS fallback even if CDIIS was
671 ! active last time: its acceptance is decided later, during diagonalization.
672 IF (scf_control%diagonalization%adiis_shift > 0.0_dp .OR. &
673 scf_env%scf_subspace_buffer%diis_weight < 1.0_dp .OR. &
674 scf_env%iter_delta >= scf_control%eps_diis) THEN
675 CALL qs_scf_subspace_push(scf_env%scf_subspace_buffer, matrix_ks_kp, rho_ao_kp, &
676 energy%total, adiis_pushed, subspace_density, subspace_fock)
677 IF (.NOT. adiis_pushed) cpabort("Failed to append the accepted ADIIS SCF state")
678 CALL qs_scf_subspace_build(scf_env%scf_subspace_buffer)
679 IF (scf_control%diagonalization%adiis_shift > 0.0_dp) THEN
680 CALL qs_scf_subspace_update_shift(scf_env%scf_subspace_buffer, scf_env%adiis_shift)
681 END IF
682 END IF
683 END IF
684 IF (do_kpoints) THEN
685 CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, &
686 added_mos_auto_grow=added_mos_auto_grow)
687 ELSE
688 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
689 END IF
690
691 IF (adiis_validation) THEN
692 CALL qs_scf_candidate_density_delta(scf_env, rho, para_env, adiis_raw_delta, kpoints)
693 scf_env%raw_map_delta = adiis_raw_delta
694 scf_env%raw_map_delta_valid = .true.
695 IF (adiis_raw_delta < scf_control%eps_scf) THEN
696 scf_env%iter_delta = adiis_raw_delta
697 scf_env%iter_param = 0.0_dp
698 scf_env%iter_method = "ADIIS/Chk."
699 scf_env%adiis_validated = .true.
700 scf_env%adiis_check_next = .false.
701 ELSE
702 adiis_stagnated = adiis_step_delta <= adiis_stagnation_ratio*adiis_raw_delta
703 IF (adiis_stagnated) THEN
704 ! A nearly vanishing ADIIS step can be a fixed point of the current
705 ! subspace without being a fixed point of the raw SCF map. Discard
706 ! the stale subspace, retain the current paired P,F[P] state, and
707 ! accept the already computed raw candidate as the restart step.
708 ! This avoids duplicate history entries and a second diagonalization.
709 CALL qs_scf_subspace_restart(scf_env%scf_subspace_buffer, matrix_ks_kp, rho_ao_kp, &
710 energy%total, adiis_restarted, subspace_density, subspace_fock)
711 IF (.NOT. adiis_restarted) THEN
712 cpabort("Failed to restart the ADIIS history from the current SCF state")
713 END IF
714 scf_env%iter_delta = adiis_raw_delta
715 scf_env%iter_param = 0.0_dp
716 scf_env%iter_method = "ADIIS/Rst."
717 scf_env%adiis_check_next = .false.
718 ELSE
719 ! The raw map is not converged, but the accelerated step is not an
720 ! artificial subspace zero. Preserve the useful history, discard the
721 ! raw trial, and repeat the normal ADIIS step in this iteration.
722 CALL qs_scf_subspace_push(scf_env%scf_subspace_buffer, matrix_ks_kp, rho_ao_kp, &
723 energy%total, adiis_pushed, subspace_density, subspace_fock)
724 IF (.NOT. adiis_pushed) cpabort("Failed to append the accepted ADIIS SCF state")
725 CALL qs_scf_subspace_build(scf_env%scf_subspace_buffer)
726 IF (do_kpoints) THEN
727 CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, &
728 added_mos_auto_grow=added_mos_auto_grow)
729 ELSE
730 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
731 END IF
732 CALL qs_scf_candidate_density_delta(scf_env, rho, para_env, scf_env%iter_delta, kpoints)
733 scf_env%adiis_validated = scf_env%iter_delta < scf_control%eps_scf
734 IF (scf_env%adiis_validated .AND. .NOT. diis_step) THEN
735 scf_env%iter_param = 0.0_dp
736 scf_env%iter_method = "ADIIS/Chk."
737 END IF
738 ! A successful sanity check must not recursively demand another raw
739 ! endpoint check. A later, independently small step can trigger one.
740 scf_env%adiis_check_next = .false.
741 END IF
742 END IF
743 END IF
744
745 IF (.NOT. adiis_validation) THEN
746 CALL qs_scf_candidate_density_delta(scf_env, rho, para_env, scf_env%iter_delta, kpoints)
747 scf_env%adiis_check_next = .NOT. diis_step .AND. &
748 scf_env%iter_delta < scf_control%eps_scf
749 END IF
750 ELSE
751 ! Keep the existing density-mixing SCF path unchanged.
752 IF (do_kpoints) THEN
753 ! kpoints
754 IF (dft_control%hairy_probes .EQV. .true.) THEN
755 scf_control%smear%do_smear = .false.
756 CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, dft_control%probe, &
757 energy_only=energy_only)
758 ELSE
759 CALL qs_scf_new_mos_kp( &
760 qs_env, scf_env, scf_control, diis_step, &
761 ot_kp_subspace_refresh=ot_kp_subspace_refresh, &
762 allow_ot_kp_subspace_refresh= &
763 ot_kp_subspace_refresh_count == 0, &
764 allow_ot_kp_exit_refresh= &
765 ot_kp_subspace_refresh_count > 0 .AND. &
766 ot_kp_subspace_refresh_count < max_ot_kp_subspace_refreshes, &
767 accepted_ot_kp_searches=accepted_ot_kp_searches, &
768 added_mos_auto_grow=added_mos_auto_grow, energy_only=energy_only)
769 END IF
770 ELSE
771 ! Gamma points only
772 IF (dft_control%hairy_probes .EQV. .true.) THEN
773 scf_control%smear%do_smear = .false.
774 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only, &
775 dft_control%probe)
776 ELSE
777 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
778 END IF
779 END IF
780 END IF
781
782 IF (added_mos_auto_grow) THEN
783 CALL qs_scf_grow_added_mos_auto_kp(qs_env, scf_env, scf_control, output_unit)
784 kp_ot_entry_reason = kp_ot_entry_resized
785 total_steps = max(0, total_steps - 1)
786 iter_count = max(0, iter_count - 1)
787 IF (.NOT. just_energy) scf_env%iter_count = max(0, scf_env%iter_count - 1)
788 CALL timestop(handle2)
789 cycle scf_outer_loop
790 END IF
791
792 IF (ot_kp_subspace_refresh) THEN
793 IF (output_unit > 0) THEN
794 WRITE (unit=output_unit, fmt="(T2,A)") &
795 "K-point OT: rebuilding physical virtual subspace by full KS diagonalization."
796 END IF
797 CALL qs_kpoint_state_commit(qs_env, update_occupations=.false.)
798 IF (ASSOCIATED(scf_env%qs_ot_env)) THEN
799 DO ispin = 1, SIZE(scf_env%qs_ot_env)
800 CALL ot_scf_destroy(scf_env%qs_ot_env(ispin))
801 END DO
802 DEALLOCATE (scf_env%qs_ot_env)
803 NULLIFY (scf_env%qs_ot_env)
804 END IF
805 IF (ASSOCIATED(kpoints%scf_diis_buffer)) THEN
806 CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
807 END IF
808 reuse_precond = .false.
809 accepted_ot_kp_searches = 0
810 ot_kp_subspace_refresh_count = ot_kp_subspace_refresh_count + 1
811 total_steps = max(0, total_steps - 1)
812 iter_count = max(0, iter_count - 1)
813 IF (.NOT. just_energy) scf_env%iter_count = max(0, scf_env%iter_count - 1)
814 ot_kp_subspace_refresh_iter_count = scf_env%iter_count
815 ot_kp_subspace_refresh_pending = .true.
816 kp_ot_entry_reason = kp_ot_entry_refresh
817 CALL timestop(handle2)
818 cycle scf_outer_loop
819 END IF
820
821 IF (do_kpoints .AND. scf_env%method == ot_method_nr .AND. &
822 qs_scf_kp_search_endpoint(scf_env%iter_method)) THEN
823 accepted_ot_kp_searches = accepted_ot_kp_searches + 1
824 END IF
825
826 ! Print requested MO information (can be computationally expensive with OT)
827 CALL qs_scf_write_mos(qs_env, scf_env, final_mos=.false.)
828
829 IF (dft_control%qs_control%xtb_control%do_tblite) THEN
830 IF (scf_env%method == ot_method_nr) THEN
831 CALL tb_update_charges(qs_env, dft_control, qs_env%tb_tblite, .false., .true.)
832 CALL evaluate_core_matrix_traces(qs_env)
833 ELSE
834 cpassert(scf_env%mixing_method > 0)
835 CALL tb_update_charges(qs_env, dft_control, qs_env%tb_tblite, .false., .false.)
836 CALL evaluate_core_matrix_traces(qs_env, rho_ao_ext=scf_env%p_mix_new)
837 END IF
838 CALL tb_get_energy(qs_env, qs_env%tb_tblite, energy)
839 END IF
840
841 IF (scf_control%diagonalization%update_method == diag_update_method_adiis) THEN
842 CALL qs_scf_commit_density_candidate(scf_env, rho)
843 density_full_step = .true.
844 ELSE
845 density_full_step = diis_step .OR. tblite_native_mixer .OR. internal_tblite_density_full_step
846 CALL qs_scf_density_mixing(scf_env, rho, para_env, density_full_step, kpoints)
847 END IF
848 IF (dft_control%qs_control%xtb_control%do_tblite .AND. &
849 .NOT. (dft_control%qs_control%do_ls_scf .OR. scf_control%use_ot)) THEN
850 scf_env%iter_delta = max(scf_env%iter_delta, &
851 tb_scf_mixer_error(dft_control, qs_env%tb_tblite, &
852 scf_control%eps_scf))
853 END IF
854 IF (dft_control%qs_control%dftb .OR. &
855 (dft_control%qs_control%xtb .AND. .NOT. dft_control%qs_control%xtb_control%do_tblite)) THEN
856 scf_env%iter_delta = max(scf_env%iter_delta, &
857 charge_mixing_scc_error(scf_env%mixing_store, scf_control%eps_scf))
858 END IF
859 IF (tblite_native_mixer) THEN
860 scf_env%iter_param = dft_control%qs_control%xtb_control%tblite_mixer_damping
861 scf_env%iter_method = "TBLite/Diag"
862 ELSE IF (internal_tblite_mixer) THEN
863 scf_env%iter_method = "TBLite/Diag"
864 IF (dft_control%qs_control%dftb) THEN
865 scf_env%iter_param = dft_control%qs_control%dftb_control%tblite_mixer_damping
866 ELSE
867 scf_env%iter_param = dft_control%qs_control%xtb_control%tblite_mixer_damping
868 END IF
869 END IF
870 scf_env%step_norm = scf_env%iter_delta
871
872 t2 = m_walltime()
873
874 CALL rebuild_xtb_total_energy(energy, dft_control)
875 CALL qs_scf_loop_info(scf_env, output_unit, just_energy, t1, t2, energy, &
876 scf_control%diagonalization%adiis_verbose)
877
878 IF (scf_control%gce%do_gce) THEN
879 CALL qs_scf_gce_info(output_unit, qs_env, just_energy)
880 END IF
881
882 IF (.NOT. just_energy) energy%tot_old = energy%total
883
884 ! check for external communicator and if the intermediate energy should be sent
885 IF (scf_energy_message_tag > 0) THEN
886 CALL external_comm%send(energy%total, ext_master_id, scf_energy_message_tag)
887 END IF
888
889 CALL qs_scf_check_inner_exit(qs_env, scf_env, scf_control, should_stop, just_energy, &
890 exit_inner_loop, inner_loop_converged, output_unit, u_changed)
891 IF (u_changed) THEN
892 CALL qs_scf_inner_finalize(scf_env, qs_env, density_full_step, 0)
893 IF (scf_env%mixing_method > 1) THEN
894 CALL get_qs_env(qs_env, rho_atom_set=rho_atom)
895 CALL mixing_init(scf_env%mixing_method, rho, scf_env%mixing_store, para_env, &
896 rho_atom=rho_atom, auxiliary=kpoints%lowdin_q)
897 END IF
898 CALL init_scf_loop(scf_env, qs_env, scf_section, kp_ot_entry_reuse)
899 energy_only = .false.
900 just_energy = .false.
901 CALL timestop(handle2)
902 cycle scf_loop
903 END IF
904
905 ! In case we decide to exit we perform few more check to see if this one
906 ! is really the last SCF step
907 IF (exit_inner_loop) THEN
908
909 CALL qs_scf_inner_finalize(scf_env, qs_env, density_full_step, output_unit)
910
911 CALL qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, &
912 outer_loop_converged, exit_outer_loop)
913
914 ! Let's tag the last SCF cycle so we can print informations only of the last step
915 IF (exit_outer_loop) CALL cp_iterate(logger%iter_info, last=.true., iter_nr=iter_count)
916
917 END IF
918
919 IF (do_kpoints) THEN
920 CALL write_kpoints_restart(rho_ao_kp, kpoints, scf_env, dft_section, particle_set, qs_kind_set)
921 ELSE
922 IF (.NOT. dft_control%mtlr_dft_with_perturbation) THEN
923 ! Write wavefunction restart file
924 ! A low-spin ROKS state is not invariant under separate occupied-space
925 ! rotations of the two spins. Preserve its shared orbitals in GAPW restarts.
926 IF (scf_env%method == ot_method_nr .AND. &
927 .NOT. (dft_control%low_spin_roks .AND. dft_control%qs_control%gapw)) THEN
928 ! With OT: provide the Kohn-Sham matrix for the calculation of the MO eigenvalues
929 CALL get_ks_env(ks_env=ks_env, matrix_ks=matrix_ks)
930 CALL write_mo_set_to_restart(mos, particle_set, dft_section=dft_section, qs_kind_set=qs_kind_set, &
931 matrix_ks=matrix_ks)
932 ELSE
933 CALL write_mo_set_to_restart(mos, particle_set, dft_section=dft_section, qs_kind_set=qs_kind_set)
934 END IF
935 END IF
936 END IF
937
938 ! Exit if we have finished with the SCF inner loop
939 IF (exit_inner_loop) THEN
940 CALL timestop(handle2)
941 EXIT scf_loop
942 END IF
943
944 IF (.NOT. btest(cp_print_key_should_output(logger%iter_info, &
945 scf_section, "PRINT%ITERATION_INFO/TIME_CUMUL"), cp_p_file)) THEN
946 t1 = m_walltime()
947 END IF
948
949 ! mixing methods have the new density matrix in p_mix_new
950 IF (scf_env%mixing_method > 0) THEN
951 DO ic = 1, SIZE(rho_ao_kp, 2)
952 DO ispin = 1, dft_control%nspins
953 CALL dbcsr_get_info(rho_ao_kp(ispin, ic)%matrix, name=name) ! keep the name
954 CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
955 END DO
956 END DO
957 END IF
958
959 CALL qs_scf_rho_update(rho, qs_env, scf_env, ks_env, &
960 mix_rho=scf_env%mixing_method >= gspace_mixing_nr)
961
962 CALL timestop(handle2)
963
964 END DO scf_loop
965
966 IF (.NOT. scf_control%outer_scf%have_scf) EXIT scf_outer_loop
967
968 ! In case we use the OUTER SCF loop let's print some info..
969 CALL qs_scf_outer_loop_info(output_unit, scf_control, scf_env, &
970 energy, total_steps, should_stop, outer_loop_converged)
971
972 ! Save MOs to converged MOs if outer_loop_converged and surf_dip_correct_switch is true
973 IF (exit_outer_loop) THEN
974 IF ((dft_control%switch_surf_dip) .AND. (outer_loop_converged) .AND. &
975 (dft_control%surf_dip_correct_switch)) THEN
976 DO ispin = 1, dft_control%nspins
977 CALL reassign_allocated_mos(mos_last_converged(ispin), mos(ispin))
978 END DO
979 IF (output_unit > 0) WRITE (unit=output_unit, fmt="(/,/,T2,A)") &
980 "COPIED mos ---> mos_last_converged"
981 END IF
982 END IF
983
984 IF (exit_outer_loop) EXIT scf_outer_loop
985
986 !
987 CALL outer_loop_optimize(scf_env, scf_control)
988 CALL outer_loop_update_qs_env(qs_env, scf_env)
989 CALL qs_ks_did_change(ks_env, potential_changed=.true.)
990
991 END DO scf_outer_loop
992
993 converged = inner_loop_converged .AND. outer_loop_converged
994 total_scf_steps = total_steps
995
996 IF (dft_control%qs_control%cdft) THEN
997 dft_control%qs_control%cdft_control%total_steps = &
998 dft_control%qs_control%cdft_control%total_steps + total_steps
999 END IF
1000
1001 IF (.NOT. converged) THEN
1002 IF (scf_control%ignore_convergence_failure .OR. should_stop) THEN
1003 CALL cp_warn(__location__, "SCF run NOT converged")
1004 ELSE
1005 CALL cp_abort(__location__, &
1006 "SCF run NOT converged. To continue the calculation "// &
1007 "regardless, please set the keyword IGNORE_CONVERGENCE_FAILURE.")
1008 END IF
1009 END IF
1010
1011 ! Skip Harris functional calculation if ground-state is NOT converged
1012 IF (qs_env%energy_correction) THEN
1013 CALL get_qs_env(qs_env, ec_env=ec_env)
1014 ec_env%do_skip = .false.
1015 IF (ec_env%skip_ec .AND. .NOT. converged) ec_env%do_skip = .true.
1016 END IF
1017
1018 ! if needed copy mo_coeff dbcsr->fm for later use in post_scf!fm->dbcsr
1019 DO ispin = 1, SIZE(mos) !fm -> dbcsr
1020 IF (mos(ispin)%use_mo_coeff_b) THEN !fm->dbcsr
1021 IF (.NOT. ASSOCIATED(mos(ispin)%mo_coeff_b)) THEN
1022 !fm->dbcsr
1023 cpabort("mo_coeff_b is not allocated")
1024 END IF !fm->dbcsr
1025 CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, & !fm->dbcsr
1026 mos(ispin)%mo_coeff) !fm -> dbcsr
1027 END IF !fm->dbcsr
1028 END DO !fm -> dbcsr
1029
1030 CALL cp_rm_iter_level(logger, level_name="QS_SCF")
1031 CALL timestop(handle)
1032
1033 END SUBROUTINE scf_env_do_scf
1034
1035! **************************************************************************************************
1036!> \brief grow an automatic k-point smearing virtual space and rebuild dimensioned state
1037!> \param qs_env ...
1038!> \param scf_env ...
1039!> \param scf_control ...
1040!> \param output_unit ...
1041! **************************************************************************************************
1042 SUBROUTINE qs_scf_grow_added_mos_auto_kp(qs_env, scf_env, scf_control, output_unit)
1043
1044 TYPE(qs_environment_type), POINTER :: qs_env
1045 TYPE(qs_scf_env_type), POINTER :: scf_env
1046 TYPE(scf_control_type), POINTER :: scf_control
1047 INTEGER, INTENT(IN) :: output_unit
1048
1049 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_scf_grow_added_mos_auto_kp'
1050
1051 INTEGER :: base_nmo, common_base_nmo, current_added, current_target_nmo, grow_by, handle, &
1052 ic, ik, ispin, nmo_mat, nspins, old_nmo, target_nmo
1053 INTEGER, DIMENSION(2) :: base_nmo_spin, new_added, new_nmo
1054 LOGICAL :: diag_step, has_unit_metric, need_resize, &
1055 shared_spin_auto
1056 REAL(kind=dp) :: energy_step, flexible_electron_count, &
1057 maxocc, n_el_f
1058 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues, occupation_numbers, &
1059 old_eigenvalues, old_occupation_numbers
1060 TYPE(cp_blacs_env_type), POINTER :: blacs_env
1061 TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: ao_mo_fm_pools
1062 TYPE(cp_fm_type), POINTER :: mo_coeff
1063 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: mo_derivs
1064 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s
1065 TYPE(dft_control_type), POINTER :: dft_control
1066 TYPE(kpoint_env_type), POINTER :: kp
1067 TYPE(kpoint_type), POINTER :: kpoints
1068 TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:) :: old_mos
1069 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
1070 TYPE(mp_para_env_type), POINTER :: para_env
1071
1072 CALL timeset(routinen, handle)
1073
1074 NULLIFY (ao_mo_fm_pools, blacs_env, dft_control, eigenvalues, kpoints, matrix_ks, matrix_s, &
1075 mo_coeff, mo_derivs, mos, occupation_numbers, old_eigenvalues, &
1076 old_occupation_numbers, para_env)
1077
1078 CALL get_qs_env(qs_env=qs_env, blacs_env=blacs_env, dft_control=dft_control, &
1079 kpoints=kpoints, matrix_ks_kp=matrix_ks, matrix_s_kp=matrix_s, &
1080 mo_derivs=mo_derivs, mos=mos, para_env=para_env, &
1081 has_unit_metric=has_unit_metric)
1082
1083 cpassert(ASSOCIATED(mos))
1084 cpassert(ASSOCIATED(kpoints))
1085 cpassert(ASSOCIATED(dft_control))
1086 cpassert(ASSOCIATED(scf_control))
1087 cpassert(ASSOCIATED(matrix_ks))
1088 cpassert(ASSOCIATED(matrix_s))
1089
1090 nspins = dft_control%nspins
1091 cpassert(nspins >= 1 .AND. nspins <= SIZE(new_nmo))
1092 new_added = scf_control%added_mos
1093 new_nmo(:) = 0
1094 need_resize = .false.
1095 shared_spin_auto = nspins == 2 .AND. all(scf_control%added_mos_auto(1:2))
1096
1097 IF (shared_spin_auto) THEN
1098 DO ispin = 1, nspins
1099 current_added = max(0, scf_control%added_mos(ispin))
1100 base_nmo_spin(ispin) = max(0, mos(ispin)%nmo - current_added)
1101 END DO
1102 common_base_nmo = maxval(base_nmo_spin(1:nspins))
1103 current_target_nmo = maxval(mos(1:nspins)%nmo)
1104 current_added = max(0, current_target_nmo - common_base_nmo)
1105 grow_by = max(4, current_added)
1106 target_nmo = min(minval(mos(1:nspins)%nao), current_target_nmo + grow_by)
1107 DO ispin = 1, nspins
1108 new_nmo(ispin) = target_nmo
1109 new_added(ispin) = target_nmo - base_nmo_spin(ispin)
1110 need_resize = need_resize .OR. new_nmo(ispin) > mos(ispin)%nmo
1111 END DO
1112 ELSE
1113 DO ispin = 1, nspins
1114 old_nmo = mos(ispin)%nmo
1115 new_nmo(ispin) = old_nmo
1116 IF (.NOT. scf_control%added_mos_auto(ispin)) cycle
1117 current_added = max(0, scf_control%added_mos(ispin))
1118 base_nmo = max(0, old_nmo - current_added)
1119 grow_by = max(4, current_added)
1120 new_added(ispin) = min(mos(ispin)%nao - base_nmo, current_added + grow_by)
1121 new_nmo(ispin) = base_nmo + new_added(ispin)
1122 need_resize = need_resize .OR. new_nmo(ispin) > old_nmo
1123 END DO
1124
1125 IF (nspins == 2) THEN
1126 target_nmo = maxval(new_nmo(1:nspins))
1127 DO ispin = 1, nspins
1128 IF (target_nmo > mos(ispin)%nao) THEN
1129 CALL cp_abort(__location__, &
1130 "K-point ADDED_MOS AUTO exhausted the AO basis while matching spin bands.")
1131 END IF
1132 IF (target_nmo > mos(ispin)%nmo .AND. .NOT. scf_control%added_mos_auto(ispin)) THEN
1133 CALL cp_abort(__location__, &
1134 "K-point ADDED_MOS AUTO needs to grow a spin channel with explicit ADDED_MOS.")
1135 END IF
1136 base_nmo = max(0, mos(ispin)%nmo - max(0, scf_control%added_mos(ispin)))
1137 new_nmo(ispin) = target_nmo
1138 new_added(ispin) = target_nmo - base_nmo
1139 need_resize = need_resize .OR. new_nmo(ispin) > mos(ispin)%nmo
1140 END DO
1141 END IF
1142 END IF
1143
1144 IF (.NOT. need_resize) THEN
1145 CALL cp_abort(__location__, &
1146 "K-point ADDED_MOS AUTO cannot grow further although the highest band is still occupied.")
1147 END IF
1148
1149 scf_control%added_mos(1:nspins) = new_added(1:nspins)
1150 IF (output_unit > 0) THEN
1151 IF (nspins == 2) THEN
1152 WRITE (unit=output_unit, fmt="(T2,A,2I5)") &
1153 "K-point ADDED_MOS AUTO: growing virtual-space buffer to:", &
1154 scf_control%added_mos(1:nspins)
1155 ELSE
1156 WRITE (unit=output_unit, fmt="(T2,A,I0)") &
1157 "K-point ADDED_MOS AUTO: growing virtual-space buffer to: ", &
1158 scf_control%added_mos(1)
1159 END IF
1160 END IF
1161
1162 CALL scf_env_release_ot_operators(scf_env)
1163 ALLOCATE (old_mos(nspins))
1164 DO ispin = 1, nspins
1165 CALL duplicate_mo_set(old_mos(ispin), mos(ispin))
1166 CALL get_mo_set(old_mos(ispin), maxocc=maxocc, n_el_f=n_el_f, &
1167 flexible_electron_count=flexible_electron_count)
1168 CALL deallocate_mo_set(mos(ispin))
1169 CALL allocate_mo_set(mo_set=mos(ispin), nao=old_mos(ispin)%nao, nmo=new_nmo(ispin), &
1170 nelectron=old_mos(ispin)%nelectron, n_el_f=n_el_f, maxocc=maxocc, &
1171 flexible_electron_count=flexible_electron_count)
1172 mos(ispin)%use_mo_coeff_b = old_mos(ispin)%use_mo_coeff_b
1173 END DO
1174
1175 CALL mpools_rebuild_fm_pools(qs_env%mpools, mos=mos, blacs_env=blacs_env, para_env=para_env)
1176 CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
1177
1178 DO ispin = 1, nspins
1179 CALL init_mo_set(mos(ispin), fm_pool=ao_mo_fm_pools(ispin)%pool, &
1180 name="qs_env%mo"//trim(adjustl(cp_to_string(ispin))))
1181 CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, eigenvalues=eigenvalues, &
1182 occupation_numbers=occupation_numbers)
1183 CALL get_mo_set(old_mos(ispin), eigenvalues=old_eigenvalues, &
1184 occupation_numbers=old_occupation_numbers)
1185
1186 old_nmo = old_mos(ispin)%nmo
1187 CALL cp_fm_init_random(mo_coeff, mos(ispin)%nmo)
1188 IF (has_unit_metric) THEN
1189 CALL make_basis_simple(mo_coeff, mos(ispin)%nmo)
1190 ELSE
1191 CALL make_basis_sm(mo_coeff, mos(ispin)%nmo, matrix_s(1, 1)%matrix)
1192 END IF
1193
1194 eigenvalues(1:old_nmo) = old_eigenvalues(1:old_nmo)
1195 occupation_numbers(:) = 0.0_dp
1196 occupation_numbers(1:old_nmo) = old_occupation_numbers(1:old_nmo)
1197 IF (mos(ispin)%nmo > old_nmo) THEN
1198 energy_step = max(scf_control%smear%electronic_temperature, 1.0e-3_dp)
1199 DO ic = old_nmo + 1, mos(ispin)%nmo
1200 eigenvalues(ic) = eigenvalues(old_nmo) + energy_step*real(ic - old_nmo, kind=dp)
1201 END DO
1202 END IF
1203 mos(ispin)%homo = old_mos(ispin)%homo
1204 mos(ispin)%lfomo = old_mos(ispin)%lfomo
1205 mos(ispin)%kTS = old_mos(ispin)%kTS
1206 mos(ispin)%mu = old_mos(ispin)%mu
1207 mos(ispin)%uniform_occupation = old_mos(ispin)%uniform_occupation
1208 END DO
1209
1210 IF (dft_control%restricted) CALL mo_set_restrict(mos)
1211
1212 DO ispin = 1, nspins
1213 IF (.NOT. mos(ispin)%use_mo_coeff_b) cycle
1214 CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
1215 CALL dbcsr_init_p(mos(ispin)%mo_coeff_b)
1216 CALL cp_dbcsr_m_by_n_from_row_template(mos(ispin)%mo_coeff_b, template=matrix_s(1, 1)%matrix, &
1217 n=mos(ispin)%nmo, sym=dbcsr_type_no_symmetry)
1218 CALL copy_fm_to_dbcsr(mo_coeff, mos(ispin)%mo_coeff_b)
1219 END DO
1220
1221 IF (ASSOCIATED(mo_derivs)) THEN
1222 DO ispin = 1, SIZE(mo_derivs)
1223 IF (ASSOCIATED(mo_derivs(ispin)%matrix)) CALL dbcsr_release_p(mo_derivs(ispin)%matrix)
1224 END DO
1225 DEALLOCATE (mo_derivs)
1226 NULLIFY (mo_derivs)
1227 CALL set_qs_env(qs_env, mo_derivs=mo_derivs)
1228 END IF
1229 IF (qs_env%requires_mo_derivs) THEN
1230 nmo_mat = merge(1, nspins, dft_control%restricted)
1231 ALLOCATE (mo_derivs(nmo_mat))
1232 DO ispin = 1, nmo_mat
1233 NULLIFY (mo_derivs(ispin)%matrix)
1234 CALL dbcsr_init_p(mo_derivs(ispin)%matrix)
1235 CALL dbcsr_create(mo_derivs(ispin)%matrix, template=mos(ispin)%mo_coeff_b, &
1236 name="mo_derivs", matrix_type=dbcsr_type_no_symmetry)
1237 END DO
1238 CALL set_qs_env(qs_env, mo_derivs=mo_derivs)
1239 END IF
1240
1241 DO ik = 1, SIZE(kpoints%kp_env)
1242 kp => kpoints%kp_env(ik)%kpoint_env
1243 IF (ASSOCIATED(kp%mos)) THEN
1244 DO ispin = 1, SIZE(kp%mos)
1245 CALL deallocate_mo_set(kp%mos(ispin))
1246 END DO
1247 DEALLOCATE (kp%mos)
1248 NULLIFY (kp%mos)
1249 END IF
1250 CALL cp_fm_release(kp%pmat)
1251 CALL cp_fm_release(kp%wmat)
1252 END DO
1253 CALL mpools_release(kpoints%mpools)
1254 CALL kpoint_initialize_mos(kpoints, mos)
1255 CALL kpoint_initialize_mo_set(kpoints)
1256
1257 diag_step = .false.
1258 CALL do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, .false., diag_step)
1259 IF (dft_control%restricted) CALL qs_kpoint_copy_spin_mos(kpoints, nspins)
1261 qs_env, update_occupations=.true., &
1262 separate_spin_occupations=dft_control%restricted, &
1263 fixed_occupations=.NOT. (dft_control%smear .OR. scf_control%smear%do_smear))
1264
1265 IF (ASSOCIATED(kpoints%scf_diis_buffer)) CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
1266 IF (ASSOCIATED(scf_env%qs_ot_env)) THEN
1267 DO ispin = 1, SIZE(scf_env%qs_ot_env)
1268 CALL ot_scf_destroy(scf_env%qs_ot_env(ispin))
1269 END DO
1270 DEALLOCATE (scf_env%qs_ot_env)
1271 NULLIFY (scf_env%qs_ot_env)
1272 END IF
1273 reuse_precond = .false.
1274
1275 DO ispin = 1, nspins
1276 CALL deallocate_mo_set(old_mos(ispin))
1277 END DO
1278 DEALLOCATE (old_mos)
1279
1280 CALL timestop(handle)
1281
1282 END SUBROUTINE qs_scf_grow_added_mos_auto_kp
1283
1284! **************************************************************************************************
1285!> \brief inits those objects needed if you want to restart the scf with, say
1286!> only a new initial guess, or different density functional or ...
1287!> this will happen just before the scf loop starts
1288!> \param scf_env ...
1289!> \param qs_env ...
1290!> \param scf_section ...
1291!> \param kp_ot_entry_reason reason for constructing a new k-point OT workspace
1292!> \par History
1293!> 03.2006 created [Joost VandeVondele]
1294! **************************************************************************************************
1295 SUBROUTINE init_scf_loop(scf_env, qs_env, scf_section, kp_ot_entry_reason)
1296
1297 TYPE(qs_scf_env_type), POINTER :: scf_env
1298 TYPE(qs_environment_type), POINTER :: qs_env
1299 TYPE(section_vals_type), POINTER :: scf_section
1300 INTEGER, INTENT(IN), OPTIONAL :: kp_ot_entry_reason
1301
1302 CHARACTER(LEN=*), PARAMETER :: routinen = 'init_scf_loop'
1303
1304 INTEGER :: entry_reason, handle, ikind, ispin, &
1305 nkpoint, nmo, nspin_ot
1306 INTEGER, DIMENSION(2) :: kp_range
1307 LOGICAL :: do_adiis, do_kpoints, do_rotation, fixed_density_prepared, has_unit_metric, &
1308 kp_diis_step, kpoint_mos_initialized
1309 REAL(kind=dp) :: u_ramping
1310 REAL(kind=dp), DIMENSION(:), POINTER :: wkp
1311 TYPE(cp_fm_type), POINTER :: mo_coeff
1312 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
1313 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp, matrix_t_kp, &
1314 rho_ao_kp
1315 TYPE(dbcsr_type), POINTER :: orthogonality_metric
1316 TYPE(dft_control_type), POINTER :: dft_control
1317 TYPE(kpoint_type), POINTER :: kpoints
1318 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
1319 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
1320 TYPE(qs_rho_type), POINTER :: rho
1321 TYPE(scf_control_type), POINTER :: scf_control
1322
1323 CALL timeset(routinen, handle)
1324
1325 NULLIFY (scf_control, matrix_ks_kp, matrix_s, matrix_s_kp, matrix_t_kp, matrix_ks, &
1326 rho_ao_kp, dft_control, mos, mo_coeff, kpoints, qs_kind_set, rho, wkp)
1327
1328 cpassert(ASSOCIATED(scf_env))
1329 cpassert(ASSOCIATED(qs_env))
1330 entry_reason = kp_ot_entry_initial
1331 IF (PRESENT(kp_ot_entry_reason)) entry_reason = kp_ot_entry_reason
1332 fixed_density_prepared = .false.
1333
1334 CALL get_qs_env(qs_env=qs_env, &
1335 scf_control=scf_control, &
1336 dft_control=dft_control, &
1337 do_kpoints=do_kpoints, &
1338 kpoints=kpoints, &
1339 mos=mos, &
1340 rho=rho, &
1341 qs_kind_set=qs_kind_set)
1342
1343 nkpoint = 1
1344 kp_range = 0
1345 nspin_ot = dft_control%nspins
1346 IF (dft_control%restricted) nspin_ot = 1
1347 IF (do_kpoints) THEN
1348 CALL get_kpoint_info(kpoints, nkp=nkpoint, kp_range=kp_range, wkp=wkp)
1349 END IF
1350
1351 ! if using mo_coeff_b then copy to fm
1352 DO ispin = 1, SIZE(mos) !fm->dbcsr
1353 IF (mos(1)%use_mo_coeff_b) THEN !fm->dbcsr
1354 CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, mos(ispin)%mo_coeff) !fm->dbcsr
1355 END IF !fm->dbcsr
1356 END DO !fm->dbcsr
1357
1358 ! this just guarantees that all mo_occupations match the eigenvalues, if smear
1359 DO ispin = 1, dft_control%nspins
1360 ! do not reset mo_occupations if the maximum overlap method is in use
1361 IF (.NOT. scf_control%diagonalization%mom) THEN
1362 !if the hair probes section is present, this sends hairy_probes to set_mo_occupation subroutine
1363 !and switches off the standard smearing
1364 IF (dft_control%hairy_probes .EQV. .true.) THEN
1365 IF (scf_env%outer_scf%iter_count > 0) THEN
1366 scf_control%smear%do_smear = .false.
1367 CALL set_mo_occupation(mo_set=mos(ispin), &
1368 smear=scf_control%smear, &
1369 probe=dft_control%probe)
1370 END IF
1371 ELSE
1372 IF (.NOT. scf_control%gce%do_gce) THEN
1373 CALL set_mo_occupation(mo_set=mos(ispin), &
1374 smear=scf_control%smear, &
1375 emit_warnings=.NOT. do_kpoints)
1376 ELSE
1377 CALL set_mo_occupation(mo_set=mos(ispin), &
1378 smear=scf_control%smear, &
1379 gce=scf_control%gce, &
1380 emit_warnings=.NOT. do_kpoints)
1381 END IF
1382 END IF
1383 END IF
1384 END DO
1385
1386 do_adiis = scf_control%diagonalization%update_method == diag_update_method_adiis
1387 IF (do_adiis) THEN
1388 IF (scf_env%method /= general_diag_method_nr) THEN
1389 cpabort("ADIIS currently requires SCF%DIAGONALIZATION ALGORITHM STANDARD")
1390 END IF
1391 IF (scf_env%cholesky_method == cholesky_dbcsr) THEN
1392 cpabort("ADIIS is not yet compatible with CHOLESKY INVERSE_DBCSR")
1393 END IF
1394 IF (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
1395 dft_control%qs_control%semi_empirical) THEN
1396 cpabort("ADIIS currently requires a Quickstep DFT KS matrix")
1397 END IF
1398 IF (dft_control%do_admm_mo) THEN
1399 cpabort("ADIIS is not yet compatible with ADMM-MO")
1400 END IF
1401 IF (dft_control%sic_method_id == sic_eo) THEN
1402 cpabort("ADIIS is not yet compatible with explicit-orbital SIC")
1403 END IF
1404 IF (dft_control%apply_period_efield) THEN
1405 cpabort("ADIIS is not yet compatible with PERIODIC_EFIELD")
1406 END IF
1407 IF (dft_control%dft_plus_u) THEN
1408 cpassert(ASSOCIATED(qs_kind_set))
1409 DO ikind = 1, SIZE(qs_kind_set)
1410 CALL get_qs_kind(qs_kind_set(ikind), u_ramping=u_ramping)
1411 IF (u_ramping > 0.0_dp .AND. &
1412 (.NOT. do_kpoints .OR. dft_control%plus_u_method_id /= plus_u_lowdin)) THEN
1413 cpabort("ADIIS is not yet compatible with DFT+U U_RAMPING")
1414 END IF
1415 END DO
1416 END IF
1417 IF (dft_control%smear .OR. scf_control%smear%do_smear) THEN
1418 cpabort("ADIIS finite-temperature smearing support is not implemented yet")
1419 END IF
1420 IF (dft_control%roks .OR. scf_control%diagonalization%mom .OR. &
1421 dft_control%hairy_probes .OR. scf_control%gce%do_gce) THEN
1422 cpabort("ADIIS currently supports ordinary RKS/UKS occupations only")
1423 END IF
1424 IF (scf_control%do_diag_sub) THEN
1425 cpabort("ADIIS is not yet compatible with DIAG_SUB_SCF")
1426 END IF
1427 IF (scf_control%diagonalization%max_history < 1) THEN
1428 cpabort("ADIIS requires SCF%ADIIS%MAX_HISTORY >= 1")
1429 END IF
1430 IF (.NOT. ASSOCIATED(scf_env%scf_subspace_buffer)) THEN
1431 ALLOCATE (scf_env%scf_subspace_buffer)
1432 CALL qs_scf_subspace_buffer_create(scf_env%scf_subspace_buffer, &
1433 scf_control%diagonalization%max_history)
1434 ELSE IF (scf_env%scf_subspace_buffer%nbuffer /= scf_control%diagonalization%max_history) THEN
1435 CALL qs_scf_subspace_buffer_release(scf_env%scf_subspace_buffer)
1436 CALL qs_scf_subspace_buffer_create(scf_env%scf_subspace_buffer, &
1437 scf_control%diagonalization%max_history)
1438 END IF
1439 CALL qs_scf_subspace_buffer_clear(scf_env%scf_subspace_buffer)
1440 END IF
1441
1442 SELECT CASE (scf_env%method)
1443 CASE DEFAULT
1444
1445 CALL cp_abort(__location__, &
1446 "Unknown SCF method <"//trim(cp_to_string(scf_env%method))//"> found. Check the code!")
1447
1449
1450 IF (.NOT. scf_env%skip_diis) THEN
1451 IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
1452 ALLOCATE (scf_env%scf_diis_buffer)
1453 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
1454 END IF
1455 CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
1456 END IF
1457
1459 IF (.NOT. scf_env%skip_diis) THEN
1460 IF (do_kpoints) THEN
1461 IF (.NOT. ASSOCIATED(kpoints%scf_diis_buffer)) THEN
1462 ALLOCATE (kpoints%scf_diis_buffer)
1463 CALL qs_diis_b_create_kp(kpoints%scf_diis_buffer, nbuffer=scf_control%max_diis)
1464 END IF
1465 CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
1466 ELSE
1467 IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
1468 ALLOCATE (scf_env%scf_diis_buffer)
1469 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
1470 END IF
1471 CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
1472 END IF
1473 END IF
1474
1475 CASE (ot_diag_method_nr)
1476 IF (scf_control%diagonalization%ot_settings%preconditioner_type == &
1478 (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
1479 dft_control%qs_control%semi_empirical)) THEN
1480 CALL cp_warn(__location__, &
1481 "FULL_KINETIC is unavailable for semi-empirical DIAGONALIZATION%OT; "// &
1482 "falling back to FULL_S_INVERSE.")
1483 scf_control%diagonalization%ot_settings%preconditioner_type = ot_precond_s_inverse
1484 scf_control%diagonalization%ot_settings%preconditioner_name = "FULL_S_INVERSE"
1485 END IF
1486 IF (do_kpoints) THEN
1487 IF (.NOT. scf_env%skip_diis) THEN
1488 IF (.NOT. ASSOCIATED(kpoints%scf_diis_buffer)) THEN
1489 ALLOCATE (kpoints%scf_diis_buffer)
1490 CALL qs_diis_b_create_kp(kpoints%scf_diis_buffer, nbuffer=scf_control%max_diis)
1491 END IF
1492 CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
1493 END IF
1494 CALL timestop(handle)
1495 RETURN
1496 END IF
1497 CALL get_qs_env(qs_env, matrix_ks=matrix_ks, matrix_s=matrix_s)
1498
1499 IF (.NOT. scf_env%skip_diis) THEN
1500 IF (.NOT. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
1501 ALLOCATE (scf_env%scf_diis_buffer)
1502 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
1503 END IF
1504 CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
1505 END IF
1506
1507 ! if an old preconditioner is still around (i.e. outer SCF is active),
1508 ! remove it if this could be worthwhile
1509 CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
1510 scf_control%diagonalization%ot_settings%preconditioner_type, &
1511 dft_control%nspins)
1512
1513 CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
1514 scf_control%diagonalization%ot_settings%preconditioner_type, &
1515 scf_control%diagonalization%ot_settings%precond_solver_type, &
1516 scf_control%diagonalization%ot_settings%energy_gap, dft_control%nspins, &
1517 chebyshev_degree=scf_control%diagonalization%ot_settings%chebyshev_degree, &
1518 low_rank_base=scf_control%diagonalization%ot_settings%low_rank_base, &
1519 fermi_low_rank_max_rank= &
1520 scf_control%diagonalization%ot_settings%fermi_low_rank_max_rank, &
1521 lattice_fft=scf_control%diagonalization%ot_settings%lattice_fft, &
1522 lattice_fft_local_cells= &
1523 scf_control%diagonalization%ot_settings%lattice_fft_local_cells)
1524
1526 ! Preconditioner initialized within the loop, when required
1527 CASE (ot_method_nr)
1528 IF (do_kpoints) THEN
1529 IF (.NOT. ASSOCIATED(scf_env%qs_ot_env)) THEN
1530 CALL get_qs_env(qs_env, matrix_s_kp=matrix_s_kp, &
1531 kinetic_kp=matrix_t_kp)
1532 cpassert(ASSOCIATED(matrix_s_kp))
1533 CALL allocate_qs_ot_envs(scf_env=scf_env, &
1534 scf_control=scf_control, &
1535 dft_control=dft_control, &
1536 scf_section=scf_section, &
1537 do_kpoints=do_kpoints, &
1538 nkpoint=nkpoint, &
1539 nspin_ot=nspin_ot, &
1540 kp_range=kp_range, &
1541 wkp=wkp)
1542
1543 CALL scf_env_release_ot_operators(scf_env)
1544 ALLOCATE (scf_env%ot_operator_ctx)
1545 CALL kpoint_operator_context_create(scf_env%ot_operator_ctx, kpoints, &
1546 scf_env%scf_work1(1), dft_control%nspins)
1547
1548 ! A previous zero-width filling may have changed the occupied rank at each k point.
1549 ! Restore the fixed OT ranks before validating or reconstructing the active space.
1550 IF (.NOT. scf_env%qs_ot_env(1)%settings%do_ener) THEN
1552 END IF
1553 kpoint_mos_initialized = qs_kpoint_mos_initialized( &
1554 kpoints, require_full_space=scf_env%qs_ot_env(1)%settings%do_ener)
1555 SELECT CASE (entry_reason)
1556 CASE (kp_ot_entry_initial)
1557 IF (kpoint_mos_initialized) THEN
1558 IF (dft_control%restricted) THEN
1559 CALL qs_kpoint_copy_spin_mos(kpoints, dft_control%nspins)
1560 END IF
1562 qs_env, update_occupations=.true., &
1563 separate_spin_occupations=dft_control%restricted, &
1564 fixed_occupations=.NOT. (dft_control%smear .OR. scf_control%smear%do_smear))
1565 END IF
1566 CASE (kp_ot_entry_reuse, kp_ot_entry_resized, kp_ot_entry_refresh)
1567 cpassert(kpoint_mos_initialized)
1568 CASE DEFAULT
1569 cpabort("Invalid k-point OT workspace entry reason.")
1570 END SELECT
1571
1572 ! For fixed occupations, reconstruct the occupied subspace directly from the
1573 ! density-only guess. This preserves its physical content instead of replacing it
1574 ! with the potentially remote ground-state projector of H[P].
1575 IF (entry_reason == kp_ot_entry_initial .AND. .NOT. kpoint_mos_initialized .AND. &
1576 .NOT. scf_env%qs_ot_env(1)%settings%do_ener) THEN
1577 cpassert(ASSOCIATED(rho))
1578 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
1579 cpassert(ASSOCIATED(rho_ao_kp))
1580 CALL kpoint_operator_store(scf_env%ot_operator_ctx, rho_ao_kp, matrix_s_kp)
1582 IF (SIZE(rho_ao_kp, 1) < nspin_ot) THEN
1583 cpassert(SIZE(rho_ao_kp, 1) == 1)
1584 CALL qs_kpoint_copy_spin_mos(kpoints, nspin_ot)
1585 END IF
1586 IF (dft_control%restricted) THEN
1587 CALL qs_kpoint_copy_spin_mos(kpoints, dft_control%nspins)
1588 END IF
1590 qs_env, update_occupations=.true., &
1591 separate_spin_occupations=dft_control%restricted, fixed_occupations=.true.)
1592
1593 ! The reconstructed projector replaces the density-only input. Rebuild H and E
1594 ! so the first minimizer call sees one coherent physical electronic state.
1595 CALL qs_ks_update_qs_env(qs_env, just_energy=.false., calculate_forces=.false.)
1596 fixed_density_prepared = .true.
1597
1598 ELSE
1599 ! Build the physical Hamiltonian from the committed state or a Mermin
1600 ! density-only guess. Ordinary workspace re-entry only needs this rebuild.
1601 CALL qs_ks_update_qs_env(qs_env, just_energy=.false., calculate_forces=.false.)
1602 CALL get_qs_env(qs_env, matrix_ks_kp=matrix_ks_kp)
1603 cpassert(ASSOCIATED(matrix_ks_kp))
1604
1605 ! Mermin initialization and deliberate REF refreshes require a full orbital
1606 ! space, so use the physical Hamiltonian replacement for those cases only.
1607 IF ((entry_reason == kp_ot_entry_initial .AND. .NOT. kpoint_mos_initialized) .OR. &
1608 entry_reason == kp_ot_entry_refresh) THEN
1609 kp_diis_step = .false.
1610 CALL do_general_diag_kp(matrix_ks_kp, matrix_s_kp, kpoints, scf_env, &
1611 scf_control, .false., kp_diis_step)
1612 IF (SIZE(matrix_ks_kp, 1) < nspin_ot) THEN
1613 cpassert(SIZE(matrix_ks_kp, 1) == 1)
1614 CALL qs_kpoint_copy_spin_mos(kpoints, nspin_ot)
1615 END IF
1616 IF (dft_control%restricted) THEN
1617 CALL qs_kpoint_copy_spin_mos(kpoints, dft_control%nspins)
1618 END IF
1620 qs_env, update_occupations=.true., &
1621 separate_spin_occupations=dft_control%restricted, &
1622 fixed_occupations=.NOT. (dft_control%smear .OR. scf_control%smear%do_smear))
1623
1624 ! The accepted projector replaced the density-only input. Rebuild H and E so
1625 ! the first minimizer call sees one coherent physical electronic state.
1626 CALL qs_ks_update_qs_env(qs_env, just_energy=.false., &
1627 calculate_forces=.false.)
1628 END IF
1629 END IF
1630
1631 CALL get_qs_env(qs_env, matrix_ks_kp=matrix_ks_kp)
1632 cpassert(ASSOCIATED(matrix_ks_kp))
1633
1634 ! Redistribute the physical operators to their owning k-point groups. OT must not
1635 ! call the collective real-space transform independently with group-local k values.
1636 IF (scf_env%qs_ot_env(1)%settings%preconditioner_type == ot_precond_full_kinetic) THEN
1637 CALL kpoint_operator_store(scf_env%ot_operator_ctx, matrix_ks_kp, matrix_s_kp, matrix_t_kp)
1638 ELSE
1639 CALL kpoint_operator_store(scf_env%ot_operator_ctx, matrix_ks_kp, matrix_s_kp)
1640 END IF
1641 scf_env%ot_ks_valid = .true.
1642
1643 ! Natural orbitals define the fixed occupied projector but not its internal gauge.
1644 ! Canonicalize that subspace with the matching physical Hamiltonian and populate
1645 ! the MO energy labels before constructing H-dependent OT preconditioners.
1646 IF (fixed_density_prepared) CALL qs_kpoint_state_canonicalize_fixed(kpoints)
1647
1648 CALL allocate_qs_ot_kpoint_state(qs_env=qs_env, &
1649 scf_env=scf_env, &
1650 dft_control=dft_control, &
1651 kpoints=kpoints, &
1652 matrix_ks_kp=matrix_ks_kp, &
1653 matrix_s_kp=matrix_s_kp, &
1654 matrix_t_kp=matrix_t_kp, &
1655 nspin_ot=nspin_ot)
1656 END IF
1657
1658 CALL timestop(handle)
1659 RETURN
1660 END IF
1661
1662 CALL get_qs_env(qs_env, &
1663 has_unit_metric=has_unit_metric, &
1664 matrix_s=matrix_s, &
1665 matrix_ks=matrix_ks)
1666
1667 ! reortho the wavefunctions if we are having an outer scf and
1668 ! this is not the first iteration
1669 ! this is useful to avoid the build-up of numerical noise
1670 ! however, we can not play this trick if restricted (don't mix non-equivalent orbs)
1671 IF (scf_control%do_outer_scf_reortho) THEN
1672 IF (scf_control%outer_scf%have_scf .AND. .NOT. dft_control%restricted) THEN
1673 IF (scf_env%outer_scf%iter_count > 0) THEN
1674 DO ispin = 1, dft_control%nspins
1675 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1676 IF (has_unit_metric) THEN
1677 CALL make_basis_simple(mo_coeff, nmo)
1678 ELSE
1679 CALL make_basis_sm(mo_coeff, nmo, matrix_s(1)%matrix)
1680 END IF
1681 END DO
1682 END IF
1683 END IF
1684 ELSE
1685 ! dont need any dirty trick for the numerically stable irac algorithm.
1686 END IF
1687
1688 IF (.NOT. ASSOCIATED(scf_env%qs_ot_env)) THEN
1689
1690 CALL allocate_qs_ot_envs(scf_env=scf_env, &
1691 scf_control=scf_control, &
1692 dft_control=dft_control, &
1693 scf_section=scf_section, &
1694 do_kpoints=do_kpoints, &
1695 nkpoint=nkpoint, &
1696 nspin_ot=nspin_ot, &
1697 kp_range=kp_range, &
1698 wkp=wkp)
1699
1700 IF (scf_env%qs_ot_env(1)%settings%preconditioner_type == &
1702 IF (.NOT. do_kpoints) THEN
1703 DO ispin = 1, SIZE(mos)
1704 IF (.NOT. mos(ispin)%uniform_occupation) THEN
1705 CALL cp_abort(__location__, &
1706 'PRECONDITIONER FULL_ALL_COVARIANT currently requires uniform occupations.')
1707 END IF
1708 END DO
1709 END IF
1710 END IF
1711
1712 ! might need the KS matrix to init properly
1713 CALL qs_ks_update_qs_env(qs_env, just_energy=.false., &
1714 calculate_forces=.false.)
1715
1716 ! if an old preconditioner is still around (i.e. outer SCF is active),
1717 ! remove it if this could be worthwhile
1718 IF (.NOT. reuse_precond) THEN
1719 CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
1720 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
1721 dft_control%nspins)
1722 END IF
1723
1724 !
1725 ! preconditioning still needs to be done correctly with has_unit_metric
1726 ! notice that a big part of the preconditioning (S^-1) is fine anyhow
1727 !
1728 IF (has_unit_metric) THEN
1729 NULLIFY (orthogonality_metric)
1730 ELSE
1731 orthogonality_metric => matrix_s(1)%matrix
1732 END IF
1733
1734 IF (.NOT. reuse_precond) THEN
1735 CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
1736 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
1737 scf_env%qs_ot_env(1)%settings%precond_solver_type, &
1738 scf_env%qs_ot_env(1)%settings%energy_gap, dft_control%nspins, &
1739 has_unit_metric=has_unit_metric, &
1740 chol_type=scf_env%qs_ot_env(1)%settings%cholesky_type, &
1741 chebyshev_degree=scf_env%qs_ot_env(1)%settings%chebyshev_degree, &
1742 low_rank_base=scf_env%qs_ot_env(1)%settings%low_rank_base, &
1743 fermi_low_rank_max_rank= &
1744 scf_env%qs_ot_env(1)%settings%fermi_low_rank_max_rank, &
1745 lattice_fft=scf_env%qs_ot_env(1)%settings%lattice_fft, &
1746 lattice_fft_local_cells= &
1747 scf_env%qs_ot_env(1)%settings%lattice_fft_local_cells)
1748 END IF
1749 IF (reuse_precond) reuse_precond = .false.
1750
1751 CALL ot_scf_init(mo_array=mos, matrix_s=orthogonality_metric, &
1752 broyden_adaptive_sigma=qs_env%broyden_adaptive_sigma, &
1753 qs_ot_env=scf_env%qs_ot_env, matrix_ks=matrix_ks(1)%matrix)
1754
1755 SELECT CASE (scf_env%qs_ot_env(1)%settings%preconditioner_type)
1756 CASE (ot_precond_none)
1759 DO ispin = 1, SIZE(scf_env%qs_ot_env)
1760 CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
1761 scf_env%ot_preconditioner(ispin)%preconditioner)
1762 END DO
1764 DO ispin = 1, SIZE(scf_env%qs_ot_env)
1765 CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
1766 scf_env%ot_preconditioner(1)%preconditioner)
1767 END DO
1768 CASE DEFAULT
1769 DO ispin = 1, SIZE(scf_env%qs_ot_env)
1770 CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ispin), &
1771 scf_env%ot_preconditioner(1)%preconditioner)
1772 END DO
1773 END SELECT
1774 END IF
1775
1776 ! if we have non-uniform occupations we should be using rotation
1777 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
1778 DO ispin = 1, SIZE(mos)
1779 IF (.NOT. mos(ispin)%uniform_occupation) THEN
1780 cpassert(do_rotation)
1781 END IF
1782 END DO
1783 END SELECT
1784
1785 ! another safety check
1786 IF (dft_control%low_spin_roks) THEN
1787 cpassert(scf_env%method == ot_method_nr)
1788 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
1789 cpassert(do_rotation)
1790 END IF
1791
1792 CALL timestop(handle)
1793
1794 END SUBROUTINE init_scf_loop
1795
1796! **************************************************************************************************
1797!> \brief allocate OT environments and label their spin/k-point channel identity
1798!> \param scf_env ...
1799!> \param scf_control ...
1800!> \param dft_control ...
1801!> \param scf_section ...
1802!> \param do_kpoints ...
1803!> \param nkpoint ...
1804!> \param nspin_ot ...
1805!> \param kp_range ...
1806!> \param wkp ...
1807! **************************************************************************************************
1808 SUBROUTINE allocate_qs_ot_envs(scf_env, scf_control, dft_control, scf_section, &
1809 do_kpoints, nkpoint, nspin_ot, kp_range, wkp)
1810 TYPE(qs_scf_env_type), POINTER :: scf_env
1811 TYPE(scf_control_type), POINTER :: scf_control
1812 TYPE(dft_control_type), POINTER :: dft_control
1813 TYPE(section_vals_type), POINTER :: scf_section
1814 LOGICAL, INTENT(IN) :: do_kpoints
1815 INTEGER, INTENT(IN) :: nkpoint, nspin_ot
1816 INTEGER, DIMENSION(2), INTENT(IN) :: kp_range
1817 REAL(kind=dp), DIMENSION(:), POINTER :: wkp
1818
1819 CHARACTER(len=*), PARAMETER :: kpoint_precond_error = &
1820 "Complex K-point OT supports PRECONDITIONER NONE, FERMI_LOW_RANK, "// &
1821 "FULL_S_INVERSE, FULL_KINETIC, FULL_SINGLE, FULL_SINGLE_INVERSE, FULL_ALL, or "// &
1822 "FULL_ALL_COVARIANT.", kpoint_solver_error = &
1823 "Complex K-point FERMI_LOW_RANK, FULL_SINGLE, FULL_ALL, and FULL_ALL_COVARIANT "// &
1824 "support PRECOND_SOLVER DEFAULT; FULL_S_INVERSE, FULL_KINETIC, and "// &
1825 "FULL_SINGLE_INVERSE support DEFAULT or INVERSE_CHOLESKY."
1826
1827 INTEGER :: ikpoint, ispin, local_kpoint, &
1828 number_of_ot_envs, ot_channel
1829 LOGICAL :: do_rotation, do_smear, is_full_all
1830 REAL(kind=dp) :: kpoint_weight
1831
1832 cpassert(.NOT. ASSOCIATED(scf_env%qs_ot_env))
1833
1834 ! Restricted calculations require just one set of OT orbitals per k-point.
1835 number_of_ot_envs = qs_ot_number_of_channels(dft_control%nspins, &
1836 nkpoint=nkpoint, &
1837 restricted=dft_control%restricted)
1838
1839 ALLOCATE (scf_env%qs_ot_env(number_of_ot_envs))
1840
1841 ! XXX Joost XXX should disentangle reading input from this part
1842 IF (scf_env%outer_scf%iter_count > 0) THEN
1843 IF (scf_env%iter_delta < scf_control%eps_diis) THEN
1844 scf_env%qs_ot_env(1)%settings%ot_state = 1
1845 END IF
1846 END IF
1847
1848 CALL ot_scf_read_input(scf_env%qs_ot_env, scf_section, do_kpoints)
1849
1850 IF (dft_control%restricted .AND. .NOT. do_kpoints .AND. &
1851 scf_env%qs_ot_env(1)%settings%preconditioner_type == &
1853 cpabort('Gamma-point PRECONDITIONER FULL_ALL_COVARIANT does not currently support ROKS.')
1854 END IF
1855
1856 IF (do_kpoints) THEN
1857 do_smear = dft_control%smear .OR. scf_control%smear%do_smear
1858 IF (do_smear) THEN
1859 IF (.NOT. scf_env%qs_ot_env(1)%settings%do_ener) THEN
1860 cpabort("K-point OT smearing requires OT%ENERGIES and OT%ROTATION.")
1861 END IF
1862 IF (.NOT. scf_env%qs_ot_env(1)%settings%do_rotation) THEN
1863 cpabort("K-point OT smearing requires OT%ROTATION.")
1864 END IF
1865 SELECT CASE (scf_control%smear%method)
1867 CONTINUE
1868 CASE DEFAULT
1869 cpabort("K-point Mermin OT does not support the selected smearing method.")
1870 END SELECT
1871 IF (scf_env%qs_ot_env(1)%settings%ot_algorithm /= "TOD" .AND. &
1872 scf_env%qs_ot_env(1)%settings%ot_algorithm /= "REF") THEN
1873 cpabort("K-point Mermin OT currently supports OT%ALGORITHM STRICT or IRAC.")
1874 END IF
1875 IF (scf_env%qs_ot_env(1)%settings%OT_METHOD /= "CG" .AND. &
1876 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "SD" .AND. &
1877 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "DIIS" .AND. &
1878 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "BROY" .AND. &
1879 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "LBFG") THEN
1880 cpabort("K-point Mermin OT supports OT%MINIMIZER CG, SD, DIIS, BROYDEN, or LBFGS.")
1881 END IF
1882 ELSE IF (scf_env%qs_ot_env(1)%settings%do_ener) THEN
1883 cpabort("OT%ENERGIES requires smearing in the complex K-point path.")
1884 END IF
1885 IF (scf_env%qs_ot_env(1)%settings%ot_algorithm /= "TOD" .AND. &
1886 scf_env%qs_ot_env(1)%settings%ot_algorithm /= "REF") THEN
1887 cpabort("K-point OT currently supports OT%ALGORITHM STRICT or IRAC.")
1888 END IF
1889 IF (scf_env%qs_ot_env(1)%settings%OT_METHOD /= "CG" .AND. &
1890 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "SD" .AND. &
1891 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "DIIS" .AND. &
1892 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "BROY" .AND. &
1893 scf_env%qs_ot_env(1)%settings%OT_METHOD /= "LBFG") THEN
1894 cpabort("K-point OT currently supports OT%MINIMIZER CG, SD, DIIS, BROYDEN, or LBFGS.")
1895 END IF
1897 scf_env%qs_ot_env(1)%settings%preconditioner_type, .false.)) THEN
1898 cpabort(kpoint_precond_error)
1899 END IF
1900 IF (scf_env%qs_ot_env(1)%settings%preconditioner_type == ot_precond_full_kinetic .AND. &
1901 (dft_control%qs_control%semi_empirical .OR. dft_control%qs_control%dftb .OR. &
1902 dft_control%qs_control%xtb)) THEN
1903 cpabort("FULL_KINETIC is unavailable for semi-empirical methods")
1904 END IF
1906 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
1907 scf_env%qs_ot_env(1)%settings%precond_solver_type, .false.)) THEN
1908 cpabort(kpoint_solver_error)
1909 END IF
1910 IF (scf_env%qs_ot_env(1)%settings%occupation_preconditioner .AND. .NOT. do_smear) THEN
1911 cpabort("K-point OCCUPATION_PRECONDITIONER requires smearing.")
1912 END IF
1913 END IF
1914
1915 IF (scf_env%outer_scf%iter_count > 0) THEN
1916 IF (scf_env%qs_ot_env(1)%settings%ot_state == 1) THEN
1917 scf_control%max_scf = max(scf_env%qs_ot_env(1)%settings%max_scf_diis, &
1918 scf_control%max_scf)
1919 END IF
1920 END IF
1921
1922 ! Keep a note that we are restricted.
1923 IF (dft_control%restricted) THEN
1924 scf_env%qs_ot_env(:)%restricted = .true.
1925 ! requires rotation
1926 IF (.NOT. scf_env%qs_ot_env(1)%settings%do_rotation) THEN
1927 CALL cp_abort(__location__, &
1928 "Restricted calculation with OT requires orbital rotation. Please "// &
1929 "activate the OT%ROTATION keyword!")
1930 END IF
1931 ELSE
1932 scf_env%qs_ot_env(:)%restricted = .false.
1933 END IF
1934
1935 ! This will rotate the MOs to be eigen states, which is not compatible with rotation.
1936 ! e.g. mo_derivs here do not yet include potentially different occupation numbers.
1937 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
1938 ! Only full all needs rotation.
1939 is_full_all = scf_env%qs_ot_env(1)%settings%preconditioner_type == ot_precond_full_all
1940 IF (do_rotation .AND. is_full_all .AND. .NOT. do_kpoints) THEN
1941 cpabort('PRECONDITIONER FULL_ALL is not compatible with ROTATION.')
1942 END IF
1943
1944 DO ikpoint = 1, nkpoint
1945 local_kpoint = 0
1946 IF (do_kpoints) THEN
1947 IF (ikpoint >= kp_range(1) .AND. ikpoint <= kp_range(2)) THEN
1948 local_kpoint = ikpoint - kp_range(1) + 1
1949 END IF
1950 END IF
1951 DO ispin = 1, nspin_ot
1952 ot_channel = qs_ot_channel_index(ispin, ikpoint, nspin_ot)
1953 IF (do_kpoints) THEN
1954 kpoint_weight = 1.0_dp
1955 IF (ASSOCIATED(wkp)) kpoint_weight = wkp(ikpoint)
1956 CALL qs_ot_set_context(scf_env%qs_ot_env(ot_channel), &
1957 spin_index=ispin, &
1958 kpoint_index=ikpoint, &
1959 local_kpoint_index=local_kpoint, &
1960 kpoint_weight=kpoint_weight)
1961 ELSE
1962 CALL qs_ot_set_context(scf_env%qs_ot_env(ot_channel), spin_index=ispin)
1963 END IF
1964 END DO
1965 END DO
1966
1967 CALL qs_ot_check_channel_context(scf_env%qs_ot_env, dft_control%nspins, &
1968 nkpoint=nkpoint, &
1969 restricted=dft_control%restricted, &
1970 require_kpoint=do_kpoints, &
1971 kp_range=kp_range, &
1972 wkp=wkp)
1973
1974 END SUBROUTINE allocate_qs_ot_envs
1975
1976! **************************************************************************************************
1977!> \brief allocate local k-point OT minimizer state for labelled spin/k-point channels
1978!> \param qs_env ...
1979!> \param scf_env ...
1980!> \param dft_control ...
1981!> \param kpoints ...
1982!> \param matrix_ks_kp ...
1983!> \param matrix_s_kp ...
1984!> \param matrix_t_kp ...
1985!> \param nspin_ot ...
1986! **************************************************************************************************
1987 SUBROUTINE allocate_qs_ot_kpoint_state(qs_env, scf_env, dft_control, &
1988 kpoints, matrix_ks_kp, matrix_s_kp, matrix_t_kp, nspin_ot)
1989 TYPE(qs_environment_type), POINTER :: qs_env
1990 TYPE(qs_scf_env_type), POINTER :: scf_env
1991 TYPE(dft_control_type), POINTER :: dft_control
1992 TYPE(kpoint_type), POINTER :: kpoints
1993 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp, matrix_t_kp
1994 INTEGER, INTENT(IN) :: nspin_ot
1995
1996 CHARACTER(LEN=*), PARAMETER :: routinen = 'allocate_qs_ot_kpoint_state'
1997
1998 INTEGER :: energy_dimension, energy_spin, energy_start, first_channel, handle, ikpoint, &
1999 ikpoint_local, ispin, local_channel, nao, nmo, nocc, ot_channel
2000 LOGICAL :: local_init, use_real_wfn
2001 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
2002 TYPE(cp_cfm_p_type), ALLOCATABLE :: local_c(:)
2003 TYPE(cp_fm_p_type), ALLOCATABLE :: local_s(:, :)
2004 TYPE(cp_fm_struct_type), POINTER :: active_mo_struct
2005 TYPE(cp_fm_type) :: active_mo_coeff, active_mo_coeff_im, &
2006 mo_coeff, mo_coeff_im
2007 TYPE(dbcsr_type), POINTER :: matrix_sk_im, matrix_sk_re
2008 TYPE(kpoint_env_type), POINTER :: kp
2009 TYPE(mo_set_type), POINTER :: mo_set
2010
2011 CALL timeset(routinen, handle)
2012
2013 NULLIFY (active_mo_struct, eigenvalues, kp, matrix_sk_im, matrix_sk_re, mo_set)
2014
2015 cpassert(ASSOCIATED(qs_env))
2016 cpassert(ASSOCIATED(scf_env%qs_ot_env))
2017 cpassert(ASSOCIATED(kpoints))
2018 cpassert(ASSOCIATED(kpoints%kp_env))
2019 cpassert(ASSOCIATED(matrix_ks_kp))
2020 cpassert(ASSOCIATED(matrix_s_kp))
2021 cpassert(ASSOCIATED(matrix_s_kp(1, 1)%matrix))
2022 cpassert(.NOT. dft_control%restricted .OR. nspin_ot == 1)
2023
2024 CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn)
2025 cpassert(.NOT. use_real_wfn)
2026
2027 local_init = kpoints%para_env_kp%num_pe == 1
2028 first_channel = qs_ot_channel_index(1, kpoints%kp_range(1), nspin_ot)
2029 IF (local_init) ALLOCATE (local_s(2, SIZE(kpoints%kp_env)*nspin_ot), local_c(SIZE(kpoints%kp_env)*nspin_ot))
2030
2031 DO ikpoint_local = 1, SIZE(kpoints%kp_env)
2032 kp => kpoints%kp_env(ikpoint_local)%kpoint_env
2033 cpassert(ASSOCIATED(kp))
2034 cpassert(ASSOCIATED(kp%mos))
2035 cpassert(ASSOCIATED(kp%ot_smat))
2036 cpassert(nspin_ot <= SIZE(kp%mos))
2037 ikpoint = kp%nkpoint
2038 cpassert(ikpoint >= 1)
2039 cpassert(SIZE(kp%ot_smat) >= 2)
2040 CALL kpoint_operator_get_local(matrix_s_kp, kpoints, kp, 1, &
2041 kp%ot_smat(1), kp%ot_smat(2), &
2042 matrix_sk_re, matrix_sk_im)
2043 DO ispin = 1, nspin_ot
2044 ot_channel = qs_ot_channel_index(ispin, ikpoint, nspin_ot)
2045 cpassert(.NOT. scf_env%qs_ot_env(ot_channel)%state_allocated)
2046
2047 mo_set => kp%mos(ispin)
2048 CALL get_mo_set(mo_set=mo_set, eigenvalues=eigenvalues, &
2049 homo=nocc, nao=nao, nmo=nmo)
2050 cpassert(ASSOCIATED(mo_set%cmo_coeff))
2051 cpassert(ASSOCIATED(eigenvalues))
2052 cpassert(nao > 0)
2053 cpassert(nmo > 0)
2054 IF (scf_env%qs_ot_env(ot_channel)%settings%do_ener) nocc = nmo
2055 IF (nocc < 1 .OR. nocc > nmo) THEN
2056 CALL cp_abort(__location__, &
2057 "K-point OT requires at least one occupied orbital in every spin channel.")
2058 END IF
2059
2060 CALL cp_fm_struct_create(active_mo_struct, template_fmstruct=mo_set%cmo_coeff%matrix_struct, &
2061 ncol_global=nocc)
2062 energy_dimension = nocc
2063 IF (dft_control%restricted .AND. &
2064 scf_env%qs_ot_env(ot_channel)%settings%do_ener) THEN
2065 energy_dimension = nocc*SIZE(kp%mos)
2066 END IF
2067 CALL qs_ot_allocate(scf_env%qs_ot_env(ot_channel), &
2068 matrix_sk_re, &
2069 active_mo_struct, energy_dimension=energy_dimension)
2070
2071 CALL qs_ot_allocate_complex_state(scf_env%qs_ot_env(ot_channel), matrix_sk_re)
2072 IF (local_init) THEN
2073 local_channel = ot_channel - first_channel + 1
2074 local_s(1, local_channel)%matrix => kp%ot_smat(1)
2075 local_s(2, local_channel)%matrix => kp%ot_smat(2)
2076 local_c(local_channel)%matrix => mo_set%cmo_coeff
2077 ELSE
2078 CALL cp_fm_create(mo_coeff, mo_set%cmo_coeff%matrix_struct)
2079 CALL cp_fm_create(mo_coeff_im, mo_set%cmo_coeff%matrix_struct)
2080 CALL cp_cfm_to_fm(mo_set%cmo_coeff, mo_coeff, mo_coeff_im)
2081 CALL cp_fm_create(active_mo_coeff, active_mo_struct)
2082 CALL cp_fm_to_fm(mo_coeff, active_mo_coeff, nocc)
2083 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_c0, 0.0_dp)
2084 CALL copy_fm_to_dbcsr(active_mo_coeff, scf_env%qs_ot_env(ot_channel)%matrix_c0)
2085
2086 CALL cp_fm_create(active_mo_coeff_im, active_mo_struct)
2087 CALL cp_fm_to_fm(mo_coeff_im, active_mo_coeff_im, nocc)
2088 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_c0_im, 0.0_dp)
2089 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_sc0_im, 0.0_dp)
2090 CALL copy_fm_to_dbcsr(active_mo_coeff_im, scf_env%qs_ot_env(ot_channel)%matrix_c0_im)
2091 CALL qs_ot_complex_multiply('N', 'N', matrix_sk_re, matrix_sk_im, &
2092 scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2093 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2094 scf_env%qs_ot_env(ot_channel)%matrix_sc0, &
2095 scf_env%qs_ot_env(ot_channel)%matrix_sc0_im, &
2096 scf_env%qs_ot_env(ot_channel)%matrix_x)
2097
2098 CALL cp_fm_release(active_mo_coeff)
2099 CALL cp_fm_release(active_mo_coeff_im)
2100 CALL cp_fm_release(mo_coeff)
2101 CALL cp_fm_release(mo_coeff_im)
2102 END IF
2103
2104 CALL qs_ot_init(scf_env%qs_ot_env(ot_channel))
2105 IF (scf_env%qs_ot_env(ot_channel)%settings%do_ener) THEN
2106 IF (dft_control%restricted) THEN
2107 DO energy_spin = 1, SIZE(kp%mos)
2108 CALL get_mo_set(kp%mos(energy_spin), eigenvalues=eigenvalues)
2109 energy_start = (energy_spin - 1)*nocc + 1
2110 scf_env%qs_ot_env(ot_channel)%ener_x(energy_start:energy_start + nocc - 1) = &
2111 eigenvalues(1:nocc)
2112 END DO
2113 ELSE
2114 scf_env%qs_ot_env(ot_channel)%ener_x(:) = eigenvalues(1:nocc)
2115 END IF
2116 END IF
2117 scf_env%qs_ot_env(ot_channel)%broyden_adaptive_sigma = qs_env%broyden_adaptive_sigma
2118
2119 SELECT CASE (scf_env%qs_ot_env(ot_channel)%settings%ot_algorithm)
2120 CASE ("TOD")
2121 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_x, 0.0_dp)
2122 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_sx, 0.0_dp)
2123 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_x_im, 0.0_dp)
2124 CALL dbcsr_set(scf_env%qs_ot_env(ot_channel)%matrix_sx_im, 0.0_dp)
2125 CALL qs_ot_get_p_complex(scf_env%qs_ot_env(ot_channel)%matrix_x, &
2126 scf_env%qs_ot_env(ot_channel)%matrix_x_im, &
2127 scf_env%qs_ot_env(ot_channel)%matrix_sx, &
2128 scf_env%qs_ot_env(ot_channel)%matrix_sx_im, &
2129 scf_env%qs_ot_env(ot_channel), zero_coordinates=.true.)
2130 CASE ("REF")
2131 IF (.NOT. local_init) THEN
2132 CALL dbcsr_copy(scf_env%qs_ot_env(ot_channel)%matrix_x, &
2133 scf_env%qs_ot_env(ot_channel)%matrix_c0)
2134 CALL dbcsr_copy(scf_env%qs_ot_env(ot_channel)%matrix_x_im, &
2135 scf_env%qs_ot_env(ot_channel)%matrix_c0_im)
2136 CALL qs_ot_get_orbitals_ref_complex(scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2137 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2138 matrix_sk_re, matrix_sk_im, &
2139 scf_env%qs_ot_env(ot_channel))
2140 END IF
2141 CASE DEFAULT
2142 cpabort("Algorithm not yet implemented")
2143 END SELECT
2144
2145 cpassert(scf_env%qs_ot_env(ot_channel)%state_allocated)
2146 cpassert(scf_env%qs_ot_env(ot_channel)%has_complex_kpoint_state)
2147 CALL cp_fm_struct_release(active_mo_struct)
2148 END DO
2149 CALL dbcsr_release_p(matrix_sk_re)
2150 CALL dbcsr_release_p(matrix_sk_im)
2151 END DO
2152
2153 IF (local_init) THEN
2155 scf_env%qs_ot_env(first_channel:first_channel + SIZE(local_c) - 1), &
2156 local_s, local_c, initialize=.true.)
2157 END IF
2158
2159 CALL qs_ot_check_channel_context(scf_env%qs_ot_env, dft_control%nspins, &
2160 nkpoint=kpoints%nkp, &
2161 restricted=any(scf_env%qs_ot_env(:)%restricted), &
2162 require_kpoint=.true., &
2163 kp_range=kpoints%kp_range, &
2164 require_local_state=.true., &
2165 require_complex_state=.true.)
2166
2167 CALL prepare_qs_ot_kpoint_preconditioners(scf_env, kpoints, matrix_ks_kp, &
2168 matrix_s_kp, matrix_t_kp, nspin_ot)
2169
2170 CALL timestop(handle)
2171
2172 END SUBROUTINE allocate_qs_ot_kpoint_state
2173
2174! **************************************************************************************************
2175!> \brief prepare and attach orbital preconditioners for complex k-point OT channels
2176!> \param scf_env ...
2177!> \param kpoints ...
2178!> \param matrix_ks ...
2179!> \param matrix_s ...
2180!> \param matrix_t ...
2181!> \param nspin_ot ...
2182! **************************************************************************************************
2183 SUBROUTINE prepare_qs_ot_kpoint_preconditioners(scf_env, kpoints, matrix_ks, matrix_s, matrix_t, nspin_ot)
2184 TYPE(qs_scf_env_type), POINTER :: scf_env
2185 TYPE(kpoint_type), POINTER :: kpoints
2186 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s, matrix_t
2187 INTEGER, INTENT(IN) :: nspin_ot
2188
2189 CHARACTER(LEN=*), PARAMETER :: routinen = 'prepare_qs_ot_kpoint_preconditioners'
2190
2191 INTEGER :: first_channel, handle, ikpoint, ispin, ks_spin, local_channel, local_kpoint, &
2192 n_ot_channels, nlocal, nocc, occupation_spin, ot_channel, prec_type, source_channel
2193 LOGICAL :: handled
2194 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: occupation_signature
2195 REAL(kind=dp), DIMENSION(:), POINTER :: occupation_numbers
2196 TYPE(cp_fm_p_type), ALLOCATABLE :: local_h(:, :), local_s(:, :)
2197 TYPE(dbcsr_p_type), ALLOCATABLE :: local_c(:, :)
2198 TYPE(dbcsr_type), POINTER :: matrix_h_im, matrix_h_re, matrix_s_im, &
2199 matrix_s_re, matrix_t_im, matrix_t_re
2200 TYPE(kpoint_env_type), POINTER :: kp
2201
2202 CALL timeset(routinen, handle)
2203
2204 NULLIFY (kp, matrix_h_im, matrix_h_re, matrix_s_im, matrix_s_re, matrix_t_im, matrix_t_re, &
2205 occupation_numbers)
2206 cpassert(ASSOCIATED(scf_env%qs_ot_env))
2207 cpassert(ASSOCIATED(kpoints))
2208 cpassert(ASSOCIATED(matrix_ks))
2209 cpassert(ASSOCIATED(matrix_s))
2210
2211 prec_type = scf_env%qs_ot_env(1)%settings%preconditioner_type
2212 IF (prec_type == ot_precond_none) THEN
2213 CALL timestop(handle)
2214 RETURN
2215 END IF
2216 SELECT CASE (prec_type)
2220 CASE DEFAULT
2221 cpabort("Unsupported complex K-point OT preconditioner")
2222 END SELECT
2223 n_ot_channels = SIZE(scf_env%qs_ot_env)
2224 IF (ASSOCIATED(scf_env%ot_preconditioner)) THEN
2225 DO ot_channel = 1, SIZE(scf_env%ot_preconditioner)
2226 IF (ASSOCIATED(scf_env%ot_preconditioner(ot_channel)%preconditioner)) THEN
2227 CALL destroy_preconditioner(scf_env%ot_preconditioner(ot_channel)%preconditioner)
2228 DEALLOCATE (scf_env%ot_preconditioner(ot_channel)%preconditioner)
2229 END IF
2230 END DO
2231 DEALLOCATE (scf_env%ot_preconditioner)
2232 NULLIFY (scf_env%ot_preconditioner)
2233 END IF
2234 ALLOCATE (scf_env%ot_preconditioner(n_ot_channels))
2235 DO local_kpoint = 1, SIZE(kpoints%kp_env)
2236 kp => kpoints%kp_env(local_kpoint)%kpoint_env
2237 ikpoint = kp%nkpoint
2238 DO ispin = 1, nspin_ot
2239 ot_channel = qs_ot_channel_index(ispin, ikpoint, nspin_ot)
2240 cpassert(scf_env%qs_ot_env(ot_channel)%state_allocated)
2241 ALLOCATE (scf_env%ot_preconditioner(ot_channel)%preconditioner)
2242 CALL init_preconditioner(scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2243 para_env=scf_env%qs_ot_env(ot_channel)%para_env, &
2244 blacs_env=scf_env%qs_ot_env(ot_channel)%blacs_env)
2245 END DO
2246 END DO
2247
2248 IF (prec_type == ot_precond_full_all .AND. .NOT. ALLOCATED(kpoints%lowdin_v)) THEN
2249 first_channel = qs_ot_channel_index(1, kpoints%kp_range(1), nspin_ot)
2250 nlocal = SIZE(kpoints%kp_env)*nspin_ot
2251 ALLOCATE (local_c(2, nlocal), local_h(2, nlocal), local_s(2, nlocal))
2252 DO local_kpoint = 1, SIZE(kpoints%kp_env)
2253 kp => kpoints%kp_env(local_kpoint)%kpoint_env
2254 DO ispin = 1, nspin_ot
2255 ot_channel = qs_ot_channel_index(ispin, kp%nkpoint, nspin_ot)
2256 local_channel = ot_channel - first_channel + 1
2257 ks_spin = min(ispin, SIZE(kp%ot_hmat, 2))
2258 local_c(1, local_channel)%matrix => scf_env%qs_ot_env(ot_channel)%matrix_c0
2259 local_c(2, local_channel)%matrix => scf_env%qs_ot_env(ot_channel)%matrix_c0_im
2260 local_h(1, local_channel)%matrix => kp%ot_hmat(1, ks_spin)
2261 local_h(2, local_channel)%matrix => kp%ot_hmat(2, ks_spin)
2262 local_s(1, local_channel)%matrix => kp%ot_smat(1)
2263 local_s(2, local_channel)%matrix => kp%ot_smat(2)
2264 END DO
2265 END DO
2267 scf_env%ot_preconditioner(first_channel:first_channel + nlocal - 1), local_c, local_h, local_s, &
2268 scf_env%qs_ot_env(1)%settings%energy_gap, scf_env%qs_ot_env(1)%settings%precond_solver_type, handled)
2269 IF (handled) THEN
2270 DO ot_channel = first_channel, first_channel + nlocal - 1
2271 CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ot_channel), &
2272 scf_env%ot_preconditioner(ot_channel)%preconditioner)
2273 END DO
2274 CALL timestop(handle)
2275 RETURN
2276 END IF
2277 END IF
2278
2279 DO local_kpoint = 1, SIZE(kpoints%kp_env)
2280 kp => kpoints%kp_env(local_kpoint)%kpoint_env
2281 cpassert(ASSOCIATED(kp))
2282 cpassert(ASSOCIATED(kp%ot_hmat))
2283 cpassert(ASSOCIATED(kp%ot_smat))
2284 cpassert(SIZE(kp%ot_smat) >= 2)
2285 ikpoint = kp%nkpoint
2286 CALL kpoint_operator_get_local(matrix_s, kpoints, kp, 1, &
2287 kp%ot_smat(1), kp%ot_smat(2), &
2288 matrix_s_re, matrix_s_im)
2289 IF (prec_type == ot_precond_full_kinetic) THEN
2290 cpassert(ASSOCIATED(matrix_t))
2291 cpassert(ASSOCIATED(kp%ot_tmat))
2292 cpassert(SIZE(kp%ot_tmat) >= 2)
2293 CALL kpoint_operator_get_local(matrix_t, kpoints, kp, 1, &
2294 kp%ot_tmat(1), kp%ot_tmat(2), &
2295 matrix_t_re, matrix_t_im)
2296 END IF
2297 DO ispin = 1, nspin_ot
2298 ot_channel = qs_ot_channel_index(ispin, ikpoint, nspin_ot)
2299 IF (ispin > 1 .AND. &
2300 (prec_type == ot_precond_full_kinetic .OR. prec_type == ot_precond_s_inverse)) THEN
2301 source_channel = qs_ot_channel_index(1, ikpoint, nspin_ot)
2302 IF (.NOT. (ASSOCIATED(scf_env%ot_preconditioner(source_channel)%preconditioner%complex_fm))) THEN
2303 CALL cp_abort(__location__, "Missing shared complex OT preconditioner")
2304 END IF
2305 scf_env%ot_preconditioner(ot_channel)%preconditioner%complex_fm => &
2306 scf_env%ot_preconditioner(source_channel)%preconditioner%complex_fm
2307 scf_env%ot_preconditioner(ot_channel)%preconditioner%owns_complex_fm = .false.
2308 scf_env%ot_preconditioner(ot_channel)%preconditioner%energy_gap = &
2309 scf_env%ot_preconditioner(source_channel)%preconditioner%energy_gap
2310 scf_env%ot_preconditioner(ot_channel)%preconditioner%in_use = &
2311 scf_env%ot_preconditioner(source_channel)%preconditioner%in_use
2312 scf_env%ot_preconditioner(ot_channel)%preconditioner%solver = &
2313 scf_env%ot_preconditioner(source_channel)%preconditioner%solver
2314 CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ot_channel), &
2315 scf_env%ot_preconditioner(ot_channel)%preconditioner)
2316 cycle
2317 END IF
2318 IF (prec_type == ot_precond_fermi_low_rank .OR. &
2319 prec_type == ot_precond_full_all .OR. &
2320 prec_type == ot_precond_full_all_covariant .OR. &
2321 prec_type == ot_precond_full_single .OR. &
2322 prec_type == ot_precond_full_single_inverse) THEN
2323 ks_spin = min(ispin, SIZE(kp%ot_hmat, 2))
2324 cpassert(SIZE(kp%ot_hmat, 1) >= 2)
2325 IF (ALLOCATED(kpoints%lowdin_v)) THEN
2326 block
2327 TYPE(cp_cfm_type) :: h
2328 TYPE(cp_fm_type) :: h_re, h_im
2329
2330 ! The physical +U operator need not share the real-space sparsity pattern.
2331 ! This full construction is only needed when preparing the preconditioner.
2332 CALL cp_cfm_create(h, kp%ot_hmat(1, ks_spin)%matrix_struct)
2333 CALL cp_fm_to_cfm(kp%ot_hmat(1, ks_spin), kp%ot_hmat(2, ks_spin), h)
2334 CALL lowdin_kp_apply(kp, kpoints%lowdin_v(:, ks_spin), cmat=h)
2335 CALL cp_fm_create(h_re, h%matrix_struct)
2336 CALL cp_fm_create(h_im, h%matrix_struct)
2337 CALL cp_cfm_to_fm(h, h_re, h_im)
2338 CALL dbcsr_init_p(matrix_h_re)
2339 CALL dbcsr_init_p(matrix_h_im)
2340 CALL copy_fm_to_dbcsr_bc(h_re, matrix_h_re)
2341 CALL copy_fm_to_dbcsr_bc(h_im, matrix_h_im)
2342 CALL cp_fm_release(h_re)
2343 CALL cp_fm_release(h_im)
2344 CALL cp_cfm_release(h)
2345 END block
2346 ELSE
2347 CALL kpoint_operator_get_local(matrix_ks, kpoints, kp, ks_spin, &
2348 kp%ot_hmat(1, ks_spin), kp%ot_hmat(2, ks_spin), &
2349 matrix_h_re, matrix_h_im)
2350 END IF
2351 END IF
2352 SELECT CASE (prec_type)
2355 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2356 scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2357 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2358 matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, &
2359 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2360 scf_env%qs_ot_env(ot_channel)%settings%fermi_low_rank_max_rank, &
2361 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2362 CASE (ot_precond_full_all)
2364 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2365 scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2366 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2367 matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, &
2368 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2369 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2372 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2373 matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, &
2374 kp%mos(ispin), &
2375 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2376 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2378 IF (nspin_ot == 1 .AND. SIZE(kp%mos) > 1) THEN
2379 CALL dbcsr_get_info(scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2380 nfullcols_total=nocc)
2381 ALLOCATE (occupation_signature(nocc, SIZE(kp%mos)))
2382 DO occupation_spin = 1, SIZE(kp%mos)
2383 CALL get_mo_set(kp%mos(occupation_spin), &
2384 occupation_numbers=occupation_numbers)
2385 cpassert(ASSOCIATED(occupation_numbers))
2386 cpassert(SIZE(occupation_numbers) >= nocc)
2387 occupation_signature(:, occupation_spin) = occupation_numbers(1:nocc)
2388 END DO
2390 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2391 scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2392 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2393 matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, &
2394 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2395 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type, &
2396 occupation_signature)
2397 DEALLOCATE (occupation_signature)
2398 ELSE
2400 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2401 scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2402 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2403 matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, &
2404 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2405 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2406 END IF
2409 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2410 scf_env%qs_ot_env(ot_channel)%matrix_c0, &
2411 scf_env%qs_ot_env(ot_channel)%matrix_c0_im, &
2412 matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, &
2413 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2414 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2417 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2418 matrix_t_re, matrix_t_im, matrix_s_re, matrix_s_im, &
2419 scf_env%qs_ot_env(ot_channel)%settings%energy_gap, &
2420 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2423 scf_env%ot_preconditioner(ot_channel)%preconditioner, &
2424 matrix_s_re, matrix_s_im, &
2425 scf_env%qs_ot_env(ot_channel)%settings%precond_solver_type)
2426 END SELECT
2427 CALL qs_ot_new_preconditioner(scf_env%qs_ot_env(ot_channel), &
2428 scf_env%ot_preconditioner(ot_channel)%preconditioner)
2429 IF (ASSOCIATED(matrix_h_re)) CALL dbcsr_release_p(matrix_h_re)
2430 IF (ASSOCIATED(matrix_h_im)) CALL dbcsr_release_p(matrix_h_im)
2431 END DO
2432 CALL dbcsr_release_p(matrix_s_re)
2433 CALL dbcsr_release_p(matrix_s_im)
2434 IF (ASSOCIATED(matrix_t_re)) CALL dbcsr_release_p(matrix_t_re)
2435 IF (ASSOCIATED(matrix_t_im)) CALL dbcsr_release_p(matrix_t_im)
2436 END DO
2437
2438 CALL timestop(handle)
2439
2440 END SUBROUTINE prepare_qs_ot_kpoint_preconditioners
2441
2442! **************************************************************************************************
2443!> \brief perform cleanup operations (like releasing temporary storage)
2444!> at the end of the scf
2445!> \param scf_env ...
2446!> \par History
2447!> 02.2003 created [fawzi]
2448!> \author fawzi
2449! **************************************************************************************************
2450 SUBROUTINE scf_env_cleanup(scf_env)
2451 TYPE(qs_scf_env_type), INTENT(INOUT) :: scf_env
2452
2453 CHARACTER(len=*), PARAMETER :: routinen = 'scf_env_cleanup'
2454
2455 INTEGER :: handle
2456
2457 CALL timeset(routinen, handle)
2458
2459 ! Release SCF work storage
2460 IF (ALLOCATED(scf_env%fm_copy_plan)) DEALLOCATE (scf_env%fm_copy_plan)
2461 CALL cp_fm_release(scf_env%scf_work1)
2462
2463 IF (ASSOCIATED(scf_env%scf_work1_red)) THEN
2464 CALL cp_fm_release(scf_env%scf_work1_red)
2465 END IF
2466 IF (ASSOCIATED(scf_env%scf_work2)) THEN
2467 CALL cp_fm_release(scf_env%scf_work2)
2468 DEALLOCATE (scf_env%scf_work2)
2469 NULLIFY (scf_env%scf_work2)
2470 END IF
2471 IF (ASSOCIATED(scf_env%scf_work2_red)) THEN
2472 CALL cp_fm_release(scf_env%scf_work2_red)
2473 DEALLOCATE (scf_env%scf_work2_red)
2474 NULLIFY (scf_env%scf_work2_red)
2475 END IF
2476 IF (ASSOCIATED(scf_env%ortho)) THEN
2477 CALL cp_fm_release(scf_env%ortho)
2478 DEALLOCATE (scf_env%ortho)
2479 NULLIFY (scf_env%ortho)
2480 END IF
2481 IF (ASSOCIATED(scf_env%ortho_red)) THEN
2482 CALL cp_fm_release(scf_env%ortho_red)
2483 DEALLOCATE (scf_env%ortho_red)
2484 NULLIFY (scf_env%ortho_red)
2485 END IF
2486 IF (ASSOCIATED(scf_env%ortho_m1)) THEN
2487 CALL cp_fm_release(scf_env%ortho_m1)
2488 DEALLOCATE (scf_env%ortho_m1)
2489 NULLIFY (scf_env%ortho_m1)
2490 END IF
2491 IF (ASSOCIATED(scf_env%ortho_m1_red)) THEN
2492 CALL cp_fm_release(scf_env%ortho_m1_red)
2493 DEALLOCATE (scf_env%ortho_m1_red)
2494 NULLIFY (scf_env%ortho_m1_red)
2495 END IF
2496
2497 IF (ASSOCIATED(scf_env%ortho_dbcsr)) THEN
2498 CALL dbcsr_deallocate_matrix(scf_env%ortho_dbcsr)
2499 END IF
2500 IF (ASSOCIATED(scf_env%buf1_dbcsr)) THEN
2501 CALL dbcsr_deallocate_matrix(scf_env%buf1_dbcsr)
2502 END IF
2503 IF (ASSOCIATED(scf_env%buf2_dbcsr)) THEN
2504 CALL dbcsr_deallocate_matrix(scf_env%buf2_dbcsr)
2505 END IF
2506
2507 IF (ASSOCIATED(scf_env%p_mix_new)) THEN
2508 CALL dbcsr_deallocate_matrix_set(scf_env%p_mix_new)
2509 END IF
2510
2511 IF (ASSOCIATED(scf_env%p_delta)) THEN
2512 CALL dbcsr_deallocate_matrix_set(scf_env%p_delta)
2513 END IF
2514
2515 ! Method dependent cleanup
2516 SELECT CASE (scf_env%method)
2517 CASE (ot_method_nr)
2518 !
2519 CASE (ot_diag_method_nr)
2520 !
2521 CASE (general_diag_method_nr)
2522 !
2523 CASE (special_diag_method_nr)
2524 !
2525 CASE (block_krylov_diag_method_nr)
2526 CASE (block_davidson_diag_method_nr)
2527 CALL block_davidson_deallocate(scf_env%block_davidson_env)
2528 CASE (filter_matrix_diag_method_nr)
2529 !
2530 CASE (smeagol_method_nr)
2531 !
2532 CASE DEFAULT
2533 cpabort("unknown scf method method:"//cp_to_string(scf_env%method))
2534 END SELECT
2535
2536 IF (ASSOCIATED(scf_env%outer_scf%variables)) THEN
2537 DEALLOCATE (scf_env%outer_scf%variables)
2538 END IF
2539 IF (ASSOCIATED(scf_env%outer_scf%count)) THEN
2540 DEALLOCATE (scf_env%outer_scf%count)
2541 END IF
2542 IF (ASSOCIATED(scf_env%outer_scf%gradient)) THEN
2543 DEALLOCATE (scf_env%outer_scf%gradient)
2544 END IF
2545 IF (ASSOCIATED(scf_env%outer_scf%energy)) THEN
2546 DEALLOCATE (scf_env%outer_scf%energy)
2547 END IF
2548 IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. &
2549 scf_env%outer_scf%deallocate_jacobian) THEN
2550 DEALLOCATE (scf_env%outer_scf%inv_jacobian)
2551 END IF
2552
2553 CALL timestop(handle)
2554
2555 END SUBROUTINE scf_env_cleanup
2556
2557! **************************************************************************************************
2558!> \brief perform a CDFT scf procedure in the given qs_env
2559!> \param qs_env the qs_environment where to perform the scf procedure
2560!> \param should_stop flag determining if calculation should stop
2561!> \param has_converged both the electronic and constraint loops converged
2562!> \param total_scf_steps number of electronic SCF steps over the constraint loop
2563!> \par History
2564!> 12.2015 Created
2565!> \author Nico Holmberg
2566! **************************************************************************************************
2567 SUBROUTINE cdft_scf(qs_env, should_stop, has_converged, total_scf_steps)
2568 TYPE(qs_environment_type), POINTER :: qs_env
2569 LOGICAL, INTENT(OUT) :: should_stop, has_converged
2570 INTEGER, INTENT(OUT) :: total_scf_steps
2571
2572 CHARACTER(len=*), PARAMETER :: routinen = 'cdft_scf'
2573
2574 INTEGER :: handle, iatom, iimage, ispin, ivar, nmo, &
2575 nvar, output_unit, tsteps
2576 LOGICAL :: cdft_loop_converged, converged, &
2577 exit_cdft_loop, first_iteration, &
2578 my_uocc, uniform_occupation
2579 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: gapw_cdft_values
2580 REAL(kind=dp), DIMENSION(:), POINTER :: mo_occupations
2581 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
2582 TYPE(cdft_control_type), POINTER :: cdft_control
2583 TYPE(cp_logger_type), POINTER :: logger
2584 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: gapw_wmat, matrix_s, rho_ao
2585 TYPE(dft_control_type), POINTER :: dft_control
2586 TYPE(local_rho_type), POINTER :: gapw_operator_rho
2587 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
2588 TYPE(mp_para_env_type), POINTER :: para_env
2589 TYPE(pw_env_type), POINTER :: pw_env
2590 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
2591 TYPE(qs_energy_type), POINTER :: energy
2592 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
2593 TYPE(qs_ks_env_type), POINTER :: ks_env
2594 TYPE(qs_rho_type), POINTER :: rho
2595 TYPE(qs_scf_env_type), POINTER :: scf_env
2596 TYPE(scf_control_type), POINTER :: scf_control
2597 TYPE(section_vals_type), POINTER :: dft_section, input, scf_section
2598
2599 NULLIFY (atomic_kind_set, gapw_operator_rho, gapw_wmat, para_env, qs_kind_set, &
2600 scf_env, ks_env, energy, rho, matrix_s, rho_ao, cdft_control, logger, &
2601 dft_control, pw_env, auxbas_pw_pool, energy, ks_env, scf_env, dft_section, &
2602 input, scf_section, scf_control, mos, mo_occupations)
2603 logger => cp_get_default_logger()
2604
2605 cpassert(ASSOCIATED(qs_env))
2606 CALL get_qs_env(qs_env, scf_env=scf_env, energy=energy, &
2607 dft_control=dft_control, scf_control=scf_control, &
2608 ks_env=ks_env, input=input)
2609
2610 CALL timeset(routinen//"_loop", handle)
2611 dft_section => section_vals_get_subs_vals(input, "DFT")
2612 scf_section => section_vals_get_subs_vals(dft_section, "SCF")
2613 output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%PROGRAM_RUN_INFO", &
2614 extension=".scfLog")
2615 first_iteration = .true.
2616
2617 cdft_control => dft_control%qs_control%cdft_control
2618
2619 scf_env%outer_scf%iter_count = 0
2620 cdft_control%total_steps = 0
2621 total_scf_steps = 0
2622
2623 ! Write some info about the CDFT calculation
2624 IF (output_unit > 0) THEN
2625 WRITE (unit=output_unit, fmt="(/,/,T2,A)") &
2626 "CDFT EXTERNAL SCF WAVEFUNCTION OPTIMIZATION"
2627 CALL qs_scf_cdft_initial_info(output_unit, cdft_control)
2628 END IF
2629 IF (cdft_control%reuse_precond) THEN
2630 reuse_precond = .false.
2631 cdft_control%nreused = 0
2632 END IF
2633 cdft_outer_loop: DO
2634 ! Change outer_scf settings to OT settings
2635 CALL outer_loop_switch(scf_env, scf_control, cdft_control, cdft2ot)
2636 ! Solve electronic structure with fixed value of constraint
2637 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
2638 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
2639 total_scf_steps = total_scf_steps + tsteps
2640 ! Decide whether to reuse the preconditioner on the next iteration
2641 IF (cdft_control%reuse_precond) THEN
2642 ! For convergence in exactly one step, the preconditioner is always reused (assuming max_reuse > 0)
2643 ! usually this means that the electronic structure has already converged to the correct state
2644 ! but the constraint optimizer keeps jumping over the optimal solution
2645 IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count == 1 &
2646 .AND. cdft_control%total_steps /= 1) THEN
2647 cdft_control%nreused = cdft_control%nreused - 1
2648 END IF
2649 ! SCF converged in less than precond_freq steps
2650 IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count <= cdft_control%precond_freq .AND. &
2651 cdft_control%total_steps /= 1 .AND. cdft_control%nreused < cdft_control%max_reuse) THEN
2652 reuse_precond = .true.
2653 cdft_control%nreused = cdft_control%nreused + 1
2654 ELSE
2655 reuse_precond = .false.
2656 cdft_control%nreused = 0
2657 END IF
2658 END IF
2659 ! Update history purging counters
2660 IF (first_iteration .AND. cdft_control%purge_history) THEN
2661 cdft_control%istep = cdft_control%istep + 1
2662 IF (scf_env%outer_scf%iter_count > 1) THEN
2663 cdft_control%nbad_conv = cdft_control%nbad_conv + 1
2664 IF (cdft_control%nbad_conv >= cdft_control%purge_freq .AND. &
2665 cdft_control%istep >= cdft_control%purge_offset) THEN
2666 cdft_control%nbad_conv = 0
2667 cdft_control%istep = 0
2668 cdft_control%should_purge = .true.
2669 END IF
2670 END IF
2671 END IF
2672 first_iteration = .false.
2673 ! Change outer_scf settings to CDFT settings
2674 CALL outer_loop_switch(scf_env, scf_control, cdft_control, ot2cdft)
2675 CALL qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, &
2676 cdft_loop_converged, exit_cdft_loop)
2677 CALL qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, &
2678 energy, cdft_control%total_steps, &
2679 should_stop, cdft_loop_converged, cdft_loop=.true.)
2680 IF (exit_cdft_loop) EXIT cdft_outer_loop
2681 ! Check if the inverse Jacobian needs to be calculated
2682 CALL qs_calculate_inverse_jacobian(qs_env)
2683 ! Check if a line search should be performed to find an optimal step size for the optimizer
2684 CALL qs_cdft_line_search(qs_env)
2685 ! Optimize constraint
2686 CALL outer_loop_optimize(scf_env, scf_control)
2687 CALL outer_loop_update_qs_env(qs_env, scf_env)
2688 CALL qs_ks_did_change(ks_env, potential_changed=.true.)
2689 END DO cdft_outer_loop
2690
2691 has_converged = converged .AND. cdft_loop_converged
2692 cdft_control%ienergy = cdft_control%ienergy + 1
2693
2694 ! Store needed arrays for ET coupling calculation
2695 IF (cdft_control%do_et) THEN
2696 CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, mos=mos)
2697 nvar = SIZE(cdft_control%target)
2698 IF (dft_control%qs_control%gapw) THEN
2699 IF (dft_control%nimages /= 1) THEN
2700 CALL cp_abort(__location__, &
2701 "GAPW CDFT-CI currently requires a Gamma-point calculation.")
2702 END IF
2703 CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, para_env=para_env, &
2704 qs_kind_set=qs_kind_set, rho=rho)
2705 CALL qs_rho_get(rho, rho_ao=rho_ao)
2706 CALL local_rho_set_create(gapw_operator_rho)
2707 CALL allocate_rho_atom_internals(gapw_operator_rho%rho_atom_set, atomic_kind_set, &
2708 qs_kind_set, dft_control, para_env)
2709 ALLOCATE (gapw_cdft_values(nvar), gapw_wmat(dft_control%nspins*dft_control%nimages))
2710 DO iimage = 1, dft_control%nimages
2711 DO ispin = 1, dft_control%nspins
2712 CALL dbcsr_init_p(gapw_wmat(dft_control%nspins*(iimage - 1) + ispin)%matrix)
2713 CALL dbcsr_copy(gapw_wmat(dft_control%nspins*(iimage - 1) + ispin)%matrix, &
2714 matrix_s(iimage)%matrix, name="GAPW CDFT WEIGHT MATRIX")
2715 END DO
2716 END DO
2717 END IF
2718 ! Matrix representation of weight function
2719 ALLOCATE (cdft_control%wmat(nvar))
2720 DO ivar = 1, nvar
2721 CALL dbcsr_init_p(cdft_control%wmat(ivar)%matrix)
2722 CALL dbcsr_copy(cdft_control%wmat(ivar)%matrix, matrix_s(1)%matrix, &
2723 name="ET_RESTRAINT_MATRIX")
2724 CALL dbcsr_set(cdft_control%wmat(ivar)%matrix, 0.0_dp)
2725 CALL integrate_v_rspace(cdft_control%group(ivar)%weight, &
2726 hmat=cdft_control%wmat(ivar), qs_env=qs_env, &
2727 calculate_forces=.false., &
2728 gapw=dft_control%qs_control%gapw)
2729 IF (dft_control%qs_control%gapw) THEN
2730 DO ispin = 1, SIZE(gapw_wmat)
2731 CALL dbcsr_set(gapw_wmat(ispin)%matrix, 0.0_dp)
2732 END DO
2733 CALL zero_rho_atom_integrals(gapw_operator_rho%rho_atom_set)
2734 CALL gapw_cdft_one_center(qs_env, energy_only=.false., calculate_forces=.false., &
2735 values=gapw_cdft_values, operator_group=ivar, &
2736 rho_atom_operator_set=gapw_operator_rho%rho_atom_set)
2737 CALL update_ks_atom(qs_env, gapw_wmat, rho_ao, forces=.false., &
2738 rho_atom_external=gapw_operator_rho%rho_atom_set)
2739 CALL dbcsr_add(cdft_control%wmat(ivar)%matrix, gapw_wmat(1)%matrix, 1.0_dp, 1.0_dp)
2740 END IF
2741 END DO
2742 IF (dft_control%qs_control%gapw) THEN
2743 CALL dbcsr_deallocate_matrix_set(gapw_wmat)
2744 CALL local_rho_set_release(gapw_operator_rho)
2745 DEALLOCATE (gapw_cdft_values)
2746 END IF
2747 ! Overlap matrix
2748 CALL dbcsr_init_p(cdft_control%matrix_s%matrix)
2749 CALL dbcsr_copy(cdft_control%matrix_s%matrix, matrix_s(1)%matrix, &
2750 name="OVERLAP")
2751 ! Molecular orbital coefficients
2752 NULLIFY (cdft_control%mo_coeff)
2753 ALLOCATE (cdft_control%mo_coeff(dft_control%nspins))
2754 DO ispin = 1, dft_control%nspins
2755 CALL cp_fm_create(matrix=cdft_control%mo_coeff(ispin), &
2756 matrix_struct=qs_env%mos(ispin)%mo_coeff%matrix_struct, &
2757 name="MO_COEFF_A"//trim(adjustl(cp_to_string(ispin)))//"MATRIX")
2758 CALL cp_fm_to_fm(qs_env%mos(ispin)%mo_coeff, &
2759 cdft_control%mo_coeff(ispin))
2760 END DO
2761 ! Density matrix
2762 IF (cdft_control%calculate_metric) THEN
2763 CALL get_qs_env(qs_env, rho=rho)
2764 CALL qs_rho_get(rho, rho_ao=rho_ao)
2765 ALLOCATE (cdft_control%matrix_p(dft_control%nspins))
2766 DO ispin = 1, dft_control%nspins
2767 NULLIFY (cdft_control%matrix_p(ispin)%matrix)
2768 CALL dbcsr_init_p(cdft_control%matrix_p(ispin)%matrix)
2769 CALL dbcsr_copy(cdft_control%matrix_p(ispin)%matrix, rho_ao(ispin)%matrix, &
2770 name="DENSITY MATRIX")
2771 END DO
2772 END IF
2773 ! Copy occupation numbers if non-uniform occupation
2774 uniform_occupation = .true.
2775 DO ispin = 1, dft_control%nspins
2776 CALL get_mo_set(mo_set=mos(ispin), uniform_occupation=my_uocc)
2777 uniform_occupation = uniform_occupation .AND. my_uocc
2778 END DO
2779 IF (.NOT. uniform_occupation) THEN
2780 ALLOCATE (cdft_control%occupations(dft_control%nspins))
2781 DO ispin = 1, dft_control%nspins
2782 CALL get_mo_set(mo_set=mos(ispin), &
2783 nmo=nmo, &
2784 occupation_numbers=mo_occupations)
2785 ALLOCATE (cdft_control%occupations(ispin)%array(nmo))
2786 cdft_control%occupations(ispin)%array(1:nmo) = mo_occupations(1:nmo)
2787 END DO
2788 END IF
2789 END IF
2790
2791 ! Deallocate constraint storage if forces are not needed
2792 ! In case of a simulation with multiple force_evals,
2793 ! deallocate only if weight function should not be copied to different force_evals
2794 IF (.NOT. (cdft_control%save_pot .OR. cdft_control%transfer_pot)) THEN
2795 CALL get_qs_env(qs_env, pw_env=pw_env)
2796 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
2797 DO iatom = 1, SIZE(cdft_control%group)
2798 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
2799 DEALLOCATE (cdft_control%group(iatom)%weight)
2800 END DO
2801 IF (cdft_control%atomic_charges) THEN
2802 DO iatom = 1, cdft_control%natoms
2803 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
2804 END DO
2805 DEALLOCATE (cdft_control%charge)
2806 END IF
2807 IF (cdft_control%type == outer_scf_becke_constraint .AND. &
2808 cdft_control%becke_control%cavity_confine) THEN
2809 IF (.NOT. ASSOCIATED(cdft_control%becke_control%cavity_mat)) THEN
2810 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
2811 ELSE
2812 DEALLOCATE (cdft_control%becke_control%cavity_mat)
2813 END IF
2814 ELSE IF (cdft_control%type == outer_scf_hirshfeld_constraint) THEN
2815 IF (ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)) THEN
2816 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
2817 END IF
2818 END IF
2819 IF (ASSOCIATED(cdft_control%charges_fragment)) DEALLOCATE (cdft_control%charges_fragment)
2820 cdft_control%need_pot = .true.
2821 cdft_control%external_control = .false.
2822 END IF
2823
2824 CALL timestop(handle)
2825
2826 END SUBROUTINE cdft_scf
2827
2828! **************************************************************************************************
2829!> \brief perform cleanup operations for cdft_control
2830!> \param cdft_control container for the external CDFT SCF loop variables
2831!> \par History
2832!> 12.2015 created [Nico Holmberg]
2833!> \author Nico Holmberg
2834! **************************************************************************************************
2835 SUBROUTINE cdft_control_cleanup(cdft_control)
2836 TYPE(cdft_control_type), POINTER :: cdft_control
2837
2838 IF (ASSOCIATED(cdft_control%constraint%variables)) THEN
2839 DEALLOCATE (cdft_control%constraint%variables)
2840 END IF
2841 IF (ASSOCIATED(cdft_control%constraint%count)) THEN
2842 DEALLOCATE (cdft_control%constraint%count)
2843 END IF
2844 IF (ASSOCIATED(cdft_control%constraint%gradient)) THEN
2845 DEALLOCATE (cdft_control%constraint%gradient)
2846 END IF
2847 IF (ASSOCIATED(cdft_control%constraint%energy)) THEN
2848 DEALLOCATE (cdft_control%constraint%energy)
2849 END IF
2850 IF (ASSOCIATED(cdft_control%constraint%inv_jacobian) .AND. &
2851 cdft_control%constraint%deallocate_jacobian) THEN
2852 DEALLOCATE (cdft_control%constraint%inv_jacobian)
2853 END IF
2854
2855 END SUBROUTINE cdft_control_cleanup
2856
2857! **************************************************************************************************
2858!> \brief Calculates the finite difference inverse Jacobian
2859!> \param qs_env the qs_environment_type where to compute the Jacobian
2860!> \par History
2861!> 01.2017 created [Nico Holmberg]
2862! **************************************************************************************************
2863 SUBROUTINE qs_calculate_inverse_jacobian(qs_env)
2864 TYPE(qs_environment_type), POINTER :: qs_env
2865
2866 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_calculate_inverse_jacobian'
2867
2868 CHARACTER(len=default_path_length) :: project_name
2869 INTEGER :: counter, handle, i, ispin, iter_count, &
2870 iwork, j, max_scf, nspins, nsteps, &
2871 nvar, nwork, output_unit, pwork, &
2872 tsteps, twork
2873 LOGICAL :: converged, explicit_jacobian, &
2874 should_build, should_stop, &
2875 use_md_history
2876 REAL(kind=dp) :: inv_error, step_size
2877 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: coeff, dh, step_multiplier
2878 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: jacobian
2879 REAL(kind=dp), DIMENSION(:), POINTER :: energy
2880 REAL(kind=dp), DIMENSION(:, :), POINTER :: gradient, inv_jacobian
2881 TYPE(cdft_control_type), POINTER :: cdft_control
2882 TYPE(cp_logger_type), POINTER :: logger, tmp_logger
2883 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: p_rmpv
2884 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
2885 TYPE(dft_control_type), POINTER :: dft_control
2886 TYPE(mo_set_type), ALLOCATABLE, DIMENSION(:) :: mos_stashed
2887 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
2888 TYPE(mp_para_env_type), POINTER :: para_env
2889 TYPE(qs_energy_type), POINTER :: energy_qs
2890 TYPE(qs_ks_env_type), POINTER :: ks_env
2891 TYPE(qs_rho_type), POINTER :: rho
2892 TYPE(qs_scf_env_type), POINTER :: scf_env
2893 TYPE(scf_control_type), POINTER :: scf_control
2894
2895 NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
2896 ks_env, scf_env, scf_control, dft_control, cdft_control, &
2897 inv_jacobian, para_env, tmp_logger, energy_qs)
2898 logger => cp_get_default_logger()
2899
2900 cpassert(ASSOCIATED(qs_env))
2901 CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
2902 scf_control=scf_control, mos=mos, rho=rho, &
2903 dft_control=dft_control, &
2904 para_env=para_env, energy=energy_qs)
2905 explicit_jacobian = .false.
2906 should_build = .false.
2907 use_md_history = .false.
2908 iter_count = scf_env%outer_scf%iter_count
2909 ! Quick exit if optimizer does not require Jacobian
2910 IF (.NOT. ASSOCIATED(scf_control%outer_scf%cdft_opt_control)) RETURN
2911 ! Check if Jacobian should be calculated and initialize
2912 CALL timeset(routinen, handle)
2913 CALL initialize_inverse_jacobian(scf_control, scf_env, explicit_jacobian, should_build, used_history)
2914 IF (scf_control%outer_scf%cdft_opt_control%jacobian_restart) THEN
2915 ! Restart from previously calculated inverse Jacobian
2916 should_build = .false.
2917 CALL restart_inverse_jacobian(qs_env)
2918 END IF
2919 IF (should_build) THEN
2920 scf_env%outer_scf%deallocate_jacobian = .false.
2921 ! Actually need to (re)build the Jacobian
2922 IF (explicit_jacobian) THEN
2923 ! Build Jacobian with finite differences
2924 cdft_control => dft_control%qs_control%cdft_control
2925 IF (.NOT. ASSOCIATED(cdft_control)) THEN
2926 CALL cp_abort(__location__, &
2927 "Optimizers that need the explicit Jacobian can"// &
2928 " only be used together with a valid CDFT constraint.")
2929 END IF
2930 ! Redirect output from Jacobian calculation to a new file by creating a temporary logger
2931 project_name = logger%iter_info%project_name
2932 CALL create_tmp_logger(para_env, project_name, "-JacobianInfo.out", output_unit, tmp_logger)
2933 ! Save last converged state so we can roll back to it (mo_coeff and some outer_loop variables)
2934 nspins = dft_control%nspins
2935 ALLOCATE (mos_stashed(nspins))
2936 DO ispin = 1, nspins
2937 CALL duplicate_mo_set(mos_stashed(ispin), mos(ispin))
2938 END DO
2939 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
2940 p_rmpv => rho_ao_kp(:, 1)
2941 ! Allocate work
2942 nvar = SIZE(scf_env%outer_scf%variables, 1)
2943 max_scf = scf_control%outer_scf%max_scf + 1
2944 ALLOCATE (gradient(nvar, max_scf))
2945 gradient = scf_env%outer_scf%gradient
2946 ALLOCATE (energy(max_scf))
2947 energy = scf_env%outer_scf%energy
2948 ALLOCATE (jacobian(nvar, nvar))
2949 jacobian = 0.0_dp
2950 nsteps = cdft_control%total_steps
2951 ! Setup finite difference scheme
2952 CALL prepare_jacobian_stencil(qs_env, output_unit, nwork, pwork, coeff, step_multiplier, dh)
2953 twork = pwork - nwork
2954 DO i = 1, nvar
2955 jacobian(i, :) = coeff(0)*scf_env%outer_scf%gradient(i, iter_count)
2956 END DO
2957 ! Calculate the Jacobian by perturbing each Lagrangian and recalculating the energy self-consistently
2958 CALL cp_add_default_logger(tmp_logger)
2959 DO i = 1, nvar
2960 IF (output_unit > 0) THEN
2961 WRITE (output_unit, fmt="(A)") " "
2962 WRITE (output_unit, fmt="(A)") " #####################################"
2963 WRITE (output_unit, '(A,I3,A,I3,A)') &
2964 " ### Constraint ", i, " of ", nvar, " ###"
2965 WRITE (output_unit, fmt="(A)") " #####################################"
2966 END IF
2967 counter = 0
2968 DO iwork = nwork, pwork
2969 IF (iwork == 0) cycle
2970 counter = counter + 1
2971 IF (output_unit > 0) THEN
2972 WRITE (output_unit, fmt="(A)") " #####################################"
2973 WRITE (output_unit, '(A,I3,A,I3,A)') &
2974 " ### Energy evaluation ", counter, " of ", twork, " ###"
2975 WRITE (output_unit, fmt="(A)") " #####################################"
2976 END IF
2977 IF (SIZE(scf_control%outer_scf%cdft_opt_control%jacobian_step) == 1) THEN
2978 step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(1)
2979 ELSE
2980 step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(i)
2981 END IF
2982 scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count)
2983 scf_env%outer_scf%variables(i, iter_count + 1) = scf_env%outer_scf%variables(i, iter_count) + &
2984 step_multiplier(iwork)*step_size
2985 CALL outer_loop_update_qs_env(qs_env, scf_env)
2986 CALL qs_ks_did_change(ks_env, potential_changed=.true.)
2987 CALL outer_loop_switch(scf_env, scf_control, cdft_control, cdft2ot)
2988 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
2989 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
2990 CALL outer_loop_switch(scf_env, scf_control, cdft_control, ot2cdft)
2991 ! Update (iter_count + 1) element of gradient and print constraint info
2992 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
2993 CALL outer_loop_gradient(qs_env, scf_env)
2994 CALL qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, &
2995 energy_qs, cdft_control%total_steps, &
2996 should_stop=.false., outer_loop_converged=.false., cdft_loop=.false.)
2997 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
2998 ! Update Jacobian
2999 DO j = 1, nvar
3000 jacobian(j, i) = jacobian(j, i) + coeff(iwork)*scf_env%outer_scf%gradient(j, iter_count + 1)
3001 END DO
3002 ! Reset everything to last converged state
3003 scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
3004 scf_env%outer_scf%gradient = gradient
3005 scf_env%outer_scf%energy = energy
3006 cdft_control%total_steps = nsteps
3007 DO ispin = 1, nspins
3008 CALL deallocate_mo_set(mos(ispin))
3009 CALL duplicate_mo_set(mos(ispin), mos_stashed(ispin))
3010 CALL calculate_density_matrix(mos(ispin), &
3011 p_rmpv(ispin)%matrix)
3012 END DO
3013 CALL qs_rho_update_rho(rho, qs_env=qs_env)
3014 CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.true.)
3015 END DO
3016 END DO
3017 CALL cp_rm_default_logger()
3018 CALL cp_logger_release(tmp_logger)
3019 ! Finalize and invert Jacobian
3020 DO j = 1, nvar
3021 DO i = 1, nvar
3022 jacobian(i, j) = jacobian(i, j)/dh(j)
3023 END DO
3024 END DO
3025 IF (.NOT. ASSOCIATED(scf_env%outer_scf%inv_jacobian)) THEN
3026 ALLOCATE (scf_env%outer_scf%inv_jacobian(nvar, nvar))
3027 END IF
3028 inv_jacobian => scf_env%outer_scf%inv_jacobian
3029 CALL invert_matrix(jacobian, inv_jacobian, inv_error)
3030 scf_control%outer_scf%cdft_opt_control%broyden_update = .false.
3031 ! Release temporary storage
3032 DO ispin = 1, nspins
3033 CALL deallocate_mo_set(mos_stashed(ispin))
3034 END DO
3035 DEALLOCATE (mos_stashed, jacobian, gradient, energy, coeff, step_multiplier, dh)
3036 IF (output_unit > 0) THEN
3037 WRITE (output_unit, fmt="(/,A)") &
3038 " ================================== JACOBIAN CALCULATED =================================="
3039 CALL close_file(unit_number=output_unit)
3040 END IF
3041 ELSE
3042 ! Build a strictly diagonal Jacobian from history and invert it
3043 CALL build_diagonal_jacobian(qs_env, used_history)
3044 END IF
3045 END IF
3046 IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. para_env%is_source()) THEN
3047 ! Write restart file for inverse Jacobian
3048 CALL print_inverse_jacobian(logger, scf_env%outer_scf%inv_jacobian, iter_count)
3049 END IF
3050 ! Update counter
3051 scf_control%outer_scf%cdft_opt_control%ijacobian(1) = scf_control%outer_scf%cdft_opt_control%ijacobian(1) + 1
3052 CALL timestop(handle)
3053
3054 END SUBROUTINE qs_calculate_inverse_jacobian
3055
3056! **************************************************************************************************
3057!> \brief Perform backtracking line search to find the optimal step size for the CDFT constraint
3058!> optimizer. Assumes that the CDFT gradient function is a smooth function of the constraint
3059!> variables.
3060!> \param qs_env the qs_environment_type where to perform the line search
3061!> \par History
3062!> 02.2017 created [Nico Holmberg]
3063! **************************************************************************************************
3064 SUBROUTINE qs_cdft_line_search(qs_env)
3065 TYPE(qs_environment_type), POINTER :: qs_env
3066
3067 CHARACTER(LEN=*), PARAMETER :: routinen = 'qs_cdft_line_search'
3068
3069 CHARACTER(len=default_path_length) :: project_name
3070 INTEGER :: handle, i, ispin, iter_count, &
3071 max_linesearch, max_scf, nspins, &
3072 nsteps, nvar, output_unit, tsteps
3073 LOGICAL :: continue_ls, continue_ls_exit, converged, do_linesearch, found_solution, &
3074 reached_maxls, should_exit, should_stop, sign_changed
3075 LOGICAL, ALLOCATABLE, DIMENSION(:) :: positive_sign
3076 REAL(kind=dp) :: alpha, alpha_ls, factor, norm_ls
3077 REAL(kind=dp), DIMENSION(:), POINTER :: energy
3078 REAL(kind=dp), DIMENSION(:, :), POINTER :: gradient, inv_jacobian
3079 REAL(kind=dp), EXTERNAL :: dnrm2
3080 TYPE(cdft_control_type), POINTER :: cdft_control
3081 TYPE(cp_logger_type), POINTER :: logger, tmp_logger
3082 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: p_rmpv
3083 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
3084 TYPE(dft_control_type), POINTER :: dft_control
3085 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
3086 TYPE(mp_para_env_type), POINTER :: para_env
3087 TYPE(qs_energy_type), POINTER :: energy_qs
3088 TYPE(qs_ks_env_type), POINTER :: ks_env
3089 TYPE(qs_rho_type), POINTER :: rho
3090 TYPE(qs_scf_env_type), POINTER :: scf_env
3091 TYPE(scf_control_type), POINTER :: scf_control
3092
3093 CALL timeset(routinen, handle)
3094
3095 NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
3096 ks_env, scf_env, scf_control, dft_control, &
3097 cdft_control, inv_jacobian, para_env, &
3098 tmp_logger, energy_qs)
3099 logger => cp_get_default_logger()
3100
3101 cpassert(ASSOCIATED(qs_env))
3102 CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
3103 scf_control=scf_control, mos=mos, rho=rho, &
3104 dft_control=dft_control, &
3105 para_env=para_env, energy=energy_qs)
3106 do_linesearch = .false.
3107 SELECT CASE (scf_control%outer_scf%optimizer)
3108 CASE DEFAULT
3109 do_linesearch = .false.
3110 CASE (outer_scf_optimizer_newton_ls)
3111 do_linesearch = .true.
3112 CASE (outer_scf_optimizer_broyden)
3113 SELECT CASE (scf_control%outer_scf%cdft_opt_control%broyden_type)
3114 CASE (broyden_type_1, broyden_type_2, broyden_type_1_explicit, broyden_type_2_explicit)
3115 do_linesearch = .false.
3116 CASE (broyden_type_1_ls, broyden_type_1_explicit_ls, broyden_type_2_ls, broyden_type_2_explicit_ls)
3117 cdft_control => dft_control%qs_control%cdft_control
3118 IF (.NOT. ASSOCIATED(cdft_control)) THEN
3119 CALL cp_abort(__location__, &
3120 "Optimizers that perform a line search can"// &
3121 " only be used together with a valid CDFT constraint")
3122 END IF
3123 IF (ASSOCIATED(scf_env%outer_scf%inv_jacobian)) THEN
3124 do_linesearch = .true.
3125 END IF
3126 END SELECT
3127 END SELECT
3128 IF (do_linesearch) THEN
3129 block
3130 TYPE(mo_set_type), DIMENSION(:), ALLOCATABLE :: mos_ls, mos_stashed
3131 cdft_control => dft_control%qs_control%cdft_control
3132 IF (.NOT. ASSOCIATED(cdft_control)) THEN
3133 CALL cp_abort(__location__, &
3134 "Optimizers that perform a line search can"// &
3135 " only be used together with a valid CDFT constraint")
3136 END IF
3137 cpassert(ASSOCIATED(scf_env%outer_scf%inv_jacobian))
3138 cpassert(ASSOCIATED(scf_control%outer_scf%cdft_opt_control))
3139 alpha = scf_control%outer_scf%cdft_opt_control%newton_step_save
3140 iter_count = scf_env%outer_scf%iter_count
3141 ! Redirect output from line search procedure to a new file by creating a temporary logger
3142 project_name = logger%iter_info%project_name
3143 CALL create_tmp_logger(para_env, project_name, "-LineSearch.out", output_unit, tmp_logger)
3144 ! Save last converged state so we can roll back to it (mo_coeff and some outer_loop variables)
3145 nspins = dft_control%nspins
3146 ALLOCATE (mos_stashed(nspins))
3147 DO ispin = 1, nspins
3148 CALL duplicate_mo_set(mos_stashed(ispin), mos(ispin))
3149 END DO
3150 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
3151 p_rmpv => rho_ao_kp(:, 1)
3152 nsteps = cdft_control%total_steps
3153 ! Allocate work
3154 nvar = SIZE(scf_env%outer_scf%variables, 1)
3155 max_scf = scf_control%outer_scf%max_scf + 1
3156 max_linesearch = scf_control%outer_scf%cdft_opt_control%max_ls
3157 continue_ls = scf_control%outer_scf%cdft_opt_control%continue_ls
3158 factor = scf_control%outer_scf%cdft_opt_control%factor_ls
3159 continue_ls_exit = .false.
3160 found_solution = .false.
3161 ALLOCATE (gradient(nvar, max_scf))
3162 gradient = scf_env%outer_scf%gradient
3163 ALLOCATE (energy(max_scf))
3164 energy = scf_env%outer_scf%energy
3165 reached_maxls = .false.
3166 ! Broyden optimizers: perform update of inv_jacobian if necessary
3167 IF (scf_control%outer_scf%cdft_opt_control%broyden_update) THEN
3168 CALL outer_loop_optimize(scf_env, scf_control)
3169 ! Reset the variables and prevent a reupdate of inv_jacobian
3170 scf_env%outer_scf%variables(:, iter_count + 1) = 0
3171 scf_control%outer_scf%cdft_opt_control%broyden_update = .false.
3172 END IF
3173 ! Print some info
3174 IF (output_unit > 0) THEN
3175 WRITE (output_unit, fmt="(/,A)") &
3176 " ================================== LINE SEARCH STARTED =================================="
3177 WRITE (output_unit, fmt="(A,I5,A)") &
3178 " Evaluating optimal step size for optimizer using a maximum of", max_linesearch, " steps"
3179 IF (continue_ls) THEN
3180 WRITE (output_unit, fmt="(A)") &
3181 " Line search continues until best step size is found or max steps are reached"
3182 END IF
3183 WRITE (output_unit, '(/,A,F5.3)') &
3184 " Initial step size: ", alpha
3185 WRITE (output_unit, '(/,A,F5.3)') &
3186 " Step size update factor: ", factor
3187 WRITE (output_unit, '(/,A,I10,A,I10)') &
3188 " Energy evaluation: ", cdft_control%ienergy, ", CDFT SCF iteration: ", iter_count
3189 END IF
3190 ! Perform backtracking line search
3191 CALL cp_add_default_logger(tmp_logger)
3192 DO i = 1, max_linesearch
3193 IF (output_unit > 0) THEN
3194 WRITE (output_unit, fmt="(A)") " "
3195 WRITE (output_unit, fmt="(A)") " #####################################"
3196 WRITE (output_unit, '(A,I10,A)') &
3197 " ### Line search step: ", i, " ###"
3198 WRITE (output_unit, fmt="(A)") " #####################################"
3199 END IF
3200 inv_jacobian => scf_env%outer_scf%inv_jacobian
3201 ! Newton update of CDFT variables with a step size of alpha
3202 scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count) - alpha* &
3203 matmul(inv_jacobian, scf_env%outer_scf%gradient(:, iter_count))
3204 ! With updated CDFT variables, perform SCF
3205 CALL outer_loop_update_qs_env(qs_env, scf_env)
3206 CALL qs_ks_did_change(ks_env, potential_changed=.true.)
3207 CALL outer_loop_switch(scf_env, scf_control, cdft_control, cdft2ot)
3208 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
3209 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
3210 CALL outer_loop_switch(scf_env, scf_control, cdft_control, ot2cdft)
3211 ! Update (iter_count + 1) element of gradient and print constraint info
3212 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
3213 CALL outer_loop_gradient(qs_env, scf_env)
3214 CALL qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, &
3215 energy_qs, cdft_control%total_steps, &
3216 should_stop=.false., outer_loop_converged=.false., cdft_loop=.false.)
3217 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
3218 ! Store sign of initial gradient for each variable for continue_ls
3219 IF (continue_ls .AND. .NOT. ALLOCATED(positive_sign)) THEN
3220 ALLOCATE (positive_sign(nvar))
3221 DO ispin = 1, nvar
3222 positive_sign(ispin) = scf_env%outer_scf%gradient(ispin, iter_count + 1) >= 0.0_dp
3223 END DO
3224 END IF
3225 ! Check if the L2 norm of the gradient decreased
3226 inv_jacobian => scf_env%outer_scf%inv_jacobian
3227 IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) < &
3228 dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count), 1)) THEN
3229 ! Optimal step size found
3230 IF (.NOT. continue_ls) THEN
3231 should_exit = .true.
3232 ELSE
3233 ! But line search continues for at least one more iteration in an attempt to find a better solution
3234 ! if max number of steps is not exceeded
3235 IF (found_solution) THEN
3236 ! Check if the norm also decreased w.r.t. to previously found solution
3237 IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) > norm_ls) THEN
3238 ! Norm increased => accept previous solution and exit
3239 continue_ls_exit = .true.
3240 END IF
3241 END IF
3242 ! Store current state and the value of alpha
3243 IF (.NOT. continue_ls_exit) THEN
3244 should_exit = .false.
3245 alpha_ls = alpha
3246 found_solution = .true.
3247 norm_ls = dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1)
3248 ! Check if the sign of the gradient has changed for all variables (w.r.t initial gradient)
3249 ! In this case we should exit because further line search steps will just increase the norm
3250 sign_changed = .true.
3251 DO ispin = 1, nvar
3252 sign_changed = sign_changed .AND. (positive_sign(ispin) .NEQV. &
3253 scf_env%outer_scf%gradient(ispin, iter_count + 1) >= 0.0_dp)
3254 END DO
3255 IF (.NOT. ALLOCATED(mos_ls)) THEN
3256 ALLOCATE (mos_ls(nspins))
3257 ELSE
3258 DO ispin = 1, nspins
3259 CALL deallocate_mo_set(mos_ls(ispin))
3260 END DO
3261 END IF
3262 DO ispin = 1, nspins
3263 CALL duplicate_mo_set(mos_ls(ispin), mos(ispin))
3264 END DO
3265 alpha = alpha*factor
3266 ! Exit on last iteration
3267 IF (i == max_linesearch) continue_ls_exit = .true.
3268 ! Exit if constraint target is satisfied to requested tolerance
3269 IF (sqrt(maxval(scf_env%outer_scf%gradient(:, scf_env%outer_scf%iter_count + 1)**2)) < &
3270 scf_control%outer_scf%eps_scf) THEN
3271 continue_ls_exit = .true.
3272 END IF
3273 ! Exit if line search jumped over the optimal step length
3274 IF (sign_changed) continue_ls_exit = .true.
3275 END IF
3276 END IF
3277 ELSE
3278 ! Gradient increased => alpha is too large (if the gradient function is smooth)
3279 should_exit = .false.
3280 ! Update alpha using Armijo's scheme
3281 alpha = alpha*factor
3282 END IF
3283 IF (continue_ls_exit) THEN
3284 ! Continuation of line search did not yield a better alpha, use previously located solution and exit
3285 alpha = alpha_ls
3286 DO ispin = 1, nspins
3287 CALL deallocate_mo_set(mos(ispin))
3288 CALL duplicate_mo_set(mos(ispin), mos_ls(ispin))
3289 CALL calculate_density_matrix(mos(ispin), &
3290 p_rmpv(ispin)%matrix)
3291 CALL deallocate_mo_set(mos_ls(ispin))
3292 END DO
3293 CALL qs_rho_update_rho(rho, qs_env=qs_env)
3294 CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.true.)
3295 DEALLOCATE (mos_ls)
3296 should_exit = .true.
3297 END IF
3298 ! Reached max steps and SCF converged: continue with last iterated step size
3299 IF (.NOT. should_exit .AND. &
3300 (i == max_linesearch .AND. converged .AND. .NOT. found_solution)) THEN
3301 should_exit = .true.
3302 reached_maxls = .true.
3303 alpha = alpha*(1.0_dp/factor)
3304 END IF
3305 ! Reset outer SCF environment to last converged state
3306 scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
3307 scf_env%outer_scf%gradient = gradient
3308 scf_env%outer_scf%energy = energy
3309 ! Exit line search if a suitable step size was found
3310 IF (should_exit) EXIT
3311 ! Reset the electronic structure
3312 cdft_control%total_steps = nsteps
3313 DO ispin = 1, nspins
3314 CALL deallocate_mo_set(mos(ispin))
3315 CALL duplicate_mo_set(mos(ispin), mos_stashed(ispin))
3316 CALL calculate_density_matrix(mos(ispin), &
3317 p_rmpv(ispin)%matrix)
3318 END DO
3319 CALL qs_rho_update_rho(rho, qs_env=qs_env)
3320 CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.true.)
3321 END DO
3322 scf_control%outer_scf%cdft_opt_control%newton_step = alpha
3323 IF (.NOT. should_exit) THEN
3324 CALL cp_warn(__location__, &
3325 "Line search did not converge. CDFT SCF proceeds with fixed step size.")
3326 scf_control%outer_scf%cdft_opt_control%newton_step = scf_control%outer_scf%cdft_opt_control%newton_step_save
3327 END IF
3328 IF (reached_maxls) THEN
3329 CALL cp_warn(__location__, &
3330 "Line search did not converge. CDFT SCF proceeds with lasted iterated step size.")
3331 END IF
3332 CALL cp_rm_default_logger()
3333 CALL cp_logger_release(tmp_logger)
3334 ! Release temporary storage
3335 DO ispin = 1, nspins
3336 CALL deallocate_mo_set(mos_stashed(ispin))
3337 END DO
3338 DEALLOCATE (mos_stashed, gradient, energy)
3339 IF (ALLOCATED(positive_sign)) DEALLOCATE (positive_sign)
3340 IF (output_unit > 0) THEN
3341 WRITE (output_unit, fmt="(/,A)") &
3342 " ================================== LINE SEARCH COMPLETE =================================="
3343 CALL close_file(unit_number=output_unit)
3344 END IF
3345 END block
3346 END IF
3347
3348 CALL timestop(handle)
3349
3350 END SUBROUTINE qs_cdft_line_search
3351
3352! **************************************************************************************************
3353!> \brief Rebuild the native xTB total energy from its components using the current
3354!> electronic entropy. The SCF matrix construction sees the entropy from the
3355!> previous iteration (or zero on the first/only one), so recompute it here
3356!> before the energy is reported or used.
3357!> \param energy ...
3358!> \param dft_control ...
3359! **************************************************************************************************
3360 SUBROUTINE rebuild_xtb_total_energy(energy, dft_control)
3361 TYPE(qs_energy_type), POINTER :: energy
3362 TYPE(dft_control_type), POINTER :: dft_control
3363
3364 IF (.NOT. dft_control%qs_control%xtb) RETURN
3365 IF (dft_control%qs_control%xtb_control%do_tblite) RETURN
3366
3367 SELECT CASE (dft_control%qs_control%xtb_control%gfn_type)
3368 CASE (0)
3369 energy%total = energy%core + energy%eeq + energy%efield + energy%qmmm_el + &
3370 energy%repulsive + energy%dispersion + energy%kTS
3371 CASE (1)
3372 energy%total = energy%core + energy%repulsive + energy%hartree + &
3373 energy%xtb_spinpol + energy%efield + energy%qmmm_el + &
3374 energy%dispersion + energy%dftb3 + energy%kTS
3375 END SELECT
3376
3377 END SUBROUTINE rebuild_xtb_total_energy
3378
3379END MODULE qs_scf
Define the atomic kind types and their sub types.
Molecular and Gamma-point periodic CC4S export from restricted RI-HF. RI_FLAVOR MO stores a weighted ...
Definition cc4s_export.F:15
subroutine, public write_cc4s(qs_env, converged)
Check the requested export, canonicalize private Fock/overlap copies and write tensors.
Definition cc4s_export.F:96
methods related to the blacs parallel environment
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_fm_to_cfm(msourcer, msourcei, mtarget)
Construct a complex full matrix by taking its real and imaginary parts from two separate real-value f...
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_to_fm(msource, mtargetr, mtargeti)
Copy real and imaginary parts of a complex full matrix into separate real-value full matrices.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_release_p(matrix)
...
subroutine, public dbcsr_deallocate_matrix(matrix)
...
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)
...
subroutine, public dbcsr_init_p(matrix)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
DBCSR operations in CP2K.
subroutine, public copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
Copy a DBCSR matrix to a BLACS matrix.
subroutine, public copy_fm_to_dbcsr_bc(fm, bc_mat)
Copy a BLACS matrix to a dbcsr matrix with a special block-cyclic distribution, which requires no com...
subroutine, public cp_dbcsr_m_by_n_from_row_template(matrix, template, n, sym)
Utility function to create dbcsr matrix, m x n matrix (n arbitrary) with the same processor grid and ...
subroutine, public copy_fm_to_dbcsr(fm, matrix, keep_sparsity)
Copy a BLACS matrix to a dbcsr matrix.
Utility routines to open and close files.
Definition cp_files.F:22
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
pool for for elements that are retained and released
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
subroutine, public cp_fm_init_random(matrix, ncol, start_col)
fills a matrix with random numbers
various routines to log and control the output. The idea is that decisions about where to log should ...
subroutine, public cp_rm_default_logger()
the cousin of cp_add_default_logger, decrements the stack, so that the default logger is what it has ...
subroutine, public cp_logger_release(logger)
releases this logger
subroutine, public cp_add_default_logger(logger)
adds a default logger. MUST be called before logging occours
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
subroutine, public cp_add_iter_level(logger, level_name, n_rlevel_new)
Adds an iteration level.
integer, parameter, public cp_p_file
subroutine, public cp_iterate(iteration_info, last, iter_nr, increment, iter_nr_out)
adds one to the actual iteration
integer function, public cp_print_key_should_output(iteration_info, basis_section, print_key_path, used_print_key, first_time)
returns what should be done with the given property if btest(res,cp_p_store) then the property should...
subroutine, public cp_rm_iter_level(logger, level_name, n_rlevel_att)
Removes an iteration level.
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout, file_access)
...
set of type/routines to handle the storage of results in force_envs
logical function, public test_for_result(results, description)
test for a certain result in the result_list
set of type/routines to handle the storage of results in force_envs
Add the DFT+U contribution to the Hamiltonian matrix.
Definition dft_plus_u.F:18
subroutine, public lowdin_kp_apply(kp, v, rmat, cmat)
Add B diag(v) B**H before any solver, DIIS residual, or history storage uses H(k).
Types needed for a for a Energy Correction.
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public smear_fermi_dirac
integer, parameter, public broyden_type_2_explicit_ls
integer, parameter, public broyden_type_1_explicit
integer, parameter, public broyden_type_2_ls
integer, parameter, public broyden_type_1
integer, parameter, public outer_scf_optimizer_broyden
integer, parameter, public ot_precond_full_all_covariant
integer, parameter, public plus_u_lowdin
integer, parameter, public broyden_type_1_explicit_ls
integer, parameter, public cholesky_dbcsr
integer, parameter, public broyden_type_2_explicit
integer, parameter, public history_guess
integer, parameter, public broyden_type_2
integer, parameter, public sic_eo
integer, parameter, public ot_precond_full_kinetic
integer, parameter, public cdft2ot
integer, parameter, public tblite_scc_mixer_tblite
integer, parameter, public outer_scf_becke_constraint
integer, parameter, public ot_precond_full_single
integer, parameter, public smear_gaussian
integer, parameter, public outer_scf_hirshfeld_constraint
integer, parameter, public ot_precond_none
integer, parameter, public smear_mv
integer, parameter, public ot_precond_full_single_inverse
integer, parameter, public diag_update_method_adiis
integer, parameter, public outer_scf_optimizer_newton_ls
integer, parameter, public ot_precond_fermi_low_rank
integer, parameter, public smear_mp
integer, parameter, public broyden_type_1_ls
integer, parameter, public ot_precond_s_inverse
integer, parameter, public ot_precond_full_all
integer, parameter, public ot2cdft
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
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
integer, parameter, public default_path_length
Definition kinds.F:58
Restart file for k point calculations.
Definition kpoint_io.F:13
subroutine, public write_kpoints_restart(denmat, kpoints, scf_env, dft_section, particle_set, qs_kind_set)
...
Definition kpoint_io.F:77
Routines needed for kpoint calculation.
subroutine, public kpoint_initialize_mo_set(kpoint)
...
subroutine, public kpoint_initialize_mos(kpoint, mos, added_mos, for_aux_fit)
Initialize a set of MOs and density matrix for each kpoint (kpoint group).
Types and basic routines needed for a kpoint calculation.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered, lattice_fft)
Retrieve information from a kpoint environment.
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:127
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:144
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
Interface to the message passing library MPI.
Define the data structure for the particle information.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public evolt
Definition physcon.F:183
types of preconditioners
subroutine, public init_preconditioner(preconditioner_env, para_env, blacs_env)
...
subroutine, public destroy_preconditioner(preconditioner_env)
...
computes preconditioners, and implements methods to apply them currently used in qs_ot
subroutine, public make_preconditioner_complex_full_s_inverse(preconditioner_env, matrix_s_re, matrix_s_im, solver_type)
Construct a complex FULL_S_INVERSE preconditioner.
subroutine, public make_preconditioner_complex_full_single(preconditioner_env, matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, mo_set, energy_gap, solver_type)
Construct a complex FULL_SINGLE preconditioner from H(k) and S(k).
subroutine, public make_preconditioner_complex_full_single_inverse(preconditioner_env, matrix_c_re, matrix_c_im, matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, energy_gap, solver_type)
Construct a complex FULL_SINGLE_INVERSE preconditioner without discarding Im(H,S,C).
subroutine, public make_preconditioner_complex_full_all_batch(preconditioners, matrix_c, matrix_h, matrix_s, energy_gap, solver_type, handled)
Build local FULL_ALL preconditioners from cached operators in bounded batches. Only the independent e...
subroutine, public make_preconditioner_complex_full_all_covariant(preconditioner_env, matrix_c_re, matrix_c_im, matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, energy_gap, solver_type, occupation_signature)
Construct rotation-covariant FULL_ALL for one complex k-point channel.
subroutine, public restart_preconditioner(qs_env, preconditioner, prec_type, nspins)
Allows for a restart of the preconditioner depending on the method it purges all arrays or keeps them...
subroutine, public prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, ot_preconditioner, prec_type, solver_type, energy_gap, nspins, has_unit_metric, convert_to_dbcsr, chol_type, full_mo_set, chebyshev_degree, low_rank_base, fermi_low_rank_max_rank, lattice_fft, lattice_fft_local_cells)
...
subroutine, public make_preconditioner_complex_full_all(preconditioner_env, matrix_c_re, matrix_c_im, matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, energy_gap, solver_type)
Construct FULL_ALL directly from one complex H(k), S(k), and C(k) channel.
subroutine, public make_preconditioner_complex_fermi_low_rank(preconditioner_env, matrix_c_re, matrix_c_im, matrix_h_re, matrix_h_im, matrix_s_re, matrix_s_im, energy_gap, max_rank, solver_type)
Construct FERMI_LOW_RANK directly for one complex k-point channel.
subroutine, public make_preconditioner_complex_full_kinetic(preconditioner_env, matrix_t_re, matrix_t_im, matrix_s_re, matrix_s_im, energy_gap, solver_type)
Construct a complex FULL_KINETIC preconditioner.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
module that contains the algorithms to perform an iterative diagonalization by the block-Davidson app...
subroutine, public block_davidson_deallocate(bdav_env)
...
Auxiliary routines for performing a constrained DFT SCF run with Quickstep.
subroutine, public prepare_jacobian_stencil(qs_env, output_unit, nwork, pwork, coeff, step_multiplier, dh)
Prepares the finite difference stencil for computing the Jacobian. The constraints are re-evaluated b...
subroutine, public build_diagonal_jacobian(qs_env, used_history)
Builds a strictly diagonal inverse Jacobian from MD/SCF history.
subroutine, public print_inverse_jacobian(logger, inv_jacobian, iter_count)
Prints the finite difference inverse Jacobian to file.
subroutine, public create_tmp_logger(para_env, project_name, suffix, output_unit, tmp_logger)
Creates a temporary logger for redirecting output to a new file.
subroutine, public restart_inverse_jacobian(qs_env)
Restarts the finite difference inverse Jacobian.
subroutine, public initialize_inverse_jacobian(scf_control, scf_env, explicit_jacobian, should_build, used_history)
Checks if the inverse Jacobian should be calculated and initializes the calculation.
Defines CDFT control structures.
real(kind=dp) function, public charge_mixing_scc_error(mixing_store, eps_scf)
Return the CP2K-side tblite SCC-mixer residual on the CP2K EPS_SCF scale.
container for information about total charges on the grids
collects routines that calculate density matrices
module that contains the definitions of the scf types
integer, parameter, public gspace_mixing_nr
Apply the direct inversion in the iterative subspace (DIIS) of Pulay in the framework of an SCF itera...
Definition qs_diis.F:21
pure subroutine, public qs_diis_b_clear(diis_buffer)
clears the buffer
Definition qs_diis.F:527
subroutine, public qs_diis_b_create(diis_buffer, nbuffer)
Allocates an SCF DIIS buffer.
Definition qs_diis.F:101
subroutine, public qs_diis_b_create_kp(diis_buffer, nbuffer)
Allocates an SCF DIIS buffer for k-points.
Definition qs_diis.F:878
pure subroutine, public qs_diis_b_clear_kp(diis_buffer)
clears the buffer
Definition qs_diis.F:984
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, matrix_vhxc, 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 set_qs_env(qs_env, super_cell, mos, qmmm, qmmm_periodic, mimic, ewald_env, ewald_pw, mpools, rho_external, external_vxc, mask, scf_control, rel_control, qs_charges, ks_env, ks_qmmm_env, wf_history, scf_env, active_space, input, oce, rho_atom_set, rho0_atom_set, rho0_mpole, run_rtp, rtp, rhoz_set, rhoz_tot, ecoul_1c, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, efield, rhoz_cneo_set, linres_control, xas_env, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, ls_scf_env, do_transport, transport_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, harris_env, gcp_env, mp2_env, bs_env, kg_env, force, kpoints, wanniercentres, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Set the QUICKSTEP environment.
Fractional occupation number weighted density (Grimme and Hansen).
Definition qs_fod.F:12
subroutine, public qs_fod_validate(input, logger, qs_env)
Reject unsupported FOD settings before starting an expensive SCF calculation.
Definition qs_fod.F:89
Integrate single or product functions over a potential on a RS grid.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
Assembly of real and complex k-point operators from real-space DBCSR matrices. Outputs use the distri...
subroutine, public kpoint_operator_context_create(ctx, kpoints, ao_ao_fm, nspin)
Create an empty operator context. The context owns no buffers after this call; they are created on fi...
subroutine, public kpoint_operator_store(ctx, matrix_ks, matrix_s, matrix_t)
Prepare owned split-FM operators for OT without entering an SCF eigensolver. H (or the initialization...
subroutine, public kpoint_operator_get_local(matrix_rs, kpoints, kp, ispin, cache_re, cache_im, matrix_re, matrix_im)
Read an operator inside a group-local OT loop, after collective cache preparation....
Methods for preparing and committing k-point orbital states to the QS environment.
subroutine, public qs_kpoint_state_commit(qs_env, update_occupations, separate_spin_occupations, fixed_occupations)
Rebuilds the physical density from the current k-point orbitals and occupations.
subroutine, public qs_kpoint_state_prepare_fixed_density(kpoints)
Reconstructs fixed-rank complex k-point orbitals from a density kernel. The density and overlap must ...
subroutine, public qs_kpoint_set_fixed_occupations(kpoints)
Restores the fixed occupied rank required by k-point OT.
logical function, public qs_kpoint_mos_initialized(kpoints, require_full_space)
Checks whether every local k-point channel contains nonzero occupied orbitals.
subroutine, public qs_kpoint_copy_spin_mos(kpoints, nspin)
Copies the first k-point spin channel into the remaining channels.
subroutine, public qs_kpoint_state_canonicalize_fixed(kpoints)
Rotates a uniformly occupied k-point subspace into Ritz states of the current Hamiltonian and stores ...
routines that build the Kohn-Sham matrix contributions coming from local atomic densities
Definition qs_ks_atom.F:12
subroutine, public update_ks_atom(qs_env, ksmat, pmat, forces, tddft, rho_atom_external, kind_set_external, oce_external, sab_external, kscale, kintegral, kforce, fscale)
The correction to the KS matrix due to the GAPW local terms to the hartree and XC contributions is he...
Definition qs_ks_atom.F:144
routines that build the Kohn-Sham matrix (i.e calculate the coulomb and xc parts
subroutine, public evaluate_core_matrix_traces(qs_env, rho_ao_ext)
Calculates the traces of the core matrices and the density matrix.
subroutine, public qs_ks_update_qs_env(qs_env, calculate_forces, just_energy, print_active)
updates the Kohn Sham matrix of the given qs_env (facility 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 get_ks_env(ks_env, v_hartree_rspace, s_mstruct_changed, rho_changed, exc_accint, potential_changed, forces_up_to_date, complex_ks, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, kinetic, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_ks_im_kp, rho, rho_xc, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, vee, neighbor_list_id, sab_orb, sab_all, sac_ae, sac_ppl, sac_lri, sap_ppnl, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_vdw, sab_scp, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, task_list, task_list_soft, kpoints, do_kpoints, atomic_kind_set, qs_kind_set, cell, cell_ref, use_ref_cell, particle_set, energy, force, local_particles, local_molecules, molecule_kind_set, molecule_set, subsys, cp_subsys, virial, results, atprop, nkind, natom, dft_control, dbcsr_dist, distribution_2d, pw_env, para_env, blacs_env, nelectron_total, nelectron_spin)
...
subroutine, public local_rho_set_create(local_rho_set)
...
subroutine, public local_rho_set_release(local_rho_set)
...
wrapper for the pools of matrixes
subroutine, public mpools_get(mpools, ao_mo_fm_pools, ao_ao_fm_pool, mo_mo_fm_pools, ao_mosub_fm_pools, mosub_mosub_fm_pools, maxao_maxmo_fm_pool, maxao_maxao_fm_pool, maxmo_maxmo_fm_pool)
returns various attributes of the mpools (notably the pools contained in it)
subroutine, public mpools_release(mpools)
releases the given mpools
subroutine, public mpools_rebuild_fm_pools(mpools, mos, blacs_env, para_env, nmosub)
rebuilds the pools of the (ao x mo, ao x ao , mo x mo) full matrixes
subroutine, public mixing_init(mixing_method, rho, mixing_store, para_env, rho_atom, auxiliary)
initialiation needed when gspace mixing is used
Definition and initialisation of the mo data type.
Definition qs_mo_io.F:21
subroutine, public write_mo_set_to_restart(mo_array, particle_set, dft_section, qs_kind_set, matrix_ks)
...
Definition qs_mo_io.F:119
collects routines that perform operations directly related to MOs
subroutine, public make_basis_simple(vmatrix, ncol)
given a set of vectors, return an orthogonal (C^T C == 1) set spanning the same space (notice,...
subroutine, public make_basis_sm(vmatrix, ncol, matrix_s)
returns an S-orthonormal basis v (v^T S v ==1)
Set occupation of molecular orbitals.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public duplicate_mo_set(mo_set_new, mo_set_old)
allocate a new mo_set, and copy the old data
subroutine, public init_mo_set(mo_set, fm_pool, fm_ref, fm_struct, name, counter, complex_coeff)
initializes an allocated mo_set. eigenvalues, mo_coeff, occupation_numbers are valid only after this ...
subroutine, public allocate_mo_set(mo_set, nao, nmo, nelectron, n_el_f, maxocc, flexible_electron_count)
Allocates a mo set and partially initializes it (nao,nmo,nelectron, and flexible_electron_count are v...
subroutine, public mo_set_restrict(mo_array, convert_dbcsr)
make the beta orbitals explicitly equal to the alpha orbitals effectively copying the orbital data
subroutine, public deallocate_mo_set(mo_set)
Deallocate a wavefunction data structure.
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count, cmo_coeff)
Get the components of a MO set data structure.
subroutine, public reassign_allocated_mos(mo_set_new, mo_set_old)
reassign an already allocated mo_set
Complex k-point orbital transformations, response kernels and local batch execution....
Definition qs_ot_kp.F:12
subroutine, public qs_ot_get_orbitals_local_batch(qs_ot_env, matrix_s, matrix_c, initialize)
Rebuild single-rank STRICT/IRAC orbitals in bounded batches using cached overlaps....
Definition qs_ot_kp.F:2239
subroutine, public qs_ot_get_orbitals_ref_complex(matrix_c, matrix_c_im, matrix_s, matrix_s_im, qs_ot_env, qs_ot_env1)
update complex REF k-point orbitals and their S(k)C(k) images
Definition qs_ot_kp.F:1136
subroutine, public qs_ot_get_p_complex(matrix_x, matrix_x_im, matrix_sx, matrix_sx_im, qs_ot_env, zero_coordinates)
compute P=X^H*S*X and the STRICT matrix functions for a complex K-point channel
Definition qs_ot_kp.F:1733
basic functionality for using ot in the scf routines.
Definition qs_ot_scf.F:14
subroutine, public ot_scf_init(mo_array, matrix_s, qs_ot_env, matrix_ks, broyden_adaptive_sigma)
initialises qs_ot_env so that mo_coeff is the current point and that the minization can be started.
Definition qs_ot_scf.F:382
subroutine, public ot_scf_read_input(qs_ot_env, scf_section, do_kpoints)
...
Definition qs_ot_scf.F:75
subroutine, public ot_scf_destroy(qs_ot_env)
...
Definition qs_ot_scf.F:481
orbital transformations
Definition qs_ot_types.F:15
subroutine, public qs_ot_init(qs_ot_env)
init matrices, needs c0 and sc0 so that c0*sc0=1
pure elemental logical function, public qs_ot_kpoint_preconditioner_supported(preconditioner_type, use_real_wfn)
Return whether a preconditioner is implemented for complex k-point OT.
subroutine, public qs_ot_allocate(qs_ot_env, matrix_s, fm_struct_ref, ortho_k, energy_dimension)
allocates the data in qs_ot_env, for a calculation with fm_struct_ref ortho_k allows for specifying a...
subroutine, public qs_ot_allocate_complex_state(qs_ot_env, matrix_s)
...
subroutine, public qs_ot_check_channel_context(qs_ot_env, nspin, nkpoint, restricted, require_kpoint, kp_range, wkp, require_local_state, require_complex_state)
validate the flat OT channel identity for spin and optional irreducible k-points
integer function, public qs_ot_number_of_channels(nspin, nkpoint, restricted)
number of OT optimization channels for spin and k-point resolved state
subroutine, public qs_ot_set_context(qs_ot_env, spin_index, kpoint_index, local_kpoint_index, kpoint_weight)
label an OT environment by spin and optional irreducible k-point context
integer function, public qs_ot_channel_index(ispin, ikpoint, nspin)
flat OT channel index for a spin/k-point pair
pure elemental logical function, public qs_ot_kpoint_preconditioner_solver_supported(preconditioner_type, solver_type, use_real_wfn)
Return whether a solver is implemented for a complex k-point OT preconditioner.
orbital transformations
Definition qs_ot.F:15
subroutine, public qs_ot_complex_multiply(op_a, op_b, a_re, a_im, b_re, b_im, c_re, c_im, tmp)
multiply paired real/imaginary DBCSR matrices, with optional conjugate transposes
Definition qs_ot.F:130
subroutine, public qs_ot_new_preconditioner(qs_ot_env, preconditioner)
gets ready to use the preconditioner/ or renew the preconditioner only keeps a pointer to the precond...
Definition qs_ot.F:74
Routines for performing an outer scf loop.
subroutine, public outer_loop_purge_history(qs_env)
purges outer_scf_history zeroing everything except the latest value of the outer_scf variable stored ...
subroutine, public outer_loop_switch(scf_env, scf_control, cdft_control, dir)
switch between two outer_scf envs stored in cdft_control
subroutine, public outer_loop_optimize(scf_env, scf_control)
optimizes the parameters of the outer_scf
subroutine, public outer_loop_update_qs_env(qs_env, scf_env)
propagates the updated variables to wherever they need to be set in qs_env
subroutine, public outer_loop_gradient(qs_env, scf_env)
computes the gradient wrt to the outer loop variables
subroutine, public allocate_rho_atom_internals(rho_atom_set, atomic_kind_set, qs_kind_set, dft_control, para_env)
...
subroutine, public zero_rho_atom_integrals(rho_atom_set)
...
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_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, update_p, diis_step, diis_error, qs_env, probe, added_mos_auto_grow, potential)
Kpoint diagonalization routine Transforms matrices to kpoint, distributes kpoint groups,...
Utility routines for qs_scf.
subroutine, public qs_scf_env_initialize(qs_env, scf_env, scf_control, scf_section)
initializes input parameters if needed or restores values from previous runs to fill scf_env with the...
Utility routines for qs_scf.
subroutine, public qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, probe, ot_kp_subspace_refresh, allow_ot_kp_subspace_refresh, allow_ot_kp_exit_refresh, accepted_ot_kp_searches, added_mos_auto_grow, energy_only)
Updates MOs and density matrix using diagonalization Kpoint code.
subroutine, public qs_scf_inner_finalize(scf_env, qs_env, diis_step, output_unit)
Performs the necessary steps before leaving innner scf loop.
pure logical function, public qs_scf_kp_search_endpoint(method)
identify an accepted OT search endpoint from its iteration label
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_check_inner_exit(qs_env, scf_env, scf_control, should_stop, just_energy, exit_inner_loop, inner_loop_converged, output_unit, u_changed)
checks whether exit conditions for inner loop are satisfied
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_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, kpoints)
Performs the requested density mixing if any needed.
subroutine, public qs_scf_commit_density_candidate(scf_env, rho)
Commit the diagonalized candidate density without numerical mixing.
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
subroutine, public qs_scf_candidate_density_delta(scf_env, rho, para_env, delta, kpoints)
Measure the distance between the diagonalized candidate density and the current density.
subroutine, public qs_scf_loop_info(scf_env, output_unit, just_energy, t1, t2, energy, adiis_verbose)
writes basic information obtained in a scf step
subroutine, public qs_scf_loop_print(qs_env, scf_env, para_env)
collects the 'heavy duty' printing tasks out of the SCF loop
subroutine, public qs_scf_outer_loop_info(output_unit, scf_control, scf_env, energy, total_steps, should_stop, outer_loop_converged)
writes basic information obtained in a scf outer loop step
subroutine, public qs_scf_write_mos(qs_env, scf_env, final_mos)
Write the MO eigenvector, eigenvalues, and occupation numbers to the output unit.
subroutine, public qs_scf_cdft_info(output_unit, scf_control, scf_env, cdft_control, energy, total_steps, should_stop, outer_loop_converged, cdft_loop)
writes CDFT constraint information and optionally CDFT scf loop info
subroutine, public qs_scf_cdft_initial_info(output_unit, cdft_control)
writes information about the CDFT env
subroutine, public qs_scf_gce_info(output_unit, qs_env, just_energy)
Print grand canonical SCF information for the current SCF iteration.
Utility routines for qs_scf.
subroutine, public qs_scf_compute_properties(qs_env, wf_type, do_mp2)
computes properties for a given hamilonian using the current wfn
Data types for the ADIIS SCF subspace accelerator.
pure subroutine, public qs_scf_subspace_buffer_create(buffer, nbuffer)
Initialize SCF subspace history metadata.
subroutine, public qs_scf_subspace_buffer_release(buffer)
Release all matrices and scalar storage owned by a subspace buffer.
pure subroutine, public qs_scf_subspace_buffer_clear(buffer)
Clear logical history while retaining allocated matrix storage.
History management and matrix combination for ADIIS.
subroutine, public qs_scf_subspace_push(buffer, fock, density, state_energy, pushed, density_aux, fock_aux)
Append one accepted, evaluated P,F[P] state to the ADIIS history.
subroutine, public qs_scf_subspace_restart(buffer, fock, density, state_energy, restarted, density_aux, fock_aux)
Restart the ADIIS history from the current strictly paired P, F[P] state.
subroutine, public qs_scf_subspace_build(buffer)
Solve the ADIIS model from previously accepted history and form an effective KS matrix.
subroutine, public qs_scf_subspace_update_shift(buffer, shift)
Adapt candidate regularization from two evaluated states; no trial KS builds.
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
subroutine, public scf_env_release_ot_operators(scf_env)
Drop k-point OT operator caches before their pools or layout can change.
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
Routines for the Quickstep SCF run.
Definition qs_scf.F:47
subroutine, public cdft_scf(qs_env, should_stop, has_converged, total_scf_steps)
perform a CDFT scf procedure in the given qs_env
Definition qs_scf.F:2568
subroutine, public scf(qs_env, has_converged, total_scf_steps)
perform an scf procedure in the given qs_env
Definition qs_scf.F:280
subroutine, public scf_env_cleanup(scf_env)
perform cleanup operations (like releasing temporary storage) at the end of the scf
Definition qs_scf.F:2451
subroutine, public init_scf_loop(scf_env, qs_env, scf_section, kp_ot_entry_reason)
inits those objects needed if you want to restart the scf with, say only a new initial guess,...
Definition qs_scf.F:1296
subroutine, public scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
perform an scf loop
Definition qs_scf.F:439
routines that build the integrals of the Vxc potential calculated for the atomic density in the basis...
Definition qs_vxc_atom.F:12
subroutine, public gapw_cdft_one_center(qs_env, energy_only, calculate_forces, values, electronic_charge, operator_group, rho_atom_operator_set)
Add the GAPW one-center correction to CDFT values and operators.
Storage of past states of the qs_env. Methods to interpolate (or actually normally extrapolate) the n...
subroutine, public wfi_purge_history(qs_env)
purges wf_history retaining only the latest snapshot
subroutine, public wfi_update(wf_history, qs_env, dt)
updates the snapshot buffer, taking a new snapshot
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.
subroutine, public run_smeagol_bulktrans(qs_env)
Save overlap, Kohn-Sham, electron density, and energy-density matrices of semi-infinite electrodes in...
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.
subroutine, public tb_update_charges(qs_env, dft_control, tb, calculate_forces, use_rho)
...
subroutine, public tb_get_energy(qs_env, tb, energy)
...
real(kind=dp) function, public tb_scf_mixer_error(dft_control, tb, eps_scf)
Return the native tblite SCC mixer residual on the CP2K iter_delta scale.
Provides all information about an atomic kind.
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Just to build arrays of pointers to matrices.
Represent a complex full matrix.
keeps the information about the structure of a full matrix
just to build arrays of pointers to matrices
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...
contains arbitrary information which need to be stored
Contains information on the energy correction functional for KG.
Keeps information about a specific k-point.
Contains information about kpoints.
stores all the informations relevant to an mpi environment
contained for different pw related things
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Container for information about total charges on the grids.
Provides all information about a quickstep kind.
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.