cdn_io.F90 41.7 KB
Newer Older
1
!--------------------------------------------------------------------------------
2
! Copyright (c) 2017 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
! 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.
!--------------------------------------------------------------------------------

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!
!!! This module is a wrapper for the charge density I/O
!!!
!!!                             GM'17
!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

MODULE m_cdn_io

   USE m_types
   USE m_juDFT
   USE m_loddop
   USE m_wrtdop
21 22 23 24
   USE m_cdnpot_io_hdf
#ifdef CPP_HDF
   USE hdf5
#endif
25 26 27
   IMPLICIT NONE

   PRIVATE
28 29
   PUBLIC readDensity, writeDensity
   PUBLIC isDensityFilePresent, isCoreDensityPresent
30
   PUBLIC readCoreDensity, writeCoreDensity
31
   PUBLIC readStars, writeStars
32
   PUBLIC readStepfunction, writeStepfunction
33
   PUBLIC setStartingDensity, readPrevEFermi
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   PUBLIC CDN_INPUT_DEN_const, CDN_OUTPUT_DEN_const
   PUBLIC CDN_ARCHIVE_TYPE_CDN1_const, CDN_ARCHIVE_TYPE_NOCO_const
   PUBLIC CDN_ARCHIVE_TYPE_CDN_const

   INTEGER,          PARAMETER :: CDN_INPUT_DEN_const = 1
   INTEGER,          PARAMETER :: CDN_OUTPUT_DEN_const = 2

   INTEGER,          PARAMETER :: CDN_ARCHIVE_TYPE_CDN1_const = 1
   INTEGER,          PARAMETER :: CDN_ARCHIVE_TYPE_NOCO_const = 2
   INTEGER,          PARAMETER :: CDN_ARCHIVE_TYPE_CDN_const  = 3

   INTEGER,          PARAMETER :: CDN_DIRECT_MODE = 1
   INTEGER,          PARAMETER :: CDN_STREAM_MODE = 2
   INTEGER,          PARAMETER :: CDN_HDF5_MODE   = 3

   CONTAINS

   SUBROUTINE readDensity(stars,vacuum,atoms,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
52
                          relCdnIndex,fermiEnergy,l_qfix,iter,fr,fpw,fz,fzxy,cdom,cdomvz,cdomvxy)
53 54 55 56 57 58 59 60 61 62 63 64 65

      TYPE(t_stars),INTENT(IN)  :: stars
      TYPE(t_vacuum),INTENT(IN) :: vacuum
      TYPE(t_atoms),INTENT(IN)  :: atoms
      TYPE(t_sphhar),INTENT(IN) :: sphhar
      TYPE(t_input),INTENT(IN)  :: input
      TYPE(t_sym),INTENT(IN)    :: sym
      TYPE(t_oneD),INTENT(IN)   :: oneD

      INTEGER, INTENT (IN)      :: inOrOutCDN
      INTEGER, INTENT (IN)      :: relCdnIndex
      INTEGER, INTENT (OUT)     :: iter
      INTEGER, INTENT (IN)      :: archiveType
66 67
      REAL,    INTENT (OUT)     :: fermiEnergy
      LOGICAL, INTENT (OUT)     :: l_qfix
68 69 70

      !     ..
      !     .. Array Arguments ..
71
      COMPLEX, INTENT (OUT) :: fpw(stars%ng3,input%jspins), fzxy(vacuum%nmzxyd,stars%ng2-1,2,input%jspins)
72 73 74 75
      COMPLEX, INTENT (OUT) :: cdom(:), cdomvz(:,:), cdomvxy(:,:,:)
      REAL,    INTENT (OUT) :: fr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins), fz(vacuum%nmzd,2,input%jspins)

      ! local variables
76 77 78
      INTEGER            :: mode, datend, k, i, iVac, j, iUnit
      LOGICAL            :: l_exist, l_rhomatFile
      CHARACTER(LEN=30)  :: filename
79

80 81 82 83
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
84 85 86
      INTEGER           :: currentStructureIndex
      INTEGER           :: readDensityIndex, lastDensityIndex
      INTEGER           :: previousDensityIndex, densityType
87 88 89
      CHARACTER(LEN=30) :: archiveName
      TYPE(t_cell)      :: cellTemp

90 91 92
      fermiEnergy = 0.0
      l_qfix = .FALSE.

93 94 95
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
96 97 98 99 100
#ifdef CPP_HDF

         densityType = 0
         archiveName = ''

101 102
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
103 104
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             readDensityIndex,lastDensityIndex)
105 106 107 108

            IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
               archiveName = 'cdn'
            ELSE
109
               WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            END IF

            SELECT CASE (inOrOutCDN)
               CASE (CDN_INPUT_DEN_const)
                  IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
                     densityType = DENSITY_TYPE_NOCO_IN_const
                  ELSE
                     densityType = DENSITY_TYPE_IN_const
                  END IF
               CASE (CDN_OUTPUT_DEN_const)
                  IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
                     densityType = DENSITY_TYPE_NOCO_OUT_const
                  ELSE
                     densityType = DENSITY_TYPE_OUT_const
                  END IF
               CASE DEFAULT
                  WRITE(*,*) 'inOrOutCDN = ', inOrOutCDN
127
                  CALL juDFT_error("Invalid inOrOutCDN selected.",calledby ="readDensity")
128 129
            END SELECT
            l_exist = isDensityEntryPresentHDF(fileID,archiveName,densityType)
130
            CALL closeCDNPOT_HDF(fileID)
131
         END IF
132

133
         IF (l_exist) THEN
134 135
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             readDensityIndex,lastDensityIndex)
136 137

            CALL readDensityHDF(fileID, archiveName, densityType,&
138
                                fermiEnergy,l_qfix,iter,fr,fpw,fz,fzxy,cdom,cdomvz,cdomvxy)
