class_bz_grid.f90 15.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
!
! Copyright (C) 2015-2017 M. Govoni 
! This file is distributed under the terms of the
! GNU General Public License. See the file `License'
! in the root directory of the present distribution,
! or http://www.gnu.org/copyleft/gpl.txt .
!
! This file is part of WEST.
!
! Contributors to this file: 
! Matteo Gerosa
!
!-----------------------------------------------------------------------
MODULE class_bz_grid
!-----------------------------------------------------------------------
   !
   USE kinds,            ONLY : DP
   !
   IMPLICIT NONE
   !
   PRIVATE
   !
   TYPE, PUBLIC :: bz_grid
      !
25
26
27
28
29
30
31
32
33
34
35
      INTEGER :: ngrid(3) = (/ 1, 1, 1 /)       ! number of points in each direction
      INTEGER :: np = 1                         ! total number of points = ngrid(1) * ngrid(2) * ngrid(3) 
      INTEGER :: ns = 1                         ! total number of spin = nspin
      INTEGER :: nps = 1                        ! total number of points and spins = np * ns
      INTEGER,ALLOCATABLE :: ip(:)              ! given ips --> ip   
      INTEGER,ALLOCATABLE :: is(:)              ! given ips --> is   
      REAL(DP), ALLOCATABLE :: p_cryst(:,:)     ! coordinates of point p in crystal                [ 1:np  ] 
      REAL(DP), ALLOCATABLE :: p_cart(:,:)      ! coordinates of point p in cart ( tpiba units )   [ 1:np  ]
      REAL(DP), ALLOCATABLE :: weight(:)        ! weight of point p (sum of weights = nspin)       [ 1:nps ]
      LOGICAL, ALLOCATABLE :: l_pIsGamma(:)     ! .true. if point p = (0,0,0), else .false.        [ 1:np  ]
      !
36
37
      CONTAINS
      !
38
39
      PROCEDURE :: init => k_or_q_grid_init
      PROCEDURE :: find => findp
40
41
42
43
44
45
      !
   END TYPE bz_grid
   !
   CONTAINS
   !
   !
46
47
48
49
   !
   SUBROUTINE k_or_q_grid_init( this, grid_type )
      !
      ! ... grid_type = [ "K", "Q" ]
50
51
52
53
      !
      USE cell_base,        ONLY : at, bg
      USE klist,            ONLY : xk, wk, nkstot
      USE start_k,          ONLY : nk1, nk2, nk3
54
      USE pwcom,            ONLY : nspin
55
      USE control_flags,    ONLY : gamma_only
56
      USE westcom,          ONLY : nq
57
      USE constants,        ONLY : eps8
58
59
60
61
62
      !
      IMPLICIT NONE
      !
      ! I/O
      !
63
      CLASS(bz_grid), INTENT(INOUT) :: this
64
65
66
67
      CHARACTER(LEN=1), INTENT(IN) :: grid_type
      !
      ! Workspace
      !
68
69
      INTEGER :: ip, iq1, iq2, iq3, ips
      INTEGER :: i, j, k
70
71
72
73
      !
      SELECT CASE( grid_type )
      CASE ( 'K', 'k')
         !
74
75
         ! This is a workaround to prevent ngrid(:) to be set to (/ 0, 0, 0 /) in the gamma_only case (espresso default)
         IF ( .NOT. gamma_only ) this%ngrid(1:3) = (/ nk1, nk2, nk3 /) 
76
         this%np = this%ngrid(1) * this%ngrid(2) * this%ngrid(3) 
77
78
         this%ns = nspin 
         this%nps = nkstot   !    =   np * ns  
79
         !
80
         ! generate p-vectors in cart 
81
         !
82
83
84
         ALLOCATE ( this%p_cart (3,this%np) )
         DO ip = 1, this%np
            this%p_cart(:,ip) = xk(:,ip)
85
86
         ENDDO
         !
87
88
89
90
91
92
93
94
         ! generate p-vectors in cryst  
         !
         ALLOCATE ( this%p_cryst  (3,this%np) )
         this%p_cryst(:,:) = this%p_cart(:,:) 
         CALL cryst_to_cart( this%nps, this%p_cryst, at, -1 )
         !
         ! set weights 
         !
95
         ALLOCATE ( this%weight (this%nps) )
96
97
         DO ips = 1, this%nps
           this%weight(ips) = wk(ips)
98
99
100
101
         ENDDO
         !
      CASE ( 'Q', 'q')
         !
102
         this%ngrid(1:3) = nq(1:3) 
103
         this%np = this%ngrid(1) * this%ngrid(2) * this%ngrid(3) 
104
105
         this%ns = 1
         this%nps = this%np 
106
         !
107
         ! generate p-vectors in cryst 
108
         !
109
110
         ALLOCATE ( this%p_cryst  (3,this%np) )
         ip = 0
111
112
113
         DO iq1 = 1, nq(1)
            DO iq2 = 1, nq(2)
               DO iq3 = 1, nq(3)
114
115
116
117
                  ip = ip + 1
                  this%p_cryst(1,ip) = DBLE( iq1 - 1 ) / DBLE( this%ngrid(1) ) 
                  this%p_cryst(2,ip) = DBLE( iq2 - 1 ) / DBLE( this%ngrid(2) )
                  this%p_cryst(3,ip) = DBLE( iq3 - 1 ) / DBLE( this%ngrid(3) )
118
119
120
121
               ENDDO
            ENDDO
         ENDDO
         !
122
         ! generate p-vectors in cart
123
         !
124
125
126
         ALLOCATE ( this%p_cart (3,this%np) )
         this%p_cart(:,:) = this%p_cryst(:,:)
         CALL cryst_to_cart( this%nps, this%p_cart, bg, +1 )
127
         !
128
129
130
131
132
         ! set weights 
         !
         ALLOCATE ( this%weight (this%nps)   )
         !
         this%weight = 1._DP / DBLE(this%np)
133
         !
134
      END SELECT
135
      !
136
137
      ALLOCATE ( this%ip( this%nps ) ) 
      ALLOCATE ( this%is( this%nps ) ) 
138
      !
139
      ! generate map ips --> ip and is
140
      !
141
142
      this%ip = 0 
      this%is = 0 
143
144
145
146
147
148
      k = 0 
      DO i = 1, this%ns
         DO j = 1, this%np
            k = k+1 
            this%ip(k) = j
            this%is(k) = i
149
         ENDDO
150
      ENDDO
151
      !
152
      ALLOCATE( this%l_pIsGamma(this%np) )
153
      this%l_pIsGamma(:)=.FALSE.
154
      DO ip = 1, this%np
155
156
         this%l_pIsGamma(ip) = ( ALL( ABS ( this%p_cryst(:,ip) ) .LT. eps8 ) )
      ENDDO
157
158
159
160
      !
   END SUBROUTINE
   !
   !
161
162
163
   !FUNCTION findp(this,p,unit_type) RESULT(ip)
   SUBROUTINE findp( this, p, is, unit_type, ip, g0 )
      ! 
164
165
      ! ... ip is the index of p (unit_type = [ "cryst", "cart"])
      ! ... if on exit ip == 0 --> p is not commensurate with this grid
166
      ! ... g0 relates p to an equivalent vector inside the 1BZ
167
168
      !
      USE constants, ONLY : eps8 
169
      USE cell_base, ONLY : at, bg
170
171
172
      !
      IMPLICIT NONE
      !
173
      ! I/O 
174
      !
175
176
177
178
179
180
      CLASS(bz_grid), INTENT(IN) :: this 
      REAL(DP), INTENT(IN) :: p(3)
      INTEGER, INTENT(IN) :: is
      CHARACTER(LEN=*), INTENT(IN) :: unit_type
      INTEGER, INTENT(OUT) :: ip
      REAL(DP), INTENT(OUT) :: g0(3)
181
182
      !
      ! Workspace
183
184
      ! 
      INTEGER :: i
185
      REAL(DP) :: deltap(3)
186
187
188
189
      !
      SELECT CASE(unit_type) 
      CASE("cryst","cart")
      CASE DEFAULT
190
         CALL errore( "types_bz_grid", "unit_type not supported, supported only cryst or cart", 1 )  
191
192
      END SELECT 
      !
193
      ! The search must be performed in crystalline coordinates
194
      !
195
      IF ( unit_type == "cart" ) CALL cryst_to_cart( 1, p, at, -1 )
196
      !
197
198
      ip = 0
      DO i = 1, this%np
199
200
         deltap(:) = p(:) - this%p_cryst(:,i) - NINT( p(:) - this%p_cryst(:,i) )
         IF ( ALL ( ABS ( deltap ) .LT. eps8 ) ) THEN
201
202
203
204
205
            ip = i + (is-1) * this%np
            g0(:) = p(:) - this%p_cryst(:,ip)
            EXIT
         ENDIF
      ENDDO
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
      ! Tranform g0 back to cartesian coordinates if needed
      !
      IF ( unit_type == "cart" ) CALL cryst_to_cart( 1, g0, bg, 1 ) 
      !      
      !ip = 0 
      !SELECT CASE( unit_type ) 
      !CASE("cryst")
      !   DO i = 1, this%np
      !      IF( ( ALL( ABS ( p(:) - this%p_cryst(:,i) ) .LT. eps8 ) ) ) THEN 
      !         ip = i 
      !         EXIT
      !      ENDIF
      !   ENDDO
      !CASE("cart")
      !   DO i = 1, this%np
      !      IF( ( ALL( ABS ( p(:) - this%p_cart(:,i) ) .LT. eps8 ) ) ) THEN  
      !         ip = i 
      !         EXIT
      !      ENDIF
      !   ENDDO
      !CASE DEFAULT
      !   CALL errore( "class_bz_grid", "unit_type not supported, supported only cryst or cart", 1 )  
      !END SELECT
      !
   !END FUNCTION
232
   END SUBROUTINE
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
   !
   !
   !SUBROUTINE addp( this, pin1, pin2, pout, g0, unit_type )
   !   !
   !   ! ... out : pout and g0 
   !   ! ... pout = pin1 + pin2 - g0   ( g0 makes sure that pout is in 1BZ ) 
   !   ! ... unit_type determines the units of pin1, pin2 and pout, g0  
   !   !
   !   USE cell_base,        ONLY : at, bg
   !   USE constants,        ONLY : eps8
   !   !
   !   IMPLICIT NONE
   !   !
   !   ! I/O
   !   !
   !   CLASS(bz_grid), INTENT(IN) :: this
   !   REAL(DP), INTENT(IN) :: pin1(3), pin2(3) 
   !   REAL(DP), INTENT(OUT) :: pout(3)
   !   REAL(DP), INTENT(OUT) :: g0(3)
   !   CHARACTER(LEN=*),INTENT(IN) :: unit_type
   !   !
   !   ! Workspace
   !   !
   !   REAL(DP) :: ptemp(3)  
   !   !
   !   SELECT CASE(unit_type) 
   !   CASE("cryst","cart")
   !   CASE DEFAULT
   !      CALL errore( "types_bz_grid", "unit_type not supported, supported only cryst or cart", 1 )  
   !   END SELECT 
   !   !   
   !   ptemp = pin1 + pin2
   !   IF( unit_type == "cart" ) CALL cryst_to_cart( 1, ptemp, at, -1 )
   !   !
   !   ! ptemp is now in cryst  
   !   !
   !   g0 = NINT( ptemp ) ! in cryst 
   !   pout = ptemp - g0  ! in cryst 
   !   !
   !   IF( unit_type == "cart" ) THEN 
   !      CALL cryst_to_cart( 1, pout , bg, 1 )
   !      CALL cryst_to_cart( 1, g0   , bg, 1 )
   !   ENDIF  
   !   !
   !END SUBROUTINE
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
!     !
!     IF ( sig == +1 ) THEN
!        csig = '+'
!     ELSE
!        csig = '-'
!     ENDIF
!     !
!     nks = kgrid%nps
!     nqs = qgrid%nps
!     !
!     ALLOCATE( temp_xkq(3,nks), temp_wkq(nks) )
!     ALLOCATE( new_ikq(nks), temp_index_ikq(nks) )
!     !
!     temp_xkq = kgrid%xp_cryst
!     temp_wkq = kgrid%wp
!     !
!     ALLOCATE( kqgrid%index_kq(nks,nqs) )
!     ALLOCATE( kqgrid%g0(3,nks,nqs) )
!     !
!     nkqs = 0
!     new_ikq = 0
!     kqgrid%g0 = 0._DP
!     !
!     DO ik = 1, nks
!        DO iq = 1, nqs
!           !
!           xkq(:) = kgrid%xp_cryst(:,ik) + DBLE(sig) * qgrid%xp_cryst(:,iq) 
!           !
!           xk_not_found = .TRUE.
!           !
!           DO ikq = 1, nks
!              IF ( xk_not_found ) THEN
!                 dxk(:) = xkq(:)-temp_xkq(:,ikq) - NINT( xkq(:)-temp_xkq(:,ikq) )
!                 IF ( ALL( ABS( dxk ) < eps ) ) THEN
!                    xk_not_found = .FALSE.
!                    IF ( new_ikq(ikq) == 0 ) THEN
!                       nkqs = nkqs + 1
!                       temp_index_ikq(nkqs) = ikq
!                       new_ikq(ikq) = nkqs
!                    ENDIF
!                    kqgrid%index_kq(ik,iq) = new_ikq(ikq)
!                    kqgrid%g0(:,ik,iq) = xkq(:) - temp_xkq(:,ikq)
!                    CALL cryst_to_cart(1, kqgrid%g0(1,ik,iq), bg, +1)
!                 ENDIF
!              ENDIF
!           ENDDO ! ikq
!           !
!           IF ( xk_not_found ) THEN
!              WRITE(*,*) ik, iq, nkqs
!              WRITE(*,*) xkq(:)
!              WRITE(message,'(a,a,a)')'k ',TRIM( csig ),' q does not belong to k-point grid '
!              CALL errore( 'kq_grid_init', TRIM( message ), (ik-1) * nqs + iq )
!           ENDIF
!           !
!        ENDDO ! iq
!        !
!     ENDDO ! ik
!     !
!     ALLOCATE( kqgrid%xp_cryst(3,nspin*nkqs) , kqgrid%xp_cart(3,nspin*nkqs), kqgrid%wp(nspin*nkqs) )
!     !
!     DO ik = 1, nkqs
!        ikq = temp_index_ikq(ik)
!        kqgrid%xp_cryst(:,ik) = temp_xkq(:,ikq)
!        kqgrid%wp(ik) = temp_wkq(ikq)
!     ENDDO
!     !
!     kqgrid%xp_cart = kqgrid%xp_cryst
!     CALL cryst_to_cart(nkqs, kqgrid%xp_cart, bg, +1)
!     !
!     IF ( nspin == 2 ) THEN
!        !
!        DO ik = 1, nks/2
!            DO iq =1, nqs
!              kqgrid%index_kq(nks/2+ik,iq) = kqgrid%index_kq(ik,iq) + nkqs
!            ENDDO
!        ENDDO
!        !
!        DO ikq = 1, nkqs
!           kqgrid%xp_cart(:,ikq + nkqs) = kqgrid%xp_cart(:,ikq)
!           kqgrid%xp_cryst(:,ikq + nkqs) = kqgrid%xp_cryst(:,ikq)
!        ENDDO
!        !
!        nkqs = 2*nkqs
!     ENDIF
!     !
!     kqgrid%nps = nkqs
!     !
!     DEALLOCATE( new_ikq, temp_index_ikq )
!     DEALLOCATE( temp_xkq, temp_wkq )
367
368
!      !
!   END SUBROUTINE
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
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
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
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
   !
   !
!  SUBROUTINE q_grid_init( qgrid, kgrid, k1grid )
!     !
!     USE cell_base,        ONLY : at, bg
!     USE klist,            ONLY : xk, wk, nkstot
!     USE pwcom,            ONLY : nspin
!     USE westcom,          ONLY : nq
!     !
!     IMPLICIT NONE
!     !
!     ! I/O
!     !
!     CLASS(bz_grid), INTENT(OUT) :: qgrid
!     CLASS(bz_grid), INTENT(IN) :: kgrid, k1grid
!     !
!     ! Workspace
!     !
!     INTEGER :: ik, ikk, iq, iqq, iq1, iq2, iq3, ipol, ig
!     INTEGER :: nks, nks1, nqs, temp_nqs
!     INTEGER, ALLOCATABLE :: new_iq(:), temp_index_iq(:)
!     REAL(DP) :: xq(3)
!     REAL(DP), ALLOCATABLE :: temp_xq(:,:), temp_wq(:)
!     REAL(DP) :: dxq(3), dq1, dq2, dq3
!     LOGICAL :: xq_not_found
!     CHARACTER(LEN=256) :: message
!     CHARACTER(LEN=1) :: csig
!     REAL(DP) :: eps = 1.D-8
!     !
!     nks = kgrid%nps
!     nks1 = k1grid%nps
!     temp_nqs = nq(1)*nq(2)*nq(3)
!     !
!     ALLOCATE( temp_xq(3,temp_nqs), temp_wq(temp_nqs) )
!     ALLOCATE( new_iq(temp_nqs), temp_index_iq(temp_nqs) )
!     !
!     dq1 = 1._DP / DBLE(nq(1))
!     dq2 = 1._DP / DBLE(nq(2))
!     dq3 = 1._DP / DBLE(nq(3))
!     iq = 0
!     DO iq1 = 1, nq(1)
!        DO iq2 = 1, nq(2)
!           DO iq3 = 1, nq(3)
!              iq = iq + 1
!              temp_xq(1,iq) = DBLE( iq1 - 1 ) * dq1
!              temp_xq(2,iq) = DBLE( iq2 - 1 ) * dq2
!              temp_xq(3,iq) = DBLE( iq3 - 1 ) * dq3
!           ENDDO
!        ENDDO
!     ENDDO
!     temp_wq = 1 / DBLE(nq(1)*nq(2)*nq(3))
!     !
!     ALLOCATE( qgrid%index_q(nks,nks) )
!     ALLOCATE( qgrid%g0(3,nks,nks) )
!     !
!     nqs = 0
!     new_iq = 0
!     qgrid%g0 = 0._DP
!     !
!     DO ik = 1, nks
!        !
!        DO ikk = 1, nks1
!           !
!           xq(:) = kgrid%xp_cryst(:,ik) - k1grid%xp_cryst(:,ikk) 
!           !
!           xq_not_found = .TRUE.
!           !
!           DO iq = 1, temp_nqs
!              IF ( xq_not_found ) THEN
!                 dxq(:) = xq(:)-temp_xq(:,iq) - NINT( xq(:)-temp_xq(:,iq) )
!                 IF ( ALL( ABS( dxq ) < eps ) ) THEN
!                    xq_not_found = .FALSE.
!                    IF ( new_iq(iq) == 0 ) THEN
!                       nqs = nqs + 1
!                       temp_index_iq(nqs) = iq
!                       new_iq(iq) = nqs
!                    ENDIF
!                    qgrid%index_q(ik,ikk) = new_iq(iq)
!                    qgrid%g0(:,ik,ikk) = xq(:) - temp_xq(:,iq)
!                    CALL cryst_to_cart(1, qgrid%g0(1,ik,ikk), bg, +1)
!                 ENDIF
!              ENDIF
!           ENDDO ! iq
!           !
!           IF ( xq_not_found ) THEN
!              WRITE(*,*) ik, ikk, nqs
!              WRITE(*,*) xq(:)
!              WRITE(message,'(a)') " k - k' is not a commensurate with k-point grid "
!              CALL errore( 'q_grid_init', TRIM( message ), (ik-1) * nqs + ikk )
!           ENDIF
!           !
!        ENDDO ! ikk
!        !
!     ENDDO ! ik
!     !
!     ALLOCATE( qgrid%xp_cryst(3,nqs) , qgrid%xp_cart(3,nqs), qgrid%wp(nqs) )
!     !
!     DO iqq = 1, nqs
!        iq = temp_index_iq(iqq)
!        qgrid%xp_cryst(:,iqq) = temp_xq(:,iq)
!        qgrid%wp(iqq) = temp_wq(iq)
!     ENDDO
!     !
!     qgrid%xp_cart = qgrid%xp_cryst
!     CALL cryst_to_cart(nqs, qgrid%xp_cart, bg, +1)
!     !
!     ALLOCATE( qgrid%l_gammap(nqs) )
!     qgrid%l_gammap(:)=.FALSE.
!     DO iq = 1, nqs
!        qgrid%l_gammap(iq) = ( ALL( ABS ( qgrid%xp_cryst(:,iq) ) .LT. eps ) )
!     ENDDO
!     !
!     qgrid%nps = nqs
!     !
!     DEALLOCATE( new_iq, temp_index_iq )
!     DEALLOCATE( temp_xq, temp_wq )
!     !
!  END SUBROUTINE
!  !
!  SUBROUTINE get_phase( grid_aux, ik, ikk )
!     !
!     USE cell_base,        ONLY : bg
!     USE gvecs,            ONLY : ngms, nls
!     USE gvect,            ONLY : g
!     USE fft_base,         ONLY : dffts
!     USE fft_interfaces,   ONLY : invfft
!     !
!     IMPLICIT NONE
!     !
!     ! I/O
!     !
!     CLASS(bz_grid), INTENT(INOUT) :: grid_aux
!     INTEGER, INTENT(IN) :: ik, ikk
!     !
!     ! Workspace
!     !
!     INTEGER :: ipol, ig, ig0
!     REAL(DP) :: eps = 1.D-8
!     !
!     ig0 = 0
!     DO ig = 1, ngms
!        IF ( ALL ( ABS( g(:,ig) - grid_aux%g0(:,ik,ikk) ) < eps ) ) THEN
!           ig0 = ig
!        ENDIF
!     ENDDO
!     !
!     IF (.NOT. ALLOCATED(grid_aux%phase) ) ALLOCATE ( grid_aux%phase(dffts%nnr) )
!     grid_aux%phase = (0._DP, 0._DP)
!     IF ( ig0 > 0 ) THEN
!        grid_aux%phase( nls(ig0) ) = (1._DP, 0._DP)
!     ENDIF
!     ! phase = exp(-iG_0*r)
!     CALL invfft( 'Wave', grid_aux%phase, dffts )
!     grid_aux%phase(1:dffts%nnr) = CONJG( grid_aux%phase(1:dffts%nnr) )
!     !
!  END SUBROUTINE
525
526
   ! 
END MODULE