(git:a90b72f)
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, &
21 USE cp_output_handling, ONLY: cp_iterate, &
25 USE force_env_types, ONLY: force_env_get, &
28 USE gopt_f_methods, ONLY: gopt_f_ii, &
29 gopt_f_io, &
34 USE gopt_f_types, ONLY: gopt_f_type
42 USE kinds, ONLY: dp
43 USE machine, ONLY: m_walltime
46 print_spgr, &
50#include "../base/base_uses.f90"
51
52 IMPLICIT NONE
53 PRIVATE
54
55
56! **************************************************************************************************
57!> \brief evaluate the potential energy and its gradients using an array
58!> with same dimension as the particle_set
59!> \param gopt_env the geometry optimization environment
60!> \param x the position where the function should be evaluated
61!> \param f the function value
62!> \param gradient the value of its gradient
63!> \par History
64!> none
65!> \author Teodoro Laino [tlaino] - University of Zurich - 01.2008
66! **************************************************************************************************
67INTERFACE
68
69 SUBROUTINE cp_eval_at(gopt_env, x, f, gradient, master, &
70 final_evaluation, para_env)
71
73 USE gopt_f_types, ONLY: gopt_f_type
74 USE kinds, ONLY: dp
75
76 TYPE(gopt_f_type), POINTER :: gopt_env
77 REAL(KIND=dp), DIMENSION(:), POINTER :: x
78 REAL(KIND=dp), INTENT(out), OPTIONAL :: f
79 REAL(KIND=dp), DIMENSION(:), OPTIONAL, &
80 POINTER :: gradient
81 INTEGER, INTENT(IN) :: master
82 LOGICAL, INTENT(IN), OPTIONAL :: final_evaluation
83 TYPE(mp_para_env_type), POINTER :: para_env
84
85 END SUBROUTINE cp_eval_at
86
87END INTERFACE
88
89 PUBLIC :: geoopt_cg
90 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
91 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cg_optimizer'
92
93CONTAINS
94
95! **************************************************************************************************
96!> \brief Driver for conjugate gradient optimization technique
97!> \param force_env ...
98!> \param gopt_param ...
99!> \param globenv ...
100!> \param geo_section ...
101!> \param gopt_env ...
102!> \param x0 ...
103!> \param do_update ...
104!> \par History
105!> 10.2005 created [tlaino]
106!> \author Teodoro Laino
107! **************************************************************************************************
108 RECURSIVE SUBROUTINE geoopt_cg(force_env, gopt_param, globenv, geo_section, &
109 gopt_env, x0, do_update)
110
111 TYPE(force_env_type), POINTER :: force_env
112 TYPE(gopt_param_type), POINTER :: gopt_param
113 TYPE(global_environment_type), POINTER :: globenv
114 TYPE(section_vals_type), POINTER :: geo_section
115 TYPE(gopt_f_type), POINTER :: gopt_env
116 REAL(kind=dp), DIMENSION(:), POINTER :: x0
117 LOGICAL, INTENT(OUT), OPTIONAL :: do_update
118
119 CHARACTER(len=*), PARAMETER :: routinen = 'geoopt_cg'
120
121 INTEGER :: handle, output_unit
122 LOGICAL :: my_do_update
123 TYPE(cp_logger_type), POINTER :: logger
124 TYPE(cp_subsys_type), POINTER :: subsys
125 TYPE(spgr_type), POINTER :: spgr
126
127 CALL timeset(routinen, handle)
128
129 NULLIFY (spgr)
130 logger => cp_get_default_logger()
131 spgr => gopt_env%spgr
132
133 output_unit = cp_print_key_unit_nr(logger, geo_section, "PRINT%PROGRAM_RUN_INFO", &
134 extension=".geoLog")
135 CALL print_geo_opt_header(gopt_env, output_unit, "CONJUGATE GRADIENTS")
136
137 ! find space_group
138 CALL force_env_get(force_env, subsys=subsys)
139 CALL section_vals_val_get(geo_section, "KEEP_SPACE_GROUP", l_val=spgr%keep_space_group)
140 IF (spgr%keep_space_group) THEN
141 SELECT CASE (gopt_env%type_id)
143 CALL force_env_get(force_env, subsys=subsys)
144 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
145 CALL spgr_apply_rotations_coord(spgr, x0)
146 CALL print_spgr(spgr)
148 CALL force_env_get(force_env, subsys=subsys)
149 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
150 CALL spgr_apply_rotations_coord(spgr, x0)
151 CALL print_spgr(spgr)
152 CASE DEFAULT
153 spgr%keep_space_group = .false.
154 END SELECT
155 END IF
156
157 CALL cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
158 gopt_env, do_update=my_do_update)
159
160 ! show space_group
161 CALL section_vals_val_get(geo_section, "SHOW_SPACE_GROUP", l_val=spgr%show_space_group)
162 IF (spgr%show_space_group) THEN
163 IF (spgr%keep_space_group) THEN
164 CALL force_env_get(force_env, subsys=subsys)
165 END IF
166 CALL identify_space_group(subsys, geo_section, gopt_env, output_unit)
167 CALL print_spgr(spgr)
168 END IF
169
170 CALL cp_print_key_finished_output(output_unit, logger, geo_section, &
171 "PRINT%PROGRAM_RUN_INFO")
172 IF (PRESENT(do_update)) do_update = my_do_update
173
174 CALL timestop(handle)
175
176 END SUBROUTINE geoopt_cg
177
178! **************************************************************************************************
179!> \brief This really performs the conjugate gradients optimization
180!> \param force_env ...
181!> \param x0 ...
182!> \param gopt_param ...
183!> \param output_unit ...
184!> \param globenv ...
185!> \param gopt_env ...
186!> \param do_update ...
187!> \par History
188!> 10.2005 created [tlaino]
189!> \author Teodoro Laino
190! **************************************************************************************************
191 RECURSIVE SUBROUTINE cp_cg_main(force_env, x0, gopt_param, output_unit, globenv, &
192 gopt_env, do_update)
193 TYPE(force_env_type), POINTER :: force_env
194 REAL(kind=dp), DIMENSION(:), POINTER :: x0
195 TYPE(gopt_param_type), POINTER :: gopt_param
196 INTEGER, INTENT(IN) :: output_unit
197 TYPE(global_environment_type), POINTER :: globenv
198 TYPE(gopt_f_type), POINTER :: gopt_env
199 LOGICAL, INTENT(OUT), OPTIONAL :: do_update
200
201 CHARACTER(len=*), PARAMETER :: routinen = 'cp_cg_main'
202
203 CHARACTER(LEN=5) :: wildcard
204 INTEGER :: handle, iter_nr, its, max_steep_steps, &
205 maxiter
206 LOGICAL :: conv, fletcher_reeves, &
207 save_consistent_energy_force, &
208 should_stop
209 REAL(kind=dp) :: emin, eold, opt_energy, res_lim, t_diff, &
210 t_now, t_old
211 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: xold
212 REAL(kind=dp), DIMENSION(:), POINTER :: g, h, xi
213 TYPE(cell_type), POINTER :: cell
214 TYPE(cp_logger_type), POINTER :: logger
215 TYPE(cp_subsys_type), POINTER :: subsys
216 TYPE(section_vals_type), POINTER :: root_section
217 TYPE(spgr_type), POINTER :: spgr
218
219 CALL timeset(routinen, handle)
220 t_old = m_walltime()
221 NULLIFY (logger, g, h, xi, spgr)
222 root_section => force_env%root_section
223 logger => cp_get_default_logger()
224 conv = .false.
225 maxiter = gopt_param%max_iter
226 max_steep_steps = gopt_param%max_steep_steps
227 fletcher_reeves = gopt_param%Fletcher_Reeves
228 res_lim = gopt_param%restart_limit
229 ALLOCATE (g(SIZE(x0)))
230 ALLOCATE (h(SIZE(x0)))
231 ALLOCATE (xi(SIZE(x0)))
232 ALLOCATE (xold(SIZE(x0)))
233 CALL force_env_get(force_env, cell=cell, subsys=subsys)
234
235 spgr => gopt_env%spgr
236 ! applies rotation matrices to coordinates
237 IF (spgr%keep_space_group) THEN
238 CALL spgr_apply_rotations_coord(spgr, x0)
239 END IF
240
241 ! Evaluate energy and forces at the first step
242 ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
243 save_consistent_energy_force = gopt_env%require_consistent_energy_force
244 gopt_env%require_consistent_energy_force = .false.
245
246 CALL cp_eval_at(gopt_env, x0, opt_energy, xi, gopt_env%force_env%para_env%mepos, &
247 .false., gopt_env%force_env%para_env)
248
249 gopt_env%require_consistent_energy_force = save_consistent_energy_force
250
251 ! Symmetrize coordinates and forces
252 IF (spgr%keep_space_group) THEN
253 CALL spgr_apply_rotations_coord(spgr, x0)
254 CALL spgr_apply_rotations_force(spgr, xi)
255 END IF
256
257 g = -xi
258 h = g
259 xi = h
260 emin = huge(0.0_dp)
261 CALL cp_iterate(logger%iter_info, increment=0, iter_nr_out=iter_nr)
262 ! Main Loop
263 wildcard = " SD"
264 t_now = m_walltime()
265 t_diff = t_now - t_old
266 t_old = t_now
267 CALL gopt_f_io_init(gopt_env, output_unit, opt_energy, wildcard, used_time=t_diff, its=iter_nr)
268 eold = opt_energy
269 DO its = iter_nr + 1, maxiter
270 CALL cp_iterate(logger%iter_info, last=(its == maxiter))
271 CALL section_vals_val_set(gopt_env%geo_section, "STEP_START_VAL", i_val=its)
272 CALL gopt_f_ii(its, output_unit)
273
274 ! Symmetrize coordinates and forces
275 IF (spgr%keep_space_group) THEN
276 CALL spgr_apply_rotations_coord(spgr, x0)
277 CALL spgr_apply_rotations_force(spgr, g)
278 CALL spgr_apply_rotations_force(spgr, xi)
279 END IF
280
281 xold(:) = x0
282
283 ! Line minimization
284 CALL cg_linmin(gopt_env, x0, xi, g, opt_energy, output_unit, gopt_param, globenv)
285
286 ! Applies rotation matrices to coordinates
287 IF (spgr%keep_space_group) THEN
288 CALL spgr_apply_rotations_coord(spgr, x0)
289 END IF
290
291 ! Check for an external exit command
292 CALL external_control(should_stop, "GEO", globenv=globenv)
293 IF (should_stop) EXIT
294
295 ! Some IO and Convergence check
296 t_now = m_walltime()
297 t_diff = t_now - t_old
298 t_old = t_now
299 CALL gopt_f_io(gopt_env, force_env, root_section, its, opt_energy, &
300 output_unit, eold, emin, wildcard, gopt_param, SIZE(x0), x0 - xold, xi, conv, &
301 used_time=t_diff)
302 eold = opt_energy
303 emin = min(emin, opt_energy)
304
305 IF (conv .OR. (its == maxiter)) EXIT
306 ![NB] consistent energies and forces not required for CG, but some line minimizers might set it
307 save_consistent_energy_force = gopt_env%require_consistent_energy_force
308 gopt_env%require_consistent_energy_force = .false.
309
310 CALL cp_eval_at(gopt_env, x0, opt_energy, xi, gopt_env%force_env%para_env%mepos, &
311 .false., gopt_env%force_env%para_env)
312
313 gopt_env%require_consistent_energy_force = save_consistent_energy_force
314
315 ! Symmetrize coordinates and forces
316 IF (spgr%keep_space_group) THEN
317 CALL spgr_apply_rotations_force(spgr, xi)
318 END IF
319
320 ! Get Conjugate Directions: updates the searching direction (h)
321 wildcard = " CG"
322 CALL get_conjugate_direction(gopt_env, fletcher_reeves, g, xi, h)
323
324 ! Symmetrize coordinates and forces
325 IF (spgr%keep_space_group) THEN
326 CALL spgr_apply_rotations_force(spgr, g)
327 CALL spgr_apply_rotations_force(spgr, h)
328 END IF
329
330 ! Reset Condition or Steepest Descent Requested
331 ! ABS(DOT_PRODUCT(g, h))/SQRT((DOT_PRODUCT(g, g)*DOT_PRODUCT(h, h))) > res_lim ...
332 IF ((dot_product(g, h)*dot_product(g, h)) > (res_lim*res_lim*dot_product(g, g)*dot_product(h, h)) &
333 .OR. its + 1 <= max_steep_steps) THEN
334 ! Steepest Descent
335 wildcard = " SD"
336 h = -xi
337 END IF
338 g = -xi
339 xi = h
340 END DO
341
342 IF (its == maxiter .AND. (.NOT. conv)) THEN
343 CALL print_geo_opt_nc(gopt_env, output_unit)
344 END IF
345
346 ! Write final particle information and restart, if converged
347 IF (PRESENT(do_update)) do_update = conv
348 CALL cp_iterate(logger%iter_info, last=.true., increment=0)
349 CALL gopt_f_io_finalize(gopt_env, force_env, x0, conv, its, root_section, &
350 gopt_env%force_env%para_env, gopt_env%force_env%para_env%mepos, output_unit)
351
352 DEALLOCATE (xold)
353 DEALLOCATE (g)
354 DEALLOCATE (h)
355 DEALLOCATE (xi)
356
357 CALL timestop(handle)
358
359 END SUBROUTINE cp_cg_main
360
361END MODULE cg_optimizer
subroutine 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...
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:1068
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:93
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 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
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns 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
Interface to the message passing library MPI.
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
stores all the informations relevant to an mpi environment