wann_gwf_commat2.f 11.5 KB
Newer Older
1 2 3 4 5 6
c*************************c
c  routine to set up the  c
c  composite matrices as  c
c  input for wannier90    c
c*************************c
      module m_wann_gwf_commat2
7
      USE m_juDFT
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 69 70 71 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
      implicit none
      contains

      subroutine wann_gwf_commat2(nkpts,nntot,bpt,nqpts,nntot_q,
     >                           bpt_q,gb,gb_q,latt_const_q,
     >                           l_unformatted,l_amn,l_mmn)
      use m_wann_gwf_tools
      use m_constants, only : pimach

      implicit none
      
      ! input parameters
      logical,intent(in) :: l_unformatted,l_amn,l_mmn
      integer,intent(in) :: nkpts,nntot,nqpts,nntot_q
      integer,intent(in) :: bpt(nntot,nkpts),bpt_q(nntot_q,nqpts)
      integer,intent(in) :: gb(3,nntot,nkpts),gb_q(3,nntot_q,nqpts)
      real,   intent(in) :: latt_const_q

      ! allocatable arrays
      integer,allocatable :: gb_kq(:,:,:)
      integer,allocatable :: bpt_kq(:,:)
      integer,allocatable :: g1(:,:,:),g2(:,:)
      integer,allocatable :: b1(:,:),b2(:)
      complex,allocatable :: tmp_a(:,:,:),tmp_m(:,:,:,:)
      complex,allocatable :: mmnk(:,:,:,:),mmnq(:,:,:,:)
      complex,allocatable :: mk(:,:,:),mq(:,:,:)
      complex,allocatable :: mmn(:,:,:,:)
      character(len=20),allocatable :: feig(:),famn(:)
      character(len=20),allocatable :: fmmn(:),fmmn2(:)

      ! further variables
      integer :: nbnd,nwfs,kb,qb,nn_kq,kqb
      integer :: i,j,ikqpt,ikqpt_b,nk,nq,g(3)
      integer :: nbnd_arti,nqpts_arti,nntot_arti
      integer :: nkqpts,nntot_kq,nn,ikqpt_help
      integer :: dump,nn_arti,nkp,k,b,q,nwf,n,is,ie
      real :: mmn_r,mmn_i,tau,tpi,a_arti,b_arti,eig
      complex :: mmn_arti,amn_arti,amn
      logical :: l_exist,l_miss,l_fe,l_fa,l_fm,l_fm2,l_proj
      character(len=20) :: fq

      integer :: ncpu

      ncpu = 8

      write(*,*)'create HDWFs input for w90...'

      nkqpts=nkpts*nqpts
      tpi = 2.0*pimach()
      a_arti = latt_const_q
      b_arti = 0.98*latt_const_q
      l_fe   = .true.
      l_fa   = .true.
      l_fm   = .true.
      l_fm2  = .true.
      l_exist= .true.

! compute auxiliary overlaps and projections
      tau = tpi/real(nqpts)/a_arti 
      mmn_arti = 2.0*tpi*tpi*sin(tau*b_arti/2.0)
      mmn_arti = mmn_arti/(tpi*tpi-tau*tau*b_arti*b_arti)
      mmn_arti = mmn_arti/(tau*b_arti)

      amn_arti = cmplx(1.0,0.0)

      open(777,file='ma_aux')
      write(777,'(2(f24.18))')real(mmn_arti),aimag(mmn_arti)
      write(777,'(2(f24.18))')real(amn_arti),aimag(amn_arti)
      close(777)

! are all necessary files present?
      allocate(feig(nqpts),famn(nqpts),fmmn(nqpts),fmmn2(nqpts))
      l_miss=.false.
      do q=1,nqpts
         write(fq,'(i4.4)')q
         feig(q) = 'WF1_'//trim(fq)//'.eig'    ! energies
         famn(q) = 'WF1_'//trim(fq)//'.amn'    ! projections
         fmmn(q) = 'WF1_'//trim(fq)//'.mmn'    ! k-overlaps
         fmmn2(q)= 'param_'//trim(fq)//'.mmn'  ! q-overlaps

         inquire(file=feig(q) ,exist=l_fe )
         if(l_amn) inquire(file=famn(q) ,exist=l_fa )
         if(l_mmn) inquire(file=fmmn(q) ,exist=l_fm )
         if(l_mmn) inquire(file=fmmn2(q),exist=l_fm2)

         if(.not.l_fe ) write(*,*)'missing: ',feig(q)
         if(.not.l_fa ) write(*,*)'missing: ',famn(q)
         if(.not.l_fm ) write(*,*)'missing: ',fmmn(q)
         if(.not.l_fm2) write(*,*)'missing: ',fmmn2(q)
         if(.not.(l_fe.and.l_fa.and.l_fm.and.l_fm2))l_miss=.true.
      enddo
      if(l_mmn) inquire(file='bkqpts',exist=l_exist)
      if(.not.l_exist) write(*,*)'missing: bkqpts'
      inquire(file='proj',exist=l_proj)
      if(.not.l_proj) write(*,*)'missing: proj'
      if(l_miss.or.(.not.l_exist).or.(.not.l_proj))
