wannier.F 108 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------

7 8 9 10
      MODULE m_wannier
      use m_juDFT
      CONTAINS
      SUBROUTINE wannier(
11
     >      DIMENSION,mpi,input,kpts,sym,atoms,stars,vacuum,sphhar,oneD,
12
     >      wann,noco,cell,enpara,banddos,sliceplot,vTot,results,
13
     >      eig_idList,l_real,nkpt)
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
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c     Makes necessary for the construction of the wannier functions
c     (W90: Yates, Mostofi, Marzari, Souza, Vanderbilt '06 f90 code)
c     ab initio preliminaries: constructs the overlaps of the periodic
c     parts of the wavefunctions and the projections of the 
c     wavefunctions
c     onto a set of starting wfs, i.e. atomic-like orbitals.
c                                                            YM 06
c     Mmn(k,b) = <u_{nk}|u_{m(k+b)}>, u being a periodic part
c                        of the wavefunction psi_nk
c     A_mn^k = <psi_mk|g_n>, where g_n is a trial orbital
c     which are written into the files 'WF1.mmn' and 'WF1.amn'
c           Marzari Vanderbilt PRB 56,12847(1997)
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c     Parallelization, Optionals, Symmetry, Noco&Soc:
c     Frank Freimuth
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c     The routine reads the bkpts file, which contains the following
c     information:
c     1st line: nntot (INT) - number of the nearest neighbors for
c                             each k-point in the MP mesh
c     2-nkpts*nntot lines containing 5 integers i1,i2,i3,i4,i5:
c     i1 - the number of the k-point in the kpts file
c     i2 - number of the k-point, which is a periodic image of
c          k+b in the 1st BZ
c     i3-i5 - coordinates of the G-vector, connecting k-point
c             i2 with the actual k+b k-point
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
42
c     In general, the number of bands for each k-poin t is
43 44 45 46 47 48
c     different: N(k), and also differs from the number of bands
c     we are interested in: N (for instance 5 d-bands of Cu among
c     the 6 s- and d-bands). While matrices Mmn(k) are square
c     for each k-point, matrices Mmn(k,b) can be made so after
c     defining the maximum number of bands max(N(k)).
c     The matrix Amn is non-diagonal by default (N(k)*N).
49
cccccc ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
50 51
c     Total number of wannier functions: nwfs
c
52
c     sliceplot%e1s,sliceplot%e2s: lower and upper boundaries of the energy window:
53 54 55
c     Needed for sorting by number and sorting by energy.
c     Not needed for sorting by index.
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
56 57 58 59
c     Extension to case of higher-dimensional Wannier functions        
c     according to the formalism in PRB 91, 184413 (2015)
c     Jan-Philipp Hanke                                         
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
60 61 62 63 64 65
      use m_types
      use m_wann_mmnk_symm
      use m_wann_rw_eig
      use m_abcof
      use m_radfun
      use m_radflo
66 67
      use m_cdnread
      use m_constants
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
      use m_wann_mmk0_od_vac
      use m_wann_mmkb_od_vac
      use m_wann_mmk0_vac
      use m_wann_mmkb_vac
      use m_wann_updown
      use m_wann_mmk0_sph
      use m_wann_ujugaunt
      use m_wann_mmkb_sph
      use m_wann_projmethod
      use m_wann_amn
      use m_wann_abinv
      use m_wann_kptsrotate
      use m_wann_plot
      use m_wann_read_inp
      use m_wann_plot_symm
      use m_wann_mmkb_int
      use m_wann_postproc
      use m_matmul,only : matmul3,matmul3r
      use m_wann_write_mmnk
      use m_wann_write_amn
      use m_wann_write_nabla
      use m_vsoc
      use m_wann_write_matrix4
      use m_wann_write_matrix5
      use m_wann_orbcomp
      use m_wann_anglmom
#ifdef CPP_TOPO
      use m_wann_surfcurr
      use m_wann_surfcurr_int2
      use m_wann_nabla
      use m_wann_nabla_vac
      use m_wann_soc_to_mom
#endif
101 102 103 104 105 106
      use m_wann_gwf_tools, only : get_index_kq, gwf_plottemplate
      use m_wann_gwf_commat
      use m_wann_gwf_anglmom
      use m_wann_write_mmnk2
      use m_wann_uHu
      use m_wann_uHu_dmi
107
      USE m_eig66_io
108 109 110 111 112 113 114 115 116

      IMPLICIT NONE
#include "cpp_double.h"
#ifdef CPP_MPI
      include 'mpif.h'
      integer ierr(3)
      integer cpu_index
      integer stt(MPI_STATUS_SIZE)
#endif
117

118 119 120
      TYPE(t_dimension), INTENT(IN) :: DIMENSION
      TYPE(t_mpi),       INTENT(IN) :: mpi
      TYPE(t_input),     INTENT(IN) :: input
121
      TYPE(t_kpts),      INTENT(IN) :: kpts
122 123 124 125 126 127 128 129 130 131 132
      TYPE(t_sym),       INTENT(IN) :: sym
      TYPE(t_atoms),     INTENT(IN) :: atoms
      TYPE(t_stars),     INTENT(IN) :: stars
      TYPE(t_vacuum),    INTENT(IN) :: vacuum
      TYPE(t_sphhar),    INTENT(IN) :: sphhar
      TYPE(t_oneD),      INTENT(IN) :: oneD
      TYPE(t_noco),      INTENT(IN) :: noco
      TYPE(t_cell),      INTENT(IN) :: cell
      TYPE(t_enpara),    INTENT(IN) :: enpara
      TYPE(t_banddos),   INTENT(IN) :: banddos
      TYPE(t_sliceplot), INTENT(IN) :: sliceplot
133
      TYPE(t_potden),    INTENT(IN) :: vTot
134
      TYPE(t_results),   INTENT(IN) :: results
135
      TYPE(t_wann),      INTENT(INOUT) :: wann
136 137 138

      logical, intent (in) :: l_real
      integer, intent (in) :: nkpt
139
      INTEGER, INTENT (IN) :: eig_idList(wann%nparampts)
140

141
cccccccccccccccccc   local variables   cccccccccccccccccccc
142
      integer :: lmd,nlotot,n,nmat,iter,ikpt,ikpt_b,nmat_b, pc
143
      integer :: addnoco,funbas,loplod,addnoco2,igvm2,eig_id
144
      integer :: noccbd,noccbd_b,nn,nkpts,i,jspin,j,l,i_rec,m,nwf,nwfp
145
      integer :: jsp_start,jsp_end,nrec,nrec1,nrec_b,nbands,nbands_b
146 147
      integer :: nodeu,noded,n_size,na,n_rank,nbnd,numbands
      integer :: i1,i2,i3,in,lda
148
      integer :: n_bands(0:DIMENSION%neigd),nslibd,nslibd_b
149
      character(len=8) :: dop,iop,name(10)
150
      real    :: bkpt(3),bkpt_b(3),sfp,tpi,wronk,phase
151
      complex :: c_phase
152 153
      real    :: eig(DIMENSION%neigd),eig_b(DIMENSION%neigd),cp_time(9)
      real    :: efermi
154 155 156 157 158 159
      logical :: l_p0,l_bkpts,l_proj,l_amn,l_mmn
!!! energy window boundaries
      integer, allocatable :: kveclo(:),nv(:)
      integer, allocatable :: kveclo_b(:),nv_b(:)
      integer, allocatable :: k1(:,:),k2(:,:),k3(:,:)
      integer, allocatable :: k1_b(:,:),k2_b(:,:),k3_b(:,:)
160
      INTEGER, ALLOCATABLE :: innerEig_idList(:)
161
      real,    allocatable :: we(:),we_b(:)
162

163
      real,    allocatable :: eigg(:)
164
      real kpoints(nkpt)
165 166 167 168 169 170 171
!!! a and b coeff. constructed for each k-point
      complex, allocatable :: acof(:,:,:),acof_b(:,:,:)
      complex, allocatable :: bcof(:,:,:),bcof_b(:,:,:)
      complex, allocatable :: ccof(:,:,:,:),ccof_b(:,:,:,:)
!!! the parameters for the number of wfs
      integer :: nwfs
!!! the potential in the spheres and the vacuum
172
      real, allocatable :: vr(:,:,:),vz(:,:,:)
173
!!! auxiliary potentials
174
      complex, allocatable :: vpw(:,:)
175 176 177 178
!!! bkpts data
      integer nntot,ikpt_help
      integer, allocatable :: gb(:,:,:),bpt(:,:)
!!! radial wavefunctions in the muffin-tins and more ...
179 180
      real,    allocatable :: flo(:,:,:,:,:),vso(:,:,:)
      real,    allocatable :: ff(:,:,:,:,:),gg(:,:,:,:,:)
181

182 183 184
      real     :: uuilon(atoms%nlod,atoms%ntype)
      real     :: duilon(atoms%nlod,atoms%ntype)
      real     :: ulouilopn(atoms%nlod,atoms%nlod,atoms%ntype)
185
   !!! the Mmn matrices
186 187
      complex, allocatable :: mmnk(:,:,:,:),mmn(:,:,:)           
      complex, allocatable :: amn(:,:,:),nablamat(:,:,:,:)        
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
      complex, allocatable :: soctomom(:,:,:,:)
      complex, allocatable :: surfcurr(:,:,:,:)
      complex, allocatable :: socmmn(:,:,:)
      complex, allocatable :: a(:)
      complex, allocatable :: psiw(:,:,:)
      complex, allocatable :: anglmom(:,:,:,:)
      complex, allocatable :: orbcomp(:,:,:,:,:)
c..wf-hamiltonian in real space (hopping in the same unit cell)
      complex, allocatable :: hwfr(:,:),hwfr2(:,:)
c      real, allocatable :: ei(:)
      complex, allocatable :: work(:)
      real,allocatable::centers(:,:,:)
      logical :: l_file
      logical :: l_amn2, l_conjugate
      character(len=3) :: spin12(2)
      data   spin12/'WF1' , 'WF2'/
      character(len=30)  :: task
      integer,allocatable::irreduc(:)
      integer,allocatable::mapkoper(:)
      integer :: fullnkpts,kpt,kptibz,kptibz_b,j1,j2,j3,oper,oper_b,k
      real :: bkrot(3),dirfacs(3)
      integer :: ios,kplot,kplotoper,plotoper,gfcut
      complex :: phasust
      integer,allocatable::pair_to_do(:,:)
212
      integer :: ngopr1(atoms%nat)
213
      integer,allocatable::maptopair(:,:,:)
214
      integer :: wannierspin,jspin2,jspin7,jspin2_b
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 241 242 243 244 245 246
      real, allocatable :: rwork(:)
      real,allocatable::kdiff(:,:)
      integer,allocatable :: shiftkpt(:,:)
      integer :: unigrid(6),gfthick
      complex,allocatable::ujug(:,:,:,:),ujdg(:,:,:,:)
      complex,allocatable::djug(:,:,:,:),djdg(:,:,:,:)
      complex,allocatable::ujulog(:,:,:,:,:)
      complex,allocatable::djulog(:,:,:,:,:)
      complex,allocatable::ulojug(:,:,:,:,:)
      complex,allocatable::ulojdg(:,:,:,:,:)
      complex,allocatable::ulojulog(:,:,:,:,:,:)
      real(4) delta,delta1,time_interstitial,time_mmn
      real(4) time_total,delta2,delta3
      real(4) time_lapw_expand,time_rw,time_symm,time_film
      real(4) time_lapw_plot,time_ujugaunt,time_abcof
      integer :: n_start,n_end,mlotot,mlolotot,err
      integer :: mlot_d,mlolot_d,ilo,dir,length
      complex :: ci
      character(len=2) :: spin012(0:2)
      data spin012/'  ', '.1', '.2'/
      character(len=6) :: filename
      real :: arg,hescale
      complex :: nsfactor,nsfactor_b,value
      real :: b1(3),b2(3)
      real,parameter :: bohrtocm=0.529177e-8
      real,parameter :: condquant=7.7480917e-5
      integer :: npotmatfile,ig3,maxvac,irec,imz,ivac,ipot
      logical :: l_orbcompinp
      integer :: num_angl
      complex,allocatable :: vxy(:,:,:)


247
c---->gwf
248 249 250

      ! FURTHER VARIABLES
      real :: qpt_i(3),qptb_i(3)
251 252
      real :: alph_i(atoms%ntype),alphb_i(atoms%ntype)
      real :: beta_i(atoms%ntype),betab_i(atoms%ntype)
253 254
      real :: theta_i, thetab_i, phi_i, phib_i
      real :: dalph,db1,db2,coph,siph
255
      real :: zero_taual(3,atoms%nat),bqpt(3)
256
      real :: tt1,tt2,tt3,tt4,tt5,tt6,t_it,t_kov,t_qov
257
      real :: eig_qb(DIMENSION%neigd)
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

      real,allocatable :: qdiff(:,:), we_qb(:)                
      real,allocatable :: energies(:,:,:)  
      real,allocatable :: zero_qdiff(:,:)


      integer,allocatable :: irreduc_q(:),mapqoper(:)        
      integer,allocatable :: shiftqpt(:,:),pair_to_do_q(:,:)  
      integer,allocatable :: maptopair_q(:,:,:)              
      integer,allocatable :: gb_q(:,:,:),bpt_q(:,:)         
      integer,allocatable :: k1_qb(:,:),k2_qb(:,:),k3_qb(:,:)
      integer,allocatable :: kveclo_qb(:),nv_qb(:)

      integer :: nntot_q = 1                               
      integer :: fullnqpts = 1                                
      integer :: funit_start = 5000
      integer :: qptibz, qptibz_b, oper_q, oper_qb
      integer :: qpt,iqpt_help, iqpt, iqpt_b
      integer :: nbands_qb, nmat_qb, nslibd_qb, noccbd_qb
      integer :: sign_q = 1,band_help
      integer :: doublespin,jspin_b,jspin3,jspin4,jspin5
      integer :: doublespin_max,nrec5
      integer :: count_i,count_j
      integer :: aoff,d1,d10,d100
      integer :: n1,n2,ii,jj

      complex :: interchi,vacchi,amnchi
      complex :: phasfac,phasfac2,cmplx_1                                 

      complex,allocatable :: chi(:)
      complex,allocatable :: acof_qb(:,:,:)                 
      complex,allocatable :: bcof_qb(:,:,:)                   
      complex,allocatable :: ccof_qb(:,:,:,:)                 
      complex,allocatable :: mmnk_q(:,:,:,:)                 
      complex,allocatable :: m_int(:,:,:,:)
      complex,allocatable :: m_sph(:,:,:,:)
      complex,allocatable :: m_vac(:,:,:,:)
      complex,allocatable :: ujug_q(:,:,:,:),ujdg_q(:,:,:,:) 
      complex,allocatable :: djug_q(:,:,:,:),djdg_q(:,:,:,:) 
      complex,allocatable :: ujulog_q(:,:,:,:,:)              
      complex,allocatable :: djulog_q(:,:,:,:,:)             
      complex,allocatable :: ulojug_q(:,:,:,:,:)             
      complex,allocatable :: ulojdg_q(:,:,:,:,:)             
      complex,allocatable :: ulojulog_q(:,:,:,:,:,:)         

      character(len=12) tmp_filename,fending
      character(len=30) fname,fstart,eigfile

      logical :: l_bqpts,l_gwf,l_nochi
307 308

      TYPE(t_usdus) :: usdus
309
      TYPE(t_zmat)  :: zMat, zzMat, zMat_b, zMat_qb
310 311
      TYPE(t_lapw)  :: lapw, lapw_b, lapw_qb
      TYPE(t_wann)  :: wannTemp
312

313 314
      eig_id = eig_idList(1)

315 316 317 318
c----<gwf


      aoff = iachar('1')-1
319 320 321
      d1  = mod(mpi%irank,10)
      IF (mpi%irank < 100) THEN
        d10 = int( (mpi%irank + 0.5)/10 )
322 323
        fstart = 'eig'//achar(d10+aoff)//achar(d1+aoff)
      ELSE
324 325
        d10 = mod((mpi%irank-d1)/10,10)
        d100 = (mpi%irank-10*d10-d1)/100
326 327 328 329 330 331
        IF ( d100.GE.10 ) d100 = d100 + iachar('7')
        fstart =
     +  'eig'//achar(d100+aoff)//achar(d10+aoff)//achar(d1+aoff)
      ENDIF


332 333 334 335 336 337 338 339 340 341
c-----initializations
      time_interstitial=0.0
      time_mmn=0.0
      time_total=0.0
      time_ujugaunt=0.0
      time_abcof=0.0
      time_rw=0.0
      time_symm=0.0
      time_film=0.0
      ngopr1(:)=1
342
      zero_taual = 0.0
343 344

      ci=cmplx(0.0,1.0)
345
      cmplx_1 = cmplx(1.0,0.0)
346 347 348 349

      call cpu_time(time_total)

      l_p0 = .false.
350
      if (mpi%irank.eq.0) l_p0 = .true.
351 352 353

      sfp = 2* sqrt( pimach() )
      tpi = 2* pimach()
354
      lmd = atoms%lmaxd*(atoms%lmaxd+2)
355 356

!!!   should be changed in case the windows are really used
357
      nkpts = nkpt
358

359 360
      ! do we have to construct GWF ?
      l_gwf = .false.
361
      l_gwf = wann%l_sgwf.or.wann%l_socgwf 
362 363 364 365 366 367
      
      l_nochi = .false.
      inquire(file='l_nochi',exist=l_nochi)
      if(l_gwf.and.l_p0) write(*,*)'disable chi trafo: ',l_nochi

      if(l_gwf.and.l_p0) call gwf_plottemplate()
368
      allocate( chi(atoms%ntype) )
369

370
c-----read the input file to determine what to do
371 372
      wann%atomlist_num=atoms%nat
      wann%oc_num_orbs=atoms%nat
373 374

      CALL wann_read_inp(input,l_p0,wann)
375

376

377
c-----input file for orbital decomposition
378 379 380 381 382 383 384 385 386 387 388
      if(wann%l_orbcomp.or.wann%l_orbcomprs)then
         inquire(file='orbcomp_inp',exist=l_orbcompinp)
         if(l_orbcompinp)then
            open(159,file='orbcomp_inp')
            read(159,*)wann%oc_num_orbs,wann%l_oc_f
            allocate(wann%oc_orbs(wann%oc_num_orbs))
            do n=1,wann%oc_num_orbs
               read(159,*)wann%oc_orbs(n)
            enddo
            close(159)
         else !default is all atoms including f
389
            wann%oc_num_orbs=atoms%nat
390 391 392 393 394 395 396 397 398
            wann%l_oc_f=.true.
            allocate(wann%oc_orbs(wann%oc_num_orbs))
            do n=1,wann%oc_num_orbs
               wann%oc_orbs(n)=n
            enddo          
         endif
      endif

      if(wann%l_updown)then            
399
         call wann_updown(
400
     >      mpi,input,sym,atoms,stars,vacuum,sphhar,oneD,noco,cell,vTot,
401
     >      enpara,eig_idList(1),l_real,
402
     >      mpi%mpi_comm,atoms%l_dulo,noco%l_noco,noco%l_ss,
403 404 405 406 407 408 409
     >      atoms%lmaxd,atoms%ntype,DIMENSION%neigd,atoms%nat,sym%nop,
     >      DIMENSION%nvd,DIMENSION%jspd,DIMENSION%nbasfcn,atoms%llod,
     >      atoms%nlod,atoms%ntype,cell%omtil,atoms%nlo,atoms%llo,
     >      atoms%lapw_l,sym%invtab,sym%mrot,atoms%ngopr,atoms%neq,
     >      atoms%lmax,atoms%invsat,sym%invsatnr,nkpt,atoms%taual,
     >      atoms%rmt,cell%amat,cell%bmat,cell%bbmat,noco%alph,
     >      noco%beta,noco%qss,                    ! TODO: adapt if needed
410 411
     >      stars%sk2,stars%phi2,oneD%odi,oneD%ods,mpi%irank,mpi%isize,
     >      stars%ng3,
412 413
     >      vacuum%nmzxyd,vacuum%nmzd,atoms%jmtd,sphhar%nlhd,stars%ng3,
     >      vacuum%nvac,sym%invs,sym%invs2,input%film,sphhar%nlh,
414
     >      atoms%jri,sphhar%ntypsd,atoms%ntypsy,input%jspins,nkpt,
415
     >      atoms%dx,stars%ng2,atoms%rmsh,sliceplot%e1s,sliceplot%e2s,
416 417
     >      atoms%ulo_der,stars%ustep,stars%ig,stars%mx1,
     >      stars%mx2,stars%mx3,stars%rgphs,
418 419 420 421 422 423 424
     >      sliceplot%slice,sliceplot%kk,sliceplot%nnne,cell%z1,
     >      DIMENSION%nv2d,vacuum%nmzxy,vacuum%nmz,vacuum%delz,
     >      stars%ig2,cell%area,sym%tau,atoms%zatom,stars%ng2,sym%nop2,
     >      cell%volint,sym%symor,atoms%pos,results%ef,noco%l_soc,
     >      sphhar%memd,atoms%lnonsph,sphhar%clnu,DIMENSION%lmplmd,
     >      sphhar%mlh,sphhar%nmem,sphhar%llh,atoms%lo1l,
     >      noco%theta,noco%phi,noco%soc_opt)
425 426 427 428 429

         DO pc = 1, wann%nparampts
            CALL close_eig(eig_idList(pc))
         END DO

430
         CALL juDFT_end("updown done",mpi%irank)
431 432
      endif

433 434 435 436 437
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  modern theory of orbital magnetization from Wannier functions
c  Jan-Philipp Hanke
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      if(wann%l_matrixuHu)then
438
         wannTemp = wann
439
         call wann_uHu(
440 441
     >      DIMENSION,stars,vacuum,atoms,sphhar,input,kpts,sym,mpi,
     >      banddos,oneD,noco,cell,vTot,wannTemp,eig_idList,
442 443 444 445 446 447 448
     >      l_real,atoms%l_dulo,noco%l_noco,noco%l_ss,atoms%lmaxd,
     >      atoms%ntype,DIMENSION%neigd,atoms%nat,sym%nop,DIMENSION%nvd,
     >      DIMENSION%jspd,DIMENSION%nbasfcn,atoms%llod,atoms%nlod,
     >      atoms%ntype,cell%omtil,atoms%nlo,atoms%llo,
     >      atoms%lapw_l,sym%invtab,sym%mrot,atoms%ngopr,atoms%neq,
     >      atoms%lmax,atoms%invsat,sym%invsatnr,nkpt,atoms%taual,
     >      atoms%rmt,cell%amat,cell%bmat,cell%bbmat,noco%alph,
449 450
     >      noco%beta,noco%qss,stars%sk2,stars%phi2,oneD%odi,oneD%ods,
     >      mpi%irank,
451 452 453
     >      mpi%isize,stars%ng3,vacuum%nmzxyd,vacuum%nmzd,atoms%jmtd,
     >      sphhar%nlhd,stars%ng3,vacuum%nvac,sym%invs,sym%invs2,
     >      input%film,sphhar%nlh,atoms%jri,sphhar%ntypsd,atoms%ntypsy,
454
     >      input%jspins,nkpt,atoms%dx,stars%ng2,atoms%rmsh,
455
     >      sliceplot%e1s,sliceplot%e2s,atoms%ulo_der,stars%ustep,
456 457
     >      stars%ig,stars%mx1,stars%mx2,stars%mx3,
     >      stars%rgphs,sliceplot%slice,
458 459 460 461 462 463 464
     >      sliceplot%kk,sliceplot%nnne,cell%z1,DIMENSION%nv2d,
     >      vacuum%nmzxy,vacuum%nmz,vacuum%delz,sym%zrfs,stars%ig2,
     >      cell%area,sym%tau,atoms%zatom,stars%ng2,stars%kv2,sym%nop2,
     >      cell%volint,sym%symor,atoms%pos,results%ef,noco%l_soc,
     >      sphhar%memd,atoms%lnonsph,sphhar%clnu,DIMENSION%lmplmd,
     >      sphhar%mlh,sphhar%nmem,sphhar%llh,atoms%lo1l,
     >      noco%theta,noco%phi,noco%soc_opt,
465 466 467
     >      wann%l_ms,wann%l_sgwf,wann%l_socgwf,wann%aux_latt_const,
     >      wann%param_file,wann%param_vec,wann%nparampts,
     >      wann%param_alpha,wann%l_dim)
468

469 470 471 472
         DO pc = 1, wann%nparampts
            CALL close_eig(eig_idList(pc))
         END DO

473
        CALL juDFT_end("wann_uHu done",mpi%irank)
474 475 476 477 478 479 480
      endif

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  modern theory of DMI from higher-dimensional Wannier functions
c  Jan-Philipp Hanke
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      if(wann%l_matrixuHu_dmi)then
481
         wannTemp = wann
482
         call wann_uHu_dmi(
483 484
     >      DIMENSION,stars,vacuum,atoms,sphhar,input,kpts,sym,mpi,
     >      banddos,oneD,noco,cell,vTot,wannTemp,eig_idList,
485 486 487 488 489 490 491
     >      l_real,atoms%l_dulo,noco%l_noco,noco%l_ss,atoms%lmaxd,
     >      atoms%ntype,DIMENSION%neigd,atoms%nat,sym%nop,DIMENSION%nvd,
     >      DIMENSION%jspd,DIMENSION%nbasfcn,atoms%llod,atoms%nlod,
     >      atoms%ntype,cell%omtil,atoms%nlo,atoms%llo,
     >      atoms%lapw_l,sym%invtab,sym%mrot,atoms%ngopr,atoms%neq,
     >      atoms%lmax,atoms%invsat,sym%invsatnr,nkpt,atoms%taual,
     >      atoms%rmt,cell%amat,cell%bmat,cell%bbmat,noco%alph,
492 493
     >      noco%beta,noco%qss,stars%sk2,stars%phi2,oneD%odi,oneD%ods,
     >      mpi%irank,
494 495 496
     >      mpi%isize,stars%ng3,vacuum%nmzxyd,vacuum%nmzd,atoms%jmtd,
     >      sphhar%nlhd,stars%ng3,vacuum%nvac,sym%invs,sym%invs2,
     >      input%film,sphhar%nlh,atoms%jri,sphhar%ntypsd,atoms%ntypsy,
497
     >      input%jspins,nkpt,atoms%dx,stars%ng2,atoms%rmsh,
498
     >      sliceplot%e1s,sliceplot%e2s,atoms%ulo_der,stars%ustep,
499 500
     >      stars%ig,stars%mx1,stars%mx2,stars%mx3,
     >      stars%rgphs,sliceplot%slice,
501 502 503 504 505 506 507
     >      sliceplot%kk,sliceplot%nnne,cell%z1,DIMENSION%nv2d,
     >      vacuum%nmzxy,vacuum%nmz,vacuum%delz,sym%zrfs,stars%ig2,
     >      cell%area,sym%tau,atoms%zatom,stars%ng2,stars%kv2,sym%nop2,
     >      cell%volint,sym%symor,atoms%pos,results%ef,noco%l_soc,
     >      sphhar%memd,atoms%lnonsph,sphhar%clnu,DIMENSION%lmplmd,
     >      sphhar%mlh,sphhar%nmem,sphhar%llh,atoms%lo1l,
     >      noco%theta,noco%phi,noco%soc_opt,
508 509 510
     >      wann%l_ms,wann%l_sgwf,wann%l_socgwf,wann%aux_latt_const,
     >      wann%param_file,wann%param_vec,wann%nparampts,
     >      wann%param_alpha,wann%l_dim,l_nochi)
511

512 513 514 515 516
         DO pc = 1, wann%nparampts
            CALL close_eig(eig_idList(pc))
         END DO

         CALL juDFT_end("wann_uHu dmi done",mpi%irank)
517 518
      endif

519 520 521 522 523 524 525 526 527
      if(wann%l_byenergy.and.wann%l_byindex) CALL juDFT_error
     +     ("byenergy.and.byindex",calledby ="wannier")
      if(wann%l_byenergy.and.wann%l_bynumber) CALL juDFT_error
     +     ("byenergy.and.bynumber",calledby ="wannier")
      if(wann%l_bynumber.and.wann%l_byindex) CALL juDFT_error
     +     ("bynumber.and.byindex",calledby ="wannier")
      if(.not.(wann%l_bynumber.or.wann%l_byindex.or.wann%l_byenergy))
     &     CALL juDFT_error("no rule to sort bands",calledby ="wannier")

528

529
      efermi=results%ef
530 531 532
      if(.not.wann%l_fermi)efermi=0.0

#ifdef CPP_MPI
533
         call MPI_BARRIER(mpi%mpi_comm,ierr)
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
#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)
         if(.not.l_file)  CALL juDFT_error
     +        ("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
         if(fullnkpts<nkpts) CALL juDFT_error("fullnkpts.lt.nkpts"
     +        ,calledby ="wannier")
         allocate(irreduc(fullnkpts),mapkoper(fullnkpts))
         allocate(shiftkpt(3,fullnkpts))
         l_file=.false.
         inquire(file='kptsmap',exist=l_file)
         if(.not.l_file)  CALL juDFT_error
     +        ("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)
            if(kpt/=i) CALL juDFT_error("kpt.ne.i",calledby ="wannier")
            if(l_p0)print*,i,irreduc(i),mapkoper(i)
         enddo
         close(713)
         if(maxval(irreduc(:))/=nkpts) CALL juDFT_error
     +        ("max(irreduc(:))/=nkpts",calledby ="wannier")
      else
         fullnkpts=nkpts
567 568
         allocate(irreduc(fullnkpts),mapkoper(fullnkpts))
         allocate(shiftkpt(3,fullnkpts))
569 570
      endif

571

572
      if(l_gwf) fullnqpts = wann%nparampts
573 574


575 576 577
      nrec = 0
      if(l_p0)then
      write (*,*) 'fermi energy:',efermi
578 579
      write (*,*) 'emin,emax=',sliceplot%e1s,sliceplot%e2s
      write (*,*) 'nbasfcn =',DIMENSION%nbasfcn
580 581
      endif
      nlotot = 0
582 583 584
      do n = 1, atoms%ntype
        do l = 1,atoms%nlo(n)
          nlotot = nlotot + atoms%neq(n) * ( 2*atoms%llo(l,n) + 1 )
585 586 587 588 589 590 591 592
        enddo
      enddo

      if((.not.wann%l_matrixmmn).and.(.not.wann%l_wann_plot).and.
     &   (.not.wann%l_matrixamn).and.(.not.wann%l_projmethod).and.
     &   (.not.wann%l_bestproj).and.(.not.wann%l_nabla).and.
     &   (.not.wann%l_mmn0).and.(.not.wann%l_surfcurr).and.
     &   (.not.wann%l_offdiposop).and.(.not.wann%l_anglmom).and.
593 594
     &   (.not.wann%l_orbcomp).and.(.not.wann%l_perturb) .and.
     &   (.not.wann%l_finishgwf) ) goto 1911
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

c**********************************************************
ccccccccccccccc   read in the bkpts file  ccccccccccccccccc
c**********************************************************
      if (wann%l_matrixmmn) then ! for Omega functional minimization
       l_bkpts = .false.
       inquire (file='bkpts',exist=l_bkpts)
       if (.not.l_bkpts)  CALL juDFT_error("need bkpts for matrixmmn"
     +      ,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)
         if (ikpt/=ikpt_help)  CALL juDFT_error("ikpt.ne.ikpt_help"
     +        ,calledby ="wannier")
619
         if (bpt(nn,ikpt)>fullnkpts) CALL juDFT_error("bpt.gt.fullnkpts"
620 621 622 623 624 625
     +        ,calledby ="wannier")
        enddo
       enddo
       close (202)
        allocate(kdiff(3,nntot))
      endif
626 627 628 629

c**********************************************************
ccccccccccccccc   read in the bqpts file  ccccccccccccccccc         
c**********************************************************
630
      if ((wann%l_matrixmmn).AND.(l_gwf.or.wann%l_ms)) then
631 632
       l_bqpts = .false.
       inquire (file='bqpts',exist=l_bqpts)
633
       if (.not.l_bqpts)  CALL juDFT_error("need bqpts for matrixmmn"
634 635 636 637 638 639 640 641 642 643 644 645 646 647
     +     ,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)
648
         if (iqpt/=iqpt_help)  CALL juDFT_error("iqpt.ne.iqpt_help"
649 650
     +        ,calledby ="wannier")
         if (bpt_q(nn,iqpt)>fullnqpts)
651
     &        CALL juDFT_error("bpt_q.gt.fullnqpts",calledby ="wannier")
652 653 654 655 656 657 658 659 660 661 662 663 664
        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
665 666
      if(wann%l_sgwf) gb_q = 2*gb_q    
      if(wann%l_socgwf) gb_q = 2*gb_q 
667 668

      if(wann%l_finishgwf) goto 9110
669 670 671 672 673 674 675
c********************************************************
c      find symmetry-related elements in mmkb
c********************************************************
      if(wann%l_matrixmmn)then
         call cpu_time(delta)
         allocate(maptopair(3,fullnkpts,nntot))
         allocate(pair_to_do(fullnkpts,nntot))
676
         call wann_mmnk_symm(input,kpts,
677
     >     fullnkpts,nntot,bpt,gb,wann%l_bzsym,
678
     >     irreduc,mapkoper,l_p0,input%film,sym%nop,sym%invtab,sym%mrot,
679 680
     >     oneD%odi%d1,sym%tau,
     <     pair_to_do,maptopair,kdiff,.false.,wann%param_file)
681 682 683
         call cpu_time(delta1)
         time_symm=delta1-delta
      endif
684 685 686 687 688

      ! do the same for q-points to construct GWFs
      if(wann%l_matrixmmn.AND.l_gwf)then 
         allocate(maptopair_q(3,fullnqpts,nntot_q))
         allocate(pair_to_do_q(fullnqpts,nntot_q))
689
         call wann_mmnk_symm(input,kpts,
690
     >     fullnqpts,nntot_q,bpt_q,gb_q,wann%l_bzsym,
691 692
     >     irreduc_q,mapqoper,l_p0,.false.,1,sym%invtab(1),
     >     sym%mrot(:,:,1),.false.,sym%tau,
693
     <     pair_to_do_q,maptopair_q,qdiff,.true.,wann%param_file)
694 695 696
      endif


697 698 699 700
c*********************************************************
cccccccccccccccc   initialize the potential   cccccccccccc
c*********************************************************

701 702 703
      allocate ( vz(vacuum%nmzd,2,4) )
      allocate ( vr(atoms%jmtd,atoms%ntype,DIMENSION%jspd) )
      allocate ( vso(atoms%jmtd,atoms%nat,2) )
704

705 706
      vz = 0.0
      vz(:,:,:SIZE(vTot%vacz,3)) = vTot%vacz(:,:,:)
707

708 709 710
      do jspin = 1,input%jspins
        do n = 1, atoms%ntype
          do j = 1,atoms%jri(n)
711
            vr(j,n,jspin) = vTot%mt(j,0,n,jspin)
712 713 714 715 716
          enddo
        enddo
      enddo

      if(wann%l_soctomom)then
717
        CALL vsoc(input,atoms,vr,enpara%el0,.TRUE., vso)
718 719
      endif

720
      if(noco%l_noco.and.input%film)then
721
         npotmatfile=25
722
         allocate(vpw(stars%ng3,1))
723
         if(.not.oneD%odi%d1)
724
     &    allocate( vxy(vacuum%nmzxyd,stars%ng2-1,2) )
725 726 727

         OPEN (npotmatfile,FILE='potmat',FORM='unformatted',
     +                  STATUS='old')
728 729 730
         READ (npotmatfile) (vpw(ig3,1),ig3=1,stars%ng3)
         READ (npotmatfile) (vpw(ig3,1),ig3=1,stars%ng3)
         READ (npotmatfile) (vpw(ig3,1),ig3=1,stars%ng3)
731
         maxvac=2
732
         if(oneD%odi%d1)maxvac=1
733 734 735 736
         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
737
            IF ((ivac.EQ.2) .AND. (vacuum%nvac.EQ.1)) THEN
738 739 740 741 742
               DO irec = 1,4
                  BACKSPACE (npotmatfile)
               ENDDO
            ENDIF
c--->       load the non-warping part of the potential
743 744
            READ (npotmatfile)
     +         ((vz(imz,ivac,ipot),imz=1,vacuum%nmzd),ipot=1,4)
745

746
            if(.not.oneD%odi%d1)then
747
            DO ipot = 1,3
748 749
               READ (npotmatfile)((vxy(imz,igvm2,ivac),
     +            imz=1,vacuum%nmzxy),igvm2=1,stars%ng2-1)
750 751 752 753 754
            enddo    
            endif   
         enddo   
         CLOSE (npotmatfile)
         deallocate(vpw)
755
         if(.not.oneD%odi%d1)then
756 757
            deallocate(vxy)
         endif
758
      endif   
759
    
760
cccccccccccccccc   end of the potential part  ccccccccccc
761 762
      wannierspin=DIMENSION%jspd
      if(noco%l_soc) wannierspin=2
763
     
764 765
      allocate ( kveclo(nlotot),nv(wannierspin) )
      allocate ( kveclo_b(nlotot),nv_b(wannierspin) )
766
      IF(l_gwf) allocate ( kveclo_qb(nlotot),nv_qb(wannierspin) )
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
      allocate ( k1(DIMENSION%nvd,wannierspin) )
      allocate ( k2(DIMENSION%nvd,wannierspin) )
      allocate ( k3(DIMENSION%nvd,wannierspin) )
      allocate ( k1_b(DIMENSION%nvd,wannierspin) )
      allocate ( k2_b(DIMENSION%nvd,wannierspin) )
      allocate ( k3_b(DIMENSION%nvd,wannierspin) )
      IF(l_gwf) THEN
         allocate ( k1_qb(DIMENSION%nvd,wannierspin) )
         allocate ( k2_qb(DIMENSION%nvd,wannierspin) )
         allocate ( k3_qb(DIMENSION%nvd,wannierspin) )
      END IF

      allocate ( ff(atoms%ntype,atoms%jmtd,2,0:atoms%lmaxd,2) )
      allocate ( gg(atoms%ntype,atoms%jmtd,2,0:atoms%lmaxd,2) )
      allocate ( usdus%us(0:atoms%lmaxd,atoms%ntype,2) )
      allocate ( usdus%uds(0:atoms%lmaxd,atoms%ntype,2) )
      allocate ( usdus%dus(0:atoms%lmaxd,atoms%ntype,2) )
      allocate ( usdus%duds(0:atoms%lmaxd,atoms%ntype,2) )
      allocate ( usdus%ddn(0:atoms%lmaxd,atoms%ntype,2) )
      allocate ( usdus%ulos(atoms%nlod,atoms%ntype,2) )
      allocate ( usdus%dulos(atoms%nlod,atoms%ntype,2) )
      allocate ( usdus%uulon(atoms%nlod,atoms%ntype,2) )
      allocate ( usdus%dulon(atoms%nlod,atoms%ntype,2) )
      allocate ( usdus%uloulopn(atoms%nlod,atoms%nlod,atoms%ntype,2) )
791 792 793 794 795 796 797 798

      if(l_gwf.and..not.(wann%l_wann_plot)) then
         doublespin_max=4!2
      else
         doublespin_max=wannierspin
      endif

      t_it=0.0
799
!      t_kov=0.0
800 801 802 803
      t_qov=0.0
c*****************************************************************c
c                         START Q LOOP                            c
c   standard functionality of code for fullnqpts = nntot_q = 1    c
804
c        and wann%l_ms = wann%l_sgwf = wann%l_socgwf = F          c
805 806
c*****************************************************************c
      do 314 iqpt = 1,fullnqpts  ! loop by q-points starts
807

808
       ALLOCATE(innerEig_idList(nntot_q))
809

810
       call cpu_time(tt1)
811
!       t_kov=0.0
812 813 814 815 816 817
       t_qov=0.0

        qptibz=iqpt                          
        if(wann%l_bzsym .AND. l_gwf) qptibz=irreduc_q(iqpt)
        if(wann%l_bzsym .AND. l_gwf) oper_q=mapqoper(iqpt)

818 819 820 821 822
       qpt_i = noco%qss
       alph_i = noco%alph
       beta_i = noco%beta
       theta_i = noco%theta
       phi_i = noco%phi
823 824 825 826 827 828
       if(wann%l_sgwf.or.wann%l_ms) then
          qpt_i(:) = wann%param_vec(:,qptibz)
          alph_i(:) = wann%param_alpha(:,qptibz)
       elseif(wann%l_socgwf) then 
          if(wann%l_dim(2)) phi_i = tpi*wann%param_vec(2,qptibz)
          if(wann%l_dim(3)) theta_i = tpi*wann%param_vec(3,qptibz)
829 830 831 832 833
       endif

       IF (l_gwf) THEN
        IF(wann%l_matrixmmn)THEN
          do iqpt_b=1,nntot_q
834 835 836 837 838 839 840 841 842 843 844 845 846 847

            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,
!     +                  noco%l_noco,.FALSE.,l_real,noco%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)
848

849 850
          enddo
        ENDIF
851 852 853 854 855 856 857 858 859 860 861 862 863

        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,
!     +                  noco%l_noco,.FALSE.,l_real,noco%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)
864

865
       ELSEIF(wann%l_ms) THEN
866 867 868 869 870 871 872 873 874 875 876 877 878

        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,
!     +                  noco%l_noco,.FALSE.,l_real,noco%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)
879