139

140
            CALL closeCDNPOT_HDF(fileID)
141 142
            RETURN
         ELSE
143
            WRITE(*,*) 'cdn.hdf file or relevant density entry not found.'
144 145 146
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
147
#endif
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
      END IF

      IF(mode.EQ.CDN_STREAM_MODE) THEN
         INQUIRE(FILE='cdn.str',EXIST=l_exist)
         IF (l_exist) THEN
            !load density from cdn.str and exit subroutine

            RETURN
         ELSE
            WRITE(*,*) 'cdn.str file not found.'
            WRITE(*,*) 'Falling back to direct access file cdn1.'
            mode = CDN_DIRECT_MODE
         END IF
      END IF

      IF (mode.EQ.CDN_DIRECT_MODE) THEN
         filename = 'cdn1'
165
         l_rhomatFile = .FALSE.
166 167 168
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            INQUIRE(file="rhomat_inp",EXIST=l_exist)
            IF (l_exist) filename = 'rhomat_inp'
169 170 171 172 173
            IF (inOrOutCDN.EQ.CDN_OUTPUT_DEN_const) THEN
               INQUIRE(file="rhomat_out",EXIST=l_exist)
               IF (l_exist) filename = 'rhomat_out'
            END IF
            IF(l_exist) l_rhomatFile = .TRUE.
174 175 176 177 178 179 180
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

         INQUIRE(file=TRIM(ADJUSTL(filename)),EXIST=l_exist)
         IF(.NOT.l_exist) THEN
181
            CALL juDFT_error("charge density file "//TRIM(ADJUSTL(filename))//" missing",calledby ="readDensity")
182 183 184 185 186
         END IF

         iUnit = 93
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),FORM='unformatted',STATUS='old')

187
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
188 189 190 191 192 193 194 195 196 197
            ! call loddop to move the file position to the output density
            CALL loddop(stars,vacuum,atoms,sphhar,input,sym,&
                        iUnit,iter,fr,fpw,fz,fzxy)
         END IF

         ! read in the density
         CALL loddop(stars,vacuum,atoms,sphhar,input,sym,&
                     iUnit,iter,fr,fpw,fz,fzxy)

         ! read in additional data if l_noco and data is present
198
         IF ((archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const).AND.l_rhomatFile) THEN
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
            READ (iUnit,iostat=datend) (cdom(k),k=1,stars%ng3)
            IF (datend == 0) THEN
               IF (input%film) THEN
                  READ (iUnit) ((cdomvz(i,iVac),i=1,vacuum%nmz),iVac=1,vacuum%nvac)
                  READ (iUnit) (((cdomvxy(i,j-1,iVac),i=1,vacuum%nmzxy),j=2,oneD%odi%nq2), iVac=1,vacuum%nvac)
               END IF
            ELSE
               ! (datend < 0)  =>  no off-diagonal magnetisation stored
               !                   in "rhomat_inp"
               IF (datend > 0) THEN
                  WRITE(*,*) 'datend = ', datend
                  CALL juDFT_error("density file has illegal state.",calledby ="readDensity")
               END IF
               cdom = CMPLX(0.0,0.0)
               IF (input%film) THEN
                  cdomvz = CMPLX(0.0,0.0)
                  cdomvxy = CMPLX(0.0,0.0)
               END IF
            END IF
         ELSE IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            cdom = CMPLX(0.0,0.0)
            IF (input%film) THEN
               cdomvz = CMPLX(0.0,0.0)
               cdomvxy = CMPLX(0.0,0.0)
            END IF
         END IF
      END IF

      CLOSE(iUnit)

   END SUBROUTINE readDensity

231
   SUBROUTINE writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
232
                           relCdnIndex,fermiEnergy,l_qfix,iter,fr,fpw,fz,fzxy,cdom,cdomvz,cdomvxy)
233 234 235 236

      TYPE(t_stars),INTENT(IN)  :: stars
      TYPE(t_vacuum),INTENT(IN) :: vacuum
      TYPE(t_atoms),INTENT(IN)  :: atoms
237
      TYPE(t_cell), INTENT(IN)  :: cell
238 239 240 241 242 243
      TYPE(t_sphhar),INTENT(IN) :: sphhar
      TYPE(t_input),INTENT(IN)  :: input
      TYPE(t_sym),INTENT(IN)    :: sym
      TYPE(t_oneD),INTENT(IN)   :: oneD

      INTEGER, INTENT (IN)      :: inOrOutCDN
244
      INTEGER, INTENT (IN)      :: relCdnIndex, iter
245
      INTEGER, INTENT (IN)      :: archiveType
246 247
      REAL,    INTENT (IN)      :: fermiEnergy
      LOGICAL, INTENT (IN)      :: l_qfix
248 249
      !     ..
      !     .. Array Arguments ..
250
      COMPLEX, INTENT (IN) :: fpw(stars%ng3,input%jspins), fzxy(vacuum%nmzxyd,stars%ng2-1,2,input%jspins)
251 252 253 254 255 256 257 258 259 260 261 262 263 264
      COMPLEX, INTENT (IN) :: cdom(:), cdomvz(:,:), cdomvxy(:,:,:)
      REAL,    INTENT (IN) :: fr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins), fz(vacuum%nmzd,2,input%jspins)

      TYPE(t_stars)        :: starsTemp
      TYPE(t_vacuum)       :: vacuumTemp
      TYPE(t_atoms)        :: atomsTemp
      TYPE(t_sphhar)       :: sphharTemp
      TYPE(t_input)        :: inputTemp
      TYPE(t_sym)          :: symTemp

      COMPLEX, ALLOCATABLE :: fpwTemp(:,:), fzxyTemp(:,:,:,:)
      REAL, ALLOCATABLE    :: frTemp(:,:,:,:), fzTemp(:,:,:)

      INTEGER           :: mode, iterTemp, k, i, iVac, j, iUnit
