type_mixvector.F90 24.1 KB
Newer Older
Daniel Wortmann's avatar
Daniel Wortmann committed
1 2 3 4
!--------------------------------------------------------------------------------
! 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.
5 6
!--------------------------------------------------------------------------------
MODULE m_types_mixvector
Daniel Wortmann's avatar
Daniel Wortmann committed
7 8 9 10
  !TODO!!!
  ! LDA+U
  ! Noco (third spin)
  
11 12
  use m_types
  implicit none
13 14 15
#ifdef CPP_MPI
      include 'mpif.h'
#endif
16 17 18 19 20
  PRIVATE
  !Here we store the pointers used for metric
  TYPE(t_stars),POINTER  :: stars
  TYPE(t_cell),POINTER   :: cell
  TYPE(t_sphhar),POINTER :: sphhar
Daniel Wortmann's avatar
Daniel Wortmann committed
21 22
  TYPE(t_atoms),POINTER  :: atoms  =>null()
  INTEGER                :: jspins,nvac
23
  LOGICAL                :: l_noco,invs,invs2,l_mtnocopot
Daniel Wortmann's avatar
Daniel Wortmann committed
24
  INTEGER                :: pw_length !The shape of the local arrays
25 26 27 28 29
  INTEGER                :: pw_start(3)=0,pw_stop(3) !First and last index for spin
  INTEGER                :: mt_length,mt_length_g
  INTEGER                :: mt_start(3)=0,mt_stop(3) !First and last index for spin
  INTEGER                :: vac_length,vac_length_g
  INTEGER                :: vac_start(3)=0,vac_stop(3) !First and last index for spin
30
  INTEGER                :: misc_length=0,misc_length_g
31
  INTEGER                :: misc_start(3)=0,misc_stop(3) !First and last index for spin
Daniel Wortmann's avatar
Daniel Wortmann committed
32 33 34 35 36 37 38 39 40 41
  INTEGER                :: mix_mpi_comm !Communicator for all PEs doing mixing
  LOGICAL                :: spin_here(3)=.TRUE.
  LOGICAL                :: pw_here=.TRUE.
  LOGICAL                :: mt_here=.TRUE.
  LOGICAL                :: vac_here=.TRUE.
  LOGICAL                :: misc_here=.TRUE.
  INTEGER                :: mt_rank=0
  INTEGER                :: mt_size=1
  LOGICAL                :: l_pot=.FALSE. !Is this a potential?
  REAL,ALLOCATABLE       :: g_mt(:),g_vac(:),g_misc(:)
42 43
  
  TYPE,PUBLIC:: t_mixvector
