hybrid.F90 5.56 KB
Newer Older
Daniel Wortmann's avatar
Daniel Wortmann committed
1 2 3 4 5 6 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
MODULE m_calc_hybrid
  USE m_judft
CONTAINS
  SUBROUTINE calc_hybrid(hybrid,kpts,atoms,input,DIMENSION,mpi,noco,cell,vacuum,oneD,banddos,results,sym,xcpot,v,it  )
    USE m_types
    USE m_mixedbasis
    USE m_coulombmatrix
    USE m_hf_init
    USE m_hf_setup
    USE m_hsfock
    USE m_eig66_io
    USE m_apws
    USE m_io_hybrid
    IMPLICIT NONE
    TYPE(t_xcpot),INTENT(IN)     :: xcpot
    TYPE(t_mpi),INTENT(IN)       :: mpi
    TYPE(t_dimension),INTENT(IN) :: DIMENSION
    TYPE(t_oneD),INTENT(IN)      :: oneD
    TYPE(t_hybrid),INTENT(INOUT) :: hybrid
    TYPE(t_input),INTENT(IN)     :: input
    TYPE(t_vacuum),INTENT(IN)    :: vacuum
    TYPE(t_noco),INTENT(IN)      :: noco
    TYPE(t_banddos),INTENT(IN)   :: banddos
    TYPE(t_results),INTENT(INOUT):: results
    TYPE(t_sym),INTENT(IN)       :: sym  
    TYPE(t_cell),INTENT(IN)      :: cell
    TYPE(t_kpts),INTENT(IN)      :: kpts
    TYPE(t_atoms),INTENT(IN)     :: atoms!in u_setup n_u might be modified
    TYPE(t_potden),INTENT(IN)    :: v

    INTEGER,INTENT(IN) :: it

    !Local variables
    INTEGER        :: eig_id,jsp,nk,nred
    TYPE(t_hybdat) :: hybdat
    type(t_lapw)   :: lapw
    LOGICAL        :: init_vex=.TRUE. !In first call we have to init v_nonlocal
38
    INTEGER,SAVE   :: nohf_it=99      !Do not rely on a converged density
Daniel Wortmann's avatar
Daniel Wortmann committed
39 40 41 42 43
    INTEGER        ::  comm(kpts%nkpt),irank2(kpts%nkpt),isize2(kpts%nkpt)
    LOGICAL        :: l_restart=.FALSE.,l_zref
    INTEGER, ALLOCATABLE :: matind(:,:)
    REAL,    ALLOCATABLE    ::  eig_irr(:,:)
    real               :: bkpt(3)
44 45

    INQUIRE(file="v_x.mat",exist=hybrid%l_addhf)
46
    CALL open_hybrid_io1(DIMENSION,sym%invs)
Daniel Wortmann's avatar
Daniel Wortmann committed
47 48 49 50
    IF (kpts%nkptf==0) CALL judft_error("kpoint-set of full BZ not available",hint="to generate kpts in the full BZ you should specify a k-mesh in inp.xml")
    
    !Check if new non-local potential shall be generated

51
    hybrid%l_subvxc = ( hybrid%l_hybrid.AND..not.xcpot%is_name("exx") )
52 53
    !If this is the first iteration loop we can not calculate a new
    !non-local potential
Daniel Wortmann's avatar
Daniel Wortmann committed
54 55
    IF (.NOT.ALLOCATED(v%pw)) THEN
       hybrid%l_calhf=.FALSE.
56
       if (hybrid%l_addhf) INQUIRE(file="cdnc",exist=hybrid%l_addhf)
Daniel Wortmann's avatar
Daniel Wortmann committed
57 58 59
       hybrid%l_subvxc = ( hybrid%l_subvxc .AND. hybrid%l_addhf)
       RETURN
    ENDIF
60
    !Check if we are converged well enough to calculate a new potential
