cdn_io.F90 61.9 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,transform_by_moving_atoms
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
                          relCdnIndex,fermiEnergy,l_qfix,den,inFilename)
137 138 139 140 141 142 143 144 145 146

      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 155
      CHARACTER(LEN=*), OPTIONAL, INTENT(IN)  :: inFilename

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

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

Gregor Michalicek's avatar
Gregor Michalicek committed
171 172
      COMPLEX, ALLOCATABLE :: cdomvz(:,:)

173 174 175
      fermiEnergy = 0.0
      l_qfix = .FALSE.

176
      CALL getIOMode(mode)
177

178 179 180 181 182 183 184 185 186
#ifndef CPP_HDF
      filename = 'cdn.hdf'
      IF(PRESENT(inFilename)) filename = TRIM(ADJUSTL(inFilename))//'.hdf'
      INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
      IF (l_exist) THEN
         CALL juDFT_warn('Fleur not compiled for HDF5, but '//TRIM(ADJUSTL(filename))//' present',calledby='readDensity')
      END IF
#endif

187
      IF(mode.EQ.CDN_HDF5_MODE) THEN
188 189 190 191 192
#ifdef CPP_HDF

         densityType = 0
         archiveName = ''

193 194 195 196
         filename = 'cdn.hdf'
         IF(PRESENT(inFilename)) filename = TRIM(ADJUSTL(inFilename))//'.hdf'

         INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
197
         IF (l_exist) THEN
198
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
199
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex,inFilename)
200 201 202 203

            IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
               archiveName = 'cdn'
            ELSE
204
               WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            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
222
                  CALL juDFT_error("Invalid inOrOutCDN selected.",calledby ="readDensity")
223 224
            END SELECT
            l_exist = isDensityEntryPresentHDF(fileID,archiveName,densityType)
225
            CALL closeCDNPOT_HDF(fileID)
226
         END IF
227

228
         IF (l_exist) THEN
229
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
230
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex,inFilename)
231

232
            CALL readDensityHDF(fileID, input, stars, sphhar, atoms, vacuum, oneD, archiveName, densityType,&
233
                                fermiEnergy,l_qfix,l_DimChange,den)
234

235
            CALL closeCDNPOT_HDF(fileID)
236 237 238

            IF(l_DimChange) THEN
               CALL writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
239
                           1,-1.0,fermiEnergy,l_qfix,den)
240
            END IF
241
         ELSE
242 243 244 245 246 247 248 249
            INQUIRE(FILE=TRIM(ADJUSTL(filename)),EXIST=l_exist)
            IF(l_exist) THEN
               WRITE(*,*) 'densityType is ', densityType
               WRITE(*,*) 'Relevant density entry is '//TRIM(ADJUSTL(archiveName))//'.'
               WRITE(*,*) 'Entry not found in '//TRIM(ADJUSTL(filename))//'.'
            ELSE
               WRITE(*,*) TRIM(ADJUSTL(filename))//' file not found.'
            END IF
250
            WRITE(*,*) 'Falling back to stream access.'
251 252
            mode = CDN_STREAM_MODE
         END IF
253
#endif
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      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'
270
         l_rhomatFile = .FALSE.
271 272 273
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
            INQUIRE(file="rhomat_inp",EXIST=l_exist)
            IF (l_exist) filename = 'rhomat_inp'
274 275 276 277 278
            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.
279 280 281 282 283
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

284 285
         IF(PRESENT(inFilename)) filename = inFilename

286 287
         INQUIRE(file=TRIM(ADJUSTL(filename)),EXIST=l_exist)
         IF(.NOT.l_exist) THEN