Daniel Wortmann's avatar
Daniel Wortmann committed
44 45 46 47
     REAL,ALLOCATABLE       :: vec_pw(:)
     REAL,ALLOCATABLE       :: vec_mt(:)
     REAL,ALLOCATABLE       :: vec_vac(:)
     REAL,ALLOCATABLE       :: vec_misc(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
48
   CONTAINS
49 50 51 52
     procedure :: alloc=>mixvector_alloc
     PROCEDURE :: from_density=>mixvector_from_density
     PROCEDURE :: to_density=>mixvector_to_density
     PROCEDURE :: apply_metric=>mixvector_metric
Daniel Wortmann's avatar
Daniel Wortmann committed
53
     PROCEDURE :: multiply_dot_mask
54 55 56 57
     PROCEDURE :: read_unformatted
     PROCEDURE :: write_unformatted
     GENERIC :: READ(UNFORMATTED) =>read_unformatted
     GENERIC :: WRITE(UNFORMATTED) =>write_unformatted
Daniel Wortmann's avatar
Daniel Wortmann committed
58 59 60 61
  END TYPE t_mixvector

  INTERFACE OPERATOR (*)
     MODULE PROCEDURE multiply_scalar
Daniel Wortmann's avatar
Daniel Wortmann committed
62
     MODULE PROCEDURE multiply_scalar_spin
Daniel Wortmann's avatar
Daniel Wortmann committed
63 64 65 66 67 68 69 70 71 72
  END INTERFACE OPERATOR (*)
  INTERFACE OPERATOR (+)
     MODULE PROCEDURE add_vectors
  END INTERFACE OPERATOR (+)
  INTERFACE OPERATOR (-)
     MODULE PROCEDURE subtract_vectors
  END INTERFACE OPERATOR (-)
  INTERFACE OPERATOR (.dot.)
     MODULE PROCEDURE multiply_dot
  END INTERFACE OPERATOR (.dot.)
73

Daniel Wortmann's avatar
Daniel Wortmann committed
74
  PUBLIC :: OPERATOR(+),OPERATOR(-),OPERATOR(*),OPERATOR(.dot.)
75
  PUBLIC :: mixvector_init,mixvector_reset
Daniel Wortmann's avatar
Daniel Wortmann committed
76

Daniel Wortmann's avatar
Daniel Wortmann committed
77 78
CONTAINS

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  SUBROUTINE READ_unformatted(this,unit,iostat,iomsg)
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(INOUT)::this
    INTEGER,INTENT(IN)::unit
    INTEGER,INTENT(OUT)::iostat
    CHARACTER(len=*),INTENT(INOUT)::iomsg

    CALL this%alloc()
    IF (pw_here) READ(unit) this%vec_pw
    IF (mt_here) READ(unit) this%vec_mt
    IF (vac_here) READ(unit) this%vec_vac
    IF (misc_here) READ(unit) this%vec_misc
  END SUBROUTINE READ_unformatted

  SUBROUTINE write_unformatted(this,unit,iostat,iomsg)
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(IN)::this
    INTEGER,INTENT(IN)::unit
    INTEGER,INTENT(OUT)::iostat
    CHARACTER(len=*),INTENT(INOUT)::iomsg
    IF (pw_here) WRITE(unit) this%vec_pw
    IF (mt_here) WRITE(unit) this%vec_mt
    IF (vac_here) WRITE(unit) this%vec_vac
    IF (misc_here) WRITE(unit) this%vec_misc
  END SUBROUTINE write_unformatted

  


108 109 110
  SUBROUTINE mixvector_reset()
    IMPLICIT NONE
    atoms=>NULL()
111
    IF (ALLOCATED(g_mt)) DEALLOCATE(g_mt)
112 113 114
    IF (ALLOCATED(g_vac)) DEALLOCATE(g_vac)
    IF (ALLOCATED(g_misc)) DEALLOCATE(g_misc)
  END SUBROUTINE mixvector_reset
Daniel Wortmann's avatar
Daniel Wortmann committed
115 116 117

  
  SUBROUTINE mixvector_from_density(vec,den,swapspin)
Daniel Wortmann's avatar
Daniel Wortmann committed
118 119 120
    USE m_types
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(INOUT)    :: vec
Daniel Wortmann's avatar
Daniel Wortmann committed
121
    TYPE(t_potden),    INTENT(inout)    :: Den
Daniel Wortmann's avatar
Daniel Wortmann committed
122 123
    LOGICAL,INTENT(IN),OPTIONAL         :: swapspin
    INTEGER:: js,ii,n,l,iv,j
Daniel Wortmann's avatar
Daniel Wortmann committed
124
    call den%distribute(mix_mpi_comm)
Daniel Wortmann's avatar
Daniel Wortmann committed
125 126 127 128 129 130 131 132
    DO js=1,MERGE(jspins,3,.NOT.l_noco)
       j=js
       IF (PRESENT(swapspin)) THEN
          IF (swapspin.AND.js<3) j=MERGE(1,2,js==2)
       ENDIF
       IF (spin_here(js)) THEN
          !PW part
          IF (pw_here) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
133
             vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)=REAL(den%pw(:,j))
134
             IF ((.NOT.invs).or.(js==3)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
135
                vec%vec_pw(pw_start(js)+stars%ng3:pw_start(js)+2*stars%ng3-1)=AIMAG(den%pw(:,j))
Daniel Wortmann's avatar
Daniel Wortmann committed
136 137 138 139 140 141 142 143 144 145
             ENDIF
          ENDIF
          IF (vac_here) THEN
             !This PE stores vac-data
             ii=vac_start(js)-1
             DO iv=1,nvac
                vec%vec_vac(ii+1:ii+SIZE(den%vacz,1))=den%vacz(:,iv,j)
                ii=ii+SIZE(den%vacz,1)
                vec%vec_vac(ii+1:ii+SIZE(den%vacxy(:,:,iv,js)))=RESHAPE(REAL(den%vacxy(:,:,iv,j)),(/SIZE(den%vacxy(:,:,iv,j))/))
                ii=ii+SIZE(den%vacxy(:,:,iv,j))
146
                IF ((.NOT.invs2).or.(js==3))THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
147 148 149
                   vec%vec_vac(ii+1:ii+SIZE(den%vacxy(:,:,iv,j)))=RESHAPE(AIMAG(den%vacxy(:,:,iv,j)),(/SIZE(den%vacxy(:,:,iv,j))/))
                   ii=ii+SIZE(den%vacxy(:,:,iv,j))
                ENDIF
150 151 152 153
                IF (js>2)THEN
                   vec%vec_vac(ii+1:ii+SIZE(den%vacz,1))=den%vacz(:,iv,4)
                   ii=ii+SIZE(den%vacz,1)
                ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
154 155
             ENDDO
          ENDIF
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
          IF (js>2.AND..NOT.l_mtnocopot) RETURN
          IF (mt_here) THEN
             !This PE stores some(or all) MT data
             ii=mt_start(js)-1
             DO n=mt_rank+1,atoms%ntype,mt_size
                DO l=0,sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))
                   vec%vec_mt(ii+1:ii+atoms%jri(n))=den%mt(:atoms%jri(n),l,n,j)
                   ii=ii+atoms%jri(n)
                ENDDO
             ENDDO
             IF (js==3) THEN !Imaginary part 
                DO n=mt_rank+1,atoms%ntype,mt_size
                   DO l=0,sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))
                      vec%vec_mt(ii+1:ii+atoms%jri(n))=den%mt(:atoms%jri(n),l,n,4)
                      ii=ii+atoms%jri(n)
                   ENDDO
                ENDDO
             ENDIF
          ENDIF
          IF (js>2) RETURN
