writeBasis.F90 28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
!--------------------------------------------------------------------------------
! Copyright (c) 2018 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.
!--------------------------------------------------------------------------------

MODULE m_writeBasis

CONTAINS

11
SUBROUTINE writeBasis(input,noco,kpts,atoms,sym,cell,enpara,vTot,vCoul,vx,mpi,DIMENSION,results,eig_id,oneD,sphhar,stars,vacuum)
12 13 14

   USE m_types
   USE m_juDFT
Daniel Wortmann's avatar
Daniel Wortmann committed
15
#ifdef CPP_HDF   
16 17
   USE hdf5
   USE m_hdf_tools
Daniel Wortmann's avatar
Daniel Wortmann committed
18
#endif   
19
   USE m_genmtbasis
20
   USE m_pot_io
21
   USE m_abcof
22
   USE m_abcrot
23
   USE m_eig66_io, ONLY : read_eig
24 25

   IMPLICIT NONE
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
!     TYPE(t_results),INTENT(IN)    :: results
      TYPE(t_dimension),INTENT(IN)  :: DIMENSION
      TYPE(t_enpara),INTENT(IN)     :: enpara
!     TYPE(t_banddos),INTENT(IN)    :: banddos

      TYPE(t_sphhar),INTENT(IN)     :: sphhar
      TYPE(t_stars),INTENT(IN)      :: stars
      TYPE(t_vacuum),INTENT(IN)     :: vacuum

      TYPE(t_input),INTENT(IN)      :: input
      TYPE(t_noco),INTENT(IN)       :: noco
      TYPE(t_kpts),INTENT(IN)       :: kpts
      TYPE(t_atoms),INTENT(INOUT)   :: atoms
      TYPE(t_sym),INTENT(IN)        :: sym
      TYPE(t_cell),INTENT(IN)       :: cell
Gregor Michalicek's avatar
Gregor Michalicek committed
41 42 43
      TYPE(t_potden), INTENT(INOUT) :: vTot
      TYPE(t_potden), INTENT(INOUT) :: vCoul
      TYPE(t_potden), INTENT(INOUT) :: vx
44 45 46 47 48 49 50 51 52 53
      TYPE(t_mpi), INTENT(IN)       :: mpi
      TYPE(t_results), INTENT(INOUT):: results
      INTEGER, INTENT(IN)           :: eig_id
      TYPE(t_oneD), INTENT(IN)      :: oneD

      TYPE (t_usdus)                :: usdus
      TYPE(t_lapw)                  :: lapw
      TYPE (t_eigVecCoeffs)         :: eigVecCoeffs
      TYPE (t_force)                :: force
      TYPE(t_mat)                   :: zMat
54

55 56
#ifdef CPP_HDF   
      
57 58
      LOGICAL           :: l_exist
      CHARACTER(LEN=30) :: filename
S.Rost's avatar
S.Rost committed
59
      CHARACTER(LEN=50) :: kpt_name
60 61
      CHARACTER(LEN=30) :: jsp_name
      CHARACTER(LEN=30) :: itype_name
62
!     CHARACTER(LEN=30) :: l_name
63 64 65

   
      INTEGER(HID_T)    :: fileID
66 67 68 69 70 71 72 73
      INTEGER(HID_T)    :: metaGroupID
      INTEGER(HID_T)    :: generalGroupID
      INTEGER(HID_T)    :: cellGroupID
      INTEGER(HID_T)    :: atomsGroupID
      INTEGER(HID_T)    :: kptsGroupID
      INTEGER(HID_T)    :: kptGroupID
      INTEGER(HID_T)    :: jspGroupID
      INTEGER(HID_T)    :: itypeGroupID,  itypeSpaceID, itypeSetID
74
!     INTEGER(HID_T)    :: lGroupID
75

76 77
!     INTEGER(HID_T)    :: stringTypeID
!     INTEGER(SIZE_T)   :: stringLength
78 79 80 81 82 83

      INTEGER(HID_T)    :: bravaisMatrixSpaceID, bravaisMatrixSetID
      INTEGER(HID_T)    :: reciprocalCellSpaceID, reciprocalCellSetID

      INTEGER(HID_T)    :: atomPosSpaceID, atomPosSetID
      INTEGER(HID_T)    :: gvecSpaceID, gvecSetID
84 85
      INTEGER(HID_T)    :: eigSpaceID, eigSetID
      INTEGER(HID_T)    :: zmatSpaceID, zmatSetID
86 87 88 89 90
      INTEGER(HID_T)    :: atomicNumbersSpaceID, atomicNumbersSetID
      INTEGER(HID_T)    :: equivAtomsClassSpaceID, equivAtomsClassSetID

      INTEGER(HID_T)    :: kptCoordSpaceID, kptCoordSetID
      INTEGER(HID_T)    :: kptWeightSpaceID, kptWeightSetID
