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 Storage of past states of the qs_env.
10 : !> Methods to interpolate (or actually normally extrapolate) the
11 : !> new guess for density and wavefunctions.
12 : !> \note
13 : !> Most of the last snapshot should actually be in qs_env, but taking
14 : !> advantage of it would make the programming much convoluted
15 : !> \par History
16 : !> 02.2003 created [fawzi]
17 : !> 11.2003 Joost VandeVondele : Implemented Nth order PS extrapolation
18 : !> 02.2005 modified for KG_GPW [MI]
19 : !> \author fawzi
20 : ! **************************************************************************************************
21 : MODULE qs_wf_history_methods
22 : USE bibliography, ONLY: Kolafa2004,&
23 : Kuhne2007,&
24 : VandeVondele2005a,&
25 : cite_reference
26 : USE cell_types, ONLY: cell_type,&
27 : pbc,&
28 : real_to_scaled
29 : USE cp_cfm_basic_linalg, ONLY: cp_cfm_column_scale,&
30 : cp_cfm_scale,&
31 : cp_cfm_scale_and_add,&
32 : cp_cfm_trace,&
33 : cp_cfm_triangular_multiply
34 : USE cp_cfm_cholesky, ONLY: cp_cfm_cholesky_decompose
35 : USE cp_cfm_diag, ONLY: cp_cfm_heevd
36 : USE cp_cfm_types, ONLY: cp_cfm_create,&
37 : cp_cfm_get_info,&
38 : cp_cfm_get_submatrix,&
39 : cp_cfm_release,&
40 : cp_cfm_set_all,&
41 : cp_cfm_set_submatrix,&
42 : cp_cfm_to_cfm,&
43 : cp_cfm_type
44 : USE cp_control_types, ONLY: dft_control_type
45 : USE cp_dbcsr_api, ONLY: dbcsr_add,&
46 : dbcsr_copy,&
47 : dbcsr_deallocate_matrix,&
48 : dbcsr_get_info,&
49 : dbcsr_multiply,&
50 : dbcsr_p_type,&
51 : dbcsr_release,&
52 : dbcsr_type
53 : USE cp_dbcsr_contrib, ONLY: dbcsr_frobenius_norm,&
54 : dbcsr_trace
55 : USE cp_dbcsr_operations, ONLY: cp_dbcsr_sm_fm_multiply,&
56 : dbcsr_allocate_matrix_set,&
57 : dbcsr_deallocate_matrix_set
58 : USE cp_fm_basic_linalg, ONLY: cp_fm_scale,&
59 : cp_fm_scale_and_add
60 : USE cp_fm_pool_types, ONLY: cp_fm_pool_p_type,&
61 : cp_fm_pool_type,&
62 : fm_pool_get_el_struct,&
63 : fm_pools_create_fm_vect,&
64 : fm_pools_give_back_fm_vect
65 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
66 : cp_fm_struct_equivalent,&
67 : cp_fm_struct_release,&
68 : cp_fm_struct_type
69 : USE cp_fm_types, ONLY: cp_fm_create,&
70 : cp_fm_get_info,&
71 : cp_fm_release,&
72 : cp_fm_set_all,&
73 : cp_fm_to_fm,&
74 : cp_fm_type
75 : USE cp_log_handling, ONLY: cp_get_default_logger,&
76 : cp_logger_type,&
77 : cp_to_string
78 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
79 : cp_print_key_unit_nr,&
80 : low_print_level
81 : USE input_constants, ONLY: &
82 : wfi_aspc_nr, wfi_frozen_method_nr, wfi_gext_proj_nr, wfi_gext_proj_qtr_nr, &
83 : wfi_linear_p_method_nr, wfi_linear_ps_method_nr, wfi_linear_wf_method_nr, &
84 : wfi_ps_method_nr, wfi_use_guess_method_nr, wfi_use_prev_p_method_nr, &
85 : wfi_use_prev_wf_method_nr
86 : USE kinds, ONLY: dp
87 : USE kpoint_types, ONLY: get_kpoint_info,&
88 : kpoint_env_type,&
89 : kpoint_type
90 : USE mathconstants, ONLY: twopi,&
91 : z_one,&
92 : z_zero
93 : USE mathlib, ONLY: binomial
94 : USE message_passing, ONLY: mp_para_env_type
95 : USE parallel_gemm_api, ONLY: parallel_gemm
96 : USE particle_types, ONLY: particle_type
97 : USE pw_env_types, ONLY: pw_env_get,&
98 : pw_env_type
99 : USE pw_methods, ONLY: pw_copy
100 : USE pw_pool_types, ONLY: pw_pool_type
101 : USE pw_types, ONLY: pw_c1d_gs_type,&
102 : pw_r3d_rs_type
103 : USE qs_density_matrices, ONLY: calculate_density_matrix
104 : USE qs_energy_types, ONLY: qs_energy_type
105 : USE qs_environment_types, ONLY: get_qs_env,&
106 : qs_environment_type,&
107 : set_qs_env
108 : USE qs_kpoint_operators, ONLY: kpoint_operator_get_all,&
109 : kpoint_slot_s,&
110 : kpoint_spin_free
111 : USE qs_kpoint_state, ONLY: qs_kpoint_state_commit
112 : USE qs_ks_types, ONLY: qs_ks_did_change
113 : USE qs_matrix_pools, ONLY: mpools_get,&
114 : qs_matrix_pools_type
115 : USE qs_mo_methods, ONLY: make_basis_cholesky,&
116 : make_basis_lowdin,&
117 : make_basis_simple,&
118 : make_basis_sm
119 : USE qs_mo_types, ONLY: get_mo_set,&
120 : mo_set_type,&
121 : set_mo_set
122 : USE qs_rho_methods, ONLY: qs_rho_update_rho
123 : USE qs_rho_types, ONLY: qs_rho_get,&
124 : qs_rho_type
125 : USE qs_scf_types, ONLY: ot_method_nr,&
126 : qs_scf_env_type
127 : USE qs_wf_history_types, ONLY: qs_wf_history_type,&
128 : qs_wf_mo_state_type,&
129 : qs_wf_snapshot_type,&
130 : wfi_get_snapshot,&
131 : wfi_release
132 : USE scf_control_types, ONLY: scf_control_type
133 : #include "./base/base_uses.f90"
134 :
135 : IMPLICIT NONE
136 : PRIVATE
137 :
138 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
139 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_wf_history_methods'
140 :
141 : PUBLIC :: wfi_create, wfi_update, wfi_create_for_kp, &
142 : wfi_enable_prev_wf_storage, wfi_extrapolate, &
143 : wfi_get_method_label, reorthogonalize_vectors, &
144 : wfi_purge_history
145 :
146 : CONTAINS
147 :
148 : ! **************************************************************************************************
149 : !> \brief allocates and initialize a wavefunction snapshot
150 : !> \param snapshot the snapshot to create
151 : !> \par History
152 : !> 02.2003 created [fawzi]
153 : !> 02.2005 added wf_mol [MI]
154 : !> \author fawzi
155 : ! **************************************************************************************************
156 12446 : SUBROUTINE wfs_create(snapshot)
157 : TYPE(qs_wf_snapshot_type), INTENT(OUT) :: snapshot
158 :
159 : NULLIFY (snapshot%wf, snapshot%rho_r, &
160 : snapshot%rho_g, snapshot%rho_ao, &
161 : snapshot%overlap, snapshot%wf_kp, snapshot%wf_kp_cfm, snapshot%overlap_cfm_kp, &
162 : snapshot%kp_pbc_shift, snapshot%rho_frozen)
163 12446 : snapshot%dt = 1.0_dp
164 12446 : END SUBROUTINE wfs_create
165 :
166 : ! **************************************************************************************************
167 : !> \brief updates the given snapshot
168 : !> \param snapshot the snapshot to be updated
169 : !> \param wf_history the history
170 : !> \param qs_env the qs_env that should be snapshotted
171 : !> \param dt the time of the snapshot (wrt. to the previous snapshot)
172 : !> \par History
173 : !> 02.2003 created [fawzi]
174 : !> 02.2005 added kg_fm_mol_set for KG_GPW [MI]
175 : !> \author fawzi
176 : ! **************************************************************************************************
177 23132 : SUBROUTINE wfs_update(snapshot, wf_history, qs_env, dt)
178 : TYPE(qs_wf_snapshot_type), POINTER :: snapshot
179 : TYPE(qs_wf_history_type), POINTER :: wf_history
180 : TYPE(qs_environment_type), POINTER :: qs_env
181 : REAL(KIND=dp), INTENT(in), OPTIONAL :: dt
182 :
183 : CHARACTER(len=*), PARAMETER :: routineN = 'wfs_update'
184 :
185 : INTEGER :: handle, ikp, img, ispin, kplocal, nimg, &
186 : nspin_kp, nspins
187 : INTEGER, DIMENSION(2) :: kp_range
188 : LOGICAL :: use_real_wfn
189 : TYPE(cell_type), POINTER :: cell
190 23132 : TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: ao_mo_pools
191 : TYPE(cp_fm_type), POINTER :: mo_coeff
192 23132 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
193 23132 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao
194 : TYPE(dft_control_type), POINTER :: dft_control
195 : TYPE(kpoint_env_type), POINTER :: kp
196 : TYPE(kpoint_type), POINTER :: kpoints
197 23132 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
198 23132 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
199 23132 : TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
200 : TYPE(pw_env_type), POINTER :: pw_env
201 : TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
202 23132 : TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r
203 : TYPE(qs_rho_type), POINTER :: rho
204 :
205 23132 : CALL timeset(routineN, handle)
206 :
207 23132 : NULLIFY (pw_env, auxbas_pw_pool, ao_mo_pools, dft_control, mos, mo_coeff, &
208 23132 : rho, rho_r, rho_g, rho_ao, matrix_s, kpoints, kp, cell, particle_set)
209 : CALL get_qs_env(qs_env, pw_env=pw_env, &
210 23132 : dft_control=dft_control, rho=rho, cell=cell, particle_set=particle_set)
211 23132 : CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_pools)
212 23132 : CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
213 :
214 23132 : CPASSERT(ASSOCIATED(wf_history))
215 23132 : CPASSERT(ASSOCIATED(dft_control))
216 23132 : IF (.NOT. ASSOCIATED(snapshot)) THEN
217 12446 : ALLOCATE (snapshot)
218 12446 : CALL wfs_create(snapshot)
219 : END IF
220 23132 : CPASSERT(wf_history%ref_count > 0)
221 :
222 23132 : nspins = dft_control%nspins
223 23132 : snapshot%dt = 1.0_dp
224 23132 : IF (PRESENT(dt)) snapshot%dt = dt
225 23132 : IF (wf_history%store_wf) THEN
226 19554 : CALL get_qs_env(qs_env, mos=mos)
227 19554 : IF (.NOT. ASSOCIATED(snapshot%wf)) THEN
228 : CALL fm_pools_create_fm_vect(ao_mo_pools, snapshot%wf, &
229 9952 : name="ws_snap-ws")
230 9952 : CPASSERT(nspins == SIZE(snapshot%wf))
231 : END IF
232 51089 : IF (.NOT. ALLOCATED(snapshot%mo_state)) ALLOCATE (snapshot%mo_state(nspins))
233 41464 : DO ispin = 1, nspins
234 21910 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
235 21910 : CALL cp_fm_to_fm(mo_coeff, snapshot%wf(ispin))
236 41464 : CALL wfs_save_mo_state(mos(ispin), snapshot%mo_state(ispin))
237 : END DO
238 : ELSE
239 3578 : CALL fm_pools_give_back_fm_vect(ao_mo_pools, snapshot%wf)
240 3578 : IF (ALLOCATED(snapshot%mo_state)) DEALLOCATE (snapshot%mo_state)
241 : END IF
242 :
243 23132 : IF (wf_history%store_rho_r) THEN
244 0 : CALL qs_rho_get(rho, rho_r=rho_r)
245 0 : CPASSERT(ASSOCIATED(rho_r))
246 0 : IF (.NOT. ASSOCIATED(snapshot%rho_r)) THEN
247 0 : ALLOCATE (snapshot%rho_r(nspins))
248 0 : DO ispin = 1, nspins
249 0 : CALL auxbas_pw_pool%create_pw(snapshot%rho_r(ispin))
250 : END DO
251 : END IF
252 0 : DO ispin = 1, nspins
253 0 : CALL pw_copy(rho_r(ispin), snapshot%rho_r(ispin))
254 : END DO
255 23132 : ELSE IF (ASSOCIATED(snapshot%rho_r)) THEN
256 0 : DO ispin = 1, SIZE(snapshot%rho_r)
257 0 : CALL auxbas_pw_pool%give_back_pw(snapshot%rho_r(ispin))
258 : END DO
259 0 : DEALLOCATE (snapshot%rho_r)
260 : END IF
261 :
262 23132 : IF (wf_history%store_rho_g) THEN
263 0 : CALL qs_rho_get(rho, rho_g=rho_g)
264 0 : CPASSERT(ASSOCIATED(rho_g))
265 0 : IF (.NOT. ASSOCIATED(snapshot%rho_g)) THEN
266 0 : ALLOCATE (snapshot%rho_g(nspins))
267 0 : DO ispin = 1, nspins
268 0 : CALL auxbas_pw_pool%create_pw(snapshot%rho_g(ispin))
269 : END DO
270 : END IF
271 0 : DO ispin = 1, nspins
272 0 : CALL pw_copy(rho_g(ispin), snapshot%rho_g(ispin))
273 : END DO
274 23132 : ELSE IF (ASSOCIATED(snapshot%rho_g)) THEN
275 0 : DO ispin = 1, SIZE(snapshot%rho_g)
276 0 : CALL auxbas_pw_pool%give_back_pw(snapshot%rho_g(ispin))
277 : END DO
278 0 : DEALLOCATE (snapshot%rho_g)
279 : END IF
280 :
281 23132 : IF (ASSOCIATED(snapshot%rho_ao)) THEN ! the sparsity might be different
282 : ! (future struct:check)
283 482 : CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao)
284 : END IF
285 23132 : IF (wf_history%store_rho_ao) THEN
286 544 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao)
287 544 : CPASSERT(ASSOCIATED(rho_ao))
288 :
289 544 : nimg = SIZE(rho_ao, 2)
290 544 : CALL dbcsr_allocate_matrix_set(snapshot%rho_ao, nspins, nimg)
291 1328 : DO ispin = 1, nspins
292 35328 : DO img = 1, nimg
293 34000 : ALLOCATE (snapshot%rho_ao(ispin, img)%matrix)
294 34784 : CALL dbcsr_copy(snapshot%rho_ao(ispin, img)%matrix, rho_ao(ispin, img)%matrix)
295 : END DO
296 : END DO
297 : END IF
298 :
299 23132 : IF (ASSOCIATED(snapshot%overlap)) THEN ! the sparsity might be different
300 : ! (future struct:check)
301 6994 : CALL dbcsr_deallocate_matrix(snapshot%overlap)
302 : END IF
303 23132 : IF (wf_history%store_overlap) THEN
304 16036 : CALL get_qs_env(qs_env, matrix_s=matrix_s)
305 16036 : CPASSERT(ASSOCIATED(matrix_s))
306 16036 : CPASSERT(ASSOCIATED(matrix_s(1)%matrix))
307 16036 : ALLOCATE (snapshot%overlap)
308 16036 : CALL dbcsr_copy(snapshot%overlap, matrix_s(1)%matrix)
309 : END IF
310 :
311 23132 : CALL get_qs_env(qs_env, kpoints=kpoints)
312 23132 : IF (ASSOCIATED(kpoints)) THEN
313 23132 : IF (ASSOCIATED(kpoints%kp_env)) THEN
314 : ! Store k-point MO snapshots in their native scalar type.
315 3250 : IF (wf_history%store_wf_kp) THEN
316 3018 : CALL get_kpoint_info(kpoints, kp_range=kp_range, use_real_wfn=use_real_wfn)
317 3018 : kplocal = kp_range(2) - kp_range(1) + 1
318 3018 : nspin_kp = SIZE(kpoints%kp_env(1)%kpoint_env%mos)
319 :
320 3018 : CALL wfi_store_kp_pbc_shift(snapshot, cell, particle_set)
321 :
322 3018 : IF (ASSOCIATED(snapshot%wf_kp)) THEN
323 0 : CALL cp_fm_release(snapshot%wf_kp)
324 : END IF
325 3018 : IF (ASSOCIATED(snapshot%wf_kp_cfm)) THEN
326 1740 : DO ikp = 1, SIZE(snapshot%wf_kp_cfm, 1)
327 3082 : DO ispin = 1, SIZE(snapshot%wf_kp_cfm, 2)
328 2492 : CALL cp_cfm_release(snapshot%wf_kp_cfm(ikp, ispin))
329 : END DO
330 : END DO
331 590 : DEALLOCATE (snapshot%wf_kp_cfm)
332 : END IF
333 :
334 3018 : IF (use_real_wfn) THEN
335 0 : ALLOCATE (snapshot%wf_kp(kplocal, nspin_kp))
336 : ELSE
337 21666 : ALLOCATE (snapshot%wf_kp_cfm(kplocal, nspin_kp))
338 : END IF
339 3018 : IF (.NOT. ALLOCATED(snapshot%mo_state_kp)) THEN
340 17278 : ALLOCATE (snapshot%mo_state_kp(kplocal, nspin_kp))
341 : END IF
342 8464 : DO ikp = 1, kplocal
343 5446 : kp => kpoints%kp_env(ikp)%kpoint_env
344 14784 : DO ispin = 1, nspin_kp
345 6320 : IF (use_real_wfn) THEN
346 0 : CALL get_mo_set(kp%mos(ispin), mo_coeff=mo_coeff)
347 : CALL cp_fm_create(snapshot%wf_kp(ikp, ispin), mo_coeff%matrix_struct, &
348 0 : name="wfkp_snap")
349 0 : CALL cp_fm_to_fm(mo_coeff, snapshot%wf_kp(ikp, ispin))
350 : ELSE
351 : CALL cp_cfm_create(snapshot%wf_kp_cfm(ikp, ispin), &
352 6320 : kp%mos(ispin)%cmo_coeff%matrix_struct, name="wfkp_snap")
353 6320 : CALL cp_cfm_to_cfm(kp%mos(ispin)%cmo_coeff, snapshot%wf_kp_cfm(ikp, ispin))
354 : END IF
355 11766 : CALL wfs_save_mo_state(kp%mos(ispin), snapshot%mo_state_kp(ikp, ispin))
356 : END DO
357 : END DO
358 232 : ELSE IF (ALLOCATED(snapshot%mo_state_kp)) THEN
359 0 : DEALLOCATE (snapshot%mo_state_kp)
360 : END IF
361 :
362 3250 : IF (wf_history%store_overlap_kp) CALL wfs_overlap_kp(snapshot, qs_env)
363 : END IF
364 : END IF
365 :
366 : IF (wf_history%store_frozen_density) THEN
367 : ! do nothing
368 : ! CALL deallocate_matrix_set(snapshot%rho_frozen%rho_ao)
369 : END IF
370 :
371 23132 : CALL timestop(handle)
372 :
373 23132 : END SUBROUTINE wfs_update
374 :
375 : ! **************************************************************************************************
376 : !> \brief Store S(k) in the current neighbor-list and PBC convention.
377 : !> Reuse the unmodified overlap cache from this SCF run when available.
378 : !> Otherwise build the snapshot now, never from S(R) at a later geometry.
379 : !> \param snapshot owns its matrices independently of the SCF cache and operator context
380 : !> \param qs_env current environment; its SCF overlap cache is invalidated by init_scf_run
381 : ! **************************************************************************************************
382 3012 : SUBROUTINE wfs_overlap_kp(snapshot, qs_env)
383 : TYPE(qs_wf_snapshot_type), INTENT(INOUT) :: snapshot
384 : TYPE(qs_environment_type), POINTER :: qs_env
385 :
386 : CHARACTER(LEN=*), PARAMETER :: routineN = 'wfs_overlap_kp'
387 :
388 : INTEGER :: handle, ikp, kplocal, use_cache
389 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
390 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct
391 3012 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
392 : TYPE(kpoint_type), POINTER :: kpoints
393 : TYPE(mp_para_env_type), POINTER :: para_env
394 : TYPE(qs_scf_env_type), POINTER :: scf_env
395 :
396 3012 : CALL timeset(routineN, handle)
397 3012 : CALL get_qs_env(qs_env, kpoints=kpoints, matrix_s_kp=matrix_s, scf_env=scf_env)
398 3012 : para_env => kpoints%blacs_env_all%para_env
399 3012 : kplocal = SIZE(kpoints%kp_env)
400 3012 : CPASSERT(kplocal > 0)
401 3012 : CALL mpools_get(kpoints%mpools, ao_ao_fm_pool=ao_ao_fm_pool)
402 3012 : matrix_struct => fm_pool_get_el_struct(ao_ao_fm_pool)
403 :
404 : ! All ranks must choose the same path if any cache is missing or incompatible.
405 3012 : use_cache = 0
406 3012 : IF (ALLOCATED(kpoints%scf_smat)) THEN
407 2850 : IF (SIZE(kpoints%scf_smat) == kplocal) THEN
408 2850 : use_cache = 1
409 7958 : DO ikp = 1, kplocal
410 7958 : IF (.NOT. cp_fm_struct_equivalent(kpoints%scf_smat(ikp)%matrix_struct, matrix_struct)) use_cache = 0
411 : END DO
412 : END IF
413 : END IF
414 3012 : CALL para_env%min(use_cache)
415 :
416 3012 : IF (ASSOCIATED(snapshot%overlap_cfm_kp)) THEN
417 1720 : DO ikp = 1, SIZE(snapshot%overlap_cfm_kp)
418 1720 : CALL cp_cfm_release(snapshot%overlap_cfm_kp(ikp))
419 : END DO
420 586 : DEALLOCATE (snapshot%overlap_cfm_kp)
421 : END IF
422 14458 : ALLOCATE (snapshot%overlap_cfm_kp(kplocal))
423 : ! Matrix allocation and structure refcounts stay outside OpenMP workers.
424 8434 : DO ikp = 1, kplocal
425 8434 : CALL cp_cfm_create(snapshot%overlap_cfm_kp(ikp), matrix_struct)
426 : END DO
427 :
428 3012 : IF (use_cache == 1) THEN
429 7958 : DO ikp = 1, kplocal
430 7958 : CALL cp_cfm_to_cfm(kpoints%scf_smat(ikp), snapshot%overlap_cfm_kp(ikp))
431 : END DO
432 : ELSE
433 : CALL kpoint_operator_get_all(kpoints, scf_env%scf_work1(1), matrix_s, kpoint_slot_s, &
434 162 : snapshot%overlap_cfm_kp, matrix_row=kpoint_spin_free)
435 : END IF
436 3012 : CALL timestop(handle)
437 :
438 3012 : END SUBROUTINE wfs_overlap_kp
439 :
440 : ! **************************************************************************************************
441 : !> \brief ...
442 : !> \param wf_history ...
443 : !> \param interpolation_method_nr the tag of the method used for
444 : !> the extrapolation of the initial density for the next md step
445 : !> (see qs_wf_history_types:wfi_*_method_nr)
446 : !> \param extrapolation_order ...
447 : !> \param has_unit_metric ...
448 : !> \par History
449 : !> 02.2003 created [fawzi]
450 : !> \author fawzi
451 : ! **************************************************************************************************
452 9414 : SUBROUTINE wfi_create(wf_history, interpolation_method_nr, extrapolation_order, &
453 : has_unit_metric)
454 : TYPE(qs_wf_history_type), POINTER :: wf_history
455 : INTEGER, INTENT(in) :: interpolation_method_nr, &
456 : extrapolation_order
457 : LOGICAL, INTENT(IN) :: has_unit_metric
458 :
459 : CHARACTER(len=*), PARAMETER :: routineN = 'wfi_create'
460 :
461 : INTEGER :: handle, i
462 :
463 9414 : CALL timeset(routineN, handle)
464 :
465 9414 : ALLOCATE (wf_history)
466 9414 : wf_history%ref_count = 1
467 9414 : wf_history%memory_depth = 0
468 9414 : wf_history%snapshot_count = 0
469 9414 : wf_history%last_state_index = 1
470 : wf_history%store_wf = .FALSE.
471 : wf_history%store_rho_r = .FALSE.
472 : wf_history%store_rho_g = .FALSE.
473 : wf_history%store_rho_ao = .FALSE.
474 : wf_history%store_overlap = .FALSE.
475 : wf_history%store_wf_kp = .FALSE.
476 : wf_history%store_overlap_kp = .FALSE.
477 : wf_history%store_frozen_density = .FALSE.
478 : NULLIFY (wf_history%past_states)
479 :
480 9414 : wf_history%interpolation_method_nr = interpolation_method_nr
481 :
482 : SELECT CASE (wf_history%interpolation_method_nr)
483 : CASE (wfi_use_guess_method_nr)
484 : wf_history%memory_depth = 0
485 : CASE (wfi_use_prev_wf_method_nr)
486 72 : wf_history%memory_depth = 0
487 : CASE (wfi_use_prev_p_method_nr)
488 72 : wf_history%memory_depth = 1
489 72 : wf_history%store_rho_ao = .TRUE.
490 : CASE (wfi_linear_wf_method_nr)
491 4 : wf_history%memory_depth = 2
492 4 : wf_history%store_wf = .TRUE.
493 : CASE (wfi_linear_p_method_nr)
494 6 : wf_history%memory_depth = 2
495 6 : wf_history%store_rho_ao = .TRUE.
496 : CASE (wfi_linear_ps_method_nr)
497 6 : wf_history%memory_depth = 2
498 6 : wf_history%store_wf = .TRUE.
499 6 : IF (.NOT. has_unit_metric) wf_history%store_overlap = .TRUE.
500 : CASE (wfi_ps_method_nr)
501 321 : CALL cite_reference(VandeVondele2005a)
502 321 : wf_history%memory_depth = extrapolation_order + 1
503 321 : wf_history%store_wf = .TRUE.
504 321 : wf_history%store_wf_kp = .TRUE.
505 321 : IF (.NOT. has_unit_metric) THEN
506 317 : wf_history%store_overlap = .TRUE.
507 317 : wf_history%store_overlap_kp = .TRUE.
508 : END IF
509 : CASE (wfi_frozen_method_nr)
510 4 : wf_history%memory_depth = 1
511 4 : wf_history%store_frozen_density = .TRUE.
512 : CASE (wfi_aspc_nr)
513 8595 : wf_history%memory_depth = extrapolation_order + 2
514 8595 : wf_history%store_wf = .TRUE.
515 8595 : wf_history%store_wf_kp = .TRUE.
516 8595 : IF (.NOT. has_unit_metric) THEN
517 7615 : wf_history%store_overlap = .TRUE.
518 7615 : wf_history%store_overlap_kp = .TRUE.
519 : END IF
520 : CASE (wfi_gext_proj_nr)
521 46 : wf_history%memory_depth = extrapolation_order
522 46 : wf_history%store_wf = .TRUE.
523 46 : wf_history%store_wf_kp = .TRUE.
524 46 : wf_history%store_overlap = .TRUE.
525 46 : wf_history%store_overlap_kp = .TRUE.
526 : CASE (wfi_gext_proj_qtr_nr)
527 6 : wf_history%memory_depth = extrapolation_order
528 6 : wf_history%store_wf = .TRUE.
529 6 : wf_history%store_wf_kp = .TRUE.
530 6 : wf_history%store_overlap = .TRUE.
531 6 : wf_history%store_overlap_kp = .TRUE.
532 : CASE default
533 : CALL cp_abort(__LOCATION__, &
534 : "Unknown interpolation method: "// &
535 9414 : TRIM(ADJUSTL(cp_to_string(interpolation_method_nr))))
536 : END SELECT
537 72153 : ALLOCATE (wf_history%past_states(wf_history%memory_depth))
538 :
539 53679 : DO i = 1, SIZE(wf_history%past_states)
540 53679 : NULLIFY (wf_history%past_states(i)%snapshot)
541 : END DO
542 :
543 9414 : CALL timestop(handle)
544 9414 : END SUBROUTINE wfi_create
545 :
546 : ! **************************************************************************************************
547 : !> \brief Adapts wf_history storage flags for k-point calculations.
548 : !> For ASPC, switches from Gamma WFN storage to k-point WFN storage.
549 : !> Other WFN-based methods remain blocked.
550 : !> \param wf_history ...
551 : !> \par History
552 : !> 06.2015 created [jhu]
553 : !> \author jhu
554 : ! **************************************************************************************************
555 822 : SUBROUTINE wfi_create_for_kp(wf_history)
556 : TYPE(qs_wf_history_type), POINTER :: wf_history
557 :
558 : INTEGER :: i
559 :
560 822 : CPASSERT(ASSOCIATED(wf_history))
561 : ! KP-compatible WFN history: store complex k-point MOs in snapshots.
562 : ! USE_PREV_WF needs one snapshot as well, since the PBC image convention
563 : ! of the saved WFN has to be known before reorthogonalization.
564 822 : IF (wf_history%interpolation_method_nr == wfi_use_prev_wf_method_nr) THEN
565 20 : wf_history%memory_depth = 1
566 20 : wf_history%store_wf_kp = .TRUE.
567 20 : wf_history%store_wf = .FALSE.
568 20 : wf_history%store_overlap = .FALSE.
569 20 : IF (ASSOCIATED(wf_history%past_states)) DEALLOCATE (wf_history%past_states)
570 80 : ALLOCATE (wf_history%past_states(wf_history%memory_depth))
571 40 : DO i = 1, SIZE(wf_history%past_states)
572 40 : NULLIFY (wf_history%past_states(i)%snapshot)
573 : END DO
574 802 : ELSE IF (wf_history%store_wf_kp) THEN
575 650 : wf_history%store_wf = .FALSE.
576 650 : wf_history%store_overlap = .FALSE.
577 : ! store_wf_kp and store_overlap_kp remain TRUE
578 : ELSE
579 : ! Linear methods (except LINEAR_P) are still blocked
580 152 : IF (wf_history%store_wf .OR. wf_history%store_overlap) THEN
581 : CALL cp_abort(__LOCATION__, &
582 0 : "Linear WFN-based extrapolation methods not implemented for k-points.")
583 : END IF
584 : END IF
585 822 : IF (wf_history%store_frozen_density) THEN
586 0 : CPABORT("Frozen density initialization method not possible for kpoints.")
587 : END IF
588 :
589 822 : END SUBROUTINE wfi_create_for_kp
590 :
591 : ! **************************************************************************************************
592 : !> \brief Stores one Gamma-point MO snapshot for USE_PREV_WF when multiple images share one qs_env.
593 : !> \param wf_history ...
594 : ! **************************************************************************************************
595 12 : SUBROUTINE wfi_enable_prev_wf_storage(wf_history)
596 : TYPE(qs_wf_history_type), POINTER :: wf_history
597 :
598 : INTEGER :: i
599 :
600 12 : CPASSERT(ASSOCIATED(wf_history))
601 12 : IF (wf_history%interpolation_method_nr == wfi_use_prev_wf_method_nr) THEN
602 0 : wf_history%memory_depth = 1
603 0 : wf_history%store_wf = .TRUE.
604 0 : IF (ASSOCIATED(wf_history%past_states)) DEALLOCATE (wf_history%past_states)
605 0 : ALLOCATE (wf_history%past_states(wf_history%memory_depth))
606 0 : DO i = 1, SIZE(wf_history%past_states)
607 0 : NULLIFY (wf_history%past_states(i)%snapshot)
608 : END DO
609 : END IF
610 12 : END SUBROUTINE wfi_enable_prev_wf_storage
611 :
612 : ! **************************************************************************************************
613 : !> \brief returns a string describing the interpolation method
614 : !> \param method_nr ...
615 : !> \return ...
616 : !> \par History
617 : !> 02.2003 created [fawzi]
618 : !> \author fawzi
619 : ! **************************************************************************************************
620 13940 : FUNCTION wfi_get_method_label(method_nr) RESULT(res)
621 : INTEGER, INTENT(in) :: method_nr
622 : CHARACTER(len=30) :: res
623 :
624 13940 : res = "unknown"
625 14178 : SELECT CASE (method_nr)
626 : CASE (wfi_use_prev_p_method_nr)
627 238 : res = "previous_p"
628 : CASE (wfi_use_prev_wf_method_nr)
629 337 : res = "previous_wf"
630 : CASE (wfi_use_guess_method_nr)
631 5311 : res = "initial_guess"
632 : CASE (wfi_linear_wf_method_nr)
633 2 : res = "mo linear"
634 : CASE (wfi_linear_p_method_nr)
635 3 : res = "P linear"
636 : CASE (wfi_linear_ps_method_nr)
637 6 : res = "PS linear"
638 : CASE (wfi_ps_method_nr)
639 154 : res = "PS Nth order"
640 : CASE (wfi_frozen_method_nr)
641 4 : res = "frozen density approximation"
642 : CASE (wfi_aspc_nr)
643 7683 : res = "ASPC"
644 : CASE (wfi_gext_proj_nr)
645 188 : res = "GEXT_PROJ"
646 : CASE (wfi_gext_proj_qtr_nr)
647 14 : res = "GEXT_PROJ_QTR"
648 : CASE default
649 : CALL cp_abort(__LOCATION__, &
650 : "Unknown interpolation method: "// &
651 13940 : TRIM(ADJUSTL(cp_to_string(method_nr))))
652 : END SELECT
653 13940 : END FUNCTION wfi_get_method_label
654 :
655 : ! **************************************************************************************************
656 : !> \brief calculates the new starting state for the scf for the next
657 : !> wf optimization
658 : !> \param wf_history the previous history needed to extrapolate
659 : !> \param qs_env the qs env with the latest result, and that will contain
660 : !> the new starting state
661 : !> \param dt the time at which to extrapolate (wrt. to the last snapshot)
662 : !> \param extrapolation_method_nr returns the extrapolation method used
663 : !> \param orthogonal_wf ...
664 : !> \par History
665 : !> 02.2003 created [fawzi]
666 : !> 11.2003 Joost VandeVondele : Implemented Nth order PS extrapolation
667 : !> 04.2026 Michele Nottoli : Added GEXT_PROJ and GEXT_PROJ_QTR extrapolations
668 : !> \author fawzi
669 : ! **************************************************************************************************
670 27607 : SUBROUTINE wfi_extrapolate(wf_history, qs_env, dt, extrapolation_method_nr, &
671 : orthogonal_wf)
672 : TYPE(qs_wf_history_type), POINTER :: wf_history
673 : TYPE(qs_environment_type), POINTER :: qs_env
674 : REAL(KIND=dp), INTENT(IN) :: dt
675 : INTEGER, INTENT(OUT), OPTIONAL :: extrapolation_method_nr
676 : LOGICAL, INTENT(OUT), OPTIONAL :: orthogonal_wf
677 :
678 : CHARACTER(len=*), PARAMETER :: routineN = 'wfi_extrapolate'
679 :
680 : INTEGER :: actual_extrapolation_method_nr, handle, &
681 : i, img, io_unit, ispin, k, n, nmo, &
682 : nvec, print_level
683 : LOGICAL :: do_kpoints, my_orthogonal_wf, use_overlap
684 : REAL(KIND=dp) :: alpha, t0, t1, t2
685 27607 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: coeffs
686 27607 : TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER :: ao_mo_fm_pools
687 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct, matrix_struct_new
688 : TYPE(cp_fm_type) :: csc, fm_tmp
689 : TYPE(cp_fm_type), POINTER :: mo_coeff
690 : TYPE(cp_logger_type), POINTER :: logger
691 27607 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, rho_ao, rho_frozen_ao
692 27607 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
693 27607 : TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
694 : TYPE(qs_energy_type), POINTER :: energy
695 : TYPE(qs_rho_type), POINTER :: rho
696 : TYPE(qs_wf_snapshot_type), POINTER :: t0_state, t1_state
697 :
698 27607 : NULLIFY (energy, mos, ao_mo_fm_pools, t0_state, t1_state, mo_coeff, &
699 27607 : rho, rho_ao, rho_frozen_ao)
700 :
701 27607 : use_overlap = wf_history%store_overlap
702 :
703 27607 : CALL timeset(routineN, handle)
704 27607 : logger => cp_get_default_logger()
705 27607 : print_level = logger%iter_info%print_level
706 : io_unit = cp_print_key_unit_nr(logger, qs_env%input, "DFT%SCF%PRINT%PROGRAM_RUN_INFO", &
707 27607 : extension=".scfLog")
708 :
709 27607 : CPASSERT(ASSOCIATED(wf_history))
710 27607 : CPASSERT(wf_history%ref_count > 0)
711 27607 : CPASSERT(ASSOCIATED(qs_env))
712 27607 : CALL get_qs_env(qs_env, energy=energy, mos=mos, rho=rho, do_kpoints=do_kpoints)
713 27607 : CALL mpools_get(qs_env%mpools, ao_mo_fm_pools=ao_mo_fm_pools)
714 : ! chooses the method for this extrapolation
715 27607 : IF (wf_history%snapshot_count < 1) THEN
716 : actual_extrapolation_method_nr = wfi_use_guess_method_nr
717 : ELSE
718 17128 : actual_extrapolation_method_nr = wf_history%interpolation_method_nr
719 : END IF
720 :
721 8 : SELECT CASE (actual_extrapolation_method_nr)
722 : CASE (wfi_linear_wf_method_nr)
723 8 : IF (wf_history%snapshot_count < 2) THEN
724 4 : actual_extrapolation_method_nr = wfi_use_prev_wf_method_nr
725 : END IF
726 : CASE (wfi_linear_p_method_nr)
727 12 : IF (wf_history%snapshot_count < 2) THEN
728 6 : actual_extrapolation_method_nr = wfi_use_prev_wf_method_nr
729 : END IF
730 : CASE (wfi_linear_ps_method_nr)
731 17128 : IF (wf_history%snapshot_count < 2) THEN
732 6 : actual_extrapolation_method_nr = wfi_use_prev_wf_method_nr
733 : END IF
734 : END SELECT
735 :
736 27607 : IF (PRESENT(extrapolation_method_nr)) THEN
737 27607 : extrapolation_method_nr = actual_extrapolation_method_nr
738 : END IF
739 27607 : my_orthogonal_wf = .FALSE.
740 :
741 27607 : IF (.NOT. do_kpoints .AND. wf_history%store_wf .AND. wf_history%snapshot_count > 0) THEN
742 15028 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
743 15028 : IF (ALLOCATED(t1_state%mo_state)) THEN
744 15028 : IF (ASSOCIATED(energy)) THEN
745 15028 : energy%kTS = 0.0_dp
746 15028 : energy%efermi = 0.0_dp
747 : END IF
748 31393 : DO ispin = 1, SIZE(mos)
749 16365 : CALL wfs_restore_mo_state(t1_state%mo_state(ispin), mos(ispin))
750 31393 : IF (ASSOCIATED(energy)) THEN
751 16365 : energy%kTS = energy%kTS + mos(ispin)%kTS
752 16365 : energy%efermi = energy%efermi + mos(ispin)%mu
753 : END IF
754 : END DO
755 15028 : IF (ASSOCIATED(energy)) energy%efermi = energy%efermi/REAL(SIZE(mos), KIND=dp)
756 : END IF
757 : END IF
758 :
759 8 : SELECT CASE (actual_extrapolation_method_nr)
760 : CASE (wfi_frozen_method_nr)
761 8 : CPASSERT(.NOT. do_kpoints)
762 8 : t0_state => wfi_get_snapshot(wf_history, wf_index=1)
763 8 : CPASSERT(ASSOCIATED(t0_state%rho_frozen))
764 :
765 8 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
766 8 : CALL wfi_set_history_variables(qs_env=qs_env, nvec=nvec)
767 :
768 8 : CALL qs_rho_get(t0_state%rho_frozen, rho_ao=rho_frozen_ao)
769 8 : CALL qs_rho_get(rho, rho_ao=rho_ao)
770 16 : DO ispin = 1, SIZE(rho_frozen_ao)
771 : CALL dbcsr_copy(rho_ao(ispin)%matrix, &
772 : rho_frozen_ao(ispin)%matrix, &
773 16 : keep_sparsity=.TRUE.)
774 : END DO
775 : !FM updating rho_ao directly with t0_state%rho_ao would have the
776 : !FM wrong matrix structure
777 8 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
778 8 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
779 :
780 8 : my_orthogonal_wf = .FALSE.
781 : CASE (wfi_use_prev_p_method_nr)
782 476 : t0_state => wfi_get_snapshot(wf_history, wf_index=1)
783 476 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
784 476 : CALL wfi_set_history_variables(qs_env=qs_env, nvec=nvec)
785 476 : CPASSERT(ASSOCIATED(t0_state%rho_ao))
786 476 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
787 1170 : DO ispin = 1, SIZE(t0_state%rho_ao, 1)
788 32282 : DO img = 1, SIZE(t0_state%rho_ao, 2)
789 31806 : IF (img > SIZE(rho_ao_kp, 2)) THEN
790 : CALL cp_warn(__LOCATION__, &
791 18 : "Change in cell neighborlist: might affect quality of initial guess")
792 : ELSE
793 : CALL dbcsr_copy(rho_ao_kp(ispin, img)%matrix, &
794 : t0_state%rho_ao(ispin, img)%matrix, &
795 31094 : keep_sparsity=.TRUE.)
796 : END IF
797 : END DO
798 : END DO
799 : !FM updating rho_ao directly with t0_state%rho_ao would have the
800 : !FM wrong matrix structure
801 476 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
802 476 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
803 : CASE (wfi_use_prev_wf_method_nr)
804 674 : my_orthogonal_wf = .TRUE.
805 674 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
806 674 : CALL wfi_set_history_variables(qs_env=qs_env, nvec=nvec)
807 :
808 674 : IF (do_kpoints) THEN
809 6 : CALL wfi_use_prev_wf_kp(qs_env, io_unit, print_level)
810 : ELSE
811 668 : NULLIFY (t1_state)
812 668 : IF (wf_history%store_wf .AND. wf_history%snapshot_count > 0) THEN
813 10 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
814 10 : CPASSERT(ASSOCIATED(t1_state%wf))
815 : END IF
816 668 : CALL qs_rho_get(rho, rho_ao=rho_ao)
817 1580 : DO ispin = 1, SIZE(mos)
818 912 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nmo=nmo)
819 912 : IF (ASSOCIATED(t1_state)) CALL cp_fm_to_fm(t1_state%wf(ispin), mo_coeff)
820 912 : CALL reorthogonalize_vectors(qs_env, v_matrix=mo_coeff, n_col=nmo)
821 2492 : CALL calculate_density_matrix(mo_set=mos(ispin), density_matrix=rho_ao(ispin)%matrix)
822 : END DO
823 668 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
824 668 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
825 : END IF
826 :
827 : CASE (wfi_use_guess_method_nr)
828 : !FM more clean to do it here, but it
829 : !FM might need to read a file (restart) and thus globenv
830 : !FM I do not want globenv here, thus done by the caller
831 : !FM (btw. it also needs the eigensolver, and unless you relocate it
832 : !FM gives circular dependencies)
833 10625 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
834 10625 : CALL wfi_set_history_variables(qs_env=qs_env, nvec=nvec)
835 : CASE (wfi_linear_wf_method_nr)
836 4 : CPASSERT(.NOT. do_kpoints)
837 4 : t0_state => wfi_get_snapshot(wf_history, wf_index=2)
838 4 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
839 4 : CPASSERT(ASSOCIATED(t0_state))
840 4 : CPASSERT(ASSOCIATED(t1_state))
841 4 : CPASSERT(ASSOCIATED(t0_state%wf))
842 4 : CPASSERT(ASSOCIATED(t1_state%wf))
843 4 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
844 4 : CALL wfi_set_history_variables(qs_env=qs_env, nvec=nvec)
845 :
846 4 : my_orthogonal_wf = .TRUE.
847 4 : t0 = 0.0_dp
848 4 : t1 = t1_state%dt
849 4 : t2 = t1 + dt
850 4 : CALL qs_rho_get(rho, rho_ao=rho_ao)
851 8 : DO ispin = 1, SIZE(mos)
852 : CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, &
853 4 : nmo=nmo)
854 : CALL cp_fm_scale_and_add(alpha=0.0_dp, &
855 : matrix_a=mo_coeff, &
856 : matrix_b=t1_state%wf(ispin), &
857 4 : beta=(t2 - t0)/(t1 - t0))
858 : ! this copy should be unnecessary
859 : CALL cp_fm_scale_and_add(alpha=1.0_dp, &
860 : matrix_a=mo_coeff, &
861 4 : beta=(t1 - t2)/(t1 - t0), matrix_b=t0_state%wf(ispin))
862 : CALL reorthogonalize_vectors(qs_env, &
863 : v_matrix=mo_coeff, &
864 4 : n_col=nmo)
865 : CALL calculate_density_matrix(mo_set=mos(ispin), &
866 12 : density_matrix=rho_ao(ispin)%matrix)
867 : END DO
868 4 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
869 :
870 : CALL qs_ks_did_change(qs_env%ks_env, &
871 4 : rho_changed=.TRUE.)
872 : CASE (wfi_linear_p_method_nr)
873 6 : t0_state => wfi_get_snapshot(wf_history, wf_index=2)
874 6 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
875 6 : CPASSERT(ASSOCIATED(t0_state))
876 6 : CPASSERT(ASSOCIATED(t1_state))
877 6 : CPASSERT(ASSOCIATED(t0_state%rho_ao))
878 6 : CPASSERT(ASSOCIATED(t1_state%rho_ao))
879 6 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
880 6 : CALL wfi_set_history_variables(qs_env=qs_env, nvec=nvec)
881 :
882 6 : t0 = 0.0_dp
883 6 : t1 = t1_state%dt
884 6 : t2 = t1 + dt
885 6 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
886 12 : DO ispin = 1, SIZE(rho_ao_kp, 1)
887 540 : DO img = 1, SIZE(rho_ao_kp, 2)
888 528 : IF (img > SIZE(t0_state%rho_ao, 2) .OR. &
889 6 : img > SIZE(t1_state%rho_ao, 2)) THEN
890 : CALL cp_warn(__LOCATION__, &
891 22 : "Change in cell neighborlist: might affect quality of initial guess")
892 : ELSE
893 : CALL dbcsr_add(rho_ao_kp(ispin, img)%matrix, t1_state%rho_ao(ispin, img)%matrix, &
894 506 : alpha_scalar=0.0_dp, beta_scalar=(t2 - t0)/(t1 - t0)) ! this copy should be unnecessary
895 : CALL dbcsr_add(rho_ao_kp(ispin, img)%matrix, t0_state%rho_ao(ispin, img)%matrix, &
896 506 : alpha_scalar=1.0_dp, beta_scalar=(t1 - t2)/(t1 - t0))
897 : END IF
898 : END DO
899 : END DO
900 6 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
901 6 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
902 : CASE (wfi_linear_ps_method_nr)
903 : ! wf not calculated, extract with PSC renormalized?
904 : ! use wf_linear?
905 12 : CPASSERT(.NOT. do_kpoints)
906 12 : t0_state => wfi_get_snapshot(wf_history, wf_index=2)
907 12 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
908 12 : CPASSERT(ASSOCIATED(t0_state))
909 12 : CPASSERT(ASSOCIATED(t1_state))
910 12 : CPASSERT(ASSOCIATED(t0_state%wf))
911 12 : CPASSERT(ASSOCIATED(t1_state%wf))
912 12 : IF (wf_history%store_overlap) THEN
913 4 : CPASSERT(ASSOCIATED(t0_state%overlap))
914 4 : CPASSERT(ASSOCIATED(t1_state%overlap))
915 : END IF
916 12 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
917 12 : IF (nvec >= wf_history%memory_depth) THEN
918 12 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. (qs_env%scf_control%eps_scf_hist /= 0)) THEN
919 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
920 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
921 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
922 12 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
923 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
924 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
925 12 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
926 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
927 : END IF
928 : END IF
929 :
930 12 : my_orthogonal_wf = .TRUE.
931 : ! use PS_2=2 PS_1-PS_0
932 : ! C_2 comes from using PS_2 as a projector acting on C_1
933 12 : CALL qs_rho_get(rho, rho_ao=rho_ao)
934 24 : DO ispin = 1, SIZE(mos)
935 12 : NULLIFY (mo_coeff, matrix_struct, matrix_struct_new)
936 12 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff)
937 : CALL cp_fm_get_info(mo_coeff, nrow_global=n, ncol_global=k, &
938 12 : matrix_struct=matrix_struct)
939 : CALL cp_fm_struct_create(matrix_struct_new, template_fmstruct=matrix_struct, &
940 12 : nrow_global=k, ncol_global=k)
941 12 : CALL cp_fm_create(csc, matrix_struct_new)
942 12 : CALL cp_fm_struct_release(matrix_struct_new)
943 :
944 12 : IF (use_overlap) THEN
945 4 : CALL cp_dbcsr_sm_fm_multiply(t0_state%overlap, t1_state%wf(ispin), mo_coeff, k)
946 4 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), mo_coeff, 0.0_dp, csc)
947 : ELSE
948 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), &
949 8 : t1_state%wf(ispin), 0.0_dp, csc)
950 : END IF
951 12 : CALL parallel_gemm('N', 'N', n, k, k, 1.0_dp, t0_state%wf(ispin), csc, 0.0_dp, mo_coeff)
952 12 : CALL cp_fm_release(csc)
953 12 : CALL cp_fm_scale_and_add(-1.0_dp, mo_coeff, 2.0_dp, t1_state%wf(ispin))
954 : CALL reorthogonalize_vectors(qs_env, &
955 : v_matrix=mo_coeff, &
956 12 : n_col=k)
957 : CALL calculate_density_matrix(mo_set=mos(ispin), &
958 48 : density_matrix=rho_ao(ispin)%matrix)
959 : END DO
960 12 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
961 12 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
962 :
963 : CASE (wfi_ps_method_nr)
964 : ! figure out the actual number of vectors to use in the extrapolation:
965 308 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
966 308 : CPASSERT(nvec > 0)
967 308 : IF (nvec >= wf_history%memory_depth) THEN
968 120 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. (qs_env%scf_control%eps_scf_hist /= 0)) THEN
969 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
970 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
971 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
972 120 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
973 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
974 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
975 120 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
976 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
977 : END IF
978 : END IF
979 :
980 308 : IF (do_kpoints) THEN
981 4 : CALL wfi_extrapolate_ps_aspc_kp(wf_history, qs_env, nvec, io_unit, print_level)
982 4 : my_orthogonal_wf = .TRUE.
983 : ELSE
984 304 : my_orthogonal_wf = .TRUE.
985 666 : DO ispin = 1, SIZE(mos)
986 362 : NULLIFY (mo_coeff, matrix_struct, matrix_struct_new)
987 362 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff)
988 : CALL cp_fm_get_info(mo_coeff, nrow_global=n, ncol_global=k, &
989 362 : matrix_struct=matrix_struct)
990 362 : CALL cp_fm_create(fm_tmp, matrix_struct)
991 : CALL cp_fm_struct_create(matrix_struct_new, template_fmstruct=matrix_struct, &
992 362 : nrow_global=k, ncol_global=k)
993 362 : CALL cp_fm_create(csc, matrix_struct_new)
994 362 : CALL cp_fm_struct_release(matrix_struct_new)
995 : ! first the most recent
996 362 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
997 362 : CALL cp_fm_to_fm(t1_state%wf(ispin), mo_coeff)
998 362 : alpha = nvec
999 362 : CALL cp_fm_scale(alpha, mo_coeff)
1000 362 : CALL qs_rho_get(rho, rho_ao=rho_ao)
1001 758 : DO i = 2, nvec
1002 396 : t0_state => wfi_get_snapshot(wf_history, wf_index=i)
1003 396 : IF (use_overlap) THEN
1004 358 : CALL cp_dbcsr_sm_fm_multiply(t0_state%overlap, t1_state%wf(ispin), fm_tmp, k)
1005 358 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), fm_tmp, 0.0_dp, csc)
1006 : ELSE
1007 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), &
1008 38 : t1_state%wf(ispin), 0.0_dp, csc)
1009 : END IF
1010 396 : CALL parallel_gemm('N', 'N', n, k, k, 1.0_dp, t0_state%wf(ispin), csc, 0.0_dp, fm_tmp)
1011 396 : alpha = -1.0_dp*alpha*REAL(nvec - i + 1, dp)/REAL(i, dp)
1012 758 : CALL cp_fm_scale_and_add(1.0_dp, mo_coeff, alpha, fm_tmp)
1013 : END DO
1014 :
1015 362 : CALL cp_fm_release(csc)
1016 362 : CALL cp_fm_release(fm_tmp)
1017 : CALL reorthogonalize_vectors(qs_env, &
1018 : v_matrix=mo_coeff, &
1019 362 : n_col=k)
1020 : CALL calculate_density_matrix(mo_set=mos(ispin), &
1021 1390 : density_matrix=rho_ao(ispin)%matrix)
1022 : END DO
1023 304 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1024 304 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
1025 : END IF
1026 :
1027 : CASE (wfi_aspc_nr)
1028 15090 : CALL cite_reference(Kolafa2004)
1029 15090 : CALL cite_reference(Kuhne2007)
1030 : ! figure out the actual number of vectors to use in the extrapolation:
1031 15090 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
1032 15090 : CPASSERT(nvec > 0)
1033 15090 : IF (nvec >= wf_history%memory_depth) THEN
1034 9764 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. &
1035 : (qs_env%scf_control%eps_scf_hist /= 0)) THEN
1036 18 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1037 18 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1038 18 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1039 9746 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
1040 62 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1041 62 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1042 9684 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
1043 8 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1044 : END IF
1045 : END IF
1046 :
1047 15090 : IF (do_kpoints) THEN
1048 552 : CALL wfi_extrapolate_ps_aspc_kp(wf_history, qs_env, nvec, io_unit, print_level)
1049 552 : my_orthogonal_wf = .TRUE.
1050 : ELSE
1051 14538 : my_orthogonal_wf = .TRUE.
1052 14538 : CALL qs_rho_get(rho, rho_ao=rho_ao)
1053 30299 : DO ispin = 1, SIZE(mos)
1054 15761 : NULLIFY (mo_coeff, matrix_struct, matrix_struct_new)
1055 15761 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff)
1056 : CALL cp_fm_get_info(mo_coeff, &
1057 : nrow_global=n, &
1058 : ncol_global=k, &
1059 15761 : matrix_struct=matrix_struct)
1060 15761 : CALL cp_fm_create(fm_tmp, matrix_struct, set_zero=.TRUE.)
1061 : CALL cp_fm_struct_create(matrix_struct_new, &
1062 : template_fmstruct=matrix_struct, &
1063 : nrow_global=k, &
1064 15761 : ncol_global=k)
1065 15761 : CALL cp_fm_create(csc, matrix_struct_new, set_zero=.TRUE.)
1066 15761 : CALL cp_fm_struct_release(matrix_struct_new)
1067 : ! first the most recent
1068 : t1_state => wfi_get_snapshot(wf_history, &
1069 15761 : wf_index=1)
1070 15761 : CALL cp_fm_to_fm(t1_state%wf(ispin), mo_coeff)
1071 15761 : alpha = REAL(4*nvec - 2, KIND=dp)/REAL(nvec + 1, KIND=dp)
1072 15761 : IF ((io_unit > 0) .AND. (print_level > low_print_level)) THEN
1073 : WRITE (UNIT=io_unit, FMT="(/,T2,A,/,/,T3,A,I0,/,/,T3,A2,I0,A4,F10.6)") &
1074 3139 : "Parameters for the always stable predictor-corrector (ASPC) method:", &
1075 3139 : "ASPC order: ", MAX(nvec - 2, 0), &
1076 6278 : "B(", 1, ") = ", alpha
1077 : END IF
1078 15761 : CALL cp_fm_scale(alpha, mo_coeff)
1079 :
1080 61473 : DO i = 2, nvec
1081 45712 : t0_state => wfi_get_snapshot(wf_history, wf_index=i)
1082 45712 : IF (use_overlap) THEN
1083 34172 : CALL cp_dbcsr_sm_fm_multiply(t0_state%overlap, t1_state%wf(ispin), fm_tmp, k)
1084 34172 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), fm_tmp, 0.0_dp, csc)
1085 : ELSE
1086 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), &
1087 11540 : t1_state%wf(ispin), 0.0_dp, csc)
1088 : END IF
1089 45712 : CALL parallel_gemm('N', 'N', n, k, k, 1.0_dp, t0_state%wf(ispin), csc, 0.0_dp, fm_tmp)
1090 : alpha = (-1.0_dp)**(i + 1)*REAL(i, KIND=dp)* &
1091 45712 : binomial(2*nvec, nvec - i)/binomial(2*nvec - 2, nvec - 1)
1092 45712 : IF ((io_unit > 0) .AND. (print_level > low_print_level)) THEN
1093 : WRITE (UNIT=io_unit, FMT="(T3,A2,I0,A4,F10.6)") &
1094 9419 : "B(", i, ") = ", alpha
1095 : END IF
1096 61473 : CALL cp_fm_scale_and_add(1.0_dp, mo_coeff, alpha, fm_tmp)
1097 : END DO
1098 15761 : CALL cp_fm_release(csc)
1099 15761 : CALL cp_fm_release(fm_tmp)
1100 : CALL reorthogonalize_vectors(qs_env, &
1101 : v_matrix=mo_coeff, &
1102 15761 : n_col=k)
1103 : CALL calculate_density_matrix(mo_set=mos(ispin), &
1104 46060 : density_matrix=rho_ao(ispin)%matrix)
1105 : END DO
1106 14538 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1107 14538 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
1108 : END IF ! do_kpoints
1109 :
1110 : CASE (wfi_gext_proj_nr)
1111 376 : IF (do_kpoints) THEN
1112 240 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
1113 240 : CPASSERT(nvec > 0)
1114 240 : CALL wfi_extrapolate_gext_proj_kp(wf_history, qs_env, nvec, io_unit, print_level)
1115 240 : my_orthogonal_wf = .TRUE.
1116 : ELSE
1117 :
1118 : ! figure out the actual number of vectors to use in the extrapolation:
1119 136 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
1120 136 : IF (nvec >= wf_history%memory_depth) THEN
1121 88 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. &
1122 : (qs_env%scf_control%eps_scf_hist /= 0)) THEN
1123 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1124 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1125 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1126 88 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
1127 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1128 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1129 88 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
1130 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1131 : END IF
1132 : END IF
1133 136 : CPASSERT(nvec > 0)
1134 :
1135 : ! get the coefficients for the fitting
1136 408 : ALLOCATE (coeffs(nvec))
1137 136 : NULLIFY (matrix_s)
1138 136 : CALL get_qs_env(qs_env, matrix_s=matrix_s)
1139 : CALL diff_fitting(wf_history, matrix_s(1)%matrix, coeffs, nvec, &
1140 136 : 1e-4_dp, io_unit, print_level)
1141 :
1142 136 : my_orthogonal_wf = .TRUE.
1143 136 : CALL qs_rho_get(rho, rho_ao=rho_ao)
1144 328 : DO ispin = 1, SIZE(mos)
1145 192 : NULLIFY (mo_coeff, matrix_struct, matrix_struct_new)
1146 192 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff)
1147 : CALL cp_fm_get_info(mo_coeff, &
1148 : nrow_global=n, &
1149 : ncol_global=k, &
1150 192 : matrix_struct=matrix_struct)
1151 192 : CALL cp_fm_create(fm_tmp, matrix_struct)
1152 : CALL cp_fm_struct_create(matrix_struct_new, &
1153 : template_fmstruct=matrix_struct, &
1154 : nrow_global=k, &
1155 192 : ncol_global=k)
1156 192 : CALL cp_fm_create(csc, matrix_struct_new)
1157 192 : CALL cp_fm_struct_release(matrix_struct_new)
1158 :
1159 192 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
1160 :
1161 : ! do the linear combination of previous PSs
1162 192 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
1163 704 : DO i = 1, nvec
1164 512 : t0_state => wfi_get_snapshot(wf_history, wf_index=i)
1165 512 : CALL cp_dbcsr_sm_fm_multiply(t0_state%overlap, t1_state%wf(ispin), fm_tmp, k)
1166 512 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), fm_tmp, 0.0_dp, csc)
1167 512 : CALL parallel_gemm('N', 'N', n, k, k, 1.0_dp, t0_state%wf(ispin), csc, 0.0_dp, fm_tmp)
1168 704 : CALL cp_fm_scale_and_add(1.0_dp, mo_coeff, coeffs(i), fm_tmp)
1169 : END DO
1170 192 : CALL cp_fm_release(csc)
1171 192 : CALL cp_fm_release(fm_tmp)
1172 : CALL reorthogonalize_vectors(qs_env, &
1173 : v_matrix=mo_coeff, &
1174 192 : n_col=k)
1175 : CALL calculate_density_matrix(mo_set=mos(ispin), &
1176 712 : density_matrix=rho_ao(ispin)%matrix)
1177 : END DO
1178 136 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1179 136 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
1180 :
1181 136 : DEALLOCATE (coeffs)
1182 :
1183 : END IF
1184 :
1185 : CASE (wfi_gext_proj_qtr_nr)
1186 28 : IF (do_kpoints) THEN
1187 4 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
1188 4 : CPASSERT(nvec > 0)
1189 4 : CALL wfi_extrapolate_gext_proj_kp(wf_history, qs_env, nvec, io_unit, print_level)
1190 4 : my_orthogonal_wf = .TRUE.
1191 : ELSE
1192 :
1193 : ! figure out the actual number of vectors to use in the extrapolation:
1194 24 : nvec = MIN(wf_history%memory_depth, wf_history%snapshot_count)
1195 24 : IF (nvec >= wf_history%memory_depth) THEN
1196 8 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. &
1197 : (qs_env%scf_control%eps_scf_hist /= 0)) THEN
1198 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1199 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1200 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1201 8 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
1202 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1203 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1204 8 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
1205 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1206 : END IF
1207 : END IF
1208 24 : CPASSERT(nvec > 0)
1209 :
1210 : ! get the coefficients for the fitting
1211 72 : ALLOCATE (coeffs(nvec))
1212 24 : NULLIFY (matrix_s)
1213 24 : CALL get_qs_env(qs_env, matrix_s=matrix_s)
1214 : CALL tr_fitting(wf_history, matrix_s(1)%matrix, coeffs, nvec, &
1215 24 : 1e-4_dp, io_unit, print_level)
1216 :
1217 24 : my_orthogonal_wf = .TRUE.
1218 24 : CALL qs_rho_get(rho, rho_ao=rho_ao)
1219 48 : DO ispin = 1, SIZE(mos)
1220 24 : NULLIFY (mo_coeff, matrix_struct, matrix_struct_new)
1221 24 : CALL get_mo_set(mo_set=mos(ispin), mo_coeff=mo_coeff)
1222 : CALL cp_fm_get_info(mo_coeff, &
1223 : nrow_global=n, &
1224 : ncol_global=k, &
1225 24 : matrix_struct=matrix_struct)
1226 24 : CALL cp_fm_create(fm_tmp, matrix_struct)
1227 : CALL cp_fm_struct_create(matrix_struct_new, &
1228 : template_fmstruct=matrix_struct, &
1229 : nrow_global=k, &
1230 24 : ncol_global=k)
1231 24 : CALL cp_fm_create(csc, matrix_struct_new)
1232 24 : CALL cp_fm_struct_release(matrix_struct_new)
1233 :
1234 24 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
1235 :
1236 : ! do the linear combination of previous PSs
1237 24 : CALL cp_fm_set_all(mo_coeff, 0.0_dp)
1238 104 : DO i = 1, nvec
1239 80 : t0_state => wfi_get_snapshot(wf_history, wf_index=i)
1240 80 : CALL cp_dbcsr_sm_fm_multiply(t0_state%overlap, t1_state%wf(ispin), fm_tmp, k)
1241 80 : CALL parallel_gemm('T', 'N', k, k, n, 1.0_dp, t0_state%wf(ispin), fm_tmp, 0.0_dp, csc)
1242 80 : CALL parallel_gemm('N', 'N', n, k, k, 1.0_dp, t0_state%wf(ispin), csc, 0.0_dp, fm_tmp)
1243 104 : CALL cp_fm_scale_and_add(1.0_dp, mo_coeff, coeffs(i), fm_tmp)
1244 : END DO
1245 24 : CALL cp_fm_release(csc)
1246 24 : CALL cp_fm_release(fm_tmp)
1247 : CALL reorthogonalize_vectors(qs_env, &
1248 : v_matrix=mo_coeff, &
1249 24 : n_col=k)
1250 : CALL calculate_density_matrix(mo_set=mos(ispin), &
1251 96 : density_matrix=rho_ao(ispin)%matrix)
1252 : END DO
1253 24 : CALL qs_rho_update_rho(rho, qs_env=qs_env)
1254 24 : CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
1255 :
1256 24 : DEALLOCATE (coeffs)
1257 :
1258 : END IF
1259 :
1260 : CASE default
1261 : CALL cp_abort(__LOCATION__, &
1262 : "Unknown interpolation method: "// &
1263 27607 : TRIM(ADJUSTL(cp_to_string(wf_history%interpolation_method_nr))))
1264 : END SELECT
1265 27607 : IF (PRESENT(orthogonal_wf)) orthogonal_wf = my_orthogonal_wf
1266 : CALL cp_print_key_finished_output(io_unit, logger, qs_env%input, &
1267 27607 : "DFT%SCF%PRINT%PROGRAM_RUN_INFO")
1268 27607 : CALL timestop(handle)
1269 27607 : END SUBROUTINE wfi_extrapolate
1270 :
1271 : ! **************************************************************************************************
1272 : !> \brief Reorthogonalizes the wavefunctions from the previous step for k-points
1273 : !> using the current S(k) metric and rebuilds the density matrix.
1274 : !> \param qs_env The QS environment
1275 : !> \param io_unit output unit
1276 : !> \param print_level print level
1277 : !> \param pbc_shift_ref ...
1278 : !> \param load_snapshot_wf ...
1279 : ! **************************************************************************************************
1280 806 : SUBROUTINE wfi_use_prev_wf_kp(qs_env, io_unit, print_level, pbc_shift_ref, load_snapshot_wf)
1281 : TYPE(qs_environment_type), POINTER :: qs_env
1282 : INTEGER, INTENT(IN) :: io_unit, print_level
1283 : INTEGER, DIMENSION(:, :), INTENT(IN), OPTIONAL :: pbc_shift_ref
1284 : LOGICAL, INTENT(IN), OPTIONAL :: load_snapshot_wf
1285 :
1286 : CHARACTER(len=*), PARAMETER :: routineN = 'wfi_use_prev_wf_kp'
1287 :
1288 806 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:) :: col_scaling
1289 : INTEGER :: chol_info, handle, ik, ikp, ispin, j, &
1290 : kplocal, nao, nmo, nspin
1291 806 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: pbc_shift_cur, pbc_shift_src
1292 : INTEGER, DIMENSION(2) :: kp_range
1293 : LOGICAL :: reload_snapshot_wf, use_pbc_phase_ref, &
1294 : use_real_wfn
1295 : REAL(KIND=dp) :: eval_thresh
1296 806 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues
1297 806 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
1298 : TYPE(cp_cfm_type) :: cfm_evecs, cfm_mhalf, cfm_nao_nmo_work, &
1299 : cmos_new, csc_cfm
1300 806 : TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:) :: csmat_cur
1301 : TYPE(cp_cfm_type), POINTER :: mo_coeff
1302 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool_kp
1303 : TYPE(cp_fm_struct_type), POINTER :: ao_ao_struct, nmo_nmo_struct
1304 806 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp
1305 : TYPE(dft_control_type), POINTER :: dft_control
1306 : TYPE(kpoint_env_type), POINTER :: kp
1307 : TYPE(kpoint_type), POINTER :: kpoints
1308 : TYPE(qs_matrix_pools_type), POINTER :: mpools_kp
1309 : TYPE(qs_scf_env_type), POINTER :: scf_env
1310 : TYPE(qs_wf_history_type), POINTER :: wf_history
1311 : TYPE(qs_wf_snapshot_type), POINTER :: t1_state
1312 : TYPE(scf_control_type), POINTER :: scf_control
1313 :
1314 806 : CALL timeset(routineN, handle)
1315 :
1316 806 : NULLIFY (dft_control, scf_control, kpoints, matrix_s_kp, scf_env, kp, &
1317 806 : mo_coeff, wf_history, t1_state)
1318 :
1319 : CALL get_qs_env(qs_env, kpoints=kpoints, matrix_s_kp=matrix_s_kp, scf_env=scf_env, &
1320 806 : dft_control=dft_control, scf_control=scf_control)
1321 806 : CALL get_kpoint_info(kpoints, xkp=xkp, use_real_wfn=use_real_wfn, kp_range=kp_range)
1322 806 : kplocal = kp_range(2) - kp_range(1) + 1
1323 :
1324 806 : IF (use_real_wfn) THEN
1325 0 : CALL timestop(handle)
1326 0 : RETURN
1327 : END IF
1328 :
1329 806 : wf_history => qs_env%wf_history
1330 806 : reload_snapshot_wf = .FALSE.
1331 806 : IF (PRESENT(load_snapshot_wf)) reload_snapshot_wf = load_snapshot_wf
1332 806 : NULLIFY (t1_state)
1333 806 : IF (ASSOCIATED(wf_history)) THEN
1334 806 : IF (wf_history%store_wf_kp .AND. wf_history%snapshot_count > 0) THEN
1335 804 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
1336 : END IF
1337 : END IF
1338 806 : IF (PRESENT(pbc_shift_ref)) THEN
1339 2400 : ALLOCATE (pbc_shift_src(3, SIZE(pbc_shift_ref, 2)))
1340 17920 : pbc_shift_src(:, :) = pbc_shift_ref(:, :)
1341 804 : use_pbc_phase_ref = .TRUE.
1342 : ELSE
1343 6 : use_pbc_phase_ref = .FALSE.
1344 6 : IF (ASSOCIATED(t1_state)) THEN
1345 4 : CPASSERT(ASSOCIATED(t1_state%wf_kp_cfm))
1346 4 : CPASSERT(ASSOCIATED(t1_state%kp_pbc_shift))
1347 4 : reload_snapshot_wf = .TRUE.
1348 12 : ALLOCATE (pbc_shift_src(3, SIZE(t1_state%kp_pbc_shift, 2)))
1349 132 : pbc_shift_src(:, :) = t1_state%kp_pbc_shift(:, :)
1350 : use_pbc_phase_ref = .TRUE.
1351 : END IF
1352 : END IF
1353 804 : IF (use_pbc_phase_ref) CALL wfi_compute_kp_pbc_shift(qs_env, pbc_shift_cur)
1354 :
1355 806 : kp => kpoints%kp_env(1)%kpoint_env
1356 806 : nspin = SIZE(kp%mos)
1357 806 : CALL get_mo_set(kp%mos(1), nao=nao, nmo=nmo, cmo_coeff=mo_coeff)
1358 :
1359 806 : IF ((io_unit > 0) .AND. (print_level > low_print_level)) THEN
1360 : WRITE (UNIT=io_unit, FMT="(/,T2,A)") &
1361 0 : "Using previous wavefunctions as initial guess for k-points (with reorthogonalization)"
1362 : END IF
1363 :
1364 806 : CALL get_kpoint_info(kpoints, mpools=mpools_kp)
1365 806 : CALL mpools_get(mpools_kp, ao_ao_fm_pool=ao_ao_fm_pool_kp)
1366 806 : ao_ao_struct => fm_pool_get_el_struct(ao_ao_fm_pool_kp)
1367 :
1368 806 : CALL cp_cfm_create(cmos_new, mo_coeff%matrix_struct)
1369 806 : CALL cp_cfm_create(cfm_nao_nmo_work, mo_coeff%matrix_struct)
1370 :
1371 806 : NULLIFY (nmo_nmo_struct)
1372 : CALL cp_fm_struct_create(nmo_nmo_struct, template_fmstruct=mo_coeff%matrix_struct, &
1373 806 : nrow_global=nmo, ncol_global=nmo)
1374 806 : CALL cp_cfm_create(csc_cfm, nmo_nmo_struct)
1375 806 : CALL cp_fm_struct_release(nmo_nmo_struct)
1376 :
1377 4274 : ALLOCATE (csmat_cur(kplocal))
1378 2662 : DO ikp = 1, kplocal
1379 2662 : CALL cp_cfm_create(csmat_cur(ikp), ao_ao_struct)
1380 : END DO
1381 :
1382 : CALL kpoint_operator_get_all(kpoints, scf_env%scf_work1(1), matrix_s_kp, kpoint_slot_s, &
1383 806 : csmat_cur, matrix_row=kpoint_spin_free)
1384 :
1385 : ! Phase B: bring the WFN from its saved/internal PBC image convention to
1386 : ! the current convention, then orthogonalize it with respect to S(k).
1387 2418 : ALLOCATE (eigenvalues(nmo))
1388 806 : eval_thresh = 1.0E-12_dp
1389 :
1390 2662 : DO ikp = 1, kplocal
1391 1856 : kp => kpoints%kp_env(ikp)%kpoint_env
1392 4152 : DO ispin = 1, nspin
1393 2296 : IF (reload_snapshot_wf) THEN
1394 16 : CALL cp_cfm_to_cfm(t1_state%wf_kp_cfm(ikp, ispin), cmos_new)
1395 : ELSE
1396 2280 : CALL cp_cfm_to_cfm(kp%mos(ispin)%cmo_coeff, cmos_new)
1397 : END IF
1398 2296 : IF (use_pbc_phase_ref) THEN
1399 2288 : ik = kp_range(1) + ikp - 1
1400 : CALL wfi_apply_kp_pbc_phase_cfm(cmos_new, pbc_shift_cur - pbc_shift_src, &
1401 42872 : xkp(1:3, ik), matrix_s_kp(1, 1)%matrix)
1402 : END IF
1403 :
1404 : CALL parallel_gemm('N', 'N', nao, nmo, nao, z_one, &
1405 2296 : csmat_cur(ikp), cmos_new, z_zero, cfm_nao_nmo_work)
1406 : CALL parallel_gemm('C', 'N', nmo, nmo, nao, z_one, &
1407 2296 : cmos_new, cfm_nao_nmo_work, z_zero, csc_cfm)
1408 :
1409 2296 : CALL cp_cfm_cholesky_decompose(csc_cfm, info_out=chol_info)
1410 2296 : IF (chol_info == 0) THEN
1411 2292 : CALL cp_cfm_triangular_multiply(csc_cfm, cmos_new, side='R', invert_tr=.TRUE., uplo_tr='U')
1412 : ELSE
1413 4 : CALL parallel_gemm('C', 'N', nmo, nmo, nao, z_one, cmos_new, cfm_nao_nmo_work, z_zero, csc_cfm)
1414 4 : CALL cp_cfm_create(cfm_evecs, csc_cfm%matrix_struct)
1415 4 : CALL cp_cfm_create(cfm_mhalf, csc_cfm%matrix_struct)
1416 4 : CALL cp_cfm_heevd(csc_cfm, cfm_evecs, eigenvalues)
1417 4 : CALL cp_cfm_to_cfm(cfm_evecs, cfm_mhalf)
1418 12 : ALLOCATE (col_scaling(nmo))
1419 12 : DO j = 1, nmo
1420 12 : IF (eigenvalues(j) > eval_thresh) THEN
1421 4 : col_scaling(j) = CMPLX(1.0_dp/SQRT(eigenvalues(j)), 0.0_dp, KIND=dp)
1422 : ELSE
1423 4 : col_scaling(j) = z_zero
1424 : END IF
1425 : END DO
1426 4 : CALL cp_cfm_column_scale(cfm_mhalf, col_scaling)
1427 4 : DEALLOCATE (col_scaling)
1428 4 : CALL parallel_gemm('N', 'C', nmo, nmo, nmo, z_one, cfm_mhalf, cfm_evecs, z_zero, csc_cfm)
1429 4 : CALL parallel_gemm('N', 'N', nao, nmo, nmo, z_one, cmos_new, csc_cfm, z_zero, cfm_nao_nmo_work)
1430 4 : CALL cp_cfm_to_cfm(cfm_nao_nmo_work, cmos_new)
1431 4 : CALL cp_cfm_release(cfm_evecs)
1432 8 : CALL cp_cfm_release(cfm_mhalf)
1433 : END IF
1434 6448 : CALL cp_cfm_to_cfm(cmos_new, kp%mos(ispin)%cmo_coeff)
1435 : END DO
1436 : ! the MOS now hold extrapolated coefficients that are orthonormal under
1437 : ! the S(k) of the current geometry: a valid trial subspace for solvers.
1438 : ! All WFN-based k-point extrapolation methods converge here (PS/ASPC
1439 : ! and GEXT_PROJ finish through this routine). The flag is set once
1440 : ! at their common write-back.
1441 2662 : kp%mos_prefilled = .TRUE.
1442 : END DO
1443 806 : DEALLOCATE (eigenvalues)
1444 :
1445 : ! Phase C: Images sharing one kp%mos need the current image's own spectrum for smearing.
1446 : ! Fixed-occupation OT must preserve its occupied rank after extrapolation; its energy
1447 : ! labels and any padded UKS columns are not a spectrum for refilling the bands.
1448 806 : IF (ASSOCIATED(t1_state)) THEN
1449 804 : IF (ALLOCATED(t1_state%mo_state_kp)) THEN
1450 804 : CPASSERT(SIZE(t1_state%mo_state_kp, 1) == kplocal)
1451 804 : CPASSERT(SIZE(t1_state%mo_state_kp, 2) == nspin)
1452 2652 : DO ikp = 1, kplocal
1453 1848 : kp => kpoints%kp_env(ikp)%kpoint_env
1454 4940 : DO ispin = 1, nspin
1455 4136 : CALL wfs_restore_mo_state(t1_state%mo_state_kp(ikp, ispin), kp%mos(ispin))
1456 : END DO
1457 : END DO
1458 : END IF
1459 : END IF
1460 : CALL qs_kpoint_state_commit(qs_env, update_occupations=.TRUE., &
1461 : fixed_occupations=scf_env%method == ot_method_nr .AND. &
1462 1488 : .NOT. (dft_control%smear .OR. scf_control%smear%do_smear))
1463 :
1464 : ! Cleanup
1465 2662 : DO ikp = 1, kplocal
1466 2662 : CALL cp_cfm_release(csmat_cur(ikp))
1467 : END DO
1468 806 : DEALLOCATE (csmat_cur)
1469 806 : CALL cp_cfm_release(cmos_new)
1470 806 : CALL cp_cfm_release(cfm_nao_nmo_work)
1471 806 : CALL cp_cfm_release(csc_cfm)
1472 806 : IF (ALLOCATED(pbc_shift_cur)) DEALLOCATE (pbc_shift_cur)
1473 806 : IF (ALLOCATED(pbc_shift_src)) DEALLOCATE (pbc_shift_src)
1474 :
1475 806 : CALL timestop(handle)
1476 4030 : END SUBROUTINE wfi_use_prev_wf_kp
1477 :
1478 : ! **************************************************************************************************
1479 : !> \brief Stores the internal PBC image shift used for k-point neighbor-list construction.
1480 : !> shift = scaled(pbc(r))-scaled(r), i.e. the integer image displacement caused by pbc().
1481 : !> \param snapshot ...
1482 : !> \param cell ...
1483 : !> \param particle_set ...
1484 : ! **************************************************************************************************
1485 3018 : SUBROUTINE wfi_store_kp_pbc_shift(snapshot, cell, particle_set)
1486 : TYPE(qs_wf_snapshot_type), POINTER :: snapshot
1487 : TYPE(cell_type), POINTER :: cell
1488 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1489 :
1490 : INTEGER :: iatom, natom
1491 : REAL(KIND=dp), DIMENSION(3) :: frac_pbc, frac_raw, r_pbc
1492 :
1493 3018 : CPASSERT(ASSOCIATED(snapshot))
1494 3018 : CPASSERT(ASSOCIATED(cell))
1495 3018 : CPASSERT(ASSOCIATED(particle_set))
1496 :
1497 3018 : natom = SIZE(particle_set)
1498 3018 : IF (ASSOCIATED(snapshot%kp_pbc_shift)) THEN
1499 590 : DEALLOCATE (snapshot%kp_pbc_shift)
1500 : END IF
1501 9054 : ALLOCATE (snapshot%kp_pbc_shift(3, natom))
1502 15420 : DO iatom = 1, natom
1503 12402 : r_pbc(1:3) = pbc(particle_set(iatom)%r(1:3), cell)
1504 12402 : CALL real_to_scaled(frac_raw, particle_set(iatom)%r(1:3), cell)
1505 12402 : CALL real_to_scaled(frac_pbc, r_pbc(1:3), cell)
1506 52626 : snapshot%kp_pbc_shift(1:3, iatom) = NINT(frac_pbc(1:3) - frac_raw(1:3))
1507 : END DO
1508 3018 : END SUBROUTINE wfi_store_kp_pbc_shift
1509 :
1510 : ! **************************************************************************************************
1511 : !> \brief Computes the current internal PBC image shift used by pbc().
1512 : !> \param qs_env ...
1513 : !> \param pbc_shift ...
1514 : ! **************************************************************************************************
1515 804 : SUBROUTINE wfi_compute_kp_pbc_shift(qs_env, pbc_shift)
1516 : TYPE(qs_environment_type), POINTER :: qs_env
1517 : INTEGER, ALLOCATABLE, DIMENSION(:, :), INTENT(OUT) :: pbc_shift
1518 :
1519 : INTEGER :: iatom, natom
1520 : REAL(KIND=dp), DIMENSION(3) :: frac_pbc, frac_raw, r_pbc
1521 : TYPE(cell_type), POINTER :: cell
1522 804 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
1523 :
1524 804 : NULLIFY (cell, particle_set)
1525 804 : CALL get_qs_env(qs_env, cell=cell, particle_set=particle_set)
1526 804 : CPASSERT(ASSOCIATED(cell))
1527 804 : CPASSERT(ASSOCIATED(particle_set))
1528 :
1529 804 : natom = SIZE(particle_set)
1530 2412 : ALLOCATE (pbc_shift(3, natom))
1531 5116 : DO iatom = 1, natom
1532 4312 : r_pbc(1:3) = pbc(particle_set(iatom)%r(1:3), cell)
1533 4312 : CALL real_to_scaled(frac_raw, particle_set(iatom)%r(1:3), cell)
1534 4312 : CALL real_to_scaled(frac_pbc, r_pbc(1:3), cell)
1535 18052 : pbc_shift(1:3, iatom) = NINT(frac_pbc(1:3) - frac_raw(1:3))
1536 : END DO
1537 804 : END SUBROUTINE wfi_compute_kp_pbc_shift
1538 :
1539 : ! **************************************************************************************************
1540 : !> \brief Applies the atom-wise Bloch phase associated with a change of the internal
1541 : !> k-point PBC image convention to a complex MO coefficient matrix.
1542 : !> \param cmos complex MO coefficients
1543 : !> \param pbc_shift_delta target shift minus source shift for each atom
1544 : !> \param xk fractional k-point coordinates
1545 : !> \param matrix_template AO block structure used to map rows to atoms
1546 : ! **************************************************************************************************
1547 13462 : SUBROUTINE wfi_apply_kp_pbc_phase_cfm(cmos, pbc_shift_delta, xk, matrix_template)
1548 : TYPE(cp_cfm_type), INTENT(INOUT) :: cmos
1549 : INTEGER, DIMENSION(:, :), INTENT(IN) :: pbc_shift_delta
1550 : REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: xk
1551 : TYPE(dbcsr_type), POINTER :: matrix_template
1552 :
1553 : COMPLEX(KIND=dp) :: phase
1554 13462 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: zblock
1555 : INTEGER :: iatom, natom, nmo, nrow, row_start
1556 13462 : INTEGER, DIMENSION(:), POINTER :: row_blk_size
1557 : REAL(KIND=dp) :: theta
1558 :
1559 0 : CPASSERT(ASSOCIATED(matrix_template))
1560 :
1561 13462 : natom = SIZE(pbc_shift_delta, 2)
1562 13462 : CALL cp_cfm_get_info(cmos, ncol_global=nmo)
1563 13462 : NULLIFY (row_blk_size)
1564 13462 : CALL dbcsr_get_info(matrix_template, row_blk_size=row_blk_size)
1565 13462 : CPASSERT(SIZE(row_blk_size) >= natom)
1566 :
1567 13462 : row_start = 1
1568 80108 : DO iatom = 1, natom
1569 66646 : nrow = row_blk_size(iatom)
1570 265136 : IF (ANY(pbc_shift_delta(1:3, iatom) /= 0)) THEN
1571 3152 : theta = twopi*SUM(xk(1:3)*REAL(pbc_shift_delta(1:3, iatom), KIND=dp))
1572 788 : phase = CMPLX(COS(theta), SIN(theta), KIND=dp)
1573 3152 : ALLOCATE (zblock(nrow, nmo))
1574 788 : CALL cp_cfm_get_submatrix(cmos, zblock, row_start, 1, nrow, nmo)
1575 278972 : zblock = phase*zblock
1576 788 : CALL cp_cfm_set_submatrix(cmos, zblock, row_start, 1, nrow, nmo)
1577 788 : DEALLOCATE (zblock)
1578 : END IF
1579 80108 : row_start = row_start + nrow
1580 : END DO
1581 26924 : END SUBROUTINE wfi_apply_kp_pbc_phase_cfm
1582 :
1583 : ! **************************************************************************************************
1584 : !> \brief Performs PS/ASPC wavefunction extrapolation for k-point calculations.
1585 : !> Applies PS/ASPC coefficients to complex MO coefficients at each k-point,
1586 : !> with subspace alignment via historical overlap matrices.
1587 : !> Delegates final orthogonalization and density building to wfi_use_prev_wf_kp.
1588 : !> \param wf_history wavefunction history buffer
1589 : !> \param qs_env QS environment
1590 : !> \param nvec number of history snapshots to use
1591 : !> \param io_unit output unit for logging
1592 : !> \param print_level current print level
1593 : ! **************************************************************************************************
1594 1112 : SUBROUTINE wfi_extrapolate_ps_aspc_kp(wf_history, qs_env, nvec, io_unit, print_level)
1595 : TYPE(qs_wf_history_type), POINTER :: wf_history
1596 : TYPE(qs_environment_type), POINTER :: qs_env
1597 : INTEGER, INTENT(IN) :: nvec, io_unit, print_level
1598 :
1599 : CHARACTER(len=*), PARAMETER :: routineN = 'wfi_extrapolate_ps_aspc_kp'
1600 :
1601 : INTEGER :: handle, i, ik, ikp, ispin, kplocal, &
1602 : method_nr, nao, nmo, nspin
1603 : INTEGER, DIMENSION(2) :: kp_range
1604 : LOGICAL :: use_real_wfn
1605 : REAL(KIND=dp) :: alpha_coeff
1606 556 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
1607 : TYPE(cp_cfm_type) :: cfm_nao_nmo_work, cmos_1, cmos_i, &
1608 : cmos_new, csc_cfm
1609 : TYPE(cp_cfm_type), POINTER :: mo_coeff
1610 : TYPE(cp_fm_struct_type), POINTER :: nmo_nmo_struct
1611 556 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp
1612 : TYPE(kpoint_env_type), POINTER :: kp
1613 : TYPE(kpoint_type), POINTER :: kpoints
1614 : TYPE(qs_wf_snapshot_type), POINTER :: t0_state, t1_state
1615 :
1616 556 : method_nr = wf_history%interpolation_method_nr
1617 :
1618 556 : CALL timeset(routineN, handle)
1619 556 : NULLIFY (kpoints, kp, mo_coeff, t0_state, t1_state, nmo_nmo_struct, &
1620 556 : matrix_s_kp, xkp)
1621 :
1622 556 : CALL get_qs_env(qs_env, kpoints=kpoints, matrix_s_kp=matrix_s_kp)
1623 556 : CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn, kp_range=kp_range, xkp=xkp)
1624 556 : kplocal = kp_range(2) - kp_range(1) + 1
1625 :
1626 556 : IF (use_real_wfn) THEN
1627 0 : IF (method_nr == wfi_aspc_nr) THEN
1628 : CALL cp_warn(__LOCATION__, "ASPC with k-points requires complex wavefunctions; "// &
1629 0 : "falling back to USE_PREV_WF.")
1630 : ELSE
1631 : CALL cp_warn(__LOCATION__, "PS with k-points requires complex wavefunctions; "// &
1632 0 : "falling back to USE_PREV_WF.")
1633 : END IF
1634 0 : CALL wfi_use_prev_wf_kp(qs_env, io_unit, print_level)
1635 0 : CALL timestop(handle)
1636 0 : RETURN
1637 : END IF
1638 :
1639 556 : kp => kpoints%kp_env(1)%kpoint_env
1640 556 : nspin = SIZE(kp%mos)
1641 556 : CALL get_mo_set(kp%mos(1), nao=nao, nmo=nmo, cmo_coeff=mo_coeff)
1642 :
1643 556 : IF (method_nr == wfi_aspc_nr) THEN
1644 552 : IF ((io_unit > 0) .AND. (print_level > low_print_level)) THEN
1645 : WRITE (UNIT=io_unit, FMT="(/,T2,A,/,T3,A,I0)") &
1646 40 : "Parameters for the always stable predictor-corrector (ASPC) method:", &
1647 80 : "ASPC order: ", MAX(nvec - 2, 0)
1648 : END IF
1649 : END IF
1650 :
1651 16 : IF (method_nr == wfi_aspc_nr) THEN
1652 552 : CALL cp_cfm_create(cmos_new, mo_coeff%matrix_struct, set_zero=.TRUE.)
1653 552 : CALL cp_cfm_create(cmos_1, mo_coeff%matrix_struct, set_zero=.TRUE.)
1654 552 : CALL cp_cfm_create(cmos_i, mo_coeff%matrix_struct, set_zero=.TRUE.)
1655 552 : CALL cp_cfm_create(cfm_nao_nmo_work, mo_coeff%matrix_struct, set_zero=.TRUE.)
1656 : ELSE
1657 4 : CALL cp_cfm_create(cmos_new, mo_coeff%matrix_struct)
1658 4 : CALL cp_cfm_create(cmos_1, mo_coeff%matrix_struct)
1659 4 : CALL cp_cfm_create(cmos_i, mo_coeff%matrix_struct)
1660 4 : CALL cp_cfm_create(cfm_nao_nmo_work, mo_coeff%matrix_struct)
1661 : END IF
1662 :
1663 : CALL cp_fm_struct_create(nmo_nmo_struct, template_fmstruct=mo_coeff%matrix_struct, &
1664 556 : nrow_global=nmo, ncol_global=nmo)
1665 556 : IF (method_nr == wfi_aspc_nr) THEN
1666 552 : CALL cp_cfm_create(csc_cfm, nmo_nmo_struct, set_zero=.TRUE.)
1667 : ELSE
1668 4 : CALL cp_cfm_create(csc_cfm, nmo_nmo_struct)
1669 : END IF
1670 556 : CALL cp_fm_struct_release(nmo_nmo_struct)
1671 :
1672 : ! Phase 1: Initialize C_new(k) = B(1) * C_1(k)
1673 556 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
1674 556 : IF (method_nr == wfi_aspc_nr) THEN
1675 552 : alpha_coeff = REAL(4*nvec - 2, KIND=dp)/REAL(nvec + 1, KIND=dp)
1676 552 : IF ((io_unit > 0) .AND. (print_level > low_print_level)) THEN
1677 40 : WRITE (UNIT=io_unit, FMT="(T3,A2,I0,A4,F10.6)") "B(", 1, ") = ", alpha_coeff
1678 : END IF
1679 : ELSE
1680 4 : alpha_coeff = nvec
1681 : END IF
1682 :
1683 1890 : DO ikp = 1, kplocal
1684 1334 : kp => kpoints%kp_env(ikp)%kpoint_env
1685 3420 : DO ispin = 1, nspin
1686 1530 : CALL cp_cfm_to_cfm(t1_state%wf_kp_cfm(ikp, ispin), cmos_new)
1687 1530 : CALL cp_cfm_scale(alpha_coeff, cmos_new)
1688 2864 : CALL cp_cfm_to_cfm(cmos_new, kp%mos(ispin)%cmo_coeff)
1689 : END DO
1690 : END DO
1691 :
1692 : ! Phase 2: Accumulate historical snapshots C_new += B(i) * C_proj(k)
1693 2308 : DO i = 2, nvec
1694 1752 : t0_state => wfi_get_snapshot(wf_history, wf_index=i)
1695 1752 : IF (method_nr == wfi_aspc_nr) THEN
1696 : alpha_coeff = (-1.0_dp)**(i + 1)*REAL(i, KIND=dp)* &
1697 1750 : binomial(2*nvec, nvec - i)/binomial(2*nvec - 2, nvec - 1)
1698 1750 : IF ((io_unit > 0) .AND. (print_level > low_print_level)) THEN
1699 71 : WRITE (UNIT=io_unit, FMT="(T3,A2,I0,A4,F10.6)") "B(", i, ") = ", alpha_coeff
1700 : END IF
1701 : ELSE
1702 2 : alpha_coeff = -1.0_dp*alpha_coeff*REAL(nvec - i + 1, dp)/REAL(i, dp)
1703 : END IF
1704 :
1705 5825 : DO ikp = 1, kplocal
1706 3517 : kp => kpoints%kp_env(ikp)%kpoint_env
1707 8882 : DO ispin = 1, nspin
1708 3613 : ik = kp_range(1) + ikp - 1
1709 3613 : CALL cp_cfm_to_cfm(t1_state%wf_kp_cfm(ikp, ispin), cmos_1)
1710 3613 : CALL cp_cfm_to_cfm(t0_state%wf_kp_cfm(ikp, ispin), cmos_i)
1711 :
1712 : ! Express the reference snapshot in the image convention of snapshot i,
1713 : ! because the historical overlap below belongs to snapshot i.
1714 : CALL wfi_apply_kp_pbc_phase_cfm(cmos_1, t0_state%kp_pbc_shift - t1_state%kp_pbc_shift, &
1715 91997 : xkp(1:3, ik), matrix_s_kp(1, 1)%matrix)
1716 :
1717 : ! Subspace projection: C_proj = C_i * (C_i^dag S_i C_1)
1718 : CALL parallel_gemm('N', 'N', nao, nmo, nao, z_one, &
1719 3613 : t0_state%overlap_cfm_kp(ikp), cmos_1, z_zero, cfm_nao_nmo_work)
1720 : CALL parallel_gemm('C', 'N', nmo, nmo, nao, z_one, &
1721 3613 : cmos_i, cfm_nao_nmo_work, z_zero, csc_cfm)
1722 : CALL parallel_gemm('N', 'N', nao, nmo, nmo, z_one, &
1723 3613 : cmos_i, csc_cfm, z_zero, cfm_nao_nmo_work)
1724 :
1725 : ! Convert the projected contribution from snapshot i to the reference
1726 : ! image convention of snapshot 1. The final conversion to the current
1727 : ! convention is centralized in wfi_use_prev_wf_kp.
1728 : CALL wfi_apply_kp_pbc_phase_cfm(cfm_nao_nmo_work, t1_state%kp_pbc_shift - t0_state%kp_pbc_shift, &
1729 91997 : xkp(1:3, ik), matrix_s_kp(1, 1)%matrix)
1730 :
1731 3613 : CALL cp_cfm_to_cfm(kp%mos(ispin)%cmo_coeff, cmos_new)
1732 3613 : CALL cp_cfm_scale_and_add(z_one, cmos_new, CMPLX(alpha_coeff, 0.0_dp, KIND=dp), cfm_nao_nmo_work)
1733 7130 : CALL cp_cfm_to_cfm(cmos_new, kp%mos(ispin)%cmo_coeff)
1734 : END DO
1735 : END DO
1736 : END DO
1737 :
1738 556 : CALL cp_cfm_release(cmos_new)
1739 556 : CALL cp_cfm_release(cmos_1)
1740 556 : CALL cp_cfm_release(cmos_i)
1741 556 : CALL cp_cfm_release(cfm_nao_nmo_work)
1742 556 : CALL cp_cfm_release(csc_cfm)
1743 :
1744 : ! Phase 3: Convert the extrapolated WFN from the reference snapshot image
1745 : ! convention to the current k-point PBC convention, then reorthogonalize and
1746 : ! rebuild the density. Keep the actual phase handling centralized in
1747 : ! wfi_use_prev_wf_kp so that USE_PREV_WF and ASPC/PS share the same path.
1748 : CALL wfi_use_prev_wf_kp(qs_env, 0, print_level, pbc_shift_ref=t1_state%kp_pbc_shift, &
1749 556 : load_snapshot_wf=.FALSE.)
1750 :
1751 556 : CALL timestop(handle)
1752 :
1753 556 : END SUBROUTINE wfi_extrapolate_ps_aspc_kp
1754 :
1755 : ! **************************************************************************************************
1756 : !> \brief GEXT_PROJ/GEXT_PROJ_QTR wavefunction extrapolation for complex k-points.
1757 : !> This follows the existing ASPC/PS k-point projection path, but uses
1758 : !> the GEXT-fitted coefficients.
1759 : !> \param wf_history wavefunction history buffer
1760 : !> \param qs_env The QS environment
1761 : !> \param nvec number of previous wavefunctions
1762 : !> \param io_unit output unit
1763 : !> \param print_level current print level
1764 : ! **************************************************************************************************
1765 244 : SUBROUTINE wfi_extrapolate_gext_proj_kp(wf_history, qs_env, nvec, io_unit, print_level)
1766 : TYPE(qs_wf_history_type), POINTER :: wf_history
1767 : TYPE(qs_environment_type), POINTER :: qs_env
1768 : INTEGER, INTENT(IN) :: nvec, io_unit, print_level
1769 :
1770 : CHARACTER(len=*), PARAMETER :: routineN = 'wfi_extrapolate_gext_proj_kp'
1771 :
1772 : INTEGER :: handle, i, ik, ikp, ispin, kplocal, &
1773 : method_nr, nao, nmo, nspin
1774 : INTEGER, DIMENSION(2) :: kp_range
1775 : LOGICAL :: use_real_wfn
1776 244 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: coeffs, weight_kp
1777 244 : REAL(KIND=dp), DIMENSION(:), POINTER :: wkp
1778 244 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
1779 : TYPE(cp_cfm_type) :: cfm_nao_nmo_work, cmos_1, cmos_i, &
1780 : cmos_new, csc_cfm
1781 244 : TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:) :: csmat_cur
1782 : TYPE(cp_cfm_type), POINTER :: mo_coeff
1783 : TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool_kp
1784 : TYPE(cp_fm_struct_type), POINTER :: ao_ao_struct, nmo_nmo_struct
1785 244 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp
1786 : TYPE(kpoint_env_type), POINTER :: kp
1787 : TYPE(kpoint_type), POINTER :: kpoints
1788 : TYPE(mp_para_env_type), POINTER :: para_env_inter_kp
1789 : TYPE(qs_matrix_pools_type), POINTER :: mpools_kp
1790 : TYPE(qs_scf_env_type), POINTER :: scf_env
1791 : TYPE(qs_wf_snapshot_type), POINTER :: t0_state, t1_state
1792 :
1793 244 : method_nr = wf_history%interpolation_method_nr
1794 :
1795 244 : CALL timeset(routineN, handle)
1796 244 : NULLIFY (ao_ao_struct, kp, kpoints, matrix_s_kp, mo_coeff, mpools_kp, &
1797 244 : para_env_inter_kp, scf_env, t0_state, t1_state, wkp, xkp, nmo_nmo_struct, &
1798 244 : ao_ao_fm_pool_kp)
1799 :
1800 244 : CALL get_qs_env(qs_env, kpoints=kpoints, matrix_s_kp=matrix_s_kp, scf_env=scf_env)
1801 : CALL get_kpoint_info(kpoints, use_real_wfn=use_real_wfn, kp_range=kp_range, &
1802 : wkp=wkp, xkp=xkp, &
1803 244 : mpools=mpools_kp, para_env_inter_kp=para_env_inter_kp)
1804 244 : kplocal = kp_range(2) - kp_range(1) + 1
1805 :
1806 244 : IF (use_real_wfn) THEN
1807 : CALL cp_warn(__LOCATION__, "GExt with k-points requires complex wavefunctions; "// &
1808 0 : "falling back to USE_PREV_WF.")
1809 0 : CALL wfi_use_prev_wf_kp(qs_env, io_unit, print_level)
1810 0 : CALL timestop(handle)
1811 0 : RETURN
1812 : END IF
1813 :
1814 244 : IF (nvec >= wf_history%memory_depth) THEN
1815 192 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. &
1816 : (qs_env%scf_control%eps_scf_hist /= 0)) THEN
1817 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1818 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1819 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1820 192 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
1821 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1822 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1823 192 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
1824 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1825 : END IF
1826 : END IF
1827 :
1828 244 : kp => kpoints%kp_env(1)%kpoint_env
1829 244 : nspin = SIZE(kp%mos)
1830 244 : CALL get_mo_set(kp%mos(1), nao=nao, nmo=nmo, cmo_coeff=mo_coeff)
1831 :
1832 244 : CALL mpools_get(mpools_kp, ao_ao_fm_pool=ao_ao_fm_pool_kp)
1833 244 : ao_ao_struct => fm_pool_get_el_struct(ao_ao_fm_pool_kp)
1834 :
1835 1718 : ALLOCATE (csmat_cur(kplocal), weight_kp(kplocal))
1836 742 : DO ikp = 1, kplocal
1837 498 : CALL cp_cfm_create(csmat_cur(ikp), ao_ao_struct)
1838 742 : weight_kp(ikp) = wkp(kp_range(1) + ikp - 1)
1839 : END DO
1840 :
1841 : CALL kpoint_operator_get_all(kpoints, scf_env%scf_work1(1), matrix_s_kp, kpoint_slot_s, &
1842 244 : csmat_cur, matrix_row=kpoint_spin_free)
1843 :
1844 732 : ALLOCATE (coeffs(nvec))
1845 244 : IF (method_nr == wfi_gext_proj_nr) THEN
1846 : CALL diff_fitting(wf_history, matrix_s_kp(1, 1)%matrix, coeffs, nvec, &
1847 : 1e-4_dp, io_unit, print_level, current_overlap_kp=csmat_cur, &
1848 240 : kpoint_weights=weight_kp, para_env_inter_kp=para_env_inter_kp)
1849 : ELSE
1850 : CALL tr_fitting(wf_history, matrix_s_kp(1, 1)%matrix, coeffs, nvec, &
1851 : 1e-4_dp, io_unit, print_level, current_overlap_kp=csmat_cur, &
1852 4 : kpoint_weights=weight_kp, para_env_inter_kp=para_env_inter_kp)
1853 : END IF
1854 :
1855 : ! Accumulate the extrapolated WFN using the same projected-WFN path as ASPC/PS.
1856 244 : CALL cp_cfm_create(cmos_new, mo_coeff%matrix_struct)
1857 244 : CALL cp_cfm_create(cmos_1, mo_coeff%matrix_struct)
1858 244 : CALL cp_cfm_create(cmos_i, mo_coeff%matrix_struct)
1859 244 : CALL cp_cfm_create(cfm_nao_nmo_work, mo_coeff%matrix_struct)
1860 : CALL cp_fm_struct_create(nmo_nmo_struct, template_fmstruct=mo_coeff%matrix_struct, &
1861 244 : nrow_global=nmo, ncol_global=nmo)
1862 244 : CALL cp_cfm_create(csc_cfm, nmo_nmo_struct)
1863 244 : CALL cp_fm_struct_release(nmo_nmo_struct)
1864 :
1865 244 : t1_state => wfi_get_snapshot(wf_history, wf_index=1)
1866 742 : DO ikp = 1, kplocal
1867 498 : kp => kpoints%kp_env(ikp)%kpoint_env
1868 1484 : DO ispin = 1, nspin
1869 1240 : CALL cp_cfm_set_all(kp%mos(ispin)%cmo_coeff, z_zero)
1870 : END DO
1871 : END DO
1872 :
1873 896 : DO i = 1, nvec
1874 652 : t0_state => wfi_get_snapshot(wf_history, wf_index=i)
1875 2216 : DO ikp = 1, kplocal
1876 1320 : kp => kpoints%kp_env(ikp)%kpoint_env
1877 1320 : ik = kp_range(1) + ikp - 1
1878 3946 : DO ispin = 1, nspin
1879 1974 : CALL cp_cfm_to_cfm(t1_state%wf_kp_cfm(ikp, ispin), cmos_1)
1880 1974 : CALL cp_cfm_to_cfm(t0_state%wf_kp_cfm(ikp, ispin), cmos_i)
1881 :
1882 : CALL wfi_apply_kp_pbc_phase_cfm(cmos_1, t0_state%kp_pbc_shift - t1_state%kp_pbc_shift, &
1883 26590 : xkp(1:3, ik), matrix_s_kp(1, 1)%matrix)
1884 :
1885 : CALL parallel_gemm('N', 'N', nao, nmo, nao, z_one, &
1886 1974 : t0_state%overlap_cfm_kp(ikp), cmos_1, z_zero, cfm_nao_nmo_work)
1887 : CALL parallel_gemm('C', 'N', nmo, nmo, nao, z_one, &
1888 1974 : cmos_i, cfm_nao_nmo_work, z_zero, csc_cfm)
1889 : CALL parallel_gemm('N', 'N', nao, nmo, nmo, z_one, &
1890 1974 : cmos_i, csc_cfm, z_zero, cfm_nao_nmo_work)
1891 :
1892 : CALL wfi_apply_kp_pbc_phase_cfm(cfm_nao_nmo_work, t1_state%kp_pbc_shift - t0_state%kp_pbc_shift, &
1893 26590 : xkp(1:3, ik), matrix_s_kp(1, 1)%matrix)
1894 :
1895 1974 : CALL cp_cfm_to_cfm(kp%mos(ispin)%cmo_coeff, cmos_new)
1896 1974 : CALL cp_cfm_scale_and_add(z_one, cmos_new, CMPLX(coeffs(i), 0.0_dp, KIND=dp), cfm_nao_nmo_work)
1897 3294 : CALL cp_cfm_to_cfm(cmos_new, kp%mos(ispin)%cmo_coeff)
1898 : END DO
1899 : END DO
1900 : END DO
1901 :
1902 244 : CALL cp_cfm_release(cmos_new)
1903 244 : CALL cp_cfm_release(cmos_1)
1904 244 : CALL cp_cfm_release(cmos_i)
1905 244 : CALL cp_cfm_release(cfm_nao_nmo_work)
1906 244 : CALL cp_cfm_release(csc_cfm)
1907 :
1908 : CALL wfi_use_prev_wf_kp(qs_env, 0, print_level, pbc_shift_ref=t1_state%kp_pbc_shift, &
1909 244 : load_snapshot_wf=.FALSE.)
1910 :
1911 742 : DO ikp = 1, kplocal
1912 742 : CALL cp_cfm_release(csmat_cur(ikp))
1913 : END DO
1914 244 : DEALLOCATE (csmat_cur, coeffs, weight_kp)
1915 :
1916 244 : CALL timestop(handle)
1917 :
1918 1220 : END SUBROUTINE wfi_extrapolate_gext_proj_kp
1919 :
1920 : ! **************************************************************************************************
1921 : !> \brief Decides if scf control variables has to changed due
1922 : !> to using a WF extrapolation.
1923 : !> \param qs_env The QS environment
1924 : !> \param nvec ...
1925 : !> \par History
1926 : !> 11.2006 created [TdK]
1927 : !> \author Thomas D. Kuehne (tkuehne@phys.chem.ethz.ch)
1928 : ! **************************************************************************************************
1929 11793 : ELEMENTAL SUBROUTINE wfi_set_history_variables(qs_env, nvec)
1930 : TYPE(qs_environment_type), INTENT(INOUT) :: qs_env
1931 : INTEGER, INTENT(IN) :: nvec
1932 :
1933 11793 : IF (nvec >= qs_env%wf_history%memory_depth) THEN
1934 1915 : IF ((qs_env%scf_control%max_scf_hist /= 0) .AND. (qs_env%scf_control%eps_scf_hist /= 0)) THEN
1935 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1936 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1937 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1938 1915 : ELSE IF (qs_env%scf_control%max_scf_hist /= 0) THEN
1939 0 : qs_env%scf_control%max_scf = qs_env%scf_control%max_scf_hist
1940 0 : qs_env%scf_control%outer_scf%have_scf = .FALSE.
1941 1915 : ELSE IF (qs_env%scf_control%eps_scf_hist /= 0) THEN
1942 0 : qs_env%scf_control%eps_scf = qs_env%scf_control%eps_scf_hist
1943 0 : qs_env%scf_control%outer_scf%eps_scf = qs_env%scf_control%eps_scf_hist
1944 : END IF
1945 : END IF
1946 :
1947 11793 : END SUBROUTINE wfi_set_history_variables
1948 :
1949 : ! **************************************************************************************************
1950 : !> \brief updates the snapshot buffer, taking a new snapshot
1951 : !> \param wf_history the history buffer to update
1952 : !> \param qs_env the qs_env we get the info from
1953 : !> \param dt ...
1954 : !> \par History
1955 : !> 02.2003 created [fawzi]
1956 : !> \author fawzi
1957 : ! **************************************************************************************************
1958 24365 : SUBROUTINE wfi_update(wf_history, qs_env, dt)
1959 : TYPE(qs_wf_history_type), POINTER :: wf_history
1960 : TYPE(qs_environment_type), POINTER :: qs_env
1961 : REAL(KIND=dp), INTENT(in) :: dt
1962 :
1963 24365 : CPASSERT(ASSOCIATED(wf_history))
1964 24365 : CPASSERT(wf_history%ref_count > 0)
1965 24365 : CPASSERT(ASSOCIATED(qs_env))
1966 :
1967 24365 : wf_history%snapshot_count = wf_history%snapshot_count + 1
1968 24365 : IF (wf_history%memory_depth > 0) THEN
1969 : wf_history%last_state_index = MODULO(wf_history%snapshot_count, &
1970 23132 : wf_history%memory_depth) + 1
1971 : CALL wfs_update(snapshot=wf_history%past_states &
1972 : (wf_history%last_state_index)%snapshot, wf_history=wf_history, &
1973 23132 : qs_env=qs_env, dt=dt)
1974 : END IF
1975 24365 : END SUBROUTINE wfi_update
1976 :
1977 : ! **************************************************************************************************
1978 : !> \brief reorthogonalizes the mos
1979 : !> \param qs_env the qs_env in which to orthogonalize
1980 : !> \param v_matrix the vectors to orthogonalize
1981 : !> \param n_col number of column of v to orthogonalize
1982 : !> \par History
1983 : !> 04.2003 created [fawzi]
1984 : !> \author Fawzi Mohamed
1985 : ! **************************************************************************************************
1986 34534 : SUBROUTINE reorthogonalize_vectors(qs_env, v_matrix, n_col)
1987 : TYPE(qs_environment_type), POINTER :: qs_env
1988 : TYPE(cp_fm_type), INTENT(IN) :: v_matrix
1989 : INTEGER, INTENT(in), OPTIONAL :: n_col
1990 :
1991 : CHARACTER(len=*), PARAMETER :: routineN = 'reorthogonalize_vectors'
1992 :
1993 : INTEGER :: handle, my_n_col
1994 : LOGICAL :: has_unit_metric, &
1995 : ortho_contains_cholesky, &
1996 : smearing_is_used
1997 : TYPE(cp_fm_pool_type), POINTER :: maxao_maxmo_fm_pool
1998 17267 : TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
1999 : TYPE(dft_control_type), POINTER :: dft_control
2000 : TYPE(qs_matrix_pools_type), POINTER :: mpools
2001 : TYPE(qs_scf_env_type), POINTER :: scf_env
2002 : TYPE(scf_control_type), POINTER :: scf_control
2003 :
2004 17267 : NULLIFY (scf_env, scf_control, maxao_maxmo_fm_pool, matrix_s, mpools, dft_control)
2005 17267 : CALL timeset(routineN, handle)
2006 :
2007 17267 : CPASSERT(ASSOCIATED(qs_env))
2008 :
2009 17267 : CALL cp_fm_get_info(v_matrix, ncol_global=my_n_col)
2010 17267 : IF (PRESENT(n_col)) my_n_col = n_col
2011 : CALL get_qs_env(qs_env, mpools=mpools, &
2012 : scf_env=scf_env, &
2013 : scf_control=scf_control, &
2014 : matrix_s=matrix_s, &
2015 17267 : dft_control=dft_control)
2016 17267 : CALL mpools_get(mpools, maxao_maxmo_fm_pool=maxao_maxmo_fm_pool)
2017 17267 : IF (ASSOCIATED(scf_env)) THEN
2018 : ortho_contains_cholesky = (scf_env%method /= ot_method_nr) .AND. &
2019 : (scf_env%cholesky_method > 0) .AND. &
2020 17267 : ASSOCIATED(scf_env%ortho)
2021 : ELSE
2022 : ortho_contains_cholesky = .FALSE.
2023 : END IF
2024 :
2025 17267 : CALL get_qs_env(qs_env, has_unit_metric=has_unit_metric)
2026 17267 : smearing_is_used = .FALSE.
2027 17267 : IF (dft_control%smear) THEN
2028 1920 : smearing_is_used = .TRUE.
2029 : END IF
2030 :
2031 17267 : IF (has_unit_metric) THEN
2032 3410 : CALL make_basis_simple(v_matrix, my_n_col)
2033 13857 : ELSE IF (smearing_is_used) THEN
2034 : CALL make_basis_lowdin(vmatrix=v_matrix, ncol=my_n_col, &
2035 1920 : matrix_s=matrix_s(1)%matrix)
2036 11937 : ELSE IF (ortho_contains_cholesky) THEN
2037 : CALL make_basis_cholesky(vmatrix=v_matrix, ncol=my_n_col, &
2038 8330 : ortho=scf_env%ortho)
2039 : ELSE
2040 3607 : CALL make_basis_sm(v_matrix, my_n_col, matrix_s(1)%matrix)
2041 : END IF
2042 17267 : CALL timestop(handle)
2043 17267 : END SUBROUTINE reorthogonalize_vectors
2044 :
2045 : ! **************************************************************************************************
2046 : !> \brief purges wf_history retaining only the latest snapshot
2047 : !> \param qs_env the qs env with the latest result, and that will contain
2048 : !> the purged wf_history
2049 : !> \par History
2050 : !> 05.2016 created [Nico Holmberg]
2051 : !> \author Nico Holmberg
2052 : ! **************************************************************************************************
2053 0 : SUBROUTINE wfi_purge_history(qs_env)
2054 : TYPE(qs_environment_type), POINTER :: qs_env
2055 :
2056 : CHARACTER(len=*), PARAMETER :: routineN = 'wfi_purge_history'
2057 :
2058 : INTEGER :: handle, io_unit, print_level
2059 : TYPE(cp_logger_type), POINTER :: logger
2060 : TYPE(dft_control_type), POINTER :: dft_control
2061 : TYPE(qs_wf_history_type), POINTER :: wf_history
2062 :
2063 0 : NULLIFY (dft_control, wf_history)
2064 :
2065 0 : CALL timeset(routineN, handle)
2066 0 : logger => cp_get_default_logger()
2067 0 : print_level = logger%iter_info%print_level
2068 : io_unit = cp_print_key_unit_nr(logger, qs_env%input, "DFT%SCF%PRINT%PROGRAM_RUN_INFO", &
2069 0 : extension=".scfLog")
2070 :
2071 0 : CPASSERT(ASSOCIATED(qs_env))
2072 0 : CPASSERT(ASSOCIATED(qs_env%wf_history))
2073 0 : CPASSERT(qs_env%wf_history%ref_count > 0)
2074 0 : CALL get_qs_env(qs_env, dft_control=dft_control)
2075 :
2076 0 : SELECT CASE (qs_env%wf_history%interpolation_method_nr)
2077 : CASE (wfi_use_guess_method_nr, wfi_use_prev_wf_method_nr, &
2078 : wfi_use_prev_p_method_nr, wfi_frozen_method_nr)
2079 : ! do nothing
2080 : CASE (wfi_linear_wf_method_nr, wfi_linear_p_method_nr, &
2081 : wfi_linear_ps_method_nr, wfi_ps_method_nr, &
2082 : wfi_aspc_nr, wfi_gext_proj_nr, wfi_gext_proj_qtr_nr)
2083 0 : IF (qs_env%wf_history%snapshot_count >= 2) THEN
2084 0 : IF (debug_this_module .AND. io_unit > 0) THEN
2085 0 : WRITE (io_unit, FMT="(T2,A)") "QS| Purging WFN history"
2086 : END IF
2087 : CALL wfi_create(wf_history, interpolation_method_nr= &
2088 : dft_control%qs_control%wf_interpolation_method_nr, &
2089 : extrapolation_order=dft_control%qs_control%wf_extrapolation_order, &
2090 0 : has_unit_metric=qs_env%has_unit_metric)
2091 : CALL set_qs_env(qs_env=qs_env, &
2092 0 : wf_history=wf_history)
2093 0 : CALL wfi_release(wf_history)
2094 0 : CALL wfi_update(qs_env%wf_history, qs_env=qs_env, dt=1.0_dp)
2095 : END IF
2096 : CASE DEFAULT
2097 0 : CPABORT("Unknown extrapolation method.")
2098 : END SELECT
2099 0 : CALL timestop(handle)
2100 :
2101 0 : END SUBROUTINE wfi_purge_history
2102 :
2103 : ! **************************************************************************************************
2104 : !> \brief Gives the coefficients that best approximate the new overlap
2105 : !> as a linear combination of the previous overlaps in the
2106 : !> wf_history buffer. This is done by solving
2107 : !> argmin_a || S_{n+1} - S_{n} - \sum_i^{nvec-1} a_i (S_{n-q+i} - S_{n}) ||^2
2108 : !> \param wf_history wavefunction history buffer, containing the previous overlaps
2109 : !> \param current_overlap current overlap in dbcsr format
2110 : !> \param coeffs resulting nvec coefficients
2111 : !> \param nvec number of previous overlaps
2112 : !> \param eps Tikhonov regularization
2113 : !> \param io_unit output unit
2114 : !> \param print_level print level
2115 : !> \param current_overlap_kp ...
2116 : !> \param kpoint_weights ...
2117 : !> \param para_env_inter_kp ...
2118 : !> \par History
2119 : !> 04.2026 created [Michele Nottoli]
2120 : !> \author Michele Nottoli
2121 : ! **************************************************************************************************
2122 376 : SUBROUTINE diff_fitting(wf_history, current_overlap, coeffs, nvec, eps, io_unit, print_level, &
2123 376 : current_overlap_kp, kpoint_weights, para_env_inter_kp)
2124 : TYPE(qs_wf_history_type), POINTER :: wf_history
2125 : TYPE(dbcsr_type), INTENT(IN) :: current_overlap
2126 : INTEGER, INTENT(IN) :: nvec
2127 : REAL(KIND=dp), INTENT(OUT) :: coeffs(nvec)
2128 : REAL(KIND=dp), INTENT(IN) :: eps
2129 : INTEGER, INTENT(IN) :: io_unit, print_level
2130 : TYPE(cp_cfm_type), DIMENSION(:), INTENT(IN), &
2131 : OPTIONAL :: current_overlap_kp
2132 : REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL :: kpoint_weights
2133 : TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_inter_kp
2134 :
2135 : COMPLEX(KIND=dp) :: ztrace
2136 : INTEGER :: i, icol_local, ikp, info, irow_local, j
2137 : REAL(KIND=dp) :: error, norm_ref, weight
2138 376 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: b
2139 376 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: A
2140 : TYPE(cp_cfm_type) :: target_diff_cfm, tmp_conj_cfm, &
2141 : tmp_i_cfm, tmp_j_cfm
2142 : TYPE(dbcsr_type) :: target_diff, tmp_i, tmp_j, tmp_k
2143 : TYPE(qs_wf_snapshot_type), POINTER :: ref_state, state
2144 :
2145 376 : IF (nvec <= 0) THEN
2146 0 : CPABORT("Not enough vectors to do the fitting")
2147 376 : ELSE IF (nvec == 1) THEN
2148 46 : coeffs(1) = 1.0_dp
2149 566 : RETURN
2150 : END IF
2151 :
2152 330 : IF (PRESENT(current_overlap_kp)) THEN
2153 1284 : ALLOCATE (A(nvec - 1, nvec - 1), b(nvec - 1))
2154 214 : A = 0.0_dp
2155 214 : b = 0.0_dp
2156 :
2157 214 : ref_state => wfi_get_snapshot(wf_history, wf_index=1)
2158 214 : CALL cp_cfm_create(target_diff_cfm, current_overlap_kp(1)%matrix_struct)
2159 214 : CALL cp_cfm_create(tmp_i_cfm, current_overlap_kp(1)%matrix_struct)
2160 214 : CALL cp_cfm_create(tmp_j_cfm, current_overlap_kp(1)%matrix_struct)
2161 214 : CALL cp_cfm_create(tmp_conj_cfm, current_overlap_kp(1)%matrix_struct)
2162 :
2163 644 : DO ikp = 1, SIZE(current_overlap_kp)
2164 430 : weight = 1.0_dp
2165 430 : IF (PRESENT(kpoint_weights)) weight = kpoint_weights(ikp)
2166 :
2167 430 : CALL cp_cfm_to_cfm(current_overlap_kp(ikp), target_diff_cfm)
2168 : CALL cp_cfm_scale_and_add(z_one, target_diff_cfm, CMPLX(-1.0_dp, 0.0_dp, KIND=dp), &
2169 430 : ref_state%overlap_cfm_kp(ikp))
2170 1458 : DO i = 2, nvec
2171 814 : state => wfi_get_snapshot(wf_history, wf_index=i)
2172 814 : CALL cp_cfm_to_cfm(state%overlap_cfm_kp(ikp), tmp_i_cfm)
2173 : CALL cp_cfm_scale_and_add(z_one, tmp_i_cfm, CMPLX(-1.0_dp, 0.0_dp, KIND=dp), &
2174 814 : ref_state%overlap_cfm_kp(ikp))
2175 814 : CALL cp_cfm_to_cfm(tmp_i_cfm, tmp_conj_cfm)
2176 19568 : DO icol_local = 1, SIZE(tmp_conj_cfm%local_data, 2)
2177 448998 : DO irow_local = 1, SIZE(tmp_conj_cfm%local_data, 1)
2178 : tmp_conj_cfm%local_data(irow_local, icol_local) = &
2179 448184 : CONJG(tmp_conj_cfm%local_data(irow_local, icol_local))
2180 : END DO
2181 : END DO
2182 814 : CALL cp_cfm_trace(tmp_conj_cfm, target_diff_cfm, ztrace)
2183 814 : b(i - 1) = b(i - 1) + weight*REAL(ztrace, KIND=dp)
2184 :
2185 2442 : DO j = 2, i
2186 1198 : state => wfi_get_snapshot(wf_history, wf_index=j)
2187 1198 : CALL cp_cfm_to_cfm(state%overlap_cfm_kp(ikp), tmp_j_cfm)
2188 : CALL cp_cfm_scale_and_add(z_one, tmp_j_cfm, CMPLX(-1.0_dp, 0.0_dp, KIND=dp), &
2189 1198 : ref_state%overlap_cfm_kp(ikp))
2190 1198 : CALL cp_cfm_trace(tmp_conj_cfm, tmp_j_cfm, ztrace)
2191 2012 : A(j - 1, i - 1) = A(j - 1, i - 1) + weight*REAL(ztrace, KIND=dp)
2192 : END DO
2193 : END DO
2194 : END DO
2195 :
2196 620 : DO i = 2, nvec
2197 1218 : DO j = 2, i
2198 1004 : A(i - 1, j - 1) = A(j - 1, i - 1)
2199 : END DO
2200 : END DO
2201 :
2202 214 : IF (PRESENT(para_env_inter_kp)) THEN
2203 214 : IF (ASSOCIATED(para_env_inter_kp)) THEN
2204 214 : CALL para_env_inter_kp%sum(A)
2205 214 : CALL para_env_inter_kp%sum(b)
2206 : END IF
2207 : END IF
2208 :
2209 620 : DO i = 1, nvec - 1
2210 620 : A(i, i) = A(i, i) + eps**2
2211 : END DO
2212 :
2213 214 : CALL dposv('u', nvec - 1, 1, A, nvec - 1, b, nvec - 1, info)
2214 214 : IF (info /= 0) THEN
2215 0 : CPABORT("DPOSV failed.")
2216 : END IF
2217 :
2218 620 : coeffs(1) = 1.0_dp - SUM(b)
2219 620 : coeffs(2:nvec) = b(:)
2220 :
2221 214 : IF (print_level > low_print_level) THEN
2222 2 : error = 0.0_dp
2223 2 : norm_ref = 0.0_dp
2224 10 : DO ikp = 1, SIZE(current_overlap_kp)
2225 8 : weight = 1.0_dp
2226 8 : IF (PRESENT(kpoint_weights)) weight = kpoint_weights(ikp)
2227 8 : CALL cp_cfm_to_cfm(current_overlap_kp(ikp), tmp_i_cfm)
2228 24 : DO i = 1, nvec
2229 16 : state => wfi_get_snapshot(wf_history, wf_index=i)
2230 : CALL cp_cfm_scale_and_add(z_one, tmp_i_cfm, CMPLX(-coeffs(i), 0.0_dp, KIND=dp), &
2231 24 : state%overlap_cfm_kp(ikp))
2232 : END DO
2233 8 : CALL cp_cfm_to_cfm(tmp_i_cfm, tmp_conj_cfm)
2234 264 : DO icol_local = 1, SIZE(tmp_conj_cfm%local_data, 2)
2235 4360 : DO irow_local = 1, SIZE(tmp_conj_cfm%local_data, 1)
2236 : tmp_conj_cfm%local_data(irow_local, icol_local) = &
2237 4352 : CONJG(tmp_conj_cfm%local_data(irow_local, icol_local))
2238 : END DO
2239 : END DO
2240 8 : CALL cp_cfm_trace(tmp_conj_cfm, tmp_i_cfm, ztrace)
2241 8 : error = error + weight*REAL(ztrace, KIND=dp)
2242 8 : CALL cp_cfm_to_cfm(ref_state%overlap_cfm_kp(ikp), tmp_conj_cfm)
2243 264 : DO icol_local = 1, SIZE(tmp_conj_cfm%local_data, 2)
2244 4360 : DO irow_local = 1, SIZE(tmp_conj_cfm%local_data, 1)
2245 : tmp_conj_cfm%local_data(irow_local, icol_local) = &
2246 4352 : CONJG(tmp_conj_cfm%local_data(irow_local, icol_local))
2247 : END DO
2248 : END DO
2249 8 : CALL cp_cfm_trace(tmp_conj_cfm, ref_state%overlap_cfm_kp(ikp), ztrace)
2250 18 : norm_ref = norm_ref + weight*REAL(ztrace, KIND=dp)
2251 : END DO
2252 2 : IF (PRESENT(para_env_inter_kp)) THEN
2253 2 : IF (ASSOCIATED(para_env_inter_kp)) THEN
2254 2 : CALL para_env_inter_kp%sum(error)
2255 2 : CALL para_env_inter_kp%sum(norm_ref)
2256 : END IF
2257 : END IF
2258 2 : IF (io_unit > 0) THEN
2259 1 : WRITE (UNIT=io_unit, FMT="(/,T2,A,F20.10)") "GEXT overlap fitting error:", &
2260 2 : SQRT(error/MAX(norm_ref, TINY(1.0_dp)))
2261 : END IF
2262 : END IF
2263 :
2264 214 : CALL cp_cfm_release(target_diff_cfm)
2265 214 : CALL cp_cfm_release(tmp_i_cfm)
2266 214 : CALL cp_cfm_release(tmp_j_cfm)
2267 214 : CALL cp_cfm_release(tmp_conj_cfm)
2268 214 : DEALLOCATE (A, b)
2269 214 : RETURN
2270 : END IF
2271 :
2272 696 : ALLOCATE (A(nvec - 1, nvec - 1), b(nvec - 1))
2273 :
2274 : ! get the reference for the difference fitting
2275 116 : ref_state => wfi_get_snapshot(wf_history, wf_index=1)
2276 :
2277 : ! assemble the target difference
2278 116 : CALL dbcsr_copy(target_diff, current_overlap)
2279 116 : CALL dbcsr_add(target_diff, ref_state%overlap, 1.0_dp, -1.0_dp)
2280 :
2281 : ! allocate tmp_k
2282 116 : CALL dbcsr_copy(tmp_k, current_overlap)
2283 :
2284 : ! assemble the matrix A and the RHS b
2285 348 : DO i = 2, nvec
2286 232 : state => wfi_get_snapshot(wf_history, wf_index=i)
2287 232 : CALL dbcsr_copy(tmp_i, state%overlap)
2288 232 : CALL dbcsr_add(tmp_i, ref_state%overlap, 1.0_dp, -1.0_dp)
2289 232 : CALL dbcsr_multiply("N", "N", 1.0_dp, tmp_i, target_diff, 0.0_dp, tmp_k)
2290 232 : CALL dbcsr_trace(tmp_k, b(i - 1))
2291 :
2292 724 : DO j = 2, i
2293 376 : state => wfi_get_snapshot(wf_history, wf_index=j)
2294 376 : CALL dbcsr_copy(tmp_j, state%overlap)
2295 376 : CALL dbcsr_add(tmp_j, ref_state%overlap, 1.0_dp, -1.0_dp)
2296 376 : CALL dbcsr_multiply("N", "N", 1.0_dp, tmp_i, tmp_j, 0.0_dp, tmp_k)
2297 376 : CALL dbcsr_trace(tmp_k, A(j - 1, i - 1))
2298 608 : A(i - 1, j - 1) = A(j - 1, i - 1)
2299 : END DO
2300 : END DO
2301 :
2302 : ! add the Tikhonov regularization
2303 348 : DO i = 1, nvec - 1
2304 348 : A(i, i) = A(i, i) + eps**2
2305 : END DO
2306 :
2307 : ! solve the linear system
2308 116 : CALL dposv('u', nvec - 1, 1, A, nvec - 1, b, nvec - 1, info)
2309 116 : IF (info /= 0) THEN
2310 0 : CPABORT("DPOSV failed.")
2311 : END IF
2312 :
2313 : ! set the coefficient for the reference snapshot
2314 348 : coeffs(1) = 1.0_dp - SUM(b)
2315 348 : coeffs(2:nvec) = b(:)
2316 :
2317 : ! as a consistency check, print how well the current overlap
2318 : ! is approximated by the linear combination of previous overlaps
2319 116 : IF (print_level > low_print_level) THEN
2320 20 : CALL dbcsr_copy(tmp_i, current_overlap)
2321 96 : DO i = 1, nvec
2322 76 : state => wfi_get_snapshot(wf_history, wf_index=i)
2323 96 : CALL dbcsr_add(tmp_i, state%overlap, 1.0_dp, -coeffs(i))
2324 : END DO
2325 20 : error = dbcsr_frobenius_norm(tmp_i)/dbcsr_frobenius_norm(state%overlap)
2326 20 : IF (io_unit > 0) THEN
2327 10 : WRITE (UNIT=io_unit, FMT="(/,T2,A,F20.10)") "GEXT overlap fitting error:", error
2328 : END IF
2329 : END IF
2330 :
2331 : ! free the memory
2332 116 : CALL dbcsr_release(tmp_i)
2333 116 : CALL dbcsr_release(tmp_j)
2334 116 : CALL dbcsr_release(tmp_k)
2335 116 : CALL dbcsr_release(target_diff)
2336 116 : DEALLOCATE (A, b)
2337 :
2338 896 : END SUBROUTINE diff_fitting
2339 :
2340 : ! **************************************************************************************************
2341 : !> \brief Gives the coefficients that best approximate the new overlap
2342 : !> as a time reversible linear combination of the previous overlaps in the
2343 : !> wf_history buffer. This is done by solving
2344 : !> argmin_a || S_{n+1} + S_{n+1-nvec}
2345 : !> - \sum_{i=1}^q a_i (S_{n+1-nvec+i} + S_{n+1-i}) ||^2
2346 : !> with q = nvec/2 if nvec is even, or q = (nvec-1)/2 if odd.
2347 : !> \param wf_history wavefunction history buffer, containing the previous overlaps
2348 : !> \param current_overlap current overlap in dbcsr format
2349 : !> \param coeffs resulting nvec coefficients
2350 : !> \param nvec number of previous overlaps
2351 : !> \param eps Tikhonov regularization
2352 : !> \param io_unit output unit
2353 : !> \param print_level print level
2354 : !> \param current_overlap_kp ...
2355 : !> \param kpoint_weights ...
2356 : !> \param para_env_inter_kp ...
2357 : !> \par History
2358 : !> 04.2026 created [Michele Nottoli]
2359 : ! **************************************************************************************************
2360 28 : SUBROUTINE tr_fitting(wf_history, current_overlap, coeffs, nvec, eps, io_unit, print_level, &
2361 28 : current_overlap_kp, kpoint_weights, para_env_inter_kp)
2362 : TYPE(qs_wf_history_type), POINTER :: wf_history
2363 : TYPE(dbcsr_type), INTENT(IN) :: current_overlap
2364 : INTEGER, INTENT(IN) :: nvec
2365 : REAL(KIND=dp), INTENT(OUT) :: coeffs(nvec)
2366 : REAL(KIND=dp), INTENT(IN) :: eps
2367 : INTEGER, INTENT(IN) :: io_unit, print_level
2368 : TYPE(cp_cfm_type), DIMENSION(:), INTENT(IN), &
2369 : OPTIONAL :: current_overlap_kp
2370 : REAL(KIND=dp), DIMENSION(:), INTENT(IN), OPTIONAL :: kpoint_weights
2371 : TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env_inter_kp
2372 :
2373 : COMPLEX(KIND=dp) :: ztrace
2374 : INTEGER :: i, icol_local, ikp, info, irow_local, j, &
2375 : ntr
2376 : REAL(KIND=dp) :: error, norm_ref, weight
2377 28 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: b
2378 28 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: A
2379 : TYPE(cp_cfm_type) :: target_overlap_cfm, tmp_conj_cfm, &
2380 : tmp_i_cfm, tmp_j_cfm
2381 : TYPE(dbcsr_type) :: target_overlap, tmp_i, tmp_j, tmp_k
2382 : TYPE(qs_wf_snapshot_type), POINTER :: ref_state, state
2383 :
2384 28 : IF (nvec <= 0) THEN
2385 0 : CPABORT("Not enough vectors to do the fitting")
2386 28 : ELSE IF (nvec == 1) THEN
2387 6 : coeffs(1) = 1.0_dp
2388 22 : RETURN
2389 : END IF
2390 :
2391 22 : IF (MOD(nvec, 2) == 0) THEN
2392 10 : ntr = nvec/2
2393 : ELSE
2394 12 : ntr = (nvec - 1)/2
2395 : END IF
2396 :
2397 22 : IF (PRESENT(current_overlap_kp)) THEN
2398 12 : ALLOCATE (A(ntr, ntr), b(ntr))
2399 2 : A = 0.0_dp
2400 2 : b = 0.0_dp
2401 :
2402 2 : ref_state => wfi_get_snapshot(wf_history, wf_index=nvec)
2403 2 : CALL cp_cfm_create(target_overlap_cfm, current_overlap_kp(1)%matrix_struct)
2404 2 : CALL cp_cfm_create(tmp_i_cfm, current_overlap_kp(1)%matrix_struct)
2405 2 : CALL cp_cfm_create(tmp_j_cfm, current_overlap_kp(1)%matrix_struct)
2406 2 : CALL cp_cfm_create(tmp_conj_cfm, current_overlap_kp(1)%matrix_struct)
2407 :
2408 10 : DO ikp = 1, SIZE(current_overlap_kp)
2409 8 : weight = 1.0_dp
2410 8 : IF (PRESENT(kpoint_weights)) weight = kpoint_weights(ikp)
2411 :
2412 8 : CALL cp_cfm_to_cfm(current_overlap_kp(ikp), target_overlap_cfm)
2413 8 : CALL cp_cfm_scale_and_add(z_one, target_overlap_cfm, z_one, ref_state%overlap_cfm_kp(ikp))
2414 18 : DO i = 1, ntr
2415 8 : state => wfi_get_snapshot(wf_history, wf_index=i)
2416 8 : CALL cp_cfm_to_cfm(state%overlap_cfm_kp(ikp), tmp_i_cfm)
2417 8 : state => wfi_get_snapshot(wf_history, wf_index=nvec - i)
2418 8 : CALL cp_cfm_scale_and_add(z_one, tmp_i_cfm, z_one, state%overlap_cfm_kp(ikp))
2419 :
2420 8 : CALL cp_cfm_to_cfm(tmp_i_cfm, tmp_conj_cfm)
2421 264 : DO icol_local = 1, SIZE(tmp_conj_cfm%local_data, 2)
2422 4360 : DO irow_local = 1, SIZE(tmp_conj_cfm%local_data, 1)
2423 : tmp_conj_cfm%local_data(irow_local, icol_local) = &
2424 4352 : CONJG(tmp_conj_cfm%local_data(irow_local, icol_local))
2425 : END DO
2426 : END DO
2427 8 : CALL cp_cfm_trace(tmp_conj_cfm, target_overlap_cfm, ztrace)
2428 8 : b(i) = b(i) + weight*REAL(ztrace, KIND=dp)
2429 24 : DO j = 1, i
2430 8 : state => wfi_get_snapshot(wf_history, wf_index=j)
2431 8 : CALL cp_cfm_to_cfm(state%overlap_cfm_kp(ikp), tmp_j_cfm)
2432 8 : state => wfi_get_snapshot(wf_history, wf_index=nvec - j)
2433 8 : CALL cp_cfm_scale_and_add(z_one, tmp_j_cfm, z_one, state%overlap_cfm_kp(ikp))
2434 8 : CALL cp_cfm_trace(tmp_conj_cfm, tmp_j_cfm, ztrace)
2435 16 : A(j, i) = A(j, i) + weight*REAL(ztrace, KIND=dp)
2436 : END DO
2437 : END DO
2438 : END DO
2439 :
2440 4 : DO i = 1, ntr
2441 6 : DO j = 1, i
2442 4 : A(i, j) = A(j, i)
2443 : END DO
2444 : END DO
2445 :
2446 2 : IF (PRESENT(para_env_inter_kp)) THEN
2447 2 : IF (ASSOCIATED(para_env_inter_kp)) THEN
2448 2 : CALL para_env_inter_kp%sum(A)
2449 2 : CALL para_env_inter_kp%sum(b)
2450 : END IF
2451 : END IF
2452 :
2453 4 : DO i = 1, ntr
2454 4 : A(i, i) = A(i, i) + eps**2
2455 : END DO
2456 :
2457 2 : CALL dposv('u', ntr, 1, A, ntr, b, ntr, info)
2458 2 : IF (info /= 0) THEN
2459 0 : CPABORT("DPOSV failed.")
2460 : END IF
2461 :
2462 6 : coeffs = 0.0_dp
2463 2 : coeffs(nvec) = -1.0_dp
2464 4 : DO i = 1, ntr
2465 2 : coeffs(i) = coeffs(i) + b(i)
2466 4 : coeffs(nvec - i) = coeffs(nvec - i) + b(i)
2467 : END DO
2468 :
2469 2 : IF (print_level > low_print_level) THEN
2470 2 : error = 0.0_dp
2471 2 : norm_ref = 0.0_dp
2472 10 : DO ikp = 1, SIZE(current_overlap_kp)
2473 8 : weight = 1.0_dp
2474 8 : IF (PRESENT(kpoint_weights)) weight = kpoint_weights(ikp)
2475 8 : CALL cp_cfm_to_cfm(current_overlap_kp(ikp), tmp_i_cfm)
2476 24 : DO i = 1, nvec
2477 16 : state => wfi_get_snapshot(wf_history, wf_index=i)
2478 : CALL cp_cfm_scale_and_add(z_one, tmp_i_cfm, CMPLX(-coeffs(i), 0.0_dp, KIND=dp), &
2479 24 : state%overlap_cfm_kp(ikp))
2480 : END DO
2481 8 : CALL cp_cfm_to_cfm(tmp_i_cfm, tmp_conj_cfm)
2482 264 : DO icol_local = 1, SIZE(tmp_conj_cfm%local_data, 2)
2483 4360 : DO irow_local = 1, SIZE(tmp_conj_cfm%local_data, 1)
2484 : tmp_conj_cfm%local_data(irow_local, icol_local) = &
2485 4352 : CONJG(tmp_conj_cfm%local_data(irow_local, icol_local))
2486 : END DO
2487 : END DO
2488 8 : CALL cp_cfm_trace(tmp_conj_cfm, tmp_i_cfm, ztrace)
2489 8 : error = error + weight*REAL(ztrace, KIND=dp)
2490 8 : CALL cp_cfm_to_cfm(ref_state%overlap_cfm_kp(ikp), tmp_conj_cfm)
2491 264 : DO icol_local = 1, SIZE(tmp_conj_cfm%local_data, 2)
2492 4360 : DO irow_local = 1, SIZE(tmp_conj_cfm%local_data, 1)
2493 : tmp_conj_cfm%local_data(irow_local, icol_local) = &
2494 4352 : CONJG(tmp_conj_cfm%local_data(irow_local, icol_local))
2495 : END DO
2496 : END DO
2497 8 : CALL cp_cfm_trace(tmp_conj_cfm, ref_state%overlap_cfm_kp(ikp), ztrace)
2498 18 : norm_ref = norm_ref + weight*REAL(ztrace, KIND=dp)
2499 : END DO
2500 2 : IF (PRESENT(para_env_inter_kp)) THEN
2501 2 : IF (ASSOCIATED(para_env_inter_kp)) THEN
2502 2 : CALL para_env_inter_kp%sum(error)
2503 2 : CALL para_env_inter_kp%sum(norm_ref)
2504 : END IF
2505 : END IF
2506 2 : IF (io_unit > 0) THEN
2507 1 : WRITE (UNIT=io_unit, FMT="(/,T2,A,F20.10)") "GEXT overlap fitting error:", &
2508 2 : SQRT(error/MAX(norm_ref, TINY(1.0_dp)))
2509 : END IF
2510 : END IF
2511 :
2512 2 : CALL cp_cfm_release(target_overlap_cfm)
2513 2 : CALL cp_cfm_release(tmp_i_cfm)
2514 2 : CALL cp_cfm_release(tmp_j_cfm)
2515 2 : CALL cp_cfm_release(tmp_conj_cfm)
2516 2 : DEALLOCATE (A, b)
2517 2 : RETURN
2518 : END IF
2519 :
2520 120 : ALLOCATE (A(ntr, ntr), b(ntr))
2521 :
2522 : ! get the reference for the difference fitting
2523 20 : ref_state => wfi_get_snapshot(wf_history, wf_index=nvec)
2524 :
2525 : ! assemble the target sum
2526 20 : CALL dbcsr_copy(target_overlap, current_overlap)
2527 20 : CALL dbcsr_add(target_overlap, ref_state%overlap, 1.0_dp, 1.0_dp)
2528 :
2529 : ! allocate tmp_k
2530 20 : CALL dbcsr_copy(tmp_k, current_overlap)
2531 :
2532 : ! assemble the matrix A and the RHS b
2533 52 : DO i = 1, ntr
2534 32 : state => wfi_get_snapshot(wf_history, wf_index=i)
2535 32 : CALL dbcsr_copy(tmp_i, state%overlap)
2536 32 : state => wfi_get_snapshot(wf_history, wf_index=nvec - i)
2537 32 : CALL dbcsr_add(tmp_i, state%overlap, 1.0_dp, 1.0_dp)
2538 :
2539 : CALL dbcsr_multiply("N", "N", 1.0_dp, tmp_i, target_overlap, &
2540 32 : 0.0_dp, tmp_k)
2541 32 : CALL dbcsr_trace(tmp_k, b(i))
2542 96 : DO j = 1, i
2543 44 : state => wfi_get_snapshot(wf_history, wf_index=j)
2544 44 : CALL dbcsr_copy(tmp_j, state%overlap)
2545 44 : state => wfi_get_snapshot(wf_history, wf_index=nvec - j)
2546 44 : CALL dbcsr_add(tmp_j, state%overlap, 1.0_dp, 1.0_dp)
2547 44 : CALL dbcsr_multiply("N", "N", 1.0_dp, tmp_i, tmp_j, 0.0_dp, tmp_k)
2548 44 : CALL dbcsr_trace(tmp_k, A(j, i))
2549 76 : A(i, j) = A(j, i)
2550 : END DO
2551 : END DO
2552 :
2553 : ! add the Tikhonov regularization
2554 52 : DO i = 1, ntr
2555 52 : A(i, i) = A(i, i) + eps**2
2556 : END DO
2557 :
2558 : ! solve the linear system
2559 20 : CALL dposv('u', ntr, 1, A, ntr, b, ntr, info)
2560 20 : IF (info /= 0) THEN
2561 0 : CPABORT("DPOSV failed.")
2562 : END IF
2563 :
2564 : ! reorder the coefficients
2565 96 : coeffs = 0.0_dp
2566 20 : coeffs(nvec) = -1.0_dp
2567 52 : DO i = 1, ntr
2568 32 : coeffs(i) = coeffs(i) + b(i)
2569 52 : coeffs(nvec - i) = coeffs(nvec - i) + b(i)
2570 : END DO
2571 :
2572 : ! as a consistency check, print how well the current overlap
2573 : ! is approximated by the linear combination of previous overlaps
2574 20 : IF (print_level > low_print_level) THEN
2575 20 : CALL dbcsr_copy(tmp_i, current_overlap)
2576 96 : DO i = 1, nvec
2577 76 : state => wfi_get_snapshot(wf_history, wf_index=i)
2578 96 : CALL dbcsr_add(tmp_i, state%overlap, 1.0_dp, -coeffs(i))
2579 : END DO
2580 20 : error = dbcsr_frobenius_norm(tmp_i)/dbcsr_frobenius_norm(state%overlap)
2581 20 : IF (io_unit > 0) THEN
2582 10 : WRITE (UNIT=io_unit, FMT="(/,T2,A,F20.10)") "GEXT overlap fitting error:", error
2583 : END IF
2584 : END IF
2585 :
2586 : ! free the memory
2587 20 : CALL dbcsr_release(tmp_i)
2588 20 : CALL dbcsr_release(tmp_j)
2589 20 : CALL dbcsr_release(tmp_k)
2590 20 : CALL dbcsr_release(target_overlap)
2591 20 : DEALLOCATE (A, b)
2592 :
2593 44 : END SUBROUTINE tr_fitting
2594 :
2595 : ! **************************************************************************************************
2596 : !> \brief Saves scalar and occupation metadata from mo_set into a wavefunction snapshot.
2597 : !> \param mo_set ...
2598 : !> \param state ...
2599 : ! **************************************************************************************************
2600 28230 : SUBROUTINE wfs_save_mo_state(mo_set, state)
2601 : TYPE(mo_set_type), INTENT(IN) :: mo_set
2602 : TYPE(qs_wf_mo_state_type), INTENT(INOUT) :: state
2603 :
2604 : INTEGER :: nmo
2605 28230 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues, occupation_numbers
2606 :
2607 28230 : NULLIFY (eigenvalues, occupation_numbers)
2608 : CALL get_mo_set(mo_set, nmo=nmo, eigenvalues=eigenvalues, &
2609 : occupation_numbers=occupation_numbers, homo=state%homo, &
2610 : lfomo=state%lfomo, uniform_occupation=state%uniform_occupation, &
2611 28230 : kTS=state%kTS, mu=state%mu)
2612 28230 : CPASSERT(ASSOCIATED(eigenvalues))
2613 28230 : CPASSERT(ASSOCIATED(occupation_numbers))
2614 28230 : IF (ALLOCATED(state%eigenvalues)) DEALLOCATE (state%eigenvalues)
2615 84474 : ALLOCATE (state%eigenvalues(nmo))
2616 28230 : IF (ALLOCATED(state%occupation_numbers)) DEALLOCATE (state%occupation_numbers)
2617 56244 : ALLOCATE (state%occupation_numbers(nmo))
2618 370385 : state%eigenvalues(1:nmo) = eigenvalues(1:nmo)
2619 370385 : state%occupation_numbers(1:nmo) = occupation_numbers(1:nmo)
2620 :
2621 28230 : END SUBROUTINE wfs_save_mo_state
2622 :
2623 : ! **************************************************************************************************
2624 : !> \brief Restores scalar and occupation metadata from a wavefunction snapshot into mo_set.
2625 : !> \param state ...
2626 : !> \param mo_set ...
2627 : ! **************************************************************************************************
2628 18653 : SUBROUTINE wfs_restore_mo_state(state, mo_set)
2629 : TYPE(qs_wf_mo_state_type), INTENT(IN) :: state
2630 : TYPE(mo_set_type), INTENT(INOUT) :: mo_set
2631 :
2632 : INTEGER :: nmo
2633 18653 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues, occupation_numbers
2634 :
2635 18653 : NULLIFY (eigenvalues, occupation_numbers)
2636 : CALL get_mo_set(mo_set, nmo=nmo, eigenvalues=eigenvalues, &
2637 18653 : occupation_numbers=occupation_numbers)
2638 18653 : CPASSERT(ASSOCIATED(eigenvalues))
2639 18653 : CPASSERT(ASSOCIATED(occupation_numbers))
2640 18653 : CPASSERT(ALLOCATED(state%eigenvalues))
2641 18653 : CPASSERT(ALLOCATED(state%occupation_numbers))
2642 18653 : CPASSERT(SIZE(state%eigenvalues) == nmo)
2643 18653 : CPASSERT(SIZE(state%occupation_numbers) == nmo)
2644 229472 : eigenvalues(1:nmo) = state%eigenvalues(1:nmo)
2645 229472 : occupation_numbers(1:nmo) = state%occupation_numbers(1:nmo)
2646 : CALL set_mo_set(mo_set, homo=state%homo, lfomo=state%lfomo, &
2647 18653 : uniform_occupation=state%uniform_occupation, kTS=state%kTS, mu=state%mu)
2648 :
2649 18653 : END SUBROUTINE wfs_restore_mo_state
2650 :
2651 : END MODULE qs_wf_history_methods
|