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 Routines for Geometry optimization using Conjugate Gradients
10 : !> \author Teodoro Laino [teo]
11 : !> 10.2005
12 : ! **************************************************************************************************
13 : MODULE cg_optimizer
14 :
15 : USE cell_types, ONLY: cell_type
16 : USE cg_utils, ONLY: cg_linmin,&
17 : get_conjugate_direction
18 : USE cp_external_control, ONLY: external_control
19 : USE cp_log_handling, ONLY: cp_get_default_logger,&
20 : cp_logger_type
21 : USE cp_output_handling, ONLY: cp_iterate,&
22 : cp_print_key_finished_output,&
23 : cp_print_key_unit_nr
24 : USE cp_subsys_types, ONLY: cp_subsys_type
25 : USE force_env_types, ONLY: force_env_get,&
26 : force_env_type
27 : USE global_types, ONLY: global_environment_type
28 : USE gopt_f_methods, ONLY: &
29 : cp_eval_at, gopt_f_ii, gopt_f_io, gopt_f_io_finalize, gopt_f_io_init, &
30 : gopt_f_project_coord, gopt_f_project_vector, print_geo_opt_header, print_geo_opt_nc
31 : USE gopt_f_types, ONLY: gopt_f_type
32 : USE gopt_param_types, ONLY: gopt_param_type
33 : USE input_constants, ONLY: default_ts_method_id
34 : USE input_section_types, ONLY: section_vals_type,&
35 : section_vals_val_set
36 : USE kinds, ONLY: dp
37 : USE machine, ONLY: m_walltime
38 : #include "../base/base_uses.f90"
39 :
40 : IMPLICIT NONE
41 : PRIVATE
42 :
43 : PUBLIC :: geoopt_cg
44 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cg_optimizer'
45 :
46 : CONTAINS
47 :
48 : ! **************************************************************************************************
49 : !> \brief Driver for conjugate gradient optimization technique
50 : !> \param force_env ...
51 : !> \param gopt_param ...
52 : !> \param globenv ...
53 : !> \param geo_section ...
54 : !> \param gopt_env ...
55 : !> \param x0 ...
56 : !> \param do_update ...
57 : !> \par History
58 : !> 10.2005 created [tlaino]
59 : !> \author Teodoro Laino
60 : ! **************************************************************************************************
61 556 : RECURSIVE SUBROUTINE geoopt_cg(force_env, gopt_param, globenv, geo_section, &
62 : gopt_env, x0, do_update)
63 :
64 : TYPE(force_env_type), POINTER :: force_env
65 : TYPE(gopt_param_type), POINTER :: gopt_param
66 : TYPE(global_environment_type), POINTER :: globenv
67 : TYPE(section_vals_type), POINTER :: geo_section
68 : TYPE(gopt_f_type), POINTER :: gopt_env
69 : REAL(KIND=dp), DIMENSION(:), POINTER :: x0
70 : LOGICAL, INTENT(OUT), OPTIONAL :: do_update
71 :
72 : CHARACTER(len=*), PARAMETER :: routineN = 'geoopt_cg'
73 :
74 : INTEGER :: handle, output_unit
75 : LOGICAL :: my_do_update
76 : TYPE(cp_logger_type), POINTER :: logger
77 :
78 278 : CALL timeset(routineN, handle)
79 :
80 278 : logger => cp_get_default_logger()
81 :
82 : output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
83 278 : extension=".geoLog")
84 278 : CALL print_geo_opt_header(gopt_env, output_unit, "CONJUGATE GRADIENTS")
85 :
86 : CALL cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
87 278 : gopt_env, do_update=my_do_update)
88 :
89 : CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
90 278 : "PRINT%PROGRAM_RUN_INFO")
91 278 : IF (PRESENT(do_update)) do_update = my_do_update
92 :
93 278 : CALL timestop(handle)
94 :
95 278 : END SUBROUTINE geoopt_cg
96 :
97 : ! **************************************************************************************************
98 : !> \brief This really performs the conjugate gradients optimization
99 : !> \param force_env ...
100 : !> \param x0 ...
101 : !> \param gopt_param ...
102 : !> \param output_unit ...
103 : !> \param globenv ...
104 : !> \param gopt_env ...
105 : !> \param do_update ...
106 : !> \par History
107 : !> 10.2005 created [tlaino]
108 : !> \author Teodoro Laino
109 : ! **************************************************************************************************
110 278 : RECURSIVE SUBROUTINE cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
111 : gopt_env, do_update)
112 : TYPE(force_env_type), POINTER :: force_env
113 : REAL(KIND=dp), DIMENSION(:), POINTER :: x0
114 : TYPE(gopt_param_type), POINTER :: gopt_param
115 : INTEGER, INTENT(IN) :: output_unit
116 : TYPE(global_environment_type), POINTER :: globenv
117 : TYPE(gopt_f_type), POINTER :: gopt_env
118 : LOGICAL, INTENT(OUT), OPTIONAL :: do_update
119 :
120 : CHARACTER(len=*), PARAMETER :: routineN = 'cp_cg_main'
121 :
122 : CHARACTER(LEN=5) :: wildcard
123 : INTEGER :: handle, iter_nr, its, max_steep_steps, &
124 : maxiter
125 : LOGICAL :: conv, evaluate_before_io, &
126 : Fletcher_Reeves, &
127 : save_consistent_energy_force, &
128 : should_stop
129 : REAL(KIND=dp) :: emin, eold, opt_energy, res_lim, t_diff, &
130 : t_now, t_old
131 278 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: xold
132 278 : REAL(KIND=dp), DIMENSION(:), POINTER :: g, h, xi
133 : TYPE(cell_type), POINTER :: cell
134 : TYPE(cp_logger_type), POINTER :: logger
135 : TYPE(cp_subsys_type), POINTER :: subsys
136 : TYPE(section_vals_type), POINTER :: root_section
137 :
138 278 : CALL timeset(routineN, handle)
139 278 : t_old = m_walltime()
140 278 : NULLIFY (logger, g, h, xi)
141 278 : root_section => force_env%root_section
142 278 : logger => cp_get_default_logger()
143 278 : conv = .FALSE.
144 278 : maxiter = gopt_param%max_iter
145 278 : max_steep_steps = gopt_param%max_steep_steps
146 278 : Fletcher_Reeves = gopt_param%Fletcher_Reeves
147 : ! Dimer evaluations update the rotational state, so retain their original timing.
148 : ! For solid-state translation, the curvature check needs the accepted geometry.
149 : evaluate_before_io = gopt_env%type_id /= default_ts_method_id .OR. &
150 278 : (gopt_env%solid_state_dimer .AND. .NOT. gopt_env%dimer_rotation)
151 278 : res_lim = gopt_param%restart_limit
152 834 : ALLOCATE (g(SIZE(x0)))
153 556 : ALLOCATE (h(SIZE(x0)))
154 556 : ALLOCATE (xi(SIZE(x0)))
155 556 : ALLOCATE (xold(SIZE(x0)))
156 278 : CALL force_env_get(force_env, cell=cell, subsys=subsys)
157 :
158 : ! applies rotation matrices to coordinates
159 278 : CALL gopt_f_project_coord(gopt_env, x0)
160 :
161 : ! Evaluate energy and forces at the first step
162 : ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
163 278 : save_consistent_energy_force = gopt_env%require_consistent_energy_force
164 278 : gopt_env%require_consistent_energy_force = .FALSE.
165 :
166 : CALL cp_eval_at(gopt_env, x0, opt_energy, xi, master=gopt_env%force_env%para_env%mepos, &
167 278 : para_env=gopt_env%force_env%para_env)
168 :
169 278 : gopt_env%require_consistent_energy_force = save_consistent_energy_force
170 :
171 : ! Symmetrize coordinates and forces
172 278 : CALL gopt_f_project_coord(gopt_env, x0)
173 278 : CALL gopt_f_project_vector(gopt_env, xi)
174 :
175 145028 : g = -xi
176 145028 : h = g
177 145028 : xi = h
178 278 : emin = HUGE(0.0_dp)
179 278 : CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
180 : ! Main Loop
181 278 : wildcard = " SD"
182 278 : t_now = m_walltime()
183 278 : t_diff = t_now - t_old
184 278 : t_old = t_now
185 278 : CALL gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, used_time=t_diff, its=iter_nr)
186 278 : eold = opt_energy
187 2100 : DO its = iter_nr + 1, maxiter
188 2100 : CALL cp_iterate(logger%iter_info, last=(its == maxiter))
189 2100 : CALL section_vals_val_set(gopt_env%geo_section, "STEP_START_VAL", i_val=its)
190 2100 : CALL gopt_f_ii(its, output_unit)
191 :
192 : ! Symmetrize coordinates and forces
193 2100 : CALL gopt_f_project_coord(gopt_env, x0)
194 2100 : CALL gopt_f_project_vector(gopt_env, g)
195 2100 : CALL gopt_f_project_vector(gopt_env, xi)
196 :
197 387442 : xold(:) = x0
198 :
199 : ! Line minimization
200 2100 : CALL cg_linmin(gopt_env, x0, xi, g, opt_energy, output_unit, gopt_param, globenv)
201 :
202 : ! Applies rotation matrices to coordinates
203 2100 : CALL gopt_f_project_coord(gopt_env, x0)
204 :
205 2100 : IF (evaluate_before_io) THEN
206 : ! Keep the accepted point and the physical state consistent even on external stop.
207 1240 : CALL cg_eval_current(gopt_env, x0, opt_energy, xi)
208 : END IF
209 :
210 : ! Check for an external exit command
211 2100 : CALL external_control(should_stop, "GEO", globenv=globenv)
212 2100 : IF (should_stop) EXIT
213 :
214 : ! Some IO and Convergence check
215 2100 : t_now = m_walltime()
216 2100 : t_diff = t_now - t_old
217 2100 : t_old = t_now
218 : CALL gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, &
219 : output_unit, eold, emin, wildcard, gopt_param, SIZE(x0), x0 - xold, xi, conv, &
220 387442 : used_time=t_diff)
221 2100 : eold = opt_energy
222 2100 : emin = MIN(emin, opt_energy)
223 :
224 2100 : IF (conv .OR. (its == maxiter)) EXIT
225 1822 : IF (.NOT. evaluate_before_io) THEN
226 700 : CALL cg_eval_current(gopt_env, x0, opt_energy, xi)
227 : END IF
228 :
229 : ! Get Conjugate Directions: updates the searching direction (h)
230 1822 : wildcard = " CG"
231 1822 : CALL get_conjugate_direction(gopt_env, Fletcher_Reeves, g, xi, h)
232 :
233 : ! Symmetrize coordinates and forces
234 1822 : CALL gopt_f_project_vector(gopt_env, g)
235 1822 : CALL gopt_f_project_vector(gopt_env, h)
236 :
237 : ! Reset Condition or Steepest Descent Requested
238 : ! ABS(DOT_PRODUCT(g, h))/SQRT((DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h))) > res_lim ...
239 : IF ((DOT_PRODUCT(g, h)*DOT_PRODUCT(g, h)) > (res_lim*res_lim*DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h)) &
240 942962 : .OR. its + 1 <= max_steep_steps) THEN
241 : ! Steepest Descent
242 568 : wildcard = " SD"
243 108448 : h = -xi
244 : END IF
245 629856 : g = -xi
246 632234 : xi = h
247 : END DO
248 :
249 278 : IF (its == maxiter .AND. (.NOT. conv)) THEN
250 82 : CALL print_geo_opt_nc(gopt_env, output_unit)
251 : END IF
252 :
253 : ! Write final particle information and restart, if converged
254 278 : IF (PRESENT(do_update)) do_update = conv
255 278 : CALL cp_iterate(logger%iter_info, last=.TRUE., increment=0)
256 : CALL gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, &
257 278 : gopt_env%force_env%para_env, gopt_env%force_env%para_env%mepos, output_unit)
258 :
259 278 : DEALLOCATE (xold)
260 278 : DEALLOCATE (g)
261 278 : DEALLOCATE (h)
262 278 : DEALLOCATE (xi)
263 :
264 278 : CALL timestop(handle)
265 :
266 556 : END SUBROUTINE cp_cg_main
267 :
268 : ! **************************************************************************************************
269 : !> \brief Evaluate the current CG point with the force-consistency setting used by CG.
270 : !> \param gopt_env Optimization environment
271 : !> \param x Current coordinates, possibly projected by cp_eval_at
272 : !> \param energy Energy at the evaluated coordinates
273 : !> \param gradient Gradient at the evaluated coordinates
274 : ! **************************************************************************************************
275 1940 : SUBROUTINE cg_eval_current(gopt_env, x, energy, gradient)
276 : TYPE(gopt_f_type), POINTER :: gopt_env
277 : REAL(KIND=dp), DIMENSION(:), POINTER :: x
278 : REAL(KIND=dp), INTENT(OUT) :: energy
279 : REAL(KIND=dp), DIMENSION(:), POINTER :: gradient
280 :
281 : LOGICAL :: save_consistent_energy_force
282 :
283 1940 : save_consistent_energy_force = gopt_env%require_consistent_energy_force
284 1940 : gopt_env%require_consistent_energy_force = .FALSE.
285 : CALL cp_eval_at(gopt_env, x, energy, gradient, master=gopt_env%force_env%para_env%mepos, &
286 1940 : para_env=gopt_env%force_env%para_env)
287 1940 : gopt_env%require_consistent_energy_force = save_consistent_energy_force
288 :
289 1940 : CALL gopt_f_project_vector(gopt_env, gradient)
290 1940 : END SUBROUTINE cg_eval_current
291 :
292 : END MODULE cg_optimizer
|