1  !
2  ! Copyright (C) 2016-2019 Samuel Ponce', Roxana Margine, Feliciano Giustino
3  !
4  ! This file is distributed under the terms of the GNU General Public
5  ! License. See the file `LICENSE' in the root directory of the
6  ! present distribution, or http://www.gnu.org/copyleft.gpl.txt .
7  !
8  !----------------------------------------------------------------------
9  MODULE dvqpsi
10  !----------------------------------------------------------------------
11  !!
12  !! This module contains the routines to computes dV_bare/dtau * psi or its components.
13  !!
14  IMPLICIT NONE
15  !
16  CONTAINS
17    !
18    !----------------------------------------------------------------------
19    SUBROUTINE dvqpsi_us3(ik, uact, addnlcc, xxkq, xq0, igk, igkq, npw, npwq, evc)
20    !----------------------------------------------------------------------
21    !!
22    !! This routine calculates dV_bare/dtau * psi for one perturbation
23    !! with a given q. The displacements are described by a vector u.
24    !! The result is stored in dvpsi. The routine is called for each k point
25    !! and for each pattern u. It computes simultaneously all the bands.
26    !! It implements Eq. B29 of PRB 64, 235118 (2001). The contribution
27    !! of the local pseudopotential is calculated here, that of the nonlocal
28    !! pseudopotential in dvqpsi_us_only3.
29    !! Adapted from PH/dvqpsi_us (QE)
30    !!
31    !! RM - Nov/Dec 2014
32    !! Imported the noncolinear case implemented by xlzhang
33    !!
34    !! RM - Jan 2019
35    !! Updated based on QE 6.3
36    !!
37    !! SP - Feb 2020
38    !! Pass the wfc at k (evc) explicitely to work with noncolin rotation.
39    !!
40    USE kinds,                 ONLY : DP
41    USE pwcom,                 ONLY : nbnd
42    USE ions_base,             ONLY : nat, ityp
43    USE cell_base,             ONLY : tpiba
44    USE fft_base,              ONLY : dfftp, dffts
45    USE fft_interfaces,        ONLY : fwfft, invfft
46    USE gvect,                 ONLY : eigts1, eigts2, eigts3, mill, g, ngm
47    USE gvecs,                 ONLY : ngms
48    USE lsda_mod,              ONLY : lsda
49    USE scf,                   ONLY : rho, rho_core
50    USE noncollin_module,      ONLY : nspin_lsda, nspin_gga, npol
51    use uspp_param,            ONLY : upf
52    USE wvfct,                 ONLY : npwx
53    USE nlcc_ph,               ONLY : drc
54    USE uspp,                  ONLY : nlcc_any
55    USE eqv,                   ONLY : dvpsi, dmuxc, vlocq
56    USE qpoint,                ONLY : eigqts
57    USE klist_epw,             ONLY : isk_loc
58    USE gc_lr,                 ONLY : grho, dvxc_rr, dvxc_sr, dvxc_ss, dvxc_s
59    USE funct,                 ONLY : dft_is_gradient, dft_is_nonlocc
60    USE elph2,                 ONLY : lower_band, upper_band, ibndstart
61    USE constants_epw,         ONLY : czero, eps12
62    !
63    IMPLICIT NONE
64    !
65    LOGICAL, INTENT(in) :: addnlcc
66    !! True if NLCC is present
67    INTEGER, INTENT(in) :: ik
68    !! Counter on k-point
69    INTEGER, INTENT(in) :: npw
70    !! Number of k+G-vectors inside 'ecut sphere'
71    INTEGER, INTENT(in) :: npwq
72    !! Number of k+G-vectors inside 'ecut sphere'
73    INTEGER, INTENT(in) :: igk(npw)
74    !! k+G mapping
75    INTEGER, INTENT(in) :: igkq(npwq)
76    !! k+G+q mapping
77    REAL(KIND = DP), INTENT(in) :: xq0(3)
78    !! Current coarse q-point coordinate
79    REAL(KIND = DP), INTENT(in) :: xxkq(3)
80    !! k+q point coordinate
81    COMPLEX(KIND = DP), INTENT(in) :: uact(3 * nat)
82    !! the pattern of displacements
83    COMPLEX(KIND = DP), INTENT(in) :: evc(npwx * npol, nbnd)
84    !! Wavefunction at k
85    !
86    ! Local variables
87    INTEGER :: na
88    !! counter on atoms
89    INTEGER :: mu
90    !! counter on modes
91    INTEGER :: ig
92    !! counter on G vectors
93    INTEGER :: nt
94    !! counter on atomic types
95    INTEGER :: ibnd
96    !! counter on bands
97    INTEGER ::  ir
98    !! counter on real mesh
99    INTEGER :: is
100    !! counter on spin
101    INTEGER :: ip
102    !! counter on polarizations
103    INTEGER :: ierr
104    !! Error status
105    REAL(KIND = DP) :: fac
106    !! spin degeneracy factor
107    COMPLEX(KIND = DP) :: gtau
108    !! e^{-i G * \tau}
109    COMPLEX(KIND = DP) :: u1, u2, u3
110    !! components of displacement pattern u
111    COMPLEX(KIND = DP) :: gu0
112    !! scalar product q * u
113    COMPLEX(KIND = DP) :: gu
114    !! q * u + G * u
115    COMPLEX(KIND = DP) :: fact
116    !! e^{-i q * \tau}
117    COMPLEX(KIND = DP), ALLOCATABLE, TARGET :: aux(:)
118    !! Auxillary variable
119    COMPLEX(KIND = DP), ALLOCATABLE :: aux1(:), aux2(:)
120    !! Auxillary variable
121    COMPLEX(KIND = DP), POINTER :: auxs(:)
122    !! Auxiallary pointer
123    COMPLEX(KIND = DP), ALLOCATABLE :: drhoc(:)
124    !! response core charge density
125    !
126    CALL start_clock('dvqpsi_us3')
127    !
128    IF (nlcc_any .AND. addnlcc) THEN
129      ALLOCATE(drhoc(dfftp%nnr), STAT = ierr)
130      IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error allocating drhoc', 1)
131      ALLOCATE(aux(dfftp%nnr), STAT = ierr)
132      IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error allocating aux', 1)
133      ALLOCATE(auxs(dffts%nnr), STAT = ierr)
134      IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error allocating auxs', 1)
135    ENDIF
136    ALLOCATE(aux1(dffts%nnr), STAT = ierr)
137    IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error allocating aux1', 1)
138    ALLOCATE(aux2(dffts%nnr), STAT = ierr)
139    IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error allocating aux2', 1)
140    !
141    !    We start by computing the contribution of the local potential.
142    !    The computation of the derivative of the local potential is done in
143    !    reciprocal space while the product with the wavefunction is done in real space
144    !
145    dvpsi(:, :) = czero
146    aux1(:) = czero
147    DO na = 1, nat
148      fact = tpiba * (0.d0, -1.d0) * eigqts(na)
149      mu = 3 * (na - 1)
150      u1 = uact(mu + 1)
151      u2 = uact(mu + 2)
152      u3 = uact(mu + 3)
153      IF (ABS(u1) + ABS(u2) + ABS(u3) > eps12) THEN
154        nt = ityp(na)
155        gu0 = xq0(1) * u1 + xq0(2) * u2 + xq0(3) * u3
156        DO ig = 1, ngms
157          gtau = eigts1(mill(1, ig), na) * &
158                 eigts2(mill(2, ig), na) * &
159                 eigts3(mill(3, ig), na)
160          gu = gu0 + g(1, ig) * u1 + g(2, ig) * u2 + g(3, ig) * u3
161          aux1(dffts%nl(ig)) = aux1(dffts%nl(ig)) + vlocq(ig, nt) * gu * fact * gtau
162        ENDDO
163      ENDIF
164    ENDDO
165    !
166    ! add NLCC when present
167    !
168    IF (nlcc_any .AND. addnlcc) THEN
169      drhoc(:) = czero
170      DO na = 1, nat
171        fact = tpiba * (0.d0, -1.d0) * eigqts(na)
172        mu = 3 * (na - 1)
173        u1 = uact(mu + 1)
174        u2 = uact(mu + 2)
175        u3 = uact(mu + 3)
176        IF (ABS(u1) + ABS(u2) + ABS(u3) > eps12) THEN
177          nt = ityp(na)
178          gu0 = xq0(1) * u1 + xq0(2) * u2 + xq0(3) * u3
179          IF (upf(nt)%nlcc) THEN
180            DO ig = 1, ngm
181              gtau = eigts1(mill(1, ig), na) * &
182                     eigts2(mill(2, ig), na) * &
183                     eigts3(mill(3, ig), na)
184              gu = gu0 + g(1, ig) * u1 + g(2, ig) * u2 + g(3, ig) * u3
185              drhoc(dfftp%nl(ig)) = drhoc(dfftp%nl(ig)) + drc(ig, nt) * gu * fact * gtau
186            ENDDO
187          ENDIF
188        ENDIF
189      ENDDO
190      !
191      CALL invfft('Rho', drhoc, dfftp)
192      !
193      aux(:) = czero
194      IF (.NOT. lsda) THEN
195        DO ir = 1, dfftp%nnr
196          aux(ir) = drhoc(ir) * dmuxc(ir, 1, 1)
197        ENDDO
198      ELSE
199        is = isk_loc(ik)
200        DO ir = 1, dfftp%nnr
201          aux(ir) = drhoc(ir) * 0.5d0 * (dmuxc(ir, is, 1) + dmuxc(ir, is, 2))
202        ENDDO
203      ENDIF
204      !
205      fac = 1.d0 / DBLE(nspin_lsda)
206      DO is = 1, nspin_lsda
207        rho%of_r(:, is) = rho%of_r(:, is) + fac * rho_core
208      ENDDO
209      !
210      IF (dft_is_gradient()) THEN
211        CALL dgradcorr(dfftp, rho%of_r, grho, dvxc_rr, dvxc_sr, dvxc_ss, dvxc_s, xq0, drhoc, &
212                       1, nspin_gga, g, aux)
213      ENDIF
214      !
215      IF (dft_is_nonlocc()) THEN
216        CALL dnonloccorr(rho%of_r, drhoc, xq0, aux)
217      ENDIF
218      !
219      DO is = 1, nspin_lsda
220        rho%of_r(:, is) = rho%of_r(:, is) - fac * rho_core
221      ENDDO
222      !
223      CALL fwfft('Rho', aux, dfftp)
224      !
225      ! This is needed also when the smooth and the thick grids coincide to
226      ! cut the potential at the cut-off
227      !
228      auxs(:) = czero
229      DO ig = 1, ngms
230        auxs(dffts%nl(ig)) = aux(dfftp%nl(ig))
231      ENDDO
232      aux1(:) = aux1(:) + auxs(:)
233    ENDIF
234    !
235    ! Now we compute dV_loc/dtau in real space
236    !
237    CALL invfft('Rho', aux1, dffts)
238    DO ibnd = lower_band, upper_band
239      DO ip = 1, npol
240        aux2(:) = czero
241        IF (ip == 1) THEN
242          DO ig = 1, npw
243            aux2(dffts%nl(igk(ig))) = evc(ig, ibnd + ibndstart - 1)
244          ENDDO
245        ELSE
246          DO ig = 1, npw
247            aux2(dffts%nl(igk(ig))) = evc(ig + npwx, ibnd + ibndstart - 1)
248          ENDDO
249        ENDIF
250        !
251        !  This wavefunction is computed in real space
252        !
253        CALL invfft('Wave', aux2, dffts)
254        DO ir = 1, dffts%nnr
255          aux2(ir) = aux2(ir) * aux1(ir)
256        ENDDO
257        !
258        ! and finally dV_loc/dtau * psi is transformed in reciprocal space
259        !
260        CALL fwfft('Wave', aux2, dffts)
261        IF (ip == 1) THEN
262          DO ig = 1, npwq
263            dvpsi(ig, ibnd) = aux2(dffts%nl(igkq(ig)))
264          ENDDO
265        ELSE
266          DO ig = 1, npwq
267            dvpsi(ig + npwx, ibnd) = aux2(dffts%nl(igkq(ig)))
268          ENDDO
269        ENDIF
270      ENDDO
271    ENDDO
272    !
273    IF (nlcc_any .AND. addnlcc) THEN
274      DEALLOCATE(drhoc, STAT = ierr)
275      IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error deallocating drhoc', 1)
276      DEALLOCATE(aux, STAT = ierr)
277      IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error deallocating aux', 1)
278      DEALLOCATE(auxs, STAT = ierr)
279      IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error deallocating auxs', 1)
280    ENDIF
281    DEALLOCATE(aux1, STAT = ierr)
282    IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error deallocating aux1', 1)
283    DEALLOCATE(aux2, STAT = ierr)
284    IF (ierr /= 0) CALL errore('dvqpsi_us3', 'Error deallocating aux2', 1)
285    !
286    ! We add the contribution of the nonlocal potential in the US form
287    ! First a term similar to the KB case.
288    ! Then a term due to the change of the D coefficients in the perturbat
289    !
290    CALL dvqpsi_us_only3(ik, uact, xxkq, igkq, npwq)
291    !
292    CALL stop_clock('dvqpsi_us3')
293    !
294    RETURN
295    !
296    !----------------------------------------------------------------------
297    END SUBROUTINE dvqpsi_us3
298    !----------------------------------------------------------------------
299    !
300    !----------------------------------------------------------------------
301    SUBROUTINE dvqpsi_us_only3(ik, uact, xxkq, igkq, npwq)
302    !----------------------------------------------------------------------
303    !!
304    !! This routine calculates dV_bare/dtau * psi for one perturbation
305    !! with a given q. The displacements are described by a vector uact.
306    !! The result is stored in dvpsi. The routine is called for each k point
307    !! and for each pattern u. It computes simultaneously all the bands.
308    !! This routine implements Eq. B29 of PRB 64, 235118 (2001).
309    !! Only the contribution of the nonlocal potential is calculated here.
310    !! Adapted from PH/dvqpsi_us_only (QE)
311    !!
312    !-----------------------------------------------------------------------
313    USE kinds,      ONLY : DP
314    USE cell_base,  ONLY : tpiba
315    USE gvect,      ONLY : g
316    USE ions_base,  ONLY : nat, ityp, ntyp => nsp
317    USE lsda_mod,   ONLY : lsda, current_spin, nspin
318    USE spin_orb,   ONLY : lspinorb
319    USE wvfct,      ONLY : npwx, et
320    USE uspp,       ONLY : okvan, nkb, vkb
321    USE uspp_param, ONLY : nh, nhm
322    USE phus,       ONLY : int1, int1_nc, int2, int2_so, alphap
323    USE lrus,       ONLY : becp1
324    USE eqv,        ONLY : dvpsi
325    USE elph2,      ONLY : lower_band, upper_band, ibndstart
326    USE noncollin_module, ONLY : noncolin, npol
327    USE constants_epw,    ONLY : czero, zero, cone, eps12
328    USE klist_epw,  ONLY : isk_loc
329    !
330    IMPLICIT NONE
331    !
332    INTEGER, INTENT(in) :: ik
333    !! the k point
334    INTEGER, INTENT(in) :: npwq
335    !! Number of k+G-vectors inside 'ecut sphere'
336    INTEGER, INTENT(in) :: igkq(npwq)
337    !! k+G+q mapping
338    REAL(KIND = DP), INTENT(in) :: xxkq(3)
339    !! the k+q point (cartesian coordinates)
340    COMPLEX(KIND = DP), INTENT(in) :: uact(3 * nat)
341    !! the pattern of displacements
342    !
343    ! Local variables
344    LOGICAL :: ok
345    !!
346    INTEGER :: na
347    !! Counter on atoms
348    INTEGER :: nb
349    !! Counter on atoms
350    INTEGER :: mu
351    !! Counter on modes
352    INTEGER :: nu
353    !! Counter on modes
354    INTEGER :: ig
355    !! Counter on G vectors
356    INTEGER :: igg
357    !! Auxiliary counter on G vectors
358    INTEGER :: nt
359    !! Counter on atomic types
360    INTEGER :: ibnd
361    !! Counter on bands
362    INTEGER :: ijkb0
363    !! Auxiliary variable for counting
364    INTEGER :: ikb
365    !! Counter on becp functions
366    INTEGER :: jkb
367    !! Counter on becp functions
368    INTEGER :: ipol
369    !! Counter on polarizations
370    INTEGER :: ih
371    !! Counter on beta functions
372    INTEGER :: jh
373    !! Counter on beta functions
374    INTEGER :: is
375    !! Counter on polarization
376    INTEGER :: js
377    !! Counter on polarization
378    INTEGER ::  ijs
379    !! Counter on combined is and js polarization
380    INTEGER :: ierr
381    !! Error status
382    REAL(KIND = DP), ALLOCATABLE :: deff(:, :, :)
383    !
384    COMPLEX(KIND = DP), ALLOCATABLE :: ps1(:, :)
385    !!
386    COMPLEX(KIND = DP), ALLOCATABLE :: ps2(:, :, :)
387    !!
388    COMPLEX(KIND = DP), ALLOCATABLE :: aux(:)
389    !!
390    COMPLEX(KIND = DP), ALLOCATABLE :: deff_nc(:, :, :, :)
391    !!
392    COMPLEX(KIND = DP), ALLOCATABLE :: ps1_nc(:, :, :)
393    !!
394    COMPLEX(KIND = DP), ALLOCATABLE :: ps2_nc(:, :, :, :)
395    !!
396    !
397    CALL start_clock('dvqpsi_us_on')
398    IF (noncolin) THEN
399      ALLOCATE(ps1_nc(nkb, npol, lower_band:upper_band), STAT = ierr)
400      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating ps1_nc', 1)
401      ALLOCATE(ps2_nc(nkb, npol, lower_band:upper_band, 3), STAT = ierr)
402      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating ps2_nc', 1)
403      ALLOCATE(deff_nc(nhm, nhm, nat, nspin), STAT = ierr)
404      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating deff_nc', 1)
405      ps1_nc(:, :, :) = czero
406      ps2_nc(:, :, :, :) = czero
407      deff_nc(:, :, :, :) = czero
408    ELSE
409      ALLOCATE(ps1(nkb, lower_band:upper_band), STAT = ierr)
410      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating ps1', 1)
411      ALLOCATE(ps2(nkb, lower_band:upper_band, 3), STAT = ierr)
412      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating ps2', 1)
413      ALLOCATE(deff(nhm, nhm, nat), STAT = ierr)
414      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating deff', 1)
415      ps1(:, :) = czero
416      ps2(:, :, :) = czero
417      deff(:, :, :) = zero
418    ENDIF
419    ALLOCATE(aux(npwx), STAT = ierr)
420    IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error allocating aux', 1)
421    aux(:) = czero
422    !
423    IF (lsda) current_spin = isk_loc(ik)
424    !
425    !   we first compute the coefficients of the vectors
426    !
427    DO ibnd = lower_band, upper_band
428      IF (noncolin) THEN
429        CALL compute_deff_nc(deff_nc, et(ibnd + ibndstart - 1, ik))
430      ELSE
431        CALL compute_deff(deff, et(ibnd + ibndstart - 1, ik))
432      ENDIF
433      !
434      ijkb0 = 0
435      DO nt = 1, ntyp
436        DO na = 1, nat
437          IF (ityp(na) == nt) THEN
438            mu = 3 * (na - 1)
439            DO ih = 1, nh(nt)
440              ikb = ijkb0 + ih
441              DO jh = 1, nh(nt)
442                jkb = ijkb0 + jh
443                DO ipol = 1, 3
444                  IF (ABS(uact(mu + 1)) + ABS(uact(mu + 2)) + ABS(uact(mu + 3)) > eps12) THEN
445                    IF (noncolin) THEN
446                      ijs = 0
447                      DO is = 1, npol
448                        DO js = 1, npol
449                          ijs = ijs + 1
450                          ps1_nc(ikb, is, ibnd) = ps1_nc(ikb, is, ibnd) + deff_nc(ih, jh, na, ijs) * &
451                                 alphap(ipol, ik)%nc(jkb, js, ibnd + ibndstart - 1) * uact(mu + ipol)
452                          ps2_nc(ikb, is, ibnd, ipol) = ps2_nc(ikb, is, ibnd, ipol) + &
453                                 deff_nc(ih, jh, na, ijs) * becp1(ik)%nc(jkb, js, ibnd + ibndstart - 1) * &
454                                 (0.d0, -1.d0) * uact(mu + ipol) * tpiba
455                        ENDDO
456                      ENDDO
457                    ELSE
458                      ps1(ikb, ibnd) = ps1(ikb, ibnd) + &
459                                       deff(ih, jh, na) * alphap(ipol, ik)%k(jkb, ibnd + ibndstart - 1) * uact(mu + ipol)
460                      ps2(ikb, ibnd, ipol) = ps2(ikb, ibnd, ipol) + deff(ih, jh, na) * becp1(ik)%k(jkb, ibnd + ibndstart - 1) * &
461                                             (0.d0, -1.d0) * uact(mu + ipol) * tpiba
462                    ENDIF
463                    IF (okvan) THEN
464                      IF (noncolin) THEN
465                        ijs = 0
466                        DO is = 1, npol
467                          DO js = 1, npol
468                            ijs = ijs + 1
469                            ps1_nc(ikb, is, ibnd) = ps1_nc(ikb, is, ibnd) + int1_nc(ih, jh, ipol, na, ijs) * &
470                               becp1(ik)%nc(jkb, js, ibnd + ibndstart - 1) * uact(mu + ipol)
471                          ENDDO
472                        ENDDO
473                      ELSE
474                        ps1(ikb, ibnd) = ps1(ikb, ibnd) + int1(ih, jh, ipol, na, current_spin) * &
475                            becp1(ik)%k(jkb, ibnd + ibndstart - 1) * uact(mu + ipol)
476                      ENDIF
477                    ENDIF ! okvan
478                  ENDIF  ! uact>0
479                  IF (okvan) THEN
480                    DO nb = 1, nat
481                      nu = 3 * (nb - 1)
482                      IF (noncolin) THEN
483                        IF (lspinorb) THEN
484                          ijs = 0
485                          DO is = 1, npol
486                            DO js = 1, npol
487                              ijs = ijs + 1
488                              ps1_nc(ikb, is, ibnd) = ps1_nc(ikb, is, ibnd) + int2_so(ih, jh, ipol, nb, na, ijs) * &
489                                 becp1(ik)%nc(jkb, js, ibnd + ibndstart - 1) * uact(nu + ipol)
490                            ENDDO
491                          ENDDO
492                        ELSE
493                          DO is = 1, npol
494                            ps1_nc(ikb, is, ibnd) = ps1_nc(ikb, is, ibnd) + int2(ih, jh, ipol, nb, na) * &
495                               becp1(ik)%nc(jkb, is, ibnd + ibndstart - 1) * uact(nu + ipol)
496                          ENDDO
497                        ENDIF
498                      ELSE
499                        ps1(ikb,ibnd) = ps1(ikb,ibnd) + int2(ih, jh, ipol, nb, na) * &
500                            becp1(ik)%k(jkb, ibnd + ibndstart - 1) * uact(nu + ipol)
501                      ENDIF
502                    ENDDO
503                  ENDIF  ! okvan
504                ENDDO ! ipol
505              ENDDO ! jh
506            ENDDO ! ih
507            ijkb0 = ijkb0 + nh(nt)
508          ENDIF
509        ENDDO  ! na
510      ENDDO ! nt
511    ENDDO ! nbnd
512    !
513    !      This term is proportional to beta(k+q+G)
514    !
515    IF (nkb > 0) THEN
516      IF (noncolin) THEN
517        CALL ZGEMM('n', 'n', npwq, (upper_band - lower_band + 1)*npol, nkb, &
518                   cone, vkb, npwx, ps1_nc, nkb, cone, dvpsi, npwx)
519      ELSE
520        CALL ZGEMM('n', 'n', npwq, (upper_band - lower_band + 1), nkb, &
521                   cone, vkb, npwx, ps1, nkb, cone, dvpsi, npwx)
522      ENDIF
523    ENDIF
524    !
525    !      This term is proportional to (k+q+G)_\alpha*beta(k+q+G)
526    !
527    DO ikb = 1, nkb
528      DO ipol = 1, 3
529        ok = .FALSE.
530        IF (noncolin) THEN
531          DO ibnd = lower_band, upper_band
532            ok = ok .OR. (ABS(ps2_nc(ikb, 1, ibnd, ipol) ) > eps12) .OR. &
533                         (ABS(ps2_nc(ikb, 2, ibnd, ipol) ) > eps12)
534          ENDDO
535        ELSE
536          DO ibnd = lower_band, upper_band
537            ok = ok .OR. (ABS(ps2(ikb, ibnd, ipol)) > eps12)
538          ENDDO
539        ENDIF
540        IF (ok) THEN
541          DO ig = 1, npwq
542            igg = igkq(ig)
543            aux(ig) = vkb(ig, ikb) * (xxkq(ipol) + g(ipol, igg))
544          ENDDO
545          DO ibnd = lower_band, upper_band
546            IF (noncolin) THEN
547              CALL ZAXPY(npwq, ps2_nc(ikb, 1, ibnd, ipol), aux, 1, dvpsi(1, ibnd), 1)
548              CALL ZAXPY(npwq, ps2_nc(ikb, 2, ibnd, ipol), aux, 1, dvpsi(1 + npwx, ibnd), 1)
549            ELSE
550              CALL ZAXPY(npwq, ps2(ikb, ibnd, ipol), aux, 1, dvpsi(1, ibnd), 1)
551            ENDIF
552          ENDDO
553        ENDIF
554      ENDDO
555    ENDDO
556    !
557    DEALLOCATE(aux, STAT = ierr)
558    IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating aux', 1)
559    IF (noncolin) THEN
560      DEALLOCATE(ps1_nc, STAT = ierr)
561      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating ps1_nc', 1)
562      DEALLOCATE(ps2_nc, STAT = ierr)
563      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating ps2_nc', 1)
564      DEALLOCATE(deff_nc, STAT = ierr)
565      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating deff_nc', 1)
566    ELSE
567      DEALLOCATE(ps1, STAT = ierr)
568      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating ps1', 1)
569      DEALLOCATE(ps2, STAT = ierr)
570      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating ps2', 1)
571      DEALLOCATE(deff, STAT = ierr)
572      IF (ierr /= 0) CALL errore('dvqpsi_us_only3', 'Error deallocating deff', 1)
573    ENDIF
574    !
575    CALL stop_clock('dvqpsi_us_on')
576    !
577    RETURN
578    !
579    !----------------------------------------------------------------------
580    END SUBROUTINE dvqpsi_us_only3
581    !----------------------------------------------------------------------
582    !
583    !----------------------------------------------------------------------
584    SUBROUTINE dvanqq2()
585    !----------------------------------------------------------------------
586    !!
587    !! This routine calculates two integrals of the Q functions and
588    !! its derivatives with V_loc and V_eff which are used
589    !! to compute term dV_bare/dtau * psi  in addusdvqpsi.
590    !! The result is stored in int1, int2. The routine is called
591    !! for each q in nqc.
592    !! int1 -> Eq. B20 of Ref.[1]
593    !! int2 -> Eq. B21 of Ref.[1]
594    !!
595    !! [1] PRB 64, 235118 (2001).
596    !!
597    !! RM - Nov/Dec 2014
598    !! Imported the noncolinear case implemented by xlzhang
599    !!
600    !! Roxana Margine - Dec 2018: Updated based on QE 6.3
601    !! SP: Sept. 2019 - Cleaning
602    !!
603    !! HL: Mar 2020 - Parallelization over G using intra image communicator
604    !!
605    !
606    USE kinds,            ONLY : DP
607    USE ions_base,        ONLY : nat, ityp, ntyp => nsp
608    USE spin_orb,         ONLY : lspinorb
609    USE cell_base,        ONLY : tpiba2, omega, tpiba
610    USE gvect,            ONLY : ngm, gg, g, eigts1, eigts2, eigts3, mill
611    USE scf,              ONLY : v, vltot
612    USE noncollin_module, ONLY : noncolin, nspin_mag
613    USE phcom,            ONLY : int1, int2, vlocq
614    USE qpoint,           ONLY : xq, eigqts
615    USE uspp_param,       ONLY : upf, lmaxq, nh
616    USE uspp,             ONLY : okvan, ijtoh
617    USE mp_global,        ONLY : intra_pool_comm
618    USE mp,               ONLY : mp_sum
619    USE fft_base,         ONLY : dfftp
620    USE fft_interfaces,   ONLY : fwfft
621    USE constants_epw,    ONLY : zero, czero
622    USE mp_images,        ONLY : intra_image_comm
623    USE elph2,            ONLY : veff, ig_s, ig_e
624    !
625    IMPLICIT NONE
626    !
627    ! Local variables
628    INTEGER :: na
629    !! counter on atoms
630    INTEGER :: nb
631    !! counter on atoms
632    INTEGER :: ntb
633    !! counter on atomic types (species)
634    INTEGER :: nta
635    !! index of atomic type (specie)
636    INTEGER :: ig
637    !! counter on G vectors
638    INTEGER :: ir
639    !! counter on FFT mesh points
640    INTEGER :: ih
641    !! counter on beta functions per atomic type
642    INTEGER :: jh
643    !! counter on beta functions per atomic type
644    INTEGER :: ijh
645    !! correspondence beta indexes ih,jh -> composite index ijh
646    INTEGER :: ipol
647    !! counter on polarizations
648    INTEGER :: jpol
649    !! counter on polarizations
650    INTEGER :: is
651    !! counter on spin
652    INTEGER :: ierr
653    !! Error status
654    INTEGER :: ngvec
655    !! number of G in this group
656    REAL(KIND = DP), ALLOCATABLE :: qmod(:)
657    !! the modulus of q+G
658    REAL(KIND = DP), ALLOCATABLE :: qmodg(:)
659    !! the modulus of G
660    REAL(KIND = DP), ALLOCATABLE :: qpg(:, :)
661    !! the q+G vectors
662    REAL(KIND = DP), ALLOCATABLE :: ylmkq(:, :)
663    !! the spherical harmonics at q+G
664    REAL(KIND = DP), ALLOCATABLE ::  ylmk0(:, :)
665    !! the spherical harmonics at G
666    COMPLEX(KIND = DP) :: fact
667    !! e^{-i q * \tau} * CONJG(e^{-i q * \tau})
668    COMPLEX(KIND = DP) :: fact1
669    !! -i * omega
670    COMPLEX(KIND = DP), EXTERNAL :: ZDOTC
671    !! the scalar product function
672    COMPLEX(KIND = DP), ALLOCATABLE :: aux1(:), aux2(:), aux5(:)
673    !! Auxiallary array
674    COMPLEX(KIND = DP), ALLOCATABLE :: sk(:)
675    !!
676    COMPLEX(KIND = DP), ALLOCATABLE, TARGET :: qgm(:)
677    !! the augmentation function at G
678    COMPLEX(KIND = DP), POINTER :: qgmq(:)
679    !! the augmentation function at q+G
680    !
681    IF (.NOT. okvan) RETURN
682    !
683    CALL start_clock('dvanqq2')
684    !
685    ngvec = ig_e - ig_s + 1
686    !
687    int1(:, :, :, :, :) = czero
688    int2(:, :, :, :, :) = czero
689    !
690    ALLOCATE(sk(ngvec), STAT = ierr)
691    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating sk', 1)
692    ALLOCATE(aux1(ngvec), STAT = ierr)
693    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating aux1', 1)
694    ALLOCATE(aux2(ngvec), STAT = ierr)
695    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating aux2', 1)
696    ALLOCATE(aux5(ngvec), STAT = ierr)
697    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating aux5', 1)
698    ALLOCATE(qmodg(ngvec), STAT = ierr)
699    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating qmodg', 1)
700    ALLOCATE(qmod(ngvec), STAT = ierr)
701    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating qmod', 1)
702    ALLOCATE(qgmq(ngvec), STAT = ierr)
703    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating qgmq', 1)
704    ALLOCATE(qgm(ngvec), STAT = ierr)
705    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating qgm', 1)
706    ALLOCATE(ylmk0(ngvec, lmaxq * lmaxq), STAT = ierr)
707    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating ylmk0', 1)
708    ALLOCATE(ylmkq(ngvec, lmaxq * lmaxq), STAT = ierr)
709    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating ylmkq', 1)
710    sk(:) = czero
711    aux1(:) = czero
712    aux2(:) = czero
713    aux5(:) = czero
714    qmodg(:) = zero
715    qmod(:) = zero
716    qgmq(:) = czero
717    qgm(:) = czero
718    ylmk0(:, :) = zero
719    ylmkq(:, :) = zero
720    !
721    ! compute spherical harmonics
722    !
723    CALL ylmr2(lmaxq * lmaxq, ngvec, g(:, ig_s), gg(ig_s), ylmk0)
724    !
725    DO ig = 1, ngvec
726      qmodg(ig) = DSQRT(gg(ig + ig_s - 1))*tpiba
727    ENDDO
728    !
729    ALLOCATE(qpg(3, ngvec), STAT = ierr)
730    IF (ierr /= 0) CALL errore('dvanqq2', 'Error allocating qpg', 1)
731    qpg(:, :) = zero
732    !
733    CALL setqmod(ngvec, xq, g(:, ig_s), qmod, qpg)
734    CALL ylmr2(lmaxq * lmaxq, ngvec, qpg, qmod, ylmkq)
735    !
736    DEALLOCATE(qpg, STAT = ierr)
737    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating qpg', 1)
738    DO ig = 1, ngvec
739      qmod(ig) = DSQRT(qmod(ig))*tpiba
740    ENDDO
741    !
742    ! We compute here two of the three integrals needed in the phonon
743    !
744    fact1 = CMPLX(0.d0, - tpiba * omega, KIND = DP)
745    !
746    DO ntb = 1, ntyp
747      IF (upf(ntb)%tvanp) THEN
748        !
749        DO ih = 1, nh(ntb)
750          DO jh = ih, nh(ntb)
751            ijh = ijtoh(ih, jh, ntb)
752            !
753            ! Compute the augmentation function
754            !
755            CALL qvan2(ngvec, ih, jh, ntb, qmodg, qgm, ylmk0)
756            CALL qvan2(ngvec, ih, jh, ntb, qmod, qgmq, ylmkq)
757            !
758            ! NB: for this integral the moving atom and the atom of Q
759            ! do not necessarily coincide
760            !
761            DO nb = 1, nat
762              IF (ityp(nb) == ntb) THEN
763                DO ig = 1, ngvec
764                  aux1(ig) = qgmq(ig) * eigts1(mill(1, ig + ig_s - 1), nb) &
765                                      * eigts2(mill(2, ig + ig_s - 1), nb) &
766                                      * eigts3(mill(3, ig + ig_s - 1), nb)
767                ENDDO
768                !
769                DO na = 1, nat
770                  fact = eigqts(na) * CONJG(eigqts(nb))
771                  !
772                  !    nb is the atom of the augmentation function
773                  !
774                  nta = ityp(na)
775                  DO ig = 1, ngvec
776                    sk(ig) = vlocq(ig + ig_s - 1, nta) &
777                             * eigts1(mill(1, ig + ig_s - 1), na) &
778                             * eigts2(mill(2, ig + ig_s - 1), na) &
779                             * eigts3(mill(3, ig + ig_s - 1), na)
780                  ENDDO
781                  !
782                  DO ipol = 1, 3
783                    DO ig = 1, ngvec
784                      aux5(ig) = sk(ig) * (g(ipol, ig + ig_s - 1) + xq(ipol))
785                    ENDDO
786                    int2(ih, jh, ipol, na, nb) = fact * fact1 * ZDOTC(ngvec, aux1, 1, aux5, 1)
787                    !
788                  ENDDO !ipol
789                  !
790                ENDDO !na
791                !
792                DO ig = 1, ngvec
793                  aux1(ig) = qgm(ig) * eigts1(mill(1,ig + ig_s - 1),nb) &
794                                     * eigts2(mill(2,ig + ig_s - 1),nb) &
795                                     * eigts3(mill(3,ig + ig_s - 1),nb)
796                ENDDO
797                !
798                DO is = 1, nspin_mag
799                  DO ipol = 1, 3
800                    DO ig = 1, ngvec
801                      aux2(ig) = veff(dfftp%nl(ig + ig_s - 1), is) * g(ipol, ig + ig_s - 1)
802                    ENDDO
803                    int1(ih, jh, ipol, nb, is) = - fact1 * ZDOTC(ngvec, aux1, 1, aux2, 1)
804                  ENDDO ! ipol
805                ENDDO ! is
806              ENDIF ! ityp
807            ENDDO ! nb
808          ENDDO ! jh
809        ENDDO ! ih
810        !
811        DO ih = 1, nh(ntb)
812          DO jh = ih + 1, nh(ntb)
813            !
814            !    We use the symmetry properties of the integral factor
815            !
816            DO nb = 1, nat
817              IF (ityp(nb) == ntb) THEN
818                DO ipol = 1, 3
819                  DO is = 1, nspin_mag
820                    int1(jh, ih, ipol, nb, is) = int1(ih, jh, ipol, nb, is)
821                  ENDDO
822                  DO na = 1, nat
823                    int2(jh, ih, ipol, na, nb) = int2(ih, jh, ipol, na, nb)
824                  ENDDO ! na
825                ENDDO ! ipol
826              ENDIF
827            ENDDO ! nb
828          ENDDO ! jh
829        ENDDO ! ih
830      ENDIF ! upf
831    ENDDO ! ntb
832    CALL mp_sum(int1, intra_image_comm)
833    CALL mp_sum(int2, intra_image_comm)
834    !
835    IF (noncolin) THEN
836      CALL set_int12_nc(0)
837    ENDIF
838    !
839    DEALLOCATE(sk, STAT = ierr)
840    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating sk', 1)
841    DEALLOCATE(aux1, STAT = ierr)
842    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating aux1', 1)
843    DEALLOCATE(aux2, STAT = ierr)
844    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating aux2', 1)
845    DEALLOCATE(aux5, STAT = ierr)
846    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating aux5', 1)
847    DEALLOCATE(qmodg, STAT = ierr)
848    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating qmodg', 1)
849    DEALLOCATE(qmod, STAT = ierr)
850    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating qmod', 1)
851    DEALLOCATE(qgmq, STAT = ierr)
852    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating qgmq', 1)
853    DEALLOCATE(qgm, STAT = ierr)
854    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating qgm', 1)
855    DEALLOCATE(ylmk0, STAT = ierr)
856    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating ylmk0', 1)
857    DEALLOCATE(ylmkq, STAT = ierr)
858    IF (ierr /= 0) CALL errore('dvanqq2', 'Error deallocating ylmkq', 1)
859    !
860    CALL stop_clock('dvanqq2')
861    RETURN
862    !
863    !----------------------------------------------------------------------
864    END SUBROUTINE dvanqq2
865    !----------------------------------------------------------------------
866    !
867    !----------------------------------------------------------------------
868    SUBROUTINE newdq2(dvscf, npe, xq0, timerev)
869    !----------------------------------------------------------------------
870    !!
871    !! This routine computes the contribution of the selfconsistent
872    !! change of the potential to the known part of the linear
873    !! system and adds it to dvpsi.
874    !! Adapted from LR_Modules/newdq.f90 (QE)
875    !!
876    !! Roxana Margine - Jan 2019: Updated based on QE 6.3
877    !!
878    !! HL: Mar 2020
879    !! 1. Parallelization over G using intra image communicator
880    !! 2. PAW added and cleaning
881    !!
882    USE kinds,                ONLY : DP
883    USE ions_base,            ONLY : nat, ityp, ntyp => nsp
884    USE noncollin_module,     ONLY : noncolin, nspin_mag
885    USE cell_base,            ONLY : omega, tpiba
886    USE fft_base,             ONLY : dfftp
887    USE fft_interfaces,       ONLY : fwfft
888    USE gvect,                ONLY : g, ngm, mill, eigts1, eigts2, eigts3
889    USE uspp,                 ONLY : okvan
890    USE uspp_param,           ONLY : upf, lmaxq, nh
891    USE paw_variables,        ONLY : okpaw
892    USE mp_global,            ONLY : intra_pool_comm
893    USE mp,                   ONLY : mp_sum
894    USE lrus,                 ONLY : int3, int3_paw
895    USE qpoint,               ONLY : eigqts
896    USE constants_epw,        ONLY : czero, zero
897    USE mp_images,            ONLY : intra_image_comm
898    USE elph2,                ONLY : ig_s, ig_e
899    !
900    IMPLICIT NONE
901    !
902    LOGICAL, INTENT(in) :: timerev
903    !!  true if we are using time reversal
904    INTEGER, INTENT(in) :: npe
905    !! Number of perturbations for this irr representation
906    REAL(KIND = DP), INTENT(in) :: xq0(3)
907    !! The first q-point in the star (cartesian coords.)
908    COMPLEX(KIND = DP), INTENT(in) :: dvscf(dfftp%nnr, nspin_mag, npe)
909    !! Change of the selfconsistent potential
910    !
911    ! Local variables
912    INTEGER :: na
913    !! counter on atoms
914    INTEGER :: ig
915    !! counter on G vectors
916    INTEGER :: nt
917    !! counter on atomic types
918    INTEGER ::  ir
919    !! counter on real mesh
920    INTEGER :: ipert
921    !! counter on change of Vscf due to perturbations
922    INTEGER :: is
923    !! counter on spin
924    INTEGER :: ih
925    !! Counter on beta functions
926    INTEGER :: jh
927    !! Counter on beta functions
928    INTEGER :: ierr
929    !! Error status
930    INTEGER :: ngvec
931    !! number of G in this group
932    REAL(KIND = DP), ALLOCATABLE :: qmod(:)
933    !! the modulus of q+G
934    REAL(KIND = DP), ALLOCATABLE :: qg(:, :)
935    !! the values of q+G
936    REAL(KIND = DP), ALLOCATABLE :: ylmk0(:, :)
937    !! the spherical harmonics at q+G
938    COMPLEX(KIND = DP), EXTERNAL :: ZDOTC
939    !! the scalar product function
940    COMPLEX(KIND = DP), ALLOCATABLE :: aux1(:), aux2(:, :)
941    !! Auxillary variable
942    COMPLEX(KIND = DP), ALLOCATABLE :: qgm(:)
943    !! the augmentation function at q+G
944    COMPLEX(KIND = DP), ALLOCATABLE :: veff(:)
945    !! effective potential
946    !
947    IF (.NOT. okvan) RETURN
948    !
949    CALL start_clock('newdq2')
950    !
951    ngvec = ig_e - ig_s + 1
952    !
953    int3(:, :, :, :, :) = czero
954    ALLOCATE(aux1(ngvec), STAT = ierr)
955    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating aux1', 1)
956    ALLOCATE(aux2(ngvec, nspin_mag), STAT = ierr)
957    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating aux2', 1)
958    ALLOCATE(veff(dfftp%nnr), STAT = ierr)
959    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating veff', 1)
960    ALLOCATE(ylmk0(ngvec, lmaxq * lmaxq), STAT = ierr)
961    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating ylmk0', 1)
962    ALLOCATE(qgm(ngvec), STAT = ierr)
963    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating qgm', 1)
964    ALLOCATE(qmod(ngvec), STAT = ierr)
965    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating qmod', 1)
966    ALLOCATE(qg(3, ngvec), STAT = ierr)
967    IF (ierr /= 0) CALL errore('newdq2', 'Error allocating qg', 1)
968    aux1(:)     = czero
969    aux2(:, :)  = czero
970    veff(:)     = czero
971    ylmk0(:, :) = zero
972    qgm(:)      = czero
973    qmod(:)     = zero
974    qg(:, :)    = zero
975    !
976    ! first compute the spherical harmonics
977    !
978    CALL setqmod(ngvec, xq0, g(:, ig_s), qmod, qg)
979    CALL ylmr2(lmaxq * lmaxq, ngvec, qg, qmod, ylmk0)
980    !
981    DO ig = 1, ngvec
982      qmod(ig) = DSQRT(qmod(ig))*tpiba
983    ENDDO
984    !
985    ! and for each perturbation of this irreducible representation
986    ! integrate the change of the self consistent potential and
987    ! the Q functions
988    !
989    DO ipert = 1, npe
990      DO is = 1, nspin_mag
991        DO ir = 1, dfftp%nnr
992          IF (timerev) THEN
993            veff(ir) = CONJG(dvscf(ir, is, ipert))
994          ELSE
995            veff(ir) = dvscf(ir, is, ipert)
996          ENDIF
997        ENDDO
998        CALL fwfft('Rho', veff, dfftp)
999        DO ig = 1, ngvec
1000          aux2(ig, is) = veff(dfftp%nl(ig + ig_s - 1))
1001        ENDDO
1002      ENDDO
1003      !
1004      DO nt = 1, ntyp
1005        IF (upf(nt)%tvanp) THEN
1006          !
1007          DO ih = 1, nh(nt)
1008            DO jh = ih, nh(nt)
1009              !
1010              CALL qvan2(ngvec, ih, jh, nt, qmod, qgm, ylmk0)
1011              !
1012              DO na = 1, nat
1013                IF (ityp(na) == nt) THEN
1014                  DO ig = 1, ngvec
1015                    aux1(ig) = qgm(ig) * eigts1(mill(1, ig + ig_s - 1), na) * &
1016                                         eigts2(mill(2, ig + ig_s - 1), na) * &
1017                                         eigts3(mill(3, ig + ig_s - 1), na) * &
1018                                         eigqts(na)
1019                  ENDDO
1020                  DO is = 1, nspin_mag
1021                    int3(ih, jh, na, is, ipert) = omega * ZDOTC(ngvec, aux1, 1, aux2(1, is), 1)
1022                  ENDDO
1023                ENDIF
1024              ENDDO
1025            ENDDO ! jh
1026          ENDDO ! ih
1027          !
1028          DO na = 1, nat
1029            IF (ityp(na) == nt) THEN
1030              !
1031              ! We use the symmetry properties of the ps factor
1032              !
1033              DO ih = 1, nh(nt)
1034                DO jh = ih, nh(nt)
1035                  DO is = 1, nspin_mag
1036                    int3(jh, ih, na, is, ipert) = int3(ih, jh, na, is, ipert)
1037                  ENDDO
1038                ENDDO
1039              ENDDO
1040            ENDIF ! ityp
1041          ENDDO ! na
1042        ENDIF ! upf
1043      ENDDO ! nt
1044    ENDDO ! ipert
1045    !
1046    CALL mp_sum(int3, intra_image_comm)
1047    !
1048    ! Sum of the USPP and PAW terms
1049    ! (see last two terms in Eq.(12) in PRB 81, 075123 (2010))
1050    !
1051    IF (okpaw) int3 = int3 + int3_paw
1052    !
1053    IF (noncolin) CALL set_int3_nc(npe)
1054    !
1055    DEALLOCATE(aux1, STAT = ierr)
1056    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating aux1', 1)
1057    DEALLOCATE(aux2, STAT = ierr)
1058    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating aux2', 1)
1059    DEALLOCATE(veff, STAT = ierr)
1060    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating veff', 1)
1061    DEALLOCATE(ylmk0, STAT = ierr)
1062    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating ylmk0', 1)
1063    DEALLOCATE(qgm, STAT = ierr)
1064    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating qgm', 1)
1065    DEALLOCATE(qmod, STAT = ierr)
1066    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating qmod', 1)
1067    DEALLOCATE(qg, STAT = ierr)
1068    IF (ierr /= 0) CALL errore('newdq2', 'Error deallocating qg', 1)
1069    !
1070    CALL stop_clock('newdq2')
1071    !
1072    RETURN
1073    !
1074    !---------------------------------------------------------------------------
1075    END SUBROUTINE newdq2
1076    !---------------------------------------------------------------------------
1077    !
1078    !----------------------------------------------------------------------
1079    SUBROUTINE adddvscf2(ipert, ik)
1080    !----------------------------------------------------------------------
1081    !!
1082    !! This routine computes the contribution of the selfconsistent
1083    !! change of the potential to the known part of the linear
1084    !! system and adds it to dvpsi.
1085    !! It implements the second term in Eq. B30 of PRB 64, 235118 (2001).
1086    !! Only used in the case of USPP.
1087    !! Adapted from LR_Modules/adddvscf.f90 (QE)
1088    !!
1089    !! Roxana Margine - Jan 2019: Updated based on QE 6.3
1090    !! SP - Jan 2019: Clean
1091    !!
1092    USE kinds,      ONLY : DP
1093    USE uspp_param, ONLY : upf, nh
1094    USE uspp,       ONLY : vkb, okvan
1095    USE lsda_mod,   ONLY : lsda, current_spin
1096    USE klist_epw,  ONLY : isk_loc
1097    USE ions_base,  ONLY : ntyp => nsp, nat, ityp
1098    USE wvfct,      ONLY : npwx
1099    USE lrus,       ONLY : int3, int3_nc, becp1
1100    USE qpoint,     ONLY : npwq
1101    USE eqv,        ONLY : dvpsi
1102    USE elph2,      ONLY : lower_band, upper_band, ibndstart
1103    USE noncollin_module, ONLY : noncolin, npol
1104    USE constants_epw,    ONLY : czero
1105    !
1106    IMPLICIT NONE
1107    !
1108    INTEGER, INTENT(in) :: ik
1109    !! Counter on k-point
1110    INTEGER, INTENT(in) :: ipert
1111    !! Counter on Vscf perturbations
1112    !
1113    !   Local variables
1114    !
1115    INTEGER :: na
1116    !! Counter on atoms
1117    INTEGER :: nt
1118    !! Counter on atomic types
1119    INTEGER :: ibnd
1120    !! Counter on bands
1121    INTEGER :: ih
1122    !! Counter on beta functions
1123    INTEGER :: jh
1124    !! Counter on beta functions
1125    INTEGER :: ijkb0
1126    !! Auxiliary variable for counting
1127    INTEGER :: ikb
1128    !! Counter on becp functions
1129    INTEGER :: jkb
1130    !! Counter on becp functions
1131    INTEGER :: is
1132    !! Counter on polarization
1133    INTEGER :: js
1134    !! Counter on polarization
1135    INTEGER ::  ijs
1136    !! Counter on combined is and js polarization
1137    !
1138    COMPLEX(KIND = DP) :: sum_k
1139    !! auxiliary sum variable
1140    COMPLEX(KIND = DP) :: sum_nc(npol)
1141    !! auxiliary sum variable non-collinear case
1142    !
1143    IF (.NOT. okvan) RETURN
1144    !
1145    CALL start_clock('adddvscf2')
1146    !
1147    IF (lsda) current_spin = isk_loc(ik)
1148    !
1149    ijkb0 = 0
1150    DO nt = 1, ntyp
1151      IF (upf(nt)%tvanp) THEN
1152        DO na = 1, nat
1153          IF (ityp(na) == nt) THEN
1154            !
1155            ! We multiply the integral for the becp term and the beta_n
1156            !
1157            DO ibnd = lower_band, upper_band
1158              DO ih = 1, nh(nt)
1159                 ikb = ijkb0 + ih
1160                 IF (noncolin) THEN
1161                   sum_nc = czero
1162                 ELSE
1163                   sum_k = czero
1164                 ENDIF
1165                 DO jh = 1, nh(nt)
1166                   jkb = ijkb0 + jh
1167                   IF (noncolin) THEN
1168                     ijs = 0
1169                     DO is = 1, npol
1170                       DO js = 1, npol
1171                         ijs = ijs + 1
1172                         sum_nc(is) = sum_nc(is) + int3_nc(ih, jh, na, ijs, ipert) * becp1(ik)%nc(jkb, js, ibnd + ibndstart - 1)
1173                       ENDDO
1174                     ENDDO
1175                   ELSE
1176                     sum_k = sum_k + int3(ih,jh,na,current_spin,ipert) * &
1177                                 becp1(ik)%k(jkb,ibnd + ibndstart - 1)
1178                   ENDIF
1179                 ENDDO
1180                 IF (noncolin) THEN
1181                   CALL ZAXPY(npwq, sum_nc(1), vkb(1, ikb), 1, dvpsi(1, ibnd), 1)
1182                   CALL ZAXPY(npwq, sum_nc(2), vkb(1, ikb), 1, dvpsi(1 + npwx, ibnd), 1)
1183                 ELSE
1184                   CALL ZAXPY(npwq, sum_k, vkb(1, ikb), 1, dvpsi(1, ibnd), 1)
1185                 ENDIF
1186              ENDDO
1187            ENDDO
1188            ijkb0 = ijkb0 + nh(nt)
1189          ENDIF
1190        ENDDO
1191      ELSE
1192        DO na = 1, nat
1193          IF (ityp(na) == nt) ijkb0 = ijkb0 + nh(nt)
1194        ENDDO
1195      ENDIF
1196    ENDDO
1197    !
1198    CALL stop_clock('adddvscf2')
1199    !
1200    RETURN
1201    !
1202    !---------------------------------------------------------------------------
1203    END SUBROUTINE adddvscf2
1204    !---------------------------------------------------------------------------
1205  !-----------------------------------------------------------------------------
1206  END MODULE dvqpsi
1207  !-----------------------------------------------------------------------------
1208
1209