cdn_io.F90 56.1 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 readCoreDensity, writeCoreDensity
34
   PUBLIC readStars, writeStars
35
   PUBLIC readStepfunction, writeStepfunction
36
   PUBLIC setStartingDensity, readPrevEFermi, deleteDensities
37
   PUBLIC storeStructureIfNew
38
   PUBLIC getIOMode
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   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

56 57 58 59 60 61 62 63 64 65 66 67 68
   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

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

80
      CALL getIOMode(mode)
81

82
      WRITE(*,*) 'Available densities info:'
83 84 85 86 87 88 89 90
      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)
91
            WRITE(*,*) 'densityIndex   iteration   prevDensity   prevDistance        timeStamp'
92 93 94 95 96 97 98 99 100 101 102 103
            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,&
104
                                        dateTemp, timeTemp, distanceTemp, fermiEnergyTemp, l_qfixTemp)
105

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

109 110 111 112 113 114 115 116 117 118 119
               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))
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            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


135
   SUBROUTINE readDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
136 137 138 139 140 141 142 143 144 145 146
                          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
147 148 149 150

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

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

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

169 170 171
      fermiEnergy = 0.0
      l_qfix = .FALSE.

172
      CALL getIOMode(mode)
173 174

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

         densityType = 0
         archiveName = ''

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

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

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

216
            CALL readDensityHDF(fileID, input, stars, sphhar, atoms, vacuum, oneD, archiveName, densityType,&
217
                                fermiEnergy,l_qfix,l_DimChange,den)
218

219
            CALL closeCDNPOT_HDF(fileID)
220 221 222

            IF(l_DimChange) THEN
               CALL writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
223
                           1,-1.0,fermiEnergy,l_qfix,den)
224
            END IF
225
         ELSE
226
            WRITE(*,*) 'cdn.hdf file or relevant density entry not found.'
227 228 229
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
230
#endif
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      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'
247
         l_rhomatFile = .FALSE.
248 249 250
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            INQUIRE(file="rhomat_inp",EXIST=l_exist)
            IF (l_exist) filename = 'rhomat_inp'
251 252 253 254 255
            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.
256 257 258 259 260 261 262
         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
263
            CALL juDFT_error("charge density file "//TRIM(ADJUSTL(filename))//" missing",calledby ="readDensity")
264 265 266 267 268
         END IF

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

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

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

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

310 311 312 313 314 315 316 317 318 319 320 321
      INQUIRE(FILE='n_mmp_mat',EXIST=l_exist)
      IF(l_exist.AND.atoms%n_u.GT.0) THEN
         OPEN (69,file='n_mmp_mat',status='unknown',form='formatted')
         READ (69,'(7f20.13)',IOSTAT=ioStatus) den%mmpMat
         REWIND(69)
         numLines = 0
         DO
            READ (69,*,iostat=iofl)
            IF (iofl < 0) EXIT
            numLines = numLines + 1
         END DO
         IF (MOD(numLines,14*input%jspins).NE.0) THEN
322 323 324 325
            WRITE(*,*) 'The n_mmp_mat file could not be read.'
            WRITE(*,*) 'Was it an old style file with linear mixing parameter specification'
            WRITE(*,*) 'in the last line? Linear mixing for the density matrix can now be'
            WRITE(*,*) 'activated and specified in the inp.xml file.'
326 327 328 329 330
            CALL juDFT_error("strange n_mmp_mat-file...", calledby = "readDensity")
         END IF
         CLOSE(69)
      END IF

331 332
   END SUBROUTINE readDensity

333
   SUBROUTINE writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
334
                           relCdnIndex,distance,fermiEnergy,l_qfix,den)
335

336 337 338 339 340 341 342 343 344
      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
345 346

      INTEGER, INTENT (IN)      :: inOrOutCDN
347
      INTEGER, INTENT (IN)      :: relCdnIndex
348
      INTEGER, INTENT (IN)      :: archiveType
349
      REAL,    INTENT (IN)      :: fermiEnergy, distance
350
      LOGICAL, INTENT (IN)      :: l_qfix
351 352 353 354 355 356 357

      TYPE(t_stars)        :: starsTemp
      TYPE(t_vacuum)       :: vacuumTemp
      TYPE(t_atoms)        :: atomsTemp
      TYPE(t_sphhar)       :: sphharTemp
      TYPE(t_input)        :: inputTemp
      TYPE(t_sym)          :: symTemp