265
      INTEGER           :: d1, d10, asciioffset, iUnitTemp
266
      LOGICAL           :: l_exist, l_storeIndices
267
      CHARACTER(len=30) :: filename
268
      CHARACTER(len=5)  :: cdnfile
269

270 271 272 273
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
274 275 276 277 278
      INTEGER           :: currentStructureIndex
      INTEGER           :: readDensityIndex, writeDensityIndex, lastDensityIndex
      INTEGER           :: previousDensityIndex, densityType
      INTEGER           :: starsIndexTemp, latharmsIndexTemp, structureIndexTemp
      INTEGER           :: jspinsTemp
279 280
      REAL              :: fermiEnergyTemp
      LOGICAL           :: l_qfixTemp
281 282
      CHARACTER(LEN=30) :: archiveName

283 284 285
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
286
#ifdef CPP_HDF
287 288
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                          readDensityIndex,lastDensityIndex)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

         l_storeIndices = .FALSE.
         IF (currentStarsIndex.EQ.0) THEN
            currentStarsIndex = 1
            l_storeIndices = .TRUE.
            CALL writeStarsHDF(fileID, currentStarsIndex, stars)
         END IF
         IF (currentLatharmsIndex.EQ.0) THEN
            currentLatharmsIndex = 1
            l_storeIndices = .TRUE.
            CALL writeLatharmsHDF(fileID, currentLatharmsIndex, sphhar)
         END IF
         IF(currentStructureIndex.EQ.0) THEN
            currentStructureIndex = 1
            l_storeIndices = .TRUE.
            CALL writeStructureHDF(fileID, input, atoms, cell, vacuum, oneD, currentStructureIndex)
         END IF
306 307
         previousDensityIndex = readDensityIndex
         writeDensityIndex = readDensityIndex
308
         IF(relCdnIndex.NE.0) THEN
309 310 311
            writeDensityIndex = lastDensityIndex+relCdnIndex
            lastDensityIndex = writeDensityIndex
            readDensityIndex = writeDensityIndex
312 313 314 315 316 317 318
            l_storeIndices = .TRUE.
         END IF

         archiveName = ''
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            archiveName = 'cdn'
         ELSE
319
            WRITE(archiveName,'(a,i0)') '/cdn-', writeDensityIndex
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
         END IF

         densityType = 0
         SELECT CASE (inOrOutCDN)
            CASE (CDN_INPUT_DEN_const)
               IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
                  densityType = DENSITY_TYPE_NOCO_IN_const
               ELSE
                  densityType = DENSITY_TYPE_IN_const
               END IF
            CASE (CDN_OUTPUT_DEN_const)
               IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
                  densityType = DENSITY_TYPE_NOCO_OUT_const
               ELSE
                  densityType = DENSITY_TYPE_OUT_const
               END IF
            CASE DEFAULT
               WRITE(*,*) 'inOrOutCDN = ', inOrOutCDN
               CALL juDFT_error("Invalid inOrOutCDN selected.",calledby ="writeDensity")
         END SELECT

341 342 343 344 345
         IF(relCdnIndex.EQ.0) THEN
            l_exist = isDensityEntryPresentHDF(fileID,archiveName,DENSITY_TYPE_UNDEFINED_const)
            IF(l_exist) THEN
               CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_UNDEFINED_const,&
                                        iterTemp, starsIndexTemp, latharmsIndexTemp, structureIndexTemp,&
346
                                        previousDensityIndex, jspinsTemp, fermiEnergyTemp, l_qfixTemp)
347 348 349
            END IF
         END IF

350 351 352 353 354 355 356 357 358 359 360 361 362
         ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
         ALLOCATE (fzTemp(vacuum%nmzd,2,input%jspins))
         fzTemp(:,:,:) = fz(:,:,:)
         fzxyTemp(:,:,:,:) = fzxy(:,:,:,:)
         IF(vacuum%nvac.EQ.1) THEN
            fzTemp(:,2,:)=fzTemp(:,1,:)
            IF (sym%invs) THEN
               fzxyTemp(:,:,2,:) = CONJG(fzxyTemp(:,:,1,:))
            ELSE
               fzxyTemp(:,:,2,:) = fzxyTemp(:,:,1,:)
            END IF
         END IF

363
         CALL writeDensityHDF(input, fileID, archiveName, densityType, previousDensityIndex,&
364
                              currentStarsIndex, currentLatharmsIndex, currentStructureIndex,&
365 366 367
                              fermiEnergy,l_qfix,iter+relCdnIndex,fr,fpw,fzTemp,fzxyTemp,cdom,cdomvz,cdomvxy)

         DEALLOCATE(fzTemp,fzxyTemp)
368 369

         IF(l_storeIndices) THEN
370 371
            CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,&
                                    currentStructureIndex,readDensityIndex,lastDensityIndex)
372 373
         END IF

374
         CALL closeCDNPOT_HDF(fileID)
375
#endif
376 377 378 379 380 381 382
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         ! Write density to cdn.str file
         STOP 'CDN_STREAM_MODE not yet implemented!'
      ELSE
         filename = 'cdn1'
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            filename = 'rhomat_inp'
383
            IF(inOrOutCDN.EQ.CDN_OUTPUT_DEN_const) filename = 'rhomat_out'
384 385 386 387 388
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

