hsmt_ab.F90 8.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------
MODULE m_hsmt_ab
  use m_juDFT
  implicit none

  INTERFACE hsmt_ab
    module procedure hsmt_ab_cpu
12
#ifdef CPP_GPU
13 14 15 16 17 18 19
    module procedure hsmt_ab_gpu
#endif
  END INTERFACE


CONTAINS

20
#ifdef CPP_GPU
21 22 23 24 25 26 27

  SUBROUTINE hsmt_ab_gpu(sym,atoms,noco,ispin,iintsp,n,na,cell,lapw,fj,gj,ab,ab_size,l_nonsph,abclo,alo1,blo1,clo1)
!Calculate overlap matrix
    USE m_constants, ONLY : fpi_const,tpi_const
    USE m_types
    USE m_ylm
    USE m_apws
28 29
    USE cudafor
    USE nvtx
30 31 32 33 34 35 36 37 38 39 40 41 42
    IMPLICIT NONE
    TYPE(t_sym),INTENT(IN)      :: sym
    TYPE(t_cell),INTENT(IN)     :: cell
    TYPE(t_atoms),INTENT(IN)    :: atoms
    TYPE(t_lapw),INTENT(IN)     :: lapw
    TYPE(t_noco),INTENT(IN)     :: noco
    !     ..
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: ispin,n,na,iintsp
    LOGICAL,INTENT(IN)   :: l_nonsph
    INTEGER,INTENT(OUT)  :: ab_size
    !     ..
    !     .. Array Arguments ..
43
    REAL, DEVICE, INTENT(IN)       :: fj(:,:,:),gj(:,:,:)
44 45 46 47 48 49
    COMPLEX,DEVICE, INTENT (OUT) :: ab(:,:)
    !Optional arguments if abc coef for LOs are needed
    COMPLEX, INTENT(INOUT),OPTIONAL:: abclo(:,-atoms%llod:,:,:)
    REAL,INTENT(IN),OPTIONAL:: alo1(:),blo1(:),clo1(:)
    
    INTEGER:: np,k,l,ll1,m,lmax,nkvec,lo,lm,invsfct
Uliana Alekseeva's avatar
Uliana Alekseeva committed
50 51 52 53 54 55
    REAL   :: th,v(3),bmrot(3,3),vmult(3)
    COMPLEX,ALLOCATABLE :: ylm(:,:)
    COMPLEX,ALLOCATABLE :: c_ph(:,:)
    REAL,   ALLOCATABLE :: gkrot(:,:)
    COMPLEX:: term

56
    REAL,   ALLOCATABLE,DEVICE :: fj_dev(:,:,:), gj_dev(:,:,:)
Uliana Alekseeva's avatar
Uliana Alekseeva committed
57 58
    COMPLEX,ALLOCATABLE,DEVICE :: c_ph_dev(:,:)
    COMPLEX,ALLOCATABLE,DEVICE :: ylm_dev(:,:)
59 60 61
    REAL,   ALLOCATABLE,DEVICE :: gkrot_dev(:,:)
    INTEGER :: istat

Uliana Alekseeva's avatar
Uliana Alekseeva committed
62
 
63 64 65
    call nvtxStartRange("hsmt_ab",2)    
    lmax=MERGE(atoms%lnonsph(n),atoms%lmax(n),l_nonsph)

Uliana Alekseeva's avatar
Uliana Alekseeva committed
66
    ALLOCATE(c_ph_dev(lapw%nv(1),MERGE(2,1,noco%l_ss)))
67 68
    ALLOCATE(ylm_dev((lmax+1)**2,lapw%nv(1)))
    ALLOCATE(gkrot_dev(3,lapw%nv(1)))
69

70
    ALLOCATE(ylm((lmax+1)**2,lapw%nv(1)))
71 72 73 74 75 76 77 78 79 80
    ALLOCATE(c_ph(lapw%nv(1),MERGE(2,1,noco%l_ss)))
    ALLOCATE(gkrot(3,lapw%nv(1)))

    
    ab_size=lmax*(lmax+2)+1
    ab=0.0
    
    np = sym%invtab(atoms%ngopr(na))
    !--->          set up phase factors
    CALL lapw%phase_factors(iintsp,atoms%taual(:,na),noco%qss,c_ph(:,iintsp))
Uliana Alekseeva's avatar
Uliana Alekseeva committed
81 82
    c_ph_dev=c_ph   
 