880 881
       ELSE
          fending=''
882
       ENDIF ! l_gwf.or.wann%l_ms
883 884 885 886
       nrec=0
       nrec_b=0


887 888 889
c****************************************************
c cycle by spins starts! 
c****************************************************
890 891 892 893 894 895 896 897 898
      do 110 doublespin=1,doublespin_max   ! cycle by spins

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

         nrec_b = nrec

899
         if(.not.noco%l_noco) then
900 901 902 903 904 905 906 907
            nrec = (jspin-1)*nkpts
            nrec_b = (jspin_b-1)*nkpts
         endif

       ! spin-dependent sign of the q-dependent phase
       ! in the generalized Bloch theorem
       ! -1: spin up, +1: spin down
       sign_q = -sign_q
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

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
         CALL juDFT_error("no proj/proj.1/proj.2"
     &             ,calledby ="wannier")
       endif  


       jspin2=jspin
935
       if(noco%l_soc .and. input%jspins.eq.1)jspin2=1
936
       jspin2_b=jspin_b
937
       if(noco%l_soc .and. input%jspins.eq.1)jspin2_b=1
938

939 940
       jsp_start = jspin ; jsp_end = jspin

941
       call cpu_time(tt2)
942
cccccccccccc   read in the eigenvalues and vectors   cccccc
943 944 945 946 947
       write(*,*)'wannierspin',wannierspin
       do jspin5=1,wannierspin!1!2
