store_sqvc.f90 13.6 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
!
2
! Copyright (C) 2015-2017 M. Govoni 
Marco Govoni's avatar
Marco Govoni committed
3
4
5
6
7
8
9
10
11
12
13
! 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: 
! Marco Govoni
!
!-----------------------------------------------------------------------
14
SUBROUTINE store_sqvc(sqvc_tmp,numg,singularity_removal_mode,div,printout_div)
Marco Govoni's avatar
Marco Govoni committed
15
16
17
18
 !-----------------------------------------------------------------------
 !
 ! This routine computes results of applying sqVc to a potential
 ! associated with vector q. Coulomb cutoff technique can be used
19
 ! Used for gamma_only case
Marco Govoni's avatar
Marco Govoni committed
20
21
22
23
24
25
26
27
28
29
30
 !
 USE kinds,                ONLY : DP
 USE io_global,            ONLY : stdout
 USE constants,            ONLY : fpi, e2, tpi, pi
 USE cell_base,            ONLY : tpiba2, tpiba,omega,at,alat,bg
 USE gvect,                ONLY : g, gstart
 USE coulomb_vcut_module,  ONLY : vcut_init, vcut_type, vcut_info, &
                                  vcut_get,  vcut_spheric_get, vcut_destroy
 USE mp,                   ONLY : mp_sum
 USE mp_global,            ONLY : intra_bgrp_comm
 USE mp_world,             ONLY : mpime,world_comm,nproc
31
 USE gvecw,                ONLY : ecutwfc, gcutw
Marco Govoni's avatar
Marco Govoni committed
32
33
 USE control_flags,        ONLY : gamma_only
 USE random_numbers,       ONLY : randy
34
35
 USE class_bz_grid,        ONLY : bz_grid
 USE types_bz_grid,        ONLY : q_grid
Marco Govoni's avatar
Marco Govoni committed
36
37
38
39
40
41
42
43
44
 !
 IMPLICIT NONE
 !
 ! I/O
 !
 INTEGER,INTENT(IN) :: numg
 REAL(DP),INTENT(OUT) :: sqvc_tmp(numg)
 INTEGER,INTENT(IN) :: singularity_removal_mode
 REAL(DP),INTENT(OUT) :: div
45
 LOGICAL,INTENT(IN) :: printout_div
Marco Govoni's avatar
Marco Govoni committed
46
47
48
49
50
51
52
 !
 ! Workspace
 !
 REAL(DP) :: gnorm2,q(3),x,nq(3),ecutvcut,atws(3,3),dq(3),xq(3),alpha
 INTEGER :: ig, ipol, iq1, iq2, iq3, i1, i2, i3
 LOGICAL :: on_double_grid
 REAL(DP),PARAMETER :: eps=1.d-6
53
 REAL(DP),PARAMETER :: eps_qdiv=1.d-8
Marco Govoni's avatar
Marco Govoni committed
54
 REAL(DP) :: grid_factor = 8.d0/7.d0
55
! REAL(DP) :: grid_factor = 1.0_DP
Marco Govoni's avatar
Marco Govoni committed
56
57
58
59
60
61
62
 TYPE(vcut_type)   :: vcut
 LOGICAL :: try_ort_div=.TRUE.,i_am_ort 
 REAL(DP) :: prod(3,3), qhelp, edge(3), qbz(3), rand, qmo, vbz, vhelp 
 REAL(DP) :: intcounter
 !
 CALL start_clock('storesqvc')
 !
63
64
65
66
67
68
69
70
71
72
 IF ( gamma_only ) THEN
    nq(1)=1._DP
    nq(2)=1._DP
    nq(3)=1._DP
 ELSE
    nq(1) = REAL( q_grid%np1, KIND=DP )
    nq(2) = REAL( q_grid%np2, KIND=DP )
    nq(3) = REAL( q_grid%np3, KIND=DP )
 ENDIF
 !
