Debian Codegraph @

tests/mbd_grad_test_cases.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_grad_test_cases
5
6 use mbd_constants
7 use mbd_damping, only: damping_t, damping_fermi
8 use mbd_dipole, only: dipole_matrix, T_bare, T_erf_coulomb, damping_grad, T_erfc
9 use mbd_formulas, only: rpa_rescale_eigval
10 use mbd_geom, only: geom_t
11 use mbd_gradients, only: grad_t, grad_matrix_re_t, grad_request_t, grad_scalar_t
12 use mbd_hamiltonian, only: get_mbd_hamiltonian_energy
13 use mbd_methods, only: get_mbd_scs_energy, get_mbd_energy
14 use mbd_rpa, only: get_mbd_rpa_energy
15 use mbd_ts, only: get_ts_energy
16 use mbd_scs, only: run_scs
17 use mbd_utils, only: diff7, findval, tostr, result_t
18
19 implicit none
20
21 type(geom_t), allocatable :: geom
22 integer :: n_failed, rank
23
24 contains
25
26 logical function failed(diff, thre)
27 real(dp), intent(in) :: diff, thre
28
29 failed = .not. abs(diff) < thre
30 if (rank == 0) write (6, '(A,G10.3,A,G10.3,A)') 'diff:', diff, ', threshold:', thre, ': '
31 if (failed) n_failed = n_failed + 1
32 end function
33
34 subroutine print_matrix(label, A, prec)
35 character(len=*), intent(in) :: label
36 real(dp), intent(in) :: A(:, :)
37 integer, optional, intent(in) :: prec
38
39 integer :: m, n, i, j, prec_
40 character(len=10) :: fm
41
42 if (present(prec)) then
43 prec_ = prec
44 else
45 prec_ = 3
46 end if
47 m = size(A, 1)
48 n = size(A, 2)
49 write (fm, '("(g",i2,".",i1,")")') prec_ + 8, prec_
50 write (6, '(A,":")') label
51 do i = 1, m
52 do j = 1, n
53 write (6, fm, advance="no") A(i, j)
54 end do
55 write (6, *)
56 end do
57 end subroutine
58
59 subroutine test_T_bare_deriv()
60 real(dp) :: r(3), r_diff(3), T(3, 3), diff(3, 3), T_diff_num(3, 3, -3:3), delta
61 type(grad_matrix_re_t) :: dT
62 integer :: a, b, c, i_step
63
64 delta = 1d-2
65 r = [1.12d0, -2.12d0, 0.12d0]
66 T = T_bare(r, dT, .true.)
67 diff = 0d0
68 do c = 1, 3
69 do i_step = -3, 3
70 if (i_step == 0) cycle
71 r_diff = r
72 r_diff(c) = r_diff(c) + i_step * delta
73 T_diff_num(:, :, i_step) = T_bare(r_diff)
74 end do
75 #ifndef WITHOUT_DO_CONCURRENT
76 do concurrent(a=1:3, b=1:3)
77 #else
78 do a = 1, 3
79 do b = 1, 3
80 #endif
81 T_diff_num(a, b, 0) = diff7(T_diff_num(a, b, :), delta)
82 end do
83 #ifdef WITHOUT_DO_CONCURRENT
84 end do
85 #endif
86 diff = max(diff, abs(T_diff_num(:, :, 0) - dT%dr(:, :, c)) / T_diff_num(:, :, 0))
87 end do
88 if (failed(maxval(abs(diff)), 1d-10)) then
89 end if
90 end subroutine
91
92 subroutine test_T_GG_deriv_expl()
93 real(dp) :: r(3), r_diff(3), T(3, 3), diff(3, 3), T_diff_num(3, 3, -3:3), delta, sigma
94 type(grad_matrix_re_t) :: dT
95 integer :: a, b, c, i_step
96
97 delta = 1d-2
98 r = [1.02d0, -2.22d0, 0.15d0]
99 sigma = 1.2d0
100 T = T_erf_coulomb(r, sigma, dT, grad_request_t(dcoords=.true.))
101 diff = 0d0
102 do c = 1, 3
103 do i_step = -3, 3
104 if (i_step == 0) cycle
105 r_diff = r
106 r_diff(c) = r_diff(c) + i_step * delta
107 T_diff_num(:, :, i_step) = T_erf_coulomb(r_diff, sigma)
108 end do
109 #ifndef WITHOUT_DO_CONCURRENT
110 do concurrent(a=1:3, b=1:3)
111 #else
112 do a = 1, 3
113 do b = 1, 3
114 #endif
115 T_diff_num(a, b, 0) = diff7(T_diff_num(a, b, :), delta)
116 end do
117 #ifdef WITHOUT_DO_CONCURRENT
118 end do
119 #endif
120 diff = max(diff, abs(T_diff_num(:, :, 0) - dT%dr(:, :, c)) / T_diff_num(:, :, 0))
121 end do
122 if (failed(maxval(abs(diff)), 1d-10)) then
123 end if
124 end subroutine
125
126 subroutine test_T_GG_deriv_impl()
127 real(dp) :: r(3), T(3, 3), diff(3, 3), T_diff_num(3, 3, -3:3), delta, sigma, sigma_diff
128 type(grad_matrix_re_t) :: dT
129 integer :: a, b, i_step
130
131 delta = 1d-3
132 r = [1.02d0, -2.22d0, 0.15d0]
133 sigma = 1.2d0
134 T = T_erf_coulomb(r, sigma, dT, grad_request_t(dsigma=.true.))
135 do i_step = -3, 3
136 if (i_step == 0) cycle
137 sigma_diff = sigma + i_step * delta
138 T_diff_num(:, :, i_step) = T_erf_coulomb(r, sigma_diff)
139 end do
140 #ifndef WITHOUT_DO_CONCURRENT
141 do concurrent(a=1:3, b=1:3)
142 #else
143 do a = 1, 3
144 do b = 1, 3
145 #endif
146 T_diff_num(a, b, 0) = diff7(T_diff_num(a, b, :), delta)
147 end do
148 #ifdef WITHOUT_DO_CONCURRENT
149 end do
150 #endif
151 diff = (T_diff_num(:, :, 0) - dT%dsigma) / T_diff_num(:, :, 0)
152 if (failed(maxval(abs(diff)), 1d-10)) then
153 call print_matrix('delta dTGG', diff)
154 end if
155 end subroutine
156
157 subroutine test_T_fermi_deriv_impl()
158 real(dp) :: r(3), T(3, 3), T0(3, 3), &
159 diff(3, 3), T_diff_num(3, 3, -3:3), delta, rvdw, rvdw_diff, f
160 type(grad_matrix_re_t) :: dT, dT0
161 type(grad_scalar_t) :: df
162 integer :: a, b, i_step
163
164 delta = 1d-3
165 r = [1.02d0, -2.22d0, 0.15d0]
166 rvdw = 2.5d0
167 f = damping_fermi(r, rvdw, 6d0, df, grad_request_t(dr_vdw=.true.))
168 T0 = T_bare(r)
169 T = damping_grad(f, df, T0, dT0, dT, grad_request_t(dr_vdw=.true.))
170 do i_step = -3, 3
171 if (i_step == 0) cycle
172 rvdw_diff = rvdw + i_step * delta
173 T_diff_num(:, :, i_step) = damping_fermi(r, rvdw_diff, 6d0) * T_bare(r)
174 end do
175 #ifndef WITHOUT_DO_CONCURRENT
176 do concurrent(a=1:3, b=1:3)
177 #else
178 do a = 1, 3
179 do b = 1, 3
180 #endif
181 T_diff_num(a, b, 0) = diff7(T_diff_num(a, b, :), delta)
182 end do
183 #ifdef WITHOUT_DO_CONCURRENT
184 end do
185 #endif
186 diff = (T_diff_num(:, :, 0) - dT%dvdw) / T_diff_num(:, :, 0)
187 if (failed(maxval(abs(diff)), 1d-10)) then
188 call print_matrix('delta dTfermi', diff)
189 end if
190 end subroutine
191
192 subroutine test_T_erfc_deriv_expl()
193 real(dp) :: r(3), r_diff(3), T(3, 3), diff(3, 3), T_diff_num(3, 3, -3:3), delta, gamm
194 type(grad_matrix_re_t) :: dT
195 integer :: a, b, c, i_step
196
197 delta = 1d-2
198 r = [1.02d0, -2.22d0, 0.15d0]
199 gamm = 1.2d0
200 T = T_erfc(r, gamm, dT, grad_request_t(dcoords=.true.))
201 diff = 0d0
202 do c = 1, 3
203 do i_step = -3, 3
204 if (i_step == 0) cycle
205 r_diff = r
206 r_diff(c) = r_diff(c) + i_step * delta
207 T_diff_num(:, :, i_step) = T_erfc(r_diff, gamm)
208 end do
209 #ifndef WITHOUT_DO_CONCURRENT
210 do concurrent(a=1:3, b=1:3)
211 #else
212 do a = 1, 3
213 do b = 1, 3
214 #endif
215 T_diff_num(a, b, 0) = diff7(T_diff_num(a, b, :), delta)
216 end do
217 #ifdef WITHOUT_DO_CONCURRENT
218 end do
219 #endif
220 diff = max(diff, abs(T_diff_num(:, :, 0) - dT%dr(:, :, c)) / T_diff_num(:, :, 0))
221 end do
222 if (failed(maxval(abs(diff)), 1d-9)) then
223 end if
224 end subroutine
225
226 subroutine test_mbd_deriv_expl()
227 real(dp) :: delta
228 type(damping_t) :: damp
229 real(dp), allocatable :: coords(:, :)
230 real(dp), allocatable :: gradients(:, :)
231 real(dp), allocatable :: diff(:, :)
232 real(dp), allocatable :: alpha_0(:)
233 real(dp), allocatable :: omega(:)
234 type(result_t) :: res(-3:3)
235 real(dp), allocatable :: gradients_anl(:, :)
236 integer :: i_atom, n_atoms, i_xyz, i_step
237
238 delta = 0.01d0
239 n_atoms = 3
240 allocate (coords(3, n_atoms), source=0d0)
241 allocate (gradients(n_atoms, 3))
242 coords(1, 3) = 1d0
243 coords(2, 1) = 4d0
244 coords(3, 2) = 4d0
245 geom%coords = coords
246 call geom%init()
247 damp%version = 'fermi,dip'
248 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
249 damp%beta = 0.83d0
250 alpha_0 = [11d0, 10d0, 12d0]
251 omega = [.7d0, .65d0, .75d0]
252 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, grad_request_t(dcoords=.true.))
253 gradients_anl = res(0)%dE%dcoords
254 do i_atom = 1, n_atoms
255 do i_xyz = 1, 3
256 do i_step = -3, 3
257 if (i_step == 0) cycle
258 geom%coords = coords
259 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + i_step * delta
260 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, grad_request_t())
261 end do
262 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
263 end do
264 end do
265 call geom%destroy()
266 diff = (gradients - gradients_anl) / gradients_anl
267 if (failed(maxval(abs(diff)), 1d-8)) then
268 call print_matrix('delta gradients', diff)
269 end if
270 end subroutine
271
272 subroutine test_mbd_ewald_deriv_expl()
273 real(dp) :: delta, k_point(3)
274 type(damping_t) :: damp
275 real(dp), allocatable :: &
276 coords(:, :), gradients(:, :), diff(:, :), alpha_0(:), omega(:), &
277 gradients_anl(:, :)
278 type(result_t) :: res(-3:3)
279 integer :: i_atom, n_atoms, i_xyz, i_step
280
281 delta = 0.01d0
282 n_atoms = 2
283 allocate (coords(3, n_atoms), source=0d0)
284 allocate (gradients(n_atoms, 3))
285 coords(3, 1) = 1d0
286 coords(1, 2) = 1d0
287 coords(2, 2) = 4d0
288 geom%coords = coords
289 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
290 k_point = [0.4d0, 0d0, 0d0]
291 call geom%init()
292 damp%version = 'fermi,dip'
293 damp%r_vdw = [3.55d0, 3.5d0]
294 damp%beta = 0.83d0
295 alpha_0 = [11d0, 10d0]
296 omega = [.7d0, .65d0]
297 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
298 grad_request_t(dcoords=.true.), k_point)
299 gradients_anl = res(0)%dE%dcoords
300 do i_atom = 1, n_atoms
301 do i_xyz = 1, 3
302 do i_step = -3, 3
303 if (i_step == 0) cycle
304 geom%coords = coords
305 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + i_step * delta
306 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
307 grad_request_t(), k_point)
308 end do
309 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
310 end do
311 end do
312 call geom%destroy()
313 diff = (gradients - gradients_anl) / gradients_anl
314 if (failed(maxval(abs(diff)), 2d-8)) then
315 call print_matrix('delta gradients', diff)
316 call print_matrix('anl', gradients_anl)
317 call print_matrix('num', gradients)
318 end if
319 end subroutine
320
321 subroutine test_mbd_ewald_deriv_impl_q()
322 real(dp) :: delta, k_point(3), k_point_diff(3)
323 type(damping_t) :: damp
324 real(dp), allocatable :: &
325 gradients(:), diff(:), alpha_0(:), omega(:), gradients_anl(:)
326 type(result_t) :: res(-3:3)
327 integer :: n_atoms, i_xyz, i_step
328
329 delta = 0.01d0
330 n_atoms = 2
331 allocate (geom%coords(3, n_atoms), source=0d0)
332 allocate (gradients(3))
333 geom%coords(3, 1) = 1d0
334 geom%coords(1, 2) = 1d0
335 geom%coords(2, 2) = 4d0
336 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
337 k_point = [0.4d0, 0d0, 0d0]
338 call geom%init()
339 damp%version = 'fermi,dip'
340 damp%r_vdw = [3.55d0, 3.5d0]
341 damp%beta = 0.83d0
342 alpha_0 = [11d0, 10d0]
343 omega = [.7d0, .65d0]
344 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
345 grad_request_t(dq=.true.), k_point)
346 gradients_anl = res(0)%dE%dq
347 do i_xyz = 1, 3
348 do i_step = -3, 3
349 if (i_step == 0) cycle
350 k_point_diff = k_point
351 k_point_diff(i_xyz) = k_point_diff(i_xyz) + i_step * delta
352 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
353 grad_request_t(), k_point_diff)
354 end do
355 gradients(i_xyz) = diff7(res%energy, delta)
356 end do
357 call geom%destroy()
358 diff = (gradients - gradients_anl) / gradients_anl
359 if (failed(maxval(abs(diff)), 1d-6)) then
360 print *, 'delta gradients', diff
361 print *, 'anl', gradients_anl
362 print *, 'num', gradients
363 end if
364 end subroutine
365
366 subroutine test_mbd_ewald_deriv_stress()
367 real(dp) :: delta, k_point(3)
368 type(damping_t) :: damp
369 real(dp), allocatable :: &
370 lattice(:, :), gradients(:, :), diff(:, :), alpha_0(:), omega(:), &
371 gradients_anl(:, :)
372 type(result_t) :: res(-3:3)
373 integer :: i_latt, n_atoms, i_xyz, i_step
374
375 delta = 0.01d0
376 n_atoms = 2
377 allocate (geom%coords(3, n_atoms), source=0d0)
378 allocate (gradients(3, 3))
379 geom%coords(3, 1) = 1d0
380 geom%coords(1, 2) = 1d0
381 geom%coords(2, 2) = 4d0
382 lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
383 geom%lattice = lattice
384 k_point = [0.4d0, 0d0, 0d0]
385 call geom%init()
386 damp%version = 'fermi,dip'
387 damp%r_vdw = [3.55d0, 3.5d0]
388 damp%beta = 0.83d0
389 alpha_0 = [11d0, 10d0]
390 omega = [.7d0, .65d0]
391 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
392 grad_request_t(dlattice=.true.), k_point)
393 gradients_anl = res(0)%dE%dlattice
394 do i_latt = 1, 3
395 do i_xyz = 1, 3
396 do i_step = -3, 3
397 if (i_step == 0) cycle
398 geom%lattice = lattice
399 geom%lattice(i_xyz, i_latt) = geom%lattice(i_xyz, i_latt) + i_step * delta
400 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
401 grad_request_t(), k_point)
402 end do
403 gradients(i_latt, i_xyz) = diff7(res%energy, delta)
404 end do
405 end do
406 call geom%destroy()
407 diff = (gradients - gradients_anl) / gradients_anl
408 if (failed(maxval(abs(diff)), 1d-5)) then
409 call print_matrix('delta gradients', diff)
410 call print_matrix('anl', gradients_anl)
411 call print_matrix('num', gradients)
412 end if
413 end subroutine
414
415 subroutine test_scs_deriv_expl()
416 real(dp) :: delta
417 type(damping_t) :: damp
418 real(dp), allocatable :: coords(:, :)
419 real(dp), allocatable :: gradients(:, :, :), gradients_anl(:, :, :)
420 real(dp), allocatable :: diff(:, :, :)
421 real(dp), allocatable :: alpha_0(:)
422 integer :: i_atom, n_atoms, i_xyz, i_step, j_atom, my_i_atom, my_nratoms, &
423 my_ncatoms, my_j_atom
424 real(dp), allocatable :: alpha_scs(:, :)
425 type(grad_t), allocatable :: dalpha_scs(:)
426
427 delta = 0.05d0
428 n_atoms = 3
429 allocate (coords(3, n_atoms), source=0d0)
430 coords(1, 3) = 1d0
431 coords(2, 1) = 4d0
432 coords(3, 2) = 4d0
433 geom%coords = coords
434 call geom%init()
435 my_nratoms = size(geom%idx%i_atom)
436 my_ncatoms = size(geom%idx%j_atom)
437 allocate (gradients(my_nratoms, my_ncatoms, 3))
438 allocate (gradients_anl(my_nratoms, my_ncatoms, 3))
439 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
440 damp%version = 'fermi,dip,gg'
441 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
442 damp%beta = 0.83d0
443 alpha_0 = [11d0, 10d0, 12d0]
444 alpha_scs(:, 0) = &
445 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dcoords=.true.))
446 do my_i_atom = 1, my_nratoms
447 gradients_anl(my_i_atom, :, :) = dalpha_scs(my_i_atom)%dcoords
448 end do
449 do j_atom = 1, n_atoms
450 my_j_atom = findval(geom%idx%j_atom, j_atom)
451 do i_xyz = 1, 3
452 do i_step = -3, 3
453 if (i_step == 0) cycle
454 geom%coords = coords
455 geom%coords(i_xyz, j_atom) = geom%coords(i_xyz, j_atom) + &
456 i_step * delta
457 alpha_scs(:, i_step) = &
458 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t())
459 end do
460 if (my_j_atom > 0) then
461 do my_i_atom = 1, my_nratoms
462 i_atom = geom%idx%i_atom(my_i_atom)
463 gradients(my_i_atom, my_j_atom, i_xyz) = &
464 diff7(alpha_scs(i_atom, :), delta)
465 end do
466 end if
467 end do
468 end do
469 call geom%destroy()
470 diff = (gradients - gradients_anl) / gradients_anl
471 if (failed(maxval(abs(diff)), 1d-5)) then
472 call print_matrix('diff x', diff(:, :, 1))
473 call print_matrix('diff y', diff(:, :, 2))
474 call print_matrix('diff z', diff(:, :, 3))
475 end if
476 end subroutine
477
478 subroutine test_scs_ewald_deriv_expl()
479 real(dp) :: delta
480 type(damping_t) :: damp
481 real(dp), allocatable :: coords(:, :)
482 real(dp), allocatable :: gradients(:, :, :), gradients_anl(:, :, :)
483 real(dp), allocatable :: diff(:, :, :)
484 real(dp), allocatable :: alpha_0(:)
485 integer :: i_atom, n_atoms, i_xyz, i_step, j_atom, my_i_atom, my_nratoms, &
486 my_ncatoms, my_j_atom
487 real(dp), allocatable :: alpha_scs(:, :)
488 type(grad_t), allocatable :: dalpha_scs(:)
489
490 delta = 0.05d0
491 n_atoms = 2
492 allocate (coords(3, n_atoms), source=0d0)
493 coords(3, 1) = 1d0
494 coords(1, 2) = 1d0
495 coords(2, 2) = 4d0
496 geom%coords = coords
497 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
498 call geom%init()
499 my_nratoms = size(geom%idx%i_atom)
500 my_ncatoms = size(geom%idx%j_atom)
501 allocate (gradients(my_nratoms, my_ncatoms, 3))
502 allocate (gradients_anl(my_nratoms, my_ncatoms, 3))
503 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
504 damp%version = 'fermi,dip,gg'
505 damp%r_vdw = [3.55d0, 3.5d0]
506 damp%beta = 0.83d0
507 alpha_0 = [11d0, 10d0]
508 alpha_scs(:, 0) = &
509 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dcoords=.true.))
510 do my_i_atom = 1, my_nratoms
511 gradients_anl(my_i_atom, :, :) = dalpha_scs(my_i_atom)%dcoords
512 end do
513 do j_atom = 1, n_atoms
514 my_j_atom = findval(geom%idx%j_atom, j_atom)
515 do i_xyz = 1, 3
516 do i_step = -3, 3
517 if (i_step == 0) cycle
518 geom%coords = coords
519 geom%coords(i_xyz, j_atom) = geom%coords(i_xyz, j_atom) + &
520 i_step * delta
521 alpha_scs(:, i_step) = &
522 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t())
523 end do
524 if (my_j_atom > 0) then
525 do my_i_atom = 1, my_nratoms
526 i_atom = geom%idx%i_atom(my_i_atom)
527 gradients(my_i_atom, my_j_atom, i_xyz) = &
528 diff7(alpha_scs(i_atom, :), delta)
529 end do
530 end if
531 end do
532 end do
533 call geom%destroy()
534 diff = (gradients - gradients_anl) / gradients_anl
535 if (failed(maxval(abs(diff)), 1d-5)) then
536 call print_matrix('diff x', diff(:, :, 1))
537 call print_matrix('diff y', diff(:, :, 2))
538 call print_matrix('diff z', diff(:, :, 3))
539 end if
540 end subroutine
541
542 subroutine test_scs_ewald_deriv_stress()
543 real(dp) :: delta
544 type(damping_t) :: damp
545 real(dp), allocatable :: gradients(:, :, :), gradients_anl(:, :, :), &
546 diff(:, :, :), alpha_0(:), lattice(:, :)
547 integer :: i_atom, n_atoms, i_xyz, i_step, my_i_atom, my_nratoms, &
548 my_ncatoms, i_latt
549 real(dp), allocatable :: alpha_scs(:, :)
550 type(grad_t), allocatable :: dalpha_scs(:)
551
552 delta = 0.05d0
553 n_atoms = 2
554 allocate (geom%coords(3, n_atoms), source=0d0)
555 geom%coords(3, 1) = 1d0
556 geom%coords(1, 2) = 1d0
557 geom%coords(2, 2) = 4d0
558 lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
559 geom%lattice = lattice
560 call geom%init()
561 my_nratoms = size(geom%idx%i_atom)
562 my_ncatoms = size(geom%idx%j_atom)
563 allocate (gradients(my_nratoms, 3, 3))
564 allocate (gradients_anl(my_nratoms, 3, 3))
565 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
566 damp%version = 'fermi,dip,gg'
567 damp%r_vdw = [3.55d0, 3.5d0]
568 damp%beta = 0.83d0
569 alpha_0 = [11d0, 10d0]
570 alpha_scs(:, 0) = &
571 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dlattice=.true.))
572 do my_i_atom = 1, my_nratoms
573 gradients_anl(my_i_atom, :, :) = dalpha_scs(my_i_atom)%dlattice
574 end do
575 do i_latt = 1, 3
576 do i_xyz = 1, 3
577 do i_step = -3, 3
578 if (i_step == 0) cycle
579 geom%lattice = lattice
580 geom%lattice(i_xyz, i_latt) = geom%lattice(i_xyz, i_latt) + i_step * delta
581 alpha_scs(:, i_step) = &
582 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t())
583 end do
584 do my_i_atom = 1, my_nratoms
585 i_atom = geom%idx%i_atom(my_i_atom)
586 gradients(my_i_atom, i_latt, i_xyz) = diff7(alpha_scs(i_atom, :), delta)
587 end do
588 end do
589 end do
590 call geom%destroy()
591 diff = (gradients - gradients_anl) / gradients_anl
592 if (failed(maxval(abs(diff)), 1d-4)) then
593 call print_matrix('diff x', diff(:, :, 1))
594 call print_matrix('diff y', diff(:, :, 2))
595 call print_matrix('diff z', diff(:, :, 3))
596 end if
597 end subroutine
598
599 subroutine test_scs_deriv_impl_alpha
600 real(dp) :: delta
601 type(damping_t) :: damp
602 real(dp), allocatable :: coords(:, :), gradients(:, :), &
603 gradients_anl(:, :), diff(:, :), alpha_0(:), alpha_0_diff(:), &
604 alpha_scs(:, :)
605 integer :: i_atom, n_atoms, i_step, j_atom, my_i_atom, my_nratoms, &
606 my_ncatoms, my_j_atom
607 type(grad_t), allocatable :: dalpha_scs(:)
608
609 delta = 0.1d0
610 n_atoms = 3
611 allocate (coords(3, n_atoms), source=0d0)
612 coords(1, 3) = 1d0
613 coords(2, 1) = 4d0
614 coords(3, 2) = 4d0
615 geom%coords = coords
616 call geom%init()
617 my_nratoms = size(geom%idx%i_atom)
618 my_ncatoms = size(geom%idx%j_atom)
619 allocate (gradients(my_nratoms, my_ncatoms))
620 allocate (gradients_anl(my_nratoms, my_ncatoms))
621 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
622 damp%version = 'fermi,dip,gg'
623 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
624 damp%beta = 0.83d0
625 alpha_0 = [11d0, 10d0, 12d0]
626 alpha_scs(:, 0) = &
627 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dalpha=.true.))
628 do my_i_atom = 1, my_nratoms
629 gradients_anl(my_i_atom, :) = dalpha_scs(my_i_atom)%dalpha
630 end do
631 do j_atom = 1, n_atoms
632 my_j_atom = findval(geom%idx%j_atom, j_atom)
633 do i_step = -3, 3
634 if (i_step == 0) cycle
635 alpha_0_diff = alpha_0
636 alpha_0_diff(j_atom) = alpha_0_diff(j_atom) + i_step * delta
637 alpha_scs(:, i_step) = &
638 run_scs(geom, alpha_0_diff, damp, dalpha_scs, grad_request_t())
639 end do
640 if (my_j_atom > 0) then
641 do my_i_atom = 1, my_nratoms
642 i_atom = geom%idx%i_atom(my_i_atom)
643 gradients(my_i_atom, my_j_atom) = diff7(alpha_scs(i_atom, :), delta)
644 end do
645 end if
646 end do
647 call geom%destroy()
648 diff = (gradients - gradients_anl) / gradients_anl
649 if (failed(maxval(abs(diff)), 1d-6)) then
650 call print_matrix('diff', diff)
651 end if
652 end subroutine
653
654 subroutine test_scs_ewald_deriv_impl_alpha
655 real(dp) :: delta
656 type(damping_t) :: damp
657 real(dp), allocatable :: coords(:, :), gradients(:, :), &
658 gradients_anl(:, :), diff(:, :), alpha_0(:), alpha_0_diff(:), &
659 alpha_scs(:, :)
660 integer :: i_atom, n_atoms, i_step, j_atom, my_i_atom, my_nratoms, &
661 my_ncatoms, my_j_atom
662 type(grad_t), allocatable :: dalpha_scs(:)
663
664 delta = 0.1d0
665 n_atoms = 2
666 allocate (coords(3, n_atoms), source=0d0)
667 coords(3, 1) = 1d0
668 coords(1, 2) = 1d0
669 coords(2, 2) = 4d0
670 geom%coords = coords
671 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
672 call geom%init()
673 my_nratoms = size(geom%idx%i_atom)
674 my_ncatoms = size(geom%idx%j_atom)
675 allocate (gradients(my_nratoms, my_ncatoms))
676 allocate (gradients_anl(my_nratoms, my_ncatoms))
677 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
678 damp%version = 'fermi,dip,gg'
679 damp%r_vdw = [3.55d0, 3.5d0]
680 damp%beta = 0.83d0
681 alpha_0 = [11d0, 10d0]
682 alpha_scs(:, 0) = &
683 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dalpha=.true.))
684 do my_i_atom = 1, my_nratoms
685 gradients_anl(my_i_atom, :) = dalpha_scs(my_i_atom)%dalpha
686 end do
687 do j_atom = 1, n_atoms
688 my_j_atom = findval(geom%idx%j_atom, j_atom)
689 do i_step = -3, 3
690 if (i_step == 0) cycle
691 alpha_0_diff = alpha_0
692 alpha_0_diff(j_atom) = alpha_0_diff(j_atom) + i_step * delta
693 alpha_scs(:, i_step) = &
694 run_scs(geom, alpha_0_diff, damp, dalpha_scs, grad_request_t())
695 end do
696 if (my_j_atom > 0) then
697 do my_i_atom = 1, my_nratoms
698 i_atom = geom%idx%i_atom(my_i_atom)
699 gradients(my_i_atom, my_j_atom) = diff7(alpha_scs(i_atom, :), delta)
700 end do
701 end if
702 end do
703 call geom%destroy()
704 diff = (gradients - gradients_anl) / gradients_anl
705 if (failed(maxval(abs(diff)), 1d-6)) then
706 call print_matrix('diff', diff)
707 end if
708 end subroutine
709
710 subroutine test_scs_deriv_impl_vdw
711 real(dp) :: delta
712 type(damping_t) :: damp
713 real(dp), allocatable :: coords(:, :), gradients(:, :), &
714 gradients_anl(:, :), diff(:, :), alpha_0(:), alpha_scs(:, :), rvdw(:)
715 integer :: i_atom, n_atoms, i_step, j_atom, my_i_atom, my_nratoms, &
716 my_ncatoms, my_j_atom
717 type(grad_t), allocatable :: dalpha_scs(:)
718
719 delta = 0.1d0
720 n_atoms = 3
721 allocate (coords(3, n_atoms), source=0d0)
722 coords(1, 3) = 1d0
723 coords(2, 1) = 4d0
724 coords(3, 2) = 4d0
725 geom%coords = coords
726 call geom%init()
727 my_nratoms = size(geom%idx%i_atom)
728 my_ncatoms = size(geom%idx%j_atom)
729 allocate (gradients(my_nratoms, my_ncatoms))
730 allocate (gradients_anl(my_nratoms, my_ncatoms))
731 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
732 damp%version = 'fermi,dip,gg'
733 rvdw = [3.55d0, 3.5d0, 3.56d0]
734 damp%r_vdw = rvdw
735 damp%beta = 0.83d0
736 alpha_0 = [11d0, 10d0, 12d0]
737 alpha_scs(:, 0) = &
738 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dr_vdw=.true.))
739 do my_i_atom = 1, my_nratoms
740 gradients_anl(my_i_atom, :) = dalpha_scs(my_i_atom)%dr_vdw
741 end do
742 do j_atom = 1, n_atoms
743 my_j_atom = findval(geom%idx%j_atom, j_atom)
744 do i_step = -3, 3
745 if (i_step == 0) cycle
746 damp%r_vdw = rvdw
747 damp%r_vdw(j_atom) = damp%r_vdw(j_atom) + i_step * delta
748 alpha_scs(:, i_step) = &
749 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t())
750 end do
751 if (my_j_atom > 0) then
752 do my_i_atom = 1, my_nratoms
753 i_atom = geom%idx%i_atom(my_i_atom)
754 gradients(my_i_atom, my_j_atom) = diff7(alpha_scs(i_atom, :), delta)
755 end do
756 end if
757 end do
758 call geom%destroy()
759 diff = (gradients - gradients_anl) / gradients_anl
760 if (failed(maxval(abs(diff)), 1d-6)) then
761 call print_matrix('diff', diff(:, :))
762 end if
763 end subroutine
764
765 subroutine test_scs_ewald_deriv_impl_vdw
766 real(dp) :: delta
767 type(damping_t) :: damp
768 real(dp), allocatable :: coords(:, :), gradients(:, :), &
769 gradients_anl(:, :), diff(:, :), alpha_0(:), alpha_scs(:, :), rvdw(:)
770 integer :: i_atom, n_atoms, i_step, j_atom, my_i_atom, my_nratoms, &
771 my_ncatoms, my_j_atom
772 type(grad_t), allocatable :: dalpha_scs(:)
773
774 delta = 0.1d0
775 n_atoms = 2
776 allocate (coords(3, n_atoms), source=0d0)
777 coords(3, 1) = 1d0
778 coords(1, 2) = 1d0
779 coords(2, 2) = 4d0
780 geom%coords = coords
781 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
782 call geom%init()
783 my_nratoms = size(geom%idx%i_atom)
784 my_ncatoms = size(geom%idx%j_atom)
785 allocate (gradients(my_nratoms, my_ncatoms))
786 allocate (gradients_anl(my_nratoms, my_ncatoms))
787 allocate (alpha_scs(n_atoms, -3:3), dalpha_scs(my_nratoms))
788 damp%version = 'fermi,dip,gg'
789 rvdw = [3.55d0, 3.5d0]
790 damp%r_vdw = rvdw
791 damp%beta = 0.83d0
792 alpha_0 = [11d0, 10d0]
793 alpha_scs(:, 0) = &
794 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t(dr_vdw=.true.))
795 do my_i_atom = 1, my_nratoms
796 gradients_anl(my_i_atom, :) = dalpha_scs(my_i_atom)%dr_vdw
797 end do
798 do j_atom = 1, n_atoms
799 my_j_atom = findval(geom%idx%j_atom, j_atom)
800 do i_step = -3, 3
801 if (i_step == 0) cycle
802 damp%r_vdw = rvdw
803 damp%r_vdw(j_atom) = damp%r_vdw(j_atom) + i_step * delta
804 alpha_scs(:, i_step) = &
805 run_scs(geom, alpha_0, damp, dalpha_scs, grad_request_t())
806 end do
807 if (my_j_atom > 0) then
808 do my_i_atom = 1, my_nratoms
809 i_atom = geom%idx%i_atom(my_i_atom)
810 gradients(my_i_atom, my_j_atom) = diff7(alpha_scs(i_atom, :), delta)
811 end do
812 end if
813 end do
814 call geom%destroy()
815 diff = (gradients - gradients_anl) / gradients_anl
816 if (failed(maxval(abs(diff)), 1d-5)) then
817 call print_matrix('diff', diff(:, :))
818 end if
819 end subroutine
820
821 subroutine test_mbd_deriv_impl_alpha()
822 real(dp) :: delta
823 type(damping_t) :: damp
824 real(dp), allocatable :: coords(:, :), gradients(:), &
825 gradients_anl(:), diff(:), alpha_0(:), alpha_0_diff(:), omega(:)
826 type(result_t) :: res(-3:3)
827 integer :: i_atom, n_atoms, i_step
828
829 delta = 0.1d0
830 n_atoms = 3
831 allocate (coords(3, n_atoms), source=0d0)
832 allocate (gradients(n_atoms))
833 coords(1, 3) = 1d0
834 coords(2, 1) = 4d0
835 coords(3, 2) = 4d0
836 geom%coords = coords
837 call geom%init()
838 damp%version = 'fermi,dip'
839 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
840 damp%beta = 0.83d0
841 alpha_0 = [11d0, 10d0, 12d0]
842 omega = [.7d0, .65d0, .75d0]
843 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
844 grad_request_t(dalpha=.true.))
845 gradients_anl = res(0)%dE%dalpha
846 do i_atom = 1, n_atoms
847 do i_step = -3, 3
848 if (i_step == 0) cycle
849 alpha_0_diff = alpha_0
850 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
851 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0_diff, omega, damp, &
852 grad_request_t())
853 end do
854 gradients(i_atom) = diff7(res%energy, delta)
855 end do
856 call geom%destroy()
857 diff = (gradients - gradients_anl) / gradients_anl
858 if (failed(maxval(abs(diff)), 1d-6)) then
859 call print_matrix('diff', reshape(diff, [n_atoms, 1]))
860 end if
861 end subroutine
862
863 subroutine test_mbd_ewald_deriv_impl_alpha()
864 real(dp) :: delta, k_point(3)
865 type(damping_t) :: damp
866 real(dp), allocatable :: coords(:, :), gradients(:), &
867 gradients_anl(:), diff(:), alpha_0(:), alpha_0_diff(:), omega(:)
868 type(result_t) :: res(-3:3)
869 integer :: i_atom, n_atoms, i_step
870
871 delta = 0.1d0
872 n_atoms = 2
873 allocate (coords(3, n_atoms), source=0d0)
874 allocate (gradients(n_atoms))
875 coords(3, 1) = 1d0
876 coords(1, 2) = 1d0
877 coords(2, 2) = 4d0
878 geom%coords = coords
879 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
880 k_point = [0.4d0, 0d0, 0d0]
881 call geom%init()
882 damp%version = 'fermi,dip'
883 damp%r_vdw = [3.55d0, 3.5d0]
884 damp%beta = 0.83d0
885 alpha_0 = [11d0, 10d0]
886 omega = [.7d0, .65d0]
887 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
888 grad_request_t(dalpha=.true.), k_point)
889 gradients_anl = res(0)%dE%dalpha
890 do i_atom = 1, n_atoms
891 do i_step = -3, 3
892 if (i_step == 0) cycle
893 alpha_0_diff = alpha_0
894 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
895 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0_diff, omega, damp, &
896 grad_request_t(), k_point)
897 end do
898 gradients(i_atom) = diff7(res%energy, delta)
899 end do
900 call geom%destroy()
901 diff = (gradients - gradients_anl) / gradients_anl
902 if (failed(maxval(abs(diff)), 1d-6)) then
903 call print_matrix('diff', reshape(diff, [n_atoms, 1]))
904 call print_matrix('anl', reshape(gradients_anl, [n_atoms, 1]))
905 call print_matrix('num', reshape(gradients, [n_atoms, 1]))
906 end if
907 end subroutine
908
909 subroutine test_mbd_deriv_impl_omega()
910 real(dp) :: delta
911 type(damping_t) :: damp
912 real(dp), allocatable :: coords(:, :), gradients(:), &
913 gradients_anl(:), diff(:), alpha_0(:), omega_diff(:), omega(:)
914 type(result_t) :: res(-3:3)
915 integer :: i_atom, n_atoms, i_step
916
917 delta = 0.03d0
918 n_atoms = 3
919 allocate (coords(3, n_atoms), source=0d0)
920 allocate (gradients(n_atoms))
921 coords(1, 3) = 1d0
922 coords(2, 1) = 4d0
923 coords(3, 2) = 4d0
924 geom%coords = coords
925 call geom%init()
926 damp%version = 'fermi,dip'
927 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
928 damp%beta = 0.83d0
929 alpha_0 = [11d0, 10d0, 12d0]
930 omega = [.7d0, .65d0, .75d0]
931 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
932 grad_request_t(domega=.true.))
933 gradients_anl = res(0)%dE%domega
934 do i_atom = 1, n_atoms
935 do i_step = -3, 3
936 if (i_step == 0) cycle
937 omega_diff = omega
938 omega_diff(i_atom) = omega_diff(i_atom) + i_step * delta
939 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega_diff, damp, &
940 grad_request_t())
941 end do
942 gradients(i_atom) = diff7(res%energy, delta)
943 end do
944 call geom%destroy()
945 diff = (gradients - gradients_anl) / gradients_anl
946 if (failed(maxval(abs(diff)), 2d-8)) then
947 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
948 end if
949 end subroutine
950
951 subroutine test_mbd_ewald_deriv_impl_omega()
952 real(dp) :: delta, k_point(3)
953 type(damping_t) :: damp
954 real(dp), allocatable :: coords(:, :), gradients(:), &
955 gradients_anl(:), diff(:), alpha_0(:), omega_diff(:), omega(:)
956 type(result_t) :: res(-3:3)
957 integer :: i_atom, n_atoms, i_step
958
959 delta = 0.03d0
960 n_atoms = 2
961 allocate (coords(3, n_atoms), source=0d0)
962 allocate (gradients(n_atoms))
963 coords(3, 1) = 1d0
964 coords(1, 2) = 1d0
965 coords(2, 2) = 4d0
966 geom%coords = coords
967 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
968 k_point = [0.4d0, 0d0, 0d0]
969 call geom%init()
970 damp%version = 'fermi,dip'
971 damp%r_vdw = [3.55d0, 3.5d0]
972 damp%beta = 0.83d0
973 alpha_0 = [11d0, 10d0]
974 omega = [.7d0, .65d0]
975 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
976 grad_request_t(domega=.true.), k_point)
977 gradients_anl = res(0)%dE%domega
978 do i_atom = 1, n_atoms
979 do i_step = -3, 3
980 if (i_step == 0) cycle
981 omega_diff = omega
982 omega_diff(i_atom) = omega_diff(i_atom) + i_step * delta
983 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega_diff, damp, &
984 grad_request_t(), k_point)
985 end do
986 gradients(i_atom) = diff7(res%energy, delta)
987 end do
988 call geom%destroy()
989 diff = (gradients - gradients_anl) / gradients_anl
990 if (failed(maxval(abs(diff)), 2d-8)) then
991 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
992 end if
993 end subroutine
994
995 subroutine test_mbd_deriv_impl_vdw()
996 real(dp) :: delta
997 type(damping_t) :: damp
998 real(dp), allocatable :: coords(:, :), gradients(:), &
999 gradients_anl(:), diff(:), alpha_0(:), omega(:), r_vdw(:)
1000 type(result_t) :: res(-3:3)
1001 integer :: i_atom, n_atoms, i_step
1002
1003 delta = 1d-3
1004 n_atoms = 3
1005 allocate (coords(3, n_atoms), source=0d0)
1006 allocate (gradients(n_atoms))
1007 coords(2, 1) = 4d0
1008 coords(3, 2) = 4d0
1009 geom%coords = coords
1010 call geom%init()
1011 damp%version = 'fermi,dip'
1012 r_vdw = [3.55d0, 3.5d0, 3.56d0]
1013 damp%r_vdw = r_vdw
1014 damp%beta = 0.83d0
1015 alpha_0 = [11d0, 10d0, 12d0]
1016 omega = [.7d0, .65d0, .75d0]
1017 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
1018 grad_request_t(dr_vdw=.true.))
1019 gradients_anl = res(0)%dE%dr_vdw
1020 do i_atom = 1, n_atoms
1021 do i_step = -3, 3
1022 if (i_step == 0) cycle
1023 damp%r_vdw = r_vdw
1024 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
1025 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
1026 grad_request_t())
1027 end do
1028 gradients(i_atom) = diff7(res%energy, delta)
1029 end do
1030 call geom%destroy()
1031 diff = (gradients - gradients_anl) / gradients_anl
1032 if (failed(maxval(abs(diff)), 1d-8)) then
1033 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1034 end if
1035 end subroutine
1036
1037 subroutine test_mbd_ewald_deriv_impl_vdw()
1038 real(dp) :: delta, k_point(3)
1039 type(damping_t) :: damp
1040 real(dp), allocatable :: coords(:, :), gradients(:), &
1041 gradients_anl(:), diff(:), alpha_0(:), omega(:), r_vdw(:)
1042 type(result_t) :: res(-3:3)
1043 integer :: i_atom, n_atoms, i_step
1044
1045 delta = 1d-3
1046 n_atoms = 2
1047 allocate (coords(3, n_atoms), source=0d0)
1048 allocate (gradients(n_atoms))
1049 coords(3, 1) = 1d0
1050 coords(1, 2) = 1d0
1051 coords(2, 2) = 4d0
1052 geom%coords = coords
1053 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1054 k_point = [0.4d0, 0d0, 0d0]
1055 call geom%init()
1056 damp%version = 'fermi,dip'
1057 r_vdw = [3.55d0, 3.5d0]
1058 damp%r_vdw = r_vdw
1059 damp%beta = 0.83d0
1060 alpha_0 = [11d0, 10d0]
1061 omega = [.7d0, .65d0]
1062 res(0) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
1063 grad_request_t(dr_vdw=.true.), k_point)
1064 gradients_anl = res(0)%dE%dr_vdw
1065 do i_atom = 1, n_atoms
1066 do i_step = -3, 3
1067 if (i_step == 0) cycle
1068 damp%r_vdw = r_vdw
1069 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
1070 res(i_step) = get_mbd_hamiltonian_energy(geom, alpha_0, omega, damp, &
1071 grad_request_t(), k_point)
1072 end do
1073 gradients(i_atom) = diff7(res%energy, delta)
1074 end do
1075 call geom%destroy()
1076 diff = (gradients - gradients_anl) / gradients_anl
1077 if (failed(maxval(abs(diff)), 1d-8)) then
1078 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1079 call print_matrix('gradients anl', reshape(gradients_anl, [n_atoms, 1]))
1080 call print_matrix('gradients num', reshape(gradients, [n_atoms, 1]))
1081 end if
1082 end subroutine
1083
1084 subroutine test_mbd_rsscs_deriv_expl()
1085 real(dp) :: delta
1086 type(damping_t) :: damp
1087 real(dp), allocatable :: coords(:, :)
1088 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :)
1089 real(dp), allocatable :: diff(:, :)
1090 real(dp), allocatable :: alpha_0(:)
1091 real(dp), allocatable :: C6(:)
1092 type(result_t) :: res(-3:3)
1093 integer :: i_atom, n_atoms, i_xyz, i_step
1094
1095 delta = 0.01d0
1096 n_atoms = 3
1097 allocate (coords(3, n_atoms), source=0d0)
1098 allocate (gradients(n_atoms, 3))
1099 coords(2, 1) = 4d0
1100 coords(3, 2) = 4d0
1101 coords(1, 3) = 1d0
1102 geom%coords = coords
1103 call geom%init()
1104 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1105 damp%beta = 0.83d0
1106 alpha_0 = [11d0, 10d0, 12d0]
1107 C6 = [65d0, 60d0, 70d0]
1108 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1109 grad_request_t(dcoords=.true.))
1110 gradients_anl = res(0)%dE%dcoords
1111 do i_atom = 1, n_atoms
1112 do i_xyz = 1, 3
1113 do i_step = -3, 3
1114 if (i_step == 0) cycle
1115 geom%coords = coords
1116 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + &
1117 i_step * delta
1118 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1119 grad_request_t())
1120 end do
1121 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
1122 end do
1123 end do
1124 call geom%destroy()
1125 diff = (gradients - gradients_anl) / gradients_anl
1126 if (failed(maxval(abs(diff)), 1d-8)) then
1127 call print_matrix('delta gradients', diff)
1128 end if
1129 end subroutine
1130
1131 subroutine test_mbd_rsscs_ewald_deriv_expl()
1132 real(dp) :: delta
1133 type(damping_t) :: damp
1134 real(dp), allocatable :: coords(:, :)
1135 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :)
1136 real(dp), allocatable :: diff(:, :)
1137 real(dp), allocatable :: alpha_0(:)
1138 real(dp), allocatable :: C6(:)
1139 type(result_t) :: res(-3:3)
1140 integer :: i_atom, n_atoms, i_xyz, i_step
1141
1142 delta = 0.01d0
1143 n_atoms = 2
1144 allocate (coords(3, n_atoms), source=0d0)
1145 allocate (gradients(n_atoms, 3))
1146 coords(3, 1) = 1d0
1147 coords(1, 2) = 1d0
1148 coords(2, 2) = 4d0
1149 geom%coords = coords
1150 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1151 geom%k_grid = [2, 2, 2]
1152 call geom%init()
1153 damp%version = 'fermi,dip'
1154 damp%r_vdw = [3.55d0, 3.5d0]
1155 damp%beta = 0.83d0
1156 alpha_0 = [11d0, 10d0]
1157 C6 = [65d0, 60d0]
1158 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1159 grad_request_t(dcoords=.true.))
1160 gradients_anl = res(0)%dE%dcoords
1161 do i_atom = 1, n_atoms
1162 do i_xyz = 1, 3
1163 do i_step = -3, 3
1164 if (i_step == 0) cycle
1165 geom%coords = coords
1166 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + &
1167 i_step * delta
1168 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1169 grad_request_t())
1170 end do
1171 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
1172 end do
1173 end do
1174 call geom%destroy()
1175 diff = (gradients - gradients_anl) / gradients_anl
1176 if (failed(maxval(abs(diff)), 1d-8)) then
1177 call print_matrix('delta gradients', diff)
1178 end if
1179 end subroutine
1180
1181 subroutine test_mbd_rsscs_ewald_deriv_stress()
1182 real(dp) :: delta
1183 type(damping_t) :: damp
1184 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :), &
1185 diff(:, :), alpha_0(:), C6(:), lattice(:, :)
1186 type(result_t) :: res(-3:3)
1187 integer :: n_atoms, i_xyz, i_step, i_latt
1188
1189 delta = 0.01d0
1190 n_atoms = 2
1191 allocate (geom%coords(3, n_atoms), source=0d0)
1192 allocate (gradients(3, 3))
1193 geom%coords(3, 1) = 1d0
1194 geom%coords(1, 2) = 1d0
1195 geom%coords(2, 2) = 4d0
1196 lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1197 geom%lattice = lattice
1198 geom%k_grid = [2, 2, 2]
1199 call geom%init()
1200 damp%version = 'fermi,dip'
1201 damp%r_vdw = [3.55d0, 3.5d0]
1202 damp%beta = 0.83d0
1203 alpha_0 = [11d0, 10d0]
1204 C6 = [65d0, 60d0]
1205 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1206 grad_request_t(dlattice=.true.))
1207 gradients_anl = res(0)%dE%dlattice
1208 do i_latt = 1, 3
1209 do i_xyz = 1, 3
1210 do i_step = -3, 3
1211 if (i_step == 0) cycle
1212 geom%lattice = lattice
1213 geom%lattice(i_xyz, i_latt) = geom%lattice(i_xyz, i_latt) + i_step * delta
1214 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1215 grad_request_t())
1216 end do
1217 gradients(i_latt, i_xyz) = diff7(res%energy, delta)
1218 end do
1219 end do
1220 call geom%destroy()
1221 diff = (gradients - gradients_anl) / gradients_anl
1222 if (failed(maxval(abs(diff)), 3d-6)) then
1223 call print_matrix('delta gradients', diff)
1224 call print_matrix('gradients anl', gradients_anl)
1225 call print_matrix('gradients num', gradients)
1226 end if
1227 end subroutine
1228
1229 subroutine test_mbd_rsscs_deriv_impl_alpha()
1230 real(dp) :: delta
1231 type(damping_t) :: damp
1232 real(dp), allocatable :: coords(:, :), gradients(:), &
1233 gradients_anl(:), diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
1234 type(result_t) :: res(-3:3)
1235 integer :: i_atom, n_atoms, i_step
1236
1237 delta = 3d-2
1238 n_atoms = 3
1239 allocate (coords(3, n_atoms), source=0d0)
1240 allocate (gradients(n_atoms))
1241 coords(1, 3) = 1d0
1242 coords(2, 1) = 4d0
1243 coords(3, 2) = 4d0
1244 geom%coords = coords
1245 call geom%init()
1246 damp%version = 'fermi,dip'
1247 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1248 damp%beta = 0.83d0
1249 alpha_0 = [11d0, 10d0, 12d0]
1250 C6 = [65d0, 60d0, 70d0]
1251 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1252 grad_request_t(dalpha=.true.))
1253 gradients_anl = res(0)%dE%dalpha
1254 do i_atom = 1, n_atoms
1255 do i_step = -3, 3
1256 if (i_step == 0) cycle
1257 alpha_0_diff = alpha_0
1258 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
1259 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0_diff, C6, damp, &
1260 grad_request_t())
1261 end do
1262 gradients(i_atom) = diff7(res%energy, delta)
1263 end do
1264 call geom%destroy()
1265 diff = (gradients - gradients_anl) / gradients_anl
1266 if (failed(maxval(abs(diff)), 1d-7)) then
1267 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1268 end if
1269 end subroutine
1270
1271 subroutine test_mbd_rsscs_deriv_impl_C6()
1272 real(dp) :: delta
1273 type(damping_t) :: damp
1274 real(dp), allocatable :: coords(:, :), gradients(:), &
1275 gradients_anl(:), diff(:), alpha_0(:), C6_diff(:), C6(:)
1276 type(result_t) :: res(-3:3)
1277 integer :: i_atom, n_atoms, i_step
1278
1279 delta = 0.01d0
1280 n_atoms = 3
1281 allocate (coords(3, n_atoms), source=0d0)
1282 allocate (gradients(n_atoms))
1283 coords(1, 3) = 1d0
1284 coords(2, 1) = 4d0
1285 coords(3, 2) = 4d0
1286 geom%coords = coords
1287 call geom%init()
1288 damp%version = 'fermi,dip'
1289 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1290 damp%beta = 0.83d0
1291 alpha_0 = [11d0, 10d0, 12d0]
1292 C6 = [65d0, 60d0, 70d0]
1293 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1294 grad_request_t(dC6=.true.))
1295 gradients_anl = res(0)%dE%dC6
1296 do i_atom = 1, n_atoms
1297 do i_step = -3, 3
1298 if (i_step == 0) cycle
1299 C6_diff = C6
1300 C6_diff(i_atom) = C6_diff(i_atom) + i_step * delta
1301 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6_diff, damp, &
1302 grad_request_t())
1303 end do
1304 gradients(i_atom) = diff7(res%energy, delta)
1305 end do
1306 call geom%destroy()
1307 diff = (gradients - gradients_anl) / gradients_anl
1308 if (failed(maxval(abs(diff)), 5d-8)) then
1309 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1310 end if
1311 end subroutine
1312
1313 subroutine test_mbd_rsscs_deriv_impl_vdw()
1314 real(dp) :: delta
1315 type(damping_t) :: damp
1316 real(dp), allocatable :: coords(:, :), gradients(:), &
1317 gradients_anl(:), diff(:), alpha_0(:), C6(:), r_vdw(:)
1318 type(result_t) :: res(-3:3)
1319 integer :: i_atom, n_atoms, i_step
1320
1321 delta = 1d-2
1322 n_atoms = 3
1323 allocate (coords(3, n_atoms), source=0d0)
1324 allocate (gradients(n_atoms))
1325 coords(2, 1) = 4d0
1326 coords(3, 2) = 4d0
1327 geom%coords = coords
1328 call geom%init()
1329 damp%version = 'fermi,dip'
1330 r_vdw = [3.55d0, 3.5d0, 3.56d0]
1331 damp%r_vdw = r_vdw
1332 damp%beta = 0.83d0
1333 alpha_0 = [11d0, 10d0, 12d0]
1334 C6 = [65d0, 60d0, 70d0]
1335 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1336 grad_request_t(dr_vdw=.true.))
1337 gradients_anl = res(0)%dE%dr_vdw
1338 do i_atom = 1, n_atoms
1339 do i_step = -3, 3
1340 if (i_step == 0) cycle
1341 damp%r_vdw = r_vdw
1342 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
1343 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1344 grad_request_t())
1345 end do
1346 gradients(i_atom) = diff7(res%energy, delta)
1347 end do
1348 call geom%destroy()
1349 diff = (gradients - gradients_anl) / gradients_anl
1350 if (failed(maxval(abs(diff)), 1d-8)) then
1351 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1352 end if
1353 end subroutine
1354
1355 subroutine test_mbd_rsscs_ewald_deriv_impl_alpha()
1356 real(dp) :: delta
1357 type(damping_t) :: damp
1358 real(dp), allocatable :: coords(:, :), gradients(:), &
1359 gradients_anl(:), diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
1360 type(result_t) :: res(-3:3)
1361 integer :: i_atom, n_atoms, i_step
1362
1363 delta = 3d-2
1364 n_atoms = 2
1365 allocate (coords(3, n_atoms), source=0d0)
1366 allocate (gradients(n_atoms))
1367 coords(3, 1) = 1d0
1368 coords(1, 2) = 1d0
1369 coords(2, 2) = 4d0
1370 geom%coords = coords
1371 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1372 geom%k_grid = [2, 2, 2]
1373 call geom%init()
1374 damp%version = 'fermi,dip'
1375 damp%r_vdw = [3.55d0, 3.5d0]
1376 damp%beta = 0.83d0
1377 alpha_0 = [11d0, 10d0]
1378 C6 = [65d0, 60d0]
1379 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1380 grad_request_t(dalpha=.true.))
1381 gradients_anl = res(0)%dE%dalpha
1382 do i_atom = 1, n_atoms
1383 do i_step = -3, 3
1384 if (i_step == 0) cycle
1385 alpha_0_diff = alpha_0
1386 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
1387 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0_diff, C6, damp, &
1388 grad_request_t())
1389 end do
1390 gradients(i_atom) = diff7(res%energy, delta)
1391 end do
1392 call geom%destroy()
1393 diff = (gradients - gradients_anl) / gradients_anl
1394 if (failed(maxval(abs(diff)), 1d-7)) then
1395 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1396 end if
1397 end subroutine
1398
1399 subroutine test_mbd_rsscs_ewald_deriv_impl_C6()
1400 real(dp) :: delta
1401 type(damping_t) :: damp
1402 real(dp), allocatable :: coords(:, :), gradients(:), &
1403 gradients_anl(:), diff(:), alpha_0(:), C6_diff(:), C6(:)
1404 type(result_t) :: res(-3:3)
1405 integer :: i_atom, n_atoms, i_step
1406
1407 delta = 0.01d0
1408 n_atoms = 2
1409 allocate (coords(3, n_atoms), source=0d0)
1410 allocate (gradients(n_atoms))
1411 coords(3, 1) = 1d0
1412 coords(1, 2) = 1d0
1413 coords(2, 2) = 4d0
1414 geom%coords = coords
1415 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1416 geom%k_grid = [2, 2, 2]
1417 call geom%init()
1418 damp%version = 'fermi,dip'
1419 damp%r_vdw = [3.55d0, 3.5d0]
1420 damp%beta = 0.83d0
1421 alpha_0 = [11d0, 10d0]
1422 C6 = [65d0, 60d0]
1423 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1424 grad_request_t(dC6=.true.))
1425 gradients_anl = res(0)%dE%dC6
1426 do i_atom = 1, n_atoms
1427 do i_step = -3, 3
1428 if (i_step == 0) cycle
1429 C6_diff = C6
1430 C6_diff(i_atom) = C6_diff(i_atom) + i_step * delta
1431 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6_diff, damp, &
1432 grad_request_t())
1433 end do
1434 gradients(i_atom) = diff7(res%energy, delta)
1435 end do
1436 call geom%destroy()
1437 diff = (gradients - gradients_anl) / gradients_anl
1438 if (failed(maxval(abs(diff)), 5d-8)) then
1439 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1440 end if
1441 end subroutine
1442
1443 subroutine test_mbd_rsscs_ewald_deriv_impl_vdw()
1444 real(dp) :: delta
1445 type(damping_t) :: damp
1446 real(dp), allocatable :: coords(:, :), gradients(:), &
1447 gradients_anl(:), diff(:), alpha_0(:), C6(:), r_vdw(:)
1448 type(result_t) :: res(-3:3)
1449 integer :: i_atom, n_atoms, i_step
1450
1451 delta = 1d-2
1452 n_atoms = 2
1453 allocate (coords(3, n_atoms), source=0d0)
1454 allocate (gradients(n_atoms))
1455 coords(3, 1) = 1d0
1456 coords(1, 2) = 1d0
1457 coords(2, 2) = 4d0
1458 geom%coords = coords
1459 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1460 geom%k_grid = [2, 2, 2]
1461 call geom%init()
1462 damp%version = 'fermi,dip'
1463 r_vdw = [3.55d0, 3.5d0]
1464 damp%r_vdw = r_vdw
1465 damp%beta = 0.83d0
1466 alpha_0 = [11d0, 10d0]
1467 C6 = [65d0, 60d0]
1468 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1469 grad_request_t(dr_vdw=.true.))
1470 gradients_anl = res(0)%dE%dr_vdw
1471 do i_atom = 1, n_atoms
1472 do i_step = -3, 3
1473 if (i_step == 0) cycle
1474 damp%r_vdw = r_vdw
1475 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
1476 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
1477 grad_request_t())
1478 end do
1479 gradients(i_atom) = diff7(res%energy, delta)
1480 end do
1481 call geom%destroy()
1482 diff = (gradients - gradients_anl) / gradients_anl
1483 if (failed(maxval(abs(diff)), 1d-8)) then
1484 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1485 end if
1486 end subroutine
1487
1488 subroutine test_ts_deriv_expl()
1489 real(dp) :: delta
1490 type(damping_t) :: damp
1491 real(dp), allocatable :: coords(:, :)
1492 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :)
1493 real(dp), allocatable :: diff(:, :)
1494 real(dp), allocatable :: alpha_0(:)
1495 real(dp), allocatable :: C6(:)
1496 type(result_t) :: res(-3:3)
1497 integer :: i_atom, n_atoms, i_xyz, i_step
1498
1499 delta = 0.01d0
1500 n_atoms = 3
1501 allocate (coords(3, n_atoms), source=0d0)
1502 allocate (gradients(n_atoms, 3))
1503 coords(2, 1) = 4d0
1504 coords(3, 2) = 4d0
1505 coords(1, 3) = 1d0
1506 geom%coords = coords
1507 call geom%init()
1508 damp%version = 'fermi'
1509 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1510 damp%ts_sr = 0.94d0
1511 alpha_0 = [11d0, 10d0, 12d0]
1512 C6 = [65d0, 60d0, 70d0]
1513 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dcoords=.true.))
1514 gradients_anl = res(0)%dE%dcoords
1515 do i_atom = 1, n_atoms
1516 do i_xyz = 1, 3
1517 do i_step = -3, 3
1518 if (i_step == 0) cycle
1519 geom%coords = coords
1520 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + &
1521 i_step * delta
1522 res(i_step) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t())
1523 end do
1524 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
1525 end do
1526 end do
1527 call geom%destroy()
1528 diff = (gradients - gradients_anl) / gradients_anl
1529 if (failed(maxval(abs(diff)), 1d-8)) then
1530 call print_matrix('gradients', gradients)
1531 call print_matrix('gradients_anl', gradients_anl)
1532 call print_matrix('delta gradients', diff)
1533 end if
1534 end subroutine
1535
1536 subroutine test_ts_ewald_deriv_expl()
1537 real(dp) :: delta
1538 type(damping_t) :: damp
1539 real(dp), allocatable :: coords(:, :)
1540 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :)
1541 real(dp), allocatable :: diff(:, :)
1542 real(dp), allocatable :: alpha_0(:)
1543 real(dp), allocatable :: C6(:)
1544 type(result_t) :: res(-3:3)
1545 integer :: i_atom, n_atoms, i_xyz, i_step
1546
1547 delta = 0.01d0
1548 n_atoms = 2
1549 allocate (coords(3, n_atoms), source=0d0)
1550 allocate (gradients(n_atoms, 3))
1551 coords(3, 1) = 1d0
1552 coords(1, 2) = 1d0
1553 coords(2, 2) = 4d0
1554 geom%coords = coords
1555 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1556 geom%k_grid = [2, 2, 2]
1557 call geom%init()
1558 damp%version = 'fermi'
1559 damp%r_vdw = [3.55d0, 3.5d0]
1560 damp%ts_sr = 0.94d0
1561 alpha_0 = [11d0, 10d0]
1562 C6 = [65d0, 60d0]
1563 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dcoords=.true.))
1564 gradients_anl = res(0)%dE%dcoords
1565 do i_atom = 1, n_atoms
1566 do i_xyz = 1, 3
1567 do i_step = -3, 3
1568 if (i_step == 0) cycle
1569 geom%coords = coords
1570 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + &
1571 i_step * delta
1572 res(i_step) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t())
1573 end do
1574 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
1575 end do
1576 end do
1577 call geom%destroy()
1578 diff = (gradients - gradients_anl) / gradients_anl
1579 if (failed(maxval(abs(diff)), 1d-8)) then
1580 call print_matrix('delta gradients', diff)
1581 end if
1582 end subroutine
1583
1584 subroutine test_ts_ewald_deriv_stress()
1585 real(dp) :: delta
1586 type(damping_t) :: damp
1587 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :), &
1588 diff(:, :), alpha_0(:), C6(:), lattice(:, :)
1589 type(result_t) :: res(-3:3)
1590 integer :: n_atoms, i_xyz, i_step, i_latt
1591
1592 delta = 0.01d0
1593 n_atoms = 2
1594 allocate (geom%coords(3, n_atoms), source=0d0)
1595 allocate (gradients(3, 3))
1596 geom%coords(3, 1) = 1d0
1597 geom%coords(1, 2) = 1d0
1598 geom%coords(2, 2) = 4d0
1599 lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1600 geom%lattice = lattice
1601 geom%k_grid = [2, 2, 2]
1602 call geom%init()
1603 damp%version = 'fermi'
1604 damp%r_vdw = [3.55d0, 3.5d0]
1605 damp%ts_sr = 0.94d0
1606 alpha_0 = [11d0, 10d0]
1607 C6 = [65d0, 60d0]
1608 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dlattice=.true.))
1609 gradients_anl = res(0)%dE%dlattice
1610 do i_latt = 1, 3
1611 do i_xyz = 1, 3
1612 do i_step = -3, 3
1613 if (i_step == 0) cycle
1614 geom%lattice = lattice
1615 geom%lattice(i_xyz, i_latt) = geom%lattice(i_xyz, i_latt) + i_step * delta
1616 res(i_step) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t())
1617 end do
1618 gradients(i_latt, i_xyz) = diff7(res%energy, delta)
1619 end do
1620 end do
1621 call geom%destroy()
1622 diff = (gradients - gradients_anl) / gradients_anl
1623 if (failed(maxval(abs(diff)), 3d-6)) then
1624 call print_matrix('delta gradients', diff)
1625 call print_matrix('gradients anl', gradients_anl)
1626 call print_matrix('gradients num', gradients)
1627 end if
1628 end subroutine
1629
1630 subroutine test_ts_deriv_impl_alpha()
1631 real(dp) :: delta
1632 type(damping_t) :: damp
1633 real(dp), allocatable :: coords(:, :), gradients(:), &
1634 gradients_anl(:), diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
1635 type(result_t) :: res(-3:3)
1636 integer :: i_atom, n_atoms, i_step
1637
1638 delta = 3d-2
1639 n_atoms = 3
1640 allocate (coords(3, n_atoms), source=0d0)
1641 allocate (gradients(n_atoms))
1642 coords(1, 3) = 1d0
1643 coords(2, 1) = 4d0
1644 coords(3, 2) = 4d0
1645 geom%coords = coords
1646 call geom%init()
1647 damp%version = 'fermi'
1648 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1649 damp%ts_sr = 0.94d0
1650 alpha_0 = [11d0, 10d0, 12d0]
1651 C6 = [65d0, 60d0, 70d0]
1652 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dalpha=.true.))
1653 gradients_anl = res(0)%dE%dalpha
1654 do i_atom = 1, n_atoms
1655 do i_step = -3, 3
1656 if (i_step == 0) cycle
1657 alpha_0_diff = alpha_0
1658 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
1659 res(i_step) = get_ts_energy(geom, alpha_0_diff, C6, damp, grad_request_t())
1660 end do
1661 gradients(i_atom) = diff7(res%energy, delta)
1662 end do
1663 call geom%destroy()
1664 diff = (gradients - gradients_anl) / gradients_anl
1665 if (failed(maxval(abs(diff)), 1d-7)) then
1666 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1667 end if
1668 end subroutine
1669
1670 subroutine test_ts_deriv_impl_C6()
1671 real(dp) :: delta
1672 type(damping_t) :: damp
1673 real(dp), allocatable :: coords(:, :), gradients(:), &
1674 gradients_anl(:), diff(:), alpha_0(:), C6_diff(:), C6(:)
1675 type(result_t) :: res(-3:3)
1676 integer :: i_atom, n_atoms, i_step
1677
1678 delta = 0.01d0
1679 n_atoms = 3
1680 allocate (coords(3, n_atoms), source=0d0)
1681 allocate (gradients(n_atoms))
1682 coords(1, 3) = 1d0
1683 coords(2, 1) = 4d0
1684 coords(3, 2) = 4d0
1685 geom%coords = coords
1686 call geom%init()
1687 damp%version = 'fermi'
1688 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1689 damp%ts_sr = 0.94d0
1690 alpha_0 = [11d0, 10d0, 12d0]
1691 C6 = [65d0, 60d0, 70d0]
1692 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dC6=.true.))
1693 gradients_anl = res(0)%dE%dC6
1694 do i_atom = 1, n_atoms
1695 do i_step = -3, 3
1696 if (i_step == 0) cycle
1697 C6_diff = C6
1698 C6_diff(i_atom) = C6_diff(i_atom) + i_step * delta
1699 res(i_step) = get_ts_energy(geom, alpha_0, C6_diff, damp, grad_request_t())
1700 end do
1701 gradients(i_atom) = diff7(res%energy, delta)
1702 end do
1703 call geom%destroy()
1704 diff = (gradients - gradients_anl) / gradients_anl
1705 if (failed(maxval(abs(diff)), 5d-8)) then
1706 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1707 end if
1708 end subroutine
1709
1710 subroutine test_ts_deriv_impl_vdw()
1711 real(dp) :: delta
1712 type(damping_t) :: damp
1713 real(dp), allocatable :: coords(:, :), gradients(:), &
1714 gradients_anl(:), diff(:), alpha_0(:), C6(:), r_vdw(:)
1715 type(result_t) :: res(-3:3)
1716 integer :: i_atom, n_atoms, i_step
1717
1718 delta = 1d-2
1719 n_atoms = 3
1720 allocate (coords(3, n_atoms), source=0d0)
1721 allocate (gradients(n_atoms))
1722 coords(2, 1) = 4d0
1723 coords(3, 2) = 4d0
1724 geom%coords = coords
1725 call geom%init()
1726 damp%version = 'fermi'
1727 r_vdw = [3.55d0, 3.5d0, 3.56d0]
1728 damp%r_vdw = r_vdw
1729 damp%ts_sr = 0.94d0
1730 alpha_0 = [11d0, 10d0, 12d0]
1731 C6 = [65d0, 60d0, 70d0]
1732 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dr_vdw=.true.))
1733 gradients_anl = res(0)%dE%dr_vdw
1734 do i_atom = 1, n_atoms
1735 do i_step = -3, 3
1736 if (i_step == 0) cycle
1737 damp%r_vdw = r_vdw
1738 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
1739 res(i_step) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t())
1740 end do
1741 gradients(i_atom) = diff7(res%energy, delta)
1742 end do
1743 call geom%destroy()
1744 diff = (gradients - gradients_anl) / gradients_anl
1745 if (failed(maxval(abs(diff)), 1d-8)) then
1746 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1747 end if
1748 end subroutine
1749
1750 subroutine test_ts_ewald_deriv_impl_alpha()
1751 real(dp) :: delta
1752 type(damping_t) :: damp
1753 real(dp), allocatable :: coords(:, :), gradients(:), &
1754 gradients_anl(:), diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
1755 type(result_t) :: res(-3:3)
1756 integer :: i_atom, n_atoms, i_step
1757
1758 delta = 3d-2
1759 n_atoms = 2
1760 allocate (coords(3, n_atoms), source=0d0)
1761 allocate (gradients(n_atoms))
1762 coords(3, 1) = 1d0
1763 coords(1, 2) = 1d0
1764 coords(2, 2) = 4d0
1765 geom%coords = coords
1766 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1767 geom%k_grid = [2, 2, 2]
1768 call geom%init()
1769 damp%version = 'fermi'
1770 damp%r_vdw = [3.55d0, 3.5d0]
1771 damp%ts_sr = 0.94d0
1772 alpha_0 = [11d0, 10d0]
1773 C6 = [65d0, 60d0]
1774 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dalpha=.true.))
1775 gradients_anl = res(0)%dE%dalpha
1776 do i_atom = 1, n_atoms
1777 do i_step = -3, 3
1778 if (i_step == 0) cycle
1779 alpha_0_diff = alpha_0
1780 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
1781 res(i_step) = get_ts_energy(geom, alpha_0_diff, C6, damp, grad_request_t())
1782 end do
1783 gradients(i_atom) = diff7(res%energy, delta)
1784 end do
1785 call geom%destroy()
1786 diff = (gradients - gradients_anl) / gradients_anl
1787 if (failed(maxval(abs(diff)), 1d-7)) then
1788 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1789 end if
1790 end subroutine
1791
1792 subroutine test_ts_ewald_deriv_impl_C6()
1793 real(dp) :: delta
1794 type(damping_t) :: damp
1795 real(dp), allocatable :: coords(:, :), gradients(:), &
1796 gradients_anl(:), diff(:), alpha_0(:), C6_diff(:), C6(:)
1797 type(result_t) :: res(-3:3)
1798 integer :: i_atom, n_atoms, i_step
1799
1800 delta = 0.01d0
1801 n_atoms = 2
1802 allocate (coords(3, n_atoms), source=0d0)
1803 allocate (gradients(n_atoms))
1804 coords(3, 1) = 1d0
1805 coords(1, 2) = 1d0
1806 coords(2, 2) = 4d0
1807 geom%coords = coords
1808 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1809 geom%k_grid = [2, 2, 2]
1810 call geom%init()
1811 damp%version = 'fermi'
1812 damp%r_vdw = [3.55d0, 3.5d0]
1813 damp%ts_sr = 0.94d0
1814 alpha_0 = [11d0, 10d0]
1815 C6 = [65d0, 60d0]
1816 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dC6=.true.))
1817 gradients_anl = res(0)%dE%dC6
1818 do i_atom = 1, n_atoms
1819 do i_step = -3, 3
1820 if (i_step == 0) cycle
1821 C6_diff = C6
1822 C6_diff(i_atom) = C6_diff(i_atom) + i_step * delta
1823 res(i_step) = get_ts_energy(geom, alpha_0, C6_diff, damp, grad_request_t())
1824 end do
1825 gradients(i_atom) = diff7(res%energy, delta)
1826 end do
1827 call geom%destroy()
1828 diff = (gradients - gradients_anl) / gradients_anl
1829 if (failed(maxval(abs(diff)), 5d-8)) then
1830 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1831 end if
1832 end subroutine
1833
1834 subroutine test_ts_ewald_deriv_impl_vdw()
1835 real(dp) :: delta
1836 type(damping_t) :: damp
1837 real(dp), allocatable :: coords(:, :), gradients(:), &
1838 gradients_anl(:), diff(:), alpha_0(:), C6(:), r_vdw(:)
1839 type(result_t) :: res(-3:3)
1840 integer :: i_atom, n_atoms, i_step
1841
1842 delta = 1d-2
1843 n_atoms = 2
1844 allocate (coords(3, n_atoms), source=0d0)
1845 allocate (gradients(n_atoms))
1846 coords(3, 1) = 1d0
1847 coords(1, 2) = 1d0
1848 coords(2, 2) = 4d0
1849 geom%coords = coords
1850 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
1851 geom%k_grid = [2, 2, 2]
1852 call geom%init()
1853 damp%version = 'fermi'
1854 r_vdw = [3.55d0, 3.5d0]
1855 damp%r_vdw = r_vdw
1856 damp%ts_sr = 0.94d0
1857 alpha_0 = [11d0, 10d0]
1858 C6 = [65d0, 60d0]
1859 res(0) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t(dr_vdw=.true.))
1860 gradients_anl = res(0)%dE%dr_vdw
1861 do i_atom = 1, n_atoms
1862 do i_step = -3, 3
1863 if (i_step == 0) cycle
1864 damp%r_vdw = r_vdw
1865 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
1866 res(i_step) = get_ts_energy(geom, alpha_0, C6, damp, grad_request_t())
1867 end do
1868 gradients(i_atom) = diff7(res%energy, delta)
1869 end do
1870 call geom%destroy()
1871 diff = (gradients - gradients_anl) / gradients_anl
1872 if (failed(maxval(abs(diff)), 1d-8)) then
1873 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1874 end if
1875 end subroutine
1876
1877 subroutine test_mbd_rpa_deriv_expl()
1878 real(dp) :: delta
1879 type(damping_t) :: damp
1880 real(dp), allocatable :: coords(:, :), gradients(:, :), gradients_anl(:, :), &
1881 diff(:, :), alpha_0(:), C6(:)
1882 type(result_t) :: res(-3:3)
1883 integer :: i_atom, n_atoms, i_xyz, i_step
1884
1885 delta = 0.01d0
1886 n_atoms = 3
1887 allocate (coords(3, n_atoms), source=0d0)
1888 allocate (gradients(n_atoms, 3))
1889 coords(1, 3) = 1d0
1890 coords(2, 1) = 4d0
1891 coords(3, 2) = 4d0
1892 geom%coords = coords
1893 geom%do_rpa = .true.
1894 call geom%init()
1895 damp%version = 'fermi,dip'
1896 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1897 damp%beta = 0.83d0
1898 alpha_0 = [11d0, 10d0, 12d0]
1899 C6 = [65d0, 60d0, 70d0]
1900 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dcoords=.true.))
1901 gradients_anl = res(0)%dE%dcoords
1902 do i_atom = 1, n_atoms
1903 do i_xyz = 1, 3
1904 do i_step = -3, 3
1905 if (i_step == 0) cycle
1906 geom%coords = coords
1907 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + i_step * delta
1908 res(i_step) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t())
1909 end do
1910 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
1911 end do
1912 end do
1913 call geom%destroy()
1914 diff = (gradients - gradients_anl) / gradients_anl
1915 if (failed(maxval(abs(diff)), 1d-6)) then
1916 call print_matrix('delta gradients', diff)
1917 end if
1918 end subroutine
1919
1920 subroutine test_mbd_rpa_deriv_impl_alpha()
1921 real(dp) :: delta
1922 type(damping_t) :: damp
1923 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
1924 diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
1925 type(result_t) :: res(-3:3)
1926 integer :: i_atom, n_atoms, i_step
1927
1928 delta = 3d-2
1929 n_atoms = 3
1930 allocate (coords(3, n_atoms), source=0d0)
1931 allocate (gradients(n_atoms))
1932 coords(1, 3) = 1d0
1933 coords(2, 1) = 4d0
1934 coords(3, 2) = 4d0
1935 geom%coords = coords
1936 geom%do_rpa = .true.
1937 call geom%init()
1938 damp%version = 'fermi,dip'
1939 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1940 damp%beta = 0.83d0
1941 alpha_0 = [11d0, 10d0, 12d0]
1942 C6 = [65d0, 60d0, 70d0]
1943 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dalpha=.true.))
1944 gradients_anl = res(0)%dE%dalpha
1945 do i_atom = 1, n_atoms
1946 do i_step = -3, 3
1947 if (i_step == 0) cycle
1948 alpha_0_diff = alpha_0
1949 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
1950 res(i_step) = get_mbd_energy(geom, alpha_0_diff, C6, damp, grad_request_t())
1951 end do
1952 gradients(i_atom) = diff7(res%energy, delta)
1953 end do
1954 call geom%destroy()
1955 diff = (gradients - gradients_anl) / gradients_anl
1956 if (failed(maxval(abs(diff)), 1d-6)) then
1957 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1958 end if
1959 end subroutine
1960
1961 subroutine test_mbd_rpa_deriv_impl_C6()
1962 real(dp) :: delta
1963 type(damping_t) :: damp
1964 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
1965 diff(:), alpha_0(:), C6_diff(:), C6(:)
1966 type(result_t) :: res(-3:3)
1967 integer :: i_atom, n_atoms, i_step
1968
1969 delta = 0.01d0
1970 n_atoms = 3
1971 allocate (coords(3, n_atoms), source=0d0)
1972 allocate (gradients(n_atoms))
1973 coords(1, 3) = 1d0
1974 coords(2, 1) = 4d0
1975 coords(3, 2) = 4d0
1976 geom%coords = coords
1977 geom%do_rpa = .true.
1978 call geom%init()
1979 damp%version = 'fermi,dip'
1980 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
1981 damp%beta = 0.83d0
1982 alpha_0 = [11d0, 10d0, 12d0]
1983 C6 = [65d0, 60d0, 70d0]
1984 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dC6=.true.))
1985 gradients_anl = res(0)%dE%dC6
1986 do i_atom = 1, n_atoms
1987 do i_step = -3, 3
1988 if (i_step == 0) cycle
1989 C6_diff = C6
1990 C6_diff(i_atom) = C6_diff(i_atom) + i_step * delta
1991 res(i_step) = get_mbd_energy(geom, alpha_0, C6_diff, damp, grad_request_t())
1992 end do
1993 gradients(i_atom) = diff7(res%energy, delta)
1994 end do
1995 call geom%destroy()
1996 diff = (gradients - gradients_anl) / gradients_anl
1997 if (failed(maxval(abs(diff)), 1d-6)) then
1998 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
1999 end if
2000 end subroutine
2001
2002 subroutine test_mbd_rpa_deriv_impl_vdw()
2003 real(dp) :: delta
2004 type(damping_t) :: damp
2005 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2006 diff(:), alpha_0(:), C6(:), r_vdw(:)
2007 type(result_t) :: res(-3:3)
2008 integer :: i_atom, n_atoms, i_step
2009
2010 delta = 1d-2
2011 n_atoms = 3
2012 allocate (coords(3, n_atoms), source=0d0)
2013 allocate (gradients(n_atoms))
2014 coords(2, 1) = 4d0
2015 coords(3, 2) = 4d0
2016 coords(1, 3) = 1d0
2017 geom%coords = coords
2018 geom%do_rpa = .true.
2019 call geom%init()
2020 damp%version = 'fermi,dip'
2021 r_vdw = [3.55d0, 3.5d0, 3.56d0]
2022 damp%r_vdw = r_vdw
2023 damp%beta = 0.83d0
2024 alpha_0 = [11d0, 10d0, 12d0]
2025 C6 = [65d0, 60d0, 70d0]
2026 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dr_vdw=.true.))
2027 gradients_anl = res(0)%dE%dr_vdw
2028 do i_atom = 1, n_atoms
2029 do i_step = -3, 3
2030 if (i_step == 0) cycle
2031 damp%r_vdw = r_vdw
2032 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
2033 res(i_step) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t())
2034 end do
2035 gradients(i_atom) = diff7(res%energy, delta)
2036 end do
2037 call geom%destroy()
2038 diff = (gradients - gradients_anl) / gradients_anl
2039 if (failed(maxval(abs(diff)), 1d-8)) then
2040 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2041 end if
2042 end subroutine
2043
2044 subroutine test_mbd_rpa_gg_deriv_impl_alpha()
2045 ! exercises the derivative of the dynamic polarizability through the
2046 ! self-interaction width sigma(alpha) of the gg damping
2047 real(dp) :: delta
2048 type(damping_t) :: damp
2049 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2050 diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
2051 type(result_t) :: res(-3:3)
2052 integer :: i_atom, n_atoms, i_step
2053
2054 delta = 3d-2
2055 n_atoms = 3
2056 allocate (coords(3, n_atoms), source=0d0)
2057 allocate (gradients(n_atoms))
2058 coords(1, 3) = 1d0
2059 coords(2, 1) = 4d0
2060 coords(3, 2) = 4d0
2061 geom%coords = coords
2062 geom%do_rpa = .true.
2063 call geom%init()
2064 damp%version = 'dip,gg'
2065 alpha_0 = [11d0, 10d0, 12d0]
2066 C6 = [65d0, 60d0, 70d0]
2067 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dalpha=.true.))
2068 gradients_anl = res(0)%dE%dalpha
2069 do i_atom = 1, n_atoms
2070 do i_step = -3, 3
2071 if (i_step == 0) cycle
2072 alpha_0_diff = alpha_0
2073 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
2074 res(i_step) = get_mbd_energy(geom, alpha_0_diff, C6, damp, grad_request_t())
2075 end do
2076 gradients(i_atom) = diff7(res%energy, delta)
2077 end do
2078 call geom%destroy()
2079 diff = (gradients - gradients_anl) / gradients_anl
2080 if (failed(maxval(abs(diff)), 1d-7)) then
2081 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2082 end if
2083 end subroutine
2084
2085 subroutine test_mbd_rpa_dynalpha()
2086 ! direct FD check of get_mbd_rpa_energy w.r.t. the dynamic polarizabilities
2087 real(dp) :: delta
2088 type(damping_t) :: damp
2089 real(dp), allocatable :: coords(:, :), alpha(:, :), alpha_d(:, :), &
2090 gradients(:, :), gradients_anl(:, :), diff(:, :)
2091 type(result_t) :: res(-3:3), res0
2092 integer :: i_atom, n_atoms, i_step, i_freq, n_freq
2093
2094 delta = 1d-4
2095 n_atoms = 3
2096 allocate (coords(3, n_atoms), source=0d0)
2097 coords(1, 3) = 1d0
2098 coords(2, 1) = 4d0
2099 coords(3, 2) = 4d0
2100 geom%coords = coords
2101 geom%do_rpa = .true.
2102 call geom%init()
2103 damp%version = 'dip,gg'
2104 n_freq = ubound(geom%freq, 1)
2105 allocate (alpha(n_atoms, 0:n_freq), gradients(n_atoms, 0:2), &
2106 gradients_anl(n_atoms, 0:2))
2107 do i_freq = 0, n_freq
2108 alpha(:, i_freq) = [11d0, 10d0, 12d0] / (1 + geom%freq(i_freq)%val**2)
2109 end do
2110 res0 = get_mbd_rpa_energy(geom, alpha, damp, grad_request_t(dalpha_dyn=.true.))
2111 gradients_anl = res0%dE%dalpha_dyn(:, 1:2) ! freq 0 has zero weight
2112 do i_freq = 1, 2
2113 do i_atom = 1, n_atoms
2114 do i_step = -3, 3
2115 if (i_step == 0) cycle
2116 alpha_d = alpha
2117 alpha_d(i_atom, i_freq) = alpha_d(i_atom, i_freq) + i_step * delta
2118 res(i_step) = get_mbd_rpa_energy(geom, alpha_d, damp, grad_request_t())
2119 end do
2120 gradients(i_atom, i_freq - 1) = diff7(res%energy, delta)
2121 end do
2122 end do
2123 call geom%destroy()
2124 diff = (gradients(:, 0:1) - gradients_anl) / gradients_anl
2125 if (failed(maxval(abs(diff)), 1d-7)) then
2126 call print_matrix('anl', gradients_anl, 6)
2127 call print_matrix('num', gradients(:, 0:1), 6)
2128 end if
2129 end subroutine
2130
2131 subroutine test_mbd_rpa_ewald_deriv_expl()
2132 real(dp) :: delta
2133 type(damping_t) :: damp
2134 real(dp), allocatable :: coords(:, :), gradients(:, :), gradients_anl(:, :), &
2135 diff(:, :), alpha_0(:), C6(:)
2136 type(result_t) :: res(-3:3)
2137 integer :: i_atom, n_atoms, i_xyz, i_step
2138
2139 delta = 0.01d0
2140 n_atoms = 2
2141 allocate (coords(3, n_atoms), source=0d0)
2142 allocate (gradients(n_atoms, 3))
2143 coords(3, 1) = 1d0
2144 coords(1, 2) = 1d0
2145 coords(2, 2) = 4d0
2146 geom%coords = coords
2147 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
2148 geom%k_grid = [2, 2, 2]
2149 geom%do_rpa = .true.
2150 call geom%init()
2151 damp%version = 'fermi,dip'
2152 damp%r_vdw = [3.55d0, 3.5d0]
2153 damp%beta = 0.83d0
2154 alpha_0 = [11d0, 10d0]
2155 C6 = [65d0, 60d0]
2156 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dcoords=.true.))
2157 gradients_anl = res(0)%dE%dcoords
2158 do i_atom = 1, n_atoms
2159 do i_xyz = 1, 3
2160 do i_step = -3, 3
2161 if (i_step == 0) cycle
2162 geom%coords = coords
2163 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + i_step * delta
2164 res(i_step) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t())
2165 end do
2166 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
2167 end do
2168 end do
2169 call geom%destroy()
2170 diff = (gradients - gradients_anl) / gradients_anl
2171 if (failed(maxval(abs(diff)), 1d-8)) then
2172 call print_matrix('delta gradients', diff)
2173 end if
2174 end subroutine
2175
2176 subroutine test_mbd_rpa_ewald_deriv_stress()
2177 real(dp) :: delta
2178 type(damping_t) :: damp
2179 real(dp), allocatable :: gradients(:, :), gradients_anl(:, :), diff(:, :), &
2180 alpha_0(:), C6(:), lattice(:, :)
2181 type(result_t) :: res(-3:3)
2182 integer :: n_atoms, i_xyz, i_step, i_latt
2183
2184 delta = 0.01d0
2185 n_atoms = 2
2186 allocate (geom%coords(3, n_atoms), source=0d0)
2187 allocate (gradients(3, 3))
2188 geom%coords(3, 1) = 1d0
2189 geom%coords(1, 2) = 1d0
2190 geom%coords(2, 2) = 4d0
2191 lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
2192 geom%lattice = lattice
2193 geom%k_grid = [2, 2, 2]
2194 geom%do_rpa = .true.
2195 call geom%init()
2196 damp%version = 'fermi,dip'
2197 damp%r_vdw = [3.55d0, 3.5d0]
2198 damp%beta = 0.83d0
2199 alpha_0 = [11d0, 10d0]
2200 C6 = [65d0, 60d0]
2201 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dlattice=.true.))
2202 gradients_anl = res(0)%dE%dlattice
2203 do i_latt = 1, 3
2204 do i_xyz = 1, 3
2205 do i_step = -3, 3
2206 if (i_step == 0) cycle
2207 geom%lattice = lattice
2208 geom%lattice(i_xyz, i_latt) = geom%lattice(i_xyz, i_latt) + i_step * delta
2209 res(i_step) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t())
2210 end do
2211 gradients(i_latt, i_xyz) = diff7(res%energy, delta)
2212 end do
2213 end do
2214 call geom%destroy()
2215 diff = (gradients - gradients_anl) / gradients_anl
2216 if (failed(maxval(abs(diff)), 3d-6)) then
2217 call print_matrix('delta gradients', diff)
2218 call print_matrix('gradients anl', gradients_anl)
2219 call print_matrix('gradients num', gradients)
2220 end if
2221 end subroutine
2222
2223 subroutine test_mbd_rpa_ewald_deriv_impl_alpha()
2224 real(dp) :: delta
2225 type(damping_t) :: damp
2226 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2227 diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
2228 type(result_t) :: res(-3:3)
2229 integer :: i_atom, n_atoms, i_step
2230
2231 delta = 3d-2
2232 n_atoms = 2
2233 allocate (coords(3, n_atoms), source=0d0)
2234 allocate (gradients(n_atoms))
2235 coords(3, 1) = 1d0
2236 coords(1, 2) = 1d0
2237 coords(2, 2) = 4d0
2238 geom%coords = coords
2239 geom%lattice = reshape([6d0, 1d0, 0d0, -1d0, 9d0, 1d0, 0d0, 1d0, 7d0], [3, 3])
2240 geom%k_grid = [2, 2, 2]
2241 geom%do_rpa = .true.
2242 call geom%init()
2243 damp%version = 'fermi,dip'
2244 damp%r_vdw = [3.55d0, 3.5d0]
2245 damp%beta = 0.83d0
2246 alpha_0 = [11d0, 10d0]
2247 C6 = [65d0, 60d0]
2248 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dalpha=.true.))
2249 gradients_anl = res(0)%dE%dalpha
2250 do i_atom = 1, n_atoms
2251 do i_step = -3, 3
2252 if (i_step == 0) cycle
2253 alpha_0_diff = alpha_0
2254 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
2255 res(i_step) = get_mbd_energy(geom, alpha_0_diff, C6, damp, grad_request_t())
2256 end do
2257 gradients(i_atom) = diff7(res%energy, delta)
2258 end do
2259 call geom%destroy()
2260 diff = (gradients - gradients_anl) / gradients_anl
2261 if (failed(maxval(abs(diff)), 1d-7)) then
2262 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2263 end if
2264 end subroutine
2265
2266 subroutine test_rpa_rescale_deriv()
2267 real(dp) :: delta, num, diff, xr(-3:3), xpts(7), rpts(7), dummy(4), dxr(4)
2268 integer :: i_x, j
2269 real(dp) :: xs(4)
2270
2271 xs = [-1d0, -0.5d0, -0.2d0, 1.5d0]
2272 delta = 1d-3
2273 dummy = rpa_rescale_eigval(xs, dxr)
2274 diff = 0d0
2275 do i_x = 1, size(xs)
2276 do j = 1, 7
2277 xpts(j) = xs(i_x) + (j - 4) * delta
2278 end do
2279 rpts = rpa_rescale_eigval(xpts)
2280 xr = rpts
2281 num = diff7(xr, delta)
2282 diff = max(diff, abs((num - dxr(i_x)) / dxr(i_x)))
2283 end do
2284 if (failed(diff, 1d-9)) then
2285 end if
2286 end subroutine
2287
2288 subroutine test_mbd_rpa_rescale_deriv_expl()
2289 real(dp) :: delta
2290 type(damping_t) :: damp
2291 real(dp), allocatable :: coords(:, :), gradients(:, :), gradients_anl(:, :), &
2292 diff(:, :), alpha_0(:), C6(:)
2293 type(result_t) :: res(-3:3)
2294 integer :: i_atom, n_atoms, i_xyz, i_step
2295
2296 delta = 0.01d0
2297 n_atoms = 3
2298 allocate (coords(3, n_atoms), source=0d0)
2299 allocate (gradients(n_atoms, 3))
2300 coords(1, 3) = 1d0
2301 coords(2, 1) = 4d0
2302 coords(3, 2) = 4d0
2303 geom%coords = coords
2304 geom%do_rpa = .true.
2305 geom%param%rpa_rescale_eigs = .true.
2306 call geom%init()
2307 damp%version = 'fermi,dip'
2308 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
2309 damp%beta = 0.83d0
2310 alpha_0 = [11d0, 10d0, 12d0]
2311 C6 = [65d0, 60d0, 70d0]
2312 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dcoords=.true.))
2313 gradients_anl = res(0)%dE%dcoords
2314 do i_atom = 1, n_atoms
2315 do i_xyz = 1, 3
2316 do i_step = -3, 3
2317 if (i_step == 0) cycle
2318 geom%coords = coords
2319 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + i_step * delta
2320 res(i_step) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t())
2321 end do
2322 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
2323 end do
2324 end do
2325 call geom%destroy()
2326 diff = (gradients - gradients_anl) / gradients_anl
2327 if (failed(maxval(abs(diff)), 1d-6)) then
2328 call print_matrix('delta gradients', diff)
2329 end if
2330 end subroutine
2331
2332 subroutine test_mbd_rpa_rescale_deriv_impl_alpha()
2333 real(dp) :: delta
2334 type(damping_t) :: damp
2335 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2336 diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
2337 type(result_t) :: res(-3:3)
2338 integer :: i_atom, n_atoms, i_step
2339
2340 delta = 3d-2
2341 n_atoms = 3
2342 allocate (coords(3, n_atoms), source=0d0)
2343 allocate (gradients(n_atoms))
2344 coords(1, 3) = 1d0
2345 coords(2, 1) = 4d0
2346 coords(3, 2) = 4d0
2347 geom%coords = coords
2348 geom%do_rpa = .true.
2349 geom%param%rpa_rescale_eigs = .true.
2350 call geom%init()
2351 damp%version = 'fermi,dip'
2352 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
2353 damp%beta = 0.83d0
2354 alpha_0 = [11d0, 10d0, 12d0]
2355 C6 = [65d0, 60d0, 70d0]
2356 res(0) = get_mbd_energy(geom, alpha_0, C6, damp, grad_request_t(dalpha=.true.))
2357 gradients_anl = res(0)%dE%dalpha
2358 do i_atom = 1, n_atoms
2359 do i_step = -3, 3
2360 if (i_step == 0) cycle
2361 alpha_0_diff = alpha_0
2362 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
2363 res(i_step) = get_mbd_energy(geom, alpha_0_diff, C6, damp, grad_request_t())
2364 end do
2365 gradients(i_atom) = diff7(res%energy, delta)
2366 end do
2367 call geom%destroy()
2368 diff = (gradients - gradients_anl) / gradients_anl
2369 if (failed(maxval(abs(diff)), 1d-6)) then
2370 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2371 end if
2372 end subroutine
2373
2374 subroutine test_mbd_rpa_rsscs_deriv_expl()
2375 ! full stack: screening + RPA energy, with combined gradient request
2376 real(dp) :: delta
2377 type(damping_t) :: damp
2378 real(dp), allocatable :: coords(:, :), gradients(:, :), gradients_anl(:, :), &
2379 diff(:, :), alpha_0(:), C6(:)
2380 type(result_t) :: res(-3:3)
2381 integer :: i_atom, n_atoms, i_xyz, i_step
2382
2383 delta = 0.01d0
2384 n_atoms = 3
2385 allocate (coords(3, n_atoms), source=0d0)
2386 allocate (gradients(n_atoms, 3))
2387 coords(2, 1) = 4d0
2388 coords(3, 2) = 4d0
2389 coords(1, 3) = 1d0
2390 geom%coords = coords
2391 geom%do_rpa = .true.
2392 call geom%init()
2393 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
2394 damp%beta = 0.83d0
2395 alpha_0 = [11d0, 10d0, 12d0]
2396 C6 = [65d0, 60d0, 70d0]
2397 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
2398 grad_request_t(dcoords=.true.))
2399 gradients_anl = res(0)%dE%dcoords
2400 do i_atom = 1, n_atoms
2401 do i_xyz = 1, 3
2402 do i_step = -3, 3
2403 if (i_step == 0) cycle
2404 geom%coords = coords
2405 geom%coords(i_xyz, i_atom) = geom%coords(i_xyz, i_atom) + i_step * delta
2406 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
2407 grad_request_t())
2408 end do
2409 gradients(i_atom, i_xyz) = diff7(res%energy, delta)
2410 end do
2411 end do
2412 call geom%destroy()
2413 diff = (gradients - gradients_anl) / gradients_anl
2414 if (failed(maxval(abs(diff)), 1d-6)) then
2415 call print_matrix('delta gradients', diff)
2416 end if
2417 end subroutine
2418
2419 subroutine test_mbd_rpa_rsscs_deriv_impl_alpha()
2420 real(dp) :: delta
2421 type(damping_t) :: damp
2422 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2423 diff(:), alpha_0(:), alpha_0_diff(:), C6(:)
2424 type(result_t) :: res(-3:3)
2425 integer :: i_atom, n_atoms, i_step
2426
2427 delta = 3d-2
2428 n_atoms = 3
2429 allocate (coords(3, n_atoms), source=0d0)
2430 allocate (gradients(n_atoms))
2431 coords(1, 3) = 1d0
2432 coords(2, 1) = 4d0
2433 coords(3, 2) = 4d0
2434 geom%coords = coords
2435 geom%do_rpa = .true.
2436 call geom%init()
2437 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
2438 damp%beta = 0.83d0
2439 alpha_0 = [11d0, 10d0, 12d0]
2440 C6 = [65d0, 60d0, 70d0]
2441 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
2442 grad_request_t(dalpha=.true.))
2443 gradients_anl = res(0)%dE%dalpha
2444 do i_atom = 1, n_atoms
2445 do i_step = -3, 3
2446 if (i_step == 0) cycle
2447 alpha_0_diff = alpha_0
2448 alpha_0_diff(i_atom) = alpha_0_diff(i_atom) + i_step * delta
2449 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0_diff, C6, damp, &
2450 grad_request_t())
2451 end do
2452 gradients(i_atom) = diff7(res%energy, delta)
2453 end do
2454 call geom%destroy()
2455 diff = (gradients - gradients_anl) / gradients_anl
2456 if (failed(maxval(abs(diff)), 1d-6)) then
2457 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2458 end if
2459 end subroutine
2460
2461 subroutine test_mbd_rpa_rsscs_deriv_impl_C6()
2462 real(dp) :: delta
2463 type(damping_t) :: damp
2464 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2465 diff(:), alpha_0(:), C6_diff(:), C6(:)
2466 type(result_t) :: res(-3:3)
2467 integer :: i_atom, n_atoms, i_step
2468
2469 delta = 0.01d0
2470 n_atoms = 3
2471 allocate (coords(3, n_atoms), source=0d0)
2472 allocate (gradients(n_atoms))
2473 coords(1, 3) = 1d0
2474 coords(2, 1) = 4d0
2475 coords(3, 2) = 4d0
2476 geom%coords = coords
2477 geom%do_rpa = .true.
2478 call geom%init()
2479 damp%r_vdw = [3.55d0, 3.5d0, 3.56d0]
2480 damp%beta = 0.83d0
2481 alpha_0 = [11d0, 10d0, 12d0]
2482 C6 = [65d0, 60d0, 70d0]
2483 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
2484 grad_request_t(dC6=.true.))
2485 gradients_anl = res(0)%dE%dC6
2486 do i_atom = 1, n_atoms
2487 do i_step = -3, 3
2488 if (i_step == 0) cycle
2489 C6_diff = C6
2490 C6_diff(i_atom) = C6_diff(i_atom) + i_step * delta
2491 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6_diff, damp, &
2492 grad_request_t())
2493 end do
2494 gradients(i_atom) = diff7(res%energy, delta)
2495 end do
2496 call geom%destroy()
2497 diff = (gradients - gradients_anl) / gradients_anl
2498 if (failed(maxval(abs(diff)), 1d-6)) then
2499 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2500 end if
2501 end subroutine
2502
2503 subroutine test_mbd_rpa_rsscs_deriv_impl_vdw()
2504 real(dp) :: delta
2505 type(damping_t) :: damp
2506 real(dp), allocatable :: coords(:, :), gradients(:), gradients_anl(:), &
2507 diff(:), alpha_0(:), C6(:), r_vdw(:)
2508 type(result_t) :: res(-3:3)
2509 integer :: i_atom, n_atoms, i_step
2510
2511 delta = 1d-2
2512 n_atoms = 3
2513 allocate (coords(3, n_atoms), source=0d0)
2514 allocate (gradients(n_atoms))
2515 coords(2, 1) = 4d0
2516 coords(3, 2) = 4d0
2517 coords(1, 3) = 1d0
2518 geom%coords = coords
2519 geom%do_rpa = .true.
2520 call geom%init()
2521 damp%beta = 0.83d0
2522 r_vdw = [3.55d0, 3.5d0, 3.56d0]
2523 damp%r_vdw = r_vdw
2524 alpha_0 = [11d0, 10d0, 12d0]
2525 C6 = [65d0, 60d0, 70d0]
2526 res(0) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
2527 grad_request_t(dr_vdw=.true.))
2528 gradients_anl = res(0)%dE%dr_vdw
2529 do i_atom = 1, n_atoms
2530 do i_step = -3, 3
2531 if (i_step == 0) cycle
2532 damp%r_vdw = r_vdw
2533 damp%r_vdw(i_atom) = damp%r_vdw(i_atom) + i_step * delta
2534 res(i_step) = get_mbd_scs_energy(geom, 'rsscs', alpha_0, C6, damp, &
2535 grad_request_t())
2536 end do
2537 gradients(i_atom) = diff7(res%energy, delta)
2538 end do
2539 call geom%destroy()
2540 diff = (gradients - gradients_anl) / gradients_anl
2541 if (failed(maxval(abs(diff)), 1d-6)) then
2542 call print_matrix('delta gradients', reshape(diff, [n_atoms, 1]))
2543 end if
2544 end subroutine
2545
2546 end module