mix.F90 6.33 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 31
    USE m_kerker
    use m_types_mixvector
Daniel Wortmann's avatar
Daniel Wortmann committed
32 33
    USE m_distance
    use m_mixing_history
34 35 36 37 38 39 40
    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
41 42 43
    TYPE(t_stars),TARGET,INTENT(in)  :: stars
    TYPE(t_cell),TARGET,INTENT(in)   :: cell
    TYPE(t_sphhar),TARGET,INTENT(in) :: sphhar
44 45 46
    type(t_field),     intent(inout) :: field
    type(t_dimension), intent(in)    :: dimension
    type(t_mpi),       intent(in)    :: mpi
47
    TYPE(t_atoms),TARGET,INTENT(in)  :: atoms 
48 49 50 51
    type(t_potden),    intent(inout) :: outDen
    type(t_results),   intent(inout) :: results
    type(t_potden),    intent(inout) :: inDen
    integer,           intent(in)    :: archiveType
52
    LOGICAL,           INTENT(IN)    :: l_writehistory
53

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

61

Daniel Wortmann's avatar
Daniel Wortmann committed
62
    MPI0_a: IF( mpi%irank == 0 ) THEN
63 64 65 66 67 68 69 70 71 72 73 74
       !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 )
75
          write( 6, fmt='(a,f10.5)' ) 'BROYDEN FIRST MIXING',input%alpha
76
       case( 5 )
77
          write( 6, fmt='(a,f10.5)' ) 'BROYDEN SECOND MIXING',input%alpha
78
       case( 7 )
79
          write( 6, fmt='(a,f10.5)' ) 'ANDERSON GENERALIZED',input%alpha
80
       case default
81
          call juDFT_error( "mix: input%imix =/= 0,3,5,7 ", calledby ="mix" )
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
       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)

108
    ! KERKER PRECONDITIONER
109 110 111 112 113 114 115
    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
116
    !mixing of the densities
117 118
    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
119
    if(it>1.and.(input%imix==3.or.input%imix==5.or.input%imix==7)) Call broyden(input%alpha,fsm,sm)
120

121 122 123 124 125
    !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
126
    CALL sm(it)%to_density(inDen)
127 128 129 130 131 132 133 134
    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
135
    !fix charge of the new density
136 137
    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
138 139 140 141 142 143 144 145 146 147


    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
148 149


Daniel Wortmann's avatar
Daniel Wortmann committed
150
    !write out mixed density
151
    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
152
         1,results%last_distance,results%ef,.TRUE.,inDen)
153

154
#ifdef CPP_HDF
155
    IF (mpi%irank==0.and.judft_was_argument("-last_extra")) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
156 157 158
       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')
159

Daniel Wortmann's avatar
Daniel Wortmann committed
160
    END IF
161 162
#endif

Daniel Wortmann's avatar
Daniel Wortmann committed
163
    inDen%iter = inDen%iter + 1
164

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

167
  END SUBROUTINE mix_charge
Daniel Wortmann's avatar
Daniel Wortmann committed
168 169
  
END MODULE m_mix