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

   PRIVATE
28 29
   PUBLIC readDensity, writeDensity
   PUBLIC isDensityFilePresent, isCoreDensityPresent
30
   PUBLIC readCoreDensity, writeCoreDensity
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   PUBLIC CDN_INPUT_DEN_const, CDN_OUTPUT_DEN_const
   PUBLIC CDN_ARCHIVE_TYPE_CDN1_const, CDN_ARCHIVE_TYPE_NOCO_const
   PUBLIC CDN_ARCHIVE_TYPE_CDN_const

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

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

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

   CONTAINS

   SUBROUTINE readDensity(stars,vacuum,atoms,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
                          relCdnIndex,iter,fr,fpw,fz,fzxy,cdom,cdomvz,cdomvxy)

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

      INTEGER, INTENT (IN)      :: inOrOutCDN
      INTEGER, INTENT (IN)      :: relCdnIndex
      INTEGER, INTENT (OUT)     :: iter
      INTEGER, INTENT (IN)      :: archiveType

      !     ..
      !     .. Array Arguments ..
66
      COMPLEX, INTENT (OUT) :: fpw(stars%ng3,input%jspins), fzxy(vacuum%nmzxyd,stars%ng2-1,2,input%jspins)
67 68 69 70
      COMPLEX, INTENT (OUT) :: cdom(:), cdomvz(:,:), cdomvxy(:,:,:)
      REAL,    INTENT (OUT) :: fr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins), fz(vacuum%nmzd,2,input%jspins)

      ! local variables
71 72 73
      INTEGER            :: mode, datend, k, i, iVac, j, iUnit
      LOGICAL            :: l_exist, l_rhomatFile
      CHARACTER(LEN=30)  :: filename
74

75 76 77 78 79 80 81 82 83
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
      INTEGER           :: currentStructureIndex,currentDensityIndex
      INTEGER           :: lastDensityIndex, densityType
      CHARACTER(LEN=30) :: archiveName
      TYPE(t_cell)      :: cellTemp

84 85 86
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
87 88 89 90 91
#ifdef CPP_HDF

         densityType = 0
         archiveName = ''

92 93
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,currentDensityIndex)

            currentDensityIndex = currentDensityIndex + relCdnIndex ! This is actually wrong. I should go back step by step.

            IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
               archiveName = 'cdn'
            ELSE
               WRITE(archiveName,'(a,i0)') '/cdn-', currentDensityIndex
            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
                  CALL juDFT_error("Invalid inOrOutCDN selected.",calledby ="writeDensity")
            END SELECT
            l_exist = isDensityEntryPresentHDF(fileID,archiveName,densityType)
            CALL closeCDN_HDF(fileID)
         END IF
124

125 126 127 128 129 130 131
         IF (l_exist) THEN
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,currentDensityIndex)

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

            CALL closeCDN_HDF(fileID)
132 133
            RETURN
         ELSE
134
            WRITE(*,*) 'cdn.hdf file or relevant density entry not found.'
135 136 137
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
138
#endif
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      END IF

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

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

      IF (mode.EQ.CDN_DIRECT_MODE) THEN
         filename = 'cdn1'
156
         l_rhomatFile = .FALSE.
157 158 159
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            INQUIRE(file="rhomat_inp",EXIST=l_exist)
            IF (l_exist) filename = 'rhomat_inp'
160 161 162 163 164
            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.
165 166 167 168 169 170 171
         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
