cdn_io.F90 58.3 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
   USE m_cdnpot_io_hdf
22
   USE m_cdnpot_io_common
23
   USE m_constants
24 25 26
#ifdef CPP_HDF
   USE hdf5
#endif
27 28 29
   IMPLICIT NONE

   PRIVATE
30
   PUBLIC printDensityFileInfo
31 32
   PUBLIC readDensity, writeDensity
   PUBLIC isDensityFilePresent, isCoreDensityPresent
33
   PUBLIC isDensityMatrixPresent
34
   PUBLIC readCoreDensity, writeCoreDensity
35
   PUBLIC readDensityMatrix
36
   PUBLIC readStars, writeStars
37
   PUBLIC readStepfunction, writeStepfunction
38
   PUBLIC setStartingDensity, readPrevEFermi, deleteDensities
39
   PUBLIC storeStructureIfNew
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
   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

57 58 59 60 61 62 63 64 65 66 67 68 69
   SUBROUTINE printDensityFileInfo()

      INTEGER            :: mode, i
      LOGICAL            :: l_exist

#ifdef CPP_HDF
      INTEGER(HID_T)    :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
      INTEGER           :: currentStructureIndex,currentStepfunctionIndex
      INTEGER           :: readDensityIndex, lastDensityIndex
      CHARACTER(LEN=30) :: archiveName

70
      INTEGER           :: dateTemp, timeTemp
71 72 73
      INTEGER           :: iterTemp, starsIndexTemp, latharmsIndexTemp 
      INTEGER           :: structureIndexTemp,stepfunctionIndexTemp
      INTEGER           :: previousDensityIndex, jspinsTemp
74
      REAL              :: fermiEnergyTemp, distanceTemp
75
      LOGICAL           :: l_qfixTemp
76 77 78 79
      CHARACTER(LEN=10) :: dateString
      CHARACTER(LEN=10) :: timeString
      CHARACTER(LEN=19) :: timeStampString
      CHARACTER(LEN=15) :: distanceString
80 81 82

      CALL getMode(mode)

83
      WRITE(*,*) 'Available densities info:'
84 85 86 87 88 89 90 91
      WRITE(*,*)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
92
            WRITE(*,*) 'densityIndex   iteration   prevDensity   prevDistance        timeStamp'
93 94 95 96 97 98 99 100 101 102 103 104
            DO i = 1, lastDensityIndex
               archiveName = ''
               WRITE(archiveName,'(a,i0)') '/cdn-', i

               l_exist = isDensityEntryPresentHDF(fileID,archiveName,DENSITY_TYPE_UNDEFINED_const)
               IF(.NOT.l_exist) THEN
                  CYCLE
               END IF

               CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_UNDEFINED_const,&
                                        iterTemp, starsIndexTemp, latharmsIndexTemp, structureIndexTemp,&
                                        stepfunctionIndexTemp,previousDensityIndex, jspinsTemp,&
105
                                        dateTemp, timeTemp, distanceTemp, fermiEnergyTemp, l_qfixTemp)
106

107 108
               WRITE(dateString,'(i8)') dateTemp
               WRITE(timeString,'(i6)') timeTemp
109

110 111 112 113 114 115 116 117 118 119 120
               distanceString = ''
               IF (distanceTemp.GE.-1e-10) THEN
                  WRITE(distanceString,'(f15.8)') distanceTemp
               END IF

               WRITE(timeStampString,'(a4,a1,a2,a1,a2,1x,a2,a1,a2,a1,a2)') &
                  dateString(1:4),'/',dateString(5:6),'/',dateString(7:8),&
                  timeString(1:2),':',timeString(3:4),':',timeString(5:6)

               WRITE(*,'(1x,i7,6x,i7,7x,i7,4x,a15,3x,a)') i, iterTemp, previousDensityIndex, distanceString,&
                                                            TRIM(ADJUSTL(timeStampString))
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            END DO
            CALL closeCDNPOT_HDF(fileID)
         ELSE
            WRITE(*,'(a)') "No cdn.hdf file found. Density file info is not available."
         END IF
#else
         WRITE(*,'(a)') "Fleur is not compiled with HDF5 support. Density file info is not available."
