franza.F90 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 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 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 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 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 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
MODULE m_franza
CONTAINS
  SUBROUTINE franza(nbasfcn,neigd, nsize,&
       l_zref,l_J,matind,nred, a,b,gw, z,eig,ne)
    !***********************************************************************
    !
    !     solves the secular equation a*z=eig*b*z
    !
    !     nbase,nval  array dimensions as declared by calling routin
    !                         unchanged on exit
    !     nsize   actual dimension of a, b
    !     a       on entry:   hamiltonian, lower triangle stored row-wise
    !                              = upper triangle stored column-wise 
    !             on exit :   destroyed
    !     b       on entry:   overlap matrix, lower tr. stored row-wise
    !             on exit :   destroyed(overwritten by cholesky factor)
    !     eig     on entry:   ----
    !             on exit :   eigenvalues in ascending rder
    !     z       on exit :   corresponding eigenvectors
    !     ne      on exit :   number of eigenvalues/vectors actually found
    !     nblw    on exit :   number of eigenvalues below e(1)
    !     work,iwork          temporary storage of dimensions
    !                            nrtmp .ge. 8*nv
    !                            nitmp .ge. 5*nv
    !
    !  This subroutine was written by Gustav Bihlmayer 
    !                                 IFF, July 1996
    !
    !***********************************************************************
    ! lapack routines inserted for the cray
    ! please note, that if too many ev's are in your interval, you'll get 
    ! a memory fault. Then change dspevx('V','V' ... to dspevx('V','A'
    ! to see what's going on .... 
    !***********************************************************************
    USE m_juDFT
#include"cpp_double.h"
    IMPLICIT NONE
    !     ..
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN)    :: nbasfcn,neigd
    INTEGER, INTENT (INOUT) :: nsize
    INTEGER, INTENT (OUT)   :: ne
    INTEGER, INTENT (IN)    :: nred,gw
    LOGICAL, INTENT (IN)    :: l_zref,l_J
    !     ..
    !     .. Array Arguments ..
    INTEGER, INTENT (IN)  :: matind(nbasfcn,2)
    REAL,    INTENT (OUT) :: eig(neigd)
#ifdef CPP_INVERSION
    REAL,    INTENT (INOUT):: a(:),b(:)!(matsize)
    REAL,    INTENT (OUT) :: z(nbasfcn,neigd)
#else
    COMPLEX, INTENT (INOUT):: a(:),b(:)
    COMPLEX, INTENT (OUT) :: z(nbasfcn,neigd)
#endif
    !     ..
    !     .. Local Scalars ..
    REAL toler,sq2i
    INTEGER j,i1,i2,j1,j2,k,nsym,jsym,matsz
    INTEGER info,i,iu,ne_a,nsize_a,n
    LOGICAL addstate
    !     ..
    !     .. Local Arrays
#ifdef CPP_INVERSION
    REAL,    ALLOCATABLE :: aa(:), bb(:)
    REAL                 :: zz(nbasfcn,neigd+1)
#else
    COMPLEX, ALLOCATABLE :: aa(:), bb(:), cwork(:)
    COMPLEX              :: zz(nbasfcn,neigd+1)
#endif
    REAL,    ALLOCATABLE ::  work(:), etemp(:)
    INTEGER, ALLOCATABLE ::  iwork(:),ifail(:)
    LOGICAL sort(nbasfcn)
    REAL:: lb,ub

    !     ..
    !     ..
    !     .. External Subroutines ..
#ifdef CPP_INVERSION
    EXTERNAL CPP_LAPACK_spptrf,CPP_LAPACK_sspgst,CPP_LAPACK_sspevx, CPP_LAPACK_stptrs,CPP_BLAS_scopy
#else
    EXTERNAL CPP_LAPACK_cpptrf,CPP_LAPACK_chpgst,CPP_LAPACK_chpevx, CPP_LAPACK_ctptrs,CPP_BLAS_ccopy
#endif
    !     ..
    nsym=1
    IF (l_zref) THEN
       !
       ALLOCATE (aa(nred*(nred+1)/2),bb(nred*(nred+1)/2))
       !
       ! separate H and S matrix in symmetric (aa,bb) and antisymmetric (a,b) part
       !
       nsym=2
       i2=0
       j2=0
       DO i=1,nred
          DO j=1,i 
             i1=(matind(i,1)-1)*matind(i,1)/2+matind(j,1)
             j1=(matind(i,1)-1)*matind(i,1)/2+matind(j,2)
             i2=i2+1
             aa(i2)=a(i1)+a(j1)
             bb(i2)=b(i1)+b(j1)
             IF ((matind(i,1).NE.matind(i,2)).AND.(matind(j,1).NE.matind(j,2))) THEN
                j2=j2+1
                a(j2)=a(i1)-a(j1)
                b(j2)=b(i1)-b(j1)
             ENDIF
          ENDDO
       ENDDO
       nsize = nsize - nred
    ENDIF
    DO jsym=1,nsym

       IF (jsym.EQ.2) THEN
          !
          ! second time calculate symmetric part and store the antisym. EV's and EW's
          !
          matsz=(nred+1)*nred/2
#ifdef CPP_INVERSION
          CALL CPP_BLAS_scopy(matsz,aa,1,a,1)
          CALL CPP_BLAS_scopy(matsz,bb,1,b,1)
#else
          CALL CPP_BLAS_ccopy(matsz,aa,1,a,1)
          CALL CPP_BLAS_ccopy(matsz,bb,1,b,1)
#endif

          ne_a=ne
          k=1
          nsize_a=nsize
          DO i=1,ne
#ifdef CPP_INVERSION
             aa(i)=eig(i)
#else
             aa(i)=CMPLX(eig(i),0.0)
#endif
             DO j=1,nsize
                bb(k)=z(j,i)
                k=k+1
             ENDDO
          ENDDO
          nsize=nred

       ENDIF
       !-gu
       ! --> start with Cholesky factorization of b ( so that b = l * l^t)
       ! --> b is overwritten by l
       !
#ifdef CPP_INVERSION
       CALL CPP_LAPACK_spptrf('U',nsize,b,info)
#else
       CALL CPP_LAPACK_cpptrf('U',nsize,b,info)
#endif
       !
       IF (info.NE.0) THEN
#ifdef CPP_INVERSION
          WRITE (*,*) 'Error in spptrf: info =',info
#else
          WRITE (*,*) 'Error in cpptrf: info =',info
#endif
          CALL juDFT_error("Diagonalization failed",calledby="franza")
       ENDIF
       !
       ! --> now reduce a * z = eig * b * z to the standard form a' * z' = eig * z' 
       ! --> where a' = (l)^-1 * a * (l^t)^-1 and z' = l^t * z
       !
#ifdef CPP_INVERSION
       CALL CPP_LAPACK_sspgst(1,'U',nsize,a,b,info)
#else
       CALL CPP_LAPACK_chpgst(1,'U',nsize,a,b,info)
#endif
       !
       IF (info.NE.0) THEN
#ifdef CPP_INVERSION
          WRITE (6,*) 'Error in sspgst: info =',info
#else
          WRITE (6,*) 'Error in chpgst: info =',info
#endif
          CALL juDFT_error("Diagonalization failed",calledby="franza")
       ENDIF
       !
       ! --> solve a' * z' = eig * z' for eigenvalues eig between lb und ub
       !
       iu = MIN(neigd,nsize)
       IF (l_zref) iu = (neigd-1)/2
       !      toler=2.0*CPP_LAPACK_slamch('S')
       toler=2.0*TINY(sq2i)
       ALLOCATE ( work(8*nbasfcn),iwork(5*nbasfcn),ifail(nbasfcn) )
       ALLOCATE ( etemp(nbasfcn) )
       addstate = gw.NE.0.AND.iu.LT.nsize ! add one state, 
       IF(addstate) iu = iu + 1           ! see below (CF)
#ifdef CPP_INVERSION
       IF(l_J)THEN
          CALL CPP_LAPACK_sspevx('N','I','U',nsize,a,lb,ub,1,iu,toler,ne, etemp,zz,nbasfcn,work,iwork,ifail,info)
       ELSE
          CALL CPP_LAPACK_sspevx('V','I','U',nsize,a,lb,ub,1,iu,toler,ne, etemp,zz,nbasfcn,work,iwork,ifail,info)
       ENDIF
#else
       ALLOCATE ( cwork(2*nbasfcn) )
       IF(l_J)THEN
          CALL CPP_LAPACK_chpevx('N','I','U',nsize,a,lb,ub,1,iu,toler,ne, etemp,zz,nbasfcn,cwork,work,iwork,ifail,info)
       ELSE
          CALL CPP_LAPACK_chpevx('V','I','U',nsize,a,lb,ub,1,iu,toler,ne, etemp,zz,nbasfcn,cwork,work,iwork,ifail,info)
       ENDIF
       DEALLOCATE ( cwork )
#endif
       IF(addstate) THEN ! cut topmost subspace of degenerate states to avoid symmetry breaking (CF)
          iu = ne
          ne = ne - 1
          DO WHILE(ABS(etemp(ne)-etemp(iu)).LT.1d-8)
             ne = ne - 1
          ENDDO
       ENDIF
       z = zz(:,:ne) 
       !
       IF (ne.GT.neigd) THEN
          WRITE(6,*) 'ne=',ne,' > neigd'
          CALL juDFT_error("ne.GT.neigd",calledby="franza")
       ENDIF
       eig(:ne) = etemp(:ne)
       !
       IF(l_J) THEN
          DEALLOCATE ( work,iwork,ifail,etemp )
       ELSE
          IF (info.NE.0) THEN
#ifdef CPP_INVERSION
             WRITE (6,*) 'Error in sspevx: info =',info
#else
             WRITE (6,*) 'Error in chpevx: info =',info
#endif
             WRITE (6,*) 'The following eigenvectors did not converge:'
             WRITE (6,'(30i5)') (ifail(i),i=1,ne)
             CALL juDFT_error("Diagonalization failed",calledby="franza")
          ENDIF
          DEALLOCATE ( work,iwork,ifail,etemp )
          !
          ! --> recover the generalized eigenvectors z by solving z' = l^t * z
          !
#ifdef CPP_INVERSION
          CALL CPP_LAPACK_stptrs('U','N','N',nsize,ne,b,z,nbasfcn,info)
#else
          CALL CPP_LAPACK_ctptrs('U','N','N',nsize,ne,b,z,nbasfcn,info)
#endif
          !
          IF (info.NE.0) THEN
#ifdef CPP_INVERSION
             WRITE (6,*) 'Error in stptrs: info =',info
#else
             WRITE (6,*) 'Error in ctptrs: info =',info
#endif
             CALL juDFT_error("Diagonalization failed",calledby="franza")
          ENDIF
       ENDIF !l_J
       !+gu
    ENDDO
    !
    ! now collect symmetric and antisym. EW's and EV's and sort
    !     
    IF (l_zref) THEN

       k=1
       DO i=1,ne
          DO j=1,nsize
             b(k)=z(j,i)
             k=k+1
          ENDDO
       ENDDO
       !
       ! prepare sort-array: even=.true., odd=.false.
       !
       i=1
       j=1
       eig(ne+1)    = 99.9e9
#ifdef CPP_INVERSION
       aa(ne_a+1) = 99.9e9
#else
       aa(ne_a+1) = CMPLX(99.9e9,0.0)
#endif
       DO k=1,ne+ne_a
          IF (eig(i).LT.REAL(aa(j))) THEN
             sort(k)=.TRUE.
             i=i+1
          ELSE
             sort(k)=.FALSE.
             j=j+1
          ENDIF
       ENDDO
       !
       ! sort EW's and EV's
       !
       i=ne
       j=ne_a
       nsize = nsize + nsize_a
       ne = ne + ne_a
       sq2i=1.0/SQRT(2.0)
       DO k=ne,1,-1
          DO n=1,nsize
#ifdef CPP_INVERSION
             z(n,k)=0.0
#else
             z(n,k)=CMPLX(0.0,0.0)
#endif
          ENDDO
          IF (sort(k)) THEN
             eig(k)=eig(i)
             i1=nred * (i-1)
             DO n=1,nred
                i1=i1+1
#ifdef CPP_INVERSION
                z(matind(n,1),k) = z(matind(n,1),k)+b(i1)*sq2i
                z(matind(n,2),k) = z(matind(n,2),k)+b(i1)*sq2i
#else
                z(matind(n,1),k) = z(matind(n,1),k)+b(i1)*CMPLX(sq2i,0.0)
                z(matind(n,2),k) = z(matind(n,2),k)+b(i1)*CMPLX(sq2i,0.0)
#endif
             ENDDO
             i=i-1
          ELSE
#ifdef CPP_INVERSION
             eig(k)=aa(j)
#else
             eig(k)=REAL(aa(j))
#endif
             j1=nsize_a * (j-1)
             DO n=1,nred
                IF (matind(n,1).NE.matind(n,2)) THEN
                   j1=j1+1
#ifdef CPP_INVERSION
                   z(matind(n,1),k) = z(matind(n,1),k)+bb(j1)*sq2i
                   z(matind(n,2),k) = z(matind(n,2),k)-bb(j1)*sq2i
#else
                   z(matind(n,1),k) = z(matind(n,1),k)+bb(j1) *CMPLX(sq2i,0.0)
                   z(matind(n,2),k) = z(matind(n,2),k)-bb(j1) *CMPLX(sq2i,0.0)
#endif
                ELSE
#ifdef CPP_INVERSION
                   z(matind(n,1),k) = 0.0
#else
                   z(matind(n,1),k) = CMPLX(0.0,0.0)
#endif
                ENDIF
             ENDDO
             j=j-1
          ENDIF
       ENDDO
       DEALLOCATE ( aa,bb )

    ENDIF
    !-gu
  END SUBROUTINE franza
END