type_mixvector.F90 22.8 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 11
  !TODO!!!
  ! LDA+U
  ! Noco (third spin)

  
12 13 14 15 16 17 18
  use m_types
  implicit none
  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
19 20
  TYPE(t_atoms),POINTER  :: atoms  =>null()
  INTEGER                :: jspins,nvac
21
  LOGICAL                :: l_noco,invs,invs2,l_mtnocopot
Daniel Wortmann's avatar
Daniel Wortmann committed
22
  INTEGER                :: pw_length !The shape of the local arrays
23 24 25 26 27
  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
Daniel Wortmann's avatar
Daniel Wortmann committed
28
  INTEGER                :: misc_length=0
29
  INTEGER                :: misc_start(3)=0,misc_stop(3) !First and last index for spin
Daniel Wortmann's avatar
Daniel Wortmann committed
30 31 32 33 34 35 36 37 38 39
  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(:)
40 41
  
  TYPE,PUBLIC:: t_mixvector
Daniel Wortmann's avatar
Daniel Wortmann committed
42 43 44 45
     REAL,ALLOCATABLE       :: vec_pw(:)
     REAL,ALLOCATABLE       :: vec_mt(:)
     REAL,ALLOCATABLE       :: vec_vac(:)
     REAL,ALLOCATABLE       :: vec_misc(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
46
   CONTAINS
47 48 49 50
     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
51
     PROCEDURE :: multiply_dot_mask
Daniel Wortmann's avatar
Daniel Wortmann committed
52 53 54 55
  END TYPE t_mixvector

  INTERFACE OPERATOR (*)
     MODULE PROCEDURE multiply_scalar
Daniel Wortmann's avatar
Daniel Wortmann committed
56
     MODULE PROCEDURE multiply_scalar_spin
Daniel Wortmann's avatar
Daniel Wortmann committed
57 58 59 60 61 62 63 64 65 66
  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.)
67

Daniel Wortmann's avatar
Daniel Wortmann committed
68
  PUBLIC :: OPERATOR(+),OPERATOR(-),OPERATOR(*),OPERATOR(.dot.)
69
  PUBLIC :: mixvector_init,mixvector_reset
Daniel Wortmann's avatar
Daniel Wortmann committed
70

Daniel Wortmann's avatar
Daniel Wortmann committed
71 72
CONTAINS

73 74 75 76 77 78 79
  SUBROUTINE mixvector_reset()
    IMPLICIT NONE
    atoms=>NULL()
    DEALLOCATE(g_mt)
    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
80 81 82

  
  SUBROUTINE mixvector_from_density(vec,den,swapspin)
Daniel Wortmann's avatar
Daniel Wortmann committed
83 84 85
    USE m_types
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(INOUT)    :: vec
Daniel Wortmann's avatar
Daniel Wortmann committed
86
    TYPE(t_potden),    INTENT(inout)    :: Den
Daniel Wortmann's avatar
Daniel Wortmann committed
87 88
    LOGICAL,INTENT(IN),OPTIONAL         :: swapspin
    INTEGER:: js,ii,n,l,iv,j
Daniel Wortmann's avatar
Daniel Wortmann committed
89
    call den%distribute(mix_mpi_comm)
Daniel Wortmann's avatar
Daniel Wortmann committed
90 91 92 93 94 95 96 97
    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
98
             vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)=REAL(den%pw(:,j))
99
             IF ((.NOT.invs).or.(js==3)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
100
                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
101 102 103 104 105 106 107 108 109 110
             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))
111
                IF ((.NOT.invs2).or.(js==3))THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
112 113 114
                   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
115 116 117 118
                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
119 120
             ENDDO
          ENDIF
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
          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
141
          IF (misc_here) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
142 143
             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
144 145 146
          END IF
       END IF
    END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
147 148 149 150 151 152 153

  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
154 155
    TYPE(t_potden),    INTENT(INOUT) :: Den
     INTEGER:: js,ii,n,l,iv
Daniel Wortmann's avatar
Daniel Wortmann committed
156

Daniel Wortmann's avatar
Daniel Wortmann committed
157 158 159 160
     DO js=1,MERGE(jspins,3,.NOT.l_noco)
        IF (spin_here(js)) THEN
           !PW part
           IF (pw_here) THEN