#endif
      ELSE
         WRITE(*,'(a)') "Density file info is only available if '-hdf_cdn' switch is used."
      END IF

   END SUBROUTINE printDensityFileInfo


136
   SUBROUTINE readDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
137 138 139 140 141 142 143 144 145 146 147
                          relCdnIndex,fermiEnergy,l_qfix,den)

      TYPE(t_stars),INTENT(IN)     :: stars
      TYPE(t_vacuum),INTENT(IN)    :: vacuum
      TYPE(t_atoms),INTENT(IN)     :: atoms
      TYPE(t_cell), INTENT(IN)     :: cell
      TYPE(t_sphhar),INTENT(IN)    :: sphhar
      TYPE(t_input),INTENT(IN)     :: input
      TYPE(t_sym),INTENT(IN)       :: sym
      TYPE(t_oneD),INTENT(IN)      :: oneD
      TYPE(t_potden),INTENT(INOUT) :: den
148 149 150 151

      INTEGER, INTENT (IN)      :: inOrOutCDN
      INTEGER, INTENT (IN)      :: relCdnIndex
      INTEGER, INTENT (IN)      :: archiveType
152 153
      REAL,    INTENT (OUT)     :: fermiEnergy
      LOGICAL, INTENT (OUT)     :: l_qfix
154 155

      ! local variables
156
      INTEGER            :: mode, datend, k, i, iVac, j, iUnit, l
157
      LOGICAL            :: l_exist, l_rhomatFile, l_DimChange
158
      CHARACTER(LEN=30)  :: filename
159

160 161 162 163
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
164
      INTEGER           :: currentStructureIndex,currentStepfunctionIndex
165 166
      INTEGER           :: readDensityIndex, lastDensityIndex
      INTEGER           :: previousDensityIndex, densityType
167 168 169
      CHARACTER(LEN=30) :: archiveName
      TYPE(t_cell)      :: cellTemp

170 171 172
      fermiEnergy = 0.0
      l_qfix = .FALSE.

173 174 175
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
176 177 178 179 180
#ifdef CPP_HDF

         densityType = 0
         archiveName = ''

181 182
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
183
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
184
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
185 186 187 188

            IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
               archiveName = 'cdn'
            ELSE
189
               WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
            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
207
                  CALL juDFT_error("Invalid inOrOutCDN selected.",calledby ="readDensity")
208 209
            END SELECT
            l_exist = isDensityEntryPresentHDF(fileID,archiveName,densityType)
210
            CALL closeCDNPOT_HDF(fileID)
211
         END IF
212

213
         IF (l_exist) THEN
214
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
215
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
216

217
            CALL readDensityHDF(fileID, input, stars, sphhar, atoms, vacuum, oneD, archiveName, densityType,&
218
                                fermiEnergy,l_qfix,l_DimChange,den%iter,den%mt,den%pw,den%vacz,den%vacxy,den%cdom,den%cdomvz,den%cdomvxy)
219

220
            CALL closeCDNPOT_HDF(fileID)
221 222 223

            IF(l_DimChange) THEN
               CALL writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
224
                           1,-1.0,fermiEnergy,l_qfix,den)
225
            END IF
226
         ELSE
227
            WRITE(*,*) 'cdn.hdf file or relevant density entry not found.'
228 229 230
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
231
#endif
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      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

         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'
248
         l_rhomatFile = .FALSE.
249 250 251
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            INQUIRE(file="rhomat_inp",EXIST=l_exist)
            IF (l_exist) filename = 'rhomat_inp'
252 253 254 255 256
            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.
257 258 259 260 261 262 263
         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