389 390 391 392 393 394 395
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(iter.EQ.0)) THEN
            INQUIRE(file=TRIM(ADJUSTL(filename)),EXIST=l_exist)
            IF(l_exist) THEN
               CALL juDFT_error("Trying to generate starting density while a density exists.",calledby ="writeDensity")
            END IF
         END IF

396 397 398
         iUnit = 93
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),FORM='unformatted',STATUS='unknown')

399
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(iter.GE.1)) THEN
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
            inputTemp%jspins = input%jspins
            vacuumTemp%nmzxyd = vacuum%nmzxyd
            atomsTemp%jmtd = atoms%jmtd
            sphharTemp%nlhd = sphhar%nlhd
            vacuumTemp%nmzd = vacuum%nmzd
            atomsTemp%ntype = atoms%ntype
            ALLOCATE (sphharTemp%nlh(SIZE(sphhar%nlh)))
            sphharTemp%nlh(:) = sphhar%nlh(:)
            ALLOCATE (atomsTemp%ntypsy(SIZE(atoms%ntypsy)))
            atomsTemp%ntypsy(:) = atoms%ntypsy(:)
            ALLOCATE (atomsTemp%jri(SIZE(atoms%jri)))
            atomsTemp%jri(:) = atoms%jri(:)
            ALLOCATE (atomsTemp%neq(SIZE(atoms%neq)))
            atomsTemp%neq(:) = atoms%neq(:)
            ALLOCATE (atomsTemp%zatom(SIZE(atoms%zatom)))
            atomsTemp%zatom(:) = atoms%zatom(:)
            ALLOCATE (atomsTemp%rmt(SIZE(atoms%rmt)))
            atomsTemp%rmt(:) = atoms%rmt(:)
            ALLOCATE (atomsTemp%dx(SIZE(atoms%dx)))
            atomsTemp%dx(:) = atoms%dx(:)
            starsTemp%ng3 = stars%ng3
            symTemp%invs = sym%invs
            inputTemp%film = input%film
            vacuumTemp%nvac = vacuum%nvac
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
426 427
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
            ALLOCATE (frTemp(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins))
            ALLOCATE (fzTemp(vacuum%nmzd,2,input%jspins))

            !--->    generate name of file to hold the results of this iteration
            d1 = MOD(iter,10)
            d10 = MOD(INT((iter+0.5)/10),10)
            asciioffset = IACHAR('1')-1
            IF ( d10.GE.10 ) asciioffset = IACHAR('7')
            cdnfile = 'cdn'//ACHAR(d10+asciioffset)//ACHAR(d1+IACHAR('1')-1)

            iUnitTemp = 72
            OPEN (iUnitTemp,file=cdnfile,form='unformatted',status='unknown')
            REWIND iUnitTemp


            CALL loddop(starsTemp,vacuumTemp,atomsTemp,sphharTemp,inputTemp,symTemp,&
                        iUnit,iterTemp,frTemp,fpwTemp,fzTemp,fzxyTemp)
            CALL wrtdop(starsTemp,vacuumTemp,atomsTemp,sphharTemp,inputTemp,symTemp,&
                        iUnitTemp,iterTemp,frTemp,fpwTemp,fzTemp,fzxyTemp)
            CALL loddop(starsTemp,vacuumTemp,atomsTemp,sphharTemp,inputTemp,symTemp,&
                        iUnit,iterTemp,frTemp,fpwTemp,fzTemp,fzxyTemp)
            CALL wrtdop(starsTemp,vacuumTemp,atomsTemp,sphharTemp,inputTemp,symTemp,&
                        iUnitTemp,iterTemp,frTemp,fpwTemp,fzTemp,fzxyTemp)

            CLOSE(iUnitTemp)
            REWIND iUnit

            DEALLOCATE (fzTemp, frTemp, fzxyTemp, fpwTemp)
            DEALLOCATE (atomsTemp%neq, atomsTemp%jri, atomsTemp%zatom, atomsTemp%ntypsy, sphharTemp%nlh)
            DEALLOCATE (atomsTemp%rmt, atomsTemp%dx)
         END IF

460
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

            ! Generate data in temp arrays and variables to be able to perform loddop call.
            ! loddop is called to move the file position to the output density position.
            inputTemp%jspins = input%jspins
            vacuumTemp%nmzxyd = vacuum%nmzxyd
            atomsTemp%jmtd = atoms%jmtd
            sphharTemp%nlhd = sphhar%nlhd
            vacuumTemp%nmzd = vacuum%nmzd
            atomsTemp%ntype = atoms%ntype
            ALLOCATE (sphharTemp%nlh(SIZE(sphhar%nlh)))
            sphharTemp%nlh(:) = sphhar%nlh(:)
            ALLOCATE (atomsTemp%ntypsy(SIZE(atoms%ntypsy)))
            atomsTemp%ntypsy(:) = atoms%ntypsy(:)
            ALLOCATE (atomsTemp%jri(SIZE(atoms%jri)))
            atomsTemp%jri(:) = atoms%jri(:)
            ALLOCATE (atomsTemp%neq(SIZE(atoms%neq)))
            atomsTemp%neq(:) = atoms%neq(:)
            starsTemp%ng3 = stars%ng3
            symTemp%invs = sym%invs
            inputTemp%film = input%film
            vacuumTemp%nvac = vacuum%nvac
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
484 485
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
486 487 488 489 490 491 492 493 494 495 496 497
            ALLOCATE (frTemp(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins))
            ALLOCATE (fzTemp(vacuum%nmzd,2,input%jspins))

            CALL loddop(starsTemp,vacuumTemp,atomsTemp,sphharTemp,inputTemp,symTemp,&
                        iUnit,iterTemp,frTemp,fpwTemp,fzTemp,fzxyTemp)

            DEALLOCATE (fzTemp, frTemp, fzxyTemp, fpwTemp)
            DEALLOCATE (atomsTemp%neq, atomsTemp%jri, atomsTemp%ntypsy, sphharTemp%nlh)
         END IF

         ! Write the density
         CALL wrtdop(stars,vacuum,atoms,sphhar, input,sym,&
498
                     iUnit,iter+relCdnIndex,fr,fpw,fz,fzxy)
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

         ! Write additional data if l_noco
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            WRITE (iUnit) (cdom(k),k=1,stars%ng3)
            IF (input%film) THEN
               WRITE (iUnit) ((cdomvz(i,iVac),i=1,vacuum%nmz),iVac=1,vacuum%nvac)
               WRITE (iUnit) (((cdomvxy(i,j-1,iVac),i=1,vacuum%nmzxy),j=2,oneD%odi%nq2), iVac=1,vacuum%nvac)
            END IF
         END IF

         CLOSE(iUnit)
      END IF

   END SUBROUTINE writeDensity