288
            CALL juDFT_error("charge density file "//TRIM(ADJUSTL(filename))//" missing",calledby ="readDensity")
289 290 291 292 293
         END IF

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

294
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
295 296
            ! call loddop to move the file position to the output density
            CALL loddop(stars,vacuum,atoms,sphhar,input,sym,&
297
                        iUnit,den%iter,den%mt,den%pw,den%vacz,den%vacxy)
298 299 300 301
         END IF

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

         ! read in additional data if l_noco and data is present
305
         IF ((archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const).AND.l_rhomatFile) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
306
            READ (iUnit,iostat=datend) (den%pw(k,3),k=1,stars%ng3)
307 308
            IF (datend == 0) THEN
               IF (input%film) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
309 310 311 312 313 314 315 316 317
                  ALLOCATE(cdomvz(vacuum%nmz,vacuum%nvac))
                  READ (iUnit) ((cdomvz(i,iVac),i=1,vacuum%nmz),iVac=1,vacuum%nvac)
                  DO iVac = 1, vacuum%nvac
                     DO i = 1, vacuum%nmz
                        den%vacz(i,iVac,3) = REAL(cdomvz(i,iVac))
                        den%vacz(i,iVac,4) = AIMAG(cdomvz(i,iVac))
                     END DO
                  END DO
                  DEALLOCATE(cdomvz)
318
                  READ (iUnit) (((den%vacxy(i,j-1,iVac,3),i=1,vacuum%nmzxy),j=2,stars%ng2), iVac=1,vacuum%nvac)
319 320 321 322 323 324 325 326
               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
Gregor Michalicek's avatar
Gregor Michalicek committed
327
               den%pw(:,3) = CMPLX(0.0,0.0)
328
               IF (input%film) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
329
                  den%vacz(:,:,3:4) = 0.0
Gregor Michalicek's avatar
Gregor Michalicek committed
330
                  den%vacxy(:,:,:,3) = CMPLX(0.0,0.0)
331 332 333
               END IF
            END IF
         ELSE IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
334
            den%pw(:,3) = CMPLX(0.0,0.0)
335
            IF (input%film) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
336
               den%vacz(:,:,3:4) = 0.0
Gregor Michalicek's avatar
Gregor Michalicek committed
337
               den%vacxy(:,:,:,3) = CMPLX(0.0,0.0)
338 339
            END IF
         END IF
340
         CLOSE(iUnit)
341 342
      END IF

343 344 345 346 347 348 349 350 351 352 353 354
      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
355 356 357 358
            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.'
359 360 361 362 363
            CALL juDFT_error("strange n_mmp_mat-file...", calledby = "readDensity")
         END IF
         CLOSE(69)
      END IF

364 365
   END SUBROUTINE readDensity

366
   SUBROUTINE writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,inOrOutCDN,&
367
                           relCdnIndex,distance,fermiEnergy,l_qfix,den,inFilename)
368

369 370 371 372 373 374 375 376 377
      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
378 379

      INTEGER, INTENT (IN)      :: inOrOutCDN
380
      INTEGER, INTENT (IN)      :: relCdnIndex
381
      INTEGER, INTENT (IN)      :: archiveType
382
      REAL,    INTENT (IN)      :: fermiEnergy, distance
383
      LOGICAL, INTENT (IN)      :: l_qfix
384

385 386
      CHARACTER(LEN=*), OPTIONAL, INTENT(IN)  :: inFilename

387 388 389 390 391 392
      TYPE(t_stars)        :: starsTemp
      TYPE(t_vacuum)       :: vacuumTemp
      TYPE(t_atoms)        :: atomsTemp
      TYPE(t_sphhar)       :: sphharTemp
      TYPE(t_input)        :: inputTemp
      TYPE(t_sym)          :: symTemp
393 394
      TYPE(t_cell)         :: cellTemp
      TYPE(t_oneD)         :: oneDTemp
395 396 397 398 399

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

      INTEGER           :: mode, iterTemp, k, i, iVac, j, iUnit
400
      INTEGER           :: d1, d10, asciioffset, iUnitTemp
401
      LOGICAL           :: l_exist, l_storeIndices, l_writeNew, l_same
402
      LOGICAL           :: l_writeAll
403
      CHARACTER(len=30) :: filename
404
      CHARACTER(len=5)  :: cdnfile
405

406 407 408 409
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER           :: currentStarsIndex,currentLatharmsIndex
410
      INTEGER           :: currentStructureIndex,currentStepfunctionIndex
411 412 413
      INTEGER           :: readDensityIndex, writeDensityIndex, lastDensityIndex
      INTEGER           :: previousDensityIndex, densityType
      INTEGER           :: starsIndexTemp, latharmsIndexTemp, structureIndexTemp
414
      INTEGER           :: stepfunctionIndexTemp
