(git:f2a7cfd)
Loading...
Searching...
No Matches
cg_optimizer.F
Go to the documentation of this file.
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! **************************************************************************************************
14
15 USE cell_types, ONLY: cell_type
16 USE cg_utils, ONLY: cg_linmin,&
28 USE gopt_f_methods, ONLY: cp_eval_at,&
29 gopt_f_ii,&
30 gopt_f_io,&
35 USE gopt_f_types, ONLY: gopt_f_type
42 USE kinds, ONLY: dp
43 USE machine, ONLY: m_walltime
49#include "../base/base_uses.f90"
50
51 IMPLICIT NONE
52 PRIVATE
53
54 PUBLIC :: geoopt_cg
55 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
56 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cg_optimizer'
57
58CONTAINS
59
60! **************************************************************************************************
61!> \brief Driver for conjugate gradient optimization technique
62!> \param force_env ...
63!> \param gopt_param ...
64!> \param globenv ...
65!> \param geo_section ...
66!> \param gopt_env ...
67!> \param x0 ...
68!> \param do_update ...
69!> \par History
70!> 10.2005 created [tlaino]
71!> \author Teodoro Laino
72! **************************************************************************************************
73 RECURSIVE SUBROUTINE geoopt_cg(force_env, gopt_param, globenv, geo_section, &
74 gopt_env, x0, do_update)
75
76 TYPE(force_env_type), POINTER :: force_env
77 TYPE(gopt_param_type), POINTER :: gopt_param
78 TYPE(global_environment_type), POINTER :: globenv
79 TYPE(section_vals_type), POINTER :: geo_section
80 TYPE(gopt_f_type), POINTER :: gopt_env
81 REAL(kind=dp), DIMENSION(:), POINTER :: x0
82 LOGICAL, INTENT(OUT), OPTIONAL :: do_update
83
84 CHARACTER(len=*), PARAMETER :: routinen = 'geoopt_cg'
85
86 INTEGER :: handle, output_unit
87 LOGICAL :: my_do_update
88 TYPE(cp_logger_type), POINTER :: logger
89 TYPE(cp_subsys_type), POINTER :: subsys
90 TYPE(spgr_type), POINTER :: spgr
91
92 CALL timeset(routinen, handle)
93
94 NULLIFY (spgr)
95 logger => cp_get_default_logger()
96 spgr => gopt_env%spgr
97
98 output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
99 extension=".geoLog")
100 CALL print_geo_opt_header(gopt_env, output_unit, "CONJUGATE GRADIENTS")
101
102 ! find space_group
103 CALL force_env_get(force_env, subsys=subsys)
104 IF (spgr%keep_space_group) THEN
105 SELECT CASE (gopt_env%type_id)
107 CALL force_env_get(force_env, subsys=subsys)
108 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
109 CALL spgr_apply_rotations_coord(spgr, x0)
110 CALL print_spgr(spgr)
111 CASE DEFAULT
112 spgr%keep_space_group = .false.
113 END SELECT
114 END IF
115
116 CALL cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
117 gopt_env, do_update=my_do_update)
118
119 ! show space_group
120 IF (spgr%show_space_group) THEN
121 IF (spgr%keep_space_group) THEN
122 CALL force_env_get(force_env, subsys=subsys)
123 END IF
124 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
125 CALL print_spgr(spgr)
126 END IF
127
128 CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
129 "PRINT%PROGRAM_RUN_INFO")
130 IF (PRESENT(do_update)) do_update = my_do_update
131
132 CALL timestop(handle)
133
134 END SUBROUTINE geoopt_cg
135
136! **************************************************************************************************
137!> \brief This really performs the conjugate gradients optimization
138!> \param force_env ...
139!> \param x0 ...
140!> \param gopt_param ...
141!> \param output_unit ...
142!> \param globenv ...
143!> \param gopt_env ...
144!> \param do_update ...
145!> \par History
146!> 10.2005 created [tlaino]
147!> \author Teodoro Laino
148! **************************************************************************************************
149 RECURSIVE SUBROUTINE cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
150 gopt_env, do_update)
151 TYPE(force_env_type), POINTER :: force_env
152 REAL(kind=dp), DIMENSION(:), POINTER :: x0
153 TYPE(gopt_param_type), POINTER :: gopt_param
154 INTEGER, INTENT(IN) :: output_unit
155 TYPE(global_environment_type), POINTER :: globenv
156 TYPE(gopt_f_type), POINTER :: gopt_env
157 LOGICAL, INTENT(OUT), OPTIONAL :: do_update
158
159 CHARACTER(len=*), PARAMETER :: routinen = 'cp_cg_main'
160
161 CHARACTER(LEN=5) :: wildcard
162 INTEGER :: handle, iter_nr, its, max_steep_steps, &
163 maxiter
164 LOGICAL :: conv, evaluate_before_io, &
165 fletcher_reeves, &
166 save_consistent_energy_force, &
167 should_stop
168 REAL(kind=dp) :: emin, eold, opt_energy, res_lim, t_diff, &
169 t_now, t_old
170 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: xold
171 REAL(kind=dp), DIMENSION(:), POINTER :: g, h, xi
172 TYPE(cell_type), POINTER :: cell
173 TYPE(cp_logger_type), POINTER :: logger
174 TYPE(cp_subsys_type), POINTER :: subsys
175 TYPE(section_vals_type), POINTER :: root_section
176 TYPE(spgr_type), POINTER :: spgr
177
178 CALL timeset(routinen, handle)
179 t_old = m_walltime()
180 NULLIFY (logger, g, h, xi, spgr)
181 root_section => force_env%root_section
182 logger => cp_get_default_logger()
183 conv = .false.
184 maxiter = gopt_param%max_iter
185 max_steep_steps = gopt_param%max_steep_steps
186 fletcher_reeves = gopt_param%Fletcher_Reeves
187 ! Dimer evaluations update the rotational state, so retain their original timing.
188 evaluate_before_io = gopt_env%type_id /= default_ts_method_id
189 res_lim = gopt_param%restart_limit
190 ALLOCATE (g(SIZE(x0)))
191 ALLOCATE (h(SIZE(x0)))
192 ALLOCATE (xi(SIZE(x0)))
193 ALLOCATE (xold(SIZE(x0)))
194 CALL force_env_get(force_env, cell=cell, subsys=subsys)
195
196 spgr => gopt_env%spgr
197 ! applies rotation matrices to coordinates
198 IF (spgr%keep_space_group) THEN
199 CALL spgr_apply_rotations_coord(spgr, x0)
200 END IF
201
202 ! Evaluate energy and forces at the first step
203 ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
204 save_consistent_energy_force = gopt_env%require_consistent_energy_force
205 gopt_env%require_consistent_energy_force = .false.
206
207 CALL cp_eval_at(gopt_env, x0, opt_energy, xi, master=gopt_env%force_env%para_env%mepos, &
208 para_env=gopt_env%force_env%para_env)
209
210 gopt_env%require_consistent_energy_force = save_consistent_energy_force
211
212 ! Symmetrize coordinates and forces
213 IF (spgr%keep_space_group) THEN
214 CALL spgr_apply_rotations_coord(spgr, x0)
215 CALL spgr_apply_rotations_force(spgr, xi)
216 END IF
217
218 g = -xi
219 h = g
220 xi = h
221 emin = huge(0.0_dp)
222 CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
223 ! Main Loop
224 wildcard = " SD"
225 t_now = m_walltime()
226 t_diff = t_now - t_old
227 t_old = t_now
228 CALL gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, used_time=t_diff, its=iter_nr)
229 eold = opt_energy
230 DO its = iter_nr + 1, maxiter
231 CALL cp_iterate(logger%iter_info, last=(its == maxiter))
232 CALL section_vals_val_set(gopt_env%geo_section, "STEP_START_VAL", i_val=its)
233 CALL gopt_f_ii(its, output_unit)
234
235 ! Symmetrize coordinates and forces
236 IF (spgr%keep_space_group) THEN
237 CALL spgr_apply_rotations_coord(spgr, x0)
238 CALL spgr_apply_rotations_force(spgr, g)
239 CALL spgr_apply_rotations_force(spgr, xi)
240 END IF
241
242 xold(:) = x0
243
244 ! Line minimization
245 CALL cg_linmin(gopt_env, x0, xi, g, opt_energy, output_unit, gopt_param, globenv)
246
247 ! Applies rotation matrices to coordinates
248 IF (spgr%keep_space_group) THEN
249 CALL spgr_apply_rotations_coord(spgr, x0)
250 END IF
251
252 IF (evaluate_before_io) THEN
253 ! Keep the accepted point and the physical state consistent even on external stop.
254 CALL cg_eval_current(gopt_env, x0, opt_energy, xi, spgr)
255 END IF
256
257 ! Check for an external exit command
258 CALL external_control(should_stop, "GEO", globenv=globenv)
259 IF (should_stop) EXIT
260
261 ! Some IO and Convergence check
262 t_now = m_walltime()
263 t_diff = t_now - t_old
264 t_old = t_now
265 CALL gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, &
266 output_unit, eold, emin, wildcard, gopt_param, SIZE(x0), x0 - xold, xi, conv, &
267 used_time=t_diff)
268 eold = opt_energy
269 emin = min(emin, opt_energy)
270
271 IF (conv .OR. (its == maxiter)) EXIT
272 IF (.NOT. evaluate_before_io) THEN
273 CALL cg_eval_current(gopt_env, x0, opt_energy, xi, spgr)
274 END IF
275
276 ! Get Conjugate Directions: updates the searching direction (h)
277 wildcard = " CG"
278 CALL get_conjugate_direction(gopt_env, fletcher_reeves, g, xi, h)
279
280 ! Symmetrize coordinates and forces
281 IF (spgr%keep_space_group) THEN
282 CALL spgr_apply_rotations_force(spgr, g)
283 CALL spgr_apply_rotations_force(spgr, h)
284 END IF
285
286 ! Reset Condition or Steepest Descent Requested
287 ! ABS(DOT_PRODUCT(g, h))/SQRT((DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h))) > res_lim ...
288 IF ((dot_product(g, h)*dot_product(g, h)) > (res_lim*res_lim*dot_product(g, g)*dot_product(h, h)) &
289 .OR. its + 1 <= max_steep_steps) THEN
290 ! Steepest Descent
291 wildcard = " SD"
292 h = -xi
293 END IF
294 g = -xi
295 xi = h
296 END DO
297
298 IF (its == maxiter .AND. (.NOT. conv)) THEN
299 CALL print_geo_opt_nc(gopt_env, output_unit)
300 END IF
301
302 ! Write final particle information and restart, if converged
303 IF (PRESENT(do_update)) do_update = conv
304 CALL cp_iterate(logger%iter_info, last=.true., increment=0)
305 CALL gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, &
306 gopt_env%force_env%para_env, gopt_env%force_env%para_env%mepos, output_unit)
307
308 DEALLOCATE (xold)
309 DEALLOCATE (g)
310 DEALLOCATE (h)
311 DEALLOCATE (xi)
312
313 CALL timestop(handle)
314
315 END SUBROUTINE cp_cg_main
316
317! **************************************************************************************************
318!> \brief Evaluate the current CG point with the force-consistency setting used by CG.
319!> \param gopt_env Optimization environment
320!> \param x Current coordinates, possibly projected by cp_eval_at
321!> \param energy Energy at the evaluated coordinates
322!> \param gradient Gradient at the evaluated coordinates
323!> \param spgr Space-group data
324! **************************************************************************************************
325 SUBROUTINE cg_eval_current(gopt_env, x, energy, gradient, spgr)
326 TYPE(gopt_f_type), POINTER :: gopt_env
327 REAL(kind=dp), DIMENSION(:), POINTER :: x
328 REAL(kind=dp), INTENT(OUT) :: energy
329 REAL(kind=dp), DIMENSION(:), POINTER :: gradient
330 TYPE(spgr_type), POINTER :: spgr
331
332 LOGICAL :: save_consistent_energy_force
333
334 save_consistent_energy_force = gopt_env%require_consistent_energy_force
335 gopt_env%require_consistent_energy_force = .false.
336 CALL cp_eval_at(gopt_env, x, energy, gradient, master=gopt_env%force_env%para_env%mepos, &
337 para_env=gopt_env%force_env%para_env)
338 gopt_env%require_consistent_energy_force = save_consistent_energy_force
339
340 IF (spgr%keep_space_group) CALL spgr_apply_rotations_force(spgr, gradient)
341 END SUBROUTINE cg_eval_current
342
343END MODULE cg_optimizer
Handles all functions related to the CELL.
Definition cell_types.F:15
Routines for Geometry optimization using Conjugate Gradients.
recursive subroutine, public geoopt_cg(force_env, gopt_param, globenv, geo_section, gopt_env, x0, do_update)
Driver for conjugate gradient optimization technique.
Utilities for Geometry optimization using Conjugate Gradients.
Definition cg_utils.F:13
subroutine, public get_conjugate_direction(gopt_env, fletcher_reeves, g, xi, h)
Computes the Conjugate direction for the next search.
Definition cg_utils.F:996
recursive subroutine, public cg_linmin(gopt_env, xvec, xi, g, opt_energy, output_unit, gopt_param, globenv)
Main driver for line minimization routines for CG.
Definition cg_utils.F:59
Routines to handle the external control of CP2K.
subroutine, public external_control(should_stop, flag, globenv, target_time, start_time, force_check)
External manipulations during a run : when the <PROJECT_NAME>.EXIT_$runtype command is sent the progr...
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
subroutine, public cp_iterate(iteration_info, last, iter_nr, increment, iter_nr_out)
adds one to the actual iteration
types that represent a subsys, i.e. a part of the system
Interface for the force calculations.
recursive subroutine, public force_env_get(force_env, in_use, fist_env, qs_env, meta_env, fp_env, subsys, para_env, potential_energy, additional_potential, kinetic_energy, harmonic_shell, kinetic_shell, cell, sub_force_env, qmmm_env, qmmmx_env, eip_env, pwdft_env, globenv, input, force_env_section, method_name_id, root_section, mixed_env, nnp_env, embed_env, ipi_env)
returns various attributes about the force environment
Define type storing the global information of a run. Keep the amount of stored data small....
contains a functional that calculates the energy and its derivatives for the geometry optimizer
subroutine, public print_geo_opt_header(gopt_env, output_unit, label)
...
subroutine, public gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, its, used_time)
Handles the Output during an optimization run.
recursive subroutine, public gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, para_env, master, output_unit)
Handles the Output at the end of an optimization run.
subroutine, public gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, output_unit, eold, emin, wildcard, gopt_param, ndf, dx, xi, conv, pred, rat, step, rad, used_time)
Handles the Output during an optimization run.
subroutine, public print_geo_opt_nc(gopt_env, output_unit)
...
subroutine, public cp_eval_at(gopt_env, x, f, gradient, master, final_evaluation, para_env)
evaluete the potential energy and its gradients using an array with same dimension as the particle_se...
subroutine, public gopt_f_ii(its, output_unit)
Prints iteration step of the optimization procedure on screen.
contains a functional that calculates the energy and its derivatives for the geometry optimizer
contains typo and related routines to handle parameters controlling the GEO_OPT module
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public default_cell_method_id
integer, parameter, public default_minimization_method_id
integer, parameter, public default_ts_method_id
objects that represent the structure of input sections and the data contained in an input section
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Definition machine.F:141
Space Group Symmetry Type Module (version 1.0, Ferbruary 12, 2021).
Space Group Symmetry Module (version 1.0, January 16, 2020).
subroutine, public print_spgr(spgr)
routine prints Space Group Information.
subroutine, public spgr_apply_rotations_coord(spgr, coord)
routine applies the rotation matrices to the coordinates.
subroutine, public identify_space_group(subsys, geo_section, gopt_env, iunit)
routine indentifies the space group and finds rotation matrices.
subroutine, public spgr_apply_rotations_force(spgr, force)
routine applies the rotation matrices to the forces.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
type of a logger, at the moment it contains just a print level starting at which level it should be l...
represents a system: atoms, molecules, their pos,vel,...
wrapper to abstract the force evaluation of the various methods
contains the initially parsed file and the initial parallel environment
calculates the potential energy of a system, and its derivatives