!       jspin5=jspin
       jsp_start=jspin5; jsp_end=jspin5
       nrec5=0
948
       if(.not.noco%l_noco) nrec5 = (jspin5-1)*nkpts
949

950 951
       call cdn_read0(eig_id,mpi%irank,mpi%isize,jspin5,DIMENSION%jspd, !wannierspin instead of DIMENSION%jspd?
     >                noco%l_noco,
952
     <                n_bands,n_size)
953

954 955 956
       enddo
!       call cpu_time(tt3)
       !if(l_p0) write(*,*)'cdn_read0=',tt3-tt2
957

958
c..   now we want to define the maximum number of the bands by all kpts
959 960 961 962 963
      nbnd = 0

      i_rec = 0 ; n_rank = 0
c*************************************************************
c..writing down the eig.1 and/or eig.2 files
964

965
c..write individual files if multi-spiral mode wann%l_ms=T
966
c*************************************************************
967 968
      if(l_p0)then         
         call wann_write_eig(
969
     >            eig_id,l_real,
970 971 972 973
     >            atoms%lmaxd,atoms%ntype,atoms%nlod,DIMENSION%neigd,
     >            DIMENSION%nvd,wannierspin,
     >            mpi%isize,jspin,DIMENSION%nbasfcn,nlotot,
     >            noco%l_ss,noco%l_noco,nrec,fullnkpts,
974 975
     >            wann%l_bzsym,wann%l_byindex,wann%l_bynumber,
     >            wann%l_byenergy,
976
     >            irreduc,oneD%odi,wann%band_min(jspin),
977 978
     >            wann%band_max(jspin),
     >            numbands,
979
     >            sliceplot%e1s,sliceplot%e2s,efermi,.false.,nkpts,
980 981
     <            nbnd,kpoints,l_gwf,iqpt)       