415
      INTEGER           :: jspinsTemp
416
      INTEGER           :: date, time, dateTemp, timeTemp
417
      REAL              :: fermiEnergyTemp, distanceTemp
418
      LOGICAL           :: l_qfixTemp, l_CheckBroyd
419
      CHARACTER(LEN=30) :: archiveName
420 421 422
      CHARACTER(LEN=8)  :: dateString
      CHARACTER(LEN=10) :: timeString
      CHARACTER(LEN=10) :: zone
423

Gregor Michalicek's avatar
Gregor Michalicek committed
424 425
      COMPLEX, ALLOCATABLE :: cdomvz(:,:)

426
      CALL getIOMode(mode)
427 428 429
      CALL DATE_AND_TIME(dateString,timeString,zone)
      READ(dateString,'(i8)') date
      READ(timeString,'(i6)') time
430

431 432 433
      l_CheckBroyd = .TRUE.
      IF(PRESENT(inFilename)) l_CheckBroyd = .FALSE.

434
      IF(mode.EQ.CDN_HDF5_MODE) THEN
435
#ifdef CPP_HDF
436
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
437
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex,inFilename)
438

439 440
         CALL checkAndWriteMetadataHDF(fileID, input, atoms, cell, vacuum, oneD, stars, sphhar, sym,&
                                       currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
441
                                       currentStepfunctionIndex,l_storeIndices,l_CheckBroyd)
442

443 444
         previousDensityIndex = readDensityIndex
         writeDensityIndex = readDensityIndex
445
         IF(relCdnIndex.NE.0) THEN
446 447 448
            writeDensityIndex = lastDensityIndex+relCdnIndex
            lastDensityIndex = writeDensityIndex
            readDensityIndex = writeDensityIndex
449 450 451 452 453 454 455
            l_storeIndices = .TRUE.
         END IF

         archiveName = ''
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            archiveName = 'cdn'
         ELSE
456
            WRITE(archiveName,'(a,i0)') '/cdn-', writeDensityIndex
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
         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

478 479 480 481 482
         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,&
483
                                        stepfunctionIndexTemp,previousDensityIndex, jspinsTemp,&
484
                                        dateTemp, timeTemp, distanceTemp, fermiEnergyTemp, l_qfixTemp)
485 486 487
            END IF
         END IF

488
         IF(vacuum%nvac.EQ.1) THEN
489
            den%vacz(:,2,:)=den%vacz(:,1,:)
490
            IF (sym%invs) THEN
491
               den%vacxy(:,:,2,:) = CONJG(den%vacxy(:,:,1,:))
492
            ELSE
493
               den%vacxy(:,:,2,:) = den%vacxy(:,:,1,:)
494 495 496
            END IF
         END IF

497
         CALL writeDensityHDF(input, fileID, archiveName, densityType, previousDensityIndex,&
498
                              currentStarsIndex, currentLatharmsIndex, currentStructureIndex,&
499 500
                              currentStepfunctionIndex,date,time,distance,fermiEnergy,l_qfix,&
                              den%iter+relCdnIndex,den)
501 502

         IF(l_storeIndices) THEN
503 504
            CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                    currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
505 506
         END IF

507
         CALL closeCDNPOT_HDF(fileID)
508
#endif
509 510 511 512 513 514 515
      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'
516
            IF(inOrOutCDN.EQ.CDN_OUTPUT_DEN_const) filename = 'rhomat_out'
517 518 519 520 521
         END IF
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_CDN_const) THEN
            filename = 'cdn'
         END IF

522 523
         IF(PRESENT(inFilename)) filename = inFilename

524
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(den%iter.EQ.0)) THEN
525 526 527 528 529 530
            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

531 532 533
         iUnit = 93
         OPEN (iUnit,file=TRIM(ADJUSTL(filename)),FORM='unformatted',STATUS='unknown')

534
         IF ((relCdnIndex.EQ.1).AND.(archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).AND.(den%iter.GE.1)) THEN
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
            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
559 560 561 562
            vacuumTemp%nmzxy = vacuum%nmzxy
            vacuumTemp%nmz = vacuum%nmz
            vacuumTemp%dvac = vacuum%dvac
            vacuumTemp%delz = vacuum%delz
