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 interaction potential
10 : !> \author Laura Duran
11 : !> \date 2023-02-17
12 : ! **************************************************************************************************
13 : MODULE helium_nnp
14 :
15 : USE bibliography, ONLY: Behler2007,&
16 : Behler2011,&
17 : Schran2020a,&
18 : Schran2020b,&
19 : cite_reference
20 : USE cell_methods, ONLY: cell_create
21 : USE cell_types, ONLY: cell_release,&
22 : cell_type,&
23 : pbc
24 : USE cp_log_handling, ONLY: cp_get_default_logger,&
25 : cp_logger_type
26 : USE cp_output_handling, ONLY: cp_p_file,&
27 : cp_print_key_finished_output,&
28 : cp_print_key_should_output,&
29 : cp_print_key_unit_nr
30 : USE cp_units, ONLY: cp_unit_from_cp2k
31 : USE helium_types, ONLY: helium_solvent_type
32 : USE input_section_types, ONLY: section_vals_get,&
33 : section_vals_get_subs_vals,&
34 : section_vals_type,&
35 : section_vals_val_get
36 : USE kinds, ONLY: default_path_length,&
37 : default_string_length,&
38 : dp
39 : USE nnp_environment, ONLY: nnp_init_model
40 : USE nnp_environment_types, ONLY: nnp_env_get,&
41 : nnp_env_set,&
42 : nnp_type
43 : USE periodic_table, ONLY: get_ptable_info
44 : USE physcon, ONLY: angstrom
45 : #include "../base/base_uses.f90"
46 :
47 : IMPLICIT NONE
48 :
49 : PRIVATE
50 :
51 : LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .TRUE.
52 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'helium_nnp'
53 :
54 : PUBLIC :: helium_init_nnp, &
55 : helium_nnp_print
56 :
57 : CONTAINS
58 :
59 : ! ***************************************************************************
60 : !> \brief Read and initialize all the information for neural network potentials
61 : !> \param helium ...
62 : !> \param nnp ...
63 : !> \param input ...
64 : !> \date 2023-02-21
65 : !> \author lduran
66 : ! **************************************************************************************************
67 1 : SUBROUTINE helium_init_nnp(helium, nnp, input)
68 : TYPE(helium_solvent_type), INTENT(INOUT) :: helium
69 : TYPE(nnp_type), POINTER :: nnp
70 : TYPE(section_vals_type), POINTER :: input
71 :
72 : CHARACTER(len=default_path_length) :: msg_str
73 : CHARACTER(len=default_string_length) :: elem
74 : INTEGER :: i, ig, is, j
75 : INTEGER, DIMENSION(3) :: periodicity
76 : LOGICAL :: found
77 : TYPE(cell_type), POINTER :: he_cell
78 : TYPE(cp_logger_type), POINTER :: logger
79 : TYPE(section_vals_type), POINTER :: sr_cutoff_section
80 :
81 1 : CALL cite_reference(Behler2007)
82 1 : CALL cite_reference(Behler2011)
83 1 : CALL cite_reference(Schran2020a)
84 1 : CALL cite_reference(Schran2020b)
85 :
86 1 : NULLIFY (logger)
87 1 : logger => cp_get_default_logger()
88 :
89 1 : CALL nnp_env_set(nnp_env=nnp, nnp_input=input)
90 :
91 1 : nnp%num_atoms = helium%solute_atoms + 1
92 :
93 : ! The helium-solute coupling has no device force path, so this environment
94 : ! stays on the CPU and never claims the per-process GPU state.
95 1 : CALL nnp_init_model(nnp, "HELIUM NNP", gpu_capable=.FALSE.)
96 :
97 1 : periodicity = 0
98 1 : IF (helium%periodic) periodicity = 1
99 1 : NULLIFY (he_cell)
100 : CALL cell_create(he_cell, hmat=helium%cell_m, &
101 1 : periodic=periodicity, tag="HELIUM NNP")
102 1 : CALL nnp_env_set(nnp, cell=he_cell)
103 1 : CALL cell_release(he_cell)
104 :
105 : ! Set up arrays for calculation:
106 3 : ALLOCATE (nnp%ele_ind(nnp%num_atoms))
107 3 : ALLOCATE (nnp%nuc_atoms(nnp%num_atoms))
108 3 : ALLOCATE (nnp%coord(3, nnp%num_atoms))
109 3 : ALLOCATE (nnp%nnp_forces(3, nnp%num_atoms))
110 3 : ALLOCATE (nnp%atoms(nnp%num_atoms))
111 3 : ALLOCATE (nnp%sort(nnp%num_atoms - 1))
112 :
113 : !fill arrays, assume that order will not change during simulation
114 1 : ig = 1
115 1 : is = 1
116 4 : DO i = 1, nnp%n_ele
117 3 : IF (nnp%ele(i) == 'He') THEN
118 1 : nnp%atoms(ig) = 'He'
119 1 : CALL get_ptable_info(nnp%atoms(ig) (1:2), number=nnp%nuc_atoms(ig))
120 1 : nnp%ele_ind(ig) = i
121 1 : ig = ig + 1
122 : END IF
123 13 : DO j = 1, helium%solute_atoms
124 12 : IF (nnp%ele(i) == helium%solute_element(j)) THEN
125 3 : nnp%atoms(ig) = nnp%ele(i)
126 3 : CALL get_ptable_info(nnp%atoms(ig) (1:2), number=nnp%nuc_atoms(ig))
127 3 : nnp%ele_ind(ig) = i
128 3 : nnp%sort(is) = j
129 3 : ig = ig + 1
130 3 : is = is + 1
131 : END IF
132 : END DO
133 : END DO
134 :
135 3 : ALLOCATE (helium%nnp_sr_cut(nnp%n_ele))
136 4 : helium%nnp_sr_cut = 0.0_dp
137 :
138 1 : sr_cutoff_section => section_vals_get_subs_vals(nnp%nnp_input, "SR_CUTOFF")
139 1 : CALL section_vals_get(sr_cutoff_section, n_repetition=is)
140 3 : DO i = 1, is
141 2 : CALL section_vals_val_get(sr_cutoff_section, "ELEMENT", c_val=elem, i_rep_section=i)
142 2 : found = .FALSE.
143 8 : DO ig = 1, nnp%n_ele
144 8 : IF (TRIM(nnp%ele(ig)) == TRIM(elem)) THEN
145 2 : found = .TRUE.
146 : CALL section_vals_val_get(sr_cutoff_section, "RADIUS", r_val=helium%nnp_sr_cut(ig), &
147 2 : i_rep_section=i)
148 : END IF
149 : END DO
150 3 : IF (.NOT. found) THEN
151 0 : msg_str = "SR_CUTOFF for element "//TRIM(elem)//" defined but not found in NNP"
152 0 : CPWARN(msg_str)
153 : END IF
154 : END DO
155 4 : helium%nnp_sr_cut(:) = helium%nnp_sr_cut(:)**2
156 :
157 1 : RETURN
158 :
159 1 : END SUBROUTINE helium_init_nnp
160 :
161 : ! **************************************************************************************************
162 : !> \brief Print properties according to the requests in input file
163 : !> \param nnp ...
164 : !> \param print_section ...
165 : !> \param ind_he ...
166 : !> \date 2023-07-31
167 : !> \author Laura Duran
168 : ! **************************************************************************************************
169 55172 : SUBROUTINE helium_nnp_print(nnp, print_section, ind_he)
170 : TYPE(nnp_type), INTENT(INOUT) :: nnp
171 : TYPE(section_vals_type), INTENT(IN), POINTER :: print_section
172 : INTEGER, INTENT(IN) :: ind_he
173 :
174 : INTEGER :: unit_nr
175 : LOGICAL :: file_is_new
176 : TYPE(cp_logger_type), POINTER :: logger
177 : TYPE(section_vals_type), POINTER :: print_key
178 :
179 55172 : NULLIFY (logger, print_key)
180 55172 : logger => cp_get_default_logger()
181 :
182 55172 : print_key => section_vals_get_subs_vals(print_section, "ENERGIES")
183 55172 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
184 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".data", &
185 0 : middle_name="helium-nnp-energies", is_new_file=file_is_new)
186 0 : IF (unit_nr > 0) CALL helium_nnp_print_energies(nnp, unit_nr, file_is_new)
187 0 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
188 : END IF
189 :
190 55172 : print_key => section_vals_get_subs_vals(print_section, "FORCES_SIGMA")
191 55172 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
192 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".data", &
193 0 : middle_name="helium-nnp-forces-std", is_new_file=file_is_new)
194 0 : IF (unit_nr > 0) CALL helium_nnp_print_force_sigma(nnp, unit_nr, file_is_new)
195 0 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
196 : END IF
197 :
198 55172 : CALL logger%para_env%sum(nnp%output_expol)
199 55172 : IF (nnp%output_expol) THEN
200 0 : print_key => section_vals_get_subs_vals(print_section, "EXTRAPOLATION")
201 0 : IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key), cp_p_file)) THEN
202 : unit_nr = cp_print_key_unit_nr(logger, print_key, extension=".xyz", &
203 0 : middle_name="-NNP-He-extrapolation")
204 0 : IF (unit_nr > 0) CALL helium_nnp_print_expol(nnp, unit_nr, ind_he)
205 0 : CALL cp_print_key_finished_output(unit_nr, logger, print_key)
206 : END IF
207 : END IF
208 :
209 55172 : END SUBROUTINE helium_nnp_print
210 :
211 : ! **************************************************************************************************
212 : !> \brief Print NNP energies and standard deviation sigma
213 : !> \param nnp ...
214 : !> \param unit_nr ...
215 : !> \param file_is_new ...
216 : !> \date 2023-07-31
217 : !> \author Laura Duran
218 : ! **************************************************************************************************
219 0 : SUBROUTINE helium_nnp_print_energies(nnp, unit_nr, file_is_new)
220 : TYPE(nnp_type), INTENT(INOUT) :: nnp
221 : INTEGER, INTENT(IN) :: unit_nr
222 : LOGICAL, INTENT(IN) :: file_is_new
223 :
224 : CHARACTER(len=default_path_length) :: fmt_string
225 : INTEGER :: i
226 : REAL(KIND=dp) :: std
227 :
228 0 : IF (file_is_new) THEN
229 0 : WRITE (unit_nr, "(A1,1X,A20)", ADVANCE='no') "#", "NNP Average [a.u.],"
230 0 : WRITE (unit_nr, "(A20)", ADVANCE='no') "NNP sigma [a.u.]"
231 0 : DO i = 1, nnp%n_committee
232 0 : WRITE (unit_nr, "(A17,I3)", ADVANCE='no') "NNP", i
233 : END DO
234 0 : WRITE (unit_nr, "(A)") ""
235 : END IF
236 :
237 0 : fmt_string = "(2X,2(F20.9))"
238 0 : WRITE (fmt_string, "(A,I3,A)") "(2X", nnp%n_committee + 2, "(F20.9))"
239 0 : std = SUM((SUM(nnp%atomic_energy, 1) - nnp%nnp_potential_energy)**2)
240 0 : std = std/REAL(nnp%n_committee, dp)
241 0 : std = SQRT(std)
242 0 : WRITE (unit_nr, fmt_string) nnp%nnp_potential_energy, std, SUM(nnp%atomic_energy, 1)
243 :
244 0 : END SUBROUTINE helium_nnp_print_energies
245 :
246 : ! **************************************************************************************************
247 : !> \brief Print standard deviation sigma of NNP forces
248 : !> \param nnp ...
249 : !> \param unit_nr ...
250 : !> \param file_is_new ...
251 : !> \date 2023-07-31
252 : !> \author Laura Duran
253 : ! **************************************************************************************************
254 0 : SUBROUTINE helium_nnp_print_force_sigma(nnp, unit_nr, file_is_new)
255 : TYPE(nnp_type), INTENT(INOUT) :: nnp
256 : INTEGER, INTENT(IN) :: unit_nr
257 : LOGICAL, INTENT(IN) :: file_is_new
258 :
259 : INTEGER :: i, ig, j
260 : REAL(KIND=dp), DIMENSION(3) :: var
261 :
262 0 : IF (unit_nr > 0) THEN
263 0 : IF (file_is_new) THEN
264 0 : WRITE (unit_nr, "(A,1X,A)") "# NNP sigma of forces [a.u.] x, y, z coordinates"
265 : END IF
266 :
267 0 : ig = 1
268 0 : DO i = 1, nnp%num_atoms
269 0 : IF (nnp%ele(i) == 'He') THEN
270 0 : var = 0.0_dp
271 0 : DO j = 1, nnp%n_committee
272 0 : var = var + (nnp%committee_forces(:, i, j) - nnp%nnp_forces(:, i))**2
273 : END DO
274 0 : WRITE (unit_nr, "(A4,1X,3E20.10)") nnp%ele(i), var
275 : END IF
276 0 : ig = ig + 1
277 : END DO
278 : END IF
279 :
280 0 : END SUBROUTINE helium_nnp_print_force_sigma
281 :
282 : ! **************************************************************************************************
283 : !> \brief Print structures with extrapolation warning
284 : !> \param nnp ...
285 : !> \param unit_nr ...
286 : !> \param ind_he ...
287 : !> \date 2023-10-11
288 : !> \author Harald Forbert (harald.forbert@rub.de)
289 : ! **************************************************************************************************
290 0 : SUBROUTINE helium_nnp_print_expol(nnp, unit_nr, ind_he)
291 : TYPE(nnp_type), INTENT(INOUT) :: nnp
292 : INTEGER, INTENT(IN) :: unit_nr, ind_he
293 :
294 : CHARACTER(len=default_path_length) :: fmt_string
295 : INTEGER :: i
296 : REAL(KIND=dp) :: mass, unit_conv
297 : REAL(KIND=dp), DIMENSION(3) :: com
298 : TYPE(cell_type), POINTER :: cell
299 :
300 0 : NULLIFY (cell)
301 0 : CALL nnp_env_get(nnp_env=nnp, cell=cell)
302 :
303 0 : nnp%expol = nnp%expol + 1
304 0 : WRITE (unit_nr, *) nnp%num_atoms
305 0 : WRITE (unit_nr, "(A,1X,I6)") "HELIUM-NNP extrapolation point N =", nnp%expol
306 :
307 : ! move to COM of solute and wrap the box
308 : ! coord not needed afterwards, therefore manipulation ok
309 0 : com = 0.0_dp
310 0 : mass = 0.0_dp
311 0 : DO i = 1, nnp%num_atoms
312 0 : IF (i == ind_he) CYCLE
313 0 : CALL get_ptable_info(nnp%atoms(i) (1:2), amass=unit_conv)
314 0 : com(:) = com(:) + nnp%coord(:, i)*unit_conv
315 0 : mass = mass + unit_conv
316 : END DO
317 0 : com(:) = com(:)/mass
318 :
319 0 : DO i = 1, nnp%num_atoms
320 0 : nnp%coord(:, i) = nnp%coord(:, i) - com(:)
321 0 : nnp%coord(:, i) = pbc(nnp%coord(:, i), cell)
322 : END DO
323 :
324 0 : unit_conv = cp_unit_from_cp2k(1.0_dp, TRIM("angstrom"))
325 0 : fmt_string = "(A4,1X,3F20.10)"
326 0 : DO i = 1, nnp%num_atoms
327 : WRITE (unit_nr, fmt_string) &
328 0 : nnp%atoms(i), &
329 0 : nnp%coord(1, i)*unit_conv, &
330 0 : nnp%coord(2, i)*unit_conv, &
331 0 : nnp%coord(3, i)*unit_conv
332 : END DO
333 :
334 0 : END SUBROUTINE helium_nnp_print_expol
335 :
336 : END MODULE helium_nnp
|