982
         if(oneD%odi%d1)then
983
            kpoints(:)=kpoints(:)*cell%bmat(3,3)         
984 985
         endif
      endif!l_p0
986 987
!      call cpu_time(tt2)
      !if(l_p0)write(*,*)'write_eig=',tt2-tt3
988

989
     
990 991 992
! nbnd is calculated for process zero and is sent here to the others
#ifdef CPP_MPI
      if(l_p0)then
993 994
         do cpu_index=1,mpi%isize-1
      call MPI_SEND(nbnd,1,MPI_INTEGER,cpu_index,1,mpi%mpi_comm,ierr)
995 996
         enddo
      else
997
       call MPI_RECV(nbnd,1,MPI_INTEGER,0,1,mpi%mpi_comm,stt,ierr)
998 999
      endif
#endif
1000
     
1001
       print*,"process: ",mpi%irank," nbnd= ",nbnd
1002
!       call cpu_time(tt3)
1003 1004 1005 1006
c##################################################################
      if(wann%l_mmn0)then
           allocate ( mmn(nbnd,nbnd,fullnkpts) )
           mmn(:,:,:) = cmplx(0.,0.)
1007
           if((noco%l_soc.or.noco%l_noco) .and. (doublespin.eq.1))
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
     &          allocate(socmmn(nbnd,nbnd,fullnkpts) )
      endif     
      if(wann%l_nabla)then
         allocate ( nablamat(3,nbnd,nbnd,fullnkpts) )
         nablamat = cmplx(0.,0.)
      endif

      if(wann%l_soctomom)then
         allocate ( soctomom(3,nbnd,nbnd,fullnkpts) )
         soctomom = cmplx(0.,0.)
      endif

      if(wann%l_surfcurr)then
         allocate ( surfcurr(3,nbnd,nbnd,fullnkpts) )
         surfcurr = cmplx(0.,0.)
      endif

      if(wann%l_anglmom)then
        if(.not.allocated(anglmom))then  
          allocate ( anglmom(3,nbnd,nbnd,fullnkpts) )
          anglmom=cmplx(0.,0.)
        endif  
      endif

      if(wann%l_orbcomp)then
         if(allocated(orbcomp))deallocate(orbcomp)
         if(wann%l_oc_f)then
            allocate(orbcomp(16,wann%oc_num_orbs,nbnd,nbnd,fullnkpts))
         else
            allocate(orbcomp(9,wann%oc_num_orbs,nbnd,nbnd,fullnkpts))
         endif
         orbcomp=cmplx(0.,0.)
      endif