563 564
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
565 566
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
567 568 569 570
            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
571 572
            d1 = MOD(den%iter,10)
            d10 = MOD(INT((den%iter+0.5)/10),10)
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
            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

594
         IF ((inOrOutCDN.EQ.CDN_OUTPUT_DEN_const).AND.(archiveType.NE.CDN_ARCHIVE_TYPE_NOCO_const)) THEN
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

            ! 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
616 617 618 619
            vacuumTemp%nmzxy = vacuum%nmzxy
            vacuumTemp%nmz = vacuum%nmz
            vacuumTemp%dvac = vacuum%dvac
            vacuumTemp%delz = vacuum%delz
620 621
            starsTemp%ng2 = stars%ng2
            symTemp%invs2 = sym%invs2
622 623
            ALLOCATE (fpwTemp(stars%ng3,input%jspins))
            ALLOCATE (fzxyTemp(vacuum%nmzxyd,stars%ng2-1,2,input%jspins))
624 625 626 627 628 629 630 631 632 633 634 635
            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,&
636
                     iUnit,den%iter+relCdnIndex,den%mt,den%pw,den%vacz,den%vacxy)
637 638 639

         ! Write additional data if l_noco
         IF (archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
640
            WRITE (iUnit) (den%pw(k,3),k=1,stars%ng3)
641
            IF (input%film) THEN
Gregor Michalicek's avatar
Gregor Michalicek committed
642 643 644 645 646 647 648
               ALLOCATE(cdomvz(vacuum%nmz,vacuum%nvac))
               DO iVac = 1, vacuum%nvac
                  DO i = 1, vacuum%nmz
                     cdomvz(i,iVac) = CMPLX(den%vacz(i,iVac,3),den%vacz(i,iVac,4))
                  END DO
               END DO
               WRITE (iUnit) ((cdomvz(i,iVac),i=1,vacuum%nmz),iVac=1,vacuum%nvac)
649
               WRITE (iUnit) (((den%vacxy(i,j-1,iVac,3),i=1,vacuum%nmzxy),j=2,stars%ng2), iVac=1,vacuum%nvac)
Gregor Michalicek's avatar
Gregor Michalicek committed
650
               DEALLOCATE(cdomvz)
651 652 653 654
            END IF
         END IF

         CLOSE(iUnit)
655
      END IF
656

657
      !write density matrix to n_mmp_mat_out file
Gregor Michalicek's avatar
Gregor Michalicek committed
658
      IF((inOrOutCDN.EQ.CDN_INPUT_DEN_const).AND.(relCdnIndex.EQ.1).AND.&
659 660
         ((archiveType.EQ.CDN_ARCHIVE_TYPE_CDN1_const).OR.(archiveType.EQ.CDN_ARCHIVE_TYPE_NOCO_const))) THEN
         IF(atoms%n_u.GT.0) THEN
661 662 663 664 665 666 667 668 669 670 671
            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
672
               END IF
673
               OPEN (69,file=TRIM(ADJUSTL(filename)),status='replace',form='formatted')
674 675
               WRITE (69,'(7f20.13)') den%mmpMat(:,:,:,:)
               CLOSE (69)
676 677
            END IF
         END IF
678 679 680 681
      END IF

   END SUBROUTINE writeDensity

682 683 684 685 686 687 688 689 690 691
   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
692
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
693 694 695
      INTEGER        :: readDensityIndex, lastDensityIndex

      INTEGER           :: starsIndex, latharmsIndex, structureIndex
696
      INTEGER           :: stepfunctionIndex
697
      INTEGER           :: date, time, iter, jspins, previousDensityIndex
698
      REAL              :: fermiEnergy, distance
699 700 701
      LOGICAL           :: l_qfix, l_exist
      CHARACTER(LEN=30) :: archiveName

702
      CALL getIOMode(mode)
703 704 705 706 707 708

      eFermiPrev = 0.0
      l_error = .FALSE.
      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
709
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
710
         WRITE(archiveName,'(a,i0)') '/cdn-', readDensityIndex
Gregor Michalicek's avatar
Gregor Michalicek committed
711
         CALL peekDensityEntryHDF(fileID, archiveName, DENSITY_TYPE_NOCO_IN_const,&
712
                                  iter, starsIndex, latharmsIndex, structureIndex, stepfunctionIndex,&
713
                                  previousDensityIndex, jspins, date, time, distance, fermiEnergy, l_qfix)
Gregor Michalicek's avatar
Gregor Michalicek committed
714
         eFermiPrev = fermiEnergy
715
         CALL closeCDNPOT_HDF(fileID)
716 717 718 719 720 721 722 723 724
#endif
      ELSE IF(mode.EQ.CDN_STREAM_MODE) THEN
         STOP 'cdn.str not yet implemented!'
      ELSE
         l_error = .TRUE.
      END IF

   END SUBROUTINE

725 726 727 728 729 730
   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

731 732 733
      REAL, INTENT(OUT) :: rhcs(:,:,:)!(atoms%jmtd,atoms%ntype,input%jspins)
      REAL, INTENT(OUT) :: tecs(:,:)!(atoms%ntype,input%jspins)
      REAL, INTENT(OUT) :: qints(:,:)!(atoms%ntype,input%jspins)
734 735 736 737 738

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

739 740 741 742
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
743
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
744
      INTEGER        :: readDensityIndex, lastDensityIndex
745

746
      CALL getIOMode(mode)
747 748

      IF(mode.EQ.CDN_HDF5_MODE) THEN
749 750
#ifdef CPP_HDF
         l_exist = isCoreDensityPresentHDF()
751
         IF (l_exist) THEN
752
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
753
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
754
            CALL readCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
755
            CALL closeCDNPOT_HDF(fileID)
756 757
            RETURN
         ELSE
758
            WRITE(*,*) 'No core density is available in HDF5 format.'
759 760 761
            WRITE(*,*) 'Falling back to stream access file cdn.str.'
            mode = CDN_STREAM_MODE
         END IF
762
#endif
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
      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

804 805 806
      REAL, INTENT(IN) :: rhcs(:,:,:)!(atoms%jmtd,atoms%ntype,input%jspins)
      REAL, INTENT(IN) :: tecs(:,:)!(atoms%ntype,input%jspins)
      REAL, INTENT(IN) :: qints(:,:)!(atoms%ntype,input%jspins)
807 808 809

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

810 811 812 813
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
      INTEGER        :: currentStarsIndex,currentLatharmsIndex
814
      INTEGER        :: currentStructureIndex,currentStepfunctionIndex
815
      INTEGER        :: readDensityIndex, lastDensityIndex
816

817
      CALL getIOMode(mode)
818 819

      IF(mode.EQ.CDN_HDF5_MODE) THEN
820
#ifdef CPP_HDF
821
         CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
822
                          currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
823
         CALL writeCoreDensityHDF(fileID,input,atoms,dimension,rhcs,tecs,qints)
824
         CALL closeCDNPOT_HDF(fileID)
825
#endif
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
      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

844
   SUBROUTINE storeStructureIfNew(input,stars, atoms, cell, vacuum, oneD, sym,mpi,sphhar,noco)
845 846 847 848 849 850

      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
851
      TYPE(t_sym),INTENT(IN)     :: sym
852 853 854 855
      TYPE(t_mpi),INTENT(IN)      :: mpi
      TYPE(t_sphhar),INTENT(IN)   :: sphhar
      TYPE(t_noco),INTENT(IN)     :: noco
      TYPE(t_stars),INTENT(IN)    :: stars
856 857 858 859 860 861

      TYPE(t_input)              :: inputTemp
      TYPE(t_atoms)              :: atomsTemp
      TYPE(t_cell)               :: cellTemp
      TYPE(t_vacuum)             :: vacuumTemp
      TYPE(t_oneD)               :: oneDTemp
862
      TYPE(t_sym)                :: symTemp
863 864 865 866 867 868 869 870 871

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

872
      CALL getIOMode(mode)
873 874 875 876 877 878 879 880 881 882 883

      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
884
            CALL readStructureHDF(fileID, inputTemp, atomsTemp, cellTemp, vacuumTemp, oneDTemp, symTemp, currentStructureIndex)
885
            CALL compareStructure(input, atoms, vacuum, cell, sym, inputTemp, atomsTemp, vacuumTemp, cellTemp, symTemp, l_same)
886
            IF(.NOT.l_same) THEN
887
               currentStructureIndex = currentStructureIndex + 1
888 889 890 891
               l_writeStructure = .TRUE.
            END IF
         END IF

892
 
893
         IF (l_writeStructure) THEN
894
            CALL writeStructureHDF(fileID, input, atoms, cell, vacuum, oneD, sym, currentStructureIndex,.TRUE.)
895 896 897 898 899 900 901 902 903 904 905 906 907
            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

908
   END SUBROUTINE storeStructureIfNew
909

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
   SUBROUTINE transform_by_moving_atoms(mpi,stars,atoms,vacuum, cell, sym, sphhar,input,oned,noco)
     USE m_types
     USE m_qfix
     use m_fix_by_gaussian
     IMPLICIT NONE
     TYPE(t_mpi),INTENT(IN)      :: mpi
     TYPE(t_atoms),INTENT(IN)    :: atoms
     TYPE(t_sym),INTENT(IN)      :: sym
     TYPE(t_vacuum),INTENT(IN)   :: vacuum
     TYPE(t_sphhar),INTENT(IN)   :: sphhar
     TYPE(t_input),INTENT(IN)    :: input
     TYPE(t_oneD),INTENT(IN)     :: oneD
     TYPE(t_cell),INTENT(IN)     :: cell
     TYPE(t_noco),INTENT(IN)     :: noco
     TYPE(t_stars),INTENT(IN)    :: stars
     !Locals
     INTEGER :: archiveType
     LOGICAL :: l_qfix
     REAL    :: fermiEnergy,fix
     REAL    :: shifts(3,SIZE(atoms%taual,2))

     TYPE(t_potden):: den

     TYPE(t_input)              :: inputTemp
     TYPE(t_atoms)              :: atomsTemp
     TYPE(t_cell)               :: cellTemp
     TYPE(t_vacuum)             :: vacuumTemp
     TYPE(t_oneD)               :: oneDTemp
     TYPE(t_sym)                :: symTemp
     

     INTEGER :: mode,currentStarsIndex,currentLatharmsIndex,currentStructureIndex
     INTEGER :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex,structureindex
     LOGICAL :: l_same,l_structure_by_shift

#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
      character(len=50) :: archivename
#endif
#ifdef CPP_MPI
      INCLUDE 'mpif.h'
      integer :: ierr
#endif
      l_same=.TRUE.;l_structure_by_shift=.TRUE.
          
      CALL getIOMode(mode)
      IF(mode.EQ.CDN_HDF5_MODE) THEN
#ifdef CPP_HDF
         IF (mpi%irank==0) THEN
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                 currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
            IF (currentStructureIndex>0.AND.lastdensityindex>0) THEN
               !Determine structure of last density
               WRITE(archivename,"(a,i0)") "cdn-",lastdensityindex
               CALL peekDensityEntryHDF(fileID, archivename, DENSITY_TYPE_IN_const, structureIndex=structureIndex)
               !Read that structure
               CALL readStructureHDF(fileID, inputTemp, atomsTemp, cellTemp, vacuumTemp, oneDTemp, symTemp, StructureIndex)
               CALL compareStructure(input, atoms, vacuum, cell, sym, inputTemp, atomsTemp, vacuumTemp, cellTemp, symTemp, l_same,l_structure_by_shift)
            ENDIF
            CALL closeCDNPOT_HDF(fileID)
         ENDIF
#ifdef CPP_MPI
Daniel Wortmann's avatar
Daniel Wortmann committed
972 973
         CALL mpi_bcast(l_same,1,MPI_LOGICAL,0,mpi%mpi_comm,ierr)
         CALL mpi_bcast(l_structure_by_shift,1,MPI_LOGICAL,0,mpi%mpi_comm,ierr)
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
#endif
         IF (l_same.OR..NOT.l_structure_by_shift) RETURN ! nothing to do
         
         IF (mpi%irank==0) THEN
            WRITE(6,*) "Atomic movement detected, trying to adjust charge density" 
            
            !Calculate shifts
            shifts=atomsTemp%taual-atoms%taual
            
            !Determine type of charge
            archiveType = MERGE(CDN_ARCHIVE_TYPE_NOCO_const,CDN_ARCHIVE_TYPE_CDN1_const,noco%l_noco)
            !read the current density
            CALL den%init(stars,atoms,sphhar,vacuum,noco,input%jspins,POTDEN_TYPE_DEN)
            CALL readDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,CDN_INPUT_DEN_const,&
                 0,fermiEnergy,l_qfix,den)
         ENDIF
         !Now fix the density
         SELECT CASE(input%qfix)
         CASE (0,1) !just qfix the density
            IF (mpi%irank==0) WRITE(6,*) "Using qfix to adjust density"
            CALL qfix(mpi,stars,atoms,sym,vacuum,sphhar,input,cell,oneD,&
                 den,noco%l_noco,mpi%isize==1,force_fix=.TRUE.,fix=fix)