104
     >   call juDFT_error("missing file(s) for HDWFs")
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

! get number of bands and wfs from proj
      open(405,file='proj',status='old')
      read(405,*)nwfs,nbnd
      close(405)
      write(*,*)'nbnd=',nbnd
      write(*,*)'nwfs=',nwfs


c*****************************c
c     COMPOSITE .EIG FILE     c
c*****************************c
      open(305,file='WF1_gwf.eig')
      do q=1,nqpts
         open(405,file=feig(q))
         do k=1,nkpts
            ikqpt=get_index_kq(k,q,nkpts)
            do i=1,nbnd
               read(405,*)b,nk,eig
               write(305,'(2i12,f19.13)')b,ikqpt,eig
            enddo
         enddo
         close(405)
      enddo
      close(305)


c*****************************c
c     COMPOSITE .AMN FILE     c
c*****************************c
      if(.not.l_amn) goto 100 ! skip amn part

      if(l_unformatted) then
         allocate(tmp_a(nbnd,nwfs,nkqpts))
         do q=1,nqpts
            is = get_index_kq(1,q,nkpts)
            ie = get_index_kq(nkpts,q,nkpts)
            open(405,file=famn(q),form='unformatted')
            read(405)b,k,nwf
            read(405)tmp_a(:,:,is:ie)
            close(405)
         enddo

         open(306,file='WF1_gwf.amn',form='unformatted')
         write(306)nbnd,nkqpts,nwfs
         write(306)tmp_a*amn_arti
         close(306)
         deallocate(tmp_a)
      else
         open(306,file='WF1_gwf.amn')
         write(306,*)'Projections for HDWFs'
         write(306,'(i5,i7,i5)')nbnd,nkqpts,nwfs
         
         do q=1,nqpts
           open(405,file=famn(q))
           read(405,*)!title
           read(405,*)b,k,nwf
           do k=1,nkpts
            ikqpt=get_index_kq(k,q,nkpts)
            do nwf=1,nwfs
             do i=1,nbnd
              read(405,*)b,n,nk,mmn_r,mmn_i
              amn = cmplx(mmn_r,mmn_i)
              amn = amn*amn_arti
              write(306,'(i5,i5,i7,3x,2f18.12)')
     >                     b,n,ikqpt,real(amn),aimag(amn)
             enddo
            enddo
           enddo
         enddo
         close(306)
      endif!l_unformatted

 100  continue


c*****************************c
c     COMPOSITE .MMN FILE     c
c*****************************c
      if(.not.l_mmn) goto 200 ! skip mmn part

      open (202,file='bkqpts',form='formatted',status='old')
      rewind (202)
      read (202,'(i4)') nntot_kq
      write (*,*) 'nntot_kq=',nntot_kq
      allocate ( gb_kq(4,nntot_kq,nkqpts),
     &           bpt_kq(nntot_kq,nkqpts))
      do ikqpt=1,nkqpts
       do nn=1,nntot_kq
         read (202,'(2i6,3x,4i4)')
     &    ikqpt_help,bpt_kq(nn,ikqpt),(gb_kq(i,nn,ikqpt),i=1,4)
       enddo
      enddo
      close (202)

      if(l_unformatted) then
         allocate(mmnk(nbnd,nbnd,nntot,nkpts))
         allocate(mmnq(nbnd,nbnd,nntot_q,nkpts))
         allocate(g1(3,nntot,nkpts),g2(3,nntot_q))
         allocate(b1(nntot,nkpts),b2(nntot_q))
         allocate(mmn(nbnd,nbnd,nntot_kq,nkqpts))

         do q=1,nqpts
           is = get_index_kq(1,q,nkpts)
           ie = get_index_kq(nkpts,q,nkpts)
         
           open(405,file=fmmn(q),form='unformatted')
           read(405)b,k,nn
           read(405)b1,g1
           read(405)mmnk
           close(405)

           open(405,file=fmmn2(q),form='unformatted')
           read(405)b,k,nn
           read(405)b2,g2
           read(405)mmnq
           close(405)
           mmnq = mmnq*conjg(mmn_arti)

           do k=1,nkpts
             ikqpt=get_index_kq(k,q,nkpts)           
             do nn=1,nntot_kq
               kqb = bpt_kq(nn,ikqpt)
               kb=get_index_k(kqb,nkpts)
               qb=get_index_q(kqb,nkpts)
               if(q.eq.qb) then ! k-overlap
                  nk=get_index_nn_k(bpt(:,k),nntot,kb,
     >                         gb_kq(:,nn,ikqpt),
     >                         gb(1:3,1:nntot,k))
                  mmn(:,:,nn,ikqpt)=mmnk(:,:,nk,k)              
               elseif(k.eq.kb) then ! q-overlap
                  nq=get_index_nn_q(bpt_q(:,q),nntot_q,qb,
     >                         gb_kq(:,nn,ikqpt),
     >                         gb_q(1:3,1:nntot_q,q))
                  mmn(:,:,nn,ikqpt)=mmnq(:,:,nq,k)
               else ! otherwise problem