83 84 85 86 87 88 89 90 91 92 93 94 95
    IF (np==1) THEN
       gkrot(:, 1:lapw%nv(iintsp)) = lapw%gk(:, 1:lapw%nv(iintsp),iintsp)
    ELSE
       bmrot=MATMUL(1.*sym%mrot(:,:,np),cell%bmat)
       DO k = 1,lapw%nv(iintsp)
          !-->  apply the rotation that brings this atom into the
          !-->  representative (this is the definition of ngopr(na)
          !-->  and transform to cartesian coordinates
          v(:) = lapw%vk(:,k,iintsp)
          gkrot(:,k) = MATMUL(TRANSPOSE(bmrot),v)
       END DO
    END IF

96 97 98 99 100 101 102 103 104 105 106
    gkrot_dev = gkrot 


    !-->  synthesize the complex conjugates of a and b

    !!$cuf kernel do <<<*,256>>>
    !DO k = 1,lapw%nv(1)
    !   !-->    generate spherical harmonics
    !   CALL ylm4_dev(lmax,gkrot_dev(:,k),ylm_dev(:,k))
    !ENDDO 

107
    DO k = 1,lapw%nv(1)
108
       call ylm4(lmax,gkrot(:,k),ylm(:,k))
109
    ENDDO
110
    ylm_dev = ylm
111

112
    call nvtxStartRange("hsmt_cuf",5)    
Uliana Alekseeva's avatar
Uliana Alekseeva committed
113 114
    !$cuf kernel do <<<*,256>>>
    DO k = 1,lapw%nv(1)
115 116
       !-->    generate spherical harmonics
       !CALL ylm4_dev(lmax,gkrot_dev(:,k),ylm_dev(:,k))
Uliana Alekseeva's avatar
Uliana Alekseeva committed
117 118 119
       DO l = 0,lmax
          ll1 = l* (l+1)
          DO m = -l,l               
120 121
             ab(k,ll1+m+1)         = CONJG(fj(k,l+1,iintsp)*c_ph_dev(k,iintsp)*ylm_dev(ll1+m+1,k)) 
             ab(k,ll1+m+1+ab_size) = CONJG(gj(k,l+1,iintsp)*c_ph_dev(k,iintsp)*ylm_dev(ll1+m+1,k)) 
Uliana Alekseeva's avatar
Uliana Alekseeva committed
122 123 124
          END DO
       END DO
    ENDDO !k-loop
125 126
    istat = cudaDeviceSynchronize() 
    call nvtxEndRange
127 128

    IF (PRESENT(abclo)) THEN
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
       print*, "Ooooops, TODO in hsmt_ab"
       !DO k = 1,lapw%nv(1)
       !   !determine also the abc coeffs for LOs
       !   invsfct=MERGE(1,2,atoms%invsat(na).EQ.0)
       !   term = fpi_const/SQRT(cell%omtil)* ((atoms%rmt(n)**2)/2)*c_ph(k,iintsp)
       !   DO lo = 1,atoms%nlo(n)
       !      l = atoms%llo(lo,n)
       !      DO nkvec=1,invsfct*(2*l+1)
       !         IF (lapw%kvec(nkvec,lo,na)==k) THEN !This k-vector is used in LO
       !            ll1 = l*(l+1) + 1
       !            DO m = -l,l
       !               lm = ll1 + m
       !               abclo(1,m,nkvec,lo) = term*ylm(k,lm)*alo1(lo)
       !               abclo(2,m,nkvec,lo) = term*ylm(k,lm)*blo1(lo)
       !               abclo(3,m,nkvec,lo) = term*ylm(k,lm)*clo1(lo)
       !            END DO
       !         END IF
       !      ENDDO
       !   ENDDO
       !ENDDO
149 150
    ENDIF
       
151
    ab_size=ab_size*2
152
    
153
    call nvtxEndRange
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
  END SUBROUTINE hsmt_ab_gpu
#endif

  SUBROUTINE hsmt_ab_cpu(sym,atoms,noco,ispin,iintsp,n,na,cell,lapw,fj,gj,ab,ab_size,l_nonsph,abclo,alo1,blo1,clo1)
!Calculate overlap matrix
    USE m_constants, ONLY : fpi_const,tpi_const
    USE m_types
    USE m_ylm
    USE m_apws
    IMPLICIT NONE
    TYPE(t_sym),INTENT(IN)      :: sym
    TYPE(t_cell),INTENT(IN)     :: cell
    TYPE(t_atoms),INTENT(IN)    :: atoms
    TYPE(t_lapw),INTENT(IN)     :: lapw
    TYPE(t_noco),INTENT(IN)     :: noco
    !     ..
    !     .. Scalar Arguments ..
    INTEGER, INTENT (IN) :: ispin,n,na,iintsp
    LOGICAL,INTENT(IN)   :: l_nonsph
    INTEGER,INTENT(OUT)  :: ab_size
    !     ..
    !     .. Array Arguments ..
    REAL,INTENT(IN)       :: fj(:,0:,:),gj(:,0:,:)
    COMPLEX, INTENT (OUT) :: ab(:,:)
    !Optional arguments if abc coef for LOs are needed
    COMPLEX, INTENT(INOUT),OPTIONAL:: abclo(:,-atoms%llod:,:,:)
    REAL,INTENT(IN),OPTIONAL:: alo1(:),blo1(:),clo1(:)
    
    INTEGER:: np,k,l,ll1,m,lmax,nkvec,lo,lm,invsfct
Uliana Alekseeva's avatar
Uliana Alekseeva committed
183 184
    COMPLEX:: term
    REAL   :: th,v(3),bmrot(3,3),vmult(3)
185
    COMPLEX :: ylm((atoms%lmaxd+1)**2)
Uliana Alekseeva's avatar
Uliana Alekseeva committed
186 187
    COMPLEX,ALLOCATABLE:: c_ph(:,:)
    REAL,ALLOCATABLE   :: gkrot(:,:)
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
    LOGICAL :: l_apw
   
    ALLOCATE(c_ph(lapw%nv(1),MERGE(2,1,noco%l_ss)))
    ALLOCATE(gkrot(3,lapw%nv(1)))

    lmax=MERGE(atoms%lnonsph(n),atoms%lmax(n),l_nonsph)
    
    ab_size=lmax*(lmax+2)+1
    l_apw=ALL(gj==0.0)
    ab=0.0
    
    np = sym%invtab(atoms%ngopr(na))
    !--->          set up phase factors
    CALL lapw%phase_factors(iintsp,atoms%taual(:,na),noco%qss,c_ph(:,iintsp))
    
    IF (np==1) THEN
       gkrot(:, 1:lapw%nv(iintsp)) = lapw%gk(:, 1:lapw%nv(iintsp),iintsp)
    ELSE
       bmrot=MATMUL(1.*sym%mrot(:,:,np),cell%bmat)
       DO k = 1,lapw%nv(iintsp)
          !-->  apply the rotation that brings this atom into the
          !-->  representative (this is the definition of ngopr(na)
          !-->  and transform to cartesian coordinates
          v(:) = lapw%vk(:,k,iintsp)
          gkrot(:,k) = MATMUL(TRANSPOSE(bmrot),v)
       END DO
    END IF
    !$OMP PARALLEL DO DEFAULT(none) &
    !$OMP& SHARED(lapw,gkrot,lmax,c_ph,iintsp,ab,fj,gj,abclo,cell,atoms) &
    !$OMP& SHARED(alo1,blo1,clo1,ab_size,na,n) &
    !$OMP& PRIVATE(k,vmult,ylm,l,ll1,m,lm,term,invsfct,lo,nkvec)
    DO k = 1,lapw%nv(1)
       !-->    generate spherical harmonics
       vmult(:) =  gkrot(:,k)
       CALL ylm4(lmax,vmult,ylm)
       !-->  synthesize the complex conjugates of a and b
       DO l = 0,lmax
          ll1 = l* (l+1)
          DO m = -l,l               
             term = c_ph(k,iintsp)*ylm(ll1+m+1)
             ab(k,ll1+m+1)         = fj(k,l,iintsp)*term
             ab(k,ll1+m+1+ab_size) = gj(k,l,iintsp)*term
          END DO
       END DO
       IF (PRESENT(abclo)) THEN
          !determine also the abc coeffs for LOs
          invsfct=MERGE(1,2,atoms%invsat(na).EQ.0)
          term = fpi_const/SQRT(cell%omtil)* ((atoms%rmt(n)**2)/2)*c_ph(k,iintsp)
          DO lo = 1,atoms%nlo(n)
             l = atoms%llo(lo,n)
             DO nkvec=1,invsfct*(2*l+1)
                IF (lapw%kvec(nkvec,lo,na)==k) THEN !This k-vector is used in LO
                   ll1 = l*(l+1) + 1
                   DO m = -l,l
                      lm = ll1 + m
                      abclo(1,m,nkvec,lo) = term*ylm(lm)*alo1(lo)
                      abclo(2,m,nkvec,lo) = term*ylm(lm)*blo1(lo)
                      abclo(3,m,nkvec,lo) = term*ylm(lm)*clo1(lo)
                   END DO
                END IF
             ENDDO
          ENDDO
       ENDIF
       
    ENDDO !k-loop
    !$OMP END PARALLEL DO
    IF (.NOT.l_apw) ab_size=ab_size*2
    
  END SUBROUTINE hsmt_ab_cpu
END MODULE m_hsmt_ab