996 997 998 999
         CASE(2,3)
            CALL qfix(mpi,stars,atoms,sym,vacuum,sphhar,input,cell,oneD,&
                 den,noco%l_noco,mpi%isize==1,force_fix=.TRUE.,fix=fix,fix_pw_only=.true.)
         CASE(4,5)
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
            CALL fix_by_gaussian(shifts,atoms,stars,mpi,sym,vacuum,sphhar,input,oned,cell,noco,den)
         CASE default
            CALL judft_error("Wrong choice of qfix in input")
         END SELECT
         !Now write the density to file
         IF (mpi%irank==0) CALL writedensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,CDN_INPUT_DEN_const,&
              0,-1.0,fermiEnergy,l_qfix,den)
         
#endif
      END IF
    END SUBROUTINE transform_by_moving_atoms

1012 1013 1014 1015 1016
   SUBROUTINE writeStars(stars,l_xcExtended,l_ExtData)

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

1017 1018 1019
      INTEGER        :: mode, ngz, izmin, izmax

      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
1020
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
1021 1022 1023
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif
1024 1025 1026 1027 1028 1029 1030 1031

      INTEGER :: igz(stars%ng3)

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

1032
      CALL getIOMode(mode)
1033 1034 1035

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

         currentStarsIndex = currentStarsIndex + 1
