types_greensf.f90 19.5 KB
Newer Older
1 2
MODULE m_types_greensf

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   !------------------------------------------------------------------------------
   !
   ! MODULE: m_types_greensf
   !
   !> @author
   !> Henning Janßen
   !
   ! DESCRIPTION: 
   !>  Contains a type for onsite and intersite green's functions in the mt-sphere
   !>  It stores the energy contour in the complex plane and the corresponding   
   !>  matrix elements of the green's function
   !>  We have the following cases
   !>    -onsite
   !>       -we look at l=l' but m\=m'
   !>       -we treat non-magnetic/collinear and noco (not tested)
   !>       -we look at r=r' and spherically averaged gf
   !>    -intersite
   !>       -l\=l' and m\=m'
   !>       -r\=r' (not stored we calculate the gf by calling calc_intersite in m_intersite for specific r and r')
   !------------------------------------------------------------------------------

24 25 26 27
   IMPLICIT NONE

   PRIVATE

28
      TYPE t_greensf
29

30
         !Energy contour parameters
31 32
         INTEGER  :: mode  !Determines the shape of the contour (more information in kkintgr.f90)
         INTEGER  :: nz    !number of points in the contour
Henning Janssen's avatar
Henning Janssen committed
33
         INTEGER  :: nmatsub
34

35 36 37
         !array for energy contour
         COMPLEX, ALLOCATABLE  :: e(:)  !energy points
         COMPLEX, ALLOCATABLE  :: de(:) !weights for integration
38

39
         !Arrays for Green's function
40 41 42 43 44 45 46
         COMPLEX, ALLOCATABLE :: gmmpMat(:,:,:,:,:,:) 

         !for radial dependence
         COMPLEX, ALLOCATABLE :: uu(:,:,:,:,:,:)
         COMPLEX, ALLOCATABLE :: dd(:,:,:,:,:,:)
         COMPLEX, ALLOCATABLE :: du(:,:,:,:,:,:)
         COMPLEX, ALLOCATABLE :: ud(:,:,:,:,:,:)
47 48

         
49

50 51
         CONTAINS
            PROCEDURE, PASS :: init => greensf_init
Henning Janssen's avatar
Henning Janssen committed
52
            PROCEDURE       :: e_contour
53 54
            PROCEDURE       :: get_gf
            PROCEDURE       :: set_gf
55 56
      END TYPE t_greensf

57

58 59 60 61
   PUBLIC t_greensf

   CONTAINS

62
      SUBROUTINE greensf_init(thisGREENSF,input,lmax,atoms,noco,nz_in,e_in,de_in,matsub_in)
63

64
         USE m_juDFT
65 66
         USE m_types_setup

67 68 69 70 71
         CLASS(t_greensf),    INTENT(INOUT)  :: thisGREENSF
         TYPE(t_atoms),       INTENT(IN)     :: atoms
         TYPE(t_input),       INTENT(IN)     :: input
         INTEGER,             INTENT(IN)     :: lmax
         TYPE(t_noco),        INTENT(IN)     :: noco
72
         !Pass a already calculated energy contour to the type 
73
         INTEGER, OPTIONAL,   INTENT(IN)     :: nz_in
Henning Janssen's avatar
Henning Janssen committed
74
         INTEGER, OPTIONAL,   INTENT(IN)     :: matsub_in
75 76
         COMPLEX, OPTIONAL,   INTENT(IN)     :: e_in(:)
         COMPLEX, OPTIONAL,   INTENT(IN)     :: de_in(:)
77

Henning Janssen's avatar
Henning Janssen committed
78
         INTEGER i,j,r_dim,l_dim,spin_dim
79
         REAL    tol,n
80
         LOGICAL l_new
81 82


83
         !IF(noco%l_mperp) CALL juDFT_error("NOCO + gf not implemented",calledby="greensf_init")
84

85
         !
86
         !Set up general parameters for the Green's function
87 88 89 90
         !
         !
         !Setting up parameters for the energy contour
         !
