type_mixvector.F90 19.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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  TYPE(t_atoms),POINTER  :: atoms  =>null()
  INTEGER                :: jspins,nvac
  LOGICAL                :: l_noco,invs,invs2
  INTEGER                :: pw_length !The shape of the local arrays
  INTEGER                :: pw_start(3),pw_stop(3) !First and last index for spin
  INTEGER                :: mt_length
  INTEGER                :: mt_start(3),mt_stop(3) !First and last index for spin
  INTEGER                :: vac_length
  INTEGER                :: vac_start(3),vac_stop(3) !First and last index for spin
  INTEGER                :: misc_length=0
  INTEGER                :: misc_start(3),misc_stop(3) !First and last index for spin
  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 52 53 54
  END TYPE t_mixvector

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

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

Daniel Wortmann's avatar
Daniel Wortmann committed
70 71
CONTAINS

Daniel Wortmann's avatar
Daniel Wortmann committed
72 73 74

  
  SUBROUTINE mixvector_from_density(vec,den,swapspin)
Daniel Wortmann's avatar
Daniel Wortmann committed
75 76 77 78
    USE m_types
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(INOUT)    :: vec
    TYPE(t_potden),    INTENT(in)       :: Den
Daniel Wortmann's avatar
Daniel Wortmann committed
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    LOGICAL,INTENT(IN),OPTIONAL         :: swapspin
    INTEGER:: js,ii,n,l,iv,j
    PRINT *,"TODO MPI distribute density"
    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
             vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3)=REAL(den%pw(:,j))
             IF (.NOT.invs) THEN
                vec%vec_pw(pw_start(js)+stars%ng3+1:pw_start(js)+2*stars%ng3)=AIMAG(den%pw(:,j))
             ENDIF
          ENDIF
          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
          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))
                IF (.NOT.invs2)THEN
                   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
             ENDDO
          ENDIF
          IF (misc_here) THEN
             vec%vec_misc(misc_start(js):misc_start(js)+SIZE(den%mmpMat(:,:,:,j)))=RESHAPE(REAL(den%mmpMat(:,:,:,j)),(/SIZE(den%mmpMat(:,:,:,j))/))
             vec%vec_misc(misc_start(js)+1+SIZE(den%mmpMat(:,:,:,j)):misc_start(js)+2*SIZE(den%mmpMat(:,:,:,j)))=RESHAPE(AIMAG(den%mmpMat(:,:,:,j)),(/SIZE(den%mmpMat(:,:,:,j))/))
          END IF
       END IF
    END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
125 126 127 128 129 130 131

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

Daniel Wortmann's avatar
Daniel Wortmann committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
     DO js=1,MERGE(jspins,3,.NOT.l_noco)
        IF (spin_here(js)) THEN
           !PW part
           IF (pw_here) THEN
              IF (invs) THEN
                 den%pw(:,js)=vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3)
              ELSE
                 den%pw(:,js)=CMPLX(vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3),vec%vec_pw(pw_start(js)+stars%ng3+1:pw_start(js)+2*stars%ng3))
              ENDIF
           ENDIF
           IF (mt_here) THEN
              !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))
                    den%mt(:atoms%jri(n),l,n,js)=vec%vec_mt(ii+1:ii+atoms%jri(n))
                    ii=ii+atoms%jri(n)
                 ENDDO
              ENDDO
           ENDIF
           IF (vac_here) THEN
              !This PE stores vac-data
              ii=vac_start(js)
              DO iv=1,nvac
                 den%vacz(:,iv,js)=vec%vec_vac(ii+1:ii+SIZE(den%vacz,1))
                 ii=ii+SIZE(den%vacz,1)
                 IF (invs2)THEN
                    den%vacxy(:,:,iv,js)=RESHAPE(vec%vec_vac(ii+1:ii+SIZE(den%vacxy(:,:,iv,js))),SHAPE(den%vacxy(:,:,iv,js)))
                    ii=ii+SIZE(den%vacxy(:,:,iv,js))
                 ELSE
                    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)))),&
                         SHAPE(den%vacxy(:,:,iv,js)))
                    ii=ii+2*SIZE(den%vacxy(:,:,iv,js))
                 ENDIF
              ENDDO
           ENDIF
           IF (misc_here) THEN
              den%mmpMat(:,:,:,js)=RESHAPE(CMPLX(vec%vec_misc(misc_start(js):misc_start(js)+SIZE(den%mmpMat(:,:,:,js))),vec%vec_misc(misc_start(js)+SIZE(den%mmpMat(:,:,:,js))+1:misc_start(js)+2*SIZE(den%mmpMat(:,:,:,js)))),SHAPE(den%mmpMat(:,:,:,js)))
           END IF
        END IF
     ENDDO
     PRINT *,"TODO MPI collect density"
    