172
            CALL juDFT_error("charge density file "//TRIM(ADJUSTL(filename))//" missing",calledby ="readDensity")
173 174 175 176 177
         END IF

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

178
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
179 180 181 182 183 184 185 186 187 188
            ! call loddop to move the file position to the output density
            CALL loddop(stars,vacuum,atoms,sphhar,input,sym,&
                        iUnit,iter,fr,fpw,fz,fzxy)
         END IF

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

         ! read in additional data if l_noco and data is present
189
         IF ((archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const).AND.l_rhomatFile) THEN
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            READ (iUnit,iostat=datend) (cdom(k),k=1,stars%ng3)
            IF (datend == 0) THEN
               IF (input%film) THEN
                  READ (iUnit) ((cdomvz(i,iVac),i=1,vacuum%nmz),iVac=1,vacuum%nvac)
                  READ (iUnit) (((cdomvxy(i,j-1,iVac),i=1,vacuum%nmzxy),j=2,oneD%odi%nq2), iVac=1,vacuum%nvac)
               END IF
            ELSE
               ! (datend < 0)  =>  no off-diagonal magnetisation stored
               !                   in "rhomat_inp"
               IF (datend > 0) THEN
                  WRITE(*,*) 'datend = ', datend
                  CALL juDFT_error("density file has illegal state.",calledby ="readDensity")
               END IF
               cdom = CMPLX(0.0,0.0)
               IF (input%film) THEN
                  cdomvz = CMPLX(0.0,0.0)
                  cdomvxy = CMPLX(0.0,0.0)
               END IF
            END IF
         ELSE IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            cdom = CMPLX(0.0,0.0)
            IF (input%film) THEN
               cdomvz = CMPLX(0.0,0.0)
               cdomvxy = CMPLX(0.0,0.0)
            END IF
         END IF
      END IF

      CLOSE(iUnit)

   END SUBROUTINE readDensity

222
   SUBROUTINE writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
223
                           relCdnIndex,iter,fr,fpw,fz,fzxy,cdom,cdomvz,cdomvxy)
224 225 226 227

      TYPE(t_stars),INTENT(IN)  :: stars
      TYPE(t_vacuum),INTENT(IN) :: vacuum
      TYPE(t_atoms),INTENT(IN)  :: atoms
228
      TYPE(t_cell), INTENT(IN)  :: cell
229 230 231 232 233 234
      TYPE(t_sphhar),INTENT(IN) :: sphhar
      TYPE(t_input),INTENT(IN)  :: input
      TYPE(t_sym),INTENT(IN)    :: sym
      TYPE(t_oneD),INTENT(IN)   :: oneD

      INTEGER, INTENT (IN)      :: inOrOutCDN
235
      INTEGER, INTENT (IN)      :: relCdnIndex, iter
236 237 238
      INTEGER, INTENT (IN)      :: archiveType
      !     ..
      !     .. Array Arguments ..
239
      COMPLEX, INTENT (IN) :: fpw(stars%ng3,input%jspins), fzxy(vacuum%nmzxyd,stars%ng2-1,2,input%jspins)
240 241 242 243 244 245 246 247 248 249 250 251 252 253
      COMPLEX, INTENT (IN) :: cdom(:), cdomvz(:,:), cdomvxy(:,:,:)
      REAL,    INTENT (IN) :: fr(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins), fz(vacuum%nmzd,2,input%jspins)

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

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

      INTEGER           :: mode, iterTemp, k, i, iVac, j, iUnit
254
      INTEGER           :: d1, d10, asciioffset, iUnitTemp
255
      LOGICAL           :: l_exist, l_storeIndices
256
      CHARACTER(len=30) :: filename
257
      CHARACTER(len=5)  :: cdnfile
258

259 260 261 262 263 264 265 266
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
      INTEGER           :: currentStructureIndex,currentDensityIndex
      INTEGER           :: lastDensityIndex, densityType
      CHARACTER(LEN=30) :: archiveName