Henning Janssen's avatar
Henning Janssen committed
91
         thisGREENSF%mode     = input%gf_mode
92
         IF(PRESENT(nz_in)) THEN
Henning Janssen's avatar
Henning Janssen committed
93 94
            thisGREENSF%nz = nz_in 
            thisGREENSF%nmatsub = matsub_in
95 96 97 98
         ELSE
            !Parameters for the energy contour in the complex plane

            IF(thisGREENSF%mode.EQ.1) THEN
Henning Janssen's avatar
Henning Janssen committed
99 100
                  thisGREENSF%nz = input%gf_n1+input%gf_n2+input%gf_n3+input%gf_nmatsub
                  thisGREENSF%nmatsub = input%gf_nmatsub
Henning Janssen's avatar
Henning Janssen committed
101
            ELSE IF(thisGREENSF%mode.GE.2) THEN
Henning Janssen's avatar
Henning Janssen committed
102 103
               thisGREENSF%nz = input%gf_n
               thisGREENSF%nmatsub = 0
Henning Janssen's avatar
Henning Janssen committed
104
            ENDIF
105
         END IF
106

107 108
         ALLOCATE (thisGREENSF%e(thisGREENSF%nz))
         ALLOCATE (thisGREENSF%de(thisGREENSF%nz))
109

110 111 112 113 114 115 116 117
         IF(PRESENT(e_in)) THEN
            thisGREENSF%e(:) = e_in(:)
            thisGREENSF%de(:)= de_in(:)
         ELSE
            !If no energy contour is given it is set up to zero
            thisGREENSF%e(:) = CMPLX(0.0,0.0)
            thisGREENSF%de(:)= CMPLX(0.0,0.0)
         END IF
118 119


120
         IF(atoms%n_gf.GT.0) THEN !Are there Green's functions to be calculated?
121 122 123 124
            spin_dim = MERGE(3,input%jspins,input%l_gfmperp)
            ALLOCATE (thisGREENSF%gmmpMat(thisGREENSF%nz,MAX(1,atoms%n_gf),-lmax:lmax,-lmax:lmax,spin_dim,2))
            thisGREENSF%gmmpMat = 0.0
            IF(.NOT.input%l_gfsphavg) THEN
125 126 127 128
               ALLOCATE (thisGREENSF%uu(thisGREENSF%nz,MAX(1,atoms%n_gf),-lmax:lmax,-lmax:lmax,spin_dim,2))
               ALLOCATE (thisGREENSF%dd(thisGREENSF%nz,MAX(1,atoms%n_gf),-lmax:lmax,-lmax:lmax,spin_dim,2))
               ALLOCATE (thisGREENSF%du(thisGREENSF%nz,MAX(1,atoms%n_gf),-lmax:lmax,-lmax:lmax,spin_dim,2))
               ALLOCATE (thisGREENSF%ud(thisGREENSF%nz,MAX(1,atoms%n_gf),-lmax:lmax,-lmax:lmax,spin_dim,2))
129 130 131 132
               thisGREENSF%uu = 0.0
               thisGREENSF%dd = 0.0
               thisGREENSF%du = 0.0
               thisGREENSF%ud = 0.0
133
            ENDIF
134
         ENDIF
135

136 137
      END SUBROUTINE greensf_init

138
      SUBROUTINE e_contour(this,input,mpi,eb,et,ef)
139 140


Henning Janssen's avatar
Henning Janssen committed
141
         USE m_types_setup
142
         USE m_types_mpi
143 144
         USE m_constants
         USE m_juDFT
Henning Janssen's avatar
Henning Janssen committed
145
         USE m_grule
Henning Janssen's avatar
Henning Janssen committed
146 147
         USE m_ExpSave

148 149 150
         IMPLICIT NONE

         CLASS(t_greensf),  INTENT(INOUT)  :: this
Henning Janssen's avatar
Henning Janssen committed
151
         TYPE(t_input),     INTENT(IN)     :: input
152
         TYPE(t_mpi),       INTENT(IN)     :: mpi