Daniel Wortmann's avatar
Daniel Wortmann committed
176
          IF (misc_here) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
177 178
             vec%vec_misc(misc_start(js):misc_start(js)+SIZE(den%mmpMat(:,:,:,j))-1)=RESHAPE(REAL(den%mmpMat(:,:,:,j)),(/SIZE(den%mmpMat(:,:,:,j))/))
             vec%vec_misc(misc_start(js)+SIZE(den%mmpMat(:,:,:,j)):misc_start(js)+2*SIZE(den%mmpMat(:,:,:,j))-1)=RESHAPE(AIMAG(den%mmpMat(:,:,:,j)),(/SIZE(den%mmpMat(:,:,:,j))/))
Daniel Wortmann's avatar
Daniel Wortmann committed
179 180 181
          END IF
       END IF
    END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
182 183 184 185 186 187 188

  END SUBROUTINE mixvector_from_density

  SUBROUTINE mixvector_to_density(vec,den)
    USE m_types
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(IN)    :: vec
Daniel Wortmann's avatar
Daniel Wortmann committed
189 190
    TYPE(t_potden),    INTENT(INOUT) :: Den
     INTEGER:: js,ii,n,l,iv
Daniel Wortmann's avatar
Daniel Wortmann committed
191

Daniel Wortmann's avatar
Daniel Wortmann committed
192 193 194 195
     DO js=1,MERGE(jspins,3,.NOT.l_noco)
        IF (spin_here(js)) THEN
           !PW part
           IF (pw_here) THEN
196
              IF (invs.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
197
                 den%pw(:,js)=vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
198
              ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
199
                 den%pw(:,js)=CMPLX(vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1),vec%vec_pw(pw_start(js)+stars%ng3:pw_start(js)+2*stars%ng3-1))
Daniel Wortmann's avatar
Daniel Wortmann committed
200 201
              ENDIF
           ENDIF
202
           IF (mt_here.AND.(js<3.OR.l_mtnocopot)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
203 204 205 206
              !This PE stores some(or all) MT data
              ii=mt_start(js)
              DO n=mt_rank+1,atoms%ntype,mt_size
                 DO l=0,sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))
Daniel Wortmann's avatar
Daniel Wortmann committed
207
                    den%mt(:atoms%jri(n),l,n,js)=vec%vec_mt(ii:ii+atoms%jri(n)-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
208 209 210
                    ii=ii+atoms%jri(n)
                 ENDDO
              ENDDO
211 212 213 214 215 216 217 218
              IF (js==3) THEN !Imaginary part comes as 4th spin
                 DO n=mt_rank+1,atoms%ntype,mt_size
                    DO l=0,sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))
                       den%mt(:atoms%jri(n),l,n,4)=vec%vec_mt(ii:ii+atoms%jri(n)-1)
                       ii=ii+atoms%jri(n)
                    ENDDO
                 ENDDO
              ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
219 220 221
           ENDIF
           IF (vac_here) THEN
              !This PE stores vac-data
222
              ii=vac_start(js)-1
Daniel Wortmann's avatar
Daniel Wortmann committed
223 224 225
              DO iv=1,nvac
                 den%vacz(:,iv,js)=vec%vec_vac(ii+1:ii+SIZE(den%vacz,1))
                 ii=ii+SIZE(den%vacz,1)
226 227
                 IF (invs2.and.js<3)THEN
                    den%vacxy(:,:,iv,js)=RESHAPE(vec%vec_vac(ii+1:ii+SIZE(den%vacxy(:,:,iv,js))),SHAPE(den%vacxy(:,:,iv,js)))
Daniel Wortmann's avatar
Daniel Wortmann committed
228 229
                    ii=ii+SIZE(den%vacxy(:,:,iv,js))
                 ELSE
230 231
                    den%vacxy(:,:,iv,js)=RESHAPE(CMPLX(vec%vec_vac(ii+1:ii+SIZE(den%vacxy(:,:,iv,js))),&
                         vec%vec_vac(ii+SIZE(den%vacxy(:,:,iv,js))+1:ii+2*SIZE(den%vacxy(:,:,iv,js)))),&
Daniel Wortmann's avatar
Daniel Wortmann committed
232 233 234
                         SHAPE(den%vacxy(:,:,iv,js)))
                    ii=ii+2*SIZE(den%vacxy(:,:,iv,js))
                 ENDIF
235 236 237 238
                 IF (js>2) THEN
                    den%vacz(:,iv,4)=vec%vec_vac(ii+1:ii+SIZE(den%vacz,1))
                    ii=ii+SIZE(den%vacz,1)
              ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