1042
      !write (*,*) 'nwfs=',nwfs
1043 1044 1045 1046 1047
      if(wann%l_projmethod.or.wann%l_bestproj.or.wann%l_matrixamn)then
        if(.not.allocated(amn))then
          allocate ( amn(nbnd,nwfs,fullnkpts) )
          amn(:,:,:) = cmplx(0.,0.)
        endif
1048
      endif
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

      if (wann%l_projmethod.or.wann%l_bestproj) then
        allocate ( psiw(nbnd,nwfs,fullnkpts) )
        psiw(:,:,:) = cmplx(0.,0.)
        if(.not.allocated(hwfr))then
           allocate ( hwfr(nwfs,nwfs) )
           hwfr(:,:) = cmplx(0.,0.)
        endif
      endif


      if (wann%l_matrixmmn) then
1061
       if(.not.allocated(mmnk))then
1062 1063 1064 1065 1066
         allocate ( mmnk(nbnd,nbnd,nntot,fullnkpts) )
         mmnk = (0.,0.)
       endif  
      endif

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
      if(wann%l_matrixmmn)then
         if(.not.allocated(mmnk_q).AND.l_gwf)then
             allocate ( mmnk_q (nbnd,nbnd,nntot_q,fullnkpts) )
             mmnk_q = (0.,0.)

