Line data Source code
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 Different diagonalization schemes that can be used
10 : !> for the iterative solution of the eigenvalue problem
11 : !> \par History
12 : !> started from routines previously located in the qs_scf module
13 : !> 05.2009
14 : ! **************************************************************************************************
15 : MODULE qs_scf_diagonalization
16 : USE cp_array_utils, ONLY: cp_1d_r_p_type
17 : USE cp_blacs_env, ONLY: cp_blacs_env_type
18 : USE cp_cfm_basic_linalg, ONLY: cp_cfm_column_scale,&
19 : cp_cfm_scale_and_add,&
20 : cp_cfm_scale_and_add_fm,&
21 : cp_cfm_transpose,&
22 : cp_cfm_triangular_multiply
23 : USE cp_cfm_cholesky, ONLY: cp_cfm_cholesky_decompose
24 : USE cp_cfm_diag, ONLY: cp_cfm_diag_workspace_type,&
25 : cp_cfm_geeig,&
26 : cp_cfm_geeig_canon,&
27 : cp_cfm_geeig_canon_local,&
28 : cp_cfm_geeig_local,&
29 : cp_cfm_heevd,&
30 : cp_cfm_local_workspace_prepare
31 : USE cp_cfm_types, ONLY: cp_cfm_create,&
32 : cp_cfm_release,&
33 : cp_cfm_set_all,&
34 : cp_cfm_to_cfm,&
35 : cp_cfm_to_fm,&
36 : cp_cfm_type,&
37 : cp_fm_to_cfm
38 : USE cp_control_types, ONLY: dft_control_type,&
39 : hairy_probes_type
40 : USE cp_dbcsr_api, ONLY: dbcsr_copy,&
41 : dbcsr_create,&
42 : dbcsr_init_p,&
43 : dbcsr_p_type,&
44 : dbcsr_release_p,&
45 : dbcsr_set,&
46 : dbcsr_type,&
47 : dbcsr_type_symmetric
48 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
49 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
50 : copy_fm_to_dbcsr,&
51 : cp_dbcsr_sm_fm_multiply,&
52 : dbcsr_allocate_matrix_set
53 : USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale,&
54 : cp_fm_scale_and_add,&
55 : cp_fm_symm,&
56 : cp_fm_uplo_to_full
57 : USE cp_fm_cholesky, ONLY: cp_fm_cholesky_reduce,&
58 : cp_fm_cholesky_restore
59 : USE cp_fm_diag, ONLY: choose_eigv_solver,&
60 : cp_fm_geeig,&
61 : cp_fm_geeig_canon,&
62 : diag_check_requested,&
63 : diag_lib_explicit,&
64 : direct_generalized_diagonalization
65 : USE cp_fm_pool_types, ONLY: cp_fm_pool_type,&
66 : fm_pool_create_fm,&
67 : fm_pool_get_el_struct,&
68 : fm_pool_give_back_fm
69 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
70 : cp_fm_struct_release,&
71 : cp_fm_struct_type
72 : USE cp_fm_types, ONLY: cp_fm_add_to_element,&
73 : cp_fm_create,&
74 : cp_fm_get_info,&
75 : cp_fm_release,&
76 : cp_fm_to_fm,&
77 : cp_fm_type
78 : USE cp_log_handling, ONLY: cp_get_default_logger,&
79 : cp_logger_get_default_unit_nr,&
80 : cp_logger_type
81 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
82 : cp_print_key_unit_nr,&
83 : debug_print_level
84 : USE dft_plus_u, ONLY: lowdin_kp_apply
85 : USE ieee_arithmetic, ONLY: ieee_is_finite
86 : USE input_constants, ONLY: &
87 : cholesky_dbcsr, cholesky_inverse, cholesky_off, cholesky_reduce, cholesky_restore, &
88 : core_guess, diag_update_method_adiis, general_roks, high_spin_roks, ot_precond_full_all, &
89 : ot_precond_full_kinetic, ot_precond_full_single_inverse, ot_precond_none, &
90 : ot_precond_s_inverse, restart_guess
91 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
92 : section_vals_type
93 : USE kinds, ONLY: dp,&
94 : int_8
95 : USE kpoint_methods, ONLY: kpoint_density_matrices,&
96 : kpoint_density_transform,&
97 : kpoint_set_mo_occupation
98 : USE kpoint_types, ONLY: get_kpoint_info,&
99 : kpoint_env_type,&
100 : kpoint_type
101 : USE local_gemm_api, ONLY: LOCAL_GEMM_PU_GPU,&
102 : local_gemm_ctxt_type
103 : USE machine, ONLY: m_flush,&
104 : m_walltime
105 : USE mathconstants, ONLY: z_one,&
106 : z_zero
107 : USE message_passing, ONLY: mp_para_env_type
108 : USE parallel_gemm_api, ONLY: parallel_gemm
109 : USE preconditioner, ONLY: dbcsr_pair_to_cfm,&
110 : make_preconditioner_complex_full_kinetic,&
111 : make_preconditioner_complex_full_s_inverse,&
112 : prepare_preconditioner,&
113 : restart_preconditioner
114 : USE preconditioner_makes, ONLY: make_complex_full_all,&
115 : make_complex_full_single_inverse
116 : USE preconditioner_types, ONLY: destroy_preconditioner,&
117 : init_preconditioner
118 : USE qs_density_matrices, ONLY: calculate_density_matrix
119 : USE qs_density_mixing_types, ONLY: direct_mixing_nr,&
120 : gspace_mixing_nr
121 : USE qs_diis, ONLY: qs_diis_b_calc_err_kp,&
122 : qs_diis_b_check_i_alloc_kp,&
123 : qs_diis_b_clear,&
124 : qs_diis_b_clear_kp,&
125 : qs_diis_b_info_kp,&
126 : qs_diis_b_step,&
127 : qs_diis_b_step_kp
128 : USE qs_energy_types, ONLY: qs_energy_type
129 : USE qs_environment_types, ONLY: get_qs_env,&
130 : qs_environment_type
131 : USE qs_gspace_mixing, ONLY: gspace_mixing
132 : USE qs_kpoint_operators, ONLY: &
133 : kpoint_operator_batch_size, kpoint_operator_context_create, &
134 : kpoint_operator_context_release, kpoint_operator_context_type, kpoint_operator_evaluate, &
135 : kpoint_operator_evaluate_batch, kpoint_operator_finish, kpoint_operator_get, &
136 : kpoint_operator_get_local, kpoint_operator_prepare, kpoint_operator_start, &
137 : kpoint_operator_store, kpoint_slot_ks, kpoint_slot_s, kpoint_slot_t, kpoint_spin_free
138 : USE qs_ks_methods, ONLY: qs_ks_update_qs_env
139 : USE qs_ks_types, ONLY: qs_ks_did_change,&
140 : qs_ks_env_type
141 : USE qs_matrix_pools, ONLY: mpools_get,&
142 : qs_matrix_pools_type
143 : USE qs_mixing_utils, ONLY: charge_mixing_init,&
144 : mixing_allocate,&
145 : mixing_init,&
146 : self_consistency_check
147 : USE qs_mo_methods, ONLY: calculate_subspace_eigenvalues
148 : USE qs_mo_occupation, ONLY: set_mo_occupation
149 : USE qs_mo_types, ONLY: get_mo_set,&
150 : mo_set_type
151 : USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
152 : USE qs_ot_eigensolver, ONLY: ot_eigensolver,&
153 : ot_eigensolver_complex
154 : USE qs_rho_atom_types, ONLY: rho_atom_type
155 : USE qs_rho_methods, ONLY: qs_rho_update_rho
156 : USE qs_rho_types, ONLY: qs_rho_get,&
157 : qs_rho_type
158 : USE qs_scf_block_davidson, ONLY: generate_extended_space,&
159 : generate_extended_space_c,&
160 : generate_extended_space_sparse
161 : USE qs_scf_lanczos, ONLY: lanczos_refinement,&
162 : lanczos_refinement_2v
163 : USE qs_scf_lanczos_c, ONLY: lanczos_channel_begin,&
164 : lanczos_channel_end,&
165 : lanczos_sweep_suffix,&
166 : lanczos_workspace_create,&
167 : lanczos_workspace_release,&
168 : lanczos_workspace_type
169 : USE qs_scf_methods, ONLY: combine_ks_matrices,&
170 : eigensolver,&
171 : eigensolver_dbcsr,&
172 : eigensolver_generalized,&
173 : eigensolver_simple,&
174 : eigensolver_symm,&
175 : scf_env_density_mixing
176 : USE qs_scf_types, ONLY: qs_scf_env_type,&
177 : subspace_env_type
178 : USE scf_control_types, ONLY: scf_control_type
179 :
180 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, &
181 : !$ omp_get_thread_num
182 : #include "./base/base_uses.f90"
183 :
184 : IMPLICIT NONE
185 :
186 : PRIVATE
187 :
188 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_diagonalization'
189 :
190 : ! occupation below this weight sits beyond the thermal smearing tail
191 : ! and converges at the relaxed empty threshold. The Davidson k-point
192 : ! driver and both Lanczos k-point paths below use this rule
193 : REAL(KIND=dp), PARAMETER, PRIVATE :: occ_tol = 1.0E-3_dp
194 :
195 : TYPE, PRIVATE :: kp_diag_workspace_type
196 : TYPE(cp_cfm_type) :: cksmat, cmos, csmat, csmat_base, cwork
197 : TYPE(cp_cfm_diag_workspace_type) :: solver
198 : TYPE(lanczos_workspace_type) :: lan_ws
199 : TYPE(local_gemm_ctxt_type) :: gemm_ctx
200 : END TYPE kp_diag_workspace_type
201 :
202 : PUBLIC :: do_general_diag, do_general_diag_kp, do_roks_diag, &
203 : do_special_diag, do_ot_diag, do_ot_diag_kp, do_block_davidson_diag, &
204 : do_block_davidson_diag_kp, &
205 : do_block_krylov_diag, do_block_krylov_diag_kp, do_scf_diag_subspace, diag_subspace_allocate, &
206 : general_eigenproblem, diag_kp_smat, diag_kp_basic
207 :
208 : CONTAINS
209 :
210 : ! **************************************************************************************************
211 : !> \brief Fade candidate regularization through handover; never regularize a raw-map check.
212 : !> \param scf_env Current SCF state.
213 : !> \return ...
214 : ! **************************************************************************************************
215 210 : FUNCTION adiis_candidate_shift(scf_env) RESULT(shift)
216 : TYPE(qs_scf_env_type), INTENT(IN) :: scf_env
217 : REAL(KIND=dp) :: shift
218 :
219 210 : shift = 0.0_dp
220 210 : IF (scf_env%iter_count <= 1 .OR. scf_env%adiis_check_next) RETURN
221 194 : IF (.NOT. ASSOCIATED(scf_env%scf_subspace_buffer)) RETURN
222 194 : shift = scf_env%adiis_shift*(1.0_dp - scf_env%scf_subspace_buffer%diis_weight)
223 194 : END FUNCTION adiis_candidate_shift
224 :
225 : ! **************************************************************************************************
226 : !> \brief Add -shift*S*C*(occupation/maxocc)*C^T*S to a candidate Fock matrix.
227 : !> The occupied projector does not require any stored virtual orbitals.
228 : !> \param fock Candidate matrix; physical F[P] and all history matrices remain unchanged.
229 : !> \param overlap AO overlap.
230 : !> \param mos Previous physical orbitals and occupations.
231 : !> \param shift Nonnegative regularization parameter in hartree.
232 : ! **************************************************************************************************
233 84 : SUBROUTINE shift_candidate_real(fock, overlap, mos, shift)
234 : TYPE(cp_fm_type), INTENT(INOUT) :: fock
235 : TYPE(cp_fm_type), INTENT(IN) :: overlap
236 : TYPE(mo_set_type), INTENT(IN) :: mos
237 : REAL(KIND=dp), INTENT(IN) :: shift
238 :
239 : INTEGER :: nao, nmo
240 : REAL(KIND=dp) :: maxocc
241 28 : REAL(KIND=dp), DIMENSION(:), POINTER :: occupation
242 : TYPE(cp_fm_type) :: sc
243 : TYPE(cp_fm_type), POINTER :: coeff
244 :
245 0 : IF (shift == 0.0_dp) RETURN
246 28 : CALL get_mo_set(mos, nao=nao, nmo=nmo, maxocc=maxocc, occupation_numbers=occupation, mo_coeff=coeff)
247 28 : CALL cp_fm_create(sc, coeff%matrix_struct)
248 28 : CALL parallel_gemm("N", "N", nao, nmo, nao, 1.0_dp, overlap, coeff, 0.0_dp, sc)
249 406 : CALL cp_fm_column_scale(sc, SQRT(MAX(0.0_dp, occupation/maxocc)))
250 28 : CALL parallel_gemm("N", "T", nao, nao, nmo, -shift, sc, sc, 1.0_dp, fock)
251 28 : CALL cp_fm_release(sc)
252 28 : END SUBROUTINE shift_candidate_real
253 :
254 : ! **************************************************************************************************
255 : !> \brief Complex occupied-space shift, using conjugate transpose and existing eigensolver workspaces.
256 : !> \param fock Candidate Fock matrix, modified in place.
257 : !> \param overlap AO overlap, unchanged.
258 : !> \param mos Previous complex MOs.
259 : !> \param coeff AO/MO eigensolver workspace.
260 : !> \param work AO/AO eigensolver workspace.
261 : !> \param shift Nonnegative regularization parameter in hartree.
262 : !> \param local Whether the matrices are complete on this rank.
263 : !> \param gemm_ctx Private local GEMM context, unused for distributed groups.
264 : ! **************************************************************************************************
265 158 : SUBROUTINE shift_candidate_complex(fock, overlap, mos, coeff, work, shift, local, gemm_ctx)
266 : TYPE(cp_cfm_type), INTENT(INOUT) :: fock
267 : TYPE(cp_cfm_type), INTENT(IN) :: overlap
268 : TYPE(mo_set_type), INTENT(IN) :: mos
269 : TYPE(cp_cfm_type), INTENT(INOUT) :: coeff, work
270 : REAL(KIND=dp), INTENT(IN) :: shift
271 : LOGICAL, INTENT(IN) :: local
272 : TYPE(local_gemm_ctxt_type), INTENT(INOUT) :: gemm_ctx
273 :
274 : INTEGER :: nao, nmo
275 : REAL(KIND=dp) :: maxocc
276 138 : REAL(KIND=dp), DIMENSION(:), POINTER :: occupation
277 : TYPE(cp_cfm_type), POINTER :: cmos
278 :
279 118 : IF (shift == 0.0_dp) RETURN
280 20 : CALL get_mo_set(mos, nao=nao, nmo=nmo, maxocc=maxocc, occupation_numbers=occupation, cmo_coeff=cmos)
281 20 : CALL cp_cfm_to_cfm(cmos, coeff)
282 320 : CALL cp_cfm_column_scale(coeff, CMPLX(SQRT(MAX(0.0_dp, occupation/maxocc)), KIND=dp))
283 20 : IF (local) THEN
284 60 : CPASSERT(PRODUCT(overlap%matrix_struct%context%num_pe) == 1)
285 60 : CPASSERT(PRODUCT(coeff%matrix_struct%context%num_pe) == 1)
286 60 : CPASSERT(PRODUCT(work%matrix_struct%context%num_pe) == 1)
287 60 : CPASSERT(PRODUCT(fock%matrix_struct%context%num_pe) == 1)
288 : CALL gemm_ctx%gemm("N", "N", nao, nmo, nao, z_one, &
289 : overlap%local_data, SIZE(overlap%local_data, 1), &
290 : coeff%local_data, SIZE(coeff%local_data, 1), z_zero, &
291 20 : work%local_data, SIZE(work%local_data, 1))
292 : CALL gemm_ctx%gemm("N", "C", nao, nao, nmo, CMPLX(-shift, KIND=dp), &
293 : work%local_data, SIZE(work%local_data, 1), &
294 : work%local_data, SIZE(work%local_data, 1), z_one, &
295 20 : fock%local_data, SIZE(fock%local_data, 1))
296 : ELSE
297 0 : CALL parallel_gemm("N", "N", nao, nmo, nao, z_one, overlap, coeff, z_zero, work)
298 0 : CALL parallel_gemm("N", "C", nao, nao, nmo, CMPLX(-shift, KIND=dp), work, work, z_one, fock)
299 : END IF
300 138 : END SUBROUTINE shift_candidate_complex
301 :
302 : ! **************************************************************************************************
303 : !> \brief the inner loop of scf, specific to diagonalization with S matrix
304 : !> basically, in goes the ks matrix out goes a new p matrix
305 : !> \param scf_env ...
306 : !> \param mos ...
307 : !> \param matrix_ks ...
308 : !> \param matrix_s ...
309 : !> \param scf_control ...
310 : !> \param scf_section ...
311 : !> \param diis_step ...
312 : !> \par History
313 : !> 03.2006 created [Joost VandeVondele]
314 : ! **************************************************************************************************
315 :
316 104713 : SUBROUTINE general_eigenproblem(scf_env, mos, matrix_ks, &
317 : matrix_s, scf_control, scf_section, &
318 : diis_step)
319 :
320 : TYPE(qs_scf_env_type), POINTER :: scf_env
321 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
322 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
323 : TYPE(scf_control_type), POINTER :: scf_control
324 : TYPE(section_vals_type), POINTER :: scf_section
325 : LOGICAL, INTENT(INOUT) :: diis_step
326 :
327 : INTEGER :: cdiis_cholesky_method, cdiis_ncall, &
328 : ispin, nspin
329 : LOGICAL :: cdiis_step, do_level_shift, owns_ortho, &
330 : use_adiis, use_cdiis, use_jacobi, &
331 : use_subspace_fock
332 : REAL(KIND=dp) :: candidate_shift, cdiis_error, &
333 : diis_error, diis_weight, eps_diis
334 : TYPE(cp_fm_type), POINTER :: ortho
335 : TYPE(dbcsr_type), POINTER :: effective_ks_dbcsr, ortho_dbcsr
336 :
337 104713 : nspin = SIZE(matrix_ks)
338 104713 : NULLIFY (effective_ks_dbcsr, ortho, ortho_dbcsr)
339 :
340 104713 : use_adiis = scf_control%diagonalization%update_method == diag_update_method_adiis
341 104713 : use_subspace_fock = .FALSE.
342 104713 : IF (use_adiis) THEN
343 72 : IF (.NOT. ASSOCIATED(scf_env%scf_subspace_buffer)) THEN
344 0 : CPABORT("ADIIS is only available in the main Quickstep SCF workflow")
345 : END IF
346 72 : use_subspace_fock = scf_env%scf_subspace_buffer%use_combined_fock
347 : END IF
348 :
349 104713 : eps_diis = scf_control%eps_diis
350 104713 : use_cdiis = .FALSE.
351 104713 : IF (use_adiis .AND. ASSOCIATED(scf_env%scf_diis_buffer)) THEN
352 : IF (.NOT. scf_env%skip_diis .AND. scf_env%iter_count > 1 .AND. &
353 : .NOT. scf_env%adiis_check_next .AND. &
354 72 : .NOT. scf_env%scf_subspace_buffer%last_restart .AND. &
355 : scf_env%scf_subspace_buffer%nstored > 0) THEN
356 66 : use_cdiis = scf_env%iter_delta < eps_diis .OR. scf_env%scf_diis_buffer%ncall > 0
357 : END IF
358 : END IF
359 :
360 228698 : DO ispin = 1, nspin
361 228698 : IF (use_subspace_fock .AND. .NOT. use_cdiis) THEN
362 : CALL copy_dbcsr_to_fm(scf_env%scf_subspace_buffer%combined_fock(ispin, 1)%matrix, &
363 20 : scf_env%scf_work1(ispin), scf_env%fm_copy_plan(ispin), check_plan=.TRUE.)
364 : ELSE
365 : CALL copy_dbcsr_to_fm(matrix_ks(ispin)%matrix, scf_env%scf_work1(ispin), &
366 123965 : scf_env%fm_copy_plan(ispin), check_plan=.TRUE.)
367 : END IF
368 : END DO
369 :
370 104713 : IF (use_adiis) THEN
371 72 : diis_step = .FALSE.
372 72 : diis_error = scf_env%scf_subspace_buffer%last_old_fock_weight
373 72 : diis_weight = scf_env%scf_subspace_buffer%diis_weight
374 72 : IF (use_cdiis) THEN
375 50 : cdiis_ncall = scf_env%scf_diis_buffer%ncall
376 : CALL qs_diis_b_step(scf_env%scf_diis_buffer, mos, scf_env%scf_work1, &
377 : scf_env%scf_work2, scf_env%iter_delta, cdiis_error, cdiis_step, &
378 50 : eps_diis, 2, s_matrix=matrix_s, scf_section=scf_section)
379 50 : IF (.NOT. ieee_is_finite(cdiis_error) .OR. cdiis_error >= eps_diis) THEN
380 6 : cdiis_step = .FALSE.
381 6 : IF (cdiis_ncall < scf_env%scf_diis_buffer%nbuffer) THEN
382 : ! The rejected state has not overwritten a full circular buffer.
383 6 : scf_env%scf_diis_buffer%ncall = cdiis_ncall
384 : ELSE
385 0 : CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
386 0 : diis_weight = 0.0_dp
387 : END IF
388 : END IF
389 50 : IF (cdiis_step) THEN
390 : ! Use one half-weight handover step; MAX_DIIS only controls the buffer capacity.
391 38 : diis_weight = MIN(1.0_dp, diis_weight + 0.5_dp)
392 : ELSE
393 : ! A rejected or deferred CDIIS step must not leave a partial raw-Fock blend active.
394 12 : diis_weight = 0.0_dp
395 : END IF
396 50 : IF (diis_weight < 1.0_dp) THEN
397 54 : DO ispin = 1, nspin
398 36 : IF (use_subspace_fock) THEN
399 : CALL copy_dbcsr_to_fm( &
400 : scf_env%scf_subspace_buffer%combined_fock(ispin, 1)%matrix, scf_env%scf_work2, &
401 32 : scf_env%fm_copy_plan(ispin), check_plan=.TRUE.)
402 : ELSE
403 : CALL copy_dbcsr_to_fm(matrix_ks(ispin)%matrix, scf_env%scf_work2, &
404 4 : scf_env%fm_copy_plan(ispin), check_plan=.TRUE.)
405 : END IF
406 : CALL cp_fm_scale_and_add(diis_weight, scf_env%scf_work1(ispin), &
407 54 : 1.0_dp - diis_weight, scf_env%scf_work2)
408 : END DO
409 : END IF
410 50 : diis_step = cdiis_step .AND. diis_weight >= 1.0_dp
411 50 : IF (diis_step) diis_error = cdiis_error
412 22 : ELSE IF (ASSOCIATED(scf_env%scf_diis_buffer)) THEN
413 22 : IF (scf_env%scf_diis_buffer%ncall > 0) CALL qs_diis_b_clear(scf_env%scf_diis_buffer)
414 22 : diis_weight = 0.0_dp
415 : END IF
416 72 : scf_env%scf_subspace_buffer%diis_weight = diis_weight
417 : ELSE
418 104641 : IF (scf_env%iter_count > 1 .AND. .NOT. scf_env%skip_diis) THEN
419 : CALL qs_diis_b_step(scf_env%scf_diis_buffer, mos, scf_env%scf_work1, &
420 : scf_env%scf_work2, scf_env%iter_delta, diis_error, diis_step, &
421 : eps_diis, scf_control%nmixing, &
422 : s_matrix=matrix_s, &
423 84257 : scf_section=scf_section)
424 : ELSE
425 20384 : diis_step = .FALSE.
426 : END IF
427 : END IF
428 :
429 104713 : candidate_shift = 0.0_dp
430 104713 : IF (use_adiis) candidate_shift = adiis_candidate_shift(scf_env)
431 104713 : IF (candidate_shift > 0.0_dp) THEN
432 14 : CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, scf_env%scf_work2, scf_env%fm_copy_plan(0), check_plan=.TRUE.)
433 42 : DO ispin = 1, nspin
434 42 : CALL shift_candidate_real(scf_env%scf_work1(ispin), scf_env%scf_work2, mos(ispin), candidate_shift)
435 : END DO
436 : END IF
437 :
438 : do_level_shift = ((scf_control%level_shift /= 0.0_dp) .AND. &
439 : ((scf_control%density_guess == core_guess) .OR. &
440 104713 : (scf_env%iter_count > 1)))
441 :
442 104713 : IF (.NOT. use_adiis .AND. (scf_env%iter_count > 1) .AND. &
443 : (scf_env%iter_delta < scf_control%diagonalization%eps_jacobi)) THEN
444 0 : use_jacobi = .TRUE.
445 : ELSE
446 104641 : use_jacobi = .FALSE.
447 : END IF
448 :
449 72 : IF (use_adiis .AND. .NOT. diis_step) THEN
450 40 : scf_env%iter_param = diis_error
451 40 : IF (scf_env%scf_subspace_buffer%last_restart) THEN
452 0 : scf_env%iter_method = "ADIIS/Rst."
453 : ELSE
454 40 : scf_env%iter_method = "ADIIS/Diag."
455 : END IF
456 104673 : ELSE IF (diis_step) THEN
457 52337 : scf_env%iter_param = diis_error
458 52337 : IF (use_jacobi) THEN
459 0 : scf_env%iter_method = "DIIS/Jacobi"
460 : ELSE
461 52337 : scf_env%iter_method = "DIIS/Diag."
462 : END IF
463 : ELSE
464 52336 : IF (scf_env%mixing_method == 0) THEN
465 0 : scf_env%iter_method = "NoMix/Diag."
466 52336 : ELSE IF (scf_env%mixing_method == 1) THEN
467 46042 : scf_env%iter_param = scf_env%p_mix_alpha
468 46042 : IF (use_jacobi) THEN
469 0 : scf_env%iter_method = "P_Mix/Jacobi"
470 : ELSE
471 46042 : scf_env%iter_method = "P_Mix/Diag."
472 : END IF
473 6294 : ELSE IF (scf_env%mixing_method > 1) THEN
474 6294 : scf_env%iter_param = scf_env%mixing_store%alpha
475 6294 : IF (use_jacobi) THEN
476 0 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Jacobi"
477 : ELSE
478 6294 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Diag."
479 : END IF
480 : END IF
481 : END IF
482 :
483 104713 : IF (scf_env%cholesky_method == cholesky_dbcsr .AND. .NOT. use_cdiis) THEN
484 1236 : ortho_dbcsr => scf_env%ortho_dbcsr
485 3690 : DO ispin = 1, nspin
486 2454 : effective_ks_dbcsr => matrix_ks(ispin)%matrix
487 2454 : IF (use_subspace_fock) THEN
488 0 : effective_ks_dbcsr => scf_env%scf_subspace_buffer%combined_fock(ispin, 1)%matrix
489 : END IF
490 : CALL eigensolver_dbcsr(matrix_ks=effective_ks_dbcsr, matrix_ks_fm=scf_env%scf_work1(ispin), &
491 : mo_set=mos(ispin), &
492 : ortho_dbcsr=ortho_dbcsr, &
493 3690 : ksbuf1=scf_env%buf1_dbcsr, ksbuf2=scf_env%buf2_dbcsr)
494 : END DO
495 :
496 103477 : ELSE IF (scf_env%cholesky_method > cholesky_off) THEN
497 102971 : cdiis_cholesky_method = scf_env%cholesky_method
498 102971 : IF (cdiis_cholesky_method == cholesky_dbcsr) THEN
499 0 : cdiis_cholesky_method = cholesky_inverse
500 : END IF
501 102971 : IF (scf_env%cholesky_method == cholesky_inverse) THEN
502 628 : ortho => scf_env%ortho_m1
503 : ELSE
504 102343 : ortho => scf_env%ortho
505 : END IF
506 :
507 102971 : owns_ortho = .FALSE.
508 102971 : IF (.NOT. ASSOCIATED(ortho)) THEN
509 0 : ALLOCATE (ortho)
510 0 : owns_ortho = .TRUE.
511 : END IF
512 :
513 223842 : DO ispin = 1, nspin
514 223842 : IF (direct_generalized_diagonalization .AND. .NOT. do_level_shift) THEN
515 : CALL eigensolver_generalized(matrix_ks_fm=scf_env%scf_work1(ispin), &
516 : matrix_s=matrix_s(ispin)%matrix, &
517 : mo_set=mos(ispin), &
518 0 : work=scf_env%scf_work2)
519 : ELSE
520 120871 : IF (do_level_shift) THEN
521 : CALL eigensolver(matrix_ks_fm=scf_env%scf_work1(ispin), &
522 : mo_set=mos(ispin), &
523 : ortho=ortho, &
524 : work=scf_env%scf_work2, &
525 : cholesky_method=cdiis_cholesky_method, &
526 : do_level_shift=do_level_shift, &
527 : level_shift=scf_control%level_shift, &
528 : matrix_u_fm=scf_env%ortho, &
529 170 : use_jacobi=use_jacobi)
530 : ELSE
531 : CALL eigensolver(matrix_ks_fm=scf_env%scf_work1(ispin), &
532 : mo_set=mos(ispin), &
533 : ortho=ortho, &
534 : work=scf_env%scf_work2, &
535 : cholesky_method=cdiis_cholesky_method, &
536 : do_level_shift=do_level_shift, &
537 : level_shift=scf_control%level_shift, &
538 120701 : use_jacobi=use_jacobi)
539 : END IF
540 : END IF
541 : END DO
542 :
543 102971 : IF (scf_env%cholesky_method /= cholesky_dbcsr) THEN
544 102971 : scf_env%cholesky_method = cdiis_cholesky_method
545 : END IF
546 :
547 102971 : IF (owns_ortho) DEALLOCATE (ortho)
548 : ELSE
549 506 : ortho => scf_env%ortho
550 :
551 506 : owns_ortho = .FALSE.
552 506 : IF (.NOT. ASSOCIATED(ortho)) THEN
553 0 : ALLOCATE (ortho)
554 0 : owns_ortho = .TRUE.
555 : END IF
556 :
557 506 : IF (do_level_shift) THEN
558 172 : DO ispin = 1, nspin
559 : IF (ASSOCIATED(scf_env%scf_work1_red) .AND. ASSOCIATED(scf_env%scf_work2_red) &
560 172 : .AND. ASSOCIATED(scf_env%ortho_red) .AND. ASSOCIATED(scf_env%ortho_m1_red)) THEN
561 : CALL eigensolver_symm(matrix_ks_fm=scf_env%scf_work1(ispin), &
562 : mo_set=mos(ispin), &
563 : ortho=ortho, &
564 : work=scf_env%scf_work2, &
565 : do_level_shift=do_level_shift, &
566 : level_shift=scf_control%level_shift, &
567 : matrix_u_fm=scf_env%ortho_m1, &
568 : use_jacobi=use_jacobi, &
569 : jacobi_threshold=scf_control%diagonalization%jacobi_threshold, &
570 : matrix_ks_fm_red=scf_env%scf_work1_red(ispin), &
571 : ortho_red=scf_env%ortho_red, &
572 : work_red=scf_env%scf_work2_red, &
573 86 : matrix_u_fm_red=scf_env%ortho_m1_red)
574 : ELSE
575 : CALL eigensolver_symm(matrix_ks_fm=scf_env%scf_work1(ispin), &
576 : mo_set=mos(ispin), &
577 : ortho=ortho, &
578 : work=scf_env%scf_work2, &
579 : do_level_shift=do_level_shift, &
580 : level_shift=scf_control%level_shift, &
581 : matrix_u_fm=scf_env%ortho_m1, &
582 : use_jacobi=use_jacobi, &
583 0 : jacobi_threshold=scf_control%diagonalization%jacobi_threshold)
584 : END IF
585 : END DO
586 : ELSE
587 994 : DO ispin = 1, nspin
588 : IF (ASSOCIATED(scf_env%scf_work1_red) .AND. ASSOCIATED(scf_env%scf_work2_red) &
589 994 : .AND. ASSOCIATED(scf_env%ortho_red)) THEN
590 : CALL eigensolver_symm(matrix_ks_fm=scf_env%scf_work1(ispin), &
591 : mo_set=mos(ispin), &
592 : ortho=ortho, &
593 : work=scf_env%scf_work2, &
594 : do_level_shift=do_level_shift, &
595 : level_shift=scf_control%level_shift, &
596 : use_jacobi=use_jacobi, &
597 : jacobi_threshold=scf_control%diagonalization%jacobi_threshold, &
598 : matrix_ks_fm_red=scf_env%scf_work1_red(ispin), &
599 : ortho_red=scf_env%ortho_red, &
600 574 : work_red=scf_env%scf_work2_red)
601 : ELSE
602 : CALL eigensolver_symm(matrix_ks_fm=scf_env%scf_work1(ispin), &
603 : mo_set=mos(ispin), &
604 : ortho=ortho, &
605 : work=scf_env%scf_work2, &
606 : do_level_shift=do_level_shift, &
607 : level_shift=scf_control%level_shift, &
608 : use_jacobi=use_jacobi, &
609 0 : jacobi_threshold=scf_control%diagonalization%jacobi_threshold)
610 : END IF
611 : END DO
612 : END IF
613 :
614 506 : IF (owns_ortho) DEALLOCATE (ortho)
615 : END IF
616 :
617 104713 : END SUBROUTINE general_eigenproblem
618 :
619 : ! **************************************************************************************************
620 : !> \brief ...
621 : !> \param scf_env ...
622 : !> \param mos ...
623 : !> \param matrix_ks ...
624 : !> \param matrix_s ...
625 : !> \param scf_control ...
626 : !> \param scf_section ...
627 : !> \param diis_step ...
628 : !> \param probe ...
629 : ! **************************************************************************************************
630 103639 : SUBROUTINE do_general_diag(scf_env, mos, matrix_ks, &
631 : matrix_s, scf_control, scf_section, &
632 : diis_step, probe)
633 :
634 : TYPE(qs_scf_env_type), POINTER :: scf_env
635 : TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
636 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
637 : TYPE(scf_control_type), POINTER :: scf_control
638 : TYPE(section_vals_type), POINTER :: scf_section
639 : LOGICAL, INTENT(INOUT) :: diis_step
640 : TYPE(hairy_probes_type), DIMENSION(:), OPTIONAL, &
641 : POINTER :: probe
642 :
643 : INTEGER :: ispin, nspin
644 : REAL(KIND=dp) :: total_zeff_corr
645 :
646 103639 : nspin = SIZE(matrix_ks)
647 :
648 : CALL general_eigenproblem(scf_env, mos, matrix_ks, &
649 103639 : matrix_s, scf_control, scf_section, diis_step)
650 :
651 : total_zeff_corr = 0.0_dp
652 103639 : total_zeff_corr = scf_env%sum_zeff_corr
653 :
654 103639 : IF (ABS(total_zeff_corr) > 0.0_dp) THEN
655 : CALL set_mo_occupation(mo_array=mos, &
656 40 : smear=scf_control%smear, tot_zeff_corr=total_zeff_corr)
657 : ELSE
658 103599 : IF (PRESENT(probe) .EQV. .TRUE.) THEN
659 14 : scf_control%smear%do_smear = .FALSE.
660 : CALL set_mo_occupation(mo_array=mos, &
661 : smear=scf_control%smear, &
662 14 : probe=probe)
663 : ELSE
664 103585 : IF (.NOT. scf_control%gce%do_gce) THEN
665 : CALL set_mo_occupation(mo_array=mos, &
666 103201 : smear=scf_control%smear)
667 : ELSE
668 : CALL set_mo_occupation(mo_array=mos, &
669 : smear=scf_control%smear, &
670 384 : gce=scf_control%gce)
671 : END IF
672 : END IF
673 : END IF
674 :
675 225476 : DO ispin = 1, nspin
676 : CALL calculate_density_matrix(mos(ispin), &
677 225476 : scf_env%p_mix_new(ispin, 1)%matrix)
678 : END DO
679 :
680 103639 : END SUBROUTINE do_general_diag
681 :
682 : ! **************************************************************************************************
683 : !> \brief Kpoint diagonalization routine
684 : !> Transforms matrices to kpoint, distributes kpoint groups, performs
685 : !> general diagonalization (no storgae of overlap decomposition), stores
686 : !> MOs, calculates occupation numbers, calculates density matrices
687 : !> in kpoint representation, transforms density matrices to real space
688 : !> \param matrix_ks Kohn-sham matrices (RS indices, global)
689 : !> \param matrix_s Overlap matrices (RS indices, global)
690 : !> \param kpoints Kpoint environment
691 : !> \param scf_env SCF environment
692 : !> \param scf_control SCF control variables
693 : !> \param update_p ...
694 : !> \param diis_step ...
695 : !> \param diis_error ...
696 : !> \param qs_env ...
697 : !> \param probe ...
698 : !> \param added_mos_auto_grow true when the adaptive virtual-space buffer must grow
699 : !> \param potential Fixed Lowdin orbital potential for non-self-consistent k points.
700 : !> \par History
701 : !> 08.2014 created [JGH]
702 : ! **************************************************************************************************
703 37032 : SUBROUTINE do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, update_p, &
704 37032 : diis_step, diis_error, qs_env, probe, added_mos_auto_grow, potential)
705 :
706 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s
707 : TYPE(kpoint_type), POINTER :: kpoints
708 : TYPE(qs_scf_env_type), POINTER :: scf_env
709 : TYPE(scf_control_type), POINTER :: scf_control
710 : LOGICAL, INTENT(IN) :: update_p
711 : LOGICAL, INTENT(INOUT) :: diis_step
712 : REAL(dp), INTENT(INOUT), OPTIONAL :: diis_error
713 : TYPE(qs_environment_type), OPTIONAL, POINTER :: qs_env
714 : TYPE(hairy_probes_type), DIMENSION(:), OPTIONAL, &
715 : POINTER :: probe
716 : LOGICAL, INTENT(OUT), OPTIONAL :: added_mos_auto_grow
717 : REAL(dp), INTENT(IN), OPTIONAL, TARGET :: potential(:, :)
718 :
719 : CHARACTER(len=*), PARAMETER :: routineN = 'do_general_diag_kp'
720 :
721 37032 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:) :: coeffs
722 37032 : REAL(dp), POINTER :: effective_u(:, :), adiis_u(:, :)
723 : INTEGER :: cdiis_ncall, handle, ib, ikp, ispin, kplocal, kpmax, &
724 : nb, nkp, nspin, omp_threads, nworkers
725 : INTEGER, DIMENSION(2) :: kp_range
726 37032 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
727 : LOGICAL :: adiis_cdiis, cache_smat, cdiis_step, do_diis, my_added_mos_auto_grow, &
728 : reuse_smat, use_adiis, use_local_kpoints, use_real_wfn
729 : REAL(KIND=dp) :: diis_weight
730 37032 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
731 : TYPE(cp_cfm_type), POINTER :: cksmat, cmos, csmat, cwork
732 37032 : TYPE(kp_diag_workspace_type), ALLOCATABLE, TARGET :: workspace(:)
733 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
734 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct, mo_struct
735 : TYPE(cp_fm_type) :: fmlocal, rksmat, rsmat
736 37032 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
737 : TYPE(cp_fm_type), POINTER :: mo_coeff
738 37032 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: adiis_ks, effective_ks
739 : TYPE(kpoint_env_type), POINTER :: kp
740 37032 : TYPE(kpoint_operator_context_type) :: op_ctx
741 : TYPE(mp_para_env_type), POINTER :: para_env
742 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
743 37032 : POINTER :: sab_nl
744 : TYPE(qs_matrix_pools_type), POINTER :: mpools
745 : TYPE(section_vals_type), POINTER :: scf_section
746 :
747 37032 : CALL timeset(routineN, handle)
748 :
749 37032 : my_added_mos_auto_grow = .FALSE.
750 37032 : IF (PRESENT(added_mos_auto_grow)) added_mos_auto_grow = .FALSE.
751 :
752 37032 : NULLIFY (adiis_ks, effective_ks, sab_nl)
753 : CALL get_kpoint_info(kpoints, nkp=nkp, use_real_wfn=use_real_wfn, kp_range=kp_range, &
754 37032 : kp_dist=kp_dist, sab_nl=sab_nl)
755 37032 : CPASSERT(ASSOCIATED(sab_nl))
756 37032 : kplocal = kp_range(2) - kp_range(1) + 1
757 100038 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
758 :
759 : !Whether we use DIIS for k-points
760 37032 : do_diis = .FALSE.
761 37032 : use_adiis = update_p .AND. scf_control%diagonalization%update_method == diag_update_method_adiis
762 37032 : IF (use_adiis) THEN
763 80 : CPASSERT(ASSOCIATED(scf_env%scf_subspace_buffer))
764 : END IF
765 37032 : adiis_cdiis = .FALSE.
766 37032 : IF (use_adiis .AND. ASSOCIATED(kpoints%scf_diis_buffer)) THEN
767 : IF (scf_env%iter_count > 1 .AND. .NOT. scf_env%skip_diis .AND. &
768 : .NOT. scf_env%adiis_check_next .AND. &
769 : .NOT. scf_env%scf_subspace_buffer%last_restart .AND. &
770 : scf_env%scf_subspace_buffer%nstored > 0 .AND. &
771 80 : PRESENT(diis_error) .AND. PRESENT(qs_env)) THEN
772 : adiis_cdiis = scf_env%iter_delta < scf_control%eps_diis .OR. &
773 74 : kpoints%scf_diis_buffer%ncall > 0
774 : END IF
775 : END IF
776 : IF ((.NOT. use_adiis .AND. scf_env%iter_count > 1 .AND. .NOT. scf_env%skip_diis .AND. &
777 37032 : .NOT. use_real_wfn .AND. PRESENT(diis_error) .AND. PRESENT(qs_env)) .OR. &
778 27658 : adiis_cdiis) do_diis = .TRUE.
779 :
780 37032 : adiis_ks => matrix_ks
781 37032 : NULLIFY (adiis_u)
782 37032 : IF (ALLOCATED(kpoints%lowdin_v)) adiis_u => kpoints%lowdin_v
783 37032 : IF (use_adiis) THEN
784 80 : IF (scf_env%scf_subspace_buffer%use_combined_fock) THEN
785 32 : adiis_ks => scf_env%scf_subspace_buffer%combined_fock
786 32 : IF (ASSOCIATED(adiis_u)) adiis_u => scf_env%scf_subspace_buffer%combined_aux
787 : END IF
788 : END IF
789 37032 : effective_ks => matrix_ks
790 37032 : NULLIFY (effective_u)
791 37032 : IF (ALLOCATED(kpoints%lowdin_v)) effective_u => kpoints%lowdin_v
792 37032 : IF (PRESENT(potential)) THEN
793 0 : CPASSERT(.NOT. update_p)
794 0 : effective_u => potential
795 : END IF
796 37032 : IF (use_adiis .AND. .NOT. adiis_cdiis) THEN
797 30 : effective_ks => adiis_ks
798 30 : effective_u => adiis_u
799 : END IF
800 37032 : IF (use_adiis .AND. .NOT. do_diis) THEN
801 30 : scf_env%scf_subspace_buffer%diis_weight = 0.0_dp
802 30 : IF (ASSOCIATED(kpoints%scf_diis_buffer)) THEN
803 30 : IF (kpoints%scf_diis_buffer%ncall > 0) CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
804 : END IF
805 : END IF
806 :
807 37032 : para_env => kpoints%blacs_env_all%para_env
808 37032 : nspin = SIZE(matrix_ks, 1)
809 37032 : omp_threads = 1
810 37032 : !$ omp_threads = omp_get_max_threads()
811 : ! One-rank groups use the same worker path for MPI, OpenMP and hybrid jobs.
812 37032 : use_local_kpoints = kpoints%para_env_kp%num_pe == 1 .AND. .NOT. use_real_wfn
813 37032 : IF (diag_check_requested()) use_local_kpoints = .FALSE.
814 37032 : ib = 0
815 37032 : nb = 0
816 37032 : NULLIFY (scf_section)
817 :
818 37032 : fmwork => scf_env%scf_work1
819 : ! Both real and complex operators share the same prepared context.
820 37032 : CALL kpoint_operator_context_create(op_ctx, kpoints, fmwork(1), nspin)
821 :
822 : ! fm pools to be used within a kpoint group
823 37032 : CALL get_kpoint_info(kpoints, mpools=mpools)
824 37032 : CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
825 :
826 37032 : CALL fm_pool_create_fm(ao_ao_fm_pool, fmlocal)
827 37032 : CALL cp_fm_get_info(fmlocal, matrix_struct=matrix_struct)
828 :
829 37032 : IF (use_real_wfn) THEN
830 : CALL cp_fm_create(rksmat, matrix_struct)
831 : CALL cp_fm_create(rsmat, matrix_struct)
832 : END IF
833 : ! Retain the unmodified S(k) for this SCF run, outside the driver-local
834 : ! operator context. Solvers receive work copies because they overwrite S.
835 37032 : cache_smat = update_p .AND. .NOT. use_real_wfn
836 37032 : reuse_smat = .FALSE.
837 37032 : IF (cache_smat) THEN
838 36700 : reuse_smat = ALLOCATED(kpoints%scf_smat)
839 36700 : IF (.NOT. reuse_smat) THEN
840 19195 : ALLOCATE (kpoints%scf_smat(kplocal))
841 11611 : DO ikp = 1, kplocal
842 11611 : CALL cp_cfm_create(kpoints%scf_smat(ikp), matrix_struct)
843 : END DO
844 : END IF
845 36700 : CPASSERT(SIZE(kpoints%scf_smat) == kplocal)
846 : END IF
847 :
848 : ! Prepare H/S before starting any round. ADIIS can replace only the idle KS slot.
849 37032 : CALL kpoint_operator_prepare(op_ctx, effective_ks, kpoint_slot_ks)
850 37032 : IF (.NOT. reuse_smat) THEN
851 4124 : CALL kpoint_operator_prepare(op_ctx, matrix_s, kpoint_slot_s, matrix_row=kpoint_spin_free)
852 : END IF
853 :
854 37032 : nworkers = 1
855 37032 : IF (use_local_kpoints) nworkers = MIN(omp_threads, kplocal)
856 37032 : IF (.NOT. use_real_wfn .OR. do_diis) THEN
857 36902 : kp => kpoints%kp_env(1)%kpoint_env
858 36902 : IF (use_real_wfn) THEN
859 0 : CALL get_mo_set(kp%mos(1), mo_coeff=mo_coeff)
860 0 : CALL cp_fm_get_info(mo_coeff, matrix_struct=mo_struct)
861 : ELSE
862 36902 : mo_struct => kp%mos(1)%cmo_coeff%matrix_struct
863 : END IF
864 : CALL kp_diag_workspaces_create(workspace, nworkers, matrix_struct, mo_struct, &
865 36902 : use_local_kpoints, scf_env%cholesky_method == cholesky_off)
866 36902 : cksmat => workspace(1)%cksmat
867 36902 : csmat => workspace(1)%csmat
868 36902 : cwork => workspace(1)%cwork
869 36902 : cmos => workspace(1)%cmos
870 : END IF
871 :
872 : ! Mark operator rounds; preparation above has already captured their values.
873 37032 : IF (.NOT. use_local_kpoints) THEN
874 32858 : DO ikp = 1, kpmax
875 58498 : DO ispin = 1, nspin
876 : CALL kpoint_operator_start(op_ctx, ikp, ispin, effective_ks, kpoint_slot_ks, &
877 25640 : matrix_row=ispin)
878 47440 : IF (.NOT. cache_smat .OR. (.NOT. reuse_smat .AND. ispin == 1)) THEN
879 : CALL kpoint_operator_start(op_ctx, ikp, ispin, matrix_s, kpoint_slot_s, &
880 3724 : matrix_row=kpoint_spin_free)
881 : END IF
882 : END DO
883 : END DO
884 : END IF
885 :
886 : ! Finish communication then diagonalise in each group
887 37032 : diis_weight = 1.0_dp
888 37032 : IF (do_diis) THEN
889 27658 : scf_section => section_vals_get_subs_vals(qs_env%input, "DFT%SCF")
890 27658 : cdiis_ncall = kpoints%scf_diis_buffer%ncall
891 27658 : CALL qs_diis_b_info_kp(kpoints%scf_diis_buffer, ib, nb)
892 27658 : CALL qs_diis_b_check_i_alloc_kp(kpoints%scf_diis_buffer, matrix_struct, nspin, kplocal, scf_section)
893 27658 : IF (use_local_kpoints) THEN
894 : CALL kp_diag_local_phase(op_ctx, kpoints, workspace, scf_env, scf_control, nspin, &
895 19916 : cache_smat, reuse_smat, use_adiis, ib, .TRUE., 1.0_dp, potential=effective_u)
896 : ELSE
897 19540 : DO ikp = 1, kpmax
898 34482 : DO ispin = 1, nspin
899 14942 : CALL harvest_operators(ikp, ispin)
900 14942 : IF (ikp > kplocal) CYCLE
901 : CALL qs_diis_b_calc_err_kp(kpoints%scf_diis_buffer, ib, kp%mos, cksmat, csmat, &
902 : ispin, ikp, cmos, real_wfn=use_real_wfn, &
903 26740 : local=.FALSE., gemm_ctx=workspace(1)%gemm_ctx)
904 :
905 : END DO !ispin
906 : END DO !ikp
907 :
908 : END IF
909 :
910 82974 : ALLOCATE (coeffs(nb))
911 : CALL qs_diis_b_step_kp(kpoints%scf_diis_buffer, coeffs, ib, nb, scf_env%iter_delta, diis_error, &
912 : cdiis_step, scf_control%eps_diis, nspin, nkp, kplocal, &
913 : MERGE(2, scf_control%nmixing, use_adiis), &
914 55266 : scf_section, kpoints%para_env_inter_kp)
915 : diis_weight = 1.0_dp
916 27658 : IF (use_adiis) THEN
917 50 : diis_weight = scf_env%scf_subspace_buffer%diis_weight
918 50 : IF (.NOT. ieee_is_finite(diis_error) .OR. diis_error >= scf_control%eps_diis) THEN
919 6 : cdiis_step = .FALSE.
920 6 : IF (cdiis_ncall < kpoints%scf_diis_buffer%nbuffer) THEN
921 : ! The rejected state has not overwritten a full circular buffer.
922 6 : kpoints%scf_diis_buffer%ncall = cdiis_ncall
923 : ELSE
924 0 : CALL qs_diis_b_clear_kp(kpoints%scf_diis_buffer)
925 0 : diis_weight = 0.0_dp
926 : END IF
927 : END IF
928 50 : IF (cdiis_step) THEN
929 : ! Use one half-weight handover step; MAX_DIIS only controls the buffer capacity.
930 38 : diis_weight = MIN(1.0_dp, diis_weight + 0.5_dp)
931 : ELSE
932 : ! A rejected or deferred CDIIS step must not leave a partial raw-Fock blend active.
933 12 : diis_weight = 0.0_dp
934 : END IF
935 50 : scf_env%scf_subspace_buffer%diis_weight = diis_weight
936 50 : diis_step = cdiis_step .AND. diis_weight >= 1.0_dp
937 50 : IF (.NOT. diis_step) diis_error = scf_env%scf_subspace_buffer%last_old_fock_weight
938 : ELSE
939 27608 : diis_step = cdiis_step
940 : END IF
941 :
942 : ! All first-pass instances have finished. Capture the selected
943 : ! ADIIS operator once instead of retransferring it in every round.
944 55316 : IF (use_adiis .AND. diis_weight < 1.0_dp) THEN
945 18 : CALL kpoint_operator_prepare(op_ctx, adiis_ks, kpoint_slot_ks)
946 : END IF
947 :
948 : ELSE
949 9374 : IF (.NOT. update_p .OR. use_adiis) diis_step = .FALSE.
950 : END IF
951 :
952 37032 : IF (use_local_kpoints) THEN
953 : ! Unallocated arrays and disassociated pointers are absent at nonpointer optional dummies.
954 25974 : IF (do_diis) effective_u => adiis_u
955 : CALL kp_diag_local_phase(op_ctx, kpoints, workspace, scf_env, scf_control, nspin, &
956 25974 : cache_smat, reuse_smat, use_adiis, ib, .FALSE., diis_weight, coeffs, effective_u)
957 : ELSE
958 32858 : DO ikp = 1, kpmax
959 58498 : DO ispin = 1, nspin
960 25640 : IF (do_diis) THEN
961 14942 : IF (use_adiis .AND. diis_weight < 1.0_dp) THEN
962 : CALL kpoint_operator_start(op_ctx, ikp, ispin, adiis_ks, kpoint_slot_ks, &
963 0 : matrix_row=ispin)
964 0 : CALL kpoint_operator_finish(op_ctx, ikp, ispin, kpoint_slot_ks, cwork)
965 0 : IF (ikp <= kplocal .AND. ASSOCIATED(adiis_u)) THEN
966 0 : CALL lowdin_kp_apply(kpoints%kp_env(ikp)%kpoint_env, adiis_u(:, ispin), cmat=cwork)
967 : END IF
968 : END IF
969 14942 : IF (ikp > kplocal) CYCLE
970 14942 : CALL cp_cfm_to_cfm(kpoints%scf_diis_buffer%smat(ikp), csmat)
971 14942 : CALL kp_diis_candidate(kpoints, ikp, ispin, coeffs, diis_weight, cksmat, cwork)
972 14942 : IF (use_real_wfn) THEN
973 0 : CALL cp_cfm_to_fm(cksmat, rksmat, fmlocal)
974 0 : CALL cp_cfm_to_fm(csmat, rsmat, fmlocal)
975 : END IF
976 : ELSE
977 10698 : CALL harvest_operators(ikp, ispin)
978 10698 : IF (ikp > kplocal) CYCLE
979 : END IF
980 25640 : kp => kpoints%kp_env(ikp)%kpoint_env
981 47440 : IF (use_real_wfn) THEN
982 156 : CALL get_mo_set(kp%mos(ispin), mo_coeff=mo_coeff, eigenvalues=eigenvalues)
983 156 : IF (use_adiis) CALL shift_candidate_real(rksmat, rsmat, kp%mos(ispin), &
984 0 : adiis_candidate_shift(scf_env))
985 156 : IF (scf_env%cholesky_method == cholesky_off) THEN
986 40 : CALL cp_fm_geeig_canon(rksmat, rsmat, mo_coeff, eigenvalues, fmlocal, scf_control%eps_eigval)
987 : ELSE
988 116 : CALL cp_fm_geeig(rksmat, rsmat, mo_coeff, eigenvalues, fmlocal)
989 : END IF
990 : ELSE
991 25484 : CALL kp_solve_complex(kp%mos(ispin), workspace(1), scf_env, scf_control, use_adiis, .FALSE.)
992 : END IF
993 : END DO
994 : END DO
995 : END IF
996 37032 : IF (ALLOCATED(coeffs)) DEALLOCATE (coeffs)
997 :
998 37032 : CALL kpoint_operator_context_release(op_ctx)
999 :
1000 37032 : IF (update_p) THEN
1001 : ! MO occupations
1002 36830 : IF (PRESENT(probe) .EQV. .TRUE.) THEN
1003 0 : scf_control%smear%do_smear = .FALSE.
1004 : CALL kpoint_set_mo_occupation(kpoints, scf_control%smear, &
1005 0 : probe=probe)
1006 : ELSE
1007 : CALL kpoint_set_mo_occupation(kpoints, scf_control%smear, &
1008 : added_mos_auto=scf_control%added_mos_auto, &
1009 36830 : added_mos_auto_grow=my_added_mos_auto_grow)
1010 : END IF
1011 36830 : IF (my_added_mos_auto_grow) THEN
1012 0 : IF (PRESENT(added_mos_auto_grow)) added_mos_auto_grow = .TRUE.
1013 : ELSE
1014 : ! density matrices
1015 36830 : CALL kpoint_density_matrices(kpoints)
1016 : ! density matrices in real space
1017 : CALL kpoint_density_transform(kpoints, scf_env%p_mix_new, .FALSE., &
1018 36830 : matrix_s(1, 1)%matrix, sab_nl, fmwork, overlap_rs=matrix_s)
1019 : END IF
1020 : END IF
1021 :
1022 37032 : IF (use_real_wfn) THEN
1023 130 : CALL cp_fm_release(rksmat)
1024 130 : CALL cp_fm_release(rsmat)
1025 : END IF
1026 37032 : IF (ALLOCATED(workspace)) CALL kp_diag_workspaces_release(workspace)
1027 37032 : CALL fm_pool_give_back_fm(ao_ao_fm_pool, fmlocal)
1028 :
1029 74064 : CALL timestop(handle)
1030 :
1031 : CONTAINS
1032 :
1033 : ! Serialized harvest only. Inactive tail rounds participate in every transfer,
1034 : ! then return before reading a local matrix or publishing the overlap cache.
1035 : ! **************************************************************************************************
1036 : !> \brief ...
1037 : !> \param round ...
1038 : !> \param spin ...
1039 : ! **************************************************************************************************
1040 25640 : SUBROUTINE harvest_operators(round, spin)
1041 : INTEGER, INTENT(IN) :: round, spin
1042 :
1043 25640 : IF (use_real_wfn) THEN
1044 156 : CALL kpoint_operator_finish(op_ctx, round, spin, kpoint_slot_ks, fm_re=rksmat)
1045 156 : CALL kpoint_operator_finish(op_ctx, round, spin, kpoint_slot_s, fm_re=rsmat)
1046 156 : IF (round <= kplocal .AND. ASSOCIATED(effective_u)) THEN
1047 0 : CALL lowdin_kp_apply(kpoints%kp_env(round)%kpoint_env, effective_u(:, spin), rmat=rksmat)
1048 : END IF
1049 156 : IF (round <= kplocal .AND. do_diis) THEN
1050 0 : CALL cp_cfm_scale_and_add_fm(z_zero, cksmat, z_one, rksmat)
1051 0 : CALL cp_cfm_scale_and_add_fm(z_zero, csmat, z_one, rsmat)
1052 : END IF
1053 : ELSE
1054 25484 : CALL kpoint_operator_finish(op_ctx, round, spin, kpoint_slot_ks, cksmat)
1055 25484 : IF (round <= kplocal .AND. ASSOCIATED(effective_u)) THEN
1056 0 : CALL lowdin_kp_apply(kpoints%kp_env(round)%kpoint_env, effective_u(:, spin), cmat=cksmat)
1057 : END IF
1058 25484 : IF (.NOT. cache_smat .OR. (.NOT. reuse_smat .AND. spin == 1)) THEN
1059 3568 : CALL kpoint_operator_finish(op_ctx, round, spin, kpoint_slot_s, csmat)
1060 : END IF
1061 : END IF
1062 :
1063 25640 : IF (round > kplocal) RETURN
1064 25640 : kp => kpoints%kp_env(round)%kpoint_env
1065 25640 : IF (cache_smat) THEN
1066 24412 : IF (.NOT. reuse_smat .AND. spin == 1) THEN
1067 2496 : CALL cp_cfm_to_cfm(csmat, kpoints%scf_smat(round))
1068 : ELSE
1069 21916 : CALL cp_cfm_to_cfm(kpoints%scf_smat(round), csmat)
1070 : END IF
1071 : END IF
1072 : END SUBROUTINE harvest_operators
1073 :
1074 : END SUBROUTINE do_general_diag_kp
1075 :
1076 : ! **************************************************************************************************
1077 : !> \brief Execute a local numerical phase. All SCF decisions and collectives belong to the caller.
1078 : !> \param ctx immutable prepared operators
1079 : !> \param kpoints host environment
1080 : !> \param workspace private dense/phase/LAPACK storage per worker
1081 : !> \param scf_env SCF settings (read only during this phase)
1082 : !> \param scf_control SCF control
1083 : !> \param nspin spin count
1084 : !> \param cache_smat retain the unmodified overlap
1085 : !> \param reuse_smat overlap cache is already valid
1086 : !> \param use_adiis shift an ADIIS candidate only
1087 : !> \param ib DIIS insertion index
1088 : !> \param store_error store raw F and its residual instead of solving
1089 : !> \param diis_weight weight of the CDIIS candidate
1090 : !> \param coeffs optional DIIS coefficients for the second phase
1091 : !> \param potential optional Lowdin orbital potential paired with the prepared Hamiltonian
1092 : ! **************************************************************************************************
1093 45890 : SUBROUTINE kp_diag_local_phase(ctx, kpoints, workspace, scf_env, scf_control, nspin, &
1094 45890 : cache_smat, reuse_smat, use_adiis, ib, store_error, diis_weight, coeffs, potential)
1095 : TYPE(kpoint_operator_context_type), INTENT(IN) :: ctx
1096 : TYPE(kpoint_type), POINTER :: kpoints
1097 : TYPE(kp_diag_workspace_type), TARGET, INTENT(INOUT) :: workspace(:)
1098 : TYPE(qs_scf_env_type), POINTER :: scf_env
1099 : TYPE(scf_control_type), POINTER :: scf_control
1100 : INTEGER, INTENT(IN) :: nspin, ib
1101 : LOGICAL, INTENT(IN) :: cache_smat, reuse_smat, use_adiis, store_error
1102 : REAL(KIND=dp), INTENT(IN) :: diis_weight
1103 : COMPLEX(KIND=dp), OPTIONAL, INTENT(IN) :: coeffs(:)
1104 : REAL(dp), OPTIONAL, INTENT(IN) :: potential(:, :)
1105 :
1106 45890 : COMPLEX(KIND=dp), ALLOCATABLE :: operators(:, :, :, :)
1107 45890 : REAL(KIND=dp), ALLOCATABLE :: block_work(:)
1108 : INTEGER :: first, ik, ikp, ispin, j, kplocal, nbatch, nmatrices, nworkers, thread, width
1109 : INTEGER(KIND=int_8) :: work_size
1110 : LOGICAL :: build_h, build_s
1111 : TYPE(kpoint_env_type), POINTER :: kp
1112 : TYPE(kp_diag_workspace_type), POINTER :: work
1113 :
1114 45890 : kplocal = kpoints%kp_range(2) - kpoints%kp_range(1) + 1
1115 45890 : IF (kplocal == 0) RETURN
1116 45890 : nworkers = SIZE(workspace)
1117 45890 : build_s = .NOT. PRESENT(coeffs) .AND. .NOT. reuse_smat
1118 19916 : build_h = .NOT. PRESENT(coeffs) .OR. diis_weight < 1.0_dp
1119 45890 : nmatrices = MERGE(1, 0, build_h) + MERGE(1, 0, build_s)
1120 45890 : width = 1
1121 45890 : work_size = 0_int_8
1122 45890 : IF (nmatrices > 0) CALL kpoint_operator_batch_size(ctx, nworkers, nmatrices, width, work_size)
1123 : ! Each worker assembles a bounded k tile, then consumes it using its private solver.
1124 : ! The second CDIIS phase combines history without allocating operator tiles.
1125 : !$OMP PARALLEL DEFAULT(NONE) NUM_THREADS(nworkers) IF(nworkers > 1) &
1126 : !$OMP SHARED(ctx,kpoints,workspace,scf_env,scf_control,nspin,cache_smat,reuse_smat,use_adiis,ib, &
1127 : !$OMP store_error,diis_weight,coeffs,potential,kplocal,nworkers,build_h,build_s,nmatrices,width,work_size) &
1128 45890 : !$OMP PRIVATE(first,ik,ikp,ispin,j,nbatch,thread,kp,work,operators,block_work)
1129 : thread = 1
1130 : !$ thread = omp_get_thread_num() + 1
1131 : work => workspace(thread)
1132 : ALLOCATE (operators(SIZE(work%cksmat%local_data, 1), SIZE(work%cksmat%local_data, 2), width, nmatrices), &
1133 : block_work(work_size))
1134 : !$OMP DO SCHEDULE(DYNAMIC, 1)
1135 : DO first = 1, kplocal, width
1136 : nbatch = MIN(width, kplocal - first + 1)
1137 : ik = kpoints%kp_range(1) + first - 1
1138 : IF (build_s) THEN
1139 : CALL kpoint_operator_evaluate_batch(ctx, 1, kpoint_slot_s, ik, operators(:, :, 1:nbatch, nmatrices), block_work)
1140 : END IF
1141 : DO ispin = 1, nspin
1142 : IF (build_h) THEN
1143 : CALL kpoint_operator_evaluate_batch(ctx, ispin, kpoint_slot_ks, ik, operators(:, :, 1:nbatch, 1), block_work)
1144 : END IF
1145 : DO j = 1, nbatch
1146 : ikp = first + j - 1
1147 : kp => kpoints%kp_env(ikp)%kpoint_env
1148 : IF (build_h) THEN
1149 : IF (PRESENT(coeffs)) THEN
1150 : work%csmat_base%local_data(:, :) = operators(:, :, j, 1)
1151 : IF (PRESENT(potential)) CALL lowdin_kp_apply(kp, potential(:, ispin), cmat=work%csmat_base)
1152 : ELSE
1153 : work%cksmat%local_data(:, :) = operators(:, :, j, 1)
1154 : IF (PRESENT(potential)) CALL lowdin_kp_apply(kp, potential(:, ispin), cmat=work%cksmat)
1155 : END IF
1156 : END IF
1157 : IF (PRESENT(coeffs)) THEN
1158 : CALL cp_cfm_to_cfm(kpoints%scf_diis_buffer%smat(ikp), work%csmat)
1159 : CALL kp_diis_candidate(kpoints, ikp, ispin, coeffs, diis_weight, work%cksmat, work%csmat_base)
1160 : ELSE
1161 : IF (reuse_smat) THEN
1162 : CALL cp_cfm_to_cfm(kpoints%scf_smat(ikp), work%csmat)
1163 : ELSE
1164 : work%csmat%local_data(:, :) = operators(:, :, j, nmatrices)
1165 : END IF
1166 : IF (cache_smat .AND. .NOT. reuse_smat .AND. ispin == 1) THEN
1167 : CALL cp_cfm_to_cfm(work%csmat, kpoints%scf_smat(ikp))
1168 : END IF
1169 : END IF
1170 : IF (store_error) THEN
1171 : CALL qs_diis_b_calc_err_kp(kpoints%scf_diis_buffer, ib, kp%mos, work%cksmat, work%csmat, &
1172 : ispin, ikp, work%cmos, local=.TRUE., gemm_ctx=work%gemm_ctx)
1173 : ELSE
1174 : CALL kp_solve_complex(kp%mos(ispin), work, scf_env, scf_control, use_adiis, .TRUE.)
1175 : END IF
1176 : END DO
1177 : END DO
1178 : END DO
1179 : !$OMP END DO
1180 45890 : DEALLOCATE (operators, block_work)
1181 : !$OMP END PARALLEL
1182 :
1183 45890 : END SUBROUTINE kp_diag_local_phase
1184 :
1185 : ! **************************************************************************************************
1186 : !> \brief Form the same candidate on local and distributed layouts, preserving raw DIIS history.
1187 : !> \param kpoints host DIIS buffer
1188 : !> \param ikp local k-point index
1189 : !> \param ispin spin index
1190 : !> \param coeffs DIIS coefficients
1191 : !> \param weight CDIIS blend weight
1192 : !> \param fock output candidate
1193 : !> \param adiis_fock ADIIS candidate, read only when weight is less than one
1194 : ! **************************************************************************************************
1195 49373 : SUBROUTINE kp_diis_candidate(kpoints, ikp, ispin, coeffs, weight, fock, adiis_fock)
1196 : TYPE(kpoint_type), POINTER :: kpoints
1197 : INTEGER, INTENT(IN) :: ikp, ispin
1198 : COMPLEX(KIND=dp), INTENT(IN) :: coeffs(:)
1199 : REAL(KIND=dp), INTENT(IN) :: weight
1200 : TYPE(cp_cfm_type), INTENT(INOUT) :: fock
1201 : TYPE(cp_cfm_type), INTENT(IN) :: adiis_fock
1202 :
1203 : INTEGER :: jb
1204 :
1205 49373 : CALL cp_cfm_set_all(fock, z_zero)
1206 214988 : DO jb = 1, SIZE(coeffs)
1207 214988 : CALL cp_cfm_scale_and_add(z_one, fock, coeffs(jb), kpoints%scf_diis_buffer%param(jb, ispin, ikp))
1208 : END DO
1209 49373 : IF (weight < 1.0_dp) CALL cp_cfm_scale_and_add(CMPLX(weight, KIND=dp), fock, &
1210 30 : CMPLX(1.0_dp - weight, KIND=dp), adiis_fock)
1211 49373 : END SUBROUTINE kp_diis_candidate
1212 :
1213 : ! **************************************************************************************************
1214 : !> \brief Shift, solve and publish a complex SCF candidate using private scratch.
1215 : !> \param mos complex MO set
1216 : !> \param work private workspace
1217 : !> \param scf_env SCF settings
1218 : !> \param scf_control SCF control
1219 : !> \param use_adiis apply the candidate shift
1220 : !> \param local select the one-rank local solver; otherwise honor the configured backend
1221 : ! **************************************************************************************************
1222 76923 : SUBROUTINE kp_solve_complex(mos, work, scf_env, scf_control, use_adiis, local)
1223 : TYPE(mo_set_type), INTENT(INOUT) :: mos
1224 : TYPE(kp_diag_workspace_type), INTENT(INOUT) :: work
1225 : TYPE(qs_scf_env_type), POINTER :: scf_env
1226 : TYPE(scf_control_type), POINTER :: scf_control
1227 : LOGICAL, INTENT(IN) :: use_adiis, local
1228 :
1229 76923 : REAL(KIND=dp), POINTER :: eigenvalues(:)
1230 :
1231 76923 : CALL get_mo_set(mos, eigenvalues=eigenvalues)
1232 76923 : IF (use_adiis) CALL shift_candidate_complex(work%cksmat, work%csmat, mos, work%cmos, work%cwork, &
1233 138 : adiis_candidate_shift(scf_env), local, work%gemm_ctx)
1234 76923 : IF (scf_env%cholesky_method == cholesky_off) THEN
1235 4515 : IF (local) THEN
1236 : CALL cp_cfm_geeig_canon_local(work%cksmat, work%csmat, mos%cmo_coeff, eigenvalues, work%cwork, &
1237 1367 : scf_control%eps_eigval, ws=work%solver)
1238 : ELSE
1239 : CALL cp_cfm_geeig_canon(work%cksmat, work%csmat, mos%cmo_coeff, eigenvalues, work%cwork, &
1240 3148 : scf_control%eps_eigval)
1241 : END IF
1242 : ELSE
1243 72408 : IF (local) THEN
1244 50072 : CALL cp_cfm_geeig_local(work%cksmat, work%csmat, mos%cmo_coeff, eigenvalues, ws=work%solver)
1245 : ELSE
1246 22336 : CALL cp_cfm_geeig(work%cksmat, work%csmat, mos%cmo_coeff, eigenvalues, work%cwork)
1247 : END IF
1248 : END IF
1249 76923 : END SUBROUTINE kp_solve_complex
1250 :
1251 : ! **************************************************************************************************
1252 : !> \brief Allocate all worker storage before launching numerical phases.
1253 : !> \param workspace returned private workspaces
1254 : !> \param nworkers number of workers
1255 : !> \param matrix_struct AO layout of the k-point group
1256 : !> \param mo_struct MO layout of the k-point group
1257 : !> \param local allocate local solver scratch
1258 : !> \param canonical use the canonical solver
1259 : !> \param lanczos allocate the block-Lanczos kernel scratch
1260 : !> \param nkrylov number of block expansion steps, sizes the kernel scratch
1261 : !> \param nblock number of columns refined jointly, sizes the kernel scratch
1262 : ! **************************************************************************************************
1263 37488 : SUBROUTINE kp_diag_workspaces_create(workspace, nworkers, matrix_struct, mo_struct, local, canonical, &
1264 : lanczos, nkrylov, nblock)
1265 : TYPE(kp_diag_workspace_type), ALLOCATABLE, &
1266 : INTENT(OUT) :: workspace(:)
1267 : INTEGER, INTENT(IN) :: nworkers
1268 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct, mo_struct
1269 : LOGICAL, INTENT(IN) :: local, canonical
1270 : LOGICAL, INTENT(IN), OPTIONAL :: lanczos
1271 : INTEGER, INTENT(IN), OPTIONAL :: nkrylov, nblock
1272 :
1273 : INTEGER :: i, n
1274 : LOGICAL :: my_lanczos
1275 :
1276 37488 : my_lanczos = .FALSE.
1277 37488 : IF (PRESENT(lanczos)) my_lanczos = lanczos
1278 :
1279 149952 : ALLOCATE (workspace(nworkers))
1280 74976 : DO i = 1, nworkers
1281 37488 : CALL cp_cfm_create(workspace(i)%cksmat, matrix_struct)
1282 37488 : CALL cp_cfm_create(workspace(i)%csmat, matrix_struct)
1283 37488 : CALL cp_cfm_create(workspace(i)%cwork, matrix_struct)
1284 37488 : CALL cp_cfm_create(workspace(i)%cmos, mo_struct)
1285 : ! the transfer rounds of the Lanczos driver also need the stored
1286 : ! overlap copy, although they never run the local solvers
1287 37488 : IF (local .OR. my_lanczos) THEN
1288 26560 : CALL cp_cfm_create(workspace(i)%csmat_base, matrix_struct)
1289 : END IF
1290 37488 : IF (local) THEN
1291 26430 : CALL workspace(i)%gemm_ctx%create(LOCAL_GEMM_PU_GPU, timing=.FALSE.)
1292 26430 : n = matrix_struct%nrow_global
1293 26430 : IF (canonical) THEN
1294 590 : CALL cp_cfm_local_workspace_prepare(workspace(i)%solver, n, workspace(i)%cksmat%local_data)
1295 : ELSE
1296 : CALL cp_cfm_local_workspace_prepare(workspace(i)%solver, n, workspace(i)%cksmat%local_data, &
1297 25840 : workspace(i)%csmat%local_data)
1298 : END IF
1299 : END IF
1300 74976 : IF (my_lanczos) THEN
1301 586 : CALL lanczos_workspace_create(workspace(i)%lan_ws, mo_struct, matrix_struct, nkrylov, nblock)
1302 : END IF
1303 : END DO
1304 37488 : END SUBROUTINE kp_diag_workspaces_create
1305 :
1306 : ! **************************************************************************************************
1307 : !> \brief Release dense matrices and automatic phase/LAPACK allocations after all workers finish.
1308 : !> \param workspace private workspaces
1309 : ! **************************************************************************************************
1310 37488 : SUBROUTINE kp_diag_workspaces_release(workspace)
1311 : TYPE(kp_diag_workspace_type), ALLOCATABLE, &
1312 : INTENT(INOUT) :: workspace(:)
1313 :
1314 : INTEGER :: i
1315 :
1316 74976 : DO i = 1, SIZE(workspace)
1317 37488 : CALL workspace(i)%gemm_ctx%destroy()
1318 37488 : CALL cp_cfm_release(workspace(i)%cksmat)
1319 37488 : CALL cp_cfm_release(workspace(i)%csmat)
1320 37488 : CALL cp_cfm_release(workspace(i)%csmat_base)
1321 37488 : CALL cp_cfm_release(workspace(i)%cwork)
1322 37488 : CALL cp_cfm_release(workspace(i)%cmos)
1323 74976 : CALL lanczos_workspace_release(workspace(i)%lan_ws)
1324 : END DO
1325 74976 : DEALLOCATE (workspace)
1326 37488 : END SUBROUTINE kp_diag_workspaces_release
1327 :
1328 : ! **************************************************************************************************
1329 : !> \brief Adaptive inner tolerance of an iterative solver from EPS_ADAPT:
1330 : !> loose while the density error sits far from self-consistency,
1331 : !> tracking that error at a fixed fraction and capped in the early
1332 : !> phase. A nonpositive factor or error disables the loosening and
1333 : !> returns the user threshold
1334 : !> \param eps_iter user threshold
1335 : !> \param eps_adapt loosening factor of the tracking
1336 : !> \param iter_error outer error the threshold tracks
1337 : !> \param noise_scale fraction of the outer error the solver noise keeps
1338 : !> \param cap bound of the early loose phase
1339 : !> \return ...
1340 : ! **************************************************************************************************
1341 700 : PURE FUNCTION kp_adaptive_eps_iter(eps_iter, eps_adapt, iter_error, noise_scale, cap) RESULT(eps)
1342 : REAL(KIND=dp), INTENT(IN) :: eps_iter, eps_adapt, iter_error, &
1343 : noise_scale, cap
1344 : REAL(KIND=dp) :: eps
1345 :
1346 700 : IF (eps_adapt <= 0.0_dp .OR. iter_error <= 0.0_dp) THEN
1347 612 : eps = eps_iter
1348 : ELSE
1349 88 : eps = MAX(eps_iter, MIN(eps_adapt*iter_error/noise_scale, cap))
1350 : END IF
1351 700 : END FUNCTION kp_adaptive_eps_iter
1352 :
1353 : ! **************************************************************************************************
1354 : !> \brief Occupations, ADDED_MOS_AUTO growth check and the density exit
1355 : !> chain shared by the k-point diagonalization drivers. The
1356 : !> routine keeps its own bookkeeping: a growing step abandons the
1357 : !> density of the abandoned SCF step
1358 : !> \param kpoints ...
1359 : !> \param scf_env ...
1360 : !> \param scf_control ...
1361 : !> \param matrix_s_kp real-space overlap matrix, dbcsr image set
1362 : !> \param sab_nl ...
1363 : !> \param fmwork global scratch fms of the driver
1364 : !> \param grow TRUE when the virtual-space buffer is too small for the
1365 : !> smearing tail: the outer loop grows the MOS and redoes this
1366 : !> SCF step
1367 : ! **************************************************************************************************
1368 1596 : SUBROUTINE kp_diag_exit_chain(kpoints, scf_env, scf_control, matrix_s_kp, sab_nl, fmwork, grow)
1369 : TYPE(kpoint_type), POINTER :: kpoints
1370 : TYPE(qs_scf_env_type), POINTER :: scf_env
1371 : TYPE(scf_control_type), POINTER :: scf_control
1372 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp
1373 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1374 : POINTER :: sab_nl
1375 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
1376 : LOGICAL, INTENT(OUT) :: grow
1377 :
1378 : CALL kpoint_set_mo_occupation(kpoints, scf_control%smear, &
1379 : added_mos_auto=scf_control%added_mos_auto, &
1380 1596 : added_mos_auto_grow=grow)
1381 1596 : IF (grow) RETURN
1382 1588 : CALL kpoint_density_matrices(kpoints)
1383 : CALL kpoint_density_transform(kpoints, scf_env%p_mix_new, .FALSE., &
1384 1588 : matrix_s_kp(1, 1)%matrix, sab_nl, fmwork, overlap_rs=matrix_s_kp)
1385 : END SUBROUTINE kp_diag_exit_chain
1386 :
1387 : ! **************************************************************************************************
1388 : !> \brief Per-column convergence thresholds of an iterative k-point
1389 : !> solver: the occupation decides which columns sit beyond the
1390 : !> thermal smearing tail and converge at the relaxed empty
1391 : !> threshold
1392 : !> \param occupation occupation numbers of the channel
1393 : !> \param eps_iter threshold of the occupied columns
1394 : !> \param eps_iter_empty relaxed threshold of the unoccupied columns
1395 : !> \param eps_col per-column thresholds, at least as long as the
1396 : !> occupation numbers
1397 : ! **************************************************************************************************
1398 764 : SUBROUTINE kp_eps_col_by_occupation(occupation, eps_iter, eps_iter_empty, eps_col)
1399 : REAL(KIND=dp), DIMENSION(:), INTENT(IN), POINTER :: occupation
1400 : REAL(KIND=dp), INTENT(IN) :: eps_iter, eps_iter_empty
1401 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: eps_col
1402 :
1403 : INTEGER :: imo
1404 :
1405 7916 : DO imo = 1, SIZE(occupation)
1406 7916 : IF (occupation(imo) < occ_tol) THEN
1407 4229 : eps_col(imo) = eps_iter_empty
1408 : ELSE
1409 2923 : eps_col(imo) = eps_iter
1410 : END IF
1411 : END DO
1412 764 : END SUBROUTINE kp_eps_col_by_occupation
1413 :
1414 : ! **************************************************************************************************
1415 : !> \brief Common setup of the iterative k-point drivers: the global
1416 : !> scratch fms, the operator context and the group matrix
1417 : !> structure
1418 : !> \param qs_env ...
1419 : !> \param kpoints ...
1420 : !> \param nspin number of spin components
1421 : !> \param matrix_ks_kp real-space Kohn-Sham matrices, dbcsr image set
1422 : !> \param matrix_s_kp real-space overlap matrix, dbcsr image set
1423 : !> \param fmwork returned global scratch fms
1424 : !> \param op_ctx returned operator context
1425 : !> \param matrix_struct returned AO layout of the k-point group
1426 : ! **************************************************************************************************
1427 1496 : SUBROUTINE kp_diag_setup(qs_env, kpoints, nspin, matrix_ks_kp, matrix_s_kp, fmwork, op_ctx, &
1428 : matrix_struct)
1429 : TYPE(qs_environment_type), POINTER :: qs_env
1430 : TYPE(kpoint_type), POINTER :: kpoints
1431 : INTEGER, INTENT(IN) :: nspin
1432 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp
1433 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
1434 : TYPE(kpoint_operator_context_type) :: op_ctx
1435 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
1436 :
1437 : INTEGER :: i
1438 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool, kp_ao_ao_fm_pool
1439 : TYPE(qs_matrix_pools_type), POINTER :: kp_mpools, mpools
1440 :
1441 1496 : CALL get_qs_env(qs_env, mpools=mpools)
1442 1496 : CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
1443 7480 : ALLOCATE (fmwork(4))
1444 7480 : DO i = 1, 4
1445 7480 : CALL fm_pool_create_fm(ao_ao_fm_pool, fmwork(i))
1446 : END DO
1447 1496 : CALL kpoint_operator_context_create(op_ctx, kpoints, fmwork(1), nspin)
1448 1496 : CALL kpoint_operator_prepare(op_ctx, matrix_ks_kp, kpoint_slot_ks)
1449 1496 : CALL kpoint_operator_prepare(op_ctx, matrix_s_kp, kpoint_slot_s, matrix_row=kpoint_spin_free)
1450 1496 : CALL get_kpoint_info(kpoints, mpools=kp_mpools)
1451 1496 : CALL mpools_get(kp_mpools, ao_ao_fm_pool=kp_ao_ao_fm_pool)
1452 1496 : matrix_struct => fm_pool_get_el_struct(kp_ao_ao_fm_pool)
1453 1496 : END SUBROUTINE kp_diag_setup
1454 :
1455 : ! **************************************************************************************************
1456 : !> \brief Common finalization of the iterative k-point drivers: release
1457 : !> the operator context and return the global scratch fms
1458 : !> \param qs_env ...
1459 : !> \param fmwork global scratch fms of the driver
1460 : !> \param op_ctx operator context of the driver
1461 : ! **************************************************************************************************
1462 1496 : SUBROUTINE kp_diag_finalize(qs_env, fmwork, op_ctx)
1463 : TYPE(qs_environment_type), POINTER :: qs_env
1464 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
1465 : TYPE(kpoint_operator_context_type) :: op_ctx
1466 :
1467 : INTEGER :: i
1468 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
1469 : TYPE(qs_matrix_pools_type), POINTER :: mpools
1470 :
1471 1496 : CALL kpoint_operator_context_release(op_ctx)
1472 1496 : CALL get_qs_env(qs_env, mpools=mpools)
1473 1496 : CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
1474 7480 : DO i = 1, 4
1475 7480 : CALL fm_pool_give_back_fm(ao_ao_fm_pool, fmwork(i))
1476 : END DO
1477 1496 : DEALLOCATE (fmwork)
1478 1496 : END SUBROUTINE kp_diag_finalize
1479 :
1480 : ! **************************************************************************************************
1481 : !> \brief Kpoint diagonalization routine
1482 : !> Transforms matrices to kpoint, distributes kpoint groups, performs
1483 : !> general diagonalization (no storgae of overlap decomposition), stores
1484 : !> MOs, calculates occupation numbers, calculates density matrices
1485 : !> in kpoint representation, transforms density matrices to real space
1486 : !> \param matrix_ks Kohn-sham matrices (RS indices, global)
1487 : !> \param matrix_s Overlap matrices (RS indices, global)
1488 : !> \param kpoints Kpoint environment
1489 : !> \param fmwork global-layout FM template in the first array entry
1490 : !> \par History
1491 : !> 08.2014 created [JGH]
1492 : ! **************************************************************************************************
1493 22 : SUBROUTINE diag_kp_basic(matrix_ks, matrix_s, kpoints, fmwork)
1494 :
1495 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s
1496 : TYPE(kpoint_type), POINTER :: kpoints
1497 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
1498 :
1499 : CHARACTER(len=*), PARAMETER :: routineN = 'diag_kp_basic'
1500 :
1501 : INTEGER :: handle, ikp, ispin, kplocal, kpmax, nspin
1502 : INTEGER, DIMENSION(2) :: kp_range
1503 22 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
1504 : LOGICAL :: use_real_wfn
1505 22 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
1506 : TYPE(cp_cfm_type) :: cksmat, cmos, csmat, cwork
1507 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
1508 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct, mo_struct
1509 : TYPE(cp_fm_type) :: fmlocal, rksmat, rsmat
1510 : TYPE(cp_fm_type), POINTER :: mo_coeff
1511 : TYPE(kpoint_env_type), POINTER :: kp
1512 22 : TYPE(kpoint_operator_context_type) :: op_ctx
1513 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1514 22 : POINTER :: sab_nl
1515 : TYPE(qs_matrix_pools_type), POINTER :: mpools
1516 :
1517 22 : CALL timeset(routineN, handle)
1518 :
1519 22 : NULLIFY (sab_nl)
1520 : CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn, kp_range=kp_range, &
1521 22 : kp_dist=kp_dist, sab_nl=sab_nl)
1522 22 : CPASSERT(ASSOCIATED(sab_nl))
1523 22 : kplocal = kp_range(2) - kp_range(1) + 1
1524 46 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
1525 :
1526 : ! fm pools to be used within a kpoint group
1527 22 : CALL get_kpoint_info(kpoints, mpools=mpools)
1528 22 : CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
1529 :
1530 22 : CALL fm_pool_create_fm(ao_ao_fm_pool, fmlocal)
1531 22 : CALL cp_fm_get_info(fmlocal, matrix_struct=matrix_struct)
1532 :
1533 22 : IF (use_real_wfn) THEN
1534 0 : CALL cp_fm_create(rksmat, matrix_struct)
1535 0 : CALL cp_fm_create(rsmat, matrix_struct)
1536 : ELSE
1537 22 : CALL cp_cfm_create(cksmat, matrix_struct)
1538 22 : CALL cp_cfm_create(csmat, matrix_struct)
1539 22 : CALL cp_cfm_create(cwork, matrix_struct)
1540 22 : kp => kpoints%kp_env(1)%kpoint_env
1541 22 : mo_struct => kp%mos(1)%cmo_coeff%matrix_struct
1542 22 : CALL cp_cfm_create(cmos, mo_struct)
1543 : END IF
1544 :
1545 22 : nspin = SIZE(matrix_ks, 1)
1546 22 : CALL kpoint_operator_context_create(op_ctx, kpoints, fmwork(1), nspin)
1547 22 : CALL kpoint_operator_prepare(op_ctx, matrix_ks, kpoint_slot_ks)
1548 22 : CALL kpoint_operator_prepare(op_ctx, matrix_s, kpoint_slot_s, matrix_row=kpoint_spin_free)
1549 94 : DO ikp = 1, kpmax
1550 166 : DO ispin = 1, nspin
1551 : CALL kpoint_operator_start(op_ctx, ikp, ispin, matrix_ks, kpoint_slot_ks, &
1552 72 : matrix_row=ispin)
1553 : CALL kpoint_operator_start(op_ctx, ikp, ispin, matrix_s, kpoint_slot_s, &
1554 144 : matrix_row=kpoint_spin_free)
1555 : END DO
1556 : END DO
1557 :
1558 : ! Finish communication then diagonalise in each group
1559 94 : DO ikp = 1, kpmax
1560 166 : DO ispin = 1, nspin
1561 72 : IF (use_real_wfn) THEN
1562 0 : CALL kpoint_operator_finish(op_ctx, ikp, ispin, kpoint_slot_ks, fm_re=rksmat)
1563 0 : CALL kpoint_operator_finish(op_ctx, ikp, ispin, kpoint_slot_s, fm_re=rsmat)
1564 : ELSE
1565 72 : CALL kpoint_operator_finish(op_ctx, ikp, ispin, kpoint_slot_ks, cksmat)
1566 72 : CALL kpoint_operator_finish(op_ctx, ikp, ispin, kpoint_slot_s, csmat)
1567 : END IF
1568 :
1569 : ! Each kpoint group has now information on a kpoint to be diagonalized
1570 : ! General eigensolver Hermite or Symmetric
1571 72 : IF (ikp > kplocal) CYCLE
1572 72 : kp => kpoints%kp_env(ikp)%kpoint_env
1573 144 : IF (use_real_wfn) THEN
1574 0 : CALL get_mo_set(kp%mos(ispin), mo_coeff=mo_coeff, eigenvalues=eigenvalues)
1575 0 : CALL cp_fm_geeig(rksmat, rsmat, mo_coeff, eigenvalues, fmlocal)
1576 : ELSE
1577 72 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
1578 72 : CALL cp_cfm_geeig(cksmat, csmat, cmos, eigenvalues, cwork)
1579 72 : CALL cp_cfm_to_cfm(cmos, kp%mos(ispin)%cmo_coeff)
1580 : END IF
1581 : END DO
1582 : END DO
1583 :
1584 22 : CALL kpoint_operator_context_release(op_ctx)
1585 :
1586 22 : IF (use_real_wfn) THEN
1587 0 : CALL cp_fm_release(rksmat)
1588 0 : CALL cp_fm_release(rsmat)
1589 : ELSE
1590 22 : CALL cp_cfm_release(cksmat)
1591 22 : CALL cp_cfm_release(csmat)
1592 22 : CALL cp_cfm_release(cwork)
1593 22 : CALL cp_cfm_release(cmos)
1594 : END IF
1595 22 : CALL fm_pool_give_back_fm(ao_ao_fm_pool, fmlocal)
1596 :
1597 22 : CALL timestop(handle)
1598 :
1599 44 : END SUBROUTINE diag_kp_basic
1600 :
1601 : ! **************************************************************************************************
1602 : !> \brief inner loop within MOS subspace, to refine occupation and density,
1603 : !> before next diagonalization of the Hamiltonian
1604 : !> \param qs_env ...
1605 : !> \param scf_env ...
1606 : !> \param subspace_env ...
1607 : !> \param mos ...
1608 : !> \param rho ...
1609 : !> \param ks_env ...
1610 : !> \param scf_section ...
1611 : !> \param scf_control ...
1612 : !> \par History
1613 : !> 09.2009 created [MI]
1614 : !> \note it is assumed that when diagonalization is used, also some mixing procedure is active
1615 : ! **************************************************************************************************
1616 10 : SUBROUTINE do_scf_diag_subspace(qs_env, scf_env, subspace_env, mos, rho, &
1617 : ks_env, scf_section, scf_control)
1618 :
1619 : TYPE(qs_environment_type), POINTER :: qs_env
1620 : TYPE(qs_scf_env_type), POINTER :: scf_env
1621 : TYPE(subspace_env_type), POINTER :: subspace_env
1622 : TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
1623 : TYPE(qs_rho_type), POINTER :: rho
1624 : TYPE(qs_ks_env_type), POINTER :: ks_env
1625 : TYPE(section_vals_type), POINTER :: scf_section
1626 : TYPE(scf_control_type), POINTER :: scf_control
1627 :
1628 : CHARACTER(LEN=*), PARAMETER :: routineN = 'do_scf_diag_subspace'
1629 : REAL(KIND=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
1630 :
1631 : INTEGER :: handle, i, iloop, ispin, nao, nmo, &
1632 : nspin, output_unit
1633 : LOGICAL :: converged
1634 : REAL(dp) :: ene_diff, ene_old, iter_delta, max_val, &
1635 : sum_band, sum_val, t1, t2
1636 10 : REAL(KIND=dp), DIMENSION(:), POINTER :: mo_eigenvalues, mo_occupations
1637 10 : TYPE(cp_1d_r_p_type), ALLOCATABLE, DIMENSION(:) :: eval_first, occ_first
1638 : TYPE(cp_fm_type) :: work
1639 : TYPE(cp_fm_type), POINTER :: c0, chc, evec, mo_coeff
1640 : TYPE(cp_logger_type), POINTER :: logger
1641 10 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s, rho_ao
1642 10 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
1643 : TYPE(dft_control_type), POINTER :: dft_control
1644 : TYPE(mp_para_env_type), POINTER :: para_env
1645 : TYPE(qs_energy_type), POINTER :: energy
1646 10 : TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho_atom
1647 :
1648 10 : CALL timeset(routineN, handle)
1649 10 : NULLIFY (c0, chc, energy, evec, matrix_ks, mo_coeff, mo_eigenvalues, &
1650 10 : mo_occupations, dft_control, rho_ao, rho_ao_kp)
1651 :
1652 10 : logger => cp_get_default_logger()
1653 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DIAG_SUB_SCF", &
1654 10 : extension=".scfLog")
1655 :
1656 : !Extra loop keeping mos unchanged and refining the subspace occupation
1657 10 : nspin = SIZE(mos)
1658 10 : CALL qs_rho_get(rho, rho_ao=rho_ao, rho_ao_kp=rho_ao_kp)
1659 :
1660 40 : ALLOCATE (eval_first(nspin))
1661 40 : ALLOCATE (occ_first(nspin))
1662 20 : DO ispin = 1, nspin
1663 : CALL get_mo_set(mo_set=mos(ispin), &
1664 : nmo=nmo, &
1665 : eigenvalues=mo_eigenvalues, &
1666 10 : occupation_numbers=mo_occupations)
1667 30 : ALLOCATE (eval_first(ispin)%array(nmo))
1668 20 : ALLOCATE (occ_first(ispin)%array(nmo))
1669 50 : eval_first(ispin)%array(1:nmo) = mo_eigenvalues(1:nmo)
1670 70 : occ_first(ispin)%array(1:nmo) = mo_occupations(1:nmo)
1671 : END DO
1672 :
1673 20 : DO ispin = 1, nspin
1674 : ! does not yet handle k-points
1675 10 : CALL dbcsr_copy(subspace_env%p_matrix_store(ispin)%matrix, rho_ao(ispin)%matrix)
1676 20 : CALL dbcsr_copy(rho_ao(ispin)%matrix, scf_env%p_mix_new(ispin, 1)%matrix)
1677 : END DO
1678 :
1679 10 : subspace_env%p_matrix_mix => scf_env%p_mix_new
1680 :
1681 10 : NULLIFY (matrix_ks, energy, para_env, matrix_s)
1682 : CALL get_qs_env(qs_env, &
1683 : matrix_ks=matrix_ks, &
1684 : energy=energy, &
1685 : matrix_s=matrix_s, &
1686 : para_env=para_env, &
1687 10 : dft_control=dft_control)
1688 :
1689 : ! mixing storage allocation
1690 10 : IF (subspace_env%mixing_method >= gspace_mixing_nr) THEN
1691 : CALL mixing_allocate(qs_env, subspace_env%mixing_method, scf_env%p_mix_new, &
1692 0 : scf_env%p_delta, nspin, subspace_env%mixing_store)
1693 0 : IF (dft_control%qs_control%gapw) THEN
1694 0 : CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
1695 : CALL mixing_init(subspace_env%mixing_method, rho, subspace_env%mixing_store, &
1696 0 : para_env, rho_atom=rho_atom)
1697 0 : ELSE IF (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb) THEN
1698 0 : CALL charge_mixing_init(subspace_env%mixing_store)
1699 0 : ELSE IF (dft_control%qs_control%semi_empirical) THEN
1700 0 : CPABORT('SE Code not possible')
1701 : ELSE
1702 0 : CALL mixing_init(subspace_env%mixing_method, rho, subspace_env%mixing_store, para_env)
1703 : END IF
1704 : END IF
1705 :
1706 10 : ene_old = 0.0_dp
1707 : ene_diff = 0.0_dp
1708 10 : IF (output_unit > 0) THEN
1709 0 : WRITE (output_unit, "(/T19,A)") '<<<<<<<<< SUBSPACE ROTATION <<<<<<<<<<'
1710 : WRITE (output_unit, "(T4,A,T13,A,T21,A,T38,A,T51,A,T65,A/,T4,A)") &
1711 0 : "In-step", "Time", "Convergence", "Band ene.", "Total ene.", "Energy diff.", REPEAT("-", 74)
1712 : END IF
1713 :
1714 : ! recalculate density matrix here
1715 :
1716 : ! update of density
1717 10 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1718 :
1719 22 : DO iloop = 1, subspace_env%max_iter
1720 20 : t1 = m_walltime()
1721 20 : converged = .FALSE.
1722 20 : ene_old = energy%total
1723 :
1724 20 : CALL qs_ks_did_change(ks_env, rho_changed=.TRUE.)
1725 : CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., &
1726 20 : just_energy=.FALSE., print_active=.FALSE.)
1727 :
1728 20 : max_val = 0.0_dp
1729 20 : sum_val = 0.0_dp
1730 20 : sum_band = 0.0_dp
1731 40 : DO ispin = 1, SIZE(matrix_ks)
1732 : CALL get_mo_set(mo_set=mos(ispin), &
1733 : nao=nao, &
1734 : nmo=nmo, &
1735 : eigenvalues=mo_eigenvalues, &
1736 : occupation_numbers=mo_occupations, &
1737 20 : mo_coeff=mo_coeff)
1738 :
1739 : !compute C'HC
1740 20 : chc => subspace_env%chc_mat(ispin)
1741 20 : evec => subspace_env%c_vec(ispin)
1742 20 : c0 => subspace_env%c0(ispin)
1743 20 : CALL cp_fm_to_fm(mo_coeff, c0)
1744 20 : CALL cp_fm_create(work, c0%matrix_struct)
1745 20 : CALL cp_dbcsr_sm_fm_multiply(matrix_ks(ispin)%matrix, c0, work, nmo)
1746 20 : CALL parallel_gemm('T', 'N', nmo, nmo, nao, rone, c0, work, rzero, chc)
1747 20 : CALL cp_fm_release(work)
1748 : !diagonalize C'HC
1749 20 : CALL choose_eigv_solver(chc, evec, mo_eigenvalues)
1750 :
1751 : !rotate the mos by the eigenvectors of C'HC
1752 20 : CALL parallel_gemm('N', 'N', nao, nmo, nmo, rone, c0, evec, rzero, mo_coeff)
1753 :
1754 20 : IF (.NOT. scf_control%gce%do_gce) THEN
1755 : CALL set_mo_occupation(mo_set=mos(ispin), &
1756 20 : smear=scf_control%smear)
1757 : ELSE
1758 : CALL set_mo_occupation(mo_set=mos(ispin), &
1759 : smear=scf_control%smear, &
1760 0 : gce=scf_control%gce)
1761 : END IF
1762 :
1763 : ! does not yet handle k-points
1764 : CALL calculate_density_matrix(mos(ispin), &
1765 20 : subspace_env%p_matrix_mix(ispin, 1)%matrix)
1766 :
1767 160 : DO i = 1, nmo
1768 100 : sum_band = sum_band + mo_eigenvalues(i)*mo_occupations(i)
1769 : END DO
1770 :
1771 : !check for self consistency
1772 : END DO
1773 :
1774 20 : IF (subspace_env%mixing_method == direct_mixing_nr) THEN
1775 : CALL scf_env_density_mixing(subspace_env%p_matrix_mix, &
1776 20 : scf_env%mixing_store, rho_ao_kp, para_env, iter_delta, iloop)
1777 : ELSE
1778 : CALL self_consistency_check(rho_ao_kp, scf_env%p_delta, para_env, &
1779 0 : subspace_env%p_matrix_mix, delta=iter_delta)
1780 : END IF
1781 :
1782 40 : DO ispin = 1, nspin
1783 : ! does not yet handle k-points
1784 40 : CALL dbcsr_copy(rho_ao(ispin)%matrix, subspace_env%p_matrix_mix(ispin, 1)%matrix)
1785 : END DO
1786 : ! update of density
1787 20 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1788 : ! Mixing in reciprocal space
1789 20 : IF (subspace_env%mixing_method >= gspace_mixing_nr) THEN
1790 : CALL gspace_mixing(qs_env, scf_env%mixing_method, subspace_env%mixing_store, &
1791 0 : rho, para_env, scf_env%iter_count)
1792 : END IF
1793 :
1794 20 : ene_diff = energy%total - ene_old
1795 : converged = (ABS(ene_diff) < subspace_env%eps_ene .AND. &
1796 20 : iter_delta < subspace_env%eps_adapt*scf_env%iter_delta)
1797 20 : t2 = m_walltime()
1798 20 : IF (output_unit > 0) THEN
1799 : WRITE (output_unit, "(T4,I5,T11,F8.3,T18,E14.4,T34,F12.5,T46,F16.8,T62,E14.4)") &
1800 0 : iloop, t2 - t1, iter_delta, sum_band, energy%total, ene_diff
1801 0 : CALL m_flush(output_unit)
1802 : END IF
1803 22 : IF (converged) THEN
1804 8 : IF (output_unit > 0) WRITE (output_unit, "(T10,A,I6,A,/)") &
1805 0 : " Reached convergence in ", iloop, " iterations "
1806 : EXIT
1807 : END IF
1808 :
1809 : END DO ! iloop
1810 :
1811 10 : NULLIFY (subspace_env%p_matrix_mix)
1812 20 : DO ispin = 1, nspin
1813 : ! does not yet handle k-points
1814 10 : CALL dbcsr_copy(scf_env%p_mix_new(ispin, 1)%matrix, rho_ao(ispin)%matrix)
1815 10 : CALL dbcsr_copy(rho_ao(ispin)%matrix, subspace_env%p_matrix_store(ispin)%matrix)
1816 :
1817 20 : DEALLOCATE (eval_first(ispin)%array, occ_first(ispin)%array)
1818 : END DO
1819 10 : DEALLOCATE (eval_first, occ_first)
1820 :
1821 10 : CALL timestop(handle)
1822 :
1823 10 : END SUBROUTINE do_scf_diag_subspace
1824 :
1825 : ! **************************************************************************************************
1826 : !> \brief ...
1827 : !> \param subspace_env ...
1828 : !> \param qs_env ...
1829 : !> \param mos ...
1830 : ! **************************************************************************************************
1831 2 : SUBROUTINE diag_subspace_allocate(subspace_env, qs_env, mos)
1832 :
1833 : TYPE(subspace_env_type), POINTER :: subspace_env
1834 : TYPE(qs_environment_type), POINTER :: qs_env
1835 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
1836 :
1837 : CHARACTER(LEN=*), PARAMETER :: routineN = 'diag_subspace_allocate'
1838 :
1839 : INTEGER :: handle, i, ispin, nmo, nspin
1840 : TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
1841 : TYPE(cp_fm_type), POINTER :: mo_coeff
1842 2 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
1843 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1844 2 : POINTER :: sab_orb
1845 :
1846 2 : CALL timeset(routineN, handle)
1847 :
1848 2 : NULLIFY (sab_orb, matrix_s)
1849 : CALL get_qs_env(qs_env=qs_env, sab_orb=sab_orb, &
1850 2 : matrix_s=matrix_s)
1851 :
1852 2 : nspin = SIZE(mos)
1853 : ! *** allocate p_atrix_store ***
1854 2 : IF (.NOT. ASSOCIATED(subspace_env%p_matrix_store)) THEN
1855 2 : CALL dbcsr_allocate_matrix_set(subspace_env%p_matrix_store, nspin)
1856 :
1857 4 : DO i = 1, nspin
1858 2 : ALLOCATE (subspace_env%p_matrix_store(i)%matrix)
1859 : CALL dbcsr_create(matrix=subspace_env%p_matrix_store(i)%matrix, template=matrix_s(1)%matrix, &
1860 2 : name="DENSITY_STORE", matrix_type=dbcsr_type_symmetric)
1861 : CALL cp_dbcsr_alloc_block_from_nbl(subspace_env%p_matrix_store(i)%matrix, &
1862 2 : sab_orb)
1863 4 : CALL dbcsr_set(subspace_env%p_matrix_store(i)%matrix, 0.0_dp)
1864 : END DO
1865 :
1866 : END IF
1867 :
1868 8 : ALLOCATE (subspace_env%chc_mat(nspin))
1869 6 : ALLOCATE (subspace_env%c_vec(nspin))
1870 6 : ALLOCATE (subspace_env%c0(nspin))
1871 :
1872 4 : DO ispin = 1, nspin
1873 2 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
1874 2 : CALL cp_fm_create(subspace_env%c0(ispin), mo_coeff%matrix_struct)
1875 2 : NULLIFY (fm_struct_tmp)
1876 : CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=nmo, ncol_global=nmo, &
1877 : para_env=mo_coeff%matrix_struct%para_env, &
1878 2 : context=mo_coeff%matrix_struct%context)
1879 2 : CALL cp_fm_create(subspace_env%chc_mat(ispin), fm_struct_tmp, "chc")
1880 2 : CALL cp_fm_create(subspace_env%c_vec(ispin), fm_struct_tmp, "vec")
1881 6 : CALL cp_fm_struct_release(fm_struct_tmp)
1882 : END DO
1883 :
1884 2 : CALL timestop(handle)
1885 :
1886 2 : END SUBROUTINE diag_subspace_allocate
1887 :
1888 : ! **************************************************************************************************
1889 : !> \brief the inner loop of scf, specific to diagonalization without S matrix
1890 : !> basically, in goes the ks matrix out goes a new p matrix
1891 : !> \param scf_env ...
1892 : !> \param mos ...
1893 : !> \param matrix_ks ...
1894 : !> \param scf_control ...
1895 : !> \param scf_section ...
1896 : !> \param diis_step ...
1897 : !> \par History
1898 : !> 03.2006 created [Joost VandeVondele]
1899 : ! **************************************************************************************************
1900 17876 : SUBROUTINE do_special_diag(scf_env, mos, matrix_ks, scf_control, &
1901 : scf_section, diis_step)
1902 :
1903 : TYPE(qs_scf_env_type), POINTER :: scf_env
1904 : TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
1905 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
1906 : TYPE(scf_control_type), POINTER :: scf_control
1907 : TYPE(section_vals_type), POINTER :: scf_section
1908 : LOGICAL, INTENT(INOUT) :: diis_step
1909 :
1910 : INTEGER :: ispin, nspin
1911 : LOGICAL :: do_level_shift, use_jacobi
1912 : REAL(KIND=dp) :: diis_error
1913 :
1914 17876 : nspin = SIZE(matrix_ks)
1915 :
1916 36518 : DO ispin = 1, nspin
1917 : CALL copy_dbcsr_to_fm(matrix_ks(ispin)%matrix, scf_env%scf_work1(ispin), &
1918 36518 : scf_env%fm_copy_plan(ispin), check_plan=.TRUE.)
1919 : END DO
1920 17876 : IF (scf_env%iter_count > 1 .AND. .NOT. scf_env%skip_diis) THEN
1921 : CALL qs_diis_b_step(scf_env%scf_diis_buffer, mos, scf_env%scf_work1, &
1922 : scf_env%scf_work2, scf_env%iter_delta, diis_error, diis_step, &
1923 : scf_control%eps_diis, scf_control%nmixing, &
1924 15282 : scf_section=scf_section)
1925 : ELSE
1926 2594 : diis_step = .FALSE.
1927 : END IF
1928 :
1929 17876 : IF ((scf_env%iter_count > 1) .AND. (scf_env%iter_delta < scf_control%diagonalization%eps_jacobi)) THEN
1930 18 : use_jacobi = .TRUE.
1931 : ELSE
1932 17858 : use_jacobi = .FALSE.
1933 : END IF
1934 :
1935 : do_level_shift = ((scf_control%level_shift /= 0.0_dp) .AND. &
1936 17876 : ((scf_control%density_guess == core_guess) .OR. (scf_env%iter_count > 1)))
1937 17876 : IF (diis_step) THEN
1938 11870 : scf_env%iter_param = diis_error
1939 11870 : IF (use_jacobi) THEN
1940 18 : scf_env%iter_method = "DIIS/Jacobi"
1941 : ELSE
1942 11852 : scf_env%iter_method = "DIIS/Diag."
1943 : END IF
1944 : ELSE
1945 6006 : IF (scf_env%mixing_method == 1) THEN
1946 6006 : scf_env%iter_param = scf_env%p_mix_alpha
1947 6006 : IF (use_jacobi) THEN
1948 0 : scf_env%iter_method = "P_Mix/Jacobi"
1949 : ELSE
1950 6006 : scf_env%iter_method = "P_Mix/Diag."
1951 : END IF
1952 0 : ELSE IF (scf_env%mixing_method > 1) THEN
1953 0 : scf_env%iter_param = scf_env%mixing_store%alpha
1954 0 : IF (use_jacobi) THEN
1955 0 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Jacobi"
1956 : ELSE
1957 0 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Diag."
1958 : END IF
1959 : END IF
1960 : END IF
1961 17876 : scf_env%iter_delta = 0.0_dp
1962 :
1963 36518 : DO ispin = 1, nspin
1964 : CALL eigensolver_simple(matrix_ks=scf_env%scf_work1(ispin), &
1965 : mo_set=mos(ispin), &
1966 : work=scf_env%scf_work2, &
1967 : do_level_shift=do_level_shift, &
1968 : level_shift=scf_control%level_shift, &
1969 : use_jacobi=use_jacobi, &
1970 36518 : jacobi_threshold=scf_control%diagonalization%jacobi_threshold)
1971 : END DO
1972 :
1973 17876 : IF (.NOT. scf_control%gce%do_gce) THEN
1974 : CALL set_mo_occupation(mo_array=mos, &
1975 17876 : smear=scf_control%smear)
1976 : ELSE
1977 : CALL set_mo_occupation(mo_array=mos, &
1978 : smear=scf_control%smear, &
1979 0 : gce=scf_control%gce)
1980 : END IF
1981 :
1982 36518 : DO ispin = 1, nspin
1983 : ! does not yet handle k-points
1984 : CALL calculate_density_matrix(mos(ispin), &
1985 36518 : scf_env%p_mix_new(ispin, 1)%matrix)
1986 : END DO
1987 :
1988 17876 : END SUBROUTINE do_special_diag
1989 :
1990 : ! **************************************************************************************************
1991 : !> \brief the inner loop of scf, specific to iterative diagonalization using OT
1992 : !> with S matrix; basically, in goes the ks matrix out goes a new p matrix
1993 : !> \param scf_env ...
1994 : !> \param mos ...
1995 : !> \param matrix_ks ...
1996 : !> \param matrix_s ...
1997 : !> \param scf_control ...
1998 : !> \param scf_section ...
1999 : !> \param diis_step ...
2000 : !> \par History
2001 : !> 10.2008 created [JGH]
2002 : ! **************************************************************************************************
2003 202 : SUBROUTINE do_ot_diag(scf_env, mos, matrix_ks, matrix_s, &
2004 : scf_control, scf_section, diis_step)
2005 :
2006 : TYPE(qs_scf_env_type), POINTER :: scf_env
2007 : TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
2008 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
2009 : TYPE(scf_control_type), POINTER :: scf_control
2010 : TYPE(section_vals_type), POINTER :: scf_section
2011 : LOGICAL, INTENT(INOUT) :: diis_step
2012 :
2013 : INTEGER :: homo, ispin, nmo, nspin
2014 : REAL(KIND=dp) :: diis_error, eps_iter
2015 202 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
2016 : TYPE(cp_fm_type), POINTER :: mo_coeff
2017 :
2018 202 : NULLIFY (eigenvalues)
2019 :
2020 202 : nspin = SIZE(matrix_ks)
2021 :
2022 448 : DO ispin = 1, nspin
2023 : CALL copy_dbcsr_to_fm(matrix_ks(ispin)%matrix, &
2024 448 : scf_env%scf_work1(ispin))
2025 : END DO
2026 :
2027 202 : IF ((scf_env%iter_count > 1) .AND. (.NOT. scf_env%skip_diis)) THEN
2028 : CALL qs_diis_b_step(scf_env%scf_diis_buffer, mos, scf_env%scf_work1, &
2029 : scf_env%scf_work2, scf_env%iter_delta, diis_error, diis_step, &
2030 : scf_control%eps_diis, scf_control%nmixing, &
2031 : s_matrix=matrix_s, &
2032 180 : scf_section=scf_section)
2033 : ELSE
2034 22 : diis_step = .FALSE.
2035 : END IF
2036 :
2037 202 : eps_iter = scf_control%diagonalization%eps_iter
2038 202 : IF (diis_step) THEN
2039 130 : scf_env%iter_param = diis_error
2040 130 : scf_env%iter_method = "DIIS/OTdiag"
2041 274 : DO ispin = 1, nspin
2042 : CALL copy_fm_to_dbcsr(scf_env%scf_work1(ispin), &
2043 274 : matrix_ks(ispin)%matrix, keep_sparsity=.TRUE.)
2044 : END DO
2045 130 : eps_iter = MAX(eps_iter, scf_control%diagonalization%eps_adapt*diis_error)
2046 : ELSE
2047 72 : IF (scf_env%mixing_method == 1) THEN
2048 72 : scf_env%iter_param = scf_env%p_mix_alpha
2049 72 : scf_env%iter_method = "P_Mix/OTdiag."
2050 0 : ELSE IF (scf_env%mixing_method > 1) THEN
2051 0 : scf_env%iter_param = scf_env%mixing_store%alpha
2052 0 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/OTdiag."
2053 : END IF
2054 : END IF
2055 :
2056 202 : scf_env%iter_delta = 0.0_dp
2057 :
2058 448 : DO ispin = 1, nspin
2059 : CALL get_mo_set(mos(ispin), &
2060 : mo_coeff=mo_coeff, &
2061 : eigenvalues=eigenvalues, &
2062 : nmo=nmo, &
2063 246 : homo=homo)
2064 : CALL ot_eigensolver(matrix_h=matrix_ks(ispin)%matrix, &
2065 : matrix_s=matrix_s(1)%matrix, &
2066 : matrix_c_fm=mo_coeff, &
2067 : preconditioner=scf_env%ot_preconditioner( &
2068 : MIN(ispin, SIZE(scf_env%ot_preconditioner)))%preconditioner, &
2069 : eps_gradient=eps_iter, &
2070 : iter_max=scf_control%diagonalization%max_iter, &
2071 : silent=.TRUE., &
2072 246 : ot_settings=scf_control%diagonalization%ot_settings)
2073 : CALL calculate_subspace_eigenvalues(mo_coeff, matrix_ks(ispin)%matrix, &
2074 : evals_arg=eigenvalues, &
2075 246 : do_rotation=.TRUE.)
2076 : CALL copy_fm_to_dbcsr(mos(ispin)%mo_coeff, &
2077 694 : mos(ispin)%mo_coeff_b)
2078 : !fm->dbcsr
2079 : END DO
2080 :
2081 : CALL set_mo_occupation(mo_array=mos, &
2082 202 : smear=scf_control%smear)
2083 :
2084 448 : DO ispin = 1, nspin
2085 : ! does not yet handle k-points
2086 : CALL calculate_density_matrix(mos(ispin), &
2087 448 : scf_env%p_mix_new(ispin, 1)%matrix)
2088 : END DO
2089 :
2090 202 : END SUBROUTINE do_ot_diag
2091 :
2092 : ! **************************************************************************************************
2093 : !> \brief fixed-H OT diagonalization for real or complex K-point channels
2094 : !> \param matrix_ks real-space Kohn-Sham matrices
2095 : !> \param matrix_s real-space overlap matrices
2096 : !> \param matrix_t ...
2097 : !> \param kpoints K-point environment
2098 : !> \param scf_env SCF environment
2099 : !> \param scf_control SCF controls
2100 : !> \param diis_step set false; density mixing remains the outer accelerator
2101 : !> \param added_mos_auto_grow request a larger adaptive virtual-space buffer
2102 : ! **************************************************************************************************
2103 100 : SUBROUTINE do_ot_diag_kp(matrix_ks, matrix_s, matrix_t, kpoints, scf_env, scf_control, &
2104 : diis_step, added_mos_auto_grow)
2105 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s, matrix_t
2106 : TYPE(kpoint_type), POINTER :: kpoints
2107 : TYPE(qs_scf_env_type), POINTER :: scf_env
2108 : TYPE(scf_control_type), POINTER :: scf_control
2109 : LOGICAL, INTENT(INOUT) :: diis_step
2110 : LOGICAL, INTENT(OUT) :: added_mos_auto_grow
2111 :
2112 : INTEGER :: ikp, ispin, ks_spin, nspin
2113 : LOGICAL :: store_kinetic, use_real_wfn
2114 : REAL(KIND=dp) :: eps_iter
2115 100 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
2116 : TYPE(cp_cfm_type) :: cmo_work
2117 : TYPE(cp_fm_type) :: coeff_im_work
2118 100 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
2119 : TYPE(cp_fm_type), POINTER :: coeff_re
2120 : TYPE(dbcsr_type), POINTER :: matrix_h_im, matrix_h_re, matrix_s_im, &
2121 : matrix_s_re, matrix_t_im, matrix_t_re
2122 : TYPE(kpoint_env_type), POINTER :: kp
2123 100 : TYPE(kpoint_operator_context_type) :: op_ctx
2124 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2125 100 : POINTER :: sab_nl
2126 :
2127 100 : NULLIFY (coeff_re, eigenvalues, fmwork, kp, matrix_h_im, matrix_h_re, &
2128 100 : matrix_s_im, matrix_s_re, matrix_t_im, matrix_t_re, sab_nl)
2129 100 : added_mos_auto_grow = .FALSE.
2130 100 : diis_step = .FALSE.
2131 100 : eps_iter = scf_control%diagonalization%eps_iter
2132 100 : nspin = SIZE(matrix_ks, 1)
2133 : store_kinetic = scf_control%diagonalization%ot_settings%preconditioner_type == &
2134 100 : ot_precond_full_kinetic
2135 100 : IF (store_kinetic) THEN
2136 0 : CPASSERT(ASSOCIATED(matrix_t))
2137 : END IF
2138 100 : CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn, sab_nl=sab_nl)
2139 100 : CPASSERT(ASSOCIATED(sab_nl))
2140 :
2141 : ! A full diagonalization supplies an orthonormal start after initialization or an
2142 : ! ADDED_MOS AUTO resize. Subsequent SCF steps only redistribute H(k) and S(k).
2143 100 : IF (scf_env%iter_count <= 1) THEN
2144 : CALL do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, &
2145 10 : .FALSE., diis_step)
2146 : END IF
2147 100 : CALL kpoint_operator_context_create(op_ctx, kpoints, scf_env%scf_work1(1), nspin)
2148 100 : IF (store_kinetic) THEN
2149 0 : CALL kpoint_operator_store(op_ctx, matrix_ks, matrix_s, matrix_t)
2150 : ELSE
2151 100 : CALL kpoint_operator_store(op_ctx, matrix_ks, matrix_s)
2152 : END IF
2153 100 : CALL kpoint_operator_context_release(op_ctx)
2154 :
2155 100 : scf_env%iter_delta = 0.0_dp
2156 284 : DO ikp = 1, SIZE(kpoints%kp_env)
2157 184 : kp => kpoints%kp_env(ikp)%kpoint_env
2158 184 : CPASSERT(ASSOCIATED(kp%ot_hmat) .AND. ASSOCIATED(kp%ot_smat))
2159 184 : IF (SIZE(kp%ot_smat) >= 2) THEN
2160 : CALL kpoint_operator_get_local(matrix_s, kpoints, kp, 1, &
2161 : kp%ot_smat(1), kp%ot_smat(2), &
2162 184 : matrix_s_re, matrix_s_im)
2163 : ELSE
2164 : CALL kpoint_operator_get_local(matrix_s, kpoints, kp, 1, &
2165 : kp%ot_smat(1), kp%ot_smat(1), &
2166 0 : matrix_s_re, matrix_s_im)
2167 0 : CALL dbcsr_set(matrix_s_im, 0.0_dp)
2168 : END IF
2169 184 : IF (store_kinetic) THEN
2170 0 : CPASSERT(ASSOCIATED(kp%ot_tmat))
2171 0 : IF (SIZE(kp%ot_tmat) >= 2) THEN
2172 : CALL kpoint_operator_get_local(matrix_t, kpoints, kp, 1, &
2173 : kp%ot_tmat(1), kp%ot_tmat(2), &
2174 0 : matrix_t_re, matrix_t_im)
2175 : ELSE
2176 : CALL kpoint_operator_get_local(matrix_t, kpoints, kp, 1, &
2177 : kp%ot_tmat(1), kp%ot_tmat(1), &
2178 0 : matrix_t_re, matrix_t_im)
2179 0 : CALL dbcsr_set(matrix_t_im, 0.0_dp)
2180 : END IF
2181 : END IF
2182 368 : DO ispin = 1, nspin
2183 184 : ks_spin = MIN(ispin, SIZE(kp%ot_hmat, 2))
2184 184 : IF (SIZE(kp%ot_hmat, 1) >= 2) THEN
2185 : CALL kpoint_operator_get_local(matrix_ks, kpoints, kp, ks_spin, &
2186 : kp%ot_hmat(1, ks_spin), kp%ot_hmat(2, ks_spin), &
2187 184 : matrix_h_re, matrix_h_im)
2188 : ELSE
2189 : CALL kpoint_operator_get_local(matrix_ks, kpoints, kp, ks_spin, &
2190 : kp%ot_hmat(1, ks_spin), kp%ot_hmat(1, ks_spin), &
2191 0 : matrix_h_re, matrix_h_im)
2192 0 : CALL dbcsr_set(matrix_h_im, 0.0_dp)
2193 : END IF
2194 184 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2195 184 : IF (use_real_wfn) THEN
2196 0 : CALL get_mo_set(kp%mos(ispin), mo_coeff=coeff_re)
2197 : CALL cp_fm_create(coeff_im_work, coeff_re%matrix_struct, &
2198 0 : name='real k-point OT imaginary work')
2199 0 : CALL cp_cfm_create(cmo_work, coeff_re%matrix_struct)
2200 0 : CALL cp_cfm_scale_and_add_fm(z_zero, cmo_work, z_one, coeff_re)
2201 : CALL ot_eigensolver_complex(matrix_h=matrix_h_re, matrix_h_im=matrix_h_im, &
2202 : matrix_s=matrix_s_re, matrix_s_im=matrix_s_im, &
2203 : matrix_c_cfm=cmo_work, &
2204 : eps_gradient=eps_iter, &
2205 : iter_max=scf_control%diagonalization%max_iter, &
2206 : eigenvalues=eigenvalues, silent=.TRUE., &
2207 : ot_settings=scf_control%diagonalization%ot_settings, &
2208 : matrix_t=matrix_t_re, matrix_t_im=matrix_t_im, &
2209 0 : mo_set=kp%mos(ispin))
2210 0 : CALL cp_cfm_to_fm(cmo_work, coeff_re, coeff_im_work)
2211 0 : CALL cp_cfm_release(cmo_work)
2212 0 : CALL cp_fm_release(coeff_im_work)
2213 : ELSE
2214 : CALL ot_eigensolver_complex(matrix_h=matrix_h_re, matrix_h_im=matrix_h_im, &
2215 : matrix_s=matrix_s_re, matrix_s_im=matrix_s_im, &
2216 : matrix_c_cfm=kp%mos(ispin)%cmo_coeff, &
2217 : eps_gradient=eps_iter, &
2218 : iter_max=scf_control%diagonalization%max_iter, &
2219 : eigenvalues=eigenvalues, silent=.TRUE., &
2220 : ot_settings=scf_control%diagonalization%ot_settings, &
2221 : matrix_t=matrix_t_re, matrix_t_im=matrix_t_im, &
2222 184 : mo_set=kp%mos(ispin))
2223 : END IF
2224 184 : CALL dbcsr_release_p(matrix_h_re)
2225 368 : CALL dbcsr_release_p(matrix_h_im)
2226 : END DO
2227 184 : CALL dbcsr_release_p(matrix_s_re)
2228 184 : CALL dbcsr_release_p(matrix_s_im)
2229 184 : IF (ASSOCIATED(matrix_t_re)) CALL dbcsr_release_p(matrix_t_re)
2230 284 : IF (ASSOCIATED(matrix_t_im)) CALL dbcsr_release_p(matrix_t_im)
2231 : END DO
2232 :
2233 100 : fmwork => scf_env%scf_work1
2234 100 : CALL kp_diag_exit_chain(kpoints, scf_env, scf_control, matrix_s, sab_nl, fmwork, added_mos_auto_grow)
2235 100 : IF (added_mos_auto_grow) RETURN
2236 100 : IF (scf_env%mixing_method == 0) THEN
2237 0 : scf_env%iter_method = 'NoMix/OTdiag.'
2238 100 : ELSE IF (scf_env%mixing_method == 1) THEN
2239 66 : scf_env%iter_param = scf_env%p_mix_alpha
2240 66 : scf_env%iter_method = 'P_Mix/OTdiag.'
2241 : ELSE
2242 34 : scf_env%iter_param = scf_env%mixing_store%alpha
2243 34 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//'/OTdiag.'
2244 : END IF
2245 200 : END SUBROUTINE do_ot_diag_kp
2246 :
2247 : ! **************************************************************************************************
2248 : !> \brief Solve a set restricted open Kohn-Sham (ROKS) equations based on the
2249 : !> alpha and beta Kohn-Sham matrices from unrestricted Kohn-Sham.
2250 : !> \param scf_env ...
2251 : !> \param mos ...
2252 : !> \param matrix_ks ...
2253 : !> \param matrix_s ...
2254 : !> \param scf_control ...
2255 : !> \param scf_section ...
2256 : !> \param diis_step ...
2257 : !> \param orthogonal_basis ...
2258 : !> \par History
2259 : !> 04.2006 created [MK]
2260 : !> Revised (01.05.06,MK)
2261 : !> \note
2262 : !> this is only a high-spin ROKS.
2263 : ! **************************************************************************************************
2264 1122 : SUBROUTINE do_roks_diag(scf_env, mos, matrix_ks, matrix_s, &
2265 : scf_control, scf_section, diis_step, &
2266 : orthogonal_basis)
2267 :
2268 : ! Literature: - C. C. J. Roothaan, Rev. Mod. Phys. 32, 179 (1960)
2269 : ! - M. F. Guest and V. R. Saunders, Mol. Phys. 28(3), 819 (1974)
2270 : ! - M. Filatov and S. Shaik, Chem. Phys. Lett. 288, 689 (1998)
2271 :
2272 : TYPE(qs_scf_env_type), POINTER :: scf_env
2273 : TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos
2274 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
2275 : TYPE(scf_control_type), POINTER :: scf_control
2276 : TYPE(section_vals_type), POINTER :: scf_section
2277 : LOGICAL, INTENT(INOUT) :: diis_step
2278 : LOGICAL, INTENT(IN) :: orthogonal_basis
2279 :
2280 : CHARACTER(LEN=*), PARAMETER :: routineN = 'do_roks_diag'
2281 :
2282 : INTEGER :: handle, homoa, homob, imo, nalpha, nao, &
2283 : nbeta, nmo
2284 : REAL(KIND=dp) :: diis_error, level_shift_loc
2285 1122 : REAL(KIND=dp), DIMENSION(:), POINTER :: eiga, eigb, occa, occb
2286 : TYPE(cp_fm_type), POINTER :: ksa, ksb, mo2ao, moa, mob, ortho, work
2287 :
2288 : ! -------------------------------------------------------------------------
2289 :
2290 1122 : CALL timeset(routineN, handle)
2291 :
2292 1122 : IF (scf_env%cholesky_method == cholesky_inverse) THEN
2293 0 : ortho => scf_env%ortho_m1
2294 : ELSE
2295 1122 : ortho => scf_env%ortho
2296 : END IF
2297 1122 : work => scf_env%scf_work2
2298 :
2299 1122 : ksa => scf_env%scf_work1(1)
2300 1122 : ksb => scf_env%scf_work1(2)
2301 :
2302 1122 : CALL copy_dbcsr_to_fm(matrix_ks(1)%matrix, ksa, scf_env%fm_copy_plan(1), check_plan=.TRUE.)
2303 1122 : CALL copy_dbcsr_to_fm(matrix_ks(2)%matrix, ksb, scf_env%fm_copy_plan(2), check_plan=.TRUE.)
2304 :
2305 : ! Get MO information
2306 :
2307 : CALL get_mo_set(mo_set=mos(1), &
2308 : nao=nao, &
2309 : nmo=nmo, &
2310 : nelectron=nalpha, &
2311 : homo=homoa, &
2312 : eigenvalues=eiga, &
2313 : occupation_numbers=occa, &
2314 1122 : mo_coeff=moa)
2315 :
2316 : CALL get_mo_set(mo_set=mos(2), &
2317 : nelectron=nbeta, &
2318 : homo=homob, &
2319 : eigenvalues=eigb, &
2320 : occupation_numbers=occb, &
2321 1122 : mo_coeff=mob)
2322 :
2323 : ! Define the amount of level-shifting
2324 :
2325 1122 : IF ((scf_control%level_shift /= 0.0_dp) .AND. &
2326 : ((scf_control%density_guess == core_guess) .OR. &
2327 : (scf_control%density_guess == restart_guess) .OR. &
2328 : (scf_env%iter_count > 1))) THEN
2329 20 : level_shift_loc = scf_control%level_shift
2330 : ELSE
2331 1102 : level_shift_loc = 0.0_dp
2332 : END IF
2333 :
2334 : IF ((scf_env%iter_count > 1) .OR. &
2335 1122 : (scf_control%density_guess == core_guess) .OR. &
2336 : (scf_control%density_guess == restart_guess)) THEN
2337 :
2338 : ! Transform the spin unrestricted alpha and beta Kohn-Sham matrices
2339 : ! from AO basis to MO basis: K(MO) = C(T)*K(AO)*C
2340 :
2341 1012 : CALL cp_fm_symm("L", "U", nao, nao, 1.0_dp, ksa, moa, 0.0_dp, work)
2342 1012 : CALL parallel_gemm("T", "N", nao, nao, nao, 1.0_dp, moa, work, 0.0_dp, ksa)
2343 :
2344 1012 : CALL cp_fm_symm("L", "U", nao, nao, 1.0_dp, ksb, moa, 0.0_dp, work)
2345 1012 : CALL parallel_gemm("T", "N", nao, nao, nao, 1.0_dp, moa, work, 0.0_dp, ksb)
2346 :
2347 : ! Combine the spin unrestricted alpha and beta Kohn-Sham matrices
2348 : ! in the MO basis
2349 :
2350 1012 : IF (scf_control%roks_scheme == general_roks) THEN
2351 : CALL combine_ks_matrices(ksa, ksb, occa, occb, scf_control%roks_f, &
2352 0 : nalpha, nbeta)
2353 1012 : ELSE IF (scf_control%roks_scheme == high_spin_roks) THEN
2354 1012 : CALL combine_ks_matrices(ksa, ksb, occa, occb, scf_control%roks_parameter)
2355 : ELSE
2356 0 : CPABORT("Unknown ROKS scheme requested")
2357 : END IF
2358 :
2359 : ! Back-transform the restricted open Kohn-Sham matrix from MO basis
2360 : ! to AO basis
2361 :
2362 1012 : IF (orthogonal_basis) THEN
2363 : ! Q = C
2364 464 : mo2ao => moa
2365 : ELSE
2366 : ! Q = S*C
2367 548 : mo2ao => mob
2368 : !MK CALL copy_sm_to_fm(matrix_s(1)%matrix,work)
2369 : !MK CALL cp_fm_symm("L", "U",nao, nao, 1.0_dp, work, moa, 0.0_dp, mo2ao)
2370 548 : CALL cp_dbcsr_sm_fm_multiply(matrix_s(1)%matrix, moa, mo2ao, nao)
2371 : END IF
2372 :
2373 : ! K(AO) = Q*K(MO)*Q(T)
2374 :
2375 1012 : CALL parallel_gemm("N", "T", nao, nao, nao, 1.0_dp, ksa, mo2ao, 0.0_dp, work)
2376 1012 : CALL parallel_gemm("N", "N", nao, nao, nao, 1.0_dp, mo2ao, work, 0.0_dp, ksa)
2377 :
2378 : ELSE
2379 :
2380 : ! No transformation matrix available, yet. The closed shell part,
2381 : ! i.e. the beta Kohn-Sham matrix in AO basis, is taken.
2382 : ! There might be better choices, anyhow.
2383 :
2384 110 : CALL cp_fm_to_fm(ksb, ksa)
2385 :
2386 : END IF
2387 :
2388 : ! Update DIIS buffer and possibly perform DIIS extrapolation step
2389 :
2390 1122 : IF (scf_env%iter_count > 1) THEN
2391 1006 : IF (orthogonal_basis) THEN
2392 : CALL qs_diis_b_step(diis_buffer=scf_env%scf_diis_buffer, &
2393 : mo_array=mos, &
2394 : kc=scf_env%scf_work1, &
2395 : sc=work, &
2396 : delta=scf_env%iter_delta, &
2397 : error_max=diis_error, &
2398 : diis_step=diis_step, &
2399 : eps_diis=scf_control%eps_diis, &
2400 : scf_section=scf_section, &
2401 460 : roks=.TRUE.)
2402 460 : CPASSERT(scf_env%iter_delta == scf_env%iter_delta)
2403 : ELSE
2404 : CALL qs_diis_b_step(diis_buffer=scf_env%scf_diis_buffer, &
2405 : mo_array=mos, &
2406 : kc=scf_env%scf_work1, &
2407 : sc=work, &
2408 : delta=scf_env%iter_delta, &
2409 : error_max=diis_error, &
2410 : diis_step=diis_step, &
2411 : eps_diis=scf_control%eps_diis, &
2412 : scf_section=scf_section, &
2413 : s_matrix=matrix_s, &
2414 546 : roks=.TRUE.)
2415 : END IF
2416 : END IF
2417 :
2418 1122 : IF (diis_step) THEN
2419 756 : scf_env%iter_param = diis_error
2420 756 : scf_env%iter_method = "DIIS/Diag."
2421 : ELSE
2422 366 : IF (scf_env%mixing_method == 1) THEN
2423 366 : scf_env%iter_param = scf_env%p_mix_alpha
2424 366 : scf_env%iter_method = "P_Mix/Diag."
2425 0 : ELSE IF (scf_env%mixing_method > 1) THEN
2426 0 : scf_env%iter_param = scf_env%mixing_store%alpha
2427 0 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Diag."
2428 : END IF
2429 : END IF
2430 :
2431 1122 : scf_env%iter_delta = 0.0_dp
2432 :
2433 1122 : IF (level_shift_loc /= 0.0_dp) THEN
2434 :
2435 : ! Transform the current Kohn-Sham matrix from AO to MO basis
2436 : ! for level-shifting using the current MO set
2437 :
2438 20 : CALL cp_fm_symm("L", "U", nao, nao, 1.0_dp, ksa, moa, 0.0_dp, work)
2439 20 : CALL parallel_gemm("T", "N", nao, nao, nao, 1.0_dp, moa, work, 0.0_dp, ksa)
2440 :
2441 : ! Apply level-shifting using 50:50 split of the shift (could be relaxed)
2442 :
2443 60 : DO imo = homob + 1, homoa
2444 60 : CALL cp_fm_add_to_element(ksa, imo, imo, 0.5_dp*level_shift_loc)
2445 : END DO
2446 220 : DO imo = homoa + 1, nmo
2447 220 : CALL cp_fm_add_to_element(ksa, imo, imo, level_shift_loc)
2448 : END DO
2449 :
2450 1102 : ELSE IF (.NOT. orthogonal_basis) THEN
2451 :
2452 : ! Transform the current Kohn-Sham matrix to an orthogonal basis
2453 580 : SELECT CASE (scf_env%cholesky_method)
2454 : CASE (cholesky_reduce)
2455 0 : CALL cp_fm_cholesky_reduce(ksa, ortho)
2456 : CASE (cholesky_restore)
2457 516 : CALL cp_fm_uplo_to_full(ksa, work)
2458 : CALL cp_fm_cholesky_restore(ksa, nao, ortho, work, &
2459 516 : "SOLVE", pos="RIGHT")
2460 : CALL cp_fm_cholesky_restore(work, nao, ortho, ksa, &
2461 516 : "SOLVE", pos="LEFT", transa="T")
2462 : CASE (cholesky_inverse)
2463 0 : CALL cp_fm_uplo_to_full(ksa, work)
2464 : CALL cp_fm_cholesky_restore(ksa, nao, ortho, work, &
2465 0 : "MULTIPLY", pos="RIGHT")
2466 : CALL cp_fm_cholesky_restore(work, nao, ortho, ksa, &
2467 0 : "MULTIPLY", pos="LEFT", transa="T")
2468 : CASE (cholesky_off)
2469 64 : CALL cp_fm_symm("L", "U", nao, nao, 1.0_dp, ksa, ortho, 0.0_dp, work)
2470 644 : CALL parallel_gemm("N", "N", nao, nao, nao, 1.0_dp, ortho, work, 0.0_dp, ksa)
2471 : END SELECT
2472 :
2473 : END IF
2474 :
2475 : ! Diagonalization of the ROKS operator matrix
2476 :
2477 1122 : CALL choose_eigv_solver(ksa, work, eiga)
2478 :
2479 : ! Back-transformation of the orthonormal eigenvectors if needed
2480 :
2481 1122 : IF (level_shift_loc /= 0.0_dp) THEN
2482 : ! Use old MO set for back-transformation if level-shifting was applied
2483 20 : CALL cp_fm_to_fm(moa, ortho)
2484 20 : CALL parallel_gemm("N", "N", nao, nmo, nao, 1.0_dp, ortho, work, 0.0_dp, moa)
2485 : ELSE
2486 1102 : IF (orthogonal_basis) THEN
2487 522 : CALL cp_fm_to_fm(work, moa)
2488 : ELSE
2489 1096 : SELECT CASE (scf_env%cholesky_method)
2490 : CASE (cholesky_reduce, cholesky_restore)
2491 516 : CALL cp_fm_cholesky_restore(work, nmo, ortho, moa, "SOLVE")
2492 : CASE (cholesky_inverse)
2493 0 : CALL cp_fm_cholesky_restore(work, nmo, ortho, moa, "MULTIPLY")
2494 : CASE (cholesky_off)
2495 580 : CALL parallel_gemm("N", "N", nao, nmo, nao, 1.0_dp, ortho, work, 0.0_dp, moa)
2496 : END SELECT
2497 : END IF
2498 : END IF
2499 :
2500 : ! Correct MO eigenvalues, if level-shifting was applied
2501 :
2502 1122 : IF (level_shift_loc /= 0.0_dp) THEN
2503 60 : DO imo = homob + 1, homoa
2504 60 : eiga(imo) = eiga(imo) - 0.5_dp*level_shift_loc
2505 : END DO
2506 220 : DO imo = homoa + 1, nmo
2507 220 : eiga(imo) = eiga(imo) - level_shift_loc
2508 : END DO
2509 : END IF
2510 :
2511 : ! Update also the beta MO set
2512 :
2513 34830 : eigb(:) = eiga(:)
2514 1122 : CALL cp_fm_to_fm(moa, mob)
2515 :
2516 : ! Calculate the new alpha and beta density matrix
2517 :
2518 : ! does not yet handle k-points
2519 1122 : CALL calculate_density_matrix(mos(1), scf_env%p_mix_new(1, 1)%matrix)
2520 1122 : CALL calculate_density_matrix(mos(2), scf_env%p_mix_new(2, 1)%matrix)
2521 :
2522 1122 : CALL timestop(handle)
2523 :
2524 1122 : END SUBROUTINE do_roks_diag
2525 :
2526 : ! **************************************************************************************************
2527 : !> \brief iterative diagonalization using the block Krylov-space approach
2528 : !> \param scf_env ...
2529 : !> \param mos ...
2530 : !> \param matrix_ks ...
2531 : !> \param scf_control ...
2532 : !> \param scf_section ...
2533 : !> \param check_moconv_only ...
2534 : !> \param
2535 : !> \par History
2536 : !> 05.2009 created [MI]
2537 : ! **************************************************************************************************
2538 :
2539 114 : SUBROUTINE do_block_krylov_diag(scf_env, mos, matrix_ks, &
2540 : scf_control, scf_section, check_moconv_only)
2541 :
2542 : TYPE(qs_scf_env_type), POINTER :: scf_env
2543 : TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
2544 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks
2545 : TYPE(scf_control_type), POINTER :: scf_control
2546 : TYPE(section_vals_type), POINTER :: scf_section
2547 : LOGICAL, INTENT(IN), OPTIONAL :: check_moconv_only
2548 :
2549 : CHARACTER(LEN=*), PARAMETER :: routineN = 'do_block_krylov_diag'
2550 : REAL(KIND=dp), PARAMETER :: rone = 1.0_dp, rzero = 0.0_dp
2551 :
2552 : INTEGER :: handle, homo, ispin, iter, nao, nmo, &
2553 : output_unit
2554 : LOGICAL :: converged, my_check_moconv_only
2555 : REAL(dp) :: eps_iter, t1, t2
2556 114 : REAL(KIND=dp), DIMENSION(:), POINTER :: mo_eigenvalues
2557 : TYPE(cp_fm_type), POINTER :: c0, c1, chc, evec, ks, mo_coeff, ortho, &
2558 : work
2559 : TYPE(cp_logger_type), POINTER :: logger
2560 :
2561 228 : logger => cp_get_default_logger()
2562 114 : CALL timeset(routineN, handle)
2563 :
2564 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%LANCZOS", &
2565 114 : extension=".scfLog")
2566 :
2567 114 : my_check_moconv_only = .FALSE.
2568 114 : IF (PRESENT(check_moconv_only)) my_check_moconv_only = check_moconv_only
2569 :
2570 114 : NULLIFY (mo_coeff, ortho, work, ks)
2571 114 : NULLIFY (mo_eigenvalues)
2572 114 : NULLIFY (c0, c1)
2573 :
2574 114 : IF (scf_env%cholesky_method == cholesky_inverse) THEN
2575 38 : ortho => scf_env%ortho_m1
2576 : ELSE
2577 76 : ortho => scf_env%ortho
2578 : END IF
2579 114 : work => scf_env%scf_work2
2580 :
2581 228 : DO ispin = 1, SIZE(matrix_ks)
2582 : CALL copy_dbcsr_to_fm(matrix_ks(ispin)%matrix, &
2583 228 : scf_env%scf_work1(ispin), scf_env%fm_copy_plan(ispin), check_plan=.TRUE.)
2584 : END DO
2585 :
2586 114 : IF (scf_env%mixing_method == 1) THEN
2587 0 : scf_env%iter_param = scf_env%p_mix_alpha
2588 0 : scf_env%iter_method = "P_Mix/Lanczos"
2589 : ELSE
2590 114 : scf_env%iter_param = scf_env%mixing_store%alpha
2591 114 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Lanc."
2592 : END IF
2593 :
2594 228 : DO ispin = 1, SIZE(matrix_ks)
2595 :
2596 114 : ks => scf_env%scf_work1(ispin)
2597 114 : CALL cp_fm_uplo_to_full(ks, work)
2598 :
2599 : CALL get_mo_set(mo_set=mos(ispin), &
2600 : nao=nao, &
2601 : nmo=nmo, &
2602 : homo=homo, &
2603 : eigenvalues=mo_eigenvalues, &
2604 114 : mo_coeff=mo_coeff)
2605 :
2606 114 : NULLIFY (c0, c1)
2607 114 : c0 => scf_env%krylov_space%mo_conv(ispin)
2608 114 : c1 => scf_env%krylov_space%mo_refine(ispin)
2609 114 : SELECT CASE (scf_env%cholesky_method)
2610 : CASE (cholesky_reduce)
2611 0 : CALL cp_fm_cholesky_reduce(ks, ortho)
2612 0 : CALL cp_fm_uplo_to_full(ks, work)
2613 0 : CALL cp_fm_cholesky_restore(mo_coeff, nmo, ortho, c0, "MULTIPLY")
2614 : CASE (cholesky_restore)
2615 : CALL cp_fm_cholesky_restore(ks, nao, ortho, work, &
2616 76 : "SOLVE", pos="RIGHT")
2617 : CALL cp_fm_cholesky_restore(work, nao, ortho, ks, &
2618 76 : "SOLVE", pos="LEFT", transa="T")
2619 76 : CALL cp_fm_cholesky_restore(mo_coeff, nmo, ortho, c0, "MULTIPLY")
2620 : CASE (cholesky_inverse)
2621 : CALL cp_fm_cholesky_restore(ks, nao, ortho, work, &
2622 38 : "MULTIPLY", pos="RIGHT")
2623 : CALL cp_fm_cholesky_restore(work, nao, ortho, ks, &
2624 38 : "MULTIPLY", pos="LEFT", transa="T")
2625 152 : CALL cp_fm_cholesky_restore(mo_coeff, nmo, ortho, c0, "SOLVE")
2626 : END SELECT
2627 :
2628 114 : scf_env%krylov_space%nmo_nc = nmo
2629 114 : scf_env%krylov_space%nmo_conv = 0
2630 :
2631 114 : t1 = m_walltime()
2632 114 : IF (output_unit > 0) THEN
2633 0 : WRITE (output_unit, "(/T15,A)") '<<<<<<<<< LANCZOS REFINEMENT <<<<<<<<<<'
2634 : WRITE (output_unit, "(T8,A,T15,A,T23,A,T36,A,T49,A,T60,A,/,T8,A)") &
2635 0 : " Spin ", " Cycle ", &
2636 0 : " conv. MOS ", " B2MAX ", " B2MIN ", " Time", REPEAT("-", 60)
2637 : END IF
2638 : eps_iter = kp_adaptive_eps_iter(scf_env%krylov_settings%eps_conv, scf_env%krylov_settings%eps_adapt, &
2639 114 : scf_env%iter_delta, 1.0_dp, HUGE(1.0_dp))
2640 114 : iter = 0
2641 114 : converged = .FALSE.
2642 : !Check convergence of MOS
2643 342 : IF (my_check_moconv_only) THEN
2644 :
2645 : CALL lanczos_refinement(scf_env%krylov_space, ks, c0, c1, mo_eigenvalues, &
2646 0 : nao, eps_iter, ispin, check_moconv_only=my_check_moconv_only)
2647 0 : t2 = m_walltime()
2648 0 : IF (output_unit > 0) THEN
2649 : WRITE (output_unit, '(T8,I3,T16,I5,T24,I6,T33,E12.4,2x,E12.4,T60,F8.3)') &
2650 0 : ispin, iter, scf_env%krylov_space%nmo_conv, &
2651 0 : scf_env%krylov_space%max_res_norm, scf_env%krylov_space%min_res_norm, t2 - t1
2652 : END IF
2653 :
2654 : CYCLE
2655 : ELSE
2656 : !Block Lanczos refinement
2657 848 : DO iter = 1, scf_env%krylov_settings%max_iter
2658 : CALL lanczos_refinement_2v(scf_env%krylov_space, ks, c0, c1, mo_eigenvalues, &
2659 744 : nao, eps_iter, ispin)
2660 744 : t2 = m_walltime()
2661 744 : IF (output_unit > 0) THEN
2662 : WRITE (output_unit, '(T8,I3,T16,I5,T24,I6,T33,E12.4,2x,E12.4,T60,F8.3)') &
2663 0 : ispin, iter, scf_env%krylov_space%nmo_conv, &
2664 0 : scf_env%krylov_space%max_res_norm, scf_env%krylov_space%min_res_norm, t2 - t1
2665 : END IF
2666 744 : t1 = m_walltime()
2667 848 : IF (scf_env%krylov_space%max_res_norm < eps_iter) THEN
2668 10 : converged = .TRUE.
2669 10 : IF (output_unit > 0) WRITE (output_unit, *) &
2670 0 : " Reached convergence in ", iter, " iterations "
2671 : EXIT
2672 : END IF
2673 : END DO
2674 :
2675 114 : IF (.NOT. converged .AND. output_unit > 0) THEN
2676 : WRITE (output_unit, "(T4, A)") " WARNING Lanczos refinement could "// &
2677 0 : "not converge all the mos:"
2678 0 : WRITE (output_unit, "(T40,A,T70,I10)") " number of not converged mos ", &
2679 0 : scf_env%krylov_space%nmo_nc
2680 0 : WRITE (output_unit, "(T40,A,T70,E10.2)") " max norm of the residual ", &
2681 0 : scf_env%krylov_space%max_res_norm
2682 :
2683 : END IF
2684 :
2685 : ! For the moment skip the re-orthogonalization
2686 : IF (.FALSE.) THEN
2687 : !Re-orthogonalization
2688 : NULLIFY (chc, evec)
2689 : chc => scf_env%krylov_space%chc_mat(ispin)
2690 : evec => scf_env%krylov_space%c_vec(ispin)
2691 : CALL parallel_gemm('N', 'N', nao, nmo, nao, rone, ks, c0, rzero, work)
2692 : CALL parallel_gemm('T', 'N', nmo, nmo, nao, rone, c0, work, rzero, chc)
2693 : !Diagonalize (C^t)HC
2694 : CALL choose_eigv_solver(chc, evec, mo_eigenvalues)
2695 : !Rotate the C vectors
2696 : CALL parallel_gemm('N', 'N', nao, nmo, nmo, rone, c0, evec, rzero, c1)
2697 : c0 => scf_env%krylov_space%mo_refine(ispin)
2698 : END IF
2699 :
2700 114 : IF (scf_env%cholesky_method == cholesky_inverse) THEN
2701 38 : CALL cp_fm_cholesky_restore(c0, nmo, ortho, mo_coeff, "MULTIPLY")
2702 : ELSE
2703 76 : CALL cp_fm_cholesky_restore(c0, nmo, ortho, mo_coeff, "SOLVE")
2704 : END IF
2705 :
2706 114 : IF (.NOT. scf_control%gce%do_gce) THEN
2707 : CALL set_mo_occupation(mo_set=mos(ispin), &
2708 114 : smear=scf_control%smear)
2709 : ELSE
2710 : CALL set_mo_occupation(mo_set=mos(ispin), &
2711 : smear=scf_control%smear, &
2712 0 : gce=scf_control%gce)
2713 : END IF
2714 :
2715 : ! does not yet handle k-points
2716 : CALL calculate_density_matrix(mos(ispin), &
2717 114 : scf_env%p_mix_new(ispin, 1)%matrix)
2718 : END IF
2719 : END DO ! ispin
2720 :
2721 114 : IF (output_unit > 0) THEN
2722 0 : WRITE (output_unit, "(T15,A/)") '<<<<<<<<< END LANCZOS REFINEMENT <<<<<<<<<<'
2723 : END IF
2724 :
2725 : CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
2726 114 : "PRINT%LANCZOS")
2727 :
2728 114 : CALL timestop(handle)
2729 :
2730 114 : END SUBROUTINE do_block_krylov_diag
2731 :
2732 : ! **************************************************************************************************
2733 : !> \brief iterative diagonalization using the block davidson space approach
2734 : !> \param qs_env ...
2735 : !> \param scf_env ...
2736 : !> \param mos ...
2737 : !> \param matrix_ks ...
2738 : !> \param matrix_s ...
2739 : !> \param scf_control ...
2740 : !> \param scf_section ...
2741 : !> \param check_moconv_only ...
2742 : !> \param
2743 : !> \par History
2744 : !> 05.2011 created [MI]
2745 : ! **************************************************************************************************
2746 :
2747 94 : SUBROUTINE do_block_davidson_diag(qs_env, scf_env, mos, matrix_ks, matrix_s, &
2748 : scf_control, scf_section, check_moconv_only)
2749 :
2750 : TYPE(qs_environment_type), POINTER :: qs_env
2751 : TYPE(qs_scf_env_type), POINTER :: scf_env
2752 : TYPE(mo_set_type), DIMENSION(:), INTENT(INOUT) :: mos
2753 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s
2754 : TYPE(scf_control_type), POINTER :: scf_control
2755 : TYPE(section_vals_type), POINTER :: scf_section
2756 : LOGICAL, INTENT(IN), OPTIONAL :: check_moconv_only
2757 :
2758 : CHARACTER(LEN=*), PARAMETER :: routineN = 'do_block_davidson_diag'
2759 :
2760 : INTEGER :: handle, ispin, nspins, output_unit
2761 : LOGICAL :: do_prec, my_check_moconv_only
2762 : TYPE(cp_logger_type), POINTER :: logger
2763 :
2764 94 : logger => cp_get_default_logger()
2765 94 : CALL timeset(routineN, handle)
2766 :
2767 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DAVIDSON", &
2768 94 : extension=".scfLog")
2769 :
2770 94 : IF (output_unit > 0) THEN
2771 0 : WRITE (output_unit, "(/T15,A)") '<<<<<<<<< DAVIDSON ITERATIONS <<<<<<<<<<'
2772 : END IF
2773 :
2774 94 : IF (scf_env%mixing_method == 1) THEN
2775 0 : scf_env%iter_param = scf_env%p_mix_alpha
2776 0 : scf_env%iter_method = "P_Mix/Dav."
2777 : ELSE
2778 94 : scf_env%iter_param = scf_env%mixing_store%alpha
2779 94 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Dav."
2780 : END IF
2781 :
2782 94 : my_check_moconv_only = .FALSE.
2783 94 : IF (PRESENT(check_moconv_only)) my_check_moconv_only = check_moconv_only
2784 94 : do_prec = .FALSE.
2785 94 : IF (scf_env%block_davidson_env(1)%prec_type /= 0 .AND. &
2786 : scf_env%iter_count >= scf_env%block_davidson_env(1)%first_prec) THEN
2787 84 : do_prec = .TRUE.
2788 : END IF
2789 :
2790 94 : nspins = SIZE(matrix_ks)
2791 :
2792 94 : IF (do_prec .AND. (scf_env%iter_count == scf_env%block_davidson_env(1)%first_prec .OR. &
2793 : MODULO(scf_env%iter_count, scf_env%block_davidson_env(1)%niter_new_prec) == 0)) THEN
2794 : CALL restart_preconditioner(qs_env, scf_env%ot_preconditioner, &
2795 18 : prec_type=scf_env%block_davidson_env(1)%prec_type, nspins=nspins)
2796 : CALL prepare_preconditioner(qs_env, mos, matrix_ks, matrix_s, scf_env%ot_preconditioner, &
2797 : scf_env%block_davidson_env(1)%prec_type, &
2798 : scf_env%block_davidson_env(1)%solver_type, &
2799 : scf_env%block_davidson_env(1)%energy_gap, nspins, &
2800 : convert_to_dbcsr=scf_env%block_davidson_env(1)%use_sparse_mos, &
2801 18 : full_mo_set=.TRUE.)
2802 : END IF
2803 :
2804 198 : DO ispin = 1, nspins
2805 198 : IF (scf_env%block_davidson_env(ispin)%use_sparse_mos) THEN
2806 64 : IF (.NOT. do_prec) THEN
2807 : CALL generate_extended_space_sparse(scf_env%block_davidson_env(ispin), mos(ispin), &
2808 8 : matrix_ks(ispin)%matrix, matrix_s(1)%matrix, output_unit)
2809 : ELSE
2810 : CALL generate_extended_space_sparse(scf_env%block_davidson_env(ispin), mos(ispin), &
2811 : matrix_ks(ispin)%matrix, matrix_s(1)%matrix, output_unit, &
2812 56 : scf_env%ot_preconditioner(ispin)%preconditioner)
2813 : END IF
2814 :
2815 : ELSE
2816 40 : IF (.NOT. do_prec) THEN
2817 : CALL generate_extended_space(scf_env%block_davidson_env(ispin), mos(ispin), &
2818 4 : matrix_ks(ispin)%matrix, matrix_s(1)%matrix, output_unit)
2819 : ELSE
2820 : CALL generate_extended_space(scf_env%block_davidson_env(ispin), mos(ispin), &
2821 : matrix_ks(ispin)%matrix, matrix_s(1)%matrix, output_unit, &
2822 36 : scf_env%ot_preconditioner(ispin)%preconditioner)
2823 : END IF
2824 : END IF
2825 : END DO !ispin
2826 :
2827 94 : IF (.NOT. scf_control%gce%do_gce) THEN
2828 : CALL set_mo_occupation(mo_array=mos, &
2829 94 : smear=scf_control%smear)
2830 : ELSE
2831 : CALL set_mo_occupation(mo_array=mos, &
2832 : smear=scf_control%smear, &
2833 0 : gce=scf_control%gce)
2834 : END IF
2835 :
2836 198 : DO ispin = 1, nspins
2837 : ! does not yet handle k-points
2838 : CALL calculate_density_matrix(mos(ispin), &
2839 198 : scf_env%p_mix_new(ispin, 1)%matrix)
2840 : END DO
2841 :
2842 94 : IF (output_unit > 0) THEN
2843 0 : WRITE (output_unit, "(T15,A/)") '<<<<<<<<< END DAVIDSON ITERATION <<<<<<<<<<'
2844 : END IF
2845 :
2846 : CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
2847 94 : "PRINT%DAVIDSON")
2848 :
2849 94 : CALL timestop(handle)
2850 :
2851 94 : END SUBROUTINE do_block_davidson_diag
2852 :
2853 : ! **************************************************************************************************
2854 : !> \brief block-Davidson SCF step for complex K points, one channel per
2855 : !> (local kpoint, spin); mirrors do_block_davidson_diag for the
2856 : !> preconditioner scheduling and do_general_diag_kp for the H(k)/S(k)
2857 : !> assembly and the density exit chain
2858 : !> \param qs_env ...
2859 : !> \param scf_env ...
2860 : !> \param matrix_ks_kp real-space Kohn-Sham matrices, dbcsr image set
2861 : !> \param matrix_s_kp real-space overlap matrix, dbcsr image set
2862 : !> \param kpoints ...
2863 : !> \param scf_control ...
2864 : !> \param added_mos_auto_grow ...
2865 : ! **************************************************************************************************
2866 910 : SUBROUTINE do_block_davidson_diag_kp(qs_env, scf_env, matrix_ks_kp, matrix_s_kp, kpoints, &
2867 : scf_control, added_mos_auto_grow)
2868 :
2869 : TYPE(qs_environment_type), POINTER :: qs_env
2870 : TYPE(qs_scf_env_type), POINTER :: scf_env
2871 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp
2872 : TYPE(kpoint_type), POINTER :: kpoints
2873 : TYPE(scf_control_type), POINTER :: scf_control
2874 : LOGICAL, INTENT(OUT), OPTIONAL :: added_mos_auto_grow
2875 :
2876 : CHARACTER(LEN=*), PARAMETER :: routineN = 'do_block_davidson_diag_kp'
2877 : REAL(KIND=dp), PARAMETER :: empty_factor = 5.0_dp, &
2878 : eps_iter_cap = 1.0E-4_dp
2879 :
2880 : INTEGER :: channel, first_prec, handle, ispin, iw, &
2881 : kplocal, kpmax, local_kpoint, &
2882 : nchannels, nspin, output_unit
2883 : INTEGER, DIMENSION(2) :: kp_range
2884 910 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
2885 910 : INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2886 : LOGICAL :: do_prec, my_grow, use_real_wfn
2887 : REAL(KIND=dp) :: eps_iter, eps_iter_empty
2888 910 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
2889 : TYPE(cp_cfm_type) :: cksmat, csmat, csmat_base
2890 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
2891 910 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
2892 : TYPE(cp_logger_type), POINTER :: logger
2893 : TYPE(kpoint_env_type), POINTER :: kp
2894 910 : TYPE(kpoint_operator_context_type) :: op_ctx
2895 : TYPE(mp_para_env_type), POINTER :: para_env
2896 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2897 910 : POINTER :: sab_nl
2898 : TYPE(section_vals_type), POINTER :: scf_section
2899 :
2900 : ! bounds of the adaptive inner tolerance. The loose phase is capped for
2901 : ! the residual-norm semantics of EPS_ITER. A residual of order 1/k would
2902 : ! leave an eigenvector error of order 10 percent at a typical metallic
2903 : ! gap. Unoccupied MOS converge empty_factor times looser. The tracking
2904 : ! divides the mixing delta by noise_scale: the SCF convergence measure
2905 : ! is the LARGEST ELEMENT of the density residual, in which the
2906 : ! per-column residuals of a whole block accumulate. The measured
2907 : ! amplification is ~20 on a smeared Bi2S3/MoS2 slab, so tracking at
2908 : ! delta/100 keeps the injected noise below the outer convergence level.
2909 :
2910 1820 : logger => cp_get_default_logger()
2911 910 : CALL timeset(routineN, handle)
2912 :
2913 910 : scf_section => section_vals_get_subs_vals(qs_env%input, "DFT%SCF")
2914 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%DAVIDSON", &
2915 910 : extension=".scfLog")
2916 910 : IF (output_unit > 0) THEN
2917 0 : WRITE (output_unit, "(/T15,A)") '<<<<<<<<< DAVIDSON ITERATIONS <<<<<<<<<<'
2918 : END IF
2919 :
2920 : CALL get_kpoint_info(kpoints, kp_range=kp_range, kp_dist=kp_dist, xkp=xkp, &
2921 : use_real_wfn=use_real_wfn, sab_nl=sab_nl, &
2922 910 : cell_to_index=cell_to_index)
2923 910 : IF (use_real_wfn) THEN
2924 0 : CPABORT('Davidson Kp requires complex K-point MOS')
2925 : END IF
2926 910 : para_env => kpoints%blacs_env_all%para_env
2927 910 : kplocal = kp_range(2) - kp_range(1) + 1
2928 2392 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
2929 910 : nspin = SIZE(matrix_ks_kp, 1)
2930 910 : nchannels = kplocal*nspin
2931 :
2932 910 : IF (scf_env%mixing_method == 1) THEN
2933 684 : scf_env%iter_param = scf_env%p_mix_alpha
2934 684 : scf_env%iter_method = "P_Mix/Dav."
2935 226 : ELSE IF (scf_env%mixing_method > 1) THEN
2936 226 : scf_env%iter_param = scf_env%mixing_store%alpha
2937 226 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Dav."
2938 : END IF
2939 :
2940 : ! preconditioner scheduling, mirrors do_block_davidson_diag. The first
2941 : ! K-point SCF step cold-starts unless extrapolation prefilled the MOS
2942 : ! (mos_prefilled). The FULL_ALL maker consumes the MO coefficients and
2943 : ! eigenvalues, so building before the first solve completed would feed it
2944 : ! uninitialized data and stall the Davidson convergence. Keep first_prec
2945 : ! clamped past that step in both cases. Warming it up under prefilled MOS
2946 : ! would mix two variables at once.
2947 910 : first_prec = MAX(scf_env%block_davidson_env(1)%first_prec, 2)
2948 910 : do_prec = .FALSE.
2949 910 : IF (scf_env%block_davidson_env(1)%prec_type /= ot_precond_none .AND. &
2950 : scf_env%iter_count >= first_prec) THEN
2951 810 : do_prec = .TRUE.
2952 : END IF
2953 : ! adaptive inner tolerance from EPS_ADAPT: loose while the density is
2954 : ! still far from self-consistency, tightening with the outer error
2955 : ! scaled by noise_scale (mirrors the block Krylov scheduling in
2956 : ! do_block_krylov_diag). The
2957 : ! ratchet is monotone across SCF steps: the threshold may tighten but
2958 : ! never relax again. A rebounding outer error would otherwise re-loosen
2959 : ! the solver, and the run could stop on a near-zero mixing delta of a
2960 : ! pair of identical, sloppily converged densities. The cap bounds the
2961 : ! early loose phase. Unoccupied MOS converge empty_factor times
2962 : ! looser (the occupancy decides which columns qualify, see
2963 : ! generate_extended_space_c). There is no restoration of the
2964 : ! user threshold at the end. The tolerance stays a margin below the
2965 : ! outer error. The MOS the run exits with satisfy that same margin
2966 : ! rather than the nominal EPS_ITER.
2967 910 : eps_iter = scf_env%block_davidson_env(1)%eps_iter
2968 910 : eps_iter_empty = eps_iter
2969 910 : IF (scf_control%diagonalization%eps_adapt > 0.0_dp .AND. scf_env%iter_delta > 0.0_dp) THEN
2970 : ! stall escape: when the mixing delta sank below the tolerance the
2971 : ! last solve used, that solve's own sloppiness may be the noise floor
2972 : ! pinning the delta. Tighten one decade below what that solve used.
2973 : ! Tightening relative to the OPERATING tolerance instead of the
2974 : ! dipped delta bounds the reaction to transient mixing dips (a
2975 : ! momentary overshoot must not collapse the threshold by orders of
2976 : ! magnitude and destabilise the mixer), while a genuine stall moves
2977 : ! the threshold down decade by decade. Checking on the stall alone
2978 : ! keeps the loose phase as loose as EPS_ADAPT allows
2979 80 : IF (scf_env%iter_delta < scf_env%block_davidson_env(1)%eps_iter_used) THEN
2980 : scf_env%block_davidson_env(1)%eps_iter_prev = &
2981 : MIN(scf_env%block_davidson_env(1)%eps_iter_prev, &
2982 : MAX(scf_env%block_davidson_env(1)%eps_iter, &
2983 2 : 0.1_dp*scf_env%block_davidson_env(1)%eps_iter_used))
2984 : END IF
2985 : ! candidate: looser than the user value, capped; ratchet: only
2986 : ! tighter than the previous adaptive value, which lives in the
2987 : ! persistent channel settings (a local variable would reset on
2988 : ! every call into this driver)
2989 : eps_iter = MIN(MAX(eps_iter, MIN(scf_control%diagonalization%eps_adapt*scf_env%iter_delta/ &
2990 : scf_env%block_davidson_env(1)%noise_scale, &
2991 80 : eps_iter_cap)), scf_env%block_davidson_env(1)%eps_iter_prev)
2992 80 : eps_iter_empty = empty_factor*eps_iter
2993 : ! record what this solve uses, after every override above: the outer
2994 : ! loop compares the mixing delta against it (qs_scf_check_inner_exit)
2995 80 : scf_env%block_davidson_env(1)%eps_iter_prev = eps_iter
2996 80 : scf_env%block_davidson_env(1)%eps_iter_used = eps_iter
2997 : END IF
2998 : ! schedule trace of the adaptive tolerance, one line per SCF step at
2999 : ! DEBUG print level
3000 910 : IF (scf_control%diagonalization%eps_adapt > 0.0_dp .AND. para_env%is_source() .AND. &
3001 : logger%iter_info%print_level >= debug_print_level) THEN
3002 0 : iw = cp_logger_get_default_unit_nr()
3003 0 : IF (iw > 0) THEN
3004 : WRITE (UNIT=iw, FMT="(T3,'EPS_ADAPT step',I5,' delta=',ES9.2,' eps_occ=',ES9.2,' eps_empty=',ES9.2)") &
3005 0 : scf_env%iter_count, scf_env%iter_delta, eps_iter, eps_iter_empty
3006 : END IF
3007 : END IF
3008 :
3009 : ! work matrices: global scratch fms (also the operator-context
3010 : ! template) and the complex H(k)/S(k)
3011 : CALL kp_diag_setup(qs_env, kpoints, nspin, matrix_ks_kp, matrix_s_kp, fmwork, op_ctx, &
3012 910 : matrix_struct)
3013 910 : CALL cp_cfm_create(cksmat, matrix_struct, name="dav_hk")
3014 910 : CALL cp_cfm_create(csmat, matrix_struct, name="dav_sk")
3015 910 : CALL cp_cfm_create(csmat_base, matrix_struct, name="dav_sk_base")
3016 :
3017 910 : IF (do_prec .AND. (scf_env%iter_count == first_prec .OR. &
3018 : MODULO(scf_env%iter_count, &
3019 : scf_env%block_davidson_env(1)%niter_new_prec) == 0)) THEN
3020 : CALL davidson_kp_prepare_preconditioners( &
3021 58 : qs_env, scf_env, matrix_ks_kp, matrix_s_kp, kpoints, op_ctx)
3022 : END IF
3023 :
3024 2798 : DO local_kpoint = 1, kpmax
3025 : ! Every k-point group executes the same sequence of global transforms.
3026 : ! Groups without a k point in the last round still participate
3027 : ! in the service calls, but skip their local solver work.
3028 : CALL kpoint_operator_get(op_ctx, local_kpoint, kpoint_spin_free, matrix_s_kp, &
3029 1888 : cmat=csmat_base, matrix_row=kpoint_spin_free, slot=kpoint_slot_s)
3030 :
3031 4762 : DO ispin = 1, nspin
3032 : CALL kpoint_operator_get(op_ctx, local_kpoint, ispin, matrix_ks_kp, &
3033 1964 : cmat=cksmat, matrix_row=ispin, slot=kpoint_slot_ks)
3034 1964 : IF (local_kpoint > kplocal) CYCLE
3035 1964 : kp => kpoints%kp_env(local_kpoint)%kpoint_env
3036 1964 : CALL cp_cfm_to_cfm(csmat_base, csmat)
3037 :
3038 3852 : IF (scf_env%iter_count == 1 .AND. .NOT. kp%mos_prefilled) THEN
3039 : ! K-point MOS enter the first SCF step allocated but uninitialized.
3040 : ! The stored S(k) copy is restored for every spin because
3041 : ! cp_cfm_geeig overwrites its matrix arguments.
3042 74 : CALL cold_start_mo_init(kp, ispin, cksmat, csmat)
3043 : ELSE
3044 1890 : channel = (local_kpoint - 1)*nspin + ispin
3045 1890 : IF (do_prec) THEN
3046 : CALL generate_extended_space_c(scf_env%block_davidson_env(channel), &
3047 : kp%mos(ispin), cksmat, csmat, output_unit, &
3048 : eps_iter, eps_iter_empty, &
3049 1762 : scf_env%ot_preconditioner(channel)%preconditioner)
3050 : ELSE
3051 : CALL generate_extended_space_c(scf_env%block_davidson_env(channel), &
3052 : kp%mos(ispin), cksmat, csmat, output_unit, &
3053 128 : eps_iter, eps_iter_empty)
3054 : END IF
3055 : END IF
3056 : END DO
3057 : END DO
3058 :
3059 : ! occupations and density: the shared exit chain, same as every
3060 : ! other k-point diagonalization driver
3061 910 : CALL kp_diag_exit_chain(kpoints, scf_env, scf_control, matrix_s_kp, sab_nl, fmwork, my_grow)
3062 910 : IF (my_grow .AND. PRESENT(added_mos_auto_grow)) added_mos_auto_grow = .TRUE.
3063 :
3064 910 : CALL cp_cfm_release(cksmat)
3065 910 : CALL cp_cfm_release(csmat)
3066 910 : CALL cp_cfm_release(csmat_base)
3067 910 : CALL kp_diag_finalize(qs_env, fmwork, op_ctx)
3068 :
3069 910 : IF (output_unit > 0) THEN
3070 0 : WRITE (output_unit, "(T15,A/)") '<<<<<<<<< END DAVIDSON ITERATION <<<<<<<<<<'
3071 : END IF
3072 : CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
3073 910 : "PRINT%DAVIDSON")
3074 :
3075 910 : CALL timestop(handle)
3076 :
3077 1820 : END SUBROUTINE do_block_davidson_diag_kp
3078 :
3079 : ! **************************************************************************************************
3080 : !> \brief Block Krylov (Lanczos) refinement of the k-point MOS, the complex
3081 : !> counterpart of do_block_krylov_diag: every channel (local kpoint,
3082 : !> spin) is standardized with the Cholesky factor of S(k), refined by
3083 : !> the complex block-Krylov kernel, and rotated back into the
3084 : !> generalized problem
3085 : !> \param qs_env ...
3086 : !> \param scf_env ...
3087 : !> \param matrix_ks_kp real-space Kohn-Sham matrix, dbcsr image set
3088 : !> \param matrix_s_kp real-space overlap matrix, dbcsr image set
3089 : !> \param kpoints ...
3090 : !> \param scf_control ...
3091 : !> \param check_moconv_only only report the convergence statistics
3092 : !> \param added_mos_auto_grow signals the outer loop that the virtual-space
3093 : !> buffer must grow before this SCF step is redone
3094 : ! **************************************************************************************************
3095 586 : SUBROUTINE do_block_krylov_diag_kp(qs_env, scf_env, matrix_ks_kp, matrix_s_kp, kpoints, &
3096 : scf_control, check_moconv_only, added_mos_auto_grow)
3097 :
3098 : TYPE(qs_environment_type), POINTER :: qs_env
3099 : TYPE(qs_scf_env_type), POINTER :: scf_env
3100 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp
3101 : TYPE(kpoint_type), POINTER :: kpoints
3102 : TYPE(scf_control_type), POINTER :: scf_control
3103 : LOGICAL, INTENT(IN), OPTIONAL :: check_moconv_only
3104 : LOGICAL, INTENT(OUT), OPTIONAL :: added_mos_auto_grow
3105 :
3106 : CHARACTER(LEN=*), PARAMETER :: routineN = 'do_block_krylov_diag_kp'
3107 : REAL(KIND=dp), PARAMETER :: empty_factor = 5.0_dp, &
3108 : eps_iter_cap = 1.0E-4_dp, &
3109 : noise_scale = 100.0_dp
3110 :
3111 : INTEGER :: handle, ik, ikp, ispin, kplocal, kpmax, &
3112 : local_kpoint, max_iter, nspin, &
3113 : output_unit
3114 : INTEGER, DIMENSION(2) :: kp_range
3115 586 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
3116 : LOGICAL :: my_check_moconv_only, my_grow, &
3117 : use_local_kpoints, use_real_wfn
3118 : REAL(KIND=dp) :: eps_iter, eps_iter_empty
3119 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct, mo_struct
3120 586 : TYPE(cp_fm_type), DIMENSION(:), POINTER :: fmwork
3121 : TYPE(cp_logger_type), POINTER :: logger
3122 586 : TYPE(kp_diag_workspace_type), ALLOCATABLE, TARGET :: workspace(:)
3123 : TYPE(kpoint_env_type), POINTER :: kp
3124 586 : TYPE(kpoint_operator_context_type) :: op_ctx
3125 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3126 586 : POINTER :: sab_nl
3127 : TYPE(section_vals_type), POINTER :: scf_section
3128 :
3129 : ! empty_factor and noise_scale mirror the defaults of the
3130 : ! Davidson k-point driver. eps_iter_cap bounds the adaptive tolerance
3131 :
3132 1172 : logger => cp_get_default_logger()
3133 586 : CALL timeset(routineN, handle)
3134 :
3135 586 : my_check_moconv_only = .FALSE.
3136 586 : IF (PRESENT(check_moconv_only)) my_check_moconv_only = check_moconv_only
3137 :
3138 586 : scf_section => section_vals_get_subs_vals(qs_env%input, "DFT%SCF")
3139 : output_unit = cp_print_key_unit_nr(logger, scf_section, "PRINT%LANCZOS", &
3140 586 : extension=".scfLog")
3141 :
3142 : CALL get_kpoint_info(kpoints, kp_range=kp_range, kp_dist=kp_dist, &
3143 586 : use_real_wfn=use_real_wfn, sab_nl=sab_nl)
3144 586 : IF (use_real_wfn) THEN
3145 0 : CPABORT('k-point Lanczos refinement requires complex k-point MOS')
3146 : END IF
3147 586 : kplocal = kp_range(2) - kp_range(1) + 1
3148 : ! the largest local k-point count across the groups: kp_dist can be
3149 : ! uneven, and every group must join the service calls for every round
3150 1634 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
3151 586 : nspin = SIZE(matrix_ks_kp, 1)
3152 :
3153 586 : IF (scf_env%mixing_method == 1) THEN
3154 264 : scf_env%iter_param = scf_env%p_mix_alpha
3155 264 : scf_env%iter_method = "P_Mix/Lanczos"
3156 : ELSE
3157 322 : scf_env%iter_param = scf_env%mixing_store%alpha
3158 322 : scf_env%iter_method = TRIM(scf_env%mixing_store%iter_method)//"/Lanc."
3159 : END IF
3160 :
3161 : ! work matrices: the driver workspace below holds the complex
3162 : ! H(k)/S(k) of the channels
3163 :
3164 : ! adaptive inner tolerance, mirrors do_block_krylov_diag; it carries
3165 : ! no ratchet state, the threshold follows the sinking mixing delta
3166 : eps_iter = kp_adaptive_eps_iter(scf_env%krylov_settings%eps_conv, scf_env%krylov_settings%eps_adapt, &
3167 586 : scf_env%iter_delta, noise_scale, eps_iter_cap)
3168 : ! unoccupied MOS relax like the Davidson k-point driver: the
3169 : ! occupation decides which columns use the relaxed empty threshold,
3170 : ! not the HOMO index (see generate_extended_space_c)
3171 586 : eps_iter_empty = eps_iter
3172 586 : IF (scf_env%krylov_settings%eps_adapt > 0.0_dp) eps_iter_empty = empty_factor*eps_iter
3173 586 : max_iter = scf_env%krylov_settings%max_iter
3174 :
3175 : ! one-rank k-point groups refine their k points locally, without a
3176 : ! transfer round; a diag check, a convergence-only probe and an
3177 : ! explicitly selected diagonalization library keep the rounds
3178 586 : use_local_kpoints = kpoints%para_env_kp%num_pe == 1
3179 586 : IF (diag_check_requested()) use_local_kpoints = .FALSE.
3180 586 : IF (my_check_moconv_only) use_local_kpoints = .FALSE.
3181 586 : IF (diag_lib_explicit) use_local_kpoints = .FALSE.
3182 : CALL kp_diag_setup(qs_env, kpoints, nspin, matrix_ks_kp, matrix_s_kp, fmwork, op_ctx, &
3183 586 : matrix_struct)
3184 :
3185 586 : kp => kpoints%kp_env(1)%kpoint_env
3186 586 : mo_struct => kp%mos(1)%cmo_coeff%matrix_struct
3187 : CALL kp_diag_workspaces_create(workspace, 1, matrix_struct, mo_struct, &
3188 : use_local_kpoints, scf_env%cholesky_method == cholesky_off, &
3189 : lanczos=.TRUE., nkrylov=scf_env%krylov_settings%nkrylov, &
3190 586 : nblock=scf_env%krylov_settings%nblock)
3191 :
3192 586 : IF (output_unit > 0 .AND. .NOT. my_check_moconv_only) THEN
3193 28 : WRITE (output_unit, "(/T15,A)") '<<<<<<<<< LANCZOS REFINEMENT <<<<<<<<<<'
3194 : WRITE (output_unit, "(T8,A,T15,A,T23,A,T36,A,T49,A,T60,A,/,T8,A)") &
3195 28 : " Spin ", " Cycle ", &
3196 56 : " conv. MOS ", " B2MAX ", " B2MIN ", " Time", REPEAT("-", 60)
3197 : END IF
3198 :
3199 586 : IF (use_local_kpoints) THEN
3200 : ! serial pass over the locally owned k points. The first SCF step
3201 : ! cold-starts every unprefilled channel in place, like the rounds
3202 : ! path below. Later steps refine
3203 1059 : DO ikp = 1, kplocal
3204 603 : kp => kpoints%kp_env(ikp)%kpoint_env
3205 603 : ik = kp_range(1) + ikp - 1
3206 : CALL kpoint_operator_evaluate(op_ctx, 1, kpoint_slot_s, ik, &
3207 603 : workspace(1)%csmat_base)
3208 1662 : DO ispin = 1, nspin
3209 : CALL kpoint_operator_evaluate(op_ctx, ispin, kpoint_slot_ks, ik, &
3210 603 : workspace(1)%cksmat)
3211 603 : IF (scf_env%iter_count == 1 .AND. .NOT. kp%mos_prefilled) THEN
3212 : ! the K-point MOS enter the first SCF step allocated but
3213 : ! uninitialized
3214 25 : CALL cold_start_mo_init(kp, ispin, workspace(1)%cksmat, workspace(1)%csmat_base)
3215 25 : CYCLE
3216 : END IF
3217 : CALL lanczos_refine_channel(kp, ispin, scf_env, my_check_moconv_only, eps_iter, &
3218 1181 : eps_iter_empty, max_iter, workspace(1), output_unit, .TRUE.)
3219 : END DO
3220 : END DO
3221 : ELSE
3222 :
3223 318 : DO local_kpoint = 1, kpmax
3224 : ! Every k-point group executes the same sequence of global transforms
3225 : ! up to the largest local k-point count (kp_dist can be uneven after
3226 : ! #5993). Only the group owning this local_kpoint receives the
3227 : ! redistributed matrix. The others participate through the dummy
3228 : ! destination. Groups without a k point in the last rounds still
3229 : ! join the service calls and skip their local solver work.
3230 : CALL kpoint_operator_get(op_ctx, local_kpoint, kpoint_spin_free, matrix_s_kp, &
3231 188 : cmat=workspace(1)%csmat_base, matrix_row=kpoint_spin_free, slot=kpoint_slot_s)
3232 :
3233 518 : DO ispin = 1, nspin
3234 : CALL kpoint_operator_get(op_ctx, local_kpoint, ispin, matrix_ks_kp, &
3235 200 : cmat=workspace(1)%cksmat, matrix_row=ispin, slot=kpoint_slot_ks)
3236 200 : IF (local_kpoint > kplocal) CYCLE
3237 200 : kp => kpoints%kp_env(local_kpoint)%kpoint_env
3238 :
3239 200 : IF (scf_env%iter_count == 1 .AND. .NOT. kp%mos_prefilled) THEN
3240 : ! first SCF step cold-starts by a direct diagonalization,
3241 : ! same entry as the Davidson k-point path
3242 14 : CALL cold_start_mo_init(kp, ispin, workspace(1)%cksmat, workspace(1)%csmat_base)
3243 14 : CYCLE
3244 : END IF
3245 :
3246 : CALL lanczos_refine_channel(kp, ispin, scf_env, my_check_moconv_only, eps_iter, &
3247 374 : eps_iter_empty, max_iter, workspace(1), output_unit, .TRUE.)
3248 : END DO
3249 : END DO
3250 : END IF
3251 :
3252 : ! occupations and density: the shared exit chain. With
3253 : ! check_moconv_only the caller falls back to the standard k-point
3254 : ! diagonalization. That path supplies MOS and density, and the
3255 : ! statistics above are printed for information only
3256 586 : IF (.NOT. my_check_moconv_only) THEN
3257 586 : CALL kp_diag_exit_chain(kpoints, scf_env, scf_control, matrix_s_kp, sab_nl, fmwork, my_grow)
3258 586 : IF (my_grow .AND. PRESENT(added_mos_auto_grow)) added_mos_auto_grow = .TRUE.
3259 : END IF
3260 :
3261 586 : CALL kp_diag_workspaces_release(workspace)
3262 586 : CALL kp_diag_finalize(qs_env, fmwork, op_ctx)
3263 :
3264 586 : IF (output_unit > 0 .AND. .NOT. my_check_moconv_only) THEN
3265 28 : WRITE (output_unit, "(T15,A/)") '<<<<<<<<< END LANCZOS REFINEMENT <<<<<<<<<<'
3266 : END IF
3267 : CALL cp_print_key_finished_output(output_unit, logger, scf_section, &
3268 586 : "PRINT%LANCZOS")
3269 :
3270 586 : CALL timestop(handle)
3271 :
3272 1172 : END SUBROUTINE do_block_krylov_diag_kp
3273 :
3274 : ! **************************************************************************************************
3275 : !> \brief Refine the MOS of one (k point, spin) channel. The routine
3276 : !> projects H(k) onto its Hermitian part, standardizes the channel
3277 : !> by the Cholesky factor of S(k), sweeps the single-pass kernel,
3278 : !> and writes the MOS back to the MO sets. The caller owns every
3279 : !> matrix. The routine allocates no CP2K matrix.
3280 : !> \param kp k-point environment holding the MOS pair of the channel
3281 : !> \param ispin spin component
3282 : !> \param scf_env SCF environment holding the Krylov settings
3283 : !> \param check_moconv_only only run the entry Rayleigh-Ritz of the kernel
3284 : !> and report the convergence count
3285 : !> \param eps_iter convergence threshold of the occupied columns
3286 : !> \param eps_iter_empty relaxed threshold of the unoccupied columns
3287 : !> \param max_iter sweeps allowed per channel
3288 : !> \param work driver workspace of the channel. cksmat holds the
3289 : !> complex H(k), standardized in place. csmat_base holds the
3290 : !> untouched S(k). csmat holds its work copy and the Cholesky
3291 : !> factor after the standardization. cwork holds the
3292 : !> Hermitian-projection scratch. cmos holds the MOS in the
3293 : !> standardized basis. lan_ws holds the kernel scratch
3294 : !> \param output_unit unit of the refinement table. A nonpositive unit
3295 : !> disables every report
3296 : !> \param print_row report this channel in the refinement table and in
3297 : !> the not-converged warnings. The local-kpoint path passes
3298 : !> .FALSE. and reports nothing, like the local phase of the
3299 : !> general k-point diagonalization
3300 : ! **************************************************************************************************
3301 764 : SUBROUTINE lanczos_refine_channel(kp, ispin, scf_env, check_moconv_only, eps_iter, eps_iter_empty, &
3302 : max_iter, work, output_unit, print_row)
3303 : TYPE(kpoint_env_type), POINTER :: kp
3304 : INTEGER, INTENT(IN) :: ispin
3305 : TYPE(qs_scf_env_type), POINTER :: scf_env
3306 : LOGICAL, INTENT(IN) :: check_moconv_only
3307 : REAL(KIND=dp), INTENT(IN) :: eps_iter, eps_iter_empty
3308 : INTEGER, INTENT(IN) :: max_iter
3309 : TYPE(kp_diag_workspace_type), INTENT(INOUT) :: work
3310 : INTEGER, INTENT(IN) :: output_unit
3311 : LOGICAL, INTENT(IN) :: print_row
3312 :
3313 : CHARACTER(LEN=*), PARAMETER :: routineN = 'lanczos_refine_channel'
3314 :
3315 : INTEGER :: handle, iter, nmo, nmo_conv, nmo_nc
3316 : LOGICAL :: any_refined, converged, need_suffix_rr, &
3317 : next_need_suffix_rr, step_refined
3318 : REAL(KIND=dp) :: max_res, min_res, t1, t2
3319 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eps_col
3320 764 : REAL(KIND=dp), DIMENSION(:), POINTER :: mo_eigenvalues, occupation
3321 : TYPE(cp_cfm_type), POINTER :: cmo_coeff
3322 :
3323 764 : CALL timeset(routineN, handle)
3324 :
3325 : CALL get_mo_set(mo_set=kp%mos(ispin), nmo=nmo, &
3326 : eigenvalues=mo_eigenvalues, occupation_numbers=occupation, &
3327 764 : cmo_coeff=cmo_coeff)
3328 :
3329 764 : CALL cp_cfm_to_cfm(work%csmat_base, work%csmat)
3330 :
3331 : ! H(k) is Hermitian only to grid accuracy and the kernel forms its
3332 : ! residuals with the full matrix: project onto the Hermitian part
3333 : ! once per channel
3334 764 : CALL cp_cfm_transpose(work%cksmat, "C", work%cwork)
3335 : CALL cp_cfm_scale_and_add(CMPLX(0.5_dp, 0.0_dp, KIND=dp), work%cksmat, &
3336 764 : CMPLX(0.5_dp, 0.0_dp, KIND=dp), work%cwork)
3337 :
3338 : ! complex MOS of the channel
3339 764 : CALL cp_cfm_to_cfm(cmo_coeff, work%cmos)
3340 :
3341 : ! standardization S(k) = U^dag U: operator and MOS are pulled
3342 : ! through the same transform, X = U C and A = U^-dag H U^-1, so
3343 : ! the kernel faces a standard eigenproblem. CHOLESKY OFF is not
3344 : ! supported on this path
3345 764 : CALL cp_cfm_cholesky_decompose(work%csmat)
3346 764 : CALL cp_cfm_triangular_multiply(work%csmat, work%cksmat, side="R", invert_tr=.TRUE.)
3347 764 : CALL cp_cfm_triangular_multiply(work%csmat, work%cksmat, side="L", transa_tr="C", invert_tr=.TRUE.)
3348 764 : CALL cp_cfm_triangular_multiply(work%csmat, work%cmos, side="L", invert_tr=.FALSE.)
3349 :
3350 : ! per-column thresholds: the occupation decides which columns
3351 : ! qualify for the relaxed empty threshold
3352 2292 : ALLOCATE (eps_col(nmo))
3353 764 : CALL kp_eps_col_by_occupation(occupation, eps_iter, eps_iter_empty, eps_col)
3354 :
3355 764 : converged = .FALSE.
3356 764 : any_refined = .FALSE.
3357 764 : need_suffix_rr = .FALSE.
3358 :
3359 764 : t1 = m_walltime()
3360 : CALL lanczos_channel_begin(work%cksmat, work%cmos, mo_eigenvalues, eps_col, &
3361 : work%lan_ws, max_res, min_res, nmo_conv, nmo_nc, &
3362 764 : check_moconv_only=check_moconv_only)
3363 764 : IF (check_moconv_only) THEN
3364 0 : DEALLOCATE (eps_col)
3365 0 : IF (print_row .AND. output_unit > 0) THEN
3366 : WRITE (output_unit, '(T8,I3,T16,I5,T24,I6,T33,E12.4,2x,E12.4)') &
3367 0 : ispin, 0, nmo_conv, max_res, min_res
3368 : END IF
3369 0 : CALL timestop(handle)
3370 : RETURN
3371 : END IF
3372 :
3373 764 : IF (nmo_nc == 0) THEN
3374 170 : t2 = m_walltime()
3375 170 : IF (print_row .AND. output_unit > 0 .AND. max_iter >= 1) THEN
3376 : WRITE (output_unit, '(T8,I3,T16,I5,T24,I6,T33,E12.4,2x,E12.4,T60,F8.3)') &
3377 1 : ispin, 1, nmo_conv, max_res, min_res, t2 - t1
3378 : END IF
3379 : converged = .TRUE.
3380 : ELSE
3381 1044 : DO iter = 1, max_iter
3382 839 : IF (iter > 1) t1 = m_walltime()
3383 : CALL lanczos_sweep_suffix(work%cksmat, work%cmos, mo_eigenvalues, eps_col, &
3384 : scf_env%krylov_settings%nkrylov, scf_env%krylov_settings%nblock, &
3385 : iter == 1, need_suffix_rr, work%lan_ws, &
3386 : max_res, min_res, nmo_conv, nmo_nc, step_refined, &
3387 839 : next_need_suffix_rr)
3388 839 : need_suffix_rr = next_need_suffix_rr
3389 839 : any_refined = any_refined .OR. step_refined
3390 839 : t2 = m_walltime()
3391 839 : IF (print_row .AND. output_unit > 0) THEN
3392 : WRITE (output_unit, '(T8,I3,T16,I5,T24,I6,T33,E12.4,2x,E12.4,T60,F8.3)') &
3393 48 : ispin, iter, nmo_conv, max_res, min_res, t2 - t1
3394 : END IF
3395 1044 : IF (nmo_nc == 0) THEN
3396 : converged = .TRUE.
3397 : EXIT
3398 : END IF
3399 : END DO
3400 : END IF
3401 764 : DEALLOCATE (eps_col)
3402 :
3403 764 : IF (any_refined) THEN
3404 594 : CALL lanczos_channel_end(work%cmos, work%lan_ws)
3405 : END IF
3406 764 : IF (.NOT. converged .AND. print_row .AND. output_unit > 0) THEN
3407 : WRITE (output_unit, "(T4, A)") " WARNING Lanczos refinement could "// &
3408 20 : "not converge all the mos:"
3409 20 : WRITE (output_unit, "(T40,A,T70,I10)") " number of not converged mos ", nmo_nc
3410 20 : WRITE (output_unit, "(T40,A,T70,E10.2)") " max norm of the residual ", max_res
3411 : END IF
3412 :
3413 : ! rotate the refined MOS back into the generalized problem, C = U^-1 X
3414 764 : CALL cp_cfm_triangular_multiply(work%csmat, work%cmos, side="L", invert_tr=.TRUE.)
3415 764 : CALL cp_cfm_to_cfm(work%cmos, cmo_coeff)
3416 :
3417 764 : CALL timestop(handle)
3418 1528 : END SUBROUTINE lanczos_refine_channel
3419 :
3420 : ! **************************************************************************************************
3421 : !> \brief first-SCF-step initialization of the K-point MOS of one spin by one
3422 : !> direct diagonalization of H(k); both arguments are assembled by the
3423 : !> caller because cp_cfm_geeig overwrites them
3424 : !> \param kp kpoint environment holding the complex MO set
3425 : !> \param ispin ...
3426 : !> \param cksmat complex H(k), assembled by the caller
3427 : !> \param csmat complex S(k), assembled by the caller
3428 : ! **************************************************************************************************
3429 452 : SUBROUTINE cold_start_mo_init(kp, ispin, cksmat, csmat)
3430 :
3431 : TYPE(kpoint_env_type), POINTER :: kp
3432 : INTEGER, INTENT(IN) :: ispin
3433 : TYPE(cp_cfm_type) :: cksmat, csmat
3434 :
3435 : CHARACTER(LEN=*), PARAMETER :: routineN = 'cold_start_mo_init'
3436 :
3437 : INTEGER :: handle
3438 113 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
3439 : TYPE(cp_cfm_type) :: chwork, cswork, cwork
3440 :
3441 113 : CALL timeset(routineN, handle)
3442 :
3443 113 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
3444 : ! cp_cfm_geeig overwrites its matrix arguments: solve on scratch
3445 : ! copies so the caller H(k)/S(k) stay valid
3446 113 : CALL cp_cfm_create(chwork, cksmat%matrix_struct, name="kp_cold_h")
3447 113 : CALL cp_cfm_create(cswork, csmat%matrix_struct, name="kp_cold_s")
3448 113 : CALL cp_cfm_to_cfm(cksmat, chwork)
3449 113 : CALL cp_cfm_to_cfm(csmat, cswork)
3450 113 : CALL cp_cfm_create(cwork, cksmat%matrix_struct, name="kp_cold_work")
3451 113 : CALL cp_cfm_geeig(chwork, cswork, kp%mos(ispin)%cmo_coeff, eigenvalues, cwork)
3452 113 : CALL cp_cfm_release(chwork)
3453 113 : CALL cp_cfm_release(cswork)
3454 113 : CALL cp_cfm_release(cwork)
3455 :
3456 113 : CALL timestop(handle)
3457 :
3458 113 : END SUBROUTINE cold_start_mo_init
3459 :
3460 : ! **************************************************************************************************
3461 : !> \brief builds one complex K-point preconditioner per (local kpoint, spin)
3462 : !> channel; mirrors prepare_qs_ot_kpoint_preconditioners
3463 : !> \param qs_env ...
3464 : !> \param scf_env ...
3465 : !> \param matrix_ks_kp ...
3466 : !> \param matrix_s_kp ...
3467 : !> \param kpoints ...
3468 : !> \param op_ctx ...
3469 : ! **************************************************************************************************
3470 58 : SUBROUTINE davidson_kp_prepare_preconditioners( &
3471 : qs_env, scf_env, matrix_ks_kp, matrix_s_kp, kpoints, op_ctx)
3472 :
3473 : TYPE(qs_environment_type), POINTER :: qs_env
3474 : TYPE(qs_scf_env_type), POINTER :: scf_env
3475 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp
3476 : TYPE(kpoint_type), POINTER :: kpoints
3477 : TYPE(kpoint_operator_context_type) :: op_ctx
3478 :
3479 : CHARACTER(LEN=*), PARAMETER :: routineN = 'davidson_kp_prepare_preconditioners'
3480 :
3481 : INTEGER :: channel, handle, ispin, kplocal, kpmax, &
3482 : local_kpoint, nchannels, nmo, nspin, &
3483 : prec_type, solver_type
3484 : INTEGER, DIMENSION(2) :: kp_range
3485 58 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
3486 58 : INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
3487 : REAL(KIND=dp) :: energy_gap
3488 58 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
3489 58 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
3490 : TYPE(cp_blacs_env_type), POINTER :: blacs_env
3491 : TYPE(cp_cfm_type) :: matrix_c, matrix_h, matrix_s
3492 58 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_t_kp
3493 : TYPE(dbcsr_type), POINTER :: matrix_h_im, matrix_h_re, matrix_s_im, &
3494 : matrix_s_re, matrix_t_im, matrix_t_re
3495 : TYPE(kpoint_env_type), POINTER :: kp
3496 : TYPE(mp_para_env_type), POINTER :: para_env
3497 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3498 58 : POINTER :: sab_nl
3499 :
3500 58 : CALL timeset(routineN, handle)
3501 :
3502 : CALL get_kpoint_info(kpoints, kp_range=kp_range, kp_dist=kp_dist, xkp=xkp, sab_nl=sab_nl, &
3503 58 : cell_to_index=cell_to_index)
3504 58 : CALL get_qs_env(qs_env, kinetic_kp=matrix_t_kp)
3505 : ! the preconditioner fms must live on the kpoint-group BLACS context,
3506 : ! the context of the MOS they are applied with
3507 58 : blacs_env => kpoints%blacs_env
3508 58 : para_env => kpoints%para_env_kp
3509 58 : kplocal = kp_range(2) - kp_range(1) + 1
3510 152 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
3511 58 : nspin = SIZE(matrix_ks_kp, 1)
3512 58 : nchannels = kplocal*nspin
3513 58 : prec_type = scf_env%block_davidson_env(1)%prec_type
3514 58 : solver_type = scf_env%block_davidson_env(1)%solver_type
3515 58 : energy_gap = scf_env%block_davidson_env(1)%energy_gap
3516 :
3517 : ! channel storage: every (kpoint, spin) channel carries its own
3518 : ! preconditioner, because S(k) and H(k) differ per kpoint. The Gamma
3519 : ! reuse pattern of restart_preconditioner (a single entry for the
3520 : ! spin-independent types) must not be used here.
3521 58 : IF (ASSOCIATED(scf_env%ot_preconditioner)) THEN
3522 120 : DO channel = 1, SIZE(scf_env%ot_preconditioner)
3523 120 : IF (ASSOCIATED(scf_env%ot_preconditioner(channel)%preconditioner)) THEN
3524 82 : CALL destroy_preconditioner(scf_env%ot_preconditioner(channel)%preconditioner)
3525 82 : DEALLOCATE (scf_env%ot_preconditioner(channel)%preconditioner)
3526 : END IF
3527 : END DO
3528 38 : DEALLOCATE (scf_env%ot_preconditioner)
3529 38 : NULLIFY (scf_env%ot_preconditioner)
3530 : END IF
3531 302 : ALLOCATE (scf_env%ot_preconditioner(nchannels))
3532 186 : DO channel = 1, nchannels
3533 640 : ALLOCATE (scf_env%ot_preconditioner(channel)%preconditioner)
3534 : CALL init_preconditioner(scf_env%ot_preconditioner(channel)%preconditioner, &
3535 186 : para_env, blacs_env)
3536 : END DO
3537 :
3538 58 : NULLIFY (eigenvalues, matrix_h_im, matrix_h_re, matrix_s_im, matrix_s_re, &
3539 58 : matrix_t_im, matrix_t_re)
3540 58 : IF (prec_type == ot_precond_full_kinetic) THEN
3541 28 : CPASSERT(ASSOCIATED(matrix_t_kp))
3542 28 : CALL kpoint_operator_prepare(op_ctx, matrix_t_kp, kpoint_slot_t, matrix_row=kpoint_spin_free)
3543 : END IF
3544 :
3545 182 : DO local_kpoint = 1, kpmax
3546 : ! S(k) is spin independent. Build it with the same global-group
3547 : ! redistribution protocol as the Davidson operator itself, directly
3548 : ! as the group-local block-cyclic DBCSR pair of the preconditioner
3549 : ! makers.
3550 124 : CALL dbcsr_init_p(matrix_s_re)
3551 124 : CALL dbcsr_init_p(matrix_s_im)
3552 : CALL kpoint_operator_get(op_ctx, local_kpoint, kpoint_spin_free, matrix_s_kp, &
3553 : mat_re=matrix_s_re, mat_im=matrix_s_im, &
3554 124 : matrix_row=kpoint_spin_free, slot=kpoint_slot_s)
3555 :
3556 124 : IF (prec_type == ot_precond_full_kinetic) THEN
3557 56 : CALL dbcsr_init_p(matrix_t_re)
3558 56 : CALL dbcsr_init_p(matrix_t_im)
3559 : CALL kpoint_operator_get(op_ctx, local_kpoint, kpoint_spin_free, matrix_t_kp, &
3560 : mat_re=matrix_t_re, mat_im=matrix_t_im, &
3561 56 : matrix_row=kpoint_spin_free, slot=kpoint_slot_t)
3562 : END IF
3563 :
3564 252 : DO ispin = 1, nspin
3565 128 : IF (prec_type == ot_precond_full_all .OR. prec_type == ot_precond_full_single_inverse) THEN
3566 56 : CALL dbcsr_init_p(matrix_h_re)
3567 56 : CALL dbcsr_init_p(matrix_h_im)
3568 : CALL kpoint_operator_get(op_ctx, local_kpoint, ispin, matrix_ks_kp, &
3569 56 : mat_re=matrix_h_re, mat_im=matrix_h_im, matrix_row=ispin, slot=kpoint_slot_ks)
3570 : END IF
3571 128 : IF (local_kpoint <= kplocal) THEN
3572 128 : kp => kpoints%kp_env(local_kpoint)%kpoint_env
3573 128 : channel = (local_kpoint - 1)*nspin + ispin
3574 128 : IF (prec_type == ot_precond_full_all .OR. prec_type == ot_precond_full_single_inverse) THEN
3575 : ! Davidson iterates the full MO set, so its active space is all nmo
3576 : ! MOs of the channel: assemble the complex channel matrices on the
3577 : ! kpoint-group context and call the complex H-based cores directly
3578 : ! (mirrors the complex preconditioners of the K-point OT case; the
3579 : ! FULL_SINGLE_INVERSE variant trades the state selectivity of
3580 : ! FULL_ALL for one shifted factorization and a gemm application)
3581 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues, &
3582 56 : nmo=nmo)
3583 56 : CALL cp_cfm_create(matrix_c, kp%mos(ispin)%cmo_coeff%matrix_struct, name="dav pc C")
3584 56 : CALL cp_cfm_to_cfm(kp%mos(ispin)%cmo_coeff, matrix_c)
3585 : CALL dbcsr_pair_to_cfm(matrix_h_re, matrix_h_im, &
3586 : scf_env%ot_preconditioner(channel)%preconditioner, &
3587 56 : 'dav pc H', matrix_h)
3588 : CALL dbcsr_pair_to_cfm(matrix_s_re, matrix_s_im, &
3589 : scf_env%ot_preconditioner(channel)%preconditioner, &
3590 56 : 'dav pc S', matrix_s)
3591 56 : IF (prec_type == ot_precond_full_all) THEN
3592 : CALL make_complex_full_all(scf_env%ot_preconditioner(channel)%preconditioner, &
3593 : matrix_c, matrix_h, matrix_s, eigenvalues(1:nmo), &
3594 40 : energy_gap)
3595 : ELSE
3596 : CALL make_complex_full_single_inverse( &
3597 : scf_env%ot_preconditioner(channel)%preconditioner, &
3598 16 : matrix_c, matrix_h, matrix_s, energy_gap)
3599 : END IF
3600 56 : CALL cp_cfm_release(matrix_c)
3601 56 : CALL cp_cfm_release(matrix_h)
3602 56 : CALL cp_cfm_release(matrix_s)
3603 72 : ELSE IF (prec_type == ot_precond_full_kinetic) THEN
3604 : CALL make_preconditioner_complex_full_kinetic( &
3605 : scf_env%ot_preconditioner(channel)%preconditioner, &
3606 56 : matrix_t_re, matrix_t_im, matrix_s_re, matrix_s_im, energy_gap, solver_type)
3607 16 : ELSE IF (prec_type == ot_precond_s_inverse) THEN
3608 : CALL make_preconditioner_complex_full_s_inverse( &
3609 : scf_env%ot_preconditioner(channel)%preconditioner, &
3610 16 : matrix_s_re, matrix_s_im, solver_type)
3611 : END IF
3612 : END IF
3613 252 : IF (prec_type == ot_precond_full_all .OR. prec_type == ot_precond_full_single_inverse) THEN
3614 56 : CALL dbcsr_release_p(matrix_h_re)
3615 56 : CALL dbcsr_release_p(matrix_h_im)
3616 : END IF
3617 : END DO
3618 :
3619 124 : CALL dbcsr_release_p(matrix_s_re)
3620 124 : CALL dbcsr_release_p(matrix_s_im)
3621 182 : IF (prec_type == ot_precond_full_kinetic) THEN
3622 56 : CALL dbcsr_release_p(matrix_t_re)
3623 56 : CALL dbcsr_release_p(matrix_t_im)
3624 : END IF
3625 : END DO
3626 :
3627 58 : CALL timestop(handle)
3628 :
3629 58 : END SUBROUTINE davidson_kp_prepare_preconditioners
3630 :
3631 : ! **************************************************************************************************
3632 : !> \brief Kpoint diagonalization routine
3633 : !> Transforms matrices to kpoint, distributes kpoint groups, performs diagonalization
3634 : !> \param matrix_s Overlap matrices (RS indices, global)
3635 : !> \param kpoints Kpoint environment
3636 : !> \param fmwork full matrices distributed over all groups
3637 : !> \param lowdin_forces retain the overlap spectrum for Lowdin force/stress derivatives
3638 : !> \par History
3639 : !> 02.2026 created [JGH]
3640 : ! **************************************************************************************************
3641 140 : SUBROUTINE diag_kp_smat(matrix_s, kpoints, fmwork, lowdin_forces)
3642 :
3643 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
3644 : TYPE(kpoint_type), POINTER :: kpoints
3645 : TYPE(cp_fm_type), DIMENSION(:) :: fmwork
3646 : LOGICAL, INTENT(IN), OPTIONAL :: lowdin_forces
3647 :
3648 : CHARACTER(len=*), PARAMETER :: routineN = 'diag_kp_smat'
3649 : COMPLEX(KIND=dp), PARAMETER :: cone = (1.0_dp, 0.0_dp), &
3650 : czero = (0.0_dp, 0.0_dp)
3651 :
3652 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:) :: ceig
3653 : INTEGER :: handle, ikp, kplocal, kpmax, nao
3654 : INTEGER, DIMENSION(2) :: kp_range
3655 140 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
3656 : LOGICAL :: save_spectrum, use_real_wfn
3657 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues
3658 : TYPE(cp_cfm_type) :: csmat, cwork
3659 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
3660 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
3661 : TYPE(cp_fm_type) :: fmlocal, rsmat
3662 : TYPE(kpoint_env_type), POINTER :: kp
3663 140 : TYPE(kpoint_operator_context_type) :: op_ctx
3664 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3665 140 : POINTER :: sab_nl
3666 : TYPE(qs_matrix_pools_type), POINTER :: mpools
3667 :
3668 140 : CALL timeset(routineN, handle)
3669 :
3670 140 : NULLIFY (sab_nl)
3671 : CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn, kp_range=kp_range, &
3672 140 : kp_dist=kp_dist, sab_nl=sab_nl)
3673 140 : CPASSERT(ASSOCIATED(sab_nl))
3674 140 : kplocal = kp_range(2) - kp_range(1) + 1
3675 408 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
3676 :
3677 : ! fm pools to be used within a kpoint group
3678 140 : CALL get_kpoint_info(kpoints, mpools=mpools)
3679 140 : CALL mpools_get(mpools, ao_ao_fm_pool=ao_ao_fm_pool)
3680 :
3681 140 : CALL fm_pool_create_fm(ao_ao_fm_pool, fmlocal)
3682 140 : CALL cp_fm_get_info(fmlocal, matrix_struct=matrix_struct)
3683 :
3684 140 : IF (use_real_wfn) THEN
3685 0 : CALL cp_fm_create(rsmat, matrix_struct)
3686 : ELSE
3687 140 : CALL cp_cfm_create(csmat, matrix_struct)
3688 140 : CALL cp_cfm_create(cwork, matrix_struct)
3689 : END IF
3690 :
3691 140 : CALL cp_fm_get_info(fmwork(1), nrow_global=nao)
3692 700 : ALLOCATE (eigenvalues(nao), ceig(nao))
3693 :
3694 : ! The overlap is spin free: the routine drives every instance under the
3695 : ! single spin-free key.
3696 140 : CALL kpoint_operator_context_create(op_ctx, kpoints, fmwork(1), 1)
3697 140 : CALL kpoint_operator_prepare(op_ctx, matrix_s, kpoint_slot_s, matrix_row=kpoint_spin_free)
3698 650 : DO ikp = 1, kpmax
3699 : CALL kpoint_operator_start(op_ctx, ikp, kpoint_spin_free, matrix_s, kpoint_slot_s, &
3700 650 : matrix_row=kpoint_spin_free)
3701 : END DO
3702 :
3703 : ! Finish communication then diagonalise in each group
3704 650 : DO ikp = 1, kpmax
3705 510 : IF (use_real_wfn) THEN
3706 0 : CALL kpoint_operator_finish(op_ctx, ikp, kpoint_spin_free, kpoint_slot_s, fm_re=rsmat)
3707 : ELSE
3708 510 : CALL kpoint_operator_finish(op_ctx, ikp, kpoint_spin_free, kpoint_slot_s, csmat)
3709 : END IF
3710 :
3711 : ! Each kpoint group has now information on a kpoint to be diagonalized
3712 : ! Eigensolver Hermite or Symmetric
3713 510 : IF (ikp > kplocal) CYCLE
3714 508 : kp => kpoints%kp_env(ikp)%kpoint_env
3715 508 : save_spectrum = ALLOCATED(kp%lowdin_sqrt)
3716 508 : IF (PRESENT(lowdin_forces)) save_spectrum = lowdin_forces
3717 508 : CALL cp_cfm_release(kp%lowdin_evec)
3718 508 : IF (ALLOCATED(kp%lowdin_sqrt)) DEALLOCATE (kp%lowdin_sqrt)
3719 508 : IF (use_real_wfn) THEN
3720 0 : CALL choose_eigv_solver(rsmat, fmlocal, eigenvalues)
3721 : ELSE
3722 508 : CALL cp_cfm_heevd(csmat, cwork, eigenvalues)
3723 : END IF
3724 8846 : CPASSERT(ALL(eigenvalues(1:nao) >= 0.0_dp))
3725 508 : IF (save_spectrum) THEN
3726 384 : IF (ANY(eigenvalues <= 0.0_dp)) THEN
3727 0 : CPABORT("Lowdin forces require a positive definite overlap")
3728 : END IF
3729 32 : ALLOCATE (kp%lowdin_sqrt(nao))
3730 384 : kp%lowdin_sqrt(:) = SQRT(eigenvalues)
3731 16 : CALL cp_cfm_create(kp%lowdin_evec, matrix_struct)
3732 16 : IF (use_real_wfn) THEN
3733 0 : CALL cp_fm_to_cfm(fmlocal, mtarget=kp%lowdin_evec)
3734 : ELSE
3735 16 : CALL cp_cfm_to_cfm(cwork, kp%lowdin_evec)
3736 : END IF
3737 : END IF
3738 648 : IF (use_real_wfn) THEN
3739 0 : CALL cp_fm_release(kp%shalf)
3740 0 : CALL cp_fm_create(kp%shalf, matrix_struct)
3741 0 : eigenvalues(1:nao) = SQRT(eigenvalues(1:nao))
3742 0 : CALL cp_fm_to_fm(fmlocal, rsmat)
3743 0 : CALL cp_fm_column_scale(rsmat, eigenvalues)
3744 : CALL parallel_gemm("N", "T", nao, nao, nao, 1.0_dp, rsmat, fmlocal, &
3745 0 : 0.0_dp, kp%shalf)
3746 : ELSE
3747 508 : CALL cp_cfm_release(kp%cshalf)
3748 508 : CALL cp_cfm_create(kp%cshalf, matrix_struct)
3749 8846 : ceig(1:nao) = SQRT(eigenvalues(1:nao))
3750 508 : CALL cp_cfm_to_cfm(cwork, csmat)
3751 508 : CALL cp_cfm_column_scale(csmat, ceig)
3752 : CALL parallel_gemm("N", "C", nao, nao, nao, cone, csmat, cwork, &
3753 508 : czero, kp%cshalf)
3754 : END IF
3755 : END DO
3756 :
3757 140 : CALL kpoint_operator_context_release(op_ctx)
3758 :
3759 : ! All done
3760 140 : DEALLOCATE (eigenvalues, ceig)
3761 :
3762 140 : IF (use_real_wfn) THEN
3763 0 : CALL cp_fm_release(rsmat)
3764 : ELSE
3765 140 : CALL cp_cfm_release(csmat)
3766 140 : CALL cp_cfm_release(cwork)
3767 : END IF
3768 140 : CALL fm_pool_give_back_fm(ao_ao_fm_pool, fmlocal)
3769 :
3770 140 : CALL timestop(handle)
3771 :
3772 560 : END SUBROUTINE diag_kp_smat
3773 :
3774 : END MODULE qs_scf_diagonalization
|