239 240
              ENDDO
           ENDIF
241 242
           IF (misc_here.AND.js<3) THEN
              den%mmpMat(:,:,:,js)=RESHAPE(CMPLX(vec%vec_misc(misc_start(js):misc_start(js)+SIZE(den%mmpMat(:,:,:,js))-1),vec%vec_misc(misc_start(js)+SIZE(den%mmpMat(:,:,:,js)):misc_start(js)+2*SIZE(den%mmpMat(:,:,:,js))-1)),SHAPE(den%mmpMat(:,:,:,js)))
Daniel Wortmann's avatar
Daniel Wortmann committed
243 244 245
           END IF
        END IF
     ENDDO
Daniel Wortmann's avatar
Daniel Wortmann committed
246
     call den%collect(mix_mpi_comm)
Daniel Wortmann's avatar
Daniel Wortmann committed
247
    
Daniel Wortmann's avatar
Daniel Wortmann committed
248 249 250 251 252
  END SUBROUTINE mixvector_to_density


  FUNCTION mixvector_metric(vec)RESULT(mvec)
    USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
253
    USE m_convol
Daniel Wortmann's avatar
Daniel Wortmann committed
254 255 256 257
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(IN)    :: vec
    TYPE(t_mixvector)                :: mvec

Daniel Wortmann's avatar
Daniel Wortmann committed
258 259
    INTEGER:: js,ii,n,l,iv
    COMPLEX,ALLOCATABLE::pw(:),pw_w(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
260
    mvec=vec
Daniel Wortmann's avatar
Daniel Wortmann committed
261 262 263 264 265 266 267
    IF (pw_here) ALLOCATE(pw(stars%ng3),pw_w(stars%ng3))
    
    DO js=1,MERGE(jspins,3,.NOT.l_noco)
       IF (spin_here(js)) THEN
          !PW part
          IF (pw_here) THEN
             !Put back on g-grid and use convol
268
             IF (invs.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
269
                pw(:)=vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
270
             ELSE
271
                pw(:)=CMPLX(vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1),vec%vec_pw(pw_start(js)+stars%ng3:pw_start(js)+2*stars%ng3-1))
Daniel Wortmann's avatar
Daniel Wortmann committed
272 273 274
             ENDIF
             CALL convol(stars,pw_w,pw,stars%ufft)
             pw_w=pw_w*cell%omtil
Daniel Wortmann's avatar
Daniel Wortmann committed
275
             mvec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)=REAL(pw_w)
276
             IF ((.NOT.invs).or.(js==3)) THEN 
Daniel Wortmann's avatar
Daniel Wortmann committed
277
                mvec%vec_pw(pw_start(js)+stars%ng3:pw_start(js)+2*stars%ng3-1)=AIMAG(pw_w)
Daniel Wortmann's avatar
Daniel Wortmann committed
278 279
             ENDIF
          ENDIF
280
          IF (mt_here.AND.(js<3.OR.l_mtnocopot)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
281
             !This PE stores some(or all) MT data
282 283 284 285 286
             mvec%vec_mt(mt_start(js):mt_start(js)+SIZE(g_mt)-1)=g_mt*vec%vec_mt(mt_start(js):mt_start(js)+SIZE(g_mt)-1)
             IF (js==3) THEN    
                !Here we have a the imaginary part as well
                mvec%vec_mt(mt_start(js)+SIZE(g_mt):mt_stop(js))=g_mt*vec%vec_mt(mt_start(js)+SIZE(g_mt):mt_stop(js))
             ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
287 288
          ENDIF
          IF (vac_here) THEN
289 290 291 292
             mvec%vec_vac(vac_start(js):vac_start(js)+SIZE(g_vac)-1)=g_vac*vec%vec_vac(vac_start(js):vac_start(js)+SIZE(g_vac)-1)
             IF (js==3) THEN !We have some extra data that corresponds to first part of metric
                mvec%vec_vac(vac_start(js)+SIZE(g_vac):vac_stop(js))=g_vac(:vac_stop(js)-vac_start(js)-SIZE(g_vac)+1)*vec%vec_vac(vac_start(js)+SIZE(g_vac):vac_stop(js))
             ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
293
          ENDIF
294
          IF (misc_here.AND.(js<3)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
295 296
             mvec%vec_misc(misc_start(js):misc_stop(js))=g_misc*vec%vec_misc(misc_start(js):misc_stop(js))
          END IF
297
       ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
298 299
    END DO
   
Daniel Wortmann's avatar
Daniel Wortmann committed
300
  END FUNCTION mixvector_metric
301

Daniel Wortmann's avatar
Daniel Wortmann committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315

  SUBROUTINE init_metric(oneD,vacuum)
    USE m_metrz0
    IMPLICIT NONE
    TYPE(t_oned),INTENT(in)::oneD
    TYPE(t_vacuum),INTENT(in)::vacuum

    
    INTEGER:: i,n,l,j,ivac,iz,iv2c,k2,iv2
    REAL:: dxn,dxn2,dxn4,dvol,volnstr2
    REAL,allocatable:: wght(:)
    
    IF (mt_here) THEN
       !This PE stores some(or all) MT data
316
       ALLOCATE(g_mt(mt_length_g)) 
Daniel Wortmann's avatar
Daniel Wortmann committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
       i=0
       DO n =mt_rank+1,atoms%ntype,mt_size
          dxn = atoms%neq(n) * atoms%dx(n) / 3.0
          dxn2 =2.0 * dxn
          dxn4 =4.0 * dxn
          DO l = 0, sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))
             i = i + 1
             g_mt(i) = dxn / atoms%rmsh(1,n)
             IF (.NOT.l_pot) THEN
                DO j = 2, atoms%jri(n) - 1, 2
                   i = i + 2
                   g_mt(i-1) = dxn4 / atoms%rmsh(j,n) 
                   g_mt(i) = dxn2 / atoms%rmsh(j+1,n) 
                END DO
                ! CHANGE JR 96/12/01
                ! take care when jri(n) is even
                i = i + 1 - MOD(atoms%jri(n),2)
                g_mt(i) = dxn / atoms%rmsh(atoms%jri(n),n)
             ELSE
                ! for the potential multiply by r^4
                DO j = 2, atoms%jri(n) - 1, 2
                   i = i + 2
                   g_mt(i-1) = dxn4 * atoms%rmsh(j,n)**3 
                   g_mt(i) = dxn2 * atoms%rmsh(j+1,n)**3
                END DO
                i = i + 1 - MOD(atoms%jri(n),2)
                g_mt(i) = dxn * atoms%rmsh(atoms%jri(n),n)**3
             END IF
          END DO
       END DO
    ENDIF
    i=0
    IF (vac_here) THEN
       iv2 = 2
       IF (invs2) iv2 = 1

