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 150 151
      character(len=8) :: dop,iop,name(10)
      real    :: bkpt(3),bkpt_b(3),sfp,tpi,wronk,wk,wk_b,phase
      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
!!! energy parameters
186 187 188 189
      real    :: ello(atoms%nlod,atoms%ntype,max(2,DIMENSION%jspd))
      real    :: evac(2,max(2,DIMENSION%jspd))
      real    :: epar(0:atoms%lmaxd,atoms%ntype,max(2,DIMENSION%jspd))
      real    :: evdu(2,max(DIMENSION%jspd,2))
190
!!! the Mmn matrices
191 192
      complex, allocatable :: mmnk(:,:,:,:),mmn(:,:,:)           
      complex, allocatable :: amn(:,:,:),nablamat(:,:,:,:)        
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
      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(:,:)
217
      integer :: ngopr1(atoms%nat)
218
      integer,allocatable::maptopair(:,:,:)
219
      integer :: wannierspin,jspin2,jspin7,jspin2_b
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 247 248 249 250 251
      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(:,:,:)


252
c---->gwf
253 254 255

      ! FURTHER VARIABLES
      real :: qpt_i(3),qptb_i(3)
256 257
      real :: alph_i(atoms%ntype),alphb_i(atoms%ntype)
      real :: beta_i(atoms%ntype),betab_i(atoms%ntype)
258 259
      real :: theta_i, thetab_i, phi_i, phib_i
      real :: dalph,db1,db2,coph,siph
260
      real :: zero_taual(3,atoms%nat),bqpt(3),wqb
261
      real :: tt1,tt2,tt3,tt4,tt5,tt6,t_it,t_kov,t_qov
262
      real :: eig_qb(DIMENSION%neigd)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

      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
312 313

      TYPE(t_usdus) :: usdus
314
      TYPE(t_zmat)  :: zMat, zzMat, zMat_b, zMat_qb
315 316
      TYPE(t_lapw)  :: lapw, lapw_b, lapw_qb
      TYPE(t_wann)  :: wannTemp
317

318 319
      eig_id = eig_idList(1)

320 321 322 323
c----<gwf


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


337 338 339 340 341 342 343 344 345 346
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
347
      zero_taual = 0.0
348 349

      ci=cmplx(0.0,1.0)
350
      cmplx_1 = cmplx(1.0,0.0)
351 352 353 354

      call cpu_time(time_total)

      l_p0 = .false.
355
      if (mpi%irank.eq.0) l_p0 = .true.
356 357 358

      sfp = 2* sqrt( pimach() )
      tpi = 2* pimach()
359
      lmd = atoms%lmaxd*(atoms%lmaxd+2)
360 361

!!!   should be changed in case the windows are really used
362
      nkpts = nkpt
363

364 365
      ! do we have to construct GWF ?
      l_gwf = .false.
366
      l_gwf = wann%l_sgwf.or.wann%l_socgwf 
367 368 369 370 371 372
      
      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()
373
      allocate( chi(atoms%ntype) )
374

375
c-----read the input file to determine what to do
376 377
      wann%atomlist_num=atoms%nat
      wann%oc_num_orbs=atoms%nat
378 379

      CALL wann_read_inp(input,l_p0,wann)
380

381

382
c-----input file for orbital decomposition
383 384 385 386 387 388 389 390 391 392 393
      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
394
            wann%oc_num_orbs=atoms%nat
395 396 397 398 399 400 401 402 403
            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            
404
         call wann_updown(
405
     >      mpi,input,sym,atoms,stars,vacuum,sphhar,oneD,noco,cell,vTot,
406
     >      enpara,eig_idList(1),l_real,
407
     >      mpi%mpi_comm,atoms%l_dulo,noco%l_noco,noco%l_ss,
408 409 410 411 412 413 414
     >      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
415 416
     >      stars%sk2,stars%phi2,oneD%odi,oneD%ods,mpi%irank,mpi%isize,
     >      stars%ng3,
417 418
     >      vacuum%nmzxyd,vacuum%nmzd,atoms%jmtd,sphhar%nlhd,stars%ng3,
     >      vacuum%nvac,sym%invs,sym%invs2,input%film,sphhar%nlh,
419
     >      atoms%jri,sphhar%ntypsd,atoms%ntypsy,input%jspins,nkpt,
420
     >      atoms%dx,stars%ng2,atoms%rmsh,sliceplot%e1s,sliceplot%e2s,
421 422
     >      atoms%ulo_der,stars%ustep,stars%ig,stars%mx1,
     >      stars%mx2,stars%mx3,stars%rgphs,
423 424 425 426 427 428 429
     >      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)
430 431 432 433 434

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

435
         CALL juDFT_end("updown done",mpi%irank)
436 437
      endif

438 439 440 441 442
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  modern theory of orbital magnetization from Wannier functions
c  Jan-Philipp Hanke
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      if(wann%l_matrixuHu)then
443
         wannTemp = wann
