(git:5057c24)
Loading...
Searching...
No Matches
qs_dispersion_d4.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Calculation of dispersion using pair potentials
10!> \author Johann Pototschnig
11! **************************************************************************************************
18 USE machine, ONLY: m_flush, &
20 USE cell_types, ONLY: cell_type, &
22 pbc, &
27 USE qs_kind_types, ONLY: get_qs_kind, &
37 USE virial_types, ONLY: virial_type
38 USE kinds, ONLY: dp
44
45#if defined(__DFTD4)
46!&<
48 USE dftd4, ONLY: d4_model, &
49 damping_param, &
50 get_dispersion, &
51 get_rational_damping, &
52 new, &
53 new_d4_model, &
54 new_work_partition, &
55 realspace_cutoff, &
56 structure_type, &
57 work_partition, &
58 rational_damping_param, &
59 get_coordination_number, &
60 get_lattice_points
61 USE multicharge, ONLY: get_charges
62 USE mctc_env, ONLY: error_type
63!&>
64#endif
65#include "./base/base_uses.f90"
66
67 IMPLICIT NONE
68
69 PRIVATE
70
71 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_dispersion_d4'
72
74
75! **************************************************************************************************
76
77CONTAINS
78
79#if defined(__DFTD4)
80! **************************************************************************************************
81!> \brief ...
82!> \param qs_env ...
83!> \param dispersion_env ...
84!> \param evdw ...
85!> \param calculate_forces ...
86!> \param iw ...
87!> \param atomic_energy ...
88! **************************************************************************************************
89 SUBROUTINE calculate_dispersion_d4_pairpot(qs_env, dispersion_env, evdw, calculate_forces, iw, &
90 atomic_energy)
91 TYPE(qs_environment_type), POINTER :: qs_env
92 TYPE(qs_dispersion_type), INTENT(IN), POINTER :: dispersion_env
93 REAL(KIND=dp), INTENT(INOUT) :: evdw
94 LOGICAL, INTENT(IN) :: calculate_forces
95 INTEGER, INTENT(IN) :: iw
96 REAL(KIND=dp), DIMENSION(:), OPTIONAL :: atomic_energy
97
98 CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_dispersion_d4_pairpot'
99
100 INTEGER :: atoma, cnfun, enshift, handle, i, iatom, &
101 ifull, ikind, mref, natom, natom_full, &
102 ncoup, nghost
103 INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_map, atom_map_back, atom_of_kind, &
104 atomtype, kind_of, species, t_atomtype
105 INTEGER, DIMENSION(3) :: periodic
106 LOGICAL :: debug, grad, ifloating, ighost, &
107 use_virial
108 LOGICAL, ALLOCATABLE, DIMENSION(:) :: a_ghost, exclude_ghost
109 LOGICAL, DIMENSION(3) :: lperiod
110 REAL(KIND=dp) :: ed2, ed3, ev1, ev2, ev3, ev4, pd2, pd3, &
111 ta, tb, tc, td, te, ts
112 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: cn, cn_red, cnd, dedcn, dedq, edcn, edq, &
113 enerd2, enerd3, energies, energies3, &
114 q_red, qd
115 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ga, gradient, t_xyz, tvec, xyz
116 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: gdeb, gwdcn, gwdq, gwvec
117 REAL(KIND=dp), DIMENSION(3, 3) :: sigma, stress
118 REAL(KIND=dp), DIMENSION(3, 3, 4) :: sdeb
119 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
120 TYPE(cell_type), POINTER :: cell
121 TYPE(dcnum_type), ALLOCATABLE, DIMENSION(:) :: dcnum
122 TYPE(dft_control_type), POINTER :: dft_control
123 TYPE(mp_para_env_type), POINTER :: para_env
124 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
125 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
126 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
127 TYPE(virial_type), POINTER :: virial
128
129 CLASS(damping_param), ALLOCATABLE :: param
130 TYPE(d4_model) :: disp
131 TYPE(structure_type) :: mol
132 TYPE(realspace_cutoff) :: cutoff
133 TYPE(work_partition) :: partition
134
135 TYPE(error_type), ALLOCATABLE :: error
136
137 CALL timeset(routinen, handle)
138
139 debug = dispersion_env%d4_debug
140
141 CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, atomic_kind_set=atomic_kind_set, &
142 cell=cell, dft_control=dft_control, force=force, virial=virial, para_env=para_env)
143 CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
144
145 !get information about particles
146 natom_full = SIZE(particle_set)
147 nghost = 0
148 ALLOCATE (t_xyz(3, natom_full), t_atomtype(natom_full), a_ghost(natom_full))
149 CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set)
150 DO iatom = 1, natom_full
151 t_xyz(:, iatom) = particle_set(iatom)%r(:)
152 ikind = kind_of(iatom)
153 CALL get_qs_kind(qs_kind_set(ikind), zatom=t_atomtype(iatom), ghost=ighost, floating=ifloating)
154 a_ghost(iatom) = ighost .OR. ifloating
155 IF (a_ghost(iatom)) nghost = nghost + 1
156 END DO
157
158 natom = natom_full - nghost
159 ! Build atom mapping: full index -> reduced index (0 for ghost)
160 ALLOCATE (atom_map(natom_full), atom_map_back(natom))
161 atom_map = 0
162 iatom = 0
163 ALLOCATE (xyz(3, natom), atomtype(natom))
164 DO i = 1, natom_full
165 IF (.NOT. a_ghost(i)) THEN
166 iatom = iatom + 1
167 atom_map(i) = iatom
168 atom_map_back(iatom) = i
169 xyz(:, iatom) = t_xyz(:, i)
170 atomtype(iatom) = t_atomtype(i)
171 END IF
172 END DO
173 DEALLOCATE (a_ghost, t_xyz, t_atomtype)
174
175 !get information about cell / lattice
176 CALL get_cell(cell=cell, periodic=periodic)
177 lperiod(1) = periodic(1) == 1
178 lperiod(2) = periodic(2) == 1
179 lperiod(3) = periodic(3) == 1
180 ! enforce en shift method 1 (original/molecular)
181 ! method 2 from paper on PBC seems not to work
182 enshift = 1
183 !IF (ALL(periodic == 0)) enshift = 1
184
185 !prepare for the call to the dispersion function
186 CALL new(mol, atomtype, xyz, charge=real(dft_control%charge, dp), lattice=cell%hmat, periodic=lperiod)
187 CALL new_d4_model(error, disp, mol)
188 IF (ALLOCATED(error)) THEN
189 cpabort(error%message)
190 END IF
191
192 ! Number of coupling
193 ncoup = disp%ncoup
194
195 ! Build species mapping: full atom index -> D4 species ID (0 for ghost)
196 ALLOCATE (species(natom_full))
197 species = 0
198 DO i = 1, natom
199 species(atom_map_back(i)) = mol%id(i)
200 END DO
201
202 ! Build per-kind exclusion mask for EEQ (ghost/floating kinds excluded)
203 ALLOCATE (exclude_ghost(SIZE(qs_kind_set)))
204 exclude_ghost = .false.
205 DO i = 1, SIZE(qs_kind_set)
206 CALL get_qs_kind(qs_kind_set(i), ghost=ighost, floating=ifloating)
207 exclude_ghost(i) = ighost .OR. ifloating
208 END DO
209
210 IF (dispersion_env%ref_functional == "none") THEN
211 CALL get_rational_damping("pbe", param, s9=0.0_dp)
212 IF (.NOT. ALLOCATED(param)) THEN
213 cpabort("D4: Failed to get rational damping parameters for default functional")
214 END IF
215 SELECT TYPE (param)
216 TYPE is (rational_damping_param)
217 param%s6 = dispersion_env%s6
218 param%s8 = dispersion_env%s8
219 param%a1 = dispersion_env%a1
220 param%a2 = dispersion_env%a2
221 param%alp = dispersion_env%alp
222 END SELECT
223 ELSE
224 CALL get_rational_damping(dispersion_env%ref_functional, param, s9=dispersion_env%s9)
225 IF (.NOT. ALLOCATED(param)) THEN
226 cpabort("D4: Unknown reference functional '"//trim(dispersion_env%ref_functional)//"'")
227 END IF
228 SELECT TYPE (param)
229 TYPE is (rational_damping_param)
230 dispersion_env%s6 = param%s6
231 dispersion_env%s8 = param%s8
232 dispersion_env%a1 = param%a1
233 dispersion_env%a2 = param%a2
234 dispersion_env%alp = param%alp
235 END SELECT
236 END IF
237
238 ! Coordination number cutoff
239 cutoff%cn = dispersion_env%rc_cn
240 ! Two-body interaction cutoff
241 cutoff%disp2 = dispersion_env%rc_d4*2._dp
242 ! Three-body interaction cutoff
243 cutoff%disp3 = dispersion_env%rc_disp*2._dp
244 IF (cutoff%disp3 > cutoff%disp2) THEN
245 cpabort("D4: Three-body cutoff should be smaller than two-body cutoff")
246 END IF
247 cutoff%width2 = dispersion_env%d4_cutoff_width
248 cutoff%width3 = dispersion_env%d4_3b_cutoff_width
249 IF (cutoff%width2 < 0.0_dp .OR. cutoff%width2 >= cutoff%disp2) THEN
250 cpabort("D4: Two-body cutoff width must be non-negative and smaller than the cutoff")
251 END IF
252 IF (cutoff%width3 < 0.0_dp .OR. cutoff%width3 >= cutoff%disp3) THEN
253 cpabort("D4: Three-body cutoff width must be non-negative and smaller than the cutoff")
254 END IF
255
256 IF (calculate_forces) THEN
257 grad = .true.
258 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
259 ELSE
260 grad = .false.
261 use_virial = .false.
262 END IF
263
264 IF (dispersion_env%d4_reference_code .OR. debug) THEN
265 CALL new_work_partition(error, partition, para_env%mepos, para_env%num_pe)
266 IF (ALLOCATED(error)) cpabort(error%message)
267 END IF
268
269 IF (dispersion_env%d4_reference_code) THEN
270
271 !> Wrapper to handle the evaluation of dispersion energy and derivatives
272 IF (.NOT. dispersion_env%doabc) THEN
273 cpwarn("Using D4_REFERENCE_CODE enforces calculation of C9 term.")
274 END IF
275 IF (grad) THEN
276 ALLOCATE (gradient(3, natom))
277 CALL get_dispersion(mol, disp, param, cutoff, evdw, gradient, stress, &
278 partition=partition, error=error)
279 IF (ALLOCATED(error)) cpabort(error%message)
280 IF (calculate_forces) THEN
281 IF (use_virial) THEN
282 virial%pv_virial = virial%pv_virial - stress
283 END IF
284 DO iatom = 1, natom
285 ifull = atom_map_back(iatom)
286 ikind = kind_of(ifull)
287 atoma = atom_of_kind(ifull)
288 force(ikind)%dispersion(:, atoma) = &
289 force(ikind)%dispersion(:, atoma) + gradient(:, iatom)
290 END DO
291 END IF
292 DEALLOCATE (gradient)
293 ELSE
294 CALL get_dispersion(mol, disp, param, cutoff, evdw, partition=partition, error=error)
295 IF (ALLOCATED(error)) cpabort(error%message)
296 END IF
297 ! Energy, forces and virial remain local until CP2K's existing reductions.
298 IF (dispersion_env%ext_charges) dispersion_env%dcharges = 0.0_dp
299 IF (PRESENT(atomic_energy)) THEN
300 cpwarn("Atomic energies not available for D4 reference code")
301 atomic_energy = 0.0_dp
302 END IF
303
304 ELSE
305
306 IF (iw > 0) THEN
307 WRITE (iw, '(/,T2,A)') '!-----------------------------------------------------------------------------!'
308 WRITE (iw, fmt="(T32,A)") "DEBUG D4 DISPERSION"
309 WRITE (iw, '(T2,A)') '!-----------------------------------------------------------------------------!'
310 WRITE (iw, '(A,T71,A10)') " DEBUG D4| Reference functional ", trim(dispersion_env%ref_functional)
311 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Scaling parameter (s6) ", dispersion_env%s6
312 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Scaling parameter (s8) ", dispersion_env%s8
313 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| BJ Damping parameter (a1) ", dispersion_env%a1
314 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| BJ Damping parameter (a2) ", dispersion_env%a2
315 WRITE (iw, '(A,T71,E10.4)') " DEBUG D4| Cutoff value coordination numbers ", dispersion_env%eps_cn
316 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Cutoff radius coordination numbers ", dispersion_env%rc_cn
317 WRITE (iw, '(A,T71,I10)') " DEBUG D4| Coordination number function type ", dispersion_env%cnfun
318 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Cutoff radius 2-body terms [bohr]", 2._dp*dispersion_env%rc_d4
319 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Cutoff radius 3-body terms [bohr]", 2._dp*dispersion_env%rc_disp
320 END IF
321
322 td = 0.0_dp
323 IF (debug) THEN
324 ts = m_walltime()
325 CALL refd4_debug(param, disp, mol, cutoff, partition, grad, dispersion_env%doabc, &
326 enerd2, enerd3, cnd, qd, edcn, edq, gdeb, sdeb)
327 ! Compare complete results; CN and charges are already replicated.
328 CALL para_env%sum(enerd2)
329 IF (dispersion_env%doabc) CALL para_env%sum(enerd3)
330 IF (grad) THEN
331 CALL para_env%sum(edcn)
332 CALL para_env%sum(edq)
333 CALL para_env%sum(gdeb)
334 CALL para_env%sum(sdeb)
335 END IF
336 te = m_walltime()
337 td = te - ts
338 CALL para_env%max(td)
339 END IF
340
341 tc = 0.0_dp
342 ts = m_walltime()
343
344 mref = maxval(disp%ref)
345 ! Coordination numbers (full-size from qs_env; ghosts excluded internally)
346 cnfun = dispersion_env%cnfun
347 CALL cnumber_init(qs_env, cn, dcnum, cnfun, grad)
348 ! cn has size natom_full; ghost entries are 0
349
350 ! Filter CN to reduced space for D4 model
351 ALLOCATE (cn_red(natom))
352 DO i = 1, natom
353 cn_red(i) = cn(atom_map_back(i))
354 END DO
355 IF (debug .AND. iw > 0) THEN
356 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| CN differences (max)", maxval(abs(cn_red - cnd))
357 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| CN differences (ave)", sum(abs(cn_red - cnd))/natom
358 END IF
359
360 ! EEQ charges
361 ! Use CP2K's MPI-parallel EEQ solver with ghost exclusion.
362 ! Ghost kinds get huge hardness + zero coupling -> q_ghost = 0 exactly,
363 ! without influencing real atom charges. Preserves full MPI parallelism.
364 IF (dispersion_env%ext_charges) THEN
365 ALLOCATE (q_red(natom))
366 q_red(1:natom) = dispersion_env%charges(1:natom)
367 ELSE
368 ! eeq_charges writes natom_full entries (from qs_env), so allocate full-size
369 ALLOCATE (q_red(natom_full))
370 CALL eeq_charges(qs_env, q_red, dispersion_env%eeq_sparam, 2, enshift, &
371 exclude=exclude_ghost, cn_max=8.0_dp)
372 ! Filter to reduced size for D4 model
373 block
374 REAL(KIND=dp), ALLOCATABLE :: q_tmp(:)
375 ALLOCATE (q_tmp(natom))
376 DO i = 1, natom
377 q_tmp(i) = q_red(atom_map_back(i))
378 END DO
379 DEALLOCATE (q_red)
380 CALL move_alloc(q_tmp, q_red)
381 END block
382 END IF
383 IF (debug .AND. iw > 0) THEN
384 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| Charge differences (max)", maxval(abs(q_red - qd))
385 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| Charge differences (ave)", sum(abs(q_red - qd))/natom
386 END IF
387 ! Weights for C6 calculation (reduced space)
388 ALLOCATE (gwvec(mref, natom, ncoup))
389 IF (grad) ALLOCATE (gwdcn(mref, natom, ncoup), gwdq(mref, natom, ncoup))
390 CALL disp%weight_references(mol, cn_red, q_red, gwvec, gwdcn, gwdq)
391
392 ! Energies and derivatives (full-size for CP2K infrastructure compatibility)
393 ALLOCATE (energies(natom_full))
394 energies(:) = 0.0_dp
395 IF (grad) THEN
396 ALLOCATE (gradient(3, natom_full), ga(3, natom_full))
397 ALLOCATE (dedcn(natom_full), dedq(natom_full))
398 dedcn(:) = 0.0_dp; dedq(:) = 0.0_dp
399 ga(:, :) = 0.0_dp
400 sigma(:, :) = 0.0_dp
401 END IF
402 CALL dispersion_2b(dispersion_env, cutoff%disp2, cutoff%width2, disp%r4r2, &
403 gwvec, gwdcn, gwdq, disp%c6, disp%ref, &
404 energies, dedcn, dedq, grad, ga, sigma, &
405 atom_map, species)
406 IF (grad) THEN
407 gradient(1:3, 1:natom_full) = ga(1:3, 1:natom_full)
408 stress = sigma
409 IF (debug) THEN
410 CALL para_env%sum(ga)
411 CALL para_env%sum(sigma)
412 IF (iw > 0) THEN
413 CALL gerror(ga, gdeb(:, :, 1), ev1, ev2, ev3, ev4)
414 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [2B]", ev1, ev2, " %"
415 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [2B]", ev3, ev4, " %"
416 IF (use_virial) THEN
417 CALL serror(sigma, sdeb(:, :, 1), ev1, ev2)
418 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [2B]", ev1, ev2, " %"
419 END IF
420 END IF
421 END IF
422 END IF
423 ! no contribution from dispersion_3b as q=0 (but q is changed!)
424 ! so we calculate this here
425 IF (grad) THEN
426 IF (dispersion_env%ext_charges) THEN
427 dispersion_env%dcharges = dedq
428 ELSE
429 CALL para_env%sum(dedq)
430 ! Reconstruct full-sized charges for eeq_forces
431 block
432 REAL(KIND=dp), ALLOCATABLE :: q_full(:)
433 ALLOCATE (q_full(natom_full))
434 q_full = 0.0_dp
435 DO i = 1, natom
436 q_full(atom_map_back(i)) = q_red(i)
437 END DO
438 ga(:, :) = 0.0_dp
439 sigma = 0.0_dp
440 CALL eeq_forces(qs_env, q_full, dedq, ga, sigma, dispersion_env%eeq_sparam, &
441 2, enshift, response_only=.true., exclude=exclude_ghost, &
442 cn_max=8.0_dp)
443 DEALLOCATE (q_full)
444 END block
445 gradient(1:3, 1:natom_full) = gradient(1:3, 1:natom_full) + ga(1:3, 1:natom_full)
446 stress = stress + sigma
447 IF (debug) THEN
448 CALL para_env%sum(ga)
449 CALL para_env%sum(sigma)
450 IF (iw > 0) THEN
451 CALL verror(dedq, edq, ev1, ev2)
452 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Derivative dEdq", ev1, ev2, " %"
453 CALL gerror(ga, gdeb(:, :, 2), ev1, ev2, ev3, ev4)
454 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [dEdq]", ev1, ev2, " %"
455 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [dEdq]", ev3, ev4, " %"
456 IF (use_virial) THEN
457 CALL serror(sigma, sdeb(:, :, 2), ev1, ev2)
458 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [dEdq]", ev1, ev2, " %"
459 END IF
460 END IF
461 END IF
462 END IF
463 END IF
464
465 IF (dispersion_env%doabc) THEN
466 ALLOCATE (energies3(natom_full))
467 energies3(:) = 0.0_dp
468 q_red(:) = 0.0_dp
469 ! i.e. dc6dq = dEdq = 0
470 CALL disp%weight_references(mol, cn_red, q_red, gwvec, gwdcn, gwdq)
471 !
472 IF (grad) THEN
473 gwdq = 0.0_dp
474 ga(:, :) = 0.0_dp
475 sigma = 0.0_dp
476 END IF
477 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%disp3, tvec)
478 CALL dispersion_3b(qs_env, dispersion_env, tvec, cutoff%disp3, disp%r4r2, &
479 gwvec, gwdcn, gwdq, disp%c6, disp%ref, &
480 energies3, dedcn, dedq, grad, ga, sigma, &
481 atom_map, species)
482 IF (grad) THEN
483 gradient(1:3, 1:natom_full) = gradient(1:3, 1:natom_full) + ga(1:3, 1:natom_full)
484 stress = stress + sigma
485 IF (debug) THEN
486 CALL para_env%sum(ga)
487 CALL para_env%sum(sigma)
488 IF (iw > 0) THEN
489 CALL gerror(ga, gdeb(:, :, 3), ev1, ev2, ev3, ev4)
490 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [3B]", ev1, ev2, " %"
491 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [3B]", ev3, ev4, " %"
492 IF (use_virial) THEN
493 CALL serror(sigma, sdeb(:, :, 3), ev1, ev2)
494 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [3B]", ev1, ev2, " %"
495 END IF
496 END IF
497 END IF
498 END IF
499 END IF
500
501 IF (grad) THEN
502 CALL para_env%sum(dedcn)
503 ga(:, :) = 0.0_dp
504 sigma = 0.0_dp
505 CALL dedcn_force(qs_env, dedcn, dcnum, ga, sigma)
506 gradient(1:3, 1:natom_full) = gradient(1:3, 1:natom_full) + ga(1:3, 1:natom_full)
507 stress = stress + sigma
508 IF (debug) THEN
509 CALL para_env%sum(ga)
510 CALL para_env%sum(sigma)
511 IF (iw > 0) THEN
512 CALL verror(dedcn, edcn, ev1, ev2)
513 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Derivative dEdcn", ev1, ev2, " %"
514 CALL gerror(ga, gdeb(:, :, 4), ev1, ev2, ev3, ev4)
515 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [dEdcn]", ev1, ev2, " %"
516 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [dEdcn]", ev3, ev4, " %"
517 IF (use_virial) THEN
518 CALL serror(sigma, sdeb(:, :, 4), ev1, ev2)
519 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [dEdcn]", ev1, ev2, " %"
520 END IF
521 END IF
522 END IF
523 END IF
524 DEALLOCATE (q_red, cn_red)
525 CALL cnumber_release(cn, dcnum, grad)
526 te = m_walltime()
527 tc = tc + te - ts
528
529 IF (debug) THEN
530 CALL para_env%max(tc)
531 ta = sum(energies)
532 CALL para_env%sum(ta)
533 IF (iw > 0) THEN
534 tb = sum(enerd2)
535 ed2 = ta - tb
536 pd2 = abs(ed2)/abs(tb)*100._dp
537 WRITE (iw, '(A,T51,F14.8,T69,F10.4,A)') " DEBUG D4| Energy error 2-body", ed2, pd2, " %"
538 END IF
539 IF (dispersion_env%doabc) THEN
540 ta = sum(energies3)
541 CALL para_env%sum(ta)
542 IF (iw > 0) THEN
543 tb = sum(enerd3)
544 ed3 = ta - tb
545 pd3 = abs(ed3)/abs(tb)*100._dp
546 WRITE (iw, '(A,T51,F14.8,T69,F10.4,A)') " DEBUG D4| Energy error 3-body", ed3, pd3, " %"
547 END IF
548 END IF
549 IF (iw > 0) THEN
550 WRITE (iw, '(A,T67,F14.4)') " DEBUG D4| Time for reference code [s]", td
551 WRITE (iw, '(A,T67,F14.4)') " DEBUG D4| Time for production code [s]", tc
552 END IF
553 END IF
554
555 IF (dispersion_env%doabc) THEN
556 energies(:) = energies(:) + energies3(:)
557 END IF
558 evdw = sum(energies)
559 IF (PRESENT(atomic_energy)) THEN
560 atomic_energy(1:natom_full) = energies(1:natom_full)
561 END IF
562
563 IF (use_virial .AND. calculate_forces) THEN
564 virial%pv_virial = virial%pv_virial - stress
565 END IF
566 IF (calculate_forces) THEN
567 DO iatom = 1, natom_full
568 IF (atom_map(iatom) == 0) cycle
569 ikind = kind_of(iatom)
570 atoma = atom_of_kind(iatom)
571 force(ikind)%dispersion(:, atoma) = &
572 force(ikind)%dispersion(:, atoma) + gradient(:, iatom)
573 END DO
574 END IF
575
576 DEALLOCATE (energies)
577 IF (dispersion_env%doabc) DEALLOCATE (energies3)
578 IF (grad) THEN
579 DEALLOCATE (gradient, ga)
580 END IF
581
582 END IF
583
584 DEALLOCATE (xyz, atomtype, atom_map, atom_map_back, species, exclude_ghost)
585
586 CALL timestop(handle)
587
589
590! **************************************************************************************************
591!> \brief ...
592!> \param param ...
593!> \param disp ...
594!> \param mol ...
595!> \param cutoff ...
596!> \param partition ...
597!> \param grad ...
598!> \param doabc ...
599!> \param enerd2 ...
600!> \param enerd3 ...
601!> \param cnd ...
602!> \param qd ...
603!> \param dEdcn ...
604!> \param dEdq ...
605!> \param gradient ...
606!> \param stress ...
607! **************************************************************************************************
608 SUBROUTINE refd4_debug(param, disp, mol, cutoff, partition, grad, doabc, &
609 enerd2, enerd3, cnd, qd, dEdcn, dEdq, gradient, stress)
610 CLASS(damping_param) :: param
611 TYPE(d4_model) :: disp
612 TYPE(structure_type) :: mol
613 TYPE(realspace_cutoff) :: cutoff
614 TYPE(work_partition), INTENT(IN) :: partition
615 TYPE(error_type), ALLOCATABLE :: error
616 LOGICAL, INTENT(IN) :: grad, doabc
617 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: enerd2, enerd3, cnd, qd, dedcn, dedq
618 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: gradient
619 REAL(KIND=dp), DIMENSION(3, 3, 4) :: stress
620
621 INTEGER :: mref, natom, i, ncoup
622 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: q, qq
623 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: lattr, c6, dc6dcn, dc6dq
624 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: cndr, cndl, qdr, qdl, gwdcn, gwdq, gwvec
625
626 mref = maxval(disp%ref)
627 natom = mol%nat
628 ncoup = disp%ncoup
629
630 ! Coordination numbers
631 ALLOCATE (cnd(natom))
632 IF (grad) ALLOCATE (cndr(3, natom, natom), cndl(3, 3, natom))
633 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%cn, lattr)
634 CALL get_coordination_number(mol, lattr, cutoff%cn, disp%rcov, disp%en, &
635 cnd, cndr, cndl)
636 ! EEQ charges
637 ALLOCATE (qd(natom))
638 IF (grad) ALLOCATE (qdr(3, natom, natom), qdl(3, 3, natom))
639 CALL get_charges(disp%mchrg, mol, error, qd, qdr, qdl)
640 IF (ALLOCATED(error)) THEN
641 cpabort(error%message)
642 END IF
643 ! C6 interpolation
644 ALLOCATE (gwvec(mref, natom, ncoup))
645 IF (grad) ALLOCATE (gwdcn(mref, natom, ncoup), gwdq(mref, natom, ncoup))
646 CALL disp%weight_references(mol, cnd, qd, gwvec, gwdcn, gwdq)
647 ALLOCATE (c6(natom, natom))
648 IF (grad) ALLOCATE (dc6dcn(natom, natom), dc6dq(natom, natom))
649 CALL disp%get_atomic_c6(mol, gwvec, gwdcn, gwdq, c6, dc6dcn, dc6dq)
650 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%disp2, lattr)
651 !
652 IF (grad) THEN
653 ALLOCATE (gradient(3, natom, 4))
654 gradient = 0.0_dp
655 stress = 0.0_dp
656 END IF
657 !
658 ALLOCATE (enerd2(natom))
659 enerd2(:) = 0.0_dp
660 IF (grad) THEN
661 ALLOCATE (dedcn(natom), dedq(natom))
662 dedcn(:) = 0.0_dp; dedq(:) = 0.0_dp
663 END IF
664 IF (grad) THEN
665 CALL param%get_dispersion2(mol, lattr, cutoff%disp2, cutoff%width2, disp%r4r2, c6, &
666 dc6dcn, dc6dq, enerd2, dedcn, dedq, gradient(:, :, 1), &
667 stress(:, :, 1), partition=partition)
668 DO i = 1, 3
669 gradient(i, :, 2) = matmul(qdr(i, :, :), dedq(:))
670 stress(i, :, 2) = matmul(qdl(i, :, :), dedq(:))
671 END DO
672 ELSE
673 CALL param%get_dispersion2(mol, lattr, cutoff%disp2, cutoff%width2, disp%r4r2, c6, &
674 energy=enerd2, partition=partition)
675 END IF
676 !
677 IF (doabc) THEN
678 ALLOCATE (q(natom), qq(natom))
679 q(:) = 0.0_dp; qq(:) = 0.0_dp
680 ALLOCATE (enerd3(natom))
681 enerd3(:) = 0.0_dp
682 CALL disp%weight_references(mol, cnd, q, gwvec, gwdcn, gwdq)
683 CALL disp%get_atomic_c6(mol, gwvec, gwdcn, gwdq, c6, dc6dcn, dc6dq)
684 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%disp3, lattr)
685 IF (grad) THEN
686 CALL param%get_dispersion3(mol, lattr, cutoff%disp3, cutoff%width3, disp%r4r2, c6, &
687 dc6dcn, dc6dq, enerd3, dedcn, qq, gradient(:, :, 3), &
688 stress(:, :, 3), partition=partition)
689 ELSE
690 CALL param%get_dispersion3(mol, lattr, cutoff%disp3, cutoff%width3, disp%r4r2, c6, &
691 energy=enerd3, partition=partition)
692 END IF
693 END IF
694 IF (grad) THEN
695 DO i = 1, 3
696 gradient(i, :, 4) = matmul(cndr(i, :, :), dedcn(:))
697 stress(i, :, 4) = matmul(cndl(i, :, :), dedcn(:))
698 END DO
699 END IF
700
701 END SUBROUTINE refd4_debug
702
703#else
704
705! **************************************************************************************************
706!> \brief ...
707!> \param qs_env ...
708!> \param dispersion_env ...
709!> \param evdw ...
710!> \param calculate_forces ...
711!> \param iw ...
712!> \param atomic_energy ...
713! **************************************************************************************************
714 SUBROUTINE calculate_dispersion_d4_pairpot(qs_env, dispersion_env, evdw, calculate_forces, &
715 iw, atomic_energy)
716 TYPE(qs_environment_type), POINTER :: qs_env
717 TYPE(qs_dispersion_type), INTENT(IN), POINTER :: dispersion_env
718 REAL(kind=dp), INTENT(INOUT) :: evdw
719 LOGICAL, INTENT(IN) :: calculate_forces
720 INTEGER, INTENT(IN) :: iw
721 REAL(kind=dp), DIMENSION(:), OPTIONAL :: atomic_energy
722
723 mark_used(qs_env)
724 mark_used(dispersion_env)
725 mark_used(evdw)
726 mark_used(calculate_forces)
727 mark_used(iw)
728 mark_used(atomic_energy)
729
730 cpabort("CP2K build without DFTD4")
731
733
734#endif
735
736! **************************************************************************************************
737!> \brief Quintic Hermite switching function used by the D4 two-body cutoff.
738!> \param r interatomic distance
739!> \param cutoff real-space cutoff
740!> \param width width of the smooth cutoff region
741!> \param sw switching function value
742!> \param dswdr derivative of the switching function with respect to r
743! **************************************************************************************************
744 SUBROUTINE d4_smooth_cutoff(r, cutoff, width, sw, dswdr)
745 REAL(kind=dp), INTENT(IN) :: r, cutoff, width
746 REAL(kind=dp), INTENT(OUT) :: sw, dswdr
747
748 REAL(kind=dp) :: effective_width, inner, x
749
750 IF (width <= 0.0_dp .OR. cutoff <= 0.0_dp) THEN
751 sw = 1.0_dp
752 dswdr = 0.0_dp
753 ELSE
754 effective_width = min(width, cutoff)
755 inner = cutoff - effective_width
756 IF (r <= inner) THEN
757 sw = 1.0_dp
758 dswdr = 0.0_dp
759 ELSE IF (r >= cutoff) THEN
760 sw = 0.0_dp
761 dswdr = 0.0_dp
762 ELSE
763 x = (cutoff - r)/effective_width
764 sw = x**3*(10.0_dp + x*(-15.0_dp + 6.0_dp*x))
765 dswdr = -30.0_dp*x**2*(1.0_dp - x)**2/effective_width
766 END IF
767 END IF
768
769 END SUBROUTINE d4_smooth_cutoff
770
771! **************************************************************************************************
772!> \brief ...
773!> \param dispersion_env ...
774!> \param cutoff ...
775!> \param width ...
776!> \param r4r2 ...
777!> \param gwvec ...
778!> \param gwdcn ...
779!> \param gwdq ...
780!> \param c6ref ...
781!> \param mrefs ...
782!> \param energies ...
783!> \param dEdcn ...
784!> \param dEdq ...
785!> \param calculate_forces ...
786!> \param gradient ...
787!> \param stress ...
788!> \param atom_map ...
789!> \param species ...
790! **************************************************************************************************
791 SUBROUTINE dispersion_2b(dispersion_env, cutoff, width, r4r2, &
792 gwvec, gwdcn, gwdq, c6ref, mrefs, &
793 energies, dEdcn, dEdq, &
794 calculate_forces, gradient, stress, &
795 atom_map, species)
796 TYPE(qs_dispersion_type), POINTER :: dispersion_env
797 REAL(kind=dp), INTENT(IN) :: cutoff, width
798 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: r4r2
799 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec, gwdcn, gwdq
800 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
801 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
802 REAL(kind=dp), DIMENSION(:), INTENT(INOUT) :: energies, dedcn, dedq
803 LOGICAL, INTENT(IN) :: calculate_forces
804 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient, stress
805 INTEGER, DIMENSION(:), INTENT(IN) :: atom_map, species
806
807 INTEGER :: ia, iatom, ik, ikind, ja, jatom, jk, &
808 jkind, mepos, num_pe
809 REAL(kind=dp) :: a1, a2, c6ij, cutoff2, d6, d8, de, dr2, &
810 dswdr, edisp, edisp0, fac, gdisp, &
811 gdisp0, r, r0ij, rrij, s6, s8, sw, t6, &
812 t8
813 REAL(kind=dp), DIMENSION(2) :: dcdcn, dcdq
814 REAL(kind=dp), DIMENSION(3) :: dg, rij
815 REAL(kind=dp), DIMENSION(3, 3) :: ds
816 TYPE(neighbor_list_iterator_p_type), &
817 DIMENSION(:), POINTER :: nl_iterator
818 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
819 POINTER :: sab_vdw
820
821 a1 = dispersion_env%a1
822 a2 = dispersion_env%a2
823 s6 = dispersion_env%s6
824 s8 = dispersion_env%s8
825 cutoff2 = cutoff*cutoff
826
827 sab_vdw => dispersion_env%sab_vdw
828
829 num_pe = 1
830 CALL neighbor_list_iterator_create(nl_iterator, sab_vdw, nthread=num_pe)
831
832 mepos = 0
833 DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
834 CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, &
835 iatom=iatom, jatom=jatom, r=rij)
836 ! Skip ghost/floating atoms
837 ia = atom_map(iatom)
838 ja = atom_map(jatom)
839 IF (ia == 0 .OR. ja == 0) cycle
840 ! D4 species indices
841 ik = species(iatom)
842 jk = species(jatom)
843 ! vdW potential
844 dr2 = sum(rij(:)**2)
845 IF (dr2 <= cutoff2 .AND. dr2 > 0.0000001_dp) THEN
846 rrij = 3._dp*r4r2(ik)*r4r2(jk)
847 r0ij = a1*sqrt(rrij) + a2
848 IF (calculate_forces) THEN
849 CALL get_c6derivs(c6ij, dcdcn, dcdq, ia, ja, ik, jk, &
850 gwvec, gwdcn, gwdq, c6ref, mrefs)
851 ELSE
852 CALL get_c6value(c6ij, ia, ja, ik, jk, gwvec, c6ref, mrefs)
853 END IF
854 fac = 1._dp
855 IF (iatom == jatom) fac = 0.5_dp
856 r = sqrt(dr2)
857 CALL d4_smooth_cutoff(r, cutoff, width, sw, dswdr)
858 IF (sw <= 0.0_dp) cycle
859 t6 = 1.0_dp/(dr2**3 + r0ij**6)
860 t8 = 1.0_dp/(dr2**4 + r0ij**8)
861
862 edisp0 = s6*t6 + s8*rrij*t8
863 edisp = sw*edisp0*fac
864 de = -c6ij*edisp
865 energies(iatom) = energies(iatom) + de*0.5_dp
866 energies(jatom) = energies(jatom) + de*0.5_dp
867
868 IF (calculate_forces) THEN
869 d6 = -6.0_dp*dr2**2*t6**2
870 d8 = -8.0_dp*dr2**3*t8**2
871 gdisp0 = s6*d6 + s8*rrij*d8
872 gdisp = (sw*gdisp0 + dswdr*edisp0/r)*fac
873 dg(:) = -c6ij*gdisp*rij(:)
874 gradient(:, iatom) = gradient(:, iatom) - dg
875 gradient(:, jatom) = gradient(:, jatom) + dg
876 ds(:, :) = spread(dg, 1, 3)*spread(rij, 2, 3)
877 stress(:, :) = stress(:, :) + ds(:, :)
878 dedcn(iatom) = dedcn(iatom) - dcdcn(1)*edisp
879 dedq(iatom) = dedq(iatom) - dcdq(1)*edisp
880 dedcn(jatom) = dedcn(jatom) - dcdcn(2)*edisp
881 dedq(jatom) = dedq(jatom) - dcdq(2)*edisp
882 END IF
883 END IF
884 END DO
885
886 CALL neighbor_list_iterator_release(nl_iterator)
887
888 END SUBROUTINE dispersion_2b
889
890! **************************************************************************************************
891!> \brief ...
892!> \param qs_env ...
893!> \param dispersion_env ...
894!> \param tvec ...
895!> \param cutoff ...
896!> \param r4r2 ...
897!> \param gwvec ...
898!> \param gwdcn ...
899!> \param gwdq ...
900!> \param c6ref ...
901!> \param mrefs ...
902!> \param energies ...
903!> \param dEdcn ...
904!> \param dEdq ...
905!> \param calculate_forces ...
906!> \param gradient ...
907!> \param stress ...
908!> \param atom_map ...
909!> \param species ...
910! **************************************************************************************************
911 SUBROUTINE dispersion_3b(qs_env, dispersion_env, tvec, cutoff, r4r2, &
912 gwvec, gwdcn, gwdq, c6ref, mrefs, &
913 energies, dEdcn, dEdq, &
914 calculate_forces, gradient, stress, &
915 atom_map, species)
916 TYPE(qs_environment_type), POINTER :: qs_env
917 TYPE(qs_dispersion_type), POINTER :: dispersion_env
918 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: tvec
919 REAL(kind=dp), INTENT(IN) :: cutoff
920 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: r4r2
921 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec, gwdcn, gwdq
922 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
923 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
924 REAL(kind=dp), DIMENSION(:), INTENT(INOUT) :: energies, dedcn, dedq
925 LOGICAL, INTENT(IN) :: calculate_forces
926 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient, stress
927 INTEGER, DIMENSION(:), INTENT(IN) :: atom_map, species
928
929 INTEGER :: ia, iatom, ik, ikind, ja, jatom, jk, &
930 jkind, ka, katom, kk, ktr, mepos, &
931 natom, num_pe
932 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
933 INTEGER, DIMENSION(3) :: cell_b
934 REAL(kind=dp) :: a1, a2, alp, ang, c6ij, c6ik, c6jk, c9, &
935 cutoff2, dang, de, dfdmp, fac, fdmp, &
936 r0, r0ij, r0ik, r0jk, r1, r2, r2ij, &
937 r2ik, r2jk, r3, r5, rr, s6, s8, s9
938 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: rcpbc
939 REAL(kind=dp), DIMENSION(2) :: dc6dcnij, dc6dcnik, dc6dcnjk, dc6dqij, &
940 dc6dqik, dc6dqjk
941 REAL(kind=dp), DIMENSION(3) :: dgij, dgik, dgjk, ra, rb, rb0, rij, vij, &
942 vik, vjk
943 REAL(kind=dp), DIMENSION(3, 3) :: ds
944 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
945 TYPE(cell_type), POINTER :: cell
946 TYPE(neighbor_list_iterator_p_type), &
947 DIMENSION(:), POINTER :: nl_iterator
948 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
949 POINTER :: sab_vdw
950 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
951
952 CALL get_qs_env(qs_env=qs_env, natom=natom, cell=cell, &
953 atomic_kind_set=atomic_kind_set, particle_set=particle_set)
954
955 ALLOCATE (rcpbc(3, natom))
956 DO iatom = 1, natom
957 rcpbc(:, iatom) = pbc(particle_set(iatom)%r(:), cell)
958 END DO
959 CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
960
961 a1 = dispersion_env%a1
962 a2 = dispersion_env%a2
963 s6 = dispersion_env%s6
964 s8 = dispersion_env%s8
965 s9 = dispersion_env%s9
966 alp = dispersion_env%alp
967
968 cutoff2 = cutoff**2
969
970 sab_vdw => dispersion_env%sab_vdw
971
972 num_pe = 1
973 CALL neighbor_list_iterator_create(nl_iterator, sab_vdw, nthread=num_pe)
974
975 mepos = 0
976 DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
977 CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, iatom=iatom, jatom=jatom, r=rij)
978
979 ! Skip ghost/floating atoms
980 ia = atom_map(iatom)
981 ja = atom_map(jatom)
982 IF (ia == 0 .OR. ja == 0) cycle
983 ik = species(iatom)
984 jk = species(jatom)
985
986 r2ij = sum(rij(:)**2)
987 IF (calculate_forces) THEN
988 CALL get_c6derivs(c6ij, dc6dcnij, dc6dqij, ia, ja, ik, jk, &
989 gwvec, gwdcn, gwdq, c6ref, mrefs)
990 ELSE
991 CALL get_c6value(c6ij, ia, ja, ik, jk, gwvec, c6ref, mrefs)
992 END IF
993 r0ij = a1*sqrt(3._dp*r4r2(jk)*r4r2(ik)) + a2
994 IF (r2ij <= cutoff2 .AND. r2ij > epsilon(1._dp)) THEN
995 CALL get_iterator_info(nl_iterator, cell=cell_b)
996 rb0(:) = matmul(cell%hmat, cell_b)
997 ra(:) = rcpbc(:, iatom)
998 rb(:) = rcpbc(:, jatom) + rb0
999 vij(:) = rb(:) - ra(:)
1000
1001 DO katom = 1, min(iatom, jatom)
1002 ka = atom_map(katom)
1003 IF (ka == 0) cycle
1004 kk = species(katom)
1005 IF (calculate_forces) THEN
1006 CALL get_c6derivs(c6ik, dc6dcnik, dc6dqik, ka, ia, kk, ik, &
1007 gwvec, gwdcn, gwdq, c6ref, mrefs)
1008 CALL get_c6derivs(c6jk, dc6dcnjk, dc6dqjk, ka, ja, kk, jk, &
1009 gwvec, gwdcn, gwdq, c6ref, mrefs)
1010 ELSE
1011 CALL get_c6value(c6ik, ka, ia, kk, ik, gwvec, c6ref, mrefs)
1012 CALL get_c6value(c6jk, ka, ja, kk, jk, gwvec, c6ref, mrefs)
1013 END IF
1014 c9 = -s9*sqrt(abs(c6ij*c6ik*c6jk))
1015 r0ik = a1*sqrt(3._dp*r4r2(kk)*r4r2(ik)) + a2
1016 r0jk = a1*sqrt(3._dp*r4r2(kk)*r4r2(jk)) + a2
1017 r0 = r0ij*r0ik*r0jk
1018 fac = triple_scale(iatom, jatom, katom)
1019 DO ktr = 1, SIZE(tvec, 2)
1020 vik(:) = rcpbc(:, katom) + tvec(:, ktr) - rcpbc(:, iatom)
1021 r2ik = vik(1)*vik(1) + vik(2)*vik(2) + vik(3)*vik(3)
1022 IF (r2ik > cutoff2 .OR. r2ik < epsilon(1.0_dp)) cycle
1023 vjk(:) = rcpbc(:, katom) + tvec(:, ktr) - rb(:)
1024 r2jk = vjk(1)*vjk(1) + vjk(2)*vjk(2) + vjk(3)*vjk(3)
1025 IF (r2jk > cutoff2 .OR. r2jk < epsilon(1.0_dp)) cycle
1026 r2 = r2ij*r2ik*r2jk
1027 r1 = sqrt(r2)
1028 r3 = r2*r1
1029 r5 = r3*r2
1030
1031 fdmp = 1.0_dp/(1.0_dp + 6.0_dp*(r0/r1)**(alp/3.0_dp))
1032 ang = 0.375_dp*(r2ij + r2jk - r2ik)*(r2ij - r2jk + r2ik)* &
1033 (-r2ij + r2jk + r2ik)/r5 + 1.0_dp/r3
1034
1035 rr = ang*fdmp
1036 de = rr*c9*fac
1037 energies(iatom) = energies(iatom) - de/3._dp
1038 energies(jatom) = energies(jatom) - de/3._dp
1039 energies(katom) = energies(katom) - de/3._dp
1040
1041 IF (calculate_forces) THEN
1042
1043 dfdmp = -2.0_dp*alp*(r0/r1)**(alp/3.0_dp)*fdmp**2
1044
1045 ! d/drij
1046 dang = -0.375_dp*(r2ij**3 + r2ij**2*(r2jk + r2ik) &
1047 + r2ij*(3.0_dp*r2jk**2 + 2.0_dp*r2jk*r2ik &
1048 + 3.0_dp*r2ik**2) &
1049 - 5.0_dp*(r2jk - r2ik)**2*(r2jk + r2ik))/r5
1050 dgij(:) = c9*(-dang*fdmp + ang*dfdmp)/r2ij*vij
1051
1052 ! d/drik
1053 dang = -0.375_dp*(r2ik**3 + r2ik**2*(r2jk + r2ij) &
1054 + r2ik*(3.0_dp*r2jk**2 + 2.0_dp*r2jk*r2ij &
1055 + 3.0_dp*r2ij**2) &
1056 - 5.0_dp*(r2jk - r2ij)**2*(r2jk + r2ij))/r5
1057 dgik(:) = c9*(-dang*fdmp + ang*dfdmp)/r2ik*vik
1058
1059 ! d/drjk
1060 dang = -0.375_dp*(r2jk**3 + r2jk**2*(r2ik + r2ij) &
1061 + r2jk*(3.0_dp*r2ik**2 + 2.0_dp*r2ik*r2ij &
1062 + 3.0_dp*r2ij**2) &
1063 - 5.0_dp*(r2ik - r2ij)**2*(r2ik + r2ij))/r5
1064 dgjk(:) = c9*(-dang*fdmp + ang*dfdmp)/r2jk*vjk
1065
1066 gradient(:, iatom) = gradient(:, iatom) - dgij - dgik
1067 gradient(:, jatom) = gradient(:, jatom) + dgij - dgjk
1068 gradient(:, katom) = gradient(:, katom) + dgik + dgjk
1069
1070 ds(:, :) = spread(dgij, 1, 3)*spread(vij, 2, 3) &
1071 + spread(dgik, 1, 3)*spread(vik, 2, 3) &
1072 + spread(dgjk, 1, 3)*spread(vjk, 2, 3)
1073
1074 stress(:, :) = stress + ds*fac
1075
1076 dedcn(iatom) = dedcn(iatom) - de*0.5_dp &
1077 *(dc6dcnij(1)/c6ij + dc6dcnik(2)/c6ik)
1078 dedcn(jatom) = dedcn(jatom) - de*0.5_dp &
1079 *(dc6dcnij(2)/c6ij + dc6dcnjk(2)/c6jk)
1080 dedcn(katom) = dedcn(katom) - de*0.5_dp &
1081 *(dc6dcnik(1)/c6ik + dc6dcnjk(1)/c6jk)
1082
1083 dedq(iatom) = dedq(iatom) - de*0.5_dp &
1084 *(dc6dqij(1)/c6ij + dc6dqik(2)/c6ik)
1085 dedq(jatom) = dedq(jatom) - de*0.5_dp &
1086 *(dc6dqij(2)/c6ij + dc6dqjk(2)/c6jk)
1087 dedq(katom) = dedq(katom) - de*0.5_dp &
1088 *(dc6dqik(1)/c6ik + dc6dqjk(1)/c6jk)
1089
1090 END IF
1091
1092 END DO
1093 END DO
1094 END IF
1095 END DO
1096
1097 CALL neighbor_list_iterator_release(nl_iterator)
1098
1099 DEALLOCATE (rcpbc)
1100
1101 END SUBROUTINE dispersion_3b
1102
1103! **************************************************************************************************
1104!> \brief ...
1105!> \param ii ...
1106!> \param jj ...
1107!> \param kk ...
1108!> \return ...
1109! **************************************************************************************************
1110 FUNCTION triple_scale(ii, jj, kk) RESULT(triple)
1111 INTEGER, INTENT(IN) :: ii, jj, kk
1112 REAL(kind=dp) :: triple
1113
1114 IF (ii == jj) THEN
1115 IF (ii == kk) THEN
1116 ! ii'i" -> 1/6
1117 triple = 1.0_dp/6.0_dp
1118 ELSE
1119 ! ii'j -> 1/2
1120 triple = 0.5_dp
1121 END IF
1122 ELSE
1123 IF (ii /= kk .AND. jj /= kk) THEN
1124 ! ijk -> 1 (full)
1125 triple = 1.0_dp
1126 ELSE
1127 ! ijj' and iji' -> 1/2
1128 triple = 0.5_dp
1129 END IF
1130 END IF
1131
1132 END FUNCTION triple_scale
1133
1134! **************************************************************************************************
1135!> \brief ...
1136!> \param qs_env ...
1137!> \param dEdcn ...
1138!> \param dcnum ...
1139!> \param gradient ...
1140!> \param stress ...
1141! **************************************************************************************************
1142 SUBROUTINE dedcn_force(qs_env, dEdcn, dcnum, gradient, stress)
1143 TYPE(qs_environment_type), POINTER :: qs_env
1144 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: dedcn
1145 TYPE(dcnum_type), DIMENSION(:), INTENT(IN) :: dcnum
1146 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient
1147 REAL(kind=dp), DIMENSION(3, 3), INTENT(INOUT) :: stress
1148
1149 CHARACTER(len=*), PARAMETER :: routinen = 'dEdcn_force'
1150
1151 INTEGER :: handle, i, ia, iatom, ikind, katom, &
1152 natom, nkind
1153 LOGICAL :: use_virial
1154 REAL(kind=dp) :: drk
1155 REAL(kind=dp), DIMENSION(3) :: fdik, rik
1156 TYPE(distribution_1d_type), POINTER :: local_particles
1157 TYPE(virial_type), POINTER :: virial
1158
1159 CALL timeset(routinen, handle)
1160
1161 CALL get_qs_env(qs_env, nkind=nkind, natom=natom, &
1162 local_particles=local_particles, &
1163 virial=virial)
1164 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
1165
1166 DO ikind = 1, nkind
1167 DO ia = 1, local_particles%n_el(ikind)
1168 iatom = local_particles%list(ikind)%array(ia)
1169 DO i = 1, dcnum(iatom)%neighbors
1170 katom = dcnum(iatom)%nlist(i)
1171 rik = dcnum(iatom)%rik(:, i)
1172 drk = sqrt(sum(rik(:)**2))
1173 fdik(:) = -(dedcn(iatom) + dedcn(katom))*dcnum(iatom)%dvals(i)*rik(:)/drk
1174 gradient(:, iatom) = gradient(:, iatom) + fdik(:)
1175 IF (use_virial) THEN
1176 CALL virial_pair_force(stress, -0.5_dp, fdik, rik)
1177 END IF
1178 END DO
1179 END DO
1180 END DO
1181
1182 CALL timestop(handle)
1183
1184 END SUBROUTINE dedcn_force
1185
1186! **************************************************************************************************
1187!> \brief ...
1188!> \param c6ij ...
1189!> \param ia ...
1190!> \param ja ...
1191!> \param ik ...
1192!> \param jk ...
1193!> \param gwvec ...
1194!> \param c6ref ...
1195!> \param mrefs ...
1196! **************************************************************************************************
1197 SUBROUTINE get_c6value(c6ij, ia, ja, ik, jk, gwvec, c6ref, mrefs)
1198 REAL(kind=dp), INTENT(OUT) :: c6ij
1199 INTEGER, INTENT(IN) :: ia, ja, ik, jk
1200 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec
1201 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
1202 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
1203
1204 INTEGER :: iref, jref
1205 REAL(kind=dp) :: refc6
1206
1207 c6ij = 0.0_dp
1208 DO jref = 1, mrefs(jk)
1209 DO iref = 1, mrefs(ik)
1210 refc6 = c6ref(iref, jref, ik, jk)
1211 c6ij = c6ij + gwvec(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1212 END DO
1213 END DO
1214
1215 END SUBROUTINE get_c6value
1216
1217! **************************************************************************************************
1218!> \brief ...
1219!> \param c6ij ...
1220!> \param dc6dcn ...
1221!> \param dc6dq ...
1222!> \param ia ...
1223!> \param ja ...
1224!> \param ik ...
1225!> \param jk ...
1226!> \param gwvec ...
1227!> \param gwdcn ...
1228!> \param gwdq ...
1229!> \param c6ref ...
1230!> \param mrefs ...
1231! **************************************************************************************************
1232 SUBROUTINE get_c6derivs(c6ij, dc6dcn, dc6dq, ia, ja, ik, jk, &
1233 gwvec, gwdcn, gwdq, c6ref, mrefs)
1234 REAL(kind=dp), INTENT(OUT) :: c6ij
1235 REAL(kind=dp), DIMENSION(2), INTENT(OUT) :: dc6dcn, dc6dq
1236 INTEGER, INTENT(IN) :: ia, ja, ik, jk
1237 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec, gwdcn, gwdq
1238 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
1239 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
1240
1241 INTEGER :: iref, jref
1242 REAL(kind=dp) :: refc6
1243
1244 c6ij = 0.0_dp
1245 dc6dcn = 0.0_dp
1246 dc6dq = 0.0_dp
1247 DO jref = 1, mrefs(jk)
1248 DO iref = 1, mrefs(ik)
1249 refc6 = c6ref(iref, jref, ik, jk)
1250 c6ij = c6ij + gwvec(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1251 dc6dcn(1) = dc6dcn(1) + gwdcn(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1252 dc6dcn(2) = dc6dcn(2) + gwvec(iref, ia, 1)*gwdcn(jref, ja, 1)*refc6
1253 dc6dq(1) = dc6dq(1) + gwdq(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1254 dc6dq(2) = dc6dq(2) + gwvec(iref, ia, 1)*gwdq(jref, ja, 1)*refc6
1255 END DO
1256 END DO
1257
1258 END SUBROUTINE get_c6derivs
1259
1260! **************************************************************************************************
1261!> \brief ...
1262!> \param ga ...
1263!> \param gd ...
1264!> \param ev1 ...
1265!> \param ev2 ...
1266!> \param ev3 ...
1267!> \param ev4 ...
1268! **************************************************************************************************
1269 SUBROUTINE gerror(ga, gd, ev1, ev2, ev3, ev4)
1270 REAL(kind=dp), DIMENSION(:, :) :: ga, gd
1271 REAL(kind=dp), INTENT(OUT) :: ev1, ev2, ev3, ev4
1272
1273 INTEGER :: na, np(2)
1274
1275 na = SIZE(ga, 2)
1276 ev1 = sqrt(sum((gd - ga)**2)/na)
1277 ev2 = ev1/sqrt(sum(gd**2)/na)*100._dp
1278 np = maxloc(abs(gd - ga))
1279 ev3 = abs(gd(np(1), np(2)) - ga(np(1), np(2)))
1280 ev4 = abs(gd(np(1), np(2)))
1281 IF (ev4 > 1.e-6_dp) THEN
1282 ev4 = ev3/ev4*100._dp
1283 ELSE
1284 ev4 = 100.0_dp
1285 END IF
1286
1287 END SUBROUTINE gerror
1288
1289! **************************************************************************************************
1290!> \brief ...
1291!> \param sa ...
1292!> \param sd ...
1293!> \param ev1 ...
1294!> \param ev2 ...
1295! **************************************************************************************************
1296 SUBROUTINE serror(sa, sd, ev1, ev2)
1297 REAL(kind=dp), DIMENSION(3, 3) :: sa, sd
1298 REAL(kind=dp), INTENT(OUT) :: ev1, ev2
1299
1300 INTEGER :: i, j
1301 REAL(kind=dp) :: rel
1302
1303 ev1 = maxval(abs(sd - sa))
1304 ev2 = 0.0_dp
1305 DO i = 1, 3
1306 DO j = 1, 3
1307 IF (abs(sd(i, j)) > 1.e-6_dp) THEN
1308 rel = abs(sd(i, j) - sa(i, j))/abs(sd(i, j))*100._dp
1309 ev2 = max(ev2, rel)
1310 END IF
1311 END DO
1312 END DO
1313
1314 END SUBROUTINE serror
1315
1316! **************************************************************************************************
1317!> \brief ...
1318!> \param va ...
1319!> \param vd ...
1320!> \param ev1 ...
1321!> \param ev2 ...
1322! **************************************************************************************************
1323 SUBROUTINE verror(va, vd, ev1, ev2)
1324 REAL(kind=dp), DIMENSION(:) :: va, vd
1325 REAL(kind=dp), INTENT(OUT) :: ev1, ev2
1326
1327 INTEGER :: i, na
1328 REAL(kind=dp) :: rel
1329
1330 na = SIZE(va)
1331 ev1 = maxval(abs(vd - va))
1332 ev2 = 0.0_dp
1333 DO i = 1, na
1334 IF (abs(vd(i)) > 1.e-8_dp) THEN
1335 rel = abs(vd(i) - va(i))/abs(vd(i))*100._dp
1336 ev2 = max(ev2, rel)
1337 END IF
1338 END DO
1339
1340 END SUBROUTINE verror
1341
1342END MODULE qs_dispersion_d4
static GRID_HOST_DEVICE double fac(const int i)
Factorial function, e.g. fac(5) = 5! = 120.
Definition grid_common.h:57
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind_set(atomic_kind_set, atom_of_kind, kind_of, natom_of_kind, maxatom, natom, nshell, fist_potential_present, shell_present, shell_adiabatic, shell_check_distance, damping_present)
Get attributes of an atomic kind set.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
Definition cell_types.F:234
real(kind=dp) function, public plane_distance(h, k, l, cell)
Calculate the distance between two lattice planes as defined by a triple of Miller indices (hkl).
Definition cell_types.F:325
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
stores a lists of integer that are local to a processor. The idea is that these integers represent ob...
Calculation of charge equilibration method.
Definition eeq_method.F:12
subroutine, public eeq_charges(qs_env, charges, eeq_sparam, eeq_model, enshift_type, exclude, cn_max)
...
Definition eeq_method.F:203
subroutine, public eeq_forces(qs_env, charges, dcharges, gradient, stress, eeq_sparam, eeq_model, enshift_type, response_only, exclude, cn_max)
...
Definition eeq_method.F:388
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
subroutine, public m_flush(lunit)
flushes units if the &GLOBAL flag is set accordingly
Definition machine.F:127
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:144
Interface to the message passing library MPI.
Define the data structure for the particle information.
Coordination number routines for dispersion pairpotentials.
subroutine, public cnumber_release(cnumbers, dcnum, derivatives)
...
subroutine, public cnumber_init(qs_env, cnumbers, dcnum, ftype, derivatives, disp_env)
...
Calculation of dispersion using pair potentials.
subroutine, public calculate_dispersion_d4_pairpot(qs_env, dispersion_env, evdw, calculate_forces, iw, atomic_energy)
...
Definition of disperson types for DFT calculations.
Set disperson types for DFT calculations.
integer function, public cellhash(cell, ncell)
...
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
subroutine, public set_qs_kind(qs_kind, paw_atom, ghost, floating, hard_radius, hard0_radius, covalent_radius, vdw_radius, lmax_rho0, zeff, no_optimize, dispersion, u_minus_j, hund_j, reltmat, dftb_parameter, xtb_parameter, elec_conf, pao_basis_size)
Set the components of an atomic kind data set.
Define the neighbor list data types and the corresponding functionality.
subroutine, public neighbor_list_iterator_create(iterator_set, nl, search, nthread)
Neighbor list iterator functions.
subroutine, public neighbor_list_iterator_release(iterator_set)
...
integer function, public neighbor_list_iterate(iterator_set, mepos)
...
subroutine, public get_iterator_info(iterator_set, mepos, ikind, jkind, nkind, ilist, nlist, inode, nnode, iatom, jatom, r, cell)
...
pure subroutine, public virial_pair_force(pv_virial, f0, force, rab)
Computes the contribution to the stress tensor from two-body pair-wise forces.
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
structure to store local (to a processor) ordered lists of integers.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.