153
         REAL,              INTENT(IN)     :: eb  
154
         REAL,              INTENT(IN)     :: et
155
         REAL,              INTENT(IN)     :: ef
Henning Janssen's avatar
Henning Janssen committed
156 157
         
         INTEGER i, j, iz,nz
158

Henning Janssen's avatar
Henning Janssen committed
159 160
         REAL e1, e2, del, sigma
         COMPLEX de
Henning Janssen's avatar
Henning Janssen committed
161 162
         REAL r, xr, xm, c, s, a, b
         REAL x(this%nz), w(this%nz)
163 164 165 166



         IF(this%mode.EQ.1) THEN
Henning Janssen's avatar
Henning Janssen committed
167 168 169
            
            sigma = input%gf_sigma * pi_const

Henning Janssen's avatar
Henning Janssen committed
170
            IF(this%nmatsub > 0) THEN
Henning Janssen's avatar
Henning Janssen committed
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

               nz = 0
               !Left Vertical part (eb,0) -> (eb,sigma)
               de = this%nmatsub * CMPLX(0.0,sigma)
               CALL grule(input%gf_n1,x(1:(input%gf_n1)/2),w(1:(input%gf_n1)/2))
               x = -x
               DO i = 1, (input%gf_n1+3)/2-1
                  x(input%gf_n1-i+1) = -x(i)
                  w(input%gf_n1-i+1) =  w(i)
               ENDDO
               DO iz = 1, input%gf_n1
                  nz = nz + 1
                  IF(nz.GT.this%nz) CALL juDFT_error("Dimension error in energy mesh",calledby="init_e_contour")
                  this%e(nz) = eb + de + de * x(iz) 
                  this%de(nz) = w(iz)*de
               ENDDO

               !Horizontal Part (eb,sigma) -> (et,sigma)
               de = (ef-30*input%gf_sigma-eb)/2.0
               CALL grule(input%gf_n2,x(1:(input%gf_n2)/2),w(1:(input%gf_n2)/2))
               x = -x
               DO i = 1, (input%gf_n2+3)/2-1
                  x(input%gf_n2-i+1) = -x(i)
                  w(input%gf_n2-i+1) =  w(i)
               ENDDO
               DO iz = 1, input%gf_n2
                  nz = nz + 1
                  IF(nz.GT.this%nz) CALL juDFT_error("Dimension error in energy mesh",calledby="init_e_contour")
                  this%e(nz) = de*x(iz) + de + eb + 2 * this%nmatsub * ImagUnit * sigma
                  this%de(nz) = de*w(iz)
               ENDDO

               !Right Vertical part (et,sigma) -> infty
               CALL grule(input%gf_n3,x(1:(input%gf_n3)/2),w(1:(input%gf_n3)/2))
               x = -x
               DO i = 1, (input%gf_n3+3)/2-1
                  x(input%gf_n3-i+1) = -x(i)
                  w(input%gf_n3-i+1) =  w(i)
               ENDDO
               de = 30*input%tkb
               DO iz = 1, input%gf_n3
                  nz = nz + 1
                  IF(nz.GT.this%nz) CALL juDFT_error("Dimension error in energy mesh",calledby="init_e_contour")
                  this%e(nz) = de*x(iz)+ef +  2 * this%nmatsub * ImagUnit * sigma
                  this%de(nz) = w(iz)*de/(1.0+exp_save((REAL(this%e(nz))-ef)/input%gf_sigma))
               ENDDO

               !Matsubara frequencies
               DO iz = this%nmatsub , 1, -1 
                  nz = nz + 1 
                  IF(nz.GT.this%nz) CALL juDFT_error("Dimension error in energy mesh",calledby="init_e_contour")
                  this%e(nz)  = ef + (2*iz-1) * ImagUnit *sigma 
223
                  this%de(nz) =  -2 * ImagUnit * sigma
Henning Janssen's avatar
Henning Janssen committed
224 225
               ENDDO 
            ENDIF
