writeBasis.F90 27.6 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 41
!     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
      TYPE(t_potden), INTENT(IN)    :: vTot
42 43
      TYPE(t_potden), INTENT(IN)    :: vCoul
      TYPE(t_potden), INTENT(IN)    :: 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

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

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

124 125
    CALL force%init1(input,atoms)

126 127 128 129 130 131 132 133
    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))

134 135


136 137 138 139
      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.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      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
155 156 157 158 159 160
      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)      
161 162 163 164 165 166

      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
167
      CALL h5dcreate_f(cellGroupID, "amat", H5T_NATIVE_DOUBLE, bravaisMatrixSpaceID, bravaisMatrixSetID, hdfError)
168 169 170 171 172 173 174
      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
175
      CALL h5dcreate_f(cellGroupID, "bmat", H5T_NATIVE_DOUBLE, reciprocalCellSpaceID, reciprocalCellSetID, hdfError)
176 177 178
      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
179 180 181 182 183 184
      
      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)

185 186

      CALL h5gclose_f(cellGroupID, hdfError)
187
      iAtom = 0
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      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
208
      CALL h5dcreate_f(atomsGroupID, "jri", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
209 210 211 212 213 214 215
      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
216
      CALL h5dcreate_f(atomsGroupID, "lmax", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
217 218 219
      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
220 221 222 223 224 225 226 227
      
      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)
228 229 230 231

      dims(:1)=(/atoms%ntype/)
      dimsInt=dims
      CALL h5screate_simple_f(1,dims(:1),atomicNumbersSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
232
      CALL h5dcreate_f(atomsGroupID, "nlo", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
233 234 235 236 237 238 239
      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
240
      CALL h5dcreate_f(atomsGroupID, "llo", H5T_NATIVE_INTEGER, atomPosSpaceID, atomPosSetID, hdfError)
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
      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
256
      CALL h5dcreate_f(atomsGroupID, "ratoms", H5T_NATIVE_DOUBLE, atomPosSpaceID, atomPosSetID, hdfError)
257 258 259 260 261 262 263
      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
264
      CALL h5dcreate_f(atomsGroupID, "zatoms", H5T_NATIVE_INTEGER, atomicNumbersSpaceID, atomicNumbersSetID, hdfError)
265 266 267 268
      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
269 270 271 272 273
      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
274
      CALL io_write_integer1(atomicNumbersSetID,(/1/),dimsInt(:1),atoms%nz)
S.Rost's avatar
S.Rost committed
275 276
      CALL h5dclose_f(atomicNumbersSetID, hdfError)

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
      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)

309
      CALL usdus%init(atoms,input%jspins)
310

311 312 313 314 315
      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
316
            CALL lapw%init(input,noco,kpts,atoms,sym,nk,cell,l_zref)
S.Rost's avatar
S.Rost committed
317
            !write(kpt_name , '(2a,i0)') TRIM(ADJUSTL(jsp_name)),'/kpt_',nk
318
            write(kpt_name , '(2a,f12.10,a,f12.10,a,f12.10)') TRIM(ADJUSTL(jsp_name)),'/kpt_',kpts%bk(1,nk),',',kpts%bk(2,nk),',',kpts%bk(3,nk)
319 320 321
            CALL h5oexists_by_name_f(fileID, TRIM(ADJUSTL(kpt_name)), link_exists, hdfError)
            IF (link_exists) CYCLE
            CALL h5gcreate_f(fileID, TRIM(ADJUSTL(kpt_name)), kptGroupID, hdfError)
322 323 324
!--------------------enter output gvec etc here--------------------
            CALL io_write_attint0(kptGroupID,'nv',lapw%nv(jsp))

325 326 327 328 329 330 331
            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)	
332 333 334
	      
            CALL h5gclose_f(kptGroupID, hdfError)   
         END DO
S.Rost's avatar
S.Rost committed
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
         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)
424 425 426
	END DO

       CALL h5gclose_f(jspGroupID, hdfError)
427 428 429 430
    END DO
    CALL h5fclose_f(fileID, hdfError)
    version = 1
    filename = 'eig_gw.hdf'
431

432
    IF(input%gw==2) THEN
433 434 435 436
    INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
    IF(l_exist) THEN
       CALL system('rm '//TRIM(ADJUSTL(filename)))       
    END IF
437 438 439 440 441 442 443 444 445 446 447 448 449

      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)
450 451
            !write(kpt_name , '(2a,i0)') TRIM(ADJUSTL(jsp_name)),'/kpt_',nk
            write(kpt_name , '(2a,f12.10,a,f12.10,a,f12.10)') TRIM(ADJUSTL(jsp_name)),'/kpt_',kpts%bk(1,nk),',',kpts%bk(2,nk),',',kpts%bk(3,nk)
452 453
            CALL h5oexists_by_name_f(fileID, TRIM(ADJUSTL(kpt_name)), link_exists, hdfError)
            IF (link_exists) CYCLE
454
            CALL h5gcreate_f(fileID, TRIM(ADJUSTL(kpt_name)), kptGroupID, hdfError)
455
!--------------------abcoff, zmat, eig output here-------------------
456 457 458 459
!,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)
460
            CALL read_eig(eig_id,nk,jsp,zmat=zMat)