444
         call wann_uHu(
445 446
     >      DIMENSION,stars,vacuum,atoms,sphhar,input,kpts,sym,mpi,
     >      banddos,oneD,noco,cell,vTot,wannTemp,eig_idList,
447 448 449 450 451 452 453
     >      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,
454 455
     >      noco%beta,noco%qss,stars%sk2,stars%phi2,oneD%odi,oneD%ods,
     >      mpi%irank,
456 457 458
     >      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,
459
     >      input%jspins,nkpt,atoms%dx,stars%ng2,atoms%rmsh,
460
     >      sliceplot%e1s,sliceplot%e2s,atoms%ulo_der,stars%ustep,
461 462
     >      stars%ig,stars%mx1,stars%mx2,stars%mx3,
     >      stars%rgphs,sliceplot%slice,
463 464 465 466 467 468 469
     >      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,
470 471 472
     >      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)
473

474 475 476 477
         DO pc = 1, wann%nparampts
            CALL close_eig(eig_idList(pc))
         END DO

478
        CALL juDFT_end("wann_uHu done",mpi%irank)
479 480 481 482 483 484 485
      endif

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  modern theory of DMI from higher-dimensional Wannier functions
c  Jan-Philipp Hanke
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      if(wann%l_matrixuHu_dmi)then
486
         wannTemp = wann
487
         call wann_uHu_dmi(
488 489
     >      DIMENSION,stars,vacuum,atoms,sphhar,input,kpts,sym,mpi,
     >      banddos,oneD,noco,cell,vTot,wannTemp,eig_idList,
490 491 492 493 494 495 496
     >      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,
497 498
     >      noco%beta,noco%qss,stars%sk2,stars%phi2,oneD%odi,oneD%ods,
     >      mpi%irank,
499 500 501
     >      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,
502
     >      input%jspins,nkpt,atoms%dx,stars%ng2,atoms%rmsh,
503
     >      sliceplot%e1s,sliceplot%e2s,atoms%ulo_der,stars%ustep,
504 505
     >      stars%ig,stars%mx1,stars%mx2,stars%mx3,
     >      stars%rgphs,sliceplot%slice,
506 507 508 509 510 511 512
     >      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,
513 514 515
     >      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)
516

517 518 519 520 521
         DO pc = 1, wann%nparampts
            CALL close_eig(eig_idList(pc))
         END DO

         CALL juDFT_end("wann_uHu dmi done",mpi%irank)
522 523
      endif

524 525 526 527 528 529 530 531 532
      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")

533

534
      efermi=results%ef
535 536 537
      if(.not.wann%l_fermi)efermi=0.0

#ifdef CPP_MPI
538
         call MPI_BARRIER(mpi%mpi_comm,ierr)
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 567 568 569 570 571
#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
572 573
         allocate(irreduc(fullnkpts),mapkoper(fullnkpts))
         allocate(shiftkpt(3,fullnkpts))
574 575
      endif

576

577
      if(l_gwf) fullnqpts = wann%nparampts
578 579


580 581 582
      nrec = 0
      if(l_p0)then
      write (*,*) 'fermi energy:',efermi
583 584
      write (*,*) 'emin,emax=',sliceplot%e1s,sliceplot%e2s
      write (*,*) 'nbasfcn =',DIMENSION%nbasfcn
585 586
      endif
      nlotot = 0
587 588 589
      do n = 1, atoms%ntype
        do l = 1,atoms%nlo(n)
          nlotot = nlotot + atoms%neq(n) * ( 2*atoms%llo(l,n) + 1 )
590 591 592 593 594 595 596 597
        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.
598 599
     &   (.not.wann%l_orbcomp).and.(.not.wann%l_perturb) .and.
     &   (.not.wann%l_finishgwf) ) goto 1911
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

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")
624
         if (bpt(nn,ikpt)>fullnkpts) CALL juDFT_error("bpt.gt.fullnkpts"
625 626 627 628 629 630
     +        ,calledby ="wannier")
        enddo
       enddo
       close (202)
        allocate(kdiff(3,nntot))
      endif
631 632 633 634

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

      if(wann%l_finishgwf) goto 9110
674 675 676 677 678 679 680
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))
681
         call wann_mmnk_symm(input,kpts,
682
     >     fullnkpts,nntot,bpt,gb,wann%l_bzsym,
683
     >     irreduc,mapkoper,l_p0,input%film,sym%nop,sym%invtab,sym%mrot,
684 685
     >     oneD%odi%d1,sym%tau,
     <     pair_to_do,maptopair,kdiff,.false.,wann%param_file)
686 687 688
         call cpu_time(delta1)
         time_symm=delta1-delta
      endif
689 690 691 692 693

      ! 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))
694
         call wann_mmnk_symm(input,kpts,
695
     >     fullnqpts,nntot_q,bpt_q,gb_q,wann%l_bzsym,
696 697
     >     irreduc_q,mapqoper,l_p0,.false.,1,sym%invtab(1),
     >     sym%mrot(:,:,1),.false.,sym%tau,
698
     <     pair_to_do_q,maptopair_q,qdiff,.true.,wann%param_file)
699 700 701
      endif


702 703 704 705
c*********************************************************
cccccccccccccccc   initialize the potential   cccccccccccc
c*********************************************************

