183#include "./base/base_uses.f90"
189 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'qs_scf'
190 LOGICAL,
PRIVATE :: reuse_precond = .false.
191 LOGICAL,
PRIVATE :: used_history = .false.
207 SUBROUTINE scf(qs_env, has_converged, total_scf_steps)
209 LOGICAL,
INTENT(OUT),
OPTIONAL :: has_converged
210 INTEGER,
INTENT(OUT),
OPTIONAL :: total_scf_steps
212 INTEGER :: ihistory, max_scf_tmp, tsteps
213 LOGICAL :: converged, outer_scf_loop, should_stop
214 LOGICAL,
SAVE :: first_step_flag = .true.
215 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gradient_history, variable_history
224 cpassert(
ASSOCIATED(qs_env))
225 IF (
PRESENT(has_converged))
THEN
226 has_converged = .false.
228 IF (
PRESENT(total_scf_steps))
THEN
231 CALL get_qs_env(qs_env, scf_env=scf_env, input=input, &
232 dft_control=dft_control, scf_control=scf_control)
233 IF (scf_control%max_scf > 0)
THEN
238 IF (.NOT.
ASSOCIATED(scf_env))
THEN
246 IF ((scf_control%density_guess ==
history_guess) .AND. (first_step_flag))
THEN
247 max_scf_tmp = scf_control%max_scf
248 scf_control%max_scf = 1
249 outer_scf_loop = scf_control%outer_scf%have_scf
250 scf_control%outer_scf%have_scf = .false.
253 IF (.NOT. dft_control%qs_control%cdft)
THEN
254 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
255 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
258 CALL cdft_scf(qs_env=qs_env, should_stop=should_stop)
262 IF (
ASSOCIATED(qs_env%mp2_env)) qs_env%mp2_env%hf_fail = .NOT. converged
265 IF (scf_control%outer_scf%have_scf)
THEN
266 ihistory = scf_env%outer_scf%iter_count
267 CALL get_qs_env(qs_env, gradient_history=gradient_history, &
268 variable_history=variable_history)
270 gradient_history(:, 1) = gradient_history(:, 2)
271 gradient_history(:, 2) = scf_env%outer_scf%gradient(:, ihistory)
272 variable_history(:, 1) = variable_history(:, 2)
273 variable_history(:, 2) = scf_env%outer_scf%variables(:, ihistory)
275 IF (used_history) used_history = .false.
277 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian))
THEN
278 scf_control%outer_scf%cdft_opt_control%ijacobian(2) = scf_control%outer_scf%cdft_opt_control%ijacobian(2) + 1
279 IF (scf_control%outer_scf%cdft_opt_control%ijacobian(2) >= &
280 scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) .AND. &
281 scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) > 0) &
282 scf_env%outer_scf%deallocate_jacobian = .true.
286 IF ((
ASSOCIATED(qs_env%wf_history)) .AND. &
288 (.NOT. first_step_flag)))
THEN
289 IF (.NOT. dft_control%qs_control%cdft)
THEN
290 CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
292 IF (dft_control%qs_control%cdft_control%should_purge)
THEN
295 dft_control%qs_control%cdft_control%should_purge = .false.
297 CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
300 ELSE IF ((scf_control%density_guess ==
history_guess) .AND. &
301 (first_step_flag))
THEN
302 scf_control%max_scf = max_scf_tmp
303 scf_control%outer_scf%have_scf = outer_scf_loop
304 first_step_flag = .false.
311 IF (.NOT. (should_stop))
THEN
320 IF (dft_control%qs_control%cdft) &
321 CALL cdft_control_cleanup(dft_control%qs_control%cdft_control)
323 IF (
PRESENT(has_converged))
THEN
324 has_converged = converged
326 IF (
PRESENT(total_scf_steps))
THEN
327 total_scf_steps = tsteps
351 SUBROUTINE scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
356 LOGICAL,
INTENT(OUT) :: converged, should_stop
357 INTEGER,
INTENT(OUT) :: total_scf_steps
359 CHARACTER(LEN=*),
PARAMETER :: routinen =
'scf_env_do_scf'
361 CHARACTER(LEN=default_string_length) :: description, name
362 INTEGER :: ext_master_id, handle, handle2, i_tmp, &
363 ic, ispin, iter_count, output_unit, &
364 scf_energy_message_tag, total_steps
365 LOGICAL :: density_full_step, diis_step, do_kpoints, energy_only, exit_inner_loop, &
366 exit_outer_loop, inner_loop_converged, internal_tblite_density_full_step, &
367 internal_tblite_mixer, just_energy, outer_loop_converged, tblite_native_mixer
368 REAL(kind=
dp) :: t1, t2
369 REAL(kind=
dp),
DIMENSION(3) :: res_val_3
374 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: rho_ao_kp
378 TYPE(
mo_set_type),
DIMENSION(:),
POINTER :: mos, mos_last_converged
385 TYPE(
qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
390 CALL timeset(routinen, handle)
392 NULLIFY (dft_control, rho, energy, &
393 logger, qs_charges, ks_env, mos, atomic_kind_set, qs_kind_set, &
394 particle_set, dft_section, input, &
395 scf_section, para_env, results, kpoints, pw_env, rho_ao_kp, mos_last_converged)
397 cpassert(
ASSOCIATED(scf_env))
398 cpassert(
ASSOCIATED(qs_env))
405 particle_set=particle_set, &
406 qs_charges=qs_charges, &
408 atomic_kind_set=atomic_kind_set, &
409 qs_kind_set=qs_kind_set, &
413 dft_control=dft_control, &
414 do_kpoints=do_kpoints, &
419 tblite_native_mixer = dft_control%qs_control%xtb_control%do_tblite .AND. &
422 internal_tblite_mixer = (dft_control%qs_control%dftb .AND. &
424 (dft_control%qs_control%xtb .AND. &
425 .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
427 internal_tblite_density_full_step = dft_control%qs_control%xtb .AND. &
428 .NOT. dft_control%qs_control%xtb_control%do_tblite .AND. &
439 IF (scf_control%gce%do_gce .AND. output_unit > 0)
THEN
440 WRITE (unit=output_unit, fmt=
"(/,T2,78('-'))")
441 WRITE (unit=output_unit, fmt=
"(T31,A)") &
442 "GRAND-CANONICAL SCF"
443 WRITE (unit=output_unit, fmt=
"(T20,A,F12.6,A)") &
444 "Target work function (TWF):", &
445 evolt*scf_control%gce%target_workfunction,
" eV"
446 WRITE (unit=output_unit, fmt=
"(T2,78('-'))")
449 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
450 "SCF WAVEFUNCTION OPTIMIZATION"
453 IF (dft_control%switch_surf_dip)
THEN
454 CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
455 DO ispin = 1, dft_control%nspins
458 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
459 "COPIED mos_last_converged ---> mos"
462 IF ((output_unit > 0) .AND. (.NOT. scf_control%use_ot))
THEN
463 WRITE (unit=output_unit, &
464 fmt=
"(/,T3,A,T12,A,T31,A,T39,A,T59,A,T75,A,/,T3,A)") &
465 "Step",
"Update method",
"Time",
"Convergence",
"Total energy",
"Change", &
471 res_val_3(:) = -1.0_dp
472 description =
"[EXT_SCF_ENER_COMM]"
474 CALL get_results(results, description=description, &
475 values=res_val_3, n_entries=i_tmp)
477 IF (all(res_val_3(:) <= 0.0)) &
478 CALL cp_abort(__location__, &
479 " Trying to access result ("//trim(description)// &
480 ") which is not correctly stored.")
481 CALL external_comm%set_handle(nint(res_val_3(1)))
483 ext_master_id = nint(res_val_3(2))
484 scf_energy_message_tag = nint(res_val_3(3))
488 scf_env%outer_scf%iter_count = 0
491 energy%tot_old = 0.0_dp
496 scf_section=scf_section)
499 energy_only, just_energy, exit_inner_loop)
502 dft_control%surf_dip_correct_switch = dft_control%correct_surf_dip
503 IF ((dft_control%correct_surf_dip) .AND. (scf_control%outer_scf%have_scf) .AND. &
504 (scf_env%outer_scf%iter_count > floor(scf_control%outer_scf%max_scf/2.0_dp)))
THEN
505 IF (dft_control%switch_surf_dip)
THEN
506 dft_control%surf_dip_correct_switch = .false.
507 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
508 "SURFACE DIPOLE CORRECTION switched off"
514 CALL timeset(routinen//
"_inner_loop", handle2)
516 IF (.NOT. just_energy) scf_env%iter_count = scf_env%iter_count + 1
517 iter_count = iter_count + 1
518 CALL cp_iterate(logger%iter_info, last=.false., iter_nr=iter_count)
520 IF (output_unit > 0)
CALL m_flush(output_unit)
522 total_steps = total_steps + 1
523 just_energy = energy_only
526 calculate_forces=.false.)
533 IF (dft_control%hairy_probes .EQV. .true.)
THEN
534 scf_control%smear%do_smear = .false.
535 CALL qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, dft_control%probe)
541 IF (dft_control%hairy_probes .EQV. .true.)
THEN
542 scf_control%smear%do_smear = .false.
543 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only, &
546 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
553 IF (dft_control%qs_control%xtb_control%do_tblite)
THEN
558 cpassert(scf_env%mixing_method > 0)
565 density_full_step = diis_step .OR. tblite_native_mixer .OR. internal_tblite_density_full_step
567 IF (dft_control%qs_control%xtb_control%do_tblite .AND. &
568 .NOT. (dft_control%qs_control%do_ls_scf .OR. scf_control%use_ot))
THEN
569 scf_env%iter_delta = max(scf_env%iter_delta, &
571 scf_control%eps_scf))
573 IF (dft_control%qs_control%dftb .OR. &
574 (dft_control%qs_control%xtb .AND. .NOT. dft_control%qs_control%xtb_control%do_tblite))
THEN
575 scf_env%iter_delta = max(scf_env%iter_delta, &
578 IF (tblite_native_mixer)
THEN
579 scf_env%iter_param = dft_control%qs_control%xtb_control%tblite_mixer_damping
580 scf_env%iter_method =
"TBLite/Diag"
581 ELSEIF (internal_tblite_mixer)
THEN
582 scf_env%iter_method =
"TBLite/Diag"
583 IF (dft_control%qs_control%dftb)
THEN
584 scf_env%iter_param = dft_control%qs_control%dftb_control%tblite_mixer_damping
586 scf_env%iter_param = dft_control%qs_control%xtb_control%tblite_mixer_damping
594 IF (scf_control%gce%do_gce)
THEN
598 IF (.NOT. just_energy) energy%tot_old = energy%total
601 IF (scf_energy_message_tag > 0)
THEN
602 CALL external_comm%send(energy%total, ext_master_id, scf_energy_message_tag)
606 exit_inner_loop, inner_loop_converged, output_unit)
610 IF (exit_inner_loop)
THEN
615 outer_loop_converged, exit_outer_loop)
618 IF (exit_outer_loop)
CALL cp_iterate(logger%iter_info, last=.true., iter_nr=iter_count)
628 CALL get_ks_env(ks_env=ks_env, matrix_ks=matrix_ks)
638 IF (exit_inner_loop)
THEN
639 CALL timestop(handle2)
644 scf_section,
"PRINT%ITERATION_INFO/TIME_CUMUL"),
cp_p_file)) &
648 IF (scf_env%mixing_method > 0)
THEN
649 DO ic = 1,
SIZE(rho_ao_kp, 2)
650 DO ispin = 1, dft_control%nspins
652 CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
660 CALL timestop(handle2)
664 IF (.NOT. scf_control%outer_scf%have_scf)
EXIT scf_outer_loop
668 energy, total_steps, should_stop, outer_loop_converged)
671 IF (exit_outer_loop)
THEN
672 IF ((dft_control%switch_surf_dip) .AND. (outer_loop_converged) .AND. &
673 (dft_control%surf_dip_correct_switch))
THEN
674 DO ispin = 1, dft_control%nspins
677 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
678 "COPIED mos ---> mos_last_converged"
682 IF (exit_outer_loop)
EXIT scf_outer_loop
689 END DO scf_outer_loop
691 converged = inner_loop_converged .AND. outer_loop_converged
692 total_scf_steps = total_steps
694 IF (dft_control%qs_control%cdft) &
695 dft_control%qs_control%cdft_control%total_steps = &
696 dft_control%qs_control%cdft_control%total_steps + total_steps
698 IF (.NOT. converged)
THEN
699 IF (scf_control%ignore_convergence_failure .OR. should_stop)
THEN
700 CALL cp_warn(__location__,
"SCF run NOT converged")
702 CALL cp_abort(__location__, &
703 "SCF run NOT converged. To continue the calculation "// &
704 "regardless, please set the keyword IGNORE_CONVERGENCE_FAILURE.")
709 IF (qs_env%energy_correction)
THEN
711 ec_env%do_skip = .false.
712 IF (ec_env%skip_ec .AND. .NOT. converged) ec_env%do_skip = .true.
716 DO ispin = 1,
SIZE(mos)
717 IF (mos(ispin)%use_mo_coeff_b)
THEN
718 IF (.NOT.
ASSOCIATED(mos(ispin)%mo_coeff_b)) &
719 cpabort(
"mo_coeff_b is not allocated")
726 CALL timestop(handle)
746 CHARACTER(LEN=*),
PARAMETER :: routinen =
'init_scf_loop'
748 INTEGER :: handle, ispin, nmo, number_of_ot_envs
749 LOGICAL :: do_kpoints, do_rotation, &
750 has_unit_metric, is_full_all
752 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: matrix_ks, matrix_s
753 TYPE(
dbcsr_type),
POINTER :: orthogonality_metric
759 CALL timeset(routinen, handle)
761 NULLIFY (scf_control, matrix_s, matrix_ks, dft_control, mos, mo_coeff, kpoints)
763 cpassert(
ASSOCIATED(scf_env))
764 cpassert(
ASSOCIATED(qs_env))
767 scf_control=scf_control, &
768 dft_control=dft_control, &
769 do_kpoints=do_kpoints, &
774 DO ispin = 1,
SIZE(mos)
775 IF (mos(1)%use_mo_coeff_b)
THEN
781 DO ispin = 1, dft_control%nspins
783 IF (.NOT. scf_control%diagonalization%mom)
THEN
786 IF (dft_control%hairy_probes .EQV. .true.)
THEN
787 IF (scf_env%outer_scf%iter_count > 0)
THEN
788 scf_control%smear%do_smear = .false.
790 smear=scf_control%smear, &
791 probe=dft_control%probe)
794 IF (.NOT. scf_control%gce%do_gce)
THEN
796 smear=scf_control%smear)
799 smear=scf_control%smear, &
806 SELECT CASE (scf_env%method)
809 cpabort(
"Unknown SCF method <"//trim(
cp_to_string(scf_env%method))//
"> found. Check the code!")
813 IF (.NOT. scf_env%skip_diis)
THEN
814 IF (.NOT.
ASSOCIATED(scf_env%scf_diis_buffer))
THEN
815 ALLOCATE (scf_env%scf_diis_buffer)
816 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
822 IF (.NOT. scf_env%skip_diis)
THEN
824 IF (.NOT.
ASSOCIATED(kpoints%scf_diis_buffer))
THEN
825 ALLOCATE (kpoints%scf_diis_buffer)
830 IF (.NOT.
ASSOCIATED(scf_env%scf_diis_buffer))
THEN
831 ALLOCATE (scf_env%scf_diis_buffer)
832 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
839 CALL get_qs_env(qs_env, matrix_ks=matrix_ks, matrix_s=matrix_s)
841 IF (.NOT. scf_env%skip_diis)
THEN
842 IF (.NOT.
ASSOCIATED(scf_env%scf_diis_buffer))
THEN
843 ALLOCATE (scf_env%scf_diis_buffer)
844 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
850 IF (dft_control%qs_control%dftb .OR. &
851 dft_control%qs_control%xtb .OR. &
852 dft_control%qs_control%semi_empirical)
THEN
853 cpabort(
"DFTB and SE not available with OT/DIAG")
859 scf_control%diagonalization%ot_settings%preconditioner_type, &
863 scf_control%diagonalization%ot_settings%preconditioner_type, &
864 scf_control%diagonalization%ot_settings%precond_solver_type, &
865 scf_control%diagonalization%ot_settings%energy_gap, dft_control%nspins)
871 has_unit_metric=has_unit_metric, &
879 IF (scf_control%do_outer_scf_reortho)
THEN
880 IF (scf_control%outer_scf%have_scf .AND. .NOT. dft_control%restricted)
THEN
881 IF (scf_env%outer_scf%iter_count > 0)
THEN
882 DO ispin = 1, dft_control%nspins
883 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
884 IF (has_unit_metric)
THEN
896 IF (.NOT.
ASSOCIATED(scf_env%qs_ot_env))
THEN
899 number_of_ot_envs = dft_control%nspins
900 IF (dft_control%restricted) number_of_ot_envs = 1
902 ALLOCATE (scf_env%qs_ot_env(number_of_ot_envs))
905 IF (scf_env%outer_scf%iter_count > 0)
THEN
906 IF (scf_env%iter_delta < scf_control%eps_diis)
THEN
907 scf_env%qs_ot_env(1)%settings%ot_state = 1
913 IF (scf_env%outer_scf%iter_count > 0)
THEN
914 IF (scf_env%qs_ot_env(1)%settings%ot_state == 1)
THEN
915 scf_control%max_scf = max(scf_env%qs_ot_env(1)%settings%max_scf_diis, &
921 IF (dft_control%restricted)
THEN
922 scf_env%qs_ot_env(1)%restricted = .true.
924 IF (.NOT. scf_env%qs_ot_env(1)%settings%do_rotation) &
925 CALL cp_abort(__location__, &
926 "Restricted calculation with OT requires orbital rotation. Please "// &
927 "activate the OT%ROTATION keyword!")
929 scf_env%qs_ot_env(:)%restricted = .false.
934 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
937 IF (do_rotation .AND. is_full_all) &
938 cpabort(
'PRECONDITIONER FULL_ALL is not compatible with ROTATION.')
942 calculate_forces=.false.)
946 IF (.NOT. reuse_precond) &
948 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
955 IF (has_unit_metric)
THEN
956 NULLIFY (orthogonality_metric)
958 orthogonality_metric => matrix_s(1)%matrix
961 IF (.NOT. reuse_precond) &
963 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
964 scf_env%qs_ot_env(1)%settings%precond_solver_type, &
965 scf_env%qs_ot_env(1)%settings%energy_gap, dft_control%nspins, &
966 has_unit_metric=has_unit_metric, &
967 chol_type=scf_env%qs_ot_env(1)%settings%cholesky_type)
968 IF (reuse_precond) reuse_precond = .false.
970 CALL ot_scf_init(mo_array=mos, matrix_s=orthogonality_metric, &
971 broyden_adaptive_sigma=qs_env%broyden_adaptive_sigma, &
972 qs_ot_env=scf_env%qs_ot_env, matrix_ks=matrix_ks(1)%matrix)
974 SELECT CASE (scf_env%qs_ot_env(1)%settings%preconditioner_type)
977 DO ispin = 1,
SIZE(scf_env%qs_ot_env)
979 scf_env%ot_preconditioner(ispin)%preconditioner)
982 DO ispin = 1,
SIZE(scf_env%qs_ot_env)
984 scf_env%ot_preconditioner(1)%preconditioner)
987 DO ispin = 1,
SIZE(scf_env%qs_ot_env)
989 scf_env%ot_preconditioner(1)%preconditioner)
995 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
996 DO ispin = 1,
SIZE(mos)
997 IF (.NOT. mos(ispin)%uniform_occupation)
THEN
998 cpassert(do_rotation)
1004 IF (dft_control%low_spin_roks)
THEN
1006 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
1007 cpassert(do_rotation)
1010 CALL timestop(handle)
1025 CHARACTER(len=*),
PARAMETER :: routinen =
'scf_env_cleanup'
1029 CALL timeset(routinen, handle)
1034 IF (
ASSOCIATED(scf_env%scf_work1_red))
THEN
1037 IF (
ASSOCIATED(scf_env%scf_work2))
THEN
1039 DEALLOCATE (scf_env%scf_work2)
1040 NULLIFY (scf_env%scf_work2)
1042 IF (
ASSOCIATED(scf_env%scf_work2_red))
THEN
1044 DEALLOCATE (scf_env%scf_work2_red)
1045 NULLIFY (scf_env%scf_work2_red)
1047 IF (
ASSOCIATED(scf_env%ortho))
THEN
1049 DEALLOCATE (scf_env%ortho)
1050 NULLIFY (scf_env%ortho)
1052 IF (
ASSOCIATED(scf_env%ortho_red))
THEN
1054 DEALLOCATE (scf_env%ortho_red)
1055 NULLIFY (scf_env%ortho_red)
1057 IF (
ASSOCIATED(scf_env%ortho_m1))
THEN
1059 DEALLOCATE (scf_env%ortho_m1)
1060 NULLIFY (scf_env%ortho_m1)
1062 IF (
ASSOCIATED(scf_env%ortho_m1_red))
THEN
1064 DEALLOCATE (scf_env%ortho_m1_red)
1065 NULLIFY (scf_env%ortho_m1_red)
1068 IF (
ASSOCIATED(scf_env%ortho_dbcsr))
THEN
1071 IF (
ASSOCIATED(scf_env%buf1_dbcsr))
THEN
1074 IF (
ASSOCIATED(scf_env%buf2_dbcsr))
THEN
1078 IF (
ASSOCIATED(scf_env%p_mix_new))
THEN
1082 IF (
ASSOCIATED(scf_env%p_delta))
THEN
1087 SELECT CASE (scf_env%method)
1104 cpabort(
"unknown scf method method:"//
cp_to_string(scf_env%method))
1107 IF (
ASSOCIATED(scf_env%outer_scf%variables))
THEN
1108 DEALLOCATE (scf_env%outer_scf%variables)
1110 IF (
ASSOCIATED(scf_env%outer_scf%count))
THEN
1111 DEALLOCATE (scf_env%outer_scf%count)
1113 IF (
ASSOCIATED(scf_env%outer_scf%gradient))
THEN
1114 DEALLOCATE (scf_env%outer_scf%gradient)
1116 IF (
ASSOCIATED(scf_env%outer_scf%energy))
THEN
1117 DEALLOCATE (scf_env%outer_scf%energy)
1119 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. &
1120 scf_env%outer_scf%deallocate_jacobian)
THEN
1121 DEALLOCATE (scf_env%outer_scf%inv_jacobian)
1124 CALL timestop(handle)
1138 LOGICAL,
INTENT(OUT) :: should_stop
1140 CHARACTER(len=*),
PARAMETER :: routinen =
'cdft_scf'
1142 INTEGER :: handle, iatom, ispin, ivar, nmo, nvar, &
1144 LOGICAL :: cdft_loop_converged, converged, &
1145 exit_cdft_loop, first_iteration, &
1146 my_uocc, uniform_occupation
1147 REAL(kind=
dp),
DIMENSION(:),
POINTER :: mo_occupations
1150 TYPE(
dbcsr_p_type),
DIMENSION(:),
POINTER :: matrix_s, rho_ao
1162 NULLIFY (scf_env, ks_env, energy, rho, matrix_s, rho_ao, cdft_control, logger, &
1163 dft_control, pw_env, auxbas_pw_pool, energy, ks_env, scf_env, dft_section, &
1164 input, scf_section, scf_control, mos, mo_occupations)
1167 cpassert(
ASSOCIATED(qs_env))
1168 CALL get_qs_env(qs_env, scf_env=scf_env, energy=energy, &
1169 dft_control=dft_control, scf_control=scf_control, &
1170 ks_env=ks_env, input=input)
1172 CALL timeset(routinen//
"_loop", handle)
1176 extension=
".scfLog")
1177 first_iteration = .true.
1179 cdft_control => dft_control%qs_control%cdft_control
1181 scf_env%outer_scf%iter_count = 0
1182 cdft_control%total_steps = 0
1185 IF (output_unit > 0)
THEN
1186 WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
1187 "CDFT EXTERNAL SCF WAVEFUNCTION OPTIMIZATION"
1190 IF (cdft_control%reuse_precond)
THEN
1191 reuse_precond = .false.
1192 cdft_control%nreused = 0
1198 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
1199 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
1201 IF (cdft_control%reuse_precond)
THEN
1205 IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count == 1 &
1206 .AND. cdft_control%total_steps /= 1) &
1207 cdft_control%nreused = cdft_control%nreused - 1
1209 IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count <= cdft_control%precond_freq .AND. &
1210 cdft_control%total_steps /= 1 .AND. cdft_control%nreused < cdft_control%max_reuse)
THEN
1211 reuse_precond = .true.
1212 cdft_control%nreused = cdft_control%nreused + 1
1214 reuse_precond = .false.
1215 cdft_control%nreused = 0
1219 IF (first_iteration .AND. cdft_control%purge_history)
THEN
1220 cdft_control%istep = cdft_control%istep + 1
1221 IF (scf_env%outer_scf%iter_count > 1)
THEN
1222 cdft_control%nbad_conv = cdft_control%nbad_conv + 1
1223 IF (cdft_control%nbad_conv >= cdft_control%purge_freq .AND. &
1224 cdft_control%istep >= cdft_control%purge_offset)
THEN
1225 cdft_control%nbad_conv = 0
1226 cdft_control%istep = 0
1227 cdft_control%should_purge = .true.
1231 first_iteration = .false.
1235 cdft_loop_converged, exit_cdft_loop)
1237 energy, cdft_control%total_steps, &
1238 should_stop, cdft_loop_converged, cdft_loop=.true.)
1239 IF (exit_cdft_loop)
EXIT cdft_outer_loop
1241 CALL qs_calculate_inverse_jacobian(qs_env)
1243 CALL qs_cdft_line_search(qs_env)
1248 END DO cdft_outer_loop
1250 cdft_control%ienergy = cdft_control%ienergy + 1
1253 IF (cdft_control%do_et)
THEN
1254 CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, mos=mos)
1255 nvar =
SIZE(cdft_control%target)
1257 ALLOCATE (cdft_control%wmat(nvar))
1260 CALL dbcsr_copy(cdft_control%wmat(ivar)%matrix, matrix_s(1)%matrix, &
1261 name=
"ET_RESTRAINT_MATRIX")
1262 CALL dbcsr_set(cdft_control%wmat(ivar)%matrix, 0.0_dp)
1263 CALL integrate_v_rspace(cdft_control%group(ivar)%weight, &
1264 hmat=cdft_control%wmat(ivar), qs_env=qs_env, &
1265 calculate_forces=.false., &
1266 gapw=dft_control%qs_control%gapw)
1270 CALL dbcsr_copy(cdft_control%matrix_s%matrix, matrix_s(1)%matrix, &
1273 NULLIFY (cdft_control%mo_coeff)
1274 ALLOCATE (cdft_control%mo_coeff(dft_control%nspins))
1275 DO ispin = 1, dft_control%nspins
1276 CALL cp_fm_create(matrix=cdft_control%mo_coeff(ispin), &
1277 matrix_struct=qs_env%mos(ispin)%mo_coeff%matrix_struct, &
1278 name=
"MO_COEFF_A"//trim(adjustl(
cp_to_string(ispin)))//
"MATRIX")
1280 cdft_control%mo_coeff(ispin))
1283 IF (cdft_control%calculate_metric)
THEN
1286 ALLOCATE (cdft_control%matrix_p(dft_control%nspins))
1287 DO ispin = 1, dft_control%nspins
1288 NULLIFY (cdft_control%matrix_p(ispin)%matrix)
1290 CALL dbcsr_copy(cdft_control%matrix_p(ispin)%matrix, rho_ao(ispin)%matrix, &
1291 name=
"DENSITY MATRIX")
1295 uniform_occupation = .true.
1296 DO ispin = 1, dft_control%nspins
1297 CALL get_mo_set(mo_set=mos(ispin), uniform_occupation=my_uocc)
1298 uniform_occupation = uniform_occupation .AND. my_uocc
1300 IF (.NOT. uniform_occupation)
THEN
1301 ALLOCATE (cdft_control%occupations(dft_control%nspins))
1302 DO ispin = 1, dft_control%nspins
1305 occupation_numbers=mo_occupations)
1306 ALLOCATE (cdft_control%occupations(ispin)%array(nmo))
1307 cdft_control%occupations(ispin)%array(1:nmo) = mo_occupations(1:nmo)
1315 IF (.NOT. (cdft_control%save_pot .OR. cdft_control%transfer_pot))
THEN
1317 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
1318 DO iatom = 1,
SIZE(cdft_control%group)
1319 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1320 DEALLOCATE (cdft_control%group(iatom)%weight)
1322 IF (cdft_control%atomic_charges)
THEN
1323 DO iatom = 1, cdft_control%natoms
1324 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1326 DEALLOCATE (cdft_control%charge)
1329 cdft_control%becke_control%cavity_confine)
THEN
1330 IF (.NOT.
ASSOCIATED(cdft_control%becke_control%cavity_mat))
THEN
1331 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1333 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1336 IF (
ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm))
THEN
1337 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1340 IF (
ASSOCIATED(cdft_control%charges_fragment))
DEALLOCATE (cdft_control%charges_fragment)
1341 cdft_control%need_pot = .true.
1342 cdft_control%external_control = .false.
1345 CALL timestop(handle)
1356 SUBROUTINE cdft_control_cleanup(cdft_control)
1359 IF (
ASSOCIATED(cdft_control%constraint%variables)) &
1360 DEALLOCATE (cdft_control%constraint%variables)
1361 IF (
ASSOCIATED(cdft_control%constraint%count)) &
1362 DEALLOCATE (cdft_control%constraint%count)
1363 IF (
ASSOCIATED(cdft_control%constraint%gradient)) &
1364 DEALLOCATE (cdft_control%constraint%gradient)
1365 IF (
ASSOCIATED(cdft_control%constraint%energy)) &
1366 DEALLOCATE (cdft_control%constraint%energy)
1367 IF (
ASSOCIATED(cdft_control%constraint%inv_jacobian) .AND. &
1368 cdft_control%constraint%deallocate_jacobian) &
1369 DEALLOCATE (cdft_control%constraint%inv_jacobian)
1371 END SUBROUTINE cdft_control_cleanup
1379 SUBROUTINE qs_calculate_inverse_jacobian(qs_env)
1382 CHARACTER(LEN=*),
PARAMETER :: routinen =
'qs_calculate_inverse_jacobian'
1384 CHARACTER(len=default_path_length) :: project_name
1385 INTEGER :: counter, handle, i, ispin, iter_count, &
1386 iwork, j, max_scf, nspins, nsteps, &
1387 nvar, nwork, output_unit, pwork, &
1389 LOGICAL :: converged, explicit_jacobian, &
1390 should_build, should_stop, &
1392 REAL(kind=
dp) :: inv_error, step_size
1393 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: coeff, dh, step_multiplier
1394 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: jacobian
1395 REAL(kind=
dp),
DIMENSION(:),
POINTER :: energy
1396 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gradient, inv_jacobian
1400 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: rho_ao_kp
1402 TYPE(
mo_set_type),
ALLOCATABLE,
DIMENSION(:) :: mos_stashed
1411 NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
1412 ks_env, scf_env, scf_control, dft_control, cdft_control, &
1413 inv_jacobian, para_env, tmp_logger, energy_qs)
1416 cpassert(
ASSOCIATED(qs_env))
1417 CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
1418 scf_control=scf_control, mos=mos, rho=rho, &
1419 dft_control=dft_control, &
1420 para_env=para_env, energy=energy_qs)
1421 explicit_jacobian = .false.
1422 should_build = .false.
1423 use_md_history = .false.
1424 iter_count = scf_env%outer_scf%iter_count
1426 IF (.NOT.
ASSOCIATED(scf_control%outer_scf%cdft_opt_control))
RETURN
1428 CALL timeset(routinen, handle)
1430 IF (scf_control%outer_scf%cdft_opt_control%jacobian_restart)
THEN
1432 should_build = .false.
1435 IF (should_build)
THEN
1436 scf_env%outer_scf%deallocate_jacobian = .false.
1438 IF (explicit_jacobian)
THEN
1440 cdft_control => dft_control%qs_control%cdft_control
1441 IF (.NOT.
ASSOCIATED(cdft_control)) &
1442 CALL cp_abort(__location__, &
1443 "Optimizers that need the explicit Jacobian can"// &
1444 " only be used together with a valid CDFT constraint.")
1446 project_name = logger%iter_info%project_name
1447 CALL create_tmp_logger(para_env, project_name,
"-JacobianInfo.out", output_unit, tmp_logger)
1449 nspins = dft_control%nspins
1450 ALLOCATE (mos_stashed(nspins))
1451 DO ispin = 1, nspins
1455 p_rmpv => rho_ao_kp(:, 1)
1457 nvar =
SIZE(scf_env%outer_scf%variables, 1)
1458 max_scf = scf_control%outer_scf%max_scf + 1
1459 ALLOCATE (gradient(nvar, max_scf))
1460 gradient = scf_env%outer_scf%gradient
1461 ALLOCATE (energy(max_scf))
1462 energy = scf_env%outer_scf%energy
1463 ALLOCATE (jacobian(nvar, nvar))
1465 nsteps = cdft_control%total_steps
1468 twork = pwork - nwork
1470 jacobian(i, :) = coeff(0)*scf_env%outer_scf%gradient(i, iter_count)
1475 IF (output_unit > 0)
THEN
1476 WRITE (output_unit, fmt=
"(A)")
" "
1477 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1478 WRITE (output_unit,
'(A,I3,A,I3,A)') &
1479 " ### Constraint ", i,
" of ", nvar,
" ###"
1480 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1483 DO iwork = nwork, pwork
1484 IF (iwork == 0) cycle
1485 counter = counter + 1
1486 IF (output_unit > 0)
THEN
1487 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1488 WRITE (output_unit,
'(A,I3,A,I3,A)') &
1489 " ### Energy evaluation ", counter,
" of ", twork,
" ###"
1490 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1492 IF (
SIZE(scf_control%outer_scf%cdft_opt_control%jacobian_step) == 1)
THEN
1493 step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(1)
1495 step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(i)
1497 scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count)
1498 scf_env%outer_scf%variables(i, iter_count + 1) = scf_env%outer_scf%variables(i, iter_count) + &
1499 step_multiplier(iwork)*step_size
1503 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
1504 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
1507 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
1510 energy_qs, cdft_control%total_steps, &
1511 should_stop=.false., outer_loop_converged=.false., cdft_loop=.false.)
1512 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
1515 jacobian(j, i) = jacobian(j, i) + coeff(iwork)*scf_env%outer_scf%gradient(j, iter_count + 1)
1518 scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
1519 scf_env%outer_scf%gradient = gradient
1520 scf_env%outer_scf%energy = energy
1521 cdft_control%total_steps = nsteps
1522 DO ispin = 1, nspins
1526 p_rmpv(ispin)%matrix)
1537 jacobian(i, j) = jacobian(i, j)/dh(j)
1540 IF (.NOT.
ASSOCIATED(scf_env%outer_scf%inv_jacobian)) &
1541 ALLOCATE (scf_env%outer_scf%inv_jacobian(nvar, nvar))
1542 inv_jacobian => scf_env%outer_scf%inv_jacobian
1544 scf_control%outer_scf%cdft_opt_control%broyden_update = .false.
1546 DO ispin = 1, nspins
1549 DEALLOCATE (mos_stashed, jacobian, gradient, energy, coeff, step_multiplier, dh)
1550 IF (output_unit > 0)
THEN
1551 WRITE (output_unit, fmt=
"(/,A)") &
1552 " ================================== JACOBIAN CALCULATED =================================="
1560 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. para_env%is_source())
THEN
1565 scf_control%outer_scf%cdft_opt_control%ijacobian(1) = scf_control%outer_scf%cdft_opt_control%ijacobian(1) + 1
1566 CALL timestop(handle)
1568 END SUBROUTINE qs_calculate_inverse_jacobian
1578 SUBROUTINE qs_cdft_line_search(qs_env)
1581 CHARACTER(LEN=*),
PARAMETER :: routinen =
'qs_cdft_line_search'
1583 CHARACTER(len=default_path_length) :: project_name
1584 INTEGER :: handle, i, ispin, iter_count, &
1585 max_linesearch, max_scf, nspins, &
1586 nsteps, nvar, output_unit, tsteps
1587 LOGICAL :: continue_ls, continue_ls_exit, converged, do_linesearch, found_solution, &
1588 reached_maxls, should_exit, should_stop, sign_changed
1589 LOGICAL,
ALLOCATABLE,
DIMENSION(:) :: positive_sign
1590 REAL(kind=
dp) :: alpha, alpha_ls, factor, norm_ls
1591 REAL(kind=
dp),
DIMENSION(:),
POINTER :: energy
1592 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gradient, inv_jacobian
1593 REAL(kind=
dp),
EXTERNAL :: dnrm2
1597 TYPE(
dbcsr_p_type),
DIMENSION(:, :),
POINTER :: rho_ao_kp
1607 CALL timeset(routinen, handle)
1609 NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
1610 ks_env, scf_env, scf_control, dft_control, &
1611 cdft_control, inv_jacobian, para_env, &
1612 tmp_logger, energy_qs)
1615 cpassert(
ASSOCIATED(qs_env))
1616 CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
1617 scf_control=scf_control, mos=mos, rho=rho, &
1618 dft_control=dft_control, &
1619 para_env=para_env, energy=energy_qs)
1620 do_linesearch = .false.
1621 SELECT CASE (scf_control%outer_scf%optimizer)
1623 do_linesearch = .false.
1625 do_linesearch = .true.
1627 SELECT CASE (scf_control%outer_scf%cdft_opt_control%broyden_type)
1629 do_linesearch = .false.
1631 cdft_control => dft_control%qs_control%cdft_control
1632 IF (.NOT.
ASSOCIATED(cdft_control)) &
1633 CALL cp_abort(__location__, &
1634 "Optimizers that perform a line search can"// &
1635 " only be used together with a valid CDFT constraint")
1636 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian)) &
1637 do_linesearch = .true.
1640 IF (do_linesearch)
THEN
1642 TYPE(
mo_set_type),
DIMENSION(:),
ALLOCATABLE :: mos_ls, mos_stashed
1643 cdft_control => dft_control%qs_control%cdft_control
1644 IF (.NOT.
ASSOCIATED(cdft_control)) &
1645 CALL cp_abort(__location__, &
1646 "Optimizers that perform a line search can"// &
1647 " only be used together with a valid CDFT constraint")
1648 cpassert(
ASSOCIATED(scf_env%outer_scf%inv_jacobian))
1649 cpassert(
ASSOCIATED(scf_control%outer_scf%cdft_opt_control))
1650 alpha = scf_control%outer_scf%cdft_opt_control%newton_step_save
1651 iter_count = scf_env%outer_scf%iter_count
1653 project_name = logger%iter_info%project_name
1654 CALL create_tmp_logger(para_env, project_name,
"-LineSearch.out", output_unit, tmp_logger)
1656 nspins = dft_control%nspins
1657 ALLOCATE (mos_stashed(nspins))
1658 DO ispin = 1, nspins
1662 p_rmpv => rho_ao_kp(:, 1)
1663 nsteps = cdft_control%total_steps
1665 nvar =
SIZE(scf_env%outer_scf%variables, 1)
1666 max_scf = scf_control%outer_scf%max_scf + 1
1667 max_linesearch = scf_control%outer_scf%cdft_opt_control%max_ls
1668 continue_ls = scf_control%outer_scf%cdft_opt_control%continue_ls
1669 factor = scf_control%outer_scf%cdft_opt_control%factor_ls
1670 continue_ls_exit = .false.
1671 found_solution = .false.
1672 ALLOCATE (gradient(nvar, max_scf))
1673 gradient = scf_env%outer_scf%gradient
1674 ALLOCATE (energy(max_scf))
1675 energy = scf_env%outer_scf%energy
1676 reached_maxls = .false.
1678 IF (scf_control%outer_scf%cdft_opt_control%broyden_update)
THEN
1681 scf_env%outer_scf%variables(:, iter_count + 1) = 0
1682 scf_control%outer_scf%cdft_opt_control%broyden_update = .false.
1685 IF (output_unit > 0)
THEN
1686 WRITE (output_unit, fmt=
"(/,A)") &
1687 " ================================== LINE SEARCH STARTED =================================="
1688 WRITE (output_unit, fmt=
"(A,I5,A)") &
1689 " Evaluating optimal step size for optimizer using a maximum of", max_linesearch,
" steps"
1690 IF (continue_ls)
THEN
1691 WRITE (output_unit, fmt=
"(A)") &
1692 " Line search continues until best step size is found or max steps are reached"
1694 WRITE (output_unit,
'(/,A,F5.3)') &
1695 " Initial step size: ", alpha
1696 WRITE (output_unit,
'(/,A,F5.3)') &
1697 " Step size update factor: ", factor
1698 WRITE (output_unit,
'(/,A,I10,A,I10)') &
1699 " Energy evaluation: ", cdft_control%ienergy,
", CDFT SCF iteration: ", iter_count
1703 DO i = 1, max_linesearch
1704 IF (output_unit > 0)
THEN
1705 WRITE (output_unit, fmt=
"(A)")
" "
1706 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1707 WRITE (output_unit,
'(A,I10,A)') &
1708 " ### Line search step: ", i,
" ###"
1709 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1711 inv_jacobian => scf_env%outer_scf%inv_jacobian
1713 scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count) - alpha* &
1714 matmul(inv_jacobian, scf_env%outer_scf%gradient(:, iter_count))
1719 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
1720 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
1723 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
1726 energy_qs, cdft_control%total_steps, &
1727 should_stop=.false., outer_loop_converged=.false., cdft_loop=.false.)
1728 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
1730 IF (continue_ls .AND. .NOT.
ALLOCATED(positive_sign))
THEN
1731 ALLOCATE (positive_sign(nvar))
1733 positive_sign(ispin) = scf_env%outer_scf%gradient(ispin, iter_count + 1) >= 0.0_dp
1737 inv_jacobian => scf_env%outer_scf%inv_jacobian
1738 IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) < &
1739 dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count), 1))
THEN
1741 IF (.NOT. continue_ls)
THEN
1742 should_exit = .true.
1746 IF (found_solution)
THEN
1748 IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) > norm_ls)
THEN
1750 continue_ls_exit = .true.
1754 IF (.NOT. continue_ls_exit)
THEN
1755 should_exit = .false.
1757 found_solution = .true.
1758 norm_ls = dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1)
1761 sign_changed = .true.
1763 sign_changed = sign_changed .AND. (positive_sign(ispin) .NEQV. &
1764 scf_env%outer_scf%gradient(ispin, iter_count + 1) >= 0.0_dp)
1766 IF (.NOT.
ALLOCATED(mos_ls))
THEN
1767 ALLOCATE (mos_ls(nspins))
1769 DO ispin = 1, nspins
1773 DO ispin = 1, nspins
1776 alpha = alpha*factor
1778 IF (i == max_linesearch) continue_ls_exit = .true.
1780 IF (sqrt(maxval(scf_env%outer_scf%gradient(:, scf_env%outer_scf%iter_count + 1)**2)) < &
1781 scf_control%outer_scf%eps_scf) &
1782 continue_ls_exit = .true.
1784 IF (sign_changed) continue_ls_exit = .true.
1789 should_exit = .false.
1791 alpha = alpha*factor
1793 IF (continue_ls_exit)
THEN
1796 DO ispin = 1, nspins
1800 p_rmpv(ispin)%matrix)
1806 should_exit = .true.
1809 IF (.NOT. should_exit .AND. &
1810 (i == max_linesearch .AND. converged .AND. .NOT. found_solution))
THEN
1811 should_exit = .true.
1812 reached_maxls = .true.
1813 alpha = alpha*(1.0_dp/factor)
1816 scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
1817 scf_env%outer_scf%gradient = gradient
1818 scf_env%outer_scf%energy = energy
1820 IF (should_exit)
EXIT
1822 cdft_control%total_steps = nsteps
1823 DO ispin = 1, nspins
1827 p_rmpv(ispin)%matrix)
1832 scf_control%outer_scf%cdft_opt_control%newton_step = alpha
1833 IF (.NOT. should_exit)
THEN
1834 CALL cp_warn(__location__, &
1835 "Line search did not converge. CDFT SCF proceeds with fixed step size.")
1836 scf_control%outer_scf%cdft_opt_control%newton_step = scf_control%outer_scf%cdft_opt_control%newton_step_save
1838 IF (reached_maxls) &
1839 CALL cp_warn(__location__, &
1840 "Line search did not converge. CDFT SCF proceeds with lasted iterated step size.")
1844 DO ispin = 1, nspins
1847 DEALLOCATE (mos_stashed, gradient, energy)
1848 IF (
ALLOCATED(positive_sign))
DEALLOCATE (positive_sign)
1849 IF (output_unit > 0)
THEN
1850 WRITE (output_unit, fmt=
"(/,A)") &
1851 " ================================== LINE SEARCH COMPLETE =================================="
1857 CALL timestop(handle)
1859 END SUBROUTINE qs_cdft_line_search
Define the atomic kind types and their sub types.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
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)
...
DBCSR operations in CP2K.
subroutine, public copy_dbcsr_to_fm(matrix, fm)
Copy a DBCSR matrix to a BLACS matrix.
Utility routines to open and close files. Tracking of preconnections.
subroutine, public close_file(unit_number, file_status, keep_preconnection)
Close an open file given by its logical unit number. Optionally, keep the file and unit preconnected.
represent a full matrix distributed on many processors
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
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...
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)
...
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
subroutine, public cp_rm_iter_level(iteration_info, level_name, n_rlevel_att)
Removes an iteration level.
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_add_iter_level(iteration_info, level_name, n_rlevel_new)
Adds an iteration level.
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
Types needed for a for a Energy Correction.
Defines the basic variable types.
integer, parameter, public dp
integer, parameter, public default_string_length
integer, parameter, public default_path_length
Restart file for k point calculations.
subroutine, public write_kpoints_restart(denmat, kpoints, scf_env, dft_section, particle_set, qs_kind_set)
...
Types and basic routines needed for a kpoint calculation.
Machine interface based on Fortran 2003 and POSIX.
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Collection of simple mathematical functions and subroutines.
Interface to the message passing library MPI.
Define the data structure for the particle information.
Definition of physical constants:
real(kind=dp), parameter, public evolt
computes preconditioners, and implements methods to apply them currently used in qs_ot
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)
...
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...
pure subroutine, public qs_diis_b_clear(diis_buffer)
clears the buffer
subroutine, public qs_diis_b_create(diis_buffer, nbuffer)
Allocates an SCF DIIS buffer.
subroutine, public qs_diis_b_create_kp(diis_buffer, nbuffer)
Allocates an SCF DIIS buffer for k-points.
pure subroutine, public qs_diis_b_clear_kp(diis_buffer)
clears the buffer
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
subroutine, public 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.
Integrate single or product functions over a potential on a RS grid.
Define the quickstep kind type and their sub types.
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_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)
...
Definition and initialisation of the mo data type.
subroutine, public write_mo_set_to_restart(mo_array, particle_set, dft_section, qs_kind_set, matrix_ks)
...
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.
subroutine, public duplicate_mo_set(mo_set_new, mo_set_old)
allocate a new mo_set, and copy the old 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)
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
basic functionality for using ot in the scf routines.
subroutine, public ot_scf_read_input(qs_ot_env, scf_section)
...
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.
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...
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
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...
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_check_inner_exit(qs_env, scf_env, scf_control, should_stop, just_energy, exit_inner_loop, inner_loop_converged, output_unit)
checks whether exit conditions for inner loop are satisfied
subroutine, public qs_scf_inner_finalize(scf_env, qs_env, diis_step, output_unit)
Performs the necessary steps before leaving innner scf loop.
subroutine, public qs_scf_set_loop_flags(scf_env, diis_step, energy_only, just_energy, exit_inner_loop)
computes properties for a given hamiltonian using the current wfn
subroutine, public qs_scf_rho_update(rho, qs_env, scf_env, ks_env, mix_rho)
Performs the updates rho (takes care of mixing as well)
subroutine, public qs_scf_new_mos_kp(qs_env, scf_env, scf_control, diis_step, probe)
Updates MOs and density matrix using diagonalization Kpoint code.
subroutine, public qs_scf_check_outer_exit(qs_env, scf_env, scf_control, should_stop, outer_loop_converged, exit_outer_loop)
checks whether exit conditions for outer loop are satisfied
subroutine, public qs_scf_density_mixing(scf_env, rho, para_env, diis_step)
Performs the requested density mixing if any needed.
subroutine, public qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only, probe)
takes known energy and derivatives and produces new wfns and or density matrix
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_loop_info(scf_env, output_unit, just_energy, t1, t2, energy)
writes basic information obtained in a scf step
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
module that contains the definitions of the scf types
integer, parameter, public ot_diag_method_nr
integer, parameter, public filter_matrix_diag_method_nr
integer, parameter, public block_davidson_diag_method_nr
integer, parameter, public smeagol_method_nr
integer, parameter, public ot_method_nr
integer, parameter, public special_diag_method_nr
integer, parameter, public block_krylov_diag_method_nr
integer, parameter, public general_diag_method_nr
Routines for the Quickstep SCF run.
subroutine, public init_scf_loop(scf_env, qs_env, scf_section)
inits those objects needed if you want to restart the scf with, say only a new initial guess,...
subroutine, public scf(qs_env, has_converged, total_scf_steps)
perform an scf procedure in the given qs_env
subroutine, public scf_env_cleanup(scf_env)
perform cleanup operations (like releasing temporary storage) at the end of the scf
subroutine, public scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
perform an scf loop
subroutine, public cdft_scf(qs_env, should_stop)
perform a CDFT scf procedure in the given qs_env
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
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...
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.
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.
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.