| 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 |
|
| 5 |
module mbd_ts |
| 6 |
!! Obtaining TS energies. |
| 7 |
|
| 8 |
use mbd_constants |
| 9 |
use mbd_utils, only: shift_idx, tostr, result_t, diff3 |
| 10 |
use mbd_damping, only: damping_t, damping_fermi |
| 11 |
use mbd_geom, only: geom_t, supercell_circum |
| 12 |
use mbd_gradients, only: grad_request_t, grad_scalar_t |
| 13 |
use mbd_lapack, only: eigvals, inverse |
| 14 |
#ifdef WITH_MPI |
| 15 |
use mbd_mpi, only: mpi_all_reduce |
| 16 |
#endif |
| 17 |
|
| 18 |
implicit none |
| 19 |
|
| 20 |
private |
| 21 |
public :: get_ts_energy |
| 22 |
|
| 23 |
contains |
| 24 |
|
| 25 |
type(result_t) function get_ts_energy(geom, alpha_0, C6, damp, grad) result(res) |
| 26 |
!! Get TS energy. |
| 27 |
type(geom_t), intent(inout) :: geom |
| 28 |
real(dp), intent(in) :: alpha_0(:) |
| 29 |
real(dp), intent(in) :: C6(:) |
| 30 |
type(damping_t), intent(in) :: damp |
| 31 |
type(grad_request_t), intent(in) :: grad |
| 32 |
|
| 33 |
real(dp) :: C6_ij, Rnij(3), Rnij_norm, R_vdw_ij, ene_ij, Rn(3), f_damp |
| 34 |
integer :: i_cell, i_atom, j_atom, range_n(3), n(3), n_atoms, i_latt |
| 35 |
logical :: is_periodic, do_ewald |
| 36 |
type(grad_request_t) :: grad_ij |
| 37 |
type(grad_scalar_t) :: df, dC6, dphi, dene_ij |
| 38 |
|
| 39 |
do_ewald = .false. |
| 40 |
is_periodic = allocated(geom%lattice) |
| 41 |
n_atoms = geom%siz() |
| 42 |
grad_ij = grad |
| 43 |
grad_ij%dcoords = grad%dcoords .or. grad%dlattice |
| 44 |
if (is_periodic) then |
| 45 |
do_ewald = geom%gamm > 0d0 |
| 46 |
range_n = supercell_circum(geom%lattice, geom%real_space_cutoff) |
| 47 |
else |
| 48 |
range_n(:) = 0 |
| 49 |
end if |
| 50 |
if (grad%dcoords) allocate (res%dE%dcoords(n_atoms, 3), source=0d0) |
| 51 |
if (grad%dlattice) allocate (res%dE%dlattice(3, 3), source=0d0) |
| 52 |
if (grad%dC6) allocate (res%dE%dC6(n_atoms), source=0d0) |
| 53 |
if (grad%dalpha) allocate (res%dE%dalpha(n_atoms), source=0d0) |
| 54 |
if (grad%dr_vdw) allocate (res%dE%dr_vdw(n_atoms), source=0d0) |
| 55 |
res%energy = 0d0 |
| 56 |
n = [0, 0, -1] |
| 57 |
each_cell: do i_cell = 1, product(1 + 2 * range_n) |
| 58 |
call shift_idx(n, -range_n, range_n) |
| 59 |
if (is_periodic) then |
| 60 |
Rn = matmul(geom%lattice, n) |
| 61 |
else |
| 62 |
Rn(:) = 0d0 |
| 63 |
end if |
| 64 |
each_atom: do i_atom = 1, geom%siz() |
| 65 |
#ifdef WITH_MPI |
| 66 |
if (modulo(i_atom, geom%mpi_size) /= geom%mpi_rank) cycle |
| 67 |
#endif |
| 68 |
each_atom_pair: do j_atom = 1, i_atom |
| 69 |
if (i_cell == 1) then |
| 70 |
if (i_atom == j_atom) cycle |
| 71 |
end if |
| 72 |
Rnij = geom%coords(:, i_atom) - geom%coords(:, j_atom) - Rn |
| 73 |
Rnij_norm = sqrt(sum(Rnij**2)) |
| 74 |
if (is_periodic .and. Rnij_norm > geom%real_space_cutoff) cycle |
| 75 |
C6_ij = combine_C6( & |
| 76 |
C6(i_atom), C6(j_atom), alpha_0(i_atom), alpha_0(j_atom), dC6, grad & |
| 77 |
) |
| 78 |
if (allocated(damp%r_vdw)) then |
| 79 |
R_vdw_ij = damp%r_vdw(i_atom) + damp%r_vdw(j_atom) |
| 80 |
end if |
| 81 |
select case (damp%version) |
| 82 |
case ("fermi") |
| 83 |
f_damp = damping_fermi( & |
| 84 |
Rnij, damp%ts_sr * R_vdw_ij, damp%ts_d, df, grad_ij & |
| 85 |
) |
| 86 |
case ("fermi2") |
| 87 |
f_damp = damping_fermi(Rnij, damp%ts_sr * R_vdw_ij, damp%ts_d)**2 |
| 88 |
case ("custom") |
| 89 |
f_damp = damp%damping_custom(i_atom, j_atom) |
| 90 |
end select |
| 91 |
ene_ij = -C6_ij * f_damp / Rnij_norm**6 |
| 92 |
if (grad_ij%dcoords) & |
| 93 |
dene_ij%dr = ene_ij * (df%dr / f_damp - 6 * Rnij / Rnij_norm**2) |
| 94 |
if (grad_ij%dr_vdw) dene_ij%dvdw = ene_ij / f_damp * df%dvdw * damp%ts_sr |
| 95 |
if (do_ewald) then |
| 96 |
ene_ij = ene_ij - C6_ij * ( & |
| 97 |
disp_real(Rnij_norm, geom%gamm, dphi, grad_ij) & |
| 98 |
- 1d0 / Rnij_norm**6 & |
| 99 |
) |
| 100 |
if (grad_ij%dcoords) dene_ij%dr = dene_ij%dr & |
| 101 |
- C6_ij * (dphi%dr_1 + 6 / Rnij_norm**7) * Rnij / Rnij_norm |
| 102 |
end if |
| 103 |
if (i_atom == j_atom) then |
| 104 |
ene_ij = ene_ij / 2 |
| 105 |
if (grad_ij%dcoords) dene_ij%dr = dene_ij%dr / 2 |
| 106 |
if (grad_ij%dr_vdw) dene_ij%dvdw = dene_ij%dvdw / 2 |
| 107 |
end if |
| 108 |
res%energy = res%energy + ene_ij |
| 109 |
if (.not. grad%any()) cycle |
| 110 |
if (grad%dcoords) then |
| 111 |
res%dE%dcoords(i_atom, :) = res%dE%dcoords(i_atom, :) + dene_ij%dr |
| 112 |
res%dE%dcoords(j_atom, :) = res%dE%dcoords(j_atom, :) - dene_ij%dr |
| 113 |
end if |
| 114 |
if (grad%dlattice) then |
| 115 |
do concurrent(i_latt=1:3) |
| 116 |
res%dE%dlattice(i_latt, :) = res%dE%dlattice(i_latt, :) & |
| 117 |
- dene_ij%dr * n(i_latt) |
| 118 |
end do |
| 119 |
end if |
| 120 |
if (grad%dC6) then |
| 121 |
res%dE%dC6(i_atom) = res%dE%dC6(i_atom) + ene_ij / C6_ij * dC6%dC6i |
| 122 |
res%dE%dC6(j_atom) = res%dE%dC6(j_atom) + ene_ij / C6_ij * dC6%dC6j |
| 123 |
end if |
| 124 |
if (grad%dalpha) then |
| 125 |
res%dE%dalpha(i_atom) = res%dE%dalpha(i_atom) + ene_ij / C6_ij * dC6%da0i |
| 126 |
res%dE%dalpha(j_atom) = res%dE%dalpha(j_atom) + ene_ij / C6_ij * dC6%da0j |
| 127 |
end if |
| 128 |
if (grad%dr_vdw) then |
| 129 |
res%dE%dr_vdw(i_atom) = res%dE%dr_vdw(i_atom) + dene_ij%dvdw |
| 130 |
res%dE%dr_vdw(j_atom) = res%dE%dr_vdw(j_atom) + dene_ij%dvdw |
| 131 |
end if |
| 132 |
end do each_atom_pair |
| 133 |
end do each_atom |
| 134 |
end do each_cell |
| 135 |
if (do_ewald) call add_ewald_ts_parts(geom, alpha_0, C6, res, grad) |
| 136 |
#ifdef WITH_MPI |
| 137 |
call mpi_all_reduce(res%energy, geom%mpi_comm) |
| 138 |
if (grad%dcoords) call mpi_all_reduce(res%dE%dcoords, geom%mpi_comm) |
| 139 |
if (grad%dlattice) call mpi_all_reduce(res%dE%dlattice, geom%mpi_comm) |
| 140 |
if (grad%dalpha) call mpi_all_reduce(res%dE%dalpha, geom%mpi_comm) |
| 141 |
if (grad%dC6) call mpi_all_reduce(res%dE%dC6, geom%mpi_comm) |
| 142 |
if (grad%dR_vdw) call mpi_all_reduce(res%dE%dR_vdw, geom%mpi_comm) |
| 143 |
#endif |
| 144 |
end function |
| 145 |
|
| 146 |
subroutine add_ewald_ts_parts(geom, alpha_0, C6, res, grad) |
| 147 |
type(geom_t), intent(in) :: geom |
| 148 |
real(dp), intent(in) :: alpha_0(:) |
| 149 |
real(dp), intent(in) :: C6(:) |
| 150 |
type(result_t), intent(inout) :: res |
| 151 |
type(grad_request_t), intent(in) :: grad |
| 152 |
|
| 153 |
real(dp) :: rec_latt(3, 3), volume, Rij(3), k(3), phi, dkdAk_proj, & |
| 154 |
k_norm, k_Rij, latt_inv(3, 3), C6_ij, exp_kR, ene_ij, dkdA(3) |
| 155 |
integer :: i_atom, j_atom, m(3), i_m, range_m(3), i_latt, i_xyz |
| 156 |
type(grad_scalar_t) :: dC6, dphi, dene_ij |
| 157 |
|
| 158 |
latt_inv = inverse(geom%lattice) |
| 159 |
rec_latt = 2 * pi * transpose(latt_inv) |
| 160 |
volume = abs(dble(product(eigvals(geom%lattice)))) |
| 161 |
range_m = supercell_circum(rec_latt, geom%rec_space_cutoff) |
| 162 |
m = [0, 0, -1] |
| 163 |
each_recip_vec: do i_m = 1, product(1 + 2 * range_m) |
| 164 |
call shift_idx(m, -range_m, range_m) |
| 165 |
k = matmul(rec_latt, m) |
| 166 |
k_norm = sqrt(sum(k**2)) |
| 167 |
if (k_norm > geom%rec_space_cutoff) cycle |
| 168 |
each_atom: do i_atom = 1, geom%siz() |
| 169 |
#ifdef WITH_MPI |
| 170 |
if (modulo(i_atom, geom%mpi_size) /= geom%mpi_rank) cycle |
| 171 |
#endif |
| 172 |
each_atom_pair: do j_atom = 1, i_atom |
| 173 |
C6_ij = combine_C6( & |
| 174 |
C6(i_atom), C6(j_atom), alpha_0(i_atom), alpha_0(j_atom), dC6, grad & |
| 175 |
) |
| 176 |
Rij = geom%coords(:, i_atom) - geom%coords(:, j_atom) |
| 177 |
k_Rij = dot_product(k, Rij) |
| 178 |
exp_kR = cos(k_Rij) |
| 179 |
phi = disp_rec(k_norm, geom%gamm, dphi, grad) |
| 180 |
ene_ij = -C6_ij * phi / volume * exp_kR |
| 181 |
if (i_atom == j_atom) ene_ij = ene_ij / 2 |
| 182 |
res%energy = res%energy + ene_ij |
| 183 |
if (grad%dcoords .and. i_atom /= j_atom) then |
| 184 |
dene_ij%dr = ene_ij / exp_kR * sin(k_Rij) * k |
| 185 |
res%dE%dcoords(i_atom, :) = res%dE%dcoords(i_atom, :) - dene_ij%dr |
| 186 |
res%dE%dcoords(j_atom, :) = res%dE%dcoords(j_atom, :) + dene_ij%dr |
| 187 |
end if |
| 188 |
if (grad%dlattice) then |
| 189 |
#ifndef WITHOUT_DO_CONCURRENT |
| 190 |
do concurrent(i_latt=1:3, i_xyz=1:3) |
| 191 |
#else |
| 192 |
do i_latt = 1, 3 |
| 193 |
do i_xyz = 1, 3 |
| 194 |
#endif |
| 195 |
dkdA = -latt_inv(i_latt, :) * k(i_xyz) |
| 196 |
if (k_norm > 0d0) then |
| 197 |
dkdAk_proj = dot_product(dkdA, k) / k_norm |
| 198 |
else |
| 199 |
dkdAk_proj = 0d0 |
| 200 |
end if |
| 201 |
res%dE%dlattice(i_latt, i_xyz) = res%dE%dlattice(i_latt, i_xyz) & |
| 202 |
- ene_ij * latt_inv(i_latt, i_xyz) & |
| 203 |
- ene_ij / exp_kR * sin(k_Rij) * dot_product(dkdA, Rij) & |
| 204 |
+ ene_ij / phi * dphi%dk_1 * dkdAk_proj |
| 205 |
#ifdef WITHOUT_DO_CONCURRENT |
| 206 |
end do |
| 207 |
#endif |
| 208 |
end do |
| 209 |
end if |
| 210 |
if (grad%dC6) then |
| 211 |
res%dE%dC6(i_atom) = res%dE%dC6(i_atom) + ene_ij / C6_ij * dC6%dC6i |
| 212 |
res%dE%dC6(j_atom) = res%dE%dC6(j_atom) + ene_ij / C6_ij * dC6%dC6j |
| 213 |
end if |
| 214 |
if (grad%dalpha) then |
| 215 |
res%dE%dalpha(i_atom) = res%dE%dalpha(i_atom) + ene_ij / C6_ij * dC6%da0i |
| 216 |
res%dE%dalpha(j_atom) = res%dE%dalpha(j_atom) + ene_ij / C6_ij * dC6%da0j |
| 217 |
end if |
| 218 |
end do each_atom_pair |
| 219 |
end do each_atom |
| 220 |
end do each_recip_vec |
| 221 |
do i_atom = 1, geom%siz() |
| 222 |
#ifdef WITH_MPI |
| 223 |
if (modulo(i_atom, geom%mpi_size) /= geom%mpi_rank) cycle |
| 224 |
#endif |
| 225 |
res%energy = res%energy + geom%gamm**6 / 12 * C6(i_atom) ! self energy |
| 226 |
if (grad%dC6) then |
| 227 |
res%dE%dC6(i_atom) = res%dE%dC6(i_atom) + geom%gamm**6 / 12 |
| 228 |
end if |
| 229 |
end do |
| 230 |
end subroutine |
| 231 |
|
| 232 |
real(dp) function combine_C6( & |
| 233 |
C6_i, C6_j, alpha_0_i, alpha_0_j, dC6, grad) result(C6_ij) |
| 234 |
real(dp), intent(in) :: C6_i, C6_j, alpha_0_i, alpha_0_j |
| 235 |
type(grad_scalar_t), intent(out) :: dC6 |
| 236 |
type(grad_request_t), intent(in) :: grad |
| 237 |
|
| 238 |
C6_ij = 2 * C6_i * C6_j & |
| 239 |
/ (alpha_0_j / alpha_0_i * C6_i + alpha_0_i / alpha_0_j * C6_j) |
| 240 |
if (grad%dC6) then |
| 241 |
dC6%dC6i = C6_ij**2 * alpha_0_i / (2 * C6_i**2 * alpha_0_j) |
| 242 |
dC6%dC6j = C6_ij**2 * alpha_0_j / (2 * C6_j**2 * alpha_0_i) |
| 243 |
end if |
| 244 |
if (grad%dalpha) then |
| 245 |
dC6%da0i = C6_ij * (1 / alpha_0_i - C6_ij / (C6_i * alpha_0_j)) |
| 246 |
dC6%da0j = C6_ij * (1 / alpha_0_j - C6_ij / (C6_j * alpha_0_i)) |
| 247 |
end if |
| 248 |
end function |
| 249 |
|
| 250 |
real(dp) function disp_real(r, gamm, dphi, grad) result(phi) |
| 251 |
real(dp), intent(in) :: r, gamm |
| 252 |
type(grad_scalar_t), intent(out) :: dphi |
| 253 |
type(grad_request_t), intent(in) :: grad |
| 254 |
|
| 255 |
real(dp) :: gamm_r |
| 256 |
|
| 257 |
gamm_r = gamm * r |
| 258 |
phi = (2 + 2 * gamm_r**2 + gamm_r**4) * gamm**6 & |
| 259 |
/ (2 * exp(gamm_r**2) * gamm_r**6) |
| 260 |
if (grad%dcoords) then |
| 261 |
dphi%dr_1 = -gamm**7 * (6 + 6 * gamm_r**2 + 3 * gamm_r**4 + gamm_r**6) & |
| 262 |
/ (exp(gamm_r**2) * gamm_r**7) |
| 263 |
end if |
| 264 |
end function |
| 265 |
|
| 266 |
real(dp) function disp_rec(k, gamm, dphi, grad) result(phi) |
| 267 |
real(dp), intent(in) :: k, gamm |
| 268 |
type(grad_scalar_t), intent(out) :: dphi |
| 269 |
type(grad_request_t), intent(in) :: grad |
| 270 |
|
| 271 |
real(dp) :: k_gamm |
| 272 |
|
| 273 |
k_gamm = k / gamm |
| 274 |
phi = pi**1.5d0 * gamm**3 / 12 * ( & |
| 275 |
(-2 * (-2 + k_gamm**2)) / exp(k_gamm**2 / 4) & |
| 276 |
+ k_gamm**3 * sqrt(pi) * erfc(k_gamm / 2) & |
| 277 |
) |
| 278 |
if (grad%dlattice) then |
| 279 |
dphi%dk_1 = k_gamm * pi**1.5d0 * gamm**2 / 4 * ( & |
| 280 |
-2 / exp(k_gamm**2 / 4) + k_gamm * sqrt(pi) * erfc(k_gamm / 2) & |
| 281 |
) |
| 282 |
end if |
| 283 |
end function |
| 284 |
|
| 285 |
end module |