(git:3add494)
manybody_deepmd.F
Go to the documentation of this file.
1 !--------------------------------------------------------------------------------------------------!
2 ! CP2K: A general program to perform molecular dynamics simulations !
3 ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 ! !
5 ! SPDX-License-Identifier: GPL-2.0-or-later !
6 !--------------------------------------------------------------------------------------------------!
7 
9 
10  USE atomic_kind_types, ONLY: atomic_kind_type
11  USE bibliography, ONLY: wang2018,&
12  zeng2023,&
13  cite_reference
14  USE cell_types, ONLY: cell_type
18  USE fist_nonbond_env_types, ONLY: deepmd_data_type,&
21  fist_nonbond_env_type
22  USE kinds, ONLY: dp
23  USE message_passing, ONLY: mp_para_env_type
25  pair_potential_pp_type,&
26  pair_potential_single_type
27  USE particle_types, ONLY: particle_type
28  USE physcon, ONLY: angstrom,&
29  evolt
30 #include "./base/base_uses.f90"
31 
32  IMPLICIT NONE
33 
34  PRIVATE
36 
37  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'manybody_deepmd'
38 
39 CONTAINS
40 
41 ! **************************************************************************************************
42 !> \brief ...
43 !> \param particle_set ...
44 !> \param cell ...
45 !> \param atomic_kind_set ...
46 !> \param potparm ...
47 !> \param fist_nonbond_env ...
48 !> \param pot_deepmd ...
49 !> \param para_env ...
50 ! **************************************************************************************************
51  SUBROUTINE deepmd_energy_store_force_virial(particle_set, cell, atomic_kind_set, potparm, fist_nonbond_env, &
52  pot_deepmd, para_env)
53  TYPE(particle_type), POINTER :: particle_set(:)
54  TYPE(cell_type), POINTER :: cell
55  TYPE(atomic_kind_type), POINTER :: atomic_kind_set(:)
56  TYPE(pair_potential_pp_type), POINTER :: potparm
57  TYPE(fist_nonbond_env_type), POINTER :: fist_nonbond_env
58  REAL(kind=dp) :: pot_deepmd
59  TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env
60 
61  CHARACTER(LEN=*), PARAMETER :: routinen = 'deepmd_energy_store_force_virial'
62 
63  INTEGER :: dpmd_natoms, handle, i, iat, iat_use, &
64  ikind, jkind, n_atoms, n_atoms_use, &
65  output_unit
66  INTEGER, ALLOCATABLE :: dpmd_atype(:), use_atom_type(:)
67  LOGICAL, ALLOCATABLE :: use_atom(:)
68  REAL(kind=dp) :: lattice(3, 3)
69  REAL(kind=dp), ALLOCATABLE :: dpmd_atomic_energy(:), &
70  dpmd_atomic_virial(:), dpmd_cell(:), &
71  dpmd_coord(:), dpmd_force(:), &
72  dpmd_virial(:)
73  TYPE(deepmd_data_type), POINTER :: deepmd_data
74  TYPE(pair_potential_single_type), POINTER :: pot
75 
76  CALL timeset(routinen, handle)
77  n_atoms = SIZE(particle_set)
78  ALLOCATE (use_atom(n_atoms))
79  ALLOCATE (use_atom_type(n_atoms))
80  use_atom = .false.
81  use_atom_type = 0
82 
83  DO ikind = 1, SIZE(atomic_kind_set)
84  DO jkind = 1, SIZE(atomic_kind_set)
85  pot => potparm%pot(ikind, jkind)%pot
86  ! ensure that each atom is only used once
87  IF (ikind /= jkind) cycle
88  DO i = 1, SIZE(pot%type)
89  IF (pot%type(i) /= deepmd_type) cycle
90  DO iat = 1, n_atoms
91  IF (particle_set(iat)%atomic_kind%kind_number == ikind .OR. &
92  particle_set(iat)%atomic_kind%kind_number == jkind) THEN
93  use_atom(iat) = .true.
94  use_atom_type(iat) = pot%set(i)%deepmd%atom_deepmd_type
95  END IF
96  END DO ! iat
97  END DO ! i
98  END DO ! jkind
99  END DO ! ikind
100 
101  n_atoms_use = count(use_atom)
102  dpmd_natoms = n_atoms_use
103  ALLOCATE (dpmd_cell(9), dpmd_atype(dpmd_natoms), dpmd_coord(dpmd_natoms*3), &
104  dpmd_force(dpmd_natoms*3), dpmd_virial(9), &
105  dpmd_atomic_energy(dpmd_natoms), dpmd_atomic_virial(dpmd_natoms*9))
106 
107  iat_use = 0
108  DO iat = 1, n_atoms
109  IF (.NOT. use_atom(iat)) cycle
110  iat_use = iat_use + 1
111  dpmd_coord((iat_use - 1)*3 + 1:(iat_use - 1)*3 + 3) = particle_set(iat)%r*angstrom
112  dpmd_atype(iat_use) = use_atom_type(iat)
113  END DO
114  IF (iat_use > 0) THEN
115  CALL cite_reference(wang2018)
116  CALL cite_reference(zeng2023)
117  END IF
118  output_unit = cp_logger_get_default_io_unit()
119  lattice = cell%hmat*angstrom
120 
121  ! change matrix to one d array
122  DO i = 1, 3
123  dpmd_cell((i - 1)*3 + 1:(i - 1)*3 + 3) = lattice(:, i)
124  END DO
125 
126  ! get deepmd_data to save force, virial info
127  CALL fist_nonbond_env_get(fist_nonbond_env, deepmd_data=deepmd_data)
128  IF (.NOT. ASSOCIATED(deepmd_data)) THEN
129  ALLOCATE (deepmd_data)
130  CALL fist_nonbond_env_set(fist_nonbond_env, deepmd_data=deepmd_data)
131  NULLIFY (deepmd_data%use_indices, deepmd_data%force)
132  deepmd_data%model = deepmd_model_load(filename=pot%set(1)%deepmd%deepmd_file_name)
133  END IF
134  IF (ASSOCIATED(deepmd_data%force)) THEN
135  IF (SIZE(deepmd_data%force, 2) /= n_atoms_use) THEN
136  DEALLOCATE (deepmd_data%force, deepmd_data%use_indices)
137  END IF
138  END IF
139  IF (.NOT. ASSOCIATED(deepmd_data%force)) THEN
140  ALLOCATE (deepmd_data%force(3, n_atoms_use))
141  ALLOCATE (deepmd_data%use_indices(n_atoms_use))
142  END IF
143 
144  CALL deepmd_model_compute(model=deepmd_data%model, &
145  natom=dpmd_natoms, &
146  coord=dpmd_coord, &
147  atype=dpmd_atype, &
148  cell=dpmd_cell, &
149  energy=pot_deepmd, &
150  force=dpmd_force, &
151  virial=dpmd_virial, &
152  atomic_energy=dpmd_atomic_energy, &
153  atomic_virial=dpmd_atomic_virial)
154 
155  ! convert units
156  pot_deepmd = pot_deepmd/evolt
157  dpmd_force = dpmd_force/(evolt/angstrom)
158  dpmd_virial = dpmd_virial/evolt
159 
160  ! account for double counting from multiple MPI processes
161  IF (PRESENT(para_env)) THEN
162  pot_deepmd = pot_deepmd/real(para_env%num_pe, dp)
163  dpmd_force = dpmd_force/real(para_env%num_pe, dp)
164  dpmd_virial = dpmd_virial/real(para_env%num_pe, dp)
165  END IF
166 
167  ! save force, virial info
168  iat_use = 0
169  DO iat = 1, n_atoms
170  IF (use_atom(iat)) THEN
171  iat_use = iat_use + 1
172  deepmd_data%use_indices(iat_use) = iat
173  deepmd_data%force(1:3, iat_use) = dpmd_force((iat_use - 1)*3 + 1:(iat_use - 1)*3 + 3)
174  END IF
175  END DO
176  DO i = 1, 3
177  deepmd_data%virial(1:3, i) = dpmd_virial((i - 1)*3 + 1:(i - 1)*3 + 3)
178  END DO
179  DEALLOCATE (use_atom, use_atom_type, dpmd_coord, dpmd_force, &
180  dpmd_virial, dpmd_atomic_energy, dpmd_atomic_virial, &
181  dpmd_cell, dpmd_atype)
182 
183  CALL timestop(handle)
184  END SUBROUTINE deepmd_energy_store_force_virial
185 
186 ! **************************************************************************************************
187 !> \brief ...
188 !> \param fist_nonbond_env ...
189 !> \param force ...
190 !> \param pv_nonbond ...
191 !> \param use_virial ...
192 ! **************************************************************************************************
193  SUBROUTINE deepmd_add_force_virial(fist_nonbond_env, force, pv_nonbond, use_virial)
194  TYPE(fist_nonbond_env_type), POINTER :: fist_nonbond_env
195  REAL(kind=dp) :: force(:, :), pv_nonbond(3, 3)
196  LOGICAL, OPTIONAL :: use_virial
197 
198  CHARACTER(LEN=*), PARAMETER :: routinen = 'deepmd_add_force_virial'
199 
200  INTEGER :: handle, iat, iat_use
201  TYPE(deepmd_data_type), POINTER :: deepmd_data
202 
203  CALL timeset(routinen, handle)
204 
205  CALL fist_nonbond_env_get(fist_nonbond_env, deepmd_data=deepmd_data)
206 
207  IF (.NOT. ASSOCIATED(deepmd_data)) RETURN
208 
209  DO iat_use = 1, SIZE(deepmd_data%use_indices)
210  iat = deepmd_data%use_indices(iat_use)
211  cpassert(iat >= 1 .AND. iat <= SIZE(force, 2))
212  force(1:3, iat) = force(1:3, iat) + deepmd_data%force(1:3, iat_use)
213  END DO
214 
215  IF (use_virial) THEN
216  pv_nonbond = pv_nonbond + deepmd_data%virial
217  END IF
218 
219  CALL timestop(handle)
220  END SUBROUTINE deepmd_add_force_virial
221 
222 END MODULE manybody_deepmd
Define the atomic kind types and their sub types.
collects all references to literature in CP2K as new algorithms / method are included from literature...
Definition: bibliography.F:28
integer, save, public zeng2023
Definition: bibliography.F:43
integer, save, public wang2018
Definition: bibliography.F:43
Handles all functions related to the CELL.
Definition: cell_types.F:15
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
Interface to the DeePMD-kit or a c++ wrapper.
type(deepmd_model_type) function, public deepmd_model_load(filename)
Load DP from a model file.
subroutine, public deepmd_model_compute(model, natom, coord, atype, cell, energy, force, virial, atomic_energy, atomic_virial)
Compute energy, force and virial from DP.
subroutine, public fist_nonbond_env_get(fist_nonbond_env, potparm14, potparm, nonbonded, rlist_cut, rlist_lowsq, aup, lup, ei_scale14, vdw_scale14, shift_cutoff, do_electrostatics, r_last_update, r_last_update_pbc, rshell_last_update_pbc, rcore_last_update_pbc, cell_last_update, num_update, last_update, counter, natom_types, long_range_correction, ij_kind_full_fac, eam_data, quip_data, nequip_data, allegro_data, deepmd_data, charges)
sets a fist_nonbond_env
subroutine, public fist_nonbond_env_set(fist_nonbond_env, potparm14, potparm, rlist_cut, rlist_lowsq, nonbonded, aup, lup, ei_scale14, vdw_scale14, shift_cutoff, do_electrostatics, r_last_update, r_last_update_pbc, rshell_last_update_pbc, rcore_last_update_pbc, cell_last_update, num_update, last_update, counter, natom_types, long_range_correction, eam_data, quip_data, nequip_data, allegro_data, deepmd_data, charges)
sets a fist_nonbond_env
Defines the basic variable types.
Definition: kinds.F:23
integer, parameter, public dp
Definition: kinds.F:34
subroutine, public deepmd_energy_store_force_virial(particle_set, cell, atomic_kind_set, potparm, fist_nonbond_env, pot_deepmd, para_env)
...
subroutine, public deepmd_add_force_virial(fist_nonbond_env, force, pv_nonbond, use_virial)
...
Interface to the message passing library MPI.
integer, parameter, public deepmd_type
Define the data structure for the particle information.
Definition of physical constants:
Definition: physcon.F:68
real(kind=dp), parameter, public evolt
Definition: physcon.F:183
real(kind=dp), parameter, public angstrom
Definition: physcon.F:144