Marco Govoni's avatar
Marco Govoni committed
73
74
75
76
77
78
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
 ! ======
 !  BODY
 ! ======
 !
 SELECT CASE(singularity_removal_mode)
    !
 CASE(1)
    !
    ! In this case we use the spherical region 
    !
    sqvc_tmp(1)=0._DP
    DO ig = gstart,numg
       gnorm2 = ( g(1,ig)*g(1,ig) + g(2,ig)*g(2,ig) + g(3,ig)*g(3,ig) ) * tpiba2
       sqvc_tmp(ig) = SQRT(e2*fpi/gnorm2)
    ENDDO
    !
 CASE(2)
    !
    ! In this case we use Gygi-Baldereschi method
    !
    !
    sqvc_tmp(1)=0._DP
    DO ig = gstart,numg
       q(:) = g(:,ig)
       gnorm2 = SUM(q(:)**2) * tpiba2
       on_double_grid = .TRUE.
       DO ipol = 1,3
          x = 0.5_DP*( q(1)*at(1,ipol)+q(2)*at(2,ipol)+q(3)*at(3,ipol) )*nq(ipol)
          on_double_grid = on_double_grid .AND. (ABS(x-NINT(x))<eps)
       ENDDO
       IF(on_double_grid) THEN
          sqvc_tmp(ig)=0._DP
       ELSE
          sqvc_tmp(ig)=SQRT(e2*fpi*grid_factor/gnorm2)
       ENDIF
    ENDDO
    !
 CASE(3)
    !
    ! In this case we use CUT_WS
    ! 
    ecutvcut = 0.7_DP
    !
    ! build the superperiodicity direct lattice
    !
    atws = alat * at
    !
    DO ipol=1,3
       atws(:,ipol) = atws(:,ipol) * nq(ipol)
    ENDDO
    !
    CALL vcut_init( vcut, atws, ecutvcut )
    !
    DO ig = 1,numg
       !
       q(:) = g(:,ig) * tpiba
       !
       sqvc_tmp( ig ) = DSQRT( vcut_get(vcut,q) )
       !
    ENDDO
    !
    CALL vcut_destroy(vcut)
    !
 END SELECT
 !
 ! ======
 !  HEAD
 ! ======
 !
 SELECT CASE(singularity_removal_mode)
    !
 CASE(1)
    !
    ! In this case we use the spherical region 
    !
148
149
150
    div = ( (6._DP * pi * pi / ( omega*nq(1)*nq(2)*nq(3) ) )**(1._DP/3._DP) ) / ( 2._DP * pi * pi ) * fpi * e2
    !
    IF ( printout_div ) WRITE(stdout,"(5X,'Spherical div        = ',es14.6)") div 
Marco Govoni's avatar
Marco Govoni committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    !
    IF( try_ort_div ) THEN
       !  
       ! Redefine div according to orthorombic cell
       !
       prod = 0._DP
       DO i1 = 1, 3
          DO i2 = 1, 3
             DO i3 = 1, 3
                prod(i1,i2) = prod(i1,i2) + bg(i3,i1) * bg(i3,i2) * tpiba2
             ENDDO
          ENDDO
       ENDDO
       !
       i_am_ort = .TRUE.
       DO i1 = 1, 3
          DO i2 = 1, 3
             IF( i1 == i2 ) CYCLE
             IF( ABS( prod(i1,i2)) > 1.d-8 ) THEN 
170
                IF ( printout_div ) WRITE(stdout,"(5X,'Warning: non orthorombic RL')") 
Marco Govoni's avatar
Marco Govoni committed
171
172
173
174
175
176
177
178
179
180
                i_am_ort = .FALSE.
             ENDIF
          ENDDO
       ENDDO 
       !
       IF ( i_am_ort ) THEN
          !
          edge(1) = SQRT(prod(1,1)) / 2._DP 
          edge(2) = SQRT(prod(2,2)) / 2._DP 
          edge(3) = SQRT(prod(3,3)) / 2._DP 
181
          edge(:) = edge(:) / nq(:)
Marco Govoni's avatar
Marco Govoni committed
182
183
          !
          qhelp = MIN( edge(1),edge(2),edge(3)  )  
184
          vbz = tpi**3 / ( omega*nq(1)*nq(2)*nq(3) )
Marco Govoni's avatar
Marco Govoni committed
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
          vhelp = fpi / 3._DP * qhelp**3
          !
          rand=randy(mpime)
          div = 0._DP
          intcounter = 0 
          !
          DO i1 = 1, 100000
             qmo=0._DP
             DO i2 = 1, 3
                qbz(i2) = randy() * edge(i2)
                qmo = qmo + qbz(i2)**2
             ENDDO
             qmo = SQRT( qmo ) 
             IF( qmo < qhelp ) CYCLE
             div = div + 1._DP/qmo/qmo
             intcounter = intcounter + 1._DP
          ENDDO
          !
          div = div * ( vbz - vhelp  ) / intcounter  
          div = div + fpi * qhelp
          div = div * fpi * e2 / ( tpi * tpi * tpi ) 
          !
          div = div / REAL(nproc,KIND=DP)
          CALL mp_sum(div,world_comm)
          !