706 707 708
      allocate ( vz(vacuum%nmzd,2,4) )
      allocate ( vr(atoms%jmtd,atoms%ntype,DIMENSION%jspd) )
      allocate ( vso(atoms%jmtd,atoms%nat,2) )
709

710 711
      vz = 0.0
      vz(:,:,:SIZE(vTot%vacz,3)) = vTot%vacz(:,:,:)
712

713 714 715
      do jspin = 1,input%jspins
        do n = 1, atoms%ntype
          do j = 1,atoms%jri(n)
716
            vr(j,n,jspin) = vTot%mt(j,0,n,jspin)
717 718 719 720 721
          enddo
        enddo
      enddo

      if(wann%l_soctomom)then
722
        CALL vsoc(input,atoms,vr,epar,.TRUE., vso)
723 724
      endif

725
      if(noco%l_noco.and.input%film)then
726
         npotmatfile=25
727
         allocate(vpw(stars%ng3,1))
728
         if(.not.oneD%odi%d1)
729
     &    allocate( vxy(vacuum%nmzxyd,stars%ng2-1,2) )
730 731 732

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

751
            if(.not.oneD%odi%d1)then
752
            DO ipot = 1,3
753 754
               READ (npotmatfile)((vxy(imz,igvm2,ivac),
     +            imz=1,vacuum%nmzxy),igvm2=1,stars%ng2-1)
755 756 757 758 759
            enddo    
            endif   
         enddo   
         CLOSE (npotmatfile)
         deallocate(vpw)
760
         if(.not.oneD%odi%d1)then
761 762
            deallocate(vxy)
         endif
763
      endif   
764
    
765
cccccccccccccccc   end of the potential part  ccccccccccc
766 767
      wannierspin=DIMENSION%jspd
      if(noco%l_soc) wannierspin=2
768
     
769 770
      allocate ( kveclo(nlotot),nv(wannierspin) )
      allocate ( kveclo_b(nlotot),nv_b(wannierspin) )
771
      IF(l_gwf) allocate ( kveclo_qb(nlotot),nv_qb(wannierspin) )
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
      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) )
796 797 798 799 800 801 802 803

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

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

813
       ALLOCATE(innerEig_idList(nntot_q))
814

815
       call cpu_time(tt1)
816
!       t_kov=0.0
817 818 819 820 821 822
       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)

823 824 825 826 827
       qpt_i = noco%qss
       alph_i = noco%alph
       beta_i = noco%beta
       theta_i = noco%theta
       phi_i = noco%phi
828 829 830 831 832 833
       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)
834 835 836 837 838
       endif

       IF (l_gwf) THEN
        IF(wann%l_matrixmmn)THEN
          do iqpt_b=1,nntot_q
839 840 841 842 843 844 845 846 847 848 849 850 851 852

            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)
853

854 855
          enddo
        ENDIF
856 857 858 859 860 861 862 863 864 865 866 867 868

        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)
869

870
       ELSEIF(wann%l_ms) THEN
871 872 873 874 875 876 877 878 879 880 881 882 883

        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)
884

885 886
       ELSE
          fending=''
887
       ENDIF ! l_gwf.or.wann%l_ms
888 889 890 891
       nrec=0
       nrec_b=0


892 893 894
c****************************************************
c cycle by spins starts! 
c****************************************************
895 896 897 898 899 900 901 902 903
      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

904
         if(.not.noco%l_noco) then
905 906 907 908 909 910 911 912
            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
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

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
940
       if(noco%l_soc .and. input%jspins.eq.1)jspin2=1
941
       jspin2_b=jspin_b
942
       if(noco%l_soc .and. input%jspins.eq.1)jspin2_b=1
943

944 945
       jsp_start = jspin ; jsp_end = jspin

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

955 956
       call cdn_read0(eig_id,mpi%irank,mpi%isize,jspin5,DIMENSION%jspd, !wannierspin instead of DIMENSION%jspd?
     >                noco%l_noco,
957 958
     <                ello,evac,epar,bkpt,wk,n_bands,n_size)

959 960 961
       enddo
!       call cpu_time(tt3)
       !if(l_p0) write(*,*)'cdn_read0=',tt3-tt2
962

963
c..   now we want to define the maximum number of the bands by all kpts
964 965 966 967 968
      nbnd = 0

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

970
c..write individual files if multi-spiral mode wann%l_ms=T
971
c*************************************************************
972 973
      if(l_p0)then         
         call wann_write_eig(
974
     >            eig_id,l_real,
975 976 977 978
     >            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,
979 980
     >            wann%l_bzsym,wann%l_byindex,wann%l_bynumber,
     >            wann%l_byenergy,
981
     >            irreduc,oneD%odi,wann%band_min(jspin),
982 983
     >            wann%band_max(jspin),
     >            numbands,
984
     >            sliceplot%e1s,sliceplot%e2s,efermi,.false.,nkpts,
985 986
     <            nbnd,kpoints,l_gwf,iqpt)       

987
         if(oneD%odi%d1)then
988
            kpoints(:)=kpoints(:)*cell%bmat(3,3)         
989 990
         endif
      endif!l_p0
991 992
!      call cpu_time(tt2)
      !if(l_p0)write(*,*)'write_eig=',tt2-tt3
