(git:3d5d2e5)
Loading...
Searching...
No Matches
mt_util.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9MODULE mt_util
10 USE bibliography, ONLY: martyna1999,&
11 cite_reference
12 USE kinds, ONLY: dp,&
13 int_8
14 USE mathconstants, ONLY: fourpi,&
15 oorootpi,&
16 pi
18 USE pw_methods, ONLY: pw_axpy,&
21 USE pw_pool_types, ONLY: pw_pool_create,&
24 USE pw_types, ONLY: pw_c1d_gs_type,&
26#include "../base/base_uses.f90"
27
28 IMPLICIT NONE
29
30 PRIVATE
31 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
32 CHARACTER(LEN=*), PARAMETER, PRIVATE :: moduleN = 'mt_util'
33
34 INTEGER, PARAMETER, PUBLIC :: mt2d = 1101, &
35 mt1d = 1102, &
36 mt0d = 1103
37
39CONTAINS
40
41! **************************************************************************************************
42!> \brief MT2D Coulomb kernel and its affine strain derivative in the periodic plane.
43!> \param g Cartesian reciprocal vector, transformed by the inverse transpose of the strain.
44!> \param special_dimension Cartesian direction normal to the slab.
45!> \param slab_size Cell thickness in the nonperiodic direction, fixed for in-plane strain.
46!> \param kernel Reciprocal Coulomb kernel, including the existing MT2D G=0 convention.
47!> \param strain Kernel derivative with respect to in-plane affine strain.
48! **************************************************************************************************
49 SUBROUTINE mt2d_kernel_strain(g, special_dimension, slab_size, kernel, strain)
50 REAL(kind=dp), INTENT(IN) :: g(3)
51 INTEGER, INTENT(IN) :: special_dimension
52 REAL(kind=dp), INTENT(IN) :: slab_size
53 REAL(kind=dp), INTENT(OUT) :: kernel, strain(3, 3)
54
55 INTEGER :: alpha, beta
56 REAL(kind=dp) :: f, g2, k, parallel(3), q, radius
57
58 cpassert(special_dimension >= 1 .AND. special_dimension <= 3)
59 cpassert(slab_size > 0.0_dp)
60 strain = 0.0_dp
61 g2 = sum(g**2)
62 IF (g2 == 0.0_dp) THEN
63 ! This kernel is independent of in-plane strain. Its volume term is
64 ! already included in the Hartree energy contribution to the virial.
65 kernel = pi*slab_size**2/2.0_dp
66 RETURN
67 END IF
68 parallel = g
69 parallel(special_dimension) = 0.0_dp
70 k = norm2(parallel)
71 radius = slab_size/2.0_dp
72 q = cos(g(special_dimension)*radius)*exp(-k*radius)
73 f = 1.0_dp - q
74 kernel = fourpi/g2*f
75 ! For k=0 the reciprocal vector is unchanged by every in-plane strain.
76 ! This also avoids dividing by k at the even-normal modes where K=0.
77 IF (k == 0.0_dp) RETURN
78 DO alpha = 1, 3
79 IF (alpha == special_dimension) cycle
80 DO beta = alpha, 3
81 IF (beta == special_dimension) cycle
82 strain(alpha, beta) = fourpi/g2*g(alpha)*g(beta)*(2.0_dp*f/g2 - q*radius/k)
83 strain(beta, alpha) = strain(alpha, beta)
84 END DO
85 END DO
86 END SUBROUTINE mt2d_kernel_strain
87
88! **************************************************************************************************
89!> \brief Initialize the Martyna && Tuckerman Poisson Solver
90!> \param screen_function ...
91!> \param pw_pool ...
92!> \param method ...
93!> \param alpha ...
94!> \param special_dimension ...
95!> \param slab_size ...
96!> \param super_ref_pw_grid ...
97!> \author Teodoro Laino (16.06.2004)
98! **************************************************************************************************
99 SUBROUTINE mtin_create_screen_fn(screen_function, pw_pool, method, alpha, &
100 special_dimension, slab_size, super_ref_pw_grid)
101 TYPE(pw_c1d_gs_type), POINTER :: screen_function
102 TYPE(pw_pool_type), POINTER :: pw_pool
103 INTEGER, INTENT(IN) :: method
104 REAL(kind=dp), INTENT(IN) :: alpha
105 INTEGER, INTENT(IN) :: special_dimension
106 REAL(kind=dp), INTENT(IN) :: slab_size
107 TYPE(pw_grid_type), POINTER :: super_ref_pw_grid
108
109 CHARACTER(LEN=*), PARAMETER :: routinen = 'MTin_create_screen_fn'
110
111 INTEGER :: handle, ig, iz
112 REAL(kind=dp) :: alpha2, g2, g3d, gxy, gz, zlength
113 TYPE(pw_c1d_gs_type), POINTER :: vlocg
114 TYPE(pw_pool_type), POINTER :: pw_pool_aux
115 TYPE(pw_r3d_rs_type), POINTER :: vloc
116
117 CALL timeset(routinen, handle)
118 NULLIFY (vloc, vlocg, pw_pool_aux)
119 !
120 ! For Martyna-Tuckerman we set up an auxiliary pw_pool at an higher cutoff
121 !
122 CALL cite_reference(martyna1999)
123 IF (ASSOCIATED(super_ref_pw_grid)) THEN
124 CALL pw_pool_create(pw_pool_aux, pw_grid=super_ref_pw_grid)
125 END IF
126 NULLIFY (screen_function)
127 ALLOCATE (screen_function)
128 CALL pw_pool%create_pw(screen_function)
129 CALL pw_zero(screen_function)
130 SELECT CASE (method)
131 CASE (mt0d)
132 NULLIFY (vloc, vlocg)
133 ALLOCATE (vloc, vlocg)
134 IF (ASSOCIATED(pw_pool_aux)) THEN
135 CALL pw_pool_aux%create_pw(vloc)
136 CALL pw_pool_aux%create_pw(vlocg)
137 ELSE
138 CALL pw_pool%create_pw(vloc)
139 CALL pw_pool%create_pw(vlocg)
140 END IF
141 CALL mt0din(vloc, alpha)
142 CALL pw_transfer(vloc, vlocg)
143 CALL pw_axpy(vlocg, screen_function)
144 IF (ASSOCIATED(pw_pool_aux)) THEN
145 CALL pw_pool_aux%give_back_pw(vloc)
146 CALL pw_pool_aux%give_back_pw(vlocg)
147 ELSE
148 CALL pw_pool%give_back_pw(vloc)
149 CALL pw_pool%give_back_pw(vlocg)
150 END IF
151 DEALLOCATE (vloc, vlocg)
152 !
153 ! Get rid of the analytical FT of the erf(a*r)/r
154 !
155 alpha2 = alpha*alpha
156!$OMP PARALLEL DO IF(screen_function%pw_grid%ngpts_cut_local >= 8192) DEFAULT(NONE) SCHEDULE(STATIC) &
157!$OMP SHARED(screen_function,alpha2) PRIVATE(g2,g3d)
158 DO ig = screen_function%pw_grid%first_gne0, screen_function%pw_grid%ngpts_cut_local
159 g2 = screen_function%pw_grid%gsq(ig)
160 g3d = fourpi/g2
161 screen_function%array(ig) = screen_function%array(ig) - g3d*exp(-g2/(4.0e0_dp*alpha2))
162 END DO
163!$OMP END PARALLEL DO
164 IF (screen_function%pw_grid%have_g0) THEN
165 screen_function%array(1) = screen_function%array(1) + fourpi/(4.0e0_dp*alpha2)
166 END IF
167 CASE (mt2d)
168 iz = special_dimension ! iz is the direction with NO PBC
169 zlength = slab_size ! zlength is the thickness of the cell
170!$OMP PARALLEL DO IF(screen_function%pw_grid%ngpts_cut_local >= 8192) DEFAULT(NONE) SCHEDULE(STATIC) &
171!$OMP SHARED(screen_function,iz,zlength) PRIVATE(gz,g2,gxy,g3d)
172 DO ig = screen_function%pw_grid%first_gne0, screen_function%pw_grid%ngpts_cut_local
173 gz = screen_function%pw_grid%g(iz, ig)
174 g2 = screen_function%pw_grid%gsq(ig)
175 gxy = sqrt(abs(g2 - gz*gz))
176 g3d = fourpi/g2
177 screen_function%array(ig) = -g3d*cos(gz*zlength/2.0_dp)*exp(-gxy*zlength/2.0_dp)
178 END DO
179!$OMP END PARALLEL DO
180 IF (screen_function%pw_grid%have_g0) screen_function%array(1) = pi*zlength*zlength/2.0_dp
181 CASE (mt1d)
182 iz = special_dimension ! iz is the direction with PBC
183 CALL mt1din(screen_function)
184 cpabort("MT1D unimplemented")
185 END SELECT
186 CALL pw_pool_release(pw_pool_aux)
187 CALL timestop(handle)
188 END SUBROUTINE mtin_create_screen_fn
189
190! **************************************************************************************************
191!> \brief Calculates the Tuckerman Green's function in reciprocal space
192!> according the scheme published on:
193!> Martyna and Tuckerman, J. Chem. Phys. Vol. 110, No. 6, 2810-2821
194!> \param Vloc ...
195!> \param alpha ...
196!> \author Teodoro Laino (09.03.2005)
197! **************************************************************************************************
198 SUBROUTINE mt0din(Vloc, alpha)
199 TYPE(pw_r3d_rs_type), POINTER :: vloc
200 REAL(kind=dp), INTENT(IN) :: alpha
201
202 CHARACTER(LEN=*), PARAMETER :: routinen = 'mt0din'
203
204 INTEGER :: handle, i, j, k
205 INTEGER, DIMENSION(:), POINTER :: glb
206 INTEGER, DIMENSION(:, :), POINTER :: bo
207 REAL(kind=dp) :: dx, dy, dz, fact, omega, r, r2, x, y, &
208 y2, z, z2
209 REAL(kind=dp), DIMENSION(3) :: box, box2
210 TYPE(pw_grid_type), POINTER :: grid
211
212 CALL timeset(routinen, handle)
213
214 grid => vloc%pw_grid
215 bo => grid%bounds_local
216 glb => grid%bounds(1, :)
217 vloc%array = 0.0_dp
218 box = real(grid%npts, kind=dp)*grid%dr
219 box2 = box/2.0_dp
220 omega = product(box)
221 fact = omega
222 dx = grid%dr(1)
223 dy = grid%dr(2)
224 dz = grid%dr(3)
225!$OMP PARALLEL DO IF(SIZE(Vloc%array,KIND=int_8) >= 8192) DEFAULT(NONE) SCHEDULE(STATIC) &
226!$OMP SHARED(Vloc,bo,glb,dx,dy,dz,box,box2,alpha,fact) PRIVATE(j,i,x,y,z,y2,z2,r,r2)
227 DO k = bo(1, 3), bo(2, 3)
228 z = real(k - glb(3), dp)*dz; IF (z > box2(3)) z = box(3) - z
229 z2 = z*z
230 DO j = bo(1, 2), bo(2, 2)
231 y = real(j - glb(2), dp)*dy; IF (y > box2(2)) y = box(2) - y
232 y2 = y*y
233 DO i = bo(1, 1), bo(2, 1)
234 x = real(i - glb(1), dp)*dx; IF (x > box2(1)) x = box(1) - x
235 r2 = x*x + y2 + z2
236 r = sqrt(r2)
237 IF (r > 1.0e-10_dp) THEN
238 vloc%array(i, j, k) = erf(alpha*r)/r*fact
239 ELSE
240 vloc%array(i, j, k) = 2.0_dp*alpha*oorootpi*fact
241 END IF
242 END DO
243 END DO
244 END DO
245!$OMP END PARALLEL DO
246 CALL timestop(handle)
247 END SUBROUTINE mt0din
248
249! **************************************************************************************************
250!> \brief Calculates the Tuckerman Green's function in reciprocal space
251!> according the scheme published on:
252!> Martyna and Tuckerman, J. Chem. Phys. Vol. 121, No. 23, 11949
253!> \param screen_function ...
254!> \author Teodoro Laino (11.2005)
255! **************************************************************************************************
256 SUBROUTINE mt1din(screen_function)
257 TYPE(pw_c1d_gs_type), POINTER :: screen_function
258
259 CHARACTER(LEN=*), PARAMETER :: routinen = 'mt1din'
260
261 INTEGER :: handle
262 REAL(kind=dp) :: dx, dy, dz, omega
263 REAL(kind=dp), DIMENSION(3) :: box, box2
264 TYPE(pw_grid_type), POINTER :: grid
265
266 CALL timeset(routinen, handle)
267 grid => screen_function%pw_grid
268 box = real(grid%npts, kind=dp)*grid%dr
269 box2 = box/2.0_dp
270 omega = product(box)
271 dx = grid%dr(1)
272 dy = grid%dr(2)
273 dz = grid%dr(3)
274
275 CALL timestop(handle)
276 END SUBROUTINE mt1din
277
278END MODULE mt_util
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public martyna1999
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public oorootpi
real(kind=dp), parameter, public pi
real(kind=dp), parameter, public fourpi
subroutine, public mt2d_kernel_strain(g, special_dimension, slab_size, kernel, strain)
MT2D Coulomb kernel and its affine strain derivative in the periodic plane.
Definition mt_util.F:50
integer, parameter, public mt0d
Definition mt_util.F:34
integer, parameter, public mt2d
Definition mt_util.F:34
subroutine, public mtin_create_screen_fn(screen_function, pw_pool, method, alpha, special_dimension, slab_size, super_ref_pw_grid)
Initialize the Martyna && Tuckerman Poisson Solver.
Definition mt_util.F:101
integer, parameter, public mt1d
Definition mt_util.F:34
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
subroutine, public pw_pool_release(pool)
releases the given pool (see cp2k/doc/ReferenceCounting.html)
subroutine, public pw_pool_create(pool, pw_grid, max_cache)
creates a pool for pw
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...