358 359
      TYPE(t_cell)         :: cellTemp
      TYPE(t_oneD)         :: oneDTemp
360 361 362 363 364

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

      INTEGER           :: mode, iterTemp, k, i, iVac, j, iUnit
365
      INTEGER           :: d1, d10, asciioffset, iUnitTemp
366
      LOGICAL           :: l_exist, l_storeIndices, l_writeNew, l_same
367
      LOGICAL           :: l_writeAll
368
      CHARACTER(len=30) :: filename
369
      CHARACTER(len=5)  :: cdnfile
370

371 372 373 374
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
375
      INTEGER           :: currentStructureIndex,currentStepfunctionIndex
376 377 378
      INTEGER           :: readDensityIndex, writeDensityIndex, lastDensityIndex
      INTEGER           :: previousDensityIndex, densityType
      INTEGER           :: starsIndexTemp, latharmsIndexTemp, structureIndexTemp
379
      INTEGER           :: stepfunctionIndexTemp
380
      INTEGER           :: jspinsTemp
381
      INTEGER           :: date, time, dateTemp, timeTemp
382
      REAL              :: fermiEnergyTemp, distanceTemp
383
      LOGICAL           :: l_qfixTemp
384
      CHARACTER(LEN=30) :: archiveName
385 386 387
      CHARACTER(LEN=8)  :: dateString
      CHARACTER(LEN=10) :: timeString
      CHARACTER(LEN=10) :: zone
388

389
      CALL getIOMode(mode)
390 391 392
      CALL DATE_AND_TIME(dateString,timeString,zone)
      READ(dateString,'(i8)') date
      READ(timeString,'(i6)') time
393 394

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

399 400 401 402
         CALL checkAndWriteMetadataHDF(fileID, input, atoms, cell, vacuum, oneD, stars, sphhar, sym,&
                                       currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                       currentStepfunctionIndex,l_storeIndices)

403 404
         previousDensityIndex = readDensityIndex
         writeDensityIndex = readDensityIndex
405
         IF(relCdnIndex.NE.0) THEN
406 407 408
            writeDensityIndex = lastDensityIndex+relCdnIndex
            lastDensityIndex = writeDensityIndex
            readDensityIndex = writeDensityIndex
409 410 411 412 413 414 415
            l_storeIndices = .TRUE.
         END IF

         archiveName = ''
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            archiveName = 'cdn'
         ELSE
416
            WRITE(archiveName,'(a,i0)') '/cdn-', writeDensityIndex
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
         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

438 439 440 441 442
         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,&
443
                                        stepfunctionIndexTemp,previousDensityIndex, jspinsTemp,&
444
                                        dateTemp, timeTemp, distanceTemp, fermiEnergyTemp, l_qfixTemp)
445 446 447
            END IF
         END IF

448
         IF(vacuum%nvac.EQ.1) THEN
449
            den%vacz(:,2,:)=den%vacz(:,1,:)
450
            IF (sym%invs) THEN
451
               den%vacxy(:,:,2,:) = CONJG(den%vacxy(:,:,1,:))
452
            ELSE
453
               den%vacxy(:,:,2,:) = den%vacxy(:,:,1,:)
454 455 456
            END IF
         END IF

457
         CALL writeDensityHDF(input, fileID, archiveName, densityType, previousDensityIndex,&
458
                              currentStarsIndex, currentLatharmsIndex, currentStructureIndex,&
459 460
                              currentStepfunctionIndex,date,time,distance,fermiEnergy,l_qfix,&
                              den%iter+relCdnIndex,den)
461 462

         IF(l_storeIndices) THEN
463 464
            CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                    currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
465 466
         END IF

467
         CALL closeCDNPOT_HDF(fileID)
468
#endif
469 470 471 472 473 474 475
      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'
476
            IF(inOrOutCDN.EQ.CDN_OUTPUT_DEN_const) filename = 'rhomat_out'
477 478 479 480 481
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

482
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(den%iter.EQ.0)) THEN
483 484 485 486 487 488
            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

489 490 491
         iUnit = 93
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),FORM='unformatted',STATUS='unknown')

492
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(den%iter.GE.1)) THEN
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
            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