264
            CALL juDFT_error("charge density file "//TRIM(ADJUSTL(filename))//" missing",calledby ="readDensity")
265 266 267 268 269
         END IF

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

270
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
271 272
            ! call loddop to move the file position to the output density
            CALL loddop(stars,vacuum,atoms,sphhar,input,sym,&
273
                        iUnit,den%iter,den%mt,den%pw,den%vacz,den%vacxy)
274 275 276 277
         END IF

         ! read in the density
         CALL loddop(stars,vacuum,atoms,sphhar,input,sym,&
278
                     iUnit,den%iter,den%mt,den%pw,den%vacz,den%vacxy)
279 280

         ! read in additional data if l_noco and data is present
281
         IF ((archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const).AND.l_rhomatFile) THEN
282
            READ (iUnit,iostat=datend) (den%cdom(k),k=1,stars%ng3)
283 284
            IF (datend == 0) THEN
               IF (input%film) THEN
285 286
                  READ (iUnit) ((den%cdomvz(i,iVac),i=1,vacuum%nmz),iVac=1,vacuum%nvac)
                  READ (iUnit) (((den%cdomvxy(i,j-1,iVac),i=1,vacuum%nmzxy),j=2,oneD%odi%nq2), iVac=1,vacuum%nvac)
287 288 289 290 291 292 293 294
               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
295
               den%cdom = CMPLX(0.0,0.0)
296
               IF (input%film) THEN
297 298
                  den%cdomvz = CMPLX(0.0,0.0)
                  den%cdomvxy = CMPLX(0.0,0.0)
299 300 301
               END IF
            END IF
         ELSE IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
302
            den%cdom = CMPLX(0.0,0.0)
303
            IF (input%film) THEN
304 305
               den%cdomvz = CMPLX(0.0,0.0)
               den%cdomvxy = CMPLX(0.0,0.0)
306 307
            END IF
         END IF
308
         CLOSE(iUnit)
309 310 311 312
      END IF

   END SUBROUTINE readDensity

313
   SUBROUTINE writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
314
                           relCdnIndex,distance,fermiEnergy,l_qfix,den)
315 316 317 318

      TYPE(t_stars),INTENT(IN)  :: stars
      TYPE(t_vacuum),INTENT(IN) :: vacuum
      TYPE(t_atoms),INTENT(IN)  :: atoms
319
      TYPE(t_cell), INTENT(IN)  :: cell
320 321 322 323
      TYPE(t_sphhar),INTENT(IN) :: sphhar
      TYPE(t_input),INTENT(IN)  :: input
      TYPE(t_sym),INTENT(IN)    :: sym
      TYPE(t_oneD),INTENT(IN)   :: oneD
324
      TYPE(t_potden),INTENT(IN) :: den
325 326

      INTEGER, INTENT (IN)      :: inOrOutCDN
327
      INTEGER, INTENT (IN)      :: relCdnIndex
328
      INTEGER, INTENT (IN)      :: archiveType
329
      REAL,    INTENT (IN)      :: fermiEnergy, distance
330
      LOGICAL, INTENT (IN)      :: l_qfix
331 332 333 334 335 336 337

      TYPE(t_stars)        :: starsTemp
      TYPE(t_vacuum)       :: vacuumTemp
      TYPE(t_atoms)        :: atomsTemp
      TYPE(t_sphhar)       :: sphharTemp
      TYPE(t_input)        :: inputTemp
      TYPE(t_sym)          :: symTemp
338 339
      TYPE(t_cell)         :: cellTemp
      TYPE(t_oneD)         :: oneDTemp
340 341 342 343 344

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

      INTEGER           :: mode, iterTemp, k, i, iVac, j, iUnit
345
      INTEGER           :: d1, d10, asciioffset, iUnitTemp
346
      LOGICAL           :: l_exist, l_storeIndices, l_writeNew, l_same
347
      LOGICAL           :: l_writeAll
348
      CHARACTER(len=30) :: filename
349
      CHARACTER(len=5)  :: cdnfile
350

351 352 353 354
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
355
      INTEGER           :: currentStructureIndex,currentStepfunctionIndex
356 357 358
      INTEGER           :: readDensityIndex, writeDensityIndex, lastDensityIndex
      INTEGER           :: previousDensityIndex, densityType
      INTEGER           :: starsIndexTemp, latharmsIndexTemp, structureIndexTemp
359
      INTEGER           :: stepfunctionIndexTemp
360
      INTEGER           :: jspinsTemp
361
      INTEGER           :: date, time, dateTemp, timeTemp
362
      REAL              :: fermiEnergyTemp, distanceTemp
363
      LOGICAL           :: l_qfixTemp
364
      CHARACTER(LEN=30) :: archiveName