993

994
     
995 996 997
! nbnd is calculated for process zero and is sent here to the others
#ifdef CPP_MPI
      if(l_p0)then
998 999
         do cpu_index=1,mpi%isize-1
      call MPI_SEND(nbnd,1,MPI_INTEGER,cpu_index,1,mpi%mpi_comm,ierr)
1000 1001
         enddo
      else
1002
       call MPI_RECV(nbnd,1,MPI_INTEGER,0,1,mpi%mpi_comm,stt,ierr)
1003 1004
      endif
#endif
1005
     
1006
       print*,"process: ",mpi%irank," nbnd= ",nbnd
1007
!       call cpu_time(tt3)
1008 1009 1010 1011
c##################################################################
      if(wann%l_mmn0)then
           allocate ( mmn(nbnd,nbnd,fullnkpts) )
           mmn(:,:,:) = cmplx(0.,0.)
1012
           if((noco%l_soc.or.noco%l_noco) .and. (doublespin.eq.1))
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 1042 1043 1044 1045 1046
     &          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

1047
      !write (*,*) 'nwfs=',nwfs
1048 1049 1050 1051 1052
      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
1053
      endif
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

      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
1066
       if(.not.allocated(mmnk))then
1067 1068 1069 1070 1071
         allocate ( mmnk(nbnd,nbnd,nntot,fullnkpts) )
         mmnk = (0.,0.)
       endif  
      endif

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
      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

 
1087
      allocate ( flo(atoms%ntype,atoms%jmtd,2,atoms%nlod,2) )
1088 1089
!      call cpu_time(tt2)
      !if(l_p0) write(*,*)'allocate',tt2-tt3
1090

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

1099
         call radfun(
1100 1101 1102 1103
     >              l,n,jspin4,epar(l,n,jspin3),vr(1,n,jspin3),atoms,
     <              ff(n,:,:,l,jspin4),gg(n,:,:,l,jspin4),usdus,
     <              nodeu,noded,wronk)

1104 1105
   30  continue
c...and the local orbital radial functions
1106
       do ilo = 1, atoms%nlo(n)
1107

1108
         call radflo(
1109 1110 1111 1112
     >             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))

1113
       enddo
1114
c       na = na + atoms%neq(n)
1115
   40 continue
1116 1117 1118 1119
      enddo!jspin3
!      call cpu_time(tt3)
      !if(l_p0) write(*,*)'radfun=',tt3-tt2

1120 1121 1122 1123
c****************************************************************
c   calculate the k-independent uju*gaunt-matrix needed for
c   mmnmatrix
c****************************************************************
1124 1125
! TODO: make this more efficient (i.e., compute ujugaunt only once
! and not for all q-points).
1126 1127 1128
      if(wann%l_matrixmmn)then
         call cpu_time(delta)
         allocate(ujug(0:lmd,0:lmd,
1129
     &              1:atoms%ntype,1:nntot))
1130
         allocate(ujdg(0:lmd,0:lmd,
1131
     &              1:atoms%ntype,1:nntot))
1132
         allocate(djug(0:lmd,0:lmd,
1133
     &              1:atoms%ntype,1:nntot))
1134
         allocate(djdg(0:lmd,0:lmd,
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
     &              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))
1147

1148
         call wann_ujugaunt(
1149 1150 1151
     >            atoms%llod,nntot,kdiff,atoms%lmax,atoms%ntype,
     >            atoms%ntype,cell%bbmat,cell%bmat,atoms%nlod,atoms%nlo,
     >            atoms%llo,flo(:,:,:,:,jspin),
1152 1153 1154 1155
     >            flo(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin),
     >            gg(:,:,:,:,jspin),
1156 1157
     >            gg(:,:,:,:,jspin),atoms%jri,atoms%rmsh,atoms%dx,
     >            atoms%jmtd,atoms%lmaxd,lmd,
1158
     <            ujug,ujdg,djug,djdg,
1159 1160 1161 1162 1163 1164 1165 1166
     <            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,
1167
     &              1:atoms%ntype,1:nntot_q))
1168
            allocate(ujdg_q(0:lmd,0:lmd,
1169
     &              1:atoms%ntype,1:nntot_q))
1170
            allocate(djug_q(0:lmd,0:lmd,
1171
     &              1:atoms%ntype,1:nntot_q))
1172
            allocate(djdg_q(0:lmd,0:lmd,
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
     &              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))
1185 1186 1187
            
            ! we need G(q+b)/2 as argument for the sph. Bessel func.
            ! and additionally a spin-dependent sign (-/+ 1)^{lpp}
1188
            if(wann%l_sgwf) call wann_ujugaunt(
1189 1190 1191
     >            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),
1192 1193 1194 1195
     >            flo(:,:,:,:,jspin_b),
     >            ff(:,:,:,:,jspin),
     >            ff(:,:,:,:,jspin_b),
     >            gg(:,:,:,:,jspin),
1196 1197
     >            gg(:,:,:,:,jspin_b),atoms%jri,atoms%rmsh,atoms%dx,
     >            atoms%jmtd,atoms%lmaxd,lmd,