91 92
!     INTEGER(HID_T)    :: kptSPLabelsSpaceID, kptSPLabelsSetID
!     INTEGER(HID_T)    :: kptsSPIndicesSpaceID, kptsSPIndicesSetID
93
      INTEGER(HSIZE_T)  :: dims(7)
94

95
      INTEGER           :: j, iAtom, i
96
!     INTEGER           :: noded, nodeu
97 98 99

      INTEGER           :: hdfError, dimsInt(7)
      INTEGER           :: version
100 101 102 103
      REAL, ALLOCATABLE :: output(:,:,:,:),output5(:,:,:,:,:),output3(:,:,:)
!     INTEGER           :: fakeLogical
!     REAL              :: eFermiPrev
!     LOGICAL           :: l_error
104

105
      INTEGER           :: atomicNumbers(atoms%nat),ngopr_temp(atoms%nat)
106 107 108 109
      INTEGER           :: equivAtomsGroup(atoms%nat)



110 111
      REAL, ALLOCATABLE :: f(:,:,:,:),g(:,:,:,:)
      REAL, ALLOCATABLE :: flo(:,:,:)
112

113 114 115 116
      real, allocatable :: cof(:,:,:,:)
      integer(HSIZE_T)  :: Hdim1(4)
      INTEGER           :: lmn, na,lm,n,nn, m
      complex,parameter :: img=(0.,1.)
117
      REAL              :: bk(3)
118

119
      LOGICAL l_zref,l_real, link_exists
120
      INTEGER jsp,nk,l,itype
121
      INTEGER numbands, nbasfcn, ndbands !ndbands number of bands without highest (degenerate)
122

123 124
    !WRITE(5000,*) 'writeBasis entry'

125 126
    CALL force%init1(input,atoms)

127 128 129 130 131 132 133 134
    IF (noco%l_mperp) THEN
       ALLOCATE ( f(atoms%jmtd,2,0:atoms%lmaxd,input%jspins),g(atoms%jmtd,2,0:atoms%lmaxd,input%jspins) )
    ELSE
       ALLOCATE ( f(atoms%jmtd,2,0:atoms%lmaxd,1:input%jspins) )
       ALLOCATE ( g(atoms%jmtd,2,0:atoms%lmaxd,1:input%jspins) )
    ENDIF
    ALLOCATE (flo(atoms%jmtd,2,atoms%nlod))

135

Stefan Rost's avatar
Stefan Rost committed
136 137 138 139 140 141 142
    !-------------------------write potential--------------------
    IF(input%gw==1) THEN
       CALL writePotential(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,POT_ARCHIVE_TYPE_TOT_const,vTot%iter,vTot,vTot%pw_w)
       CALL writePotential(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,POT_ARCHIVE_TYPE_COUL_const,vCoul%iter,vCoul,vCoul%pw_w)
       CALL writePotential(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,POT_ARCHIVE_TYPE_X_const,vx%iter,vx,vx%pw_w)
    END IF

143

144 145 146 147
      l_real=sym%invs.AND..NOT.noco%l_noco
!     check if z-reflection trick can be used
      l_zref=(sym%zrfs.AND.(SUM(ABS(kpts%bk(3,:kpts%nkpt))).LT.1e-9).AND..NOT.noco%l_noco)
