(git:98357aa)
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!&<
47 USE dftd4, ONLY: d4_model, &
48 damping_param, &
49 get_dispersion, &
50 get_rational_damping, &
51 new, &
52 new_d4_model, &
53 realspace_cutoff, &
54 structure_type, &
55 rational_damping_param, &
56 get_coordination_number, &
57 get_lattice_points
58 USE multicharge, ONLY: get_charges
59 USE mctc_env, ONLY: error_type
60!&>
61#endif
62#include "./base/base_uses.f90"
63
64 IMPLICIT NONE
65
66 PRIVATE
67
68 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_dispersion_d4'
69
71
72! **************************************************************************************************
73
74CONTAINS
75
76#if defined(__DFTD4)
77! **************************************************************************************************
78!> \brief Read smooth-cutoff width override from environment.
79!> \param primary primary environment variable name
80!> \param fallback fallback environment variable name
81!> \param cutoff real-space cutoff used to validate the width
82!> \param width smooth-cutoff width to update
83! **************************************************************************************************
84 SUBROUTINE d4_smooth_width_from_env(primary, fallback, cutoff, width)
85 CHARACTER(LEN=*), INTENT(IN) :: primary, fallback
86 REAL(KIND=dp), INTENT(IN) :: cutoff
87 REAL(KIND=dp), INTENT(INOUT) :: width
88
89 CHARACTER(LEN=64) :: env
90 INTEGER :: io, stat
91 REAL(KIND=dp) :: env_width
92
93 CALL get_environment_variable(primary, env, status=stat)
94 IF (stat /= 0 .OR. len_trim(env) == 0) THEN
95 CALL get_environment_variable(fallback, env, status=stat)
96 END IF
97 IF (stat == 0 .AND. len_trim(env) > 0) THEN
98 READ (env, *, iostat=io) env_width
99 IF (io == 0) THEN
100 IF (env_width > 0.0_dp .AND. env_width < cutoff) THEN
101 width = env_width
102 ELSE
103 width = 0.0_dp
104 END IF
105 END IF
106 END IF
107
108 END SUBROUTINE d4_smooth_width_from_env
109
110! **************************************************************************************************
111!> \brief ...
112!> \param qs_env ...
113!> \param dispersion_env ...
114!> \param evdw ...
115!> \param calculate_forces ...
116!> \param iw ...
117!> \param atomic_energy ...
118! **************************************************************************************************
119 SUBROUTINE calculate_dispersion_d4_pairpot(qs_env, dispersion_env, evdw, calculate_forces, iw, &
120 atomic_energy)
121 TYPE(qs_environment_type), POINTER :: qs_env
122 TYPE(qs_dispersion_type), INTENT(IN), POINTER :: dispersion_env
123 REAL(KIND=dp), INTENT(INOUT) :: evdw
124 LOGICAL, INTENT(IN) :: calculate_forces
125 INTEGER, INTENT(IN) :: iw
126 REAL(KIND=dp), DIMENSION(:), OPTIONAL :: atomic_energy
127
128 CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_dispersion_d4_pairpot'
129
130 INTEGER :: atoma, cnfun, enshift, handle, i, iatom, &
131 ifull, ikind, mref, natom, natom_full, &
132 ncoup, nghost
133 INTEGER, ALLOCATABLE, DIMENSION(:) :: atom_map, atom_map_back, atom_of_kind, &
134 atomtype, kind_of, species, t_atomtype
135 INTEGER, DIMENSION(3) :: periodic
136 LOGICAL :: debug, grad, ifloating, ighost, &
137 use_virial
138 LOGICAL, ALLOCATABLE, DIMENSION(:) :: a_ghost, exclude_ghost
139 LOGICAL, DIMENSION(3) :: lperiod
140 REAL(KIND=dp) :: ed2, ed3, ev1, ev2, ev3, ev4, pd2, pd3, &
141 ta, tb, tc, td, te, ts
142 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: cn, cn_red, cnd, dedcn, dedq, edcn, edq, &
143 enerd2, enerd3, energies, energies3, &
144 q_red, qd
145 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ga, gradient, t_xyz, tvec, xyz
146 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: gdeb, gwdcn, gwdq, gwvec
147 REAL(KIND=dp), DIMENSION(3, 3) :: sigma, stress
148 REAL(KIND=dp), DIMENSION(3, 3, 4) :: sdeb
149 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
150 TYPE(cell_type), POINTER :: cell
151 TYPE(dcnum_type), ALLOCATABLE, DIMENSION(:) :: dcnum
152 TYPE(mp_para_env_type), POINTER :: para_env
153 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
154 TYPE(qs_force_type), DIMENSION(:), POINTER :: force
155 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
156 TYPE(virial_type), POINTER :: virial
157
158 CLASS(damping_param), ALLOCATABLE :: param
159 TYPE(d4_model) :: disp
160 TYPE(structure_type) :: mol
161 TYPE(realspace_cutoff) :: cutoff
162
163 TYPE(error_type), ALLOCATABLE :: error
164
165 CALL timeset(routinen, handle)
166
167 debug = dispersion_env%d4_debug
168
169 CALL get_qs_env(qs_env=qs_env, particle_set=particle_set, atomic_kind_set=atomic_kind_set, &
170 cell=cell, force=force, virial=virial, para_env=para_env)
171 CALL get_atomic_kind_set(atomic_kind_set, atom_of_kind=atom_of_kind, kind_of=kind_of)
172
173 !get information about particles
174 natom_full = SIZE(particle_set)
175 nghost = 0
176 ALLOCATE (t_xyz(3, natom_full), t_atomtype(natom_full), a_ghost(natom_full))
177 CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set)
178 DO iatom = 1, natom_full
179 t_xyz(:, iatom) = particle_set(iatom)%r(:)
180 ikind = kind_of(iatom)
181 CALL get_qs_kind(qs_kind_set(ikind), zatom=t_atomtype(iatom), ghost=ighost, floating=ifloating)
182 a_ghost(iatom) = ighost .OR. ifloating
183 IF (a_ghost(iatom)) nghost = nghost + 1
184 END DO
185
186 natom = natom_full - nghost
187 ! Build atom mapping: full index -> reduced index (0 for ghost)
188 ALLOCATE (atom_map(natom_full), atom_map_back(natom))
189 atom_map = 0
190 iatom = 0
191 ALLOCATE (xyz(3, natom), atomtype(natom))
192 DO i = 1, natom_full
193 IF (.NOT. a_ghost(i)) THEN
194 iatom = iatom + 1
195 atom_map(i) = iatom
196 atom_map_back(iatom) = i
197 xyz(:, iatom) = t_xyz(:, i)
198 atomtype(iatom) = t_atomtype(i)
199 END IF
200 END DO
201 DEALLOCATE (a_ghost, t_xyz, t_atomtype)
202
203 !get information about cell / lattice
204 CALL get_cell(cell=cell, periodic=periodic)
205 lperiod(1) = periodic(1) == 1
206 lperiod(2) = periodic(2) == 1
207 lperiod(3) = periodic(3) == 1
208 ! enforce en shift method 1 (original/molecular)
209 ! method 2 from paper on PBC seems not to work
210 enshift = 1
211 !IF (ALL(periodic == 0)) enshift = 1
212
213 !prepare for the call to the dispersion function
214 CALL new(mol, atomtype, xyz, lattice=cell%hmat, periodic=lperiod)
215 CALL new_d4_model(error, disp, mol)
216 IF (ALLOCATED(error)) THEN
217 cpabort(error%message)
218 END IF
219
220 ! Number of coupling
221 ncoup = disp%ncoup
222
223 ! Build species mapping: full atom index -> D4 species ID (0 for ghost)
224 ALLOCATE (species(natom_full))
225 species = 0
226 DO i = 1, natom
227 species(atom_map_back(i)) = mol%id(i)
228 END DO
229
230 ! Build per-kind exclusion mask for EEQ (ghost/floating kinds excluded)
231 ALLOCATE (exclude_ghost(SIZE(qs_kind_set)))
232 exclude_ghost = .false.
233 DO i = 1, SIZE(qs_kind_set)
234 CALL get_qs_kind(qs_kind_set(i), ghost=ighost, floating=ifloating)
235 exclude_ghost(i) = ighost .OR. ifloating
236 END DO
237
238 IF (dispersion_env%ref_functional == "none") THEN
239 CALL get_rational_damping("pbe", param, s9=0.0_dp)
240 IF (.NOT. ALLOCATED(param)) THEN
241 cpabort("D4: Failed to get rational damping parameters for default functional")
242 END IF
243 SELECT TYPE (param)
244 TYPE is (rational_damping_param)
245 param%s6 = dispersion_env%s6
246 param%s8 = dispersion_env%s8
247 param%a1 = dispersion_env%a1
248 param%a2 = dispersion_env%a2
249 param%alp = dispersion_env%alp
250 END SELECT
251 ELSE
252 CALL get_rational_damping(dispersion_env%ref_functional, param, s9=dispersion_env%s9)
253 IF (.NOT. ALLOCATED(param)) THEN
254 cpabort("D4: Unknown reference functional '"//trim(dispersion_env%ref_functional)//"'")
255 END IF
256 SELECT TYPE (param)
257 TYPE is (rational_damping_param)
258 dispersion_env%s6 = param%s6
259 dispersion_env%s8 = param%s8
260 dispersion_env%a1 = param%a1
261 dispersion_env%a2 = param%a2
262 dispersion_env%alp = param%alp
263 END SELECT
264 END IF
265
266 ! Coordination number cutoff
267 cutoff%cn = dispersion_env%rc_cn
268 ! Two-body interaction cutoff
269 cutoff%disp2 = dispersion_env%rc_d4*2._dp
270 ! Three-body interaction cutoff
271 cutoff%disp3 = dispersion_env%rc_disp*2._dp
272 IF (cutoff%disp3 > cutoff%disp2) THEN
273 cpabort("D4: Three-body cutoff should be smaller than two-body cutoff")
274 END IF
275 ! DFTD4 4.2 introduced explicit smooth-cutoff widths.
276 ! Keep the same narrow two-body smoothing used by the toolchain patch.
277 cutoff%width2 = 0.05_dp
278 cutoff%width3 = 0.0_dp
279 CALL d4_smooth_width_from_env("DFTD4_DISP2_SMOOTH_WIDTH", "TBLITE_D4_DISP2_SMOOTH_WIDTH", &
280 cutoff%disp2, cutoff%width2)
281 CALL d4_smooth_width_from_env("DFTD4_DISP3_SMOOTH_WIDTH", "TBLITE_D4_DISP3_SMOOTH_WIDTH", &
282 cutoff%disp3, cutoff%width3)
283
284 IF (calculate_forces) THEN
285 grad = .true.
286 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
287 ELSE
288 grad = .false.
289 use_virial = .false.
290 END IF
291
292 IF (dispersion_env%d4_reference_code) THEN
293
294 !> Wrapper to handle the evaluation of dispersion energy and derivatives
295 IF (.NOT. dispersion_env%doabc) THEN
296 cpwarn("Using D4_REFERENCE_CODE enforces calculation of C9 term.")
297 END IF
298 IF (grad) THEN
299 ALLOCATE (gradient(3, natom))
300 CALL get_dispersion(mol, disp, param, cutoff, evdw, gradient, stress)
301 IF (calculate_forces) THEN
302 IF (use_virial) THEN
303 virial%pv_virial = virial%pv_virial - stress/para_env%num_pe
304 END IF
305 DO iatom = 1, natom
306 ifull = atom_map_back(iatom)
307 ikind = kind_of(ifull)
308 atoma = atom_of_kind(ifull)
309 force(ikind)%dispersion(:, atoma) = &
310 force(ikind)%dispersion(:, atoma) + gradient(:, iatom)/para_env%num_pe
311 END DO
312 END IF
313 DEALLOCATE (gradient)
314 ELSE
315 CALL get_dispersion(mol, disp, param, cutoff, evdw)
316 END IF
317 !dispersion energy is computed by every MPI process
318 evdw = evdw/para_env%num_pe
319 IF (dispersion_env%ext_charges) dispersion_env%dcharges = 0.0_dp
320 IF (PRESENT(atomic_energy)) THEN
321 cpwarn("Atomic energies not available for D4 reference code")
322 atomic_energy = 0.0_dp
323 END IF
324
325 ELSE
326
327 IF (iw > 0) THEN
328 WRITE (iw, '(/,T2,A)') '!-----------------------------------------------------------------------------!'
329 WRITE (iw, fmt="(T32,A)") "DEBUG D4 DISPERSION"
330 WRITE (iw, '(T2,A)') '!-----------------------------------------------------------------------------!'
331 WRITE (iw, '(A,T71,A10)') " DEBUG D4| Reference functional ", trim(dispersion_env%ref_functional)
332 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Scaling parameter (s6) ", dispersion_env%s6
333 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Scaling parameter (s8) ", dispersion_env%s8
334 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| BJ Damping parameter (a1) ", dispersion_env%a1
335 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| BJ Damping parameter (a2) ", dispersion_env%a2
336 WRITE (iw, '(A,T71,E10.4)') " DEBUG D4| Cutoff value coordination numbers ", dispersion_env%eps_cn
337 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Cutoff radius coordination numbers ", dispersion_env%rc_cn
338 WRITE (iw, '(A,T71,I10)') " DEBUG D4| Coordination number function type ", dispersion_env%cnfun
339 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Cutoff radius 2-body terms [bohr]", 2._dp*dispersion_env%rc_d4
340 WRITE (iw, '(A,T71,F10.4)') " DEBUG D4| Cutoff radius 3-body terms [bohr]", 2._dp*dispersion_env%rc_disp
341 END IF
342
343 td = 0.0_dp
344 IF (debug .AND. iw > 0) THEN
345 ts = m_walltime()
346 CALL refd4_debug(param, disp, mol, cutoff, grad, dispersion_env%doabc, &
347 enerd2, enerd3, cnd, qd, edcn, edq, gdeb, sdeb)
348 te = m_walltime()
349 td = te - ts
350 END IF
351
352 tc = 0.0_dp
353 ts = m_walltime()
354
355 mref = maxval(disp%ref)
356 ! Coordination numbers (full-size from qs_env; ghosts excluded internally)
357 cnfun = dispersion_env%cnfun
358 CALL cnumber_init(qs_env, cn, dcnum, cnfun, grad)
359 ! cn has size natom_full; ghost entries are 0
360
361 ! Filter CN to reduced space for D4 model
362 ALLOCATE (cn_red(natom))
363 DO i = 1, natom
364 cn_red(i) = cn(atom_map_back(i))
365 END DO
366 IF (debug .AND. iw > 0) THEN
367 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| CN differences (max)", maxval(abs(cn_red - cnd))
368 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| CN differences (ave)", sum(abs(cn_red - cnd))/natom
369 END IF
370
371 ! EEQ charges
372 ! Use CP2K's MPI-parallel EEQ solver with ghost exclusion.
373 ! Ghost kinds get huge hardness + zero coupling -> q_ghost = 0 exactly,
374 ! without influencing real atom charges. Preserves full MPI parallelism.
375 IF (dispersion_env%ext_charges) THEN
376 ALLOCATE (q_red(natom))
377 q_red(1:natom) = dispersion_env%charges(1:natom)
378 ELSE
379 ! eeq_charges writes natom_full entries (from qs_env), so allocate full-size
380 ALLOCATE (q_red(natom_full))
381 CALL eeq_charges(qs_env, q_red, dispersion_env%eeq_sparam, 2, enshift, &
382 exclude=exclude_ghost, cn_max=8.0_dp)
383 ! Filter to reduced size for D4 model
384 block
385 REAL(KIND=dp), ALLOCATABLE :: q_tmp(:)
386 ALLOCATE (q_tmp(natom))
387 DO i = 1, natom
388 q_tmp(i) = q_red(atom_map_back(i))
389 END DO
390 DEALLOCATE (q_red)
391 CALL move_alloc(q_tmp, q_red)
392 END block
393 END IF
394 IF (debug .AND. iw > 0) THEN
395 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| Charge differences (max)", maxval(abs(q_red - qd))
396 WRITE (iw, '(A,T71,F10.6)') " DEBUG D4| Charge differences (ave)", sum(abs(q_red - qd))/natom
397 END IF
398 ! Weights for C6 calculation (reduced space)
399 ALLOCATE (gwvec(mref, natom, ncoup))
400 IF (grad) ALLOCATE (gwdcn(mref, natom, ncoup), gwdq(mref, natom, ncoup))
401 CALL disp%weight_references(mol, cn_red, q_red, gwvec, gwdcn, gwdq)
402
403 ! Energies and derivatives (full-size for CP2K infrastructure compatibility)
404 ALLOCATE (energies(natom_full))
405 energies(:) = 0.0_dp
406 IF (grad) THEN
407 ALLOCATE (gradient(3, natom_full), ga(3, natom_full))
408 ALLOCATE (dedcn(natom_full), dedq(natom_full))
409 dedcn(:) = 0.0_dp; dedq(:) = 0.0_dp
410 ga(:, :) = 0.0_dp
411 sigma(:, :) = 0.0_dp
412 END IF
413 CALL dispersion_2b(dispersion_env, cutoff%disp2, disp%r4r2, &
414 gwvec, gwdcn, gwdq, disp%c6, disp%ref, &
415 energies, dedcn, dedq, grad, ga, sigma, &
416 atom_map, species)
417 IF (grad) THEN
418 gradient(1:3, 1:natom_full) = ga(1:3, 1:natom_full)
419 stress = sigma
420 IF (debug) THEN
421 CALL para_env%sum(ga)
422 CALL para_env%sum(sigma)
423 IF (iw > 0) THEN
424 CALL gerror(ga, gdeb(:, :, 1), ev1, ev2, ev3, ev4)
425 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [2B]", ev1, ev2, " %"
426 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [2B]", ev3, ev4, " %"
427 IF (use_virial) THEN
428 CALL serror(sigma, sdeb(:, :, 1), ev1, ev2)
429 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [2B]", ev1, ev2, " %"
430 END IF
431 END IF
432 END IF
433 END IF
434 ! no contribution from dispersion_3b as q=0 (but q is changed!)
435 ! so we calculate this here
436 IF (grad) THEN
437 IF (dispersion_env%ext_charges) THEN
438 dispersion_env%dcharges = dedq
439 ELSE
440 CALL para_env%sum(dedq)
441 ! Reconstruct full-sized charges for eeq_forces
442 block
443 REAL(KIND=dp), ALLOCATABLE :: q_full(:)
444 ALLOCATE (q_full(natom_full))
445 q_full = 0.0_dp
446 DO i = 1, natom
447 q_full(atom_map_back(i)) = q_red(i)
448 END DO
449 ga(:, :) = 0.0_dp
450 sigma = 0.0_dp
451 CALL eeq_forces(qs_env, q_full, dedq, ga, sigma, dispersion_env%eeq_sparam, &
452 2, enshift, response_only=.true., exclude=exclude_ghost, &
453 cn_max=8.0_dp)
454 DEALLOCATE (q_full)
455 END block
456 gradient(1:3, 1:natom_full) = gradient(1:3, 1:natom_full) + ga(1:3, 1:natom_full)
457 stress = stress + sigma
458 IF (debug) THEN
459 CALL para_env%sum(ga)
460 CALL para_env%sum(sigma)
461 IF (iw > 0) THEN
462 CALL verror(dedq, edq, ev1, ev2)
463 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Derivative dEdq", ev1, ev2, " %"
464 CALL gerror(ga, gdeb(:, :, 2), ev1, ev2, ev3, ev4)
465 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [dEdq]", ev1, ev2, " %"
466 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [dEdq]", ev3, ev4, " %"
467 IF (use_virial) THEN
468 CALL serror(sigma, sdeb(:, :, 2), ev1, ev2)
469 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [dEdq]", ev1, ev2, " %"
470 END IF
471 END IF
472 END IF
473 END IF
474 END IF
475
476 IF (dispersion_env%doabc) THEN
477 ALLOCATE (energies3(natom_full))
478 energies3(:) = 0.0_dp
479 q_red(:) = 0.0_dp
480 ! i.e. dc6dq = dEdq = 0
481 CALL disp%weight_references(mol, cn_red, q_red, gwvec, gwdcn, gwdq)
482 !
483 IF (grad) THEN
484 gwdq = 0.0_dp
485 ga(:, :) = 0.0_dp
486 sigma = 0.0_dp
487 END IF
488 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%disp3, tvec)
489 CALL dispersion_3b(qs_env, dispersion_env, tvec, cutoff%disp3, disp%r4r2, &
490 gwvec, gwdcn, gwdq, disp%c6, disp%ref, &
491 energies3, dedcn, dedq, grad, ga, sigma, &
492 atom_map, species)
493 IF (grad) THEN
494 gradient(1:3, 1:natom_full) = gradient(1:3, 1:natom_full) + ga(1:3, 1:natom_full)
495 stress = stress + sigma
496 IF (debug) THEN
497 CALL para_env%sum(ga)
498 CALL para_env%sum(sigma)
499 IF (iw > 0) THEN
500 CALL gerror(ga, gdeb(:, :, 3), ev1, ev2, ev3, ev4)
501 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [3B]", ev1, ev2, " %"
502 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [3B]", ev3, ev4, " %"
503 IF (use_virial) THEN
504 CALL serror(sigma, sdeb(:, :, 3), ev1, ev2)
505 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [3B]", ev1, ev2, " %"
506 END IF
507 END IF
508 END IF
509 END IF
510 END IF
511
512 IF (grad) THEN
513 CALL para_env%sum(dedcn)
514 ga(:, :) = 0.0_dp
515 sigma = 0.0_dp
516 CALL dedcn_force(qs_env, dedcn, dcnum, ga, sigma)
517 gradient(1:3, 1:natom_full) = gradient(1:3, 1:natom_full) + ga(1:3, 1:natom_full)
518 stress = stress + sigma
519 IF (debug) THEN
520 CALL para_env%sum(ga)
521 CALL para_env%sum(sigma)
522 IF (iw > 0) THEN
523 CALL verror(dedcn, edcn, ev1, ev2)
524 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Derivative dEdcn", ev1, ev2, " %"
525 CALL gerror(ga, gdeb(:, :, 4), ev1, ev2, ev3, ev4)
526 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| RMS error Gradient [dEdcn]", ev1, ev2, " %"
527 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Gradient [dEdcn]", ev3, ev4, " %"
528 IF (use_virial) THEN
529 CALL serror(sigma, sdeb(:, :, 4), ev1, ev2)
530 WRITE (iw, '(A,T51,F14.10,T69,F10.4,A)') " DEBUG D4| MAV error Stress [dEdcn]", ev1, ev2, " %"
531 END IF
532 END IF
533 END IF
534 END IF
535 DEALLOCATE (q_red, cn_red)
536 CALL cnumber_release(cn, dcnum, grad)
537 te = m_walltime()
538 tc = tc + te - ts
539
540 IF (debug) THEN
541 ta = sum(energies)
542 CALL para_env%sum(ta)
543 IF (iw > 0) THEN
544 tb = sum(enerd2)
545 ed2 = ta - tb
546 pd2 = abs(ed2)/abs(tb)*100.
547 WRITE (iw, '(A,T51,F14.8,T69,F10.4,A)') " DEBUG D4| Energy error 2-body", ed2, pd2, " %"
548 END IF
549 IF (dispersion_env%doabc) THEN
550 ta = sum(energies3)
551 CALL para_env%sum(ta)
552 IF (iw > 0) THEN
553 tb = sum(enerd3)
554 ed3 = ta - tb
555 pd3 = abs(ed3)/abs(tb)*100.
556 WRITE (iw, '(A,T51,F14.8,T69,F10.4,A)') " DEBUG D4| Energy error 3-body", ed3, pd3, " %"
557 END IF
558 END IF
559 IF (iw > 0) THEN
560 WRITE (iw, '(A,T67,F14.4)') " DEBUG D4| Time for reference code [s]", td
561 WRITE (iw, '(A,T67,F14.4)') " DEBUG D4| Time for production code [s]", tc
562 END IF
563 END IF
564
565 IF (dispersion_env%doabc) THEN
566 energies(:) = energies(:) + energies3(:)
567 END IF
568 evdw = sum(energies)
569 IF (PRESENT(atomic_energy)) THEN
570 atomic_energy(1:natom_full) = energies(1:natom_full)
571 END IF
572
573 IF (use_virial .AND. calculate_forces) THEN
574 virial%pv_virial = virial%pv_virial - stress
575 END IF
576 IF (calculate_forces) THEN
577 DO iatom = 1, natom_full
578 IF (atom_map(iatom) == 0) cycle
579 ikind = kind_of(iatom)
580 atoma = atom_of_kind(iatom)
581 force(ikind)%dispersion(:, atoma) = &
582 force(ikind)%dispersion(:, atoma) + gradient(:, iatom)
583 END DO
584 END IF
585
586 DEALLOCATE (energies)
587 IF (dispersion_env%doabc) DEALLOCATE (energies3)
588 IF (grad) THEN
589 DEALLOCATE (gradient, ga)
590 END IF
591
592 END IF
593
594 DEALLOCATE (xyz, atomtype, atom_map, atom_map_back, species, exclude_ghost)
595
596 CALL timestop(handle)
597
599
600! **************************************************************************************************
601!> \brief ...
602!> \param param ...
603!> \param disp ...
604!> \param mol ...
605!> \param cutoff ...
606!> \param grad ...
607!> \param doabc ...
608!> \param enerd2 ...
609!> \param enerd3 ...
610!> \param cnd ...
611!> \param qd ...
612!> \param dEdcn ...
613!> \param dEdq ...
614!> \param gradient ...
615!> \param stress ...
616! **************************************************************************************************
617 SUBROUTINE refd4_debug(param, disp, mol, cutoff, grad, doabc, &
618 enerd2, enerd3, cnd, qd, dEdcn, dEdq, gradient, stress)
619 CLASS(damping_param) :: param
620 TYPE(d4_model) :: disp
621 TYPE(structure_type) :: mol
622 TYPE(realspace_cutoff) :: cutoff
623 TYPE(error_type), ALLOCATABLE :: error
624 LOGICAL, INTENT(IN) :: grad, doabc
625 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: enerd2, enerd3, cnd, qd, dedcn, dedq
626 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: gradient
627 REAL(KIND=dp), DIMENSION(3, 3, 4) :: stress
628
629 INTEGER :: mref, natom, i, ncoup
630 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: q, qq
631 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: lattr, c6, dc6dcn, dc6dq
632 REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: cndr, cndl, qdr, qdl, gwdcn, gwdq, gwvec
633
634 mref = maxval(disp%ref)
635 natom = mol%nat
636 ncoup = disp%ncoup
637
638 ! Coordination numbers
639 ALLOCATE (cnd(natom))
640 IF (grad) ALLOCATE (cndr(3, natom, natom), cndl(3, 3, natom))
641 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%cn, lattr)
642 CALL get_coordination_number(mol, lattr, cutoff%cn, disp%rcov, disp%en, &
643 cnd, cndr, cndl)
644 ! EEQ charges
645 ALLOCATE (qd(natom))
646 IF (grad) ALLOCATE (qdr(3, natom, natom), qdl(3, 3, natom))
647 CALL get_charges(disp%mchrg, mol, error, qd, qdr, qdl)
648 IF (ALLOCATED(error)) THEN
649 cpabort(error%message)
650 END IF
651 ! C6 interpolation
652 ALLOCATE (gwvec(mref, natom, ncoup))
653 IF (grad) ALLOCATE (gwdcn(mref, natom, ncoup), gwdq(mref, natom, ncoup))
654 CALL disp%weight_references(mol, cnd, qd, gwvec, gwdcn, gwdq)
655 ALLOCATE (c6(natom, natom))
656 IF (grad) ALLOCATE (dc6dcn(natom, natom), dc6dq(natom, natom))
657 CALL disp%get_atomic_c6(mol, gwvec, gwdcn, gwdq, c6, dc6dcn, dc6dq)
658 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%disp2, lattr)
659 !
660 IF (grad) THEN
661 ALLOCATE (gradient(3, natom, 4))
662 gradient = 0.0_dp
663 stress = 0.0_dp
664 END IF
665 !
666 ALLOCATE (enerd2(natom))
667 enerd2(:) = 0.0_dp
668 IF (grad) THEN
669 ALLOCATE (dedcn(natom), dedq(natom))
670 dedcn(:) = 0.0_dp; dedq(:) = 0.0_dp
671 END IF
672 CALL param%get_dispersion2(mol, lattr, cutoff%disp2, cutoff%width2, disp%r4r2, c6, &
673 dc6dcn, dc6dq, enerd2, dedcn, dedq, gradient(:, :, 1), &
674 stress(:, :, 1))
675 !
676 IF (grad) THEN
677 DO i = 1, 3
678 gradient(i, :, 2) = matmul(qdr(i, :, :), dedq(:))
679 stress(i, :, 2) = matmul(qdl(i, :, :), dedq(:))
680 END DO
681 END IF
682 !
683 IF (doabc) THEN
684 ALLOCATE (q(natom), qq(natom))
685 q(:) = 0.0_dp; qq(:) = 0.0_dp
686 ALLOCATE (enerd3(natom))
687 enerd3(:) = 0.0_dp
688 CALL disp%weight_references(mol, cnd, q, gwvec, gwdcn, gwdq)
689 CALL disp%get_atomic_c6(mol, gwvec, gwdcn, gwdq, c6, dc6dcn, dc6dq)
690 CALL get_lattice_points(mol%periodic, mol%lattice, cutoff%disp3, lattr)
691 CALL param%get_dispersion3(mol, lattr, cutoff%disp3, cutoff%width3, disp%r4r2, c6, &
692 dc6dcn, dc6dq, enerd3, dedcn, qq, gradient(:, :, 3), &
693 stress(:, :, 3))
694 END IF
695 IF (grad) THEN
696 DO i = 1, 3
697 gradient(i, :, 4) = matmul(cndr(i, :, :), dedcn(:))
698 stress(i, :, 4) = matmul(cndl(i, :, :), dedcn(:))
699 END DO
700 END IF
701
702 END SUBROUTINE refd4_debug
703
704#else
705
706! **************************************************************************************************
707!> \brief ...
708!> \param qs_env ...
709!> \param dispersion_env ...
710!> \param evdw ...
711!> \param calculate_forces ...
712!> \param iw ...
713!> \param atomic_energy ...
714! **************************************************************************************************
715 SUBROUTINE calculate_dispersion_d4_pairpot(qs_env, dispersion_env, evdw, calculate_forces, &
716 iw, atomic_energy)
717 TYPE(qs_environment_type), POINTER :: qs_env
718 TYPE(qs_dispersion_type), INTENT(IN), POINTER :: dispersion_env
719 REAL(kind=dp), INTENT(INOUT) :: evdw
720 LOGICAL, INTENT(IN) :: calculate_forces
721 INTEGER, INTENT(IN) :: iw
722 REAL(kind=dp), DIMENSION(:), OPTIONAL :: atomic_energy
723
724 mark_used(qs_env)
725 mark_used(dispersion_env)
726 mark_used(evdw)
727 mark_used(calculate_forces)
728 mark_used(iw)
729 mark_used(atomic_energy)
730
731 cpabort("CP2K build without DFTD4")
732
734
735#endif
736
737! **************************************************************************************************
738!> \brief ...
739!> \param dispersion_env ...
740!> \param cutoff ...
741!> \param r4r2 ...
742!> \param gwvec ...
743!> \param gwdcn ...
744!> \param gwdq ...
745!> \param c6ref ...
746!> \param mrefs ...
747!> \param energies ...
748!> \param dEdcn ...
749!> \param dEdq ...
750!> \param calculate_forces ...
751!> \param gradient ...
752!> \param stress ...
753!> \param atom_map ...
754!> \param species ...
755! **************************************************************************************************
756 SUBROUTINE dispersion_2b(dispersion_env, cutoff, r4r2, &
757 gwvec, gwdcn, gwdq, c6ref, mrefs, &
758 energies, dEdcn, dEdq, &
759 calculate_forces, gradient, stress, &
760 atom_map, species)
761 TYPE(qs_dispersion_type), POINTER :: dispersion_env
762 REAL(kind=dp), INTENT(IN) :: cutoff
763 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: r4r2
764 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec, gwdcn, gwdq
765 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
766 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
767 REAL(kind=dp), DIMENSION(:), INTENT(INOUT) :: energies, dedcn, dedq
768 LOGICAL, INTENT(IN) :: calculate_forces
769 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient, stress
770 INTEGER, DIMENSION(:), INTENT(IN) :: atom_map, species
771
772 INTEGER :: ia, iatom, ik, ikind, ja, jatom, jk, &
773 jkind, mepos, num_pe
774 REAL(kind=dp) :: a1, a2, c6ij, cutoff2, d6, d8, de, dr2, &
775 edisp, fac, gdisp, r0ij, rrij, s6, s8, &
776 t6, t8
777 REAL(kind=dp), DIMENSION(2) :: dcdcn, dcdq
778 REAL(kind=dp), DIMENSION(3) :: dg, rij
779 REAL(kind=dp), DIMENSION(3, 3) :: ds
780 TYPE(neighbor_list_iterator_p_type), &
781 DIMENSION(:), POINTER :: nl_iterator
782 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
783 POINTER :: sab_vdw
784
785 a1 = dispersion_env%a1
786 a2 = dispersion_env%a2
787 s6 = dispersion_env%s6
788 s8 = dispersion_env%s8
789 cutoff2 = cutoff*cutoff
790
791 sab_vdw => dispersion_env%sab_vdw
792
793 num_pe = 1
794 CALL neighbor_list_iterator_create(nl_iterator, sab_vdw, nthread=num_pe)
795
796 mepos = 0
797 DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
798 CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, &
799 iatom=iatom, jatom=jatom, r=rij)
800 ! Skip ghost/floating atoms
801 ia = atom_map(iatom)
802 ja = atom_map(jatom)
803 IF (ia == 0 .OR. ja == 0) cycle
804 ! D4 species indices
805 ik = species(iatom)
806 jk = species(jatom)
807 ! vdW potential
808 dr2 = sum(rij(:)**2)
809 IF (dr2 <= cutoff2 .AND. dr2 > 0.0000001_dp) THEN
810 rrij = 3._dp*r4r2(ik)*r4r2(jk)
811 r0ij = a1*sqrt(rrij) + a2
812 IF (calculate_forces) THEN
813 CALL get_c6derivs(c6ij, dcdcn, dcdq, ia, ja, ik, jk, &
814 gwvec, gwdcn, gwdq, c6ref, mrefs)
815 ELSE
816 CALL get_c6value(c6ij, ia, ja, ik, jk, gwvec, c6ref, mrefs)
817 END IF
818 fac = 1._dp
819 IF (iatom == jatom) fac = 0.5_dp
820 t6 = 1.0_dp/(dr2**3 + r0ij**6)
821 t8 = 1.0_dp/(dr2**4 + r0ij**8)
822
823 edisp = (s6*t6 + s8*rrij*t8)*fac
824 de = -c6ij*edisp
825 energies(iatom) = energies(iatom) + de*0.5_dp
826 energies(jatom) = energies(jatom) + de*0.5_dp
827
828 IF (calculate_forces) THEN
829 d6 = -6.0_dp*dr2**2*t6**2
830 d8 = -8.0_dp*dr2**3*t8**2
831 gdisp = (s6*d6 + s8*rrij*d8)*fac
832 dg(:) = -c6ij*gdisp*rij(:)
833 gradient(:, iatom) = gradient(:, iatom) - dg
834 gradient(:, jatom) = gradient(:, jatom) + dg
835 ds(:, :) = spread(dg, 1, 3)*spread(rij, 2, 3)
836 stress(:, :) = stress(:, :) + ds(:, :)
837 dedcn(iatom) = dedcn(iatom) - dcdcn(1)*edisp
838 dedq(iatom) = dedq(iatom) - dcdq(1)*edisp
839 dedcn(jatom) = dedcn(jatom) - dcdcn(2)*edisp
840 dedq(jatom) = dedq(jatom) - dcdq(2)*edisp
841 END IF
842 END IF
843 END DO
844
845 CALL neighbor_list_iterator_release(nl_iterator)
846
847 END SUBROUTINE dispersion_2b
848
849! **************************************************************************************************
850!> \brief ...
851!> \param qs_env ...
852!> \param dispersion_env ...
853!> \param tvec ...
854!> \param cutoff ...
855!> \param r4r2 ...
856!> \param gwvec ...
857!> \param gwdcn ...
858!> \param gwdq ...
859!> \param c6ref ...
860!> \param mrefs ...
861!> \param energies ...
862!> \param dEdcn ...
863!> \param dEdq ...
864!> \param calculate_forces ...
865!> \param gradient ...
866!> \param stress ...
867!> \param atom_map ...
868!> \param species ...
869! **************************************************************************************************
870 SUBROUTINE dispersion_3b(qs_env, dispersion_env, tvec, cutoff, r4r2, &
871 gwvec, gwdcn, gwdq, c6ref, mrefs, &
872 energies, dEdcn, dEdq, &
873 calculate_forces, gradient, stress, &
874 atom_map, species)
875 TYPE(qs_environment_type), POINTER :: qs_env
876 TYPE(qs_dispersion_type), POINTER :: dispersion_env
877 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: tvec
878 REAL(kind=dp), INTENT(IN) :: cutoff
879 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: r4r2
880 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec, gwdcn, gwdq
881 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
882 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
883 REAL(kind=dp), DIMENSION(:), INTENT(INOUT) :: energies, dedcn, dedq
884 LOGICAL, INTENT(IN) :: calculate_forces
885 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient, stress
886 INTEGER, DIMENSION(:), INTENT(IN) :: atom_map, species
887
888 INTEGER :: ia, iatom, ik, ikind, ja, jatom, jk, &
889 jkind, ka, katom, kk, ktr, mepos, &
890 natom, num_pe
891 INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
892 INTEGER, DIMENSION(3) :: cell_b
893 REAL(kind=dp) :: a1, a2, alp, ang, c6ij, c6ik, c6jk, c9, &
894 cutoff2, dang, de, dfdmp, fac, fdmp, &
895 r0, r0ij, r0ik, r0jk, r1, r2, r2ij, &
896 r2ik, r2jk, r3, r5, rr, s6, s8, s9
897 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: rcpbc
898 REAL(kind=dp), DIMENSION(2) :: dc6dcnij, dc6dcnik, dc6dcnjk, dc6dqij, &
899 dc6dqik, dc6dqjk
900 REAL(kind=dp), DIMENSION(3) :: dgij, dgik, dgjk, ra, rb, rb0, rij, vij, &
901 vik, vjk
902 REAL(kind=dp), DIMENSION(3, 3) :: ds
903 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
904 TYPE(cell_type), POINTER :: cell
905 TYPE(neighbor_list_iterator_p_type), &
906 DIMENSION(:), POINTER :: nl_iterator
907 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
908 POINTER :: sab_vdw
909 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
910
911 CALL get_qs_env(qs_env=qs_env, natom=natom, cell=cell, &
912 atomic_kind_set=atomic_kind_set, particle_set=particle_set)
913
914 ALLOCATE (rcpbc(3, natom))
915 DO iatom = 1, natom
916 rcpbc(:, iatom) = pbc(particle_set(iatom)%r(:), cell)
917 END DO
918 CALL get_atomic_kind_set(atomic_kind_set, kind_of=kind_of)
919
920 a1 = dispersion_env%a1
921 a2 = dispersion_env%a2
922 s6 = dispersion_env%s6
923 s8 = dispersion_env%s8
924 s9 = dispersion_env%s9
925 alp = dispersion_env%alp
926
927 cutoff2 = cutoff**2
928
929 sab_vdw => dispersion_env%sab_vdw
930
931 num_pe = 1
932 CALL neighbor_list_iterator_create(nl_iterator, sab_vdw, nthread=num_pe)
933
934 mepos = 0
935 DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
936 CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, iatom=iatom, jatom=jatom, r=rij)
937
938 ! Skip ghost/floating atoms
939 ia = atom_map(iatom)
940 ja = atom_map(jatom)
941 IF (ia == 0 .OR. ja == 0) cycle
942 ik = species(iatom)
943 jk = species(jatom)
944
945 r2ij = sum(rij(:)**2)
946 IF (calculate_forces) THEN
947 CALL get_c6derivs(c6ij, dc6dcnij, dc6dqij, ia, ja, ik, jk, &
948 gwvec, gwdcn, gwdq, c6ref, mrefs)
949 ELSE
950 CALL get_c6value(c6ij, ia, ja, ik, jk, gwvec, c6ref, mrefs)
951 END IF
952 r0ij = a1*sqrt(3._dp*r4r2(jk)*r4r2(ik)) + a2
953 IF (r2ij <= cutoff2 .AND. r2ij > epsilon(1._dp)) THEN
954 CALL get_iterator_info(nl_iterator, cell=cell_b)
955 rb0(:) = matmul(cell%hmat, cell_b)
956 ra(:) = rcpbc(:, iatom)
957 rb(:) = rcpbc(:, jatom) + rb0
958 vij(:) = rb(:) - ra(:)
959
960 DO katom = 1, min(iatom, jatom)
961 ka = atom_map(katom)
962 IF (ka == 0) cycle
963 kk = species(katom)
964 IF (calculate_forces) THEN
965 CALL get_c6derivs(c6ik, dc6dcnik, dc6dqik, ka, ia, kk, ik, &
966 gwvec, gwdcn, gwdq, c6ref, mrefs)
967 CALL get_c6derivs(c6jk, dc6dcnjk, dc6dqjk, ka, ja, kk, jk, &
968 gwvec, gwdcn, gwdq, c6ref, mrefs)
969 ELSE
970 CALL get_c6value(c6ik, ka, ia, kk, ik, gwvec, c6ref, mrefs)
971 CALL get_c6value(c6jk, ka, ja, kk, jk, gwvec, c6ref, mrefs)
972 END IF
973 c9 = -s9*sqrt(abs(c6ij*c6ik*c6jk))
974 r0ik = a1*sqrt(3._dp*r4r2(kk)*r4r2(ik)) + a2
975 r0jk = a1*sqrt(3._dp*r4r2(kk)*r4r2(jk)) + a2
976 r0 = r0ij*r0ik*r0jk
977 fac = triple_scale(iatom, jatom, katom)
978 DO ktr = 1, SIZE(tvec, 2)
979 vik(:) = rcpbc(:, katom) + tvec(:, ktr) - rcpbc(:, iatom)
980 r2ik = vik(1)*vik(1) + vik(2)*vik(2) + vik(3)*vik(3)
981 IF (r2ik > cutoff2 .OR. r2ik < epsilon(1.0_dp)) cycle
982 vjk(:) = rcpbc(:, katom) + tvec(:, ktr) - rb(:)
983 r2jk = vjk(1)*vjk(1) + vjk(2)*vjk(2) + vjk(3)*vjk(3)
984 IF (r2jk > cutoff2 .OR. r2jk < epsilon(1.0_dp)) cycle
985 r2 = r2ij*r2ik*r2jk
986 r1 = sqrt(r2)
987 r3 = r2*r1
988 r5 = r3*r2
989
990 fdmp = 1.0_dp/(1.0_dp + 6.0_dp*(r0/r1)**(alp/3.0_dp))
991 ang = 0.375_dp*(r2ij + r2jk - r2ik)*(r2ij - r2jk + r2ik)* &
992 (-r2ij + r2jk + r2ik)/r5 + 1.0_dp/r3
993
994 rr = ang*fdmp
995 de = rr*c9*fac
996 energies(iatom) = energies(iatom) - de/3._dp
997 energies(jatom) = energies(jatom) - de/3._dp
998 energies(katom) = energies(katom) - de/3._dp
999
1000 IF (calculate_forces) THEN
1001
1002 dfdmp = -2.0_dp*alp*(r0/r1)**(alp/3.0_dp)*fdmp**2
1003
1004 ! d/drij
1005 dang = -0.375_dp*(r2ij**3 + r2ij**2*(r2jk + r2ik) &
1006 + r2ij*(3.0_dp*r2jk**2 + 2.0_dp*r2jk*r2ik &
1007 + 3.0_dp*r2ik**2) &
1008 - 5.0_dp*(r2jk - r2ik)**2*(r2jk + r2ik))/r5
1009 dgij(:) = c9*(-dang*fdmp + ang*dfdmp)/r2ij*vij
1010
1011 ! d/drik
1012 dang = -0.375_dp*(r2ik**3 + r2ik**2*(r2jk + r2ij) &
1013 + r2ik*(3.0_dp*r2jk**2 + 2.0_dp*r2jk*r2ij &
1014 + 3.0_dp*r2ij**2) &
1015 - 5.0_dp*(r2jk - r2ij)**2*(r2jk + r2ij))/r5
1016 dgik(:) = c9*(-dang*fdmp + ang*dfdmp)/r2ik*vik
1017
1018 ! d/drjk
1019 dang = -0.375_dp*(r2jk**3 + r2jk**2*(r2ik + r2ij) &
1020 + r2jk*(3.0_dp*r2ik**2 + 2.0_dp*r2ik*r2ij &
1021 + 3.0_dp*r2ij**2) &
1022 - 5.0_dp*(r2ik - r2ij)**2*(r2ik + r2ij))/r5
1023 dgjk(:) = c9*(-dang*fdmp + ang*dfdmp)/r2jk*vjk
1024
1025 gradient(:, iatom) = gradient(:, iatom) - dgij - dgik
1026 gradient(:, jatom) = gradient(:, jatom) + dgij - dgjk
1027 gradient(:, katom) = gradient(:, katom) + dgik + dgjk
1028
1029 ds(:, :) = spread(dgij, 1, 3)*spread(vij, 2, 3) &
1030 + spread(dgik, 1, 3)*spread(vik, 2, 3) &
1031 + spread(dgjk, 1, 3)*spread(vjk, 2, 3)
1032
1033 stress(:, :) = stress + ds*fac
1034
1035 dedcn(iatom) = dedcn(iatom) - de*0.5_dp &
1036 *(dc6dcnij(1)/c6ij + dc6dcnik(2)/c6ik)
1037 dedcn(jatom) = dedcn(jatom) - de*0.5_dp &
1038 *(dc6dcnij(2)/c6ij + dc6dcnjk(2)/c6jk)
1039 dedcn(katom) = dedcn(katom) - de*0.5_dp &
1040 *(dc6dcnik(1)/c6ik + dc6dcnjk(1)/c6jk)
1041
1042 dedq(iatom) = dedq(iatom) - de*0.5_dp &
1043 *(dc6dqij(1)/c6ij + dc6dqik(2)/c6ik)
1044 dedq(jatom) = dedq(jatom) - de*0.5_dp &
1045 *(dc6dqij(2)/c6ij + dc6dqjk(2)/c6jk)
1046 dedq(katom) = dedq(katom) - de*0.5_dp &
1047 *(dc6dqik(1)/c6ik + dc6dqjk(1)/c6jk)
1048
1049 END IF
1050
1051 END DO
1052 END DO
1053 END IF
1054 END DO
1055
1056 CALL neighbor_list_iterator_release(nl_iterator)
1057
1058 DEALLOCATE (rcpbc)
1059
1060 END SUBROUTINE dispersion_3b
1061
1062! **************************************************************************************************
1063!> \brief ...
1064!> \param ii ...
1065!> \param jj ...
1066!> \param kk ...
1067!> \return ...
1068! **************************************************************************************************
1069 FUNCTION triple_scale(ii, jj, kk) RESULT(triple)
1070 INTEGER, INTENT(IN) :: ii, jj, kk
1071 REAL(kind=dp) :: triple
1072
1073 IF (ii == jj) THEN
1074 IF (ii == kk) THEN
1075 ! ii'i" -> 1/6
1076 triple = 1.0_dp/6.0_dp
1077 ELSE
1078 ! ii'j -> 1/2
1079 triple = 0.5_dp
1080 END IF
1081 ELSE
1082 IF (ii /= kk .AND. jj /= kk) THEN
1083 ! ijk -> 1 (full)
1084 triple = 1.0_dp
1085 ELSE
1086 ! ijj' and iji' -> 1/2
1087 triple = 0.5_dp
1088 END IF
1089 END IF
1090
1091 END FUNCTION triple_scale
1092
1093! **************************************************************************************************
1094!> \brief ...
1095!> \param qs_env ...
1096!> \param dEdcn ...
1097!> \param dcnum ...
1098!> \param gradient ...
1099!> \param stress ...
1100! **************************************************************************************************
1101 SUBROUTINE dedcn_force(qs_env, dEdcn, dcnum, gradient, stress)
1102 TYPE(qs_environment_type), POINTER :: qs_env
1103 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: dedcn
1104 TYPE(dcnum_type), DIMENSION(:), INTENT(IN) :: dcnum
1105 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient
1106 REAL(kind=dp), DIMENSION(3, 3), INTENT(INOUT) :: stress
1107
1108 CHARACTER(len=*), PARAMETER :: routinen = 'dEdcn_force'
1109
1110 INTEGER :: handle, i, ia, iatom, ikind, katom, &
1111 natom, nkind
1112 LOGICAL :: use_virial
1113 REAL(kind=dp) :: drk
1114 REAL(kind=dp), DIMENSION(3) :: fdik, rik
1115 TYPE(distribution_1d_type), POINTER :: local_particles
1116 TYPE(virial_type), POINTER :: virial
1117
1118 CALL timeset(routinen, handle)
1119
1120 CALL get_qs_env(qs_env, nkind=nkind, natom=natom, &
1121 local_particles=local_particles, &
1122 virial=virial)
1123 use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
1124
1125 DO ikind = 1, nkind
1126 DO ia = 1, local_particles%n_el(ikind)
1127 iatom = local_particles%list(ikind)%array(ia)
1128 DO i = 1, dcnum(iatom)%neighbors
1129 katom = dcnum(iatom)%nlist(i)
1130 rik = dcnum(iatom)%rik(:, i)
1131 drk = sqrt(sum(rik(:)**2))
1132 fdik(:) = -(dedcn(iatom) + dedcn(katom))*dcnum(iatom)%dvals(i)*rik(:)/drk
1133 gradient(:, iatom) = gradient(:, iatom) + fdik(:)
1134 IF (use_virial) THEN
1135 CALL virial_pair_force(stress, -0.5_dp, fdik, rik)
1136 END IF
1137 END DO
1138 END DO
1139 END DO
1140
1141 CALL timestop(handle)
1142
1143 END SUBROUTINE dedcn_force
1144
1145! **************************************************************************************************
1146!> \brief ...
1147!> \param c6ij ...
1148!> \param ia ...
1149!> \param ja ...
1150!> \param ik ...
1151!> \param jk ...
1152!> \param gwvec ...
1153!> \param c6ref ...
1154!> \param mrefs ...
1155! **************************************************************************************************
1156 SUBROUTINE get_c6value(c6ij, ia, ja, ik, jk, gwvec, c6ref, mrefs)
1157 REAL(kind=dp), INTENT(OUT) :: c6ij
1158 INTEGER, INTENT(IN) :: ia, ja, ik, jk
1159 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec
1160 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
1161 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
1162
1163 INTEGER :: iref, jref
1164 REAL(kind=dp) :: refc6
1165
1166 c6ij = 0.0_dp
1167 DO jref = 1, mrefs(jk)
1168 DO iref = 1, mrefs(ik)
1169 refc6 = c6ref(iref, jref, ik, jk)
1170 c6ij = c6ij + gwvec(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1171 END DO
1172 END DO
1173
1174 END SUBROUTINE get_c6value
1175
1176! **************************************************************************************************
1177!> \brief ...
1178!> \param c6ij ...
1179!> \param dc6dcn ...
1180!> \param dc6dq ...
1181!> \param ia ...
1182!> \param ja ...
1183!> \param ik ...
1184!> \param jk ...
1185!> \param gwvec ...
1186!> \param gwdcn ...
1187!> \param gwdq ...
1188!> \param c6ref ...
1189!> \param mrefs ...
1190! **************************************************************************************************
1191 SUBROUTINE get_c6derivs(c6ij, dc6dcn, dc6dq, ia, ja, ik, jk, &
1192 gwvec, gwdcn, gwdq, c6ref, mrefs)
1193 REAL(kind=dp), INTENT(OUT) :: c6ij
1194 REAL(kind=dp), DIMENSION(2), INTENT(OUT) :: dc6dcn, dc6dq
1195 INTEGER, INTENT(IN) :: ia, ja, ik, jk
1196 REAL(kind=dp), DIMENSION(:, :, :), INTENT(IN) :: gwvec, gwdcn, gwdq
1197 REAL(kind=dp), DIMENSION(:, :, :, :), INTENT(IN) :: c6ref
1198 INTEGER, DIMENSION(:), INTENT(IN) :: mrefs
1199
1200 INTEGER :: iref, jref
1201 REAL(kind=dp) :: refc6
1202
1203 c6ij = 0.0_dp
1204 dc6dcn = 0.0_dp
1205 dc6dq = 0.0_dp
1206 DO jref = 1, mrefs(jk)
1207 DO iref = 1, mrefs(ik)
1208 refc6 = c6ref(iref, jref, ik, jk)
1209 c6ij = c6ij + gwvec(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1210 dc6dcn(1) = dc6dcn(1) + gwdcn(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1211 dc6dcn(2) = dc6dcn(2) + gwvec(iref, ia, 1)*gwdcn(jref, ja, 1)*refc6
1212 dc6dq(1) = dc6dq(1) + gwdq(iref, ia, 1)*gwvec(jref, ja, 1)*refc6
1213 dc6dq(2) = dc6dq(2) + gwvec(iref, ia, 1)*gwdq(jref, ja, 1)*refc6
1214 END DO
1215 END DO
1216
1217 END SUBROUTINE get_c6derivs
1218
1219! **************************************************************************************************
1220!> \brief ...
1221!> \param ga ...
1222!> \param gd ...
1223!> \param ev1 ...
1224!> \param ev2 ...
1225!> \param ev3 ...
1226!> \param ev4 ...
1227! **************************************************************************************************
1228 SUBROUTINE gerror(ga, gd, ev1, ev2, ev3, ev4)
1229 REAL(kind=dp), DIMENSION(:, :) :: ga, gd
1230 REAL(kind=dp), INTENT(OUT) :: ev1, ev2, ev3, ev4
1231
1232 INTEGER :: na, np(2)
1233
1234 na = SIZE(ga, 2)
1235 ev1 = sqrt(sum((gd - ga)**2)/na)
1236 ev2 = ev1/sqrt(sum(gd**2)/na)*100._dp
1237 np = maxloc(abs(gd - ga))
1238 ev3 = abs(gd(np(1), np(2)) - ga(np(1), np(2)))
1239 ev4 = abs(gd(np(1), np(2)))
1240 IF (ev4 > 1.e-6) THEN
1241 ev4 = ev3/ev4*100._dp
1242 ELSE
1243 ev4 = 100.0_dp
1244 END IF
1245
1246 END SUBROUTINE gerror
1247
1248! **************************************************************************************************
1249!> \brief ...
1250!> \param sa ...
1251!> \param sd ...
1252!> \param ev1 ...
1253!> \param ev2 ...
1254! **************************************************************************************************
1255 SUBROUTINE serror(sa, sd, ev1, ev2)
1256 REAL(kind=dp), DIMENSION(3, 3) :: sa, sd
1257 REAL(kind=dp), INTENT(OUT) :: ev1, ev2
1258
1259 INTEGER :: i, j
1260 REAL(kind=dp) :: rel
1261
1262 ev1 = maxval(abs(sd - sa))
1263 ev2 = 0.0_dp
1264 DO i = 1, 3
1265 DO j = 1, 3
1266 IF (abs(sd(i, j)) > 1.e-6_dp) THEN
1267 rel = abs(sd(i, j) - sa(i, j))/abs(sd(i, j))*100._dp
1268 ev2 = max(ev2, rel)
1269 END IF
1270 END DO
1271 END DO
1272
1273 END SUBROUTINE serror
1274
1275! **************************************************************************************************
1276!> \brief ...
1277!> \param va ...
1278!> \param vd ...
1279!> \param ev1 ...
1280!> \param ev2 ...
1281! **************************************************************************************************
1282 SUBROUTINE verror(va, vd, ev1, ev2)
1283 REAL(kind=dp), DIMENSION(:) :: va, vd
1284 REAL(kind=dp), INTENT(OUT) :: ev1, ev2
1285
1286 INTEGER :: i, na
1287 REAL(kind=dp) :: rel
1288
1289 na = SIZE(va)
1290 ev1 = maxval(abs(vd - va))
1291 ev2 = 0.0_dp
1292 DO i = 1, na
1293 IF (abs(vd(i)) > 1.e-8_dp) THEN
1294 rel = abs(vd(i) - va(i))/abs(vd(i))*100._dp
1295 ev2 = max(ev2, rel)
1296 END IF
1297 END DO
1298
1299 END SUBROUTINE verror
1300
1301END 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:56
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:233
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:324
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:195
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:380
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:124
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:141
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.