type_mixvector.F90 26.5 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
Matthias Redies's avatar
Matthias Redies committed
7 8 9
   !TODO!!!
   ! LDA+U
   ! Noco (third spin)
10
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
11
   use mpi
12
#endif
Matthias Redies's avatar
Matthias Redies committed
13 14 15
   USE m_types
   IMPLICIT NONE

Matthias Redies's avatar
Matthias Redies committed
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 42 43 44 45 46 47 48
   PRIVATE
   !Here we store the pointers used for metric
   TYPE(t_stars), POINTER  :: stars
   TYPE(t_cell), POINTER   :: cell
   TYPE(t_sphhar), POINTER :: sphhar
   TYPE(t_atoms), POINTER  :: atoms => NULL()
   TYPE(t_sym), POINTER    :: sym => NULL()
   INTEGER                :: jspins, nvac
   LOGICAL                :: l_noco, invs, invs2, l_mtnocopot, l_mperp
   INTEGER                :: pw_length !The shape of the local arrays
   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
   INTEGER                :: misc_length = 0, misc_length_g
   INTEGER                :: misc_start(3) = 0, 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(:)

   TYPE, PUBLIC:: t_mixvector
      REAL, ALLOCATABLE       :: vec_pw(:)
      REAL, ALLOCATABLE       :: vec_mt(:)
      REAL, ALLOCATABLE       :: vec_vac(:)
      REAL, ALLOCATABLE       :: vec_misc(:)
Daniel Wortmann's avatar
Daniel Wortmann committed
49
   CONTAINS
Matthias Redies's avatar
Matthias Redies committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      PROCEDURE :: alloc => mixvector_alloc
      PROCEDURE :: from_density => mixvector_from_density
      PROCEDURE :: to_density => mixvector_to_density
      PROCEDURE :: apply_metric => mixvector_metric
      PROCEDURE :: multiply_dot_mask
      PROCEDURE :: read_unformatted
      PROCEDURE :: write_unformatted
      PROCEDURE :: allocated => mixvector_allocated
   END TYPE t_mixvector

   INTERFACE OPERATOR(*)
      MODULE PROCEDURE multiply_scalar
      MODULE PROCEDURE multiply_scalar_spin
   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.)

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

Daniel Wortmann's avatar
Daniel Wortmann committed
77 78
CONTAINS