61
#if defined(CPP_MPI)&&defined(CPP_NEVER)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    CALL judft_error("Hybrid functionals do not work in parallel version yet")
    CALL MPI_BCAST(results%last_distance .... 
#endif
    hybrid%l_calhf=results%last_distance<1E-5
    IF (nohf_it>50) THEN
       hybrid%l_calhf=.TRUE.
       nohf_it=0
    ELSE
       IF (hybrid%l_calhf) THEN
          nohf_it=0
       ELSE
          nohf_it=nohf_it+1
       END IF
    ENDIF
    
Daniel Wortmann's avatar
Daniel Wortmann committed
77
    hybrid%l_addhf=.true.
78 79 80 81
    !In first iteration allocate some memory
    IF (init_vex) THEN
       ALLOCATE(hybrid%ne_eig(kpts%nkpt),hybrid%nbands(kpts%nkpt),hybrid%nobd(kpts%nkptf))
       ALLOCATE( hybrid%nbasm(kpts%nkptf))
82
       ALLOCATE(hybrid%div_vv(DIMENSION%neigd,kpts%nkpt,input%jspins))
83 84 85 86
       init_vex=.false.
    ENDIF
    
    
87
    hybrid%l_subvxc = ( hybrid%l_subvxc .AND. hybrid%l_addhf)
Daniel Wortmann's avatar
Daniel Wortmann committed
88 89 90 91 92
    IF( .NOT. ALLOCATED(results%w_iks) )&
         ALLOCATE ( results%w_iks(DIMENSION%neigd2,kpts%nkpt,DIMENSION%jspd) )
    
    IF (.NOT.hybrid%l_calhf) RETURN !use existing non-local potential

93 94 95
    !Delete broyd files
    CALL system("rm broyd*")
    
Daniel Wortmann's avatar
Daniel Wortmann committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    !check if z-reflection trick can be used

    l_zref=(sym%zrfs.AND.(SUM(ABS(kpts%bk(3,:kpts%nkpt))).LT.1e-9).AND..NOT.noco%l_noco) 
    
    ALLOCATE (  matind(DIMENSION%nbasfcn,2) )

    
    CALL timestart("generation of non-local HF potential")
    CALL timestart("Preparation for Hybrid functionals")
    CALL juDFT_WARN ("Hybrid functionals not working in this version")      
      
    eig_id=open_eig(&
         mpi%mpi_comm,dimension%nbasfcn,dimension%neigd,kpts%nkpt,dimension%jspd,atoms%lmaxd,atoms%nlod,atoms%ntype,atoms%nlotot&
         ,noco%l_noco,.FALSE.,sym%invs.AND..NOT.noco%l_noco,noco%l_soc,.FALSE.,&
         mpi%n_size,layers=vacuum%layers,nstars=vacuum%nstars,ncored=DIMENSION%nstd,&
         nsld=atoms%nat,nat=atoms%nat,l_dos=banddos%dos.OR.input%cdinf,l_mcd=banddos%l_mcd,&
         l_orb=banddos%l_orb)
    
    !construct the mixed-basis
    CALL timestart("generation of mixed basis")
    CALL mixedbasis(atoms,kpts, dimension,input,cell,sym,xcpot,hybrid, eig_id,mpi,v,l_restart)
    CALL timestop("generation of mixed basis")

         
120
    CALL open_hybrid_io2(hybrid,DIMENSION,atoms,sym%invs)
Daniel Wortmann's avatar
Daniel Wortmann committed
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

    CALL timestart("generation of coulomb matrix")
    CALL coulombmatrix(mpi,atoms,kpts,cell,sym,hybrid,xcpot,l_restart)
    CALL timestop("generation of coulomb matrix")
    
    CALL hf_init(hybrid,kpts,atoms,input,DIMENSION,hybdat,irank2,isize2,sym%invs)
    CALL timestop("Preparation for Hybrid functionals")
    
    CALL timestart("Calculation of non-local potential")
    DO jsp = 1,input%jspins
       CALL HF_setup(hybrid,input,sym,kpts,dimension,atoms,mpi,noco,cell,oneD,results,jsp,eig_id,&
            hybdat,irank2,it,sym%invs,v%mt(:,0,:,:),eig_irr)  
       DO nk = mpi%n_start,kpts%nkpt,mpi%n_stride

          CALL apws(DIMENSION,input,noco, kpts,nk,cell,l_zref, mpi%n_size,jsp, bkpt,lapw,matind,nred)
  
          CALL hsfock(nk,atoms,hybrid,lapw,DIMENSION,kpts,jsp,input,hybdat,eig_irr,&
               sym,cell,noco,results,it,MAXVAL(hybrid%nobd),xcpot,&
               mpi,irank2(nk),isize2(nk),comm(nk))
       END DO
    END DO
    CALL timestop("Calculation of non-local potential")
    CALL timestop("generation of non-local HF potential")
    CALL close_eig(eig_id)
  end subroutine calc_hybrid
END MODULE m_calc_hybrid