210
          IF ( printout_div ) WRITE(stdout,"(5X,'Orthorombic div      = ',es14.6)") div 
Marco Govoni's avatar
Marco Govoni committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
          !
       ENDIF
       !
    ENDIF
    !
 CASE(2)
    !
    ! In this case we use Gygi-Baldereschi method
    !
    alpha = 10._DP / ecutwfc
    !
    dq(1)=1._DP/nq(1)
    dq(2)=1._DP/nq(2)
    dq(3)=1._DP/nq(3)
    !
    div = 0._DP
    !
228
229
230
    DO iq1=1,INT(nq(1))
       DO iq2=1,INT(nq(2))
          DO iq3=1,INT(nq(3))
Marco Govoni's avatar
Marco Govoni committed
231
232
             xq(:) = bg(:,1)*(iq1-1)*dq(1) + bg(:,2)*(iq2-1)*dq(2) + bg(:,3)*(iq3-1)*dq(3)
             !
233
234
             !DO ig = gstart,numg
             DO ig = 1,numg
Marco Govoni's avatar
Marco Govoni committed
235
236
237
238
239
240
241
                q(:) = g(:,ig) + xq(:)
                gnorm2 = SUM(q(:)**2) * tpiba2
                on_double_grid = .TRUE.
                DO ipol = 1,3
                   x = 0.5_DP*( q(1)*at(1,ipol)+q(2)*at(2,ipol)+q(3)*at(3,ipol) )*nq(ipol)
                   on_double_grid = on_double_grid .AND. (ABS(x-NINT(x))<eps)
                ENDDO
242
                IF( .NOT.on_double_grid .AND. gnorm2 > eps_qdiv ) THEN 
Marco Govoni's avatar
Marco Govoni committed
243
244
245
246
247
248
249
250
251
252
253
254
255
                   div = div - EXP( -alpha * gnorm2 ) / gnorm2
                ENDIF
             ENDDO
             !
          ENDDO
       ENDDO
    ENDDO 
    !
    CALL mp_sum( div, intra_bgrp_comm )
    !
    IF( gamma_only ) THEN
       div = div * grid_factor * e2 * fpi / omega * 2._DP
    ELSE
256
       div = div * grid_factor * e2 * fpi / (omega* nq(1)*nq(2)*nq(3)) 
Marco Govoni's avatar
Marco Govoni committed
257
258
    ENDIF
    ! 
259
    div = div + e2 / SQRT( alpha * pi )
Marco Govoni's avatar
Marco Govoni committed
260
261
262
263
264
265
266
267
268
269
270
271
272
    !
 CASE(3)
    !
    ! In this case we use CUT_WS
    ! 
    div = 0._DP
    !
 END SELECT
 !
 CALL stop_clock('storesqvc')
 !
END SUBROUTINE
!
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
!
!
!---------------------------------------------------------------------------------
SUBROUTINE store_sqvc_q(sqvc_tmp,numg,singularity_removal_mode,iq,l_use_igq)
  !-------------------------------------------------------------------------------
  !
  ! This routine computes results of applying sqVc to a potential
  ! associated with vector q.
  !
  USE kinds,                ONLY : DP
  USE io_global,            ONLY : stdout
  USE constants,            ONLY : fpi, e2, tpi, pi
  USE cell_base,            ONLY : tpiba2,tpiba,omega,at,alat,bg
  USE gvect,                ONLY : g, gstart
  USE mp,                   ONLY : mp_sum
  USE mp_global,            ONLY : intra_bgrp_comm
  USE mp_world,             ONLY : mpime,world_comm,nproc
  USE gvecw,                ONLY : ecutwfc
  USE gvect,                ONLY : g, gstart
  USE westcom,              ONLY : igq_q
  USE coulomb_vcut_module,  ONLY : vcut_init, vcut_type, vcut_info, &
                                  vcut_get,  vcut_spheric_get, vcut_destroy
  USE random_numbers,       ONLY : randy
  USE class_bz_grid,        ONLY : bz_grid
  USE types_bz_grid,        ONLY : q_grid
  !
  IMPLICIT NONE
  !
  ! I/O
  !
  INTEGER, INTENT(IN) :: numg
  REAL(DP), INTENT(OUT) :: sqvc_tmp(numg)
  INTEGER, INTENT(IN) :: singularity_removal_mode
  INTEGER, INTENT(IN) :: iq
  LOGICAL, INTENT(IN) :: l_use_igq
  !
  ! Workspace
  !
  REAL(DP) :: gnorm2,nq(3),q(3),qq(3),ecutvcut,atws(3,3),alpha,x
  INTEGER :: ig, ipol, i1, i2, i3
  LOGICAL :: on_double_grid
  REAL(DP),PARAMETER :: eps=1.d-6
  REAL(DP), PARAMETER :: eps_qdiv = 1.d-8
  REAL(DP) :: grid_factor = 8.d0/7.d0