1198 1199 1200 1201
     <            ujug_q,ujdg_q,djug_q,djdg_q,
     <            ujulog_q,djulog_q,ulojug_q,ulojdg_q,ulojulog_q,.true.,
     >            sign_q)

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

1218 1219
         call cpu_time(delta1)
         time_ujugaunt=delta1-delta
1220 1221 1222
      endif !l_matrixmmn
!      call cpu_time(tt2)
      !if(l_p0) write(*,*)'ujugaunt=',tt2-tt3
1223

1224 1225 1226 1227
      zzMat%l_real = l_real
      zzMat%nbasfcn = DIMENSION%nbasfcn
      zzMat%nbands = DIMENSION%neigd
      IF(l_real) THEN
1228 1229
	    IF(.not.ALLOCATED(zzMat%z_r))
     >    ALLOCATE (zzMat%z_r(zzMat%nbasfcn,zzMat%nbands))
1230
      ELSE
1231 1232
	    IF(.not.ALLOCATED(zzMat%z_c))
     >    ALLOCATE (zzMat%z_c(zzMat%nbasfcn,zzMat%nbands))
1233 1234 1235 1236 1237 1238
      END IF

      zMat%l_real = zzMat%l_real
      zMat%nbasfcn = zzMat%nbasfcn
      zMat%nbands = zzMat%nbands
      IF (zzMat%l_real) THEN
1239 1240
	    IF(.not.ALLOCATED(zMat%z_r))
     >    ALLOCATE (zMat%z_r(zMat%nbasfcn,zMat%nbands))
1241 1242
         zMat%z_r = 0.0
      ELSE
1243 1244
	    IF(.not.ALLOCATED(zMat%z_c))
     >    ALLOCATE (zMat%z_c(zMat%nbasfcn,zMat%nbands))
1245 1246 1247 1248 1249 1250 1251
         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
1252 1253
	    IF(.not.ALLOCATED(zMat_b%z_r))
     >    ALLOCATE (zMat_b%z_r(zMat_b%nbasfcn,zMat_b%nbands))
1254 1255
         zMat_b%z_r = 0.0
      ELSE
1256 1257
	    IF(.not.ALLOCATED(zMat_b%z_c))
     >    ALLOCATE (zMat_b%z_c(zMat_b%nbasfcn,zMat_b%nbands))
1258 1259 1260
         zMat_b%z_c = CMPLX(0.0,0.0)
      END IF

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

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

1276
      allocate ( we(DIMENSION%neigd),eigg(DIMENSION%neigd) )
1277 1278 1279

      call cpu_time(delta)
      n_start=1
1280
      n_end=DIMENSION%neigd
1281 1282 1283 1284 1285


! 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).
1286 1287 1288

      CALL cdn_read(
     >              eig_id,
1289 1290 1291
     >              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,
1292
     <              nmat,nv,ello,evdu,epar,kveclo,
1293 1294
     <              k1,k2,k3,bkpt,wk,nbands,eigg,zzMat)

1295 1296
!      call cpu_time(tt4)
      !if(l_p0) write(*,*)'cdn_read=',tt4-tt3
1297 1298 1299 1300 1301 1302 1303 1304 1305

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

c...we work only within the energy window

      eig(:) = 0.

1306
!      print*,"bands used:"
1307 1308

      do i = 1,nbands
1309 1310 1311 1312 1313
        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
1314

1315
!           print*,i
1316 1317 1318
           nslibd = nslibd + 1
           eig(nslibd) = eigg(i)
           we(nslibd) = we(i)
1319
           if(noco%l_noco)then
1320 1321 1322 1323 1324
             funbas=       nv(1)+nlotot
             funbas=funbas+nv(2)+nlotot
           else
             funbas=nv(jspin)+nlotot
           endif
1325 1326 1327 1328 1329 1330 1331 1332 1333
           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
1334 1335 1336 1337 1338 1339 1340 1341
        endif
      enddo

c***********************************************************
c              rotate the wavefunction
c***********************************************************
      if (wann%l_bzsym.and.oper.ne.1) then  !rotate bkpt
         call wann_kptsrotate(
1342 1343 1344 1345
     >            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,
1346
     >            kveclo,jspin,
1347
     >            oper,sym%nop,sym%mrot,DIMENSION%nvd,nv,
1348
     >            shiftkpt(:,ikpt),
1349
     >            sym%tau,
1350
     x            bkpt,k1(:,:),k2(:,:),k3(:,:),
1351
     x            zMat,nsfactor)
1352 1353 1354 1355
      else
         nsfactor=cmplx(1.0,0.0)
      endif
c      print*,"bkpt1=",bkpt
1356 1357
!      call cpu_time(tt3)
      !if(l_p0) write(*,*)'nbnd=',tt3-tt4
1358 1359 1360 1361 1362 1363 1364

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

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

      noccbd = nslibd

1365 1366 1367
      allocate(acof(noccbd,0:lmd,atoms%nat),
     &         bcof(noccbd,0:lmd,atoms%nat),
     &         ccof(-atoms%llod:atoms%llod,noccbd,atoms%nlod,atoms%nat))
1368 1369 1370 1371 1372 1373 1374 1375

      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)