353
       ALLOCATE(g_vac(vac_length_g),wght(vacuum%nmzd))
Daniel Wortmann's avatar
Daniel Wortmann committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
       dvol = cell%area*vacuum%delz
       ! nvac=1 if (zrfs.or.invs)
       IF (vacuum%nvac.EQ.1) dvol = dvol + dvol
       IF (oneD%odi%d1) dvol = cell%area*vacuum%delz
       DO ivac = 1, vacuum%nvac
          ! G||=0 components
          !
          ! use 7-point simpson integration in accordance to intgz0.f
          ! calculate weights for integration
          CALL metr_z0(vacuum%nmz,wght)
          DO iz = 1, vacuum%nmz
             i = i + 1
             IF (oneD%odi%d1) THEN
                g_vac(i) = wght(iz) * dvol * (cell%z1+(iz-1)*vacuum%delz)
             ELSE
                g_vac(i) = wght(iz) * dvol
             END IF
          END DO
          ! G||.ne.0 components
          !
          ! calculate weights for integration
          CALL metr_z0(vacuum%nmzxy,wght)
          DO iv2c = 1, iv2
             DO k2 = 1, oneD%odi%nq2 - 1
                IF (oneD%odi%d1) THEN
                   DO iz = 1,vacuum%nmzxy
                      i = i + 1
                      g_vac(i) = wght(iz) * oneD%odi%nst2(k2) * dvol * (cell%z1+(iz-1)*vacuum%delz)
                   END DO
                ELSE
                   volnstr2 = dvol * stars%nstr2(k2)
                   DO iz = 1, vacuum%nmzxy
                      i = i + 1
                      g_vac(i) = wght(iz) * volnstr2
                   END DO
                END IF
             END DO
          END DO
       END DO
    END IF
    IF (misc_here) THEN
395
       ALLOCATE(g_misc(misc_length_g))
Daniel Wortmann's avatar
Daniel Wortmann committed
396 397 398 399 400 401 402 403 404 405 406
       g_misc=1.0
    END IF
    
    
  END SUBROUTINE init_metric
    
  
  
  SUBROUTINE init_storage_mpi(mpi_comm)
    IMPLICIT NONE
    INTEGER,INTENT(in):: mpi_comm
407
    INTEGER      :: irank,isize,err,js,new_comm
Daniel Wortmann's avatar
Daniel Wortmann committed
408 409 410 411 412 413 414 415
    mix_mpi_comm=mpi_comm
#ifdef CPP_MPI

    CALL mpi_comm_rank(mpi_comm,irank,err)
    CALL mpi_comm_size(mpi_comm,isize,err)

    IF (isize==1) RETURN !No parallelization
    js=MERGE(jspins,3,.NOT.l_noco)!distribute spins
416 417 418
    js=MIN(js,isize)
    CALL MPI_COMM_SPLIT(mpi_comm,MOD(irank,js),irank,new_comm,err)
    spin_here=(/MOD(irank,js)==0,MOD(irank,js)==1,(isize==2.AND.irank==0).or.MOD(irank,js)==2/)
