mixing_history.F90 3.85 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! Copyright (c) 2016 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.
!--------------------------------------------------------------------------------
MODULE m_mixing_history
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 66 67 68
  USE m_types_mixvector
  IMPLICIT NONE
  PRIVATE
  INTEGER:: iter_stored=0
  TYPE(t_mixvector),ALLOCATABLE::sm_store(:),fsm_store(:)
  PUBLIC :: mixing_history,mixing_history_reset,mixing_history_store
  PUBLIC :: mixing_history_open,mixing_history_close
CONTAINS
  
  SUBROUTINE mixing_history_open(mpi,maxiter)
    USE m_types,ONLY:t_mpi
    INTEGER,INTENT(IN)    :: maxiter
    TYPE(t_mpi),INTENT(in):: mpi
    
    CHARACTER(len=20):: filename
    LOGICAL          :: l_fileexist
    INTEGER          :: n


    IF (iter_stored>0) RETURN ! History in memory found, no need to do IO
    IF (mpi%isize>1) THEN
       WRITE(filename,'(a,i0)') "mixing_history.",mpi%irank
    ELSE
       filename="mixing_history"
    ENDIF
    INQUIRE(file=filename,exist=l_fileexist)
    IF (.NOT.l_fileexist) RETURN !No previous data
    OPEN(888,file=filename,status='old',form='unformatted')
    READ(888) iter_stored
    IF (.NOT.ALLOCATED(sm_store)) ALLOCATE(sm_store(maxiter),fsm_store(maxiter))
    DO n=1,MIN(iter_stored,maxiter)
       READ(888) sm_store(n)
       READ(888) fsm_store(n)
    ENDDO
    CLOSE(888)
  END SUBROUTINE mixing_history_open

  SUBROUTINE mixing_history_close(mpi)
    USE m_types,ONLY:t_mpi
    TYPE(t_mpi),INTENT(in):: mpi
    
    CHARACTER(len=20):: filename
    INTEGER          :: n


    IF (iter_stored==0) RETURN ! Nothing found to be stored
    IF (mpi%isize>1) THEN
       WRITE(filename,'(a,i0)') "mixing_history.",mpi%irank
    ELSE
       filename="mixing_history"
    ENDIF
    OPEN(888,file=filename,form='unformatted',status='replace')
    WRITE(888) iter_stored
    DO n=1,iter_stored
       WRITE(888) sm_store(n)
       WRITE(888) fsm_store(n)
    ENDDO
    CLOSE(888)
    DEALLOCATE(sm_store,fsm_store)
    iter_stored=0
  END SUBROUTINE mixing_history_close
    
69
  
Daniel Wortmann's avatar
Daniel Wortmann committed
70
  SUBROUTINE mixing_history(imix,maxiter,inden,outden,sm,fsm,it)
71
    USE m_types
72
    implicit none
Daniel Wortmann's avatar
Daniel Wortmann committed
73
    INTEGER,INTENT(in)::imix,maxiter
74
    type(t_potden),intent(inout)::inden,outden
75
    type(t_mixvector),ALLOCATABLE::sm(:),fsm(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
76 77 78
    INTEGER,INTENT(out)::it

    INTEGER:: n
79 80 81 82

    if (.not.allocated(sm_store)) THEN
       allocate(sm_store(maxiter),fsm_store(maxiter))
    endif
Daniel Wortmann's avatar
Daniel Wortmann committed
83 84
    IF (iter_stored+1==maxiter.AND.imix.NE.9) iter_stored=0 !This is a broyden method which has to 
                                                            !be reset as soon as maxiter is reached
Daniel Wortmann's avatar
Daniel Wortmann committed
85
    it=MIN(iter_stored+1,maxiter+1)
86 87 88 89 90 91 92 93 94
    allocate(sm(it),fsm(it))
    CALL sm(it)%alloc()
    CALL fsm(it)%alloc()
    CALL sm(it)%from_density(inDen)
    CALL fsm(it)%from_density(outDen)
    !store the difference fsm - sm in fsm
    fsm(it) = fsm(it) - sm(it)
    do n=it-1,1,-1 !Copy from storage
       sm(n)=sm_store(n)
Daniel Wortmann's avatar
Daniel Wortmann committed
95
       fsm(n)=fsm_store(n)
96 97 98 99 100 101 102
    ENDDO
    if(iter_stored<maxiter) THEN
       iter_stored=iter_stored+1
       sm_store(:iter_stored)=sm(:iter_stored)
       fsm_store(:iter_stored)=fsm(:iter_stored)
    else
       sm_store(:maxiter-1)=sm(2:maxiter)
Daniel Wortmann's avatar
Daniel Wortmann committed
103
       fsm_store(:maxiter-1)=fsm(2:maxiter)
104 105 106
    endif
  end subroutine mixing_history

107 108
  SUBROUTINE mixing_history_reset(mpi)
    USE m_types,ONLY:t_mpi
109
    IMPLICIT NONE
110
    TYPE(t_mpi),INTENT(in)::mpi
111
    iter_stored=0
Daniel Wortmann's avatar
Daniel Wortmann committed
112
    PRINT *, "Reset of history"
113
    IF (mpi%irank==0) CALL system('rm mixing_history*')
114
  END SUBROUTINE mixing_history_reset
115 116 117 118 119 120
  
  SUBROUTINE mixing_history_store(fsm)
    IMPLICIT NONE
    TYPE(t_mixvector),INTENT(IN)::fsm
    IF (iter_stored>0) fsm_store(iter_stored)=fsm
  END SUBROUTINE mixing_history_store
121
end MODULE m_mixing_history