pwden.F90 26.9 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

7 8 9
MODULE m_pwden
CONTAINS
  SUBROUTINE pwden(stars,kpts,banddos,oneD, input,mpi,noco,cell,atoms,sym, &
10
       ikpt,jspin,lapw,ne, igq_fft,we,eig,bkpt, qpw,cdom, qis,forces,f_b8,zMat)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    !     In this subroutine the star function expansion coefficients of
    !     the plane wave charge density is determined.
    !
    !     This subroutine is called for each k-point and each spin.
    !
    !
    !     Two methods are implemented to calculate the charge density
    !     1) which uses the FFT. The effort in calculating the charge
    !        density is proportional to M * N * log(N) , M being number of
    !        states and N being number of plane waves. This is the method
    !        which we use for production runs
    !     2) the traditional method for calculating the charge density
    !        using the double summation. In this case the effort scales as
    !        M * N * N. The method is only used for test purposes or for
    !        special cases.
    !
    !
    !     INPUT:    eigen vectors
    !               reciprocal lattice information
    !               Brillouine zone sampling
    !               FFT information
    !
    !     OUTPUT:   qpw(s)
    !               1) using FFT
    !
    !                2) traditional method
    !
    !                             -1             ef
    !                qpw  (g) = vol * sum{ sum{ sum{ sum{ w(k) * f(nu) *
    !                                  sp   k    nu   g'
    !                                     *
    !                                    c(g'-g,nu,k) * c(g',nu,k) } } } }
    !                or :
    !                             -1             ef
    !                qpw  (g) = vol * sum{ sum{ sum{ sum{ w(k) * f(nu) *
    !                                  sp   k    nu   g'
    !                                     *
    !                                    c(g',nu,k) * c(g'+g,nu,k) } } } }
    !
    !                qpw(g) are actuall 
    ! 
    !                the weights w(k) are normalized: sum{w(k)} = 1
    !                                                  k                -6
    !                         a) 1                           for kT < 10
    !                f(nu) = {                           -1             -6
    !                         b){ 1+exp(e(k,nu) -ef)/kt) }   for kt >=10
    !
    !
    !                                      Stefan Bl"ugel, JRCAT, Feb. 1997
    !                                      Gustav Bihlmayer, UniWien       
    !
    !     In non-collinear calculations the density becomes a hermitian 2x2
    !     matrix. This subroutine generates this density matrix in the 
    !     interstitial region. The diagonal elements of this matrix 
    !     (n_11 & n_22) are stored in qpw, while the real and imaginary part
    !     of the off-diagonal element are store in cdom. 
    !
    !     Philipp Kurz 99/07
    !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    !
    !
73
!DEC$ NOOPTIMIZE
74 75 76 77 78 79 80 81
#include"cpp_double.h"
    USE m_forceb8
    USE m_pwint
    USE m_juDFT
    USE m_rfft
    USE m_cfft
    USE m_types
    IMPLICIT NONE
82
    TYPE(t_lapw),INTENT(IN)     :: lapw
83 84 85 86 87 88 89 90 91 92
    TYPE(t_mpi),INTENT(IN)      :: mpi
    TYPE(t_oneD),INTENT(IN)     :: oneD
    TYPE(t_banddos),INTENT(IN)  :: banddos
    TYPE(t_input),INTENT(IN)    :: input
    TYPE(t_noco),INTENT(IN)     :: noco
    TYPE(t_sym),INTENT(IN)      :: sym
    TYPE(t_stars),INTENT(IN)    :: stars
    TYPE(t_cell),INTENT(IN)     :: cell
    TYPE(t_kpts),INTENT(IN)     :: kpts
    TYPE(t_atoms),INTENT(IN)    :: atoms
93 94
    TYPE(t_zMat),INTENT(IN)     :: zMat

95 96 97 98 99 100 101 102 103 104
    INTEGER, INTENT (IN)        :: igq_fft(0:stars%kq1d*stars%kq2d*stars%kq3d-1)
    REAL,INTENT(IN)   :: we(:) !(nobd) 
    REAL,INTENT(IN)   :: eig(:)!(dimension%neigd)
    REAL,INTENT(IN)   :: bkpt(3)
    !----->  BASIS FUNCTION INFORMATION
    INTEGER,INTENT(IN):: ne
    !----->  CHARGE DENSITY INFORMATION
    INTEGER,INTENT(IN)    :: ikpt,jspin 
    COMPLEX,INTENT(INOUT) :: qpw(:,:) !(stars%n3d,dimension%jspd)
    COMPLEX,INTENT(INOUT) :: cdom(:)!(stars%n3d)
