Debian Codegraph @

src/mbd_coulomb.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 module mbd_coulomb
5
6 use mbd_constants
7 use mbd_linalg, only: eye, outer, diag
8 use mbd_lapack, only: inverse, det, inv
9 use mbd_geom, only: geom_t
10 use mbd_dipole, only: dipole_matrix
11 use mbd_damping, only: damping_t, damping_fermi
12 use mbd_matrix, only: matrix_re_t
13
14 implicit none
15
16 private
17
18 public :: coulomb_energy, dipole_energy
19
20 contains
21
22 subroutine calc_coulomb_coupled_gauss(R1, R2, K, dip, coul)
23 real(dp), intent(in) :: R1(3), R2(3), K(:, :)
24 real(dp), intent(out), optional :: dip(3, 3), coul
25
26 real(dp), allocatable :: u(:), w(:), x(:)
27 real(dp) :: R(6), det_K
28 integer :: i
29 real(dp) :: det_K_plus_U2, coul_u, dot, dist, work(6, 6)
30 real(dp), dimension(:, :), allocatable :: K11, K12, K22, dip_u
31
32 integer, parameter :: n_pts_coulomb = 500
33 real(dp), parameter :: L_coulomb = 10d0
34 character(len=20), parameter :: quadrature = 'simpson'
35
36 dist = sqrt(sum((R1 - R2)**2))
37 allocate (x(n_pts_coulomb), w(n_pts_coulomb))
38 select case (quadrature)
39 case ('original')
40 w = 1d0 / n_pts_coulomb
41 do concurrent(i=1:n_pts_coulomb)
42 x(i) = w(1) / 2 + (i - 1) * w(1)
43 end do
44 u = log(1d0 / (1d0 - x)) * L_coulomb / dist
45 w = 1d0 / (1d0 - x) * w * L_coulomb / dist
46 case ('simpson')
47 call simpson1by3(n_pts_coulomb, x, w)
48 u = (1d0 - x) / (1d0 + x)
49 w = 2 * w / (1d0 + x)**2
50 end select
51 R(1:3) = R1
52 R(4:6) = R2
53 if (present(coul)) coul = 0d0
54 if (present(dip)) then
55 dip(:, :) = 0d0
56 allocate (K11(3, 3), K12(3, 3), K22(3, 3), dip_u(3, 3))
57 end if
58 do i = 1, n_pts_coulomb
59 select case (size(K, 1))
60 case (3)
61 work(1:3, 1:3) = K
62 work(4:6, 1:3) = eye(3) * u(i)**2
63 work = -matmul( &
64 work(:, 1:3), &
65 matmul(inverse(K + eye(3) * u(i)**2), transpose(work(:, 1:3))) &
66 )
67 work(1:3, 1:3) = work(1:3, 1:3) + K
68 work(4:6, 4:6) = work(4:6, 4:6) + eye(3) * u(i)**2
69 det_K_plus_U2 = det(K + eye(3) * u(i)**2)
70 case (6)
71 work = K
72 call add_U2(work, u(i)**2) ! work is K+U2
73 det_K_plus_U2 = det(work)
74 call inv(work) ! work is (K+U2)^-1
75 work = K - matmul(K, matmul(work, K)) ! work is K-K*(K+U2)^-1*K
76 end select
77 dot = dot_product(R, matmul(work, R))
78 coul_u = 1d0 / sqrt(det_K_plus_U2) * exp(-dot) * w(i)
79 if (present(coul)) coul = coul + coul_u
80 if (present(dip)) then
81 K11 = work(1:3, 1:3)
82 K12 = work(1:3, 4:6)
83 K22 = work(4:6, 4:6)
84 dip_u = (-2 * K12 + 4 * outer( &
85 matmul(K11, R1) + matmul(K12, R2), &
86 matmul(K12, R1) + matmul(K22, R2) &
87 )) * coul_u
88 dip = dip + dip_u
89 end if
90 end do
91 det_K = det(K)
92 if (present(coul)) coul = 2.d0 / sqrt(pi) * coul * sqrt(det_K)
93 if (present(dip)) dip = 2.d0 / sqrt(pi) * dip * sqrt(det_K)
94
95 contains
96
97 subroutine add_U2(A, u_sq)
98 real(dp), intent(inout) :: A(6, 6)
99 real(dp), intent(in) :: u_sq
100
101 integer :: i
102
103 do concurrent(i=1:3)
104 A(i, i) = A(i, i) + u_sq
105 A(i, i + 3) = A(i, i + 3) - u_sq
106 A(i + 3, i) = A(i + 3, i) - u_sq
107 A(i + 3, i + 3) = A(i + 3, i + 3) + u_sq
108 end do
109 end subroutine
110
111 end subroutine
112
113 real(dp) function coulomb_energy(geom, q, m, w_t, C, damp)
114 type(geom_t), intent(inout) :: geom
115 real(dp), intent(in) :: q(:), m(:), w_t(:), C(:, :)
116 type(damping_t), intent(in) :: damp
117
118 real(dp), allocatable :: O(:, :)
119 real(dp) :: OAB(6, 6), OABm(6, 6), RA(3), RB(3), ene_ABi(4), prod_w_t, &
120 K(3, 3), s_vdw, f_damp
121 integer, allocatable :: notAB(:)
122 integer :: N, A, B, i, j, AB(6), i2A(6)
123
124 allocate (notAB(size(C, 1) - 6))
125 O = matmul(matmul(C, diag(w_t)), transpose(C))
126 N = geom%siz()
127 prod_w_t = product(w_t)
128 coulomb_energy = 0.d0
129 do A = 1, N
130 do B = A + 1, N
131 RA = geom%coords(:, A)
132 RB = geom%coords(:, B)
133 AB(:) = [(3 * (A - 1) + i, i=1, 3), (3 * (B - 1) + i, i=1, 3)]
134 notAB(:) = [ &
135 (i, i=1, 3 * (A - 1)), &
136 (i, i=3 * A + 1, 3 * (B - 1)), &
137 (i, i=3 * B + 1, 3 * N) &
138 ]
139 OAB = O(AB, AB) - matmul( &
140 O(AB, notAB), &
141 matmul(inverse(O(notAB, notAB)), O(notAB, AB)) &
142 )
143 i2A = [(A, i=1, 3), (B, i=1, 3)]
144 do concurrent(i=1:6, j=1:6)
145 OABm(i, j) = OAB(i, j) * sqrt(m(i2A(i)) * m(i2A(j)))
146 end do
147 call calc_coulomb_coupled_gauss(RA, RB, OABm, coul=ene_ABi(1))
148 K = m(B) * (OAB(4:6, 4:6) - matmul( &
149 OAB(4:6, 1:3), matmul(inverse(OAB(1:3, 1:3)), OAB(1:3, 4:6)) &
150 ))
151 call calc_coulomb_coupled_gauss(RA, RB, K, coul=ene_ABi(2))
152 K = m(A) * (OAB(1:3, 1:3) - matmul( &
153 OAB(1:3, 4:6), matmul(inverse(OAB(4:6, 4:6)), OAB(4:6, 1:3)) &
154 ))
155 call calc_coulomb_coupled_gauss(RA, RB, K, coul=ene_ABi(3))
156 ene_ABi(2:3) = -ene_ABi(2:3)
157 ene_ABi(4) = 1d0 / sqrt(sum((RA - RB)**2))
158 select case (damp%version)
159 case ('fermi')
160 s_vdw = damp%ts_sr * sum(damp%r_vdw([A, B]))
161 f_damp = damping_fermi(RA - RB, s_vdw, damp%ts_d)
162 case default
163 f_damp = 1d0
164 end select
165 coulomb_energy = coulomb_energy + f_damp * q(A) * q(B) * sum(ene_ABi)
166 end do
167 end do
168 end function
169
170 real(dp) function dipole_energy(geom, a0, w, w_t, C, damp)
171 type(geom_t), intent(inout) :: geom
172 real(dp), intent(in) :: a0(:), w(:), w_t(:), C(:, :)
173 type(damping_t), intent(in) :: damp
174
175 integer :: A, B, i, j, N
176 type(matrix_re_t) :: T
177
178 N = geom%siz()
179 T = dipole_matrix(geom, damp)
180 do A = 1, N
181 do B = 1, N
182 i = 3 * (A - 1)
183 j = 3 * (B - 1)
184 T%val(i + 1:i + 3, j + 1:j + 3) = &
185 w(A) * w(B) * sqrt(a0(A) * a0(B)) * T%val(i + 1:i + 3, j + 1:j + 3)
186 end do
187 end do
188 T%val = matmul(matmul(transpose(C), T%val), C)
189 dipole_energy = sum(diag(T%val) / (4 * w_t))
190 end function
191
192 subroutine simpson1by3(n, x, w)
193 integer, intent(in) :: n
194 real(dp), intent(out) :: x(n), w(n)
195
196 integer :: i
197 real(dp) :: h, delta
198
199 delta = 1d-6
200 h = 2 * (1d0 - delta) / (n - 1)
201 do i = 1, n
202 x(i) = -(1d0 - delta) + h * (i - 1)
203 if (2 * (i - (i / 2)) == i) then
204 w(i) = 2 * h / 3
205 else
206 w(i) = 4 * h / 3
207 end if
208 end do
209 w(1) = h / 3
210 w(n) = w(1)
211 end subroutine
212
213 end module