514 515 516 517 518 519 520 521 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
   SUBROUTINE readPrevEFermi(eFermiPrev,l_error)

      REAL,    INTENT(OUT) :: eFermiPrev
      LOGICAL, INTENT(OUT) :: l_error

      INTEGER        :: mode
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
      INTEGER        :: currentStructureIndex
      INTEGER        :: readDensityIndex, lastDensityIndex

      INTEGER           :: starsIndex, latharmsIndex, structureIndex
      INTEGER           :: iter, jspins, previousDensityIndex
      REAL              :: fermiEnergy
      LOGICAL           :: l_qfix, l_exist
      CHARACTER(LEN=30) :: archiveName

      CALL getMode(mode)

      eFermiPrev = 0.0
      l_error = .FALSE.
      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                          readDensityIndex,lastDensityIndex)
         WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
         CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_UNDEFINED_const,&
                                  iter, starsIndex, latharmsIndex, structureIndex,&
                                  previousDensityIndex, jspins, fermiEnergy, l_qfix)
         archiveName = ''
         WRITE(archiveName,'(a,i0)') '/cdn-', previousDensityIndex
         l_exist = isDensityEntryPresentHDF(fileID,archiveName,DENSITY_TYPE_NOCO_OUT_const)
         IF(l_exist) THEN
            CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_NOCO_OUT_const,&
                                     iter, starsIndex, latharmsIndex, structureIndex,&
                                     previousDensityIndex, jspins, fermiEnergy, l_qfix)
            eFermiPrev = fermiEnergy
         ELSE
            l_error = .TRUE.
         END IF
556
         CALL closeCDNPOT_HDF(fileID)
557 558 559 560 561 562 563 564 565
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         STOP 'cdn.str not yet implemented!'
      ELSE
         l_error = .TRUE.
      END IF

   END SUBROUTINE

566 567 568 569 570 571 572 573 574 575 576 577 578 579
   SUBROUTINE readCoreDensity(input,atoms,dimension,rhcs,tecs,qints)

      TYPE(t_atoms),INTENT(IN)     :: atoms
      TYPE(t_input),INTENT(IN)     :: input
      TYPE(t_dimension),INTENT(IN) :: DIMENSION

      REAL, INTENT(OUT) :: rhcs(atoms%jmtd,atoms%ntype,DIMENSION%jspd)
      REAL, INTENT(OUT) :: tecs(atoms%ntype,DIMENSION%jspd)
      REAL, INTENT(OUT) :: qints(atoms%ntype,DIMENSION%jspd)

      INTEGER            :: mode, iUnit, iSpin, iAtom, i
      LOGICAL            :: l_exist
      CHARACTER(LEN=30)  :: filename

580 581 582 583
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
584 585
      INTEGER        :: currentStructureIndex
      INTEGER        :: readDensityIndex, lastDensityIndex
586

587 588 589
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
590 591
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
592
         IF (l_exist) THEN
593 594
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             readDensityIndex,lastDensityIndex)
595
            CALL readCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
596
            CALL closeCDNPOT_HDF(fileID)
597 598
            RETURN
         ELSE
599
            WRITE(*,*) 'No core density is available in HDF5 format.'
600 601 602
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
603
#endif
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      END IF

      IF(mode.EQ.CDN_STREAM_MODE) THEN
         INQUIRE(FILE='cdn.str',EXIST=l_exist)
         IF (l_exist) THEN
            !load density from cdn.str and exit subroutine

            RETURN
         ELSE
            WRITE(*,*) 'cdn.str file not found.'
            WRITE(*,*) 'Falling back to direct access file cdnc.'
            mode = CDN_DIRECT_MODE
         END IF
      END IF

      IF (mode.EQ.CDN_DIRECT_MODE) THEN
         filename = 'cdnc'
         INQUIRE(file=TRIM(ADJUSTL(filename)),EXIST=l_exist)
         IF(.NOT.l_exist) THEN
            CALL juDFT_error("core charge density file "//TRIM(ADJUSTL(filename))//" missing",calledby ="readCoreDensity")
         END IF
         iUnit = 17
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),form='unformatted',status='unknown')
         DO iSpin = 1,input%jspins
            DO iAtom = 1,atoms%ntype
               READ (iUnit) (rhcs(i,iAtom,iSpin),i=1,atoms%jri(iAtom))
               READ (iUnit) tecs(iAtom,iSpin)
            END DO
            READ (iUnit) (qints(iAtom,iSpin),iAtom=1,atoms%ntype)
         END DO
         CLOSE (iUnit)
      END IF

   END SUBROUTINE readCoreDensity

   SUBROUTINE writeCoreDensity(input,atoms,dimension,rhcs,tecs,qints)

      TYPE(t_atoms),INTENT(IN)     :: atoms
      TYPE(t_input),INTENT(IN)     :: input
      TYPE(t_dimension),INTENT(IN) :: DIMENSION

      REAL, INTENT(IN) :: rhcs(atoms%jmtd,atoms%ntype,DIMENSION%jspd)
      REAL, INTENT(IN) :: tecs(atoms%ntype,DIMENSION%jspd)
      REAL, INTENT(IN) :: qints(atoms%ntype,DIMENSION%jspd)

      INTEGER :: mode, iUnit, iSpin, iAtom, i

