broyd_io.F90 16.2 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! Copyright (c) 2018 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! 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.
!--------------------------------------------------------------------------------

7 8 9 10 11 12 13 14 15
MODULE m_broyd_io

USE m_types
USE m_cdn_io

IMPLICIT NONE

CONTAINS

16
SUBROUTINE readLastIterInAndDiffDen(hybrid,vecLen,nextIter,alpha,inDenVec,diffDenVec,inDenVecMet,diffDenVecMet)
17 18 19 20 21 22

   TYPE(t_hybrid), INTENT(IN)  :: hybrid
   INTEGER,        INTENT(IN)  :: vecLen
   INTEGER,        INTENT(OUT) :: nextIter
   REAL,           INTENT(OUT) :: alpha
   REAL,           INTENT(OUT) :: inDenVec(vecLen), diffDenVec(vecLen)
23
   REAL,OPTIONAL,  INTENT(OUT) :: inDenVecMet(vecLen), diffDenVecMet(vecLen)
24 25 26 27 28 29 30 31 32 33 34 35 36

   INTEGER             :: mode

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   IF (hybrid%l_calhf) THEN
      OPEN (57,file='hf_broyd',form='unformatted',status='unknown')
   ELSE
      OPEN (57,file='broyd',form='unformatted',status='unknown')
   ENDIF

37 38 39 40 41
   IF(PRESENT(inDenVecMet)) THEN
      READ(57) nextIter,alpha,diffDenVec,inDenVec,inDenVecMet,diffDenVecMet
   ELSE
      READ(57) nextIter,alpha,diffDenVec,inDenVec
   END IF
42 43 44 45 46

   CLOSE(57)

END SUBROUTINE readLastIterInAndDiffDen

47
SUBROUTINE writeLastIterInAndDiffDen(hybrid,vecLen,nextIter,alpha,inDenVec,diffDenVec,inDenVecMet,diffDenVecMet)
48 49 50 51 52

   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: nextIter,vecLen
   REAL,           INTENT(IN) :: alpha
   REAL,           INTENT(IN) :: inDenVec(vecLen), diffDenVec(vecLen)
53
   REAL,OPTIONAL,  INTENT(IN) :: inDenVecMet(vecLen), diffDenVecMet(vecLen)
54 55 56 57 58 59 60 61 62 63 64 65 66

   INTEGER             :: mode

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   IF (hybrid%l_calhf) THEN
      OPEN (57,file='hf_broyd',form='unformatted',status='unknown')
   ELSE
      OPEN (57,file='broyd',form='unformatted',status='unknown')
   ENDIF

67 68 69 70 71
   IF(PRESENT(inDenVecMet)) THEN
      WRITE(57) nextIter,alpha,diffDenVec,inDenVec,inDenVecMet,diffDenVecMet
   ELSE
      WRITE(57) nextIter,alpha,diffDenVec,inDenVec
   END IF
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

   CLOSE(57)

END SUBROUTINE writeLastIterInAndDiffDen

SUBROUTINE readUVec(input,hybrid,vecLen,relIter,currentIter,uVec)

   TYPE(t_input),  INTENT(IN)  :: input
   TYPE(t_hybrid), INTENT(IN)  :: hybrid
   INTEGER,        INTENT(IN)  :: vecLen, relIter, currentIter
   REAL,           INTENT(OUT) :: uVec(vecLen)

   INTEGER             :: mode, npos
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter+relIter-1
   IF (currentIter.GT.input%maxiter+1) npos = MOD(currentIter-2,input%maxiter)+1

   READ(59,rec=2*npos-1) uVec(:vecLen)

   CLOSE(59)

END SUBROUTINE readUVec

SUBROUTINE writeUVec(input,hybrid,vecLen,currentIter,uVec)

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: vecLen, currentIter
   REAL,           INTENT(IN) :: uVec(vecLen)

   INTEGER             :: mode, npos
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter-1
   IF (currentIter.GT.input%maxiter+1) npos = MOD(currentIter-2,input%maxiter)+1

   WRITE(59,rec=2*npos-1) uVec(:vecLen)

   CLOSE(59)