461 462
	    CALL eigVecCoeffs%init(input,DIMENSION,atoms,noco,jsp,numbands)
            IF (input%l_f) CALL force%init2(numbands,input,atoms)
463 464 465 466
!            DO i=1,atoms%nat
!	    	ngopr_temp(i)=atoms%ngopr(i)
!               atoms%ngopr(i)=1
!            END DO
467 468
		CALL abcof(input,atoms,sym,cell,lapw,numbands,usdus,noco,jsp,oneD,&
		    eigVecCoeffs%acof(:,0:,:,jsp),eigVecCoeffs%bcof(:,0:,:,jsp),&
469
		    eigVecCoeffs%ccof(-atoms%llod:,:,:,:,jsp),zMat,results%eig(:,nk,jsp),force) 
470 471 472 473 474 475 476
!            DO i=1,atoms%nat
!	     	atoms%ngopr(i)=ngopr_temp(i)
!            END DO
		CALL abcrot(atoms%ntype,atoms%nat,dimension%neigd,atoms%lmaxd,dimension%lmd,atoms%llod,atoms%nlod,atoms%ntype,atoms%neq,&
		            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
477
!-------------------------for spex output: nbasfcn=nv(because lo info not needed) and numbands setting to numbands without highest (degenerat) state-------- 
478 479 480 481 482 483 484 485 486 487 488 489 490 491
                nbasfcn=lapw%nv(jsp)
		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
write(*,*)numbands,ndbands
		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/)
492 493 494 495
		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)
496
                CALL io_write_real1(eigSetID,(/1/),dimsInt(:1),results%eig(:numbands,nk,jsp))
497
		CALL h5dclose_f(eigSetID, hdfError)
498
   
499
                CALL io_write_attint0(kptGroupID,'numbands',numbands)            
500
		IF (zMat%l_real) THEN
501
		      dims(:2)=(/nbasfcn,numbands/)
502 503
		      dimsInt=dims
		      CALL h5screate_simple_f(2,dims(:2),kptCoordSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
504
		      CALL h5dcreate_f(kptGroupID, "pw", H5T_NATIVE_DOUBLE, kptCoordSpaceID, kptCoordSetID, hdfError)
505
		      CALL h5sclose_f(kptCoordSpaceID,hdfError)
506
            CALL io_write_real2(kptCoordSetID,(/1,1/),dimsInt(:2),zMat%data_r(:nbasfcn,:numbands))
507 508
		      CALL h5dclose_f(kptCoordSetID, hdfError)
		ELSE
509 510
                      AllOCATE(output3(2,nbasfcn,numbands))
!			SIZE(zMat%data_c,1),SIZE(zMat%data_c,2)
511 512
		      output3(1,:,:)=REAL(zMat%data_c(:,:))
		      output3(2,:,:)=AIMAG(zMat%data_c(:,:))
513
		      dims(:3)=(/2,nbasfcn,numbands/)
514 515
		      dimsInt=dims
		      CALL h5screate_simple_f(3,dims(:3),zmatSpaceID,hdfError)
S.Rost's avatar
S.Rost committed
516
		      CALL h5dcreate_f(kptGroupID, "pw", H5T_NATIVE_DOUBLE, zmatSpaceID, zmatSetID, hdfError)
517
		      CALL h5sclose_f(zmatSpaceID,hdfError)
518
		      CALL io_write_real3(zmatSetID,(/1,1,1/),dimsInt(:3),output3(:2,:nbasfcn,:numbands))
519 520 521
		      CALL h5dclose_f(zmatSetID, hdfError)
		      DEAllOCATE(output3)
		END IF
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
		!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
609
		dimsInt = dims
610
                CALL h5screate_simple_f(4,dims(:4),itypeSpaceID,hdfError)
611
		CALL h5dcreate_f(kptGroupID, "mt", H5T_NATIVE_DOUBLE, itypeSpaceID, itypeSetID, hdfError)
612
		CALL h5sclose_f(itypeSpaceID,hdfError)
613
     		CALL io_write_real4(itypeSetID,(/1,1,1,1/),dimsInt(:4), cof)
614
		CALL h5dclose_f(itypeSetID, hdfError)
615
		deallocate ( cof )
616
!-------------------------end output spex format-----------------
617 618 619 620 621 622 623

		CALL h5gclose_f(kptGroupID, hdfError)
            
        END DO
       CALL h5gclose_f(jspGroupID, hdfError)
   END DO
   CALL h5fclose_f(fileID, hdfError)  
624
!-------------------------write potential--------------------
625 626 627
   CALL writePotential(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,POT_ARCHIVE_TYPE_TOT_const,vTot%iter,vTot%mt,vTot%pw_w,vTot%vacz,vTot%vacxy)
   CALL writePotential(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,POT_ARCHIVE_TYPE_COUL_const,vCoul%iter,vCoul%mt,vCoul%pw_w,vCoul%vacz,vCoul%vacxy)
   CALL writePotential(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,POT_ARCHIVE_TYPE_X_const,vx%iter,vx%mt,vx%pw_w,vx%vacz,vx%vacxy)
628

629 630
   END IF
   
631 632 633 634 635 636 637
#else
   CALL juDFT_error("writeBasis called without HDF5! ",calledby="writeBasis")
#endif

END SUBROUTINE writeBasis

END MODULE m_writeBasis