!     IF (mpi%n_size > 1) l_zref = .FALSE.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
      version = 1
      filename = 'basis.hdf'

      INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
      IF(l_exist) THEN
         CALL system('rm '//TRIM(ADJUSTL(filename)))       
      END IF

      CALL h5fcreate_f(TRIM(ADJUSTL(filename)), H5F_ACC_TRUNC_F, fileID, hdfError, H5P_DEFAULT_F, H5P_DEFAULT_F)

      CALL h5gcreate_f(fileID, '/meta', metaGroupID, hdfError)
      CALL io_write_attint0(metaGroupID,'version',version)
      CALL h5gclose_f(metaGroupID, hdfError)

      CALL h5gcreate_f(fileID, '/general', generalGroupID, hdfError)
S.Rost's avatar
S.Rost committed
163 164 165 166 167 168
      CALL io_write_attint0(generalGroupID,'jspins',input%jspins)
      CALL io_write_attlog0(generalGroupID,'invs',sym%invs)
      CALL io_write_attlog0(generalGroupID,'l_soc',noco%l_soc)
      CALL io_write_attlog0(generalGroupID,'l_real',l_real)
      CALL io_write_attreal0(generalGroupID,'rkmax',input%rkmax)
      CALL h5gclose_f(generalGroupID, hdfError)      
169 170 171 172 173 174

      CALL h5gcreate_f(fileID, '/cell', cellGroupID, hdfError)

      dims(:2)=(/3,3/)
      dimsInt=dims
      CALL h5screate_simple_f(2,dims(:2),bravaisMatrixSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
175
      CALL h5dcreate_f(cellGroupID, "amat", H5T_NATIVE_DOUBLE, bravaisMatrixSpaceID, bravaisMatrixSetID, hdfError)
176 177 178 179 180 181 182
      CALL h5sclose_f(bravaisMatrixSpaceID,hdfError)
      CALL io_write_real2(bravaisMatrixSetID,(/1,1/),dimsInt(:2),cell%amat)
      CALL h5dclose_f(bravaisMatrixSetID, hdfError)

      dims(:2)=(/3,3/)
      dimsInt=dims
      CALL h5screate_simple_f(2,dims(:2),reciprocalCellSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
183
      CALL h5dcreate_f(cellGroupID, "bmat", H5T_NATIVE_DOUBLE, reciprocalCellSpaceID, reciprocalCellSetID, hdfError)
184 185 186
      CALL h5sclose_f(reciprocalCellSpaceID,hdfError)
      CALL io_write_real2(reciprocalCellSetID,(/1,1/),dimsInt(:2),cell%bmat)
      CALL h5dclose_f(reciprocalCellSetID, hdfError)
S.Rost's avatar
S.Rost committed
187 188 189 190 191 192
      
      CALL io_write_attreal0(cellGroupID,'scaleCell',input%scaleCell)
      CALL io_write_attreal0(cellGroupID,'scaleA1',input%scaleA1)
      CALL io_write_attreal0(cellGroupID,'scaleA2',input%scaleA2)
      CALL io_write_attreal0(cellGroupID,'scaleC',input%scaleC)

193 194

      CALL h5gclose_f(cellGroupID, hdfError)
195
      iAtom = 0
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
      DO iType = 1, atoms%ntype
         DO j = 1, atoms%neq(iType)
            iAtom = iAtom + 1
            atomicNumbers(iAtom) = atoms%nz(iType)
            equivAtomsGroup(iAtom) = iType
         END DO
      END DO

      CALL h5gcreate_f(fileID, '/atoms', atomsGroupID, hdfError)
      CALL io_write_attint0(atomsGroupID,'nAtoms',atoms%nat)
      CALL io_write_attint0(atomsGroupID,'nTypes',atoms%ntype)
      CALL io_write_attint0(atomsGroupID,'nlod',atoms%nlod)
      CALL io_write_attint0(atomsGroupID,'llod',atoms%llod)
      CALL io_write_attint0(atomsGroupID,'nlotot',atoms%nlotot)
      CALL io_write_attint0(atomsGroupID,'lmaxd',atoms%lmaxd)
      CALL io_write_attint0(atomsGroupID,'jmtd',atoms%jmtd)

      dims(:1)=(/atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
216
      CALL h5dcreate_f(atomsGroupID, "jri", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
217 218 219 220 221 222 223
      CALL h5sclose_f(atomicNumbersSpaceID,hdfError)
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atoms%jri)
      CALL h5dclose_f(atomicNumbersSetID, hdfError)

      dims(:1)=(/atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
224
      CALL h5dcreate_f(atomsGroupID, "lmax", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
225 226 227
      CALL h5sclose_f(atomicNumbersSpaceID,hdfError)
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atoms%lmax)
      CALL h5dclose_f(atomicNumbersSetID, hdfError)
S.Rost's avatar
S.Rost committed
228 229 230 231 232 233 234 235
      
      dims(:1)=(/atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
      CALL h5dcreate_f(atomsGroupID, "neq", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
      CALL h5sclose_f(atomicNumbersSpaceID,hdfError)
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atoms%neq)
      CALL h5dclose_f(atomicNumbersSetID, hdfError)
236 237 238 239

      dims(:1)=(/atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
240
      CALL h5dcreate_f(atomsGroupID, "nlo", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
241 242 243 244 245 246 247
      CALL h5sclose_f(atomicNumbersSpaceID,hdfError)
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atoms%nlo)
      CALL h5dclose_f(atomicNumbersSetID, hdfError)

      dims(:2)=(/atoms%nlod,atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(2,dims(:2),atomPosSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
248
      CALL h5dcreate_f(atomsGroupID, "llo", H5T_NATIVE_INTEGER, atomPosSpaceID, atomPosSetID, hdfError)
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      CALL h5sclose_f(atomPosSpaceID,hdfError)
      CALL io_write_integer2(atomPosSetID,(/1,1/),dimsInt(:2),atoms%llo)
      CALL h5dclose_f(atomPosSetID, hdfError)

      dims(:2)=(/atoms%jmtd,atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(2,dims(:2),atomPosSpaceID,hdfError)
      CALL h5dcreate_f(atomsGroupID, "rmsh", H5T_NATIVE_DOUBLE, atomPosSpaceID, atomPosSetID, hdfError)
      CALL h5sclose_f(atomPosSpaceID,hdfError)
      CALL io_write_real2(atomPosSetID,(/1,1/),dimsInt(:2),atoms%rmsh)
      CALL h5dclose_f(atomPosSetID, hdfError)

      dims(:2)=(/3,atoms%nat/)
      dimsInt=dims
      CALL h5screate_simple_f(2,dims(:2),atomPosSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
264
      CALL h5dcreate_f(atomsGroupID, "ratoms", H5T_NATIVE_DOUBLE, atomPosSpaceID, atomPosSetID, hdfError)
265 266 267 268 269 270 271
      CALL h5sclose_f(atomPosSpaceID,hdfError)
      CALL io_write_real2(atomPosSetID,(/1,1/),dimsInt(:2),atoms%taual)
      CALL h5dclose_f(atomPosSetID, hdfError)

      dims(:1)=(/atoms%nat/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
272
      CALL h5dcreate_f(atomsGroupID, "zatoms", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
273 274 275 276
      CALL h5sclose_f(atomicNumbersSpaceID,hdfError)
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atomicNumbers)
      CALL h5dclose_f(atomicNumbersSetID, hdfError)

S.Rost's avatar
S.Rost committed
277 278 279 280 281
      dims(:1)=(/atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
      CALL h5dcreate_f(atomsGroupID, "ztype", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
      CALL h5sclose_f(atomicNumbersSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
282
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atoms%nz)
S.Rost's avatar
S.Rost committed
283 284
      CALL h5dclose_f(atomicNumbersSetID, hdfError)

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
      dims(:1)=(/atoms%nat/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),equivAtomsClassSpaceID,hdfError)
      CALL h5dcreate_f(atomsGroupID, "equivAtomsGroup", H5T_NATIVE_INTEGER, equivAtomsClassSpaceID, equivAtomsClassSetID, hdfError)
      CALL h5sclose_f(equivAtomsClassSpaceID,hdfError)
      CALL io_write_integer1(equivAtomsClassSetID,(/1/),dimsInt(:1),equivAtomsGroup)
      CALL h5dclose_f(equivAtomsClassSetID, hdfError)

      CALL h5gclose_f(atomsGroupID, hdfError)

      CALL h5gcreate_f(fileID, '/kpts', kptsGroupID, hdfError)

      CALL io_write_attint0(kptsGroupID,'nkpt',kpts%nkpt)

      dims(:2)=(/3,kpts%nkpt/)
      dimsInt=dims
      CALL h5screate_simple_f(2,dims(:2),kptCoordSpaceID,hdfError)
      CALL h5dcreate_f(kptsGroupID, "coordinates", H5T_NATIVE_DOUBLE, kptCoordSpaceID, kptCoordSetID, hdfError)
      CALL h5sclose_f(kptCoordSpaceID,hdfError)
      CALL io_write_real2(kptCoordSetID,(/1,1/),dimsInt(:2),kpts%bk)
      CALL h5dclose_f(kptCoordSetID, hdfError)

      dims(:1)=(/kpts%nkpt/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),kptWeightSpaceID,hdfError)
      CALL h5dcreate_f(kptsGroupID, "weights", H5T_NATIVE_DOUBLE, kptWeightSpaceID, kptWeightSetID, hdfError)
      CALL h5sclose_f(kptWeightSpaceID,hdfError)
      CALL io_write_real1(kptWeightSetID,(/1/),dimsInt(:1),kpts%wtkpt)
      CALL h5dclose_f(kptWeightSetID, hdfError)

      CALL h5gclose_f(kptsGroupID, hdfError)

317
      CALL usdus%init(atoms,input%jspins)
318

319 320 321 322 323
      DO jsp = 1,MERGE(1,input%jspins,noco%l_noco)
         write(jsp_name , '(a,i0)') '/jsp_',jsp
         CALL h5gcreate_f(fileID, TRIM(ADJUSTL(jsp_name)), jspGroupID, hdfError)
!        DO nk = mpi%n_start,kpts%nkpt,mpi%n_stride
         DO nk = 1,kpts%nkpt
324
            CALL lapw%init(input,noco,kpts,atoms,sym,nk,cell,l_zref)
325 326 327 328
            bk(:) = kpts%bk(:,nk)
            IF(abs(bk(1)).LT.1e-7) bk(1) = abs(bk(1))
            IF(abs(bk(2)).LT.1e-7) bk(2) = abs(bk(2))
            IF(abs(bk(3)).LT.1e-7) bk(3) = abs(bk(3))
S.Rost's avatar
S.Rost committed
329
            !write(kpt_name , '(2a,i0)') TRIM(ADJUSTL(jsp_name)),'/kpt_',nk
330
            write(kpt_name , '(2a,f12.10,a,f12.10,a,f12.10)') TRIM(ADJUSTL(jsp_name)),'/kpt_',bk(1),',',bk(2),',',bk(3)
Gregor Michalicek's avatar
Gregor Michalicek committed
331
            CALL h5lexists_f(fileID, TRIM(ADJUSTL(kpt_name)), link_exists, hdfError)
332 333
            IF (link_exists) CYCLE
            CALL h5gcreate_f(fileID, TRIM(ADJUSTL(kpt_name)), kptGroupID, hdfError)
334 335 336
!--------------------enter output gvec etc here--------------------
            CALL io_write_attint0(kptGroupID,'nv',lapw%nv(jsp))

337 338 339 340 341 342 343
            dims(:2)=(/3,lapw%nv(jsp)/)
            dimsInt=dims
            CALL h5screate_simple_f(2,dims(:2),gvecSpaceID,hdfError)
            CALL h5dcreate_f(kptGroupID, "gvec", H5T_NATIVE_INTEGER, gvecSpaceID, gvecSetID, hdfError)
            CALL h5sclose_f(gvecSpaceID,hdfError)
            CALL io_write_integer2(gvecSetID,(/1,1/),dimsInt(:2),lapw%gvec(:,:lapw%nv(jsp),jsp))
            CALL h5dclose_f(gvecSetID, hdfError)	
344 345 346
	      
            CALL h5gclose_f(kptGroupID, hdfError)   
         END DO
S.Rost's avatar
S.Rost committed
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
         DO itype = 1,atoms%ntype
	    write(itype_name , '(2a,i0)') TRIM(ADJUSTL(jsp_name)),'/itype_',itype
	    CALL h5gcreate_f(fileID, TRIM(ADJUSTL(itype_name)), itypeGroupID, hdfError)

            CALL genMTBasis(atoms,enpara,vTot,mpi,itype,jsp,usdus,f(:,:,0:,jsp),g(:,:,0:,jsp),flo)
	    dims(:3)=(/atoms%jmtd,2,atoms%lmaxd+1/)
	    dimsInt = dims
	    CALL h5screate_simple_f(3,dims(:3),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "f", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real3(itypeSetID,(/1,1,1/),dimsInt(:3),f(:,:,0:,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    CALL h5screate_simple_f(3,dims(:3),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "g", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real3(itypeSetID,(/1,1,1/),dimsInt(:3),g(:,:,0:,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    dims(:3)=(/atoms%jmtd,2,atoms%nlod/)
	    dimsInt = dims
	    CALL h5screate_simple_f(3,dims(:3),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "flo", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real3(itypeSetID,(/1,1,1/),dimsInt(:3),flo(:,:,:))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    dims(:1)=(/atoms%lmaxd+1/)
	    dimsInt = dims
	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "us", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),usdus%us(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "dus", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),usdus%dus(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "uds", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),usdus%uds(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "duds", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),usdus%duds(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    dims(:1)=(/atoms%nlod/)
	    dimsInt = dims
	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "dulos", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),usdus%dulos(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "ulos", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),usdus%ulos(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    dims(:1)=(/atoms%lmaxd+1/)
	    dimsInt = dims
	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "el0", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),enpara%el0(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)

	    dims(:1)=(/atoms%nlod/)
	    dimsInt = dims
	    CALL h5screate_simple_f(1,dims(:1),itypeSpaceID,hdfError)
	    CALL h5dcreate_f(itypeGroupID, "ello0", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
	    CALL h5sclose_f(itypeSpaceID,hdfError)
	    CALL io_write_real1(itypeSetID,(/1/),dimsInt(:1),enpara%ello0(:,itype,jsp))
	    CALL h5dclose_f(itypeSetID, hdfError)


!           write(kpt_name , '(3a,i0)') TRIM(ADJUSTL(jsp_name)),TRIM(ADJUSTL(itype_name)),'/l_',l
!	    CALL h5gcreate_f(fileID, TRIM(ADJUSTL(l_name)), lGroupID, hdfError)
!	    CALL h5gclose_f(lGroupID, hdfError)
	    CALL h5gclose_f(itypeGroupID, hdfError)
436 437 438
	END DO

       CALL h5gclose_f(jspGroupID, hdfError)
439 440 441 442
    END DO
    CALL h5fclose_f(fileID, hdfError)
    version = 1
    filename = 'eig_gw.hdf'
443

Stefan Rost's avatar
Stefan Rost committed
444

445
    IF(input%gw==2) THEN
446 447 448 449
    INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
    IF(l_exist) THEN
       CALL system('rm '//TRIM(ADJUSTL(filename)))       
    END IF
450 451 452 453 454 455 456 457 458 459 460 461 462

      CALL h5fcreate_f(TRIM(ADJUSTL(filename)), H5F_ACC_TRUNC_F, fileID, hdfError, H5P_DEFAULT_F, H5P_DEFAULT_F)

      CALL h5gcreate_f(fileID, '/meta', metaGroupID, hdfError)
      CALL io_write_attint0(metaGroupID,'version',version)
      CALL h5gclose_f(metaGroupID, hdfError)

   DO jsp = 1,MERGE(1,input%jspins,noco%l_noco)
       write(jsp_name , '(a,i0)') '/jsp_',jsp
       CALL h5gcreate_f(fileID, TRIM(ADJUSTL(jsp_name)), jspGroupID, hdfError)
!      DO nk = mpi%n_start,kpts%nkpt,mpi%n_stride
       DO nk = 1,kpts%nkpt
            CALL lapw%init(input,noco,kpts,atoms,sym,nk,cell,l_zref)
463 464 465 466
            bk(:) = kpts%bk(:,nk)
            IF(abs(bk(1)).LT.1e-7) bk(1) = abs(bk(1))
            IF(abs(bk(2)).LT.1e-7) bk(2) = abs(bk(2))
            IF(abs(bk(3)).LT.1e-7) bk(3) = abs(bk(3))
467
            !write(kpt_name , '(2a,i0)') TRIM(ADJUSTL(jsp_name)),'/kpt_',nk
468 469
            write(kpt_name , '(2a,f12.10,a,f12.10,a,f12.10)') TRIM(ADJUSTL(jsp_name)),'/kpt_',bk(1),',',bk(2),',',bk(3)
            CALL h5lexists_f(fileID, TRIM(ADJUSTL(kpt_name)), link_exists, hdfError)
470
            IF (link_exists) CYCLE
471
            CALL h5gcreate_f(fileID, TRIM(ADJUSTL(kpt_name)), kptGroupID, hdfError)
472
!--------------------abcoff, zmat, eig output here-------------------
473 474 475 476
!,results%neig(nk,jsp),results%eig(:,nk,jsp)
            numbands=results%neig(nk,jsp)
            nbasfcn = MERGE(lapw%nv(1)+lapw%nv(2)+2*atoms%nlotot,lapw%nv(1)+atoms%nlotot,noco%l_noco)
            CALL zMat%init(l_real,nbasfcn,numbands)
477
            CALL read_eig(eig_id,nk,jsp,zmat=zMat)
478 479
	    CALL eigVecCoeffs%init(input,DIMENSION,atoms,noco,jsp,numbands)
            IF (input%l_f) CALL force%init2(numbands,input,atoms)
480 481 482 483
!            DO i=1,atoms%nat
!	    	ngopr_temp(i)=atoms%ngopr(i)
!               atoms%ngopr(i)=1
!            END DO
484 485
		CALL abcof(input,atoms,sym,cell,lapw,numbands,usdus,noco,jsp,oneD,&
		    eigVecCoeffs%acof(:,0:,:,jsp),eigVecCoeffs%bcof(:,0:,:,jsp),&
486
		    eigVecCoeffs%ccof(-atoms%llod:,:,:,:,jsp),zMat,results%eig(:,nk,jsp),force) 
487 488 489
!            DO i=1,atoms%nat
!	     	atoms%ngopr(i)=ngopr_temp(i)
!            END DO
490
		CALL abcrot(atoms%ntype,atoms%nat,numbands,atoms%lmaxd,dimension%lmd,atoms%llod,atoms%nlod,atoms%ntype,atoms%neq,&
491 492 493
		            numbands,atoms%lmax,atoms%nlo,atoms%llo,sym%nop,atoms%ngopr,sym%mrot,atoms%invsat,sym%invsatnr,cell%bmat,&
		           oneD%odi,oneD%ods,&
		           eigVecCoeffs%acof(:,0:,:,jsp),eigVecCoeffs%bcof(:,0:,:,jsp),eigVecCoeffs%ccof(-atoms%llod:,:,:,:,jsp))
S.Rost's avatar
S.Rost committed
494
!-------------------------for spex output: nbasfcn=nv(because lo info not needed) and numbands setting to numbands without highest (degenerat) state-------- 
495
!                nbasfcn= MERGE(lapw%nv(1)+lapw%nv(2)+2*atoms%nlotot,lapw%nv(1)+atoms%nlotot,noco%l_noco)
496 497 498 499 500 501 502 503
		ndbands=numbands-1
		DO i=(numbands-1),1,-1
		    IF (abs(results%eig(i+1,nk,jsp)-results%eig(i,nk,jsp)).LT.0.000001) THEN
		        ndbands=ndbands-1
		    ELSE
			EXIT
		    END IF
		END DO
504
                write(*,*)numbands,ndbands
505 506 507 508
		numbands=ndbands
!------------------------setting variables numbands and nbasfcn end -------------------
		!CALL read_eig(eig_id,nk,jsp,eig=results%eig(:,nk,jsp),zmat=zMat)
		dims(:1)=(/numbands/)
509 510 511 512
		dimsInt=dims
		CALL h5screate_simple_f(1,dims(:1),eigSpaceID,hdfError)
		CALL h5dcreate_f(kptGroupID, "eig", H5T_NATIVE_DOUBLE, eigSpaceID, eigSetID, hdfError)
		CALL h5sclose_f(eigSpaceID,hdfError)
513
                CALL io_write_real1(eigSetID,(/1/),dimsInt(:1),results%eig(:numbands,nk,jsp))
514
		CALL h5dclose_f(eigSetID, hdfError)
515
   
516
                CALL io_write_attint0(kptGroupID,'numbands',numbands)            
517
		IF (zMat%l_real) THEN
518
		      dims(:2)=(/nbasfcn,numbands/)
519 520
		      dimsInt=dims
		      CALL h5screate_simple_f(2,dims(:2),kptCoordSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
521
		      CALL h5dcreate_f(kptGroupID, "pw", H5T_NATIVE_DOUBLE, kptCoordSpaceID, kptCoordSetID, hdfError)
522
		      CALL h5sclose_f(kptCoordSpaceID,hdfError)
523
            CALL io_write_real2(kptCoordSetID,(/1,1/),dimsInt(:2),zMat%data_r(:nbasfcn,:numbands))
524 525
		      CALL h5dclose_f(kptCoordSetID, hdfError)
		ELSE
526 527
                      AllOCATE(output3(2,nbasfcn,numbands))
!			SIZE(zMat%data_c,1),SIZE(zMat%data_c,2)
528 529
		      output3(1,:,:)=REAL(zMat%data_c(:,:numbands))
		      output3(2,:,:)=AIMAG(zMat%data_c(:,:numbands))
530
		      dims(:3)=(/2,nbasfcn,numbands/)
531 532
		      dimsInt=dims
		      CALL h5screate_simple_f(3,dims(:3),zmatSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
533
		      CALL h5dcreate_f(kptGroupID, "pw", H5T_NATIVE_DOUBLE, zmatSpaceID, zmatSetID, hdfError)
534
		      CALL h5sclose_f(zmatSpaceID,hdfError)
535
		      CALL io_write_real3(zmatSetID,(/1,1,1/),dimsInt(:3),output3(:2,:nbasfcn,:numbands))
536 537 538
		      CALL h5dclose_f(zmatSetID, hdfError)
		      DEAllOCATE(output3)
		END IF
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
		!AllOCATE(output(2,numbands,dimension%lmd+1,atoms%nat))
		!output(1,:,:,:)=REAL(eigVecCoeffs%acof(:,0:,:,jsp))
		!output(2,:,:,:)=AIMAG(eigVecCoeffs%acof(:,0:,:,jsp))
		!dims(:4)=(/2,numbands,dimension%lmd+1,atoms%nat/)
		!dimsInt = dims
                !CALL h5screate_simple_f(4,dims(:4),itypeSpaceID,hdfError)
		!CALL h5dcreate_f(kptGroupID, "acof", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
		!CALL h5sclose_f(itypeSpaceID,hdfError)
		!CALL io_write_real4(itypeSetID,(/1,1,1,1/),dimsInt(:4), output)
		!CALL h5dclose_f(itypeSetID, hdfError)
		!DEAllOCATE(output)

		!AllOCATE(output(2,numbands,dimension%lmd+1,atoms%nat))
		!output(1,:,:,:)=REAL(eigVecCoeffs%bcof(:,0:,:,jsp))
		!output(2,:,:,:)=AIMAG(eigVecCoeffs%bcof(:,0:,:,jsp))
		!dims(:4)=(/2,numbands,dimension%lmd+1,atoms%nat/)
		!dimsInt = dims
                !CALL h5screate_simple_f(4,dims(:4),itypeSpaceID,hdfError)
		!CALL h5dcreate_f(kptGroupID, "bcof", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
		!CALL h5sclose_f(itypeSpaceID,hdfError)
		!CALL io_write_real4(itypeSetID,(/1,1,1,1/),dimsInt(:4), output)
		!CALL h5dclose_f(itypeSetID, hdfError)
		!DEAllOCATE(output)

		!AllOCATE(output5(2,atoms%llod*2+1,numbands,atoms%nlod,atoms%nat))
		!output5(1,:,:,:,:)=REAL(eigVecCoeffs%ccof(-atoms%llod:,:,:,:,jsp))
		!output5(2,:,:,:,:)=AIMAG(eigVecCoeffs%ccof(-atoms%llod:,:,:,:,jsp))
		!dims(:5)=(/2,atoms%llod*2+1,numbands,atoms%nlod,atoms%nat/)
		!dimsInt = dims
                !CALL h5screate_simple_f(5,dims(:5),itypeSpaceID,hdfError)
		!CALL h5dcreate_f(kptGroupID, "ccof", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
		!CALL h5sclose_f(itypeSpaceID,hdfError)
		!CALL io_write_real5(itypeSetID,(/1,1,1,1,1/),dimsInt(:5), output5)
		!CALL h5dclose_f(itypeSetID, hdfError)
		!DEAllOCATE(output5)

!-------------------------output spex format-------------------
	      ! get dimensions for MT cof
	      lmn = 0
	      na  = 0
	      do n = 1,atoms%ntype
		do nn = 1,atoms%neq(n)
		  na = na + 1
		  lm = 0
		  do l = 0,atoms%lmax(n)
		    lm = lm + 2*(2*l+1)
		  enddo
		  do i = 1,atoms%nlo(n)
		    lm = lm + 2*atoms%llo(i,n)+1
		  enddo
		  lmn = max(lmn,lm)
		enddo
	      enddo
	      Hdim1 = (/ 2,lmn,na,numbands /)

		allocate ( cof(Hdim1(1),Hdim1(2),Hdim1(3),Hdim1(4)) )
		      cof = 0
		      na  = 0
		      do n = 1,atoms%ntype
			do nn = 1,atoms%neq(n)
			  na = na + 1
			  lm  = 0
			  lmn = 0
			  do l = 0,atoms%lmax(n)
			    do m = -l,l              
			      cof(1,lmn+1,na,:) = real ( eigVecCoeffs%acof(:numbands,lm,na,jsp) * img**l )
			      cof(1,lmn+2,na,:) = real ( eigVecCoeffs%bcof(:numbands,lm,na,jsp) * img**l )
			      cof(2,lmn+1,na,:) = aimag ( eigVecCoeffs%acof(:numbands,lm,na,jsp) * img**l )
			      cof(2,lmn+2,na,:) = aimag ( eigVecCoeffs%bcof(:numbands,lm,na,jsp) * img**l )
			      lm  = lm  + 1
			      lmn = lmn + 2
			      do i = 1,atoms%nlo(n)
				if(atoms%llo(i,n).eq.l) then
				  lmn = lmn + 1
				  cof(1,lmn,na,:) = real( eigVecCoeffs%ccof(m,:numbands,i,na,jsp) * img**l )
				  cof(2,lmn,na,:) = aimag( eigVecCoeffs%ccof(m,:numbands,i,na,jsp) * img**l )
				endif
			      enddo
			    enddo
			  enddo
			enddo
			if(lmn.gt.size(cof,2)) then
			  write(*,*) lmn,size(cof,2)
			endif
		      enddo

		dims(:4)=Hdim1
626
		dimsInt = dims
627
                CALL h5screate_simple_f(4,dims(:4),itypeSpaceID,hdfError)
628
		CALL h5dcreate_f(kptGroupID, "mt", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
629
		CALL h5sclose_f(itypeSpaceID,hdfError)
630
     		CALL io_write_real4(itypeSetID,(/1,1,1,1/),dimsInt(:4), cof)
631
		CALL h5dclose_f(itypeSetID, hdfError)
632
		deallocate ( cof )
633
!-------------------------end output spex format-----------------
634 635 636 637 638 639 640

		CALL h5gclose_f(kptGroupID, hdfError)
            
        END DO
       CALL h5gclose_f(jspGroupID, hdfError)
   END DO
   CALL h5fclose_f(fileID, hdfError)  
641

642 643
   END IF
   
644 645 646 647 648 649 650
#else
   CALL juDFT_error("writeBasis called without HDF5! ",calledby="writeBasis")
#endif

END SUBROUTINE writeBasis

END MODULE m_writeBasis