517 518 519 520
            vacuumTemp%nmzxy = vacuum%nmzxy
            vacuumTemp%nmz = vacuum%nmz
            vacuumTemp%dvac = vacuum%dvac
            vacuumTemp%delz = vacuum%delz
521 522
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
523 524
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
525 526 527 528
            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
529 530
            d1 = MOD(den%iter,10)
            d10 = MOD(INT((den%iter+0.5)/10),10)
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
            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)

            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

552
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

            ! 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
574 575 576 577
            vacuumTemp%nmzxy = vacuum%nmzxy
            vacuumTemp%nmz = vacuum%nmz
            vacuumTemp%dvac = vacuum%dvac
            vacuumTemp%delz = vacuum%delz
578 579
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
580 581
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
582 583 584 585 586 587 588 589 590 591 592 593
            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,&
594
                     iUnit,den%iter+relCdnIndex,den%mt,den%pw,den%vacz,den%vacxy)
595 596 597

         ! Write additional data if l_noco
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
598
            WRITE (iUnit) (den%cdom(k),k=1,stars%ng3)
599
            IF (input%film) THEN
600 601
               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)
602 603 604 605
            END IF
         END IF

         CLOSE(iUnit)
606
      END IF
607

608
      !write density matrix to n_mmp_mat_out file
Gregor Michalicek's avatar
Gregor Michalicek committed
609
      IF((inOrOutCDN.EQ.CDN_INPUT_DEN_const).AND.(relCdnIndex.EQ.1).AND.&
610 611
         ((archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).OR.(archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const))) THEN
         IF(atoms%n_u.GT.0) THEN
612 613 614 615 616 617 618 619 620 621 622
            filename = 'n_mmp_mat'
            IF (mode.EQ.CDN_HDF5_MODE) THEN
               filename = 'n_mmp_mat_out'
            END IF
            IF(ANY(den%mmpMat(:,:,:,:).NE.0.0)) THEN
               IF ((mode.EQ.CDN_HDF5_MODE).AND..NOT.(input%ldauLinMix.AND.(input%ldauMixParam.EQ.0.0))) THEN
                  INQUIRE(file='n_mmp_mat',exist=l_exist)
                  IF(l_exist) THEN
                     CALL system('mv n_mmp_mat n_mmp_mat_old')
                     PRINT *,"n_mmp_mat moved to n_mmp_mat_old"
                  END IF
623
               END IF
624
               OPEN (69,file=TRIM(ADJUSTL(filename)),status='replace',form='formatted')
625 626
               WRITE (69,'(7f20.13)') den%mmpMat(:,:,:,:)
               CLOSE (69)
627 628
            END IF
         END IF
629 630 631 632
      END IF

   END SUBROUTINE writeDensity

633 634 635 636 637 638 639 640 641 642
   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
643
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
644 645 646
      INTEGER        :: readDensityIndex, lastDensityIndex

      INTEGER           :: starsIndex, latharmsIndex, structureIndex
647
      INTEGER           :: stepfunctionIndex
648
      INTEGER           :: date, time, iter, jspins, previousDensityIndex
649
      REAL              :: fermiEnergy, distance
650 651 652
      LOGICAL           :: l_qfix, l_exist
      CHARACTER(LEN=30) :: archiveName

653
      CALL getIOMode(mode)
654 655 656 657 658 659

      eFermiPrev = 0.0
      l_error = .FALSE.
      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
660
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
661 662
         WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
         CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_UNDEFINED_const,&
663
                                  iter, starsIndex, latharmsIndex, structureIndex, stepfunctionIndex,&
664
                                  previousDensityIndex, jspins, date, time, distance, fermiEnergy, l_qfix)
665 666 667 668 669
         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,&
670
                                     iter, starsIndex, latharmsIndex, structureIndex, stepfunctionIndex,&
671
                                     previousDensityIndex, jspins, date, time, distance, fermiEnergy, l_qfix)
672 673 674 675
            eFermiPrev = fermiEnergy
         ELSE
            l_error = .TRUE.
         END IF
676
         CALL closeCDNPOT_HDF(fileID)
677 678 679 680 681 682 683 684 685
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         STOP 'cdn.str not yet implemented!'
      ELSE
         l_error = .TRUE.
      END IF

   END SUBROUTINE

686 687 688 689 690 691
   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
692 693 694
      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)
695 696 697 698 699

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

