wann_uHu_dmi.F 53.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
c*******************************************c
c   Set up uHu matrix necessary for         c
c   Wannier-based calc. of DMI              c
c*******************************************c
c   keyword is 'matrixuhu-dmi' in wann_inp  c
c*******************************************c
c    < u_{k+b1} | H_{k} | u_{\theta+b2} >   c
c                                           c
c   Contributions to Hamiltonian:           c
c       (i)   interstitial                  c
c       (ii)  muffin tin  (a) spherical     c
c                         (b) non-sph.      c
c                         (c) SOC           c
c       (iii) vacuum                        c
c*******************************************c
c                  J.-P. Hanke, Feb. 2016   c
c*******************************************c
      MODULE m_wann_uHu_dmi
19
      USE m_juDFT
20 21
      CONTAINS
      SUBROUTINE wann_uHu_dmi(
22 23
     >      DIMENSION,stars,vacuum,atoms,sphhar,input,kpts,sym,mpi,
     >      banddos,oneD,noco,cell,vTot,wann,
24
     >      eig_idList,l_real,l_dulo,l_noco,l_ss,lmaxd,ntypd,
25
     >      neigd,natd,nop,nvd,jspd,nbasfcn,llod,nlod,ntype,
26
     >      omtil,nlo,llo,lapw_l,invtab,mrot,ngopr,neq,lmax,
27 28 29 30 31 32
     >      invsat,invsatnr,nkpt,taual,rmt,amat,bmat,bbmat,alph,
     >      beta,qss,sk2,phi2,odi,ods,irank,isize,n3d,nmzxyd,nmzd,
     >      jmtd,nlhd,nq3,nvac,invs,invs2,film,nlh,jri,ntypsd,
     >      ntypsy,jspins,nkptd,dx,n2d,rmsh,e1s,e2s,ulo_der,
     >      ustep,ig,k1d,k2d,k3d,rgphs,slice,kk,nnne,
     >      z1,nv2d,nmzxy,nmz,delz,zrfs,ig2,area,tau,zatom,nq2,kv2,nop2,
33
     >      volint,symor,pos,ef,l_soc,
34
     >      memd,lnonsph,clnu,lmplmd,mlh,nmem,llh,lo1l,
35
     >      theta,phi,
36 37 38 39 40 41 42 43 44
     >      l_ms,l_sgwf,l_socgwf,aux_latt_const,
     >      param_file,param_vec,nparampts,param_alpha,l_dim,l_nochi)

      use m_types
      use m_wann_mmnk_symm
      use m_wann_rw_eig
      use m_abcof
      use m_radfun
      use m_radflo
45 46
      use m_cdnread
      use m_types
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      use m_constants, only : pimach
      use m_wann_projmethod
      use m_wann_abinv
      use m_wann_kptsrotate
      use m_wann_read_inp
      use m_matmul,only : matmul3,matmul3r
      use m_wann_maxbnd
      use m_wann_uHu_tlmplm2
      use m_wann_uHu_sph
      use m_wann_uHu_int
      use m_wann_uHu_soc
      use m_wann_uHu_vac
      use m_wann_uHu_od_vac
      use m_wann_uHu_util
      use m_wann_uHu_commat
      use m_wann_write_uHu
63
      USE m_eig66_io
64 65 66 67 68 69 70 71 72

      IMPLICIT NONE
#include "cpp_double.h"
#ifdef CPP_MPI
      include 'mpif.h'
      integer ierr(3)
      integer cpu_index
      integer stt(MPI_STATUS_SIZE)
#endif
73 74 75 76 77 78 79

      TYPE(t_dimension),INTENT(IN) :: DIMENSION
      TYPE(t_stars),INTENT(IN)     :: stars
      TYPE(t_vacuum),INTENT(IN)    :: vacuum
      TYPE(t_atoms),INTENT(IN)     :: atoms
      TYPE(t_sphhar),INTENT(IN)    :: sphhar
      TYPE(t_input),INTENT(IN)     :: input
80
      TYPE(t_kpts),INTENT(IN)      :: kpts
81 82 83 84 85 86
      TYPE(t_sym),INTENT(IN)       :: sym
      TYPE(t_mpi),INTENT(IN)       :: mpi
      TYPE(t_banddos),INTENT(IN)   :: banddos
      TYPE(t_oneD),INTENT(IN)      :: oneD
      TYPE(t_noco),INTENT(IN)      :: noco
      TYPE(t_cell),INTENT(IN)      :: cell
87
      TYPE(t_potden),INTENT(IN)    :: vTot
88
      TYPE(t_wann),INTENT(INOUT)   :: wann
89

90 91 92 93
c     ..scalar arguments..
      character(len=20),intent(in) :: param_file
      type (od_inp), intent (in) :: odi
      type (od_sym), intent (in) :: ods
94
      INTEGER, INTENT (IN) :: eig_idList(wann%nparampts)
95
      logical, intent (in) :: invs,invs2,film,slice,symor,zrfs
96
      logical, intent (in) :: l_real,l_noco,l_ss,l_soc,l_nochi
97 98 99
      logical, intent (in) :: l_ms,l_sgwf,l_socgwf
      integer, intent (in) :: lmaxd,ntypd,neigd,nkptd,kk,nnne
      integer, intent (in) :: natd,nop,nvd,jspd,nbasfcn,nq2,nop2
100
      integer, intent (in) :: llod,nlod,ntype,n3d,n2d
101 102 103
      integer, intent (in) :: nmzxyd,nmzd,jmtd,nlhd,nq3,nvac
      integer, intent (in) :: ntypsd,jspins,k1d,k2d,k3d
      integer, intent (in) :: irank,isize,nv2d,nmzxy,nmz
104
      integer, intent (in) :: memd,lmplmd,nparampts
105
      real,    intent (in) :: omtil,e1s,e2s,delz,area,z1,volint
106
      real,    intent (in) :: ef,theta,phi,aux_latt_const
107 108 109

c     ..array arguments..
      logical, intent (in) :: l_dulo(nlod,ntypd)
110
      logical, intent (in) :: l_dim(3)
111 112 113 114 115
      integer, intent (in) :: ig(-k1d:k1d,-k2d:k2d,-k3d:k3d)
      integer, intent (in) :: nlh(ntypsd),jri(ntypd),ntypsy(natd)
      integer, intent (in) :: nlo(ntypd),llo(nlod,ntypd),lapw_l(ntypd)
      integer, intent (in) :: invtab(nop),mrot(3,3,nop),ngopr(natd)
      integer, intent (in) :: neq(ntypd),lmax(ntypd)
116
      integer, intent (in) :: invsat(natd),invsatnr(natd),nkpt
117 118 119 120 121
      integer, intent (in) :: ulo_der(nlod,ntypd),ig2(n3d),kv2(2,n2d)
      integer, intent (in) :: mlh(memd,0:nlhd,ntypsd)
      integer, intent (in) :: nmem(0:nlhd,ntypsd)
      integer, intent (in) :: llh(0:nlhd,ntypsd),lnonsph(ntypd)
      integer, intent (in) :: lo1l(0:llod,ntypd)
122
      complex, intent (in) :: rgphs(-k1d:k1d,-k2d:k2d,-k3d:k3d)
123 124 125
      real,    intent (in) :: taual(3,natd),rmt(ntypd),dx(ntypd)
      real,    intent (in) :: amat(3,3),bmat(3,3),bbmat(3,3)
      real,    intent (in) :: rmsh(jmtd,ntypd),tau(3,nop),zatom(ntype)
126
      real,    intent (in) :: alph(ntypd),beta(ntypd),qss(3)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      real,    intent (in) :: pos(3,natd),sk2(n2d),phi2(n2d)
      real,    intent (in) :: param_vec(3,nparampts)
      real,    intent (in) :: param_alpha(ntypd,nparampts)
      complex, intent (in) :: ustep(n3d)
      complex, intent (in) :: clnu(memd,0:nlhd,ntypsd)

c     ..allocatable arrays..
      integer, allocatable :: kveclo(:)   , nv(:)
      integer, allocatable :: kveclo_b(:) , nv_b(:)
      integer, allocatable :: kveclo_b2(:), nv_b2(:)
      integer, allocatable :: k1(:,:)   , k2(:,:)   , k3(:,:)
      integer, allocatable :: k1_b(:,:) , k2_b(:,:) , k3_b(:,:)
      integer, allocatable :: k1_b2(:,:), k2_b2(:,:), k3_b2(:,:)
      integer, allocatable :: irreduc(:),mapkoper(:)
      integer, allocatable :: irreduc_q(:),mapqoper(:)        
      integer, allocatable :: shiftkpt(:,:),pair_to_do(:,:)
      integer, allocatable :: shiftqpt(:,:),pair_to_do_q(:,:)  
      integer, allocatable :: maptopair(:,:,:)
      integer, allocatable :: maptopair_q(:,:,:)              
      integer, allocatable :: counts(:),displs(:)
      integer, allocatable :: gb(:,:,:),bpt(:,:)
148
      integer, allocatable :: gb_q(:,:,:),bpt_q(:,:)
149
      INTEGER, ALLOCATABLE :: innerEig_idList(:)
150 151
      real,    allocatable :: we(:),we_b(:),we_b2(:)
      real,    allocatable :: eigg(:)