226 227
         ELSE IF(this%mode.EQ.2) THEN

Henning Janssen's avatar
Henning Janssen committed
228
            !Semicircle
229
            e1 = eb
230
            e2 = ef+input%gf_et
Henning Janssen's avatar
Henning Janssen committed
231

Henning Janssen's avatar
Henning Janssen committed
232
            this%nmatsub = 0
233 234 235 236 237
            !Radius
            r  = (e2-e1)*0.5
            !midpoint
            xr = (e2+e1)*0.5

Henning Janssen's avatar
Henning Janssen committed
238
            CALL grule(this%nz,x(1:(this%nz)/2),w(1:(this%nz)/2))
239

Henning Janssen's avatar
Henning Janssen committed
240 241 242 243 244 245 246
            DO i = 1, this%nz/2
               x(this%nz-i+1) = -x(i)
               w(this%nz-i+1) =  w(i)
            ENDDO
            DO i = 1, this%nz
               this%e(i) = xr + ImagUnit * r * EXP(-ImagUnit*pi_const/2.0 * x(this%nz-i+1))
               this%de(i) = pi_const/2.0 * r * EXP(-ImagUnit*pi_const/2.0 * x(this%nz-i+1)) * w(this%nz-i+1)
247 248 249
               !Scale the imaginary part with the given factor alpha
               this%e(i) = REAL(this%e(i)) + ImagUnit * input%gf_alpha * AIMAG(this%e(i))
               this%de(i) = REAL(this%de(i)) + ImagUnit * input%gf_alpha * AIMAG(this%de(i))
250 251
            ENDDO

Henning Janssen's avatar
Henning Janssen committed
252 253 254 255 256 257
         ELSE IF(this%mode.EQ.3) THEN
            !Equidistant contour (without vertical edges)
   
            de = (et-eb)/REAL(this%nz-1)
            DO iz = 1, this%nz
               this%e(iz) = (iz-1) * de + eb + ImagUnit * input%gf_sigma
258 259 260 261 262
               IF(input%gf_dosfermi) THEN
                  this%de(iz) = de * 1.0/(1.0+exp_save((REAL(this%e(iz))-ef)/input%gf_sigma))
               ELSE
                  this%de(iz) = de
               ENDIF
Henning Janssen's avatar
Henning Janssen committed
263 264
            ENDDO

265 266 267 268 269 270
         ELSE

            CALL juDFT_error("Invalid mode for energy contour in Green's function calculation", calledby="init_e_contour")

         END IF

271
         IF(mpi%irank.EQ.0) THEN 
272
            !Write out the information about the energy contour 
273 274 275 276 277 278 279 280 281 282 283 284
            WRITE(6,"(A)") "---------------------------------------------"
            WRITE(6,"(A)") " Green's function energy contour"
            WRITE(6,"(A)") "---------------------------------------------"
            WRITE(6,1000) this%mode
            WRITE(6,*)

            SELECT CASE(this%mode)

            CASE(1)
               WRITE(6,"(A)") "Rectangular Contour: "
               WRITE(6,1010) this%nz, this%nmatsub,input%gf_n1,input%gf_n2,input%gf_n3
               WRITE(6,"(A)") "Energy limits (rel. to fermi energy): "
285
               WRITE(6,1040) eb-ef,0.0
286 287 288 289
            CASE(2)
               WRITE(6,"(A)") "Semicircle Contour: "
               WRITE(6,1020) this%nz, input%gf_alpha
               WRITE(6,"(A)") "Energy limits (rel. to fermi energy): "
290
               WRITE(6,1040) eb-ef,input%gf_et
291 292 293 294
            CASE(3)
               WRITE(6,"(A)") "Equidistant Contour for DOS calculations: "
               WRITE(6,1030) this%nz, input%gf_sigma
               WRITE(6,"(A)") "Energy limits (rel. to fermi energy): "
295
               WRITE(6,1040) eb-ef,et-ef
