Debian Codegraph @

src/mbd_hamiltonian.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
6 module mbd_hamiltonian
7 !! Forming and solving MBD Hamiltonian.
8
9 use mbd_constants
10 use mbd_damping, only: damping_t
11 use mbd_dipole, only: dipole_matrix
12 use mbd_geom, only: geom_t
13 use mbd_gradients, only: grad_t, grad_matrix_re_t, grad_matrix_cplx_t, 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_hamiltonian_energy
21
22 interface get_mbd_hamiltonian_energy
23 !! Form and solve either a real or a complex MBD Hamiltonian.
24 !!
25 !! The real-typed version is equivalent to \(\mathbf q=0\).
26 !!
27 !! $$
28 !! \begin{gathered}
29 !! E_\text{MBD}(\mathbf q)=\frac12\operatorname{Tr}\big(\sqrt{\mathbf Q(\mathbf
30 !! q)}\big)- 3\sum_i\frac{\omega_i}2,\qquad
31 !! \mathbf Q_{ij}(\mathbf q)=\omega_i^2\delta_{ij}\mathbf I+
32 !! \omega_i\omega_j\sqrt{\alpha_{0,i}\alpha_{0,j}}\mathbf T_{ij}(\mathbf q)
33 !! \\ \mathbf Q(\mathbf q)\equiv
34 !! \mathbf C(\mathbf q)\boldsymbol\Lambda(\mathbf q)\mathbf C(\mathbf
35 !! q)^\dagger,\qquad
36 !! \boldsymbol\Lambda(\mathbf q)
37 !! \equiv\operatorname{diag}(\{\tilde\omega_i(\mathbf q)^2\}),\qquad
38 !! \operatorname{Tr}\big(\sqrt{\mathbf Q(\mathbf q)}\big)
39 !! =\sum_i\tilde\omega_i(\mathbf q)
40 !! \end{gathered}
41 !! $$
42 !!
43 !! $$
44 !! \begin{aligned}
45 !! \partial E_\text{MBD}&=\frac14\operatorname{Tr}\big(
46 !! \mathbf C\boldsymbol\Lambda^{-\frac12}\mathbf C^\dagger
47 !! \partial\mathbf Q
48 !! \big)-
49 !! 3\sum_i\frac{\partial\omega_i}2
50 !! \\ \frac{\partial E_\text{MBD}}{\partial X_i}&=
51 !! \operatorname{Re}\frac12\sum_{p\zeta}(
52 !! \mathbf C\boldsymbol\Lambda^{-\frac12}\mathbf C^\dagger
53 !! )_{i\zeta,p}
54 !! \frac{\partial Q_{p,i\zeta}}{\partial X_i}-
55 !! \frac32\frac{\partial\omega_i}{\partial X_i}
56 !! \end{aligned}
57 !! $$
58 !!
59 !! $$
60 !! \begin{aligned}
61 !! \partial\mathbf Q_{ij}=&
62 !! 2\delta_{ij}\omega_i\partial\omega_i\mathbf I+
63 !! \omega_i\omega_j\sqrt{\alpha_{0,i}\alpha_{0,j}}\mathbf T_{ij}\left(
64 !! \frac{\partial\omega_i}{\omega_i}+
65 !! \frac{\partial\omega_j}{\omega_j}+
66 !! \frac12\frac{\partial\alpha_{0,i}}{\alpha_{0,i}}+
67 !! \frac12\frac{\partial\alpha_{0,j}}{\alpha_{0,j}}
68 !! \right)
69 !! \\ &+\omega_i\omega_j\sqrt{\alpha_{0,i}\alpha_{0,j}}
70 !! \partial\mathbf T_{ij}
71 !! \end{aligned}
72 !! $$
73 module procedure get_mbd_hamiltonian_energy_real
74 module procedure get_mbd_hamiltonian_energy_complex
75 end interface
76
77 contains
78
79 #endif
80
81 #ifndef DO_COMPLEX_TYPE
82 type(result_t) function get_mbd_hamiltonian_energy_real( &
83 geom, alpha_0, omega, damp, grad) result(res)
84 #else
85 type(result_t) function get_mbd_hamiltonian_energy_complex( &
86 geom, alpha_0, omega, damp, grad, q) result(res)
87 #endif
88 type(geom_t), intent(inout) :: geom
89 real(dp), intent(in) :: alpha_0(:)
90 real(dp), intent(in) :: omega(:)
91 type(damping_t), intent(in) :: damp
92 type(grad_request_t), intent(in) :: grad
93 #ifdef DO_COMPLEX_TYPE
94 real(dp), intent(in) :: q(3)
95 #endif
96
97 #ifndef DO_COMPLEX_TYPE
98 type(matrix_re_t) :: relay, dQ, T, modes, c_lambda12i_c
99 type(grad_matrix_re_t) :: dT
100 #else
101 type(matrix_cplx_t) :: relay, dQ, T, modes, c_lambda12i_c
102 type(grad_matrix_cplx_t) :: dT
103 #endif
104 real(dp), allocatable :: eigs(:)
105 integer :: n_negative_eigs, n_atoms, i_xyz, i_latt
106 character(120) :: msg
107
108 n_atoms = geom%siz()
109 call geom%clock(20)
110 #ifndef DO_COMPLEX_TYPE
111 T = dipole_matrix(geom, damp, dT, grad)
112 #else
113 T = dipole_matrix(geom, damp, dT, grad, q)
114 #endif
115 call geom%clock(-20)
116 if (geom%has_exc()) return
117 if (grad%any()) then
118 call relay%copy_from(T)
119 else
120 call relay%move_from(T)
121 end if
122 call relay%mult_cross(omega * sqrt(alpha_0))
123 call relay%add_diag(omega**2)
124 call geom%clock(21)
125 if (geom%get_modes .or. grad%any()) then
126 call modes%alloc_from(relay)
127 allocate (eigs(3 * n_atoms))
128 call modes%eigh(eigs, geom%exc, src=relay, clock=geom%timer)
129 if (geom%get_modes) then
130 #ifndef DO_COMPLEX_TYPE
131 call move_alloc(modes%val, res%modes)
132 #else
133 call move_alloc(modes%val, res%modes_k_single)
134 #endif
135 end if
136 else
137 eigs = relay%eigvalsh(geom%exc, destroy=.true., clock=geom%timer)
138 end if
139 if (geom%has_exc()) return
140 call geom%clock(-21)
141 if (geom%get_eigs) res%mode_eigs = eigs
142 n_negative_eigs = count(eigs(:) < 0)
143 if (n_negative_eigs > 0) then
144 msg = "CDM Hamiltonian has "//trim(tostr(n_negative_eigs))// &
145 " negative eigenvalues"
146 if (geom%param%zero_negative_eigvals) then
147 where (eigs < 0) eigs = 0d0
148 call geom%log%warn(msg)
149 else
150 geom%exc%code = MBD_EXC_NEG_EIGVALS
151 geom%exc%msg = msg
152 return
153 end if
154 end if
155 res%energy = 1d0 / 2 * sum(sqrt(eigs)) - 3d0 / 2 * sum(omega)
156 if (.not. grad%any()) return
157 call geom%clock(22)
158 call c_lambda12i_c%copy_from(modes)
159 call c_lambda12i_c%mult_cols_3n(eigs**(-1d0 / 4))
160 call geom%clock(14)
161 c_lambda12i_c = c_lambda12i_c%mmul(c_lambda12i_c, transB='C')
162 call geom%clock(-14)
163 #ifdef DO_COMPLEX_TYPE
164 c_lambda12i_c%val = conjg(c_lambda12i_c%val)
165 #endif
166 call dQ%init_from(T)
167 call geom%clock(15)
168 if (grad%dcoords) then
169 allocate (res%dE%dcoords(n_atoms, 3))
170 do i_xyz = 1, 3
171 dQ%val = dT%dr(:, :, i_xyz)
172 call dQ%mult_cross(omega * sqrt(alpha_0))
173 dQ%val = c_lambda12i_c%val * dQ%val
174 res%dE%dcoords(:, i_xyz) = 1d0 / 2 * dble(dQ%contract_n33_rows())
175 end do
176 end if
177 if (grad%dlattice) then
178 allocate (res%dE%dlattice(3, 3))
179 do i_latt = 1, 3
180 do i_xyz = 1, 3
181 dQ%val = dT%dlattice(:, :, i_latt, i_xyz)
182 call dQ%mult_cross(omega * sqrt(alpha_0))
183 dQ%val = c_lambda12i_c%val * dQ%val
184 res%dE%dlattice(i_latt, i_xyz) = 1d0 / 4 * dble(dQ%sum_all())
185 end do
186 end do
187 end if
188 if (grad%dalpha) then
189 dQ%val = T%val
190 call dQ%mult_cross(omega * sqrt(alpha_0))
191 call dQ%mult_rows(1d0 / (2 * alpha_0))
192 dQ%val = c_lambda12i_c%val * dQ%val
193 res%dE%dalpha = 1d0 / 2 * dble(dQ%contract_n33_rows())
194 end if
195 if (grad%domega) then
196 dQ%val = T%val
197 call dQ%mult_cross(omega * sqrt(alpha_0))
198 call dQ%mult_rows(1d0 / omega)
199 call dQ%add_diag(omega)
200 dQ%val = c_lambda12i_c%val * dQ%val
201 res%dE%domega = 1d0 / 2 * dble(dQ%contract_n33_rows()) - 3d0 / 2
202 end if
203 if (grad%dr_vdw) then
204 dQ%val = dT%dvdw
205 call dQ%mult_cross(omega * sqrt(alpha_0))
206 dQ%val = c_lambda12i_c%val * dQ%val
207 res%dE%dr_vdw = 1d0 / 2 * dble(dQ%contract_n33_rows())
208 end if
209 #ifdef DO_COMPLEX_TYPE
210 if (grad%dq) then
211 allocate (res%dE%dq(3))
212 do i_latt = 1, 3
213 dQ%val = dT%dq(:, :, i_latt)
214 call dQ%mult_cross(omega * sqrt(alpha_0))
215 dQ%val = c_lambda12i_c%val * dQ%val
216 res%dE%dq(i_latt) = 1d0 / 4 * dble(dQ%sum_all())
217 end do
218 end if
219 #endif
220 call geom%clock(-15)
221 call geom%clock(-22)
222 end function
223
224 #ifndef DO_COMPLEX_TYPE
225 # define DO_COMPLEX_TYPE
226 # include "mbd_hamiltonian.F90"
227
228 end module
229
230 #endif