Matthias Redies's avatar
Matthias Redies 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 125 126 127 128 129 130 131 132 133 134 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 179 180 181 182 183 184 185 186 187 188 189 190 191 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 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 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
   SUBROUTINE READ_unformatted(this, unit)
      IMPLICIT NONE
      CLASS(t_mixvector), INTENT(INOUT)::this
      INTEGER, INTENT(IN)::unit

      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)
      IMPLICIT NONE
      CLASS(t_mixvector), INTENT(IN)::this
      INTEGER, INTENT(IN)::unit
      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

   SUBROUTINE mixvector_reset()
      IMPLICIT NONE
      atoms => NULL()
      sym => NULL()
      IF (ALLOCATED(g_mt)) DEALLOCATE (g_mt)
      IF (ALLOCATED(g_vac)) DEALLOCATE (g_vac)
      IF (ALLOCATED(g_misc)) DEALLOCATE (g_misc)
   END SUBROUTINE mixvector_reset

   SUBROUTINE mixvector_from_density(vec, den, swapspin)
      USE m_types
      IMPLICIT NONE
      CLASS(t_mixvector), INTENT(INOUT)    :: vec
      TYPE(t_potden), INTENT(inout)    :: Den
      LOGICAL, INTENT(IN), OPTIONAL         :: swapspin
      INTEGER:: js, ii, n, l, iv, j, mmpSize
      CALL den%DISTRIBUTE(mix_mpi_comm)
      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 - 1) = REAL(den%pw(:, j))
               IF ((.NOT. sym%invs) .OR. (js == 3)) THEN
                  vec%vec_pw(pw_start(js) + stars%ng3:pw_start(js) + 2*stars%ng3 - 1) = AIMAG(den%pw(:, j))
               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))
                  IF ((.NOT. sym%invs2) .OR. (js == 3)) 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
                  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
               ENDDO
            ENDIF
            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(sym%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(sym%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 (misc_here .AND. (js < 3 .OR. l_mperp)) THEN
               mmpSize = SIZE(den%mmpMat(:, :, 1:atoms%n_u, j))
               vec%vec_misc(misc_start(js):misc_start(js) + mmpSize - 1) = RESHAPE(REAL(den%mmpMat(:, :, 1:atoms%n_u, j)), (/mmpSize/))
               vec%vec_misc(misc_start(js) + mmpSize:misc_start(js) + 2*mmpSize - 1) = RESHAPE(AIMAG(den%mmpMat(:, :, 1:atoms%n_u, j)), (/mmpSize/))
            END IF
         END IF
      END DO

   END SUBROUTINE mixvector_from_density

   SUBROUTINE mixvector_to_density(vec, den)
      USE m_types
      IMPLICIT NONE
      CLASS(t_mixvector), INTENT(IN)    :: vec
      TYPE(t_potden), INTENT(INOUT) :: Den
      INTEGER:: js, ii, n, l, iv, mmpSize

      DO js = 1, MERGE(jspins, 3,.NOT. l_noco)
         IF (spin_here(js)) THEN
            !PW part
            IF (pw_here) THEN
               IF (sym%invs .AND. js < 3) THEN
                  den%pw(:, js) = vec%vec_pw(pw_start(js):pw_start(js) + stars%ng3 - 1)
               ELSE
                  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))
               ENDIF
            ENDIF
            IF (mt_here .AND. (js < 3 .OR. l_mtnocopot)) 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(sym%ntypsy(SUM(atoms%neq(:n - 1)) + 1))
                     den%mt(:atoms%jri(n), l, n, js) = vec%vec_mt(ii:ii + atoms%jri(n) - 1)
                     ii = ii + atoms%jri(n)
                  ENDDO
               ENDDO
               IF (js == 3) THEN !Imaginary part comes as 4th spin
                  DO n = mt_rank + 1, atoms%ntype, mt_size
                     DO l = 0, sphhar%nlh(sym%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
            ENDIF
            IF (vac_here) THEN
               !This PE stores vac-data
               ii = vac_start(js) - 1
               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 (sym%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)))
                     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
                  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
               ENDDO
            ENDIF
            IF (misc_here .AND. (js < 3 .OR. l_mperp)) THEN
               mmpSize = SIZE(den%mmpMat(:, :, 1:atoms%n_u, js))
               den%mmpMat(:, :, 1:atoms%n_u, js) = RESHAPE(CMPLX(vec%vec_misc(misc_start(js):misc_start(js) + mmpSize - 1), &
                                                                 vec%vec_misc(misc_start(js) + mmpSize:misc_start(js) + 2*mmpSize - 1)), &
                                                           SHAPE(den%mmpMat(:, :, 1:atoms%n_u, js)))
            END IF
         END IF
      ENDDO
      CALL den%collect(mix_mpi_comm)

   END SUBROUTINE mixvector_to_density

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

      INTEGER:: js, ii, n, l, iv
      COMPLEX, ALLOCATABLE::pw(:), pw_w(:)
      mvec = vec
      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 (sym%invs .AND. js < 3) THEN
                  pw(:) = vec%vec_pw(pw_start(js):pw_start(js) + stars%ng3 - 1)
               ELSE
                  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))
               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 - 1) = REAL(pw_w)
               IF ((.NOT. sym%invs) .OR. (js == 3)) THEN
                  mvec%vec_pw(pw_start(js) + stars%ng3:pw_start(js) + 2*stars%ng3 - 1) = AIMAG(pw_w)
               ENDIF
            ENDIF
            IF (mt_here .AND. (js < 3 .OR. l_mtnocopot)) THEN
               !This PE stores some(or all) MT data
               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
            ENDIF
            IF (vac_here) THEN
               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
            ENDIF
            IF (misc_here .AND. (js < 3 .OR. l_mperp)) THEN
               mvec%vec_misc(misc_start(js):misc_stop(js)) = g_misc*vec%vec_misc(misc_start(js):misc_stop(js))
            END IF
         ENDIF
      END DO

   END FUNCTION mixvector_metric

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

      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_g))
         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(sym%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 (sym%invs2) iv2 = 1

         ALLOCATE (g_vac(vac_length_g), wght(vacuum%nmzd))
         g_vac(:) = 0.0
         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, stars%ng2 - 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))
         g_misc = 1.0
      END IF

   END SUBROUTINE init_metric

Matthias Redies's avatar
Matthias Redies committed
396
   SUBROUTINE init_storage_mpi(comm_mpi)
Matthias Redies's avatar
Matthias Redies committed
397
      IMPLICIT NONE
Matthias Redies's avatar
Matthias Redies committed
398
      INTEGER, INTENT(in):: comm_mpi
Matthias Redies's avatar
Matthias Redies committed
399
      INTEGER      :: irank, isize, err, js, new_comm
Matthias Redies's avatar
Matthias Redies committed
400
      mix_mpi_comm = comm_mpi
Daniel Wortmann's avatar
Daniel Wortmann committed
401 402
#ifdef CPP_MPI

Matthias Redies's avatar
Matthias Redies committed
403 404
      CALL mpi_comm_rank(comm_mpi, irank, err)
      CALL mpi_comm_size(comm_mpi, isize, err)