365 366 367
      CHARACTER(LEN=8)  :: dateString
      CHARACTER(LEN=10) :: timeString
      CHARACTER(LEN=10) :: zone
368

369
      CALL getMode(mode)
370 371 372
      CALL DATE_AND_TIME(dateString,timeString,zone)
      READ(dateString,'(i8)') date
      READ(timeString,'(i6)') time
373 374

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

379 380 381 382
         CALL checkAndWriteMetadataHDF(fileID, input, atoms, cell, vacuum, oneD, stars, sphhar, sym,&
                                       currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                       currentStepfunctionIndex,l_storeIndices)

383 384
         previousDensityIndex = readDensityIndex
         writeDensityIndex = readDensityIndex
385
         IF(relCdnIndex.NE.0) THEN
386 387 388
            writeDensityIndex = lastDensityIndex+relCdnIndex
            lastDensityIndex = writeDensityIndex
            readDensityIndex = writeDensityIndex
389 390 391 392 393 394 395
            l_storeIndices = .TRUE.
         END IF

         archiveName = ''
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            archiveName = 'cdn'
         ELSE
396
            WRITE(archiveName,'(a,i0)') '/cdn-', writeDensityIndex
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
         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

418 419 420 421 422
         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,&
423
                                        stepfunctionIndexTemp,previousDensityIndex, jspinsTemp,&
424
                                        dateTemp, timeTemp, distanceTemp, fermiEnergyTemp, l_qfixTemp)
425 426 427
            END IF
         END IF

428 429
         ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
         ALLOCATE (fzTemp(vacuum%nmzd,2,input%jspins))
430 431
         fzTemp(:,:,:) = den%vacz(:,:,:)
         fzxyTemp(:,:,:,:) = den%vacxy(:,:,:,:)
432 433 434 435 436 437 438 439 440
         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

441
         CALL writeDensityHDF(input, fileID, archiveName, densityType, previousDensityIndex,&
442
                              currentStarsIndex, currentLatharmsIndex, currentStructureIndex,&
443 444
                              currentStepfunctionIndex,date,time,distance,fermiEnergy,l_qfix,den%iter+relCdnIndex,&
                              den%mt,den%pw,fzTemp,fzxyTemp,den%cdom,den%cdomvz,den%cdomvxy)
445 446

         DEALLOCATE(fzTemp,fzxyTemp)
447 448

         IF(l_storeIndices) THEN
449 450
            CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                    currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
451 452
         END IF

453
         CALL closeCDNPOT_HDF(fileID)
454
#endif
455 456 457 458 459 460 461
      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'
462
            IF(inOrOutCDN.EQ.CDN_OUTPUT_DEN_const) filename = 'rhomat_out'
463 464 465 466 467
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

468
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(den%iter.EQ.0)) THEN
469 470 471 472 473 474
            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

475 476 477
         iUnit = 93
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),FORM='unformatted',STATUS='unknown')

478
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(den%iter.GE.1)) THEN
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
            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
Daniel Wortmann's avatar
Daniel Wortmann committed
503 504 505 506
            vacuumTemp%nmzxy = vacuum%nmzxy
            vacuumTemp%nmz = vacuum%nmz
            vacuumTemp%dvac = vacuum%dvac
            vacuumTemp%delz = vacuum%delz
507 508
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
509 510
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
511 512 513 514
            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
515 516
            d1 = MOD(den%iter,10)
            d10 = MOD(INT((den%iter+0.5)/10),10)
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
            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

543
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564

            ! 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
Daniel Wortmann's avatar
Daniel Wortmann committed
565 566 567 568
            vacuumTemp%nmzxy = vacuum%nmzxy
            vacuumTemp%nmz = vacuum%nmz
            vacuumTemp%dvac = vacuum%dvac
            vacuumTemp%delz = vacuum%delz
569 570
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
571 572
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
573 574 575 576 577 578 579 580 581 582 583 584
            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,&
585
                     iUnit,den%iter+relCdnIndex,den%mt,den%pw,den%vacz,den%vacxy)
586 587 588

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

         CLOSE(iUnit)