152
      real,    allocatable :: vr(:,:,:),vz(:,:,:)
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
      real,    allocatable :: flo(:,:,:,:,:)
      real,    allocatable :: ff(:,:,:,:,:),gg(:,:,:,:,:)
      real,    allocatable :: us(:,:,:),uds(:,:,:),ulos(:,:,:)
      real,    allocatable :: dus(:,:,:),duds(:,:,:),dulos(:,:,:)
      real,    allocatable :: ddn(:,:,:),uulon(:,:,:),dulon(:,:,:)
      real,    allocatable :: uloulopn(:,:,:,:)
      real,    allocatable :: kdiff(:,:),qdiff(:,:),zero_qdiff(:,:)
      real,    allocatable :: kdiff2(:,:)
      complex, allocatable :: vpw(:,:),vzxy(:,:,:,:)
      complex, allocatable :: uHu(:,:,:,:,:)
      complex, allocatable :: acof_b(:,:,:),acof_b2(:,:,:)
      complex, allocatable :: bcof_b(:,:,:),bcof_b2(:,:,:)
      complex, allocatable :: ccof_b(:,:,:,:),ccof_b2(:,:,:,:)
      complex, allocatable :: tdd(:,:,:,:,:),tdu(:,:,:,:,:)
      complex, allocatable :: tud(:,:,:,:,:),tuu(:,:,:,:,:)
      complex, allocatable :: tdd_soc(:,:,:,:,:),tdu_soc(:,:,:,:,:)
      complex, allocatable :: tud_soc(:,:,:,:,:),tuu_soc(:,:,:,:,:)
      complex, allocatable :: tdulo(:,:,:,:,:,:),tuulo(:,:,:,:,:,:)
      complex, allocatable :: tulod(:,:,:,:,:,:),tulou(:,:,:,:,:,:)
      complex, allocatable :: tuloulo(:,:,:,:,:,:,:)
      complex, allocatable :: tdulo_soc(:,:,:,:,:,:),
     >                        tuulo_soc(:,:,:,:,:,:)
      complex, allocatable :: tulod_soc(:,:,:,:,:,:),
     >                        tulou_soc(:,:,:,:,:,:)
      complex, allocatable :: tuloulo_soc(:,:,:,:,:,:,:)

c     ..local arrays..
      character(len=2) :: spin012(0:2)
      data spin012/'  ', '.1', '.2'/
      character(len=3) :: spin12(2)
      data   spin12/'WF1' , 'WF2'/
      character(len=8) :: name(10)
      integer :: n_bands(0:neigd),ngopr1(natd)
      real    :: bkpt(3),bkpt_b(3),bkpt_b2(3),bkrot(3)
      real    :: eig(neigd),eig_b(neigd),eig_b2(neigd)
      real    :: uuilon(nlod,ntypd),duilon(nlod,ntypd)
      real    :: ulouilopn(nlod,nlod,ntypd)
      real    :: ello(nlod,ntypd,max(2,jspd)),evac(2,max(2,jspd))
      real    :: epar(0:lmaxd,ntypd,max(2,jspd)),evdu(2,max(jspd,2))
      real    :: qpt_i(3),qptb_i(3)
      real    :: alph_i(ntypd),alphb_i(ntypd)
      real    :: beta_i(ntypd),betab_i(ntypd)
      real    :: cp_time(9)

c     ..local scalars..
      character(len=6) :: filename
      character(len=8) :: dop,iop
      character(len=12) fending
      character(len=30) fstart
      logical :: l_p0,l_bkpts,l_proj,l_file
      logical :: l_bqpts,l_gwf,l_exist
      logical :: l_skip_sph,l_skip_non,l_skip_soc
      logical :: l_skip_int,l_skip_vac,l_skip_loc
      integer :: lmd,nlotot,n,iter,ikpt,ikpt_b,ikpt_b2,iqpt,iqpt_b
      integer :: addnoco,addnoco2,funbas,loplod,igvm2
      integer :: nn,nkpts,i,j,l,i_rec,m,nwf,nwfp
      integer :: jsp_start,jsp_end,nrec,nrec_b,nrec1
210
      integer :: nodeu,noded,n_size,na,n_rank,nbnd,numbands,eig_id
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      integer :: i1,i2,i3,in,lda
      integer :: nmat,nmat_b,nmat_b2,nmat_qb
      integer :: nbands,nbands_b,nbands_b2,nbands_qb
      integer :: nslibd,nslibd_b,nslibd_b2,nslibd_qb
      integer :: noccbd,noccbd_b,noccbd_b2,noccbd_qb
      integer :: kptibz,kptibz_b,kptibz_b2
      integer :: qptibz, qptibz_b
      integer :: oper,oper_b,oper_b2,oper_q, oper_qb
      integer :: nwfs,nntot,nntot2,nntot_q,fullnkpts,fullnqpts
      integer :: kpt,qpt,j1,j2,j3,k,ikpt_help,iqpt_help
      integer :: wannierspin,jspin,jspin_b,jspin2
      integer :: jspin3,jspin4,jspin5,tspin,tspin2
      integer :: n_start,n_end,mlotot,mlolotot,err
      integer :: mlot_d,mlolot_d,ilo,dir,length
      integer :: npotmatfile,ig3,maxvac,irec,imz,ivac,ipot
      integer :: funit_start,band_help,sign2
      integer :: doublespin,doublespin_max,doublespin_start,nrec5
      integer :: aoff,d1,d10,d100
      real    :: tpi,wronk,wk,wk_b,wk_b2
230
      real    :: t0,t00,t1,t_myTlmplm,t_init
231 232 233 234 235 236
      real    :: t_int,t_sph,t_vac,t_abcof,t_eig,t_total
      real    :: efermi,htr2ev
      real    :: theta_i, thetab_i, phi_i, phib_i,dth,dph
      complex :: nsfactor,nsfactor_b,nsfactor_b2
      complex :: ci,chi,chi2

237 238 239 240
      TYPE(t_usdus) :: usdus
      TYPE(t_zmat)  :: zMat, zzMat, zMat_b, zMat_b2
      TYPE(t_lapw)  :: lapw_b, lapw_b2

241 242 243 244 245 246 247 248 249 250 251
      if(.not.l_socgwf) stop 'wann_uHu_dmi only with l_socgwf=T'
      if(l_sgwf) stop 'wann_uHu_dmi only with l_sgwf=F'
      if(l_noco) stop 'noco and dmi not implemented'

      if(irank.eq.0) write(*,*)'Wannier-based DMI interpolation'
c      stop 'temporary_stop'

c     ..initializations..
      call cpu_time(t00)

      ci = cmplx(0.,1.)
252
      t_init = 0.; t_myTlmplm = 0.; t_eig = 0.
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
      t_abcof = 0.; t_int = 0.; t_sph = 0.
      t_vac = 0.; t_total = 0.
      htr2ev = 27.2
      nntot_q = 1
      fullnqpts = 1
      funit_start = 5000

      aoff = iachar('1')-1
      d1  = mod(irank,10)
      IF (irank < 100) THEN
        d10 = int( (irank + 0.5)/10 )
        fstart = 'eig'//achar(d10+aoff)//achar(d1+aoff)
      ELSE
        d10 = mod((irank-d1)/10,10)
        d100 = (irank-10*d10-d1)/100
        IF ( d100.GE.10 ) d100 = d100 + iachar('7')
        fstart =
     +  'eig'//achar(d100+aoff)//achar(d10+aoff)//achar(d1+aoff)
      ENDIF


      ngopr1(:)=1

      l_p0 = .false.
      if (irank.eq.0) l_p0 = .true.

      tpi = 2* pimach()
      lmd = lmaxd*(lmaxd+2)

!!!   should be changed in case the windows are really used
283
      nkpts = nkpt
284 285 286 287 288 289 290

      ! do we have to construct GWF ?
      l_gwf = .false.
      l_gwf = l_sgwf.or.l_socgwf 
      

c-----read the input file to determine what to do
291
      call wann_read_inp(input,l_p0,wann)
292

293
      if(wann%l_byenergy.and.wann%l_byindex) CALL juDFT_error
294
     +     ("byenergy.and.byindex",calledby ="wannier")
295
      if(wann%l_byenergy.and.wann%l_bynumber) CALL juDFT_error
296
     +     ("byenergy.and.bynumber",calledby ="wannier")
297
      if(wann%l_bynumber.and.wann%l_byindex) CALL juDFT_error
298 299
     +     ("bynumber.and.byindex",calledby ="wannier")
      if(.not.(wann%l_bynumber.or.wann%l_byindex.or.wann%l_byenergy))
300
     &     CALL juDFT_error("no rule to sort bands",calledby ="wannier")
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315


      efermi=ef
      if(.not.wann%l_fermi)efermi=0.0

#ifdef CPP_MPI
         call MPI_BARRIER(MPI_COMM_WORLD,ierr)
#endif

c**************************************************************
c   for bzsym=.true.: determine mapping between kpts and w90kpts
c**************************************************************
      if (wann%l_bzsym) then
         l_file=.false.
         inquire(file='w90kpts',exist=l_file)
316
         if(.not.l_file)  CALL juDFT_error
317 318 319 320 321
     +        ("w90kpts not found, needed if bzsym",calledby ="wannier")
         open(412,file='w90kpts',form='formatted')
         read(412,*)fullnkpts
         close(412)
         if(l_p0)print*,"fullnkpts=",fullnkpts
