Debian Codegraph @

src/mbd_dipole.F90

raw
1 ! This Source Code Form is subject to the terms of the Mozilla Public
2 ! License, v. 2.0. If a copy of the MPL was not distributed with this
3 ! file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 #ifndef DO_COMPLEX_TYPE
5
6 module mbd_dipole
7 !! Construction of dipole tensors and dipole matrices.
8
9 use mbd_constants
10 use mbd_matrix, only: matrix_re_t, matrix_cplx_t
11 use mbd_geom, only: geom_t, supercell_circum
12 use mbd_damping, only: damping_t, damping_fermi, damping_sqrtfermi, &
13 op1minus_grad
14 use mbd_gradients, only: grad_t, grad_matrix_re_t, grad_matrix_cplx_t, &
15 grad_scalar_t, grad_request_t
16 use mbd_lapack, only: eigvals, inverse
17 use mbd_linalg, only: outer
18 use mbd_utils, only: tostr, shift_idx
19
20 implicit none
21
22 private
23 public :: dipole_matrix, T_bare, T_erf_coulomb, damping_grad, T_erfc, B_erfc, C_erfc
24
25 interface dipole_matrix
26 !! Form either a real or a complex dipole matrix.
27 !!
28 !! The real-typed version is equivalent to \(\mathbf q=0\).
29 !!
30 !! $$
31 !! \boldsymbol{\mathcal A}:=[\mathbf a_1\mathbf a_2\mathbf
32 !! a_3],\qquad\boldsymbol{\mathcal B}:=[\mathbf b_1\mathbf b_2\mathbf b_3]
33 !! \\ \boldsymbol{\mathcal B}=2\pi(\boldsymbol{\mathcal A}^{-1})^\mathrm
34 !! T,\qquad \partial\boldsymbol{\mathcal B}=-\big((\partial\boldsymbol{\mathcal
35 !! A})\boldsymbol{\mathcal A}^{-1}\big)^\mathrm T\boldsymbol{\mathcal B}
36 !! \\ \mathbf R_\mathbf n=\boldsymbol{\mathcal A}\mathbf
37 !! n,\qquad\partial\mathbf R_\mathbf n=(\partial\boldsymbol{\mathcal
38 !! A})\mathbf n,
39 !! \\ \mathbf G_\mathbf m=\boldsymbol{\mathcal B}\mathbf m,\qquad
40 !! \partial\mathbf G_\mathbf m=-\big((\partial\boldsymbol{\mathcal
41 !! A})\boldsymbol{\mathcal A}^{-1}\big)^\mathrm T\mathbf G_\mathbf m,
42 !! \\ \frac{\partial G_{\mathbf ma}}{\partial A_{bc}}=-\mathcal A^{-1}_{ca}G_{\mathbf mb}
43 !! $$
44 !!
45 !! $$
46 !! \begin{gathered}
47 !! \mathbf T_{ij}(\mathbf q)=\sum_{\mathbf n}\mathbf T(\mathbf R_{\mathbf
48 !! nij})\mathrm e^{-\mathrm i\mathbf q\cdot\mathbf R_{\mathbf nij}},\quad\mathbf
49 !! R_{\mathbf nij}=\mathbf R_j+\mathbf R_\mathbf n-\mathbf R_i
50 !! \\ \frac{\mathrm d\mathbf R_{\mathbf nij}}{\mathrm d\mathbf
51 !! R_k}=(\delta_{jk}-\delta_{ik})\mathbf I
52 !! \\ \mathbf{T}_{ij}(\mathbf{q})\approx\mathbf{T}^\text{Ew}_{ij}(\mathbf{q})
53 !! =\sum_\mathbf n^{|\mathbf R_{\mathbf nij}|<R_\text c}\mathbf
54 !! T^\text{erfc}(\mathbf R_{\mathbf nij};\gamma)\mathrm e^{-\mathrm i\mathbf
55 !! q\cdot\mathbf R_{\mathbf nij}} +\frac{4\pi}{V_\text{uc}}\sum_{\mathbf
56 !! m}^{0<|\mathbf k_\mathbf m|<k_\text c}\mathbf{\hat k}_\mathbf
57 !! m\otimes\mathbf{\hat k}_\mathbf m\,\mathrm e^{-\frac{k_\mathbf
58 !! m^2}{4\gamma^2}-\mathrm i\mathbf G_\mathbf m\cdot\mathbf R_{ij}}
59 !! \\ -\frac{4\gamma^3}{3\sqrt\pi}\delta_{ij}\mathbf I +\delta(\mathbf q)\frac{4
60 !! \pi}{3 V_\text{uc}}\mathbf I,\qquad \mathbf k_\mathbf m=\mathbf G_\mathbf
61 !! m+\mathbf q
62 !! \end{gathered}
63 !! $$
64 !!
65 !!
66 !! $$
67 !! \partial\left(\frac{4\pi}{V_\text{uc}}\right)=-\frac{4\pi}{V_\text{uc}}\frac{\partial
68 !! V_\text{uc}}{V_\text{uc}},\qquad\frac{\partial
69 !! V_\text{uc}}{\partial\boldsymbol{\mathcal
70 !! A}}=V_\text{uc}(\boldsymbol{\mathcal A}^{-1})^\mathrm T
71 !! \\ \partial(k^2)=2\mathbf k\cdot\partial\mathbf k
72 !! \\ \mathbf{\hat k}\otimes\partial\mathbf{\hat k}=\frac{\mathbf
73 !! k\otimes\partial\mathbf k}{k^2}-\frac{\mathbf k\otimes\mathbf
74 !! k}{2k^4}\partial(k^2)
75 !! $$
76 !!
77 !! $$
78 !! \gamma:=\frac{2.5}{\sqrt[3]{V_\text{uc}}},\quad R_\text
79 !! c:=\frac6\gamma,\quad k_\text c:=10\gamma
80 !! $$
81 module procedure dipole_matrix_real
82 module procedure dipole_matrix_complex
83 end interface
84
85 contains
86
87 #endif
88
89 #ifndef DO_COMPLEX_TYPE
90 type(matrix_re_t) function dipole_matrix_real( &
91 geom, damp, ddipmat, grad) result(dipmat)
92 use mbd_constants, only: ZERO => ZERO_REAL
93 #else
94 type(matrix_cplx_t) function dipole_matrix_complex( &
95 geom, damp, ddipmat, grad, q) result(dipmat)
96 use mbd_constants, only: ZERO => ZERO_COMPLEX
97 #endif
98
99 type(geom_t), intent(inout) :: geom
100 type(damping_t), intent(in) :: damp
101 type(grad_request_t), intent(in), optional :: grad
102 #ifndef DO_COMPLEX_TYPE
103 type(grad_matrix_re_t), intent(out), optional :: ddipmat
104 #else
105 type(grad_matrix_cplx_t), intent(out), optional :: ddipmat
106 real(dp), intent(in) :: q(3)
107 #endif
108
109 real(dp) :: Rn(3), Rnij(3), Rnij_norm, T(3, 3), f_damp, &
110 sigma_ij, T0(3, 3), beta_R_vdw
111 integer :: i_atom, j_atom, i_cell, n(3), range_n(3), i, j, &
112 n_atoms, my_i_atom, my_j_atom, i_latt, my_nr, my_nc
113 logical :: do_ewald, is_periodic
114 type(grad_matrix_re_t) :: dT, dT0, dTew
115 type(grad_scalar_t) :: df
116 type(grad_request_t) :: grad_ij
117 #ifndef DO_COMPLEX_TYPE
118 real(dp) :: Tij(3, 3)
119 type(grad_matrix_re_t) :: dTij
120 #else
121 complex(dp) :: Tij(3, 3), exp_qR
122 type(grad_matrix_cplx_t) :: dTij
123 #endif
124
125 do_ewald = .false.
126 is_periodic = allocated(geom%lattice)
127 n_atoms = geom%siz()
128 if (present(grad)) then
129 grad_ij = grad
130 grad_ij%dcoords = grad%dcoords .or. grad%dlattice
131 end if
132 #ifdef WITH_SCALAPACK
133 call dipmat%init(geom%idx, geom%blacs)
134 #else
135 call dipmat%init(geom%idx)
136 #endif
137 if (is_periodic) then
138 do_ewald = geom%gamm > 0d0
139 range_n = supercell_circum(geom%lattice, geom%real_space_cutoff)
140 else
141 range_n(:) = 0
142 end if
143 if (grad_ij%dcoords) allocate (dTij%dr(3, 3, 3))
144 my_nr = size(dipmat%idx%i_atom)
145 my_nc = size(dipmat%idx%j_atom)
146 allocate (dipmat%val(3 * my_nr, 3 * my_nc), source=ZERO)
147 if (present(grad)) then
148 if (grad%dcoords) allocate (ddipmat%dr(3 * my_nr, 3 * my_nc, 3), source=ZERO)
149 if (grad%dlattice) then
150 allocate (ddipmat%dlattice(3 * my_nr, 3 * my_nc, 3, 3), source=ZERO)
151 end if
152 if (grad%dr_vdw) then
153 allocate (ddipmat%dvdw(3 * my_nr, 3 * my_nc), source=ZERO)
154 allocate (dTij%dvdw(3, 3))
155 end if
156 if (grad%dsigma) then
157 allocate (ddipmat%dsigma(3 * my_nr, 3 * my_nc), source=ZERO)
158 allocate (dTij%dsigma(3, 3))
159 end if
160 #ifdef DO_COMPLEX_TYPE
161 if (grad%dq) then
162 allocate (ddipmat%dq(3 * my_nr, 3 * my_nc, 3), source=ZERO)
163 allocate (dTij%dq(3, 3, 3))
164 end if
165 #endif
166 end if
167 call geom%clock(11)
168 n = [0, 0, -1]
169 each_cell: do i_cell = 1, product(1 + 2 * range_n)
170 call shift_idx(n, -range_n, range_n)
171 if (is_periodic) then
172 Rn = matmul(geom%lattice, n)
173 else
174 Rn(:) = 0d0
175 end if
176 each_atom: do my_i_atom = 1, size(dipmat%idx%i_atom)
177 i_atom = dipmat%idx%i_atom(my_i_atom)
178 each_atom_pair: do my_j_atom = 1, size(dipmat%idx%j_atom)
179 j_atom = dipmat%idx%j_atom(my_j_atom)
180 if (i_cell == 1) then
181 if (i_atom == j_atom) cycle
182 end if
183 Rnij = geom%coords(:, i_atom) - geom%coords(:, j_atom) - Rn
184 Rnij_norm = sqrt(sum(Rnij**2))
185 if (is_periodic .and. Rnij_norm > geom%real_space_cutoff) cycle
186 if (allocated(damp%R_vdw)) then
187 beta_R_vdw = damp%beta * sum(damp%R_vdw([i_atom, j_atom]))
188 end if
189 if (allocated(damp%sigma)) then
190 sigma_ij = damp%mayer_scaling &
191 * sqrt(sum(damp%sigma([i_atom, j_atom])**2))
192 end if
193 select case (damp%version)
194 case ("bare")
195 T = T_bare(Rnij, dT, grad_ij%dcoords)
196 case ("dip,1mexp")
197 T = T_1mexp_coulomb(Rnij, beta_R_vdw, damp%a)
198 case ("fermi,dip")
199 f_damp = damping_fermi(Rnij, beta_R_vdw, damp%a, df, grad_ij)
200 T0 = T_bare(Rnij, dT0, grad_ij%dcoords)
201 T = damping_grad(f_damp, df, T0, dT0, dT, grad_ij)
202 case ("sqrtfermi,dip")
203 T = damping_sqrtfermi(Rnij, beta_R_vdw, damp%a) * T_bare(Rnij)
204 case ("custom,dip")
205 T = damp%damping_custom(i_atom, j_atom) * T_bare(Rnij)
206 case ("dip,custom")
207 T = damp%potential_custom(:, :, i_atom, j_atom)
208 case ("dip,gg")
209 T = T_erf_coulomb(Rnij, sigma_ij, dT, grad_ij)
210 case ("fermi,dip,gg")
211 f_damp = damping_fermi(Rnij, beta_R_vdw, damp%a, df, grad_ij)
212 call op1minus_grad(f_damp, df)
213 T0 = T_erf_coulomb(Rnij, sigma_ij, dT0, grad_ij)
214 T = damping_grad(f_damp, df, T0, dT0, dT, grad_ij)
215 do_ewald = .false.
216 case ("sqrtfermi,dip,gg")
217 T = (1d0 - damping_sqrtfermi(Rnij, beta_R_vdw, damp%a)) * &
218 T_erf_coulomb(Rnij, sigma_ij)
219 do_ewald = .false.
220 case ("custom,dip,gg")
221 T = (1d0 - damp%damping_custom(i_atom, j_atom)) * &
222 T_erf_coulomb(Rnij, sigma_ij)
223 do_ewald = .false.
224 end select
225 if (grad_ij%dr_vdw) dT%dvdw = damp%beta * dT%dvdw
226 if (do_ewald) then
227 T = T &
228 + T_erfc(Rnij, geom%gamm, dTew, grad_ij) &
229 - T_bare(Rnij, dT0, grad_ij%dcoords)
230 if (grad_ij%dcoords) dT%dr = dT%dr + dTew%dr - dT0%dr
231 end if
232 Tij = T
233 if (grad_ij%dcoords) dTij%dr = dT%dr
234 if (grad_ij%dr_vdw) dTij%dvdw = dT%dvdw
235 if (grad_ij%dsigma) dTij%dsigma = dT%dsigma
236 #ifdef DO_COMPLEX_TYPE
237 exp_qR = exp(-IMI * (dot_product(q, Rnij)))
238 Tij = T * exp_qR
239 if (grad_ij%dcoords) then
240 #ifndef WITHOUT_DO_CONCURRENT
241 do concurrent(i=1:3)
242 #else
243 do i = 1, 3
244 #endif
245 dTij%dr(:, :, i) = dT%dr(:, :, i) * exp_qR - IMI * q(i) * Tij
246 end do
247 end if
248 if (grad_ij%dsigma) dTij%dsigma = dT%dsigma * exp_qR
249 if (grad_ij%dr_vdw) dTij%dvdw = dT%dvdw * exp_qR
250 if (grad_ij%dq) then
251 do concurrent(i=1:3)
252 dTij%dq(:, :, i) = -IMI * Rnij(i) * Tij
253 end do
254 end if
255 #endif
256 i = 3 * (my_i_atom - 1)
257 j = 3 * (my_j_atom - 1)
258 associate (T_sub => dipmat%val(i + 1:i + 3, j + 1:j + 3))
259 T_sub = T_sub + Tij
260 end associate
261 if (.not. present(grad)) cycle
262 if (grad%dcoords .and. i_atom /= j_atom) then
263 associate (dTdR_sub => ddipmat%dr(i + 1:i + 3, j + 1:j + 3, :))
264 dTdR_sub = dTdR_sub + dTij%dr
265 end associate
266 end if
267 if (grad%dlattice) then
268 do i_latt = 1, 3
269 associate ( &
270 dTda_sub => ddipmat%dlattice(i + 1:i + 3, j + 1:j + 3, i_latt, :) &
271 )
272 dTda_sub = dTda_sub - dTij%dr * (n(i_latt))
273 end associate
274 end do
275 end if
276 if (grad%dr_vdw) then
277 associate (dTdRvdw_sub => ddipmat%dvdw(i + 1:i + 3, j + 1:j + 3))
278 dTdRvdw_sub = dTdRvdw_sub + dTij%dvdw
279 end associate
280 end if
281 if (grad%dsigma) then
282 associate (dTdsigma_sub => ddipmat%dsigma(i + 1:i + 3, j + 1:j + 3))
283 dTdsigma_sub = dTdsigma_sub + dTij%dsigma
284 end associate
285 end if
286 #ifdef DO_COMPLEX_TYPE
287 if (grad%dq) then
288 associate (dTdq_sub => ddipmat%dq(i + 1:i + 3, j + 1:j + 3, :))
289 dTdq_sub = dTdq_sub + dTij%dq
290 end associate
291 end if
292 #endif
293 end do each_atom_pair
294 end do each_atom
295 end do each_cell
296 call geom%clock(-11)
297 if (do_ewald) then
298 #ifndef DO_COMPLEX_TYPE
299 call add_ewald_dipole_parts_real(geom, dipmat, ddipmat, grad)
300 #else
301 call add_ewald_dipole_parts_complex(geom, dipmat, ddipmat, grad, q)
302 #endif
303 end if
304 end function
305
306 #ifndef DO_COMPLEX_TYPE
307 subroutine add_ewald_dipole_parts_real(geom, dipmat, ddipmat, grad)
308 type(matrix_re_t), intent(inout) :: dipmat
309 type(grad_matrix_re_t), intent(inout), optional :: ddipmat
310 #else
311 subroutine add_ewald_dipole_parts_complex(geom, dipmat, ddipmat, grad, q)
312 type(matrix_cplx_t), intent(inout) :: dipmat
313 type(grad_matrix_cplx_t), intent(inout), optional :: ddipmat
314 #endif
315 type(geom_t), intent(inout) :: geom
316 type(grad_request_t), intent(in), optional :: grad
317 #ifdef DO_COMPLEX_TYPE
318 real(dp), intent(in) :: q(3)
319 #endif
320
321 logical :: do_surface
322 real(dp) :: rec_latt(3, 3), volume, G(3), Rij(3), k(3), &
323 k_sq, G_Rij, latt_inv(3, 3), &
324 dGdA(3), dk_sqdA, dkk_dA(3, 3), vol_prefactor, &
325 k_otimes_k(3, 3), exp_k_sq_gamma, vol_kk_exp_ksq(3, 3)
326 integer :: &
327 i_atom, j_atom, i, j, i_xyz, m(3), i_m, &
328 range_m(3), my_i_atom, my_j_atom, i_latt, a, b
329 #ifndef DO_COMPLEX_TYPE
330 real(dp) :: Tij(3, 3), exp_GR, vol_exp
331 #else
332 complex(dp) :: Tij(3, 3), exp_GR, vol_exp
333 integer :: c
334 real(dp) :: dkk_dq(3, 3, 3)
335 #endif
336
337 latt_inv = inverse(geom%lattice)
338 rec_latt = 2 * pi * transpose(latt_inv)
339 volume = abs(dble(product(eigvals(geom%lattice))))
340 vol_prefactor = 4 * pi / volume
341 range_m = supercell_circum(rec_latt, geom%rec_space_cutoff)
342 call geom%clock(12)
343 m = [0, 0, -1]
344 each_recip_vec: do i_m = 1, product(1 + 2 * range_m)
345 call shift_idx(m, -range_m, range_m)
346 G = matmul(rec_latt, m)
347 #ifdef DO_COMPLEX_TYPE
348 k = G + q
349 #else
350 k = G
351 #endif
352 k_sq = sum(k**2)
353 if (sqrt(k_sq) > geom%rec_space_cutoff .or. sqrt(k_sq) < 1d-15) cycle
354 exp_k_sq_gamma = exp(-k_sq / (4 * geom%gamm**2))
355 do concurrent(a=1:3, b=1:3)
356 k_otimes_k(a, b) = k(a) * k(b) / k_sq
357 end do
358 each_atom: do my_i_atom = 1, size(dipmat%idx%i_atom)
359 i_atom = dipmat%idx%i_atom(my_i_atom)
360 each_atom_pair: do my_j_atom = 1, size(dipmat%idx%j_atom)
361 j_atom = dipmat%idx%j_atom(my_j_atom)
362 Rij = geom%coords(:, i_atom) - geom%coords(:, j_atom)
363 G_Rij = dot_product(G, Rij)
364 #ifdef DO_COMPLEX_TYPE
365 exp_GR = exp(IMI * G_Rij)
366 #else
367 exp_GR = cos(G_Rij)
368 #endif
369 vol_kk_exp_ksq = vol_prefactor * k_otimes_k * exp_k_sq_gamma
370 Tij = vol_kk_exp_ksq * exp_GR
371 i = 3 * (my_i_atom - 1)
372 j = 3 * (my_j_atom - 1)
373 associate (T_sub => dipmat%val(i + 1:i + 3, j + 1:j + 3))
374 T_sub = T_sub + Tij
375 end associate
376 if (.not. present(grad)) cycle
377 vol_exp = vol_prefactor * exp_k_sq_gamma * exp_GR
378 if (grad%dcoords .and. i_atom /= j_atom) then
379 associate (dTdR_sub => ddipmat%dr(i + 1:i + 3, j + 1:j + 3, :))
380 ! TODO should be do-concurrent, but this crashes IBM XL
381 ! 16.1.1, see issue #16
382 do i_xyz = 1, 3
383 dTdR_sub(:, :, i_xyz) = dTdR_sub(:, :, i_xyz) &
384 #ifdef DO_COMPLEX_TYPE
385 + Tij * IMI * G(i_xyz)
386 #else
387 -vol_kk_exp_ksq * sin(G_Rij) * G(i_xyz)
388 #endif
389 end do
390 end associate
391 end if
392 if (grad%dlattice) then
393 do i_latt = 1, 3
394 do i_xyz = 1, 3
395 dGdA = -latt_inv(i_latt, :) * G(i_xyz)
396 dk_sqdA = 2 * dot_product(k, dGdA)
397 do concurrent(a=1:3, b=1:3)
398 dkk_dA(a, b) = k(a) * dGdA(b) / k_sq &
399 - k(a) * k(b) * dk_sqdA / (2 * k_sq**2)
400 end do
401 dkk_dA = dkk_dA + transpose(dkk_dA)
402 ! Using associate here was causing weird seg faults
403 ! with some Intel compilers, reporting i_xyz being
404 ! zero in the index, even though it printed as 1
405 ! associate ( &
406 ! dTda_sub => ddipmat%dlattice(i+1:i+3, j+1:j+3, i_latt, i_xyz) &
407 ! )
408 ddipmat%dlattice(i + 1:i + 3, j + 1:j + 3, i_latt, i_xyz) = &
409 ddipmat%dlattice(i + 1:i + 3, j + 1:j + 3, i_latt, i_xyz) &
410 - Tij * latt_inv(i_latt, i_xyz) &
411 + vol_exp * dkk_dA &
412 - Tij * dk_sqdA / (4 * geom%gamm**2) &
413 #ifdef DO_COMPLEX_TYPE
414 + Tij * IMI * dot_product(dGdA, Rij)
415 #else
416 -vol_kk_exp_ksq * sin(G_Rij) * dot_product(dGdA, Rij)
417 #endif
418 ! end associate
419 end do
420 end do
421 end if
422 #ifdef DO_COMPLEX_TYPE
423 if (grad%dq) then
424 do concurrent(a=1:3, b=1:3, c=1:3)
425 dkk_dq(a, b, c) = -2 * k(a) * k(b) * k(c) / k_sq**2
426 end do
427 do concurrent(a=1:3, b=1:3)
428 dkk_dq(b, a, a) = dkk_dq(b, a, a) + k(b) / k_sq
429 end do
430 do concurrent(a=1:3, b=1:3)
431 dkk_dq(a, b, a) = dkk_dq(a, b, a) + k(b) / k_sq
432 end do
433 associate (dTdq_sub => ddipmat%dq(i + 1:i + 3, j + 1:j + 3, :))
434 dTdq_sub = dTdq_sub + vol_exp * dkk_dq
435 ! TODO should be do-concurrent, but this crashes IBM XL
436 ! 16.1.1, see issue #16
437 do a = 1, 3
438 dTdq_sub(:, :, a) = dTdq_sub(:, :, a) &
439 - Tij * k(a) / (2 * geom%gamm**2)
440 end do
441 end associate
442 end if
443 #endif
444 end do each_atom_pair
445 end do each_atom
446 end do each_recip_vec
447 ! self energy
448 call dipmat%add_diag_scalar(-4 * geom%gamm**3 / (3 * sqrt(pi)))
449 ! surface term
450 #ifdef DO_COMPLEX_TYPE
451 do_surface = sqrt(sum(q**2)) < 1d-15
452 #else
453 do_surface = .true.
454 #endif
455 if (do_surface) then
456 do my_i_atom = 1, size(dipmat%idx%i_atom)
457 do my_j_atom = 1, size(dipmat%idx%j_atom)
458 do i_xyz = 1, 3
459 i = 3 * (my_i_atom - 1) + i_xyz
460 j = 3 * (my_j_atom - 1) + i_xyz
461 dipmat%val(i, j) = dipmat%val(i, j) + vol_prefactor / 3
462 if (.not. present(grad)) cycle
463 if (grad%dlattice) then
464 ddipmat%dlattice(i, j, :, :) = ddipmat%dlattice(i, j, :, :) &
465 - vol_prefactor / 3 * latt_inv
466 end if
467 end do
468 end do
469 end do
470 end if
471 call geom%clock(-12)
472 end subroutine
473
474 #ifndef DO_COMPLEX_TYPE
475 # define DO_COMPLEX_TYPE
476 # include "mbd_dipole.F90"
477
478 function T_bare(r, dT, grad) result(T)
479 !! $$
480 !! T_{ab}(\mathbf r)=\frac{\partial^2}{\partial r_a\partial r_b}\frac1r=
481 !! \frac{-3r_ar_b+r^2\delta_{ab}}{r^5},\qquad
482 !! \frac{\partial T_{ab}(\mathbf r)}{\partial r_c}=-3\left(
483 !! \frac{r_a\delta_{bc}+r_b\delta_{ca}+r_c\delta_{ab}}{r^5}-
484 !! \frac{5r_ar_br_c}{r^7}
485 !! \right)
486 !! $$
487 real(dp), intent(in) :: r(3)
488 type(grad_matrix_re_t), intent(out), optional :: dT
489 logical, intent(in), optional :: grad
490 real(dp) :: T(3, 3)
491
492 integer :: a, b, c
493 real(dp) :: r_1, r_2, r_5, r_7
494
495 r_2 = sum(r**2)
496 r_1 = sqrt(r_2)
497 r_5 = r_1**5
498 do concurrent(a=1:3)
499 T(a, a) = (-3 * r(a)**2 + r_2) / r_5
500 do concurrent(b=a + 1:3)
501 T(a, b) = -3 * r(a) * r(b) / r_5
502 T(b, a) = T(a, b)
503 end do
504 end do
505 if (.not. present(grad)) return
506 if (.not. grad) return
507 allocate (dT%dr(3, 3, 3))
508 r_7 = r_1**7
509 do concurrent(a=1:3)
510 dT%dr(a, a, a) = -3 * (3 * r(a) / r_5 - 5 * r(a)**3 / r_7)
511 do concurrent(b=a + 1:3)
512 dT%dr(a, a, b) = -3 * (r(b) / r_5 - 5 * r(a)**2 * r(b) / r_7)
513 dT%dr(a, b, a) = dT%dr(a, a, b)
514 dT%dr(b, a, a) = dT%dr(a, a, b)
515 dT%dr(b, b, a) = -3 * (r(a) / r_5 - 5 * r(b)**2 * r(a) / r_7)
516 dT%dr(b, a, b) = dT%dr(b, b, a)
517 dT%dr(a, b, b) = dT%dr(b, b, a)
518 do concurrent(c=b + 1:3)
519 dT%dr(a, b, c) = 15 * r(a) * r(b) * r(c) / r_7
520 dT%dr(a, c, b) = dT%dr(a, b, c)
521 dT%dr(b, a, c) = dT%dr(a, b, c)
522 dT%dr(b, c, a) = dT%dr(a, b, c)
523 dT%dr(c, a, b) = dT%dr(a, b, c)
524 dT%dr(c, b, a) = dT%dr(a, b, c)
525 end do
526 end do
527 end do
528 end function
529
530 real(dp) function B_erfc(r, gamm, dB, grad) result(B)
531 !! $$
532 !! \begin{aligned}
533 !! B(R,\gamma)
534 !! &=\operatorname{erfc}(\gamma R)
535 !! +\frac{2\gamma R}{\sqrt\pi}\mathrm e^{-(\gamma R)^2}
536 !! \\ \partial B(R,\gamma)
537 !! &=-\frac4{\sqrt\pi}(\gamma R)^2\mathrm e^{-(\gamma R)^2}
538 !! (R\partial\gamma+\gamma\partial R)
539 !! \end{aligned}
540 !! $$
541 real(dp), intent(in) :: r
542 real(dp), intent(in) :: gamm
543 type(grad_scalar_t), intent(out), optional :: dB
544 type(grad_request_t), intent(in), optional :: grad
545
546 real(dp) :: tmp, gamma_r_sq
547
548 gamma_r_sq = (gamm * r)**2
549 B = (erfc(gamm * r) + (2 * gamm * r / sqrt(pi)) * exp(-gamma_r_sq))
550 if (.not. present(grad)) return
551 tmp = -4d0 / sqrt(pi) * gamma_r_sq * exp(-gamma_r_sq)
552 if (grad%dcoords) dB%dr_1 = tmp * gamm
553 if (grad%dgamma) dB%dgamma = tmp * r
554 end function
555
556 real(dp) function C_erfc(r, gamm, dC, grad) result(C)
557 !! $$
558 !! \begin{aligned}
559 !! C(r,\gamma)
560 !! &=3\operatorname{erfc}(\gamma R)
561 !! +\frac{2\gamma R}{\sqrt\pi}(3+2(\gamma R)^2)\mathrm e^{-(\gamma R)^2}
562 !! \\ \partial C(R,\gamma)
563 !! &=-\frac8{\sqrt\pi}(\gamma R)^4\mathrm e^{-(\gamma R)^2}
564 !! (R\partial\gamma+\gamma\partial R)
565 !! \end{aligned}
566 !! $$
567 real(dp), intent(in) :: r
568 real(dp), intent(in) :: gamm
569 type(grad_scalar_t), intent(out), optional :: dC
570 type(grad_request_t), intent(in), optional :: grad
571
572 real(dp) :: tmp, gamma_r_sq
573
574 gamma_r_sq = (gamm * r)**2
575 C = (3 * erfc(gamm * r) + (2 * gamm * r / sqrt(pi)) * (3d0 + 2 * gamma_r_sq) * exp(-gamma_r_sq))
576 if (.not. present(grad)) return
577 tmp = -8d0 / sqrt(pi) * gamma_r_sq**2 * exp(-gamma_r_sq)
578 if (grad%dcoords) dC%dr_1 = tmp * gamm
579 if (grad%dgamma) dC%dgamma = tmp * r
580 end function
581
582 function T_erfc(r, gamm, dT, grad) result(T)
583 !! $$
584 !! T_{ab}^\text{erfc}(\mathbf r,\gamma)
585 !! =-3\frac{r_ar_b}{r^5}C(r,\gamma)+\frac{\delta_{ab}}{r^3}B(r,\gamma)
586 !! $$
587 !!
588 !! $$
589 !! \begin{aligned}
590 !! \frac{\partial T_{ab}^\text{erfc}(\mathbf r,\gamma)}{\partial r_c}
591 !! &=-\left(
592 !! \frac{r_a\delta_{bc}+r_b\delta_{ca}}{r^5}-
593 !! 5\frac{r_ar_br_c}{r^7}
594 !! \right)C(r,\gamma)-3\frac{r_c\delta_{ab}}{r^5}B(r,\gamma)
595 !! \\ &-\frac{r_ar_br_c}{r^6}\frac{\partial C(r,\gamma)}{\partial
596 !! r}+\frac{r_c\delta_{ab}}{r^4}\frac{\partial B(r,\gamma)}{\partial r}
597 !! \end{aligned}
598 !! $$
599 real(dp), intent(in) :: r(3)
600 real(dp), intent(in) :: gamm
601 type(grad_matrix_re_t), intent(out), optional :: dT
602 type(grad_request_t), intent(in), optional :: grad
603 real(dp) :: T(3, 3)
604
605 integer :: a, b, c
606 real(dp) :: r_1, r_2, r_3, r_4, r_5, r_6, r_7, B_ew, C_ew
607 type(grad_scalar_t) :: dB, dC
608
609 r_2 = sum(r**2)
610 r_1 = sqrt(r_2)
611 r_3 = r_1 * r_2
612 r_5 = r_3 * r_2
613 B_ew = B_erfc(r_1, gamm, dB, grad)
614 C_ew = C_erfc(r_1, gamm, dC, grad)
615 do concurrent(a=1:3)
616 T(a, a) = -C_ew * r(a)**2 / r_5 + B_ew / r_3
617 do concurrent(b=a + 1:3)
618 T(a, b) = -C_ew * r(a) * r(b) / r_5
619 T(b, a) = T(a, b)
620 end do
621 end do
622 if (.not. present(grad)) return
623 if (grad%dcoords) then
624 allocate (dT%dr(3, 3, 3))
625 r_7 = r_1**7
626 r_4 = r_2**2
627 r_6 = r_4 * r_2
628 #ifndef WITHOUT_DO_CONCURRENT
629 do concurrent(c=1:3)
630 #else
631 do c = 1, 3
632 #endif
633 dT%dr(c, c, c) = &
634 -(2 * r(c) / r_5 - 5 * r(c)**3 / r_7) * C_ew - 3 * r(c) / r_5 * B_ew &
635 - r(c)**3 / r_6 * dC%dr_1 + r(c) / r_4 * dB%dr_1
636 #ifndef WITHOUT_DO_CONCURRENT
637 do concurrent(a=1:3, a /= c)
638 #else
639 do a = 1, 3
640 if (a == c) cycle
641 #endif
642 dT%dr(a, c, c) = &
643 -(r(a) / r_5 - 5 * r(a) * r(c)**2 / r_7) * C_ew &
644 - r(a) * r(c)**2 / r_6 * dC%dr_1
645 dT%dr(c, a, c) = dT%dr(a, c, c)
646 dT%dr(a, a, c) = &
647 5 * r(a)**2 * r(c) / r_7 * C_ew - 3 * r(c) / r_5 * B_ew &
648 - r(a)**2 * r(c) / r_6 * dC%dr_1 + r(c) / r_4 * dB%dr_1
649 #ifndef WITHOUT_DO_CONCURRENT
650 do concurrent(b=a + 1:3, b /= c)
651 #else
652 do b = a + 1, 3
653 if (b == c) cycle
654 #endif
655 dT%dr(a, b, c) = &
656 5 * r(a) * r(b) * r(c) / r_7 * C_ew - r(a) * r(b) * r(c) / r_6 * dC%dr_1
657 dT%dr(b, a, c) = dT%dr(a, b, c)
658 end do
659 end do
660 end do
661 end if
662 if (grad%dgamma) then
663 allocate (dT%dgamma(3, 3))
664 do concurrent(a=1:3)
665 dT%dgamma(a, a) = -dC%dgamma * r(a)**2 / r_5 + dB%dgamma / r_3
666 do concurrent(b=a + 1:3)
667 dT%dgamma(a, b) = -dC%dgamma * r(a) * r(b) / r_5
668 dT%dgamma(b, a) = dT%dgamma(a, b)
669 end do
670 end do
671 end if
672 end function
673
674 function T_erf_coulomb(r, sigma, dT, grad) result(T)
675 !! $$
676 !! \begin{aligned}
677 !! T^\text{GG}_{ab}(\mathbf r,\sigma)&=
678 !! \frac{\partial^2}{\partial r_a\partial r_b}\frac{\operatorname{erf}(\zeta)}r=
679 !! \big(\operatorname{erf}(\zeta)-\Theta(\zeta)\big)T_{ab}(\mathbf r)+
680 !! 2\zeta^2\Theta(\zeta)\frac{r_ar_b}{r^5}
681 !! \\ \Theta(\zeta)&=\frac{2\zeta}{\sqrt\pi}\exp(-\zeta^2),\qquad
682 !! \zeta=\frac r\sigma
683 !! \\ \frac{\mathrm d T_{ab}^\text{GG}(\mathbf r,\sigma)}{\mathrm dr_c}&=
684 !! 2\zeta\Theta(\zeta)\left(T_{ab}(\mathbf r)+(3-2\zeta^2)\frac{r_ar_b}{r^5}\right)
685 !! \frac{\mathrm d\zeta}{\mathrm dr_c}
686 !! \\ &+\big(\operatorname{erf}(\zeta)-\Theta(\zeta)\big)
687 !! \frac{\partial T_{ab}(\mathbf r)}{\partial r_c}-
688 !! 2\zeta^2\Theta(\zeta)\left(
689 !! \frac13\frac{\partial T_{ab}(\mathbf r)}{\partial r_c}+
690 !! \frac{r_c\delta_{ab}}{r^5}
691 !! \right)
692 !! \\ \qquad\frac{\mathrm d\zeta}{\mathrm dr_c}&=
693 !! \frac{r_c}{r\sigma}-\frac r{\sigma^2}\frac{\mathrm d\sigma}{\mathrm dr_c}
694 !! \end{aligned}
695 !! $$
696 real(dp), intent(in) :: r(3)
697 real(dp), intent(in) :: sigma
698 type(grad_matrix_re_t), intent(out), optional :: dT
699 type(grad_request_t), intent(in), optional :: grad
700 real(dp) :: T(3, 3)
701
702 real(dp) :: theta, erf_theta, r_5, r_1, zeta, bare(3, 3)
703 type(grad_matrix_re_t) :: dbare
704 real(dp) :: tmp33(3, 3), tmp333(3, 3, 3), rr_r5(3, 3)
705 integer :: a, c
706
707 bare = T_bare(r, dbare, grad%dcoords)
708 r_1 = sqrt(sum(r**2))
709 r_5 = r_1**5
710 rr_r5 = outer(r, r) / r_5
711 zeta = r_1 / sigma
712 theta = 2 * zeta / sqrt(pi) * exp(-zeta**2)
713 erf_theta = erf(zeta) - theta
714 T = erf_theta * bare + 2 * (zeta**2) * theta * rr_r5
715 if (.not. present(grad)) return
716 tmp33 = 2 * zeta * theta * (bare + (3 - 2 * zeta**2) * rr_r5)
717 if (grad%dcoords) then
718 allocate (dT%dr(3, 3, 3))
719 do concurrent(c=1:3)
720 dT%dr(:, :, c) = tmp33 * r(c) / (r_1 * sigma)
721 end do
722 tmp333 = dbare%dr / 3
723 do concurrent(a=1:3, c=1:3)
724 tmp333(a, a, c) = tmp333(a, a, c) + r(c) / r_5
725 end do
726 dT%dr = dT%dr + erf_theta * dbare%dr - 2 * (zeta**2) * theta * tmp333
727 end if
728 if (grad%dsigma) dT%dsigma = -tmp33 * r_1 / sigma**2
729 end function
730
731 function T_1mexp_coulomb(rxyz, sigma, a) result(T)
732 real(dp), intent(in) :: rxyz(3), sigma, a
733 real(dp) :: T(3, 3)
734
735 real(dp) :: r_sigma, zeta_1, zeta_2
736
737 r_sigma = (sqrt(sum(rxyz**2)) / sigma)**a
738 zeta_1 = 1d0 - exp(-r_sigma) - a * r_sigma * exp(-r_sigma)
739 zeta_2 = -r_sigma * a * exp(-r_sigma) * (1 + a * (-1 + r_sigma))
740 T = zeta_1 * T_bare(rxyz) - zeta_2 * outer(rxyz, rxyz) / sqrt(sum(rxyz**2))**5
741 end function
742
743 function damping_grad(f, df, T, dT, dfT, grad) result(fT)
744 real(dp), intent(in) :: f
745 type(grad_scalar_t), intent(in) :: df
746 real(dp), intent(in) :: T(3, 3)
747 type(grad_matrix_re_t), intent(in) :: dT
748 type(grad_matrix_re_t), intent(out) :: dfT
749 type(grad_request_t), intent(in) :: grad
750 real(dp) :: fT(3, 3)
751
752 integer :: c
753
754 fT = f * T
755 if (grad%dcoords) then
756 allocate (dfT%dr(3, 3, 3), source=0d0)
757 if (allocated(df%dr)) then
758 do concurrent(c=1:3)
759 dfT%dr(:, :, c) = df%dr(c) * T
760 end do
761 end if
762 if (allocated(dT%dr)) dfT%dr = dfT%dr + f * dT%dr
763 end if
764 if (grad%dr_vdw) then
765 allocate (dfT%dvdw(3, 3), source=0d0)
766 if (allocated(df%dvdw)) dfT%dvdw = df%dvdw * T
767 if (allocated(dT%dvdw)) dfT%dvdw = dfT%dvdw + f * dT%dvdw
768 end if
769 if (grad%dsigma) dfT%dsigma = f * dT%dsigma
770 end function
771
772 end module
773
774 #endif