!             allocate ( m_int(nbnd,nbnd,nntot_q,fullnkpts) )
!             allocate ( m_sph(nbnd,nbnd,nntot_q,fullnkpts) )
!             allocate ( m_vac(nbnd,nbnd,nntot_q,fullnkpts) )
!             m_int = cmplx(0.,0.)
!             m_sph = cmplx(0.,0.)
!             m_vac = cmplx(0.,0.)
         endif
      endif

 
1082
      allocate ( flo(atoms%ntype,atoms%jmtd,2,atoms%nlod,2) )
1083 1084
!      call cpu_time(tt2)
      !if(l_p0) write(*,*)'allocate',tt2-tt3
1085

1086 1087
      do jspin4=1,wannierspin!2
         jspin3=jspin4
1088
         if(input%jspins.eq.1) jspin3=1
1089
      na = 1
1090 1091
      do 40 n = 1,atoms%ntype
       do 30 l = 0,atoms%lmax(n)
1092
c...compute the l-dependent, k-independent radial MT- basis functions
1093

1094
         call radfun(
1095
     >           l,n,jspin4,enpara%el0(l,n,jspin3),vr(1,n,jspin3),atoms,
1096 1097 1098
     <              ff(n,:,:,l,jspin4),gg(n,:,:,l,jspin4),usdus,
     <              nodeu,noded,wronk)

1099 1100
   30  continue
c...and the local orbital radial functions
1101
       do ilo = 1, atoms%nlo(n)
1102

1103
         call radflo(
1104
     >           atoms,n,jspin4,enpara%ello0(:,:,jspin3),vr(1,n,jspin3),
1105 1106 1107
     >             ff(n,1:,1:,0:,jspin4),gg(n,1:,1:,0:,jspin4),mpi,
     <             usdus,uuilon,duilon,ulouilopn,flo(n,:,:,:,jspin4))

1108
       enddo
1109
c       na = na + atoms%neq(n)
1110
   40 continue
1111 1112 1113 1114
      enddo!jspin3
!      call cpu_time(tt3)
      !if(l_p0) write(*,*)'radfun=',tt3-tt2

1115 1116 1117 1118
c****************************************************************
c   calculate the k-independent uju*gaunt-matrix needed for
c   mmnmatrix
c****************************************************************
1119 1120
! TODO: make this more efficient (i.e., compute ujugaunt only once
! and not for all q-points).
1121 1122 1123
      if(wann%l_matrixmmn)then
         call cpu_time(delta)
         allocate(ujug(0:lmd,0:lmd,
1124
     &              1:atoms%ntype,1:nntot))
1125
         allocate(ujdg(0:lmd,0:lmd,
1126
     &              1:atoms%ntype,1:nntot))