Daniel Wortmann's avatar
Daniel Wortmann committed
419 420 421 422 423 424 425 426 427

    CALL mpi_comm_rank(new_comm,irank,err)
    CALL mpi_comm_size(new_comm,isize,err)
    CALL mpi_comm_free(new_comm,err)

    !Now distribute data   
    IF(isize==1) return !No further parallelism
    !Split off the pw-part
    pw_here=(irank==0)
428 429 430
    mt_here=(irank>0)
    vac_here=vac_here.AND.(irank>0)
    misc_here=misc_here.AND.(irank>0)
Daniel Wortmann's avatar
Daniel Wortmann committed
431 432
    isize=isize-1
    irank=irank-1
433 434
    mt_rank=irank
    mt_size=isize
Daniel Wortmann's avatar
Daniel Wortmann committed
435 436 437 438
    IF(isize==1.OR.irank<0) RETURN !No further parallelism
    IF (vac_here.OR.misc_here) THEN !split off-vacuum&misc part
       vac_here=vac_here.AND.(irank==0)
       misc_here=misc_here.AND.(irank==0)
439
       mt_here=(irank>0)
Daniel Wortmann's avatar
Daniel Wortmann committed
440 441 442 443 444 445 446 447 448 449 450
       isize=isize-1
       irank=irank-1
    ENDIF
    mt_rank=irank
    mt_size=isize
#endif
  END SUBROUTINE init_storage_mpi
      

  
  SUBROUTINE mixvector_init(mpi_comm,l_densitymatrix,oneD,input,vacuum,noco,sym,stars_i,cell_i,sphhar_i,atoms_i)
Daniel Wortmann's avatar
Daniel Wortmann committed
451 452
    USE m_types
    IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
453 454 455 456 457 458 459
    INTEGER,INTENT(IN)               :: mpi_comm
    LOGICAL,INTENT(IN)               :: l_densitymatrix
    TYPE(t_oneD),INTENT(IN)          :: oneD
    TYPE(t_input),INTENT(IN)         :: input
    TYPE(t_vacuum),INTENT(IN),TARGET :: vacuum
    TYPE(t_noco),INTENT(IN)          :: noco
    TYPE(t_sym),INTENT(IN)           :: sym
460 461 462 463 464
    TYPE(t_stars),INTENT(IN),TARGET  :: stars_i
    TYPE(t_cell),INTENT(IN),TARGET   :: cell_i
    TYPE(t_sphhar),INTENT(IN),TARGET :: sphhar_i
    TYPE(t_atoms),INTENT(IN),TARGET  :: atoms_i

465
    INTEGER::js,n,len
Daniel Wortmann's avatar
Daniel Wortmann committed
466
    
467

Daniel Wortmann's avatar
Daniel Wortmann committed
468
    !Store pointers to data-types
Daniel Wortmann's avatar
Daniel Wortmann committed
469
    if (associated(atoms)) return !was done before...
Daniel Wortmann's avatar
Daniel Wortmann committed
470 471 472
    jspins=input%jspins
    nvac=vacuum%nvac
    l_noco=noco%l_noco
473
    l_mtnocopot=noco%l_mtnocopot
Daniel Wortmann's avatar
Daniel Wortmann committed
474 475 476 477 478 479 480 481 482
    invs=sym%invs
    invs2=sym%invs2
    stars=>stars_i;cell=>cell_i;sphhar=>sphhar_i;atoms=>atoms_i
    
    vac_here=input%film
    misc_here=l_densitymatrix
    CALL init_storage_mpi(mpi_comm)
    
    pw_length=0;mt_length=0;vac_length=0;misc_length=0
483
    mt_length_g=0;vac_length_g=0;misc_length_g=0
Daniel Wortmann's avatar
Daniel Wortmann committed
484 485 486 487 488
    DO js=1,MERGE(jspins,3,.NOT.l_noco)
       IF (spin_here(js)) THEN
          !Now calculate the length of the vectors
          IF (pw_here) THEN
             pw_start(js)=pw_length+1
489
             IF (invs.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
490 491 492 493 494 495
                pw_length=pw_length+stars%ng3
             ELSE
                pw_length=pw_length+2*stars%ng3
             ENDIF
          ENDIF
          pw_stop(js)=pw_length
496 497 498
          IF (mt_here) THEN
             IF (js<3.OR.noco%l_mtnocopot) mt_start(js)=mt_length+1
             len=0
Daniel Wortmann's avatar
Daniel Wortmann committed
499 500
             !This PE stores some(or all) MT data
             DO n=mt_rank+1,atoms%ntype,mt_size
501
                len=len+(sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))+1)*atoms%jri(n)
Daniel Wortmann's avatar
Daniel Wortmann committed
502
             ENDDO
503
             mt_length_g=MAX(len,mt_length_g)
504 505 506
             IF (js==3) THEN
                !need to store imaginary part as well...
                DO n=mt_rank+1,atoms%ntype,mt_size