322
         if(fullnkpts<nkpts) CALL juDFT_error("fullnkpts.lt.nkpts"
323 324 325 326 327
     +        ,calledby ="wannier")
         allocate(irreduc(fullnkpts),mapkoper(fullnkpts))
         allocate(shiftkpt(3,fullnkpts))
         l_file=.false.
         inquire(file='kptsmap',exist=l_file)
328
         if(.not.l_file)  CALL juDFT_error
329 330 331 332
     +        ("kptsmap not found, needed if bzsym",calledby ="wannier")
         open(713,file='kptsmap')
         do i=1,fullnkpts
            read(713,*)kpt,irreduc(i),mapkoper(i),shiftkpt(:,i)
333
            if(kpt/=i) CALL juDFT_error("kpt.ne.i",calledby ="wannier")
334 335 336
            if(l_p0)print*,i,irreduc(i),mapkoper(i)
         enddo
         close(713)
337
         if(maxval(irreduc(:))/=nkpts) CALL juDFT_error
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
     +        ("max(irreduc(:))/=nkpts",calledby ="wannier")
      else
         fullnkpts=nkpts
      endif


      if(l_gwf) fullnqpts = nparampts


      nrec = 0
      if(l_p0)then
      write (*,*) 'fermi energy:',efermi
      write (*,*) 'emin,emax=',e1s,e2s
      write (*,*) 'nbasfcn =',nbasfcn
      endif
      nlotot = 0
      mlotot = 0
      mlolotot = 0
      do n = 1, ntype
        mlotot = mlotot + nlo(n)
        mlolotot = mlolotot + nlo(n)*(nlo(n)+1)/2
        do l = 1,nlo(n)
          nlotot = nlotot + neq(n) * ( 2*llo(l,n) + 1 )
        enddo
      enddo


      allocate(counts(0:isize-1),displs(0:isize-1))
      call array_split(fullnkpts,isize,counts,displs)

c**********************************************************
ccccccccccccccc   read in the bkpts file  ccccccccccccccccc
c**********************************************************
       l_bkpts = .false.
       inquire (file='bkpts',exist=l_bkpts)
373
       if (.not.l_bkpts)  CALL juDFT_error("need bkpts for matrixmmn"
374 375 376 377 378 379 380 381 382 383 384 385 386 387
     +      ,calledby ="wannier")
       open (202,file='bkpts',form='formatted',status='old')
       rewind (202)
       read (202,'(i4)') nntot
       if(l_p0)then
       write (*,*) 'nntot=',nntot
       write(*,*) 'fullnkpts=',fullnkpts
       write(*,*) 'nkpts=',nkpts
       endif
       allocate ( gb(1:3,1:nntot,1:fullnkpts),bpt(1:nntot,1:fullnkpts))
       do ikpt=1,fullnkpts
        do nn=1,nntot
         read (202,'(2i6,3x,3i4)')
     &     ikpt_help,bpt(nn,ikpt),(gb(i,nn,ikpt),i=1,3)
388
         if (ikpt/=ikpt_help)  CALL juDFT_error("ikpt.ne.ikpt_help"
389
     +        ,calledby ="wannier")       
390
         if (bpt(nn,ikpt)>fullnkpts) CALL juDFT_error("bpt.gt.fullnkpts"
391 392 393 394 395 396 397 398 399 400 401 402
     +        ,calledby ="wannier")
        enddo
       enddo
       close (202)
        allocate(kdiff(3,nntot))

c**********************************************************
ccccccccccccccc   read in the bqpts file  ccccccccccccccccc         
c**********************************************************
      if (l_gwf.or.l_ms) then ! for Omega functional minimization
       l_bqpts = .false.
       inquire (file='bqpts',exist=l_bqpts)
403
       if (.not.l_bqpts)  CALL juDFT_error("need bqpts for matrixmmn"
404 405 406 407 408 409 410 411 412 413 414 415 416 417
     +     ,calledby ="wannier")
       open (202,file='bqpts',form='formatted',status='old')
       rewind (202)
       read (202,'(i4)') nntot_q
       if(l_p0)then
       write (*,*) 'nntot_q=',nntot_q
       write(*,*) 'fullnqpts=',fullnqpts
       endif
       allocate ( gb_q(1:3,1:nntot_q,1:fullnqpts),
     &            bpt_q(1:nntot_q,1:fullnqpts))
       do iqpt=1,fullnqpts
        do nn=1,nntot_q
         read (202,'(2i6,3x,3i4)')
     &     iqpt_help,bpt_q(nn,iqpt),(gb_q(i,nn,iqpt),i=1,3)
418
         if (iqpt/=iqpt_help)  CALL juDFT_error("iqpt.ne.iqpt_help"
419 420
     +        ,calledby ="wannier")
         if (bpt_q(nn,iqpt)>fullnqpts)
421
     &        CALL juDFT_error("bpt_q.gt.fullnqpts",calledby ="wannier")
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        enddo
       enddo
       close (202)
        allocate(qdiff(3,nntot_q))
        allocate(zero_qdiff(3,nntot_q))
        zero_qdiff=0.0
      endif                                                        


! when treating gen. WF for spin spirals, the Brillouin zone
! of q-points is twice as large compared to k-BZ. Thus,
! the G-vectors connecting neighbors across the boundary
! need to be doubled
      if(l_sgwf) gb_q = 2*gb_q    
      if(l_socgwf) gb_q = 2*gb_q 

      if(wann%l_finishgwf) goto 9110
c********************************************************
c      find symmetry-related elements in mmkb
c********************************************************
         allocate(maptopair(3,fullnkpts,nntot))
         allocate(pair_to_do(fullnkpts,nntot))
444
         call wann_mmnk_symm(input,kpts,
445 446 447 448 449 450 451 452 453
     >     fullnkpts,nntot,bpt,gb,wann%l_bzsym,
     >     irreduc,mapkoper,l_p0,film,nop,invtab,mrot,odi%d1,
     >     tau,
     <     pair_to_do,maptopair,kdiff,.false.,param_file)

      ! do the same for q-points to construct GWFs
      if(l_gwf)then 
         allocate(maptopair_q(3,fullnqpts,nntot_q))
         allocate(pair_to_do_q(fullnqpts,nntot_q))
454
         call wann_mmnk_symm(input,kpts,
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
     >     fullnqpts,nntot_q,bpt_q,gb_q,wann%l_bzsym,
     >     irreduc_q,mapqoper,l_p0,.false.,1,invtab(1),mrot(:,:,1),
     >     .false.,tau,
     <     pair_to_do_q,maptopair_q,qdiff,.true.,param_file)
      endif


      nntot2 = 1
      allocate(kdiff2(3,nntot2))
      kdiff2 = 0.0

c*********************************************************
cccccccccccccccc   initialize the potential   cccccccccccc
c*********************************************************

      if(.not. l_noco) then
       allocate ( vpw(n3d,jspd),vzxy(nmzxyd,odi%n2d-1,2,jspd) )
      else
       allocate ( vpw(n3d,4),vzxy(nmzxyd,odi%n2d-1,2,4) )
      endif

476 477
      allocate (vz(nmzd,2,4))
      allocate (vr(jmtd,ntypd,jspd))
478

479 480 481 482 483 484 485
      vpw(:,1:SIZE(vTot%pw,2)) = vTot%pw(:,1:SIZE(vTot%pw,2))
      IF(film) THEN
         vz(:,:,1:SIZE(vTot%vacz,3)) =
     &      vTot%vacz(:,:,1:SIZE(vTot%vacz,3))
         vzxy(:,:,:,1:SIZE(vTot%vacxy,4)) =
     &      vTot%vacxy(:,:,:,1:SIZE(vTot%vacxy,4))
      END IF
486 487 488 489

      do jspin = 1,jspins
        do n = 1, ntype
          do j = 1,jri(n)
490
            vr(j,n,jspin) = vTot%mt(j,0,n,jspin)
491 492 493 494
          enddo
        enddo
      enddo

495
      if(.not.film) deallocate(vz,vzxy)
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

      if(l_noco)then
         npotmatfile=25

         OPEN (npotmatfile,FILE='potmat',FORM='unformatted',
     +                  STATUS='old')
c--->    load the interstitial potential
         READ (npotmatfile) (vpw(ig3,1),ig3=1,n3d)
         READ (npotmatfile) (vpw(ig3,2),ig3=1,n3d)
         READ (npotmatfile) (vpw(ig3,3),ig3=1,n3d)
         vpw(:,4) = conjg(vpw(:,3))
         if(film) then
          maxvac=2
          if(odi%d1)maxvac=1
          DO ivac = 1,maxvac
c--->       if the two vacuua are equivalent, the potential file has to
c--->       be backspaced, because the potential is the same at both
c--->       surfaces of the film
            IF ((ivac.EQ.2) .AND. (nvac.EQ.1)) THEN
               DO irec = 1,4
                  BACKSPACE (npotmatfile)
               ENDDO
            ENDIF
c--->       load the non-warping part of the potential
            READ (npotmatfile)((vz(imz,ivac,ipot),imz=1,nmzd),ipot=1,4)

c--->       load the warping part of the potential
            if(.not.odi%d1)then
               DO ipot = 1,3
                  READ (npotmatfile)((vzxy(imz,igvm2,ivac,ipot),
     +                           imz=1,nmzxy),igvm2=1,nq2-1)
               ENDDO   
            else
               DO ipot = 1,3
                  READ (npotmatfile)((vzxy(imz,igvm2,ivac,ipot),
     +                           imz=1,nmzxy),igvm2=1,odi%n2d-1)
               ENDDO   
            endif   
            vzxy(:,:,:,4) = conjg(vzxy(:,:,:,3))
          enddo   
         endif
         CLOSE (npotmatfile)
      endif   
   

      if(film .and. l_p0) write(*,*)'nvac',nvac