267 268 269
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,currentDensityIndex)

         l_storeIndices = .FALSE.
         IF (currentStarsIndex.EQ.0) THEN
            currentStarsIndex = 1
            l_storeIndices = .TRUE.
            CALL writeStarsHDF(fileID, currentStarsIndex, stars)
         END IF
         IF (currentLatharmsIndex.EQ.0) THEN
            currentLatharmsIndex = 1
            l_storeIndices = .TRUE.
            CALL writeLatharmsHDF(fileID, currentLatharmsIndex, sphhar)
         END IF
         IF(currentStructureIndex.EQ.0) THEN
            currentStructureIndex = 1
            l_storeIndices = .TRUE.
            CALL writeStructureHDF(fileID, input, atoms, cell, vacuum, oneD, currentStructureIndex)
         END IF
         lastDensityIndex = currentDensityIndex
         IF(relCdnIndex.NE.0) THEN
            currentDensityIndex = currentDensityIndex+relCdnIndex
            l_storeIndices = .TRUE.
         END IF

         archiveName = ''
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            archiveName = 'cdn'
         ELSE
            WRITE(archiveName,'(a,i0)') '/cdn-', currentDensityIndex
         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

         CALL writeDensityHDF(input, fileID, archiveName, densityType, lastDensityIndex,&
                              currentStarsIndex, currentLatharmsIndex, currentStructureIndex,&
                              iter+relCdnIndex,fr,fpw,fz,fzxy,cdom,cdomvz,cdomvxy)

         IF(l_storeIndices) THEN
            CALL writeHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,&
                                 currentStructureIndex,currentDensityIndex)
         END IF

         CALL closeCDN_HDF(fileID)
#endif
332 333 334 335 336 337 338
      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'
339
            IF(inOrOutCDN.EQ.CDN_OUTPUT_DEN_const) filename = 'rhomat_out'
340 341 342 343 344
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

345 346 347 348 349 350 351
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(iter.EQ.0)) THEN
            INQUIRE(file=TRIM(ADJUSTL(filename)),EXIST=l_exist)
            IF(l_exist) THEN
               CALL juDFT_error("Trying to generate starting density while a density exists.",calledby ="writeDensity")
            END IF
         END IF

352 353 354
         iUnit = 93
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),FORM='unformatted',STATUS='unknown')

355
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(iter.GE.1)) THEN
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            inputTemp%jspins = input%jspins
            vacuumTemp%nmzxyd = vacuum%nmzxyd
            atomsTemp%jmtd = atoms%jmtd
            sphharTemp%nlhd = sphhar%nlhd
            vacuumTemp%nmzd = vacuum%nmzd
            atomsTemp%ntype = atoms%ntype
            ALLOCATE (sphharTemp%nlh(SIZE(sphhar%nlh)))
            sphharTemp%nlh(:) = sphhar%nlh(:)
            ALLOCATE (atomsTemp%ntypsy(SIZE(atoms%ntypsy)))
            atomsTemp%ntypsy(:) = atoms%ntypsy(:)
            ALLOCATE (atomsTemp%jri(SIZE(atoms%jri)))
            atomsTemp%jri(:) = atoms%jri(:)
            ALLOCATE (atomsTemp%neq(SIZE(atoms%neq)))
            atomsTemp%neq(:) = atoms%neq(:)
            ALLOCATE (atomsTemp%zatom(SIZE(atoms%zatom)))
            atomsTemp%zatom(:) = atoms%zatom(:)
            ALLOCATE (atomsTemp%rmt(SIZE(atoms%rmt)))
            atomsTemp%rmt(:) = atoms%rmt(:)
            ALLOCATE (atomsTemp%dx(SIZE(atoms%dx)))
            atomsTemp%dx(:) = atoms%dx(:)
            starsTemp%ng3 = stars%ng3
            symTemp%invs = sym%invs
            inputTemp%film = input%film
            vacuumTemp%nvac = vacuum%nvac
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
382 383
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
            ALLOCATE (frTemp(atoms%jmtd,0:sphhar%nlhd,atoms%ntype,input%jspins))
            ALLOCATE (fzTemp(vacuum%nmzd,2,input%jspins))

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

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


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

            CLOSE(iUnitTemp)
            REWIND iUnit

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