END SUBROUTINE writeUVec

SUBROUTINE readVVec(input,hybrid,vecLen,relIter,currentIter,dfivi,vVec)

   TYPE(t_input),  INTENT(IN)  :: input
   TYPE(t_hybrid), INTENT(IN)  :: hybrid
   INTEGER,        INTENT(IN)  :: vecLen, relIter, currentIter
   REAL,           INTENT(OUT) :: dfivi
   REAL,           INTENT(OUT) :: vVec(vecLen)

   INTEGER             :: mode, npos
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter+relIter-1
   IF (currentIter.GT.input%maxiter+1) npos = MOD(currentIter-2,input%maxiter)+1

   READ(59,rec=2*npos) vVec(:vecLen), dfivi

   CLOSE(59)

END SUBROUTINE readVVec

SUBROUTINE writeVVec(input,hybrid,vecLen,currentIter,dfivi,vVec)

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: vecLen, currentIter
   REAL,           INTENT(IN) :: dfivi
   REAL,           INTENT(IN) :: vVec(vecLen)

   INTEGER             :: mode, npos
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter-1
   IF (currentIter.GT.input%maxiter+1) npos = MOD(currentIter-2,input%maxiter)+1

   WRITE(59,rec=2*npos) vVec(:vecLen), dfivi

   CLOSE(59)

END SUBROUTINE writeVVec

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225








SUBROUTINE readDeltaNVec(input,hybrid,vecLen,relIter,currentIter,deltaNVec)

   TYPE(t_input),  INTENT(IN)  :: input
   TYPE(t_hybrid), INTENT(IN)  :: hybrid
   INTEGER,        INTENT(IN)  :: vecLen, relIter, currentIter
   REAL,           INTENT(OUT) :: deltaNVec(vecLen)

226
   INTEGER             :: mode, npos, iter
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd_DN',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd_DN',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

243 244 245 246 247
   iter = currentIter+relIter
   npos=iter-1
   IF (iter.GT.1) npos = MOD(iter-2,input%maxiter)+1

!   WRITE(1400,*) 'readDeltaNVec: iter,npos: ', iter, npos
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

   READ(59,rec=npos) deltaNVec(:vecLen)

   CLOSE(59)

END SUBROUTINE readDeltaNVec

SUBROUTINE writeDeltaNVec(input,hybrid,vecLen,currentIter,deltaNVec)

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: vecLen, currentIter
   REAL,           INTENT(IN) :: deltaNVec(vecLen)

   INTEGER             :: mode, npos
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd_DN',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd_DN',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter-1
280 281 282
   IF (currentIter.GT.1) npos = MOD(currentIter-2,input%maxiter)+1

!   WRITE(1500,*) 'writeDeltaNVec: iter,npos: ', currentIter, npos
283 284 285 286 287 288 289 290 291 292 293 294 295 296

   WRITE(59,rec=npos) deltaNVec(:vecLen)

   CLOSE(59)

END SUBROUTINE writeDeltaNVec

SUBROUTINE readDeltaFVec(input,hybrid,vecLen,relIter,currentIter,deltaFVec)

   TYPE(t_input),  INTENT(IN)  :: input
   TYPE(t_hybrid), INTENT(IN)  :: hybrid
   INTEGER,        INTENT(IN)  :: vecLen, relIter, currentIter
   REAL,           INTENT(OUT) :: deltaFVec(vecLen)

297
   INTEGER             :: mode, npos, iter
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd_DF',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd_DF',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

314 315 316 317 318
   iter = currentIter+relIter
   npos=iter-1
   IF (iter.GT.1) npos = MOD(iter-2,input%maxiter)+1

!   WRITE(1400,*) 'readDeltaFVec: iter,npos: ', iter, npos
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

   READ(59,rec=npos) deltaFVec(:vecLen)

   CLOSE(59)

END SUBROUTINE readDeltaFVec