651 652 653 654
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
655 656
      INTEGER        :: currentStructureIndex
      INTEGER        :: readDensityIndex, lastDensityIndex
657

658 659 660
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
661
#ifdef CPP_HDF
662 663
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                          readDensityIndex,lastDensityIndex)
664
         CALL writeCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
665
         CALL closeCDNPOT_HDF(fileID)
666
#endif
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         ! Write core density to cdn.str file
         STOP 'CDN_STREAM_MODE not yet implemented!'
      ELSE
         iUnit = 17
         OPEN (iUnit,file='cdnc',form='unformatted',status='unknown')
         DO iSpin = 1,input%jspins
            DO iAtom = 1,atoms%ntype
               WRITE (iUnit) (rhcs(i,iAtom,iSpin),i=1,atoms%jri(iAtom))
               WRITE (iUnit) tecs(iAtom,iSpin)
            END DO
            WRITE (iUnit) (qints(iAtom,iSpin),iAtom=1,atoms%ntype)
         END DO
         CLOSE (iUnit)
      END IF

   END SUBROUTINE writeCoreDensity

685 686 687 688 689
   SUBROUTINE writeStars(stars,l_xcExtended,l_ExtData)

      TYPE(t_stars),INTENT(IN)   :: stars
      LOGICAL, INTENT(IN)        :: l_xcExtended, l_ExtData

690 691 692 693 694 695 696
      INTEGER        :: mode, ngz, izmin, izmax

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
      INTEGER        :: readDensityIndex,lastDensityIndex
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
697 698 699 700 701 702 703 704 705 706 707 708

      INTEGER :: igz(stars%ng3)

      ngz = 0
      izmin = 0
      izmax = 0
      igz = 0

      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
709 710 711 712 713 714 715 716 717 718
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                          readDensityIndex,lastDensityIndex)

         currentStarsIndex = currentStarsIndex + 1
         CALL writeStarsHDF(fileID, currentStarsIndex, stars)

         CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,&
                                 currentStructureIndex,readDensityIndex,lastDensityIndex)

         CALL closeCDNPOT_HDF(fileID)
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         ! Write stars to stars file
         STOP 'CDN_STREAM_MODE not yet implemented!'
      ELSE
         OPEN (51,file='stars',form='unformatted',status='unknown')
         WRITE (51) stars%gmax,stars%ng3,stars%ng2,ngz,izmin,izmax,stars%mx1,stars%mx2,stars%mx3
         IF(l_ExtData) THEN
            IF (.NOT.l_xcExtended) THEN
               WRITE (51) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%phi2,stars%kv3,stars%kv2,&
                          stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                          stars%igfft2,stars%pgfft2
            ELSE
               WRITE (51) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%phi2,stars%kv3,stars%kv2,&
                          stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                          stars%igfft2,stars%pgfft2,stars%ft2_gfx,stars%ft2_gfy
            END IF
         ELSE
            IF (.NOT.l_xcExtended) THEN
               WRITE (51) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%kv3,stars%kv2,&
                          stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                          stars%igfft2,stars%pgfft2
            ELSE
               WRITE (51) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%kv3,stars%kv2,&
                          stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                          stars%igfft2,stars%pgfft2,stars%ft2_gfx,stars%ft2_gfy
            END IF
         END IF
         CLOSE (51)
      END IF
   END SUBROUTINE writeStars

   SUBROUTINE readStars(stars,l_xcExtended,l_ExtData,l_error)
752

753 754 755 756 757 758 759
      TYPE(t_stars),INTENT(INOUT) :: stars
      LOGICAL, INTENT(IN)         :: l_xcExtended,l_ExtData
      LOGICAL, INTENT(OUT)        :: l_error

      INTEGER                     :: mode, ioStatus, ngz,izmin,izmax
      LOGICAL                     :: l_exist

760 761 762 763 764 765
      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
      INTEGER        :: readDensityIndex,lastDensityIndex
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

766 767 768 769 770 771 772 773 774 775 776 777 778 779
      INTEGER :: igz(stars%ng3)

      l_error = .FALSE.
      ngz = 0
      izmin = 0
      izmax = 0
      igz = 0

      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