1376

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
      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.

1387 1388 1389
      CALL abcof(input,atoms,noccbd,sym,cell,bkpt,lapw,noccbd,usdus,
     >           noco,jspin,kveclo,oneD,acof,bcof,ccof,zMat)

1390 1391
      DEALLOCATE(lapw%k1,lapw%k2,lapw%k3)

1392 1393 1394 1395
      call cpu_time(delta1)
      time_abcof=time_abcof+delta1-delta

      call wann_abinv(
1396 1397 1398 1399
     >        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,
1400
     X        acof,bcof,ccof)
1401 1402 1403 1404 1405 1406
!      call cpu_time(tt4)
      !if(l_p0) write(*,*)'abcof=',tt4-tt3


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

1407 1408 1409

      if(wann%l_orbcomp)then
         call wann_orbcomp(
1410 1411 1412
     >            atoms%llod,noccbd,atoms%nlod,atoms%nat,atoms%ntype,
     >            atoms%lmaxd,lmd,atoms%ntype,atoms%neq,atoms%nlo,
     >            atoms%llo,acof,bcof,ccof,
1413 1414
     >            usdus%ddn(:,:,jspin),usdus%uulon(:,:,jspin),
     >            usdus%dulon(:,:,jspin),usdus%uloulopn(:,:,:,jspin),
1415 1416 1417 1418 1419 1420 1421 1422
     >            wann%oc_num_orbs,
     >            wann%oc_orbs,
     >            wann%l_oc_f,
     =            orbcomp(:,:,:,:,ikpt))
      endif

      if(wann%l_anglmom)then
         call wann_anglmom(
1423 1424 1425 1426
     >                  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,
1427 1428 1429
     >                  usdus%ddn(:,:,jspin),usdus%uulon(:,:,jspin),
     >                  usdus%dulon(:,:,jspin),
     >                  usdus%uloulopn(:,:,:,jspin),
1430 1431 1432 1433 1434 1435
     =                  anglmom(:,:,:,ikpt))
      endif

#ifdef CPP_TOPO
      if(wann%l_surfcurr)then
c         call wann_surfcurr_int(
1436
c     >        DIMENSION%nv2d,jspin,oneD%odi,oneD%ods,stars%ng3,vacuum%nmzxyd,stars%ng2,sphhar%ntypsd,
1437 1438 1439 1440 1441
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,
1442 1443
c     >        k1(:,jspin),k2(:,jspin),k3(:,jspin),evac(:,jspin),
c     >        vz(:,:,jspin2),
1444
c     >        nslibd,DIMENSION%nbasfcn,DIMENSION%neigd,ff,gg,flo,acof,bcof,ccof,z,
1445 1446 1447
c     >        surfcurr(:,:,:,ikpt))

         call wann_surfcurr_int2(
1448 1449
     >            DIMENSION%nv2d,jspin,oneD%odi,oneD%ods,stars%ng3,
     >            vacuum%nmzxyd,
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
     >            stars%ng2,sphhar%ntypsd,atoms%ntype,atoms%lmaxd,
     >            atoms%jmtd,atoms%ntype,atoms%nat,vacuum%nmzd,
     >            atoms%neq,stars%ng3,vacuum%nvac,vacuum%nmz,
     >            vacuum%nmzxy,stars%ng2,sym%nop,sym%nop2,cell%volint,
     >            input%film,sliceplot%slice,sym%symor,
     >            sym%invs,sym%invs2,cell%z1,vacuum%delz,atoms%ngopr,
     >            atoms%ntypsy,atoms%jri,atoms%pos,atoms%taual,
     >            atoms%zatom,atoms%rmt,atoms%lmax,sym%mrot,sym%tau,
     >            atoms%rmsh,sym%invtab,cell%amat,cell%bmat,cell%bbmat,
     >            ikpt,DIMENSION%nvd,nv(jspin),bkpt,cell%omtil,
1460
     >            k1(:,jspin),k2(:,jspin),k3(:,jspin),
1461
     >            nslibd,DIMENSION%nbasfcn,DIMENSION%neigd,z,
1462 1463 1464 1465
     <            dirfacs,
     >            surfcurr(:,:,:,ikpt))

         call wann_surfcurr(
1466 1467 1468 1469
     >            dirfacs,cell%amat,
     >            jspin,atoms%ntype,atoms%lmaxd,atoms%lmax,atoms%nat,
     >            atoms%neq,noccbd,lmd,atoms%nat,atoms%llod,atoms%nlod,
     >            atoms%nlo,atoms%llo, 
1470
     >            acof,bcof,ccof,
1471 1472 1473
     >            us(:,:,jspin),dus(:,:,jspin),duds(:,:,jspin),
     >            uds(:,:,jspin),
     >            ulos(:,:,jspin),dulos(:,:,jspin),
1474
     >            atoms%rmt,atoms%pos, 
1475 1476 1477 1478 1479 1480
     &            surfcurr(:,:,:,ikpt))
        write(6,*)"dirfacs=",dirfacs
      endif

      if(wann%l_soctomom)then
         call wann_soc_to_mom(
1481 1482 1483
     >            jspin,atoms%ntype,atoms%lmaxd,atoms%lmax,atoms%nat,
     >            atoms%jmtd,atoms%jri,atoms%rmsh,atoms%dx,atoms%neq,
     >            noccbd,lmd,atoms%nat,atoms%llod,atoms%nlod,
1484
     >            vso(:,:,1), 
1485
     >            ff(:,:,:,:,jspin),gg(:,:,:,:,jspin),
1486 1487 1488 1489 1490 1491
     >            acof,bcof,ccof,
     &            soctomom(:,:,:,ikpt))
      endif

      if(wann%l_nabla)then
         call wann_nabla(
1492 1493 1494 1495
     >       atoms%nlo,atoms%llo,
     >       jspin,atoms%ntype,atoms%lmaxd,atoms%lmax,atoms%nat,
     >       atoms%jmtd,atoms%jri,atoms%rmsh,atoms%dx,atoms%neq,
     >       noccbd,lmd,atoms%nat,atoms%llod,atoms%nlod, 
1496
     >       ff(:,:,:,:,jspin),gg(:,:,:,:,jspin),flo(:,:,:,:,jspin),
1497 1498
     >       acof,bcof,ccof,
     &       nablamat(:,:,:,ikpt))