241
                  call juDFT_error("problem in overlap mmn gwf",
242 243 244 245 246 247 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
     >                        calledby="wann_gwf_commat")
               endif
             enddo!nn
           enddo!k
         enddo!q
         deallocate(b1,b2,g1,g2,mmnk,mmnq)

         open(307,file='WF1_gwf.mmn',form='unformatted')
         write(307)nbnd,nkqpts,nntot_kq
         write(307)bpt_kq,gb_kq
         write(307)mmn
         close(307)
         deallocate(mmn)
      else
         allocate(mk(nbnd,nbnd,nntot))
         allocate(mq(nbnd,nbnd,nntot_q))
         open(307,file='WF1_gwf.mmn')
         write(307,*)'Overlaps for HDWFs'
         write(307,'(i5,i7,i5)')nbnd,nkqpts,nntot_kq

         do q=1,nqpts       
           open(405,file=fmmn(q))
           read(405,*)!title
           read(405,*)b,k,nn
           open(406,file=fmmn2(q))
           read(406,*)!title
           read(406,*)b,k,nn
           do k=1,nkpts
             ikqpt=get_index_kq(k,q,nkpts)           

             do nn=1,nntot ! read k-overlaps
               kb = bpt(nn,k)
               read(405,*)nkp,b,g(1:3)
               do i=1,nbnd
                  do j=1,nbnd
                     read(405,*)mmn_r,mmn_i
                     mk(j,i,nn)=cmplx(mmn_r,mmn_i)
                  enddo
               enddo               
             enddo  

             do nn=1,nntot_q ! read q-overlaps
               qb = bpt_q(nn,q)
               read(406,*)nkp,b,g(1:3)
               do i=1,nbnd
                  do j=1,nbnd
                     read(406,*)mmn_r,mmn_i
                     mq(j,i,nn)=cmplx(mmn_r,mmn_i)
                  enddo
               enddo                              
             enddo
             mq = mq*conjg(mmn_arti)   

             do nn=1,nntot_kq ! write composite overlaps
               kqb = bpt_kq(nn,ikqpt)
               kb=get_index_k(kqb,nkpts)
               qb=get_index_q(kqb,nkpts)
               write(307,'(i7,i7,3x,4i4)')ikqpt,kqb,gb_kq(1:4,nn,ikqpt)
               if(q.eq.qb) then ! k-overlap
                  nk=get_index_nn_k(bpt(:,k),nntot,kb,
     >                         gb_kq(:,nn,ikqpt),
     >                         gb(1:3,1:nntot,k))
                  do i=1,nbnd
                     do j=1,nbnd
                        write(307,'(2f24.18)')
     >                      real(mk(j,i,nk)),aimag(mk(j,i,nk))
                     enddo
                  enddo
               elseif(k.eq.kb) then ! q-overlap
                  nq=get_index_nn_q(bpt_q(:,q),nntot_q,qb,
     >                         gb_kq(:,nn,ikqpt),
     >                         gb_q(1:3,1:nntot_q,q))
                  do i=1,nbnd
                     do j=1,nbnd
                        write(307,'(2f24.18)')
     >                      real(mq(j,i,nq)),aimag(mq(j,i,nq))
                     enddo
                  enddo
               else ! otherwise problem
321
                  call juDFT_error("problem in overlap mmn gwf",
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 351 352 353 354 355 356 357 358 359 360
     >                        calledby="wann_gwf_commat")
               endif
             enddo!nn
           enddo!k
           close(406)
           close(405)
         enddo!q           
         close(307)
         deallocate(mk,mq)
      endif!l_unformatted

      deallocate(gb_kq,bpt_kq)
 200  continue
      deallocate(feig,famn,fmmn,fmmn2)

      end subroutine wann_gwf_commat2  


      subroutine split_array(numpoints,n,ncpu,counts,displs)
      implicit none
      integer, intent(in) :: numpoints,ncpu,n
      integer, intent(out) :: counts,displs

      integer :: ratio, remainder, i

      ratio = numpoints / ncpu
      remainder = mod(numpoints,ncpu)

      if(n < remainder) then
         counts = ratio+1
         displs = i*(ratio+1)
      else
         counts = ratio
         displs = remainder*(ratio+1)+(i-remainder)*ratio
      endif   
      
      end subroutine split_array

      end module m_wann_gwf_commat2