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 Module with utility for Nudged Elastic Band Calculation
10 : !> \note
11 : !> Numerical accuracy for parallel runs:
12 : !> Each replica starts the SCF run from the one optimized
13 : !> in a previous run. It may happen then energies and derivatives
14 : !> of a serial run and a parallel run could be slightly different
15 : !> 'cause of a different starting density matrix.
16 : !> Exact results are obtained using:
17 : !> EXTRAPOLATION USE_GUESS in QS section (Teo 09.2006)
18 : !> \author Teodoro Laino 10.2006
19 : ! **************************************************************************************************
20 : MODULE neb_utils
21 : USE bibliography, ONLY: &
22 : E2002, Elber1987, Jonsson1998, Jonsson2000_1, Jonsson2000_2, Schmerwitz2024, &
23 : Smidstrup2014, Wales2004, cite_reference
24 : USE cell_methods, ONLY: init_cell
25 : USE cell_types, ONLY: cell_type
26 : USE colvar_utils, ONLY: eval_colvar,&
27 : get_clv_force,&
28 : set_colvars_target
29 : USE cp_log_handling, ONLY: cp_get_default_logger,&
30 : cp_logger_type
31 : USE cp_output_handling, ONLY: cp_print_key_finished_output,&
32 : cp_print_key_unit_nr
33 : USE cp_parser_methods, ONLY: parser_get_next_line,&
34 : parser_get_object
35 : USE cp_parser_types, ONLY: cp_parser_type,&
36 : parser_create,&
37 : parser_release
38 : USE cp_subsys_types, ONLY: cp_subsys_get,&
39 : cp_subsys_set,&
40 : cp_subsys_type
41 : USE f77_interface, ONLY: f_env_add_defaults,&
42 : f_env_rm_defaults,&
43 : f_env_type,&
44 : get_energy,&
45 : get_force,&
46 : get_pos,&
47 : set_pos
48 : USE force_env_methods, ONLY: force_env_calc_energy_force
49 : USE force_env_types, ONLY: force_env_get
50 : USE geo_opt, ONLY: cp_geo_opt
51 : USE global_types, ONLY: global_environment_type
52 : USE input_constants, ONLY: &
53 : band_diis_opt, do_b_neb, do_band_cartesian, do_ci_neb, do_d_neb, do_eb, do_it_neb, do_sm, &
54 : pot_neb_fe, pot_neb_full, pot_neb_me
55 : USE input_cp2k_check, ONLY: remove_restart_info
56 : USE input_cp2k_neb, ONLY: neb_li_bisect,&
57 : neb_s_idpp
58 : USE input_section_types, ONLY: section_vals_get,&
59 : section_vals_get_subs_vals,&
60 : section_vals_type,&
61 : section_vals_val_get
62 : USE kinds, ONLY: default_path_length,&
63 : default_string_length,&
64 : dp
65 : USE mathlib, ONLY: det_3x3
66 : USE md_run, ONLY: qs_mol_dyn
67 : USE message_passing, ONLY: mp_para_env_type
68 : USE neb_idpp, ONLY: neb_initialize_idpp
69 : USE neb_io, ONLY: dump_replica_coordinates,&
70 : handle_band_file_names
71 : USE neb_md_utils, ONLY: neb_initialize_velocity
72 : USE neb_types, ONLY: neb_type,&
73 : neb_var_type
74 : USE particle_types, ONLY: particle_type
75 : USE physcon, ONLY: bohr
76 : USE replica_methods, ONLY: rep_env_calc_e_f
77 : USE replica_types, ONLY: rep_env_sync,&
78 : replica_env_type
79 : USE rmsd, ONLY: rmsd3
80 : USE solid_state_coordinates, ONLY: solid_state_force,&
81 : solid_state_frame,&
82 : solid_state_pack,&
83 : solid_state_unpack
84 : USE virial_types, ONLY: virial_type
85 : #include "../base/base_uses.f90"
86 :
87 : IMPLICIT NONE
88 : PRIVATE
89 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'neb_utils'
90 : LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .FALSE.
91 :
92 : PUBLIC :: build_replica_coords, &
93 : neb_calc_energy_forces, &
94 : reorient_images, &
95 : reparametrize_images, &
96 : check_convergence
97 :
98 : CONTAINS
99 :
100 : ! **************************************************************************************************
101 : !> \brief Computes the distance between two replica
102 : !> \param particle_set ...
103 : !> \param coords ...
104 : !> \param i0 ...
105 : !> \param i ...
106 : !> \param distance ...
107 : !> \param iw ...
108 : !> \param rotate ...
109 : !> \author Teodoro Laino 09.2006
110 : ! **************************************************************************************************
111 13868 : SUBROUTINE neb_replica_distance(particle_set, coords, i0, i, distance, iw, rotate)
112 : TYPE(particle_type), DIMENSION(:), OPTIONAL, &
113 : POINTER :: particle_set
114 : TYPE(neb_var_type), POINTER :: coords
115 : INTEGER, INTENT(IN) :: i0, i
116 : REAL(KIND=dp), INTENT(OUT) :: distance
117 : INTEGER, INTENT(IN) :: iw
118 : LOGICAL, INTENT(IN), OPTIONAL :: rotate
119 :
120 : LOGICAL :: my_rotate
121 :
122 13868 : my_rotate = .FALSE.
123 13868 : IF (PRESENT(rotate)) my_rotate = rotate
124 : ! The rotation of the replica is enabled exclusively when working in
125 : ! cartesian coordinates
126 13868 : IF (my_rotate .AND. (coords%in_use == do_band_cartesian)) THEN
127 444 : CPASSERT(PRESENT(particle_set))
128 : CALL rmsd3(particle_set, coords%xyz(:, i), coords%xyz(:, i0), &
129 444 : iw, rotate=my_rotate)
130 : END IF
131 1855260 : distance = NORM2(coords%wrk(:, i) - coords%wrk(:, i0))
132 :
133 13868 : END SUBROUTINE neb_replica_distance
134 :
135 : ! **************************************************************************************************
136 : !> \brief Constructs or Read the coordinates for all replica
137 : !> \param neb_section ...
138 : !> \param particle_set ...
139 : !> \param coords ...
140 : !> \param vels ...
141 : !> \param neb_env ...
142 : !> \param iw ...
143 : !> \param globenv ...
144 : !> \param para_env ...
145 : !> \author Teodoro Laino 09.2006
146 : ! **************************************************************************************************
147 40 : SUBROUTINE build_replica_coords(neb_section, particle_set, &
148 : coords, vels, neb_env, iw, globenv, para_env)
149 : TYPE(section_vals_type), POINTER :: neb_section
150 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
151 : TYPE(neb_var_type), POINTER :: coords, vels
152 : TYPE(neb_type), POINTER :: neb_env
153 : INTEGER, INTENT(IN) :: iw
154 : TYPE(global_environment_type), POINTER :: globenv
155 : TYPE(mp_para_env_type), POINTER :: para_env
156 :
157 : CHARACTER(len=*), PARAMETER :: routineN = 'build_replica_coords'
158 :
159 : CHARACTER(LEN=default_path_length) :: filename
160 : INTEGER :: handle, i_rep, iatom, ic, input_nr_replica, interp_method, is, ivar, j, jtarg, k, &
161 : n_rep, natom, neb_nr_replica, nr_replica_to_interpolate, nval, nvar, shell_index
162 40 : INTEGER, ALLOCATABLE, DIMENSION(:) :: rep_map
163 : LOGICAL :: align_interpolated, check, explicit, &
164 : idpp, sequential, skip_vel_section
165 40 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: distance
166 : REAL(KIND=dp), DIMENSION(3) :: r
167 40 : REAL(KIND=dp), DIMENSION(:), POINTER :: initial_colvars, rptr
168 : TYPE(cell_type), POINTER :: cell
169 : TYPE(section_vals_type), POINTER :: constraint_section, coord_section, &
170 : replica_section, vel_section
171 :
172 40 : CALL timeset(routineN, handle)
173 40 : CPASSERT(ASSOCIATED(coords))
174 40 : CPASSERT(ASSOCIATED(vels))
175 40 : neb_nr_replica = neb_env%number_of_replica
176 40 : replica_section => section_vals_get_subs_vals(neb_section, "REPLICA")
177 40 : CALL section_vals_get(replica_section, n_repetition=input_nr_replica)
178 40 : IF (neb_env%solid_state .AND. input_nr_replica < 2) THEN
179 0 : CPABORT("SOLID_STATE requires two endpoint replicas.")
180 : END IF
181 : ! Calculation is aborted if input replicas are more then the requested ones for the BAND..
182 40 : CPASSERT(input_nr_replica <= neb_nr_replica)
183 40 : CALL section_vals_val_get(neb_section, "INTERPOLATION_METHOD", i_val=interp_method)
184 40 : IF (neb_env%solid_state .AND. interp_method /= neb_li_bisect) THEN
185 0 : CPABORT("BAND IDPP is not supported with SOLID_STATE.")
186 : END IF
187 40 : idpp = interp_method /= neb_li_bisect .AND. input_nr_replica < neb_nr_replica
188 4 : sequential = idpp .AND. interp_method == neb_s_idpp
189 40 : align_interpolated = neb_env%align_frames .AND. .NOT. sequential
190 40 : IF (idpp) THEN
191 4 : CPASSERT(input_nr_replica >= 2)
192 12 : IF (neb_env%use_colvar .OR. ANY(particle_set%shell_index /= 0)) THEN
193 0 : CPABORT("BAND IDPP requires Cartesian images without shells.")
194 : END IF
195 4 : constraint_section => section_vals_get_subs_vals(neb_env%root_section, "MOTION%CONSTRAINT")
196 4 : CALL section_vals_get(constraint_section, explicit=explicit)
197 4 : IF (explicit) CPABORT("BAND IDPP does not currently support MOTION%CONSTRAINT.")
198 4 : CALL force_env_get(neb_env%force_env, cell=cell)
199 4 : IF (ANY(cell%perd /= 0) .AND. .NOT. cell%orthorhombic) THEN
200 0 : CPABORT("BAND IDPP requires an orthorhombic periodic cell.")
201 : END IF
202 : END IF
203 : ! Read in replicas coordinates
204 40 : skip_vel_section = (input_nr_replica /= neb_nr_replica)
205 40 : IF ((iw > 0) .AND. skip_vel_section) THEN
206 4 : WRITE (iw, '(T2,A)') 'NEB| The number of replica in the input is different from the number', &
207 4 : 'NEB| of replica requested for NEB. More Replica will be interpolated.', &
208 8 : 'NEB| Therefore the possibly provided velocities will not be read.'
209 : END IF
210 : ! Further check on velocity section...
211 212 : DO i_rep = 1, input_nr_replica
212 : vel_section => section_vals_get_subs_vals(replica_section, "VELOCITY", &
213 172 : i_rep_section=i_rep)
214 172 : CALL section_vals_get(vel_section, explicit=explicit)
215 240 : skip_vel_section = skip_vel_section .OR. (.NOT. explicit)
216 : END DO
217 : ! Setup cartesian coordinates and COLVAR (if requested)
218 41974 : coords%xyz(:, :) = 0.0_dp
219 : ! Mapping between input replica and actual replica
220 120 : ALLOCATE (rep_map(neb_nr_replica))
221 40 : rep_map(:) = 0
222 212 : DO i_rep = 1, input_nr_replica
223 : coord_section => section_vals_get_subs_vals(replica_section, "COORD", &
224 172 : i_rep_section=i_rep)
225 172 : CALL section_vals_get(coord_section, explicit=explicit)
226 172 : rep_map(i_rep) = i_rep
227 : ! Cartesian Coordinates
228 172 : IF (explicit) THEN
229 : CALL section_vals_val_get(coord_section, "_DEFAULT_KEYWORD_", &
230 44 : n_rep_val=natom)
231 44 : CPASSERT((natom == SIZE(particle_set)))
232 1548 : DO iatom = 1, natom
233 : CALL section_vals_val_get(coord_section, "_DEFAULT_KEYWORD_", &
234 1504 : i_rep_val=iatom, r_vals=rptr)
235 1504 : ic = 3*(iatom - 1)
236 10528 : coords%xyz(ic + 1:ic + 3, i_rep) = rptr(1:3)*bohr
237 : ! Initially core and shell positions are set to the atomic positions
238 1504 : shell_index = particle_set(iatom)%shell_index
239 1548 : IF (shell_index /= 0) THEN
240 1140 : is = 3*(natom + shell_index - 1)
241 7980 : coords%xyz(is + 1:is + 3, i_rep) = coords%xyz(ic + 1:ic + 3, i_rep)
242 : END IF
243 : END DO
244 : ELSE
245 128 : BLOCK
246 : LOGICAL :: my_end
247 : CHARACTER(LEN=default_string_length) :: dummy_char
248 : TYPE(cp_parser_type) :: parser
249 : CALL section_vals_val_get(replica_section, "COORD_FILE_NAME", &
250 128 : i_rep_section=i_rep, c_val=filename)
251 128 : CPASSERT(TRIM(filename) /= "")
252 128 : CALL parser_create(parser, filename, para_env=para_env, parse_white_lines=.TRUE.)
253 128 : CALL parser_get_next_line(parser, 1)
254 : ! Start parser
255 128 : CALL parser_get_object(parser, natom)
256 128 : CPASSERT((natom == SIZE(particle_set)))
257 128 : CALL parser_get_next_line(parser, 1)
258 9774 : DO iatom = 1, natom
259 : ! Atom coordinates
260 9646 : CALL parser_get_next_line(parser, 1, at_end=my_end)
261 9646 : IF (my_end) THEN
262 : CALL cp_abort(__LOCATION__, &
263 : "Number of lines in XYZ format not equal to the number of atoms."// &
264 : " Error in XYZ format for REPLICA coordinates. Very probably the"// &
265 0 : " line with title is missing or is empty. Please check the XYZ file and rerun your job!")
266 : END IF
267 9646 : READ (parser%input_line, *) dummy_char, r(1:3)
268 9646 : ic = 3*(iatom - 1)
269 38584 : coords%xyz(ic + 1:ic + 3, i_rep) = r(1:3)*bohr
270 : ! Initially core and shell positions are set to the atomic positions
271 9646 : shell_index = particle_set(iatom)%shell_index
272 9774 : IF (shell_index /= 0) THEN
273 0 : is = 3*(natom + shell_index - 1)
274 0 : coords%xyz(is + 1:is + 3, i_rep) = coords%xyz(ic + 1:ic + 3, i_rep)
275 : END IF
276 : END DO
277 512 : CALL parser_release(parser)
278 : END BLOCK
279 : END IF
280 172 : IF (neb_env%solid_state) THEN
281 4 : CALL section_vals_val_get(replica_section, "CELL_MATRIX", i_rep_section=i_rep, n_rep_val=nval)
282 4 : IF (nval /= 1) THEN
283 0 : CPABORT("Each SOLID_STATE replica needs a CELL_MATRIX.")
284 : END IF
285 4 : CALL section_vals_val_get(replica_section, "CELL_MATRIX", i_rep_section=i_rep, r_vals=rptr)
286 52 : coords%cells(:, :, i_rep) = RESHAPE(rptr, [3, 3])
287 4 : IF (i_rep == 1) CALL solid_state_frame(neb_env%solid_frame, coords%cells(:, :, i_rep), &
288 2 : SIZE(particle_set), neb_env%cell_weight)
289 : CALL solid_state_pack(neb_env%solid_frame, coords%cells(:, :, i_rep), coords%xyz(:, i_rep), &
290 4 : coords%wrk(:, i_rep))
291 : END IF
292 : ! Collective Variables
293 172 : IF (neb_env%use_colvar) THEN
294 : CALL section_vals_val_get(replica_section, "COLLECTIVE", &
295 18 : i_rep_section=i_rep, n_rep_val=n_rep)
296 18 : IF (n_rep /= 0) THEN
297 : ! Read the values of the collective variables
298 10 : NULLIFY (initial_colvars)
299 : CALL section_vals_val_get(replica_section, "COLLECTIVE", &
300 10 : i_rep_section=i_rep, r_vals=initial_colvars)
301 10 : check = (neb_env%nsize_int == SIZE(initial_colvars))
302 10 : CPASSERT(check)
303 30 : coords%int(:, i_rep) = initial_colvars
304 : ELSE
305 : ! Compute the values of the collective variables
306 8 : CALL eval_colvar(neb_env%force_env, coords%xyz(:, i_rep), coords%int(:, i_rep))
307 : END IF
308 : END IF
309 : ! Dump cartesian and colvar info..
310 172 : CALL dump_replica_coordinates(particle_set, coords, i_rep, i_rep, iw, neb_env%use_colvar)
311 : ! Setup Velocities
312 384 : IF (neb_env%solid_state) THEN
313 28 : vels%xyz(:, i_rep) = 0.0_dp
314 168 : ELSE IF (skip_vel_section) THEN
315 : CALL neb_initialize_velocity(vels%wrk, neb_section, particle_set, &
316 140 : i_rep, iw, globenv, neb_env)
317 : ELSE
318 : vel_section => section_vals_get_subs_vals(replica_section, "VELOCITY", &
319 28 : i_rep_section=i_rep)
320 : CALL section_vals_val_get(vel_section, "_DEFAULT_KEYWORD_", &
321 28 : n_rep_val=nval)
322 : ! Setup Velocities for collective or cartesian coordinates
323 28 : IF (neb_env%use_colvar) THEN
324 10 : nvar = SIZE(vels%wrk, 1)
325 10 : CPASSERT(nval == nvar)
326 20 : DO ivar = 1, nvar
327 : CALL section_vals_val_get(vel_section, "_DEFAULT_KEYWORD_", &
328 10 : i_rep_val=ivar, r_vals=rptr)
329 20 : vels%wrk(ivar, i_rep) = rptr(1)
330 : END DO
331 : ELSE
332 18 : natom = SIZE(particle_set)
333 18 : CPASSERT(nval == natom)
334 948 : DO iatom = 1, natom
335 : CALL section_vals_val_get(vel_section, "_DEFAULT_KEYWORD_", &
336 930 : i_rep_val=iatom, r_vals=rptr)
337 930 : ic = 3*(iatom - 1)
338 6510 : vels%wrk(ic + 1:ic + 3, i_rep) = rptr(1:3)
339 : ! Initially set shell velocities to core velocity
340 930 : shell_index = particle_set(iatom)%shell_index
341 948 : IF (shell_index /= 0) THEN
342 760 : is = 3*(natom + shell_index - 1)
343 5320 : vels%wrk(is + 1:is + 3, i_rep) = vels%wrk(ic + 1:ic + 3, i_rep)
344 : END IF
345 : END DO
346 : END IF
347 : END IF
348 : END DO ! i_rep
349 120 : ALLOCATE (distance(neb_nr_replica - 1))
350 40 : IF (iw > 0) THEN
351 6 : WRITE (iw, '(T2,A)') 'NEB| Mapping between input and requested replica so far'
352 6 : WRITE (iw, '(T2,A)') 'NEB| 1, 2, ... = input replica in sections order,'
353 6 : WRITE (iw, '(T2,A)') 'NEB| -1, -2, ... = added replica in insertion order (if any),'
354 6 : WRITE (iw, '(T2,A)') 'NEB| 0 = yet to be completed replica (if any)'
355 12 : DO j = 1, neb_nr_replica, 8
356 12 : WRITE (iw, '(T2,A,T9,8(1X,I8))') 'NEB|', rep_map(j:MIN(j + 7, neb_nr_replica))
357 : END DO
358 : END IF
359 40 : IF (input_nr_replica < neb_nr_replica) THEN
360 : ! S-IDPP uses the bisection only to allocate the missing images among
361 : ! anchors. Align the anchors before constructing any provisional images.
362 18 : IF (sequential .AND. neb_env%align_frames) THEN
363 0 : DO j = 1, input_nr_replica - 1
364 0 : CALL neb_replica_distance(particle_set, coords, j, j + 1, distance(j), iw, rotate=.TRUE.)
365 : END DO
366 : END IF
367 : ! Interpolate missing replicas
368 18 : nr_replica_to_interpolate = neb_nr_replica - input_nr_replica
369 18 : k = 0
370 18 : distance = 0.0_dp
371 18 : IF (iw > 0) THEN
372 4 : IF (sequential) THEN
373 1 : WRITE (iw, '(T2,A,I0,A)') 'NEB| Allocating ', nr_replica_to_interpolate, ' missing replicas for S-IDPP.'
374 : ELSE
375 3 : WRITE (iw, '(T2,A,I0,A)') 'NEB| Interpolating ', nr_replica_to_interpolate, ' missing replica '// &
376 6 : 'by stepwise bisection of distance.'
377 : END IF
378 : END IF
379 86 : DO WHILE (nr_replica_to_interpolate > 0)
380 : ! Compute distances between known images to find the interval
381 : ! where to add a new image
382 420 : DO j = 1, input_nr_replica - 1
383 : CALL neb_replica_distance(particle_set, coords, j, j + 1, distance(j), iw, &
384 420 : rotate=align_interpolated)
385 : END DO
386 420 : jtarg = MAXLOC(distance(1:input_nr_replica - 1), 1)
387 68 : IF (iw > 0) THEN
388 10 : WRITE (iw, '(/,T2,3(A,I0),A)') 'NEB| Interpolating Nr. ', &
389 10 : nr_replica_to_interpolate, ' missing Replica; next between Replica Nr. ', &
390 20 : jtarg, ' and ', jtarg + 1, '.'
391 : END IF
392 68 : input_nr_replica = input_nr_replica + 1
393 68 : nr_replica_to_interpolate = nr_replica_to_interpolate - 1
394 68 : k = k + 1
395 284 : rep_map(jtarg + 2:input_nr_replica) = rep_map(jtarg + 1:input_nr_replica - 1)
396 68 : IF (jtarg + 1 <= neb_nr_replica .AND. jtarg + 1 >= 1) rep_map(jtarg + 1) = -k
397 : ! Insert a Cartesian midpoint. S-IDPP uses this provisional image only
398 : ! to distribute missing replicas before growing the band from its anchors.
399 12884 : coords%xyz(:, jtarg + 2:input_nr_replica) = coords%xyz(:, jtarg + 1:input_nr_replica - 1)
400 4892 : coords%xyz(:, jtarg + 1) = (coords%xyz(:, jtarg) + coords%xyz(:, jtarg + 2))/2.0_dp
401 68 : IF (neb_env%solid_state) THEN
402 28 : coords%wrk(:, jtarg + 2:input_nr_replica) = coords%wrk(:, jtarg + 1:input_nr_replica - 1)
403 26 : coords%wrk(:, jtarg + 1) = 0.5_dp*(coords%wrk(:, jtarg) + coords%wrk(:, jtarg + 2))
404 : CALL solid_state_unpack(neb_env%solid_frame, coords%wrk(:, jtarg + 1), &
405 2 : coords%cells(:, :, jtarg + 1), coords%xyz(:, jtarg + 1))
406 : END IF
407 68 : IF (neb_env%use_colvar) THEN
408 : ! In a collective-variable band the internal midpoint is the target;
409 : ! the Cartesian midpoint is only the starting geometry.
410 6 : coords%int(:, jtarg + 2:input_nr_replica) = coords%int(:, jtarg + 1:input_nr_replica - 1)
411 4 : coords%int(:, jtarg + 1) = (coords%int(:, jtarg) + coords%int(:, jtarg + 2))/2.0_dp
412 : END IF
413 12796 : vels%wrk(:, jtarg + 2:input_nr_replica) = vels%wrk(:, jtarg + 1:input_nr_replica - 1)
414 4804 : vels%wrk(:, jtarg + 1) = 0.0_dp
415 68 : IF (.NOT. sequential) THEN
416 : CALL dump_replica_coordinates(particle_set, coords, jtarg + 1, &
417 56 : input_nr_replica, iw, neb_env%use_colvar)
418 : END IF
419 68 : IF (.NOT. neb_env%solid_state) CALL neb_initialize_velocity(vels%wrk, neb_section, particle_set, &
420 66 : jtarg + 1, iw, globenv, neb_env)
421 86 : IF (iw > 0) THEN
422 10 : WRITE (iw, '(T2,A)') 'NEB| Mapping between input and requested replica so far'
423 20 : DO j = 1, neb_nr_replica, 8
424 20 : WRITE (iw, '(T2,A,T9,8(1X,I8))') 'NEB|', rep_map(j:MIN(j + 7, neb_nr_replica))
425 : END DO
426 : END IF
427 : END DO
428 : END IF
429 8180 : vels%wrk(:, 1) = 0.0_dp
430 8180 : vels%wrk(:, neb_nr_replica) = 0.0_dp
431 : ! If we perform a DIIS optimization we don't need velocities
432 37330 : IF (neb_env%opt_type == band_diis_opt) vels%wrk = 0.0_dp
433 : ! Compute distances between replicas and in case of Cartesian Coordinates
434 : ! Rotate the frames in order to minimize the RMSD
435 240 : DO j = 1, input_nr_replica - 1
436 : CALL neb_replica_distance(particle_set, coords, j, j + 1, distance(j), iw, &
437 240 : rotate=align_interpolated)
438 : END DO
439 40 : IF (idpp) THEN
440 4 : CALL cite_reference(Smidstrup2014)
441 4 : IF (sequential) CALL cite_reference(Schmerwitz2024)
442 26 : CALL neb_initialize_idpp(coords%xyz, rep_map > 0, cell, iw, sequential)
443 4 : IF (sequential .AND. iw > 0) THEN
444 1 : WRITE (iw, '(T2,A)') "NEB| Initial band after S-IDPP"
445 9 : DO j = 1, input_nr_replica
446 9 : CALL dump_replica_coordinates(particle_set, coords, j, j, iw, neb_env%use_colvar)
447 : END DO
448 : END IF
449 22 : DO j = 1, input_nr_replica - 1
450 22 : CALL neb_replica_distance(particle_set, coords, j, j + 1, distance(j), iw, rotate=.FALSE.)
451 : END DO
452 : END IF
453 : ! If there are still large distances, hint that more replica may be requested
454 440 : IF (MAXVAL(distance) > 1.5_dp*MINVAL(distance)) THEN
455 32 : IF (iw > 0) THEN
456 4 : WRITE (iw, '(/,T2,A)') 'NEB| After interpolating replica, the maximum distance between adjacent replica'
457 4 : WRITE (iw, '(T2,A)') 'NEB| is still larger than 1.5 times the minimum distance. In order to make the'
458 4 : WRITE (iw, '(T2,A)') 'NEB| distribution of replica more uniform, consider providing more structures'
459 4 : WRITE (iw, '(T2,A)') 'NEB| with &BAND/&REPLICA sections or increasing &BAND/NUMBER_OF_REPLICA value.'
460 : END IF
461 : END IF
462 40 : DEALLOCATE (distance)
463 40 : DEALLOCATE (rep_map)
464 :
465 40 : CALL timestop(handle)
466 :
467 120 : END SUBROUTINE build_replica_coords
468 :
469 : ! **************************************************************************************************
470 : !> \brief Driver to compute energy and forces within a NEB,
471 : !> Based on the use of the replica_env
472 : !> \param rep_env ...
473 : !> \param neb_env ...
474 : !> \param coords ...
475 : !> \param energies ...
476 : !> \param forces ...
477 : !> \param particle_set ...
478 : !> \param output_unit ...
479 : !> \author Teodoro Laino 09.2006
480 : ! **************************************************************************************************
481 922 : SUBROUTINE neb_calc_energy_forces(rep_env, neb_env, coords, energies, forces, &
482 : particle_set, output_unit)
483 : TYPE(replica_env_type), POINTER :: rep_env
484 : TYPE(neb_type), OPTIONAL, POINTER :: neb_env
485 : TYPE(neb_var_type), POINTER :: coords
486 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: energies
487 : TYPE(neb_var_type), POINTER :: forces
488 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
489 : INTEGER, INTENT(IN) :: output_unit
490 :
491 : CHARACTER(len=*), PARAMETER :: routineN = 'neb_calc_energy_forces'
492 : CHARACTER(LEN=1), DIMENSION(3), PARAMETER :: lab = ["X", "Y", "Z"]
493 :
494 : INTEGER :: handle, i, ihist, ilocal, irep, j, &
495 : n_int, n_rep, n_rep_neb, nsize_wrk
496 : LOGICAL :: has_local_img
497 922 : REAL(KIND=dp), ALLOCATABLE :: cell_data(:, :)
498 922 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: tangent, tmp_a, tmp_b
499 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: cvalues, Mmatrix, Mmatrix_tmp
500 : TYPE(cell_type), POINTER :: cell
501 : TYPE(cp_subsys_type), POINTER :: subsys
502 : TYPE(virial_type), POINTER :: virial
503 :
504 922 : CALL timeset(routineN, handle)
505 922 : n_int = neb_env%nsize_int
506 922 : n_rep_neb = neb_env%number_of_replica
507 922 : n_rep = rep_env%nrep
508 922 : nsize_wrk = coords%size_wrk(1)
509 6024 : energies = 0.0_dp
510 2790 : ALLOCATE (cvalues(n_int, n_rep))
511 2790 : ALLOCATE (Mmatrix_tmp(n_int*n_int, n_rep))
512 2790 : ALLOCATE (Mmatrix(n_int*n_int, n_rep_neb))
513 922 : IF (neb_env%solid_state) THEN
514 18 : ALLOCATE (cell_data(10, n_rep))
515 6 : CALL force_env_get(neb_env%force_env, cell=cell, subsys=subsys)
516 6 : CALL cp_subsys_get(subsys, virial=virial)
517 24 : DO i = 1, n_rep_neb
518 24 : CALL solid_state_unpack(neb_env%solid_frame, coords%wrk(:, i), coords%cells(:, :, i), coords%xyz(:, i))
519 : END DO
520 : END IF
521 922 : IF (output_unit > 0) WRITE (output_unit, '(/,T2,A)') "NEB| Computing Energies and Forces"
522 3750 : DO irep = 1, n_rep_neb, n_rep
523 8454 : DO j = 0, n_rep - 1
524 8454 : IF (irep + j <= n_rep_neb) THEN
525 2174018 : rep_env%r(:, j + 1) = coords%xyz(:, irep + j)
526 : END IF
527 : END DO
528 : ! Fix file name for BAND replicas.. Each BAND replica has its own file
529 : ! independently from the number of replicas in replica_env..
530 2828 : ihist = (irep - 1)/n_rep + 1
531 2828 : ilocal = rep_env%local_rep_indices(1)
532 2828 : has_local_img = (irep + ilocal - 1 <= n_rep_neb)
533 2828 : IF (has_local_img) THEN
534 2566 : CALL handle_band_file_names(rep_env, irep, n_rep_neb, neb_env%istep)
535 : END IF
536 : ! Let's select the potential we want to use for the band calculation
537 5584 : SELECT CASE (neb_env%pot_type)
538 : CASE (pot_neb_full)
539 : ! Full potential Energy
540 2756 : IF (neb_env%solid_state .AND. has_local_img) THEN
541 9 : CALL init_cell(cell, hmat=coords%cells(:, :, irep + ilocal - 1))
542 9 : CALL cp_subsys_set(subsys, cell=cell)
543 : END IF
544 2756 : CALL rep_env_calc_e_f(rep_env, calc_f=.TRUE., wf_hist_slot=ihist, do_calc=has_local_img)
545 2756 : IF (neb_env%solid_state) THEN
546 12 : cell_data(:, :) = 0.0_dp
547 12 : IF (has_local_img) THEN
548 9 : cell_data(1:9, ilocal) = RESHAPE(virial%pv_virial, [9])
549 9 : cell_data(10, ilocal) = neb_env%external_pressure*cell%deth
550 : END IF
551 12 : CALL rep_env_sync(rep_env, cell_data)
552 : END IF
553 : CASE (pot_neb_fe)
554 : ! Free Energy Case
555 0 : CALL perform_replica_md(rep_env, coords, irep, n_rep_neb, cvalues, Mmatrix_tmp)
556 : CASE (pot_neb_me)
557 : ! Minimum Potential Energy Case
558 2828 : CALL perform_replica_geo(rep_env, coords, irep, n_rep_neb, cvalues, Mmatrix_tmp)
559 : END SELECT
560 :
561 9376 : DO j = 0, n_rep - 1
562 8454 : IF (irep + j <= n_rep_neb) THEN
563 : ! Copy back Forces and Energies
564 5102 : energies(irep + j) = rep_env%f(rep_env%ndim + 1, j + 1)
565 5102 : IF (neb_env%solid_state) THEN
566 234 : forces%xyz(:, irep + j) = rep_env%f(1:rep_env%ndim, j + 1)
567 : CALL solid_state_force(neb_env%solid_frame, coords%wrk(:, irep + j), forces%xyz(:, irep + j), &
568 : RESHAPE(cell_data(1:9, j + 1), [3, 3]), cell_data(10, j + 1), &
569 18 : forces%wrk(:, irep + j))
570 18 : energies(irep + j) = energies(irep + j) + cell_data(10, j + 1)
571 : ELSE
572 2161784 : forces%wrk(:, irep + j) = rep_env%f(1:nsize_wrk, j + 1)
573 : END IF
574 10084 : SELECT CASE (neb_env%pot_type)
575 : CASE (pot_neb_full)
576 : ! Dump Info
577 4982 : IF (output_unit > 0) THEN
578 : WRITE (output_unit, '(T2,A,I5,A,I5,A)') &
579 9 : "NEB| REPLICA Nr.", irep + j, "- Energy and Forces"
580 : WRITE (output_unit, '(T2,A,T43,A,T57,F24.12)') &
581 9 : "NEB|", "Total energy:", rep_env%f(rep_env%ndim + 1, j + 1)
582 9 : IF (neb_env%solid_state) THEN
583 9 : WRITE (output_unit, '(T2,A,T43,A,T57,F24.12)') "NEB|", "Enthalpy:", energies(irep + j)
584 9 : WRITE (output_unit, '(T2,A,1X,I0,1X,F24.12)') "SS-NEB| Volume [bohr^3]", irep + j, &
585 18 : det_3x3(coords%cells(:, :, irep + j))
586 9 : WRITE (output_unit, '(T2,A,1X,I0,6(1X,ES18.10))') "SS-NEB| Cell force", irep + j, &
587 72 : forces%wrk(rep_env%ndim + 1:, irep + j)
588 : END IF
589 9 : WRITE (output_unit, '(T2,"NEB|",T10,"ATOM",T33,3(9X,A,7X))') lab(1), lab(2), lab(3)
590 27 : DO i = 1, SIZE(particle_set)
591 : WRITE (output_unit, '(T2,"NEB|",T12,A,T30,3(2X,F15.9))') &
592 18 : particle_set(i)%atomic_kind%name, &
593 99 : rep_env%f((i - 1)*3 + 1:(i - 1)*3 + 3, j + 1)
594 : END DO
595 : END IF
596 : CASE (pot_neb_fe, pot_neb_me)
597 : ! Let's update the cartesian coordinates. This will make
598 : ! easier the next evaluation of energies and forces...
599 12360 : coords%xyz(:, irep + j) = rep_env%r(1:rep_env%ndim, j + 1)
600 240 : Mmatrix(:, irep + j) = Mmatrix_tmp(:, j + 1)
601 5222 : IF (output_unit > 0) THEN
602 : WRITE (output_unit, '(/,T2,A,I5,A,I5,A)') &
603 60 : "NEB| REPLICA Nr.", irep + j, "- Energy, Collective Variables, Forces"
604 : WRITE (output_unit, '(T2,A,T43,A,T57,F24.12)') &
605 60 : "NEB|", "Total energy:", rep_env%f(rep_env%ndim + 1, j + 1)
606 : WRITE (output_unit, &
607 60 : '(T2,"NEB|",T10,"CV Nr.",12X,"Expected COLVAR",5X,"Present COLVAR",10X,"Forces")')
608 120 : DO i = 1, n_int
609 : WRITE (output_unit, '(T2,"NEB|",T12,I2,7X,3(5X,F15.9))') &
610 120 : i, coords%int(i, irep + j), cvalues(i, j + 1), rep_env%f(i, j + 1)
611 : END DO
612 : END IF
613 : END SELECT
614 : END IF
615 : END DO
616 : END DO
617 922 : DEALLOCATE (cvalues)
618 922 : DEALLOCATE (Mmatrix_tmp)
619 922 : IF (PRESENT(neb_env)) THEN
620 : ! First identify the image of the chain with the higher potential energy
621 : ! First and last point of the band are never considered
622 4180 : neb_env%nr_HE_image = MAXLOC(energies(2:n_rep_neb - 1), 1) + 1
623 2766 : ALLOCATE (tangent(nsize_wrk))
624 : ! Then modify image forces accordingly to the scheme chosen for the
625 : ! calculation.
626 922 : neb_env%spring_energy = 0.0_dp
627 922 : IF (neb_env%optimize_end_points) THEN
628 1290 : ALLOCATE (tmp_a(SIZE(forces%wrk, 1)))
629 860 : ALLOCATE (tmp_b(SIZE(forces%wrk, 1)))
630 212314 : tmp_a(:) = forces%wrk(:, 1)
631 212314 : tmp_b(:) = forces%wrk(:, SIZE(forces%wrk, 2))
632 : END IF
633 5102 : DO i = 2, neb_env%number_of_replica
634 4180 : CALL get_tangent(neb_env, coords, i, tangent, energies, output_unit)
635 : CALL get_neb_force(neb_env, tangent, coords, i, forces, Mmatrix=Mmatrix, &
636 5102 : iw=output_unit)
637 : END DO
638 922 : IF (neb_env%optimize_end_points) THEN
639 212314 : forces%wrk(:, 1) = tmp_a ! Image A
640 212314 : forces%wrk(:, SIZE(forces%wrk, 2)) = tmp_b ! Image B
641 430 : DEALLOCATE (tmp_a)
642 430 : DEALLOCATE (tmp_b)
643 : ELSE
644 : ! Nullify forces on the two end points images
645 37236 : forces%wrk(:, 1) = 0.0_dp ! Image A
646 37236 : forces%wrk(:, SIZE(forces%wrk, 2)) = 0.0_dp ! Image B
647 : END IF
648 922 : DEALLOCATE (tangent)
649 : END IF
650 922 : DEALLOCATE (Mmatrix)
651 922 : CALL timestop(handle)
652 1844 : END SUBROUTINE neb_calc_energy_forces
653 :
654 : ! **************************************************************************************************
655 : !> \brief Driver to perform an MD run on each single replica to
656 : !> compute specifically Free Energies in a NEB scheme
657 : !> \param rep_env ...
658 : !> \param coords ...
659 : !> \param irep ...
660 : !> \param n_rep_neb ...
661 : !> \param cvalues ...
662 : !> \param Mmatrix ...
663 : !> \author Teodoro Laino 01.2007
664 : ! **************************************************************************************************
665 0 : SUBROUTINE perform_replica_md(rep_env, coords, irep, n_rep_neb, cvalues, Mmatrix)
666 : TYPE(replica_env_type), POINTER :: rep_env
667 : TYPE(neb_var_type), POINTER :: coords
668 : INTEGER, INTENT(IN) :: irep, n_rep_neb
669 : REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT) :: cvalues, Mmatrix
670 :
671 : CHARACTER(len=*), PARAMETER :: routineN = 'perform_replica_md'
672 :
673 : INTEGER :: handle, handle2, ierr, j, n_el
674 : LOGICAL :: explicit
675 : TYPE(cp_logger_type), POINTER :: logger
676 : TYPE(f_env_type), POINTER :: f_env
677 : TYPE(global_environment_type), POINTER :: globenv
678 : TYPE(section_vals_type), POINTER :: md_section, root_section
679 :
680 0 : CALL timeset(routineN, handle)
681 : CALL f_env_add_defaults(f_env_id=rep_env%f_env_id, f_env=f_env, &
682 0 : handle=handle2)
683 0 : logger => cp_get_default_logger()
684 : CALL force_env_get(f_env%force_env, globenv=globenv, &
685 0 : root_section=root_section)
686 0 : j = rep_env%local_rep_indices(1) - 1
687 0 : n_el = 3*rep_env%nparticle
688 0 : Mmatrix = 0.0_dp
689 : ! Syncronize position on the replica procs
690 0 : CALL set_pos(rep_env%f_env_id, rep_env%r(:, j + 1), n_el, ierr)
691 0 : CPASSERT(ierr == 0)
692 : !
693 0 : IF (irep + j <= n_rep_neb) THEN
694 0 : logger%iter_info%iteration(2) = irep + j
695 0 : CALL remove_restart_info(root_section)
696 0 : md_section => section_vals_get_subs_vals(root_section, "MOTION%MD")
697 0 : CALL section_vals_get(md_section, explicit=explicit)
698 0 : CPASSERT(explicit)
699 : ! Let's syncronize the target of Collective Variables for this run
700 0 : CALL set_colvars_target(coords%int(:, irep + j), f_env%force_env)
701 : ! Do a molecular dynamics and get back the derivative
702 : ! of the free energy w.r.t. the colvar and the metric tensor
703 0 : CALL qs_mol_dyn(f_env%force_env, globenv=globenv)
704 : ! Collect the equilibrated coordinates
705 0 : CALL get_pos(rep_env%f_env_id, rep_env%r(1:n_el, j + 1), n_el, ierr)
706 0 : CPASSERT(ierr == 0)
707 : ! Write he gradients in the colvar coordinates into the replica_env array
708 : ! and copy back also the metric tensor..
709 : ! work in progress..
710 0 : CPABORT("implementation incomplete in perform_replica_md")
711 0 : rep_env%f(:, j + 1) = 0.0_dp
712 0 : Mmatrix = 0.0_dp
713 : ELSE
714 0 : rep_env%r(:, j + 1) = 0.0_dp
715 0 : rep_env%f(:, j + 1) = 0.0_dp
716 0 : cvalues(:, j + 1) = 0.0_dp
717 0 : Mmatrix(:, j + 1) = 0.0_dp
718 : END IF
719 0 : CALL rep_env_sync(rep_env, rep_env%f)
720 0 : CALL rep_env_sync(rep_env, rep_env%r)
721 0 : CALL rep_env_sync(rep_env, cvalues)
722 0 : CALL rep_env_sync(rep_env, Mmatrix)
723 0 : CALL f_env_rm_defaults(f_env=f_env, ierr=ierr, handle=handle2)
724 0 : CPASSERT(ierr == 0)
725 0 : CALL timestop(handle)
726 0 : END SUBROUTINE perform_replica_md
727 :
728 : ! **************************************************************************************************
729 : !> \brief Driver to perform a GEO_OPT run on each single replica to
730 : !> compute specifically minimum energies in a collective variable
731 : !> NEB scheme
732 : !> \param rep_env ...
733 : !> \param coords ...
734 : !> \param irep ...
735 : !> \param n_rep_neb ...
736 : !> \param cvalues ...
737 : !> \param Mmatrix ...
738 : !> \author Teodoro Laino 05.2007
739 : ! **************************************************************************************************
740 72 : SUBROUTINE perform_replica_geo(rep_env, coords, irep, n_rep_neb, cvalues, Mmatrix)
741 : TYPE(replica_env_type), POINTER :: rep_env
742 : TYPE(neb_var_type), POINTER :: coords
743 : INTEGER, INTENT(IN) :: irep, n_rep_neb
744 : REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT) :: cvalues, Mmatrix
745 :
746 : CHARACTER(len=*), PARAMETER :: routineN = 'perform_replica_geo'
747 :
748 : INTEGER :: handle, handle2, ierr, j, n_el
749 : LOGICAL :: explicit
750 : TYPE(cp_logger_type), POINTER :: logger
751 : TYPE(f_env_type), POINTER :: f_env
752 : TYPE(global_environment_type), POINTER :: globenv
753 : TYPE(section_vals_type), POINTER :: geoopt_section, root_section
754 :
755 72 : CALL timeset(routineN, handle)
756 : CALL f_env_add_defaults(f_env_id=rep_env%f_env_id, f_env=f_env, &
757 72 : handle=handle2)
758 72 : logger => cp_get_default_logger()
759 : CALL force_env_get(f_env%force_env, globenv=globenv, &
760 72 : root_section=root_section)
761 72 : j = rep_env%local_rep_indices(1) - 1
762 72 : n_el = 3*rep_env%nparticle
763 360 : Mmatrix = 0.0_dp
764 : ! Syncronize position on the replica procs
765 72 : CALL set_pos(rep_env%f_env_id, rep_env%r(:, j + 1), n_el, ierr)
766 72 : CPASSERT(ierr == 0)
767 72 : IF (irep + j <= n_rep_neb) THEN
768 60 : logger%iter_info%iteration(2) = irep + j
769 60 : CALL remove_restart_info(root_section)
770 60 : geoopt_section => section_vals_get_subs_vals(root_section, "MOTION%GEO_OPT")
771 60 : CALL section_vals_get(geoopt_section, explicit=explicit)
772 60 : CPASSERT(explicit)
773 : ! Let's syncronize the target of Collective Variables for this run
774 60 : CALL set_colvars_target(coords%int(:, irep + j), f_env%force_env)
775 : ! Do a geometry optimization..
776 60 : CALL cp_geo_opt(f_env%force_env, globenv=globenv)
777 : ! Once the geometry optimization is ended let's do a single run
778 : ! without any constraints/restraints
779 : CALL force_env_calc_energy_force(f_env%force_env, &
780 60 : calc_force=.TRUE., skip_external_control=.TRUE.)
781 : ! Collect the optimized coordinates
782 60 : CALL get_pos(rep_env%f_env_id, rep_env%r(1:n_el, j + 1), n_el, ierr)
783 60 : CPASSERT(ierr == 0)
784 : ! Collect the gradients in cartesian coordinates
785 60 : CALL get_force(rep_env%f_env_id, rep_env%f(1:n_el, j + 1), n_el, ierr)
786 60 : CPASSERT(ierr == 0)
787 : ! Copy the energy
788 60 : CALL get_energy(rep_env%f_env_id, rep_env%f(n_el + 1, j + 1), ierr)
789 60 : CPASSERT(ierr == 0)
790 : ! The gradients in the colvar coordinates
791 : CALL get_clv_force(f_env%force_env, rep_env%f(1:n_el, j + 1), rep_env%r(1:n_el, j + 1), &
792 60 : SIZE(coords%xyz, 1), SIZE(coords%wrk, 1), cvalues(:, j + 1), Mmatrix(:, j + 1))
793 : ELSE
794 624 : rep_env%r(:, j + 1) = 0.0_dp
795 636 : rep_env%f(:, j + 1) = 0.0_dp
796 24 : cvalues(:, j + 1) = 0.0_dp
797 24 : Mmatrix(:, j + 1) = 0.0_dp
798 : END IF
799 72 : CALL rep_env_sync(rep_env, rep_env%f)
800 72 : CALL rep_env_sync(rep_env, rep_env%r)
801 72 : CALL rep_env_sync(rep_env, cvalues)
802 72 : CALL rep_env_sync(rep_env, Mmatrix)
803 72 : CALL f_env_rm_defaults(f_env=f_env, ierr=ierr, handle=handle2)
804 72 : CPASSERT(ierr == 0)
805 72 : CALL timestop(handle)
806 72 : END SUBROUTINE perform_replica_geo
807 :
808 : ! **************************************************************************************************
809 : !> \brief Computes the tangent for point i of the NEB chain
810 : !> \param neb_env ...
811 : !> \param coords ...
812 : !> \param i ...
813 : !> \param tangent ...
814 : !> \param energies ...
815 : !> \param iw ...
816 : !> \author Teodoro Laino 09.2006
817 : ! **************************************************************************************************
818 4180 : SUBROUTINE get_tangent(neb_env, coords, i, tangent, energies, iw)
819 : TYPE(neb_type), POINTER :: neb_env
820 : TYPE(neb_var_type), POINTER :: coords
821 : INTEGER, INTENT(IN) :: i
822 : REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: tangent
823 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: energies
824 : INTEGER, INTENT(IN) :: iw
825 :
826 : REAL(KINd=dp) :: distance0, distance1, distance2, DVmax, &
827 : Dvmin
828 :
829 4180 : CPASSERT(ASSOCIATED(coords))
830 834118 : tangent(:) = 0.0_dp
831 : ! For the last point we don't need any tangent..
832 4180 : IF (i == neb_env%number_of_replica) RETURN
833 : ! Several kind of tangents implemented...
834 3258 : SELECT CASE (neb_env%id_type)
835 : CASE (do_eb)
836 38792 : tangent(:) = 0.0_dp
837 : CASE (do_b_neb)
838 : CALL neb_replica_distance(coords=coords, i0=i, i=i - 1, distance=distance1, iw=iw, &
839 126 : rotate=.FALSE.)
840 : CALL neb_replica_distance(coords=coords, i0=i + 1, i=i, distance=distance2, iw=iw, &
841 126 : rotate=.FALSE.)
842 : tangent(:) = (coords%wrk(:, i) - coords%wrk(:, i - 1))/distance1 + &
843 6552 : (coords%wrk(:, i + 1) - coords%wrk(:, i))/distance2
844 : CASE (do_it_neb, do_ci_neb, do_d_neb)
845 2008 : IF ((energies(i + 1) > energies(i)) .AND. (energies(i) > (energies(i - 1)))) THEN
846 226942 : tangent(:) = coords%wrk(:, i + 1) - coords%wrk(:, i)
847 1312 : ELSE IF ((energies(i + 1) < energies(i)) .AND. (energies(i) < (energies(i - 1)))) THEN
848 35296 : tangent(:) = coords%wrk(:, i) - coords%wrk(:, i - 1)
849 : ELSE
850 1204 : DVmax = MAX(ABS(energies(i + 1) - energies(i)), ABS(energies(i - 1) - energies(i)))
851 1204 : DVmin = MIN(ABS(energies(i + 1) - energies(i)), ABS(energies(i - 1) - energies(i)))
852 1204 : IF (energies(i + 1) >= energies(i - 1)) THEN
853 26084 : tangent(:) = (coords%wrk(:, i + 1) - coords%wrk(:, i))*DVmax + (coords%wrk(:, i) - coords%wrk(:, i - 1))*DVmin
854 : ELSE
855 231246 : tangent(:) = (coords%wrk(:, i + 1) - coords%wrk(:, i))*DVmin + (coords%wrk(:, i) - coords%wrk(:, i - 1))*DVmax
856 : END IF
857 : END IF
858 : CASE (do_sm)
859 : ! String method..
860 22536 : tangent(:) = 0.0_dp
861 : END SELECT
862 584568 : distance0 = NORM2(tangent(:))
863 3258 : IF (neb_env%solid_state .AND. distance0 == 0.0_dp) THEN
864 0 : tangent(:) = coords%wrk(:, i + 1) - coords%wrk(:, i - 1)
865 0 : distance0 = NORM2(tangent(:))
866 : END IF
867 527244 : IF (distance0 /= 0.0_dp) tangent(:) = tangent(:)/distance0
868 : END SUBROUTINE get_tangent
869 :
870 : ! **************************************************************************************************
871 : !> \brief Computes the forces for point i of the NEB chain
872 : !> \param neb_env ...
873 : !> \param tangent ...
874 : !> \param coords ...
875 : !> \param i ...
876 : !> \param forces ...
877 : !> \param tag ...
878 : !> \param Mmatrix ...
879 : !> \param iw ...
880 : !> \author Teodoro Laino 09.2006
881 : ! **************************************************************************************************
882 4680 : RECURSIVE SUBROUTINE get_neb_force(neb_env, tangent, coords, i, forces, tag, Mmatrix, &
883 : iw)
884 : TYPE(neb_type), POINTER :: neb_env
885 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: tangent
886 : TYPE(neb_var_type), POINTER :: coords
887 : INTEGER, INTENT(IN) :: i
888 : TYPE(neb_var_type), POINTER :: forces
889 : INTEGER, INTENT(IN), OPTIONAL :: tag
890 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: Mmatrix
891 : INTEGER, INTENT(IN) :: iw
892 :
893 : INTEGER :: j, my_tag, nsize_wrk
894 : REAL(KIND=dp) :: distance1, distance2, fac, tmp
895 4680 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: dtmp1, wrk
896 :
897 4680 : my_tag = neb_env%id_type
898 4680 : IF (PRESENT(tag)) my_tag = tag
899 4680 : CPASSERT(ASSOCIATED(forces))
900 4680 : CPASSERT(ASSOCIATED(coords))
901 4680 : nsize_wrk = coords%size_wrk(1)
902 : ! All methods but not the classical elastic band will skip the force
903 : ! calculation for the last frame of the band
904 3386 : SELECT CASE (my_tag)
905 : CASE (do_b_neb, do_it_neb, do_ci_neb, do_d_neb)
906 3386 : IF (i == neb_env%number_of_replica) RETURN
907 : CASE (do_sm)
908 : ! String Method
909 : ! The forces do not require any projection. Reparametrization required
910 : ! after the update of the replica.
911 420 : CALL cite_reference(E2002)
912 4680 : RETURN
913 : END SELECT
914 : ! otherwise proceeed normally..
915 10524 : ALLOCATE (wrk(nsize_wrk))
916 : ! Spring Energy
917 : CALL neb_replica_distance(coords=coords, i0=i - 1, i=i, distance=distance1, iw=iw, &
918 3508 : rotate=.FALSE.)
919 3508 : tmp = distance1 - neb_env%avg_distance
920 3508 : neb_env%spring_energy = neb_env%spring_energy + 0.5_dp*neb_env%k*tmp**2
921 874 : SELECT CASE (my_tag)
922 : CASE (do_eb)
923 874 : CALL cite_reference(Elber1987)
924 : ! Elastic band - Hamiltonian formulation according the original Karplus/Elber
925 : ! formulation
926 1748 : ALLOCATE (dtmp1(nsize_wrk))
927 : ! derivatives of the spring
928 874 : tmp = distance1 - neb_env%avg_distance
929 45448 : dtmp1(:) = 1.0_dp/distance1*(coords%wrk(:, i) - coords%wrk(:, i - 1))
930 45448 : wrk(:) = neb_env%k*tmp*dtmp1
931 45448 : forces%wrk(:, i) = forces%wrk(:, i) - wrk
932 45448 : forces%wrk(:, i - 1) = forces%wrk(:, i - 1) + wrk
933 : ! derivatives due to the average length of the spring
934 874 : fac = 1.0_dp/(neb_env%avg_distance*REAL(neb_env%number_of_replica - 1, KIND=dp))
935 45448 : wrk(:) = neb_env%k*fac*(coords%wrk(:, i) - coords%wrk(:, i - 1))
936 874 : tmp = 0.0_dp
937 7880 : DO j = 2, neb_env%number_of_replica
938 : CALL neb_replica_distance(coords=coords, i0=j - 1, i=j, distance=distance1, iw=iw, &
939 7006 : rotate=.FALSE.)
940 7880 : tmp = tmp + distance1 - neb_env%avg_distance
941 : END DO
942 45448 : forces%wrk(:, i) = forces%wrk(:, i) + wrk*tmp
943 45448 : forces%wrk(:, i - 1) = forces%wrk(:, i - 1) - wrk*tmp
944 874 : DEALLOCATE (dtmp1)
945 : CASE (do_b_neb)
946 : ! Bisection NEB
947 126 : CALL cite_reference(Jonsson1998)
948 6552 : wrk(:) = (coords%wrk(:, i + 1) - 2.0_dp*coords%wrk(:, i) + coords%wrk(:, i - 1))
949 6552 : tmp = neb_env%k*DOT_PRODUCT(wrk, tangent)
950 6552 : wrk(:) = forces%wrk(:, i) - dot_product_band(neb_env, forces%wrk(:, i), tangent, Mmatrix)*tangent
951 6552 : forces%wrk(:, i) = wrk + tmp*tangent
952 : CASE (do_it_neb)
953 : ! Improved tangent NEB
954 1266 : CALL cite_reference(Jonsson2000_1)
955 : CALL neb_replica_distance(coords=coords, i0=i, i=i + 1, distance=distance1, iw=iw, &
956 1266 : rotate=.FALSE.)
957 : CALL neb_replica_distance(coords=coords, i0=i - 1, i=i, distance=distance2, iw=iw, &
958 1266 : rotate=.FALSE.)
959 1266 : tmp = neb_env%k*(distance1 - distance2)
960 309870 : wrk(:) = forces%wrk(:, i) - dot_product_band(neb_env, forces%wrk(:, i), tangent, Mmatrix)*tangent
961 309870 : forces%wrk(:, i) = wrk + tmp*tangent
962 : CASE (do_ci_neb)
963 : ! Climbing Image NEB
964 864 : CALL cite_reference(Jonsson2000_2)
965 864 : IF (neb_env%istep <= neb_env%nsteps_it .OR. i /= neb_env%nr_HE_image) THEN
966 500 : CALL get_neb_force(neb_env, tangent, coords, i, forces, do_it_neb, Mmatrix, iw)
967 : ELSE
968 190042 : wrk(:) = forces%wrk(:, i)
969 364 : tmp = -2.0_dp*dot_product_band(neb_env, wrk, tangent, Mmatrix)
970 190042 : forces%wrk(:, i) = wrk + tmp*tangent
971 : END IF
972 : CASE (do_d_neb)
973 : ! Doubly NEB
974 378 : CALL cite_reference(Wales2004)
975 756 : ALLOCATE (dtmp1(nsize_wrk))
976 19656 : dtmp1(:) = forces%wrk(:, i) - dot_product_band(neb_env, forces%wrk(:, i), tangent, Mmatrix)*tangent
977 19656 : forces%wrk(:, i) = dtmp1
978 19656 : tmp = NORM2(dtmp1)
979 19656 : dtmp1(:) = dtmp1(:)/tmp
980 : ! Project out only the spring component interfering with the
981 : ! orthogonal gradient of the band
982 19656 : wrk(:) = (coords%wrk(:, i + 1) - 2.0_dp*coords%wrk(:, i) + coords%wrk(:, i - 1))
983 19656 : tmp = DOT_PRODUCT(wrk, dtmp1)
984 19656 : dtmp1(:) = neb_env%k*(wrk(:) - tmp*dtmp1(:))
985 19656 : forces%wrk(:, i) = forces%wrk(:, i) + dtmp1(:)
986 3886 : DEALLOCATE (dtmp1)
987 : END SELECT
988 3508 : DEALLOCATE (wrk)
989 : END SUBROUTINE get_neb_force
990 :
991 : ! **************************************************************************************************
992 : !> \brief Handles the dot_product when using colvar.. in this case
993 : !> the scalar product needs to take into account the metric
994 : !> tensor
995 : !> \param neb_env ...
996 : !> \param array1 ...
997 : !> \param array2 ...
998 : !> \param array3 ...
999 : !> \return ...
1000 : !> \author Teodoro Laino 09.2006
1001 : ! **************************************************************************************************
1002 2134 : FUNCTION dot_product_band(neb_env, array1, array2, array3) RESULT(value)
1003 : TYPE(neb_type), POINTER :: neb_env
1004 : REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: array1, array2
1005 : REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: array3
1006 : REAL(KIND=dp) :: value
1007 :
1008 : INTEGER :: nsize_int
1009 : LOGICAL :: check
1010 :
1011 2134 : IF (neb_env%use_colvar) THEN
1012 72 : nsize_int = neb_env%nsize_int
1013 : check = ((SIZE(array1) /= SIZE(array2)) .OR. &
1014 : (SIZE(array1) /= nsize_int) .OR. &
1015 216 : (SIZE(array3) /= nsize_int*nsize_int))
1016 : ! This condition should always be satisfied..
1017 0 : CPASSERT(check)
1018 576 : value = DOT_PRODUCT(MATMUL(RESHAPE(array3, [nsize_int, nsize_int]), array1), array2)
1019 : ELSE
1020 525976 : value = DOT_PRODUCT(array1, array2)
1021 : END IF
1022 2134 : END FUNCTION dot_product_band
1023 :
1024 : ! **************************************************************************************************
1025 : !> \brief Reorient iteratively all images of the NEB chain in order to
1026 : !> have always the smaller RMSD between two following images
1027 : !> \param rotate_frames ...
1028 : !> \param particle_set ...
1029 : !> \param coords ...
1030 : !> \param vels ...
1031 : !> \param iw ...
1032 : !> \param distances ...
1033 : !> \param number_of_replica ...
1034 : !> \author Teodoro Laino 09.2006
1035 : ! **************************************************************************************************
1036 922 : SUBROUTINE reorient_images(rotate_frames, particle_set, coords, vels, iw, &
1037 922 : distances, number_of_replica)
1038 : LOGICAL, INTENT(IN) :: rotate_frames
1039 : TYPE(particle_type), DIMENSION(:), OPTIONAL, &
1040 : POINTER :: particle_set
1041 : TYPE(neb_var_type), POINTER :: coords, vels
1042 : INTEGER, INTENT(IN) :: iw
1043 : REAL(KIND=dp), DIMENSION(:), OPTIONAL :: distances
1044 : INTEGER, INTENT(IN) :: number_of_replica
1045 :
1046 : INTEGER :: i, k, kind
1047 : LOGICAL :: check
1048 : REAL(KIND=dp) :: xtmp
1049 : REAL(KIND=dp), DIMENSION(3) :: tmp
1050 : REAL(KIND=dp), DIMENSION(3, 3) :: rot
1051 :
1052 922 : rot = 0.0_dp
1053 922 : rot(1, 1) = 1.0_dp
1054 922 : rot(2, 2) = 1.0_dp
1055 922 : rot(3, 3) = 1.0_dp
1056 5102 : DO i = 2, number_of_replica
1057 : ! The rotation of the replica is enabled exclusively when working in
1058 : ! cartesian coordinates
1059 4180 : IF (rotate_frames .AND. (coords%in_use == do_band_cartesian)) THEN
1060 : CALL rmsd3(particle_set, coords%xyz(:, i), coords%xyz(:, i - 1), iw, &
1061 540 : rotate=.TRUE., rot=rot)
1062 : ! Rotate velocities
1063 11796 : DO k = 1, SIZE(vels%xyz, 1)/3
1064 11256 : kind = (k - 1)*3
1065 45024 : tmp = vels%xyz(kind + 1:kind + 3, i)
1066 45564 : vels%xyz(kind + 1:kind + 3, i) = MATMUL(TRANSPOSE(rot), tmp)
1067 : END DO
1068 : END IF
1069 5102 : IF (PRESENT(distances)) THEN
1070 4180 : check = SIZE(distances) == (number_of_replica - 1)
1071 4180 : CPASSERT(check)
1072 : xtmp = DOT_PRODUCT(coords%wrk(:, i) - coords%wrk(:, i - 1), &
1073 834118 : coords%wrk(:, i) - coords%wrk(:, i - 1))
1074 4180 : distances(i - 1) = SQRT(xtmp)
1075 : END IF
1076 : END DO
1077 922 : END SUBROUTINE reorient_images
1078 :
1079 : ! **************************************************************************************************
1080 : !> \brief Reparametrization of the replica for String Method with splines
1081 : !> \param reparametrize_frames ...
1082 : !> \param spline_order ...
1083 : !> \param smoothing ...
1084 : !> \param coords ...
1085 : !> \param sline ...
1086 : !> \param distances ...
1087 : !> \author Teodoro Laino - Rodolphe Vuilleumier 09.2008
1088 : ! **************************************************************************************************
1089 416 : SUBROUTINE reparametrize_images(reparametrize_frames, spline_order, smoothing, &
1090 416 : coords, sline, distances)
1091 :
1092 : LOGICAL, INTENT(IN) :: reparametrize_frames
1093 : INTEGER, INTENT(IN) :: spline_order
1094 : REAL(KIND=dp), INTENT(IN) :: smoothing
1095 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: coords, sline
1096 : REAL(KIND=dp), DIMENSION(:) :: distances
1097 :
1098 : INTEGER :: i, j
1099 : REAL(KIND=dp) :: avg_distance, xtmp
1100 416 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: tmp_coords
1101 :
1102 416 : IF (reparametrize_frames) THEN
1103 168 : ALLOCATE (tmp_coords(SIZE(coords, 1), SIZE(coords, 2)))
1104 24066 : tmp_coords(:, :) = coords
1105 : ! Smoothing
1106 420 : DO i = 2, SIZE(coords, 2) - 1
1107 : coords(:, i) = tmp_coords(:, i)*(1.0_dp - 2.0_dp*smoothing) + &
1108 19698 : tmp_coords(:, i - 1)*smoothing + tmp_coords(:, i + 1)*smoothing
1109 : END DO
1110 48090 : sline = coords - tmp_coords + sline
1111 24066 : tmp_coords(:, :) = coords
1112 : ! Reparametrization
1113 84 : SELECT CASE (spline_order)
1114 : CASE (1)
1115 : ! Compute distances
1116 462 : DO i = 2, SIZE(coords, 2)
1117 21840 : xtmp = DOT_PRODUCT(coords(:, i) - coords(:, i - 1), coords(:, i) - coords(:, i - 1))
1118 462 : distances(i - 1) = SQRT(xtmp)
1119 : END DO
1120 462 : avg_distance = SUM(distances)/REAL(SIZE(coords, 2) - 1, KIND=dp)
1121 : ! Redistribute frames
1122 420 : DO i = 2, SIZE(coords, 2) - 1
1123 378 : xtmp = 0.0_dp
1124 2022 : DO j = 1, SIZE(coords, 2) - 1
1125 1980 : xtmp = xtmp + distances(j)
1126 1980 : IF (xtmp > avg_distance*REAL(i - 1, KIND=dp)) THEN
1127 378 : xtmp = (xtmp - avg_distance*REAL(i - 1, KIND=dp))/distances(j)
1128 19656 : coords(:, i) = (1.0_dp - xtmp)*tmp_coords(:, j + 1) + xtmp*tmp_coords(:, j)
1129 : EXIT
1130 : END IF
1131 : END DO
1132 : END DO
1133 : ! Re-compute distances
1134 462 : DO i = 2, SIZE(coords, 2)
1135 21840 : xtmp = DOT_PRODUCT(coords(:, i) - coords(:, i - 1), coords(:, i) - coords(:, i - 1))
1136 462 : distances(i - 1) = SQRT(xtmp)
1137 : END DO
1138 : CASE DEFAULT
1139 42 : CPWARN("String Method: Spline order greater than 1 not implemented.")
1140 : END SELECT
1141 48090 : sline = coords - tmp_coords + sline
1142 42 : DEALLOCATE (tmp_coords)
1143 : END IF
1144 416 : END SUBROUTINE reparametrize_images
1145 :
1146 : ! **************************************************************************************************
1147 : !> \brief Checks for convergence criteria during a NEB run
1148 : !> \param neb_env ...
1149 : !> \param Dcoords ...
1150 : !> \param forces ...
1151 : !> \param logger ...
1152 : !> \return ...
1153 : !> \author Teodoro Laino 10.2006
1154 : ! **************************************************************************************************
1155 2760 : FUNCTION check_convergence(neb_env, Dcoords, forces, logger) RESULT(converged)
1156 : TYPE(neb_type), POINTER :: neb_env
1157 : TYPE(neb_var_type), POINTER :: Dcoords, forces
1158 : TYPE(cp_logger_type), POINTER :: logger
1159 : LOGICAL :: converged
1160 :
1161 : CHARACTER(LEN=3), DIMENSION(4) :: labels
1162 : INTEGER :: iw
1163 : REAL(KIND=dp) :: max_dr, max_force, my_max_dr, &
1164 : my_max_force, my_rms_dr, my_rms_force, &
1165 : rms_dr, rms_force
1166 : TYPE(section_vals_type), POINTER :: cc_section
1167 :
1168 552 : NULLIFY (cc_section)
1169 552 : cc_section => section_vals_get_subs_vals(neb_env%neb_section, "CONVERGENCE_CONTROL")
1170 552 : CALL section_vals_val_get(cc_section, "MAX_DR", r_val=max_dr)
1171 552 : CALL section_vals_val_get(cc_section, "MAX_FORCE", r_val=max_force)
1172 552 : CALL section_vals_val_get(cc_section, "RMS_DR", r_val=rms_dr)
1173 552 : CALL section_vals_val_get(cc_section, "RMS_FORCE", r_val=rms_force)
1174 552 : converged = .FALSE.
1175 2760 : labels = " NO"
1176 562624 : my_max_dr = MAXVAL(ABS(Dcoords%wrk))
1177 562624 : my_max_force = MAXVAL(ABS(forces%wrk))
1178 562624 : my_rms_dr = SQRT(SUM(Dcoords%wrk*Dcoords%wrk)/REAL(SIZE(Dcoords%wrk, 1)*SIZE(Dcoords%wrk, 2), KIND=dp))
1179 562624 : my_rms_force = SQRT(SUM(forces%wrk*forces%wrk)/REAL(SIZE(forces%wrk, 1)*SIZE(forces%wrk, 2), KIND=dp))
1180 552 : IF (my_max_dr < max_dr) labels(1) = "YES"
1181 552 : IF (my_max_force < max_force) labels(2) = "YES"
1182 552 : IF (my_rms_dr < rms_dr) labels(3) = "YES"
1183 552 : IF (my_rms_force < rms_force) labels(4) = "YES"
1184 594 : IF (ALL(labels == "YES")) converged = .TRUE.
1185 :
1186 : iw = cp_print_key_unit_nr(logger, neb_env%neb_section, "CONVERGENCE_INFO", &
1187 552 : extension=".nebLog")
1188 552 : IF (iw > 0) THEN
1189 : ! Print convergence info
1190 276 : WRITE (iw, FMT='(A,A)') ' **************************************', &
1191 552 : '*****************************************'
1192 : WRITE (iw, FMT='(1X,A,2X,F16.10,5X,"[",F16.10,1X,"]",T76,"(",A,")")') &
1193 276 : 'RMS DISPLACEMENT =', my_rms_dr, rms_dr, labels(3), &
1194 276 : 'MAX DISPLACEMENT =', my_max_dr, max_dr, labels(1), &
1195 276 : 'RMS FORCE =', my_rms_force, rms_force, labels(4), &
1196 552 : 'MAX FORCE =', my_max_force, max_force, labels(2)
1197 276 : WRITE (iw, FMT='(A,A)') ' **************************************', &
1198 552 : '*****************************************'
1199 : END IF
1200 : CALL cp_print_key_finished_output(iw, logger, neb_env%neb_section, &
1201 552 : "CONVERGENCE_INFO")
1202 552 : END FUNCTION check_convergence
1203 :
1204 72 : END MODULE neb_utils
|