! REAL(DP) :: grid_factor = 1.0_DP
  TYPE(vcut_type)   :: vcut
  LOGICAL :: try_ort_div=.TRUE.,i_am_ort 
  REAL(DP) :: prod(3,3), qhelp, edge(3), qbz(3), rand, qmo, vbz, vhelp 
  REAL(DP) :: intcounter
  !
  CALL start_clock( 'storesqvcq' )
  !
  nq(1) = REAL( q_grid%np1, KIND=DP )
  nq(2) = REAL( q_grid%np2, KIND=DP )
  nq(3) = REAL( q_grid%np3, KIND=DP )
  !
  q(:) = q_grid%xp_cart(:,iq)
  !
  ! =======
  !  BODY
  ! =======
  !
  SELECT CASE(singularity_removal_mode)
     !
  CASE(1)
     !
     ! In this case we use the spherical region 
     !
     DO ig = 1,numg
        IF (l_use_igq) THEN
           qq(:) = q(:) + g(:,igq_q(ig,iq))
        ELSE
           qq(:) = q(:) + g(:,ig)
        ENDIF
        gnorm2 = SUM(qq(:)**2) * tpiba2
        IF ( gnorm2 < eps_qdiv ) THEN
           sqvc_tmp(ig) = 0._DP
        ELSE
           sqvc_tmp(ig) = SQRT(e2*fpi/gnorm2)
        ENDIF
     ENDDO
     !
  CASE(2)
     !
     ! In this case we use Gygi-Baldereschi method
     !
     DO ig = 1,numg
        IF (l_use_igq) THEN
           qq(:) = q(:) + g(:,igq_q(ig,iq))
        ELSE
           qq(:) = q(:) + g(:,ig)
        ENDIF
        gnorm2 = SUM(qq(:)**2) * tpiba2
        on_double_grid = .TRUE.
        DO ipol = 1,3
           x = 0.5_DP*( qq(1)*at(1,ipol)+qq(2)*at(2,ipol)+qq(3)*at(3,ipol) )*nq(ipol)
           on_double_grid = on_double_grid .AND. (ABS(x-NINT(x))<eps)
        ENDDO
        IF( on_double_grid .OR. gnorm2 < eps_qdiv ) THEN
           sqvc_tmp(ig)=0._DP
        ELSE
           sqvc_tmp(ig)=SQRT(e2*fpi*grid_factor/gnorm2)
        ENDIF
     ENDDO
     !
  CASE(3)
     !
     ! In this case we use CUT_WS
     ! 
     !
     ecutvcut = 0.7_DP
     !
     ! build the superperiodicity direct lattice
     !
     atws = alat * at
     !
     DO ipol=1,3
        atws(:,ipol) = atws(:,ipol) * nq(ipol)
     ENDDO
     !
     CALL vcut_init( vcut, atws, ecutvcut )
     !
     DO ig = 1,numg
        !
        IF (l_use_igq) THEN
           qq(:) = (q(:) + g(:,igq_q(ig,iq))) * tpiba
        ELSE
           qq(:) = (q(:) + g(:,ig)) * tpiba
        ENDIF
        !
        sqvc_tmp( ig ) = DSQRT( vcut_get(vcut,qq) )
        !
     ENDDO
     !
     CALL vcut_destroy(vcut)
     !
  END SELECT
  !
  CALL stop_clock( 'storesqvcq' )
  !