507
                   len=len+(sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))+1)*atoms%jri(n)
508 509
                ENDDO
             ENDIF
510
             IF (js<3.OR.noco%l_mtnocopot) mt_length=mt_length+len
Daniel Wortmann's avatar
Daniel Wortmann committed
511 512 513 514 515
             mt_stop(js)=mt_length
          END IF
          IF (vac_here) THEN
             !This PE stores vac-data
             vac_start(js)=vac_length+1
516
             len=0
517
             IF (invs2.and.js<3) THEN
518
                len=len+vacuum%nmzxyd * ( oneD%odi%n2d - 1 ) * vacuum%nvac + vacuum%nmzd * vacuum%nvac
Daniel Wortmann's avatar
Daniel Wortmann committed
519
             ELSE
520
                len=len+2*vacuum%nmzxyd * ( oneD%odi%n2d - 1 ) * vacuum%nvac + vacuum%nmzd * vacuum%nvac
Daniel Wortmann's avatar
Daniel Wortmann committed
521
             ENDIF
522 523 524
             vac_length_g=MAX(vac_length_g,len)
             IF (js==3) len=len+vacuum%nmzd * vacuum%nvac !Offdiagnal potential is complex
             vac_length=vac_length+len
Daniel Wortmann's avatar
Daniel Wortmann committed
525 526
             vac_stop(js)=vac_length
          ENDIF
527
          IF (misc_here.AND.(js<3)) THEN
528
             len = 7*7*2*atoms%n_u
Daniel Wortmann's avatar
Daniel Wortmann committed
529
             misc_start(js)=misc_length+1
530
             misc_length = misc_length + len
Daniel Wortmann's avatar
Daniel Wortmann committed
531
             misc_stop(js)=misc_length
532
             misc_length_g = MAX(len,misc_length_g)
Daniel Wortmann's avatar
Daniel Wortmann committed
533 534 535 536 537 538
          END IF
       END IF
    END DO
    CALL init_metric(oneD,vacuum)
  END SUBROUTINE mixvector_init
  SUBROUTINE mixvector_alloc(vec)
539 540
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(OUT)    :: vec
Daniel Wortmann's avatar
Daniel Wortmann committed
541 542 543 544
    ALLOCATE( vec%vec_pw(pw_length) )
    ALLOCATE( vec%vec_mt(mt_length) )
    ALLOCATE( vec%vec_vac(vac_length) )
    ALLOCATE( vec%vec_misc(misc_length) )   
545 546
  END SUBROUTINE mixvector_alloc

Daniel Wortmann's avatar
Daniel Wortmann committed
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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

    FUNCTION multiply_scalar(scalar,vec)RESULT(vecout)
      TYPE(t_mixvector),INTENT(IN)::vec
      REAL,INTENT(IN)             ::scalar
      TYPE(t_mixvector)           ::vecout

      vecout=vec
      vecout%vec_pw=vecout%vec_pw*scalar
      vecout%vec_mt=vecout%vec_mt*scalar
      vecout%vec_vac=vecout%vec_vac*scalar
      vecout%vec_misc=vecout%vec_misc*scalar
    END FUNCTION multiply_scalar


    FUNCTION multiply_scalar_spin(scalar,vec)RESULT(vecout)
      TYPE(t_mixvector),INTENT(IN)::vec
      REAL,INTENT(IN)             ::scalar(:)
      TYPE(t_mixvector)           ::vecout

      INTEGER:: js
      REAL:: fac
      
      vecout=vec
      DO js=1,MERGE(jspins,3,.NOT.l_noco)
         IF (SIZE(scalar)<js) THEN
            fac=0.0
         ELSE
            fac=scalar(js)
         ENDIF
         IF (pw_start(js)>0) vecout%vec_pw(pw_start(js):pw_stop(js))=vecout%vec_pw(pw_start(js):pw_stop(js))*fac
         IF (mt_start(js)>0)vecout%vec_mt(mt_start(js):mt_stop(js))=vecout%vec_mt(mt_start(js):mt_stop(js))*fac
         IF (vac_start(js)>0)vecout%vec_vac(vac_start(js):vac_stop(js))=vecout%vec_vac(vac_start(js):vac_stop(js))*fac
         IF (misc_start(js)>0)vecout%vec_misc(misc_start(js):misc_stop(js))=vecout%vec_misc(misc_start(js):misc_stop(js))*fac
      END DO
    END FUNCTION multiply_scalar_spin

    FUNCTION add_vectors(vec1,vec2)RESULT(vecout)
      TYPE(t_mixvector),INTENT(IN)::vec1,vec2
      TYPE(t_mixvector)           ::vecout
      
      vecout=vec1
      vecout%vec_pw=vecout%vec_pw+vec2%vec_pw
Daniel Wortmann's avatar
Daniel Wortmann committed
589 590 591
      vecout%vec_mt=vecout%vec_mt+vec2%vec_mt
      vecout%vec_vac=vecout%vec_vac+vec2%vec_vac
      vecout%vec_misc=vecout%vec_misc+vec2%vec_misc