1499
         if(input%film.and..not.oneD%odi%d1)then
1500
            call wann_nabla_vac(
1501 1502
     >          cell%z1,vacuum%nmzd,DIMENSION%nv2d,
     >          stars%mx1,stars%mx2,stars%mx3,
1503 1504 1505 1506 1507
     >          stars%ng3,vacuum%nvac,stars%ig,vacuum%nmz,vacuum%delz,
     >          stars%ig2,cell%area,cell%bmat,cell%bbmat,evac(:,jspin),
     >          bkpt,vz(:,:,jspin2),nslibd,jspin,k1,k2,k3,wannierspin,
     >          DIMENSION%nvd,DIMENSION%nbasfcn,DIMENSION%neigd,z,nv,
     >          cell%omtil,
1508 1509 1510 1511 1512 1513 1514
     <          nablamat(:,:,:,ikpt))
         endif   
         addnoco=0
         do 41 i = n_rank+1,nv(jspin),n_size
           b1(1)=bkpt(1)+k1(i,jspin)
           b1(2)=bkpt(2)+k2(i,jspin)
           b1(3)=bkpt(3)+k3(i,jspin)
1515 1516 1517 1518 1519 1520
           b2(1)=b1(1)*cell%bmat(1,1)+b1(2)*cell%bmat(2,1)+
     +           b1(3)*cell%bmat(3,1)
           b2(2)=b1(1)*cell%bmat(1,2)+b1(2)*cell%bmat(2,2)+
     +           b1(3)*cell%bmat(3,2)
           b2(3)=b1(1)*cell%bmat(1,3)+b1(2)*cell%bmat(2,3)+
     +           b1(3)*cell%bmat(3,3)
1521 1522 1523 1524 1525
           do 42 j = n_rank+1,nv(jspin),n_size
c-->     determine index and phase factor
            i1 = k1(j,jspin) - k1(i,jspin)
            i2 = k2(j,jspin) - k2(i,jspin)
            i3 = k3(j,jspin) - k3(i,jspin)
1526
            in = stars%ig(i1,i2,i3)
1527
            if (in.eq.0) goto 42
1528 1529
            phase   = stars%rgphs(i1,i2,i3)
            phasust = cmplx(phase,0.0)*stars%ustep(in)
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

            do m = 1,nslibd
             do n = 1,nslibd
              do dir=1,3  
                  
#if ( !defined(CPP_INVERSION) || defined(CPP_SOC) )
               value=phasust*z(i+addnoco,m)*conjg(z(j+addnoco,n))
               nablamat(dir,m,n,ikpt) =
     =         nablamat(dir,m,n,ikpt) -
     +          value*b2(dir)
#else
               value=phasust*cmplx(z(i+addnoco,m)*z(j+addnoco,n),0.0)
               nablamat(dir,m,n,ikpt) =
     =         nablamat(dir,m,n,ikpt) - 
     +          value*b2(dir)
#endif
              enddo 
             enddo
            enddo

 42        continue
 41      continue

      endif   
#endif
c      goto jump no longer needed?
c      if ((.not.wann%l_matrixmmn).and.(.not.wann%l_matrixamn).and.
c     &    (.not.wann%l_bestproj).and.(.not.wann%l_projmethod).and.
c     &    (.not.wann%l_mmn0)) goto 3

1560

1561 1562 1563
c------mmn0-matrix
       if(wann%l_mmn0)then
       addnoco=0
1564
       if(noco%l_noco.and.(jspin.eq.2))then
1565 1566
          addnoco=nv(1)+nlotot
       endif
1567
       
