dfpt.f90 20.6 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
!
2
! Copyright (C) 2015-2017 M. Govoni 
Marco Govoni's avatar
Marco Govoni committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
! This file is distributed under the terms of the
! GNU General Public License. See the file `License'
! in the root directory of the present distribution,
! or http://www.gnu.org/copyleft/gpl.txt .
!
! This file is part of WEST.
!
! Contributors to this file: 
! Marco Govoni
!
!-----------------------------------------------------------------------
SUBROUTINE dfpt (m,dvg,dng,tr2)
  !-----------------------------------------------------------------------
  !
  USE kinds,                 ONLY : DP
  USE io_global,             ONLY : stdout
  USE wvfct,                 ONLY : nbnd,g2kin,et
  USE fft_base,              ONLY : dfftp,dffts
Govoni's avatar
Govoni committed
21
  USE gvect,                 ONLY : nl,gstart,ig_l2g,g,ngm
Marco Govoni's avatar
Marco Govoni committed
22
23
24
25
26
27
28
29
30
31
  USE wavefunctions_module,  ONLY : evc,psic
  USE gvecs,                 ONLY : ngms
  USE gvecw,                 ONLY : gcutw
  USE mp,                    ONLY : mp_sum,mp_barrier,mp_bcast
  USE mp_global,             ONLY : inter_image_comm,inter_pool_comm,my_image_id
  USE fft_at_gamma,          ONLY : single_fwfft_gamma,single_invfft_gamma,double_fwfft_gamma,double_invfft_gamma
  USE fft_at_k,              ONLY : single_fwfft_k,single_invfft_k
  USE buffers,               ONLY : get_buffer
  USE noncollin_module,      ONLY : noncolin,npol
  USE bar,                   ONLY : bar_type,start_bar_type,update_bar_type,stop_bar_type
Govoni's avatar
Govoni committed
32
  USE pwcom,                 ONLY : current_spin,wk,nks,nelup,neldw,isk,igk_k,xk,npw,npwx,lsda,nkstot,&
Marco Govoni's avatar
Marco Govoni committed
33
                                  & current_k,ngk
Govoni's avatar
Govoni committed
34
  USE cell_base,             ONLY : tpiba2,omega
Marco Govoni's avatar
Marco Govoni committed
35
36
37
38
  USE control_flags,         ONLY : gamma_only, io_level
  USE io_files,              ONLY : tmp_dir, nwordwfc, iunwfc, diropn
  USE uspp,                  ONLY : nkb, vkb, okvan
  USE constants,             ONLY : e2,fpi
39
  USE westcom,               ONLY : npwq,sqvc,nbnd_occ,iuwfc,lrwfc,npwqx,fftdriver
Marco Govoni's avatar
Marco Govoni committed
40
41
42
43
44
45
46
47
  USE io_push,               ONLY : io_push_title
  USE mp_world,              ONLY : mpime,world_comm
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  INTEGER,INTENT(IN) :: m
48
49
  COMPLEX(DP),INTENT(IN) :: dvg(npwqx,m)
  COMPLEX(DP),INTENT(OUT) :: dng(npwqx,m)
Marco Govoni's avatar
Marco Govoni committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  REAL(DP),INTENT(IN) :: tr2
  !
  ! Workspace
  !
  INTEGER :: ipert, ig, ir, ibnd, iks
  INTEGER :: nbndval, ierr
  !
  REAL(DP) :: anorm, prod 
  REAL(DP),ALLOCATABLE :: eprec(:)
  !
  COMPLEX(DP),ALLOCATABLE :: dvpsi(:,:),dpsi(:,:)
  COMPLEX(DP),ALLOCATABLE :: aux_r(:),aux_g(:)
  COMPLEX(DP),ALLOCATABLE :: dpsic(:)
  !
  TYPE(bar_type) :: barra
  !
  LOGICAL :: conv_dfpt
  LOGICAL :: exst,exst_mem
  LOGICAL :: l_dost
  !
70
  CHARACTER(LEN=512) :: title
Marco Govoni's avatar
Marco Govoni committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  !
  CALL mp_barrier( world_comm )
  !
  CALL report_dynamical_memory()
  !
  l_dost = ( tr2 >= 0._DP ) 
  !
  IF( l_dost ) THEN 
     WRITE(title,'(a,es14.6)') "Sternheimer eq. solver... with threshold = ", tr2 
  ELSE
     WRITE(title,'(a,es14.6)') "Sternheimer eq. solver... with lite-solver"
  ENDIF
  CALL io_push_title(TRIM(ADJUSTL(title)))
  !
  dng=0.0_DP
  !
  CALL start_bar_type( barra, 'dfpt', MAX(m,1) * nks ) 
  !IF(nks>1) CALL diropn(iuwfc,'wfc',lrwfc,exst) 
  !
  DO iks = 1, nks  ! KPOINT-SPIN LOOP
     !
     ! ... Set k-point, spin, kinetic energy, needed by Hpsi
     !
     current_k = iks
     IF ( lsda ) current_spin = isk(iks)
     call g2_kin( iks )
     !
     ! ... More stuff needed by the hamiltonian: nonlocal projectors
     !
     IF ( nkb > 0 ) CALL init_us_2( ngk(iks), igk_k(1,iks), xk(1,iks), vkb )
     !npw = ngk(iks)
     !
     ! ... read in wavefunctions from the previous iteration
     !
     IF(nks>1) THEN
        !iuwfc = 20
        !lrwfc = nbnd * npwx * npol 
        !!CALL get_buffer( evc, nwordwfc, iunwfc, iks )
        IF(my_image_id==0) CALL get_buffer( evc, lrwfc, iuwfc, iks )
        !CALL mp_bcast(evc,0,inter_image_comm)
        !CALL davcio(evc,lrwfc,iuwfc,iks,-1)
        CALL mp_bcast(evc,0,inter_image_comm)
     ENDIF
!     !
!     ! ... Needed for LDA+U
!     !
!     IF ( nks > 1 .AND. lda_plus_u .AND. (U_projection .NE. 'pseudo') ) &
!          CALL get_buffer ( wfcU, nwordwfcU, iunhub, iks )
!     !
!     current_k = iks
!     current_spin = isk(iks)
!     !
!     CALL gk_sort(xk(1,iks),ngm,g,gcutw,npw,igk,g2kin)
!     g2kin=g2kin*tpiba2
!     !
!     ! reads unperturbed wavefuctions psi_k in G_space, for all bands
!     !
!     !
!     CALL init_us_2 (npw, igk, xk (1, iks), vkb)
     !
     nbndval = nbnd_occ(iks)
     IF(nbndval==0) THEN
        CALL update_bar_type( barra,'dfpt', MAX(1,m) )
        CYCLE
     ENDIF
     !
     ALLOCATE(eprec(nbndval))
     CALL set_eprec(nbndval,evc(1,1),eprec)
     !
     ALLOCATE(dvpsi(npwx*npol,nbndval))   
     ALLOCATE(dpsi(npwx*npol,nbndval))
     !
     DO ipert = 1, m
        !
145
        ALLOCATE(aux_g(npwqx))
Marco Govoni's avatar
Marco Govoni committed
146
147
148
149
150
        ALLOCATE(aux_r(dffts%nnr))
        !
        aux_g = 0._DP
        aux_r = 0._DP
        !
151
        DO ig = 1, npwq  ! perturbation acts only on body
Marco Govoni's avatar
Marco Govoni committed
152
153
154
155
           aux_g(ig) = dvg(ig,ipert) * sqvc(ig) 
        ENDDO
        !
        IF(gamma_only) THEN
156
          CALL single_invfft_gamma(dffts,npwq,npwqx,aux_g,aux_r,TRIM(fftdriver))
Marco Govoni's avatar
Marco Govoni committed
157
        ELSE
158
          CALL single_invfft_k(dffts,npwq,npwqx,aux_g,aux_r,TRIM(fftdriver)) ! no igk
Marco Govoni's avatar
Marco Govoni committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
        ENDIF
        !
        ! The perturbation is in aux_r
        !
        dvpsi=0._DP
        dpsi =0._DP
        !
        IF(gamma_only) THEN
           !
           ! double bands @ gamma
           DO ibnd=1,nbndval-MOD(nbndval,2),2
              !
              CALL double_invfft_gamma(dffts,npw,npwx,evc(1,ibnd),evc(1,ibnd+1),psic,'Wave')
              DO ir=1,dffts%nnr
                 psic(ir) = psic(ir) * REAL(aux_r(ir),KIND=DP)
              ENDDO
              CALL double_fwfft_gamma(dffts,npw,npwx,psic,dvpsi(1,ibnd),dvpsi(1,ibnd+1),'Wave')
              !
           ENDDO
           ! 
           ! single band @ gamma
           IF( MOD(nbndval,2) == 1 ) THEN
              ibnd=nbndval
              !
              CALL single_invfft_gamma(dffts,npw,npwx,evc(1,ibnd),psic,'Wave')
              DO ir=1,dffts%nnr
                 psic(ir) = CMPLX( REAL(psic(ir),KIND=DP) * REAL(aux_r(ir),KIND=DP), 0._DP, KIND=DP)
              ENDDO
              CALL single_fwfft_gamma(dffts,npw,npwx,psic,dvpsi(1,ibnd),'Wave') 
              !
           ENDIF
           !
        ELSE
           !
           ! only single bands
           DO ibnd=1,nbndval
              !
              CALL single_invfft_k(dffts,npw,npwx,evc(1,ibnd),psic,'Wave',igk_k(1,current_k))
              DO ir=1,dffts%nnr
                 psic(ir) = psic(ir) * aux_r(ir)
              ENDDO
              CALL single_fwfft_k(dffts,npw,npwx,psic,dvpsi(1,ibnd),'Wave',igk_k(1,current_k)) 
              !
           ENDDO
           !
           IF(npol==2) THEN
              DO ibnd=1,nbndval
                 !
                 CALL single_invfft_k(dffts,npw,npwx,evc(npwx+1,ibnd),psic,'Wave',igk_k(1,current_k))
                 DO ir=1,dffts%nnr
                    psic(ir) = psic(ir) * aux_r(ir)
                 ENDDO
                 CALL single_fwfft_k(dffts,npw,npwx,psic,dvpsi(npwx+1,ibnd),'Wave',igk_k(1,current_k)) 
                 !
              ENDDO
           ENDIF
           !
        ENDIF   
        !
        DEALLOCATE(aux_g)
        DEALLOCATE(aux_r)
        !
        CALL apply_alpha_pc_to_m_wfcs(nbndval,nbndval,dvpsi,(-1._DP,0._DP))
        !
        CALL precondition_m_wfcts( nbndval, dvpsi, dpsi, eprec ) 
        !
        IF( l_dost) THEN
           ! 
           CALL linsolve_sternheimer_m_wfcts (nbndval, nbndval, dvpsi, dpsi, et(1,iks), eprec, tr2, ierr ) 
           !
           IF(ierr/=0) THEN 
              WRITE(stdout, '(7X,"** WARNING : PERT ",i8," not converged, ierr = ",i8)') ipert,ierr
           ENDIF
           !
        ENDIF
        !
        ALLOCATE(aux_r(dffts%nnr))
        !
        aux_r=0._DP
        ! 
        IF(gamma_only) THEN
           !
           ! double band @ gamma
           DO ibnd=1,nbndval
              !
              CALL double_invfft_gamma(dffts,npw,npwx,evc(1,ibnd),dpsi(1,ibnd),psic,'Wave')
              DO ir=1,dffts%nnr
                 prod =  REAL( psic(ir),KIND=DP) * DIMAG( psic(ir)) 
                 aux_r(ir) = aux_r(ir) + CMPLX( prod, 0.0_DP, KIND=DP)
              ENDDO
              !
           ENDDO
           !
        ELSE
           !
           ALLOCATE(dpsic(dffts%nnr))
           !
           ! only single bands
           DO ibnd=1,nbndval 
              !
              CALL single_invfft_k(dffts,npw,npwx,evc(1,ibnd),psic,'Wave',igk_k(1,current_k))
              CALL single_invfft_k(dffts,npw,npwx,dpsi(1,ibnd),dpsic,'Wave',igk_k(1,current_k))
              DO ir=1,dffts%nnr 
                 aux_r(ir) = aux_r(ir) + DCONJG(psic(ir))*dpsic(ir)
              ENDDO
              !
           ENDDO
           !
           IF(npol==2) THEN
              DO ibnd=1,nbndval 
                 !
                 CALL single_invfft_k(dffts,npw,npwx,evc(npwx+1,ibnd),psic,'Wave',igk_k(1,current_k))
                 CALL single_invfft_k(dffts,npw,npwx,dpsi(npwx+1,ibnd),dpsic,'Wave',igk_k(1,current_k))
                 DO ir=1,dffts%nnr 
                    aux_r(ir) = aux_r(ir) + DCONJG(psic(ir))*dpsic(ir)
                 ENDDO
                 !
              ENDDO
           ENDIF
           !
           DEALLOCATE(dpsic)
           !
        ENDIF
        !
        ! The perturbation is in aux_r
        !
285
        ALLOCATE(aux_g(npwqx))
Marco Govoni's avatar
Marco Govoni committed
286
        IF(gamma_only) THEN
287
           CALL single_fwfft_gamma(dffts,npwq,npwqx,aux_r,aux_g,TRIM(fftdriver)) 
Marco Govoni's avatar
Marco Govoni committed
288
        ELSE
289
           CALL single_fwfft_k(dffts,npwq,npwqx,aux_r,aux_g,TRIM(fftdriver)) ! no igk
Marco Govoni's avatar
Marco Govoni committed
290
291
        ENDIF
        !
292
        DO ig=1,npwq ! pert acts only on body
Marco Govoni's avatar
Marco Govoni committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
           dng(ig,ipert) = dng(ig,ipert) + 2._DP * wk(iks) * aux_g(ig) * sqvc(ig) / omega
        ENDDO
        !
        DEALLOCATE(aux_g)
        DEALLOCATE(aux_r)
        !
        CALL update_bar_type( barra,'dfpt', 1 ) 
        !
     ENDDO ! ipert
     !
     IF( m==0 ) CALL update_bar_type( barra,'dfpt', 1 )
     !
     DEALLOCATE(eprec)
     DEALLOCATE(dpsi)
     DEALLOCATE(dvpsi)
     !
  ENDDO ! K-POINT and SPIN
  !
  IF( gstart==2 ) dng(1,1:m) = CMPLX( 0._DP, 0._DP, KIND=DP )
  !
  CALL mp_sum(dng,inter_pool_comm)
  !
  CALL mp_barrier( world_comm )
  !
  CALL stop_bar_type( barra, 'dfpt' )
  !
!  CALL close_buffer(iuwfc,'delete')
  !IF( nks > 1 ) THEN
  !   IF ( exst ) THEN
  !      CLOSE(unit=iuwfc,status='keep')
  !   ELSE
  !      CLOSE(unit=iuwfc,status='delete')
  !   ENDIF
  !ENDIF
  !
END SUBROUTINE
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
!
!
!-------------------------------------------------------------------------
SUBROUTINE dfpt_q (m,dvg,dng,tr2,iq)
  !-----------------------------------------------------------------------
  !
  USE kinds,                 ONLY : DP
  USE constants,             ONLY : tpi
  USE io_global,             ONLY : stdout
  USE wvfct,                 ONLY : nbnd,g2kin,et
  USE fft_base,              ONLY : dfftp,dffts
  USE gvect,                 ONLY : nl,nl,gstart,ig_l2g,g,ngm
  USE wavefunctions_module,  ONLY : evc,psic
  USE gvecs,                 ONLY : ngms,nls
  USE gvecw,                 ONLY : gcutw
  USE mp,                    ONLY : mp_sum,mp_barrier,mp_bcast
  USE mp_global,             ONLY : inter_image_comm,inter_pool_comm,my_image_id
  USE mp_bands,              ONLY : me_bgrp
  USE fft_at_k,              ONLY : single_fwfft_k,single_invfft_k
  USE fft_interfaces,        ONLY : fwfft, invfft
  USE buffers,               ONLY : get_buffer
  USE noncollin_module,      ONLY : noncolin,npol
  USE bar,                   ONLY : bar_type,start_bar_type,update_bar_type,stop_bar_type
  USE pwcom,                 ONLY : current_spin,wk,nks,nelup,neldw,isk,xk,npw,npwx,lsda,nkstot,&
                                  & current_k,ngk,igk_k
  USE cell_base,             ONLY : tpiba2,omega,at
  USE control_flags,         ONLY : gamma_only, io_level
  USE io_files,              ONLY : tmp_dir, nwordwfc, iunwfc, diropn
  USE uspp,                  ONLY : nkb, vkb, okvan
  USE westcom,               ONLY : sqvc,nbnd_occ,iuwfc,lrwfc,l_gammaq,npwqx,npwq,igq_q
  USE io_push,               ONLY : io_push_title
  USE mp_world,              ONLY : mpime,world_comm
  USE class_bz_grid,         ONLY : bz_grid
  USE types_bz_grid,              ONLY : kmq_grid
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  INTEGER, INTENT(IN) :: iq
  INTEGER, INTENT(IN) :: m
  COMPLEX(DP), INTENT(IN) :: dvg(npwqx,m)
  COMPLEX(DP), INTENT(OUT) :: dng(npwqx,m)
  REAL(DP),INTENT(IN) :: tr2
  !
  ! Workspace
  !
  INTEGER :: ipert, ig, ir, ibnd, iks, ikqs
  ! Counter on perturbations
  ! Counter on plane waves
  ! Counter on real space grids
  ! Counter on bands
  ! Counter on k-points
  ! Counter on (k-q)-points
  INTEGER :: i, j, k
  ! Counter on real space grid
  INTEGER :: nbndval, ierr
  ! Current k-q point
  INTEGER :: npwkq
  !
  REAL(DP) :: anorm, prod 
  REAL(DP), ALLOCATABLE :: eprec(:)
  ! Preconditioning matrix
  !
  COMPLEX(DP), ALLOCATABLE :: dvpsi(:,:),dpsi(:,:)
  COMPLEX(DP), ALLOCATABLE :: aux_r(:),aux_g(:)
  COMPLEX(DP), ALLOCATABLE :: dpsic(:)
  !
  COMPLEX(DP), ALLOCATABLE :: evckmq(:,:)
  COMPLEX(DP), ALLOCATABLE :: phase(:)
  !
  TYPE(bar_type) :: barra
  !
  LOGICAL :: conv_dfpt
  LOGICAL :: exst,exst_mem
  LOGICAL :: l_dost
  !
  CHARACTER(LEN=512) :: title
  !
  CALL mp_barrier( world_comm )
  !
  CALL report_dynamical_memory( )
  !
  l_dost = ( tr2 >= 0._DP )
  !
  IF( l_dost ) THEN
     WRITE(title,'(a,es14.6)') "Sternheimer eq. solver... with threshold = ", tr2
  ELSE
     WRITE(title,'(a,es14.6)') "Sternheimer eq. solver... with lite-solver"
  ENDIF
  CALL io_push_title(TRIM(ADJUSTL(title)))
  !
  dng=0.0_DP
  !
  CALL start_bar_type( barra, 'dfpt_q', MAX(m,1) * nks )
  !
  ALLOCATE( evckmq(npwx*npol,nbnd) )
  ALLOCATE( phase(dffts%nnr) )
  !
  DO iks = 1, nks  ! KPOINT-SPIN LOOP
     !
     current_k = iks
     !
     ikqs = kmq_grid%index_kq(iks,iq)
     !
     ! computes the phase needed to bring the wavefunction at k-q 
     ! to the equivalent [k-q] point in the first BZ
     !
     CALL kmq_grid%get_phase(iks,iq)
     phase = kmq_grid%phase
     !
     IF ( lsda ) current_spin = isk(iks)
     CALL g2_kin(iks) 
     !
     !
     nbndval = nbnd_occ(iks)
     !
     !
     ! Number of G vectors for planewave expansion of wavefunctions
     ! at k and k-q; max number over all processors defined by npwx
     ! for both (see set_psi_kmq)
     !
     npw = ngk(iks)
     npwkq = ngk(ikqs)
     !
     ! ... More stuff needed by the hamiltonian: nonlocal projectors
     !
     IF ( nkb > 0 ) CALL init_us_2( ngk(iks), igk_k(1,iks), xk(1,iks), vkb )
     !
     ! Read wavefuctions at k in G space, for all bands,
     ! and store them in evc
     !
     IF ( my_image_id == 0 ) CALL get_buffer( evc, lrwfc, iuwfc, iks )
     CALL mp_bcast( evc, 0, inter_image_comm )
     !
     ! Set wavefunctions at [k-q] in G space, for all bands,
     ! and store them in evckmq
     ! 
     IF ( my_image_id == 0 ) CALL get_buffer( evckmq, lrwfc, iuwfc, ikqs )
     CALL mp_bcast( evckmq, 0, inter_image_comm )
     !
     ALLOCATE(eprec(nbndval))
     CALL set_eprec(nbndval,evc(1,1),eprec)
     !
     ALLOCATE(dvpsi(npwx*npol,nbndval))
     ALLOCATE(dpsi(npwx*npol,nbndval))
     !
     DO ipert = 1, m
        !
        ALLOCATE( aux_g(npwqx) )
        ALLOCATE( aux_r(dffts%nnr) )
        !
        aux_g = (0._DP,0._DP)
        aux_r = (0._DP,0._DP)
        !
        DO ig = 1, npwq  ! perturbation acts only on body
           aux_g(ig) = dvg(ig,ipert) * sqvc(ig) 
        ENDDO
        !
        ! inverse Fourier transform of the perturbation: (q+)G ---> R
        !
        CALL single_invfft_k(dffts,npwq,npwqx,aux_g,aux_r,'Wave',igq_q(1,iq))
        !
        ! The perturbation is in aux_r
        !
        dvpsi = (0._DP,0._DP)
        dpsi  = (0._DP,0._DP)
        !
        DO ibnd = 1, nbndval
           !
           ! inverse Fourier transform of wavefunction at [k-q]: (k-q+)G ---> R
           !
           CALL single_invfft_k(dffts,npwkq,npwx,evckmq(1,ibnd),psic,'Wave',igk_k(1,ikqs))
           !
           ! construct right-hand-side term of Sternheimer equation:
           ! product of wavefunction at [k-q], phase and perturbation in real space
           !
           DO ir = 1, dffts%nnr
              psic(ir) = psic(ir) * phase(ir) * aux_r(ir)
           ENDDO
           !
           ! Fourier transform product of wavefunction at [k-q], phase and
           ! perturbation of wavevector q: R ---> (k+)G
           !
           CALL single_fwfft_k(dffts,npw,npwx,psic,dvpsi(1,ibnd),'Wave',igk_k(1,iks))
           !
           ! dv|psi> is in dvpsi
           !
        ENDDO
        !
        IF ( npol == 2 ) THEN
           DO ibnd = 1, nbndval
              !
              ! inverse Fourier transform of wavefunction at [k-q]: (k-q+)G ---> R
              !
              CALL single_invfft_k(dffts,npwkq,npwx,evckmq(1+npwx,ibnd),psic,'Wave',igk_k(1,ikqs))
              !
              ! construct right-hand-side term of Sternheimer equation:
              ! product of wavefunction at [k-q], phase and perturbation in real space
              !
              DO ir = 1, dffts%nnr
                 psic(ir) = psic(ir) * phase(ir) * aux_r(ir)
              ENDDO
              !
              ! Fourier transform product of wavefunction at [k-q], phase and
              ! perturbation of wavevector q: R ---> (k+)G
              !
              CALL single_fwfft_k(dffts,npw,npwx,psic,dvpsi(npwx+1,ibnd),'Wave',igk_k(1,iks))
              !
              ! dv|psi> is in dvpsi
              !
           ENDDO
        ENDIF
        !
        DEALLOCATE( aux_g )
        DEALLOCATE( aux_r )
        !
        ! P_c | dvpsi >
        !
        CALL apply_alpha_pc_to_m_wfcs( nbndval, nbndval, dvpsi, (-1._DP,0._DP) )
        !
        CALL precondition_m_wfcts( nbndval, dvpsi, dpsi, eprec )
        !
        IF( l_dost) THEN
           ! 
           ! The Sternheimer operator is (H_k - E_(k-q) + alpha * P_v)
           ! The Hamiltonian is evaluated at the k-point current_k in h_psi
           ! (see also PHonon/PH/cch_psi_all.f90, where H_(k+q) is evaluated)
           !
           CALL linsolve_sternheimer_m_wfcts (nbndval, nbndval, dvpsi, dpsi, et(1,ikqs), eprec, tr2, ierr )
           !
           IF(ierr/=0) THEN
              WRITE(stdout, '(7X,"** WARNING : PERT ",i8," iks ",I8," not converged, ierr = ",i8)') ipert,iks,ierr
           ENDIF
           !
        ENDIF
        !
        ALLOCATE( aux_r(dffts%nnr) )
        !
        aux_r = (0._DP,0._DP)
        ! 
        ALLOCATE( dpsic(dffts%nnr) )
        !
        DO ibnd = 1, nbndval
           !
           ! inverse Fourier transform of wavefunction at [k-q]: (k-q+)G ---> R
           !
           CALL single_invfft_k(dffts,npwkq,npwx,evckmq(1,ibnd),psic,'Wave',igk_k(1,ikqs))
           !
           ! inverse Fourier transform of perturbed wavefunction: (k+)G ---> R
           !
           CALL single_invfft_k(dffts,npw,npwx,dpsi(1,ibnd),dpsic,'Wave',igk_k(1,iks))
           !
           DO ir = 1, dffts%nnr 
              aux_r(ir) = aux_r(ir) + CONJG( psic(ir) * phase(ir) ) * dpsic(ir)
           ENDDO
           !
        ENDDO
        !
        IF ( npol == 2 ) THEN
           DO ibnd = 1, nbndval 
              !
              ! inverse Fourier transform of wavefunction at [k-q]: (k-q+)G ---> R
              !
              CALL single_invfft_k(dffts,npwkq,npwx,evckmq(1+npwx,ibnd),psic,'Wave',igk_k(1,ikqs))
              !
              ! inverse Fourier transform of perturbed wavefunction: (k+)G ---> R
              !
              CALL single_invfft_k(dffts,npw,npwx,dpsi(npwx+1,ibnd),dpsic,'Wave',igk_k(1,iks))
              !
              DO ir = 1, dffts%nnr 
                 aux_r(ir) = aux_r(ir) + CONJG( psic(ir) * phase(ir) ) * dpsic(ir)
              ENDDO
              !
           ENDDO
        ENDIF
        !
        DEALLOCATE( dpsic )
        !
        ! The perturbation is in aux_r
        !
        ALLOCATE( aux_g(npwqx) )
        !       
        ! Fourier transform of product of unperturbed and perturbed wavefunction: R ---> (q+)G
        !
        CALL single_fwfft_k(dffts,npwq,npwqx,aux_r,aux_g,'Wave',igq_q(1,iq))
        !
        DO ig = 1, npwq ! pert acts only on body
           dng(ig,ipert) = dng(ig,ipert) + 2._DP * wk(iks) * aux_g(ig) * sqvc(ig) / omega
        ENDDO
        !
        DEALLOCATE( aux_g )
        DEALLOCATE( aux_r )
        !
        CALL update_bar_type( barra, 'dfpt', 1 ) 
        !
     ENDDO ! ipert
     !
     IF( m == 0 ) CALL update_bar_type( barra, 'dfpt', 1 )
     !
     DEALLOCATE( eprec )
     DEALLOCATE( dpsi )
     DEALLOCATE( dvpsi )
     !
  ENDDO ! K-POINT and SPIN
  !
  IF ( l_gammaq ) THEN
     IF ( gstart == 2 ) dng(1,1:m) = CMPLX( 0._DP, 0._DP, KIND=DP )
  ENDIF
  !
  DEALLOCATE( evckmq )
  DEALLOCATE( phase )
  !
  CALL mp_sum(dng,inter_pool_comm)
  !
  CALL mp_barrier( world_comm )
  !
  CALL stop_bar_type( barra, 'dfpt_q' )
  !
END SUBROUTINE