eig66_da.F90 14.7 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
MODULE m_eig66_da
#include "juDFT_env.h"
9 10 11 12 13 14 15 16 17 18
  ! Do the IO of the eig-file in fortran direct-access
  ! The eig-file is split into two parts:
  ! eig.bas contains the basis-set information
  ! eig.vec contains the eigenvalues and the eigenvectors
  ! The record number is given by nrec=nk+(jspin-1)*nkpts
  ! each record contains:
  ! eig.bas: el,evac,ello,bkpt,wtkpt,nv,nmat,k1,k2,k3,kveclo
  ! eig.vec: ne,eig,z**
  !**: real or complex depending on calculation type
  USE m_eig66_data
19
  USE m_types
20
  IMPLICIT NONE
21 22

CONTAINS
23 24 25 26 27 28 29 30 31 32 33 34 35 36
  SUBROUTINE priv_find_data(id,d)
    INTEGER,INTENT(IN)            :: id
    TYPE(t_data_DA),POINTER,INTENT(out)   :: d

    CLASS(t_data),POINTER   ::dp
    CALL eig66_find_data(dp,id)
    SELECT TYPE(dp)
    TYPE is (t_data_da)
       d=>dp
       CLASS default
       CALL judft_error("BUG: wrong datatype in eig66_da")
    END SELECT
  END SUBROUTINE priv_find_data

37
  SUBROUTINE open_eig(id,nmat,neig,nkpts,jspins,lmax,nlo,ntype,nlotot,create,l_real,l_soc,l_dos,l_mcd,l_orb,filename,layers,nstars,ncored,nsld,nat)
38
    INTEGER, INTENT(IN) :: id,nmat,neig,nkpts,jspins,nlo,ntype,lmax,nlotot
39
    LOGICAL, INTENT(IN) :: create,l_real,l_soc
40
    LOGICAL,INTENT(IN),OPTIONAL ::  l_dos,l_mcd,l_orb
41 42 43 44 45 46 47 48 49 50 51 52
    CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: filename
    INTEGER,INTENT(IN),OPTIONAL :: layers,nstars,ncored,nsld,nat
    !locals
    LOGICAL :: l_file
    INTEGER :: i1,recl_z,recl_eig,recl_dos
    REAL    :: r1,r3(3)
    COMPLEX :: c1
    TYPE(t_data_DA),POINTER:: d

    CALL priv_find_data(id,d)

    IF (PRESENT(filename)) d%fname=filename
53
    CALL eig66_data_storedefault(d,jspins,nkpts,nmat,neig,lmax,nlotot,nlo,ntype,l_real,l_soc,l_dos,l_mcd,l_orb)
54 55 56 57 58 59 60 61

    !Allocate the storage for the DATA always read/write
    ALLOCATE(d%el_s(0:lmax,ntype),d%ello_s(nlo,ntype),d%evac_s(2))
    ALLOCATE(d%kvec_s(nmat,3),d%kveclo_s(nlotot))
    !Calculate the record length
    INQUIRE(IOLENGTH=recl_eig) d%el_s,d%evac_s,d%ello_s,r3,r1,i1,i1,d%kvec_s,d%kveclo_s
    d%recl_bas=recl_eig
    INQUIRE(IOLENGTH=recl_eig) r1
Daniel Wortmann's avatar
Daniel Wortmann committed
62 63
    print *,lmax,ntype,nlo,nlotot,nmat,neig
    
64
    recl_eig=recl_eig*(neig+2) ! add a 2 for integer 'neig'
65 66 67 68 69
    if (l_real.and..not.l_soc ) THEN
       INQUIRE(IOLENGTH=recl_z) r1
    else
       INQUIRE(IOLENGTH=recl_z) c1
    endif
70 71 72
    recl_z=recl_z*nmat*neig

    d%recl_vec=recl_eig+recl_z