Daniel Wortmann's avatar
Daniel Wortmann committed
179 180 181 182 183 184 185 186 187 188
  END SUBROUTINE mixvector_to_density


  FUNCTION mixvector_metric(vec)RESULT(mvec)
    USE m_types
    USE m_metric
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(IN)    :: vec
    TYPE(t_mixvector)                :: mvec

Daniel Wortmann's avatar
Daniel Wortmann committed
189 190
    INTEGER:: js,ii,n,l,iv
    COMPLEX,ALLOCATABLE::pw(:),pw_w(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
191
    mvec=vec
Daniel Wortmann's avatar
Daniel Wortmann committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    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
             IF (invs) THEN
                pw(:)=vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3)
             ELSE
                pw(:)=CMPLX(vec%vec_pw(pw_start(js):pw_start(js)+stars%ng3),vec%vec_pw(pw_start(js)+stars%ng3+1:pw_start(js)+2*stars%ng3))
             ENDIF
             CALL convol(stars,pw_w,pw,stars%ufft)
             pw_w=pw_w*cell%omtil
             mvec%vec_pw(pw_start(js):pw_start(js)+stars%ng3)=REAL(pw_w)
             IF (.NOT.invs) THEN
                mvec%vec_pw(pw_start(js)+stars%ng3+1:pw_start(js)+2*stars%ng3)=AIMAG(pw_w)
             ENDIF
          ENDIF
          IF (mt_here) THEN
             !This PE stores some(or all) MT data
             mvec%vec_mt(mt_start(js):mt_stop(js))=g_mt*vec%vec_mt(mt_start(js):mt_stop(js))
          ENDIF
          IF (vac_here) THEN
             mvec%vec_vac(vac_start(js):vac_stop(js))=g_vac*vec%vec_vac(vac_start(js):vac_stop(js))
          ENDIF
          IF (misc_here) THEN
             mvec%vec_misc(misc_start(js):misc_stop(js))=g_misc*vec%vec_misc(misc_start(js):misc_stop(js))
          END IF
       END IF
    END DO
   
Daniel Wortmann's avatar
Daniel Wortmann committed
224
  END FUNCTION mixvector_metric
225

Daniel Wortmann's avatar
Daniel Wortmann committed
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

  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
       ALLOCATE(g_mt(mt_length))
       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

       ALLOCATE(g_vac(vac_length),wght(vacuum%nmzd))
       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
375 376
    USE m_types
    IMPLICIT NONE
Daniel Wortmann's avatar
Daniel Wortmann committed
377 378 379 380 381 382 383
    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
384 385 386 387 388
    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
389 390
    INTEGER::js,n
    
391

