43#include "./base/base_uses.f90"
49 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'qs_gspace_mixing'
70 SUBROUTINE gspace_mixing(qs_env, mixing_method, mixing_store, rho, para_env, iter_count)
72 INTEGER :: mixing_method
78 CHARACTER(len=*),
PARAMETER :: routinen =
'gspace_mixing'
80 INTEGER :: handle, iatom, ig, ispin, natom, ng, &
84 REAL(kind=
dp),
DIMENSION(:),
POINTER :: tot_rho_r
93 CALL timeset(routinen, handle)
95 CALL get_qs_env(qs_env, dft_control=dft_control, pw_env=pw_env)
96 IF (.NOT. (dft_control%qs_control%dftb .OR. dft_control%qs_control%xtb .OR. &
97 dft_control%qs_control%semi_empirical))
THEN
99 cpassert(
ASSOCIATED(mixing_store))
100 NULLIFY (auxbas_pw_pool, rho_atom, rho_g, rho_r, tot_rho_r)
101 CALL qs_rho_get(rho, rho_g=rho_g, rho_r=rho_r, tot_rho_r=tot_rho_r)
103 nspin =
SIZE(rho_g, 1)
104 ng =
SIZE(rho_g(1)%pw_grid%gsq)
105 cpassert(ng ==
SIZE(mixing_store%rhoin(1)%cc))
107 alpha = mixing_store%alpha
108 gapw = dft_control%qs_control%gapw
111 IF (mixing_store%gmix_p .AND. gapw)
THEN
112 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
113 natom =
SIZE(rho_atom)
117 CALL pw_env_get(pw_env=pw_env, auxbas_pw_pool=auxbas_pw_pool)
118 CALL auxbas_pw_pool%create_pw(rho_tmp)
120 CALL pw_copy(rho_g(1), rho_tmp)
121 CALL pw_axpy(rho_g(2), rho_g(1), 1.0_dp)
122 CALL pw_axpy(rho_tmp, rho_g(2), -1.0_dp)
124 IF (mixing_store%gmix_p .AND. gapw)
THEN
125 CALL gapw_cpc_spin_to_charge_mag(rho_atom, mixing_store)
129 IF (iter_count + 1 <= mixing_store%nskip_mixing)
THEN
133 mixing_store%rhoin(ispin)%cc(ig) = rho_g(ispin)%array(ig)
136 IF (mixing_store%gmix_p .AND. gapw)
THEN
139 IF (mixing_store%paw(iatom))
THEN
140 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
141 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
147 mixing_store%iter_method =
"NoMix"
149 IF (mixing_store%gmix_p .AND. gapw)
THEN
150 CALL gapw_cpc_charge_mag_to_spin(rho_atom, mixing_store)
152 CALL pw_axpy(rho_g(2), rho_g(1), 1.0_dp)
154 CALL pw_axpy(rho_g(1), rho_g(2), -1.0_dp)
156 CALL auxbas_pw_pool%give_back_pw(rho_tmp)
158 CALL timestop(handle)
162 IF ((iter_count + 1 - mixing_store%nskip_mixing) <= mixing_store%n_simple_mix)
THEN
164 CALL gmix_potential_only(qs_env, mixing_store, rho)
165 mixing_store%iter_method =
"Kerker"
168 CALL gmix_potential_only(qs_env, mixing_store, rho)
169 mixing_store%iter_method =
"Kerker"
171 CALL pulay_mixing(qs_env, mixing_store, rho, para_env)
172 mixing_store%iter_method =
"Pulay"
175 CALL broyden_mixing(qs_env, mixing_store, rho, para_env)
176 mixing_store%iter_method =
"Broy."
179 CALL modified_broyden_mixing(qs_env, mixing_store, rho, para_env)
180 mixing_store%iter_method =
"MBroy"
183 CALL multisecant_mixing(mixing_store, rho, para_env)
184 mixing_store%iter_method =
"MSec."
188 IF (mixing_store%gmix_p .AND. gapw)
THEN
189 CALL gapw_cpc_charge_mag_to_spin(rho_atom, mixing_store)
191 CALL pw_axpy(rho_g(2), rho_g(1), 1.0_dp)
193 CALL pw_axpy(rho_g(1), rho_g(2), -1.0_dp)
195 CALL auxbas_pw_pool%give_back_pw(rho_tmp)
204 CALL timestop(handle)
214 SUBROUTINE gapw_cpc_spin_to_charge_mag(rho_atom, mixing_store)
218 INTEGER :: iatom, icoef1, icoef2, ncoef_h1, &
219 ncoef_h2, ncoef_s1, ncoef_s2
220 REAL(
dp) :: cpc_h_tmp, cpc_s_tmp
222 cpassert(
ASSOCIATED(rho_atom))
223 cpassert(
ASSOCIATED(mixing_store%paw))
225 DO iatom = 1,
SIZE(rho_atom)
226 IF (mixing_store%paw(iatom))
THEN
227 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(1)%r_coef))
228 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(2)%r_coef))
229 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(1)%r_coef))
230 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(2)%r_coef))
231 ncoef_h1 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 1)
232 ncoef_h2 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 2)
233 ncoef_s1 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 1)
234 ncoef_s2 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 2)
235 cpassert(ncoef_h1 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 1))
236 cpassert(ncoef_h2 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 2))
237 cpassert(ncoef_s1 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 1))
238 cpassert(ncoef_s2 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 2))
240 DO icoef2 = 1, ncoef_h2
241 DO icoef1 = 1, ncoef_h1
242 cpc_h_tmp = rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2)
243 rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2) = &
244 cpc_h_tmp + rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2)
245 rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2) = &
246 cpc_h_tmp - rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2)
250 DO icoef2 = 1, ncoef_s2
251 DO icoef1 = 1, ncoef_s1
252 cpc_s_tmp = rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2)
253 rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2) = &
254 cpc_s_tmp + rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2)
255 rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2) = &
256 cpc_s_tmp - rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2)
262 END SUBROUTINE gapw_cpc_spin_to_charge_mag
270 SUBROUTINE gapw_cpc_charge_mag_to_spin(rho_atom, mixing_store)
274 INTEGER :: iatom, icoef1, icoef2, ncoef_h1, &
275 ncoef_h2, ncoef_s1, ncoef_s2
276 REAL(
dp) :: cpc_h_tmp, cpc_s_tmp
278 cpassert(
ASSOCIATED(rho_atom))
279 cpassert(
ASSOCIATED(mixing_store%paw))
281 DO iatom = 1,
SIZE(rho_atom)
282 IF (mixing_store%paw(iatom))
THEN
283 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(1)%r_coef))
284 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_h(2)%r_coef))
285 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(1)%r_coef))
286 cpassert(
ASSOCIATED(rho_atom(iatom)%cpc_s(2)%r_coef))
287 ncoef_h1 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 1)
288 ncoef_h2 =
SIZE(rho_atom(iatom)%cpc_h(1)%r_coef, 2)
289 ncoef_s1 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 1)
290 ncoef_s2 =
SIZE(rho_atom(iatom)%cpc_s(1)%r_coef, 2)
291 cpassert(ncoef_h1 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 1))
292 cpassert(ncoef_h2 ==
SIZE(rho_atom(iatom)%cpc_h(2)%r_coef, 2))
293 cpassert(ncoef_s1 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 1))
294 cpassert(ncoef_s2 ==
SIZE(rho_atom(iatom)%cpc_s(2)%r_coef, 2))
296 DO icoef2 = 1, ncoef_h2
297 DO icoef1 = 1, ncoef_h1
298 cpc_h_tmp = rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2)
299 rho_atom(iatom)%cpc_h(1)%r_coef(icoef1, icoef2) = &
300 0.5_dp*(cpc_h_tmp + rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2))
301 rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2) = &
302 0.5_dp*(cpc_h_tmp - rho_atom(iatom)%cpc_h(2)%r_coef(icoef1, icoef2))
306 DO icoef2 = 1, ncoef_s2
307 DO icoef1 = 1, ncoef_s1
308 cpc_s_tmp = rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2)
309 rho_atom(iatom)%cpc_s(1)%r_coef(icoef1, icoef2) = &
310 0.5_dp*(cpc_s_tmp + rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2))
311 rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2) = &
312 0.5_dp*(cpc_s_tmp - rho_atom(iatom)%cpc_s(2)%r_coef(icoef1, icoef2))
318 END SUBROUTINE gapw_cpc_charge_mag_to_spin
331 SUBROUTINE gmix_potential_only(qs_env, mixing_store, rho)
337 CHARACTER(len=*),
PARAMETER :: routinen =
'gmix_potential_only'
339 COMPLEX(dp),
DIMENSION(:),
POINTER :: cc_new
340 INTEGER :: handle, iatom, ig, ispin, natom, ng, &
343 REAL(
dp) :: alpha, alpha_eff, f_mix
344 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
349 cpassert(
ASSOCIATED(mixing_store%rhoin))
350 cpassert(
ASSOCIATED(mixing_store%kerker_factor))
352 CALL timeset(routinen, handle)
353 NULLIFY (cc_new, dft_control, rho_atom, rho_g, kerker_eff)
355 CALL get_qs_env(qs_env, dft_control=dft_control)
358 nspin =
SIZE(rho_g, 1)
359 ng =
SIZE(rho_g(1)%pw_grid%gsq)
361 gapw = dft_control%qs_control%gapw
362 alpha = mixing_store%alpha
366 IF (ispin >= 2 .AND. nspin >= 2)
THEN
367 alpha_eff = mixing_store%alpha_mag
368 kerker_eff => mixing_store%kerker_factor_mag
370 alpha_eff = mixing_store%alpha
371 kerker_eff => mixing_store%kerker_factor
373 cc_new => rho_g(ispin)%array
374 DO ig = 1, mixing_store%ig_max
375 f_mix = alpha_eff*kerker_eff(ig)
376 cc_new(ig) = (1.0_dp - f_mix)*mixing_store%rhoin(ispin)%cc(ig) + f_mix*cc_new(ig)
377 mixing_store%rhoin(ispin)%cc(ig) = cc_new(ig)
379 DO ig = mixing_store%ig_max + 1, ng
381 cc_new(ig) = (1.0_dp - f_mix)*mixing_store%rhoin(ispin)%cc(ig) + f_mix*cc_new(ig)
382 mixing_store%rhoin(ispin)%cc(ig) = cc_new(ig)
387 IF (mixing_store%gmix_p .AND. gapw)
THEN
389 rho_atom_set=rho_atom)
390 natom =
SIZE(rho_atom)
392 IF (ispin >= 2 .AND. nspin >= 2)
THEN
393 alpha_eff = mixing_store%alpha_mag
395 alpha_eff = mixing_store%alpha
398 IF (mixing_store%paw(iatom))
THEN
399 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
400 mixing_store%cpc_h_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
401 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
402 mixing_store%cpc_s_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
403 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
404 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
410 CALL timestop(handle)
412 END SUBROUTINE gmix_potential_only
428 SUBROUTINE pulay_mixing(qs_env, mixing_store, rho, para_env)
435 CHARACTER(len=*),
PARAMETER :: routinen =
'pulay_mixing'
437 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: cc_mix
438 INTEGER :: handle, i, iatom, ib, ibb, ig, ispin, &
439 jb, n1, n2, natom, nb, nb1, nbuffer, &
442 REAL(
dp) :: alpha_eff, alpha_kerker, alpha_pulay, &
443 beta, f_mix, inv_err, norm, &
444 norm_c_inv, res_norm, vol
445 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: alpha_c, ev
446 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: b, c, c_inv, cpc_h_mix, cpc_s_mix
447 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
452 cpassert(
ASSOCIATED(mixing_store%res_buffer))
453 cpassert(
ASSOCIATED(mixing_store%rhoin_buffer))
454 NULLIFY (dft_control, rho_atom, rho_g, kerker_eff)
455 CALL timeset(routinen, handle)
457 CALL get_qs_env(qs_env, dft_control=dft_control)
459 nspin =
SIZE(rho_g, 1)
460 ng =
SIZE(mixing_store%res_buffer(1, 1)%cc)
461 vol = rho_g(1)%pw_grid%vol
463 alpha_kerker = mixing_store%alpha
464 beta = mixing_store%pulay_beta
465 alpha_pulay = mixing_store%pulay_alpha
466 nbuffer = mixing_store%nbuffer
467 gapw = dft_control%qs_control%gapw
469 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
470 natom =
SIZE(rho_atom)
473 ALLOCATE (cc_mix(ng))
477 IF (ispin >= 2 .AND. nspin >= 2)
THEN
478 alpha_eff = mixing_store%alpha_mag
479 kerker_eff => mixing_store%kerker_factor_mag
481 alpha_eff = mixing_store%alpha
482 kerker_eff => mixing_store%kerker_factor
484 alpha_kerker = alpha_eff
485 ib =
modulo(mixing_store%ncall_p(ispin), nbuffer) + 1
487 mixing_store%ncall_p(ispin) = mixing_store%ncall_p(ispin) + 1
488 nb = min(mixing_store%ncall_p(ispin), nbuffer)
489 ibb =
modulo(mixing_store%ncall_p(ispin), nbuffer) + 1
491 mixing_store%res_buffer(ib, ispin)%cc(:) = cmplx(0._dp, 0._dp, kind=
dp)
493 IF (nb == 1) mixing_store%rhoin_buffer(1, ispin)%cc = mixing_store%rhoin(ispin)%cc
496 f_mix = kerker_eff(ig)
497 mixing_store%res_buffer(ib, ispin)%cc(ig) = f_mix*(rho_g(ispin)%array(ig) - &
498 mixing_store%rhoin_buffer(ib, ispin)%cc(ig))
499 res_norm = res_norm + &
500 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp)*
REAL(mixing_store%res_buffer(ib, ispin)%cc(ig), dp) + &
501 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))*aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))
504 CALL para_env%sum(res_norm)
505 res_norm = sqrt(res_norm)
507 IF (res_norm < 1.e-14_dp)
THEN
508 mixing_store%ncall_p(ispin) = mixing_store%ncall_p(ispin) - 1
511 IF (
ALLOCATED(b))
DEALLOCATE (b)
512 ALLOCATE (b(nb1, nb1))
514 IF (
ALLOCATED(c))
DEALLOCATE (c)
517 IF (
ALLOCATED(c_inv))
DEALLOCATE (c_inv)
518 ALLOCATE (c_inv(nb, nb))
520 IF (
ALLOCATED(alpha_c))
DEALLOCATE (alpha_c)
521 ALLOCATE (alpha_c(nb))
524 IF (
ALLOCATED(ev))
DEALLOCATE (ev)
531 mixing_store%pulay_matrix(jb, ib, ispin) = 0.0_dp
533 f_mix = mixing_store%special_metric(ig)
534 mixing_store%pulay_matrix(jb, ib, ispin) = mixing_store%pulay_matrix(jb, ib, ispin) + &
535 f_mix*(real(mixing_store%res_buffer(jb, ispin)%cc(ig),
dp) &
536 *real(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp) + &
537 aimag(mixing_store%res_buffer(jb, ispin)%cc(ig))* &
538 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig)))
540 CALL para_env%sum(mixing_store%pulay_matrix(jb, ib, ispin))
541 mixing_store%pulay_matrix(jb, ib, ispin) = mixing_store%pulay_matrix(jb, ib, ispin)*vol*vol
542 mixing_store%pulay_matrix(ib, jb, ispin) = mixing_store%pulay_matrix(jb, ib, ispin)
545 IF (nb == 1 .OR. res_norm < 1.e-14_dp)
THEN
547 f_mix = alpha_kerker*kerker_eff(ig)
548 cc_mix(ig) = rho_g(ispin)%array(ig) - &
549 mixing_store%rhoin_buffer(ib, ispin)%cc(ig)
550 rho_g(ispin)%array(ig) = f_mix*cc_mix(ig) + &
551 mixing_store%rhoin_buffer(ib, ispin)%cc(ig)
552 mixing_store%rhoin_buffer(ibb, ispin)%cc(ig) = rho_g(ispin)%array(ig)
554 IF (mixing_store%gmix_p)
THEN
557 IF (mixing_store%paw(iatom))
THEN
558 mixing_store%cpc_h_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
559 mixing_store%cpc_h_in(iatom, ispin)%r_coef
560 mixing_store%cpc_s_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
561 mixing_store%cpc_s_in(iatom, ispin)%r_coef
563 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_kerker*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
564 (1.0_dp - alpha_kerker)*mixing_store%cpc_h_in(iatom, ispin)%r_coef
565 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_kerker*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
566 (1.0_dp - alpha_kerker)*mixing_store%cpc_s_in(iatom, ispin)%r_coef
568 mixing_store%cpc_h_in_buffer(ib, iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
569 mixing_store%cpc_s_in_buffer(ib, iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
571 mixing_store%cpc_h_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
572 mixing_store%cpc_s_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
579 b(1:nb, 1:nb) = mixing_store%pulay_matrix(1:nb, 1:nb, ispin)
580 c(1:nb, 1:nb) = b(1:nb, 1:nb)
581 b(nb1, 1:nb) = -1.0_dp
582 b(1:nb, nb1) = -1.0_dp
589 alpha_c(i) = alpha_c(i) + c_inv(jb, i)
590 norm_c_inv = norm_c_inv + c_inv(jb, i)
593 alpha_c(1:nb) = alpha_c(1:nb)/norm_c_inv
594 cc_mix = cmplx(0._dp, 0._dp, kind=
dp)
597 cc_mix(ig) = cc_mix(ig) + &
598 alpha_c(jb)*(mixing_store%rhoin_buffer(jb, ispin)%cc(ig) + &
599 mixing_store%pulay_beta*mixing_store%res_buffer(jb, ispin)%cc(ig))
602 mixing_store%rhoin_buffer(ibb, ispin)%cc = cmplx(0._dp, 0._dp, kind=
dp)
603 IF (alpha_pulay > 0.0_dp)
THEN
605 f_mix = alpha_pulay*kerker_eff(ig)
606 rho_g(ispin)%array(ig) = f_mix*rho_g(ispin)%array(ig) + &
607 (1.0_dp - f_mix)*cc_mix(ig)
608 mixing_store%rhoin_buffer(ibb, ispin)%cc(ig) = rho_g(ispin)%array(ig)
612 rho_g(ispin)%array(ig) = cc_mix(ig)
613 mixing_store%rhoin_buffer(ibb, ispin)%cc(ig) = rho_g(ispin)%array(ig)
617 IF (mixing_store%gmix_p .AND. gapw)
THEN
618 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
620 IF (mixing_store%paw(iatom))
THEN
621 n1 =
SIZE(rho_atom(iatom)%cpc_h(ispin)%r_coef, 1)
622 n2 =
SIZE(rho_atom(iatom)%cpc_h(ispin)%r_coef, 2)
623 ALLOCATE (cpc_h_mix(n1, n2))
624 ALLOCATE (cpc_s_mix(n1, n2))
626 mixing_store%cpc_h_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
627 mixing_store%cpc_h_in_buffer(ib, iatom, ispin)%r_coef
628 mixing_store%cpc_s_res_buffer(ib, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
629 mixing_store%cpc_s_in_buffer(ib, iatom, ispin)%r_coef
633 cpc_h_mix(:, :) = cpc_h_mix(:, :) + &
634 alpha_c(jb)*mixing_store%cpc_h_in_buffer(jb, iatom, ispin)%r_coef(:, :) + &
635 alpha_c(jb)*beta*mixing_store%cpc_h_res_buffer(jb, iatom, ispin)%r_coef(:, :)
636 cpc_s_mix(:, :) = cpc_s_mix(:, :) + &
637 alpha_c(jb)*mixing_store%cpc_s_in_buffer(jb, iatom, ispin)%r_coef(:, :) + &
638 alpha_c(jb)*beta*mixing_store%cpc_s_res_buffer(jb, iatom, ispin)%r_coef(:, :)
640 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_pulay*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
641 (1.0_dp - alpha_pulay)*cpc_h_mix
642 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_pulay*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
643 (1.0_dp - alpha_pulay)*cpc_s_mix
644 mixing_store%cpc_h_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
645 mixing_store%cpc_s_in_buffer(ibb, iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
646 DEALLOCATE (cpc_h_mix)
647 DEALLOCATE (cpc_s_mix)
653 IF (res_norm > 1.e-14_dp)
THEN
656 DEALLOCATE (c, c_inv, alpha_c)
663 CALL timestop(handle)
665 END SUBROUTINE pulay_mixing
679 SUBROUTINE broyden_mixing(qs_env, mixing_store, rho, para_env)
686 CHARACTER(len=*),
PARAMETER :: routinen =
'broyden_mixing'
688 COMPLEX(dp) :: cc_mix
689 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: res_rho
690 INTEGER :: handle, i, iatom, ib, ig, ispin, j, jb, &
691 kb, n1, n2, natom, nb, nbuffer, ng, &
693 LOGICAL :: gapw, only_kerker
694 REAL(
dp) :: alpha, alpha_eff, dcpc_h_res, &
695 dcpc_s_res, delta_norm, f_mix, &
696 inv_err, res_norm, rho_norm, valh, &
698 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: c, g
699 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: a, b
700 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
705 cpassert(
ASSOCIATED(mixing_store%res_buffer))
706 cpassert(
ASSOCIATED(mixing_store%rhoin))
707 cpassert(
ASSOCIATED(mixing_store%rhoin_old))
708 cpassert(
ASSOCIATED(mixing_store%drho_buffer))
709 NULLIFY (dft_control, rho_atom, rho_g, kerker_eff)
710 CALL timeset(routinen, handle)
712 CALL get_qs_env(qs_env, dft_control=dft_control)
715 nspin =
SIZE(rho_g, 1)
716 ng =
SIZE(mixing_store%res_buffer(1, 1)%cc)
718 alpha = mixing_store%alpha
719 w0 = mixing_store%broy_w0
720 nbuffer = mixing_store%nbuffer
721 gapw = dft_control%qs_control%gapw
723 ALLOCATE (res_rho(ng))
725 mixing_store%ncall = mixing_store%ncall + 1
726 IF (mixing_store%ncall == 1)
THEN
729 only_kerker = .false.
730 nb = min(mixing_store%ncall - 1, nbuffer)
731 ib =
modulo(mixing_store%ncall - 2, nbuffer) + 1
735 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
736 natom =
SIZE(rho_atom)
743 IF (ispin >= 2 .AND. nspin >= 2)
THEN
744 alpha_eff = mixing_store%alpha_mag
745 kerker_eff => mixing_store%kerker_factor_mag
747 alpha_eff = mixing_store%alpha
748 kerker_eff => mixing_store%kerker_factor
752 res_rho(ig) = rho_g(ispin)%array(ig) - mixing_store%rhoin(ispin)%cc(ig)
755 IF (only_kerker)
THEN
757 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
758 f_mix = alpha_eff*kerker_eff(ig)
759 rho_g(ispin)%array(ig) = mixing_store%rhoin(ispin)%cc(ig) + f_mix*res_rho(ig)
760 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
761 mixing_store%rhoin(ispin)%cc(ig) = rho_g(ispin)%array(ig)
764 IF (mixing_store%gmix_p)
THEN
767 IF (mixing_store%paw(iatom))
THEN
768 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
769 mixing_store%cpc_h_in(iatom, ispin)%r_coef
770 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
771 mixing_store%cpc_s_in(iatom, ispin)%r_coef
773 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
774 mixing_store%cpc_h_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
775 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
776 mixing_store%cpc_s_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
778 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
779 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
780 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
781 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
801 mixing_store%res_buffer(ib, ispin)%cc(ig) = res_rho(ig) - mixing_store%last_res(ispin)%cc(ig)
802 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
803 res_norm = res_norm + &
804 REAL(res_rho(ig),
dp)*
REAL(res_rho(ig), dp) + &
805 aimag(res_rho(ig))*aimag(res_rho(ig))
806 delta_norm = delta_norm + &
807 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp)* &
808 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig), dp) + &
809 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))* &
810 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))
811 rho_norm = rho_norm + &
812 REAL(rho_g(ispin)%array(ig),
dp)*
REAL(rho_g(ispin)%array(ig), dp) + &
813 aimag(rho_g(ispin)%array(ig))*aimag(rho_g(ispin)%array(ig))
816 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
817 mixing_store%rhoin(ispin)%cc(ig) - &
818 mixing_store%rhoin_old(ispin)%cc(ig)
820 CALL para_env%sum(delta_norm)
821 delta_norm = sqrt(delta_norm)
822 CALL para_env%sum(res_norm)
823 res_norm = sqrt(res_norm)
824 CALL para_env%sum(rho_norm)
825 rho_norm = sqrt(rho_norm)
827 IF (res_norm > 1.e-14_dp)
THEN
828 mixing_store%res_buffer(ib, ispin)%cc(:) = mixing_store%res_buffer(ib, ispin)%cc(:)/delta_norm
829 mixing_store%drho_buffer(ib, ispin)%cc(:) = mixing_store%drho_buffer(ib, ispin)%cc(:)/delta_norm
831 IF (mixing_store%gmix_p .AND. gapw)
THEN
833 IF (mixing_store%paw(iatom))
THEN
834 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
835 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
838 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
839 (mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i) - &
840 mixing_store%cpc_h_old(iatom, ispin)%r_coef(j, i))/delta_norm
841 dcpc_h_res = ((rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
842 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)) - &
843 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
844 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
845 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)
847 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
848 (mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i) - &
849 mixing_store%cpc_s_old(iatom, ispin)%r_coef(j, i))/delta_norm
850 dcpc_s_res = ((rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
851 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)) - &
852 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
853 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
854 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)
856 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
857 alpha_eff*dcpc_h_res + &
858 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i)
859 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
860 alpha_eff*dcpc_s_res + &
861 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i)
870 f_mix = alpha_eff*kerker_eff(ig)
871 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
872 f_mix*mixing_store%res_buffer(ib, ispin)%cc(ig) + &
873 mixing_store%drho_buffer(ib, ispin)%cc(ig)
877 DO ig = 1, mixing_store%ig_max
878 a(kb, jb) = a(kb, jb) + mixing_store%p_metric(ig)*( &
879 REAL(mixing_store%res_buffer(jb, ispin)%cc(ig),
dp)* &
880 REAL(mixing_store%res_buffer(kb, ispin)%cc(ig), dp) + &
881 aimag(mixing_store%res_buffer(jb, ispin)%cc(ig))* &
882 aimag(mixing_store%res_buffer(kb, ispin)%cc(ig)))
884 a(jb, kb) = a(kb, jb)
891 a(jb, jb) = w0 + a(jb, jb)
892 DO ig = 1, mixing_store%ig_max
893 c(jb) = c(jb) + mixing_store%p_metric(ig)*( &
894 REAL(mixing_store%res_buffer(jb, ispin)%cc(ig),
dp)*
REAL(res_rho(ig), dp) + &
895 aimag(mixing_store%res_buffer(jb, ispin)%cc(ig))*aimag(res_rho(ig)))
901 CALL dgemv(
'T', nb, nb, 1.0_dp, b, nb, c, 1, 0.0_dp, g, 1)
909 cc_mix = cc_mix - g(jb)*mixing_store%drho_buffer(jb, ispin)%cc(ig)
911 f_mix = alpha_eff*kerker_eff(ig)
913 IF (res_norm > 1.e-14_dp) rho_g(ispin)%array(ig) = mixing_store%rhoin(ispin)%cc(ig) + &
914 f_mix*res_rho(ig) + cc_mix
915 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
916 mixing_store%rhoin(ispin)%cc(ig) = rho_g(ispin)%array(ig)
919 IF (mixing_store%gmix_p)
THEN
922 IF (mixing_store%paw(iatom))
THEN
923 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
924 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
930 valh = valh - g(jb)*mixing_store%dcpc_h_in(jb, iatom, ispin)%r_coef(j, i)
931 vals = vals - g(jb)*mixing_store%dcpc_s_in(jb, iatom, ispin)%r_coef(j, i)
933 IF (res_norm > 1.e-14_dp)
THEN
934 rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) = &
935 alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) + &
936 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + valh
937 rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) = &
938 alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) + &
939 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + vals
944 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
945 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
946 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
947 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
953 DEALLOCATE (a, b, c, g)
960 CALL timestop(handle)
962 END SUBROUTINE broyden_mixing
973 SUBROUTINE modified_broyden_mixing(qs_env, mixing_store, rho, para_env)
980 CHARACTER(len=*),
PARAMETER :: routinen =
'modified_broyden_mixing'
982 COMPLEX(dp) :: cc_mix
983 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: res_rho
984 INTEGER :: handle, i, iatom, ib, ig, ispin, j, jb, &
985 kb, n1, n2, natom, nb, nbuffer, ng, &
987 LOGICAL :: can_update, gapw, only_kerker
988 REAL(
dp) :: alpha_eff, broy_w0, broy_wmax, broy_wmin, broy_wref, dcpc_h_res, dcpc_s_res, &
989 delta_norm, f_mix, inv_err, res_norm, rho_norm, valh, vals
990 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: c, g
991 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: a, b
992 REAL(
dp),
DIMENSION(:),
POINTER :: kerker_eff
997 cpassert(
ASSOCIATED(mixing_store%res_buffer))
998 cpassert(
ASSOCIATED(mixing_store%rhoin))
999 cpassert(
ASSOCIATED(mixing_store%rhoin_old))
1000 cpassert(
ASSOCIATED(mixing_store%drho_buffer))
1001 cpassert(
ASSOCIATED(mixing_store%weight))
1002 NULLIFY (dft_control, rho_atom, rho_g, kerker_eff)
1003 CALL timeset(routinen, handle)
1005 CALL get_qs_env(qs_env, dft_control=dft_control)
1008 nspin =
SIZE(rho_g, 1)
1009 ng =
SIZE(mixing_store%res_buffer(1, 1)%cc)
1011 broy_w0 = mixing_store%broy_w0
1012 broy_wref = mixing_store%wc
1013 broy_wmax = mixing_store%wmax
1015 nbuffer = mixing_store%nbuffer
1016 gapw = dft_control%qs_control%gapw
1018 ALLOCATE (res_rho(ng))
1020 mixing_store%ncall = mixing_store%ncall + 1
1021 IF (mixing_store%ncall == 1)
THEN
1022 only_kerker = .true.
1024 only_kerker = .false.
1025 nb = min(mixing_store%ncall - 1, nbuffer)
1026 ib =
modulo(mixing_store%ncall - 2, nbuffer) + 1
1030 CALL get_qs_env(qs_env=qs_env, rho_atom_set=rho_atom)
1031 natom =
SIZE(rho_atom)
1037 IF (ispin >= 2 .AND. nspin >= 2)
THEN
1038 alpha_eff = mixing_store%alpha_mag
1039 kerker_eff => mixing_store%kerker_factor_mag
1041 alpha_eff = mixing_store%alpha
1042 kerker_eff => mixing_store%kerker_factor
1046 res_rho(ig) = rho_g(ispin)%array(ig) - mixing_store%rhoin(ispin)%cc(ig)
1049 IF (only_kerker)
THEN
1051 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
1052 f_mix = alpha_eff*kerker_eff(ig)
1053 rho_g(ispin)%array(ig) = mixing_store%rhoin(ispin)%cc(ig) + f_mix*res_rho(ig)
1054 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
1055 mixing_store%rhoin(ispin)%cc(ig) = rho_g(ispin)%array(ig)
1058 IF (mixing_store%gmix_p)
THEN
1061 IF (mixing_store%paw(iatom))
THEN
1062 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef - &
1063 mixing_store%cpc_h_in(iatom, ispin)%r_coef
1064 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef - &
1065 mixing_store%cpc_s_in(iatom, ispin)%r_coef
1067 rho_atom(iatom)%cpc_h(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef + &
1068 mixing_store%cpc_h_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
1069 rho_atom(iatom)%cpc_s(ispin)%r_coef = alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef + &
1070 mixing_store%cpc_s_in(iatom, ispin)%r_coef*(1._dp - alpha_eff)
1072 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
1073 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
1074 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
1075 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
1082 ALLOCATE (a(nb, nb))
1084 ALLOCATE (b(nb, nb))
1095 mixing_store%res_buffer(ib, ispin)%cc(ig) = res_rho(ig) - mixing_store%last_res(ispin)%cc(ig)
1096 mixing_store%last_res(ispin)%cc(ig) = res_rho(ig)
1097 res_norm = res_norm + &
1098 REAL(res_rho(ig),
dp)*
REAL(res_rho(ig), dp) + &
1099 aimag(res_rho(ig))*aimag(res_rho(ig))
1100 delta_norm = delta_norm + &
1101 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig),
dp)* &
1102 REAL(mixing_store%res_buffer(ib, ispin)%cc(ig), dp) + &
1103 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))* &
1104 aimag(mixing_store%res_buffer(ib, ispin)%cc(ig))
1105 rho_norm = rho_norm + &
1106 REAL(rho_g(ispin)%array(ig),
dp)*
REAL(rho_g(ispin)%array(ig), dp) + &
1107 aimag(rho_g(ispin)%array(ig))*aimag(rho_g(ispin)%array(ig))
1110 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
1111 mixing_store%rhoin(ispin)%cc(ig) - &
1112 mixing_store%rhoin_old(ispin)%cc(ig)
1114 CALL para_env%sum(delta_norm)
1115 delta_norm = sqrt(delta_norm)
1116 CALL para_env%sum(res_norm)
1117 res_norm = sqrt(res_norm)
1118 CALL para_env%sum(rho_norm)
1119 rho_norm = sqrt(rho_norm)
1121 IF (res_norm > (broy_wref/broy_wmax))
THEN
1122 mixing_store%weight(ib, ispin) = broy_wref/res_norm
1124 mixing_store%weight(ib, ispin) = broy_wmax
1126 mixing_store%weight(ib, ispin) = max(broy_wmin, mixing_store%weight(ib, ispin))
1127 can_update = res_norm > 1.e-14_dp .AND. delta_norm > 1.e-14_dp
1129 IF (can_update)
THEN
1130 mixing_store%res_buffer(ib, ispin)%cc(:) = mixing_store%res_buffer(ib, ispin)%cc(:)/delta_norm
1131 mixing_store%drho_buffer(ib, ispin)%cc(:) = mixing_store%drho_buffer(ib, ispin)%cc(:)/delta_norm
1133 IF (mixing_store%gmix_p .AND. gapw)
THEN
1135 IF (mixing_store%paw(iatom))
THEN
1136 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
1137 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
1140 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
1141 (mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i) - &
1142 mixing_store%cpc_h_old(iatom, ispin)%r_coef(j, i))/delta_norm
1143 dcpc_h_res = ((rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
1144 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)) - &
1145 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
1146 mixing_store%cpc_h_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) - &
1147 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)
1149 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
1150 (mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i) - &
1151 mixing_store%cpc_s_old(iatom, ispin)%r_coef(j, i))/delta_norm
1152 dcpc_s_res = ((rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
1153 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)) - &
1154 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i))/delta_norm
1155 mixing_store%cpc_s_lastres(iatom, ispin)%r_coef(j, i) = rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) - &
1156 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)
1158 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i) = &
1159 alpha_eff*dcpc_h_res + &
1160 mixing_store%dcpc_h_in(ib, iatom, ispin)%r_coef(j, i)
1161 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i) = &
1162 alpha_eff*dcpc_s_res + &
1163 mixing_store%dcpc_s_in(ib, iatom, ispin)%r_coef(j, i)
1172 f_mix = alpha_eff*kerker_eff(ig)
1173 mixing_store%drho_buffer(ib, ispin)%cc(ig) = &
1174 f_mix*mixing_store%res_buffer(ib, ispin)%cc(ig) + &
1175 mixing_store%drho_buffer(ib, ispin)%cc(ig)
1179 DO ig = 1, mixing_store%ig_max
1180 a(kb, jb) = a(kb, jb) + mixing_store%p_metric(ig)*( &
1181 REAL(mixing_store%res_buffer(jb, ispin)%cc(ig),
dp)* &
1182 REAL(mixing_store%res_buffer(kb, ispin)%cc(ig), dp) + &
1183 aimag(mixing_store%res_buffer(jb, ispin)%cc(ig))* &
1184 aimag(mixing_store%res_buffer(kb, ispin)%cc(ig)))
1186 a(jb, kb) = a(kb, jb)
1189 CALL para_env%sum(a)
1193 DO ig = 1, mixing_store%ig_max
1194 c(jb) = c(jb) + mixing_store%p_metric(ig)*( &
1195 REAL(mixing_store%res_buffer(jb, ispin)%cc(ig),
dp)*
REAL(res_rho(ig), dp) + &
1196 aimag(mixing_store%res_buffer(jb, ispin)%cc(ig))*aimag(res_rho(ig)))
1199 CALL para_env%sum(c)
1202 c(jb) = mixing_store%weight(jb, ispin)*c(jb)
1204 a(kb, jb) = mixing_store%weight(kb, ispin)*mixing_store%weight(jb, ispin)*a(kb, jb)
1206 a(jb, jb) = broy_w0*broy_w0 + a(jb, jb)
1210 CALL dgemv(
'T', nb, nb, 1.0_dp, b, nb, c, 1, 0.0_dp, g, 1)
1213 mixing_store%res_buffer(ib, ispin)%cc =
z_zero
1214 mixing_store%drho_buffer(ib, ispin)%cc =
z_zero
1220 cc_mix = cc_mix - mixing_store%weight(jb, ispin)*g(jb)*mixing_store%drho_buffer(jb, ispin)%cc(ig)
1222 f_mix = alpha_eff*kerker_eff(ig)
1224 IF (res_norm > 1.e-14_dp) rho_g(ispin)%array(ig) = mixing_store%rhoin(ispin)%cc(ig) + &
1225 f_mix*res_rho(ig) + cc_mix
1226 mixing_store%rhoin_old(ispin)%cc(ig) = mixing_store%rhoin(ispin)%cc(ig)
1227 mixing_store%rhoin(ispin)%cc(ig) = rho_g(ispin)%array(ig)
1230 IF (mixing_store%gmix_p)
THEN
1233 IF (mixing_store%paw(iatom))
THEN
1234 n1 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 1)
1235 n2 =
SIZE(mixing_store%cpc_s_in(iatom, ispin)%r_coef, 2)
1241 valh = valh - mixing_store%weight(jb, ispin)*g(jb)* &
1242 mixing_store%dcpc_h_in(jb, iatom, ispin)%r_coef(j, i)
1243 vals = vals - mixing_store%weight(jb, ispin)*g(jb)* &
1244 mixing_store%dcpc_s_in(jb, iatom, ispin)%r_coef(j, i)
1246 IF (res_norm > 1.e-14_dp)
THEN
1247 rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) = &
1248 alpha_eff*rho_atom(iatom)%cpc_h(ispin)%r_coef(j, i) + &
1249 mixing_store%cpc_h_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + valh
1250 rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) = &
1251 alpha_eff*rho_atom(iatom)%cpc_s(ispin)%r_coef(j, i) + &
1252 mixing_store%cpc_s_in(iatom, ispin)%r_coef(j, i)*(1._dp - alpha_eff) + vals
1257 mixing_store%cpc_h_old(iatom, ispin)%r_coef = mixing_store%cpc_h_in(iatom, ispin)%r_coef
1258 mixing_store%cpc_s_old(iatom, ispin)%r_coef = mixing_store%cpc_s_in(iatom, ispin)%r_coef
1259 mixing_store%cpc_h_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_h(ispin)%r_coef
1260 mixing_store%cpc_s_in(iatom, ispin)%r_coef = rho_atom(iatom)%cpc_s(ispin)%r_coef
1266 DEALLOCATE (a, b, c, g)
1271 DEALLOCATE (res_rho)
1273 CALL timestop(handle)
1275 END SUBROUTINE modified_broyden_mixing
1290 SUBROUTINE multisecant_mixing(mixing_store, rho, para_env)
1296 CHARACTER(len=*),
PARAMETER :: routinen =
'multisecant_mixing'
1297 COMPLEX(KIND=dp),
PARAMETER :: cmone = (-1.0_dp, 0.0_dp), &
1298 cone = (1.0_dp, 0.0_dp), &
1299 czero = (0.0_dp, 0.0_dp)
1301 COMPLEX(dp) :: saa, yaa
1302 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:) :: gn_global, pgn, res_matrix_global, &
1304 COMPLEX(dp),
ALLOCATABLE,
DIMENSION(:, :) :: a_matrix, res_matrix, sa, step_matrix, ya
1305 COMPLEX(dp),
DIMENSION(:),
POINTER :: gn
1306 INTEGER :: handle, ib, ib_next, ib_prev, ig, &
1307 ig_global, iig, ispin, jb, kb, nb, &
1308 nbuffer, ng, ng_global, nspin
1309 LOGICAL :: use_zgemm, use_zgemm_rev
1310 REAL(
dp) :: alpha, alpha_eff, f_mix, gn_norm, gn_norm_old, inv_err, n_low, n_up, pgn_norm, &
1311 prec, r_step, reg_par, sigma_max, sigma_tilde, step_size
1312 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: norm_res, norm_res_low, norm_res_up
1313 REAL(
dp),
ALLOCATABLE,
DIMENSION(:, :) :: b_matrix, binv_matrix
1314 REAL(
dp),
DIMENSION(:),
POINTER :: g2, kerker_eff
1315 REAL(
dp),
SAVE :: sigma_old = 1.0_dp
1318 cpassert(
ASSOCIATED(mixing_store))
1320 CALL timeset(routinen, handle)
1325 use_zgemm_rev = .true.
1330 nspin =
SIZE(rho_g, 1)
1332 cpassert(rho_g(1)%pw_grid%ngpts < huge(ng_global))
1333 ng_global = int(rho_g(1)%pw_grid%ngpts)
1334 ng =
SIZE(mixing_store%rhoin_buffer(1, 1)%cc)
1335 alpha = mixing_store%alpha
1337 sigma_max = mixing_store%sigma_max
1338 reg_par = mixing_store%reg_par
1339 r_step = mixing_store%r_step
1340 nbuffer = mixing_store%nbuffer
1343 nb = min(mixing_store%ncall, nbuffer - 1)
1344 ib =
modulo(mixing_store%ncall, nbuffer) + 1
1345 IF (mixing_store%ncall > 0)
THEN
1346 ib_prev =
modulo(mixing_store%ncall - 1, nbuffer) + 1
1350 mixing_store%ncall = mixing_store%ncall + 1
1351 ib_next =
modulo(mixing_store%ncall, nbuffer) + 1
1355 gn => mixing_store%res_buffer(ib, ispin)%cc
1358 gn(ig) = (rho_g(ispin)%array(ig) - mixing_store%rhoin_buffer(ib, ispin)%cc(ig))
1359 gn_norm = gn_norm + &
1360 REAL(gn(ig),
dp)*
REAL(gn(ig), dp) + aimag(gn(ig))*aimag(gn(ig))
1362 CALL para_env%sum(gn_norm)
1363 gn_norm = sqrt(gn_norm)
1364 mixing_store%norm_res_buffer(ib, ispin) = gn_norm
1370 IF (ispin >= 2 .AND. nspin >= 2)
THEN
1371 alpha_eff = mixing_store%alpha_mag
1372 kerker_eff => mixing_store%kerker_factor_mag
1374 alpha_eff = mixing_store%alpha
1375 kerker_eff => mixing_store%kerker_factor
1378 f_mix = alpha_eff*kerker_eff(ig)
1379 rho_g(ispin)%array(ig) = mixing_store%rhoin_buffer(1, ispin)%cc(ig) + &
1380 f_mix*mixing_store%res_buffer(1, ispin)%cc(ig)
1381 mixing_store%rhoin_buffer(ib_next, ispin)%cc(ig) = rho_g(ispin)%array(ig)
1384 CALL timestop(handle)
1390 ALLOCATE (step_matrix(ng, nb))
1392 ALLOCATE (res_matrix(ng, nb))
1394 ALLOCATE (a_matrix(nb, ng_global))
1396 ALLOCATE (norm_res(nb))
1397 ALLOCATE (norm_res_low(nb))
1398 ALLOCATE (norm_res_up(nb))
1401 ALLOCATE (b_matrix(nb, nb))
1403 ALLOCATE (binv_matrix(nb, nb))
1405 ALLOCATE (gn_global(ng_global))
1406 ALLOCATE (res_matrix_global(ng_global))
1408 ALLOCATE (sa(ng, ng_global))
1409 ALLOCATE (ya(ng, ng_global))
1411 IF (use_zgemm_rev)
THEN
1412 ALLOCATE (tmp_vec(nb))
1419 IF (ispin >= 2 .AND. nspin >= 2)
THEN
1420 alpha_eff = mixing_store%alpha_mag
1421 kerker_eff => mixing_store%kerker_factor_mag
1423 alpha_eff = mixing_store%alpha
1424 kerker_eff => mixing_store%kerker_factor
1427 gn => mixing_store%res_buffer(ib, ispin)%cc
1430 ig_global = mixing_store%ig_global_index(ig)
1431 gn_global(ig_global) = gn(ig)
1433 CALL para_env%sum(gn_global)
1440 g2 => rho_g(1)%pw_grid%gsq
1445 ELSEIF (jb > ib)
THEN
1450 norm_res(kb) = 0.0_dp
1451 norm_res_low(kb) = 0.0_dp
1452 norm_res_up(kb) = 0.0_dp
1457 step_matrix(ig, kb) = prec*(mixing_store%rhoin_buffer(jb, ispin)%cc(ig) - &
1458 mixing_store%rhoin_buffer(ib, ispin)%cc(ig))
1459 res_matrix(ig, kb) = (mixing_store%res_buffer(jb, ispin)%cc(ig) - &
1460 mixing_store%res_buffer(ib, ispin)%cc(ig))
1461 norm_res(kb) = norm_res(kb) + real(res_matrix(ig, kb),
dp)*real(res_matrix(ig, kb),
dp) + &
1462 aimag(res_matrix(ig, kb))*aimag(res_matrix(ig, kb))
1463 IF (g2(ig) < 4.0_dp)
THEN
1464 norm_res_low(kb) = norm_res_low(kb) + &
1465 REAL(res_matrix(ig, kb),
dp)*
REAL(res_matrix(ig, kb), dp) + &
1466 aimag(res_matrix(ig, kb))*aimag(res_matrix(ig, kb))
1468 norm_res_up(kb) = norm_res_up(kb) + &
1469 REAL(res_matrix(ig, kb),
dp)*
REAL(res_matrix(ig, kb), dp) + &
1470 aimag(res_matrix(ig, kb))*aimag(res_matrix(ig, kb))
1472 res_matrix(ig, kb) = prec*res_matrix(ig, kb)
1477 CALL para_env%sum(norm_res)
1478 CALL para_env%sum(norm_res_up)
1479 CALL para_env%sum(norm_res_low)
1480 norm_res(1:nb) = 1.0_dp/sqrt(norm_res(1:nb))
1484 n_low = n_low + norm_res_low(jb)/norm_res(jb)
1485 n_up = n_up + norm_res_up(jb)/norm_res(jb)
1488 IF (g2(ig) > 4.0_dp)
THEN
1489 step_matrix(ig, 1:nb) = step_matrix(ig, 1:nb)*sqrt(n_low/n_up)
1490 res_matrix(ig, 1:nb) = res_matrix(ig, 1:nb)*sqrt(n_low/n_up)
1494 step_matrix(1:ng, kb) = step_matrix(1:ng, kb)*norm_res(kb)
1495 res_matrix(1:ng, kb) = res_matrix(1:ng, kb)*norm_res(kb)
1502 b_matrix(kb, jb) = 0.0_dp
1506 b_matrix(kb, jb) = b_matrix(kb, jb) + real(res_matrix(ig, kb)*res_matrix(ig, jb),
dp)
1511 CALL para_env%sum(b_matrix)
1513 b_matrix(jb, jb) = b_matrix(jb, jb) + reg_par
1521 res_matrix_global =
z_zero
1523 ig_global = mixing_store%ig_global_index(ig)
1524 res_matrix_global(ig_global) = res_matrix(ig, kb)
1526 CALL para_env%sum(res_matrix_global)
1530 ig_global = mixing_store%ig_global_index(ig)
1531 a_matrix(jb, ig_global) = a_matrix(jb, ig_global) + &
1532 binv_matrix(jb, kb)*res_matrix_global(ig_global)
1536 CALL para_env%sum(a_matrix)
1539 gn => mixing_store%res_buffer(ib, ispin)%cc
1544 CALL zgemm(
"N",
"N", ng, ng_global, nb, cmone, step_matrix(1, 1), ng, &
1545 a_matrix(1, 1), nb, czero, sa(1, 1), ng)
1546 CALL zgemm(
"N",
"N", ng, ng_global, nb, cmone, res_matrix(1, 1), ng, &
1547 a_matrix(1, 1), nb, czero, ya(1, 1), ng)
1549 ig_global = mixing_store%ig_global_index(ig)
1550 ya(ig, ig_global) = ya(ig, ig_global) +
z_one
1553 CALL zgemv(
"N", ng, ng_global, cone, sa(1, 1), &
1554 ng, gn_global(1), 1, czero, pgn(1), 1)
1555 CALL zgemv(
"N", ng, ng_global, cone, ya(1, 1), &
1556 ng, gn_global(1), 1, czero, ugn(1), 1)
1559 pgn_norm = pgn_norm + real(pgn(ig),
dp)*real(pgn(ig),
dp) + &
1560 aimag(pgn(ig))*aimag(pgn(ig))
1562 CALL para_env%sum(pgn_norm)
1563 ELSEIF (use_zgemm_rev)
THEN
1565 CALL zgemv(
"N", nb, ng_global, cone, a_matrix(1, 1), &
1566 nb, gn_global(1), 1, czero, tmp_vec(1), 1)
1568 CALL zgemv(
"N", ng, nb, cmone, step_matrix(1, 1), ng, &
1569 tmp_vec(1), 1, czero, pgn(1), 1)
1571 CALL zgemv(
"N", ng, nb, cmone, res_matrix(1, 1), ng, &
1572 tmp_vec(1), 1, czero, ugn(1), 1)
1575 pgn_norm = pgn_norm + real(pgn(ig),
dp)*real(pgn(ig),
dp) + &
1576 aimag(pgn(ig))*aimag(pgn(ig))
1577 ugn(ig) = ugn(ig) + gn(ig)
1579 CALL para_env%sum(pgn_norm)
1585 ig_global = mixing_store%ig_global_index(ig)
1586 DO iig = 1, ng_global
1590 IF (ig_global == iig) yaa =
z_one
1593 saa = saa - step_matrix(ig, jb)*a_matrix(jb, iig)
1594 yaa = yaa - res_matrix(ig, jb)*a_matrix(jb, iig)
1596 pgn(ig) = pgn(ig) + saa*gn_global(iig)
1597 ugn(ig) = ugn(ig) + yaa*gn_global(iig)
1601 pgn_norm = pgn_norm + real(pgn(ig),
dp)*real(pgn(ig),
dp) + &
1602 aimag(pgn(ig))*aimag(pgn(ig))
1604 CALL para_env%sum(pgn_norm)
1607 gn_norm = mixing_store%norm_res_buffer(ib, ispin)
1608 gn_norm_old = mixing_store%norm_res_buffer(ib_prev, ispin)
1609 IF (ib_prev /= 0)
THEN
1610 sigma_tilde = sigma_old*max(0.5_dp, min(2.0_dp, gn_norm_old/gn_norm))
1612 sigma_tilde = 0.5_dp
1614 sigma_tilde = 0.1_dp
1616 step_size = min(sigma_tilde, r_step*pgn_norm/gn_norm, sigma_max)
1617 sigma_old = step_size
1621 prec = kerker_eff(ig)
1622 rho_g(ispin)%array(ig) = mixing_store%rhoin_buffer(ib, ispin)%cc(ig) &
1623 - prec*step_size*ugn(ig) + prec*pgn(ig)
1624 mixing_store%rhoin_buffer(ib_next, ispin)%cc(ig) = rho_g(ispin)%array(ig)
1630 DEALLOCATE (step_matrix, res_matrix)
1631 DEALLOCATE (norm_res)
1632 DEALLOCATE (norm_res_up)
1633 DEALLOCATE (norm_res_low)
1634 DEALLOCATE (a_matrix, b_matrix, binv_matrix)
1635 DEALLOCATE (ugn, pgn)
1639 IF (use_zgemm_rev)
THEN
1640 DEALLOCATE (tmp_vec)
1642 DEALLOCATE (gn_global, res_matrix_global)
1644 CALL timestop(handle)
1646 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.
container for various plainwaves related things
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
module that contains the definitions of the scf types
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, 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)
Driver for the g-space mixing, calls the proper routine given the requested method.
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
contained for different pw related things
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
keeps the density in various representations, keeping track of which ones are valid.