Matthias Redies's avatar
Matthias Redies committed
405 406 407 408

      IF (isize == 1) RETURN !No parallelization
      js = MERGE(jspins, 3,.NOT. l_noco)!distribute spins
      js = MIN(js, isize)
Matthias Redies's avatar
Matthias Redies committed
409
      CALL judft_comm_split(comm_mpi, MOD(irank, js), irank, new_comm)
Matthias Redies's avatar
Matthias Redies committed
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
      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 > 0)
      vac_here = vac_here .AND. (irank > 0)
      misc_here = misc_here .AND. (irank > 0)
      isize = isize - 1
      irank = irank - 1
      mt_rank = irank
      mt_size = isize
      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 > 0)
         isize = isize - 1
         irank = irank - 1
      ENDIF
      mt_rank = irank
      mt_size = isize
Daniel Wortmann's avatar
Daniel Wortmann committed
437
#endif
Matthias Redies's avatar
Matthias Redies committed
438 439
   END SUBROUTINE init_storage_mpi

Matthias Redies's avatar
Matthias Redies committed
440
   SUBROUTINE mixvector_init(comm_mpi, l_densitymatrix, oneD, input, vacuum, noco, stars_i, cell_i, sphhar_i, atoms_i, sym_i)
Matthias Redies's avatar
Matthias Redies committed
441 442
      USE m_types
      IMPLICIT NONE
Matthias Redies's avatar
Matthias Redies committed
443
      INTEGER, INTENT(IN)               :: comm_mpi
Matthias Redies's avatar
Matthias Redies committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
      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_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
      TYPE(t_sym), INTENT(IN), TARGET    :: sym_i

      INTEGER::js, n, len

      !Store pointers to data-types
      IF (ASSOCIATED(atoms)) RETURN !was done before...
      jspins = input%jspins
      nvac = vacuum%nvac
      l_noco = noco%l_noco
      l_mperp = noco%l_mperp
      l_mtnocopot = noco%l_mtnocopot
      stars => stars_i; cell => cell_i; sphhar => sphhar_i; atoms => atoms_i; sym => sym_i

      vac_here = input%film
      misc_here = l_densitymatrix
Matthias Redies's avatar
Matthias Redies committed
468
      CALL init_storage_mpi(comm_mpi)
Matthias Redies's avatar
Matthias Redies committed
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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

      pw_length = 0; mt_length = 0; vac_length = 0; misc_length = 0
      mt_length_g = 0; vac_length_g = 0; misc_length_g = 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 (sym%invs .AND. js < 3) 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
               IF (js < 3 .OR. noco%l_mtnocopot) mt_start(js) = mt_length + 1
               len = 0
               !This PE stores some(or all) MT data
               DO n = mt_rank + 1, atoms%ntype, mt_size
                  len = len + (sphhar%nlh(sym%ntypsy(SUM(atoms%neq(:n - 1)) + 1)) + 1)*atoms%jri(n)
               ENDDO
               mt_length_g = MAX(len, mt_length_g)
               IF (js == 3) THEN
                  !need to store imaginary part as well...
                  DO n = mt_rank + 1, atoms%ntype, mt_size
                     len = len + (sphhar%nlh(sym%ntypsy(SUM(atoms%neq(:n - 1)) + 1)) + 1)*atoms%jri(n)
                  ENDDO
               ENDIF
               IF (js < 3 .OR. noco%l_mtnocopot) mt_length = mt_length + len
               mt_stop(js) = mt_length
            END IF
            IF (vac_here) THEN
               !This PE stores vac-data
               vac_start(js) = vac_length + 1
               len = 0
               IF (sym%invs2 .AND. js < 3) THEN
                  len = len + vacuum%nmzxyd*(stars%ng2 - 1)*vacuum%nvac + vacuum%nmzd*vacuum%nvac
               ELSE
                  len = len + 2*vacuum%nmzxyd*(stars%ng2 - 1)*vacuum%nvac + vacuum%nmzd*vacuum%nvac
               ENDIF
               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
               vac_stop(js) = vac_length
            ENDIF
            IF (misc_here .AND. (js < 3 .OR. l_mperp)) THEN
               len = 7*7*2*atoms%n_u
               misc_start(js) = misc_length + 1
               misc_length = misc_length + len
               misc_stop(js) = misc_length
               misc_length_g = MAX(len, misc_length_g)
            END IF
         END IF
      END DO
      CALL init_metric(vacuum, stars)
   END SUBROUTINE mixvector_init
   SUBROUTINE mixvector_alloc(vec)
      IMPLICIT NONE
      CLASS(t_mixvector), INTENT(OUT)    :: vec
      ALLOCATE (vec%vec_pw(pw_length))
      ALLOCATE (vec%vec_mt(mt_length))
      ALLOCATE (vec%vec_vac(vac_length))
      ALLOCATE (vec%vec_misc(misc_length))
   END SUBROUTINE mixvector_alloc

   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_mt
      vecout%vec_vac = vecout%vec_vac + vec2%vec_vac
      vecout%vec_misc = vecout%vec_misc + vec2%vec_misc
   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_mt
      vecout%vec_vac = vecout%vec_vac - vec2%vec_vac
      vecout%vec_misc = vecout%vec_misc - vec2%vec_misc
   END FUNCTION subtract_vectors

   FUNCTION multiply_dot(vec1, vec2) RESULT(dprod)
      TYPE(t_mixvector), INTENT(IN)::vec1, vec2
      REAL                        ::dprod, dprod_tmp
      INTEGER                     ::ierr
      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)
