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