597 598 599 600 601 602 603

         !write density matrix to n_mmp_mat file
         IF((archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(relCdnIndex.EQ.1)) THEN
            IF(atoms%n_u.GT.0) THEN
            END IF
         END IF

604 605 606 607
      END IF

   END SUBROUTINE writeDensity

608 609 610 611 612 613 614 615 616 617
   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
618
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
619 620 621
      INTEGER        :: readDensityIndex, lastDensityIndex

      INTEGER           :: starsIndex, latharmsIndex, structureIndex
622
      INTEGER           :: stepfunctionIndex
623
      INTEGER           :: date, time, iter, jspins, previousDensityIndex
624
      REAL              :: fermiEnergy, distance
625 626 627 628 629 630 631 632 633 634
      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,&
635
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
636 637
         WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
         CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_UNDEFINED_const,&
638
                                  iter, starsIndex, latharmsIndex, structureIndex, stepfunctionIndex,&
639
                                  previousDensityIndex, jspins, date, time, distance, fermiEnergy, l_qfix)
640 641 642 643 644
         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,&
645
                                     iter, starsIndex, latharmsIndex, structureIndex, stepfunctionIndex,&
646
                                     previousDensityIndex, jspins, date, time, distance, fermiEnergy, l_qfix)
647 648 649 650
            eFermiPrev = fermiEnergy
         ELSE
            l_error = .TRUE.
         END IF
651
         CALL closeCDNPOT_HDF(fileID)
652 653 654 655 656 657 658 659 660
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         STOP 'cdn.str not yet implemented!'
      ELSE
         l_error = .TRUE.
      END IF

   END SUBROUTINE

661 662 663 664 665 666
   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

Daniel Wortmann's avatar
Daniel Wortmann committed
667 668 669
      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)
670 671 672 673 674

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

675 676 677 678
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
679
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
680
      INTEGER        :: readDensityIndex, lastDensityIndex
681

682 683 684
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
685 686
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
687
         IF (l_exist) THEN
688
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
689
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
690
            CALL readCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
691
            CALL closeCDNPOT_HDF(fileID)
692 693
            RETURN
         ELSE
694
            WRITE(*,*) 'No core density is available in HDF5 format.'
695 696 697
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
698
#endif
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
      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

Daniel Wortmann's avatar
Daniel Wortmann committed
740 741 742
      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)
743 744 745

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

746 747 748 749
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
750
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
751
      INTEGER        :: readDensityIndex, lastDensityIndex
752

753 754 755
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
756
#ifdef CPP_HDF
757
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
758
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
759
         CALL writeCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
760
         CALL closeCDNPOT_HDF(fileID)
761
#endif
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
      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

780
   SUBROUTINE storeStructureIfNew(input, atoms, cell, vacuum, oneD, sym)
781 782 783 784 785 786

      TYPE(t_input),INTENT(IN)   :: input
      TYPE(t_atoms), INTENT(IN)  :: atoms
      TYPE(t_cell), INTENT(IN)   :: cell
      TYPE(t_vacuum), INTENT(IN) :: vacuum
      TYPE(t_oneD),INTENT(IN)    :: oneD
787
      TYPE(t_sym),INTENT(IN)     :: sym
788 789 790 791 792 793

      TYPE(t_input)              :: inputTemp
      TYPE(t_atoms)              :: atomsTemp
      TYPE(t_cell)               :: cellTemp
      TYPE(t_vacuum)             :: vacuumTemp
      TYPE(t_oneD)               :: oneDTemp
794
      TYPE(t_sym)                :: symTemp
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

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

      CALL getMode(mode)

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

         IF (currentStructureIndex.EQ.0) THEN
            currentStructureIndex = currentStructureIndex + 1
            l_writeStructure = .TRUE.
         ELSE
816 817
            CALL readStructureHDF(fileID, inputTemp, atomsTemp, cellTemp, vacuumTemp, oneDTemp, symTemp, currentStructureIndex)
            CALL compareStructure(atoms, vacuum, cell, sym, atomsTemp, vacuumTemp, cellTemp, symTemp, l_same)
818
            IF(.NOT.l_same) THEN
819
               currentStructureIndex = currentStructureIndex + 1
820 821 822 823 824
               l_writeStructure = .TRUE.
            END IF
         END IF

         IF (l_writeStructure) THEN