Daniel Wortmann's avatar
Daniel Wortmann committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    !Store pointers to data-types
    jspins=input%jspins
    nvac=vacuum%nvac
    l_noco=noco%l_noco
    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
             IF (invs) THEN
                pw_length=pw_length+stars%ng3
             ELSE
                pw_length=pw_length+2*stars%ng3
             ENDIF
          ENDIF
          pw_stop(js)=pw_length
          IF (mt_here) THEN
             mt_start(js)=mt_length+1
             !This PE stores some(or all) MT data
             DO n=mt_rank+1,atoms%ntype,mt_size
                mt_length=mt_length+sphhar%nlh(atoms%ntypsy(SUM(atoms%neq(:n-1))+1))*atoms%jri(n)
             ENDDO
             mt_stop(js)=mt_length
          END IF
          IF (vac_here) THEN
             !This PE stores vac-data
             vac_start(js)=vac_length+1
             IF (invs2) THEN
                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
             vac_stop(js)=vac_length
          ENDIF
          IF (misc_here) THEN
             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)
445 446
    IMPLICIT NONE
    CLASS(t_mixvector),INTENT(OUT)    :: vec
Daniel Wortmann's avatar
Daniel Wortmann committed
447 448 449 450
    ALLOCATE( vec%vec_pw(pw_length) )
    ALLOCATE( vec%vec_mt(mt_length) )
    ALLOCATE( vec%vec_vac(vac_length) )
    ALLOCATE( vec%vec_misc(misc_length) )   
451 452
  END SUBROUTINE mixvector_alloc

Daniel Wortmann's avatar
Daniel Wortmann committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

    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
      vecout%vec_mt=vecout%vec_mt+vec2%vec_pw
      vecout%vec_vac=vecout%vec_vac+vec2%vec_pw
      vecout%vec_misc=vecout%vec_misc+vec2%vec_pw
    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
      vecout%vec_mt=vecout%vec_mt-vec2%vec_pw
      vecout%vec_vac=vecout%vec_vac-vec2%vec_pw
      vecout%vec_misc=vecout%vec_misc-vec2%vec_pw
    END FUNCTION subtract_vectors
    
    FUNCTION multiply_dot(vec1,vec2)RESULT(dprod)
      TYPE(t_mixvector),INTENT(IN)::vec1,vec2
      REAL                        ::dprod
      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
      CALL MPI_REDUCE_ALL()
#endif      
    END FUNCTION multiply_dot

523
    FUNCTION multiply_dot_mask(vec1,vec2,mask,spin)RESULT(dprod)
Daniel Wortmann's avatar
Daniel Wortmann committed
524 525
      TYPE(t_mixvector),INTENT(IN)::vec1,vec2
      LOGICAL,INTENT(IN)          ::mask(4)
526
      INTEGER,INTENT(IN)          ::spin
Daniel Wortmann's avatar
Daniel Wortmann committed
527 528
      REAL                        ::dprod
      dprod=0.0
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

      DO js=1,3
         IF (mask(1).and.(spin==js.or.spin==0.and.start_pw(js)>0)) &
                 dprod=dprod+dot_PRODUCT(vec1%vec_pw(start_pw(js):stop_pw(js)),&
                 vec2%vec_pw(start_pw(js):stop_pw(js)))
         IF (mask(2).and.(spin==js.or.spin==0.and.start_mt(js)>0)) &
                 dprod=dprod+dot_PRODUCT(vec1%vec_mt(start_mt(js):stop_mt(js)),&
                 vec2%vec_mt(start_mt(js):stop_mt(js)))
         IF (mask(3).and.(spin==js.or.spin==0.and.start_vac(js)>0)) &
                 dprod=dprod+dot_PRODUCT(vec1%vec_vac(start_vac(js):stop_vac(js)),&
                 vec2%vec_vac(start_vac(js):stop_vac(js)))
         IF (mask(4).and.(spin==js.or.spin==0.and.start_misc(js)>0)) &
                 dprod=dprod+dot_PRODUCT(vec1%vec_misc(start_misc(js):stop_misc(js)),&
                 vec2%vec_misc(start_misc(js):stop_misc(js)))
      enddo
Daniel Wortmann's avatar
Daniel Wortmann committed
544 545 546 547 548 549
         
#ifdef CPP_MPI
      CALL MPI_REDUCE_ALL()
#endif      
    END FUNCTION multiply_dot_mask
  end MODULE m_types_mixvector