39#include "./base/base_uses.f90"
45 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'qs_gspace_mixing'
67 SUBROUTINE gspace_mixing(qs_env, mixing_method, mixing_store, rho, para_env, iter_count, auxiliary)
69 INTEGER :: mixing_method
74 REAL(kind=
dp),
INTENT(INOUT),
OPTIONAL :: auxiliary(:, :)
76 CHARACTER(len=*),
PARAMETER :: routinen =
'gspace_mixing'
78 COMPLEX(KIND=dp) :: tmp
79 INTEGER :: handle, iatom, ig, ispin, natom, ng, &
82 REAL(kind=
dp),
DIMENSION(:),
POINTER :: tot_rho_r
89 CALL timeset(routinen, handle)
91 CALL get_qs_env(qs_env, dft_control=dft_control)
92 IF (.NOT. (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
93 dft_control%qs_control%semi_empirical))
THEN
95 cpassert(
ASSOCIATED(mixing_store))
96 NULLIFY (rho_atom, rho_g, rho_r, tot_rho_r)
97 CALL qs_rho_get(rho, rho_g=rho_g, rho_r=rho_r, tot_rho_r=tot_rho_r)
99 nspin =
SIZE(rho_g, 1)
101 ng =
SIZE(density(1)%cc)
102 cpassert(ng ==
SIZE(mixing_store%rhoin(1)%cc))
103 gapw = dft_control%qs_control%gapw
106 IF (mixing_store%gmix_p .AND. gapw)
THEN
107 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
108 natom =
SIZE(rho_atom)
113 tmp = density(1)%cc(ig)
114 density(1)%cc(ig) = tmp + density(2)%cc(ig)
115 density(2)%cc(ig) = tmp - density(2)%cc(ig)
117 IF (mixing_store%gmix_p .AND. gapw)
THEN
118 CALL gapw_cpc_spin_to_charge_mag(rho_atom, mixing_store)
122 IF (iter_count + 1 <= mixing_store%nskip_mixing)
THEN
126 mixing_store%rhoin(ispin)%cc(ig) = density(ispin)%cc(ig)
129 IF (mixing_store%gmix_p .AND. gapw)
THEN
132 IF (mixing_store%paw(iatom))
THEN
133 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
134 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
140 mixing_store%iter_method =
"NoMix"
141 ELSE IF ((iter_count + 1 - mixing_store%nskip_mixing) <= mixing_store%n_simple_mix)
THEN
143 CALL gmix_potential_only(qs_env, mixing_store, density)
144 mixing_store%iter_method =
"Kerker"
147 CALL gmix_potential_only(qs_env, mixing_store, density)
148 mixing_store%iter_method =
"Kerker"
150 CALL pulay_mixing(qs_env, mixing_store, density, para_env, rho_g(1)%pw_grid%vol)
151 mixing_store%iter_method =
"Pulay"
153 CALL pulay_mixing(qs_env, mixing_store, density, para_env, rho_g(1)%pw_grid%vol)
154 mixing_store%iter_method =
"NPulay"
157 CALL broyden_mixing(qs_env, mixing_store, density, para_env)
158 mixing_store%iter_method =
"Broy."
161 CALL modified_broyden_mixing(qs_env, mixing_store, density, para_env)
162 mixing_store%iter_method =
"MBroy"
165 cpassert(rho_g(1)%pw_grid%ngpts < huge(ng) - mixing_store%naux)
166 CALL multisecant_mixing(mixing_store, density, para_env, rho_g(1)%pw_grid%gsq, &
167 int(rho_g(1)%pw_grid%ngpts) + mixing_store%naux)
168 mixing_store%iter_method =
"MSec."
172 IF (mixing_store%gmix_p .AND. gapw)
THEN
173 CALL gapw_cpc_charge_mag_to_spin(rho_atom, mixing_store)
176 tmp = density(1)%cc(ig)
177 density(1)%cc(ig) = 0.5_dp*(tmp + density(2)%cc(ig))
178 density(2)%cc(ig) = 0.5_dp*(tmp - density(2)%cc(ig))
182 IF (mixing_store%naux > 0)
THEN
183 IF (
ALLOCATED(mixing_store%aux_buffer))
THEN
184 ng =
SIZE(rho_g(1)%array)
186 rho_g(ispin)%array = density(ispin)%cc(:ng)
187 auxiliary(:, ispin) = real(density(ispin)%cc(ng + 1:),
dp)*rho_g(ispin)%pw_grid%vol
190 CALL para_env%bcast(auxiliary, 0)
193 IF (iter_count + 1 <= mixing_store%nskip_mixing)
THEN
194 CALL timestop(handle)
203 CALL timestop(handle)
213 SUBROUTINE gapw_cpc_spin_to_charge_mag(rho_atom, mixing_store)
217 INTEGER :: iatom, icoef1, icoef2, ncoef_h1, &
218 ncoef_h2, ncoef_s1, ncoef_s2
219 REAL(
dp) :: cpc_h_tmp, cpc_s_tmp
221 cpassert(
ASSOCIATED(rho_atom))
222 cpassert(
ASSOCIATED(mixing_store%paw))
224 DO iatom = 1,
SIZE(rho_atom)
225 IF (mixing_store%paw(iatom))
THEN
226 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(1)%r_coef))
227 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(2)%r_coef))
228 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(1)%r_coef))
229 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(2)%r_coef))
230 ncoef_h1 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 1)
231 ncoef_h2 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 2)
232 ncoef_s1 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 1)
233 ncoef_s2 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 2)
234 cpassert(ncoef_h1 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 1))
235 cpassert(ncoef_h2 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 2))
236 cpassert(ncoef_s1 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 1))
237 cpassert(ncoef_s2 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 2))
239 DO icoef2 = 1, ncoef_h2
240 DO icoef1 = 1, ncoef_h1
241 cpc_h_tmp = rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2)
242 rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2) = &
243 cpc_h_tmp + rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2)
244 rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2) = &
245 cpc_h_tmp - rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2)
249 DO icoef2 = 1, ncoef_s2
250 DO icoef1 = 1, ncoef_s1
251 cpc_s_tmp = rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2)
252 rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2) = &
253 cpc_s_tmp + rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2)
254 rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2) = &
255 cpc_s_tmp - rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2)
261 END SUBROUTINE gapw_cpc_spin_to_charge_mag
269 SUBROUTINE gapw_cpc_charge_mag_to_spin(rho_atom, mixing_store)
273 INTEGER :: iatom, icoef1, icoef2, ncoef_h1, &
274 ncoef_h2, ncoef_s1, ncoef_s2
275 REAL(
dp) :: cpc_h_tmp, cpc_s_tmp
277 cpassert(
ASSOCIATED(rho_atom))
278 cpassert(
ASSOCIATED(mixing_store%paw))
280 DO iatom = 1,
SIZE(rho_atom)
281 IF (mixing_store%paw(iatom))
THEN
282 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(1)%r_coef))
283 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(2)%r_coef))
284 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(1)%r_coef))
285 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(2)%r_coef))
286 ncoef_h1 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 1)
287 ncoef_h2 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 2)
288 ncoef_s1 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 1)
289 ncoef_s2 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 2)
290 cpassert(ncoef_h1 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 1))
291 cpassert(ncoef_h2 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 2))
292 cpassert(ncoef_s1 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 1))
293 cpassert(ncoef_s2 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 2))
295 DO icoef2 = 1, ncoef_h2
296 DO icoef1 = 1, ncoef_h1
297 cpc_h_tmp = rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2)
298 rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2) = &
299 0.5_dp*(cpc_h_tmp + rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2))
300 rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2) = &
301 0.5_dp*(cpc_h_tmp - rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2))
305 DO icoef2 = 1, ncoef_s2
306 DO icoef1 = 1, ncoef_s1
307 cpc_s_tmp = rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2)
308 rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2) = &
309 0.5_dp*(cpc_s_tmp + rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2))
310 rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2) = &
311 0.5_dp*(cpc_s_tmp - rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2))
317 END SUBROUTINE gapw_cpc_charge_mag_to_spin
330 SUBROUTINE gmix_potential_only(qs_env, mixing_store, density)
336 CHARACTER(len=*),
PARAMETER :: routinen =
'gmix_potential_only'
338 COMPLEX(dp),
DIMENSION(:),
POINTER :: cc_new
339 INTEGER :: handle, iatom, ig, ispin, natom, ng, &
342 REAL(
dp) :: alpha, alpha_eff, f_mix
343 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
347 cpassert(
ASSOCIATED(mixing_store%rhoin))
348 cpassert(
ASSOCIATED(mixing_store%kerker_factor))
350 CALL timeset(routinen, handle)
351 NULLIFY (cc_new, dft_control, rho_atom, kerker_eff)
353 CALL get_qs_env(qs_env, dft_control=dft_control)
355 nspin =
SIZE(density)
356 ng =
SIZE(density(1)%cc)
358 gapw = dft_control%qs_control%gapw
359 alpha = mixing_store%alpha
363 IF (ispin >= 2 .AND. nspin >= 2)
THEN
364 alpha_eff = mixing_store%alpha_mag
365 kerker_eff => mixing_store%kerker_factor_mag
367 alpha_eff = mixing_store%alpha
368 kerker_eff => mixing_store%kerker_factor
370 cc_new => density(ispin)%cc
371 DO ig = 1, mixing_store%ig_max
372 f_mix = alpha_eff*kerker_eff(ig)
373 cc_new(ig) = (1.0_dp - f_mix)*mixing_store%rhoin(ispin)%cc(ig) + f_mix*cc_new(ig)
374 mixing_store%rhoin(ispin)%cc(ig) = cc_new(ig)
376 DO ig = mixing_store%ig_max + 1, ng
378 cc_new(ig) = (1.0_dp - f_mix)*mixing_store%rhoin(ispin)%cc(ig) + f_mix*cc_new(ig)
379 mixing_store%rhoin(ispin)%cc(ig) = cc_new(ig)
384 IF (mixing_store%gmix_p .AND. gapw)
THEN
386 rho_atom_set=rho_atom)
387 natom =
SIZE(rho_atom)
389 IF (ispin >= 2 .AND. nspin >= 2)
THEN
390 alpha_eff = mixing_store%alpha_mag
392 alpha_eff = mixing_store%alpha
395 IF (mixing_store%paw(iatom))
THEN
396 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
397 mixing_store%cpc_h_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
398 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
399 mixing_store%cpc_s_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
400 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
401 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
407 CALL timestop(handle)
409 END SUBROUTINE gmix_potential_only
426 SUBROUTINE pulay_mixing(qs_env, mixing_store, density, para_env, vol)
432 REAL(kind=
dp),
INTENT(IN) :: vol
434 CHARACTER(len=*),
PARAMETER :: routinen =
'pulay_mixing'
436 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: cc_mix
437 INTEGER :: handle, i, iatom, ib, ibb, ig, ispin, &
438 jb, n1, n2, natom, nb, nb1, nbuffer, &
441 REAL(
dp) :: alpha_eff, alpha_kerker, alpha_pulay, &
442 beta, f_mix, inv_err, norm, &
444 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: alpha_c, ev
445 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: b, c, c_inv, cpc_h_mix, cpc_s_mix
446 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
450 cpassert(
ASSOCIATED(mixing_store%res_buffer))
451 cpassert(
ASSOCIATED(mixing_store%rhoin_buffer))
452 NULLIFY (dft_control, rho_atom, kerker_eff)
453 CALL timeset(routinen, handle)
455 CALL get_qs_env(qs_env, dft_control=dft_control)
456 nspin =
SIZE(density)
457 ng =
SIZE(mixing_store%res_buffer(1, 1)%cc)
459 alpha_kerker = mixing_store%alpha
460 beta = mixing_store%pulay_beta
461 alpha_pulay = mixing_store%pulay_alpha
462 nbuffer = mixing_store%nbuffer
463 gapw = dft_control%qs_control%gapw
465 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
466 natom =
SIZE(rho_atom)
469 ALLOCATE (cc_mix(ng))
473 IF (ispin >= 2 .AND. nspin >= 2)
THEN
474 alpha_eff = mixing_store%alpha_mag
475 kerker_eff => mixing_store%kerker_factor_mag
477 alpha_eff = mixing_store%alpha
478 kerker_eff => mixing_store%kerker_factor
480 alpha_kerker = alpha_eff
481 ib =
modulo(mixing_store%ncall_p(ispin), nbuffer) + 1
483 mixing_store%ncall_p(ispin) = mixing_store%ncall_p(ispin) + 1
484 nb = min(mixing_store%ncall_p(ispin), nbuffer)
485 ibb =
modulo(mixing_store%ncall_p(ispin), nbuffer) + 1
487 mixing_store%res_buffer(ib, ispin)%cc(:) = cmplx(0._dp, 0._dp, kind=
dp)
489 IF (nb == 1) mixing_store%rhoin_buffer(1, ispin)%cc = mixing_store%rhoin(ispin)%cc
492 f_mix = kerker_eff(ig)
493 mixing_store%res_buffer(ib, ispin)%cc(ig) = f_mix*(density(ispin)%cc(ig) - &
494 mixing_store%rhoin_buffer(ib, ispin)%cc(ig))
495 res_norm = res_norm + &
496 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp)*
REAL(mixing_store%res_buffer(ib, ispin)%cc(ig), dp) + &
497 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))*aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))
500 CALL para_env%sum(res_norm)
501 res_norm = sqrt(res_norm)
503 IF (res_norm < 1.e-14_dp)
THEN
504 mixing_store%ncall_p(ispin) = mixing_store%ncall_p(ispin) - 1
507 IF (
ALLOCATED(b))
DEALLOCATE (b)
508 ALLOCATE (b(nb1, nb1))
510 IF (
ALLOCATED(c))
DEALLOCATE (c)
513 IF (
ALLOCATED(c_inv))
DEALLOCATE (c_inv)
514 ALLOCATE (c_inv(nb, nb))
516 IF (
ALLOCATED(alpha_c))
DEALLOCATE (alpha_c)
517 ALLOCATE (alpha_c(nb))
520 IF (
ALLOCATED(ev))
DEALLOCATE (ev)
527 mixing_store%pulay_matrix(jb, ib, ispin) = 0.0_dp
529 f_mix = mixing_store%special_metric(ig)
530 mixing_store%pulay_matrix(jb, ib, ispin) = mixing_store%pulay_matrix(jb, ib, ispin) + &
531 f_mix*(real(mixing_store%res_buffer(jb, ispin)%cc(ig),
dp) &
532 *real(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp) + &
533 aimag(mixing_store%res_buffer(jb, ispin)%cc(ig))* &
534 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig)))
536 CALL para_env%sum(mixing_store%pulay_matrix(jb, ib, ispin))
537 mixing_store%pulay_matrix(jb, ib, ispin) = mixing_store%pulay_matrix(jb, ib, ispin)*vol*vol
538 mixing_store%pulay_matrix(ib, jb, ispin) = mixing_store%pulay_matrix(jb, ib, ispin)
541 IF (nb == 1 .OR. res_norm < 1.e-14_dp)
THEN
543 f_mix = alpha_kerker*kerker_eff(ig)
544 cc_mix(ig) = density(ispin)%cc(ig) - &
545 mixing_store%rhoin_buffer(ib, ispin)%cc(ig)
546 density(ispin)%cc(ig) = f_mix*cc_mix(ig) + &
547 mixing_store%rhoin_buffer(ib, ispin)%cc(ig)
548 mixing_store%rhoin_buffer(ibb, ispin)%cc(ig) = density(ispin)%cc(ig)
550 IF (mixing_store%gmix_p)
THEN
553 IF (mixing_store%paw(iatom))
THEN
554 mixing_store%cpc_h_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
555 mixing_store%cpc_h_in(iatom, ispin)%r_coef
556 mixing_store%cpc_s_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
557 mixing_store%cpc_s_in(iatom, ispin)%r_coef
559 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_kerker*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
560 (1.0_dp - alpha_kerker)*mixing_store%cpc_h_in(iatom, ispin)%r_coef
561 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_kerker*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
562 (1.0_dp - alpha_kerker)*mixing_store%cpc_s_in(iatom, ispin)%r_coef
564 mixing_store%cpc_h_in_buffer(ib, iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
565 mixing_store%cpc_s_in_buffer(ib, iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
567 mixing_store%cpc_h_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
568 mixing_store%cpc_s_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
575 b(1:nb, 1:nb) = mixing_store%pulay_matrix(1:nb, 1:nb, ispin)
576 c(1:nb, 1:nb) = b(1:nb, 1:nb)
577 b(nb1, 1:nb) = -1.0_dp
578 b(1:nb, nb1) = -1.0_dp
585 alpha_c(i) = alpha_c(i) + c_inv(jb, i)
586 norm_c_inv = norm_c_inv + c_inv(jb, i)
589 alpha_c(1:nb) = alpha_c(1:nb)/norm_c_inv
590 cc_mix = cmplx(0._dp, 0._dp, kind=
dp)
593 cc_mix(ig) = cc_mix(ig) + &
594 alpha_c(jb)*(mixing_store%rhoin_buffer(jb, ispin)%cc(ig) + &
595 mixing_store%pulay_beta*mixing_store%res_buffer(jb, ispin)%cc(ig))
598 mixing_store%rhoin_buffer(ibb, ispin)%cc = cmplx(0._dp, 0._dp, kind=
dp)
599 IF (alpha_pulay > 0.0_dp)
THEN
601 f_mix = alpha_pulay*kerker_eff(ig)
602 density(ispin)%cc(ig) = f_mix*density(ispin)%cc(ig) + &
603 (1.0_dp - f_mix)*cc_mix(ig)
604 mixing_store%rhoin_buffer(ibb, ispin)%cc(ig) = density(ispin)%cc(ig)
608 density(ispin)%cc(ig) = cc_mix(ig)
609 mixing_store%rhoin_buffer(ibb, ispin)%cc(ig) = density(ispin)%cc(ig)
613 IF (mixing_store%gmix_p .AND. gapw)
THEN
614 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
616 IF (mixing_store%paw(iatom))
THEN
617 n1 =
SIZE(rho_atom(iatom)%cpc_h(ispin)%r_coef, 1)
618 n2 =
SIZE(rho_atom(iatom)%cpc_h(ispin)%r_coef, 2)
619 ALLOCATE (cpc_h_mix(n1, n2))
620 ALLOCATE (cpc_s_mix(n1, n2))
622 mixing_store%cpc_h_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
623 mixing_store%cpc_h_in_buffer(ib, iatom, ispin)%r_coef
624 mixing_store%cpc_s_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
625 mixing_store%cpc_s_in_buffer(ib, iatom, ispin)%r_coef
629 cpc_h_mix(:, :) = cpc_h_mix(:, :) + &
630 alpha_c(jb)*mixing_store%cpc_h_in_buffer(jb, iatom, ispin)%r_coef(:, :) + &
631 alpha_c(jb)*beta*mixing_store%cpc_h_res_buffer(jb, iatom, ispin)%r_coef(:, :)
632 cpc_s_mix(:, :) = cpc_s_mix(:, :) + &
633 alpha_c(jb)*mixing_store%cpc_s_in_buffer(jb, iatom, ispin)%r_coef(:, :) + &
634 alpha_c(jb)*beta*mixing_store%cpc_s_res_buffer(jb, iatom, ispin)%r_coef(:, :)
636 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_pulay*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
637 (1.0_dp - alpha_pulay)*cpc_h_mix
638 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_pulay*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
639 (1.0_dp - alpha_pulay)*cpc_s_mix
640 mixing_store%cpc_h_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
641 mixing_store%cpc_s_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
642 DEALLOCATE (cpc_h_mix)
643 DEALLOCATE (cpc_s_mix)
649 IF (res_norm > 1.e-14_dp)
THEN
652 DEALLOCATE (c, c_inv, alpha_c)
659 CALL timestop(handle)
661 END SUBROUTINE pulay_mixing
675 SUBROUTINE broyden_mixing(qs_env, mixing_store, density, para_env)
682 CHARACTER(len=*),
PARAMETER :: routinen =
'broyden_mixing'
684 COMPLEX(dp) :: cc_mix
685 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: res_rho
686 INTEGER :: handle, i, iatom, ib, ig, ispin, j, jb, &
687 n1, n2, natom, nb, nbuffer, ng, nspin
688 LOGICAL :: gapw, only_kerker
689 REAL(
dp) :: alpha, alpha_eff, dcpc_h_res, &
690 dcpc_s_res, delta_norm, f_mix, &
691 inv_err, res_norm, valh, vals, w0
692 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: c, g
693 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: a, b
694 REAL(
dp),
DIMENSION(2) :: norms
695 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
699 cpassert(
ASSOCIATED(mixing_store%res_buffer))
700 cpassert(
ASSOCIATED(mixing_store%rhoin))
701 cpassert(
ASSOCIATED(mixing_store%rhoin_old))
702 cpassert(
ASSOCIATED(mixing_store%drho_buffer))
703 NULLIFY (dft_control, rho_atom, kerker_eff)
704 CALL timeset(routinen, handle)
706 CALL get_qs_env(qs_env, dft_control=dft_control)
708 nspin =
SIZE(density)
709 ng =
SIZE(mixing_store%res_buffer(1, 1)%cc)
711 alpha = mixing_store%alpha
712 w0 = mixing_store%broy_w0
713 nbuffer = mixing_store%nbuffer
714 gapw = dft_control%qs_control%gapw
716 ALLOCATE (res_rho(ng))
718 mixing_store%ncall = mixing_store%ncall + 1
719 IF (mixing_store%ncall == 1)
THEN
722 only_kerker = .false.
723 nb = min(mixing_store%ncall - 1, nbuffer)
724 ib =
modulo(mixing_store%ncall - 2, nbuffer) + 1
728 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
729 natom =
SIZE(rho_atom)
736 IF (ispin >= 2 .AND. nspin >= 2)
THEN
737 alpha_eff = mixing_store%alpha_mag
738 kerker_eff => mixing_store%kerker_factor_mag
740 alpha_eff = mixing_store%alpha
741 kerker_eff => mixing_store%kerker_factor
743 IF (only_kerker)
THEN
747 res_rho(ig) = density(ispin)%cc(ig) - mixing_store%rhoin(ispin)%cc(ig)
748 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
749 f_mix = alpha_eff*kerker_eff(ig)
750 density(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig) + f_mix*res_rho(ig)
751 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
752 mixing_store%rhoin(ispin)%cc(ig) = density(ispin)%cc(ig)
756 IF (mixing_store%gmix_p)
THEN
759 IF (mixing_store%paw(iatom))
THEN
760 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
761 mixing_store%cpc_h_in(iatom, ispin)%r_coef
762 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
763 mixing_store%cpc_s_in(iatom, ispin)%r_coef
765 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
766 mixing_store%cpc_h_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
767 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
768 mixing_store%cpc_s_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
770 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
771 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
772 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
773 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
794 res_rho(ig) = density(ispin)%cc(ig) - mixing_store%rhoin(ispin)%cc(ig)
795 mixing_store%res_buffer(ib, ispin)%cc(ig) = res_rho(ig) - mixing_store%last_res(ispin)%cc(ig)
796 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
797 res_norm = res_norm + &
798 REAL(res_rho(ig),
dp)*
REAL(res_rho(ig), dp) + &
799 aimag(res_rho(ig))*aimag(res_rho(ig))
800 delta_norm = delta_norm + &
801 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp)* &
802 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig), dp) + &
803 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))* &
804 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))
805 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
806 mixing_store%rhoin(ispin)%cc(ig) - &
807 mixing_store%rhoin_old(ispin)%cc(ig)
810 norms(:) = [delta_norm, res_norm]
811 CALL para_env%sum(norms)
812 delta_norm = sqrt(norms(1))
813 res_norm = sqrt(norms(2))
815 IF (res_norm > 1.e-14_dp)
THEN
819 mixing_store%res_buffer(ib, ispin)%cc(ig) = mixing_store%res_buffer(ib, ispin)%cc(ig)/delta_norm
820 mixing_store%drho_buffer(ib, ispin)%cc(ig) = mixing_store%drho_buffer(ib, ispin)%cc(ig)/delta_norm
821 f_mix = alpha_eff*kerker_eff(ig)
822 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
823 f_mix*mixing_store%res_buffer(ib, ispin)%cc(ig) + &
824 mixing_store%drho_buffer(ib, ispin)%cc(ig)
828 IF (mixing_store%gmix_p .AND. gapw)
THEN
830 IF (mixing_store%paw(iatom))
THEN
831 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
832 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
835 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
836 (mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i) - &
837 mixing_store%cpc_h_old(iatom, ispin)%r_coef(j, i))/delta_norm
838 dcpc_h_res = ((rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
839 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)) - &
840 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
841 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
842 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)
844 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
845 (mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i) - &
846 mixing_store%cpc_s_old(iatom, ispin)%r_coef(j, i))/delta_norm
847 dcpc_s_res = ((rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
848 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)) - &
849 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
850 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
851 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)
853 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
854 alpha_eff*dcpc_h_res + &
855 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i)
856 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
857 alpha_eff*dcpc_s_res + &
858 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i)
865 CALL broyden_history_products(mixing_store%res_buffer(1:nb, ispin), res_rho, &
866 mixing_store%p_metric, mixing_store%ig_max, a, c)
870 a(jb, jb) = w0 + a(jb, jb)
874 CALL dgemv(
'T', nb, nb, 1.0_dp, b, nb, c, 1, 0.0_dp, g, 1)
884 cc_mix = cc_mix - g(jb)*mixing_store%drho_buffer(jb, ispin)%cc(ig)
886 f_mix = alpha_eff*kerker_eff(ig)
888 IF (res_norm > 1.e-14_dp) density(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig) + &
889 f_mix*res_rho(ig) + cc_mix
890 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
891 mixing_store%rhoin(ispin)%cc(ig) = density(ispin)%cc(ig)
895 IF (mixing_store%gmix_p)
THEN
898 IF (mixing_store%paw(iatom))
THEN
899 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
900 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
906 valh = valh - g(jb)*mixing_store%dcpc_h_in(jb, iatom, ispin)%r_coef(j, i)
907 vals = vals - g(jb)*mixing_store%dcpc_s_in(jb, iatom, ispin)%r_coef(j, i)
909 IF (res_norm > 1.e-14_dp)
THEN
910 rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) = &
911 alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) + &
912 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + valh
913 rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) = &
914 alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) + &
915 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + vals
920 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
921 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
922 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
923 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
929 DEALLOCATE (a, b, c, g)
936 CALL timestop(handle)
938 END SUBROUTINE broyden_mixing
949 SUBROUTINE modified_broyden_mixing(qs_env, mixing_store, density, para_env)
956 CHARACTER(len=*),
PARAMETER :: routinen =
'modified_broyden_mixing'
958 COMPLEX(dp) :: cc_mix
959 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: res_rho
960 INTEGER :: handle, i, iatom, ib, ig, ispin, j, jb, &
961 kb, n1, n2, natom, nb, nbuffer, ng, &
963 LOGICAL :: can_update, gapw, only_kerker
964 REAL(
dp) :: alpha_eff, broy_w0, broy_wmax, broy_wmin, broy_wref, dcpc_h_res, dcpc_s_res, &
965 delta_norm, f_mix, inv_err, res_norm, valh, vals
966 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: c, g
967 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: a, b
968 REAL(
dp),
DIMENSION(2) :: norms
969 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
973 cpassert(
ASSOCIATED(mixing_store%res_buffer))
974 cpassert(
ASSOCIATED(mixing_store%rhoin))
975 cpassert(
ASSOCIATED(mixing_store%rhoin_old))
976 cpassert(
ASSOCIATED(mixing_store%drho_buffer))
977 cpassert(
ASSOCIATED(mixing_store%weight))
978 NULLIFY (dft_control, rho_atom, kerker_eff)
979 CALL timeset(routinen, handle)
981 CALL get_qs_env(qs_env, dft_control=dft_control)
983 nspin =
SIZE(density)
984 ng =
SIZE(mixing_store%res_buffer(1, 1)%cc)
986 broy_w0 = mixing_store%broy_w0
987 broy_wref = mixing_store%wc
988 broy_wmax = mixing_store%wmax
990 nbuffer = mixing_store%nbuffer
991 gapw = dft_control%qs_control%gapw
993 ALLOCATE (res_rho(ng))
995 mixing_store%ncall = mixing_store%ncall + 1
996 IF (mixing_store%ncall == 1)
THEN
999 only_kerker = .false.
1000 nb = min(mixing_store%ncall - 1, nbuffer)
1001 ib =
modulo(mixing_store%ncall - 2, nbuffer) + 1
1005 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
1006 natom =
SIZE(rho_atom)
1012 IF (ispin >= 2 .AND. nspin >= 2)
THEN
1013 alpha_eff = mixing_store%alpha_mag
1014 kerker_eff => mixing_store%kerker_factor_mag
1016 alpha_eff = mixing_store%alpha
1017 kerker_eff => mixing_store%kerker_factor
1019 IF (only_kerker)
THEN
1023 res_rho(ig) = density(ispin)%cc(ig) - mixing_store%rhoin(ispin)%cc(ig)
1024 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
1025 f_mix = alpha_eff*kerker_eff(ig)
1026 density(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig) + f_mix*res_rho(ig)
1027 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
1028 mixing_store%rhoin(ispin)%cc(ig) = density(ispin)%cc(ig)
1032 IF (mixing_store%gmix_p)
THEN
1035 IF (mixing_store%paw(iatom))
THEN
1036 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
1037 mixing_store%cpc_h_in(iatom, ispin)%r_coef
1038 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
1039 mixing_store%cpc_s_in(iatom, ispin)%r_coef
1041 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
1042 mixing_store%cpc_h_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
1043 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
1044 mixing_store%cpc_s_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
1046 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
1047 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
1048 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
1049 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
1056 ALLOCATE (a(nb, nb))
1058 ALLOCATE (b(nb, nb))
1070 res_rho(ig) = density(ispin)%cc(ig) - mixing_store%rhoin(ispin)%cc(ig)
1071 mixing_store%res_buffer(ib, ispin)%cc(ig) = res_rho(ig) - mixing_store%last_res(ispin)%cc(ig)
1072 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
1073 res_norm = res_norm + &
1074 REAL(res_rho(ig),
dp)*
REAL(res_rho(ig), dp) + &
1075 aimag(res_rho(ig))*aimag(res_rho(ig))
1076 delta_norm = delta_norm + &
1077 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp)* &
1078 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig), dp) + &
1079 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))* &
1080 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))
1081 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
1082 mixing_store%rhoin(ispin)%cc(ig) - &
1083 mixing_store%rhoin_old(ispin)%cc(ig)
1086 norms(:) = [delta_norm, res_norm]
1087 CALL para_env%sum(norms)
1088 delta_norm = sqrt(norms(1))
1089 res_norm = sqrt(norms(2))
1091 IF (res_norm > (broy_wref/broy_wmax))
THEN
1092 mixing_store%weight(ib, ispin) = broy_wref/res_norm
1094 mixing_store%weight(ib, ispin) = broy_wmax
1096 mixing_store%weight(ib, ispin) = max(broy_wmin, mixing_store%weight(ib, ispin))
1097 can_update = res_norm > 1.e-14_dp .AND. delta_norm > 1.e-14_dp
1099 IF (can_update)
THEN
1103 mixing_store%res_buffer(ib, ispin)%cc(ig) = mixing_store%res_buffer(ib, ispin)%cc(ig)/delta_norm
1104 mixing_store%drho_buffer(ib, ispin)%cc(ig) = mixing_store%drho_buffer(ib, ispin)%cc(ig)/delta_norm
1105 f_mix = alpha_eff*kerker_eff(ig)
1106 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
1107 f_mix*mixing_store%res_buffer(ib, ispin)%cc(ig) + &
1108 mixing_store%drho_buffer(ib, ispin)%cc(ig)
1112 IF (mixing_store%gmix_p .AND. gapw)
THEN
1114 IF (mixing_store%paw(iatom))
THEN
1115 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
1116 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
1119 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
1120 (mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i) - &
1121 mixing_store%cpc_h_old(iatom, ispin)%r_coef(j, i))/delta_norm
1122 dcpc_h_res = ((rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
1123 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)) - &
1124 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
1125 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
1126 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)
1128 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
1129 (mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i) - &
1130 mixing_store%cpc_s_old(iatom, ispin)%r_coef(j, i))/delta_norm
1131 dcpc_s_res = ((rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
1132 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)) - &
1133 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
1134 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
1135 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)
1137 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
1138 alpha_eff*dcpc_h_res + &
1139 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i)
1140 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
1141 alpha_eff*dcpc_s_res + &
1142 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i)
1149 CALL broyden_history_products(mixing_store%res_buffer(1:nb, ispin), res_rho, &
1150 mixing_store%p_metric, mixing_store%ig_max, a, c)
1151 CALL para_env%sum(a)
1152 CALL para_env%sum(c)
1155 c(jb) = mixing_store%weight(jb, ispin)*c(jb)
1157 a(kb, jb) = mixing_store%weight(kb, ispin)*mixing_store%weight(jb, ispin)*a(kb, jb)
1159 a(jb, jb) = broy_w0*broy_w0 + a(jb, jb)
1163 CALL dgemv(
'T', nb, nb, 1.0_dp, b, nb, c, 1, 0.0_dp, g, 1)
1168 mixing_store%res_buffer(ib, ispin)%cc(ig) =
z_zero
1169 mixing_store%drho_buffer(ib, ispin)%cc(ig) =
z_zero
1179 cc_mix = cc_mix - mixing_store%weight(jb, ispin)*g(jb)*mixing_store%drho_buffer(jb, ispin)%cc(ig)
1181 f_mix = alpha_eff*kerker_eff(ig)
1183 IF (res_norm > 1.e-14_dp) density(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig) + &
1184 f_mix*res_rho(ig) + cc_mix
1185 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
1186 mixing_store%rhoin(ispin)%cc(ig) = density(ispin)%cc(ig)
1190 IF (mixing_store%gmix_p)
THEN
1193 IF (mixing_store%paw(iatom))
THEN
1194 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
1195 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
1201 valh = valh - mixing_store%weight(jb, ispin)*g(jb)* &
1202 mixing_store%dcpc_h_in(jb, iatom, ispin)%r_coef(j, i)
1203 vals = vals - mixing_store%weight(jb, ispin)*g(jb)* &
1204 mixing_store%dcpc_s_in(jb, iatom, ispin)%r_coef(j, i)
1206 IF (res_norm > 1.e-14_dp)
THEN
1207 rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) = &
1208 alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) + &
1209 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + valh
1210 rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) = &
1211 alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) + &
1212 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + vals
1217 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
1218 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
1219 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
1220 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
1226 DEALLOCATE (a, b, c, g)
1231 DEALLOCATE (res_rho)
1233 CALL timestop(handle)
1235 END SUBROUTINE modified_broyden_mixing
1246 SUBROUTINE broyden_history_products(history, residual, metric, ng, a, c)
1248 COMPLEX(dp),
INTENT(IN) :: residual(:)
1249 REAL(
dp),
INTENT(IN) :: metric(:)
1250 INTEGER,
INTENT(IN) :: ng
1251 REAL(
dp),
INTENT(OUT) :: a(:, :), c(:)
1253 INTEGER :: ig, jb, kb, nb
1267 prod = prod + metric(ig)*( &
1268 REAL(history(jb)%cc(ig),
dp)*
REAL(history(kb)%cc(ig), dp) + &
1269 aimag(history(jb)%cc(ig))*aimag(history(kb)%cc(ig)))
1277 prod = prod + metric(ig)*( &
1278 REAL(history(jb)%cc(ig),
dp)*
REAL(residual(ig), dp) + &
1279 aimag(history(jb)%cc(ig))*aimag(residual(ig)))
1287 a(jb, kb) = a(kb, jb)
1290 END SUBROUTINE broyden_history_products
1307 SUBROUTINE multisecant_mixing(mixing_store, density, para_env, g2, ng_global)
1312 REAL(kind=
dp),
INTENT(IN) :: g2(:)
1313 INTEGER,
INTENT(IN) :: ng_global
1315 CHARACTER(len=*),
PARAMETER :: routinen =
'multisecant_mixing'
1316 COMPLEX(KIND=dp),
PARAMETER :: cmone = (-1.0_dp, 0.0_dp), &
1317 cone = (1.0_dp, 0.0_dp), &
1318 czero = (0.0_dp, 0.0_dp)
1320 COMPLEX(dp) :: saa, yaa
1321 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: gn_global, pgn, res_matrix_global, &
1323 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:, :) :: a_matrix, res_matrix, sa, step_matrix, ya
1324 COMPLEX(dp),
DIMENSION(:),
POINTER :: gn
1325 INTEGER :: handle, ib, ib_next, ib_prev, ig, &
1326 ig_global, iig, ispin, jb, kb, nb, &
1328 LOGICAL :: use_zgemm, use_zgemm_rev
1329 REAL(
dp) :: alpha, alpha_eff, f_mix, gn_norm, gn_norm_old, inv_err, n_low, n_up, pgn_norm, &
1330 prec, r_step, reg_par, sigma_max, sigma_tilde, step_size
1331 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: norm_res, norm_res_low, norm_res_up
1332 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: b_matrix, binv_matrix
1333 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
1334 REAL(
dp),
SAVE :: sigma_old = 1.0_dp
1336 cpassert(
ASSOCIATED(mixing_store))
1338 CALL timeset(routinen, handle)
1343 use_zgemm_rev = .true.
1347 nspin =
SIZE(density)
1348 ng =
SIZE(mixing_store%rhoin_buffer(1, 1)%cc)
1349 alpha = mixing_store%alpha
1351 sigma_max = mixing_store%sigma_max
1352 reg_par = mixing_store%reg_par
1353 r_step = mixing_store%r_step
1354 nbuffer = mixing_store%nbuffer
1357 nb = min(mixing_store%ncall, nbuffer - 1)
1358 ib =
modulo(mixing_store%ncall, nbuffer) + 1
1359 IF (mixing_store%ncall > 0)
THEN
1360 ib_prev =
modulo(mixing_store%ncall - 1, nbuffer) + 1
1364 mixing_store%ncall = mixing_store%ncall + 1
1365 ib_next =
modulo(mixing_store%ncall, nbuffer) + 1
1370 IF (nb == 0) mixing_store%rhoin_buffer(ib, ispin)%cc = mixing_store%rhoin(ispin)%cc
1371 gn => mixing_store%res_buffer(ib, ispin)%cc
1374 gn(ig) = (density(ispin)%cc(ig) - mixing_store%rhoin_buffer(ib, ispin)%cc(ig))
1375 gn_norm = gn_norm + &
1376 REAL(gn(ig),
dp)*
REAL(gn(ig), dp) + aimag(gn(ig))*aimag(gn(ig))
1378 CALL para_env%sum(gn_norm)
1379 gn_norm = sqrt(gn_norm)
1380 mixing_store%norm_res_buffer(ib, ispin) = gn_norm
1386 IF (ispin >= 2 .AND. nspin >= 2)
THEN
1387 alpha_eff = mixing_store%alpha_mag
1388 kerker_eff => mixing_store%kerker_factor_mag
1390 alpha_eff = mixing_store%alpha
1391 kerker_eff => mixing_store%kerker_factor
1394 f_mix = alpha_eff*kerker_eff(ig)
1395 density(ispin)%cc(ig) = mixing_store%rhoin_buffer(1, ispin)%cc(ig) + &
1396 f_mix*mixing_store%res_buffer(1, ispin)%cc(ig)
1397 mixing_store%rhoin_buffer(ib_next, ispin)%cc(ig) = density(ispin)%cc(ig)
1400 CALL timestop(handle)
1406 ALLOCATE (step_matrix(ng, nb))
1408 ALLOCATE (res_matrix(ng, nb))
1410 ALLOCATE (a_matrix(nb, ng_global))
1412 ALLOCATE (norm_res(nb))
1413 ALLOCATE (norm_res_low(nb))
1414 ALLOCATE (norm_res_up(nb))
1417 ALLOCATE (b_matrix(nb, nb))
1419 ALLOCATE (binv_matrix(nb, nb))
1421 ALLOCATE (gn_global(ng_global))
1422 ALLOCATE (res_matrix_global(ng_global))
1424 ALLOCATE (sa(ng, ng_global))
1425 ALLOCATE (ya(ng, ng_global))
1427 IF (use_zgemm_rev)
THEN
1428 ALLOCATE (tmp_vec(nb))
1435 IF (ispin >= 2 .AND. nspin >= 2)
THEN
1436 alpha_eff = mixing_store%alpha_mag
1437 kerker_eff => mixing_store%kerker_factor_mag
1439 alpha_eff = mixing_store%alpha
1440 kerker_eff => mixing_store%kerker_factor
1443 gn => mixing_store%res_buffer(ib, ispin)%cc
1446 ig_global = mixing_store%ig_global_index(ig)
1447 gn_global(ig_global) = gn(ig)
1449 CALL para_env%sum(gn_global)
1460 ELSE IF (jb > ib)
THEN
1465 norm_res(kb) = 0.0_dp
1466 norm_res_low(kb) = 0.0_dp
1467 norm_res_up(kb) = 0.0_dp
1472 step_matrix(ig, kb) = prec*(mixing_store%rhoin_buffer(jb, ispin)%cc(ig) - &
1473 mixing_store%rhoin_buffer(ib, ispin)%cc(ig))
1474 res_matrix(ig, kb) = (mixing_store%res_buffer(jb, ispin)%cc(ig) - &
1475 mixing_store%res_buffer(ib, ispin)%cc(ig))
1476 norm_res(kb) = norm_res(kb) + real(res_matrix(ig, kb),
dp)*real(res_matrix(ig, kb),
dp) + &
1477 aimag(res_matrix(ig, kb))*aimag(res_matrix(ig, kb))
1478 IF (ig <=
SIZE(g2))
THEN
1479 IF (g2(ig) < 4.0_dp)
THEN
1480 norm_res_low(kb) = norm_res_low(kb) + &
1481 REAL(res_matrix(ig, kb),
dp)*
REAL(res_matrix(ig, kb), dp) + &
1482 aimag(res_matrix(ig, kb))*aimag(res_matrix(ig, kb))
1484 norm_res_up(kb) = norm_res_up(kb) + &
1485 REAL(res_matrix(ig, kb),
dp)*
REAL(res_matrix(ig, kb), dp) + &
1486 aimag(res_matrix(ig, kb))*aimag(res_matrix(ig, kb))
1489 res_matrix(ig, kb) = prec*res_matrix(ig, kb)
1494 CALL para_env%sum(norm_res)
1495 CALL para_env%sum(norm_res_up)
1496 CALL para_env%sum(norm_res_low)
1497 norm_res(1:nb) = 1.0_dp/sqrt(norm_res(1:nb))
1501 n_low = n_low + norm_res_low(jb)/norm_res(jb)
1502 n_up = n_up + norm_res_up(jb)/norm_res(jb)
1505 IF (n_low > 0.0_dp .AND. n_up > 0.0_dp)
THEN
1507 IF (g2(ig) > 4.0_dp)
THEN
1508 step_matrix(ig, 1:nb) = step_matrix(ig, 1:nb)*sqrt(n_low/n_up)
1509 res_matrix(ig, 1:nb) = res_matrix(ig, 1:nb)*sqrt(n_low/n_up)
1514 step_matrix(1:ng, kb) = step_matrix(1:ng, kb)*norm_res(kb)
1515 res_matrix(1:ng, kb) = res_matrix(1:ng, kb)*norm_res(kb)
1522 b_matrix(kb, jb) = 0.0_dp
1526 b_matrix(kb, jb) = b_matrix(kb, jb) + real(res_matrix(ig, kb)*res_matrix(ig, jb),
dp)
1531 CALL para_env%sum(b_matrix)
1533 b_matrix(jb, jb) = b_matrix(jb, jb) + reg_par
1541 res_matrix_global =
z_zero
1543 ig_global = mixing_store%ig_global_index(ig)
1544 res_matrix_global(ig_global) = res_matrix(ig, kb)
1546 CALL para_env%sum(res_matrix_global)
1550 ig_global = mixing_store%ig_global_index(ig)
1551 a_matrix(jb, ig_global) = a_matrix(jb, ig_global) + &
1552 binv_matrix(jb, kb)*res_matrix_global(ig_global)
1556 CALL para_env%sum(a_matrix)
1559 gn => mixing_store%res_buffer(ib, ispin)%cc
1564 CALL zgemm(
"N",
"N", ng, ng_global, nb, cmone, step_matrix(1, 1), ng, &
1565 a_matrix(1, 1), nb, czero, sa(1, 1), ng)
1566 CALL zgemm(
"N",
"N", ng, ng_global, nb, cmone, res_matrix(1, 1), ng, &
1567 a_matrix(1, 1), nb, czero, ya(1, 1), ng)
1569 ig_global = mixing_store%ig_global_index(ig)
1570 ya(ig, ig_global) = ya(ig, ig_global) +
z_one
1573 CALL zgemv(
"N", ng, ng_global, cone, sa(1, 1), &
1574 ng, gn_global(1), 1, czero, pgn(1), 1)
1575 CALL zgemv(
"N", ng, ng_global, cone, ya(1, 1), &
1576 ng, gn_global(1), 1, czero, ugn(1), 1)
1579 pgn_norm = pgn_norm + real(pgn(ig),
dp)*real(pgn(ig),
dp) + &
1580 aimag(pgn(ig))*aimag(pgn(ig))
1582 CALL para_env%sum(pgn_norm)
1583 ELSE IF (use_zgemm_rev)
THEN
1585 CALL zgemv(
"N", nb, ng_global, cone, a_matrix(1, 1), &
1586 nb, gn_global(1), 1, czero, tmp_vec(1), 1)
1588 CALL zgemv(
"N", ng, nb, cmone, step_matrix(1, 1), ng, &
1589 tmp_vec(1), 1, czero, pgn(1), 1)
1591 CALL zgemv(
"N", ng, nb, cmone, res_matrix(1, 1), ng, &
1592 tmp_vec(1), 1, czero, ugn(1), 1)
1595 pgn_norm = pgn_norm + real(pgn(ig),
dp)*real(pgn(ig),
dp) + &
1596 aimag(pgn(ig))*aimag(pgn(ig))
1597 ugn(ig) = ugn(ig) + gn(ig)
1599 CALL para_env%sum(pgn_norm)
1605 ig_global = mixing_store%ig_global_index(ig)
1606 DO iig = 1, ng_global
1610 IF (ig_global == iig) yaa =
z_one
1613 saa = saa - step_matrix(ig, jb)*a_matrix(jb, iig)
1614 yaa = yaa - res_matrix(ig, jb)*a_matrix(jb, iig)
1616 pgn(ig) = pgn(ig) + saa*gn_global(iig)
1617 ugn(ig) = ugn(ig) + yaa*gn_global(iig)
1621 pgn_norm = pgn_norm + real(pgn(ig),
dp)*real(pgn(ig),
dp) + &
1622 aimag(pgn(ig))*aimag(pgn(ig))
1624 CALL para_env%sum(pgn_norm)
1627 gn_norm = mixing_store%norm_res_buffer(ib, ispin)
1628 gn_norm_old = mixing_store%norm_res_buffer(ib_prev, ispin)
1629 IF (ib_prev /= 0)
THEN
1630 sigma_tilde = sigma_old*max(0.5_dp, min(2.0_dp, gn_norm_old/gn_norm))
1632 sigma_tilde = 0.5_dp
1634 sigma_tilde = 0.1_dp
1636 step_size = min(sigma_tilde, r_step*pgn_norm/gn_norm, sigma_max)
1637 sigma_old = step_size
1641 prec = kerker_eff(ig)
1642 density(ispin)%cc(ig) = mixing_store%rhoin_buffer(ib, ispin)%cc(ig) &
1643 - prec*step_size*ugn(ig) + prec*pgn(ig)
1644 mixing_store%rhoin_buffer(ib_next, ispin)%cc(ig) = density(ispin)%cc(ig)
1650 DEALLOCATE (step_matrix, res_matrix)
1651 DEALLOCATE (norm_res)
1652 DEALLOCATE (norm_res_up)
1653 DEALLOCATE (norm_res_low)
1654 DEALLOCATE (a_matrix, b_matrix, binv_matrix)
1655 DEALLOCATE (ugn, pgn)
1659 IF (use_zgemm_rev)
THEN
1660 DEALLOCATE (tmp_vec)
1662 DEALLOCATE (gn_global, res_matrix_global)
1664 CALL timestop(handle)
1666 END SUBROUTINE multisecant_mixing
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public johnson1988
integer, save, public kerker1981
integer, save, public broyden1965
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
Defines the basic variable types.
integer, parameter, public dp
Definition of mathematical constants and functions.
complex(kind=dp), parameter, public z_one
complex(kind=dp), parameter, public z_zero
Collection of simple mathematical functions and subroutines.
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.
subroutine, public gspace_mixing(qs_env, mixing_method, mixing_store, rho, para_env, iter_count, auxiliary)
Driver for the g-space mixing, calls the proper routine given the requested method.
subroutine, public mixing_density(mixing_store, rho_g, density, auxiliary)
View the physical density, appending auxiliary coordinates on their owning rank.
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...
stores all the informations relevant to an mpi environment
keeps the density in various representations, keeping track of which ones are valid.