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 Creates the NNP section of the input
10 : !> \author Christoph Schran (christoph.schran@rub.de)
11 : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
12 : !> \date 2020-10-10
13 : ! **************************************************************************************************
14 : MODULE input_cp2k_nnp
15 :
16 : USE bibliography, ONLY: Behler2007,&
17 : Behler2011,&
18 : Schran2020a,&
19 : Schran2020b
20 : USE cp_output_handling, ONLY: cp_print_key_section_create,&
21 : medium_print_level
22 : USE cp_units, ONLY: cp_unit_to_cp2k
23 : USE input_constants, ONLY: nnp_gpu_prec_double,&
24 : nnp_gpu_prec_mixed,&
25 : nnp_use_gpu_auto,&
26 : nnp_use_gpu_off,&
27 : nnp_use_gpu_on
28 : USE input_keyword_types, ONLY: keyword_create,&
29 : keyword_release,&
30 : keyword_type
31 : USE input_section_types, ONLY: section_add_keyword,&
32 : section_add_subsection,&
33 : section_create,&
34 : section_release,&
35 : section_type
36 : USE input_val_types, ONLY: char_t,&
37 : integer_t,&
38 : real_t
39 : USE kinds, ONLY: dp
40 : USE string_utilities, ONLY: s2a
41 : #include "./base/base_uses.f90"
42 :
43 : IMPLICIT NONE
44 : PRIVATE
45 :
46 : LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
47 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'input_cp2k_nnp'
48 :
49 : PUBLIC :: create_nnp_section
50 :
51 : CONTAINS
52 :
53 : ! **************************************************************************************************
54 : !> \brief Create the input section for NNP
55 : !> \param section the section to create
56 : !> \date 2020-10-10
57 : !> \author Christoph Schran (christoph.schran@rub.de)
58 : ! **************************************************************************************************
59 1480 : SUBROUTINE create_nnp_section(section)
60 : TYPE(section_type), POINTER :: section
61 :
62 : TYPE(keyword_type), POINTER :: keyword
63 : TYPE(section_type), POINTER :: subsection, subsubsection
64 :
65 1480 : CPASSERT(.NOT. ASSOCIATED(section))
66 : CALL section_create(section, __LOCATION__, name="NNP", &
67 : description="This section contains all information to run a "// &
68 : "Neural Network Potential (NNP) calculation.", &
69 : n_keywords=5, n_subsections=3, repeats=.FALSE., &
70 7400 : citations=[Behler2007, Behler2011, Schran2020a, Schran2020b])
71 :
72 1480 : NULLIFY (subsection, subsubsection, keyword)
73 :
74 : CALL keyword_create(keyword, __LOCATION__, name="NNP_INPUT_FILE_NAME", &
75 : description="File containing the input information for "// &
76 : "the setup of the NNP (n2p2/RuNNer format).", &
77 1480 : repeats=.FALSE., default_lc_val="input.nn")
78 1480 : CALL section_add_keyword(section, keyword)
79 1480 : CALL keyword_release(keyword)
80 : CALL keyword_create(keyword, __LOCATION__, name="SCALE_FILE_NAME", &
81 : description="File containing the scaling information for "// &
82 : "the symmetry functions of the NNP.", &
83 1480 : repeats=.FALSE., default_lc_val="scaling.data")
84 1480 : CALL section_add_keyword(section, keyword)
85 1480 : CALL keyword_release(keyword)
86 :
87 : CALL keyword_create(keyword, __LOCATION__, name="RAD_SPLINE_N", &
88 : description="Number of knots per radial group in "// &
89 : "the cubic-Hermite spline tables that tabulate "// &
90 : "the radial symmetry-function product "// &
91 : "y(r) = exp(-eta*(r-rs)^2) * fcut(r). "// &
92 : "The value error scales as O(1/n^4) and the "// &
93 : "force (derivative) error as O(1/n^3); the "// &
94 : "default keeps both inside the NNP regression "// &
95 : "tolerance for a radial cutoff of ~12 bohr "// &
96 : "(value ~1e-14, force ~1e-10). Larger cutoffs or "// &
97 : "stricter tolerances may need a larger n, with "// &
98 : "the force term the binding constraint. Memory "// &
99 : "cost scales linearly in n per group.", &
100 : repeats=.FALSE., &
101 : n_var=1, &
102 : type_of_var=integer_t, &
103 : default_i_val=8192, &
104 1480 : usage="RAD_SPLINE_N 8192")
105 1480 : CALL section_add_keyword(section, keyword)
106 1480 : CALL keyword_release(keyword)
107 :
108 : CALL keyword_create(keyword, __LOCATION__, name="VERLET_SKIN", &
109 : description="Verlet skin distance for the NNP descriptor "// &
110 : "neighbour cell-list. The neighbour-list cutoff is "// &
111 : "(symmetry-function cutoff + skin); the cell-list "// &
112 : "chain is rebuilt only when an atom drifts more "// &
113 : "than skin/2 from its rebuild-time position, "// &
114 : "analogous to the LAMMPS 'neighbor <skin> bin' "// &
115 : "command. Larger skin reduces the rebuild rate "// &
116 : "but enlarges the per-atom neighbour list. "// &
117 : "A negative value (default) selects the "// &
118 : "automatic heuristic MIN(0.5 bohr, 0.1*cutoff). "// &
119 : "Useful upper bound is half the smallest "// &
120 : "perpendicular cell width.", &
121 : repeats=.FALSE., &
122 : n_var=1, &
123 : type_of_var=real_t, &
124 : default_r_val=-1.0_dp, &
125 : unit_str="bohr", &
126 1480 : usage="VERLET_SKIN [bohr] 0.5")
127 1480 : CALL section_add_keyword(section, keyword)
128 1480 : CALL keyword_release(keyword)
129 :
130 : CALL keyword_create(keyword, __LOCATION__, name="USE_GPU", &
131 : description="Choose between the CPU and GPU code paths for "// &
132 : "the NNP energy and force evaluation. The GPU path "// &
133 : "requires a build with CUDA or HIP support; it "// &
134 : "reproduces the CPU results within the "// &
135 : "regression-test tolerances. Analytic stress (e.g. "// &
136 : "NPT) is accumulated on the device by the "// &
137 : "force-scatter kernels; only energy-only "// &
138 : "evaluations always use the CPU path.", &
139 : usage="USE_GPU AUTO", &
140 : repeats=.FALSE., &
141 : enum_c_vals=s2a("AUTO", "ON", "OFF"), &
142 : enum_i_vals=[nnp_use_gpu_auto, nnp_use_gpu_on, nnp_use_gpu_off], &
143 : enum_desc=s2a("Probe for a GPU at initialization and use it when "// &
144 : "the build supports one, every rank sees a device, "// &
145 : "the model fits the compiled-in device-table "// &
146 : "capacities, and the system is large enough for the "// &
147 : "transfer and launch overhead to pay off (currently "// &
148 : "a thousand atoms per rank); otherwise fall back to "// &
149 : "the CPU.", &
150 : "Force the GPU code path; abort if the build has "// &
151 : "no GPU support or no device is available.", &
152 : "Force the CPU code path."), &
153 1480 : default_i_val=nnp_use_gpu_auto)
154 1480 : CALL section_add_keyword(section, keyword)
155 1480 : CALL keyword_release(keyword)
156 :
157 : CALL keyword_create(keyword, __LOCATION__, name="GPU_PRECISION", &
158 : description="Floating-point precision of the GPU code path. "// &
159 : "Only meaningful when the GPU path is active.", &
160 : usage="GPU_PRECISION DOUBLE", &
161 : repeats=.FALSE., &
162 : enum_c_vals=s2a("DOUBLE", "MIXED"), &
163 : enum_i_vals=[nnp_gpu_prec_double, nnp_gpu_prec_mixed], &
164 : enum_desc=s2a("All-FP64 path; reproduces the CPU trajectory "// &
165 : "within the regression-test tolerances.", &
166 : "FP32 angular symmetry-function math with FP64 "// &
167 : "radial functions, network evaluation and every "// &
168 : "accumulator that sums over neighbours. Faster on "// &
169 : "GPUs whose single-precision throughput exceeds "// &
170 : "their double-precision throughput, at the cost of "// &
171 : "a perturbation of energies and forces that is "// &
172 : "model dependent (a relative 4.5e-11 on the "// &
173 : "step-zero energy of the shipped regression-test "// &
174 : "model, whose tests bound it at 1e-6; measure it "// &
175 : "for your own)."), &
176 1480 : default_i_val=nnp_gpu_prec_double)
177 1480 : CALL section_add_keyword(section, keyword)
178 1480 : CALL keyword_release(keyword)
179 :
180 : CALL keyword_create(keyword, __LOCATION__, name="VALIDATE", &
181 : description="When enabled and the GPU code path is active, the CPU "// &
182 : "reference implementation is run in shadow mode on every force "// &
183 : "evaluation and its energies, forces and stress are compared with "// &
184 : "those from the GPU path. If the two results differ by too much "// &
185 : "then the calculation is aborted. Under USE_GPU AUTO the "// &
186 : "system-size heuristic is skipped, so the GPU path engages "// &
187 : "whenever the build and the machine support it. Has no effect "// &
188 : "when the evaluation runs on the CPU, so the same input can be "// &
189 : "used on every architecture.", &
190 : usage="VALIDATE", &
191 : default_l_val=.FALSE., &
192 1480 : lone_keyword_l_val=.TRUE.)
193 1480 : CALL section_add_keyword(section, keyword)
194 1480 : CALL keyword_release(keyword)
195 :
196 : ! BIAS subsection
197 : CALL section_create(subsection, __LOCATION__, name="BIAS", &
198 : description="Section to bias the committee disagreement (sigma) by "// &
199 : "E = 0.5 * K_B * (sigma - SIGMA_0)**2, if sigma > SIGMA_0.", &
200 : n_keywords=2, n_subsections=0, repeats=.FALSE., &
201 2960 : citations=[Schran2020b])
202 : CALL keyword_create(keyword, __LOCATION__, name="K_B", &
203 : description="Harmonic spring constant of the bias potential [1/hartree].", &
204 : repeats=.FALSE., &
205 : n_var=1, &
206 : type_of_var=real_t, &
207 : default_r_val=cp_unit_to_cp2k(value=0.1_dp, unit_str="hartree^-1"), &
208 : unit_str="hartree^-1", &
209 1480 : usage="K_B [hartree^-1] 0.1")
210 1480 : CALL section_add_keyword(subsection, keyword)
211 1480 : CALL keyword_release(keyword)
212 : CALL keyword_create(keyword, __LOCATION__, name="SIGMA_0", &
213 : description="Shift of the harmonic bias potential.", &
214 : repeats=.FALSE., &
215 : n_var=1, &
216 : type_of_var=real_t, &
217 : default_r_val=cp_unit_to_cp2k(value=0.1_dp, unit_str="hartree"), &
218 : unit_str="hartree", &
219 1480 : usage="SIGMA_0 [hartree] 0.1")
220 1480 : CALL section_add_keyword(subsection, keyword)
221 1480 : CALL keyword_release(keyword)
222 : CALL keyword_create(keyword, __LOCATION__, name="ALIGN_NNP_ENERGIES", &
223 : description="Remove PES shifts within the committee by "// &
224 : "subtracting energy for each committee member. Provide "// &
225 : "one number per C-NNP member.", &
226 : repeats=.FALSE., &
227 : n_var=-1, &
228 : type_of_var=real_t, &
229 1480 : usage="ALIGN_NNP_ENERGIES <REAL> <REAL> ... <REAL>")
230 1480 : CALL section_add_keyword(subsection, keyword)
231 1480 : CALL keyword_release(keyword)
232 : ! print bias subsubsection:
233 1480 : CALL create_nnp_bias_print_section(subsubsection)
234 1480 : CALL section_add_subsection(subsection, subsubsection)
235 1480 : CALL section_release(subsubsection)
236 :
237 1480 : CALL section_add_subsection(section, subsection)
238 1480 : CALL section_release(subsection)
239 : ! end BIAS subsection
240 :
241 : CALL section_create(subsection, __LOCATION__, name="MODEL", &
242 : description="Section for a single NNP model. "// &
243 : "If this section is repeated, a committee model (C-NNP) "// &
244 : "is used where the NNP members share the same symmetry functions.", &
245 1480 : n_keywords=1, n_subsections=0, repeats=.TRUE.)
246 : CALL keyword_create(keyword, __LOCATION__, name="WEIGHTS", &
247 : description="File containing the weights for the "// &
248 : "artificial neural networks of the NNP. "// &
249 : "The specified name is extended by .XXX.data", &
250 1480 : repeats=.FALSE., default_lc_val="weights")
251 1480 : CALL section_add_keyword(subsection, keyword)
252 1480 : CALL keyword_release(keyword)
253 1480 : CALL section_add_subsection(section, subsection)
254 1480 : CALL section_release(subsection)
255 :
256 1480 : CALL create_nnp_print_section(subsection)
257 1480 : CALL section_add_subsection(section, subsection)
258 1480 : CALL section_release(subsection)
259 :
260 1480 : END SUBROUTINE create_nnp_section
261 :
262 : ! **************************************************************************************************
263 : !> \brief Creates the print section for the nnp subsection
264 : !> \param section the section to create
265 : !> \date 2020-10-10
266 : !> \author Christoph Schran (christoph.schran@rub.de)
267 : ! **************************************************************************************************
268 1480 : SUBROUTINE create_nnp_print_section(section)
269 : TYPE(section_type), POINTER :: section
270 :
271 : TYPE(keyword_type), POINTER :: keyword
272 : TYPE(section_type), POINTER :: print_key
273 :
274 1480 : CPASSERT(.NOT. ASSOCIATED(section))
275 : CALL section_create(section, __LOCATION__, name="PRINT", &
276 : description="Section of possible print options in NNP code.", &
277 1480 : n_keywords=0, n_subsections=5, repeats=.FALSE.)
278 :
279 1480 : NULLIFY (print_key, keyword)
280 :
281 : CALL cp_print_key_section_create(print_key, __LOCATION__, "ENERGIES", &
282 : description="Controls the printing of the NNP energies.", &
283 1480 : print_level=medium_print_level, common_iter_levels=1)
284 1480 : CALL section_add_subsection(section, print_key)
285 1480 : CALL section_release(print_key)
286 :
287 : CALL cp_print_key_section_create(print_key, __LOCATION__, "FORCES", &
288 : description="Controls the printing of the NNP forces.", &
289 1480 : print_level=medium_print_level, common_iter_levels=1)
290 1480 : CALL section_add_subsection(section, print_key)
291 1480 : CALL section_release(print_key)
292 :
293 : CALL cp_print_key_section_create(print_key, __LOCATION__, "FORCES_SIGMA", &
294 : description="Controls the printing of the STD per atom of the NNP forces.", &
295 1480 : print_level=medium_print_level, common_iter_levels=1)
296 1480 : CALL section_add_subsection(section, print_key)
297 1480 : CALL section_release(print_key)
298 :
299 : CALL cp_print_key_section_create(print_key, __LOCATION__, "EXTRAPOLATION", &
300 : description="If activated, output structures with extrapolation "// &
301 : "warning in xyz-format", &
302 1480 : print_level=medium_print_level, common_iter_levels=1)
303 1480 : CALL section_add_subsection(section, print_key)
304 1480 : CALL section_release(print_key)
305 :
306 : CALL cp_print_key_section_create(print_key, __LOCATION__, "SUM_FORCE", &
307 : description="If activated, output summed force over specified atoms. "// &
308 : "Used in Green-Kubo relation for friction at liquid-solid interfaces.", &
309 1480 : print_level=medium_print_level, common_iter_levels=1)
310 :
311 : CALL keyword_create(keyword, __LOCATION__, name="ATOM_LIST", &
312 : description="List of atoms over which to calculate summed force", &
313 : usage="ATOM_LISTS {O} {H} .. {X}", repeats=.FALSE., &
314 1480 : n_var=-1, type_of_var=char_t)
315 1480 : CALL section_add_keyword(print_key, keyword)
316 1480 : CALL keyword_release(keyword)
317 :
318 1480 : CALL section_add_subsection(section, print_key)
319 1480 : CALL section_release(print_key)
320 :
321 1480 : END SUBROUTINE create_nnp_print_section
322 :
323 : ! **************************************************************************************************
324 : !> \brief Creates the print section for the nnp bias subsubsection
325 : !> \param section the section to create
326 : !> \date 2020-10-10
327 : !> \author Christoph Schran (christoph.schran@rub.de)
328 : ! **************************************************************************************************
329 1480 : SUBROUTINE create_nnp_bias_print_section(section)
330 : TYPE(section_type), POINTER :: section
331 :
332 : TYPE(section_type), POINTER :: print_key
333 :
334 1480 : CPASSERT(.NOT. ASSOCIATED(section))
335 : CALL section_create(section, __LOCATION__, name="PRINT", &
336 : description="Section of possible print options in NNP code.", &
337 1480 : n_keywords=0, n_subsections=3, repeats=.FALSE.)
338 :
339 1480 : NULLIFY (print_key)
340 :
341 : CALL cp_print_key_section_create(print_key, __LOCATION__, "BIAS_ENERGY", &
342 : description="Controls the printing of the BIAS energy.", &
343 1480 : print_level=medium_print_level, common_iter_levels=1)
344 1480 : CALL section_add_subsection(section, print_key)
345 1480 : CALL section_release(print_key)
346 :
347 : CALL cp_print_key_section_create(print_key, __LOCATION__, "BIAS_FORCES", &
348 : description="Controls the printing of the BIAS forces.", &
349 1480 : print_level=medium_print_level, common_iter_levels=1)
350 1480 : CALL section_add_subsection(section, print_key)
351 1480 : CALL section_release(print_key)
352 :
353 1480 : END SUBROUTINE create_nnp_bias_print_section
354 :
355 : END MODULE input_cp2k_nnp
|