700 701 702 703
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
704
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
705
      INTEGER        :: readDensityIndex, lastDensityIndex
706

707
      CALL getIOMode(mode)
708 709

      IF(mode.EQ.CDN_HDF5_MODE) THEN
710 711
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
712
         IF (l_exist) THEN
713
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
714
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
715
            CALL readCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
716
            CALL closeCDNPOT_HDF(fileID)
717 718
            RETURN
         ELSE
719
            WRITE(*,*) 'No core density is available in HDF5 format.'
720 721 722
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
723
#endif
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 752 753 754 755 756 757 758 759 760 761 762 763 764
      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
765 766 767
      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)
768 769 770

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

771 772 773 774
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
775
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
776
      INTEGER        :: readDensityIndex, lastDensityIndex
777

778
      CALL getIOMode(mode)
779 780

      IF(mode.EQ.CDN_HDF5_MODE) THEN
781
#ifdef CPP_HDF
782
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
783
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
784
         CALL writeCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
785
         CALL closeCDNPOT_HDF(fileID)
786
#endif
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
      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

805
   SUBROUTINE storeStructureIfNew(input, atoms, cell, vacuum, oneD, sym)
806 807 808 809 810 811

      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
812
      TYPE(t_sym),INTENT(IN)     :: sym
813 814 815 816 817 818

      TYPE(t_input)              :: inputTemp
      TYPE(t_atoms)              :: atomsTemp
      TYPE(t_cell)               :: cellTemp
      TYPE(t_vacuum)             :: vacuumTemp
      TYPE(t_oneD)               :: oneDTemp
819
      TYPE(t_sym)                :: symTemp
820 821 822 823 824 825 826 827 828

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

829
      CALL getIOMode(mode)
830 831 832 833 834 835 836 837 838 839 840

      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
841
            CALL readStructureHDF(fileID, inputTemp, atomsTemp, cellTemp, vacuumTemp, oneDTemp, symTemp, currentStructureIndex)
842
            CALL compareStructure(input, atoms, vacuum, cell, sym, inputTemp, atomsTemp, vacuumTemp, cellTemp, symTemp, l_same)
843
            IF(.NOT.l_same) THEN
844
               currentStructureIndex = currentStructureIndex + 1
845 846 847 848 849
               l_writeStructure = .TRUE.
            END IF
         END IF

         IF (l_writeStructure) THEN
850
            CALL writeStructureHDF(fileID, input, atoms, cell, vacuum, oneD, sym, currentStructureIndex)
851 852 853 854 855 856 857 858 859 860 861 862 863
            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

864
   END SUBROUTINE storeStructureIfNew
865

866 867 868 869 870
   SUBROUTINE writeStars(stars,l_xcExtended,l_ExtData)

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

871 872 873
      INTEGER        :: mode, ngz, izmin, izmax

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
874
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
875 876 877
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
878 879 880 881 882 883 884 885

      INTEGER :: igz(stars%ng3)

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

886
      CALL getIOMode(mode)
887 888 889

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

         currentStarsIndex = currentStarsIndex + 1
894
         CALL writeStarsHDF(fileID, currentStarsIndex, currentStructureIndex, stars)
895

896 897
         CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                 currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
898 899

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

934 935 936 937
      TYPE(t_stars),INTENT(INOUT) :: stars
      LOGICAL, INTENT(IN)         :: l_xcExtended,l_ExtData
      LOGICAL, INTENT(OUT)        :: l_error

938 939

      TYPE(t_stars)               :: starsTemp
940
      INTEGER                     :: mode, ioStatus, ngz,izmin,izmax
941
      LOGICAL                     :: l_exist, l_same
942

943
      INTEGER        :: structureIndexTemp
944
      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
945
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
946 947 948 949
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

950 951 952 953 954 955 956 957
      INTEGER :: igz(stars%ng3)

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

958
      CALL getIOMode(mode)
959 960 961 962 963

      IF(mode.EQ.CDN_HDF5_MODE) THEN
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
#ifdef CPP_HDF
964
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
965
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
966 967 968
            IF (currentStarsIndex.LT.1) THEN
               mode = CDN_DIRECT_MODE ! (no stars entry found in cdn.hdf file)
            ELSE
969 970 971 972 973 974
               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
975 976 977 978 979
               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
980 981
            END IF
            CALL closeCDNPOT_HDF(fileID)
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 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
#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<