1127
         allocate(djug(0:lmd,0:lmd,
1128
     &              1:atoms%ntype,1:nntot))
1129
         allocate(djdg(0:lmd,0:lmd,
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
     &              1:atoms%ntype,1:nntot))
         allocate(ujulog(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot))
         allocate(djulog(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot))
         allocate(ulojug(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot))
         allocate(ulojdg(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot))
         allocate(ulojulog(1:atoms%nlod,-atoms%llod:atoms%llod,
     &                     1:atoms%nlod,-atoms%llod:atoms%llod,
     &                     1:atoms%ntype,1:nntot))
1142

1143
         call wann_ujugaunt(
1144 1145 1146
     >            atoms%llod,nntot,kdiff,atoms%lmax,atoms%ntype,
     >            atoms%ntype,cell%bbmat,cell%bmat,atoms%nlod,atoms%nlo,
     >            atoms%llo,flo(:,:,:,:,jspin),
1147 1148 1149 1150
     >            flo(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin),
     >            gg(:,:,:,:,jspin),
1151 1152
     >            gg(:,:,:,:,jspin),atoms%jri,atoms%rmsh,atoms%dx,
     >            atoms%jmtd,atoms%lmaxd,lmd,
1153
     <            ujug,ujdg,djug,djdg,
1154 1155 1156 1157 1158 1159 1160 1161
     <            ujulog,djulog,ulojug,ulojdg,ulojulog,.false.,1)

! compute integrals of radial solution, according energy derivatives,
! the spherical Bessel function and the Gaunt coefficients in order
! to account for the overlap of the lattice periodic parts at
! neighboring q-points
         IF(l_gwf)THEN
            allocate(ujug_q(0:lmd,0:lmd,
1162
     &              1:atoms%ntype,1:nntot_q))
1163
            allocate(ujdg_q(0:lmd,0:lmd,
1164
     &              1:atoms%ntype,1:nntot_q))
1165
            allocate(djug_q(0:lmd,0:lmd,
1166
     &              1:atoms%ntype,1:nntot_q))
1167
            allocate(djdg_q(0:lmd,0:lmd,
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
     &              1:atoms%ntype,1:nntot_q))
            allocate(ujulog_q(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot_q))
            allocate(djulog_q(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot_q))
            allocate(ulojug_q(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot_q))
            allocate(ulojdg_q(0:lmd,1:atoms%nlod,-atoms%llod:atoms%llod,
     &              1:atoms%ntype,1:nntot_q))
            allocate(ulojulog_q(1:atoms%nlod,-atoms%llod:atoms%llod,
     &                          1:atoms%nlod,-atoms%llod:atoms%llod,
     &                          1:atoms%ntype,1:nntot_q))
1180 1181 1182
            
            ! we need G(q+b)/2 as argument for the sph. Bessel func.
            ! and additionally a spin-dependent sign (-/+ 1)^{lpp}
1183
            if(wann%l_sgwf) call wann_ujugaunt(
1184 1185 1186
     >            atoms%llod,nntot_q,qdiff/2.0,atoms%lmax,atoms%ntype,
     >            atoms%ntype,cell%bbmat,cell%bmat,atoms%nlod,atoms%nlo,
     >            atoms%llo,flo(:,:,:,:,jspin),
1187 1188 1189 1190
     >            flo(:,:,:,:,jspin_b),
     >            ff(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin_b),
     >            gg(:,:,:,:,jspin),
1191 1192
     >            gg(:,:,:,:,jspin_b),atoms%jri,atoms%rmsh,atoms%dx,
     >            atoms%jmtd,atoms%lmaxd,lmd,
1193 1194 1195 1196
     <            ujug_q,ujdg_q,djug_q,djdg_q,
     <            ujulog_q,djulog_q,ulojug_q,ulojdg_q,ulojulog_q,.true.,
     >            sign_q)

1197
            if(wann%l_socgwf) call wann_ujugaunt(
1198 1199 1200
     >            atoms%llod,nntot_q,zero_qdiff,atoms%lmax,atoms%ntype,
     >            atoms%ntype,cell%bbmat,cell%bmat,atoms%nlod,atoms%nlo,
     >            atoms%llo,flo(:,:,:,:,jspin),
1201 1202 1203 1204
     >            flo(:,:,:,:,jspin_b),
     >            ff(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin_b),
     >            gg(:,:,:,:,jspin),
1205 1206 1207
     >            gg(:,:,:,:,jspin_b),atoms%jri,atoms%rmsh,atoms%dx,
     >            atoms%jmtd,
     >            atoms%lmaxd,lmd,ujug_q,ujdg_q,djug_q,djdg_q,
1208 1209 1210 1211 1212
     <            ujulog_q,djulog_q,ulojug_q,ulojdg_q,ulojulog_q,
     >            .false.,1)
      
         ENDIF ! l_gwf

1213 1214
         call cpu_time(delta1)
         time_ujugaunt=delta1-delta
1215 1216 1217
      endif !l_matrixmmn
!      call cpu_time(tt2)
      !if(l_p0) write(*,*)'ujugaunt=',tt2-tt3
1218

1219 1220 1221 1222
      zzMat%l_real = l_real
      zzMat%nbasfcn = DIMENSION%nbasfcn
      zzMat%nbands = DIMENSION%neigd
      IF(l_real) THEN
1223 1224
	    IF(.not.ALLOCATED(zzMat%z_r))
     >    ALLOCATE (zzMat%z_r(zzMat%nbasfcn,zzMat%nbands))
1225
      ELSE
1226 1227
	    IF(.not.ALLOCATED(zzMat%z_c))
     >    ALLOCATE (zzMat%z_c(zzMat%nbasfcn,zzMat%nbands))
1228 1229 1230 1231 1232 1233
      END IF

      zMat%l_real = zzMat%l_real
      zMat%nbasfcn = zzMat%nbasfcn
      zMat%nbands = zzMat%nbands
      IF (zzMat%l_real) THEN
1234 1235
	    IF(.not.ALLOCATED(zMat%z_r))
     >    ALLOCATE (zMat%z_r(zMat%nbasfcn,zMat%nbands))
1236 1237
         zMat%z_r = 0.0
      ELSE
1238 1239
	    IF(.not.ALLOCATED(zMat%z_c))
     >    ALLOCATE (zMat%z_c(zMat%nbasfcn,zMat%nbands))
1240 1241 1242 1243 1244 1245 1246
         zMat%z_c = CMPLX(0.0,0.0)
      END IF

      zMat_b%l_real = zzMat%l_real
      zMat_b%nbasfcn = zzMat%nbasfcn
      zMat_b%nbands = zzMat%nbands
      IF (zzMat%l_real) THEN
1247 1248
	    IF(.not.ALLOCATED(zMat_b%z_r))
     >    ALLOCATE (zMat_b%z_r(zMat_b%nbasfcn,zMat_b%nbands))
1249 1250
         zMat_b%z_r = 0.0
      ELSE
1251 1252
	    IF(.not.ALLOCATED(zMat_b%z_c))
     >    ALLOCATE (zMat_b%z_c(zMat_b%nbasfcn,zMat_b%nbands))
1253 1254 1255
         zMat_b%z_c = CMPLX(0.0,0.0)
      END IF

1256
      i_rec = 0 ; n_rank = 0
1257
      
1258 1259 1260 1261
c****************************************************************
c.. loop by kpoints starts!      each may be a separate task
c****************************************************************
      do 10 ikpt = wann%ikptstart,fullnkpts  ! loop by k-points starts
1262 1263
!         call cpu_time(tt2)
!         call cpu_time(tt3)
1264 1265 1266 1267 1268
        kptibz=ikpt
        if(wann%l_bzsym) kptibz=irreduc(ikpt)
        if(wann%l_bzsym) oper=mapkoper(ikpt)

        i_rec = i_rec + 1
1269
      if (mod(i_rec-1,mpi%isize).eq.mpi%irank) then
1270

1271
      allocate ( we(DIMENSION%neigd),eigg(DIMENSION%neigd) )
1272 1273 1274

      call cpu_time(delta)
      n_start=1
1275
      n_end=DIMENSION%neigd
1276 1277 1278 1279 1280


! read information of diagonalization for fixed q-point iqpt
! stored in the eig file on unit 66. the lattice respectively
! plane-wave vectors G(k,q) are saved in (k1,k2,k3).
1281 1282 1283

      CALL cdn_read(
     >              eig_id,
1284 1285 1286
     >              DIMENSION%nvd,DIMENSION%jspd,mpi%irank,mpi%isize, !wannierspin instead of DIMENSION%jspd?
     >              kptibz,jspin,DIMENSION%nbasfcn,
     >              noco%l_ss,noco%l_noco,DIMENSION%neigd,n_start,n_end,
1287
     <              nbands,eigg,zzMat)
