mix.F90 6.46 KB
Newer Older
1 2 3 4 5
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------
Daniel Wortmann's avatar
Daniel Wortmann committed
6
MODULE m_mix
Gregor Michalicek's avatar
Gregor Michalicek committed
7

8
  !------------------------------------------------------------------------
9
  !  mixing of charge densities or potentials:
10
  !    IMIX = 0 : linear mixing                                     
11 12 13 14
  !    IMIX = 3 : Broyden's First method                            
  !    IMIX = 5 : Broyden's Second method                           
  !    IMIX = 7 : Generalized Anderson method                       
  !------------------------------------------------------------------------
Gregor Michalicek's avatar
Gregor Michalicek committed
15

16
contains
Gregor Michalicek's avatar
Gregor Michalicek committed
17

18 19 20
  SUBROUTINE mix_charge( field, DIMENSION,  mpi, l_writehistory,&
       stars, atoms, sphhar, vacuum, input, sym, cell, noco, &
       oneD, archiveType, inDen, outDen, results )
Gregor Michalicek's avatar
Gregor Michalicek committed
21

22 23 24 25 26 27 28 29
    use m_juDFT
    use m_constants
    use m_cdn_io
    use m_stmix
    use m_broyden
    use m_qfix
    use m_types
    use m_umix
Daniel Wortmann's avatar
Daniel Wortmann committed
30
    USE m_kerker
Daniel Wortmann's avatar
Daniel Wortmann committed
31
    use m_pulay
Daniel Wortmann's avatar
Daniel Wortmann committed
32
    use m_types_mixvector
Daniel Wortmann's avatar
Daniel Wortmann committed
33 34
    USE m_distance
    use m_mixing_history
35 36 37 38 39 40 41
    implicit none

    type(t_oneD),      intent(in)    :: oneD
    type(t_input),     intent(in)    :: input
    type(t_vacuum),    intent(in)    :: vacuum
    type(t_noco),      intent(in)    :: noco
    type(t_sym),       intent(in)    :: sym
42 43 44
    TYPE(t_stars),TARGET,INTENT(in)  :: stars
    TYPE(t_cell),TARGET,INTENT(in)   :: cell
    TYPE(t_sphhar),TARGET,INTENT(in) :: sphhar
45 46 47
    type(t_field),     intent(inout) :: field
    type(t_dimension), intent(in)    :: dimension
    type(t_mpi),       intent(in)    :: mpi
48
    TYPE(t_atoms),TARGET,INTENT(in)  :: atoms 
49 50 51 52
    type(t_potden),    intent(inout) :: outDen
    type(t_results),   intent(inout) :: results
    type(t_potden),    intent(inout) :: inDen
    integer,           intent(in)    :: archiveType
53
    LOGICAL,           INTENT(IN)    :: l_writehistory
54

Daniel Wortmann's avatar
Daniel Wortmann committed
55
    real                             :: fix
56
    type(t_potden)                   :: resDen, vYukawa
Daniel Wortmann's avatar
Daniel Wortmann committed
57 58
    TYPE(t_mixvector),ALLOCATABLE    :: sm(:), fsm(:)
    TYPE(t_mixvector)                :: fsm_mag
Daniel Wortmann's avatar
Daniel Wortmann committed
59
    LOGICAL                          :: l_densitymatrix
Daniel Wortmann's avatar
Daniel Wortmann committed
60
    INTEGER                          :: it,maxiter
61

62

Daniel Wortmann's avatar
Daniel Wortmann committed
63
    MPI0_a: IF( mpi%irank == 0 ) THEN
64 65 66 67 68 69 70 71 72 73 74 75
       !determine type of mixing:
       !imix=0:straight, imix=o broyden first, imix=5:broyden second
       !imix=:generalozed anderson mixing
       select case( input%imix )
       case( 0 )
          WRITE( 6, fmt='(a,2f10.5)' ) 'STRAIGHT MIXING',input%alpha
          IF (input%jspins.EQ.1) WRITE (6,FMT='(a,2f10.5)')&
               &    'charge density mixing parameter:',input%alpha
          IF (input%jspins.EQ.2) WRITE (6,FMT='(a,2f10.5)')&
               &    'spin density mixing parameter:',input%alpha*input%spinf

       case( 3 )
76
          write( 6, fmt='(a,f10.5)' ) 'BROYDEN FIRST MIXING',input%alpha
77
       case( 5 )
78
          write( 6, fmt='(a,f10.5)' ) 'BROYDEN SECOND MIXING',input%alpha
79
       case( 7 )
80
          write( 6, fmt='(a,f10.5)' ) 'ANDERSON GENERALIZED',input%alpha
81
       case default