296 297 298 299 300 301 302 303 304 305 306 307 308 309
            CASE default

            END SELECT

            !Write out points and weights
            WRITE(6,*)
            WRITE(6,"(A)") " Energy points: "
            WRITE(6,"(A)") "---------------------------------------------"
            DO iz = 1, this%nz 
               WRITE(6,1050) REAL(this%e(iz)), AIMAG(this%e(iz)), REAL(this%de(iz)), AIMAG(this%de(iz))
            ENDDO

1000        FORMAT("Using energy contour mode: ", I1)
1010        FORMAT("nz: ", I5.1,"; nmatsub: ", I5.1,"; n1: ", I5.1,"; n2: ", I5.1,"; n3: ", I5.1)
310
1020        FORMAT("nz: ", I5.1," alpha: ", f8.4)
311 312
1030        FORMAT("n: ", I5.1,"; sigma: ", f8.4)
1040        FORMAT("eb: ", f8.4,"; et: ",f8.4)
Henning Janssen's avatar
Henning Janssen committed
313
1050        FORMAT(2f8.4,"      weight: ",2e11.4)
314 315
         ENDIF

Henning Janssen's avatar
Henning Janssen committed
316
      END SUBROUTINE e_contour
317

318
      SUBROUTINE get_gf(this,gmat,atoms,input,iz,l,nType,l_conjg,spin,lp,nTypep,u,udot)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

         USE m_types_mat
         USE m_types_setup
         USE m_juDFT
         USE m_ind_greensf

         !Returns the matrix belonging to energy point iz with l,lp,nType,nTypep
         !when jr (and jrp) are given return for that radial point

         IMPLICIT NONE

         CLASS(t_greensf),    INTENT(IN)  :: this
         TYPE(t_atoms),       INTENT(IN)  :: atoms 
         TYPE(t_input),       INTENT(IN)  :: input
         TYPE(t_mat),         INTENT(OUT) :: gmat !Return matrix 

         INTEGER,             INTENT(IN)  :: iz
         INTEGER,             INTENT(IN)  :: nType
         INTEGER,             INTENT(IN)  :: l 
         LOGICAL,             INTENT(IN)  :: l_conjg
         INTEGER, OPTIONAL,   INTENT(IN)  :: spin
         INTEGER, OPTIONAL,   INTENT(IN)  :: nTypep
         INTEGER, OPTIONAL,   INTENT(IN)  :: lp 
342 343
         REAL   , OPTIONAL,   INTENT(IN)  :: u(2,input%jspins)       !Radial functions at the point where you want to evaluate the greens function 
         REAL   , OPTIONAL,   INTENT(IN)  :: udot(2,input%jspins)
344 345

         INTEGER matsize1,matsize2,i_gf,i,j,ind1,ind2,ind1_start,ind2_start
346
         INTEGER m,mp,spin1,spin2,ipm,ispin,ispin_end,spin_ind
347
         INTEGER lp_loop
348
         LOGICAL l_radial,l_full
349

350
         IF(PRESENT(u).OR.PRESENT(udot).AND.input%l_gfsphavg) THEN
351 352
            CALL juDFT_error("Greens function not calculated for radial dependence", calledby="get_gf")
         ENDIF
353 354 355 356 357 358 359 360

         IF((PRESENT(u).AND..NOT.PRESENT(udot)).OR.&
            (PRESENT(udot).AND..NOT.PRESENT(u))) THEN
            CALL juDFT_error("Not a valid input: Either provide both u and udot or neither of them", calledby="get_gf")
         ENDIF

         l_radial = PRESENT(u).AND.PRESENT(udot)

361 362 363 364
         IF(PRESENT(spin)) THEN
            IF(spin.GT.4.OR.spin.LT.1) THEN
               CALL juDFT_error("Invalid argument for spin",calledby="get_gf")
            ENDIF
365
         END IF
366
         
367
         !Determine matsize for the result gmat (if spin is given only return this diagonal element)
368 369 370
         l_full = .NOT.PRESENT(spin)
         matsize1 = (2*l+1) * MERGE(2,1,l_full)
         
