72 USE dbcsr_api,
ONLY: dbcsr_copy,&
73 dbcsr_deallocate_matrix,&
96 USE mathlib,
ONLY: invert_matrix
122 qs_environment_type,&
176 #include "./base/base_uses.f90"
182 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'qs_scf'
183 LOGICAL,
PRIVATE :: reuse_precond = .false.
184 LOGICAL,
PRIVATE :: used_history = .false.
200 SUBROUTINE scf(qs_env, has_converged, total_scf_steps)
201 TYPE(qs_environment_type),
POINTER :: qs_env
202 LOGICAL,
INTENT(OUT),
OPTIONAL :: has_converged
203 INTEGER,
INTENT(OUT),
OPTIONAL :: total_scf_steps
205 INTEGER :: ihistory, max_scf_tmp, tsteps
206 LOGICAL :: converged, outer_scf_loop, should_stop
207 LOGICAL,
SAVE :: first_step_flag = .true.
208 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gradient_history, variable_history
209 TYPE(cp_logger_type),
POINTER :: logger
210 TYPE(dft_control_type),
POINTER :: dft_control
211 TYPE(qs_scf_env_type),
POINTER :: scf_env
212 TYPE(scf_control_type),
POINTER :: scf_control
213 TYPE(section_vals_type),
POINTER :: dft_section, input, scf_section
217 cpassert(
ASSOCIATED(qs_env))
218 IF (
PRESENT(has_converged))
THEN
219 has_converged = .false.
221 IF (
PRESENT(total_scf_steps))
THEN
224 CALL get_qs_env(qs_env, scf_env=scf_env, input=input, &
225 dft_control=dft_control, scf_control=scf_control)
226 IF (scf_control%max_scf > 0)
THEN
231 IF (.NOT.
ASSOCIATED(scf_env))
THEN
239 IF ((scf_control%density_guess .EQ.
history_guess) .AND. (first_step_flag))
THEN
240 max_scf_tmp = scf_control%max_scf
241 scf_control%max_scf = 1
242 outer_scf_loop = scf_control%outer_scf%have_scf
243 scf_control%outer_scf%have_scf = .false.
246 IF (.NOT. dft_control%qs_control%cdft)
THEN
247 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
248 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
251 CALL cdft_scf(qs_env=qs_env, should_stop=should_stop)
255 IF (
ASSOCIATED(qs_env%mp2_env)) qs_env%mp2_env%hf_fail = .NOT. converged
258 IF (scf_control%outer_scf%have_scf)
THEN
259 ihistory = scf_env%outer_scf%iter_count
260 CALL get_qs_env(qs_env, gradient_history=gradient_history, &
261 variable_history=variable_history)
263 gradient_history(:, 1) = gradient_history(:, 2)
264 gradient_history(:, 2) = scf_env%outer_scf%gradient(:, ihistory)
265 variable_history(:, 1) = variable_history(:, 2)
266 variable_history(:, 2) = scf_env%outer_scf%variables(:, ihistory)
268 IF (used_history) used_history = .false.
270 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian))
THEN
271 scf_control%outer_scf%cdft_opt_control%ijacobian(2) = scf_control%outer_scf%cdft_opt_control%ijacobian(2) + 1
272 IF (scf_control%outer_scf%cdft_opt_control%ijacobian(2) .GE. &
273 scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) .AND. &
274 scf_control%outer_scf%cdft_opt_control%jacobian_freq(2) > 0) &
275 scf_env%outer_scf%deallocate_jacobian = .true.
279 IF ((
ASSOCIATED(qs_env%wf_history)) .AND. &
281 (.NOT. first_step_flag)))
THEN
282 IF (.NOT. dft_control%qs_control%cdft)
THEN
283 CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
285 IF (dft_control%qs_control%cdft_control%should_purge)
THEN
288 dft_control%qs_control%cdft_control%should_purge = .false.
290 CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
293 ELSE IF ((scf_control%density_guess .EQ.
history_guess) .AND. &
294 (first_step_flag))
THEN
295 scf_control%max_scf = max_scf_tmp
296 scf_control%outer_scf%have_scf = outer_scf_loop
297 first_step_flag = .false.
305 IF (dft_control%qs_control%cdft) &
306 CALL cdft_control_cleanup(dft_control%qs_control%cdft_control)
308 IF (
PRESENT(has_converged))
THEN
309 has_converged = converged
311 IF (
PRESENT(total_scf_steps))
THEN
312 total_scf_steps = tsteps
336 SUBROUTINE scf_env_do_scf(scf_env, scf_control, qs_env, converged, should_stop, total_scf_steps)
338 TYPE(qs_scf_env_type),
POINTER :: scf_env
339 TYPE(scf_control_type),
POINTER :: scf_control
340 TYPE(qs_environment_type),
POINTER :: qs_env
341 LOGICAL,
INTENT(OUT) :: converged, should_stop
342 INTEGER,
INTENT(OUT) :: total_scf_steps
344 CHARACTER(LEN=*),
PARAMETER :: routinen =
'scf_env_do_scf'
346 CHARACTER(LEN=default_string_length) :: description, name
347 INTEGER :: ext_master_id, handle, handle2, i_tmp, &
348 ic, ispin, iter_count, output_unit, &
349 scf_energy_message_tag, total_steps
350 LOGICAL :: diis_step, do_kpoints, energy_only, exit_inner_loop, exit_outer_loop, &
351 inner_loop_converged, just_energy, outer_loop_converged
352 REAL(kind=
dp) :: t1, t2
353 REAL(kind=
dp),
DIMENSION(3) :: res_val_3
354 TYPE(atomic_kind_type),
DIMENSION(:),
POINTER :: atomic_kind_set
355 TYPE(cp_logger_type),
POINTER :: logger
356 TYPE(cp_result_type),
POINTER :: results
357 TYPE(dbcsr_p_type),
DIMENSION(:, :),
POINTER :: rho_ao_kp
358 TYPE(dft_control_type),
POINTER :: dft_control
359 TYPE(energy_correction_type),
POINTER :: ec_env
360 TYPE(kpoint_type),
POINTER :: kpoints
361 TYPE(mo_set_type),
DIMENSION(:),
POINTER :: mos, mos_last_converged
362 TYPE(mp_comm_type) :: external_comm
363 TYPE(mp_para_env_type),
POINTER :: para_env
364 TYPE(particle_type),
DIMENSION(:),
POINTER :: particle_set
365 TYPE(pw_env_type),
POINTER :: pw_env
366 TYPE(qs_charges_type),
POINTER :: qs_charges
367 TYPE(qs_energy_type),
POINTER :: energy
368 TYPE(qs_kind_type),
DIMENSION(:),
POINTER :: qs_kind_set
369 TYPE(qs_ks_env_type),
POINTER :: ks_env
370 TYPE(qs_rho_type),
POINTER :: rho
371 TYPE(section_vals_type),
POINTER :: dft_section, input, scf_section
373 CALL timeset(routinen, handle)
375 NULLIFY (dft_control, rho, energy, &
376 logger, qs_charges, ks_env, mos, atomic_kind_set, qs_kind_set, &
377 particle_set, dft_section, input, &
378 scf_section, para_env, results, kpoints, pw_env, rho_ao_kp, mos_last_converged)
380 cpassert(
ASSOCIATED(scf_env))
381 cpassert(
ASSOCIATED(qs_env))
388 particle_set=particle_set, &
389 qs_charges=qs_charges, &
391 atomic_kind_set=atomic_kind_set, &
392 qs_kind_set=qs_kind_set, &
396 dft_control=dft_control, &
397 do_kpoints=do_kpoints, &
411 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
412 "SCF WAVEFUNCTION OPTIMIZATION"
415 IF (dft_control%switch_surf_dip)
THEN
416 CALL get_qs_env(qs_env, mos_last_converged=mos_last_converged)
417 DO ispin = 1, dft_control%nspins
420 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
421 "COPIED mos_last_converged ---> mos"
424 IF ((output_unit > 0) .AND. (.NOT. scf_control%use_ot))
THEN
425 WRITE (unit=output_unit, &
426 fmt=
"(/,T3,A,T12,A,T31,A,T39,A,T59,A,T75,A,/,T3,A)") &
427 "Step",
"Update method",
"Time",
"Convergence",
"Total energy",
"Change", &
433 res_val_3(:) = -1.0_dp
434 description =
"[EXT_SCF_ENER_COMM]"
436 CALL get_results(results, description=description, &
437 values=res_val_3, n_entries=i_tmp)
438 cpassert(i_tmp .EQ. 3)
439 IF (all(res_val_3(:) .LE. 0.0)) &
440 CALL cp_abort(__location__, &
441 " Trying to access result ("//trim(description)// &
442 ") which is not correctly stored.")
443 CALL external_comm%set_handle(nint(res_val_3(1)))
445 ext_master_id = nint(res_val_3(2))
446 scf_energy_message_tag = nint(res_val_3(3))
450 scf_env%outer_scf%iter_count = 0
453 energy%tot_old = 0.0_dp
457 CALL init_scf_loop(scf_env=scf_env, qs_env=qs_env, &
458 scf_section=scf_section)
461 energy_only, just_energy, exit_inner_loop)
464 dft_control%surf_dip_correct_switch = dft_control%correct_surf_dip
465 IF ((dft_control%correct_surf_dip) .AND. (scf_control%outer_scf%have_scf) .AND. &
466 (scf_env%outer_scf%iter_count > floor(scf_control%outer_scf%max_scf/2.0_dp)))
THEN
467 IF (dft_control%switch_surf_dip)
THEN
468 dft_control%surf_dip_correct_switch = .false.
469 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
470 "SURFACE DIPOLE CORRECTION switched off"
475 CALL timeset(routinen//
"_inner_loop", handle2)
477 scf_env%iter_count = scf_env%iter_count + 1
478 iter_count = iter_count + 1
479 CALL cp_iterate(logger%iter_info, last=.false., iter_nr=iter_count)
481 IF (output_unit > 0)
CALL m_flush(output_unit)
483 total_steps = total_steps + 1
484 just_energy = energy_only
487 calculate_forces=.false.)
497 CALL qs_scf_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
509 IF (.NOT. just_energy) energy%tot_old = energy%total
512 IF (scf_energy_message_tag .GT. 0)
THEN
513 CALL external_comm%send(energy%total, ext_master_id, scf_energy_message_tag)
517 inner_loop_converged, output_unit)
521 IF (exit_inner_loop)
THEN
526 outer_loop_converged, exit_outer_loop)
529 IF (exit_outer_loop)
CALL cp_iterate(logger%iter_info, last=.true., iter_nr=iter_count)
541 IF (exit_inner_loop)
THEN
542 CALL timestop(handle2)
547 scf_section,
"PRINT%ITERATION_INFO/TIME_CUMUL"),
cp_p_file)) &
551 IF (scf_env%mixing_method > 0)
THEN
552 DO ic = 1,
SIZE(rho_ao_kp, 2)
553 DO ispin = 1, dft_control%nspins
554 CALL dbcsr_get_info(rho_ao_kp(ispin, ic)%matrix, name=name)
555 CALL dbcsr_copy(rho_ao_kp(ispin, ic)%matrix, scf_env%p_mix_new(ispin, ic)%matrix, name=name)
563 CALL timestop(handle2)
567 IF (.NOT. scf_control%outer_scf%have_scf)
EXIT scf_outer_loop
571 energy, total_steps, should_stop, outer_loop_converged)
574 IF (exit_outer_loop)
THEN
575 IF ((dft_control%switch_surf_dip) .AND. (outer_loop_converged) .AND. &
576 (dft_control%surf_dip_correct_switch))
THEN
577 DO ispin = 1, dft_control%nspins
580 IF (output_unit > 0)
WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
581 "COPIED mos ---> mos_last_converged"
585 IF (exit_outer_loop)
EXIT scf_outer_loop
592 END DO scf_outer_loop
594 converged = inner_loop_converged .AND. outer_loop_converged
595 total_scf_steps = total_steps
597 IF (dft_control%qs_control%cdft) &
598 dft_control%qs_control%cdft_control%total_steps = &
599 dft_control%qs_control%cdft_control%total_steps + total_steps
601 IF (.NOT. converged)
THEN
602 IF (scf_control%ignore_convergence_failure .OR. should_stop)
THEN
603 CALL cp_warn(__location__,
"SCF run NOT converged")
605 CALL cp_abort(__location__, &
606 "SCF run NOT converged. To continue the calculation "// &
607 "regardless, please set the keyword IGNORE_CONVERGENCE_FAILURE.")
612 IF (qs_env%energy_correction)
THEN
614 ec_env%do_skip = .false.
615 IF (ec_env%skip_ec .AND. .NOT. converged) ec_env%do_skip = .true.
619 DO ispin = 1,
SIZE(mos)
620 IF (mos(ispin)%use_mo_coeff_b)
THEN
621 IF (.NOT.
ASSOCIATED(mos(ispin)%mo_coeff_b)) &
622 cpabort(
"mo_coeff_b is not allocated")
629 CALL timestop(handle)
643 SUBROUTINE init_scf_loop(scf_env, qs_env, scf_section)
645 TYPE(qs_scf_env_type),
POINTER :: scf_env
646 TYPE(qs_environment_type),
POINTER :: qs_env
647 TYPE(section_vals_type),
POINTER :: scf_section
649 CHARACTER(LEN=*),
PARAMETER :: routinen =
'init_scf_loop'
651 INTEGER :: handle, ispin, nmo, number_of_ot_envs
652 LOGICAL :: do_kpoints, do_rotation, &
653 has_unit_metric, is_full_all
654 TYPE(cp_fm_type),
POINTER :: mo_coeff
655 TYPE(dbcsr_p_type),
DIMENSION(:),
POINTER :: matrix_ks, matrix_s
656 TYPE(dbcsr_type),
POINTER :: orthogonality_metric
657 TYPE(dft_control_type),
POINTER :: dft_control
658 TYPE(kpoint_type),
POINTER :: kpoints
659 TYPE(mo_set_type),
DIMENSION(:),
POINTER :: mos
660 TYPE(scf_control_type),
POINTER :: scf_control
662 CALL timeset(routinen, handle)
664 NULLIFY (scf_control, matrix_s, matrix_ks, dft_control, mos, mo_coeff, kpoints)
666 cpassert(
ASSOCIATED(scf_env))
667 cpassert(
ASSOCIATED(qs_env))
670 scf_control=scf_control, &
671 dft_control=dft_control, &
672 do_kpoints=do_kpoints, &
677 DO ispin = 1,
SIZE(mos)
678 IF (mos(1)%use_mo_coeff_b)
THEN
684 DO ispin = 1, dft_control%nspins
686 IF (.NOT. scf_control%diagonalization%mom) &
687 CALL set_mo_occupation(mo_set=mos(ispin), &
688 smear=scf_control%smear)
691 SELECT CASE (scf_env%method)
694 cpabort(
"unknown scf method method:"//cp_to_string(scf_env%method))
698 IF (.NOT. scf_env%skip_diis)
THEN
699 IF (.NOT.
ASSOCIATED(scf_env%scf_diis_buffer))
THEN
700 ALLOCATE (scf_env%scf_diis_buffer)
701 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
707 IF (.NOT. scf_env%skip_diis)
THEN
709 IF (.NOT.
ASSOCIATED(kpoints%scf_diis_buffer))
THEN
710 ALLOCATE (kpoints%scf_diis_buffer)
715 IF (.NOT.
ASSOCIATED(scf_env%scf_diis_buffer))
THEN
716 ALLOCATE (scf_env%scf_diis_buffer)
717 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
724 CALL get_qs_env(qs_env, matrix_ks=matrix_ks, matrix_s=matrix_s)
726 IF (.NOT. scf_env%skip_diis)
THEN
727 IF (.NOT.
ASSOCIATED(scf_env%scf_diis_buffer))
THEN
728 ALLOCATE (scf_env%scf_diis_buffer)
729 CALL qs_diis_b_create(scf_env%scf_diis_buffer, nbuffer=scf_control%max_diis)
735 IF (dft_control%qs_control%dftb .OR. &
736 dft_control%qs_control%xtb .OR. &
737 dft_control%qs_control%semi_empirical)
THEN
738 cpabort(
"DFTB and SE not available with OT/DIAG")
744 scf_control%diagonalization%ot_settings%preconditioner_type, &
748 scf_control%diagonalization%ot_settings%preconditioner_type, &
749 scf_control%diagonalization%ot_settings%precond_solver_type, &
750 scf_control%diagonalization%ot_settings%energy_gap, dft_control%nspins)
756 has_unit_metric=has_unit_metric, &
764 IF (scf_control%do_outer_scf_reortho)
THEN
765 IF (scf_control%outer_scf%have_scf .AND. .NOT. dft_control%restricted)
THEN
766 IF (scf_env%outer_scf%iter_count > 0)
THEN
767 DO ispin = 1, dft_control%nspins
768 CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
769 IF (has_unit_metric)
THEN
781 IF (.NOT.
ASSOCIATED(scf_env%qs_ot_env))
THEN
784 number_of_ot_envs = dft_control%nspins
785 IF (dft_control%restricted) number_of_ot_envs = 1
787 ALLOCATE (scf_env%qs_ot_env(number_of_ot_envs))
793 IF (dft_control%restricted)
THEN
794 scf_env%qs_ot_env(1)%restricted = .true.
796 IF (.NOT. scf_env%qs_ot_env(1)%settings%do_rotation) &
797 CALL cp_abort(__location__, &
798 "Restricted calculation with OT requires orbital rotation. Please "// &
799 "activate the OT%ROTATION keyword!")
801 scf_env%qs_ot_env(:)%restricted = .false.
806 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
809 IF (do_rotation .AND. is_full_all) &
810 cpabort(
'PRECONDITIONER FULL_ALL is not compatible with ROTATION.')
814 calculate_forces=.false.)
818 IF (.NOT. reuse_precond) &
820 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
827 IF (has_unit_metric)
THEN
828 NULLIFY (orthogonality_metric)
830 orthogonality_metric => matrix_s(1)%matrix
833 IF (.NOT. reuse_precond) &
835 scf_env%qs_ot_env(1)%settings%preconditioner_type, &
836 scf_env%qs_ot_env(1)%settings%precond_solver_type, &
837 scf_env%qs_ot_env(1)%settings%energy_gap, dft_control%nspins, &
838 has_unit_metric=has_unit_metric, &
839 chol_type=scf_env%qs_ot_env(1)%settings%cholesky_type)
840 IF (reuse_precond) reuse_precond = .false.
842 CALL ot_scf_init(mo_array=mos, matrix_s=orthogonality_metric, &
843 broyden_adaptive_sigma=qs_env%broyden_adaptive_sigma, &
844 qs_ot_env=scf_env%qs_ot_env, matrix_ks=matrix_ks(1)%matrix)
846 SELECT CASE (scf_env%qs_ot_env(1)%settings%preconditioner_type)
849 DO ispin = 1,
SIZE(scf_env%qs_ot_env)
851 scf_env%ot_preconditioner(ispin)%preconditioner)
854 DO ispin = 1,
SIZE(scf_env%qs_ot_env)
856 scf_env%ot_preconditioner(1)%preconditioner)
859 DO ispin = 1,
SIZE(scf_env%qs_ot_env)
861 scf_env%ot_preconditioner(1)%preconditioner)
867 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
868 DO ispin = 1,
SIZE(mos)
869 IF (.NOT. mos(ispin)%uniform_occupation)
THEN
870 cpassert(do_rotation)
876 IF (dft_control%low_spin_roks)
THEN
878 do_rotation = scf_env%qs_ot_env(1)%settings%do_rotation
879 cpassert(do_rotation)
882 CALL timestop(handle)
884 END SUBROUTINE init_scf_loop
895 TYPE(qs_scf_env_type),
INTENT(INOUT) :: scf_env
897 CHARACTER(len=*),
PARAMETER :: routinen =
'scf_env_cleanup'
901 CALL timeset(routinen, handle)
904 CALL cp_fm_release(scf_env%scf_work1)
906 IF (
ASSOCIATED(scf_env%scf_work2))
THEN
907 CALL cp_fm_release(scf_env%scf_work2)
908 DEALLOCATE (scf_env%scf_work2)
910 IF (
ASSOCIATED(scf_env%ortho))
THEN
911 CALL cp_fm_release(scf_env%ortho)
912 DEALLOCATE (scf_env%ortho)
914 IF (
ASSOCIATED(scf_env%ortho_m1))
THEN
915 CALL cp_fm_release(scf_env%ortho_m1)
916 DEALLOCATE (scf_env%ortho_m1)
919 IF (
ASSOCIATED(scf_env%ortho_dbcsr))
THEN
920 CALL dbcsr_deallocate_matrix(scf_env%ortho_dbcsr)
922 IF (
ASSOCIATED(scf_env%buf1_dbcsr))
THEN
923 CALL dbcsr_deallocate_matrix(scf_env%buf1_dbcsr)
925 IF (
ASSOCIATED(scf_env%buf2_dbcsr))
THEN
926 CALL dbcsr_deallocate_matrix(scf_env%buf2_dbcsr)
929 IF (
ASSOCIATED(scf_env%p_mix_new))
THEN
933 IF (
ASSOCIATED(scf_env%p_delta))
THEN
938 SELECT CASE (scf_env%method)
953 cpabort(
"unknown scf method method:"//cp_to_string(scf_env%method))
956 IF (
ASSOCIATED(scf_env%outer_scf%variables))
THEN
957 DEALLOCATE (scf_env%outer_scf%variables)
959 IF (
ASSOCIATED(scf_env%outer_scf%count))
THEN
960 DEALLOCATE (scf_env%outer_scf%count)
962 IF (
ASSOCIATED(scf_env%outer_scf%gradient))
THEN
963 DEALLOCATE (scf_env%outer_scf%gradient)
965 IF (
ASSOCIATED(scf_env%outer_scf%energy))
THEN
966 DEALLOCATE (scf_env%outer_scf%energy)
968 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. &
969 scf_env%outer_scf%deallocate_jacobian)
THEN
970 DEALLOCATE (scf_env%outer_scf%inv_jacobian)
973 CALL timestop(handle)
986 TYPE(qs_environment_type),
POINTER :: qs_env
987 LOGICAL,
INTENT(OUT) :: should_stop
989 CHARACTER(len=*),
PARAMETER :: routinen =
'cdft_scf'
991 INTEGER :: handle, iatom, ispin, ivar, nmo, nvar, &
993 LOGICAL :: cdft_loop_converged, converged, &
994 exit_cdft_loop, first_iteration, &
995 my_uocc, uniform_occupation
996 REAL(kind=
dp),
DIMENSION(:),
POINTER :: mo_occupations
997 TYPE(cdft_control_type),
POINTER :: cdft_control
998 TYPE(cp_logger_type),
POINTER :: logger
999 TYPE(dbcsr_p_type),
DIMENSION(:),
POINTER :: matrix_s, rho_ao
1000 TYPE(dft_control_type),
POINTER :: dft_control
1001 TYPE(mo_set_type),
DIMENSION(:),
POINTER :: mos
1002 TYPE(pw_env_type),
POINTER :: pw_env
1003 TYPE(pw_pool_type),
POINTER :: auxbas_pw_pool
1004 TYPE(qs_energy_type),
POINTER :: energy
1005 TYPE(qs_ks_env_type),
POINTER :: ks_env
1006 TYPE(qs_rho_type),
POINTER :: rho
1007 TYPE(qs_scf_env_type),
POINTER :: scf_env
1008 TYPE(scf_control_type),
POINTER :: scf_control
1009 TYPE(section_vals_type),
POINTER :: dft_section, input, scf_section
1011 NULLIFY (scf_env, ks_env, energy, rho, matrix_s, rho_ao, cdft_control, logger, &
1012 dft_control, pw_env, auxbas_pw_pool, energy, ks_env, scf_env, dft_section, &
1013 input, scf_section, scf_control, mos, mo_occupations)
1016 cpassert(
ASSOCIATED(qs_env))
1017 CALL get_qs_env(qs_env, scf_env=scf_env, energy=energy, &
1018 dft_control=dft_control, scf_control=scf_control, &
1019 ks_env=ks_env, input=input)
1021 CALL timeset(routinen//
"_loop", handle)
1025 extension=
".scfLog")
1026 first_iteration = .true.
1028 cdft_control => dft_control%qs_control%cdft_control
1030 scf_env%outer_scf%iter_count = 0
1031 cdft_control%total_steps = 0
1034 IF (output_unit > 0)
THEN
1035 WRITE (unit=output_unit, fmt=
"(/,/,T2,A)") &
1036 "CDFT EXTERNAL SCF WAVEFUNCTION OPTIMIZATION"
1039 IF (cdft_control%reuse_precond)
THEN
1040 reuse_precond = .false.
1041 cdft_control%nreused = 0
1047 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
1048 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
1050 IF (cdft_control%reuse_precond)
THEN
1054 IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count == 1 &
1055 .AND. cdft_control%total_steps /= 1) &
1056 cdft_control%nreused = cdft_control%nreused - 1
1058 IF (scf_env%outer_scf%iter_count == 1 .AND. scf_env%iter_count .LE. cdft_control%precond_freq .AND. &
1059 cdft_control%total_steps /= 1 .AND. cdft_control%nreused .LT. cdft_control%max_reuse)
THEN
1060 reuse_precond = .true.
1061 cdft_control%nreused = cdft_control%nreused + 1
1063 reuse_precond = .false.
1064 cdft_control%nreused = 0
1068 IF (first_iteration .AND. cdft_control%purge_history)
THEN
1069 cdft_control%istep = cdft_control%istep + 1
1070 IF (scf_env%outer_scf%iter_count .GT. 1)
THEN
1071 cdft_control%nbad_conv = cdft_control%nbad_conv + 1
1072 IF (cdft_control%nbad_conv .GE. cdft_control%purge_freq .AND. &
1073 cdft_control%istep .GE. cdft_control%purge_offset)
THEN
1074 cdft_control%nbad_conv = 0
1075 cdft_control%istep = 0
1076 cdft_control%should_purge = .true.
1080 first_iteration = .false.
1084 cdft_loop_converged, exit_cdft_loop)
1086 energy, cdft_control%total_steps, &
1087 should_stop, cdft_loop_converged, cdft_loop=.true.)
1088 IF (exit_cdft_loop)
EXIT cdft_outer_loop
1090 CALL qs_calculate_inverse_jacobian(qs_env)
1092 CALL qs_cdft_line_search(qs_env)
1097 END DO cdft_outer_loop
1099 cdft_control%ienergy = cdft_control%ienergy + 1
1102 IF (cdft_control%do_et)
THEN
1103 CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, mos=mos)
1104 nvar =
SIZE(cdft_control%target)
1106 ALLOCATE (cdft_control%wmat(nvar))
1108 CALL dbcsr_init_p(cdft_control%wmat(ivar)%matrix)
1109 CALL dbcsr_copy(cdft_control%wmat(ivar)%matrix, matrix_s(1)%matrix, &
1110 name=
"ET_RESTRAINT_MATRIX")
1111 CALL dbcsr_set(cdft_control%wmat(ivar)%matrix, 0.0_dp)
1112 CALL integrate_v_rspace(cdft_control%group(ivar)%weight, &
1113 hmat=cdft_control%wmat(ivar), qs_env=qs_env, &
1114 calculate_forces=.false., &
1115 gapw=dft_control%qs_control%gapw)
1118 CALL dbcsr_init_p(cdft_control%matrix_s%matrix)
1119 CALL dbcsr_copy(cdft_control%matrix_s%matrix, matrix_s(1)%matrix, &
1122 NULLIFY (cdft_control%mo_coeff)
1123 ALLOCATE (cdft_control%mo_coeff(dft_control%nspins))
1124 DO ispin = 1, dft_control%nspins
1125 CALL cp_fm_create(matrix=cdft_control%mo_coeff(ispin), &
1126 matrix_struct=qs_env%mos(ispin)%mo_coeff%matrix_struct, &
1127 name=
"MO_COEFF_A"//trim(adjustl(cp_to_string(ispin)))//
"MATRIX")
1128 CALL cp_fm_to_fm(qs_env%mos(ispin)%mo_coeff, &
1129 cdft_control%mo_coeff(ispin))
1132 IF (cdft_control%calculate_metric)
THEN
1135 ALLOCATE (cdft_control%matrix_p(dft_control%nspins))
1136 DO ispin = 1, dft_control%nspins
1137 NULLIFY (cdft_control%matrix_p(ispin)%matrix)
1138 CALL dbcsr_init_p(cdft_control%matrix_p(ispin)%matrix)
1139 CALL dbcsr_copy(cdft_control%matrix_p(ispin)%matrix, rho_ao(ispin)%matrix, &
1140 name=
"DENSITY MATRIX")
1144 uniform_occupation = .true.
1145 DO ispin = 1, dft_control%nspins
1146 CALL get_mo_set(mo_set=mos(ispin), uniform_occupation=my_uocc)
1147 uniform_occupation = uniform_occupation .AND. my_uocc
1149 IF (.NOT. uniform_occupation)
THEN
1150 ALLOCATE (cdft_control%occupations(dft_control%nspins))
1151 DO ispin = 1, dft_control%nspins
1154 occupation_numbers=mo_occupations)
1155 ALLOCATE (cdft_control%occupations(ispin)%array(nmo))
1156 cdft_control%occupations(ispin)%array(1:nmo) = mo_occupations(1:nmo)
1164 IF (.NOT. (cdft_control%save_pot .OR. cdft_control%transfer_pot))
THEN
1166 CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
1167 DO iatom = 1,
SIZE(cdft_control%group)
1168 CALL auxbas_pw_pool%give_back_pw(cdft_control%group(iatom)%weight)
1169 DEALLOCATE (cdft_control%group(iatom)%weight)
1171 IF (cdft_control%atomic_charges)
THEN
1172 DO iatom = 1, cdft_control%natoms
1173 CALL auxbas_pw_pool%give_back_pw(cdft_control%charge(iatom))
1175 DEALLOCATE (cdft_control%charge)
1178 cdft_control%becke_control%cavity_confine)
THEN
1179 IF (.NOT.
ASSOCIATED(cdft_control%becke_control%cavity_mat))
THEN
1180 CALL auxbas_pw_pool%give_back_pw(cdft_control%becke_control%cavity)
1182 DEALLOCATE (cdft_control%becke_control%cavity_mat)
1185 IF (
ASSOCIATED(cdft_control%hirshfeld_control%hirshfeld_env%fnorm))
THEN
1186 CALL auxbas_pw_pool%give_back_pw(cdft_control%hirshfeld_control%hirshfeld_env%fnorm)
1189 IF (
ASSOCIATED(cdft_control%charges_fragment))
DEALLOCATE (cdft_control%charges_fragment)
1190 cdft_control%need_pot = .true.
1191 cdft_control%external_control = .false.
1194 CALL timestop(handle)
1205 SUBROUTINE cdft_control_cleanup(cdft_control)
1206 TYPE(cdft_control_type),
POINTER :: cdft_control
1208 IF (
ASSOCIATED(cdft_control%constraint%variables)) &
1209 DEALLOCATE (cdft_control%constraint%variables)
1210 IF (
ASSOCIATED(cdft_control%constraint%count)) &
1211 DEALLOCATE (cdft_control%constraint%count)
1212 IF (
ASSOCIATED(cdft_control%constraint%gradient)) &
1213 DEALLOCATE (cdft_control%constraint%gradient)
1214 IF (
ASSOCIATED(cdft_control%constraint%energy)) &
1215 DEALLOCATE (cdft_control%constraint%energy)
1216 IF (
ASSOCIATED(cdft_control%constraint%inv_jacobian) .AND. &
1217 cdft_control%constraint%deallocate_jacobian) &
1218 DEALLOCATE (cdft_control%constraint%inv_jacobian)
1220 END SUBROUTINE cdft_control_cleanup
1228 SUBROUTINE qs_calculate_inverse_jacobian(qs_env)
1229 TYPE(qs_environment_type),
POINTER :: qs_env
1231 CHARACTER(LEN=*),
PARAMETER :: routinen =
'qs_calculate_inverse_jacobian'
1233 CHARACTER(len=default_path_length) :: project_name
1234 INTEGER :: counter, handle, i, ispin, iter_count, &
1235 iwork, j, max_scf, nspins, nsteps, &
1236 nvar, nwork, output_unit, pwork, &
1238 LOGICAL :: converged, explicit_jacobian, &
1239 should_build, should_stop, &
1241 REAL(kind=
dp) :: inv_error, step_size
1242 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: coeff, dh, step_multiplier
1243 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :) :: jacobian
1244 REAL(kind=
dp),
DIMENSION(:),
POINTER :: energy
1245 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gradient, inv_jacobian
1246 TYPE(cdft_control_type),
POINTER :: cdft_control
1247 TYPE(cp_logger_type),
POINTER :: logger, tmp_logger
1248 TYPE(dbcsr_p_type),
DIMENSION(:),
POINTER :: p_rmpv
1249 TYPE(dbcsr_p_type),
DIMENSION(:, :),
POINTER :: rho_ao_kp
1250 TYPE(dft_control_type),
POINTER :: dft_control
1251 TYPE(mo_set_type),
ALLOCATABLE,
DIMENSION(:) :: mos_stashed
1252 TYPE(mo_set_type),
DIMENSION(:),
POINTER :: mos
1253 TYPE(mp_para_env_type),
POINTER :: para_env
1254 TYPE(qs_energy_type),
POINTER :: energy_qs
1255 TYPE(qs_ks_env_type),
POINTER :: ks_env
1256 TYPE(qs_rho_type),
POINTER :: rho
1257 TYPE(qs_scf_env_type),
POINTER :: scf_env
1258 TYPE(scf_control_type),
POINTER :: scf_control
1260 NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
1261 ks_env, scf_env, scf_control, dft_control, cdft_control, &
1262 inv_jacobian, para_env, tmp_logger, energy_qs)
1265 cpassert(
ASSOCIATED(qs_env))
1266 CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
1267 scf_control=scf_control, mos=mos, rho=rho, &
1268 dft_control=dft_control, &
1269 para_env=para_env, energy=energy_qs)
1270 explicit_jacobian = .false.
1271 should_build = .false.
1272 use_md_history = .false.
1273 iter_count = scf_env%outer_scf%iter_count
1275 IF (.NOT.
ASSOCIATED(scf_control%outer_scf%cdft_opt_control))
RETURN
1277 CALL timeset(routinen, handle)
1279 IF (scf_control%outer_scf%cdft_opt_control%jacobian_restart)
THEN
1281 should_build = .false.
1284 IF (should_build)
THEN
1285 scf_env%outer_scf%deallocate_jacobian = .false.
1287 IF (explicit_jacobian)
THEN
1289 cdft_control => dft_control%qs_control%cdft_control
1290 IF (.NOT.
ASSOCIATED(cdft_control)) &
1291 CALL cp_abort(__location__, &
1292 "Optimizers that need the explicit Jacobian can"// &
1293 " only be used together with a valid CDFT constraint.")
1295 project_name = logger%iter_info%project_name
1296 CALL create_tmp_logger(para_env, project_name,
"-JacobianInfo.out", output_unit, tmp_logger)
1298 nspins = dft_control%nspins
1299 ALLOCATE (mos_stashed(nspins))
1300 DO ispin = 1, nspins
1304 p_rmpv => rho_ao_kp(:, 1)
1306 nvar =
SIZE(scf_env%outer_scf%variables, 1)
1307 max_scf = scf_control%outer_scf%max_scf + 1
1308 ALLOCATE (gradient(nvar, max_scf))
1309 gradient = scf_env%outer_scf%gradient
1310 ALLOCATE (energy(max_scf))
1311 energy = scf_env%outer_scf%energy
1312 ALLOCATE (jacobian(nvar, nvar))
1314 nsteps = cdft_control%total_steps
1317 twork = pwork - nwork
1319 jacobian(i, :) = coeff(0)*scf_env%outer_scf%gradient(i, iter_count)
1324 IF (output_unit > 0)
THEN
1325 WRITE (output_unit, fmt=
"(A)")
" "
1326 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1327 WRITE (output_unit,
'(A,I3,A,I3,A)') &
1328 " ### Constraint ", i,
" of ", nvar,
" ###"
1329 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1332 DO iwork = nwork, pwork
1333 IF (iwork == 0) cycle
1334 counter = counter + 1
1335 IF (output_unit > 0)
THEN
1336 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1337 WRITE (output_unit,
'(A,I3,A,I3,A)') &
1338 " ### Energy evaluation ", counter,
" of ", twork,
" ###"
1339 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1341 IF (
SIZE(scf_control%outer_scf%cdft_opt_control%jacobian_step) == 1)
THEN
1342 step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(1)
1344 step_size = scf_control%outer_scf%cdft_opt_control%jacobian_step(i)
1346 scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count)
1347 scf_env%outer_scf%variables(i, iter_count + 1) = scf_env%outer_scf%variables(i, iter_count) + &
1348 step_multiplier(iwork)*step_size
1352 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
1353 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
1356 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
1359 energy_qs, cdft_control%total_steps, &
1360 should_stop=.false., outer_loop_converged=.false., cdft_loop=.false.)
1361 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
1364 jacobian(j, i) = jacobian(j, i) + coeff(iwork)*scf_env%outer_scf%gradient(j, iter_count + 1)
1367 scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
1368 scf_env%outer_scf%gradient = gradient
1369 scf_env%outer_scf%energy = energy
1370 cdft_control%total_steps = nsteps
1371 DO ispin = 1, nspins
1374 CALL calculate_density_matrix(mos(ispin), &
1375 p_rmpv(ispin)%matrix)
1386 jacobian(i, j) = jacobian(i, j)/dh(j)
1389 IF (.NOT.
ASSOCIATED(scf_env%outer_scf%inv_jacobian)) &
1390 ALLOCATE (scf_env%outer_scf%inv_jacobian(nvar, nvar))
1391 inv_jacobian => scf_env%outer_scf%inv_jacobian
1392 CALL invert_matrix(jacobian, inv_jacobian, inv_error)
1393 scf_control%outer_scf%cdft_opt_control%broyden_update = .false.
1395 DO ispin = 1, nspins
1398 DEALLOCATE (mos_stashed, jacobian, gradient, energy, coeff, step_multiplier, dh)
1399 IF (output_unit > 0)
THEN
1400 WRITE (output_unit, fmt=
"(/,A)") &
1401 " ================================== JACOBIAN CALCULATED =================================="
1409 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian) .AND. para_env%is_source())
THEN
1414 scf_control%outer_scf%cdft_opt_control%ijacobian(1) = scf_control%outer_scf%cdft_opt_control%ijacobian(1) + 1
1415 CALL timestop(handle)
1417 END SUBROUTINE qs_calculate_inverse_jacobian
1427 SUBROUTINE qs_cdft_line_search(qs_env)
1428 TYPE(qs_environment_type),
POINTER :: qs_env
1430 CHARACTER(LEN=*),
PARAMETER :: routinen =
'qs_cdft_line_search'
1432 CHARACTER(len=default_path_length) :: project_name
1433 INTEGER :: handle, i, ispin, iter_count, &
1434 max_linesearch, max_scf, nspins, &
1435 nsteps, nvar, output_unit, tsteps
1436 LOGICAL :: continue_ls, continue_ls_exit, converged, do_linesearch, found_solution, &
1437 reached_maxls, should_exit, should_stop, sign_changed
1438 LOGICAL,
ALLOCATABLE,
DIMENSION(:) :: positive_sign
1439 REAL(kind=
dp) :: alpha, alpha_ls, factor, norm_ls
1440 REAL(kind=
dp),
DIMENSION(:),
POINTER :: energy
1441 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: gradient, inv_jacobian
1442 REAL(kind=
dp),
EXTERNAL :: dnrm2
1443 TYPE(cdft_control_type),
POINTER :: cdft_control
1444 TYPE(cp_logger_type),
POINTER :: logger, tmp_logger
1445 TYPE(dbcsr_p_type),
DIMENSION(:),
POINTER :: p_rmpv
1446 TYPE(dbcsr_p_type),
DIMENSION(:, :),
POINTER :: rho_ao_kp
1447 TYPE(dft_control_type),
POINTER :: dft_control
1448 TYPE(mo_set_type),
DIMENSION(:),
POINTER :: mos
1449 TYPE(mp_para_env_type),
POINTER :: para_env
1450 TYPE(qs_energy_type),
POINTER :: energy_qs
1451 TYPE(qs_ks_env_type),
POINTER :: ks_env
1452 TYPE(qs_rho_type),
POINTER :: rho
1453 TYPE(qs_scf_env_type),
POINTER :: scf_env
1454 TYPE(scf_control_type),
POINTER :: scf_control
1456 CALL timeset(routinen, handle)
1458 NULLIFY (energy, gradient, p_rmpv, rho_ao_kp, mos, rho, &
1459 ks_env, scf_env, scf_control, dft_control, &
1460 cdft_control, inv_jacobian, para_env, &
1461 tmp_logger, energy_qs)
1464 cpassert(
ASSOCIATED(qs_env))
1465 CALL get_qs_env(qs_env, scf_env=scf_env, ks_env=ks_env, &
1466 scf_control=scf_control, mos=mos, rho=rho, &
1467 dft_control=dft_control, &
1468 para_env=para_env, energy=energy_qs)
1469 do_linesearch = .false.
1470 SELECT CASE (scf_control%outer_scf%optimizer)
1472 do_linesearch = .false.
1474 do_linesearch = .true.
1476 SELECT CASE (scf_control%outer_scf%cdft_opt_control%broyden_type)
1478 do_linesearch = .false.
1480 cdft_control => dft_control%qs_control%cdft_control
1481 IF (.NOT.
ASSOCIATED(cdft_control)) &
1482 CALL cp_abort(__location__, &
1483 "Optimizers that perform a line search can"// &
1484 " only be used together with a valid CDFT constraint")
1485 IF (
ASSOCIATED(scf_env%outer_scf%inv_jacobian)) &
1486 do_linesearch = .true.
1489 IF (do_linesearch)
THEN
1491 TYPE(mo_set_type),
DIMENSION(:),
ALLOCATABLE :: mos_ls, mos_stashed
1492 cdft_control => dft_control%qs_control%cdft_control
1493 IF (.NOT.
ASSOCIATED(cdft_control)) &
1494 CALL cp_abort(__location__, &
1495 "Optimizers that perform a line search can"// &
1496 " only be used together with a valid CDFT constraint")
1497 cpassert(
ASSOCIATED(scf_env%outer_scf%inv_jacobian))
1498 cpassert(
ASSOCIATED(scf_control%outer_scf%cdft_opt_control))
1499 alpha = scf_control%outer_scf%cdft_opt_control%newton_step_save
1500 iter_count = scf_env%outer_scf%iter_count
1502 project_name = logger%iter_info%project_name
1503 CALL create_tmp_logger(para_env, project_name,
"-LineSearch.out", output_unit, tmp_logger)
1505 nspins = dft_control%nspins
1506 ALLOCATE (mos_stashed(nspins))
1507 DO ispin = 1, nspins
1511 p_rmpv => rho_ao_kp(:, 1)
1512 nsteps = cdft_control%total_steps
1514 nvar =
SIZE(scf_env%outer_scf%variables, 1)
1515 max_scf = scf_control%outer_scf%max_scf + 1
1516 max_linesearch = scf_control%outer_scf%cdft_opt_control%max_ls
1517 continue_ls = scf_control%outer_scf%cdft_opt_control%continue_ls
1518 factor = scf_control%outer_scf%cdft_opt_control%factor_ls
1519 continue_ls_exit = .false.
1520 found_solution = .false.
1521 ALLOCATE (gradient(nvar, max_scf))
1522 gradient = scf_env%outer_scf%gradient
1523 ALLOCATE (energy(max_scf))
1524 energy = scf_env%outer_scf%energy
1525 reached_maxls = .false.
1527 IF (scf_control%outer_scf%cdft_opt_control%broyden_update)
THEN
1530 scf_env%outer_scf%variables(:, iter_count + 1) = 0
1531 scf_control%outer_scf%cdft_opt_control%broyden_update = .false.
1534 IF (output_unit > 0)
THEN
1535 WRITE (output_unit, fmt=
"(/,A)") &
1536 " ================================== LINE SEARCH STARTED =================================="
1537 WRITE (output_unit, fmt=
"(A,I5,A)") &
1538 " Evaluating optimal step size for optimizer using a maximum of", max_linesearch,
" steps"
1539 IF (continue_ls)
THEN
1540 WRITE (output_unit, fmt=
"(A)") &
1541 " Line search continues until best step size is found or max steps are reached"
1543 WRITE (output_unit,
'(/,A,F5.3)') &
1544 " Initial step size: ", alpha
1545 WRITE (output_unit,
'(/,A,F5.3)') &
1546 " Step size update factor: ", factor
1547 WRITE (output_unit,
'(/,A,I10,A,I10)') &
1548 " Energy evaluation: ", cdft_control%ienergy,
", CDFT SCF iteration: ", iter_count
1552 DO i = 1, max_linesearch
1553 IF (output_unit > 0)
THEN
1554 WRITE (output_unit, fmt=
"(A)")
" "
1555 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1556 WRITE (output_unit,
'(A,I10,A)') &
1557 " ### Line search step: ", i,
" ###"
1558 WRITE (output_unit, fmt=
"(A)")
" #####################################"
1560 inv_jacobian => scf_env%outer_scf%inv_jacobian
1562 scf_env%outer_scf%variables(:, iter_count + 1) = scf_env%outer_scf%variables(:, iter_count) - alpha* &
1563 matmul(inv_jacobian, scf_env%outer_scf%gradient(:, iter_count))
1568 CALL scf_env_do_scf(scf_env=scf_env, scf_control=scf_control, qs_env=qs_env, &
1569 converged=converged, should_stop=should_stop, total_scf_steps=tsteps)
1572 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count + 1
1575 energy_qs, cdft_control%total_steps, &
1576 should_stop=.false., outer_loop_converged=.false., cdft_loop=.false.)
1577 scf_env%outer_scf%iter_count = scf_env%outer_scf%iter_count - 1
1579 IF (continue_ls .AND. .NOT.
ALLOCATED(positive_sign))
THEN
1580 ALLOCATE (positive_sign(nvar))
1582 positive_sign(ispin) = scf_env%outer_scf%gradient(ispin, iter_count + 1) .GE. 0.0_dp
1586 inv_jacobian => scf_env%outer_scf%inv_jacobian
1587 IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) .LT. &
1588 dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count), 1))
THEN
1590 IF (.NOT. continue_ls)
THEN
1591 should_exit = .true.
1595 IF (found_solution)
THEN
1597 IF (dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1) .GT. norm_ls)
THEN
1599 continue_ls_exit = .true.
1603 IF (.NOT. continue_ls_exit)
THEN
1604 should_exit = .false.
1606 found_solution = .true.
1607 norm_ls = dnrm2(nvar, scf_env%outer_scf%gradient(:, iter_count + 1), 1)
1610 sign_changed = .true.
1612 sign_changed = sign_changed .AND. (positive_sign(ispin) .NEQV. &
1613 scf_env%outer_scf%gradient(ispin, iter_count + 1) .GE. 0.0_dp)
1615 IF (.NOT.
ALLOCATED(mos_ls))
THEN
1616 ALLOCATE (mos_ls(nspins))
1618 DO ispin = 1, nspins
1622 DO ispin = 1, nspins
1625 alpha = alpha*factor
1627 IF (i == max_linesearch) continue_ls_exit = .true.
1629 IF (sqrt(maxval(scf_env%outer_scf%gradient(:, scf_env%outer_scf%iter_count + 1)**2)) .LT. &
1630 scf_control%outer_scf%eps_scf) &
1631 continue_ls_exit = .true.
1633 IF (sign_changed) continue_ls_exit = .true.
1638 should_exit = .false.
1640 alpha = alpha*factor
1642 IF (continue_ls_exit)
THEN
1645 DO ispin = 1, nspins
1648 CALL calculate_density_matrix(mos(ispin), &
1649 p_rmpv(ispin)%matrix)
1655 should_exit = .true.
1658 IF (.NOT. should_exit .AND. &
1659 (i == max_linesearch .AND. converged .AND. .NOT. found_solution))
THEN
1660 should_exit = .true.
1661 reached_maxls = .true.
1662 alpha = alpha*(1.0_dp/factor)
1665 scf_env%outer_scf%variables(:, iter_count + 1) = 0.0_dp
1666 scf_env%outer_scf%gradient = gradient
1667 scf_env%outer_scf%energy = energy
1669 IF (should_exit)
EXIT
1671 cdft_control%total_steps = nsteps
1672 DO ispin = 1, nspins
1675 CALL calculate_density_matrix(mos(ispin), &
1676 p_rmpv(ispin)%matrix)
1681 scf_control%outer_scf%cdft_opt_control%newton_step = alpha
1682 IF (.NOT. should_exit)
THEN
1683 CALL cp_warn(__location__, &
1684 "Line search did not converge. CDFT SCF proceeds with fixed step size.")
1685 scf_control%outer_scf%cdft_opt_control%newton_step = scf_control%outer_scf%cdft_opt_control%newton_step_save
1687 IF (reached_maxls) &
1688 CALL cp_warn(__location__, &
1689 "Line search did not converge. CDFT SCF proceeds with lasted iterated step size.")
1693 DO ispin = 1, nspins
1696 DEALLOCATE (mos_stashed, gradient, energy)
1697 IF (
ALLOCATED(positive_sign))
DEALLOCATE (positive_sign)
1698 IF (output_unit > 0)
THEN
1699 WRITE (output_unit, fmt=
"(/,A)") &
1700 " ================================== LINE SEARCH COMPLETE =================================="
1706 CALL timestop(handle)
1708 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...
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, use_sp)
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.
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 itrative diagonalization by the block-Davidson appr...
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.
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, 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_nonbond, sab_almo, sab_kp, sab_kp_nosym, 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, 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, ecoul_1c, rho0_s_rs, rho0_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, 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, rhs)
Get the QUICKSTEP environment.
subroutine, public set_qs_env(qs_env, super_cell, mos, qmmm, qmmm_periodic, 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, 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, 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, rhs)
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 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...
Definition and initialisation of the mo data type.
subroutine, public write_mo_set_to_restart(mo_array, particle_set, dft_section, qs_kind_set)
...
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)
...
subroutine, public qs_ot_new_preconditioner(qs_ot_env, preconditioner)
...
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_new_mos(qs_env, scf_env, scf_control, scf_section, diis_step, energy_only)
takes known energy and derivatives and produces new wfns and or density matrix
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_check_inner_exit(qs_env, scf_env, scf_control, should_stop, exit_inner_loop, inner_loop_converged, output_unit)
checks whether exit conditions for inner loop are satisfied
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)
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_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
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 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 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