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 performing a 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 09.2006
19 : !> \par History
20 : !> - Teodoro Laino 10.2008 [tlaino] - University of Zurich
21 : !> Extension to a subspace of collective variables
22 : ! **************************************************************************************************
23 : MODULE neb_methods
24 : USE bibliography, ONLY: Sheppard2012,&
25 : cite_reference
26 : USE cell_types, ONLY: cell_sym_none,&
27 : cell_type
28 : USE colvar_utils, ONLY: number_of_colvar
29 : USE cp_external_control, ONLY: external_control
30 : USE cp_log_handling, ONLY: cp_get_default_logger,&
31 : cp_logger_type,&
32 : cp_to_string
33 : USE cp_output_handling, ONLY: cp_add_iter_level,&
34 : cp_iterate,&
35 : cp_print_key_finished_output,&
36 : cp_print_key_unit_nr,&
37 : cp_rm_iter_level
38 : USE cp_subsys_types, ONLY: cp_subsys_get,&
39 : cp_subsys_type
40 : USE f77_interface, ONLY: f_env_add_defaults,&
41 : f_env_rm_defaults,&
42 : f_env_type
43 : USE force_env_types, ONLY: force_env_get,&
44 : use_fist_force,&
45 : use_qs_force
46 : USE global_types, ONLY: global_environment_type
47 : USE header, ONLY: band_header
48 : USE input_constants, ONLY: band_diis_opt,&
49 : band_md_opt,&
50 : do_ci_neb,&
51 : do_it_neb,&
52 : do_rep_blocked,&
53 : do_sm,&
54 : pot_neb_full
55 : USE input_section_types, ONLY: section_type,&
56 : section_vals_get,&
57 : section_vals_get_subs_vals,&
58 : section_vals_type,&
59 : section_vals_val_get
60 : USE kinds, ONLY: dp
61 : USE message_passing, ONLY: mp_para_env_type
62 : USE neb_io, ONLY: dump_neb_final,&
63 : dump_neb_info,&
64 : neb_rep_env_map_info,&
65 : read_neb_section
66 : USE neb_md_utils, ONLY: control_vels_a,&
67 : control_vels_b
68 : USE neb_opt_utils, ONLY: accept_diis_step,&
69 : neb_ls
70 : USE neb_types, ONLY: neb_type,&
71 : neb_var_create,&
72 : neb_var_release,&
73 : neb_var_type
74 : USE neb_utils, ONLY: build_replica_coords,&
75 : check_convergence,&
76 : neb_calc_energy_forces,&
77 : reorient_images,&
78 : reparametrize_images
79 : USE particle_types, ONLY: particle_type
80 : USE physcon, ONLY: massunit
81 : USE replica_methods, ONLY: rep_env_create
82 : USE replica_types, ONLY: rep_env_release,&
83 : replica_env_type
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_methods'
90 : LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .FALSE.
91 : PUBLIC :: neb
92 :
93 : CONTAINS
94 :
95 : ! **************************************************************************************************
96 : !> \brief Real subroutine for NEB calculations
97 : !> \param input ...
98 : !> \param input_declaration ...
99 : !> \param para_env ...
100 : !> \param globenv ...
101 : !> \author Teodoro Laino 09.2006
102 : !> \note
103 : !> Based on the use of replica_env
104 : ! **************************************************************************************************
105 200 : SUBROUTINE neb(input, input_declaration, para_env, globenv)
106 : TYPE(section_vals_type), POINTER :: input
107 : TYPE(section_type), POINTER :: input_declaration
108 : TYPE(mp_para_env_type), POINTER :: para_env
109 : TYPE(global_environment_type), POINTER :: globenv
110 :
111 : CHARACTER(len=*), PARAMETER :: routineN = 'neb'
112 :
113 : INTEGER :: force_kind, handle, ierr, iw, iw2, &
114 : nr_replica, nrep, nrep_total, &
115 : output_unit, pot_type, prep, &
116 : proc_dist_type
117 : LOGICAL :: check, explicit, row_force
118 : TYPE(cell_type), POINTER :: cell
119 : TYPE(cp_logger_type), POINTER :: logger
120 : TYPE(cp_subsys_type), POINTER :: subsys
121 : TYPE(f_env_type), POINTER :: f_env
122 : TYPE(neb_type), POINTER :: neb_env
123 : TYPE(neb_var_type), POINTER :: coords, forces, vels
124 40 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
125 : TYPE(replica_env_type), POINTER :: rep_env
126 : TYPE(section_vals_type), POINTER :: diis_section, force_env_section, &
127 : md_section, motion_section, &
128 : neb_section, print_section
129 : TYPE(virial_type), POINTER :: virial
130 :
131 40 : CALL timeset(routineN, handle)
132 40 : NULLIFY (logger, subsys, f_env, rep_env)
133 40 : NULLIFY (forces, coords, vels, neb_env)
134 40 : logger => cp_get_default_logger()
135 40 : CALL cp_add_iter_level(logger, "BAND")
136 40 : motion_section => section_vals_get_subs_vals(input, "MOTION")
137 40 : print_section => section_vals_get_subs_vals(motion_section, "PRINT")
138 40 : neb_section => section_vals_get_subs_vals(motion_section, "BAND")
139 : output_unit = cp_print_key_unit_nr(logger, neb_section, "PROGRAM_RUN_INFO", &
140 40 : extension=".nebLog")
141 40 : CALL section_vals_val_get(neb_section, "NPROC_REP", i_val=prep)
142 40 : CALL section_vals_val_get(neb_section, "PROC_DIST_TYPE", i_val=proc_dist_type)
143 40 : CALL section_vals_val_get(neb_section, "NUMBER_OF_REPLICA", i_val=nr_replica)
144 40 : CALL section_vals_val_get(neb_section, "POT_TYPE", i_val=pot_type)
145 40 : row_force = (proc_dist_type == do_rep_blocked)
146 40 : nrep = MAX(1, para_env%num_pe/prep)
147 40 : nrep_total = nrep
148 40 : IF (pot_type == pot_neb_full) nrep_total = nr_replica
149 40 : IF (nrep*prep /= para_env%num_pe .AND. output_unit > 0) THEN
150 : CALL cp_warn(__LOCATION__, &
151 : "Number of totally requested processors ("//TRIM(ADJUSTL(cp_to_string(para_env%num_pe)))//") "// &
152 : "is not compatible with the number of processors requested per replica ("// &
153 : TRIM(ADJUSTL(cp_to_string(prep)))//") and the number of replicas ("// &
154 : TRIM(ADJUSTL(cp_to_string(nrep)))//") . ["// &
155 0 : TRIM(ADJUSTL(cp_to_string(para_env%num_pe - nrep*prep)))//"] processors will be wasted!")
156 : END IF
157 40 : force_env_section => section_vals_get_subs_vals(input, "FORCE_EVAL")
158 : ! Create Replica Environments
159 40 : IF (output_unit > 0) WRITE (output_unit, '(T2,"NEB|",A)') " Replica_env Setup. START"
160 : CALL rep_env_create(rep_env, para_env=para_env, input=input, &
161 : input_declaration=input_declaration, nrep=nrep, prep=prep, &
162 40 : row_force=row_force, nrep_total=nrep_total)
163 40 : IF (output_unit > 0) WRITE (output_unit, '(T2,"NEB|",A)') " Replica_env Setup. END"
164 40 : IF (ASSOCIATED(rep_env)) THEN
165 40 : CPASSERT(SIZE(rep_env%local_rep_indices) == 1)
166 40 : CALL f_env_add_defaults(f_env_id=rep_env%f_env_id, f_env=f_env)
167 40 : CALL force_env_get(f_env%force_env, subsys=subsys)
168 40 : particle_set => subsys%particles%els
169 : ! Read NEB controlling parameters
170 1080 : ALLOCATE (neb_env)
171 40 : neb_env%force_env => f_env%force_env
172 40 : neb_env%root_section => input
173 40 : neb_env%force_env_section => force_env_section
174 40 : neb_env%motion_print_section => print_section
175 40 : neb_env%neb_section => neb_section
176 40 : neb_env%nsize_xyz = rep_env%ndim
177 40 : neb_env%nsize_int = number_of_colvar(f_env%force_env)
178 40 : check = (neb_env%nsize_xyz >= neb_env%nsize_int)
179 40 : CPASSERT(check)
180 : ! Check that the used colvar are uniquely determined
181 : check = (number_of_colvar(f_env%force_env) == &
182 40 : number_of_colvar(f_env%force_env, unique=.TRUE.))
183 40 : CPASSERT(check)
184 40 : CALL read_neb_section(neb_env, neb_section)
185 40 : CALL section_vals_val_get(neb_section, "SOLID_STATE", l_val=neb_env%solid_state)
186 40 : IF (neb_env%solid_state) THEN
187 2 : CALL cite_reference(Sheppard2012)
188 2 : CALL section_vals_val_get(neb_section, "CELL_WEIGHT", r_val=neb_env%cell_weight)
189 2 : CALL section_vals_val_get(neb_section, "EXTERNAL_PRESSURE", r_val=neb_env%external_pressure)
190 2 : IF (.NOT. (neb_env%cell_weight > 0.0_dp)) THEN
191 0 : CPABORT("SOLID_STATE requires a positive CELL_WEIGHT.")
192 : END IF
193 : IF (nr_replica < 3 .OR. pot_type /= pot_neb_full .OR. neb_env%opt_type /= band_diis_opt .OR. &
194 2 : (neb_env%id_type /= do_it_neb .AND. neb_env%id_type /= do_ci_neb) .OR. &
195 : neb_env%optimize_end_points) THEN
196 : CALL cp_abort(__LOCATION__, &
197 : "SOLID_STATE requires at least three replicas, POT_TYPE FULL, DIIS, IT-NEB or CI-NEB, "// &
198 0 : "and fixed endpoints.")
199 : END IF
200 2 : CALL force_env_get(neb_env%force_env, in_use=force_kind, cell=cell)
201 2 : CALL cp_subsys_get(subsys, virial=virial)
202 2 : IF (force_kind /= use_qs_force .AND. force_kind /= use_fist_force) THEN
203 0 : CPABORT("SOLID_STATE currently supports Quickstep and FIST.")
204 : END IF
205 2 : IF (.NOT. virial%pv_availability .OR. virial%pv_numer .OR. virial%pv_diagonal) THEN
206 0 : CPABORT("SOLID_STATE requires FORCE_EVAL/STRESS_TENSOR ANALYTICAL.")
207 : END IF
208 8 : IF (ANY(cell%perd /= 1) .OR. cell%symmetry_id /= cell_sym_none .OR. cell%input_cell_canonicalized) THEN
209 : CALL cp_abort(__LOCATION__, &
210 0 : "SOLID_STATE requires PERIODIC XYZ, no SUBSYS/CELL symmetry, and no automatic cell rotation.")
211 : END IF
212 2 : CALL section_vals_get(section_vals_get_subs_vals(motion_section, "CONSTRAINT"), explicit=explicit)
213 2 : IF (explicit .OR. rep_env%ndim /= 3*SIZE(particle_set)) THEN
214 0 : CPABORT("SOLID_STATE does not support constraints or shell particles.")
215 : END IF
216 : ! The cell frame defines the alignment between replicas.
217 2 : neb_env%align_frames = .FALSE.
218 2 : neb_env%rotate_frames = .FALSE.
219 : END IF
220 : ! Print BAND header
221 40 : iw2 = cp_print_key_unit_nr(logger, neb_section, "BANNER", extension=".nebLog")
222 40 : CALL band_header(iw2, neb_env%number_of_replica, nrep, prep)
223 40 : CALL cp_print_key_finished_output(iw2, logger, neb_section, "BANNER")
224 : ! Allocate the principal vectors used in the BAND calculation
225 40 : CALL neb_var_create(coords, neb_env, full_allocation=.TRUE.)
226 40 : CALL neb_var_create(forces, neb_env)
227 40 : CALL neb_var_create(vels, neb_env)
228 : ! Collecting the coordinates of the starting replicas of the BAND calculation
229 40 : IF (output_unit > 0) WRITE (output_unit, '(T2,"NEB|",A)') " Building initial set of coordinates. START"
230 : iw = cp_print_key_unit_nr(logger, neb_section, "PROGRAM_RUN_INFO/INITIAL_CONFIGURATION_INFO", &
231 40 : extension=".nebLog")
232 : CALL build_replica_coords(neb_section, particle_set, coords, vels, neb_env, iw, globenv, &
233 40 : rep_env%para_env)
234 : CALL cp_print_key_finished_output(iw, logger, neb_section, &
235 40 : "PROGRAM_RUN_INFO/INITIAL_CONFIGURATION_INFO")
236 40 : IF (output_unit > 0) WRITE (output_unit, '(T2,"NEB|",A)') " Building initial set of coordinates. END"
237 : ! Print some additional info in the replica_env initialization file
238 40 : CALL neb_rep_env_map_info(rep_env, neb_env)
239 : ! Perform NEB optimization
240 56 : SELECT CASE (neb_env%opt_type)
241 : CASE (band_md_opt)
242 16 : neb_env%opt_type_label = "MOLECULAR DYNAMICS"
243 16 : md_section => section_vals_get_subs_vals(neb_section, "OPTIMIZE_BAND%MD")
244 : CALL neb_md(rep_env, neb_env, coords, vels, forces, particle_set, output_unit, &
245 16 : md_section, logger, globenv)
246 : CASE (band_diis_opt)
247 24 : neb_env%opt_type_label = "DIIS"
248 24 : diis_section => section_vals_get_subs_vals(neb_section, "OPTIMIZE_BAND%DIIS")
249 : CALL neb_diis(rep_env, neb_env, coords, vels, forces, particle_set, output_unit, &
250 64 : diis_section, logger, globenv)
251 : END SELECT
252 : ! Release force_eval
253 40 : CALL f_env_rm_defaults(f_env, ierr)
254 : ! Release coords, vels and forces
255 40 : CALL neb_var_release(coords)
256 40 : CALL neb_var_release(forces)
257 40 : CALL neb_var_release(vels)
258 : ! At the end let's destroy the environment of the BAND calculation
259 40 : DEALLOCATE (neb_env)
260 : END IF
261 40 : CALL rep_env_release(rep_env)
262 : CALL cp_print_key_finished_output(output_unit, logger, neb_section, &
263 40 : "PROGRAM_RUN_INFO")
264 40 : CALL cp_rm_iter_level(logger, "BAND")
265 40 : CALL timestop(handle)
266 40 : END SUBROUTINE neb
267 :
268 : ! **************************************************************************************************
269 : !> \brief MD type optimization NEB
270 : !> \param rep_env ...
271 : !> \param neb_env ...
272 : !> \param coords ...
273 : !> \param vels ...
274 : !> \param forces ...
275 : !> \param particle_set ...
276 : !> \param output_unit ...
277 : !> \param md_section ...
278 : !> \param logger ...
279 : !> \param globenv ...
280 : !> \author Teodoro Laino 09.2006
281 : ! **************************************************************************************************
282 16 : SUBROUTINE neb_md(rep_env, neb_env, coords, vels, forces, particle_set, output_unit, &
283 : md_section, logger, globenv)
284 : TYPE(replica_env_type), POINTER :: rep_env
285 : TYPE(neb_type), OPTIONAL, POINTER :: neb_env
286 : TYPE(neb_var_type), POINTER :: coords, vels, forces
287 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
288 : INTEGER, INTENT(IN) :: output_unit
289 : TYPE(section_vals_type), POINTER :: md_section
290 : TYPE(cp_logger_type), POINTER :: logger
291 : TYPE(global_environment_type), POINTER :: globenv
292 :
293 : CHARACTER(len=*), PARAMETER :: routineN = 'neb_md'
294 :
295 : INTEGER :: handle, iatom, ic, is, istep, iw, &
296 : max_steps, natom, shell_index
297 : LOGICAL :: converged, should_stop
298 : REAL(KIND=dp) :: dt
299 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: distances, energies
300 16 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: mass
301 : TYPE(neb_var_type), POINTER :: Dcoords
302 : TYPE(section_vals_type), POINTER :: tc_section, vc_section
303 :
304 16 : CALL timeset(routineN, handle)
305 16 : NULLIFY (Dcoords, tc_section, vc_section)
306 16 : CPASSERT(ASSOCIATED(coords))
307 16 : CPASSERT(ASSOCIATED(vels))
308 : ! MD band for string methods type does not make anywa sense. Stop calculation.
309 16 : IF (neb_env%id_type == do_sm) THEN
310 0 : CPWARN("MD band optimization and String Method incompatible.")
311 : END IF
312 : ! Output unit
313 : iw = cp_print_key_unit_nr(logger, neb_env%neb_section, "REPLICA_INFO", &
314 16 : extension=".replicaLog")
315 16 : tc_section => section_vals_get_subs_vals(md_section, "TEMP_CONTROL")
316 16 : vc_section => section_vals_get_subs_vals(md_section, "VEL_CONTROL")
317 16 : CALL section_vals_val_get(md_section, "TIMESTEP", r_val=dt)
318 16 : CALL section_vals_val_get(md_section, "MAX_STEPS", i_val=max_steps)
319 : ! Initial setup for MD
320 16 : CALL neb_var_create(Dcoords, neb_env)
321 64 : ALLOCATE (mass(SIZE(coords%wrk, 1), neb_env%number_of_replica))
322 48 : ALLOCATE (energies(neb_env%number_of_replica))
323 48 : ALLOCATE (distances(neb_env%number_of_replica - 1))
324 : ! Setting up the mass array
325 16 : IF (neb_env%use_colvar) THEN
326 44 : mass(:, :) = 0.5_dp*dt/massunit
327 : ELSE
328 12 : natom = SIZE(particle_set)
329 216 : DO iatom = 1, natom
330 204 : ic = 3*(iatom - 1)
331 204 : shell_index = particle_set(iatom)%shell_index
332 216 : IF (shell_index == 0) THEN
333 4964 : mass(ic + 1:ic + 3, :) = 0.5_dp*dt/particle_set(iatom)%atomic_kind%mass
334 : ELSE
335 0 : is = 3*(natom + shell_index - 1)
336 0 : mass(ic + 1:ic + 3, :) = 0.5_dp*dt/particle_set(iatom)%atomic_kind%shell%mass_core
337 0 : mass(is + 1:is + 3, :) = 0.5_dp*dt/particle_set(iatom)%atomic_kind%shell%mass_shell
338 : END IF
339 : END DO
340 : END IF
341 : ! Initializing forces array
342 : CALL reorient_images(neb_env%rotate_frames, particle_set, coords, vels, &
343 16 : output_unit, distances, neb_env%number_of_replica)
344 90 : neb_env%avg_distance = SQRT(SUM(distances*distances)/REAL(SIZE(distances), KIND=dp))
345 : CALL neb_calc_energy_forces(rep_env, neb_env, coords, energies, forces, &
346 16 : particle_set, iw)
347 :
348 : CALL dump_neb_info(neb_env=neb_env, &
349 : coords=coords, &
350 : vels=vels, &
351 : forces=forces, &
352 : particle_set=particle_set, &
353 : logger=logger, &
354 : istep=0, &
355 : energies=energies, &
356 : distances=distances, &
357 16 : output_unit=output_unit)
358 176 : md_opt_loop: DO istep = 1, max_steps
359 160 : CALL cp_iterate(logger%iter_info, iter_nr=istep)
360 : ! Save the optimization step counter
361 160 : neb_env%istep = istep
362 : ! Velocity Verlet (first part)
363 83760 : vels%wrk(:, :) = vels%wrk(:, :) + mass(:, :)*forces%wrk(:, :)
364 : ! Control on velocity - I part [rescale, annealing]
365 : CALL control_vels_a(vels, particle_set, tc_section, vc_section, output_unit, &
366 160 : istep)
367 : ! Coordinate step
368 83760 : Dcoords%wrk(:, :) = dt*vels%wrk(:, :)
369 83760 : coords%wrk(:, :) = coords%wrk(:, :) + Dcoords%wrk(:, :)
370 :
371 : CALL reorient_images(neb_env%rotate_frames, particle_set, coords, vels, &
372 160 : output_unit, distances, neb_env%number_of_replica)
373 900 : neb_env%avg_distance = SQRT(SUM(distances*distances)/REAL(SIZE(distances), KIND=dp))
374 : CALL neb_calc_energy_forces(rep_env, neb_env, coords, energies, forces, &
375 160 : particle_set, iw)
376 : ! Check for an external exit command
377 160 : CALL external_control(should_stop, "NEB", globenv=globenv)
378 160 : IF (should_stop) EXIT md_opt_loop
379 : ! Control on velocity - II part [check vels VS forces, Steepest Descent like]
380 160 : CALL control_vels_b(vels, forces, vc_section)
381 : ! Velocity Verlet (second part)
382 83760 : vels%wrk(:, :) = vels%wrk(:, :) + mass(:, :)*forces%wrk(:, :)
383 : ! Dump Infos
384 : CALL dump_neb_info(neb_env=neb_env, &
385 : coords=coords, &
386 : vels=vels, &
387 : forces=forces, &
388 : particle_set=particle_set, &
389 : logger=logger, &
390 : istep=istep, &
391 : energies=energies, &
392 : distances=distances, &
393 160 : output_unit=output_unit)
394 160 : converged = check_convergence(neb_env, Dcoords, forces, logger)
395 336 : IF (converged) THEN
396 0 : IF (output_unit > 0) THEN
397 0 : WRITE (UNIT=output_unit, FMT="(/,T2,A)") REPEAT("*", 79)
398 : WRITE (UNIT=output_unit, FMT="(T2,A,T32,A,T78,A)") &
399 0 : "***", "BAND TASK COMPLETED", "***"
400 0 : WRITE (UNIT=output_unit, FMT="(T2,A)") REPEAT("*", 79)
401 : END IF
402 : EXIT md_opt_loop
403 : END IF
404 : END DO md_opt_loop
405 16 : CALL dump_neb_final(neb_env, energies, coords, particle_set, logger, output_unit, converged)
406 16 : DEALLOCATE (mass)
407 16 : DEALLOCATE (energies)
408 16 : DEALLOCATE (distances)
409 16 : CALL neb_var_release(Dcoords)
410 : CALL cp_print_key_finished_output(iw, logger, neb_env%neb_section, &
411 16 : "REPLICA_INFO")
412 16 : CALL timestop(handle)
413 :
414 32 : END SUBROUTINE neb_md
415 :
416 : ! **************************************************************************************************
417 : !> \brief DIIS type optimization NEB
418 : !> \param rep_env ...
419 : !> \param neb_env ...
420 : !> \param coords ...
421 : !> \param vels ...
422 : !> \param forces ...
423 : !> \param particle_set ...
424 : !> \param output_unit ...
425 : !> \param diis_section ...
426 : !> \param logger ...
427 : !> \param globenv ...
428 : !> \author Teodoro Laino 09.2006
429 : ! **************************************************************************************************
430 24 : SUBROUTINE neb_diis(rep_env, neb_env, coords, vels, forces, particle_set, output_unit, &
431 : diis_section, logger, globenv)
432 : TYPE(replica_env_type), POINTER :: rep_env
433 : TYPE(neb_type), OPTIONAL, POINTER :: neb_env
434 : TYPE(neb_var_type), POINTER :: coords, vels, forces
435 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
436 : INTEGER, INTENT(IN) :: output_unit
437 : TYPE(section_vals_type), POINTER :: diis_section
438 : TYPE(cp_logger_type), POINTER :: logger
439 : TYPE(global_environment_type), POINTER :: globenv
440 :
441 : CHARACTER(len=*), PARAMETER :: routineN = 'neb_diis'
442 :
443 : INTEGER :: handle, istep, iw, iw2, max_sd_steps, &
444 : max_steps, n_diis
445 24 : INTEGER, DIMENSION(:), POINTER :: set_err
446 : LOGICAL :: check_diis, converged, diis_on, do_ls, &
447 : should_stop, skip_ls
448 : REAL(KIND=dp) :: max_stepsize, norm, stepsize, stepsize0
449 24 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: distances, energies
450 24 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: crr, err
451 : TYPE(neb_var_type), POINTER :: sline
452 :
453 24 : CALL timeset(routineN, handle)
454 24 : NULLIFY (sline, crr, err)
455 24 : neb_env%opt_type_label = "SD"
456 24 : do_ls = .TRUE.
457 24 : CPASSERT(ASSOCIATED(coords))
458 24 : CPASSERT(ASSOCIATED(vels))
459 24 : CPASSERT(ASSOCIATED(forces))
460 : iw = cp_print_key_unit_nr(logger, neb_env%neb_section, "REPLICA_INFO", &
461 24 : extension=".replicaLog")
462 24 : CALL section_vals_val_get(diis_section, "MAX_STEPS", i_val=max_steps)
463 24 : CALL section_vals_val_get(diis_section, "N_DIIS", i_val=n_diis)
464 24 : CALL section_vals_val_get(diis_section, "STEPSIZE", r_val=stepsize0)
465 24 : CALL section_vals_val_get(diis_section, "MAX_STEPSIZE", r_val=max_stepsize)
466 24 : CALL section_vals_val_get(diis_section, "NO_LS", l_val=skip_ls)
467 24 : CALL section_vals_val_get(diis_section, "MAX_SD_STEPS", i_val=max_sd_steps)
468 24 : CALL section_vals_val_get(diis_section, "CHECK_DIIS", l_val=check_diis)
469 24 : converged = .FALSE.
470 : iw2 = cp_print_key_unit_nr(logger, diis_section, "DIIS_INFO", &
471 24 : extension=".diisLog")
472 : ! Initial setup for DIIS
473 24 : stepsize = stepsize0
474 : ! Allocate type for Line Search direction
475 24 : CALL neb_var_create(sline, neb_env, full_allocation=.TRUE.)
476 : ! Array of error vectors
477 144 : ALLOCATE (err(PRODUCT(coords%size_wrk), n_diis))
478 144 : ALLOCATE (crr(PRODUCT(coords%size_wrk), n_diis))
479 72 : ALLOCATE (set_err(n_diis))
480 72 : ALLOCATE (energies(neb_env%number_of_replica))
481 72 : ALLOCATE (distances(neb_env%number_of_replica - 1))
482 : ! Initializing forces array
483 : CALL reorient_images(neb_env%rotate_frames, particle_set, coords, vels, &
484 24 : output_unit, distances, neb_env%number_of_replica)
485 : CALL reparametrize_images(neb_env%reparametrize_frames, neb_env%spline_order, &
486 24 : neb_env%smoothing, coords%wrk, sline%wrk, distances)
487 150 : neb_env%avg_distance = SQRT(SUM(distances*distances)/REAL(SIZE(distances), KIND=dp))
488 : CALL neb_calc_energy_forces(rep_env, neb_env, coords, energies, forces, &
489 24 : particle_set, iw)
490 : ! Dump Infos
491 : CALL dump_neb_info(neb_env=neb_env, &
492 : coords=coords, &
493 : forces=forces, &
494 : particle_set=particle_set, &
495 : logger=logger, &
496 : istep=0, &
497 : energies=energies, &
498 : distances=distances, &
499 : vels=vels, &
500 24 : output_unit=output_unit)
501 : ! If rotation is requested let's apply it at the beginning of the
502 : ! Geometry optimization and then let's disable it
503 24 : neb_env%rotate_frames = .FALSE.
504 : ! Main SD/DIIS loop
505 152 : set_err = -1
506 412 : DO istep = 1, max_steps
507 392 : CALL cp_iterate(logger%iter_info, iter_nr=istep)
508 392 : neb_env%opt_type_label = "SD"
509 : ! Save the optimization step counter
510 392 : neb_env%istep = istep
511 : ! Perform one step of SD with line search
512 520664 : norm = SQRT(SUM(forces%wrk*forces%wrk))
513 392 : IF (norm < EPSILON(0.0_dp)) THEN
514 : ! Let's handle the case in which the band is already fully optimized
515 0 : converged = .TRUE.
516 0 : EXIT
517 : END IF
518 1040936 : sline%wrk = forces%wrk/norm
519 392 : IF (do_ls .AND. (.NOT. skip_ls)) THEN
520 : CALL neb_ls(stepsize, sline, rep_env, neb_env, coords, energies, forces, &
521 150 : vels, particle_set, iw, output_unit, distances, diis_section, iw2)
522 150 : IF (iw2 > 0) THEN
523 0 : WRITE (iw2, '(T2,A,T69,F12.6)') "SD| Stepsize in SD after linesearch", &
524 0 : stepsize
525 : END IF
526 : ELSE
527 242 : stepsize = MIN(norm*stepsize0, max_stepsize)
528 242 : IF (iw2 > 0) THEN
529 0 : WRITE (iw2, '(T2,A,T69,F12.6)') "SD| Stepsize in SD no linesearch performed", &
530 0 : stepsize
531 : END IF
532 : END IF
533 520664 : sline%wrk = stepsize*sline%wrk
534 : diis_on = accept_diis_step(istep > max_sd_steps, n_diis, err, crr, set_err, sline, coords, &
535 392 : check_diis, iw2)
536 392 : IF (neb_env%solid_state) THEN
537 52 : sline%wrk(:, 1) = 0.0_dp
538 52 : sline%wrk(:, neb_env%number_of_replica) = 0.0_dp
539 : END IF
540 392 : IF (diis_on) THEN
541 146 : neb_env%opt_type_label = "DIIS"
542 : END IF
543 392 : do_ls = .TRUE.
544 2042 : IF (COUNT(set_err == -1) == 1) do_ls = .FALSE.
545 1040936 : coords%wrk = coords%wrk + sline%wrk
546 : ! Compute forces
547 : CALL reorient_images(neb_env%rotate_frames, particle_set, coords, vels, &
548 392 : output_unit, distances, neb_env%number_of_replica)
549 : CALL reparametrize_images(neb_env%reparametrize_frames, neb_env%spline_order, &
550 392 : neb_env%smoothing, coords%wrk, sline%wrk, distances)
551 2496 : neb_env%avg_distance = SQRT(SUM(distances*distances)/REAL(SIZE(distances), KIND=dp))
552 : CALL neb_calc_energy_forces(rep_env, neb_env, coords, energies, forces, &
553 392 : particle_set, iw)
554 : ! Check for an external exit command
555 392 : CALL external_control(should_stop, "NEB", globenv=globenv)
556 392 : IF (should_stop) EXIT
557 : ! Dump Infos
558 : CALL dump_neb_info(neb_env=neb_env, &
559 : coords=coords, &
560 : forces=forces, &
561 : particle_set=particle_set, &
562 : logger=logger, &
563 : istep=istep, &
564 : energies=energies, &
565 : distances=distances, &
566 : vels=vels, &
567 392 : output_unit=output_unit)
568 :
569 392 : converged = check_convergence(neb_env, sline, forces, logger)
570 804 : IF (converged) THEN
571 4 : IF (output_unit > 0) THEN
572 2 : WRITE (UNIT=output_unit, FMT="(/,T2,A)") REPEAT("*", 79)
573 : WRITE (UNIT=output_unit, FMT="(T2,A,T32,A,T78,A)") &
574 2 : "***", "BAND TASK COMPLETED", "***"
575 2 : WRITE (UNIT=output_unit, FMT="(T2,A)") REPEAT("*", 79)
576 : END IF
577 : EXIT
578 : END IF
579 : END DO
580 24 : CALL dump_neb_final(neb_env, energies, coords, particle_set, logger, output_unit, converged)
581 24 : DEALLOCATE (energies)
582 24 : DEALLOCATE (distances)
583 24 : DEALLOCATE (err)
584 24 : DEALLOCATE (crr)
585 24 : DEALLOCATE (set_err)
586 24 : CALL neb_var_release(sline)
587 : CALL cp_print_key_finished_output(iw, logger, neb_env%neb_section, &
588 24 : "REPLICA_INFO")
589 24 : CALL timestop(handle)
590 72 : END SUBROUTINE neb_diis
591 :
592 : END MODULE neb_methods
|