371
         IF(PRESENT(lp)) THEN
372
            matsize2 = (2*lp+1) * MERGE(2,1,l_full)
373 374 375
         ELSE
            matsize2 = matsize1
         ENDIF 
376 377 378
         !If we give no spin argument and we have only one spin in the 
         !calculation we blow up the one spin to the full matrix

379 380 381 382 383 384 385 386 387 388 389 390
         CALL gmat%init(.FALSE.,matsize1,matsize2)

         IF(.NOT.PRESENT(lp)) THEN
            lp_loop = l 
         ELSE 
            lp_loop = lp
         ENDIF

         !Find the index i_gf corresponding to l,lp,nType,nTypep
         i_gf = ind_greensf(atoms,l,nType,lp,nTypep)
         ipm = MERGE(2,1,l_conjg)

391
         gmat%data_c = 0.0
Henning Janssen's avatar
Henning Janssen committed
392
         ispin_end = MERGE(4,2,input%l_gfmperp)
393

394
         DO ispin = MERGE(1,spin,l_full), MERGE(ispin_end,spin,l_full)
395
            !Find the right quadrant in gmat according to the spin index
396 397 398 399
            spin1 = MERGE(ispin,2,ispin.NE.3)
            spin1 = MERGE(spin1,1,ispin.NE.4)
            spin2 = MERGE(ispin,1,ispin.NE.3)
            spin2 = MERGE(spin2,2,ispin.NE.4)
400 401

            spin_ind = MERGE(ispin,1,input%jspins.EQ.2)
Henning Janssen's avatar
Henning Janssen committed
402
            spin_ind = MERGE(3,spin_ind,ispin.EQ.4)
403
            IF(l_full) THEN
404
               ind1_start = (spin1-1)*(2*l+1) 
405
               ind2_start = (spin2-1)*(2*lp_loop+1) 
406 407 408 409 410 411 412 413 414 415
            ELSE 
               ind1_start = 0
               ind2_start = 0
            ENDIF
            ind1 = ind1_start
            DO m = -l,l 
               ind1 = ind1 + 1 
               ind2 = ind2_start
               DO mp = -lp_loop,lp_loop
                  ind2 = ind2 + 1
416
                  IF(l_radial) THEN
417 418 419 420
                     gmat%data_c(ind1,ind2) = this%uu(iz,i_gf,m,mp,spin_ind,ipm) * u(1,spin1)*u(2,spin2) + &
                                              this%dd(iz,i_gf,m,mp,spin_ind,ipm) * udot(1,spin1)*udot(2,spin2) + &
                                              this%du(iz,i_gf,m,mp,spin_ind,ipm) * udot(1,spin1)*u(2,spin2) + &
                                              this%ud(iz,i_gf,m,mp,spin_ind,ipm) * u(1,spin1)*udot(2,spin2) 
421
                  ELSE        
422
                     IF(ispin.EQ.1.AND.input%jspins.EQ.1) THEN
423
                        !In this case the ordering of m and mp has to be reversed
424
                        gmat%data_c(ind1,ind2) = this%gmmpMat(iz,i_gf,-m,-mp,spin_ind,ipm)
Henning Janssen's avatar
Henning Janssen committed
425 426
                     ELSE IF(ispin.EQ.4) THEN
                        gmat%data_c(ind1,ind2) = conjg(this%gmmpMat(iz,i_gf,mp,m,spin_ind,ipm))
427
                     ELSE
428
                        gmat%data_c(ind1,ind2) = this%gmmpMat(iz,i_gf,m,mp,spin_ind,ipm)
429
                     ENDIF
430
                     IF(l_full) gmat%data_c(ind1,ind2) = gmat%data_c(ind1,ind2)/(3-input%jspins) 
