41#include "./base/base_uses.f90"
47 LOGICAL,
PARAMETER,
PRIVATE :: debug_this_module = .true.
48 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'nnp_force'
62 TYPE(
nnp_type),
INTENT(INOUT),
POINTER :: nnp
63 LOGICAL,
INTENT(IN) :: calc_forces
65 CHARACTER(len=*),
PARAMETER :: routinen =
'nnp_calc_energy_force'
67 INTEGER :: handle, i, i_com, ig, ind, istart, j, k, &
69 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: allcalc
70 LOGICAL :: calc_stress
71 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: denergydsym
72 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:, :, :) :: dsymdxyz, stress
81 CALL timeset(routinen, handle)
83 NULLIFY (particle_set, logger, local_particles, subsys, &
87 cpassert(
ASSOCIATED(nnp))
88 CALL nnp_env_get(nnp_env=nnp, particle_set=particle_set, &
89 subsys=subsys, local_particles=local_particles, &
90 atomic_kind_set=atomic_kind_set)
95 calc_stress = virial%pv_availability .AND. (.NOT. virial%pv_numer)
96 IF (calc_stress .AND. .NOT. calc_forces) cpabort(
'Stress cannot be calculated without forces')
99 nnp%atomic_energy(:, :) = 0.0_dp
100 IF (calc_forces) nnp%myforce(:, :, :) = 0.0_dp
101 IF (calc_forces) nnp%committee_forces(:, :, :) = 0.0_dp
102 IF (calc_stress) nnp%committee_stress(:, :, :) = 0.0_dp
107 DO j = 1, nnp%num_atoms
108 IF (nnp%ele(i) == particle_set(j)%atomic_kind%element_symbol)
THEN
110 nnp%coord(m, ig) = particle_set(j)%r(m)
112 nnp%atoms(ig) = nnp%ele(i)
123 mecalc = nnp%num_atoms/logger%para_env%num_pe + &
124 min(mod(nnp%num_atoms, logger%para_env%num_pe)/ &
125 (logger%para_env%mepos + 1), 1)
126 ALLOCATE (allcalc(logger%para_env%num_pe))
128 CALL logger%para_env%allgather(mecalc, allcalc)
130 DO i = 2, logger%para_env%mepos + 1
131 istart = istart + allcalc(i - 1)
135 nnp%output_expol = .false.
138 DO i = istart, istart + mecalc - 1
144 nnp%arc(ind)%layer(1)%node(:) = 0.0_dp
147 IF (calc_forces)
THEN
149 nnp%arc(ind)%layer(1)%node_grad(:) = 0.0_dp
150 ALLOCATE (dsymdxyz(3, nnp%arc(ind)%n_nodes(1), nnp%num_atoms))
151 dsymdxyz(:, :, :) = 0.0_dp
152 IF (calc_stress)
THEN
153 ALLOCATE (stress(3, 3, nnp%arc(ind)%n_nodes(1)))
154 stress(:, :, :) = 0.0_dp
163 DO i_com = 1, nnp%n_committee
166 nnp%atomic_energy(i, i_com) = nnp%arc(ind)%layer(nnp%n_layer)%node(1) + &
167 nnp%atom_energies(ind)
170 IF (calc_forces)
THEN
171 ALLOCATE (denergydsym(nnp%arc(ind)%n_nodes(1)))
172 denergydsym(:) = 0.0_dp
174 DO j = 1, nnp%arc(ind)%n_nodes(1)
175 DO k = 1, nnp%num_atoms
177 nnp%myforce(m, k, i_com) = nnp%myforce(m, k, i_com) - denergydsym(j)*dsymdxyz(m, j, k)
180 IF (calc_stress)
THEN
181 nnp%committee_stress(:, :, i_com) = nnp%committee_stress(:, :, i_com) - &
182 denergydsym(j)*stress(:, :, j)
185 DEALLOCATE (denergydsym)
190 IF (calc_forces)
THEN
191 DEALLOCATE (dsymdxyz)
192 IF (calc_stress)
THEN
200 CALL logger%para_env%sum(nnp%atomic_energy(:, :))
201 nnp%committee_energy(:) = sum(nnp%atomic_energy, 1)
202 nnp%nnp_potential_energy = sum(nnp%committee_energy)/real(nnp%n_committee,
dp)
204 IF (calc_forces)
THEN
206 DO j = 1, nnp%num_atoms
208 nnp%committee_forces(k, (nnp%sort(j)), :) = nnp%myforce(k, j, :)
211 CALL logger%para_env%sum(nnp%committee_forces)
212 nnp%nnp_forces(:, :) = sum(nnp%committee_forces, dim=3)/real(nnp%n_committee,
dp)
213 DO j = 1, nnp%num_atoms
214 particle_set(j)%f(:) = nnp%nnp_forces(:, j)
218 IF (calc_stress)
THEN
219 CALL logger%para_env%sum(nnp%committee_stress)
220 virial%pv_virial = sum(nnp%committee_stress, dim=3)/real(nnp%n_committee,
dp)
225 CALL nnp_bias_sigma(nnp, calc_forces)
226 nnp%nnp_potential_energy = nnp%nnp_potential_energy + nnp%bias_energy
227 IF (calc_forces)
THEN
228 DO j = 1, nnp%num_atoms
229 particle_set(j)%f(:) = particle_set(j)%f(:) + nnp%bias_forces(:, j)
234 CALL nnp_print_bias(nnp, print_section)
239 CALL nnp_print(nnp, print_section)
243 CALL timestop(handle)
254 SUBROUTINE nnp_bias_sigma(nnp, calc_forces)
255 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
256 LOGICAL,
INTENT(IN) :: calc_forces
258 CHARACTER(len=*),
PARAMETER :: routinen =
'nnp_bias_sigma'
261 REAL(kind=
dp) :: avrg, pref, sigma
263 CALL timeset(routinen, handle)
267 nnp%bias_energy = 0.0_dp
268 IF (calc_forces) nnp%bias_forces = 0.0_dp
271 IF (nnp%bias_align)
THEN
273 nnp%committee_energy(:) = nnp%committee_energy(:) - nnp%bias_e_avrg(:)
279 avrg = sum(nnp%committee_energy)/real(nnp%n_committee,
dp)
280 DO i = 1, nnp%n_committee
281 sigma = sigma + (nnp%committee_energy(i) - avrg)**2
283 sigma = sqrt(sigma/real(nnp%n_committee,
dp))
284 nnp%bias_sigma = sigma
286 IF (sigma > nnp%bias_sigma0)
THEN
288 nnp%bias_energy = 0.5_dp*nnp%bias_kb*(sigma - nnp%bias_sigma0)**2
290 IF (calc_forces)
THEN
294 pref = nnp%bias_kb*(1.0_dp - nnp%bias_sigma0/sigma)
295 DO i = 1, nnp%n_committee
296 nnp%bias_forces(:, :) = nnp%bias_forces(:, :) + &
297 (nnp%committee_energy(i) - avrg)* &
298 (nnp%committee_forces(:, :, i) - nnp%nnp_forces(:, :))
300 pref = pref/real(nnp%n_committee,
dp)
301 nnp%bias_forces(:, :) = nnp%bias_forces(:, :)*pref
305 CALL timestop(handle)
307 END SUBROUTINE nnp_bias_sigma
316 SUBROUTINE nnp_print(nnp, print_section)
317 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
321 LOGICAL :: explicit, file_is_new
325 NULLIFY (logger, print_key)
331 middle_name=
"nnp-energies", is_new_file=file_is_new)
332 IF (unit_nr > 0)
CALL nnp_print_energies(nnp, unit_nr, file_is_new)
338 IF (unit_nr > 0)
CALL nnp_print_forces(nnp, print_key)
344 middle_name=
"nnp-forces-std")
345 IF (unit_nr > 0)
CALL nnp_print_force_sigma(nnp, unit_nr)
350 CALL logger%para_env%sum(nnp%output_expol)
351 IF (nnp%output_expol)
THEN
355 middle_name=
"nnp-extrapolation")
356 IF (unit_nr > 0)
CALL nnp_print_expol(nnp, unit_nr)
368 middle_name=
"nnp-sumforce", is_new_file=file_is_new)
369 IF (unit_nr > 0)
CALL nnp_print_sumforces(nnp, print_section, unit_nr, file_is_new)
374 END SUBROUTINE nnp_print
384 SUBROUTINE nnp_print_energies(nnp, unit_nr, file_is_new)
385 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
386 INTEGER,
INTENT(IN) :: unit_nr
387 LOGICAL,
INTENT(IN) :: file_is_new
389 CHARACTER(LEN=12) :: fmt_string
393 IF (file_is_new)
THEN
394 WRITE (unit_nr,
"(A1,1X,A20)", advance=
'no')
"#",
"NNP Average [a.u.],"
395 WRITE (unit_nr,
"(A20)", advance=
'no')
"NNP sigma [a.u.]"
396 DO i = 1, nnp%n_committee
397 WRITE (unit_nr,
"(A17,I3)", advance=
'no')
"NNP", i
399 WRITE (unit_nr,
"(A)")
""
402 fmt_string =
"(2X, F20.9)"
403 WRITE (unit=fmt_string(5:6), fmt=
"(I2)") nnp%n_committee + 2
404 std = sum((sum(nnp%atomic_energy, 1) - nnp%nnp_potential_energy)**2)
405 std = std/real(nnp%n_committee,
dp)
407 WRITE (unit_nr, fmt_string) nnp%nnp_potential_energy, std, sum(nnp%atomic_energy, 1)
409 END SUBROUTINE nnp_print_energies
418 SUBROUTINE nnp_print_forces(nnp, print_key)
419 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
422 CHARACTER(len=default_path_length) :: fmt_string, middle_name
423 INTEGER :: i, j, unit_nr
430 DO i = 1, nnp%n_committee
431 WRITE (fmt_string, *) i
432 WRITE (middle_name,
"(A,A)")
"nnp-forces-", adjustl(trim(fmt_string))
434 middle_name=trim(middle_name))
435 IF (unit_nr > 0)
THEN
436 WRITE (unit_nr, *) nnp%num_atoms
437 WRITE (unit_nr,
"(A,1X,A,A,F20.9)")
"NNP forces [a.u.] of committee member", &
438 adjustl(trim(fmt_string)),
"energy [a.u.]=", nnp%committee_energy(i)
440 fmt_string =
"(A4,1X,3F20.10)"
441 DO j = 1, nnp%num_atoms
442 WRITE (unit_nr, fmt_string) nnp%atoms(nnp%sort_inv(j)), nnp%committee_forces(:, j, i)
448 END SUBROUTINE nnp_print_forces
457 SUBROUTINE nnp_print_force_sigma(nnp, unit_nr)
458 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
459 INTEGER,
INTENT(IN) :: unit_nr
462 REAL(kind=
dp),
DIMENSION(3) :: var
464 IF (unit_nr > 0)
THEN
465 WRITE (unit_nr, *) nnp%num_atoms
466 WRITE (unit_nr,
"(A,1X,A)")
"NNP sigma of forces [a.u.]"
468 DO i = 1, nnp%num_atoms
470 DO j = 1, nnp%n_committee
471 var = var + (nnp%committee_forces(:, i, j) - nnp%nnp_forces(:, i))**2
473 var = var/real(nnp%n_committee,
dp)
475 WRITE (unit_nr,
"(A4,1X,3F20.10)") nnp%atoms(nnp%sort_inv(i)), var
479 END SUBROUTINE nnp_print_force_sigma
488 SUBROUTINE nnp_print_expol(nnp, unit_nr)
489 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
490 INTEGER,
INTENT(IN) :: unit_nr
492 CHARACTER(len=default_path_length) :: fmt_string
494 REAL(kind=
dp) :: mass, unit_conv
495 REAL(kind=
dp),
DIMENSION(3) :: com
497 nnp%expol = nnp%expol + 1
498 WRITE (unit_nr, *) nnp%num_atoms
499 WRITE (unit_nr,
"(A,1X,I6)")
"NNP extrapolation point N =", nnp%expol
505 DO i = 1, nnp%num_atoms
507 com(:) = com(:) + nnp%coord(:, i)*unit_conv
508 mass = mass + unit_conv
512 DO i = 1, nnp%num_atoms
513 nnp%coord(:, i) = nnp%coord(:, i) - com(:)
518 fmt_string =
"(A4,1X,3F20.10)"
519 DO i = 1, nnp%num_atoms
520 WRITE (unit_nr, fmt_string) &
521 nnp%atoms(nnp%sort_inv(i)), &
522 nnp%coord(1, nnp%sort_inv(i))*unit_conv, &
523 nnp%coord(2, nnp%sort_inv(i))*unit_conv, &
524 nnp%coord(3, nnp%sort_inv(i))*unit_conv
527 END SUBROUTINE nnp_print_expol
536 SUBROUTINE nnp_print_bias(nnp, print_section)
537 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
541 LOGICAL :: file_is_new
545 NULLIFY (logger, print_key)
551 middle_name=
"nnp-bias-energy", is_new_file=file_is_new)
552 IF (unit_nr > 0)
CALL nnp_print_bias_energy(nnp, unit_nr, file_is_new)
559 middle_name=
"nnp-bias-forces")
560 IF (unit_nr > 0)
CALL nnp_print_bias_forces(nnp, unit_nr)
564 END SUBROUTINE nnp_print_bias
574 SUBROUTINE nnp_print_bias_energy(nnp, unit_nr, file_is_new)
575 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
576 INTEGER,
INTENT(IN) :: unit_nr
577 LOGICAL,
INTENT(IN) :: file_is_new
579 CHARACTER(len=default_path_length) :: fmt_string
582 IF (file_is_new)
THEN
583 WRITE (unit_nr,
"(A1)", advance=
'no')
"#"
584 WRITE (unit_nr,
"(2(2X,A19))", advance=
'no')
"Sigma [a.u.]",
"Bias energy [a.u.]"
585 DO i = 1, nnp%n_committee
586 IF (nnp%bias_align)
THEN
587 WRITE (unit_nr,
"(2X,A16,I3)", advance=
'no')
"shifted E_NNP", i
589 WRITE (unit_nr,
"(2X,A16,I3)", advance=
'no')
"E_NNP", i
592 WRITE (unit_nr,
"(A)")
""
596 WRITE (fmt_string,
"(A,I3,A)")
"(2X,", nnp%n_committee + 2,
"(F20.9,1X))"
597 WRITE (unit_nr, fmt_string) nnp%bias_sigma, nnp%bias_energy, nnp%committee_energy
599 END SUBROUTINE nnp_print_bias_energy
608 SUBROUTINE nnp_print_bias_forces(nnp, unit_nr)
609 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
610 INTEGER,
INTENT(IN) :: unit_nr
612 CHARACTER(len=default_path_length) :: fmt_string
616 WRITE (unit_nr, *) nnp%num_atoms
617 WRITE (unit_nr,
"(A,F20.9)")
"NNP bias forces [a.u.] for bias energy [a.u]=", nnp%bias_energy
619 fmt_string =
"(A4,1X,3F20.10)"
620 DO i = 1, nnp%num_atoms
621 WRITE (unit_nr, fmt_string) nnp%atoms(nnp%sort_inv(i)), nnp%bias_forces(:, i)
624 END SUBROUTINE nnp_print_bias_forces
635 SUBROUTINE nnp_print_sumforces(nnp, print_section, unit_nr, file_is_new)
636 TYPE(
nnp_type),
INTENT(INOUT) :: nnp
638 INTEGER,
INTENT(IN) :: unit_nr
639 LOGICAL,
INTENT(IN) :: file_is_new
641 CHARACTER(len=default_path_length) :: fmt_string
642 CHARACTER(LEN=default_string_length), &
643 DIMENSION(:),
POINTER :: atomlist
644 INTEGER :: i, ig, j, n
645 REAL(kind=
dp),
DIMENSION(3) :: rvec
648 IF (file_is_new)
THEN
649 WRITE (unit_nr,
"(A)")
"# Summed forces [a.u.]"
657 IF (
ASSOCIATED(atomlist))
THEN
659 DO i = 1, nnp%num_atoms
662 IF (trim(adjustl(atomlist(j))) == trim(adjustl(nnp%atoms(ig))))
THEN
663 rvec(:) = rvec(:) + nnp%nnp_forces(:, i)
669 fmt_string =
"(3(F20.10,1X))"
670 WRITE (unit_nr, fmt_string) rvec
672 END SUBROUTINE nnp_print_sumforces
Define the atomic kind types and their sub types.
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
integer, parameter, public cp_p_file
integer function, public cp_print_key_should_output(iteration_info, basis_section, print_key_path, used_print_key, first_time)
returns what should be done with the given property if btest(res,cp_p_store) then the property should...
types that represent a subsys, i.e. a part of the system
subroutine, public cp_subsys_get(subsys, ref_count, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell)
returns information about various attributes of the given subsys
real(kind=dp) function, public cp_unit_from_cp2k(value, unit_str, defaults, power)
converts from the internal cp2k units to the given unit
stores a lists of integer that are local to a processor. The idea is that these integers represent ob...
Defines the basic variable types.
integer, parameter, public dp
integer, parameter, public default_string_length
integer, parameter, public default_path_length
Functionality for atom centered symmetry functions for neural network potentials.
subroutine, public nnp_calc_acsf(nnp, i, dsymdxyz, stress)
Calculate atom centered symmetry functions for given atom i.
Data types for neural network potentials.
subroutine, public nnp_env_get(nnp_env, nnp_forces, subsys, atomic_kind_set, particle_set, local_particles, molecule_kind_set, molecule_set, local_molecules, nnp_input, force_env_input, cell, cell_ref, use_ref_cell, nnp_potential_energy, virial)
Returns various attributes of the nnp environment.
Methods dealing with Neural Network potentials.
subroutine, public nnp_calc_energy_force(nnp, calc_forces)
Calculate the energy and force for a given configuration with the NNP.
Methods dealing with core routines for artificial neural networks.
subroutine, public nnp_predict(arc, nnp, i_com)
Predict energy by evaluating neural network.
subroutine, public nnp_gradients(arc, nnp, i_com, denergydsym)
Calculate gradients of neural network.
Define the data structure for the particle information.
Periodic Table related data definitions.
subroutine, public get_ptable_info(symbol, number, amass, ielement, covalent_radius, metallic_radius, vdw_radius, found)
Pass information about the kind given the element symbol.
Definition of physical constants:
real(kind=dp), parameter, public angstrom
Provides all information about an atomic kind.
type of a logger, at the moment it contains just a print level starting at which level it should be l...
represents a system: atoms, molecules, their pos,vel,...
structure to store local (to a processor) ordered lists of integers.
Main data type collecting all relevant data for neural network potentials.