1288

1289 1290
!      call cpu_time(tt4)
      !if(l_p0) write(*,*)'cdn_read=',tt4-tt3
1291 1292 1293 1294 1295 1296 1297 1298 1299

      call cpu_time(delta1)
      time_rw=time_rw+delta1-delta
      nslibd = 0

c...we work only within the energy window

      eig(:) = 0.

1300
!      print*,"bands used:"
1301 1302

      do i = 1,nbands
1303 1304 1305 1306 1307
        if ((eigg(i).ge.sliceplot%e1s.and.nslibd.lt.numbands.and.
     &       wann%l_bynumber).or.
     &      (eigg(i).ge.sliceplot%e1s.and.eigg(i).le.sliceplot%e2s.and.
     &       wann%l_byenergy).or.(i.ge.wann%band_min(jspin).and.
     &      (i.le.wann%band_max(jspin)).and.wann%l_byindex))then
1308

1309
!           print*,i
1310 1311 1312
           nslibd = nslibd + 1
           eig(nslibd) = eigg(i)
           we(nslibd) = we(i)
1313
           if(noco%l_noco)then
1314 1315 1316 1317 1318
             funbas=       nv(1)+nlotot
             funbas=funbas+nv(2)+nlotot
           else
             funbas=nv(jspin)+nlotot
           endif
1319 1320 1321 1322 1323 1324 1325 1326 1327
           IF(zzMat%l_real) THEN
              do j = 1, funbas
                 zMat%z_r(j,nslibd) = zzMat%z_r(j,i)
              end do
           ELSE
              do j = 1, funbas
                 zMat%z_c(j,nslibd) = zzMat%z_c(j,i)
              end do
           END IF
1328 1329 1330 1331 1332 1333 1334
        endif
      enddo

c***********************************************************
c              rotate the wavefunction
c***********************************************************
      if (wann%l_bzsym.and.oper.ne.1) then  !rotate bkpt
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
!         call wann_kptsrotate(
!     >            atoms%nat,atoms%nlod,atoms%llod,
!     >            atoms%ntype,atoms%nlo,atoms%llo,atoms%invsat,
!     >            noco%l_noco,noco%l_soc,
!     >            atoms%ntype,atoms%neq,nlotot,
!     >            kveclo,jspin,
!     >            oper,sym%nop,sym%mrot,DIMENSION%nvd,nv,
!     >            shiftkpt(:,ikpt),
!     >            sym%tau,
!     x            bkpt,k1(:,:),k2(:,:),k3(:,:),
!     x            zMat,nsfactor)
1346 1347 1348 1349
      else
         nsfactor=cmplx(1.0,0.0)
      endif
c      print*,"bkpt1=",bkpt
1350 1351
!      call cpu_time(tt3)
      !if(l_p0) write(*,*)'nbnd=',tt3-tt4
1352 1353 1354 1355 1356 1357 1358

c******************************************************************

c...the overlap matrix Mmn which is computed for each k- and b-point

      noccbd = nslibd

1359 1360 1361
      allocate(acof(noccbd,0:lmd,atoms%nat),
     &         bcof(noccbd,0:lmd,atoms%nat),
     &         ccof(-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat))
1362 1363 1364 1365 1366 1367 1368 1369

      acof(:,:,:) = cmplx(0.,0.) ; bcof(:,:,:) = cmplx(0.,0.)
      ccof(:,:,:,:) = cmplx(0.,0.)

c...generation the A,B,C coefficients in the spheres
c...for the lapws and local orbitals, summed by the basis functions

      call cpu_time(delta)
1370

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
      ALLOCATE(lapw%k1(SIZE(k1,1),SIZE(k1,2)))
      ALLOCATE(lapw%k2(SIZE(k1,1),SIZE(k1,2)))
      ALLOCATE(lapw%k3(SIZE(k1,1),SIZE(k1,2)))
      lapw%k1 = k1
      lapw%k2 = k2
      lapw%k3 = k3
      lapw%nmat = nmat
      lapw%nv = nv
      ! I think the other variables of lapw are not needed here.

1381 1382
!      CALL abcof(input,atoms,noccbd,sym,cell,bkpt,lapw,noccbd,usdus,
!     >           noco,jspin,kveclo,oneD,acof,bcof,ccof,zMat)
1383

1384 1385
      DEALLOCATE(lapw%k1,lapw%k2,lapw%k3)

1386 1387 1388 1389
      call cpu_time(delta1)
      time_abcof=time_abcof+delta1-delta

      call wann_abinv(
1390 1391 1392 1393
     >        atoms%ntype,atoms%nat,noccbd,atoms%lmaxd,lmd,atoms%llod,
     >        atoms%nlod,atoms%ntype,atoms%neq,noccbd,atoms%lmax,
     >        atoms%nlo,atoms%llo,atoms%invsat,sym%invsatnr,bkpt,
     >        atoms%taual,
1394
     X        acof,bcof,ccof)
1395 1396 1397 1398 1399 1400
!      call cpu_time(tt4)
      !if(l_p0) write(*,*)'abcof=',tt4-tt3


      if((doublespin.eq.3).or.(doublespin.eq.4)) goto 9900

1401 1402 1403

      if(wann%l_orbcomp)then
         call wann_orbcomp(
1404 1405 1406
     >            atoms%llod,noccbd,atoms%nlod,atoms%nat,atoms%ntype,
     >            atoms%lmaxd,lmd,atoms%ntype,atoms%neq,atoms%nlo,
     >            atoms%llo,acof,bcof,ccof,
1407 1408
     >            usdus%ddn(:,:,jspin),usdus%uulon(:,:,jspin),
     >            usdus%dulon(:,:,jspin),usdus%uloulopn(:,:,:,jspin),
1409 1410 1411 1412 1413 1414 1415 1416
     >            wann%oc_num_orbs,
     >            wann%oc_orbs,
     >            wann%l_oc_f,
     =            orbcomp(:,:,:,:,ikpt))
      endif

      if(wann%l_anglmom)then
         call wann_anglmom(
1417 1418 1419 1420
     >                  atoms%llod,noccbd,atoms%nlod,atoms%nat,
     >                  atoms%ntype,atoms%lmax,lmd,!atoms%lmaxd,lmd,
     >                  atoms%ntype,atoms%neq,atoms%nlo,atoms%llo,
     >                  acof,bcof,ccof,
1421 1422 1423
     >                  usdus%ddn(:,:,jspin),usdus%uulon(:,:,jspin),
     >                  usdus%dulon(:,:,jspin),
     >                  usdus%uloulopn(:,:,:,jspin),
1424 1425 1426 1427 1428 1429
     =                  anglmom(:,:,:,ikpt))
      endif

#ifdef CPP_TOPO
      if(wann%l_surfcurr)then
c         call wann_surfcurr_int(
1430
c     >        DIMENSION%nv2d,jspin,oneD%odi,oneD%ods,stars%ng3,vacuum%nmzxyd,stars%ng2,sphhar%ntypsd,
1431 1432 1433 1434 1435
c     >        atoms%ntype,atoms%lmaxd,atoms%jmtd,atoms%ntype,atoms%nat,vacuum%nmzd,atoms%neq,stars%ng3,vacuum%nvac,
c     >        vacuum%nmz,vacuum%nmzxy,stars%ng2,sym%nop,sym%nop2,cell%volint,input%film,sliceplot%slice,sym%symor,
c     >        sym%invs,sym%invs2,cell%z1,vacuum%delz,atoms%ngopr,atoms%ntypsy,atoms%jri,atoms%pos,atoms%zatom,
c     >        atoms%lmax,sym%mrot,sym%tau,atoms%rmsh,sym%invtab,cell%amat,cell%bmat,cell%bbmat,ikpt,sliceplot%nnne,sliceplot%kk,
c     >        DIMENSION%nvd,atoms%nlod,atoms%llod,nv(jspin),lmd,bkpt,cell%omtil,atoms%nlo,atoms%llo,
1436 1437
c     >        k1(:,jspin),k2(:,jspin),k3(:,jspin),evac(:,jspin),
c     >        vz(:,:,jspin2),
1438
c     >        nslibd,DIMENSION%nbasfcn,DIMENSION%neigd,ff,gg,flo,acof,bcof,ccof,z,
1439 1440 1441
c     >        surfcurr(:,:,:,ikpt))

         call wann_surfcurr_int2(
1442 1443
     >            DIMENSION%nv2d,jspin,oneD%odi,oneD%ods,stars%ng3,
     >            vacuum%nmzxyd,
1444