Daniel Wortmann's avatar
Daniel Wortmann committed
105 106 107
    REAL,INTENT(OUT)      :: qis(:,:,:) !(dimension%neigd,kpts%nkpt,dimension%jspd)
    COMPLEX, INTENT (INOUT) ::  f_b8(3,atoms%ntype)
    REAL,    INTENT (INOUT) :: forces(:,:,:) !(3,atoms%ntype,dimension%jspd)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    !
    !-----> LOCAL VARIABLES
    !
    !----->  FFT  INFORMATION
    INTEGER :: ifftq2d,ifftq3d

    INTEGER  isn,nu,iv,ir,ik,il,im,in,istr,nw1,nw2,nw3,i,j
    INTEGER  ifftq1,ifftq2,ifftq3
    INTEGER  idens,ndens,ispin,jkpt,jsp_start,jsp_end
    REAL     q0,q0_11,q0_22,scale,xk(3)
    REAL     s
    COMPLEX  x
    INTEGER,PARAMETER::  ist(-1:1)=(/1,0,0/)
    REAL,PARAMETER:: zero   = 0.00,  tol_3=1.0e-3 
    !
    INTEGER  iv1d(SIZE(lapw%k1,1),input%jspins)
    REAL wtf(ne),wsave(stars%kq3d+15)
    REAL,    ALLOCATABLE :: psir(:),psii(:),rhon(:)
    REAL,    ALLOCATABLE :: psi1r(:),psi1i(:),psi2r(:),psi2i(:)
    REAL,    ALLOCATABLE :: rhomat(:,:)
    REAL,    ALLOCATABLE :: kpsir(:),kpsii(:)
    REAL,    ALLOCATABLE :: ekin(:)
    COMPLEX, ALLOCATABLE :: cwk(:),ecwk(:)
    !
132 133
    LOGICAL l_real
    REAL     CPP_BLAS_sdot
134 135 136
    EXTERNAL CPP_BLAS_sdot
    COMPLEX  CPP_BLAS_cdotc
    EXTERNAL CPP_BLAS_cdotc
137

