store_sqvc.f90 9.21 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
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
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
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
!
! Copyright (C) 2015-2016 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: 
! Marco Govoni
!
!-----------------------------------------------------------------------
SUBROUTINE store_sqvc(sqvc_tmp,numg,singularity_removal_mode,div)
 !-----------------------------------------------------------------------
 !
 ! 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, 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
 USE gvecw,                ONLY : ecutwfc
 USE control_flags,        ONLY : gamma_only
 USE random_numbers,       ONLY : randy
 !
 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
 !
 ! 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
 REAL(DP) :: grid_factor = 8.d0/7.d0
 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')
 !
 ! ======
 !  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
    !
    nq(1)=1._DP
    nq(2)=1._DP
    nq(3)=1._DP
    !
    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
    ! 
    !
    nq(1)=1._DP
    nq(2)=1._DP
    nq(3)=1._DP
    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 
    !
    div = ( (6._DP * pi * pi / omega )**(1._DP/3._DP) ) / ( 2._DP * pi * pi ) * fpi * e2
    WRITE(stdout,"(5X,'Spherical div        = ',es14.6)") div 
    !
    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 
                WRITE(stdout,"(5X,'Warning: non orthorombic RL')") 
                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 
          !
          qhelp = MIN( edge(1),edge(2),edge(3)  )  
          vbz = tpi**3 / omega
          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)
          !
          WRITE(stdout,"(5X,'Orthorombic div      = ',es14.6)") div 
          !
       ENDIF
       !
    ENDIF
    !
 CASE(2)
    !
    ! In this case we use Gygi-Baldereschi method
    !
    alpha = 10._DP / ecutwfc
    !
    nq(1)=1._DP
    nq(2)=1._DP
    nq(3)=1._DP
    !
    dq(1)=1._DP/nq(1)
    dq(2)=1._DP/nq(2)
    dq(3)=1._DP/nq(3)
    !
    div = 0._DP
    !
    DO iq1=1,1,INT(nq(1))
       DO iq2=1,1,INT(nq(2))
          DO iq3=1,1,INT(nq(3))
             xq(:) = bg(:,1)*(iq1-1)*dq(1) + bg(:,2)*(iq2-1)*dq(2) + bg(:,3)*(iq3-1)*dq(3)
             !
             DO ig = gstart,numg
                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
                IF(.NOT.on_double_grid) THEN 
                   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
       div = div * grid_factor * e2 * fpi / omega
    ENDIF
    ! 
    div = div + nq(1)*nq(2)*nq(3) * e2 / SQRT( alpha * pi )
    !
 CASE(3)
    !
    ! In this case we use CUT_WS
    ! 
    div = 0._DP
    !
 END SELECT
 !
 CALL stop_clock('storesqvc')
 !
END SUBROUTINE
!
!------------------------------------------------------------------
SUBROUTINE store_vcspecial_H(vc_tmp,numg)
 !
 ! vcspecail_H
 !
 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