825
            CALL writeStructureHDF(fileID, input, atoms, cell, vacuum, oneD, sym, currentStructureIndex)
826 827 828 829 830 831 832 833 834 835 836 837 838
            CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                    currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
         END IF

         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
         ! In direct access mode no structure information is written to any file.
      END IF

839
   END SUBROUTINE storeStructureIfNew
840

841 842 843 844 845
   SUBROUTINE writeStars(stars,l_xcExtended,l_ExtData)

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

846 847 848
      INTEGER        :: mode, ngz, izmin, izmax

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
849
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
850 851 852
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
853 854 855 856 857 858 859 860 861 862 863 864

      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
865
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
866
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
867 868

         currentStarsIndex = currentStarsIndex + 1
869
         CALL writeStarsHDF(fileID, currentStarsIndex, currentStructureIndex, stars)
870

871 872
         CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                 currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
873 874

         CALL closeCDNPOT_HDF(fileID)
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
#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)
908

909 910 911 912
      TYPE(t_stars),INTENT(INOUT) :: stars
      LOGICAL, INTENT(IN)         :: l_xcExtended,l_ExtData
      LOGICAL, INTENT(OUT)        :: l_error

913 914

      TYPE(t_stars)               :: starsTemp
915
      INTEGER                     :: mode, ioStatus, ngz,izmin,izmax
916
      LOGICAL                     :: l_exist, l_same
917

918
      INTEGER        :: structureIndexTemp
919
      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
920
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
921 922 923 924
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

925 926 927 928 929 930 931 932 933 934 935 936 937 938
      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
939
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
940
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
941 942 943
            IF (currentStarsIndex.LT.1) THEN
               mode = CDN_DIRECT_MODE ! (no stars entry found in cdn.hdf file)
            ELSE
944 945 946 947 948 949
               CALL peekStarsHDF(fileID, currentStarsIndex, structureIndexTemp)
               l_same = structureIndexTemp.EQ.currentStructureIndex
               IF(l_same) THEN
                  CALL readStarsHDF(fileID, currentStarsIndex, starsTemp)
                  CALL compareStars(stars, starsTemp, l_same)
               END IF
950 951 952 953 954
               IF(l_same) THEN
                  CALL readStarsHDF(fileID, currentStarsIndex, stars)
               ELSE
                 mode = CDN_DIRECT_MODE ! (no adequate stars entry found in cdn.hdf file)
               END IF
955 956
            END IF
            CALL closeCDNPOT_HDF(fileID)
957 958 959 960 961 962 963 964 965 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 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
#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

1024 1025 1026 1027 1028 1029 1030
   SUBROUTINE writeStepfunction(stars)

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

      INTEGER                  :: mode, ifftd, i

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
1031
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
1032 1033 1034 1035
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

Daniel Wortmann's avatar
Daniel Wortmann committed
1036
      ifftd=size(stars%ufft)
1037

Daniel Wortmann's avatar
Daniel Wortmann committed
1038
      CALL getMode(mode)
1039 1040 1041 1042

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

         currentStepfunctionIndex = currentStepfunctionIndex + 1
1046
         CALL writeStepfunctionHDF(fileID, currentStepfunctionIndex, currentStarsIndex, currentStructureIndex, stars)
1047 1048
         CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                 currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

         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

1067
   SUBROUTINE readStepfunction(stars, atoms, cell, vacuum, l_error)
1068

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
      TYPE(t_stars),INTENT(INOUT)   :: stars
      TYPE(t_atoms), INTENT(IN)     :: atoms
      TYPE(t_cell), INTENT(IN)      :: cell
      TYPE(t_vacuum), INTENT(IN)    :: vacuum
      LOGICAL, INTENT(OUT)          :: l_error

      TYPE(t_stars)                 :: starsTemp
      TYPE(t_input)                 :: inputTemp
      TYPE(t_atoms)                 :: atomsTemp
      TYPE(t_cell)                  :: cellTemp
      TYPE(t_vacuum)                :: vacuumTemp
      TYPE(t_oneD)                  :: oneDTemp
1081 1082

      INTEGER        :: mode
1083
      INTEGER        :: ifftd, ng3Temp, ifftdTemp, ioStatus, i, starsIndexTemp