#ifdef CPP_HDF
780 781 782 783 784 785 786 787
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             readDensityIndex,lastDensityIndex)
            IF (currentStarsIndex.LT.1) THEN
               mode = CDN_DIRECT_MODE ! (no stars entry found in cdn.hdf file)
            ELSE
               CALL readStarsHDF(fileID, currentStarsIndex, stars)
            END IF
            CALL closeCDNPOT_HDF(fileID)
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
#endif
         END IF
         IF(.NOT.l_exist) THEN
            mode = CDN_STREAM_MODE
         END IF
      END IF

      IF(mode.EQ.CDN_STREAM_MODE) THEN
         INQUIRE(FILE='cdn.str',EXIST=l_exist)
         IF (l_exist) THEN
            STOP 'cdn.str code path not yet implemented!'
         END IF
         IF (.NOT.l_exist) THEN
            mode = CDN_DIRECT_MODE
         END IF
      END IF

      IF (mode.EQ.CDN_DIRECT_MODE) THEN
         INQUIRE(FILE='stars',EXIST=l_exist)
         IF(.NOT.l_exist) THEN
            l_error = .TRUE.
            RETURN
         END IF
         OPEN (51,file='stars',form='unformatted',status='unknown')

         READ (51,IOSTAT=ioStatus) stars%gmax,stars%ng3,stars%ng2,ngz,izmin,izmax,stars%mx1,stars%mx2,stars%mx3
         IF (ioStatus.NE.0) THEN
            l_error = .TRUE.
            RETURN
         END IF

         IF (l_ExtData) THEN
            IF (.NOT.l_xcExtended) THEN
               READ (51,IOSTAT=ioStatus) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%phi2,stars%kv3,stars%kv2,&
                                         stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                                         stars%igfft2,stars%pgfft2
               stars%ft2_gfx = 0.0
               stars%ft2_gfy = 0.0
            ELSE
               READ (51,IOSTAT=ioStatus) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%phi2,stars%kv3,stars%kv2,&
                                         stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                                         stars%igfft2,stars%pgfft2,stars%ft2_gfx,stars%ft2_gfy
            END IF
         ELSE
            IF (.NOT.l_xcExtended) THEN
               READ (51,IOSTAT=ioStatus) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%kv3,stars%kv2,&
                                         stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                                         stars%igfft2,stars%pgfft2
               stars%ft2_gfx = 0.0
               stars%ft2_gfy = 0.0
            ELSE
               READ (51,IOSTAT=ioStatus) stars%nstr,stars%nstr2,stars%rgphs,stars%sk3,stars%sk2,stars%kv3,stars%kv2,&
                                         stars%ig,stars%ig2,igz,stars%kimax,stars%igfft,stars%pgfft,stars%kimax2,&
                                         stars%igfft2,stars%pgfft2,stars%ft2_gfx,stars%ft2_gfy
            END IF
         END IF

         IF (ioStatus.NE.0) THEN
            l_error = .TRUE.
            RETURN
         END IF

         CLOSE (51)
      END IF

   END SUBROUTINE readStars

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
   SUBROUTINE writeStepfunction(stars)

      TYPE(t_stars),INTENT(IN) :: stars

      INTEGER                  :: mode, ifftd, i

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
      INTEGER        :: readDensityIndex,lastDensityIndex
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

      CALL getMode(mode)

      WRITE(*,*) 'temporary fallback to direct mode for stepfunction file!'
      mode = CDN_DIRECT_MODE

      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                          readDensityIndex,lastDensityIndex)

         CALL closeCDNPOT_HDF(fileID)
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         ! Write stars to stars file
         STOP 'CDN_STREAM_MODE not yet implemented!'
      ELSE
         OPEN (14,file='wkf2',form='unformatted',status='unknown')

         WRITE (14) stars%ng3,ifftd
         WRITE (14) (stars%ustep(i),i=1,stars%ng3)
         WRITE (14) (stars%ufft(i),i=0,ifftd-1)

         CLOSE (14)
      END IF

   END SUBROUTINE writeStepfunction

   SUBROUTINE readStepfunction(stars, l_error)

      TYPE(t_stars),INTENT(INOUT) :: stars
      LOGICAL, INTENT(OUT)        :: l_error

      INTEGER        :: mode
      INTEGER        :: ifftd, ng3Temp, ifftdTemp, ioStatus, i
      LOGICAL        :: l_exist

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
      INTEGER        :: readDensityIndex,lastDensityIndex
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

      l_error = .FALSE.
      ioStatus = 0
      ifftd = 27*stars%mx1*stars%mx2*stars%mx3

      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
            WRITE(*,*) 'temporary fallback to direct mode for stepfunction file!'
            mode = CDN_DIRECT_MODE
#ifdef CPP_HDF
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             readDensityIndex,lastDensityIndex)

            CALL closeCDNPOT_HDF(fileID)
#endif
         END IF
         IF(.NOT.l_exist) THEN
            mode = CDN_STREAM_MODE
         END IF
      END IF

      IF(mode.EQ.CDN_STREAM_MODE) THEN
         INQUIRE(FILE='cdn.str',EXIST=l_exist)
         IF (l_exist) THEN
            STOP 'cdn.str code path not yet implemented!'
         END IF
         IF (.NOT.l_exist) THEN
            mode = CDN_DIRECT_MODE
         END IF
      END IF

      IF (mode.EQ.CDN_DIRECT_MODE) THEN
         INQUIRE(FILE='wkf2',EXIST=l_exist)
         IF(.NOT.l_exist) THEN
            l_error = .TRUE.
            RETURN
         END IF
         OPEN (14,file='wkf2',form='unformatted',status='unknown')
949
         ng3temp=0;ifftdTemp=0
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
         READ (14,IOSTAT=ioStatus) ng3Temp, ifftdTemp
         IF (ng3Temp.NE.stars%ng3) ioStatus = 1
         IF (ifftdTemp.NE.ifftd) ioStatus = 1
         IF (ioStatus.NE.0) THEN
            l_error = .TRUE.
            CLOSE (14)
            RETURN
         END IF
         READ (14) (stars%ustep(i),i=1,stars%ng3)
         READ (14) (stars%ufft(i),i=0,ifftd-1)

         CLOSE (14)
      END IF

   END SUBROUTINE readStepfunction

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
   SUBROUTINE setStartingDensity(l_noco)

      LOGICAL,INTENT(IN) :: l_noco