SUBROUTINE writeDeltaFVec(input,hybrid,vecLen,currentIter,deltaFVec)

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: vecLen, currentIter
   REAL,           INTENT(IN) :: deltaFVec(vecLen)

   INTEGER             :: mode, npos
   INTEGER*8           :: recLen

   CALL getIOMode(mode)

   ! At the moment broyden IO is mode independent

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd_DF',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd_DF',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter-1
351 352 353
   IF (currentIter.GT.1) npos = MOD(currentIter-2,input%maxiter)+1

!   WRITE(1500,*) 'writeDeltaFVec: iter,npos: ', currentIter, npos
354 355 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 382 383 384 385 386

   WRITE(59,rec=npos) deltaFVec(:vecLen)

   CLOSE(59)

END SUBROUTINE writeDeltaFVec


SUBROUTINE writeBroydenOverlapExt(input,hybrid,currentIter,historyLength,&
                                  dNdNLast,dFdFLast,dNdFLast,dFdNLast)

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: currentIter, historyLength
   REAL,           INTENT(IN) :: dNdNLast(input%maxIter)
   REAL,           INTENT(IN) :: dFdFLast(input%maxIter)
   REAL,           INTENT(IN) :: dNdFLast(input%maxIter)
   REAL,           INTENT(IN) :: dFdNLast(input%maxIter)

   INTEGER                    :: recLen, npos

   recLen = 8*4*input%maxIter    ! sizeOfReal*numberOfVectors*vectorLength
   recLen = recLen + 2*8         ! storage for currentIter, historyLength

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broydOvlp',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broydOvlp',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   npos=currentIter-1
387 388 389
   IF (currentIter.GT.1) npos = MOD(currentIter-2,input%maxiter)+1

!   WRITE(1500,*) 'writeBroydenOverlapExt: iter, npos: ', currentIter, npos
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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

   WRITE(59,rec=npos) currentIter, historyLength,&
                      dNdNLast(:input%maxIter), dFdFLast(:input%maxIter), &
                      dNdFLast(:input%maxIter), dFdNLast(:input%maxIter)

   CLOSE(59)

END SUBROUTINE writeBroydenOverlapExt

SUBROUTINE readBroydenOverlaps(input,hybrid,currentIter,historyLength,&
                               dNdNMat,dFdFMat,dNdFMat,dFdNMat)

   TYPE(t_input),  INTENT(IN)    :: input
   TYPE(t_hybrid), INTENT(IN)    :: hybrid
   INTEGER,        INTENT(IN)    :: currentIter, historyLength
   REAL,           INTENT(INOUT) :: dNdNMat(historyLength,historyLength)
   REAL,           INTENT(INOUT) :: dFdFMat(historyLength,historyLength)
   REAL,           INTENT(INOUT) :: dNdFMat(historyLength,historyLength)
   REAL,           INTENT(INOUT) :: dFdNMat(historyLength,historyLength)

   INTEGER                    :: i, j
   INTEGER                    :: recLen, npos, iter
   INTEGER                    :: recIter, recHistLen

   REAL                       :: dNdNLast(input%maxIter)
   REAL                       :: dFdFLast(input%maxIter)
   REAL                       :: dNdFLast(input%maxIter)
   REAL                       :: dFdNLast(input%maxIter)

   recLen = 8*4*input%maxIter    ! sizeOfReal*numberOfVectors*vectorLength
   recLen = recLen + 2*8         ! storage for currentIter, historyLength

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broydOvlp',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broydOvlp',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   dNdNMat = 0.0
   dFdFMat = 0.0
   dNdFMat = 0.0
   dFdNMat = 0.0

   DO i = 1, historyLength

      iter = currentIter - historyLength + i
      npos=iter-1
439 440 441
      IF (iter.GT.1) npos = MOD(iter-2,input%maxiter)+1

