(git:f824184)
Loading...
Searching...
No Matches
qs_mixing_utils.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
11 cite_reference
13 USE cp_dbcsr_api, ONLY: dbcsr_create,&
15 dbcsr_set,&
17 dbcsr_type_symmetric
21 USE kinds, ONLY: dp
22 USE mathconstants, ONLY: z_zero
24 USE pw_types, ONLY: pw_c1d_gs_type
37 USE qs_rho_types, ONLY: qs_rho_get,&
39 USE qs_scf_methods, ONLY: cp_sm_mix
40#include "./base/base_uses.f90"
41
42 IMPLICIT NONE
43
44 PRIVATE
45
46 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_mixing_utils'
47 INTEGER, PARAMETER, PRIVATE :: max_dftb_scc_vars = 1, &
48 max_xtb_scc_vars = 28
49
52
53CONTAINS
54
55! **************************************************************************************************
56!> \brief ...
57!> \param rho_ao ...
58!> \param p_delta ...
59!> \param para_env ...
60!> \param p_out ...
61!> \param delta ...
62! **************************************************************************************************
63 SUBROUTINE self_consistency_check(rho_ao, p_delta, para_env, p_out, delta)
64 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao, p_delta
65 TYPE(mp_para_env_type), POINTER :: para_env
66 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: p_out
67 REAL(kind=dp), INTENT(INOUT) :: delta
68
69 CHARACTER(len=*), PARAMETER :: routinen = 'self_consistency_check'
70
71 INTEGER :: handle, ic, ispin, nimg, nspins
72 REAL(kind=dp) :: tmp
73 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_q, p_in
74
75 CALL timeset(routinen, handle)
76
77 NULLIFY (matrix_q, p_in)
78
79 cpassert(ASSOCIATED(p_out))
80 NULLIFY (matrix_q, p_in)
81 p_in => rho_ao
82 matrix_q => p_delta
83 nspins = SIZE(p_in, 1)
84 nimg = SIZE(p_in, 2)
85
86 ! Compute the difference (p_out - p_in)and check convergence
87 delta = 0.0_dp
88 DO ispin = 1, nspins
89 DO ic = 1, nimg
90 CALL dbcsr_set(matrix_q(ispin, ic)%matrix, 0.0_dp)
91 CALL cp_sm_mix(m1=p_out(ispin, ic)%matrix, m2=p_in(ispin, ic)%matrix, &
92 p_mix=1.0_dp, delta=tmp, para_env=para_env, &
93 m3=matrix_q(ispin, ic)%matrix)
94 delta = max(tmp, delta)
95 END DO
96 END DO
97 CALL timestop(handle)
98
99 END SUBROUTINE self_consistency_check
100
101! **************************************************************************************************
102!> \brief allocation needed when density mixing is used
103!> \param qs_env ...
104!> \param mixing_method ...
105!> \param p_mix_new ...
106!> \param p_delta ...
107!> \param nspins ...
108!> \param mixing_store ...
109!> \par History
110!> 05.2009 created [MI]
111!> 08.2014 kpoints [JGH]
112!> 02.2015 changed input to make g-space mixing available in linear scaling SCF [Patrick Seewald]
113!> 02.2019 charge mixing [JGH]
114!> \author fawzi
115! **************************************************************************************************
116 SUBROUTINE mixing_allocate(qs_env, mixing_method, p_mix_new, p_delta, nspins, mixing_store)
117 TYPE(qs_environment_type), POINTER :: qs_env
118 INTEGER :: mixing_method
119 TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
120 POINTER :: p_mix_new, p_delta
121 INTEGER, INTENT(IN) :: nspins
122 TYPE(mixing_storage_type), POINTER :: mixing_store
123
124 CHARACTER(LEN=*), PARAMETER :: routinen = 'mixing_allocate'
125
126 INTEGER :: handle, i, ia, iat, ic, ikind, ispin, &
127 max_shell, na, natom, nbuffer, nel, &
128 nimg, nkind
129 LOGICAL :: charge_mixing
130 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
131 TYPE(dbcsr_type), POINTER :: refmatrix
132 TYPE(dft_control_type), POINTER :: dft_control
133 TYPE(distribution_1d_type), POINTER :: distribution_1d
134 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
135 POINTER :: sab_orb
136 TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho_atom
137
138 CALL timeset(routinen, handle)
139
140 NULLIFY (matrix_s, dft_control, sab_orb, refmatrix, rho_atom)
141 CALL get_qs_env(qs_env, &
142 sab_orb=sab_orb, &
143 matrix_s_kp=matrix_s, &
144 dft_control=dft_control)
145
146 charge_mixing = dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb &
147 .OR. dft_control%qs_control%semi_empirical
148 refmatrix => matrix_s(1, 1)%matrix
149 nimg = dft_control%nimages
150
151 ! *** allocate p_mix_new ***
152 IF (PRESENT(p_mix_new)) THEN
153 IF (.NOT. ASSOCIATED(p_mix_new)) THEN
154 CALL dbcsr_allocate_matrix_set(p_mix_new, nspins, nimg)
155 DO i = 1, nspins
156 DO ic = 1, nimg
157 ALLOCATE (p_mix_new(i, ic)%matrix)
158 CALL dbcsr_create(matrix=p_mix_new(i, ic)%matrix, template=refmatrix, &
159 name="SCF DENSITY", matrix_type=dbcsr_type_symmetric)
160 CALL cp_dbcsr_alloc_block_from_nbl(p_mix_new(i, ic)%matrix, sab_orb)
161 CALL dbcsr_set(p_mix_new(i, ic)%matrix, 0.0_dp)
162 END DO
163 END DO
164 END IF
165 END IF
166
167 ! *** allocate p_delta ***
168 IF (PRESENT(p_delta)) THEN
169 IF (mixing_method >= gspace_mixing_nr) THEN
170 IF (.NOT. ASSOCIATED(p_delta)) THEN
171 CALL dbcsr_allocate_matrix_set(p_delta, nspins, nimg)
172 DO i = 1, nspins
173 DO ic = 1, nimg
174 ALLOCATE (p_delta(i, ic)%matrix)
175 CALL dbcsr_create(matrix=p_delta(i, ic)%matrix, template=refmatrix, &
176 name="SCF DENSITY", matrix_type=dbcsr_type_symmetric)
177 CALL cp_dbcsr_alloc_block_from_nbl(p_delta(i, ic)%matrix, sab_orb)
178 CALL dbcsr_set(p_delta(i, ic)%matrix, 0.0_dp)
179 END DO
180 END DO
181 END IF
182 cpassert(ASSOCIATED(mixing_store))
183 END IF
184 END IF
185
186 IF (charge_mixing) THEN
187 ! *** allocate buffer for TB SCC variable mixing ***
188 IF (mixing_method >= gspace_mixing_nr) THEN
189 cpassert(.NOT. mixing_store%gmix_p)
190 IF (dft_control%qs_control%dftb) THEN
191 max_shell = max_dftb_scc_vars
192 ELSE IF (dft_control%qs_control%xtb) THEN
193 max_shell = max_xtb_scc_vars
194 ELSE
195 cpabort('UNKNOWN METHOD')
196 END IF
197 nbuffer = mixing_store%nbuffer
198 mixing_store%ncall = 0
199 CALL get_qs_env(qs_env, local_particles=distribution_1d)
200 nkind = SIZE(distribution_1d%n_el)
201 na = sum(distribution_1d%n_el(:))
202 IF (ASSOCIATED(mixing_store%atlist)) DEALLOCATE (mixing_store%atlist)
203 ALLOCATE (mixing_store%atlist(na))
204 mixing_store%nat_local = na
205 mixing_store%max_shell = max_shell
206 ia = 0
207 DO ikind = 1, nkind
208 nel = distribution_1d%n_el(ikind)
209 DO iat = 1, nel
210 ia = ia + 1
211 mixing_store%atlist(ia) = distribution_1d%list(ikind)%array(iat)
212 END DO
213 END DO
214 IF (ASSOCIATED(mixing_store%acharge)) DEALLOCATE (mixing_store%acharge)
215 ALLOCATE (mixing_store%acharge(na, max_shell, nbuffer))
216 IF (ASSOCIATED(mixing_store%dacharge)) DEALLOCATE (mixing_store%dacharge)
217 ALLOCATE (mixing_store%dacharge(na, max_shell, nbuffer))
218 END IF
219 IF (mixing_method == pulay_mixing_nr .OR. mixing_method == new_pulay_mixing_nr) THEN
220 IF (ASSOCIATED(mixing_store%pulay_matrix)) DEALLOCATE (mixing_store%pulay_matrix)
221 ALLOCATE (mixing_store%pulay_matrix(nbuffer, nbuffer, nspins))
222 mixing_store%pulay_matrix = 0.0_dp
223 ELSE IF (mixing_method == broyden_mixing_nr .OR. mixing_method == modified_broyden_mixing_nr) THEN
224 IF (ASSOCIATED(mixing_store%abroy)) DEALLOCATE (mixing_store%abroy)
225 ALLOCATE (mixing_store%abroy(nbuffer, nbuffer))
226 mixing_store%abroy = 0.0_dp
227 IF (ASSOCIATED(mixing_store%wbroy)) DEALLOCATE (mixing_store%wbroy)
228 ALLOCATE (mixing_store%wbroy(nbuffer))
229 mixing_store%wbroy = 0.0_dp
230 IF (ASSOCIATED(mixing_store%dfbroy)) DEALLOCATE (mixing_store%dfbroy)
231 ALLOCATE (mixing_store%dfbroy(na, max_shell, nbuffer))
232 mixing_store%dfbroy = 0.0_dp
233 IF (ASSOCIATED(mixing_store%ubroy)) DEALLOCATE (mixing_store%ubroy)
234 ALLOCATE (mixing_store%ubroy(na, max_shell, nbuffer))
235 mixing_store%ubroy = 0.0_dp
236 ELSE IF (mixing_method == multisecant_mixing_nr) THEN
237 cpabort("multisecant_mixing not available")
238 END IF
239 ELSE
240 ! *** allocate buffer for gspace mixing ***
241 IF (mixing_method >= gspace_mixing_nr) THEN
242 nbuffer = mixing_store%nbuffer
243 mixing_store%ncall = 0
244 mixing_store%ncall_p = 0
245 IF (.NOT. ASSOCIATED(mixing_store%rhoin)) THEN
246 ALLOCATE (mixing_store%rhoin(nspins))
247 DO ispin = 1, nspins
248 NULLIFY (mixing_store%rhoin(ispin)%cc)
249 END DO
250 END IF
251
252 IF (mixing_store%gmix_p .AND. dft_control%qs_control%gapw) THEN
253 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
254 natom = SIZE(rho_atom)
255 IF (.NOT. ASSOCIATED(mixing_store%paw)) THEN
256 ALLOCATE (mixing_store%paw(natom))
257 mixing_store%paw = .false.
258 ALLOCATE (mixing_store%cpc_h_in(natom, nspins))
259 ALLOCATE (mixing_store%cpc_s_in(natom, nspins))
260 DO ispin = 1, nspins
261 DO iat = 1, natom
262 NULLIFY (mixing_store%cpc_h_in(iat, ispin)%r_coef)
263 NULLIFY (mixing_store%cpc_s_in(iat, ispin)%r_coef)
264 END DO
265 END DO
266 END IF
267 END IF
268 END IF
269
270 ! *** allocare res_buffer if needed
271 IF (mixing_method >= pulay_mixing_nr) THEN
272 IF (.NOT. ASSOCIATED(mixing_store%res_buffer)) THEN
273 ALLOCATE (mixing_store%res_buffer(nbuffer, nspins))
274 DO ispin = 1, nspins
275 DO i = 1, nbuffer
276 NULLIFY (mixing_store%res_buffer(i, ispin)%cc)
277 END DO
278 END DO
279 END IF
280 END IF
281
282 ! *** allocate pulay
283 IF (mixing_method == pulay_mixing_nr .OR. mixing_method == new_pulay_mixing_nr) THEN
284 IF (.NOT. ASSOCIATED(mixing_store%pulay_matrix)) THEN
285 ALLOCATE (mixing_store%pulay_matrix(nbuffer, nbuffer, nspins))
286 END IF
287
288 IF (.NOT. ASSOCIATED(mixing_store%rhoin_buffer)) THEN
289 ALLOCATE (mixing_store%rhoin_buffer(nbuffer, nspins))
290 DO ispin = 1, nspins
291 DO i = 1, nbuffer
292 NULLIFY (mixing_store%rhoin_buffer(i, ispin)%cc)
293 END DO
294 END DO
295 END IF
296 IF (mixing_store%gmix_p) THEN
297 IF (dft_control%qs_control%gapw) THEN
298 IF (.NOT. ASSOCIATED(mixing_store%cpc_h_in_buffer)) THEN
299 ALLOCATE (mixing_store%cpc_h_in_buffer(nbuffer, natom, nspins))
300 ALLOCATE (mixing_store%cpc_s_in_buffer(nbuffer, natom, nspins))
301 ALLOCATE (mixing_store%cpc_h_res_buffer(nbuffer, natom, nspins))
302 ALLOCATE (mixing_store%cpc_s_res_buffer(nbuffer, natom, nspins))
303 DO ispin = 1, nspins
304 DO iat = 1, natom
305 DO i = 1, nbuffer
306 NULLIFY (mixing_store%cpc_h_in_buffer(i, iat, ispin)%r_coef)
307 NULLIFY (mixing_store%cpc_s_in_buffer(i, iat, ispin)%r_coef)
308 NULLIFY (mixing_store%cpc_h_res_buffer(i, iat, ispin)%r_coef)
309 NULLIFY (mixing_store%cpc_s_res_buffer(i, iat, ispin)%r_coef)
310 END DO
311 END DO
312 END DO
313 END IF
314 END IF
315 END IF
316
317 END IF
318 ! *** allocate broyden buffer ***
319 IF (mixing_method == broyden_mixing_nr .OR. mixing_method == modified_broyden_mixing_nr) THEN
320 IF (.NOT. ASSOCIATED(mixing_store%rhoin_old)) THEN
321 ALLOCATE (mixing_store%rhoin_old(nspins))
322 DO ispin = 1, nspins
323 NULLIFY (mixing_store%rhoin_old(ispin)%cc)
324 END DO
325 END IF
326 IF (.NOT. ASSOCIATED(mixing_store%drho_buffer)) THEN
327 ALLOCATE (mixing_store%drho_buffer(nbuffer, nspins))
328 ALLOCATE (mixing_store%last_res(nspins))
329 DO ispin = 1, nspins
330 DO i = 1, nbuffer
331 NULLIFY (mixing_store%drho_buffer(i, ispin)%cc)
332 END DO
333 NULLIFY (mixing_store%last_res(ispin)%cc)
334 END DO
335 END IF
336 IF (mixing_method == modified_broyden_mixing_nr) THEN
337 IF (.NOT. ASSOCIATED(mixing_store%weight)) THEN
338 ALLOCATE (mixing_store%weight(nbuffer, nspins))
339 END IF
340 mixing_store%weight = 0.0_dp
341 END IF
342 IF (mixing_store%gmix_p) THEN
343
344 IF (dft_control%qs_control%gapw) THEN
345 IF (.NOT. ASSOCIATED(mixing_store%cpc_h_old)) THEN
346 ALLOCATE (mixing_store%cpc_h_old(natom, nspins))
347 ALLOCATE (mixing_store%cpc_s_old(natom, nspins))
348 DO ispin = 1, nspins
349 DO iat = 1, natom
350 NULLIFY (mixing_store%cpc_h_old(iat, ispin)%r_coef)
351 NULLIFY (mixing_store%cpc_s_old(iat, ispin)%r_coef)
352 END DO
353 END DO
354 END IF
355 IF (.NOT. ASSOCIATED(mixing_store%dcpc_h_in)) THEN
356 ALLOCATE (mixing_store%dcpc_h_in(nbuffer, natom, nspins))
357 ALLOCATE (mixing_store%dcpc_s_in(nbuffer, natom, nspins))
358 ALLOCATE (mixing_store%cpc_h_lastres(natom, nspins))
359 ALLOCATE (mixing_store%cpc_s_lastres(natom, nspins))
360 DO ispin = 1, nspins
361 DO iat = 1, natom
362 DO i = 1, nbuffer
363 NULLIFY (mixing_store%dcpc_h_in(i, iat, ispin)%r_coef)
364 NULLIFY (mixing_store%dcpc_s_in(i, iat, ispin)%r_coef)
365 END DO
366 NULLIFY (mixing_store%cpc_h_lastres(iat, ispin)%r_coef)
367 NULLIFY (mixing_store%cpc_s_lastres(iat, ispin)%r_coef)
368 END DO
369 END DO
370 END IF
371 END IF
372 END IF
373 END IF
374
375 ! *** allocate multisecant buffer ***
376 IF (mixing_method == multisecant_mixing_nr) THEN
377 IF (.NOT. ASSOCIATED(mixing_store%norm_res_buffer)) THEN
378 ALLOCATE (mixing_store%norm_res_buffer(nbuffer, nspins))
379 END IF
380 END IF
381
382 IF (mixing_method == multisecant_mixing_nr) THEN
383 IF (.NOT. ASSOCIATED(mixing_store%rhoin_buffer)) THEN
384 ALLOCATE (mixing_store%rhoin_buffer(nbuffer, nspins))
385 DO ispin = 1, nspins
386 DO i = 1, nbuffer
387 NULLIFY (mixing_store%rhoin_buffer(i, ispin)%cc)
388 END DO
389 END DO
390 END IF
391 END IF
392
393 END IF
394
395 CALL timestop(handle)
396
397 END SUBROUTINE mixing_allocate
398
399! **************************************************************************************************
400!> \brief Allocate or resize a mixing vector, optionally clearing its contents.
401!> \param buffer Vector or array of vectors.
402!> \param n Local vector length, including any auxiliary coordinates.
403!> \param clear Whether to clear the vector; Pulay retains history across SCF runs.
404! **************************************************************************************************
405 ELEMENTAL SUBROUTINE mixing_buffer_allocate(buffer, n, clear)
406 TYPE(cp_1d_z_p_type), INTENT(INOUT) :: buffer
407 INTEGER, INTENT(IN) :: n
408 LOGICAL, INTENT(IN) :: clear
409
410 IF (ASSOCIATED(buffer%cc)) THEN
411 IF (SIZE(buffer%cc) /= n) DEALLOCATE (buffer%cc)
412 END IF
413 IF (.NOT. ASSOCIATED(buffer%cc)) ALLOCATE (buffer%cc(n))
414 IF (clear) buffer%cc = z_zero
415 END SUBROUTINE mixing_buffer_allocate
416
417! **************************************************************************************************
418!> \brief View the physical density, appending auxiliary coordinates on their owning rank.
419!> \param mixing_store Owns the optional enlarged work buffer.
420!> \param rho_g Physical plane-wave density; its storage and grid are unchanged.
421!> \param density Vectors passed to the existing mixing algorithms.
422!> \param auxiliary Replicated density coordinates in electrons, scaled to Fourier-density units.
423! **************************************************************************************************
424 SUBROUTINE mixing_density(mixing_store, rho_g, density, auxiliary)
425 TYPE(mixing_storage_type), POINTER :: mixing_store
426 TYPE(pw_c1d_gs_type), INTENT(IN) :: rho_g(:)
427 TYPE(cp_1d_z_p_type), ALLOCATABLE, INTENT(OUT) :: density(:)
428 REAL(kind=dp), INTENT(IN), OPTIONAL :: auxiliary(:, :)
429
430 INTEGER :: ispin, ng
431
432 IF (mixing_store%naux > 0) THEN
433 cpassert(PRESENT(auxiliary))
434 cpassert(SIZE(auxiliary, 1) == mixing_store%naux)
435 cpassert(SIZE(auxiliary, 2) == SIZE(rho_g))
436 END IF
437 ng = SIZE(rho_g(1)%array)
438 ALLOCATE (density(SIZE(rho_g)))
439 DO ispin = 1, SIZE(rho_g)
440 density(ispin)%cc => rho_g(ispin)%array
441 IF (ALLOCATED(mixing_store%aux_buffer)) THEN
442 density(ispin)%cc => mixing_store%aux_buffer(:, ispin)
443 density(ispin)%cc(:ng) = rho_g(ispin)%array
444 density(ispin)%cc(ng + 1:) = auxiliary(:, ispin)/rho_g(ispin)%pw_grid%vol
445 END IF
446 END DO
447 END SUBROUTINE mixing_density
448
449! **************************************************************************************************
450!> \brief initialiation needed when gspace mixing is used
451!> \param mixing_method ...
452!> \param rho ...
453!> \param mixing_store ...
454!> \param para_env ...
455!> \param rho_atom ...
456!> \param auxiliary Replicated real density coordinates, included once in the mixing metric.
457!> \par History
458!> 05.2009 created [MI]
459!> \author MI
460! **************************************************************************************************
461 SUBROUTINE mixing_init(mixing_method, rho, mixing_store, para_env, rho_atom, auxiliary)
462 INTEGER, INTENT(IN) :: mixing_method
463 TYPE(qs_rho_type), POINTER :: rho
464 TYPE(mixing_storage_type), POINTER :: mixing_store
465 TYPE(mp_para_env_type), POINTER :: para_env
466 TYPE(rho_atom_type), DIMENSION(:), OPTIONAL, &
467 POINTER :: rho_atom
468 REAL(kind=dp), INTENT(IN), OPTIONAL :: auxiliary(:, :)
469
470 CHARACTER(len=*), PARAMETER :: routinen = 'mixing_init'
471
472 INTEGER :: handle, iat, ib, icoef1, icoef2, ig, ig1, ig_count, iproc, ispin, n1, n2, natom, &
473 nbuffer, ncoef_h1, ncoef_h2, ncoef_s1, ncoef_s2, ng, nimg, nspin, nvec
474 REAL(dp) :: bconst, beta, cpc_h_tmp, cpc_s_tmp, &
475 fdamp, g2max, g2min, kmin, qk, qkappa, &
476 qm
477 REAL(dp), DIMENSION(:), POINTER :: g2
478 REAL(dp), DIMENSION(:, :), POINTER :: g_vec
479 TYPE(cp_1d_z_p_type), ALLOCATABLE :: density(:)
480 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
481 TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g
482
483 CALL timeset(routinen, handle)
484
485 NULLIFY (g2, g_vec, rho_ao_kp, rho_g)
486 CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp, rho_g=rho_g)
487
488 nspin = SIZE(rho_g)
489 ng = SIZE(rho_g(1)%pw_grid%gsq, 1)
490 nimg = SIZE(rho_ao_kp, 2)
491 IF (PRESENT(auxiliary) .OR. mixing_store%naux > 0) THEN
492 mixing_store%ncall = 0
493 mixing_store%ncall_p = 0
494 END IF
495 mixing_store%naux = 0
496 IF (PRESENT(auxiliary)) mixing_store%naux = SIZE(auxiliary, 1)
497 nvec = ng
498 IF (ALLOCATED(mixing_store%aux_buffer)) DEALLOCATE (mixing_store%aux_buffer)
499 IF (mixing_store%naux > 0) THEN
500 IF (para_env%mepos == 0) THEN
501 nvec = ng + mixing_store%naux
502 ALLOCATE (mixing_store%aux_buffer(nvec, nspin))
503 END IF
504 END IF
505 CALL mixing_density(mixing_store, rho_g, density, auxiliary)
506 mixing_store%ig_max = ng
507 g2 => rho_g(1)%pw_grid%gsq
508 g_vec => rho_g(1)%pw_grid%g
509
510 IF (mixing_store%max_gvec_exp > 0._dp) THEN
511 DO ig = 1, ng
512 IF (g2(ig) > mixing_store%max_g2) THEN
513 mixing_store%ig_max = ig
514 EXIT
515 END IF
516 END DO
517 END IF
518
519 IF (ASSOCIATED(mixing_store%kerker_factor)) DEALLOCATE (mixing_store%kerker_factor)
520 ALLOCATE (mixing_store%kerker_factor(nvec))
521 IF (ASSOCIATED(mixing_store%kerker_factor_mag)) DEALLOCATE (mixing_store%kerker_factor_mag)
522 ALLOCATE (mixing_store%kerker_factor_mag(nvec))
523 IF (ASSOCIATED(mixing_store%special_metric)) DEALLOCATE (mixing_store%special_metric)
524 ALLOCATE (mixing_store%special_metric(nvec))
525 beta = mixing_store%beta
526 kmin = 0.1_dp
527 mixing_store%kerker_factor = 1.0_dp
528 mixing_store%kerker_factor_mag = 1.0_dp
529 mixing_store%special_metric = 1.0_dp
530 ig1 = 1
531
532 IF (.NOT. (mixing_method == new_pulay_mixing_nr)) THEN
533 IF (rho_g(1)%pw_grid%have_g0) ig1 = 2
534 DO ig = ig1, mixing_store%ig_max
535 mixing_store%kerker_factor(ig) = max(g2(ig)/(g2(ig) + beta*beta), kmin)
536 IF (mixing_store%beta_mag > 1.0e-10_dp) THEN
537 mixing_store%kerker_factor_mag(ig) = max(g2(ig)/(g2(ig) + mixing_store%beta_mag*mixing_store%beta_mag), kmin)
538 ELSE
539 ! beta_mag ~ 0 means no Kerker screening on the magnetization channel
540 mixing_store%kerker_factor_mag(ig) = 1.0_dp
541 END IF
542 mixing_store%special_metric(ig) = &
543 1.0_dp + 50.0_dp/8.0_dp*( &
544 1.0_dp + cos(g_vec(1, ig)) + cos(g_vec(2, ig)) + cos(g_vec(3, ig)) + &
545 cos(g_vec(1, ig))*cos(g_vec(2, ig)) + &
546 cos(g_vec(2, ig))*cos(g_vec(3, ig)) + &
547 cos(g_vec(1, ig))*cos(g_vec(3, ig)) + &
548 cos(g_vec(1, ig))*cos(g_vec(2, ig))*cos(g_vec(3, ig)))
549 END DO
550 ELSE
551 CALL cite_reference(sundararaman2017)
552 qkappa = mixing_store%qkappa
553 qk = mixing_store%qk
554 qm = mixing_store%qm
555 IF (rho_g(1)%pw_grid%have_g0) ig1 = 1
556 DO ig = ig1, mixing_store%ig_max
557 mixing_store%kerker_factor(ig) = (g2(ig) + qkappa*qkappa)/(g2(ig) + qkappa*qkappa + qk*qk)
558 mixing_store%special_metric(ig) = (g2(ig) + qkappa*qkappa + qm*qm)/(g2(ig) + qkappa*qkappa)
559 END DO
560 END IF
561 nbuffer = mixing_store%nbuffer
562 CALL mixing_buffer_allocate(mixing_store%rhoin, nvec, .false.)
563 IF (ASSOCIATED(mixing_store%rhoin_buffer)) CALL mixing_buffer_allocate(mixing_store%rhoin_buffer, nvec, .false.)
564 IF (ASSOCIATED(mixing_store%res_buffer)) CALL mixing_buffer_allocate(mixing_store%res_buffer, nvec, .false.)
565 DO ispin = 1, nspin
566 mixing_store%rhoin(ispin)%cc = density(ispin)%cc
567 END DO
568 IF (nspin == 2) THEN
569 mixing_store%rhoin(1)%cc = density(1)%cc + density(2)%cc
570 mixing_store%rhoin(2)%cc = density(1)%cc - density(2)%cc
571 END IF
572 IF (ASSOCIATED(mixing_store%rhoin_buffer)) THEN
573 DO ispin = 1, nspin
574 mixing_store%rhoin_buffer(1, ispin)%cc = mixing_store%rhoin(ispin)%cc
575 END DO
576 END IF
577
578 IF (mixing_store%gmix_p) THEN
579 IF (PRESENT(rho_atom)) THEN
580 natom = SIZE(rho_atom)
581 DO ispin = 1, nspin
582 DO iat = 1, natom
583 IF (ASSOCIATED(rho_atom(iat)%cpc_s(ispin)%r_coef)) THEN
584 mixing_store%paw(iat) = .true.
585 n1 = SIZE(rho_atom(iat)%cpc_s(ispin)%r_coef, 1)
586 n2 = SIZE(rho_atom(iat)%cpc_s(ispin)%r_coef, 2)
587 IF (ASSOCIATED(mixing_store%cpc_s_in)) THEN
588 IF (.NOT. ASSOCIATED(mixing_store%cpc_s_in(iat, ispin)%r_coef)) THEN
589 ALLOCATE (mixing_store%cpc_s_in(iat, ispin)%r_coef(n1, n2))
590 ALLOCATE (mixing_store%cpc_h_in(iat, ispin)%r_coef(n1, n2))
591 END IF
592 mixing_store%cpc_h_in(iat, ispin)%r_coef = rho_atom(iat)%cpc_h(ispin)%r_coef
593 mixing_store%cpc_s_in(iat, ispin)%r_coef = rho_atom(iat)%cpc_s(ispin)%r_coef
594 END IF
595 END IF
596 END DO
597 END DO
598 IF (nspin == 2 .AND. ASSOCIATED(mixing_store%cpc_s_in)) THEN
599 DO iat = 1, natom
600 IF (mixing_store%paw(iat)) THEN
601 cpassert(ASSOCIATED(mixing_store%cpc_h_in(iat, 1)%r_coef))
602 cpassert(ASSOCIATED(mixing_store%cpc_h_in(iat, 2)%r_coef))
603 cpassert(ASSOCIATED(mixing_store%cpc_s_in(iat, 1)%r_coef))
604 cpassert(ASSOCIATED(mixing_store%cpc_s_in(iat, 2)%r_coef))
605
606 ncoef_h1 = SIZE(mixing_store%cpc_h_in(iat, 1)%r_coef, 1)
607 ncoef_h2 = SIZE(mixing_store%cpc_h_in(iat, 1)%r_coef, 2)
608 ncoef_s1 = SIZE(mixing_store%cpc_s_in(iat, 1)%r_coef, 1)
609 ncoef_s2 = SIZE(mixing_store%cpc_s_in(iat, 1)%r_coef, 2)
610 cpassert(ncoef_h1 == SIZE(mixing_store%cpc_h_in(iat, 2)%r_coef, 1))
611 cpassert(ncoef_h2 == SIZE(mixing_store%cpc_h_in(iat, 2)%r_coef, 2))
612 cpassert(ncoef_s1 == SIZE(mixing_store%cpc_s_in(iat, 2)%r_coef, 1))
613 cpassert(ncoef_s2 == SIZE(mixing_store%cpc_s_in(iat, 2)%r_coef, 2))
614
615 DO icoef2 = 1, ncoef_h2
616 DO icoef1 = 1, ncoef_h1
617 cpc_h_tmp = mixing_store%cpc_h_in(iat, 1)%r_coef(icoef1, icoef2)
618 mixing_store%cpc_h_in(iat, 1)%r_coef(icoef1, icoef2) = &
619 cpc_h_tmp + mixing_store%cpc_h_in(iat, 2)%r_coef(icoef1, icoef2)
620 mixing_store%cpc_h_in(iat, 2)%r_coef(icoef1, icoef2) = &
621 cpc_h_tmp - mixing_store%cpc_h_in(iat, 2)%r_coef(icoef1, icoef2)
622 END DO
623 END DO
624
625 DO icoef2 = 1, ncoef_s2
626 DO icoef1 = 1, ncoef_s1
627 cpc_s_tmp = mixing_store%cpc_s_in(iat, 1)%r_coef(icoef1, icoef2)
628 mixing_store%cpc_s_in(iat, 1)%r_coef(icoef1, icoef2) = &
629 cpc_s_tmp + mixing_store%cpc_s_in(iat, 2)%r_coef(icoef1, icoef2)
630 mixing_store%cpc_s_in(iat, 2)%r_coef(icoef1, icoef2) = &
631 cpc_s_tmp - mixing_store%cpc_s_in(iat, 2)%r_coef(icoef1, icoef2)
632 END DO
633 END DO
634 END IF
635 END DO
636 END IF
637 END IF
638 END IF
639
640 IF (mixing_method == gspace_mixing_nr) THEN
641 ELSE IF (mixing_method == pulay_mixing_nr .OR. mixing_method == new_pulay_mixing_nr) THEN
642 IF (mixing_store%gmix_p .AND. PRESENT(rho_atom)) THEN
643 DO ispin = 1, nspin
644 DO iat = 1, natom
645 IF (mixing_store%paw(iat)) THEN
646 n1 = SIZE(rho_atom(iat)%cpc_s(ispin)%r_coef, 1)
647 n2 = SIZE(rho_atom(iat)%cpc_s(ispin)%r_coef, 2)
648 IF (.NOT. ASSOCIATED(mixing_store%cpc_h_in_buffer(1, iat, ispin)%r_coef)) THEN
649 DO ib = 1, nbuffer
650 ALLOCATE (mixing_store%cpc_s_in_buffer(ib, iat, ispin)%r_coef(n1, n2))
651 ALLOCATE (mixing_store%cpc_h_in_buffer(ib, iat, ispin)%r_coef(n1, n2))
652 ALLOCATE (mixing_store%cpc_s_res_buffer(ib, iat, ispin)%r_coef(n1, n2))
653 ALLOCATE (mixing_store%cpc_h_res_buffer(ib, iat, ispin)%r_coef(n1, n2))
654 END DO
655 END IF
656 DO ib = 1, nbuffer
657 mixing_store%cpc_h_in_buffer(ib, iat, ispin)%r_coef = 0.0_dp
658 mixing_store%cpc_s_in_buffer(ib, iat, ispin)%r_coef = 0.0_dp
659 mixing_store%cpc_h_res_buffer(ib, iat, ispin)%r_coef = 0.0_dp
660 mixing_store%cpc_s_res_buffer(ib, iat, ispin)%r_coef = 0.0_dp
661 END DO
662 END IF
663 END DO
664 END DO
665 END IF
666 ELSE IF (mixing_method == broyden_mixing_nr .OR. mixing_method == modified_broyden_mixing_nr) THEN
667 CALL mixing_buffer_allocate(mixing_store%rhoin_old, nvec, .true.)
668 CALL mixing_buffer_allocate(mixing_store%drho_buffer, nvec, .true.)
669 CALL mixing_buffer_allocate(mixing_store%last_res, nvec, .true.)
670 IF (mixing_store%gmix_p) THEN
671 IF (PRESENT(rho_atom)) THEN
672 DO ispin = 1, nspin
673 DO iat = 1, natom
674 IF (mixing_store%paw(iat)) THEN
675 n1 = SIZE(rho_atom(iat)%cpc_s(ispin)%r_coef, 1)
676 n2 = SIZE(rho_atom(iat)%cpc_s(ispin)%r_coef, 2)
677 IF (.NOT. ASSOCIATED(mixing_store%cpc_s_old(iat, ispin)%r_coef)) THEN
678 ALLOCATE (mixing_store%cpc_s_old(iat, ispin)%r_coef(n1, n2))
679 ALLOCATE (mixing_store%cpc_h_old(iat, ispin)%r_coef(n1, n2))
680 END IF
681 mixing_store%cpc_h_old(iat, ispin)%r_coef = 0.0_dp
682 mixing_store%cpc_s_old(iat, ispin)%r_coef = 0.0_dp
683 IF (.NOT. ASSOCIATED(mixing_store%dcpc_s_in(1, iat, ispin)%r_coef)) THEN
684 DO ib = 1, nbuffer
685 ALLOCATE (mixing_store%dcpc_h_in(ib, iat, ispin)%r_coef(n1, n2))
686 ALLOCATE (mixing_store%dcpc_s_in(ib, iat, ispin)%r_coef(n1, n2))
687 END DO
688 ALLOCATE (mixing_store%cpc_h_lastres(iat, ispin)%r_coef(n1, n2))
689 ALLOCATE (mixing_store%cpc_s_lastres(iat, ispin)%r_coef(n1, n2))
690 END IF
691 DO ib = 1, nbuffer
692 mixing_store%dcpc_h_in(ib, iat, ispin)%r_coef = 0.0_dp
693 mixing_store%dcpc_s_in(ib, iat, ispin)%r_coef = 0.0_dp
694 END DO
695 mixing_store%cpc_h_lastres(iat, ispin)%r_coef = 0.0_dp
696 mixing_store%cpc_s_lastres(iat, ispin)%r_coef = 0.0_dp
697 END IF
698 END DO
699 END DO
700 END IF
701 END IF
702
703 IF (ASSOCIATED(mixing_store%p_metric)) DEALLOCATE (mixing_store%p_metric)
704 ALLOCATE (mixing_store%p_metric(nvec))
705 mixing_store%p_metric = 1.0_dp
706 bconst = mixing_store%bconst
707 g2min = 1.e30_dp
708 DO ig = 1, ng
709 IF (g2(ig) > 1.e-10_dp) g2min = min(g2min, g2(ig))
710 END DO
711 g2max = -1.e30_dp
712 DO ig = 1, ng
713 g2max = max(g2max, g2(ig))
714 END DO
715 CALL para_env%min(g2min)
716 CALL para_env%max(g2max)
717 ! fdamp/g2 varies between (bconst-1) and 0
718 ! i.e. p_metric varies between bconst and 1
719 ! fdamp = (bconst-1.0_dp)*g2min
720 fdamp = (bconst - 1.0_dp)*g2min*g2max/(g2max - g2min*bconst)
721 DO ig = 1, ng
722 mixing_store%p_metric(ig) = (g2(ig) + fdamp)/max(g2(ig), 1.e-10_dp)
723 END DO
724 IF (rho_g(1)%pw_grid%have_g0) mixing_store%p_metric(1) = bconst
725 ! Keep the original G cutoff; auxiliary coordinates have unit metric.
726 IF (nvec > ng) mixing_store%p_metric(mixing_store%ig_max + 1:ng) = 0.0_dp
727 ELSE IF (mixing_method == multisecant_mixing_nr) THEN
728 IF (ASSOCIATED(mixing_store%ig_global_index)) DEALLOCATE (mixing_store%ig_global_index)
729 ALLOCATE (mixing_store%ig_global_index(nvec))
730 mixing_store%ig_global_index = 0
731 ig_count = 0
732 DO iproc = 0, para_env%num_pe - 1
733 IF (para_env%mepos == iproc) THEN
734 DO ig = 1, nvec
735 ig_count = ig_count + 1
736 mixing_store%ig_global_index(ig) = ig_count
737 END DO
738 END IF
739 CALL para_env%bcast(ig_count, iproc)
740 END DO
741 END IF
742
743 IF (nvec > ng) mixing_store%ig_max = nvec
744 CALL timestop(handle)
745
746 END SUBROUTINE mixing_init
747
748! **************************************************************************************************
749!> \brief initialiation needed when charge mixing is used
750!> \param mixing_store ...
751!> \par History
752!> 02.2019 created [JGH]
753!> \author JGH
754! **************************************************************************************************
755 ELEMENTAL SUBROUTINE charge_mixing_init(mixing_store)
756 TYPE(mixing_storage_type), INTENT(INOUT) :: mixing_store
757
758 mixing_store%ncall = 0
759 mixing_store%tb_scc_mixer_error = 0.0_dp
760 mixing_store%tb_scc_mixer_step = 0
761 IF (ASSOCIATED(mixing_store%acharge)) mixing_store%acharge = 0.0_dp
762 IF (ASSOCIATED(mixing_store%dacharge)) mixing_store%dacharge = 0.0_dp
763
764 END SUBROUTINE charge_mixing_init
765
766END MODULE qs_mixing_utils
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public sundararaman2017
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_set(matrix, alpha)
...
DBCSR operations in CP2K.
stores a lists of integer that are local to a processor. The idea is that these integers represent ob...
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_zero
Interface to the message passing library MPI.
module that contains the definitions of the scf types
integer, parameter, public new_pulay_mixing_nr
integer, parameter, public broyden_mixing_nr
integer, parameter, public modified_broyden_mixing_nr
integer, parameter, public multisecant_mixing_nr
integer, parameter, public pulay_mixing_nr
integer, parameter, public gspace_mixing_nr
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
elemental subroutine, public charge_mixing_init(mixing_store)
initialiation needed when charge mixing is used
subroutine, public mixing_density(mixing_store, rho_g, density, auxiliary)
View the physical density, appending auxiliary coordinates on their owning rank.
subroutine, public mixing_init(mixing_method, rho, mixing_store, para_env, rho_atom, auxiliary)
initialiation needed when gspace mixing is used
subroutine, public mixing_allocate(qs_env, mixing_method, p_mix_new, p_delta, nspins, mixing_store)
allocation needed when density mixing is used
subroutine, public self_consistency_check(rho_ao, p_delta, para_env, p_out, delta)
...
Define the neighbor list data types and the corresponding functionality.
superstucture that hold various representations of the density and keeps track of which ones are vali...
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
groups fairly general SCF methods, so that modules other than qs_scf can use them too split off from ...
subroutine, public cp_sm_mix(m1, m2, p_mix, delta, para_env, m3)
Perform a mixing of the given matrixes into the first matrix m1 = m2 + p_mix (m1-m2).
structure to store local (to a processor) ordered lists of integers.
stores all the informations relevant to an mpi environment
keeps the density in various representations, keeping track of which ones are valid.