1084 1085
      INTEGER        :: structureIndexTemp
      LOGICAL        :: l_exist, l_same, l_sameTemp
1086 1087

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
1088
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
#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
#ifdef CPP_HDF
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
1104 1105
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
            IF(currentStepfunctionIndex.GT.0) THEN
1106
               CALL peekStepfunctionHDF(fileID, currentStepfunctionIndex, starsIndexTemp, structureIndexTemp)
1107
               IF((starsIndexTemp.EQ.currentStarsIndex).AND.(structureIndexTemp.EQ.currentStructureIndex)) THEN
1108 1109 1110 1111
                  CALL readStepfunctionHDF(fileID, currentStepfunctionIndex, stars)
               ELSE
                  mode = CDN_STREAM_MODE ! No adequate stepfunction entry found. Fall back to other IO modes.
               END IF
1112 1113 1114
            ELSE
               mode = CDN_STREAM_MODE ! No adequate stepfunction entry found. Fall back to other IO modes.
            END IF
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
            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')
1140
         ng3temp=0;ifftdTemp=0
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
         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

1157 1158
   SUBROUTINE readDensityMatrix(input, atoms, n_mmp, l_error)

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
      TYPE(t_input), INTENT(INOUT) :: input
      TYPE(t_atoms), INTENT(IN)    :: atoms
      COMPLEX, INTENT(OUT)         :: n_mmp(-lmaxU_const:lmaxU_const,-lmaxU_const:lmaxU_const,&
                                            -lmaxU_const:lmaxU_const,-lmaxU_const:lmaxU_const,&
                                            atoms%n_u,input%jspins)
      LOGICAL, INTENT(OUT)         :: l_error

      INTEGER                      :: mode, ioStatus, numLines, iofl, i
      LOGICAL                      :: l_exist
      CHARACTER(LEN=30)            :: filename
      REAL                         :: alpha, spinf
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

      l_error = .FALSE.
      ioStatus = 0
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
         l_exist = .FALSE.
         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
         filename = 'n_mmp_mat'
         INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
         IF(.NOT.l_exist) THEN
            l_error = .TRUE.
            RETURN
         END IF

         OPEN (69,file=TRIM(ADJUSTL(filename)),status='unknown',form='formatted')
         READ (69,'(7f20.13)',IOSTAT=ioStatus) n_mmp
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
         REWIND(69)
         numLines = 0 
         DO
            READ (69,*,iostat=iofl)
            IF (iofl < 0) EXIT
            numLines = numLines + 1
         END DO
         IF (MOD(numLines,14*input%jspins) == 1) THEN !read in alpha, spinf in this case
            REWIND(69)
            DO i = 1, numLines-1
               READ (69,*,iostat=iofl)
            END DO
            READ (69,'(2(6x,f5.3))',IOSTAT=ioStatus) alpha, spinf
            IF(input%ldauLinMix.AND.(input%ldauMixParam.LT.0.0)) THEN
               input%ldauMixParam = alpha
               input%ldauSpinf = spinf
            END IF
         ELSE IF (MOD(numLines,14*input%jspins).NE.0) THEN
            CALL juDFT_error("strange n_mmp_mat-file...",calledby ="readDensityMatrix")
         ELSE IF(input%ldauMixParam.LT.0.0) THEN
            input%ldauLinMix = .FALSE.
         END IF
1224
         CLOSE(69)
1225

1226 1227 1228 1229 1230 1231 1232 1233 1234
!         IF (ioStatus.NE.0) THEN
!            l_error = .TRUE.
!            RETURN
!         END IF

      END IF
   
   END SUBROUTINE readDensityMatrix

1235 1236 1237 1238 1239 1240 1241 1242
   SUBROUTINE setStartingDensity(l_noco)

      LOGICAL,INTENT(IN) :: l_noco

#ifdef CPP_HDF
      INTEGER(HID_T)    :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
1243
      INTEGER           :: currentStructureIndex,currentStepfunctionIndex
1244 1245 1246
      INTEGER           :: readDensityIndex, lastDensityIndex
      INTEGER           :: sdIndex, ioStatus, mode
      INTEGER           :: densityType
1247
      CHARACTER(LEN=1000) :: numberString
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
      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