138
    
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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
    !------->          ABBREVIATIONS
    !
    !     rhon  : charge density in real space
    !     ne    : number of occupied states
    !     nv    : number of g-components in eigenstate
    !     cv=z  : wavefunction in g-space (reciprocal space)
    !     psir   : wavefunction in r-space (real-space)
    !     cwk   : complex work array: charge density in g-space (as stars)
    !     qpw   : charge density stored as stars
    !     trdchg: logical key, determines the mode of charge density
    !             calculation: false (default) : fft
    !                          true            : double sum over stars
    !     we    : weights for the BZ-integration for a particular k-point
    !     omtil : volume (slab) unit cell, between -.5*D_tilde and +.5*D_tilde
    !     k1   : reciprocal lattice vectors G=G(k1,k2,k3) for wavefunction
    !     k2   :                             =k1*a_1 + k2*a_2 + k3*a_3
    !     k3   : where a_i= Bravais lattice vectors in reciprocal space
    !             kwi, depend on k-point.                            
    !     kq1d  : dimension of the charge density FFT box in the pos. domain
    !     kq2d  : defined in dimens.f program (subroutine apws).1,2,3 indicate
    !     kq3d  ; a_1, a_2, a_3 directions.
    !     kq(i) : i=1,2,3 actual length of the fft-box for which FFT is done.
    !     nstr  : number of members (arms) of reciprocal lattice (g) vector
    !             of each star.
    !     ng3_fft: number of stars in the  charge density  FFT-box
    !     ng3   : number of 3 dim. stars in the charge density sphere defined
    !             by gmax
    !     kmxq_fft: number of g-vectors forming the ng3_fft stars in the
    !               charge density sphere 
    !     kimax : number of g-vectors forming the ng3 stars in the gmax-sphere
    !     iv1d  : maps vector (k1,k2,k3) of wave function into one
    !             dimensional vector of cdn-fft box in positive domain.
    !     ifftq3d: elements (g-vectors) in the charge density  FFT-box
    !     igfft : pointer from the g-sphere (stored as stars) to fft-grid 
    !             and     from fft-grid to g-sphere (stored as stars)
    !     pgfft : contains the phases of the g-vectors of sph.     
    !     isn   : isn = +1, FFT transform for g-space to r-space
    !             isn = -1, vice versa
    !


    ALLOCATE(cwk(stars%n3d),ecwk(stars%n3d))

    IF (noco%l_noco) THEN
       ALLOCATE ( psi1r(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
            psi1i(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
            psi2r(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
            psi2i(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
            rhomat(0:stars%kq1d*stars%kq2d*stars%kq3d-1,4) )
    ELSE
189
       IF (zmat%l_real) THEN
190 191 192 193 194 195 196 197 198 199 200 201 202 203
          ALLOCATE ( psir(-stars%kq1d*stars%kq2d:2*stars%kq1d*stars%kq2d*(stars%kq3d+1)-1),&
               psii(1),&
               rhon(-stars%kq1d*stars%kq2d:stars%kq1d*stars%kq2d*(stars%kq3d+1)-1) )
          IF (input%l_f) ALLOCATE ( kpsii(1),&
               kpsir(-stars%kq1d*stars%kq2d:2*stars%kq1d*stars%kq2d*(stars%kq3d+1)-1),&
               ekin(-stars%kq1d*stars%kq2d:2*stars%kq1d*stars%kq2d*(stars%kq3d+1)-1))
       ELSE
          ALLOCATE ( psir(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
               psii(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
               rhon(0:stars%kq1d*stars%kq2d*stars%kq3d-1) )
          IF (input%l_f) ALLOCATE ( kpsir(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
               kpsii(0:stars%kq1d*stars%kq2d*stars%kq3d-1),&
               ekin(0:stars%kq1d*stars%kq2d*stars%kq3d-1) )
       ENDIF
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
    ENDIF
    !
    !=======>  CALCULATE CHARGE DENSITY USING FFT
    ! 
    !
    !------> setup FFT
    !
    ifftq1  = stars%kq1_fft
    ifftq2  = stars%kq1_fft*stars%kq2_fft
    ifftq3  = stars%kq1_fft*stars%kq2_fft*stars%kq3_fft
    ifftq3d = stars%kq1d*stars%kq2d*stars%kq3d
    ifftq2d = stars%kq1d*stars%kq2d
    !
    nw1=NINT(stars%kq1_fft/4.+0.3)
    nw2=NINT(stars%kq2_fft/4.+0.3)
    nw3=NINT(stars%kq3_fft/4.+0.3)
    !
    !------> g=0 star: calculate the charge for this k-point and spin
    !                  analytically to test the quality of FFT
    !
    q0 = zero
    q0_11 = zero
    q0_22 = zero
    IF (noco%l_noco) THEN
       q0_11 = zero
       q0_22 = zero
230
       IF (.NOT.zmat%l_real ) THEN
231
          DO nu = 1 , ne
232 233
             q0_11 = q0_11 + we(nu) * CPP_BLAS_cdotc(lapw%nv(1),zMat%z_c(1,nu),1,zMat%z_c(1,nu),1)
             q0_22 = q0_22 + we(nu) * CPP_BLAS_cdotc(lapw%nv(2),zMat%z_c(lapw%nv(1)+atoms%nlotot+1,nu),1, zMat%z_c(lapw%nv(1)+atoms%nlotot+1,nu),1)
234 235
          ENDDO
       ENDIF
236 237 238
       q0_11 = q0_11/cell%omtil
       q0_22 = q0_22/cell%omtil
    ELSE
239
       IF (zmat%l_real) THEN
240
          DO nu = 1 , ne
241
             q0=q0+we(nu)*CPP_BLAS_sdot(lapw%nv(jspin),zMat%z_r(1,nu),1,zMat%z_r(1,nu),1)
242 243 244
          ENDDO
       ELSE
          DO nu = 1 , ne
245
             q0=q0+we(nu) *REAL(CPP_BLAS_cdotc(lapw%nv(jspin),zMat%z_c(1,nu),1,zMat%z_c(1,nu),1))
246 247
          ENDDO
       ENDIF
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
       q0 = q0/cell%omtil
    ENDIF
    !
    !--------> initialize charge density with zero
    !
    IF (noco%l_noco) THEN
       rhomat = 0.0
       IF (ikpt.LE.mpi%isize) THEN
          qis=0.0
       ENDIF
    ELSE
       rhon=0.0
       IF (input%l_f) ekin=0.0
    ENDIF
    !
    !------> calculate:  wtf(nu,k) =  w(k)*f(nu,k)/vol
    !
    wtf(:ne) = we(:ne)/cell%omtil
    !
    !------> prepare mapping from wave function box to cdn FFT box
    !
    IF (noco%l_ss) THEN
       jsp_start = 1
       jsp_end   = 2
    ELSE
       jsp_start = jspin
       jsp_end   = jspin
    ENDIF
    DO ispin = jsp_start,jsp_end
       DO iv = 1 , lapw%nv(ispin)
          !                                              -k1d <= L <= k1d
          !                                              -k2d <= M <= k2d
          !                                              -k3d <= N <= k3d
          il = lapw%k1(iv,ispin)
          im = lapw%k2(iv,ispin)
          in = lapw%k3(iv,ispin)
          !
          !------>  L,M,N LATTICE POINTS OF G-VECTOR IN POSITIVE DOMAIN
          !         (since charge density box = two times charge density box
          !          wrap arround error should not occur )
          !                                           0<= L <=2*k1-1 = kq1_fft-1
          !                                           0<= M <=2*k2-1 = kq2_fft-1
          !                                           0<= N <=2*k3-1 = kq3_fft-1
          !
          il = il  +  stars%kq1_fft * ist( isign(1,il) )
          im = im  +  stars%kq2_fft * ist( isign(1,im) )
          in = in  +  stars%kq3_fft * ist( isign(1,in) )
          !
          iv1d(iv,ispin) =  in*ifftq2 + im*ifftq1 + il
       ENDDO
    ENDDO

    !
    !------------> LOOP OVER OCCUPIED STATES
    !
    DO  nu = 1 , ne
       !
       !---> FFT transform c_nu,k(g) --> psi_nu,k(r), for each k-point
       !                                              and each nu-state
       IF (noco%l_noco) THEN
          psi1r=0.0
          psi1i=0.0
          psi2r=0.0
          psi2i=0.0
          !------> map WF into FFTbox
          IF (noco%l_ss) THEN
             DO iv = 1 , lapw%nv(1)
315 316
                psi1r( iv1d(iv,1) )   = REAL( zMat%z_c(iv,nu) )
                psi1i( iv1d(iv,1) )   = AIMAG( zMat%z_c(iv,nu) )
317 318
             ENDDO
             DO iv = 1 , lapw%nv(2)
319 320
                psi2r( iv1d(iv,2) ) =  REAL(zMat%z_c(lapw%nv(1)+atoms%nlotot+iv,nu))
                psi2i( iv1d(iv,2) ) = AIMAG(zMat%z_c(lapw%nv(1)+atoms%nlotot+iv,nu))
321 322 323
             ENDDO
          ELSE
             DO iv = 1 , lapw%nv(jspin)
324 325 326 327
                psi1r( iv1d(iv,jspin) ) = REAL( zMat%z_c(iv,nu) )
                psi1i( iv1d(iv,jspin) ) = AIMAG( zMat%z_c(iv,nu) )
                psi2r(iv1d(iv,jspin))=REAL( zMat%z_c(lapw%nv(1)+atoms%nlotot+iv,nu))
                psi2i(iv1d(iv,jspin))=AIMAG(zMat%z_c(lapw%nv(1)+atoms%nlotot+iv,nu))
328 329
             ENDDO
          ENDIF
330

331 332 333 334
       ELSE
          psir=0.0
          psii=0.0
          !------> map WF into FFTbox
335
          IF (zmat%l_real) THEN
336
             DO iv = 1 , lapw%nv(jspin)
337
                psir( iv1d(iv,jspin) ) = zMat%z_r(iv,nu)
338 339 340
             ENDDO
          ELSE
             DO iv = 1 , lapw%nv(jspin)
341 342
                psir( iv1d(iv,jspin) ) =  REAL(zMat%z_c(iv,nu))
                psii( iv1d(iv,jspin) ) = AIMAG(zMat%z_c(iv,nu))
343 344
             ENDDO
          ENDIF
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
       ENDIF
       !
       !------> do (real) inverse FFT; notice that the array psir is filled from
       !        0 to ifftq3-1, but starts at -ifftq2 to give work space for rfft
       !
       IF (noco%l_noco) THEN
          isn = 1

          CALL cfft(psi1r,psi1i,ifftq3,stars%kq1_fft,ifftq1,isn)
          CALL cfft(psi1r,psi1i,ifftq3,stars%kq2_fft,ifftq2,isn)
          CALL cfft(psi1r,psi1i,ifftq3,stars%kq3_fft,ifftq3,isn)

          CALL cfft(psi2r,psi2i,ifftq3,stars%kq1_fft,ifftq1,isn)
          CALL cfft(psi2r,psi2i,ifftq3,stars%kq2_fft,ifftq2,isn)
          CALL cfft(psi2r,psi2i,ifftq3,stars%kq3_fft,ifftq3,isn)
       ELSE
          isn = 1
362
          IF (zmat%l_real) THEN
363 364
             CALL rfft(isn,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft+1,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft,&
                  nw1,nw2,nw3,wsave,psir(ifftq3d), psir(-ifftq2))
365

366 367
             ! GM forces part
             IF (input%l_f) THEN
368 369 370
                DO in=-1,stars%kq3_fft,2
                   DO im=0,ifftq2-1
                      ir = ifftq2 * in + im
371
                      ekin(ir) = ekin(ir) - wtf(nu) * eig(nu) * (psir(ir)**2 + psir(ir+ifftq2)**2)
372 373 374
                   ENDDO
                ENDDO

375 376 377 378 379 380 381 382 383 384 385
                DO j = 1,3
                   kpsir(ifftq3d:)=0.0
                   kpsir(-ifftq2d:ifftq3d)=0.0
                   DO iv = 1 , lapw%nv(jspin)
                      xk(1)=lapw%k1(iv,jspin)+bkpt(1)
                      xk(2)=lapw%k2(iv,jspin)+bkpt(2)
                      xk(3)=lapw%k3(iv,jspin)+bkpt(3)
                      s = 0.0
                      DO i = 1,3
                         s = s + xk(i)*cell%bmat(i,j)
                      ENDDO
386
                      kpsir( iv1d(iv,jspin) ) = s * zMat%z_r(iv,nu)
387 388 389 390 391 392 393 394
                   ENDDO
                   CALL rfft(isn,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft+1,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft,&
                        nw1,nw2,nw3,wsave,kpsir(ifftq3d), kpsir(-ifftq2))
                   DO in=-1,stars%kq3_fft,2
                      DO im=0,ifftq2-1
                         ir = ifftq2 * in + im
                         ekin(ir) = ekin(ir) + wtf(nu) * 0.5 * (kpsir(ir)**2 + kpsir(ir+ifftq2)**2)
                      ENDDO
395
                   ENDDO
396 397 398 399 400 401 402 403 404 405
                ENDDO
             ENDIF
          ELSE
             CALL cfft(psir,psii,ifftq3,stars%kq1_fft,ifftq1,isn)
             CALL cfft(psir,psii,ifftq3,stars%kq2_fft,ifftq2,isn)
             CALL cfft(psir,psii,ifftq3,stars%kq3_fft,ifftq3,isn)
             ! GM forces part
             IF (input%l_f) THEN
                DO ir = 0,ifftq3d-1
                   ekin(ir) = ekin(ir) - wtf(nu)*eig(nu)* (psir(ir)**2+psii(ir)**2)
406 407
                ENDDO

408 409 410 411 412 413 414 415 416 417 418
                DO j = 1,3
                   kpsir=0.0
                   kpsii=0.0
                   DO iv = 1 , lapw%nv(jspin)
                      xk(1)=lapw%k1(iv,jspin)+bkpt(1)
                      xk(2)=lapw%k2(iv,jspin)+bkpt(2)
                      xk(3)=lapw%k3(iv,jspin)+bkpt(3)
                      s = 0.0
                      DO i = 1,3
                         s = s + xk(i)*cell%bmat(i,j)
                      ENDDO
419 420
                      kpsir( iv1d(iv,jspin) ) = s *  REAL(zMat%z_c(iv,nu))
                      kpsii( iv1d(iv,jspin) ) = s * AIMAG(zMat%z_c(iv,nu))
421
                   ENDDO
422

423 424 425 426 427 428 429
                   CALL cfft(kpsir,kpsii,ifftq3,stars%kq1_fft,ifftq1,isn)
                   CALL cfft(kpsir,kpsii,ifftq3,stars%kq2_fft,ifftq2,isn)
                   CALL cfft(kpsir,kpsii,ifftq3,stars%kq3_fft,ifftq3,isn)

                   DO ir = 0,ifftq3d-1
                      ekin(ir) = ekin(ir) + wtf(nu) * 0.5 * (kpsir(ir)**2+kpsii(ir)**2)
                   ENDDO
430
                ENDDO
431
             ENDIF
432 433 434 435 436 437 438 439 440 441
          ENDIF
       ENDIF
       !----> calculate rho(r) = sum w(k)*f(nu)*conjg(psi_nu,k(r))*psi_nu,k(r)
       !                         k,nu
       !      again, we fill rhon() from -ifftq2 to ifftq3-1 for the rfft
       !
       IF (noco%l_noco) THEN
          !--->             in the non-collinear case rho becomes a hermitian 2x2
          !--->             matrix (rhomat).
          DO ir = 0,ifftq3d-1
442 443 444 445
             rhomat(ir,1) = rhomat(ir,1) + wtf(nu)*( psi1r(ir)**2 + psi1i(ir)**2 )
             rhomat(ir,2) = rhomat(ir,2) + wtf(nu)*( psi2r(ir)**2 + psi2i(ir)**2 )
             rhomat(ir,3) = rhomat(ir,3) + wtf(nu)* (psi2r(ir)*psi1r(ir)+psi2i(ir)*psi1i(ir))
             rhomat(ir,4) = rhomat(ir,4) + wtf(nu)* (psi2r(ir)*psi1i(ir)-psi2i(ir)*psi1r(ir))
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
          ENDDO
          !--->             in a non-collinear calculation the interstitial charge
          !--->             cannot be calculated by a simple substraction if the
          !--->             muffin-tin (and vacuum) charge is know, because the
          !--->             total charge does not need to be one in each spin-
          !--->             channel. Thus it has to be calculated explicitly, if
          !--->             it is needed.
          IF (banddos%dos .OR. banddos%vacdos .OR. input%cdinf) THEN
             DO ir = 0,ifftq3d-1
                psi1r(ir) = (psi1r(ir)**2 + psi1i(ir)**2)
                psi2r(ir) = (psi2r(ir)**2 + psi2i(ir)**2)
             ENDDO
             isn = -1
             psi1i=0.0
             CALL cfft(psi1r,psi1i,ifftq3,stars%kq1_fft,ifftq1,isn)
             CALL cfft(psi1r,psi1i,ifftq3,stars%kq2_fft,ifftq2,isn)
             CALL cfft(psi1r,psi1i,ifftq3,stars%kq3_fft,ifftq3,isn)
             psi2i=0.0
             CALL cfft(psi2r,psi2i,ifftq3,stars%kq1_fft,ifftq1,isn)
             CALL cfft(psi2r,psi2i,ifftq3,stars%kq2_fft,ifftq2,isn)
             CALL cfft(psi2r,psi2i,ifftq3,stars%kq3_fft,ifftq3,isn)
             cwk=0.0
             DO ik = 0 , stars%kmxq_fft - 1
469
                cwk(stars%igfft(ik,1))=cwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))*&
470 471 472
                     CMPLX(psi1r(igq_fft(ik)),psi1i(igq_fft(ik)))
             ENDDO
             DO istr = 1,stars%ng3_fft
473 474
                CALL pwint(stars,atoms,sym, oneD,cell,stars%kv3(1,istr),x)
                qis(nu,ikpt,1) = qis(nu,ikpt,1) + REAL(cwk(istr)*x)/cell%omtil/REAL(ifftq3)
475 476 477 478
             ENDDO

             cwk=0.0
             DO ik = 0 , stars%kmxq_fft - 1
479
                cwk(stars%igfft(ik,1))=cwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))* CMPLX(psi2r(igq_fft(ik)),psi2i(igq_fft(ik)))
480 481
             ENDDO
             DO istr = 1,stars%ng3_fft
482 483
                CALL pwint(stars,atoms,sym, oneD,cell, stars%kv3(1,istr), x)
                qis(nu,ikpt,input%jspins) = qis(nu,ikpt,input%jspins) + REAL(cwk(istr)*x)/cell%omtil/REAL(ifftq3)
484 485 486
             ENDDO
          ENDIF
       ELSE
487
          IF (zmat%l_real) THEN
488 489 490 491 492 493 494 495 496 497 498
             DO in=-1,stars%kq3_fft,2
                DO im=0,ifftq2-1
                   ir = ifftq2 * in + im
                   rhon(ir) = rhon(ir) + wtf(nu) * ( psir(ir)**2 + psir(ir+ifftq2)**2 )
                ENDDO
             ENDDO
          ELSE
             DO ir = 0,ifftq3d-1
                rhon(ir)=rhon(ir)+wtf(nu)*(psir(ir)**2+psii(ir)**2)
             ENDDO
          ENDIF
499
       ENDIF
500 501 502 503 504
       !              DO ir = -ifftq2 , ifftq3-1
       !     +                      + wtf(nu)*(psi(ir+ifftq3d) * psi(ir+ifftq3d)
       !     +                               + psi(ir  ) * psi(ir  )
       !     +                                 )
       !              ENDDO
505

506 507 508
    ENDDO
    !
    !<<<<<<<<<<<<<< END OUTER LOOP OVER STATES NU  >>>>>>>>>>>>>>>>>>
509 510
    !
    !
511 512 513 514 515 516 517 518 519 520
    !----> perform back  FFT transform: rho(r) --> chgn(star)
    !        ( do direct FFT)                    = cwk(star)

    !--->  In a collinear calculation pwden is calles once per spin.
    !--->  However in a non-collinear calculation pwden is only called once
    !--->  and all four components of the density matrix (n_11 n_22 n_12
    !--->  n_21) have to be calculated at once.
    ndens = 1
    IF (noco%l_noco) ndens = 4
    DO idens = 1,ndens
521
       IF (noco%l_noco) THEN
522 523 524 525 526 527 528
          psi1r=0.0
          isn = -1
          CALL cfft(rhomat(0,idens),psi1r,ifftq3,stars%kq1_fft,ifftq1,isn)
          CALL cfft(rhomat(0,idens),psi1r,ifftq3,stars%kq2_fft,ifftq2,isn)
          CALL cfft(rhomat(0,idens),psi1r,ifftq3,stars%kq3_fft,ifftq3,isn)
       ELSE
          !--->  psir is used here as work array, charge is real ,but fft complex
529
          IF (zmat%l_real) THEN
530 531 532 533 534 535 536 537 538 539
             psir(ifftq3d:)=0.0
             IF (input%l_f) kpsir(ifftq3d:)=0.0
          ELSE
             psir=0.0
             psii=0.0
             IF (input%l_f) kpsir=0.0
             IF (input%l_f) kpsii=0.0
          ENDIF

          isn = -1
540
          IF (zmat%l_real) THEN
541 542 543 544
             CALL rfft(isn,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft+1,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft,&
                  stars%kq1_fft,stars%kq2_fft,stars%kq3_fft,wsave,psir(ifftq3d), rhon(-ifftq2))
             IF (input%l_f) CALL rfft(isn,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft+1,stars%kq1_fft,stars%kq2_fft,stars%kq3_fft,&
                  stars%kq1_fft,stars%kq2_fft,stars%kq3_fft,wsave,kpsir(ifftq3d), ekin(-ifftq2))
545
          ELSE
546 547 548 549 550 551 552 553 554
             CALL cfft(rhon,psir,ifftq3,stars%kq1_fft,ifftq1,isn)
             CALL cfft(rhon,psir,ifftq3,stars%kq2_fft,ifftq2,isn)
             CALL cfft(rhon,psir,ifftq3,stars%kq3_fft,ifftq3,isn)
             !+apw
             IF (input%l_f) THEN 
                CALL cfft(ekin,psii,ifftq3,stars%kq1_fft,ifftq1,isn)
                CALL cfft(ekin,psii,ifftq3,stars%kq2_fft,ifftq2,isn)
                CALL cfft(ekin,psii,ifftq3,stars%kq3_fft,ifftq3,isn)
             ENDIF
555 556
          ENDIF
       ENDIF
557 558 559 560 561 562 563 564 565
       !  ---> collect stars from the fft-grid
       !
       cwk=0.0
       ecwk=0.0
       IF (noco%l_noco) THEN
          DO ik = 0 , stars%kmxq_fft - 1
             cwk(stars%igfft(ik,1))=cwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))* CMPLX(rhomat(igq_fft(ik),idens),psi1r(igq_fft(ik)))
          ENDDO
       ELSE
566
          IF (zmat%l_real) THEN
567 568
             DO ik = 0 , stars%kmxq_fft - 1
                cwk(stars%igfft(ik,1))=cwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))* CMPLX(rhon(igq_fft(ik)),zero)