Daniel Wortmann's avatar
Daniel Wortmann committed
599
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
600 601
      CALL MPI_ALLREDUCE(dprod, dprod_tmp, 1, MPI_DOUBLE_PRECISION, MPI_SUM, mix_mpi_comm, ierr)
      dprod = dprod_tmp
602
#endif
Matthias Redies's avatar
Matthias Redies committed
603
   END FUNCTION multiply_dot
604

Matthias Redies's avatar
Matthias Redies committed
605 606 607 608 609 610
   FUNCTION multiply_dot_mask(vec1, vec2, mask, spin) RESULT(dprod)
      CLASS(t_mixvector), INTENT(IN)::vec1
      TYPE(t_mixvector), INTENT(IN)::vec2
      LOGICAL, INTENT(IN)          ::mask(4)
      INTEGER, INTENT(IN)          ::spin
      REAL                        ::dprod, dprod_tmp
611

Matthias Redies's avatar
Matthias Redies committed
612
      INTEGER:: js, ierr
613

Matthias Redies's avatar
Matthias Redies committed
614
      dprod = 0.0
615

Matthias Redies's avatar
Matthias Redies committed
616 617
      DO js = 1, 3
         IF (mask(1) .AND. (spin == js .OR. spin == 0) .AND. pw_start(js) > 0) &
618
            dprod = dprod + DOT_PRODUCT(vec1%vec_pw(pw_start(js):pw_stop(js)), &
Matthias Redies's avatar
Matthias Redies committed
619 620
                                        vec2%vec_pw(pw_start(js):pw_stop(js)))
         IF (mask(2) .AND. (spin == js .OR. spin == 0) .AND. mt_start(js) > 0) &
621
            dprod = dprod + DOT_PRODUCT(vec1%vec_mt(mt_start(js):mt_stop(js)), &
Matthias Redies's avatar
Matthias Redies committed
622 623
                                        vec2%vec_mt(mt_start(js):mt_stop(js)))
         IF (mask(3) .AND. (spin == js .OR. spin == 0) .AND. vac_start(js) > 0) &
624
            dprod = dprod + DOT_PRODUCT(vec1%vec_vac(vac_start(js):vac_stop(js)), &
Matthias Redies's avatar
Matthias Redies committed
625 626
                                        vec2%vec_vac(vac_start(js):vac_stop(js)))
         IF (mask(4) .AND. (spin == js .OR. spin == 0) .AND. misc_start(js) > 0) &
627
            dprod = dprod + DOT_PRODUCT(vec1%vec_misc(misc_start(js):misc_stop(js)), &
Matthias Redies's avatar
Matthias Redies committed
628 629
                                        vec2%vec_misc(misc_start(js):misc_stop(js)))
      ENDDO
Daniel Wortmann's avatar
Daniel Wortmann committed
630

Daniel Wortmann's avatar
Daniel Wortmann committed
631
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
632 633
      CALL MPI_ALLREDUCE(dprod, dprod_tmp, 1, MPI_DOUBLE_PRECISION, MPI_SUM, mix_mpi_comm, ierr)
      dprod = dprod_tmp
Daniel Wortmann's avatar
Daniel Wortmann committed
634
#endif
Matthias Redies's avatar
Matthias Redies committed
635 636 637 638 639 640 641 642 643 644 645 646
   END FUNCTION multiply_dot_mask

   FUNCTION mixvector_allocated(self) RESULT(l_array)
      IMPLICIT NONE
      CLASS(t_mixvector), INTENT(in) :: self
      LOGICAL, ALLOCATABLE :: l_array(:)

      l_array = [ALLOCATED(self%vec_pw), &
                 ALLOCATED(self%vec_mt), &
                 ALLOCATED(self%vec_vac), &
                 ALLOCATED(self%vec_misc)]
   END FUNCTION mixvector_allocated
647
END MODULE m_types_mixvector