161
              IF (invs.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
162
                 den%pw(:,js)=vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
163
              ELSE
Daniel Wortmann's avatar
Daniel Wortmann committed
164
                 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
165 166
              ENDIF
           ENDIF
167
           IF (mt_here.AND.(js<3.OR.l_mtnocopot)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
168 169 170 171
              !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
172
                    den%mt(:atoms%jri(n),l,n,js)=vec%vec_mt(ii:ii+atoms%jri(n)-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
173 174 175
                    ii=ii+atoms%jri(n)
                 ENDDO
              ENDDO
176 177 178 179 180 181 182 183
              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
184 185 186
           ENDIF
           IF (vac_here) THEN
              !This PE stores vac-data
187
              ii=vac_start(js)-1
Daniel Wortmann's avatar
Daniel Wortmann committed
188 189 190
              DO iv=1,nvac
                 den%vacz(:,iv,js)=vec%vec_vac(ii+1:ii+SIZE(den%vacz,1))
                 ii=ii+SIZE(den%vacz,1)
191 192
                 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
193 194
                    ii=ii+SIZE(den%vacxy(:,:,iv,js))
                 ELSE
195 196
                    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
197 198 199
                         SHAPE(den%vacxy(:,:,iv,js)))
                    ii=ii+2*SIZE(den%vacxy(:,:,iv,js))
                 ENDIF
200 201 202 203
                 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
204 205
              ENDDO
           ENDIF
206 207
           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
208 209 210
           END IF
        END IF
     ENDDO
Daniel Wortmann's avatar
Daniel Wortmann committed
211
     call den%collect(mix_mpi_comm)
Daniel Wortmann's avatar
Daniel Wortmann committed
212
    
Daniel Wortmann's avatar
Daniel Wortmann committed
213 214 215 216 217
  END SUBROUTINE mixvector_to_density


  FUNCTION mixvector_metric(vec)RESULT(mvec)
    USE m_types
Daniel Wortmann's avatar
Daniel Wortmann committed
218
    USE m_convol
Daniel Wortmann's avatar
Daniel Wortmann committed
219 220 221 222
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(IN)    :: vec
    TYPE(t_mixvector)                :: mvec

Daniel Wortmann's avatar
Daniel Wortmann committed
223 224
    INTEGER:: js,ii,n,l,iv
    COMPLEX,ALLOCATABLE::pw(:),pw_w(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
225
    mvec=vec
Daniel Wortmann's avatar
Daniel Wortmann committed
226 227 228 229 230 231 232
    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
233
             IF (invs.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
234
                pw(:)=vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)
Daniel Wortmann's avatar
Daniel Wortmann committed
235
             ELSE
236
                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
237 238 239
             ENDIF
             CALL convol(stars,pw_w,pw,stars%ufft)
             pw_w=pw_w*cell%omtil
Daniel Wortmann's avatar
Daniel Wortmann committed
240
             mvec%vec_pw(pw_start(js):pw_start(js)+stars%ng3-1)=REAL(pw_w)
241
             IF ((.NOT.invs).or.(js==3)) THEN 
Daniel Wortmann's avatar
Daniel Wortmann committed
242
                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
243 244
             ENDIF
          ENDIF
245
          IF (mt_here.AND.(js<3.OR.l_mtnocopot)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
246
             !This PE stores some(or all) MT data
247 248 249 250 251
             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
252 253
          ENDIF
          IF (vac_here) THEN
254 255 256 257
             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
258
          ENDIF
259
          IF (misc_here.AND.(js<3)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
260 261
             mvec%vec_misc(misc_start(js):misc_stop(js))=g_misc*vec%vec_misc(misc_start(js):misc_stop(js))
          END IF
262
       ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
263 264
    END DO
   
Daniel Wortmann's avatar
Daniel Wortmann committed
265
  END FUNCTION mixvector_metric
266

Daniel Wortmann's avatar
Daniel Wortmann committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280

  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
281
       ALLOCATE(g_mt(mt_length_g)) 
Daniel Wortmann's avatar
Daniel Wortmann committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
       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

318
       ALLOCATE(g_vac(vac_length_g),wght(vacuum%nmzd))
Daniel Wortmann's avatar
Daniel Wortmann committed
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 353 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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
       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
       ALLOCATE(g_misc(misc_length))
       g_misc=1.0
    END IF
    
    
  END SUBROUTINE init_metric
    
  
  
  SUBROUTINE init_storage_mpi(mpi_comm)
    IMPLICIT NONE
    INTEGER,INTENT(in):: mpi_comm
    mix_mpi_comm=mpi_comm
#ifdef CPP_MPI
    INCLUDE 'mpif.h'
    INTEGER      :: irank,isize,err,js,new_comm

    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
    js=MAX(js,isize)
    CALL MPI_COMM_SPLIT(mpi_comm,MOD(irank,js),irank,new_comm)
    spin_here=(/MOD(irank,js)==0,MOD(irank,js)==1,(isize=2.AND.irank==0).or.MOD(irank,js)==2/)

    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)
    mt_here=(irank>1)
    vac_here=vac_here.AND.(irank>1)
    misc_here=misc_here.AND.(irank>1)
    isize=isize-1
    irank=irank-1
    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)
       mt_here=(irank>1)
       isize=isize-1
       irank=irank-1
    ENDIF
    IF(isize==1.OR.irank<0) RETURN !No further parallelism
    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
416 417
    USE m_types
    IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
418 419 420 421 422 423 424
    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
425 426 427 428 429
    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

Daniel Wortmann's avatar
Daniel Wortmann committed
430 431
    INTEGER::js,n
    
432

Daniel Wortmann's avatar
Daniel Wortmann committed
433
    !Store pointers to data-types
Daniel Wortmann's avatar
Daniel Wortmann committed
434
    if (associated(atoms)) return !was done before...
Daniel Wortmann's avatar
Daniel Wortmann committed
435 436 437
    jspins=input%jspins
    nvac=vacuum%nvac
    l_noco=noco%l_noco
438
    l_mtnocopot=noco%l_mtnocopot
Daniel Wortmann's avatar
Daniel Wortmann committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452
    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
    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
453
             IF (invs.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
454 455 456 457 458 459
                pw_length=pw_length+stars%ng3
             ELSE
                pw_length=pw_length+2*stars%ng3
             ENDIF
          ENDIF
          pw_stop(js)=pw_length
460
          IF (mt_here.AND.(js<3.OR.noco%l_mtnocopot)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
461 462 463
             mt_start(js)=mt_length+1
             !This PE stores some(or all) MT data
             DO n=mt_rank+1,atoms%ntype,mt_size
Daniel Wortmann's avatar
Daniel Wortmann committed
464
                mt_length=mt_length+(sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))+1)*atoms%jri(n)
Daniel Wortmann's avatar
Daniel Wortmann committed
465
             ENDDO
466 467 468 469 470 471
             IF (js==3) THEN
                !need to store imaginary part as well...
                DO n=mt_rank+1,atoms%ntype,mt_size
                   mt_length=mt_length+(sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))+1)*atoms%jri(n)
                ENDDO
             ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
472 473
             mt_stop(js)=mt_length
          END IF
474
          IF (js==1) mt_length_g=mt_length
Daniel Wortmann's avatar
Daniel Wortmann committed
475 476 477
          IF (vac_here) THEN
             !This PE stores vac-data
             vac_start(js)=vac_length+1
478
             IF (invs2.and.js<3) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
479 480 481 482
                vac_length=vac_length+vacuum%nmzxyd * ( oneD%odi%n2d - 1 ) * vacuum%nvac + vacuum%nmzd * vacuum%nvac
             ELSE
                vac_length=vac_length+2*vacuum%nmzxyd * ( oneD%odi%n2d - 1 ) * vacuum%nvac + vacuum%nmzd * vacuum%nvac
             ENDIF
483
             IF (js==3) vac_length=vac_length+vacuum%nmzd * vacuum%nvac !Offdiagnal potential is complex
Daniel Wortmann's avatar
Daniel Wortmann committed
484 485
             vac_stop(js)=vac_length
          ENDIF
486 487
          IF (js==1) vac_length_g=vac_length
          IF (misc_here.AND.(js<3)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
488 489 490 491 492 493 494 495 496
             misc_start(js)=misc_length+1
             misc_length=misc_length+7*7*2*atoms%n_u
             misc_stop(js)=misc_length
          END IF
       END IF
    END DO
    CALL init_metric(oneD,vacuum)
  END SUBROUTINE mixvector_init
  SUBROUTINE mixvector_alloc(vec)
497 498
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(OUT)    :: vec
Daniel Wortmann's avatar
Daniel Wortmann committed
499 500 501 502
    ALLOCATE( vec%vec_pw(pw_length) )
    ALLOCATE( vec%vec_mt(mt_length) )
    ALLOCATE( vec%vec_vac(vac_length) )
    ALLOCATE( vec%vec_misc(misc_length) )   
503 504
  END SUBROUTINE mixvector_alloc

Daniel Wortmann's avatar
Daniel Wortmann committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

    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
547 548 549
      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
550 551 552 553 554 555 556 557
    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
558 559 560
      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
561 562 563 564
    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
565
      REAL                        ::dprod,dprod_tmp
Daniel Wortmann's avatar
Daniel Wortmann committed
566 567 568 569 570
      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
Daniel Wortmann's avatar
Daniel Wortmann committed
571 572
      CALL MPI_ALLREDUCE_ALL(dprod,dprod_tmp,1,MPI_DOUBLE_PRECISION,MPI_SUM,mix_mpi_comm)
      dprod=dprod_tmp
Daniel Wortmann's avatar
Daniel Wortmann committed
573 574 575
#endif      
    END FUNCTION multiply_dot

576
    FUNCTION multiply_dot_mask(vec1,vec2,mask,spin)RESULT(dprod)
Daniel Wortmann's avatar
Daniel Wortmann committed
577 578
      CLASS(t_mixvector),INTENT(IN)::vec1
      TYPE(t_mixvector),INTENT(IN)::vec2
Daniel Wortmann's avatar
Daniel Wortmann committed
579
      LOGICAL,INTENT(IN)          ::mask(4)
580
      INTEGER,INTENT(IN)          ::spin
Daniel Wortmann's avatar
Daniel Wortmann committed
581
      REAL                        ::dprod,dprod_tmp
Daniel Wortmann's avatar
Daniel Wortmann committed
582 583 584

      INTEGER:: js

Daniel Wortmann's avatar
Daniel Wortmann committed
585
      dprod=0.0
586 587

      DO js=1,3
588
         IF (mask(1).and.(spin==js.or.spin==0).and.pw_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
589 590
                 dprod=dprod+dot_PRODUCT(vec1%vec_pw(pw_start(js):pw_stop(js)),&
                 vec2%vec_pw(pw_start(js):pw_stop(js)))
591
         IF (mask(2).and.(spin==js.or.spin==0).and.mt_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
592 593
                 dprod=dprod+dot_PRODUCT(vec1%vec_mt(mt_start(js):mt_stop(js)),&
                 vec2%vec_mt(mt_start(js):mt_stop(js)))
594
         IF (mask(3).and.(spin==js.or.spin==0).and.vac_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
595 596
                 dprod=dprod+dot_PRODUCT(vec1%vec_vac(vac_start(js):vac_stop(js)),&
                 vec2%vec_vac(vac_start(js):vac_stop(js)))
597
         IF (mask(4).and.(spin==js.or.spin==0).and.misc_start(js)>0) &
Daniel Wortmann's avatar
Daniel Wortmann committed
598 599
                 dprod=dprod+dot_PRODUCT(vec1%vec_misc(misc_start(js):misc_stop(js)),&
                 vec2%vec_misc(misc_start(js):misc_stop(js)))
600
      enddo
Daniel Wortmann's avatar
Daniel Wortmann committed
601

Daniel Wortmann's avatar
Daniel Wortmann committed
602
#ifdef CPP_MPI
Daniel Wortmann's avatar
Daniel Wortmann committed
603 604 605
      CALL MPI_ALLREDUCE_ALL(dprod,dprod_tmp,1,MPI_DOUBLE_PRECISION,MPI_SUM,mix_mpi_comm)
      dprod=dprod_tmp
#endif
Daniel Wortmann's avatar
Daniel Wortmann committed
606 607
    END FUNCTION multiply_dot_mask
  end MODULE m_types_mixvector