1568 1569 1570 1571 1572 1573 1574
c$$$       do 21 i = n_rank+1,nv(jspin),n_size
c$$$         do 22 j = n_rank+1,nv(jspin),n_size
c$$$
c$$$c-->     determine index and phase factor
c$$$            i1 = k1(j,jspin) - k1(i,jspin)
c$$$            i2 = k2(j,jspin) - k2(i,jspin)
c$$$            i3 = k3(j,jspin) - k3(i,jspin)
1575 1576
c$$$c            if(abs(i1).gt.stars%mx1.or.abs(i2).gt.stars%mx2
c$$$c     & .or. abs(i3).gt.stars%mx3)print*,"interstitial warning"
1577
c$$$            in = stars%ig(i1,i2,i3)
1578
c$$$            if (in.eq.0) goto 22
1579 1580
c$$$            phase   = stars%rgphs(i1,i2,i3)
c$$$            phasust = cmplx(phase,0.0)*stars%ustep(in)
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
c$$$            do m = 1,nslibd
c$$$             do n = 1,nslibd
c$$$#if ( !defined(CPP_INVERSION) || defined(CPP_SOC) )
c$$$              mmn(m,n,ikpt) =
c$$$     =        mmn(m,n,ikpt) + 
c$$$     +          phasust*z(i+addnoco,m)*conjg(z(j+addnoco,n))
c$$$#else
c$$$              mmn(m,n,ikpt) =
c$$$     =        mmn(m,n,ikpt) + 
c$$$     +          phasust*cmplx(z(i+addnoco,m)*z(j+addnoco,n),0.0)
c$$$#endif
c$$$             enddo
c$$$            enddo
c$$$
c$$$  22     continue
c$$$  21   continue

c-----> interstitial contribution to mmn0-matrix
1599

1600
      call wann_mmkb_int(
1601
     >         cmplx_1,addnoco,addnoco,
1602
     >         DIMENSION%nvd,stars%mx1,stars%mx2,stars%mx3,
1603 1604
     >         stars%ng3,k1(:,jspin),k2(:,jspin),k3(:,jspin),
     >         nv(jspin),DIMENSION%neigd,DIMENSION%nbasfcn,zMat,nslibd,
1605
     >         k1(:,jspin),k2(:,jspin),k3(:,jspin),
1606
     >         nv(jspin),zMat,nslibd,
1607
     >         nbnd,
1608
     >         stars%rgphs,stars%ustep,stars%ig,(/ 0,0,0 /),
1609 1610 1611 1612 1613
     <         mmn(:,:,ikpt))

c---> spherical contribution to mmn0-matrix

       call wann_mmk0_sph(
1614
     >           atoms%llod,noccbd,atoms%nlod,atoms%nat,atoms%ntype,
1615 1616
     >           atoms%lmaxd,atoms%lmax,lmd,atoms%ntype,atoms%neq,
     >           atoms%nlo,atoms%llo,acof(1:noccbd,:,:),
1617
     >           bcof(1:noccbd,:,:),ccof(:,1:noccbd,:,:),
1618 1619
     >           usdus%ddn(:,:,jspin),usdus%uulon(:,:,jspin),
     >           usdus%dulon(:,:,jspin),usdus%uloulopn,
1620 1621 1622
     =           mmn(:,:,ikpt))
c---> vacuum contribution to mmn0-matrix

1623
       if (input%film .and. .not.oneD%odi%d1) then
1624

1625
            call wann_mmk0_vac(
1626
     >           noco%l_noco,nlotot,qpt_i,
1627 1628
     >           cell%z1,vacuum%nmzd,DIMENSION%nv2d,
     >           stars%mx1,stars%mx2,stars%mx3,
1629 1630 1631 1632 1633
     >           stars%ng3,vacuum%nvac,stars%ig,vacuum%nmz,vacuum%delz,
     >           stars%ig2,cell%area,cell%bmat,
     >           cell%bbmat,evac(:,jspin),bkpt,vz(:,:,jspin2),
     >           nslibd,jspin,k1,k2,k3,wannierspin,DIMENSION%nvd,
     >           DIMENSION%nbasfcn,DIMENSION%neigd,zMat,nv,cell%omtil,
1634
     <           mmn(:,:,ikpt))
1635
       elseif (oneD%odi%d1) then
1636

1637
             call wann_mmk0_od_vac(
1638
     >           DIMENSION, oneD, vacuum, stars, cell,
1639 1640
     >           noco%l_noco,nlotot,
     >           cell%z1,vacuum%nmzxyd,vacuum%nmzd,DIMENSION%nv2d,
1641
     >           stars%mx1,stars%mx2,stars%mx3,stars%ng2,stars%ng3,
1642
     >           stars%ig,vacuum%nmzxy,vacuum%nmz,vacuum%delz,stars%ig2,
1643 1644
     >           oneD%odi%n2d,cell%bbmat,evac(1,jspin),bkpt,oneD%odi%M,
     >           oneD%odi%mb,vz(:,1,jspin2),oneD%odi,
1645 1646 1647
     >           nslibd,jspin,k1,k2,k3,wannierspin,DIMENSION%nvd,
     >           cell%area,DIMENSION%nbasfcn,DIMENSION%neigd,zMat,nv,
     >           stars%sk2,stars%phi2,cell%omtil,qpt_i,
1648 1649 1650 1651 1652
     <           mmn(:,:,ikpt))

       endif
       endif !l_mmn0

1653 1654
!       call cpu_time(tt3)

Markus Betzinger's avatar