569
             ENDDO
570 571 572 573 574 575 576
          ELSE
             DO ik = 0 , stars%kmxq_fft - 1
                cwk(stars%igfft(ik,1))=cwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))* CMPLX(rhon(igq_fft(ik)),psir(igq_fft(ik)))
             ENDDO
          ENDIF
          !+apw
          IF (input%l_f) THEN 
577
             IF (zmat%l_real) THEN
578 579 580 581 582 583 584 585
                DO ik = 0 , stars%kmxq_fft - 1
                   ecwk(stars%igfft(ik,1))=ecwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))* CMPLX(ekin(igq_fft(ik)),zero)
                ENDDO
             ELSE
                DO ik = 0 , stars%kmxq_fft - 1
                   ecwk(stars%igfft(ik,1))=ecwk(stars%igfft(ik,1))+CONJG(stars%pgfft(ik))* CMPLX(ekin(igq_fft(ik)),psii(igq_fft(ik)))
                ENDDO
             ENDIF
586
          ENDIF
587
          !-apw
588
       ENDIF
589 590
       !
       scale=1.0/ifftq3
591
       DO istr = 1 , stars%ng3_fft
592
          cwk(istr) = scale * cwk(istr) / REAL( stars%nstr(istr) )
593
       ENDDO
594
       IF (input%l_useapw) THEN