cccccccccccccccc   end of the potential part  ccccccccccc
      wannierspin=jspd
      if(l_soc) wannierspin=2
     
      allocate ( flo(ntypd,jmtd,2,nlod,2) )
      allocate ( ff(ntypd,jmtd,2,0:lmaxd,2) )
      allocate ( gg(ntypd,jmtd,2,0:lmaxd,2) )
550 551 552 553 554 555 556 557 558 559
      allocate ( usdus%us(0:lmaxd,ntypd,2) )
      allocate ( usdus%uds(0:lmaxd,ntypd,2) )
      allocate ( usdus%dus(0:lmaxd,ntypd,2) )
      allocate ( usdus%duds(0:lmaxd,ntypd,2) )
      allocate ( usdus%ddn(0:lmaxd,ntypd,2) )
      allocate ( usdus%ulos(nlod,ntypd,2) )
      allocate ( usdus%dulos(nlod,ntypd,2) )
      allocate ( usdus%uulon(nlod,ntypd,2) )
      allocate ( usdus%dulon(nlod,ntypd,2) )
      allocate ( usdus%uloulopn(nlod,nlod,ntypd,2) )
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

      allocate ( kveclo(nlotot),nv(wannierspin) )
      allocate ( kveclo_b(nlotot),nv_b(wannierspin) )
      allocate ( kveclo_b2(nlotot),nv_b2(wannierspin) )
      allocate ( k1(nvd,wannierspin),k2(nvd,wannierspin),
     &           k3(nvd,wannierspin) )
      allocate ( k1_b(nvd,wannierspin),k2_b(nvd,wannierspin),
     &           k3_b(nvd,wannierspin) )
      allocate ( k1_b2(nvd,wannierspin),k2_b2(nvd,wannierspin),
     &           k3_b2(nvd,wannierspin) )

      doublespin_start=1
      if(l_noco.or.l_soc) then
         doublespin_max=4
      else
         doublespin_max=wannierspin
      endif

      l_skip_int = .false.; l_skip_soc = .false.; l_skip_vac = .false.
      l_skip_sph = .false.; l_skip_non = .false.; l_skip_loc = .false.
      inquire(file='debug_uHu',exist=l_exist)
      if(l_exist) then
       open(888,file='debug_uHu')
       read(888,*)l_skip_int
       read(888,*)l_skip_sph
       read(888,*)l_skip_non
       read(888,*)l_skip_soc
       read(888,*)l_skip_loc
       read(888,*)l_skip_vac
       read(888,*)doublespin_max 
       read(888,*)doublespin_start 
       close(888)
       if(l_p0) then
        write(*,*)'skip INT :',l_skip_int
        write(*,*)'skip SPH :',l_skip_sph
        write(*,*)'skip NON :',l_skip_non
        write(*,*)'skip SOC :',l_skip_soc
        write(*,*)'skip LOC :',l_skip_loc
        write(*,*)'skip VAC :',l_skip_vac
        write(*,*)'doublespin_max:',doublespin_max
       endif
      endif

      allocate( tdd(0:lmd,0:lmd,ntypd,nntot*nntot2,4) )
      allocate( tdu(0:lmd,0:lmd,ntypd,nntot*nntot2,4) )
      allocate( tud(0:lmd,0:lmd,ntypd,nntot*nntot2,4) )
      allocate( tuu(0:lmd,0:lmd,ntypd,nntot*nntot2,4) )
      allocate( tdd_soc(0:lmd,0:lmd,ntypd,nntot*nntot2,2) )
      allocate( tdu_soc(0:lmd,0:lmd,ntypd,nntot*nntot2,2) )
      allocate( tud_soc(0:lmd,0:lmd,ntypd,nntot*nntot2,2) )
      allocate( tuu_soc(0:lmd,0:lmd,ntypd,nntot*nntot2,2) )
      allocate( tdulo(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,4) )
      allocate( tuulo(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,4) )
      allocate( tulou(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,4) )
      allocate( tulod(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,4) )
      allocate( tuloulo(nlod,-llod:llod,nlod,-llod:llod,
     >                  ntypd,nntot*nntot2,4) )
      allocate( tdulo_soc(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,2) )
      allocate( tuulo_soc(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,2) )
      allocate( tulou_soc(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,2) )
      allocate( tulod_soc(0:lmd,nlod,-llod:llod,ntypd,nntot*nntot2,2) )
      allocate( tuloulo_soc(nlod,-llod:llod,nlod,-llod:llod,
     >                  ntypd,nntot*nntot2,2) )

      tuu = cmplx(0.,0.); tdu = cmplx(0.,0.)
      tud = cmplx(0.,0.); tdd = cmplx(0.,0.)
      tuu_soc = cmplx(0.,0.); tdu_soc = cmplx(0.,0.)
      tud_soc = cmplx(0.,0.); tdd_soc = cmplx(0.,0.)
      tuulo = cmplx(0.,0.); tdulo = cmplx(0.,0.)
      tulou = cmplx(0.,0.); tulod = cmplx(0.,0.)
      tuloulo = cmplx(0.,0.)
      tuulo_soc = cmplx(0.,0.); tdulo_soc = cmplx(0.,0.)
      tulou_soc = cmplx(0.,0.); tulod_soc = cmplx(0.,0.)
      tuloulo_soc = cmplx(0.,0.)


      call cpu_time(t1)
      t_init = t1-t00

c*****************************************************************c
c                         START Q LOOP                            c
c*****************************************************************c
      do 314 iqpt = 1,fullnqpts  ! loop by q-points starts

644
       ALLOCATE(innerEig_idList(nntot_q))
645

646 647 648 649 650 651 652
        qptibz=iqpt                          
        if(wann%l_bzsym .AND. l_gwf) qptibz=irreduc_q(iqpt)
        if(wann%l_bzsym .AND. l_gwf) oper_q=mapqoper(iqpt)

       qpt_i = qss
       alph_i = alph
       beta_i = beta
653 654
       theta_i = theta
       phi_i = phi
655 656 657 658 659 660 661 662 663 664
       if(l_sgwf.or.l_ms) then
          qpt_i(:) = param_vec(:,qptibz)
          alph_i(:) = param_alpha(:,qptibz)
       elseif(l_socgwf) then 
          if(l_dim(2)) phi_i = tpi*param_vec(2,qptibz)
          if(l_dim(3)) theta_i = tpi*param_vec(3,qptibz)
       endif

       IF (l_gwf) THEN
          do iqpt_b=1,nntot_q
665 666 667 668 669 670 671 672 673 674 675 676 677 678

            innerEig_idList(iqpt_b) = eig_idList(bpt_q(iqpt_b,iqpt))

!            WRITE(fending,'("_",i4.4)')bpt_q(iqpt_b,iqpt)
!            innerEig_idList(iqpt_b)=open_eig(mpi%mpi_comm,
!     +                  DIMENSION%nbasfcn,DIMENSION%neigd,
!     +                  nkpts,wannierspin,atoms%lmaxd,
!     +                  atoms%nlod,atoms%ntype,atoms%nlotot,
!     +                  l_noco,.FALSE.,l_real,l_soc,.FALSE.,
!     +                  mpi%n_size,filename=trim(fstart)//fending,
!     +                  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)
679
          enddo
680 681 682 683 684 685 686 687 688 689 690 691 692

        eig_id = eig_idList(qptibz)

!        WRITE(fending,'("_",i4.4)')qptibz
!        eig_id=open_eig(mpi%mpi_comm,DIMENSION%nbasfcn,DIMENSION%neigd,
!     +                  nkpts,wannierspin,atoms%lmaxd,
!     +                  atoms%nlod,atoms%ntype,atoms%nlotot,
!     +                  l_noco,.FALSE.,l_real,l_soc,.FALSE.,
!     +                  mpi%n_size,filename=trim(fstart)//fending,
!     +                  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)
693
       ELSEIF(l_ms) THEN
694 695 696 697 698 699 700 701 702 703 704 705 706

        eig_id = eig_idList(qptibz)

!        WRITE(fending,'("_",i4.4)')qptibz
!        eig_id=open_eig(mpi%mpi_comm,DIMENSION%nbasfcn,DIMENSION%neigd,
!     +                  nkpts,wannierspin,atoms%lmaxd,
!     +                  atoms%nlod,atoms%ntype,atoms%nlotot,
!     +                  l_noco,.FALSE.,l_real,l_soc,.FALSE.,
!     +                  mpi%n_size,filename=trim(fstart)//fending,
!     +                  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)
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
       ELSE
          fending=''
       ENDIF ! l_gwf.or.l_ms
       nrec=0
       nrec_b=0

c****************************************************
c cycle by spins starts! 
c****************************************************
      do 110 doublespin=doublespin_start,doublespin_max   ! cycle by spins
         if(l_p0) write(*,*)'spin loop:',doublespin

c         jspin=mod(doublespin+1,2)+1
c         jspin_b=jspin
c         if(doublespin.eq.3) jspin_b=2
c         if(doublespin.eq.4) jspin_b=1
         jspin_b=mod(doublespin+1,2)+1
         jspin=jspin_b
         if(doublespin.eq.3) jspin=2
         if(doublespin.eq.4) jspin=1

         nrec_b = nrec

         if(.not.l_noco) then
            nrec = (jspin-1)*nkpts
            nrec_b = (jspin_b-1)*nkpts
         endif

