Debian Codegraph @

src/mbd_formulas.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_formulas
6 !! Common formulas used at multiple places.
7
8 use mbd_constants
9 use mbd_gradients, only: grad_t, grad_request_t
10 use mbd_utils, only: quad_pt_t, tostr
11
12 implicit none
13
14 private
15 public :: omega_qho, alpha_dyn_qho, C6_from_alpha, sigma_selfint, scale_with_ratio
16 public :: rpa_rescale_eigval
17
18 contains
19
20 function omega_qho(C6, alpha, domega, grad) result(omega)
21 !! $$
22 !! \omega=\frac{4C_6}{3\alpha_{0}^2},\qquad
23 !! \partial\omega=\omega\left(
24 !! \frac{\partial C_6}{C_6}-\frac{2\partial\alpha_0}{\alpha_0}
25 !! \right)
26 !! $$
27 real(dp), intent(in) :: C6(:)
28 real(dp), intent(in) :: alpha(:)
29 type(grad_t), intent(out), optional :: domega
30 type(grad_request_t), intent(in), optional :: grad
31 real(dp) :: omega(size(C6))
32
33 omega = 4d0 / 3 * C6 / alpha**2
34 if (.not. present(grad)) return
35 if (grad%dC6) domega%dC6 = omega / C6
36 if (grad%dalpha) domega%dalpha = -2 * omega / alpha
37 end function
38
39 function alpha_dyn_qho(alpha_0, omega, freq, dalpha, grad) result(alpha)
40 !! $$
41 !! \alpha(\mathrm iu)=\frac{\alpha_0}{1+u^2/\omega^2},\qquad
42 !! \partial\alpha(\mathrm iu)=\alpha(\mathrm iu)\left(
43 !! \frac{\partial\alpha_0}{\alpha_0}+
44 !! \frac2\omega\frac{\partial\omega}{1+\omega^2/u^2}
45 !! \right)
46 !! $$
47 real(dp), intent(in) :: alpha_0(:)
48 real(dp), intent(in) :: omega(:)
49 type(quad_pt_t), intent(in) :: freq(0:)
50 type(grad_t), allocatable, intent(out) :: dalpha(:)
51 type(grad_request_t), intent(in) :: grad
52 real(dp) :: alpha(size(alpha_0), 0:ubound(freq, 1))
53
54 integer :: i_freq, n_atoms
55
56 n_atoms = size(alpha_0)
57 allocate (dalpha(0:ubound(alpha, 2)))
58 do i_freq = 0, ubound(alpha, 2)
59 associate (alpha => alpha(:, i_freq), u => freq(i_freq)%val)
60 alpha = alpha_0 / (1 + (u / omega)**2)
61 if (grad%dalpha) dalpha(i_freq)%dalpha = alpha / alpha_0
62 if (grad%domega) then
63 if (u <= 0d0) then
64 allocate (dalpha(i_freq)%domega(size(omega)), source=0d0)
65 else
66 dalpha(i_freq)%domega = alpha * 2d0 / omega / (1d0 + (omega / u)**2)
67 end if
68 end if
69 end associate
70 end do
71 end function
72
73 function C6_from_alpha(alpha, freq, dC6_dalpha, grad) result(C6)
74 !! $$
75 !! \bar C_6=\frac3\pi\int_0^\infty\mathrm du\,\bar\alpha(u)^2,\qquad
76 !! \partial\bar C_6=\frac6\pi\int_0^\infty\mathrm du
77 !! \bar\alpha(u)\partial\bar\alpha(u)
78 !! $$
79 real(dp), intent(in) :: alpha(:, 0:)
80 type(quad_pt_t), intent(in) :: freq(0:)
81 real(dp), allocatable, intent(out), optional :: dC6_dalpha(:, :)
82 logical, intent(in), optional :: grad
83 real(dp) :: C6(size(alpha, 1))
84
85 integer :: i_freq, n_atoms
86
87 n_atoms = size(alpha, 1)
88 C6 = 0d0
89 do i_freq = 0, ubound(alpha, 2)
90 C6 = C6 + 3d0 / pi * alpha(:, i_freq)**2 * freq(i_freq)%weight
91 end do
92 if (.not. present(grad)) return
93 if (.not. grad) return
94 allocate (dC6_dalpha(n_atoms, 0:ubound(alpha, 2)), source=0d0)
95 do i_freq = 0, ubound(alpha, 2)
96 dC6_dalpha(:, i_freq) = dC6_dalpha(:, i_freq) + 6d0 / pi * alpha(:, i_freq)
97 end do
98 end function
99
100 function sigma_selfint(alpha, dsigma_dalpha, grad) result(sigma)
101 !! $$
102 !! \begin{gathered}
103 !! \sigma_i(u)=\left(\frac13\sqrt{\frac2\pi}\alpha_i(u)\right)^{\frac13},\qquad
104 !! \partial\sigma_i=\sigma_i\frac{\partial\alpha_i}{3\alpha_i}
105 !! \\ \sigma_{ij}(u)=\sqrt{\sigma_i(u)^2+\sigma_j(u)^2},\qquad
106 !! \partial\sigma_{ij}=
107 !! \frac{\sigma_i\partial\sigma_i+\sigma_j\partial\sigma_j}{\sigma_{ij}}
108 !! \end{gathered}
109 !! $$
110 real(dp), intent(in) :: alpha(:)
111 real(dp), allocatable, intent(out), optional :: dsigma_dalpha(:)
112 logical, intent(in), optional :: grad
113 real(dp) :: sigma(size(alpha))
114
115 sigma = (sqrt(2d0 / pi) * alpha / 3d0)**(1d0 / 3)
116 if (.not. present(grad)) return
117 if (grad) dsigma_dalpha = sigma / (3 * alpha)
118 end function
119
120 function rpa_rescale_eigval(x, dxr) result(xr)
121 !! Rescale eigenvalues \(x\) of the RPA matrix \(\mathbf{AT}\) to tame the
122 !! spurious divergence of \(\log(1+x)\) as \(x\to-1\). Non-negative
123 !! eigenvalues are left intact.
124 !!
125 !! $$
126 !! x_\mathrm r=\begin{cases}x & x\ge0\\
127 !! -\operatorname{erf}\!\big(\tfrac{\sqrt\pi}2 x^4\big)^\frac14 & x<0
128 !! \end{cases},\qquad
129 !! \frac{\mathrm dx_\mathrm r}{\mathrm dx}=\begin{cases}1 & x\ge0\\
130 !! \big(\tfrac x{x_\mathrm r}\big)^3
131 !! \mathrm e^{-(\frac{\sqrt\pi}2 x^4)^2} & x<0\end{cases}
132 !! $$
133 real(dp), intent(in) :: x(:)
134 real(dp), intent(out), optional :: dxr(:)
135 real(dp) :: xr(size(x))
136
137 where (x >= 0)
138 xr = x
139 elsewhere
140 xr = -erf(sqrt(pi) / 2 * x**4)**(1d0 / 4)
141 end where
142 if (.not. present(dxr)) return
143 where (x >= 0)
144 dxr = 1d0
145 elsewhere
146 dxr = (x / xr)**3 * exp(-(sqrt(pi) / 2 * x**4)**2)
147 end where
148 end function
149
150 function scale_with_ratio(x, yp, y, q, dx, grad) result(xp)
151 !! $$
152 !! x'=x\left(\frac{y'}y\right)^q,\qquad
153 !! \partial x'=x\left(
154 !! \frac{\partial x}x+
155 !! q\frac{\partial y'}{y'}-
156 !! q\frac{\partial y}{y}
157 !! \right)
158 !! $$
159 real(dp), intent(in) :: x(:), yp(:), y(:)
160 real(dp), intent(in) :: q
161 type(grad_t), intent(out), optional :: dx
162 type(grad_request_t), intent(in), optional :: grad
163 real(dp) :: xp(size(x))
164
165 xp = x * (yp / y)**q
166 if (.not. present(grad)) return
167 if (grad%dX_free) dx%dX_free = xp / x
168 if (grad%dV) dx%dV = xp * q / yp
169 if (grad%dV_free) dx%dV_free = -xp * q / y
170 end function
171
172 end module