416
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

            ! Generate data in temp arrays and variables to be able to perform loddop call.
            ! loddop is called to move the file position to the output density position.
            inputTemp%jspins = input%jspins
            vacuumTemp%nmzxyd = vacuum%nmzxyd
            atomsTemp%jmtd = atoms%jmtd
            sphharTemp%nlhd = sphhar%nlhd
            vacuumTemp%nmzd = vacuum%nmzd
            atomsTemp%ntype = atoms%ntype
            ALLOCATE (sphharTemp%nlh(SIZE(sphhar%nlh)))
            sphharTemp%nlh(:) = sphhar%nlh(:)
            ALLOCATE (atomsTemp%ntypsy(SIZE(atoms%ntypsy)))
            atomsTemp%ntypsy(:) = atoms%ntypsy(:)
            ALLOCATE (atomsTemp%jri(SIZE(atoms%jri)))
            atomsTemp%jri(:) = atoms%jri(:)
            ALLOCATE (atomsTemp%neq(SIZE(atoms%neq)))
            atomsTemp%neq(:) = atoms%neq(:)
            starsTemp%ng3 = stars%ng3
            symTemp%invs = sym%invs
            inputTemp%film = input%film
            vacuumTemp%nvac = vacuum%nvac
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
440 441
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
442 443 444 445 446 447 448 449 450 451 452 453
            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,&
454
                     iUnit,iter+relCdnIndex,fr,fpw,fz,fzxy)
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

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

         CLOSE(iUnit)
      END IF

   END SUBROUTINE writeDensity

470 471 472 473 474 475 476 477 478 479 480 481 482 483
   SUBROUTINE readCoreDensity(input,atoms,dimension,rhcs,tecs,qints)

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

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

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

484 485 486 487 488 489
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
      INTEGER        :: currentStructureIndex,currentDensityIndex

490 491 492
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
493 494
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
495
         IF (l_exist) THEN
496 497 498
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,currentDensityIndex)
            CALL readCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
            CALL closeCDN_HDF(fileID)
499 500
            RETURN
         ELSE
501
            WRITE(*,*) 'No core density is available in HDF5 format.'
502 503 504
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
505
#endif
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
      END IF

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

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

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

   END SUBROUTINE readCoreDensity

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

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

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

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

553 554 555 556 557 558
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
      INTEGER        :: currentStructureIndex,currentDensityIndex

559 560 561
      CALL getMode(mode)

      IF(mode.EQ.CDN_HDF5_MODE) THEN
562 563 564 565 566
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,currentDensityIndex)
         CALL writeCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
         CALL closeCDN_HDF(fileID)
#endif
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
      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


586 587 588 589
   SUBROUTINE getMode(mode)
      INTEGER, INTENT(OUT) :: mode

      mode = CDN_DIRECT_MODE
590 591 592 593 594 595 596 597 598 599 600
      IF (juDFT_was_argument("-stream_cdn")) THEN
         mode=CDN_STREAM_MODE
      END IF
      IF (juDFT_was_argument("-hdf_cdn")) THEN
#ifdef CPP_HDF
         mode=CDN_HDF5_MODE
#else
         WRITE(*,*) 'Code not compiled with HDF5 support.'
         WRITE(*,*) 'Falling back to direct access.'
#endif
      END IF
601 602
   END SUBROUTINE getMode

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
   LOGICAL FUNCTION isDensityFilePresent(archiveType)

      INTEGER, INTENT(IN) :: archiveType

      LOGICAL             :: l_exist
      INTEGER             :: mode

      CALL getMode(mode)

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

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

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

645 646 647 648 649 650 651 652
   LOGICAL FUNCTION isCoreDensityPresent()

      LOGICAL             :: l_exist
      INTEGER             :: mode

      CALL getMode(mode)

      IF (mode.EQ.CDN_HDF5_MODE) THEN
653 654
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
655
         IF(l_exist) THEN
656
            isCoreDensityPresent = l_exist
657 658
            RETURN
         END IF
659
#endif
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
      END IF

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

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


680
END MODULE m_cdn_io