Daniel Wortmann's avatar
Daniel Wortmann committed
592 593 594 595 596 597 598 599
    END FUNCTION add_vectors
    
    FUNCTION subtract_vectors(vec1,vec2)RESULT(vecout)
      TYPE(t_mixvector),INTENT(IN)::vec1,vec2
      TYPE(t_mixvector)           ::vecout
      
      vecout=vec1
      vecout%vec_pw=vecout%vec_pw-vec2%vec_pw
Daniel Wortmann's avatar
Daniel Wortmann committed
600 601 602
      vecout%vec_mt=vecout%vec_mt-vec2%vec_mt
      vecout%vec_vac=vecout%vec_vac-vec2%vec_vac
      vecout%vec_misc=vecout%vec_misc-vec2%vec_misc
Daniel Wortmann's avatar
Daniel Wortmann committed
603 604 605 606
    END FUNCTION subtract_vectors
    
    FUNCTION multiply_dot(vec1,vec2)RESULT(dprod)
      TYPE(t_mixvector),INTENT(IN)::vec1,vec2
Daniel Wortmann's avatar
Daniel Wortmann committed
607
      REAL                        ::dprod,dprod_tmp
608
      integer                     ::ierr
Daniel Wortmann's avatar
Daniel Wortmann committed
609 610 611 612 613
      dprod=dot_PRODUCT(vec1%vec_pw,vec2%vec_pw)
      dprod=dprod+dot_PRODUCT(vec1%vec_mt,vec2%vec_mt)
      dprod=dprod+dot_PRODUCT(vec1%vec_vac,vec2%vec_vac)
      dprod=dprod+dot_PRODUCT(vec1%vec_misc,vec2%vec_misc)
#ifdef CPP_MPI
614
      CALL MPI_ALLREDUCE(dprod,dprod_tmp,1,MPI_DOUBLE_PRECISION,MPI_SUM,mix_mpi_comm,ierr)
Daniel Wortmann's avatar
Daniel Wortmann committed
615
      dprod=dprod_tmp
Daniel Wortmann's avatar
Daniel Wortmann committed
616 617 618
#endif      
    END FUNCTION multiply_dot

619
    FUNCTION multiply_dot_mask(vec1,vec2,mask,spin)RESULT(dprod)
Daniel Wortmann's avatar
Daniel Wortmann committed
620 621
      CLASS(t_mixvector),INTENT(IN)::vec1
      TYPE(t_mixvector),INTENT(IN)::vec2
Daniel Wortmann's avatar
Daniel Wortmann committed
622
      LOGICAL,INTENT(IN)          ::mask(4)
623
      INTEGER,INTENT(IN)          ::spin
Daniel Wortmann's avatar
Daniel Wortmann committed
624
      REAL                        ::dprod,dprod_tmp
Daniel Wortmann's avatar
Daniel Wortmann committed
625

626
      INTEGER:: js,ierr
Daniel Wortmann's avatar
Daniel Wortmann committed
627

Daniel Wortmann's avatar
Daniel Wortmann committed
628
      dprod=0.0
629 630

      DO js=1,3
631
         IF (mask(1).and.(spin==js.or.spin==0).and.pw_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
632 633
                 dprod=dprod+dot_PRODUCT(vec1%vec_pw(pw_start(js):pw_stop(js)),&
                 vec2%vec_pw(pw_start(js):pw_stop(js)))
634
         IF (mask(2).and.(spin==js.or.spin==0).and.mt_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
635 636
                 dprod=dprod+dot_PRODUCT(vec1%vec_mt(mt_start(js):mt_stop(js)),&
                 vec2%vec_mt(mt_start(js):mt_stop(js)))
637
         IF (mask(3).and.(spin==js.or.spin==0).and.vac_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
638 639
                 dprod=dprod+dot_PRODUCT(vec1%vec_vac(vac_start(js):vac_stop(js)),&
                 vec2%vec_vac(vac_start(js):vac_stop(js)))
640
         IF (mask(4).and.(spin==js.or.spin==0).and.misc_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
641 642
                 dprod=dprod+dot_PRODUCT(vec1%vec_misc(misc_start(js):misc_stop(js)),&
                 vec2%vec_misc(misc_start(js):misc_stop(js)))
643
      enddo
Daniel Wortmann's avatar
Daniel Wortmann committed
644

Daniel Wortmann's avatar
Daniel Wortmann committed
645
#ifdef CPP_MPI
646
      CALL MPI_ALLREDUCE(dprod,dprod_tmp,1,MPI_DOUBLE_PRECISION,MPI_SUM,mix_mpi_comm,ierr)
Daniel Wortmann's avatar
Daniel Wortmann committed
647 648
      dprod=dprod_tmp
#endif
Daniel Wortmann's avatar
Daniel Wortmann committed
649 650
    END FUNCTION multiply_dot_mask
  end MODULE m_types_mixvector