#ifdef CPP_HDF
      INTEGER(HID_T)    :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
      INTEGER           :: currentStructureIndex
      INTEGER           :: readDensityIndex, lastDensityIndex
      INTEGER           :: sdIndex, ioStatus, mode
      INTEGER           :: densityType
      CHARACTER(LEN=20) :: numberString
      CHARACTER(LEN=30) :: archiveName
      LOGICAL           :: l_exist

      IF (.NOT.juDFT_was_argument("-sd")) THEN
         RETURN
      END IF
      numberString = juDFT_string_for_argument("-sd")
      IF (TRIM(ADJUSTL(numberString)).EQ.'') THEN
         CALL juDFT_error("No number for starting density set in command line.",calledby ="setStartingDensity")
      END IF
      ioStatus = 0
      READ(numberString,'(i8)',iostat=ioStatus) sdIndex
      IF(ioStatus.NE.0) THEN
         CALL juDFT_error("Could not convert starting density index string to number.",calledby ="setStartingDensity")
      END IF

      WRITE(*,'(a,i0,a)') 'Using density ', sdIndex, ' as starting density.'

      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                          readDensityIndex,lastDensityIndex)
         densityType = DENSITY_TYPE_IN_const
         IF(l_noco) THEN
            densityType = DENSITY_TYPE_NOCO_IN_const
         END IF
         archiveName = ''
         WRITE(archiveName,'(a,i0)') '/cdn-', sdIndex
         l_exist = isDensityEntryPresentHDF(fileID,archiveName,densityType)
         IF(.NOT.l_exist) THEN
            WRITE(*,*) 'archiveName: ', TRIM(ADJUSTL(archiveName))
            CALL juDFT_error("For selected starting density index no in-density is present.",calledby ="setStartingDensity")
         END IF
1014 1015 1016
         CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,&
                                 currentStructureIndex,sdIndex,lastDensityIndex)
         CALL closeCDNPOT_HDF(fileID)
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         STOP 'CDN_STREAM_MODE not yet implemented!'
      ELSE      
         WRITE(*,*) 'Explicit setting of starting density in direct access mode'
         WRITE(*,*) 'not implemented.'
         WRITE(*,*) ''
         WRITE(*,*) 'Ignoring -sd command line argument.'
      END IF

   END SUBROUTINE setStartingDensity
1028

1029 1030 1031 1032
   SUBROUTINE getMode(mode)
      INTEGER, INTENT(OUT) :: mode

      mode = CDN_DIRECT_MODE
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
      IF (juDFT_was_argument("-stream_cdn")) THEN
         mode=CDN_STREAM_MODE
      END IF
      IF (juDFT_was_argument("-hdf_cdn")) THEN
#ifdef CPP_HDF
         mode=CDN_HDF5_MODE
#else
         WRITE(*,*) 'Code not compiled with HDF5 support.'
         WRITE(*,*) 'Falling back to direct access.'
#endif
      END IF
1044 1045
   END SUBROUTINE getMode

1046 1047 1048 1049 1050 1051 1052
   LOGICAL FUNCTION isDensityFilePresent(archiveType)

      INTEGER, INTENT(IN) :: archiveType

      LOGICAL             :: l_exist
      INTEGER             :: mode

1053 1054 1055 1056 1057 1058
      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
      INTEGER        :: readDensityIndex,lastDensityIndex
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

1059 1060 1061 1062 1063
      CALL getMode(mode)

      IF (mode.EQ.CDN_HDF5_MODE) THEN
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF(l_exist) THEN
1064 1065 1066 1067 1068 1069 1070 1071 1072
#ifdef CPP_HDF
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             readDensityIndex,lastDensityIndex)
            CALL closeCDNPOT_HDF(fileID)
            IF(readDensityIndex.GT.0) THEN
               isDensityFilePresent = .TRUE.
               RETURN
            END IF
#endif
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
         END IF
      END IF

      IF ((mode.EQ.CDN_STREAM_MODE).OR.(mode.EQ.CDN_HDF5_MODE)) THEN
         INQUIRE(FILE='cdn.str',EXIST=l_exist)
         IF(l_exist) THEN
            isDensityFilePresent = l_exist
            RETURN
         END IF
      END IF

      !cdn1 or rhomat_inp should be enough for any mode...
      INQUIRE(FILE='cdn1',EXIST=l_exist)
      IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const) THEN
         isDensityFilePresent = l_exist
         RETURN
      END IF
      IF (archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const) THEN
         CALL juDFT_error("Illegal archive type selected.",calledby ="isDensityFilePresent")
      END IF
      IF (l_exist) THEN
         isDensityFilePresent = l_exist
         RETURN
      END IF
      INQUIRE(FILE='rhomat_inp',EXIST=l_exist)
      isDensityFilePresent = l_exist
   END FUNCTION isDensityFilePresent

1101 1102 1103 1104 1105 1106 1107 1108
   LOGICAL FUNCTION isCoreDensityPresent()

      LOGICAL             :: l_exist
      INTEGER             :: mode

      CALL getMode(mode)

      IF (mode.EQ.CDN_HDF5_MODE) THEN
1109 1110
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
1111
         IF(l_exist) THEN
1112
            isCoreDensityPresent = l_exist
1113 1114
            RETURN
         END IF
1115
#endif
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
      END IF

      IF ((mode.EQ.CDN_STREAM_MODE).OR.(mode.EQ.CDN_HDF5_MODE)) THEN
         INQUIRE(FILE='cdn.str',EXIST=l_exist)
         IF(l_exist) THEN
            STOP 'Not yet implemented!'
            RETURN
         END IF
      END IF

      !cdnc should be enough for any mode...
      INQUIRE(FILE='cdnc',EXIST=l_exist)
      IF (l_exist) THEN
         isCoreDensityPresent = l_exist
         RETURN
      END IF
      isCoreDensityPresent = .FALSE.
   END FUNCTION isCoreDensityPresent


1136
END MODULE m_cdn_io