595

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
          IF (input%l_f) THEN
             DO istr = 1 , stars%ng3_fft
                ecwk(istr) = scale * ecwk(istr) / REAL( stars%nstr(istr) )
             ENDDO
             CALL force_b8(atoms,ecwk,stars, sym,cell, jspin, forces,f_b8)
          ENDIF
       ENDIF
       !
       !---> check charge neutralilty
       !
       IF ((idens.EQ.1).OR.(idens.EQ.2)) THEN
          IF (noco%l_noco) THEN
             IF (idens.EQ.1) THEN
                q0 = q0_11
             ELSE
                q0 = q0_22
             ENDIF
          ENDIF
          IF ( ABS( q0 ) .GT. 1.0e-9) THEN
             IF ( ABS( q0 - REAL(cwk(1)) )/q0 .GT. tol_3 ) THEN
                WRITE(99,*) "XX:",ne,lapw%nv
617
                IF (zmat%l_real) THEN
618 619
                   DO istr=1,SIZE(zMat%z_r,2)
                      WRITE(99,*) "X:",istr,zMat%z_r(:,istr)
620 621
                   ENDDO
                ELSE
622 623
                   DO istr=1,SIZE(zMat%z_c,2)
                      WRITE(99,*) "X:",istr,zMat%z_c(:,istr)
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 649 650 651 652 653 654
                   ENDDO
                ENDIF
                WRITE ( 6,'(''bad quality of charge density'',2f13.8)')q0, REAL( cwk(1) )
                WRITE (16,'(''bad quality of charge density'',2f13.8)')q0, REAL( cwk(1) )
                CALL juDFT_warn('pwden: bad quality of charge')
             ENDIF
          ENDIF
       ENDIF
       !
       !---> add charge density to existing one
       !
       IF(idens.LE.2) THEN
          !--->       add to spin-up or -down density (collinear & non-collinear)
          ispin = jspin
          IF (noco%l_noco) ispin = idens
          DO istr = 1 , stars%ng3_fft
             qpw(istr,ispin) = qpw(istr,ispin) + cwk(istr)
          ENDDO
       ELSE IF (idens.EQ.3) THEN
          !--->       add to off-diag. part of density matrix (only non-collinear)
          DO istr = 1 , stars%ng3_fft
             cdom(istr) = cdom(istr) + cwk(istr)
          ENDDO
       ELSE
          !--->       add to off-diag. part of density matrix (only non-collinear)
          DO istr = 1 , stars%ng3_fft
             cdom(istr) = cdom(istr) + CMPLX(0.0,1.0)*cwk(istr)
          ENDDO
       ENDIF

    ENDDO
655

656
    DEALLOCATE(cwk,ecwk)
657

658 659 660 661 662 663
    IF (noco%l_noco) THEN
       DEALLOCATE ( psi1r,psi1i,psi2r,psi2i,rhomat )
    ELSE
       DEALLOCATE ( psir,psii,rhon )
       IF (input%l_f) DEALLOCATE ( kpsir,kpsii,ekin)
    ENDIF
664

665
  END SUBROUTINE pwden
666
END MODULE m_pwden