Daniel Wortmann's avatar
Daniel Wortmann committed
73 74
    print *,l_real,l_soc
    print *,"reclen:",d%recl_vec,nmat,neig,recl_z,recl_eig
75

76
    IF (d%l_dos) THEN
77 78 79 80 81 82
       IF (.NOT.(PRESENT(layers).AND.PRESENT(nstars).AND.PRESENT(ncored).AND.PRESENT(nsld).AND.PRESENT(nat))) &
            CALL judft_error("BUG:Could not open file for DOS-data",calledby="eig66_da")
       INQUIRE(IOLENGTH=i1) i1
       recl_dos=i1*2*neig !ksym&jsym
       INQUIRE(IOLENGTH=i1) r1
       recl_dos=recl_dos+i1*3*neig !qvac&qis
83 84
       recl_dos=recl_dos+i1*4*ntype*neig !qal
       recl_dos=recl_dos+i1*neig*2*max(1,layers) !qvlay
85 86 87 88 89
       IF (l_orb) THEN
          recl_dos=recl_dos+i1*2*nsld*neig !qintsl,qmtsl
          recl_dos=recl_dos+i1*24*neig*nat !qmtp,orbcomp
       ENDIF
       INQUIRE(IOLENGTH=i1) c1
90
       recl_dos=recl_dos+i1*nstars*neig*max(1,layers)*2 !qstars