1040
         CALL writeStarsHDF(fileID, currentStarsIndex, currentStructureIndex, stars,.TRUE.)
1041

1042 1043
         CALL writeCDNHeaderData(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
                                 currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
1044 1045

         CALL closeCDNPOT_HDF(fileID)
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
#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)
1079

1080 1081 1082 1083
      TYPE(t_stars),INTENT(INOUT) :: stars
      LOGICAL, INTENT(IN)         :: l_xcExtended,l_ExtData
      LOGICAL, INTENT(OUT)        :: l_error

1084 1085

      TYPE(t_stars)               :: starsTemp
1086
      INTEGER                     :: mode, ioStatus, ngz,izmin,izmax
1087
      LOGICAL                     :: l_exist, l_same
1088

1089
      INTEGER        :: structureIndexTemp
1090
      INTEGER        :: currentStarsIndex,currentLatharmsIndex,currentStructureIndex
1091
      INTEGER        :: currentStepfunctionIndex,readDensityIndex,lastDensityIndex
1092 1093 1094 1095
#ifdef CPP_HDF
      INTEGER(HID_T) :: fileID
#endif

1096 1097 1098 1099 1100 1101 1102 1103
      INTEGER :: igz(stars%ng3)

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

1104
      CALL getIOMode(mode)
1105 1106 1107 1108 1109

      IF(mode.EQ.CDN_HDF5_MODE) THEN
         INQUIRE(FILE='cdn.hdf',EXIST=l_exist)
         IF (l_exist) THEN
#ifdef CPP_HDF
1110
            CALL openCDN_HDF(fileID,currentStarsIndex,currentLatharmsIndex,currentStructureIndex,&
1111
                             currentStepfunctionIndex,readDensityIndex,lastDensityIndex)
1112 1113 1114
            IF (currentStarsIndex.LT.1) THEN
               mode = CDN_DIRECT_MODE ! (no stars entry found in cdn.hdf file)
            ELSE
1115 1116 1117 1118 1119 1120
               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
1121 1122 1123 1124 1125
               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
1126 1127
            END IF
            CALL closeCDNPOT_HDF(fileID)
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 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
#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%