Debian Codegraph @

src/mbd_rpa.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 module mbd_rpa
6
7 use mbd_constants
8 use mbd_damping, only: damping_t
9 use mbd_dipole, only: dipole_matrix
10 use mbd_formulas, only: sigma_selfint, rpa_rescale_eigval
11 use mbd_geom, only: geom_t
12 use mbd_gradients, only: grad_t, grad_matrix_re_t, grad_matrix_cplx_t, &
13 grad_request_t
14 use mbd_matrix, only: matrix_re_t, matrix_cplx_t
15 use mbd_utils, only: result_t, tostr
16
17 implicit none
18
19 private
20 public :: get_mbd_rpa_energy
21
22 interface get_mbd_rpa_energy
23 !! Get the MBD energy by the RPA method.
24 !!
25 !! The energy is obtained from the eigenvalues \(\mu_k(\mathrm iu)\) of the
26 !! symmetrized dipole matrix \(\mathbf M(\mathrm iu)=\mathbf A(\mathrm
27 !! iu)^\frac12\mathbf T\mathbf A(\mathrm iu)^\frac12\), with \(A_{ij}(\mathrm
28 !! iu)=\alpha_i(\mathrm iu)\delta_{ij}\),
29 !!
30 !! $$
31 !! E=\frac1{2\pi}\int_0^\infty\mathrm du\sum_k\log(1+\mu_k(\mathrm iu))
32 !! =\frac1{2\pi}\int_0^\infty\mathrm du\,\log\det(\mathbf 1+\mathbf M(\mathrm iu))
33 !! $$
34 !!
35 !! The derivatives use \(\partial\mu_k=\mathbf c_k^\dagger(\partial\mathbf
36 !! M)\mathbf c_k\), which upon summation over the eigenvalues gives the
37 !! weighted resolvent \(\mathbf B(\mathrm iu)=\mathbf C\operatorname{diag}(
38 !! g'(\mu_k))\mathbf C^\dagger\), where \(g(\mu)=\log(1+\mu)\) is the
39 !! per-eigenvalue energy contribution,
40 !!
41 !! $$
42 !! \partial E=\frac1{2\pi}\int_0^\infty\mathrm du
43 !! \operatorname{Tr}\big(\mathbf B(\mathrm iu)\partial\mathbf M(\mathrm iu)\big)
44 !! $$
45 !!
46 !! Both the explicit derivatives (coordinates, lattice vectors, van der
47 !! Waals radii, \(\mathbf q\)) and the implicit derivatives with respect to
48 !! the dynamic polarizabilities are returned. The latter propagate both
49 !! through the \(\sqrt{\alpha_i\alpha_j}\) prefactor and, when the damping
50 !! depends on it, through \(\sigma_{ij}(\alpha)\). With eigenvalue rescaling
51 !! ([[mbd_calc_params_t:rpa_rescale_eigs]]) the contribution becomes
52 !! \(g(\mu)=\log(1+\lambda(\mu))-\lambda(\mu)\), with
53 !! \(\lambda=-\operatorname{erf}(\tfrac{\sqrt\pi}2\mu^4)^\frac14\) for
54 !! \(\mu<0\), and the same weighted resolvent is used.
55 !!
56 !! The real-typed version is equivalent to \(\mathbf q=0\).
57 module procedure get_mbd_rpa_energy_real
58 module procedure get_mbd_rpa_energy_complex
59 end interface
60
61 contains
62
63 #endif
64
65 #ifndef DO_COMPLEX_TYPE
66 type(result_t) function get_mbd_rpa_energy_real( &
67 geom, alpha, damp, grad) result(res)
68 #else
69 type(result_t) function get_mbd_rpa_energy_complex( &
70 geom, alpha, damp, q, grad) result(res)
71 #endif
72 type(geom_t), intent(inout) :: geom
73 real(dp), intent(in) :: alpha(:, 0:)
74 type(damping_t), intent(in) :: damp
75 #ifdef DO_COMPLEX_TYPE
76 real(dp), intent(in) :: q(3)
77 #endif
78 type(grad_request_t), intent(in), optional :: grad
79
80 #ifndef DO_COMPLEX_TYPE
81 type(matrix_re_t) :: relay, AT, Mmat, modes, B, dQ
82 type(grad_matrix_re_t) :: dT
83 #else
84 type(matrix_cplx_t) :: relay, AT, Mmat, modes, B, dQ
85 type(grad_matrix_cplx_t) :: dT
86 #endif
87 real(dp), allocatable :: eigs(:), log_eigs(:), sqrt_alpha(:), &
88 g_prime(:), contr(:), dxr(:)
89 integer :: i_freq, my_i_atom, n_order, n_negative_eigs, my_j_atom, &
90 n_atoms, i_xyz, i_latt
91 real(dp) :: freq_w, sigma_ij
92 type(damping_t) :: damp_alpha
93 type(grad_request_t) :: grad_dip
94 logical :: do_grad
95
96 do_grad = .false.
97 if (present(grad)) do_grad = grad%any()
98 n_atoms = geom%siz()
99 if (do_grad) then
100 allocate (g_prime(3 * n_atoms))
101 if (geom%param%rpa_rescale_eigs) allocate (dxr(3 * n_atoms))
102 grad_dip%dcoords = grad%dcoords
103 grad_dip%dlattice = grad%dlattice
104 grad_dip%dr_vdw = grad%dr_vdw
105 ! the dipole matrix depends on the dynamic polarizability through the
106 ! self-consistent-screening width sigma only for the gg dampings
107 grad_dip%dsigma = grad%dalpha_dyn .and. index(damp%version, 'gg') > 0
108 #ifdef DO_COMPLEX_TYPE
109 grad_dip%dq = grad%dq
110 #endif
111 if (grad%dcoords) allocate (res%dE%dcoords(n_atoms, 3), source=0d0)
112 if (grad%dr_vdw) allocate (res%dE%dr_vdw(n_atoms), source=0d0)
113 if (grad%dalpha_dyn) &
114 allocate (res%dE%dalpha_dyn(n_atoms, 0:ubound(alpha, 2)), source=0d0)
115 #ifndef DO_COMPLEX_TYPE
116 if (grad%dlattice) allocate (res%dE%dlattice(3, 3), source=0d0)
117 #else
118 if (grad%dlattice) allocate (res%dE%dlattice(3, 3), source=0d0)
119 if (grad%dq) allocate (res%dE%dq(3), source=0d0)
120 #endif
121 end if
122
123 res%energy = 0d0
124 damp_alpha = damp
125 ! implicit allocation doesn't work here in gfortran 4.9
126 allocate (eigs(3 * geom%siz()), log_eigs(3 * geom%siz()))
127 if (geom%get_rpa_orders) allocate (res%rpa_orders(geom%param%rpa_order_max), source=0d0)
128 do i_freq = 0, ubound(geom%freq, 1)
129 damp_alpha%sigma = sigma_selfint(alpha(:, i_freq))
130 sqrt_alpha = sqrt(alpha(:, i_freq))
131 if (do_grad) then
132 #ifndef DO_COMPLEX_TYPE
133 relay = dipole_matrix(geom, damp_alpha, dT, grad_dip)
134 #else
135 relay = dipole_matrix(geom, damp_alpha, dT, grad_dip, q=q)
136 #endif
137 else
138 #ifndef DO_COMPLEX_TYPE
139 relay = dipole_matrix(geom, damp_alpha)
140 #else
141 relay = dipole_matrix(geom, damp_alpha, q=q)
142 #endif
143 end if
144 if (geom%has_exc()) return
145 if (do_grad) then
146 ! keep relay = T, build M = A^1/2 T A^1/2 separately
147 call Mmat%copy_from(relay)
148 call Mmat%mult_cross(sqrt_alpha)
149 call modes%alloc_from(Mmat)
150 call geom%clock(23)
151 call modes%eigh(eigs, geom%exc, src=Mmat, clock=geom%timer)
152 call geom%clock(-23)
153 else
154 call relay%mult_cross(sqrt_alpha)
155 call AT%move_from(relay)
156 call geom%clock(23)
157 eigs = AT%eigvalsh(geom%exc, destroy=.true.)
158 call geom%clock(-23)
159 end if
160 if (geom%has_exc()) return
161 if (geom%param%rpa_rescale_eigs) then
162 eigs(:) = rpa_rescale_eigval(eigs, dxr)
163 end if
164 n_negative_eigs = count(eigs(:) <= -1)
165 if (n_negative_eigs > 0) then
166 geom%exc%code = MBD_EXC_NEG_EIGVALS
167 geom%exc%msg = "1+AT matrix has "// &
168 trim(tostr(n_negative_eigs))//" negative eigenvalues"
169 return
170 end if
171 log_eigs = log(1 + eigs)
172 if (geom%param%rpa_rescale_eigs) then
173 log_eigs = log_eigs - eigs
174 end if
175 res%energy = res%energy + &
176 1d0 / (2 * pi) * sum(log_eigs) * geom%freq(i_freq)%weight
177 if (geom%get_rpa_orders) then
178 do n_order = 2, geom%param%rpa_order_max
179 res%rpa_orders(n_order) = res%rpa_orders(n_order) &
180 + (-1d0 / (2 * pi) * (-1)**n_order &
181 * sum(eigs**n_order) / n_order) &
182 * geom%freq(i_freq)%weight
183 end do
184 end if
185 if (.not. do_grad) cycle
186 freq_w = geom%freq(i_freq)%weight
187 ! The per-eigenvalue energy contribution is g(mu_k), whose derivative
188 ! with respect to a parameter is g'(mu_k) times the derivative of the
189 ! raw eigenvalue mu_k of M. Summed, this gives the weighted resolvent
190 ! B = C diag(g'(mu_k)) C^dagger, contracted below with dM.
191 if (.not. geom%param%rpa_rescale_eigs) then
192 ! g(mu) = log(1 + mu)
193 g_prime = 1d0 / (1d0 + eigs)
194 else
195 ! g(mu) = log(1 + lambda) - lambda with lambda = eigs (rescaled) and
196 ! dlambda/dmu = dxr; chain rule g'(mu) = (1/(1 + lambda) - 1) dlambda/dmu
197 g_prime = (1d0 / (1d0 + eigs) - 1d0) * dxr
198 end if
199 call B%copy_from(modes)
200 call B%mult_cols_3n(g_prime)
201 B = B%mmul(modes, transB='C')
202 #ifdef DO_COMPLEX_TYPE
203 B%val = conjg(B%val)
204 #endif
205 call dQ%init_from(relay)
206 if (grad%dcoords) then
207 do i_xyz = 1, 3
208 dQ%val = dT%dr(:, :, i_xyz)
209 call dQ%mult_cross(sqrt_alpha)
210 dQ%val = B%val * dQ%val
211 contr = freq_w / pi * dble(dQ%contract_n33_rows())
212 res%dE%dcoords(:, i_xyz) = res%dE%dcoords(:, i_xyz) + contr
213 end do
214 end if
215 if (grad%dlattice) then
216 do i_latt = 1, 3
217 do i_xyz = 1, 3
218 dQ%val = dT%dlattice(:, :, i_latt, i_xyz)
219 call dQ%mult_cross(sqrt_alpha)
220 dQ%val = B%val * dQ%val
221 res%dE%dlattice(i_latt, i_xyz) = res%dE%dlattice(i_latt, i_xyz) + &
222 freq_w / (2 * pi) * dble(dQ%sum_all())
223 end do
224 end do
225 end if
226 if (grad%dr_vdw) then
227 dQ%val = dT%dvdw
228 call dQ%mult_cross(sqrt_alpha)
229 dQ%val = B%val * dQ%val
230 contr = freq_w / pi * dble(dQ%contract_n33_rows())
231 res%dE%dr_vdw = res%dE%dr_vdw + contr
232 end if
233 #ifdef DO_COMPLEX_TYPE
234 if (grad%dq) then
235 do i_latt = 1, 3
236 dQ%val = dT%dq(:, :, i_latt)
237 call dQ%mult_cross(sqrt_alpha)
238 dQ%val = B%val * dQ%val
239 res%dE%dq(i_latt) = res%dE%dq(i_latt) + &
240 freq_w / (2 * pi) * dble(dQ%sum_all())
241 end do
242 end if
243 #endif
244 if (grad%dalpha_dyn) then
245 ! channel through the sqrt(alpha_i alpha_j) prefactor
246 dQ%val = relay%val
247 call dQ%mult_cross(sqrt_alpha)
248 call dQ%mult_rows(1d0 / (2 * alpha(:, i_freq)))
249 dQ%val = B%val * dQ%val
250 contr = freq_w / pi * dble(dQ%contract_n33_rows())
251 res%dE%dalpha_dyn(:, i_freq) = res%dE%dalpha_dyn(:, i_freq) + contr
252 ! channel through sigma_ij(alpha), if the damping uses it
253 if (grad_dip%dsigma) then
254 dQ%val = dT%dsigma
255 call dQ%mult_cross(sqrt_alpha)
256 dQ%val = B%val * dQ%val
257 ! scale each block (i, j) by 1 / sigma_ij
258 do my_i_atom = 1, size(dQ%idx%i_atom)
259 do my_j_atom = 1, size(dQ%idx%j_atom)
260 associate ( &
261 i_atom => dQ%idx%i_atom(my_i_atom), &
262 j_atom => dQ%idx%j_atom(my_j_atom), &
263 dQ_sub => dQ%val( &
264 3 * (my_i_atom - 1) + 1:, &
265 3 * (my_j_atom - 1) + 1: &
266 ) &
267 )
268 sigma_ij = damp%mayer_scaling * sqrt(sum( &
269 damp_alpha%sigma([i_atom, j_atom])**2))
270 dQ_sub(:3, :3) = dQ_sub(:3, :3) / sigma_ij
271 end associate
272 end do
273 end do
274 contr = freq_w / pi * damp%mayer_scaling**2 &
275 * damp_alpha%sigma**2 / (3 * alpha(:, i_freq)) &
276 * dble(dQ%contract_n33_rows())
277 res%dE%dalpha_dyn(:, i_freq) = res%dE%dalpha_dyn(:, i_freq) + contr
278 end if
279 end if
280 end do
281 end function
282
283 #ifndef DO_COMPLEX_TYPE
284 # define DO_COMPLEX_TYPE
285 # include "mbd_rpa.F90"
286
287 end module
288
289 #endif