END SUBROUTINE
!
!
!
!----------------------------------------------------------------------------
Marco Govoni's avatar
Marco Govoni committed
418
SUBROUTINE store_vcspecial_H(vc_tmp,numg)
419
!----------------------------------------------------------------------------
Marco Govoni's avatar
Marco Govoni committed
420
 !
421
 ! vcspecial_H
Marco Govoni's avatar
Marco Govoni committed
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
525
526
 !
 USE kinds,     ONLY : DP
 USE cell_base, ONLY : tpiba, at,omega
 USE gvect,     ONLY : g,gstart
 USE constants, ONLY : fpi, e2, pi
 USE exx,       ONLY : exxdiv 
 !
 IMPLICIT NONE
 !
 ! I/O
 !
 INTEGER,INTENT(IN) :: numg
 REAL(DP),INTENT(OUT) :: vc_tmp(numg)
 ! 
 !Local variables
 INTEGER :: ig !Counters 
 REAL(DP) :: q(3), qq, x
 REAL(DP) :: grid_factor = 8.d0/7.d0
 REAL(DP) :: eps = 1.d-6 
 LOGICAL :: on_double_grid
 REAL(DP) :: kost
 !
 kost = e2*fpi
 !
 IF(gstart==2) vc_tmp(1) = -exxdiv 
 !
!$OMP PARALLEL SHARED(numg,g,tpiba,at,vc_tmp,kost,gstart) PRIVATE(ig,q,qq,on_double_grid,x)
!$OMP DO
 DO ig=gstart,numg
     !
     q(:)= g(:,ig)
     !
     q = q * tpiba
     !
     qq = SUM(q(:)**2) 
     !
     on_double_grid = .TRUE.
     x= 0.5_DP/tpiba*(q(1)*at(1,1)+q(2)*at(2,1)+q(3)*at(3,1))!*nq1
     on_double_grid = on_double_grid .AND. (ABS(x-NINT(x))<eps)
     x= 0.5_DP/tpiba*(q(1)*at(1,2)+q(2)*at(2,2)+q(3)*at(3,2))!*nq2
     on_double_grid = on_double_grid .AND. (ABS(x-NINT(x))<eps)
     x= 0.5_DP/tpiba*(q(1)*at(1,3)+q(2)*at(2,3)+q(3)*at(3,3))!*nq3
     on_double_grid = on_double_grid .AND. (ABS(x-NINT(x))<eps)
     ! 
     IF( on_double_grid ) THEN
        !
        vc_tmp(ig) = -kost / qq
        !
     ELSE
        !
        vc_tmp(ig)= kost/qq / 7._DP
        !
     ENDIF
     !
  ENDDO
!$OMP ENDDO
!$OMP END PARALLEL 
  !
END SUBROUTINE 
!
!
!
SUBROUTINE store_sqvc_sphcut(sqvc_tmp,numg,rcut)
 !
 ! This routine computes results of applying sqVc to a potential
 ! associated with vector q. Coulomb cutoff technique can be used
 !
 USE kinds,            ONLY : DP
 USE io_global,        ONLY : stdout
 USE constants,        ONLY : fpi, e2, tpi
 USE cell_base,        ONLY : tpiba2, tpiba
 USE gvect,            ONLY : g, gstart
 !
 IMPLICIT NONE
 !
 ! I/O
 !
 INTEGER,INTENT(IN) :: numg
 REAL(DP),INTENT(OUT) :: sqvc_tmp(numg)
 REAL(DP),INTENT(IN) :: rcut
 !
 ! Workspace
 !
 REAL(DP) :: gnorm2,gnorm
 INTEGER :: ig
 !
 !
 CALL start_clock('storesqvc')
 !
 !
 IF(gstart==2) sqvc_tmp(1)=SQRT(tpi*e2*rcut*rcut)
 !
!$OMP PARALLEL private(ig,gnorm2,gnorm)
!$OMP DO
 DO ig = gstart,numg
    gnorm2 = ( g(1,ig)*g(1,ig) + g(2,ig)*g(2,ig) + g(3,ig)*g(3,ig) ) * tpiba2
    gnorm  = SQRT(gnorm2)
    sqvc_tmp(ig) = SQRT(e2*fpi/gnorm2 * (1._DP-COS(gnorm*rcut)))
 ENDDO
!$OMP ENDDO
!$OMP END PARALLEL
 !
 CALL stop_clock('storesqvc')
 !
END SUBROUTINE