!      WRITE(1400,*) 'readBroydenOverlaps: iter,npos: ', iter, npos
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

      READ(59,rec=npos) recIter, recHistLen,&
                        dNdNLast(:input%maxIter), dFdFLast(:input%maxIter), &
                        dNdFLast(:input%maxIter), dFdNLast(:input%maxIter)

      DO j = 1, recHistLen
         IF ((j-recHistLen+i).LE.0) CYCLE
         dNdNMat(j-recHistLen+i,i) = dNdNLast(j)
         dFdFMat(j-recHistLen+i,i) = dFdFLast(j)
         dNdFMat(j-recHistLen+i,i) = dNdFLast(j)
         dFdNMat(j-recHistLen+i,i) = dFdNLast(j)
      END DO
   END DO

   CLOSE(59)

END SUBROUTINE readBroydenOverlaps







466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
SUBROUTINE resetBroydenHistory()

   INTEGER           :: i
   LOGICAL           :: l_exist
   CHARACTER(LEN=20) :: filename
   INQUIRE(file='broyd',exist=l_exist)
   IF (l_exist) THEN
      CALL system('rm broyd')
      PRINT *,"Broyden history has been reset."
   END IF
   DO i = 1, 9
      filename = 'broyd.'//CHAR(i+48)
      INQUIRE(file=TRIM(ADJUSTL(filename)),exist=l_exist)
      IF (l_exist) THEN
         CALL system('rm '//TRIM(ADJUSTL(filename)))
      END IF
      filename = 'hf_broyd.'//CHAR(i+48)
      INQUIRE(file=TRIM(ADJUSTL(filename)),exist=l_exist)
      IF (l_exist) THEN
         CALL system('rm '//TRIM(ADJUSTL(filename)))
      END IF
   END DO
488 489 490 491 492 493 494 495 496 497 498 499 500 501
   INQUIRE(file='broydOvlp',exist=l_exist)
   IF (l_exist) CALL system('rm broydOvlp')
   INQUIRE(file='hf_broydOvlp',exist=l_exist)
   IF (l_exist) CALL system('rm hf_broydOvlp')

   INQUIRE(file='broyd_DF',exist=l_exist)
   IF (l_exist) CALL system('rm broyd_DF')
   INQUIRE(file='hf_broyd_DF',exist=l_exist)
   IF (l_exist) CALL system('rm hf_broyd_DF')

   INQUIRE(file='broyd_DN',exist=l_exist)
   IF (l_exist) CALL system('rm broyd_DN')
   INQUIRE(file='hf_broyd_DN',exist=l_exist)
   IF (l_exist) CALL system('rm hf_broyd_DN')
502 503 504

END SUBROUTINE resetBroydenHistory

505 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
LOGICAL FUNCTION initBroydenHistory(input,hybrid, vecLen)
! Initializes a Broyden history
! returns true if there already exists a Broyden history

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: vecLen

   INTEGER*8                  :: recLen
   LOGICAL                    :: l_exist

   INQUIRE (file='broyd.'//CHAR(input%imix+48),exist=l_exist)

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd.'//CHAR(input%imix+48),access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   CLOSE(59)

   initBroydenHistory = l_exist

END FUNCTION initBroydenHistory

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
LOGICAL FUNCTION initBroydenHistory2(input,hybrid, vecLen)
! Initializes a Broyden history
! returns true if there already exists a Broyden history

   TYPE(t_input),  INTENT(IN) :: input
   TYPE(t_hybrid), INTENT(IN) :: hybrid
   INTEGER,        INTENT(IN) :: vecLen

   INTEGER*8                  :: recLen
   LOGICAL                    :: l_exist

   INQUIRE (file='broyd_DF',exist=l_exist)

   recLen=(vecLen+1)*8

   IF (hybrid%l_calhf) THEN
      OPEN (59,file='hf_broyd_DF',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
      OPEN (60,file='hf_broyd_DN',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ELSE
      OPEN (59,file='broyd_DF',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
      OPEN (60,file='broyd_DN',access='direct',&
            recl=recLen,form='unformatted',status='unknown')
   ENDIF

   CLOSE(59)
   CLOSE(60)

   initBroydenHistory2 = l_exist

END FUNCTION initBroydenHistory2

568
END MODULE m_broyd_io