c...read number of bands and wannier functions from file proj

c..reading the proj.1 / proj.2 / proj file
       l_proj=.false.  
       do j=jspin,0,-1
         inquire(file=trim('proj'//spin012(j)),exist=l_proj)
         if(l_proj)then
            filename='proj'//spin012(j)
            exit
         endif
       enddo

       if(l_proj)then
         open (203,file=trim(filename),status='old')
         rewind (203)
         read (203,*) nwfs,numbands
         rewind (203)
         close (203)
       elseif(wann%l_projmethod.or.wann%l_bestproj
     &                         .or.wann%l_matrixamn)then
755
          CALL juDFT_error("no proj/proj.1/proj.2",calledby ="wannier")
756 757 758 759 760 761 762 763 764 765 766 767
       endif  


       jspin2=jspin
       if(l_soc .and. jspins.eq.1)jspin2=1
       jsp_start = jspin ; jsp_end = jspin

cccccccccccc   read in the eigenvalues and vectors   cccccc
       do jspin5=1,2
       jsp_start=jspin5; jsp_end=jspin5
       nrec5=0
       if(.not.l_noco) nrec5 = (jspin5-1)*nkpts
768 769 770
       call judft_error("TODO:wann_uHU_dmi")
!       call cdn_read0(eig_id,irank,isize,jspin5,jspd,l_noco, !wannierspin instead of jspd?
!     <                ello,evac,epar,wk,n_bands,n_size)
771

772 773 774 775 776 777 778 779
       enddo

c..   now we want to define the maximum number of the bands by all kpts
      nbnd = 0
      i_rec = 0 ; n_rank = 0

      if(l_p0)then         
         call wann_maxbnd(
780
     >            eig_id,l_real,
781 782
     >            lmaxd,ntypd,nlod,neigd,nvd,wannierspin,
     >            isize,jspin,nbasfcn,nlotot,
783
     >            l_ss,l_noco,nrec,fullnkpts,
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
     >            wann%l_bzsym,wann%l_byindex,wann%l_bynumber,
     >            wann%l_byenergy,
     >            irreduc,odi,wann%band_min(jspin),
     >            wann%band_max(jspin),
     >            numbands,e1s,e2s,efermi,nkpts,
     <            nbnd,l_gwf,iqpt)      
         write(*,*)'iqpt=',iqpt,'nbnd=',nbnd 
      endif!l_p0

! nbnd is calculated for process zero and is sent here to the others
#ifdef CPP_MPI
      if(l_p0)then
         do cpu_index=1,isize-1
      call MPI_SEND(nbnd,1,MPI_INTEGER,cpu_index,1,MPI_COMM_WORLD,ierr)
         enddo
      else
       call MPI_RECV(nbnd,1,MPI_INTEGER,0,1,MPI_COMM_WORLD,stt,ierr)
      endif
#endif
     
c##################################################################
         if(.not.allocated(uHu)) then
            allocate(uHu(nbnd,nbnd,nntot_q,nntot,counts(irank)))
            uHu = cmplx(0.,0.)
         endif



      if(iqpt.eq.1) then

      do jspin4=1,2
         jspin3=jspin4
         if(jspins.eq.1) jspin3=1
      na = 1
      do 40 n = 1,ntype
       do 30 l = 0,lmax(n)
c...compute the l-dependent, k-independent radial MT- basis functions
         call radfun(
822 823 824 825
     >              l,n,jspin4,epar(l,n,jspin3),vr(1,n,jspin3),atoms,
     <              ff(n,:,:,l,jspin4),gg(n,:,:,l,jspin4),usdus,
     <              nodeu,noded,wronk)

826 827 828
   30  continue
c...and the local orbital radial functions
       do ilo = 1, nlo(n)
829

830
         call radflo(
831 832 833 834
     >             atoms,n,jspin4,ello(:,:,jspin3),vr(1,n,jspin3),
     >             ff(n,1:,1:,0:,jspin4),gg(n,1:,1:,0:,jspin4),mpi,
     <             usdus,uuilon,duilon,ulouilopn,flo(n,:,:,:,jspin4))

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
       enddo
c       na = na + neq(n)
   40 continue
      enddo!jspin3


         if(l_p0) write(*,'(4(a,i1),a)')'< ',jspin,' | H_MT(',jspin,',',
     >                                       jspin,') | ',jspin_b,' >'
         jspin3=jspin
         if(jspins.eq.1) jspin3=1
         call cpu_time(t0)
         call wann_uHu_tlmplm2(
     >        memd,nlhd,ntypsd,ntypd,jmtd,lmaxd,jspd,ntype,dx,
     >        rmsh,jri,lmax,ntypsy,natd,lnonsph,lmd,lmplmd,clnu,
     >        mlh,nmem,llh,nlh,neq,irank,mlotot,mlolotot,
850
     >        vTot%mt(:,:,:,jspin3),nlod,llod,loplod,ello(1,1,jspin3),
851 852 853 854 855 856 857 858 859 860 861 862
     >        llo,nlo,lo1l,l_dulo,ulo_der,ff(:,:,:,:,jspin),
     >        gg(:,:,:,:,jspin),flo(:,:,:,:,jspin),
     >        ff(:,:,:,:,jspin_b),gg(:,:,:,:,jspin_b),
     >        flo(:,:,:,:,jspin_b),kdiff,kdiff2,nntot,nntot2,bmat,bbmat,
     >        vr(1,1,jspin3),epar(0,1,1),invsat,jspin,jspin_b,
     >        l_skip_sph,l_skip_non,l_skip_loc,
     <        tuu(:,:,:,:,doublespin),tud(:,:,:,:,doublespin),
     >        tdu(:,:,:,:,doublespin),tdd(:,:,:,:,doublespin),
     >        tuulo(:,:,:,:,:,doublespin),tulou(:,:,:,:,:,doublespin),
     >        tdulo(:,:,:,:,:,doublespin),tulod(:,:,:,:,:,doublespin),
     >        tuloulo(:,:,:,:,:,:,doublespin))
         call cpu_time(t1)
863
         t_myTlmplm = t_myTlmplm + t1-t0
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

      endif!iqpt.eq.1

      if(l_soc.and. (.not.l_skip_soc)) then

          tuu_soc = cmplx(0.,0.); tdu_soc = cmplx(0.,0.)
          tdd_soc = cmplx(0.,0.); tud_soc = cmplx(0.,0.)
          tuulo_soc = cmplx(0.,0.); tdulo_soc = cmplx(0.,0.)
          tulou_soc = cmplx(0.,0.); tulod_soc = cmplx(0.,0.)
          tuloulo_soc = cmplx(0.,0.)

         do tspin=1,2
          jspin5=jspin
          if(tspin.eq.2) then
           if(jspin.eq.1) jspin5=2
           if(jspin.eq.2) jspin5=1
          endif
         if(l_p0) write(*,'(4(a,i1),a,f9.6)')
     >         '< ',jspin,' | H_SO(',jspin,',',jspin5,') | ',
     >              jspin_b,' >  for theta=',theta_i
         call cpu_time(t0)
         call wann_uHu_soc(
886
     >     input,atoms,
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
     >     ntypd,jmtd,lmaxd,jspd,
     >     ntype,dx,rmsh,jri,lmax,natd,
     >     lmd,lmplmd,neq,irank,
     >     nlod,llod,loplod,ello,llo,nlo,lo1l,l_dulo,ulo_der,
     >     ff(:,:,:,:,jspin),gg(:,:,:,:,jspin),flo(:,:,:,:,jspin),
     >     ff(:,:,:,:,jspin_b),gg(:,:,:,:,jspin_b),flo(:,:,:,:,jspin_b),
     >     kdiff,kdiff2,nntot,nntot2,bmat,bbmat,
     >     vr,epar,jspin,jspin5,jspins,
     >     .true.,theta_i,phi_i,alph_i,beta_i,l_noco,l_skip_loc,
     <     tuu_soc(:,:,:,:,tspin),tud_soc(:,:,:,:,tspin),
     >     tdu_soc(:,:,:,:,tspin),tdd_soc(:,:,:,:,tspin),
     >     tuulo_soc(:,:,:,:,:,tspin),tulou_soc(:,:,:,:,:,tspin),
     >     tdulo_soc(:,:,:,:,:,tspin),tulod_soc(:,:,:,:,:,tspin),
     >     tuloulo_soc(:,:,:,:,:,:,tspin))
         call cpu_time(t1)
902
         t_myTlmplm = t_myTlmplm + t1-t0
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
         enddo!tspin 
      endif!l_soc

      !endif!iqpt.eq.1

      i_rec = 0 ; n_rank = 0
      
c****************************************************************
c.. loop by kpoints starts!      each may be a separate task
c****************************************************************
      if(l_p0) write(*,*)'start k-loop'
      do 10 ikpt = wann%ikptstart,fullnkpts  ! loop by k-points starts
        kptibz=ikpt
        if(wann%l_bzsym) kptibz=irreduc(ikpt)
        if(wann%l_bzsym) oper=mapkoper(ikpt)

        if(kpt_on_node(ikpt,isize,counts,displs).eq.irank) then
        i_rec = i_rec + 1
c      if (mod(i_rec-1,isize).eq.irank) then

923
      allocate (eigg(neigd))
924 925 926 927 928 929

          n_start=1
          n_end=neigd
 
      call cpu_time(t0)
      ! get current bkpt vector
930 931 932 933 934 935 936 937 938 939

      zzMat%l_real = l_real
      zzMat%nbasfcn = nbasfcn
      zzMat%nbands = neigd
      IF(l_real) THEN
         ALLOCATE (zzMat%z_r(zzMat%nbasfcn,zzMat%nbands))
      ELSE
         ALLOCATE (zzMat%z_c(zzMat%nbasfcn,zzMat%nbands))
      END IF

940 941 942 943 944 945
!      CALL cdn_read(
!     >              eig_id,
!     >              nvd,jspd,irank,isize,kptibz,jspin,nbasfcn, !wannierspin instead of jspd?
!     >              l_ss,l_noco,neigd,n_start,n_end,
!     <              nmat,nv,ello,evdu,epar,kveclo,
!     <              k1,k2,k3,bkpt,wk,nbands,eigg,zzMat)
946

947 948 949
      call cpu_time(t1)
      t_eig = t_eig + t1 - t0

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
      zMat_b%l_real = zzMat%l_real
      zMat_b2%l_real = zzMat%l_real
      zMat_b%nbasfcn = zzMat%nbasfcn
      zMat_b2%nbasfcn = zzMat%nbasfcn
      zMat_b%nbands = zzMat%nbands
      zMat_b2%nbands = zzMat%nbands
      IF(zzMat%l_real) THEN
         ALLOCATE (zMat_b%z_r(zMat%nbasfcn,zMat%nbands))
         ALLOCATE (zMat_b2%z_r(zMat%nbasfcn,zMat%nbands))
      ELSE
         ALLOCATE (zMat_b%z_c(zMat%nbasfcn,zMat%nbands))
         ALLOCATE (zMat_b2%z_c(zMat%nbasfcn,zMat%nbands))
      END IF

      allocate (we_b(neigd), we_b2(neigd))
965 966 967 968 969 970 971 972 973 974 975 976 977 978

  !!! the cycle by the nearest neighbors (nntot) for each kpoint

       do 15  ikpt_b = 1,nntot
          kptibz_b=bpt(ikpt_b,ikpt)

          if(wann%l_bzsym) oper_b=mapkoper(kptibz_b)
          if (wann%l_bzsym) kptibz_b=irreduc(kptibz_b)

          n_start=1
          n_end=neigd

        eigg = 0.
        call cpu_time(t0)
979

980 981 982 983 984 985
!      CALL cdn_read(
!     >              eig_id,
!     >              nvd,jspd,irank,isize,kptibz_b,jspin,nbasfcn, !wannierspin instead of jspd?
!     >              l_ss,l_noco,neigd,n_start,n_end,
!     <              nmat_b,nv_b,ello,evdu,epar,kveclo_b,
!     <              k1_b,k2_b,k3_b,bkpt_b,wk_b,nbands_b,eigg,zzMat)
986 987 988

        nslibd_b = 0

989 990 991 992 993 994
      IF(zzMat%l_real) THEN
         zMat_b%z_r = 0.0
      ELSE
         zMat_b%z_c = CMPLX(0.0,0.0)
      END IF

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
      eig_b(:) = 0.

        do i = 1,nbands_b
          if((eigg(i).ge.e1s.and.nslibd_b.lt.numbands
     & .and.wann%l_bynumber)
     &.or.(eigg(i).ge.e1s.and.eigg(i).le.e2s.and.wann%l_byenergy)
     &.or.(i.ge.wann%band_min(jspin)
     &                 .and.
     &       (i.le.wann%band_max(jspin))
     &                 .and.
     &       wann%l_byindex))then
            nslibd_b = nslibd_b + 1
            eig_b(nslibd_b) = eigg(i)
            we_b(nslibd_b) = we_b(i)
            if(l_noco)then
               funbas =        nv_b(1) + nlotot
               funbas = funbas+nv_b(2) + nlotot
            else
               funbas = nv_b(jspin) + nlotot
            endif
1015 1016 1017 1018 1019 1020 1021 1022 1023
            IF (zzMat%l_real) THEN
               do j = 1,funbas
                  zMat_b%z_r(j,nslibd_b) = zzMat%z_r(j,i)
               enddo
            ELSE
               do j = 1,funbas
                  zMat_b%z_c(j,nslibd_b) = zzMat%z_c(j,i)
               enddo
            END IF
1024 1025 1026 1027 1028 1029 1030
          endif
        enddo

c***********************************************************
c              Rotate the wavefunction of next neighbor.
c***********************************************************
      if (wann%l_bzsym .and. (oper_b.ne.1)  ) then
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
!         call wann_kptsrotate(
!     >            natd,nlod,llod,
!     >            ntypd,nlo,llo,invsat,
!     >            l_noco,l_soc,
!     >            ntype,neq,nlotot,
!     >            kveclo_b,jspin,
!     >            oper_b,nop,mrot,nvd,
!     >            nv_b,
!     >            shiftkpt(:,bpt(ikpt_b,ikpt)),
!     >            tau,
!     x            bkpt_b,k1_b(:,:),
!     x            k2_b(:,:),k3_b(:,:),
!     x            zMat_b,nsfactor_b)
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
      else
         nsfactor_b=cmplx(1.0,0.0)
      endif
        noccbd_b = nslibd_b
      call cpu_time(t1)
      t_eig = t_eig + t1 - t0

      addnoco = 0
      if(l_noco.and.jspin.eq.2) addnoco=nv_b(1)+nlotot

      ! set up a(k+b1),b(k+b1),c(k+b1)
      allocate( acof_b(noccbd_b,0:lmd,natd),
     >          bcof_b(noccbd_b,0:lmd,natd),
     >          ccof_b(-llod:llod,noccbd_b,nlod,natd) )

      call cpu_time(t0)
1060

1061 1062 1063
      ALLOCATE(lapw_b%k1(SIZE(k1_b,1),SIZE(k1_b,2)))
      ALLOCATE(lapw_b%k2(SIZE(k1_b,1),SIZE(k1_b,2)))
      ALLOCATE(lapw_b%k3(SIZE(k1_b,1),SIZE(k1_b,2)))
1064 1065 1066 1067 1068
      lapw_b%k1 = k1_b
      lapw_b%k2 = k2_b
      lapw_b%k3 = k3_b
      lapw_b%nmat = nmat_b
      lapw_b%nv = nv_b
1069
      ! I think the other variables of lapw are not needed here.
1070

1071 1072 1073
!      CALL abcof(input,atoms,noccbd_b,sym,cell,bkpt_b,lapw_b,
!     +           noccbd_b,usdus,noco,jspin,kveclo_b,oneD,
!     +           acof_b,bcof_b,ccof_b,zMat_b)
1074 1075 1076

      DEALLOCATE(lapw_b%k1,lapw_b%k2,lapw_b%k3)

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
      call wann_abinv(
     >        ntypd,natd,noccbd_b,lmaxd,lmd,llod,nlod,ntype,neq,
     >        noccbd_b,lmax,nlo,llo,invsat,invsatnr,bkpt_b,taual,
     X        acof_b,bcof_b,ccof_b)
      call cpu_time(t1)
      t_abcof = t_abcof + t1 - t0

       do 25  iqpt_b = 1,nntot_q

          qptibz_b=bpt_q(iqpt_b,iqpt)

          if(qptibz_b.eq.qptibz) cycle  ! no need to compute overlaps
                                        ! with periodic images for now
          qptb_i = qss
          alphb_i = alph
          betab_i = beta
1093 1094
          thetab_i = theta
          phib_i = phi
1095 1096 1097 1098 1099 1100 1101 1102 1103
          if(l_dim(2)) phib_i = tpi*param_vec(2,qptibz_b)
          if(l_dim(3)) thetab_i = tpi*param_vec(3,qptibz_b)

          n_start=1
          n_end=neigd

          eigg = 0.
          call cpu_time(t0)

1104
        WRITE(*,*) 'Do I read out the right record here? (wann_uHu_dmi)'
1105 1106 1107 1108 1109 1110 1111
!          CALL cdn_read(
!     >                  innerEig_idList(iqpt_b),
!     >                  nvd,jspd,irank,isize,kptibz,jspin_b,nbasfcn, !wannierspin instead of jspd? !kptibz_b2?
!     >                  l_ss,l_noco,neigd,n_start,n_end,
!     <                  nmat_b2,nv_b2,ello,evdu,epar,kveclo_b2,
!     <                  k1_b2,k2_b2,k3_b2,bkpt_b2,wk_b2,nbands_b2,eigg,
!     <                  zzMat)
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
!#if(!defined(CPP_HDF) && defined(CPP_MPI))
!        !if(l_p0) write(*,*)'wann_read_eig for qptibz_b=',qptibz_b
!        call wann_read_eig(
!     >              lmaxd,ntypd,nlod,neigd,nvd,wannierspin,
!     >              irank,isize,kptibz,jspin_b,nbasfcn,nlotot,
!     >              l_ss,l_noco,nrec_b,irecl,
!     <              nmat_b2,nv_b2,ello,evdu,epar,kveclo_b2,
!     <              k1_b2,k2_b2,k3_b2,bkpt_b2,wk_b2,nbands_b2,
!     <              eigg,zz,cp_time,funit_start+iqpt_b,l_gwf,qptibz_b)
!#else
!        call cdn_read(
!     >                lmaxd,ntypd,nlod,neigd,nvd,jspd,!wannierspin,
!     >                irank,isize,kptibz,jspin_b,nbasfcn,nlotot,
!     >                l_ss,l_noco,nrec_b,kptibz,funit_start+iqpt_b,
!     >                neigd,n_start,n_end,
!     <                nmat_b2,nv_b2,ello,evdu,epar,kveclo_b2,
!     <                k1_b2,k2_b2,k3_b2,bkpt_b2,wk_b2,nbands_b2,
!     <                eigg,zz,cp_time)
!
!#endif
1132 1133 1134 1135
        nslibd_b2 = 0

        IF(ANY(bkpt_b2.ne.bkpt)) stop 'bkpt_b2.ne.bkpt'

1136 1137 1138 1139 1140 1141
      IF(zzMat%l_real) THEN
         zMat_b2%z_r = 0.0
      ELSE
         zMat_b2%z_c = CMPLX(0.0,0.0)
      END IF

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
      eig_b2(:) = 0.

        do i = 1,nbands_b2
          if((eigg(i).ge.e1s.and.nslibd_b2.lt.numbands
     & .and.wann%l_bynumber)
     &.or.(eigg(i).ge.e1s.and.eigg(i).le.e2s.and.wann%l_byenergy)
     &.or.(i.ge.wann%band_min(jspin_b)
     &                 .and.
     &       (i.le.wann%band_max(jspin_b))
     &                 .and.
     &       wann%l_byindex))then
            nslibd_b2 = nslibd_b2 + 1
            eig_b2(nslibd_b2) = eigg(i)
            we_b2(nslibd_b2) = we_b2(i)
            if(l_noco)then
               funbas =        nv_b2(1) + nlotot
               funbas = funbas+nv_b2(2) + nlotot
            else
               funbas = nv_b2(jspin_b) + nlotot
            endif
1162 1163 1164 1165 1166 1167 1168 1169 1170
            IF (zzMat%l_real) THEN
               do j = 1,funbas
                  zMat_b2%z_r(j,nslibd_b2) = zzMat%z_r(j,i)
               enddo
            ELSE
               do j = 1,funbas
                  zMat_b2%z_c(j,nslibd_b2) = zzMat%z_c(j,i)
               enddo
            END IF
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
          endif
        enddo

c***********************************************************
c              Rotate the wavefunction of next neighbor.
c***********************************************************
      nsfactor_b2=cmplx(1.0,0.0)
      noccbd_b2 = nslibd_b2

      call cpu_time(t1)
      t_eig = t_eig + t1 - t0

      addnoco2 = 0
      if(l_noco.and.jspin_b.eq.2) addnoco2=nv_b2(1)+nlotot

      ! set up a(k+b2),b(k+b2),c(k+b2)
      allocate( acof_b2(noccbd_b2,0:lmd,natd),
     >          bcof_b2(noccbd_b2,0:lmd,natd),
     >          ccof_b2(-llod:llod,noccbd_b2,nlod,natd) )

      call cpu_time(t0)
1192

1193 1194 1195
      ALLOCATE(lapw_b2%k1(SIZE(k1_b2,1),SIZE(k1_b2,2)))
      ALLOCATE(lapw_b2%k2(SIZE(k1_b2,1),SIZE(k1_b2,2)))
      ALLOCATE(lapw_b2%k3(SIZE(k1_b2,1),SIZE(k1_b2,2)))
1196 1197 1198 1199 1200
      lapw_b2%k1 = k1_b2
      lapw_b2%k2 = k2_b2
      lapw_b2%k3 = k3_b2
      lapw_b2%nmat = nmat_b2
      lapw_b2%nv = nv_b2
1201
      ! I think the other variables of lapw are not needed here.
1202

1203 1204 1205
!      CALL abcof(input,atoms,noccbd_b2,sym,cell,bkpt_b2,lapw_b2,
!     +           noccbd_b2,usdus,noco,jspin_b,kveclo_b2,oneD,
!     +           acof_b2,bcof_b2,ccof_b2,zMat_b2)
1206 1207 1208

      DEALLOCATE(lapw_b2%k1,lapw_b2%k2,lapw_b2%k3)

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
      call wann_abinv(
     >        ntypd,natd,noccbd_b2,lmaxd,lmd,llod,nlod,ntype,neq,
     >        noccbd_b2,lmax,nlo,llo,invsat,invsatnr,bkpt_b2,taual,
     X        acof_b2,bcof_b2,ccof_b2)
      call cpu_time(t1)
      t_abcof = t_abcof + t1 - t0


c**************************************c
c    calculate uHu matrix due to:      c
c             (i)    interstitial      c
c             (ii)   muffin tins       c
c             (iii)  vacuum            c 
c**************************************c

      ! transformation from (thetab_i,phib_i)-frame
      ! to global frame to (theta_i,phi_i)-frame
      dth = (thetab_i - theta_i)/2.0
      dph = (phib_i - phi_i)/2.0
      if(l_nochi) then
       dth = 0.0
       dph = 0.0
      endif

      if(l_p0 .and. dph.ne.0.0) then
       write(*,*)'WARNING: include dph in chi trafo!'
      endif

      if( (jspin.eq.1) .and. (jspin_b.eq.1) ) then     ! (S,S') = uu
         chi =  cos(dth) !* cmplx(cos(dph),-sin(dph))
         chi2=  sin(dth) !* cmplx(cos(dph), sin(dph))
      elseif( (jspin.eq.2) .and. (jspin_b.eq.2) ) then ! (S,S') = dd
         chi =  cos(dth) !* cmplx(cos(dph), sin(dph))
         chi2= -sin(dth) !* cmplx(cos(dph),-sin(dph))
      elseif( (jspin.eq.2) .and. (jspin_b.eq.1) ) then ! (S,S') = du
         chi =  sin(dth) !* cmplx(cos(dph), sin(dph))
         chi2=  cos(dth) !* cmplx(cos(dph),-sin(dph))
      elseif( (jspin.eq.1) .and. (jspin_b.eq.2) ) then ! (S,S') = ud
         chi = -sin(dth) !* cmplx(cos(dph),-sin(dph))
         chi2=  cos(dth) !* cmplx(cos(dph), sin(dph))
      else
       stop 'problem setting up chi: jspin,jspin_b'
      endif
      chi = conjg(chi)
      chi2= conjg(chi2)

      !if(irank.eq.0) write(*,*)theta_i,thetab_i,chi,chi2

      ! MT (SPH, NON)
      if(.not.(l_skip_sph.and.l_skip_non)) then
         ! matrix elements < S | H_{SS} | S' > x chi
         call cpu_time(t0)
         call wann_uHu_sph(
     >        chi,nbnd,llod,nslibd_b,nslibd_b2,nlod,natd,ntypd,
     >        lmd,jmtd,taual,nop,lmax,ntype,neq,nlo,llo,
     >        acof_b,bcof_b,ccof_b,bkpt_b2,
     >        acof_b2,bcof_b2,ccof_b2,bkpt_b,bkpt,
     >        gb(:,ikpt_b,ikpt),(/0,0,0/),
     <        tuu(:,:,:,:,doublespin),
     >        tud(:,:,:,:,doublespin),
     >        tdu(:,:,:,:,doublespin),
     >        tdd(:,:,:,:,doublespin),
     >        tuulo(:,:,:,:,:,doublespin),tulou(:,:,:,:,:,doublespin),
     >        tdulo(:,:,:,:,:,doublespin),tulod(:,:,:,:,:,doublespin),
     >        tuloulo(:,:,:,:,:,:,doublespin),
     >        kdiff,kdiff2,nntot,nntot2,
     >        uHu(:,:,iqpt_b,ikpt_b,i_rec))
         call cpu_time(t1)
         t_sph = t_sph + t1 - t0
      endif

      ! MT (SOC)
      if(.not.l_skip_soc) then
         ! matrix elements < S | H_{SS} | S' > x chi
         call cpu_time(t0)
         call wann_uHu_sph(
     >        chi,nbnd,llod,nslibd_b,nslibd_b2,nlod,natd,ntypd,
     >        lmd,jmtd,taual,nop,lmax,ntype,neq,nlo,llo,
     >        acof_b,bcof_b,ccof_b,bkpt_b2,
     >        acof_b2,bcof_b2,ccof_b2,bkpt_b,bkpt,
     >        gb(:,ikpt_b,ikpt),(/0,0,0/),
     <        tuu_soc(:,:,:,:,1),
     >        tud_soc(:,:,:,:,1),
     >        tdu_soc(:,:,:,:,1),
     >        tdd_soc(:,:,:,:,1),
     >        tuulo_soc(:,:,:,:,:,1),tulou_soc(:,:,:,:,:,1),
     >        tdulo_soc(:,:,:,:,:,1),tulod_soc(:,:,:,:,:,1),
     >        tuloulo_soc(:,:,:,:,:,:,1),
     >        kdiff,kdiff2,nntot,nntot2,
     >        uHu(:,:,iqpt_b,ikpt_b,i_rec))
         call cpu_time(t1)
         t_sph = t_sph + t1 - t0

         ! matrix elements < S | H_{SS''} | S' > x chi2 , S''=/=S
         call cpu_time(t0)
         call wann_uHu_sph(
     >        chi2,nbnd,llod,nslibd_b,nslibd_b2,nlod,natd,ntypd,
     >        lmd,jmtd,taual,nop,lmax,ntype,neq,nlo,llo,
     >        acof_b,bcof_b,ccof_b,bkpt_b2,
     >        acof_b2,bcof_b2,ccof_b2,bkpt_b,bkpt,
     >        gb(:,ikpt_b,ikpt),(/0,0,0/),
     <        tuu_soc(:,:,:,:,2),
     >        tud_soc(:,:,:,:,2),
     >        tdu_soc(:,:,:,:,2),
     >        tdd_soc(:,:,:,:,2),
     >        tuulo_soc(:,:,:,:,:,2),tulou_soc(:,:,:,:,:,2),
     >        tdulo_soc(:,:,:,:,:,2),tulod_soc(:,:,:,:,:,2),
     >        tuloulo_soc(:,:,:,:,:,:,2),
     >        kdiff,kdiff2,nntot,nntot2,
     >        uHu(:,:,iqpt_b,ikpt_b,i_rec))
         call cpu_time(t1)
         t_sph = t_sph + t1 - t0
      endif


         jspin3 = jspin !doublespin
         if(jspins.eq.1) jspin3=1

         sign2 = 1

         ! INT
         if(.not.l_skip_int) then
            ! matrix elements < S | H_{SS} | S' > x chi
            call cpu_time(t0)
            call wann_uHu_int(chi,nvd,k1d,k2d,k3d,n3d,
     >            nv_b(jspin),nv_b2(jspin_b),nbnd,neigd,
     >            nslibd_b,nslibd_b2,nbasfcn,addnoco,addnoco2,
     >            k1_b(:,jspin), k2_b(:,jspin), k3_b(:,jspin),
     >            gb(:,ikpt_b,ikpt),
     >            k1_b2(:,jspin_b),k2_b2(:,jspin_b),k3_b2(:,jspin_b),
     >            (/0,0,0/),bkpt,bbmat,
1340
     >            vpw(:,jspin3),zMat_b,zMat_b2,rgphs,
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
     >            ustep,ig,.true.,sign2,
     >            uHu(:,:,iqpt_b,ikpt_b,i_rec))
            call cpu_time(t1)
            t_int = t_int + t1 - t0
         endif

         ! VAC
         if ((.not.l_skip_vac) .and. film .and. (.not.odi%d1)) then
            ! matrix elements < S | H_{SS} | S' > x chi
            call cpu_time(t0)
            call wann_uHu_vac(
     >            chi,l_noco,l_soc,zrfs,jspins,nlotot,qpt_i,nbnd,z1,
     >            nmzxyd,nmzd,n2d,nv2d,k1d,k2d,k3d,n3d,nvac,ig,
     >            rgphs,nmzxy,nmz,delz,ig2,nq2,kv2,area,bmat,bbmat,
     >            evac(:,jspin),evac(:,jspin_b),bkpt_b,bkpt_b2,
     >            vzxy(:,:,:,jspin3),vz,nslibd_b,nslibd_b2,
     >            jspin,jspin_b,jspin,k1_b,k2_b,k3_b,
     >            k1_b2,k2_b2,k3_b2,wannierspin,nvd,nbasfcn,neigd,
1359
     >            zMat_b,zMat_b2,nv_b,nv_b2,omtil,gb(:,ikpt_b,ikpt),
1360 1361 1362 1363 1364 1365 1366 1367 1368
     >            (/0,0,0/),sign2,
     >            uHu(:,:,iqpt_b,ikpt_b,i_rec))
            call cpu_time(t1)
            t_vac = t_vac + t1 - t0

         elseif ((.not.l_skip_vac) .and. odi%d1) then

            call cpu_time(t0)
            call wann_uHu_od_vac(
1369
     >            DIMENSION,oneD,vacuum,stars,cell,
1370 1371 1372 1373 1374
     >            chi,l_noco,l_soc,jspins,nlotot,nbnd,z1,nmzxyd,nmzd,
     >            nv2d,k1d,k2d,k3d,n2d,n3d,ig,nmzxy,nmz,delz,ig2,
     >            bbmat,evac(1,jspin),evac(1,jspin_b),bkpt_b,bkpt_b2,
     >            odi,vzxy(:,:,:,jspin3),vz,nslibd_b,nslibd_b2,jspin,
     >            jspin_b,jspin,k1_b,k2_b,k3_b,k1_b2,k2_b2,k3_b2,
1375
     >           wannierspin,nvd,area,nbasfcn,neigd,zMat_b,zMat_b2,nv_b,
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
     >            nv_b2,sk2,phi2,omtil,gb(:,ikpt_b,ikpt),
     >            (/0,0,0/),qpt_i,sign2,
     >            uHu(:,:,iqpt_b,ikpt_b,i_rec))
            call cpu_time(t1)
            t_vac = t_vac + t1 - t0

         endif

        deallocate (acof_b2,bcof_b2,ccof_b2)
25      continue ! end of loop by the nearest k-neighbors

        deallocate (acof_b,bcof_b,ccof_b)
15      continue ! end of loop by the nearest k-neighbors
1389 1390
        deallocate (we_b, we_b2)
      deallocate ( eigg )
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

      endif   ! loop by processors

10    continue ! end of cycle by the k-points

#ifdef CPP_MPI
      call MPI_BARRIER(MPI_COMM_WORLD,ierr)
#endif
  5   continue

      if(.not.l_noco)nrec=nrec+nkpts

110   continue ! end of cycle by spins

#ifdef CPP_MPI
      call MPI_BARRIER(MPI_COMM_WORLD,ierr)
#endif

      ! close eig files
      IF (l_gwf) THEN
1411
!         CALL close_eig(eig_id)
1412
         DO iqpt_b=1,nntot_q
1413
!            CALL close_eig(innerEig_idList(iqpt_b))
1414
         ENDDO
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
      ENDIF

      if(l_p0) write(*,*)'write uHu file'
      uHu = htr2ev * uHu
      call wann_write_uHu(1,l_p0,fullnkpts,nntot,nntot_q,wann,
     >          nbnd,bpt,gb,isize,irank,fending,'_kq',uHu,
     >          counts(irank),counts,displs,isize,
     >          wann%l_unformatted,.false.,.false.)
      if(allocated(uHu)) deallocate(uHu)

1425
      DEALLOCATE(innerEig_idList)
1426

1427 1428 1429 1430 1431 1432 1433
314   continue ! iqpt, q-points
c************************************************c
c               END Q LOOP                       c
c************************************************c


      deallocate ( kveclo,nv,k1,k2,k3 )
1434
      deallocate ( ff,gg)
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
      deallocate ( flo )
      if (allocated(nv_b))deallocate(kveclo_b,nv_b,k1_b,k2_b,k3_b)
      if (allocated(nv_b2))deallocate(kveclo_b2,nv_b2,k1_b2,k2_b2,k3_b2)
      if (wann%l_bzsym)deallocate(irreduc,mapkoper,shiftkpt)
      if (wann%l_bzsym.AND.l_gwf)deallocate(irreduc_q,mapqoper,shiftqpt)
      if (allocated(pair_to_do)) deallocate(pair_to_do,maptopair)
      if (allocated(pair_to_do_q)) deallocate(pair_to_do_q,maptopair_q)
      if (allocated(kdiff)) deallocate ( kdiff )
      if (allocated(kdiff2)) deallocate ( kdiff2 )
      if (allocated(qdiff)) deallocate(qdiff,zero_qdiff)
      if(allocated(vpw)) deallocate(vpw)
      if(allocated(vzxy)) deallocate(vzxy)
      if(allocated(vz)) deallocate(vz)
1448
      deallocate(vr)
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
      deallocate(tdd,tdu,tud,tuu)
      deallocate(tdd_soc,tdu_soc,tud_soc,tuu_soc)
      deallocate(tdulo,tuulo)
      deallocate(tulou,tulod)
      deallocate(tuloulo)
      deallocate(tdulo_soc,tuulo_soc)
      deallocate(tulou_soc,tulod_soc)
      deallocate(tuloulo_soc)
      deallocate(counts,displs)   

9110  continue

      if(l_sgwf .or. l_socgwf) gb_q = gb_q/2

      if(l_p0.and.l_gwf) then
         call wann_uHu_commat(
     >              fullnkpts,nntot,bpt,fullnqpts,nntot_q,bpt_q,
     >              gb,gb_q,aux_latt_const,wann%l_unformatted,l_dim,
     >              nparampts,param_vec/2.0)
      endif

      if(allocated(gb)) deallocate(gb,bpt)
      if(allocated(gb_q)) deallocate(gb_q,bpt_q)

#ifdef CPP_MPI
      call MPI_BARRIER(MPI_COMM_WORLD,ierr)
#endif
      call cpu_time(t1)
      t_total = t1-t00
      if(l_p0) then
       write(*,900)'t_init  =',t_init
1480
       write(*,900)'t_myTlmplm=',t_myTlmplm
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
       write(*,900)'t_eig   =',t_eig   
       write(*,900)'t_abcof =',t_abcof 
       write(*,900)'t_int   =',t_int   
       write(*,900)'t_sph   =',t_sph   
       write(*,900)'t_vac   =',t_vac   
       write(*,900)'t_total =',t_total
      endif

900   FORMAT(a,f14.4)

      END SUBROUTINE wann_uHu_dmi

 
      END MODULE m_wann_uHu_dmi