Line data Source code
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 Methods dealing with Neural Network potentials
10 : !> \author Christoph Schran (christoph.schran@rub.de)
11 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
12 : !> \author Claudio Malvino (claudiormal@gmail.com)
13 : !> \date 2020-10-10
14 : ! **************************************************************************************************
15 : MODULE nnp_force
16 :
17 : USE atomic_kind_types, ONLY: atomic_kind_type
18 : USE cp_log_handling, ONLY: cp_get_default_logger,&
19 : cp_logger_get_default_io_unit,&
20 : cp_logger_type
21 : USE cp_output_handling, ONLY: cp_p_file,&
22 : cp_print_key_finished_output,&
23 : cp_print_key_should_output,&
24 : cp_print_key_unit_nr
25 : USE cp_subsys_types, ONLY: cp_subsys_get,&
26 : cp_subsys_type
27 : USE cp_units, ONLY: cp_unit_from_cp2k
28 : USE distribution_1d_types, ONLY: distribution_1d_type
29 : USE input_constants, ONLY: nnp_gpu_prec_mixed,&
30 : nnp_use_gpu_on
31 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
32 : section_vals_type,&
33 : section_vals_val_get
34 : USE kinds, ONLY: default_path_length,&
35 : default_string_length,&
36 : dp
37 : USE nnp_acsf, ONLY: nnp_calc_acsf,&
38 : nnp_prepare_neighbor_cache
39 : USE nnp_environment_types, ONLY: nnp_arc_type,&
40 : nnp_env_get,&
41 : nnp_type
42 : USE nnp_gpu, ONLY: nnp_gpu_energy_force
43 : USE nnp_model, ONLY: nnp_gradients,&
44 : nnp_predict
45 : USE particle_types, ONLY: particle_type
46 : USE periodic_table, ONLY: get_ptable_info
47 : USE physcon, ONLY: angstrom
48 : USE virial_types, ONLY: virial_type
49 :
50 : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num
51 : #include "./base/base_uses.f90"
52 :
53 : IMPLICIT NONE
54 :
55 : PRIVATE
56 :
57 : LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .FALSE.
58 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'nnp_force'
59 :
60 : PUBLIC :: nnp_calc_energy_force, nnp_scatter_dgdr_to_forces
61 :
62 : CONTAINS
63 :
64 : ! **************************************************************************************************
65 : !> \brief Calculate the energy and force for a given configuration with the NNP
66 : !> \param nnp ...
67 : !> \param calc_forces ...
68 : !> \date 2020-10-10
69 : !> \author Christoph Schran (christoph.schran@rub.de)
70 : !> \author Claudio Malvino (claudiormal@gmail.com)
71 : ! **************************************************************************************************
72 427 : SUBROUTINE nnp_calc_energy_force(nnp, calc_forces)
73 : TYPE(nnp_type), INTENT(INOUT), POINTER :: nnp
74 : LOGICAL, INTENT(IN) :: calc_forces
75 :
76 : CHARACTER(len=*), PARAMETER :: routineN = 'nnp_calc_energy_force'
77 :
78 : INTEGER :: handle, handle_loop, i, i_com, ie, ig, &
79 : il, ind, istart, j, k, m, &
80 : max_input_nodes, mecalc, &
81 : n_input_nodes, nthreads, t, tid
82 427 : INTEGER, ALLOCATABLE, DIMENSION(:) :: allcalc
83 : LOGICAL :: calc_stress, run_cpu_path, use_gpu_path, &
84 : validate
85 427 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: denergydsym
86 427 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: val_energy
87 427 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: stress, val_force, val_stress
88 427 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: my_cstress_t, my_force_t
89 427 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
90 : TYPE(cp_logger_type), POINTER :: logger
91 : TYPE(cp_subsys_type), POINTER :: subsys
92 : TYPE(distribution_1d_type), POINTER :: local_particles
93 427 : TYPE(nnp_arc_type), ALLOCATABLE, DIMENSION(:) :: my_arc
94 427 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
95 : TYPE(section_vals_type), POINTER :: print_section
96 : TYPE(virial_type), POINTER :: virial
97 :
98 427 : CALL timeset(routineN, handle)
99 :
100 427 : NULLIFY (particle_set, logger, local_particles, subsys, &
101 427 : atomic_kind_set)
102 427 : logger => cp_get_default_logger()
103 :
104 427 : CPASSERT(ASSOCIATED(nnp))
105 427 : CPASSERT(nnp%n_committee >= 1)
106 : CALL nnp_env_get(nnp_env=nnp, particle_set=particle_set, &
107 : subsys=subsys, local_particles=local_particles, &
108 427 : atomic_kind_set=atomic_kind_set)
109 :
110 : CALL cp_subsys_get(subsys, &
111 427 : virial=virial)
112 :
113 427 : calc_stress = virial%pv_availability .AND. (.NOT. virial%pv_numer)
114 427 : IF (calc_stress .AND. .NOT. calc_forces) THEN
115 0 : CPABORT('Stress cannot be calculated without forces')
116 : END IF
117 :
118 630345 : nnp%atomic_energy(:, :) = 0.0_dp
119 1563241 : IF (calc_forces) nnp%myforce(:, :, :) = 0.0_dp
120 1563241 : IF (calc_forces) nnp%committee_forces(:, :, :) = 0.0_dp
121 5315 : IF (calc_stress) nnp%committee_stress(:, :, :) = 0.0_dp
122 :
123 : !fill coord array
124 427 : ig = 1
125 1313 : DO i = 1, nnp%n_ele
126 176677 : DO j = 1, nnp%num_atoms
127 176250 : IF (nnp%ele(i) == particle_set(j)%atomic_kind%element_symbol) THEN
128 338696 : DO m = 1, 3
129 338696 : nnp%coord(m, ig) = particle_set(j)%r(m)
130 : END DO
131 84674 : nnp%atoms(ig) = nnp%ele(i)
132 84674 : CALL get_ptable_info(nnp%atoms(ig) (1:2), number=nnp%nuc_atoms(ig))
133 84674 : nnp%ele_ind(ig) = i
134 84674 : nnp%sort(ig) = j
135 84674 : nnp%sort_inv(j) = ig
136 84674 : ig = ig + 1
137 : END IF
138 : END DO
139 : END DO
140 :
141 : ! parallization:
142 : mecalc = nnp%num_atoms/logger%para_env%num_pe + &
143 : MIN(MOD(nnp%num_atoms, logger%para_env%num_pe)/ &
144 427 : (logger%para_env%mepos + 1), 1)
145 1281 : ALLOCATE (allcalc(logger%para_env%num_pe))
146 427 : allcalc(:) = 0
147 427 : CALL logger%para_env%allgather(mecalc, allcalc)
148 427 : istart = 1
149 639 : DO i = 2, logger%para_env%mepos + 1
150 639 : istart = istart + allcalc(i - 1)
151 : END DO
152 :
153 : ! reset extrapolation status
154 427 : nnp%output_expol = .FALSE.
155 :
156 427 : CALL nnp_prepare_neighbor_cache(nnp)
157 :
158 427 : max_input_nodes = 0
159 1313 : DO i = 1, nnp%n_ele
160 1313 : max_input_nodes = MAX(max_input_nodes, nnp%arc(i)%n_nodes(1))
161 : END DO
162 :
163 : ! The GPU path runs the whole per-rank step (neighbour walk, descriptors,
164 : ! network and force assembly) on the device. It handles energies, forces
165 : ! and, when a stress tensor is requested, the analytical virial the
166 : ! scatter kernels accumulate alongside the forces. Energy-only evaluations
167 : ! always use the CPU loop.
168 427 : use_gpu_path = (nnp%use_gpu == nnp_use_gpu_on) .AND. calc_forces
169 : ! VALIDATE runs the CPU reference in shadow mode against the device
170 : ! results, so both paths execute on a validating evaluation.
171 0 : validate = use_gpu_path .AND. nnp%validate_gpu
172 427 : run_cpu_path = (.NOT. use_gpu_path) .OR. validate
173 :
174 : ! number of threads that will run the atom loop below. The per-thread
175 : ! accumulator slabs belong to the CPU loop: the device kernels scatter
176 : ! straight into nnp%myforce and nnp%committee_stress.
177 427 : nthreads = 1
178 427 : !$ nthreads = omp_get_max_threads()
179 427 : IF (calc_forces .AND. run_cpu_path) THEN
180 : ! dGdr lives in per-element workspace; no global (3, n_sf, num_atoms) slab.
181 1365 : ALLOCATE (my_force_t(3, nnp%num_atoms, nnp%n_committee, 0:nthreads - 1))
182 273 : my_force_t(:, :, :, :) = 0.0_dp
183 273 : IF (calc_stress) THEN
184 188 : ALLOCATE (my_cstress_t(3, 3, nnp%n_committee, 0:nthreads - 1))
185 47 : my_cstress_t(:, :, :, :) = 0.0_dp
186 : END IF
187 : END IF
188 :
189 : ! Per-rank timer around the per-atom work. CP2K aggregates the label as
190 : ! avg/max across ranks, so max - avg measures the load imbalance.
191 427 : CALL timeset('nnp_per_rank_atom_loop', handle_loop)
192 :
193 427 : IF (use_gpu_path) THEN
194 0 : CALL nnp_gpu_energy_force(nnp, istart, mecalc, max_input_nodes, calc_stress)
195 0 : IF (validate) THEN
196 : ! Snapshot the device results and clear the accumulators, so the
197 : ! CPU reference below rebuilds them from scratch for the comparison.
198 0 : ALLOCATE (val_energy, SOURCE=nnp%atomic_energy)
199 0 : ALLOCATE (val_force, SOURCE=nnp%myforce)
200 0 : IF (calc_stress) ALLOCATE (val_stress, SOURCE=nnp%committee_stress)
201 0 : nnp%atomic_energy(:, :) = 0.0_dp
202 0 : nnp%myforce(:, :, :) = 0.0_dp
203 0 : IF (calc_stress) nnp%committee_stress(:, :, :) = 0.0_dp
204 : END IF
205 : END IF
206 427 : IF (run_cpu_path) THEN
207 :
208 : ! Thread the loop over atomic centres. Each thread works on its own copy
209 : ! of the networks (my_arc), its own workspace column (selected by
210 : ! omp_get_thread_num inside the ACSF routines) and its own slab of
211 : ! my_force_t/my_cstress_t. SCHEDULE(STATIC) hands out contiguous atom
212 : ! blocks in thread order, so the ordered sum over threads after the
213 : ! region reproduces the serial summation order exactly.
214 : !$OMP PARALLEL DEFAULT(NONE) &
215 : !$OMP SHARED(calc_forces, calc_stress, istart, max_input_nodes, mecalc, &
216 : !$OMP my_cstress_t, my_force_t, nnp) &
217 427 : !$OMP PRIVATE(i, i_com, ind, ie, il, j, n_input_nodes, tid, my_arc, denergydsym, stress)
218 :
219 : tid = 1
220 : !$ tid = omp_get_thread_num() + 1
221 :
222 : ! Copy the networks for this thread. ALLOCATE with SOURCE= rather than
223 : ! plain assignment: allocate-on-assignment to an ALLOCATABLE is what
224 : ! -Werror=realloc-lhs rejects in the debug builds. SOURCE= copies the
225 : ! shape, the bounds and the values, so the copy is structurally identical
226 : ! to the original either way. %layer itself is a POINTER array and is
227 : ! allocated and freed explicitly.
228 : ALLOCATE (my_arc(nnp%n_ele))
229 : DO ie = 1, nnp%n_ele
230 : ALLOCATE (my_arc(ie)%n_nodes, SOURCE=nnp%arc(ie)%n_nodes)
231 : ALLOCATE (my_arc(ie)%layer(nnp%n_layer))
232 : DO il = 1, nnp%n_layer
233 : IF (ALLOCATED(nnp%arc(ie)%layer(il)%weights)) THEN
234 : ALLOCATE (my_arc(ie)%layer(il)%weights, SOURCE=nnp%arc(ie)%layer(il)%weights)
235 : END IF
236 : IF (ALLOCATED(nnp%arc(ie)%layer(il)%bweights)) THEN
237 : ALLOCATE (my_arc(ie)%layer(il)%bweights, SOURCE=nnp%arc(ie)%layer(il)%bweights)
238 : END IF
239 : IF (ALLOCATED(nnp%arc(ie)%layer(il)%node)) THEN
240 : ALLOCATE (my_arc(ie)%layer(il)%node, SOURCE=nnp%arc(ie)%layer(il)%node)
241 : END IF
242 : IF (ALLOCATED(nnp%arc(ie)%layer(il)%node_grad)) THEN
243 : ALLOCATE (my_arc(ie)%layer(il)%node_grad, SOURCE=nnp%arc(ie)%layer(il)%node_grad)
244 : END IF
245 : IF (ALLOCATED(nnp%arc(ie)%layer(il)%tmp_der)) THEN
246 : ALLOCATE (my_arc(ie)%layer(il)%tmp_der, SOURCE=nnp%arc(ie)%layer(il)%tmp_der)
247 : END IF
248 : END DO
249 : END DO
250 :
251 : IF (calc_forces) THEN
252 : ALLOCATE (denergydsym(max_input_nodes))
253 : denergydsym(:) = 0.0_dp
254 : IF (calc_stress) THEN
255 : ALLOCATE (stress(3, 3, max_input_nodes))
256 : stress(:, :, :) = 0.0_dp
257 : END IF
258 : END IF
259 :
260 : ! calc atomic contribution to energy and force
261 : !$OMP DO SCHEDULE(STATIC)
262 : DO i = istart, istart + mecalc - 1
263 :
264 : ! determine index of atom type and offset
265 : ind = nnp%ele_ind(i)
266 : n_input_nodes = nnp%arc(ind)%n_nodes(1)
267 :
268 : ! reset input nodes of ele(ind):
269 : my_arc(ind)%layer(1)%node(:) = 0.0_dp
270 :
271 : ! compute sym fnct values
272 : IF (calc_forces) THEN
273 : !reset input grads of ele(ind):
274 : my_arc(ind)%layer(1)%node_grad(:) = 0.0_dp
275 : IF (calc_stress) THEN
276 : stress(:, :, 1:n_input_nodes) = 0.0_dp
277 : CALL nnp_calc_acsf(nnp, i, .TRUE., my_arc(ind), stress(:, :, 1:n_input_nodes))
278 : ELSE
279 : CALL nnp_calc_acsf(nnp, i, .TRUE., my_arc(ind))
280 : END IF
281 : ELSE
282 : CALL nnp_calc_acsf(nnp, i, .FALSE., my_arc(ind))
283 : END IF
284 :
285 : DO i_com = 1, nnp%n_committee
286 : ! predict energy
287 : CALL nnp_predict(my_arc(ind), nnp, i_com)
288 : nnp%atomic_energy(i, i_com) = my_arc(ind)%layer(nnp%n_layer)%node(1) + &
289 : nnp%atom_energies(ind)
290 :
291 : ! predict forces
292 : IF (calc_forces) THEN
293 : denergydsym(1:n_input_nodes) = 0.0_dp
294 : CALL nnp_gradients(my_arc(ind), nnp, i_com, denergydsym(1:n_input_nodes))
295 :
296 : ! Force scatter into this thread's private accumulator slab; see
297 : ! nnp_scatter_dgdr_to_forces below, also reused by the helium-NNP
298 : ! coupling in src/motion/helium_interactions.F.
299 : CALL nnp_scatter_dgdr_to_forces(nnp, ind, i, denergydsym(1:n_input_nodes), &
300 : my_force_t(:, :, i_com, tid - 1))
301 :
302 : IF (calc_stress) THEN
303 : DO j = 1, n_input_nodes
304 : my_cstress_t(:, :, i_com, tid - 1) = my_cstress_t(:, :, i_com, tid - 1) - &
305 : denergydsym(j)*stress(:, :, j)
306 : END DO
307 : END IF
308 : END IF
309 : END DO
310 :
311 : END DO ! loop over num_atoms
312 : !$OMP END DO
313 :
314 : IF (calc_forces) THEN
315 : DEALLOCATE (denergydsym)
316 : IF (calc_stress) DEALLOCATE (stress)
317 : END IF
318 : ! Free this thread's copy of the networks. %layer is a POINTER array, and
319 : ! deallocating a pointer array does NOT release the ALLOCATABLE components
320 : ! of its elements, so each one is released explicitly. Without this the
321 : ! whole per-thread network copy leaks once per thread per MD step; the
322 : ! LeakSanitizer in CP2K's pdbg CI build reports it. (%n_nodes needs no such
323 : ! treatment: my_arc is ALLOCATABLE, and deallocating an allocatable does
324 : ! cascade to its allocatable components.)
325 : DO ie = 1, nnp%n_ele
326 : DO il = 1, nnp%n_layer
327 : IF (ALLOCATED(my_arc(ie)%layer(il)%weights)) DEALLOCATE (my_arc(ie)%layer(il)%weights)
328 : IF (ALLOCATED(my_arc(ie)%layer(il)%bweights)) DEALLOCATE (my_arc(ie)%layer(il)%bweights)
329 : IF (ALLOCATED(my_arc(ie)%layer(il)%node)) DEALLOCATE (my_arc(ie)%layer(il)%node)
330 : IF (ALLOCATED(my_arc(ie)%layer(il)%node_grad)) DEALLOCATE (my_arc(ie)%layer(il)%node_grad)
331 : IF (ALLOCATED(my_arc(ie)%layer(il)%tmp_der)) DEALLOCATE (my_arc(ie)%layer(il)%tmp_der)
332 : END DO
333 : DEALLOCATE (my_arc(ie)%layer)
334 : END DO
335 : DEALLOCATE (my_arc)
336 : !$OMP END PARALLEL
337 :
338 : ! Sum the per-thread slabs in ascending thread order. Together with the
339 : ! static schedule above this keeps the summation order of the serial
340 : ! loop, so forces are bit-identical to serial for any thread count.
341 427 : IF (calc_forces) THEN
342 546 : DO t = 0, nthreads - 1
343 1563360 : nnp%myforce(:, :, :) = nnp%myforce(:, :, :) + my_force_t(:, :, :, t)
344 : END DO
345 273 : DEALLOCATE (my_force_t)
346 273 : IF (calc_stress) THEN
347 94 : DO t = 0, nthreads - 1
348 4982 : nnp%committee_stress(:, :, :) = nnp%committee_stress(:, :, :) + my_cstress_t(:, :, :, t)
349 : END DO
350 47 : DEALLOCATE (my_cstress_t)
351 : END IF
352 : END IF
353 :
354 : END IF
355 :
356 427 : CALL timestop(handle_loop)
357 :
358 : ! Compare the CPU reference built above against the device snapshot, then
359 : ! continue with the device results.
360 427 : IF (validate) THEN
361 0 : IF (calc_stress) THEN
362 0 : CALL nnp_gpu_validate(nnp, logger, val_energy, val_force, val_stress)
363 : ELSE
364 0 : CALL nnp_gpu_validate(nnp, logger, val_energy, val_force)
365 : END IF
366 : END IF
367 :
368 : ! calculate energy:
369 427 : CALL logger%para_env%sum(nnp%atomic_energy(:, :))
370 630345 : nnp%committee_energy(:) = SUM(nnp%atomic_energy, 1)
371 3577 : nnp%nnp_potential_energy = SUM(nnp%committee_energy)/REAL(nnp%n_committee, dp)
372 :
373 427 : IF (calc_forces) THEN
374 : ! bring myforce to force array
375 55379 : DO j = 1, nnp%num_atoms
376 220697 : DO k = 1, 3
377 1391096 : nnp%committee_forces(k, (nnp%sort(j)), :) = nnp%myforce(k, j, :)
378 : END DO
379 : END DO
380 273 : CALL logger%para_env%sum(nnp%committee_forces)
381 1391369 : nnp%nnp_forces(:, :) = SUM(nnp%committee_forces, DIM=3)/REAL(nnp%n_committee, dp)
382 55379 : DO j = 1, nnp%num_atoms
383 386015 : particle_set(j)%f(:) = nnp%nnp_forces(:, j)
384 : END DO
385 : END IF
386 :
387 427 : IF (calc_stress) THEN
388 47 : CALL logger%para_env%sum(nnp%committee_stress)
389 4559 : virial%pv_virial = SUM(nnp%committee_stress, DIM=3)/REAL(nnp%n_committee, dp)
390 : END IF
391 :
392 : ! Bias the standard deviation of committee disagreement
393 427 : IF (nnp%bias) THEN
394 44 : CALL nnp_bias_sigma(nnp, calc_forces)
395 44 : nnp%nnp_potential_energy = nnp%nnp_potential_energy + nnp%bias_energy
396 44 : IF (calc_forces) THEN
397 8492 : DO j = 1, nnp%num_atoms
398 59180 : particle_set(j)%f(:) = particle_set(j)%f(:) + nnp%bias_forces(:, j)
399 : END DO
400 : END IF
401 : ! print properties if requested
402 44 : print_section => section_vals_get_subs_vals(nnp%nnp_input, "BIAS%PRINT")
403 44 : CALL nnp_print_bias(nnp, print_section)
404 : END IF
405 :
406 : ! print properties if requested
407 427 : print_section => section_vals_get_subs_vals(nnp%nnp_input, "PRINT")
408 427 : CALL nnp_print(nnp, print_section)
409 :
410 427 : DEALLOCATE (allcalc)
411 :
412 427 : CALL timestop(handle)
413 :
414 1281 : END SUBROUTINE nnp_calc_energy_force
415 :
416 : ! **************************************************************************************************
417 : !> \brief Compare the CPU reference results against the device results, keep
418 : !> the device results, and abort when the two disagree.
419 : !>
420 : !> Runs on the rank-local accumulators, before any parallel reduction: the
421 : !> per-atom committee energies, the per-rank force contributions, and, when a
422 : !> stress tensor is requested, the per-rank virial contributions. The metric is
423 : !> the relative difference floored at one, so small values are compared
424 : !> absolutely. The atomic additions of the device descriptor accumulation give
425 : !> its results a scheduling-dependent summation order, which bounds the
426 : !> agreement of the all-FP64 path; the FP32 angular symmetry functions of the
427 : !> MIXED path perturb the results much further, so its tolerances are looser.
428 : !>
429 : !> \param nnp NNP environment holding the CPU results; overwritten with
430 : !> the device results on success.
431 : !> \param logger Default logger, for its parallel environment.
432 : !> \param gpu_energy Device per-atom committee energies.
433 : !> \param gpu_force Device per-rank force contributions.
434 : !> \param gpu_stress Device per-rank virial contributions; present exactly when
435 : !> the evaluation computed a stress tensor.
436 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
437 : ! **************************************************************************************************
438 0 : SUBROUTINE nnp_gpu_validate(nnp, logger, gpu_energy, gpu_force, gpu_stress)
439 : TYPE(nnp_type), INTENT(INOUT) :: nnp
440 : TYPE(cp_logger_type), POINTER :: logger
441 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: gpu_energy
442 : REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: gpu_force
443 : REAL(KIND=dp), DIMENSION(:, :, :), INTENT(IN), &
444 : OPTIONAL :: gpu_stress
445 :
446 : CHARACTER(len=*), PARAMETER :: routineN = 'nnp_gpu_validate'
447 :
448 : CHARACTER(LEN=2*default_string_length) :: message
449 : INTEGER :: handle, unit_nr
450 : REAL(KIND=dp) :: diff_energy, diff_force, diff_stress, &
451 : tol_energy, tol_force
452 :
453 0 : CALL timeset(routineN, handle)
454 :
455 0 : IF (nnp%gpu_precision == nnp_gpu_prec_mixed) THEN
456 0 : tol_energy = 1.0E-5_dp
457 0 : tol_force = 1.0E-4_dp
458 : ELSE
459 0 : tol_energy = 1.0E-9_dp
460 0 : tol_force = 1.0E-8_dp
461 : END IF
462 :
463 : diff_energy = MAXVAL(ABS(nnp%atomic_energy - gpu_energy)/ &
464 0 : MAX(1.0_dp, ABS(nnp%atomic_energy)))
465 : diff_force = MAXVAL(ABS(nnp%myforce - gpu_force)/ &
466 0 : MAX(1.0_dp, ABS(nnp%myforce)))
467 0 : diff_stress = 0.0_dp
468 0 : IF (PRESENT(gpu_stress)) THEN
469 : diff_stress = MAXVAL(ABS(nnp%committee_stress - gpu_stress)/ &
470 0 : MAX(1.0_dp, ABS(nnp%committee_stress)))
471 : END IF
472 :
473 : ! Reduce before deciding, so every rank reaches the same verdict.
474 0 : CALL logger%para_env%max(diff_energy)
475 0 : CALL logger%para_env%max(diff_force)
476 0 : CALL logger%para_env%max(diff_stress)
477 :
478 0 : IF (diff_energy > tol_energy .OR. diff_force > tol_force .OR. &
479 : diff_stress > tol_force) THEN
480 : WRITE (message, "(A,3ES11.3,A,2ES10.1)") &
481 : "NNP GPU validation failure. Max rel. diff (energies, forces, "// &
482 0 : "stress):", diff_energy, diff_force, diff_stress, &
483 0 : ", tolerances:", tol_energy, tol_force
484 0 : CPABORT(TRIM(message))
485 : END IF
486 :
487 0 : unit_nr = cp_logger_get_default_io_unit(logger)
488 0 : IF (unit_nr > 0) THEN
489 : WRITE (unit_nr, "(T2,A,3ES11.3)") &
490 0 : "NNP| Validated GPU force evaluation, max rel. diff: ", &
491 0 : diff_energy, diff_force, diff_stress
492 : END IF
493 :
494 : ! Keep the device results as the results of the run, matching an
495 : ! evaluation without validation.
496 0 : nnp%atomic_energy(:, :) = gpu_energy(:, :)
497 0 : nnp%myforce(:, :, :) = gpu_force(:, :, :)
498 0 : IF (PRESENT(gpu_stress)) nnp%committee_stress(:, :, :) = gpu_stress(:, :, :)
499 :
500 0 : CALL timestop(handle)
501 :
502 0 : END SUBROUTINE nnp_gpu_validate
503 :
504 : ! **************************************************************************************************
505 : !> \brief Calculate bias potential and force based on standard deviation of committee disagreement
506 : !> \param nnp ...
507 : !> \param calc_forces ...
508 : !> \date 2020-10-10
509 : !> \author Christoph Schran (christoph.schran@rub.de)
510 : ! **************************************************************************************************
511 44 : SUBROUTINE nnp_bias_sigma(nnp, calc_forces)
512 : TYPE(nnp_type), INTENT(INOUT) :: nnp
513 : LOGICAL, INTENT(IN) :: calc_forces
514 :
515 : CHARACTER(len=*), PARAMETER :: routineN = 'nnp_bias_sigma'
516 :
517 : INTEGER :: handle, i
518 : REAL(KIND=dp) :: avrg, pref, sigma
519 :
520 44 : CALL timeset(routineN, handle)
521 :
522 : ! init
523 44 : sigma = 0.0_dp
524 44 : nnp%bias_energy = 0.0_dp
525 33836 : IF (calc_forces) nnp%bias_forces = 0.0_dp
526 :
527 : ! Subtract reference energy of each committee member, if requested
528 44 : IF (nnp%bias_align) THEN
529 : ! committee energy not used afterward, therefore overwritten
530 396 : nnp%committee_energy(:) = nnp%committee_energy(:) - nnp%bias_e_avrg(:)
531 : END IF
532 :
533 : ! <E> = 1/n sum(E_i)
534 : ! sigma = sqrt(1/n sum((E_i - <E>)**2))
535 : ! = sqrt(1/n sum(dE_i**2))
536 396 : avrg = SUM(nnp%committee_energy)/REAL(nnp%n_committee, dp)
537 396 : DO i = 1, nnp%n_committee
538 396 : sigma = sigma + (nnp%committee_energy(i) - avrg)**2
539 : END DO
540 44 : sigma = SQRT(sigma/REAL(nnp%n_committee, dp))
541 44 : nnp%bias_sigma = sigma
542 :
543 44 : IF (sigma > nnp%bias_sigma0) THEN
544 : ! E_b = 0.5 * kb * (sigma - sigma_0)**2
545 44 : nnp%bias_energy = 0.5_dp*nnp%bias_kb*(sigma - nnp%bias_sigma0)**2
546 :
547 44 : IF (calc_forces) THEN
548 : ! nabla(E_b) = kb*(sigma - sigma_0)*nabla(sigma)
549 : ! nabla(sigma) = 1/sigma * 1/n sum(dE_i* nabla(dE_i))
550 : ! nabla(dE_i) = nabla(E_i) - nabla(<E>)
551 44 : pref = nnp%bias_kb*(1.0_dp - nnp%bias_sigma0/sigma)
552 396 : DO i = 1, nnp%n_committee
553 : nnp%bias_forces(:, :) = nnp%bias_forces(:, :) + &
554 : (nnp%committee_energy(i) - avrg)* &
555 270732 : (nnp%committee_forces(:, :, i) - nnp%nnp_forces(:, :))
556 : END DO
557 44 : pref = pref/REAL(nnp%n_committee, dp)
558 33836 : nnp%bias_forces(:, :) = nnp%bias_forces(:, :)*pref
559 : END IF
560 : END IF
561 :
562 44 : CALL timestop(handle)
563 :
564 44 : END SUBROUTINE nnp_bias_sigma
565 :
566 : ! **************************************************************************************************
567 : !> \brief Print properties according to the requests in input file
568 : !> \param nnp ...
569 : !> \param print_section ...
570 : !> \date 2020-10-10
571 : !> \author Christoph Schran (christoph.schran@rub.de)
572 : ! **************************************************************************************************
573 854 : SUBROUTINE nnp_print(nnp, print_section)
574 : TYPE(nnp_type), INTENT(INOUT) :: nnp
575 : TYPE(section_vals_type), INTENT(IN), POINTER :: print_section
576 :
577 : INTEGER :: unit_nr
578 : LOGICAL :: explicit, file_is_new
579 : TYPE(cp_logger_type), POINTER :: logger
580 : TYPE(section_vals_type), POINTER :: print_key
581 :
582 427 : NULLIFY (logger, print_key)
583 427 : logger => cp_get_default_logger()
584 :
585 427 : print_key => section_vals_get_subs_vals(print_section, "ENERGIES")
586 427 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
587 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".data", &
588 398 : middle_name="nnp-energies", is_new_file=file_is_new)
589 398 : IF (unit_nr > 0) CALL nnp_print_energies(nnp, unit_nr, file_is_new)
590 398 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
591 : END IF
592 :
593 427 : print_key => section_vals_get_subs_vals(print_section, "FORCES")
594 427 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
595 374 : CALL nnp_print_forces(nnp, print_key)
596 : END IF
597 :
598 427 : print_key => section_vals_get_subs_vals(print_section, "FORCES_SIGMA")
599 427 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
600 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".xyz", &
601 352 : middle_name="nnp-forces-std")
602 352 : IF (unit_nr > 0) CALL nnp_print_force_sigma(nnp, unit_nr)
603 352 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
604 : END IF
605 :
606 : ! Output structures with extrapolation warning on any processor
607 427 : CALL logger%para_env%sum(nnp%output_expol)
608 427 : IF (nnp%output_expol) THEN
609 22 : print_key => section_vals_get_subs_vals(print_section, "EXTRAPOLATION")
610 22 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
611 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".xyz", &
612 22 : middle_name="nnp-extrapolation")
613 22 : IF (unit_nr > 0) CALL nnp_print_expol(nnp, unit_nr)
614 22 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
615 : END IF
616 : END IF
617 :
618 427 : print_key => section_vals_get_subs_vals(print_section, "SUM_FORCE")
619 :
620 : CALL section_vals_val_get(print_section, "SUM_FORCE%ATOM_LIST", &
621 427 : explicit=explicit)
622 427 : IF (explicit) THEN
623 0 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
624 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".dat", &
625 0 : middle_name="nnp-sumforce", is_new_file=file_is_new)
626 0 : IF (unit_nr > 0) CALL nnp_print_sumforces(nnp, print_section, unit_nr, file_is_new)
627 0 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
628 : END IF
629 : END IF
630 :
631 427 : END SUBROUTINE nnp_print
632 :
633 : ! **************************************************************************************************
634 : !> \brief Print NNP energies and standard deviation sigma
635 : !> \param nnp ...
636 : !> \param unit_nr ...
637 : !> \param file_is_new ...
638 : !> \date 2020-10-10
639 : !> \author Christoph Schran (christoph.schran@rub.de)
640 : ! **************************************************************************************************
641 199 : SUBROUTINE nnp_print_energies(nnp, unit_nr, file_is_new)
642 : TYPE(nnp_type), INTENT(INOUT) :: nnp
643 : INTEGER, INTENT(IN) :: unit_nr
644 : LOGICAL, INTENT(IN) :: file_is_new
645 :
646 : CHARACTER(LEN=12) :: fmt_string
647 : INTEGER :: i
648 : REAL(KIND=dp) :: std
649 :
650 199 : IF (file_is_new) THEN
651 11 : WRITE (unit_nr, "(A1,1X,A20)", ADVANCE='no') "#", "NNP Average [a.u.],"
652 11 : WRITE (unit_nr, "(A20)", ADVANCE='no') "NNP sigma [a.u.]"
653 92 : DO i = 1, nnp%n_committee
654 92 : WRITE (unit_nr, "(A17,I3)", ADVANCE='no') "NNP", i
655 : END DO
656 11 : WRITE (unit_nr, "(A)") ""
657 : END IF
658 :
659 199 : fmt_string = "(2X, F20.9)"
660 199 : WRITE (UNIT=fmt_string(5:6), FMT="(I2)") nnp%n_committee + 2
661 280570 : std = SUM((SUM(nnp%atomic_energy, 1) - nnp%nnp_potential_energy)**2)
662 199 : std = std/REAL(nnp%n_committee, dp)
663 199 : std = SQRT(std)
664 280570 : WRITE (unit_nr, fmt_string) nnp%nnp_potential_energy, std, SUM(nnp%atomic_energy, 1)
665 :
666 199 : END SUBROUTINE nnp_print_energies
667 :
668 : ! **************************************************************************************************
669 : !> \brief Print nnp forces
670 : !> \param nnp ...
671 : !> \param print_key ...
672 : !> \date 2020-10-10
673 : !> \author Christoph Schran (christoph.schran@rub.de)
674 : ! **************************************************************************************************
675 374 : SUBROUTINE nnp_print_forces(nnp, print_key)
676 : TYPE(nnp_type), INTENT(INOUT) :: nnp
677 : TYPE(section_vals_type), INTENT(IN), POINTER :: print_key
678 :
679 : CHARACTER(len=default_path_length) :: fmt_string, middle_name
680 : INTEGER :: i, j, unit_nr
681 : TYPE(cp_logger_type), POINTER :: logger
682 :
683 374 : NULLIFY (logger)
684 374 : logger => cp_get_default_logger()
685 :
686 3212 : DO i = 1, nnp%n_committee
687 2838 : WRITE (fmt_string, *) i
688 2838 : WRITE (middle_name, "(A,A)") "nnp-forces-", ADJUSTL(TRIM(fmt_string))
689 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".xyz", &
690 2838 : middle_name=TRIM(middle_name))
691 2838 : IF (unit_nr > 0) THEN
692 1419 : WRITE (unit_nr, *) nnp%num_atoms
693 1419 : WRITE (unit_nr, "(A,1X,A,A,F20.9)") "NNP forces [a.u.] of committee member", &
694 2838 : ADJUSTL(TRIM(fmt_string)), "energy [a.u.]=", nnp%committee_energy(i)
695 :
696 1419 : fmt_string = "(A4,1X,3F20.10)"
697 273867 : DO j = 1, nnp%num_atoms
698 273867 : WRITE (unit_nr, fmt_string) nnp%atoms(nnp%sort_inv(j)), nnp%committee_forces(:, j, i)
699 : END DO
700 : END IF
701 3212 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
702 : END DO
703 :
704 374 : END SUBROUTINE nnp_print_forces
705 :
706 : ! **************************************************************************************************
707 : !> \brief Print standard deviation sigma of NNP forces
708 : !> \param nnp ...
709 : !> \param unit_nr ...
710 : !> \date 2020-10-10
711 : !> \author Christoph Schran (christoph.schran@rub.de)
712 : ! **************************************************************************************************
713 176 : SUBROUTINE nnp_print_force_sigma(nnp, unit_nr)
714 : TYPE(nnp_type), INTENT(INOUT) :: nnp
715 : INTEGER, INTENT(IN) :: unit_nr
716 :
717 : INTEGER :: i, j
718 : REAL(KIND=dp), DIMENSION(3) :: var
719 :
720 176 : IF (unit_nr > 0) THEN
721 176 : WRITE (unit_nr, *) nnp%num_atoms
722 176 : WRITE (unit_nr, "(A,1X,A)") "NNP sigma of forces [a.u.]"
723 :
724 33968 : DO i = 1, nnp%num_atoms
725 33792 : var = 0.0_dp
726 304128 : DO j = 1, nnp%n_committee
727 1115136 : var = var + (nnp%committee_forces(:, i, j) - nnp%nnp_forces(:, i))**2
728 : END DO
729 135168 : var = var/REAL(nnp%n_committee, dp)
730 135168 : var = SQRT(var)
731 33968 : WRITE (unit_nr, "(A4,1X,3F20.10)") nnp%atoms(nnp%sort_inv(i)), var
732 : END DO
733 : END IF
734 :
735 176 : END SUBROUTINE nnp_print_force_sigma
736 :
737 : ! **************************************************************************************************
738 : !> \brief Print structures with extrapolation warning
739 : !> \param nnp ...
740 : !> \param unit_nr ...
741 : !> \date 2020-10-10
742 : !> \author Christoph Schran (christoph.schran@rub.de)
743 : ! **************************************************************************************************
744 11 : SUBROUTINE nnp_print_expol(nnp, unit_nr)
745 : TYPE(nnp_type), INTENT(INOUT) :: nnp
746 : INTEGER, INTENT(IN) :: unit_nr
747 :
748 : CHARACTER(len=default_path_length) :: fmt_string
749 : INTEGER :: i
750 : REAL(KIND=dp) :: mass, unit_conv
751 : REAL(KIND=dp), DIMENSION(3) :: com
752 :
753 11 : nnp%expol = nnp%expol + 1
754 11 : WRITE (unit_nr, *) nnp%num_atoms
755 11 : WRITE (unit_nr, "(A,1X,I6)") "NNP extrapolation point N =", nnp%expol
756 :
757 : ! move to COM of solute and wrap the box
758 : ! coord not needed afterwards, therefore manipulation ok
759 11 : com = 0.0_dp
760 11 : mass = 0.0_dp
761 44 : DO i = 1, nnp%num_atoms
762 33 : CALL get_ptable_info(nnp%atoms(i) (1:2), amass=unit_conv)
763 132 : com(:) = com(:) + nnp%coord(:, i)*unit_conv
764 77 : mass = mass + unit_conv
765 : END DO
766 44 : com(:) = com(:)/mass
767 :
768 44 : DO i = 1, nnp%num_atoms
769 143 : nnp%coord(:, i) = nnp%coord(:, i) - com(:)
770 : END DO
771 :
772 : ! write out coordinates
773 11 : unit_conv = cp_unit_from_cp2k(1.0_dp, TRIM("angstrom"))
774 11 : fmt_string = "(A4,1X,3F20.10)"
775 44 : DO i = 1, nnp%num_atoms
776 : WRITE (unit_nr, fmt_string) &
777 33 : nnp%atoms(nnp%sort_inv(i)), &
778 33 : nnp%coord(1, nnp%sort_inv(i))*unit_conv, &
779 33 : nnp%coord(2, nnp%sort_inv(i))*unit_conv, &
780 77 : nnp%coord(3, nnp%sort_inv(i))*unit_conv
781 : END DO
782 :
783 11 : END SUBROUTINE nnp_print_expol
784 :
785 : ! **************************************************************************************************
786 : !> \brief Print properties number according the requests in input file
787 : !> \param nnp ...
788 : !> \param print_section ...
789 : !> \date 2020-10-10
790 : !> \author Christoph Schran (christoph.schran@rub.de)
791 : ! **************************************************************************************************
792 44 : SUBROUTINE nnp_print_bias(nnp, print_section)
793 : TYPE(nnp_type), INTENT(INOUT) :: nnp
794 : TYPE(section_vals_type), INTENT(IN), POINTER :: print_section
795 :
796 : INTEGER :: unit_nr
797 : LOGICAL :: file_is_new
798 : TYPE(cp_logger_type), POINTER :: logger
799 : TYPE(section_vals_type), POINTER :: print_key
800 :
801 44 : NULLIFY (logger, print_key)
802 44 : logger => cp_get_default_logger()
803 :
804 44 : print_key => section_vals_get_subs_vals(print_section, "BIAS_ENERGY")
805 44 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
806 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".data", &
807 44 : middle_name="nnp-bias-energy", is_new_file=file_is_new)
808 44 : IF (unit_nr > 0) CALL nnp_print_bias_energy(nnp, unit_nr, file_is_new)
809 44 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
810 : END IF
811 :
812 44 : print_key => section_vals_get_subs_vals(print_section, "BIAS_FORCES")
813 44 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
814 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".xyz", &
815 0 : middle_name="nnp-bias-forces")
816 0 : IF (unit_nr > 0) CALL nnp_print_bias_forces(nnp, unit_nr)
817 0 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
818 : END IF
819 :
820 44 : END SUBROUTINE nnp_print_bias
821 :
822 : ! **************************************************************************************************
823 : !> \brief Print NNP bias energies
824 : !> \param nnp ...
825 : !> \param unit_nr ...
826 : !> \param file_is_new ...
827 : !> \date 2020-10-10
828 : !> \author Christoph Schran (christoph.schran@rub.de)
829 : ! **************************************************************************************************
830 22 : SUBROUTINE nnp_print_bias_energy(nnp, unit_nr, file_is_new)
831 : TYPE(nnp_type), INTENT(INOUT) :: nnp
832 : INTEGER, INTENT(IN) :: unit_nr
833 : LOGICAL, INTENT(IN) :: file_is_new
834 :
835 : CHARACTER(len=default_path_length) :: fmt_string
836 : INTEGER :: i
837 :
838 22 : IF (file_is_new) THEN
839 1 : WRITE (unit_nr, "(A1)", ADVANCE='no') "#"
840 1 : WRITE (unit_nr, "(2(2X,A19))", ADVANCE='no') "Sigma [a.u.]", "Bias energy [a.u.]"
841 9 : DO i = 1, nnp%n_committee
842 9 : IF (nnp%bias_align) THEN
843 8 : WRITE (unit_nr, "(2X,A16,I3)", ADVANCE='no') "shifted E_NNP", i
844 : ELSE
845 0 : WRITE (unit_nr, "(2X,A16,I3)", ADVANCE='no') "E_NNP", i
846 : END IF
847 : END DO
848 1 : WRITE (unit_nr, "(A)") ""
849 :
850 : END IF
851 :
852 22 : WRITE (fmt_string, "(A,I3,A)") "(2X,", nnp%n_committee + 2, "(F20.9,1X))"
853 22 : WRITE (unit_nr, fmt_string) nnp%bias_sigma, nnp%bias_energy, nnp%committee_energy
854 :
855 22 : END SUBROUTINE nnp_print_bias_energy
856 :
857 : ! **************************************************************************************************
858 : !> \brief Print NNP bias forces
859 : !> \param nnp ...
860 : !> \param unit_nr ...
861 : !> \date 2020-10-10
862 : !> \author Christoph Schran (christoph.schran@rub.de)
863 : ! **************************************************************************************************
864 0 : SUBROUTINE nnp_print_bias_forces(nnp, unit_nr)
865 : TYPE(nnp_type), INTENT(INOUT) :: nnp
866 : INTEGER, INTENT(IN) :: unit_nr
867 :
868 : CHARACTER(len=default_path_length) :: fmt_string
869 : INTEGER :: i
870 :
871 0 : WRITE (unit_nr, *) nnp%num_atoms
872 0 : WRITE (unit_nr, "(A,F20.9)") "NNP bias forces [a.u.] for bias energy [a.u]=", nnp%bias_energy
873 :
874 0 : fmt_string = "(A4,1X,3F20.10)"
875 0 : DO i = 1, nnp%num_atoms
876 0 : WRITE (unit_nr, fmt_string) nnp%atoms(nnp%sort_inv(i)), nnp%bias_forces(:, i)
877 : END DO
878 :
879 0 : END SUBROUTINE nnp_print_bias_forces
880 :
881 : ! **************************************************************************************************
882 : !> \brief Print NNP summed forces
883 : !> \param nnp ...
884 : !> \param print_section ...
885 : !> \param unit_nr ...
886 : !> \param file_is_new ...
887 : !> \date 2020-10-10
888 : !> \author Christoph Schran (christoph.schran@rub.de)
889 : ! **************************************************************************************************
890 0 : SUBROUTINE nnp_print_sumforces(nnp, print_section, unit_nr, file_is_new)
891 : TYPE(nnp_type), INTENT(INOUT) :: nnp
892 : TYPE(section_vals_type), INTENT(IN), POINTER :: print_section
893 : INTEGER, INTENT(IN) :: unit_nr
894 : LOGICAL, INTENT(IN) :: file_is_new
895 :
896 : CHARACTER(len=default_path_length) :: fmt_string
897 : CHARACTER(LEN=default_string_length), &
898 0 : DIMENSION(:), POINTER :: atomlist
899 : INTEGER :: i, ig, j, n
900 : REAL(KIND=dp), DIMENSION(3) :: rvec
901 :
902 0 : NULLIFY (atomlist)
903 0 : IF (file_is_new) THEN
904 0 : WRITE (unit_nr, "(A)") "# Summed forces [a.u.]"
905 : END IF
906 :
907 0 : rvec = 0.0_dp
908 :
909 : ! get atoms to sum over:
910 : CALL section_vals_val_get(print_section, "SUM_FORCE%ATOM_LIST", &
911 0 : c_vals=atomlist)
912 0 : IF (ASSOCIATED(atomlist)) THEN
913 0 : n = SIZE(atomlist)
914 0 : DO i = 1, nnp%num_atoms
915 0 : DO j = 1, n
916 0 : ig = nnp%sort_inv(i)
917 0 : IF (TRIM(ADJUSTL(atomlist(j))) == TRIM(ADJUSTL(nnp%atoms(ig)))) THEN
918 0 : rvec(:) = rvec(:) + nnp%nnp_forces(:, i)
919 : END IF
920 : END DO
921 : END DO
922 : END IF
923 :
924 0 : fmt_string = "(3(F20.10,1X))"
925 0 : WRITE (unit_nr, fmt_string) rvec
926 :
927 0 : END SUBROUTINE nnp_print_sumforces
928 :
929 : ! **************************************************************************************************
930 : !> \brief Scatter the per-neighbour dG/dr arrays held in the nnp_neighbor_workspace
931 : !> into a per-atom Cartesian force array. Three contributions per central
932 : !> atom i:
933 : !> 1) self: self_dGdr(:, m) -> force_xyz(:, i)
934 : !> 2) radial: walk neighbor%rad(s)%ind(j), read dGdr_rad(s)%data(:, sf, j)
935 : !> 3) angular: walk neighbor%ang1(s)%ind / ang2(s)%ind, read dGdr_ang_jj/kk
936 : !> No global (n_sf, num_atoms) slab is touched. Reused by the helium-NNP
937 : !> coupling in helium_interactions.
938 : !>
939 : !> Precondition: nnp_calc_acsf(nnp, i, calc_forces=.TRUE.[, stress]) must
940 : !> have run for this same atom i immediately before; the per-element
941 : !> workspace it fills is overwritten on every ACSF call.
942 : !> \param nnp ...
943 : !> \param ind central-atom element index
944 : !> \param i central atom index (absolute)
945 : !> \param denergydsym dE/dG_k for k = 1..n_input_nodes(ind)
946 : !> \param force_xyz (3, num_atoms) destination -- accumulated, NOT overwritten
947 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
948 : ! **************************************************************************************************
949 203816 : SUBROUTINE nnp_scatter_dgdr_to_forces(nnp, ind, i, denergydsym, force_xyz)
950 :
951 : TYPE(nnp_type), INTENT(IN) :: nnp
952 : INTEGER, INTENT(IN) :: ind, i
953 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: denergydsym
954 : REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: force_xyz
955 :
956 : INTEGER :: j, k, k_atom, m, n_ang1_s, n_ang2_s, &
957 : n_input_nodes, n_symf_s, off, s, sf, &
958 : tid
959 : LOGICAL :: homo_grp
960 : REAL(KIND=dp) :: de
961 :
962 203816 : tid = 1
963 203816 : !$ tid = omp_get_thread_num() + 1
964 : ASSOCIATE (workspace => nnp%neighbor_interface_state%workspace(ind, tid), &
965 : neighbor => nnp%neighbor_interface_state%workspace(ind, tid)%neighbor, &
966 : self_dGdr => nnp%neighbor_interface_state%workspace(ind, tid)%self_dGdr)
967 :
968 203816 : n_input_nodes = workspace%n_input_nodes
969 :
970 : ! Self contribution
971 5940872 : DO j = 1, n_input_nodes
972 5737056 : de = denergydsym(j)
973 5737056 : force_xyz(1, i) = force_xyz(1, i) - de*self_dGdr(1, j)
974 5737056 : force_xyz(2, i) = force_xyz(2, i) - de*self_dGdr(2, j)
975 5940872 : force_xyz(3, i) = force_xyz(3, i) - de*self_dGdr(3, j)
976 : END DO
977 :
978 : ! Radial neighbours
979 617656 : DO s = 1, nnp%rad(ind)%n_symfgrp
980 413840 : n_symf_s = nnp%rad(ind)%symfgrp(s)%n_symf
981 203816 : ASSOCIATE (rad_buf => workspace%dGdr_rad(s)%data)
982 21405654 : DO j = 1, neighbor%n_rad(s)
983 20991814 : k_atom = neighbor%rad(s)%ind(j)
984 189630458 : DO sf = 1, n_symf_s
985 168224804 : m = nnp%rad(ind)%symfgrp(s)%symf(sf)
986 168224804 : de = denergydsym(m)
987 168224804 : force_xyz(1, k_atom) = force_xyz(1, k_atom) - de*rad_buf(1, sf, j)
988 168224804 : force_xyz(2, k_atom) = force_xyz(2, k_atom) - de*rad_buf(2, sf, j)
989 189216618 : force_xyz(3, k_atom) = force_xyz(3, k_atom) - de*rad_buf(3, sf, j)
990 : END DO
991 : END DO
992 : END ASSOCIATE
993 : END DO
994 :
995 : ! Angular neighbours
996 203816 : off = nnp%n_rad(ind)
997 892600 : DO s = 1, nnp%ang(ind)%n_symfgrp
998 484968 : n_symf_s = nnp%ang(ind)%symfgrp(s)%n_symf
999 484968 : n_ang1_s = neighbor%n_ang1(s)
1000 484968 : homo_grp = (nnp%ang(ind)%symfgrp(s)%ele(1) == nnp%ang(ind)%symfgrp(s)%ele(2))
1001 484968 : IF (homo_grp) THEN
1002 : n_ang2_s = n_ang1_s ! kk buffer is also indexed in ang1(s)%ind
1003 : ELSE
1004 214536 : n_ang2_s = neighbor%n_ang2(s)
1005 : END IF
1006 :
1007 : ASSOCIATE (jj_buf => workspace%dGdr_ang_jj(s)%data, &
1008 203816 : kk_buf => workspace%dGdr_ang_kk(s)%data)
1009 : ! jj-side
1010 26561605 : DO j = 1, n_ang1_s
1011 26076637 : k_atom = neighbor%ang1(s)%ind(j)
1012 167909745 : DO sf = 1, n_symf_s
1013 141348140 : m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
1014 141348140 : de = denergydsym(m)
1015 141348140 : force_xyz(1, k_atom) = force_xyz(1, k_atom) - de*jj_buf(1, sf, j)
1016 141348140 : force_xyz(2, k_atom) = force_xyz(2, k_atom) - de*jj_buf(2, sf, j)
1017 167424777 : force_xyz(3, k_atom) = force_xyz(3, k_atom) - de*jj_buf(3, sf, j)
1018 : END DO
1019 : END DO
1020 :
1021 : ! kk-side: homo reads ang1(s)%ind, hetero reads ang2(s)%ind
1022 969936 : IF (homo_grp) THEN
1023 12016906 : DO k = 1, n_ang2_s
1024 11746474 : k_atom = neighbor%ang1(s)%ind(k)
1025 68333974 : DO sf = 1, n_symf_s
1026 56317068 : m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
1027 56317068 : de = denergydsym(m)
1028 56317068 : force_xyz(1, k_atom) = force_xyz(1, k_atom) - de*kk_buf(1, sf, k)
1029 56317068 : force_xyz(2, k_atom) = force_xyz(2, k_atom) - de*kk_buf(2, sf, k)
1030 68063542 : force_xyz(3, k_atom) = force_xyz(3, k_atom) - de*kk_buf(3, sf, k)
1031 : END DO
1032 : END DO
1033 : ELSE
1034 7192947 : DO k = 1, n_ang2_s
1035 6978411 : k_atom = neighbor%ang2(s)%ind(k)
1036 49105356 : DO sf = 1, n_symf_s
1037 41912409 : m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
1038 41912409 : de = denergydsym(m)
1039 41912409 : force_xyz(1, k_atom) = force_xyz(1, k_atom) - de*kk_buf(1, sf, k)
1040 41912409 : force_xyz(2, k_atom) = force_xyz(2, k_atom) - de*kk_buf(2, sf, k)
1041 48890820 : force_xyz(3, k_atom) = force_xyz(3, k_atom) - de*kk_buf(3, sf, k)
1042 : END DO
1043 : END DO
1044 : END IF
1045 : END ASSOCIATE
1046 : END DO
1047 :
1048 : END ASSOCIATE
1049 :
1050 203816 : END SUBROUTINE nnp_scatter_dgdr_to_forces
1051 :
1052 : END MODULE nnp_force
|