82
          call juDFT_error( "mix: input%imix =/= 0,3,5,7 ", calledby ="mix" )
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
       end select

       if ( input%jspins == 2 .and. input%imix /= 0 ) then
          write( 6, '(''WARNING : for QUASI-NEWTON METHODS SPINF=1'')' )
       end if
    ENDIF MPI0_a

    l_densitymatrix=.FALSE.
    IF (atoms%n_u>0) THEN
       l_densitymatrix=.NOT.input%ldaulinmix
       IF (mpi%irank==0) CALL u_mix(input,atoms,inDen%mmpMat,outDen%mmpMat)
       IF (ALL(inDen%mmpMat==0.0)) THEN
          l_densitymatrix=.FALSE.
          inDen%mmpMat=outDen%mmpMat
          if (mpi%irank.ne.0) inden%mmpmat=0.0 
       ENDIF
    ENDIF
    CALL mixvector_init(mpi%mpi_comm,l_densitymatrix,oneD,input,vacuum,noco,sym,stars,cell,sphhar,atoms)

    CALL mixing_history_open(mpi,input%maxiter)

    maxiter=MERGE(1,input%maxiter,input%imix==0)
    CALL mixing_history(input%imix,maxiter,inden,outden,sm,fsm,it)

    CALL distance(mpi%irank,cell%vol,input%jspins,fsm(it),inDen,outDen,results,fsm_Mag)

109
    ! KERKER PRECONDITIONER
110 111 112 113 114 115 116
    IF( input%preconditioning_param /= 0 )  THEN 
       CALL kerker(field, DIMENSION, mpi, &
            stars, atoms, sphhar, vacuum, input, sym, cell, noco, &
            oneD, inDen, outDen, fsm(it) )
       !Store modified density in history
       CALL mixing_history_store(fsm(it))
    END IF
Daniel Wortmann's avatar
Daniel Wortmann committed
117
    !mixing of the densities
118 119
    if(input%imix==0.or.it==1) CALL stmix(atoms,input,noco,fsm(it),fsm_mag,sm(it))
    !if(it>1.and.input%imix==9) CALL pulay(input%alpha,fsm,sm)
Daniel Wortmann's avatar
Daniel Wortmann committed
120
    if(it>1.and.(input%imix==3.or.input%imix==5.or.input%imix==7)) Call broyden(input%alpha,fsm,sm)
Daniel Wortmann's avatar
Daniel Wortmann committed
121 122
    !PRINT *,"ATTENTION Broyden replaced by Pulay"
    IF (it>1.and.input%imix==9) CALL pulay(input%alpha,fsm,sm)
123

124 125 126 127 128
    !extracte mixed density 
    inDen%pw=0.0;inDen%mt=0.0
    IF (ALLOCATED(inDen%vacz)) inden%vacz=0.0
    IF (ALLOCATED(inDen%vacxy)) inden%vacxy=0.0
    IF (ALLOCATED(inDen%mmpMat).AND.l_densitymatrix) inden%mmpMat=0.0
129
    CALL sm(it)%to_density(inDen)
130 131 132 133 134 135 136 137
    IF (atoms%n_u>0.AND..NOT.l_densitymatrix.AND..NOT.input%ldaulinmix) THEN
       !No density matrix was present 
       !but is now created...
       inden%mmpMAT=outden%mmpMat
       CALL mixing_history_reset(mpi)
       CALL mixvector_reset()
    ENDIF

Daniel Wortmann's avatar
Daniel Wortmann committed
138
    !fix charge of the new density
139 140
    IF (mpi%irank==0) CALL qfix(mpi,stars,atoms,sym,vacuum, sphhar,input,cell,oneD,inDen,noco%l_noco,.FALSE.,.FALSE., fix)

Daniel Wortmann's avatar
Daniel Wortmann committed
141 142 143 144 145 146 147 148 149 150


    IF(vacuum%nvac.EQ.1) THEN
       inDen%vacz(:,2,:) = inDen%vacz(:,1,:)
       IF (sym%invs) THEN
          inDen%vacxy(:,:,2,:) = CONJG(inDen%vacxy(:,:,1,:))
       ELSE
          inDen%vacxy(:,:,2,:) = inDen%vacxy(:,:,1,:)
       END IF
    END IF
151 152


Daniel Wortmann's avatar
Daniel Wortmann committed
153
    !write out mixed density
154
    IF (mpi%irank==0) CALL writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,CDN_INPUT_DEN_const,&
Daniel Wortmann's avatar
Daniel Wortmann committed
155
         1,results%last_distance,results%ef,.TRUE.,inDen)
156

157
#ifdef CPP_HDF
158
    IF (mpi%irank==0.and.judft_was_argument("-last_extra")) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
159 160 161
       CALL system("rm cdn_last.hdf")
       CALL writeDensity(stars,vacuum,atoms,cell,sphhar,input,sym,oneD,archiveType,CDN_INPUT_DEN_const,&
            1,results%last_distance,results%ef,.TRUE.,inDen,'cdn_last')
162

Daniel Wortmann's avatar
Daniel Wortmann committed
163
    END IF
164 165
#endif

Daniel Wortmann's avatar
Daniel Wortmann committed
166
    inDen%iter = inDen%iter + 1
167

168
    IF (l_writehistory.AND.input%imix.NE.0) CALL mixing_history_close(mpi)
Daniel Wortmann's avatar
Daniel Wortmann committed
169

170
  END SUBROUTINE mix_charge
Daniel Wortmann's avatar
Daniel Wortmann committed
171 172
  
END MODULE m_mix