431
                  ENDIF
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 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
               ENDDO
            ENDDO
         ENDDO

      END SUBROUTINE get_gf

      SUBROUTINE set_gf(this,gmat,atoms,input,iz,l,nType,l_conjg,spin,lp,nTypep) 

         USE m_types_mat
         USE m_types_setup
         USE m_juDFT
         USE m_ind_greensf

         !Sets the spherically averaged greens function matrix belonging to energy point iz with l,lp,nType,nTypep
         !equal to gmat 

         IMPLICIT NONE

         CLASS(t_greensf),    INTENT(INOUT)  :: this
         TYPE(t_atoms),       INTENT(IN)     :: atoms 
         TYPE(t_input),       INTENT(IN)     :: input
         TYPE(t_mat),         INTENT(IN)     :: gmat  

         INTEGER,             INTENT(IN)     :: iz
         INTEGER,             INTENT(IN)     :: nType
         INTEGER,             INTENT(IN)     :: l 
         LOGICAL,             INTENT(IN)     :: l_conjg
         INTEGER, OPTIONAL,   INTENT(IN)     :: spin
         INTEGER, OPTIONAL,   INTENT(IN)     :: nTypep
         INTEGER, OPTIONAL,   INTENT(IN)     :: lp 

         INTEGER matsize1,matsize2,i_gf,i,j,ind1,ind2,ind1_start,ind2_start
         INTEGER m,mp,spin1,spin2,ipm,ispin,ispin_end
         INTEGER lp_loop

         IF(PRESENT(spin)) THEN
            IF(spin.GT.4.OR.spin.LT.1) THEN
               CALL juDFT_error("Invalid argument for spin",calledby="get_gf")
            ENDIF
         ENDIF
         
         !Determine matsize for the result gmat (if spin is given only return this digonal element)
         matsize1 = (2*l+1) * MERGE(1,input%jspins,PRESENT(spin))
         IF(PRESENT(lp)) THEN
            matsize2 = (2*lp+1) * MERGE(1,input%jspins,PRESENT(spin))
         ELSE
            matsize2 = matsize1
         ENDIF 
         
         !Check the expected matsizes against the actual
         IF(matsize1.NE.gmat%matsize1.OR.matsize2.NE.gmat%matsize2) THEN
            CALL juDFT_error("Mismatch in matsizes", calledby="set_gf")
         ENDIF

         IF(.NOT.PRESENT(lp)) THEN
            lp_loop = l 
         ELSE 
            lp_loop = lp
         ENDIF

         !Find the index i_gf corresponding to l,lp,nType,nTypep
493
         i_gf = ind_greensf(atoms,l,nType,lp=lp,nTypep=nTypep)
494 495
         ipm = MERGE(2,1,l_conjg)

496
         ispin_end = MERGE(3,input%jspins,input%l_gfmperp)
497 498 499 500 501 502 503 504 505

         DO ispin = MERGE(spin,1,PRESENT(spin)), MERGE(spin,ispin_end,PRESENT(spin))
            !Find the right quadrant in gmat according to the spin index
            IF(.NOT.PRESENT(spin)) THEN
               spin1 = MERGE(ispin,1,ispin.NE.3)
               spin1 = MERGE(spin1,2,ispin.NE.4)
               spin2 = MERGE(ispin,2,ispin.NE.3)
               spin2 = MERGE(spin2,1,ispin.NE.4)
               ind1_start = (spin1-1)*(2*l+1) 
506
               ind2_start = (spin2-1)*(2*lp_loop+1) 
507 508 509 510 511 512 513 514 515 516
            ELSE 
               ind1_start = 0
               ind2_start = 0
            ENDIF

            ind1 = ind1_start
            DO m = -l,l 
               ind1 = ind1 + 1 
               ind2 = ind2_start
               DO mp = -lp_loop,lp_loop
517
                  ind2 = ind2 + 1
518 519 520 521 522 523
                  this%gmmpMat(iz,i_gf,m,mp,ispin,ipm) = gmat%data_c(ind1,ind2) 
               ENDDO
            ENDDO
         ENDDO

      END SUBROUTINE set_gf
Henning Janssen's avatar
Henning Janssen committed
524

525
END MODULE m_types_greensf