91 92 93 94 95 96 97 98 99 100 101
       IF (l_mcd) recl_dos=recl_dos+i1*3*ntype*ncored*neig !mcd
    ELSE
       recl_dos=-1
    ENDIF
    d%recl_dos=recl_dos


    IF (create) THEN
       d%file_io_id_bas=priv_free_uid()
       INQUIRE(file=TRIM(d%fname)//".bas",opened=l_file)
       DO WHILE(l_file)
Daniel Wortmann's avatar
Daniel Wortmann committed
102
          write(*,*) "eig66_open_da:",d%fname," in use"
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
          d%fname=TRIM(d%fname)//"6"
          INQUIRE(file=TRIM(d%fname)//".bas",opened=l_file)
       ENDDO
       OPEN(d%file_io_id_bas,FILE=TRIM(d%fname)//".bas",ACCESS='direct',FORM='unformatted',RECL=d%recl_bas,STATUS='unknown')
       d%file_io_id_vec=priv_free_uid()
       OPEN(d%file_io_id_vec,FILE=TRIM(d%fname)//".vec",ACCESS='direct',FORM='unformatted',RECL=d%recl_vec,STATUS='unknown')
       IF(d%recl_dos>0) THEN
          d%file_io_id_dos=priv_free_uid()
          OPEN(d%file_io_id_dos,FILE=TRIM(d%fname)//".dos",ACCESS='direct',FORM='unformatted',RECL=d%recl_dos,STATUS='unknown')
       ENDIF

    ELSE
       d%file_io_id_bas=priv_free_uid()
       OPEN(d%file_io_id_bas,FILE=TRIM(d%fname)//".bas",ACCESS='direct',FORM='unformatted',RECL=d%recl_bas,STATUS='old')
       d%file_io_id_vec=priv_free_uid()
       OPEN(d%file_io_id_vec,FILE=TRIM(d%fname)//".vec",ACCESS='direct',FORM='unformatted',RECL=d%recl_vec,STATUS='old')
       IF(d%recl_dos>0) THEN
          d%file_io_id_dos=priv_free_uid()
          OPEN(d%file_io_id_dos,FILE=TRIM(d%fname)//".dos",ACCESS='direct',FORM='unformatted',RECL=d%recl_dos,STATUS='old')
       ENDIF
    ENDIF
  CONTAINS
    INTEGER FUNCTION priv_free_uid() RESULT(uid)
      IMPLICIT NONE
      LOGICAL::used
      used=.TRUE.
      uid=665
      DO WHILE(used)
         uid=uid+1
         INQUIRE(UNIT=uid,OPENED=used)
      END DO
    END FUNCTION priv_free_uid
  END SUBROUTINE open_eig
  SUBROUTINE close_eig(id,filename)
    INTEGER,INTENT(IN)::id
    CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: filename
    TYPE(t_data_DA),POINTER:: d

    CALL priv_find_data(id,d)

    DEALLOCATE(d%el_s,d%ello_s,d%evac_s,d%kvec_s,d%kveclo_s)
    CLOSE(d%file_io_id_bas)
    CLOSE(d%file_io_id_vec)
    d%recl_vec=0
    d%recl_bas=0

    !If a filename was given and the name is not the current filename then rename
    IF (PRESENT(filename)) THEN
       IF (filename.NE.d%fname) THEN
          CALL system("mv "//TRIM(d%fname)//".bas "//TRIM(filename)//".bas")
          CALL system("mv "//TRIM(d%fname)//".vec "//TRIM(filename)//".vec")
       ENDIF
    ENDIF
    d%fname="eig"
    CALL eig66_remove_data(id)
  END SUBROUTINE close_eig
159
  SUBROUTINE read_eig(id,nk,jspin,nv,nmat,k1,k2,k3,bk,wk,neig,eig,el,ello,evac,kveclo,n_start,n_end,zmat)
160 161 162 163 164 165 166 167 168
    IMPLICIT NONE
    INTEGER, INTENT(IN)            :: id,nk,jspin
    INTEGER, INTENT(OUT),OPTIONAL  :: nv,nmat
    INTEGER, INTENT(OUT),OPTIONAL  :: neig
    REAL,    INTENT(OUT),OPTIONAL  :: eig(:)
    INTEGER, INTENT(OUT),OPTIONAL  :: k1(:),k2(:),k3(:),kveclo(:)
    REAL,    INTENT(OUT),OPTIONAL  :: evac(:),ello(:,:),el(:,:)
    REAL,    INTENT(OUT),OPTIONAL  :: bk(:),wk
    INTEGER, INTENT(IN),OPTIONAL   :: n_start,n_end
169
    TYPE(t_zmat),OPTIONAL  :: zmat
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

    !Local variables
    INTEGER:: nv_s,nmat_s,n,nrec,neig_s
    REAL   :: bkpt(3),wtkpt
    REAL,ALLOCATABLE::eig_s(:),zr_s(:,:)
    COMPLEX,ALLOCATABLE::zc_s(:,:)
    TYPE(t_data_DA),POINTER:: d



    CALL priv_find_data(id,d)
    ! check if io is performed correctly
    IF (PRESENT(n_start)) THEN
       IF (n_start/=1) &
            CALL juDFT_error("In direct access mode only all eigenstates can be read")
    ENDIF

    nrec=nk+(jspin-1)*d%nkpts
    IF (PRESENT(el).OR.PRESENT(ello).OR.PRESENT(evac).OR.PRESENT(bk).OR.PRESENT(wk).OR.&
         PRESENT(nv).OR.PRESENT(nmat).OR.PRESENT(k1).OR.PRESENT(k2).OR.PRESENT(k3).OR.&
         PRESENT(kveclo)) THEN
       !IO of basis-set information
       READ(d%file_io_id_bas,REC=nrec) nmat_s,d%el_s,d%evac_s,d%ello_s,bkpt,wtkpt,nv_s,d%kvec_s,d%kveclo_s
       IF (PRESENT(el)) el=d%el_s
       IF (PRESENT(evac)) evac=d%evac_s
       IF (PRESENT(ello)) ello=d%ello_s
       IF (PRESENT(bk)) bk=bkpt
       IF (PRESENT(wk)) wk=wtkpt
       IF (PRESENT(nv)) nv=nv_s
       IF (PRESENT(nmat)) nmat=nmat_s
Daniel Wortmann's avatar
Daniel Wortmann committed
200 201 202
       IF (PRESENT(k1)) k1=d%kvec_s(:size(k1),1)
       IF (PRESENT(k2)) k2=d%kvec_s(:size(k1),2)
       IF (PRESENT(k3)) k3=d%kvec_s(:size(k1),3)
203 204 205
       IF (PRESENT(kveclo)) kveclo=d%kveclo_s
    ENDIF

206
    IF (.NOT.(PRESENT(eig).OR.PRESENT(neig).OR.PRESENT(zmat))) RETURN
207
    READ(d%file_io_id_vec,REC=nrec) neig_s
208
    IF (PRESENT(neig)) neig=neig_s
209
    IF (.NOT.(PRESENT(eig).OR.PRESENT(zmat))) RETURN
210
    ALLOCATE(eig_s(neig_s))
211 212 213
    IF (PRESENT(zmat)) THEN
       IF (zmat%l_real) THEN
          INQUIRE(IOLENGTH=n) neig_s,eig_s,REAL(zmat%z_r)
Daniel Wortmann's avatar
Daniel Wortmann committed
214 215 216 217 218 219
          IF (n>d%recl_vec) THEN
             print *,n,d%recl_vec
             print *,size(eig_s)
             print *,size(zmat%z_r)
             CALL juDFT_error("BUG: Too long record")
          END IF
220 221 222
          READ(d%file_io_id_vec,REC=nrec) neig_s,eig_s,zmat%z_r
       ELSE
          INQUIRE(IOLENGTH=n) neig_s,eig_s,CMPLX(zmat%z_c)
Daniel Wortmann's avatar
Daniel Wortmann committed
223 224 225 226 227 228
          IF (n>d%recl_vec) THEN
             print *,n,d%recl_vec
             print *,size(eig_s)
             print *,size(zmat%z_c)
             CALL juDFT_error("BUG: Too long record")
          END IF
229 230
          READ(d%file_io_id_vec,REC=nrec) neig_s,eig_s,zmat%z_c
       ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
231
       print *,"R:",nrec,nk,neig_s
232 233 234 235 236
    ELSE
       INQUIRE(IOLENGTH=n) neig_s,eig_s
       IF (n>d%recl_vec) CALL juDFT_error("BUG: Too long record")
       READ(d%file_io_id_vec,REC=nrec) neig_s,eig_s
    ENDIF
237
    IF (PRESENT(eig)) eig(:min(size(eig),neig_s))=eig_s(:min(size(eig),neig_s))
238 239 240 241

  END SUBROUTINE read_eig

  SUBROUTINE write_eig(id,nk,jspin,neig,neig_total,nv,nmat,k1,k2,k3,bk,wk, &
242
       eig,el,ello,evac,nlotot,kveclo,n_size,n_rank,zmat)
243 244 245 246 247 248 249
    INTEGER, INTENT(IN)          :: id,nk,jspin
    INTEGER, INTENT(IN),OPTIONAL :: n_size,n_rank
    REAL,    INTENT(IN),OPTIONAL :: wk
    INTEGER, INTENT(IN),OPTIONAL :: neig,nv,nmat,nlotot,neig_total
    INTEGER, INTENT(IN),OPTIONAL :: k1(:),k2(:),k3(:),kveclo(:)
    REAL,    INTENT(IN),OPTIONAL :: bk(3),eig(:),el(:,:)
    REAL,    INTENT(IN),OPTIONAL :: evac(:),ello(:,:)
250
    TYPE(t_zmat),INTENT(IN),OPTIONAL :: zmat
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

    INTEGER:: nrec,r_len
    INTEGER:: nv_s,nmat_s
    REAL   :: bkpt(3),wtkpt
    TYPE(t_data_DA),POINTER:: d

    CALL priv_find_data(id,d)
    !This mode requires all data to be written at once!!

    IF (PRESENT(n_size).AND.PRESENT(n_rank)) THEN
       IF (n_size/=1.OR.n_rank/=0) &
            CALL juDFT_error("Direct Access IO not possible in eigenvalue parallel code")
    ENDIF
    !check record length
    !INQUIRE(iolength=r_len) nmat,el,evac,ello,bk,wk,nv,d%kvec_s,kveclo
    !if (r_len>recl_bas) call juDFT_error("BUG: too long record")

    !Now it is time for the IO :-)
    nrec=nk+(jspin-1)*d%nkpts
    IF (PRESENT(nmat).AND..NOT.PRESENT(el)) THEN
       !IO of basis-set information
       READ(d%file_io_id_bas,REC=nrec,ERR=88) nmat_s,d%el_s,d%evac_s,d%ello_s,bkpt,wtkpt,nv_s,d%kvec_s,d%kveclo_s
88     WRITE(d%file_io_id_bas,REC=nrec)       nmat  ,d%el_s,d%evac_s,d%ello_s,bkpt,wtkpt,nv_s,d%kvec_s,d%kveclo_s
       IF (PRESENT(wk).OR.PRESENT(nv).OR.PRESENT(nlotot) &
            .OR.PRESENT(k1).OR.PRESENT(k2).OR.PRESENT(k3).OR.PRESENT(kveclo).OR.&
            PRESENT(bk).OR.PRESENT(ello).OR.PRESENT(evac)) THEN
          CALL juDFT_error("BUG:Direct access IO of eig-file only with all scalar data")
       ENDIF
    ELSE
       IF (.NOT.(PRESENT(wk).AND.PRESENT(nv).AND.PRESENT(nmat).AND.PRESENT(nlotot) &
            .AND.PRESENT(k1).AND.PRESENT(k2).AND.PRESENT(k3).AND.PRESENT(kveclo).AND.&
            PRESENT(bk).AND.PRESENT(el).AND.PRESENT(ello).AND.PRESENT(evac))) THEN
          CALL juDFT_error("BUG:Direct access IO of eig-file only with all data")
       ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
285 286 287
       d%kvec_s(:size(k1),1)=k1
       d%kvec_s(:size(k1),2)=k2
       d%kvec_s(:size(k1),3)=k3
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
       IF ((SIZE(el).NE.SIZE(d%el_s)).OR.(SIZE(ello).NE.SIZE(d%ello_s).OR.(SIZE(evac).NE.SIZE(d%evac_s)))) THEN
          WRITE(*,*) SHAPE(el),SHAPE(d%el_s)
          WRITE(*,*) SHAPE(ello),SHAPE(d%ello_s)
          WRITE(*,*) SHAPE(evac),SHAPE(d%evac_s)
          CALL juDFT_error("Mismatch of sizes")
       ENDIF
       WRITE(d%file_io_id_bas,REC=nrec) nmat,el,evac,ello,bk,wk,nv,d%kvec_s,kveclo

    ENDIF
    IF (PRESENT(neig).AND.PRESENT(neig_total)) THEN
       IF (neig.NE.neig_total) THEN
          CALL juDFT_error("Neig and neig_total have to be equal in DA mode",calledby="eig66_da")
       ENDIF
    ENDIF
    IF (.NOT.PRESENT(eig).OR..NOT.PRESENT(neig)) RETURN
    !Now the IO of the eigenvalues/vectors
304 305 306
    IF (PRESENT(zmat)) THEN
       IF (zmat%l_real) THEN
          INQUIRE(IOLENGTH=r_len) neig,eig,REAL(zmat%z_r)
307
          IF (r_len>d%recl_vec) CALL juDFT_error("BUG: too long record")
308 309 310
          WRITE(d%file_io_id_vec,REC=nrec) neig,eig,REAL(zmat%z_r)
       ELSE
          INQUIRE(IOLENGTH=r_len) neig,eig(:neig),CMPLX(zmat%z_c)
311
          IF (r_len>d%recl_vec) CALL juDFT_error("BUG: too long record")
312 313
          WRITE(d%file_io_id_vec,REC=nrec) neig,eig(:neig),CMPLX(zmat%z_c)
       ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
314
       print *,"W:",nrec,nk,neig
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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    ELSE
       INQUIRE(IOLENGTH=r_len) neig,eig
       IF (r_len>d%recl_vec) CALL juDFT_error("BUG: too long record")
       WRITE(d%file_io_id_vec,REC=nrec) neig,eig
    ENDIF

  END SUBROUTINE write_eig


  SUBROUTINE write_dos(id,nk,jspin,qal,qvac,qis,qvlay,qstars,ksym,jsym,mcd,qintsl,qmtsl,qmtp,orbcomp)
    IMPLICIT NONE
    INTEGER, INTENT(IN)          :: id,nk,jspin
    REAL,INTENT(IN)              :: qal(:,:,:),qvac(:,:),qis(:),qvlay(:,:,:)
    COMPLEX,INTENT(IN)           :: qstars(:,:,:,:)
    INTEGER,INTENT(IN)           :: ksym(:),jsym(:)
    REAL,INTENT(IN),OPTIONAL  :: mcd(:,:,:)
    REAL,INTENT(IN),OPTIONAL     :: qintsl(:,:),qmtsl(:,:),qmtp(:,:),orbcomp(:,:,:)
    TYPE(t_data_DA),POINTER:: d
    INTEGER:: nrec
    CALL priv_find_data(id,d)

    nrec=nk+(jspin-1)*d%nkpts

    IF (d%l_orb.AND.PRESENT(qmtsl)) THEN
       IF (d%l_mcd) CPP_error("mcd & orbital decomposition not implemented in IO")
       WRITE(d%file_io_id_dos,REC=nrec) qal,qvac,qis,qvlay,qstars,ksym,jsym,qintsl,qmtsl,qmtp,orbcomp
    ELSEIF(d%l_mcd.AND.PRESENT(mcd)) THEN
       WRITE(d%file_io_id_dos,REC=nrec) qal,qvac,qis,qvlay,qstars,ksym,jsym,mcd
    ELSE
       WRITE(d%file_io_id_dos,REC=nrec) qal,qvac,qis,qvlay,qstars,ksym,jsym
    END IF
  END SUBROUTINE write_dos

  SUBROUTINE read_dos(id,nk,jspin,qal,qvac,qis,qvlay,qstars,ksym,jsym,mcd,qintsl,qmtsl,qmtp,orbcomp)
    IMPLICIT NONE
    INTEGER, INTENT(IN)          :: id,nk,jspin
    REAL,INTENT(OUT)              :: qal(:,:,:),qvac(:,:),qis(:),qvlay(:,:,:)
    COMPLEX,INTENT(OUT)           :: qstars(:,:,:,:)
    INTEGER,INTENT(OUT)           :: ksym(:),jsym(:)
    REAL,INTENT(OUT),OPTIONAL  :: mcd(:,:,:)
    REAL,INTENT(OUT),OPTIONAL     :: qintsl(:,:),qmtsl(:,:),qmtp(:,:),orbcomp(:,:,:)
    TYPE(t_data_DA),POINTER:: d
    INTEGER:: nrec
    CALL priv_find_data(id,d)

    nrec=nk+(jspin-1)*d%nkpts

    IF (d%l_orb.AND.PRESENT(qmtsl)) THEN
       IF (d%l_mcd) CPP_error("mcd & orbital decomposition not implemented in IO")
       READ(d%file_io_id_dos,REC=nrec) qal,qvac,qis,qvlay,qstars,ksym,jsym,qintsl,qmtsl,qmtp,orbcomp
    ELSEIF(d%l_mcd.AND.PRESENT(mcd)) THEN
       READ(d%file_io_id_dos,REC=nrec) qal,qvac,qis,qvlay,qstars,ksym,jsym,mcd
    ELSE
       READ(d%file_io_id_dos,REC=nrec) qal,qvac,qis,qvlay,qstars,ksym,jsym
    END IF
  END SUBROUTINE read_dos


373
END MODULE m_eig66_da