Debian Codegraph @

src/mbd_scs.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
5 module mbd_scs
6 !! Performing self-consistent screening.
7
8 use mbd_constants
9 use mbd_damping, only: damping_t
10 use mbd_dipole, only: dipole_matrix
11 use mbd_formulas, only: sigma_selfint
12 use mbd_geom, only: geom_t
13 use mbd_gradients, only: grad_t, grad_matrix_re_t, grad_request_t
14 use mbd_matrix, only: matrix_re_t, contract_cross_33
15 use mbd_utils, only: findval
16
17 implicit none
18
19 private
20 public :: run_scs
21
22 contains
23
24 function run_scs(geom, alpha, damp, dalpha_scs, grad) result(alpha_scs)
25 !! $$
26 !! \begin{gathered}
27 !! \bar\alpha_i=\tfrac13\operatorname{Tr}
28 !! \big(\textstyle\sum_j\boldsymbol{\bar\alpha}_{ij}\big),\qquad
29 !! \boldsymbol{\bar\alpha}=(\boldsymbol\alpha^{-1}+\mathbf T_\text{GG})^{-1}
30 !! \\ \partial\boldsymbol{\bar\alpha}=
31 !! -\boldsymbol{\bar\alpha}(
32 !! \partial\boldsymbol\alpha^{-1}+\partial\mathbf T_\text{GG}
33 !! )\boldsymbol{\bar\alpha},\qquad
34 !! \frac{\partial\bar\alpha_i}{\partial X_j}=
35 !! -\frac13\sum_{\zeta\eta}\big(
36 !! B_{i\zeta,j\eta}\bar\alpha'_{\zeta,j\eta}+
37 !! B'_{j\eta,\zeta}\bar\alpha_{j\eta,i\zeta}
38 !! \big)
39 !! \\ \mathbf B=\boldsymbol{\bar\alpha}\mathbf A,
40 !! \quad A_{i\zeta,j\eta}=
41 !! \frac{\partial(\alpha_i^{-1})}{\partial X_i}
42 !! \delta_{ij}\delta_{\zeta\eta}+
43 !! \frac{\partial T^\text{GG}_{i\zeta,j\eta}}{\partial X_i},\quad
44 !! \bar\alpha'_{\zeta,p}=\sum_i\bar\alpha_{i\zeta,p},\quad
45 !! B'_{p,\zeta}=\sum_iB_{p,i\zeta}
46 !! \end{gathered}
47 !! $$
48 type(geom_t), intent(inout) :: geom
49 real(dp), intent(in) :: alpha(:)
50 type(damping_t), intent(in) :: damp
51 type(grad_t), intent(out) :: dalpha_scs(:)
52 type(grad_request_t), intent(in) :: grad
53 real(dp) :: alpha_scs(size(alpha))
54
55 type(matrix_re_t) :: alpha_full, dQ, T
56 integer :: n_atoms, i_xyz, i_atom, my_i_atom, i_latt
57 type(damping_t) :: damp_local
58 real(dp), allocatable :: dsij_dsi(:), dsigma_dalpha(:), &
59 alpha_prime(:, :), B_prime(:, :), grads_i(:), dalphadA(:)
60 real(dp) :: sigma_tmp(size(alpha)) ! circumventing PGI 19 compiler bug
61 type(grad_matrix_re_t) :: dT
62 type(grad_request_t) :: grad_req
63
64 n_atoms = geom%siz()
65 damp_local = damp
66 sigma_tmp = sigma_selfint(alpha, dsigma_dalpha, grad%dalpha)
67 damp_local%sigma = sigma_tmp
68 grad_req = grad_request_t( &
69 dcoords=grad%dcoords, &
70 dlattice=grad%dlattice, &
71 dsigma=grad%dalpha, &
72 dr_vdw=grad%dr_vdw &
73 )
74 call geom%clock(30)
75 T = dipole_matrix(geom, damp_local, dT, grad_req)
76 call geom%clock(-30)
77 if (geom%has_exc()) return
78 if (grad%any()) then
79 call alpha_full%copy_from(T)
80 else
81 call alpha_full%move_from(T)
82 end if
83 call alpha_full%add_diag(1d0 / alpha)
84 call geom%clock(32)
85 call alpha_full%invh(geom%exc, clock=geom%timer)
86 if (geom%has_exc()) return
87 call geom%clock(-32)
88 alpha_scs = alpha_full%contract_n33diag_cols()
89 if (any(alpha_scs < 0)) then
90 geom%exc%code = MBD_EXC_NEG_POL
91 geom%exc%msg = 'Screening leads to negative polarizability'
92 return
93 end if
94 if (.not. grad%any()) return
95 call geom%clock(33)
96 allocate (alpha_prime(3, 3 * n_atoms), source=0d0)
97 allocate (B_prime(3 * n_atoms, 3), source=0d0)
98 allocate (grads_i(n_atoms))
99 call alpha_full%contract_n_transp('R', alpha_prime)
100 call dQ%init_from(T)
101 if (grad%dcoords) then
102 do my_i_atom = 1, size(geom%idx%i_atom)
103 allocate (dalpha_scs(my_i_atom)%dcoords(size(geom%idx%j_atom), 3))
104 end do
105 do i_xyz = 1, 3
106 dQ%val = -dT%dr(:, :, i_xyz)
107 call geom%clock(14)
108 dQ = alpha_full%mmul(dQ)
109 call geom%clock(-14)
110 call geom%clock(15)
111 call dQ%contract_n_transp('C', B_prime)
112 do i_atom = 1, n_atoms
113 grads_i = contract_cross_33( &
114 i_atom, dQ, alpha_prime, alpha_full, B_prime &
115 )
116 my_i_atom = findval(geom%idx%i_atom, i_atom)
117 if (my_i_atom > 0) then
118 dalpha_scs(my_i_atom)%dcoords(:, i_xyz) = &
119 grads_i(geom%idx%j_atom)
120 end if
121 end do
122 call geom%clock(-15)
123 end do
124 end if
125 if (grad%dlattice) then
126 do my_i_atom = 1, size(geom%idx%i_atom)
127 allocate (dalpha_scs(my_i_atom)%dlattice(3, 3))
128 end do
129 do i_latt = 1, 3
130 do i_xyz = 1, 3
131 dQ%val = -dT%dlattice(:, :, i_latt, i_xyz)
132 call geom%clock(14)
133 dQ = alpha_full%mmul(dQ)
134 dQ = dQ%mmul(alpha_full)
135 call geom%clock(-14)
136 call geom%clock(15)
137 dalphadA = dQ%contract_n33diag_cols()
138 do concurrent(my_i_atom=1:size(geom%idx%i_atom))
139 dalpha_scs(my_i_atom)%dlattice(i_latt, i_xyz) &
140 = dalphadA(geom%idx%i_atom(my_i_atom))
141 end do
142 call geom%clock(-15)
143 end do
144 end do
145 end if
146 if (grad%dalpha) then
147 dQ%val = dT%dsigma
148 do i_atom = 1, n_atoms
149 dsij_dsi = damp_local%sigma(i_atom) * dsigma_dalpha(i_atom) / &
150 sqrt(damp_local%sigma(i_atom)**2 + damp_local%sigma**2)
151 call dQ%mult_col(i_atom, dsij_dsi)
152 end do
153 call dQ%add_diag(-0.5d0 / alpha**2)
154 call geom%clock(14)
155 dQ = alpha_full%mmul(dQ)
156 call geom%clock(-14)
157 call geom%clock(15)
158 call dQ%contract_n_transp('C', B_prime)
159 do i_atom = 1, n_atoms
160 grads_i = contract_cross_33( &
161 i_atom, dQ, alpha_prime, alpha_full, B_prime &
162 )
163 my_i_atom = findval(geom%idx%i_atom, i_atom)
164 if (my_i_atom > 0) then
165 dalpha_scs(my_i_atom)%dalpha = grads_i(geom%idx%j_atom)
166 end if
167 end do
168 call geom%clock(-15)
169 end if
170 if (grad%dr_vdw) then
171 dQ%val = dT%dvdw
172 call geom%clock(14)
173 dQ = alpha_full%mmul(dQ)
174 call geom%clock(-14)
175 call geom%clock(15)
176 call dQ%contract_n_transp('C', B_prime)
177 do i_atom = 1, n_atoms
178 grads_i = contract_cross_33( &
179 i_atom, dQ, alpha_prime, alpha_full, B_prime &
180 )
181 my_i_atom = findval(geom%idx%i_atom, i_atom)
182 if (my_i_atom > 0) then
183 dalpha_scs(my_i_atom)%dr_vdw = grads_i(geom%idx%j_atom)
184 end if
185 end do
186 call geom%clock(-15)
187 end if
188 call geom%clock(-33)
189 end function
190
191 end module